drm/vmwgfx: CRTC atomic state
[linux-2.6-block.git] / drivers / gpu / drm / vmwgfx / vmwgfx_kms.c
CommitLineData
fb1d9738
JB
1/**************************************************************************
2 *
54fbde8a 3 * Copyright © 2009-2015 VMware, Inc., Palo Alto, CA., USA
fb1d9738
JB
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 * USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28#include "vmwgfx_kms.h"
9c2542a4
SY
29#include <drm/drm_atomic.h>
30#include <drm/drm_atomic_helper.h>
fb1d9738 31
56d1c78d 32
fb1d9738
JB
33/* Might need a hrtimer here? */
34#define VMWGFX_PRESENT_RATE ((HZ / 60 > 0) ? HZ / 60 : 1)
35
c8261a96 36void vmw_du_cleanup(struct vmw_display_unit *du)
fb1d9738 37{
36cc79bc
SY
38 drm_plane_cleanup(&du->primary);
39 drm_plane_cleanup(&du->cursor);
40
34ea3d38 41 drm_connector_unregister(&du->connector);
fb1d9738
JB
42 drm_crtc_cleanup(&du->crtc);
43 drm_encoder_cleanup(&du->encoder);
44 drm_connector_cleanup(&du->connector);
45}
46
47/*
48 * Display Unit Cursor functions
49 */
50
36cc79bc
SY
51static int vmw_cursor_update_image(struct vmw_private *dev_priv,
52 u32 *image, u32 width, u32 height,
53 u32 hotspotX, u32 hotspotY)
fb1d9738
JB
54{
55 struct {
56 u32 cmd;
57 SVGAFifoCmdDefineAlphaCursor cursor;
58 } *cmd;
59 u32 image_size = width * height * 4;
60 u32 cmd_size = sizeof(*cmd) + image_size;
61
62 if (!image)
63 return -EINVAL;
64
65 cmd = vmw_fifo_reserve(dev_priv, cmd_size);
66 if (unlikely(cmd == NULL)) {
67 DRM_ERROR("Fifo reserve failed.\n");
68 return -ENOMEM;
69 }
70
71 memset(cmd, 0, sizeof(*cmd));
72
73 memcpy(&cmd[1], image, image_size);
74
b9eb1a61
TH
75 cmd->cmd = SVGA_CMD_DEFINE_ALPHA_CURSOR;
76 cmd->cursor.id = 0;
77 cmd->cursor.width = width;
78 cmd->cursor.height = height;
79 cmd->cursor.hotspotX = hotspotX;
80 cmd->cursor.hotspotY = hotspotY;
fb1d9738 81
4e0858a6 82 vmw_fifo_commit_flush(dev_priv, cmd_size);
fb1d9738
JB
83
84 return 0;
85}
86
36cc79bc
SY
87static int vmw_cursor_update_dmabuf(struct vmw_private *dev_priv,
88 struct vmw_dma_buffer *dmabuf,
89 u32 width, u32 height,
90 u32 hotspotX, u32 hotspotY)
6a91d97e
JB
91{
92 struct ttm_bo_kmap_obj map;
93 unsigned long kmap_offset;
94 unsigned long kmap_num;
95 void *virtual;
96 bool dummy;
97 int ret;
98
99 kmap_offset = 0;
100 kmap_num = (width*height*4 + PAGE_SIZE - 1) >> PAGE_SHIFT;
101
dfd5e50e 102 ret = ttm_bo_reserve(&dmabuf->base, true, false, NULL);
6a91d97e
JB
103 if (unlikely(ret != 0)) {
104 DRM_ERROR("reserve failed\n");
105 return -EINVAL;
106 }
107
108 ret = ttm_bo_kmap(&dmabuf->base, kmap_offset, kmap_num, &map);
109 if (unlikely(ret != 0))
110 goto err_unreserve;
111
112 virtual = ttm_kmap_obj_virtual(&map, &dummy);
113 ret = vmw_cursor_update_image(dev_priv, virtual, width, height,
114 hotspotX, hotspotY);
115
116 ttm_bo_kunmap(&map);
117err_unreserve:
118 ttm_bo_unreserve(&dmabuf->base);
119
120 return ret;
121}
122
123
36cc79bc
SY
124static void vmw_cursor_update_position(struct vmw_private *dev_priv,
125 bool show, int x, int y)
fb1d9738 126{
b76ff5ea 127 u32 *fifo_mem = dev_priv->mmio_virt;
fb1d9738
JB
128 uint32_t count;
129
36cc79bc 130 spin_lock(&dev_priv->cursor_lock);
b76ff5ea
TH
131 vmw_mmio_write(show ? 1 : 0, fifo_mem + SVGA_FIFO_CURSOR_ON);
132 vmw_mmio_write(x, fifo_mem + SVGA_FIFO_CURSOR_X);
133 vmw_mmio_write(y, fifo_mem + SVGA_FIFO_CURSOR_Y);
134 count = vmw_mmio_read(fifo_mem + SVGA_FIFO_CURSOR_COUNT);
135 vmw_mmio_write(++count, fifo_mem + SVGA_FIFO_CURSOR_COUNT);
36cc79bc 136 spin_unlock(&dev_priv->cursor_lock);
fb1d9738
JB
137}
138
8fbf9d92 139
fb1d9738
JB
140void vmw_kms_cursor_snoop(struct vmw_surface *srf,
141 struct ttm_object_file *tfile,
142 struct ttm_buffer_object *bo,
143 SVGA3dCmdHeader *header)
144{
145 struct ttm_bo_kmap_obj map;
146 unsigned long kmap_offset;
147 unsigned long kmap_num;
148 SVGA3dCopyBox *box;
149 unsigned box_count;
150 void *virtual;
151 bool dummy;
152 struct vmw_dma_cmd {
153 SVGA3dCmdHeader header;
154 SVGA3dCmdSurfaceDMA dma;
155 } *cmd;
2ac86371 156 int i, ret;
fb1d9738
JB
157
158 cmd = container_of(header, struct vmw_dma_cmd, header);
159
160 /* No snooper installed */
161 if (!srf->snooper.image)
162 return;
163
164 if (cmd->dma.host.face != 0 || cmd->dma.host.mipmap != 0) {
165 DRM_ERROR("face and mipmap for cursors should never != 0\n");
166 return;
167 }
168
169 if (cmd->header.size < 64) {
170 DRM_ERROR("at least one full copy box must be given\n");
171 return;
172 }
173
174 box = (SVGA3dCopyBox *)&cmd[1];
175 box_count = (cmd->header.size - sizeof(SVGA3dCmdSurfaceDMA)) /
176 sizeof(SVGA3dCopyBox);
177
2ac86371 178 if (cmd->dma.guest.ptr.offset % PAGE_SIZE ||
fb1d9738
JB
179 box->x != 0 || box->y != 0 || box->z != 0 ||
180 box->srcx != 0 || box->srcy != 0 || box->srcz != 0 ||
2ac86371 181 box->d != 1 || box_count != 1) {
fb1d9738 182 /* TODO handle none page aligned offsets */
2ac86371
JB
183 /* TODO handle more dst & src != 0 */
184 /* TODO handle more then one copy */
185 DRM_ERROR("Cant snoop dma request for cursor!\n");
186 DRM_ERROR("(%u, %u, %u) (%u, %u, %u) (%ux%ux%u) %u %u\n",
187 box->srcx, box->srcy, box->srcz,
188 box->x, box->y, box->z,
189 box->w, box->h, box->d, box_count,
190 cmd->dma.guest.ptr.offset);
fb1d9738
JB
191 return;
192 }
193
194 kmap_offset = cmd->dma.guest.ptr.offset >> PAGE_SHIFT;
195 kmap_num = (64*64*4) >> PAGE_SHIFT;
196
dfd5e50e 197 ret = ttm_bo_reserve(bo, true, false, NULL);
fb1d9738
JB
198 if (unlikely(ret != 0)) {
199 DRM_ERROR("reserve failed\n");
200 return;
201 }
202
203 ret = ttm_bo_kmap(bo, kmap_offset, kmap_num, &map);
204 if (unlikely(ret != 0))
205 goto err_unreserve;
206
207 virtual = ttm_kmap_obj_virtual(&map, &dummy);
208
2ac86371
JB
209 if (box->w == 64 && cmd->dma.guest.pitch == 64*4) {
210 memcpy(srf->snooper.image, virtual, 64*64*4);
211 } else {
212 /* Image is unsigned pointer. */
213 for (i = 0; i < box->h; i++)
214 memcpy(srf->snooper.image + i * 64,
215 virtual + i * cmd->dma.guest.pitch,
216 box->w * 4);
217 }
218
fb1d9738
JB
219 srf->snooper.age++;
220
fb1d9738
JB
221 ttm_bo_kunmap(&map);
222err_unreserve:
223 ttm_bo_unreserve(bo);
224}
225
8fbf9d92
TH
226/**
227 * vmw_kms_legacy_hotspot_clear - Clear legacy hotspots
228 *
229 * @dev_priv: Pointer to the device private struct.
230 *
231 * Clears all legacy hotspots.
232 */
233void vmw_kms_legacy_hotspot_clear(struct vmw_private *dev_priv)
234{
235 struct drm_device *dev = dev_priv->dev;
236 struct vmw_display_unit *du;
237 struct drm_crtc *crtc;
238
239 drm_modeset_lock_all(dev);
240 drm_for_each_crtc(crtc, dev) {
241 du = vmw_crtc_to_du(crtc);
242
243 du->hotspot_x = 0;
244 du->hotspot_y = 0;
245 }
246 drm_modeset_unlock_all(dev);
247}
248
fb1d9738
JB
249void vmw_kms_cursor_post_execbuf(struct vmw_private *dev_priv)
250{
251 struct drm_device *dev = dev_priv->dev;
252 struct vmw_display_unit *du;
253 struct drm_crtc *crtc;
254
255 mutex_lock(&dev->mode_config.mutex);
256
257 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
258 du = vmw_crtc_to_du(crtc);
259 if (!du->cursor_surface ||
260 du->cursor_age == du->cursor_surface->snooper.age)
261 continue;
262
263 du->cursor_age = du->cursor_surface->snooper.age;
264 vmw_cursor_update_image(dev_priv,
265 du->cursor_surface->snooper.image,
8fbf9d92
TH
266 64, 64,
267 du->hotspot_x + du->core_hotspot_x,
268 du->hotspot_y + du->core_hotspot_y);
fb1d9738
JB
269 }
270
271 mutex_unlock(&dev->mode_config.mutex);
272}
273
36cc79bc
SY
274
275
276/**
277 * vmw_du_cursor_plane_update() - Update cursor image and location
278 *
279 * @plane: plane object to update
280 * @crtc: owning CRTC of @plane
281 * @fb: framebuffer to flip onto plane
282 * @crtc_x: x offset of plane on crtc
283 * @crtc_y: y offset of plane on crtc
284 * @crtc_w: width of plane rectangle on crtc
285 * @crtc_h: height of plane rectangle on crtc
286 * @src_x: Not used
287 * @src_y: Not used
288 * @src_w: Not used
289 * @src_h: Not used
290 *
291 *
292 * RETURNS:
293 * Zero on success, error code on failure
294 */
295int vmw_du_cursor_plane_update(struct drm_plane *plane,
296 struct drm_crtc *crtc,
297 struct drm_framebuffer *fb,
298 int crtc_x, int crtc_y,
299 unsigned int crtc_w,
300 unsigned int crtc_h,
301 uint32_t src_x, uint32_t src_y,
302 uint32_t src_w, uint32_t src_h)
303{
304 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
305 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
306 struct vmw_surface *surface = NULL;
307 struct vmw_dma_buffer *dmabuf = NULL;
308 s32 hotspot_x, hotspot_y;
309 int ret;
310
311 hotspot_x = du->hotspot_x + fb->hot_x;
312 hotspot_y = du->hotspot_y + fb->hot_y;
313
314 /* A lot of the code assumes this */
315 if (crtc_w != 64 || crtc_h != 64) {
316 ret = -EINVAL;
317 goto out;
318 }
319
320 if (vmw_framebuffer_to_vfb(fb)->dmabuf)
321 dmabuf = vmw_framebuffer_to_vfbd(fb)->buffer;
322 else
323 surface = vmw_framebuffer_to_vfbs(fb)->surface;
324
325 if (surface && !surface->snooper.image) {
326 DRM_ERROR("surface not suitable for cursor\n");
327 ret = -EINVAL;
328 goto out;
329 }
330
331 /* setup new image */
332 ret = 0;
333 if (surface) {
334 /* vmw_user_surface_lookup takes one reference */
335 du->cursor_surface = surface;
336
337 du->cursor_age = du->cursor_surface->snooper.age;
338
339 ret = vmw_cursor_update_image(dev_priv, surface->snooper.image,
340 64, 64, hotspot_x, hotspot_y);
341 } else if (dmabuf) {
342 /* vmw_user_surface_lookup takes one reference */
343 du->cursor_dmabuf = dmabuf;
344
345 ret = vmw_cursor_update_dmabuf(dev_priv, dmabuf, crtc_w, crtc_h,
346 hotspot_x, hotspot_y);
347 } else {
348 vmw_cursor_update_position(dev_priv, false, 0, 0);
349 goto out;
350 }
351
352 if (!ret) {
353 du->cursor_x = crtc_x + du->set_gui_x;
354 du->cursor_y = crtc_y + du->set_gui_y;
355
356 vmw_cursor_update_position(dev_priv, true,
357 du->cursor_x + hotspot_x,
358 du->cursor_y + hotspot_y);
359 }
360
361out:
362 return ret;
363}
364
365
366int vmw_du_cursor_plane_disable(struct drm_plane *plane)
367{
368 if (plane->fb) {
369 drm_framebuffer_unreference(plane->fb);
370 plane->fb = NULL;
371 }
372
373 return -EINVAL;
374}
375
376
377void vmw_du_cursor_plane_destroy(struct drm_plane *plane)
378{
379 vmw_cursor_update_position(plane->dev->dev_private, false, 0, 0);
380
381 drm_plane_cleanup(plane);
382}
383
384
385void vmw_du_primary_plane_destroy(struct drm_plane *plane)
386{
387 drm_plane_cleanup(plane);
388
389 /* Planes are static in our case so we don't free it */
390}
391
392
9c2542a4
SY
393/**
394 * vmw_du_crtc_duplicate_state - duplicate crtc state
395 * @crtc: DRM crtc
396 *
397 * Allocates and returns a copy of the crtc state (both common and
398 * vmw-specific) for the specified crtc.
399 *
400 * Returns: The newly allocated crtc state, or NULL on failure.
401 */
402struct drm_crtc_state *
403vmw_du_crtc_duplicate_state(struct drm_crtc *crtc)
404{
405 struct drm_crtc_state *state;
406 struct vmw_crtc_state *vcs;
407
408 if (WARN_ON(!crtc->state))
409 return NULL;
410
411 vcs = kmemdup(crtc->state, sizeof(*vcs), GFP_KERNEL);
412
413 if (!vcs)
414 return NULL;
415
416 state = &vcs->base;
417
418 __drm_atomic_helper_crtc_duplicate_state(crtc, state);
419
420 return state;
421}
422
423
424/**
425 * vmw_du_crtc_reset - creates a blank vmw crtc state
426 * @crtc: DRM crtc
427 *
428 * Resets the atomic state for @crtc by freeing the state pointer (which
429 * might be NULL, e.g. at driver load time) and allocating a new empty state
430 * object.
431 */
432void vmw_du_crtc_reset(struct drm_crtc *crtc)
433{
434 struct vmw_crtc_state *vcs;
435
436
437 if (crtc->state) {
438 __drm_atomic_helper_crtc_destroy_state(crtc->state);
439
440 kfree(vmw_crtc_state_to_vcs(crtc->state));
441 }
442
443 vcs = kzalloc(sizeof(*vcs), GFP_KERNEL);
444
445 if (!vcs) {
446 DRM_ERROR("Cannot allocate vmw_crtc_state\n");
447 return;
448 }
449
450 crtc->state = &vcs->base;
451 crtc->state->crtc = crtc;
452}
453
454
455/**
456 * vmw_du_crtc_destroy_state - destroy crtc state
457 * @crtc: DRM crtc
458 * @state: state object to destroy
459 *
460 * Destroys the crtc state (both common and vmw-specific) for the
461 * specified plane.
462 */
463void
464vmw_du_crtc_destroy_state(struct drm_crtc *crtc,
465 struct drm_crtc_state *state)
466{
467 drm_atomic_helper_crtc_destroy_state(crtc, state);
468}
469
470
fb1d9738
JB
471/*
472 * Generic framebuffer code
473 */
474
fb1d9738
JB
475/*
476 * Surface framebuffer code
477 */
478
847c5964 479static void vmw_framebuffer_surface_destroy(struct drm_framebuffer *framebuffer)
fb1d9738 480{
3a939a5e 481 struct vmw_framebuffer_surface *vfbs =
fb1d9738 482 vmw_framebuffer_to_vfbs(framebuffer);
3a939a5e 483
fb1d9738 484 drm_framebuffer_cleanup(framebuffer);
3a939a5e 485 vmw_surface_unreference(&vfbs->surface);
a278724a
TH
486 if (vfbs->base.user_obj)
487 ttm_base_object_unref(&vfbs->base.user_obj);
fb1d9738 488
3a939a5e 489 kfree(vfbs);
fb1d9738
JB
490}
491
847c5964 492static int vmw_framebuffer_surface_dirty(struct drm_framebuffer *framebuffer,
02b00162 493 struct drm_file *file_priv,
fb1d9738
JB
494 unsigned flags, unsigned color,
495 struct drm_clip_rect *clips,
496 unsigned num_clips)
497{
498 struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
499 struct vmw_framebuffer_surface *vfbs =
500 vmw_framebuffer_to_vfbs(framebuffer);
fb1d9738 501 struct drm_clip_rect norect;
5deb65cf 502 int ret, inc = 1;
fb1d9738 503
c8261a96
SY
504 /* Legacy Display Unit does not support 3D */
505 if (dev_priv->active_display_unit == vmw_du_legacy)
01e81419
JB
506 return -EINVAL;
507
73e9efd4
VS
508 drm_modeset_lock_all(dev_priv->dev);
509
294adf7d 510 ret = ttm_read_lock(&dev_priv->reservation_sem, true);
73e9efd4
VS
511 if (unlikely(ret != 0)) {
512 drm_modeset_unlock_all(dev_priv->dev);
3a939a5e 513 return ret;
73e9efd4 514 }
3a939a5e 515
fb1d9738
JB
516 if (!num_clips) {
517 num_clips = 1;
518 clips = &norect;
519 norect.x1 = norect.y1 = 0;
520 norect.x2 = framebuffer->width;
521 norect.y2 = framebuffer->height;
522 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
523 num_clips /= 2;
524 inc = 2; /* skip source rects */
525 }
526
c8261a96 527 if (dev_priv->active_display_unit == vmw_du_screen_object)
10b1e0ca
TH
528 ret = vmw_kms_sou_do_surface_dirty(dev_priv, &vfbs->base,
529 clips, NULL, NULL, 0, 0,
530 num_clips, inc, NULL);
35c05125 531 else
6bf6bf03
TH
532 ret = vmw_kms_stdu_surface_dirty(dev_priv, &vfbs->base,
533 clips, NULL, NULL, 0, 0,
534 num_clips, inc, NULL);
fb1d9738 535
3eab3d9e 536 vmw_fifo_flush(dev_priv, false);
294adf7d 537 ttm_read_unlock(&dev_priv->reservation_sem);
73e9efd4
VS
538
539 drm_modeset_unlock_all(dev_priv->dev);
540
fb1d9738
JB
541 return 0;
542}
543
10b1e0ca
TH
544/**
545 * vmw_kms_readback - Perform a readback from the screen system to
546 * a dma-buffer backed framebuffer.
547 *
548 * @dev_priv: Pointer to the device private structure.
549 * @file_priv: Pointer to a struct drm_file identifying the caller.
550 * Must be set to NULL if @user_fence_rep is NULL.
551 * @vfb: Pointer to the dma-buffer backed framebuffer.
552 * @user_fence_rep: User-space provided structure for fence information.
553 * Must be set to non-NULL if @file_priv is non-NULL.
554 * @vclips: Array of clip rects.
555 * @num_clips: Number of clip rects in @vclips.
556 *
557 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
558 * interrupted.
559 */
560int vmw_kms_readback(struct vmw_private *dev_priv,
561 struct drm_file *file_priv,
562 struct vmw_framebuffer *vfb,
563 struct drm_vmw_fence_rep __user *user_fence_rep,
564 struct drm_vmw_rect *vclips,
565 uint32_t num_clips)
566{
567 switch (dev_priv->active_display_unit) {
568 case vmw_du_screen_object:
569 return vmw_kms_sou_readback(dev_priv, file_priv, vfb,
570 user_fence_rep, vclips, num_clips);
6bf6bf03
TH
571 case vmw_du_screen_target:
572 return vmw_kms_stdu_dma(dev_priv, file_priv, vfb,
573 user_fence_rep, NULL, vclips, num_clips,
574 1, false, true);
10b1e0ca
TH
575 default:
576 WARN_ONCE(true,
577 "Readback called with invalid display system.\n");
6bf6bf03 578}
10b1e0ca
TH
579
580 return -ENOSYS;
581}
582
583
d7955fcf 584static const struct drm_framebuffer_funcs vmw_framebuffer_surface_funcs = {
fb1d9738
JB
585 .destroy = vmw_framebuffer_surface_destroy,
586 .dirty = vmw_framebuffer_surface_dirty,
fb1d9738
JB
587};
588
d3216a0c
TH
589static int vmw_kms_new_framebuffer_surface(struct vmw_private *dev_priv,
590 struct vmw_surface *surface,
591 struct vmw_framebuffer **out,
dabdcdc9 592 const struct drm_mode_fb_cmd2
f89c6c32
SY
593 *mode_cmd,
594 bool is_dmabuf_proxy)
fb1d9738
JB
595
596{
597 struct drm_device *dev = dev_priv->dev;
598 struct vmw_framebuffer_surface *vfbs;
d3216a0c 599 enum SVGA3dSurfaceFormat format;
fb1d9738 600 int ret;
dabdcdc9 601 struct drm_format_name_buf format_name;
fb1d9738 602
c8261a96
SY
603 /* 3D is only supported on HWv8 and newer hosts */
604 if (dev_priv->active_display_unit == vmw_du_legacy)
01e81419
JB
605 return -ENOSYS;
606
d3216a0c
TH
607 /*
608 * Sanity checks.
609 */
610
e7ac9211
JB
611 /* Surface must be marked as a scanout. */
612 if (unlikely(!surface->scanout))
613 return -EINVAL;
614
d3216a0c
TH
615 if (unlikely(surface->mip_levels[0] != 1 ||
616 surface->num_sizes != 1 ||
b360a3ce
TH
617 surface->base_size.width < mode_cmd->width ||
618 surface->base_size.height < mode_cmd->height ||
619 surface->base_size.depth != 1)) {
d3216a0c
TH
620 DRM_ERROR("Incompatible surface dimensions "
621 "for requested mode.\n");
622 return -EINVAL;
623 }
624
dabdcdc9
DV
625 switch (mode_cmd->pixel_format) {
626 case DRM_FORMAT_ARGB8888:
d3216a0c
TH
627 format = SVGA3D_A8R8G8B8;
628 break;
dabdcdc9 629 case DRM_FORMAT_XRGB8888:
d3216a0c
TH
630 format = SVGA3D_X8R8G8B8;
631 break;
dabdcdc9 632 case DRM_FORMAT_RGB565:
d3216a0c
TH
633 format = SVGA3D_R5G6B5;
634 break;
dabdcdc9 635 case DRM_FORMAT_XRGB1555:
d3216a0c
TH
636 format = SVGA3D_A1R5G5B5;
637 break;
638 default:
dabdcdc9
DV
639 DRM_ERROR("Invalid pixel format: %s\n",
640 drm_get_format_name(mode_cmd->pixel_format, &format_name));
d3216a0c
TH
641 return -EINVAL;
642 }
643
d80efd5c
TH
644 /*
645 * For DX, surface format validation is done when surface->scanout
646 * is set.
647 */
648 if (!dev_priv->has_dx && format != surface->format) {
d3216a0c
TH
649 DRM_ERROR("Invalid surface format for requested mode.\n");
650 return -EINVAL;
651 }
652
fb1d9738
JB
653 vfbs = kzalloc(sizeof(*vfbs), GFP_KERNEL);
654 if (!vfbs) {
655 ret = -ENOMEM;
656 goto out_err1;
657 }
658
a3f913ca 659 drm_helper_mode_fill_fb_struct(dev, &vfbs->base.base, mode_cmd);
05c95018 660 vfbs->surface = vmw_surface_reference(surface);
dabdcdc9 661 vfbs->base.user_handle = mode_cmd->handles[0];
f89c6c32 662 vfbs->is_dmabuf_proxy = is_dmabuf_proxy;
3a939a5e 663
fb1d9738
JB
664 *out = &vfbs->base;
665
80f0b5af
DV
666 ret = drm_framebuffer_init(dev, &vfbs->base.base,
667 &vmw_framebuffer_surface_funcs);
668 if (ret)
05c95018 669 goto out_err2;
80f0b5af 670
fb1d9738
JB
671 return 0;
672
fb1d9738 673out_err2:
05c95018 674 vmw_surface_unreference(&surface);
fb1d9738
JB
675 kfree(vfbs);
676out_err1:
677 return ret;
678}
679
680/*
681 * Dmabuf framebuffer code
682 */
683
847c5964 684static void vmw_framebuffer_dmabuf_destroy(struct drm_framebuffer *framebuffer)
fb1d9738
JB
685{
686 struct vmw_framebuffer_dmabuf *vfbd =
687 vmw_framebuffer_to_vfbd(framebuffer);
688
689 drm_framebuffer_cleanup(framebuffer);
690 vmw_dmabuf_unreference(&vfbd->buffer);
a278724a
TH
691 if (vfbd->base.user_obj)
692 ttm_base_object_unref(&vfbd->base.user_obj);
fb1d9738
JB
693
694 kfree(vfbd);
695}
696
847c5964 697static int vmw_framebuffer_dmabuf_dirty(struct drm_framebuffer *framebuffer,
02b00162 698 struct drm_file *file_priv,
fb1d9738
JB
699 unsigned flags, unsigned color,
700 struct drm_clip_rect *clips,
701 unsigned num_clips)
702{
703 struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
5deb65cf
JB
704 struct vmw_framebuffer_dmabuf *vfbd =
705 vmw_framebuffer_to_vfbd(framebuffer);
fb1d9738 706 struct drm_clip_rect norect;
5deb65cf 707 int ret, increment = 1;
fb1d9738 708
73e9efd4
VS
709 drm_modeset_lock_all(dev_priv->dev);
710
294adf7d 711 ret = ttm_read_lock(&dev_priv->reservation_sem, true);
73e9efd4
VS
712 if (unlikely(ret != 0)) {
713 drm_modeset_unlock_all(dev_priv->dev);
3a939a5e 714 return ret;
73e9efd4 715 }
3a939a5e 716
df1c93ba 717 if (!num_clips) {
fb1d9738
JB
718 num_clips = 1;
719 clips = &norect;
720 norect.x1 = norect.y1 = 0;
721 norect.x2 = framebuffer->width;
722 norect.y2 = framebuffer->height;
723 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
724 num_clips /= 2;
725 increment = 2;
726 }
727
6bf6bf03
TH
728 switch (dev_priv->active_display_unit) {
729 case vmw_du_screen_target:
730 ret = vmw_kms_stdu_dma(dev_priv, NULL, &vfbd->base, NULL,
731 clips, NULL, num_clips, increment,
732 true, true);
733 break;
734 case vmw_du_screen_object:
10b1e0ca 735 ret = vmw_kms_sou_do_dmabuf_dirty(dev_priv, &vfbd->base,
897b8180
TH
736 clips, NULL, num_clips,
737 increment, true, NULL);
6bf6bf03 738 break;
352b20dc
TH
739 case vmw_du_legacy:
740 ret = vmw_kms_ldu_do_dmabuf_dirty(dev_priv, &vfbd->base, 0, 0,
741 clips, num_clips, increment);
742 break;
6bf6bf03 743 default:
352b20dc
TH
744 ret = -EINVAL;
745 WARN_ONCE(true, "Dirty called with invalid display system.\n");
6bf6bf03 746 break;
56d1c78d 747 }
fb1d9738 748
3eab3d9e 749 vmw_fifo_flush(dev_priv, false);
294adf7d 750 ttm_read_unlock(&dev_priv->reservation_sem);
73e9efd4
VS
751
752 drm_modeset_unlock_all(dev_priv->dev);
753
5deb65cf 754 return ret;
fb1d9738
JB
755}
756
d7955fcf 757static const struct drm_framebuffer_funcs vmw_framebuffer_dmabuf_funcs = {
fb1d9738
JB
758 .destroy = vmw_framebuffer_dmabuf_destroy,
759 .dirty = vmw_framebuffer_dmabuf_dirty,
fb1d9738
JB
760};
761
497a3ff9
JB
762/**
763 * Pin the dmabuffer to the start of vram.
764 */
fd006a43 765static int vmw_framebuffer_pin(struct vmw_framebuffer *vfb)
fb1d9738
JB
766{
767 struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
fd006a43 768 struct vmw_dma_buffer *buf;
fb1d9738
JB
769 int ret;
770
fd006a43
TH
771 buf = vfb->dmabuf ? vmw_framebuffer_to_vfbd(&vfb->base)->buffer :
772 vmw_framebuffer_to_vfbs(&vfb->base)->surface->res.backup;
fb1d9738 773
fd006a43
TH
774 if (!buf)
775 return 0;
fb1d9738 776
fd006a43
TH
777 switch (dev_priv->active_display_unit) {
778 case vmw_du_legacy:
779 vmw_overlay_pause_all(dev_priv);
780 ret = vmw_dmabuf_pin_in_start_of_vram(dev_priv, buf, false);
781 vmw_overlay_resume_all(dev_priv);
782 break;
783 case vmw_du_screen_object:
784 case vmw_du_screen_target:
785 if (vfb->dmabuf)
786 return vmw_dmabuf_pin_in_vram_or_gmr(dev_priv, buf,
787 false);
fb1d9738 788
fd006a43
TH
789 return vmw_dmabuf_pin_in_placement(dev_priv, buf,
790 &vmw_mob_placement, false);
791 default:
792 return -EINVAL;
793 }
316ab13a 794
fd006a43 795 return ret;
fb1d9738
JB
796}
797
fd006a43 798static int vmw_framebuffer_unpin(struct vmw_framebuffer *vfb)
fb1d9738
JB
799{
800 struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
fd006a43 801 struct vmw_dma_buffer *buf;
fb1d9738 802
fd006a43
TH
803 buf = vfb->dmabuf ? vmw_framebuffer_to_vfbd(&vfb->base)->buffer :
804 vmw_framebuffer_to_vfbs(&vfb->base)->surface->res.backup;
fb1d9738 805
fd006a43 806 if (WARN_ON(!buf))
fb1d9738 807 return 0;
fb1d9738 808
fd006a43 809 return vmw_dmabuf_unpin(dev_priv, buf, false);
fb1d9738
JB
810}
811
f89c6c32
SY
812/**
813 * vmw_create_dmabuf_proxy - create a proxy surface for the DMA buf
814 *
815 * @dev: DRM device
816 * @mode_cmd: parameters for the new surface
817 * @dmabuf_mob: MOB backing the DMA buf
818 * @srf_out: newly created surface
819 *
820 * When the content FB is a DMA buf, we create a surface as a proxy to the
821 * same buffer. This way we can do a surface copy rather than a surface DMA.
822 * This is a more efficient approach
823 *
824 * RETURNS:
825 * 0 on success, error code otherwise
826 */
827static int vmw_create_dmabuf_proxy(struct drm_device *dev,
dabdcdc9 828 const struct drm_mode_fb_cmd2 *mode_cmd,
f89c6c32
SY
829 struct vmw_dma_buffer *dmabuf_mob,
830 struct vmw_surface **srf_out)
831{
832 uint32_t format;
8cd9f251 833 struct drm_vmw_size content_base_size = {0};
6bf6bf03 834 struct vmw_resource *res;
a50e2bf5 835 unsigned int bytes_pp;
dabdcdc9 836 struct drm_format_name_buf format_name;
f89c6c32
SY
837 int ret;
838
dabdcdc9
DV
839 switch (mode_cmd->pixel_format) {
840 case DRM_FORMAT_ARGB8888:
841 case DRM_FORMAT_XRGB8888:
f89c6c32 842 format = SVGA3D_X8R8G8B8;
a50e2bf5 843 bytes_pp = 4;
f89c6c32
SY
844 break;
845
dabdcdc9
DV
846 case DRM_FORMAT_RGB565:
847 case DRM_FORMAT_XRGB1555:
f89c6c32 848 format = SVGA3D_R5G6B5;
a50e2bf5 849 bytes_pp = 2;
f89c6c32
SY
850 break;
851
852 case 8:
853 format = SVGA3D_P8;
a50e2bf5 854 bytes_pp = 1;
f89c6c32
SY
855 break;
856
857 default:
dabdcdc9
DV
858 DRM_ERROR("Invalid framebuffer format %s\n",
859 drm_get_format_name(mode_cmd->pixel_format, &format_name));
f89c6c32
SY
860 return -EINVAL;
861 }
862
dabdcdc9 863 content_base_size.width = mode_cmd->pitches[0] / bytes_pp;
f89c6c32
SY
864 content_base_size.height = mode_cmd->height;
865 content_base_size.depth = 1;
866
867 ret = vmw_surface_gb_priv_define(dev,
868 0, /* kernel visible only */
869 0, /* flags */
870 format,
871 true, /* can be a scanout buffer */
872 1, /* num of mip levels */
873 0,
d80efd5c 874 0,
f89c6c32
SY
875 content_base_size,
876 srf_out);
877 if (ret) {
878 DRM_ERROR("Failed to allocate proxy content buffer\n");
879 return ret;
fb1d9738
JB
880 }
881
6bf6bf03 882 res = &(*srf_out)->res;
f89c6c32 883
6bf6bf03
TH
884 /* Reserve and switch the backing mob. */
885 mutex_lock(&res->dev_priv->cmdbuf_mutex);
886 (void) vmw_resource_reserve(res, false, true);
887 vmw_dmabuf_unreference(&res->backup);
888 res->backup = vmw_dmabuf_reference(dmabuf_mob);
889 res->backup_offset = 0;
d80efd5c 890 vmw_resource_unreserve(res, false, NULL, 0);
6bf6bf03 891 mutex_unlock(&res->dev_priv->cmdbuf_mutex);
f89c6c32 892
6bf6bf03 893 return 0;
fb1d9738
JB
894}
895
f89c6c32
SY
896
897
d3216a0c
TH
898static int vmw_kms_new_framebuffer_dmabuf(struct vmw_private *dev_priv,
899 struct vmw_dma_buffer *dmabuf,
900 struct vmw_framebuffer **out,
dabdcdc9 901 const struct drm_mode_fb_cmd2
d3216a0c 902 *mode_cmd)
fb1d9738
JB
903
904{
905 struct drm_device *dev = dev_priv->dev;
906 struct vmw_framebuffer_dmabuf *vfbd;
d3216a0c 907 unsigned int requested_size;
dabdcdc9 908 struct drm_format_name_buf format_name;
fb1d9738
JB
909 int ret;
910
dabdcdc9 911 requested_size = mode_cmd->height * mode_cmd->pitches[0];
d3216a0c
TH
912 if (unlikely(requested_size > dmabuf->base.num_pages * PAGE_SIZE)) {
913 DRM_ERROR("Screen buffer object size is too small "
914 "for requested mode.\n");
915 return -EINVAL;
916 }
917
c337ada7 918 /* Limited framebuffer color depth support for screen objects */
c8261a96 919 if (dev_priv->active_display_unit == vmw_du_screen_object) {
dabdcdc9
DV
920 switch (mode_cmd->pixel_format) {
921 case DRM_FORMAT_XRGB8888:
922 case DRM_FORMAT_ARGB8888:
923 break;
924 case DRM_FORMAT_XRGB1555:
925 case DRM_FORMAT_RGB565:
926 break;
c337ada7 927 default:
dabdcdc9
DV
928 DRM_ERROR("Invalid pixel format: %s\n",
929 drm_get_format_name(mode_cmd->pixel_format, &format_name));
c337ada7
JB
930 return -EINVAL;
931 }
932 }
933
fb1d9738
JB
934 vfbd = kzalloc(sizeof(*vfbd), GFP_KERNEL);
935 if (!vfbd) {
936 ret = -ENOMEM;
937 goto out_err1;
938 }
939
a3f913ca 940 drm_helper_mode_fill_fb_struct(dev, &vfbd->base.base, mode_cmd);
2fcd5a73 941 vfbd->base.dmabuf = true;
05c95018 942 vfbd->buffer = vmw_dmabuf_reference(dmabuf);
dabdcdc9 943 vfbd->base.user_handle = mode_cmd->handles[0];
fb1d9738
JB
944 *out = &vfbd->base;
945
80f0b5af
DV
946 ret = drm_framebuffer_init(dev, &vfbd->base.base,
947 &vmw_framebuffer_dmabuf_funcs);
948 if (ret)
05c95018 949 goto out_err2;
80f0b5af 950
fb1d9738
JB
951 return 0;
952
fb1d9738 953out_err2:
05c95018 954 vmw_dmabuf_unreference(&dmabuf);
fb1d9738
JB
955 kfree(vfbd);
956out_err1:
957 return ret;
958}
959
fd006a43
TH
960/**
961 * vmw_kms_new_framebuffer - Create a new framebuffer.
962 *
963 * @dev_priv: Pointer to device private struct.
964 * @dmabuf: Pointer to dma buffer to wrap the kms framebuffer around.
965 * Either @dmabuf or @surface must be NULL.
966 * @surface: Pointer to a surface to wrap the kms framebuffer around.
967 * Either @dmabuf or @surface must be NULL.
968 * @only_2d: No presents will occur to this dma buffer based framebuffer. This
969 * Helps the code to do some important optimizations.
970 * @mode_cmd: Frame-buffer metadata.
fb1d9738 971 */
fd006a43
TH
972struct vmw_framebuffer *
973vmw_kms_new_framebuffer(struct vmw_private *dev_priv,
974 struct vmw_dma_buffer *dmabuf,
975 struct vmw_surface *surface,
976 bool only_2d,
dabdcdc9 977 const struct drm_mode_fb_cmd2 *mode_cmd)
fb1d9738 978{
fb1d9738 979 struct vmw_framebuffer *vfb = NULL;
fd006a43 980 bool is_dmabuf_proxy = false;
fb1d9738
JB
981 int ret;
982
fd006a43
TH
983 /*
984 * We cannot use the SurfaceDMA command in an non-accelerated VM,
985 * therefore, wrap the DMA buf in a surface so we can use the
986 * SurfaceCopy command.
987 */
988 if (dmabuf && only_2d &&
989 dev_priv->active_display_unit == vmw_du_screen_target) {
990 ret = vmw_create_dmabuf_proxy(dev_priv->dev, mode_cmd,
991 dmabuf, &surface);
992 if (ret)
993 return ERR_PTR(ret);
994
995 is_dmabuf_proxy = true;
996 }
997
998 /* Create the new framebuffer depending one what we have */
05c95018 999 if (surface) {
fd006a43
TH
1000 ret = vmw_kms_new_framebuffer_surface(dev_priv, surface, &vfb,
1001 mode_cmd,
1002 is_dmabuf_proxy);
05c95018
SY
1003
1004 /*
1005 * vmw_create_dmabuf_proxy() adds a reference that is no longer
1006 * needed
1007 */
1008 if (is_dmabuf_proxy)
1009 vmw_surface_unreference(&surface);
1010 } else if (dmabuf) {
fd006a43
TH
1011 ret = vmw_kms_new_framebuffer_dmabuf(dev_priv, dmabuf, &vfb,
1012 mode_cmd);
05c95018 1013 } else {
fd006a43 1014 BUG();
05c95018 1015 }
fd006a43
TH
1016
1017 if (ret)
1018 return ERR_PTR(ret);
1019
1020 vfb->pin = vmw_framebuffer_pin;
1021 vfb->unpin = vmw_framebuffer_unpin;
1022
1023 return vfb;
1024}
1025
fb1d9738
JB
1026/*
1027 * Generic Kernel modesetting functions
1028 */
1029
1030static struct drm_framebuffer *vmw_kms_fb_create(struct drm_device *dev,
1031 struct drm_file *file_priv,
dabdcdc9 1032 const struct drm_mode_fb_cmd2 *mode_cmd)
fb1d9738
JB
1033{
1034 struct vmw_private *dev_priv = vmw_priv(dev);
1035 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
1036 struct vmw_framebuffer *vfb = NULL;
1037 struct vmw_surface *surface = NULL;
1038 struct vmw_dma_buffer *bo = NULL;
90ff18bc 1039 struct ttm_base_object *user_obj;
fb1d9738
JB
1040 int ret;
1041
d3216a0c
TH
1042 /**
1043 * This code should be conditioned on Screen Objects not being used.
1044 * If screen objects are used, we can allocate a GMR to hold the
1045 * requested framebuffer.
1046 */
1047
8a783896 1048 if (!vmw_kms_validate_mode_vram(dev_priv,
dabdcdc9
DV
1049 mode_cmd->pitches[0],
1050 mode_cmd->height)) {
c8261a96 1051 DRM_ERROR("Requested mode exceed bounding box limit.\n");
d9826409 1052 return ERR_PTR(-ENOMEM);
d3216a0c
TH
1053 }
1054
90ff18bc
TH
1055 /*
1056 * Take a reference on the user object of the resource
1057 * backing the kms fb. This ensures that user-space handle
1058 * lookups on that resource will always work as long as
1059 * it's registered with a kms framebuffer. This is important,
1060 * since vmw_execbuf_process identifies resources in the
1061 * command stream using user-space handles.
1062 */
1063
dabdcdc9 1064 user_obj = ttm_base_object_lookup(tfile, mode_cmd->handles[0]);
90ff18bc
TH
1065 if (unlikely(user_obj == NULL)) {
1066 DRM_ERROR("Could not locate requested kms frame buffer.\n");
1067 return ERR_PTR(-ENOENT);
1068 }
1069
d3216a0c
TH
1070 /**
1071 * End conditioned code.
1072 */
1073
e7ac9211
JB
1074 /* returns either a dmabuf or surface */
1075 ret = vmw_user_lookup_handle(dev_priv, tfile,
dabdcdc9 1076 mode_cmd->handles[0],
e7ac9211 1077 &surface, &bo);
fb1d9738 1078 if (ret)
e7ac9211
JB
1079 goto err_out;
1080
fd006a43
TH
1081 vfb = vmw_kms_new_framebuffer(dev_priv, bo, surface,
1082 !(dev_priv->capabilities & SVGA_CAP_3D),
dabdcdc9 1083 mode_cmd);
fd006a43
TH
1084 if (IS_ERR(vfb)) {
1085 ret = PTR_ERR(vfb);
1086 goto err_out;
1087 }
e7ac9211
JB
1088
1089err_out:
1090 /* vmw_user_lookup_handle takes one ref so does new_fb */
1091 if (bo)
1092 vmw_dmabuf_unreference(&bo);
1093 if (surface)
1094 vmw_surface_unreference(&surface);
fb1d9738
JB
1095
1096 if (ret) {
1097 DRM_ERROR("failed to create vmw_framebuffer: %i\n", ret);
90ff18bc 1098 ttm_base_object_unref(&user_obj);
cce13ff7 1099 return ERR_PTR(ret);
90ff18bc
TH
1100 } else
1101 vfb->user_obj = user_obj;
fb1d9738
JB
1102
1103 return &vfb->base;
1104}
1105
e6ecefaa 1106static const struct drm_mode_config_funcs vmw_kms_funcs = {
fb1d9738 1107 .fb_create = vmw_kms_fb_create,
fb1d9738
JB
1108};
1109
b9eb1a61
TH
1110static int vmw_kms_generic_present(struct vmw_private *dev_priv,
1111 struct drm_file *file_priv,
1112 struct vmw_framebuffer *vfb,
1113 struct vmw_surface *surface,
1114 uint32_t sid,
1115 int32_t destX, int32_t destY,
1116 struct drm_vmw_rect *clips,
1117 uint32_t num_clips)
2fcd5a73 1118{
10b1e0ca
TH
1119 return vmw_kms_sou_do_surface_dirty(dev_priv, vfb, NULL, clips,
1120 &surface->res, destX, destY,
1121 num_clips, 1, NULL);
2fcd5a73
JB
1122}
1123
6bf6bf03 1124
2fcd5a73
JB
1125int vmw_kms_present(struct vmw_private *dev_priv,
1126 struct drm_file *file_priv,
1127 struct vmw_framebuffer *vfb,
1128 struct vmw_surface *surface,
1129 uint32_t sid,
1130 int32_t destX, int32_t destY,
1131 struct drm_vmw_rect *clips,
1132 uint32_t num_clips)
1133{
35c05125 1134 int ret;
2fcd5a73 1135
6bf6bf03
TH
1136 switch (dev_priv->active_display_unit) {
1137 case vmw_du_screen_target:
1138 ret = vmw_kms_stdu_surface_dirty(dev_priv, vfb, NULL, clips,
1139 &surface->res, destX, destY,
1140 num_clips, 1, NULL);
1141 break;
1142 case vmw_du_screen_object:
1143 ret = vmw_kms_generic_present(dev_priv, file_priv, vfb, surface,
1144 sid, destX, destY, clips,
1145 num_clips);
1146 break;
1147 default:
1148 WARN_ONCE(true,
1149 "Present called with invalid display system.\n");
1150 ret = -ENOSYS;
1151 break;
2fcd5a73 1152 }
35c05125
SY
1153 if (ret)
1154 return ret;
2fcd5a73 1155
35c05125 1156 vmw_fifo_flush(dev_priv, false);
2fcd5a73 1157
35c05125 1158 return 0;
2fcd5a73
JB
1159}
1160
578e609a
TH
1161static void
1162vmw_kms_create_hotplug_mode_update_property(struct vmw_private *dev_priv)
1163{
1164 if (dev_priv->hotplug_mode_update_property)
1165 return;
1166
1167 dev_priv->hotplug_mode_update_property =
1168 drm_property_create_range(dev_priv->dev,
1169 DRM_MODE_PROP_IMMUTABLE,
1170 "hotplug_mode_update", 0, 1);
1171
1172 if (!dev_priv->hotplug_mode_update_property)
1173 return;
1174
1175}
1176
fb1d9738
JB
1177int vmw_kms_init(struct vmw_private *dev_priv)
1178{
1179 struct drm_device *dev = dev_priv->dev;
1180 int ret;
1181
1182 drm_mode_config_init(dev);
1183 dev->mode_config.funcs = &vmw_kms_funcs;
3bef3572
JB
1184 dev->mode_config.min_width = 1;
1185 dev->mode_config.min_height = 1;
65ade7d3
SY
1186 dev->mode_config.max_width = dev_priv->texture_max_width;
1187 dev->mode_config.max_height = dev_priv->texture_max_height;
fb1d9738 1188
578e609a
TH
1189 drm_mode_create_suggested_offset_properties(dev);
1190 vmw_kms_create_hotplug_mode_update_property(dev_priv);
1191
35c05125
SY
1192 ret = vmw_kms_stdu_init_display(dev_priv);
1193 if (ret) {
1194 ret = vmw_kms_sou_init_display(dev_priv);
1195 if (ret) /* Fallback */
1196 ret = vmw_kms_ldu_init_display(dev_priv);
1197 }
fb1d9738 1198
c8261a96 1199 return ret;
fb1d9738
JB
1200}
1201
1202int vmw_kms_close(struct vmw_private *dev_priv)
1203{
c8261a96
SY
1204 int ret;
1205
fb1d9738
JB
1206 /*
1207 * Docs says we should take the lock before calling this function
1208 * but since it destroys encoders and our destructor calls
1209 * drm_encoder_cleanup which takes the lock we deadlock.
1210 */
1211 drm_mode_config_cleanup(dev_priv->dev);
c8261a96
SY
1212 if (dev_priv->active_display_unit == vmw_du_screen_object)
1213 ret = vmw_kms_sou_close_display(dev_priv);
35c05125
SY
1214 else if (dev_priv->active_display_unit == vmw_du_screen_target)
1215 ret = vmw_kms_stdu_close_display(dev_priv);
c0d18316 1216 else
c8261a96
SY
1217 ret = vmw_kms_ldu_close_display(dev_priv);
1218
1219 return ret;
fb1d9738
JB
1220}
1221
1222int vmw_kms_cursor_bypass_ioctl(struct drm_device *dev, void *data,
1223 struct drm_file *file_priv)
1224{
1225 struct drm_vmw_cursor_bypass_arg *arg = data;
1226 struct vmw_display_unit *du;
fb1d9738
JB
1227 struct drm_crtc *crtc;
1228 int ret = 0;
1229
1230
1231 mutex_lock(&dev->mode_config.mutex);
1232 if (arg->flags & DRM_VMW_CURSOR_BYPASS_ALL) {
1233
1234 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1235 du = vmw_crtc_to_du(crtc);
1236 du->hotspot_x = arg->xhot;
1237 du->hotspot_y = arg->yhot;
1238 }
1239
1240 mutex_unlock(&dev->mode_config.mutex);
1241 return 0;
1242 }
1243
a4cd5d68
RC
1244 crtc = drm_crtc_find(dev, arg->crtc_id);
1245 if (!crtc) {
4ae87ff0 1246 ret = -ENOENT;
fb1d9738
JB
1247 goto out;
1248 }
1249
fb1d9738
JB
1250 du = vmw_crtc_to_du(crtc);
1251
1252 du->hotspot_x = arg->xhot;
1253 du->hotspot_y = arg->yhot;
1254
1255out:
1256 mutex_unlock(&dev->mode_config.mutex);
1257
1258 return ret;
1259}
1260
0bef23f9 1261int vmw_kms_write_svga(struct vmw_private *vmw_priv,
d7e1958d 1262 unsigned width, unsigned height, unsigned pitch,
6558429b 1263 unsigned bpp, unsigned depth)
fb1d9738 1264{
d7e1958d
JB
1265 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1266 vmw_write(vmw_priv, SVGA_REG_PITCHLOCK, pitch);
1267 else if (vmw_fifo_have_pitchlock(vmw_priv))
b76ff5ea
TH
1268 vmw_mmio_write(pitch, vmw_priv->mmio_virt +
1269 SVGA_FIFO_PITCHLOCK);
d7e1958d
JB
1270 vmw_write(vmw_priv, SVGA_REG_WIDTH, width);
1271 vmw_write(vmw_priv, SVGA_REG_HEIGHT, height);
6558429b 1272 vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, bpp);
0bef23f9
MD
1273
1274 if (vmw_read(vmw_priv, SVGA_REG_DEPTH) != depth) {
1275 DRM_ERROR("Invalid depth %u for %u bpp, host expects %u\n",
1276 depth, bpp, vmw_read(vmw_priv, SVGA_REG_DEPTH));
1277 return -EINVAL;
1278 }
1279
1280 return 0;
d7e1958d 1281}
fb1d9738 1282
d7e1958d
JB
1283int vmw_kms_save_vga(struct vmw_private *vmw_priv)
1284{
7c4f7780
TH
1285 struct vmw_vga_topology_state *save;
1286 uint32_t i;
1287
fb1d9738
JB
1288 vmw_priv->vga_width = vmw_read(vmw_priv, SVGA_REG_WIDTH);
1289 vmw_priv->vga_height = vmw_read(vmw_priv, SVGA_REG_HEIGHT);
7c4f7780 1290 vmw_priv->vga_bpp = vmw_read(vmw_priv, SVGA_REG_BITS_PER_PIXEL);
d7e1958d
JB
1291 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1292 vmw_priv->vga_pitchlock =
7c4f7780 1293 vmw_read(vmw_priv, SVGA_REG_PITCHLOCK);
d7e1958d 1294 else if (vmw_fifo_have_pitchlock(vmw_priv))
b76ff5ea
TH
1295 vmw_priv->vga_pitchlock = vmw_mmio_read(vmw_priv->mmio_virt +
1296 SVGA_FIFO_PITCHLOCK);
7c4f7780
TH
1297
1298 if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
1299 return 0;
fb1d9738 1300
7c4f7780
TH
1301 vmw_priv->num_displays = vmw_read(vmw_priv,
1302 SVGA_REG_NUM_GUEST_DISPLAYS);
1303
029e50bf
TH
1304 if (vmw_priv->num_displays == 0)
1305 vmw_priv->num_displays = 1;
1306
7c4f7780
TH
1307 for (i = 0; i < vmw_priv->num_displays; ++i) {
1308 save = &vmw_priv->vga_save[i];
1309 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
1310 save->primary = vmw_read(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY);
1311 save->pos_x = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_X);
1312 save->pos_y = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y);
1313 save->width = vmw_read(vmw_priv, SVGA_REG_DISPLAY_WIDTH);
1314 save->height = vmw_read(vmw_priv, SVGA_REG_DISPLAY_HEIGHT);
1315 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
30c78bb8
TH
1316 if (i == 0 && vmw_priv->num_displays == 1 &&
1317 save->width == 0 && save->height == 0) {
1318
1319 /*
1320 * It should be fairly safe to assume that these
1321 * values are uninitialized.
1322 */
1323
1324 save->width = vmw_priv->vga_width - save->pos_x;
1325 save->height = vmw_priv->vga_height - save->pos_y;
1326 }
7c4f7780 1327 }
30c78bb8 1328
fb1d9738
JB
1329 return 0;
1330}
1331
1332int vmw_kms_restore_vga(struct vmw_private *vmw_priv)
1333{
7c4f7780
TH
1334 struct vmw_vga_topology_state *save;
1335 uint32_t i;
1336
fb1d9738
JB
1337 vmw_write(vmw_priv, SVGA_REG_WIDTH, vmw_priv->vga_width);
1338 vmw_write(vmw_priv, SVGA_REG_HEIGHT, vmw_priv->vga_height);
7c4f7780 1339 vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, vmw_priv->vga_bpp);
d7e1958d
JB
1340 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1341 vmw_write(vmw_priv, SVGA_REG_PITCHLOCK,
1342 vmw_priv->vga_pitchlock);
1343 else if (vmw_fifo_have_pitchlock(vmw_priv))
b76ff5ea
TH
1344 vmw_mmio_write(vmw_priv->vga_pitchlock,
1345 vmw_priv->mmio_virt + SVGA_FIFO_PITCHLOCK);
fb1d9738 1346
7c4f7780
TH
1347 if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
1348 return 0;
1349
1350 for (i = 0; i < vmw_priv->num_displays; ++i) {
1351 save = &vmw_priv->vga_save[i];
1352 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
1353 vmw_write(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY, save->primary);
1354 vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_X, save->pos_x);
1355 vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y, save->pos_y);
1356 vmw_write(vmw_priv, SVGA_REG_DISPLAY_WIDTH, save->width);
1357 vmw_write(vmw_priv, SVGA_REG_DISPLAY_HEIGHT, save->height);
1358 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
1359 }
1360
fb1d9738
JB
1361 return 0;
1362}
d8bd19d2 1363
e133e737
TH
1364bool vmw_kms_validate_mode_vram(struct vmw_private *dev_priv,
1365 uint32_t pitch,
1366 uint32_t height)
1367{
35c05125
SY
1368 return ((u64) pitch * (u64) height) < (u64)
1369 ((dev_priv->active_display_unit == vmw_du_screen_target) ?
1370 dev_priv->prim_bb_mem : dev_priv->vram_size);
e133e737
TH
1371}
1372
1c482ab3
JB
1373
1374/**
1375 * Function called by DRM code called with vbl_lock held.
1376 */
88e72717 1377u32 vmw_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
7a1c2f6c
TH
1378{
1379 return 0;
1380}
626ab771 1381
1c482ab3
JB
1382/**
1383 * Function called by DRM code called with vbl_lock held.
1384 */
88e72717 1385int vmw_enable_vblank(struct drm_device *dev, unsigned int pipe)
1c482ab3
JB
1386{
1387 return -ENOSYS;
1388}
1389
1390/**
1391 * Function called by DRM code called with vbl_lock held.
1392 */
88e72717 1393void vmw_disable_vblank(struct drm_device *dev, unsigned int pipe)
1c482ab3
JB
1394{
1395}
1396
626ab771
JB
1397
1398/*
1399 * Small shared kms functions.
1400 */
1401
847c5964 1402static int vmw_du_update_layout(struct vmw_private *dev_priv, unsigned num,
626ab771
JB
1403 struct drm_vmw_rect *rects)
1404{
1405 struct drm_device *dev = dev_priv->dev;
1406 struct vmw_display_unit *du;
1407 struct drm_connector *con;
626ab771
JB
1408
1409 mutex_lock(&dev->mode_config.mutex);
1410
1411#if 0
6ea77d13
TH
1412 {
1413 unsigned int i;
1414
1415 DRM_INFO("%s: new layout ", __func__);
1416 for (i = 0; i < num; i++)
1417 DRM_INFO("(%i, %i %ux%u) ", rects[i].x, rects[i].y,
1418 rects[i].w, rects[i].h);
1419 DRM_INFO("\n");
1420 }
626ab771
JB
1421#endif
1422
1423 list_for_each_entry(con, &dev->mode_config.connector_list, head) {
1424 du = vmw_connector_to_du(con);
1425 if (num > du->unit) {
1426 du->pref_width = rects[du->unit].w;
1427 du->pref_height = rects[du->unit].h;
1428 du->pref_active = true;
cd2b89e7
TH
1429 du->gui_x = rects[du->unit].x;
1430 du->gui_y = rects[du->unit].y;
578e609a
TH
1431 drm_object_property_set_value
1432 (&con->base, dev->mode_config.suggested_x_property,
1433 du->gui_x);
1434 drm_object_property_set_value
1435 (&con->base, dev->mode_config.suggested_y_property,
1436 du->gui_y);
626ab771
JB
1437 } else {
1438 du->pref_width = 800;
1439 du->pref_height = 600;
1440 du->pref_active = false;
578e609a
TH
1441 drm_object_property_set_value
1442 (&con->base, dev->mode_config.suggested_x_property,
1443 0);
1444 drm_object_property_set_value
1445 (&con->base, dev->mode_config.suggested_y_property,
1446 0);
626ab771
JB
1447 }
1448 con->status = vmw_du_connector_detect(con, true);
1449 }
1450
1451 mutex_unlock(&dev->mode_config.mutex);
578e609a 1452 drm_sysfs_hotplug_event(dev);
626ab771
JB
1453
1454 return 0;
1455}
1456
7ea77283
ML
1457int vmw_du_crtc_gamma_set(struct drm_crtc *crtc,
1458 u16 *r, u16 *g, u16 *b,
1459 uint32_t size)
626ab771
JB
1460{
1461 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
1462 int i;
1463
1464 for (i = 0; i < size; i++) {
1465 DRM_DEBUG("%d r/g/b = 0x%04x / 0x%04x / 0x%04x\n", i,
1466 r[i], g[i], b[i]);
1467 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 0, r[i] >> 8);
1468 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 1, g[i] >> 8);
1469 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 2, b[i] >> 8);
1470 }
7ea77283
ML
1471
1472 return 0;
626ab771
JB
1473}
1474
9a69a9ac 1475int vmw_du_connector_dpms(struct drm_connector *connector, int mode)
626ab771 1476{
9a69a9ac 1477 return 0;
626ab771
JB
1478}
1479
626ab771
JB
1480enum drm_connector_status
1481vmw_du_connector_detect(struct drm_connector *connector, bool force)
1482{
1483 uint32_t num_displays;
1484 struct drm_device *dev = connector->dev;
1485 struct vmw_private *dev_priv = vmw_priv(dev);
cd2b89e7 1486 struct vmw_display_unit *du = vmw_connector_to_du(connector);
626ab771 1487
626ab771 1488 num_displays = vmw_read(dev_priv, SVGA_REG_NUM_DISPLAYS);
626ab771 1489
cd2b89e7
TH
1490 return ((vmw_connector_to_du(connector)->unit < num_displays &&
1491 du->pref_active) ?
626ab771
JB
1492 connector_status_connected : connector_status_disconnected);
1493}
1494
1495static struct drm_display_mode vmw_kms_connector_builtin[] = {
1496 /* 640x480@60Hz */
1497 { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 25175, 640, 656,
1498 752, 800, 0, 480, 489, 492, 525, 0,
1499 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
1500 /* 800x600@60Hz */
1501 { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 40000, 800, 840,
1502 968, 1056, 0, 600, 601, 605, 628, 0,
1503 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1504 /* 1024x768@60Hz */
1505 { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 65000, 1024, 1048,
1506 1184, 1344, 0, 768, 771, 777, 806, 0,
1507 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
1508 /* 1152x864@75Hz */
1509 { DRM_MODE("1152x864", DRM_MODE_TYPE_DRIVER, 108000, 1152, 1216,
1510 1344, 1600, 0, 864, 865, 868, 900, 0,
1511 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1512 /* 1280x768@60Hz */
1513 { DRM_MODE("1280x768", DRM_MODE_TYPE_DRIVER, 79500, 1280, 1344,
1514 1472, 1664, 0, 768, 771, 778, 798, 0,
1515 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1516 /* 1280x800@60Hz */
1517 { DRM_MODE("1280x800", DRM_MODE_TYPE_DRIVER, 83500, 1280, 1352,
1518 1480, 1680, 0, 800, 803, 809, 831, 0,
1519 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
1520 /* 1280x960@60Hz */
1521 { DRM_MODE("1280x960", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1376,
1522 1488, 1800, 0, 960, 961, 964, 1000, 0,
1523 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1524 /* 1280x1024@60Hz */
1525 { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1328,
1526 1440, 1688, 0, 1024, 1025, 1028, 1066, 0,
1527 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1528 /* 1360x768@60Hz */
1529 { DRM_MODE("1360x768", DRM_MODE_TYPE_DRIVER, 85500, 1360, 1424,
1530 1536, 1792, 0, 768, 771, 777, 795, 0,
1531 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1532 /* 1440x1050@60Hz */
1533 { DRM_MODE("1400x1050", DRM_MODE_TYPE_DRIVER, 121750, 1400, 1488,
1534 1632, 1864, 0, 1050, 1053, 1057, 1089, 0,
1535 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1536 /* 1440x900@60Hz */
1537 { DRM_MODE("1440x900", DRM_MODE_TYPE_DRIVER, 106500, 1440, 1520,
1538 1672, 1904, 0, 900, 903, 909, 934, 0,
1539 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1540 /* 1600x1200@60Hz */
1541 { DRM_MODE("1600x1200", DRM_MODE_TYPE_DRIVER, 162000, 1600, 1664,
1542 1856, 2160, 0, 1200, 1201, 1204, 1250, 0,
1543 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1544 /* 1680x1050@60Hz */
1545 { DRM_MODE("1680x1050", DRM_MODE_TYPE_DRIVER, 146250, 1680, 1784,
1546 1960, 2240, 0, 1050, 1053, 1059, 1089, 0,
1547 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1548 /* 1792x1344@60Hz */
1549 { DRM_MODE("1792x1344", DRM_MODE_TYPE_DRIVER, 204750, 1792, 1920,
1550 2120, 2448, 0, 1344, 1345, 1348, 1394, 0,
1551 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1552 /* 1853x1392@60Hz */
1553 { DRM_MODE("1856x1392", DRM_MODE_TYPE_DRIVER, 218250, 1856, 1952,
1554 2176, 2528, 0, 1392, 1393, 1396, 1439, 0,
1555 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1556 /* 1920x1200@60Hz */
1557 { DRM_MODE("1920x1200", DRM_MODE_TYPE_DRIVER, 193250, 1920, 2056,
1558 2256, 2592, 0, 1200, 1203, 1209, 1245, 0,
1559 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1560 /* 1920x1440@60Hz */
1561 { DRM_MODE("1920x1440", DRM_MODE_TYPE_DRIVER, 234000, 1920, 2048,
1562 2256, 2600, 0, 1440, 1441, 1444, 1500, 0,
1563 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1564 /* 2560x1600@60Hz */
1565 { DRM_MODE("2560x1600", DRM_MODE_TYPE_DRIVER, 348500, 2560, 2752,
1566 3032, 3504, 0, 1600, 1603, 1609, 1658, 0,
1567 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1568 /* Terminate */
1569 { DRM_MODE("", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) },
1570};
1571
1543b4dd
TH
1572/**
1573 * vmw_guess_mode_timing - Provide fake timings for a
1574 * 60Hz vrefresh mode.
1575 *
1576 * @mode - Pointer to a struct drm_display_mode with hdisplay and vdisplay
1577 * members filled in.
1578 */
a278724a 1579void vmw_guess_mode_timing(struct drm_display_mode *mode)
1543b4dd
TH
1580{
1581 mode->hsync_start = mode->hdisplay + 50;
1582 mode->hsync_end = mode->hsync_start + 50;
1583 mode->htotal = mode->hsync_end + 50;
1584
1585 mode->vsync_start = mode->vdisplay + 50;
1586 mode->vsync_end = mode->vsync_start + 50;
1587 mode->vtotal = mode->vsync_end + 50;
1588
1589 mode->clock = (u32)mode->htotal * (u32)mode->vtotal / 100 * 6;
1590 mode->vrefresh = drm_mode_vrefresh(mode);
1591}
1592
1593
626ab771
JB
1594int vmw_du_connector_fill_modes(struct drm_connector *connector,
1595 uint32_t max_width, uint32_t max_height)
1596{
1597 struct vmw_display_unit *du = vmw_connector_to_du(connector);
1598 struct drm_device *dev = connector->dev;
1599 struct vmw_private *dev_priv = vmw_priv(dev);
1600 struct drm_display_mode *mode = NULL;
1601 struct drm_display_mode *bmode;
1602 struct drm_display_mode prefmode = { DRM_MODE("preferred",
1603 DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED,
1604 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1605 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC)
1606 };
1607 int i;
7c20d213 1608 u32 assumed_bpp = 4;
9a72384d 1609
04319d89
SY
1610 if (dev_priv->assume_16bpp)
1611 assumed_bpp = 2;
626ab771 1612
35c05125
SY
1613 if (dev_priv->active_display_unit == vmw_du_screen_target) {
1614 max_width = min(max_width, dev_priv->stdu_max_width);
1615 max_height = min(max_height, dev_priv->stdu_max_height);
1616 }
1617
626ab771 1618 /* Add preferred mode */
c8261a96
SY
1619 mode = drm_mode_duplicate(dev, &prefmode);
1620 if (!mode)
1621 return 0;
1622 mode->hdisplay = du->pref_width;
1623 mode->vdisplay = du->pref_height;
1624 vmw_guess_mode_timing(mode);
626ab771 1625
c8261a96
SY
1626 if (vmw_kms_validate_mode_vram(dev_priv,
1627 mode->hdisplay * assumed_bpp,
1628 mode->vdisplay)) {
1629 drm_mode_probed_add(connector, mode);
1630 } else {
1631 drm_mode_destroy(dev, mode);
1632 mode = NULL;
1633 }
55bde5b2 1634
c8261a96
SY
1635 if (du->pref_mode) {
1636 list_del_init(&du->pref_mode->head);
1637 drm_mode_destroy(dev, du->pref_mode);
626ab771
JB
1638 }
1639
c8261a96
SY
1640 /* mode might be null here, this is intended */
1641 du->pref_mode = mode;
1642
626ab771
JB
1643 for (i = 0; vmw_kms_connector_builtin[i].type != 0; i++) {
1644 bmode = &vmw_kms_connector_builtin[i];
1645 if (bmode->hdisplay > max_width ||
1646 bmode->vdisplay > max_height)
1647 continue;
1648
9a72384d
SY
1649 if (!vmw_kms_validate_mode_vram(dev_priv,
1650 bmode->hdisplay * assumed_bpp,
626ab771
JB
1651 bmode->vdisplay))
1652 continue;
1653
1654 mode = drm_mode_duplicate(dev, bmode);
1655 if (!mode)
1656 return 0;
1657 mode->vrefresh = drm_mode_vrefresh(mode);
1658
1659 drm_mode_probed_add(connector, mode);
1660 }
1661
6af3e656 1662 drm_mode_connector_list_update(connector);
f6b05004
TH
1663 /* Move the prefered mode first, help apps pick the right mode. */
1664 drm_mode_sort(&connector->modes);
626ab771
JB
1665
1666 return 1;
1667}
1668
1669int vmw_du_connector_set_property(struct drm_connector *connector,
1670 struct drm_property *property,
1671 uint64_t val)
1672{
76404ac0
TH
1673 struct vmw_display_unit *du = vmw_connector_to_du(connector);
1674 struct vmw_private *dev_priv = vmw_priv(connector->dev);
1675
1676 if (property == dev_priv->implicit_placement_property)
1677 du->is_implicit = val;
1678
626ab771
JB
1679 return 0;
1680}
cd2b89e7
TH
1681
1682
9c2542a4 1683
cd2b89e7
TH
1684int vmw_kms_update_layout_ioctl(struct drm_device *dev, void *data,
1685 struct drm_file *file_priv)
1686{
1687 struct vmw_private *dev_priv = vmw_priv(dev);
1688 struct drm_vmw_update_layout_arg *arg =
1689 (struct drm_vmw_update_layout_arg *)data;
cd2b89e7
TH
1690 void __user *user_rects;
1691 struct drm_vmw_rect *rects;
1692 unsigned rects_size;
1693 int ret;
1694 int i;
65ade7d3 1695 u64 total_pixels = 0;
cd2b89e7 1696 struct drm_mode_config *mode_config = &dev->mode_config;
c8261a96 1697 struct drm_vmw_rect bounding_box = {0};
cd2b89e7 1698
cd2b89e7
TH
1699 if (!arg->num_outputs) {
1700 struct drm_vmw_rect def_rect = {0, 0, 800, 600};
1701 vmw_du_update_layout(dev_priv, 1, &def_rect);
5151adb3 1702 return 0;
cd2b89e7
TH
1703 }
1704
1705 rects_size = arg->num_outputs * sizeof(struct drm_vmw_rect);
bab9efc2
XW
1706 rects = kcalloc(arg->num_outputs, sizeof(struct drm_vmw_rect),
1707 GFP_KERNEL);
5151adb3
TH
1708 if (unlikely(!rects))
1709 return -ENOMEM;
cd2b89e7
TH
1710
1711 user_rects = (void __user *)(unsigned long)arg->rects;
1712 ret = copy_from_user(rects, user_rects, rects_size);
1713 if (unlikely(ret != 0)) {
1714 DRM_ERROR("Failed to get rects.\n");
1715 ret = -EFAULT;
1716 goto out_free;
1717 }
1718
1719 for (i = 0; i < arg->num_outputs; ++i) {
bab9efc2
XW
1720 if (rects[i].x < 0 ||
1721 rects[i].y < 0 ||
1722 rects[i].x + rects[i].w > mode_config->max_width ||
1723 rects[i].y + rects[i].h > mode_config->max_height) {
cd2b89e7
TH
1724 DRM_ERROR("Invalid GUI layout.\n");
1725 ret = -EINVAL;
1726 goto out_free;
1727 }
c8261a96
SY
1728
1729 /*
1730 * bounding_box.w and bunding_box.h are used as
1731 * lower-right coordinates
1732 */
1733 if (rects[i].x + rects[i].w > bounding_box.w)
1734 bounding_box.w = rects[i].x + rects[i].w;
1735
1736 if (rects[i].y + rects[i].h > bounding_box.h)
1737 bounding_box.h = rects[i].y + rects[i].h;
65ade7d3
SY
1738
1739 total_pixels += (u64) rects[i].w * (u64) rects[i].h;
cd2b89e7
TH
1740 }
1741
65ade7d3
SY
1742 if (dev_priv->active_display_unit == vmw_du_screen_target) {
1743 /*
1744 * For Screen Targets, the limits for a toplogy are:
1745 * 1. Bounding box (assuming 32bpp) must be < prim_bb_mem
1746 * 2. Total pixels (assuming 32bpp) must be < prim_bb_mem
1747 */
0f580386 1748 u64 bb_mem = (u64) bounding_box.w * bounding_box.h * 4;
65ade7d3
SY
1749 u64 pixel_mem = total_pixels * 4;
1750
1751 if (bb_mem > dev_priv->prim_bb_mem) {
1752 DRM_ERROR("Topology is beyond supported limits.\n");
35c05125
SY
1753 ret = -EINVAL;
1754 goto out_free;
1755 }
1756
65ade7d3
SY
1757 if (pixel_mem > dev_priv->prim_bb_mem) {
1758 DRM_ERROR("Combined output size too large\n");
1759 ret = -EINVAL;
1760 goto out_free;
1761 }
cd2b89e7
TH
1762 }
1763
1764 vmw_du_update_layout(dev_priv, arg->num_outputs, rects);
1765
1766out_free:
1767 kfree(rects);
cd2b89e7
TH
1768 return ret;
1769}
1a4b172a
TH
1770
1771/**
1772 * vmw_kms_helper_dirty - Helper to build commands and perform actions based
1773 * on a set of cliprects and a set of display units.
1774 *
1775 * @dev_priv: Pointer to a device private structure.
1776 * @framebuffer: Pointer to the framebuffer on which to perform the actions.
1777 * @clips: A set of struct drm_clip_rect. Either this os @vclips must be NULL.
1778 * Cliprects are given in framebuffer coordinates.
1779 * @vclips: A set of struct drm_vmw_rect cliprects. Either this or @clips must
1780 * be NULL. Cliprects are given in source coordinates.
1781 * @dest_x: X coordinate offset for the crtc / destination clip rects.
1782 * @dest_y: Y coordinate offset for the crtc / destination clip rects.
1783 * @num_clips: Number of cliprects in the @clips or @vclips array.
1784 * @increment: Integer with which to increment the clip counter when looping.
1785 * Used to skip a predetermined number of clip rects.
1786 * @dirty: Closure structure. See the description of struct vmw_kms_dirty.
1787 */
1788int vmw_kms_helper_dirty(struct vmw_private *dev_priv,
1789 struct vmw_framebuffer *framebuffer,
1790 const struct drm_clip_rect *clips,
1791 const struct drm_vmw_rect *vclips,
1792 s32 dest_x, s32 dest_y,
1793 int num_clips,
1794 int increment,
1795 struct vmw_kms_dirty *dirty)
1796{
1797 struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
1798 struct drm_crtc *crtc;
1799 u32 num_units = 0;
1800 u32 i, k;
1a4b172a
TH
1801
1802 dirty->dev_priv = dev_priv;
1803
1804 list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list, head) {
1805 if (crtc->primary->fb != &framebuffer->base)
1806 continue;
1807 units[num_units++] = vmw_crtc_to_du(crtc);
1808 }
1809
1810 for (k = 0; k < num_units; k++) {
1811 struct vmw_display_unit *unit = units[k];
1812 s32 crtc_x = unit->crtc.x;
1813 s32 crtc_y = unit->crtc.y;
1814 s32 crtc_width = unit->crtc.mode.hdisplay;
1815 s32 crtc_height = unit->crtc.mode.vdisplay;
1816 const struct drm_clip_rect *clips_ptr = clips;
1817 const struct drm_vmw_rect *vclips_ptr = vclips;
1818
1819 dirty->unit = unit;
1820 if (dirty->fifo_reserve_size > 0) {
1821 dirty->cmd = vmw_fifo_reserve(dev_priv,
1822 dirty->fifo_reserve_size);
1823 if (!dirty->cmd) {
1824 DRM_ERROR("Couldn't reserve fifo space "
1825 "for dirty blits.\n");
f3b8c0ca 1826 return -ENOMEM;
1a4b172a
TH
1827 }
1828 memset(dirty->cmd, 0, dirty->fifo_reserve_size);
1829 }
1830 dirty->num_hits = 0;
1831 for (i = 0; i < num_clips; i++, clips_ptr += increment,
1832 vclips_ptr += increment) {
1833 s32 clip_left;
1834 s32 clip_top;
1835
1836 /*
1837 * Select clip array type. Note that integer type
1838 * in @clips is unsigned short, whereas in @vclips
1839 * it's 32-bit.
1840 */
1841 if (clips) {
1842 dirty->fb_x = (s32) clips_ptr->x1;
1843 dirty->fb_y = (s32) clips_ptr->y1;
1844 dirty->unit_x2 = (s32) clips_ptr->x2 + dest_x -
1845 crtc_x;
1846 dirty->unit_y2 = (s32) clips_ptr->y2 + dest_y -
1847 crtc_y;
1848 } else {
1849 dirty->fb_x = vclips_ptr->x;
1850 dirty->fb_y = vclips_ptr->y;
1851 dirty->unit_x2 = dirty->fb_x + vclips_ptr->w +
1852 dest_x - crtc_x;
1853 dirty->unit_y2 = dirty->fb_y + vclips_ptr->h +
1854 dest_y - crtc_y;
1855 }
1856
1857 dirty->unit_x1 = dirty->fb_x + dest_x - crtc_x;
1858 dirty->unit_y1 = dirty->fb_y + dest_y - crtc_y;
1859
1860 /* Skip this clip if it's outside the crtc region */
1861 if (dirty->unit_x1 >= crtc_width ||
1862 dirty->unit_y1 >= crtc_height ||
1863 dirty->unit_x2 <= 0 || dirty->unit_y2 <= 0)
1864 continue;
1865
1866 /* Clip right and bottom to crtc limits */
1867 dirty->unit_x2 = min_t(s32, dirty->unit_x2,
1868 crtc_width);
1869 dirty->unit_y2 = min_t(s32, dirty->unit_y2,
1870 crtc_height);
1871
1872 /* Clip left and top to crtc limits */
1873 clip_left = min_t(s32, dirty->unit_x1, 0);
1874 clip_top = min_t(s32, dirty->unit_y1, 0);
1875 dirty->unit_x1 -= clip_left;
1876 dirty->unit_y1 -= clip_top;
1877 dirty->fb_x -= clip_left;
1878 dirty->fb_y -= clip_top;
1879
1880 dirty->clip(dirty);
1881 }
1882
1883 dirty->fifo_commit(dirty);
1884 }
1885
1886 return 0;
1887}
1888
1889/**
1890 * vmw_kms_helper_buffer_prepare - Reserve and validate a buffer object before
1891 * command submission.
1892 *
1893 * @dev_priv. Pointer to a device private structure.
1894 * @buf: The buffer object
1895 * @interruptible: Whether to perform waits as interruptible.
1896 * @validate_as_mob: Whether the buffer should be validated as a MOB. If false,
1897 * The buffer will be validated as a GMR. Already pinned buffers will not be
1898 * validated.
1899 *
1900 * Returns 0 on success, negative error code on failure, -ERESTARTSYS if
1901 * interrupted by a signal.
1902 */
1903int vmw_kms_helper_buffer_prepare(struct vmw_private *dev_priv,
1904 struct vmw_dma_buffer *buf,
1905 bool interruptible,
1906 bool validate_as_mob)
1907{
1908 struct ttm_buffer_object *bo = &buf->base;
1909 int ret;
1910
dfd5e50e 1911 ttm_bo_reserve(bo, false, false, NULL);
1a4b172a
TH
1912 ret = vmw_validate_single_buffer(dev_priv, bo, interruptible,
1913 validate_as_mob);
1914 if (ret)
1915 ttm_bo_unreserve(bo);
1916
1917 return ret;
1918}
1919
1920/**
1921 * vmw_kms_helper_buffer_revert - Undo the actions of
1922 * vmw_kms_helper_buffer_prepare.
1923 *
1924 * @res: Pointer to the buffer object.
1925 *
1926 * Helper to be used if an error forces the caller to undo the actions of
1927 * vmw_kms_helper_buffer_prepare.
1928 */
1929void vmw_kms_helper_buffer_revert(struct vmw_dma_buffer *buf)
1930{
1931 if (buf)
1932 ttm_bo_unreserve(&buf->base);
1933}
1934
1935/**
1936 * vmw_kms_helper_buffer_finish - Unreserve and fence a buffer object after
1937 * kms command submission.
1938 *
1939 * @dev_priv: Pointer to a device private structure.
1940 * @file_priv: Pointer to a struct drm_file representing the caller's
1941 * connection. Must be set to NULL if @user_fence_rep is NULL, and conversely
1942 * if non-NULL, @user_fence_rep must be non-NULL.
1943 * @buf: The buffer object.
1944 * @out_fence: Optional pointer to a fence pointer. If non-NULL, a
1945 * ref-counted fence pointer is returned here.
1946 * @user_fence_rep: Optional pointer to a user-space provided struct
1947 * drm_vmw_fence_rep. If provided, @file_priv must also be provided and the
1948 * function copies fence data to user-space in a fail-safe manner.
1949 */
1950void vmw_kms_helper_buffer_finish(struct vmw_private *dev_priv,
1951 struct drm_file *file_priv,
1952 struct vmw_dma_buffer *buf,
1953 struct vmw_fence_obj **out_fence,
1954 struct drm_vmw_fence_rep __user *
1955 user_fence_rep)
1956{
1957 struct vmw_fence_obj *fence;
1958 uint32_t handle;
1959 int ret;
1960
1961 ret = vmw_execbuf_fence_commands(file_priv, dev_priv, &fence,
1962 file_priv ? &handle : NULL);
1963 if (buf)
1964 vmw_fence_single_bo(&buf->base, fence);
1965 if (file_priv)
1966 vmw_execbuf_copy_fence_user(dev_priv, vmw_fpriv(file_priv),
1967 ret, user_fence_rep, fence,
1968 handle);
1969 if (out_fence)
1970 *out_fence = fence;
1971 else
1972 vmw_fence_obj_unreference(&fence);
1973
1974 vmw_kms_helper_buffer_revert(buf);
1975}
1976
1977
1978/**
1979 * vmw_kms_helper_resource_revert - Undo the actions of
1980 * vmw_kms_helper_resource_prepare.
1981 *
1982 * @res: Pointer to the resource. Typically a surface.
1983 *
1984 * Helper to be used if an error forces the caller to undo the actions of
1985 * vmw_kms_helper_resource_prepare.
1986 */
1987void vmw_kms_helper_resource_revert(struct vmw_resource *res)
1988{
1989 vmw_kms_helper_buffer_revert(res->backup);
d80efd5c 1990 vmw_resource_unreserve(res, false, NULL, 0);
1a4b172a
TH
1991 mutex_unlock(&res->dev_priv->cmdbuf_mutex);
1992}
1993
1994/**
1995 * vmw_kms_helper_resource_prepare - Reserve and validate a resource before
1996 * command submission.
1997 *
1998 * @res: Pointer to the resource. Typically a surface.
1999 * @interruptible: Whether to perform waits as interruptible.
2000 *
2001 * Reserves and validates also the backup buffer if a guest-backed resource.
2002 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
2003 * interrupted by a signal.
2004 */
2005int vmw_kms_helper_resource_prepare(struct vmw_resource *res,
2006 bool interruptible)
2007{
2008 int ret = 0;
2009
2010 if (interruptible)
2011 ret = mutex_lock_interruptible(&res->dev_priv->cmdbuf_mutex);
2012 else
2013 mutex_lock(&res->dev_priv->cmdbuf_mutex);
2014
2015 if (unlikely(ret != 0))
2016 return -ERESTARTSYS;
2017
2018 ret = vmw_resource_reserve(res, interruptible, false);
2019 if (ret)
2020 goto out_unlock;
2021
2022 if (res->backup) {
2023 ret = vmw_kms_helper_buffer_prepare(res->dev_priv, res->backup,
2024 interruptible,
2025 res->dev_priv->has_mob);
2026 if (ret)
2027 goto out_unreserve;
2028 }
2029 ret = vmw_resource_validate(res);
2030 if (ret)
2031 goto out_revert;
2032 return 0;
2033
2034out_revert:
2035 vmw_kms_helper_buffer_revert(res->backup);
2036out_unreserve:
d80efd5c 2037 vmw_resource_unreserve(res, false, NULL, 0);
1a4b172a
TH
2038out_unlock:
2039 mutex_unlock(&res->dev_priv->cmdbuf_mutex);
2040 return ret;
2041}
2042
2043/**
2044 * vmw_kms_helper_resource_finish - Unreserve and fence a resource after
2045 * kms command submission.
2046 *
2047 * @res: Pointer to the resource. Typically a surface.
2048 * @out_fence: Optional pointer to a fence pointer. If non-NULL, a
2049 * ref-counted fence pointer is returned here.
2050 */
2051void vmw_kms_helper_resource_finish(struct vmw_resource *res,
2052 struct vmw_fence_obj **out_fence)
2053{
2054 if (res->backup || out_fence)
2055 vmw_kms_helper_buffer_finish(res->dev_priv, NULL, res->backup,
2056 out_fence, NULL);
2057
d80efd5c 2058 vmw_resource_unreserve(res, false, NULL, 0);
1a4b172a
TH
2059 mutex_unlock(&res->dev_priv->cmdbuf_mutex);
2060}
6bf6bf03
TH
2061
2062/**
2063 * vmw_kms_update_proxy - Helper function to update a proxy surface from
2064 * its backing MOB.
2065 *
2066 * @res: Pointer to the surface resource
2067 * @clips: Clip rects in framebuffer (surface) space.
2068 * @num_clips: Number of clips in @clips.
2069 * @increment: Integer with which to increment the clip counter when looping.
2070 * Used to skip a predetermined number of clip rects.
2071 *
2072 * This function makes sure the proxy surface is updated from its backing MOB
2073 * using the region given by @clips. The surface resource @res and its backing
2074 * MOB needs to be reserved and validated on call.
2075 */
2076int vmw_kms_update_proxy(struct vmw_resource *res,
2077 const struct drm_clip_rect *clips,
2078 unsigned num_clips,
2079 int increment)
2080{
2081 struct vmw_private *dev_priv = res->dev_priv;
2082 struct drm_vmw_size *size = &vmw_res_to_srf(res)->base_size;
2083 struct {
2084 SVGA3dCmdHeader header;
2085 SVGA3dCmdUpdateGBImage body;
2086 } *cmd;
2087 SVGA3dBox *box;
2088 size_t copy_size = 0;
2089 int i;
2090
2091 if (!clips)
2092 return 0;
2093
2094 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd) * num_clips);
2095 if (!cmd) {
2096 DRM_ERROR("Couldn't reserve fifo space for proxy surface "
2097 "update.\n");
2098 return -ENOMEM;
2099 }
2100
2101 for (i = 0; i < num_clips; ++i, clips += increment, ++cmd) {
2102 box = &cmd->body.box;
2103
2104 cmd->header.id = SVGA_3D_CMD_UPDATE_GB_IMAGE;
2105 cmd->header.size = sizeof(cmd->body);
2106 cmd->body.image.sid = res->id;
2107 cmd->body.image.face = 0;
2108 cmd->body.image.mipmap = 0;
2109
2110 if (clips->x1 > size->width || clips->x2 > size->width ||
2111 clips->y1 > size->height || clips->y2 > size->height) {
2112 DRM_ERROR("Invalid clips outsize of framebuffer.\n");
2113 return -EINVAL;
2114 }
2115
2116 box->x = clips->x1;
2117 box->y = clips->y1;
2118 box->z = 0;
2119 box->w = clips->x2 - clips->x1;
2120 box->h = clips->y2 - clips->y1;
2121 box->d = 1;
2122
2123 copy_size += sizeof(*cmd);
2124 }
2125
2126 vmw_fifo_commit(dev_priv, copy_size);
2127
2128 return 0;
2129}
a278724a
TH
2130
2131int vmw_kms_fbdev_init_data(struct vmw_private *dev_priv,
2132 unsigned unit,
2133 u32 max_width,
2134 u32 max_height,
2135 struct drm_connector **p_con,
2136 struct drm_crtc **p_crtc,
2137 struct drm_display_mode **p_mode)
2138{
2139 struct drm_connector *con;
2140 struct vmw_display_unit *du;
2141 struct drm_display_mode *mode;
2142 int i = 0;
2143
2144 list_for_each_entry(con, &dev_priv->dev->mode_config.connector_list,
2145 head) {
2146 if (i == unit)
2147 break;
2148
2149 ++i;
2150 }
2151
2152 if (i != unit) {
2153 DRM_ERROR("Could not find initial display unit.\n");
2154 return -EINVAL;
2155 }
2156
2157 if (list_empty(&con->modes))
2158 (void) vmw_du_connector_fill_modes(con, max_width, max_height);
2159
2160 if (list_empty(&con->modes)) {
2161 DRM_ERROR("Could not find initial display mode.\n");
2162 return -EINVAL;
2163 }
2164
2165 du = vmw_connector_to_du(con);
2166 *p_con = con;
2167 *p_crtc = &du->crtc;
2168
2169 list_for_each_entry(mode, &con->modes, head) {
2170 if (mode->type & DRM_MODE_TYPE_PREFERRED)
2171 break;
2172 }
2173
2174 if (mode->type & DRM_MODE_TYPE_PREFERRED)
2175 *p_mode = mode;
2176 else {
2177 WARN_ONCE(true, "Could not find initial preferred mode.\n");
2178 *p_mode = list_first_entry(&con->modes,
2179 struct drm_display_mode,
2180 head);
2181 }
2182
2183 return 0;
2184}
75c06855
TH
2185
2186/**
2187 * vmw_kms_del_active - unregister a crtc binding to the implicit framebuffer
2188 *
2189 * @dev_priv: Pointer to a device private struct.
2190 * @du: The display unit of the crtc.
2191 */
2192void vmw_kms_del_active(struct vmw_private *dev_priv,
2193 struct vmw_display_unit *du)
2194{
93cd1681 2195 mutex_lock(&dev_priv->global_kms_state_mutex);
75c06855
TH
2196 if (du->active_implicit) {
2197 if (--(dev_priv->num_implicit) == 0)
2198 dev_priv->implicit_fb = NULL;
2199 du->active_implicit = false;
2200 }
93cd1681 2201 mutex_unlock(&dev_priv->global_kms_state_mutex);
75c06855
TH
2202}
2203
2204/**
2205 * vmw_kms_add_active - register a crtc binding to an implicit framebuffer
2206 *
2207 * @vmw_priv: Pointer to a device private struct.
2208 * @du: The display unit of the crtc.
2209 * @vfb: The implicit framebuffer
2210 *
2211 * Registers a binding to an implicit framebuffer.
2212 */
2213void vmw_kms_add_active(struct vmw_private *dev_priv,
2214 struct vmw_display_unit *du,
2215 struct vmw_framebuffer *vfb)
2216{
93cd1681 2217 mutex_lock(&dev_priv->global_kms_state_mutex);
75c06855
TH
2218 WARN_ON_ONCE(!dev_priv->num_implicit && dev_priv->implicit_fb);
2219
2220 if (!du->active_implicit && du->is_implicit) {
2221 dev_priv->implicit_fb = vfb;
2222 du->active_implicit = true;
2223 dev_priv->num_implicit++;
2224 }
93cd1681 2225 mutex_unlock(&dev_priv->global_kms_state_mutex);
75c06855
TH
2226}
2227
2228/**
2229 * vmw_kms_screen_object_flippable - Check whether we can page-flip a crtc.
2230 *
2231 * @dev_priv: Pointer to device-private struct.
2232 * @crtc: The crtc we want to flip.
2233 *
2234 * Returns true or false depending whether it's OK to flip this crtc
2235 * based on the criterion that we must not have more than one implicit
2236 * frame-buffer at any one time.
2237 */
2238bool vmw_kms_crtc_flippable(struct vmw_private *dev_priv,
2239 struct drm_crtc *crtc)
2240{
2241 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
93cd1681 2242 bool ret;
75c06855 2243
93cd1681
TH
2244 mutex_lock(&dev_priv->global_kms_state_mutex);
2245 ret = !du->is_implicit || dev_priv->num_implicit == 1;
2246 mutex_unlock(&dev_priv->global_kms_state_mutex);
75c06855 2247
93cd1681 2248 return ret;
75c06855
TH
2249}
2250
2251/**
2252 * vmw_kms_update_implicit_fb - Update the implicit fb.
2253 *
2254 * @dev_priv: Pointer to device-private struct.
2255 * @crtc: The crtc the new implicit frame-buffer is bound to.
2256 */
2257void vmw_kms_update_implicit_fb(struct vmw_private *dev_priv,
2258 struct drm_crtc *crtc)
2259{
2260 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
2261 struct vmw_framebuffer *vfb;
2262
93cd1681 2263 mutex_lock(&dev_priv->global_kms_state_mutex);
75c06855
TH
2264
2265 if (!du->is_implicit)
93cd1681 2266 goto out_unlock;
75c06855
TH
2267
2268 vfb = vmw_framebuffer_to_vfb(crtc->primary->fb);
2269 WARN_ON_ONCE(dev_priv->num_implicit != 1 &&
2270 dev_priv->implicit_fb != vfb);
2271
2272 dev_priv->implicit_fb = vfb;
93cd1681
TH
2273out_unlock:
2274 mutex_unlock(&dev_priv->global_kms_state_mutex);
75c06855 2275}
76404ac0
TH
2276
2277/**
2278 * vmw_kms_create_implicit_placement_proparty - Set up the implicit placement
2279 * property.
2280 *
2281 * @dev_priv: Pointer to a device private struct.
2282 * @immutable: Whether the property is immutable.
2283 *
2284 * Sets up the implicit placement property unless it's already set up.
2285 */
2286void
2287vmw_kms_create_implicit_placement_property(struct vmw_private *dev_priv,
2288 bool immutable)
2289{
2290 if (dev_priv->implicit_placement_property)
2291 return;
2292
2293 dev_priv->implicit_placement_property =
2294 drm_property_create_range(dev_priv->dev,
2295 immutable ?
2296 DRM_MODE_PROP_IMMUTABLE : 0,
2297 "implicit_placement", 0, 1);
2298
2299}