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