blob: 2757cda391bbf2f852b49ef381d5b29e6ae9f6ae [file] [log] [blame]
Sinclair Yeh35c05122015-06-26 01:42:06 -07001/******************************************************************************
2 *
Sinclair Yeh54fbde82015-07-29 12:38:02 -07003 * COPYRIGHT © 2014-2015 VMware, Inc., Palo Alto, CA., USA
Sinclair Yeh35c05122015-06-26 01:42:06 -07004 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 * USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 ******************************************************************************/
27
28#include "vmwgfx_kms.h"
Sinclair Yeh8ce75f82015-07-08 21:20:39 -070029#include "device_include/svga3d_surfacedefs.h"
Sinclair Yeh35c05122015-06-26 01:42:06 -070030#include <drm/drm_plane_helper.h>
Sinclair Yehd7721ca2017-03-23 11:48:44 -070031#include <drm/drm_atomic.h>
32#include <drm/drm_atomic_helper.h>
33
Sinclair Yeh35c05122015-06-26 01:42:06 -070034
35#define vmw_crtc_to_stdu(x) \
36 container_of(x, struct vmw_screen_target_display_unit, base.crtc)
37#define vmw_encoder_to_stdu(x) \
38 container_of(x, struct vmw_screen_target_display_unit, base.encoder)
39#define vmw_connector_to_stdu(x) \
40 container_of(x, struct vmw_screen_target_display_unit, base.connector)
41
42
43
44enum stdu_content_type {
45 SAME_AS_DISPLAY = 0,
46 SEPARATE_SURFACE,
47 SEPARATE_DMA
48};
49
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -070050/**
51 * struct vmw_stdu_dirty - closure structure for the update functions
52 *
53 * @base: The base type we derive from. Used by vmw_kms_helper_dirty().
54 * @transfer: Transfer direction for DMA command.
55 * @left: Left side of bounding box.
56 * @right: Right side of bounding box.
57 * @top: Top side of bounding box.
58 * @bottom: Bottom side of bounding box.
59 * @buf: DMA buffer when DMA-ing between buffer and screen targets.
60 * @sid: Surface ID when copying between surface and screen targets.
61 */
62struct vmw_stdu_dirty {
63 struct vmw_kms_dirty base;
64 SVGA3dTransferType transfer;
65 s32 left, right, top, bottom;
66 u32 pitch;
67 union {
68 struct vmw_dma_buffer *buf;
69 u32 sid;
70 };
71};
72
73/*
74 * SVGA commands that are used by this code. Please see the device headers
75 * for explanation.
76 */
77struct vmw_stdu_update {
78 SVGA3dCmdHeader header;
79 SVGA3dCmdUpdateGBScreenTarget body;
80};
81
82struct vmw_stdu_dma {
83 SVGA3dCmdHeader header;
84 SVGA3dCmdSurfaceDMA body;
85};
86
87struct vmw_stdu_surface_copy {
88 SVGA3dCmdHeader header;
89 SVGA3dCmdSurfaceCopy body;
90};
Sinclair Yeh35c05122015-06-26 01:42:06 -070091
92
93/**
94 * struct vmw_screen_target_display_unit
95 *
96 * @base: VMW specific DU structure
97 * @display_srf: surface to be displayed. The dimension of this will always
98 * match the display mode. If the display mode matches
99 * content_vfbs dimensions, then this is a pointer into the
100 * corresponding field in content_vfbs. If not, then this
101 * is a separate buffer to which content_vfbs will blit to.
Sinclair Yeh35c05122015-06-26 01:42:06 -0700102 * @content_type: content_fb type
103 * @defined: true if the current display unit has been initialized
104 */
105struct vmw_screen_target_display_unit {
106 struct vmw_display_unit base;
Sinclair Yeh904bb5e2017-03-23 14:29:22 -0700107 const struct vmw_surface *display_srf;
Sinclair Yeh35c05122015-06-26 01:42:06 -0700108 enum stdu_content_type content_fb_type;
Sinclair Yeh9aa8dca2017-03-23 14:40:04 -0700109 s32 display_width, display_height;
Sinclair Yeh35c05122015-06-26 01:42:06 -0700110
111 bool defined;
112};
113
114
115
116static void vmw_stdu_destroy(struct vmw_screen_target_display_unit *stdu);
117
118
119
120/******************************************************************************
Sinclair Yeh35c05122015-06-26 01:42:06 -0700121 * Screen Target Display Unit CRTC Functions
122 *****************************************************************************/
123
124
125/**
126 * vmw_stdu_crtc_destroy - cleans up the STDU
127 *
128 * @crtc: used to get a reference to the containing STDU
129 */
130static void vmw_stdu_crtc_destroy(struct drm_crtc *crtc)
131{
132 vmw_stdu_destroy(vmw_crtc_to_stdu(crtc));
133}
134
Sinclair Yeh35c05122015-06-26 01:42:06 -0700135/**
136 * vmw_stdu_define_st - Defines a Screen Target
137 *
138 * @dev_priv: VMW DRM device
139 * @stdu: display unit to create a Screen Target for
Thomas Hellstromb1097ae2016-02-12 08:54:07 +0100140 * @mode: The mode to set.
141 * @crtc_x: X coordinate of screen target relative to framebuffer origin.
142 * @crtc_y: Y coordinate of screen target relative to framebuffer origin.
Sinclair Yeh35c05122015-06-26 01:42:06 -0700143 *
144 * Creates a STDU that we can used later. This function is called whenever the
145 * framebuffer size changes.
146 *
147 * RETURNs:
148 * 0 on success, error code on failure
149 */
150static int vmw_stdu_define_st(struct vmw_private *dev_priv,
Thomas Hellstromb1097ae2016-02-12 08:54:07 +0100151 struct vmw_screen_target_display_unit *stdu,
152 struct drm_display_mode *mode,
153 int crtc_x, int crtc_y)
Sinclair Yeh35c05122015-06-26 01:42:06 -0700154{
155 struct {
156 SVGA3dCmdHeader header;
157 SVGA3dCmdDefineGBScreenTarget body;
158 } *cmd;
159
160 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
161
162 if (unlikely(cmd == NULL)) {
163 DRM_ERROR("Out of FIFO space defining Screen Target\n");
164 return -ENOMEM;
165 }
166
167 cmd->header.id = SVGA_3D_CMD_DEFINE_GB_SCREENTARGET;
168 cmd->header.size = sizeof(cmd->body);
169
170 cmd->body.stid = stdu->base.unit;
Thomas Hellstromb1097ae2016-02-12 08:54:07 +0100171 cmd->body.width = mode->hdisplay;
172 cmd->body.height = mode->vdisplay;
Sinclair Yeh35c05122015-06-26 01:42:06 -0700173 cmd->body.flags = (0 == cmd->body.stid) ? SVGA_STFLAG_PRIMARY : 0;
174 cmd->body.dpi = 0;
Thomas Hellstromb1097ae2016-02-12 08:54:07 +0100175 if (stdu->base.is_implicit) {
176 cmd->body.xRoot = crtc_x;
177 cmd->body.yRoot = crtc_y;
178 } else {
Sinclair Yeh35c05122015-06-26 01:42:06 -0700179 cmd->body.xRoot = stdu->base.gui_x;
180 cmd->body.yRoot = stdu->base.gui_y;
181 }
Thomas Hellstrom6dd687b2016-02-12 09:57:15 +0100182 stdu->base.set_gui_x = cmd->body.xRoot;
183 stdu->base.set_gui_y = cmd->body.yRoot;
Sinclair Yeh35c05122015-06-26 01:42:06 -0700184
185 vmw_fifo_commit(dev_priv, sizeof(*cmd));
186
187 stdu->defined = true;
Sinclair Yeh9aa8dca2017-03-23 14:40:04 -0700188 stdu->display_width = mode->hdisplay;
189 stdu->display_height = mode->vdisplay;
Sinclair Yeh35c05122015-06-26 01:42:06 -0700190
191 return 0;
192}
193
194
195
196/**
197 * vmw_stdu_bind_st - Binds a surface to a Screen Target
198 *
199 * @dev_priv: VMW DRM device
200 * @stdu: display unit affected
201 * @res: Buffer to bind to the screen target. Set to NULL to blank screen.
202 *
203 * Binding a surface to a Screen Target the same as flipping
204 */
205static int vmw_stdu_bind_st(struct vmw_private *dev_priv,
206 struct vmw_screen_target_display_unit *stdu,
Sinclair Yeh904bb5e2017-03-23 14:29:22 -0700207 const struct vmw_resource *res)
Sinclair Yeh35c05122015-06-26 01:42:06 -0700208{
209 SVGA3dSurfaceImageId image;
210
211 struct {
212 SVGA3dCmdHeader header;
213 SVGA3dCmdBindGBScreenTarget body;
214 } *cmd;
215
216
217 if (!stdu->defined) {
218 DRM_ERROR("No screen target defined\n");
219 return -EINVAL;
220 }
221
222 /* Set up image using information in vfb */
223 memset(&image, 0, sizeof(image));
224 image.sid = res ? res->id : SVGA3D_INVALID_ID;
225
226 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
227
228 if (unlikely(cmd == NULL)) {
229 DRM_ERROR("Out of FIFO space binding a screen target\n");
230 return -ENOMEM;
231 }
232
233 cmd->header.id = SVGA_3D_CMD_BIND_GB_SCREENTARGET;
234 cmd->header.size = sizeof(cmd->body);
235
236 cmd->body.stid = stdu->base.unit;
237 cmd->body.image = image;
238
239 vmw_fifo_commit(dev_priv, sizeof(*cmd));
240
241 return 0;
242}
243
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -0700244/**
245 * vmw_stdu_populate_update - populate an UPDATE_GB_SCREENTARGET command with a
246 * bounding box.
247 *
248 * @cmd: Pointer to command stream.
249 * @unit: Screen target unit.
250 * @left: Left side of bounding box.
251 * @right: Right side of bounding box.
252 * @top: Top side of bounding box.
253 * @bottom: Bottom side of bounding box.
254 */
255static void vmw_stdu_populate_update(void *cmd, int unit,
256 s32 left, s32 right, s32 top, s32 bottom)
257{
258 struct vmw_stdu_update *update = cmd;
Sinclair Yeh35c05122015-06-26 01:42:06 -0700259
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -0700260 update->header.id = SVGA_3D_CMD_UPDATE_GB_SCREENTARGET;
261 update->header.size = sizeof(update->body);
262
263 update->body.stid = unit;
264 update->body.rect.x = left;
265 update->body.rect.y = top;
266 update->body.rect.w = right - left;
267 update->body.rect.h = bottom - top;
268}
Sinclair Yeh35c05122015-06-26 01:42:06 -0700269
270/**
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -0700271 * vmw_stdu_update_st - Full update of a Screen Target
Sinclair Yeh35c05122015-06-26 01:42:06 -0700272 *
273 * @dev_priv: VMW DRM device
Sinclair Yeh35c05122015-06-26 01:42:06 -0700274 * @stdu: display unit affected
Sinclair Yeh35c05122015-06-26 01:42:06 -0700275 *
276 * This function needs to be called whenever the content of a screen
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -0700277 * target has changed completely. Typically as a result of a backing
278 * surface change.
Sinclair Yeh35c05122015-06-26 01:42:06 -0700279 *
280 * RETURNS:
281 * 0 on success, error code on failure
282 */
283static int vmw_stdu_update_st(struct vmw_private *dev_priv,
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -0700284 struct vmw_screen_target_display_unit *stdu)
Sinclair Yeh35c05122015-06-26 01:42:06 -0700285{
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -0700286 struct vmw_stdu_update *cmd;
Sinclair Yeh35c05122015-06-26 01:42:06 -0700287
288 if (!stdu->defined) {
289 DRM_ERROR("No screen target defined");
290 return -EINVAL;
291 }
292
Sinclair Yeh35c05122015-06-26 01:42:06 -0700293 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
294
295 if (unlikely(cmd == NULL)) {
296 DRM_ERROR("Out of FIFO space updating a Screen Target\n");
297 return -ENOMEM;
298 }
299
Sinclair Yeh9aa8dca2017-03-23 14:40:04 -0700300 vmw_stdu_populate_update(cmd, stdu->base.unit,
301 0, stdu->display_width,
302 0, stdu->display_height);
Sinclair Yeh35c05122015-06-26 01:42:06 -0700303
304 vmw_fifo_commit(dev_priv, sizeof(*cmd));
305
306 return 0;
307}
308
309
310
311/**
312 * vmw_stdu_destroy_st - Destroy a Screen Target
313 *
314 * @dev_priv: VMW DRM device
315 * @stdu: display unit to destroy
316 */
317static int vmw_stdu_destroy_st(struct vmw_private *dev_priv,
318 struct vmw_screen_target_display_unit *stdu)
319{
320 int ret;
321
322 struct {
323 SVGA3dCmdHeader header;
324 SVGA3dCmdDestroyGBScreenTarget body;
325 } *cmd;
326
327
328 /* Nothing to do if not successfully defined */
329 if (unlikely(!stdu->defined))
330 return 0;
331
332 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
333
334 if (unlikely(cmd == NULL)) {
335 DRM_ERROR("Out of FIFO space, screen target not destroyed\n");
336 return -ENOMEM;
337 }
338
339 cmd->header.id = SVGA_3D_CMD_DESTROY_GB_SCREENTARGET;
340 cmd->header.size = sizeof(cmd->body);
341
342 cmd->body.stid = stdu->base.unit;
343
344 vmw_fifo_commit(dev_priv, sizeof(*cmd));
345
346 /* Force sync */
347 ret = vmw_fallback_wait(dev_priv, false, true, 0, false, 3*HZ);
348 if (unlikely(ret != 0))
349 DRM_ERROR("Failed to sync with HW");
350
351 stdu->defined = false;
Sinclair Yeh9aa8dca2017-03-23 14:40:04 -0700352 stdu->display_width = 0;
353 stdu->display_height = 0;
Sinclair Yeh35c05122015-06-26 01:42:06 -0700354
355 return ret;
356}
357
Sinclair Yeh06ec4192017-03-23 13:14:54 -0700358
359/**
360 * vmw_stdu_crtc_mode_set_nofb - Updates screen target size
361 *
362 * @crtc: CRTC associated with the screen target
363 *
364 * This function defines/destroys a screen target
365 *
366 */
367static void vmw_stdu_crtc_mode_set_nofb(struct drm_crtc *crtc)
368{
369 struct vmw_private *dev_priv;
370 struct vmw_screen_target_display_unit *stdu;
371 int ret;
372
373
374 stdu = vmw_crtc_to_stdu(crtc);
375 dev_priv = vmw_priv(crtc->dev);
376
377 if (stdu->defined) {
378 ret = vmw_stdu_bind_st(dev_priv, stdu, NULL);
379 if (ret)
380 DRM_ERROR("Failed to blank CRTC\n");
381
382 (void) vmw_stdu_update_st(dev_priv, stdu);
383
384 ret = vmw_stdu_destroy_st(dev_priv, stdu);
385 if (ret)
386 DRM_ERROR("Failed to destroy Screen Target\n");
387
388 stdu->content_fb_type = SAME_AS_DISPLAY;
389 }
390
391 if (!crtc->state->enable)
392 return;
393
394 vmw_svga_enable(dev_priv);
395 ret = vmw_stdu_define_st(dev_priv, stdu, &crtc->mode, crtc->x, crtc->y);
396
397 if (ret)
398 DRM_ERROR("Failed to define Screen Target of size %dx%d\n",
399 crtc->x, crtc->y);
400}
401
402
403static void vmw_stdu_crtc_helper_prepare(struct drm_crtc *crtc)
404{
405}
406
407
408static void vmw_stdu_crtc_helper_commit(struct drm_crtc *crtc)
409{
410 struct vmw_private *dev_priv;
411 struct vmw_screen_target_display_unit *stdu;
412 struct vmw_framebuffer *vfb;
413 struct drm_framebuffer *fb;
414
415
416 stdu = vmw_crtc_to_stdu(crtc);
417 dev_priv = vmw_priv(crtc->dev);
418 fb = crtc->primary->fb;
419
420 vfb = (fb) ? vmw_framebuffer_to_vfb(fb) : NULL;
421
422 if (vfb)
423 vmw_kms_add_active(dev_priv, &stdu->base, vfb);
424 else
425 vmw_kms_del_active(dev_priv, &stdu->base);
426}
427
428static void vmw_stdu_crtc_helper_disable(struct drm_crtc *crtc)
429{
430 struct vmw_private *dev_priv;
431 struct vmw_screen_target_display_unit *stdu;
432 int ret;
433
434
435 if (!crtc) {
436 DRM_ERROR("CRTC is NULL\n");
437 return;
438 }
439
440 stdu = vmw_crtc_to_stdu(crtc);
441 dev_priv = vmw_priv(crtc->dev);
442
443 if (stdu->defined) {
444 ret = vmw_stdu_bind_st(dev_priv, stdu, NULL);
445 if (ret)
446 DRM_ERROR("Failed to blank CRTC\n");
447
448 (void) vmw_stdu_update_st(dev_priv, stdu);
449
450 ret = vmw_stdu_destroy_st(dev_priv, stdu);
451 if (ret)
452 DRM_ERROR("Failed to destroy Screen Target\n");
453
454 stdu->content_fb_type = SAME_AS_DISPLAY;
455 }
456}
457
Sinclair Yeh35c05122015-06-26 01:42:06 -0700458/**
Sinclair Yeh35c05122015-06-26 01:42:06 -0700459 * vmw_stdu_crtc_page_flip - Binds a buffer to a screen target
460 *
461 * @crtc: CRTC to attach FB to
462 * @fb: FB to attach
463 * @event: Event to be posted. This event should've been alloced
464 * using k[mz]alloc, and should've been completely initialized.
465 * @page_flip_flags: Input flags.
466 *
467 * If the STDU uses the same display and content buffers, i.e. a true flip,
468 * this function will replace the existing display buffer with the new content
469 * buffer.
470 *
471 * If the STDU uses different display and content buffers, i.e. a blit, then
472 * only the content buffer will be updated.
473 *
474 * RETURNS:
475 * 0 on success, error code on failure
476 */
477static int vmw_stdu_crtc_page_flip(struct drm_crtc *crtc,
478 struct drm_framebuffer *new_fb,
479 struct drm_pending_vblank_event *event,
480 uint32_t flags)
481
482{
483 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
Sinclair Yeh904bb5e2017-03-23 14:29:22 -0700484 struct vmw_screen_target_display_unit *stdu = vmw_crtc_to_stdu(crtc);
Thomas Hellstromb1097ae2016-02-12 08:54:07 +0100485 struct vmw_framebuffer *vfb = vmw_framebuffer_to_vfb(new_fb);
Sinclair Yeh904bb5e2017-03-23 14:29:22 -0700486 struct drm_vmw_rect vclips;
Sinclair Yeh35c05122015-06-26 01:42:06 -0700487 int ret;
488
Sinclair Yeh35c05122015-06-26 01:42:06 -0700489 dev_priv = vmw_priv(crtc->dev);
490 stdu = vmw_crtc_to_stdu(crtc);
Sinclair Yeh35c05122015-06-26 01:42:06 -0700491
Thomas Hellstrom4d492a02016-02-12 09:01:29 +0100492 if (!stdu->defined || !vmw_kms_crtc_flippable(dev_priv, crtc))
Thomas Hellstromb1097ae2016-02-12 08:54:07 +0100493 return -EINVAL;
Sinclair Yeh35c05122015-06-26 01:42:06 -0700494
Sinclair Yeh904bb5e2017-03-23 14:29:22 -0700495 /*
496 * We're always async, but the helper doesn't know how to set async
497 * so lie to the helper. Also, the helper expects someone
498 * to pick the event up from the crtc state, and if nobody does,
499 * it will free it. Since we handle the event in this function,
500 * don't hand it to the helper.
501 */
502 flags &= ~DRM_MODE_PAGE_FLIP_ASYNC;
503 ret = drm_atomic_helper_page_flip(crtc, new_fb, NULL, flags);
504 if (ret) {
505 DRM_ERROR("Page flip error %d.\n", ret);
Thomas Hellstromb1097ae2016-02-12 08:54:07 +0100506 return ret;
Sinclair Yeh904bb5e2017-03-23 14:29:22 -0700507 }
Sinclair Yeh35c05122015-06-26 01:42:06 -0700508
Thomas Hellstrom4d492a02016-02-12 09:01:29 +0100509 if (stdu->base.is_implicit)
510 vmw_kms_update_implicit_fb(dev_priv, crtc);
511
Sinclair Yeh904bb5e2017-03-23 14:29:22 -0700512 /*
513 * Now that we've bound a new surface to the screen target,
514 * update the contents.
515 */
Thomas Hellstromb1097ae2016-02-12 08:54:07 +0100516 vclips.x = crtc->x;
517 vclips.y = crtc->y;
518 vclips.w = crtc->mode.hdisplay;
519 vclips.h = crtc->mode.vdisplay;
Sinclair Yeh904bb5e2017-03-23 14:29:22 -0700520
Thomas Hellstromb1097ae2016-02-12 08:54:07 +0100521 if (vfb->dmabuf)
522 ret = vmw_kms_stdu_dma(dev_priv, NULL, vfb, NULL, NULL, &vclips,
523 1, 1, true, false);
524 else
525 ret = vmw_kms_stdu_surface_dirty(dev_priv, vfb, NULL, &vclips,
526 NULL, 0, 0, 1, 1, NULL);
Sinclair Yeh904bb5e2017-03-23 14:29:22 -0700527 if (ret) {
528 DRM_ERROR("Page flip update error %d.\n", ret);
Sinclair Yeh35c05122015-06-26 01:42:06 -0700529 return ret;
Sinclair Yeh904bb5e2017-03-23 14:29:22 -0700530 }
Sinclair Yeh35c05122015-06-26 01:42:06 -0700531
532 if (event) {
533 struct vmw_fence_obj *fence = NULL;
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -0700534 struct drm_file *file_priv = event->base.file_priv;
Sinclair Yeh35c05122015-06-26 01:42:06 -0700535
536 vmw_execbuf_fence_commands(NULL, dev_priv, &fence, NULL);
537 if (!fence)
538 return -ENOMEM;
539
540 ret = vmw_event_fence_action_queue(file_priv, fence,
541 &event->base,
542 &event->event.tv_sec,
543 &event->event.tv_usec,
544 true);
545 vmw_fence_obj_unreference(&fence);
Thomas Hellstrom4e0858a2015-11-05 02:18:55 -0800546 } else {
Sinclair Yeh904bb5e2017-03-23 14:29:22 -0700547 (void) vmw_fifo_flush(dev_priv, false);
Sinclair Yeh35c05122015-06-26 01:42:06 -0700548 }
549
Thomas Hellstromb1097ae2016-02-12 08:54:07 +0100550 return 0;
Sinclair Yeh35c05122015-06-26 01:42:06 -0700551}
552
553
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -0700554/**
555 * vmw_stdu_dmabuf_clip - Callback to encode a suface DMA command cliprect
556 *
557 * @dirty: The closure structure.
558 *
559 * Encodes a surface DMA command cliprect and updates the bounding box
560 * for the DMA.
561 */
562static void vmw_stdu_dmabuf_clip(struct vmw_kms_dirty *dirty)
563{
564 struct vmw_stdu_dirty *ddirty =
565 container_of(dirty, struct vmw_stdu_dirty, base);
566 struct vmw_stdu_dma *cmd = dirty->cmd;
567 struct SVGA3dCopyBox *blit = (struct SVGA3dCopyBox *) &cmd[1];
568
569 blit += dirty->num_hits;
570 blit->srcx = dirty->fb_x;
571 blit->srcy = dirty->fb_y;
572 blit->x = dirty->unit_x1;
573 blit->y = dirty->unit_y1;
574 blit->d = 1;
575 blit->w = dirty->unit_x2 - dirty->unit_x1;
576 blit->h = dirty->unit_y2 - dirty->unit_y1;
577 dirty->num_hits++;
578
579 if (ddirty->transfer != SVGA3D_WRITE_HOST_VRAM)
580 return;
581
582 /* Destination bounding box */
583 ddirty->left = min_t(s32, ddirty->left, dirty->unit_x1);
584 ddirty->top = min_t(s32, ddirty->top, dirty->unit_y1);
585 ddirty->right = max_t(s32, ddirty->right, dirty->unit_x2);
586 ddirty->bottom = max_t(s32, ddirty->bottom, dirty->unit_y2);
587}
588
589/**
590 * vmw_stdu_dmabuf_fifo_commit - Callback to fill in and submit a DMA command.
591 *
592 * @dirty: The closure structure.
593 *
594 * Fills in the missing fields in a DMA command, and optionally encodes
595 * a screen target update command, depending on transfer direction.
596 */
597static void vmw_stdu_dmabuf_fifo_commit(struct vmw_kms_dirty *dirty)
598{
599 struct vmw_stdu_dirty *ddirty =
600 container_of(dirty, struct vmw_stdu_dirty, base);
601 struct vmw_screen_target_display_unit *stdu =
602 container_of(dirty->unit, typeof(*stdu), base);
603 struct vmw_stdu_dma *cmd = dirty->cmd;
604 struct SVGA3dCopyBox *blit = (struct SVGA3dCopyBox *) &cmd[1];
605 SVGA3dCmdSurfaceDMASuffix *suffix =
606 (SVGA3dCmdSurfaceDMASuffix *) &blit[dirty->num_hits];
607 size_t blit_size = sizeof(*blit) * dirty->num_hits + sizeof(*suffix);
608
609 if (!dirty->num_hits) {
610 vmw_fifo_commit(dirty->dev_priv, 0);
611 return;
612 }
613
614 cmd->header.id = SVGA_3D_CMD_SURFACE_DMA;
615 cmd->header.size = sizeof(cmd->body) + blit_size;
616 vmw_bo_get_guest_ptr(&ddirty->buf->base, &cmd->body.guest.ptr);
617 cmd->body.guest.pitch = ddirty->pitch;
618 cmd->body.host.sid = stdu->display_srf->res.id;
619 cmd->body.host.face = 0;
620 cmd->body.host.mipmap = 0;
621 cmd->body.transfer = ddirty->transfer;
622 suffix->suffixSize = sizeof(*suffix);
623 suffix->maximumOffset = ddirty->buf->base.num_pages * PAGE_SIZE;
624
625 if (ddirty->transfer == SVGA3D_WRITE_HOST_VRAM) {
626 blit_size += sizeof(struct vmw_stdu_update);
627
628 vmw_stdu_populate_update(&suffix[1], stdu->base.unit,
629 ddirty->left, ddirty->right,
630 ddirty->top, ddirty->bottom);
631 }
632
633 vmw_fifo_commit(dirty->dev_priv, sizeof(*cmd) + blit_size);
634
635 ddirty->left = ddirty->top = S32_MAX;
636 ddirty->right = ddirty->bottom = S32_MIN;
637}
638
639/**
640 * vmw_kms_stdu_dma - Perform a DMA transfer between a dma-buffer backed
641 * framebuffer and the screen target system.
642 *
643 * @dev_priv: Pointer to the device private structure.
644 * @file_priv: Pointer to a struct drm-file identifying the caller. May be
645 * set to NULL, but then @user_fence_rep must also be set to NULL.
646 * @vfb: Pointer to the dma-buffer backed framebuffer.
647 * @clips: Array of clip rects. Either @clips or @vclips must be NULL.
648 * @vclips: Alternate array of clip rects. Either @clips or @vclips must
649 * be NULL.
650 * @num_clips: Number of clip rects in @clips or @vclips.
651 * @increment: Increment to use when looping over @clips or @vclips.
652 * @to_surface: Whether to DMA to the screen target system as opposed to
653 * from the screen target system.
654 * @interruptible: Whether to perform waits interruptible if possible.
655 *
656 * If DMA-ing till the screen target system, the function will also notify
657 * the screen target system that a bounding box of the cliprects has been
658 * updated.
659 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
660 * interrupted.
661 */
662int vmw_kms_stdu_dma(struct vmw_private *dev_priv,
663 struct drm_file *file_priv,
664 struct vmw_framebuffer *vfb,
665 struct drm_vmw_fence_rep __user *user_fence_rep,
666 struct drm_clip_rect *clips,
667 struct drm_vmw_rect *vclips,
668 uint32_t num_clips,
669 int increment,
670 bool to_surface,
671 bool interruptible)
672{
673 struct vmw_dma_buffer *buf =
674 container_of(vfb, struct vmw_framebuffer_dmabuf, base)->buffer;
675 struct vmw_stdu_dirty ddirty;
676 int ret;
677
678 ret = vmw_kms_helper_buffer_prepare(dev_priv, buf, interruptible,
679 false);
680 if (ret)
681 return ret;
682
683 ddirty.transfer = (to_surface) ? SVGA3D_WRITE_HOST_VRAM :
684 SVGA3D_READ_HOST_VRAM;
685 ddirty.left = ddirty.top = S32_MAX;
686 ddirty.right = ddirty.bottom = S32_MIN;
687 ddirty.pitch = vfb->base.pitches[0];
688 ddirty.buf = buf;
689 ddirty.base.fifo_commit = vmw_stdu_dmabuf_fifo_commit;
690 ddirty.base.clip = vmw_stdu_dmabuf_clip;
691 ddirty.base.fifo_reserve_size = sizeof(struct vmw_stdu_dma) +
692 num_clips * sizeof(SVGA3dCopyBox) +
693 sizeof(SVGA3dCmdSurfaceDMASuffix);
694 if (to_surface)
695 ddirty.base.fifo_reserve_size += sizeof(struct vmw_stdu_update);
696
697 ret = vmw_kms_helper_dirty(dev_priv, vfb, clips, vclips,
698 0, 0, num_clips, increment, &ddirty.base);
699 vmw_kms_helper_buffer_finish(dev_priv, file_priv, buf, NULL,
700 user_fence_rep);
701
702 return ret;
703}
704
705/**
706 * vmw_stdu_surface_clip - Callback to encode a surface copy command cliprect
707 *
708 * @dirty: The closure structure.
709 *
710 * Encodes a surface copy command cliprect and updates the bounding box
711 * for the copy.
712 */
713static void vmw_kms_stdu_surface_clip(struct vmw_kms_dirty *dirty)
714{
715 struct vmw_stdu_dirty *sdirty =
716 container_of(dirty, struct vmw_stdu_dirty, base);
717 struct vmw_stdu_surface_copy *cmd = dirty->cmd;
718 struct vmw_screen_target_display_unit *stdu =
719 container_of(dirty->unit, typeof(*stdu), base);
720
721 if (sdirty->sid != stdu->display_srf->res.id) {
722 struct SVGA3dCopyBox *blit = (struct SVGA3dCopyBox *) &cmd[1];
723
724 blit += dirty->num_hits;
725 blit->srcx = dirty->fb_x;
726 blit->srcy = dirty->fb_y;
727 blit->x = dirty->unit_x1;
728 blit->y = dirty->unit_y1;
729 blit->d = 1;
730 blit->w = dirty->unit_x2 - dirty->unit_x1;
731 blit->h = dirty->unit_y2 - dirty->unit_y1;
732 }
733
734 dirty->num_hits++;
735
736 /* Destination bounding box */
737 sdirty->left = min_t(s32, sdirty->left, dirty->unit_x1);
738 sdirty->top = min_t(s32, sdirty->top, dirty->unit_y1);
739 sdirty->right = max_t(s32, sdirty->right, dirty->unit_x2);
740 sdirty->bottom = max_t(s32, sdirty->bottom, dirty->unit_y2);
741}
742
743/**
744 * vmw_stdu_surface_fifo_commit - Callback to fill in and submit a surface
745 * copy command.
746 *
747 * @dirty: The closure structure.
748 *
749 * Fills in the missing fields in a surface copy command, and encodes a screen
750 * target update command.
751 */
752static void vmw_kms_stdu_surface_fifo_commit(struct vmw_kms_dirty *dirty)
753{
754 struct vmw_stdu_dirty *sdirty =
755 container_of(dirty, struct vmw_stdu_dirty, base);
756 struct vmw_screen_target_display_unit *stdu =
757 container_of(dirty->unit, typeof(*stdu), base);
758 struct vmw_stdu_surface_copy *cmd = dirty->cmd;
759 struct vmw_stdu_update *update;
760 size_t blit_size = sizeof(SVGA3dCopyBox) * dirty->num_hits;
761 size_t commit_size;
762
763 if (!dirty->num_hits) {
764 vmw_fifo_commit(dirty->dev_priv, 0);
765 return;
766 }
767
768 if (sdirty->sid != stdu->display_srf->res.id) {
769 struct SVGA3dCopyBox *blit = (struct SVGA3dCopyBox *) &cmd[1];
770
771 cmd->header.id = SVGA_3D_CMD_SURFACE_COPY;
772 cmd->header.size = sizeof(cmd->body) + blit_size;
773 cmd->body.src.sid = sdirty->sid;
774 cmd->body.dest.sid = stdu->display_srf->res.id;
775 update = (struct vmw_stdu_update *) &blit[dirty->num_hits];
776 commit_size = sizeof(*cmd) + blit_size + sizeof(*update);
777 } else {
778 update = dirty->cmd;
779 commit_size = sizeof(*update);
780 }
781
782 vmw_stdu_populate_update(update, stdu->base.unit, sdirty->left,
783 sdirty->right, sdirty->top, sdirty->bottom);
784
785 vmw_fifo_commit(dirty->dev_priv, commit_size);
786
787 sdirty->left = sdirty->top = S32_MAX;
788 sdirty->right = sdirty->bottom = S32_MIN;
789}
790
791/**
792 * vmw_kms_stdu_surface_dirty - Dirty part of a surface backed framebuffer
793 *
794 * @dev_priv: Pointer to the device private structure.
795 * @framebuffer: Pointer to the surface-buffer backed framebuffer.
796 * @clips: Array of clip rects. Either @clips or @vclips must be NULL.
797 * @vclips: Alternate array of clip rects. Either @clips or @vclips must
798 * be NULL.
799 * @srf: Pointer to surface to blit from. If NULL, the surface attached
800 * to @framebuffer will be used.
801 * @dest_x: X coordinate offset to align @srf with framebuffer coordinates.
802 * @dest_y: Y coordinate offset to align @srf with framebuffer coordinates.
803 * @num_clips: Number of clip rects in @clips.
804 * @inc: Increment to use when looping over @clips.
805 * @out_fence: If non-NULL, will return a ref-counted pointer to a
806 * struct vmw_fence_obj. The returned fence pointer may be NULL in which
807 * case the device has already synchronized.
808 *
809 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
810 * interrupted.
811 */
812int vmw_kms_stdu_surface_dirty(struct vmw_private *dev_priv,
813 struct vmw_framebuffer *framebuffer,
814 struct drm_clip_rect *clips,
815 struct drm_vmw_rect *vclips,
816 struct vmw_resource *srf,
817 s32 dest_x,
818 s32 dest_y,
819 unsigned num_clips, int inc,
820 struct vmw_fence_obj **out_fence)
821{
822 struct vmw_framebuffer_surface *vfbs =
823 container_of(framebuffer, typeof(*vfbs), base);
824 struct vmw_stdu_dirty sdirty;
825 int ret;
826
827 if (!srf)
828 srf = &vfbs->surface->res;
829
830 ret = vmw_kms_helper_resource_prepare(srf, true);
831 if (ret)
832 return ret;
833
834 if (vfbs->is_dmabuf_proxy) {
835 ret = vmw_kms_update_proxy(srf, clips, num_clips, inc);
836 if (ret)
837 goto out_finish;
838 }
839
840 sdirty.base.fifo_commit = vmw_kms_stdu_surface_fifo_commit;
841 sdirty.base.clip = vmw_kms_stdu_surface_clip;
842 sdirty.base.fifo_reserve_size = sizeof(struct vmw_stdu_surface_copy) +
843 sizeof(SVGA3dCopyBox) * num_clips +
844 sizeof(struct vmw_stdu_update);
845 sdirty.sid = srf->id;
846 sdirty.left = sdirty.top = S32_MAX;
847 sdirty.right = sdirty.bottom = S32_MIN;
848
849 ret = vmw_kms_helper_dirty(dev_priv, framebuffer, clips, vclips,
850 dest_x, dest_y, num_clips, inc,
851 &sdirty.base);
852out_finish:
853 vmw_kms_helper_resource_finish(srf, out_fence);
854
855 return ret;
856}
857
Sinclair Yeh35c05122015-06-26 01:42:06 -0700858
859/*
860 * Screen Target CRTC dispatch table
861 */
Ville Syrjäläd7955fc2015-12-15 12:21:15 +0100862static const struct drm_crtc_funcs vmw_stdu_crtc_funcs = {
Sinclair Yeh35c05122015-06-26 01:42:06 -0700863 .gamma_set = vmw_du_crtc_gamma_set,
864 .destroy = vmw_stdu_crtc_destroy,
Sinclair Yeh9c2542a2017-03-23 11:33:39 -0700865 .reset = vmw_du_crtc_reset,
866 .atomic_duplicate_state = vmw_du_crtc_duplicate_state,
867 .atomic_destroy_state = vmw_du_crtc_destroy_state,
Sinclair Yeh904bb5e2017-03-23 14:29:22 -0700868 .set_config = vmw_kms_set_config,
Sinclair Yeh35c05122015-06-26 01:42:06 -0700869 .page_flip = vmw_stdu_crtc_page_flip,
870};
871
872
873
874/******************************************************************************
875 * Screen Target Display Unit Encoder Functions
876 *****************************************************************************/
877
878/**
879 * vmw_stdu_encoder_destroy - cleans up the STDU
880 *
881 * @encoder: used the get the containing STDU
882 *
883 * vmwgfx cleans up crtc/encoder/connector all at the same time so technically
884 * this can be a no-op. Nevertheless, it doesn't hurt of have this in case
885 * the common KMS code changes and somehow vmw_stdu_crtc_destroy() doesn't
886 * get called.
887 */
888static void vmw_stdu_encoder_destroy(struct drm_encoder *encoder)
889{
890 vmw_stdu_destroy(vmw_encoder_to_stdu(encoder));
891}
892
Ville Syrjäläd7955fc2015-12-15 12:21:15 +0100893static const struct drm_encoder_funcs vmw_stdu_encoder_funcs = {
Sinclair Yeh35c05122015-06-26 01:42:06 -0700894 .destroy = vmw_stdu_encoder_destroy,
895};
896
897
898
899/******************************************************************************
900 * Screen Target Display Unit Connector Functions
901 *****************************************************************************/
902
903/**
904 * vmw_stdu_connector_destroy - cleans up the STDU
905 *
906 * @connector: used to get the containing STDU
907 *
908 * vmwgfx cleans up crtc/encoder/connector all at the same time so technically
909 * this can be a no-op. Nevertheless, it doesn't hurt of have this in case
910 * the common KMS code changes and somehow vmw_stdu_crtc_destroy() doesn't
911 * get called.
912 */
913static void vmw_stdu_connector_destroy(struct drm_connector *connector)
914{
915 vmw_stdu_destroy(vmw_connector_to_stdu(connector));
916}
917
918
919
Ville Syrjäläd7955fc2015-12-15 12:21:15 +0100920static const struct drm_connector_funcs vmw_stdu_connector_funcs = {
Sinclair Yeh35c05122015-06-26 01:42:06 -0700921 .dpms = vmw_du_connector_dpms,
Sinclair Yeh35c05122015-06-26 01:42:06 -0700922 .detect = vmw_du_connector_detect,
923 .fill_modes = vmw_du_connector_fill_modes,
924 .set_property = vmw_du_connector_set_property,
925 .destroy = vmw_stdu_connector_destroy,
Sinclair Yehd7721ca2017-03-23 11:48:44 -0700926 .reset = vmw_du_connector_reset,
927 .atomic_duplicate_state = vmw_du_connector_duplicate_state,
928 .atomic_destroy_state = vmw_du_connector_destroy_state,
929 .atomic_set_property = vmw_du_connector_atomic_set_property,
930 .atomic_get_property = vmw_du_connector_atomic_get_property,
Sinclair Yeh35c05122015-06-26 01:42:06 -0700931};
932
933
Sinclair Yehd947d1b2017-03-23 14:23:20 -0700934static const struct
935drm_connector_helper_funcs vmw_stdu_connector_helper_funcs = {
936 .best_encoder = drm_atomic_helper_best_encoder,
937};
938
939
Sinclair Yeh35c05122015-06-26 01:42:06 -0700940
Sinclair Yeh36cc79b2017-03-23 11:28:11 -0700941/******************************************************************************
942 * Screen Target Display Plane Functions
943 *****************************************************************************/
944
Sinclair Yeh060e2ad2017-03-23 14:18:32 -0700945
946
947/**
948 * vmw_stdu_primary_plane_cleanup_fb - Unpins the display surface
949 *
950 * @plane: display plane
951 * @old_state: Contains the FB to clean up
952 *
953 * Unpins the display surface
954 *
955 * Returns 0 on success
956 */
957static void
958vmw_stdu_primary_plane_cleanup_fb(struct drm_plane *plane,
959 struct drm_plane_state *old_state)
960{
961 struct vmw_plane_state *vps = vmw_plane_state_to_vps(old_state);
962
963 if (vps->surf)
964 WARN_ON(!vps->pinned);
965
966 vmw_du_plane_cleanup_fb(plane, old_state);
967
968 vps->content_fb_type = SAME_AS_DISPLAY;
969}
970
971
972
973/**
974 * vmw_stdu_primary_plane_prepare_fb - Readies the display surface
975 *
976 * @plane: display plane
977 * @new_state: info on the new plane state, including the FB
978 *
979 * This function allocates a new display surface if the content is
980 * backed by a DMA. The display surface is pinned here, and it'll
981 * be unpinned in .cleanup_fb()
982 *
983 * Returns 0 on success
984 */
985static int
986vmw_stdu_primary_plane_prepare_fb(struct drm_plane *plane,
987 struct drm_plane_state *new_state)
988{
989 struct drm_framebuffer *new_fb = new_state->fb;
990 struct vmw_framebuffer *vfb;
991 struct vmw_plane_state *vps = vmw_plane_state_to_vps(new_state);
992 enum stdu_content_type new_content_type;
993 struct vmw_framebuffer_surface *new_vfbs;
994 struct drm_crtc *crtc = new_state->crtc;
995 uint32_t hdisplay = new_state->crtc_w, vdisplay = new_state->crtc_h;
996 int ret;
997
998 /* No FB to prepare */
999 if (!new_fb) {
1000 if (vps->surf) {
1001 WARN_ON(vps->pinned != 0);
1002 vmw_surface_unreference(&vps->surf);
1003 }
1004
1005 return 0;
1006 }
1007
1008 vfb = vmw_framebuffer_to_vfb(new_fb);
1009 new_vfbs = (vfb->dmabuf) ? NULL : vmw_framebuffer_to_vfbs(new_fb);
1010
1011 if (new_vfbs && new_vfbs->surface->base_size.width == hdisplay &&
1012 new_vfbs->surface->base_size.height == vdisplay)
1013 new_content_type = SAME_AS_DISPLAY;
1014 else if (vfb->dmabuf)
1015 new_content_type = SEPARATE_DMA;
1016 else
1017 new_content_type = SEPARATE_SURFACE;
1018
1019 if (new_content_type != SAME_AS_DISPLAY) {
1020 struct vmw_surface content_srf;
1021 struct drm_vmw_size display_base_size = {0};
1022
1023 display_base_size.width = hdisplay;
1024 display_base_size.height = vdisplay;
1025 display_base_size.depth = 1;
1026
1027 /*
1028 * If content buffer is a DMA buf, then we have to construct
1029 * surface info
1030 */
1031 if (new_content_type == SEPARATE_DMA) {
1032
1033 switch (new_fb->format->cpp[0]*8) {
1034 case 32:
1035 content_srf.format = SVGA3D_X8R8G8B8;
1036 break;
1037
1038 case 16:
1039 content_srf.format = SVGA3D_R5G6B5;
1040 break;
1041
1042 case 8:
1043 content_srf.format = SVGA3D_P8;
1044 break;
1045
1046 default:
1047 DRM_ERROR("Invalid format\n");
1048 return -EINVAL;
1049 }
1050
1051 content_srf.flags = 0;
1052 content_srf.mip_levels[0] = 1;
1053 content_srf.multisample_count = 0;
1054 } else {
1055 content_srf = *new_vfbs->surface;
1056 }
1057
1058 if (vps->surf) {
1059 struct drm_vmw_size cur_base_size = vps->surf->base_size;
1060
1061 if (cur_base_size.width != display_base_size.width ||
1062 cur_base_size.height != display_base_size.height ||
1063 vps->surf->format != content_srf.format) {
1064 WARN_ON(vps->pinned != 0);
1065 vmw_surface_unreference(&vps->surf);
1066 }
1067
1068 }
1069
1070 if (!vps->surf) {
1071 ret = vmw_surface_gb_priv_define
1072 (crtc->dev,
1073 /* Kernel visible only */
1074 0,
1075 content_srf.flags,
1076 content_srf.format,
1077 true, /* a scanout buffer */
1078 content_srf.mip_levels[0],
1079 content_srf.multisample_count,
1080 0,
1081 display_base_size,
1082 &vps->surf);
1083 if (ret != 0) {
1084 DRM_ERROR("Couldn't allocate STDU surface.\n");
1085 return ret;
1086 }
1087 }
1088 } else {
1089 /*
1090 * prepare_fb and clean_fb should only take care of pinning
1091 * and unpinning. References are tracked by state objects.
1092 * The only time we add a reference in prepare_fb is if the
1093 * state object doesn't have a reference to begin with
1094 */
1095 if (vps->surf) {
1096 WARN_ON(vps->pinned != 0);
1097 vmw_surface_unreference(&vps->surf);
1098 }
1099
1100 vps->surf = vmw_surface_reference(new_vfbs->surface);
1101 }
1102
1103 if (vps->surf) {
1104
1105 /* Pin new surface before flipping */
1106 ret = vmw_resource_pin(&vps->surf->res, false);
1107 if (ret)
1108 goto out_srf_unref;
1109
1110 vps->pinned++;
1111 }
1112
1113 vps->content_fb_type = new_content_type;
1114 return 0;
1115
1116out_srf_unref:
1117 vmw_surface_unreference(&vps->surf);
1118 return ret;
1119}
1120
1121
1122
1123/**
1124 * vmw_stdu_primary_plane_atomic_update - formally switches STDU to new plane
1125 *
1126 * @plane: display plane
1127 * @old_state: Only used to get crtc info
1128 *
1129 * Formally update stdu->display_srf to the new plane, and bind the new
1130 * plane STDU. This function is called during the commit phase when
1131 * all the preparation have been done and all the configurations have
1132 * been checked.
1133 */
1134static void
1135vmw_stdu_primary_plane_atomic_update(struct drm_plane *plane,
1136 struct drm_plane_state *old_state)
1137{
1138 struct vmw_private *dev_priv;
1139 struct vmw_screen_target_display_unit *stdu;
1140 struct vmw_plane_state *vps = vmw_plane_state_to_vps(plane->state);
1141 struct drm_crtc *crtc = plane->state->crtc ?: old_state->crtc;
1142 int ret;
1143
1144 stdu = vmw_crtc_to_stdu(crtc);
1145 dev_priv = vmw_priv(crtc->dev);
1146
1147 stdu->display_srf = vps->surf;
1148 stdu->content_fb_type = vps->content_fb_type;
1149
1150 if (!stdu->defined)
1151 return;
1152
1153 if (plane->state->fb)
1154 ret = vmw_stdu_bind_st(dev_priv, stdu, &stdu->display_srf->res);
1155 else
1156 ret = vmw_stdu_bind_st(dev_priv, stdu, NULL);
1157
1158 /*
1159 * We cannot really fail this function, so if we do, then output an
1160 * error and quit
1161 */
1162 if (ret)
1163 DRM_ERROR("Failed to bind surface to STDU.\n");
1164 else
1165 crtc->primary->fb = plane->state->fb;
1166}
1167
1168
Sinclair Yeh36cc79b2017-03-23 11:28:11 -07001169static const struct drm_plane_funcs vmw_stdu_plane_funcs = {
Sinclair Yeh904bb5e2017-03-23 14:29:22 -07001170 .update_plane = drm_atomic_helper_update_plane,
1171 .disable_plane = drm_atomic_helper_disable_plane,
Sinclair Yeh36cc79b2017-03-23 11:28:11 -07001172 .destroy = vmw_du_primary_plane_destroy,
Sinclair Yehcc5ec452017-03-23 11:36:05 -07001173 .reset = vmw_du_plane_reset,
1174 .atomic_duplicate_state = vmw_du_plane_duplicate_state,
1175 .atomic_destroy_state = vmw_du_plane_destroy_state,
Sinclair Yeh36cc79b2017-03-23 11:28:11 -07001176};
1177
1178static const struct drm_plane_funcs vmw_stdu_cursor_funcs = {
Sinclair Yeh904bb5e2017-03-23 14:29:22 -07001179 .update_plane = drm_atomic_helper_update_plane,
1180 .disable_plane = drm_atomic_helper_disable_plane,
Sinclair Yeh36cc79b2017-03-23 11:28:11 -07001181 .destroy = vmw_du_cursor_plane_destroy,
Sinclair Yehcc5ec452017-03-23 11:36:05 -07001182 .reset = vmw_du_plane_reset,
1183 .atomic_duplicate_state = vmw_du_plane_duplicate_state,
1184 .atomic_destroy_state = vmw_du_plane_destroy_state,
Sinclair Yeh36cc79b2017-03-23 11:28:11 -07001185};
1186
1187
Sinclair Yeh06ec4192017-03-23 13:14:54 -07001188/*
1189 * Atomic Helpers
1190 */
Sinclair Yeh060e2ad2017-03-23 14:18:32 -07001191static const struct
1192drm_plane_helper_funcs vmw_stdu_cursor_plane_helper_funcs = {
1193 .atomic_check = vmw_du_cursor_plane_atomic_check,
1194 .atomic_update = vmw_du_cursor_plane_atomic_update,
1195 .prepare_fb = vmw_du_cursor_plane_prepare_fb,
1196 .cleanup_fb = vmw_du_plane_cleanup_fb,
1197};
1198
1199static const struct
1200drm_plane_helper_funcs vmw_stdu_primary_plane_helper_funcs = {
1201 .atomic_check = vmw_du_primary_plane_atomic_check,
1202 .atomic_update = vmw_stdu_primary_plane_atomic_update,
1203 .prepare_fb = vmw_stdu_primary_plane_prepare_fb,
1204 .cleanup_fb = vmw_stdu_primary_plane_cleanup_fb,
1205};
1206
Sinclair Yeh06ec4192017-03-23 13:14:54 -07001207static const struct drm_crtc_helper_funcs vmw_stdu_crtc_helper_funcs = {
1208 .prepare = vmw_stdu_crtc_helper_prepare,
1209 .commit = vmw_stdu_crtc_helper_commit,
1210 .disable = vmw_stdu_crtc_helper_disable,
Sinclair Yeh06ec4192017-03-23 13:14:54 -07001211 .mode_set_nofb = vmw_stdu_crtc_mode_set_nofb,
1212 .atomic_check = vmw_du_crtc_atomic_check,
1213 .atomic_begin = vmw_du_crtc_atomic_begin,
1214 .atomic_flush = vmw_du_crtc_atomic_flush,
1215};
1216
1217
Sinclair Yeh35c05122015-06-26 01:42:06 -07001218/**
1219 * vmw_stdu_init - Sets up a Screen Target Display Unit
1220 *
1221 * @dev_priv: VMW DRM device
1222 * @unit: unit number range from 0 to VMWGFX_NUM_DISPLAY_UNITS
1223 *
1224 * This function is called once per CRTC, and allocates one Screen Target
1225 * display unit to represent that CRTC. Since the SVGA device does not separate
1226 * out encoder and connector, they are represented as part of the STDU as well.
1227 */
1228static int vmw_stdu_init(struct vmw_private *dev_priv, unsigned unit)
1229{
1230 struct vmw_screen_target_display_unit *stdu;
1231 struct drm_device *dev = dev_priv->dev;
1232 struct drm_connector *connector;
1233 struct drm_encoder *encoder;
Sinclair Yeh36cc79b2017-03-23 11:28:11 -07001234 struct drm_plane *primary, *cursor;
Sinclair Yeh35c05122015-06-26 01:42:06 -07001235 struct drm_crtc *crtc;
Sinclair Yeh36cc79b2017-03-23 11:28:11 -07001236 int ret;
Sinclair Yeh35c05122015-06-26 01:42:06 -07001237
1238
1239 stdu = kzalloc(sizeof(*stdu), GFP_KERNEL);
1240 if (!stdu)
1241 return -ENOMEM;
1242
1243 stdu->base.unit = unit;
1244 crtc = &stdu->base.crtc;
1245 encoder = &stdu->base.encoder;
1246 connector = &stdu->base.connector;
Sinclair Yeh36cc79b2017-03-23 11:28:11 -07001247 primary = &stdu->base.primary;
1248 cursor = &stdu->base.cursor;
Sinclair Yeh35c05122015-06-26 01:42:06 -07001249
1250 stdu->base.pref_active = (unit == 0);
1251 stdu->base.pref_width = dev_priv->initial_width;
1252 stdu->base.pref_height = dev_priv->initial_height;
Sinclair Yeh9c2542a2017-03-23 11:33:39 -07001253
1254 /*
1255 * Remove this after enabling atomic because property values can
1256 * only exist in a state object
1257 */
Thomas Hellstrom2e69b252016-02-12 09:59:50 +01001258 stdu->base.is_implicit = false;
Sinclair Yeh35c05122015-06-26 01:42:06 -07001259
Sinclair Yeh36cc79b2017-03-23 11:28:11 -07001260 /* Initialize primary plane */
Sinclair Yehcc5ec452017-03-23 11:36:05 -07001261 vmw_du_plane_reset(primary);
1262
Sinclair Yeh36cc79b2017-03-23 11:28:11 -07001263 ret = drm_universal_plane_init(dev, primary,
1264 0, &vmw_stdu_plane_funcs,
1265 vmw_primary_plane_formats,
1266 ARRAY_SIZE(vmw_primary_plane_formats),
1267 DRM_PLANE_TYPE_PRIMARY, NULL);
1268 if (ret) {
1269 DRM_ERROR("Failed to initialize primary plane");
1270 goto err_free;
1271 }
1272
Sinclair Yeh060e2ad2017-03-23 14:18:32 -07001273 drm_plane_helper_add(primary, &vmw_stdu_primary_plane_helper_funcs);
1274
Sinclair Yeh36cc79b2017-03-23 11:28:11 -07001275 /* Initialize cursor plane */
Sinclair Yehcc5ec452017-03-23 11:36:05 -07001276 vmw_du_plane_reset(cursor);
1277
Sinclair Yeh36cc79b2017-03-23 11:28:11 -07001278 ret = drm_universal_plane_init(dev, cursor,
1279 0, &vmw_stdu_cursor_funcs,
1280 vmw_cursor_plane_formats,
1281 ARRAY_SIZE(vmw_cursor_plane_formats),
1282 DRM_PLANE_TYPE_CURSOR, NULL);
1283 if (ret) {
1284 DRM_ERROR("Failed to initialize cursor plane");
1285 drm_plane_cleanup(&stdu->base.primary);
1286 goto err_free;
1287 }
1288
Sinclair Yeh060e2ad2017-03-23 14:18:32 -07001289 drm_plane_helper_add(cursor, &vmw_stdu_cursor_plane_helper_funcs);
1290
Sinclair Yehd7721ca2017-03-23 11:48:44 -07001291 vmw_du_connector_reset(connector);
1292
Sinclair Yeh36cc79b2017-03-23 11:28:11 -07001293 ret = drm_connector_init(dev, connector, &vmw_stdu_connector_funcs,
1294 DRM_MODE_CONNECTOR_VIRTUAL);
1295 if (ret) {
1296 DRM_ERROR("Failed to initialize connector\n");
1297 goto err_free;
1298 }
Sinclair Yehd947d1b2017-03-23 14:23:20 -07001299
1300 drm_connector_helper_add(connector, &vmw_stdu_connector_helper_funcs);
Sinclair Yeh35c05122015-06-26 01:42:06 -07001301 connector->status = vmw_du_connector_detect(connector, false);
Sinclair Yehd7721ca2017-03-23 11:48:44 -07001302 vmw_connector_state_to_vcs(connector->state)->is_implicit = false;
Sinclair Yeh35c05122015-06-26 01:42:06 -07001303
Sinclair Yeh36cc79b2017-03-23 11:28:11 -07001304 ret = drm_encoder_init(dev, encoder, &vmw_stdu_encoder_funcs,
1305 DRM_MODE_ENCODER_VIRTUAL, NULL);
1306 if (ret) {
1307 DRM_ERROR("Failed to initialize encoder\n");
1308 goto err_free_connector;
1309 }
1310
1311 (void) drm_mode_connector_attach_encoder(connector, encoder);
Sinclair Yeh35c05122015-06-26 01:42:06 -07001312 encoder->possible_crtcs = (1 << unit);
1313 encoder->possible_clones = 0;
1314
Sinclair Yeh36cc79b2017-03-23 11:28:11 -07001315 ret = drm_connector_register(connector);
1316 if (ret) {
1317 DRM_ERROR("Failed to register connector\n");
1318 goto err_free_encoder;
1319 }
Sinclair Yeh35c05122015-06-26 01:42:06 -07001320
Sinclair Yehd7721ca2017-03-23 11:48:44 -07001321 vmw_du_crtc_reset(crtc);
Sinclair Yeh36cc79b2017-03-23 11:28:11 -07001322 ret = drm_crtc_init_with_planes(dev, crtc, &stdu->base.primary,
1323 &stdu->base.cursor,
1324 &vmw_stdu_crtc_funcs, NULL);
1325 if (ret) {
1326 DRM_ERROR("Failed to initialize CRTC\n");
1327 goto err_free_unregister;
1328 }
Sinclair Yeh35c05122015-06-26 01:42:06 -07001329
Sinclair Yeh06ec4192017-03-23 13:14:54 -07001330 drm_crtc_helper_add(crtc, &vmw_stdu_crtc_helper_funcs);
1331
Sinclair Yeh35c05122015-06-26 01:42:06 -07001332 drm_mode_crtc_set_gamma_size(crtc, 256);
1333
1334 drm_object_attach_property(&connector->base,
Thomas Hellstrom578e6092016-02-12 09:45:42 +01001335 dev_priv->hotplug_mode_update_property, 1);
1336 drm_object_attach_property(&connector->base,
1337 dev->mode_config.suggested_x_property, 0);
1338 drm_object_attach_property(&connector->base,
1339 dev->mode_config.suggested_y_property, 0);
Thomas Hellstrom76404ac2016-02-12 09:55:45 +01001340 if (dev_priv->implicit_placement_property)
1341 drm_object_attach_property
1342 (&connector->base,
1343 dev_priv->implicit_placement_property,
1344 stdu->base.is_implicit);
Sinclair Yeh35c05122015-06-26 01:42:06 -07001345 return 0;
Sinclair Yeh36cc79b2017-03-23 11:28:11 -07001346
1347err_free_unregister:
1348 drm_connector_unregister(connector);
1349err_free_encoder:
1350 drm_encoder_cleanup(encoder);
1351err_free_connector:
1352 drm_connector_cleanup(connector);
1353err_free:
1354 kfree(stdu);
1355 return ret;
Sinclair Yeh35c05122015-06-26 01:42:06 -07001356}
1357
1358
1359
1360/**
1361 * vmw_stdu_destroy - Cleans up a vmw_screen_target_display_unit
1362 *
1363 * @stdu: Screen Target Display Unit to be destroyed
1364 *
1365 * Clean up after vmw_stdu_init
1366 */
1367static void vmw_stdu_destroy(struct vmw_screen_target_display_unit *stdu)
1368{
Sinclair Yeh35c05122015-06-26 01:42:06 -07001369 vmw_du_cleanup(&stdu->base);
1370 kfree(stdu);
1371}
1372
1373
1374
1375/******************************************************************************
1376 * Screen Target Display KMS Functions
1377 *
1378 * These functions are called by the common KMS code in vmwgfx_kms.c
1379 *****************************************************************************/
1380
1381/**
1382 * vmw_kms_stdu_init_display - Initializes a Screen Target based display
1383 *
1384 * @dev_priv: VMW DRM device
1385 *
1386 * This function initialize a Screen Target based display device. It checks
1387 * the capability bits to make sure the underlying hardware can support
1388 * screen targets, and then creates the maximum number of CRTCs, a.k.a Display
1389 * Units, as supported by the display hardware.
1390 *
1391 * RETURNS:
1392 * 0 on success, error code otherwise
1393 */
1394int vmw_kms_stdu_init_display(struct vmw_private *dev_priv)
1395{
1396 struct drm_device *dev = dev_priv->dev;
1397 int i, ret;
1398
1399
1400 /* Do nothing if Screen Target support is turned off */
1401 if (!VMWGFX_ENABLE_SCREEN_TARGET_OTABLE)
1402 return -ENOSYS;
1403
Sinclair Yehf89c6c32015-06-26 01:54:28 -07001404 if (!(dev_priv->capabilities & SVGA_CAP_GBOBJECTS))
Sinclair Yeh35c05122015-06-26 01:42:06 -07001405 return -ENOSYS;
1406
1407 ret = drm_vblank_init(dev, VMWGFX_NUM_DISPLAY_UNITS);
1408 if (unlikely(ret != 0))
1409 return ret;
1410
Thomas Hellstrom6bf6bf02015-06-26 02:22:40 -07001411 dev_priv->active_display_unit = vmw_du_screen_target;
1412
Thomas Hellstrom76404ac2016-02-12 09:55:45 +01001413 vmw_kms_create_implicit_placement_property(dev_priv, false);
1414
Sinclair Yeh35c05122015-06-26 01:42:06 -07001415 for (i = 0; i < VMWGFX_NUM_DISPLAY_UNITS; ++i) {
1416 ret = vmw_stdu_init(dev_priv, i);
1417
1418 if (unlikely(ret != 0)) {
1419 DRM_ERROR("Failed to initialize STDU %d", i);
1420 goto err_vblank_cleanup;
1421 }
1422 }
1423
Sinclair Yeh35c05122015-06-26 01:42:06 -07001424 DRM_INFO("Screen Target Display device initialized\n");
1425
1426 return 0;
1427
1428err_vblank_cleanup:
1429 drm_vblank_cleanup(dev);
1430 return ret;
1431}
1432
1433
1434
1435/**
1436 * vmw_kms_stdu_close_display - Cleans up after vmw_kms_stdu_init_display
1437 *
1438 * @dev_priv: VMW DRM device
1439 *
1440 * Frees up any resources allocated by vmw_kms_stdu_init_display
1441 *
1442 * RETURNS:
1443 * 0 on success
1444 */
1445int vmw_kms_stdu_close_display(struct vmw_private *dev_priv)
1446{
1447 struct drm_device *dev = dev_priv->dev;
1448
1449 drm_vblank_cleanup(dev);
1450
1451 return 0;
1452}