1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /**************************************************************************
4 * Copyright 2011-2022 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 <drm/drm_atomic.h>
29 #include <drm/drm_atomic_helper.h>
30 #include <drm/drm_damage_helper.h>
31 #include <drm/drm_fourcc.h>
32 #include <drm/drm_vblank.h>
34 #include "vmwgfx_kms.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_buffer_object *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->base, &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,
323 .get_vblank_counter = vmw_get_vblank_counter,
324 .enable_vblank = vmw_enable_vblank,
325 .disable_vblank = vmw_disable_vblank,
329 * Screen Object Display Unit encoder functions
332 static void vmw_sou_encoder_destroy(struct drm_encoder *encoder)
334 vmw_sou_destroy(vmw_encoder_to_sou(encoder));
337 static const struct drm_encoder_funcs vmw_screen_object_encoder_funcs = {
338 .destroy = vmw_sou_encoder_destroy,
342 * Screen Object Display Unit connector functions
345 static void vmw_sou_connector_destroy(struct drm_connector *connector)
347 vmw_sou_destroy(vmw_connector_to_sou(connector));
350 static const struct drm_connector_funcs vmw_sou_connector_funcs = {
351 .dpms = vmw_du_connector_dpms,
352 .detect = vmw_du_connector_detect,
353 .fill_modes = vmw_du_connector_fill_modes,
354 .destroy = vmw_sou_connector_destroy,
355 .reset = vmw_du_connector_reset,
356 .atomic_duplicate_state = vmw_du_connector_duplicate_state,
357 .atomic_destroy_state = vmw_du_connector_destroy_state,
362 drm_connector_helper_funcs vmw_sou_connector_helper_funcs = {
368 * Screen Object Display Plane Functions
372 * vmw_sou_primary_plane_cleanup_fb - Frees sou backing buffer
374 * @plane: display plane
375 * @old_state: Contains the FB to clean up
377 * Unpins the display surface
379 * Returns 0 on success
382 vmw_sou_primary_plane_cleanup_fb(struct drm_plane *plane,
383 struct drm_plane_state *old_state)
385 struct vmw_plane_state *vps = vmw_plane_state_to_vps(old_state);
386 struct drm_crtc *crtc = plane->state->crtc ?
387 plane->state->crtc : old_state->crtc;
390 vmw_bo_unpin(vmw_priv(crtc->dev), vps->bo, false);
391 vmw_bo_unreference(&vps->bo);
394 vmw_du_plane_cleanup_fb(plane, old_state);
399 * vmw_sou_primary_plane_prepare_fb - allocate backing buffer
401 * @plane: display plane
402 * @new_state: info on the new plane state, including the FB
404 * The SOU backing buffer is our equivalent of the display plane.
406 * Returns 0 on success
409 vmw_sou_primary_plane_prepare_fb(struct drm_plane *plane,
410 struct drm_plane_state *new_state)
412 struct drm_framebuffer *new_fb = new_state->fb;
413 struct drm_crtc *crtc = plane->state->crtc ?: new_state->crtc;
414 struct vmw_plane_state *vps = vmw_plane_state_to_vps(new_state);
415 struct vmw_private *dev_priv;
421 vmw_bo_unreference(&vps->bo);
427 size = new_state->crtc_w * new_state->crtc_h * 4;
428 dev_priv = vmw_priv(crtc->dev);
431 if (vps->bo_size == size) {
433 * Note that this might temporarily up the pin-count
434 * to 2, until cleanup_fb() is called.
436 return vmw_bo_pin_in_vram(dev_priv, vps->bo,
440 vmw_bo_unreference(&vps->bo);
444 vmw_svga_enable(dev_priv);
446 /* After we have alloced the backing store might not be able to
447 * resume the overlays, this is preferred to failing to alloc.
449 vmw_overlay_pause_all(dev_priv);
450 ret = vmw_bo_create(dev_priv, size,
452 false, true, &vmw_bo_bo_free, &vps->bo);
453 vmw_overlay_resume_all(dev_priv);
455 vps->bo = NULL; /* vmw_bo_init frees on error */
462 * TTM already thinks the buffer is pinned, but make sure the
463 * pin_count is upped.
465 return vmw_bo_pin_in_vram(dev_priv, vps->bo, true);
468 static uint32_t vmw_sou_bo_fifo_size(struct vmw_du_update_plane *update,
471 return sizeof(struct vmw_kms_sou_define_gmrfb) +
472 sizeof(struct vmw_kms_sou_bo_blit) * num_hits;
475 static uint32_t vmw_sou_bo_define_gmrfb(struct vmw_du_update_plane *update,
478 struct vmw_framebuffer_bo *vfbbo =
479 container_of(update->vfb, typeof(*vfbbo), base);
480 struct vmw_kms_sou_define_gmrfb *gmr = cmd;
481 int depth = update->vfb->base.format->depth;
483 /* Emulate RGBA support, contrary to svga_reg.h this is not
484 * supported by hosts. This is only a problem if we are reading
485 * this value later and expecting what we uploaded back.
490 gmr->header = SVGA_CMD_DEFINE_GMRFB;
492 gmr->body.format.bitsPerPixel = update->vfb->base.format->cpp[0] * 8;
493 gmr->body.format.colorDepth = depth;
494 gmr->body.format.reserved = 0;
495 gmr->body.bytesPerLine = update->vfb->base.pitches[0];
496 vmw_bo_get_guest_ptr(&vfbbo->buffer->base, &gmr->body.ptr);
501 static uint32_t vmw_sou_bo_populate_clip(struct vmw_du_update_plane *update,
502 void *cmd, struct drm_rect *clip,
503 uint32_t fb_x, uint32_t fb_y)
505 struct vmw_kms_sou_bo_blit *blit = cmd;
507 blit->header = SVGA_CMD_BLIT_GMRFB_TO_SCREEN;
508 blit->body.destScreenId = update->du->unit;
509 blit->body.srcOrigin.x = fb_x;
510 blit->body.srcOrigin.y = fb_y;
511 blit->body.destRect.left = clip->x1;
512 blit->body.destRect.top = clip->y1;
513 blit->body.destRect.right = clip->x2;
514 blit->body.destRect.bottom = clip->y2;
516 return sizeof(*blit);
519 static uint32_t vmw_stud_bo_post_clip(struct vmw_du_update_plane *update,
520 void *cmd, struct drm_rect *bb)
526 * vmw_sou_plane_update_bo - Update display unit for bo backed fb.
527 * @dev_priv: Device private.
528 * @plane: Plane state.
529 * @old_state: Old plane state.
530 * @vfb: Framebuffer which is blitted to display unit.
531 * @out_fence: If non-NULL, will return a ref-counted pointer to vmw_fence_obj.
532 * The returned fence pointer may be NULL in which case the device
533 * has already synchronized.
535 * Return: 0 on success or a negative error code on failure.
537 static int vmw_sou_plane_update_bo(struct vmw_private *dev_priv,
538 struct drm_plane *plane,
539 struct drm_plane_state *old_state,
540 struct vmw_framebuffer *vfb,
541 struct vmw_fence_obj **out_fence)
543 struct vmw_du_update_plane_buffer bo_update;
545 memset(&bo_update, 0, sizeof(struct vmw_du_update_plane_buffer));
546 bo_update.base.plane = plane;
547 bo_update.base.old_state = old_state;
548 bo_update.base.dev_priv = dev_priv;
549 bo_update.base.du = vmw_crtc_to_du(plane->state->crtc);
550 bo_update.base.vfb = vfb;
551 bo_update.base.out_fence = out_fence;
552 bo_update.base.mutex = NULL;
553 bo_update.base.cpu_blit = false;
554 bo_update.base.intr = true;
556 bo_update.base.calc_fifo_size = vmw_sou_bo_fifo_size;
557 bo_update.base.post_prepare = vmw_sou_bo_define_gmrfb;
558 bo_update.base.clip = vmw_sou_bo_populate_clip;
559 bo_update.base.post_clip = vmw_stud_bo_post_clip;
561 return vmw_du_helper_plane_update(&bo_update.base);
564 static uint32_t vmw_sou_surface_fifo_size(struct vmw_du_update_plane *update,
567 return sizeof(struct vmw_kms_sou_dirty_cmd) + sizeof(SVGASignedRect) *
571 static uint32_t vmw_sou_surface_post_prepare(struct vmw_du_update_plane *update,
574 struct vmw_du_update_plane_surface *srf_update;
576 srf_update = container_of(update, typeof(*srf_update), base);
579 * SOU SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN is special in the sense that
580 * its bounding box is filled before iterating over all the clips. So
581 * store the FIFO start address and revisit to fill the details.
583 srf_update->cmd_start = cmd;
588 static uint32_t vmw_sou_surface_pre_clip(struct vmw_du_update_plane *update,
589 void *cmd, uint32_t num_hits)
591 struct vmw_kms_sou_dirty_cmd *blit = cmd;
592 struct vmw_framebuffer_surface *vfbs;
594 vfbs = container_of(update->vfb, typeof(*vfbs), base);
596 blit->header.id = SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN;
597 blit->header.size = sizeof(blit->body) + sizeof(SVGASignedRect) *
600 blit->body.srcImage.sid = vfbs->surface->res.id;
601 blit->body.destScreenId = update->du->unit;
603 /* Update the source and destination bounding box later in post_clip */
604 blit->body.srcRect.left = 0;
605 blit->body.srcRect.top = 0;
606 blit->body.srcRect.right = 0;
607 blit->body.srcRect.bottom = 0;
609 blit->body.destRect.left = 0;
610 blit->body.destRect.top = 0;
611 blit->body.destRect.right = 0;
612 blit->body.destRect.bottom = 0;
614 return sizeof(*blit);
617 static uint32_t vmw_sou_surface_clip_rect(struct vmw_du_update_plane *update,
618 void *cmd, struct drm_rect *clip,
619 uint32_t src_x, uint32_t src_y)
621 SVGASignedRect *rect = cmd;
624 * rects are relative to dest bounding box rect on screen object, so
625 * translate to it later in post_clip
627 rect->left = clip->x1;
628 rect->top = clip->y1;
629 rect->right = clip->x2;
630 rect->bottom = clip->y2;
632 return sizeof(*rect);
635 static uint32_t vmw_sou_surface_post_clip(struct vmw_du_update_plane *update,
636 void *cmd, struct drm_rect *bb)
638 struct vmw_du_update_plane_surface *srf_update;
639 struct drm_plane_state *state = update->plane->state;
640 struct drm_rect src_bb;
641 struct vmw_kms_sou_dirty_cmd *blit;
642 SVGASignedRect *rect;
648 srf_update = container_of(update, typeof(*srf_update), base);
650 blit = srf_update->cmd_start;
651 rect = (SVGASignedRect *)&blit[1];
653 num_hits = (blit->header.size - sizeof(blit->body))/
654 sizeof(SVGASignedRect);
658 /* To translate bb back to fb src coord */
659 translate_src_x = (state->src_x >> 16) - state->crtc_x;
660 translate_src_y = (state->src_y >> 16) - state->crtc_y;
662 drm_rect_translate(&src_bb, translate_src_x, translate_src_y);
664 blit->body.srcRect.left = src_bb.x1;
665 blit->body.srcRect.top = src_bb.y1;
666 blit->body.srcRect.right = src_bb.x2;
667 blit->body.srcRect.bottom = src_bb.y2;
669 blit->body.destRect.left = bb->x1;
670 blit->body.destRect.top = bb->y1;
671 blit->body.destRect.right = bb->x2;
672 blit->body.destRect.bottom = bb->y2;
674 /* rects are relative to dest bb rect */
675 for (i = 0; i < num_hits; i++) {
676 rect->left -= bb->x1;
678 rect->right -= bb->x1;
679 rect->bottom -= bb->y1;
687 * vmw_sou_plane_update_surface - Update display unit for surface backed fb.
688 * @dev_priv: Device private.
689 * @plane: Plane state.
690 * @old_state: Old plane state.
691 * @vfb: Framebuffer which is blitted to display unit
692 * @out_fence: If non-NULL, will return a ref-counted pointer to vmw_fence_obj.
693 * The returned fence pointer may be NULL in which case the device
694 * has already synchronized.
696 * Return: 0 on success or a negative error code on failure.
698 static int vmw_sou_plane_update_surface(struct vmw_private *dev_priv,
699 struct drm_plane *plane,
700 struct drm_plane_state *old_state,
701 struct vmw_framebuffer *vfb,
702 struct vmw_fence_obj **out_fence)
704 struct vmw_du_update_plane_surface srf_update;
706 memset(&srf_update, 0, sizeof(struct vmw_du_update_plane_surface));
707 srf_update.base.plane = plane;
708 srf_update.base.old_state = old_state;
709 srf_update.base.dev_priv = dev_priv;
710 srf_update.base.du = vmw_crtc_to_du(plane->state->crtc);
711 srf_update.base.vfb = vfb;
712 srf_update.base.out_fence = out_fence;
713 srf_update.base.mutex = &dev_priv->cmdbuf_mutex;
714 srf_update.base.cpu_blit = false;
715 srf_update.base.intr = true;
717 srf_update.base.calc_fifo_size = vmw_sou_surface_fifo_size;
718 srf_update.base.post_prepare = vmw_sou_surface_post_prepare;
719 srf_update.base.pre_clip = vmw_sou_surface_pre_clip;
720 srf_update.base.clip = vmw_sou_surface_clip_rect;
721 srf_update.base.post_clip = vmw_sou_surface_post_clip;
723 return vmw_du_helper_plane_update(&srf_update.base);
727 vmw_sou_primary_plane_atomic_update(struct drm_plane *plane,
728 struct drm_atomic_state *state)
730 struct drm_plane_state *old_state = drm_atomic_get_old_plane_state(state, plane);
731 struct drm_plane_state *new_state = drm_atomic_get_new_plane_state(state, plane);
732 struct drm_crtc *crtc = new_state->crtc;
733 struct drm_pending_vblank_event *event = NULL;
734 struct vmw_fence_obj *fence = NULL;
737 /* In case of device error, maintain consistent atomic state */
738 if (crtc && new_state->fb) {
739 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
740 struct vmw_framebuffer *vfb =
741 vmw_framebuffer_to_vfb(new_state->fb);
744 ret = vmw_sou_plane_update_bo(dev_priv, plane,
745 old_state, vfb, &fence);
747 ret = vmw_sou_plane_update_surface(dev_priv, plane,
751 DRM_ERROR("Failed to update screen.\n");
753 /* Do nothing when fb and crtc is NULL (blank crtc) */
757 /* For error case vblank event is send from vmw_du_crtc_atomic_flush */
758 event = crtc->state->event;
759 if (event && fence) {
760 struct drm_file *file_priv = event->base.file_priv;
762 ret = vmw_event_fence_action_queue(file_priv,
765 &event->event.vbl.tv_sec,
766 &event->event.vbl.tv_usec,
769 if (unlikely(ret != 0))
770 DRM_ERROR("Failed to queue event on fence.\n");
772 crtc->state->event = NULL;
776 vmw_fence_obj_unreference(&fence);
780 static const struct drm_plane_funcs vmw_sou_plane_funcs = {
781 .update_plane = drm_atomic_helper_update_plane,
782 .disable_plane = drm_atomic_helper_disable_plane,
783 .destroy = vmw_du_primary_plane_destroy,
784 .reset = vmw_du_plane_reset,
785 .atomic_duplicate_state = vmw_du_plane_duplicate_state,
786 .atomic_destroy_state = vmw_du_plane_destroy_state,
789 static const struct drm_plane_funcs vmw_sou_cursor_funcs = {
790 .update_plane = drm_atomic_helper_update_plane,
791 .disable_plane = drm_atomic_helper_disable_plane,
792 .destroy = vmw_du_cursor_plane_destroy,
793 .reset = vmw_du_plane_reset,
794 .atomic_duplicate_state = vmw_du_plane_duplicate_state,
795 .atomic_destroy_state = vmw_du_plane_destroy_state,
802 drm_plane_helper_funcs vmw_sou_cursor_plane_helper_funcs = {
803 .atomic_check = vmw_du_cursor_plane_atomic_check,
804 .atomic_update = vmw_du_cursor_plane_atomic_update,
805 .prepare_fb = vmw_du_cursor_plane_prepare_fb,
806 .cleanup_fb = vmw_du_cursor_plane_cleanup_fb,
810 drm_plane_helper_funcs vmw_sou_primary_plane_helper_funcs = {
811 .atomic_check = vmw_du_primary_plane_atomic_check,
812 .atomic_update = vmw_sou_primary_plane_atomic_update,
813 .prepare_fb = vmw_sou_primary_plane_prepare_fb,
814 .cleanup_fb = vmw_sou_primary_plane_cleanup_fb,
817 static const struct drm_crtc_helper_funcs vmw_sou_crtc_helper_funcs = {
818 .prepare = vmw_sou_crtc_helper_prepare,
819 .mode_set_nofb = vmw_sou_crtc_mode_set_nofb,
820 .atomic_check = vmw_du_crtc_atomic_check,
821 .atomic_begin = vmw_du_crtc_atomic_begin,
822 .atomic_flush = vmw_du_crtc_atomic_flush,
823 .atomic_enable = vmw_sou_crtc_atomic_enable,
824 .atomic_disable = vmw_sou_crtc_atomic_disable,
828 static int vmw_sou_init(struct vmw_private *dev_priv, unsigned unit)
830 struct vmw_screen_object_unit *sou;
831 struct drm_device *dev = &dev_priv->drm;
832 struct drm_connector *connector;
833 struct drm_encoder *encoder;
834 struct drm_plane *primary;
835 struct vmw_cursor_plane *cursor;
836 struct drm_crtc *crtc;
839 sou = kzalloc(sizeof(*sou), GFP_KERNEL);
843 sou->base.unit = unit;
844 crtc = &sou->base.crtc;
845 encoder = &sou->base.encoder;
846 connector = &sou->base.connector;
847 primary = &sou->base.primary;
848 cursor = &sou->base.cursor;
850 sou->base.pref_active = (unit == 0);
851 sou->base.pref_width = dev_priv->initial_width;
852 sou->base.pref_height = dev_priv->initial_height;
853 sou->base.pref_mode = NULL;
856 * Remove this after enabling atomic because property values can
857 * only exist in a state object
859 sou->base.is_implicit = false;
861 /* Initialize primary plane */
862 ret = drm_universal_plane_init(dev, primary,
863 0, &vmw_sou_plane_funcs,
864 vmw_primary_plane_formats,
865 ARRAY_SIZE(vmw_primary_plane_formats),
866 NULL, DRM_PLANE_TYPE_PRIMARY, NULL);
868 DRM_ERROR("Failed to initialize primary plane");
872 drm_plane_helper_add(primary, &vmw_sou_primary_plane_helper_funcs);
873 drm_plane_enable_fb_damage_clips(primary);
875 /* Initialize cursor plane */
876 ret = drm_universal_plane_init(dev, &cursor->base,
877 0, &vmw_sou_cursor_funcs,
878 vmw_cursor_plane_formats,
879 ARRAY_SIZE(vmw_cursor_plane_formats),
880 NULL, DRM_PLANE_TYPE_CURSOR, NULL);
882 DRM_ERROR("Failed to initialize cursor plane");
883 drm_plane_cleanup(&sou->base.primary);
887 drm_plane_helper_add(&cursor->base, &vmw_sou_cursor_plane_helper_funcs);
889 ret = drm_connector_init(dev, connector, &vmw_sou_connector_funcs,
890 DRM_MODE_CONNECTOR_VIRTUAL);
892 DRM_ERROR("Failed to initialize connector\n");
896 drm_connector_helper_add(connector, &vmw_sou_connector_helper_funcs);
897 connector->status = vmw_du_connector_detect(connector, true);
899 ret = drm_encoder_init(dev, encoder, &vmw_screen_object_encoder_funcs,
900 DRM_MODE_ENCODER_VIRTUAL, NULL);
902 DRM_ERROR("Failed to initialize encoder\n");
903 goto err_free_connector;
906 (void) drm_connector_attach_encoder(connector, encoder);
907 encoder->possible_crtcs = (1 << unit);
908 encoder->possible_clones = 0;
910 ret = drm_connector_register(connector);
912 DRM_ERROR("Failed to register connector\n");
913 goto err_free_encoder;
916 ret = drm_crtc_init_with_planes(dev, crtc, primary,
918 &vmw_screen_object_crtc_funcs, NULL);
920 DRM_ERROR("Failed to initialize CRTC\n");
921 goto err_free_unregister;
924 drm_crtc_helper_add(crtc, &vmw_sou_crtc_helper_funcs);
926 drm_mode_crtc_set_gamma_size(crtc, 256);
928 drm_object_attach_property(&connector->base,
929 dev_priv->hotplug_mode_update_property, 1);
930 drm_object_attach_property(&connector->base,
931 dev->mode_config.suggested_x_property, 0);
932 drm_object_attach_property(&connector->base,
933 dev->mode_config.suggested_y_property, 0);
937 drm_connector_unregister(connector);
939 drm_encoder_cleanup(encoder);
941 drm_connector_cleanup(connector);
947 int vmw_kms_sou_init_display(struct vmw_private *dev_priv)
949 struct drm_device *dev = &dev_priv->drm;
952 if (!(dev_priv->capabilities & SVGA_CAP_SCREEN_OBJECT_2)) {
958 ret = drm_vblank_init(dev, VMWGFX_NUM_DISPLAY_UNITS);
959 if (unlikely(ret != 0))
962 for (i = 0; i < VMWGFX_NUM_DISPLAY_UNITS; ++i)
963 vmw_sou_init(dev_priv, i);
965 dev_priv->active_display_unit = vmw_du_screen_object;
967 drm_mode_config_reset(dev);
972 static int do_bo_define_gmrfb(struct vmw_private *dev_priv,
973 struct vmw_framebuffer *framebuffer)
975 struct vmw_buffer_object *buf =
976 container_of(framebuffer, struct vmw_framebuffer_bo,
978 int depth = framebuffer->base.format->depth;
981 SVGAFifoCmdDefineGMRFB body;
984 /* Emulate RGBA support, contrary to svga_reg.h this is not
985 * supported by hosts. This is only a problem if we are reading
986 * this value later and expecting what we uploaded back.
991 cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd));
995 cmd->header = SVGA_CMD_DEFINE_GMRFB;
996 cmd->body.format.bitsPerPixel = framebuffer->base.format->cpp[0] * 8;
997 cmd->body.format.colorDepth = depth;
998 cmd->body.format.reserved = 0;
999 cmd->body.bytesPerLine = framebuffer->base.pitches[0];
1000 /* Buffer is reserved in vram or GMR */
1001 vmw_bo_get_guest_ptr(&buf->base, &cmd->body.ptr);
1002 vmw_cmd_commit(dev_priv, sizeof(*cmd));
1008 * vmw_sou_surface_fifo_commit - Callback to fill in and submit a
1009 * blit surface to screen command.
1011 * @dirty: The closure structure.
1013 * Fills in the missing fields in the command, and translates the cliprects
1014 * to match the destination bounding box encoded.
1016 static void vmw_sou_surface_fifo_commit(struct vmw_kms_dirty *dirty)
1018 struct vmw_kms_sou_surface_dirty *sdirty =
1019 container_of(dirty, typeof(*sdirty), base);
1020 struct vmw_kms_sou_dirty_cmd *cmd = dirty->cmd;
1021 s32 trans_x = dirty->unit->crtc.x - sdirty->dst_x;
1022 s32 trans_y = dirty->unit->crtc.y - sdirty->dst_y;
1023 size_t region_size = dirty->num_hits * sizeof(SVGASignedRect);
1024 SVGASignedRect *blit = (SVGASignedRect *) &cmd[1];
1027 if (!dirty->num_hits) {
1028 vmw_cmd_commit(dirty->dev_priv, 0);
1032 cmd->header.id = SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN;
1033 cmd->header.size = sizeof(cmd->body) + region_size;
1036 * Use the destination bounding box to specify destination - and
1037 * source bounding regions.
1039 cmd->body.destRect.left = sdirty->left;
1040 cmd->body.destRect.right = sdirty->right;
1041 cmd->body.destRect.top = sdirty->top;
1042 cmd->body.destRect.bottom = sdirty->bottom;
1044 cmd->body.srcRect.left = sdirty->left + trans_x;
1045 cmd->body.srcRect.right = sdirty->right + trans_x;
1046 cmd->body.srcRect.top = sdirty->top + trans_y;
1047 cmd->body.srcRect.bottom = sdirty->bottom + trans_y;
1049 cmd->body.srcImage.sid = sdirty->sid;
1050 cmd->body.destScreenId = dirty->unit->unit;
1052 /* Blits are relative to the destination rect. Translate. */
1053 for (i = 0; i < dirty->num_hits; ++i, ++blit) {
1054 blit->left -= sdirty->left;
1055 blit->right -= sdirty->left;
1056 blit->top -= sdirty->top;
1057 blit->bottom -= sdirty->top;
1060 vmw_cmd_commit(dirty->dev_priv, region_size + sizeof(*cmd));
1062 sdirty->left = sdirty->top = S32_MAX;
1063 sdirty->right = sdirty->bottom = S32_MIN;
1067 * vmw_sou_surface_clip - Callback to encode a blit surface to screen cliprect.
1069 * @dirty: The closure structure
1071 * Encodes a SVGASignedRect cliprect and updates the bounding box of the
1072 * BLIT_SURFACE_TO_SCREEN command.
1074 static void vmw_sou_surface_clip(struct vmw_kms_dirty *dirty)
1076 struct vmw_kms_sou_surface_dirty *sdirty =
1077 container_of(dirty, typeof(*sdirty), base);
1078 struct vmw_kms_sou_dirty_cmd *cmd = dirty->cmd;
1079 SVGASignedRect *blit = (SVGASignedRect *) &cmd[1];
1081 /* Destination rect. */
1082 blit += dirty->num_hits;
1083 blit->left = dirty->unit_x1;
1084 blit->top = dirty->unit_y1;
1085 blit->right = dirty->unit_x2;
1086 blit->bottom = dirty->unit_y2;
1088 /* Destination bounding box */
1089 sdirty->left = min_t(s32, sdirty->left, dirty->unit_x1);
1090 sdirty->top = min_t(s32, sdirty->top, dirty->unit_y1);
1091 sdirty->right = max_t(s32, sdirty->right, dirty->unit_x2);
1092 sdirty->bottom = max_t(s32, sdirty->bottom, dirty->unit_y2);
1098 * vmw_kms_sou_do_surface_dirty - Dirty part of a surface backed framebuffer
1100 * @dev_priv: Pointer to the device private structure.
1101 * @framebuffer: Pointer to the surface-buffer backed framebuffer.
1102 * @clips: Array of clip rects. Either @clips or @vclips must be NULL.
1103 * @vclips: Alternate array of clip rects. Either @clips or @vclips must
1105 * @srf: Pointer to surface to blit from. If NULL, the surface attached
1106 * to @framebuffer will be used.
1107 * @dest_x: X coordinate offset to align @srf with framebuffer coordinates.
1108 * @dest_y: Y coordinate offset to align @srf with framebuffer coordinates.
1109 * @num_clips: Number of clip rects in @clips.
1110 * @inc: Increment to use when looping over @clips.
1111 * @out_fence: If non-NULL, will return a ref-counted pointer to a
1112 * struct vmw_fence_obj. The returned fence pointer may be NULL in which
1113 * case the device has already synchronized.
1114 * @crtc: If crtc is passed, perform surface dirty on that crtc only.
1116 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
1119 int vmw_kms_sou_do_surface_dirty(struct vmw_private *dev_priv,
1120 struct vmw_framebuffer *framebuffer,
1121 struct drm_clip_rect *clips,
1122 struct drm_vmw_rect *vclips,
1123 struct vmw_resource *srf,
1126 unsigned num_clips, int inc,
1127 struct vmw_fence_obj **out_fence,
1128 struct drm_crtc *crtc)
1130 struct vmw_framebuffer_surface *vfbs =
1131 container_of(framebuffer, typeof(*vfbs), base);
1132 struct vmw_kms_sou_surface_dirty sdirty;
1133 DECLARE_VAL_CONTEXT(val_ctx, NULL, 0);
1137 srf = &vfbs->surface->res;
1139 ret = vmw_validation_add_resource(&val_ctx, srf, 0, VMW_RES_DIRTY_NONE,
1144 ret = vmw_validation_prepare(&val_ctx, &dev_priv->cmdbuf_mutex, true);
1148 sdirty.base.fifo_commit = vmw_sou_surface_fifo_commit;
1149 sdirty.base.clip = vmw_sou_surface_clip;
1150 sdirty.base.dev_priv = dev_priv;
1151 sdirty.base.fifo_reserve_size = sizeof(struct vmw_kms_sou_dirty_cmd) +
1152 sizeof(SVGASignedRect) * num_clips;
1153 sdirty.base.crtc = crtc;
1155 sdirty.sid = srf->id;
1156 sdirty.left = sdirty.top = S32_MAX;
1157 sdirty.right = sdirty.bottom = S32_MIN;
1158 sdirty.dst_x = dest_x;
1159 sdirty.dst_y = dest_y;
1161 ret = vmw_kms_helper_dirty(dev_priv, framebuffer, clips, vclips,
1162 dest_x, dest_y, num_clips, inc,
1164 vmw_kms_helper_validation_finish(dev_priv, NULL, &val_ctx, out_fence,
1170 vmw_validation_unref_lists(&val_ctx);
1175 * vmw_sou_bo_fifo_commit - Callback to submit a set of readback clips.
1177 * @dirty: The closure structure.
1179 * Commits a previously built command buffer of readback clips.
1181 static void vmw_sou_bo_fifo_commit(struct vmw_kms_dirty *dirty)
1183 if (!dirty->num_hits) {
1184 vmw_cmd_commit(dirty->dev_priv, 0);
1188 vmw_cmd_commit(dirty->dev_priv,
1189 sizeof(struct vmw_kms_sou_bo_blit) *
1194 * vmw_sou_bo_clip - Callback to encode a readback cliprect.
1196 * @dirty: The closure structure
1198 * Encodes a BLIT_GMRFB_TO_SCREEN cliprect.
1200 static void vmw_sou_bo_clip(struct vmw_kms_dirty *dirty)
1202 struct vmw_kms_sou_bo_blit *blit = dirty->cmd;
1204 blit += dirty->num_hits;
1205 blit->header = SVGA_CMD_BLIT_GMRFB_TO_SCREEN;
1206 blit->body.destScreenId = dirty->unit->unit;
1207 blit->body.srcOrigin.x = dirty->fb_x;
1208 blit->body.srcOrigin.y = dirty->fb_y;
1209 blit->body.destRect.left = dirty->unit_x1;
1210 blit->body.destRect.top = dirty->unit_y1;
1211 blit->body.destRect.right = dirty->unit_x2;
1212 blit->body.destRect.bottom = dirty->unit_y2;
1217 * vmw_kms_sou_do_bo_dirty - Dirty part of a buffer-object backed framebuffer
1219 * @dev_priv: Pointer to the device private structure.
1220 * @framebuffer: Pointer to the buffer-object backed framebuffer.
1221 * @clips: Array of clip rects.
1222 * @vclips: Alternate array of clip rects. Either @clips or @vclips must
1224 * @num_clips: Number of clip rects in @clips.
1225 * @increment: Increment to use when looping over @clips.
1226 * @interruptible: Whether to perform waits interruptible if possible.
1227 * @out_fence: If non-NULL, will return a ref-counted pointer to a
1228 * struct vmw_fence_obj. The returned fence pointer may be NULL in which
1229 * case the device has already synchronized.
1230 * @crtc: If crtc is passed, perform bo dirty on that crtc only.
1232 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
1235 int vmw_kms_sou_do_bo_dirty(struct vmw_private *dev_priv,
1236 struct vmw_framebuffer *framebuffer,
1237 struct drm_clip_rect *clips,
1238 struct drm_vmw_rect *vclips,
1239 unsigned num_clips, int increment,
1241 struct vmw_fence_obj **out_fence,
1242 struct drm_crtc *crtc)
1244 struct vmw_buffer_object *buf =
1245 container_of(framebuffer, struct vmw_framebuffer_bo,
1247 struct vmw_kms_dirty dirty;
1248 DECLARE_VAL_CONTEXT(val_ctx, NULL, 0);
1251 ret = vmw_validation_add_bo(&val_ctx, buf, false, false);
1255 ret = vmw_validation_prepare(&val_ctx, NULL, interruptible);
1259 ret = do_bo_define_gmrfb(dev_priv, framebuffer);
1260 if (unlikely(ret != 0))
1264 dirty.fifo_commit = vmw_sou_bo_fifo_commit;
1265 dirty.clip = vmw_sou_bo_clip;
1266 dirty.fifo_reserve_size = sizeof(struct vmw_kms_sou_bo_blit) *
1268 ret = vmw_kms_helper_dirty(dev_priv, framebuffer, clips, vclips,
1269 0, 0, num_clips, increment, &dirty);
1270 vmw_kms_helper_validation_finish(dev_priv, NULL, &val_ctx, out_fence,
1276 vmw_validation_revert(&val_ctx);
1278 vmw_validation_unref_lists(&val_ctx);
1285 * vmw_sou_readback_fifo_commit - Callback to submit a set of readback clips.
1287 * @dirty: The closure structure.
1289 * Commits a previously built command buffer of readback clips.
1291 static void vmw_sou_readback_fifo_commit(struct vmw_kms_dirty *dirty)
1293 if (!dirty->num_hits) {
1294 vmw_cmd_commit(dirty->dev_priv, 0);
1298 vmw_cmd_commit(dirty->dev_priv,
1299 sizeof(struct vmw_kms_sou_readback_blit) *
1304 * vmw_sou_readback_clip - Callback to encode a readback cliprect.
1306 * @dirty: The closure structure
1308 * Encodes a BLIT_SCREEN_TO_GMRFB cliprect.
1310 static void vmw_sou_readback_clip(struct vmw_kms_dirty *dirty)
1312 struct vmw_kms_sou_readback_blit *blit = dirty->cmd;
1314 blit += dirty->num_hits;
1315 blit->header = SVGA_CMD_BLIT_SCREEN_TO_GMRFB;
1316 blit->body.srcScreenId = dirty->unit->unit;
1317 blit->body.destOrigin.x = dirty->fb_x;
1318 blit->body.destOrigin.y = dirty->fb_y;
1319 blit->body.srcRect.left = dirty->unit_x1;
1320 blit->body.srcRect.top = dirty->unit_y1;
1321 blit->body.srcRect.right = dirty->unit_x2;
1322 blit->body.srcRect.bottom = dirty->unit_y2;
1327 * vmw_kms_sou_readback - Perform a readback from the screen object system to
1328 * a buffer-object backed framebuffer.
1330 * @dev_priv: Pointer to the device private structure.
1331 * @file_priv: Pointer to a struct drm_file identifying the caller.
1332 * Must be set to NULL if @user_fence_rep is NULL.
1333 * @vfb: Pointer to the buffer-object backed framebuffer.
1334 * @user_fence_rep: User-space provided structure for fence information.
1335 * Must be set to non-NULL if @file_priv is non-NULL.
1336 * @vclips: Array of clip rects.
1337 * @num_clips: Number of clip rects in @vclips.
1338 * @crtc: If crtc is passed, readback on that crtc only.
1340 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
1343 int vmw_kms_sou_readback(struct vmw_private *dev_priv,
1344 struct drm_file *file_priv,
1345 struct vmw_framebuffer *vfb,
1346 struct drm_vmw_fence_rep __user *user_fence_rep,
1347 struct drm_vmw_rect *vclips,
1349 struct drm_crtc *crtc)
1351 struct vmw_buffer_object *buf =
1352 container_of(vfb, struct vmw_framebuffer_bo, base)->buffer;
1353 struct vmw_kms_dirty dirty;
1354 DECLARE_VAL_CONTEXT(val_ctx, NULL, 0);
1357 ret = vmw_validation_add_bo(&val_ctx, buf, false, false);
1361 ret = vmw_validation_prepare(&val_ctx, NULL, true);
1365 ret = do_bo_define_gmrfb(dev_priv, vfb);
1366 if (unlikely(ret != 0))
1370 dirty.fifo_commit = vmw_sou_readback_fifo_commit;
1371 dirty.clip = vmw_sou_readback_clip;
1372 dirty.fifo_reserve_size = sizeof(struct vmw_kms_sou_readback_blit) *
1374 ret = vmw_kms_helper_dirty(dev_priv, vfb, NULL, vclips,
1375 0, 0, num_clips, 1, &dirty);
1376 vmw_kms_helper_validation_finish(dev_priv, file_priv, &val_ctx, NULL,
1382 vmw_validation_revert(&val_ctx);
1384 vmw_validation_unref_lists(&val_ctx);