1 /**************************************************************************
3 * Copyright © 2011-2015 VMware, Inc., Palo Alto, CA., USA
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 * USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
28 #include "vmwgfx_kms.h"
29 #include <drm/drm_plane_helper.h>
30 #include <drm/drm_atomic.h>
31 #include <drm/drm_atomic_helper.h>
34 #define vmw_crtc_to_sou(x) \
35 container_of(x, struct vmw_screen_object_unit, base.crtc)
36 #define vmw_encoder_to_sou(x) \
37 container_of(x, struct vmw_screen_object_unit, base.encoder)
38 #define vmw_connector_to_sou(x) \
39 container_of(x, struct vmw_screen_object_unit, base.connector)
42 * struct vmw_kms_sou_surface_dirty - Closure structure for
43 * blit surface to screen command.
44 * @base: The base type we derive from. Used by vmw_kms_helper_dirty().
45 * @left: Left side of bounding box.
46 * @right: Right side of bounding box.
47 * @top: Top side of bounding box.
48 * @bottom: Bottom side of bounding box.
49 * @dst_x: Difference between source clip rects and framebuffer coordinates.
50 * @dst_y: Difference between source clip rects and framebuffer coordinates.
51 * @sid: Surface id of surface to copy from.
53 struct vmw_kms_sou_surface_dirty
{
54 struct vmw_kms_dirty base
;
55 s32 left
, right
, top
, bottom
;
61 * SVGA commands that are used by this code. Please see the device headers
64 struct vmw_kms_sou_readback_blit
{
66 SVGAFifoCmdBlitScreenToGMRFB body
;
69 struct vmw_kms_sou_dmabuf_blit
{
71 SVGAFifoCmdBlitGMRFBToScreen body
;
74 struct vmw_kms_sou_dirty_cmd
{
75 SVGA3dCmdHeader header
;
76 SVGA3dCmdBlitSurfaceToScreen body
;
80 * Display unit using screen objects.
82 struct vmw_screen_object_unit
{
83 struct vmw_display_unit base
;
85 unsigned long buffer_size
; /**< Size of allocated buffer */
86 struct vmw_dma_buffer
*buffer
; /**< Backing store buffer */
91 static void vmw_sou_destroy(struct vmw_screen_object_unit
*sou
)
93 vmw_du_cleanup(&sou
->base
);
99 * Screen Object Display Unit CRTC functions
102 static void vmw_sou_crtc_destroy(struct drm_crtc
*crtc
)
104 vmw_sou_destroy(vmw_crtc_to_sou(crtc
));
108 * Send the fifo command to create a screen.
110 static int vmw_sou_fifo_create(struct vmw_private
*dev_priv
,
111 struct vmw_screen_object_unit
*sou
,
112 uint32_t x
, uint32_t y
,
113 struct drm_display_mode
*mode
)
121 SVGAScreenObject obj
;
124 BUG_ON(!sou
->buffer
);
126 fifo_size
= sizeof(*cmd
);
127 cmd
= vmw_fifo_reserve(dev_priv
, fifo_size
);
128 /* The hardware has hung, nothing we can do about it here. */
129 if (unlikely(cmd
== NULL
)) {
130 DRM_ERROR("Fifo reserve failed.\n");
134 memset(cmd
, 0, fifo_size
);
135 cmd
->header
.cmdType
= SVGA_CMD_DEFINE_SCREEN
;
136 cmd
->obj
.structSize
= sizeof(SVGAScreenObject
);
137 cmd
->obj
.id
= sou
->base
.unit
;
138 cmd
->obj
.flags
= SVGA_SCREEN_HAS_ROOT
|
139 (sou
->base
.unit
== 0 ? SVGA_SCREEN_IS_PRIMARY
: 0);
140 cmd
->obj
.size
.width
= mode
->hdisplay
;
141 cmd
->obj
.size
.height
= mode
->vdisplay
;
142 if (sou
->base
.is_implicit
) {
146 cmd
->obj
.root
.x
= sou
->base
.gui_x
;
147 cmd
->obj
.root
.y
= sou
->base
.gui_y
;
149 sou
->base
.set_gui_x
= cmd
->obj
.root
.x
;
150 sou
->base
.set_gui_y
= cmd
->obj
.root
.y
;
152 /* Ok to assume that buffer is pinned in vram */
153 vmw_bo_get_guest_ptr(&sou
->buffer
->base
, &cmd
->obj
.backingStore
.ptr
);
154 cmd
->obj
.backingStore
.pitch
= mode
->hdisplay
* 4;
156 vmw_fifo_commit(dev_priv
, fifo_size
);
164 * Send the fifo command to destroy a screen.
166 static int vmw_sou_fifo_destroy(struct vmw_private
*dev_priv
,
167 struct vmw_screen_object_unit
*sou
)
176 SVGAFifoCmdDestroyScreen body
;
179 /* no need to do anything */
180 if (unlikely(!sou
->defined
))
183 fifo_size
= sizeof(*cmd
);
184 cmd
= vmw_fifo_reserve(dev_priv
, fifo_size
);
185 /* the hardware has hung, nothing we can do about it here */
186 if (unlikely(cmd
== NULL
)) {
187 DRM_ERROR("Fifo reserve failed.\n");
191 memset(cmd
, 0, fifo_size
);
192 cmd
->header
.cmdType
= SVGA_CMD_DESTROY_SCREEN
;
193 cmd
->body
.screenId
= sou
->base
.unit
;
195 vmw_fifo_commit(dev_priv
, fifo_size
);
198 ret
= vmw_fallback_wait(dev_priv
, false, true, 0, false, 3*HZ
);
199 if (unlikely(ret
!= 0))
200 DRM_ERROR("Failed to sync with HW");
202 sou
->defined
= false;
208 * vmw_sou_crtc_mode_set_nofb - Create new screen
210 * @crtc: CRTC associated with the new screen
212 * This function creates/destroys a screen. This function cannot fail, so if
213 * somehow we run into a failure, just do the best we can to get out.
215 static void vmw_sou_crtc_mode_set_nofb(struct drm_crtc
*crtc
)
217 struct vmw_private
*dev_priv
;
218 struct vmw_screen_object_unit
*sou
;
219 struct vmw_framebuffer
*vfb
;
220 struct drm_framebuffer
*fb
;
221 struct drm_plane_state
*ps
;
222 struct vmw_plane_state
*vps
;
226 sou
= vmw_crtc_to_sou(crtc
);
227 dev_priv
= vmw_priv(crtc
->dev
);
228 ps
= crtc
->primary
->state
;
230 vps
= vmw_plane_state_to_vps(ps
);
232 vfb
= (fb
) ? vmw_framebuffer_to_vfb(fb
) : NULL
;
235 ret
= vmw_sou_fifo_destroy(dev_priv
, sou
);
237 DRM_ERROR("Failed to destroy Screen Object\n");
243 sou
->buffer
= vps
->dmabuf
;
244 sou
->buffer_size
= vps
->dmabuf_size
;
246 ret
= vmw_sou_fifo_create(dev_priv
, sou
, crtc
->x
, crtc
->y
,
249 DRM_ERROR("Failed to define Screen Object %dx%d\n",
252 vmw_kms_add_active(dev_priv
, &sou
->base
, vfb
);
255 sou
->buffer_size
= 0;
257 vmw_kms_del_active(dev_priv
, &sou
->base
);
262 * vmw_sou_crtc_helper_prepare - Noop
264 * @crtc: CRTC associated with the new screen
266 * Prepares the CRTC for a mode set, but we don't need to do anything here.
268 static void vmw_sou_crtc_helper_prepare(struct drm_crtc
*crtc
)
273 * vmw_sou_crtc_atomic_enable - Noop
275 * @crtc: CRTC associated with the new screen
277 * This is called after a mode set has been completed.
279 static void vmw_sou_crtc_atomic_enable(struct drm_crtc
*crtc
,
280 struct drm_crtc_state
*old_state
)
285 * vmw_sou_crtc_atomic_disable - Turns off CRTC
287 * @crtc: CRTC to be turned off
289 static void vmw_sou_crtc_atomic_disable(struct drm_crtc
*crtc
,
290 struct drm_crtc_state
*old_state
)
292 struct vmw_private
*dev_priv
;
293 struct vmw_screen_object_unit
*sou
;
298 DRM_ERROR("CRTC is NULL\n");
302 sou
= vmw_crtc_to_sou(crtc
);
303 dev_priv
= vmw_priv(crtc
->dev
);
306 ret
= vmw_sou_fifo_destroy(dev_priv
, sou
);
308 DRM_ERROR("Failed to destroy Screen Object\n");
312 static int vmw_sou_crtc_page_flip(struct drm_crtc
*crtc
,
313 struct drm_framebuffer
*new_fb
,
314 struct drm_pending_vblank_event
*event
,
316 struct drm_modeset_acquire_ctx
*ctx
)
318 struct vmw_private
*dev_priv
= vmw_priv(crtc
->dev
);
319 struct drm_framebuffer
*old_fb
= crtc
->primary
->fb
;
320 struct vmw_framebuffer
*vfb
= vmw_framebuffer_to_vfb(new_fb
);
321 struct vmw_fence_obj
*fence
= NULL
;
322 struct drm_vmw_rect vclips
;
325 if (!vmw_kms_crtc_flippable(dev_priv
, crtc
))
328 flags
&= ~DRM_MODE_PAGE_FLIP_ASYNC
;
329 ret
= drm_atomic_helper_page_flip(crtc
, new_fb
, NULL
, flags
, ctx
);
331 DRM_ERROR("Page flip error %d.\n", ret
);
335 /* do a full screen dirty update */
338 vclips
.w
= crtc
->mode
.hdisplay
;
339 vclips
.h
= crtc
->mode
.vdisplay
;
342 ret
= vmw_kms_sou_do_dmabuf_dirty(dev_priv
, vfb
,
346 ret
= vmw_kms_sou_do_surface_dirty(dev_priv
, vfb
,
359 struct drm_file
*file_priv
= event
->base
.file_priv
;
361 ret
= vmw_event_fence_action_queue(file_priv
, fence
,
363 &event
->event
.vbl
.tv_sec
,
364 &event
->event
.vbl
.tv_usec
,
369 * No need to hold on to this now. The only cleanup
370 * we need to do if we fail is unref the fence.
372 vmw_fence_obj_unreference(&fence
);
374 if (vmw_crtc_to_du(crtc
)->is_implicit
)
375 vmw_kms_update_implicit_fb(dev_priv
, crtc
);
380 drm_atomic_set_fb_for_plane(crtc
->primary
->state
, old_fb
);
384 static const struct drm_crtc_funcs vmw_screen_object_crtc_funcs
= {
385 .gamma_set
= vmw_du_crtc_gamma_set
,
386 .destroy
= vmw_sou_crtc_destroy
,
387 .reset
= vmw_du_crtc_reset
,
388 .atomic_duplicate_state
= vmw_du_crtc_duplicate_state
,
389 .atomic_destroy_state
= vmw_du_crtc_destroy_state
,
390 .set_config
= vmw_kms_set_config
,
391 .page_flip
= vmw_sou_crtc_page_flip
,
395 * Screen Object Display Unit encoder functions
398 static void vmw_sou_encoder_destroy(struct drm_encoder
*encoder
)
400 vmw_sou_destroy(vmw_encoder_to_sou(encoder
));
403 static const struct drm_encoder_funcs vmw_screen_object_encoder_funcs
= {
404 .destroy
= vmw_sou_encoder_destroy
,
408 * Screen Object Display Unit connector functions
411 static void vmw_sou_connector_destroy(struct drm_connector
*connector
)
413 vmw_sou_destroy(vmw_connector_to_sou(connector
));
416 static const struct drm_connector_funcs vmw_sou_connector_funcs
= {
417 .dpms
= vmw_du_connector_dpms
,
418 .detect
= vmw_du_connector_detect
,
419 .fill_modes
= vmw_du_connector_fill_modes
,
420 .set_property
= vmw_du_connector_set_property
,
421 .destroy
= vmw_sou_connector_destroy
,
422 .reset
= vmw_du_connector_reset
,
423 .atomic_duplicate_state
= vmw_du_connector_duplicate_state
,
424 .atomic_destroy_state
= vmw_du_connector_destroy_state
,
425 .atomic_set_property
= vmw_du_connector_atomic_set_property
,
426 .atomic_get_property
= vmw_du_connector_atomic_get_property
,
431 drm_connector_helper_funcs vmw_sou_connector_helper_funcs
= {
432 .best_encoder
= drm_atomic_helper_best_encoder
,
438 * Screen Object Display Plane Functions
442 * vmw_sou_primary_plane_cleanup_fb - Frees sou backing buffer
444 * @plane: display plane
445 * @old_state: Contains the FB to clean up
447 * Unpins the display surface
449 * Returns 0 on success
452 vmw_sou_primary_plane_cleanup_fb(struct drm_plane
*plane
,
453 struct drm_plane_state
*old_state
)
455 struct vmw_plane_state
*vps
= vmw_plane_state_to_vps(old_state
);
457 vmw_dmabuf_unreference(&vps
->dmabuf
);
458 vps
->dmabuf_size
= 0;
460 vmw_du_plane_cleanup_fb(plane
, old_state
);
465 * vmw_sou_primary_plane_prepare_fb - allocate backing buffer
467 * @plane: display plane
468 * @new_state: info on the new plane state, including the FB
470 * The SOU backing buffer is our equivalent of the display plane.
472 * Returns 0 on success
475 vmw_sou_primary_plane_prepare_fb(struct drm_plane
*plane
,
476 struct drm_plane_state
*new_state
)
478 struct drm_framebuffer
*new_fb
= new_state
->fb
;
479 struct drm_crtc
*crtc
= plane
->state
->crtc
?: new_state
->crtc
;
480 struct vmw_plane_state
*vps
= vmw_plane_state_to_vps(new_state
);
481 struct vmw_private
*dev_priv
;
487 vmw_dmabuf_unreference(&vps
->dmabuf
);
488 vps
->dmabuf_size
= 0;
493 size
= new_state
->crtc_w
* new_state
->crtc_h
* 4;
496 if (vps
->dmabuf_size
== size
)
499 vmw_dmabuf_unreference(&vps
->dmabuf
);
500 vps
->dmabuf_size
= 0;
503 vps
->dmabuf
= kzalloc(sizeof(*vps
->dmabuf
), GFP_KERNEL
);
507 dev_priv
= vmw_priv(crtc
->dev
);
508 vmw_svga_enable(dev_priv
);
510 /* After we have alloced the backing store might not be able to
511 * resume the overlays, this is preferred to failing to alloc.
513 vmw_overlay_pause_all(dev_priv
);
514 ret
= vmw_dmabuf_init(dev_priv
, vps
->dmabuf
, size
,
515 &vmw_vram_ne_placement
,
516 false, &vmw_dmabuf_bo_free
);
517 vmw_overlay_resume_all(dev_priv
);
520 vps
->dmabuf
= NULL
; /* vmw_dmabuf_init frees on error */
522 vps
->dmabuf_size
= size
;
529 vmw_sou_primary_plane_atomic_update(struct drm_plane
*plane
,
530 struct drm_plane_state
*old_state
)
532 struct drm_crtc
*crtc
= plane
->state
->crtc
;
535 crtc
->primary
->fb
= plane
->state
->fb
;
539 static const struct drm_plane_funcs vmw_sou_plane_funcs
= {
540 .update_plane
= drm_atomic_helper_update_plane
,
541 .disable_plane
= drm_atomic_helper_disable_plane
,
542 .destroy
= vmw_du_primary_plane_destroy
,
543 .reset
= vmw_du_plane_reset
,
544 .atomic_duplicate_state
= vmw_du_plane_duplicate_state
,
545 .atomic_destroy_state
= vmw_du_plane_destroy_state
,
548 static const struct drm_plane_funcs vmw_sou_cursor_funcs
= {
549 .update_plane
= drm_atomic_helper_update_plane
,
550 .disable_plane
= drm_atomic_helper_disable_plane
,
551 .destroy
= vmw_du_cursor_plane_destroy
,
552 .reset
= vmw_du_plane_reset
,
553 .atomic_duplicate_state
= vmw_du_plane_duplicate_state
,
554 .atomic_destroy_state
= vmw_du_plane_destroy_state
,
561 drm_plane_helper_funcs vmw_sou_cursor_plane_helper_funcs
= {
562 .atomic_check
= vmw_du_cursor_plane_atomic_check
,
563 .atomic_update
= vmw_du_cursor_plane_atomic_update
,
564 .prepare_fb
= vmw_du_cursor_plane_prepare_fb
,
565 .cleanup_fb
= vmw_du_plane_cleanup_fb
,
569 drm_plane_helper_funcs vmw_sou_primary_plane_helper_funcs
= {
570 .atomic_check
= vmw_du_primary_plane_atomic_check
,
571 .atomic_update
= vmw_sou_primary_plane_atomic_update
,
572 .prepare_fb
= vmw_sou_primary_plane_prepare_fb
,
573 .cleanup_fb
= vmw_sou_primary_plane_cleanup_fb
,
576 static const struct drm_crtc_helper_funcs vmw_sou_crtc_helper_funcs
= {
577 .prepare
= vmw_sou_crtc_helper_prepare
,
578 .mode_set_nofb
= vmw_sou_crtc_mode_set_nofb
,
579 .atomic_check
= vmw_du_crtc_atomic_check
,
580 .atomic_begin
= vmw_du_crtc_atomic_begin
,
581 .atomic_flush
= vmw_du_crtc_atomic_flush
,
582 .atomic_enable
= vmw_sou_crtc_atomic_enable
,
583 .atomic_disable
= vmw_sou_crtc_atomic_disable
,
587 static int vmw_sou_init(struct vmw_private
*dev_priv
, unsigned unit
)
589 struct vmw_screen_object_unit
*sou
;
590 struct drm_device
*dev
= dev_priv
->dev
;
591 struct drm_connector
*connector
;
592 struct drm_encoder
*encoder
;
593 struct drm_plane
*primary
, *cursor
;
594 struct drm_crtc
*crtc
;
597 sou
= kzalloc(sizeof(*sou
), GFP_KERNEL
);
601 sou
->base
.unit
= unit
;
602 crtc
= &sou
->base
.crtc
;
603 encoder
= &sou
->base
.encoder
;
604 connector
= &sou
->base
.connector
;
605 primary
= &sou
->base
.primary
;
606 cursor
= &sou
->base
.cursor
;
608 sou
->base
.active_implicit
= false;
609 sou
->base
.pref_active
= (unit
== 0);
610 sou
->base
.pref_width
= dev_priv
->initial_width
;
611 sou
->base
.pref_height
= dev_priv
->initial_height
;
612 sou
->base
.pref_mode
= NULL
;
615 * Remove this after enabling atomic because property values can
616 * only exist in a state object
618 sou
->base
.is_implicit
= false;
620 /* Initialize primary plane */
621 vmw_du_plane_reset(primary
);
623 ret
= drm_universal_plane_init(dev
, &sou
->base
.primary
,
624 0, &vmw_sou_plane_funcs
,
625 vmw_primary_plane_formats
,
626 ARRAY_SIZE(vmw_primary_plane_formats
),
627 NULL
, DRM_PLANE_TYPE_PRIMARY
, NULL
);
629 DRM_ERROR("Failed to initialize primary plane");
633 drm_plane_helper_add(primary
, &vmw_sou_primary_plane_helper_funcs
);
635 /* Initialize cursor plane */
636 vmw_du_plane_reset(cursor
);
638 ret
= drm_universal_plane_init(dev
, &sou
->base
.cursor
,
639 0, &vmw_sou_cursor_funcs
,
640 vmw_cursor_plane_formats
,
641 ARRAY_SIZE(vmw_cursor_plane_formats
),
642 NULL
, DRM_PLANE_TYPE_CURSOR
, NULL
);
644 DRM_ERROR("Failed to initialize cursor plane");
645 drm_plane_cleanup(&sou
->base
.primary
);
649 drm_plane_helper_add(cursor
, &vmw_sou_cursor_plane_helper_funcs
);
651 vmw_du_connector_reset(connector
);
652 ret
= drm_connector_init(dev
, connector
, &vmw_sou_connector_funcs
,
653 DRM_MODE_CONNECTOR_VIRTUAL
);
655 DRM_ERROR("Failed to initialize connector\n");
659 drm_connector_helper_add(connector
, &vmw_sou_connector_helper_funcs
);
660 connector
->status
= vmw_du_connector_detect(connector
, true);
661 vmw_connector_state_to_vcs(connector
->state
)->is_implicit
= false;
664 ret
= drm_encoder_init(dev
, encoder
, &vmw_screen_object_encoder_funcs
,
665 DRM_MODE_ENCODER_VIRTUAL
, NULL
);
667 DRM_ERROR("Failed to initialize encoder\n");
668 goto err_free_connector
;
671 (void) drm_mode_connector_attach_encoder(connector
, encoder
);
672 encoder
->possible_crtcs
= (1 << unit
);
673 encoder
->possible_clones
= 0;
675 ret
= drm_connector_register(connector
);
677 DRM_ERROR("Failed to register connector\n");
678 goto err_free_encoder
;
682 vmw_du_crtc_reset(crtc
);
683 ret
= drm_crtc_init_with_planes(dev
, crtc
, &sou
->base
.primary
,
685 &vmw_screen_object_crtc_funcs
, NULL
);
687 DRM_ERROR("Failed to initialize CRTC\n");
688 goto err_free_unregister
;
691 drm_crtc_helper_add(crtc
, &vmw_sou_crtc_helper_funcs
);
693 drm_mode_crtc_set_gamma_size(crtc
, 256);
695 drm_object_attach_property(&connector
->base
,
696 dev_priv
->hotplug_mode_update_property
, 1);
697 drm_object_attach_property(&connector
->base
,
698 dev
->mode_config
.suggested_x_property
, 0);
699 drm_object_attach_property(&connector
->base
,
700 dev
->mode_config
.suggested_y_property
, 0);
701 if (dev_priv
->implicit_placement_property
)
702 drm_object_attach_property
704 dev_priv
->implicit_placement_property
,
705 sou
->base
.is_implicit
);
710 drm_connector_unregister(connector
);
712 drm_encoder_cleanup(encoder
);
714 drm_connector_cleanup(connector
);
720 int vmw_kms_sou_init_display(struct vmw_private
*dev_priv
)
722 struct drm_device
*dev
= dev_priv
->dev
;
725 if (!(dev_priv
->capabilities
& SVGA_CAP_SCREEN_OBJECT_2
)) {
726 DRM_INFO("Not using screen objects,"
727 " missing cap SCREEN_OBJECT_2\n");
732 dev_priv
->num_implicit
= 0;
733 dev_priv
->implicit_fb
= NULL
;
735 ret
= drm_vblank_init(dev
, VMWGFX_NUM_DISPLAY_UNITS
);
736 if (unlikely(ret
!= 0))
739 vmw_kms_create_implicit_placement_property(dev_priv
, false);
741 for (i
= 0; i
< VMWGFX_NUM_DISPLAY_UNITS
; ++i
)
742 vmw_sou_init(dev_priv
, i
);
744 dev_priv
->active_display_unit
= vmw_du_screen_object
;
746 DRM_INFO("Screen Objects Display Unit initialized\n");
751 static int do_dmabuf_define_gmrfb(struct vmw_private
*dev_priv
,
752 struct vmw_framebuffer
*framebuffer
)
754 struct vmw_dma_buffer
*buf
=
755 container_of(framebuffer
, struct vmw_framebuffer_dmabuf
,
757 int depth
= framebuffer
->base
.format
->depth
;
760 SVGAFifoCmdDefineGMRFB body
;
763 /* Emulate RGBA support, contrary to svga_reg.h this is not
764 * supported by hosts. This is only a problem if we are reading
765 * this value later and expecting what we uploaded back.
770 cmd
= vmw_fifo_reserve(dev_priv
, sizeof(*cmd
));
772 DRM_ERROR("Out of fifo space for dirty framebuffer command.\n");
776 cmd
->header
= SVGA_CMD_DEFINE_GMRFB
;
777 cmd
->body
.format
.bitsPerPixel
= framebuffer
->base
.format
->cpp
[0] * 8;
778 cmd
->body
.format
.colorDepth
= depth
;
779 cmd
->body
.format
.reserved
= 0;
780 cmd
->body
.bytesPerLine
= framebuffer
->base
.pitches
[0];
781 /* Buffer is reserved in vram or GMR */
782 vmw_bo_get_guest_ptr(&buf
->base
, &cmd
->body
.ptr
);
783 vmw_fifo_commit(dev_priv
, sizeof(*cmd
));
789 * vmw_sou_surface_fifo_commit - Callback to fill in and submit a
790 * blit surface to screen command.
792 * @dirty: The closure structure.
794 * Fills in the missing fields in the command, and translates the cliprects
795 * to match the destination bounding box encoded.
797 static void vmw_sou_surface_fifo_commit(struct vmw_kms_dirty
*dirty
)
799 struct vmw_kms_sou_surface_dirty
*sdirty
=
800 container_of(dirty
, typeof(*sdirty
), base
);
801 struct vmw_kms_sou_dirty_cmd
*cmd
= dirty
->cmd
;
802 s32 trans_x
= dirty
->unit
->crtc
.x
- sdirty
->dst_x
;
803 s32 trans_y
= dirty
->unit
->crtc
.y
- sdirty
->dst_y
;
804 size_t region_size
= dirty
->num_hits
* sizeof(SVGASignedRect
);
805 SVGASignedRect
*blit
= (SVGASignedRect
*) &cmd
[1];
808 if (!dirty
->num_hits
) {
809 vmw_fifo_commit(dirty
->dev_priv
, 0);
813 cmd
->header
.id
= SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN
;
814 cmd
->header
.size
= sizeof(cmd
->body
) + region_size
;
817 * Use the destination bounding box to specify destination - and
818 * source bounding regions.
820 cmd
->body
.destRect
.left
= sdirty
->left
;
821 cmd
->body
.destRect
.right
= sdirty
->right
;
822 cmd
->body
.destRect
.top
= sdirty
->top
;
823 cmd
->body
.destRect
.bottom
= sdirty
->bottom
;
825 cmd
->body
.srcRect
.left
= sdirty
->left
+ trans_x
;
826 cmd
->body
.srcRect
.right
= sdirty
->right
+ trans_x
;
827 cmd
->body
.srcRect
.top
= sdirty
->top
+ trans_y
;
828 cmd
->body
.srcRect
.bottom
= sdirty
->bottom
+ trans_y
;
830 cmd
->body
.srcImage
.sid
= sdirty
->sid
;
831 cmd
->body
.destScreenId
= dirty
->unit
->unit
;
833 /* Blits are relative to the destination rect. Translate. */
834 for (i
= 0; i
< dirty
->num_hits
; ++i
, ++blit
) {
835 blit
->left
-= sdirty
->left
;
836 blit
->right
-= sdirty
->left
;
837 blit
->top
-= sdirty
->top
;
838 blit
->bottom
-= sdirty
->top
;
841 vmw_fifo_commit(dirty
->dev_priv
, region_size
+ sizeof(*cmd
));
843 sdirty
->left
= sdirty
->top
= S32_MAX
;
844 sdirty
->right
= sdirty
->bottom
= S32_MIN
;
848 * vmw_sou_surface_clip - Callback to encode a blit surface to screen cliprect.
850 * @dirty: The closure structure
852 * Encodes a SVGASignedRect cliprect and updates the bounding box of the
853 * BLIT_SURFACE_TO_SCREEN command.
855 static void vmw_sou_surface_clip(struct vmw_kms_dirty
*dirty
)
857 struct vmw_kms_sou_surface_dirty
*sdirty
=
858 container_of(dirty
, typeof(*sdirty
), base
);
859 struct vmw_kms_sou_dirty_cmd
*cmd
= dirty
->cmd
;
860 SVGASignedRect
*blit
= (SVGASignedRect
*) &cmd
[1];
862 /* Destination rect. */
863 blit
+= dirty
->num_hits
;
864 blit
->left
= dirty
->unit_x1
;
865 blit
->top
= dirty
->unit_y1
;
866 blit
->right
= dirty
->unit_x2
;
867 blit
->bottom
= dirty
->unit_y2
;
869 /* Destination bounding box */
870 sdirty
->left
= min_t(s32
, sdirty
->left
, dirty
->unit_x1
);
871 sdirty
->top
= min_t(s32
, sdirty
->top
, dirty
->unit_y1
);
872 sdirty
->right
= max_t(s32
, sdirty
->right
, dirty
->unit_x2
);
873 sdirty
->bottom
= max_t(s32
, sdirty
->bottom
, dirty
->unit_y2
);
879 * vmw_kms_sou_do_surface_dirty - Dirty part of a surface backed framebuffer
881 * @dev_priv: Pointer to the device private structure.
882 * @framebuffer: Pointer to the surface-buffer backed framebuffer.
883 * @clips: Array of clip rects. Either @clips or @vclips must be NULL.
884 * @vclips: Alternate array of clip rects. Either @clips or @vclips must
886 * @srf: Pointer to surface to blit from. If NULL, the surface attached
887 * to @framebuffer will be used.
888 * @dest_x: X coordinate offset to align @srf with framebuffer coordinates.
889 * @dest_y: Y coordinate offset to align @srf with framebuffer coordinates.
890 * @num_clips: Number of clip rects in @clips.
891 * @inc: Increment to use when looping over @clips.
892 * @out_fence: If non-NULL, will return a ref-counted pointer to a
893 * struct vmw_fence_obj. The returned fence pointer may be NULL in which
894 * case the device has already synchronized.
896 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
899 int vmw_kms_sou_do_surface_dirty(struct vmw_private
*dev_priv
,
900 struct vmw_framebuffer
*framebuffer
,
901 struct drm_clip_rect
*clips
,
902 struct drm_vmw_rect
*vclips
,
903 struct vmw_resource
*srf
,
906 unsigned num_clips
, int inc
,
907 struct vmw_fence_obj
**out_fence
)
909 struct vmw_framebuffer_surface
*vfbs
=
910 container_of(framebuffer
, typeof(*vfbs
), base
);
911 struct vmw_kms_sou_surface_dirty sdirty
;
915 srf
= &vfbs
->surface
->res
;
917 ret
= vmw_kms_helper_resource_prepare(srf
, true);
921 sdirty
.base
.fifo_commit
= vmw_sou_surface_fifo_commit
;
922 sdirty
.base
.clip
= vmw_sou_surface_clip
;
923 sdirty
.base
.dev_priv
= dev_priv
;
924 sdirty
.base
.fifo_reserve_size
= sizeof(struct vmw_kms_sou_dirty_cmd
) +
925 sizeof(SVGASignedRect
) * num_clips
;
927 sdirty
.sid
= srf
->id
;
928 sdirty
.left
= sdirty
.top
= S32_MAX
;
929 sdirty
.right
= sdirty
.bottom
= S32_MIN
;
930 sdirty
.dst_x
= dest_x
;
931 sdirty
.dst_y
= dest_y
;
933 ret
= vmw_kms_helper_dirty(dev_priv
, framebuffer
, clips
, vclips
,
934 dest_x
, dest_y
, num_clips
, inc
,
936 vmw_kms_helper_resource_finish(srf
, out_fence
);
942 * vmw_sou_dmabuf_fifo_commit - Callback to submit a set of readback clips.
944 * @dirty: The closure structure.
946 * Commits a previously built command buffer of readback clips.
948 static void vmw_sou_dmabuf_fifo_commit(struct vmw_kms_dirty
*dirty
)
950 if (!dirty
->num_hits
) {
951 vmw_fifo_commit(dirty
->dev_priv
, 0);
955 vmw_fifo_commit(dirty
->dev_priv
,
956 sizeof(struct vmw_kms_sou_dmabuf_blit
) *
961 * vmw_sou_dmabuf_clip - Callback to encode a readback cliprect.
963 * @dirty: The closure structure
965 * Encodes a BLIT_GMRFB_TO_SCREEN cliprect.
967 static void vmw_sou_dmabuf_clip(struct vmw_kms_dirty
*dirty
)
969 struct vmw_kms_sou_dmabuf_blit
*blit
= dirty
->cmd
;
971 blit
+= dirty
->num_hits
;
972 blit
->header
= SVGA_CMD_BLIT_GMRFB_TO_SCREEN
;
973 blit
->body
.destScreenId
= dirty
->unit
->unit
;
974 blit
->body
.srcOrigin
.x
= dirty
->fb_x
;
975 blit
->body
.srcOrigin
.y
= dirty
->fb_y
;
976 blit
->body
.destRect
.left
= dirty
->unit_x1
;
977 blit
->body
.destRect
.top
= dirty
->unit_y1
;
978 blit
->body
.destRect
.right
= dirty
->unit_x2
;
979 blit
->body
.destRect
.bottom
= dirty
->unit_y2
;
984 * vmw_kms_do_dmabuf_dirty - Dirty part of a dma-buffer backed framebuffer
986 * @dev_priv: Pointer to the device private structure.
987 * @framebuffer: Pointer to the dma-buffer backed framebuffer.
988 * @clips: Array of clip rects.
989 * @vclips: Alternate array of clip rects. Either @clips or @vclips must
991 * @num_clips: Number of clip rects in @clips.
992 * @increment: Increment to use when looping over @clips.
993 * @interruptible: Whether to perform waits interruptible if possible.
994 * @out_fence: If non-NULL, will return a ref-counted pointer to a
995 * struct vmw_fence_obj. The returned fence pointer may be NULL in which
996 * case the device has already synchronized.
998 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
1001 int vmw_kms_sou_do_dmabuf_dirty(struct vmw_private
*dev_priv
,
1002 struct vmw_framebuffer
*framebuffer
,
1003 struct drm_clip_rect
*clips
,
1004 struct drm_vmw_rect
*vclips
,
1005 unsigned num_clips
, int increment
,
1007 struct vmw_fence_obj
**out_fence
)
1009 struct vmw_dma_buffer
*buf
=
1010 container_of(framebuffer
, struct vmw_framebuffer_dmabuf
,
1012 struct vmw_kms_dirty dirty
;
1015 ret
= vmw_kms_helper_buffer_prepare(dev_priv
, buf
, interruptible
,
1020 ret
= do_dmabuf_define_gmrfb(dev_priv
, framebuffer
);
1021 if (unlikely(ret
!= 0))
1024 dirty
.fifo_commit
= vmw_sou_dmabuf_fifo_commit
;
1025 dirty
.clip
= vmw_sou_dmabuf_clip
;
1026 dirty
.fifo_reserve_size
= sizeof(struct vmw_kms_sou_dmabuf_blit
) *
1028 ret
= vmw_kms_helper_dirty(dev_priv
, framebuffer
, clips
, vclips
,
1029 0, 0, num_clips
, increment
, &dirty
);
1030 vmw_kms_helper_buffer_finish(dev_priv
, NULL
, buf
, out_fence
, NULL
);
1035 vmw_kms_helper_buffer_revert(buf
);
1042 * vmw_sou_readback_fifo_commit - Callback to submit a set of readback clips.
1044 * @dirty: The closure structure.
1046 * Commits a previously built command buffer of readback clips.
1048 static void vmw_sou_readback_fifo_commit(struct vmw_kms_dirty
*dirty
)
1050 if (!dirty
->num_hits
) {
1051 vmw_fifo_commit(dirty
->dev_priv
, 0);
1055 vmw_fifo_commit(dirty
->dev_priv
,
1056 sizeof(struct vmw_kms_sou_readback_blit
) *
1061 * vmw_sou_readback_clip - Callback to encode a readback cliprect.
1063 * @dirty: The closure structure
1065 * Encodes a BLIT_SCREEN_TO_GMRFB cliprect.
1067 static void vmw_sou_readback_clip(struct vmw_kms_dirty
*dirty
)
1069 struct vmw_kms_sou_readback_blit
*blit
= dirty
->cmd
;
1071 blit
+= dirty
->num_hits
;
1072 blit
->header
= SVGA_CMD_BLIT_SCREEN_TO_GMRFB
;
1073 blit
->body
.srcScreenId
= dirty
->unit
->unit
;
1074 blit
->body
.destOrigin
.x
= dirty
->fb_x
;
1075 blit
->body
.destOrigin
.y
= dirty
->fb_y
;
1076 blit
->body
.srcRect
.left
= dirty
->unit_x1
;
1077 blit
->body
.srcRect
.top
= dirty
->unit_y1
;
1078 blit
->body
.srcRect
.right
= dirty
->unit_x2
;
1079 blit
->body
.srcRect
.bottom
= dirty
->unit_y2
;
1084 * vmw_kms_sou_readback - Perform a readback from the screen object system to
1085 * a dma-buffer backed framebuffer.
1087 * @dev_priv: Pointer to the device private structure.
1088 * @file_priv: Pointer to a struct drm_file identifying the caller.
1089 * Must be set to NULL if @user_fence_rep is NULL.
1090 * @vfb: Pointer to the dma-buffer backed framebuffer.
1091 * @user_fence_rep: User-space provided structure for fence information.
1092 * Must be set to non-NULL if @file_priv is non-NULL.
1093 * @vclips: Array of clip rects.
1094 * @num_clips: Number of clip rects in @vclips.
1096 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
1099 int vmw_kms_sou_readback(struct vmw_private
*dev_priv
,
1100 struct drm_file
*file_priv
,
1101 struct vmw_framebuffer
*vfb
,
1102 struct drm_vmw_fence_rep __user
*user_fence_rep
,
1103 struct drm_vmw_rect
*vclips
,
1106 struct vmw_dma_buffer
*buf
=
1107 container_of(vfb
, struct vmw_framebuffer_dmabuf
, base
)->buffer
;
1108 struct vmw_kms_dirty dirty
;
1111 ret
= vmw_kms_helper_buffer_prepare(dev_priv
, buf
, true, false);
1115 ret
= do_dmabuf_define_gmrfb(dev_priv
, vfb
);
1116 if (unlikely(ret
!= 0))
1119 dirty
.fifo_commit
= vmw_sou_readback_fifo_commit
;
1120 dirty
.clip
= vmw_sou_readback_clip
;
1121 dirty
.fifo_reserve_size
= sizeof(struct vmw_kms_sou_readback_blit
) *
1123 ret
= vmw_kms_helper_dirty(dev_priv
, vfb
, NULL
, vclips
,
1124 0, 0, num_clips
, 1, &dirty
);
1125 vmw_kms_helper_buffer_finish(dev_priv
, file_priv
, buf
, NULL
,
1131 vmw_kms_helper_buffer_revert(buf
);