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