1 /**************************************************************************
3 * Copyright © 2009-2014 VMware, Inc., Palo Alto, CA., USA
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
28 #include "vmwgfx_kms.h"
31 /* Might need a hrtimer here? */
32 #define VMWGFX_PRESENT_RATE ((HZ / 60 > 0) ? HZ / 60 : 1)
34 void vmw_du_cleanup(struct vmw_display_unit
*du
)
36 if (du
->cursor_surface
)
37 vmw_surface_unreference(&du
->cursor_surface
);
38 if (du
->cursor_dmabuf
)
39 vmw_dmabuf_unreference(&du
->cursor_dmabuf
);
40 drm_connector_unregister(&du
->connector
);
41 drm_crtc_cleanup(&du
->crtc
);
42 drm_encoder_cleanup(&du
->encoder
);
43 drm_connector_cleanup(&du
->connector
);
47 * Display Unit Cursor functions
50 int vmw_cursor_update_image(struct vmw_private
*dev_priv
,
51 u32
*image
, u32 width
, u32 height
,
52 u32 hotspotX
, u32 hotspotY
)
56 SVGAFifoCmdDefineAlphaCursor cursor
;
58 u32 image_size
= width
* height
* 4;
59 u32 cmd_size
= sizeof(*cmd
) + image_size
;
64 cmd
= vmw_fifo_reserve(dev_priv
, cmd_size
);
65 if (unlikely(cmd
== NULL
)) {
66 DRM_ERROR("Fifo reserve failed.\n");
70 memset(cmd
, 0, sizeof(*cmd
));
72 memcpy(&cmd
[1], image
, image_size
);
74 cmd
->cmd
= cpu_to_le32(SVGA_CMD_DEFINE_ALPHA_CURSOR
);
75 cmd
->cursor
.id
= cpu_to_le32(0);
76 cmd
->cursor
.width
= cpu_to_le32(width
);
77 cmd
->cursor
.height
= cpu_to_le32(height
);
78 cmd
->cursor
.hotspotX
= cpu_to_le32(hotspotX
);
79 cmd
->cursor
.hotspotY
= cpu_to_le32(hotspotY
);
81 vmw_fifo_commit(dev_priv
, cmd_size
);
86 int vmw_cursor_update_dmabuf(struct vmw_private
*dev_priv
,
87 struct vmw_dma_buffer
*dmabuf
,
88 u32 width
, u32 height
,
89 u32 hotspotX
, u32 hotspotY
)
91 struct ttm_bo_kmap_obj map
;
92 unsigned long kmap_offset
;
93 unsigned long kmap_num
;
99 kmap_num
= (width
*height
*4 + PAGE_SIZE
- 1) >> PAGE_SHIFT
;
101 ret
= ttm_bo_reserve(&dmabuf
->base
, true, false, false, NULL
);
102 if (unlikely(ret
!= 0)) {
103 DRM_ERROR("reserve failed\n");
107 ret
= ttm_bo_kmap(&dmabuf
->base
, kmap_offset
, kmap_num
, &map
);
108 if (unlikely(ret
!= 0))
111 virtual = ttm_kmap_obj_virtual(&map
, &dummy
);
112 ret
= vmw_cursor_update_image(dev_priv
, virtual, width
, height
,
117 ttm_bo_unreserve(&dmabuf
->base
);
123 void vmw_cursor_update_position(struct vmw_private
*dev_priv
,
124 bool show
, int x
, int y
)
126 __le32 __iomem
*fifo_mem
= dev_priv
->mmio_virt
;
129 iowrite32(show
? 1 : 0, fifo_mem
+ SVGA_FIFO_CURSOR_ON
);
130 iowrite32(x
, fifo_mem
+ SVGA_FIFO_CURSOR_X
);
131 iowrite32(y
, fifo_mem
+ SVGA_FIFO_CURSOR_Y
);
132 count
= ioread32(fifo_mem
+ SVGA_FIFO_CURSOR_COUNT
);
133 iowrite32(++count
, fifo_mem
+ SVGA_FIFO_CURSOR_COUNT
);
136 int vmw_du_crtc_cursor_set(struct drm_crtc
*crtc
, struct drm_file
*file_priv
,
137 uint32_t handle
, uint32_t width
, uint32_t height
)
139 struct vmw_private
*dev_priv
= vmw_priv(crtc
->dev
);
140 struct vmw_display_unit
*du
= vmw_crtc_to_du(crtc
);
141 struct vmw_surface
*surface
= NULL
;
142 struct vmw_dma_buffer
*dmabuf
= NULL
;
146 * FIXME: Unclear whether there's any global state touched by the
147 * cursor_set function, especially vmw_cursor_update_position looks
148 * suspicious. For now take the easy route and reacquire all locks. We
149 * can do this since the caller in the drm core doesn't check anything
150 * which is protected by any looks.
152 drm_modeset_unlock_crtc(crtc
);
153 drm_modeset_lock_all(dev_priv
->dev
);
155 /* A lot of the code assumes this */
156 if (handle
&& (width
!= 64 || height
!= 64)) {
162 struct ttm_object_file
*tfile
= vmw_fpriv(file_priv
)->tfile
;
164 ret
= vmw_user_lookup_handle(dev_priv
, tfile
,
165 handle
, &surface
, &dmabuf
);
167 DRM_ERROR("failed to find surface or dmabuf: %i\n", ret
);
173 /* need to do this before taking down old image */
174 if (surface
&& !surface
->snooper
.image
) {
175 DRM_ERROR("surface not suitable for cursor\n");
176 vmw_surface_unreference(&surface
);
181 /* takedown old cursor */
182 if (du
->cursor_surface
) {
183 du
->cursor_surface
->snooper
.crtc
= NULL
;
184 vmw_surface_unreference(&du
->cursor_surface
);
186 if (du
->cursor_dmabuf
)
187 vmw_dmabuf_unreference(&du
->cursor_dmabuf
);
189 /* setup new image */
191 /* vmw_user_surface_lookup takes one reference */
192 du
->cursor_surface
= surface
;
194 du
->cursor_surface
->snooper
.crtc
= crtc
;
195 du
->cursor_age
= du
->cursor_surface
->snooper
.age
;
196 vmw_cursor_update_image(dev_priv
, surface
->snooper
.image
,
197 64, 64, du
->hotspot_x
, du
->hotspot_y
);
199 /* vmw_user_surface_lookup takes one reference */
200 du
->cursor_dmabuf
= dmabuf
;
202 ret
= vmw_cursor_update_dmabuf(dev_priv
, dmabuf
, width
, height
,
203 du
->hotspot_x
, du
->hotspot_y
);
205 vmw_cursor_update_position(dev_priv
, false, 0, 0);
210 vmw_cursor_update_position(dev_priv
, true,
211 du
->cursor_x
+ du
->hotspot_x
,
212 du
->cursor_y
+ du
->hotspot_y
);
216 drm_modeset_unlock_all(dev_priv
->dev
);
217 drm_modeset_lock_crtc(crtc
, crtc
->cursor
);
222 int vmw_du_crtc_cursor_move(struct drm_crtc
*crtc
, int x
, int y
)
224 struct vmw_private
*dev_priv
= vmw_priv(crtc
->dev
);
225 struct vmw_display_unit
*du
= vmw_crtc_to_du(crtc
);
226 bool shown
= du
->cursor_surface
|| du
->cursor_dmabuf
? true : false;
228 du
->cursor_x
= x
+ crtc
->x
;
229 du
->cursor_y
= y
+ crtc
->y
;
232 * FIXME: Unclear whether there's any global state touched by the
233 * cursor_set function, especially vmw_cursor_update_position looks
234 * suspicious. For now take the easy route and reacquire all locks. We
235 * can do this since the caller in the drm core doesn't check anything
236 * which is protected by any looks.
238 drm_modeset_unlock_crtc(crtc
);
239 drm_modeset_lock_all(dev_priv
->dev
);
241 vmw_cursor_update_position(dev_priv
, shown
,
242 du
->cursor_x
+ du
->hotspot_x
,
243 du
->cursor_y
+ du
->hotspot_y
);
245 drm_modeset_unlock_all(dev_priv
->dev
);
246 drm_modeset_lock_crtc(crtc
, crtc
->cursor
);
251 void vmw_kms_cursor_snoop(struct vmw_surface
*srf
,
252 struct ttm_object_file
*tfile
,
253 struct ttm_buffer_object
*bo
,
254 SVGA3dCmdHeader
*header
)
256 struct ttm_bo_kmap_obj map
;
257 unsigned long kmap_offset
;
258 unsigned long kmap_num
;
264 SVGA3dCmdHeader header
;
265 SVGA3dCmdSurfaceDMA dma
;
269 cmd
= container_of(header
, struct vmw_dma_cmd
, header
);
271 /* No snooper installed */
272 if (!srf
->snooper
.image
)
275 if (cmd
->dma
.host
.face
!= 0 || cmd
->dma
.host
.mipmap
!= 0) {
276 DRM_ERROR("face and mipmap for cursors should never != 0\n");
280 if (cmd
->header
.size
< 64) {
281 DRM_ERROR("at least one full copy box must be given\n");
285 box
= (SVGA3dCopyBox
*)&cmd
[1];
286 box_count
= (cmd
->header
.size
- sizeof(SVGA3dCmdSurfaceDMA
)) /
287 sizeof(SVGA3dCopyBox
);
289 if (cmd
->dma
.guest
.ptr
.offset
% PAGE_SIZE
||
290 box
->x
!= 0 || box
->y
!= 0 || box
->z
!= 0 ||
291 box
->srcx
!= 0 || box
->srcy
!= 0 || box
->srcz
!= 0 ||
292 box
->d
!= 1 || box_count
!= 1) {
293 /* TODO handle none page aligned offsets */
294 /* TODO handle more dst & src != 0 */
295 /* TODO handle more then one copy */
296 DRM_ERROR("Cant snoop dma request for cursor!\n");
297 DRM_ERROR("(%u, %u, %u) (%u, %u, %u) (%ux%ux%u) %u %u\n",
298 box
->srcx
, box
->srcy
, box
->srcz
,
299 box
->x
, box
->y
, box
->z
,
300 box
->w
, box
->h
, box
->d
, box_count
,
301 cmd
->dma
.guest
.ptr
.offset
);
305 kmap_offset
= cmd
->dma
.guest
.ptr
.offset
>> PAGE_SHIFT
;
306 kmap_num
= (64*64*4) >> PAGE_SHIFT
;
308 ret
= ttm_bo_reserve(bo
, true, false, false, NULL
);
309 if (unlikely(ret
!= 0)) {
310 DRM_ERROR("reserve failed\n");
314 ret
= ttm_bo_kmap(bo
, kmap_offset
, kmap_num
, &map
);
315 if (unlikely(ret
!= 0))
318 virtual = ttm_kmap_obj_virtual(&map
, &dummy
);
320 if (box
->w
== 64 && cmd
->dma
.guest
.pitch
== 64*4) {
321 memcpy(srf
->snooper
.image
, virtual, 64*64*4);
323 /* Image is unsigned pointer. */
324 for (i
= 0; i
< box
->h
; i
++)
325 memcpy(srf
->snooper
.image
+ i
* 64,
326 virtual + i
* cmd
->dma
.guest
.pitch
,
334 ttm_bo_unreserve(bo
);
337 void vmw_kms_cursor_post_execbuf(struct vmw_private
*dev_priv
)
339 struct drm_device
*dev
= dev_priv
->dev
;
340 struct vmw_display_unit
*du
;
341 struct drm_crtc
*crtc
;
343 mutex_lock(&dev
->mode_config
.mutex
);
345 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
) {
346 du
= vmw_crtc_to_du(crtc
);
347 if (!du
->cursor_surface
||
348 du
->cursor_age
== du
->cursor_surface
->snooper
.age
)
351 du
->cursor_age
= du
->cursor_surface
->snooper
.age
;
352 vmw_cursor_update_image(dev_priv
,
353 du
->cursor_surface
->snooper
.image
,
354 64, 64, du
->hotspot_x
, du
->hotspot_y
);
357 mutex_unlock(&dev
->mode_config
.mutex
);
361 * Generic framebuffer code
365 * Surface framebuffer code
368 static void vmw_framebuffer_surface_destroy(struct drm_framebuffer
*framebuffer
)
370 struct vmw_framebuffer_surface
*vfbs
=
371 vmw_framebuffer_to_vfbs(framebuffer
);
373 drm_framebuffer_cleanup(framebuffer
);
374 vmw_surface_unreference(&vfbs
->surface
);
375 if (vfbs
->base
.user_obj
)
376 ttm_base_object_unref(&vfbs
->base
.user_obj
);
381 static int vmw_framebuffer_surface_dirty(struct drm_framebuffer
*framebuffer
,
382 struct drm_file
*file_priv
,
383 unsigned flags
, unsigned color
,
384 struct drm_clip_rect
*clips
,
387 struct vmw_private
*dev_priv
= vmw_priv(framebuffer
->dev
);
388 struct vmw_framebuffer_surface
*vfbs
=
389 vmw_framebuffer_to_vfbs(framebuffer
);
390 struct drm_clip_rect norect
;
393 /* Legacy Display Unit does not support 3D */
394 if (dev_priv
->active_display_unit
== vmw_du_legacy
)
397 drm_modeset_lock_all(dev_priv
->dev
);
399 ret
= ttm_read_lock(&dev_priv
->reservation_sem
, true);
400 if (unlikely(ret
!= 0)) {
401 drm_modeset_unlock_all(dev_priv
->dev
);
408 norect
.x1
= norect
.y1
= 0;
409 norect
.x2
= framebuffer
->width
;
410 norect
.y2
= framebuffer
->height
;
411 } else if (flags
& DRM_MODE_FB_DIRTY_ANNOTATE_COPY
) {
413 inc
= 2; /* skip source rects */
416 if (dev_priv
->active_display_unit
== vmw_du_screen_object
)
417 ret
= vmw_kms_sou_do_surface_dirty(dev_priv
, &vfbs
->base
,
418 clips
, NULL
, NULL
, 0, 0,
419 num_clips
, inc
, NULL
);
421 ret
= vmw_kms_stdu_surface_dirty(dev_priv
, &vfbs
->base
,
422 clips
, NULL
, NULL
, 0, 0,
423 num_clips
, inc
, NULL
);
425 vmw_fifo_flush(dev_priv
, false);
426 ttm_read_unlock(&dev_priv
->reservation_sem
);
428 drm_modeset_unlock_all(dev_priv
->dev
);
434 * vmw_kms_readback - Perform a readback from the screen system to
435 * a dma-buffer backed framebuffer.
437 * @dev_priv: Pointer to the device private structure.
438 * @file_priv: Pointer to a struct drm_file identifying the caller.
439 * Must be set to NULL if @user_fence_rep is NULL.
440 * @vfb: Pointer to the dma-buffer backed framebuffer.
441 * @user_fence_rep: User-space provided structure for fence information.
442 * Must be set to non-NULL if @file_priv is non-NULL.
443 * @vclips: Array of clip rects.
444 * @num_clips: Number of clip rects in @vclips.
446 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
449 int vmw_kms_readback(struct vmw_private
*dev_priv
,
450 struct drm_file
*file_priv
,
451 struct vmw_framebuffer
*vfb
,
452 struct drm_vmw_fence_rep __user
*user_fence_rep
,
453 struct drm_vmw_rect
*vclips
,
456 switch (dev_priv
->active_display_unit
) {
457 case vmw_du_screen_object
:
458 return vmw_kms_sou_readback(dev_priv
, file_priv
, vfb
,
459 user_fence_rep
, vclips
, num_clips
);
460 case vmw_du_screen_target
:
461 return vmw_kms_stdu_dma(dev_priv
, file_priv
, vfb
,
462 user_fence_rep
, NULL
, vclips
, num_clips
,
466 "Readback called with invalid display system.\n");
473 static struct drm_framebuffer_funcs vmw_framebuffer_surface_funcs
= {
474 .destroy
= vmw_framebuffer_surface_destroy
,
475 .dirty
= vmw_framebuffer_surface_dirty
,
478 static int vmw_kms_new_framebuffer_surface(struct vmw_private
*dev_priv
,
479 struct vmw_surface
*surface
,
480 struct vmw_framebuffer
**out
,
481 const struct drm_mode_fb_cmd
483 bool is_dmabuf_proxy
)
486 struct drm_device
*dev
= dev_priv
->dev
;
487 struct vmw_framebuffer_surface
*vfbs
;
488 enum SVGA3dSurfaceFormat format
;
491 /* 3D is only supported on HWv8 and newer hosts */
492 if (dev_priv
->active_display_unit
== vmw_du_legacy
)
499 /* Surface must be marked as a scanout. */
500 if (unlikely(!surface
->scanout
))
503 if (unlikely(surface
->mip_levels
[0] != 1 ||
504 surface
->num_sizes
!= 1 ||
505 surface
->base_size
.width
< mode_cmd
->width
||
506 surface
->base_size
.height
< mode_cmd
->height
||
507 surface
->base_size
.depth
!= 1)) {
508 DRM_ERROR("Incompatible surface dimensions "
509 "for requested mode.\n");
513 switch (mode_cmd
->depth
) {
515 format
= SVGA3D_A8R8G8B8
;
518 format
= SVGA3D_X8R8G8B8
;
521 format
= SVGA3D_R5G6B5
;
524 format
= SVGA3D_A1R5G5B5
;
527 DRM_ERROR("Invalid color depth: %d\n", mode_cmd
->depth
);
531 if (unlikely(format
!= surface
->format
)) {
532 DRM_ERROR("Invalid surface format for requested mode.\n");
536 vfbs
= kzalloc(sizeof(*vfbs
), GFP_KERNEL
);
542 if (!vmw_surface_reference(surface
)) {
543 DRM_ERROR("failed to reference surface %p\n", surface
);
548 /* XXX get the first 3 from the surface info */
549 vfbs
->base
.base
.bits_per_pixel
= mode_cmd
->bpp
;
550 vfbs
->base
.base
.pitches
[0] = mode_cmd
->pitch
;
551 vfbs
->base
.base
.depth
= mode_cmd
->depth
;
552 vfbs
->base
.base
.width
= mode_cmd
->width
;
553 vfbs
->base
.base
.height
= mode_cmd
->height
;
554 vfbs
->surface
= surface
;
555 vfbs
->base
.user_handle
= mode_cmd
->handle
;
556 vfbs
->is_dmabuf_proxy
= is_dmabuf_proxy
;
560 ret
= drm_framebuffer_init(dev
, &vfbs
->base
.base
,
561 &vmw_framebuffer_surface_funcs
);
568 vmw_surface_unreference(&surface
);
576 * Dmabuf framebuffer code
579 static void vmw_framebuffer_dmabuf_destroy(struct drm_framebuffer
*framebuffer
)
581 struct vmw_framebuffer_dmabuf
*vfbd
=
582 vmw_framebuffer_to_vfbd(framebuffer
);
584 drm_framebuffer_cleanup(framebuffer
);
585 vmw_dmabuf_unreference(&vfbd
->buffer
);
586 if (vfbd
->base
.user_obj
)
587 ttm_base_object_unref(&vfbd
->base
.user_obj
);
592 static int vmw_framebuffer_dmabuf_dirty(struct drm_framebuffer
*framebuffer
,
593 struct drm_file
*file_priv
,
594 unsigned flags
, unsigned color
,
595 struct drm_clip_rect
*clips
,
598 struct vmw_private
*dev_priv
= vmw_priv(framebuffer
->dev
);
599 struct vmw_framebuffer_dmabuf
*vfbd
=
600 vmw_framebuffer_to_vfbd(framebuffer
);
601 struct drm_clip_rect norect
;
602 int ret
, increment
= 1;
604 drm_modeset_lock_all(dev_priv
->dev
);
606 ret
= ttm_read_lock(&dev_priv
->reservation_sem
, true);
607 if (unlikely(ret
!= 0)) {
608 drm_modeset_unlock_all(dev_priv
->dev
);
615 norect
.x1
= norect
.y1
= 0;
616 norect
.x2
= framebuffer
->width
;
617 norect
.y2
= framebuffer
->height
;
618 } else if (flags
& DRM_MODE_FB_DIRTY_ANNOTATE_COPY
) {
623 switch (dev_priv
->active_display_unit
) {
624 case vmw_du_screen_target
:
625 ret
= vmw_kms_stdu_dma(dev_priv
, NULL
, &vfbd
->base
, NULL
,
626 clips
, NULL
, num_clips
, increment
,
629 case vmw_du_screen_object
:
630 ret
= vmw_kms_sou_do_dmabuf_dirty(dev_priv
, &vfbd
->base
,
631 clips
, num_clips
, increment
,
636 ret
= vmw_kms_ldu_do_dmabuf_dirty(dev_priv
, &vfbd
->base
, 0, 0,
637 clips
, num_clips
, increment
);
641 WARN_ONCE(true, "Dirty called with invalid display system.\n");
645 vmw_fifo_flush(dev_priv
, false);
646 ttm_read_unlock(&dev_priv
->reservation_sem
);
648 drm_modeset_unlock_all(dev_priv
->dev
);
653 static struct drm_framebuffer_funcs vmw_framebuffer_dmabuf_funcs
= {
654 .destroy
= vmw_framebuffer_dmabuf_destroy
,
655 .dirty
= vmw_framebuffer_dmabuf_dirty
,
659 * Pin the dmabuffer to the start of vram.
661 static int vmw_framebuffer_pin(struct vmw_framebuffer
*vfb
)
663 struct vmw_private
*dev_priv
= vmw_priv(vfb
->base
.dev
);
664 struct vmw_dma_buffer
*buf
;
667 buf
= vfb
->dmabuf
? vmw_framebuffer_to_vfbd(&vfb
->base
)->buffer
:
668 vmw_framebuffer_to_vfbs(&vfb
->base
)->surface
->res
.backup
;
673 switch (dev_priv
->active_display_unit
) {
675 vmw_overlay_pause_all(dev_priv
);
676 ret
= vmw_dmabuf_pin_in_start_of_vram(dev_priv
, buf
, false);
677 vmw_overlay_resume_all(dev_priv
);
679 case vmw_du_screen_object
:
680 case vmw_du_screen_target
:
682 return vmw_dmabuf_pin_in_vram_or_gmr(dev_priv
, buf
,
685 return vmw_dmabuf_pin_in_placement(dev_priv
, buf
,
686 &vmw_mob_placement
, false);
694 static int vmw_framebuffer_unpin(struct vmw_framebuffer
*vfb
)
696 struct vmw_private
*dev_priv
= vmw_priv(vfb
->base
.dev
);
697 struct vmw_dma_buffer
*buf
;
699 buf
= vfb
->dmabuf
? vmw_framebuffer_to_vfbd(&vfb
->base
)->buffer
:
700 vmw_framebuffer_to_vfbs(&vfb
->base
)->surface
->res
.backup
;
705 return vmw_dmabuf_unpin(dev_priv
, buf
, false);
709 * vmw_create_dmabuf_proxy - create a proxy surface for the DMA buf
712 * @mode_cmd: parameters for the new surface
713 * @dmabuf_mob: MOB backing the DMA buf
714 * @srf_out: newly created surface
716 * When the content FB is a DMA buf, we create a surface as a proxy to the
717 * same buffer. This way we can do a surface copy rather than a surface DMA.
718 * This is a more efficient approach
721 * 0 on success, error code otherwise
723 static int vmw_create_dmabuf_proxy(struct drm_device
*dev
,
724 const struct drm_mode_fb_cmd
*mode_cmd
,
725 struct vmw_dma_buffer
*dmabuf_mob
,
726 struct vmw_surface
**srf_out
)
729 struct drm_vmw_size content_base_size
;
730 struct vmw_resource
*res
;
733 switch (mode_cmd
->depth
) {
736 format
= SVGA3D_X8R8G8B8
;
741 format
= SVGA3D_R5G6B5
;
749 DRM_ERROR("Invalid framebuffer format %d\n", mode_cmd
->depth
);
753 content_base_size
.width
= mode_cmd
->width
;
754 content_base_size
.height
= mode_cmd
->height
;
755 content_base_size
.depth
= 1;
757 ret
= vmw_surface_gb_priv_define(dev
,
758 0, /* kernel visible only */
761 true, /* can be a scanout buffer */
762 1, /* num of mip levels */
767 DRM_ERROR("Failed to allocate proxy content buffer\n");
771 res
= &(*srf_out
)->res
;
773 /* Reserve and switch the backing mob. */
774 mutex_lock(&res
->dev_priv
->cmdbuf_mutex
);
775 (void) vmw_resource_reserve(res
, false, true);
776 vmw_dmabuf_unreference(&res
->backup
);
777 res
->backup
= vmw_dmabuf_reference(dmabuf_mob
);
778 res
->backup_offset
= 0;
779 vmw_resource_unreserve(res
, NULL
, 0);
780 mutex_unlock(&res
->dev_priv
->cmdbuf_mutex
);
787 static int vmw_kms_new_framebuffer_dmabuf(struct vmw_private
*dev_priv
,
788 struct vmw_dma_buffer
*dmabuf
,
789 struct vmw_framebuffer
**out
,
790 const struct drm_mode_fb_cmd
794 struct drm_device
*dev
= dev_priv
->dev
;
795 struct vmw_framebuffer_dmabuf
*vfbd
;
796 unsigned int requested_size
;
799 requested_size
= mode_cmd
->height
* mode_cmd
->pitch
;
800 if (unlikely(requested_size
> dmabuf
->base
.num_pages
* PAGE_SIZE
)) {
801 DRM_ERROR("Screen buffer object size is too small "
802 "for requested mode.\n");
806 /* Limited framebuffer color depth support for screen objects */
807 if (dev_priv
->active_display_unit
== vmw_du_screen_object
) {
808 switch (mode_cmd
->depth
) {
811 /* Only support 32 bpp for 32 and 24 depth fbs */
812 if (mode_cmd
->bpp
== 32)
815 DRM_ERROR("Invalid color depth/bbp: %d %d\n",
816 mode_cmd
->depth
, mode_cmd
->bpp
);
820 /* Only support 16 bpp for 16 and 15 depth fbs */
821 if (mode_cmd
->bpp
== 16)
824 DRM_ERROR("Invalid color depth/bbp: %d %d\n",
825 mode_cmd
->depth
, mode_cmd
->bpp
);
828 DRM_ERROR("Invalid color depth: %d\n", mode_cmd
->depth
);
833 vfbd
= kzalloc(sizeof(*vfbd
), GFP_KERNEL
);
839 if (!vmw_dmabuf_reference(dmabuf
)) {
840 DRM_ERROR("failed to reference dmabuf %p\n", dmabuf
);
845 vfbd
->base
.base
.bits_per_pixel
= mode_cmd
->bpp
;
846 vfbd
->base
.base
.pitches
[0] = mode_cmd
->pitch
;
847 vfbd
->base
.base
.depth
= mode_cmd
->depth
;
848 vfbd
->base
.base
.width
= mode_cmd
->width
;
849 vfbd
->base
.base
.height
= mode_cmd
->height
;
850 vfbd
->base
.dmabuf
= true;
851 vfbd
->buffer
= dmabuf
;
852 vfbd
->base
.user_handle
= mode_cmd
->handle
;
855 ret
= drm_framebuffer_init(dev
, &vfbd
->base
.base
,
856 &vmw_framebuffer_dmabuf_funcs
);
863 vmw_dmabuf_unreference(&dmabuf
);
871 * vmw_kms_new_framebuffer - Create a new framebuffer.
873 * @dev_priv: Pointer to device private struct.
874 * @dmabuf: Pointer to dma buffer to wrap the kms framebuffer around.
875 * Either @dmabuf or @surface must be NULL.
876 * @surface: Pointer to a surface to wrap the kms framebuffer around.
877 * Either @dmabuf or @surface must be NULL.
878 * @only_2d: No presents will occur to this dma buffer based framebuffer. This
879 * Helps the code to do some important optimizations.
880 * @mode_cmd: Frame-buffer metadata.
882 struct vmw_framebuffer
*
883 vmw_kms_new_framebuffer(struct vmw_private
*dev_priv
,
884 struct vmw_dma_buffer
*dmabuf
,
885 struct vmw_surface
*surface
,
887 const struct drm_mode_fb_cmd
*mode_cmd
)
889 struct vmw_framebuffer
*vfb
;
890 bool is_dmabuf_proxy
= false;
894 * We cannot use the SurfaceDMA command in an non-accelerated VM,
895 * therefore, wrap the DMA buf in a surface so we can use the
896 * SurfaceCopy command.
898 if (dmabuf
&& only_2d
&&
899 dev_priv
->active_display_unit
== vmw_du_screen_target
) {
900 ret
= vmw_create_dmabuf_proxy(dev_priv
->dev
, mode_cmd
,
905 is_dmabuf_proxy
= true;
908 /* Create the new framebuffer depending one what we have */
910 ret
= vmw_kms_new_framebuffer_surface(dev_priv
, surface
, &vfb
,
914 ret
= vmw_kms_new_framebuffer_dmabuf(dev_priv
, dmabuf
, &vfb
,
922 vfb
->pin
= vmw_framebuffer_pin
;
923 vfb
->unpin
= vmw_framebuffer_unpin
;
929 * Generic Kernel modesetting functions
932 static struct drm_framebuffer
*vmw_kms_fb_create(struct drm_device
*dev
,
933 struct drm_file
*file_priv
,
934 struct drm_mode_fb_cmd2
*mode_cmd2
)
936 struct vmw_private
*dev_priv
= vmw_priv(dev
);
937 struct ttm_object_file
*tfile
= vmw_fpriv(file_priv
)->tfile
;
938 struct vmw_framebuffer
*vfb
= NULL
;
939 struct vmw_surface
*surface
= NULL
;
940 struct vmw_dma_buffer
*bo
= NULL
;
941 struct ttm_base_object
*user_obj
;
942 struct drm_mode_fb_cmd mode_cmd
;
945 mode_cmd
.width
= mode_cmd2
->width
;
946 mode_cmd
.height
= mode_cmd2
->height
;
947 mode_cmd
.pitch
= mode_cmd2
->pitches
[0];
948 mode_cmd
.handle
= mode_cmd2
->handles
[0];
949 drm_fb_get_bpp_depth(mode_cmd2
->pixel_format
, &mode_cmd
.depth
,
953 * This code should be conditioned on Screen Objects not being used.
954 * If screen objects are used, we can allocate a GMR to hold the
955 * requested framebuffer.
958 if (!vmw_kms_validate_mode_vram(dev_priv
,
961 DRM_ERROR("Requested mode exceed bounding box limit.\n");
962 return ERR_PTR(-ENOMEM
);
966 * Take a reference on the user object of the resource
967 * backing the kms fb. This ensures that user-space handle
968 * lookups on that resource will always work as long as
969 * it's registered with a kms framebuffer. This is important,
970 * since vmw_execbuf_process identifies resources in the
971 * command stream using user-space handles.
974 user_obj
= ttm_base_object_lookup(tfile
, mode_cmd
.handle
);
975 if (unlikely(user_obj
== NULL
)) {
976 DRM_ERROR("Could not locate requested kms frame buffer.\n");
977 return ERR_PTR(-ENOENT
);
981 * End conditioned code.
984 /* returns either a dmabuf or surface */
985 ret
= vmw_user_lookup_handle(dev_priv
, tfile
,
991 vfb
= vmw_kms_new_framebuffer(dev_priv
, bo
, surface
,
992 !(dev_priv
->capabilities
& SVGA_CAP_3D
),
1000 /* vmw_user_lookup_handle takes one ref so does new_fb */
1002 vmw_dmabuf_unreference(&bo
);
1004 vmw_surface_unreference(&surface
);
1007 DRM_ERROR("failed to create vmw_framebuffer: %i\n", ret
);
1008 ttm_base_object_unref(&user_obj
);
1009 return ERR_PTR(ret
);
1011 vfb
->user_obj
= user_obj
;
1016 static const struct drm_mode_config_funcs vmw_kms_funcs
= {
1017 .fb_create
= vmw_kms_fb_create
,
1020 int vmw_kms_generic_present(struct vmw_private
*dev_priv
,
1021 struct drm_file
*file_priv
,
1022 struct vmw_framebuffer
*vfb
,
1023 struct vmw_surface
*surface
,
1025 int32_t destX
, int32_t destY
,
1026 struct drm_vmw_rect
*clips
,
1029 return vmw_kms_sou_do_surface_dirty(dev_priv
, vfb
, NULL
, clips
,
1030 &surface
->res
, destX
, destY
,
1031 num_clips
, 1, NULL
);
1035 int vmw_kms_present(struct vmw_private
*dev_priv
,
1036 struct drm_file
*file_priv
,
1037 struct vmw_framebuffer
*vfb
,
1038 struct vmw_surface
*surface
,
1040 int32_t destX
, int32_t destY
,
1041 struct drm_vmw_rect
*clips
,
1046 switch (dev_priv
->active_display_unit
) {
1047 case vmw_du_screen_target
:
1048 ret
= vmw_kms_stdu_surface_dirty(dev_priv
, vfb
, NULL
, clips
,
1049 &surface
->res
, destX
, destY
,
1050 num_clips
, 1, NULL
);
1052 case vmw_du_screen_object
:
1053 ret
= vmw_kms_generic_present(dev_priv
, file_priv
, vfb
, surface
,
1054 sid
, destX
, destY
, clips
,
1059 "Present called with invalid display system.\n");
1066 vmw_fifo_flush(dev_priv
, false);
1071 int vmw_kms_init(struct vmw_private
*dev_priv
)
1073 struct drm_device
*dev
= dev_priv
->dev
;
1076 drm_mode_config_init(dev
);
1077 dev
->mode_config
.funcs
= &vmw_kms_funcs
;
1078 dev
->mode_config
.min_width
= 1;
1079 dev
->mode_config
.min_height
= 1;
1080 /* assumed largest fb size */
1081 dev
->mode_config
.max_width
= 8192;
1082 dev
->mode_config
.max_height
= 8192;
1084 ret
= vmw_kms_stdu_init_display(dev_priv
);
1086 ret
= vmw_kms_sou_init_display(dev_priv
);
1087 if (ret
) /* Fallback */
1088 ret
= vmw_kms_ldu_init_display(dev_priv
);
1094 int vmw_kms_close(struct vmw_private
*dev_priv
)
1099 * Docs says we should take the lock before calling this function
1100 * but since it destroys encoders and our destructor calls
1101 * drm_encoder_cleanup which takes the lock we deadlock.
1103 drm_mode_config_cleanup(dev_priv
->dev
);
1104 if (dev_priv
->active_display_unit
== vmw_du_screen_object
)
1105 ret
= vmw_kms_sou_close_display(dev_priv
);
1106 else if (dev_priv
->active_display_unit
== vmw_du_screen_target
)
1107 ret
= vmw_kms_stdu_close_display(dev_priv
);
1109 ret
= vmw_kms_ldu_close_display(dev_priv
);
1114 int vmw_kms_cursor_bypass_ioctl(struct drm_device
*dev
, void *data
,
1115 struct drm_file
*file_priv
)
1117 struct drm_vmw_cursor_bypass_arg
*arg
= data
;
1118 struct vmw_display_unit
*du
;
1119 struct drm_crtc
*crtc
;
1123 mutex_lock(&dev
->mode_config
.mutex
);
1124 if (arg
->flags
& DRM_VMW_CURSOR_BYPASS_ALL
) {
1126 list_for_each_entry(crtc
, &dev
->mode_config
.crtc_list
, head
) {
1127 du
= vmw_crtc_to_du(crtc
);
1128 du
->hotspot_x
= arg
->xhot
;
1129 du
->hotspot_y
= arg
->yhot
;
1132 mutex_unlock(&dev
->mode_config
.mutex
);
1136 crtc
= drm_crtc_find(dev
, arg
->crtc_id
);
1142 du
= vmw_crtc_to_du(crtc
);
1144 du
->hotspot_x
= arg
->xhot
;
1145 du
->hotspot_y
= arg
->yhot
;
1148 mutex_unlock(&dev
->mode_config
.mutex
);
1153 int vmw_kms_write_svga(struct vmw_private
*vmw_priv
,
1154 unsigned width
, unsigned height
, unsigned pitch
,
1155 unsigned bpp
, unsigned depth
)
1157 if (vmw_priv
->capabilities
& SVGA_CAP_PITCHLOCK
)
1158 vmw_write(vmw_priv
, SVGA_REG_PITCHLOCK
, pitch
);
1159 else if (vmw_fifo_have_pitchlock(vmw_priv
))
1160 iowrite32(pitch
, vmw_priv
->mmio_virt
+ SVGA_FIFO_PITCHLOCK
);
1161 vmw_write(vmw_priv
, SVGA_REG_WIDTH
, width
);
1162 vmw_write(vmw_priv
, SVGA_REG_HEIGHT
, height
);
1163 vmw_write(vmw_priv
, SVGA_REG_BITS_PER_PIXEL
, bpp
);
1165 if (vmw_read(vmw_priv
, SVGA_REG_DEPTH
) != depth
) {
1166 DRM_ERROR("Invalid depth %u for %u bpp, host expects %u\n",
1167 depth
, bpp
, vmw_read(vmw_priv
, SVGA_REG_DEPTH
));
1174 int vmw_kms_save_vga(struct vmw_private
*vmw_priv
)
1176 struct vmw_vga_topology_state
*save
;
1179 vmw_priv
->vga_width
= vmw_read(vmw_priv
, SVGA_REG_WIDTH
);
1180 vmw_priv
->vga_height
= vmw_read(vmw_priv
, SVGA_REG_HEIGHT
);
1181 vmw_priv
->vga_bpp
= vmw_read(vmw_priv
, SVGA_REG_BITS_PER_PIXEL
);
1182 if (vmw_priv
->capabilities
& SVGA_CAP_PITCHLOCK
)
1183 vmw_priv
->vga_pitchlock
=
1184 vmw_read(vmw_priv
, SVGA_REG_PITCHLOCK
);
1185 else if (vmw_fifo_have_pitchlock(vmw_priv
))
1186 vmw_priv
->vga_pitchlock
= ioread32(vmw_priv
->mmio_virt
+
1187 SVGA_FIFO_PITCHLOCK
);
1189 if (!(vmw_priv
->capabilities
& SVGA_CAP_DISPLAY_TOPOLOGY
))
1192 vmw_priv
->num_displays
= vmw_read(vmw_priv
,
1193 SVGA_REG_NUM_GUEST_DISPLAYS
);
1195 if (vmw_priv
->num_displays
== 0)
1196 vmw_priv
->num_displays
= 1;
1198 for (i
= 0; i
< vmw_priv
->num_displays
; ++i
) {
1199 save
= &vmw_priv
->vga_save
[i
];
1200 vmw_write(vmw_priv
, SVGA_REG_DISPLAY_ID
, i
);
1201 save
->primary
= vmw_read(vmw_priv
, SVGA_REG_DISPLAY_IS_PRIMARY
);
1202 save
->pos_x
= vmw_read(vmw_priv
, SVGA_REG_DISPLAY_POSITION_X
);
1203 save
->pos_y
= vmw_read(vmw_priv
, SVGA_REG_DISPLAY_POSITION_Y
);
1204 save
->width
= vmw_read(vmw_priv
, SVGA_REG_DISPLAY_WIDTH
);
1205 save
->height
= vmw_read(vmw_priv
, SVGA_REG_DISPLAY_HEIGHT
);
1206 vmw_write(vmw_priv
, SVGA_REG_DISPLAY_ID
, SVGA_ID_INVALID
);
1207 if (i
== 0 && vmw_priv
->num_displays
== 1 &&
1208 save
->width
== 0 && save
->height
== 0) {
1211 * It should be fairly safe to assume that these
1212 * values are uninitialized.
1215 save
->width
= vmw_priv
->vga_width
- save
->pos_x
;
1216 save
->height
= vmw_priv
->vga_height
- save
->pos_y
;
1223 int vmw_kms_restore_vga(struct vmw_private
*vmw_priv
)
1225 struct vmw_vga_topology_state
*save
;
1228 vmw_write(vmw_priv
, SVGA_REG_WIDTH
, vmw_priv
->vga_width
);
1229 vmw_write(vmw_priv
, SVGA_REG_HEIGHT
, vmw_priv
->vga_height
);
1230 vmw_write(vmw_priv
, SVGA_REG_BITS_PER_PIXEL
, vmw_priv
->vga_bpp
);
1231 if (vmw_priv
->capabilities
& SVGA_CAP_PITCHLOCK
)
1232 vmw_write(vmw_priv
, SVGA_REG_PITCHLOCK
,
1233 vmw_priv
->vga_pitchlock
);
1234 else if (vmw_fifo_have_pitchlock(vmw_priv
))
1235 iowrite32(vmw_priv
->vga_pitchlock
,
1236 vmw_priv
->mmio_virt
+ SVGA_FIFO_PITCHLOCK
);
1238 if (!(vmw_priv
->capabilities
& SVGA_CAP_DISPLAY_TOPOLOGY
))
1241 for (i
= 0; i
< vmw_priv
->num_displays
; ++i
) {
1242 save
= &vmw_priv
->vga_save
[i
];
1243 vmw_write(vmw_priv
, SVGA_REG_DISPLAY_ID
, i
);
1244 vmw_write(vmw_priv
, SVGA_REG_DISPLAY_IS_PRIMARY
, save
->primary
);
1245 vmw_write(vmw_priv
, SVGA_REG_DISPLAY_POSITION_X
, save
->pos_x
);
1246 vmw_write(vmw_priv
, SVGA_REG_DISPLAY_POSITION_Y
, save
->pos_y
);
1247 vmw_write(vmw_priv
, SVGA_REG_DISPLAY_WIDTH
, save
->width
);
1248 vmw_write(vmw_priv
, SVGA_REG_DISPLAY_HEIGHT
, save
->height
);
1249 vmw_write(vmw_priv
, SVGA_REG_DISPLAY_ID
, SVGA_ID_INVALID
);
1255 bool vmw_kms_validate_mode_vram(struct vmw_private
*dev_priv
,
1259 return ((u64
) pitch
* (u64
) height
) < (u64
)
1260 ((dev_priv
->active_display_unit
== vmw_du_screen_target
) ?
1261 dev_priv
->prim_bb_mem
: dev_priv
->vram_size
);
1266 * Function called by DRM code called with vbl_lock held.
1268 u32
vmw_get_vblank_counter(struct drm_device
*dev
, int crtc
)
1274 * Function called by DRM code called with vbl_lock held.
1276 int vmw_enable_vblank(struct drm_device
*dev
, int crtc
)
1282 * Function called by DRM code called with vbl_lock held.
1284 void vmw_disable_vblank(struct drm_device
*dev
, int crtc
)
1290 * Small shared kms functions.
1293 static int vmw_du_update_layout(struct vmw_private
*dev_priv
, unsigned num
,
1294 struct drm_vmw_rect
*rects
)
1296 struct drm_device
*dev
= dev_priv
->dev
;
1297 struct vmw_display_unit
*du
;
1298 struct drm_connector
*con
;
1300 mutex_lock(&dev
->mode_config
.mutex
);
1306 DRM_INFO("%s: new layout ", __func__
);
1307 for (i
= 0; i
< num
; i
++)
1308 DRM_INFO("(%i, %i %ux%u) ", rects
[i
].x
, rects
[i
].y
,
1309 rects
[i
].w
, rects
[i
].h
);
1314 list_for_each_entry(con
, &dev
->mode_config
.connector_list
, head
) {
1315 du
= vmw_connector_to_du(con
);
1316 if (num
> du
->unit
) {
1317 du
->pref_width
= rects
[du
->unit
].w
;
1318 du
->pref_height
= rects
[du
->unit
].h
;
1319 du
->pref_active
= true;
1320 du
->gui_x
= rects
[du
->unit
].x
;
1321 du
->gui_y
= rects
[du
->unit
].y
;
1323 du
->pref_width
= 800;
1324 du
->pref_height
= 600;
1325 du
->pref_active
= false;
1327 con
->status
= vmw_du_connector_detect(con
, true);
1330 mutex_unlock(&dev
->mode_config
.mutex
);
1335 void vmw_du_crtc_save(struct drm_crtc
*crtc
)
1339 void vmw_du_crtc_restore(struct drm_crtc
*crtc
)
1343 void vmw_du_crtc_gamma_set(struct drm_crtc
*crtc
,
1344 u16
*r
, u16
*g
, u16
*b
,
1345 uint32_t start
, uint32_t size
)
1347 struct vmw_private
*dev_priv
= vmw_priv(crtc
->dev
);
1350 for (i
= 0; i
< size
; i
++) {
1351 DRM_DEBUG("%d r/g/b = 0x%04x / 0x%04x / 0x%04x\n", i
,
1353 vmw_write(dev_priv
, SVGA_PALETTE_BASE
+ i
* 3 + 0, r
[i
] >> 8);
1354 vmw_write(dev_priv
, SVGA_PALETTE_BASE
+ i
* 3 + 1, g
[i
] >> 8);
1355 vmw_write(dev_priv
, SVGA_PALETTE_BASE
+ i
* 3 + 2, b
[i
] >> 8);
1359 void vmw_du_connector_dpms(struct drm_connector
*connector
, int mode
)
1363 void vmw_du_connector_save(struct drm_connector
*connector
)
1367 void vmw_du_connector_restore(struct drm_connector
*connector
)
1371 enum drm_connector_status
1372 vmw_du_connector_detect(struct drm_connector
*connector
, bool force
)
1374 uint32_t num_displays
;
1375 struct drm_device
*dev
= connector
->dev
;
1376 struct vmw_private
*dev_priv
= vmw_priv(dev
);
1377 struct vmw_display_unit
*du
= vmw_connector_to_du(connector
);
1379 num_displays
= vmw_read(dev_priv
, SVGA_REG_NUM_DISPLAYS
);
1381 return ((vmw_connector_to_du(connector
)->unit
< num_displays
&&
1383 connector_status_connected
: connector_status_disconnected
);
1386 static struct drm_display_mode vmw_kms_connector_builtin
[] = {
1388 { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER
, 25175, 640, 656,
1389 752, 800, 0, 480, 489, 492, 525, 0,
1390 DRM_MODE_FLAG_NHSYNC
| DRM_MODE_FLAG_NVSYNC
) },
1392 { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER
, 40000, 800, 840,
1393 968, 1056, 0, 600, 601, 605, 628, 0,
1394 DRM_MODE_FLAG_PHSYNC
| DRM_MODE_FLAG_PVSYNC
) },
1396 { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER
, 65000, 1024, 1048,
1397 1184, 1344, 0, 768, 771, 777, 806, 0,
1398 DRM_MODE_FLAG_NHSYNC
| DRM_MODE_FLAG_NVSYNC
) },
1400 { DRM_MODE("1152x864", DRM_MODE_TYPE_DRIVER
, 108000, 1152, 1216,
1401 1344, 1600, 0, 864, 865, 868, 900, 0,
1402 DRM_MODE_FLAG_PHSYNC
| DRM_MODE_FLAG_PVSYNC
) },
1404 { DRM_MODE("1280x768", DRM_MODE_TYPE_DRIVER
, 79500, 1280, 1344,
1405 1472, 1664, 0, 768, 771, 778, 798, 0,
1406 DRM_MODE_FLAG_NHSYNC
| DRM_MODE_FLAG_PVSYNC
) },
1408 { DRM_MODE("1280x800", DRM_MODE_TYPE_DRIVER
, 83500, 1280, 1352,
1409 1480, 1680, 0, 800, 803, 809, 831, 0,
1410 DRM_MODE_FLAG_PHSYNC
| DRM_MODE_FLAG_NVSYNC
) },
1412 { DRM_MODE("1280x960", DRM_MODE_TYPE_DRIVER
, 108000, 1280, 1376,
1413 1488, 1800, 0, 960, 961, 964, 1000, 0,
1414 DRM_MODE_FLAG_PHSYNC
| DRM_MODE_FLAG_PVSYNC
) },
1415 /* 1280x1024@60Hz */
1416 { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER
, 108000, 1280, 1328,
1417 1440, 1688, 0, 1024, 1025, 1028, 1066, 0,
1418 DRM_MODE_FLAG_PHSYNC
| DRM_MODE_FLAG_PVSYNC
) },
1420 { DRM_MODE("1360x768", DRM_MODE_TYPE_DRIVER
, 85500, 1360, 1424,
1421 1536, 1792, 0, 768, 771, 777, 795, 0,
1422 DRM_MODE_FLAG_PHSYNC
| DRM_MODE_FLAG_PVSYNC
) },
1423 /* 1440x1050@60Hz */
1424 { DRM_MODE("1400x1050", DRM_MODE_TYPE_DRIVER
, 121750, 1400, 1488,
1425 1632, 1864, 0, 1050, 1053, 1057, 1089, 0,
1426 DRM_MODE_FLAG_NHSYNC
| DRM_MODE_FLAG_PVSYNC
) },
1428 { DRM_MODE("1440x900", DRM_MODE_TYPE_DRIVER
, 106500, 1440, 1520,
1429 1672, 1904, 0, 900, 903, 909, 934, 0,
1430 DRM_MODE_FLAG_NHSYNC
| DRM_MODE_FLAG_PVSYNC
) },
1431 /* 1600x1200@60Hz */
1432 { DRM_MODE("1600x1200", DRM_MODE_TYPE_DRIVER
, 162000, 1600, 1664,
1433 1856, 2160, 0, 1200, 1201, 1204, 1250, 0,
1434 DRM_MODE_FLAG_PHSYNC
| DRM_MODE_FLAG_PVSYNC
) },
1435 /* 1680x1050@60Hz */
1436 { DRM_MODE("1680x1050", DRM_MODE_TYPE_DRIVER
, 146250, 1680, 1784,
1437 1960, 2240, 0, 1050, 1053, 1059, 1089, 0,
1438 DRM_MODE_FLAG_NHSYNC
| DRM_MODE_FLAG_PVSYNC
) },
1439 /* 1792x1344@60Hz */
1440 { DRM_MODE("1792x1344", DRM_MODE_TYPE_DRIVER
, 204750, 1792, 1920,
1441 2120, 2448, 0, 1344, 1345, 1348, 1394, 0,
1442 DRM_MODE_FLAG_NHSYNC
| DRM_MODE_FLAG_PVSYNC
) },
1443 /* 1853x1392@60Hz */
1444 { DRM_MODE("1856x1392", DRM_MODE_TYPE_DRIVER
, 218250, 1856, 1952,
1445 2176, 2528, 0, 1392, 1393, 1396, 1439, 0,
1446 DRM_MODE_FLAG_NHSYNC
| DRM_MODE_FLAG_PVSYNC
) },
1447 /* 1920x1200@60Hz */
1448 { DRM_MODE("1920x1200", DRM_MODE_TYPE_DRIVER
, 193250, 1920, 2056,
1449 2256, 2592, 0, 1200, 1203, 1209, 1245, 0,
1450 DRM_MODE_FLAG_NHSYNC
| DRM_MODE_FLAG_PVSYNC
) },
1451 /* 1920x1440@60Hz */
1452 { DRM_MODE("1920x1440", DRM_MODE_TYPE_DRIVER
, 234000, 1920, 2048,
1453 2256, 2600, 0, 1440, 1441, 1444, 1500, 0,
1454 DRM_MODE_FLAG_NHSYNC
| DRM_MODE_FLAG_PVSYNC
) },
1455 /* 2560x1600@60Hz */
1456 { DRM_MODE("2560x1600", DRM_MODE_TYPE_DRIVER
, 348500, 2560, 2752,
1457 3032, 3504, 0, 1600, 1603, 1609, 1658, 0,
1458 DRM_MODE_FLAG_NHSYNC
| DRM_MODE_FLAG_PVSYNC
) },
1460 { DRM_MODE("", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) },
1464 * vmw_guess_mode_timing - Provide fake timings for a
1465 * 60Hz vrefresh mode.
1467 * @mode - Pointer to a struct drm_display_mode with hdisplay and vdisplay
1468 * members filled in.
1470 void vmw_guess_mode_timing(struct drm_display_mode
*mode
)
1472 mode
->hsync_start
= mode
->hdisplay
+ 50;
1473 mode
->hsync_end
= mode
->hsync_start
+ 50;
1474 mode
->htotal
= mode
->hsync_end
+ 50;
1476 mode
->vsync_start
= mode
->vdisplay
+ 50;
1477 mode
->vsync_end
= mode
->vsync_start
+ 50;
1478 mode
->vtotal
= mode
->vsync_end
+ 50;
1480 mode
->clock
= (u32
)mode
->htotal
* (u32
)mode
->vtotal
/ 100 * 6;
1481 mode
->vrefresh
= drm_mode_vrefresh(mode
);
1485 int vmw_du_connector_fill_modes(struct drm_connector
*connector
,
1486 uint32_t max_width
, uint32_t max_height
)
1488 struct vmw_display_unit
*du
= vmw_connector_to_du(connector
);
1489 struct drm_device
*dev
= connector
->dev
;
1490 struct vmw_private
*dev_priv
= vmw_priv(dev
);
1491 struct drm_display_mode
*mode
= NULL
;
1492 struct drm_display_mode
*bmode
;
1493 struct drm_display_mode prefmode
= { DRM_MODE("preferred",
1494 DRM_MODE_TYPE_DRIVER
| DRM_MODE_TYPE_PREFERRED
,
1495 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1496 DRM_MODE_FLAG_NHSYNC
| DRM_MODE_FLAG_PVSYNC
)
1499 u32 assumed_bpp
= 2;
1502 * If using screen objects, then assume 32-bpp because that's what the
1503 * SVGA device is assuming
1505 if (dev_priv
->active_display_unit
== vmw_du_screen_object
)
1508 if (dev_priv
->active_display_unit
== vmw_du_screen_target
) {
1509 max_width
= min(max_width
, dev_priv
->stdu_max_width
);
1510 max_height
= min(max_height
, dev_priv
->stdu_max_height
);
1513 /* Add preferred mode */
1514 mode
= drm_mode_duplicate(dev
, &prefmode
);
1517 mode
->hdisplay
= du
->pref_width
;
1518 mode
->vdisplay
= du
->pref_height
;
1519 vmw_guess_mode_timing(mode
);
1521 if (vmw_kms_validate_mode_vram(dev_priv
,
1522 mode
->hdisplay
* assumed_bpp
,
1524 drm_mode_probed_add(connector
, mode
);
1526 drm_mode_destroy(dev
, mode
);
1530 if (du
->pref_mode
) {
1531 list_del_init(&du
->pref_mode
->head
);
1532 drm_mode_destroy(dev
, du
->pref_mode
);
1535 /* mode might be null here, this is intended */
1536 du
->pref_mode
= mode
;
1538 for (i
= 0; vmw_kms_connector_builtin
[i
].type
!= 0; i
++) {
1539 bmode
= &vmw_kms_connector_builtin
[i
];
1540 if (bmode
->hdisplay
> max_width
||
1541 bmode
->vdisplay
> max_height
)
1544 if (!vmw_kms_validate_mode_vram(dev_priv
,
1545 bmode
->hdisplay
* assumed_bpp
,
1549 mode
= drm_mode_duplicate(dev
, bmode
);
1552 mode
->vrefresh
= drm_mode_vrefresh(mode
);
1554 drm_mode_probed_add(connector
, mode
);
1557 drm_mode_connector_list_update(connector
, true);
1558 /* Move the prefered mode first, help apps pick the right mode. */
1559 drm_mode_sort(&connector
->modes
);
1564 int vmw_du_connector_set_property(struct drm_connector
*connector
,
1565 struct drm_property
*property
,
1572 int vmw_kms_update_layout_ioctl(struct drm_device
*dev
, void *data
,
1573 struct drm_file
*file_priv
)
1575 struct vmw_private
*dev_priv
= vmw_priv(dev
);
1576 struct drm_vmw_update_layout_arg
*arg
=
1577 (struct drm_vmw_update_layout_arg
*)data
;
1578 void __user
*user_rects
;
1579 struct drm_vmw_rect
*rects
;
1580 unsigned rects_size
;
1583 struct drm_mode_config
*mode_config
= &dev
->mode_config
;
1584 struct drm_vmw_rect bounding_box
= {0};
1586 if (!arg
->num_outputs
) {
1587 struct drm_vmw_rect def_rect
= {0, 0, 800, 600};
1588 vmw_du_update_layout(dev_priv
, 1, &def_rect
);
1592 rects_size
= arg
->num_outputs
* sizeof(struct drm_vmw_rect
);
1593 rects
= kcalloc(arg
->num_outputs
, sizeof(struct drm_vmw_rect
),
1595 if (unlikely(!rects
))
1598 user_rects
= (void __user
*)(unsigned long)arg
->rects
;
1599 ret
= copy_from_user(rects
, user_rects
, rects_size
);
1600 if (unlikely(ret
!= 0)) {
1601 DRM_ERROR("Failed to get rects.\n");
1606 for (i
= 0; i
< arg
->num_outputs
; ++i
) {
1607 if (rects
[i
].x
< 0 ||
1609 rects
[i
].x
+ rects
[i
].w
> mode_config
->max_width
||
1610 rects
[i
].y
+ rects
[i
].h
> mode_config
->max_height
) {
1611 DRM_ERROR("Invalid GUI layout.\n");
1617 * bounding_box.w and bunding_box.h are used as
1618 * lower-right coordinates
1620 if (rects
[i
].x
+ rects
[i
].w
> bounding_box
.w
)
1621 bounding_box
.w
= rects
[i
].x
+ rects
[i
].w
;
1623 if (rects
[i
].y
+ rects
[i
].h
> bounding_box
.h
)
1624 bounding_box
.h
= rects
[i
].y
+ rects
[i
].h
;
1628 * For Screen Target Display Unit, all the displays must fit
1629 * inside of maximum texture size.
1631 if (dev_priv
->active_display_unit
== vmw_du_screen_target
)
1632 if (bounding_box
.w
> dev_priv
->texture_max_width
||
1633 bounding_box
.h
> dev_priv
->texture_max_height
) {
1634 DRM_ERROR("Layout exceeds maximum texture size\n");
1640 vmw_du_update_layout(dev_priv
, arg
->num_outputs
, rects
);
1648 * vmw_kms_helper_dirty - Helper to build commands and perform actions based
1649 * on a set of cliprects and a set of display units.
1651 * @dev_priv: Pointer to a device private structure.
1652 * @framebuffer: Pointer to the framebuffer on which to perform the actions.
1653 * @clips: A set of struct drm_clip_rect. Either this os @vclips must be NULL.
1654 * Cliprects are given in framebuffer coordinates.
1655 * @vclips: A set of struct drm_vmw_rect cliprects. Either this or @clips must
1656 * be NULL. Cliprects are given in source coordinates.
1657 * @dest_x: X coordinate offset for the crtc / destination clip rects.
1658 * @dest_y: Y coordinate offset for the crtc / destination clip rects.
1659 * @num_clips: Number of cliprects in the @clips or @vclips array.
1660 * @increment: Integer with which to increment the clip counter when looping.
1661 * Used to skip a predetermined number of clip rects.
1662 * @dirty: Closure structure. See the description of struct vmw_kms_dirty.
1664 int vmw_kms_helper_dirty(struct vmw_private
*dev_priv
,
1665 struct vmw_framebuffer
*framebuffer
,
1666 const struct drm_clip_rect
*clips
,
1667 const struct drm_vmw_rect
*vclips
,
1668 s32 dest_x
, s32 dest_y
,
1671 struct vmw_kms_dirty
*dirty
)
1673 struct vmw_display_unit
*units
[VMWGFX_NUM_DISPLAY_UNITS
];
1674 struct drm_crtc
*crtc
;
1679 dirty
->dev_priv
= dev_priv
;
1681 list_for_each_entry(crtc
, &dev_priv
->dev
->mode_config
.crtc_list
, head
) {
1682 if (crtc
->primary
->fb
!= &framebuffer
->base
)
1684 units
[num_units
++] = vmw_crtc_to_du(crtc
);
1687 for (k
= 0; k
< num_units
; k
++) {
1688 struct vmw_display_unit
*unit
= units
[k
];
1689 s32 crtc_x
= unit
->crtc
.x
;
1690 s32 crtc_y
= unit
->crtc
.y
;
1691 s32 crtc_width
= unit
->crtc
.mode
.hdisplay
;
1692 s32 crtc_height
= unit
->crtc
.mode
.vdisplay
;
1693 const struct drm_clip_rect
*clips_ptr
= clips
;
1694 const struct drm_vmw_rect
*vclips_ptr
= vclips
;
1697 if (dirty
->fifo_reserve_size
> 0) {
1698 dirty
->cmd
= vmw_fifo_reserve(dev_priv
,
1699 dirty
->fifo_reserve_size
);
1701 DRM_ERROR("Couldn't reserve fifo space "
1702 "for dirty blits.\n");
1705 memset(dirty
->cmd
, 0, dirty
->fifo_reserve_size
);
1707 dirty
->num_hits
= 0;
1708 for (i
= 0; i
< num_clips
; i
++, clips_ptr
+= increment
,
1709 vclips_ptr
+= increment
) {
1714 * Select clip array type. Note that integer type
1715 * in @clips is unsigned short, whereas in @vclips
1719 dirty
->fb_x
= (s32
) clips_ptr
->x1
;
1720 dirty
->fb_y
= (s32
) clips_ptr
->y1
;
1721 dirty
->unit_x2
= (s32
) clips_ptr
->x2
+ dest_x
-
1723 dirty
->unit_y2
= (s32
) clips_ptr
->y2
+ dest_y
-
1726 dirty
->fb_x
= vclips_ptr
->x
;
1727 dirty
->fb_y
= vclips_ptr
->y
;
1728 dirty
->unit_x2
= dirty
->fb_x
+ vclips_ptr
->w
+
1730 dirty
->unit_y2
= dirty
->fb_y
+ vclips_ptr
->h
+
1734 dirty
->unit_x1
= dirty
->fb_x
+ dest_x
- crtc_x
;
1735 dirty
->unit_y1
= dirty
->fb_y
+ dest_y
- crtc_y
;
1737 /* Skip this clip if it's outside the crtc region */
1738 if (dirty
->unit_x1
>= crtc_width
||
1739 dirty
->unit_y1
>= crtc_height
||
1740 dirty
->unit_x2
<= 0 || dirty
->unit_y2
<= 0)
1743 /* Clip right and bottom to crtc limits */
1744 dirty
->unit_x2
= min_t(s32
, dirty
->unit_x2
,
1746 dirty
->unit_y2
= min_t(s32
, dirty
->unit_y2
,
1749 /* Clip left and top to crtc limits */
1750 clip_left
= min_t(s32
, dirty
->unit_x1
, 0);
1751 clip_top
= min_t(s32
, dirty
->unit_y1
, 0);
1752 dirty
->unit_x1
-= clip_left
;
1753 dirty
->unit_y1
-= clip_top
;
1754 dirty
->fb_x
-= clip_left
;
1755 dirty
->fb_y
-= clip_top
;
1760 dirty
->fifo_commit(dirty
);
1767 * vmw_kms_helper_buffer_prepare - Reserve and validate a buffer object before
1768 * command submission.
1770 * @dev_priv. Pointer to a device private structure.
1771 * @buf: The buffer object
1772 * @interruptible: Whether to perform waits as interruptible.
1773 * @validate_as_mob: Whether the buffer should be validated as a MOB. If false,
1774 * The buffer will be validated as a GMR. Already pinned buffers will not be
1777 * Returns 0 on success, negative error code on failure, -ERESTARTSYS if
1778 * interrupted by a signal.
1780 int vmw_kms_helper_buffer_prepare(struct vmw_private
*dev_priv
,
1781 struct vmw_dma_buffer
*buf
,
1783 bool validate_as_mob
)
1785 struct ttm_buffer_object
*bo
= &buf
->base
;
1788 ttm_bo_reserve(bo
, false, false, interruptible
, 0);
1789 ret
= vmw_validate_single_buffer(dev_priv
, bo
, interruptible
,
1792 ttm_bo_unreserve(bo
);
1798 * vmw_kms_helper_buffer_revert - Undo the actions of
1799 * vmw_kms_helper_buffer_prepare.
1801 * @res: Pointer to the buffer object.
1803 * Helper to be used if an error forces the caller to undo the actions of
1804 * vmw_kms_helper_buffer_prepare.
1806 void vmw_kms_helper_buffer_revert(struct vmw_dma_buffer
*buf
)
1809 ttm_bo_unreserve(&buf
->base
);
1813 * vmw_kms_helper_buffer_finish - Unreserve and fence a buffer object after
1814 * kms command submission.
1816 * @dev_priv: Pointer to a device private structure.
1817 * @file_priv: Pointer to a struct drm_file representing the caller's
1818 * connection. Must be set to NULL if @user_fence_rep is NULL, and conversely
1819 * if non-NULL, @user_fence_rep must be non-NULL.
1820 * @buf: The buffer object.
1821 * @out_fence: Optional pointer to a fence pointer. If non-NULL, a
1822 * ref-counted fence pointer is returned here.
1823 * @user_fence_rep: Optional pointer to a user-space provided struct
1824 * drm_vmw_fence_rep. If provided, @file_priv must also be provided and the
1825 * function copies fence data to user-space in a fail-safe manner.
1827 void vmw_kms_helper_buffer_finish(struct vmw_private
*dev_priv
,
1828 struct drm_file
*file_priv
,
1829 struct vmw_dma_buffer
*buf
,
1830 struct vmw_fence_obj
**out_fence
,
1831 struct drm_vmw_fence_rep __user
*
1834 struct vmw_fence_obj
*fence
;
1838 ret
= vmw_execbuf_fence_commands(file_priv
, dev_priv
, &fence
,
1839 file_priv
? &handle
: NULL
);
1841 vmw_fence_single_bo(&buf
->base
, fence
);
1843 vmw_execbuf_copy_fence_user(dev_priv
, vmw_fpriv(file_priv
),
1844 ret
, user_fence_rep
, fence
,
1849 vmw_fence_obj_unreference(&fence
);
1851 vmw_kms_helper_buffer_revert(buf
);
1856 * vmw_kms_helper_resource_revert - Undo the actions of
1857 * vmw_kms_helper_resource_prepare.
1859 * @res: Pointer to the resource. Typically a surface.
1861 * Helper to be used if an error forces the caller to undo the actions of
1862 * vmw_kms_helper_resource_prepare.
1864 void vmw_kms_helper_resource_revert(struct vmw_resource
*res
)
1866 vmw_kms_helper_buffer_revert(res
->backup
);
1867 vmw_resource_unreserve(res
, NULL
, 0);
1868 mutex_unlock(&res
->dev_priv
->cmdbuf_mutex
);
1872 * vmw_kms_helper_resource_prepare - Reserve and validate a resource before
1873 * command submission.
1875 * @res: Pointer to the resource. Typically a surface.
1876 * @interruptible: Whether to perform waits as interruptible.
1878 * Reserves and validates also the backup buffer if a guest-backed resource.
1879 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
1880 * interrupted by a signal.
1882 int vmw_kms_helper_resource_prepare(struct vmw_resource
*res
,
1888 ret
= mutex_lock_interruptible(&res
->dev_priv
->cmdbuf_mutex
);
1890 mutex_lock(&res
->dev_priv
->cmdbuf_mutex
);
1892 if (unlikely(ret
!= 0))
1893 return -ERESTARTSYS
;
1895 ret
= vmw_resource_reserve(res
, interruptible
, false);
1900 ret
= vmw_kms_helper_buffer_prepare(res
->dev_priv
, res
->backup
,
1902 res
->dev_priv
->has_mob
);
1906 ret
= vmw_resource_validate(res
);
1912 vmw_kms_helper_buffer_revert(res
->backup
);
1914 vmw_resource_unreserve(res
, NULL
, 0);
1916 mutex_unlock(&res
->dev_priv
->cmdbuf_mutex
);
1921 * vmw_kms_helper_resource_finish - Unreserve and fence a resource after
1922 * kms command submission.
1924 * @res: Pointer to the resource. Typically a surface.
1925 * @out_fence: Optional pointer to a fence pointer. If non-NULL, a
1926 * ref-counted fence pointer is returned here.
1928 void vmw_kms_helper_resource_finish(struct vmw_resource
*res
,
1929 struct vmw_fence_obj
**out_fence
)
1931 if (res
->backup
|| out_fence
)
1932 vmw_kms_helper_buffer_finish(res
->dev_priv
, NULL
, res
->backup
,
1935 vmw_resource_unreserve(res
, NULL
, 0);
1936 mutex_unlock(&res
->dev_priv
->cmdbuf_mutex
);
1940 * vmw_kms_update_proxy - Helper function to update a proxy surface from
1943 * @res: Pointer to the surface resource
1944 * @clips: Clip rects in framebuffer (surface) space.
1945 * @num_clips: Number of clips in @clips.
1946 * @increment: Integer with which to increment the clip counter when looping.
1947 * Used to skip a predetermined number of clip rects.
1949 * This function makes sure the proxy surface is updated from its backing MOB
1950 * using the region given by @clips. The surface resource @res and its backing
1951 * MOB needs to be reserved and validated on call.
1953 int vmw_kms_update_proxy(struct vmw_resource
*res
,
1954 const struct drm_clip_rect
*clips
,
1958 struct vmw_private
*dev_priv
= res
->dev_priv
;
1959 struct drm_vmw_size
*size
= &vmw_res_to_srf(res
)->base_size
;
1961 SVGA3dCmdHeader header
;
1962 SVGA3dCmdUpdateGBImage body
;
1965 size_t copy_size
= 0;
1971 cmd
= vmw_fifo_reserve(dev_priv
, sizeof(*cmd
) * num_clips
);
1973 DRM_ERROR("Couldn't reserve fifo space for proxy surface "
1978 for (i
= 0; i
< num_clips
; ++i
, clips
+= increment
, ++cmd
) {
1979 box
= &cmd
->body
.box
;
1981 cmd
->header
.id
= SVGA_3D_CMD_UPDATE_GB_IMAGE
;
1982 cmd
->header
.size
= sizeof(cmd
->body
);
1983 cmd
->body
.image
.sid
= res
->id
;
1984 cmd
->body
.image
.face
= 0;
1985 cmd
->body
.image
.mipmap
= 0;
1987 if (clips
->x1
> size
->width
|| clips
->x2
> size
->width
||
1988 clips
->y1
> size
->height
|| clips
->y2
> size
->height
) {
1989 DRM_ERROR("Invalid clips outsize of framebuffer.\n");
1996 box
->w
= clips
->x2
- clips
->x1
;
1997 box
->h
= clips
->y2
- clips
->y1
;
2000 copy_size
+= sizeof(*cmd
);
2003 vmw_fifo_commit(dev_priv
, copy_size
);
2008 int vmw_kms_fbdev_init_data(struct vmw_private
*dev_priv
,
2012 struct drm_connector
**p_con
,
2013 struct drm_crtc
**p_crtc
,
2014 struct drm_display_mode
**p_mode
)
2016 struct drm_connector
*con
;
2017 struct vmw_display_unit
*du
;
2018 struct drm_display_mode
*mode
;
2021 list_for_each_entry(con
, &dev_priv
->dev
->mode_config
.connector_list
,
2030 DRM_ERROR("Could not find initial display unit.\n");
2034 if (list_empty(&con
->modes
))
2035 (void) vmw_du_connector_fill_modes(con
, max_width
, max_height
);
2037 if (list_empty(&con
->modes
)) {
2038 DRM_ERROR("Could not find initial display mode.\n");
2042 du
= vmw_connector_to_du(con
);
2044 *p_crtc
= &du
->crtc
;
2046 list_for_each_entry(mode
, &con
->modes
, head
) {
2047 if (mode
->type
& DRM_MODE_TYPE_PREFERRED
)
2051 if (mode
->type
& DRM_MODE_TYPE_PREFERRED
)
2054 WARN_ONCE(true, "Could not find initial preferred mode.\n");
2055 *p_mode
= list_first_entry(&con
->modes
,
2056 struct drm_display_mode
,