Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wirel...
[linux-drm-fsl-dcu.git] / drivers / gpu / drm / vmwgfx / vmwgfx_kms.c
1 /**************************************************************************
2  *
3  * Copyright © 2009 VMware, Inc., Palo Alto, CA., USA
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sub license, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24  * USE OR OTHER DEALINGS IN THE SOFTWARE.
25  *
26  **************************************************************************/
27
28 #include "vmwgfx_kms.h"
29
30
31 /* Might need a hrtimer here? */
32 #define VMWGFX_PRESENT_RATE ((HZ / 60 > 0) ? HZ / 60 : 1)
33
34
35 struct vmw_clip_rect {
36         int x1, x2, y1, y2;
37 };
38
39 /**
40  * Clip @num_rects number of @rects against @clip storing the
41  * results in @out_rects and the number of passed rects in @out_num.
42  */
43 void vmw_clip_cliprects(struct drm_clip_rect *rects,
44                         int num_rects,
45                         struct vmw_clip_rect clip,
46                         SVGASignedRect *out_rects,
47                         int *out_num)
48 {
49         int i, k;
50
51         for (i = 0, k = 0; i < num_rects; i++) {
52                 int x1 = max_t(int, clip.x1, rects[i].x1);
53                 int y1 = max_t(int, clip.y1, rects[i].y1);
54                 int x2 = min_t(int, clip.x2, rects[i].x2);
55                 int y2 = min_t(int, clip.y2, rects[i].y2);
56
57                 if (x1 >= x2)
58                         continue;
59                 if (y1 >= y2)
60                         continue;
61
62                 out_rects[k].left   = x1;
63                 out_rects[k].top    = y1;
64                 out_rects[k].right  = x2;
65                 out_rects[k].bottom = y2;
66                 k++;
67         }
68
69         *out_num = k;
70 }
71
72 void vmw_display_unit_cleanup(struct vmw_display_unit *du)
73 {
74         if (du->cursor_surface)
75                 vmw_surface_unreference(&du->cursor_surface);
76         if (du->cursor_dmabuf)
77                 vmw_dmabuf_unreference(&du->cursor_dmabuf);
78         drm_sysfs_connector_remove(&du->connector);
79         drm_crtc_cleanup(&du->crtc);
80         drm_encoder_cleanup(&du->encoder);
81         drm_connector_cleanup(&du->connector);
82 }
83
84 /*
85  * Display Unit Cursor functions
86  */
87
88 int vmw_cursor_update_image(struct vmw_private *dev_priv,
89                             u32 *image, u32 width, u32 height,
90                             u32 hotspotX, u32 hotspotY)
91 {
92         struct {
93                 u32 cmd;
94                 SVGAFifoCmdDefineAlphaCursor cursor;
95         } *cmd;
96         u32 image_size = width * height * 4;
97         u32 cmd_size = sizeof(*cmd) + image_size;
98
99         if (!image)
100                 return -EINVAL;
101
102         cmd = vmw_fifo_reserve(dev_priv, cmd_size);
103         if (unlikely(cmd == NULL)) {
104                 DRM_ERROR("Fifo reserve failed.\n");
105                 return -ENOMEM;
106         }
107
108         memset(cmd, 0, sizeof(*cmd));
109
110         memcpy(&cmd[1], image, image_size);
111
112         cmd->cmd = cpu_to_le32(SVGA_CMD_DEFINE_ALPHA_CURSOR);
113         cmd->cursor.id = cpu_to_le32(0);
114         cmd->cursor.width = cpu_to_le32(width);
115         cmd->cursor.height = cpu_to_le32(height);
116         cmd->cursor.hotspotX = cpu_to_le32(hotspotX);
117         cmd->cursor.hotspotY = cpu_to_le32(hotspotY);
118
119         vmw_fifo_commit(dev_priv, cmd_size);
120
121         return 0;
122 }
123
124 int vmw_cursor_update_dmabuf(struct vmw_private *dev_priv,
125                              struct vmw_dma_buffer *dmabuf,
126                              u32 width, u32 height,
127                              u32 hotspotX, u32 hotspotY)
128 {
129         struct ttm_bo_kmap_obj map;
130         unsigned long kmap_offset;
131         unsigned long kmap_num;
132         void *virtual;
133         bool dummy;
134         int ret;
135
136         kmap_offset = 0;
137         kmap_num = (width*height*4 + PAGE_SIZE - 1) >> PAGE_SHIFT;
138
139         ret = ttm_bo_reserve(&dmabuf->base, true, false, false, 0);
140         if (unlikely(ret != 0)) {
141                 DRM_ERROR("reserve failed\n");
142                 return -EINVAL;
143         }
144
145         ret = ttm_bo_kmap(&dmabuf->base, kmap_offset, kmap_num, &map);
146         if (unlikely(ret != 0))
147                 goto err_unreserve;
148
149         virtual = ttm_kmap_obj_virtual(&map, &dummy);
150         ret = vmw_cursor_update_image(dev_priv, virtual, width, height,
151                                       hotspotX, hotspotY);
152
153         ttm_bo_kunmap(&map);
154 err_unreserve:
155         ttm_bo_unreserve(&dmabuf->base);
156
157         return ret;
158 }
159
160
161 void vmw_cursor_update_position(struct vmw_private *dev_priv,
162                                 bool show, int x, int y)
163 {
164         __le32 __iomem *fifo_mem = dev_priv->mmio_virt;
165         uint32_t count;
166
167         iowrite32(show ? 1 : 0, fifo_mem + SVGA_FIFO_CURSOR_ON);
168         iowrite32(x, fifo_mem + SVGA_FIFO_CURSOR_X);
169         iowrite32(y, fifo_mem + SVGA_FIFO_CURSOR_Y);
170         count = ioread32(fifo_mem + SVGA_FIFO_CURSOR_COUNT);
171         iowrite32(++count, fifo_mem + SVGA_FIFO_CURSOR_COUNT);
172 }
173
174 int vmw_du_crtc_cursor_set(struct drm_crtc *crtc, struct drm_file *file_priv,
175                            uint32_t handle, uint32_t width, uint32_t height)
176 {
177         struct vmw_private *dev_priv = vmw_priv(crtc->dev);
178         struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
179         struct vmw_surface *surface = NULL;
180         struct vmw_dma_buffer *dmabuf = NULL;
181         int ret;
182
183         /*
184          * FIXME: Unclear whether there's any global state touched by the
185          * cursor_set function, especially vmw_cursor_update_position looks
186          * suspicious. For now take the easy route and reacquire all locks. We
187          * can do this since the caller in the drm core doesn't check anything
188          * which is protected by any looks.
189          */
190         mutex_unlock(&crtc->mutex);
191         drm_modeset_lock_all(dev_priv->dev);
192
193         /* A lot of the code assumes this */
194         if (handle && (width != 64 || height != 64)) {
195                 ret = -EINVAL;
196                 goto out;
197         }
198
199         if (handle) {
200                 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
201
202                 ret = vmw_user_lookup_handle(dev_priv, tfile,
203                                              handle, &surface, &dmabuf);
204                 if (ret) {
205                         DRM_ERROR("failed to find surface or dmabuf: %i\n", ret);
206                         ret = -EINVAL;
207                         goto out;
208                 }
209         }
210
211         /* need to do this before taking down old image */
212         if (surface && !surface->snooper.image) {
213                 DRM_ERROR("surface not suitable for cursor\n");
214                 vmw_surface_unreference(&surface);
215                 ret = -EINVAL;
216                 goto out;
217         }
218
219         /* takedown old cursor */
220         if (du->cursor_surface) {
221                 du->cursor_surface->snooper.crtc = NULL;
222                 vmw_surface_unreference(&du->cursor_surface);
223         }
224         if (du->cursor_dmabuf)
225                 vmw_dmabuf_unreference(&du->cursor_dmabuf);
226
227         /* setup new image */
228         if (surface) {
229                 /* vmw_user_surface_lookup takes one reference */
230                 du->cursor_surface = surface;
231
232                 du->cursor_surface->snooper.crtc = crtc;
233                 du->cursor_age = du->cursor_surface->snooper.age;
234                 vmw_cursor_update_image(dev_priv, surface->snooper.image,
235                                         64, 64, du->hotspot_x, du->hotspot_y);
236         } else if (dmabuf) {
237                 /* vmw_user_surface_lookup takes one reference */
238                 du->cursor_dmabuf = dmabuf;
239
240                 ret = vmw_cursor_update_dmabuf(dev_priv, dmabuf, width, height,
241                                                du->hotspot_x, du->hotspot_y);
242         } else {
243                 vmw_cursor_update_position(dev_priv, false, 0, 0);
244                 ret = 0;
245                 goto out;
246         }
247
248         vmw_cursor_update_position(dev_priv, true,
249                                    du->cursor_x + du->hotspot_x,
250                                    du->cursor_y + du->hotspot_y);
251
252         ret = 0;
253 out:
254         drm_modeset_unlock_all(dev_priv->dev);
255         mutex_lock(&crtc->mutex);
256
257         return ret;
258 }
259
260 int vmw_du_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
261 {
262         struct vmw_private *dev_priv = vmw_priv(crtc->dev);
263         struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
264         bool shown = du->cursor_surface || du->cursor_dmabuf ? true : false;
265
266         du->cursor_x = x + crtc->x;
267         du->cursor_y = y + crtc->y;
268
269         /*
270          * FIXME: Unclear whether there's any global state touched by the
271          * cursor_set function, especially vmw_cursor_update_position looks
272          * suspicious. For now take the easy route and reacquire all locks. We
273          * can do this since the caller in the drm core doesn't check anything
274          * which is protected by any looks.
275          */
276         mutex_unlock(&crtc->mutex);
277         drm_modeset_lock_all(dev_priv->dev);
278
279         vmw_cursor_update_position(dev_priv, shown,
280                                    du->cursor_x + du->hotspot_x,
281                                    du->cursor_y + du->hotspot_y);
282
283         drm_modeset_unlock_all(dev_priv->dev);
284         mutex_lock(&crtc->mutex);
285
286         return 0;
287 }
288
289 void vmw_kms_cursor_snoop(struct vmw_surface *srf,
290                           struct ttm_object_file *tfile,
291                           struct ttm_buffer_object *bo,
292                           SVGA3dCmdHeader *header)
293 {
294         struct ttm_bo_kmap_obj map;
295         unsigned long kmap_offset;
296         unsigned long kmap_num;
297         SVGA3dCopyBox *box;
298         unsigned box_count;
299         void *virtual;
300         bool dummy;
301         struct vmw_dma_cmd {
302                 SVGA3dCmdHeader header;
303                 SVGA3dCmdSurfaceDMA dma;
304         } *cmd;
305         int i, ret;
306
307         cmd = container_of(header, struct vmw_dma_cmd, header);
308
309         /* No snooper installed */
310         if (!srf->snooper.image)
311                 return;
312
313         if (cmd->dma.host.face != 0 || cmd->dma.host.mipmap != 0) {
314                 DRM_ERROR("face and mipmap for cursors should never != 0\n");
315                 return;
316         }
317
318         if (cmd->header.size < 64) {
319                 DRM_ERROR("at least one full copy box must be given\n");
320                 return;
321         }
322
323         box = (SVGA3dCopyBox *)&cmd[1];
324         box_count = (cmd->header.size - sizeof(SVGA3dCmdSurfaceDMA)) /
325                         sizeof(SVGA3dCopyBox);
326
327         if (cmd->dma.guest.ptr.offset % PAGE_SIZE ||
328             box->x != 0    || box->y != 0    || box->z != 0    ||
329             box->srcx != 0 || box->srcy != 0 || box->srcz != 0 ||
330             box->d != 1    || box_count != 1) {
331                 /* TODO handle none page aligned offsets */
332                 /* TODO handle more dst & src != 0 */
333                 /* TODO handle more then one copy */
334                 DRM_ERROR("Cant snoop dma request for cursor!\n");
335                 DRM_ERROR("(%u, %u, %u) (%u, %u, %u) (%ux%ux%u) %u %u\n",
336                           box->srcx, box->srcy, box->srcz,
337                           box->x, box->y, box->z,
338                           box->w, box->h, box->d, box_count,
339                           cmd->dma.guest.ptr.offset);
340                 return;
341         }
342
343         kmap_offset = cmd->dma.guest.ptr.offset >> PAGE_SHIFT;
344         kmap_num = (64*64*4) >> PAGE_SHIFT;
345
346         ret = ttm_bo_reserve(bo, true, false, false, 0);
347         if (unlikely(ret != 0)) {
348                 DRM_ERROR("reserve failed\n");
349                 return;
350         }
351
352         ret = ttm_bo_kmap(bo, kmap_offset, kmap_num, &map);
353         if (unlikely(ret != 0))
354                 goto err_unreserve;
355
356         virtual = ttm_kmap_obj_virtual(&map, &dummy);
357
358         if (box->w == 64 && cmd->dma.guest.pitch == 64*4) {
359                 memcpy(srf->snooper.image, virtual, 64*64*4);
360         } else {
361                 /* Image is unsigned pointer. */
362                 for (i = 0; i < box->h; i++)
363                         memcpy(srf->snooper.image + i * 64,
364                                virtual + i * cmd->dma.guest.pitch,
365                                box->w * 4);
366         }
367
368         srf->snooper.age++;
369
370         /* we can't call this function from this function since execbuf has
371          * reserved fifo space.
372          *
373          * if (srf->snooper.crtc)
374          *      vmw_ldu_crtc_cursor_update_image(dev_priv,
375          *                                       srf->snooper.image, 64, 64,
376          *                                       du->hotspot_x, du->hotspot_y);
377          */
378
379         ttm_bo_kunmap(&map);
380 err_unreserve:
381         ttm_bo_unreserve(bo);
382 }
383
384 void vmw_kms_cursor_post_execbuf(struct vmw_private *dev_priv)
385 {
386         struct drm_device *dev = dev_priv->dev;
387         struct vmw_display_unit *du;
388         struct drm_crtc *crtc;
389
390         mutex_lock(&dev->mode_config.mutex);
391
392         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
393                 du = vmw_crtc_to_du(crtc);
394                 if (!du->cursor_surface ||
395                     du->cursor_age == du->cursor_surface->snooper.age)
396                         continue;
397
398                 du->cursor_age = du->cursor_surface->snooper.age;
399                 vmw_cursor_update_image(dev_priv,
400                                         du->cursor_surface->snooper.image,
401                                         64, 64, du->hotspot_x, du->hotspot_y);
402         }
403
404         mutex_unlock(&dev->mode_config.mutex);
405 }
406
407 /*
408  * Generic framebuffer code
409  */
410
411 /*
412  * Surface framebuffer code
413  */
414
415 #define vmw_framebuffer_to_vfbs(x) \
416         container_of(x, struct vmw_framebuffer_surface, base.base)
417
418 struct vmw_framebuffer_surface {
419         struct vmw_framebuffer base;
420         struct vmw_surface *surface;
421         struct vmw_dma_buffer *buffer;
422         struct list_head head;
423         struct drm_master *master;
424 };
425
426 void vmw_framebuffer_surface_destroy(struct drm_framebuffer *framebuffer)
427 {
428         struct vmw_framebuffer_surface *vfbs =
429                 vmw_framebuffer_to_vfbs(framebuffer);
430         struct vmw_master *vmaster = vmw_master(vfbs->master);
431
432
433         mutex_lock(&vmaster->fb_surf_mutex);
434         list_del(&vfbs->head);
435         mutex_unlock(&vmaster->fb_surf_mutex);
436
437         drm_master_put(&vfbs->master);
438         drm_framebuffer_cleanup(framebuffer);
439         vmw_surface_unreference(&vfbs->surface);
440         ttm_base_object_unref(&vfbs->base.user_obj);
441
442         kfree(vfbs);
443 }
444
445 static int do_surface_dirty_sou(struct vmw_private *dev_priv,
446                                 struct drm_file *file_priv,
447                                 struct vmw_framebuffer *framebuffer,
448                                 unsigned flags, unsigned color,
449                                 struct drm_clip_rect *clips,
450                                 unsigned num_clips, int inc,
451                                 struct vmw_fence_obj **out_fence)
452 {
453         struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
454         struct drm_clip_rect *clips_ptr;
455         struct drm_clip_rect *tmp;
456         struct drm_crtc *crtc;
457         size_t fifo_size;
458         int i, num_units;
459         int ret = 0; /* silence warning */
460         int left, right, top, bottom;
461
462         struct {
463                 SVGA3dCmdHeader header;
464                 SVGA3dCmdBlitSurfaceToScreen body;
465         } *cmd;
466         SVGASignedRect *blits;
467
468         num_units = 0;
469         list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list,
470                             head) {
471                 if (crtc->fb != &framebuffer->base)
472                         continue;
473                 units[num_units++] = vmw_crtc_to_du(crtc);
474         }
475
476         BUG_ON(!clips || !num_clips);
477
478         tmp = kzalloc(sizeof(*tmp) * num_clips, GFP_KERNEL);
479         if (unlikely(tmp == NULL)) {
480                 DRM_ERROR("Temporary cliprect memory alloc failed.\n");
481                 return -ENOMEM;
482         }
483
484         fifo_size = sizeof(*cmd) + sizeof(SVGASignedRect) * num_clips;
485         cmd = kzalloc(fifo_size, GFP_KERNEL);
486         if (unlikely(cmd == NULL)) {
487                 DRM_ERROR("Temporary fifo memory alloc failed.\n");
488                 ret = -ENOMEM;
489                 goto out_free_tmp;
490         }
491
492         /* setup blits pointer */
493         blits = (SVGASignedRect *)&cmd[1];
494
495         /* initial clip region */
496         left = clips->x1;
497         right = clips->x2;
498         top = clips->y1;
499         bottom = clips->y2;
500
501         /* skip the first clip rect */
502         for (i = 1, clips_ptr = clips + inc;
503              i < num_clips; i++, clips_ptr += inc) {
504                 left = min_t(int, left, (int)clips_ptr->x1);
505                 right = max_t(int, right, (int)clips_ptr->x2);
506                 top = min_t(int, top, (int)clips_ptr->y1);
507                 bottom = max_t(int, bottom, (int)clips_ptr->y2);
508         }
509
510         /* only need to do this once */
511         cmd->header.id = cpu_to_le32(SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN);
512         cmd->header.size = cpu_to_le32(fifo_size - sizeof(cmd->header));
513
514         cmd->body.srcRect.left = left;
515         cmd->body.srcRect.right = right;
516         cmd->body.srcRect.top = top;
517         cmd->body.srcRect.bottom = bottom;
518
519         clips_ptr = clips;
520         for (i = 0; i < num_clips; i++, clips_ptr += inc) {
521                 tmp[i].x1 = clips_ptr->x1 - left;
522                 tmp[i].x2 = clips_ptr->x2 - left;
523                 tmp[i].y1 = clips_ptr->y1 - top;
524                 tmp[i].y2 = clips_ptr->y2 - top;
525         }
526
527         /* do per unit writing, reuse fifo for each */
528         for (i = 0; i < num_units; i++) {
529                 struct vmw_display_unit *unit = units[i];
530                 struct vmw_clip_rect clip;
531                 int num;
532
533                 clip.x1 = left - unit->crtc.x;
534                 clip.y1 = top - unit->crtc.y;
535                 clip.x2 = right - unit->crtc.x;
536                 clip.y2 = bottom - unit->crtc.y;
537
538                 /* skip any crtcs that misses the clip region */
539                 if (clip.x1 >= unit->crtc.mode.hdisplay ||
540                     clip.y1 >= unit->crtc.mode.vdisplay ||
541                     clip.x2 <= 0 || clip.y2 <= 0)
542                         continue;
543
544                 /*
545                  * In order for the clip rects to be correctly scaled
546                  * the src and dest rects needs to be the same size.
547                  */
548                 cmd->body.destRect.left = clip.x1;
549                 cmd->body.destRect.right = clip.x2;
550                 cmd->body.destRect.top = clip.y1;
551                 cmd->body.destRect.bottom = clip.y2;
552
553                 /* create a clip rect of the crtc in dest coords */
554                 clip.x2 = unit->crtc.mode.hdisplay - clip.x1;
555                 clip.y2 = unit->crtc.mode.vdisplay - clip.y1;
556                 clip.x1 = 0 - clip.x1;
557                 clip.y1 = 0 - clip.y1;
558
559                 /* need to reset sid as it is changed by execbuf */
560                 cmd->body.srcImage.sid = cpu_to_le32(framebuffer->user_handle);
561                 cmd->body.destScreenId = unit->unit;
562
563                 /* clip and write blits to cmd stream */
564                 vmw_clip_cliprects(tmp, num_clips, clip, blits, &num);
565
566                 /* if no cliprects hit skip this */
567                 if (num == 0)
568                         continue;
569
570                 /* only return the last fence */
571                 if (out_fence && *out_fence)
572                         vmw_fence_obj_unreference(out_fence);
573
574                 /* recalculate package length */
575                 fifo_size = sizeof(*cmd) + sizeof(SVGASignedRect) * num;
576                 cmd->header.size = cpu_to_le32(fifo_size - sizeof(cmd->header));
577                 ret = vmw_execbuf_process(file_priv, dev_priv, NULL, cmd,
578                                           fifo_size, 0, NULL, out_fence);
579
580                 if (unlikely(ret != 0))
581                         break;
582         }
583
584
585         kfree(cmd);
586 out_free_tmp:
587         kfree(tmp);
588
589         return ret;
590 }
591
592 int vmw_framebuffer_surface_dirty(struct drm_framebuffer *framebuffer,
593                                   struct drm_file *file_priv,
594                                   unsigned flags, unsigned color,
595                                   struct drm_clip_rect *clips,
596                                   unsigned num_clips)
597 {
598         struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
599         struct vmw_master *vmaster = vmw_master(file_priv->master);
600         struct vmw_framebuffer_surface *vfbs =
601                 vmw_framebuffer_to_vfbs(framebuffer);
602         struct drm_clip_rect norect;
603         int ret, inc = 1;
604
605         if (unlikely(vfbs->master != file_priv->master))
606                 return -EINVAL;
607
608         /* Require ScreenObject support for 3D */
609         if (!dev_priv->sou_priv)
610                 return -EINVAL;
611
612         ret = ttm_read_lock(&vmaster->lock, true);
613         if (unlikely(ret != 0))
614                 return ret;
615
616         if (!num_clips) {
617                 num_clips = 1;
618                 clips = &norect;
619                 norect.x1 = norect.y1 = 0;
620                 norect.x2 = framebuffer->width;
621                 norect.y2 = framebuffer->height;
622         } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
623                 num_clips /= 2;
624                 inc = 2; /* skip source rects */
625         }
626
627         ret = do_surface_dirty_sou(dev_priv, file_priv, &vfbs->base,
628                                    flags, color,
629                                    clips, num_clips, inc, NULL);
630
631         ttm_read_unlock(&vmaster->lock);
632         return 0;
633 }
634
635 static struct drm_framebuffer_funcs vmw_framebuffer_surface_funcs = {
636         .destroy = vmw_framebuffer_surface_destroy,
637         .dirty = vmw_framebuffer_surface_dirty,
638 };
639
640 static int vmw_kms_new_framebuffer_surface(struct vmw_private *dev_priv,
641                                            struct drm_file *file_priv,
642                                            struct vmw_surface *surface,
643                                            struct vmw_framebuffer **out,
644                                            const struct drm_mode_fb_cmd
645                                            *mode_cmd)
646
647 {
648         struct drm_device *dev = dev_priv->dev;
649         struct vmw_framebuffer_surface *vfbs;
650         enum SVGA3dSurfaceFormat format;
651         struct vmw_master *vmaster = vmw_master(file_priv->master);
652         int ret;
653
654         /* 3D is only supported on HWv8 hosts which supports screen objects */
655         if (!dev_priv->sou_priv)
656                 return -ENOSYS;
657
658         /*
659          * Sanity checks.
660          */
661
662         /* Surface must be marked as a scanout. */
663         if (unlikely(!surface->scanout))
664                 return -EINVAL;
665
666         if (unlikely(surface->mip_levels[0] != 1 ||
667                      surface->num_sizes != 1 ||
668                      surface->sizes[0].width < mode_cmd->width ||
669                      surface->sizes[0].height < mode_cmd->height ||
670                      surface->sizes[0].depth != 1)) {
671                 DRM_ERROR("Incompatible surface dimensions "
672                           "for requested mode.\n");
673                 return -EINVAL;
674         }
675
676         switch (mode_cmd->depth) {
677         case 32:
678                 format = SVGA3D_A8R8G8B8;
679                 break;
680         case 24:
681                 format = SVGA3D_X8R8G8B8;
682                 break;
683         case 16:
684                 format = SVGA3D_R5G6B5;
685                 break;
686         case 15:
687                 format = SVGA3D_A1R5G5B5;
688                 break;
689         case 8:
690                 format = SVGA3D_LUMINANCE8;
691                 break;
692         default:
693                 DRM_ERROR("Invalid color depth: %d\n", mode_cmd->depth);
694                 return -EINVAL;
695         }
696
697         if (unlikely(format != surface->format)) {
698                 DRM_ERROR("Invalid surface format for requested mode.\n");
699                 return -EINVAL;
700         }
701
702         vfbs = kzalloc(sizeof(*vfbs), GFP_KERNEL);
703         if (!vfbs) {
704                 ret = -ENOMEM;
705                 goto out_err1;
706         }
707
708         if (!vmw_surface_reference(surface)) {
709                 DRM_ERROR("failed to reference surface %p\n", surface);
710                 ret = -EINVAL;
711                 goto out_err2;
712         }
713
714         /* XXX get the first 3 from the surface info */
715         vfbs->base.base.bits_per_pixel = mode_cmd->bpp;
716         vfbs->base.base.pitches[0] = mode_cmd->pitch;
717         vfbs->base.base.depth = mode_cmd->depth;
718         vfbs->base.base.width = mode_cmd->width;
719         vfbs->base.base.height = mode_cmd->height;
720         vfbs->surface = surface;
721         vfbs->base.user_handle = mode_cmd->handle;
722         vfbs->master = drm_master_get(file_priv->master);
723
724         mutex_lock(&vmaster->fb_surf_mutex);
725         list_add_tail(&vfbs->head, &vmaster->fb_surf);
726         mutex_unlock(&vmaster->fb_surf_mutex);
727
728         *out = &vfbs->base;
729
730         ret = drm_framebuffer_init(dev, &vfbs->base.base,
731                                    &vmw_framebuffer_surface_funcs);
732         if (ret)
733                 goto out_err3;
734
735         return 0;
736
737 out_err3:
738         vmw_surface_unreference(&surface);
739 out_err2:
740         kfree(vfbs);
741 out_err1:
742         return ret;
743 }
744
745 /*
746  * Dmabuf framebuffer code
747  */
748
749 #define vmw_framebuffer_to_vfbd(x) \
750         container_of(x, struct vmw_framebuffer_dmabuf, base.base)
751
752 struct vmw_framebuffer_dmabuf {
753         struct vmw_framebuffer base;
754         struct vmw_dma_buffer *buffer;
755 };
756
757 void vmw_framebuffer_dmabuf_destroy(struct drm_framebuffer *framebuffer)
758 {
759         struct vmw_framebuffer_dmabuf *vfbd =
760                 vmw_framebuffer_to_vfbd(framebuffer);
761
762         drm_framebuffer_cleanup(framebuffer);
763         vmw_dmabuf_unreference(&vfbd->buffer);
764         ttm_base_object_unref(&vfbd->base.user_obj);
765
766         kfree(vfbd);
767 }
768
769 static int do_dmabuf_dirty_ldu(struct vmw_private *dev_priv,
770                                struct vmw_framebuffer *framebuffer,
771                                unsigned flags, unsigned color,
772                                struct drm_clip_rect *clips,
773                                unsigned num_clips, int increment)
774 {
775         size_t fifo_size;
776         int i;
777
778         struct {
779                 uint32_t header;
780                 SVGAFifoCmdUpdate body;
781         } *cmd;
782
783         fifo_size = sizeof(*cmd) * num_clips;
784         cmd = vmw_fifo_reserve(dev_priv, fifo_size);
785         if (unlikely(cmd == NULL)) {
786                 DRM_ERROR("Fifo reserve failed.\n");
787                 return -ENOMEM;
788         }
789
790         memset(cmd, 0, fifo_size);
791         for (i = 0; i < num_clips; i++, clips += increment) {
792                 cmd[i].header = cpu_to_le32(SVGA_CMD_UPDATE);
793                 cmd[i].body.x = cpu_to_le32(clips->x1);
794                 cmd[i].body.y = cpu_to_le32(clips->y1);
795                 cmd[i].body.width = cpu_to_le32(clips->x2 - clips->x1);
796                 cmd[i].body.height = cpu_to_le32(clips->y2 - clips->y1);
797         }
798
799         vmw_fifo_commit(dev_priv, fifo_size);
800         return 0;
801 }
802
803 static int do_dmabuf_define_gmrfb(struct drm_file *file_priv,
804                                   struct vmw_private *dev_priv,
805                                   struct vmw_framebuffer *framebuffer)
806 {
807         int depth = framebuffer->base.depth;
808         size_t fifo_size;
809         int ret;
810
811         struct {
812                 uint32_t header;
813                 SVGAFifoCmdDefineGMRFB body;
814         } *cmd;
815
816         /* Emulate RGBA support, contrary to svga_reg.h this is not
817          * supported by hosts. This is only a problem if we are reading
818          * this value later and expecting what we uploaded back.
819          */
820         if (depth == 32)
821                 depth = 24;
822
823         fifo_size = sizeof(*cmd);
824         cmd = kmalloc(fifo_size, GFP_KERNEL);
825         if (unlikely(cmd == NULL)) {
826                 DRM_ERROR("Failed to allocate temporary cmd buffer.\n");
827                 return -ENOMEM;
828         }
829
830         memset(cmd, 0, fifo_size);
831         cmd->header = SVGA_CMD_DEFINE_GMRFB;
832         cmd->body.format.bitsPerPixel = framebuffer->base.bits_per_pixel;
833         cmd->body.format.colorDepth = depth;
834         cmd->body.format.reserved = 0;
835         cmd->body.bytesPerLine = framebuffer->base.pitches[0];
836         cmd->body.ptr.gmrId = framebuffer->user_handle;
837         cmd->body.ptr.offset = 0;
838
839         ret = vmw_execbuf_process(file_priv, dev_priv, NULL, cmd,
840                                   fifo_size, 0, NULL, NULL);
841
842         kfree(cmd);
843
844         return ret;
845 }
846
847 static int do_dmabuf_dirty_sou(struct drm_file *file_priv,
848                                struct vmw_private *dev_priv,
849                                struct vmw_framebuffer *framebuffer,
850                                unsigned flags, unsigned color,
851                                struct drm_clip_rect *clips,
852                                unsigned num_clips, int increment,
853                                struct vmw_fence_obj **out_fence)
854 {
855         struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
856         struct drm_clip_rect *clips_ptr;
857         int i, k, num_units, ret;
858         struct drm_crtc *crtc;
859         size_t fifo_size;
860
861         struct {
862                 uint32_t header;
863                 SVGAFifoCmdBlitGMRFBToScreen body;
864         } *blits;
865
866         ret = do_dmabuf_define_gmrfb(file_priv, dev_priv, framebuffer);
867         if (unlikely(ret != 0))
868                 return ret; /* define_gmrfb prints warnings */
869
870         fifo_size = sizeof(*blits) * num_clips;
871         blits = kmalloc(fifo_size, GFP_KERNEL);
872         if (unlikely(blits == NULL)) {
873                 DRM_ERROR("Failed to allocate temporary cmd buffer.\n");
874                 return -ENOMEM;
875         }
876
877         num_units = 0;
878         list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list, head) {
879                 if (crtc->fb != &framebuffer->base)
880                         continue;
881                 units[num_units++] = vmw_crtc_to_du(crtc);
882         }
883
884         for (k = 0; k < num_units; k++) {
885                 struct vmw_display_unit *unit = units[k];
886                 int hit_num = 0;
887
888                 clips_ptr = clips;
889                 for (i = 0; i < num_clips; i++, clips_ptr += increment) {
890                         int clip_x1 = clips_ptr->x1 - unit->crtc.x;
891                         int clip_y1 = clips_ptr->y1 - unit->crtc.y;
892                         int clip_x2 = clips_ptr->x2 - unit->crtc.x;
893                         int clip_y2 = clips_ptr->y2 - unit->crtc.y;
894                         int move_x, move_y;
895
896                         /* skip any crtcs that misses the clip region */
897                         if (clip_x1 >= unit->crtc.mode.hdisplay ||
898                             clip_y1 >= unit->crtc.mode.vdisplay ||
899                             clip_x2 <= 0 || clip_y2 <= 0)
900                                 continue;
901
902                         /* clip size to crtc size */
903                         clip_x2 = min_t(int, clip_x2, unit->crtc.mode.hdisplay);
904                         clip_y2 = min_t(int, clip_y2, unit->crtc.mode.vdisplay);
905
906                         /* translate both src and dest to bring clip into screen */
907                         move_x = min_t(int, clip_x1, 0);
908                         move_y = min_t(int, clip_y1, 0);
909
910                         /* actual translate done here */
911                         blits[hit_num].header = SVGA_CMD_BLIT_GMRFB_TO_SCREEN;
912                         blits[hit_num].body.destScreenId = unit->unit;
913                         blits[hit_num].body.srcOrigin.x = clips_ptr->x1 - move_x;
914                         blits[hit_num].body.srcOrigin.y = clips_ptr->y1 - move_y;
915                         blits[hit_num].body.destRect.left = clip_x1 - move_x;
916                         blits[hit_num].body.destRect.top = clip_y1 - move_y;
917                         blits[hit_num].body.destRect.right = clip_x2;
918                         blits[hit_num].body.destRect.bottom = clip_y2;
919                         hit_num++;
920                 }
921
922                 /* no clips hit the crtc */
923                 if (hit_num == 0)
924                         continue;
925
926                 /* only return the last fence */
927                 if (out_fence && *out_fence)
928                         vmw_fence_obj_unreference(out_fence);
929
930                 fifo_size = sizeof(*blits) * hit_num;
931                 ret = vmw_execbuf_process(file_priv, dev_priv, NULL, blits,
932                                           fifo_size, 0, NULL, out_fence);
933
934                 if (unlikely(ret != 0))
935                         break;
936         }
937
938         kfree(blits);
939
940         return ret;
941 }
942
943 int vmw_framebuffer_dmabuf_dirty(struct drm_framebuffer *framebuffer,
944                                  struct drm_file *file_priv,
945                                  unsigned flags, unsigned color,
946                                  struct drm_clip_rect *clips,
947                                  unsigned num_clips)
948 {
949         struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
950         struct vmw_master *vmaster = vmw_master(file_priv->master);
951         struct vmw_framebuffer_dmabuf *vfbd =
952                 vmw_framebuffer_to_vfbd(framebuffer);
953         struct drm_clip_rect norect;
954         int ret, increment = 1;
955
956         ret = ttm_read_lock(&vmaster->lock, true);
957         if (unlikely(ret != 0))
958                 return ret;
959
960         if (!num_clips) {
961                 num_clips = 1;
962                 clips = &norect;
963                 norect.x1 = norect.y1 = 0;
964                 norect.x2 = framebuffer->width;
965                 norect.y2 = framebuffer->height;
966         } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
967                 num_clips /= 2;
968                 increment = 2;
969         }
970
971         if (dev_priv->ldu_priv) {
972                 ret = do_dmabuf_dirty_ldu(dev_priv, &vfbd->base,
973                                           flags, color,
974                                           clips, num_clips, increment);
975         } else {
976                 ret = do_dmabuf_dirty_sou(file_priv, dev_priv, &vfbd->base,
977                                           flags, color,
978                                           clips, num_clips, increment, NULL);
979         }
980
981         ttm_read_unlock(&vmaster->lock);
982         return ret;
983 }
984
985 static struct drm_framebuffer_funcs vmw_framebuffer_dmabuf_funcs = {
986         .destroy = vmw_framebuffer_dmabuf_destroy,
987         .dirty = vmw_framebuffer_dmabuf_dirty,
988 };
989
990 /**
991  * Pin the dmabuffer to the start of vram.
992  */
993 static int vmw_framebuffer_dmabuf_pin(struct vmw_framebuffer *vfb)
994 {
995         struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
996         struct vmw_framebuffer_dmabuf *vfbd =
997                 vmw_framebuffer_to_vfbd(&vfb->base);
998         int ret;
999
1000         /* This code should not be used with screen objects */
1001         BUG_ON(dev_priv->sou_priv);
1002
1003         vmw_overlay_pause_all(dev_priv);
1004
1005         ret = vmw_dmabuf_to_start_of_vram(dev_priv, vfbd->buffer, true, false);
1006
1007         vmw_overlay_resume_all(dev_priv);
1008
1009         WARN_ON(ret != 0);
1010
1011         return 0;
1012 }
1013
1014 static int vmw_framebuffer_dmabuf_unpin(struct vmw_framebuffer *vfb)
1015 {
1016         struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
1017         struct vmw_framebuffer_dmabuf *vfbd =
1018                 vmw_framebuffer_to_vfbd(&vfb->base);
1019
1020         if (!vfbd->buffer) {
1021                 WARN_ON(!vfbd->buffer);
1022                 return 0;
1023         }
1024
1025         return vmw_dmabuf_unpin(dev_priv, vfbd->buffer, false);
1026 }
1027
1028 static int vmw_kms_new_framebuffer_dmabuf(struct vmw_private *dev_priv,
1029                                           struct vmw_dma_buffer *dmabuf,
1030                                           struct vmw_framebuffer **out,
1031                                           const struct drm_mode_fb_cmd
1032                                           *mode_cmd)
1033
1034 {
1035         struct drm_device *dev = dev_priv->dev;
1036         struct vmw_framebuffer_dmabuf *vfbd;
1037         unsigned int requested_size;
1038         int ret;
1039
1040         requested_size = mode_cmd->height * mode_cmd->pitch;
1041         if (unlikely(requested_size > dmabuf->base.num_pages * PAGE_SIZE)) {
1042                 DRM_ERROR("Screen buffer object size is too small "
1043                           "for requested mode.\n");
1044                 return -EINVAL;
1045         }
1046
1047         /* Limited framebuffer color depth support for screen objects */
1048         if (dev_priv->sou_priv) {
1049                 switch (mode_cmd->depth) {
1050                 case 32:
1051                 case 24:
1052                         /* Only support 32 bpp for 32 and 24 depth fbs */
1053                         if (mode_cmd->bpp == 32)
1054                                 break;
1055
1056                         DRM_ERROR("Invalid color depth/bbp: %d %d\n",
1057                                   mode_cmd->depth, mode_cmd->bpp);
1058                         return -EINVAL;
1059                 case 16:
1060                 case 15:
1061                         /* Only support 16 bpp for 16 and 15 depth fbs */
1062                         if (mode_cmd->bpp == 16)
1063                                 break;
1064
1065                         DRM_ERROR("Invalid color depth/bbp: %d %d\n",
1066                                   mode_cmd->depth, mode_cmd->bpp);
1067                         return -EINVAL;
1068                 default:
1069                         DRM_ERROR("Invalid color depth: %d\n", mode_cmd->depth);
1070                         return -EINVAL;
1071                 }
1072         }
1073
1074         vfbd = kzalloc(sizeof(*vfbd), GFP_KERNEL);
1075         if (!vfbd) {
1076                 ret = -ENOMEM;
1077                 goto out_err1;
1078         }
1079
1080         if (!vmw_dmabuf_reference(dmabuf)) {
1081                 DRM_ERROR("failed to reference dmabuf %p\n", dmabuf);
1082                 ret = -EINVAL;
1083                 goto out_err2;
1084         }
1085
1086         vfbd->base.base.bits_per_pixel = mode_cmd->bpp;
1087         vfbd->base.base.pitches[0] = mode_cmd->pitch;
1088         vfbd->base.base.depth = mode_cmd->depth;
1089         vfbd->base.base.width = mode_cmd->width;
1090         vfbd->base.base.height = mode_cmd->height;
1091         if (!dev_priv->sou_priv) {
1092                 vfbd->base.pin = vmw_framebuffer_dmabuf_pin;
1093                 vfbd->base.unpin = vmw_framebuffer_dmabuf_unpin;
1094         }
1095         vfbd->base.dmabuf = true;
1096         vfbd->buffer = dmabuf;
1097         vfbd->base.user_handle = mode_cmd->handle;
1098         *out = &vfbd->base;
1099
1100         ret = drm_framebuffer_init(dev, &vfbd->base.base,
1101                                    &vmw_framebuffer_dmabuf_funcs);
1102         if (ret)
1103                 goto out_err3;
1104
1105         return 0;
1106
1107 out_err3:
1108         vmw_dmabuf_unreference(&dmabuf);
1109 out_err2:
1110         kfree(vfbd);
1111 out_err1:
1112         return ret;
1113 }
1114
1115 /*
1116  * Generic Kernel modesetting functions
1117  */
1118
1119 static struct drm_framebuffer *vmw_kms_fb_create(struct drm_device *dev,
1120                                                  struct drm_file *file_priv,
1121                                                  struct drm_mode_fb_cmd2 *mode_cmd2)
1122 {
1123         struct vmw_private *dev_priv = vmw_priv(dev);
1124         struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
1125         struct vmw_framebuffer *vfb = NULL;
1126         struct vmw_surface *surface = NULL;
1127         struct vmw_dma_buffer *bo = NULL;
1128         struct ttm_base_object *user_obj;
1129         struct drm_mode_fb_cmd mode_cmd;
1130         int ret;
1131
1132         mode_cmd.width = mode_cmd2->width;
1133         mode_cmd.height = mode_cmd2->height;
1134         mode_cmd.pitch = mode_cmd2->pitches[0];
1135         mode_cmd.handle = mode_cmd2->handles[0];
1136         drm_fb_get_bpp_depth(mode_cmd2->pixel_format, &mode_cmd.depth,
1137                                     &mode_cmd.bpp);
1138
1139         /**
1140          * This code should be conditioned on Screen Objects not being used.
1141          * If screen objects are used, we can allocate a GMR to hold the
1142          * requested framebuffer.
1143          */
1144
1145         if (!vmw_kms_validate_mode_vram(dev_priv,
1146                                         mode_cmd.pitch,
1147                                         mode_cmd.height)) {
1148                 DRM_ERROR("VRAM size is too small for requested mode.\n");
1149                 return ERR_PTR(-ENOMEM);
1150         }
1151
1152         /*
1153          * Take a reference on the user object of the resource
1154          * backing the kms fb. This ensures that user-space handle
1155          * lookups on that resource will always work as long as
1156          * it's registered with a kms framebuffer. This is important,
1157          * since vmw_execbuf_process identifies resources in the
1158          * command stream using user-space handles.
1159          */
1160
1161         user_obj = ttm_base_object_lookup(tfile, mode_cmd.handle);
1162         if (unlikely(user_obj == NULL)) {
1163                 DRM_ERROR("Could not locate requested kms frame buffer.\n");
1164                 return ERR_PTR(-ENOENT);
1165         }
1166
1167         /**
1168          * End conditioned code.
1169          */
1170
1171         /* returns either a dmabuf or surface */
1172         ret = vmw_user_lookup_handle(dev_priv, tfile,
1173                                      mode_cmd.handle,
1174                                      &surface, &bo);
1175         if (ret)
1176                 goto err_out;
1177
1178         /* Create the new framebuffer depending one what we got back */
1179         if (bo)
1180                 ret = vmw_kms_new_framebuffer_dmabuf(dev_priv, bo, &vfb,
1181                                                      &mode_cmd);
1182         else if (surface)
1183                 ret = vmw_kms_new_framebuffer_surface(dev_priv, file_priv,
1184                                                       surface, &vfb, &mode_cmd);
1185         else
1186                 BUG();
1187
1188 err_out:
1189         /* vmw_user_lookup_handle takes one ref so does new_fb */
1190         if (bo)
1191                 vmw_dmabuf_unreference(&bo);
1192         if (surface)
1193                 vmw_surface_unreference(&surface);
1194
1195         if (ret) {
1196                 DRM_ERROR("failed to create vmw_framebuffer: %i\n", ret);
1197                 ttm_base_object_unref(&user_obj);
1198                 return ERR_PTR(ret);
1199         } else
1200                 vfb->user_obj = user_obj;
1201
1202         return &vfb->base;
1203 }
1204
1205 static const struct drm_mode_config_funcs vmw_kms_funcs = {
1206         .fb_create = vmw_kms_fb_create,
1207 };
1208
1209 int vmw_kms_present(struct vmw_private *dev_priv,
1210                     struct drm_file *file_priv,
1211                     struct vmw_framebuffer *vfb,
1212                     struct vmw_surface *surface,
1213                     uint32_t sid,
1214                     int32_t destX, int32_t destY,
1215                     struct drm_vmw_rect *clips,
1216                     uint32_t num_clips)
1217 {
1218         struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
1219         struct drm_clip_rect *tmp;
1220         struct drm_crtc *crtc;
1221         size_t fifo_size;
1222         int i, k, num_units;
1223         int ret = 0; /* silence warning */
1224         int left, right, top, bottom;
1225
1226         struct {
1227                 SVGA3dCmdHeader header;
1228                 SVGA3dCmdBlitSurfaceToScreen body;
1229         } *cmd;
1230         SVGASignedRect *blits;
1231
1232         num_units = 0;
1233         list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list, head) {
1234                 if (crtc->fb != &vfb->base)
1235                         continue;
1236                 units[num_units++] = vmw_crtc_to_du(crtc);
1237         }
1238
1239         BUG_ON(surface == NULL);
1240         BUG_ON(!clips || !num_clips);
1241
1242         tmp = kzalloc(sizeof(*tmp) * num_clips, GFP_KERNEL);
1243         if (unlikely(tmp == NULL)) {
1244                 DRM_ERROR("Temporary cliprect memory alloc failed.\n");
1245                 return -ENOMEM;
1246         }
1247
1248         fifo_size = sizeof(*cmd) + sizeof(SVGASignedRect) * num_clips;
1249         cmd = kmalloc(fifo_size, GFP_KERNEL);
1250         if (unlikely(cmd == NULL)) {
1251                 DRM_ERROR("Failed to allocate temporary fifo memory.\n");
1252                 ret = -ENOMEM;
1253                 goto out_free_tmp;
1254         }
1255
1256         left = clips->x;
1257         right = clips->x + clips->w;
1258         top = clips->y;
1259         bottom = clips->y + clips->h;
1260
1261         for (i = 1; i < num_clips; i++) {
1262                 left = min_t(int, left, (int)clips[i].x);
1263                 right = max_t(int, right, (int)clips[i].x + clips[i].w);
1264                 top = min_t(int, top, (int)clips[i].y);
1265                 bottom = max_t(int, bottom, (int)clips[i].y + clips[i].h);
1266         }
1267
1268         /* only need to do this once */
1269         memset(cmd, 0, fifo_size);
1270         cmd->header.id = cpu_to_le32(SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN);
1271
1272         blits = (SVGASignedRect *)&cmd[1];
1273
1274         cmd->body.srcRect.left = left;
1275         cmd->body.srcRect.right = right;
1276         cmd->body.srcRect.top = top;
1277         cmd->body.srcRect.bottom = bottom;
1278
1279         for (i = 0; i < num_clips; i++) {
1280                 tmp[i].x1 = clips[i].x - left;
1281                 tmp[i].x2 = clips[i].x + clips[i].w - left;
1282                 tmp[i].y1 = clips[i].y - top;
1283                 tmp[i].y2 = clips[i].y + clips[i].h - top;
1284         }
1285
1286         for (k = 0; k < num_units; k++) {
1287                 struct vmw_display_unit *unit = units[k];
1288                 struct vmw_clip_rect clip;
1289                 int num;
1290
1291                 clip.x1 = left + destX - unit->crtc.x;
1292                 clip.y1 = top + destY - unit->crtc.y;
1293                 clip.x2 = right + destX - unit->crtc.x;
1294                 clip.y2 = bottom + destY - unit->crtc.y;
1295
1296                 /* skip any crtcs that misses the clip region */
1297                 if (clip.x1 >= unit->crtc.mode.hdisplay ||
1298                     clip.y1 >= unit->crtc.mode.vdisplay ||
1299                     clip.x2 <= 0 || clip.y2 <= 0)
1300                         continue;
1301
1302                 /*
1303                  * In order for the clip rects to be correctly scaled
1304                  * the src and dest rects needs to be the same size.
1305                  */
1306                 cmd->body.destRect.left = clip.x1;
1307                 cmd->body.destRect.right = clip.x2;
1308                 cmd->body.destRect.top = clip.y1;
1309                 cmd->body.destRect.bottom = clip.y2;
1310
1311                 /* create a clip rect of the crtc in dest coords */
1312                 clip.x2 = unit->crtc.mode.hdisplay - clip.x1;
1313                 clip.y2 = unit->crtc.mode.vdisplay - clip.y1;
1314                 clip.x1 = 0 - clip.x1;
1315                 clip.y1 = 0 - clip.y1;
1316
1317                 /* need to reset sid as it is changed by execbuf */
1318                 cmd->body.srcImage.sid = sid;
1319                 cmd->body.destScreenId = unit->unit;
1320
1321                 /* clip and write blits to cmd stream */
1322                 vmw_clip_cliprects(tmp, num_clips, clip, blits, &num);
1323
1324                 /* if no cliprects hit skip this */
1325                 if (num == 0)
1326                         continue;
1327
1328                 /* recalculate package length */
1329                 fifo_size = sizeof(*cmd) + sizeof(SVGASignedRect) * num;
1330                 cmd->header.size = cpu_to_le32(fifo_size - sizeof(cmd->header));
1331                 ret = vmw_execbuf_process(file_priv, dev_priv, NULL, cmd,
1332                                           fifo_size, 0, NULL, NULL);
1333
1334                 if (unlikely(ret != 0))
1335                         break;
1336         }
1337
1338         kfree(cmd);
1339 out_free_tmp:
1340         kfree(tmp);
1341
1342         return ret;
1343 }
1344
1345 int vmw_kms_readback(struct vmw_private *dev_priv,
1346                      struct drm_file *file_priv,
1347                      struct vmw_framebuffer *vfb,
1348                      struct drm_vmw_fence_rep __user *user_fence_rep,
1349                      struct drm_vmw_rect *clips,
1350                      uint32_t num_clips)
1351 {
1352         struct vmw_framebuffer_dmabuf *vfbd =
1353                 vmw_framebuffer_to_vfbd(&vfb->base);
1354         struct vmw_dma_buffer *dmabuf = vfbd->buffer;
1355         struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
1356         struct drm_crtc *crtc;
1357         size_t fifo_size;
1358         int i, k, ret, num_units, blits_pos;
1359
1360         struct {
1361                 uint32_t header;
1362                 SVGAFifoCmdDefineGMRFB body;
1363         } *cmd;
1364         struct {
1365                 uint32_t header;
1366                 SVGAFifoCmdBlitScreenToGMRFB body;
1367         } *blits;
1368
1369         num_units = 0;
1370         list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list, head) {
1371                 if (crtc->fb != &vfb->base)
1372                         continue;
1373                 units[num_units++] = vmw_crtc_to_du(crtc);
1374         }
1375
1376         BUG_ON(dmabuf == NULL);
1377         BUG_ON(!clips || !num_clips);
1378
1379         /* take a safe guess at fifo size */
1380         fifo_size = sizeof(*cmd) + sizeof(*blits) * num_clips * num_units;
1381         cmd = kmalloc(fifo_size, GFP_KERNEL);
1382         if (unlikely(cmd == NULL)) {
1383                 DRM_ERROR("Failed to allocate temporary fifo memory.\n");
1384                 return -ENOMEM;
1385         }
1386
1387         memset(cmd, 0, fifo_size);
1388         cmd->header = SVGA_CMD_DEFINE_GMRFB;
1389         cmd->body.format.bitsPerPixel = vfb->base.bits_per_pixel;
1390         cmd->body.format.colorDepth = vfb->base.depth;
1391         cmd->body.format.reserved = 0;
1392         cmd->body.bytesPerLine = vfb->base.pitches[0];
1393         cmd->body.ptr.gmrId = vfb->user_handle;
1394         cmd->body.ptr.offset = 0;
1395
1396         blits = (void *)&cmd[1];
1397         blits_pos = 0;
1398         for (i = 0; i < num_units; i++) {
1399                 struct drm_vmw_rect *c = clips;
1400                 for (k = 0; k < num_clips; k++, c++) {
1401                         /* transform clip coords to crtc origin based coords */
1402                         int clip_x1 = c->x - units[i]->crtc.x;
1403                         int clip_x2 = c->x - units[i]->crtc.x + c->w;
1404                         int clip_y1 = c->y - units[i]->crtc.y;
1405                         int clip_y2 = c->y - units[i]->crtc.y + c->h;
1406                         int dest_x = c->x;
1407                         int dest_y = c->y;
1408
1409                         /* compensate for clipping, we negate
1410                          * a negative number and add that.
1411                          */
1412                         if (clip_x1 < 0)
1413                                 dest_x += -clip_x1;
1414                         if (clip_y1 < 0)
1415                                 dest_y += -clip_y1;
1416
1417                         /* clip */
1418                         clip_x1 = max(clip_x1, 0);
1419                         clip_y1 = max(clip_y1, 0);
1420                         clip_x2 = min(clip_x2, units[i]->crtc.mode.hdisplay);
1421                         clip_y2 = min(clip_y2, units[i]->crtc.mode.vdisplay);
1422
1423                         /* and cull any rects that misses the crtc */
1424                         if (clip_x1 >= units[i]->crtc.mode.hdisplay ||
1425                             clip_y1 >= units[i]->crtc.mode.vdisplay ||
1426                             clip_x2 <= 0 || clip_y2 <= 0)
1427                                 continue;
1428
1429                         blits[blits_pos].header = SVGA_CMD_BLIT_SCREEN_TO_GMRFB;
1430                         blits[blits_pos].body.srcScreenId = units[i]->unit;
1431                         blits[blits_pos].body.destOrigin.x = dest_x;
1432                         blits[blits_pos].body.destOrigin.y = dest_y;
1433
1434                         blits[blits_pos].body.srcRect.left = clip_x1;
1435                         blits[blits_pos].body.srcRect.top = clip_y1;
1436                         blits[blits_pos].body.srcRect.right = clip_x2;
1437                         blits[blits_pos].body.srcRect.bottom = clip_y2;
1438                         blits_pos++;
1439                 }
1440         }
1441         /* reset size here and use calculated exact size from loops */
1442         fifo_size = sizeof(*cmd) + sizeof(*blits) * blits_pos;
1443
1444         ret = vmw_execbuf_process(file_priv, dev_priv, NULL, cmd, fifo_size,
1445                                   0, user_fence_rep, NULL);
1446
1447         kfree(cmd);
1448
1449         return ret;
1450 }
1451
1452 int vmw_kms_init(struct vmw_private *dev_priv)
1453 {
1454         struct drm_device *dev = dev_priv->dev;
1455         int ret;
1456
1457         drm_mode_config_init(dev);
1458         dev->mode_config.funcs = &vmw_kms_funcs;
1459         dev->mode_config.min_width = 1;
1460         dev->mode_config.min_height = 1;
1461         /* assumed largest fb size */
1462         dev->mode_config.max_width = 8192;
1463         dev->mode_config.max_height = 8192;
1464
1465         ret = vmw_kms_init_screen_object_display(dev_priv);
1466         if (ret) /* Fallback */
1467                 (void)vmw_kms_init_legacy_display_system(dev_priv);
1468
1469         return 0;
1470 }
1471
1472 int vmw_kms_close(struct vmw_private *dev_priv)
1473 {
1474         /*
1475          * Docs says we should take the lock before calling this function
1476          * but since it destroys encoders and our destructor calls
1477          * drm_encoder_cleanup which takes the lock we deadlock.
1478          */
1479         drm_mode_config_cleanup(dev_priv->dev);
1480         if (dev_priv->sou_priv)
1481                 vmw_kms_close_screen_object_display(dev_priv);
1482         else
1483                 vmw_kms_close_legacy_display_system(dev_priv);
1484         return 0;
1485 }
1486
1487 int vmw_kms_cursor_bypass_ioctl(struct drm_device *dev, void *data,
1488                                 struct drm_file *file_priv)
1489 {
1490         struct drm_vmw_cursor_bypass_arg *arg = data;
1491         struct vmw_display_unit *du;
1492         struct drm_mode_object *obj;
1493         struct drm_crtc *crtc;
1494         int ret = 0;
1495
1496
1497         mutex_lock(&dev->mode_config.mutex);
1498         if (arg->flags & DRM_VMW_CURSOR_BYPASS_ALL) {
1499
1500                 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1501                         du = vmw_crtc_to_du(crtc);
1502                         du->hotspot_x = arg->xhot;
1503                         du->hotspot_y = arg->yhot;
1504                 }
1505
1506                 mutex_unlock(&dev->mode_config.mutex);
1507                 return 0;
1508         }
1509
1510         obj = drm_mode_object_find(dev, arg->crtc_id, DRM_MODE_OBJECT_CRTC);
1511         if (!obj) {
1512                 ret = -ENOENT;
1513                 goto out;
1514         }
1515
1516         crtc = obj_to_crtc(obj);
1517         du = vmw_crtc_to_du(crtc);
1518
1519         du->hotspot_x = arg->xhot;
1520         du->hotspot_y = arg->yhot;
1521
1522 out:
1523         mutex_unlock(&dev->mode_config.mutex);
1524
1525         return ret;
1526 }
1527
1528 int vmw_kms_write_svga(struct vmw_private *vmw_priv,
1529                         unsigned width, unsigned height, unsigned pitch,
1530                         unsigned bpp, unsigned depth)
1531 {
1532         if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1533                 vmw_write(vmw_priv, SVGA_REG_PITCHLOCK, pitch);
1534         else if (vmw_fifo_have_pitchlock(vmw_priv))
1535                 iowrite32(pitch, vmw_priv->mmio_virt + SVGA_FIFO_PITCHLOCK);
1536         vmw_write(vmw_priv, SVGA_REG_WIDTH, width);
1537         vmw_write(vmw_priv, SVGA_REG_HEIGHT, height);
1538         vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, bpp);
1539
1540         if (vmw_read(vmw_priv, SVGA_REG_DEPTH) != depth) {
1541                 DRM_ERROR("Invalid depth %u for %u bpp, host expects %u\n",
1542                           depth, bpp, vmw_read(vmw_priv, SVGA_REG_DEPTH));
1543                 return -EINVAL;
1544         }
1545
1546         return 0;
1547 }
1548
1549 int vmw_kms_save_vga(struct vmw_private *vmw_priv)
1550 {
1551         struct vmw_vga_topology_state *save;
1552         uint32_t i;
1553
1554         vmw_priv->vga_width = vmw_read(vmw_priv, SVGA_REG_WIDTH);
1555         vmw_priv->vga_height = vmw_read(vmw_priv, SVGA_REG_HEIGHT);
1556         vmw_priv->vga_bpp = vmw_read(vmw_priv, SVGA_REG_BITS_PER_PIXEL);
1557         if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1558                 vmw_priv->vga_pitchlock =
1559                   vmw_read(vmw_priv, SVGA_REG_PITCHLOCK);
1560         else if (vmw_fifo_have_pitchlock(vmw_priv))
1561                 vmw_priv->vga_pitchlock = ioread32(vmw_priv->mmio_virt +
1562                                                        SVGA_FIFO_PITCHLOCK);
1563
1564         if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
1565                 return 0;
1566
1567         vmw_priv->num_displays = vmw_read(vmw_priv,
1568                                           SVGA_REG_NUM_GUEST_DISPLAYS);
1569
1570         if (vmw_priv->num_displays == 0)
1571                 vmw_priv->num_displays = 1;
1572
1573         for (i = 0; i < vmw_priv->num_displays; ++i) {
1574                 save = &vmw_priv->vga_save[i];
1575                 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
1576                 save->primary = vmw_read(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY);
1577                 save->pos_x = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_X);
1578                 save->pos_y = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y);
1579                 save->width = vmw_read(vmw_priv, SVGA_REG_DISPLAY_WIDTH);
1580                 save->height = vmw_read(vmw_priv, SVGA_REG_DISPLAY_HEIGHT);
1581                 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
1582                 if (i == 0 && vmw_priv->num_displays == 1 &&
1583                     save->width == 0 && save->height == 0) {
1584
1585                         /*
1586                          * It should be fairly safe to assume that these
1587                          * values are uninitialized.
1588                          */
1589
1590                         save->width = vmw_priv->vga_width - save->pos_x;
1591                         save->height = vmw_priv->vga_height - save->pos_y;
1592                 }
1593         }
1594
1595         return 0;
1596 }
1597
1598 int vmw_kms_restore_vga(struct vmw_private *vmw_priv)
1599 {
1600         struct vmw_vga_topology_state *save;
1601         uint32_t i;
1602
1603         vmw_write(vmw_priv, SVGA_REG_WIDTH, vmw_priv->vga_width);
1604         vmw_write(vmw_priv, SVGA_REG_HEIGHT, vmw_priv->vga_height);
1605         vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, vmw_priv->vga_bpp);
1606         if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1607                 vmw_write(vmw_priv, SVGA_REG_PITCHLOCK,
1608                           vmw_priv->vga_pitchlock);
1609         else if (vmw_fifo_have_pitchlock(vmw_priv))
1610                 iowrite32(vmw_priv->vga_pitchlock,
1611                           vmw_priv->mmio_virt + SVGA_FIFO_PITCHLOCK);
1612
1613         if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
1614                 return 0;
1615
1616         for (i = 0; i < vmw_priv->num_displays; ++i) {
1617                 save = &vmw_priv->vga_save[i];
1618                 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
1619                 vmw_write(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY, save->primary);
1620                 vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_X, save->pos_x);
1621                 vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y, save->pos_y);
1622                 vmw_write(vmw_priv, SVGA_REG_DISPLAY_WIDTH, save->width);
1623                 vmw_write(vmw_priv, SVGA_REG_DISPLAY_HEIGHT, save->height);
1624                 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
1625         }
1626
1627         return 0;
1628 }
1629
1630 bool vmw_kms_validate_mode_vram(struct vmw_private *dev_priv,
1631                                 uint32_t pitch,
1632                                 uint32_t height)
1633 {
1634         return ((u64) pitch * (u64) height) < (u64) dev_priv->vram_size;
1635 }
1636
1637
1638 /**
1639  * Function called by DRM code called with vbl_lock held.
1640  */
1641 u32 vmw_get_vblank_counter(struct drm_device *dev, int crtc)
1642 {
1643         return 0;
1644 }
1645
1646 /**
1647  * Function called by DRM code called with vbl_lock held.
1648  */
1649 int vmw_enable_vblank(struct drm_device *dev, int crtc)
1650 {
1651         return -ENOSYS;
1652 }
1653
1654 /**
1655  * Function called by DRM code called with vbl_lock held.
1656  */
1657 void vmw_disable_vblank(struct drm_device *dev, int crtc)
1658 {
1659 }
1660
1661
1662 /*
1663  * Small shared kms functions.
1664  */
1665
1666 int vmw_du_update_layout(struct vmw_private *dev_priv, unsigned num,
1667                          struct drm_vmw_rect *rects)
1668 {
1669         struct drm_device *dev = dev_priv->dev;
1670         struct vmw_display_unit *du;
1671         struct drm_connector *con;
1672
1673         mutex_lock(&dev->mode_config.mutex);
1674
1675 #if 0
1676         {
1677                 unsigned int i;
1678
1679                 DRM_INFO("%s: new layout ", __func__);
1680                 for (i = 0; i < num; i++)
1681                         DRM_INFO("(%i, %i %ux%u) ", rects[i].x, rects[i].y,
1682                                  rects[i].w, rects[i].h);
1683                 DRM_INFO("\n");
1684         }
1685 #endif
1686
1687         list_for_each_entry(con, &dev->mode_config.connector_list, head) {
1688                 du = vmw_connector_to_du(con);
1689                 if (num > du->unit) {
1690                         du->pref_width = rects[du->unit].w;
1691                         du->pref_height = rects[du->unit].h;
1692                         du->pref_active = true;
1693                         du->gui_x = rects[du->unit].x;
1694                         du->gui_y = rects[du->unit].y;
1695                 } else {
1696                         du->pref_width = 800;
1697                         du->pref_height = 600;
1698                         du->pref_active = false;
1699                 }
1700                 con->status = vmw_du_connector_detect(con, true);
1701         }
1702
1703         mutex_unlock(&dev->mode_config.mutex);
1704
1705         return 0;
1706 }
1707
1708 int vmw_du_page_flip(struct drm_crtc *crtc,
1709                      struct drm_framebuffer *fb,
1710                      struct drm_pending_vblank_event *event,
1711                      uint32_t page_flip_flags)
1712 {
1713         struct vmw_private *dev_priv = vmw_priv(crtc->dev);
1714         struct drm_framebuffer *old_fb = crtc->fb;
1715         struct vmw_framebuffer *vfb = vmw_framebuffer_to_vfb(fb);
1716         struct drm_file *file_priv ;
1717         struct vmw_fence_obj *fence = NULL;
1718         struct drm_clip_rect clips;
1719         int ret;
1720
1721         if (event == NULL)
1722                 return -EINVAL;
1723
1724         /* require ScreenObject support for page flipping */
1725         if (!dev_priv->sou_priv)
1726                 return -ENOSYS;
1727
1728         file_priv = event->base.file_priv;
1729         if (!vmw_kms_screen_object_flippable(dev_priv, crtc))
1730                 return -EINVAL;
1731
1732         crtc->fb = fb;
1733
1734         /* do a full screen dirty update */
1735         clips.x1 = clips.y1 = 0;
1736         clips.x2 = fb->width;
1737         clips.y2 = fb->height;
1738
1739         if (vfb->dmabuf)
1740                 ret = do_dmabuf_dirty_sou(file_priv, dev_priv, vfb,
1741                                           0, 0, &clips, 1, 1, &fence);
1742         else
1743                 ret = do_surface_dirty_sou(dev_priv, file_priv, vfb,
1744                                            0, 0, &clips, 1, 1, &fence);
1745
1746
1747         if (ret != 0)
1748                 goto out_no_fence;
1749         if (!fence) {
1750                 ret = -EINVAL;
1751                 goto out_no_fence;
1752         }
1753
1754         ret = vmw_event_fence_action_queue(file_priv, fence,
1755                                            &event->base,
1756                                            &event->event.tv_sec,
1757                                            &event->event.tv_usec,
1758                                            true);
1759
1760         /*
1761          * No need to hold on to this now. The only cleanup
1762          * we need to do if we fail is unref the fence.
1763          */
1764         vmw_fence_obj_unreference(&fence);
1765
1766         if (vmw_crtc_to_du(crtc)->is_implicit)
1767                 vmw_kms_screen_object_update_implicit_fb(dev_priv, crtc);
1768
1769         return ret;
1770
1771 out_no_fence:
1772         crtc->fb = old_fb;
1773         return ret;
1774 }
1775
1776
1777 void vmw_du_crtc_save(struct drm_crtc *crtc)
1778 {
1779 }
1780
1781 void vmw_du_crtc_restore(struct drm_crtc *crtc)
1782 {
1783 }
1784
1785 void vmw_du_crtc_gamma_set(struct drm_crtc *crtc,
1786                            u16 *r, u16 *g, u16 *b,
1787                            uint32_t start, uint32_t size)
1788 {
1789         struct vmw_private *dev_priv = vmw_priv(crtc->dev);
1790         int i;
1791
1792         for (i = 0; i < size; i++) {
1793                 DRM_DEBUG("%d r/g/b = 0x%04x / 0x%04x / 0x%04x\n", i,
1794                           r[i], g[i], b[i]);
1795                 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 0, r[i] >> 8);
1796                 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 1, g[i] >> 8);
1797                 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 2, b[i] >> 8);
1798         }
1799 }
1800
1801 void vmw_du_connector_dpms(struct drm_connector *connector, int mode)
1802 {
1803 }
1804
1805 void vmw_du_connector_save(struct drm_connector *connector)
1806 {
1807 }
1808
1809 void vmw_du_connector_restore(struct drm_connector *connector)
1810 {
1811 }
1812
1813 enum drm_connector_status
1814 vmw_du_connector_detect(struct drm_connector *connector, bool force)
1815 {
1816         uint32_t num_displays;
1817         struct drm_device *dev = connector->dev;
1818         struct vmw_private *dev_priv = vmw_priv(dev);
1819         struct vmw_display_unit *du = vmw_connector_to_du(connector);
1820
1821         mutex_lock(&dev_priv->hw_mutex);
1822         num_displays = vmw_read(dev_priv, SVGA_REG_NUM_DISPLAYS);
1823         mutex_unlock(&dev_priv->hw_mutex);
1824
1825         return ((vmw_connector_to_du(connector)->unit < num_displays &&
1826                  du->pref_active) ?
1827                 connector_status_connected : connector_status_disconnected);
1828 }
1829
1830 static struct drm_display_mode vmw_kms_connector_builtin[] = {
1831         /* 640x480@60Hz */
1832         { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 25175, 640, 656,
1833                    752, 800, 0, 480, 489, 492, 525, 0,
1834                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
1835         /* 800x600@60Hz */
1836         { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 40000, 800, 840,
1837                    968, 1056, 0, 600, 601, 605, 628, 0,
1838                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1839         /* 1024x768@60Hz */
1840         { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 65000, 1024, 1048,
1841                    1184, 1344, 0, 768, 771, 777, 806, 0,
1842                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
1843         /* 1152x864@75Hz */
1844         { DRM_MODE("1152x864", DRM_MODE_TYPE_DRIVER, 108000, 1152, 1216,
1845                    1344, 1600, 0, 864, 865, 868, 900, 0,
1846                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1847         /* 1280x768@60Hz */
1848         { DRM_MODE("1280x768", DRM_MODE_TYPE_DRIVER, 79500, 1280, 1344,
1849                    1472, 1664, 0, 768, 771, 778, 798, 0,
1850                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1851         /* 1280x800@60Hz */
1852         { DRM_MODE("1280x800", DRM_MODE_TYPE_DRIVER, 83500, 1280, 1352,
1853                    1480, 1680, 0, 800, 803, 809, 831, 0,
1854                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
1855         /* 1280x960@60Hz */
1856         { DRM_MODE("1280x960", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1376,
1857                    1488, 1800, 0, 960, 961, 964, 1000, 0,
1858                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1859         /* 1280x1024@60Hz */
1860         { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1328,
1861                    1440, 1688, 0, 1024, 1025, 1028, 1066, 0,
1862                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1863         /* 1360x768@60Hz */
1864         { DRM_MODE("1360x768", DRM_MODE_TYPE_DRIVER, 85500, 1360, 1424,
1865                    1536, 1792, 0, 768, 771, 777, 795, 0,
1866                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1867         /* 1440x1050@60Hz */
1868         { DRM_MODE("1400x1050", DRM_MODE_TYPE_DRIVER, 121750, 1400, 1488,
1869                    1632, 1864, 0, 1050, 1053, 1057, 1089, 0,
1870                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1871         /* 1440x900@60Hz */
1872         { DRM_MODE("1440x900", DRM_MODE_TYPE_DRIVER, 106500, 1440, 1520,
1873                    1672, 1904, 0, 900, 903, 909, 934, 0,
1874                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1875         /* 1600x1200@60Hz */
1876         { DRM_MODE("1600x1200", DRM_MODE_TYPE_DRIVER, 162000, 1600, 1664,
1877                    1856, 2160, 0, 1200, 1201, 1204, 1250, 0,
1878                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1879         /* 1680x1050@60Hz */
1880         { DRM_MODE("1680x1050", DRM_MODE_TYPE_DRIVER, 146250, 1680, 1784,
1881                    1960, 2240, 0, 1050, 1053, 1059, 1089, 0,
1882                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1883         /* 1792x1344@60Hz */
1884         { DRM_MODE("1792x1344", DRM_MODE_TYPE_DRIVER, 204750, 1792, 1920,
1885                    2120, 2448, 0, 1344, 1345, 1348, 1394, 0,
1886                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1887         /* 1853x1392@60Hz */
1888         { DRM_MODE("1856x1392", DRM_MODE_TYPE_DRIVER, 218250, 1856, 1952,
1889                    2176, 2528, 0, 1392, 1393, 1396, 1439, 0,
1890                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1891         /* 1920x1200@60Hz */
1892         { DRM_MODE("1920x1200", DRM_MODE_TYPE_DRIVER, 193250, 1920, 2056,
1893                    2256, 2592, 0, 1200, 1203, 1209, 1245, 0,
1894                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1895         /* 1920x1440@60Hz */
1896         { DRM_MODE("1920x1440", DRM_MODE_TYPE_DRIVER, 234000, 1920, 2048,
1897                    2256, 2600, 0, 1440, 1441, 1444, 1500, 0,
1898                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1899         /* 2560x1600@60Hz */
1900         { DRM_MODE("2560x1600", DRM_MODE_TYPE_DRIVER, 348500, 2560, 2752,
1901                    3032, 3504, 0, 1600, 1603, 1609, 1658, 0,
1902                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1903         /* Terminate */
1904         { DRM_MODE("", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) },
1905 };
1906
1907 /**
1908  * vmw_guess_mode_timing - Provide fake timings for a
1909  * 60Hz vrefresh mode.
1910  *
1911  * @mode - Pointer to a struct drm_display_mode with hdisplay and vdisplay
1912  * members filled in.
1913  */
1914 static void vmw_guess_mode_timing(struct drm_display_mode *mode)
1915 {
1916         mode->hsync_start = mode->hdisplay + 50;
1917         mode->hsync_end = mode->hsync_start + 50;
1918         mode->htotal = mode->hsync_end + 50;
1919
1920         mode->vsync_start = mode->vdisplay + 50;
1921         mode->vsync_end = mode->vsync_start + 50;
1922         mode->vtotal = mode->vsync_end + 50;
1923
1924         mode->clock = (u32)mode->htotal * (u32)mode->vtotal / 100 * 6;
1925         mode->vrefresh = drm_mode_vrefresh(mode);
1926 }
1927
1928
1929 int vmw_du_connector_fill_modes(struct drm_connector *connector,
1930                                 uint32_t max_width, uint32_t max_height)
1931 {
1932         struct vmw_display_unit *du = vmw_connector_to_du(connector);
1933         struct drm_device *dev = connector->dev;
1934         struct vmw_private *dev_priv = vmw_priv(dev);
1935         struct drm_display_mode *mode = NULL;
1936         struct drm_display_mode *bmode;
1937         struct drm_display_mode prefmode = { DRM_MODE("preferred",
1938                 DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED,
1939                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1940                 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC)
1941         };
1942         int i;
1943
1944         /* Add preferred mode */
1945         {
1946                 mode = drm_mode_duplicate(dev, &prefmode);
1947                 if (!mode)
1948                         return 0;
1949                 mode->hdisplay = du->pref_width;
1950                 mode->vdisplay = du->pref_height;
1951                 vmw_guess_mode_timing(mode);
1952
1953                 if (vmw_kms_validate_mode_vram(dev_priv, mode->hdisplay * 2,
1954                                                mode->vdisplay)) {
1955                         drm_mode_probed_add(connector, mode);
1956                 } else {
1957                         drm_mode_destroy(dev, mode);
1958                         mode = NULL;
1959                 }
1960
1961                 if (du->pref_mode) {
1962                         list_del_init(&du->pref_mode->head);
1963                         drm_mode_destroy(dev, du->pref_mode);
1964                 }
1965
1966                 /* mode might be null here, this is intended */
1967                 du->pref_mode = mode;
1968         }
1969
1970         for (i = 0; vmw_kms_connector_builtin[i].type != 0; i++) {
1971                 bmode = &vmw_kms_connector_builtin[i];
1972                 if (bmode->hdisplay > max_width ||
1973                     bmode->vdisplay > max_height)
1974                         continue;
1975
1976                 if (!vmw_kms_validate_mode_vram(dev_priv, bmode->hdisplay * 2,
1977                                                 bmode->vdisplay))
1978                         continue;
1979
1980                 mode = drm_mode_duplicate(dev, bmode);
1981                 if (!mode)
1982                         return 0;
1983                 mode->vrefresh = drm_mode_vrefresh(mode);
1984
1985                 drm_mode_probed_add(connector, mode);
1986         }
1987
1988         /* Move the prefered mode first, help apps pick the right mode. */
1989         if (du->pref_mode)
1990                 list_move(&du->pref_mode->head, &connector->probed_modes);
1991
1992         drm_mode_connector_list_update(connector);
1993
1994         return 1;
1995 }
1996
1997 int vmw_du_connector_set_property(struct drm_connector *connector,
1998                                   struct drm_property *property,
1999                                   uint64_t val)
2000 {
2001         return 0;
2002 }
2003
2004
2005 int vmw_kms_update_layout_ioctl(struct drm_device *dev, void *data,
2006                                 struct drm_file *file_priv)
2007 {
2008         struct vmw_private *dev_priv = vmw_priv(dev);
2009         struct drm_vmw_update_layout_arg *arg =
2010                 (struct drm_vmw_update_layout_arg *)data;
2011         struct vmw_master *vmaster = vmw_master(file_priv->master);
2012         void __user *user_rects;
2013         struct drm_vmw_rect *rects;
2014         unsigned rects_size;
2015         int ret;
2016         int i;
2017         struct drm_mode_config *mode_config = &dev->mode_config;
2018
2019         ret = ttm_read_lock(&vmaster->lock, true);
2020         if (unlikely(ret != 0))
2021                 return ret;
2022
2023         if (!arg->num_outputs) {
2024                 struct drm_vmw_rect def_rect = {0, 0, 800, 600};
2025                 vmw_du_update_layout(dev_priv, 1, &def_rect);
2026                 goto out_unlock;
2027         }
2028
2029         rects_size = arg->num_outputs * sizeof(struct drm_vmw_rect);
2030         rects = kcalloc(arg->num_outputs, sizeof(struct drm_vmw_rect),
2031                         GFP_KERNEL);
2032         if (unlikely(!rects)) {
2033                 ret = -ENOMEM;
2034                 goto out_unlock;
2035         }
2036
2037         user_rects = (void __user *)(unsigned long)arg->rects;
2038         ret = copy_from_user(rects, user_rects, rects_size);
2039         if (unlikely(ret != 0)) {
2040                 DRM_ERROR("Failed to get rects.\n");
2041                 ret = -EFAULT;
2042                 goto out_free;
2043         }
2044
2045         for (i = 0; i < arg->num_outputs; ++i) {
2046                 if (rects[i].x < 0 ||
2047                     rects[i].y < 0 ||
2048                     rects[i].x + rects[i].w > mode_config->max_width ||
2049                     rects[i].y + rects[i].h > mode_config->max_height) {
2050                         DRM_ERROR("Invalid GUI layout.\n");
2051                         ret = -EINVAL;
2052                         goto out_free;
2053                 }
2054         }
2055
2056         vmw_du_update_layout(dev_priv, arg->num_outputs, rects);
2057
2058 out_free:
2059         kfree(rects);
2060 out_unlock:
2061         ttm_read_unlock(&vmaster->lock);
2062         return ret;
2063 }