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