]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/gpu/drm/vmwgfx/vmwgfx_scrn.c
apple-gmux: Don't switch external DP port on 2011+ MacBook Pros
[karo-tx-linux.git] / drivers / gpu / drm / vmwgfx / vmwgfx_scrn.c
1 /**************************************************************************
2  *
3  * Copyright © 2011-2015 VMware, Inc., Palo Alto, CA., USA
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sub license, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24  * USE OR OTHER DEALINGS IN THE SOFTWARE.
25  *
26  **************************************************************************/
27
28 #include "vmwgfx_kms.h"
29 #include <drm/drm_plane_helper.h>
30
31
32 #define vmw_crtc_to_sou(x) \
33         container_of(x, struct vmw_screen_object_unit, base.crtc)
34 #define vmw_encoder_to_sou(x) \
35         container_of(x, struct vmw_screen_object_unit, base.encoder)
36 #define vmw_connector_to_sou(x) \
37         container_of(x, struct vmw_screen_object_unit, base.connector)
38
39 /**
40  * struct vmw_kms_sou_surface_dirty - Closure structure for
41  * blit surface to screen command.
42  * @base: The base type we derive from. Used by vmw_kms_helper_dirty().
43  * @left: Left side of bounding box.
44  * @right: Right side of bounding box.
45  * @top: Top side of bounding box.
46  * @bottom: Bottom side of bounding box.
47  * @dst_x: Difference between source clip rects and framebuffer coordinates.
48  * @dst_y: Difference between source clip rects and framebuffer coordinates.
49  * @sid: Surface id of surface to copy from.
50  */
51 struct vmw_kms_sou_surface_dirty {
52         struct vmw_kms_dirty base;
53         s32 left, right, top, bottom;
54         s32 dst_x, dst_y;
55         u32 sid;
56 };
57
58 /*
59  * SVGA commands that are used by this code. Please see the device headers
60  * for explanation.
61  */
62 struct vmw_kms_sou_readback_blit {
63         uint32 header;
64         SVGAFifoCmdBlitScreenToGMRFB body;
65 };
66
67 struct vmw_kms_sou_dmabuf_blit {
68         uint32 header;
69         SVGAFifoCmdBlitGMRFBToScreen body;
70 };
71
72 struct vmw_kms_sou_dirty_cmd {
73         SVGA3dCmdHeader header;
74         SVGA3dCmdBlitSurfaceToScreen body;
75 };
76
77 /**
78  * Display unit using screen objects.
79  */
80 struct vmw_screen_object_unit {
81         struct vmw_display_unit base;
82
83         unsigned long buffer_size; /**< Size of allocated buffer */
84         struct vmw_dma_buffer *buffer; /**< Backing store buffer */
85
86         bool defined;
87 };
88
89 static void vmw_sou_destroy(struct vmw_screen_object_unit *sou)
90 {
91         vmw_du_cleanup(&sou->base);
92         kfree(sou);
93 }
94
95
96 /*
97  * Screen Object Display Unit CRTC functions
98  */
99
100 static void vmw_sou_crtc_destroy(struct drm_crtc *crtc)
101 {
102         vmw_sou_destroy(vmw_crtc_to_sou(crtc));
103 }
104
105 /**
106  * Send the fifo command to create a screen.
107  */
108 static int vmw_sou_fifo_create(struct vmw_private *dev_priv,
109                                struct vmw_screen_object_unit *sou,
110                                uint32_t x, uint32_t y,
111                                struct drm_display_mode *mode)
112 {
113         size_t fifo_size;
114
115         struct {
116                 struct {
117                         uint32_t cmdType;
118                 } header;
119                 SVGAScreenObject obj;
120         } *cmd;
121
122         BUG_ON(!sou->buffer);
123
124         fifo_size = sizeof(*cmd);
125         cmd = vmw_fifo_reserve(dev_priv, fifo_size);
126         /* The hardware has hung, nothing we can do about it here. */
127         if (unlikely(cmd == NULL)) {
128                 DRM_ERROR("Fifo reserve failed.\n");
129                 return -ENOMEM;
130         }
131
132         memset(cmd, 0, fifo_size);
133         cmd->header.cmdType = SVGA_CMD_DEFINE_SCREEN;
134         cmd->obj.structSize = sizeof(SVGAScreenObject);
135         cmd->obj.id = sou->base.unit;
136         cmd->obj.flags = SVGA_SCREEN_HAS_ROOT |
137                 (sou->base.unit == 0 ? SVGA_SCREEN_IS_PRIMARY : 0);
138         cmd->obj.size.width = mode->hdisplay;
139         cmd->obj.size.height = mode->vdisplay;
140         if (sou->base.is_implicit) {
141                 cmd->obj.root.x = x;
142                 cmd->obj.root.y = y;
143         } else {
144                 cmd->obj.root.x = sou->base.gui_x;
145                 cmd->obj.root.y = sou->base.gui_y;
146         }
147         sou->base.set_gui_x = cmd->obj.root.x;
148         sou->base.set_gui_y = cmd->obj.root.y;
149
150         /* Ok to assume that buffer is pinned in vram */
151         vmw_bo_get_guest_ptr(&sou->buffer->base, &cmd->obj.backingStore.ptr);
152         cmd->obj.backingStore.pitch = mode->hdisplay * 4;
153
154         vmw_fifo_commit(dev_priv, fifo_size);
155
156         sou->defined = true;
157
158         return 0;
159 }
160
161 /**
162  * Send the fifo command to destroy a screen.
163  */
164 static int vmw_sou_fifo_destroy(struct vmw_private *dev_priv,
165                                 struct vmw_screen_object_unit *sou)
166 {
167         size_t fifo_size;
168         int ret;
169
170         struct {
171                 struct {
172                         uint32_t cmdType;
173                 } header;
174                 SVGAFifoCmdDestroyScreen body;
175         } *cmd;
176
177         /* no need to do anything */
178         if (unlikely(!sou->defined))
179                 return 0;
180
181         fifo_size = sizeof(*cmd);
182         cmd = vmw_fifo_reserve(dev_priv, fifo_size);
183         /* the hardware has hung, nothing we can do about it here */
184         if (unlikely(cmd == NULL)) {
185                 DRM_ERROR("Fifo reserve failed.\n");
186                 return -ENOMEM;
187         }
188
189         memset(cmd, 0, fifo_size);
190         cmd->header.cmdType = SVGA_CMD_DESTROY_SCREEN;
191         cmd->body.screenId = sou->base.unit;
192
193         vmw_fifo_commit(dev_priv, fifo_size);
194
195         /* Force sync */
196         ret = vmw_fallback_wait(dev_priv, false, true, 0, false, 3*HZ);
197         if (unlikely(ret != 0))
198                 DRM_ERROR("Failed to sync with HW");
199         else
200                 sou->defined = false;
201
202         return ret;
203 }
204
205 /**
206  * Free the backing store.
207  */
208 static void vmw_sou_backing_free(struct vmw_private *dev_priv,
209                                  struct vmw_screen_object_unit *sou)
210 {
211         vmw_dmabuf_unreference(&sou->buffer);
212         sou->buffer_size = 0;
213 }
214
215 /**
216  * Allocate the backing store for the buffer.
217  */
218 static int vmw_sou_backing_alloc(struct vmw_private *dev_priv,
219                                  struct vmw_screen_object_unit *sou,
220                                  unsigned long size)
221 {
222         int ret;
223
224         if (sou->buffer_size == size)
225                 return 0;
226
227         if (sou->buffer)
228                 vmw_sou_backing_free(dev_priv, sou);
229
230         sou->buffer = kzalloc(sizeof(*sou->buffer), GFP_KERNEL);
231         if (unlikely(sou->buffer == NULL))
232                 return -ENOMEM;
233
234         /* After we have alloced the backing store might not be able to
235          * resume the overlays, this is preferred to failing to alloc.
236          */
237         vmw_overlay_pause_all(dev_priv);
238         ret = vmw_dmabuf_init(dev_priv, sou->buffer, size,
239                               &vmw_vram_ne_placement,
240                               false, &vmw_dmabuf_bo_free);
241         vmw_overlay_resume_all(dev_priv);
242
243         if (unlikely(ret != 0))
244                 sou->buffer = NULL; /* vmw_dmabuf_init frees on error */
245         else
246                 sou->buffer_size = size;
247
248         return ret;
249 }
250
251 static int vmw_sou_crtc_set_config(struct drm_mode_set *set,
252                                    struct drm_modeset_acquire_ctx *ctx)
253 {
254         struct vmw_private *dev_priv;
255         struct vmw_screen_object_unit *sou;
256         struct drm_connector *connector;
257         struct drm_display_mode *mode;
258         struct drm_encoder *encoder;
259         struct vmw_framebuffer *vfb;
260         struct drm_framebuffer *fb;
261         struct drm_crtc *crtc;
262         int ret = 0;
263
264         if (!set)
265                 return -EINVAL;
266
267         if (!set->crtc)
268                 return -EINVAL;
269
270         /* get the sou */
271         crtc = set->crtc;
272         sou = vmw_crtc_to_sou(crtc);
273         vfb = set->fb ? vmw_framebuffer_to_vfb(set->fb) : NULL;
274         dev_priv = vmw_priv(crtc->dev);
275
276         if (set->num_connectors > 1) {
277                 DRM_ERROR("Too many connectors\n");
278                 return -EINVAL;
279         }
280
281         if (set->num_connectors == 1 &&
282             set->connectors[0] != &sou->base.connector) {
283                 DRM_ERROR("Connector doesn't match %p %p\n",
284                         set->connectors[0], &sou->base.connector);
285                 return -EINVAL;
286         }
287
288         /* Only one active implicit frame-buffer at a time. */
289         mutex_lock(&dev_priv->global_kms_state_mutex);
290         if (sou->base.is_implicit &&
291             dev_priv->implicit_fb && vfb &&
292             !(dev_priv->num_implicit == 1 &&
293               sou->base.active_implicit) &&
294             dev_priv->implicit_fb != vfb) {
295                 mutex_unlock(&dev_priv->global_kms_state_mutex);
296                 DRM_ERROR("Multiple implicit framebuffers not supported.\n");
297                 return -EINVAL;
298         }
299         mutex_unlock(&dev_priv->global_kms_state_mutex);
300
301         /* since they always map one to one these are safe */
302         connector = &sou->base.connector;
303         encoder = &sou->base.encoder;
304
305         /* should we turn the crtc off */
306         if (set->num_connectors == 0 || !set->mode || !set->fb) {
307                 ret = vmw_sou_fifo_destroy(dev_priv, sou);
308                 /* the hardware has hung don't do anything more */
309                 if (unlikely(ret != 0))
310                         return ret;
311
312                 connector->encoder = NULL;
313                 encoder->crtc = NULL;
314                 crtc->primary->fb = NULL;
315                 crtc->x = 0;
316                 crtc->y = 0;
317                 crtc->enabled = false;
318
319                 vmw_kms_del_active(dev_priv, &sou->base);
320
321                 vmw_sou_backing_free(dev_priv, sou);
322
323                 return 0;
324         }
325
326
327         /* we now know we want to set a mode */
328         mode = set->mode;
329         fb = set->fb;
330
331         if (set->x + mode->hdisplay > fb->width ||
332             set->y + mode->vdisplay > fb->height) {
333                 DRM_ERROR("set outside of framebuffer\n");
334                 return -EINVAL;
335         }
336
337         vmw_svga_enable(dev_priv);
338
339         if (mode->hdisplay != crtc->mode.hdisplay ||
340             mode->vdisplay != crtc->mode.vdisplay) {
341                 /* no need to check if depth is different, because backing
342                  * store depth is forced to 4 by the device.
343                  */
344
345                 ret = vmw_sou_fifo_destroy(dev_priv, sou);
346                 /* the hardware has hung don't do anything more */
347                 if (unlikely(ret != 0))
348                         return ret;
349
350                 vmw_sou_backing_free(dev_priv, sou);
351         }
352
353         if (!sou->buffer) {
354                 /* forced to depth 4 by the device */
355                 size_t size = mode->hdisplay * mode->vdisplay * 4;
356                 ret = vmw_sou_backing_alloc(dev_priv, sou, size);
357                 if (unlikely(ret != 0))
358                         return ret;
359         }
360
361         ret = vmw_sou_fifo_create(dev_priv, sou, set->x, set->y, mode);
362         if (unlikely(ret != 0)) {
363                 /*
364                  * We are in a bit of a situation here, the hardware has
365                  * hung and we may or may not have a buffer hanging of
366                  * the screen object, best thing to do is not do anything
367                  * if we where defined, if not just turn the crtc of.
368                  * Not what userspace wants but it needs to htfu.
369                  */
370                 if (sou->defined)
371                         return ret;
372
373                 connector->encoder = NULL;
374                 encoder->crtc = NULL;
375                 crtc->primary->fb = NULL;
376                 crtc->x = 0;
377                 crtc->y = 0;
378                 crtc->enabled = false;
379
380                 return ret;
381         }
382
383         vmw_kms_add_active(dev_priv, &sou->base, vfb);
384
385         connector->encoder = encoder;
386         encoder->crtc = crtc;
387         crtc->mode = *mode;
388         crtc->primary->fb = fb;
389         crtc->x = set->x;
390         crtc->y = set->y;
391         crtc->enabled = true;
392
393         return 0;
394 }
395
396 static int vmw_sou_crtc_page_flip(struct drm_crtc *crtc,
397                                   struct drm_framebuffer *fb,
398                                   struct drm_pending_vblank_event *event,
399                                   uint32_t flags,
400                                   struct drm_modeset_acquire_ctx *ctx)
401 {
402         struct vmw_private *dev_priv = vmw_priv(crtc->dev);
403         struct drm_framebuffer *old_fb = crtc->primary->fb;
404         struct vmw_framebuffer *vfb = vmw_framebuffer_to_vfb(fb);
405         struct vmw_fence_obj *fence = NULL;
406         struct drm_vmw_rect vclips;
407         int ret;
408
409         if (!vmw_kms_crtc_flippable(dev_priv, crtc))
410                 return -EINVAL;
411
412         crtc->primary->fb = fb;
413
414         /* do a full screen dirty update */
415         vclips.x = crtc->x;
416         vclips.y = crtc->y;
417         vclips.w = crtc->mode.hdisplay;
418         vclips.h = crtc->mode.vdisplay;
419
420         if (vfb->dmabuf)
421                 ret = vmw_kms_sou_do_dmabuf_dirty(dev_priv, vfb,
422                                                   NULL, &vclips, 1, 1,
423                                                   true, &fence);
424         else
425                 ret = vmw_kms_sou_do_surface_dirty(dev_priv, vfb,
426                                                    NULL, &vclips, NULL,
427                                                    0, 0, 1, 1, &fence);
428
429
430         if (ret != 0)
431                 goto out_no_fence;
432         if (!fence) {
433                 ret = -EINVAL;
434                 goto out_no_fence;
435         }
436
437         if (event) {
438                 struct drm_file *file_priv = event->base.file_priv;
439
440                 ret = vmw_event_fence_action_queue(file_priv, fence,
441                                                    &event->base,
442                                                    &event->event.tv_sec,
443                                                    &event->event.tv_usec,
444                                                    true);
445         }
446
447         /*
448          * No need to hold on to this now. The only cleanup
449          * we need to do if we fail is unref the fence.
450          */
451         vmw_fence_obj_unreference(&fence);
452
453         if (vmw_crtc_to_du(crtc)->is_implicit)
454                 vmw_kms_update_implicit_fb(dev_priv, crtc);
455
456         return ret;
457
458 out_no_fence:
459         crtc->primary->fb = old_fb;
460         return ret;
461 }
462
463 static const struct drm_crtc_funcs vmw_screen_object_crtc_funcs = {
464         .cursor_set2 = vmw_du_crtc_cursor_set2,
465         .cursor_move = vmw_du_crtc_cursor_move,
466         .gamma_set = vmw_du_crtc_gamma_set,
467         .destroy = vmw_sou_crtc_destroy,
468         .set_config = vmw_sou_crtc_set_config,
469         .page_flip = vmw_sou_crtc_page_flip,
470 };
471
472 /*
473  * Screen Object Display Unit encoder functions
474  */
475
476 static void vmw_sou_encoder_destroy(struct drm_encoder *encoder)
477 {
478         vmw_sou_destroy(vmw_encoder_to_sou(encoder));
479 }
480
481 static const struct drm_encoder_funcs vmw_screen_object_encoder_funcs = {
482         .destroy = vmw_sou_encoder_destroy,
483 };
484
485 /*
486  * Screen Object Display Unit connector functions
487  */
488
489 static void vmw_sou_connector_destroy(struct drm_connector *connector)
490 {
491         vmw_sou_destroy(vmw_connector_to_sou(connector));
492 }
493
494 static const struct drm_connector_funcs vmw_sou_connector_funcs = {
495         .dpms = vmw_du_connector_dpms,
496         .detect = vmw_du_connector_detect,
497         .fill_modes = vmw_du_connector_fill_modes,
498         .set_property = vmw_du_connector_set_property,
499         .destroy = vmw_sou_connector_destroy,
500 };
501
502 static int vmw_sou_init(struct vmw_private *dev_priv, unsigned unit)
503 {
504         struct vmw_screen_object_unit *sou;
505         struct drm_device *dev = dev_priv->dev;
506         struct drm_connector *connector;
507         struct drm_encoder *encoder;
508         struct drm_crtc *crtc;
509
510         sou = kzalloc(sizeof(*sou), GFP_KERNEL);
511         if (!sou)
512                 return -ENOMEM;
513
514         sou->base.unit = unit;
515         crtc = &sou->base.crtc;
516         encoder = &sou->base.encoder;
517         connector = &sou->base.connector;
518
519         sou->base.active_implicit = false;
520         sou->base.pref_active = (unit == 0);
521         sou->base.pref_width = dev_priv->initial_width;
522         sou->base.pref_height = dev_priv->initial_height;
523         sou->base.pref_mode = NULL;
524         sou->base.is_implicit = false;
525
526         drm_connector_init(dev, connector, &vmw_sou_connector_funcs,
527                            DRM_MODE_CONNECTOR_VIRTUAL);
528         connector->status = vmw_du_connector_detect(connector, true);
529
530         drm_encoder_init(dev, encoder, &vmw_screen_object_encoder_funcs,
531                          DRM_MODE_ENCODER_VIRTUAL, NULL);
532         drm_mode_connector_attach_encoder(connector, encoder);
533         encoder->possible_crtcs = (1 << unit);
534         encoder->possible_clones = 0;
535
536         (void) drm_connector_register(connector);
537
538         drm_crtc_init(dev, crtc, &vmw_screen_object_crtc_funcs);
539
540         drm_mode_crtc_set_gamma_size(crtc, 256);
541
542         drm_object_attach_property(&connector->base,
543                                    dev_priv->hotplug_mode_update_property, 1);
544         drm_object_attach_property(&connector->base,
545                                    dev->mode_config.suggested_x_property, 0);
546         drm_object_attach_property(&connector->base,
547                                    dev->mode_config.suggested_y_property, 0);
548         if (dev_priv->implicit_placement_property)
549                 drm_object_attach_property
550                         (&connector->base,
551                          dev_priv->implicit_placement_property,
552                          sou->base.is_implicit);
553
554         return 0;
555 }
556
557 int vmw_kms_sou_init_display(struct vmw_private *dev_priv)
558 {
559         struct drm_device *dev = dev_priv->dev;
560         int i, ret;
561
562         if (!(dev_priv->capabilities & SVGA_CAP_SCREEN_OBJECT_2)) {
563                 DRM_INFO("Not using screen objects,"
564                          " missing cap SCREEN_OBJECT_2\n");
565                 return -ENOSYS;
566         }
567
568         ret = -ENOMEM;
569         dev_priv->num_implicit = 0;
570         dev_priv->implicit_fb = NULL;
571
572         ret = drm_vblank_init(dev, VMWGFX_NUM_DISPLAY_UNITS);
573         if (unlikely(ret != 0))
574                 return ret;
575
576         vmw_kms_create_implicit_placement_property(dev_priv, false);
577
578         for (i = 0; i < VMWGFX_NUM_DISPLAY_UNITS; ++i)
579                 vmw_sou_init(dev_priv, i);
580
581         dev_priv->active_display_unit = vmw_du_screen_object;
582
583         DRM_INFO("Screen Objects Display Unit initialized\n");
584
585         return 0;
586 }
587
588 int vmw_kms_sou_close_display(struct vmw_private *dev_priv)
589 {
590         struct drm_device *dev = dev_priv->dev;
591
592         drm_vblank_cleanup(dev);
593
594         return 0;
595 }
596
597 static int do_dmabuf_define_gmrfb(struct vmw_private *dev_priv,
598                                   struct vmw_framebuffer *framebuffer)
599 {
600         struct vmw_dma_buffer *buf =
601                 container_of(framebuffer, struct vmw_framebuffer_dmabuf,
602                              base)->buffer;
603         int depth = framebuffer->base.format->depth;
604         struct {
605                 uint32_t header;
606                 SVGAFifoCmdDefineGMRFB body;
607         } *cmd;
608
609         /* Emulate RGBA support, contrary to svga_reg.h this is not
610          * supported by hosts. This is only a problem if we are reading
611          * this value later and expecting what we uploaded back.
612          */
613         if (depth == 32)
614                 depth = 24;
615
616         cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
617         if (!cmd) {
618                 DRM_ERROR("Out of fifo space for dirty framebuffer command.\n");
619                 return -ENOMEM;
620         }
621
622         cmd->header = SVGA_CMD_DEFINE_GMRFB;
623         cmd->body.format.bitsPerPixel = framebuffer->base.format->cpp[0] * 8;
624         cmd->body.format.colorDepth = depth;
625         cmd->body.format.reserved = 0;
626         cmd->body.bytesPerLine = framebuffer->base.pitches[0];
627         /* Buffer is reserved in vram or GMR */
628         vmw_bo_get_guest_ptr(&buf->base, &cmd->body.ptr);
629         vmw_fifo_commit(dev_priv, sizeof(*cmd));
630
631         return 0;
632 }
633
634 /**
635  * vmw_sou_surface_fifo_commit - Callback to fill in and submit a
636  * blit surface to screen command.
637  *
638  * @dirty: The closure structure.
639  *
640  * Fills in the missing fields in the command, and translates the cliprects
641  * to match the destination bounding box encoded.
642  */
643 static void vmw_sou_surface_fifo_commit(struct vmw_kms_dirty *dirty)
644 {
645         struct vmw_kms_sou_surface_dirty *sdirty =
646                 container_of(dirty, typeof(*sdirty), base);
647         struct vmw_kms_sou_dirty_cmd *cmd = dirty->cmd;
648         s32 trans_x = dirty->unit->crtc.x - sdirty->dst_x;
649         s32 trans_y = dirty->unit->crtc.y - sdirty->dst_y;
650         size_t region_size = dirty->num_hits * sizeof(SVGASignedRect);
651         SVGASignedRect *blit = (SVGASignedRect *) &cmd[1];
652         int i;
653
654         if (!dirty->num_hits) {
655                 vmw_fifo_commit(dirty->dev_priv, 0);
656                 return;
657         }
658
659         cmd->header.id = SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN;
660         cmd->header.size = sizeof(cmd->body) + region_size;
661
662         /*
663          * Use the destination bounding box to specify destination - and
664          * source bounding regions.
665          */
666         cmd->body.destRect.left = sdirty->left;
667         cmd->body.destRect.right = sdirty->right;
668         cmd->body.destRect.top = sdirty->top;
669         cmd->body.destRect.bottom = sdirty->bottom;
670
671         cmd->body.srcRect.left = sdirty->left + trans_x;
672         cmd->body.srcRect.right = sdirty->right + trans_x;
673         cmd->body.srcRect.top = sdirty->top + trans_y;
674         cmd->body.srcRect.bottom = sdirty->bottom + trans_y;
675
676         cmd->body.srcImage.sid = sdirty->sid;
677         cmd->body.destScreenId = dirty->unit->unit;
678
679         /* Blits are relative to the destination rect. Translate. */
680         for (i = 0; i < dirty->num_hits; ++i, ++blit) {
681                 blit->left -= sdirty->left;
682                 blit->right -= sdirty->left;
683                 blit->top -= sdirty->top;
684                 blit->bottom -= sdirty->top;
685         }
686
687         vmw_fifo_commit(dirty->dev_priv, region_size + sizeof(*cmd));
688
689         sdirty->left = sdirty->top = S32_MAX;
690         sdirty->right = sdirty->bottom = S32_MIN;
691 }
692
693 /**
694  * vmw_sou_surface_clip - Callback to encode a blit surface to screen cliprect.
695  *
696  * @dirty: The closure structure
697  *
698  * Encodes a SVGASignedRect cliprect and updates the bounding box of the
699  * BLIT_SURFACE_TO_SCREEN command.
700  */
701 static void vmw_sou_surface_clip(struct vmw_kms_dirty *dirty)
702 {
703         struct vmw_kms_sou_surface_dirty *sdirty =
704                 container_of(dirty, typeof(*sdirty), base);
705         struct vmw_kms_sou_dirty_cmd *cmd = dirty->cmd;
706         SVGASignedRect *blit = (SVGASignedRect *) &cmd[1];
707
708         /* Destination rect. */
709         blit += dirty->num_hits;
710         blit->left = dirty->unit_x1;
711         blit->top = dirty->unit_y1;
712         blit->right = dirty->unit_x2;
713         blit->bottom = dirty->unit_y2;
714
715         /* Destination bounding box */
716         sdirty->left = min_t(s32, sdirty->left, dirty->unit_x1);
717         sdirty->top = min_t(s32, sdirty->top, dirty->unit_y1);
718         sdirty->right = max_t(s32, sdirty->right, dirty->unit_x2);
719         sdirty->bottom = max_t(s32, sdirty->bottom, dirty->unit_y2);
720
721         dirty->num_hits++;
722 }
723
724 /**
725  * vmw_kms_sou_do_surface_dirty - Dirty part of a surface backed framebuffer
726  *
727  * @dev_priv: Pointer to the device private structure.
728  * @framebuffer: Pointer to the surface-buffer backed framebuffer.
729  * @clips: Array of clip rects. Either @clips or @vclips must be NULL.
730  * @vclips: Alternate array of clip rects. Either @clips or @vclips must
731  * be NULL.
732  * @srf: Pointer to surface to blit from. If NULL, the surface attached
733  * to @framebuffer will be used.
734  * @dest_x: X coordinate offset to align @srf with framebuffer coordinates.
735  * @dest_y: Y coordinate offset to align @srf with framebuffer coordinates.
736  * @num_clips: Number of clip rects in @clips.
737  * @inc: Increment to use when looping over @clips.
738  * @out_fence: If non-NULL, will return a ref-counted pointer to a
739  * struct vmw_fence_obj. The returned fence pointer may be NULL in which
740  * case the device has already synchronized.
741  *
742  * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
743  * interrupted.
744  */
745 int vmw_kms_sou_do_surface_dirty(struct vmw_private *dev_priv,
746                                  struct vmw_framebuffer *framebuffer,
747                                  struct drm_clip_rect *clips,
748                                  struct drm_vmw_rect *vclips,
749                                  struct vmw_resource *srf,
750                                  s32 dest_x,
751                                  s32 dest_y,
752                                  unsigned num_clips, int inc,
753                                  struct vmw_fence_obj **out_fence)
754 {
755         struct vmw_framebuffer_surface *vfbs =
756                 container_of(framebuffer, typeof(*vfbs), base);
757         struct vmw_kms_sou_surface_dirty sdirty;
758         int ret;
759
760         if (!srf)
761                 srf = &vfbs->surface->res;
762
763         ret = vmw_kms_helper_resource_prepare(srf, true);
764         if (ret)
765                 return ret;
766
767         sdirty.base.fifo_commit = vmw_sou_surface_fifo_commit;
768         sdirty.base.clip = vmw_sou_surface_clip;
769         sdirty.base.dev_priv = dev_priv;
770         sdirty.base.fifo_reserve_size = sizeof(struct vmw_kms_sou_dirty_cmd) +
771           sizeof(SVGASignedRect) * num_clips;
772
773         sdirty.sid = srf->id;
774         sdirty.left = sdirty.top = S32_MAX;
775         sdirty.right = sdirty.bottom = S32_MIN;
776         sdirty.dst_x = dest_x;
777         sdirty.dst_y = dest_y;
778
779         ret = vmw_kms_helper_dirty(dev_priv, framebuffer, clips, vclips,
780                                    dest_x, dest_y, num_clips, inc,
781                                    &sdirty.base);
782         vmw_kms_helper_resource_finish(srf, out_fence);
783
784         return ret;
785 }
786
787 /**
788  * vmw_sou_dmabuf_fifo_commit - Callback to submit a set of readback clips.
789  *
790  * @dirty: The closure structure.
791  *
792  * Commits a previously built command buffer of readback clips.
793  */
794 static void vmw_sou_dmabuf_fifo_commit(struct vmw_kms_dirty *dirty)
795 {
796         if (!dirty->num_hits) {
797                 vmw_fifo_commit(dirty->dev_priv, 0);
798                 return;
799         }
800
801         vmw_fifo_commit(dirty->dev_priv,
802                         sizeof(struct vmw_kms_sou_dmabuf_blit) *
803                         dirty->num_hits);
804 }
805
806 /**
807  * vmw_sou_dmabuf_clip - Callback to encode a readback cliprect.
808  *
809  * @dirty: The closure structure
810  *
811  * Encodes a BLIT_GMRFB_TO_SCREEN cliprect.
812  */
813 static void vmw_sou_dmabuf_clip(struct vmw_kms_dirty *dirty)
814 {
815         struct vmw_kms_sou_dmabuf_blit *blit = dirty->cmd;
816
817         blit += dirty->num_hits;
818         blit->header = SVGA_CMD_BLIT_GMRFB_TO_SCREEN;
819         blit->body.destScreenId = dirty->unit->unit;
820         blit->body.srcOrigin.x = dirty->fb_x;
821         blit->body.srcOrigin.y = dirty->fb_y;
822         blit->body.destRect.left = dirty->unit_x1;
823         blit->body.destRect.top = dirty->unit_y1;
824         blit->body.destRect.right = dirty->unit_x2;
825         blit->body.destRect.bottom = dirty->unit_y2;
826         dirty->num_hits++;
827 }
828
829 /**
830  * vmw_kms_do_dmabuf_dirty - Dirty part of a dma-buffer backed framebuffer
831  *
832  * @dev_priv: Pointer to the device private structure.
833  * @framebuffer: Pointer to the dma-buffer backed framebuffer.
834  * @clips: Array of clip rects.
835  * @vclips: Alternate array of clip rects. Either @clips or @vclips must
836  * be NULL.
837  * @num_clips: Number of clip rects in @clips.
838  * @increment: Increment to use when looping over @clips.
839  * @interruptible: Whether to perform waits interruptible if possible.
840  * @out_fence: If non-NULL, will return a ref-counted pointer to a
841  * struct vmw_fence_obj. The returned fence pointer may be NULL in which
842  * case the device has already synchronized.
843  *
844  * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
845  * interrupted.
846  */
847 int vmw_kms_sou_do_dmabuf_dirty(struct vmw_private *dev_priv,
848                                 struct vmw_framebuffer *framebuffer,
849                                 struct drm_clip_rect *clips,
850                                 struct drm_vmw_rect *vclips,
851                                 unsigned num_clips, int increment,
852                                 bool interruptible,
853                                 struct vmw_fence_obj **out_fence)
854 {
855         struct vmw_dma_buffer *buf =
856                 container_of(framebuffer, struct vmw_framebuffer_dmabuf,
857                              base)->buffer;
858         struct vmw_kms_dirty dirty;
859         int ret;
860
861         ret = vmw_kms_helper_buffer_prepare(dev_priv, buf, interruptible,
862                                             false);
863         if (ret)
864                 return ret;
865
866         ret = do_dmabuf_define_gmrfb(dev_priv, framebuffer);
867         if (unlikely(ret != 0))
868                 goto out_revert;
869
870         dirty.fifo_commit = vmw_sou_dmabuf_fifo_commit;
871         dirty.clip = vmw_sou_dmabuf_clip;
872         dirty.fifo_reserve_size = sizeof(struct vmw_kms_sou_dmabuf_blit) *
873                 num_clips;
874         ret = vmw_kms_helper_dirty(dev_priv, framebuffer, clips, vclips,
875                                    0, 0, num_clips, increment, &dirty);
876         vmw_kms_helper_buffer_finish(dev_priv, NULL, buf, out_fence, NULL);
877
878         return ret;
879
880 out_revert:
881         vmw_kms_helper_buffer_revert(buf);
882
883         return ret;
884 }
885
886
887 /**
888  * vmw_sou_readback_fifo_commit - Callback to submit a set of readback clips.
889  *
890  * @dirty: The closure structure.
891  *
892  * Commits a previously built command buffer of readback clips.
893  */
894 static void vmw_sou_readback_fifo_commit(struct vmw_kms_dirty *dirty)
895 {
896         if (!dirty->num_hits) {
897                 vmw_fifo_commit(dirty->dev_priv, 0);
898                 return;
899         }
900
901         vmw_fifo_commit(dirty->dev_priv,
902                         sizeof(struct vmw_kms_sou_readback_blit) *
903                         dirty->num_hits);
904 }
905
906 /**
907  * vmw_sou_readback_clip - Callback to encode a readback cliprect.
908  *
909  * @dirty: The closure structure
910  *
911  * Encodes a BLIT_SCREEN_TO_GMRFB cliprect.
912  */
913 static void vmw_sou_readback_clip(struct vmw_kms_dirty *dirty)
914 {
915         struct vmw_kms_sou_readback_blit *blit = dirty->cmd;
916
917         blit += dirty->num_hits;
918         blit->header = SVGA_CMD_BLIT_SCREEN_TO_GMRFB;
919         blit->body.srcScreenId = dirty->unit->unit;
920         blit->body.destOrigin.x = dirty->fb_x;
921         blit->body.destOrigin.y = dirty->fb_y;
922         blit->body.srcRect.left = dirty->unit_x1;
923         blit->body.srcRect.top = dirty->unit_y1;
924         blit->body.srcRect.right = dirty->unit_x2;
925         blit->body.srcRect.bottom = dirty->unit_y2;
926         dirty->num_hits++;
927 }
928
929 /**
930  * vmw_kms_sou_readback - Perform a readback from the screen object system to
931  * a dma-buffer backed framebuffer.
932  *
933  * @dev_priv: Pointer to the device private structure.
934  * @file_priv: Pointer to a struct drm_file identifying the caller.
935  * Must be set to NULL if @user_fence_rep is NULL.
936  * @vfb: Pointer to the dma-buffer backed framebuffer.
937  * @user_fence_rep: User-space provided structure for fence information.
938  * Must be set to non-NULL if @file_priv is non-NULL.
939  * @vclips: Array of clip rects.
940  * @num_clips: Number of clip rects in @vclips.
941  *
942  * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
943  * interrupted.
944  */
945 int vmw_kms_sou_readback(struct vmw_private *dev_priv,
946                          struct drm_file *file_priv,
947                          struct vmw_framebuffer *vfb,
948                          struct drm_vmw_fence_rep __user *user_fence_rep,
949                          struct drm_vmw_rect *vclips,
950                          uint32_t num_clips)
951 {
952         struct vmw_dma_buffer *buf =
953                 container_of(vfb, struct vmw_framebuffer_dmabuf, base)->buffer;
954         struct vmw_kms_dirty dirty;
955         int ret;
956
957         ret = vmw_kms_helper_buffer_prepare(dev_priv, buf, true, false);
958         if (ret)
959                 return ret;
960
961         ret = do_dmabuf_define_gmrfb(dev_priv, vfb);
962         if (unlikely(ret != 0))
963                 goto out_revert;
964
965         dirty.fifo_commit = vmw_sou_readback_fifo_commit;
966         dirty.clip = vmw_sou_readback_clip;
967         dirty.fifo_reserve_size = sizeof(struct vmw_kms_sou_readback_blit) *
968                 num_clips;
969         ret = vmw_kms_helper_dirty(dev_priv, vfb, NULL, vclips,
970                                    0, 0, num_clips, 1, &dirty);
971         vmw_kms_helper_buffer_finish(dev_priv, file_priv, buf, NULL,
972                                      user_fence_rep);
973
974         return ret;
975
976 out_revert:
977         vmw_kms_helper_buffer_revert(buf);
978
979         return ret;
980 }