1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /******************************************************************************
4 * Copyright (c) 2014-2024 Broadcom. All Rights Reserved. The term
5 * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
22 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
23 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
25 * USE OR OTHER DEALINGS IN THE SOFTWARE.
27 ******************************************************************************/
29 #include "vmwgfx_bo.h"
30 #include "vmwgfx_kms.h"
31 #include "vmwgfx_vkms.h"
32 #include "vmw_surface_cache.h"
33 #include <linux/fsnotify.h>
35 #include <drm/drm_atomic.h>
36 #include <drm/drm_atomic_helper.h>
37 #include <drm/drm_damage_helper.h>
38 #include <drm/drm_fourcc.h>
39 #include <drm/drm_vblank.h>
41 #define vmw_crtc_to_stdu(x) \
42 container_of(x, struct vmw_screen_target_display_unit, base.crtc)
43 #define vmw_encoder_to_stdu(x) \
44 container_of(x, struct vmw_screen_target_display_unit, base.encoder)
45 #define vmw_connector_to_stdu(x) \
46 container_of(x, struct vmw_screen_target_display_unit, base.connector)
49 * Some renderers such as llvmpipe will align the width and height of their
50 * buffers to match their tile size. We need to keep this in mind when exposing
51 * modes to userspace so that this possible over-allocation will not exceed
52 * graphics memory. 64x64 pixels seems to be a reasonable upper bound for the
53 * tile size of current renderers.
55 #define GPU_TILE_SIZE 64
57 enum stdu_content_type {
64 * struct vmw_stdu_dirty - closure structure for the update functions
66 * @base: The base type we derive from. Used by vmw_kms_helper_dirty().
67 * @left: Left side of bounding box.
68 * @right: Right side of bounding box.
69 * @top: Top side of bounding box.
70 * @bottom: Bottom side of bounding box.
71 * @fb_left: Left side of the framebuffer/content bounding box
72 * @fb_top: Top of the framebuffer/content bounding box
73 * @pitch: framebuffer pitch (stride)
74 * @buf: buffer object when DMA-ing between buffer and screen targets.
75 * @sid: Surface ID when copying between surface and screen targets.
77 struct vmw_stdu_dirty {
78 struct vmw_kms_dirty base;
79 s32 left, right, top, bottom;
89 * SVGA commands that are used by this code. Please see the device headers
92 struct vmw_stdu_update {
93 SVGA3dCmdHeader header;
94 SVGA3dCmdUpdateGBScreenTarget body;
97 struct vmw_stdu_surface_copy {
98 SVGA3dCmdHeader header;
99 SVGA3dCmdSurfaceCopy body;
102 struct vmw_stdu_update_gb_image {
103 SVGA3dCmdHeader header;
104 SVGA3dCmdUpdateGBImage body;
108 * struct vmw_screen_target_display_unit - conglomerated STDU structure
110 * @base: VMW specific DU structure
111 * @display_srf: surface to be displayed. The dimension of this will always
112 * match the display mode. If the display mode matches
113 * content_vfbs dimensions, then this is a pointer into the
114 * corresponding field in content_vfbs. If not, then this
115 * is a separate buffer to which content_vfbs will blit to.
116 * @content_fb_type: content_fb type
117 * @display_width: display width
118 * @display_height: display height
119 * @defined: true if the current display unit has been initialized
120 * @cpp: Bytes per pixel
122 struct vmw_screen_target_display_unit {
123 struct vmw_display_unit base;
124 struct vmw_surface *display_srf;
125 enum stdu_content_type content_fb_type;
126 s32 display_width, display_height;
136 static void vmw_stdu_destroy(struct vmw_screen_target_display_unit *stdu);
140 /******************************************************************************
141 * Screen Target Display Unit CRTC Functions
142 *****************************************************************************/
145 * vmw_stdu_crtc_destroy - cleans up the STDU
147 * @crtc: used to get a reference to the containing STDU
149 static void vmw_stdu_crtc_destroy(struct drm_crtc *crtc)
151 vmw_stdu_destroy(vmw_crtc_to_stdu(crtc));
155 * vmw_stdu_define_st - Defines a Screen Target
157 * @dev_priv: VMW DRM device
158 * @stdu: display unit to create a Screen Target for
159 * @mode: The mode to set.
160 * @crtc_x: X coordinate of screen target relative to framebuffer origin.
161 * @crtc_y: Y coordinate of screen target relative to framebuffer origin.
163 * Creates a STDU that we can used later. This function is called whenever the
164 * framebuffer size changes.
167 * 0 on success, error code on failure
169 static int vmw_stdu_define_st(struct vmw_private *dev_priv,
170 struct vmw_screen_target_display_unit *stdu,
171 struct drm_display_mode *mode,
172 int crtc_x, int crtc_y)
175 SVGA3dCmdHeader header;
176 SVGA3dCmdDefineGBScreenTarget body;
179 cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd));
180 if (unlikely(cmd == NULL))
183 cmd->header.id = SVGA_3D_CMD_DEFINE_GB_SCREENTARGET;
184 cmd->header.size = sizeof(cmd->body);
186 cmd->body.stid = stdu->base.unit;
187 cmd->body.width = mode->hdisplay;
188 cmd->body.height = mode->vdisplay;
189 cmd->body.flags = (0 == cmd->body.stid) ? SVGA_STFLAG_PRIMARY : 0;
191 cmd->body.xRoot = crtc_x;
192 cmd->body.yRoot = crtc_y;
194 stdu->base.set_gui_x = cmd->body.xRoot;
195 stdu->base.set_gui_y = cmd->body.yRoot;
197 vmw_cmd_commit(dev_priv, sizeof(*cmd));
199 stdu->defined = true;
200 stdu->display_width = mode->hdisplay;
201 stdu->display_height = mode->vdisplay;
209 * vmw_stdu_bind_st - Binds a surface to a Screen Target
211 * @dev_priv: VMW DRM device
212 * @stdu: display unit affected
213 * @res: Buffer to bind to the screen target. Set to NULL to blank screen.
215 * Binding a surface to a Screen Target the same as flipping
217 * Returns: %0 on success or -errno code on failure
219 static int vmw_stdu_bind_st(struct vmw_private *dev_priv,
220 struct vmw_screen_target_display_unit *stdu,
221 const struct vmw_resource *res)
223 SVGA3dSurfaceImageId image;
226 SVGA3dCmdHeader header;
227 SVGA3dCmdBindGBScreenTarget body;
231 if (!stdu->defined) {
232 DRM_ERROR("No screen target defined\n");
236 /* Set up image using information in vfb */
237 memset(&image, 0, sizeof(image));
238 image.sid = res ? res->id : SVGA3D_INVALID_ID;
240 cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd));
241 if (unlikely(cmd == NULL))
244 cmd->header.id = SVGA_3D_CMD_BIND_GB_SCREENTARGET;
245 cmd->header.size = sizeof(cmd->body);
247 cmd->body.stid = stdu->base.unit;
248 cmd->body.image = image;
250 vmw_cmd_commit(dev_priv, sizeof(*cmd));
256 * vmw_stdu_populate_update - populate an UPDATE_GB_SCREENTARGET command with a
259 * @cmd: Pointer to command stream.
260 * @unit: Screen target unit.
261 * @left: Left side of bounding box.
262 * @right: Right side of bounding box.
263 * @top: Top side of bounding box.
264 * @bottom: Bottom side of bounding box.
266 static void vmw_stdu_populate_update(void *cmd, int unit,
267 s32 left, s32 right, s32 top, s32 bottom)
269 struct vmw_stdu_update *update = cmd;
271 update->header.id = SVGA_3D_CMD_UPDATE_GB_SCREENTARGET;
272 update->header.size = sizeof(update->body);
274 update->body.stid = unit;
275 update->body.rect.x = left;
276 update->body.rect.y = top;
277 update->body.rect.w = right - left;
278 update->body.rect.h = bottom - top;
282 * vmw_stdu_update_st - Full update of a Screen Target
284 * @dev_priv: VMW DRM device
285 * @stdu: display unit affected
287 * This function needs to be called whenever the content of a screen
288 * target has changed completely. Typically as a result of a backing
292 * 0 on success, error code on failure
294 static int vmw_stdu_update_st(struct vmw_private *dev_priv,
295 struct vmw_screen_target_display_unit *stdu)
297 struct vmw_stdu_update *cmd;
299 if (!stdu->defined) {
300 DRM_ERROR("No screen target defined");
304 cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd));
305 if (unlikely(cmd == NULL))
308 vmw_stdu_populate_update(cmd, stdu->base.unit,
309 0, stdu->display_width,
310 0, stdu->display_height);
312 vmw_cmd_commit(dev_priv, sizeof(*cmd));
320 * vmw_stdu_destroy_st - Destroy a Screen Target
322 * @dev_priv: VMW DRM device
323 * @stdu: display unit to destroy
325 * Returns: %0 on success, negative error code on failure. -ERESTARTSYS if
328 static int vmw_stdu_destroy_st(struct vmw_private *dev_priv,
329 struct vmw_screen_target_display_unit *stdu)
334 SVGA3dCmdHeader header;
335 SVGA3dCmdDestroyGBScreenTarget body;
339 /* Nothing to do if not successfully defined */
340 if (unlikely(!stdu->defined))
343 cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd));
344 if (unlikely(cmd == NULL))
347 cmd->header.id = SVGA_3D_CMD_DESTROY_GB_SCREENTARGET;
348 cmd->header.size = sizeof(cmd->body);
350 cmd->body.stid = stdu->base.unit;
352 vmw_cmd_commit(dev_priv, sizeof(*cmd));
355 ret = vmw_fallback_wait(dev_priv, false, true, 0, false, 3*HZ);
356 if (unlikely(ret != 0))
357 DRM_ERROR("Failed to sync with HW");
359 stdu->defined = false;
360 stdu->display_width = 0;
361 stdu->display_height = 0;
368 * vmw_stdu_crtc_mode_set_nofb - Updates screen target size
370 * @crtc: CRTC associated with the screen target
372 * This function defines/destroys a screen target
375 static void vmw_stdu_crtc_mode_set_nofb(struct drm_crtc *crtc)
377 struct vmw_private *dev_priv;
378 struct vmw_screen_target_display_unit *stdu;
379 struct drm_connector_state *conn_state;
380 struct vmw_connector_state *vmw_conn_state;
383 stdu = vmw_crtc_to_stdu(crtc);
384 dev_priv = vmw_priv(crtc->dev);
385 conn_state = stdu->base.connector.state;
386 vmw_conn_state = vmw_connector_state_to_vcs(conn_state);
389 ret = vmw_stdu_bind_st(dev_priv, stdu, NULL);
391 DRM_ERROR("Failed to blank CRTC\n");
393 (void) vmw_stdu_update_st(dev_priv, stdu);
395 ret = vmw_stdu_destroy_st(dev_priv, stdu);
397 DRM_ERROR("Failed to destroy Screen Target\n");
399 stdu->content_fb_type = SAME_AS_DISPLAY;
402 if (!crtc->state->enable)
405 x = vmw_conn_state->gui_x;
406 y = vmw_conn_state->gui_y;
408 vmw_svga_enable(dev_priv);
409 ret = vmw_stdu_define_st(dev_priv, stdu, &crtc->mode, x, y);
412 DRM_ERROR("Failed to define Screen Target of size %dx%d\n",
416 static void vmw_stdu_crtc_atomic_disable(struct drm_crtc *crtc,
417 struct drm_atomic_state *state)
419 struct vmw_private *dev_priv;
420 struct vmw_screen_target_display_unit *stdu;
421 struct drm_crtc_state *new_crtc_state;
425 DRM_ERROR("CRTC is NULL\n");
429 stdu = vmw_crtc_to_stdu(crtc);
430 dev_priv = vmw_priv(crtc->dev);
431 new_crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
433 if (dev_priv->vkms_enabled)
434 drm_crtc_vblank_off(crtc);
437 ret = vmw_stdu_bind_st(dev_priv, stdu, NULL);
439 DRM_ERROR("Failed to blank CRTC\n");
441 (void) vmw_stdu_update_st(dev_priv, stdu);
443 /* Don't destroy the Screen Target if we are only setting the
444 * display as inactive
446 if (new_crtc_state->enable &&
447 !new_crtc_state->active &&
448 !new_crtc_state->mode_changed)
451 ret = vmw_stdu_destroy_st(dev_priv, stdu);
453 DRM_ERROR("Failed to destroy Screen Target\n");
455 stdu->content_fb_type = SAME_AS_DISPLAY;
460 * vmw_stdu_bo_cpu_clip - Callback to encode a CPU blit
462 * @dirty: The closure structure.
464 * This function calculates the bounding box for all the incoming clips.
466 static void vmw_stdu_bo_cpu_clip(struct vmw_kms_dirty *dirty)
468 struct vmw_stdu_dirty *ddirty =
469 container_of(dirty, struct vmw_stdu_dirty, base);
473 /* Calculate destination bounding box */
474 ddirty->left = min_t(s32, ddirty->left, dirty->unit_x1);
475 ddirty->top = min_t(s32, ddirty->top, dirty->unit_y1);
476 ddirty->right = max_t(s32, ddirty->right, dirty->unit_x2);
477 ddirty->bottom = max_t(s32, ddirty->bottom, dirty->unit_y2);
480 * Calculate content bounding box. We only need the top-left
481 * coordinate because width and height will be the same as the
482 * destination bounding box above
484 ddirty->fb_left = min_t(s32, ddirty->fb_left, dirty->fb_x);
485 ddirty->fb_top = min_t(s32, ddirty->fb_top, dirty->fb_y);
490 * vmw_stdu_bo_cpu_commit - Callback to do a CPU blit from buffer object
492 * @dirty: The closure structure.
494 * For the special case when we cannot create a proxy surface in a
495 * 2D VM, we have to do a CPU blit ourselves.
497 static void vmw_stdu_bo_cpu_commit(struct vmw_kms_dirty *dirty)
499 struct vmw_stdu_dirty *ddirty =
500 container_of(dirty, struct vmw_stdu_dirty, base);
501 struct vmw_screen_target_display_unit *stdu =
502 container_of(dirty->unit, typeof(*stdu), base);
504 s32 src_pitch, dst_pitch;
505 struct vmw_bo *src_bo, *dst_bo;
506 u32 src_offset, dst_offset;
507 struct vmw_diff_cpy diff = VMW_CPU_BLIT_DIFF_INITIALIZER(stdu->cpp);
509 if (!dirty->num_hits)
512 width = ddirty->right - ddirty->left;
513 height = ddirty->bottom - ddirty->top;
515 if (width == 0 || height == 0)
518 /* Assume we are blitting from Guest (bo) to Host (display_srf) */
519 src_pitch = stdu->display_srf->metadata.base_size.width * stdu->cpp;
520 src_bo = stdu->display_srf->res.guest_memory_bo;
521 src_offset = ddirty->top * src_pitch + ddirty->left * stdu->cpp;
523 dst_pitch = ddirty->pitch;
524 dst_bo = ddirty->buf;
525 dst_offset = ddirty->fb_top * dst_pitch + ddirty->fb_left * stdu->cpp;
527 (void) vmw_bo_cpu_blit(dst_bo, dst_offset, dst_pitch,
528 src_bo, src_offset, src_pitch,
529 width * stdu->cpp, height, &diff);
533 * vmw_kms_stdu_readback - Perform a readback from a buffer-object backed
534 * framebuffer and the screen target system.
536 * @dev_priv: Pointer to the device private structure.
537 * @file_priv: Pointer to a struct drm-file identifying the caller. May be
538 * set to NULL, but then @user_fence_rep must also be set to NULL.
539 * @vfb: Pointer to the buffer-object backed framebuffer.
540 * @user_fence_rep: User-space provided structure for fence information.
541 * @clips: Array of clip rects. Either @clips or @vclips must be NULL.
542 * @vclips: Alternate array of clip rects. Either @clips or @vclips must
544 * @num_clips: Number of clip rects in @clips or @vclips.
545 * @increment: Increment to use when looping over @clips or @vclips.
546 * @crtc: If crtc is passed, perform stdu dma on that crtc only.
548 * If DMA-ing till the screen target system, the function will also notify
549 * the screen target system that a bounding box of the cliprects has been
552 * Returns: %0 on success, negative error code on failure. -ERESTARTSYS if
555 int vmw_kms_stdu_readback(struct vmw_private *dev_priv,
556 struct drm_file *file_priv,
557 struct vmw_framebuffer *vfb,
558 struct drm_vmw_fence_rep __user *user_fence_rep,
559 struct drm_clip_rect *clips,
560 struct drm_vmw_rect *vclips,
563 struct drm_crtc *crtc)
566 container_of(vfb, struct vmw_framebuffer_bo, base)->buffer;
567 struct vmw_stdu_dirty ddirty;
569 DECLARE_VAL_CONTEXT(val_ctx, NULL, 0);
572 * The GMR domain might seem confusing because it might seem like it should
573 * never happen with screen targets but e.g. the xorg vmware driver issues
574 * CMD_SURFACE_DMA for various pixmap updates which might transition our bo to
575 * a GMR. Instead of forcing another transition we can optimize the readback
576 * by reading directly from the GMR.
578 vmw_bo_placement_set(buf,
579 VMW_BO_DOMAIN_MOB | VMW_BO_DOMAIN_SYS | VMW_BO_DOMAIN_GMR,
580 VMW_BO_DOMAIN_MOB | VMW_BO_DOMAIN_SYS | VMW_BO_DOMAIN_GMR);
581 ret = vmw_validation_add_bo(&val_ctx, buf);
585 ret = vmw_validation_prepare(&val_ctx, NULL, true);
589 ddirty.left = ddirty.top = S32_MAX;
590 ddirty.right = ddirty.bottom = S32_MIN;
591 ddirty.fb_left = ddirty.fb_top = S32_MAX;
592 ddirty.pitch = vfb->base.pitches[0];
595 ddirty.base.fifo_commit = vmw_stdu_bo_cpu_commit;
596 ddirty.base.clip = vmw_stdu_bo_cpu_clip;
597 ddirty.base.fifo_reserve_size = 0;
599 ddirty.base.crtc = crtc;
601 ret = vmw_kms_helper_dirty(dev_priv, vfb, clips, vclips,
602 0, 0, num_clips, increment, &ddirty.base);
604 vmw_kms_helper_validation_finish(dev_priv, file_priv, &val_ctx, NULL,
609 vmw_validation_unref_lists(&val_ctx);
614 * vmw_kms_stdu_surface_clip - Callback to encode a surface copy command cliprect
616 * @dirty: The closure structure.
618 * Encodes a surface copy command cliprect and updates the bounding box
621 static void vmw_kms_stdu_surface_clip(struct vmw_kms_dirty *dirty)
623 struct vmw_stdu_dirty *sdirty =
624 container_of(dirty, struct vmw_stdu_dirty, base);
625 struct vmw_stdu_surface_copy *cmd = dirty->cmd;
626 struct vmw_screen_target_display_unit *stdu =
627 container_of(dirty->unit, typeof(*stdu), base);
629 if (sdirty->sid != stdu->display_srf->res.id) {
630 struct SVGA3dCopyBox *blit = (struct SVGA3dCopyBox *) &cmd[1];
632 blit += dirty->num_hits;
633 blit->srcx = dirty->fb_x;
634 blit->srcy = dirty->fb_y;
635 blit->x = dirty->unit_x1;
636 blit->y = dirty->unit_y1;
638 blit->w = dirty->unit_x2 - dirty->unit_x1;
639 blit->h = dirty->unit_y2 - dirty->unit_y1;
644 /* Destination bounding box */
645 sdirty->left = min_t(s32, sdirty->left, dirty->unit_x1);
646 sdirty->top = min_t(s32, sdirty->top, dirty->unit_y1);
647 sdirty->right = max_t(s32, sdirty->right, dirty->unit_x2);
648 sdirty->bottom = max_t(s32, sdirty->bottom, dirty->unit_y2);
652 * vmw_kms_stdu_surface_fifo_commit - Callback to fill in and submit a surface
655 * @dirty: The closure structure.
657 * Fills in the missing fields in a surface copy command, and encodes a screen
658 * target update command.
660 static void vmw_kms_stdu_surface_fifo_commit(struct vmw_kms_dirty *dirty)
662 struct vmw_stdu_dirty *sdirty =
663 container_of(dirty, struct vmw_stdu_dirty, base);
664 struct vmw_screen_target_display_unit *stdu =
665 container_of(dirty->unit, typeof(*stdu), base);
666 struct vmw_stdu_surface_copy *cmd = dirty->cmd;
667 struct vmw_stdu_update *update;
668 size_t blit_size = sizeof(SVGA3dCopyBox) * dirty->num_hits;
671 if (!dirty->num_hits) {
672 vmw_cmd_commit(dirty->dev_priv, 0);
676 if (sdirty->sid != stdu->display_srf->res.id) {
677 struct SVGA3dCopyBox *blit = (struct SVGA3dCopyBox *) &cmd[1];
679 cmd->header.id = SVGA_3D_CMD_SURFACE_COPY;
680 cmd->header.size = sizeof(cmd->body) + blit_size;
681 cmd->body.src.sid = sdirty->sid;
682 cmd->body.dest.sid = stdu->display_srf->res.id;
683 update = (struct vmw_stdu_update *) &blit[dirty->num_hits];
684 commit_size = sizeof(*cmd) + blit_size + sizeof(*update);
685 stdu->display_srf->res.res_dirty = true;
688 commit_size = sizeof(*update);
691 vmw_stdu_populate_update(update, stdu->base.unit, sdirty->left,
692 sdirty->right, sdirty->top, sdirty->bottom);
694 vmw_cmd_commit(dirty->dev_priv, commit_size);
696 sdirty->left = sdirty->top = S32_MAX;
697 sdirty->right = sdirty->bottom = S32_MIN;
701 * vmw_kms_stdu_surface_dirty - Dirty part of a surface backed framebuffer
703 * @dev_priv: Pointer to the device private structure.
704 * @framebuffer: Pointer to the surface-buffer backed framebuffer.
705 * @clips: Array of clip rects. Either @clips or @vclips must be NULL.
706 * @vclips: Alternate array of clip rects. Either @clips or @vclips must
708 * @srf: Pointer to surface to blit from. If NULL, the surface attached
709 * to @framebuffer will be used.
710 * @dest_x: X coordinate offset to align @srf with framebuffer coordinates.
711 * @dest_y: Y coordinate offset to align @srf with framebuffer coordinates.
712 * @num_clips: Number of clip rects in @clips.
713 * @inc: Increment to use when looping over @clips.
714 * @out_fence: If non-NULL, will return a ref-counted pointer to a
715 * struct vmw_fence_obj. The returned fence pointer may be NULL in which
716 * case the device has already synchronized.
717 * @crtc: If crtc is passed, perform surface dirty on that crtc only.
719 * Returns: %0 on success, negative error code on failure. -ERESTARTSYS if
722 int vmw_kms_stdu_surface_dirty(struct vmw_private *dev_priv,
723 struct vmw_framebuffer *framebuffer,
724 struct drm_clip_rect *clips,
725 struct drm_vmw_rect *vclips,
726 struct vmw_resource *srf,
729 unsigned num_clips, int inc,
730 struct vmw_fence_obj **out_fence,
731 struct drm_crtc *crtc)
733 struct vmw_framebuffer_surface *vfbs =
734 container_of(framebuffer, typeof(*vfbs), base);
735 struct vmw_stdu_dirty sdirty;
736 DECLARE_VAL_CONTEXT(val_ctx, NULL, 0);
740 srf = &vmw_user_object_surface(&vfbs->uo)->res;
742 ret = vmw_validation_add_resource(&val_ctx, srf, 0, VMW_RES_DIRTY_NONE,
747 ret = vmw_validation_prepare(&val_ctx, &dev_priv->cmdbuf_mutex, true);
751 sdirty.base.fifo_commit = vmw_kms_stdu_surface_fifo_commit;
752 sdirty.base.clip = vmw_kms_stdu_surface_clip;
753 sdirty.base.fifo_reserve_size = sizeof(struct vmw_stdu_surface_copy) +
754 sizeof(SVGA3dCopyBox) * num_clips +
755 sizeof(struct vmw_stdu_update);
756 sdirty.base.crtc = crtc;
757 sdirty.sid = srf->id;
758 sdirty.left = sdirty.top = S32_MAX;
759 sdirty.right = sdirty.bottom = S32_MIN;
761 ret = vmw_kms_helper_dirty(dev_priv, framebuffer, clips, vclips,
762 dest_x, dest_y, num_clips, inc,
765 vmw_kms_helper_validation_finish(dev_priv, NULL, &val_ctx, out_fence,
771 vmw_validation_unref_lists(&val_ctx);
776 * Screen Target CRTC dispatch table
778 static const struct drm_crtc_funcs vmw_stdu_crtc_funcs = {
779 .gamma_set = vmw_du_crtc_gamma_set,
780 .destroy = vmw_stdu_crtc_destroy,
781 .reset = vmw_du_crtc_reset,
782 .atomic_duplicate_state = vmw_du_crtc_duplicate_state,
783 .atomic_destroy_state = vmw_du_crtc_destroy_state,
784 .set_config = drm_atomic_helper_set_config,
785 .page_flip = drm_atomic_helper_page_flip,
786 .enable_vblank = vmw_vkms_enable_vblank,
787 .disable_vblank = vmw_vkms_disable_vblank,
788 .get_vblank_timestamp = vmw_vkms_get_vblank_timestamp,
789 .get_crc_sources = vmw_vkms_get_crc_sources,
790 .set_crc_source = vmw_vkms_set_crc_source,
791 .verify_crc_source = vmw_vkms_verify_crc_source,
796 /******************************************************************************
797 * Screen Target Display Unit Encoder Functions
798 *****************************************************************************/
801 * vmw_stdu_encoder_destroy - cleans up the STDU
803 * @encoder: used the get the containing STDU
805 * vmwgfx cleans up crtc/encoder/connector all at the same time so technically
806 * this can be a no-op. Nevertheless, it doesn't hurt of have this in case
807 * the common KMS code changes and somehow vmw_stdu_crtc_destroy() doesn't
810 static void vmw_stdu_encoder_destroy(struct drm_encoder *encoder)
812 vmw_stdu_destroy(vmw_encoder_to_stdu(encoder));
815 static const struct drm_encoder_funcs vmw_stdu_encoder_funcs = {
816 .destroy = vmw_stdu_encoder_destroy,
821 /******************************************************************************
822 * Screen Target Display Unit Connector Functions
823 *****************************************************************************/
826 * vmw_stdu_connector_destroy - cleans up the STDU
828 * @connector: used to get the containing STDU
830 * vmwgfx cleans up crtc/encoder/connector all at the same time so technically
831 * this can be a no-op. Nevertheless, it doesn't hurt of have this in case
832 * the common KMS code changes and somehow vmw_stdu_crtc_destroy() doesn't
835 static void vmw_stdu_connector_destroy(struct drm_connector *connector)
837 vmw_stdu_destroy(vmw_connector_to_stdu(connector));
840 static enum drm_mode_status
841 vmw_stdu_connector_mode_valid(struct drm_connector *connector,
842 struct drm_display_mode *mode)
844 enum drm_mode_status ret;
845 struct drm_device *dev = connector->dev;
846 struct vmw_private *dev_priv = vmw_priv(dev);
847 u64 assumed_cpp = dev_priv->assume_16bpp ? 2 : 4;
848 /* Align width and height to account for GPU tile over-alignment */
849 u64 required_mem = ALIGN(mode->hdisplay, GPU_TILE_SIZE) *
850 ALIGN(mode->vdisplay, GPU_TILE_SIZE) *
852 required_mem = ALIGN(required_mem, PAGE_SIZE);
854 ret = drm_mode_validate_size(mode, dev_priv->stdu_max_width,
855 dev_priv->stdu_max_height);
859 ret = drm_mode_validate_size(mode, dev_priv->texture_max_width,
860 dev_priv->texture_max_height);
864 if (required_mem > dev_priv->max_primary_mem)
867 if (required_mem > dev_priv->max_mob_pages * PAGE_SIZE)
870 if (required_mem > dev_priv->max_mob_size)
877 * Trigger a modeset if the X,Y position of the Screen Target changes.
878 * This is needed when multi-mon is cycled. The original Screen Target will have
879 * the same mode but its relative X,Y position in the topology will change.
881 static int vmw_stdu_connector_atomic_check(struct drm_connector *conn,
882 struct drm_atomic_state *state)
884 struct drm_connector_state *conn_state;
885 struct vmw_screen_target_display_unit *du;
886 struct drm_crtc_state *new_crtc_state;
888 conn_state = drm_atomic_get_connector_state(state, conn);
889 du = vmw_connector_to_stdu(conn);
891 if (!conn_state->crtc)
894 new_crtc_state = drm_atomic_get_new_crtc_state(state, conn_state->crtc);
895 if (du->base.gui_x != du->base.set_gui_x ||
896 du->base.gui_y != du->base.set_gui_y)
897 new_crtc_state->mode_changed = true;
902 static const struct drm_connector_funcs vmw_stdu_connector_funcs = {
903 .dpms = vmw_du_connector_dpms,
904 .detect = vmw_du_connector_detect,
905 .fill_modes = drm_helper_probe_single_connector_modes,
906 .destroy = vmw_stdu_connector_destroy,
907 .reset = vmw_du_connector_reset,
908 .atomic_duplicate_state = vmw_du_connector_duplicate_state,
909 .atomic_destroy_state = vmw_du_connector_destroy_state,
914 drm_connector_helper_funcs vmw_stdu_connector_helper_funcs = {
915 .get_modes = vmw_connector_get_modes,
916 .mode_valid = vmw_stdu_connector_mode_valid,
917 .atomic_check = vmw_stdu_connector_atomic_check,
922 /******************************************************************************
923 * Screen Target Display Plane Functions
924 *****************************************************************************/
929 * vmw_stdu_primary_plane_cleanup_fb - Unpins the display surface
931 * @plane: display plane
932 * @old_state: Contains the FB to clean up
934 * Unpins the display surface
936 * Returns 0 on success
939 vmw_stdu_primary_plane_cleanup_fb(struct drm_plane *plane,
940 struct drm_plane_state *old_state)
942 struct vmw_plane_state *vps = vmw_plane_state_to_vps(old_state);
944 if (vmw_user_object_surface(&vps->uo))
945 WARN_ON(!vps->pinned);
946 vmw_du_plane_cleanup_fb(plane, old_state);
948 vps->content_fb_type = SAME_AS_DISPLAY;
954 * vmw_stdu_primary_plane_prepare_fb - Readies the display surface
956 * @plane: display plane
957 * @new_state: info on the new plane state, including the FB
959 * This function allocates a new display surface if the content is
960 * backed by a buffer object. The display surface is pinned here, and it'll
961 * be unpinned in .cleanup_fb()
963 * Returns: %0 on success
966 vmw_stdu_primary_plane_prepare_fb(struct drm_plane *plane,
967 struct drm_plane_state *new_state)
969 struct vmw_private *dev_priv = vmw_priv(plane->dev);
970 struct drm_framebuffer *new_fb = new_state->fb;
971 struct vmw_framebuffer *vfb;
972 struct vmw_plane_state *vps = vmw_plane_state_to_vps(new_state);
973 enum stdu_content_type new_content_type;
974 struct vmw_framebuffer_surface *new_vfbs;
975 uint32_t hdisplay = new_state->crtc_w, vdisplay = new_state->crtc_h;
976 struct drm_plane_state *old_state = plane->state;
977 struct drm_rect rect;
980 /* No FB to prepare */
982 if (vmw_user_object_surface(&vps->uo)) {
983 WARN_ON(vps->pinned != 0);
984 vmw_user_object_unref(&vps->uo);
990 vfb = vmw_framebuffer_to_vfb(new_fb);
991 new_vfbs = (vfb->bo) ? NULL : vmw_framebuffer_to_vfbs(new_fb);
994 vmw_user_object_surface(&new_vfbs->uo)->metadata.base_size.width == hdisplay &&
995 vmw_user_object_surface(&new_vfbs->uo)->metadata.base_size.height == vdisplay)
996 new_content_type = SAME_AS_DISPLAY;
998 new_content_type = SEPARATE_BO;
1000 new_content_type = SEPARATE_SURFACE;
1002 if (new_content_type != SAME_AS_DISPLAY) {
1003 struct vmw_surface_metadata metadata = {0};
1006 * If content buffer is a buffer object, then we have to
1007 * construct surface info
1009 if (new_content_type == SEPARATE_BO) {
1011 switch (new_fb->format->cpp[0]*8) {
1013 metadata.format = SVGA3D_X8R8G8B8;
1017 metadata.format = SVGA3D_R5G6B5;
1021 metadata.format = SVGA3D_P8;
1025 DRM_ERROR("Invalid format\n");
1029 metadata.mip_levels[0] = 1;
1030 metadata.num_sizes = 1;
1031 metadata.scanout = true;
1033 metadata = vmw_user_object_surface(&new_vfbs->uo)->metadata;
1036 metadata.base_size.width = hdisplay;
1037 metadata.base_size.height = vdisplay;
1038 metadata.base_size.depth = 1;
1040 if (vmw_user_object_surface(&vps->uo)) {
1041 struct drm_vmw_size cur_base_size =
1042 vmw_user_object_surface(&vps->uo)->metadata.base_size;
1044 if (cur_base_size.width != metadata.base_size.width ||
1045 cur_base_size.height != metadata.base_size.height ||
1046 vmw_user_object_surface(&vps->uo)->metadata.format != metadata.format) {
1047 WARN_ON(vps->pinned != 0);
1048 vmw_user_object_unref(&vps->uo);
1053 if (!vmw_user_object_surface(&vps->uo)) {
1054 ret = vmw_gb_surface_define(dev_priv, &metadata,
1057 DRM_ERROR("Couldn't allocate STDU surface.\n");
1063 * prepare_fb and clean_fb should only take care of pinning
1064 * and unpinning. References are tracked by state objects.
1065 * The only time we add a reference in prepare_fb is if the
1066 * state object doesn't have a reference to begin with
1068 if (vmw_user_object_surface(&vps->uo)) {
1069 WARN_ON(vps->pinned != 0);
1070 vmw_user_object_unref(&vps->uo);
1073 memcpy(&vps->uo, &new_vfbs->uo, sizeof(vps->uo));
1074 vmw_user_object_ref(&vps->uo);
1077 if (vmw_user_object_surface(&vps->uo)) {
1079 /* Pin new surface before flipping */
1080 ret = vmw_resource_pin(&vmw_user_object_surface(&vps->uo)->res, false);
1087 vps->content_fb_type = new_content_type;
1090 * The drm fb code will do blit's via the vmap interface, which doesn't
1091 * trigger vmw_bo page dirty tracking due to being kernel side (and thus
1092 * doesn't require mmap'ing) so we have to update the surface's dirty
1093 * regions by hand but we want to be careful to not overwrite the
1094 * resource if it has been written to by the gpu (res_dirty).
1096 if (vps->uo.buffer && vps->uo.buffer->is_dumb) {
1097 struct vmw_surface *surf = vmw_user_object_surface(&vps->uo);
1098 struct vmw_resource *res = &surf->res;
1100 if (!res->res_dirty && drm_atomic_helper_damage_merged(old_state,
1104 * At some point it might be useful to actually translate
1105 * (rect.x1, rect.y1) => start, and (rect.x2, rect.y2) => end,
1106 * but currently the fb code will just report the entire fb
1107 * dirty so in practice it doesn't matter.
1109 pgoff_t start = res->guest_memory_offset >> PAGE_SHIFT;
1110 pgoff_t end = __KERNEL_DIV_ROUND_UP(res->guest_memory_offset +
1111 res->guest_memory_size,
1113 vmw_resource_dirty_update(res, start, end);
1118 * This should only happen if the buffer object is too large to create a
1119 * proxy surface for.
1121 if (vps->content_fb_type == SEPARATE_BO)
1122 vps->cpp = new_fb->pitches[0] / new_fb->width;
1127 vmw_user_object_unref(&vps->uo);
1131 static uint32_t vmw_stdu_bo_fifo_size_cpu(struct vmw_du_update_plane *update,
1134 return sizeof(struct vmw_stdu_update_gb_image) +
1135 sizeof(struct vmw_stdu_update);
1138 static uint32_t vmw_stdu_bo_pre_clip_cpu(struct vmw_du_update_plane *update,
1139 void *cmd, uint32_t num_hits)
1141 struct vmw_du_update_plane_buffer *bo_update =
1142 container_of(update, typeof(*bo_update), base);
1144 bo_update->fb_left = INT_MAX;
1145 bo_update->fb_top = INT_MAX;
1150 static uint32_t vmw_stdu_bo_clip_cpu(struct vmw_du_update_plane *update,
1151 void *cmd, struct drm_rect *clip,
1152 uint32_t fb_x, uint32_t fb_y)
1154 struct vmw_du_update_plane_buffer *bo_update =
1155 container_of(update, typeof(*bo_update), base);
1157 bo_update->fb_left = min_t(int, bo_update->fb_left, fb_x);
1158 bo_update->fb_top = min_t(int, bo_update->fb_top, fb_y);
1164 vmw_stdu_bo_populate_update_cpu(struct vmw_du_update_plane *update, void *cmd,
1165 struct drm_rect *bb)
1167 struct vmw_du_update_plane_buffer *bo_update;
1168 struct vmw_screen_target_display_unit *stdu;
1169 struct vmw_framebuffer_bo *vfbbo;
1170 struct vmw_diff_cpy diff = VMW_CPU_BLIT_DIFF_INITIALIZER(0);
1171 struct vmw_stdu_update_gb_image *cmd_img = cmd;
1172 struct vmw_stdu_update *cmd_update;
1173 struct vmw_bo *src_bo, *dst_bo;
1174 u32 src_offset, dst_offset;
1175 s32 src_pitch, dst_pitch;
1178 bo_update = container_of(update, typeof(*bo_update), base);
1179 stdu = container_of(update->du, typeof(*stdu), base);
1180 vfbbo = container_of(update->vfb, typeof(*vfbbo), base);
1182 width = bb->x2 - bb->x1;
1183 height = bb->y2 - bb->y1;
1185 diff.cpp = stdu->cpp;
1187 dst_bo = stdu->display_srf->res.guest_memory_bo;
1188 dst_pitch = stdu->display_srf->metadata.base_size.width * stdu->cpp;
1189 dst_offset = bb->y1 * dst_pitch + bb->x1 * stdu->cpp;
1191 src_bo = vfbbo->buffer;
1192 src_pitch = update->vfb->base.pitches[0];
1193 src_offset = bo_update->fb_top * src_pitch + bo_update->fb_left *
1196 (void) vmw_bo_cpu_blit(dst_bo, dst_offset, dst_pitch, src_bo,
1197 src_offset, src_pitch, width * stdu->cpp, height,
1200 if (drm_rect_visible(&diff.rect)) {
1201 SVGA3dBox *box = &cmd_img->body.box;
1203 cmd_img->header.id = SVGA_3D_CMD_UPDATE_GB_IMAGE;
1204 cmd_img->header.size = sizeof(cmd_img->body);
1205 cmd_img->body.image.sid = stdu->display_srf->res.id;
1206 cmd_img->body.image.face = 0;
1207 cmd_img->body.image.mipmap = 0;
1209 box->x = diff.rect.x1;
1210 box->y = diff.rect.y1;
1212 box->w = drm_rect_width(&diff.rect);
1213 box->h = drm_rect_height(&diff.rect);
1216 cmd_update = (struct vmw_stdu_update *)&cmd_img[1];
1217 vmw_stdu_populate_update(cmd_update, stdu->base.unit,
1218 diff.rect.x1, diff.rect.x2,
1219 diff.rect.y1, diff.rect.y2);
1221 return sizeof(*cmd_img) + sizeof(*cmd_update);
1228 * vmw_stdu_plane_update_bo - Update display unit for bo backed fb.
1229 * @dev_priv: device private.
1230 * @plane: plane state.
1231 * @old_state: old plane state.
1232 * @vfb: framebuffer which is blitted to display unit.
1233 * @out_fence: If non-NULL, will return a ref-counted pointer to vmw_fence_obj.
1234 * The returned fence pointer may be NULL in which case the device
1235 * has already synchronized.
1237 * Return: 0 on success or a negative error code on failure.
1239 static int vmw_stdu_plane_update_bo(struct vmw_private *dev_priv,
1240 struct drm_plane *plane,
1241 struct drm_plane_state *old_state,
1242 struct vmw_framebuffer *vfb,
1243 struct vmw_fence_obj **out_fence)
1245 struct vmw_du_update_plane_buffer bo_update;
1247 memset(&bo_update, 0, sizeof(struct vmw_du_update_plane_buffer));
1248 bo_update.base.plane = plane;
1249 bo_update.base.old_state = old_state;
1250 bo_update.base.dev_priv = dev_priv;
1251 bo_update.base.du = vmw_crtc_to_du(plane->state->crtc);
1252 bo_update.base.vfb = vfb;
1253 bo_update.base.out_fence = out_fence;
1254 bo_update.base.mutex = NULL;
1255 bo_update.base.intr = false;
1257 bo_update.base.calc_fifo_size = vmw_stdu_bo_fifo_size_cpu;
1258 bo_update.base.pre_clip = vmw_stdu_bo_pre_clip_cpu;
1259 bo_update.base.clip = vmw_stdu_bo_clip_cpu;
1260 bo_update.base.post_clip = vmw_stdu_bo_populate_update_cpu;
1262 return vmw_du_helper_plane_update(&bo_update.base);
1266 vmw_stdu_surface_fifo_size_same_display(struct vmw_du_update_plane *update,
1271 size += sizeof(struct vmw_stdu_update);
1276 static uint32_t vmw_stdu_surface_fifo_size(struct vmw_du_update_plane *update,
1281 size += sizeof(struct vmw_stdu_surface_copy) + sizeof(SVGA3dCopyBox) *
1282 num_hits + sizeof(struct vmw_stdu_update);
1288 vmw_stdu_surface_populate_copy(struct vmw_du_update_plane *update, void *cmd,
1291 struct vmw_screen_target_display_unit *stdu;
1292 struct vmw_framebuffer_surface *vfbs;
1293 struct vmw_stdu_surface_copy *cmd_copy = cmd;
1295 stdu = container_of(update->du, typeof(*stdu), base);
1296 vfbs = container_of(update->vfb, typeof(*vfbs), base);
1298 cmd_copy->header.id = SVGA_3D_CMD_SURFACE_COPY;
1299 cmd_copy->header.size = sizeof(cmd_copy->body) + sizeof(SVGA3dCopyBox) *
1301 cmd_copy->body.src.sid = vmw_user_object_surface(&vfbs->uo)->res.id;
1302 cmd_copy->body.dest.sid = stdu->display_srf->res.id;
1304 return sizeof(*cmd_copy);
1308 vmw_stdu_surface_populate_clip(struct vmw_du_update_plane *update, void *cmd,
1309 struct drm_rect *clip, uint32_t fb_x,
1312 struct SVGA3dCopyBox *box = cmd;
1320 box->w = drm_rect_width(clip);
1321 box->h = drm_rect_height(clip);
1324 return sizeof(*box);
1328 vmw_stdu_surface_populate_update(struct vmw_du_update_plane *update, void *cmd,
1329 struct drm_rect *bb)
1331 vmw_stdu_populate_update(cmd, update->du->unit, bb->x1, bb->x2, bb->y1,
1334 return sizeof(struct vmw_stdu_update);
1338 * vmw_stdu_plane_update_surface - Update display unit for surface backed fb
1339 * @dev_priv: Device private
1340 * @plane: Plane state
1341 * @old_state: Old plane state
1342 * @vfb: Framebuffer which is blitted to display unit
1343 * @out_fence: If non-NULL, will return a ref-counted pointer to vmw_fence_obj.
1344 * The returned fence pointer may be NULL in which case the device
1345 * has already synchronized.
1347 * Return: 0 on success or a negative error code on failure.
1349 static int vmw_stdu_plane_update_surface(struct vmw_private *dev_priv,
1350 struct drm_plane *plane,
1351 struct drm_plane_state *old_state,
1352 struct vmw_framebuffer *vfb,
1353 struct vmw_fence_obj **out_fence)
1355 struct vmw_du_update_plane srf_update;
1356 struct vmw_screen_target_display_unit *stdu;
1357 struct vmw_framebuffer_surface *vfbs;
1359 stdu = vmw_crtc_to_stdu(plane->state->crtc);
1360 vfbs = container_of(vfb, typeof(*vfbs), base);
1362 memset(&srf_update, 0, sizeof(struct vmw_du_update_plane));
1363 srf_update.plane = plane;
1364 srf_update.old_state = old_state;
1365 srf_update.dev_priv = dev_priv;
1366 srf_update.du = vmw_crtc_to_du(plane->state->crtc);
1367 srf_update.vfb = vfb;
1368 srf_update.out_fence = out_fence;
1369 srf_update.mutex = &dev_priv->cmdbuf_mutex;
1370 srf_update.intr = true;
1372 if (vmw_user_object_surface(&vfbs->uo)->res.id != stdu->display_srf->res.id) {
1373 srf_update.calc_fifo_size = vmw_stdu_surface_fifo_size;
1374 srf_update.pre_clip = vmw_stdu_surface_populate_copy;
1375 srf_update.clip = vmw_stdu_surface_populate_clip;
1377 srf_update.calc_fifo_size =
1378 vmw_stdu_surface_fifo_size_same_display;
1381 srf_update.post_clip = vmw_stdu_surface_populate_update;
1383 return vmw_du_helper_plane_update(&srf_update);
1387 * vmw_stdu_primary_plane_atomic_update - formally switches STDU to new plane
1388 * @plane: display plane
1389 * @state: Only used to get crtc info
1391 * Formally update stdu->display_srf to the new plane, and bind the new
1392 * plane STDU. This function is called during the commit phase when
1393 * all the preparation have been done and all the configurations have
1397 vmw_stdu_primary_plane_atomic_update(struct drm_plane *plane,
1398 struct drm_atomic_state *state)
1400 struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state, plane);
1401 struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state, plane);
1402 struct vmw_plane_state *vps = vmw_plane_state_to_vps(new_state);
1403 struct drm_crtc *crtc = new_state->crtc;
1404 struct vmw_screen_target_display_unit *stdu;
1405 struct vmw_fence_obj *fence = NULL;
1406 struct vmw_private *dev_priv;
1409 /* If case of device error, maintain consistent atomic state */
1410 if (crtc && new_state->fb) {
1411 struct vmw_framebuffer *vfb =
1412 vmw_framebuffer_to_vfb(new_state->fb);
1413 stdu = vmw_crtc_to_stdu(crtc);
1414 dev_priv = vmw_priv(crtc->dev);
1416 stdu->display_srf = vmw_user_object_surface(&vps->uo);
1417 stdu->content_fb_type = vps->content_fb_type;
1418 stdu->cpp = vps->cpp;
1420 ret = vmw_stdu_bind_st(dev_priv, stdu, &stdu->display_srf->res);
1422 DRM_ERROR("Failed to bind surface to STDU.\n");
1425 ret = vmw_stdu_plane_update_bo(dev_priv, plane,
1426 old_state, vfb, &fence);
1428 ret = vmw_stdu_plane_update_surface(dev_priv, plane,
1432 DRM_ERROR("Failed to update STDU.\n");
1434 crtc = old_state->crtc;
1435 stdu = vmw_crtc_to_stdu(crtc);
1436 dev_priv = vmw_priv(crtc->dev);
1438 /* Blank STDU when fb and crtc are NULL */
1442 ret = vmw_stdu_bind_st(dev_priv, stdu, NULL);
1444 DRM_ERROR("Failed to blank STDU\n");
1446 ret = vmw_stdu_update_st(dev_priv, stdu);
1448 DRM_ERROR("Failed to update STDU.\n");
1454 vmw_fence_obj_unreference(&fence);
1458 vmw_stdu_crtc_atomic_flush(struct drm_crtc *crtc,
1459 struct drm_atomic_state *state)
1461 struct vmw_private *vmw = vmw_priv(crtc->dev);
1462 struct vmw_screen_target_display_unit *stdu = vmw_crtc_to_stdu(crtc);
1464 if (vmw->vkms_enabled)
1465 vmw_vkms_set_crc_surface(crtc, stdu->display_srf);
1466 vmw_vkms_crtc_atomic_flush(crtc, state);
1469 static const struct drm_plane_funcs vmw_stdu_plane_funcs = {
1470 .update_plane = drm_atomic_helper_update_plane,
1471 .disable_plane = drm_atomic_helper_disable_plane,
1472 .destroy = vmw_du_primary_plane_destroy,
1473 .reset = vmw_du_plane_reset,
1474 .atomic_duplicate_state = vmw_du_plane_duplicate_state,
1475 .atomic_destroy_state = vmw_du_plane_destroy_state,
1478 static const struct drm_plane_funcs vmw_stdu_cursor_funcs = {
1479 .update_plane = drm_atomic_helper_update_plane,
1480 .disable_plane = drm_atomic_helper_disable_plane,
1481 .destroy = vmw_du_cursor_plane_destroy,
1482 .reset = vmw_du_plane_reset,
1483 .atomic_duplicate_state = vmw_du_plane_duplicate_state,
1484 .atomic_destroy_state = vmw_du_plane_destroy_state,
1492 drm_plane_helper_funcs vmw_stdu_cursor_plane_helper_funcs = {
1493 .atomic_check = vmw_du_cursor_plane_atomic_check,
1494 .atomic_update = vmw_du_cursor_plane_atomic_update,
1495 .prepare_fb = vmw_du_cursor_plane_prepare_fb,
1496 .cleanup_fb = vmw_du_cursor_plane_cleanup_fb,
1500 drm_plane_helper_funcs vmw_stdu_primary_plane_helper_funcs = {
1501 .atomic_check = vmw_du_primary_plane_atomic_check,
1502 .atomic_update = vmw_stdu_primary_plane_atomic_update,
1503 .prepare_fb = vmw_stdu_primary_plane_prepare_fb,
1504 .cleanup_fb = vmw_stdu_primary_plane_cleanup_fb,
1507 static const struct drm_crtc_helper_funcs vmw_stdu_crtc_helper_funcs = {
1508 .mode_set_nofb = vmw_stdu_crtc_mode_set_nofb,
1509 .atomic_check = vmw_du_crtc_atomic_check,
1510 .atomic_begin = vmw_du_crtc_atomic_begin,
1511 .atomic_flush = vmw_stdu_crtc_atomic_flush,
1512 .atomic_enable = vmw_vkms_crtc_atomic_enable,
1513 .atomic_disable = vmw_stdu_crtc_atomic_disable,
1518 * vmw_stdu_init - Sets up a Screen Target Display Unit
1520 * @dev_priv: VMW DRM device
1521 * @unit: unit number range from 0 to VMWGFX_NUM_DISPLAY_UNITS
1523 * This function is called once per CRTC, and allocates one Screen Target
1524 * display unit to represent that CRTC. Since the SVGA device does not separate
1525 * out encoder and connector, they are represented as part of the STDU as well.
1527 * Returns: %0 on success or -errno code on failure
1529 static int vmw_stdu_init(struct vmw_private *dev_priv, unsigned unit)
1531 struct vmw_screen_target_display_unit *stdu;
1532 struct drm_device *dev = &dev_priv->drm;
1533 struct drm_connector *connector;
1534 struct drm_encoder *encoder;
1535 struct drm_plane *primary;
1536 struct vmw_cursor_plane *cursor;
1537 struct drm_crtc *crtc;
1540 stdu = kzalloc(sizeof(*stdu), GFP_KERNEL);
1544 stdu->base.unit = unit;
1545 crtc = &stdu->base.crtc;
1546 encoder = &stdu->base.encoder;
1547 connector = &stdu->base.connector;
1548 primary = &stdu->base.primary;
1549 cursor = &stdu->base.cursor;
1551 stdu->base.pref_active = (unit == 0);
1552 stdu->base.pref_width = dev_priv->initial_width;
1553 stdu->base.pref_height = dev_priv->initial_height;
1554 stdu->base.is_implicit = false;
1556 /* Initialize primary plane */
1557 ret = drm_universal_plane_init(dev, primary,
1558 0, &vmw_stdu_plane_funcs,
1559 vmw_primary_plane_formats,
1560 ARRAY_SIZE(vmw_primary_plane_formats),
1561 NULL, DRM_PLANE_TYPE_PRIMARY, NULL);
1563 DRM_ERROR("Failed to initialize primary plane");
1567 drm_plane_helper_add(primary, &vmw_stdu_primary_plane_helper_funcs);
1568 drm_plane_enable_fb_damage_clips(primary);
1570 /* Initialize cursor plane */
1571 ret = drm_universal_plane_init(dev, &cursor->base,
1572 0, &vmw_stdu_cursor_funcs,
1573 vmw_cursor_plane_formats,
1574 ARRAY_SIZE(vmw_cursor_plane_formats),
1575 NULL, DRM_PLANE_TYPE_CURSOR, NULL);
1577 DRM_ERROR("Failed to initialize cursor plane");
1578 drm_plane_cleanup(&stdu->base.primary);
1582 drm_plane_helper_add(&cursor->base, &vmw_stdu_cursor_plane_helper_funcs);
1584 ret = drm_connector_init(dev, connector, &vmw_stdu_connector_funcs,
1585 DRM_MODE_CONNECTOR_VIRTUAL);
1587 DRM_ERROR("Failed to initialize connector\n");
1591 drm_connector_helper_add(connector, &vmw_stdu_connector_helper_funcs);
1592 connector->status = vmw_du_connector_detect(connector, false);
1594 ret = drm_encoder_init(dev, encoder, &vmw_stdu_encoder_funcs,
1595 DRM_MODE_ENCODER_VIRTUAL, NULL);
1597 DRM_ERROR("Failed to initialize encoder\n");
1598 goto err_free_connector;
1601 (void) drm_connector_attach_encoder(connector, encoder);
1602 encoder->possible_crtcs = (1 << unit);
1603 encoder->possible_clones = 0;
1605 ret = drm_connector_register(connector);
1607 DRM_ERROR("Failed to register connector\n");
1608 goto err_free_encoder;
1611 ret = drm_crtc_init_with_planes(dev, crtc, primary,
1613 &vmw_stdu_crtc_funcs, NULL);
1615 DRM_ERROR("Failed to initialize CRTC\n");
1616 goto err_free_unregister;
1619 drm_crtc_helper_add(crtc, &vmw_stdu_crtc_helper_funcs);
1621 drm_mode_crtc_set_gamma_size(crtc, 256);
1623 drm_object_attach_property(&connector->base,
1624 dev_priv->hotplug_mode_update_property, 1);
1625 drm_object_attach_property(&connector->base,
1626 dev->mode_config.suggested_x_property, 0);
1627 drm_object_attach_property(&connector->base,
1628 dev->mode_config.suggested_y_property, 0);
1630 vmw_du_init(&stdu->base);
1634 err_free_unregister:
1635 drm_connector_unregister(connector);
1637 drm_encoder_cleanup(encoder);
1639 drm_connector_cleanup(connector);
1648 * vmw_stdu_destroy - Cleans up a vmw_screen_target_display_unit
1650 * @stdu: Screen Target Display Unit to be destroyed
1652 * Clean up after vmw_stdu_init
1654 static void vmw_stdu_destroy(struct vmw_screen_target_display_unit *stdu)
1656 vmw_du_cleanup(&stdu->base);
1662 /******************************************************************************
1663 * Screen Target Display KMS Functions
1665 * These functions are called by the common KMS code in vmwgfx_kms.c
1666 *****************************************************************************/
1669 * vmw_kms_stdu_init_display - Initializes a Screen Target based display
1671 * @dev_priv: VMW DRM device
1673 * This function initialize a Screen Target based display device. It checks
1674 * the capability bits to make sure the underlying hardware can support
1675 * screen targets, and then creates the maximum number of CRTCs, a.k.a Display
1676 * Units, as supported by the display hardware.
1679 * 0 on success, error code otherwise
1681 int vmw_kms_stdu_init_display(struct vmw_private *dev_priv)
1683 struct drm_device *dev = &dev_priv->drm;
1687 /* Do nothing if there's no support for MOBs */
1688 if (!dev_priv->has_mob)
1691 if (!(dev_priv->capabilities & SVGA_CAP_GBOBJECTS))
1694 dev_priv->active_display_unit = vmw_du_screen_target;
1696 for (i = 0; i < VMWGFX_NUM_DISPLAY_UNITS; ++i) {
1697 ret = vmw_stdu_init(dev_priv, i);
1699 if (unlikely(ret != 0)) {
1700 drm_err(&dev_priv->drm,
1701 "Failed to initialize STDU %d", i);
1706 drm_mode_config_reset(dev);