1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /**************************************************************************
4 * 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_bo_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_buffer_object *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,
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;
144 sou->base.set_gui_x = cmd->obj.root.x;
145 sou->base.set_gui_y = cmd->obj.root.y;
147 /* Ok to assume that buffer is pinned in vram */
148 vmw_bo_get_guest_ptr(&sou->buffer->base, &cmd->obj.backingStore.ptr);
149 cmd->obj.backingStore.pitch = mode->hdisplay * 4;
151 vmw_fifo_commit(dev_priv, fifo_size);
159 * Send the fifo command to destroy a screen.
161 static int vmw_sou_fifo_destroy(struct vmw_private *dev_priv,
162 struct vmw_screen_object_unit *sou)
171 SVGAFifoCmdDestroyScreen body;
174 /* no need to do anything */
175 if (unlikely(!sou->defined))
178 fifo_size = sizeof(*cmd);
179 cmd = vmw_fifo_reserve(dev_priv, fifo_size);
180 /* the hardware has hung, nothing we can do about it here */
181 if (unlikely(cmd == NULL)) {
182 DRM_ERROR("Fifo reserve failed.\n");
186 memset(cmd, 0, fifo_size);
187 cmd->header.cmdType = SVGA_CMD_DESTROY_SCREEN;
188 cmd->body.screenId = sou->base.unit;
190 vmw_fifo_commit(dev_priv, fifo_size);
193 ret = vmw_fallback_wait(dev_priv, false, true, 0, false, 3*HZ);
194 if (unlikely(ret != 0))
195 DRM_ERROR("Failed to sync with HW");
197 sou->defined = false;
203 * vmw_sou_crtc_mode_set_nofb - Create new screen
205 * @crtc: CRTC associated with the new screen
207 * This function creates/destroys a screen. This function cannot fail, so if
208 * somehow we run into a failure, just do the best we can to get out.
210 static void vmw_sou_crtc_mode_set_nofb(struct drm_crtc *crtc)
212 struct vmw_private *dev_priv;
213 struct vmw_screen_object_unit *sou;
214 struct vmw_framebuffer *vfb;
215 struct drm_framebuffer *fb;
216 struct drm_plane_state *ps;
217 struct vmw_plane_state *vps;
220 sou = vmw_crtc_to_sou(crtc);
221 dev_priv = vmw_priv(crtc->dev);
222 ps = crtc->primary->state;
224 vps = vmw_plane_state_to_vps(ps);
226 vfb = (fb) ? vmw_framebuffer_to_vfb(fb) : NULL;
229 ret = vmw_sou_fifo_destroy(dev_priv, sou);
231 DRM_ERROR("Failed to destroy Screen Object\n");
237 struct drm_connector_state *conn_state;
238 struct vmw_connector_state *vmw_conn_state;
241 sou->buffer = vps->bo;
242 sou->buffer_size = vps->bo_size;
244 if (sou->base.is_implicit) {
248 conn_state = sou->base.connector.state;
249 vmw_conn_state = vmw_connector_state_to_vcs(conn_state);
251 x = vmw_conn_state->gui_x;
252 y = vmw_conn_state->gui_y;
255 ret = vmw_sou_fifo_create(dev_priv, sou, x, y, &crtc->mode);
257 DRM_ERROR("Failed to define Screen Object %dx%d\n",
260 vmw_kms_add_active(dev_priv, &sou->base, vfb);
263 sou->buffer_size = 0;
265 vmw_kms_del_active(dev_priv, &sou->base);
270 * vmw_sou_crtc_helper_prepare - Noop
272 * @crtc: CRTC associated with the new screen
274 * Prepares the CRTC for a mode set, but we don't need to do anything here.
276 static void vmw_sou_crtc_helper_prepare(struct drm_crtc *crtc)
281 * vmw_sou_crtc_atomic_enable - Noop
283 * @crtc: CRTC associated with the new screen
285 * This is called after a mode set has been completed.
287 static void vmw_sou_crtc_atomic_enable(struct drm_crtc *crtc,
288 struct drm_crtc_state *old_state)
293 * vmw_sou_crtc_atomic_disable - Turns off CRTC
295 * @crtc: CRTC to be turned off
297 static void vmw_sou_crtc_atomic_disable(struct drm_crtc *crtc,
298 struct drm_crtc_state *old_state)
300 struct vmw_private *dev_priv;
301 struct vmw_screen_object_unit *sou;
306 DRM_ERROR("CRTC is NULL\n");
310 sou = vmw_crtc_to_sou(crtc);
311 dev_priv = vmw_priv(crtc->dev);
314 ret = vmw_sou_fifo_destroy(dev_priv, sou);
316 DRM_ERROR("Failed to destroy Screen Object\n");
320 static int vmw_sou_crtc_page_flip(struct drm_crtc *crtc,
321 struct drm_framebuffer *new_fb,
322 struct drm_pending_vblank_event *event,
324 struct drm_modeset_acquire_ctx *ctx)
326 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
329 if (!vmw_kms_crtc_flippable(dev_priv, crtc))
332 ret = drm_atomic_helper_page_flip(crtc, new_fb, event, flags, ctx);
334 DRM_ERROR("Page flip error %d.\n", ret);
338 if (vmw_crtc_to_du(crtc)->is_implicit)
339 vmw_kms_update_implicit_fb(dev_priv, crtc);
344 static const struct drm_crtc_funcs vmw_screen_object_crtc_funcs = {
345 .gamma_set = vmw_du_crtc_gamma_set,
346 .destroy = vmw_sou_crtc_destroy,
347 .reset = vmw_du_crtc_reset,
348 .atomic_duplicate_state = vmw_du_crtc_duplicate_state,
349 .atomic_destroy_state = vmw_du_crtc_destroy_state,
350 .set_config = vmw_kms_set_config,
351 .page_flip = vmw_sou_crtc_page_flip,
355 * Screen Object Display Unit encoder functions
358 static void vmw_sou_encoder_destroy(struct drm_encoder *encoder)
360 vmw_sou_destroy(vmw_encoder_to_sou(encoder));
363 static const struct drm_encoder_funcs vmw_screen_object_encoder_funcs = {
364 .destroy = vmw_sou_encoder_destroy,
368 * Screen Object Display Unit connector functions
371 static void vmw_sou_connector_destroy(struct drm_connector *connector)
373 vmw_sou_destroy(vmw_connector_to_sou(connector));
376 static const struct drm_connector_funcs vmw_sou_connector_funcs = {
377 .dpms = vmw_du_connector_dpms,
378 .detect = vmw_du_connector_detect,
379 .fill_modes = vmw_du_connector_fill_modes,
380 .set_property = vmw_du_connector_set_property,
381 .destroy = vmw_sou_connector_destroy,
382 .reset = vmw_du_connector_reset,
383 .atomic_duplicate_state = vmw_du_connector_duplicate_state,
384 .atomic_destroy_state = vmw_du_connector_destroy_state,
385 .atomic_set_property = vmw_du_connector_atomic_set_property,
386 .atomic_get_property = vmw_du_connector_atomic_get_property,
391 drm_connector_helper_funcs vmw_sou_connector_helper_funcs = {
397 * Screen Object Display Plane Functions
401 * vmw_sou_primary_plane_cleanup_fb - Frees sou backing buffer
403 * @plane: display plane
404 * @old_state: Contains the FB to clean up
406 * Unpins the display surface
408 * Returns 0 on success
411 vmw_sou_primary_plane_cleanup_fb(struct drm_plane *plane,
412 struct drm_plane_state *old_state)
414 struct vmw_plane_state *vps = vmw_plane_state_to_vps(old_state);
415 struct drm_crtc *crtc = plane->state->crtc ?
416 plane->state->crtc : old_state->crtc;
419 vmw_bo_unpin(vmw_priv(crtc->dev), vps->bo, false);
420 vmw_bo_unreference(&vps->bo);
423 vmw_du_plane_cleanup_fb(plane, old_state);
428 * vmw_sou_primary_plane_prepare_fb - allocate backing buffer
430 * @plane: display plane
431 * @new_state: info on the new plane state, including the FB
433 * The SOU backing buffer is our equivalent of the display plane.
435 * Returns 0 on success
438 vmw_sou_primary_plane_prepare_fb(struct drm_plane *plane,
439 struct drm_plane_state *new_state)
441 struct drm_framebuffer *new_fb = new_state->fb;
442 struct drm_crtc *crtc = plane->state->crtc ?: new_state->crtc;
443 struct vmw_plane_state *vps = vmw_plane_state_to_vps(new_state);
444 struct vmw_private *dev_priv;
450 vmw_bo_unreference(&vps->bo);
456 size = new_state->crtc_w * new_state->crtc_h * 4;
457 dev_priv = vmw_priv(crtc->dev);
460 if (vps->bo_size == size) {
462 * Note that this might temporarily up the pin-count
463 * to 2, until cleanup_fb() is called.
465 return vmw_bo_pin_in_vram(dev_priv, vps->bo,
469 vmw_bo_unreference(&vps->bo);
473 vps->bo = kzalloc(sizeof(*vps->bo), GFP_KERNEL);
477 vmw_svga_enable(dev_priv);
479 /* After we have alloced the backing store might not be able to
480 * resume the overlays, this is preferred to failing to alloc.
482 vmw_overlay_pause_all(dev_priv);
483 ret = vmw_bo_init(dev_priv, vps->bo, size,
484 &vmw_vram_ne_placement,
485 false, &vmw_bo_bo_free);
486 vmw_overlay_resume_all(dev_priv);
488 vps->bo = NULL; /* vmw_bo_init frees on error */
495 * TTM already thinks the buffer is pinned, but make sure the
496 * pin_count is upped.
498 return vmw_bo_pin_in_vram(dev_priv, vps->bo, true);
503 vmw_sou_primary_plane_atomic_update(struct drm_plane *plane,
504 struct drm_plane_state *old_state)
506 struct drm_crtc *crtc = plane->state->crtc;
507 struct drm_pending_vblank_event *event = NULL;
508 struct vmw_fence_obj *fence = NULL;
511 if (crtc && plane->state->fb) {
512 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
513 struct vmw_framebuffer *vfb =
514 vmw_framebuffer_to_vfb(plane->state->fb);
515 struct drm_vmw_rect vclips;
519 vclips.w = crtc->mode.hdisplay;
520 vclips.h = crtc->mode.vdisplay;
523 ret = vmw_kms_sou_do_bo_dirty(dev_priv, vfb, NULL,
527 ret = vmw_kms_sou_do_surface_dirty(dev_priv, vfb, NULL,
532 * We cannot really fail this function, so if we do, then output
533 * an error and maintain consistent atomic state.
536 DRM_ERROR("Failed to update screen.\n");
539 * When disabling a plane, CRTC and FB should always be NULL
540 * together, otherwise it's an error.
541 * Here primary plane is being disable so should really blank
542 * the screen object display unit, if not already done.
547 event = crtc->state->event;
549 * In case of failure and other cases, vblank event will be sent in
550 * vmw_du_crtc_atomic_flush.
552 if (event && fence) {
553 struct drm_file *file_priv = event->base.file_priv;
555 ret = vmw_event_fence_action_queue(file_priv,
558 &event->event.vbl.tv_sec,
559 &event->event.vbl.tv_usec,
562 if (unlikely(ret != 0))
563 DRM_ERROR("Failed to queue event on fence.\n");
565 crtc->state->event = NULL;
569 vmw_fence_obj_unreference(&fence);
573 static const struct drm_plane_funcs vmw_sou_plane_funcs = {
574 .update_plane = drm_atomic_helper_update_plane,
575 .disable_plane = drm_atomic_helper_disable_plane,
576 .destroy = vmw_du_primary_plane_destroy,
577 .reset = vmw_du_plane_reset,
578 .atomic_duplicate_state = vmw_du_plane_duplicate_state,
579 .atomic_destroy_state = vmw_du_plane_destroy_state,
582 static const struct drm_plane_funcs vmw_sou_cursor_funcs = {
583 .update_plane = drm_atomic_helper_update_plane,
584 .disable_plane = drm_atomic_helper_disable_plane,
585 .destroy = vmw_du_cursor_plane_destroy,
586 .reset = vmw_du_plane_reset,
587 .atomic_duplicate_state = vmw_du_plane_duplicate_state,
588 .atomic_destroy_state = vmw_du_plane_destroy_state,
595 drm_plane_helper_funcs vmw_sou_cursor_plane_helper_funcs = {
596 .atomic_check = vmw_du_cursor_plane_atomic_check,
597 .atomic_update = vmw_du_cursor_plane_atomic_update,
598 .prepare_fb = vmw_du_cursor_plane_prepare_fb,
599 .cleanup_fb = vmw_du_plane_cleanup_fb,
603 drm_plane_helper_funcs vmw_sou_primary_plane_helper_funcs = {
604 .atomic_check = vmw_du_primary_plane_atomic_check,
605 .atomic_update = vmw_sou_primary_plane_atomic_update,
606 .prepare_fb = vmw_sou_primary_plane_prepare_fb,
607 .cleanup_fb = vmw_sou_primary_plane_cleanup_fb,
610 static const struct drm_crtc_helper_funcs vmw_sou_crtc_helper_funcs = {
611 .prepare = vmw_sou_crtc_helper_prepare,
612 .mode_set_nofb = vmw_sou_crtc_mode_set_nofb,
613 .atomic_check = vmw_du_crtc_atomic_check,
614 .atomic_begin = vmw_du_crtc_atomic_begin,
615 .atomic_flush = vmw_du_crtc_atomic_flush,
616 .atomic_enable = vmw_sou_crtc_atomic_enable,
617 .atomic_disable = vmw_sou_crtc_atomic_disable,
621 static int vmw_sou_init(struct vmw_private *dev_priv, unsigned unit)
623 struct vmw_screen_object_unit *sou;
624 struct drm_device *dev = dev_priv->dev;
625 struct drm_connector *connector;
626 struct drm_encoder *encoder;
627 struct drm_plane *primary, *cursor;
628 struct drm_crtc *crtc;
631 sou = kzalloc(sizeof(*sou), GFP_KERNEL);
635 sou->base.unit = unit;
636 crtc = &sou->base.crtc;
637 encoder = &sou->base.encoder;
638 connector = &sou->base.connector;
639 primary = &sou->base.primary;
640 cursor = &sou->base.cursor;
642 sou->base.active_implicit = false;
643 sou->base.pref_active = (unit == 0);
644 sou->base.pref_width = dev_priv->initial_width;
645 sou->base.pref_height = dev_priv->initial_height;
646 sou->base.pref_mode = NULL;
649 * Remove this after enabling atomic because property values can
650 * only exist in a state object
652 sou->base.is_implicit = false;
654 /* Initialize primary plane */
655 vmw_du_plane_reset(primary);
657 ret = drm_universal_plane_init(dev, &sou->base.primary,
658 0, &vmw_sou_plane_funcs,
659 vmw_primary_plane_formats,
660 ARRAY_SIZE(vmw_primary_plane_formats),
661 NULL, DRM_PLANE_TYPE_PRIMARY, NULL);
663 DRM_ERROR("Failed to initialize primary plane");
667 drm_plane_helper_add(primary, &vmw_sou_primary_plane_helper_funcs);
669 /* Initialize cursor plane */
670 vmw_du_plane_reset(cursor);
672 ret = drm_universal_plane_init(dev, &sou->base.cursor,
673 0, &vmw_sou_cursor_funcs,
674 vmw_cursor_plane_formats,
675 ARRAY_SIZE(vmw_cursor_plane_formats),
676 NULL, DRM_PLANE_TYPE_CURSOR, NULL);
678 DRM_ERROR("Failed to initialize cursor plane");
679 drm_plane_cleanup(&sou->base.primary);
683 drm_plane_helper_add(cursor, &vmw_sou_cursor_plane_helper_funcs);
685 vmw_du_connector_reset(connector);
686 ret = drm_connector_init(dev, connector, &vmw_sou_connector_funcs,
687 DRM_MODE_CONNECTOR_VIRTUAL);
689 DRM_ERROR("Failed to initialize connector\n");
693 drm_connector_helper_add(connector, &vmw_sou_connector_helper_funcs);
694 connector->status = vmw_du_connector_detect(connector, true);
695 vmw_connector_state_to_vcs(connector->state)->is_implicit = false;
698 ret = drm_encoder_init(dev, encoder, &vmw_screen_object_encoder_funcs,
699 DRM_MODE_ENCODER_VIRTUAL, NULL);
701 DRM_ERROR("Failed to initialize encoder\n");
702 goto err_free_connector;
705 (void) drm_connector_attach_encoder(connector, encoder);
706 encoder->possible_crtcs = (1 << unit);
707 encoder->possible_clones = 0;
709 ret = drm_connector_register(connector);
711 DRM_ERROR("Failed to register connector\n");
712 goto err_free_encoder;
716 vmw_du_crtc_reset(crtc);
717 ret = drm_crtc_init_with_planes(dev, crtc, &sou->base.primary,
719 &vmw_screen_object_crtc_funcs, NULL);
721 DRM_ERROR("Failed to initialize CRTC\n");
722 goto err_free_unregister;
725 drm_crtc_helper_add(crtc, &vmw_sou_crtc_helper_funcs);
727 drm_mode_crtc_set_gamma_size(crtc, 256);
729 drm_object_attach_property(&connector->base,
730 dev_priv->hotplug_mode_update_property, 1);
731 drm_object_attach_property(&connector->base,
732 dev->mode_config.suggested_x_property, 0);
733 drm_object_attach_property(&connector->base,
734 dev->mode_config.suggested_y_property, 0);
735 if (dev_priv->implicit_placement_property)
736 drm_object_attach_property
738 dev_priv->implicit_placement_property,
739 sou->base.is_implicit);
744 drm_connector_unregister(connector);
746 drm_encoder_cleanup(encoder);
748 drm_connector_cleanup(connector);
754 int vmw_kms_sou_init_display(struct vmw_private *dev_priv)
756 struct drm_device *dev = dev_priv->dev;
759 if (!(dev_priv->capabilities & SVGA_CAP_SCREEN_OBJECT_2)) {
760 DRM_INFO("Not using screen objects,"
761 " missing cap SCREEN_OBJECT_2\n");
766 dev_priv->num_implicit = 0;
767 dev_priv->implicit_fb = NULL;
769 ret = drm_vblank_init(dev, VMWGFX_NUM_DISPLAY_UNITS);
770 if (unlikely(ret != 0))
773 vmw_kms_create_implicit_placement_property(dev_priv, false);
775 for (i = 0; i < VMWGFX_NUM_DISPLAY_UNITS; ++i)
776 vmw_sou_init(dev_priv, i);
778 dev_priv->active_display_unit = vmw_du_screen_object;
780 DRM_INFO("Screen Objects Display Unit initialized\n");
785 static int do_bo_define_gmrfb(struct vmw_private *dev_priv,
786 struct vmw_framebuffer *framebuffer)
788 struct vmw_buffer_object *buf =
789 container_of(framebuffer, struct vmw_framebuffer_bo,
791 int depth = framebuffer->base.format->depth;
794 SVGAFifoCmdDefineGMRFB body;
797 /* Emulate RGBA support, contrary to svga_reg.h this is not
798 * supported by hosts. This is only a problem if we are reading
799 * this value later and expecting what we uploaded back.
804 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
806 DRM_ERROR("Out of fifo space for dirty framebuffer command.\n");
810 cmd->header = SVGA_CMD_DEFINE_GMRFB;
811 cmd->body.format.bitsPerPixel = framebuffer->base.format->cpp[0] * 8;
812 cmd->body.format.colorDepth = depth;
813 cmd->body.format.reserved = 0;
814 cmd->body.bytesPerLine = framebuffer->base.pitches[0];
815 /* Buffer is reserved in vram or GMR */
816 vmw_bo_get_guest_ptr(&buf->base, &cmd->body.ptr);
817 vmw_fifo_commit(dev_priv, sizeof(*cmd));
823 * vmw_sou_surface_fifo_commit - Callback to fill in and submit a
824 * blit surface to screen command.
826 * @dirty: The closure structure.
828 * Fills in the missing fields in the command, and translates the cliprects
829 * to match the destination bounding box encoded.
831 static void vmw_sou_surface_fifo_commit(struct vmw_kms_dirty *dirty)
833 struct vmw_kms_sou_surface_dirty *sdirty =
834 container_of(dirty, typeof(*sdirty), base);
835 struct vmw_kms_sou_dirty_cmd *cmd = dirty->cmd;
836 s32 trans_x = dirty->unit->crtc.x - sdirty->dst_x;
837 s32 trans_y = dirty->unit->crtc.y - sdirty->dst_y;
838 size_t region_size = dirty->num_hits * sizeof(SVGASignedRect);
839 SVGASignedRect *blit = (SVGASignedRect *) &cmd[1];
842 if (!dirty->num_hits) {
843 vmw_fifo_commit(dirty->dev_priv, 0);
847 cmd->header.id = SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN;
848 cmd->header.size = sizeof(cmd->body) + region_size;
851 * Use the destination bounding box to specify destination - and
852 * source bounding regions.
854 cmd->body.destRect.left = sdirty->left;
855 cmd->body.destRect.right = sdirty->right;
856 cmd->body.destRect.top = sdirty->top;
857 cmd->body.destRect.bottom = sdirty->bottom;
859 cmd->body.srcRect.left = sdirty->left + trans_x;
860 cmd->body.srcRect.right = sdirty->right + trans_x;
861 cmd->body.srcRect.top = sdirty->top + trans_y;
862 cmd->body.srcRect.bottom = sdirty->bottom + trans_y;
864 cmd->body.srcImage.sid = sdirty->sid;
865 cmd->body.destScreenId = dirty->unit->unit;
867 /* Blits are relative to the destination rect. Translate. */
868 for (i = 0; i < dirty->num_hits; ++i, ++blit) {
869 blit->left -= sdirty->left;
870 blit->right -= sdirty->left;
871 blit->top -= sdirty->top;
872 blit->bottom -= sdirty->top;
875 vmw_fifo_commit(dirty->dev_priv, region_size + sizeof(*cmd));
877 sdirty->left = sdirty->top = S32_MAX;
878 sdirty->right = sdirty->bottom = S32_MIN;
882 * vmw_sou_surface_clip - Callback to encode a blit surface to screen cliprect.
884 * @dirty: The closure structure
886 * Encodes a SVGASignedRect cliprect and updates the bounding box of the
887 * BLIT_SURFACE_TO_SCREEN command.
889 static void vmw_sou_surface_clip(struct vmw_kms_dirty *dirty)
891 struct vmw_kms_sou_surface_dirty *sdirty =
892 container_of(dirty, typeof(*sdirty), base);
893 struct vmw_kms_sou_dirty_cmd *cmd = dirty->cmd;
894 SVGASignedRect *blit = (SVGASignedRect *) &cmd[1];
896 /* Destination rect. */
897 blit += dirty->num_hits;
898 blit->left = dirty->unit_x1;
899 blit->top = dirty->unit_y1;
900 blit->right = dirty->unit_x2;
901 blit->bottom = dirty->unit_y2;
903 /* Destination bounding box */
904 sdirty->left = min_t(s32, sdirty->left, dirty->unit_x1);
905 sdirty->top = min_t(s32, sdirty->top, dirty->unit_y1);
906 sdirty->right = max_t(s32, sdirty->right, dirty->unit_x2);
907 sdirty->bottom = max_t(s32, sdirty->bottom, dirty->unit_y2);
913 * vmw_kms_sou_do_surface_dirty - Dirty part of a surface backed framebuffer
915 * @dev_priv: Pointer to the device private structure.
916 * @framebuffer: Pointer to the surface-buffer backed framebuffer.
917 * @clips: Array of clip rects. Either @clips or @vclips must be NULL.
918 * @vclips: Alternate array of clip rects. Either @clips or @vclips must
920 * @srf: Pointer to surface to blit from. If NULL, the surface attached
921 * to @framebuffer will be used.
922 * @dest_x: X coordinate offset to align @srf with framebuffer coordinates.
923 * @dest_y: Y coordinate offset to align @srf with framebuffer coordinates.
924 * @num_clips: Number of clip rects in @clips.
925 * @inc: Increment to use when looping over @clips.
926 * @out_fence: If non-NULL, will return a ref-counted pointer to a
927 * struct vmw_fence_obj. The returned fence pointer may be NULL in which
928 * case the device has already synchronized.
929 * @crtc: If crtc is passed, perform surface dirty on that crtc only.
931 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
934 int vmw_kms_sou_do_surface_dirty(struct vmw_private *dev_priv,
935 struct vmw_framebuffer *framebuffer,
936 struct drm_clip_rect *clips,
937 struct drm_vmw_rect *vclips,
938 struct vmw_resource *srf,
941 unsigned num_clips, int inc,
942 struct vmw_fence_obj **out_fence,
943 struct drm_crtc *crtc)
945 struct vmw_framebuffer_surface *vfbs =
946 container_of(framebuffer, typeof(*vfbs), base);
947 struct vmw_kms_sou_surface_dirty sdirty;
948 DECLARE_VAL_CONTEXT(val_ctx, NULL, 0);
952 srf = &vfbs->surface->res;
954 ret = vmw_validation_add_resource(&val_ctx, srf, 0, NULL, NULL);
958 ret = vmw_validation_prepare(&val_ctx, &dev_priv->cmdbuf_mutex, true);
962 sdirty.base.fifo_commit = vmw_sou_surface_fifo_commit;
963 sdirty.base.clip = vmw_sou_surface_clip;
964 sdirty.base.dev_priv = dev_priv;
965 sdirty.base.fifo_reserve_size = sizeof(struct vmw_kms_sou_dirty_cmd) +
966 sizeof(SVGASignedRect) * num_clips;
967 sdirty.base.crtc = crtc;
969 sdirty.sid = srf->id;
970 sdirty.left = sdirty.top = S32_MAX;
971 sdirty.right = sdirty.bottom = S32_MIN;
972 sdirty.dst_x = dest_x;
973 sdirty.dst_y = dest_y;
975 ret = vmw_kms_helper_dirty(dev_priv, framebuffer, clips, vclips,
976 dest_x, dest_y, num_clips, inc,
978 vmw_kms_helper_validation_finish(dev_priv, NULL, &val_ctx, out_fence,
984 vmw_validation_unref_lists(&val_ctx);
989 * vmw_sou_bo_fifo_commit - Callback to submit a set of readback clips.
991 * @dirty: The closure structure.
993 * Commits a previously built command buffer of readback clips.
995 static void vmw_sou_bo_fifo_commit(struct vmw_kms_dirty *dirty)
997 if (!dirty->num_hits) {
998 vmw_fifo_commit(dirty->dev_priv, 0);
1002 vmw_fifo_commit(dirty->dev_priv,
1003 sizeof(struct vmw_kms_sou_bo_blit) *
1008 * vmw_sou_bo_clip - Callback to encode a readback cliprect.
1010 * @dirty: The closure structure
1012 * Encodes a BLIT_GMRFB_TO_SCREEN cliprect.
1014 static void vmw_sou_bo_clip(struct vmw_kms_dirty *dirty)
1016 struct vmw_kms_sou_bo_blit *blit = dirty->cmd;
1018 blit += dirty->num_hits;
1019 blit->header = SVGA_CMD_BLIT_GMRFB_TO_SCREEN;
1020 blit->body.destScreenId = dirty->unit->unit;
1021 blit->body.srcOrigin.x = dirty->fb_x;
1022 blit->body.srcOrigin.y = dirty->fb_y;
1023 blit->body.destRect.left = dirty->unit_x1;
1024 blit->body.destRect.top = dirty->unit_y1;
1025 blit->body.destRect.right = dirty->unit_x2;
1026 blit->body.destRect.bottom = dirty->unit_y2;
1031 * vmw_kms_do_bo_dirty - Dirty part of a buffer-object backed framebuffer
1033 * @dev_priv: Pointer to the device private structure.
1034 * @framebuffer: Pointer to the buffer-object backed framebuffer.
1035 * @clips: Array of clip rects.
1036 * @vclips: Alternate array of clip rects. Either @clips or @vclips must
1038 * @num_clips: Number of clip rects in @clips.
1039 * @increment: Increment to use when looping over @clips.
1040 * @interruptible: Whether to perform waits interruptible if possible.
1041 * @out_fence: If non-NULL, will return a ref-counted pointer to a
1042 * struct vmw_fence_obj. The returned fence pointer may be NULL in which
1043 * case the device has already synchronized.
1044 * @crtc: If crtc is passed, perform bo dirty on that crtc only.
1046 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
1049 int vmw_kms_sou_do_bo_dirty(struct vmw_private *dev_priv,
1050 struct vmw_framebuffer *framebuffer,
1051 struct drm_clip_rect *clips,
1052 struct drm_vmw_rect *vclips,
1053 unsigned num_clips, int increment,
1055 struct vmw_fence_obj **out_fence,
1056 struct drm_crtc *crtc)
1058 struct vmw_buffer_object *buf =
1059 container_of(framebuffer, struct vmw_framebuffer_bo,
1061 struct vmw_kms_dirty dirty;
1062 DECLARE_VAL_CONTEXT(val_ctx, NULL, 0);
1065 ret = vmw_validation_add_bo(&val_ctx, buf, false, false);
1069 ret = vmw_validation_prepare(&val_ctx, NULL, interruptible);
1073 ret = do_bo_define_gmrfb(dev_priv, framebuffer);
1074 if (unlikely(ret != 0))
1078 dirty.fifo_commit = vmw_sou_bo_fifo_commit;
1079 dirty.clip = vmw_sou_bo_clip;
1080 dirty.fifo_reserve_size = sizeof(struct vmw_kms_sou_bo_blit) *
1082 ret = vmw_kms_helper_dirty(dev_priv, framebuffer, clips, vclips,
1083 0, 0, num_clips, increment, &dirty);
1084 vmw_kms_helper_validation_finish(dev_priv, NULL, &val_ctx, out_fence,
1090 vmw_validation_revert(&val_ctx);
1092 vmw_validation_unref_lists(&val_ctx);
1099 * vmw_sou_readback_fifo_commit - Callback to submit a set of readback clips.
1101 * @dirty: The closure structure.
1103 * Commits a previously built command buffer of readback clips.
1105 static void vmw_sou_readback_fifo_commit(struct vmw_kms_dirty *dirty)
1107 if (!dirty->num_hits) {
1108 vmw_fifo_commit(dirty->dev_priv, 0);
1112 vmw_fifo_commit(dirty->dev_priv,
1113 sizeof(struct vmw_kms_sou_readback_blit) *
1118 * vmw_sou_readback_clip - Callback to encode a readback cliprect.
1120 * @dirty: The closure structure
1122 * Encodes a BLIT_SCREEN_TO_GMRFB cliprect.
1124 static void vmw_sou_readback_clip(struct vmw_kms_dirty *dirty)
1126 struct vmw_kms_sou_readback_blit *blit = dirty->cmd;
1128 blit += dirty->num_hits;
1129 blit->header = SVGA_CMD_BLIT_SCREEN_TO_GMRFB;
1130 blit->body.srcScreenId = dirty->unit->unit;
1131 blit->body.destOrigin.x = dirty->fb_x;
1132 blit->body.destOrigin.y = dirty->fb_y;
1133 blit->body.srcRect.left = dirty->unit_x1;
1134 blit->body.srcRect.top = dirty->unit_y1;
1135 blit->body.srcRect.right = dirty->unit_x2;
1136 blit->body.srcRect.bottom = dirty->unit_y2;
1141 * vmw_kms_sou_readback - Perform a readback from the screen object system to
1142 * a buffer-object backed framebuffer.
1144 * @dev_priv: Pointer to the device private structure.
1145 * @file_priv: Pointer to a struct drm_file identifying the caller.
1146 * Must be set to NULL if @user_fence_rep is NULL.
1147 * @vfb: Pointer to the buffer-object backed framebuffer.
1148 * @user_fence_rep: User-space provided structure for fence information.
1149 * Must be set to non-NULL if @file_priv is non-NULL.
1150 * @vclips: Array of clip rects.
1151 * @num_clips: Number of clip rects in @vclips.
1152 * @crtc: If crtc is passed, readback on that crtc only.
1154 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
1157 int vmw_kms_sou_readback(struct vmw_private *dev_priv,
1158 struct drm_file *file_priv,
1159 struct vmw_framebuffer *vfb,
1160 struct drm_vmw_fence_rep __user *user_fence_rep,
1161 struct drm_vmw_rect *vclips,
1163 struct drm_crtc *crtc)
1165 struct vmw_buffer_object *buf =
1166 container_of(vfb, struct vmw_framebuffer_bo, base)->buffer;
1167 struct vmw_kms_dirty dirty;
1168 DECLARE_VAL_CONTEXT(val_ctx, NULL, 0);
1171 ret = vmw_validation_add_bo(&val_ctx, buf, false, false);
1175 ret = vmw_validation_prepare(&val_ctx, NULL, true);
1179 ret = do_bo_define_gmrfb(dev_priv, vfb);
1180 if (unlikely(ret != 0))
1184 dirty.fifo_commit = vmw_sou_readback_fifo_commit;
1185 dirty.clip = vmw_sou_readback_clip;
1186 dirty.fifo_reserve_size = sizeof(struct vmw_kms_sou_readback_blit) *
1188 ret = vmw_kms_helper_dirty(dev_priv, vfb, NULL, vclips,
1189 0, 0, num_clips, 1, &dirty);
1190 vmw_kms_helper_validation_finish(dev_priv, file_priv, &val_ctx, NULL,
1196 vmw_validation_revert(&val_ctx);
1198 vmw_validation_unref_lists(&val_ctx);