Merge remote-tracking branch 'asoc/topic/pcm5102a' into asoc-next
[linux-2.6-block.git] / drivers / gpu / drm / vmwgfx / vmwgfx_stdu.c
CommitLineData
35c05125
SY
1/******************************************************************************
2 *
54fbde8a 3 * COPYRIGHT © 2014-2015 VMware, Inc., Palo Alto, CA., USA
35c05125
SY
4 * 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"
8ce75f8a 29#include "device_include/svga3d_surfacedefs.h"
35c05125 30#include <drm/drm_plane_helper.h>
d7721ca7
SY
31#include <drm/drm_atomic.h>
32#include <drm/drm_atomic_helper.h>
33
35c05125
SY
34
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
6bf6bf03
TH
50/**
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.
a1ac6339
SY
59 * @fb_left: Left side of the framebuffer/content bounding box
60 * @fb_top: Top of the framebuffer/content bounding box
6bf6bf03
TH
61 * @buf: DMA buffer when DMA-ing between buffer and screen targets.
62 * @sid: Surface ID when copying between surface and screen targets.
63 */
64struct vmw_stdu_dirty {
65 struct vmw_kms_dirty base;
66 SVGA3dTransferType transfer;
67 s32 left, right, top, bottom;
a1ac6339 68 s32 fb_left, fb_top;
6bf6bf03
TH
69 u32 pitch;
70 union {
71 struct vmw_dma_buffer *buf;
72 u32 sid;
73 };
74};
75
76/*
77 * SVGA commands that are used by this code. Please see the device headers
78 * for explanation.
79 */
80struct vmw_stdu_update {
81 SVGA3dCmdHeader header;
82 SVGA3dCmdUpdateGBScreenTarget body;
83};
84
85struct vmw_stdu_dma {
86 SVGA3dCmdHeader header;
87 SVGA3dCmdSurfaceDMA body;
88};
89
90struct vmw_stdu_surface_copy {
91 SVGA3dCmdHeader header;
92 SVGA3dCmdSurfaceCopy body;
93};
35c05125
SY
94
95
96/**
97 * struct vmw_screen_target_display_unit
98 *
99 * @base: VMW specific DU structure
100 * @display_srf: surface to be displayed. The dimension of this will always
101 * match the display mode. If the display mode matches
102 * content_vfbs dimensions, then this is a pointer into the
103 * corresponding field in content_vfbs. If not, then this
104 * is a separate buffer to which content_vfbs will blit to.
35c05125
SY
105 * @content_type: content_fb type
106 * @defined: true if the current display unit has been initialized
107 */
108struct vmw_screen_target_display_unit {
109 struct vmw_display_unit base;
904bb5e5 110 const struct vmw_surface *display_srf;
35c05125 111 enum stdu_content_type content_fb_type;
9aa8dcab 112 s32 display_width, display_height;
35c05125
SY
113
114 bool defined;
810b3e16
SY
115
116 /* For CPU Blit */
98648ae6 117 struct ttm_bo_kmap_obj host_map;
810b3e16 118 unsigned int cpp;
35c05125
SY
119};
120
121
122
123static void vmw_stdu_destroy(struct vmw_screen_target_display_unit *stdu);
124
125
126
35c05125
SY
127/******************************************************************************
128 * Screen Target Display Unit CRTC Functions
129 *****************************************************************************/
130
131
132/**
133 * vmw_stdu_crtc_destroy - cleans up the STDU
134 *
135 * @crtc: used to get a reference to the containing STDU
136 */
137static void vmw_stdu_crtc_destroy(struct drm_crtc *crtc)
138{
139 vmw_stdu_destroy(vmw_crtc_to_stdu(crtc));
140}
141
35c05125
SY
142/**
143 * vmw_stdu_define_st - Defines a Screen Target
144 *
145 * @dev_priv: VMW DRM device
146 * @stdu: display unit to create a Screen Target for
b1097aeb
TH
147 * @mode: The mode to set.
148 * @crtc_x: X coordinate of screen target relative to framebuffer origin.
149 * @crtc_y: Y coordinate of screen target relative to framebuffer origin.
35c05125
SY
150 *
151 * Creates a STDU that we can used later. This function is called whenever the
152 * framebuffer size changes.
153 *
154 * RETURNs:
155 * 0 on success, error code on failure
156 */
157static int vmw_stdu_define_st(struct vmw_private *dev_priv,
b1097aeb
TH
158 struct vmw_screen_target_display_unit *stdu,
159 struct drm_display_mode *mode,
160 int crtc_x, int crtc_y)
35c05125
SY
161{
162 struct {
163 SVGA3dCmdHeader header;
164 SVGA3dCmdDefineGBScreenTarget body;
165 } *cmd;
166
167 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
168
169 if (unlikely(cmd == NULL)) {
170 DRM_ERROR("Out of FIFO space defining Screen Target\n");
171 return -ENOMEM;
172 }
173
174 cmd->header.id = SVGA_3D_CMD_DEFINE_GB_SCREENTARGET;
175 cmd->header.size = sizeof(cmd->body);
176
177 cmd->body.stid = stdu->base.unit;
b1097aeb
TH
178 cmd->body.width = mode->hdisplay;
179 cmd->body.height = mode->vdisplay;
35c05125
SY
180 cmd->body.flags = (0 == cmd->body.stid) ? SVGA_STFLAG_PRIMARY : 0;
181 cmd->body.dpi = 0;
b1097aeb
TH
182 if (stdu->base.is_implicit) {
183 cmd->body.xRoot = crtc_x;
184 cmd->body.yRoot = crtc_y;
185 } else {
35c05125
SY
186 cmd->body.xRoot = stdu->base.gui_x;
187 cmd->body.yRoot = stdu->base.gui_y;
188 }
6dd687b4
TH
189 stdu->base.set_gui_x = cmd->body.xRoot;
190 stdu->base.set_gui_y = cmd->body.yRoot;
35c05125
SY
191
192 vmw_fifo_commit(dev_priv, sizeof(*cmd));
193
194 stdu->defined = true;
9aa8dcab
SY
195 stdu->display_width = mode->hdisplay;
196 stdu->display_height = mode->vdisplay;
35c05125
SY
197
198 return 0;
199}
200
201
202
203/**
204 * vmw_stdu_bind_st - Binds a surface to a Screen Target
205 *
206 * @dev_priv: VMW DRM device
207 * @stdu: display unit affected
208 * @res: Buffer to bind to the screen target. Set to NULL to blank screen.
209 *
210 * Binding a surface to a Screen Target the same as flipping
211 */
212static int vmw_stdu_bind_st(struct vmw_private *dev_priv,
213 struct vmw_screen_target_display_unit *stdu,
904bb5e5 214 const struct vmw_resource *res)
35c05125
SY
215{
216 SVGA3dSurfaceImageId image;
217
218 struct {
219 SVGA3dCmdHeader header;
220 SVGA3dCmdBindGBScreenTarget body;
221 } *cmd;
222
223
224 if (!stdu->defined) {
225 DRM_ERROR("No screen target defined\n");
226 return -EINVAL;
227 }
228
229 /* Set up image using information in vfb */
230 memset(&image, 0, sizeof(image));
231 image.sid = res ? res->id : SVGA3D_INVALID_ID;
232
233 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
234
235 if (unlikely(cmd == NULL)) {
236 DRM_ERROR("Out of FIFO space binding a screen target\n");
237 return -ENOMEM;
238 }
239
240 cmd->header.id = SVGA_3D_CMD_BIND_GB_SCREENTARGET;
241 cmd->header.size = sizeof(cmd->body);
242
243 cmd->body.stid = stdu->base.unit;
244 cmd->body.image = image;
245
246 vmw_fifo_commit(dev_priv, sizeof(*cmd));
247
248 return 0;
249}
250
6bf6bf03
TH
251/**
252 * vmw_stdu_populate_update - populate an UPDATE_GB_SCREENTARGET command with a
253 * bounding box.
254 *
255 * @cmd: Pointer to command stream.
256 * @unit: Screen target unit.
257 * @left: Left side of bounding box.
258 * @right: Right side of bounding box.
259 * @top: Top side of bounding box.
260 * @bottom: Bottom side of bounding box.
261 */
262static void vmw_stdu_populate_update(void *cmd, int unit,
263 s32 left, s32 right, s32 top, s32 bottom)
264{
265 struct vmw_stdu_update *update = cmd;
266
267 update->header.id = SVGA_3D_CMD_UPDATE_GB_SCREENTARGET;
268 update->header.size = sizeof(update->body);
35c05125 269
6bf6bf03
TH
270 update->body.stid = unit;
271 update->body.rect.x = left;
272 update->body.rect.y = top;
273 update->body.rect.w = right - left;
274 update->body.rect.h = bottom - top;
275}
35c05125
SY
276
277/**
6bf6bf03 278 * vmw_stdu_update_st - Full update of a Screen Target
35c05125
SY
279 *
280 * @dev_priv: VMW DRM device
35c05125 281 * @stdu: display unit affected
35c05125
SY
282 *
283 * This function needs to be called whenever the content of a screen
6bf6bf03
TH
284 * target has changed completely. Typically as a result of a backing
285 * surface change.
35c05125
SY
286 *
287 * RETURNS:
288 * 0 on success, error code on failure
289 */
290static int vmw_stdu_update_st(struct vmw_private *dev_priv,
6bf6bf03 291 struct vmw_screen_target_display_unit *stdu)
35c05125 292{
6bf6bf03 293 struct vmw_stdu_update *cmd;
35c05125
SY
294
295 if (!stdu->defined) {
296 DRM_ERROR("No screen target defined");
297 return -EINVAL;
298 }
299
35c05125
SY
300 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
301
302 if (unlikely(cmd == NULL)) {
303 DRM_ERROR("Out of FIFO space updating a Screen Target\n");
304 return -ENOMEM;
305 }
306
9aa8dcab
SY
307 vmw_stdu_populate_update(cmd, stdu->base.unit,
308 0, stdu->display_width,
309 0, stdu->display_height);
35c05125
SY
310
311 vmw_fifo_commit(dev_priv, sizeof(*cmd));
312
313 return 0;
314}
315
316
317
318/**
319 * vmw_stdu_destroy_st - Destroy a Screen Target
320 *
321 * @dev_priv: VMW DRM device
322 * @stdu: display unit to destroy
323 */
324static int vmw_stdu_destroy_st(struct vmw_private *dev_priv,
325 struct vmw_screen_target_display_unit *stdu)
326{
327 int ret;
328
329 struct {
330 SVGA3dCmdHeader header;
331 SVGA3dCmdDestroyGBScreenTarget body;
332 } *cmd;
333
334
335 /* Nothing to do if not successfully defined */
336 if (unlikely(!stdu->defined))
337 return 0;
338
339 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
340
341 if (unlikely(cmd == NULL)) {
342 DRM_ERROR("Out of FIFO space, screen target not destroyed\n");
343 return -ENOMEM;
344 }
345
346 cmd->header.id = SVGA_3D_CMD_DESTROY_GB_SCREENTARGET;
347 cmd->header.size = sizeof(cmd->body);
348
349 cmd->body.stid = stdu->base.unit;
350
351 vmw_fifo_commit(dev_priv, sizeof(*cmd));
352
353 /* Force sync */
354 ret = vmw_fallback_wait(dev_priv, false, true, 0, false, 3*HZ);
355 if (unlikely(ret != 0))
356 DRM_ERROR("Failed to sync with HW");
357
358 stdu->defined = false;
9aa8dcab
SY
359 stdu->display_width = 0;
360 stdu->display_height = 0;
35c05125
SY
361
362 return ret;
363}
364
06ec4190
SY
365
366/**
367 * vmw_stdu_crtc_mode_set_nofb - Updates screen target size
368 *
369 * @crtc: CRTC associated with the screen target
370 *
371 * This function defines/destroys a screen target
372 *
373 */
374static void vmw_stdu_crtc_mode_set_nofb(struct drm_crtc *crtc)
375{
376 struct vmw_private *dev_priv;
377 struct vmw_screen_target_display_unit *stdu;
378 int ret;
379
380
381 stdu = vmw_crtc_to_stdu(crtc);
382 dev_priv = vmw_priv(crtc->dev);
383
384 if (stdu->defined) {
385 ret = vmw_stdu_bind_st(dev_priv, stdu, NULL);
386 if (ret)
387 DRM_ERROR("Failed to blank CRTC\n");
388
389 (void) vmw_stdu_update_st(dev_priv, stdu);
390
391 ret = vmw_stdu_destroy_st(dev_priv, stdu);
392 if (ret)
393 DRM_ERROR("Failed to destroy Screen Target\n");
394
395 stdu->content_fb_type = SAME_AS_DISPLAY;
396 }
397
398 if (!crtc->state->enable)
399 return;
400
401 vmw_svga_enable(dev_priv);
402 ret = vmw_stdu_define_st(dev_priv, stdu, &crtc->mode, crtc->x, crtc->y);
403
404 if (ret)
405 DRM_ERROR("Failed to define Screen Target of size %dx%d\n",
406 crtc->x, crtc->y);
407}
408
409
410static void vmw_stdu_crtc_helper_prepare(struct drm_crtc *crtc)
411{
412}
413
414
0b20a0f8
LP
415static void vmw_stdu_crtc_atomic_enable(struct drm_crtc *crtc,
416 struct drm_crtc_state *old_state)
06ec4190
SY
417{
418 struct vmw_private *dev_priv;
419 struct vmw_screen_target_display_unit *stdu;
420 struct vmw_framebuffer *vfb;
421 struct drm_framebuffer *fb;
422
423
424 stdu = vmw_crtc_to_stdu(crtc);
425 dev_priv = vmw_priv(crtc->dev);
426 fb = crtc->primary->fb;
427
428 vfb = (fb) ? vmw_framebuffer_to_vfb(fb) : NULL;
429
430 if (vfb)
431 vmw_kms_add_active(dev_priv, &stdu->base, vfb);
432 else
433 vmw_kms_del_active(dev_priv, &stdu->base);
434}
435
64581714
LP
436static void vmw_stdu_crtc_atomic_disable(struct drm_crtc *crtc,
437 struct drm_crtc_state *old_state)
06ec4190
SY
438{
439 struct vmw_private *dev_priv;
440 struct vmw_screen_target_display_unit *stdu;
441 int ret;
442
443
444 if (!crtc) {
445 DRM_ERROR("CRTC is NULL\n");
446 return;
447 }
448
449 stdu = vmw_crtc_to_stdu(crtc);
450 dev_priv = vmw_priv(crtc->dev);
451
452 if (stdu->defined) {
453 ret = vmw_stdu_bind_st(dev_priv, stdu, NULL);
454 if (ret)
455 DRM_ERROR("Failed to blank CRTC\n");
456
457 (void) vmw_stdu_update_st(dev_priv, stdu);
458
459 ret = vmw_stdu_destroy_st(dev_priv, stdu);
460 if (ret)
461 DRM_ERROR("Failed to destroy Screen Target\n");
462
463 stdu->content_fb_type = SAME_AS_DISPLAY;
464 }
465}
466
35c05125
SY
467/**
468 * vmw_stdu_crtc_page_flip - Binds a buffer to a screen target
469 *
470 * @crtc: CRTC to attach FB to
471 * @fb: FB to attach
472 * @event: Event to be posted. This event should've been alloced
473 * using k[mz]alloc, and should've been completely initialized.
474 * @page_flip_flags: Input flags.
475 *
476 * If the STDU uses the same display and content buffers, i.e. a true flip,
477 * this function will replace the existing display buffer with the new content
478 * buffer.
479 *
480 * If the STDU uses different display and content buffers, i.e. a blit, then
481 * only the content buffer will be updated.
482 *
483 * RETURNS:
484 * 0 on success, error code on failure
485 */
486static int vmw_stdu_crtc_page_flip(struct drm_crtc *crtc,
487 struct drm_framebuffer *new_fb,
488 struct drm_pending_vblank_event *event,
41292b1f
DV
489 uint32_t flags,
490 struct drm_modeset_acquire_ctx *ctx)
35c05125
SY
491
492{
493 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
904bb5e5 494 struct vmw_screen_target_display_unit *stdu = vmw_crtc_to_stdu(crtc);
b1097aeb 495 struct vmw_framebuffer *vfb = vmw_framebuffer_to_vfb(new_fb);
904bb5e5 496 struct drm_vmw_rect vclips;
35c05125
SY
497 int ret;
498
35c05125
SY
499 dev_priv = vmw_priv(crtc->dev);
500 stdu = vmw_crtc_to_stdu(crtc);
35c05125 501
4d492a07 502 if (!stdu->defined || !vmw_kms_crtc_flippable(dev_priv, crtc))
b1097aeb 503 return -EINVAL;
35c05125 504
904bb5e5
SY
505 /*
506 * We're always async, but the helper doesn't know how to set async
507 * so lie to the helper. Also, the helper expects someone
508 * to pick the event up from the crtc state, and if nobody does,
509 * it will free it. Since we handle the event in this function,
510 * don't hand it to the helper.
511 */
512 flags &= ~DRM_MODE_PAGE_FLIP_ASYNC;
320d8c3d 513 ret = drm_atomic_helper_page_flip(crtc, new_fb, NULL, flags, ctx);
904bb5e5
SY
514 if (ret) {
515 DRM_ERROR("Page flip error %d.\n", ret);
b1097aeb 516 return ret;
904bb5e5 517 }
35c05125 518
4d492a07
TH
519 if (stdu->base.is_implicit)
520 vmw_kms_update_implicit_fb(dev_priv, crtc);
521
904bb5e5
SY
522 /*
523 * Now that we've bound a new surface to the screen target,
524 * update the contents.
525 */
b1097aeb
TH
526 vclips.x = crtc->x;
527 vclips.y = crtc->y;
528 vclips.w = crtc->mode.hdisplay;
529 vclips.h = crtc->mode.vdisplay;
904bb5e5 530
b1097aeb
TH
531 if (vfb->dmabuf)
532 ret = vmw_kms_stdu_dma(dev_priv, NULL, vfb, NULL, NULL, &vclips,
533 1, 1, true, false);
534 else
535 ret = vmw_kms_stdu_surface_dirty(dev_priv, vfb, NULL, &vclips,
536 NULL, 0, 0, 1, 1, NULL);
904bb5e5
SY
537 if (ret) {
538 DRM_ERROR("Page flip update error %d.\n", ret);
35c05125 539 return ret;
904bb5e5 540 }
35c05125
SY
541
542 if (event) {
543 struct vmw_fence_obj *fence = NULL;
6bf6bf03 544 struct drm_file *file_priv = event->base.file_priv;
35c05125
SY
545
546 vmw_execbuf_fence_commands(NULL, dev_priv, &fence, NULL);
547 if (!fence)
548 return -ENOMEM;
549
550 ret = vmw_event_fence_action_queue(file_priv, fence,
551 &event->base,
bd386e51
KP
552 &event->event.vbl.tv_sec,
553 &event->event.vbl.tv_usec,
35c05125
SY
554 true);
555 vmw_fence_obj_unreference(&fence);
4e0858a6 556 } else {
904bb5e5 557 (void) vmw_fifo_flush(dev_priv, false);
35c05125
SY
558 }
559
b1097aeb 560 return 0;
35c05125
SY
561}
562
563
6bf6bf03
TH
564/**
565 * vmw_stdu_dmabuf_clip - Callback to encode a suface DMA command cliprect
566 *
567 * @dirty: The closure structure.
568 *
569 * Encodes a surface DMA command cliprect and updates the bounding box
570 * for the DMA.
571 */
572static void vmw_stdu_dmabuf_clip(struct vmw_kms_dirty *dirty)
573{
574 struct vmw_stdu_dirty *ddirty =
575 container_of(dirty, struct vmw_stdu_dirty, base);
576 struct vmw_stdu_dma *cmd = dirty->cmd;
577 struct SVGA3dCopyBox *blit = (struct SVGA3dCopyBox *) &cmd[1];
578
579 blit += dirty->num_hits;
580 blit->srcx = dirty->fb_x;
581 blit->srcy = dirty->fb_y;
582 blit->x = dirty->unit_x1;
583 blit->y = dirty->unit_y1;
584 blit->d = 1;
585 blit->w = dirty->unit_x2 - dirty->unit_x1;
586 blit->h = dirty->unit_y2 - dirty->unit_y1;
587 dirty->num_hits++;
588
589 if (ddirty->transfer != SVGA3D_WRITE_HOST_VRAM)
590 return;
591
592 /* Destination bounding box */
593 ddirty->left = min_t(s32, ddirty->left, dirty->unit_x1);
594 ddirty->top = min_t(s32, ddirty->top, dirty->unit_y1);
595 ddirty->right = max_t(s32, ddirty->right, dirty->unit_x2);
596 ddirty->bottom = max_t(s32, ddirty->bottom, dirty->unit_y2);
597}
598
599/**
600 * vmw_stdu_dmabuf_fifo_commit - Callback to fill in and submit a DMA command.
601 *
602 * @dirty: The closure structure.
603 *
604 * Fills in the missing fields in a DMA command, and optionally encodes
605 * a screen target update command, depending on transfer direction.
606 */
607static void vmw_stdu_dmabuf_fifo_commit(struct vmw_kms_dirty *dirty)
608{
609 struct vmw_stdu_dirty *ddirty =
610 container_of(dirty, struct vmw_stdu_dirty, base);
611 struct vmw_screen_target_display_unit *stdu =
612 container_of(dirty->unit, typeof(*stdu), base);
613 struct vmw_stdu_dma *cmd = dirty->cmd;
614 struct SVGA3dCopyBox *blit = (struct SVGA3dCopyBox *) &cmd[1];
615 SVGA3dCmdSurfaceDMASuffix *suffix =
616 (SVGA3dCmdSurfaceDMASuffix *) &blit[dirty->num_hits];
617 size_t blit_size = sizeof(*blit) * dirty->num_hits + sizeof(*suffix);
618
619 if (!dirty->num_hits) {
620 vmw_fifo_commit(dirty->dev_priv, 0);
621 return;
622 }
623
624 cmd->header.id = SVGA_3D_CMD_SURFACE_DMA;
625 cmd->header.size = sizeof(cmd->body) + blit_size;
626 vmw_bo_get_guest_ptr(&ddirty->buf->base, &cmd->body.guest.ptr);
627 cmd->body.guest.pitch = ddirty->pitch;
628 cmd->body.host.sid = stdu->display_srf->res.id;
629 cmd->body.host.face = 0;
630 cmd->body.host.mipmap = 0;
631 cmd->body.transfer = ddirty->transfer;
632 suffix->suffixSize = sizeof(*suffix);
633 suffix->maximumOffset = ddirty->buf->base.num_pages * PAGE_SIZE;
634
635 if (ddirty->transfer == SVGA3D_WRITE_HOST_VRAM) {
636 blit_size += sizeof(struct vmw_stdu_update);
637
638 vmw_stdu_populate_update(&suffix[1], stdu->base.unit,
639 ddirty->left, ddirty->right,
640 ddirty->top, ddirty->bottom);
641 }
642
643 vmw_fifo_commit(dirty->dev_priv, sizeof(*cmd) + blit_size);
644
645 ddirty->left = ddirty->top = S32_MAX;
646 ddirty->right = ddirty->bottom = S32_MIN;
647}
648
810b3e16
SY
649
650/**
651 * vmw_stdu_dmabuf_cpu_clip - Callback to encode a CPU blit
652 *
653 * @dirty: The closure structure.
654 *
a1ac6339 655 * This function calculates the bounding box for all the incoming clips.
810b3e16
SY
656 */
657static void vmw_stdu_dmabuf_cpu_clip(struct vmw_kms_dirty *dirty)
658{
659 struct vmw_stdu_dirty *ddirty =
660 container_of(dirty, struct vmw_stdu_dirty, base);
661
662 dirty->num_hits = 1;
663
a1ac6339 664 /* Calculate destination bounding box */
810b3e16
SY
665 ddirty->left = min_t(s32, ddirty->left, dirty->unit_x1);
666 ddirty->top = min_t(s32, ddirty->top, dirty->unit_y1);
667 ddirty->right = max_t(s32, ddirty->right, dirty->unit_x2);
668 ddirty->bottom = max_t(s32, ddirty->bottom, dirty->unit_y2);
a1ac6339
SY
669
670 /*
671 * Calculate content bounding box. We only need the top-left
672 * coordinate because width and height will be the same as the
673 * destination bounding box above
674 */
675 ddirty->fb_left = min_t(s32, ddirty->fb_left, dirty->fb_x);
676 ddirty->fb_top = min_t(s32, ddirty->fb_top, dirty->fb_y);
810b3e16
SY
677}
678
679
680/**
681 * vmw_stdu_dmabuf_cpu_commit - Callback to do a CPU blit from DMAbuf
682 *
683 * @dirty: The closure structure.
684 *
685 * For the special case when we cannot create a proxy surface in a
686 * 2D VM, we have to do a CPU blit ourselves.
687 */
688static void vmw_stdu_dmabuf_cpu_commit(struct vmw_kms_dirty *dirty)
689{
690 struct vmw_stdu_dirty *ddirty =
691 container_of(dirty, struct vmw_stdu_dirty, base);
692 struct vmw_screen_target_display_unit *stdu =
693 container_of(dirty->unit, typeof(*stdu), base);
694 s32 width, height;
695 s32 src_pitch, dst_pitch;
696 u8 *src, *dst;
697 bool not_used;
98648ae6
TH
698 struct ttm_bo_kmap_obj guest_map;
699 int ret;
810b3e16
SY
700
701 if (!dirty->num_hits)
702 return;
703
704 width = ddirty->right - ddirty->left;
705 height = ddirty->bottom - ddirty->top;
706
707 if (width == 0 || height == 0)
708 return;
709
98648ae6
TH
710 ret = ttm_bo_kmap(&ddirty->buf->base, 0, ddirty->buf->base.num_pages,
711 &guest_map);
712 if (ret) {
713 DRM_ERROR("Failed mapping framebuffer for blit: %d\n",
714 ret);
715 goto out_cleanup;
716 }
810b3e16
SY
717
718 /* Assume we are blitting from Host (display_srf) to Guest (dmabuf) */
719 src_pitch = stdu->display_srf->base_size.width * stdu->cpp;
720 src = ttm_kmap_obj_virtual(&stdu->host_map, &not_used);
a1ac6339 721 src += ddirty->top * src_pitch + ddirty->left * stdu->cpp;
810b3e16
SY
722
723 dst_pitch = ddirty->pitch;
98648ae6 724 dst = ttm_kmap_obj_virtual(&guest_map, &not_used);
a1ac6339 725 dst += ddirty->fb_top * dst_pitch + ddirty->fb_left * stdu->cpp;
810b3e16
SY
726
727
728 /* Figure out the real direction */
729 if (ddirty->transfer == SVGA3D_WRITE_HOST_VRAM) {
730 u8 *tmp;
731 s32 tmp_pitch;
732
733 tmp = src;
734 tmp_pitch = src_pitch;
735
736 src = dst;
737 src_pitch = dst_pitch;
738
739 dst = tmp;
740 dst_pitch = tmp_pitch;
741 }
742
743 /* CPU Blit */
744 while (height-- > 0) {
745 memcpy(dst, src, width * stdu->cpp);
746 dst += dst_pitch;
747 src += src_pitch;
748 }
749
750 if (ddirty->transfer == SVGA3D_WRITE_HOST_VRAM) {
751 struct vmw_private *dev_priv;
752 struct vmw_stdu_update *cmd;
753 struct drm_clip_rect region;
754 int ret;
755
756 /* We are updating the actual surface, not a proxy */
757 region.x1 = ddirty->left;
758 region.x2 = ddirty->right;
759 region.y1 = ddirty->top;
760 region.y2 = ddirty->bottom;
761 ret = vmw_kms_update_proxy(
762 (struct vmw_resource *) &stdu->display_srf->res,
763 (const struct drm_clip_rect *) &region, 1, 1);
764 if (ret)
765 goto out_cleanup;
766
767
768 dev_priv = vmw_priv(stdu->base.crtc.dev);
769 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
770
771 if (!cmd) {
772 DRM_ERROR("Cannot reserve FIFO space to update STDU");
773 goto out_cleanup;
774 }
775
776 vmw_stdu_populate_update(cmd, stdu->base.unit,
777 ddirty->left, ddirty->right,
778 ddirty->top, ddirty->bottom);
779
780 vmw_fifo_commit(dev_priv, sizeof(*cmd));
781 }
782
98648ae6 783 ttm_bo_kunmap(&guest_map);
810b3e16 784out_cleanup:
a1ac6339 785 ddirty->left = ddirty->top = ddirty->fb_left = ddirty->fb_top = S32_MAX;
810b3e16
SY
786 ddirty->right = ddirty->bottom = S32_MIN;
787}
788
6bf6bf03
TH
789/**
790 * vmw_kms_stdu_dma - Perform a DMA transfer between a dma-buffer backed
791 * framebuffer and the screen target system.
792 *
793 * @dev_priv: Pointer to the device private structure.
794 * @file_priv: Pointer to a struct drm-file identifying the caller. May be
795 * set to NULL, but then @user_fence_rep must also be set to NULL.
796 * @vfb: Pointer to the dma-buffer backed framebuffer.
797 * @clips: Array of clip rects. Either @clips or @vclips must be NULL.
798 * @vclips: Alternate array of clip rects. Either @clips or @vclips must
799 * be NULL.
800 * @num_clips: Number of clip rects in @clips or @vclips.
801 * @increment: Increment to use when looping over @clips or @vclips.
802 * @to_surface: Whether to DMA to the screen target system as opposed to
803 * from the screen target system.
804 * @interruptible: Whether to perform waits interruptible if possible.
805 *
806 * If DMA-ing till the screen target system, the function will also notify
807 * the screen target system that a bounding box of the cliprects has been
808 * updated.
809 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
810 * interrupted.
811 */
812int vmw_kms_stdu_dma(struct vmw_private *dev_priv,
813 struct drm_file *file_priv,
814 struct vmw_framebuffer *vfb,
815 struct drm_vmw_fence_rep __user *user_fence_rep,
816 struct drm_clip_rect *clips,
817 struct drm_vmw_rect *vclips,
818 uint32_t num_clips,
819 int increment,
820 bool to_surface,
821 bool interruptible)
822{
823 struct vmw_dma_buffer *buf =
824 container_of(vfb, struct vmw_framebuffer_dmabuf, base)->buffer;
825 struct vmw_stdu_dirty ddirty;
826 int ret;
827
828 ret = vmw_kms_helper_buffer_prepare(dev_priv, buf, interruptible,
829 false);
830 if (ret)
831 return ret;
832
833 ddirty.transfer = (to_surface) ? SVGA3D_WRITE_HOST_VRAM :
834 SVGA3D_READ_HOST_VRAM;
835 ddirty.left = ddirty.top = S32_MAX;
836 ddirty.right = ddirty.bottom = S32_MIN;
a1ac6339 837 ddirty.fb_left = ddirty.fb_top = S32_MAX;
6bf6bf03
TH
838 ddirty.pitch = vfb->base.pitches[0];
839 ddirty.buf = buf;
840 ddirty.base.fifo_commit = vmw_stdu_dmabuf_fifo_commit;
841 ddirty.base.clip = vmw_stdu_dmabuf_clip;
842 ddirty.base.fifo_reserve_size = sizeof(struct vmw_stdu_dma) +
843 num_clips * sizeof(SVGA3dCopyBox) +
844 sizeof(SVGA3dCmdSurfaceDMASuffix);
845 if (to_surface)
846 ddirty.base.fifo_reserve_size += sizeof(struct vmw_stdu_update);
847
810b3e16
SY
848 /* 2D VMs cannot use SVGA_3D_CMD_SURFACE_DMA so do CPU blit instead */
849 if (!(dev_priv->capabilities & SVGA_CAP_3D)) {
850 ddirty.base.fifo_commit = vmw_stdu_dmabuf_cpu_commit;
851 ddirty.base.clip = vmw_stdu_dmabuf_cpu_clip;
852 ddirty.base.fifo_reserve_size = 0;
853 }
854
6bf6bf03
TH
855 ret = vmw_kms_helper_dirty(dev_priv, vfb, clips, vclips,
856 0, 0, num_clips, increment, &ddirty.base);
857 vmw_kms_helper_buffer_finish(dev_priv, file_priv, buf, NULL,
858 user_fence_rep);
859
860 return ret;
861}
862
863/**
864 * vmw_stdu_surface_clip - Callback to encode a surface copy command cliprect
865 *
866 * @dirty: The closure structure.
867 *
868 * Encodes a surface copy command cliprect and updates the bounding box
869 * for the copy.
870 */
871static void vmw_kms_stdu_surface_clip(struct vmw_kms_dirty *dirty)
872{
873 struct vmw_stdu_dirty *sdirty =
874 container_of(dirty, struct vmw_stdu_dirty, base);
875 struct vmw_stdu_surface_copy *cmd = dirty->cmd;
876 struct vmw_screen_target_display_unit *stdu =
877 container_of(dirty->unit, typeof(*stdu), base);
878
879 if (sdirty->sid != stdu->display_srf->res.id) {
880 struct SVGA3dCopyBox *blit = (struct SVGA3dCopyBox *) &cmd[1];
881
882 blit += dirty->num_hits;
883 blit->srcx = dirty->fb_x;
884 blit->srcy = dirty->fb_y;
885 blit->x = dirty->unit_x1;
886 blit->y = dirty->unit_y1;
887 blit->d = 1;
888 blit->w = dirty->unit_x2 - dirty->unit_x1;
889 blit->h = dirty->unit_y2 - dirty->unit_y1;
890 }
891
892 dirty->num_hits++;
893
894 /* Destination bounding box */
895 sdirty->left = min_t(s32, sdirty->left, dirty->unit_x1);
896 sdirty->top = min_t(s32, sdirty->top, dirty->unit_y1);
897 sdirty->right = max_t(s32, sdirty->right, dirty->unit_x2);
898 sdirty->bottom = max_t(s32, sdirty->bottom, dirty->unit_y2);
899}
900
901/**
902 * vmw_stdu_surface_fifo_commit - Callback to fill in and submit a surface
903 * copy command.
904 *
905 * @dirty: The closure structure.
906 *
907 * Fills in the missing fields in a surface copy command, and encodes a screen
908 * target update command.
909 */
910static void vmw_kms_stdu_surface_fifo_commit(struct vmw_kms_dirty *dirty)
911{
912 struct vmw_stdu_dirty *sdirty =
913 container_of(dirty, struct vmw_stdu_dirty, base);
914 struct vmw_screen_target_display_unit *stdu =
915 container_of(dirty->unit, typeof(*stdu), base);
916 struct vmw_stdu_surface_copy *cmd = dirty->cmd;
917 struct vmw_stdu_update *update;
918 size_t blit_size = sizeof(SVGA3dCopyBox) * dirty->num_hits;
919 size_t commit_size;
920
921 if (!dirty->num_hits) {
922 vmw_fifo_commit(dirty->dev_priv, 0);
923 return;
924 }
925
926 if (sdirty->sid != stdu->display_srf->res.id) {
927 struct SVGA3dCopyBox *blit = (struct SVGA3dCopyBox *) &cmd[1];
928
929 cmd->header.id = SVGA_3D_CMD_SURFACE_COPY;
930 cmd->header.size = sizeof(cmd->body) + blit_size;
931 cmd->body.src.sid = sdirty->sid;
932 cmd->body.dest.sid = stdu->display_srf->res.id;
933 update = (struct vmw_stdu_update *) &blit[dirty->num_hits];
934 commit_size = sizeof(*cmd) + blit_size + sizeof(*update);
935 } else {
936 update = dirty->cmd;
937 commit_size = sizeof(*update);
938 }
939
940 vmw_stdu_populate_update(update, stdu->base.unit, sdirty->left,
941 sdirty->right, sdirty->top, sdirty->bottom);
942
943 vmw_fifo_commit(dirty->dev_priv, commit_size);
944
945 sdirty->left = sdirty->top = S32_MAX;
946 sdirty->right = sdirty->bottom = S32_MIN;
947}
948
949/**
950 * vmw_kms_stdu_surface_dirty - Dirty part of a surface backed framebuffer
951 *
952 * @dev_priv: Pointer to the device private structure.
953 * @framebuffer: Pointer to the surface-buffer backed framebuffer.
954 * @clips: Array of clip rects. Either @clips or @vclips must be NULL.
955 * @vclips: Alternate array of clip rects. Either @clips or @vclips must
956 * be NULL.
957 * @srf: Pointer to surface to blit from. If NULL, the surface attached
958 * to @framebuffer will be used.
959 * @dest_x: X coordinate offset to align @srf with framebuffer coordinates.
960 * @dest_y: Y coordinate offset to align @srf with framebuffer coordinates.
961 * @num_clips: Number of clip rects in @clips.
962 * @inc: Increment to use when looping over @clips.
963 * @out_fence: If non-NULL, will return a ref-counted pointer to a
964 * struct vmw_fence_obj. The returned fence pointer may be NULL in which
965 * case the device has already synchronized.
966 *
967 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
968 * interrupted.
969 */
970int vmw_kms_stdu_surface_dirty(struct vmw_private *dev_priv,
971 struct vmw_framebuffer *framebuffer,
972 struct drm_clip_rect *clips,
973 struct drm_vmw_rect *vclips,
974 struct vmw_resource *srf,
975 s32 dest_x,
976 s32 dest_y,
977 unsigned num_clips, int inc,
978 struct vmw_fence_obj **out_fence)
979{
980 struct vmw_framebuffer_surface *vfbs =
981 container_of(framebuffer, typeof(*vfbs), base);
982 struct vmw_stdu_dirty sdirty;
73a88250 983 struct vmw_validation_ctx ctx;
6bf6bf03
TH
984 int ret;
985
986 if (!srf)
987 srf = &vfbs->surface->res;
988
73a88250 989 ret = vmw_kms_helper_resource_prepare(srf, true, &ctx);
6bf6bf03
TH
990 if (ret)
991 return ret;
992
993 if (vfbs->is_dmabuf_proxy) {
994 ret = vmw_kms_update_proxy(srf, clips, num_clips, inc);
995 if (ret)
996 goto out_finish;
997 }
998
999 sdirty.base.fifo_commit = vmw_kms_stdu_surface_fifo_commit;
1000 sdirty.base.clip = vmw_kms_stdu_surface_clip;
1001 sdirty.base.fifo_reserve_size = sizeof(struct vmw_stdu_surface_copy) +
1002 sizeof(SVGA3dCopyBox) * num_clips +
1003 sizeof(struct vmw_stdu_update);
1004 sdirty.sid = srf->id;
1005 sdirty.left = sdirty.top = S32_MAX;
1006 sdirty.right = sdirty.bottom = S32_MIN;
1007
1008 ret = vmw_kms_helper_dirty(dev_priv, framebuffer, clips, vclips,
1009 dest_x, dest_y, num_clips, inc,
1010 &sdirty.base);
1011out_finish:
73a88250 1012 vmw_kms_helper_resource_finish(&ctx, out_fence);
6bf6bf03
TH
1013
1014 return ret;
1015}
1016
35c05125
SY
1017
1018/*
1019 * Screen Target CRTC dispatch table
1020 */
d7955fcf 1021static const struct drm_crtc_funcs vmw_stdu_crtc_funcs = {
35c05125
SY
1022 .gamma_set = vmw_du_crtc_gamma_set,
1023 .destroy = vmw_stdu_crtc_destroy,
9c2542a4
SY
1024 .reset = vmw_du_crtc_reset,
1025 .atomic_duplicate_state = vmw_du_crtc_duplicate_state,
1026 .atomic_destroy_state = vmw_du_crtc_destroy_state,
904bb5e5 1027 .set_config = vmw_kms_set_config,
35c05125
SY
1028 .page_flip = vmw_stdu_crtc_page_flip,
1029};
1030
1031
1032
1033/******************************************************************************
1034 * Screen Target Display Unit Encoder Functions
1035 *****************************************************************************/
1036
1037/**
1038 * vmw_stdu_encoder_destroy - cleans up the STDU
1039 *
1040 * @encoder: used the get the containing STDU
1041 *
1042 * vmwgfx cleans up crtc/encoder/connector all at the same time so technically
1043 * this can be a no-op. Nevertheless, it doesn't hurt of have this in case
1044 * the common KMS code changes and somehow vmw_stdu_crtc_destroy() doesn't
1045 * get called.
1046 */
1047static void vmw_stdu_encoder_destroy(struct drm_encoder *encoder)
1048{
1049 vmw_stdu_destroy(vmw_encoder_to_stdu(encoder));
1050}
1051
d7955fcf 1052static const struct drm_encoder_funcs vmw_stdu_encoder_funcs = {
35c05125
SY
1053 .destroy = vmw_stdu_encoder_destroy,
1054};
1055
1056
1057
1058/******************************************************************************
1059 * Screen Target Display Unit Connector Functions
1060 *****************************************************************************/
1061
1062/**
1063 * vmw_stdu_connector_destroy - cleans up the STDU
1064 *
1065 * @connector: used to get the containing STDU
1066 *
1067 * vmwgfx cleans up crtc/encoder/connector all at the same time so technically
1068 * this can be a no-op. Nevertheless, it doesn't hurt of have this in case
1069 * the common KMS code changes and somehow vmw_stdu_crtc_destroy() doesn't
1070 * get called.
1071 */
1072static void vmw_stdu_connector_destroy(struct drm_connector *connector)
1073{
1074 vmw_stdu_destroy(vmw_connector_to_stdu(connector));
1075}
1076
1077
1078
d7955fcf 1079static const struct drm_connector_funcs vmw_stdu_connector_funcs = {
35c05125 1080 .dpms = vmw_du_connector_dpms,
35c05125
SY
1081 .detect = vmw_du_connector_detect,
1082 .fill_modes = vmw_du_connector_fill_modes,
1083 .set_property = vmw_du_connector_set_property,
1084 .destroy = vmw_stdu_connector_destroy,
d7721ca7
SY
1085 .reset = vmw_du_connector_reset,
1086 .atomic_duplicate_state = vmw_du_connector_duplicate_state,
1087 .atomic_destroy_state = vmw_du_connector_destroy_state,
1088 .atomic_set_property = vmw_du_connector_atomic_set_property,
1089 .atomic_get_property = vmw_du_connector_atomic_get_property,
35c05125
SY
1090};
1091
1092
d947d1b7
SY
1093static const struct
1094drm_connector_helper_funcs vmw_stdu_connector_helper_funcs = {
1095 .best_encoder = drm_atomic_helper_best_encoder,
1096};
1097
1098
35c05125 1099
36cc79bc
SY
1100/******************************************************************************
1101 * Screen Target Display Plane Functions
1102 *****************************************************************************/
1103
060e2ad5
SY
1104
1105
1106/**
1107 * vmw_stdu_primary_plane_cleanup_fb - Unpins the display surface
1108 *
1109 * @plane: display plane
1110 * @old_state: Contains the FB to clean up
1111 *
1112 * Unpins the display surface
1113 *
1114 * Returns 0 on success
1115 */
1116static void
1117vmw_stdu_primary_plane_cleanup_fb(struct drm_plane *plane,
1118 struct drm_plane_state *old_state)
1119{
1120 struct vmw_plane_state *vps = vmw_plane_state_to_vps(old_state);
1121
810b3e16
SY
1122 if (vps->host_map.virtual)
1123 ttm_bo_kunmap(&vps->host_map);
1124
060e2ad5
SY
1125 if (vps->surf)
1126 WARN_ON(!vps->pinned);
1127
1128 vmw_du_plane_cleanup_fb(plane, old_state);
1129
1130 vps->content_fb_type = SAME_AS_DISPLAY;
810b3e16 1131 vps->cpp = 0;
060e2ad5
SY
1132}
1133
1134
1135
1136/**
1137 * vmw_stdu_primary_plane_prepare_fb - Readies the display surface
1138 *
1139 * @plane: display plane
1140 * @new_state: info on the new plane state, including the FB
1141 *
1142 * This function allocates a new display surface if the content is
1143 * backed by a DMA. The display surface is pinned here, and it'll
1144 * be unpinned in .cleanup_fb()
1145 *
1146 * Returns 0 on success
1147 */
1148static int
1149vmw_stdu_primary_plane_prepare_fb(struct drm_plane *plane,
1150 struct drm_plane_state *new_state)
1151{
810b3e16 1152 struct vmw_private *dev_priv = vmw_priv(plane->dev);
060e2ad5
SY
1153 struct drm_framebuffer *new_fb = new_state->fb;
1154 struct vmw_framebuffer *vfb;
1155 struct vmw_plane_state *vps = vmw_plane_state_to_vps(new_state);
1156 enum stdu_content_type new_content_type;
1157 struct vmw_framebuffer_surface *new_vfbs;
1158 struct drm_crtc *crtc = new_state->crtc;
1159 uint32_t hdisplay = new_state->crtc_w, vdisplay = new_state->crtc_h;
1160 int ret;
1161
1162 /* No FB to prepare */
1163 if (!new_fb) {
1164 if (vps->surf) {
1165 WARN_ON(vps->pinned != 0);
1166 vmw_surface_unreference(&vps->surf);
1167 }
1168
1169 return 0;
1170 }
1171
1172 vfb = vmw_framebuffer_to_vfb(new_fb);
1173 new_vfbs = (vfb->dmabuf) ? NULL : vmw_framebuffer_to_vfbs(new_fb);
1174
1175 if (new_vfbs && new_vfbs->surface->base_size.width == hdisplay &&
1176 new_vfbs->surface->base_size.height == vdisplay)
1177 new_content_type = SAME_AS_DISPLAY;
1178 else if (vfb->dmabuf)
1179 new_content_type = SEPARATE_DMA;
1180 else
1181 new_content_type = SEPARATE_SURFACE;
1182
1183 if (new_content_type != SAME_AS_DISPLAY) {
1184 struct vmw_surface content_srf;
1185 struct drm_vmw_size display_base_size = {0};
1186
1187 display_base_size.width = hdisplay;
1188 display_base_size.height = vdisplay;
1189 display_base_size.depth = 1;
1190
1191 /*
1192 * If content buffer is a DMA buf, then we have to construct
1193 * surface info
1194 */
1195 if (new_content_type == SEPARATE_DMA) {
1196
1197 switch (new_fb->format->cpp[0]*8) {
1198 case 32:
1199 content_srf.format = SVGA3D_X8R8G8B8;
1200 break;
1201
1202 case 16:
1203 content_srf.format = SVGA3D_R5G6B5;
1204 break;
1205
1206 case 8:
1207 content_srf.format = SVGA3D_P8;
1208 break;
1209
1210 default:
1211 DRM_ERROR("Invalid format\n");
1212 return -EINVAL;
1213 }
1214
1215 content_srf.flags = 0;
1216 content_srf.mip_levels[0] = 1;
1217 content_srf.multisample_count = 0;
1218 } else {
1219 content_srf = *new_vfbs->surface;
1220 }
1221
1222 if (vps->surf) {
1223 struct drm_vmw_size cur_base_size = vps->surf->base_size;
1224
1225 if (cur_base_size.width != display_base_size.width ||
1226 cur_base_size.height != display_base_size.height ||
1227 vps->surf->format != content_srf.format) {
1228 WARN_ON(vps->pinned != 0);
1229 vmw_surface_unreference(&vps->surf);
1230 }
1231
1232 }
1233
1234 if (!vps->surf) {
1235 ret = vmw_surface_gb_priv_define
1236 (crtc->dev,
1237 /* Kernel visible only */
1238 0,
1239 content_srf.flags,
1240 content_srf.format,
1241 true, /* a scanout buffer */
1242 content_srf.mip_levels[0],
1243 content_srf.multisample_count,
1244 0,
1245 display_base_size,
1246 &vps->surf);
1247 if (ret != 0) {
1248 DRM_ERROR("Couldn't allocate STDU surface.\n");
1249 return ret;
1250 }
1251 }
1252 } else {
1253 /*
1254 * prepare_fb and clean_fb should only take care of pinning
1255 * and unpinning. References are tracked by state objects.
1256 * The only time we add a reference in prepare_fb is if the
1257 * state object doesn't have a reference to begin with
1258 */
1259 if (vps->surf) {
1260 WARN_ON(vps->pinned != 0);
1261 vmw_surface_unreference(&vps->surf);
1262 }
1263
1264 vps->surf = vmw_surface_reference(new_vfbs->surface);
1265 }
1266
1267 if (vps->surf) {
1268
1269 /* Pin new surface before flipping */
1270 ret = vmw_resource_pin(&vps->surf->res, false);
1271 if (ret)
1272 goto out_srf_unref;
1273
1274 vps->pinned++;
1275 }
1276
1277 vps->content_fb_type = new_content_type;
810b3e16
SY
1278
1279 /*
1280 * This should only happen if the DMA buf is too large to create a
1281 * proxy surface for.
1282 * If we are a 2D VM with a DMA buffer then we have to use CPU blit
1283 * so cache these mappings
1284 */
1285 if (vps->content_fb_type == SEPARATE_DMA &&
1286 !(dev_priv->capabilities & SVGA_CAP_3D)) {
810b3e16
SY
1287 ret = ttm_bo_kmap(&vps->surf->res.backup->base, 0,
1288 vps->surf->res.backup->base.num_pages,
1289 &vps->host_map);
1290 if (ret) {
1291 DRM_ERROR("Failed to map display buffer to CPU\n");
810b3e16
SY
1292 goto out_srf_unpin;
1293 }
1294
1295 vps->cpp = new_fb->pitches[0] / new_fb->width;
1296 }
1297
060e2ad5
SY
1298 return 0;
1299
810b3e16
SY
1300out_srf_unpin:
1301 vmw_resource_unpin(&vps->surf->res);
1302 vps->pinned--;
1303
060e2ad5
SY
1304out_srf_unref:
1305 vmw_surface_unreference(&vps->surf);
1306 return ret;
1307}
1308
1309
1310
1311/**
1312 * vmw_stdu_primary_plane_atomic_update - formally switches STDU to new plane
1313 *
1314 * @plane: display plane
1315 * @old_state: Only used to get crtc info
1316 *
1317 * Formally update stdu->display_srf to the new plane, and bind the new
1318 * plane STDU. This function is called during the commit phase when
1319 * all the preparation have been done and all the configurations have
1320 * been checked.
1321 */
1322static void
1323vmw_stdu_primary_plane_atomic_update(struct drm_plane *plane,
1324 struct drm_plane_state *old_state)
1325{
1326 struct vmw_private *dev_priv;
1327 struct vmw_screen_target_display_unit *stdu;
1328 struct vmw_plane_state *vps = vmw_plane_state_to_vps(plane->state);
1329 struct drm_crtc *crtc = plane->state->crtc ?: old_state->crtc;
1330 int ret;
1331
1332 stdu = vmw_crtc_to_stdu(crtc);
1333 dev_priv = vmw_priv(crtc->dev);
1334
1335 stdu->display_srf = vps->surf;
1336 stdu->content_fb_type = vps->content_fb_type;
810b3e16 1337 stdu->cpp = vps->cpp;
810b3e16 1338 memcpy(&stdu->host_map, &vps->host_map, sizeof(vps->host_map));
060e2ad5
SY
1339
1340 if (!stdu->defined)
1341 return;
1342
1343 if (plane->state->fb)
1344 ret = vmw_stdu_bind_st(dev_priv, stdu, &stdu->display_srf->res);
1345 else
1346 ret = vmw_stdu_bind_st(dev_priv, stdu, NULL);
1347
1348 /*
1349 * We cannot really fail this function, so if we do, then output an
1350 * error and quit
1351 */
1352 if (ret)
1353 DRM_ERROR("Failed to bind surface to STDU.\n");
1354 else
1355 crtc->primary->fb = plane->state->fb;
8a309c8a
SY
1356
1357 ret = vmw_stdu_update_st(dev_priv, stdu);
1358
1359 if (ret)
1360 DRM_ERROR("Failed to update STDU.\n");
060e2ad5
SY
1361}
1362
1363
36cc79bc 1364static const struct drm_plane_funcs vmw_stdu_plane_funcs = {
904bb5e5
SY
1365 .update_plane = drm_atomic_helper_update_plane,
1366 .disable_plane = drm_atomic_helper_disable_plane,
36cc79bc 1367 .destroy = vmw_du_primary_plane_destroy,
cc5ec459
SY
1368 .reset = vmw_du_plane_reset,
1369 .atomic_duplicate_state = vmw_du_plane_duplicate_state,
1370 .atomic_destroy_state = vmw_du_plane_destroy_state,
36cc79bc
SY
1371};
1372
1373static const struct drm_plane_funcs vmw_stdu_cursor_funcs = {
904bb5e5
SY
1374 .update_plane = drm_atomic_helper_update_plane,
1375 .disable_plane = drm_atomic_helper_disable_plane,
36cc79bc 1376 .destroy = vmw_du_cursor_plane_destroy,
cc5ec459
SY
1377 .reset = vmw_du_plane_reset,
1378 .atomic_duplicate_state = vmw_du_plane_duplicate_state,
1379 .atomic_destroy_state = vmw_du_plane_destroy_state,
36cc79bc
SY
1380};
1381
1382
06ec4190
SY
1383/*
1384 * Atomic Helpers
1385 */
060e2ad5
SY
1386static const struct
1387drm_plane_helper_funcs vmw_stdu_cursor_plane_helper_funcs = {
1388 .atomic_check = vmw_du_cursor_plane_atomic_check,
1389 .atomic_update = vmw_du_cursor_plane_atomic_update,
1390 .prepare_fb = vmw_du_cursor_plane_prepare_fb,
1391 .cleanup_fb = vmw_du_plane_cleanup_fb,
1392};
1393
1394static const struct
1395drm_plane_helper_funcs vmw_stdu_primary_plane_helper_funcs = {
1396 .atomic_check = vmw_du_primary_plane_atomic_check,
1397 .atomic_update = vmw_stdu_primary_plane_atomic_update,
1398 .prepare_fb = vmw_stdu_primary_plane_prepare_fb,
1399 .cleanup_fb = vmw_stdu_primary_plane_cleanup_fb,
1400};
1401
06ec4190
SY
1402static const struct drm_crtc_helper_funcs vmw_stdu_crtc_helper_funcs = {
1403 .prepare = vmw_stdu_crtc_helper_prepare,
06ec4190
SY
1404 .mode_set_nofb = vmw_stdu_crtc_mode_set_nofb,
1405 .atomic_check = vmw_du_crtc_atomic_check,
1406 .atomic_begin = vmw_du_crtc_atomic_begin,
1407 .atomic_flush = vmw_du_crtc_atomic_flush,
0b20a0f8 1408 .atomic_enable = vmw_stdu_crtc_atomic_enable,
64581714 1409 .atomic_disable = vmw_stdu_crtc_atomic_disable,
06ec4190
SY
1410};
1411
1412
35c05125
SY
1413/**
1414 * vmw_stdu_init - Sets up a Screen Target Display Unit
1415 *
1416 * @dev_priv: VMW DRM device
1417 * @unit: unit number range from 0 to VMWGFX_NUM_DISPLAY_UNITS
1418 *
1419 * This function is called once per CRTC, and allocates one Screen Target
1420 * display unit to represent that CRTC. Since the SVGA device does not separate
1421 * out encoder and connector, they are represented as part of the STDU as well.
1422 */
1423static int vmw_stdu_init(struct vmw_private *dev_priv, unsigned unit)
1424{
1425 struct vmw_screen_target_display_unit *stdu;
1426 struct drm_device *dev = dev_priv->dev;
1427 struct drm_connector *connector;
1428 struct drm_encoder *encoder;
36cc79bc 1429 struct drm_plane *primary, *cursor;
35c05125 1430 struct drm_crtc *crtc;
36cc79bc 1431 int ret;
35c05125
SY
1432
1433
1434 stdu = kzalloc(sizeof(*stdu), GFP_KERNEL);
1435 if (!stdu)
1436 return -ENOMEM;
1437
1438 stdu->base.unit = unit;
1439 crtc = &stdu->base.crtc;
1440 encoder = &stdu->base.encoder;
1441 connector = &stdu->base.connector;
36cc79bc
SY
1442 primary = &stdu->base.primary;
1443 cursor = &stdu->base.cursor;
35c05125
SY
1444
1445 stdu->base.pref_active = (unit == 0);
1446 stdu->base.pref_width = dev_priv->initial_width;
1447 stdu->base.pref_height = dev_priv->initial_height;
9c2542a4
SY
1448
1449 /*
1450 * Remove this after enabling atomic because property values can
1451 * only exist in a state object
1452 */
2e69b25b 1453 stdu->base.is_implicit = false;
35c05125 1454
36cc79bc 1455 /* Initialize primary plane */
cc5ec459
SY
1456 vmw_du_plane_reset(primary);
1457
36cc79bc
SY
1458 ret = drm_universal_plane_init(dev, primary,
1459 0, &vmw_stdu_plane_funcs,
1460 vmw_primary_plane_formats,
1461 ARRAY_SIZE(vmw_primary_plane_formats),
e6fc3b68 1462 NULL, DRM_PLANE_TYPE_PRIMARY, NULL);
36cc79bc
SY
1463 if (ret) {
1464 DRM_ERROR("Failed to initialize primary plane");
1465 goto err_free;
1466 }
1467
060e2ad5
SY
1468 drm_plane_helper_add(primary, &vmw_stdu_primary_plane_helper_funcs);
1469
36cc79bc 1470 /* Initialize cursor plane */
cc5ec459
SY
1471 vmw_du_plane_reset(cursor);
1472
36cc79bc
SY
1473 ret = drm_universal_plane_init(dev, cursor,
1474 0, &vmw_stdu_cursor_funcs,
1475 vmw_cursor_plane_formats,
1476 ARRAY_SIZE(vmw_cursor_plane_formats),
e6fc3b68 1477 NULL, DRM_PLANE_TYPE_CURSOR, NULL);
36cc79bc
SY
1478 if (ret) {
1479 DRM_ERROR("Failed to initialize cursor plane");
1480 drm_plane_cleanup(&stdu->base.primary);
1481 goto err_free;
1482 }
1483
060e2ad5
SY
1484 drm_plane_helper_add(cursor, &vmw_stdu_cursor_plane_helper_funcs);
1485
d7721ca7
SY
1486 vmw_du_connector_reset(connector);
1487
36cc79bc
SY
1488 ret = drm_connector_init(dev, connector, &vmw_stdu_connector_funcs,
1489 DRM_MODE_CONNECTOR_VIRTUAL);
1490 if (ret) {
1491 DRM_ERROR("Failed to initialize connector\n");
1492 goto err_free;
1493 }
d947d1b7
SY
1494
1495 drm_connector_helper_add(connector, &vmw_stdu_connector_helper_funcs);
35c05125 1496 connector->status = vmw_du_connector_detect(connector, false);
d7721ca7 1497 vmw_connector_state_to_vcs(connector->state)->is_implicit = false;
35c05125 1498
36cc79bc
SY
1499 ret = drm_encoder_init(dev, encoder, &vmw_stdu_encoder_funcs,
1500 DRM_MODE_ENCODER_VIRTUAL, NULL);
1501 if (ret) {
1502 DRM_ERROR("Failed to initialize encoder\n");
1503 goto err_free_connector;
1504 }
1505
1506 (void) drm_mode_connector_attach_encoder(connector, encoder);
35c05125
SY
1507 encoder->possible_crtcs = (1 << unit);
1508 encoder->possible_clones = 0;
1509
36cc79bc
SY
1510 ret = drm_connector_register(connector);
1511 if (ret) {
1512 DRM_ERROR("Failed to register connector\n");
1513 goto err_free_encoder;
1514 }
35c05125 1515
d7721ca7 1516 vmw_du_crtc_reset(crtc);
36cc79bc
SY
1517 ret = drm_crtc_init_with_planes(dev, crtc, &stdu->base.primary,
1518 &stdu->base.cursor,
1519 &vmw_stdu_crtc_funcs, NULL);
1520 if (ret) {
1521 DRM_ERROR("Failed to initialize CRTC\n");
1522 goto err_free_unregister;
1523 }
35c05125 1524
06ec4190
SY
1525 drm_crtc_helper_add(crtc, &vmw_stdu_crtc_helper_funcs);
1526
35c05125
SY
1527 drm_mode_crtc_set_gamma_size(crtc, 256);
1528
578e609a
TH
1529 drm_object_attach_property(&connector->base,
1530 dev_priv->hotplug_mode_update_property, 1);
1531 drm_object_attach_property(&connector->base,
1532 dev->mode_config.suggested_x_property, 0);
1533 drm_object_attach_property(&connector->base,
1534 dev->mode_config.suggested_y_property, 0);
76404ac0
TH
1535 if (dev_priv->implicit_placement_property)
1536 drm_object_attach_property
1537 (&connector->base,
1538 dev_priv->implicit_placement_property,
1539 stdu->base.is_implicit);
35c05125 1540 return 0;
36cc79bc
SY
1541
1542err_free_unregister:
1543 drm_connector_unregister(connector);
1544err_free_encoder:
1545 drm_encoder_cleanup(encoder);
1546err_free_connector:
1547 drm_connector_cleanup(connector);
1548err_free:
1549 kfree(stdu);
1550 return ret;
35c05125
SY
1551}
1552
1553
1554
1555/**
1556 * vmw_stdu_destroy - Cleans up a vmw_screen_target_display_unit
1557 *
1558 * @stdu: Screen Target Display Unit to be destroyed
1559 *
1560 * Clean up after vmw_stdu_init
1561 */
1562static void vmw_stdu_destroy(struct vmw_screen_target_display_unit *stdu)
1563{
35c05125
SY
1564 vmw_du_cleanup(&stdu->base);
1565 kfree(stdu);
1566}
1567
1568
1569
1570/******************************************************************************
1571 * Screen Target Display KMS Functions
1572 *
1573 * These functions are called by the common KMS code in vmwgfx_kms.c
1574 *****************************************************************************/
1575
1576/**
1577 * vmw_kms_stdu_init_display - Initializes a Screen Target based display
1578 *
1579 * @dev_priv: VMW DRM device
1580 *
1581 * This function initialize a Screen Target based display device. It checks
1582 * the capability bits to make sure the underlying hardware can support
1583 * screen targets, and then creates the maximum number of CRTCs, a.k.a Display
1584 * Units, as supported by the display hardware.
1585 *
1586 * RETURNS:
1587 * 0 on success, error code otherwise
1588 */
1589int vmw_kms_stdu_init_display(struct vmw_private *dev_priv)
1590{
1591 struct drm_device *dev = dev_priv->dev;
1592 int i, ret;
1593
1594
1595 /* Do nothing if Screen Target support is turned off */
1596 if (!VMWGFX_ENABLE_SCREEN_TARGET_OTABLE)
1597 return -ENOSYS;
1598
f89c6c32 1599 if (!(dev_priv->capabilities & SVGA_CAP_GBOBJECTS))
35c05125
SY
1600 return -ENOSYS;
1601
1602 ret = drm_vblank_init(dev, VMWGFX_NUM_DISPLAY_UNITS);
1603 if (unlikely(ret != 0))
1604 return ret;
1605
6bf6bf03
TH
1606 dev_priv->active_display_unit = vmw_du_screen_target;
1607
28c95429
SY
1608 if (dev_priv->capabilities & SVGA_CAP_3D) {
1609 /*
1610 * For 3D VMs, display (scanout) buffer size is the smaller of
1611 * max texture and max STDU
1612 */
1613 uint32_t max_width, max_height;
1614
1615 max_width = min(dev_priv->texture_max_width,
1616 dev_priv->stdu_max_width);
1617 max_height = min(dev_priv->texture_max_height,
1618 dev_priv->stdu_max_height);
1619
1620 dev->mode_config.max_width = max_width;
1621 dev->mode_config.max_height = max_height;
1622 } else {
810b3e16
SY
1623 /*
1624 * Given various display aspect ratios, there's no way to
1625 * estimate these using prim_bb_mem. So just set these to
1626 * something arbitrarily large and we will reject any layout
1627 * that doesn't fit prim_bb_mem later
1628 */
7b009e76
SY
1629 dev->mode_config.max_width = 8192;
1630 dev->mode_config.max_height = 8192;
810b3e16
SY
1631 }
1632
76404ac0
TH
1633 vmw_kms_create_implicit_placement_property(dev_priv, false);
1634
35c05125
SY
1635 for (i = 0; i < VMWGFX_NUM_DISPLAY_UNITS; ++i) {
1636 ret = vmw_stdu_init(dev_priv, i);
1637
1638 if (unlikely(ret != 0)) {
1639 DRM_ERROR("Failed to initialize STDU %d", i);
5f58e974 1640 return ret;
35c05125
SY
1641 }
1642 }
1643
35c05125
SY
1644 DRM_INFO("Screen Target Display device initialized\n");
1645
35c05125
SY
1646 return 0;
1647}