Loading...
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 */
51struct 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 */
62struct vmw_kms_sou_readback_blit {
63 uint32 header;
64 SVGAFifoCmdBlitScreenToGMRFB body;
65};
66
67struct vmw_kms_sou_dmabuf_blit {
68 uint32 header;
69 SVGAFifoCmdBlitGMRFBToScreen body;
70};
71
72struct vmw_kms_sou_dirty_cmd {
73 SVGA3dCmdHeader header;
74 SVGA3dCmdBlitSurfaceToScreen body;
75};
76
77/**
78 * Display unit using screen objects.
79 */
80struct 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
89static 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
100static 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 */
108static 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 */
164static 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 */
208static 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 */
218static 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
251static int vmw_sou_crtc_set_config(struct drm_mode_set *set)
252{
253 struct vmw_private *dev_priv;
254 struct vmw_screen_object_unit *sou;
255 struct drm_connector *connector;
256 struct drm_display_mode *mode;
257 struct drm_encoder *encoder;
258 struct vmw_framebuffer *vfb;
259 struct drm_framebuffer *fb;
260 struct drm_crtc *crtc;
261 int ret = 0;
262
263 if (!set)
264 return -EINVAL;
265
266 if (!set->crtc)
267 return -EINVAL;
268
269 /* get the sou */
270 crtc = set->crtc;
271 sou = vmw_crtc_to_sou(crtc);
272 vfb = set->fb ? vmw_framebuffer_to_vfb(set->fb) : NULL;
273 dev_priv = vmw_priv(crtc->dev);
274
275 if (set->num_connectors > 1) {
276 DRM_ERROR("Too many connectors\n");
277 return -EINVAL;
278 }
279
280 if (set->num_connectors == 1 &&
281 set->connectors[0] != &sou->base.connector) {
282 DRM_ERROR("Connector doesn't match %p %p\n",
283 set->connectors[0], &sou->base.connector);
284 return -EINVAL;
285 }
286
287 /* Only one active implicit frame-buffer at a time. */
288 if (sou->base.is_implicit &&
289 dev_priv->implicit_fb && vfb &&
290 !(dev_priv->num_implicit == 1 &&
291 sou->base.active_implicit) &&
292 dev_priv->implicit_fb != vfb) {
293 DRM_ERROR("Multiple implicit framebuffers not supported.\n");
294 return -EINVAL;
295 }
296
297 /* since they always map one to one these are safe */
298 connector = &sou->base.connector;
299 encoder = &sou->base.encoder;
300
301 /* should we turn the crtc off */
302 if (set->num_connectors == 0 || !set->mode || !set->fb) {
303 ret = vmw_sou_fifo_destroy(dev_priv, sou);
304 /* the hardware has hung don't do anything more */
305 if (unlikely(ret != 0))
306 return ret;
307
308 connector->encoder = NULL;
309 encoder->crtc = NULL;
310 crtc->primary->fb = NULL;
311 crtc->x = 0;
312 crtc->y = 0;
313 crtc->enabled = false;
314
315 vmw_kms_del_active(dev_priv, &sou->base);
316
317 vmw_sou_backing_free(dev_priv, sou);
318
319 return 0;
320 }
321
322
323 /* we now know we want to set a mode */
324 mode = set->mode;
325 fb = set->fb;
326
327 if (set->x + mode->hdisplay > fb->width ||
328 set->y + mode->vdisplay > fb->height) {
329 DRM_ERROR("set outside of framebuffer\n");
330 return -EINVAL;
331 }
332
333 vmw_svga_enable(dev_priv);
334
335 if (mode->hdisplay != crtc->mode.hdisplay ||
336 mode->vdisplay != crtc->mode.vdisplay) {
337 /* no need to check if depth is different, because backing
338 * store depth is forced to 4 by the device.
339 */
340
341 ret = vmw_sou_fifo_destroy(dev_priv, sou);
342 /* the hardware has hung don't do anything more */
343 if (unlikely(ret != 0))
344 return ret;
345
346 vmw_sou_backing_free(dev_priv, sou);
347 }
348
349 if (!sou->buffer) {
350 /* forced to depth 4 by the device */
351 size_t size = mode->hdisplay * mode->vdisplay * 4;
352 ret = vmw_sou_backing_alloc(dev_priv, sou, size);
353 if (unlikely(ret != 0))
354 return ret;
355 }
356
357 ret = vmw_sou_fifo_create(dev_priv, sou, set->x, set->y, mode);
358 if (unlikely(ret != 0)) {
359 /*
360 * We are in a bit of a situation here, the hardware has
361 * hung and we may or may not have a buffer hanging of
362 * the screen object, best thing to do is not do anything
363 * if we where defined, if not just turn the crtc of.
364 * Not what userspace wants but it needs to htfu.
365 */
366 if (sou->defined)
367 return ret;
368
369 connector->encoder = NULL;
370 encoder->crtc = NULL;
371 crtc->primary->fb = NULL;
372 crtc->x = 0;
373 crtc->y = 0;
374 crtc->enabled = false;
375
376 return ret;
377 }
378
379 vmw_kms_add_active(dev_priv, &sou->base, vfb);
380
381 connector->encoder = encoder;
382 encoder->crtc = crtc;
383 crtc->mode = *mode;
384 crtc->primary->fb = fb;
385 crtc->x = set->x;
386 crtc->y = set->y;
387 crtc->enabled = true;
388
389 return 0;
390}
391
392static int vmw_sou_crtc_page_flip(struct drm_crtc *crtc,
393 struct drm_framebuffer *fb,
394 struct drm_pending_vblank_event *event,
395 uint32_t flags)
396{
397 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
398 struct drm_framebuffer *old_fb = crtc->primary->fb;
399 struct vmw_framebuffer *vfb = vmw_framebuffer_to_vfb(fb);
400 struct vmw_fence_obj *fence = NULL;
401 struct drm_vmw_rect vclips;
402 int ret;
403
404 if (!vmw_kms_crtc_flippable(dev_priv, crtc))
405 return -EINVAL;
406
407 crtc->primary->fb = fb;
408
409 /* do a full screen dirty update */
410 vclips.x = crtc->x;
411 vclips.y = crtc->y;
412 vclips.w = crtc->mode.hdisplay;
413 vclips.h = crtc->mode.vdisplay;
414
415 if (vfb->dmabuf)
416 ret = vmw_kms_sou_do_dmabuf_dirty(dev_priv, vfb,
417 NULL, &vclips, 1, 1,
418 true, &fence);
419 else
420 ret = vmw_kms_sou_do_surface_dirty(dev_priv, vfb,
421 NULL, &vclips, NULL,
422 0, 0, 1, 1, &fence);
423
424
425 if (ret != 0)
426 goto out_no_fence;
427 if (!fence) {
428 ret = -EINVAL;
429 goto out_no_fence;
430 }
431
432 if (event) {
433 struct drm_file *file_priv = event->base.file_priv;
434
435 ret = vmw_event_fence_action_queue(file_priv, fence,
436 &event->base,
437 &event->event.tv_sec,
438 &event->event.tv_usec,
439 true);
440 }
441
442 /*
443 * No need to hold on to this now. The only cleanup
444 * we need to do if we fail is unref the fence.
445 */
446 vmw_fence_obj_unreference(&fence);
447
448 if (vmw_crtc_to_du(crtc)->is_implicit)
449 vmw_kms_update_implicit_fb(dev_priv, crtc);
450
451 return ret;
452
453out_no_fence:
454 crtc->primary->fb = old_fb;
455 return ret;
456}
457
458static const struct drm_crtc_funcs vmw_screen_object_crtc_funcs = {
459 .cursor_set2 = vmw_du_crtc_cursor_set2,
460 .cursor_move = vmw_du_crtc_cursor_move,
461 .gamma_set = vmw_du_crtc_gamma_set,
462 .destroy = vmw_sou_crtc_destroy,
463 .set_config = vmw_sou_crtc_set_config,
464 .page_flip = vmw_sou_crtc_page_flip,
465};
466
467/*
468 * Screen Object Display Unit encoder functions
469 */
470
471static void vmw_sou_encoder_destroy(struct drm_encoder *encoder)
472{
473 vmw_sou_destroy(vmw_encoder_to_sou(encoder));
474}
475
476static const struct drm_encoder_funcs vmw_screen_object_encoder_funcs = {
477 .destroy = vmw_sou_encoder_destroy,
478};
479
480/*
481 * Screen Object Display Unit connector functions
482 */
483
484static void vmw_sou_connector_destroy(struct drm_connector *connector)
485{
486 vmw_sou_destroy(vmw_connector_to_sou(connector));
487}
488
489static const struct drm_connector_funcs vmw_sou_connector_funcs = {
490 .dpms = vmw_du_connector_dpms,
491 .detect = vmw_du_connector_detect,
492 .fill_modes = vmw_du_connector_fill_modes,
493 .set_property = vmw_du_connector_set_property,
494 .destroy = vmw_sou_connector_destroy,
495};
496
497static int vmw_sou_init(struct vmw_private *dev_priv, unsigned unit)
498{
499 struct vmw_screen_object_unit *sou;
500 struct drm_device *dev = dev_priv->dev;
501 struct drm_connector *connector;
502 struct drm_encoder *encoder;
503 struct drm_crtc *crtc;
504
505 sou = kzalloc(sizeof(*sou), GFP_KERNEL);
506 if (!sou)
507 return -ENOMEM;
508
509 sou->base.unit = unit;
510 crtc = &sou->base.crtc;
511 encoder = &sou->base.encoder;
512 connector = &sou->base.connector;
513
514 sou->base.active_implicit = false;
515 sou->base.pref_active = (unit == 0);
516 sou->base.pref_width = dev_priv->initial_width;
517 sou->base.pref_height = dev_priv->initial_height;
518 sou->base.pref_mode = NULL;
519 sou->base.is_implicit = false;
520
521 drm_connector_init(dev, connector, &vmw_sou_connector_funcs,
522 DRM_MODE_CONNECTOR_VIRTUAL);
523 connector->status = vmw_du_connector_detect(connector, true);
524
525 drm_encoder_init(dev, encoder, &vmw_screen_object_encoder_funcs,
526 DRM_MODE_ENCODER_VIRTUAL, NULL);
527 drm_mode_connector_attach_encoder(connector, encoder);
528 encoder->possible_crtcs = (1 << unit);
529 encoder->possible_clones = 0;
530
531 (void) drm_connector_register(connector);
532
533 drm_crtc_init(dev, crtc, &vmw_screen_object_crtc_funcs);
534
535 drm_mode_crtc_set_gamma_size(crtc, 256);
536
537 drm_object_attach_property(&connector->base,
538 dev->mode_config.dirty_info_property,
539 1);
540 drm_object_attach_property(&connector->base,
541 dev_priv->hotplug_mode_update_property, 1);
542 drm_object_attach_property(&connector->base,
543 dev->mode_config.suggested_x_property, 0);
544 drm_object_attach_property(&connector->base,
545 dev->mode_config.suggested_y_property, 0);
546 if (dev_priv->implicit_placement_property)
547 drm_object_attach_property
548 (&connector->base,
549 dev_priv->implicit_placement_property,
550 sou->base.is_implicit);
551
552 return 0;
553}
554
555int vmw_kms_sou_init_display(struct vmw_private *dev_priv)
556{
557 struct drm_device *dev = dev_priv->dev;
558 int i, ret;
559
560 if (!(dev_priv->capabilities & SVGA_CAP_SCREEN_OBJECT_2)) {
561 DRM_INFO("Not using screen objects,"
562 " missing cap SCREEN_OBJECT_2\n");
563 return -ENOSYS;
564 }
565
566 ret = -ENOMEM;
567 dev_priv->num_implicit = 0;
568 dev_priv->implicit_fb = NULL;
569
570 ret = drm_vblank_init(dev, VMWGFX_NUM_DISPLAY_UNITS);
571 if (unlikely(ret != 0))
572 return ret;
573
574 ret = drm_mode_create_dirty_info_property(dev);
575 if (unlikely(ret != 0))
576 goto err_vblank_cleanup;
577
578 vmw_kms_create_implicit_placement_property(dev_priv, false);
579
580 for (i = 0; i < VMWGFX_NUM_DISPLAY_UNITS; ++i)
581 vmw_sou_init(dev_priv, i);
582
583 dev_priv->active_display_unit = vmw_du_screen_object;
584
585 DRM_INFO("Screen Objects Display Unit initialized\n");
586
587 return 0;
588
589err_vblank_cleanup:
590 drm_vblank_cleanup(dev);
591 return ret;
592}
593
594int vmw_kms_sou_close_display(struct vmw_private *dev_priv)
595{
596 struct drm_device *dev = dev_priv->dev;
597
598 drm_vblank_cleanup(dev);
599
600 return 0;
601}
602
603static int do_dmabuf_define_gmrfb(struct vmw_private *dev_priv,
604 struct vmw_framebuffer *framebuffer)
605{
606 struct vmw_dma_buffer *buf =
607 container_of(framebuffer, struct vmw_framebuffer_dmabuf,
608 base)->buffer;
609 int depth = framebuffer->base.depth;
610 struct {
611 uint32_t header;
612 SVGAFifoCmdDefineGMRFB body;
613 } *cmd;
614
615 /* Emulate RGBA support, contrary to svga_reg.h this is not
616 * supported by hosts. This is only a problem if we are reading
617 * this value later and expecting what we uploaded back.
618 */
619 if (depth == 32)
620 depth = 24;
621
622 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
623 if (!cmd) {
624 DRM_ERROR("Out of fifo space for dirty framebuffer command.\n");
625 return -ENOMEM;
626 }
627
628 cmd->header = SVGA_CMD_DEFINE_GMRFB;
629 cmd->body.format.bitsPerPixel = framebuffer->base.bits_per_pixel;
630 cmd->body.format.colorDepth = depth;
631 cmd->body.format.reserved = 0;
632 cmd->body.bytesPerLine = framebuffer->base.pitches[0];
633 /* Buffer is reserved in vram or GMR */
634 vmw_bo_get_guest_ptr(&buf->base, &cmd->body.ptr);
635 vmw_fifo_commit(dev_priv, sizeof(*cmd));
636
637 return 0;
638}
639
640/**
641 * vmw_sou_surface_fifo_commit - Callback to fill in and submit a
642 * blit surface to screen command.
643 *
644 * @dirty: The closure structure.
645 *
646 * Fills in the missing fields in the command, and translates the cliprects
647 * to match the destination bounding box encoded.
648 */
649static void vmw_sou_surface_fifo_commit(struct vmw_kms_dirty *dirty)
650{
651 struct vmw_kms_sou_surface_dirty *sdirty =
652 container_of(dirty, typeof(*sdirty), base);
653 struct vmw_kms_sou_dirty_cmd *cmd = dirty->cmd;
654 s32 trans_x = dirty->unit->crtc.x - sdirty->dst_x;
655 s32 trans_y = dirty->unit->crtc.y - sdirty->dst_y;
656 size_t region_size = dirty->num_hits * sizeof(SVGASignedRect);
657 SVGASignedRect *blit = (SVGASignedRect *) &cmd[1];
658 int i;
659
660 if (!dirty->num_hits) {
661 vmw_fifo_commit(dirty->dev_priv, 0);
662 return;
663 }
664
665 cmd->header.id = SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN;
666 cmd->header.size = sizeof(cmd->body) + region_size;
667
668 /*
669 * Use the destination bounding box to specify destination - and
670 * source bounding regions.
671 */
672 cmd->body.destRect.left = sdirty->left;
673 cmd->body.destRect.right = sdirty->right;
674 cmd->body.destRect.top = sdirty->top;
675 cmd->body.destRect.bottom = sdirty->bottom;
676
677 cmd->body.srcRect.left = sdirty->left + trans_x;
678 cmd->body.srcRect.right = sdirty->right + trans_x;
679 cmd->body.srcRect.top = sdirty->top + trans_y;
680 cmd->body.srcRect.bottom = sdirty->bottom + trans_y;
681
682 cmd->body.srcImage.sid = sdirty->sid;
683 cmd->body.destScreenId = dirty->unit->unit;
684
685 /* Blits are relative to the destination rect. Translate. */
686 for (i = 0; i < dirty->num_hits; ++i, ++blit) {
687 blit->left -= sdirty->left;
688 blit->right -= sdirty->left;
689 blit->top -= sdirty->top;
690 blit->bottom -= sdirty->top;
691 }
692
693 vmw_fifo_commit(dirty->dev_priv, region_size + sizeof(*cmd));
694
695 sdirty->left = sdirty->top = S32_MAX;
696 sdirty->right = sdirty->bottom = S32_MIN;
697}
698
699/**
700 * vmw_sou_surface_clip - Callback to encode a blit surface to screen cliprect.
701 *
702 * @dirty: The closure structure
703 *
704 * Encodes a SVGASignedRect cliprect and updates the bounding box of the
705 * BLIT_SURFACE_TO_SCREEN command.
706 */
707static void vmw_sou_surface_clip(struct vmw_kms_dirty *dirty)
708{
709 struct vmw_kms_sou_surface_dirty *sdirty =
710 container_of(dirty, typeof(*sdirty), base);
711 struct vmw_kms_sou_dirty_cmd *cmd = dirty->cmd;
712 SVGASignedRect *blit = (SVGASignedRect *) &cmd[1];
713
714 /* Destination rect. */
715 blit += dirty->num_hits;
716 blit->left = dirty->unit_x1;
717 blit->top = dirty->unit_y1;
718 blit->right = dirty->unit_x2;
719 blit->bottom = dirty->unit_y2;
720
721 /* Destination bounding box */
722 sdirty->left = min_t(s32, sdirty->left, dirty->unit_x1);
723 sdirty->top = min_t(s32, sdirty->top, dirty->unit_y1);
724 sdirty->right = max_t(s32, sdirty->right, dirty->unit_x2);
725 sdirty->bottom = max_t(s32, sdirty->bottom, dirty->unit_y2);
726
727 dirty->num_hits++;
728}
729
730/**
731 * vmw_kms_sou_do_surface_dirty - Dirty part of a surface backed framebuffer
732 *
733 * @dev_priv: Pointer to the device private structure.
734 * @framebuffer: Pointer to the surface-buffer backed framebuffer.
735 * @clips: Array of clip rects. Either @clips or @vclips must be NULL.
736 * @vclips: Alternate array of clip rects. Either @clips or @vclips must
737 * be NULL.
738 * @srf: Pointer to surface to blit from. If NULL, the surface attached
739 * to @framebuffer will be used.
740 * @dest_x: X coordinate offset to align @srf with framebuffer coordinates.
741 * @dest_y: Y coordinate offset to align @srf with framebuffer coordinates.
742 * @num_clips: Number of clip rects in @clips.
743 * @inc: Increment to use when looping over @clips.
744 * @out_fence: If non-NULL, will return a ref-counted pointer to a
745 * struct vmw_fence_obj. The returned fence pointer may be NULL in which
746 * case the device has already synchronized.
747 *
748 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
749 * interrupted.
750 */
751int vmw_kms_sou_do_surface_dirty(struct vmw_private *dev_priv,
752 struct vmw_framebuffer *framebuffer,
753 struct drm_clip_rect *clips,
754 struct drm_vmw_rect *vclips,
755 struct vmw_resource *srf,
756 s32 dest_x,
757 s32 dest_y,
758 unsigned num_clips, int inc,
759 struct vmw_fence_obj **out_fence)
760{
761 struct vmw_framebuffer_surface *vfbs =
762 container_of(framebuffer, typeof(*vfbs), base);
763 struct vmw_kms_sou_surface_dirty sdirty;
764 int ret;
765
766 if (!srf)
767 srf = &vfbs->surface->res;
768
769 ret = vmw_kms_helper_resource_prepare(srf, true);
770 if (ret)
771 return ret;
772
773 sdirty.base.fifo_commit = vmw_sou_surface_fifo_commit;
774 sdirty.base.clip = vmw_sou_surface_clip;
775 sdirty.base.dev_priv = dev_priv;
776 sdirty.base.fifo_reserve_size = sizeof(struct vmw_kms_sou_dirty_cmd) +
777 sizeof(SVGASignedRect) * num_clips;
778
779 sdirty.sid = srf->id;
780 sdirty.left = sdirty.top = S32_MAX;
781 sdirty.right = sdirty.bottom = S32_MIN;
782 sdirty.dst_x = dest_x;
783 sdirty.dst_y = dest_y;
784
785 ret = vmw_kms_helper_dirty(dev_priv, framebuffer, clips, vclips,
786 dest_x, dest_y, num_clips, inc,
787 &sdirty.base);
788 vmw_kms_helper_resource_finish(srf, out_fence);
789
790 return ret;
791}
792
793/**
794 * vmw_sou_dmabuf_fifo_commit - Callback to submit a set of readback clips.
795 *
796 * @dirty: The closure structure.
797 *
798 * Commits a previously built command buffer of readback clips.
799 */
800static void vmw_sou_dmabuf_fifo_commit(struct vmw_kms_dirty *dirty)
801{
802 if (!dirty->num_hits) {
803 vmw_fifo_commit(dirty->dev_priv, 0);
804 return;
805 }
806
807 vmw_fifo_commit(dirty->dev_priv,
808 sizeof(struct vmw_kms_sou_dmabuf_blit) *
809 dirty->num_hits);
810}
811
812/**
813 * vmw_sou_dmabuf_clip - Callback to encode a readback cliprect.
814 *
815 * @dirty: The closure structure
816 *
817 * Encodes a BLIT_GMRFB_TO_SCREEN cliprect.
818 */
819static void vmw_sou_dmabuf_clip(struct vmw_kms_dirty *dirty)
820{
821 struct vmw_kms_sou_dmabuf_blit *blit = dirty->cmd;
822
823 blit += dirty->num_hits;
824 blit->header = SVGA_CMD_BLIT_GMRFB_TO_SCREEN;
825 blit->body.destScreenId = dirty->unit->unit;
826 blit->body.srcOrigin.x = dirty->fb_x;
827 blit->body.srcOrigin.y = dirty->fb_y;
828 blit->body.destRect.left = dirty->unit_x1;
829 blit->body.destRect.top = dirty->unit_y1;
830 blit->body.destRect.right = dirty->unit_x2;
831 blit->body.destRect.bottom = dirty->unit_y2;
832 dirty->num_hits++;
833}
834
835/**
836 * vmw_kms_do_dmabuf_dirty - Dirty part of a dma-buffer backed framebuffer
837 *
838 * @dev_priv: Pointer to the device private structure.
839 * @framebuffer: Pointer to the dma-buffer backed framebuffer.
840 * @clips: Array of clip rects.
841 * @vclips: Alternate array of clip rects. Either @clips or @vclips must
842 * be NULL.
843 * @num_clips: Number of clip rects in @clips.
844 * @increment: Increment to use when looping over @clips.
845 * @interruptible: Whether to perform waits interruptible if possible.
846 * @out_fence: If non-NULL, will return a ref-counted pointer to a
847 * struct vmw_fence_obj. The returned fence pointer may be NULL in which
848 * case the device has already synchronized.
849 *
850 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
851 * interrupted.
852 */
853int vmw_kms_sou_do_dmabuf_dirty(struct vmw_private *dev_priv,
854 struct vmw_framebuffer *framebuffer,
855 struct drm_clip_rect *clips,
856 struct drm_vmw_rect *vclips,
857 unsigned num_clips, int increment,
858 bool interruptible,
859 struct vmw_fence_obj **out_fence)
860{
861 struct vmw_dma_buffer *buf =
862 container_of(framebuffer, struct vmw_framebuffer_dmabuf,
863 base)->buffer;
864 struct vmw_kms_dirty dirty;
865 int ret;
866
867 ret = vmw_kms_helper_buffer_prepare(dev_priv, buf, interruptible,
868 false);
869 if (ret)
870 return ret;
871
872 ret = do_dmabuf_define_gmrfb(dev_priv, framebuffer);
873 if (unlikely(ret != 0))
874 goto out_revert;
875
876 dirty.fifo_commit = vmw_sou_dmabuf_fifo_commit;
877 dirty.clip = vmw_sou_dmabuf_clip;
878 dirty.fifo_reserve_size = sizeof(struct vmw_kms_sou_dmabuf_blit) *
879 num_clips;
880 ret = vmw_kms_helper_dirty(dev_priv, framebuffer, clips, vclips,
881 0, 0, num_clips, increment, &dirty);
882 vmw_kms_helper_buffer_finish(dev_priv, NULL, buf, out_fence, NULL);
883
884 return ret;
885
886out_revert:
887 vmw_kms_helper_buffer_revert(buf);
888
889 return ret;
890}
891
892
893/**
894 * vmw_sou_readback_fifo_commit - Callback to submit a set of readback clips.
895 *
896 * @dirty: The closure structure.
897 *
898 * Commits a previously built command buffer of readback clips.
899 */
900static void vmw_sou_readback_fifo_commit(struct vmw_kms_dirty *dirty)
901{
902 if (!dirty->num_hits) {
903 vmw_fifo_commit(dirty->dev_priv, 0);
904 return;
905 }
906
907 vmw_fifo_commit(dirty->dev_priv,
908 sizeof(struct vmw_kms_sou_readback_blit) *
909 dirty->num_hits);
910}
911
912/**
913 * vmw_sou_readback_clip - Callback to encode a readback cliprect.
914 *
915 * @dirty: The closure structure
916 *
917 * Encodes a BLIT_SCREEN_TO_GMRFB cliprect.
918 */
919static void vmw_sou_readback_clip(struct vmw_kms_dirty *dirty)
920{
921 struct vmw_kms_sou_readback_blit *blit = dirty->cmd;
922
923 blit += dirty->num_hits;
924 blit->header = SVGA_CMD_BLIT_SCREEN_TO_GMRFB;
925 blit->body.srcScreenId = dirty->unit->unit;
926 blit->body.destOrigin.x = dirty->fb_x;
927 blit->body.destOrigin.y = dirty->fb_y;
928 blit->body.srcRect.left = dirty->unit_x1;
929 blit->body.srcRect.top = dirty->unit_y1;
930 blit->body.srcRect.right = dirty->unit_x2;
931 blit->body.srcRect.bottom = dirty->unit_y2;
932 dirty->num_hits++;
933}
934
935/**
936 * vmw_kms_sou_readback - Perform a readback from the screen object system to
937 * a dma-buffer backed framebuffer.
938 *
939 * @dev_priv: Pointer to the device private structure.
940 * @file_priv: Pointer to a struct drm_file identifying the caller.
941 * Must be set to NULL if @user_fence_rep is NULL.
942 * @vfb: Pointer to the dma-buffer backed framebuffer.
943 * @user_fence_rep: User-space provided structure for fence information.
944 * Must be set to non-NULL if @file_priv is non-NULL.
945 * @vclips: Array of clip rects.
946 * @num_clips: Number of clip rects in @vclips.
947 *
948 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
949 * interrupted.
950 */
951int vmw_kms_sou_readback(struct vmw_private *dev_priv,
952 struct drm_file *file_priv,
953 struct vmw_framebuffer *vfb,
954 struct drm_vmw_fence_rep __user *user_fence_rep,
955 struct drm_vmw_rect *vclips,
956 uint32_t num_clips)
957{
958 struct vmw_dma_buffer *buf =
959 container_of(vfb, struct vmw_framebuffer_dmabuf, base)->buffer;
960 struct vmw_kms_dirty dirty;
961 int ret;
962
963 ret = vmw_kms_helper_buffer_prepare(dev_priv, buf, true, false);
964 if (ret)
965 return ret;
966
967 ret = do_dmabuf_define_gmrfb(dev_priv, vfb);
968 if (unlikely(ret != 0))
969 goto out_revert;
970
971 dirty.fifo_commit = vmw_sou_readback_fifo_commit;
972 dirty.clip = vmw_sou_readback_clip;
973 dirty.fifo_reserve_size = sizeof(struct vmw_kms_sou_readback_blit) *
974 num_clips;
975 ret = vmw_kms_helper_dirty(dev_priv, vfb, NULL, vclips,
976 0, 0, num_clips, 1, &dirty);
977 vmw_kms_helper_buffer_finish(dev_priv, file_priv, buf, NULL,
978 user_fence_rep);
979
980 return ret;
981
982out_revert:
983 vmw_kms_helper_buffer_revert(buf);
984
985 return ret;
986}
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#include <drm/drm_atomic.h>
31#include <drm/drm_atomic_helper.h>
32
33
34#define vmw_crtc_to_sou(x) \
35 container_of(x, struct vmw_screen_object_unit, base.crtc)
36#define vmw_encoder_to_sou(x) \
37 container_of(x, struct vmw_screen_object_unit, base.encoder)
38#define vmw_connector_to_sou(x) \
39 container_of(x, struct vmw_screen_object_unit, base.connector)
40
41/**
42 * struct vmw_kms_sou_surface_dirty - Closure structure for
43 * blit surface to screen command.
44 * @base: The base type we derive from. Used by vmw_kms_helper_dirty().
45 * @left: Left side of bounding box.
46 * @right: Right side of bounding box.
47 * @top: Top side of bounding box.
48 * @bottom: Bottom side of bounding box.
49 * @dst_x: Difference between source clip rects and framebuffer coordinates.
50 * @dst_y: Difference between source clip rects and framebuffer coordinates.
51 * @sid: Surface id of surface to copy from.
52 */
53struct vmw_kms_sou_surface_dirty {
54 struct vmw_kms_dirty base;
55 s32 left, right, top, bottom;
56 s32 dst_x, dst_y;
57 u32 sid;
58};
59
60/*
61 * SVGA commands that are used by this code. Please see the device headers
62 * for explanation.
63 */
64struct vmw_kms_sou_readback_blit {
65 uint32 header;
66 SVGAFifoCmdBlitScreenToGMRFB body;
67};
68
69struct vmw_kms_sou_dmabuf_blit {
70 uint32 header;
71 SVGAFifoCmdBlitGMRFBToScreen body;
72};
73
74struct vmw_kms_sou_dirty_cmd {
75 SVGA3dCmdHeader header;
76 SVGA3dCmdBlitSurfaceToScreen body;
77};
78
79/**
80 * Display unit using screen objects.
81 */
82struct vmw_screen_object_unit {
83 struct vmw_display_unit base;
84
85 unsigned long buffer_size; /**< Size of allocated buffer */
86 struct vmw_dma_buffer *buffer; /**< Backing store buffer */
87
88 bool defined;
89};
90
91static void vmw_sou_destroy(struct vmw_screen_object_unit *sou)
92{
93 vmw_du_cleanup(&sou->base);
94 kfree(sou);
95}
96
97
98/*
99 * Screen Object Display Unit CRTC functions
100 */
101
102static void vmw_sou_crtc_destroy(struct drm_crtc *crtc)
103{
104 vmw_sou_destroy(vmw_crtc_to_sou(crtc));
105}
106
107/**
108 * Send the fifo command to create a screen.
109 */
110static int vmw_sou_fifo_create(struct vmw_private *dev_priv,
111 struct vmw_screen_object_unit *sou,
112 uint32_t x, uint32_t y,
113 struct drm_display_mode *mode)
114{
115 size_t fifo_size;
116
117 struct {
118 struct {
119 uint32_t cmdType;
120 } header;
121 SVGAScreenObject obj;
122 } *cmd;
123
124 BUG_ON(!sou->buffer);
125
126 fifo_size = sizeof(*cmd);
127 cmd = vmw_fifo_reserve(dev_priv, fifo_size);
128 /* The hardware has hung, nothing we can do about it here. */
129 if (unlikely(cmd == NULL)) {
130 DRM_ERROR("Fifo reserve failed.\n");
131 return -ENOMEM;
132 }
133
134 memset(cmd, 0, fifo_size);
135 cmd->header.cmdType = SVGA_CMD_DEFINE_SCREEN;
136 cmd->obj.structSize = sizeof(SVGAScreenObject);
137 cmd->obj.id = sou->base.unit;
138 cmd->obj.flags = SVGA_SCREEN_HAS_ROOT |
139 (sou->base.unit == 0 ? SVGA_SCREEN_IS_PRIMARY : 0);
140 cmd->obj.size.width = mode->hdisplay;
141 cmd->obj.size.height = mode->vdisplay;
142 if (sou->base.is_implicit) {
143 cmd->obj.root.x = x;
144 cmd->obj.root.y = y;
145 } else {
146 cmd->obj.root.x = sou->base.gui_x;
147 cmd->obj.root.y = sou->base.gui_y;
148 }
149 sou->base.set_gui_x = cmd->obj.root.x;
150 sou->base.set_gui_y = cmd->obj.root.y;
151
152 /* Ok to assume that buffer is pinned in vram */
153 vmw_bo_get_guest_ptr(&sou->buffer->base, &cmd->obj.backingStore.ptr);
154 cmd->obj.backingStore.pitch = mode->hdisplay * 4;
155
156 vmw_fifo_commit(dev_priv, fifo_size);
157
158 sou->defined = true;
159
160 return 0;
161}
162
163/**
164 * Send the fifo command to destroy a screen.
165 */
166static int vmw_sou_fifo_destroy(struct vmw_private *dev_priv,
167 struct vmw_screen_object_unit *sou)
168{
169 size_t fifo_size;
170 int ret;
171
172 struct {
173 struct {
174 uint32_t cmdType;
175 } header;
176 SVGAFifoCmdDestroyScreen body;
177 } *cmd;
178
179 /* no need to do anything */
180 if (unlikely(!sou->defined))
181 return 0;
182
183 fifo_size = sizeof(*cmd);
184 cmd = vmw_fifo_reserve(dev_priv, fifo_size);
185 /* the hardware has hung, nothing we can do about it here */
186 if (unlikely(cmd == NULL)) {
187 DRM_ERROR("Fifo reserve failed.\n");
188 return -ENOMEM;
189 }
190
191 memset(cmd, 0, fifo_size);
192 cmd->header.cmdType = SVGA_CMD_DESTROY_SCREEN;
193 cmd->body.screenId = sou->base.unit;
194
195 vmw_fifo_commit(dev_priv, fifo_size);
196
197 /* Force sync */
198 ret = vmw_fallback_wait(dev_priv, false, true, 0, false, 3*HZ);
199 if (unlikely(ret != 0))
200 DRM_ERROR("Failed to sync with HW");
201 else
202 sou->defined = false;
203
204 return ret;
205}
206
207/**
208 * vmw_sou_crtc_mode_set_nofb - Create new screen
209 *
210 * @crtc: CRTC associated with the new screen
211 *
212 * This function creates/destroys a screen. This function cannot fail, so if
213 * somehow we run into a failure, just do the best we can to get out.
214 */
215static void vmw_sou_crtc_mode_set_nofb(struct drm_crtc *crtc)
216{
217 struct vmw_private *dev_priv;
218 struct vmw_screen_object_unit *sou;
219 struct vmw_framebuffer *vfb;
220 struct drm_framebuffer *fb;
221 struct drm_plane_state *ps;
222 struct vmw_plane_state *vps;
223 int ret;
224
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 sou->buffer = vps->dmabuf;
244 sou->buffer_size = vps->dmabuf_size;
245
246 ret = vmw_sou_fifo_create(dev_priv, sou, crtc->x, crtc->y,
247 &crtc->mode);
248 if (ret)
249 DRM_ERROR("Failed to define Screen Object %dx%d\n",
250 crtc->x, crtc->y);
251
252 vmw_kms_add_active(dev_priv, &sou->base, vfb);
253 } else {
254 sou->buffer = NULL;
255 sou->buffer_size = 0;
256
257 vmw_kms_del_active(dev_priv, &sou->base);
258 }
259}
260
261/**
262 * vmw_sou_crtc_helper_prepare - Noop
263 *
264 * @crtc: CRTC associated with the new screen
265 *
266 * Prepares the CRTC for a mode set, but we don't need to do anything here.
267 */
268static void vmw_sou_crtc_helper_prepare(struct drm_crtc *crtc)
269{
270}
271
272/**
273 * vmw_sou_crtc_atomic_enable - Noop
274 *
275 * @crtc: CRTC associated with the new screen
276 *
277 * This is called after a mode set has been completed.
278 */
279static void vmw_sou_crtc_atomic_enable(struct drm_crtc *crtc,
280 struct drm_crtc_state *old_state)
281{
282}
283
284/**
285 * vmw_sou_crtc_atomic_disable - Turns off CRTC
286 *
287 * @crtc: CRTC to be turned off
288 */
289static void vmw_sou_crtc_atomic_disable(struct drm_crtc *crtc,
290 struct drm_crtc_state *old_state)
291{
292 struct vmw_private *dev_priv;
293 struct vmw_screen_object_unit *sou;
294 int ret;
295
296
297 if (!crtc) {
298 DRM_ERROR("CRTC is NULL\n");
299 return;
300 }
301
302 sou = vmw_crtc_to_sou(crtc);
303 dev_priv = vmw_priv(crtc->dev);
304
305 if (sou->defined) {
306 ret = vmw_sou_fifo_destroy(dev_priv, sou);
307 if (ret)
308 DRM_ERROR("Failed to destroy Screen Object\n");
309 }
310}
311
312static int vmw_sou_crtc_page_flip(struct drm_crtc *crtc,
313 struct drm_framebuffer *new_fb,
314 struct drm_pending_vblank_event *event,
315 uint32_t flags,
316 struct drm_modeset_acquire_ctx *ctx)
317{
318 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
319 int ret;
320
321 if (!vmw_kms_crtc_flippable(dev_priv, crtc))
322 return -EINVAL;
323
324 ret = drm_atomic_helper_page_flip(crtc, new_fb, event, flags, ctx);
325 if (ret) {
326 DRM_ERROR("Page flip error %d.\n", ret);
327 return ret;
328 }
329
330 if (vmw_crtc_to_du(crtc)->is_implicit)
331 vmw_kms_update_implicit_fb(dev_priv, crtc);
332
333 return ret;
334}
335
336static const struct drm_crtc_funcs vmw_screen_object_crtc_funcs = {
337 .gamma_set = vmw_du_crtc_gamma_set,
338 .destroy = vmw_sou_crtc_destroy,
339 .reset = vmw_du_crtc_reset,
340 .atomic_duplicate_state = vmw_du_crtc_duplicate_state,
341 .atomic_destroy_state = vmw_du_crtc_destroy_state,
342 .set_config = vmw_kms_set_config,
343 .page_flip = vmw_sou_crtc_page_flip,
344};
345
346/*
347 * Screen Object Display Unit encoder functions
348 */
349
350static void vmw_sou_encoder_destroy(struct drm_encoder *encoder)
351{
352 vmw_sou_destroy(vmw_encoder_to_sou(encoder));
353}
354
355static const struct drm_encoder_funcs vmw_screen_object_encoder_funcs = {
356 .destroy = vmw_sou_encoder_destroy,
357};
358
359/*
360 * Screen Object Display Unit connector functions
361 */
362
363static void vmw_sou_connector_destroy(struct drm_connector *connector)
364{
365 vmw_sou_destroy(vmw_connector_to_sou(connector));
366}
367
368static const struct drm_connector_funcs vmw_sou_connector_funcs = {
369 .dpms = vmw_du_connector_dpms,
370 .detect = vmw_du_connector_detect,
371 .fill_modes = vmw_du_connector_fill_modes,
372 .set_property = vmw_du_connector_set_property,
373 .destroy = vmw_sou_connector_destroy,
374 .reset = vmw_du_connector_reset,
375 .atomic_duplicate_state = vmw_du_connector_duplicate_state,
376 .atomic_destroy_state = vmw_du_connector_destroy_state,
377 .atomic_set_property = vmw_du_connector_atomic_set_property,
378 .atomic_get_property = vmw_du_connector_atomic_get_property,
379};
380
381
382static const struct
383drm_connector_helper_funcs vmw_sou_connector_helper_funcs = {
384 .best_encoder = drm_atomic_helper_best_encoder,
385};
386
387
388
389/*
390 * Screen Object Display Plane Functions
391 */
392
393/**
394 * vmw_sou_primary_plane_cleanup_fb - Frees sou backing buffer
395 *
396 * @plane: display plane
397 * @old_state: Contains the FB to clean up
398 *
399 * Unpins the display surface
400 *
401 * Returns 0 on success
402 */
403static void
404vmw_sou_primary_plane_cleanup_fb(struct drm_plane *plane,
405 struct drm_plane_state *old_state)
406{
407 struct vmw_plane_state *vps = vmw_plane_state_to_vps(old_state);
408 struct drm_crtc *crtc = plane->state->crtc ?
409 plane->state->crtc : old_state->crtc;
410
411 if (vps->dmabuf)
412 vmw_dmabuf_unpin(vmw_priv(crtc->dev), vps->dmabuf, false);
413 vmw_dmabuf_unreference(&vps->dmabuf);
414 vps->dmabuf_size = 0;
415
416 vmw_du_plane_cleanup_fb(plane, old_state);
417}
418
419
420/**
421 * vmw_sou_primary_plane_prepare_fb - allocate backing buffer
422 *
423 * @plane: display plane
424 * @new_state: info on the new plane state, including the FB
425 *
426 * The SOU backing buffer is our equivalent of the display plane.
427 *
428 * Returns 0 on success
429 */
430static int
431vmw_sou_primary_plane_prepare_fb(struct drm_plane *plane,
432 struct drm_plane_state *new_state)
433{
434 struct drm_framebuffer *new_fb = new_state->fb;
435 struct drm_crtc *crtc = plane->state->crtc ?: new_state->crtc;
436 struct vmw_plane_state *vps = vmw_plane_state_to_vps(new_state);
437 struct vmw_private *dev_priv;
438 size_t size;
439 int ret;
440
441
442 if (!new_fb) {
443 vmw_dmabuf_unreference(&vps->dmabuf);
444 vps->dmabuf_size = 0;
445
446 return 0;
447 }
448
449 size = new_state->crtc_w * new_state->crtc_h * 4;
450 dev_priv = vmw_priv(crtc->dev);
451
452 if (vps->dmabuf) {
453 if (vps->dmabuf_size == size) {
454 /*
455 * Note that this might temporarily up the pin-count
456 * to 2, until cleanup_fb() is called.
457 */
458 return vmw_dmabuf_pin_in_vram(dev_priv, vps->dmabuf,
459 true);
460 }
461
462 vmw_dmabuf_unreference(&vps->dmabuf);
463 vps->dmabuf_size = 0;
464 }
465
466 vps->dmabuf = kzalloc(sizeof(*vps->dmabuf), GFP_KERNEL);
467 if (!vps->dmabuf)
468 return -ENOMEM;
469
470 vmw_svga_enable(dev_priv);
471
472 /* After we have alloced the backing store might not be able to
473 * resume the overlays, this is preferred to failing to alloc.
474 */
475 vmw_overlay_pause_all(dev_priv);
476 ret = vmw_dmabuf_init(dev_priv, vps->dmabuf, size,
477 &vmw_vram_ne_placement,
478 false, &vmw_dmabuf_bo_free);
479 vmw_overlay_resume_all(dev_priv);
480 if (ret) {
481 vps->dmabuf = NULL; /* vmw_dmabuf_init frees on error */
482 return ret;
483 }
484
485 vps->dmabuf_size = size;
486
487 /*
488 * TTM already thinks the buffer is pinned, but make sure the
489 * pin_count is upped.
490 */
491 return vmw_dmabuf_pin_in_vram(dev_priv, vps->dmabuf, true);
492}
493
494
495static void
496vmw_sou_primary_plane_atomic_update(struct drm_plane *plane,
497 struct drm_plane_state *old_state)
498{
499 struct drm_crtc *crtc = plane->state->crtc;
500 struct drm_pending_vblank_event *event = NULL;
501 struct vmw_fence_obj *fence = NULL;
502 int ret;
503
504 if (crtc && plane->state->fb) {
505 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
506 struct vmw_framebuffer *vfb =
507 vmw_framebuffer_to_vfb(plane->state->fb);
508 struct drm_vmw_rect vclips;
509
510 vclips.x = crtc->x;
511 vclips.y = crtc->y;
512 vclips.w = crtc->mode.hdisplay;
513 vclips.h = crtc->mode.vdisplay;
514
515 if (vfb->dmabuf)
516 ret = vmw_kms_sou_do_dmabuf_dirty(dev_priv, vfb, NULL,
517 &vclips, 1, 1, true,
518 &fence, crtc);
519 else
520 ret = vmw_kms_sou_do_surface_dirty(dev_priv, vfb, NULL,
521 &vclips, NULL, 0, 0,
522 1, 1, &fence, crtc);
523
524 /*
525 * We cannot really fail this function, so if we do, then output
526 * an error and maintain consistent atomic state.
527 */
528 if (ret != 0)
529 DRM_ERROR("Failed to update screen.\n");
530
531 crtc->primary->fb = plane->state->fb;
532 } else {
533 /*
534 * When disabling a plane, CRTC and FB should always be NULL
535 * together, otherwise it's an error.
536 * Here primary plane is being disable so should really blank
537 * the screen object display unit, if not already done.
538 */
539 return;
540 }
541
542 event = crtc->state->event;
543 /*
544 * In case of failure and other cases, vblank event will be sent in
545 * vmw_du_crtc_atomic_flush.
546 */
547 if (event && fence) {
548 struct drm_file *file_priv = event->base.file_priv;
549
550 ret = vmw_event_fence_action_queue(file_priv,
551 fence,
552 &event->base,
553 &event->event.vbl.tv_sec,
554 &event->event.vbl.tv_usec,
555 true);
556
557 if (unlikely(ret != 0))
558 DRM_ERROR("Failed to queue event on fence.\n");
559 else
560 crtc->state->event = NULL;
561 }
562
563 if (fence)
564 vmw_fence_obj_unreference(&fence);
565}
566
567
568static const struct drm_plane_funcs vmw_sou_plane_funcs = {
569 .update_plane = drm_atomic_helper_update_plane,
570 .disable_plane = drm_atomic_helper_disable_plane,
571 .destroy = vmw_du_primary_plane_destroy,
572 .reset = vmw_du_plane_reset,
573 .atomic_duplicate_state = vmw_du_plane_duplicate_state,
574 .atomic_destroy_state = vmw_du_plane_destroy_state,
575};
576
577static const struct drm_plane_funcs vmw_sou_cursor_funcs = {
578 .update_plane = drm_atomic_helper_update_plane,
579 .disable_plane = drm_atomic_helper_disable_plane,
580 .destroy = vmw_du_cursor_plane_destroy,
581 .reset = vmw_du_plane_reset,
582 .atomic_duplicate_state = vmw_du_plane_duplicate_state,
583 .atomic_destroy_state = vmw_du_plane_destroy_state,
584};
585
586/*
587 * Atomic Helpers
588 */
589static const struct
590drm_plane_helper_funcs vmw_sou_cursor_plane_helper_funcs = {
591 .atomic_check = vmw_du_cursor_plane_atomic_check,
592 .atomic_update = vmw_du_cursor_plane_atomic_update,
593 .prepare_fb = vmw_du_cursor_plane_prepare_fb,
594 .cleanup_fb = vmw_du_plane_cleanup_fb,
595};
596
597static const struct
598drm_plane_helper_funcs vmw_sou_primary_plane_helper_funcs = {
599 .atomic_check = vmw_du_primary_plane_atomic_check,
600 .atomic_update = vmw_sou_primary_plane_atomic_update,
601 .prepare_fb = vmw_sou_primary_plane_prepare_fb,
602 .cleanup_fb = vmw_sou_primary_plane_cleanup_fb,
603};
604
605static const struct drm_crtc_helper_funcs vmw_sou_crtc_helper_funcs = {
606 .prepare = vmw_sou_crtc_helper_prepare,
607 .mode_set_nofb = vmw_sou_crtc_mode_set_nofb,
608 .atomic_check = vmw_du_crtc_atomic_check,
609 .atomic_begin = vmw_du_crtc_atomic_begin,
610 .atomic_flush = vmw_du_crtc_atomic_flush,
611 .atomic_enable = vmw_sou_crtc_atomic_enable,
612 .atomic_disable = vmw_sou_crtc_atomic_disable,
613};
614
615
616static int vmw_sou_init(struct vmw_private *dev_priv, unsigned unit)
617{
618 struct vmw_screen_object_unit *sou;
619 struct drm_device *dev = dev_priv->dev;
620 struct drm_connector *connector;
621 struct drm_encoder *encoder;
622 struct drm_plane *primary, *cursor;
623 struct drm_crtc *crtc;
624 int ret;
625
626 sou = kzalloc(sizeof(*sou), GFP_KERNEL);
627 if (!sou)
628 return -ENOMEM;
629
630 sou->base.unit = unit;
631 crtc = &sou->base.crtc;
632 encoder = &sou->base.encoder;
633 connector = &sou->base.connector;
634 primary = &sou->base.primary;
635 cursor = &sou->base.cursor;
636
637 sou->base.active_implicit = false;
638 sou->base.pref_active = (unit == 0);
639 sou->base.pref_width = dev_priv->initial_width;
640 sou->base.pref_height = dev_priv->initial_height;
641 sou->base.pref_mode = NULL;
642
643 /*
644 * Remove this after enabling atomic because property values can
645 * only exist in a state object
646 */
647 sou->base.is_implicit = false;
648
649 /* Initialize primary plane */
650 vmw_du_plane_reset(primary);
651
652 ret = drm_universal_plane_init(dev, &sou->base.primary,
653 0, &vmw_sou_plane_funcs,
654 vmw_primary_plane_formats,
655 ARRAY_SIZE(vmw_primary_plane_formats),
656 NULL, DRM_PLANE_TYPE_PRIMARY, NULL);
657 if (ret) {
658 DRM_ERROR("Failed to initialize primary plane");
659 goto err_free;
660 }
661
662 drm_plane_helper_add(primary, &vmw_sou_primary_plane_helper_funcs);
663
664 /* Initialize cursor plane */
665 vmw_du_plane_reset(cursor);
666
667 ret = drm_universal_plane_init(dev, &sou->base.cursor,
668 0, &vmw_sou_cursor_funcs,
669 vmw_cursor_plane_formats,
670 ARRAY_SIZE(vmw_cursor_plane_formats),
671 NULL, DRM_PLANE_TYPE_CURSOR, NULL);
672 if (ret) {
673 DRM_ERROR("Failed to initialize cursor plane");
674 drm_plane_cleanup(&sou->base.primary);
675 goto err_free;
676 }
677
678 drm_plane_helper_add(cursor, &vmw_sou_cursor_plane_helper_funcs);
679
680 vmw_du_connector_reset(connector);
681 ret = drm_connector_init(dev, connector, &vmw_sou_connector_funcs,
682 DRM_MODE_CONNECTOR_VIRTUAL);
683 if (ret) {
684 DRM_ERROR("Failed to initialize connector\n");
685 goto err_free;
686 }
687
688 drm_connector_helper_add(connector, &vmw_sou_connector_helper_funcs);
689 connector->status = vmw_du_connector_detect(connector, true);
690 vmw_connector_state_to_vcs(connector->state)->is_implicit = false;
691
692
693 ret = drm_encoder_init(dev, encoder, &vmw_screen_object_encoder_funcs,
694 DRM_MODE_ENCODER_VIRTUAL, NULL);
695 if (ret) {
696 DRM_ERROR("Failed to initialize encoder\n");
697 goto err_free_connector;
698 }
699
700 (void) drm_mode_connector_attach_encoder(connector, encoder);
701 encoder->possible_crtcs = (1 << unit);
702 encoder->possible_clones = 0;
703
704 ret = drm_connector_register(connector);
705 if (ret) {
706 DRM_ERROR("Failed to register connector\n");
707 goto err_free_encoder;
708 }
709
710
711 vmw_du_crtc_reset(crtc);
712 ret = drm_crtc_init_with_planes(dev, crtc, &sou->base.primary,
713 &sou->base.cursor,
714 &vmw_screen_object_crtc_funcs, NULL);
715 if (ret) {
716 DRM_ERROR("Failed to initialize CRTC\n");
717 goto err_free_unregister;
718 }
719
720 drm_crtc_helper_add(crtc, &vmw_sou_crtc_helper_funcs);
721
722 drm_mode_crtc_set_gamma_size(crtc, 256);
723
724 drm_object_attach_property(&connector->base,
725 dev_priv->hotplug_mode_update_property, 1);
726 drm_object_attach_property(&connector->base,
727 dev->mode_config.suggested_x_property, 0);
728 drm_object_attach_property(&connector->base,
729 dev->mode_config.suggested_y_property, 0);
730 if (dev_priv->implicit_placement_property)
731 drm_object_attach_property
732 (&connector->base,
733 dev_priv->implicit_placement_property,
734 sou->base.is_implicit);
735
736 return 0;
737
738err_free_unregister:
739 drm_connector_unregister(connector);
740err_free_encoder:
741 drm_encoder_cleanup(encoder);
742err_free_connector:
743 drm_connector_cleanup(connector);
744err_free:
745 kfree(sou);
746 return ret;
747}
748
749int vmw_kms_sou_init_display(struct vmw_private *dev_priv)
750{
751 struct drm_device *dev = dev_priv->dev;
752 int i, ret;
753
754 if (!(dev_priv->capabilities & SVGA_CAP_SCREEN_OBJECT_2)) {
755 DRM_INFO("Not using screen objects,"
756 " missing cap SCREEN_OBJECT_2\n");
757 return -ENOSYS;
758 }
759
760 ret = -ENOMEM;
761 dev_priv->num_implicit = 0;
762 dev_priv->implicit_fb = NULL;
763
764 ret = drm_vblank_init(dev, VMWGFX_NUM_DISPLAY_UNITS);
765 if (unlikely(ret != 0))
766 return ret;
767
768 vmw_kms_create_implicit_placement_property(dev_priv, false);
769
770 for (i = 0; i < VMWGFX_NUM_DISPLAY_UNITS; ++i)
771 vmw_sou_init(dev_priv, i);
772
773 dev_priv->active_display_unit = vmw_du_screen_object;
774
775 DRM_INFO("Screen Objects Display Unit initialized\n");
776
777 return 0;
778}
779
780static int do_dmabuf_define_gmrfb(struct vmw_private *dev_priv,
781 struct vmw_framebuffer *framebuffer)
782{
783 struct vmw_dma_buffer *buf =
784 container_of(framebuffer, struct vmw_framebuffer_dmabuf,
785 base)->buffer;
786 int depth = framebuffer->base.format->depth;
787 struct {
788 uint32_t header;
789 SVGAFifoCmdDefineGMRFB body;
790 } *cmd;
791
792 /* Emulate RGBA support, contrary to svga_reg.h this is not
793 * supported by hosts. This is only a problem if we are reading
794 * this value later and expecting what we uploaded back.
795 */
796 if (depth == 32)
797 depth = 24;
798
799 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
800 if (!cmd) {
801 DRM_ERROR("Out of fifo space for dirty framebuffer command.\n");
802 return -ENOMEM;
803 }
804
805 cmd->header = SVGA_CMD_DEFINE_GMRFB;
806 cmd->body.format.bitsPerPixel = framebuffer->base.format->cpp[0] * 8;
807 cmd->body.format.colorDepth = depth;
808 cmd->body.format.reserved = 0;
809 cmd->body.bytesPerLine = framebuffer->base.pitches[0];
810 /* Buffer is reserved in vram or GMR */
811 vmw_bo_get_guest_ptr(&buf->base, &cmd->body.ptr);
812 vmw_fifo_commit(dev_priv, sizeof(*cmd));
813
814 return 0;
815}
816
817/**
818 * vmw_sou_surface_fifo_commit - Callback to fill in and submit a
819 * blit surface to screen command.
820 *
821 * @dirty: The closure structure.
822 *
823 * Fills in the missing fields in the command, and translates the cliprects
824 * to match the destination bounding box encoded.
825 */
826static void vmw_sou_surface_fifo_commit(struct vmw_kms_dirty *dirty)
827{
828 struct vmw_kms_sou_surface_dirty *sdirty =
829 container_of(dirty, typeof(*sdirty), base);
830 struct vmw_kms_sou_dirty_cmd *cmd = dirty->cmd;
831 s32 trans_x = dirty->unit->crtc.x - sdirty->dst_x;
832 s32 trans_y = dirty->unit->crtc.y - sdirty->dst_y;
833 size_t region_size = dirty->num_hits * sizeof(SVGASignedRect);
834 SVGASignedRect *blit = (SVGASignedRect *) &cmd[1];
835 int i;
836
837 if (!dirty->num_hits) {
838 vmw_fifo_commit(dirty->dev_priv, 0);
839 return;
840 }
841
842 cmd->header.id = SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN;
843 cmd->header.size = sizeof(cmd->body) + region_size;
844
845 /*
846 * Use the destination bounding box to specify destination - and
847 * source bounding regions.
848 */
849 cmd->body.destRect.left = sdirty->left;
850 cmd->body.destRect.right = sdirty->right;
851 cmd->body.destRect.top = sdirty->top;
852 cmd->body.destRect.bottom = sdirty->bottom;
853
854 cmd->body.srcRect.left = sdirty->left + trans_x;
855 cmd->body.srcRect.right = sdirty->right + trans_x;
856 cmd->body.srcRect.top = sdirty->top + trans_y;
857 cmd->body.srcRect.bottom = sdirty->bottom + trans_y;
858
859 cmd->body.srcImage.sid = sdirty->sid;
860 cmd->body.destScreenId = dirty->unit->unit;
861
862 /* Blits are relative to the destination rect. Translate. */
863 for (i = 0; i < dirty->num_hits; ++i, ++blit) {
864 blit->left -= sdirty->left;
865 blit->right -= sdirty->left;
866 blit->top -= sdirty->top;
867 blit->bottom -= sdirty->top;
868 }
869
870 vmw_fifo_commit(dirty->dev_priv, region_size + sizeof(*cmd));
871
872 sdirty->left = sdirty->top = S32_MAX;
873 sdirty->right = sdirty->bottom = S32_MIN;
874}
875
876/**
877 * vmw_sou_surface_clip - Callback to encode a blit surface to screen cliprect.
878 *
879 * @dirty: The closure structure
880 *
881 * Encodes a SVGASignedRect cliprect and updates the bounding box of the
882 * BLIT_SURFACE_TO_SCREEN command.
883 */
884static void vmw_sou_surface_clip(struct vmw_kms_dirty *dirty)
885{
886 struct vmw_kms_sou_surface_dirty *sdirty =
887 container_of(dirty, typeof(*sdirty), base);
888 struct vmw_kms_sou_dirty_cmd *cmd = dirty->cmd;
889 SVGASignedRect *blit = (SVGASignedRect *) &cmd[1];
890
891 /* Destination rect. */
892 blit += dirty->num_hits;
893 blit->left = dirty->unit_x1;
894 blit->top = dirty->unit_y1;
895 blit->right = dirty->unit_x2;
896 blit->bottom = dirty->unit_y2;
897
898 /* Destination bounding box */
899 sdirty->left = min_t(s32, sdirty->left, dirty->unit_x1);
900 sdirty->top = min_t(s32, sdirty->top, dirty->unit_y1);
901 sdirty->right = max_t(s32, sdirty->right, dirty->unit_x2);
902 sdirty->bottom = max_t(s32, sdirty->bottom, dirty->unit_y2);
903
904 dirty->num_hits++;
905}
906
907/**
908 * vmw_kms_sou_do_surface_dirty - Dirty part of a surface backed framebuffer
909 *
910 * @dev_priv: Pointer to the device private structure.
911 * @framebuffer: Pointer to the surface-buffer backed framebuffer.
912 * @clips: Array of clip rects. Either @clips or @vclips must be NULL.
913 * @vclips: Alternate array of clip rects. Either @clips or @vclips must
914 * be NULL.
915 * @srf: Pointer to surface to blit from. If NULL, the surface attached
916 * to @framebuffer will be used.
917 * @dest_x: X coordinate offset to align @srf with framebuffer coordinates.
918 * @dest_y: Y coordinate offset to align @srf with framebuffer coordinates.
919 * @num_clips: Number of clip rects in @clips.
920 * @inc: Increment to use when looping over @clips.
921 * @out_fence: If non-NULL, will return a ref-counted pointer to a
922 * struct vmw_fence_obj. The returned fence pointer may be NULL in which
923 * case the device has already synchronized.
924 * @crtc: If crtc is passed, perform surface dirty on that crtc only.
925 *
926 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
927 * interrupted.
928 */
929int vmw_kms_sou_do_surface_dirty(struct vmw_private *dev_priv,
930 struct vmw_framebuffer *framebuffer,
931 struct drm_clip_rect *clips,
932 struct drm_vmw_rect *vclips,
933 struct vmw_resource *srf,
934 s32 dest_x,
935 s32 dest_y,
936 unsigned num_clips, int inc,
937 struct vmw_fence_obj **out_fence,
938 struct drm_crtc *crtc)
939{
940 struct vmw_framebuffer_surface *vfbs =
941 container_of(framebuffer, typeof(*vfbs), base);
942 struct vmw_kms_sou_surface_dirty sdirty;
943 struct vmw_validation_ctx ctx;
944 int ret;
945
946 if (!srf)
947 srf = &vfbs->surface->res;
948
949 ret = vmw_kms_helper_resource_prepare(srf, true, &ctx);
950 if (ret)
951 return ret;
952
953 sdirty.base.fifo_commit = vmw_sou_surface_fifo_commit;
954 sdirty.base.clip = vmw_sou_surface_clip;
955 sdirty.base.dev_priv = dev_priv;
956 sdirty.base.fifo_reserve_size = sizeof(struct vmw_kms_sou_dirty_cmd) +
957 sizeof(SVGASignedRect) * num_clips;
958 sdirty.base.crtc = crtc;
959
960 sdirty.sid = srf->id;
961 sdirty.left = sdirty.top = S32_MAX;
962 sdirty.right = sdirty.bottom = S32_MIN;
963 sdirty.dst_x = dest_x;
964 sdirty.dst_y = dest_y;
965
966 ret = vmw_kms_helper_dirty(dev_priv, framebuffer, clips, vclips,
967 dest_x, dest_y, num_clips, inc,
968 &sdirty.base);
969 vmw_kms_helper_resource_finish(&ctx, out_fence);
970
971 return ret;
972}
973
974/**
975 * vmw_sou_dmabuf_fifo_commit - Callback to submit a set of readback clips.
976 *
977 * @dirty: The closure structure.
978 *
979 * Commits a previously built command buffer of readback clips.
980 */
981static void vmw_sou_dmabuf_fifo_commit(struct vmw_kms_dirty *dirty)
982{
983 if (!dirty->num_hits) {
984 vmw_fifo_commit(dirty->dev_priv, 0);
985 return;
986 }
987
988 vmw_fifo_commit(dirty->dev_priv,
989 sizeof(struct vmw_kms_sou_dmabuf_blit) *
990 dirty->num_hits);
991}
992
993/**
994 * vmw_sou_dmabuf_clip - Callback to encode a readback cliprect.
995 *
996 * @dirty: The closure structure
997 *
998 * Encodes a BLIT_GMRFB_TO_SCREEN cliprect.
999 */
1000static void vmw_sou_dmabuf_clip(struct vmw_kms_dirty *dirty)
1001{
1002 struct vmw_kms_sou_dmabuf_blit *blit = dirty->cmd;
1003
1004 blit += dirty->num_hits;
1005 blit->header = SVGA_CMD_BLIT_GMRFB_TO_SCREEN;
1006 blit->body.destScreenId = dirty->unit->unit;
1007 blit->body.srcOrigin.x = dirty->fb_x;
1008 blit->body.srcOrigin.y = dirty->fb_y;
1009 blit->body.destRect.left = dirty->unit_x1;
1010 blit->body.destRect.top = dirty->unit_y1;
1011 blit->body.destRect.right = dirty->unit_x2;
1012 blit->body.destRect.bottom = dirty->unit_y2;
1013 dirty->num_hits++;
1014}
1015
1016/**
1017 * vmw_kms_do_dmabuf_dirty - Dirty part of a dma-buffer backed framebuffer
1018 *
1019 * @dev_priv: Pointer to the device private structure.
1020 * @framebuffer: Pointer to the dma-buffer backed framebuffer.
1021 * @clips: Array of clip rects.
1022 * @vclips: Alternate array of clip rects. Either @clips or @vclips must
1023 * be NULL.
1024 * @num_clips: Number of clip rects in @clips.
1025 * @increment: Increment to use when looping over @clips.
1026 * @interruptible: Whether to perform waits interruptible if possible.
1027 * @out_fence: If non-NULL, will return a ref-counted pointer to a
1028 * struct vmw_fence_obj. The returned fence pointer may be NULL in which
1029 * case the device has already synchronized.
1030 * @crtc: If crtc is passed, perform dmabuf dirty on that crtc only.
1031 *
1032 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
1033 * interrupted.
1034 */
1035int vmw_kms_sou_do_dmabuf_dirty(struct vmw_private *dev_priv,
1036 struct vmw_framebuffer *framebuffer,
1037 struct drm_clip_rect *clips,
1038 struct drm_vmw_rect *vclips,
1039 unsigned num_clips, int increment,
1040 bool interruptible,
1041 struct vmw_fence_obj **out_fence,
1042 struct drm_crtc *crtc)
1043{
1044 struct vmw_dma_buffer *buf =
1045 container_of(framebuffer, struct vmw_framebuffer_dmabuf,
1046 base)->buffer;
1047 struct vmw_kms_dirty dirty;
1048 int ret;
1049
1050 ret = vmw_kms_helper_buffer_prepare(dev_priv, buf, interruptible,
1051 false, false);
1052 if (ret)
1053 return ret;
1054
1055 ret = do_dmabuf_define_gmrfb(dev_priv, framebuffer);
1056 if (unlikely(ret != 0))
1057 goto out_revert;
1058
1059 dirty.crtc = crtc;
1060 dirty.fifo_commit = vmw_sou_dmabuf_fifo_commit;
1061 dirty.clip = vmw_sou_dmabuf_clip;
1062 dirty.fifo_reserve_size = sizeof(struct vmw_kms_sou_dmabuf_blit) *
1063 num_clips;
1064 ret = vmw_kms_helper_dirty(dev_priv, framebuffer, clips, vclips,
1065 0, 0, num_clips, increment, &dirty);
1066 vmw_kms_helper_buffer_finish(dev_priv, NULL, buf, out_fence, NULL);
1067
1068 return ret;
1069
1070out_revert:
1071 vmw_kms_helper_buffer_revert(buf);
1072
1073 return ret;
1074}
1075
1076
1077/**
1078 * vmw_sou_readback_fifo_commit - Callback to submit a set of readback clips.
1079 *
1080 * @dirty: The closure structure.
1081 *
1082 * Commits a previously built command buffer of readback clips.
1083 */
1084static void vmw_sou_readback_fifo_commit(struct vmw_kms_dirty *dirty)
1085{
1086 if (!dirty->num_hits) {
1087 vmw_fifo_commit(dirty->dev_priv, 0);
1088 return;
1089 }
1090
1091 vmw_fifo_commit(dirty->dev_priv,
1092 sizeof(struct vmw_kms_sou_readback_blit) *
1093 dirty->num_hits);
1094}
1095
1096/**
1097 * vmw_sou_readback_clip - Callback to encode a readback cliprect.
1098 *
1099 * @dirty: The closure structure
1100 *
1101 * Encodes a BLIT_SCREEN_TO_GMRFB cliprect.
1102 */
1103static void vmw_sou_readback_clip(struct vmw_kms_dirty *dirty)
1104{
1105 struct vmw_kms_sou_readback_blit *blit = dirty->cmd;
1106
1107 blit += dirty->num_hits;
1108 blit->header = SVGA_CMD_BLIT_SCREEN_TO_GMRFB;
1109 blit->body.srcScreenId = dirty->unit->unit;
1110 blit->body.destOrigin.x = dirty->fb_x;
1111 blit->body.destOrigin.y = dirty->fb_y;
1112 blit->body.srcRect.left = dirty->unit_x1;
1113 blit->body.srcRect.top = dirty->unit_y1;
1114 blit->body.srcRect.right = dirty->unit_x2;
1115 blit->body.srcRect.bottom = dirty->unit_y2;
1116 dirty->num_hits++;
1117}
1118
1119/**
1120 * vmw_kms_sou_readback - Perform a readback from the screen object system to
1121 * a dma-buffer backed framebuffer.
1122 *
1123 * @dev_priv: Pointer to the device private structure.
1124 * @file_priv: Pointer to a struct drm_file identifying the caller.
1125 * Must be set to NULL if @user_fence_rep is NULL.
1126 * @vfb: Pointer to the dma-buffer backed framebuffer.
1127 * @user_fence_rep: User-space provided structure for fence information.
1128 * Must be set to non-NULL if @file_priv is non-NULL.
1129 * @vclips: Array of clip rects.
1130 * @num_clips: Number of clip rects in @vclips.
1131 * @crtc: If crtc is passed, readback on that crtc only.
1132 *
1133 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
1134 * interrupted.
1135 */
1136int vmw_kms_sou_readback(struct vmw_private *dev_priv,
1137 struct drm_file *file_priv,
1138 struct vmw_framebuffer *vfb,
1139 struct drm_vmw_fence_rep __user *user_fence_rep,
1140 struct drm_vmw_rect *vclips,
1141 uint32_t num_clips,
1142 struct drm_crtc *crtc)
1143{
1144 struct vmw_dma_buffer *buf =
1145 container_of(vfb, struct vmw_framebuffer_dmabuf, base)->buffer;
1146 struct vmw_kms_dirty dirty;
1147 int ret;
1148
1149 ret = vmw_kms_helper_buffer_prepare(dev_priv, buf, true, false,
1150 false);
1151 if (ret)
1152 return ret;
1153
1154 ret = do_dmabuf_define_gmrfb(dev_priv, vfb);
1155 if (unlikely(ret != 0))
1156 goto out_revert;
1157
1158 dirty.crtc = crtc;
1159 dirty.fifo_commit = vmw_sou_readback_fifo_commit;
1160 dirty.clip = vmw_sou_readback_clip;
1161 dirty.fifo_reserve_size = sizeof(struct vmw_kms_sou_readback_blit) *
1162 num_clips;
1163 ret = vmw_kms_helper_dirty(dev_priv, vfb, NULL, vclips,
1164 0, 0, num_clips, 1, &dirty);
1165 vmw_kms_helper_buffer_finish(dev_priv, file_priv, buf, NULL,
1166 user_fence_rep);
1167
1168 return ret;
1169
1170out_revert:
1171 vmw_kms_helper_buffer_revert(buf);
1172
1173 return ret;
1174}