xref: /linux/drivers/gpu/drm/vmwgfx/vmwgfx_kms.c (revision 5b0764a53235d3c77e1ed674e836e1b3fe1a4b1f)
1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /**************************************************************************
3  *
4  * Copyright (c) 2009-2025 Broadcom. All Rights Reserved. The term
5  * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
6  *
7  **************************************************************************/
8 
9 #include "vmwgfx_kms.h"
10 
11 #include "vmwgfx_bo.h"
12 #include "vmwgfx_resource_priv.h"
13 #include "vmwgfx_vkms.h"
14 #include "vmw_surface_cache.h"
15 
16 #include <drm/drm_atomic.h>
17 #include <drm/drm_atomic_helper.h>
18 #include <drm/drm_damage_helper.h>
19 #include <drm/drm_fourcc.h>
20 #include <drm/drm_rect.h>
21 #include <drm/drm_sysfs.h>
22 #include <drm/drm_edid.h>
23 #include <drm/drm_blend.h>
24 
25 void vmw_du_init(struct vmw_display_unit *du)
26 {
27 	vmw_vkms_crtc_init(&du->crtc);
28 	drm_plane_create_blend_mode_property(&du->primary,
29 					     BIT(DRM_MODE_BLEND_PIXEL_NONE) |
30 					     BIT(DRM_MODE_BLEND_PREMULTI));
31 	// Check that the cursor plane was actually initialized
32 	if (du->crtc.cursor == &du->cursor.base)
33 		drm_plane_create_blend_mode_property(&du->cursor.base,
34 						     BIT(DRM_MODE_BLEND_PREMULTI));
35 }
36 
37 void vmw_du_cleanup(struct vmw_display_unit *du)
38 {
39 	struct vmw_private *dev_priv = vmw_priv(du->primary.dev);
40 
41 	vmw_vkms_crtc_cleanup(&du->crtc);
42 	drm_plane_cleanup(&du->primary);
43 	if (vmw_cmd_supported(dev_priv))
44 		drm_plane_cleanup(&du->cursor.base);
45 
46 	drm_connector_unregister(&du->connector);
47 	drm_crtc_cleanup(&du->crtc);
48 	drm_encoder_cleanup(&du->encoder);
49 	drm_connector_cleanup(&du->connector);
50 }
51 
52 
53 void vmw_du_primary_plane_destroy(struct drm_plane *plane)
54 {
55 	drm_plane_cleanup(plane);
56 
57 	/* Planes are static in our case so we don't free it */
58 }
59 
60 
61 /**
62  * vmw_du_plane_unpin_surf - unpins resource associated with a framebuffer surface
63  *
64  * @vps: plane state associated with the display surface
65  */
66 void vmw_du_plane_unpin_surf(struct vmw_plane_state *vps)
67 {
68 	struct vmw_surface *surf = vmw_user_object_surface(&vps->uo);
69 
70 	if (surf) {
71 		if (vps->pinned) {
72 			vmw_resource_unpin(&surf->res);
73 			vps->pinned--;
74 		}
75 	}
76 }
77 
78 
79 /**
80  * vmw_du_plane_cleanup_fb - Unpins the plane surface
81  *
82  * @plane:  display plane
83  * @old_state: Contains the FB to clean up
84  *
85  * Unpins the framebuffer surface
86  *
87  * Returns 0 on success
88  */
89 void
90 vmw_du_plane_cleanup_fb(struct drm_plane *plane,
91 			struct drm_plane_state *old_state)
92 {
93 	struct vmw_plane_state *vps = vmw_plane_state_to_vps(old_state);
94 
95 	vmw_du_plane_unpin_surf(vps);
96 }
97 
98 
99 /**
100  * vmw_du_primary_plane_atomic_check - check if the new state is okay
101  *
102  * @plane: display plane
103  * @state: info on the new plane state, including the FB
104  *
105  * Check if the new state is settable given the current state.  Other
106  * than what the atomic helper checks, we care about crtc fitting
107  * the FB and maintaining one active framebuffer.
108  *
109  * Returns 0 on success
110  */
111 int vmw_du_primary_plane_atomic_check(struct drm_plane *plane,
112 				      struct drm_atomic_commit *state)
113 {
114 	struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state,
115 									   plane);
116 	struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state,
117 									   plane);
118 	struct drm_crtc_state *crtc_state = NULL;
119 	struct drm_framebuffer *new_fb = new_state->fb;
120 	struct drm_framebuffer *old_fb = old_state->fb;
121 	int ret;
122 
123 	/*
124 	 * Ignore damage clips if the framebuffer attached to the plane's state
125 	 * has changed since the last plane update (page-flip). In this case, a
126 	 * full plane update should happen because uploads are done per-buffer.
127 	 */
128 	if (old_fb != new_fb)
129 		new_state->ignore_damage_clips = true;
130 
131 	if (new_state->crtc)
132 		crtc_state = drm_atomic_get_new_crtc_state(state,
133 							   new_state->crtc);
134 
135 	ret = drm_atomic_helper_check_plane_state(new_state, crtc_state,
136 						  DRM_PLANE_NO_SCALING,
137 						  DRM_PLANE_NO_SCALING,
138 						  false, true);
139 	return ret;
140 }
141 
142 int vmw_du_crtc_atomic_check(struct drm_crtc *crtc,
143 			     struct drm_atomic_commit *state)
144 {
145 	struct vmw_private *vmw = vmw_priv(crtc->dev);
146 	struct drm_crtc_state *new_state = drm_atomic_get_new_crtc_state(state,
147 									 crtc);
148 	struct vmw_display_unit *du = vmw_crtc_to_du(new_state->crtc);
149 	int connector_mask = drm_connector_mask(&du->connector);
150 	bool has_primary = new_state->plane_mask &
151 			   drm_plane_mask(crtc->primary);
152 
153 	/*
154 	 * This is fine in general, but broken userspace might expect
155 	 * some actual rendering so give a clue as why it's blank.
156 	 */
157 	if (new_state->enable && !has_primary)
158 		drm_dbg_driver(&vmw->drm,
159 			       "CRTC without a primary plane will be blank.\n");
160 
161 
162 	if (new_state->connector_mask != connector_mask &&
163 	    new_state->connector_mask != 0) {
164 		DRM_ERROR("Invalid connectors configuration\n");
165 		return -EINVAL;
166 	}
167 
168 	/*
169 	 * Our virtual device does not have a dot clock, so use the logical
170 	 * clock value as the dot clock.
171 	 */
172 	if (new_state->mode.crtc_clock == 0)
173 		new_state->adjusted_mode.crtc_clock = new_state->mode.clock;
174 
175 	return 0;
176 }
177 
178 
179 void vmw_du_crtc_atomic_begin(struct drm_crtc *crtc,
180 			      struct drm_atomic_commit *state)
181 {
182 	vmw_vkms_crtc_atomic_begin(crtc, state);
183 }
184 
185 /**
186  * vmw_du_crtc_duplicate_state - duplicate crtc state
187  * @crtc: DRM crtc
188  *
189  * Allocates and returns a copy of the crtc state (both common and
190  * vmw-specific) for the specified crtc.
191  *
192  * Returns: The newly allocated crtc state, or NULL on failure.
193  */
194 struct drm_crtc_state *
195 vmw_du_crtc_duplicate_state(struct drm_crtc *crtc)
196 {
197 	struct drm_crtc_state *state;
198 	struct vmw_crtc_state *vcs;
199 
200 	if (WARN_ON(!crtc->state))
201 		return NULL;
202 
203 	vcs = kmemdup(crtc->state, sizeof(*vcs), GFP_KERNEL);
204 
205 	if (!vcs)
206 		return NULL;
207 
208 	state = &vcs->base;
209 
210 	__drm_atomic_helper_crtc_duplicate_state(crtc, state);
211 
212 	return state;
213 }
214 
215 
216 /**
217  * vmw_du_crtc_create_state - creates a blank vmw crtc state
218  * @crtc: DRM crtc
219  *
220  * Allocates a new empty state object for @crtc.
221  */
222 struct drm_crtc_state *vmw_du_crtc_create_state(struct drm_crtc *crtc)
223 {
224 	struct vmw_crtc_state *vcs;
225 
226 	vcs = kzalloc_obj(*vcs);
227 	if (!vcs) {
228 		DRM_ERROR("Cannot allocate vmw_crtc_state\n");
229 		return ERR_PTR(-ENOMEM);
230 	}
231 
232 	__drm_atomic_helper_crtc_state_init(&vcs->base, crtc);
233 
234 	return &vcs->base;
235 }
236 
237 
238 /**
239  * vmw_du_crtc_destroy_state - destroy crtc state
240  * @crtc: DRM crtc
241  * @state: state object to destroy
242  *
243  * Destroys the crtc state (both common and vmw-specific) for the
244  * specified plane.
245  */
246 void
247 vmw_du_crtc_destroy_state(struct drm_crtc *crtc,
248 			  struct drm_crtc_state *state)
249 {
250 	drm_atomic_helper_crtc_destroy_state(crtc, state);
251 }
252 
253 
254 /**
255  * vmw_du_plane_duplicate_state - duplicate plane state
256  * @plane: drm plane
257  *
258  * Allocates and returns a copy of the plane state (both common and
259  * vmw-specific) for the specified plane.
260  *
261  * Returns: The newly allocated plane state, or NULL on failure.
262  */
263 struct drm_plane_state *
264 vmw_du_plane_duplicate_state(struct drm_plane *plane)
265 {
266 	struct drm_plane_state *state;
267 	struct vmw_plane_state *vps;
268 
269 	vps = kmemdup(plane->state, sizeof(*vps), GFP_KERNEL);
270 
271 	if (!vps)
272 		return NULL;
273 
274 	vps->pinned = 0;
275 	vps->cpp = 0;
276 
277 	vps->cursor.mob = NULL;
278 
279 	/* Each ref counted resource needs to be acquired again */
280 	vmw_user_object_ref(&vps->uo);
281 	state = &vps->base;
282 
283 	__drm_atomic_helper_plane_duplicate_state(plane, state);
284 
285 	return state;
286 }
287 
288 
289 /**
290  * vmw_du_plane_create_state - creates a blank vmw plane state
291  * @plane: drm plane
292  *
293  * Allocates a new empty state object.
294  */
295 struct drm_plane_state *vmw_du_plane_create_state(struct drm_plane *plane)
296 {
297 	struct vmw_plane_state *vps;
298 
299 	vps = kzalloc_obj(*vps);
300 	if (!vps) {
301 		DRM_ERROR("Cannot allocate vmw_plane_state\n");
302 		return ERR_PTR(-ENOMEM);
303 	}
304 
305 	__drm_atomic_helper_plane_state_init(&vps->base, plane);
306 
307 	return &vps->base;
308 }
309 
310 
311 /**
312  * vmw_du_plane_destroy_state - destroy plane state
313  * @plane: DRM plane
314  * @state: state object to destroy
315  *
316  * Destroys the plane state (both common and vmw-specific) for the
317  * specified plane.
318  */
319 void
320 vmw_du_plane_destroy_state(struct drm_plane *plane,
321 			   struct drm_plane_state *state)
322 {
323 	struct vmw_plane_state *vps = vmw_plane_state_to_vps(state);
324 
325 	/* Should have been freed by cleanup_fb */
326 	vmw_user_object_unref(&vps->uo);
327 
328 	drm_atomic_helper_plane_destroy_state(plane, state);
329 }
330 
331 
332 /**
333  * vmw_du_connector_duplicate_state - duplicate connector state
334  * @connector: DRM connector
335  *
336  * Allocates and returns a copy of the connector state (both common and
337  * vmw-specific) for the specified connector.
338  *
339  * Returns: The newly allocated connector state, or NULL on failure.
340  */
341 struct drm_connector_state *
342 vmw_du_connector_duplicate_state(struct drm_connector *connector)
343 {
344 	struct drm_connector_state *state;
345 	struct vmw_connector_state *vcs;
346 
347 	if (WARN_ON(!connector->state))
348 		return NULL;
349 
350 	vcs = kmemdup(connector->state, sizeof(*vcs), GFP_KERNEL);
351 
352 	if (!vcs)
353 		return NULL;
354 
355 	state = &vcs->base;
356 
357 	__drm_atomic_helper_connector_duplicate_state(connector, state);
358 
359 	return state;
360 }
361 
362 
363 /**
364  * vmw_du_connector_reset - creates a blank vmw connector state
365  * @connector: DRM connector
366  *
367  * Resets the atomic state for @connector by freeing the state pointer (which
368  * might be NULL, e.g. at driver load time) and allocating a new empty state
369  * object.
370  */
371 void vmw_du_connector_reset(struct drm_connector *connector)
372 {
373 	struct vmw_connector_state *vcs;
374 
375 
376 	if (connector->state) {
377 		__drm_atomic_helper_connector_destroy_state(connector->state);
378 
379 		kfree(vmw_connector_state_to_vcs(connector->state));
380 	}
381 
382 	vcs = kzalloc_obj(*vcs);
383 
384 	if (!vcs) {
385 		DRM_ERROR("Cannot allocate vmw_connector_state\n");
386 		return;
387 	}
388 
389 	__drm_atomic_helper_connector_reset(connector, &vcs->base);
390 }
391 
392 
393 /**
394  * vmw_du_connector_destroy_state - destroy connector state
395  * @connector: DRM connector
396  * @state: state object to destroy
397  *
398  * Destroys the connector state (both common and vmw-specific) for the
399  * specified plane.
400  */
401 void
402 vmw_du_connector_destroy_state(struct drm_connector *connector,
403 			  struct drm_connector_state *state)
404 {
405 	drm_atomic_helper_connector_destroy_state(connector, state);
406 }
407 /*
408  * Generic framebuffer code
409  */
410 
411 /*
412  * Surface framebuffer code
413  */
414 
415 static void vmw_framebuffer_surface_destroy(struct drm_framebuffer *framebuffer)
416 {
417 	struct vmw_framebuffer_surface *vfbs =
418 		vmw_framebuffer_to_vfbs(framebuffer);
419 	struct vmw_bo *bo = vmw_user_object_buffer(&vfbs->uo);
420 	struct vmw_surface *surf = vmw_user_object_surface(&vfbs->uo);
421 
422 	if (bo) {
423 		vmw_bo_dirty_release(bo);
424 		/*
425 		 * bo->dirty is reference counted so it being NULL
426 		 * means that the surface wasn't coherent to begin
427 		 * with and so we have to free the dirty tracker
428 		 * in the vmw_resource
429 		 */
430 		if (!bo->dirty && surf && surf->res.dirty)
431 			surf->res.func->dirty_free(&surf->res);
432 	}
433 	drm_framebuffer_cleanup(framebuffer);
434 	vmw_user_object_unref(&vfbs->uo);
435 
436 	kfree(vfbs);
437 }
438 
439 /**
440  * vmw_kms_readback - Perform a readback from the screen system to
441  * a buffer-object backed framebuffer.
442  *
443  * @dev_priv: Pointer to the device private structure.
444  * @file_priv: Pointer to a struct drm_file identifying the caller.
445  * Must be set to NULL if @user_fence_rep is NULL.
446  * @vfb: Pointer to the buffer-object backed framebuffer.
447  * @user_fence_rep: User-space provided structure for fence information.
448  * Must be set to non-NULL if @file_priv is non-NULL.
449  * @vclips: Array of clip rects.
450  * @num_clips: Number of clip rects in @vclips.
451  *
452  * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
453  * interrupted.
454  */
455 int vmw_kms_readback(struct vmw_private *dev_priv,
456 		     struct drm_file *file_priv,
457 		     struct vmw_framebuffer *vfb,
458 		     struct drm_vmw_fence_rep __user *user_fence_rep,
459 		     struct drm_vmw_rect *vclips,
460 		     uint32_t num_clips)
461 {
462 	switch (dev_priv->active_display_unit) {
463 	case vmw_du_screen_object:
464 		return vmw_kms_sou_readback(dev_priv, file_priv, vfb,
465 					    user_fence_rep, vclips, num_clips,
466 					    NULL);
467 	case vmw_du_screen_target:
468 		return vmw_kms_stdu_readback(dev_priv, file_priv, vfb,
469 					     user_fence_rep, NULL, vclips, num_clips,
470 					     1, NULL);
471 	default:
472 		WARN_ONCE(true,
473 			  "Readback called with invalid display system.\n");
474 }
475 
476 	return -ENOSYS;
477 }
478 
479 static int vmw_framebuffer_surface_create_handle(struct drm_framebuffer *fb,
480 						 struct drm_file *file_priv,
481 						 unsigned int *handle)
482 {
483 	struct vmw_framebuffer_surface *vfbs = vmw_framebuffer_to_vfbs(fb);
484 	struct vmw_bo *bo = vmw_user_object_buffer(&vfbs->uo);
485 
486 	if (WARN_ON(!bo))
487 		return -EINVAL;
488 	return drm_gem_handle_create(file_priv, &bo->tbo.base, handle);
489 }
490 
491 static const struct drm_framebuffer_funcs vmw_framebuffer_surface_funcs = {
492 	.create_handle = vmw_framebuffer_surface_create_handle,
493 	.destroy = vmw_framebuffer_surface_destroy,
494 	.dirty = drm_atomic_helper_dirtyfb,
495 };
496 
497 static int vmw_kms_new_framebuffer_surface(struct vmw_private *dev_priv,
498 					   struct vmw_user_object *uo,
499 					   struct vmw_framebuffer **out,
500 					   const struct drm_format_info *info,
501 					   const struct drm_mode_fb_cmd2
502 					   *mode_cmd)
503 
504 {
505 	struct drm_device *dev = &dev_priv->drm;
506 	struct vmw_framebuffer_surface *vfbs;
507 	struct vmw_surface *surface;
508 	int ret;
509 
510 	/* 3D is only supported on HWv8 and newer hosts */
511 	if (dev_priv->active_display_unit == vmw_du_legacy)
512 		return -ENOSYS;
513 
514 	surface = vmw_user_object_surface(uo);
515 
516 	/*
517 	 * Sanity checks.
518 	 */
519 
520 	if (!drm_any_plane_has_format(&dev_priv->drm,
521 				      mode_cmd->pixel_format,
522 				      mode_cmd->modifier[0])) {
523 		drm_dbg(&dev_priv->drm,
524 			"unsupported pixel format %p4cc / modifier 0x%llx\n",
525 			&mode_cmd->pixel_format, mode_cmd->modifier[0]);
526 		return -EINVAL;
527 	}
528 
529 	/* Surface must be marked as a scanout. */
530 	if (unlikely(!surface->metadata.scanout))
531 		return -EINVAL;
532 
533 	if (unlikely(surface->metadata.mip_levels[0] != 1 ||
534 		     surface->metadata.num_sizes != 1 ||
535 		     surface->metadata.base_size.width < mode_cmd->width ||
536 		     surface->metadata.base_size.height < mode_cmd->height ||
537 		     surface->metadata.base_size.depth != 1)) {
538 		DRM_ERROR("Incompatible surface dimensions "
539 			  "for requested mode.\n");
540 		return -EINVAL;
541 	}
542 
543 	vfbs = kzalloc_obj(*vfbs);
544 	if (!vfbs) {
545 		ret = -ENOMEM;
546 		goto out_err1;
547 	}
548 
549 	drm_helper_mode_fill_fb_struct(dev, &vfbs->base.base, info, mode_cmd);
550 	memcpy(&vfbs->uo, uo, sizeof(vfbs->uo));
551 	vmw_user_object_ref(&vfbs->uo);
552 
553 	if (vfbs->uo.buffer)
554 		vfbs->base.base.obj[0] = &vfbs->uo.buffer->tbo.base;
555 
556 	*out = &vfbs->base;
557 
558 	ret = drm_framebuffer_init(dev, &vfbs->base.base,
559 				   &vmw_framebuffer_surface_funcs);
560 	if (ret)
561 		goto out_err2;
562 
563 	return 0;
564 
565 out_err2:
566 	vmw_user_object_unref(&vfbs->uo);
567 	kfree(vfbs);
568 out_err1:
569 	return ret;
570 }
571 
572 /*
573  * Buffer-object framebuffer code
574  */
575 
576 static int vmw_framebuffer_bo_create_handle(struct drm_framebuffer *fb,
577 					    struct drm_file *file_priv,
578 					    unsigned int *handle)
579 {
580 	struct vmw_framebuffer_bo *vfbd =
581 			vmw_framebuffer_to_vfbd(fb);
582 	return drm_gem_handle_create(file_priv, &vfbd->buffer->tbo.base, handle);
583 }
584 
585 static void vmw_framebuffer_bo_destroy(struct drm_framebuffer *framebuffer)
586 {
587 	struct vmw_framebuffer_bo *vfbd =
588 		vmw_framebuffer_to_vfbd(framebuffer);
589 
590 	vmw_bo_dirty_release(vfbd->buffer);
591 	drm_framebuffer_cleanup(framebuffer);
592 	vmw_bo_unreference(&vfbd->buffer);
593 
594 	kfree(vfbd);
595 }
596 
597 static const struct drm_framebuffer_funcs vmw_framebuffer_bo_funcs = {
598 	.create_handle = vmw_framebuffer_bo_create_handle,
599 	.destroy = vmw_framebuffer_bo_destroy,
600 	.dirty = drm_atomic_helper_dirtyfb,
601 };
602 
603 static int vmw_kms_new_framebuffer_bo(struct vmw_private *dev_priv,
604 				      struct vmw_bo *bo,
605 				      struct vmw_framebuffer **out,
606 				      const struct drm_format_info *info,
607 				      const struct drm_mode_fb_cmd2
608 				      *mode_cmd)
609 
610 {
611 	struct drm_device *dev = &dev_priv->drm;
612 	struct vmw_framebuffer_bo *vfbd;
613 	unsigned int requested_size;
614 	int ret;
615 
616 	requested_size = mode_cmd->height * mode_cmd->pitches[0];
617 	if (unlikely(requested_size > bo->tbo.base.size)) {
618 		DRM_ERROR("Screen buffer object size is too small "
619 			  "for requested mode.\n");
620 		return -EINVAL;
621 	}
622 
623 	if (!drm_any_plane_has_format(&dev_priv->drm,
624 				      mode_cmd->pixel_format,
625 				      mode_cmd->modifier[0])) {
626 		drm_dbg(&dev_priv->drm,
627 			"unsupported pixel format %p4cc / modifier 0x%llx\n",
628 			&mode_cmd->pixel_format, mode_cmd->modifier[0]);
629 		return -EINVAL;
630 	}
631 
632 	vfbd = kzalloc_obj(*vfbd);
633 	if (!vfbd) {
634 		ret = -ENOMEM;
635 		goto out_err1;
636 	}
637 
638 	vfbd->base.base.obj[0] = &bo->tbo.base;
639 	drm_helper_mode_fill_fb_struct(dev, &vfbd->base.base, info, mode_cmd);
640 	vfbd->base.bo = true;
641 	vfbd->buffer = vmw_bo_reference(bo);
642 	*out = &vfbd->base;
643 
644 	ret = drm_framebuffer_init(dev, &vfbd->base.base,
645 				   &vmw_framebuffer_bo_funcs);
646 	if (ret)
647 		goto out_err2;
648 
649 	return 0;
650 
651 out_err2:
652 	vmw_bo_unreference(&bo);
653 	kfree(vfbd);
654 out_err1:
655 	return ret;
656 }
657 
658 
659 /**
660  * vmw_kms_srf_ok - check if a surface can be created
661  *
662  * @dev_priv: Pointer to device private struct.
663  * @width: requested width
664  * @height: requested height
665  *
666  * Surfaces need to be less than texture size
667  */
668 static bool
669 vmw_kms_srf_ok(struct vmw_private *dev_priv, uint32_t width, uint32_t height)
670 {
671 	if (width  > dev_priv->texture_max_width ||
672 	    height > dev_priv->texture_max_height)
673 		return false;
674 
675 	return true;
676 }
677 
678 /**
679  * vmw_kms_new_framebuffer - Create a new framebuffer.
680  *
681  * @dev_priv: Pointer to device private struct.
682  * @uo: Pointer to user object to wrap the kms framebuffer around.
683  * Either the buffer or surface inside the user object must be NULL.
684  * @info: pixel format information.
685  * @mode_cmd: Frame-buffer metadata.
686  */
687 struct vmw_framebuffer *
688 vmw_kms_new_framebuffer(struct vmw_private *dev_priv,
689 			struct vmw_user_object *uo,
690 			const struct drm_format_info *info,
691 			const struct drm_mode_fb_cmd2 *mode_cmd)
692 {
693 	struct vmw_framebuffer *vfb = NULL;
694 	int ret;
695 
696 	/* Create the new framebuffer depending one what we have */
697 	if (vmw_user_object_surface(uo)) {
698 		ret = vmw_kms_new_framebuffer_surface(dev_priv, uo, &vfb,
699 						      info, mode_cmd);
700 	} else if (uo->buffer) {
701 		ret = vmw_kms_new_framebuffer_bo(dev_priv, uo->buffer, &vfb,
702 						 info, mode_cmd);
703 	} else {
704 		BUG();
705 	}
706 
707 	if (ret)
708 		return ERR_PTR(ret);
709 
710 	return vfb;
711 }
712 
713 /*
714  * Generic Kernel modesetting functions
715  */
716 
717 static struct drm_framebuffer *vmw_kms_fb_create(struct drm_device *dev,
718 						 struct drm_file *file_priv,
719 						 const struct drm_format_info *info,
720 						 const struct drm_mode_fb_cmd2 *mode_cmd)
721 {
722 	struct vmw_private *dev_priv = vmw_priv(dev);
723 	struct vmw_framebuffer *vfb = NULL;
724 	struct vmw_user_object uo = {0};
725 	struct vmw_bo *bo;
726 	struct vmw_surface *surface;
727 	int ret;
728 
729 	/* returns either a bo or surface */
730 	ret = vmw_user_object_lookup(dev_priv, file_priv, mode_cmd->handles[0],
731 				     &uo);
732 	if (ret) {
733 		DRM_ERROR("Invalid buffer object handle %u (0x%x).\n",
734 			  mode_cmd->handles[0], mode_cmd->handles[0]);
735 		goto err_out;
736 	}
737 
738 
739 	if (vmw_user_object_surface(&uo) &&
740 	    !vmw_kms_srf_ok(dev_priv, mode_cmd->width, mode_cmd->height)) {
741 		DRM_ERROR("Surface size cannot exceed %dx%d\n",
742 			dev_priv->texture_max_width,
743 			dev_priv->texture_max_height);
744 		ret = -EINVAL;
745 		goto err_out;
746 	}
747 
748 
749 	vfb = vmw_kms_new_framebuffer(dev_priv, &uo, info, mode_cmd);
750 	if (IS_ERR(vfb)) {
751 		ret = PTR_ERR(vfb);
752 		goto err_out;
753 	}
754 
755 err_out:
756 	bo = vmw_user_object_buffer(&uo);
757 	surface = vmw_user_object_surface(&uo);
758 	/* vmw_user_object_lookup takes one ref so does new_fb */
759 	vmw_user_object_unref(&uo);
760 
761 	if (ret) {
762 		DRM_ERROR("failed to create vmw_framebuffer: %i\n", ret);
763 		return ERR_PTR(ret);
764 	}
765 
766 	if (bo) {
767 		ttm_bo_reserve(&bo->tbo, false, false, NULL);
768 		ret = vmw_bo_dirty_add(bo);
769 		if (!ret && surface && surface->res.func->dirty_alloc) {
770 			surface->res.coherent = true;
771 			if (surface->res.dirty == NULL)
772 				ret = surface->res.func->dirty_alloc(&surface->res);
773 		}
774 		ttm_bo_unreserve(&bo->tbo);
775 	}
776 
777 	return &vfb->base;
778 }
779 
780 /**
781  * vmw_kms_check_display_memory - Validates display memory required for a
782  * topology
783  * @dev: DRM device
784  * @num_rects: number of drm_rect in rects
785  * @rects: array of drm_rect representing the topology to validate indexed by
786  * crtc index.
787  *
788  * Returns:
789  * 0 on success otherwise negative error code
790  */
791 static int vmw_kms_check_display_memory(struct drm_device *dev,
792 					uint32_t num_rects,
793 					struct drm_rect *rects)
794 {
795 	struct vmw_private *dev_priv = vmw_priv(dev);
796 	struct drm_rect bounding_box = {0};
797 	u64 total_pixels = 0, pixel_mem, bb_mem;
798 	int i;
799 
800 	for (i = 0; i < num_rects; i++) {
801 		/*
802 		 * For STDU only individual screen (screen target) is limited by
803 		 * SCREENTARGET_MAX_WIDTH/HEIGHT registers.
804 		 */
805 		if (dev_priv->active_display_unit == vmw_du_screen_target &&
806 		    (drm_rect_width(&rects[i]) > dev_priv->stdu_max_width ||
807 		     drm_rect_height(&rects[i]) > dev_priv->stdu_max_height)) {
808 			VMW_DEBUG_KMS("Screen size not supported.\n");
809 			return -EINVAL;
810 		}
811 
812 		/* Bounding box upper left is at (0,0). */
813 		if (rects[i].x2 > bounding_box.x2)
814 			bounding_box.x2 = rects[i].x2;
815 
816 		if (rects[i].y2 > bounding_box.y2)
817 			bounding_box.y2 = rects[i].y2;
818 
819 		total_pixels += (u64) drm_rect_width(&rects[i]) *
820 			(u64) drm_rect_height(&rects[i]);
821 	}
822 
823 	/* Virtual svga device primary limits are always in 32-bpp. */
824 	pixel_mem = total_pixels * 4;
825 
826 	/*
827 	 * For HV10 and below prim_bb_mem is vram size. When
828 	 * SVGA_REG_MAX_PRIMARY_BOUNDING_BOX_MEM is not present vram size is
829 	 * limit on primary bounding box
830 	 */
831 	if (pixel_mem > dev_priv->max_primary_mem) {
832 		VMW_DEBUG_KMS("Combined output size too large.\n");
833 		return -EINVAL;
834 	}
835 
836 	/* SVGA_CAP_NO_BB_RESTRICTION is available for STDU only. */
837 	if (dev_priv->active_display_unit != vmw_du_screen_target ||
838 	    !(dev_priv->capabilities & SVGA_CAP_NO_BB_RESTRICTION)) {
839 		bb_mem = (u64) bounding_box.x2 * bounding_box.y2 * 4;
840 
841 		if (bb_mem > dev_priv->max_primary_mem) {
842 			VMW_DEBUG_KMS("Topology is beyond supported limits.\n");
843 			return -EINVAL;
844 		}
845 	}
846 
847 	return 0;
848 }
849 
850 /**
851  * vmw_crtc_state_and_lock - Return new or current crtc state with locked
852  * crtc mutex
853  * @state: The atomic state pointer containing the new atomic state
854  * @crtc: The crtc
855  *
856  * This function returns the new crtc state if it's part of the state update.
857  * Otherwise returns the current crtc state. It also makes sure that the
858  * crtc mutex is locked.
859  *
860  * Returns: A valid crtc state pointer or NULL. It may also return a
861  * pointer error, in particular -EDEADLK if locking needs to be rerun.
862  */
863 static struct drm_crtc_state *
864 vmw_crtc_state_and_lock(struct drm_atomic_commit *state, struct drm_crtc *crtc)
865 {
866 	struct drm_crtc_state *crtc_state;
867 
868 	crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
869 	if (crtc_state) {
870 		lockdep_assert_held(&crtc->mutex.mutex.base);
871 	} else {
872 		int ret = drm_modeset_lock(&crtc->mutex, state->acquire_ctx);
873 
874 		if (ret != 0 && ret != -EALREADY)
875 			return ERR_PTR(ret);
876 
877 		crtc_state = crtc->state;
878 	}
879 
880 	return crtc_state;
881 }
882 
883 /**
884  * vmw_kms_check_implicit - Verify that all implicit display units scan out
885  * from the same fb after the new state is committed.
886  * @dev: The drm_device.
887  * @state: The new state to be checked.
888  *
889  * Returns:
890  *   Zero on success,
891  *   -EINVAL on invalid state,
892  *   -EDEADLK if modeset locking needs to be rerun.
893  */
894 static int vmw_kms_check_implicit(struct drm_device *dev,
895 				  struct drm_atomic_commit *state)
896 {
897 	struct drm_framebuffer *implicit_fb = NULL;
898 	struct drm_crtc *crtc;
899 	struct drm_crtc_state *crtc_state;
900 	struct drm_plane_state *plane_state;
901 
902 	drm_for_each_crtc(crtc, dev) {
903 		struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
904 
905 		if (!du->is_implicit)
906 			continue;
907 
908 		crtc_state = vmw_crtc_state_and_lock(state, crtc);
909 		if (IS_ERR(crtc_state))
910 			return PTR_ERR(crtc_state);
911 
912 		if (!crtc_state || !crtc_state->enable)
913 			continue;
914 
915 		/*
916 		 * Can't move primary planes across crtcs, so this is OK.
917 		 * It also means we don't need to take the plane mutex.
918 		 */
919 		plane_state = du->primary.state;
920 		if (plane_state->crtc != crtc)
921 			continue;
922 
923 		if (!implicit_fb)
924 			implicit_fb = plane_state->fb;
925 		else if (implicit_fb != plane_state->fb)
926 			return -EINVAL;
927 	}
928 
929 	return 0;
930 }
931 
932 /**
933  * vmw_kms_check_topology - Validates topology in drm_atomic_commit
934  * @dev: DRM device
935  * @state: the driver state object
936  *
937  * Returns:
938  * 0 on success otherwise negative error code
939  */
940 static int vmw_kms_check_topology(struct drm_device *dev,
941 				  struct drm_atomic_commit *state)
942 {
943 	struct drm_crtc_state *old_crtc_state, *new_crtc_state;
944 	struct drm_rect *rects;
945 	struct drm_crtc *crtc;
946 	uint32_t i;
947 	int ret = 0;
948 
949 	rects = kzalloc_objs(struct drm_rect, dev->mode_config.num_crtc);
950 	if (!rects)
951 		return -ENOMEM;
952 
953 	drm_for_each_crtc(crtc, dev) {
954 		struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
955 		struct drm_crtc_state *crtc_state;
956 
957 		i = drm_crtc_index(crtc);
958 
959 		crtc_state = vmw_crtc_state_and_lock(state, crtc);
960 		if (IS_ERR(crtc_state)) {
961 			ret = PTR_ERR(crtc_state);
962 			goto clean;
963 		}
964 
965 		if (!crtc_state)
966 			continue;
967 
968 		if (crtc_state->enable) {
969 			rects[i].x1 = du->gui_x;
970 			rects[i].y1 = du->gui_y;
971 			rects[i].x2 = du->gui_x + crtc_state->mode.hdisplay;
972 			rects[i].y2 = du->gui_y + crtc_state->mode.vdisplay;
973 		} else {
974 			rects[i].x1 = 0;
975 			rects[i].y1 = 0;
976 			rects[i].x2 = 0;
977 			rects[i].y2 = 0;
978 		}
979 	}
980 
981 	/* Determine change to topology due to new atomic state */
982 	for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state,
983 				      new_crtc_state, i) {
984 		struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
985 		struct drm_connector *connector;
986 		struct drm_connector_state *conn_state;
987 		struct vmw_connector_state *vmw_conn_state;
988 
989 		if (!du->pref_active && new_crtc_state->enable) {
990 			VMW_DEBUG_KMS("Enabling a disabled display unit\n");
991 			ret = -EINVAL;
992 			goto clean;
993 		}
994 
995 		/*
996 		 * For vmwgfx each crtc has only one connector attached and it
997 		 * is not changed so don't really need to check the
998 		 * crtc->connector_mask and iterate over it.
999 		 */
1000 		connector = &du->connector;
1001 		conn_state = drm_atomic_get_connector_state(state, connector);
1002 		if (IS_ERR(conn_state)) {
1003 			ret = PTR_ERR(conn_state);
1004 			goto clean;
1005 		}
1006 
1007 		vmw_conn_state = vmw_connector_state_to_vcs(conn_state);
1008 		vmw_conn_state->gui_x = du->gui_x;
1009 		vmw_conn_state->gui_y = du->gui_y;
1010 	}
1011 
1012 	ret = vmw_kms_check_display_memory(dev, dev->mode_config.num_crtc,
1013 					   rects);
1014 
1015 clean:
1016 	kfree(rects);
1017 	return ret;
1018 }
1019 
1020 /**
1021  * vmw_kms_atomic_check_modeset- validate state object for modeset changes
1022  *
1023  * @dev: DRM device
1024  * @state: the driver state object
1025  *
1026  * This is a simple wrapper around drm_atomic_helper_check_modeset() for
1027  * us to assign a value to mode->crtc_clock so that
1028  * drm_calc_timestamping_constants() won't throw an error message
1029  *
1030  * Returns:
1031  * Zero for success or -errno
1032  */
1033 static int
1034 vmw_kms_atomic_check_modeset(struct drm_device *dev,
1035 			     struct drm_atomic_commit *state)
1036 {
1037 	struct drm_crtc *crtc;
1038 	struct drm_crtc_state *crtc_state;
1039 	bool need_modeset = false;
1040 	int i, ret;
1041 
1042 	ret = drm_atomic_helper_check(dev, state);
1043 	if (ret)
1044 		return ret;
1045 
1046 	ret = vmw_kms_check_implicit(dev, state);
1047 	if (ret) {
1048 		VMW_DEBUG_KMS("Invalid implicit state\n");
1049 		return ret;
1050 	}
1051 
1052 	for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
1053 		if (drm_atomic_crtc_needs_modeset(crtc_state))
1054 			need_modeset = true;
1055 	}
1056 
1057 	if (need_modeset)
1058 		return vmw_kms_check_topology(dev, state);
1059 
1060 	return ret;
1061 }
1062 
1063 static const struct drm_mode_config_funcs vmw_kms_funcs = {
1064 	.fb_create = vmw_kms_fb_create,
1065 	.atomic_check = vmw_kms_atomic_check_modeset,
1066 	.atomic_commit = drm_atomic_helper_commit,
1067 };
1068 
1069 static int vmw_kms_generic_present(struct vmw_private *dev_priv,
1070 				   struct drm_file *file_priv,
1071 				   struct vmw_framebuffer *vfb,
1072 				   struct vmw_surface *surface,
1073 				   uint32_t sid,
1074 				   int32_t destX, int32_t destY,
1075 				   struct drm_vmw_rect *clips,
1076 				   uint32_t num_clips)
1077 {
1078 	return vmw_kms_sou_do_surface_dirty(dev_priv, vfb, NULL, clips,
1079 					    &surface->res, destX, destY,
1080 					    num_clips, 1, NULL, NULL);
1081 }
1082 
1083 
1084 int vmw_kms_present(struct vmw_private *dev_priv,
1085 		    struct drm_file *file_priv,
1086 		    struct vmw_framebuffer *vfb,
1087 		    struct vmw_surface *surface,
1088 		    uint32_t sid,
1089 		    int32_t destX, int32_t destY,
1090 		    struct drm_vmw_rect *clips,
1091 		    uint32_t num_clips)
1092 {
1093 	int ret;
1094 
1095 	switch (dev_priv->active_display_unit) {
1096 	case vmw_du_screen_target:
1097 		ret = vmw_kms_stdu_surface_dirty(dev_priv, vfb, NULL, clips,
1098 						 &surface->res, destX, destY,
1099 						 num_clips, 1, NULL, NULL);
1100 		break;
1101 	case vmw_du_screen_object:
1102 		ret = vmw_kms_generic_present(dev_priv, file_priv, vfb, surface,
1103 					      sid, destX, destY, clips,
1104 					      num_clips);
1105 		break;
1106 	default:
1107 		WARN_ONCE(true,
1108 			  "Present called with invalid display system.\n");
1109 		ret = -ENOSYS;
1110 		break;
1111 	}
1112 	if (ret)
1113 		return ret;
1114 
1115 	vmw_cmd_flush(dev_priv, false);
1116 
1117 	return 0;
1118 }
1119 
1120 static void
1121 vmw_kms_create_hotplug_mode_update_property(struct vmw_private *dev_priv)
1122 {
1123 	if (dev_priv->hotplug_mode_update_property)
1124 		return;
1125 
1126 	dev_priv->hotplug_mode_update_property =
1127 		drm_property_create_range(&dev_priv->drm,
1128 					  DRM_MODE_PROP_IMMUTABLE,
1129 					  "hotplug_mode_update", 0, 1);
1130 }
1131 
1132 static void
1133 vmw_atomic_commit_tail(struct drm_atomic_commit *old_state)
1134 {
1135 	struct vmw_private *vmw = vmw_priv(old_state->dev);
1136 	struct drm_crtc *crtc;
1137 	struct drm_crtc_state *old_crtc_state;
1138 	int i;
1139 
1140 	drm_atomic_helper_commit_tail(old_state);
1141 
1142 	if (vmw->vkms_enabled) {
1143 		for_each_old_crtc_in_state(old_state, crtc, old_crtc_state, i) {
1144 			struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
1145 			(void)old_crtc_state;
1146 			flush_work(&du->vkms.crc_generator_work);
1147 		}
1148 	}
1149 }
1150 
1151 static const struct drm_mode_config_helper_funcs vmw_mode_config_helpers = {
1152 	.atomic_commit_tail = vmw_atomic_commit_tail,
1153 };
1154 
1155 int vmw_kms_init(struct vmw_private *dev_priv)
1156 {
1157 	struct drm_device *dev = &dev_priv->drm;
1158 	int ret;
1159 	static const char *display_unit_names[] = {
1160 		"Invalid",
1161 		"Legacy",
1162 		"Screen Object",
1163 		"Screen Target",
1164 		"Invalid (max)"
1165 	};
1166 
1167 	drm_mode_config_init(dev);
1168 	dev->mode_config.funcs = &vmw_kms_funcs;
1169 	dev->mode_config.min_width = 1;
1170 	dev->mode_config.min_height = 1;
1171 	dev->mode_config.max_width = dev_priv->texture_max_width;
1172 	dev->mode_config.max_height = dev_priv->texture_max_height;
1173 	dev->mode_config.preferred_depth = dev_priv->assume_16bpp ? 16 : 32;
1174 	dev->mode_config.helper_private = &vmw_mode_config_helpers;
1175 
1176 	drm_mode_create_suggested_offset_properties(dev);
1177 	vmw_kms_create_hotplug_mode_update_property(dev_priv);
1178 
1179 	ret = vmw_kms_stdu_init_display(dev_priv);
1180 	if (ret) {
1181 		ret = vmw_kms_sou_init_display(dev_priv);
1182 		if (ret) /* Fallback */
1183 			ret = vmw_kms_ldu_init_display(dev_priv);
1184 	}
1185 	BUILD_BUG_ON(ARRAY_SIZE(display_unit_names) != (vmw_du_max + 1));
1186 	drm_info(&dev_priv->drm, "%s display unit initialized\n",
1187 		 display_unit_names[dev_priv->active_display_unit]);
1188 
1189 	return ret;
1190 }
1191 
1192 int vmw_kms_close(struct vmw_private *dev_priv)
1193 {
1194 	int ret = 0;
1195 
1196 	/*
1197 	 * Docs says we should take the lock before calling this function
1198 	 * but since it destroys encoders and our destructor calls
1199 	 * drm_encoder_cleanup which takes the lock we deadlock.
1200 	 */
1201 	drm_mode_config_cleanup(&dev_priv->drm);
1202 	if (dev_priv->active_display_unit == vmw_du_legacy)
1203 		ret = vmw_kms_ldu_close_display(dev_priv);
1204 
1205 	return ret;
1206 }
1207 
1208 int vmw_kms_write_svga(struct vmw_private *vmw_priv,
1209 			unsigned width, unsigned height, unsigned pitch,
1210 			unsigned bpp, unsigned depth)
1211 {
1212 	if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1213 		vmw_write(vmw_priv, SVGA_REG_PITCHLOCK, pitch);
1214 	else if (vmw_fifo_have_pitchlock(vmw_priv))
1215 		vmw_fifo_mem_write(vmw_priv, SVGA_FIFO_PITCHLOCK, pitch);
1216 	vmw_write(vmw_priv, SVGA_REG_WIDTH, width);
1217 	vmw_write(vmw_priv, SVGA_REG_HEIGHT, height);
1218 	if ((vmw_priv->capabilities & SVGA_CAP_8BIT_EMULATION) != 0)
1219 		vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, bpp);
1220 
1221 	if (vmw_read(vmw_priv, SVGA_REG_DEPTH) != depth) {
1222 		DRM_ERROR("Invalid depth %u for %u bpp, host expects %u\n",
1223 			  depth, bpp, vmw_read(vmw_priv, SVGA_REG_DEPTH));
1224 		return -EINVAL;
1225 	}
1226 
1227 	return 0;
1228 }
1229 
1230 static
1231 bool vmw_kms_validate_mode_vram(struct vmw_private *dev_priv,
1232 				u64 pitch,
1233 				u64 height)
1234 {
1235 	return (pitch * height) < (u64)dev_priv->vram_size;
1236 }
1237 
1238 /**
1239  * vmw_du_update_layout - Update the display unit with topology from resolution
1240  * plugin and generate DRM uevent
1241  * @dev_priv: device private
1242  * @num_rects: number of drm_rect in rects
1243  * @rects: toplogy to update
1244  */
1245 static int vmw_du_update_layout(struct vmw_private *dev_priv,
1246 				unsigned int num_rects, struct drm_rect *rects)
1247 {
1248 	struct drm_device *dev = &dev_priv->drm;
1249 	struct vmw_display_unit *du;
1250 	struct drm_connector *con;
1251 	struct drm_connector_list_iter conn_iter;
1252 	struct drm_modeset_acquire_ctx ctx;
1253 	struct drm_crtc *crtc;
1254 	int ret;
1255 
1256 	/* Currently gui_x/y is protected with the crtc mutex */
1257 	mutex_lock(&dev->mode_config.mutex);
1258 	drm_modeset_acquire_init(&ctx, 0);
1259 retry:
1260 	drm_for_each_crtc(crtc, dev) {
1261 		ret = drm_modeset_lock(&crtc->mutex, &ctx);
1262 		if (ret < 0) {
1263 			if (ret == -EDEADLK) {
1264 				drm_modeset_backoff(&ctx);
1265 				goto retry;
1266 		}
1267 			goto out_fini;
1268 		}
1269 	}
1270 
1271 	drm_connector_list_iter_begin(dev, &conn_iter);
1272 	drm_for_each_connector_iter(con, &conn_iter) {
1273 		du = vmw_connector_to_du(con);
1274 		if (num_rects > du->unit) {
1275 			du->pref_width = drm_rect_width(&rects[du->unit]);
1276 			du->pref_height = drm_rect_height(&rects[du->unit]);
1277 			du->pref_active = true;
1278 			du->gui_x = rects[du->unit].x1;
1279 			du->gui_y = rects[du->unit].y1;
1280 		} else {
1281 			du->pref_width  = VMWGFX_MIN_INITIAL_WIDTH;
1282 			du->pref_height = VMWGFX_MIN_INITIAL_HEIGHT;
1283 			du->pref_active = false;
1284 			du->gui_x = 0;
1285 			du->gui_y = 0;
1286 		}
1287 	}
1288 	drm_connector_list_iter_end(&conn_iter);
1289 
1290 	list_for_each_entry(con, &dev->mode_config.connector_list, head) {
1291 		du = vmw_connector_to_du(con);
1292 		if (num_rects > du->unit) {
1293 			drm_object_property_set_value
1294 			  (&con->base, dev->mode_config.suggested_x_property,
1295 			   du->gui_x);
1296 			drm_object_property_set_value
1297 			  (&con->base, dev->mode_config.suggested_y_property,
1298 			   du->gui_y);
1299 		} else {
1300 			drm_object_property_set_value
1301 			  (&con->base, dev->mode_config.suggested_x_property,
1302 			   0);
1303 			drm_object_property_set_value
1304 			  (&con->base, dev->mode_config.suggested_y_property,
1305 			   0);
1306 		}
1307 		con->status = vmw_du_connector_detect(con, true);
1308 	}
1309 out_fini:
1310 	drm_modeset_drop_locks(&ctx);
1311 	drm_modeset_acquire_fini(&ctx);
1312 	mutex_unlock(&dev->mode_config.mutex);
1313 
1314 	drm_sysfs_hotplug_event(dev);
1315 
1316 	return 0;
1317 }
1318 
1319 int vmw_du_crtc_gamma_set(struct drm_crtc *crtc,
1320 			  u16 *r, u16 *g, u16 *b,
1321 			  uint32_t size,
1322 			  struct drm_modeset_acquire_ctx *ctx)
1323 {
1324 	struct vmw_private *dev_priv = vmw_priv(crtc->dev);
1325 	int i;
1326 
1327 	for (i = 0; i < size; i++) {
1328 		DRM_DEBUG("%d r/g/b = 0x%04x / 0x%04x / 0x%04x\n", i,
1329 			  r[i], g[i], b[i]);
1330 		vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 0, r[i] >> 8);
1331 		vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 1, g[i] >> 8);
1332 		vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 2, b[i] >> 8);
1333 	}
1334 
1335 	return 0;
1336 }
1337 
1338 int vmw_du_connector_dpms(struct drm_connector *connector, int mode)
1339 {
1340 	return 0;
1341 }
1342 
1343 enum drm_connector_status
1344 vmw_du_connector_detect(struct drm_connector *connector, bool force)
1345 {
1346 	uint32_t num_displays;
1347 	struct drm_device *dev = connector->dev;
1348 	struct vmw_private *dev_priv = vmw_priv(dev);
1349 	struct vmw_display_unit *du = vmw_connector_to_du(connector);
1350 
1351 	num_displays = vmw_read(dev_priv, SVGA_REG_NUM_DISPLAYS);
1352 
1353 	return ((vmw_connector_to_du(connector)->unit < num_displays &&
1354 		 du->pref_active) ?
1355 		connector_status_connected : connector_status_disconnected);
1356 }
1357 
1358 /**
1359  * vmw_guess_mode_timing - Provide fake timings for a
1360  * 60Hz vrefresh mode.
1361  *
1362  * @mode: Pointer to a struct drm_display_mode with hdisplay and vdisplay
1363  * members filled in.
1364  */
1365 void vmw_guess_mode_timing(struct drm_display_mode *mode)
1366 {
1367 	mode->hsync_start = mode->hdisplay + 50;
1368 	mode->hsync_end = mode->hsync_start + 50;
1369 	mode->htotal = mode->hsync_end + 50;
1370 
1371 	mode->vsync_start = mode->vdisplay + 50;
1372 	mode->vsync_end = mode->vsync_start + 50;
1373 	mode->vtotal = mode->vsync_end + 50;
1374 
1375 	mode->clock = (u32)mode->htotal * (u32)mode->vtotal / 100 * 6;
1376 }
1377 
1378 
1379 /**
1380  * vmw_kms_update_layout_ioctl - Handler for DRM_VMW_UPDATE_LAYOUT ioctl
1381  * @dev: drm device for the ioctl
1382  * @data: data pointer for the ioctl
1383  * @file_priv: drm file for the ioctl call
1384  *
1385  * Update preferred topology of display unit as per ioctl request. The topology
1386  * is expressed as array of drm_vmw_rect.
1387  * e.g.
1388  * [0 0 640 480] [640 0 800 600] [0 480 640 480]
1389  *
1390  * NOTE:
1391  * The x and y offset (upper left) in drm_vmw_rect cannot be less than 0. Beside
1392  * device limit on topology, x + w and y + h (lower right) cannot be greater
1393  * than INT_MAX. So topology beyond these limits will return with error.
1394  *
1395  * Returns:
1396  * Zero on success, negative errno on failure.
1397  */
1398 int vmw_kms_update_layout_ioctl(struct drm_device *dev, void *data,
1399 				struct drm_file *file_priv)
1400 {
1401 	struct vmw_private *dev_priv = vmw_priv(dev);
1402 	struct drm_mode_config *mode_config = &dev->mode_config;
1403 	struct drm_vmw_update_layout_arg *arg =
1404 		(struct drm_vmw_update_layout_arg *)data;
1405 	const void __user *user_rects;
1406 	struct drm_vmw_rect *rects;
1407 	struct drm_rect *drm_rects;
1408 	unsigned rects_size;
1409 	int ret, i;
1410 
1411 	if (!arg->num_outputs) {
1412 		struct drm_rect def_rect = {0, 0,
1413 					    VMWGFX_MIN_INITIAL_WIDTH,
1414 					    VMWGFX_MIN_INITIAL_HEIGHT};
1415 		vmw_du_update_layout(dev_priv, 1, &def_rect);
1416 		return 0;
1417 	} else if (arg->num_outputs > VMWGFX_NUM_DISPLAY_UNITS) {
1418 		return -E2BIG;
1419 	}
1420 
1421 	rects_size = arg->num_outputs * sizeof(struct drm_vmw_rect);
1422 	rects = kzalloc_objs(struct drm_vmw_rect, arg->num_outputs);
1423 	if (unlikely(!rects))
1424 		return -ENOMEM;
1425 
1426 	user_rects = (void __user *)(unsigned long)arg->rects;
1427 	ret = copy_from_user(rects, user_rects, rects_size);
1428 	if (unlikely(ret != 0)) {
1429 		DRM_ERROR("Failed to get rects.\n");
1430 		ret = -EFAULT;
1431 		goto out_free;
1432 	}
1433 
1434 	drm_rects = (struct drm_rect *)rects;
1435 
1436 	VMW_DEBUG_KMS("Layout count = %u\n", arg->num_outputs);
1437 	for (i = 0; i < arg->num_outputs; i++) {
1438 		struct drm_vmw_rect curr_rect;
1439 
1440 		/* Verify user-space for overflow as kernel use drm_rect */
1441 		if ((rects[i].x + rects[i].w > INT_MAX) ||
1442 		    (rects[i].y + rects[i].h > INT_MAX)) {
1443 			ret = -ERANGE;
1444 			goto out_free;
1445 		}
1446 
1447 		curr_rect = rects[i];
1448 		drm_rects[i].x1 = curr_rect.x;
1449 		drm_rects[i].y1 = curr_rect.y;
1450 		drm_rects[i].x2 = curr_rect.x + curr_rect.w;
1451 		drm_rects[i].y2 = curr_rect.y + curr_rect.h;
1452 
1453 		VMW_DEBUG_KMS("  x1 = %d y1 = %d x2 = %d y2 = %d\n",
1454 			      drm_rects[i].x1, drm_rects[i].y1,
1455 			      drm_rects[i].x2, drm_rects[i].y2);
1456 
1457 		/*
1458 		 * Currently this check is limiting the topology within
1459 		 * mode_config->max (which actually is max texture size
1460 		 * supported by virtual device). This limit is here to address
1461 		 * window managers that create a big framebuffer for whole
1462 		 * topology.
1463 		 */
1464 		if (drm_rects[i].x1 < 0 ||  drm_rects[i].y1 < 0 ||
1465 		    drm_rects[i].x2 > mode_config->max_width ||
1466 		    drm_rects[i].y2 > mode_config->max_height) {
1467 			VMW_DEBUG_KMS("Invalid layout %d %d %d %d\n",
1468 				      drm_rects[i].x1, drm_rects[i].y1,
1469 				      drm_rects[i].x2, drm_rects[i].y2);
1470 			ret = -EINVAL;
1471 			goto out_free;
1472 		}
1473 	}
1474 
1475 	ret = vmw_kms_check_display_memory(dev, arg->num_outputs, drm_rects);
1476 
1477 	if (ret == 0)
1478 		vmw_du_update_layout(dev_priv, arg->num_outputs, drm_rects);
1479 
1480 out_free:
1481 	kfree(rects);
1482 	return ret;
1483 }
1484 
1485 /**
1486  * vmw_kms_helper_dirty - Helper to build commands and perform actions based
1487  * on a set of cliprects and a set of display units.
1488  *
1489  * @dev_priv: Pointer to a device private structure.
1490  * @framebuffer: Pointer to the framebuffer on which to perform the actions.
1491  * @clips: A set of struct drm_clip_rect. Either this os @vclips must be NULL.
1492  * Cliprects are given in framebuffer coordinates.
1493  * @vclips: A set of struct drm_vmw_rect cliprects. Either this or @clips must
1494  * be NULL. Cliprects are given in source coordinates.
1495  * @dest_x: X coordinate offset for the crtc / destination clip rects.
1496  * @dest_y: Y coordinate offset for the crtc / destination clip rects.
1497  * @num_clips: Number of cliprects in the @clips or @vclips array.
1498  * @increment: Integer with which to increment the clip counter when looping.
1499  * Used to skip a predetermined number of clip rects.
1500  * @dirty: Closure structure. See the description of struct vmw_kms_dirty.
1501  */
1502 int vmw_kms_helper_dirty(struct vmw_private *dev_priv,
1503 			 struct vmw_framebuffer *framebuffer,
1504 			 const struct drm_clip_rect *clips,
1505 			 const struct drm_vmw_rect *vclips,
1506 			 s32 dest_x, s32 dest_y,
1507 			 int num_clips,
1508 			 int increment,
1509 			 struct vmw_kms_dirty *dirty)
1510 {
1511 	struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
1512 	struct drm_crtc *crtc;
1513 	u32 num_units = 0;
1514 	u32 i, k;
1515 
1516 	dirty->dev_priv = dev_priv;
1517 
1518 	/* If crtc is passed, no need to iterate over other display units */
1519 	if (dirty->crtc) {
1520 		units[num_units++] = vmw_crtc_to_du(dirty->crtc);
1521 	} else {
1522 		list_for_each_entry(crtc, &dev_priv->drm.mode_config.crtc_list,
1523 				    head) {
1524 			struct drm_plane *plane = crtc->primary;
1525 
1526 			if (plane->state->fb == &framebuffer->base)
1527 				units[num_units++] = vmw_crtc_to_du(crtc);
1528 		}
1529 	}
1530 
1531 	for (k = 0; k < num_units; k++) {
1532 		struct vmw_display_unit *unit = units[k];
1533 		s32 crtc_x = unit->crtc.x;
1534 		s32 crtc_y = unit->crtc.y;
1535 		s32 crtc_width = unit->crtc.mode.hdisplay;
1536 		s32 crtc_height = unit->crtc.mode.vdisplay;
1537 		const struct drm_clip_rect *clips_ptr = clips;
1538 		const struct drm_vmw_rect *vclips_ptr = vclips;
1539 
1540 		dirty->unit = unit;
1541 		if (dirty->fifo_reserve_size > 0) {
1542 			dirty->cmd = VMW_CMD_RESERVE(dev_priv,
1543 						      dirty->fifo_reserve_size);
1544 			if (!dirty->cmd)
1545 				return -ENOMEM;
1546 
1547 			memset(dirty->cmd, 0, dirty->fifo_reserve_size);
1548 		}
1549 		dirty->num_hits = 0;
1550 		for (i = 0; i < num_clips; i++, clips_ptr += increment,
1551 		       vclips_ptr += increment) {
1552 			s32 clip_left;
1553 			s32 clip_top;
1554 
1555 			/*
1556 			 * Select clip array type. Note that integer type
1557 			 * in @clips is unsigned short, whereas in @vclips
1558 			 * it's 32-bit.
1559 			 */
1560 			if (clips) {
1561 				dirty->fb_x = (s32) clips_ptr->x1;
1562 				dirty->fb_y = (s32) clips_ptr->y1;
1563 				dirty->unit_x2 = (s32) clips_ptr->x2 + dest_x -
1564 					crtc_x;
1565 				dirty->unit_y2 = (s32) clips_ptr->y2 + dest_y -
1566 					crtc_y;
1567 			} else {
1568 				dirty->fb_x = vclips_ptr->x;
1569 				dirty->fb_y = vclips_ptr->y;
1570 				dirty->unit_x2 = dirty->fb_x + vclips_ptr->w +
1571 					dest_x - crtc_x;
1572 				dirty->unit_y2 = dirty->fb_y + vclips_ptr->h +
1573 					dest_y - crtc_y;
1574 			}
1575 
1576 			dirty->unit_x1 = dirty->fb_x + dest_x - crtc_x;
1577 			dirty->unit_y1 = dirty->fb_y + dest_y - crtc_y;
1578 
1579 			/* Skip this clip if it's outside the crtc region */
1580 			if (dirty->unit_x1 >= crtc_width ||
1581 			    dirty->unit_y1 >= crtc_height ||
1582 			    dirty->unit_x2 <= 0 || dirty->unit_y2 <= 0)
1583 				continue;
1584 
1585 			/* Clip right and bottom to crtc limits */
1586 			dirty->unit_x2 = min_t(s32, dirty->unit_x2,
1587 					       crtc_width);
1588 			dirty->unit_y2 = min_t(s32, dirty->unit_y2,
1589 					       crtc_height);
1590 
1591 			/* Clip left and top to crtc limits */
1592 			clip_left = min_t(s32, dirty->unit_x1, 0);
1593 			clip_top = min_t(s32, dirty->unit_y1, 0);
1594 			dirty->unit_x1 -= clip_left;
1595 			dirty->unit_y1 -= clip_top;
1596 			dirty->fb_x -= clip_left;
1597 			dirty->fb_y -= clip_top;
1598 
1599 			dirty->clip(dirty);
1600 		}
1601 
1602 		dirty->fifo_commit(dirty);
1603 	}
1604 
1605 	return 0;
1606 }
1607 
1608 /**
1609  * vmw_kms_helper_validation_finish - Helper for post KMS command submission
1610  * cleanup and fencing
1611  * @dev_priv: Pointer to the device-private struct
1612  * @file_priv: Pointer identifying the client when user-space fencing is used
1613  * @ctx: Pointer to the validation context
1614  * @out_fence: If non-NULL, returned refcounted fence-pointer
1615  * @user_fence_rep: If non-NULL, pointer to user-space address area
1616  * in which to copy user-space fence info
1617  */
1618 void vmw_kms_helper_validation_finish(struct vmw_private *dev_priv,
1619 				      struct drm_file *file_priv,
1620 				      struct vmw_validation_context *ctx,
1621 				      struct vmw_fence_obj **out_fence,
1622 				      struct drm_vmw_fence_rep __user *
1623 				      user_fence_rep)
1624 {
1625 	struct vmw_fence_obj *fence = NULL;
1626 	uint32_t handle = 0;
1627 	int ret = 0;
1628 
1629 	if (file_priv || user_fence_rep || vmw_validation_has_bos(ctx) ||
1630 	    out_fence)
1631 		ret = vmw_execbuf_fence_commands(file_priv, dev_priv, &fence,
1632 						 file_priv ? &handle : NULL);
1633 	vmw_validation_done(ctx, fence);
1634 	if (file_priv)
1635 		vmw_execbuf_copy_fence_user(dev_priv, vmw_fpriv(file_priv),
1636 					    ret, user_fence_rep, fence,
1637 					    handle, -1);
1638 	if (out_fence)
1639 		*out_fence = fence;
1640 	else
1641 		vmw_fence_obj_unreference(&fence);
1642 }
1643 
1644 /**
1645  * vmw_kms_create_implicit_placement_property - Set up the implicit placement
1646  * property.
1647  *
1648  * @dev_priv: Pointer to a device private struct.
1649  *
1650  * Sets up the implicit placement property unless it's already set up.
1651  */
1652 void
1653 vmw_kms_create_implicit_placement_property(struct vmw_private *dev_priv)
1654 {
1655 	if (dev_priv->implicit_placement_property)
1656 		return;
1657 
1658 	dev_priv->implicit_placement_property =
1659 		drm_property_create_range(&dev_priv->drm,
1660 					  DRM_MODE_PROP_IMMUTABLE,
1661 					  "implicit_placement", 0, 1);
1662 }
1663 
1664 /**
1665  * vmw_kms_suspend - Save modesetting state and turn modesetting off.
1666  *
1667  * @dev: Pointer to the drm device
1668  * Return: 0 on success. Negative error code on failure.
1669  */
1670 int vmw_kms_suspend(struct drm_device *dev)
1671 {
1672 	struct vmw_private *dev_priv = vmw_priv(dev);
1673 
1674 	dev_priv->suspend_state = drm_atomic_helper_suspend(dev);
1675 	if (IS_ERR(dev_priv->suspend_state)) {
1676 		int ret = PTR_ERR(dev_priv->suspend_state);
1677 
1678 		DRM_ERROR("Failed kms suspend: %d\n", ret);
1679 		dev_priv->suspend_state = NULL;
1680 
1681 		return ret;
1682 	}
1683 
1684 	return 0;
1685 }
1686 
1687 
1688 /**
1689  * vmw_kms_resume - Re-enable modesetting and restore state
1690  *
1691  * @dev: Pointer to the drm device
1692  * Return: 0 on success. Negative error code on failure.
1693  *
1694  * State is resumed from a previous vmw_kms_suspend(). It's illegal
1695  * to call this function without a previous vmw_kms_suspend().
1696  */
1697 int vmw_kms_resume(struct drm_device *dev)
1698 {
1699 	struct vmw_private *dev_priv = vmw_priv(dev);
1700 	int ret;
1701 
1702 	if (WARN_ON(!dev_priv->suspend_state))
1703 		return 0;
1704 
1705 	ret = drm_atomic_helper_resume(dev, dev_priv->suspend_state);
1706 	dev_priv->suspend_state = NULL;
1707 
1708 	return ret;
1709 }
1710 
1711 /**
1712  * vmw_kms_lost_device - Notify kms that modesetting capabilities will be lost
1713  *
1714  * @dev: Pointer to the drm device
1715  */
1716 void vmw_kms_lost_device(struct drm_device *dev)
1717 {
1718 	drm_atomic_helper_shutdown(dev);
1719 }
1720 
1721 /**
1722  * vmw_du_helper_plane_update - Helper to do plane update on a display unit.
1723  * @update: The closure structure.
1724  *
1725  * Call this helper after setting callbacks in &vmw_du_update_plane to do plane
1726  * update on display unit.
1727  *
1728  * Return: 0 on success or a negative error code on failure.
1729  */
1730 int vmw_du_helper_plane_update(struct vmw_du_update_plane *update)
1731 {
1732 	struct drm_plane_state *state = update->plane->state;
1733 	struct drm_plane_state *old_state = update->old_state;
1734 	struct drm_atomic_helper_damage_iter iter;
1735 	struct drm_rect clip;
1736 	struct drm_rect bb;
1737 	DECLARE_VAL_CONTEXT(val_ctx, NULL, 0);
1738 	uint32_t reserved_size = 0;
1739 	uint32_t submit_size = 0;
1740 	uint32_t curr_size = 0;
1741 	uint32_t num_hits = 0;
1742 	void *cmd_start;
1743 	char *cmd_next;
1744 	int ret;
1745 
1746 	/*
1747 	 * Iterate in advance to check if really need plane update and find the
1748 	 * number of clips that actually are in plane src for fifo allocation.
1749 	 */
1750 	drm_atomic_helper_damage_iter_init(&iter, old_state, state);
1751 	drm_atomic_for_each_plane_damage(&iter, &clip)
1752 		num_hits++;
1753 
1754 	if (num_hits == 0)
1755 		return 0;
1756 
1757 	if (update->vfb->bo) {
1758 		struct vmw_framebuffer_bo *vfbbo =
1759 			container_of(update->vfb, typeof(*vfbbo), base);
1760 
1761 		/*
1762 		 * For screen targets we want a mappable bo, for everything else we want
1763 		 * accelerated i.e. host backed (vram or gmr) bo. If the display unit
1764 		 * is not screen target then mob's shouldn't be available.
1765 		 */
1766 		if (update->dev_priv->active_display_unit == vmw_du_screen_target) {
1767 			vmw_bo_placement_set(vfbbo->buffer,
1768 					     VMW_BO_DOMAIN_SYS | VMW_BO_DOMAIN_MOB | VMW_BO_DOMAIN_GMR,
1769 					     VMW_BO_DOMAIN_SYS | VMW_BO_DOMAIN_MOB | VMW_BO_DOMAIN_GMR);
1770 		} else {
1771 			WARN_ON(update->dev_priv->has_mob);
1772 			vmw_bo_placement_set_default_accelerated(vfbbo->buffer);
1773 		}
1774 		ret = vmw_validation_add_bo(&val_ctx, vfbbo->buffer);
1775 	} else {
1776 		struct vmw_framebuffer_surface *vfbs =
1777 			container_of(update->vfb, typeof(*vfbs), base);
1778 		struct vmw_surface *surf = vmw_user_object_surface(&vfbs->uo);
1779 
1780 		ret = vmw_validation_add_resource(&val_ctx, &surf->res,
1781 						  0, VMW_RES_DIRTY_NONE, NULL,
1782 						  NULL);
1783 	}
1784 
1785 	if (ret)
1786 		return ret;
1787 
1788 	ret = vmw_validation_prepare(&val_ctx, update->mutex, update->intr);
1789 	if (ret)
1790 		goto out_unref;
1791 
1792 	reserved_size = update->calc_fifo_size(update, num_hits);
1793 	cmd_start = VMW_CMD_RESERVE(update->dev_priv, reserved_size);
1794 	if (!cmd_start) {
1795 		ret = -ENOMEM;
1796 		goto out_revert;
1797 	}
1798 
1799 	cmd_next = cmd_start;
1800 
1801 	if (update->post_prepare) {
1802 		curr_size = update->post_prepare(update, cmd_next);
1803 		cmd_next += curr_size;
1804 		submit_size += curr_size;
1805 	}
1806 
1807 	if (update->pre_clip) {
1808 		curr_size = update->pre_clip(update, cmd_next, num_hits);
1809 		cmd_next += curr_size;
1810 		submit_size += curr_size;
1811 	}
1812 
1813 	bb.x1 = INT_MAX;
1814 	bb.y1 = INT_MAX;
1815 	bb.x2 = INT_MIN;
1816 	bb.y2 = INT_MIN;
1817 
1818 	drm_atomic_helper_damage_iter_init(&iter, old_state, state);
1819 	drm_atomic_for_each_plane_damage(&iter, &clip) {
1820 		uint32_t fb_x = clip.x1;
1821 		uint32_t fb_y = clip.y1;
1822 
1823 		vmw_du_translate_to_crtc(state, &clip);
1824 		if (update->clip) {
1825 			curr_size = update->clip(update, cmd_next, &clip, fb_x,
1826 						 fb_y);
1827 			cmd_next += curr_size;
1828 			submit_size += curr_size;
1829 		}
1830 		bb.x1 = min_t(int, bb.x1, clip.x1);
1831 		bb.y1 = min_t(int, bb.y1, clip.y1);
1832 		bb.x2 = max_t(int, bb.x2, clip.x2);
1833 		bb.y2 = max_t(int, bb.y2, clip.y2);
1834 	}
1835 
1836 	curr_size = update->post_clip(update, cmd_next, &bb);
1837 	submit_size += curr_size;
1838 
1839 	if (reserved_size < submit_size)
1840 		submit_size = 0;
1841 
1842 	vmw_cmd_commit(update->dev_priv, submit_size);
1843 
1844 	vmw_kms_helper_validation_finish(update->dev_priv, NULL, &val_ctx,
1845 					 update->out_fence, NULL);
1846 	return ret;
1847 
1848 out_revert:
1849 	vmw_validation_revert(&val_ctx);
1850 
1851 out_unref:
1852 	vmw_validation_unref_lists(&val_ctx);
1853 	return ret;
1854 }
1855 
1856 /**
1857  * vmw_connector_mode_valid - implements drm_connector_helper_funcs.mode_valid callback
1858  *
1859  * @connector: the drm connector, part of a DU container
1860  * @mode: drm mode to check
1861  *
1862  * Returns MODE_OK on success, or a drm_mode_status error code.
1863  */
1864 enum drm_mode_status vmw_connector_mode_valid(struct drm_connector *connector,
1865 					      const struct drm_display_mode *mode)
1866 {
1867 	enum drm_mode_status ret;
1868 	struct drm_device *dev = connector->dev;
1869 	struct vmw_private *dev_priv = vmw_priv(dev);
1870 	u32 assumed_cpp = 4;
1871 
1872 	if (dev_priv->assume_16bpp)
1873 		assumed_cpp = 2;
1874 
1875 	ret = drm_mode_validate_size(mode, dev_priv->texture_max_width,
1876 				     dev_priv->texture_max_height);
1877 	if (ret != MODE_OK)
1878 		return ret;
1879 
1880 	if (!vmw_kms_validate_mode_vram(dev_priv,
1881 					mode->hdisplay * assumed_cpp,
1882 					mode->vdisplay))
1883 		return MODE_MEM;
1884 
1885 	return MODE_OK;
1886 }
1887 
1888 /**
1889  * vmw_connector_get_modes - implements drm_connector_helper_funcs.get_modes callback
1890  *
1891  * @connector: the drm connector, part of a DU container
1892  *
1893  * Returns the number of added modes.
1894  */
1895 int vmw_connector_get_modes(struct drm_connector *connector)
1896 {
1897 	struct vmw_display_unit *du = vmw_connector_to_du(connector);
1898 	struct drm_device *dev = connector->dev;
1899 	struct vmw_private *dev_priv = vmw_priv(dev);
1900 	struct drm_display_mode *mode = NULL;
1901 	struct drm_display_mode prefmode = { DRM_MODE("preferred",
1902 		DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED,
1903 		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1904 		DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC)
1905 	};
1906 	u32 max_width;
1907 	u32 max_height;
1908 	u32 num_modes;
1909 
1910 	/* Add preferred mode */
1911 	mode = drm_mode_duplicate(dev, &prefmode);
1912 	if (!mode)
1913 		return 0;
1914 
1915 	mode->hdisplay = du->pref_width;
1916 	mode->vdisplay = du->pref_height;
1917 	vmw_guess_mode_timing(mode);
1918 	drm_mode_set_name(mode);
1919 
1920 	drm_mode_probed_add(connector, mode);
1921 	drm_dbg_kms(dev, "preferred mode " DRM_MODE_FMT "\n", DRM_MODE_ARG(mode));
1922 
1923 	/* Probe connector for all modes not exceeding our geom limits */
1924 	max_width  = dev_priv->texture_max_width;
1925 	max_height = dev_priv->texture_max_height;
1926 
1927 	if (dev_priv->active_display_unit == vmw_du_screen_target) {
1928 		max_width  = min(dev_priv->stdu_max_width,  max_width);
1929 		max_height = min(dev_priv->stdu_max_height, max_height);
1930 	}
1931 
1932 	num_modes = 1 + drm_add_modes_noedid(connector, max_width, max_height);
1933 
1934 	return num_modes;
1935 }
1936 
1937 struct vmw_user_object *vmw_user_object_ref(struct vmw_user_object *uo)
1938 {
1939 	if (uo->buffer)
1940 		vmw_user_bo_ref(uo->buffer);
1941 	else if (uo->surface)
1942 		vmw_surface_reference(uo->surface);
1943 	return uo;
1944 }
1945 
1946 void vmw_user_object_unref(struct vmw_user_object *uo)
1947 {
1948 	if (uo->buffer)
1949 		vmw_user_bo_unref(&uo->buffer);
1950 	else if (uo->surface)
1951 		vmw_surface_unreference(&uo->surface);
1952 }
1953 
1954 struct vmw_bo *
1955 vmw_user_object_buffer(struct vmw_user_object *uo)
1956 {
1957 	if (uo->buffer)
1958 		return uo->buffer;
1959 	else if (uo->surface)
1960 		return uo->surface->res.guest_memory_bo;
1961 	return NULL;
1962 }
1963 
1964 struct vmw_surface *
1965 vmw_user_object_surface(struct vmw_user_object *uo)
1966 {
1967 	if (uo->buffer)
1968 		return uo->buffer->dumb_surface;
1969 	return uo->surface;
1970 }
1971 
1972 void *vmw_user_object_map(struct vmw_user_object *uo)
1973 {
1974 	struct vmw_bo *bo = vmw_user_object_buffer(uo);
1975 
1976 	WARN_ON(!bo);
1977 	return vmw_bo_map_and_cache(bo);
1978 }
1979 
1980 void *vmw_user_object_map_size(struct vmw_user_object *uo, size_t size)
1981 {
1982 	struct vmw_bo *bo = vmw_user_object_buffer(uo);
1983 
1984 	WARN_ON(!bo);
1985 	return vmw_bo_map_and_cache_size(bo, size);
1986 }
1987 
1988 void vmw_user_object_unmap(struct vmw_user_object *uo)
1989 {
1990 	struct vmw_bo *bo = vmw_user_object_buffer(uo);
1991 	int ret;
1992 
1993 	WARN_ON(!bo);
1994 
1995 	/* Fence the mob creation so we are guarateed to have the mob */
1996 	ret = ttm_bo_reserve(&bo->tbo, false, false, NULL);
1997 	if (ret != 0)
1998 		return;
1999 
2000 	vmw_bo_unmap(bo);
2001 	vmw_bo_pin_reserved(bo, false);
2002 
2003 	ttm_bo_unreserve(&bo->tbo);
2004 }
2005 
2006 bool vmw_user_object_is_mapped(struct vmw_user_object *uo)
2007 {
2008 	struct vmw_bo *bo;
2009 
2010 	if (!uo || vmw_user_object_is_null(uo))
2011 		return false;
2012 
2013 	bo = vmw_user_object_buffer(uo);
2014 
2015 	if (WARN_ON(!bo))
2016 		return false;
2017 
2018 	WARN_ON(bo->map.bo && !bo->map.virtual);
2019 	return bo->map.virtual;
2020 }
2021 
2022 bool vmw_user_object_is_null(struct vmw_user_object *uo)
2023 {
2024 	return !uo->buffer && !uo->surface;
2025 }
2026