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