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 int vmw_cursor_update_dmabuf(struct vmw_private *dev_priv,
86 struct vmw_dma_buffer *dmabuf,
87 u32 width, u32 height,
88 u32 hotspotX, u32 hotspotY)
90 struct ttm_bo_kmap_obj map;
91 unsigned long kmap_offset;
92 unsigned long kmap_num;
98 kmap_num = (width*height*4 + PAGE_SIZE - 1) >> PAGE_SHIFT;
100 ret = ttm_bo_reserve(&dmabuf->base, true, false, false, 0);
101 if (unlikely(ret != 0)) {
102 DRM_ERROR("reserve failed\n");
106 ret = ttm_bo_kmap(&dmabuf->base, kmap_offset, kmap_num, &map);
107 if (unlikely(ret != 0))
110 virtual = ttm_kmap_obj_virtual(&map, &dummy);
111 ret = vmw_cursor_update_image(dev_priv, virtual, width, height,
116 ttm_bo_unreserve(&dmabuf->base);
122 void vmw_cursor_update_position(struct vmw_private *dev_priv,
123 bool show, int x, int y)
125 __le32 __iomem *fifo_mem = dev_priv->mmio_virt;
128 iowrite32(show ? 1 : 0, fifo_mem + SVGA_FIFO_CURSOR_ON);
129 iowrite32(x, fifo_mem + SVGA_FIFO_CURSOR_X);
130 iowrite32(y, fifo_mem + SVGA_FIFO_CURSOR_Y);
131 count = ioread32(fifo_mem + SVGA_FIFO_CURSOR_COUNT);
132 iowrite32(++count, fifo_mem + SVGA_FIFO_CURSOR_COUNT);
135 int vmw_du_crtc_cursor_set(struct drm_crtc *crtc, struct drm_file *file_priv,
136 uint32_t handle, uint32_t width, uint32_t height)
138 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
139 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
140 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
141 struct vmw_surface *surface = NULL;
142 struct vmw_dma_buffer *dmabuf = NULL;
145 /* A lot of the code assumes this */
146 if (handle && (width != 64 || height != 64))
150 ret = vmw_user_lookup_handle(dev_priv, tfile,
151 handle, &surface, &dmabuf);
153 DRM_ERROR("failed to find surface or dmabuf: %i\n", ret);
158 /* need to do this before taking down old image */
159 if (surface && !surface->snooper.image) {
160 DRM_ERROR("surface not suitable for cursor\n");
161 vmw_surface_unreference(&surface);
165 /* takedown old cursor */
166 if (du->cursor_surface) {
167 du->cursor_surface->snooper.crtc = NULL;
168 vmw_surface_unreference(&du->cursor_surface);
170 if (du->cursor_dmabuf)
171 vmw_dmabuf_unreference(&du->cursor_dmabuf);
173 /* setup new image */
175 /* vmw_user_surface_lookup takes one reference */
176 du->cursor_surface = surface;
178 du->cursor_surface->snooper.crtc = crtc;
179 du->cursor_age = du->cursor_surface->snooper.age;
180 vmw_cursor_update_image(dev_priv, surface->snooper.image,
181 64, 64, du->hotspot_x, du->hotspot_y);
183 /* vmw_user_surface_lookup takes one reference */
184 du->cursor_dmabuf = dmabuf;
186 ret = vmw_cursor_update_dmabuf(dev_priv, dmabuf, width, height,
187 du->hotspot_x, du->hotspot_y);
189 vmw_cursor_update_position(dev_priv, false, 0, 0);
193 vmw_cursor_update_position(dev_priv, true,
194 du->cursor_x + du->hotspot_x,
195 du->cursor_y + du->hotspot_y);
200 int vmw_du_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
202 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
203 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
204 bool shown = du->cursor_surface || du->cursor_dmabuf ? true : false;
206 du->cursor_x = x + crtc->x;
207 du->cursor_y = y + crtc->y;
209 vmw_cursor_update_position(dev_priv, shown,
210 du->cursor_x + du->hotspot_x,
211 du->cursor_y + du->hotspot_y);
216 void vmw_kms_cursor_snoop(struct vmw_surface *srf,
217 struct ttm_object_file *tfile,
218 struct ttm_buffer_object *bo,
219 SVGA3dCmdHeader *header)
221 struct ttm_bo_kmap_obj map;
222 unsigned long kmap_offset;
223 unsigned long kmap_num;
229 SVGA3dCmdHeader header;
230 SVGA3dCmdSurfaceDMA dma;
234 cmd = container_of(header, struct vmw_dma_cmd, header);
236 /* No snooper installed */
237 if (!srf->snooper.image)
240 if (cmd->dma.host.face != 0 || cmd->dma.host.mipmap != 0) {
241 DRM_ERROR("face and mipmap for cursors should never != 0\n");
245 if (cmd->header.size < 64) {
246 DRM_ERROR("at least one full copy box must be given\n");
250 box = (SVGA3dCopyBox *)&cmd[1];
251 box_count = (cmd->header.size - sizeof(SVGA3dCmdSurfaceDMA)) /
252 sizeof(SVGA3dCopyBox);
254 if (cmd->dma.guest.ptr.offset % PAGE_SIZE ||
255 box->x != 0 || box->y != 0 || box->z != 0 ||
256 box->srcx != 0 || box->srcy != 0 || box->srcz != 0 ||
257 box->d != 1 || box_count != 1) {
258 /* TODO handle none page aligned offsets */
259 /* TODO handle more dst & src != 0 */
260 /* TODO handle more then one copy */
261 DRM_ERROR("Cant snoop dma request for cursor!\n");
262 DRM_ERROR("(%u, %u, %u) (%u, %u, %u) (%ux%ux%u) %u %u\n",
263 box->srcx, box->srcy, box->srcz,
264 box->x, box->y, box->z,
265 box->w, box->h, box->d, box_count,
266 cmd->dma.guest.ptr.offset);
270 kmap_offset = cmd->dma.guest.ptr.offset >> PAGE_SHIFT;
271 kmap_num = (64*64*4) >> PAGE_SHIFT;
273 ret = ttm_bo_reserve(bo, true, false, false, 0);
274 if (unlikely(ret != 0)) {
275 DRM_ERROR("reserve failed\n");
279 ret = ttm_bo_kmap(bo, kmap_offset, kmap_num, &map);
280 if (unlikely(ret != 0))
283 virtual = ttm_kmap_obj_virtual(&map, &dummy);
285 if (box->w == 64 && cmd->dma.guest.pitch == 64*4) {
286 memcpy(srf->snooper.image, virtual, 64*64*4);
288 /* Image is unsigned pointer. */
289 for (i = 0; i < box->h; i++)
290 memcpy(srf->snooper.image + i * 64,
291 virtual + i * cmd->dma.guest.pitch,
297 /* we can't call this function from this function since execbuf has
298 * reserved fifo space.
300 * if (srf->snooper.crtc)
301 * vmw_ldu_crtc_cursor_update_image(dev_priv,
302 * srf->snooper.image, 64, 64,
303 * du->hotspot_x, du->hotspot_y);
308 ttm_bo_unreserve(bo);
311 void vmw_kms_cursor_post_execbuf(struct vmw_private *dev_priv)
313 struct drm_device *dev = dev_priv->dev;
314 struct vmw_display_unit *du;
315 struct drm_crtc *crtc;
317 mutex_lock(&dev->mode_config.mutex);
319 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
320 du = vmw_crtc_to_du(crtc);
321 if (!du->cursor_surface ||
322 du->cursor_age == du->cursor_surface->snooper.age)
325 du->cursor_age = du->cursor_surface->snooper.age;
326 vmw_cursor_update_image(dev_priv,
327 du->cursor_surface->snooper.image,
328 64, 64, du->hotspot_x, du->hotspot_y);
331 mutex_unlock(&dev->mode_config.mutex);
335 * Generic framebuffer code
338 int vmw_framebuffer_create_handle(struct drm_framebuffer *fb,
339 struct drm_file *file_priv,
340 unsigned int *handle)
349 * Surface framebuffer code
352 #define vmw_framebuffer_to_vfbs(x) \
353 container_of(x, struct vmw_framebuffer_surface, base.base)
355 struct vmw_framebuffer_surface {
356 struct vmw_framebuffer base;
357 struct vmw_surface *surface;
358 struct vmw_dma_buffer *buffer;
359 struct list_head head;
360 struct drm_master *master;
363 void vmw_framebuffer_surface_destroy(struct drm_framebuffer *framebuffer)
365 struct vmw_framebuffer_surface *vfbs =
366 vmw_framebuffer_to_vfbs(framebuffer);
367 struct vmw_master *vmaster = vmw_master(vfbs->master);
370 mutex_lock(&vmaster->fb_surf_mutex);
371 list_del(&vfbs->head);
372 mutex_unlock(&vmaster->fb_surf_mutex);
374 drm_master_put(&vfbs->master);
375 drm_framebuffer_cleanup(framebuffer);
376 vmw_surface_unreference(&vfbs->surface);
377 ttm_base_object_unref(&vfbs->base.user_obj);
382 static int do_surface_dirty_sou(struct vmw_private *dev_priv,
383 struct drm_file *file_priv,
384 struct vmw_framebuffer *framebuffer,
385 unsigned flags, unsigned color,
386 struct drm_clip_rect *clips,
387 unsigned num_clips, int inc)
389 struct drm_clip_rect *clips_ptr;
390 struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
391 struct drm_crtc *crtc;
394 int ret = 0; /* silence warning */
395 int left, right, top, bottom;
398 SVGA3dCmdHeader header;
399 SVGA3dCmdBlitSurfaceToScreen body;
401 SVGASignedRect *blits;
405 list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list,
407 if (crtc->fb != &framebuffer->base)
409 units[num_units++] = vmw_crtc_to_du(crtc);
412 BUG_ON(!clips || !num_clips);
414 fifo_size = sizeof(*cmd) + sizeof(SVGASignedRect) * num_clips;
415 cmd = kzalloc(fifo_size, GFP_KERNEL);
416 if (unlikely(cmd == NULL)) {
417 DRM_ERROR("Temporary fifo memory alloc failed.\n");
426 /* skip the first clip rect */
427 for (i = 1, clips_ptr = clips + inc;
428 i < num_clips; i++, clips_ptr += inc) {
429 left = min_t(int, left, (int)clips_ptr->x1);
430 right = max_t(int, right, (int)clips_ptr->x2);
431 top = min_t(int, top, (int)clips_ptr->y1);
432 bottom = max_t(int, bottom, (int)clips_ptr->y2);
435 /* only need to do this once */
436 memset(cmd, 0, fifo_size);
437 cmd->header.id = cpu_to_le32(SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN);
438 cmd->header.size = cpu_to_le32(fifo_size - sizeof(cmd->header));
440 cmd->body.srcRect.left = left;
441 cmd->body.srcRect.right = right;
442 cmd->body.srcRect.top = top;
443 cmd->body.srcRect.bottom = bottom;
446 blits = (SVGASignedRect *)&cmd[1];
447 for (i = 0; i < num_clips; i++, clips_ptr += inc) {
448 blits[i].left = clips_ptr->x1 - left;
449 blits[i].right = clips_ptr->x2 - left;
450 blits[i].top = clips_ptr->y1 - top;
451 blits[i].bottom = clips_ptr->y2 - top;
454 /* do per unit writing, reuse fifo for each */
455 for (i = 0; i < num_units; i++) {
456 struct vmw_display_unit *unit = units[i];
457 int clip_x1 = left - unit->crtc.x;
458 int clip_y1 = top - unit->crtc.y;
459 int clip_x2 = right - unit->crtc.x;
460 int clip_y2 = bottom - unit->crtc.y;
462 /* skip any crtcs that misses the clip region */
463 if (clip_x1 >= unit->crtc.mode.hdisplay ||
464 clip_y1 >= unit->crtc.mode.vdisplay ||
465 clip_x2 <= 0 || clip_y2 <= 0)
468 /* need to reset sid as it is changed by execbuf */
469 cmd->body.srcImage.sid = cpu_to_le32(framebuffer->user_handle);
471 cmd->body.destScreenId = unit->unit;
474 * The blit command is a lot more resilient then the
475 * readback command when it comes to clip rects. So its
476 * okay to go out of bounds.
479 cmd->body.destRect.left = clip_x1;
480 cmd->body.destRect.right = clip_x2;
481 cmd->body.destRect.top = clip_y1;
482 cmd->body.destRect.bottom = clip_y2;
485 ret = vmw_execbuf_process(file_priv, dev_priv, NULL, cmd,
488 if (unlikely(ret != 0))
497 int vmw_framebuffer_surface_dirty(struct drm_framebuffer *framebuffer,
498 struct drm_file *file_priv,
499 unsigned flags, unsigned color,
500 struct drm_clip_rect *clips,
503 struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
504 struct vmw_master *vmaster = vmw_master(file_priv->master);
505 struct vmw_framebuffer_surface *vfbs =
506 vmw_framebuffer_to_vfbs(framebuffer);
507 struct drm_clip_rect norect;
510 if (unlikely(vfbs->master != file_priv->master))
513 /* Require ScreenObject support for 3D */
514 if (!dev_priv->sou_priv)
517 ret = ttm_read_lock(&vmaster->lock, true);
518 if (unlikely(ret != 0))
524 norect.x1 = norect.y1 = 0;
525 norect.x2 = framebuffer->width;
526 norect.y2 = framebuffer->height;
527 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
529 inc = 2; /* skip source rects */
532 ret = do_surface_dirty_sou(dev_priv, file_priv, &vfbs->base,
534 clips, num_clips, inc);
536 ttm_read_unlock(&vmaster->lock);
540 static struct drm_framebuffer_funcs vmw_framebuffer_surface_funcs = {
541 .destroy = vmw_framebuffer_surface_destroy,
542 .dirty = vmw_framebuffer_surface_dirty,
543 .create_handle = vmw_framebuffer_create_handle,
546 static int vmw_kms_new_framebuffer_surface(struct vmw_private *dev_priv,
547 struct drm_file *file_priv,
548 struct vmw_surface *surface,
549 struct vmw_framebuffer **out,
550 const struct drm_mode_fb_cmd
554 struct drm_device *dev = dev_priv->dev;
555 struct vmw_framebuffer_surface *vfbs;
556 enum SVGA3dSurfaceFormat format;
557 struct vmw_master *vmaster = vmw_master(file_priv->master);
560 /* 3D is only supported on HWv8 hosts which supports screen objects */
561 if (!dev_priv->sou_priv)
568 /* Surface must be marked as a scanout. */
569 if (unlikely(!surface->scanout))
572 if (unlikely(surface->mip_levels[0] != 1 ||
573 surface->num_sizes != 1 ||
574 surface->sizes[0].width < mode_cmd->width ||
575 surface->sizes[0].height < mode_cmd->height ||
576 surface->sizes[0].depth != 1)) {
577 DRM_ERROR("Incompatible surface dimensions "
578 "for requested mode.\n");
582 switch (mode_cmd->depth) {
584 format = SVGA3D_A8R8G8B8;
587 format = SVGA3D_X8R8G8B8;
590 format = SVGA3D_R5G6B5;
593 format = SVGA3D_A1R5G5B5;
596 format = SVGA3D_LUMINANCE8;
599 DRM_ERROR("Invalid color depth: %d\n", mode_cmd->depth);
603 if (unlikely(format != surface->format)) {
604 DRM_ERROR("Invalid surface format for requested mode.\n");
608 vfbs = kzalloc(sizeof(*vfbs), GFP_KERNEL);
614 ret = drm_framebuffer_init(dev, &vfbs->base.base,
615 &vmw_framebuffer_surface_funcs);
619 if (!vmw_surface_reference(surface)) {
620 DRM_ERROR("failed to reference surface %p\n", surface);
624 /* XXX get the first 3 from the surface info */
625 vfbs->base.base.bits_per_pixel = mode_cmd->bpp;
626 vfbs->base.base.pitch = mode_cmd->pitch;
627 vfbs->base.base.depth = mode_cmd->depth;
628 vfbs->base.base.width = mode_cmd->width;
629 vfbs->base.base.height = mode_cmd->height;
630 vfbs->surface = surface;
631 vfbs->base.user_handle = mode_cmd->handle;
632 vfbs->master = drm_master_get(file_priv->master);
634 mutex_lock(&vmaster->fb_surf_mutex);
635 list_add_tail(&vfbs->head, &vmaster->fb_surf);
636 mutex_unlock(&vmaster->fb_surf_mutex);
643 drm_framebuffer_cleanup(&vfbs->base.base);
651 * Dmabuf framebuffer code
654 #define vmw_framebuffer_to_vfbd(x) \
655 container_of(x, struct vmw_framebuffer_dmabuf, base.base)
657 struct vmw_framebuffer_dmabuf {
658 struct vmw_framebuffer base;
659 struct vmw_dma_buffer *buffer;
662 void vmw_framebuffer_dmabuf_destroy(struct drm_framebuffer *framebuffer)
664 struct vmw_framebuffer_dmabuf *vfbd =
665 vmw_framebuffer_to_vfbd(framebuffer);
667 drm_framebuffer_cleanup(framebuffer);
668 vmw_dmabuf_unreference(&vfbd->buffer);
669 ttm_base_object_unref(&vfbd->base.user_obj);
674 static int do_dmabuf_dirty_ldu(struct vmw_private *dev_priv,
675 struct vmw_framebuffer *framebuffer,
676 unsigned flags, unsigned color,
677 struct drm_clip_rect *clips,
678 unsigned num_clips, int increment)
685 SVGAFifoCmdUpdate body;
688 fifo_size = sizeof(*cmd) * num_clips;
689 cmd = vmw_fifo_reserve(dev_priv, fifo_size);
690 if (unlikely(cmd == NULL)) {
691 DRM_ERROR("Fifo reserve failed.\n");
695 memset(cmd, 0, fifo_size);
696 for (i = 0; i < num_clips; i++, clips += increment) {
697 cmd[i].header = cpu_to_le32(SVGA_CMD_UPDATE);
698 cmd[i].body.x = cpu_to_le32(clips->x1);
699 cmd[i].body.y = cpu_to_le32(clips->y1);
700 cmd[i].body.width = cpu_to_le32(clips->x2 - clips->x1);
701 cmd[i].body.height = cpu_to_le32(clips->y2 - clips->y1);
704 vmw_fifo_commit(dev_priv, fifo_size);
708 static int do_dmabuf_define_gmrfb(struct drm_file *file_priv,
709 struct vmw_private *dev_priv,
710 struct vmw_framebuffer *framebuffer)
712 int depth = framebuffer->base.depth;
718 SVGAFifoCmdDefineGMRFB body;
721 /* Emulate RGBA support, contrary to svga_reg.h this is not
722 * supported by hosts. This is only a problem if we are reading
723 * this value later and expecting what we uploaded back.
728 fifo_size = sizeof(*cmd);
729 cmd = kmalloc(fifo_size, GFP_KERNEL);
730 if (unlikely(cmd == NULL)) {
731 DRM_ERROR("Failed to allocate temporary cmd buffer.\n");
735 memset(cmd, 0, fifo_size);
736 cmd->header = SVGA_CMD_DEFINE_GMRFB;
737 cmd->body.format.bitsPerPixel = framebuffer->base.bits_per_pixel;
738 cmd->body.format.colorDepth = depth;
739 cmd->body.format.reserved = 0;
740 cmd->body.bytesPerLine = framebuffer->base.pitch;
741 cmd->body.ptr.gmrId = framebuffer->user_handle;
742 cmd->body.ptr.offset = 0;
744 ret = vmw_execbuf_process(file_priv, dev_priv, NULL, cmd,
752 static int do_dmabuf_dirty_sou(struct drm_file *file_priv,
753 struct vmw_private *dev_priv,
754 struct vmw_framebuffer *framebuffer,
755 unsigned flags, unsigned color,
756 struct drm_clip_rect *clips,
757 unsigned num_clips, int increment)
759 struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
760 struct drm_clip_rect *clips_ptr;
761 int i, k, num_units, ret;
762 struct drm_crtc *crtc;
767 SVGAFifoCmdBlitGMRFBToScreen body;
770 ret = do_dmabuf_define_gmrfb(file_priv, dev_priv, framebuffer);
771 if (unlikely(ret != 0))
772 return ret; /* define_gmrfb prints warnings */
774 fifo_size = sizeof(*blits) * num_clips;
775 blits = kmalloc(fifo_size, GFP_KERNEL);
776 if (unlikely(blits == NULL)) {
777 DRM_ERROR("Failed to allocate temporary cmd buffer.\n");
782 list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list, head) {
783 if (crtc->fb != &framebuffer->base)
785 units[num_units++] = vmw_crtc_to_du(crtc);
788 for (k = 0; k < num_units; k++) {
789 struct vmw_display_unit *unit = units[k];
793 for (i = 0; i < num_clips; i++, clips_ptr += increment) {
794 int clip_x1 = clips_ptr->x1 - unit->crtc.x;
795 int clip_y1 = clips_ptr->y1 - unit->crtc.y;
796 int clip_x2 = clips_ptr->x2 - unit->crtc.x;
797 int clip_y2 = clips_ptr->y2 - unit->crtc.y;
799 /* skip any crtcs that misses the clip region */
800 if (clip_x1 >= unit->crtc.mode.hdisplay ||
801 clip_y1 >= unit->crtc.mode.vdisplay ||
802 clip_x2 <= 0 || clip_y2 <= 0)
805 blits[hit_num].header = SVGA_CMD_BLIT_GMRFB_TO_SCREEN;
806 blits[hit_num].body.destScreenId = unit->unit;
807 blits[hit_num].body.srcOrigin.x = clips_ptr->x1;
808 blits[hit_num].body.srcOrigin.y = clips_ptr->y1;
809 blits[hit_num].body.destRect.left = clip_x1;
810 blits[hit_num].body.destRect.top = clip_y1;
811 blits[hit_num].body.destRect.right = clip_x2;
812 blits[hit_num].body.destRect.bottom = clip_y2;
816 /* no clips hit the crtc */
820 fifo_size = sizeof(*blits) * hit_num;
821 ret = vmw_execbuf_process(file_priv, dev_priv, NULL, blits,
824 if (unlikely(ret != 0))
833 int vmw_framebuffer_dmabuf_dirty(struct drm_framebuffer *framebuffer,
834 struct drm_file *file_priv,
835 unsigned flags, unsigned color,
836 struct drm_clip_rect *clips,
839 struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
840 struct vmw_master *vmaster = vmw_master(file_priv->master);
841 struct vmw_framebuffer_dmabuf *vfbd =
842 vmw_framebuffer_to_vfbd(framebuffer);
843 struct drm_clip_rect norect;
844 int ret, increment = 1;
846 ret = ttm_read_lock(&vmaster->lock, true);
847 if (unlikely(ret != 0))
853 norect.x1 = norect.y1 = 0;
854 norect.x2 = framebuffer->width;
855 norect.y2 = framebuffer->height;
856 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
861 if (dev_priv->ldu_priv) {
862 ret = do_dmabuf_dirty_ldu(dev_priv, &vfbd->base,
864 clips, num_clips, increment);
866 ret = do_dmabuf_dirty_sou(file_priv, dev_priv, &vfbd->base,
868 clips, num_clips, increment);
871 ttm_read_unlock(&vmaster->lock);
875 static struct drm_framebuffer_funcs vmw_framebuffer_dmabuf_funcs = {
876 .destroy = vmw_framebuffer_dmabuf_destroy,
877 .dirty = vmw_framebuffer_dmabuf_dirty,
878 .create_handle = vmw_framebuffer_create_handle,
882 * Pin the dmabuffer to the start of vram.
884 static int vmw_framebuffer_dmabuf_pin(struct vmw_framebuffer *vfb)
886 struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
887 struct vmw_framebuffer_dmabuf *vfbd =
888 vmw_framebuffer_to_vfbd(&vfb->base);
891 /* This code should not be used with screen objects */
892 BUG_ON(dev_priv->sou_priv);
894 vmw_overlay_pause_all(dev_priv);
896 ret = vmw_dmabuf_to_start_of_vram(dev_priv, vfbd->buffer, true, false);
898 vmw_overlay_resume_all(dev_priv);
905 static int vmw_framebuffer_dmabuf_unpin(struct vmw_framebuffer *vfb)
907 struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
908 struct vmw_framebuffer_dmabuf *vfbd =
909 vmw_framebuffer_to_vfbd(&vfb->base);
912 WARN_ON(!vfbd->buffer);
916 return vmw_dmabuf_unpin(dev_priv, vfbd->buffer, false);
919 static int vmw_kms_new_framebuffer_dmabuf(struct vmw_private *dev_priv,
920 struct vmw_dma_buffer *dmabuf,
921 struct vmw_framebuffer **out,
922 const struct drm_mode_fb_cmd
926 struct drm_device *dev = dev_priv->dev;
927 struct vmw_framebuffer_dmabuf *vfbd;
928 unsigned int requested_size;
931 requested_size = mode_cmd->height * mode_cmd->pitch;
932 if (unlikely(requested_size > dmabuf->base.num_pages * PAGE_SIZE)) {
933 DRM_ERROR("Screen buffer object size is too small "
934 "for requested mode.\n");
938 /* Limited framebuffer color depth support for screen objects */
939 if (dev_priv->sou_priv) {
940 switch (mode_cmd->depth) {
943 /* Only support 32 bpp for 32 and 24 depth fbs */
944 if (mode_cmd->bpp == 32)
947 DRM_ERROR("Invalid color depth/bbp: %d %d\n",
948 mode_cmd->depth, mode_cmd->bpp);
952 /* Only support 16 bpp for 16 and 15 depth fbs */
953 if (mode_cmd->bpp == 16)
956 DRM_ERROR("Invalid color depth/bbp: %d %d\n",
957 mode_cmd->depth, mode_cmd->bpp);
960 DRM_ERROR("Invalid color depth: %d\n", mode_cmd->depth);
965 vfbd = kzalloc(sizeof(*vfbd), GFP_KERNEL);
971 ret = drm_framebuffer_init(dev, &vfbd->base.base,
972 &vmw_framebuffer_dmabuf_funcs);
976 if (!vmw_dmabuf_reference(dmabuf)) {
977 DRM_ERROR("failed to reference dmabuf %p\n", dmabuf);
981 vfbd->base.base.bits_per_pixel = mode_cmd->bpp;
982 vfbd->base.base.pitch = mode_cmd->pitch;
983 vfbd->base.base.depth = mode_cmd->depth;
984 vfbd->base.base.width = mode_cmd->width;
985 vfbd->base.base.height = mode_cmd->height;
986 if (!dev_priv->sou_priv) {
987 vfbd->base.pin = vmw_framebuffer_dmabuf_pin;
988 vfbd->base.unpin = vmw_framebuffer_dmabuf_unpin;
990 vfbd->base.dmabuf = true;
991 vfbd->buffer = dmabuf;
992 vfbd->base.user_handle = mode_cmd->handle;
998 drm_framebuffer_cleanup(&vfbd->base.base);
1006 * Generic Kernel modesetting functions
1009 static struct drm_framebuffer *vmw_kms_fb_create(struct drm_device *dev,
1010 struct drm_file *file_priv,
1011 struct drm_mode_fb_cmd *mode_cmd)
1013 struct vmw_private *dev_priv = vmw_priv(dev);
1014 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
1015 struct vmw_framebuffer *vfb = NULL;
1016 struct vmw_surface *surface = NULL;
1017 struct vmw_dma_buffer *bo = NULL;
1018 struct ttm_base_object *user_obj;
1023 * This code should be conditioned on Screen Objects not being used.
1024 * If screen objects are used, we can allocate a GMR to hold the
1025 * requested framebuffer.
1028 required_size = mode_cmd->pitch * mode_cmd->height;
1029 if (unlikely(required_size > (u64) dev_priv->vram_size)) {
1030 DRM_ERROR("VRAM size is too small for requested mode.\n");
1031 return ERR_PTR(-ENOMEM);
1035 * Take a reference on the user object of the resource
1036 * backing the kms fb. This ensures that user-space handle
1037 * lookups on that resource will always work as long as
1038 * it's registered with a kms framebuffer. This is important,
1039 * since vmw_execbuf_process identifies resources in the
1040 * command stream using user-space handles.
1043 user_obj = ttm_base_object_lookup(tfile, mode_cmd->handle);
1044 if (unlikely(user_obj == NULL)) {
1045 DRM_ERROR("Could not locate requested kms frame buffer.\n");
1046 return ERR_PTR(-ENOENT);
1049 /* returns either a dmabuf or surface */
1050 ret = vmw_user_lookup_handle(dev_priv, tfile,
1056 /* Create the new framebuffer depending one what we got back */
1058 ret = vmw_kms_new_framebuffer_dmabuf(dev_priv, bo, &vfb,
1061 ret = vmw_kms_new_framebuffer_surface(dev_priv, file_priv,
1062 surface, &vfb, mode_cmd);
1067 /* vmw_user_lookup_handle takes one ref so does new_fb */
1069 vmw_dmabuf_unreference(&bo);
1071 vmw_surface_unreference(&surface);
1074 DRM_ERROR("failed to create vmw_framebuffer: %i\n", ret);
1075 ttm_base_object_unref(&user_obj);
1076 return ERR_PTR(ret);
1078 vfb->user_obj = user_obj;
1083 static struct drm_mode_config_funcs vmw_kms_funcs = {
1084 .fb_create = vmw_kms_fb_create,
1087 int vmw_kms_present(struct vmw_private *dev_priv,
1088 struct drm_file *file_priv,
1089 struct vmw_framebuffer *vfb,
1090 struct vmw_surface *surface,
1092 int32_t destX, int32_t destY,
1093 struct drm_vmw_rect *clips,
1096 struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
1097 struct drm_crtc *crtc;
1099 int i, k, num_units;
1100 int ret = 0; /* silence warning */
1103 SVGA3dCmdHeader header;
1104 SVGA3dCmdBlitSurfaceToScreen body;
1106 SVGASignedRect *blits;
1109 list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list, head) {
1110 if (crtc->fb != &vfb->base)
1112 units[num_units++] = vmw_crtc_to_du(crtc);
1115 BUG_ON(surface == NULL);
1116 BUG_ON(!clips || !num_clips);
1118 fifo_size = sizeof(*cmd) + sizeof(SVGASignedRect) * num_clips;
1119 cmd = kmalloc(fifo_size, GFP_KERNEL);
1120 if (unlikely(cmd == NULL)) {
1121 DRM_ERROR("Failed to allocate temporary fifo memory.\n");
1125 /* only need to do this once */
1126 memset(cmd, 0, fifo_size);
1127 cmd->header.id = cpu_to_le32(SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN);
1128 cmd->header.size = cpu_to_le32(fifo_size - sizeof(cmd->header));
1130 cmd->body.srcRect.left = 0;
1131 cmd->body.srcRect.right = surface->sizes[0].width;
1132 cmd->body.srcRect.top = 0;
1133 cmd->body.srcRect.bottom = surface->sizes[0].height;
1135 blits = (SVGASignedRect *)&cmd[1];
1136 for (i = 0; i < num_clips; i++) {
1137 blits[i].left = clips[i].x;
1138 blits[i].right = clips[i].x + clips[i].w;
1139 blits[i].top = clips[i].y;
1140 blits[i].bottom = clips[i].y + clips[i].h;
1143 for (k = 0; k < num_units; k++) {
1144 struct vmw_display_unit *unit = units[k];
1145 int clip_x1 = destX - unit->crtc.x;
1146 int clip_y1 = destY - unit->crtc.y;
1147 int clip_x2 = clip_x1 + surface->sizes[0].width;
1148 int clip_y2 = clip_y1 + surface->sizes[0].height;
1150 /* skip any crtcs that misses the clip region */
1151 if (clip_x1 >= unit->crtc.mode.hdisplay ||
1152 clip_y1 >= unit->crtc.mode.vdisplay ||
1153 clip_x2 <= 0 || clip_y2 <= 0)
1156 /* need to reset sid as it is changed by execbuf */
1157 cmd->body.srcImage.sid = sid;
1159 cmd->body.destScreenId = unit->unit;
1162 * The blit command is a lot more resilient then the
1163 * readback command when it comes to clip rects. So its
1164 * okay to go out of bounds.
1167 cmd->body.destRect.left = clip_x1;
1168 cmd->body.destRect.right = clip_x2;
1169 cmd->body.destRect.top = clip_y1;
1170 cmd->body.destRect.bottom = clip_y2;
1172 ret = vmw_execbuf_process(file_priv, dev_priv, NULL, cmd,
1173 fifo_size, 0, NULL);
1175 if (unlikely(ret != 0))
1184 int vmw_kms_readback(struct vmw_private *dev_priv,
1185 struct drm_file *file_priv,
1186 struct vmw_framebuffer *vfb,
1187 struct drm_vmw_fence_rep __user *user_fence_rep,
1188 struct drm_vmw_rect *clips,
1191 struct vmw_framebuffer_dmabuf *vfbd =
1192 vmw_framebuffer_to_vfbd(&vfb->base);
1193 struct vmw_dma_buffer *dmabuf = vfbd->buffer;
1194 struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
1195 struct drm_crtc *crtc;
1197 int i, k, ret, num_units, blits_pos;
1201 SVGAFifoCmdDefineGMRFB body;
1205 SVGAFifoCmdBlitScreenToGMRFB body;
1209 list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list, head) {
1210 if (crtc->fb != &vfb->base)
1212 units[num_units++] = vmw_crtc_to_du(crtc);
1215 BUG_ON(dmabuf == NULL);
1216 BUG_ON(!clips || !num_clips);
1218 /* take a safe guess at fifo size */
1219 fifo_size = sizeof(*cmd) + sizeof(*blits) * num_clips * num_units;
1220 cmd = kmalloc(fifo_size, GFP_KERNEL);
1221 if (unlikely(cmd == NULL)) {
1222 DRM_ERROR("Failed to allocate temporary fifo memory.\n");
1226 memset(cmd, 0, fifo_size);
1227 cmd->header = SVGA_CMD_DEFINE_GMRFB;
1228 cmd->body.format.bitsPerPixel = vfb->base.bits_per_pixel;
1229 cmd->body.format.colorDepth = vfb->base.depth;
1230 cmd->body.format.reserved = 0;
1231 cmd->body.bytesPerLine = vfb->base.pitch;
1232 cmd->body.ptr.gmrId = vfb->user_handle;
1233 cmd->body.ptr.offset = 0;
1235 blits = (void *)&cmd[1];
1237 for (i = 0; i < num_units; i++) {
1238 struct drm_vmw_rect *c = clips;
1239 for (k = 0; k < num_clips; k++, c++) {
1240 /* transform clip coords to crtc origin based coords */
1241 int clip_x1 = c->x - units[i]->crtc.x;
1242 int clip_x2 = c->x - units[i]->crtc.x + c->w;
1243 int clip_y1 = c->y - units[i]->crtc.y;
1244 int clip_y2 = c->y - units[i]->crtc.y + c->h;
1248 /* compensate for clipping, we negate
1249 * a negative number and add that.
1257 clip_x1 = max(clip_x1, 0);
1258 clip_y1 = max(clip_y1, 0);
1259 clip_x2 = min(clip_x2, units[i]->crtc.mode.hdisplay);
1260 clip_y2 = min(clip_y2, units[i]->crtc.mode.vdisplay);
1262 /* and cull any rects that misses the crtc */
1263 if (clip_x1 >= units[i]->crtc.mode.hdisplay ||
1264 clip_y1 >= units[i]->crtc.mode.vdisplay ||
1265 clip_x2 <= 0 || clip_y2 <= 0)
1268 blits[blits_pos].header = SVGA_CMD_BLIT_SCREEN_TO_GMRFB;
1269 blits[blits_pos].body.srcScreenId = units[i]->unit;
1270 blits[blits_pos].body.destOrigin.x = dest_x;
1271 blits[blits_pos].body.destOrigin.y = dest_y;
1273 blits[blits_pos].body.srcRect.left = clip_x1;
1274 blits[blits_pos].body.srcRect.top = clip_y1;
1275 blits[blits_pos].body.srcRect.right = clip_x2;
1276 blits[blits_pos].body.srcRect.bottom = clip_y2;
1280 /* reset size here and use calculated exact size from loops */
1281 fifo_size = sizeof(*cmd) + sizeof(*blits) * blits_pos;
1283 ret = vmw_execbuf_process(file_priv, dev_priv, NULL, cmd, fifo_size,
1291 int vmw_kms_init(struct vmw_private *dev_priv)
1293 struct drm_device *dev = dev_priv->dev;
1296 drm_mode_config_init(dev);
1297 dev->mode_config.funcs = &vmw_kms_funcs;
1298 dev->mode_config.min_width = 1;
1299 dev->mode_config.min_height = 1;
1300 /* assumed largest fb size */
1301 dev->mode_config.max_width = 8192;
1302 dev->mode_config.max_height = 8192;
1304 ret = vmw_kms_init_screen_object_display(dev_priv);
1305 if (ret) /* Fallback */
1306 (void)vmw_kms_init_legacy_display_system(dev_priv);
1311 int vmw_kms_close(struct vmw_private *dev_priv)
1314 * Docs says we should take the lock before calling this function
1315 * but since it destroys encoders and our destructor calls
1316 * drm_encoder_cleanup which takes the lock we deadlock.
1318 drm_mode_config_cleanup(dev_priv->dev);
1319 if (dev_priv->sou_priv)
1320 vmw_kms_close_screen_object_display(dev_priv);
1322 vmw_kms_close_legacy_display_system(dev_priv);
1326 int vmw_kms_cursor_bypass_ioctl(struct drm_device *dev, void *data,
1327 struct drm_file *file_priv)
1329 struct drm_vmw_cursor_bypass_arg *arg = data;
1330 struct vmw_display_unit *du;
1331 struct drm_mode_object *obj;
1332 struct drm_crtc *crtc;
1336 mutex_lock(&dev->mode_config.mutex);
1337 if (arg->flags & DRM_VMW_CURSOR_BYPASS_ALL) {
1339 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1340 du = vmw_crtc_to_du(crtc);
1341 du->hotspot_x = arg->xhot;
1342 du->hotspot_y = arg->yhot;
1345 mutex_unlock(&dev->mode_config.mutex);
1349 obj = drm_mode_object_find(dev, arg->crtc_id, DRM_MODE_OBJECT_CRTC);
1355 crtc = obj_to_crtc(obj);
1356 du = vmw_crtc_to_du(crtc);
1358 du->hotspot_x = arg->xhot;
1359 du->hotspot_y = arg->yhot;
1362 mutex_unlock(&dev->mode_config.mutex);
1367 int vmw_kms_write_svga(struct vmw_private *vmw_priv,
1368 unsigned width, unsigned height, unsigned pitch,
1369 unsigned bpp, unsigned depth)
1371 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1372 vmw_write(vmw_priv, SVGA_REG_PITCHLOCK, pitch);
1373 else if (vmw_fifo_have_pitchlock(vmw_priv))
1374 iowrite32(pitch, vmw_priv->mmio_virt + SVGA_FIFO_PITCHLOCK);
1375 vmw_write(vmw_priv, SVGA_REG_WIDTH, width);
1376 vmw_write(vmw_priv, SVGA_REG_HEIGHT, height);
1377 vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, bpp);
1379 if (vmw_read(vmw_priv, SVGA_REG_DEPTH) != depth) {
1380 DRM_ERROR("Invalid depth %u for %u bpp, host expects %u\n",
1381 depth, bpp, vmw_read(vmw_priv, SVGA_REG_DEPTH));
1388 int vmw_kms_save_vga(struct vmw_private *vmw_priv)
1390 struct vmw_vga_topology_state *save;
1393 vmw_priv->vga_width = vmw_read(vmw_priv, SVGA_REG_WIDTH);
1394 vmw_priv->vga_height = vmw_read(vmw_priv, SVGA_REG_HEIGHT);
1395 vmw_priv->vga_bpp = vmw_read(vmw_priv, SVGA_REG_BITS_PER_PIXEL);
1396 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1397 vmw_priv->vga_pitchlock =
1398 vmw_read(vmw_priv, SVGA_REG_PITCHLOCK);
1399 else if (vmw_fifo_have_pitchlock(vmw_priv))
1400 vmw_priv->vga_pitchlock = ioread32(vmw_priv->mmio_virt +
1401 SVGA_FIFO_PITCHLOCK);
1403 if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
1406 vmw_priv->num_displays = vmw_read(vmw_priv,
1407 SVGA_REG_NUM_GUEST_DISPLAYS);
1409 if (vmw_priv->num_displays == 0)
1410 vmw_priv->num_displays = 1;
1412 for (i = 0; i < vmw_priv->num_displays; ++i) {
1413 save = &vmw_priv->vga_save[i];
1414 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
1415 save->primary = vmw_read(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY);
1416 save->pos_x = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_X);
1417 save->pos_y = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y);
1418 save->width = vmw_read(vmw_priv, SVGA_REG_DISPLAY_WIDTH);
1419 save->height = vmw_read(vmw_priv, SVGA_REG_DISPLAY_HEIGHT);
1420 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
1421 if (i == 0 && vmw_priv->num_displays == 1 &&
1422 save->width == 0 && save->height == 0) {
1425 * It should be fairly safe to assume that these
1426 * values are uninitialized.
1429 save->width = vmw_priv->vga_width - save->pos_x;
1430 save->height = vmw_priv->vga_height - save->pos_y;
1437 int vmw_kms_restore_vga(struct vmw_private *vmw_priv)
1439 struct vmw_vga_topology_state *save;
1442 vmw_write(vmw_priv, SVGA_REG_WIDTH, vmw_priv->vga_width);
1443 vmw_write(vmw_priv, SVGA_REG_HEIGHT, vmw_priv->vga_height);
1444 vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, vmw_priv->vga_bpp);
1445 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1446 vmw_write(vmw_priv, SVGA_REG_PITCHLOCK,
1447 vmw_priv->vga_pitchlock);
1448 else if (vmw_fifo_have_pitchlock(vmw_priv))
1449 iowrite32(vmw_priv->vga_pitchlock,
1450 vmw_priv->mmio_virt + SVGA_FIFO_PITCHLOCK);
1452 if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
1455 for (i = 0; i < vmw_priv->num_displays; ++i) {
1456 save = &vmw_priv->vga_save[i];
1457 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
1458 vmw_write(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY, save->primary);
1459 vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_X, save->pos_x);
1460 vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y, save->pos_y);
1461 vmw_write(vmw_priv, SVGA_REG_DISPLAY_WIDTH, save->width);
1462 vmw_write(vmw_priv, SVGA_REG_DISPLAY_HEIGHT, save->height);
1463 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
1469 bool vmw_kms_validate_mode_vram(struct vmw_private *dev_priv,
1473 return ((u64) pitch * (u64) height) < (u64) dev_priv->vram_size;
1478 * Function called by DRM code called with vbl_lock held.
1480 u32 vmw_get_vblank_counter(struct drm_device *dev, int crtc)
1486 * Function called by DRM code called with vbl_lock held.
1488 int vmw_enable_vblank(struct drm_device *dev, int crtc)
1494 * Function called by DRM code called with vbl_lock held.
1496 void vmw_disable_vblank(struct drm_device *dev, int crtc)
1502 * Small shared kms functions.
1505 int vmw_du_update_layout(struct vmw_private *dev_priv, unsigned num,
1506 struct drm_vmw_rect *rects)
1508 struct drm_device *dev = dev_priv->dev;
1509 struct vmw_display_unit *du;
1510 struct drm_connector *con;
1512 mutex_lock(&dev->mode_config.mutex);
1518 DRM_INFO("%s: new layout ", __func__);
1519 for (i = 0; i < num; i++)
1520 DRM_INFO("(%i, %i %ux%u) ", rects[i].x, rects[i].y,
1521 rects[i].w, rects[i].h);
1526 list_for_each_entry(con, &dev->mode_config.connector_list, head) {
1527 du = vmw_connector_to_du(con);
1528 if (num > du->unit) {
1529 du->pref_width = rects[du->unit].w;
1530 du->pref_height = rects[du->unit].h;
1531 du->pref_active = true;
1532 du->gui_x = rects[du->unit].x;
1533 du->gui_y = rects[du->unit].y;
1535 du->pref_width = 800;
1536 du->pref_height = 600;
1537 du->pref_active = false;
1539 con->status = vmw_du_connector_detect(con, true);
1542 mutex_unlock(&dev->mode_config.mutex);
1547 void vmw_du_crtc_save(struct drm_crtc *crtc)
1551 void vmw_du_crtc_restore(struct drm_crtc *crtc)
1555 void vmw_du_crtc_gamma_set(struct drm_crtc *crtc,
1556 u16 *r, u16 *g, u16 *b,
1557 uint32_t start, uint32_t size)
1559 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
1562 for (i = 0; i < size; i++) {
1563 DRM_DEBUG("%d r/g/b = 0x%04x / 0x%04x / 0x%04x\n", i,
1565 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 0, r[i] >> 8);
1566 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 1, g[i] >> 8);
1567 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 2, b[i] >> 8);
1571 void vmw_du_connector_dpms(struct drm_connector *connector, int mode)
1575 void vmw_du_connector_save(struct drm_connector *connector)
1579 void vmw_du_connector_restore(struct drm_connector *connector)
1583 enum drm_connector_status
1584 vmw_du_connector_detect(struct drm_connector *connector, bool force)
1586 uint32_t num_displays;
1587 struct drm_device *dev = connector->dev;
1588 struct vmw_private *dev_priv = vmw_priv(dev);
1589 struct vmw_display_unit *du = vmw_connector_to_du(connector);
1591 mutex_lock(&dev_priv->hw_mutex);
1592 num_displays = vmw_read(dev_priv, SVGA_REG_NUM_DISPLAYS);
1593 mutex_unlock(&dev_priv->hw_mutex);
1595 return ((vmw_connector_to_du(connector)->unit < num_displays &&
1597 connector_status_connected : connector_status_disconnected);
1600 static struct drm_display_mode vmw_kms_connector_builtin[] = {
1602 { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 25175, 640, 656,
1603 752, 800, 0, 480, 489, 492, 525, 0,
1604 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
1606 { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 40000, 800, 840,
1607 968, 1056, 0, 600, 601, 605, 628, 0,
1608 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1610 { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 65000, 1024, 1048,
1611 1184, 1344, 0, 768, 771, 777, 806, 0,
1612 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
1614 { DRM_MODE("1152x864", DRM_MODE_TYPE_DRIVER, 108000, 1152, 1216,
1615 1344, 1600, 0, 864, 865, 868, 900, 0,
1616 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1618 { DRM_MODE("1280x768", DRM_MODE_TYPE_DRIVER, 79500, 1280, 1344,
1619 1472, 1664, 0, 768, 771, 778, 798, 0,
1620 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1622 { DRM_MODE("1280x800", DRM_MODE_TYPE_DRIVER, 83500, 1280, 1352,
1623 1480, 1680, 0, 800, 803, 809, 831, 0,
1624 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
1626 { DRM_MODE("1280x960", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1376,
1627 1488, 1800, 0, 960, 961, 964, 1000, 0,
1628 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1629 /* 1280x1024@60Hz */
1630 { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1328,
1631 1440, 1688, 0, 1024, 1025, 1028, 1066, 0,
1632 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1634 { DRM_MODE("1360x768", DRM_MODE_TYPE_DRIVER, 85500, 1360, 1424,
1635 1536, 1792, 0, 768, 771, 777, 795, 0,
1636 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1637 /* 1440x1050@60Hz */
1638 { DRM_MODE("1400x1050", DRM_MODE_TYPE_DRIVER, 121750, 1400, 1488,
1639 1632, 1864, 0, 1050, 1053, 1057, 1089, 0,
1640 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1642 { DRM_MODE("1440x900", DRM_MODE_TYPE_DRIVER, 106500, 1440, 1520,
1643 1672, 1904, 0, 900, 903, 909, 934, 0,
1644 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1645 /* 1600x1200@60Hz */
1646 { DRM_MODE("1600x1200", DRM_MODE_TYPE_DRIVER, 162000, 1600, 1664,
1647 1856, 2160, 0, 1200, 1201, 1204, 1250, 0,
1648 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1649 /* 1680x1050@60Hz */
1650 { DRM_MODE("1680x1050", DRM_MODE_TYPE_DRIVER, 146250, 1680, 1784,
1651 1960, 2240, 0, 1050, 1053, 1059, 1089, 0,
1652 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1653 /* 1792x1344@60Hz */
1654 { DRM_MODE("1792x1344", DRM_MODE_TYPE_DRIVER, 204750, 1792, 1920,
1655 2120, 2448, 0, 1344, 1345, 1348, 1394, 0,
1656 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1657 /* 1853x1392@60Hz */
1658 { DRM_MODE("1856x1392", DRM_MODE_TYPE_DRIVER, 218250, 1856, 1952,
1659 2176, 2528, 0, 1392, 1393, 1396, 1439, 0,
1660 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1661 /* 1920x1200@60Hz */
1662 { DRM_MODE("1920x1200", DRM_MODE_TYPE_DRIVER, 193250, 1920, 2056,
1663 2256, 2592, 0, 1200, 1203, 1209, 1245, 0,
1664 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1665 /* 1920x1440@60Hz */
1666 { DRM_MODE("1920x1440", DRM_MODE_TYPE_DRIVER, 234000, 1920, 2048,
1667 2256, 2600, 0, 1440, 1441, 1444, 1500, 0,
1668 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1669 /* 2560x1600@60Hz */
1670 { DRM_MODE("2560x1600", DRM_MODE_TYPE_DRIVER, 348500, 2560, 2752,
1671 3032, 3504, 0, 1600, 1603, 1609, 1658, 0,
1672 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1674 { DRM_MODE("", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) },
1678 * vmw_guess_mode_timing - Provide fake timings for a
1679 * 60Hz vrefresh mode.
1681 * @mode - Pointer to a struct drm_display_mode with hdisplay and vdisplay
1682 * members filled in.
1684 static void vmw_guess_mode_timing(struct drm_display_mode *mode)
1686 mode->hsync_start = mode->hdisplay + 50;
1687 mode->hsync_end = mode->hsync_start + 50;
1688 mode->htotal = mode->hsync_end + 50;
1690 mode->vsync_start = mode->vdisplay + 50;
1691 mode->vsync_end = mode->vsync_start + 50;
1692 mode->vtotal = mode->vsync_end + 50;
1694 mode->clock = (u32)mode->htotal * (u32)mode->vtotal / 100 * 6;
1695 mode->vrefresh = drm_mode_vrefresh(mode);
1699 int vmw_du_connector_fill_modes(struct drm_connector *connector,
1700 uint32_t max_width, uint32_t max_height)
1702 struct vmw_display_unit *du = vmw_connector_to_du(connector);
1703 struct drm_device *dev = connector->dev;
1704 struct vmw_private *dev_priv = vmw_priv(dev);
1705 struct drm_display_mode *mode = NULL;
1706 struct drm_display_mode *bmode;
1707 struct drm_display_mode prefmode = { DRM_MODE("preferred",
1708 DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED,
1709 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1710 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC)
1714 /* Add preferred mode */
1716 mode = drm_mode_duplicate(dev, &prefmode);
1719 mode->hdisplay = du->pref_width;
1720 mode->vdisplay = du->pref_height;
1721 vmw_guess_mode_timing(mode);
1723 if (vmw_kms_validate_mode_vram(dev_priv, mode->hdisplay * 2,
1725 drm_mode_probed_add(connector, mode);
1727 drm_mode_destroy(dev, mode);
1731 if (du->pref_mode) {
1732 list_del_init(&du->pref_mode->head);
1733 drm_mode_destroy(dev, du->pref_mode);
1736 /* mode might be null here, this is intended */
1737 du->pref_mode = mode;
1740 for (i = 0; vmw_kms_connector_builtin[i].type != 0; i++) {
1741 bmode = &vmw_kms_connector_builtin[i];
1742 if (bmode->hdisplay > max_width ||
1743 bmode->vdisplay > max_height)
1746 if (!vmw_kms_validate_mode_vram(dev_priv, bmode->hdisplay * 2,
1750 mode = drm_mode_duplicate(dev, bmode);
1753 mode->vrefresh = drm_mode_vrefresh(mode);
1755 drm_mode_probed_add(connector, mode);
1758 /* Move the prefered mode first, help apps pick the right mode. */
1760 list_move(&du->pref_mode->head, &connector->probed_modes);
1762 drm_mode_connector_list_update(connector);
1767 int vmw_du_connector_set_property(struct drm_connector *connector,
1768 struct drm_property *property,
1775 int vmw_kms_update_layout_ioctl(struct drm_device *dev, void *data,
1776 struct drm_file *file_priv)
1778 struct vmw_private *dev_priv = vmw_priv(dev);
1779 struct drm_vmw_update_layout_arg *arg =
1780 (struct drm_vmw_update_layout_arg *)data;
1781 struct vmw_master *vmaster = vmw_master(file_priv->master);
1782 void __user *user_rects;
1783 struct drm_vmw_rect *rects;
1784 unsigned rects_size;
1787 struct drm_mode_config *mode_config = &dev->mode_config;
1789 ret = ttm_read_lock(&vmaster->lock, true);
1790 if (unlikely(ret != 0))
1793 if (!arg->num_outputs) {
1794 struct drm_vmw_rect def_rect = {0, 0, 800, 600};
1795 vmw_du_update_layout(dev_priv, 1, &def_rect);
1799 rects_size = arg->num_outputs * sizeof(struct drm_vmw_rect);
1800 rects = kcalloc(arg->num_outputs, sizeof(struct drm_vmw_rect),
1802 if (unlikely(!rects)) {
1807 user_rects = (void __user *)(unsigned long)arg->rects;
1808 ret = copy_from_user(rects, user_rects, rects_size);
1809 if (unlikely(ret != 0)) {
1810 DRM_ERROR("Failed to get rects.\n");
1815 for (i = 0; i < arg->num_outputs; ++i) {
1816 if (rects[i].x < 0 ||
1818 rects[i].x + rects[i].w > mode_config->max_width ||
1819 rects[i].y + rects[i].h > mode_config->max_height) {
1820 DRM_ERROR("Invalid GUI layout.\n");
1826 vmw_du_update_layout(dev_priv, arg->num_outputs, rects);
1831 ttm_read_unlock(&vmaster->lock);