1 /**************************************************************************
2  *
3  * Copyright © 2011-2015 VMware, Inc., Palo Alto, CA., USA
4  * All Rights Reserved.
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 <drm/drm_plane_helper.h>
30 #include <drm/drm_atomic.h>
31 #include <drm/drm_atomic_helper.h>
32 
33 
34 #define vmw_crtc_to_sou(x) \
35 	container_of(x, struct vmw_screen_object_unit, base.crtc)
36 #define vmw_encoder_to_sou(x) \
37 	container_of(x, struct vmw_screen_object_unit, base.encoder)
38 #define vmw_connector_to_sou(x) \
39 	container_of(x, struct vmw_screen_object_unit, base.connector)
40 
41 /**
42  * struct vmw_kms_sou_surface_dirty - Closure structure for
43  * blit surface to screen command.
44  * @base: The base type we derive from. Used by vmw_kms_helper_dirty().
45  * @left: Left side of bounding box.
46  * @right: Right side of bounding box.
47  * @top: Top side of bounding box.
48  * @bottom: Bottom side of bounding box.
49  * @dst_x: Difference between source clip rects and framebuffer coordinates.
50  * @dst_y: Difference between source clip rects and framebuffer coordinates.
51  * @sid: Surface id of surface to copy from.
52  */
53 struct vmw_kms_sou_surface_dirty {
54 	struct vmw_kms_dirty base;
55 	s32 left, right, top, bottom;
56 	s32 dst_x, dst_y;
57 	u32 sid;
58 };
59 
60 /*
61  * SVGA commands that are used by this code. Please see the device headers
62  * for explanation.
63  */
64 struct vmw_kms_sou_readback_blit {
65 	uint32 header;
66 	SVGAFifoCmdBlitScreenToGMRFB body;
67 };
68 
69 struct vmw_kms_sou_dmabuf_blit {
70 	uint32 header;
71 	SVGAFifoCmdBlitGMRFBToScreen body;
72 };
73 
74 struct vmw_kms_sou_dirty_cmd {
75 	SVGA3dCmdHeader header;
76 	SVGA3dCmdBlitSurfaceToScreen body;
77 };
78 
79 /**
80  * Display unit using screen objects.
81  */
82 struct vmw_screen_object_unit {
83 	struct vmw_display_unit base;
84 
85 	unsigned long buffer_size; /**< Size of allocated buffer */
86 	struct vmw_dma_buffer *buffer; /**< Backing store buffer */
87 
88 	bool defined;
89 };
90 
91 static void vmw_sou_destroy(struct vmw_screen_object_unit *sou)
92 {
93 	vmw_du_cleanup(&sou->base);
94 	kfree(sou);
95 }
96 
97 
98 /*
99  * Screen Object Display Unit CRTC functions
100  */
101 
102 static void vmw_sou_crtc_destroy(struct drm_crtc *crtc)
103 {
104 	vmw_sou_destroy(vmw_crtc_to_sou(crtc));
105 }
106 
107 /**
108  * Send the fifo command to create a screen.
109  */
110 static int vmw_sou_fifo_create(struct vmw_private *dev_priv,
111 			       struct vmw_screen_object_unit *sou,
112 			       uint32_t x, uint32_t y,
113 			       struct drm_display_mode *mode)
114 {
115 	size_t fifo_size;
116 
117 	struct {
118 		struct {
119 			uint32_t cmdType;
120 		} header;
121 		SVGAScreenObject obj;
122 	} *cmd;
123 
124 	BUG_ON(!sou->buffer);
125 
126 	fifo_size = sizeof(*cmd);
127 	cmd = vmw_fifo_reserve(dev_priv, fifo_size);
128 	/* The hardware has hung, nothing we can do about it here. */
129 	if (unlikely(cmd == NULL)) {
130 		DRM_ERROR("Fifo reserve failed.\n");
131 		return -ENOMEM;
132 	}
133 
134 	memset(cmd, 0, fifo_size);
135 	cmd->header.cmdType = SVGA_CMD_DEFINE_SCREEN;
136 	cmd->obj.structSize = sizeof(SVGAScreenObject);
137 	cmd->obj.id = sou->base.unit;
138 	cmd->obj.flags = SVGA_SCREEN_HAS_ROOT |
139 		(sou->base.unit == 0 ? SVGA_SCREEN_IS_PRIMARY : 0);
140 	cmd->obj.size.width = mode->hdisplay;
141 	cmd->obj.size.height = mode->vdisplay;
142 	if (sou->base.is_implicit) {
143 		cmd->obj.root.x = x;
144 		cmd->obj.root.y = y;
145 	} else {
146 		cmd->obj.root.x = sou->base.gui_x;
147 		cmd->obj.root.y = sou->base.gui_y;
148 	}
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_fifo_commit(dev_priv, fifo_size);
157 
158 	sou->defined = true;
159 
160 	return 0;
161 }
162 
163 /**
164  * Send the fifo command to destroy a screen.
165  */
166 static int vmw_sou_fifo_destroy(struct vmw_private *dev_priv,
167 				struct vmw_screen_object_unit *sou)
168 {
169 	size_t fifo_size;
170 	int ret;
171 
172 	struct {
173 		struct {
174 			uint32_t cmdType;
175 		} header;
176 		SVGAFifoCmdDestroyScreen body;
177 	} *cmd;
178 
179 	/* no need to do anything */
180 	if (unlikely(!sou->defined))
181 		return 0;
182 
183 	fifo_size = sizeof(*cmd);
184 	cmd = vmw_fifo_reserve(dev_priv, fifo_size);
185 	/* the hardware has hung, nothing we can do about it here */
186 	if (unlikely(cmd == NULL)) {
187 		DRM_ERROR("Fifo reserve failed.\n");
188 		return -ENOMEM;
189 	}
190 
191 	memset(cmd, 0, fifo_size);
192 	cmd->header.cmdType = SVGA_CMD_DESTROY_SCREEN;
193 	cmd->body.screenId = sou->base.unit;
194 
195 	vmw_fifo_commit(dev_priv, fifo_size);
196 
197 	/* Force sync */
198 	ret = vmw_fallback_wait(dev_priv, false, true, 0, false, 3*HZ);
199 	if (unlikely(ret != 0))
200 		DRM_ERROR("Failed to sync with HW");
201 	else
202 		sou->defined = false;
203 
204 	return ret;
205 }
206 
207 /**
208  * vmw_sou_crtc_mode_set_nofb - Create new screen
209  *
210  * @crtc: CRTC associated with the new screen
211  *
212  * This function creates/destroys a screen.  This function cannot fail, so if
213  * somehow we run into a failure, just do the best we can to get out.
214  */
215 static void vmw_sou_crtc_mode_set_nofb(struct drm_crtc *crtc)
216 {
217 	struct vmw_private *dev_priv;
218 	struct vmw_screen_object_unit *sou;
219 	struct vmw_framebuffer *vfb;
220 	struct drm_framebuffer *fb;
221 	struct drm_plane_state *ps;
222 	struct vmw_plane_state *vps;
223 	int ret;
224 
225 
226 	sou      = vmw_crtc_to_sou(crtc);
227 	dev_priv = vmw_priv(crtc->dev);
228 	ps       = crtc->primary->state;
229 	fb       = ps->fb;
230 	vps      = vmw_plane_state_to_vps(ps);
231 
232 	vfb = (fb) ? vmw_framebuffer_to_vfb(fb) : NULL;
233 
234 	if (sou->defined) {
235 		ret = vmw_sou_fifo_destroy(dev_priv, sou);
236 		if (ret) {
237 			DRM_ERROR("Failed to destroy Screen Object\n");
238 			return;
239 		}
240 	}
241 
242 	if (vfb) {
243 		sou->buffer = vps->dmabuf;
244 		sou->buffer_size = vps->dmabuf_size;
245 
246 		ret = vmw_sou_fifo_create(dev_priv, sou, crtc->x, crtc->y,
247 					  &crtc->mode);
248 		if (ret)
249 			DRM_ERROR("Failed to define Screen Object %dx%d\n",
250 				  crtc->x, crtc->y);
251 
252 		vmw_kms_add_active(dev_priv, &sou->base, vfb);
253 	} else {
254 		sou->buffer = NULL;
255 		sou->buffer_size = 0;
256 
257 		vmw_kms_del_active(dev_priv, &sou->base);
258 	}
259 }
260 
261 /**
262  * vmw_sou_crtc_helper_prepare - Noop
263  *
264  * @crtc: CRTC associated with the new screen
265  *
266  * Prepares the CRTC for a mode set, but we don't need to do anything here.
267  */
268 static void vmw_sou_crtc_helper_prepare(struct drm_crtc *crtc)
269 {
270 }
271 
272 /**
273  * vmw_sou_crtc_helper_commit - Noop
274  *
275  * @crtc: CRTC associated with the new screen
276  *
277  * This is called after a mode set has been completed.
278  */
279 static void vmw_sou_crtc_helper_commit(struct drm_crtc *crtc)
280 {
281 }
282 
283 /**
284  * vmw_sou_crtc_helper_disable - Turns off CRTC
285  *
286  * @crtc: CRTC to be turned off
287  */
288 static void vmw_sou_crtc_helper_disable(struct drm_crtc *crtc)
289 {
290 	struct vmw_private *dev_priv;
291 	struct vmw_screen_object_unit *sou;
292 	int ret;
293 
294 
295 	if (!crtc) {
296 		DRM_ERROR("CRTC is NULL\n");
297 		return;
298 	}
299 
300 	sou = vmw_crtc_to_sou(crtc);
301 	dev_priv = vmw_priv(crtc->dev);
302 
303 	if (sou->defined) {
304 		ret = vmw_sou_fifo_destroy(dev_priv, sou);
305 		if (ret)
306 			DRM_ERROR("Failed to destroy Screen Object\n");
307 	}
308 }
309 
310 static int vmw_sou_crtc_page_flip(struct drm_crtc *crtc,
311 				  struct drm_framebuffer *new_fb,
312 				  struct drm_pending_vblank_event *event,
313 				  uint32_t flags,
314 				  struct drm_modeset_acquire_ctx *ctx)
315 {
316 	struct vmw_private *dev_priv = vmw_priv(crtc->dev);
317 	struct drm_framebuffer *old_fb = crtc->primary->fb;
318 	struct vmw_framebuffer *vfb = vmw_framebuffer_to_vfb(new_fb);
319 	struct vmw_fence_obj *fence = NULL;
320 	struct drm_vmw_rect vclips;
321 	int ret;
322 
323 	if (!vmw_kms_crtc_flippable(dev_priv, crtc))
324 		return -EINVAL;
325 
326 	flags &= ~DRM_MODE_PAGE_FLIP_ASYNC;
327 	ret = drm_atomic_helper_page_flip(crtc, new_fb, NULL, flags, ctx);
328 	if (ret) {
329 		DRM_ERROR("Page flip error %d.\n", ret);
330 		return ret;
331 	}
332 
333 	/* do a full screen dirty update */
334 	vclips.x = crtc->x;
335 	vclips.y = crtc->y;
336 	vclips.w = crtc->mode.hdisplay;
337 	vclips.h = crtc->mode.vdisplay;
338 
339 	if (vfb->dmabuf)
340 		ret = vmw_kms_sou_do_dmabuf_dirty(dev_priv, vfb,
341 						  NULL, &vclips, 1, 1,
342 						  true, &fence);
343 	else
344 		ret = vmw_kms_sou_do_surface_dirty(dev_priv, vfb,
345 						   NULL, &vclips, NULL,
346 						   0, 0, 1, 1, &fence);
347 
348 
349 	if (ret != 0)
350 		goto out_no_fence;
351 	if (!fence) {
352 		ret = -EINVAL;
353 		goto out_no_fence;
354 	}
355 
356 	if (event) {
357 		struct drm_file *file_priv = event->base.file_priv;
358 
359 		ret = vmw_event_fence_action_queue(file_priv, fence,
360 						   &event->base,
361 						   &event->event.tv_sec,
362 						   &event->event.tv_usec,
363 						   true);
364 	}
365 
366 	/*
367 	 * No need to hold on to this now. The only cleanup
368 	 * we need to do if we fail is unref the fence.
369 	 */
370 	vmw_fence_obj_unreference(&fence);
371 
372 	if (vmw_crtc_to_du(crtc)->is_implicit)
373 		vmw_kms_update_implicit_fb(dev_priv, crtc);
374 
375 	return ret;
376 
377 out_no_fence:
378 	drm_atomic_set_fb_for_plane(crtc->primary->state, old_fb);
379 	return ret;
380 }
381 
382 static const struct drm_crtc_funcs vmw_screen_object_crtc_funcs = {
383 	.gamma_set = vmw_du_crtc_gamma_set,
384 	.destroy = vmw_sou_crtc_destroy,
385 	.reset = vmw_du_crtc_reset,
386 	.atomic_duplicate_state = vmw_du_crtc_duplicate_state,
387 	.atomic_destroy_state = vmw_du_crtc_destroy_state,
388 	.set_config = vmw_kms_set_config,
389 	.page_flip = vmw_sou_crtc_page_flip,
390 };
391 
392 /*
393  * Screen Object Display Unit encoder functions
394  */
395 
396 static void vmw_sou_encoder_destroy(struct drm_encoder *encoder)
397 {
398 	vmw_sou_destroy(vmw_encoder_to_sou(encoder));
399 }
400 
401 static const struct drm_encoder_funcs vmw_screen_object_encoder_funcs = {
402 	.destroy = vmw_sou_encoder_destroy,
403 };
404 
405 /*
406  * Screen Object Display Unit connector functions
407  */
408 
409 static void vmw_sou_connector_destroy(struct drm_connector *connector)
410 {
411 	vmw_sou_destroy(vmw_connector_to_sou(connector));
412 }
413 
414 static const struct drm_connector_funcs vmw_sou_connector_funcs = {
415 	.dpms = vmw_du_connector_dpms,
416 	.detect = vmw_du_connector_detect,
417 	.fill_modes = vmw_du_connector_fill_modes,
418 	.set_property = vmw_du_connector_set_property,
419 	.destroy = vmw_sou_connector_destroy,
420 	.reset = vmw_du_connector_reset,
421 	.atomic_duplicate_state = drm_atomic_helper_connector_duplicate_state,
422 	.atomic_destroy_state = drm_atomic_helper_connector_destroy_state,
423 	.atomic_set_property = vmw_du_connector_atomic_set_property,
424 	.atomic_get_property = vmw_du_connector_atomic_get_property,
425 };
426 
427 
428 static const struct
429 drm_connector_helper_funcs vmw_sou_connector_helper_funcs = {
430 	.best_encoder = drm_atomic_helper_best_encoder,
431 };
432 
433 
434 
435 /*
436  * Screen Object Display Plane Functions
437  */
438 
439 /**
440  * vmw_sou_primary_plane_cleanup_fb - Frees sou backing buffer
441  *
442  * @plane:  display plane
443  * @old_state: Contains the FB to clean up
444  *
445  * Unpins the display surface
446  *
447  * Returns 0 on success
448  */
449 static void
450 vmw_sou_primary_plane_cleanup_fb(struct drm_plane *plane,
451 				 struct drm_plane_state *old_state)
452 {
453 	struct vmw_plane_state *vps = vmw_plane_state_to_vps(old_state);
454 
455 	vmw_dmabuf_unreference(&vps->dmabuf);
456 	vps->dmabuf_size = 0;
457 
458 	vmw_du_plane_cleanup_fb(plane, old_state);
459 }
460 
461 
462 /**
463  * vmw_sou_primary_plane_prepare_fb - allocate backing buffer
464  *
465  * @plane:  display plane
466  * @new_state: info on the new plane state, including the FB
467  *
468  * The SOU backing buffer is our equivalent of the display plane.
469  *
470  * Returns 0 on success
471  */
472 static int
473 vmw_sou_primary_plane_prepare_fb(struct drm_plane *plane,
474 				 struct drm_plane_state *new_state)
475 {
476 	struct drm_framebuffer *new_fb = new_state->fb;
477 	struct drm_crtc *crtc = plane->state->crtc ?: new_state->crtc;
478 	struct vmw_plane_state *vps = vmw_plane_state_to_vps(new_state);
479 	struct vmw_private *dev_priv;
480 	size_t size;
481 	int ret;
482 
483 
484 	if (!new_fb) {
485 		vmw_dmabuf_unreference(&vps->dmabuf);
486 		vps->dmabuf_size = 0;
487 
488 		return 0;
489 	}
490 
491 	size = new_state->crtc_w * new_state->crtc_h * 4;
492 
493 	if (vps->dmabuf) {
494 		if (vps->dmabuf_size == size)
495 			return 0;
496 
497 		vmw_dmabuf_unreference(&vps->dmabuf);
498 		vps->dmabuf_size = 0;
499 	}
500 
501 	vps->dmabuf = kzalloc(sizeof(*vps->dmabuf), GFP_KERNEL);
502 	if (!vps->dmabuf)
503 		return -ENOMEM;
504 
505 	dev_priv = vmw_priv(crtc->dev);
506 	vmw_svga_enable(dev_priv);
507 
508 	/* After we have alloced the backing store might not be able to
509 	 * resume the overlays, this is preferred to failing to alloc.
510 	 */
511 	vmw_overlay_pause_all(dev_priv);
512 	ret = vmw_dmabuf_init(dev_priv, vps->dmabuf, size,
513 			      &vmw_vram_ne_placement,
514 			      false, &vmw_dmabuf_bo_free);
515 	vmw_overlay_resume_all(dev_priv);
516 
517 	if (ret != 0)
518 		vps->dmabuf = NULL; /* vmw_dmabuf_init frees on error */
519 	else
520 		vps->dmabuf_size = size;
521 
522 	return ret;
523 }
524 
525 
526 static void
527 vmw_sou_primary_plane_atomic_update(struct drm_plane *plane,
528 				    struct drm_plane_state *old_state)
529 {
530 	struct drm_crtc *crtc = plane->state->crtc;
531 
532 	if (crtc)
533 		crtc->primary->fb = plane->state->fb;
534 }
535 
536 
537 static const struct drm_plane_funcs vmw_sou_plane_funcs = {
538 	.update_plane = drm_atomic_helper_update_plane,
539 	.disable_plane = drm_atomic_helper_disable_plane,
540 	.destroy = vmw_du_primary_plane_destroy,
541 	.reset = vmw_du_plane_reset,
542 	.atomic_duplicate_state = vmw_du_plane_duplicate_state,
543 	.atomic_destroy_state = vmw_du_plane_destroy_state,
544 };
545 
546 static const struct drm_plane_funcs vmw_sou_cursor_funcs = {
547 	.update_plane = drm_atomic_helper_update_plane,
548 	.disable_plane = drm_atomic_helper_disable_plane,
549 	.destroy = vmw_du_cursor_plane_destroy,
550 	.reset = vmw_du_plane_reset,
551 	.atomic_duplicate_state = vmw_du_plane_duplicate_state,
552 	.atomic_destroy_state = vmw_du_plane_destroy_state,
553 };
554 
555 /*
556  * Atomic Helpers
557  */
558 static const struct
559 drm_plane_helper_funcs vmw_sou_cursor_plane_helper_funcs = {
560 	.atomic_check = vmw_du_cursor_plane_atomic_check,
561 	.atomic_update = vmw_du_cursor_plane_atomic_update,
562 	.prepare_fb = vmw_du_cursor_plane_prepare_fb,
563 	.cleanup_fb = vmw_du_plane_cleanup_fb,
564 };
565 
566 static const struct
567 drm_plane_helper_funcs vmw_sou_primary_plane_helper_funcs = {
568 	.atomic_check = vmw_du_primary_plane_atomic_check,
569 	.atomic_update = vmw_sou_primary_plane_atomic_update,
570 	.prepare_fb = vmw_sou_primary_plane_prepare_fb,
571 	.cleanup_fb = vmw_sou_primary_plane_cleanup_fb,
572 };
573 
574 static const struct drm_crtc_helper_funcs vmw_sou_crtc_helper_funcs = {
575 	.prepare = vmw_sou_crtc_helper_prepare,
576 	.commit = vmw_sou_crtc_helper_commit,
577 	.disable = vmw_sou_crtc_helper_disable,
578 	.mode_set_nofb = vmw_sou_crtc_mode_set_nofb,
579 	.atomic_check = vmw_du_crtc_atomic_check,
580 	.atomic_begin = vmw_du_crtc_atomic_begin,
581 	.atomic_flush = vmw_du_crtc_atomic_flush,
582 };
583 
584 
585 static int vmw_sou_init(struct vmw_private *dev_priv, unsigned unit)
586 {
587 	struct vmw_screen_object_unit *sou;
588 	struct drm_device *dev = dev_priv->dev;
589 	struct drm_connector *connector;
590 	struct drm_encoder *encoder;
591 	struct drm_plane *primary, *cursor;
592 	struct drm_crtc *crtc;
593 	int ret;
594 
595 	sou = kzalloc(sizeof(*sou), GFP_KERNEL);
596 	if (!sou)
597 		return -ENOMEM;
598 
599 	sou->base.unit = unit;
600 	crtc = &sou->base.crtc;
601 	encoder = &sou->base.encoder;
602 	connector = &sou->base.connector;
603 	primary = &sou->base.primary;
604 	cursor = &sou->base.cursor;
605 
606 	sou->base.active_implicit = false;
607 	sou->base.pref_active = (unit == 0);
608 	sou->base.pref_width = dev_priv->initial_width;
609 	sou->base.pref_height = dev_priv->initial_height;
610 	sou->base.pref_mode = NULL;
611 
612 	/*
613 	 * Remove this after enabling atomic because property values can
614 	 * only exist in a state object
615 	 */
616 	sou->base.is_implicit = false;
617 
618 	/* Initialize primary plane */
619 	vmw_du_plane_reset(primary);
620 
621 	ret = drm_universal_plane_init(dev, &sou->base.primary,
622 				       0, &vmw_sou_plane_funcs,
623 				       vmw_primary_plane_formats,
624 				       ARRAY_SIZE(vmw_primary_plane_formats),
625 				       DRM_PLANE_TYPE_PRIMARY, NULL);
626 	if (ret) {
627 		DRM_ERROR("Failed to initialize primary plane");
628 		goto err_free;
629 	}
630 
631 	drm_plane_helper_add(primary, &vmw_sou_primary_plane_helper_funcs);
632 
633 	/* Initialize cursor plane */
634 	vmw_du_plane_reset(cursor);
635 
636 	ret = drm_universal_plane_init(dev, &sou->base.cursor,
637 			0, &vmw_sou_cursor_funcs,
638 			vmw_cursor_plane_formats,
639 			ARRAY_SIZE(vmw_cursor_plane_formats),
640 			DRM_PLANE_TYPE_CURSOR, NULL);
641 	if (ret) {
642 		DRM_ERROR("Failed to initialize cursor plane");
643 		drm_plane_cleanup(&sou->base.primary);
644 		goto err_free;
645 	}
646 
647 	drm_plane_helper_add(cursor, &vmw_sou_cursor_plane_helper_funcs);
648 
649 	vmw_du_connector_reset(connector);
650 	ret = drm_connector_init(dev, connector, &vmw_sou_connector_funcs,
651 				 DRM_MODE_CONNECTOR_VIRTUAL);
652 	if (ret) {
653 		DRM_ERROR("Failed to initialize connector\n");
654 		goto err_free;
655 	}
656 
657 	drm_connector_helper_add(connector, &vmw_sou_connector_helper_funcs);
658 	connector->status = vmw_du_connector_detect(connector, true);
659 	vmw_connector_state_to_vcs(connector->state)->is_implicit = false;
660 
661 
662 	ret = drm_encoder_init(dev, encoder, &vmw_screen_object_encoder_funcs,
663 			       DRM_MODE_ENCODER_VIRTUAL, NULL);
664 	if (ret) {
665 		DRM_ERROR("Failed to initialize encoder\n");
666 		goto err_free_connector;
667 	}
668 
669 	(void) drm_mode_connector_attach_encoder(connector, encoder);
670 	encoder->possible_crtcs = (1 << unit);
671 	encoder->possible_clones = 0;
672 
673 	ret = drm_connector_register(connector);
674 	if (ret) {
675 		DRM_ERROR("Failed to register connector\n");
676 		goto err_free_encoder;
677 	}
678 
679 
680 	vmw_du_crtc_reset(crtc);
681 	ret = drm_crtc_init_with_planes(dev, crtc, &sou->base.primary,
682 					&sou->base.cursor,
683 					&vmw_screen_object_crtc_funcs, NULL);
684 	if (ret) {
685 		DRM_ERROR("Failed to initialize CRTC\n");
686 		goto err_free_unregister;
687 	}
688 
689 	drm_crtc_helper_add(crtc, &vmw_sou_crtc_helper_funcs);
690 
691 	drm_mode_crtc_set_gamma_size(crtc, 256);
692 
693 	drm_object_attach_property(&connector->base,
694 				   dev_priv->hotplug_mode_update_property, 1);
695 	drm_object_attach_property(&connector->base,
696 				   dev->mode_config.suggested_x_property, 0);
697 	drm_object_attach_property(&connector->base,
698 				   dev->mode_config.suggested_y_property, 0);
699 	if (dev_priv->implicit_placement_property)
700 		drm_object_attach_property
701 			(&connector->base,
702 			 dev_priv->implicit_placement_property,
703 			 sou->base.is_implicit);
704 
705 	return 0;
706 
707 err_free_unregister:
708 	drm_connector_unregister(connector);
709 err_free_encoder:
710 	drm_encoder_cleanup(encoder);
711 err_free_connector:
712 	drm_connector_cleanup(connector);
713 err_free:
714 	kfree(sou);
715 	return ret;
716 }
717 
718 int vmw_kms_sou_init_display(struct vmw_private *dev_priv)
719 {
720 	struct drm_device *dev = dev_priv->dev;
721 	int i, ret;
722 
723 	if (!(dev_priv->capabilities & SVGA_CAP_SCREEN_OBJECT_2)) {
724 		DRM_INFO("Not using screen objects,"
725 			 " missing cap SCREEN_OBJECT_2\n");
726 		return -ENOSYS;
727 	}
728 
729 	ret = -ENOMEM;
730 	dev_priv->num_implicit = 0;
731 	dev_priv->implicit_fb = NULL;
732 
733 	ret = drm_vblank_init(dev, VMWGFX_NUM_DISPLAY_UNITS);
734 	if (unlikely(ret != 0))
735 		return ret;
736 
737 	vmw_kms_create_implicit_placement_property(dev_priv, false);
738 
739 	for (i = 0; i < VMWGFX_NUM_DISPLAY_UNITS; ++i)
740 		vmw_sou_init(dev_priv, i);
741 
742 	dev_priv->active_display_unit = vmw_du_screen_object;
743 
744 	DRM_INFO("Screen Objects Display Unit initialized\n");
745 
746 	return 0;
747 }
748 
749 int vmw_kms_sou_close_display(struct vmw_private *dev_priv)
750 {
751 	struct drm_device *dev = dev_priv->dev;
752 
753 	drm_vblank_cleanup(dev);
754 
755 	return 0;
756 }
757 
758 static int do_dmabuf_define_gmrfb(struct vmw_private *dev_priv,
759 				  struct vmw_framebuffer *framebuffer)
760 {
761 	struct vmw_dma_buffer *buf =
762 		container_of(framebuffer, struct vmw_framebuffer_dmabuf,
763 			     base)->buffer;
764 	int depth = framebuffer->base.format->depth;
765 	struct {
766 		uint32_t header;
767 		SVGAFifoCmdDefineGMRFB body;
768 	} *cmd;
769 
770 	/* Emulate RGBA support, contrary to svga_reg.h this is not
771 	 * supported by hosts. This is only a problem if we are reading
772 	 * this value later and expecting what we uploaded back.
773 	 */
774 	if (depth == 32)
775 		depth = 24;
776 
777 	cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
778 	if (!cmd) {
779 		DRM_ERROR("Out of fifo space for dirty framebuffer command.\n");
780 		return -ENOMEM;
781 	}
782 
783 	cmd->header = SVGA_CMD_DEFINE_GMRFB;
784 	cmd->body.format.bitsPerPixel = framebuffer->base.format->cpp[0] * 8;
785 	cmd->body.format.colorDepth = depth;
786 	cmd->body.format.reserved = 0;
787 	cmd->body.bytesPerLine = framebuffer->base.pitches[0];
788 	/* Buffer is reserved in vram or GMR */
789 	vmw_bo_get_guest_ptr(&buf->base, &cmd->body.ptr);
790 	vmw_fifo_commit(dev_priv, sizeof(*cmd));
791 
792 	return 0;
793 }
794 
795 /**
796  * vmw_sou_surface_fifo_commit - Callback to fill in and submit a
797  * blit surface to screen command.
798  *
799  * @dirty: The closure structure.
800  *
801  * Fills in the missing fields in the command, and translates the cliprects
802  * to match the destination bounding box encoded.
803  */
804 static void vmw_sou_surface_fifo_commit(struct vmw_kms_dirty *dirty)
805 {
806 	struct vmw_kms_sou_surface_dirty *sdirty =
807 		container_of(dirty, typeof(*sdirty), base);
808 	struct vmw_kms_sou_dirty_cmd *cmd = dirty->cmd;
809 	s32 trans_x = dirty->unit->crtc.x - sdirty->dst_x;
810 	s32 trans_y = dirty->unit->crtc.y - sdirty->dst_y;
811 	size_t region_size = dirty->num_hits * sizeof(SVGASignedRect);
812 	SVGASignedRect *blit = (SVGASignedRect *) &cmd[1];
813 	int i;
814 
815 	if (!dirty->num_hits) {
816 		vmw_fifo_commit(dirty->dev_priv, 0);
817 		return;
818 	}
819 
820 	cmd->header.id = SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN;
821 	cmd->header.size = sizeof(cmd->body) + region_size;
822 
823 	/*
824 	 * Use the destination bounding box to specify destination - and
825 	 * source bounding regions.
826 	 */
827 	cmd->body.destRect.left = sdirty->left;
828 	cmd->body.destRect.right = sdirty->right;
829 	cmd->body.destRect.top = sdirty->top;
830 	cmd->body.destRect.bottom = sdirty->bottom;
831 
832 	cmd->body.srcRect.left = sdirty->left + trans_x;
833 	cmd->body.srcRect.right = sdirty->right + trans_x;
834 	cmd->body.srcRect.top = sdirty->top + trans_y;
835 	cmd->body.srcRect.bottom = sdirty->bottom + trans_y;
836 
837 	cmd->body.srcImage.sid = sdirty->sid;
838 	cmd->body.destScreenId = dirty->unit->unit;
839 
840 	/* Blits are relative to the destination rect. Translate. */
841 	for (i = 0; i < dirty->num_hits; ++i, ++blit) {
842 		blit->left -= sdirty->left;
843 		blit->right -= sdirty->left;
844 		blit->top -= sdirty->top;
845 		blit->bottom -= sdirty->top;
846 	}
847 
848 	vmw_fifo_commit(dirty->dev_priv, region_size + sizeof(*cmd));
849 
850 	sdirty->left = sdirty->top = S32_MAX;
851 	sdirty->right = sdirty->bottom = S32_MIN;
852 }
853 
854 /**
855  * vmw_sou_surface_clip - Callback to encode a blit surface to screen cliprect.
856  *
857  * @dirty: The closure structure
858  *
859  * Encodes a SVGASignedRect cliprect and updates the bounding box of the
860  * BLIT_SURFACE_TO_SCREEN command.
861  */
862 static void vmw_sou_surface_clip(struct vmw_kms_dirty *dirty)
863 {
864 	struct vmw_kms_sou_surface_dirty *sdirty =
865 		container_of(dirty, typeof(*sdirty), base);
866 	struct vmw_kms_sou_dirty_cmd *cmd = dirty->cmd;
867 	SVGASignedRect *blit = (SVGASignedRect *) &cmd[1];
868 
869 	/* Destination rect. */
870 	blit += dirty->num_hits;
871 	blit->left = dirty->unit_x1;
872 	blit->top = dirty->unit_y1;
873 	blit->right = dirty->unit_x2;
874 	blit->bottom = dirty->unit_y2;
875 
876 	/* Destination bounding box */
877 	sdirty->left = min_t(s32, sdirty->left, dirty->unit_x1);
878 	sdirty->top = min_t(s32, sdirty->top, dirty->unit_y1);
879 	sdirty->right = max_t(s32, sdirty->right, dirty->unit_x2);
880 	sdirty->bottom = max_t(s32, sdirty->bottom, dirty->unit_y2);
881 
882 	dirty->num_hits++;
883 }
884 
885 /**
886  * vmw_kms_sou_do_surface_dirty - Dirty part of a surface backed framebuffer
887  *
888  * @dev_priv: Pointer to the device private structure.
889  * @framebuffer: Pointer to the surface-buffer backed framebuffer.
890  * @clips: Array of clip rects. Either @clips or @vclips must be NULL.
891  * @vclips: Alternate array of clip rects. Either @clips or @vclips must
892  * be NULL.
893  * @srf: Pointer to surface to blit from. If NULL, the surface attached
894  * to @framebuffer will be used.
895  * @dest_x: X coordinate offset to align @srf with framebuffer coordinates.
896  * @dest_y: Y coordinate offset to align @srf with framebuffer coordinates.
897  * @num_clips: Number of clip rects in @clips.
898  * @inc: Increment to use when looping over @clips.
899  * @out_fence: If non-NULL, will return a ref-counted pointer to a
900  * struct vmw_fence_obj. The returned fence pointer may be NULL in which
901  * case the device has already synchronized.
902  *
903  * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
904  * interrupted.
905  */
906 int vmw_kms_sou_do_surface_dirty(struct vmw_private *dev_priv,
907 				 struct vmw_framebuffer *framebuffer,
908 				 struct drm_clip_rect *clips,
909 				 struct drm_vmw_rect *vclips,
910 				 struct vmw_resource *srf,
911 				 s32 dest_x,
912 				 s32 dest_y,
913 				 unsigned num_clips, int inc,
914 				 struct vmw_fence_obj **out_fence)
915 {
916 	struct vmw_framebuffer_surface *vfbs =
917 		container_of(framebuffer, typeof(*vfbs), base);
918 	struct vmw_kms_sou_surface_dirty sdirty;
919 	int ret;
920 
921 	if (!srf)
922 		srf = &vfbs->surface->res;
923 
924 	ret = vmw_kms_helper_resource_prepare(srf, true);
925 	if (ret)
926 		return ret;
927 
928 	sdirty.base.fifo_commit = vmw_sou_surface_fifo_commit;
929 	sdirty.base.clip = vmw_sou_surface_clip;
930 	sdirty.base.dev_priv = dev_priv;
931 	sdirty.base.fifo_reserve_size = sizeof(struct vmw_kms_sou_dirty_cmd) +
932 	  sizeof(SVGASignedRect) * num_clips;
933 
934 	sdirty.sid = srf->id;
935 	sdirty.left = sdirty.top = S32_MAX;
936 	sdirty.right = sdirty.bottom = S32_MIN;
937 	sdirty.dst_x = dest_x;
938 	sdirty.dst_y = dest_y;
939 
940 	ret = vmw_kms_helper_dirty(dev_priv, framebuffer, clips, vclips,
941 				   dest_x, dest_y, num_clips, inc,
942 				   &sdirty.base);
943 	vmw_kms_helper_resource_finish(srf, out_fence);
944 
945 	return ret;
946 }
947 
948 /**
949  * vmw_sou_dmabuf_fifo_commit - Callback to submit a set of readback clips.
950  *
951  * @dirty: The closure structure.
952  *
953  * Commits a previously built command buffer of readback clips.
954  */
955 static void vmw_sou_dmabuf_fifo_commit(struct vmw_kms_dirty *dirty)
956 {
957 	if (!dirty->num_hits) {
958 		vmw_fifo_commit(dirty->dev_priv, 0);
959 		return;
960 	}
961 
962 	vmw_fifo_commit(dirty->dev_priv,
963 			sizeof(struct vmw_kms_sou_dmabuf_blit) *
964 			dirty->num_hits);
965 }
966 
967 /**
968  * vmw_sou_dmabuf_clip - Callback to encode a readback cliprect.
969  *
970  * @dirty: The closure structure
971  *
972  * Encodes a BLIT_GMRFB_TO_SCREEN cliprect.
973  */
974 static void vmw_sou_dmabuf_clip(struct vmw_kms_dirty *dirty)
975 {
976 	struct vmw_kms_sou_dmabuf_blit *blit = dirty->cmd;
977 
978 	blit += dirty->num_hits;
979 	blit->header = SVGA_CMD_BLIT_GMRFB_TO_SCREEN;
980 	blit->body.destScreenId = dirty->unit->unit;
981 	blit->body.srcOrigin.x = dirty->fb_x;
982 	blit->body.srcOrigin.y = dirty->fb_y;
983 	blit->body.destRect.left = dirty->unit_x1;
984 	blit->body.destRect.top = dirty->unit_y1;
985 	blit->body.destRect.right = dirty->unit_x2;
986 	blit->body.destRect.bottom = dirty->unit_y2;
987 	dirty->num_hits++;
988 }
989 
990 /**
991  * vmw_kms_do_dmabuf_dirty - Dirty part of a dma-buffer backed framebuffer
992  *
993  * @dev_priv: Pointer to the device private structure.
994  * @framebuffer: Pointer to the dma-buffer backed framebuffer.
995  * @clips: Array of clip rects.
996  * @vclips: Alternate array of clip rects. Either @clips or @vclips must
997  * be NULL.
998  * @num_clips: Number of clip rects in @clips.
999  * @increment: Increment to use when looping over @clips.
1000  * @interruptible: Whether to perform waits interruptible if possible.
1001  * @out_fence: If non-NULL, will return a ref-counted pointer to a
1002  * struct vmw_fence_obj. The returned fence pointer may be NULL in which
1003  * case the device has already synchronized.
1004  *
1005  * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
1006  * interrupted.
1007  */
1008 int vmw_kms_sou_do_dmabuf_dirty(struct vmw_private *dev_priv,
1009 				struct vmw_framebuffer *framebuffer,
1010 				struct drm_clip_rect *clips,
1011 				struct drm_vmw_rect *vclips,
1012 				unsigned num_clips, int increment,
1013 				bool interruptible,
1014 				struct vmw_fence_obj **out_fence)
1015 {
1016 	struct vmw_dma_buffer *buf =
1017 		container_of(framebuffer, struct vmw_framebuffer_dmabuf,
1018 			     base)->buffer;
1019 	struct vmw_kms_dirty dirty;
1020 	int ret;
1021 
1022 	ret = vmw_kms_helper_buffer_prepare(dev_priv, buf, interruptible,
1023 					    false);
1024 	if (ret)
1025 		return ret;
1026 
1027 	ret = do_dmabuf_define_gmrfb(dev_priv, framebuffer);
1028 	if (unlikely(ret != 0))
1029 		goto out_revert;
1030 
1031 	dirty.fifo_commit = vmw_sou_dmabuf_fifo_commit;
1032 	dirty.clip = vmw_sou_dmabuf_clip;
1033 	dirty.fifo_reserve_size = sizeof(struct vmw_kms_sou_dmabuf_blit) *
1034 		num_clips;
1035 	ret = vmw_kms_helper_dirty(dev_priv, framebuffer, clips, vclips,
1036 				   0, 0, num_clips, increment, &dirty);
1037 	vmw_kms_helper_buffer_finish(dev_priv, NULL, buf, out_fence, NULL);
1038 
1039 	return ret;
1040 
1041 out_revert:
1042 	vmw_kms_helper_buffer_revert(buf);
1043 
1044 	return ret;
1045 }
1046 
1047 
1048 /**
1049  * vmw_sou_readback_fifo_commit - Callback to submit a set of readback clips.
1050  *
1051  * @dirty: The closure structure.
1052  *
1053  * Commits a previously built command buffer of readback clips.
1054  */
1055 static void vmw_sou_readback_fifo_commit(struct vmw_kms_dirty *dirty)
1056 {
1057 	if (!dirty->num_hits) {
1058 		vmw_fifo_commit(dirty->dev_priv, 0);
1059 		return;
1060 	}
1061 
1062 	vmw_fifo_commit(dirty->dev_priv,
1063 			sizeof(struct vmw_kms_sou_readback_blit) *
1064 			dirty->num_hits);
1065 }
1066 
1067 /**
1068  * vmw_sou_readback_clip - Callback to encode a readback cliprect.
1069  *
1070  * @dirty: The closure structure
1071  *
1072  * Encodes a BLIT_SCREEN_TO_GMRFB cliprect.
1073  */
1074 static void vmw_sou_readback_clip(struct vmw_kms_dirty *dirty)
1075 {
1076 	struct vmw_kms_sou_readback_blit *blit = dirty->cmd;
1077 
1078 	blit += dirty->num_hits;
1079 	blit->header = SVGA_CMD_BLIT_SCREEN_TO_GMRFB;
1080 	blit->body.srcScreenId = dirty->unit->unit;
1081 	blit->body.destOrigin.x = dirty->fb_x;
1082 	blit->body.destOrigin.y = dirty->fb_y;
1083 	blit->body.srcRect.left = dirty->unit_x1;
1084 	blit->body.srcRect.top = dirty->unit_y1;
1085 	blit->body.srcRect.right = dirty->unit_x2;
1086 	blit->body.srcRect.bottom = dirty->unit_y2;
1087 	dirty->num_hits++;
1088 }
1089 
1090 /**
1091  * vmw_kms_sou_readback - Perform a readback from the screen object system to
1092  * a dma-buffer backed framebuffer.
1093  *
1094  * @dev_priv: Pointer to the device private structure.
1095  * @file_priv: Pointer to a struct drm_file identifying the caller.
1096  * Must be set to NULL if @user_fence_rep is NULL.
1097  * @vfb: Pointer to the dma-buffer backed framebuffer.
1098  * @user_fence_rep: User-space provided structure for fence information.
1099  * Must be set to non-NULL if @file_priv is non-NULL.
1100  * @vclips: Array of clip rects.
1101  * @num_clips: Number of clip rects in @vclips.
1102  *
1103  * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
1104  * interrupted.
1105  */
1106 int vmw_kms_sou_readback(struct vmw_private *dev_priv,
1107 			 struct drm_file *file_priv,
1108 			 struct vmw_framebuffer *vfb,
1109 			 struct drm_vmw_fence_rep __user *user_fence_rep,
1110 			 struct drm_vmw_rect *vclips,
1111 			 uint32_t num_clips)
1112 {
1113 	struct vmw_dma_buffer *buf =
1114 		container_of(vfb, struct vmw_framebuffer_dmabuf, base)->buffer;
1115 	struct vmw_kms_dirty dirty;
1116 	int ret;
1117 
1118 	ret = vmw_kms_helper_buffer_prepare(dev_priv, buf, true, false);
1119 	if (ret)
1120 		return ret;
1121 
1122 	ret = do_dmabuf_define_gmrfb(dev_priv, vfb);
1123 	if (unlikely(ret != 0))
1124 		goto out_revert;
1125 
1126 	dirty.fifo_commit = vmw_sou_readback_fifo_commit;
1127 	dirty.clip = vmw_sou_readback_clip;
1128 	dirty.fifo_reserve_size = sizeof(struct vmw_kms_sou_readback_blit) *
1129 		num_clips;
1130 	ret = vmw_kms_helper_dirty(dev_priv, vfb, NULL, vclips,
1131 				   0, 0, num_clips, 1, &dirty);
1132 	vmw_kms_helper_buffer_finish(dev_priv, file_priv, buf, NULL,
1133 				     user_fence_rep);
1134 
1135 	return ret;
1136 
1137 out_revert:
1138 	vmw_kms_helper_buffer_revert(buf);
1139 
1140 	return ret;
1141 }
1142