1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /******************************************************************************
4 * COPYRIGHT (C) 2014-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 "device_include/svga3d_surfacedefs.h"
30 #include <drm/drm_plane_helper.h>
31 #include <drm/drm_atomic.h>
32 #include <drm/drm_atomic_helper.h>
35 #define vmw_crtc_to_stdu(x) \
36 container_of(x, struct vmw_screen_target_display_unit, base.crtc)
37 #define vmw_encoder_to_stdu(x) \
38 container_of(x, struct vmw_screen_target_display_unit, base.encoder)
39 #define vmw_connector_to_stdu(x) \
40 container_of(x, struct vmw_screen_target_display_unit, base.connector)
44 enum stdu_content_type {
51 * struct vmw_stdu_dirty - closure structure for the update functions
53 * @base: The base type we derive from. Used by vmw_kms_helper_dirty().
54 * @transfer: Transfer direction for DMA command.
55 * @left: Left side of bounding box.
56 * @right: Right side of bounding box.
57 * @top: Top side of bounding box.
58 * @bottom: Bottom side of bounding box.
59 * @fb_left: Left side of the framebuffer/content bounding box
60 * @fb_top: Top of the framebuffer/content bounding box
61 * @buf: buffer object when DMA-ing between buffer and screen targets.
62 * @sid: Surface ID when copying between surface and screen targets.
64 struct vmw_stdu_dirty {
65 struct vmw_kms_dirty base;
66 SVGA3dTransferType transfer;
67 s32 left, right, top, bottom;
71 struct vmw_buffer_object *buf;
77 * SVGA commands that are used by this code. Please see the device headers
80 struct vmw_stdu_update {
81 SVGA3dCmdHeader header;
82 SVGA3dCmdUpdateGBScreenTarget body;
86 SVGA3dCmdHeader header;
87 SVGA3dCmdSurfaceDMA body;
90 struct vmw_stdu_surface_copy {
91 SVGA3dCmdHeader header;
92 SVGA3dCmdSurfaceCopy body;
97 * struct vmw_screen_target_display_unit
99 * @base: VMW specific DU structure
100 * @display_srf: surface to be displayed. The dimension of this will always
101 * match the display mode. If the display mode matches
102 * content_vfbs dimensions, then this is a pointer into the
103 * corresponding field in content_vfbs. If not, then this
104 * is a separate buffer to which content_vfbs will blit to.
105 * @content_type: content_fb type
106 * @defined: true if the current display unit has been initialized
108 struct vmw_screen_target_display_unit {
109 struct vmw_display_unit base;
110 const struct vmw_surface *display_srf;
111 enum stdu_content_type content_fb_type;
112 s32 display_width, display_height;
122 static void vmw_stdu_destroy(struct vmw_screen_target_display_unit *stdu);
126 /******************************************************************************
127 * Screen Target Display Unit CRTC Functions
128 *****************************************************************************/
132 * vmw_stdu_crtc_destroy - cleans up the STDU
134 * @crtc: used to get a reference to the containing STDU
136 static void vmw_stdu_crtc_destroy(struct drm_crtc *crtc)
138 vmw_stdu_destroy(vmw_crtc_to_stdu(crtc));
142 * vmw_stdu_define_st - Defines a Screen Target
144 * @dev_priv: VMW DRM device
145 * @stdu: display unit to create a Screen Target for
146 * @mode: The mode to set.
147 * @crtc_x: X coordinate of screen target relative to framebuffer origin.
148 * @crtc_y: Y coordinate of screen target relative to framebuffer origin.
150 * Creates a STDU that we can used later. This function is called whenever the
151 * framebuffer size changes.
154 * 0 on success, error code on failure
156 static int vmw_stdu_define_st(struct vmw_private *dev_priv,
157 struct vmw_screen_target_display_unit *stdu,
158 struct drm_display_mode *mode,
159 int crtc_x, int crtc_y)
162 SVGA3dCmdHeader header;
163 SVGA3dCmdDefineGBScreenTarget body;
166 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
168 if (unlikely(cmd == NULL)) {
169 DRM_ERROR("Out of FIFO space defining Screen Target\n");
173 cmd->header.id = SVGA_3D_CMD_DEFINE_GB_SCREENTARGET;
174 cmd->header.size = sizeof(cmd->body);
176 cmd->body.stid = stdu->base.unit;
177 cmd->body.width = mode->hdisplay;
178 cmd->body.height = mode->vdisplay;
179 cmd->body.flags = (0 == cmd->body.stid) ? SVGA_STFLAG_PRIMARY : 0;
181 cmd->body.xRoot = crtc_x;
182 cmd->body.yRoot = crtc_y;
184 stdu->base.set_gui_x = cmd->body.xRoot;
185 stdu->base.set_gui_y = cmd->body.yRoot;
187 vmw_fifo_commit(dev_priv, sizeof(*cmd));
189 stdu->defined = true;
190 stdu->display_width = mode->hdisplay;
191 stdu->display_height = mode->vdisplay;
199 * vmw_stdu_bind_st - Binds a surface to a Screen Target
201 * @dev_priv: VMW DRM device
202 * @stdu: display unit affected
203 * @res: Buffer to bind to the screen target. Set to NULL to blank screen.
205 * Binding a surface to a Screen Target the same as flipping
207 static int vmw_stdu_bind_st(struct vmw_private *dev_priv,
208 struct vmw_screen_target_display_unit *stdu,
209 const struct vmw_resource *res)
211 SVGA3dSurfaceImageId image;
214 SVGA3dCmdHeader header;
215 SVGA3dCmdBindGBScreenTarget body;
219 if (!stdu->defined) {
220 DRM_ERROR("No screen target defined\n");
224 /* Set up image using information in vfb */
225 memset(&image, 0, sizeof(image));
226 image.sid = res ? res->id : SVGA3D_INVALID_ID;
228 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
230 if (unlikely(cmd == NULL)) {
231 DRM_ERROR("Out of FIFO space binding a screen target\n");
235 cmd->header.id = SVGA_3D_CMD_BIND_GB_SCREENTARGET;
236 cmd->header.size = sizeof(cmd->body);
238 cmd->body.stid = stdu->base.unit;
239 cmd->body.image = image;
241 vmw_fifo_commit(dev_priv, sizeof(*cmd));
247 * vmw_stdu_populate_update - populate an UPDATE_GB_SCREENTARGET command with a
250 * @cmd: Pointer to command stream.
251 * @unit: Screen target unit.
252 * @left: Left side of bounding box.
253 * @right: Right side of bounding box.
254 * @top: Top side of bounding box.
255 * @bottom: Bottom side of bounding box.
257 static void vmw_stdu_populate_update(void *cmd, int unit,
258 s32 left, s32 right, s32 top, s32 bottom)
260 struct vmw_stdu_update *update = cmd;
262 update->header.id = SVGA_3D_CMD_UPDATE_GB_SCREENTARGET;
263 update->header.size = sizeof(update->body);
265 update->body.stid = unit;
266 update->body.rect.x = left;
267 update->body.rect.y = top;
268 update->body.rect.w = right - left;
269 update->body.rect.h = bottom - top;
273 * vmw_stdu_update_st - Full update of a Screen Target
275 * @dev_priv: VMW DRM device
276 * @stdu: display unit affected
278 * This function needs to be called whenever the content of a screen
279 * target has changed completely. Typically as a result of a backing
283 * 0 on success, error code on failure
285 static int vmw_stdu_update_st(struct vmw_private *dev_priv,
286 struct vmw_screen_target_display_unit *stdu)
288 struct vmw_stdu_update *cmd;
290 if (!stdu->defined) {
291 DRM_ERROR("No screen target defined");
295 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
297 if (unlikely(cmd == NULL)) {
298 DRM_ERROR("Out of FIFO space updating a Screen Target\n");
302 vmw_stdu_populate_update(cmd, stdu->base.unit,
303 0, stdu->display_width,
304 0, stdu->display_height);
306 vmw_fifo_commit(dev_priv, sizeof(*cmd));
314 * vmw_stdu_destroy_st - Destroy a Screen Target
316 * @dev_priv: VMW DRM device
317 * @stdu: display unit to destroy
319 static int vmw_stdu_destroy_st(struct vmw_private *dev_priv,
320 struct vmw_screen_target_display_unit *stdu)
325 SVGA3dCmdHeader header;
326 SVGA3dCmdDestroyGBScreenTarget body;
330 /* Nothing to do if not successfully defined */
331 if (unlikely(!stdu->defined))
334 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
336 if (unlikely(cmd == NULL)) {
337 DRM_ERROR("Out of FIFO space, screen target not destroyed\n");
341 cmd->header.id = SVGA_3D_CMD_DESTROY_GB_SCREENTARGET;
342 cmd->header.size = sizeof(cmd->body);
344 cmd->body.stid = stdu->base.unit;
346 vmw_fifo_commit(dev_priv, sizeof(*cmd));
349 ret = vmw_fallback_wait(dev_priv, false, true, 0, false, 3*HZ);
350 if (unlikely(ret != 0))
351 DRM_ERROR("Failed to sync with HW");
353 stdu->defined = false;
354 stdu->display_width = 0;
355 stdu->display_height = 0;
362 * vmw_stdu_crtc_mode_set_nofb - Updates screen target size
364 * @crtc: CRTC associated with the screen target
366 * This function defines/destroys a screen target
369 static void vmw_stdu_crtc_mode_set_nofb(struct drm_crtc *crtc)
371 struct vmw_private *dev_priv;
372 struct vmw_screen_target_display_unit *stdu;
373 struct drm_connector_state *conn_state;
374 struct vmw_connector_state *vmw_conn_state;
377 stdu = vmw_crtc_to_stdu(crtc);
378 dev_priv = vmw_priv(crtc->dev);
379 conn_state = stdu->base.connector.state;
380 vmw_conn_state = vmw_connector_state_to_vcs(conn_state);
383 ret = vmw_stdu_bind_st(dev_priv, stdu, NULL);
385 DRM_ERROR("Failed to blank CRTC\n");
387 (void) vmw_stdu_update_st(dev_priv, stdu);
389 ret = vmw_stdu_destroy_st(dev_priv, stdu);
391 DRM_ERROR("Failed to destroy Screen Target\n");
393 stdu->content_fb_type = SAME_AS_DISPLAY;
396 if (!crtc->state->enable)
399 if (stdu->base.is_implicit) {
403 x = vmw_conn_state->gui_x;
404 y = vmw_conn_state->gui_y;
407 vmw_svga_enable(dev_priv);
408 ret = vmw_stdu_define_st(dev_priv, stdu, &crtc->mode, x, y);
411 DRM_ERROR("Failed to define Screen Target of size %dx%d\n",
416 static void vmw_stdu_crtc_helper_prepare(struct drm_crtc *crtc)
421 static void vmw_stdu_crtc_atomic_enable(struct drm_crtc *crtc,
422 struct drm_crtc_state *old_state)
424 struct drm_plane_state *plane_state = crtc->primary->state;
425 struct vmw_private *dev_priv;
426 struct vmw_screen_target_display_unit *stdu;
427 struct vmw_framebuffer *vfb;
428 struct drm_framebuffer *fb;
431 stdu = vmw_crtc_to_stdu(crtc);
432 dev_priv = vmw_priv(crtc->dev);
433 fb = plane_state->fb;
435 vfb = (fb) ? vmw_framebuffer_to_vfb(fb) : NULL;
438 vmw_kms_add_active(dev_priv, &stdu->base, vfb);
440 vmw_kms_del_active(dev_priv, &stdu->base);
443 static void vmw_stdu_crtc_atomic_disable(struct drm_crtc *crtc,
444 struct drm_crtc_state *old_state)
446 struct vmw_private *dev_priv;
447 struct vmw_screen_target_display_unit *stdu;
452 DRM_ERROR("CRTC is NULL\n");
456 stdu = vmw_crtc_to_stdu(crtc);
457 dev_priv = vmw_priv(crtc->dev);
460 ret = vmw_stdu_bind_st(dev_priv, stdu, NULL);
462 DRM_ERROR("Failed to blank CRTC\n");
464 (void) vmw_stdu_update_st(dev_priv, stdu);
466 ret = vmw_stdu_destroy_st(dev_priv, stdu);
468 DRM_ERROR("Failed to destroy Screen Target\n");
470 stdu->content_fb_type = SAME_AS_DISPLAY;
475 * vmw_stdu_crtc_page_flip - Binds a buffer to a screen target
477 * @crtc: CRTC to attach FB to
479 * @event: Event to be posted. This event should've been alloced
480 * using k[mz]alloc, and should've been completely initialized.
481 * @page_flip_flags: Input flags.
483 * If the STDU uses the same display and content buffers, i.e. a true flip,
484 * this function will replace the existing display buffer with the new content
487 * If the STDU uses different display and content buffers, i.e. a blit, then
488 * only the content buffer will be updated.
491 * 0 on success, error code on failure
493 static int vmw_stdu_crtc_page_flip(struct drm_crtc *crtc,
494 struct drm_framebuffer *new_fb,
495 struct drm_pending_vblank_event *event,
497 struct drm_modeset_acquire_ctx *ctx)
500 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
501 struct vmw_screen_target_display_unit *stdu = vmw_crtc_to_stdu(crtc);
504 if (!stdu->defined || !vmw_kms_crtc_flippable(dev_priv, crtc))
507 ret = drm_atomic_helper_page_flip(crtc, new_fb, event, flags, ctx);
509 DRM_ERROR("Page flip error %d.\n", ret);
518 * vmw_stdu_bo_clip - Callback to encode a suface DMA command cliprect
520 * @dirty: The closure structure.
522 * Encodes a surface DMA command cliprect and updates the bounding box
525 static void vmw_stdu_bo_clip(struct vmw_kms_dirty *dirty)
527 struct vmw_stdu_dirty *ddirty =
528 container_of(dirty, struct vmw_stdu_dirty, base);
529 struct vmw_stdu_dma *cmd = dirty->cmd;
530 struct SVGA3dCopyBox *blit = (struct SVGA3dCopyBox *) &cmd[1];
532 blit += dirty->num_hits;
533 blit->srcx = dirty->fb_x;
534 blit->srcy = dirty->fb_y;
535 blit->x = dirty->unit_x1;
536 blit->y = dirty->unit_y1;
538 blit->w = dirty->unit_x2 - dirty->unit_x1;
539 blit->h = dirty->unit_y2 - dirty->unit_y1;
542 if (ddirty->transfer != SVGA3D_WRITE_HOST_VRAM)
545 /* Destination bounding box */
546 ddirty->left = min_t(s32, ddirty->left, dirty->unit_x1);
547 ddirty->top = min_t(s32, ddirty->top, dirty->unit_y1);
548 ddirty->right = max_t(s32, ddirty->right, dirty->unit_x2);
549 ddirty->bottom = max_t(s32, ddirty->bottom, dirty->unit_y2);
553 * vmw_stdu_bo_fifo_commit - Callback to fill in and submit a DMA command.
555 * @dirty: The closure structure.
557 * Fills in the missing fields in a DMA command, and optionally encodes
558 * a screen target update command, depending on transfer direction.
560 static void vmw_stdu_bo_fifo_commit(struct vmw_kms_dirty *dirty)
562 struct vmw_stdu_dirty *ddirty =
563 container_of(dirty, struct vmw_stdu_dirty, base);
564 struct vmw_screen_target_display_unit *stdu =
565 container_of(dirty->unit, typeof(*stdu), base);
566 struct vmw_stdu_dma *cmd = dirty->cmd;
567 struct SVGA3dCopyBox *blit = (struct SVGA3dCopyBox *) &cmd[1];
568 SVGA3dCmdSurfaceDMASuffix *suffix =
569 (SVGA3dCmdSurfaceDMASuffix *) &blit[dirty->num_hits];
570 size_t blit_size = sizeof(*blit) * dirty->num_hits + sizeof(*suffix);
572 if (!dirty->num_hits) {
573 vmw_fifo_commit(dirty->dev_priv, 0);
577 cmd->header.id = SVGA_3D_CMD_SURFACE_DMA;
578 cmd->header.size = sizeof(cmd->body) + blit_size;
579 vmw_bo_get_guest_ptr(&ddirty->buf->base, &cmd->body.guest.ptr);
580 cmd->body.guest.pitch = ddirty->pitch;
581 cmd->body.host.sid = stdu->display_srf->res.id;
582 cmd->body.host.face = 0;
583 cmd->body.host.mipmap = 0;
584 cmd->body.transfer = ddirty->transfer;
585 suffix->suffixSize = sizeof(*suffix);
586 suffix->maximumOffset = ddirty->buf->base.num_pages * PAGE_SIZE;
588 if (ddirty->transfer == SVGA3D_WRITE_HOST_VRAM) {
589 blit_size += sizeof(struct vmw_stdu_update);
591 vmw_stdu_populate_update(&suffix[1], stdu->base.unit,
592 ddirty->left, ddirty->right,
593 ddirty->top, ddirty->bottom);
596 vmw_fifo_commit(dirty->dev_priv, sizeof(*cmd) + blit_size);
598 ddirty->left = ddirty->top = S32_MAX;
599 ddirty->right = ddirty->bottom = S32_MIN;
604 * vmw_stdu_bo_cpu_clip - Callback to encode a CPU blit
606 * @dirty: The closure structure.
608 * This function calculates the bounding box for all the incoming clips.
610 static void vmw_stdu_bo_cpu_clip(struct vmw_kms_dirty *dirty)
612 struct vmw_stdu_dirty *ddirty =
613 container_of(dirty, struct vmw_stdu_dirty, base);
617 /* Calculate destination bounding box */
618 ddirty->left = min_t(s32, ddirty->left, dirty->unit_x1);
619 ddirty->top = min_t(s32, ddirty->top, dirty->unit_y1);
620 ddirty->right = max_t(s32, ddirty->right, dirty->unit_x2);
621 ddirty->bottom = max_t(s32, ddirty->bottom, dirty->unit_y2);
624 * Calculate content bounding box. We only need the top-left
625 * coordinate because width and height will be the same as the
626 * destination bounding box above
628 ddirty->fb_left = min_t(s32, ddirty->fb_left, dirty->fb_x);
629 ddirty->fb_top = min_t(s32, ddirty->fb_top, dirty->fb_y);
634 * vmw_stdu_bo_cpu_commit - Callback to do a CPU blit from buffer object
636 * @dirty: The closure structure.
638 * For the special case when we cannot create a proxy surface in a
639 * 2D VM, we have to do a CPU blit ourselves.
641 static void vmw_stdu_bo_cpu_commit(struct vmw_kms_dirty *dirty)
643 struct vmw_stdu_dirty *ddirty =
644 container_of(dirty, struct vmw_stdu_dirty, base);
645 struct vmw_screen_target_display_unit *stdu =
646 container_of(dirty->unit, typeof(*stdu), base);
648 s32 src_pitch, dst_pitch;
649 struct ttm_buffer_object *src_bo, *dst_bo;
650 u32 src_offset, dst_offset;
651 struct vmw_diff_cpy diff = VMW_CPU_BLIT_DIFF_INITIALIZER(stdu->cpp);
653 if (!dirty->num_hits)
656 width = ddirty->right - ddirty->left;
657 height = ddirty->bottom - ddirty->top;
659 if (width == 0 || height == 0)
662 /* Assume we are blitting from Guest (bo) to Host (display_srf) */
663 dst_pitch = stdu->display_srf->base_size.width * stdu->cpp;
664 dst_bo = &stdu->display_srf->res.backup->base;
665 dst_offset = ddirty->top * dst_pitch + ddirty->left * stdu->cpp;
667 src_pitch = ddirty->pitch;
668 src_bo = &ddirty->buf->base;
669 src_offset = ddirty->fb_top * src_pitch + ddirty->fb_left * stdu->cpp;
671 /* Swap src and dst if the assumption was wrong. */
672 if (ddirty->transfer != SVGA3D_WRITE_HOST_VRAM) {
673 swap(dst_pitch, src_pitch);
674 swap(dst_bo, src_bo);
675 swap(src_offset, dst_offset);
678 (void) vmw_bo_cpu_blit(dst_bo, dst_offset, dst_pitch,
679 src_bo, src_offset, src_pitch,
680 width * stdu->cpp, height, &diff);
682 if (ddirty->transfer == SVGA3D_WRITE_HOST_VRAM &&
683 drm_rect_visible(&diff.rect)) {
684 struct vmw_private *dev_priv;
685 struct vmw_stdu_update *cmd;
686 struct drm_clip_rect region;
689 /* We are updating the actual surface, not a proxy */
690 region.x1 = diff.rect.x1;
691 region.x2 = diff.rect.x2;
692 region.y1 = diff.rect.y1;
693 region.y2 = diff.rect.y2;
694 ret = vmw_kms_update_proxy(
695 (struct vmw_resource *) &stdu->display_srf->res,
696 (const struct drm_clip_rect *) ®ion, 1, 1);
701 dev_priv = vmw_priv(stdu->base.crtc.dev);
702 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd));
705 DRM_ERROR("Cannot reserve FIFO space to update STDU");
709 vmw_stdu_populate_update(cmd, stdu->base.unit,
710 region.x1, region.x2,
711 region.y1, region.y2);
713 vmw_fifo_commit(dev_priv, sizeof(*cmd));
717 ddirty->left = ddirty->top = ddirty->fb_left = ddirty->fb_top = S32_MAX;
718 ddirty->right = ddirty->bottom = S32_MIN;
722 * vmw_kms_stdu_dma - Perform a DMA transfer between a buffer-object backed
723 * framebuffer and the screen target system.
725 * @dev_priv: Pointer to the device private structure.
726 * @file_priv: Pointer to a struct drm-file identifying the caller. May be
727 * set to NULL, but then @user_fence_rep must also be set to NULL.
728 * @vfb: Pointer to the buffer-object backed framebuffer.
729 * @clips: Array of clip rects. Either @clips or @vclips must be NULL.
730 * @vclips: Alternate array of clip rects. Either @clips or @vclips must
732 * @num_clips: Number of clip rects in @clips or @vclips.
733 * @increment: Increment to use when looping over @clips or @vclips.
734 * @to_surface: Whether to DMA to the screen target system as opposed to
735 * from the screen target system.
736 * @interruptible: Whether to perform waits interruptible if possible.
737 * @crtc: If crtc is passed, perform stdu dma on that crtc only.
739 * If DMA-ing till the screen target system, the function will also notify
740 * the screen target system that a bounding box of the cliprects has been
742 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
745 int vmw_kms_stdu_dma(struct vmw_private *dev_priv,
746 struct drm_file *file_priv,
747 struct vmw_framebuffer *vfb,
748 struct drm_vmw_fence_rep __user *user_fence_rep,
749 struct drm_clip_rect *clips,
750 struct drm_vmw_rect *vclips,
755 struct drm_crtc *crtc)
757 struct vmw_buffer_object *buf =
758 container_of(vfb, struct vmw_framebuffer_bo, base)->buffer;
759 struct vmw_stdu_dirty ddirty;
761 bool cpu_blit = !(dev_priv->capabilities & SVGA_CAP_3D);
762 DECLARE_VAL_CONTEXT(val_ctx, NULL, 0);
765 * VMs without 3D support don't have the surface DMA command and
766 * we'll be using a CPU blit, and the framebuffer should be moved out
769 ret = vmw_validation_add_bo(&val_ctx, buf, false, cpu_blit);
773 ret = vmw_validation_prepare(&val_ctx, NULL, interruptible);
777 ddirty.transfer = (to_surface) ? SVGA3D_WRITE_HOST_VRAM :
778 SVGA3D_READ_HOST_VRAM;
779 ddirty.left = ddirty.top = S32_MAX;
780 ddirty.right = ddirty.bottom = S32_MIN;
781 ddirty.fb_left = ddirty.fb_top = S32_MAX;
782 ddirty.pitch = vfb->base.pitches[0];
784 ddirty.base.fifo_commit = vmw_stdu_bo_fifo_commit;
785 ddirty.base.clip = vmw_stdu_bo_clip;
786 ddirty.base.fifo_reserve_size = sizeof(struct vmw_stdu_dma) +
787 num_clips * sizeof(SVGA3dCopyBox) +
788 sizeof(SVGA3dCmdSurfaceDMASuffix);
790 ddirty.base.fifo_reserve_size += sizeof(struct vmw_stdu_update);
794 ddirty.base.fifo_commit = vmw_stdu_bo_cpu_commit;
795 ddirty.base.clip = vmw_stdu_bo_cpu_clip;
796 ddirty.base.fifo_reserve_size = 0;
799 ddirty.base.crtc = crtc;
801 ret = vmw_kms_helper_dirty(dev_priv, vfb, clips, vclips,
802 0, 0, num_clips, increment, &ddirty.base);
804 vmw_kms_helper_validation_finish(dev_priv, file_priv, &val_ctx, NULL,
809 vmw_validation_unref_lists(&val_ctx);
814 * vmw_stdu_surface_clip - Callback to encode a surface copy command cliprect
816 * @dirty: The closure structure.
818 * Encodes a surface copy command cliprect and updates the bounding box
821 static void vmw_kms_stdu_surface_clip(struct vmw_kms_dirty *dirty)
823 struct vmw_stdu_dirty *sdirty =
824 container_of(dirty, struct vmw_stdu_dirty, base);
825 struct vmw_stdu_surface_copy *cmd = dirty->cmd;
826 struct vmw_screen_target_display_unit *stdu =
827 container_of(dirty->unit, typeof(*stdu), base);
829 if (sdirty->sid != stdu->display_srf->res.id) {
830 struct SVGA3dCopyBox *blit = (struct SVGA3dCopyBox *) &cmd[1];
832 blit += dirty->num_hits;
833 blit->srcx = dirty->fb_x;
834 blit->srcy = dirty->fb_y;
835 blit->x = dirty->unit_x1;
836 blit->y = dirty->unit_y1;
838 blit->w = dirty->unit_x2 - dirty->unit_x1;
839 blit->h = dirty->unit_y2 - dirty->unit_y1;
844 /* Destination bounding box */
845 sdirty->left = min_t(s32, sdirty->left, dirty->unit_x1);
846 sdirty->top = min_t(s32, sdirty->top, dirty->unit_y1);
847 sdirty->right = max_t(s32, sdirty->right, dirty->unit_x2);
848 sdirty->bottom = max_t(s32, sdirty->bottom, dirty->unit_y2);
852 * vmw_stdu_surface_fifo_commit - Callback to fill in and submit a surface
855 * @dirty: The closure structure.
857 * Fills in the missing fields in a surface copy command, and encodes a screen
858 * target update command.
860 static void vmw_kms_stdu_surface_fifo_commit(struct vmw_kms_dirty *dirty)
862 struct vmw_stdu_dirty *sdirty =
863 container_of(dirty, struct vmw_stdu_dirty, base);
864 struct vmw_screen_target_display_unit *stdu =
865 container_of(dirty->unit, typeof(*stdu), base);
866 struct vmw_stdu_surface_copy *cmd = dirty->cmd;
867 struct vmw_stdu_update *update;
868 size_t blit_size = sizeof(SVGA3dCopyBox) * dirty->num_hits;
871 if (!dirty->num_hits) {
872 vmw_fifo_commit(dirty->dev_priv, 0);
876 if (sdirty->sid != stdu->display_srf->res.id) {
877 struct SVGA3dCopyBox *blit = (struct SVGA3dCopyBox *) &cmd[1];
879 cmd->header.id = SVGA_3D_CMD_SURFACE_COPY;
880 cmd->header.size = sizeof(cmd->body) + blit_size;
881 cmd->body.src.sid = sdirty->sid;
882 cmd->body.dest.sid = stdu->display_srf->res.id;
883 update = (struct vmw_stdu_update *) &blit[dirty->num_hits];
884 commit_size = sizeof(*cmd) + blit_size + sizeof(*update);
887 commit_size = sizeof(*update);
890 vmw_stdu_populate_update(update, stdu->base.unit, sdirty->left,
891 sdirty->right, sdirty->top, sdirty->bottom);
893 vmw_fifo_commit(dirty->dev_priv, commit_size);
895 sdirty->left = sdirty->top = S32_MAX;
896 sdirty->right = sdirty->bottom = S32_MIN;
900 * vmw_kms_stdu_surface_dirty - Dirty part of a surface backed framebuffer
902 * @dev_priv: Pointer to the device private structure.
903 * @framebuffer: Pointer to the surface-buffer backed framebuffer.
904 * @clips: Array of clip rects. Either @clips or @vclips must be NULL.
905 * @vclips: Alternate array of clip rects. Either @clips or @vclips must
907 * @srf: Pointer to surface to blit from. If NULL, the surface attached
908 * to @framebuffer will be used.
909 * @dest_x: X coordinate offset to align @srf with framebuffer coordinates.
910 * @dest_y: Y coordinate offset to align @srf with framebuffer coordinates.
911 * @num_clips: Number of clip rects in @clips.
912 * @inc: Increment to use when looping over @clips.
913 * @out_fence: If non-NULL, will return a ref-counted pointer to a
914 * struct vmw_fence_obj. The returned fence pointer may be NULL in which
915 * case the device has already synchronized.
916 * @crtc: If crtc is passed, perform surface dirty on that crtc only.
918 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
921 int vmw_kms_stdu_surface_dirty(struct vmw_private *dev_priv,
922 struct vmw_framebuffer *framebuffer,
923 struct drm_clip_rect *clips,
924 struct drm_vmw_rect *vclips,
925 struct vmw_resource *srf,
928 unsigned num_clips, int inc,
929 struct vmw_fence_obj **out_fence,
930 struct drm_crtc *crtc)
932 struct vmw_framebuffer_surface *vfbs =
933 container_of(framebuffer, typeof(*vfbs), base);
934 struct vmw_stdu_dirty sdirty;
935 DECLARE_VAL_CONTEXT(val_ctx, NULL, 0);
939 srf = &vfbs->surface->res;
941 ret = vmw_validation_add_resource(&val_ctx, srf, 0, NULL, NULL);
945 ret = vmw_validation_prepare(&val_ctx, &dev_priv->cmdbuf_mutex, true);
949 if (vfbs->is_bo_proxy) {
950 ret = vmw_kms_update_proxy(srf, clips, num_clips, inc);
955 sdirty.base.fifo_commit = vmw_kms_stdu_surface_fifo_commit;
956 sdirty.base.clip = vmw_kms_stdu_surface_clip;
957 sdirty.base.fifo_reserve_size = sizeof(struct vmw_stdu_surface_copy) +
958 sizeof(SVGA3dCopyBox) * num_clips +
959 sizeof(struct vmw_stdu_update);
960 sdirty.base.crtc = crtc;
961 sdirty.sid = srf->id;
962 sdirty.left = sdirty.top = S32_MAX;
963 sdirty.right = sdirty.bottom = S32_MIN;
965 ret = vmw_kms_helper_dirty(dev_priv, framebuffer, clips, vclips,
966 dest_x, dest_y, num_clips, inc,
969 vmw_kms_helper_validation_finish(dev_priv, NULL, &val_ctx, out_fence,
975 vmw_validation_unref_lists(&val_ctx);
981 * Screen Target CRTC dispatch table
983 static const struct drm_crtc_funcs vmw_stdu_crtc_funcs = {
984 .gamma_set = vmw_du_crtc_gamma_set,
985 .destroy = vmw_stdu_crtc_destroy,
986 .reset = vmw_du_crtc_reset,
987 .atomic_duplicate_state = vmw_du_crtc_duplicate_state,
988 .atomic_destroy_state = vmw_du_crtc_destroy_state,
989 .set_config = vmw_kms_set_config,
990 .page_flip = vmw_stdu_crtc_page_flip,
995 /******************************************************************************
996 * Screen Target Display Unit Encoder Functions
997 *****************************************************************************/
1000 * vmw_stdu_encoder_destroy - cleans up the STDU
1002 * @encoder: used the get the containing STDU
1004 * vmwgfx cleans up crtc/encoder/connector all at the same time so technically
1005 * this can be a no-op. Nevertheless, it doesn't hurt of have this in case
1006 * the common KMS code changes and somehow vmw_stdu_crtc_destroy() doesn't
1009 static void vmw_stdu_encoder_destroy(struct drm_encoder *encoder)
1011 vmw_stdu_destroy(vmw_encoder_to_stdu(encoder));
1014 static const struct drm_encoder_funcs vmw_stdu_encoder_funcs = {
1015 .destroy = vmw_stdu_encoder_destroy,
1020 /******************************************************************************
1021 * Screen Target Display Unit Connector Functions
1022 *****************************************************************************/
1025 * vmw_stdu_connector_destroy - cleans up the STDU
1027 * @connector: used to get the containing STDU
1029 * vmwgfx cleans up crtc/encoder/connector all at the same time so technically
1030 * this can be a no-op. Nevertheless, it doesn't hurt of have this in case
1031 * the common KMS code changes and somehow vmw_stdu_crtc_destroy() doesn't
1034 static void vmw_stdu_connector_destroy(struct drm_connector *connector)
1036 vmw_stdu_destroy(vmw_connector_to_stdu(connector));
1041 static const struct drm_connector_funcs vmw_stdu_connector_funcs = {
1042 .dpms = vmw_du_connector_dpms,
1043 .detect = vmw_du_connector_detect,
1044 .fill_modes = vmw_du_connector_fill_modes,
1045 .set_property = vmw_du_connector_set_property,
1046 .destroy = vmw_stdu_connector_destroy,
1047 .reset = vmw_du_connector_reset,
1048 .atomic_duplicate_state = vmw_du_connector_duplicate_state,
1049 .atomic_destroy_state = vmw_du_connector_destroy_state,
1050 .atomic_set_property = vmw_du_connector_atomic_set_property,
1051 .atomic_get_property = vmw_du_connector_atomic_get_property,
1056 drm_connector_helper_funcs vmw_stdu_connector_helper_funcs = {
1061 /******************************************************************************
1062 * Screen Target Display Plane Functions
1063 *****************************************************************************/
1068 * vmw_stdu_primary_plane_cleanup_fb - Unpins the display surface
1070 * @plane: display plane
1071 * @old_state: Contains the FB to clean up
1073 * Unpins the display surface
1075 * Returns 0 on success
1078 vmw_stdu_primary_plane_cleanup_fb(struct drm_plane *plane,
1079 struct drm_plane_state *old_state)
1081 struct vmw_plane_state *vps = vmw_plane_state_to_vps(old_state);
1084 WARN_ON(!vps->pinned);
1086 vmw_du_plane_cleanup_fb(plane, old_state);
1088 vps->content_fb_type = SAME_AS_DISPLAY;
1095 * vmw_stdu_primary_plane_prepare_fb - Readies the display surface
1097 * @plane: display plane
1098 * @new_state: info on the new plane state, including the FB
1100 * This function allocates a new display surface if the content is
1101 * backed by a buffer object. The display surface is pinned here, and it'll
1102 * be unpinned in .cleanup_fb()
1104 * Returns 0 on success
1107 vmw_stdu_primary_plane_prepare_fb(struct drm_plane *plane,
1108 struct drm_plane_state *new_state)
1110 struct vmw_private *dev_priv = vmw_priv(plane->dev);
1111 struct drm_framebuffer *new_fb = new_state->fb;
1112 struct vmw_framebuffer *vfb;
1113 struct vmw_plane_state *vps = vmw_plane_state_to_vps(new_state);
1114 enum stdu_content_type new_content_type;
1115 struct vmw_framebuffer_surface *new_vfbs;
1116 struct drm_crtc *crtc = new_state->crtc;
1117 uint32_t hdisplay = new_state->crtc_w, vdisplay = new_state->crtc_h;
1120 /* No FB to prepare */
1123 WARN_ON(vps->pinned != 0);
1124 vmw_surface_unreference(&vps->surf);
1130 vfb = vmw_framebuffer_to_vfb(new_fb);
1131 new_vfbs = (vfb->bo) ? NULL : vmw_framebuffer_to_vfbs(new_fb);
1133 if (new_vfbs && new_vfbs->surface->base_size.width == hdisplay &&
1134 new_vfbs->surface->base_size.height == vdisplay)
1135 new_content_type = SAME_AS_DISPLAY;
1137 new_content_type = SEPARATE_BO;
1139 new_content_type = SEPARATE_SURFACE;
1141 if (new_content_type != SAME_AS_DISPLAY) {
1142 struct vmw_surface content_srf;
1143 struct drm_vmw_size display_base_size = {0};
1145 display_base_size.width = hdisplay;
1146 display_base_size.height = vdisplay;
1147 display_base_size.depth = 1;
1150 * If content buffer is a buffer object, then we have to
1151 * construct surface info
1153 if (new_content_type == SEPARATE_BO) {
1155 switch (new_fb->format->cpp[0]*8) {
1157 content_srf.format = SVGA3D_X8R8G8B8;
1161 content_srf.format = SVGA3D_R5G6B5;
1165 content_srf.format = SVGA3D_P8;
1169 DRM_ERROR("Invalid format\n");
1173 content_srf.flags = 0;
1174 content_srf.mip_levels[0] = 1;
1175 content_srf.multisample_count = 0;
1176 content_srf.multisample_pattern =
1177 SVGA3D_MS_PATTERN_NONE;
1178 content_srf.quality_level = SVGA3D_MS_QUALITY_NONE;
1180 content_srf = *new_vfbs->surface;
1184 struct drm_vmw_size cur_base_size = vps->surf->base_size;
1186 if (cur_base_size.width != display_base_size.width ||
1187 cur_base_size.height != display_base_size.height ||
1188 vps->surf->format != content_srf.format) {
1189 WARN_ON(vps->pinned != 0);
1190 vmw_surface_unreference(&vps->surf);
1196 ret = vmw_surface_gb_priv_define
1198 /* Kernel visible only */
1202 true, /* a scanout buffer */
1203 content_srf.mip_levels[0],
1204 content_srf.multisample_count,
1207 content_srf.multisample_pattern,
1208 content_srf.quality_level,
1211 DRM_ERROR("Couldn't allocate STDU surface.\n");
1217 * prepare_fb and clean_fb should only take care of pinning
1218 * and unpinning. References are tracked by state objects.
1219 * The only time we add a reference in prepare_fb is if the
1220 * state object doesn't have a reference to begin with
1223 WARN_ON(vps->pinned != 0);
1224 vmw_surface_unreference(&vps->surf);
1227 vps->surf = vmw_surface_reference(new_vfbs->surface);
1232 /* Pin new surface before flipping */
1233 ret = vmw_resource_pin(&vps->surf->res, false);
1240 vps->content_fb_type = new_content_type;
1243 * This should only happen if the buffer object is too large to create a
1244 * proxy surface for.
1245 * If we are a 2D VM with a buffer object then we have to use CPU blit
1246 * so cache these mappings
1248 if (vps->content_fb_type == SEPARATE_BO &&
1249 !(dev_priv->capabilities & SVGA_CAP_3D))
1250 vps->cpp = new_fb->pitches[0] / new_fb->width;
1255 vmw_surface_unreference(&vps->surf);
1262 * vmw_stdu_primary_plane_atomic_update - formally switches STDU to new plane
1264 * @plane: display plane
1265 * @old_state: Only used to get crtc info
1267 * Formally update stdu->display_srf to the new plane, and bind the new
1268 * plane STDU. This function is called during the commit phase when
1269 * all the preparation have been done and all the configurations have
1273 vmw_stdu_primary_plane_atomic_update(struct drm_plane *plane,
1274 struct drm_plane_state *old_state)
1276 struct vmw_plane_state *vps = vmw_plane_state_to_vps(plane->state);
1277 struct drm_crtc *crtc = plane->state->crtc;
1278 struct vmw_screen_target_display_unit *stdu;
1279 struct drm_pending_vblank_event *event;
1280 struct vmw_private *dev_priv;
1284 * We cannot really fail this function, so if we do, then output an
1285 * error and maintain consistent atomic state.
1287 if (crtc && plane->state->fb) {
1288 struct vmw_framebuffer *vfb =
1289 vmw_framebuffer_to_vfb(plane->state->fb);
1290 struct drm_vmw_rect vclips;
1291 stdu = vmw_crtc_to_stdu(crtc);
1292 dev_priv = vmw_priv(crtc->dev);
1294 stdu->display_srf = vps->surf;
1295 stdu->content_fb_type = vps->content_fb_type;
1296 stdu->cpp = vps->cpp;
1300 vclips.w = crtc->mode.hdisplay;
1301 vclips.h = crtc->mode.vdisplay;
1303 ret = vmw_stdu_bind_st(dev_priv, stdu, &stdu->display_srf->res);
1305 DRM_ERROR("Failed to bind surface to STDU.\n");
1308 ret = vmw_kms_stdu_dma(dev_priv, NULL, vfb, NULL, NULL,
1309 &vclips, 1, 1, true, false,
1312 ret = vmw_kms_stdu_surface_dirty(dev_priv, vfb, NULL,
1313 &vclips, NULL, 0, 0,
1316 DRM_ERROR("Failed to update STDU.\n");
1318 crtc = old_state->crtc;
1319 stdu = vmw_crtc_to_stdu(crtc);
1320 dev_priv = vmw_priv(crtc->dev);
1323 * When disabling a plane, CRTC and FB should always be NULL
1324 * together, otherwise it's an error.
1325 * Here primary plane is being disable so blank the screen
1326 * target display unit, if not already done.
1331 ret = vmw_stdu_bind_st(dev_priv, stdu, NULL);
1333 DRM_ERROR("Failed to blank STDU\n");
1335 ret = vmw_stdu_update_st(dev_priv, stdu);
1337 DRM_ERROR("Failed to update STDU.\n");
1342 event = crtc->state->event;
1344 * In case of failure and other cases, vblank event will be sent in
1345 * vmw_du_crtc_atomic_flush.
1347 if (event && (ret == 0)) {
1348 struct vmw_fence_obj *fence = NULL;
1349 struct drm_file *file_priv = event->base.file_priv;
1351 vmw_execbuf_fence_commands(NULL, dev_priv, &fence, NULL);
1354 * If fence is NULL, then already sync.
1357 ret = vmw_event_fence_action_queue(
1358 file_priv, fence, &event->base,
1359 &event->event.vbl.tv_sec,
1360 &event->event.vbl.tv_usec,
1363 DRM_ERROR("Failed to queue event on fence.\n");
1365 crtc->state->event = NULL;
1367 vmw_fence_obj_unreference(&fence);
1370 (void) vmw_fifo_flush(dev_priv, false);
1375 static const struct drm_plane_funcs vmw_stdu_plane_funcs = {
1376 .update_plane = drm_atomic_helper_update_plane,
1377 .disable_plane = drm_atomic_helper_disable_plane,
1378 .destroy = vmw_du_primary_plane_destroy,
1379 .reset = vmw_du_plane_reset,
1380 .atomic_duplicate_state = vmw_du_plane_duplicate_state,
1381 .atomic_destroy_state = vmw_du_plane_destroy_state,
1384 static const struct drm_plane_funcs vmw_stdu_cursor_funcs = {
1385 .update_plane = drm_atomic_helper_update_plane,
1386 .disable_plane = drm_atomic_helper_disable_plane,
1387 .destroy = vmw_du_cursor_plane_destroy,
1388 .reset = vmw_du_plane_reset,
1389 .atomic_duplicate_state = vmw_du_plane_duplicate_state,
1390 .atomic_destroy_state = vmw_du_plane_destroy_state,
1398 drm_plane_helper_funcs vmw_stdu_cursor_plane_helper_funcs = {
1399 .atomic_check = vmw_du_cursor_plane_atomic_check,
1400 .atomic_update = vmw_du_cursor_plane_atomic_update,
1401 .prepare_fb = vmw_du_cursor_plane_prepare_fb,
1402 .cleanup_fb = vmw_du_plane_cleanup_fb,
1406 drm_plane_helper_funcs vmw_stdu_primary_plane_helper_funcs = {
1407 .atomic_check = vmw_du_primary_plane_atomic_check,
1408 .atomic_update = vmw_stdu_primary_plane_atomic_update,
1409 .prepare_fb = vmw_stdu_primary_plane_prepare_fb,
1410 .cleanup_fb = vmw_stdu_primary_plane_cleanup_fb,
1413 static const struct drm_crtc_helper_funcs vmw_stdu_crtc_helper_funcs = {
1414 .prepare = vmw_stdu_crtc_helper_prepare,
1415 .mode_set_nofb = vmw_stdu_crtc_mode_set_nofb,
1416 .atomic_check = vmw_du_crtc_atomic_check,
1417 .atomic_begin = vmw_du_crtc_atomic_begin,
1418 .atomic_flush = vmw_du_crtc_atomic_flush,
1419 .atomic_enable = vmw_stdu_crtc_atomic_enable,
1420 .atomic_disable = vmw_stdu_crtc_atomic_disable,
1425 * vmw_stdu_init - Sets up a Screen Target Display Unit
1427 * @dev_priv: VMW DRM device
1428 * @unit: unit number range from 0 to VMWGFX_NUM_DISPLAY_UNITS
1430 * This function is called once per CRTC, and allocates one Screen Target
1431 * display unit to represent that CRTC. Since the SVGA device does not separate
1432 * out encoder and connector, they are represented as part of the STDU as well.
1434 static int vmw_stdu_init(struct vmw_private *dev_priv, unsigned unit)
1436 struct vmw_screen_target_display_unit *stdu;
1437 struct drm_device *dev = dev_priv->dev;
1438 struct drm_connector *connector;
1439 struct drm_encoder *encoder;
1440 struct drm_plane *primary, *cursor;
1441 struct drm_crtc *crtc;
1445 stdu = kzalloc(sizeof(*stdu), GFP_KERNEL);
1449 stdu->base.unit = unit;
1450 crtc = &stdu->base.crtc;
1451 encoder = &stdu->base.encoder;
1452 connector = &stdu->base.connector;
1453 primary = &stdu->base.primary;
1454 cursor = &stdu->base.cursor;
1456 stdu->base.pref_active = (unit == 0);
1457 stdu->base.pref_width = dev_priv->initial_width;
1458 stdu->base.pref_height = dev_priv->initial_height;
1461 * Remove this after enabling atomic because property values can
1462 * only exist in a state object
1464 stdu->base.is_implicit = false;
1466 /* Initialize primary plane */
1467 vmw_du_plane_reset(primary);
1469 ret = drm_universal_plane_init(dev, primary,
1470 0, &vmw_stdu_plane_funcs,
1471 vmw_primary_plane_formats,
1472 ARRAY_SIZE(vmw_primary_plane_formats),
1473 NULL, DRM_PLANE_TYPE_PRIMARY, NULL);
1475 DRM_ERROR("Failed to initialize primary plane");
1479 drm_plane_helper_add(primary, &vmw_stdu_primary_plane_helper_funcs);
1481 /* Initialize cursor plane */
1482 vmw_du_plane_reset(cursor);
1484 ret = drm_universal_plane_init(dev, cursor,
1485 0, &vmw_stdu_cursor_funcs,
1486 vmw_cursor_plane_formats,
1487 ARRAY_SIZE(vmw_cursor_plane_formats),
1488 NULL, DRM_PLANE_TYPE_CURSOR, NULL);
1490 DRM_ERROR("Failed to initialize cursor plane");
1491 drm_plane_cleanup(&stdu->base.primary);
1495 drm_plane_helper_add(cursor, &vmw_stdu_cursor_plane_helper_funcs);
1497 vmw_du_connector_reset(connector);
1499 ret = drm_connector_init(dev, connector, &vmw_stdu_connector_funcs,
1500 DRM_MODE_CONNECTOR_VIRTUAL);
1502 DRM_ERROR("Failed to initialize connector\n");
1506 drm_connector_helper_add(connector, &vmw_stdu_connector_helper_funcs);
1507 connector->status = vmw_du_connector_detect(connector, false);
1508 vmw_connector_state_to_vcs(connector->state)->is_implicit = false;
1510 ret = drm_encoder_init(dev, encoder, &vmw_stdu_encoder_funcs,
1511 DRM_MODE_ENCODER_VIRTUAL, NULL);
1513 DRM_ERROR("Failed to initialize encoder\n");
1514 goto err_free_connector;
1517 (void) drm_connector_attach_encoder(connector, encoder);
1518 encoder->possible_crtcs = (1 << unit);
1519 encoder->possible_clones = 0;
1521 ret = drm_connector_register(connector);
1523 DRM_ERROR("Failed to register connector\n");
1524 goto err_free_encoder;
1527 vmw_du_crtc_reset(crtc);
1528 ret = drm_crtc_init_with_planes(dev, crtc, &stdu->base.primary,
1530 &vmw_stdu_crtc_funcs, NULL);
1532 DRM_ERROR("Failed to initialize CRTC\n");
1533 goto err_free_unregister;
1536 drm_crtc_helper_add(crtc, &vmw_stdu_crtc_helper_funcs);
1538 drm_mode_crtc_set_gamma_size(crtc, 256);
1540 drm_object_attach_property(&connector->base,
1541 dev_priv->hotplug_mode_update_property, 1);
1542 drm_object_attach_property(&connector->base,
1543 dev->mode_config.suggested_x_property, 0);
1544 drm_object_attach_property(&connector->base,
1545 dev->mode_config.suggested_y_property, 0);
1546 if (dev_priv->implicit_placement_property)
1547 drm_object_attach_property
1549 dev_priv->implicit_placement_property,
1550 stdu->base.is_implicit);
1553 err_free_unregister:
1554 drm_connector_unregister(connector);
1556 drm_encoder_cleanup(encoder);
1558 drm_connector_cleanup(connector);
1567 * vmw_stdu_destroy - Cleans up a vmw_screen_target_display_unit
1569 * @stdu: Screen Target Display Unit to be destroyed
1571 * Clean up after vmw_stdu_init
1573 static void vmw_stdu_destroy(struct vmw_screen_target_display_unit *stdu)
1575 vmw_du_cleanup(&stdu->base);
1581 /******************************************************************************
1582 * Screen Target Display KMS Functions
1584 * These functions are called by the common KMS code in vmwgfx_kms.c
1585 *****************************************************************************/
1588 * vmw_kms_stdu_init_display - Initializes a Screen Target based display
1590 * @dev_priv: VMW DRM device
1592 * This function initialize a Screen Target based display device. It checks
1593 * the capability bits to make sure the underlying hardware can support
1594 * screen targets, and then creates the maximum number of CRTCs, a.k.a Display
1595 * Units, as supported by the display hardware.
1598 * 0 on success, error code otherwise
1600 int vmw_kms_stdu_init_display(struct vmw_private *dev_priv)
1602 struct drm_device *dev = dev_priv->dev;
1606 /* Do nothing if Screen Target support is turned off */
1607 if (!VMWGFX_ENABLE_SCREEN_TARGET_OTABLE)
1610 if (!(dev_priv->capabilities & SVGA_CAP_GBOBJECTS))
1613 ret = drm_vblank_init(dev, VMWGFX_NUM_DISPLAY_UNITS);
1614 if (unlikely(ret != 0))
1617 dev_priv->active_display_unit = vmw_du_screen_target;
1619 vmw_kms_create_implicit_placement_property(dev_priv, false);
1621 for (i = 0; i < VMWGFX_NUM_DISPLAY_UNITS; ++i) {
1622 ret = vmw_stdu_init(dev_priv, i);
1624 if (unlikely(ret != 0)) {
1625 DRM_ERROR("Failed to initialize STDU %d", i);
1630 DRM_INFO("Screen Target Display device initialized\n");