1 /**************************************************************************
3 * Copyright © 2011-2015 VMware, Inc., Palo Alto, CA., USA
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
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>
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)
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.
53 struct vmw_kms_sou_surface_dirty {
54 struct vmw_kms_dirty base;
55 s32 left, right, top, bottom;
61 * SVGA commands that are used by this code. Please see the device headers
64 struct vmw_kms_sou_readback_blit {
66 SVGAFifoCmdBlitScreenToGMRFB body;
69 struct vmw_kms_sou_dmabuf_blit {
71 SVGAFifoCmdBlitGMRFBToScreen body;
74 struct vmw_kms_sou_dirty_cmd {
75 SVGA3dCmdHeader header;
76 SVGA3dCmdBlitSurfaceToScreen body;
80 * Display unit using screen objects.
82 struct vmw_screen_object_unit {
83 struct vmw_display_unit base;
85 unsigned long buffer_size; /**< Size of allocated buffer */
86 struct vmw_dma_buffer *buffer; /**< Backing store buffer */
91 static void vmw_sou_destroy(struct vmw_screen_object_unit *sou)
93 vmw_du_cleanup(&sou->base);
99 * Screen Object Display Unit CRTC functions
102 static void vmw_sou_crtc_destroy(struct drm_crtc *crtc)
104 vmw_sou_destroy(vmw_crtc_to_sou(crtc));
108 * Send the fifo command to create a screen.
110 static 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)
121 SVGAScreenObject obj;
124 BUG_ON(!sou->buffer);
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");
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) {
146 cmd->obj.root.x = sou->base.gui_x;
147 cmd->obj.root.y = sou->base.gui_y;
149 sou->base.set_gui_x = cmd->obj.root.x;
150 sou->base.set_gui_y = cmd->obj.root.y;
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;
156 vmw_fifo_commit(dev_priv, fifo_size);
164 * Send the fifo command to destroy a screen.
166 static int vmw_sou_fifo_destroy(struct vmw_private *dev_priv,
167 struct vmw_screen_object_unit *sou)
176 SVGAFifoCmdDestroyScreen body;
179 /* no need to do anything */
180 if (unlikely(!sou->defined))
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");
191 memset(cmd, 0, fifo_size);
192 cmd->header.cmdType = SVGA_CMD_DESTROY_SCREEN;
193 cmd->body.screenId = sou->base.unit;
195 vmw_fifo_commit(dev_priv, fifo_size);
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");
202 sou->defined = false;
208 * vmw_sou_crtc_mode_set_nofb - Create new screen
210 * @crtc: CRTC associated with the new screen
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.
215 static void vmw_sou_crtc_mode_set_nofb(struct drm_crtc *crtc)
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;
226 sou = vmw_crtc_to_sou(crtc);
227 dev_priv = vmw_priv(crtc->dev);
228 ps = crtc->primary->state;
230 vps = vmw_plane_state_to_vps(ps);
232 vfb = (fb) ? vmw_framebuffer_to_vfb(fb) : NULL;
235 ret = vmw_sou_fifo_destroy(dev_priv, sou);
237 DRM_ERROR("Failed to destroy Screen Object\n");
243 sou->buffer = vps->dmabuf;
244 sou->buffer_size = vps->dmabuf_size;
246 ret = vmw_sou_fifo_create(dev_priv, sou, crtc->x, crtc->y,
249 DRM_ERROR("Failed to define Screen Object %dx%d\n",
252 vmw_kms_add_active(dev_priv, &sou->base, vfb);
255 sou->buffer_size = 0;
257 vmw_kms_del_active(dev_priv, &sou->base);
262 * vmw_sou_crtc_helper_prepare - Noop
264 * @crtc: CRTC associated with the new screen
266 * Prepares the CRTC for a mode set, but we don't need to do anything here.
268 static void vmw_sou_crtc_helper_prepare(struct drm_crtc *crtc)
273 * vmw_sou_crtc_atomic_enable - Noop
275 * @crtc: CRTC associated with the new screen
277 * This is called after a mode set has been completed.
279 static void vmw_sou_crtc_atomic_enable(struct drm_crtc *crtc,
280 struct drm_crtc_state *old_state)
285 * vmw_sou_crtc_atomic_disable - Turns off CRTC
287 * @crtc: CRTC to be turned off
289 static void vmw_sou_crtc_atomic_disable(struct drm_crtc *crtc,
290 struct drm_crtc_state *old_state)
292 struct vmw_private *dev_priv;
293 struct vmw_screen_object_unit *sou;
298 DRM_ERROR("CRTC is NULL\n");
302 sou = vmw_crtc_to_sou(crtc);
303 dev_priv = vmw_priv(crtc->dev);
306 ret = vmw_sou_fifo_destroy(dev_priv, sou);
308 DRM_ERROR("Failed to destroy Screen Object\n");
312 static int vmw_sou_crtc_page_flip(struct drm_crtc *crtc,
313 struct drm_framebuffer *new_fb,
314 struct drm_pending_vblank_event *event,
316 struct drm_modeset_acquire_ctx *ctx)
318 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
321 if (!vmw_kms_crtc_flippable(dev_priv, crtc))
324 ret = drm_atomic_helper_page_flip(crtc, new_fb, event, flags, ctx);
326 DRM_ERROR("Page flip error %d.\n", ret);
330 if (vmw_crtc_to_du(crtc)->is_implicit)
331 vmw_kms_update_implicit_fb(dev_priv, crtc);
336 static 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,
347 * Screen Object Display Unit encoder functions
350 static void vmw_sou_encoder_destroy(struct drm_encoder *encoder)
352 vmw_sou_destroy(vmw_encoder_to_sou(encoder));
355 static const struct drm_encoder_funcs vmw_screen_object_encoder_funcs = {
356 .destroy = vmw_sou_encoder_destroy,
360 * Screen Object Display Unit connector functions
363 static void vmw_sou_connector_destroy(struct drm_connector *connector)
365 vmw_sou_destroy(vmw_connector_to_sou(connector));
368 static 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,
383 drm_connector_helper_funcs vmw_sou_connector_helper_funcs = {
384 .best_encoder = drm_atomic_helper_best_encoder,
390 * Screen Object Display Plane Functions
394 * vmw_sou_primary_plane_cleanup_fb - Frees sou backing buffer
396 * @plane: display plane
397 * @old_state: Contains the FB to clean up
399 * Unpins the display surface
401 * Returns 0 on success
404 vmw_sou_primary_plane_cleanup_fb(struct drm_plane *plane,
405 struct drm_plane_state *old_state)
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;
412 vmw_dmabuf_unpin(vmw_priv(crtc->dev), vps->dmabuf, false);
413 vmw_dmabuf_unreference(&vps->dmabuf);
414 vps->dmabuf_size = 0;
416 vmw_du_plane_cleanup_fb(plane, old_state);
421 * vmw_sou_primary_plane_prepare_fb - allocate backing buffer
423 * @plane: display plane
424 * @new_state: info on the new plane state, including the FB
426 * The SOU backing buffer is our equivalent of the display plane.
428 * Returns 0 on success
431 vmw_sou_primary_plane_prepare_fb(struct drm_plane *plane,
432 struct drm_plane_state *new_state)
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;
443 vmw_dmabuf_unreference(&vps->dmabuf);
444 vps->dmabuf_size = 0;
449 size = new_state->crtc_w * new_state->crtc_h * 4;
450 dev_priv = vmw_priv(crtc->dev);
453 if (vps->dmabuf_size == size) {
455 * Note that this might temporarily up the pin-count
456 * to 2, until cleanup_fb() is called.
458 return vmw_dmabuf_pin_in_vram(dev_priv, vps->dmabuf,
462 vmw_dmabuf_unreference(&vps->dmabuf);
463 vps->dmabuf_size = 0;
466 vps->dmabuf = kzalloc(sizeof(*vps->dmabuf), GFP_KERNEL);
470 vmw_svga_enable(dev_priv);
472 /* After we have alloced the backing store might not be able to
473 * resume the overlays, this is preferred to failing to alloc.
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);
481 vps->dmabuf = NULL; /* vmw_dmabuf_init frees on error */
486 * TTM already thinks the buffer is pinned, but make sure the
487 * pin_count is upped.
489 return vmw_dmabuf_pin_in_vram(dev_priv, vps->dmabuf, true);
494 vmw_sou_primary_plane_atomic_update(struct drm_plane *plane,
495 struct drm_plane_state *old_state)
497 struct drm_crtc *crtc = plane->state->crtc;
498 struct drm_pending_vblank_event *event = NULL;
499 struct vmw_fence_obj *fence = NULL;
502 if (crtc && plane->state->fb) {
503 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
504 struct vmw_framebuffer *vfb =
505 vmw_framebuffer_to_vfb(plane->state->fb);
506 struct drm_vmw_rect vclips;
510 vclips.w = crtc->mode.hdisplay;
511 vclips.h = crtc->mode.vdisplay;
514 ret = vmw_kms_sou_do_dmabuf_dirty(dev_priv, vfb, NULL,
518 ret = vmw_kms_sou_do_surface_dirty(dev_priv, vfb, NULL,
523 * We cannot really fail this function, so if we do, then output
524 * an error and maintain consistent atomic state.
527 DRM_ERROR("Failed to update screen.\n");
529 crtc->primary->fb = plane->state->fb;
532 * When disabling a plane, CRTC and FB should always be NULL
533 * together, otherwise it's an error.
534 * Here primary plane is being disable so should really blank
535 * the screen object display unit, if not already done.
540 event = crtc->state->event;
542 * In case of failure and other cases, vblank event will be sent in
543 * vmw_du_crtc_atomic_flush.
545 if (event && fence) {
546 struct drm_file *file_priv = event->base.file_priv;
548 ret = vmw_event_fence_action_queue(file_priv,
551 &event->event.vbl.tv_sec,
552 &event->event.vbl.tv_usec,
555 if (unlikely(ret != 0))
556 DRM_ERROR("Failed to queue event on fence.\n");
558 crtc->state->event = NULL;
562 vmw_fence_obj_unreference(&fence);
566 static const struct drm_plane_funcs vmw_sou_plane_funcs = {
567 .update_plane = drm_atomic_helper_update_plane,
568 .disable_plane = drm_atomic_helper_disable_plane,
569 .destroy = vmw_du_primary_plane_destroy,
570 .reset = vmw_du_plane_reset,
571 .atomic_duplicate_state = vmw_du_plane_duplicate_state,
572 .atomic_destroy_state = vmw_du_plane_destroy_state,
575 static const struct drm_plane_funcs vmw_sou_cursor_funcs = {
576 .update_plane = drm_atomic_helper_update_plane,
577 .disable_plane = drm_atomic_helper_disable_plane,
578 .destroy = vmw_du_cursor_plane_destroy,
579 .reset = vmw_du_plane_reset,
580 .atomic_duplicate_state = vmw_du_plane_duplicate_state,
581 .atomic_destroy_state = vmw_du_plane_destroy_state,
588 drm_plane_helper_funcs vmw_sou_cursor_plane_helper_funcs = {
589 .atomic_check = vmw_du_cursor_plane_atomic_check,
590 .atomic_update = vmw_du_cursor_plane_atomic_update,
591 .prepare_fb = vmw_du_cursor_plane_prepare_fb,
592 .cleanup_fb = vmw_du_plane_cleanup_fb,
596 drm_plane_helper_funcs vmw_sou_primary_plane_helper_funcs = {
597 .atomic_check = vmw_du_primary_plane_atomic_check,
598 .atomic_update = vmw_sou_primary_plane_atomic_update,
599 .prepare_fb = vmw_sou_primary_plane_prepare_fb,
600 .cleanup_fb = vmw_sou_primary_plane_cleanup_fb,
603 static const struct drm_crtc_helper_funcs vmw_sou_crtc_helper_funcs = {
604 .prepare = vmw_sou_crtc_helper_prepare,
605 .mode_set_nofb = vmw_sou_crtc_mode_set_nofb,
606 .atomic_check = vmw_du_crtc_atomic_check,
607 .atomic_begin = vmw_du_crtc_atomic_begin,
608 .atomic_flush = vmw_du_crtc_atomic_flush,
609 .atomic_enable = vmw_sou_crtc_atomic_enable,
610 .atomic_disable = vmw_sou_crtc_atomic_disable,
614 static int vmw_sou_init(struct vmw_private *dev_priv, unsigned unit)
616 struct vmw_screen_object_unit *sou;
617 struct drm_device *dev = dev_priv->dev;
618 struct drm_connector *connector;
619 struct drm_encoder *encoder;
620 struct drm_plane *primary, *cursor;
621 struct drm_crtc *crtc;
624 sou = kzalloc(sizeof(*sou), GFP_KERNEL);
628 sou->base.unit = unit;
629 crtc = &sou->base.crtc;
630 encoder = &sou->base.encoder;
631 connector = &sou->base.connector;
632 primary = &sou->base.primary;
633 cursor = &sou->base.cursor;
635 sou->base.active_implicit = false;
636 sou->base.pref_active = (unit == 0);
637 sou->base.pref_width = dev_priv->initial_width;
638 sou->base.pref_height = dev_priv->initial_height;
639 sou->base.pref_mode = NULL;
642 * Remove this after enabling atomic because property values can
643 * only exist in a state object
645 sou->base.is_implicit = false;
647 /* Initialize primary plane */
648 vmw_du_plane_reset(primary);
650 ret = drm_universal_plane_init(dev, &sou->base.primary,
651 0, &vmw_sou_plane_funcs,
652 vmw_primary_plane_formats,
653 ARRAY_SIZE(vmw_primary_plane_formats),
654 NULL, DRM_PLANE_TYPE_PRIMARY, NULL);
656 DRM_ERROR("Failed to initialize primary plane");
660 drm_plane_helper_add(primary, &vmw_sou_primary_plane_helper_funcs);
662 /* Initialize cursor plane */
663 vmw_du_plane_reset(cursor);
665 ret = drm_universal_plane_init(dev, &sou->base.cursor,
666 0, &vmw_sou_cursor_funcs,
667 vmw_cursor_plane_formats,
668 ARRAY_SIZE(vmw_cursor_plane_formats),
669 NULL, DRM_PLANE_TYPE_CURSOR, NULL);
671 DRM_ERROR("Failed to initialize cursor plane");
672 drm_plane_cleanup(&sou->base.primary);
676 drm_plane_helper_add(cursor, &vmw_sou_cursor_plane_helper_funcs);
678 vmw_du_connector_reset(connector);
679 ret = drm_connector_init(dev, connector, &vmw_sou_connector_funcs,
680 DRM_MODE_CONNECTOR_VIRTUAL);
682 DRM_ERROR("Failed to initialize connector\n");
686 drm_connector_helper_add(connector, &vmw_sou_connector_helper_funcs);
687 connector->status = vmw_du_connector_detect(connector, true);
688 vmw_connector_state_to_vcs(connector->state)->is_implicit = false;
691 ret = drm_encoder_init(dev, encoder, &vmw_screen_object_encoder_funcs,
692 DRM_MODE_ENCODER_VIRTUAL, NULL);
694 DRM_ERROR("Failed to initialize encoder\n");
695 goto err_free_connector;
698 (void) drm_mode_connector_attach_encoder(connector, encoder);
699 encoder->possible_crtcs = (1 << unit);
700 encoder->possible_clones = 0;
702 ret = drm_connector_register(connector);
704 DRM_ERROR("Failed to register connector\n");
705 goto err_free_encoder;
709 vmw_du_crtc_reset(crtc);
710 ret = drm_crtc_init_with_planes(dev, crtc, &sou->base.primary,
712 &vmw_screen_object_crtc_funcs, NULL);
714 DRM_ERROR("Failed to initialize CRTC\n");
715 goto err_free_unregister;
718 drm_crtc_helper_add(crtc, &vmw_sou_crtc_helper_funcs);
720 drm_mode_crtc_set_gamma_size(crtc, 256);
722 drm_object_attach_property(&connector->base,
723 dev_priv->hotplug_mode_update_property, 1);
724 drm_object_attach_property(&connector->base,
725 dev->mode_config.suggested_x_property, 0);
726 drm_object_attach_property(&connector->base,
727 dev->mode_config.suggested_y_property, 0);
728 if (dev_priv->implicit_placement_property)
729 drm_object_attach_property
731 dev_priv->implicit_placement_property,
732 sou->base.is_implicit);
737 drm_connector_unregister(connector);
739 drm_encoder_cleanup(encoder);
741 drm_connector_cleanup(connector);
747 int vmw_kms_sou_init_display(struct vmw_private *dev_priv)
749 struct drm_device *dev = dev_priv->dev;
752 if (!(dev_priv->capabilities & SVGA_CAP_SCREEN_OBJECT_2)) {
753 DRM_INFO("Not using screen objects,"
754 " missing cap SCREEN_OBJECT_2\n");
759 dev_priv->num_implicit = 0;
760 dev_priv->implicit_fb = NULL;
762 ret = drm_vblank_init(dev, VMWGFX_NUM_DISPLAY_UNITS);
763 if (unlikely(ret != 0))
766 vmw_kms_create_implicit_placement_property(dev_priv, false);
768 for (i = 0; i < VMWGFX_NUM_DISPLAY_UNITS; ++i)
769 vmw_sou_init(dev_priv, i);
771 dev_priv->active_display_unit = vmw_du_screen_object;
773 DRM_INFO("Screen Objects Display Unit initialized\n");
778 static int do_dmabuf_define_gmrfb(struct vmw_private *dev_priv,
779 struct vmw_framebuffer *framebuffer)
781 struct vmw_dma_buffer *buf =
782 container_of(framebuffer, struct vmw_framebuffer_dmabuf,
784 int depth = framebuffer->base.format->depth;
787 SVGAFifoCmdDefineGMRFB body;
790 /* Emulate RGBA support, contrary to svga_reg.h this is not
791 * supported by hosts. This is only a problem if we are reading
792 * this value later and expecting what we uploaded back.
797 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
799 DRM_ERROR("Out of fifo space for dirty framebuffer command.\n");
803 cmd->header = SVGA_CMD_DEFINE_GMRFB;
804 cmd->body.format.bitsPerPixel = framebuffer->base.format->cpp[0] * 8;
805 cmd->body.format.colorDepth = depth;
806 cmd->body.format.reserved = 0;
807 cmd->body.bytesPerLine = framebuffer->base.pitches[0];
808 /* Buffer is reserved in vram or GMR */
809 vmw_bo_get_guest_ptr(&buf->base, &cmd->body.ptr);
810 vmw_fifo_commit(dev_priv, sizeof(*cmd));
816 * vmw_sou_surface_fifo_commit - Callback to fill in and submit a
817 * blit surface to screen command.
819 * @dirty: The closure structure.
821 * Fills in the missing fields in the command, and translates the cliprects
822 * to match the destination bounding box encoded.
824 static void vmw_sou_surface_fifo_commit(struct vmw_kms_dirty *dirty)
826 struct vmw_kms_sou_surface_dirty *sdirty =
827 container_of(dirty, typeof(*sdirty), base);
828 struct vmw_kms_sou_dirty_cmd *cmd = dirty->cmd;
829 s32 trans_x = dirty->unit->crtc.x - sdirty->dst_x;
830 s32 trans_y = dirty->unit->crtc.y - sdirty->dst_y;
831 size_t region_size = dirty->num_hits * sizeof(SVGASignedRect);
832 SVGASignedRect *blit = (SVGASignedRect *) &cmd[1];
835 if (!dirty->num_hits) {
836 vmw_fifo_commit(dirty->dev_priv, 0);
840 cmd->header.id = SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN;
841 cmd->header.size = sizeof(cmd->body) + region_size;
844 * Use the destination bounding box to specify destination - and
845 * source bounding regions.
847 cmd->body.destRect.left = sdirty->left;
848 cmd->body.destRect.right = sdirty->right;
849 cmd->body.destRect.top = sdirty->top;
850 cmd->body.destRect.bottom = sdirty->bottom;
852 cmd->body.srcRect.left = sdirty->left + trans_x;
853 cmd->body.srcRect.right = sdirty->right + trans_x;
854 cmd->body.srcRect.top = sdirty->top + trans_y;
855 cmd->body.srcRect.bottom = sdirty->bottom + trans_y;
857 cmd->body.srcImage.sid = sdirty->sid;
858 cmd->body.destScreenId = dirty->unit->unit;
860 /* Blits are relative to the destination rect. Translate. */
861 for (i = 0; i < dirty->num_hits; ++i, ++blit) {
862 blit->left -= sdirty->left;
863 blit->right -= sdirty->left;
864 blit->top -= sdirty->top;
865 blit->bottom -= sdirty->top;
868 vmw_fifo_commit(dirty->dev_priv, region_size + sizeof(*cmd));
870 sdirty->left = sdirty->top = S32_MAX;
871 sdirty->right = sdirty->bottom = S32_MIN;
875 * vmw_sou_surface_clip - Callback to encode a blit surface to screen cliprect.
877 * @dirty: The closure structure
879 * Encodes a SVGASignedRect cliprect and updates the bounding box of the
880 * BLIT_SURFACE_TO_SCREEN command.
882 static void vmw_sou_surface_clip(struct vmw_kms_dirty *dirty)
884 struct vmw_kms_sou_surface_dirty *sdirty =
885 container_of(dirty, typeof(*sdirty), base);
886 struct vmw_kms_sou_dirty_cmd *cmd = dirty->cmd;
887 SVGASignedRect *blit = (SVGASignedRect *) &cmd[1];
889 /* Destination rect. */
890 blit += dirty->num_hits;
891 blit->left = dirty->unit_x1;
892 blit->top = dirty->unit_y1;
893 blit->right = dirty->unit_x2;
894 blit->bottom = dirty->unit_y2;
896 /* Destination bounding box */
897 sdirty->left = min_t(s32, sdirty->left, dirty->unit_x1);
898 sdirty->top = min_t(s32, sdirty->top, dirty->unit_y1);
899 sdirty->right = max_t(s32, sdirty->right, dirty->unit_x2);
900 sdirty->bottom = max_t(s32, sdirty->bottom, dirty->unit_y2);
906 * vmw_kms_sou_do_surface_dirty - Dirty part of a surface backed framebuffer
908 * @dev_priv: Pointer to the device private structure.
909 * @framebuffer: Pointer to the surface-buffer backed framebuffer.
910 * @clips: Array of clip rects. Either @clips or @vclips must be NULL.
911 * @vclips: Alternate array of clip rects. Either @clips or @vclips must
913 * @srf: Pointer to surface to blit from. If NULL, the surface attached
914 * to @framebuffer will be used.
915 * @dest_x: X coordinate offset to align @srf with framebuffer coordinates.
916 * @dest_y: Y coordinate offset to align @srf with framebuffer coordinates.
917 * @num_clips: Number of clip rects in @clips.
918 * @inc: Increment to use when looping over @clips.
919 * @out_fence: If non-NULL, will return a ref-counted pointer to a
920 * struct vmw_fence_obj. The returned fence pointer may be NULL in which
921 * case the device has already synchronized.
922 * @crtc: If crtc is passed, perform surface dirty on that crtc only.
924 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
927 int vmw_kms_sou_do_surface_dirty(struct vmw_private *dev_priv,
928 struct vmw_framebuffer *framebuffer,
929 struct drm_clip_rect *clips,
930 struct drm_vmw_rect *vclips,
931 struct vmw_resource *srf,
934 unsigned num_clips, int inc,
935 struct vmw_fence_obj **out_fence,
936 struct drm_crtc *crtc)
938 struct vmw_framebuffer_surface *vfbs =
939 container_of(framebuffer, typeof(*vfbs), base);
940 struct vmw_kms_sou_surface_dirty sdirty;
941 struct vmw_validation_ctx ctx;
945 srf = &vfbs->surface->res;
947 ret = vmw_kms_helper_resource_prepare(srf, true, &ctx);
951 sdirty.base.fifo_commit = vmw_sou_surface_fifo_commit;
952 sdirty.base.clip = vmw_sou_surface_clip;
953 sdirty.base.dev_priv = dev_priv;
954 sdirty.base.fifo_reserve_size = sizeof(struct vmw_kms_sou_dirty_cmd) +
955 sizeof(SVGASignedRect) * num_clips;
956 sdirty.base.crtc = crtc;
958 sdirty.sid = srf->id;
959 sdirty.left = sdirty.top = S32_MAX;
960 sdirty.right = sdirty.bottom = S32_MIN;
961 sdirty.dst_x = dest_x;
962 sdirty.dst_y = dest_y;
964 ret = vmw_kms_helper_dirty(dev_priv, framebuffer, clips, vclips,
965 dest_x, dest_y, num_clips, inc,
967 vmw_kms_helper_resource_finish(&ctx, out_fence);
973 * vmw_sou_dmabuf_fifo_commit - Callback to submit a set of readback clips.
975 * @dirty: The closure structure.
977 * Commits a previously built command buffer of readback clips.
979 static void vmw_sou_dmabuf_fifo_commit(struct vmw_kms_dirty *dirty)
981 if (!dirty->num_hits) {
982 vmw_fifo_commit(dirty->dev_priv, 0);
986 vmw_fifo_commit(dirty->dev_priv,
987 sizeof(struct vmw_kms_sou_dmabuf_blit) *
992 * vmw_sou_dmabuf_clip - Callback to encode a readback cliprect.
994 * @dirty: The closure structure
996 * Encodes a BLIT_GMRFB_TO_SCREEN cliprect.
998 static void vmw_sou_dmabuf_clip(struct vmw_kms_dirty *dirty)
1000 struct vmw_kms_sou_dmabuf_blit *blit = dirty->cmd;
1002 blit += dirty->num_hits;
1003 blit->header = SVGA_CMD_BLIT_GMRFB_TO_SCREEN;
1004 blit->body.destScreenId = dirty->unit->unit;
1005 blit->body.srcOrigin.x = dirty->fb_x;
1006 blit->body.srcOrigin.y = dirty->fb_y;
1007 blit->body.destRect.left = dirty->unit_x1;
1008 blit->body.destRect.top = dirty->unit_y1;
1009 blit->body.destRect.right = dirty->unit_x2;
1010 blit->body.destRect.bottom = dirty->unit_y2;
1015 * vmw_kms_do_dmabuf_dirty - Dirty part of a dma-buffer backed framebuffer
1017 * @dev_priv: Pointer to the device private structure.
1018 * @framebuffer: Pointer to the dma-buffer backed framebuffer.
1019 * @clips: Array of clip rects.
1020 * @vclips: Alternate array of clip rects. Either @clips or @vclips must
1022 * @num_clips: Number of clip rects in @clips.
1023 * @increment: Increment to use when looping over @clips.
1024 * @interruptible: Whether to perform waits interruptible if possible.
1025 * @out_fence: If non-NULL, will return a ref-counted pointer to a
1026 * struct vmw_fence_obj. The returned fence pointer may be NULL in which
1027 * case the device has already synchronized.
1028 * @crtc: If crtc is passed, perform dmabuf dirty on that crtc only.
1030 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
1033 int vmw_kms_sou_do_dmabuf_dirty(struct vmw_private *dev_priv,
1034 struct vmw_framebuffer *framebuffer,
1035 struct drm_clip_rect *clips,
1036 struct drm_vmw_rect *vclips,
1037 unsigned num_clips, int increment,
1039 struct vmw_fence_obj **out_fence,
1040 struct drm_crtc *crtc)
1042 struct vmw_dma_buffer *buf =
1043 container_of(framebuffer, struct vmw_framebuffer_dmabuf,
1045 struct vmw_kms_dirty dirty;
1048 ret = vmw_kms_helper_buffer_prepare(dev_priv, buf, interruptible,
1053 ret = do_dmabuf_define_gmrfb(dev_priv, framebuffer);
1054 if (unlikely(ret != 0))
1058 dirty.fifo_commit = vmw_sou_dmabuf_fifo_commit;
1059 dirty.clip = vmw_sou_dmabuf_clip;
1060 dirty.fifo_reserve_size = sizeof(struct vmw_kms_sou_dmabuf_blit) *
1062 ret = vmw_kms_helper_dirty(dev_priv, framebuffer, clips, vclips,
1063 0, 0, num_clips, increment, &dirty);
1064 vmw_kms_helper_buffer_finish(dev_priv, NULL, buf, out_fence, NULL);
1069 vmw_kms_helper_buffer_revert(buf);
1076 * vmw_sou_readback_fifo_commit - Callback to submit a set of readback clips.
1078 * @dirty: The closure structure.
1080 * Commits a previously built command buffer of readback clips.
1082 static void vmw_sou_readback_fifo_commit(struct vmw_kms_dirty *dirty)
1084 if (!dirty->num_hits) {
1085 vmw_fifo_commit(dirty->dev_priv, 0);
1089 vmw_fifo_commit(dirty->dev_priv,
1090 sizeof(struct vmw_kms_sou_readback_blit) *
1095 * vmw_sou_readback_clip - Callback to encode a readback cliprect.
1097 * @dirty: The closure structure
1099 * Encodes a BLIT_SCREEN_TO_GMRFB cliprect.
1101 static void vmw_sou_readback_clip(struct vmw_kms_dirty *dirty)
1103 struct vmw_kms_sou_readback_blit *blit = dirty->cmd;
1105 blit += dirty->num_hits;
1106 blit->header = SVGA_CMD_BLIT_SCREEN_TO_GMRFB;
1107 blit->body.srcScreenId = dirty->unit->unit;
1108 blit->body.destOrigin.x = dirty->fb_x;
1109 blit->body.destOrigin.y = dirty->fb_y;
1110 blit->body.srcRect.left = dirty->unit_x1;
1111 blit->body.srcRect.top = dirty->unit_y1;
1112 blit->body.srcRect.right = dirty->unit_x2;
1113 blit->body.srcRect.bottom = dirty->unit_y2;
1118 * vmw_kms_sou_readback - Perform a readback from the screen object system to
1119 * a dma-buffer backed framebuffer.
1121 * @dev_priv: Pointer to the device private structure.
1122 * @file_priv: Pointer to a struct drm_file identifying the caller.
1123 * Must be set to NULL if @user_fence_rep is NULL.
1124 * @vfb: Pointer to the dma-buffer backed framebuffer.
1125 * @user_fence_rep: User-space provided structure for fence information.
1126 * Must be set to non-NULL if @file_priv is non-NULL.
1127 * @vclips: Array of clip rects.
1128 * @num_clips: Number of clip rects in @vclips.
1129 * @crtc: If crtc is passed, readback on that crtc only.
1131 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
1134 int vmw_kms_sou_readback(struct vmw_private *dev_priv,
1135 struct drm_file *file_priv,
1136 struct vmw_framebuffer *vfb,
1137 struct drm_vmw_fence_rep __user *user_fence_rep,
1138 struct drm_vmw_rect *vclips,
1140 struct drm_crtc *crtc)
1142 struct vmw_dma_buffer *buf =
1143 container_of(vfb, struct vmw_framebuffer_dmabuf, base)->buffer;
1144 struct vmw_kms_dirty dirty;
1147 ret = vmw_kms_helper_buffer_prepare(dev_priv, buf, true, false,
1152 ret = do_dmabuf_define_gmrfb(dev_priv, vfb);
1153 if (unlikely(ret != 0))
1157 dirty.fifo_commit = vmw_sou_readback_fifo_commit;
1158 dirty.clip = vmw_sou_readback_clip;
1159 dirty.fifo_reserve_size = sizeof(struct vmw_kms_sou_readback_blit) *
1161 ret = vmw_kms_helper_dirty(dev_priv, vfb, NULL, vclips,
1162 0, 0, num_clips, 1, &dirty);
1163 vmw_kms_helper_buffer_finish(dev_priv, file_priv, buf, NULL,
1169 vmw_kms_helper_buffer_revert(buf);