Merge tag 'drm-misc-next-2018-02-13' of git://anongit.freedesktop.org/drm/drm-misc...
[linux-2.6-microblaze.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 #include <drm/drm_plane_helper.h>
30 #include <drm/drm_atomic.h>
31 #include <drm/drm_atomic_helper.h>
32 #include <drm/drm_rect.h>
33
34
35 /* Might need a hrtimer here? */
36 #define VMWGFX_PRESENT_RATE ((HZ / 60 > 0) ? HZ / 60 : 1)
37
38 void vmw_du_cleanup(struct vmw_display_unit *du)
39 {
40         drm_plane_cleanup(&du->primary);
41         drm_plane_cleanup(&du->cursor);
42
43         drm_connector_unregister(&du->connector);
44         drm_crtc_cleanup(&du->crtc);
45         drm_encoder_cleanup(&du->encoder);
46         drm_connector_cleanup(&du->connector);
47 }
48
49 /*
50  * Display Unit Cursor functions
51  */
52
53 static int vmw_cursor_update_image(struct vmw_private *dev_priv,
54                                    u32 *image, u32 width, u32 height,
55                                    u32 hotspotX, u32 hotspotY)
56 {
57         struct {
58                 u32 cmd;
59                 SVGAFifoCmdDefineAlphaCursor cursor;
60         } *cmd;
61         u32 image_size = width * height * 4;
62         u32 cmd_size = sizeof(*cmd) + image_size;
63
64         if (!image)
65                 return -EINVAL;
66
67         cmd = vmw_fifo_reserve(dev_priv, cmd_size);
68         if (unlikely(cmd == NULL)) {
69                 DRM_ERROR("Fifo reserve failed.\n");
70                 return -ENOMEM;
71         }
72
73         memset(cmd, 0, sizeof(*cmd));
74
75         memcpy(&cmd[1], image, image_size);
76
77         cmd->cmd = SVGA_CMD_DEFINE_ALPHA_CURSOR;
78         cmd->cursor.id = 0;
79         cmd->cursor.width = width;
80         cmd->cursor.height = height;
81         cmd->cursor.hotspotX = hotspotX;
82         cmd->cursor.hotspotY = hotspotY;
83
84         vmw_fifo_commit_flush(dev_priv, cmd_size);
85
86         return 0;
87 }
88
89 static int vmw_cursor_update_dmabuf(struct vmw_private *dev_priv,
90                                     struct vmw_dma_buffer *dmabuf,
91                                     u32 width, u32 height,
92                                     u32 hotspotX, u32 hotspotY)
93 {
94         struct ttm_bo_kmap_obj map;
95         unsigned long kmap_offset;
96         unsigned long kmap_num;
97         void *virtual;
98         bool dummy;
99         int ret;
100
101         kmap_offset = 0;
102         kmap_num = (width*height*4 + PAGE_SIZE - 1) >> PAGE_SHIFT;
103
104         ret = ttm_bo_reserve(&dmabuf->base, true, false, NULL);
105         if (unlikely(ret != 0)) {
106                 DRM_ERROR("reserve failed\n");
107                 return -EINVAL;
108         }
109
110         ret = ttm_bo_kmap(&dmabuf->base, kmap_offset, kmap_num, &map);
111         if (unlikely(ret != 0))
112                 goto err_unreserve;
113
114         virtual = ttm_kmap_obj_virtual(&map, &dummy);
115         ret = vmw_cursor_update_image(dev_priv, virtual, width, height,
116                                       hotspotX, hotspotY);
117
118         ttm_bo_kunmap(&map);
119 err_unreserve:
120         ttm_bo_unreserve(&dmabuf->base);
121
122         return ret;
123 }
124
125
126 static void vmw_cursor_update_position(struct vmw_private *dev_priv,
127                                        bool show, int x, int y)
128 {
129         u32 *fifo_mem = dev_priv->mmio_virt;
130         uint32_t count;
131
132         spin_lock(&dev_priv->cursor_lock);
133         vmw_mmio_write(show ? 1 : 0, fifo_mem + SVGA_FIFO_CURSOR_ON);
134         vmw_mmio_write(x, fifo_mem + SVGA_FIFO_CURSOR_X);
135         vmw_mmio_write(y, fifo_mem + SVGA_FIFO_CURSOR_Y);
136         count = vmw_mmio_read(fifo_mem + SVGA_FIFO_CURSOR_COUNT);
137         vmw_mmio_write(++count, fifo_mem + SVGA_FIFO_CURSOR_COUNT);
138         spin_unlock(&dev_priv->cursor_lock);
139 }
140
141
142 void vmw_kms_cursor_snoop(struct vmw_surface *srf,
143                           struct ttm_object_file *tfile,
144                           struct ttm_buffer_object *bo,
145                           SVGA3dCmdHeader *header)
146 {
147         struct ttm_bo_kmap_obj map;
148         unsigned long kmap_offset;
149         unsigned long kmap_num;
150         SVGA3dCopyBox *box;
151         unsigned box_count;
152         void *virtual;
153         bool dummy;
154         struct vmw_dma_cmd {
155                 SVGA3dCmdHeader header;
156                 SVGA3dCmdSurfaceDMA dma;
157         } *cmd;
158         int i, ret;
159
160         cmd = container_of(header, struct vmw_dma_cmd, header);
161
162         /* No snooper installed */
163         if (!srf->snooper.image)
164                 return;
165
166         if (cmd->dma.host.face != 0 || cmd->dma.host.mipmap != 0) {
167                 DRM_ERROR("face and mipmap for cursors should never != 0\n");
168                 return;
169         }
170
171         if (cmd->header.size < 64) {
172                 DRM_ERROR("at least one full copy box must be given\n");
173                 return;
174         }
175
176         box = (SVGA3dCopyBox *)&cmd[1];
177         box_count = (cmd->header.size - sizeof(SVGA3dCmdSurfaceDMA)) /
178                         sizeof(SVGA3dCopyBox);
179
180         if (cmd->dma.guest.ptr.offset % PAGE_SIZE ||
181             box->x != 0    || box->y != 0    || box->z != 0    ||
182             box->srcx != 0 || box->srcy != 0 || box->srcz != 0 ||
183             box->d != 1    || box_count != 1) {
184                 /* TODO handle none page aligned offsets */
185                 /* TODO handle more dst & src != 0 */
186                 /* TODO handle more then one copy */
187                 DRM_ERROR("Cant snoop dma request for cursor!\n");
188                 DRM_ERROR("(%u, %u, %u) (%u, %u, %u) (%ux%ux%u) %u %u\n",
189                           box->srcx, box->srcy, box->srcz,
190                           box->x, box->y, box->z,
191                           box->w, box->h, box->d, box_count,
192                           cmd->dma.guest.ptr.offset);
193                 return;
194         }
195
196         kmap_offset = cmd->dma.guest.ptr.offset >> PAGE_SHIFT;
197         kmap_num = (64*64*4) >> PAGE_SHIFT;
198
199         ret = ttm_bo_reserve(bo, true, false, NULL);
200         if (unlikely(ret != 0)) {
201                 DRM_ERROR("reserve failed\n");
202                 return;
203         }
204
205         ret = ttm_bo_kmap(bo, kmap_offset, kmap_num, &map);
206         if (unlikely(ret != 0))
207                 goto err_unreserve;
208
209         virtual = ttm_kmap_obj_virtual(&map, &dummy);
210
211         if (box->w == 64 && cmd->dma.guest.pitch == 64*4) {
212                 memcpy(srf->snooper.image, virtual, 64*64*4);
213         } else {
214                 /* Image is unsigned pointer. */
215                 for (i = 0; i < box->h; i++)
216                         memcpy(srf->snooper.image + i * 64,
217                                virtual + i * cmd->dma.guest.pitch,
218                                box->w * 4);
219         }
220
221         srf->snooper.age++;
222
223         ttm_bo_kunmap(&map);
224 err_unreserve:
225         ttm_bo_unreserve(bo);
226 }
227
228 /**
229  * vmw_kms_legacy_hotspot_clear - Clear legacy hotspots
230  *
231  * @dev_priv: Pointer to the device private struct.
232  *
233  * Clears all legacy hotspots.
234  */
235 void vmw_kms_legacy_hotspot_clear(struct vmw_private *dev_priv)
236 {
237         struct drm_device *dev = dev_priv->dev;
238         struct vmw_display_unit *du;
239         struct drm_crtc *crtc;
240
241         drm_modeset_lock_all(dev);
242         drm_for_each_crtc(crtc, dev) {
243                 du = vmw_crtc_to_du(crtc);
244
245                 du->hotspot_x = 0;
246                 du->hotspot_y = 0;
247         }
248         drm_modeset_unlock_all(dev);
249 }
250
251 void vmw_kms_cursor_post_execbuf(struct vmw_private *dev_priv)
252 {
253         struct drm_device *dev = dev_priv->dev;
254         struct vmw_display_unit *du;
255         struct drm_crtc *crtc;
256
257         mutex_lock(&dev->mode_config.mutex);
258
259         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
260                 du = vmw_crtc_to_du(crtc);
261                 if (!du->cursor_surface ||
262                     du->cursor_age == du->cursor_surface->snooper.age)
263                         continue;
264
265                 du->cursor_age = du->cursor_surface->snooper.age;
266                 vmw_cursor_update_image(dev_priv,
267                                         du->cursor_surface->snooper.image,
268                                         64, 64,
269                                         du->hotspot_x + du->core_hotspot_x,
270                                         du->hotspot_y + du->core_hotspot_y);
271         }
272
273         mutex_unlock(&dev->mode_config.mutex);
274 }
275
276
277 void vmw_du_cursor_plane_destroy(struct drm_plane *plane)
278 {
279         vmw_cursor_update_position(plane->dev->dev_private, false, 0, 0);
280
281         drm_plane_cleanup(plane);
282 }
283
284
285 void vmw_du_primary_plane_destroy(struct drm_plane *plane)
286 {
287         drm_plane_cleanup(plane);
288
289         /* Planes are static in our case so we don't free it */
290 }
291
292
293 /**
294  * vmw_du_vps_unpin_surf - unpins resource associated with a framebuffer surface
295  *
296  * @vps: plane state associated with the display surface
297  * @unreference: true if we also want to unreference the display.
298  */
299 void vmw_du_plane_unpin_surf(struct vmw_plane_state *vps,
300                              bool unreference)
301 {
302         if (vps->surf) {
303                 if (vps->pinned) {
304                         vmw_resource_unpin(&vps->surf->res);
305                         vps->pinned--;
306                 }
307
308                 if (unreference) {
309                         if (vps->pinned)
310                                 DRM_ERROR("Surface still pinned\n");
311                         vmw_surface_unreference(&vps->surf);
312                 }
313         }
314 }
315
316
317 /**
318  * vmw_du_plane_cleanup_fb - Unpins the cursor
319  *
320  * @plane:  display plane
321  * @old_state: Contains the FB to clean up
322  *
323  * Unpins the framebuffer surface
324  *
325  * Returns 0 on success
326  */
327 void
328 vmw_du_plane_cleanup_fb(struct drm_plane *plane,
329                         struct drm_plane_state *old_state)
330 {
331         struct vmw_plane_state *vps = vmw_plane_state_to_vps(old_state);
332
333         vmw_du_plane_unpin_surf(vps, false);
334 }
335
336
337 /**
338  * vmw_du_cursor_plane_prepare_fb - Readies the cursor by referencing it
339  *
340  * @plane:  display plane
341  * @new_state: info on the new plane state, including the FB
342  *
343  * Returns 0 on success
344  */
345 int
346 vmw_du_cursor_plane_prepare_fb(struct drm_plane *plane,
347                                struct drm_plane_state *new_state)
348 {
349         struct drm_framebuffer *fb = new_state->fb;
350         struct vmw_plane_state *vps = vmw_plane_state_to_vps(new_state);
351
352
353         if (vps->surf)
354                 vmw_surface_unreference(&vps->surf);
355
356         if (vps->dmabuf)
357                 vmw_dmabuf_unreference(&vps->dmabuf);
358
359         if (fb) {
360                 if (vmw_framebuffer_to_vfb(fb)->dmabuf) {
361                         vps->dmabuf = vmw_framebuffer_to_vfbd(fb)->buffer;
362                         vmw_dmabuf_reference(vps->dmabuf);
363                 } else {
364                         vps->surf = vmw_framebuffer_to_vfbs(fb)->surface;
365                         vmw_surface_reference(vps->surf);
366                 }
367         }
368
369         return 0;
370 }
371
372
373 void
374 vmw_du_cursor_plane_atomic_update(struct drm_plane *plane,
375                                   struct drm_plane_state *old_state)
376 {
377         struct drm_crtc *crtc = plane->state->crtc ?: old_state->crtc;
378         struct vmw_private *dev_priv = vmw_priv(crtc->dev);
379         struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
380         struct vmw_plane_state *vps = vmw_plane_state_to_vps(plane->state);
381         s32 hotspot_x, hotspot_y;
382         int ret = 0;
383
384
385         hotspot_x = du->hotspot_x;
386         hotspot_y = du->hotspot_y;
387
388         if (plane->fb) {
389                 hotspot_x += plane->fb->hot_x;
390                 hotspot_y += plane->fb->hot_y;
391         }
392
393         du->cursor_surface = vps->surf;
394         du->cursor_dmabuf = vps->dmabuf;
395
396         /* setup new image */
397         if (vps->surf) {
398                 du->cursor_age = du->cursor_surface->snooper.age;
399
400                 ret = vmw_cursor_update_image(dev_priv,
401                                               vps->surf->snooper.image,
402                                               64, 64, hotspot_x, hotspot_y);
403         } else if (vps->dmabuf) {
404                 ret = vmw_cursor_update_dmabuf(dev_priv, vps->dmabuf,
405                                                plane->state->crtc_w,
406                                                plane->state->crtc_h,
407                                                hotspot_x, hotspot_y);
408         } else {
409                 vmw_cursor_update_position(dev_priv, false, 0, 0);
410                 return;
411         }
412
413         if (!ret) {
414                 du->cursor_x = plane->state->crtc_x + du->set_gui_x;
415                 du->cursor_y = plane->state->crtc_y + du->set_gui_y;
416
417                 vmw_cursor_update_position(dev_priv, true,
418                                            du->cursor_x + hotspot_x,
419                                            du->cursor_y + hotspot_y);
420
421                 du->core_hotspot_x = hotspot_x - du->hotspot_x;
422                 du->core_hotspot_y = hotspot_y - du->hotspot_y;
423         } else {
424                 DRM_ERROR("Failed to update cursor image\n");
425         }
426 }
427
428
429 /**
430  * vmw_du_primary_plane_atomic_check - check if the new state is okay
431  *
432  * @plane: display plane
433  * @state: info on the new plane state, including the FB
434  *
435  * Check if the new state is settable given the current state.  Other
436  * than what the atomic helper checks, we care about crtc fitting
437  * the FB and maintaining one active framebuffer.
438  *
439  * Returns 0 on success
440  */
441 int vmw_du_primary_plane_atomic_check(struct drm_plane *plane,
442                                       struct drm_plane_state *state)
443 {
444         struct drm_crtc_state *crtc_state = NULL;
445         struct drm_framebuffer *new_fb = state->fb;
446         struct drm_rect clip = {};
447         int ret;
448
449         if (state->crtc)
450                 crtc_state = drm_atomic_get_new_crtc_state(state->state, state->crtc);
451
452         if (crtc_state && crtc_state->enable)
453                 drm_mode_get_hv_timing(&crtc_state->mode,
454                                        &clip.x2, &clip.y2);
455
456         ret = drm_atomic_helper_check_plane_state(state, crtc_state, &clip,
457                                                   DRM_PLANE_HELPER_NO_SCALING,
458                                                   DRM_PLANE_HELPER_NO_SCALING,
459                                                   false, true);
460
461         if (!ret && new_fb) {
462                 struct drm_crtc *crtc = state->crtc;
463                 struct vmw_connector_state *vcs;
464                 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
465                 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
466                 struct vmw_framebuffer *vfb = vmw_framebuffer_to_vfb(new_fb);
467
468                 vcs = vmw_connector_state_to_vcs(du->connector.state);
469
470                 /* Only one active implicit framebuffer at a time. */
471                 mutex_lock(&dev_priv->global_kms_state_mutex);
472                 if (vcs->is_implicit && dev_priv->implicit_fb &&
473                     !(dev_priv->num_implicit == 1 && du->active_implicit)
474                     && dev_priv->implicit_fb != vfb) {
475                         DRM_ERROR("Multiple implicit framebuffers "
476                                   "not supported.\n");
477                         ret = -EINVAL;
478                 }
479                 mutex_unlock(&dev_priv->global_kms_state_mutex);
480         }
481
482
483         return ret;
484 }
485
486
487 /**
488  * vmw_du_cursor_plane_atomic_check - check if the new state is okay
489  *
490  * @plane: cursor plane
491  * @state: info on the new plane state
492  *
493  * This is a chance to fail if the new cursor state does not fit
494  * our requirements.
495  *
496  * Returns 0 on success
497  */
498 int vmw_du_cursor_plane_atomic_check(struct drm_plane *plane,
499                                      struct drm_plane_state *new_state)
500 {
501         int ret = 0;
502         struct vmw_surface *surface = NULL;
503         struct drm_framebuffer *fb = new_state->fb;
504
505
506         /* Turning off */
507         if (!fb)
508                 return ret;
509
510         /* A lot of the code assumes this */
511         if (new_state->crtc_w != 64 || new_state->crtc_h != 64) {
512                 DRM_ERROR("Invalid cursor dimensions (%d, %d)\n",
513                           new_state->crtc_w, new_state->crtc_h);
514                 ret = -EINVAL;
515         }
516
517         if (!vmw_framebuffer_to_vfb(fb)->dmabuf)
518                 surface = vmw_framebuffer_to_vfbs(fb)->surface;
519
520         if (surface && !surface->snooper.image) {
521                 DRM_ERROR("surface not suitable for cursor\n");
522                 ret = -EINVAL;
523         }
524
525         return ret;
526 }
527
528
529 int vmw_du_crtc_atomic_check(struct drm_crtc *crtc,
530                              struct drm_crtc_state *new_state)
531 {
532         struct vmw_display_unit *du = vmw_crtc_to_du(new_state->crtc);
533         int connector_mask = 1 << drm_connector_index(&du->connector);
534         bool has_primary = new_state->plane_mask &
535                            BIT(drm_plane_index(crtc->primary));
536
537         /* We always want to have an active plane with an active CRTC */
538         if (has_primary != new_state->enable)
539                 return -EINVAL;
540
541
542         if (new_state->connector_mask != connector_mask &&
543             new_state->connector_mask != 0) {
544                 DRM_ERROR("Invalid connectors configuration\n");
545                 return -EINVAL;
546         }
547
548         /*
549          * Our virtual device does not have a dot clock, so use the logical
550          * clock value as the dot clock.
551          */
552         if (new_state->mode.crtc_clock == 0)
553                 new_state->adjusted_mode.crtc_clock = new_state->mode.clock;
554
555         return 0;
556 }
557
558
559 void vmw_du_crtc_atomic_begin(struct drm_crtc *crtc,
560                               struct drm_crtc_state *old_crtc_state)
561 {
562 }
563
564
565 void vmw_du_crtc_atomic_flush(struct drm_crtc *crtc,
566                               struct drm_crtc_state *old_crtc_state)
567 {
568         struct drm_pending_vblank_event *event = crtc->state->event;
569
570         if (event) {
571                 crtc->state->event = NULL;
572
573                 spin_lock_irq(&crtc->dev->event_lock);
574                 if (drm_crtc_vblank_get(crtc) == 0)
575                         drm_crtc_arm_vblank_event(crtc, event);
576                 else
577                         drm_crtc_send_vblank_event(crtc, event);
578                 spin_unlock_irq(&crtc->dev->event_lock);
579         }
580
581 }
582
583
584 /**
585  * vmw_du_crtc_duplicate_state - duplicate crtc state
586  * @crtc: DRM crtc
587  *
588  * Allocates and returns a copy of the crtc state (both common and
589  * vmw-specific) for the specified crtc.
590  *
591  * Returns: The newly allocated crtc state, or NULL on failure.
592  */
593 struct drm_crtc_state *
594 vmw_du_crtc_duplicate_state(struct drm_crtc *crtc)
595 {
596         struct drm_crtc_state *state;
597         struct vmw_crtc_state *vcs;
598
599         if (WARN_ON(!crtc->state))
600                 return NULL;
601
602         vcs = kmemdup(crtc->state, sizeof(*vcs), GFP_KERNEL);
603
604         if (!vcs)
605                 return NULL;
606
607         state = &vcs->base;
608
609         __drm_atomic_helper_crtc_duplicate_state(crtc, state);
610
611         return state;
612 }
613
614
615 /**
616  * vmw_du_crtc_reset - creates a blank vmw crtc state
617  * @crtc: DRM crtc
618  *
619  * Resets the atomic state for @crtc by freeing the state pointer (which
620  * might be NULL, e.g. at driver load time) and allocating a new empty state
621  * object.
622  */
623 void vmw_du_crtc_reset(struct drm_crtc *crtc)
624 {
625         struct vmw_crtc_state *vcs;
626
627
628         if (crtc->state) {
629                 __drm_atomic_helper_crtc_destroy_state(crtc->state);
630
631                 kfree(vmw_crtc_state_to_vcs(crtc->state));
632         }
633
634         vcs = kzalloc(sizeof(*vcs), GFP_KERNEL);
635
636         if (!vcs) {
637                 DRM_ERROR("Cannot allocate vmw_crtc_state\n");
638                 return;
639         }
640
641         crtc->state = &vcs->base;
642         crtc->state->crtc = crtc;
643 }
644
645
646 /**
647  * vmw_du_crtc_destroy_state - destroy crtc state
648  * @crtc: DRM crtc
649  * @state: state object to destroy
650  *
651  * Destroys the crtc state (both common and vmw-specific) for the
652  * specified plane.
653  */
654 void
655 vmw_du_crtc_destroy_state(struct drm_crtc *crtc,
656                           struct drm_crtc_state *state)
657 {
658         drm_atomic_helper_crtc_destroy_state(crtc, state);
659 }
660
661
662 /**
663  * vmw_du_plane_duplicate_state - duplicate plane state
664  * @plane: drm plane
665  *
666  * Allocates and returns a copy of the plane state (both common and
667  * vmw-specific) for the specified plane.
668  *
669  * Returns: The newly allocated plane state, or NULL on failure.
670  */
671 struct drm_plane_state *
672 vmw_du_plane_duplicate_state(struct drm_plane *plane)
673 {
674         struct drm_plane_state *state;
675         struct vmw_plane_state *vps;
676
677         vps = kmemdup(plane->state, sizeof(*vps), GFP_KERNEL);
678
679         if (!vps)
680                 return NULL;
681
682         vps->pinned = 0;
683
684         /* Mapping is managed by prepare_fb/cleanup_fb */
685         memset(&vps->host_map, 0, sizeof(vps->host_map));
686         vps->cpp = 0;
687
688         /* Each ref counted resource needs to be acquired again */
689         if (vps->surf)
690                 (void) vmw_surface_reference(vps->surf);
691
692         if (vps->dmabuf)
693                 (void) vmw_dmabuf_reference(vps->dmabuf);
694
695         state = &vps->base;
696
697         __drm_atomic_helper_plane_duplicate_state(plane, state);
698
699         return state;
700 }
701
702
703 /**
704  * vmw_du_plane_reset - creates a blank vmw plane state
705  * @plane: drm plane
706  *
707  * Resets the atomic state for @plane by freeing the state pointer (which might
708  * be NULL, e.g. at driver load time) and allocating a new empty state object.
709  */
710 void vmw_du_plane_reset(struct drm_plane *plane)
711 {
712         struct vmw_plane_state *vps;
713
714
715         if (plane->state)
716                 vmw_du_plane_destroy_state(plane, plane->state);
717
718         vps = kzalloc(sizeof(*vps), GFP_KERNEL);
719
720         if (!vps) {
721                 DRM_ERROR("Cannot allocate vmw_plane_state\n");
722                 return;
723         }
724
725         plane->state = &vps->base;
726         plane->state->plane = plane;
727         plane->state->rotation = DRM_MODE_ROTATE_0;
728 }
729
730
731 /**
732  * vmw_du_plane_destroy_state - destroy plane state
733  * @plane: DRM plane
734  * @state: state object to destroy
735  *
736  * Destroys the plane state (both common and vmw-specific) for the
737  * specified plane.
738  */
739 void
740 vmw_du_plane_destroy_state(struct drm_plane *plane,
741                            struct drm_plane_state *state)
742 {
743         struct vmw_plane_state *vps = vmw_plane_state_to_vps(state);
744
745
746         /* Should have been freed by cleanup_fb */
747         if (vps->host_map.virtual) {
748                 DRM_ERROR("Host mapping not freed\n");
749                 ttm_bo_kunmap(&vps->host_map);
750         }
751
752         if (vps->surf)
753                 vmw_surface_unreference(&vps->surf);
754
755         if (vps->dmabuf)
756                 vmw_dmabuf_unreference(&vps->dmabuf);
757
758         drm_atomic_helper_plane_destroy_state(plane, state);
759 }
760
761
762 /**
763  * vmw_du_connector_duplicate_state - duplicate connector state
764  * @connector: DRM connector
765  *
766  * Allocates and returns a copy of the connector state (both common and
767  * vmw-specific) for the specified connector.
768  *
769  * Returns: The newly allocated connector state, or NULL on failure.
770  */
771 struct drm_connector_state *
772 vmw_du_connector_duplicate_state(struct drm_connector *connector)
773 {
774         struct drm_connector_state *state;
775         struct vmw_connector_state *vcs;
776
777         if (WARN_ON(!connector->state))
778                 return NULL;
779
780         vcs = kmemdup(connector->state, sizeof(*vcs), GFP_KERNEL);
781
782         if (!vcs)
783                 return NULL;
784
785         state = &vcs->base;
786
787         __drm_atomic_helper_connector_duplicate_state(connector, state);
788
789         return state;
790 }
791
792
793 /**
794  * vmw_du_connector_reset - creates a blank vmw connector state
795  * @connector: DRM connector
796  *
797  * Resets the atomic state for @connector by freeing the state pointer (which
798  * might be NULL, e.g. at driver load time) and allocating a new empty state
799  * object.
800  */
801 void vmw_du_connector_reset(struct drm_connector *connector)
802 {
803         struct vmw_connector_state *vcs;
804
805
806         if (connector->state) {
807                 __drm_atomic_helper_connector_destroy_state(connector->state);
808
809                 kfree(vmw_connector_state_to_vcs(connector->state));
810         }
811
812         vcs = kzalloc(sizeof(*vcs), GFP_KERNEL);
813
814         if (!vcs) {
815                 DRM_ERROR("Cannot allocate vmw_connector_state\n");
816                 return;
817         }
818
819         __drm_atomic_helper_connector_reset(connector, &vcs->base);
820 }
821
822
823 /**
824  * vmw_du_connector_destroy_state - destroy connector state
825  * @connector: DRM connector
826  * @state: state object to destroy
827  *
828  * Destroys the connector state (both common and vmw-specific) for the
829  * specified plane.
830  */
831 void
832 vmw_du_connector_destroy_state(struct drm_connector *connector,
833                           struct drm_connector_state *state)
834 {
835         drm_atomic_helper_connector_destroy_state(connector, state);
836 }
837 /*
838  * Generic framebuffer code
839  */
840
841 /*
842  * Surface framebuffer code
843  */
844
845 static void vmw_framebuffer_surface_destroy(struct drm_framebuffer *framebuffer)
846 {
847         struct vmw_framebuffer_surface *vfbs =
848                 vmw_framebuffer_to_vfbs(framebuffer);
849
850         drm_framebuffer_cleanup(framebuffer);
851         vmw_surface_unreference(&vfbs->surface);
852         if (vfbs->base.user_obj)
853                 ttm_base_object_unref(&vfbs->base.user_obj);
854
855         kfree(vfbs);
856 }
857
858 static int vmw_framebuffer_surface_dirty(struct drm_framebuffer *framebuffer,
859                                   struct drm_file *file_priv,
860                                   unsigned flags, unsigned color,
861                                   struct drm_clip_rect *clips,
862                                   unsigned num_clips)
863 {
864         struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
865         struct vmw_framebuffer_surface *vfbs =
866                 vmw_framebuffer_to_vfbs(framebuffer);
867         struct drm_clip_rect norect;
868         int ret, inc = 1;
869
870         /* Legacy Display Unit does not support 3D */
871         if (dev_priv->active_display_unit == vmw_du_legacy)
872                 return -EINVAL;
873
874         drm_modeset_lock_all(dev_priv->dev);
875
876         ret = ttm_read_lock(&dev_priv->reservation_sem, true);
877         if (unlikely(ret != 0)) {
878                 drm_modeset_unlock_all(dev_priv->dev);
879                 return ret;
880         }
881
882         if (!num_clips) {
883                 num_clips = 1;
884                 clips = &norect;
885                 norect.x1 = norect.y1 = 0;
886                 norect.x2 = framebuffer->width;
887                 norect.y2 = framebuffer->height;
888         } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
889                 num_clips /= 2;
890                 inc = 2; /* skip source rects */
891         }
892
893         if (dev_priv->active_display_unit == vmw_du_screen_object)
894                 ret = vmw_kms_sou_do_surface_dirty(dev_priv, &vfbs->base,
895                                                    clips, NULL, NULL, 0, 0,
896                                                    num_clips, inc, NULL);
897         else
898                 ret = vmw_kms_stdu_surface_dirty(dev_priv, &vfbs->base,
899                                                  clips, NULL, NULL, 0, 0,
900                                                  num_clips, inc, NULL);
901
902         vmw_fifo_flush(dev_priv, false);
903         ttm_read_unlock(&dev_priv->reservation_sem);
904
905         drm_modeset_unlock_all(dev_priv->dev);
906
907         return 0;
908 }
909
910 /**
911  * vmw_kms_readback - Perform a readback from the screen system to
912  * a dma-buffer backed framebuffer.
913  *
914  * @dev_priv: Pointer to the device private structure.
915  * @file_priv: Pointer to a struct drm_file identifying the caller.
916  * Must be set to NULL if @user_fence_rep is NULL.
917  * @vfb: Pointer to the dma-buffer backed framebuffer.
918  * @user_fence_rep: User-space provided structure for fence information.
919  * Must be set to non-NULL if @file_priv is non-NULL.
920  * @vclips: Array of clip rects.
921  * @num_clips: Number of clip rects in @vclips.
922  *
923  * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
924  * interrupted.
925  */
926 int vmw_kms_readback(struct vmw_private *dev_priv,
927                      struct drm_file *file_priv,
928                      struct vmw_framebuffer *vfb,
929                      struct drm_vmw_fence_rep __user *user_fence_rep,
930                      struct drm_vmw_rect *vclips,
931                      uint32_t num_clips)
932 {
933         switch (dev_priv->active_display_unit) {
934         case vmw_du_screen_object:
935                 return vmw_kms_sou_readback(dev_priv, file_priv, vfb,
936                                             user_fence_rep, vclips, num_clips);
937         case vmw_du_screen_target:
938                 return vmw_kms_stdu_dma(dev_priv, file_priv, vfb,
939                                         user_fence_rep, NULL, vclips, num_clips,
940                                         1, false, true);
941         default:
942                 WARN_ONCE(true,
943                           "Readback called with invalid display system.\n");
944 }
945
946         return -ENOSYS;
947 }
948
949
950 static const struct drm_framebuffer_funcs vmw_framebuffer_surface_funcs = {
951         .destroy = vmw_framebuffer_surface_destroy,
952         .dirty = vmw_framebuffer_surface_dirty,
953 };
954
955 static int vmw_kms_new_framebuffer_surface(struct vmw_private *dev_priv,
956                                            struct vmw_surface *surface,
957                                            struct vmw_framebuffer **out,
958                                            const struct drm_mode_fb_cmd2
959                                            *mode_cmd,
960                                            bool is_dmabuf_proxy)
961
962 {
963         struct drm_device *dev = dev_priv->dev;
964         struct vmw_framebuffer_surface *vfbs;
965         enum SVGA3dSurfaceFormat format;
966         int ret;
967         struct drm_format_name_buf format_name;
968
969         /* 3D is only supported on HWv8 and newer hosts */
970         if (dev_priv->active_display_unit == vmw_du_legacy)
971                 return -ENOSYS;
972
973         /*
974          * Sanity checks.
975          */
976
977         /* Surface must be marked as a scanout. */
978         if (unlikely(!surface->scanout))
979                 return -EINVAL;
980
981         if (unlikely(surface->mip_levels[0] != 1 ||
982                      surface->num_sizes != 1 ||
983                      surface->base_size.width < mode_cmd->width ||
984                      surface->base_size.height < mode_cmd->height ||
985                      surface->base_size.depth != 1)) {
986                 DRM_ERROR("Incompatible surface dimensions "
987                           "for requested mode.\n");
988                 return -EINVAL;
989         }
990
991         switch (mode_cmd->pixel_format) {
992         case DRM_FORMAT_ARGB8888:
993                 format = SVGA3D_A8R8G8B8;
994                 break;
995         case DRM_FORMAT_XRGB8888:
996                 format = SVGA3D_X8R8G8B8;
997                 break;
998         case DRM_FORMAT_RGB565:
999                 format = SVGA3D_R5G6B5;
1000                 break;
1001         case DRM_FORMAT_XRGB1555:
1002                 format = SVGA3D_A1R5G5B5;
1003                 break;
1004         default:
1005                 DRM_ERROR("Invalid pixel format: %s\n",
1006                           drm_get_format_name(mode_cmd->pixel_format, &format_name));
1007                 return -EINVAL;
1008         }
1009
1010         /*
1011          * For DX, surface format validation is done when surface->scanout
1012          * is set.
1013          */
1014         if (!dev_priv->has_dx && format != surface->format) {
1015                 DRM_ERROR("Invalid surface format for requested mode.\n");
1016                 return -EINVAL;
1017         }
1018
1019         vfbs = kzalloc(sizeof(*vfbs), GFP_KERNEL);
1020         if (!vfbs) {
1021                 ret = -ENOMEM;
1022                 goto out_err1;
1023         }
1024
1025         drm_helper_mode_fill_fb_struct(dev, &vfbs->base.base, mode_cmd);
1026         vfbs->surface = vmw_surface_reference(surface);
1027         vfbs->base.user_handle = mode_cmd->handles[0];
1028         vfbs->is_dmabuf_proxy = is_dmabuf_proxy;
1029
1030         *out = &vfbs->base;
1031
1032         ret = drm_framebuffer_init(dev, &vfbs->base.base,
1033                                    &vmw_framebuffer_surface_funcs);
1034         if (ret)
1035                 goto out_err2;
1036
1037         return 0;
1038
1039 out_err2:
1040         vmw_surface_unreference(&surface);
1041         kfree(vfbs);
1042 out_err1:
1043         return ret;
1044 }
1045
1046 /*
1047  * Dmabuf framebuffer code
1048  */
1049
1050 static void vmw_framebuffer_dmabuf_destroy(struct drm_framebuffer *framebuffer)
1051 {
1052         struct vmw_framebuffer_dmabuf *vfbd =
1053                 vmw_framebuffer_to_vfbd(framebuffer);
1054
1055         drm_framebuffer_cleanup(framebuffer);
1056         vmw_dmabuf_unreference(&vfbd->buffer);
1057         if (vfbd->base.user_obj)
1058                 ttm_base_object_unref(&vfbd->base.user_obj);
1059
1060         kfree(vfbd);
1061 }
1062
1063 static int vmw_framebuffer_dmabuf_dirty(struct drm_framebuffer *framebuffer,
1064                                  struct drm_file *file_priv,
1065                                  unsigned flags, unsigned color,
1066                                  struct drm_clip_rect *clips,
1067                                  unsigned num_clips)
1068 {
1069         struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
1070         struct vmw_framebuffer_dmabuf *vfbd =
1071                 vmw_framebuffer_to_vfbd(framebuffer);
1072         struct drm_clip_rect norect;
1073         int ret, increment = 1;
1074
1075         drm_modeset_lock_all(dev_priv->dev);
1076
1077         ret = ttm_read_lock(&dev_priv->reservation_sem, true);
1078         if (unlikely(ret != 0)) {
1079                 drm_modeset_unlock_all(dev_priv->dev);
1080                 return ret;
1081         }
1082
1083         if (!num_clips) {
1084                 num_clips = 1;
1085                 clips = &norect;
1086                 norect.x1 = norect.y1 = 0;
1087                 norect.x2 = framebuffer->width;
1088                 norect.y2 = framebuffer->height;
1089         } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
1090                 num_clips /= 2;
1091                 increment = 2;
1092         }
1093
1094         switch (dev_priv->active_display_unit) {
1095         case vmw_du_screen_target:
1096                 ret = vmw_kms_stdu_dma(dev_priv, NULL, &vfbd->base, NULL,
1097                                        clips, NULL, num_clips, increment,
1098                                        true, true);
1099                 break;
1100         case vmw_du_screen_object:
1101                 ret = vmw_kms_sou_do_dmabuf_dirty(dev_priv, &vfbd->base,
1102                                                   clips, NULL, num_clips,
1103                                                   increment, true, NULL);
1104                 break;
1105         case vmw_du_legacy:
1106                 ret = vmw_kms_ldu_do_dmabuf_dirty(dev_priv, &vfbd->base, 0, 0,
1107                                                   clips, num_clips, increment);
1108                 break;
1109         default:
1110                 ret = -EINVAL;
1111                 WARN_ONCE(true, "Dirty called with invalid display system.\n");
1112                 break;
1113         }
1114
1115         vmw_fifo_flush(dev_priv, false);
1116         ttm_read_unlock(&dev_priv->reservation_sem);
1117
1118         drm_modeset_unlock_all(dev_priv->dev);
1119
1120         return ret;
1121 }
1122
1123 static const struct drm_framebuffer_funcs vmw_framebuffer_dmabuf_funcs = {
1124         .destroy = vmw_framebuffer_dmabuf_destroy,
1125         .dirty = vmw_framebuffer_dmabuf_dirty,
1126 };
1127
1128 /**
1129  * Pin the dmabuffer to the start of vram.
1130  */
1131 static int vmw_framebuffer_pin(struct vmw_framebuffer *vfb)
1132 {
1133         struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
1134         struct vmw_dma_buffer *buf;
1135         int ret;
1136
1137         buf = vfb->dmabuf ?  vmw_framebuffer_to_vfbd(&vfb->base)->buffer :
1138                 vmw_framebuffer_to_vfbs(&vfb->base)->surface->res.backup;
1139
1140         if (!buf)
1141                 return 0;
1142
1143         switch (dev_priv->active_display_unit) {
1144         case vmw_du_legacy:
1145                 vmw_overlay_pause_all(dev_priv);
1146                 ret = vmw_dmabuf_pin_in_start_of_vram(dev_priv, buf, false);
1147                 vmw_overlay_resume_all(dev_priv);
1148                 break;
1149         case vmw_du_screen_object:
1150         case vmw_du_screen_target:
1151                 if (vfb->dmabuf)
1152                         return vmw_dmabuf_pin_in_vram_or_gmr(dev_priv, buf,
1153                                                              false);
1154
1155                 return vmw_dmabuf_pin_in_placement(dev_priv, buf,
1156                                                    &vmw_mob_placement, false);
1157         default:
1158                 return -EINVAL;
1159         }
1160
1161         return ret;
1162 }
1163
1164 static int vmw_framebuffer_unpin(struct vmw_framebuffer *vfb)
1165 {
1166         struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
1167         struct vmw_dma_buffer *buf;
1168
1169         buf = vfb->dmabuf ?  vmw_framebuffer_to_vfbd(&vfb->base)->buffer :
1170                 vmw_framebuffer_to_vfbs(&vfb->base)->surface->res.backup;
1171
1172         if (WARN_ON(!buf))
1173                 return 0;
1174
1175         return vmw_dmabuf_unpin(dev_priv, buf, false);
1176 }
1177
1178 /**
1179  * vmw_create_dmabuf_proxy - create a proxy surface for the DMA buf
1180  *
1181  * @dev: DRM device
1182  * @mode_cmd: parameters for the new surface
1183  * @dmabuf_mob: MOB backing the DMA buf
1184  * @srf_out: newly created surface
1185  *
1186  * When the content FB is a DMA buf, we create a surface as a proxy to the
1187  * same buffer.  This way we can do a surface copy rather than a surface DMA.
1188  * This is a more efficient approach
1189  *
1190  * RETURNS:
1191  * 0 on success, error code otherwise
1192  */
1193 static int vmw_create_dmabuf_proxy(struct drm_device *dev,
1194                                    const struct drm_mode_fb_cmd2 *mode_cmd,
1195                                    struct vmw_dma_buffer *dmabuf_mob,
1196                                    struct vmw_surface **srf_out)
1197 {
1198         uint32_t format;
1199         struct drm_vmw_size content_base_size = {0};
1200         struct vmw_resource *res;
1201         unsigned int bytes_pp;
1202         struct drm_format_name_buf format_name;
1203         int ret;
1204
1205         switch (mode_cmd->pixel_format) {
1206         case DRM_FORMAT_ARGB8888:
1207         case DRM_FORMAT_XRGB8888:
1208                 format = SVGA3D_X8R8G8B8;
1209                 bytes_pp = 4;
1210                 break;
1211
1212         case DRM_FORMAT_RGB565:
1213         case DRM_FORMAT_XRGB1555:
1214                 format = SVGA3D_R5G6B5;
1215                 bytes_pp = 2;
1216                 break;
1217
1218         case 8:
1219                 format = SVGA3D_P8;
1220                 bytes_pp = 1;
1221                 break;
1222
1223         default:
1224                 DRM_ERROR("Invalid framebuffer format %s\n",
1225                           drm_get_format_name(mode_cmd->pixel_format, &format_name));
1226                 return -EINVAL;
1227         }
1228
1229         content_base_size.width  = mode_cmd->pitches[0] / bytes_pp;
1230         content_base_size.height = mode_cmd->height;
1231         content_base_size.depth  = 1;
1232
1233         ret = vmw_surface_gb_priv_define(dev,
1234                         0, /* kernel visible only */
1235                         0, /* flags */
1236                         format,
1237                         true, /* can be a scanout buffer */
1238                         1, /* num of mip levels */
1239                         0,
1240                         0,
1241                         content_base_size,
1242                         srf_out);
1243         if (ret) {
1244                 DRM_ERROR("Failed to allocate proxy content buffer\n");
1245                 return ret;
1246         }
1247
1248         res = &(*srf_out)->res;
1249
1250         /* Reserve and switch the backing mob. */
1251         mutex_lock(&res->dev_priv->cmdbuf_mutex);
1252         (void) vmw_resource_reserve(res, false, true);
1253         vmw_dmabuf_unreference(&res->backup);
1254         res->backup = vmw_dmabuf_reference(dmabuf_mob);
1255         res->backup_offset = 0;
1256         vmw_resource_unreserve(res, false, NULL, 0);
1257         mutex_unlock(&res->dev_priv->cmdbuf_mutex);
1258
1259         return 0;
1260 }
1261
1262
1263
1264 static int vmw_kms_new_framebuffer_dmabuf(struct vmw_private *dev_priv,
1265                                           struct vmw_dma_buffer *dmabuf,
1266                                           struct vmw_framebuffer **out,
1267                                           const struct drm_mode_fb_cmd2
1268                                           *mode_cmd)
1269
1270 {
1271         struct drm_device *dev = dev_priv->dev;
1272         struct vmw_framebuffer_dmabuf *vfbd;
1273         unsigned int requested_size;
1274         struct drm_format_name_buf format_name;
1275         int ret;
1276
1277         requested_size = mode_cmd->height * mode_cmd->pitches[0];
1278         if (unlikely(requested_size > dmabuf->base.num_pages * PAGE_SIZE)) {
1279                 DRM_ERROR("Screen buffer object size is too small "
1280                           "for requested mode.\n");
1281                 return -EINVAL;
1282         }
1283
1284         /* Limited framebuffer color depth support for screen objects */
1285         if (dev_priv->active_display_unit == vmw_du_screen_object) {
1286                 switch (mode_cmd->pixel_format) {
1287                 case DRM_FORMAT_XRGB8888:
1288                 case DRM_FORMAT_ARGB8888:
1289                         break;
1290                 case DRM_FORMAT_XRGB1555:
1291                 case DRM_FORMAT_RGB565:
1292                         break;
1293                 default:
1294                         DRM_ERROR("Invalid pixel format: %s\n",
1295                                   drm_get_format_name(mode_cmd->pixel_format, &format_name));
1296                         return -EINVAL;
1297                 }
1298         }
1299
1300         vfbd = kzalloc(sizeof(*vfbd), GFP_KERNEL);
1301         if (!vfbd) {
1302                 ret = -ENOMEM;
1303                 goto out_err1;
1304         }
1305
1306         drm_helper_mode_fill_fb_struct(dev, &vfbd->base.base, mode_cmd);
1307         vfbd->base.dmabuf = true;
1308         vfbd->buffer = vmw_dmabuf_reference(dmabuf);
1309         vfbd->base.user_handle = mode_cmd->handles[0];
1310         *out = &vfbd->base;
1311
1312         ret = drm_framebuffer_init(dev, &vfbd->base.base,
1313                                    &vmw_framebuffer_dmabuf_funcs);
1314         if (ret)
1315                 goto out_err2;
1316
1317         return 0;
1318
1319 out_err2:
1320         vmw_dmabuf_unreference(&dmabuf);
1321         kfree(vfbd);
1322 out_err1:
1323         return ret;
1324 }
1325
1326
1327 /**
1328  * vmw_kms_srf_ok - check if a surface can be created
1329  *
1330  * @width: requested width
1331  * @height: requested height
1332  *
1333  * Surfaces need to be less than texture size
1334  */
1335 static bool
1336 vmw_kms_srf_ok(struct vmw_private *dev_priv, uint32_t width, uint32_t height)
1337 {
1338         if (width  > dev_priv->texture_max_width ||
1339             height > dev_priv->texture_max_height)
1340                 return false;
1341
1342         return true;
1343 }
1344
1345 /**
1346  * vmw_kms_new_framebuffer - Create a new framebuffer.
1347  *
1348  * @dev_priv: Pointer to device private struct.
1349  * @dmabuf: Pointer to dma buffer to wrap the kms framebuffer around.
1350  * Either @dmabuf or @surface must be NULL.
1351  * @surface: Pointer to a surface to wrap the kms framebuffer around.
1352  * Either @dmabuf or @surface must be NULL.
1353  * @only_2d: No presents will occur to this dma buffer based framebuffer. This
1354  * Helps the code to do some important optimizations.
1355  * @mode_cmd: Frame-buffer metadata.
1356  */
1357 struct vmw_framebuffer *
1358 vmw_kms_new_framebuffer(struct vmw_private *dev_priv,
1359                         struct vmw_dma_buffer *dmabuf,
1360                         struct vmw_surface *surface,
1361                         bool only_2d,
1362                         const struct drm_mode_fb_cmd2 *mode_cmd)
1363 {
1364         struct vmw_framebuffer *vfb = NULL;
1365         bool is_dmabuf_proxy = false;
1366         int ret;
1367
1368         /*
1369          * We cannot use the SurfaceDMA command in an non-accelerated VM,
1370          * therefore, wrap the DMA buf in a surface so we can use the
1371          * SurfaceCopy command.
1372          */
1373         if (vmw_kms_srf_ok(dev_priv, mode_cmd->width, mode_cmd->height)  &&
1374             dmabuf && only_2d &&
1375             mode_cmd->width > 64 &&  /* Don't create a proxy for cursor */
1376             dev_priv->active_display_unit == vmw_du_screen_target) {
1377                 ret = vmw_create_dmabuf_proxy(dev_priv->dev, mode_cmd,
1378                                               dmabuf, &surface);
1379                 if (ret)
1380                         return ERR_PTR(ret);
1381
1382                 is_dmabuf_proxy = true;
1383         }
1384
1385         /* Create the new framebuffer depending one what we have */
1386         if (surface) {
1387                 ret = vmw_kms_new_framebuffer_surface(dev_priv, surface, &vfb,
1388                                                       mode_cmd,
1389                                                       is_dmabuf_proxy);
1390
1391                 /*
1392                  * vmw_create_dmabuf_proxy() adds a reference that is no longer
1393                  * needed
1394                  */
1395                 if (is_dmabuf_proxy)
1396                         vmw_surface_unreference(&surface);
1397         } else if (dmabuf) {
1398                 ret = vmw_kms_new_framebuffer_dmabuf(dev_priv, dmabuf, &vfb,
1399                                                      mode_cmd);
1400         } else {
1401                 BUG();
1402         }
1403
1404         if (ret)
1405                 return ERR_PTR(ret);
1406
1407         vfb->pin = vmw_framebuffer_pin;
1408         vfb->unpin = vmw_framebuffer_unpin;
1409
1410         return vfb;
1411 }
1412
1413 /*
1414  * Generic Kernel modesetting functions
1415  */
1416
1417 static struct drm_framebuffer *vmw_kms_fb_create(struct drm_device *dev,
1418                                                  struct drm_file *file_priv,
1419                                                  const struct drm_mode_fb_cmd2 *mode_cmd)
1420 {
1421         struct vmw_private *dev_priv = vmw_priv(dev);
1422         struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
1423         struct vmw_framebuffer *vfb = NULL;
1424         struct vmw_surface *surface = NULL;
1425         struct vmw_dma_buffer *bo = NULL;
1426         struct ttm_base_object *user_obj;
1427         int ret;
1428
1429         /**
1430          * This code should be conditioned on Screen Objects not being used.
1431          * If screen objects are used, we can allocate a GMR to hold the
1432          * requested framebuffer.
1433          */
1434
1435         if (!vmw_kms_validate_mode_vram(dev_priv,
1436                                         mode_cmd->pitches[0],
1437                                         mode_cmd->height)) {
1438                 DRM_ERROR("Requested mode exceed bounding box limit.\n");
1439                 return ERR_PTR(-ENOMEM);
1440         }
1441
1442         /*
1443          * Take a reference on the user object of the resource
1444          * backing the kms fb. This ensures that user-space handle
1445          * lookups on that resource will always work as long as
1446          * it's registered with a kms framebuffer. This is important,
1447          * since vmw_execbuf_process identifies resources in the
1448          * command stream using user-space handles.
1449          */
1450
1451         user_obj = ttm_base_object_lookup(tfile, mode_cmd->handles[0]);
1452         if (unlikely(user_obj == NULL)) {
1453                 DRM_ERROR("Could not locate requested kms frame buffer.\n");
1454                 return ERR_PTR(-ENOENT);
1455         }
1456
1457         /**
1458          * End conditioned code.
1459          */
1460
1461         /* returns either a dmabuf or surface */
1462         ret = vmw_user_lookup_handle(dev_priv, tfile,
1463                                      mode_cmd->handles[0],
1464                                      &surface, &bo);
1465         if (ret)
1466                 goto err_out;
1467
1468
1469         if (!bo &&
1470             !vmw_kms_srf_ok(dev_priv, mode_cmd->width, mode_cmd->height)) {
1471                 DRM_ERROR("Surface size cannot exceed %dx%d",
1472                         dev_priv->texture_max_width,
1473                         dev_priv->texture_max_height);
1474                 goto err_out;
1475         }
1476
1477
1478         vfb = vmw_kms_new_framebuffer(dev_priv, bo, surface,
1479                                       !(dev_priv->capabilities & SVGA_CAP_3D),
1480                                       mode_cmd);
1481         if (IS_ERR(vfb)) {
1482                 ret = PTR_ERR(vfb);
1483                 goto err_out;
1484         }
1485
1486 err_out:
1487         /* vmw_user_lookup_handle takes one ref so does new_fb */
1488         if (bo)
1489                 vmw_dmabuf_unreference(&bo);
1490         if (surface)
1491                 vmw_surface_unreference(&surface);
1492
1493         if (ret) {
1494                 DRM_ERROR("failed to create vmw_framebuffer: %i\n", ret);
1495                 ttm_base_object_unref(&user_obj);
1496                 return ERR_PTR(ret);
1497         } else
1498                 vfb->user_obj = user_obj;
1499
1500         return &vfb->base;
1501 }
1502
1503
1504
1505 /**
1506  * vmw_kms_atomic_check_modeset- validate state object for modeset changes
1507  *
1508  * @dev: DRM device
1509  * @state: the driver state object
1510  *
1511  * This is a simple wrapper around drm_atomic_helper_check_modeset() for
1512  * us to assign a value to mode->crtc_clock so that
1513  * drm_calc_timestamping_constants() won't throw an error message
1514  *
1515  * RETURNS
1516  * Zero for success or -errno
1517  */
1518 static int
1519 vmw_kms_atomic_check_modeset(struct drm_device *dev,
1520                              struct drm_atomic_state *state)
1521 {
1522         struct drm_crtc_state *crtc_state;
1523         struct drm_crtc *crtc;
1524         struct vmw_private *dev_priv = vmw_priv(dev);
1525         int i;
1526
1527         for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
1528                 unsigned long requested_bb_mem = 0;
1529
1530                 if (dev_priv->active_display_unit == vmw_du_screen_target) {
1531                         if (crtc->primary->fb) {
1532                                 int cpp = crtc->primary->fb->pitches[0] /
1533                                           crtc->primary->fb->width;
1534
1535                                 requested_bb_mem += crtc->mode.hdisplay * cpp *
1536                                                     crtc->mode.vdisplay;
1537                         }
1538
1539                         if (requested_bb_mem > dev_priv->prim_bb_mem)
1540                                 return -EINVAL;
1541                 }
1542         }
1543
1544         return drm_atomic_helper_check(dev, state);
1545 }
1546
1547
1548 /**
1549  * vmw_kms_atomic_commit - Perform an atomic state commit
1550  *
1551  * @dev: DRM device
1552  * @state: the driver state object
1553  * @nonblock: Whether nonblocking behaviour is requested
1554  *
1555  * This is a simple wrapper around drm_atomic_helper_commit() for
1556  * us to clear the nonblocking value.
1557  *
1558  * Nonblocking commits currently cause synchronization issues
1559  * for vmwgfx.
1560  *
1561  * RETURNS
1562  * Zero for success or negative error code on failure.
1563  */
1564 int vmw_kms_atomic_commit(struct drm_device *dev,
1565                           struct drm_atomic_state *state,
1566                           bool nonblock)
1567 {
1568         return drm_atomic_helper_commit(dev, state, false);
1569 }
1570
1571
1572 static const struct drm_mode_config_funcs vmw_kms_funcs = {
1573         .fb_create = vmw_kms_fb_create,
1574         .atomic_check = vmw_kms_atomic_check_modeset,
1575         .atomic_commit = vmw_kms_atomic_commit,
1576 };
1577
1578 static int vmw_kms_generic_present(struct vmw_private *dev_priv,
1579                                    struct drm_file *file_priv,
1580                                    struct vmw_framebuffer *vfb,
1581                                    struct vmw_surface *surface,
1582                                    uint32_t sid,
1583                                    int32_t destX, int32_t destY,
1584                                    struct drm_vmw_rect *clips,
1585                                    uint32_t num_clips)
1586 {
1587         return vmw_kms_sou_do_surface_dirty(dev_priv, vfb, NULL, clips,
1588                                             &surface->res, destX, destY,
1589                                             num_clips, 1, NULL);
1590 }
1591
1592
1593 int vmw_kms_present(struct vmw_private *dev_priv,
1594                     struct drm_file *file_priv,
1595                     struct vmw_framebuffer *vfb,
1596                     struct vmw_surface *surface,
1597                     uint32_t sid,
1598                     int32_t destX, int32_t destY,
1599                     struct drm_vmw_rect *clips,
1600                     uint32_t num_clips)
1601 {
1602         int ret;
1603
1604         switch (dev_priv->active_display_unit) {
1605         case vmw_du_screen_target:
1606                 ret = vmw_kms_stdu_surface_dirty(dev_priv, vfb, NULL, clips,
1607                                                  &surface->res, destX, destY,
1608                                                  num_clips, 1, NULL);
1609                 break;
1610         case vmw_du_screen_object:
1611                 ret = vmw_kms_generic_present(dev_priv, file_priv, vfb, surface,
1612                                               sid, destX, destY, clips,
1613                                               num_clips);
1614                 break;
1615         default:
1616                 WARN_ONCE(true,
1617                           "Present called with invalid display system.\n");
1618                 ret = -ENOSYS;
1619                 break;
1620         }
1621         if (ret)
1622                 return ret;
1623
1624         vmw_fifo_flush(dev_priv, false);
1625
1626         return 0;
1627 }
1628
1629 static void
1630 vmw_kms_create_hotplug_mode_update_property(struct vmw_private *dev_priv)
1631 {
1632         if (dev_priv->hotplug_mode_update_property)
1633                 return;
1634
1635         dev_priv->hotplug_mode_update_property =
1636                 drm_property_create_range(dev_priv->dev,
1637                                           DRM_MODE_PROP_IMMUTABLE,
1638                                           "hotplug_mode_update", 0, 1);
1639
1640         if (!dev_priv->hotplug_mode_update_property)
1641                 return;
1642
1643 }
1644
1645 int vmw_kms_init(struct vmw_private *dev_priv)
1646 {
1647         struct drm_device *dev = dev_priv->dev;
1648         int ret;
1649
1650         drm_mode_config_init(dev);
1651         dev->mode_config.funcs = &vmw_kms_funcs;
1652         dev->mode_config.min_width = 1;
1653         dev->mode_config.min_height = 1;
1654         dev->mode_config.max_width = dev_priv->texture_max_width;
1655         dev->mode_config.max_height = dev_priv->texture_max_height;
1656
1657         drm_mode_create_suggested_offset_properties(dev);
1658         vmw_kms_create_hotplug_mode_update_property(dev_priv);
1659
1660         ret = vmw_kms_stdu_init_display(dev_priv);
1661         if (ret) {
1662                 ret = vmw_kms_sou_init_display(dev_priv);
1663                 if (ret) /* Fallback */
1664                         ret = vmw_kms_ldu_init_display(dev_priv);
1665         }
1666
1667         return ret;
1668 }
1669
1670 int vmw_kms_close(struct vmw_private *dev_priv)
1671 {
1672         int ret = 0;
1673
1674         /*
1675          * Docs says we should take the lock before calling this function
1676          * but since it destroys encoders and our destructor calls
1677          * drm_encoder_cleanup which takes the lock we deadlock.
1678          */
1679         drm_mode_config_cleanup(dev_priv->dev);
1680         if (dev_priv->active_display_unit == vmw_du_legacy)
1681                 ret = vmw_kms_ldu_close_display(dev_priv);
1682
1683         return ret;
1684 }
1685
1686 int vmw_kms_cursor_bypass_ioctl(struct drm_device *dev, void *data,
1687                                 struct drm_file *file_priv)
1688 {
1689         struct drm_vmw_cursor_bypass_arg *arg = data;
1690         struct vmw_display_unit *du;
1691         struct drm_crtc *crtc;
1692         int ret = 0;
1693
1694
1695         mutex_lock(&dev->mode_config.mutex);
1696         if (arg->flags & DRM_VMW_CURSOR_BYPASS_ALL) {
1697
1698                 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1699                         du = vmw_crtc_to_du(crtc);
1700                         du->hotspot_x = arg->xhot;
1701                         du->hotspot_y = arg->yhot;
1702                 }
1703
1704                 mutex_unlock(&dev->mode_config.mutex);
1705                 return 0;
1706         }
1707
1708         crtc = drm_crtc_find(dev, file_priv, arg->crtc_id);
1709         if (!crtc) {
1710                 ret = -ENOENT;
1711                 goto out;
1712         }
1713
1714         du = vmw_crtc_to_du(crtc);
1715
1716         du->hotspot_x = arg->xhot;
1717         du->hotspot_y = arg->yhot;
1718
1719 out:
1720         mutex_unlock(&dev->mode_config.mutex);
1721
1722         return ret;
1723 }
1724
1725 int vmw_kms_write_svga(struct vmw_private *vmw_priv,
1726                         unsigned width, unsigned height, unsigned pitch,
1727                         unsigned bpp, unsigned depth)
1728 {
1729         if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1730                 vmw_write(vmw_priv, SVGA_REG_PITCHLOCK, pitch);
1731         else if (vmw_fifo_have_pitchlock(vmw_priv))
1732                 vmw_mmio_write(pitch, vmw_priv->mmio_virt +
1733                                SVGA_FIFO_PITCHLOCK);
1734         vmw_write(vmw_priv, SVGA_REG_WIDTH, width);
1735         vmw_write(vmw_priv, SVGA_REG_HEIGHT, height);
1736         vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, bpp);
1737
1738         if (vmw_read(vmw_priv, SVGA_REG_DEPTH) != depth) {
1739                 DRM_ERROR("Invalid depth %u for %u bpp, host expects %u\n",
1740                           depth, bpp, vmw_read(vmw_priv, SVGA_REG_DEPTH));
1741                 return -EINVAL;
1742         }
1743
1744         return 0;
1745 }
1746
1747 int vmw_kms_save_vga(struct vmw_private *vmw_priv)
1748 {
1749         struct vmw_vga_topology_state *save;
1750         uint32_t i;
1751
1752         vmw_priv->vga_width = vmw_read(vmw_priv, SVGA_REG_WIDTH);
1753         vmw_priv->vga_height = vmw_read(vmw_priv, SVGA_REG_HEIGHT);
1754         vmw_priv->vga_bpp = vmw_read(vmw_priv, SVGA_REG_BITS_PER_PIXEL);
1755         if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1756                 vmw_priv->vga_pitchlock =
1757                   vmw_read(vmw_priv, SVGA_REG_PITCHLOCK);
1758         else if (vmw_fifo_have_pitchlock(vmw_priv))
1759                 vmw_priv->vga_pitchlock = vmw_mmio_read(vmw_priv->mmio_virt +
1760                                                         SVGA_FIFO_PITCHLOCK);
1761
1762         if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
1763                 return 0;
1764
1765         vmw_priv->num_displays = vmw_read(vmw_priv,
1766                                           SVGA_REG_NUM_GUEST_DISPLAYS);
1767
1768         if (vmw_priv->num_displays == 0)
1769                 vmw_priv->num_displays = 1;
1770
1771         for (i = 0; i < vmw_priv->num_displays; ++i) {
1772                 save = &vmw_priv->vga_save[i];
1773                 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
1774                 save->primary = vmw_read(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY);
1775                 save->pos_x = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_X);
1776                 save->pos_y = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y);
1777                 save->width = vmw_read(vmw_priv, SVGA_REG_DISPLAY_WIDTH);
1778                 save->height = vmw_read(vmw_priv, SVGA_REG_DISPLAY_HEIGHT);
1779                 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
1780                 if (i == 0 && vmw_priv->num_displays == 1 &&
1781                     save->width == 0 && save->height == 0) {
1782
1783                         /*
1784                          * It should be fairly safe to assume that these
1785                          * values are uninitialized.
1786                          */
1787
1788                         save->width = vmw_priv->vga_width - save->pos_x;
1789                         save->height = vmw_priv->vga_height - save->pos_y;
1790                 }
1791         }
1792
1793         return 0;
1794 }
1795
1796 int vmw_kms_restore_vga(struct vmw_private *vmw_priv)
1797 {
1798         struct vmw_vga_topology_state *save;
1799         uint32_t i;
1800
1801         vmw_write(vmw_priv, SVGA_REG_WIDTH, vmw_priv->vga_width);
1802         vmw_write(vmw_priv, SVGA_REG_HEIGHT, vmw_priv->vga_height);
1803         vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, vmw_priv->vga_bpp);
1804         if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1805                 vmw_write(vmw_priv, SVGA_REG_PITCHLOCK,
1806                           vmw_priv->vga_pitchlock);
1807         else if (vmw_fifo_have_pitchlock(vmw_priv))
1808                 vmw_mmio_write(vmw_priv->vga_pitchlock,
1809                                vmw_priv->mmio_virt + SVGA_FIFO_PITCHLOCK);
1810
1811         if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
1812                 return 0;
1813
1814         for (i = 0; i < vmw_priv->num_displays; ++i) {
1815                 save = &vmw_priv->vga_save[i];
1816                 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
1817                 vmw_write(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY, save->primary);
1818                 vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_X, save->pos_x);
1819                 vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y, save->pos_y);
1820                 vmw_write(vmw_priv, SVGA_REG_DISPLAY_WIDTH, save->width);
1821                 vmw_write(vmw_priv, SVGA_REG_DISPLAY_HEIGHT, save->height);
1822                 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
1823         }
1824
1825         return 0;
1826 }
1827
1828 bool vmw_kms_validate_mode_vram(struct vmw_private *dev_priv,
1829                                 uint32_t pitch,
1830                                 uint32_t height)
1831 {
1832         return ((u64) pitch * (u64) height) < (u64)
1833                 ((dev_priv->active_display_unit == vmw_du_screen_target) ?
1834                  dev_priv->prim_bb_mem : dev_priv->vram_size);
1835 }
1836
1837
1838 /**
1839  * Function called by DRM code called with vbl_lock held.
1840  */
1841 u32 vmw_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
1842 {
1843         return 0;
1844 }
1845
1846 /**
1847  * Function called by DRM code called with vbl_lock held.
1848  */
1849 int vmw_enable_vblank(struct drm_device *dev, unsigned int pipe)
1850 {
1851         return -EINVAL;
1852 }
1853
1854 /**
1855  * Function called by DRM code called with vbl_lock held.
1856  */
1857 void vmw_disable_vblank(struct drm_device *dev, unsigned int pipe)
1858 {
1859 }
1860
1861
1862 /*
1863  * Small shared kms functions.
1864  */
1865
1866 static int vmw_du_update_layout(struct vmw_private *dev_priv, unsigned num,
1867                          struct drm_vmw_rect *rects)
1868 {
1869         struct drm_device *dev = dev_priv->dev;
1870         struct vmw_display_unit *du;
1871         struct drm_connector *con;
1872
1873         mutex_lock(&dev->mode_config.mutex);
1874
1875 #if 0
1876         {
1877                 unsigned int i;
1878
1879                 DRM_INFO("%s: new layout ", __func__);
1880                 for (i = 0; i < num; i++)
1881                         DRM_INFO("(%i, %i %ux%u) ", rects[i].x, rects[i].y,
1882                                  rects[i].w, rects[i].h);
1883                 DRM_INFO("\n");
1884         }
1885 #endif
1886
1887         list_for_each_entry(con, &dev->mode_config.connector_list, head) {
1888                 du = vmw_connector_to_du(con);
1889                 if (num > du->unit) {
1890                         du->pref_width = rects[du->unit].w;
1891                         du->pref_height = rects[du->unit].h;
1892                         du->pref_active = true;
1893                         du->gui_x = rects[du->unit].x;
1894                         du->gui_y = rects[du->unit].y;
1895                         drm_object_property_set_value
1896                           (&con->base, dev->mode_config.suggested_x_property,
1897                            du->gui_x);
1898                         drm_object_property_set_value
1899                           (&con->base, dev->mode_config.suggested_y_property,
1900                            du->gui_y);
1901                 } else {
1902                         du->pref_width = 800;
1903                         du->pref_height = 600;
1904                         du->pref_active = false;
1905                         drm_object_property_set_value
1906                           (&con->base, dev->mode_config.suggested_x_property,
1907                            0);
1908                         drm_object_property_set_value
1909                           (&con->base, dev->mode_config.suggested_y_property,
1910                            0);
1911                 }
1912                 con->status = vmw_du_connector_detect(con, true);
1913         }
1914
1915         mutex_unlock(&dev->mode_config.mutex);
1916         drm_sysfs_hotplug_event(dev);
1917
1918         return 0;
1919 }
1920
1921 int vmw_du_crtc_gamma_set(struct drm_crtc *crtc,
1922                           u16 *r, u16 *g, u16 *b,
1923                           uint32_t size,
1924                           struct drm_modeset_acquire_ctx *ctx)
1925 {
1926         struct vmw_private *dev_priv = vmw_priv(crtc->dev);
1927         int i;
1928
1929         for (i = 0; i < size; i++) {
1930                 DRM_DEBUG("%d r/g/b = 0x%04x / 0x%04x / 0x%04x\n", i,
1931                           r[i], g[i], b[i]);
1932                 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 0, r[i] >> 8);
1933                 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 1, g[i] >> 8);
1934                 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 2, b[i] >> 8);
1935         }
1936
1937         return 0;
1938 }
1939
1940 int vmw_du_connector_dpms(struct drm_connector *connector, int mode)
1941 {
1942         return 0;
1943 }
1944
1945 enum drm_connector_status
1946 vmw_du_connector_detect(struct drm_connector *connector, bool force)
1947 {
1948         uint32_t num_displays;
1949         struct drm_device *dev = connector->dev;
1950         struct vmw_private *dev_priv = vmw_priv(dev);
1951         struct vmw_display_unit *du = vmw_connector_to_du(connector);
1952
1953         num_displays = vmw_read(dev_priv, SVGA_REG_NUM_DISPLAYS);
1954
1955         return ((vmw_connector_to_du(connector)->unit < num_displays &&
1956                  du->pref_active) ?
1957                 connector_status_connected : connector_status_disconnected);
1958 }
1959
1960 static struct drm_display_mode vmw_kms_connector_builtin[] = {
1961         /* 640x480@60Hz */
1962         { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 25175, 640, 656,
1963                    752, 800, 0, 480, 489, 492, 525, 0,
1964                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
1965         /* 800x600@60Hz */
1966         { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 40000, 800, 840,
1967                    968, 1056, 0, 600, 601, 605, 628, 0,
1968                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1969         /* 1024x768@60Hz */
1970         { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 65000, 1024, 1048,
1971                    1184, 1344, 0, 768, 771, 777, 806, 0,
1972                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
1973         /* 1152x864@75Hz */
1974         { DRM_MODE("1152x864", DRM_MODE_TYPE_DRIVER, 108000, 1152, 1216,
1975                    1344, 1600, 0, 864, 865, 868, 900, 0,
1976                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1977         /* 1280x768@60Hz */
1978         { DRM_MODE("1280x768", DRM_MODE_TYPE_DRIVER, 79500, 1280, 1344,
1979                    1472, 1664, 0, 768, 771, 778, 798, 0,
1980                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1981         /* 1280x800@60Hz */
1982         { DRM_MODE("1280x800", DRM_MODE_TYPE_DRIVER, 83500, 1280, 1352,
1983                    1480, 1680, 0, 800, 803, 809, 831, 0,
1984                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
1985         /* 1280x960@60Hz */
1986         { DRM_MODE("1280x960", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1376,
1987                    1488, 1800, 0, 960, 961, 964, 1000, 0,
1988                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1989         /* 1280x1024@60Hz */
1990         { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1328,
1991                    1440, 1688, 0, 1024, 1025, 1028, 1066, 0,
1992                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1993         /* 1360x768@60Hz */
1994         { DRM_MODE("1360x768", DRM_MODE_TYPE_DRIVER, 85500, 1360, 1424,
1995                    1536, 1792, 0, 768, 771, 777, 795, 0,
1996                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1997         /* 1440x1050@60Hz */
1998         { DRM_MODE("1400x1050", DRM_MODE_TYPE_DRIVER, 121750, 1400, 1488,
1999                    1632, 1864, 0, 1050, 1053, 1057, 1089, 0,
2000                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2001         /* 1440x900@60Hz */
2002         { DRM_MODE("1440x900", DRM_MODE_TYPE_DRIVER, 106500, 1440, 1520,
2003                    1672, 1904, 0, 900, 903, 909, 934, 0,
2004                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2005         /* 1600x1200@60Hz */
2006         { DRM_MODE("1600x1200", DRM_MODE_TYPE_DRIVER, 162000, 1600, 1664,
2007                    1856, 2160, 0, 1200, 1201, 1204, 1250, 0,
2008                    DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
2009         /* 1680x1050@60Hz */
2010         { DRM_MODE("1680x1050", DRM_MODE_TYPE_DRIVER, 146250, 1680, 1784,
2011                    1960, 2240, 0, 1050, 1053, 1059, 1089, 0,
2012                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2013         /* 1792x1344@60Hz */
2014         { DRM_MODE("1792x1344", DRM_MODE_TYPE_DRIVER, 204750, 1792, 1920,
2015                    2120, 2448, 0, 1344, 1345, 1348, 1394, 0,
2016                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2017         /* 1853x1392@60Hz */
2018         { DRM_MODE("1856x1392", DRM_MODE_TYPE_DRIVER, 218250, 1856, 1952,
2019                    2176, 2528, 0, 1392, 1393, 1396, 1439, 0,
2020                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2021         /* 1920x1200@60Hz */
2022         { DRM_MODE("1920x1200", DRM_MODE_TYPE_DRIVER, 193250, 1920, 2056,
2023                    2256, 2592, 0, 1200, 1203, 1209, 1245, 0,
2024                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2025         /* 1920x1440@60Hz */
2026         { DRM_MODE("1920x1440", DRM_MODE_TYPE_DRIVER, 234000, 1920, 2048,
2027                    2256, 2600, 0, 1440, 1441, 1444, 1500, 0,
2028                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2029         /* 2560x1600@60Hz */
2030         { DRM_MODE("2560x1600", DRM_MODE_TYPE_DRIVER, 348500, 2560, 2752,
2031                    3032, 3504, 0, 1600, 1603, 1609, 1658, 0,
2032                    DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2033         /* Terminate */
2034         { DRM_MODE("", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) },
2035 };
2036
2037 /**
2038  * vmw_guess_mode_timing - Provide fake timings for a
2039  * 60Hz vrefresh mode.
2040  *
2041  * @mode - Pointer to a struct drm_display_mode with hdisplay and vdisplay
2042  * members filled in.
2043  */
2044 void vmw_guess_mode_timing(struct drm_display_mode *mode)
2045 {
2046         mode->hsync_start = mode->hdisplay + 50;
2047         mode->hsync_end = mode->hsync_start + 50;
2048         mode->htotal = mode->hsync_end + 50;
2049
2050         mode->vsync_start = mode->vdisplay + 50;
2051         mode->vsync_end = mode->vsync_start + 50;
2052         mode->vtotal = mode->vsync_end + 50;
2053
2054         mode->clock = (u32)mode->htotal * (u32)mode->vtotal / 100 * 6;
2055         mode->vrefresh = drm_mode_vrefresh(mode);
2056 }
2057
2058
2059 int vmw_du_connector_fill_modes(struct drm_connector *connector,
2060                                 uint32_t max_width, uint32_t max_height)
2061 {
2062         struct vmw_display_unit *du = vmw_connector_to_du(connector);
2063         struct drm_device *dev = connector->dev;
2064         struct vmw_private *dev_priv = vmw_priv(dev);
2065         struct drm_display_mode *mode = NULL;
2066         struct drm_display_mode *bmode;
2067         struct drm_display_mode prefmode = { DRM_MODE("preferred",
2068                 DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED,
2069                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2070                 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC)
2071         };
2072         int i;
2073         u32 assumed_bpp = 4;
2074
2075         if (dev_priv->assume_16bpp)
2076                 assumed_bpp = 2;
2077
2078         if (dev_priv->active_display_unit == vmw_du_screen_target) {
2079                 max_width  = min(max_width,  dev_priv->stdu_max_width);
2080                 max_width  = min(max_width,  dev_priv->texture_max_width);
2081
2082                 max_height = min(max_height, dev_priv->stdu_max_height);
2083                 max_height = min(max_height, dev_priv->texture_max_height);
2084         }
2085
2086         /* Add preferred mode */
2087         mode = drm_mode_duplicate(dev, &prefmode);
2088         if (!mode)
2089                 return 0;
2090         mode->hdisplay = du->pref_width;
2091         mode->vdisplay = du->pref_height;
2092         vmw_guess_mode_timing(mode);
2093
2094         if (vmw_kms_validate_mode_vram(dev_priv,
2095                                         mode->hdisplay * assumed_bpp,
2096                                         mode->vdisplay)) {
2097                 drm_mode_probed_add(connector, mode);
2098         } else {
2099                 drm_mode_destroy(dev, mode);
2100                 mode = NULL;
2101         }
2102
2103         if (du->pref_mode) {
2104                 list_del_init(&du->pref_mode->head);
2105                 drm_mode_destroy(dev, du->pref_mode);
2106         }
2107
2108         /* mode might be null here, this is intended */
2109         du->pref_mode = mode;
2110
2111         for (i = 0; vmw_kms_connector_builtin[i].type != 0; i++) {
2112                 bmode = &vmw_kms_connector_builtin[i];
2113                 if (bmode->hdisplay > max_width ||
2114                     bmode->vdisplay > max_height)
2115                         continue;
2116
2117                 if (!vmw_kms_validate_mode_vram(dev_priv,
2118                                                 bmode->hdisplay * assumed_bpp,
2119                                                 bmode->vdisplay))
2120                         continue;
2121
2122                 mode = drm_mode_duplicate(dev, bmode);
2123                 if (!mode)
2124                         return 0;
2125                 mode->vrefresh = drm_mode_vrefresh(mode);
2126
2127                 drm_mode_probed_add(connector, mode);
2128         }
2129
2130         drm_mode_connector_list_update(connector);
2131         /* Move the prefered mode first, help apps pick the right mode. */
2132         drm_mode_sort(&connector->modes);
2133
2134         return 1;
2135 }
2136
2137 int vmw_du_connector_set_property(struct drm_connector *connector,
2138                                   struct drm_property *property,
2139                                   uint64_t val)
2140 {
2141         struct vmw_display_unit *du = vmw_connector_to_du(connector);
2142         struct vmw_private *dev_priv = vmw_priv(connector->dev);
2143
2144         if (property == dev_priv->implicit_placement_property)
2145                 du->is_implicit = val;
2146
2147         return 0;
2148 }
2149
2150
2151
2152 /**
2153  * vmw_du_connector_atomic_set_property - Atomic version of get property
2154  *
2155  * @crtc - crtc the property is associated with
2156  *
2157  * Returns:
2158  * Zero on success, negative errno on failure.
2159  */
2160 int
2161 vmw_du_connector_atomic_set_property(struct drm_connector *connector,
2162                                      struct drm_connector_state *state,
2163                                      struct drm_property *property,
2164                                      uint64_t val)
2165 {
2166         struct vmw_private *dev_priv = vmw_priv(connector->dev);
2167         struct vmw_connector_state *vcs = vmw_connector_state_to_vcs(state);
2168         struct vmw_display_unit *du = vmw_connector_to_du(connector);
2169
2170
2171         if (property == dev_priv->implicit_placement_property) {
2172                 vcs->is_implicit = val;
2173
2174                 /*
2175                  * We should really be doing a drm_atomic_commit() to
2176                  * commit the new state, but since this doesn't cause
2177                  * an immedate state change, this is probably ok
2178                  */
2179                 du->is_implicit = vcs->is_implicit;
2180         } else {
2181                 return -EINVAL;
2182         }
2183
2184         return 0;
2185 }
2186
2187
2188 /**
2189  * vmw_du_connector_atomic_get_property - Atomic version of get property
2190  *
2191  * @connector - connector the property is associated with
2192  *
2193  * Returns:
2194  * Zero on success, negative errno on failure.
2195  */
2196 int
2197 vmw_du_connector_atomic_get_property(struct drm_connector *connector,
2198                                      const struct drm_connector_state *state,
2199                                      struct drm_property *property,
2200                                      uint64_t *val)
2201 {
2202         struct vmw_private *dev_priv = vmw_priv(connector->dev);
2203         struct vmw_connector_state *vcs = vmw_connector_state_to_vcs(state);
2204
2205         if (property == dev_priv->implicit_placement_property)
2206                 *val = vcs->is_implicit;
2207         else {
2208                 DRM_ERROR("Invalid Property %s\n", property->name);
2209                 return -EINVAL;
2210         }
2211
2212         return 0;
2213 }
2214
2215
2216 int vmw_kms_update_layout_ioctl(struct drm_device *dev, void *data,
2217                                 struct drm_file *file_priv)
2218 {
2219         struct vmw_private *dev_priv = vmw_priv(dev);
2220         struct drm_vmw_update_layout_arg *arg =
2221                 (struct drm_vmw_update_layout_arg *)data;
2222         void __user *user_rects;
2223         struct drm_vmw_rect *rects;
2224         unsigned rects_size;
2225         int ret;
2226         int i;
2227         u64 total_pixels = 0;
2228         struct drm_mode_config *mode_config = &dev->mode_config;
2229         struct drm_vmw_rect bounding_box = {0};
2230
2231         if (!arg->num_outputs) {
2232                 struct drm_vmw_rect def_rect = {0, 0, 800, 600};
2233                 vmw_du_update_layout(dev_priv, 1, &def_rect);
2234                 return 0;
2235         }
2236
2237         rects_size = arg->num_outputs * sizeof(struct drm_vmw_rect);
2238         rects = kcalloc(arg->num_outputs, sizeof(struct drm_vmw_rect),
2239                         GFP_KERNEL);
2240         if (unlikely(!rects))
2241                 return -ENOMEM;
2242
2243         user_rects = (void __user *)(unsigned long)arg->rects;
2244         ret = copy_from_user(rects, user_rects, rects_size);
2245         if (unlikely(ret != 0)) {
2246                 DRM_ERROR("Failed to get rects.\n");
2247                 ret = -EFAULT;
2248                 goto out_free;
2249         }
2250
2251         for (i = 0; i < arg->num_outputs; ++i) {
2252                 if (rects[i].x < 0 ||
2253                     rects[i].y < 0 ||
2254                     rects[i].x + rects[i].w > mode_config->max_width ||
2255                     rects[i].y + rects[i].h > mode_config->max_height) {
2256                         DRM_ERROR("Invalid GUI layout.\n");
2257                         ret = -EINVAL;
2258                         goto out_free;
2259                 }
2260
2261                 /*
2262                  * bounding_box.w and bunding_box.h are used as
2263                  * lower-right coordinates
2264                  */
2265                 if (rects[i].x + rects[i].w > bounding_box.w)
2266                         bounding_box.w = rects[i].x + rects[i].w;
2267
2268                 if (rects[i].y + rects[i].h > bounding_box.h)
2269                         bounding_box.h = rects[i].y + rects[i].h;
2270
2271                 total_pixels += (u64) rects[i].w * (u64) rects[i].h;
2272         }
2273
2274         if (dev_priv->active_display_unit == vmw_du_screen_target) {
2275                 /*
2276                  * For Screen Targets, the limits for a toplogy are:
2277                  *      1. Bounding box (assuming 32bpp) must be < prim_bb_mem
2278                  *      2. Total pixels (assuming 32bpp) must be < prim_bb_mem
2279                  */
2280                 u64 bb_mem    = (u64) bounding_box.w * bounding_box.h * 4;
2281                 u64 pixel_mem = total_pixels * 4;
2282
2283                 if (bb_mem > dev_priv->prim_bb_mem) {
2284                         DRM_ERROR("Topology is beyond supported limits.\n");
2285                         ret = -EINVAL;
2286                         goto out_free;
2287                 }
2288
2289                 if (pixel_mem > dev_priv->prim_bb_mem) {
2290                         DRM_ERROR("Combined output size too large\n");
2291                         ret = -EINVAL;
2292                         goto out_free;
2293                 }
2294         }
2295
2296         vmw_du_update_layout(dev_priv, arg->num_outputs, rects);
2297
2298 out_free:
2299         kfree(rects);
2300         return ret;
2301 }
2302
2303 /**
2304  * vmw_kms_helper_dirty - Helper to build commands and perform actions based
2305  * on a set of cliprects and a set of display units.
2306  *
2307  * @dev_priv: Pointer to a device private structure.
2308  * @framebuffer: Pointer to the framebuffer on which to perform the actions.
2309  * @clips: A set of struct drm_clip_rect. Either this os @vclips must be NULL.
2310  * Cliprects are given in framebuffer coordinates.
2311  * @vclips: A set of struct drm_vmw_rect cliprects. Either this or @clips must
2312  * be NULL. Cliprects are given in source coordinates.
2313  * @dest_x: X coordinate offset for the crtc / destination clip rects.
2314  * @dest_y: Y coordinate offset for the crtc / destination clip rects.
2315  * @num_clips: Number of cliprects in the @clips or @vclips array.
2316  * @increment: Integer with which to increment the clip counter when looping.
2317  * Used to skip a predetermined number of clip rects.
2318  * @dirty: Closure structure. See the description of struct vmw_kms_dirty.
2319  */
2320 int vmw_kms_helper_dirty(struct vmw_private *dev_priv,
2321                          struct vmw_framebuffer *framebuffer,
2322                          const struct drm_clip_rect *clips,
2323                          const struct drm_vmw_rect *vclips,
2324                          s32 dest_x, s32 dest_y,
2325                          int num_clips,
2326                          int increment,
2327                          struct vmw_kms_dirty *dirty)
2328 {
2329         struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
2330         struct drm_crtc *crtc;
2331         u32 num_units = 0;
2332         u32 i, k;
2333
2334         dirty->dev_priv = dev_priv;
2335
2336         list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list, head) {
2337                 if (crtc->primary->fb != &framebuffer->base)
2338                         continue;
2339                 units[num_units++] = vmw_crtc_to_du(crtc);
2340         }
2341
2342         for (k = 0; k < num_units; k++) {
2343                 struct vmw_display_unit *unit = units[k];
2344                 s32 crtc_x = unit->crtc.x;
2345                 s32 crtc_y = unit->crtc.y;
2346                 s32 crtc_width = unit->crtc.mode.hdisplay;
2347                 s32 crtc_height = unit->crtc.mode.vdisplay;
2348                 const struct drm_clip_rect *clips_ptr = clips;
2349                 const struct drm_vmw_rect *vclips_ptr = vclips;
2350
2351                 dirty->unit = unit;
2352                 if (dirty->fifo_reserve_size > 0) {
2353                         dirty->cmd = vmw_fifo_reserve(dev_priv,
2354                                                       dirty->fifo_reserve_size);
2355                         if (!dirty->cmd) {
2356                                 DRM_ERROR("Couldn't reserve fifo space "
2357                                           "for dirty blits.\n");
2358                                 return -ENOMEM;
2359                         }
2360                         memset(dirty->cmd, 0, dirty->fifo_reserve_size);
2361                 }
2362                 dirty->num_hits = 0;
2363                 for (i = 0; i < num_clips; i++, clips_ptr += increment,
2364                        vclips_ptr += increment) {
2365                         s32 clip_left;
2366                         s32 clip_top;
2367
2368                         /*
2369                          * Select clip array type. Note that integer type
2370                          * in @clips is unsigned short, whereas in @vclips
2371                          * it's 32-bit.
2372                          */
2373                         if (clips) {
2374                                 dirty->fb_x = (s32) clips_ptr->x1;
2375                                 dirty->fb_y = (s32) clips_ptr->y1;
2376                                 dirty->unit_x2 = (s32) clips_ptr->x2 + dest_x -
2377                                         crtc_x;
2378                                 dirty->unit_y2 = (s32) clips_ptr->y2 + dest_y -
2379                                         crtc_y;
2380                         } else {
2381                                 dirty->fb_x = vclips_ptr->x;
2382                                 dirty->fb_y = vclips_ptr->y;
2383                                 dirty->unit_x2 = dirty->fb_x + vclips_ptr->w +
2384                                         dest_x - crtc_x;
2385                                 dirty->unit_y2 = dirty->fb_y + vclips_ptr->h +
2386                                         dest_y - crtc_y;
2387                         }
2388
2389                         dirty->unit_x1 = dirty->fb_x + dest_x - crtc_x;
2390                         dirty->unit_y1 = dirty->fb_y + dest_y - crtc_y;
2391
2392                         /* Skip this clip if it's outside the crtc region */
2393                         if (dirty->unit_x1 >= crtc_width ||
2394                             dirty->unit_y1 >= crtc_height ||
2395                             dirty->unit_x2 <= 0 || dirty->unit_y2 <= 0)
2396                                 continue;
2397
2398                         /* Clip right and bottom to crtc limits */
2399                         dirty->unit_x2 = min_t(s32, dirty->unit_x2,
2400                                                crtc_width);
2401                         dirty->unit_y2 = min_t(s32, dirty->unit_y2,
2402                                                crtc_height);
2403
2404                         /* Clip left and top to crtc limits */
2405                         clip_left = min_t(s32, dirty->unit_x1, 0);
2406                         clip_top = min_t(s32, dirty->unit_y1, 0);
2407                         dirty->unit_x1 -= clip_left;
2408                         dirty->unit_y1 -= clip_top;
2409                         dirty->fb_x -= clip_left;
2410                         dirty->fb_y -= clip_top;
2411
2412                         dirty->clip(dirty);
2413                 }
2414
2415                 dirty->fifo_commit(dirty);
2416         }
2417
2418         return 0;
2419 }
2420
2421 /**
2422  * vmw_kms_helper_buffer_prepare - Reserve and validate a buffer object before
2423  * command submission.
2424  *
2425  * @dev_priv. Pointer to a device private structure.
2426  * @buf: The buffer object
2427  * @interruptible: Whether to perform waits as interruptible.
2428  * @validate_as_mob: Whether the buffer should be validated as a MOB. If false,
2429  * The buffer will be validated as a GMR. Already pinned buffers will not be
2430  * validated.
2431  *
2432  * Returns 0 on success, negative error code on failure, -ERESTARTSYS if
2433  * interrupted by a signal.
2434  */
2435 int vmw_kms_helper_buffer_prepare(struct vmw_private *dev_priv,
2436                                   struct vmw_dma_buffer *buf,
2437                                   bool interruptible,
2438                                   bool validate_as_mob)
2439 {
2440         struct ttm_buffer_object *bo = &buf->base;
2441         int ret;
2442
2443         ttm_bo_reserve(bo, false, false, NULL);
2444         ret = vmw_validate_single_buffer(dev_priv, bo, interruptible,
2445                                          validate_as_mob);
2446         if (ret)
2447                 ttm_bo_unreserve(bo);
2448
2449         return ret;
2450 }
2451
2452 /**
2453  * vmw_kms_helper_buffer_revert - Undo the actions of
2454  * vmw_kms_helper_buffer_prepare.
2455  *
2456  * @res: Pointer to the buffer object.
2457  *
2458  * Helper to be used if an error forces the caller to undo the actions of
2459  * vmw_kms_helper_buffer_prepare.
2460  */
2461 void vmw_kms_helper_buffer_revert(struct vmw_dma_buffer *buf)
2462 {
2463         if (buf)
2464                 ttm_bo_unreserve(&buf->base);
2465 }
2466
2467 /**
2468  * vmw_kms_helper_buffer_finish - Unreserve and fence a buffer object after
2469  * kms command submission.
2470  *
2471  * @dev_priv: Pointer to a device private structure.
2472  * @file_priv: Pointer to a struct drm_file representing the caller's
2473  * connection. Must be set to NULL if @user_fence_rep is NULL, and conversely
2474  * if non-NULL, @user_fence_rep must be non-NULL.
2475  * @buf: The buffer object.
2476  * @out_fence:  Optional pointer to a fence pointer. If non-NULL, a
2477  * ref-counted fence pointer is returned here.
2478  * @user_fence_rep: Optional pointer to a user-space provided struct
2479  * drm_vmw_fence_rep. If provided, @file_priv must also be provided and the
2480  * function copies fence data to user-space in a fail-safe manner.
2481  */
2482 void vmw_kms_helper_buffer_finish(struct vmw_private *dev_priv,
2483                                   struct drm_file *file_priv,
2484                                   struct vmw_dma_buffer *buf,
2485                                   struct vmw_fence_obj **out_fence,
2486                                   struct drm_vmw_fence_rep __user *
2487                                   user_fence_rep)
2488 {
2489         struct vmw_fence_obj *fence;
2490         uint32_t handle;
2491         int ret;
2492
2493         ret = vmw_execbuf_fence_commands(file_priv, dev_priv, &fence,
2494                                          file_priv ? &handle : NULL);
2495         if (buf)
2496                 vmw_fence_single_bo(&buf->base, fence);
2497         if (file_priv)
2498                 vmw_execbuf_copy_fence_user(dev_priv, vmw_fpriv(file_priv),
2499                                             ret, user_fence_rep, fence,
2500                                             handle, -1, NULL);
2501         if (out_fence)
2502                 *out_fence = fence;
2503         else
2504                 vmw_fence_obj_unreference(&fence);
2505
2506         vmw_kms_helper_buffer_revert(buf);
2507 }
2508
2509
2510 /**
2511  * vmw_kms_helper_resource_revert - Undo the actions of
2512  * vmw_kms_helper_resource_prepare.
2513  *
2514  * @res: Pointer to the resource. Typically a surface.
2515  *
2516  * Helper to be used if an error forces the caller to undo the actions of
2517  * vmw_kms_helper_resource_prepare.
2518  */
2519 void vmw_kms_helper_resource_revert(struct vmw_resource *res)
2520 {
2521         vmw_kms_helper_buffer_revert(res->backup);
2522         vmw_resource_unreserve(res, false, NULL, 0);
2523         mutex_unlock(&res->dev_priv->cmdbuf_mutex);
2524 }
2525
2526 /**
2527  * vmw_kms_helper_resource_prepare - Reserve and validate a resource before
2528  * command submission.
2529  *
2530  * @res: Pointer to the resource. Typically a surface.
2531  * @interruptible: Whether to perform waits as interruptible.
2532  *
2533  * Reserves and validates also the backup buffer if a guest-backed resource.
2534  * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
2535  * interrupted by a signal.
2536  */
2537 int vmw_kms_helper_resource_prepare(struct vmw_resource *res,
2538                                     bool interruptible)
2539 {
2540         int ret = 0;
2541
2542         if (interruptible)
2543                 ret = mutex_lock_interruptible(&res->dev_priv->cmdbuf_mutex);
2544         else
2545                 mutex_lock(&res->dev_priv->cmdbuf_mutex);
2546
2547         if (unlikely(ret != 0))
2548                 return -ERESTARTSYS;
2549
2550         ret = vmw_resource_reserve(res, interruptible, false);
2551         if (ret)
2552                 goto out_unlock;
2553
2554         if (res->backup) {
2555                 ret = vmw_kms_helper_buffer_prepare(res->dev_priv, res->backup,
2556                                                     interruptible,
2557                                                     res->dev_priv->has_mob);
2558                 if (ret)
2559                         goto out_unreserve;
2560         }
2561         ret = vmw_resource_validate(res);
2562         if (ret)
2563                 goto out_revert;
2564         return 0;
2565
2566 out_revert:
2567         vmw_kms_helper_buffer_revert(res->backup);
2568 out_unreserve:
2569         vmw_resource_unreserve(res, false, NULL, 0);
2570 out_unlock:
2571         mutex_unlock(&res->dev_priv->cmdbuf_mutex);
2572         return ret;
2573 }
2574
2575 /**
2576  * vmw_kms_helper_resource_finish - Unreserve and fence a resource after
2577  * kms command submission.
2578  *
2579  * @res: Pointer to the resource. Typically a surface.
2580  * @out_fence: Optional pointer to a fence pointer. If non-NULL, a
2581  * ref-counted fence pointer is returned here.
2582  */
2583 void vmw_kms_helper_resource_finish(struct vmw_resource *res,
2584                              struct vmw_fence_obj **out_fence)
2585 {
2586         if (res->backup || out_fence)
2587                 vmw_kms_helper_buffer_finish(res->dev_priv, NULL, res->backup,
2588                                              out_fence, NULL);
2589
2590         vmw_resource_unreserve(res, false, NULL, 0);
2591         mutex_unlock(&res->dev_priv->cmdbuf_mutex);
2592 }
2593
2594 /**
2595  * vmw_kms_update_proxy - Helper function to update a proxy surface from
2596  * its backing MOB.
2597  *
2598  * @res: Pointer to the surface resource
2599  * @clips: Clip rects in framebuffer (surface) space.
2600  * @num_clips: Number of clips in @clips.
2601  * @increment: Integer with which to increment the clip counter when looping.
2602  * Used to skip a predetermined number of clip rects.
2603  *
2604  * This function makes sure the proxy surface is updated from its backing MOB
2605  * using the region given by @clips. The surface resource @res and its backing
2606  * MOB needs to be reserved and validated on call.
2607  */
2608 int vmw_kms_update_proxy(struct vmw_resource *res,
2609                          const struct drm_clip_rect *clips,
2610                          unsigned num_clips,
2611                          int increment)
2612 {
2613         struct vmw_private *dev_priv = res->dev_priv;
2614         struct drm_vmw_size *size = &vmw_res_to_srf(res)->base_size;
2615         struct {
2616                 SVGA3dCmdHeader header;
2617                 SVGA3dCmdUpdateGBImage body;
2618         } *cmd;
2619         SVGA3dBox *box;
2620         size_t copy_size = 0;
2621         int i;
2622
2623         if (!clips)
2624                 return 0;
2625
2626         cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd) * num_clips);
2627         if (!cmd) {
2628                 DRM_ERROR("Couldn't reserve fifo space for proxy surface "
2629                           "update.\n");
2630                 return -ENOMEM;
2631         }
2632
2633         for (i = 0; i < num_clips; ++i, clips += increment, ++cmd) {
2634                 box = &cmd->body.box;
2635
2636                 cmd->header.id = SVGA_3D_CMD_UPDATE_GB_IMAGE;
2637                 cmd->header.size = sizeof(cmd->body);
2638                 cmd->body.image.sid = res->id;
2639                 cmd->body.image.face = 0;
2640                 cmd->body.image.mipmap = 0;
2641
2642                 if (clips->x1 > size->width || clips->x2 > size->width ||
2643                     clips->y1 > size->height || clips->y2 > size->height) {
2644                         DRM_ERROR("Invalid clips outsize of framebuffer.\n");
2645                         return -EINVAL;
2646                 }
2647
2648                 box->x = clips->x1;
2649                 box->y = clips->y1;
2650                 box->z = 0;
2651                 box->w = clips->x2 - clips->x1;
2652                 box->h = clips->y2 - clips->y1;
2653                 box->d = 1;
2654
2655                 copy_size += sizeof(*cmd);
2656         }
2657
2658         vmw_fifo_commit(dev_priv, copy_size);
2659
2660         return 0;
2661 }
2662
2663 int vmw_kms_fbdev_init_data(struct vmw_private *dev_priv,
2664                             unsigned unit,
2665                             u32 max_width,
2666                             u32 max_height,
2667                             struct drm_connector **p_con,
2668                             struct drm_crtc **p_crtc,
2669                             struct drm_display_mode **p_mode)
2670 {
2671         struct drm_connector *con;
2672         struct vmw_display_unit *du;
2673         struct drm_display_mode *mode;
2674         int i = 0;
2675
2676         list_for_each_entry(con, &dev_priv->dev->mode_config.connector_list,
2677                             head) {
2678                 if (i == unit)
2679                         break;
2680
2681                 ++i;
2682         }
2683
2684         if (i != unit) {
2685                 DRM_ERROR("Could not find initial display unit.\n");
2686                 return -EINVAL;
2687         }
2688
2689         if (list_empty(&con->modes))
2690                 (void) vmw_du_connector_fill_modes(con, max_width, max_height);
2691
2692         if (list_empty(&con->modes)) {
2693                 DRM_ERROR("Could not find initial display mode.\n");
2694                 return -EINVAL;
2695         }
2696
2697         du = vmw_connector_to_du(con);
2698         *p_con = con;
2699         *p_crtc = &du->crtc;
2700
2701         list_for_each_entry(mode, &con->modes, head) {
2702                 if (mode->type & DRM_MODE_TYPE_PREFERRED)
2703                         break;
2704         }
2705
2706         if (mode->type & DRM_MODE_TYPE_PREFERRED)
2707                 *p_mode = mode;
2708         else {
2709                 WARN_ONCE(true, "Could not find initial preferred mode.\n");
2710                 *p_mode = list_first_entry(&con->modes,
2711                                            struct drm_display_mode,
2712                                            head);
2713         }
2714
2715         return 0;
2716 }
2717
2718 /**
2719  * vmw_kms_del_active - unregister a crtc binding to the implicit framebuffer
2720  *
2721  * @dev_priv: Pointer to a device private struct.
2722  * @du: The display unit of the crtc.
2723  */
2724 void vmw_kms_del_active(struct vmw_private *dev_priv,
2725                         struct vmw_display_unit *du)
2726 {
2727         mutex_lock(&dev_priv->global_kms_state_mutex);
2728         if (du->active_implicit) {
2729                 if (--(dev_priv->num_implicit) == 0)
2730                         dev_priv->implicit_fb = NULL;
2731                 du->active_implicit = false;
2732         }
2733         mutex_unlock(&dev_priv->global_kms_state_mutex);
2734 }
2735
2736 /**
2737  * vmw_kms_add_active - register a crtc binding to an implicit framebuffer
2738  *
2739  * @vmw_priv: Pointer to a device private struct.
2740  * @du: The display unit of the crtc.
2741  * @vfb: The implicit framebuffer
2742  *
2743  * Registers a binding to an implicit framebuffer.
2744  */
2745 void vmw_kms_add_active(struct vmw_private *dev_priv,
2746                         struct vmw_display_unit *du,
2747                         struct vmw_framebuffer *vfb)
2748 {
2749         mutex_lock(&dev_priv->global_kms_state_mutex);
2750         WARN_ON_ONCE(!dev_priv->num_implicit && dev_priv->implicit_fb);
2751
2752         if (!du->active_implicit && du->is_implicit) {
2753                 dev_priv->implicit_fb = vfb;
2754                 du->active_implicit = true;
2755                 dev_priv->num_implicit++;
2756         }
2757         mutex_unlock(&dev_priv->global_kms_state_mutex);
2758 }
2759
2760 /**
2761  * vmw_kms_screen_object_flippable - Check whether we can page-flip a crtc.
2762  *
2763  * @dev_priv: Pointer to device-private struct.
2764  * @crtc: The crtc we want to flip.
2765  *
2766  * Returns true or false depending whether it's OK to flip this crtc
2767  * based on the criterion that we must not have more than one implicit
2768  * frame-buffer at any one time.
2769  */
2770 bool vmw_kms_crtc_flippable(struct vmw_private *dev_priv,
2771                             struct drm_crtc *crtc)
2772 {
2773         struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
2774         bool ret;
2775
2776         mutex_lock(&dev_priv->global_kms_state_mutex);
2777         ret = !du->is_implicit || dev_priv->num_implicit == 1;
2778         mutex_unlock(&dev_priv->global_kms_state_mutex);
2779
2780         return ret;
2781 }
2782
2783 /**
2784  * vmw_kms_update_implicit_fb - Update the implicit fb.
2785  *
2786  * @dev_priv: Pointer to device-private struct.
2787  * @crtc: The crtc the new implicit frame-buffer is bound to.
2788  */
2789 void vmw_kms_update_implicit_fb(struct vmw_private *dev_priv,
2790                                 struct drm_crtc *crtc)
2791 {
2792         struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
2793         struct vmw_framebuffer *vfb;
2794
2795         mutex_lock(&dev_priv->global_kms_state_mutex);
2796
2797         if (!du->is_implicit)
2798                 goto out_unlock;
2799
2800         vfb = vmw_framebuffer_to_vfb(crtc->primary->fb);
2801         WARN_ON_ONCE(dev_priv->num_implicit != 1 &&
2802                      dev_priv->implicit_fb != vfb);
2803
2804         dev_priv->implicit_fb = vfb;
2805 out_unlock:
2806         mutex_unlock(&dev_priv->global_kms_state_mutex);
2807 }
2808
2809 /**
2810  * vmw_kms_create_implicit_placement_proparty - Set up the implicit placement
2811  * property.
2812  *
2813  * @dev_priv: Pointer to a device private struct.
2814  * @immutable: Whether the property is immutable.
2815  *
2816  * Sets up the implicit placement property unless it's already set up.
2817  */
2818 void
2819 vmw_kms_create_implicit_placement_property(struct vmw_private *dev_priv,
2820                                            bool immutable)
2821 {
2822         if (dev_priv->implicit_placement_property)
2823                 return;
2824
2825         dev_priv->implicit_placement_property =
2826                 drm_property_create_range(dev_priv->dev,
2827                                           immutable ?
2828                                           DRM_MODE_PROP_IMMUTABLE : 0,
2829                                           "implicit_placement", 0, 1);
2830
2831 }
2832
2833
2834 /**
2835  * vmw_kms_set_config - Wrapper around drm_atomic_helper_set_config
2836  *
2837  * @set: The configuration to set.
2838  *
2839  * The vmwgfx Xorg driver doesn't assign the mode::type member, which
2840  * when drm_mode_set_crtcinfo is called as part of the configuration setting
2841  * causes it to return incorrect crtc dimensions causing severe problems in
2842  * the vmwgfx modesetting. So explicitly clear that member before calling
2843  * into drm_atomic_helper_set_config.
2844  */
2845 int vmw_kms_set_config(struct drm_mode_set *set,
2846                        struct drm_modeset_acquire_ctx *ctx)
2847 {
2848         if (set && set->mode)
2849                 set->mode->type = 0;
2850
2851         return drm_atomic_helper_set_config(set, ctx);
2852 }