Merge tag 'staging-5.1-rc3' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh...
[linux-2.6-microblaze.git] / drivers / gpu / drm / vmwgfx / vmwgfx_scrn.c
1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /**************************************************************************
3  *
4  * Copyright 2011-2015 VMware, Inc., Palo Alto, CA., USA
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sub license, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21  * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24  * USE OR OTHER DEALINGS IN THE SOFTWARE.
25  *
26  **************************************************************************/
27
28 #include "vmwgfx_kms.h"
29 #include <drm/drm_plane_helper.h>
30 #include <drm/drm_atomic.h>
31 #include <drm/drm_atomic_helper.h>
32 #include <drm/drm_damage_helper.h>
33
34
35 #define vmw_crtc_to_sou(x) \
36         container_of(x, struct vmw_screen_object_unit, base.crtc)
37 #define vmw_encoder_to_sou(x) \
38         container_of(x, struct vmw_screen_object_unit, base.encoder)
39 #define vmw_connector_to_sou(x) \
40         container_of(x, struct vmw_screen_object_unit, base.connector)
41
42 /**
43  * struct vmw_kms_sou_surface_dirty - Closure structure for
44  * blit surface to screen command.
45  * @base: The base type we derive from. Used by vmw_kms_helper_dirty().
46  * @left: Left side of bounding box.
47  * @right: Right side of bounding box.
48  * @top: Top side of bounding box.
49  * @bottom: Bottom side of bounding box.
50  * @dst_x: Difference between source clip rects and framebuffer coordinates.
51  * @dst_y: Difference between source clip rects and framebuffer coordinates.
52  * @sid: Surface id of surface to copy from.
53  */
54 struct vmw_kms_sou_surface_dirty {
55         struct vmw_kms_dirty base;
56         s32 left, right, top, bottom;
57         s32 dst_x, dst_y;
58         u32 sid;
59 };
60
61 /*
62  * SVGA commands that are used by this code. Please see the device headers
63  * for explanation.
64  */
65 struct vmw_kms_sou_readback_blit {
66         uint32 header;
67         SVGAFifoCmdBlitScreenToGMRFB body;
68 };
69
70 struct vmw_kms_sou_bo_blit {
71         uint32 header;
72         SVGAFifoCmdBlitGMRFBToScreen body;
73 };
74
75 struct vmw_kms_sou_dirty_cmd {
76         SVGA3dCmdHeader header;
77         SVGA3dCmdBlitSurfaceToScreen body;
78 };
79
80 struct vmw_kms_sou_define_gmrfb {
81         uint32_t header;
82         SVGAFifoCmdDefineGMRFB body;
83 };
84
85 /**
86  * Display unit using screen objects.
87  */
88 struct vmw_screen_object_unit {
89         struct vmw_display_unit base;
90
91         unsigned long buffer_size; /**< Size of allocated buffer */
92         struct vmw_buffer_object *buffer; /**< Backing store buffer */
93
94         bool defined;
95 };
96
97 static void vmw_sou_destroy(struct vmw_screen_object_unit *sou)
98 {
99         vmw_du_cleanup(&sou->base);
100         kfree(sou);
101 }
102
103
104 /*
105  * Screen Object Display Unit CRTC functions
106  */
107
108 static void vmw_sou_crtc_destroy(struct drm_crtc *crtc)
109 {
110         vmw_sou_destroy(vmw_crtc_to_sou(crtc));
111 }
112
113 /**
114  * Send the fifo command to create a screen.
115  */
116 static int vmw_sou_fifo_create(struct vmw_private *dev_priv,
117                                struct vmw_screen_object_unit *sou,
118                                int x, int y,
119                                struct drm_display_mode *mode)
120 {
121         size_t fifo_size;
122
123         struct {
124                 struct {
125                         uint32_t cmdType;
126                 } header;
127                 SVGAScreenObject obj;
128         } *cmd;
129
130         BUG_ON(!sou->buffer);
131
132         fifo_size = sizeof(*cmd);
133         cmd = vmw_fifo_reserve(dev_priv, fifo_size);
134         /* The hardware has hung, nothing we can do about it here. */
135         if (unlikely(cmd == NULL)) {
136                 DRM_ERROR("Fifo reserve failed.\n");
137                 return -ENOMEM;
138         }
139
140         memset(cmd, 0, fifo_size);
141         cmd->header.cmdType = SVGA_CMD_DEFINE_SCREEN;
142         cmd->obj.structSize = sizeof(SVGAScreenObject);
143         cmd->obj.id = sou->base.unit;
144         cmd->obj.flags = SVGA_SCREEN_HAS_ROOT |
145                 (sou->base.unit == 0 ? SVGA_SCREEN_IS_PRIMARY : 0);
146         cmd->obj.size.width = mode->hdisplay;
147         cmd->obj.size.height = mode->vdisplay;
148         cmd->obj.root.x = x;
149         cmd->obj.root.y = y;
150         sou->base.set_gui_x = cmd->obj.root.x;
151         sou->base.set_gui_y = cmd->obj.root.y;
152
153         /* Ok to assume that buffer is pinned in vram */
154         vmw_bo_get_guest_ptr(&sou->buffer->base, &cmd->obj.backingStore.ptr);
155         cmd->obj.backingStore.pitch = mode->hdisplay * 4;
156
157         vmw_fifo_commit(dev_priv, fifo_size);
158
159         sou->defined = true;
160
161         return 0;
162 }
163
164 /**
165  * Send the fifo command to destroy a screen.
166  */
167 static int vmw_sou_fifo_destroy(struct vmw_private *dev_priv,
168                                 struct vmw_screen_object_unit *sou)
169 {
170         size_t fifo_size;
171         int ret;
172
173         struct {
174                 struct {
175                         uint32_t cmdType;
176                 } header;
177                 SVGAFifoCmdDestroyScreen body;
178         } *cmd;
179
180         /* no need to do anything */
181         if (unlikely(!sou->defined))
182                 return 0;
183
184         fifo_size = sizeof(*cmd);
185         cmd = vmw_fifo_reserve(dev_priv, fifo_size);
186         /* the hardware has hung, nothing we can do about it here */
187         if (unlikely(cmd == NULL)) {
188                 DRM_ERROR("Fifo reserve failed.\n");
189                 return -ENOMEM;
190         }
191
192         memset(cmd, 0, fifo_size);
193         cmd->header.cmdType = SVGA_CMD_DESTROY_SCREEN;
194         cmd->body.screenId = sou->base.unit;
195
196         vmw_fifo_commit(dev_priv, fifo_size);
197
198         /* Force sync */
199         ret = vmw_fallback_wait(dev_priv, false, true, 0, false, 3*HZ);
200         if (unlikely(ret != 0))
201                 DRM_ERROR("Failed to sync with HW");
202         else
203                 sou->defined = false;
204
205         return ret;
206 }
207
208 /**
209  * vmw_sou_crtc_mode_set_nofb - Create new screen
210  *
211  * @crtc: CRTC associated with the new screen
212  *
213  * This function creates/destroys a screen.  This function cannot fail, so if
214  * somehow we run into a failure, just do the best we can to get out.
215  */
216 static void vmw_sou_crtc_mode_set_nofb(struct drm_crtc *crtc)
217 {
218         struct vmw_private *dev_priv;
219         struct vmw_screen_object_unit *sou;
220         struct vmw_framebuffer *vfb;
221         struct drm_framebuffer *fb;
222         struct drm_plane_state *ps;
223         struct vmw_plane_state *vps;
224         int ret;
225
226         sou = vmw_crtc_to_sou(crtc);
227         dev_priv = vmw_priv(crtc->dev);
228         ps = crtc->primary->state;
229         fb = ps->fb;
230         vps = vmw_plane_state_to_vps(ps);
231
232         vfb = (fb) ? vmw_framebuffer_to_vfb(fb) : NULL;
233
234         if (sou->defined) {
235                 ret = vmw_sou_fifo_destroy(dev_priv, sou);
236                 if (ret) {
237                         DRM_ERROR("Failed to destroy Screen Object\n");
238                         return;
239                 }
240         }
241
242         if (vfb) {
243                 struct drm_connector_state *conn_state;
244                 struct vmw_connector_state *vmw_conn_state;
245                 int x, y;
246
247                 sou->buffer = vps->bo;
248                 sou->buffer_size = vps->bo_size;
249
250                 conn_state = sou->base.connector.state;
251                 vmw_conn_state = vmw_connector_state_to_vcs(conn_state);
252
253                 x = vmw_conn_state->gui_x;
254                 y = vmw_conn_state->gui_y;
255
256                 ret = vmw_sou_fifo_create(dev_priv, sou, x, y, &crtc->mode);
257                 if (ret)
258                         DRM_ERROR("Failed to define Screen Object %dx%d\n",
259                                   crtc->x, crtc->y);
260
261         } else {
262                 sou->buffer = NULL;
263                 sou->buffer_size = 0;
264         }
265 }
266
267 /**
268  * vmw_sou_crtc_helper_prepare - Noop
269  *
270  * @crtc: CRTC associated with the new screen
271  *
272  * Prepares the CRTC for a mode set, but we don't need to do anything here.
273  */
274 static void vmw_sou_crtc_helper_prepare(struct drm_crtc *crtc)
275 {
276 }
277
278 /**
279  * vmw_sou_crtc_atomic_enable - Noop
280  *
281  * @crtc: CRTC associated with the new screen
282  *
283  * This is called after a mode set has been completed.
284  */
285 static void vmw_sou_crtc_atomic_enable(struct drm_crtc *crtc,
286                                        struct drm_crtc_state *old_state)
287 {
288 }
289
290 /**
291  * vmw_sou_crtc_atomic_disable - Turns off CRTC
292  *
293  * @crtc: CRTC to be turned off
294  */
295 static void vmw_sou_crtc_atomic_disable(struct drm_crtc *crtc,
296                                         struct drm_crtc_state *old_state)
297 {
298         struct vmw_private *dev_priv;
299         struct vmw_screen_object_unit *sou;
300         int ret;
301
302
303         if (!crtc) {
304                 DRM_ERROR("CRTC is NULL\n");
305                 return;
306         }
307
308         sou = vmw_crtc_to_sou(crtc);
309         dev_priv = vmw_priv(crtc->dev);
310
311         if (sou->defined) {
312                 ret = vmw_sou_fifo_destroy(dev_priv, sou);
313                 if (ret)
314                         DRM_ERROR("Failed to destroy Screen Object\n");
315         }
316 }
317
318 static const struct drm_crtc_funcs vmw_screen_object_crtc_funcs = {
319         .gamma_set = vmw_du_crtc_gamma_set,
320         .destroy = vmw_sou_crtc_destroy,
321         .reset = vmw_du_crtc_reset,
322         .atomic_duplicate_state = vmw_du_crtc_duplicate_state,
323         .atomic_destroy_state = vmw_du_crtc_destroy_state,
324         .set_config = drm_atomic_helper_set_config,
325         .page_flip = drm_atomic_helper_page_flip,
326 };
327
328 /*
329  * Screen Object Display Unit encoder functions
330  */
331
332 static void vmw_sou_encoder_destroy(struct drm_encoder *encoder)
333 {
334         vmw_sou_destroy(vmw_encoder_to_sou(encoder));
335 }
336
337 static const struct drm_encoder_funcs vmw_screen_object_encoder_funcs = {
338         .destroy = vmw_sou_encoder_destroy,
339 };
340
341 /*
342  * Screen Object Display Unit connector functions
343  */
344
345 static void vmw_sou_connector_destroy(struct drm_connector *connector)
346 {
347         vmw_sou_destroy(vmw_connector_to_sou(connector));
348 }
349
350 static const struct drm_connector_funcs vmw_sou_connector_funcs = {
351         .dpms = vmw_du_connector_dpms,
352         .detect = vmw_du_connector_detect,
353         .fill_modes = vmw_du_connector_fill_modes,
354         .destroy = vmw_sou_connector_destroy,
355         .reset = vmw_du_connector_reset,
356         .atomic_duplicate_state = vmw_du_connector_duplicate_state,
357         .atomic_destroy_state = vmw_du_connector_destroy_state,
358 };
359
360
361 static const struct
362 drm_connector_helper_funcs vmw_sou_connector_helper_funcs = {
363 };
364
365
366
367 /*
368  * Screen Object Display Plane Functions
369  */
370
371 /**
372  * vmw_sou_primary_plane_cleanup_fb - Frees sou backing buffer
373  *
374  * @plane:  display plane
375  * @old_state: Contains the FB to clean up
376  *
377  * Unpins the display surface
378  *
379  * Returns 0 on success
380  */
381 static void
382 vmw_sou_primary_plane_cleanup_fb(struct drm_plane *plane,
383                                  struct drm_plane_state *old_state)
384 {
385         struct vmw_plane_state *vps = vmw_plane_state_to_vps(old_state);
386         struct drm_crtc *crtc = plane->state->crtc ?
387                 plane->state->crtc : old_state->crtc;
388
389         if (vps->bo)
390                 vmw_bo_unpin(vmw_priv(crtc->dev), vps->bo, false);
391         vmw_bo_unreference(&vps->bo);
392         vps->bo_size = 0;
393
394         vmw_du_plane_cleanup_fb(plane, old_state);
395 }
396
397
398 /**
399  * vmw_sou_primary_plane_prepare_fb - allocate backing buffer
400  *
401  * @plane:  display plane
402  * @new_state: info on the new plane state, including the FB
403  *
404  * The SOU backing buffer is our equivalent of the display plane.
405  *
406  * Returns 0 on success
407  */
408 static int
409 vmw_sou_primary_plane_prepare_fb(struct drm_plane *plane,
410                                  struct drm_plane_state *new_state)
411 {
412         struct drm_framebuffer *new_fb = new_state->fb;
413         struct drm_crtc *crtc = plane->state->crtc ?: new_state->crtc;
414         struct vmw_plane_state *vps = vmw_plane_state_to_vps(new_state);
415         struct vmw_private *dev_priv;
416         size_t size;
417         int ret;
418
419
420         if (!new_fb) {
421                 vmw_bo_unreference(&vps->bo);
422                 vps->bo_size = 0;
423
424                 return 0;
425         }
426
427         size = new_state->crtc_w * new_state->crtc_h * 4;
428         dev_priv = vmw_priv(crtc->dev);
429
430         if (vps->bo) {
431                 if (vps->bo_size == size) {
432                         /*
433                          * Note that this might temporarily up the pin-count
434                          * to 2, until cleanup_fb() is called.
435                          */
436                         return vmw_bo_pin_in_vram(dev_priv, vps->bo,
437                                                       true);
438                 }
439
440                 vmw_bo_unreference(&vps->bo);
441                 vps->bo_size = 0;
442         }
443
444         vps->bo = kzalloc(sizeof(*vps->bo), GFP_KERNEL);
445         if (!vps->bo)
446                 return -ENOMEM;
447
448         vmw_svga_enable(dev_priv);
449
450         /* After we have alloced the backing store might not be able to
451          * resume the overlays, this is preferred to failing to alloc.
452          */
453         vmw_overlay_pause_all(dev_priv);
454         ret = vmw_bo_init(dev_priv, vps->bo, size,
455                               &vmw_vram_ne_placement,
456                               false, &vmw_bo_bo_free);
457         vmw_overlay_resume_all(dev_priv);
458         if (ret) {
459                 vps->bo = NULL; /* vmw_bo_init frees on error */
460                 return ret;
461         }
462
463         vps->bo_size = size;
464
465         /*
466          * TTM already thinks the buffer is pinned, but make sure the
467          * pin_count is upped.
468          */
469         return vmw_bo_pin_in_vram(dev_priv, vps->bo, true);
470 }
471
472 static uint32_t vmw_sou_bo_fifo_size(struct vmw_du_update_plane *update,
473                                      uint32_t num_hits)
474 {
475         return sizeof(struct vmw_kms_sou_define_gmrfb) +
476                 sizeof(struct vmw_kms_sou_bo_blit) * num_hits;
477 }
478
479 static uint32_t vmw_sou_bo_define_gmrfb(struct vmw_du_update_plane *update,
480                                         void *cmd)
481 {
482         struct vmw_framebuffer_bo *vfbbo =
483                 container_of(update->vfb, typeof(*vfbbo), base);
484         struct vmw_kms_sou_define_gmrfb *gmr = cmd;
485         int depth = update->vfb->base.format->depth;
486
487         /* Emulate RGBA support, contrary to svga_reg.h this is not
488          * supported by hosts. This is only a problem if we are reading
489          * this value later and expecting what we uploaded back.
490          */
491         if (depth == 32)
492                 depth = 24;
493
494         gmr->header = SVGA_CMD_DEFINE_GMRFB;
495
496         gmr->body.format.bitsPerPixel = update->vfb->base.format->cpp[0] * 8;
497         gmr->body.format.colorDepth = depth;
498         gmr->body.format.reserved = 0;
499         gmr->body.bytesPerLine = update->vfb->base.pitches[0];
500         vmw_bo_get_guest_ptr(&vfbbo->buffer->base, &gmr->body.ptr);
501
502         return sizeof(*gmr);
503 }
504
505 static uint32_t vmw_sou_bo_populate_clip(struct vmw_du_update_plane  *update,
506                                          void *cmd, struct drm_rect *clip,
507                                          uint32_t fb_x, uint32_t fb_y)
508 {
509         struct vmw_kms_sou_bo_blit *blit = cmd;
510
511         blit->header = SVGA_CMD_BLIT_GMRFB_TO_SCREEN;
512         blit->body.destScreenId = update->du->unit;
513         blit->body.srcOrigin.x = fb_x;
514         blit->body.srcOrigin.y = fb_y;
515         blit->body.destRect.left = clip->x1;
516         blit->body.destRect.top = clip->y1;
517         blit->body.destRect.right = clip->x2;
518         blit->body.destRect.bottom = clip->y2;
519
520         return sizeof(*blit);
521 }
522
523 static uint32_t vmw_stud_bo_post_clip(struct vmw_du_update_plane  *update,
524                                       void *cmd, struct drm_rect *bb)
525 {
526         return 0;
527 }
528
529 /**
530  * vmw_sou_plane_update_bo - Update display unit for bo backed fb.
531  * @dev_priv: Device private.
532  * @plane: Plane state.
533  * @old_state: Old plane state.
534  * @vfb: Framebuffer which is blitted to display unit.
535  * @out_fence: If non-NULL, will return a ref-counted pointer to vmw_fence_obj.
536  *             The returned fence pointer may be NULL in which case the device
537  *             has already synchronized.
538  *
539  * Return: 0 on success or a negative error code on failure.
540  */
541 static int vmw_sou_plane_update_bo(struct vmw_private *dev_priv,
542                                    struct drm_plane *plane,
543                                    struct drm_plane_state *old_state,
544                                    struct vmw_framebuffer *vfb,
545                                    struct vmw_fence_obj **out_fence)
546 {
547         struct vmw_du_update_plane_buffer bo_update;
548
549         memset(&bo_update, 0, sizeof(struct vmw_du_update_plane_buffer));
550         bo_update.base.plane = plane;
551         bo_update.base.old_state = old_state;
552         bo_update.base.dev_priv = dev_priv;
553         bo_update.base.du = vmw_crtc_to_du(plane->state->crtc);
554         bo_update.base.vfb = vfb;
555         bo_update.base.out_fence = out_fence;
556         bo_update.base.mutex = NULL;
557         bo_update.base.cpu_blit = false;
558         bo_update.base.intr = true;
559
560         bo_update.base.calc_fifo_size = vmw_sou_bo_fifo_size;
561         bo_update.base.post_prepare = vmw_sou_bo_define_gmrfb;
562         bo_update.base.clip = vmw_sou_bo_populate_clip;
563         bo_update.base.post_clip = vmw_stud_bo_post_clip;
564
565         return vmw_du_helper_plane_update(&bo_update.base);
566 }
567
568 static uint32_t vmw_sou_surface_fifo_size(struct vmw_du_update_plane *update,
569                                           uint32_t num_hits)
570 {
571         return sizeof(struct vmw_kms_sou_dirty_cmd) + sizeof(SVGASignedRect) *
572                 num_hits;
573 }
574
575 static uint32_t vmw_sou_surface_post_prepare(struct vmw_du_update_plane *update,
576                                              void *cmd)
577 {
578         struct vmw_du_update_plane_surface *srf_update;
579
580         srf_update = container_of(update, typeof(*srf_update), base);
581
582         /*
583          * SOU SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN is special in the sense that
584          * its bounding box is filled before iterating over all the clips. So
585          * store the FIFO start address and revisit to fill the details.
586          */
587         srf_update->cmd_start = cmd;
588
589         return 0;
590 }
591
592 static uint32_t vmw_sou_surface_pre_clip(struct vmw_du_update_plane *update,
593                                          void *cmd, uint32_t num_hits)
594 {
595         struct vmw_kms_sou_dirty_cmd *blit = cmd;
596         struct vmw_framebuffer_surface *vfbs;
597
598         vfbs = container_of(update->vfb, typeof(*vfbs), base);
599
600         blit->header.id = SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN;
601         blit->header.size = sizeof(blit->body) + sizeof(SVGASignedRect) *
602                 num_hits;
603
604         blit->body.srcImage.sid = vfbs->surface->res.id;
605         blit->body.destScreenId = update->du->unit;
606
607         /* Update the source and destination bounding box later in post_clip */
608         blit->body.srcRect.left = 0;
609         blit->body.srcRect.top = 0;
610         blit->body.srcRect.right = 0;
611         blit->body.srcRect.bottom = 0;
612
613         blit->body.destRect.left = 0;
614         blit->body.destRect.top = 0;
615         blit->body.destRect.right = 0;
616         blit->body.destRect.bottom = 0;
617
618         return sizeof(*blit);
619 }
620
621 static uint32_t vmw_sou_surface_clip_rect(struct vmw_du_update_plane *update,
622                                           void *cmd, struct drm_rect *clip,
623                                           uint32_t src_x, uint32_t src_y)
624 {
625         SVGASignedRect *rect = cmd;
626
627         /*
628          * rects are relative to dest bounding box rect on screen object, so
629          * translate to it later in post_clip
630          */
631         rect->left = clip->x1;
632         rect->top = clip->y1;
633         rect->right = clip->x2;
634         rect->bottom = clip->y2;
635
636         return sizeof(*rect);
637 }
638
639 static uint32_t vmw_sou_surface_post_clip(struct vmw_du_update_plane *update,
640                                           void *cmd, struct drm_rect *bb)
641 {
642         struct vmw_du_update_plane_surface *srf_update;
643         struct drm_plane_state *state = update->plane->state;
644         struct drm_rect src_bb;
645         struct vmw_kms_sou_dirty_cmd *blit;
646         SVGASignedRect *rect;
647         uint32_t num_hits;
648         int translate_src_x;
649         int translate_src_y;
650         int i;
651
652         srf_update = container_of(update, typeof(*srf_update), base);
653
654         blit = srf_update->cmd_start;
655         rect = (SVGASignedRect *)&blit[1];
656
657         num_hits = (blit->header.size - sizeof(blit->body))/
658                 sizeof(SVGASignedRect);
659
660         src_bb = *bb;
661
662         /* To translate bb back to fb src coord */
663         translate_src_x = (state->src_x >> 16) - state->crtc_x;
664         translate_src_y = (state->src_y >> 16) - state->crtc_y;
665
666         drm_rect_translate(&src_bb, translate_src_x, translate_src_y);
667
668         blit->body.srcRect.left = src_bb.x1;
669         blit->body.srcRect.top = src_bb.y1;
670         blit->body.srcRect.right = src_bb.x2;
671         blit->body.srcRect.bottom = src_bb.y2;
672
673         blit->body.destRect.left = bb->x1;
674         blit->body.destRect.top = bb->y1;
675         blit->body.destRect.right = bb->x2;
676         blit->body.destRect.bottom = bb->y2;
677
678         /* rects are relative to dest bb rect */
679         for (i = 0; i < num_hits; i++) {
680                 rect->left -= bb->x1;
681                 rect->top -= bb->y1;
682                 rect->right -= bb->x1;
683                 rect->bottom -= bb->y1;
684                 rect++;
685         }
686
687         return 0;
688 }
689
690 /**
691  * vmw_sou_plane_update_surface - Update display unit for surface backed fb.
692  * @dev_priv: Device private.
693  * @plane: Plane state.
694  * @old_state: Old plane state.
695  * @vfb: Framebuffer which is blitted to display unit
696  * @out_fence: If non-NULL, will return a ref-counted pointer to vmw_fence_obj.
697  *             The returned fence pointer may be NULL in which case the device
698  *             has already synchronized.
699  *
700  * Return: 0 on success or a negative error code on failure.
701  */
702 static int vmw_sou_plane_update_surface(struct vmw_private *dev_priv,
703                                         struct drm_plane *plane,
704                                         struct drm_plane_state *old_state,
705                                         struct vmw_framebuffer *vfb,
706                                         struct vmw_fence_obj **out_fence)
707 {
708         struct vmw_du_update_plane_surface srf_update;
709
710         memset(&srf_update, 0, sizeof(struct vmw_du_update_plane_surface));
711         srf_update.base.plane = plane;
712         srf_update.base.old_state = old_state;
713         srf_update.base.dev_priv = dev_priv;
714         srf_update.base.du = vmw_crtc_to_du(plane->state->crtc);
715         srf_update.base.vfb = vfb;
716         srf_update.base.out_fence = out_fence;
717         srf_update.base.mutex = &dev_priv->cmdbuf_mutex;
718         srf_update.base.cpu_blit = false;
719         srf_update.base.intr = true;
720
721         srf_update.base.calc_fifo_size = vmw_sou_surface_fifo_size;
722         srf_update.base.post_prepare = vmw_sou_surface_post_prepare;
723         srf_update.base.pre_clip = vmw_sou_surface_pre_clip;
724         srf_update.base.clip = vmw_sou_surface_clip_rect;
725         srf_update.base.post_clip = vmw_sou_surface_post_clip;
726
727         return vmw_du_helper_plane_update(&srf_update.base);
728 }
729
730 static void
731 vmw_sou_primary_plane_atomic_update(struct drm_plane *plane,
732                                     struct drm_plane_state *old_state)
733 {
734         struct drm_crtc *crtc = plane->state->crtc;
735         struct drm_pending_vblank_event *event = NULL;
736         struct vmw_fence_obj *fence = NULL;
737         int ret;
738
739         /* In case of device error, maintain consistent atomic state */
740         if (crtc && plane->state->fb) {
741                 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
742                 struct vmw_framebuffer *vfb =
743                         vmw_framebuffer_to_vfb(plane->state->fb);
744
745                 if (vfb->bo)
746                         ret = vmw_sou_plane_update_bo(dev_priv, plane,
747                                                       old_state, vfb, &fence);
748                 else
749                         ret = vmw_sou_plane_update_surface(dev_priv, plane,
750                                                            old_state, vfb,
751                                                            &fence);
752                 if (ret != 0)
753                         DRM_ERROR("Failed to update screen.\n");
754         } else {
755                 /* Do nothing when fb and crtc is NULL (blank crtc) */
756                 return;
757         }
758
759         /* For error case vblank event is send from vmw_du_crtc_atomic_flush */
760         event = crtc->state->event;
761         if (event && fence) {
762                 struct drm_file *file_priv = event->base.file_priv;
763
764                 ret = vmw_event_fence_action_queue(file_priv,
765                                                    fence,
766                                                    &event->base,
767                                                    &event->event.vbl.tv_sec,
768                                                    &event->event.vbl.tv_usec,
769                                                    true);
770
771                 if (unlikely(ret != 0))
772                         DRM_ERROR("Failed to queue event on fence.\n");
773                 else
774                         crtc->state->event = NULL;
775         }
776
777         if (fence)
778                 vmw_fence_obj_unreference(&fence);
779 }
780
781
782 static const struct drm_plane_funcs vmw_sou_plane_funcs = {
783         .update_plane = drm_atomic_helper_update_plane,
784         .disable_plane = drm_atomic_helper_disable_plane,
785         .destroy = vmw_du_primary_plane_destroy,
786         .reset = vmw_du_plane_reset,
787         .atomic_duplicate_state = vmw_du_plane_duplicate_state,
788         .atomic_destroy_state = vmw_du_plane_destroy_state,
789 };
790
791 static const struct drm_plane_funcs vmw_sou_cursor_funcs = {
792         .update_plane = drm_atomic_helper_update_plane,
793         .disable_plane = drm_atomic_helper_disable_plane,
794         .destroy = vmw_du_cursor_plane_destroy,
795         .reset = vmw_du_plane_reset,
796         .atomic_duplicate_state = vmw_du_plane_duplicate_state,
797         .atomic_destroy_state = vmw_du_plane_destroy_state,
798 };
799
800 /*
801  * Atomic Helpers
802  */
803 static const struct
804 drm_plane_helper_funcs vmw_sou_cursor_plane_helper_funcs = {
805         .atomic_check = vmw_du_cursor_plane_atomic_check,
806         .atomic_update = vmw_du_cursor_plane_atomic_update,
807         .prepare_fb = vmw_du_cursor_plane_prepare_fb,
808         .cleanup_fb = vmw_du_plane_cleanup_fb,
809 };
810
811 static const struct
812 drm_plane_helper_funcs vmw_sou_primary_plane_helper_funcs = {
813         .atomic_check = vmw_du_primary_plane_atomic_check,
814         .atomic_update = vmw_sou_primary_plane_atomic_update,
815         .prepare_fb = vmw_sou_primary_plane_prepare_fb,
816         .cleanup_fb = vmw_sou_primary_plane_cleanup_fb,
817 };
818
819 static const struct drm_crtc_helper_funcs vmw_sou_crtc_helper_funcs = {
820         .prepare = vmw_sou_crtc_helper_prepare,
821         .mode_set_nofb = vmw_sou_crtc_mode_set_nofb,
822         .atomic_check = vmw_du_crtc_atomic_check,
823         .atomic_begin = vmw_du_crtc_atomic_begin,
824         .atomic_flush = vmw_du_crtc_atomic_flush,
825         .atomic_enable = vmw_sou_crtc_atomic_enable,
826         .atomic_disable = vmw_sou_crtc_atomic_disable,
827 };
828
829
830 static int vmw_sou_init(struct vmw_private *dev_priv, unsigned unit)
831 {
832         struct vmw_screen_object_unit *sou;
833         struct drm_device *dev = dev_priv->dev;
834         struct drm_connector *connector;
835         struct drm_encoder *encoder;
836         struct drm_plane *primary, *cursor;
837         struct drm_crtc *crtc;
838         int ret;
839
840         sou = kzalloc(sizeof(*sou), GFP_KERNEL);
841         if (!sou)
842                 return -ENOMEM;
843
844         sou->base.unit = unit;
845         crtc = &sou->base.crtc;
846         encoder = &sou->base.encoder;
847         connector = &sou->base.connector;
848         primary = &sou->base.primary;
849         cursor = &sou->base.cursor;
850
851         sou->base.pref_active = (unit == 0);
852         sou->base.pref_width = dev_priv->initial_width;
853         sou->base.pref_height = dev_priv->initial_height;
854         sou->base.pref_mode = NULL;
855
856         /*
857          * Remove this after enabling atomic because property values can
858          * only exist in a state object
859          */
860         sou->base.is_implicit = false;
861
862         /* Initialize primary plane */
863         vmw_du_plane_reset(primary);
864
865         ret = drm_universal_plane_init(dev, &sou->base.primary,
866                                        0, &vmw_sou_plane_funcs,
867                                        vmw_primary_plane_formats,
868                                        ARRAY_SIZE(vmw_primary_plane_formats),
869                                        NULL, DRM_PLANE_TYPE_PRIMARY, NULL);
870         if (ret) {
871                 DRM_ERROR("Failed to initialize primary plane");
872                 goto err_free;
873         }
874
875         drm_plane_helper_add(primary, &vmw_sou_primary_plane_helper_funcs);
876         drm_plane_enable_fb_damage_clips(primary);
877
878         /* Initialize cursor plane */
879         vmw_du_plane_reset(cursor);
880
881         ret = drm_universal_plane_init(dev, &sou->base.cursor,
882                         0, &vmw_sou_cursor_funcs,
883                         vmw_cursor_plane_formats,
884                         ARRAY_SIZE(vmw_cursor_plane_formats),
885                         NULL, DRM_PLANE_TYPE_CURSOR, NULL);
886         if (ret) {
887                 DRM_ERROR("Failed to initialize cursor plane");
888                 drm_plane_cleanup(&sou->base.primary);
889                 goto err_free;
890         }
891
892         drm_plane_helper_add(cursor, &vmw_sou_cursor_plane_helper_funcs);
893
894         vmw_du_connector_reset(connector);
895         ret = drm_connector_init(dev, connector, &vmw_sou_connector_funcs,
896                                  DRM_MODE_CONNECTOR_VIRTUAL);
897         if (ret) {
898                 DRM_ERROR("Failed to initialize connector\n");
899                 goto err_free;
900         }
901
902         drm_connector_helper_add(connector, &vmw_sou_connector_helper_funcs);
903         connector->status = vmw_du_connector_detect(connector, true);
904
905         ret = drm_encoder_init(dev, encoder, &vmw_screen_object_encoder_funcs,
906                                DRM_MODE_ENCODER_VIRTUAL, NULL);
907         if (ret) {
908                 DRM_ERROR("Failed to initialize encoder\n");
909                 goto err_free_connector;
910         }
911
912         (void) drm_connector_attach_encoder(connector, encoder);
913         encoder->possible_crtcs = (1 << unit);
914         encoder->possible_clones = 0;
915
916         ret = drm_connector_register(connector);
917         if (ret) {
918                 DRM_ERROR("Failed to register connector\n");
919                 goto err_free_encoder;
920         }
921
922
923         vmw_du_crtc_reset(crtc);
924         ret = drm_crtc_init_with_planes(dev, crtc, &sou->base.primary,
925                                         &sou->base.cursor,
926                                         &vmw_screen_object_crtc_funcs, NULL);
927         if (ret) {
928                 DRM_ERROR("Failed to initialize CRTC\n");
929                 goto err_free_unregister;
930         }
931
932         drm_crtc_helper_add(crtc, &vmw_sou_crtc_helper_funcs);
933
934         drm_mode_crtc_set_gamma_size(crtc, 256);
935
936         drm_object_attach_property(&connector->base,
937                                    dev_priv->hotplug_mode_update_property, 1);
938         drm_object_attach_property(&connector->base,
939                                    dev->mode_config.suggested_x_property, 0);
940         drm_object_attach_property(&connector->base,
941                                    dev->mode_config.suggested_y_property, 0);
942         return 0;
943
944 err_free_unregister:
945         drm_connector_unregister(connector);
946 err_free_encoder:
947         drm_encoder_cleanup(encoder);
948 err_free_connector:
949         drm_connector_cleanup(connector);
950 err_free:
951         kfree(sou);
952         return ret;
953 }
954
955 int vmw_kms_sou_init_display(struct vmw_private *dev_priv)
956 {
957         struct drm_device *dev = dev_priv->dev;
958         int i, ret;
959
960         if (!(dev_priv->capabilities & SVGA_CAP_SCREEN_OBJECT_2)) {
961                 DRM_INFO("Not using screen objects,"
962                          " missing cap SCREEN_OBJECT_2\n");
963                 return -ENOSYS;
964         }
965
966         ret = -ENOMEM;
967
968         ret = drm_vblank_init(dev, VMWGFX_NUM_DISPLAY_UNITS);
969         if (unlikely(ret != 0))
970                 return ret;
971
972         for (i = 0; i < VMWGFX_NUM_DISPLAY_UNITS; ++i)
973                 vmw_sou_init(dev_priv, i);
974
975         dev_priv->active_display_unit = vmw_du_screen_object;
976
977         DRM_INFO("Screen Objects Display Unit initialized\n");
978
979         return 0;
980 }
981
982 static int do_bo_define_gmrfb(struct vmw_private *dev_priv,
983                                   struct vmw_framebuffer *framebuffer)
984 {
985         struct vmw_buffer_object *buf =
986                 container_of(framebuffer, struct vmw_framebuffer_bo,
987                              base)->buffer;
988         int depth = framebuffer->base.format->depth;
989         struct {
990                 uint32_t header;
991                 SVGAFifoCmdDefineGMRFB body;
992         } *cmd;
993
994         /* Emulate RGBA support, contrary to svga_reg.h this is not
995          * supported by hosts. This is only a problem if we are reading
996          * this value later and expecting what we uploaded back.
997          */
998         if (depth == 32)
999                 depth = 24;
1000
1001         cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
1002         if (!cmd) {
1003                 DRM_ERROR("Out of fifo space for dirty framebuffer command.\n");
1004                 return -ENOMEM;
1005         }
1006
1007         cmd->header = SVGA_CMD_DEFINE_GMRFB;
1008         cmd->body.format.bitsPerPixel = framebuffer->base.format->cpp[0] * 8;
1009         cmd->body.format.colorDepth = depth;
1010         cmd->body.format.reserved = 0;
1011         cmd->body.bytesPerLine = framebuffer->base.pitches[0];
1012         /* Buffer is reserved in vram or GMR */
1013         vmw_bo_get_guest_ptr(&buf->base, &cmd->body.ptr);
1014         vmw_fifo_commit(dev_priv, sizeof(*cmd));
1015
1016         return 0;
1017 }
1018
1019 /**
1020  * vmw_sou_surface_fifo_commit - Callback to fill in and submit a
1021  * blit surface to screen command.
1022  *
1023  * @dirty: The closure structure.
1024  *
1025  * Fills in the missing fields in the command, and translates the cliprects
1026  * to match the destination bounding box encoded.
1027  */
1028 static void vmw_sou_surface_fifo_commit(struct vmw_kms_dirty *dirty)
1029 {
1030         struct vmw_kms_sou_surface_dirty *sdirty =
1031                 container_of(dirty, typeof(*sdirty), base);
1032         struct vmw_kms_sou_dirty_cmd *cmd = dirty->cmd;
1033         s32 trans_x = dirty->unit->crtc.x - sdirty->dst_x;
1034         s32 trans_y = dirty->unit->crtc.y - sdirty->dst_y;
1035         size_t region_size = dirty->num_hits * sizeof(SVGASignedRect);
1036         SVGASignedRect *blit = (SVGASignedRect *) &cmd[1];
1037         int i;
1038
1039         if (!dirty->num_hits) {
1040                 vmw_fifo_commit(dirty->dev_priv, 0);
1041                 return;
1042         }
1043
1044         cmd->header.id = SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN;
1045         cmd->header.size = sizeof(cmd->body) + region_size;
1046
1047         /*
1048          * Use the destination bounding box to specify destination - and
1049          * source bounding regions.
1050          */
1051         cmd->body.destRect.left = sdirty->left;
1052         cmd->body.destRect.right = sdirty->right;
1053         cmd->body.destRect.top = sdirty->top;
1054         cmd->body.destRect.bottom = sdirty->bottom;
1055
1056         cmd->body.srcRect.left = sdirty->left + trans_x;
1057         cmd->body.srcRect.right = sdirty->right + trans_x;
1058         cmd->body.srcRect.top = sdirty->top + trans_y;
1059         cmd->body.srcRect.bottom = sdirty->bottom + trans_y;
1060
1061         cmd->body.srcImage.sid = sdirty->sid;
1062         cmd->body.destScreenId = dirty->unit->unit;
1063
1064         /* Blits are relative to the destination rect. Translate. */
1065         for (i = 0; i < dirty->num_hits; ++i, ++blit) {
1066                 blit->left -= sdirty->left;
1067                 blit->right -= sdirty->left;
1068                 blit->top -= sdirty->top;
1069                 blit->bottom -= sdirty->top;
1070         }
1071
1072         vmw_fifo_commit(dirty->dev_priv, region_size + sizeof(*cmd));
1073
1074         sdirty->left = sdirty->top = S32_MAX;
1075         sdirty->right = sdirty->bottom = S32_MIN;
1076 }
1077
1078 /**
1079  * vmw_sou_surface_clip - Callback to encode a blit surface to screen cliprect.
1080  *
1081  * @dirty: The closure structure
1082  *
1083  * Encodes a SVGASignedRect cliprect and updates the bounding box of the
1084  * BLIT_SURFACE_TO_SCREEN command.
1085  */
1086 static void vmw_sou_surface_clip(struct vmw_kms_dirty *dirty)
1087 {
1088         struct vmw_kms_sou_surface_dirty *sdirty =
1089                 container_of(dirty, typeof(*sdirty), base);
1090         struct vmw_kms_sou_dirty_cmd *cmd = dirty->cmd;
1091         SVGASignedRect *blit = (SVGASignedRect *) &cmd[1];
1092
1093         /* Destination rect. */
1094         blit += dirty->num_hits;
1095         blit->left = dirty->unit_x1;
1096         blit->top = dirty->unit_y1;
1097         blit->right = dirty->unit_x2;
1098         blit->bottom = dirty->unit_y2;
1099
1100         /* Destination bounding box */
1101         sdirty->left = min_t(s32, sdirty->left, dirty->unit_x1);
1102         sdirty->top = min_t(s32, sdirty->top, dirty->unit_y1);
1103         sdirty->right = max_t(s32, sdirty->right, dirty->unit_x2);
1104         sdirty->bottom = max_t(s32, sdirty->bottom, dirty->unit_y2);
1105
1106         dirty->num_hits++;
1107 }
1108
1109 /**
1110  * vmw_kms_sou_do_surface_dirty - Dirty part of a surface backed framebuffer
1111  *
1112  * @dev_priv: Pointer to the device private structure.
1113  * @framebuffer: Pointer to the surface-buffer backed framebuffer.
1114  * @clips: Array of clip rects. Either @clips or @vclips must be NULL.
1115  * @vclips: Alternate array of clip rects. Either @clips or @vclips must
1116  * be NULL.
1117  * @srf: Pointer to surface to blit from. If NULL, the surface attached
1118  * to @framebuffer will be used.
1119  * @dest_x: X coordinate offset to align @srf with framebuffer coordinates.
1120  * @dest_y: Y coordinate offset to align @srf with framebuffer coordinates.
1121  * @num_clips: Number of clip rects in @clips.
1122  * @inc: Increment to use when looping over @clips.
1123  * @out_fence: If non-NULL, will return a ref-counted pointer to a
1124  * struct vmw_fence_obj. The returned fence pointer may be NULL in which
1125  * case the device has already synchronized.
1126  * @crtc: If crtc is passed, perform surface dirty on that crtc only.
1127  *
1128  * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
1129  * interrupted.
1130  */
1131 int vmw_kms_sou_do_surface_dirty(struct vmw_private *dev_priv,
1132                                  struct vmw_framebuffer *framebuffer,
1133                                  struct drm_clip_rect *clips,
1134                                  struct drm_vmw_rect *vclips,
1135                                  struct vmw_resource *srf,
1136                                  s32 dest_x,
1137                                  s32 dest_y,
1138                                  unsigned num_clips, int inc,
1139                                  struct vmw_fence_obj **out_fence,
1140                                  struct drm_crtc *crtc)
1141 {
1142         struct vmw_framebuffer_surface *vfbs =
1143                 container_of(framebuffer, typeof(*vfbs), base);
1144         struct vmw_kms_sou_surface_dirty sdirty;
1145         DECLARE_VAL_CONTEXT(val_ctx, NULL, 0);
1146         int ret;
1147
1148         if (!srf)
1149                 srf = &vfbs->surface->res;
1150
1151         ret = vmw_validation_add_resource(&val_ctx, srf, 0, NULL, NULL);
1152         if (ret)
1153                 return ret;
1154
1155         ret = vmw_validation_prepare(&val_ctx, &dev_priv->cmdbuf_mutex, true);
1156         if (ret)
1157                 goto out_unref;
1158
1159         sdirty.base.fifo_commit = vmw_sou_surface_fifo_commit;
1160         sdirty.base.clip = vmw_sou_surface_clip;
1161         sdirty.base.dev_priv = dev_priv;
1162         sdirty.base.fifo_reserve_size = sizeof(struct vmw_kms_sou_dirty_cmd) +
1163           sizeof(SVGASignedRect) * num_clips;
1164         sdirty.base.crtc = crtc;
1165
1166         sdirty.sid = srf->id;
1167         sdirty.left = sdirty.top = S32_MAX;
1168         sdirty.right = sdirty.bottom = S32_MIN;
1169         sdirty.dst_x = dest_x;
1170         sdirty.dst_y = dest_y;
1171
1172         ret = vmw_kms_helper_dirty(dev_priv, framebuffer, clips, vclips,
1173                                    dest_x, dest_y, num_clips, inc,
1174                                    &sdirty.base);
1175         vmw_kms_helper_validation_finish(dev_priv, NULL, &val_ctx, out_fence,
1176                                          NULL);
1177
1178         return ret;
1179
1180 out_unref:
1181         vmw_validation_unref_lists(&val_ctx);
1182         return ret;
1183 }
1184
1185 /**
1186  * vmw_sou_bo_fifo_commit - Callback to submit a set of readback clips.
1187  *
1188  * @dirty: The closure structure.
1189  *
1190  * Commits a previously built command buffer of readback clips.
1191  */
1192 static void vmw_sou_bo_fifo_commit(struct vmw_kms_dirty *dirty)
1193 {
1194         if (!dirty->num_hits) {
1195                 vmw_fifo_commit(dirty->dev_priv, 0);
1196                 return;
1197         }
1198
1199         vmw_fifo_commit(dirty->dev_priv,
1200                         sizeof(struct vmw_kms_sou_bo_blit) *
1201                         dirty->num_hits);
1202 }
1203
1204 /**
1205  * vmw_sou_bo_clip - Callback to encode a readback cliprect.
1206  *
1207  * @dirty: The closure structure
1208  *
1209  * Encodes a BLIT_GMRFB_TO_SCREEN cliprect.
1210  */
1211 static void vmw_sou_bo_clip(struct vmw_kms_dirty *dirty)
1212 {
1213         struct vmw_kms_sou_bo_blit *blit = dirty->cmd;
1214
1215         blit += dirty->num_hits;
1216         blit->header = SVGA_CMD_BLIT_GMRFB_TO_SCREEN;
1217         blit->body.destScreenId = dirty->unit->unit;
1218         blit->body.srcOrigin.x = dirty->fb_x;
1219         blit->body.srcOrigin.y = dirty->fb_y;
1220         blit->body.destRect.left = dirty->unit_x1;
1221         blit->body.destRect.top = dirty->unit_y1;
1222         blit->body.destRect.right = dirty->unit_x2;
1223         blit->body.destRect.bottom = dirty->unit_y2;
1224         dirty->num_hits++;
1225 }
1226
1227 /**
1228  * vmw_kms_do_bo_dirty - Dirty part of a buffer-object backed framebuffer
1229  *
1230  * @dev_priv: Pointer to the device private structure.
1231  * @framebuffer: Pointer to the buffer-object backed framebuffer.
1232  * @clips: Array of clip rects.
1233  * @vclips: Alternate array of clip rects. Either @clips or @vclips must
1234  * be NULL.
1235  * @num_clips: Number of clip rects in @clips.
1236  * @increment: Increment to use when looping over @clips.
1237  * @interruptible: Whether to perform waits interruptible if possible.
1238  * @out_fence: If non-NULL, will return a ref-counted pointer to a
1239  * struct vmw_fence_obj. The returned fence pointer may be NULL in which
1240  * case the device has already synchronized.
1241  * @crtc: If crtc is passed, perform bo dirty on that crtc only.
1242  *
1243  * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
1244  * interrupted.
1245  */
1246 int vmw_kms_sou_do_bo_dirty(struct vmw_private *dev_priv,
1247                                 struct vmw_framebuffer *framebuffer,
1248                                 struct drm_clip_rect *clips,
1249                                 struct drm_vmw_rect *vclips,
1250                                 unsigned num_clips, int increment,
1251                                 bool interruptible,
1252                                 struct vmw_fence_obj **out_fence,
1253                                 struct drm_crtc *crtc)
1254 {
1255         struct vmw_buffer_object *buf =
1256                 container_of(framebuffer, struct vmw_framebuffer_bo,
1257                              base)->buffer;
1258         struct vmw_kms_dirty dirty;
1259         DECLARE_VAL_CONTEXT(val_ctx, NULL, 0);
1260         int ret;
1261
1262         ret = vmw_validation_add_bo(&val_ctx, buf, false, false);
1263         if (ret)
1264                 return ret;
1265
1266         ret = vmw_validation_prepare(&val_ctx, NULL, interruptible);
1267         if (ret)
1268                 goto out_unref;
1269
1270         ret = do_bo_define_gmrfb(dev_priv, framebuffer);
1271         if (unlikely(ret != 0))
1272                 goto out_revert;
1273
1274         dirty.crtc = crtc;
1275         dirty.fifo_commit = vmw_sou_bo_fifo_commit;
1276         dirty.clip = vmw_sou_bo_clip;
1277         dirty.fifo_reserve_size = sizeof(struct vmw_kms_sou_bo_blit) *
1278                 num_clips;
1279         ret = vmw_kms_helper_dirty(dev_priv, framebuffer, clips, vclips,
1280                                    0, 0, num_clips, increment, &dirty);
1281         vmw_kms_helper_validation_finish(dev_priv, NULL, &val_ctx, out_fence,
1282                                          NULL);
1283
1284         return ret;
1285
1286 out_revert:
1287         vmw_validation_revert(&val_ctx);
1288 out_unref:
1289         vmw_validation_unref_lists(&val_ctx);
1290
1291         return ret;
1292 }
1293
1294
1295 /**
1296  * vmw_sou_readback_fifo_commit - Callback to submit a set of readback clips.
1297  *
1298  * @dirty: The closure structure.
1299  *
1300  * Commits a previously built command buffer of readback clips.
1301  */
1302 static void vmw_sou_readback_fifo_commit(struct vmw_kms_dirty *dirty)
1303 {
1304         if (!dirty->num_hits) {
1305                 vmw_fifo_commit(dirty->dev_priv, 0);
1306                 return;
1307         }
1308
1309         vmw_fifo_commit(dirty->dev_priv,
1310                         sizeof(struct vmw_kms_sou_readback_blit) *
1311                         dirty->num_hits);
1312 }
1313
1314 /**
1315  * vmw_sou_readback_clip - Callback to encode a readback cliprect.
1316  *
1317  * @dirty: The closure structure
1318  *
1319  * Encodes a BLIT_SCREEN_TO_GMRFB cliprect.
1320  */
1321 static void vmw_sou_readback_clip(struct vmw_kms_dirty *dirty)
1322 {
1323         struct vmw_kms_sou_readback_blit *blit = dirty->cmd;
1324
1325         blit += dirty->num_hits;
1326         blit->header = SVGA_CMD_BLIT_SCREEN_TO_GMRFB;
1327         blit->body.srcScreenId = dirty->unit->unit;
1328         blit->body.destOrigin.x = dirty->fb_x;
1329         blit->body.destOrigin.y = dirty->fb_y;
1330         blit->body.srcRect.left = dirty->unit_x1;
1331         blit->body.srcRect.top = dirty->unit_y1;
1332         blit->body.srcRect.right = dirty->unit_x2;
1333         blit->body.srcRect.bottom = dirty->unit_y2;
1334         dirty->num_hits++;
1335 }
1336
1337 /**
1338  * vmw_kms_sou_readback - Perform a readback from the screen object system to
1339  * a buffer-object backed framebuffer.
1340  *
1341  * @dev_priv: Pointer to the device private structure.
1342  * @file_priv: Pointer to a struct drm_file identifying the caller.
1343  * Must be set to NULL if @user_fence_rep is NULL.
1344  * @vfb: Pointer to the buffer-object backed framebuffer.
1345  * @user_fence_rep: User-space provided structure for fence information.
1346  * Must be set to non-NULL if @file_priv is non-NULL.
1347  * @vclips: Array of clip rects.
1348  * @num_clips: Number of clip rects in @vclips.
1349  * @crtc: If crtc is passed, readback on that crtc only.
1350  *
1351  * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
1352  * interrupted.
1353  */
1354 int vmw_kms_sou_readback(struct vmw_private *dev_priv,
1355                          struct drm_file *file_priv,
1356                          struct vmw_framebuffer *vfb,
1357                          struct drm_vmw_fence_rep __user *user_fence_rep,
1358                          struct drm_vmw_rect *vclips,
1359                          uint32_t num_clips,
1360                          struct drm_crtc *crtc)
1361 {
1362         struct vmw_buffer_object *buf =
1363                 container_of(vfb, struct vmw_framebuffer_bo, base)->buffer;
1364         struct vmw_kms_dirty dirty;
1365         DECLARE_VAL_CONTEXT(val_ctx, NULL, 0);
1366         int ret;
1367
1368         ret = vmw_validation_add_bo(&val_ctx, buf, false, false);
1369         if (ret)
1370                 return ret;
1371
1372         ret = vmw_validation_prepare(&val_ctx, NULL, true);
1373         if (ret)
1374                 goto out_unref;
1375
1376         ret = do_bo_define_gmrfb(dev_priv, vfb);
1377         if (unlikely(ret != 0))
1378                 goto out_revert;
1379
1380         dirty.crtc = crtc;
1381         dirty.fifo_commit = vmw_sou_readback_fifo_commit;
1382         dirty.clip = vmw_sou_readback_clip;
1383         dirty.fifo_reserve_size = sizeof(struct vmw_kms_sou_readback_blit) *
1384                 num_clips;
1385         ret = vmw_kms_helper_dirty(dev_priv, vfb, NULL, vclips,
1386                                    0, 0, num_clips, 1, &dirty);
1387         vmw_kms_helper_validation_finish(dev_priv, file_priv, &val_ctx, NULL,
1388                                          user_fence_rep);
1389
1390         return ret;
1391
1392 out_revert:
1393         vmw_validation_revert(&val_ctx);
1394 out_unref:
1395         vmw_validation_unref_lists(&val_ctx);
1396         
1397         return ret;
1398 }