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