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 
vmw_sou_destroy(struct vmw_screen_object_unit * sou)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 
vmw_sou_crtc_destroy(struct drm_crtc * crtc)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  */
vmw_sou_fifo_create(struct vmw_private * dev_priv,struct vmw_screen_object_unit * sou,int x,int y,struct drm_display_mode * mode)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  */
vmw_sou_fifo_destroy(struct vmw_private * dev_priv,struct vmw_screen_object_unit * sou)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  */
vmw_sou_crtc_mode_set_nofb(struct drm_crtc * crtc)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  */
vmw_sou_crtc_helper_prepare(struct drm_crtc * crtc)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  */
vmw_sou_crtc_atomic_enable(struct drm_crtc * crtc,struct drm_atomic_state * state)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  */
vmw_sou_crtc_atomic_disable(struct drm_crtc * crtc,struct drm_atomic_state * state)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 
vmw_sou_encoder_destroy(struct drm_encoder * encoder)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 
vmw_sou_connector_destroy(struct drm_connector * connector)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
vmw_sou_primary_plane_cleanup_fb(struct drm_plane * plane,struct drm_plane_state * old_state)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
vmw_sou_primary_plane_prepare_fb(struct drm_plane * plane,struct drm_plane_state * new_state)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 	vps->bo = kzalloc(sizeof(*vps->bo), GFP_KERNEL);
446 	if (!vps->bo)
447 		return -ENOMEM;
448 
449 	vmw_svga_enable(dev_priv);
450 
451 	/* After we have alloced the backing store might not be able to
452 	 * resume the overlays, this is preferred to failing to alloc.
453 	 */
454 	vmw_overlay_pause_all(dev_priv);
455 	ret = vmw_bo_init(dev_priv, vps->bo, size,
456 			      &vmw_vram_placement,
457 			      false, true, &vmw_bo_bo_free);
458 	vmw_overlay_resume_all(dev_priv);
459 	if (ret) {
460 		vps->bo = NULL; /* vmw_bo_init frees on error */
461 		return ret;
462 	}
463 
464 	vps->bo_size = size;
465 
466 	/*
467 	 * TTM already thinks the buffer is pinned, but make sure the
468 	 * pin_count is upped.
469 	 */
470 	return vmw_bo_pin_in_vram(dev_priv, vps->bo, true);
471 }
472 
vmw_sou_bo_fifo_size(struct vmw_du_update_plane * update,uint32_t num_hits)473 static uint32_t vmw_sou_bo_fifo_size(struct vmw_du_update_plane *update,
474 				     uint32_t num_hits)
475 {
476 	return sizeof(struct vmw_kms_sou_define_gmrfb) +
477 		sizeof(struct vmw_kms_sou_bo_blit) * num_hits;
478 }
479 
vmw_sou_bo_define_gmrfb(struct vmw_du_update_plane * update,void * cmd)480 static uint32_t vmw_sou_bo_define_gmrfb(struct vmw_du_update_plane *update,
481 					void *cmd)
482 {
483 	struct vmw_framebuffer_bo *vfbbo =
484 		container_of(update->vfb, typeof(*vfbbo), base);
485 	struct vmw_kms_sou_define_gmrfb *gmr = cmd;
486 	int depth = update->vfb->base.format->depth;
487 
488 	/* Emulate RGBA support, contrary to svga_reg.h this is not
489 	 * supported by hosts. This is only a problem if we are reading
490 	 * this value later and expecting what we uploaded back.
491 	 */
492 	if (depth == 32)
493 		depth = 24;
494 
495 	gmr->header = SVGA_CMD_DEFINE_GMRFB;
496 
497 	gmr->body.format.bitsPerPixel = update->vfb->base.format->cpp[0] * 8;
498 	gmr->body.format.colorDepth = depth;
499 	gmr->body.format.reserved = 0;
500 	gmr->body.bytesPerLine = update->vfb->base.pitches[0];
501 	vmw_bo_get_guest_ptr(&vfbbo->buffer->base, &gmr->body.ptr);
502 
503 	return sizeof(*gmr);
504 }
505 
vmw_sou_bo_populate_clip(struct vmw_du_update_plane * update,void * cmd,struct drm_rect * clip,uint32_t fb_x,uint32_t fb_y)506 static uint32_t vmw_sou_bo_populate_clip(struct vmw_du_update_plane  *update,
507 					 void *cmd, struct drm_rect *clip,
508 					 uint32_t fb_x, uint32_t fb_y)
509 {
510 	struct vmw_kms_sou_bo_blit *blit = cmd;
511 
512 	blit->header = SVGA_CMD_BLIT_GMRFB_TO_SCREEN;
513 	blit->body.destScreenId = update->du->unit;
514 	blit->body.srcOrigin.x = fb_x;
515 	blit->body.srcOrigin.y = fb_y;
516 	blit->body.destRect.left = clip->x1;
517 	blit->body.destRect.top = clip->y1;
518 	blit->body.destRect.right = clip->x2;
519 	blit->body.destRect.bottom = clip->y2;
520 
521 	return sizeof(*blit);
522 }
523 
vmw_stud_bo_post_clip(struct vmw_du_update_plane * update,void * cmd,struct drm_rect * bb)524 static uint32_t vmw_stud_bo_post_clip(struct vmw_du_update_plane  *update,
525 				      void *cmd, struct drm_rect *bb)
526 {
527 	return 0;
528 }
529 
530 /**
531  * vmw_sou_plane_update_bo - Update display unit for bo backed fb.
532  * @dev_priv: Device private.
533  * @plane: Plane state.
534  * @old_state: Old plane state.
535  * @vfb: Framebuffer which is blitted to display unit.
536  * @out_fence: If non-NULL, will return a ref-counted pointer to vmw_fence_obj.
537  *             The returned fence pointer may be NULL in which case the device
538  *             has already synchronized.
539  *
540  * Return: 0 on success or a negative error code on failure.
541  */
vmw_sou_plane_update_bo(struct vmw_private * dev_priv,struct drm_plane * plane,struct drm_plane_state * old_state,struct vmw_framebuffer * vfb,struct vmw_fence_obj ** out_fence)542 static int vmw_sou_plane_update_bo(struct vmw_private *dev_priv,
543 				   struct drm_plane *plane,
544 				   struct drm_plane_state *old_state,
545 				   struct vmw_framebuffer *vfb,
546 				   struct vmw_fence_obj **out_fence)
547 {
548 	struct vmw_du_update_plane_buffer bo_update;
549 
550 	memset(&bo_update, 0, sizeof(struct vmw_du_update_plane_buffer));
551 	bo_update.base.plane = plane;
552 	bo_update.base.old_state = old_state;
553 	bo_update.base.dev_priv = dev_priv;
554 	bo_update.base.du = vmw_crtc_to_du(plane->state->crtc);
555 	bo_update.base.vfb = vfb;
556 	bo_update.base.out_fence = out_fence;
557 	bo_update.base.mutex = NULL;
558 	bo_update.base.cpu_blit = false;
559 	bo_update.base.intr = true;
560 
561 	bo_update.base.calc_fifo_size = vmw_sou_bo_fifo_size;
562 	bo_update.base.post_prepare = vmw_sou_bo_define_gmrfb;
563 	bo_update.base.clip = vmw_sou_bo_populate_clip;
564 	bo_update.base.post_clip = vmw_stud_bo_post_clip;
565 
566 	return vmw_du_helper_plane_update(&bo_update.base);
567 }
568 
vmw_sou_surface_fifo_size(struct vmw_du_update_plane * update,uint32_t num_hits)569 static uint32_t vmw_sou_surface_fifo_size(struct vmw_du_update_plane *update,
570 					  uint32_t num_hits)
571 {
572 	return sizeof(struct vmw_kms_sou_dirty_cmd) + sizeof(SVGASignedRect) *
573 		num_hits;
574 }
575 
vmw_sou_surface_post_prepare(struct vmw_du_update_plane * update,void * cmd)576 static uint32_t vmw_sou_surface_post_prepare(struct vmw_du_update_plane *update,
577 					     void *cmd)
578 {
579 	struct vmw_du_update_plane_surface *srf_update;
580 
581 	srf_update = container_of(update, typeof(*srf_update), base);
582 
583 	/*
584 	 * SOU SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN is special in the sense that
585 	 * its bounding box is filled before iterating over all the clips. So
586 	 * store the FIFO start address and revisit to fill the details.
587 	 */
588 	srf_update->cmd_start = cmd;
589 
590 	return 0;
591 }
592 
vmw_sou_surface_pre_clip(struct vmw_du_update_plane * update,void * cmd,uint32_t num_hits)593 static uint32_t vmw_sou_surface_pre_clip(struct vmw_du_update_plane *update,
594 					 void *cmd, uint32_t num_hits)
595 {
596 	struct vmw_kms_sou_dirty_cmd *blit = cmd;
597 	struct vmw_framebuffer_surface *vfbs;
598 
599 	vfbs = container_of(update->vfb, typeof(*vfbs), base);
600 
601 	blit->header.id = SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN;
602 	blit->header.size = sizeof(blit->body) + sizeof(SVGASignedRect) *
603 		num_hits;
604 
605 	blit->body.srcImage.sid = vfbs->surface->res.id;
606 	blit->body.destScreenId = update->du->unit;
607 
608 	/* Update the source and destination bounding box later in post_clip */
609 	blit->body.srcRect.left = 0;
610 	blit->body.srcRect.top = 0;
611 	blit->body.srcRect.right = 0;
612 	blit->body.srcRect.bottom = 0;
613 
614 	blit->body.destRect.left = 0;
615 	blit->body.destRect.top = 0;
616 	blit->body.destRect.right = 0;
617 	blit->body.destRect.bottom = 0;
618 
619 	return sizeof(*blit);
620 }
621 
vmw_sou_surface_clip_rect(struct vmw_du_update_plane * update,void * cmd,struct drm_rect * clip,uint32_t src_x,uint32_t src_y)622 static uint32_t vmw_sou_surface_clip_rect(struct vmw_du_update_plane *update,
623 					  void *cmd, struct drm_rect *clip,
624 					  uint32_t src_x, uint32_t src_y)
625 {
626 	SVGASignedRect *rect = cmd;
627 
628 	/*
629 	 * rects are relative to dest bounding box rect on screen object, so
630 	 * translate to it later in post_clip
631 	 */
632 	rect->left = clip->x1;
633 	rect->top = clip->y1;
634 	rect->right = clip->x2;
635 	rect->bottom = clip->y2;
636 
637 	return sizeof(*rect);
638 }
639 
vmw_sou_surface_post_clip(struct vmw_du_update_plane * update,void * cmd,struct drm_rect * bb)640 static uint32_t vmw_sou_surface_post_clip(struct vmw_du_update_plane *update,
641 					  void *cmd, struct drm_rect *bb)
642 {
643 	struct vmw_du_update_plane_surface *srf_update;
644 	struct drm_plane_state *state = update->plane->state;
645 	struct drm_rect src_bb;
646 	struct vmw_kms_sou_dirty_cmd *blit;
647 	SVGASignedRect *rect;
648 	uint32_t num_hits;
649 	int translate_src_x;
650 	int translate_src_y;
651 	int i;
652 
653 	srf_update = container_of(update, typeof(*srf_update), base);
654 
655 	blit = srf_update->cmd_start;
656 	rect = (SVGASignedRect *)&blit[1];
657 
658 	num_hits = (blit->header.size - sizeof(blit->body))/
659 		sizeof(SVGASignedRect);
660 
661 	src_bb = *bb;
662 
663 	/* To translate bb back to fb src coord */
664 	translate_src_x = (state->src_x >> 16) - state->crtc_x;
665 	translate_src_y = (state->src_y >> 16) - state->crtc_y;
666 
667 	drm_rect_translate(&src_bb, translate_src_x, translate_src_y);
668 
669 	blit->body.srcRect.left = src_bb.x1;
670 	blit->body.srcRect.top = src_bb.y1;
671 	blit->body.srcRect.right = src_bb.x2;
672 	blit->body.srcRect.bottom = src_bb.y2;
673 
674 	blit->body.destRect.left = bb->x1;
675 	blit->body.destRect.top = bb->y1;
676 	blit->body.destRect.right = bb->x2;
677 	blit->body.destRect.bottom = bb->y2;
678 
679 	/* rects are relative to dest bb rect */
680 	for (i = 0; i < num_hits; i++) {
681 		rect->left -= bb->x1;
682 		rect->top -= bb->y1;
683 		rect->right -= bb->x1;
684 		rect->bottom -= bb->y1;
685 		rect++;
686 	}
687 
688 	return 0;
689 }
690 
691 /**
692  * vmw_sou_plane_update_surface - Update display unit for surface backed fb.
693  * @dev_priv: Device private.
694  * @plane: Plane state.
695  * @old_state: Old plane state.
696  * @vfb: Framebuffer which is blitted to display unit
697  * @out_fence: If non-NULL, will return a ref-counted pointer to vmw_fence_obj.
698  *             The returned fence pointer may be NULL in which case the device
699  *             has already synchronized.
700  *
701  * Return: 0 on success or a negative error code on failure.
702  */
vmw_sou_plane_update_surface(struct vmw_private * dev_priv,struct drm_plane * plane,struct drm_plane_state * old_state,struct vmw_framebuffer * vfb,struct vmw_fence_obj ** out_fence)703 static int vmw_sou_plane_update_surface(struct vmw_private *dev_priv,
704 					struct drm_plane *plane,
705 					struct drm_plane_state *old_state,
706 					struct vmw_framebuffer *vfb,
707 					struct vmw_fence_obj **out_fence)
708 {
709 	struct vmw_du_update_plane_surface srf_update;
710 
711 	memset(&srf_update, 0, sizeof(struct vmw_du_update_plane_surface));
712 	srf_update.base.plane = plane;
713 	srf_update.base.old_state = old_state;
714 	srf_update.base.dev_priv = dev_priv;
715 	srf_update.base.du = vmw_crtc_to_du(plane->state->crtc);
716 	srf_update.base.vfb = vfb;
717 	srf_update.base.out_fence = out_fence;
718 	srf_update.base.mutex = &dev_priv->cmdbuf_mutex;
719 	srf_update.base.cpu_blit = false;
720 	srf_update.base.intr = true;
721 
722 	srf_update.base.calc_fifo_size = vmw_sou_surface_fifo_size;
723 	srf_update.base.post_prepare = vmw_sou_surface_post_prepare;
724 	srf_update.base.pre_clip = vmw_sou_surface_pre_clip;
725 	srf_update.base.clip = vmw_sou_surface_clip_rect;
726 	srf_update.base.post_clip = vmw_sou_surface_post_clip;
727 
728 	return vmw_du_helper_plane_update(&srf_update.base);
729 }
730 
731 static void
vmw_sou_primary_plane_atomic_update(struct drm_plane * plane,struct drm_atomic_state * state)732 vmw_sou_primary_plane_atomic_update(struct drm_plane *plane,
733 				    struct drm_atomic_state *state)
734 {
735 	struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state, plane);
736 	struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state, plane);
737 	struct drm_crtc *crtc = new_state->crtc;
738 	struct drm_pending_vblank_event *event = NULL;
739 	struct vmw_fence_obj *fence = NULL;
740 	int ret;
741 
742 	/* In case of device error, maintain consistent atomic state */
743 	if (crtc && new_state->fb) {
744 		struct vmw_private *dev_priv = vmw_priv(crtc->dev);
745 		struct vmw_framebuffer *vfb =
746 			vmw_framebuffer_to_vfb(new_state->fb);
747 
748 		if (vfb->bo)
749 			ret = vmw_sou_plane_update_bo(dev_priv, plane,
750 						      old_state, vfb, &fence);
751 		else
752 			ret = vmw_sou_plane_update_surface(dev_priv, plane,
753 							   old_state, vfb,
754 							   &fence);
755 		if (ret != 0)
756 			DRM_ERROR("Failed to update screen.\n");
757 	} else {
758 		/* Do nothing when fb and crtc is NULL (blank crtc) */
759 		return;
760 	}
761 
762 	/* For error case vblank event is send from vmw_du_crtc_atomic_flush */
763 	event = crtc->state->event;
764 	if (event && fence) {
765 		struct drm_file *file_priv = event->base.file_priv;
766 
767 		ret = vmw_event_fence_action_queue(file_priv,
768 						   fence,
769 						   &event->base,
770 						   &event->event.vbl.tv_sec,
771 						   &event->event.vbl.tv_usec,
772 						   true);
773 
774 		if (unlikely(ret != 0))
775 			DRM_ERROR("Failed to queue event on fence.\n");
776 		else
777 			crtc->state->event = NULL;
778 	}
779 
780 	if (fence)
781 		vmw_fence_obj_unreference(&fence);
782 }
783 
784 
785 static const struct drm_plane_funcs vmw_sou_plane_funcs = {
786 	.update_plane = drm_atomic_helper_update_plane,
787 	.disable_plane = drm_atomic_helper_disable_plane,
788 	.destroy = vmw_du_primary_plane_destroy,
789 	.reset = vmw_du_plane_reset,
790 	.atomic_duplicate_state = vmw_du_plane_duplicate_state,
791 	.atomic_destroy_state = vmw_du_plane_destroy_state,
792 };
793 
794 static const struct drm_plane_funcs vmw_sou_cursor_funcs = {
795 	.update_plane = drm_atomic_helper_update_plane,
796 	.disable_plane = drm_atomic_helper_disable_plane,
797 	.destroy = vmw_du_cursor_plane_destroy,
798 	.reset = vmw_du_plane_reset,
799 	.atomic_duplicate_state = vmw_du_plane_duplicate_state,
800 	.atomic_destroy_state = vmw_du_plane_destroy_state,
801 };
802 
803 /*
804  * Atomic Helpers
805  */
806 static const struct
807 drm_plane_helper_funcs vmw_sou_cursor_plane_helper_funcs = {
808 	.atomic_check = vmw_du_cursor_plane_atomic_check,
809 	.atomic_update = vmw_du_cursor_plane_atomic_update,
810 	.prepare_fb = vmw_du_cursor_plane_prepare_fb,
811 	.cleanup_fb = vmw_du_plane_cleanup_fb,
812 };
813 
814 static const struct
815 drm_plane_helper_funcs vmw_sou_primary_plane_helper_funcs = {
816 	.atomic_check = vmw_du_primary_plane_atomic_check,
817 	.atomic_update = vmw_sou_primary_plane_atomic_update,
818 	.prepare_fb = vmw_sou_primary_plane_prepare_fb,
819 	.cleanup_fb = vmw_sou_primary_plane_cleanup_fb,
820 };
821 
822 static const struct drm_crtc_helper_funcs vmw_sou_crtc_helper_funcs = {
823 	.prepare = vmw_sou_crtc_helper_prepare,
824 	.mode_set_nofb = vmw_sou_crtc_mode_set_nofb,
825 	.atomic_check = vmw_du_crtc_atomic_check,
826 	.atomic_begin = vmw_du_crtc_atomic_begin,
827 	.atomic_flush = vmw_du_crtc_atomic_flush,
828 	.atomic_enable = vmw_sou_crtc_atomic_enable,
829 	.atomic_disable = vmw_sou_crtc_atomic_disable,
830 };
831 
832 
vmw_sou_init(struct vmw_private * dev_priv,unsigned unit)833 static int vmw_sou_init(struct vmw_private *dev_priv, unsigned unit)
834 {
835 	struct vmw_screen_object_unit *sou;
836 	struct drm_device *dev = &dev_priv->drm;
837 	struct drm_connector *connector;
838 	struct drm_encoder *encoder;
839 	struct drm_plane *primary, *cursor;
840 	struct drm_crtc *crtc;
841 	int ret;
842 
843 	sou = kzalloc(sizeof(*sou), GFP_KERNEL);
844 	if (!sou)
845 		return -ENOMEM;
846 
847 	sou->base.unit = unit;
848 	crtc = &sou->base.crtc;
849 	encoder = &sou->base.encoder;
850 	connector = &sou->base.connector;
851 	primary = &sou->base.primary;
852 	cursor = &sou->base.cursor;
853 
854 	sou->base.pref_active = (unit == 0);
855 	sou->base.pref_width = dev_priv->initial_width;
856 	sou->base.pref_height = dev_priv->initial_height;
857 	sou->base.pref_mode = NULL;
858 
859 	/*
860 	 * Remove this after enabling atomic because property values can
861 	 * only exist in a state object
862 	 */
863 	sou->base.is_implicit = false;
864 
865 	/* Initialize primary plane */
866 	ret = drm_universal_plane_init(dev, &sou->base.primary,
867 				       0, &vmw_sou_plane_funcs,
868 				       vmw_primary_plane_formats,
869 				       ARRAY_SIZE(vmw_primary_plane_formats),
870 				       NULL, DRM_PLANE_TYPE_PRIMARY, NULL);
871 	if (ret) {
872 		DRM_ERROR("Failed to initialize primary plane");
873 		goto err_free;
874 	}
875 
876 	drm_plane_helper_add(primary, &vmw_sou_primary_plane_helper_funcs);
877 	drm_plane_enable_fb_damage_clips(primary);
878 
879 	/* Initialize cursor plane */
880 	ret = drm_universal_plane_init(dev, &sou->base.cursor,
881 			0, &vmw_sou_cursor_funcs,
882 			vmw_cursor_plane_formats,
883 			ARRAY_SIZE(vmw_cursor_plane_formats),
884 			NULL, DRM_PLANE_TYPE_CURSOR, NULL);
885 	if (ret) {
886 		DRM_ERROR("Failed to initialize cursor plane");
887 		drm_plane_cleanup(&sou->base.primary);
888 		goto err_free;
889 	}
890 
891 	drm_plane_helper_add(cursor, &vmw_sou_cursor_plane_helper_funcs);
892 
893 	ret = drm_connector_init(dev, connector, &vmw_sou_connector_funcs,
894 				 DRM_MODE_CONNECTOR_VIRTUAL);
895 	if (ret) {
896 		DRM_ERROR("Failed to initialize connector\n");
897 		goto err_free;
898 	}
899 
900 	drm_connector_helper_add(connector, &vmw_sou_connector_helper_funcs);
901 	connector->status = vmw_du_connector_detect(connector, true);
902 
903 	ret = drm_encoder_init(dev, encoder, &vmw_screen_object_encoder_funcs,
904 			       DRM_MODE_ENCODER_VIRTUAL, NULL);
905 	if (ret) {
906 		DRM_ERROR("Failed to initialize encoder\n");
907 		goto err_free_connector;
908 	}
909 
910 	(void) drm_connector_attach_encoder(connector, encoder);
911 	encoder->possible_crtcs = (1 << unit);
912 	encoder->possible_clones = 0;
913 
914 	ret = drm_connector_register(connector);
915 	if (ret) {
916 		DRM_ERROR("Failed to register connector\n");
917 		goto err_free_encoder;
918 	}
919 
920 	ret = drm_crtc_init_with_planes(dev, crtc, &sou->base.primary,
921 					&sou->base.cursor,
922 					&vmw_screen_object_crtc_funcs, NULL);
923 	if (ret) {
924 		DRM_ERROR("Failed to initialize CRTC\n");
925 		goto err_free_unregister;
926 	}
927 
928 	drm_crtc_helper_add(crtc, &vmw_sou_crtc_helper_funcs);
929 
930 	drm_mode_crtc_set_gamma_size(crtc, 256);
931 
932 	drm_object_attach_property(&connector->base,
933 				   dev_priv->hotplug_mode_update_property, 1);
934 	drm_object_attach_property(&connector->base,
935 				   dev->mode_config.suggested_x_property, 0);
936 	drm_object_attach_property(&connector->base,
937 				   dev->mode_config.suggested_y_property, 0);
938 	return 0;
939 
940 err_free_unregister:
941 	drm_connector_unregister(connector);
942 err_free_encoder:
943 	drm_encoder_cleanup(encoder);
944 err_free_connector:
945 	drm_connector_cleanup(connector);
946 err_free:
947 	kfree(sou);
948 	return ret;
949 }
950 
vmw_kms_sou_init_display(struct vmw_private * dev_priv)951 int vmw_kms_sou_init_display(struct vmw_private *dev_priv)
952 {
953 	struct drm_device *dev = &dev_priv->drm;
954 	int i, ret;
955 
956 	if (!(dev_priv->capabilities & SVGA_CAP_SCREEN_OBJECT_2)) {
957 		return -ENOSYS;
958 	}
959 
960 	ret = -ENOMEM;
961 
962 	ret = drm_vblank_init(dev, VMWGFX_NUM_DISPLAY_UNITS);
963 	if (unlikely(ret != 0))
964 		return ret;
965 
966 	for (i = 0; i < VMWGFX_NUM_DISPLAY_UNITS; ++i)
967 		vmw_sou_init(dev_priv, i);
968 
969 	dev_priv->active_display_unit = vmw_du_screen_object;
970 
971 	drm_mode_config_reset(dev);
972 
973 	return 0;
974 }
975 
do_bo_define_gmrfb(struct vmw_private * dev_priv,struct vmw_framebuffer * framebuffer)976 static int do_bo_define_gmrfb(struct vmw_private *dev_priv,
977 				  struct vmw_framebuffer *framebuffer)
978 {
979 	struct vmw_buffer_object *buf =
980 		container_of(framebuffer, struct vmw_framebuffer_bo,
981 			     base)->buffer;
982 	int depth = framebuffer->base.format->depth;
983 	struct {
984 		uint32_t header;
985 		SVGAFifoCmdDefineGMRFB body;
986 	} *cmd;
987 
988 	/* Emulate RGBA support, contrary to svga_reg.h this is not
989 	 * supported by hosts. This is only a problem if we are reading
990 	 * this value later and expecting what we uploaded back.
991 	 */
992 	if (depth == 32)
993 		depth = 24;
994 
995 	cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd));
996 	if (!cmd)
997 		return -ENOMEM;
998 
999 	cmd->header = SVGA_CMD_DEFINE_GMRFB;
1000 	cmd->body.format.bitsPerPixel = framebuffer->base.format->cpp[0] * 8;
1001 	cmd->body.format.colorDepth = depth;
1002 	cmd->body.format.reserved = 0;
1003 	cmd->body.bytesPerLine = framebuffer->base.pitches[0];
1004 	/* Buffer is reserved in vram or GMR */
1005 	vmw_bo_get_guest_ptr(&buf->base, &cmd->body.ptr);
1006 	vmw_cmd_commit(dev_priv, sizeof(*cmd));
1007 
1008 	return 0;
1009 }
1010 
1011 /**
1012  * vmw_sou_surface_fifo_commit - Callback to fill in and submit a
1013  * blit surface to screen command.
1014  *
1015  * @dirty: The closure structure.
1016  *
1017  * Fills in the missing fields in the command, and translates the cliprects
1018  * to match the destination bounding box encoded.
1019  */
vmw_sou_surface_fifo_commit(struct vmw_kms_dirty * dirty)1020 static void vmw_sou_surface_fifo_commit(struct vmw_kms_dirty *dirty)
1021 {
1022 	struct vmw_kms_sou_surface_dirty *sdirty =
1023 		container_of(dirty, typeof(*sdirty), base);
1024 	struct vmw_kms_sou_dirty_cmd *cmd = dirty->cmd;
1025 	s32 trans_x = dirty->unit->crtc.x - sdirty->dst_x;
1026 	s32 trans_y = dirty->unit->crtc.y - sdirty->dst_y;
1027 	size_t region_size = dirty->num_hits * sizeof(SVGASignedRect);
1028 	SVGASignedRect *blit = (SVGASignedRect *) &cmd[1];
1029 	int i;
1030 
1031 	if (!dirty->num_hits) {
1032 		vmw_cmd_commit(dirty->dev_priv, 0);
1033 		return;
1034 	}
1035 
1036 	cmd->header.id = SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN;
1037 	cmd->header.size = sizeof(cmd->body) + region_size;
1038 
1039 	/*
1040 	 * Use the destination bounding box to specify destination - and
1041 	 * source bounding regions.
1042 	 */
1043 	cmd->body.destRect.left = sdirty->left;
1044 	cmd->body.destRect.right = sdirty->right;
1045 	cmd->body.destRect.top = sdirty->top;
1046 	cmd->body.destRect.bottom = sdirty->bottom;
1047 
1048 	cmd->body.srcRect.left = sdirty->left + trans_x;
1049 	cmd->body.srcRect.right = sdirty->right + trans_x;
1050 	cmd->body.srcRect.top = sdirty->top + trans_y;
1051 	cmd->body.srcRect.bottom = sdirty->bottom + trans_y;
1052 
1053 	cmd->body.srcImage.sid = sdirty->sid;
1054 	cmd->body.destScreenId = dirty->unit->unit;
1055 
1056 	/* Blits are relative to the destination rect. Translate. */
1057 	for (i = 0; i < dirty->num_hits; ++i, ++blit) {
1058 		blit->left -= sdirty->left;
1059 		blit->right -= sdirty->left;
1060 		blit->top -= sdirty->top;
1061 		blit->bottom -= sdirty->top;
1062 	}
1063 
1064 	vmw_cmd_commit(dirty->dev_priv, region_size + sizeof(*cmd));
1065 
1066 	sdirty->left = sdirty->top = S32_MAX;
1067 	sdirty->right = sdirty->bottom = S32_MIN;
1068 }
1069 
1070 /**
1071  * vmw_sou_surface_clip - Callback to encode a blit surface to screen cliprect.
1072  *
1073  * @dirty: The closure structure
1074  *
1075  * Encodes a SVGASignedRect cliprect and updates the bounding box of the
1076  * BLIT_SURFACE_TO_SCREEN command.
1077  */
vmw_sou_surface_clip(struct vmw_kms_dirty * dirty)1078 static void vmw_sou_surface_clip(struct vmw_kms_dirty *dirty)
1079 {
1080 	struct vmw_kms_sou_surface_dirty *sdirty =
1081 		container_of(dirty, typeof(*sdirty), base);
1082 	struct vmw_kms_sou_dirty_cmd *cmd = dirty->cmd;
1083 	SVGASignedRect *blit = (SVGASignedRect *) &cmd[1];
1084 
1085 	/* Destination rect. */
1086 	blit += dirty->num_hits;
1087 	blit->left = dirty->unit_x1;
1088 	blit->top = dirty->unit_y1;
1089 	blit->right = dirty->unit_x2;
1090 	blit->bottom = dirty->unit_y2;
1091 
1092 	/* Destination bounding box */
1093 	sdirty->left = min_t(s32, sdirty->left, dirty->unit_x1);
1094 	sdirty->top = min_t(s32, sdirty->top, dirty->unit_y1);
1095 	sdirty->right = max_t(s32, sdirty->right, dirty->unit_x2);
1096 	sdirty->bottom = max_t(s32, sdirty->bottom, dirty->unit_y2);
1097 
1098 	dirty->num_hits++;
1099 }
1100 
1101 /**
1102  * vmw_kms_sou_do_surface_dirty - Dirty part of a surface backed framebuffer
1103  *
1104  * @dev_priv: Pointer to the device private structure.
1105  * @framebuffer: Pointer to the surface-buffer backed framebuffer.
1106  * @clips: Array of clip rects. Either @clips or @vclips must be NULL.
1107  * @vclips: Alternate array of clip rects. Either @clips or @vclips must
1108  * be NULL.
1109  * @srf: Pointer to surface to blit from. If NULL, the surface attached
1110  * to @framebuffer will be used.
1111  * @dest_x: X coordinate offset to align @srf with framebuffer coordinates.
1112  * @dest_y: Y coordinate offset to align @srf with framebuffer coordinates.
1113  * @num_clips: Number of clip rects in @clips.
1114  * @inc: Increment to use when looping over @clips.
1115  * @out_fence: If non-NULL, will return a ref-counted pointer to a
1116  * struct vmw_fence_obj. The returned fence pointer may be NULL in which
1117  * case the device has already synchronized.
1118  * @crtc: If crtc is passed, perform surface dirty on that crtc only.
1119  *
1120  * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
1121  * interrupted.
1122  */
vmw_kms_sou_do_surface_dirty(struct vmw_private * dev_priv,struct vmw_framebuffer * framebuffer,struct drm_clip_rect * clips,struct drm_vmw_rect * vclips,struct vmw_resource * srf,s32 dest_x,s32 dest_y,unsigned num_clips,int inc,struct vmw_fence_obj ** out_fence,struct drm_crtc * crtc)1123 int vmw_kms_sou_do_surface_dirty(struct vmw_private *dev_priv,
1124 				 struct vmw_framebuffer *framebuffer,
1125 				 struct drm_clip_rect *clips,
1126 				 struct drm_vmw_rect *vclips,
1127 				 struct vmw_resource *srf,
1128 				 s32 dest_x,
1129 				 s32 dest_y,
1130 				 unsigned num_clips, int inc,
1131 				 struct vmw_fence_obj **out_fence,
1132 				 struct drm_crtc *crtc)
1133 {
1134 	struct vmw_framebuffer_surface *vfbs =
1135 		container_of(framebuffer, typeof(*vfbs), base);
1136 	struct vmw_kms_sou_surface_dirty sdirty;
1137 	DECLARE_VAL_CONTEXT(val_ctx, NULL, 0);
1138 	int ret;
1139 
1140 	if (!srf)
1141 		srf = &vfbs->surface->res;
1142 
1143 	ret = vmw_validation_add_resource(&val_ctx, srf, 0, VMW_RES_DIRTY_NONE,
1144 					  NULL, NULL);
1145 	if (ret)
1146 		return ret;
1147 
1148 	ret = vmw_validation_prepare(&val_ctx, &dev_priv->cmdbuf_mutex, true);
1149 	if (ret)
1150 		goto out_unref;
1151 
1152 	sdirty.base.fifo_commit = vmw_sou_surface_fifo_commit;
1153 	sdirty.base.clip = vmw_sou_surface_clip;
1154 	sdirty.base.dev_priv = dev_priv;
1155 	sdirty.base.fifo_reserve_size = sizeof(struct vmw_kms_sou_dirty_cmd) +
1156 	  sizeof(SVGASignedRect) * num_clips;
1157 	sdirty.base.crtc = crtc;
1158 
1159 	sdirty.sid = srf->id;
1160 	sdirty.left = sdirty.top = S32_MAX;
1161 	sdirty.right = sdirty.bottom = S32_MIN;
1162 	sdirty.dst_x = dest_x;
1163 	sdirty.dst_y = dest_y;
1164 
1165 	ret = vmw_kms_helper_dirty(dev_priv, framebuffer, clips, vclips,
1166 				   dest_x, dest_y, num_clips, inc,
1167 				   &sdirty.base);
1168 	vmw_kms_helper_validation_finish(dev_priv, NULL, &val_ctx, out_fence,
1169 					 NULL);
1170 
1171 	return ret;
1172 
1173 out_unref:
1174 	vmw_validation_unref_lists(&val_ctx);
1175 	return ret;
1176 }
1177 
1178 /**
1179  * vmw_sou_bo_fifo_commit - Callback to submit a set of readback clips.
1180  *
1181  * @dirty: The closure structure.
1182  *
1183  * Commits a previously built command buffer of readback clips.
1184  */
vmw_sou_bo_fifo_commit(struct vmw_kms_dirty * dirty)1185 static void vmw_sou_bo_fifo_commit(struct vmw_kms_dirty *dirty)
1186 {
1187 	if (!dirty->num_hits) {
1188 		vmw_cmd_commit(dirty->dev_priv, 0);
1189 		return;
1190 	}
1191 
1192 	vmw_cmd_commit(dirty->dev_priv,
1193 			sizeof(struct vmw_kms_sou_bo_blit) *
1194 			dirty->num_hits);
1195 }
1196 
1197 /**
1198  * vmw_sou_bo_clip - Callback to encode a readback cliprect.
1199  *
1200  * @dirty: The closure structure
1201  *
1202  * Encodes a BLIT_GMRFB_TO_SCREEN cliprect.
1203  */
vmw_sou_bo_clip(struct vmw_kms_dirty * dirty)1204 static void vmw_sou_bo_clip(struct vmw_kms_dirty *dirty)
1205 {
1206 	struct vmw_kms_sou_bo_blit *blit = dirty->cmd;
1207 
1208 	blit += dirty->num_hits;
1209 	blit->header = SVGA_CMD_BLIT_GMRFB_TO_SCREEN;
1210 	blit->body.destScreenId = dirty->unit->unit;
1211 	blit->body.srcOrigin.x = dirty->fb_x;
1212 	blit->body.srcOrigin.y = dirty->fb_y;
1213 	blit->body.destRect.left = dirty->unit_x1;
1214 	blit->body.destRect.top = dirty->unit_y1;
1215 	blit->body.destRect.right = dirty->unit_x2;
1216 	blit->body.destRect.bottom = dirty->unit_y2;
1217 	dirty->num_hits++;
1218 }
1219 
1220 /**
1221  * vmw_kms_sou_do_bo_dirty - Dirty part of a buffer-object backed framebuffer
1222  *
1223  * @dev_priv: Pointer to the device private structure.
1224  * @framebuffer: Pointer to the buffer-object backed framebuffer.
1225  * @clips: Array of clip rects.
1226  * @vclips: Alternate array of clip rects. Either @clips or @vclips must
1227  * be NULL.
1228  * @num_clips: Number of clip rects in @clips.
1229  * @increment: Increment to use when looping over @clips.
1230  * @interruptible: Whether to perform waits interruptible if possible.
1231  * @out_fence: If non-NULL, will return a ref-counted pointer to a
1232  * struct vmw_fence_obj. The returned fence pointer may be NULL in which
1233  * case the device has already synchronized.
1234  * @crtc: If crtc is passed, perform bo dirty on that crtc only.
1235  *
1236  * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
1237  * interrupted.
1238  */
vmw_kms_sou_do_bo_dirty(struct vmw_private * dev_priv,struct vmw_framebuffer * framebuffer,struct drm_clip_rect * clips,struct drm_vmw_rect * vclips,unsigned num_clips,int increment,bool interruptible,struct vmw_fence_obj ** out_fence,struct drm_crtc * crtc)1239 int vmw_kms_sou_do_bo_dirty(struct vmw_private *dev_priv,
1240 				struct vmw_framebuffer *framebuffer,
1241 				struct drm_clip_rect *clips,
1242 				struct drm_vmw_rect *vclips,
1243 				unsigned num_clips, int increment,
1244 				bool interruptible,
1245 				struct vmw_fence_obj **out_fence,
1246 				struct drm_crtc *crtc)
1247 {
1248 	struct vmw_buffer_object *buf =
1249 		container_of(framebuffer, struct vmw_framebuffer_bo,
1250 			     base)->buffer;
1251 	struct vmw_kms_dirty dirty;
1252 	DECLARE_VAL_CONTEXT(val_ctx, NULL, 0);
1253 	int ret;
1254 
1255 	ret = vmw_validation_add_bo(&val_ctx, buf, false, false);
1256 	if (ret)
1257 		return ret;
1258 
1259 	ret = vmw_validation_prepare(&val_ctx, NULL, interruptible);
1260 	if (ret)
1261 		goto out_unref;
1262 
1263 	ret = do_bo_define_gmrfb(dev_priv, framebuffer);
1264 	if (unlikely(ret != 0))
1265 		goto out_revert;
1266 
1267 	dirty.crtc = crtc;
1268 	dirty.fifo_commit = vmw_sou_bo_fifo_commit;
1269 	dirty.clip = vmw_sou_bo_clip;
1270 	dirty.fifo_reserve_size = sizeof(struct vmw_kms_sou_bo_blit) *
1271 		num_clips;
1272 	ret = vmw_kms_helper_dirty(dev_priv, framebuffer, clips, vclips,
1273 				   0, 0, num_clips, increment, &dirty);
1274 	vmw_kms_helper_validation_finish(dev_priv, NULL, &val_ctx, out_fence,
1275 					 NULL);
1276 
1277 	return ret;
1278 
1279 out_revert:
1280 	vmw_validation_revert(&val_ctx);
1281 out_unref:
1282 	vmw_validation_unref_lists(&val_ctx);
1283 
1284 	return ret;
1285 }
1286 
1287 
1288 /**
1289  * vmw_sou_readback_fifo_commit - Callback to submit a set of readback clips.
1290  *
1291  * @dirty: The closure structure.
1292  *
1293  * Commits a previously built command buffer of readback clips.
1294  */
vmw_sou_readback_fifo_commit(struct vmw_kms_dirty * dirty)1295 static void vmw_sou_readback_fifo_commit(struct vmw_kms_dirty *dirty)
1296 {
1297 	if (!dirty->num_hits) {
1298 		vmw_cmd_commit(dirty->dev_priv, 0);
1299 		return;
1300 	}
1301 
1302 	vmw_cmd_commit(dirty->dev_priv,
1303 			sizeof(struct vmw_kms_sou_readback_blit) *
1304 			dirty->num_hits);
1305 }
1306 
1307 /**
1308  * vmw_sou_readback_clip - Callback to encode a readback cliprect.
1309  *
1310  * @dirty: The closure structure
1311  *
1312  * Encodes a BLIT_SCREEN_TO_GMRFB cliprect.
1313  */
vmw_sou_readback_clip(struct vmw_kms_dirty * dirty)1314 static void vmw_sou_readback_clip(struct vmw_kms_dirty *dirty)
1315 {
1316 	struct vmw_kms_sou_readback_blit *blit = dirty->cmd;
1317 
1318 	blit += dirty->num_hits;
1319 	blit->header = SVGA_CMD_BLIT_SCREEN_TO_GMRFB;
1320 	blit->body.srcScreenId = dirty->unit->unit;
1321 	blit->body.destOrigin.x = dirty->fb_x;
1322 	blit->body.destOrigin.y = dirty->fb_y;
1323 	blit->body.srcRect.left = dirty->unit_x1;
1324 	blit->body.srcRect.top = dirty->unit_y1;
1325 	blit->body.srcRect.right = dirty->unit_x2;
1326 	blit->body.srcRect.bottom = dirty->unit_y2;
1327 	dirty->num_hits++;
1328 }
1329 
1330 /**
1331  * vmw_kms_sou_readback - Perform a readback from the screen object system to
1332  * a buffer-object backed framebuffer.
1333  *
1334  * @dev_priv: Pointer to the device private structure.
1335  * @file_priv: Pointer to a struct drm_file identifying the caller.
1336  * Must be set to NULL if @user_fence_rep is NULL.
1337  * @vfb: Pointer to the buffer-object backed framebuffer.
1338  * @user_fence_rep: User-space provided structure for fence information.
1339  * Must be set to non-NULL if @file_priv is non-NULL.
1340  * @vclips: Array of clip rects.
1341  * @num_clips: Number of clip rects in @vclips.
1342  * @crtc: If crtc is passed, readback on that crtc only.
1343  *
1344  * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
1345  * interrupted.
1346  */
vmw_kms_sou_readback(struct vmw_private * dev_priv,struct drm_file * file_priv,struct vmw_framebuffer * vfb,struct drm_vmw_fence_rep __user * user_fence_rep,struct drm_vmw_rect * vclips,uint32_t num_clips,struct drm_crtc * crtc)1347 int vmw_kms_sou_readback(struct vmw_private *dev_priv,
1348 			 struct drm_file *file_priv,
1349 			 struct vmw_framebuffer *vfb,
1350 			 struct drm_vmw_fence_rep __user *user_fence_rep,
1351 			 struct drm_vmw_rect *vclips,
1352 			 uint32_t num_clips,
1353 			 struct drm_crtc *crtc)
1354 {
1355 	struct vmw_buffer_object *buf =
1356 		container_of(vfb, struct vmw_framebuffer_bo, base)->buffer;
1357 	struct vmw_kms_dirty dirty;
1358 	DECLARE_VAL_CONTEXT(val_ctx, NULL, 0);
1359 	int ret;
1360 
1361 	ret = vmw_validation_add_bo(&val_ctx, buf, false, false);
1362 	if (ret)
1363 		return ret;
1364 
1365 	ret = vmw_validation_prepare(&val_ctx, NULL, true);
1366 	if (ret)
1367 		goto out_unref;
1368 
1369 	ret = do_bo_define_gmrfb(dev_priv, vfb);
1370 	if (unlikely(ret != 0))
1371 		goto out_revert;
1372 
1373 	dirty.crtc = crtc;
1374 	dirty.fifo_commit = vmw_sou_readback_fifo_commit;
1375 	dirty.clip = vmw_sou_readback_clip;
1376 	dirty.fifo_reserve_size = sizeof(struct vmw_kms_sou_readback_blit) *
1377 		num_clips;
1378 	ret = vmw_kms_helper_dirty(dev_priv, vfb, NULL, vclips,
1379 				   0, 0, num_clips, 1, &dirty);
1380 	vmw_kms_helper_validation_finish(dev_priv, file_priv, &val_ctx, NULL,
1381 					 user_fence_rep);
1382 
1383 	return ret;
1384 
1385 out_revert:
1386 	vmw_validation_revert(&val_ctx);
1387 out_unref:
1388 	vmw_validation_unref_lists(&val_ctx);
1389 
1390 	return ret;
1391 }
1392