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