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