1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /**************************************************************************
3  *
4  * Copyright 2011-2022 VMware, Inc., Palo Alto, CA., USA
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 <drm/drm_atomic.h>
29 #include <drm/drm_atomic_helper.h>
30 #include <drm/drm_damage_helper.h>
31 #include <drm/drm_fourcc.h>
32 
33 #include "vmwgfx_kms.h"
34 
35 #define vmw_crtc_to_sou(x) \
36 	container_of(x, struct vmw_screen_object_unit, base.crtc)
37 #define vmw_encoder_to_sou(x) \
38 	container_of(x, struct vmw_screen_object_unit, base.encoder)
39 #define vmw_connector_to_sou(x) \
40 	container_of(x, struct vmw_screen_object_unit, base.connector)
41 
42 /**
43  * struct vmw_kms_sou_surface_dirty - Closure structure for
44  * blit surface to screen command.
45  * @base: The base type we derive from. Used by vmw_kms_helper_dirty().
46  * @left: Left side of bounding box.
47  * @right: Right side of bounding box.
48  * @top: Top side of bounding box.
49  * @bottom: Bottom side of bounding box.
50  * @dst_x: Difference between source clip rects and framebuffer coordinates.
51  * @dst_y: Difference between source clip rects and framebuffer coordinates.
52  * @sid: Surface id of surface to copy from.
53  */
54 struct vmw_kms_sou_surface_dirty {
55 	struct vmw_kms_dirty base;
56 	s32 left, right, top, bottom;
57 	s32 dst_x, dst_y;
58 	u32 sid;
59 };
60 
61 /*
62  * SVGA commands that are used by this code. Please see the device headers
63  * for explanation.
64  */
65 struct vmw_kms_sou_readback_blit {
66 	uint32 header;
67 	SVGAFifoCmdBlitScreenToGMRFB body;
68 };
69 
70 struct vmw_kms_sou_bo_blit {
71 	uint32 header;
72 	SVGAFifoCmdBlitGMRFBToScreen body;
73 };
74 
75 struct vmw_kms_sou_dirty_cmd {
76 	SVGA3dCmdHeader header;
77 	SVGA3dCmdBlitSurfaceToScreen body;
78 };
79 
80 struct vmw_kms_sou_define_gmrfb {
81 	uint32_t header;
82 	SVGAFifoCmdDefineGMRFB body;
83 };
84 
85 /*
86  * Display unit using screen objects.
87  */
88 struct vmw_screen_object_unit {
89 	struct vmw_display_unit base;
90 
91 	unsigned long buffer_size; /**< Size of allocated buffer */
92 	struct vmw_buffer_object *buffer; /**< Backing store buffer */
93 
94 	bool defined;
95 };
96 
97 static void vmw_sou_destroy(struct vmw_screen_object_unit *sou)
98 {
99 	vmw_du_cleanup(&sou->base);
100 	kfree(sou);
101 }
102 
103 
104 /*
105  * Screen Object Display Unit CRTC functions
106  */
107 
108 static void vmw_sou_crtc_destroy(struct drm_crtc *crtc)
109 {
110 	vmw_sou_destroy(vmw_crtc_to_sou(crtc));
111 }
112 
113 /*
114  * Send the fifo command to create a screen.
115  */
116 static int vmw_sou_fifo_create(struct vmw_private *dev_priv,
117 			       struct vmw_screen_object_unit *sou,
118 			       int x, int y,
119 			       struct drm_display_mode *mode)
120 {
121 	size_t fifo_size;
122 
123 	struct {
124 		struct {
125 			uint32_t cmdType;
126 		} header;
127 		SVGAScreenObject obj;
128 	} *cmd;
129 
130 	BUG_ON(!sou->buffer);
131 
132 	fifo_size = sizeof(*cmd);
133 	cmd = VMW_CMD_RESERVE(dev_priv, fifo_size);
134 	if (unlikely(cmd == NULL))
135 		return -ENOMEM;
136 
137 	memset(cmd, 0, fifo_size);
138 	cmd->header.cmdType = SVGA_CMD_DEFINE_SCREEN;
139 	cmd->obj.structSize = sizeof(SVGAScreenObject);
140 	cmd->obj.id = sou->base.unit;
141 	cmd->obj.flags = SVGA_SCREEN_HAS_ROOT |
142 		(sou->base.unit == 0 ? SVGA_SCREEN_IS_PRIMARY : 0);
143 	cmd->obj.size.width = mode->hdisplay;
144 	cmd->obj.size.height = mode->vdisplay;
145 	cmd->obj.root.x = x;
146 	cmd->obj.root.y = y;
147 	sou->base.set_gui_x = cmd->obj.root.x;
148 	sou->base.set_gui_y = cmd->obj.root.y;
149 
150 	/* Ok to assume that buffer is pinned in vram */
151 	vmw_bo_get_guest_ptr(&sou->buffer->base, &cmd->obj.backingStore.ptr);
152 	cmd->obj.backingStore.pitch = mode->hdisplay * 4;
153 
154 	vmw_cmd_commit(dev_priv, fifo_size);
155 
156 	sou->defined = true;
157 
158 	return 0;
159 }
160 
161 /*
162  * Send the fifo command to destroy a screen.
163  */
164 static int vmw_sou_fifo_destroy(struct vmw_private *dev_priv,
165 				struct vmw_screen_object_unit *sou)
166 {
167 	size_t fifo_size;
168 	int ret;
169 
170 	struct {
171 		struct {
172 			uint32_t cmdType;
173 		} header;
174 		SVGAFifoCmdDestroyScreen body;
175 	} *cmd;
176 
177 	/* no need to do anything */
178 	if (unlikely(!sou->defined))
179 		return 0;
180 
181 	fifo_size = sizeof(*cmd);
182 	cmd = VMW_CMD_RESERVE(dev_priv, fifo_size);
183 	if (unlikely(cmd == NULL))
184 		return -ENOMEM;
185 
186 	memset(cmd, 0, fifo_size);
187 	cmd->header.cmdType = SVGA_CMD_DESTROY_SCREEN;
188 	cmd->body.screenId = sou->base.unit;
189 
190 	vmw_cmd_commit(dev_priv, fifo_size);
191 
192 	/* Force sync */
193 	ret = vmw_fallback_wait(dev_priv, false, true, 0, false, 3*HZ);
194 	if (unlikely(ret != 0))
195 		DRM_ERROR("Failed to sync with HW");
196 	else
197 		sou->defined = false;
198 
199 	return ret;
200 }
201 
202 /**
203  * vmw_sou_crtc_mode_set_nofb - Create new screen
204  *
205  * @crtc: CRTC associated with the new screen
206  *
207  * This function creates/destroys a screen.  This function cannot fail, so if
208  * somehow we run into a failure, just do the best we can to get out.
209  */
210 static void vmw_sou_crtc_mode_set_nofb(struct drm_crtc *crtc)
211 {
212 	struct vmw_private *dev_priv;
213 	struct vmw_screen_object_unit *sou;
214 	struct vmw_framebuffer *vfb;
215 	struct drm_framebuffer *fb;
216 	struct drm_plane_state *ps;
217 	struct vmw_plane_state *vps;
218 	int ret;
219 
220 	sou = vmw_crtc_to_sou(crtc);
221 	dev_priv = vmw_priv(crtc->dev);
222 	ps = crtc->primary->state;
223 	fb = ps->fb;
224 	vps = vmw_plane_state_to_vps(ps);
225 
226 	vfb = (fb) ? vmw_framebuffer_to_vfb(fb) : NULL;
227 
228 	if (sou->defined) {
229 		ret = vmw_sou_fifo_destroy(dev_priv, sou);
230 		if (ret) {
231 			DRM_ERROR("Failed to destroy Screen Object\n");
232 			return;
233 		}
234 	}
235 
236 	if (vfb) {
237 		struct drm_connector_state *conn_state;
238 		struct vmw_connector_state *vmw_conn_state;
239 		int x, y;
240 
241 		sou->buffer = vps->bo;
242 		sou->buffer_size = vps->bo_size;
243 
244 		conn_state = sou->base.connector.state;
245 		vmw_conn_state = vmw_connector_state_to_vcs(conn_state);
246 
247 		x = vmw_conn_state->gui_x;
248 		y = vmw_conn_state->gui_y;
249 
250 		ret = vmw_sou_fifo_create(dev_priv, sou, x, y, &crtc->mode);
251 		if (ret)
252 			DRM_ERROR("Failed to define Screen Object %dx%d\n",
253 				  crtc->x, crtc->y);
254 
255 	} else {
256 		sou->buffer = NULL;
257 		sou->buffer_size = 0;
258 	}
259 }
260 
261 /**
262  * vmw_sou_crtc_helper_prepare - Noop
263  *
264  * @crtc:  CRTC associated with the new screen
265  *
266  * Prepares the CRTC for a mode set, but we don't need to do anything here.
267  */
268 static void vmw_sou_crtc_helper_prepare(struct drm_crtc *crtc)
269 {
270 }
271 
272 /**
273  * vmw_sou_crtc_atomic_enable - Noop
274  *
275  * @crtc: CRTC associated with the new screen
276  * @state: Unused
277  *
278  * This is called after a mode set has been completed.
279  */
280 static void vmw_sou_crtc_atomic_enable(struct drm_crtc *crtc,
281 				       struct drm_atomic_state *state)
282 {
283 }
284 
285 /**
286  * vmw_sou_crtc_atomic_disable - Turns off CRTC
287  *
288  * @crtc: CRTC to be turned off
289  * @state: Unused
290  */
291 static void vmw_sou_crtc_atomic_disable(struct drm_crtc *crtc,
292 					struct drm_atomic_state *state)
293 {
294 	struct vmw_private *dev_priv;
295 	struct vmw_screen_object_unit *sou;
296 	int ret;
297 
298 
299 	if (!crtc) {
300 		DRM_ERROR("CRTC is NULL\n");
301 		return;
302 	}
303 
304 	sou = vmw_crtc_to_sou(crtc);
305 	dev_priv = vmw_priv(crtc->dev);
306 
307 	if (sou->defined) {
308 		ret = vmw_sou_fifo_destroy(dev_priv, sou);
309 		if (ret)
310 			DRM_ERROR("Failed to destroy Screen Object\n");
311 	}
312 }
313 
314 static const struct drm_crtc_funcs vmw_screen_object_crtc_funcs = {
315 	.gamma_set = vmw_du_crtc_gamma_set,
316 	.destroy = vmw_sou_crtc_destroy,
317 	.reset = vmw_du_crtc_reset,
318 	.atomic_duplicate_state = vmw_du_crtc_duplicate_state,
319 	.atomic_destroy_state = vmw_du_crtc_destroy_state,
320 	.set_config = drm_atomic_helper_set_config,
321 	.page_flip = drm_atomic_helper_page_flip,
322 };
323 
324 /*
325  * Screen Object Display Unit encoder functions
326  */
327 
328 static void vmw_sou_encoder_destroy(struct drm_encoder *encoder)
329 {
330 	vmw_sou_destroy(vmw_encoder_to_sou(encoder));
331 }
332 
333 static const struct drm_encoder_funcs vmw_screen_object_encoder_funcs = {
334 	.destroy = vmw_sou_encoder_destroy,
335 };
336 
337 /*
338  * Screen Object Display Unit connector functions
339  */
340 
341 static void vmw_sou_connector_destroy(struct drm_connector *connector)
342 {
343 	vmw_sou_destroy(vmw_connector_to_sou(connector));
344 }
345 
346 static const struct drm_connector_funcs vmw_sou_connector_funcs = {
347 	.dpms = vmw_du_connector_dpms,
348 	.detect = vmw_du_connector_detect,
349 	.fill_modes = vmw_du_connector_fill_modes,
350 	.destroy = vmw_sou_connector_destroy,
351 	.reset = vmw_du_connector_reset,
352 	.atomic_duplicate_state = vmw_du_connector_duplicate_state,
353 	.atomic_destroy_state = vmw_du_connector_destroy_state,
354 };
355 
356 
357 static const struct
358 drm_connector_helper_funcs vmw_sou_connector_helper_funcs = {
359 };
360 
361 
362 
363 /*
364  * Screen Object Display Plane Functions
365  */
366 
367 /**
368  * vmw_sou_primary_plane_cleanup_fb - Frees sou backing buffer
369  *
370  * @plane:  display plane
371  * @old_state: Contains the FB to clean up
372  *
373  * Unpins the display surface
374  *
375  * Returns 0 on success
376  */
377 static void
378 vmw_sou_primary_plane_cleanup_fb(struct drm_plane *plane,
379 				 struct drm_plane_state *old_state)
380 {
381 	struct vmw_plane_state *vps = vmw_plane_state_to_vps(old_state);
382 	struct drm_crtc *crtc = plane->state->crtc ?
383 		plane->state->crtc : old_state->crtc;
384 
385 	if (vps->bo)
386 		vmw_bo_unpin(vmw_priv(crtc->dev), vps->bo, false);
387 	vmw_bo_unreference(&vps->bo);
388 	vps->bo_size = 0;
389 
390 	vmw_du_plane_cleanup_fb(plane, old_state);
391 }
392 
393 
394 /**
395  * vmw_sou_primary_plane_prepare_fb - allocate backing buffer
396  *
397  * @plane:  display plane
398  * @new_state: info on the new plane state, including the FB
399  *
400  * The SOU backing buffer is our equivalent of the display plane.
401  *
402  * Returns 0 on success
403  */
404 static int
405 vmw_sou_primary_plane_prepare_fb(struct drm_plane *plane,
406 				 struct drm_plane_state *new_state)
407 {
408 	struct drm_framebuffer *new_fb = new_state->fb;
409 	struct drm_crtc *crtc = plane->state->crtc ?: new_state->crtc;
410 	struct vmw_plane_state *vps = vmw_plane_state_to_vps(new_state);
411 	struct vmw_private *dev_priv;
412 	size_t size;
413 	int ret;
414 
415 
416 	if (!new_fb) {
417 		vmw_bo_unreference(&vps->bo);
418 		vps->bo_size = 0;
419 
420 		return 0;
421 	}
422 
423 	size = new_state->crtc_w * new_state->crtc_h * 4;
424 	dev_priv = vmw_priv(crtc->dev);
425 
426 	if (vps->bo) {
427 		if (vps->bo_size == size) {
428 			/*
429 			 * Note that this might temporarily up the pin-count
430 			 * to 2, until cleanup_fb() is called.
431 			 */
432 			return vmw_bo_pin_in_vram(dev_priv, vps->bo,
433 						      true);
434 		}
435 
436 		vmw_bo_unreference(&vps->bo);
437 		vps->bo_size = 0;
438 	}
439 
440 	vmw_svga_enable(dev_priv);
441 
442 	/* After we have alloced the backing store might not be able to
443 	 * resume the overlays, this is preferred to failing to alloc.
444 	 */
445 	vmw_overlay_pause_all(dev_priv);
446 	ret = vmw_bo_create(dev_priv, size,
447 			    &vmw_vram_placement,
448 			    false, true, &vmw_bo_bo_free, &vps->bo);
449 	vmw_overlay_resume_all(dev_priv);
450 	if (ret) {
451 		vps->bo = NULL; /* vmw_bo_init frees on error */
452 		return ret;
453 	}
454 
455 	vps->bo_size = size;
456 
457 	/*
458 	 * TTM already thinks the buffer is pinned, but make sure the
459 	 * pin_count is upped.
460 	 */
461 	return vmw_bo_pin_in_vram(dev_priv, vps->bo, true);
462 }
463 
464 static uint32_t vmw_sou_bo_fifo_size(struct vmw_du_update_plane *update,
465 				     uint32_t num_hits)
466 {
467 	return sizeof(struct vmw_kms_sou_define_gmrfb) +
468 		sizeof(struct vmw_kms_sou_bo_blit) * num_hits;
469 }
470 
471 static uint32_t vmw_sou_bo_define_gmrfb(struct vmw_du_update_plane *update,
472 					void *cmd)
473 {
474 	struct vmw_framebuffer_bo *vfbbo =
475 		container_of(update->vfb, typeof(*vfbbo), base);
476 	struct vmw_kms_sou_define_gmrfb *gmr = cmd;
477 	int depth = update->vfb->base.format->depth;
478 
479 	/* Emulate RGBA support, contrary to svga_reg.h this is not
480 	 * supported by hosts. This is only a problem if we are reading
481 	 * this value later and expecting what we uploaded back.
482 	 */
483 	if (depth == 32)
484 		depth = 24;
485 
486 	gmr->header = SVGA_CMD_DEFINE_GMRFB;
487 
488 	gmr->body.format.bitsPerPixel = update->vfb->base.format->cpp[0] * 8;
489 	gmr->body.format.colorDepth = depth;
490 	gmr->body.format.reserved = 0;
491 	gmr->body.bytesPerLine = update->vfb->base.pitches[0];
492 	vmw_bo_get_guest_ptr(&vfbbo->buffer->base, &gmr->body.ptr);
493 
494 	return sizeof(*gmr);
495 }
496 
497 static uint32_t vmw_sou_bo_populate_clip(struct vmw_du_update_plane  *update,
498 					 void *cmd, struct drm_rect *clip,
499 					 uint32_t fb_x, uint32_t fb_y)
500 {
501 	struct vmw_kms_sou_bo_blit *blit = cmd;
502 
503 	blit->header = SVGA_CMD_BLIT_GMRFB_TO_SCREEN;
504 	blit->body.destScreenId = update->du->unit;
505 	blit->body.srcOrigin.x = fb_x;
506 	blit->body.srcOrigin.y = fb_y;
507 	blit->body.destRect.left = clip->x1;
508 	blit->body.destRect.top = clip->y1;
509 	blit->body.destRect.right = clip->x2;
510 	blit->body.destRect.bottom = clip->y2;
511 
512 	return sizeof(*blit);
513 }
514 
515 static uint32_t vmw_stud_bo_post_clip(struct vmw_du_update_plane  *update,
516 				      void *cmd, struct drm_rect *bb)
517 {
518 	return 0;
519 }
520 
521 /**
522  * vmw_sou_plane_update_bo - Update display unit for bo backed fb.
523  * @dev_priv: Device private.
524  * @plane: Plane state.
525  * @old_state: Old plane state.
526  * @vfb: Framebuffer which is blitted to display unit.
527  * @out_fence: If non-NULL, will return a ref-counted pointer to vmw_fence_obj.
528  *             The returned fence pointer may be NULL in which case the device
529  *             has already synchronized.
530  *
531  * Return: 0 on success or a negative error code on failure.
532  */
533 static int vmw_sou_plane_update_bo(struct vmw_private *dev_priv,
534 				   struct drm_plane *plane,
535 				   struct drm_plane_state *old_state,
536 				   struct vmw_framebuffer *vfb,
537 				   struct vmw_fence_obj **out_fence)
538 {
539 	struct vmw_du_update_plane_buffer bo_update;
540 
541 	memset(&bo_update, 0, sizeof(struct vmw_du_update_plane_buffer));
542 	bo_update.base.plane = plane;
543 	bo_update.base.old_state = old_state;
544 	bo_update.base.dev_priv = dev_priv;
545 	bo_update.base.du = vmw_crtc_to_du(plane->state->crtc);
546 	bo_update.base.vfb = vfb;
547 	bo_update.base.out_fence = out_fence;
548 	bo_update.base.mutex = NULL;
549 	bo_update.base.cpu_blit = false;
550 	bo_update.base.intr = true;
551 
552 	bo_update.base.calc_fifo_size = vmw_sou_bo_fifo_size;
553 	bo_update.base.post_prepare = vmw_sou_bo_define_gmrfb;
554 	bo_update.base.clip = vmw_sou_bo_populate_clip;
555 	bo_update.base.post_clip = vmw_stud_bo_post_clip;
556 
557 	return vmw_du_helper_plane_update(&bo_update.base);
558 }
559 
560 static uint32_t vmw_sou_surface_fifo_size(struct vmw_du_update_plane *update,
561 					  uint32_t num_hits)
562 {
563 	return sizeof(struct vmw_kms_sou_dirty_cmd) + sizeof(SVGASignedRect) *
564 		num_hits;
565 }
566 
567 static uint32_t vmw_sou_surface_post_prepare(struct vmw_du_update_plane *update,
568 					     void *cmd)
569 {
570 	struct vmw_du_update_plane_surface *srf_update;
571 
572 	srf_update = container_of(update, typeof(*srf_update), base);
573 
574 	/*
575 	 * SOU SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN is special in the sense that
576 	 * its bounding box is filled before iterating over all the clips. So
577 	 * store the FIFO start address and revisit to fill the details.
578 	 */
579 	srf_update->cmd_start = cmd;
580 
581 	return 0;
582 }
583 
584 static uint32_t vmw_sou_surface_pre_clip(struct vmw_du_update_plane *update,
585 					 void *cmd, uint32_t num_hits)
586 {
587 	struct vmw_kms_sou_dirty_cmd *blit = cmd;
588 	struct vmw_framebuffer_surface *vfbs;
589 
590 	vfbs = container_of(update->vfb, typeof(*vfbs), base);
591 
592 	blit->header.id = SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN;
593 	blit->header.size = sizeof(blit->body) + sizeof(SVGASignedRect) *
594 		num_hits;
595 
596 	blit->body.srcImage.sid = vfbs->surface->res.id;
597 	blit->body.destScreenId = update->du->unit;
598 
599 	/* Update the source and destination bounding box later in post_clip */
600 	blit->body.srcRect.left = 0;
601 	blit->body.srcRect.top = 0;
602 	blit->body.srcRect.right = 0;
603 	blit->body.srcRect.bottom = 0;
604 
605 	blit->body.destRect.left = 0;
606 	blit->body.destRect.top = 0;
607 	blit->body.destRect.right = 0;
608 	blit->body.destRect.bottom = 0;
609 
610 	return sizeof(*blit);
611 }
612 
613 static uint32_t vmw_sou_surface_clip_rect(struct vmw_du_update_plane *update,
614 					  void *cmd, struct drm_rect *clip,
615 					  uint32_t src_x, uint32_t src_y)
616 {
617 	SVGASignedRect *rect = cmd;
618 
619 	/*
620 	 * rects are relative to dest bounding box rect on screen object, so
621 	 * translate to it later in post_clip
622 	 */
623 	rect->left = clip->x1;
624 	rect->top = clip->y1;
625 	rect->right = clip->x2;
626 	rect->bottom = clip->y2;
627 
628 	return sizeof(*rect);
629 }
630 
631 static uint32_t vmw_sou_surface_post_clip(struct vmw_du_update_plane *update,
632 					  void *cmd, struct drm_rect *bb)
633 {
634 	struct vmw_du_update_plane_surface *srf_update;
635 	struct drm_plane_state *state = update->plane->state;
636 	struct drm_rect src_bb;
637 	struct vmw_kms_sou_dirty_cmd *blit;
638 	SVGASignedRect *rect;
639 	uint32_t num_hits;
640 	int translate_src_x;
641 	int translate_src_y;
642 	int i;
643 
644 	srf_update = container_of(update, typeof(*srf_update), base);
645 
646 	blit = srf_update->cmd_start;
647 	rect = (SVGASignedRect *)&blit[1];
648 
649 	num_hits = (blit->header.size - sizeof(blit->body))/
650 		sizeof(SVGASignedRect);
651 
652 	src_bb = *bb;
653 
654 	/* To translate bb back to fb src coord */
655 	translate_src_x = (state->src_x >> 16) - state->crtc_x;
656 	translate_src_y = (state->src_y >> 16) - state->crtc_y;
657 
658 	drm_rect_translate(&src_bb, translate_src_x, translate_src_y);
659 
660 	blit->body.srcRect.left = src_bb.x1;
661 	blit->body.srcRect.top = src_bb.y1;
662 	blit->body.srcRect.right = src_bb.x2;
663 	blit->body.srcRect.bottom = src_bb.y2;
664 
665 	blit->body.destRect.left = bb->x1;
666 	blit->body.destRect.top = bb->y1;
667 	blit->body.destRect.right = bb->x2;
668 	blit->body.destRect.bottom = bb->y2;
669 
670 	/* rects are relative to dest bb rect */
671 	for (i = 0; i < num_hits; i++) {
672 		rect->left -= bb->x1;
673 		rect->top -= bb->y1;
674 		rect->right -= bb->x1;
675 		rect->bottom -= bb->y1;
676 		rect++;
677 	}
678 
679 	return 0;
680 }
681 
682 /**
683  * vmw_sou_plane_update_surface - Update display unit for surface backed fb.
684  * @dev_priv: Device private.
685  * @plane: Plane state.
686  * @old_state: Old plane state.
687  * @vfb: Framebuffer which is blitted to display unit
688  * @out_fence: If non-NULL, will return a ref-counted pointer to vmw_fence_obj.
689  *             The returned fence pointer may be NULL in which case the device
690  *             has already synchronized.
691  *
692  * Return: 0 on success or a negative error code on failure.
693  */
694 static int vmw_sou_plane_update_surface(struct vmw_private *dev_priv,
695 					struct drm_plane *plane,
696 					struct drm_plane_state *old_state,
697 					struct vmw_framebuffer *vfb,
698 					struct vmw_fence_obj **out_fence)
699 {
700 	struct vmw_du_update_plane_surface srf_update;
701 
702 	memset(&srf_update, 0, sizeof(struct vmw_du_update_plane_surface));
703 	srf_update.base.plane = plane;
704 	srf_update.base.old_state = old_state;
705 	srf_update.base.dev_priv = dev_priv;
706 	srf_update.base.du = vmw_crtc_to_du(plane->state->crtc);
707 	srf_update.base.vfb = vfb;
708 	srf_update.base.out_fence = out_fence;
709 	srf_update.base.mutex = &dev_priv->cmdbuf_mutex;
710 	srf_update.base.cpu_blit = false;
711 	srf_update.base.intr = true;
712 
713 	srf_update.base.calc_fifo_size = vmw_sou_surface_fifo_size;
714 	srf_update.base.post_prepare = vmw_sou_surface_post_prepare;
715 	srf_update.base.pre_clip = vmw_sou_surface_pre_clip;
716 	srf_update.base.clip = vmw_sou_surface_clip_rect;
717 	srf_update.base.post_clip = vmw_sou_surface_post_clip;
718 
719 	return vmw_du_helper_plane_update(&srf_update.base);
720 }
721 
722 static void
723 vmw_sou_primary_plane_atomic_update(struct drm_plane *plane,
724 				    struct drm_atomic_state *state)
725 {
726 	struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state, plane);
727 	struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state, plane);
728 	struct drm_crtc *crtc = new_state->crtc;
729 	struct vmw_fence_obj *fence = NULL;
730 	int ret;
731 
732 	/* In case of device error, maintain consistent atomic state */
733 	if (crtc && new_state->fb) {
734 		struct vmw_private *dev_priv = vmw_priv(crtc->dev);
735 		struct vmw_framebuffer *vfb =
736 			vmw_framebuffer_to_vfb(new_state->fb);
737 
738 		if (vfb->bo)
739 			ret = vmw_sou_plane_update_bo(dev_priv, plane,
740 						      old_state, vfb, &fence);
741 		else
742 			ret = vmw_sou_plane_update_surface(dev_priv, plane,
743 							   old_state, vfb,
744 							   &fence);
745 		if (ret != 0)
746 			DRM_ERROR("Failed to update screen.\n");
747 	} else {
748 		/* Do nothing when fb and crtc is NULL (blank crtc) */
749 		return;
750 	}
751 
752 	if (fence)
753 		vmw_fence_obj_unreference(&fence);
754 }
755 
756 
757 static const struct drm_plane_funcs vmw_sou_plane_funcs = {
758 	.update_plane = drm_atomic_helper_update_plane,
759 	.disable_plane = drm_atomic_helper_disable_plane,
760 	.destroy = vmw_du_primary_plane_destroy,
761 	.reset = vmw_du_plane_reset,
762 	.atomic_duplicate_state = vmw_du_plane_duplicate_state,
763 	.atomic_destroy_state = vmw_du_plane_destroy_state,
764 };
765 
766 static const struct drm_plane_funcs vmw_sou_cursor_funcs = {
767 	.update_plane = drm_atomic_helper_update_plane,
768 	.disable_plane = drm_atomic_helper_disable_plane,
769 	.destroy = vmw_du_cursor_plane_destroy,
770 	.reset = vmw_du_plane_reset,
771 	.atomic_duplicate_state = vmw_du_plane_duplicate_state,
772 	.atomic_destroy_state = vmw_du_plane_destroy_state,
773 };
774 
775 /*
776  * Atomic Helpers
777  */
778 static const struct
779 drm_plane_helper_funcs vmw_sou_cursor_plane_helper_funcs = {
780 	.atomic_check = vmw_du_cursor_plane_atomic_check,
781 	.atomic_update = vmw_du_cursor_plane_atomic_update,
782 	.prepare_fb = vmw_du_cursor_plane_prepare_fb,
783 	.cleanup_fb = vmw_du_cursor_plane_cleanup_fb,
784 };
785 
786 static const struct
787 drm_plane_helper_funcs vmw_sou_primary_plane_helper_funcs = {
788 	.atomic_check = vmw_du_primary_plane_atomic_check,
789 	.atomic_update = vmw_sou_primary_plane_atomic_update,
790 	.prepare_fb = vmw_sou_primary_plane_prepare_fb,
791 	.cleanup_fb = vmw_sou_primary_plane_cleanup_fb,
792 };
793 
794 static const struct drm_crtc_helper_funcs vmw_sou_crtc_helper_funcs = {
795 	.prepare = vmw_sou_crtc_helper_prepare,
796 	.mode_set_nofb = vmw_sou_crtc_mode_set_nofb,
797 	.atomic_check = vmw_du_crtc_atomic_check,
798 	.atomic_begin = vmw_du_crtc_atomic_begin,
799 	.atomic_flush = vmw_du_crtc_atomic_flush,
800 	.atomic_enable = vmw_sou_crtc_atomic_enable,
801 	.atomic_disable = vmw_sou_crtc_atomic_disable,
802 };
803 
804 
805 static int vmw_sou_init(struct vmw_private *dev_priv, unsigned unit)
806 {
807 	struct vmw_screen_object_unit *sou;
808 	struct drm_device *dev = &dev_priv->drm;
809 	struct drm_connector *connector;
810 	struct drm_encoder *encoder;
811 	struct drm_plane *primary;
812 	struct vmw_cursor_plane *cursor;
813 	struct drm_crtc *crtc;
814 	int ret;
815 
816 	sou = kzalloc(sizeof(*sou), GFP_KERNEL);
817 	if (!sou)
818 		return -ENOMEM;
819 
820 	sou->base.unit = unit;
821 	crtc = &sou->base.crtc;
822 	encoder = &sou->base.encoder;
823 	connector = &sou->base.connector;
824 	primary = &sou->base.primary;
825 	cursor = &sou->base.cursor;
826 
827 	sou->base.pref_active = (unit == 0);
828 	sou->base.pref_width = dev_priv->initial_width;
829 	sou->base.pref_height = dev_priv->initial_height;
830 	sou->base.pref_mode = NULL;
831 
832 	/*
833 	 * Remove this after enabling atomic because property values can
834 	 * only exist in a state object
835 	 */
836 	sou->base.is_implicit = false;
837 
838 	/* Initialize primary plane */
839 	ret = drm_universal_plane_init(dev, primary,
840 				       0, &vmw_sou_plane_funcs,
841 				       vmw_primary_plane_formats,
842 				       ARRAY_SIZE(vmw_primary_plane_formats),
843 				       NULL, DRM_PLANE_TYPE_PRIMARY, NULL);
844 	if (ret) {
845 		DRM_ERROR("Failed to initialize primary plane");
846 		goto err_free;
847 	}
848 
849 	drm_plane_helper_add(primary, &vmw_sou_primary_plane_helper_funcs);
850 	drm_plane_enable_fb_damage_clips(primary);
851 
852 	/* Initialize cursor plane */
853 	ret = drm_universal_plane_init(dev, &cursor->base,
854 			0, &vmw_sou_cursor_funcs,
855 			vmw_cursor_plane_formats,
856 			ARRAY_SIZE(vmw_cursor_plane_formats),
857 			NULL, DRM_PLANE_TYPE_CURSOR, NULL);
858 	if (ret) {
859 		DRM_ERROR("Failed to initialize cursor plane");
860 		drm_plane_cleanup(&sou->base.primary);
861 		goto err_free;
862 	}
863 
864 	drm_plane_helper_add(&cursor->base, &vmw_sou_cursor_plane_helper_funcs);
865 
866 	ret = drm_connector_init(dev, connector, &vmw_sou_connector_funcs,
867 				 DRM_MODE_CONNECTOR_VIRTUAL);
868 	if (ret) {
869 		DRM_ERROR("Failed to initialize connector\n");
870 		goto err_free;
871 	}
872 
873 	drm_connector_helper_add(connector, &vmw_sou_connector_helper_funcs);
874 	connector->status = vmw_du_connector_detect(connector, true);
875 
876 	ret = drm_encoder_init(dev, encoder, &vmw_screen_object_encoder_funcs,
877 			       DRM_MODE_ENCODER_VIRTUAL, NULL);
878 	if (ret) {
879 		DRM_ERROR("Failed to initialize encoder\n");
880 		goto err_free_connector;
881 	}
882 
883 	(void) drm_connector_attach_encoder(connector, encoder);
884 	encoder->possible_crtcs = (1 << unit);
885 	encoder->possible_clones = 0;
886 
887 	ret = drm_connector_register(connector);
888 	if (ret) {
889 		DRM_ERROR("Failed to register connector\n");
890 		goto err_free_encoder;
891 	}
892 
893 	ret = drm_crtc_init_with_planes(dev, crtc, primary,
894 					&cursor->base,
895 					&vmw_screen_object_crtc_funcs, NULL);
896 	if (ret) {
897 		DRM_ERROR("Failed to initialize CRTC\n");
898 		goto err_free_unregister;
899 	}
900 
901 	drm_crtc_helper_add(crtc, &vmw_sou_crtc_helper_funcs);
902 
903 	drm_mode_crtc_set_gamma_size(crtc, 256);
904 
905 	drm_object_attach_property(&connector->base,
906 				   dev_priv->hotplug_mode_update_property, 1);
907 	drm_object_attach_property(&connector->base,
908 				   dev->mode_config.suggested_x_property, 0);
909 	drm_object_attach_property(&connector->base,
910 				   dev->mode_config.suggested_y_property, 0);
911 	return 0;
912 
913 err_free_unregister:
914 	drm_connector_unregister(connector);
915 err_free_encoder:
916 	drm_encoder_cleanup(encoder);
917 err_free_connector:
918 	drm_connector_cleanup(connector);
919 err_free:
920 	kfree(sou);
921 	return ret;
922 }
923 
924 int vmw_kms_sou_init_display(struct vmw_private *dev_priv)
925 {
926 	struct drm_device *dev = &dev_priv->drm;
927 	int i;
928 
929 	if (!(dev_priv->capabilities & SVGA_CAP_SCREEN_OBJECT_2)) {
930 		return -ENOSYS;
931 	}
932 
933 	for (i = 0; i < VMWGFX_NUM_DISPLAY_UNITS; ++i)
934 		vmw_sou_init(dev_priv, i);
935 
936 	dev_priv->active_display_unit = vmw_du_screen_object;
937 
938 	drm_mode_config_reset(dev);
939 
940 	return 0;
941 }
942 
943 static int do_bo_define_gmrfb(struct vmw_private *dev_priv,
944 				  struct vmw_framebuffer *framebuffer)
945 {
946 	struct vmw_buffer_object *buf =
947 		container_of(framebuffer, struct vmw_framebuffer_bo,
948 			     base)->buffer;
949 	int depth = framebuffer->base.format->depth;
950 	struct {
951 		uint32_t header;
952 		SVGAFifoCmdDefineGMRFB body;
953 	} *cmd;
954 
955 	/* Emulate RGBA support, contrary to svga_reg.h this is not
956 	 * supported by hosts. This is only a problem if we are reading
957 	 * this value later and expecting what we uploaded back.
958 	 */
959 	if (depth == 32)
960 		depth = 24;
961 
962 	cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd));
963 	if (!cmd)
964 		return -ENOMEM;
965 
966 	cmd->header = SVGA_CMD_DEFINE_GMRFB;
967 	cmd->body.format.bitsPerPixel = framebuffer->base.format->cpp[0] * 8;
968 	cmd->body.format.colorDepth = depth;
969 	cmd->body.format.reserved = 0;
970 	cmd->body.bytesPerLine = framebuffer->base.pitches[0];
971 	/* Buffer is reserved in vram or GMR */
972 	vmw_bo_get_guest_ptr(&buf->base, &cmd->body.ptr);
973 	vmw_cmd_commit(dev_priv, sizeof(*cmd));
974 
975 	return 0;
976 }
977 
978 /**
979  * vmw_sou_surface_fifo_commit - Callback to fill in and submit a
980  * blit surface to screen command.
981  *
982  * @dirty: The closure structure.
983  *
984  * Fills in the missing fields in the command, and translates the cliprects
985  * to match the destination bounding box encoded.
986  */
987 static void vmw_sou_surface_fifo_commit(struct vmw_kms_dirty *dirty)
988 {
989 	struct vmw_kms_sou_surface_dirty *sdirty =
990 		container_of(dirty, typeof(*sdirty), base);
991 	struct vmw_kms_sou_dirty_cmd *cmd = dirty->cmd;
992 	s32 trans_x = dirty->unit->crtc.x - sdirty->dst_x;
993 	s32 trans_y = dirty->unit->crtc.y - sdirty->dst_y;
994 	size_t region_size = dirty->num_hits * sizeof(SVGASignedRect);
995 	SVGASignedRect *blit = (SVGASignedRect *) &cmd[1];
996 	int i;
997 
998 	if (!dirty->num_hits) {
999 		vmw_cmd_commit(dirty->dev_priv, 0);
1000 		return;
1001 	}
1002 
1003 	cmd->header.id = SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN;
1004 	cmd->header.size = sizeof(cmd->body) + region_size;
1005 
1006 	/*
1007 	 * Use the destination bounding box to specify destination - and
1008 	 * source bounding regions.
1009 	 */
1010 	cmd->body.destRect.left = sdirty->left;
1011 	cmd->body.destRect.right = sdirty->right;
1012 	cmd->body.destRect.top = sdirty->top;
1013 	cmd->body.destRect.bottom = sdirty->bottom;
1014 
1015 	cmd->body.srcRect.left = sdirty->left + trans_x;
1016 	cmd->body.srcRect.right = sdirty->right + trans_x;
1017 	cmd->body.srcRect.top = sdirty->top + trans_y;
1018 	cmd->body.srcRect.bottom = sdirty->bottom + trans_y;
1019 
1020 	cmd->body.srcImage.sid = sdirty->sid;
1021 	cmd->body.destScreenId = dirty->unit->unit;
1022 
1023 	/* Blits are relative to the destination rect. Translate. */
1024 	for (i = 0; i < dirty->num_hits; ++i, ++blit) {
1025 		blit->left -= sdirty->left;
1026 		blit->right -= sdirty->left;
1027 		blit->top -= sdirty->top;
1028 		blit->bottom -= sdirty->top;
1029 	}
1030 
1031 	vmw_cmd_commit(dirty->dev_priv, region_size + sizeof(*cmd));
1032 
1033 	sdirty->left = sdirty->top = S32_MAX;
1034 	sdirty->right = sdirty->bottom = S32_MIN;
1035 }
1036 
1037 /**
1038  * vmw_sou_surface_clip - Callback to encode a blit surface to screen cliprect.
1039  *
1040  * @dirty: The closure structure
1041  *
1042  * Encodes a SVGASignedRect cliprect and updates the bounding box of the
1043  * BLIT_SURFACE_TO_SCREEN command.
1044  */
1045 static void vmw_sou_surface_clip(struct vmw_kms_dirty *dirty)
1046 {
1047 	struct vmw_kms_sou_surface_dirty *sdirty =
1048 		container_of(dirty, typeof(*sdirty), base);
1049 	struct vmw_kms_sou_dirty_cmd *cmd = dirty->cmd;
1050 	SVGASignedRect *blit = (SVGASignedRect *) &cmd[1];
1051 
1052 	/* Destination rect. */
1053 	blit += dirty->num_hits;
1054 	blit->left = dirty->unit_x1;
1055 	blit->top = dirty->unit_y1;
1056 	blit->right = dirty->unit_x2;
1057 	blit->bottom = dirty->unit_y2;
1058 
1059 	/* Destination bounding box */
1060 	sdirty->left = min_t(s32, sdirty->left, dirty->unit_x1);
1061 	sdirty->top = min_t(s32, sdirty->top, dirty->unit_y1);
1062 	sdirty->right = max_t(s32, sdirty->right, dirty->unit_x2);
1063 	sdirty->bottom = max_t(s32, sdirty->bottom, dirty->unit_y2);
1064 
1065 	dirty->num_hits++;
1066 }
1067 
1068 /**
1069  * vmw_kms_sou_do_surface_dirty - Dirty part of a surface backed framebuffer
1070  *
1071  * @dev_priv: Pointer to the device private structure.
1072  * @framebuffer: Pointer to the surface-buffer backed framebuffer.
1073  * @clips: Array of clip rects. Either @clips or @vclips must be NULL.
1074  * @vclips: Alternate array of clip rects. Either @clips or @vclips must
1075  * be NULL.
1076  * @srf: Pointer to surface to blit from. If NULL, the surface attached
1077  * to @framebuffer will be used.
1078  * @dest_x: X coordinate offset to align @srf with framebuffer coordinates.
1079  * @dest_y: Y coordinate offset to align @srf with framebuffer coordinates.
1080  * @num_clips: Number of clip rects in @clips.
1081  * @inc: Increment to use when looping over @clips.
1082  * @out_fence: If non-NULL, will return a ref-counted pointer to a
1083  * struct vmw_fence_obj. The returned fence pointer may be NULL in which
1084  * case the device has already synchronized.
1085  * @crtc: If crtc is passed, perform surface dirty on that crtc only.
1086  *
1087  * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
1088  * interrupted.
1089  */
1090 int vmw_kms_sou_do_surface_dirty(struct vmw_private *dev_priv,
1091 				 struct vmw_framebuffer *framebuffer,
1092 				 struct drm_clip_rect *clips,
1093 				 struct drm_vmw_rect *vclips,
1094 				 struct vmw_resource *srf,
1095 				 s32 dest_x,
1096 				 s32 dest_y,
1097 				 unsigned num_clips, int inc,
1098 				 struct vmw_fence_obj **out_fence,
1099 				 struct drm_crtc *crtc)
1100 {
1101 	struct vmw_framebuffer_surface *vfbs =
1102 		container_of(framebuffer, typeof(*vfbs), base);
1103 	struct vmw_kms_sou_surface_dirty sdirty;
1104 	DECLARE_VAL_CONTEXT(val_ctx, NULL, 0);
1105 	int ret;
1106 
1107 	if (!srf)
1108 		srf = &vfbs->surface->res;
1109 
1110 	ret = vmw_validation_add_resource(&val_ctx, srf, 0, VMW_RES_DIRTY_NONE,
1111 					  NULL, NULL);
1112 	if (ret)
1113 		return ret;
1114 
1115 	ret = vmw_validation_prepare(&val_ctx, &dev_priv->cmdbuf_mutex, true);
1116 	if (ret)
1117 		goto out_unref;
1118 
1119 	sdirty.base.fifo_commit = vmw_sou_surface_fifo_commit;
1120 	sdirty.base.clip = vmw_sou_surface_clip;
1121 	sdirty.base.dev_priv = dev_priv;
1122 	sdirty.base.fifo_reserve_size = sizeof(struct vmw_kms_sou_dirty_cmd) +
1123 	  sizeof(SVGASignedRect) * num_clips;
1124 	sdirty.base.crtc = crtc;
1125 
1126 	sdirty.sid = srf->id;
1127 	sdirty.left = sdirty.top = S32_MAX;
1128 	sdirty.right = sdirty.bottom = S32_MIN;
1129 	sdirty.dst_x = dest_x;
1130 	sdirty.dst_y = dest_y;
1131 
1132 	ret = vmw_kms_helper_dirty(dev_priv, framebuffer, clips, vclips,
1133 				   dest_x, dest_y, num_clips, inc,
1134 				   &sdirty.base);
1135 	vmw_kms_helper_validation_finish(dev_priv, NULL, &val_ctx, out_fence,
1136 					 NULL);
1137 
1138 	return ret;
1139 
1140 out_unref:
1141 	vmw_validation_unref_lists(&val_ctx);
1142 	return ret;
1143 }
1144 
1145 /**
1146  * vmw_sou_bo_fifo_commit - Callback to submit a set of readback clips.
1147  *
1148  * @dirty: The closure structure.
1149  *
1150  * Commits a previously built command buffer of readback clips.
1151  */
1152 static void vmw_sou_bo_fifo_commit(struct vmw_kms_dirty *dirty)
1153 {
1154 	if (!dirty->num_hits) {
1155 		vmw_cmd_commit(dirty->dev_priv, 0);
1156 		return;
1157 	}
1158 
1159 	vmw_cmd_commit(dirty->dev_priv,
1160 			sizeof(struct vmw_kms_sou_bo_blit) *
1161 			dirty->num_hits);
1162 }
1163 
1164 /**
1165  * vmw_sou_bo_clip - Callback to encode a readback cliprect.
1166  *
1167  * @dirty: The closure structure
1168  *
1169  * Encodes a BLIT_GMRFB_TO_SCREEN cliprect.
1170  */
1171 static void vmw_sou_bo_clip(struct vmw_kms_dirty *dirty)
1172 {
1173 	struct vmw_kms_sou_bo_blit *blit = dirty->cmd;
1174 
1175 	blit += dirty->num_hits;
1176 	blit->header = SVGA_CMD_BLIT_GMRFB_TO_SCREEN;
1177 	blit->body.destScreenId = dirty->unit->unit;
1178 	blit->body.srcOrigin.x = dirty->fb_x;
1179 	blit->body.srcOrigin.y = dirty->fb_y;
1180 	blit->body.destRect.left = dirty->unit_x1;
1181 	blit->body.destRect.top = dirty->unit_y1;
1182 	blit->body.destRect.right = dirty->unit_x2;
1183 	blit->body.destRect.bottom = dirty->unit_y2;
1184 	dirty->num_hits++;
1185 }
1186 
1187 /**
1188  * vmw_kms_sou_do_bo_dirty - Dirty part of a buffer-object backed framebuffer
1189  *
1190  * @dev_priv: Pointer to the device private structure.
1191  * @framebuffer: Pointer to the buffer-object backed framebuffer.
1192  * @clips: Array of clip rects.
1193  * @vclips: Alternate array of clip rects. Either @clips or @vclips must
1194  * be NULL.
1195  * @num_clips: Number of clip rects in @clips.
1196  * @increment: Increment to use when looping over @clips.
1197  * @interruptible: Whether to perform waits interruptible if possible.
1198  * @out_fence: If non-NULL, will return a ref-counted pointer to a
1199  * struct vmw_fence_obj. The returned fence pointer may be NULL in which
1200  * case the device has already synchronized.
1201  * @crtc: If crtc is passed, perform bo dirty on that crtc only.
1202  *
1203  * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
1204  * interrupted.
1205  */
1206 int vmw_kms_sou_do_bo_dirty(struct vmw_private *dev_priv,
1207 				struct vmw_framebuffer *framebuffer,
1208 				struct drm_clip_rect *clips,
1209 				struct drm_vmw_rect *vclips,
1210 				unsigned num_clips, int increment,
1211 				bool interruptible,
1212 				struct vmw_fence_obj **out_fence,
1213 				struct drm_crtc *crtc)
1214 {
1215 	struct vmw_buffer_object *buf =
1216 		container_of(framebuffer, struct vmw_framebuffer_bo,
1217 			     base)->buffer;
1218 	struct vmw_kms_dirty dirty;
1219 	DECLARE_VAL_CONTEXT(val_ctx, NULL, 0);
1220 	int ret;
1221 
1222 	ret = vmw_validation_add_bo(&val_ctx, buf, false, false);
1223 	if (ret)
1224 		return ret;
1225 
1226 	ret = vmw_validation_prepare(&val_ctx, NULL, interruptible);
1227 	if (ret)
1228 		goto out_unref;
1229 
1230 	ret = do_bo_define_gmrfb(dev_priv, framebuffer);
1231 	if (unlikely(ret != 0))
1232 		goto out_revert;
1233 
1234 	dirty.crtc = crtc;
1235 	dirty.fifo_commit = vmw_sou_bo_fifo_commit;
1236 	dirty.clip = vmw_sou_bo_clip;
1237 	dirty.fifo_reserve_size = sizeof(struct vmw_kms_sou_bo_blit) *
1238 		num_clips;
1239 	ret = vmw_kms_helper_dirty(dev_priv, framebuffer, clips, vclips,
1240 				   0, 0, num_clips, increment, &dirty);
1241 	vmw_kms_helper_validation_finish(dev_priv, NULL, &val_ctx, out_fence,
1242 					 NULL);
1243 
1244 	return ret;
1245 
1246 out_revert:
1247 	vmw_validation_revert(&val_ctx);
1248 out_unref:
1249 	vmw_validation_unref_lists(&val_ctx);
1250 
1251 	return ret;
1252 }
1253 
1254 
1255 /**
1256  * vmw_sou_readback_fifo_commit - Callback to submit a set of readback clips.
1257  *
1258  * @dirty: The closure structure.
1259  *
1260  * Commits a previously built command buffer of readback clips.
1261  */
1262 static void vmw_sou_readback_fifo_commit(struct vmw_kms_dirty *dirty)
1263 {
1264 	if (!dirty->num_hits) {
1265 		vmw_cmd_commit(dirty->dev_priv, 0);
1266 		return;
1267 	}
1268 
1269 	vmw_cmd_commit(dirty->dev_priv,
1270 			sizeof(struct vmw_kms_sou_readback_blit) *
1271 			dirty->num_hits);
1272 }
1273 
1274 /**
1275  * vmw_sou_readback_clip - Callback to encode a readback cliprect.
1276  *
1277  * @dirty: The closure structure
1278  *
1279  * Encodes a BLIT_SCREEN_TO_GMRFB cliprect.
1280  */
1281 static void vmw_sou_readback_clip(struct vmw_kms_dirty *dirty)
1282 {
1283 	struct vmw_kms_sou_readback_blit *blit = dirty->cmd;
1284 
1285 	blit += dirty->num_hits;
1286 	blit->header = SVGA_CMD_BLIT_SCREEN_TO_GMRFB;
1287 	blit->body.srcScreenId = dirty->unit->unit;
1288 	blit->body.destOrigin.x = dirty->fb_x;
1289 	blit->body.destOrigin.y = dirty->fb_y;
1290 	blit->body.srcRect.left = dirty->unit_x1;
1291 	blit->body.srcRect.top = dirty->unit_y1;
1292 	blit->body.srcRect.right = dirty->unit_x2;
1293 	blit->body.srcRect.bottom = dirty->unit_y2;
1294 	dirty->num_hits++;
1295 }
1296 
1297 /**
1298  * vmw_kms_sou_readback - Perform a readback from the screen object system to
1299  * a buffer-object backed framebuffer.
1300  *
1301  * @dev_priv: Pointer to the device private structure.
1302  * @file_priv: Pointer to a struct drm_file identifying the caller.
1303  * Must be set to NULL if @user_fence_rep is NULL.
1304  * @vfb: Pointer to the buffer-object backed framebuffer.
1305  * @user_fence_rep: User-space provided structure for fence information.
1306  * Must be set to non-NULL if @file_priv is non-NULL.
1307  * @vclips: Array of clip rects.
1308  * @num_clips: Number of clip rects in @vclips.
1309  * @crtc: If crtc is passed, readback on that crtc only.
1310  *
1311  * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
1312  * interrupted.
1313  */
1314 int vmw_kms_sou_readback(struct vmw_private *dev_priv,
1315 			 struct drm_file *file_priv,
1316 			 struct vmw_framebuffer *vfb,
1317 			 struct drm_vmw_fence_rep __user *user_fence_rep,
1318 			 struct drm_vmw_rect *vclips,
1319 			 uint32_t num_clips,
1320 			 struct drm_crtc *crtc)
1321 {
1322 	struct vmw_buffer_object *buf =
1323 		container_of(vfb, struct vmw_framebuffer_bo, base)->buffer;
1324 	struct vmw_kms_dirty dirty;
1325 	DECLARE_VAL_CONTEXT(val_ctx, NULL, 0);
1326 	int ret;
1327 
1328 	ret = vmw_validation_add_bo(&val_ctx, buf, false, false);
1329 	if (ret)
1330 		return ret;
1331 
1332 	ret = vmw_validation_prepare(&val_ctx, NULL, true);
1333 	if (ret)
1334 		goto out_unref;
1335 
1336 	ret = do_bo_define_gmrfb(dev_priv, vfb);
1337 	if (unlikely(ret != 0))
1338 		goto out_revert;
1339 
1340 	dirty.crtc = crtc;
1341 	dirty.fifo_commit = vmw_sou_readback_fifo_commit;
1342 	dirty.clip = vmw_sou_readback_clip;
1343 	dirty.fifo_reserve_size = sizeof(struct vmw_kms_sou_readback_blit) *
1344 		num_clips;
1345 	ret = vmw_kms_helper_dirty(dev_priv, vfb, NULL, vclips,
1346 				   0, 0, num_clips, 1, &dirty);
1347 	vmw_kms_helper_validation_finish(dev_priv, file_priv, &val_ctx, NULL,
1348 					 user_fence_rep);
1349 
1350 	return ret;
1351 
1352 out_revert:
1353 	vmw_validation_revert(&val_ctx);
1354 out_unref:
1355 	vmw_validation_unref_lists(&val_ctx);
1356 
1357 	return ret;
1358 }
1359