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