1 /**************************************************************************
3 * Copyright © 2009 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_display_unit_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_crtc_cleanup(&du->crtc);
41 drm_encoder_cleanup(&du->encoder);
42 drm_connector_cleanup(&du->connector);
46 * Display Unit Cursor functions
49 int vmw_cursor_update_image(struct vmw_private *dev_priv,
50 u32 *image, u32 width, u32 height,
51 u32 hotspotX, u32 hotspotY)
55 SVGAFifoCmdDefineAlphaCursor cursor;
57 u32 image_size = width * height * 4;
58 u32 cmd_size = sizeof(*cmd) + image_size;
63 cmd = vmw_fifo_reserve(dev_priv, cmd_size);
64 if (unlikely(cmd == NULL)) {
65 DRM_ERROR("Fifo reserve failed.\n");
69 memset(cmd, 0, sizeof(*cmd));
71 memcpy(&cmd[1], image, image_size);
73 cmd->cmd = cpu_to_le32(SVGA_CMD_DEFINE_ALPHA_CURSOR);
74 cmd->cursor.id = cpu_to_le32(0);
75 cmd->cursor.width = cpu_to_le32(width);
76 cmd->cursor.height = cpu_to_le32(height);
77 cmd->cursor.hotspotX = cpu_to_le32(hotspotX);
78 cmd->cursor.hotspotY = cpu_to_le32(hotspotY);
80 vmw_fifo_commit(dev_priv, cmd_size);
85 void vmw_cursor_update_position(struct vmw_private *dev_priv,
86 bool show, int x, int y)
88 __le32 __iomem *fifo_mem = dev_priv->mmio_virt;
91 iowrite32(show ? 1 : 0, fifo_mem + SVGA_FIFO_CURSOR_ON);
92 iowrite32(x, fifo_mem + SVGA_FIFO_CURSOR_X);
93 iowrite32(y, fifo_mem + SVGA_FIFO_CURSOR_Y);
94 count = ioread32(fifo_mem + SVGA_FIFO_CURSOR_COUNT);
95 iowrite32(++count, fifo_mem + SVGA_FIFO_CURSOR_COUNT);
98 int vmw_du_crtc_cursor_set(struct drm_crtc *crtc, struct drm_file *file_priv,
99 uint32_t handle, uint32_t width, uint32_t height)
101 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
102 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
103 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
104 struct vmw_surface *surface = NULL;
105 struct vmw_dma_buffer *dmabuf = NULL;
109 ret = vmw_user_surface_lookup_handle(dev_priv, tfile,
112 if (!surface->snooper.image) {
113 DRM_ERROR("surface not suitable for cursor\n");
117 ret = vmw_user_dmabuf_lookup(tfile,
120 DRM_ERROR("failed to find surface or dmabuf: %i\n", ret);
126 /* takedown old cursor */
127 if (du->cursor_surface) {
128 du->cursor_surface->snooper.crtc = NULL;
129 vmw_surface_unreference(&du->cursor_surface);
131 if (du->cursor_dmabuf)
132 vmw_dmabuf_unreference(&du->cursor_dmabuf);
134 /* setup new image */
136 /* vmw_user_surface_lookup takes one reference */
137 du->cursor_surface = surface;
139 du->cursor_surface->snooper.crtc = crtc;
140 du->cursor_age = du->cursor_surface->snooper.age;
141 vmw_cursor_update_image(dev_priv, surface->snooper.image,
142 64, 64, du->hotspot_x, du->hotspot_y);
144 struct ttm_bo_kmap_obj map;
145 unsigned long kmap_offset;
146 unsigned long kmap_num;
150 /* vmw_user_surface_lookup takes one reference */
151 du->cursor_dmabuf = dmabuf;
154 kmap_num = (64*64*4) >> PAGE_SHIFT;
156 ret = ttm_bo_reserve(&dmabuf->base, true, false, false, 0);
157 if (unlikely(ret != 0)) {
158 DRM_ERROR("reserve failed\n");
162 ret = ttm_bo_kmap(&dmabuf->base, kmap_offset, kmap_num, &map);
163 if (unlikely(ret != 0))
166 virtual = ttm_kmap_obj_virtual(&map, &dummy);
167 vmw_cursor_update_image(dev_priv, virtual, 64, 64,
168 du->hotspot_x, du->hotspot_y);
172 ttm_bo_unreserve(&dmabuf->base);
175 vmw_cursor_update_position(dev_priv, false, 0, 0);
179 vmw_cursor_update_position(dev_priv, true,
180 du->cursor_x + du->hotspot_x,
181 du->cursor_y + du->hotspot_y);
186 int vmw_du_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
188 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
189 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
190 bool shown = du->cursor_surface || du->cursor_dmabuf ? true : false;
192 du->cursor_x = x + crtc->x;
193 du->cursor_y = y + crtc->y;
195 vmw_cursor_update_position(dev_priv, shown,
196 du->cursor_x + du->hotspot_x,
197 du->cursor_y + du->hotspot_y);
202 void vmw_kms_cursor_snoop(struct vmw_surface *srf,
203 struct ttm_object_file *tfile,
204 struct ttm_buffer_object *bo,
205 SVGA3dCmdHeader *header)
207 struct ttm_bo_kmap_obj map;
208 unsigned long kmap_offset;
209 unsigned long kmap_num;
215 SVGA3dCmdHeader header;
216 SVGA3dCmdSurfaceDMA dma;
220 cmd = container_of(header, struct vmw_dma_cmd, header);
222 /* No snooper installed */
223 if (!srf->snooper.image)
226 if (cmd->dma.host.face != 0 || cmd->dma.host.mipmap != 0) {
227 DRM_ERROR("face and mipmap for cursors should never != 0\n");
231 if (cmd->header.size < 64) {
232 DRM_ERROR("at least one full copy box must be given\n");
236 box = (SVGA3dCopyBox *)&cmd[1];
237 box_count = (cmd->header.size - sizeof(SVGA3dCmdSurfaceDMA)) /
238 sizeof(SVGA3dCopyBox);
240 if (cmd->dma.guest.pitch != (64 * 4) ||
241 cmd->dma.guest.ptr.offset % PAGE_SIZE ||
242 box->x != 0 || box->y != 0 || box->z != 0 ||
243 box->srcx != 0 || box->srcy != 0 || box->srcz != 0 ||
244 box->w != 64 || box->h != 64 || box->d != 1 ||
246 /* TODO handle none page aligned offsets */
247 /* TODO handle partial uploads and pitch != 256 */
248 /* TODO handle more then one copy (size != 64) */
249 DRM_ERROR("lazy programmer, can't handle weird stuff\n");
253 kmap_offset = cmd->dma.guest.ptr.offset >> PAGE_SHIFT;
254 kmap_num = (64*64*4) >> PAGE_SHIFT;
256 ret = ttm_bo_reserve(bo, true, false, false, 0);
257 if (unlikely(ret != 0)) {
258 DRM_ERROR("reserve failed\n");
262 ret = ttm_bo_kmap(bo, kmap_offset, kmap_num, &map);
263 if (unlikely(ret != 0))
266 virtual = ttm_kmap_obj_virtual(&map, &dummy);
268 memcpy(srf->snooper.image, virtual, 64*64*4);
271 /* we can't call this function from this function since execbuf has
272 * reserved fifo space.
274 * if (srf->snooper.crtc)
275 * vmw_ldu_crtc_cursor_update_image(dev_priv,
276 * srf->snooper.image, 64, 64,
277 * du->hotspot_x, du->hotspot_y);
282 ttm_bo_unreserve(bo);
285 void vmw_kms_cursor_post_execbuf(struct vmw_private *dev_priv)
287 struct drm_device *dev = dev_priv->dev;
288 struct vmw_display_unit *du;
289 struct drm_crtc *crtc;
291 mutex_lock(&dev->mode_config.mutex);
293 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
294 du = vmw_crtc_to_du(crtc);
295 if (!du->cursor_surface ||
296 du->cursor_age == du->cursor_surface->snooper.age)
299 du->cursor_age = du->cursor_surface->snooper.age;
300 vmw_cursor_update_image(dev_priv,
301 du->cursor_surface->snooper.image,
302 64, 64, du->hotspot_x, du->hotspot_y);
305 mutex_unlock(&dev->mode_config.mutex);
309 * Generic framebuffer code
312 int vmw_framebuffer_create_handle(struct drm_framebuffer *fb,
313 struct drm_file *file_priv,
314 unsigned int *handle)
323 * Surface framebuffer code
326 #define vmw_framebuffer_to_vfbs(x) \
327 container_of(x, struct vmw_framebuffer_surface, base.base)
329 struct vmw_framebuffer_surface {
330 struct vmw_framebuffer base;
331 struct vmw_surface *surface;
332 struct vmw_dma_buffer *buffer;
333 struct list_head head;
334 struct drm_master *master;
337 void vmw_framebuffer_surface_destroy(struct drm_framebuffer *framebuffer)
339 struct vmw_framebuffer_surface *vfbs =
340 vmw_framebuffer_to_vfbs(framebuffer);
341 struct vmw_master *vmaster = vmw_master(vfbs->master);
344 mutex_lock(&vmaster->fb_surf_mutex);
345 list_del(&vfbs->head);
346 mutex_unlock(&vmaster->fb_surf_mutex);
348 drm_master_put(&vfbs->master);
349 drm_framebuffer_cleanup(framebuffer);
350 vmw_surface_unreference(&vfbs->surface);
351 ttm_base_object_unref(&vfbs->base.user_obj);
356 static int do_surface_dirty_sou(struct vmw_private *dev_priv,
357 struct drm_file *file_priv,
358 struct vmw_framebuffer *framebuffer,
359 unsigned flags, unsigned color,
360 struct drm_clip_rect *clips,
361 unsigned num_clips, int inc)
363 struct drm_clip_rect *clips_ptr;
364 struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
365 struct drm_crtc *crtc;
368 int ret = 0; /* silence warning */
369 int left, right, top, bottom;
372 SVGA3dCmdHeader header;
373 SVGA3dCmdBlitSurfaceToScreen body;
375 SVGASignedRect *blits;
379 list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list,
381 if (crtc->fb != &framebuffer->base)
383 units[num_units++] = vmw_crtc_to_du(crtc);
386 BUG_ON(!clips || !num_clips);
388 fifo_size = sizeof(*cmd) + sizeof(SVGASignedRect) * num_clips;
389 cmd = kzalloc(fifo_size, GFP_KERNEL);
390 if (unlikely(cmd == NULL)) {
391 DRM_ERROR("Temporary fifo memory alloc failed.\n");
401 for (i = 1; i < num_clips; i++, clips_ptr += inc) {
402 left = min_t(int, left, (int)clips_ptr->x1);
403 right = max_t(int, right, (int)clips_ptr->x2);
404 top = min_t(int, top, (int)clips_ptr->y1);
405 bottom = max_t(int, bottom, (int)clips_ptr->y2);
408 /* only need to do this once */
409 memset(cmd, 0, fifo_size);
410 cmd->header.id = cpu_to_le32(SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN);
411 cmd->header.size = cpu_to_le32(fifo_size - sizeof(cmd->header));
413 cmd->body.srcRect.left = left;
414 cmd->body.srcRect.right = right;
415 cmd->body.srcRect.top = top;
416 cmd->body.srcRect.bottom = bottom;
419 blits = (SVGASignedRect *)&cmd[1];
420 for (i = 0; i < num_clips; i++, clips_ptr += inc) {
421 blits[i].left = clips_ptr->x1 - left;
422 blits[i].right = clips_ptr->x2 - left;
423 blits[i].top = clips_ptr->y1 - top;
424 blits[i].bottom = clips_ptr->y2 - top;
427 /* do per unit writing, reuse fifo for each */
428 for (i = 0; i < num_units; i++) {
429 struct vmw_display_unit *unit = units[i];
430 int clip_x1 = left - unit->crtc.x;
431 int clip_y1 = top - unit->crtc.y;
432 int clip_x2 = right - unit->crtc.x;
433 int clip_y2 = bottom - unit->crtc.y;
435 /* skip any crtcs that misses the clip region */
436 if (clip_x1 >= unit->crtc.mode.hdisplay ||
437 clip_y1 >= unit->crtc.mode.vdisplay ||
438 clip_x2 <= 0 || clip_y2 <= 0)
441 /* need to reset sid as it is changed by execbuf */
442 cmd->body.srcImage.sid = cpu_to_le32(framebuffer->user_handle);
444 cmd->body.destScreenId = unit->unit;
447 * The blit command is a lot more resilient then the
448 * readback command when it comes to clip rects. So its
449 * okay to go out of bounds.
452 cmd->body.destRect.left = clip_x1;
453 cmd->body.destRect.right = clip_x2;
454 cmd->body.destRect.top = clip_y1;
455 cmd->body.destRect.bottom = clip_y2;
458 ret = vmw_execbuf_process(file_priv, dev_priv, NULL, cmd,
461 if (unlikely(ret != 0))
470 int vmw_framebuffer_surface_dirty(struct drm_framebuffer *framebuffer,
471 struct drm_file *file_priv,
472 unsigned flags, unsigned color,
473 struct drm_clip_rect *clips,
476 struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
477 struct vmw_master *vmaster = vmw_master(file_priv->master);
478 struct vmw_framebuffer_surface *vfbs =
479 vmw_framebuffer_to_vfbs(framebuffer);
480 struct drm_clip_rect norect;
483 if (unlikely(vfbs->master != file_priv->master))
486 /* Require ScreenObject support for 3D */
487 if (!dev_priv->sou_priv)
490 ret = ttm_read_lock(&vmaster->lock, true);
491 if (unlikely(ret != 0))
497 norect.x1 = norect.y1 = 0;
498 norect.x2 = framebuffer->width;
499 norect.y2 = framebuffer->height;
500 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
502 inc = 2; /* skip source rects */
505 ret = do_surface_dirty_sou(dev_priv, file_priv, &vfbs->base,
507 clips, num_clips, inc);
509 ttm_read_unlock(&vmaster->lock);
513 static struct drm_framebuffer_funcs vmw_framebuffer_surface_funcs = {
514 .destroy = vmw_framebuffer_surface_destroy,
515 .dirty = vmw_framebuffer_surface_dirty,
516 .create_handle = vmw_framebuffer_create_handle,
519 static int vmw_kms_new_framebuffer_surface(struct vmw_private *dev_priv,
520 struct drm_file *file_priv,
521 struct vmw_surface *surface,
522 struct vmw_framebuffer **out,
523 const struct drm_mode_fb_cmd
527 struct drm_device *dev = dev_priv->dev;
528 struct vmw_framebuffer_surface *vfbs;
529 enum SVGA3dSurfaceFormat format;
530 struct vmw_master *vmaster = vmw_master(file_priv->master);
533 /* 3D is only supported on HWv8 hosts which supports screen objects */
534 if (!dev_priv->sou_priv)
541 if (unlikely(surface->mip_levels[0] != 1 ||
542 surface->num_sizes != 1 ||
543 surface->sizes[0].width < mode_cmd->width ||
544 surface->sizes[0].height < mode_cmd->height ||
545 surface->sizes[0].depth != 1)) {
546 DRM_ERROR("Incompatible surface dimensions "
547 "for requested mode.\n");
551 switch (mode_cmd->depth) {
553 format = SVGA3D_A8R8G8B8;
556 format = SVGA3D_X8R8G8B8;
559 format = SVGA3D_R5G6B5;
562 format = SVGA3D_A1R5G5B5;
565 format = SVGA3D_LUMINANCE8;
568 DRM_ERROR("Invalid color depth: %d\n", mode_cmd->depth);
572 if (unlikely(format != surface->format)) {
573 DRM_ERROR("Invalid surface format for requested mode.\n");
577 vfbs = kzalloc(sizeof(*vfbs), GFP_KERNEL);
583 ret = drm_framebuffer_init(dev, &vfbs->base.base,
584 &vmw_framebuffer_surface_funcs);
588 if (!vmw_surface_reference(surface)) {
589 DRM_ERROR("failed to reference surface %p\n", surface);
593 /* XXX get the first 3 from the surface info */
594 vfbs->base.base.bits_per_pixel = mode_cmd->bpp;
595 vfbs->base.base.pitch = mode_cmd->pitch;
596 vfbs->base.base.depth = mode_cmd->depth;
597 vfbs->base.base.width = mode_cmd->width;
598 vfbs->base.base.height = mode_cmd->height;
599 vfbs->surface = surface;
600 vfbs->base.user_handle = mode_cmd->handle;
601 vfbs->master = drm_master_get(file_priv->master);
603 mutex_lock(&vmaster->fb_surf_mutex);
604 list_add_tail(&vfbs->head, &vmaster->fb_surf);
605 mutex_unlock(&vmaster->fb_surf_mutex);
612 drm_framebuffer_cleanup(&vfbs->base.base);
620 * Dmabuf framebuffer code
623 #define vmw_framebuffer_to_vfbd(x) \
624 container_of(x, struct vmw_framebuffer_dmabuf, base.base)
626 struct vmw_framebuffer_dmabuf {
627 struct vmw_framebuffer base;
628 struct vmw_dma_buffer *buffer;
631 void vmw_framebuffer_dmabuf_destroy(struct drm_framebuffer *framebuffer)
633 struct vmw_framebuffer_dmabuf *vfbd =
634 vmw_framebuffer_to_vfbd(framebuffer);
636 drm_framebuffer_cleanup(framebuffer);
637 vmw_dmabuf_unreference(&vfbd->buffer);
638 ttm_base_object_unref(&vfbd->base.user_obj);
643 static int do_dmabuf_dirty_ldu(struct vmw_private *dev_priv,
644 struct vmw_framebuffer *framebuffer,
645 unsigned flags, unsigned color,
646 struct drm_clip_rect *clips,
647 unsigned num_clips, int increment)
654 SVGAFifoCmdUpdate body;
657 fifo_size = sizeof(*cmd) * num_clips;
658 cmd = vmw_fifo_reserve(dev_priv, fifo_size);
659 if (unlikely(cmd == NULL)) {
660 DRM_ERROR("Fifo reserve failed.\n");
664 memset(cmd, 0, fifo_size);
665 for (i = 0; i < num_clips; i++, clips += increment) {
666 cmd[i].header = cpu_to_le32(SVGA_CMD_UPDATE);
667 cmd[i].body.x = cpu_to_le32(clips->x1);
668 cmd[i].body.y = cpu_to_le32(clips->y1);
669 cmd[i].body.width = cpu_to_le32(clips->x2 - clips->x1);
670 cmd[i].body.height = cpu_to_le32(clips->y2 - clips->y1);
673 vmw_fifo_commit(dev_priv, fifo_size);
677 static int do_dmabuf_define_gmrfb(struct drm_file *file_priv,
678 struct vmw_private *dev_priv,
679 struct vmw_framebuffer *framebuffer)
681 int depth = framebuffer->base.depth;
687 SVGAFifoCmdDefineGMRFB body;
690 /* Emulate RGBA support, contrary to svga_reg.h this is not
691 * supported by hosts. This is only a problem if we are reading
692 * this value later and expecting what we uploaded back.
697 fifo_size = sizeof(*cmd);
698 cmd = kmalloc(fifo_size, GFP_KERNEL);
699 if (unlikely(cmd == NULL)) {
700 DRM_ERROR("Failed to allocate temporary cmd buffer.\n");
704 memset(cmd, 0, fifo_size);
705 cmd->header = SVGA_CMD_DEFINE_GMRFB;
706 cmd->body.format.bitsPerPixel = framebuffer->base.bits_per_pixel;
707 cmd->body.format.colorDepth = depth;
708 cmd->body.format.reserved = 0;
709 cmd->body.bytesPerLine = framebuffer->base.pitch;
710 cmd->body.ptr.gmrId = framebuffer->user_handle;
711 cmd->body.ptr.offset = 0;
713 ret = vmw_execbuf_process(file_priv, dev_priv, NULL, cmd,
721 static int do_dmabuf_dirty_sou(struct drm_file *file_priv,
722 struct vmw_private *dev_priv,
723 struct vmw_framebuffer *framebuffer,
724 unsigned flags, unsigned color,
725 struct drm_clip_rect *clips,
726 unsigned num_clips, int increment)
728 struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
729 struct drm_clip_rect *clips_ptr;
730 int i, k, num_units, ret;
731 struct drm_crtc *crtc;
736 SVGAFifoCmdBlitGMRFBToScreen body;
739 ret = do_dmabuf_define_gmrfb(file_priv, dev_priv, framebuffer);
740 if (unlikely(ret != 0))
741 return ret; /* define_gmrfb prints warnings */
743 fifo_size = sizeof(*blits) * num_clips;
744 blits = kmalloc(fifo_size, GFP_KERNEL);
745 if (unlikely(blits == NULL)) {
746 DRM_ERROR("Failed to allocate temporary cmd buffer.\n");
751 list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list, head) {
752 if (crtc->fb != &framebuffer->base)
754 units[num_units++] = vmw_crtc_to_du(crtc);
757 for (k = 0; k < num_units; k++) {
758 struct vmw_display_unit *unit = units[k];
762 for (i = 0; i < num_clips; i++, clips_ptr += increment) {
763 int clip_x1 = clips_ptr->x1 - unit->crtc.x;
764 int clip_y1 = clips_ptr->y1 - unit->crtc.y;
765 int clip_x2 = clips_ptr->x2 - unit->crtc.x;
766 int clip_y2 = clips_ptr->y2 - unit->crtc.y;
768 /* skip any crtcs that misses the clip region */
769 if (clip_x1 >= unit->crtc.mode.hdisplay ||
770 clip_y1 >= unit->crtc.mode.vdisplay ||
771 clip_x2 <= 0 || clip_y2 <= 0)
774 blits[hit_num].header = SVGA_CMD_BLIT_GMRFB_TO_SCREEN;
775 blits[hit_num].body.destScreenId = unit->unit;
776 blits[hit_num].body.srcOrigin.x = clips_ptr->x1;
777 blits[hit_num].body.srcOrigin.y = clips_ptr->y1;
778 blits[hit_num].body.destRect.left = clip_x1;
779 blits[hit_num].body.destRect.top = clip_y1;
780 blits[hit_num].body.destRect.right = clip_x2;
781 blits[hit_num].body.destRect.bottom = clip_y2;
785 /* no clips hit the crtc */
789 fifo_size = sizeof(*blits) * hit_num;
790 ret = vmw_execbuf_process(file_priv, dev_priv, NULL, blits,
793 if (unlikely(ret != 0))
802 int vmw_framebuffer_dmabuf_dirty(struct drm_framebuffer *framebuffer,
803 struct drm_file *file_priv,
804 unsigned flags, unsigned color,
805 struct drm_clip_rect *clips,
808 struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
809 struct vmw_master *vmaster = vmw_master(file_priv->master);
810 struct vmw_framebuffer_dmabuf *vfbd =
811 vmw_framebuffer_to_vfbd(framebuffer);
812 struct drm_clip_rect norect;
813 int ret, increment = 1;
815 ret = ttm_read_lock(&vmaster->lock, true);
816 if (unlikely(ret != 0))
822 norect.x1 = norect.y1 = 0;
823 norect.x2 = framebuffer->width;
824 norect.y2 = framebuffer->height;
825 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
830 if (dev_priv->ldu_priv) {
831 ret = do_dmabuf_dirty_ldu(dev_priv, &vfbd->base,
833 clips, num_clips, increment);
835 ret = do_dmabuf_dirty_sou(file_priv, dev_priv, &vfbd->base,
837 clips, num_clips, increment);
840 ttm_read_unlock(&vmaster->lock);
844 static struct drm_framebuffer_funcs vmw_framebuffer_dmabuf_funcs = {
845 .destroy = vmw_framebuffer_dmabuf_destroy,
846 .dirty = vmw_framebuffer_dmabuf_dirty,
847 .create_handle = vmw_framebuffer_create_handle,
851 * Pin the dmabuffer to the start of vram.
853 static int vmw_framebuffer_dmabuf_pin(struct vmw_framebuffer *vfb)
855 struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
856 struct vmw_framebuffer_dmabuf *vfbd =
857 vmw_framebuffer_to_vfbd(&vfb->base);
860 /* This code should not be used with screen objects */
861 BUG_ON(dev_priv->sou_priv);
863 vmw_overlay_pause_all(dev_priv);
865 ret = vmw_dmabuf_to_start_of_vram(dev_priv, vfbd->buffer, true, false);
867 vmw_overlay_resume_all(dev_priv);
874 static int vmw_framebuffer_dmabuf_unpin(struct vmw_framebuffer *vfb)
876 struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
877 struct vmw_framebuffer_dmabuf *vfbd =
878 vmw_framebuffer_to_vfbd(&vfb->base);
881 WARN_ON(!vfbd->buffer);
885 return vmw_dmabuf_unpin(dev_priv, vfbd->buffer, false);
888 static int vmw_kms_new_framebuffer_dmabuf(struct vmw_private *dev_priv,
889 struct vmw_dma_buffer *dmabuf,
890 struct vmw_framebuffer **out,
891 const struct drm_mode_fb_cmd
895 struct drm_device *dev = dev_priv->dev;
896 struct vmw_framebuffer_dmabuf *vfbd;
897 unsigned int requested_size;
900 requested_size = mode_cmd->height * mode_cmd->pitch;
901 if (unlikely(requested_size > dmabuf->base.num_pages * PAGE_SIZE)) {
902 DRM_ERROR("Screen buffer object size is too small "
903 "for requested mode.\n");
907 /* Limited framebuffer color depth support for screen objects */
908 if (dev_priv->sou_priv) {
909 switch (mode_cmd->depth) {
912 /* Only support 32 bpp for 32 and 24 depth fbs */
913 if (mode_cmd->bpp == 32)
916 DRM_ERROR("Invalid color depth/bbp: %d %d\n",
917 mode_cmd->depth, mode_cmd->bpp);
921 /* Only support 16 bpp for 16 and 15 depth fbs */
922 if (mode_cmd->bpp == 16)
925 DRM_ERROR("Invalid color depth/bbp: %d %d\n",
926 mode_cmd->depth, mode_cmd->bpp);
929 DRM_ERROR("Invalid color depth: %d\n", mode_cmd->depth);
934 vfbd = kzalloc(sizeof(*vfbd), GFP_KERNEL);
940 ret = drm_framebuffer_init(dev, &vfbd->base.base,
941 &vmw_framebuffer_dmabuf_funcs);
945 if (!vmw_dmabuf_reference(dmabuf)) {
946 DRM_ERROR("failed to reference dmabuf %p\n", dmabuf);
950 vfbd->base.base.bits_per_pixel = mode_cmd->bpp;
951 vfbd->base.base.pitch = mode_cmd->pitch;
952 vfbd->base.base.depth = mode_cmd->depth;
953 vfbd->base.base.width = mode_cmd->width;
954 vfbd->base.base.height = mode_cmd->height;
955 if (!dev_priv->sou_priv) {
956 vfbd->base.pin = vmw_framebuffer_dmabuf_pin;
957 vfbd->base.unpin = vmw_framebuffer_dmabuf_unpin;
959 vfbd->base.dmabuf = true;
960 vfbd->buffer = dmabuf;
961 vfbd->base.user_handle = mode_cmd->handle;
967 drm_framebuffer_cleanup(&vfbd->base.base);
975 * Generic Kernel modesetting functions
978 static struct drm_framebuffer *vmw_kms_fb_create(struct drm_device *dev,
979 struct drm_file *file_priv,
980 struct drm_mode_fb_cmd *mode_cmd)
982 struct vmw_private *dev_priv = vmw_priv(dev);
983 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
984 struct vmw_framebuffer *vfb = NULL;
985 struct vmw_surface *surface = NULL;
986 struct vmw_dma_buffer *bo = NULL;
987 struct ttm_base_object *user_obj;
992 * This code should be conditioned on Screen Objects not being used.
993 * If screen objects are used, we can allocate a GMR to hold the
994 * requested framebuffer.
997 required_size = mode_cmd->pitch * mode_cmd->height;
998 if (unlikely(required_size > (u64) dev_priv->vram_size)) {
999 DRM_ERROR("VRAM size is too small for requested mode.\n");
1000 return ERR_PTR(-ENOMEM);
1004 * Take a reference on the user object of the resource
1005 * backing the kms fb. This ensures that user-space handle
1006 * lookups on that resource will always work as long as
1007 * it's registered with a kms framebuffer. This is important,
1008 * since vmw_execbuf_process identifies resources in the
1009 * command stream using user-space handles.
1012 user_obj = ttm_base_object_lookup(tfile, mode_cmd->handle);
1013 if (unlikely(user_obj == NULL)) {
1014 DRM_ERROR("Could not locate requested kms frame buffer.\n");
1015 return ERR_PTR(-ENOENT);
1019 * End conditioned code.
1022 ret = vmw_user_surface_lookup_handle(dev_priv, tfile,
1023 mode_cmd->handle, &surface);
1027 if (!surface->scanout)
1028 goto err_not_scanout;
1030 ret = vmw_kms_new_framebuffer_surface(dev_priv, file_priv, surface,
1033 /* vmw_user_surface_lookup takes one ref so does new_fb */
1034 vmw_surface_unreference(&surface);
1037 DRM_ERROR("failed to create vmw_framebuffer: %i\n", ret);
1038 ttm_base_object_unref(&user_obj);
1039 return ERR_PTR(ret);
1041 vfb->user_obj = user_obj;
1045 DRM_INFO("%s: trying buffer\n", __func__);
1047 ret = vmw_user_dmabuf_lookup(tfile, mode_cmd->handle, &bo);
1049 DRM_ERROR("failed to find buffer: %i\n", ret);
1050 return ERR_PTR(-ENOENT);
1053 ret = vmw_kms_new_framebuffer_dmabuf(dev_priv, bo, &vfb,
1056 /* vmw_user_dmabuf_lookup takes one ref so does new_fb */
1057 vmw_dmabuf_unreference(&bo);
1060 DRM_ERROR("failed to create vmw_framebuffer: %i\n", ret);
1061 ttm_base_object_unref(&user_obj);
1062 return ERR_PTR(ret);
1064 vfb->user_obj = user_obj;
1069 DRM_ERROR("surface not marked as scanout\n");
1070 /* vmw_user_surface_lookup takes one ref */
1071 vmw_surface_unreference(&surface);
1072 ttm_base_object_unref(&user_obj);
1074 return ERR_PTR(-EINVAL);
1077 static struct drm_mode_config_funcs vmw_kms_funcs = {
1078 .fb_create = vmw_kms_fb_create,
1081 int vmw_kms_present(struct vmw_private *dev_priv,
1082 struct drm_file *file_priv,
1083 struct vmw_framebuffer *vfb,
1084 struct vmw_surface *surface,
1086 int32_t destX, int32_t destY,
1087 struct drm_vmw_rect *clips,
1090 struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
1091 struct drm_crtc *crtc;
1093 int i, k, num_units;
1094 int ret = 0; /* silence warning */
1097 SVGA3dCmdHeader header;
1098 SVGA3dCmdBlitSurfaceToScreen body;
1100 SVGASignedRect *blits;
1103 list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list, head) {
1104 if (crtc->fb != &vfb->base)
1106 units[num_units++] = vmw_crtc_to_du(crtc);
1109 BUG_ON(surface == NULL);
1110 BUG_ON(!clips || !num_clips);
1112 fifo_size = sizeof(*cmd) + sizeof(SVGASignedRect) * num_clips;
1113 cmd = kmalloc(fifo_size, GFP_KERNEL);
1114 if (unlikely(cmd == NULL)) {
1115 DRM_ERROR("Failed to allocate temporary fifo memory.\n");
1119 /* only need to do this once */
1120 memset(cmd, 0, fifo_size);
1121 cmd->header.id = cpu_to_le32(SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN);
1122 cmd->header.size = cpu_to_le32(fifo_size - sizeof(cmd->header));
1124 cmd->body.srcRect.left = 0;
1125 cmd->body.srcRect.right = surface->sizes[0].width;
1126 cmd->body.srcRect.top = 0;
1127 cmd->body.srcRect.bottom = surface->sizes[0].height;
1129 blits = (SVGASignedRect *)&cmd[1];
1130 for (i = 0; i < num_clips; i++) {
1131 blits[i].left = clips[i].x;
1132 blits[i].right = clips[i].x + clips[i].w;
1133 blits[i].top = clips[i].y;
1134 blits[i].bottom = clips[i].y + clips[i].h;
1137 for (k = 0; k < num_units; k++) {
1138 struct vmw_display_unit *unit = units[k];
1139 int clip_x1 = destX - unit->crtc.x;
1140 int clip_y1 = destY - unit->crtc.y;
1141 int clip_x2 = clip_x1 + surface->sizes[0].width;
1142 int clip_y2 = clip_y1 + surface->sizes[0].height;
1144 /* skip any crtcs that misses the clip region */
1145 if (clip_x1 >= unit->crtc.mode.hdisplay ||
1146 clip_y1 >= unit->crtc.mode.vdisplay ||
1147 clip_x2 <= 0 || clip_y2 <= 0)
1150 /* need to reset sid as it is changed by execbuf */
1151 cmd->body.srcImage.sid = sid;
1153 cmd->body.destScreenId = unit->unit;
1156 * The blit command is a lot more resilient then the
1157 * readback command when it comes to clip rects. So its
1158 * okay to go out of bounds.
1161 cmd->body.destRect.left = clip_x1;
1162 cmd->body.destRect.right = clip_x2;
1163 cmd->body.destRect.top = clip_y1;
1164 cmd->body.destRect.bottom = clip_y2;
1166 ret = vmw_execbuf_process(file_priv, dev_priv, NULL, cmd,
1167 fifo_size, 0, NULL);
1169 if (unlikely(ret != 0))
1178 int vmw_kms_readback(struct vmw_private *dev_priv,
1179 struct drm_file *file_priv,
1180 struct vmw_framebuffer *vfb,
1181 struct drm_vmw_fence_rep __user *user_fence_rep,
1182 struct drm_vmw_rect *clips,
1185 struct vmw_framebuffer_dmabuf *vfbd =
1186 vmw_framebuffer_to_vfbd(&vfb->base);
1187 struct vmw_dma_buffer *dmabuf = vfbd->buffer;
1188 struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
1189 struct drm_crtc *crtc;
1191 int i, k, ret, num_units, blits_pos;
1195 SVGAFifoCmdDefineGMRFB body;
1199 SVGAFifoCmdBlitScreenToGMRFB body;
1203 list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list, head) {
1204 if (crtc->fb != &vfb->base)
1206 units[num_units++] = vmw_crtc_to_du(crtc);
1209 BUG_ON(dmabuf == NULL);
1210 BUG_ON(!clips || !num_clips);
1212 /* take a safe guess at fifo size */
1213 fifo_size = sizeof(*cmd) + sizeof(*blits) * num_clips * num_units;
1214 cmd = kmalloc(fifo_size, GFP_KERNEL);
1215 if (unlikely(cmd == NULL)) {
1216 DRM_ERROR("Failed to allocate temporary fifo memory.\n");
1220 memset(cmd, 0, fifo_size);
1221 cmd->header = SVGA_CMD_DEFINE_GMRFB;
1222 cmd->body.format.bitsPerPixel = vfb->base.bits_per_pixel;
1223 cmd->body.format.colorDepth = vfb->base.depth;
1224 cmd->body.format.reserved = 0;
1225 cmd->body.bytesPerLine = vfb->base.pitch;
1226 cmd->body.ptr.gmrId = vfb->user_handle;
1227 cmd->body.ptr.offset = 0;
1229 blits = (void *)&cmd[1];
1231 for (i = 0; i < num_units; i++) {
1232 struct drm_vmw_rect *c = clips;
1233 for (k = 0; k < num_clips; k++, c++) {
1234 /* transform clip coords to crtc origin based coords */
1235 int clip_x1 = c->x - units[i]->crtc.x;
1236 int clip_x2 = c->x - units[i]->crtc.x + c->w;
1237 int clip_y1 = c->y - units[i]->crtc.y;
1238 int clip_y2 = c->y - units[i]->crtc.y + c->h;
1242 /* compensate for clipping, we negate
1243 * a negative number and add that.
1251 clip_x1 = max(clip_x1, 0);
1252 clip_y1 = max(clip_y1, 0);
1253 clip_x2 = min(clip_x2, units[i]->crtc.mode.hdisplay);
1254 clip_y2 = min(clip_y2, units[i]->crtc.mode.vdisplay);
1256 /* and cull any rects that misses the crtc */
1257 if (clip_x1 >= units[i]->crtc.mode.hdisplay ||
1258 clip_y1 >= units[i]->crtc.mode.vdisplay ||
1259 clip_x2 <= 0 || clip_y2 <= 0)
1262 blits[blits_pos].header = SVGA_CMD_BLIT_SCREEN_TO_GMRFB;
1263 blits[blits_pos].body.srcScreenId = units[i]->unit;
1264 blits[blits_pos].body.destOrigin.x = dest_x;
1265 blits[blits_pos].body.destOrigin.y = dest_y;
1267 blits[blits_pos].body.srcRect.left = clip_x1;
1268 blits[blits_pos].body.srcRect.top = clip_y1;
1269 blits[blits_pos].body.srcRect.right = clip_x2;
1270 blits[blits_pos].body.srcRect.bottom = clip_y2;
1274 /* reset size here and use calculated exact size from loops */
1275 fifo_size = sizeof(*cmd) + sizeof(*blits) * blits_pos;
1277 ret = vmw_execbuf_process(file_priv, dev_priv, NULL, cmd, fifo_size,
1285 int vmw_kms_init(struct vmw_private *dev_priv)
1287 struct drm_device *dev = dev_priv->dev;
1290 drm_mode_config_init(dev);
1291 dev->mode_config.funcs = &vmw_kms_funcs;
1292 dev->mode_config.min_width = 1;
1293 dev->mode_config.min_height = 1;
1294 /* assumed largest fb size */
1295 dev->mode_config.max_width = 8192;
1296 dev->mode_config.max_height = 8192;
1298 ret = vmw_kms_init_screen_object_display(dev_priv);
1299 if (ret) /* Fallback */
1300 (void)vmw_kms_init_legacy_display_system(dev_priv);
1305 int vmw_kms_close(struct vmw_private *dev_priv)
1308 * Docs says we should take the lock before calling this function
1309 * but since it destroys encoders and our destructor calls
1310 * drm_encoder_cleanup which takes the lock we deadlock.
1312 drm_mode_config_cleanup(dev_priv->dev);
1313 vmw_kms_close_legacy_display_system(dev_priv);
1317 int vmw_kms_cursor_bypass_ioctl(struct drm_device *dev, void *data,
1318 struct drm_file *file_priv)
1320 struct drm_vmw_cursor_bypass_arg *arg = data;
1321 struct vmw_display_unit *du;
1322 struct drm_mode_object *obj;
1323 struct drm_crtc *crtc;
1327 mutex_lock(&dev->mode_config.mutex);
1328 if (arg->flags & DRM_VMW_CURSOR_BYPASS_ALL) {
1330 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1331 du = vmw_crtc_to_du(crtc);
1332 du->hotspot_x = arg->xhot;
1333 du->hotspot_y = arg->yhot;
1336 mutex_unlock(&dev->mode_config.mutex);
1340 obj = drm_mode_object_find(dev, arg->crtc_id, DRM_MODE_OBJECT_CRTC);
1346 crtc = obj_to_crtc(obj);
1347 du = vmw_crtc_to_du(crtc);
1349 du->hotspot_x = arg->xhot;
1350 du->hotspot_y = arg->yhot;
1353 mutex_unlock(&dev->mode_config.mutex);
1358 int vmw_kms_write_svga(struct vmw_private *vmw_priv,
1359 unsigned width, unsigned height, unsigned pitch,
1360 unsigned bpp, unsigned depth)
1362 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1363 vmw_write(vmw_priv, SVGA_REG_PITCHLOCK, pitch);
1364 else if (vmw_fifo_have_pitchlock(vmw_priv))
1365 iowrite32(pitch, vmw_priv->mmio_virt + SVGA_FIFO_PITCHLOCK);
1366 vmw_write(vmw_priv, SVGA_REG_WIDTH, width);
1367 vmw_write(vmw_priv, SVGA_REG_HEIGHT, height);
1368 vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, bpp);
1370 if (vmw_read(vmw_priv, SVGA_REG_DEPTH) != depth) {
1371 DRM_ERROR("Invalid depth %u for %u bpp, host expects %u\n",
1372 depth, bpp, vmw_read(vmw_priv, SVGA_REG_DEPTH));
1379 int vmw_kms_save_vga(struct vmw_private *vmw_priv)
1381 struct vmw_vga_topology_state *save;
1384 vmw_priv->vga_width = vmw_read(vmw_priv, SVGA_REG_WIDTH);
1385 vmw_priv->vga_height = vmw_read(vmw_priv, SVGA_REG_HEIGHT);
1386 vmw_priv->vga_bpp = vmw_read(vmw_priv, SVGA_REG_BITS_PER_PIXEL);
1387 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1388 vmw_priv->vga_pitchlock =
1389 vmw_read(vmw_priv, SVGA_REG_PITCHLOCK);
1390 else if (vmw_fifo_have_pitchlock(vmw_priv))
1391 vmw_priv->vga_pitchlock = ioread32(vmw_priv->mmio_virt +
1392 SVGA_FIFO_PITCHLOCK);
1394 if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
1397 vmw_priv->num_displays = vmw_read(vmw_priv,
1398 SVGA_REG_NUM_GUEST_DISPLAYS);
1400 if (vmw_priv->num_displays == 0)
1401 vmw_priv->num_displays = 1;
1403 for (i = 0; i < vmw_priv->num_displays; ++i) {
1404 save = &vmw_priv->vga_save[i];
1405 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
1406 save->primary = vmw_read(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY);
1407 save->pos_x = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_X);
1408 save->pos_y = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y);
1409 save->width = vmw_read(vmw_priv, SVGA_REG_DISPLAY_WIDTH);
1410 save->height = vmw_read(vmw_priv, SVGA_REG_DISPLAY_HEIGHT);
1411 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
1412 if (i == 0 && vmw_priv->num_displays == 1 &&
1413 save->width == 0 && save->height == 0) {
1416 * It should be fairly safe to assume that these
1417 * values are uninitialized.
1420 save->width = vmw_priv->vga_width - save->pos_x;
1421 save->height = vmw_priv->vga_height - save->pos_y;
1428 int vmw_kms_restore_vga(struct vmw_private *vmw_priv)
1430 struct vmw_vga_topology_state *save;
1433 vmw_write(vmw_priv, SVGA_REG_WIDTH, vmw_priv->vga_width);
1434 vmw_write(vmw_priv, SVGA_REG_HEIGHT, vmw_priv->vga_height);
1435 vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, vmw_priv->vga_bpp);
1436 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1437 vmw_write(vmw_priv, SVGA_REG_PITCHLOCK,
1438 vmw_priv->vga_pitchlock);
1439 else if (vmw_fifo_have_pitchlock(vmw_priv))
1440 iowrite32(vmw_priv->vga_pitchlock,
1441 vmw_priv->mmio_virt + SVGA_FIFO_PITCHLOCK);
1443 if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
1446 for (i = 0; i < vmw_priv->num_displays; ++i) {
1447 save = &vmw_priv->vga_save[i];
1448 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
1449 vmw_write(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY, save->primary);
1450 vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_X, save->pos_x);
1451 vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y, save->pos_y);
1452 vmw_write(vmw_priv, SVGA_REG_DISPLAY_WIDTH, save->width);
1453 vmw_write(vmw_priv, SVGA_REG_DISPLAY_HEIGHT, save->height);
1454 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
1460 bool vmw_kms_validate_mode_vram(struct vmw_private *dev_priv,
1464 return ((u64) pitch * (u64) height) < (u64) dev_priv->vram_size;
1469 * Function called by DRM code called with vbl_lock held.
1471 u32 vmw_get_vblank_counter(struct drm_device *dev, int crtc)
1477 * Function called by DRM code called with vbl_lock held.
1479 int vmw_enable_vblank(struct drm_device *dev, int crtc)
1485 * Function called by DRM code called with vbl_lock held.
1487 void vmw_disable_vblank(struct drm_device *dev, int crtc)
1493 * Small shared kms functions.
1496 int vmw_du_update_layout(struct vmw_private *dev_priv, unsigned num,
1497 struct drm_vmw_rect *rects)
1499 struct drm_device *dev = dev_priv->dev;
1500 struct vmw_display_unit *du;
1501 struct drm_connector *con;
1503 mutex_lock(&dev->mode_config.mutex);
1509 DRM_INFO("%s: new layout ", __func__);
1510 for (i = 0; i < num; i++)
1511 DRM_INFO("(%i, %i %ux%u) ", rects[i].x, rects[i].y,
1512 rects[i].w, rects[i].h);
1517 list_for_each_entry(con, &dev->mode_config.connector_list, head) {
1518 du = vmw_connector_to_du(con);
1519 if (num > du->unit) {
1520 du->pref_width = rects[du->unit].w;
1521 du->pref_height = rects[du->unit].h;
1522 du->pref_active = true;
1523 du->gui_x = rects[du->unit].x;
1524 du->gui_y = rects[du->unit].y;
1526 du->pref_width = 800;
1527 du->pref_height = 600;
1528 du->pref_active = false;
1530 con->status = vmw_du_connector_detect(con, true);
1533 mutex_unlock(&dev->mode_config.mutex);
1538 void vmw_du_crtc_save(struct drm_crtc *crtc)
1542 void vmw_du_crtc_restore(struct drm_crtc *crtc)
1546 void vmw_du_crtc_gamma_set(struct drm_crtc *crtc,
1547 u16 *r, u16 *g, u16 *b,
1548 uint32_t start, uint32_t size)
1550 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
1553 for (i = 0; i < size; i++) {
1554 DRM_DEBUG("%d r/g/b = 0x%04x / 0x%04x / 0x%04x\n", i,
1556 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 0, r[i] >> 8);
1557 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 1, g[i] >> 8);
1558 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 2, b[i] >> 8);
1562 void vmw_du_connector_dpms(struct drm_connector *connector, int mode)
1566 void vmw_du_connector_save(struct drm_connector *connector)
1570 void vmw_du_connector_restore(struct drm_connector *connector)
1574 enum drm_connector_status
1575 vmw_du_connector_detect(struct drm_connector *connector, bool force)
1577 uint32_t num_displays;
1578 struct drm_device *dev = connector->dev;
1579 struct vmw_private *dev_priv = vmw_priv(dev);
1580 struct vmw_display_unit *du = vmw_connector_to_du(connector);
1582 mutex_lock(&dev_priv->hw_mutex);
1583 num_displays = vmw_read(dev_priv, SVGA_REG_NUM_DISPLAYS);
1584 mutex_unlock(&dev_priv->hw_mutex);
1586 return ((vmw_connector_to_du(connector)->unit < num_displays &&
1588 connector_status_connected : connector_status_disconnected);
1591 static struct drm_display_mode vmw_kms_connector_builtin[] = {
1593 { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 25175, 640, 656,
1594 752, 800, 0, 480, 489, 492, 525, 0,
1595 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
1597 { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 40000, 800, 840,
1598 968, 1056, 0, 600, 601, 605, 628, 0,
1599 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1601 { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 65000, 1024, 1048,
1602 1184, 1344, 0, 768, 771, 777, 806, 0,
1603 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
1605 { DRM_MODE("1152x864", DRM_MODE_TYPE_DRIVER, 108000, 1152, 1216,
1606 1344, 1600, 0, 864, 865, 868, 900, 0,
1607 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1609 { DRM_MODE("1280x768", DRM_MODE_TYPE_DRIVER, 79500, 1280, 1344,
1610 1472, 1664, 0, 768, 771, 778, 798, 0,
1611 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1613 { DRM_MODE("1280x800", DRM_MODE_TYPE_DRIVER, 83500, 1280, 1352,
1614 1480, 1680, 0, 800, 803, 809, 831, 0,
1615 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
1617 { DRM_MODE("1280x960", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1376,
1618 1488, 1800, 0, 960, 961, 964, 1000, 0,
1619 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1620 /* 1280x1024@60Hz */
1621 { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1328,
1622 1440, 1688, 0, 1024, 1025, 1028, 1066, 0,
1623 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1625 { DRM_MODE("1360x768", DRM_MODE_TYPE_DRIVER, 85500, 1360, 1424,
1626 1536, 1792, 0, 768, 771, 777, 795, 0,
1627 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1628 /* 1440x1050@60Hz */
1629 { DRM_MODE("1400x1050", DRM_MODE_TYPE_DRIVER, 121750, 1400, 1488,
1630 1632, 1864, 0, 1050, 1053, 1057, 1089, 0,
1631 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1633 { DRM_MODE("1440x900", DRM_MODE_TYPE_DRIVER, 106500, 1440, 1520,
1634 1672, 1904, 0, 900, 903, 909, 934, 0,
1635 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1636 /* 1600x1200@60Hz */
1637 { DRM_MODE("1600x1200", DRM_MODE_TYPE_DRIVER, 162000, 1600, 1664,
1638 1856, 2160, 0, 1200, 1201, 1204, 1250, 0,
1639 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1640 /* 1680x1050@60Hz */
1641 { DRM_MODE("1680x1050", DRM_MODE_TYPE_DRIVER, 146250, 1680, 1784,
1642 1960, 2240, 0, 1050, 1053, 1059, 1089, 0,
1643 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1644 /* 1792x1344@60Hz */
1645 { DRM_MODE("1792x1344", DRM_MODE_TYPE_DRIVER, 204750, 1792, 1920,
1646 2120, 2448, 0, 1344, 1345, 1348, 1394, 0,
1647 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1648 /* 1853x1392@60Hz */
1649 { DRM_MODE("1856x1392", DRM_MODE_TYPE_DRIVER, 218250, 1856, 1952,
1650 2176, 2528, 0, 1392, 1393, 1396, 1439, 0,
1651 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1652 /* 1920x1200@60Hz */
1653 { DRM_MODE("1920x1200", DRM_MODE_TYPE_DRIVER, 193250, 1920, 2056,
1654 2256, 2592, 0, 1200, 1203, 1209, 1245, 0,
1655 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1656 /* 1920x1440@60Hz */
1657 { DRM_MODE("1920x1440", DRM_MODE_TYPE_DRIVER, 234000, 1920, 2048,
1658 2256, 2600, 0, 1440, 1441, 1444, 1500, 0,
1659 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1660 /* 2560x1600@60Hz */
1661 { DRM_MODE("2560x1600", DRM_MODE_TYPE_DRIVER, 348500, 2560, 2752,
1662 3032, 3504, 0, 1600, 1603, 1609, 1658, 0,
1663 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1665 { DRM_MODE("", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) },
1669 * vmw_guess_mode_timing - Provide fake timings for a
1670 * 60Hz vrefresh mode.
1672 * @mode - Pointer to a struct drm_display_mode with hdisplay and vdisplay
1673 * members filled in.
1675 static void vmw_guess_mode_timing(struct drm_display_mode *mode)
1677 mode->hsync_start = mode->hdisplay + 50;
1678 mode->hsync_end = mode->hsync_start + 50;
1679 mode->htotal = mode->hsync_end + 50;
1681 mode->vsync_start = mode->vdisplay + 50;
1682 mode->vsync_end = mode->vsync_start + 50;
1683 mode->vtotal = mode->vsync_end + 50;
1685 mode->clock = (u32)mode->htotal * (u32)mode->vtotal / 100 * 6;
1686 mode->vrefresh = drm_mode_vrefresh(mode);
1690 int vmw_du_connector_fill_modes(struct drm_connector *connector,
1691 uint32_t max_width, uint32_t max_height)
1693 struct vmw_display_unit *du = vmw_connector_to_du(connector);
1694 struct drm_device *dev = connector->dev;
1695 struct vmw_private *dev_priv = vmw_priv(dev);
1696 struct drm_display_mode *mode = NULL;
1697 struct drm_display_mode *bmode;
1698 struct drm_display_mode prefmode = { DRM_MODE("preferred",
1699 DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED,
1700 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1701 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC)
1705 /* Add preferred mode */
1707 mode = drm_mode_duplicate(dev, &prefmode);
1710 mode->hdisplay = du->pref_width;
1711 mode->vdisplay = du->pref_height;
1712 vmw_guess_mode_timing(mode);
1714 if (vmw_kms_validate_mode_vram(dev_priv, mode->hdisplay * 2,
1716 drm_mode_probed_add(connector, mode);
1718 drm_mode_destroy(dev, mode);
1722 if (du->pref_mode) {
1723 list_del_init(&du->pref_mode->head);
1724 drm_mode_destroy(dev, du->pref_mode);
1727 /* mode might be null here, this is intended */
1728 du->pref_mode = mode;
1731 for (i = 0; vmw_kms_connector_builtin[i].type != 0; i++) {
1732 bmode = &vmw_kms_connector_builtin[i];
1733 if (bmode->hdisplay > max_width ||
1734 bmode->vdisplay > max_height)
1737 if (!vmw_kms_validate_mode_vram(dev_priv, bmode->hdisplay * 2,
1741 mode = drm_mode_duplicate(dev, bmode);
1744 mode->vrefresh = drm_mode_vrefresh(mode);
1746 drm_mode_probed_add(connector, mode);
1749 drm_mode_connector_list_update(connector);
1754 int vmw_du_connector_set_property(struct drm_connector *connector,
1755 struct drm_property *property,
1762 int vmw_kms_update_layout_ioctl(struct drm_device *dev, void *data,
1763 struct drm_file *file_priv)
1765 struct vmw_private *dev_priv = vmw_priv(dev);
1766 struct drm_vmw_update_layout_arg *arg =
1767 (struct drm_vmw_update_layout_arg *)data;
1768 struct vmw_master *vmaster = vmw_master(file_priv->master);
1769 void __user *user_rects;
1770 struct drm_vmw_rect *rects;
1771 unsigned rects_size;
1774 struct drm_mode_config *mode_config = &dev->mode_config;
1776 ret = ttm_read_lock(&vmaster->lock, true);
1777 if (unlikely(ret != 0))
1780 if (!arg->num_outputs) {
1781 struct drm_vmw_rect def_rect = {0, 0, 800, 600};
1782 vmw_du_update_layout(dev_priv, 1, &def_rect);
1786 rects_size = arg->num_outputs * sizeof(struct drm_vmw_rect);
1787 rects = kzalloc(rects_size, GFP_KERNEL);
1788 if (unlikely(!rects)) {
1793 user_rects = (void __user *)(unsigned long)arg->rects;
1794 ret = copy_from_user(rects, user_rects, rects_size);
1795 if (unlikely(ret != 0)) {
1796 DRM_ERROR("Failed to get rects.\n");
1801 for (i = 0; i < arg->num_outputs; ++i) {
1804 rects->x + rects->w > mode_config->max_width ||
1805 rects->y + rects->h > mode_config->max_height) {
1806 DRM_ERROR("Invalid GUI layout.\n");
1812 vmw_du_update_layout(dev_priv, arg->num_outputs, rects);
1817 ttm_read_unlock(&vmaster->lock);