1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /**************************************************************************
4 * Copyright 2011-2023 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_bo.h"
29 #include "vmwgfx_kms.h"
31 #include <drm/drm_atomic.h>
32 #include <drm/drm_atomic_helper.h>
33 #include <drm/drm_damage_helper.h>
34 #include <drm/drm_fourcc.h>
36 #define vmw_crtc_to_sou(x) \
37 container_of(x, struct vmw_screen_object_unit, base.crtc)
38 #define vmw_encoder_to_sou(x) \
39 container_of(x, struct vmw_screen_object_unit, base.encoder)
40 #define vmw_connector_to_sou(x) \
41 container_of(x, struct vmw_screen_object_unit, base.connector)
44 * struct vmw_kms_sou_surface_dirty - Closure structure for
45 * blit surface to screen command.
46 * @base: The base type we derive from. Used by vmw_kms_helper_dirty().
47 * @left: Left side of bounding box.
48 * @right: Right side of bounding box.
49 * @top: Top side of bounding box.
50 * @bottom: Bottom side of bounding box.
51 * @dst_x: Difference between source clip rects and framebuffer coordinates.
52 * @dst_y: Difference between source clip rects and framebuffer coordinates.
53 * @sid: Surface id of surface to copy from.
55 struct vmw_kms_sou_surface_dirty {
56 struct vmw_kms_dirty base;
57 s32 left, right, top, bottom;
63 * SVGA commands that are used by this code. Please see the device headers
66 struct vmw_kms_sou_readback_blit {
68 SVGAFifoCmdBlitScreenToGMRFB body;
71 struct vmw_kms_sou_bo_blit {
73 SVGAFifoCmdBlitGMRFBToScreen body;
76 struct vmw_kms_sou_dirty_cmd {
77 SVGA3dCmdHeader header;
78 SVGA3dCmdBlitSurfaceToScreen body;
81 struct vmw_kms_sou_define_gmrfb {
83 SVGAFifoCmdDefineGMRFB body;
87 * Display unit using screen objects.
89 struct vmw_screen_object_unit {
90 struct vmw_display_unit base;
92 unsigned long buffer_size; /**< Size of allocated buffer */
93 struct vmw_bo *buffer; /**< Backing store buffer */
98 static void vmw_sou_destroy(struct vmw_screen_object_unit *sou)
100 vmw_du_cleanup(&sou->base);
106 * Screen Object Display Unit CRTC functions
109 static void vmw_sou_crtc_destroy(struct drm_crtc *crtc)
111 vmw_sou_destroy(vmw_crtc_to_sou(crtc));
115 * Send the fifo command to create a screen.
117 static int vmw_sou_fifo_create(struct vmw_private *dev_priv,
118 struct vmw_screen_object_unit *sou,
120 struct drm_display_mode *mode)
128 SVGAScreenObject obj;
131 BUG_ON(!sou->buffer);
133 fifo_size = sizeof(*cmd);
134 cmd = VMW_CMD_RESERVE(dev_priv, fifo_size);
135 if (unlikely(cmd == NULL))
138 memset(cmd, 0, fifo_size);
139 cmd->header.cmdType = SVGA_CMD_DEFINE_SCREEN;
140 cmd->obj.structSize = sizeof(SVGAScreenObject);
141 cmd->obj.id = sou->base.unit;
142 cmd->obj.flags = SVGA_SCREEN_HAS_ROOT |
143 (sou->base.unit == 0 ? SVGA_SCREEN_IS_PRIMARY : 0);
144 cmd->obj.size.width = mode->hdisplay;
145 cmd->obj.size.height = mode->vdisplay;
148 sou->base.set_gui_x = cmd->obj.root.x;
149 sou->base.set_gui_y = cmd->obj.root.y;
151 /* Ok to assume that buffer is pinned in vram */
152 vmw_bo_get_guest_ptr(&sou->buffer->tbo, &cmd->obj.backingStore.ptr);
153 cmd->obj.backingStore.pitch = mode->hdisplay * 4;
155 vmw_cmd_commit(dev_priv, fifo_size);
163 * Send the fifo command to destroy a screen.
165 static int vmw_sou_fifo_destroy(struct vmw_private *dev_priv,
166 struct vmw_screen_object_unit *sou)
175 SVGAFifoCmdDestroyScreen body;
178 /* no need to do anything */
179 if (unlikely(!sou->defined))
182 fifo_size = sizeof(*cmd);
183 cmd = VMW_CMD_RESERVE(dev_priv, fifo_size);
184 if (unlikely(cmd == NULL))
187 memset(cmd, 0, fifo_size);
188 cmd->header.cmdType = SVGA_CMD_DESTROY_SCREEN;
189 cmd->body.screenId = sou->base.unit;
191 vmw_cmd_commit(dev_priv, fifo_size);
194 ret = vmw_fallback_wait(dev_priv, false, true, 0, false, 3*HZ);
195 if (unlikely(ret != 0))
196 DRM_ERROR("Failed to sync with HW");
198 sou->defined = false;
204 * vmw_sou_crtc_mode_set_nofb - Create new screen
206 * @crtc: CRTC associated with the new screen
208 * This function creates/destroys a screen. This function cannot fail, so if
209 * somehow we run into a failure, just do the best we can to get out.
211 static void vmw_sou_crtc_mode_set_nofb(struct drm_crtc *crtc)
213 struct vmw_private *dev_priv;
214 struct vmw_screen_object_unit *sou;
215 struct vmw_framebuffer *vfb;
216 struct drm_framebuffer *fb;
217 struct drm_plane_state *ps;
218 struct vmw_plane_state *vps;
221 sou = vmw_crtc_to_sou(crtc);
222 dev_priv = vmw_priv(crtc->dev);
223 ps = crtc->primary->state;
225 vps = vmw_plane_state_to_vps(ps);
227 vfb = (fb) ? vmw_framebuffer_to_vfb(fb) : NULL;
230 ret = vmw_sou_fifo_destroy(dev_priv, sou);
232 DRM_ERROR("Failed to destroy Screen Object\n");
238 struct drm_connector_state *conn_state;
239 struct vmw_connector_state *vmw_conn_state;
242 sou->buffer = vps->bo;
243 sou->buffer_size = vps->bo_size;
245 conn_state = sou->base.connector.state;
246 vmw_conn_state = vmw_connector_state_to_vcs(conn_state);
248 x = vmw_conn_state->gui_x;
249 y = vmw_conn_state->gui_y;
251 ret = vmw_sou_fifo_create(dev_priv, sou, x, y, &crtc->mode);
253 DRM_ERROR("Failed to define Screen Object %dx%d\n",
258 sou->buffer_size = 0;
263 * vmw_sou_crtc_helper_prepare - Noop
265 * @crtc: CRTC associated with the new screen
267 * Prepares the CRTC for a mode set, but we don't need to do anything here.
269 static void vmw_sou_crtc_helper_prepare(struct drm_crtc *crtc)
274 * vmw_sou_crtc_atomic_enable - Noop
276 * @crtc: CRTC associated with the new screen
279 * This is called after a mode set has been completed.
281 static void vmw_sou_crtc_atomic_enable(struct drm_crtc *crtc,
282 struct drm_atomic_state *state)
287 * vmw_sou_crtc_atomic_disable - Turns off CRTC
289 * @crtc: CRTC to be turned off
292 static void vmw_sou_crtc_atomic_disable(struct drm_crtc *crtc,
293 struct drm_atomic_state *state)
295 struct vmw_private *dev_priv;
296 struct vmw_screen_object_unit *sou;
301 DRM_ERROR("CRTC is NULL\n");
305 sou = vmw_crtc_to_sou(crtc);
306 dev_priv = vmw_priv(crtc->dev);
309 ret = vmw_sou_fifo_destroy(dev_priv, sou);
311 DRM_ERROR("Failed to destroy Screen Object\n");
315 static const struct drm_crtc_funcs vmw_screen_object_crtc_funcs = {
316 .gamma_set = vmw_du_crtc_gamma_set,
317 .destroy = vmw_sou_crtc_destroy,
318 .reset = vmw_du_crtc_reset,
319 .atomic_duplicate_state = vmw_du_crtc_duplicate_state,
320 .atomic_destroy_state = vmw_du_crtc_destroy_state,
321 .set_config = drm_atomic_helper_set_config,
322 .page_flip = drm_atomic_helper_page_flip,
326 * Screen Object Display Unit encoder functions
329 static void vmw_sou_encoder_destroy(struct drm_encoder *encoder)
331 vmw_sou_destroy(vmw_encoder_to_sou(encoder));
334 static const struct drm_encoder_funcs vmw_screen_object_encoder_funcs = {
335 .destroy = vmw_sou_encoder_destroy,
339 * Screen Object Display Unit connector functions
342 static void vmw_sou_connector_destroy(struct drm_connector *connector)
344 vmw_sou_destroy(vmw_connector_to_sou(connector));
347 static const struct drm_connector_funcs vmw_sou_connector_funcs = {
348 .dpms = vmw_du_connector_dpms,
349 .detect = vmw_du_connector_detect,
350 .fill_modes = vmw_du_connector_fill_modes,
351 .destroy = vmw_sou_connector_destroy,
352 .reset = vmw_du_connector_reset,
353 .atomic_duplicate_state = vmw_du_connector_duplicate_state,
354 .atomic_destroy_state = vmw_du_connector_destroy_state,
359 drm_connector_helper_funcs vmw_sou_connector_helper_funcs = {
365 * Screen Object Display Plane Functions
369 * vmw_sou_primary_plane_cleanup_fb - Frees sou backing buffer
371 * @plane: display plane
372 * @old_state: Contains the FB to clean up
374 * Unpins the display surface
376 * Returns 0 on success
379 vmw_sou_primary_plane_cleanup_fb(struct drm_plane *plane,
380 struct drm_plane_state *old_state)
382 struct vmw_plane_state *vps = vmw_plane_state_to_vps(old_state);
383 struct drm_crtc *crtc = plane->state->crtc ?
384 plane->state->crtc : old_state->crtc;
387 vmw_bo_unpin(vmw_priv(crtc->dev), vps->bo, false);
388 vmw_bo_unreference(&vps->bo);
391 vmw_du_plane_cleanup_fb(plane, old_state);
396 * vmw_sou_primary_plane_prepare_fb - allocate backing buffer
398 * @plane: display plane
399 * @new_state: info on the new plane state, including the FB
401 * The SOU backing buffer is our equivalent of the display plane.
403 * Returns 0 on success
406 vmw_sou_primary_plane_prepare_fb(struct drm_plane *plane,
407 struct drm_plane_state *new_state)
409 struct drm_framebuffer *new_fb = new_state->fb;
410 struct drm_crtc *crtc = plane->state->crtc ?: new_state->crtc;
411 struct vmw_plane_state *vps = vmw_plane_state_to_vps(new_state);
412 struct vmw_private *dev_priv;
414 struct vmw_bo_params bo_params = {
415 .domain = VMW_BO_DOMAIN_VRAM,
416 .busy_domain = VMW_BO_DOMAIN_VRAM,
417 .bo_type = ttm_bo_type_device,
422 vmw_bo_unreference(&vps->bo);
428 bo_params.size = new_state->crtc_w * new_state->crtc_h * 4;
429 dev_priv = vmw_priv(crtc->dev);
432 if (vps->bo_size == bo_params.size) {
434 * Note that this might temporarily up the pin-count
435 * to 2, until cleanup_fb() is called.
437 return vmw_bo_pin_in_vram(dev_priv, vps->bo,
441 vmw_bo_unreference(&vps->bo);
445 vmw_svga_enable(dev_priv);
447 /* After we have alloced the backing store might not be able to
448 * resume the overlays, this is preferred to failing to alloc.
450 vmw_overlay_pause_all(dev_priv);
451 ret = vmw_bo_create(dev_priv, &bo_params, &vps->bo);
452 vmw_overlay_resume_all(dev_priv);
456 vps->bo_size = bo_params.size;
459 * TTM already thinks the buffer is pinned, but make sure the
460 * pin_count is upped.
462 return vmw_bo_pin_in_vram(dev_priv, vps->bo, true);
465 static uint32_t vmw_sou_bo_fifo_size(struct vmw_du_update_plane *update,
468 return sizeof(struct vmw_kms_sou_define_gmrfb) +
469 sizeof(struct vmw_kms_sou_bo_blit) * num_hits;
472 static uint32_t vmw_sou_bo_define_gmrfb(struct vmw_du_update_plane *update,
475 struct vmw_framebuffer_bo *vfbbo =
476 container_of(update->vfb, typeof(*vfbbo), base);
477 struct vmw_kms_sou_define_gmrfb *gmr = cmd;
478 int depth = update->vfb->base.format->depth;
480 /* Emulate RGBA support, contrary to svga_reg.h this is not
481 * supported by hosts. This is only a problem if we are reading
482 * this value later and expecting what we uploaded back.
487 gmr->header = SVGA_CMD_DEFINE_GMRFB;
489 gmr->body.format.bitsPerPixel = update->vfb->base.format->cpp[0] * 8;
490 gmr->body.format.colorDepth = depth;
491 gmr->body.format.reserved = 0;
492 gmr->body.bytesPerLine = update->vfb->base.pitches[0];
493 vmw_bo_get_guest_ptr(&vfbbo->buffer->tbo, &gmr->body.ptr);
498 static uint32_t vmw_sou_bo_populate_clip(struct vmw_du_update_plane *update,
499 void *cmd, struct drm_rect *clip,
500 uint32_t fb_x, uint32_t fb_y)
502 struct vmw_kms_sou_bo_blit *blit = cmd;
504 blit->header = SVGA_CMD_BLIT_GMRFB_TO_SCREEN;
505 blit->body.destScreenId = update->du->unit;
506 blit->body.srcOrigin.x = fb_x;
507 blit->body.srcOrigin.y = fb_y;
508 blit->body.destRect.left = clip->x1;
509 blit->body.destRect.top = clip->y1;
510 blit->body.destRect.right = clip->x2;
511 blit->body.destRect.bottom = clip->y2;
513 return sizeof(*blit);
516 static uint32_t vmw_stud_bo_post_clip(struct vmw_du_update_plane *update,
517 void *cmd, struct drm_rect *bb)
523 * vmw_sou_plane_update_bo - Update display unit for bo backed fb.
524 * @dev_priv: Device private.
525 * @plane: Plane state.
526 * @old_state: Old plane state.
527 * @vfb: Framebuffer which is blitted to display unit.
528 * @out_fence: If non-NULL, will return a ref-counted pointer to vmw_fence_obj.
529 * The returned fence pointer may be NULL in which case the device
530 * has already synchronized.
532 * Return: 0 on success or a negative error code on failure.
534 static int vmw_sou_plane_update_bo(struct vmw_private *dev_priv,
535 struct drm_plane *plane,
536 struct drm_plane_state *old_state,
537 struct vmw_framebuffer *vfb,
538 struct vmw_fence_obj **out_fence)
540 struct vmw_du_update_plane_buffer bo_update;
542 memset(&bo_update, 0, sizeof(struct vmw_du_update_plane_buffer));
543 bo_update.base.plane = plane;
544 bo_update.base.old_state = old_state;
545 bo_update.base.dev_priv = dev_priv;
546 bo_update.base.du = vmw_crtc_to_du(plane->state->crtc);
547 bo_update.base.vfb = vfb;
548 bo_update.base.out_fence = out_fence;
549 bo_update.base.mutex = NULL;
550 bo_update.base.intr = true;
552 bo_update.base.calc_fifo_size = vmw_sou_bo_fifo_size;
553 bo_update.base.post_prepare = vmw_sou_bo_define_gmrfb;
554 bo_update.base.clip = vmw_sou_bo_populate_clip;
555 bo_update.base.post_clip = vmw_stud_bo_post_clip;
557 return vmw_du_helper_plane_update(&bo_update.base);
560 static uint32_t vmw_sou_surface_fifo_size(struct vmw_du_update_plane *update,
563 return sizeof(struct vmw_kms_sou_dirty_cmd) + sizeof(SVGASignedRect) *
567 static uint32_t vmw_sou_surface_post_prepare(struct vmw_du_update_plane *update,
570 struct vmw_du_update_plane_surface *srf_update;
572 srf_update = container_of(update, typeof(*srf_update), base);
575 * SOU SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN is special in the sense that
576 * its bounding box is filled before iterating over all the clips. So
577 * store the FIFO start address and revisit to fill the details.
579 srf_update->cmd_start = cmd;
584 static uint32_t vmw_sou_surface_pre_clip(struct vmw_du_update_plane *update,
585 void *cmd, uint32_t num_hits)
587 struct vmw_kms_sou_dirty_cmd *blit = cmd;
588 struct vmw_framebuffer_surface *vfbs;
590 vfbs = container_of(update->vfb, typeof(*vfbs), base);
592 blit->header.id = SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN;
593 blit->header.size = sizeof(blit->body) + sizeof(SVGASignedRect) *
596 blit->body.srcImage.sid = vfbs->surface->res.id;
597 blit->body.destScreenId = update->du->unit;
599 /* Update the source and destination bounding box later in post_clip */
600 blit->body.srcRect.left = 0;
601 blit->body.srcRect.top = 0;
602 blit->body.srcRect.right = 0;
603 blit->body.srcRect.bottom = 0;
605 blit->body.destRect.left = 0;
606 blit->body.destRect.top = 0;
607 blit->body.destRect.right = 0;
608 blit->body.destRect.bottom = 0;
610 return sizeof(*blit);
613 static uint32_t vmw_sou_surface_clip_rect(struct vmw_du_update_plane *update,
614 void *cmd, struct drm_rect *clip,
615 uint32_t src_x, uint32_t src_y)
617 SVGASignedRect *rect = cmd;
620 * rects are relative to dest bounding box rect on screen object, so
621 * translate to it later in post_clip
623 rect->left = clip->x1;
624 rect->top = clip->y1;
625 rect->right = clip->x2;
626 rect->bottom = clip->y2;
628 return sizeof(*rect);
631 static uint32_t vmw_sou_surface_post_clip(struct vmw_du_update_plane *update,
632 void *cmd, struct drm_rect *bb)
634 struct vmw_du_update_plane_surface *srf_update;
635 struct drm_plane_state *state = update->plane->state;
636 struct drm_rect src_bb;
637 struct vmw_kms_sou_dirty_cmd *blit;
638 SVGASignedRect *rect;
644 srf_update = container_of(update, typeof(*srf_update), base);
646 blit = srf_update->cmd_start;
647 rect = (SVGASignedRect *)&blit[1];
649 num_hits = (blit->header.size - sizeof(blit->body))/
650 sizeof(SVGASignedRect);
654 /* To translate bb back to fb src coord */
655 translate_src_x = (state->src_x >> 16) - state->crtc_x;
656 translate_src_y = (state->src_y >> 16) - state->crtc_y;
658 drm_rect_translate(&src_bb, translate_src_x, translate_src_y);
660 blit->body.srcRect.left = src_bb.x1;
661 blit->body.srcRect.top = src_bb.y1;
662 blit->body.srcRect.right = src_bb.x2;
663 blit->body.srcRect.bottom = src_bb.y2;
665 blit->body.destRect.left = bb->x1;
666 blit->body.destRect.top = bb->y1;
667 blit->body.destRect.right = bb->x2;
668 blit->body.destRect.bottom = bb->y2;
670 /* rects are relative to dest bb rect */
671 for (i = 0; i < num_hits; i++) {
672 rect->left -= bb->x1;
674 rect->right -= bb->x1;
675 rect->bottom -= bb->y1;
683 * vmw_sou_plane_update_surface - Update display unit for surface backed fb.
684 * @dev_priv: Device private.
685 * @plane: Plane state.
686 * @old_state: Old plane state.
687 * @vfb: Framebuffer which is blitted to display unit
688 * @out_fence: If non-NULL, will return a ref-counted pointer to vmw_fence_obj.
689 * The returned fence pointer may be NULL in which case the device
690 * has already synchronized.
692 * Return: 0 on success or a negative error code on failure.
694 static int vmw_sou_plane_update_surface(struct vmw_private *dev_priv,
695 struct drm_plane *plane,
696 struct drm_plane_state *old_state,
697 struct vmw_framebuffer *vfb,
698 struct vmw_fence_obj **out_fence)
700 struct vmw_du_update_plane_surface srf_update;
702 memset(&srf_update, 0, sizeof(struct vmw_du_update_plane_surface));
703 srf_update.base.plane = plane;
704 srf_update.base.old_state = old_state;
705 srf_update.base.dev_priv = dev_priv;
706 srf_update.base.du = vmw_crtc_to_du(plane->state->crtc);
707 srf_update.base.vfb = vfb;
708 srf_update.base.out_fence = out_fence;
709 srf_update.base.mutex = &dev_priv->cmdbuf_mutex;
710 srf_update.base.intr = true;
712 srf_update.base.calc_fifo_size = vmw_sou_surface_fifo_size;
713 srf_update.base.post_prepare = vmw_sou_surface_post_prepare;
714 srf_update.base.pre_clip = vmw_sou_surface_pre_clip;
715 srf_update.base.clip = vmw_sou_surface_clip_rect;
716 srf_update.base.post_clip = vmw_sou_surface_post_clip;
718 return vmw_du_helper_plane_update(&srf_update.base);
722 vmw_sou_primary_plane_atomic_update(struct drm_plane *plane,
723 struct drm_atomic_state *state)
725 struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state, plane);
726 struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state, plane);
727 struct drm_crtc *crtc = new_state->crtc;
728 struct vmw_fence_obj *fence = NULL;
731 /* In case of device error, maintain consistent atomic state */
732 if (crtc && new_state->fb) {
733 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
734 struct vmw_framebuffer *vfb =
735 vmw_framebuffer_to_vfb(new_state->fb);
738 ret = vmw_sou_plane_update_bo(dev_priv, plane,
739 old_state, vfb, &fence);
741 ret = vmw_sou_plane_update_surface(dev_priv, plane,
745 DRM_ERROR("Failed to update screen.\n");
747 /* Do nothing when fb and crtc is NULL (blank crtc) */
752 vmw_fence_obj_unreference(&fence);
756 static const struct drm_plane_funcs vmw_sou_plane_funcs = {
757 .update_plane = drm_atomic_helper_update_plane,
758 .disable_plane = drm_atomic_helper_disable_plane,
759 .destroy = vmw_du_primary_plane_destroy,
760 .reset = vmw_du_plane_reset,
761 .atomic_duplicate_state = vmw_du_plane_duplicate_state,
762 .atomic_destroy_state = vmw_du_plane_destroy_state,
765 static const struct drm_plane_funcs vmw_sou_cursor_funcs = {
766 .update_plane = drm_atomic_helper_update_plane,
767 .disable_plane = drm_atomic_helper_disable_plane,
768 .destroy = vmw_du_cursor_plane_destroy,
769 .reset = vmw_du_plane_reset,
770 .atomic_duplicate_state = vmw_du_plane_duplicate_state,
771 .atomic_destroy_state = vmw_du_plane_destroy_state,
778 drm_plane_helper_funcs vmw_sou_cursor_plane_helper_funcs = {
779 .atomic_check = vmw_du_cursor_plane_atomic_check,
780 .atomic_update = vmw_du_cursor_plane_atomic_update,
781 .prepare_fb = vmw_du_cursor_plane_prepare_fb,
782 .cleanup_fb = vmw_du_cursor_plane_cleanup_fb,
786 drm_plane_helper_funcs vmw_sou_primary_plane_helper_funcs = {
787 .atomic_check = vmw_du_primary_plane_atomic_check,
788 .atomic_update = vmw_sou_primary_plane_atomic_update,
789 .prepare_fb = vmw_sou_primary_plane_prepare_fb,
790 .cleanup_fb = vmw_sou_primary_plane_cleanup_fb,
793 static const struct drm_crtc_helper_funcs vmw_sou_crtc_helper_funcs = {
794 .prepare = vmw_sou_crtc_helper_prepare,
795 .mode_set_nofb = vmw_sou_crtc_mode_set_nofb,
796 .atomic_check = vmw_du_crtc_atomic_check,
797 .atomic_begin = vmw_du_crtc_atomic_begin,
798 .atomic_flush = vmw_du_crtc_atomic_flush,
799 .atomic_enable = vmw_sou_crtc_atomic_enable,
800 .atomic_disable = vmw_sou_crtc_atomic_disable,
804 static int vmw_sou_init(struct vmw_private *dev_priv, unsigned unit)
806 struct vmw_screen_object_unit *sou;
807 struct drm_device *dev = &dev_priv->drm;
808 struct drm_connector *connector;
809 struct drm_encoder *encoder;
810 struct drm_plane *primary;
811 struct vmw_cursor_plane *cursor;
812 struct drm_crtc *crtc;
815 sou = kzalloc(sizeof(*sou), GFP_KERNEL);
819 sou->base.unit = unit;
820 crtc = &sou->base.crtc;
821 encoder = &sou->base.encoder;
822 connector = &sou->base.connector;
823 primary = &sou->base.primary;
824 cursor = &sou->base.cursor;
826 sou->base.pref_active = (unit == 0);
827 sou->base.pref_width = dev_priv->initial_width;
828 sou->base.pref_height = dev_priv->initial_height;
829 sou->base.pref_mode = NULL;
832 * Remove this after enabling atomic because property values can
833 * only exist in a state object
835 sou->base.is_implicit = false;
837 /* Initialize primary plane */
838 ret = drm_universal_plane_init(dev, primary,
839 0, &vmw_sou_plane_funcs,
840 vmw_primary_plane_formats,
841 ARRAY_SIZE(vmw_primary_plane_formats),
842 NULL, DRM_PLANE_TYPE_PRIMARY, NULL);
844 DRM_ERROR("Failed to initialize primary plane");
848 drm_plane_helper_add(primary, &vmw_sou_primary_plane_helper_funcs);
849 drm_plane_enable_fb_damage_clips(primary);
851 /* Initialize cursor plane */
852 ret = drm_universal_plane_init(dev, &cursor->base,
853 0, &vmw_sou_cursor_funcs,
854 vmw_cursor_plane_formats,
855 ARRAY_SIZE(vmw_cursor_plane_formats),
856 NULL, DRM_PLANE_TYPE_CURSOR, NULL);
858 DRM_ERROR("Failed to initialize cursor plane");
859 drm_plane_cleanup(&sou->base.primary);
863 drm_plane_helper_add(&cursor->base, &vmw_sou_cursor_plane_helper_funcs);
865 ret = drm_connector_init(dev, connector, &vmw_sou_connector_funcs,
866 DRM_MODE_CONNECTOR_VIRTUAL);
868 DRM_ERROR("Failed to initialize connector\n");
872 drm_connector_helper_add(connector, &vmw_sou_connector_helper_funcs);
873 connector->status = vmw_du_connector_detect(connector, true);
875 ret = drm_encoder_init(dev, encoder, &vmw_screen_object_encoder_funcs,
876 DRM_MODE_ENCODER_VIRTUAL, NULL);
878 DRM_ERROR("Failed to initialize encoder\n");
879 goto err_free_connector;
882 (void) drm_connector_attach_encoder(connector, encoder);
883 encoder->possible_crtcs = (1 << unit);
884 encoder->possible_clones = 0;
886 ret = drm_connector_register(connector);
888 DRM_ERROR("Failed to register connector\n");
889 goto err_free_encoder;
892 ret = drm_crtc_init_with_planes(dev, crtc, primary,
894 &vmw_screen_object_crtc_funcs, NULL);
896 DRM_ERROR("Failed to initialize CRTC\n");
897 goto err_free_unregister;
900 drm_crtc_helper_add(crtc, &vmw_sou_crtc_helper_funcs);
902 drm_mode_crtc_set_gamma_size(crtc, 256);
904 drm_object_attach_property(&connector->base,
905 dev_priv->hotplug_mode_update_property, 1);
906 drm_object_attach_property(&connector->base,
907 dev->mode_config.suggested_x_property, 0);
908 drm_object_attach_property(&connector->base,
909 dev->mode_config.suggested_y_property, 0);
913 drm_connector_unregister(connector);
915 drm_encoder_cleanup(encoder);
917 drm_connector_cleanup(connector);
923 int vmw_kms_sou_init_display(struct vmw_private *dev_priv)
925 struct drm_device *dev = &dev_priv->drm;
928 /* Screen objects won't work if GMR's aren't available */
929 if (!dev_priv->has_gmr)
932 if (!(dev_priv->capabilities & SVGA_CAP_SCREEN_OBJECT_2)) {
936 for (i = 0; i < VMWGFX_NUM_DISPLAY_UNITS; ++i)
937 vmw_sou_init(dev_priv, i);
939 dev_priv->active_display_unit = vmw_du_screen_object;
941 drm_mode_config_reset(dev);
946 static int do_bo_define_gmrfb(struct vmw_private *dev_priv,
947 struct vmw_framebuffer *framebuffer)
950 container_of(framebuffer, struct vmw_framebuffer_bo,
952 int depth = framebuffer->base.format->depth;
955 SVGAFifoCmdDefineGMRFB body;
958 /* Emulate RGBA support, contrary to svga_reg.h this is not
959 * supported by hosts. This is only a problem if we are reading
960 * this value later and expecting what we uploaded back.
965 cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd));
969 cmd->header = SVGA_CMD_DEFINE_GMRFB;
970 cmd->body.format.bitsPerPixel = framebuffer->base.format->cpp[0] * 8;
971 cmd->body.format.colorDepth = depth;
972 cmd->body.format.reserved = 0;
973 cmd->body.bytesPerLine = framebuffer->base.pitches[0];
974 /* Buffer is reserved in vram or GMR */
975 vmw_bo_get_guest_ptr(&buf->tbo, &cmd->body.ptr);
976 vmw_cmd_commit(dev_priv, sizeof(*cmd));
982 * vmw_sou_surface_fifo_commit - Callback to fill in and submit a
983 * blit surface to screen command.
985 * @dirty: The closure structure.
987 * Fills in the missing fields in the command, and translates the cliprects
988 * to match the destination bounding box encoded.
990 static void vmw_sou_surface_fifo_commit(struct vmw_kms_dirty *dirty)
992 struct vmw_kms_sou_surface_dirty *sdirty =
993 container_of(dirty, typeof(*sdirty), base);
994 struct vmw_kms_sou_dirty_cmd *cmd = dirty->cmd;
995 s32 trans_x = dirty->unit->crtc.x - sdirty->dst_x;
996 s32 trans_y = dirty->unit->crtc.y - sdirty->dst_y;
997 size_t region_size = dirty->num_hits * sizeof(SVGASignedRect);
998 SVGASignedRect *blit = (SVGASignedRect *) &cmd[1];
1001 if (!dirty->num_hits) {
1002 vmw_cmd_commit(dirty->dev_priv, 0);
1006 cmd->header.id = SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN;
1007 cmd->header.size = sizeof(cmd->body) + region_size;
1010 * Use the destination bounding box to specify destination - and
1011 * source bounding regions.
1013 cmd->body.destRect.left = sdirty->left;
1014 cmd->body.destRect.right = sdirty->right;
1015 cmd->body.destRect.top = sdirty->top;
1016 cmd->body.destRect.bottom = sdirty->bottom;
1018 cmd->body.srcRect.left = sdirty->left + trans_x;
1019 cmd->body.srcRect.right = sdirty->right + trans_x;
1020 cmd->body.srcRect.top = sdirty->top + trans_y;
1021 cmd->body.srcRect.bottom = sdirty->bottom + trans_y;
1023 cmd->body.srcImage.sid = sdirty->sid;
1024 cmd->body.destScreenId = dirty->unit->unit;
1026 /* Blits are relative to the destination rect. Translate. */
1027 for (i = 0; i < dirty->num_hits; ++i, ++blit) {
1028 blit->left -= sdirty->left;
1029 blit->right -= sdirty->left;
1030 blit->top -= sdirty->top;
1031 blit->bottom -= sdirty->top;
1034 vmw_cmd_commit(dirty->dev_priv, region_size + sizeof(*cmd));
1036 sdirty->left = sdirty->top = S32_MAX;
1037 sdirty->right = sdirty->bottom = S32_MIN;
1041 * vmw_sou_surface_clip - Callback to encode a blit surface to screen cliprect.
1043 * @dirty: The closure structure
1045 * Encodes a SVGASignedRect cliprect and updates the bounding box of the
1046 * BLIT_SURFACE_TO_SCREEN command.
1048 static void vmw_sou_surface_clip(struct vmw_kms_dirty *dirty)
1050 struct vmw_kms_sou_surface_dirty *sdirty =
1051 container_of(dirty, typeof(*sdirty), base);
1052 struct vmw_kms_sou_dirty_cmd *cmd = dirty->cmd;
1053 SVGASignedRect *blit = (SVGASignedRect *) &cmd[1];
1055 /* Destination rect. */
1056 blit += dirty->num_hits;
1057 blit->left = dirty->unit_x1;
1058 blit->top = dirty->unit_y1;
1059 blit->right = dirty->unit_x2;
1060 blit->bottom = dirty->unit_y2;
1062 /* Destination bounding box */
1063 sdirty->left = min_t(s32, sdirty->left, dirty->unit_x1);
1064 sdirty->top = min_t(s32, sdirty->top, dirty->unit_y1);
1065 sdirty->right = max_t(s32, sdirty->right, dirty->unit_x2);
1066 sdirty->bottom = max_t(s32, sdirty->bottom, dirty->unit_y2);
1072 * vmw_kms_sou_do_surface_dirty - Dirty part of a surface backed framebuffer
1074 * @dev_priv: Pointer to the device private structure.
1075 * @framebuffer: Pointer to the surface-buffer backed framebuffer.
1076 * @clips: Array of clip rects. Either @clips or @vclips must be NULL.
1077 * @vclips: Alternate array of clip rects. Either @clips or @vclips must
1079 * @srf: Pointer to surface to blit from. If NULL, the surface attached
1080 * to @framebuffer will be used.
1081 * @dest_x: X coordinate offset to align @srf with framebuffer coordinates.
1082 * @dest_y: Y coordinate offset to align @srf with framebuffer coordinates.
1083 * @num_clips: Number of clip rects in @clips.
1084 * @inc: Increment to use when looping over @clips.
1085 * @out_fence: If non-NULL, will return a ref-counted pointer to a
1086 * struct vmw_fence_obj. The returned fence pointer may be NULL in which
1087 * case the device has already synchronized.
1088 * @crtc: If crtc is passed, perform surface dirty on that crtc only.
1090 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
1093 int vmw_kms_sou_do_surface_dirty(struct vmw_private *dev_priv,
1094 struct vmw_framebuffer *framebuffer,
1095 struct drm_clip_rect *clips,
1096 struct drm_vmw_rect *vclips,
1097 struct vmw_resource *srf,
1100 unsigned num_clips, int inc,
1101 struct vmw_fence_obj **out_fence,
1102 struct drm_crtc *crtc)
1104 struct vmw_framebuffer_surface *vfbs =
1105 container_of(framebuffer, typeof(*vfbs), base);
1106 struct vmw_kms_sou_surface_dirty sdirty;
1107 DECLARE_VAL_CONTEXT(val_ctx, NULL, 0);
1111 srf = &vfbs->surface->res;
1113 ret = vmw_validation_add_resource(&val_ctx, srf, 0, VMW_RES_DIRTY_NONE,
1118 ret = vmw_validation_prepare(&val_ctx, &dev_priv->cmdbuf_mutex, true);
1122 sdirty.base.fifo_commit = vmw_sou_surface_fifo_commit;
1123 sdirty.base.clip = vmw_sou_surface_clip;
1124 sdirty.base.dev_priv = dev_priv;
1125 sdirty.base.fifo_reserve_size = sizeof(struct vmw_kms_sou_dirty_cmd) +
1126 sizeof(SVGASignedRect) * num_clips;
1127 sdirty.base.crtc = crtc;
1129 sdirty.sid = srf->id;
1130 sdirty.left = sdirty.top = S32_MAX;
1131 sdirty.right = sdirty.bottom = S32_MIN;
1132 sdirty.dst_x = dest_x;
1133 sdirty.dst_y = dest_y;
1135 ret = vmw_kms_helper_dirty(dev_priv, framebuffer, clips, vclips,
1136 dest_x, dest_y, num_clips, inc,
1138 vmw_kms_helper_validation_finish(dev_priv, NULL, &val_ctx, out_fence,
1144 vmw_validation_unref_lists(&val_ctx);
1149 * vmw_sou_bo_fifo_commit - Callback to submit a set of readback clips.
1151 * @dirty: The closure structure.
1153 * Commits a previously built command buffer of readback clips.
1155 static void vmw_sou_bo_fifo_commit(struct vmw_kms_dirty *dirty)
1157 if (!dirty->num_hits) {
1158 vmw_cmd_commit(dirty->dev_priv, 0);
1162 vmw_cmd_commit(dirty->dev_priv,
1163 sizeof(struct vmw_kms_sou_bo_blit) *
1168 * vmw_sou_bo_clip - Callback to encode a readback cliprect.
1170 * @dirty: The closure structure
1172 * Encodes a BLIT_GMRFB_TO_SCREEN cliprect.
1174 static void vmw_sou_bo_clip(struct vmw_kms_dirty *dirty)
1176 struct vmw_kms_sou_bo_blit *blit = dirty->cmd;
1178 blit += dirty->num_hits;
1179 blit->header = SVGA_CMD_BLIT_GMRFB_TO_SCREEN;
1180 blit->body.destScreenId = dirty->unit->unit;
1181 blit->body.srcOrigin.x = dirty->fb_x;
1182 blit->body.srcOrigin.y = dirty->fb_y;
1183 blit->body.destRect.left = dirty->unit_x1;
1184 blit->body.destRect.top = dirty->unit_y1;
1185 blit->body.destRect.right = dirty->unit_x2;
1186 blit->body.destRect.bottom = dirty->unit_y2;
1191 * vmw_kms_sou_do_bo_dirty - Dirty part of a buffer-object backed framebuffer
1193 * @dev_priv: Pointer to the device private structure.
1194 * @framebuffer: Pointer to the buffer-object backed framebuffer.
1195 * @clips: Array of clip rects.
1196 * @vclips: Alternate array of clip rects. Either @clips or @vclips must
1198 * @num_clips: Number of clip rects in @clips.
1199 * @increment: Increment to use when looping over @clips.
1200 * @interruptible: Whether to perform waits interruptible if possible.
1201 * @out_fence: If non-NULL, will return a ref-counted pointer to a
1202 * struct vmw_fence_obj. The returned fence pointer may be NULL in which
1203 * case the device has already synchronized.
1204 * @crtc: If crtc is passed, perform bo dirty on that crtc only.
1206 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
1209 int vmw_kms_sou_do_bo_dirty(struct vmw_private *dev_priv,
1210 struct vmw_framebuffer *framebuffer,
1211 struct drm_clip_rect *clips,
1212 struct drm_vmw_rect *vclips,
1213 unsigned num_clips, int increment,
1215 struct vmw_fence_obj **out_fence,
1216 struct drm_crtc *crtc)
1218 struct vmw_bo *buf =
1219 container_of(framebuffer, struct vmw_framebuffer_bo,
1221 struct vmw_kms_dirty dirty;
1222 DECLARE_VAL_CONTEXT(val_ctx, NULL, 0);
1225 vmw_bo_placement_set(buf, VMW_BO_DOMAIN_GMR | VMW_BO_DOMAIN_VRAM,
1226 VMW_BO_DOMAIN_GMR | VMW_BO_DOMAIN_VRAM);
1227 ret = vmw_validation_add_bo(&val_ctx, buf);
1231 ret = vmw_validation_prepare(&val_ctx, NULL, interruptible);
1235 ret = do_bo_define_gmrfb(dev_priv, framebuffer);
1236 if (unlikely(ret != 0))
1240 dirty.fifo_commit = vmw_sou_bo_fifo_commit;
1241 dirty.clip = vmw_sou_bo_clip;
1242 dirty.fifo_reserve_size = sizeof(struct vmw_kms_sou_bo_blit) *
1244 ret = vmw_kms_helper_dirty(dev_priv, framebuffer, clips, vclips,
1245 0, 0, num_clips, increment, &dirty);
1246 vmw_kms_helper_validation_finish(dev_priv, NULL, &val_ctx, out_fence,
1252 vmw_validation_revert(&val_ctx);
1254 vmw_validation_unref_lists(&val_ctx);
1261 * vmw_sou_readback_fifo_commit - Callback to submit a set of readback clips.
1263 * @dirty: The closure structure.
1265 * Commits a previously built command buffer of readback clips.
1267 static void vmw_sou_readback_fifo_commit(struct vmw_kms_dirty *dirty)
1269 if (!dirty->num_hits) {
1270 vmw_cmd_commit(dirty->dev_priv, 0);
1274 vmw_cmd_commit(dirty->dev_priv,
1275 sizeof(struct vmw_kms_sou_readback_blit) *
1280 * vmw_sou_readback_clip - Callback to encode a readback cliprect.
1282 * @dirty: The closure structure
1284 * Encodes a BLIT_SCREEN_TO_GMRFB cliprect.
1286 static void vmw_sou_readback_clip(struct vmw_kms_dirty *dirty)
1288 struct vmw_kms_sou_readback_blit *blit = dirty->cmd;
1290 blit += dirty->num_hits;
1291 blit->header = SVGA_CMD_BLIT_SCREEN_TO_GMRFB;
1292 blit->body.srcScreenId = dirty->unit->unit;
1293 blit->body.destOrigin.x = dirty->fb_x;
1294 blit->body.destOrigin.y = dirty->fb_y;
1295 blit->body.srcRect.left = dirty->unit_x1;
1296 blit->body.srcRect.top = dirty->unit_y1;
1297 blit->body.srcRect.right = dirty->unit_x2;
1298 blit->body.srcRect.bottom = dirty->unit_y2;
1303 * vmw_kms_sou_readback - Perform a readback from the screen object system to
1304 * a buffer-object backed framebuffer.
1306 * @dev_priv: Pointer to the device private structure.
1307 * @file_priv: Pointer to a struct drm_file identifying the caller.
1308 * Must be set to NULL if @user_fence_rep is NULL.
1309 * @vfb: Pointer to the buffer-object backed framebuffer.
1310 * @user_fence_rep: User-space provided structure for fence information.
1311 * Must be set to non-NULL if @file_priv is non-NULL.
1312 * @vclips: Array of clip rects.
1313 * @num_clips: Number of clip rects in @vclips.
1314 * @crtc: If crtc is passed, readback on that crtc only.
1316 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
1319 int vmw_kms_sou_readback(struct vmw_private *dev_priv,
1320 struct drm_file *file_priv,
1321 struct vmw_framebuffer *vfb,
1322 struct drm_vmw_fence_rep __user *user_fence_rep,
1323 struct drm_vmw_rect *vclips,
1325 struct drm_crtc *crtc)
1327 struct vmw_bo *buf =
1328 container_of(vfb, struct vmw_framebuffer_bo, base)->buffer;
1329 struct vmw_kms_dirty dirty;
1330 DECLARE_VAL_CONTEXT(val_ctx, NULL, 0);
1333 vmw_bo_placement_set(buf, VMW_BO_DOMAIN_GMR | VMW_BO_DOMAIN_VRAM,
1334 VMW_BO_DOMAIN_GMR | VMW_BO_DOMAIN_VRAM);
1335 ret = vmw_validation_add_bo(&val_ctx, buf);
1339 ret = vmw_validation_prepare(&val_ctx, NULL, true);
1343 ret = do_bo_define_gmrfb(dev_priv, vfb);
1344 if (unlikely(ret != 0))
1348 dirty.fifo_commit = vmw_sou_readback_fifo_commit;
1349 dirty.clip = vmw_sou_readback_clip;
1350 dirty.fifo_reserve_size = sizeof(struct vmw_kms_sou_readback_blit) *
1352 ret = vmw_kms_helper_dirty(dev_priv, vfb, NULL, vclips,
1353 0, 0, num_clips, 1, &dirty);
1354 vmw_kms_helper_validation_finish(dev_priv, file_priv, &val_ctx, NULL,
1360 vmw_validation_revert(&val_ctx);
1362 vmw_validation_unref_lists(&val_ctx);