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