1 /**************************************************************************
3 * Copyright © 2009-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>
32 #include <drm/drm_rect.h>
35 /* Might need a hrtimer here? */
36 #define VMWGFX_PRESENT_RATE ((HZ / 60 > 0) ? HZ / 60 : 1)
38 void vmw_du_cleanup(struct vmw_display_unit *du)
40 drm_plane_cleanup(&du->primary);
41 drm_plane_cleanup(&du->cursor);
43 drm_connector_unregister(&du->connector);
44 drm_crtc_cleanup(&du->crtc);
45 drm_encoder_cleanup(&du->encoder);
46 drm_connector_cleanup(&du->connector);
50 * Display Unit Cursor functions
53 static int vmw_cursor_update_image(struct vmw_private *dev_priv,
54 u32 *image, u32 width, u32 height,
55 u32 hotspotX, u32 hotspotY)
59 SVGAFifoCmdDefineAlphaCursor cursor;
61 u32 image_size = width * height * 4;
62 u32 cmd_size = sizeof(*cmd) + image_size;
67 cmd = vmw_fifo_reserve(dev_priv, cmd_size);
68 if (unlikely(cmd == NULL)) {
69 DRM_ERROR("Fifo reserve failed.\n");
73 memset(cmd, 0, sizeof(*cmd));
75 memcpy(&cmd[1], image, image_size);
77 cmd->cmd = SVGA_CMD_DEFINE_ALPHA_CURSOR;
79 cmd->cursor.width = width;
80 cmd->cursor.height = height;
81 cmd->cursor.hotspotX = hotspotX;
82 cmd->cursor.hotspotY = hotspotY;
84 vmw_fifo_commit_flush(dev_priv, cmd_size);
89 static int vmw_cursor_update_dmabuf(struct vmw_private *dev_priv,
90 struct vmw_dma_buffer *dmabuf,
91 u32 width, u32 height,
92 u32 hotspotX, u32 hotspotY)
94 struct ttm_bo_kmap_obj map;
95 unsigned long kmap_offset;
96 unsigned long kmap_num;
102 kmap_num = (width*height*4 + PAGE_SIZE - 1) >> PAGE_SHIFT;
104 ret = ttm_bo_reserve(&dmabuf->base, true, false, NULL);
105 if (unlikely(ret != 0)) {
106 DRM_ERROR("reserve failed\n");
110 ret = ttm_bo_kmap(&dmabuf->base, kmap_offset, kmap_num, &map);
111 if (unlikely(ret != 0))
114 virtual = ttm_kmap_obj_virtual(&map, &dummy);
115 ret = vmw_cursor_update_image(dev_priv, virtual, width, height,
120 ttm_bo_unreserve(&dmabuf->base);
126 static void vmw_cursor_update_position(struct vmw_private *dev_priv,
127 bool show, int x, int y)
129 u32 *fifo_mem = dev_priv->mmio_virt;
132 spin_lock(&dev_priv->cursor_lock);
133 vmw_mmio_write(show ? 1 : 0, fifo_mem + SVGA_FIFO_CURSOR_ON);
134 vmw_mmio_write(x, fifo_mem + SVGA_FIFO_CURSOR_X);
135 vmw_mmio_write(y, fifo_mem + SVGA_FIFO_CURSOR_Y);
136 count = vmw_mmio_read(fifo_mem + SVGA_FIFO_CURSOR_COUNT);
137 vmw_mmio_write(++count, fifo_mem + SVGA_FIFO_CURSOR_COUNT);
138 spin_unlock(&dev_priv->cursor_lock);
142 void vmw_kms_cursor_snoop(struct vmw_surface *srf,
143 struct ttm_object_file *tfile,
144 struct ttm_buffer_object *bo,
145 SVGA3dCmdHeader *header)
147 struct ttm_bo_kmap_obj map;
148 unsigned long kmap_offset;
149 unsigned long kmap_num;
155 SVGA3dCmdHeader header;
156 SVGA3dCmdSurfaceDMA dma;
160 cmd = container_of(header, struct vmw_dma_cmd, header);
162 /* No snooper installed */
163 if (!srf->snooper.image)
166 if (cmd->dma.host.face != 0 || cmd->dma.host.mipmap != 0) {
167 DRM_ERROR("face and mipmap for cursors should never != 0\n");
171 if (cmd->header.size < 64) {
172 DRM_ERROR("at least one full copy box must be given\n");
176 box = (SVGA3dCopyBox *)&cmd[1];
177 box_count = (cmd->header.size - sizeof(SVGA3dCmdSurfaceDMA)) /
178 sizeof(SVGA3dCopyBox);
180 if (cmd->dma.guest.ptr.offset % PAGE_SIZE ||
181 box->x != 0 || box->y != 0 || box->z != 0 ||
182 box->srcx != 0 || box->srcy != 0 || box->srcz != 0 ||
183 box->d != 1 || box_count != 1) {
184 /* TODO handle none page aligned offsets */
185 /* TODO handle more dst & src != 0 */
186 /* TODO handle more then one copy */
187 DRM_ERROR("Cant snoop dma request for cursor!\n");
188 DRM_ERROR("(%u, %u, %u) (%u, %u, %u) (%ux%ux%u) %u %u\n",
189 box->srcx, box->srcy, box->srcz,
190 box->x, box->y, box->z,
191 box->w, box->h, box->d, box_count,
192 cmd->dma.guest.ptr.offset);
196 kmap_offset = cmd->dma.guest.ptr.offset >> PAGE_SHIFT;
197 kmap_num = (64*64*4) >> PAGE_SHIFT;
199 ret = ttm_bo_reserve(bo, true, false, NULL);
200 if (unlikely(ret != 0)) {
201 DRM_ERROR("reserve failed\n");
205 ret = ttm_bo_kmap(bo, kmap_offset, kmap_num, &map);
206 if (unlikely(ret != 0))
209 virtual = ttm_kmap_obj_virtual(&map, &dummy);
211 if (box->w == 64 && cmd->dma.guest.pitch == 64*4) {
212 memcpy(srf->snooper.image, virtual, 64*64*4);
214 /* Image is unsigned pointer. */
215 for (i = 0; i < box->h; i++)
216 memcpy(srf->snooper.image + i * 64,
217 virtual + i * cmd->dma.guest.pitch,
225 ttm_bo_unreserve(bo);
229 * vmw_kms_legacy_hotspot_clear - Clear legacy hotspots
231 * @dev_priv: Pointer to the device private struct.
233 * Clears all legacy hotspots.
235 void vmw_kms_legacy_hotspot_clear(struct vmw_private *dev_priv)
237 struct drm_device *dev = dev_priv->dev;
238 struct vmw_display_unit *du;
239 struct drm_crtc *crtc;
241 drm_modeset_lock_all(dev);
242 drm_for_each_crtc(crtc, dev) {
243 du = vmw_crtc_to_du(crtc);
248 drm_modeset_unlock_all(dev);
251 void vmw_kms_cursor_post_execbuf(struct vmw_private *dev_priv)
253 struct drm_device *dev = dev_priv->dev;
254 struct vmw_display_unit *du;
255 struct drm_crtc *crtc;
257 mutex_lock(&dev->mode_config.mutex);
259 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
260 du = vmw_crtc_to_du(crtc);
261 if (!du->cursor_surface ||
262 du->cursor_age == du->cursor_surface->snooper.age)
265 du->cursor_age = du->cursor_surface->snooper.age;
266 vmw_cursor_update_image(dev_priv,
267 du->cursor_surface->snooper.image,
269 du->hotspot_x + du->core_hotspot_x,
270 du->hotspot_y + du->core_hotspot_y);
273 mutex_unlock(&dev->mode_config.mutex);
277 void vmw_du_cursor_plane_destroy(struct drm_plane *plane)
279 vmw_cursor_update_position(plane->dev->dev_private, false, 0, 0);
281 drm_plane_cleanup(plane);
285 void vmw_du_primary_plane_destroy(struct drm_plane *plane)
287 drm_plane_cleanup(plane);
289 /* Planes are static in our case so we don't free it */
294 * vmw_du_vps_unpin_surf - unpins resource associated with a framebuffer surface
296 * @vps: plane state associated with the display surface
297 * @unreference: true if we also want to unreference the display.
299 void vmw_du_plane_unpin_surf(struct vmw_plane_state *vps,
304 vmw_resource_unpin(&vps->surf->res);
310 DRM_ERROR("Surface still pinned\n");
311 vmw_surface_unreference(&vps->surf);
318 * vmw_du_plane_cleanup_fb - Unpins the cursor
320 * @plane: display plane
321 * @old_state: Contains the FB to clean up
323 * Unpins the framebuffer surface
325 * Returns 0 on success
328 vmw_du_plane_cleanup_fb(struct drm_plane *plane,
329 struct drm_plane_state *old_state)
331 struct vmw_plane_state *vps = vmw_plane_state_to_vps(old_state);
333 vmw_du_plane_unpin_surf(vps, false);
338 * vmw_du_cursor_plane_prepare_fb - Readies the cursor by referencing it
340 * @plane: display plane
341 * @new_state: info on the new plane state, including the FB
343 * Returns 0 on success
346 vmw_du_cursor_plane_prepare_fb(struct drm_plane *plane,
347 struct drm_plane_state *new_state)
349 struct drm_framebuffer *fb = new_state->fb;
350 struct vmw_plane_state *vps = vmw_plane_state_to_vps(new_state);
354 vmw_surface_unreference(&vps->surf);
357 vmw_dmabuf_unreference(&vps->dmabuf);
360 if (vmw_framebuffer_to_vfb(fb)->dmabuf) {
361 vps->dmabuf = vmw_framebuffer_to_vfbd(fb)->buffer;
362 vmw_dmabuf_reference(vps->dmabuf);
364 vps->surf = vmw_framebuffer_to_vfbs(fb)->surface;
365 vmw_surface_reference(vps->surf);
374 vmw_du_cursor_plane_atomic_update(struct drm_plane *plane,
375 struct drm_plane_state *old_state)
377 struct drm_crtc *crtc = plane->state->crtc ?: old_state->crtc;
378 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
379 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
380 struct vmw_plane_state *vps = vmw_plane_state_to_vps(plane->state);
381 s32 hotspot_x, hotspot_y;
385 hotspot_x = du->hotspot_x;
386 hotspot_y = du->hotspot_y;
389 hotspot_x += plane->fb->hot_x;
390 hotspot_y += plane->fb->hot_y;
393 du->cursor_surface = vps->surf;
394 du->cursor_dmabuf = vps->dmabuf;
396 /* setup new image */
398 du->cursor_age = du->cursor_surface->snooper.age;
400 ret = vmw_cursor_update_image(dev_priv,
401 vps->surf->snooper.image,
402 64, 64, hotspot_x, hotspot_y);
403 } else if (vps->dmabuf) {
404 ret = vmw_cursor_update_dmabuf(dev_priv, vps->dmabuf,
405 plane->state->crtc_w,
406 plane->state->crtc_h,
407 hotspot_x, hotspot_y);
409 vmw_cursor_update_position(dev_priv, false, 0, 0);
414 du->cursor_x = plane->state->crtc_x + du->set_gui_x;
415 du->cursor_y = plane->state->crtc_y + du->set_gui_y;
417 vmw_cursor_update_position(dev_priv, true,
418 du->cursor_x + hotspot_x,
419 du->cursor_y + hotspot_y);
421 du->core_hotspot_x = hotspot_x - du->hotspot_x;
422 du->core_hotspot_y = hotspot_y - du->hotspot_y;
424 DRM_ERROR("Failed to update cursor image\n");
430 * vmw_du_primary_plane_atomic_check - check if the new state is okay
432 * @plane: display plane
433 * @state: info on the new plane state, including the FB
435 * Check if the new state is settable given the current state. Other
436 * than what the atomic helper checks, we care about crtc fitting
437 * the FB and maintaining one active framebuffer.
439 * Returns 0 on success
441 int vmw_du_primary_plane_atomic_check(struct drm_plane *plane,
442 struct drm_plane_state *state)
444 struct drm_framebuffer *new_fb = state->fb;
447 struct drm_rect src = {
450 .x2 = state->src_x + state->src_w,
451 .y2 = state->src_y + state->src_h,
453 struct drm_rect dest = {
456 .x2 = state->crtc_x + state->crtc_w,
457 .y2 = state->crtc_y + state->crtc_h,
459 struct drm_rect clip = dest;
462 ret = drm_plane_helper_check_update(plane, state->crtc, new_fb,
465 DRM_PLANE_HELPER_NO_SCALING,
466 DRM_PLANE_HELPER_NO_SCALING,
467 false, true, &visible);
470 if (!ret && new_fb) {
471 struct drm_crtc *crtc = state->crtc;
472 struct vmw_connector_state *vcs;
473 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
474 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
475 struct vmw_framebuffer *vfb = vmw_framebuffer_to_vfb(new_fb);
477 vcs = vmw_connector_state_to_vcs(du->connector.state);
479 if ((dest.x2 > new_fb->width ||
480 dest.y2 > new_fb->height)) {
481 DRM_ERROR("CRTC area outside of framebuffer\n");
485 /* Only one active implicit framebuffer at a time. */
486 mutex_lock(&dev_priv->global_kms_state_mutex);
487 if (vcs->is_implicit && dev_priv->implicit_fb &&
488 !(dev_priv->num_implicit == 1 && du->active_implicit)
489 && dev_priv->implicit_fb != vfb) {
490 DRM_ERROR("Multiple implicit framebuffers "
494 mutex_unlock(&dev_priv->global_kms_state_mutex);
503 * vmw_du_cursor_plane_atomic_check - check if the new state is okay
505 * @plane: cursor plane
506 * @state: info on the new plane state
508 * This is a chance to fail if the new cursor state does not fit
511 * Returns 0 on success
513 int vmw_du_cursor_plane_atomic_check(struct drm_plane *plane,
514 struct drm_plane_state *new_state)
517 struct vmw_surface *surface = NULL;
518 struct drm_framebuffer *fb = new_state->fb;
525 /* A lot of the code assumes this */
526 if (new_state->crtc_w != 64 || new_state->crtc_h != 64) {
527 DRM_ERROR("Invalid cursor dimensions (%d, %d)\n",
528 new_state->crtc_w, new_state->crtc_h);
532 if (!vmw_framebuffer_to_vfb(fb)->dmabuf)
533 surface = vmw_framebuffer_to_vfbs(fb)->surface;
535 if (surface && !surface->snooper.image) {
536 DRM_ERROR("surface not suitable for cursor\n");
544 int vmw_du_crtc_atomic_check(struct drm_crtc *crtc,
545 struct drm_crtc_state *new_state)
547 struct vmw_display_unit *du = vmw_crtc_to_du(new_state->crtc);
548 int connector_mask = 1 << drm_connector_index(&du->connector);
549 bool has_primary = new_state->plane_mask &
550 BIT(drm_plane_index(crtc->primary));
552 /* We always want to have an active plane with an active CRTC */
553 if (has_primary != new_state->enable)
557 if (new_state->connector_mask != connector_mask &&
558 new_state->connector_mask != 0) {
559 DRM_ERROR("Invalid connectors configuration\n");
564 * Our virtual device does not have a dot clock, so use the logical
565 * clock value as the dot clock.
567 if (new_state->mode.crtc_clock == 0)
568 new_state->adjusted_mode.crtc_clock = new_state->mode.clock;
574 void vmw_du_crtc_atomic_begin(struct drm_crtc *crtc,
575 struct drm_crtc_state *old_crtc_state)
580 void vmw_du_crtc_atomic_flush(struct drm_crtc *crtc,
581 struct drm_crtc_state *old_crtc_state)
583 struct drm_pending_vblank_event *event = crtc->state->event;
586 crtc->state->event = NULL;
588 spin_lock_irq(&crtc->dev->event_lock);
589 if (drm_crtc_vblank_get(crtc) == 0)
590 drm_crtc_arm_vblank_event(crtc, event);
592 drm_crtc_send_vblank_event(crtc, event);
593 spin_unlock_irq(&crtc->dev->event_lock);
600 * vmw_du_crtc_duplicate_state - duplicate crtc state
603 * Allocates and returns a copy of the crtc state (both common and
604 * vmw-specific) for the specified crtc.
606 * Returns: The newly allocated crtc state, or NULL on failure.
608 struct drm_crtc_state *
609 vmw_du_crtc_duplicate_state(struct drm_crtc *crtc)
611 struct drm_crtc_state *state;
612 struct vmw_crtc_state *vcs;
614 if (WARN_ON(!crtc->state))
617 vcs = kmemdup(crtc->state, sizeof(*vcs), GFP_KERNEL);
624 __drm_atomic_helper_crtc_duplicate_state(crtc, state);
631 * vmw_du_crtc_reset - creates a blank vmw crtc state
634 * Resets the atomic state for @crtc by freeing the state pointer (which
635 * might be NULL, e.g. at driver load time) and allocating a new empty state
638 void vmw_du_crtc_reset(struct drm_crtc *crtc)
640 struct vmw_crtc_state *vcs;
644 __drm_atomic_helper_crtc_destroy_state(crtc->state);
646 kfree(vmw_crtc_state_to_vcs(crtc->state));
649 vcs = kzalloc(sizeof(*vcs), GFP_KERNEL);
652 DRM_ERROR("Cannot allocate vmw_crtc_state\n");
656 crtc->state = &vcs->base;
657 crtc->state->crtc = crtc;
662 * vmw_du_crtc_destroy_state - destroy crtc state
664 * @state: state object to destroy
666 * Destroys the crtc state (both common and vmw-specific) for the
670 vmw_du_crtc_destroy_state(struct drm_crtc *crtc,
671 struct drm_crtc_state *state)
673 drm_atomic_helper_crtc_destroy_state(crtc, state);
678 * vmw_du_plane_duplicate_state - duplicate plane state
681 * Allocates and returns a copy of the plane state (both common and
682 * vmw-specific) for the specified plane.
684 * Returns: The newly allocated plane state, or NULL on failure.
686 struct drm_plane_state *
687 vmw_du_plane_duplicate_state(struct drm_plane *plane)
689 struct drm_plane_state *state;
690 struct vmw_plane_state *vps;
692 vps = kmemdup(plane->state, sizeof(*vps), GFP_KERNEL);
699 /* Mapping is managed by prepare_fb/cleanup_fb */
700 memset(&vps->guest_map, 0, sizeof(vps->guest_map));
701 memset(&vps->host_map, 0, sizeof(vps->host_map));
704 /* Each ref counted resource needs to be acquired again */
706 (void) vmw_surface_reference(vps->surf);
709 (void) vmw_dmabuf_reference(vps->dmabuf);
713 __drm_atomic_helper_plane_duplicate_state(plane, state);
720 * vmw_du_plane_reset - creates a blank vmw plane state
723 * Resets the atomic state for @plane by freeing the state pointer (which might
724 * be NULL, e.g. at driver load time) and allocating a new empty state object.
726 void vmw_du_plane_reset(struct drm_plane *plane)
728 struct vmw_plane_state *vps;
732 vmw_du_plane_destroy_state(plane, plane->state);
734 vps = kzalloc(sizeof(*vps), GFP_KERNEL);
737 DRM_ERROR("Cannot allocate vmw_plane_state\n");
741 plane->state = &vps->base;
742 plane->state->plane = plane;
743 plane->state->rotation = DRM_MODE_ROTATE_0;
748 * vmw_du_plane_destroy_state - destroy plane state
750 * @state: state object to destroy
752 * Destroys the plane state (both common and vmw-specific) for the
756 vmw_du_plane_destroy_state(struct drm_plane *plane,
757 struct drm_plane_state *state)
759 struct vmw_plane_state *vps = vmw_plane_state_to_vps(state);
762 /* Should have been freed by cleanup_fb */
763 if (vps->guest_map.virtual) {
764 DRM_ERROR("Guest mapping not freed\n");
765 ttm_bo_kunmap(&vps->guest_map);
768 if (vps->host_map.virtual) {
769 DRM_ERROR("Host mapping not freed\n");
770 ttm_bo_kunmap(&vps->host_map);
774 vmw_surface_unreference(&vps->surf);
777 vmw_dmabuf_unreference(&vps->dmabuf);
779 drm_atomic_helper_plane_destroy_state(plane, state);
784 * vmw_du_connector_duplicate_state - duplicate connector state
785 * @connector: DRM connector
787 * Allocates and returns a copy of the connector state (both common and
788 * vmw-specific) for the specified connector.
790 * Returns: The newly allocated connector state, or NULL on failure.
792 struct drm_connector_state *
793 vmw_du_connector_duplicate_state(struct drm_connector *connector)
795 struct drm_connector_state *state;
796 struct vmw_connector_state *vcs;
798 if (WARN_ON(!connector->state))
801 vcs = kmemdup(connector->state, sizeof(*vcs), GFP_KERNEL);
808 __drm_atomic_helper_connector_duplicate_state(connector, state);
815 * vmw_du_connector_reset - creates a blank vmw connector state
816 * @connector: DRM connector
818 * Resets the atomic state for @connector by freeing the state pointer (which
819 * might be NULL, e.g. at driver load time) and allocating a new empty state
822 void vmw_du_connector_reset(struct drm_connector *connector)
824 struct vmw_connector_state *vcs;
827 if (connector->state) {
828 __drm_atomic_helper_connector_destroy_state(connector->state);
830 kfree(vmw_connector_state_to_vcs(connector->state));
833 vcs = kzalloc(sizeof(*vcs), GFP_KERNEL);
836 DRM_ERROR("Cannot allocate vmw_connector_state\n");
840 __drm_atomic_helper_connector_reset(connector, &vcs->base);
845 * vmw_du_connector_destroy_state - destroy connector state
846 * @connector: DRM connector
847 * @state: state object to destroy
849 * Destroys the connector state (both common and vmw-specific) for the
853 vmw_du_connector_destroy_state(struct drm_connector *connector,
854 struct drm_connector_state *state)
856 drm_atomic_helper_connector_destroy_state(connector, state);
859 * Generic framebuffer code
863 * Surface framebuffer code
866 static void vmw_framebuffer_surface_destroy(struct drm_framebuffer *framebuffer)
868 struct vmw_framebuffer_surface *vfbs =
869 vmw_framebuffer_to_vfbs(framebuffer);
871 drm_framebuffer_cleanup(framebuffer);
872 vmw_surface_unreference(&vfbs->surface);
873 if (vfbs->base.user_obj)
874 ttm_base_object_unref(&vfbs->base.user_obj);
879 static int vmw_framebuffer_surface_dirty(struct drm_framebuffer *framebuffer,
880 struct drm_file *file_priv,
881 unsigned flags, unsigned color,
882 struct drm_clip_rect *clips,
885 struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
886 struct vmw_framebuffer_surface *vfbs =
887 vmw_framebuffer_to_vfbs(framebuffer);
888 struct drm_clip_rect norect;
891 /* Legacy Display Unit does not support 3D */
892 if (dev_priv->active_display_unit == vmw_du_legacy)
895 drm_modeset_lock_all(dev_priv->dev);
897 ret = ttm_read_lock(&dev_priv->reservation_sem, true);
898 if (unlikely(ret != 0)) {
899 drm_modeset_unlock_all(dev_priv->dev);
906 norect.x1 = norect.y1 = 0;
907 norect.x2 = framebuffer->width;
908 norect.y2 = framebuffer->height;
909 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
911 inc = 2; /* skip source rects */
914 if (dev_priv->active_display_unit == vmw_du_screen_object)
915 ret = vmw_kms_sou_do_surface_dirty(dev_priv, &vfbs->base,
916 clips, NULL, NULL, 0, 0,
917 num_clips, inc, NULL);
919 ret = vmw_kms_stdu_surface_dirty(dev_priv, &vfbs->base,
920 clips, NULL, NULL, 0, 0,
921 num_clips, inc, NULL);
923 vmw_fifo_flush(dev_priv, false);
924 ttm_read_unlock(&dev_priv->reservation_sem);
926 drm_modeset_unlock_all(dev_priv->dev);
932 * vmw_kms_readback - Perform a readback from the screen system to
933 * a dma-buffer backed framebuffer.
935 * @dev_priv: Pointer to the device private structure.
936 * @file_priv: Pointer to a struct drm_file identifying the caller.
937 * Must be set to NULL if @user_fence_rep is NULL.
938 * @vfb: Pointer to the dma-buffer backed framebuffer.
939 * @user_fence_rep: User-space provided structure for fence information.
940 * Must be set to non-NULL if @file_priv is non-NULL.
941 * @vclips: Array of clip rects.
942 * @num_clips: Number of clip rects in @vclips.
944 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
947 int vmw_kms_readback(struct vmw_private *dev_priv,
948 struct drm_file *file_priv,
949 struct vmw_framebuffer *vfb,
950 struct drm_vmw_fence_rep __user *user_fence_rep,
951 struct drm_vmw_rect *vclips,
954 switch (dev_priv->active_display_unit) {
955 case vmw_du_screen_object:
956 return vmw_kms_sou_readback(dev_priv, file_priv, vfb,
957 user_fence_rep, vclips, num_clips);
958 case vmw_du_screen_target:
959 return vmw_kms_stdu_dma(dev_priv, file_priv, vfb,
960 user_fence_rep, NULL, vclips, num_clips,
964 "Readback called with invalid display system.\n");
971 static const struct drm_framebuffer_funcs vmw_framebuffer_surface_funcs = {
972 .destroy = vmw_framebuffer_surface_destroy,
973 .dirty = vmw_framebuffer_surface_dirty,
976 static int vmw_kms_new_framebuffer_surface(struct vmw_private *dev_priv,
977 struct vmw_surface *surface,
978 struct vmw_framebuffer **out,
979 const struct drm_mode_fb_cmd2
981 bool is_dmabuf_proxy)
984 struct drm_device *dev = dev_priv->dev;
985 struct vmw_framebuffer_surface *vfbs;
986 enum SVGA3dSurfaceFormat format;
988 struct drm_format_name_buf format_name;
990 /* 3D is only supported on HWv8 and newer hosts */
991 if (dev_priv->active_display_unit == vmw_du_legacy)
998 /* Surface must be marked as a scanout. */
999 if (unlikely(!surface->scanout))
1002 if (unlikely(surface->mip_levels[0] != 1 ||
1003 surface->num_sizes != 1 ||
1004 surface->base_size.width < mode_cmd->width ||
1005 surface->base_size.height < mode_cmd->height ||
1006 surface->base_size.depth != 1)) {
1007 DRM_ERROR("Incompatible surface dimensions "
1008 "for requested mode.\n");
1012 switch (mode_cmd->pixel_format) {
1013 case DRM_FORMAT_ARGB8888:
1014 format = SVGA3D_A8R8G8B8;
1016 case DRM_FORMAT_XRGB8888:
1017 format = SVGA3D_X8R8G8B8;
1019 case DRM_FORMAT_RGB565:
1020 format = SVGA3D_R5G6B5;
1022 case DRM_FORMAT_XRGB1555:
1023 format = SVGA3D_A1R5G5B5;
1026 DRM_ERROR("Invalid pixel format: %s\n",
1027 drm_get_format_name(mode_cmd->pixel_format, &format_name));
1032 * For DX, surface format validation is done when surface->scanout
1035 if (!dev_priv->has_dx && format != surface->format) {
1036 DRM_ERROR("Invalid surface format for requested mode.\n");
1040 vfbs = kzalloc(sizeof(*vfbs), GFP_KERNEL);
1046 drm_helper_mode_fill_fb_struct(dev, &vfbs->base.base, mode_cmd);
1047 vfbs->surface = vmw_surface_reference(surface);
1048 vfbs->base.user_handle = mode_cmd->handles[0];
1049 vfbs->is_dmabuf_proxy = is_dmabuf_proxy;
1053 ret = drm_framebuffer_init(dev, &vfbs->base.base,
1054 &vmw_framebuffer_surface_funcs);
1061 vmw_surface_unreference(&surface);
1068 * Dmabuf framebuffer code
1071 static void vmw_framebuffer_dmabuf_destroy(struct drm_framebuffer *framebuffer)
1073 struct vmw_framebuffer_dmabuf *vfbd =
1074 vmw_framebuffer_to_vfbd(framebuffer);
1076 drm_framebuffer_cleanup(framebuffer);
1077 vmw_dmabuf_unreference(&vfbd->buffer);
1078 if (vfbd->base.user_obj)
1079 ttm_base_object_unref(&vfbd->base.user_obj);
1084 static int vmw_framebuffer_dmabuf_dirty(struct drm_framebuffer *framebuffer,
1085 struct drm_file *file_priv,
1086 unsigned flags, unsigned color,
1087 struct drm_clip_rect *clips,
1090 struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
1091 struct vmw_framebuffer_dmabuf *vfbd =
1092 vmw_framebuffer_to_vfbd(framebuffer);
1093 struct drm_clip_rect norect;
1094 int ret, increment = 1;
1096 drm_modeset_lock_all(dev_priv->dev);
1098 ret = ttm_read_lock(&dev_priv->reservation_sem, true);
1099 if (unlikely(ret != 0)) {
1100 drm_modeset_unlock_all(dev_priv->dev);
1107 norect.x1 = norect.y1 = 0;
1108 norect.x2 = framebuffer->width;
1109 norect.y2 = framebuffer->height;
1110 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
1115 switch (dev_priv->active_display_unit) {
1116 case vmw_du_screen_target:
1117 ret = vmw_kms_stdu_dma(dev_priv, NULL, &vfbd->base, NULL,
1118 clips, NULL, num_clips, increment,
1121 case vmw_du_screen_object:
1122 ret = vmw_kms_sou_do_dmabuf_dirty(dev_priv, &vfbd->base,
1123 clips, NULL, num_clips,
1124 increment, true, NULL);
1127 ret = vmw_kms_ldu_do_dmabuf_dirty(dev_priv, &vfbd->base, 0, 0,
1128 clips, num_clips, increment);
1132 WARN_ONCE(true, "Dirty called with invalid display system.\n");
1136 vmw_fifo_flush(dev_priv, false);
1137 ttm_read_unlock(&dev_priv->reservation_sem);
1139 drm_modeset_unlock_all(dev_priv->dev);
1144 static const struct drm_framebuffer_funcs vmw_framebuffer_dmabuf_funcs = {
1145 .destroy = vmw_framebuffer_dmabuf_destroy,
1146 .dirty = vmw_framebuffer_dmabuf_dirty,
1150 * Pin the dmabuffer to the start of vram.
1152 static int vmw_framebuffer_pin(struct vmw_framebuffer *vfb)
1154 struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
1155 struct vmw_dma_buffer *buf;
1158 buf = vfb->dmabuf ? vmw_framebuffer_to_vfbd(&vfb->base)->buffer :
1159 vmw_framebuffer_to_vfbs(&vfb->base)->surface->res.backup;
1164 switch (dev_priv->active_display_unit) {
1166 vmw_overlay_pause_all(dev_priv);
1167 ret = vmw_dmabuf_pin_in_start_of_vram(dev_priv, buf, false);
1168 vmw_overlay_resume_all(dev_priv);
1170 case vmw_du_screen_object:
1171 case vmw_du_screen_target:
1173 return vmw_dmabuf_pin_in_vram_or_gmr(dev_priv, buf,
1176 return vmw_dmabuf_pin_in_placement(dev_priv, buf,
1177 &vmw_mob_placement, false);
1185 static int vmw_framebuffer_unpin(struct vmw_framebuffer *vfb)
1187 struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
1188 struct vmw_dma_buffer *buf;
1190 buf = vfb->dmabuf ? vmw_framebuffer_to_vfbd(&vfb->base)->buffer :
1191 vmw_framebuffer_to_vfbs(&vfb->base)->surface->res.backup;
1196 return vmw_dmabuf_unpin(dev_priv, buf, false);
1200 * vmw_create_dmabuf_proxy - create a proxy surface for the DMA buf
1203 * @mode_cmd: parameters for the new surface
1204 * @dmabuf_mob: MOB backing the DMA buf
1205 * @srf_out: newly created surface
1207 * When the content FB is a DMA buf, we create a surface as a proxy to the
1208 * same buffer. This way we can do a surface copy rather than a surface DMA.
1209 * This is a more efficient approach
1212 * 0 on success, error code otherwise
1214 static int vmw_create_dmabuf_proxy(struct drm_device *dev,
1215 const struct drm_mode_fb_cmd2 *mode_cmd,
1216 struct vmw_dma_buffer *dmabuf_mob,
1217 struct vmw_surface **srf_out)
1220 struct drm_vmw_size content_base_size = {0};
1221 struct vmw_resource *res;
1222 unsigned int bytes_pp;
1223 struct drm_format_name_buf format_name;
1226 switch (mode_cmd->pixel_format) {
1227 case DRM_FORMAT_ARGB8888:
1228 case DRM_FORMAT_XRGB8888:
1229 format = SVGA3D_X8R8G8B8;
1233 case DRM_FORMAT_RGB565:
1234 case DRM_FORMAT_XRGB1555:
1235 format = SVGA3D_R5G6B5;
1245 DRM_ERROR("Invalid framebuffer format %s\n",
1246 drm_get_format_name(mode_cmd->pixel_format, &format_name));
1250 content_base_size.width = mode_cmd->pitches[0] / bytes_pp;
1251 content_base_size.height = mode_cmd->height;
1252 content_base_size.depth = 1;
1254 ret = vmw_surface_gb_priv_define(dev,
1255 0, /* kernel visible only */
1258 true, /* can be a scanout buffer */
1259 1, /* num of mip levels */
1265 DRM_ERROR("Failed to allocate proxy content buffer\n");
1269 res = &(*srf_out)->res;
1271 /* Reserve and switch the backing mob. */
1272 mutex_lock(&res->dev_priv->cmdbuf_mutex);
1273 (void) vmw_resource_reserve(res, false, true);
1274 vmw_dmabuf_unreference(&res->backup);
1275 res->backup = vmw_dmabuf_reference(dmabuf_mob);
1276 res->backup_offset = 0;
1277 vmw_resource_unreserve(res, false, NULL, 0);
1278 mutex_unlock(&res->dev_priv->cmdbuf_mutex);
1285 static int vmw_kms_new_framebuffer_dmabuf(struct vmw_private *dev_priv,
1286 struct vmw_dma_buffer *dmabuf,
1287 struct vmw_framebuffer **out,
1288 const struct drm_mode_fb_cmd2
1292 struct drm_device *dev = dev_priv->dev;
1293 struct vmw_framebuffer_dmabuf *vfbd;
1294 unsigned int requested_size;
1295 struct drm_format_name_buf format_name;
1298 requested_size = mode_cmd->height * mode_cmd->pitches[0];
1299 if (unlikely(requested_size > dmabuf->base.num_pages * PAGE_SIZE)) {
1300 DRM_ERROR("Screen buffer object size is too small "
1301 "for requested mode.\n");
1305 /* Limited framebuffer color depth support for screen objects */
1306 if (dev_priv->active_display_unit == vmw_du_screen_object) {
1307 switch (mode_cmd->pixel_format) {
1308 case DRM_FORMAT_XRGB8888:
1309 case DRM_FORMAT_ARGB8888:
1311 case DRM_FORMAT_XRGB1555:
1312 case DRM_FORMAT_RGB565:
1315 DRM_ERROR("Invalid pixel format: %s\n",
1316 drm_get_format_name(mode_cmd->pixel_format, &format_name));
1321 vfbd = kzalloc(sizeof(*vfbd), GFP_KERNEL);
1327 drm_helper_mode_fill_fb_struct(dev, &vfbd->base.base, mode_cmd);
1328 vfbd->base.dmabuf = true;
1329 vfbd->buffer = vmw_dmabuf_reference(dmabuf);
1330 vfbd->base.user_handle = mode_cmd->handles[0];
1333 ret = drm_framebuffer_init(dev, &vfbd->base.base,
1334 &vmw_framebuffer_dmabuf_funcs);
1341 vmw_dmabuf_unreference(&dmabuf);
1349 * vmw_kms_srf_ok - check if a surface can be created
1351 * @width: requested width
1352 * @height: requested height
1354 * Surfaces need to be less than texture size
1357 vmw_kms_srf_ok(struct vmw_private *dev_priv, uint32_t width, uint32_t height)
1359 if (width > dev_priv->texture_max_width ||
1360 height > dev_priv->texture_max_height)
1367 * vmw_kms_new_framebuffer - Create a new framebuffer.
1369 * @dev_priv: Pointer to device private struct.
1370 * @dmabuf: Pointer to dma buffer to wrap the kms framebuffer around.
1371 * Either @dmabuf or @surface must be NULL.
1372 * @surface: Pointer to a surface to wrap the kms framebuffer around.
1373 * Either @dmabuf or @surface must be NULL.
1374 * @only_2d: No presents will occur to this dma buffer based framebuffer. This
1375 * Helps the code to do some important optimizations.
1376 * @mode_cmd: Frame-buffer metadata.
1378 struct vmw_framebuffer *
1379 vmw_kms_new_framebuffer(struct vmw_private *dev_priv,
1380 struct vmw_dma_buffer *dmabuf,
1381 struct vmw_surface *surface,
1383 const struct drm_mode_fb_cmd2 *mode_cmd)
1385 struct vmw_framebuffer *vfb = NULL;
1386 bool is_dmabuf_proxy = false;
1390 * We cannot use the SurfaceDMA command in an non-accelerated VM,
1391 * therefore, wrap the DMA buf in a surface so we can use the
1392 * SurfaceCopy command.
1394 if (vmw_kms_srf_ok(dev_priv, mode_cmd->width, mode_cmd->height) &&
1395 dmabuf && only_2d &&
1396 mode_cmd->width > 64 && /* Don't create a proxy for cursor */
1397 dev_priv->active_display_unit == vmw_du_screen_target) {
1398 ret = vmw_create_dmabuf_proxy(dev_priv->dev, mode_cmd,
1401 return ERR_PTR(ret);
1403 is_dmabuf_proxy = true;
1406 /* Create the new framebuffer depending one what we have */
1408 ret = vmw_kms_new_framebuffer_surface(dev_priv, surface, &vfb,
1413 * vmw_create_dmabuf_proxy() adds a reference that is no longer
1416 if (is_dmabuf_proxy)
1417 vmw_surface_unreference(&surface);
1418 } else if (dmabuf) {
1419 ret = vmw_kms_new_framebuffer_dmabuf(dev_priv, dmabuf, &vfb,
1426 return ERR_PTR(ret);
1428 vfb->pin = vmw_framebuffer_pin;
1429 vfb->unpin = vmw_framebuffer_unpin;
1435 * Generic Kernel modesetting functions
1438 static struct drm_framebuffer *vmw_kms_fb_create(struct drm_device *dev,
1439 struct drm_file *file_priv,
1440 const struct drm_mode_fb_cmd2 *mode_cmd)
1442 struct vmw_private *dev_priv = vmw_priv(dev);
1443 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
1444 struct vmw_framebuffer *vfb = NULL;
1445 struct vmw_surface *surface = NULL;
1446 struct vmw_dma_buffer *bo = NULL;
1447 struct ttm_base_object *user_obj;
1451 * This code should be conditioned on Screen Objects not being used.
1452 * If screen objects are used, we can allocate a GMR to hold the
1453 * requested framebuffer.
1456 if (!vmw_kms_validate_mode_vram(dev_priv,
1457 mode_cmd->pitches[0],
1458 mode_cmd->height)) {
1459 DRM_ERROR("Requested mode exceed bounding box limit.\n");
1460 return ERR_PTR(-ENOMEM);
1464 * Take a reference on the user object of the resource
1465 * backing the kms fb. This ensures that user-space handle
1466 * lookups on that resource will always work as long as
1467 * it's registered with a kms framebuffer. This is important,
1468 * since vmw_execbuf_process identifies resources in the
1469 * command stream using user-space handles.
1472 user_obj = ttm_base_object_lookup(tfile, mode_cmd->handles[0]);
1473 if (unlikely(user_obj == NULL)) {
1474 DRM_ERROR("Could not locate requested kms frame buffer.\n");
1475 return ERR_PTR(-ENOENT);
1479 * End conditioned code.
1482 /* returns either a dmabuf or surface */
1483 ret = vmw_user_lookup_handle(dev_priv, tfile,
1484 mode_cmd->handles[0],
1491 !vmw_kms_srf_ok(dev_priv, mode_cmd->width, mode_cmd->height)) {
1492 DRM_ERROR("Surface size cannot exceed %dx%d",
1493 dev_priv->texture_max_width,
1494 dev_priv->texture_max_height);
1499 vfb = vmw_kms_new_framebuffer(dev_priv, bo, surface,
1500 !(dev_priv->capabilities & SVGA_CAP_3D),
1508 /* vmw_user_lookup_handle takes one ref so does new_fb */
1510 vmw_dmabuf_unreference(&bo);
1512 vmw_surface_unreference(&surface);
1515 DRM_ERROR("failed to create vmw_framebuffer: %i\n", ret);
1516 ttm_base_object_unref(&user_obj);
1517 return ERR_PTR(ret);
1519 vfb->user_obj = user_obj;
1527 * vmw_kms_atomic_check_modeset- validate state object for modeset changes
1530 * @state: the driver state object
1532 * This is a simple wrapper around drm_atomic_helper_check_modeset() for
1533 * us to assign a value to mode->crtc_clock so that
1534 * drm_calc_timestamping_constants() won't throw an error message
1537 * Zero for success or -errno
1540 vmw_kms_atomic_check_modeset(struct drm_device *dev,
1541 struct drm_atomic_state *state)
1543 struct drm_crtc_state *crtc_state;
1544 struct drm_crtc *crtc;
1545 struct vmw_private *dev_priv = vmw_priv(dev);
1549 for_each_crtc_in_state(state, crtc, crtc_state, i) {
1550 unsigned long requested_bb_mem = 0;
1552 if (dev_priv->active_display_unit == vmw_du_screen_target) {
1553 if (crtc->primary->fb) {
1554 int cpp = crtc->primary->fb->pitches[0] /
1555 crtc->primary->fb->width;
1557 requested_bb_mem += crtc->mode.hdisplay * cpp *
1558 crtc->mode.vdisplay;
1561 if (requested_bb_mem > dev_priv->prim_bb_mem)
1566 return drm_atomic_helper_check(dev, state);
1570 static const struct drm_mode_config_funcs vmw_kms_funcs = {
1571 .fb_create = vmw_kms_fb_create,
1572 .atomic_check = vmw_kms_atomic_check_modeset,
1573 .atomic_commit = drm_atomic_helper_commit,
1576 static int vmw_kms_generic_present(struct vmw_private *dev_priv,
1577 struct drm_file *file_priv,
1578 struct vmw_framebuffer *vfb,
1579 struct vmw_surface *surface,
1581 int32_t destX, int32_t destY,
1582 struct drm_vmw_rect *clips,
1585 return vmw_kms_sou_do_surface_dirty(dev_priv, vfb, NULL, clips,
1586 &surface->res, destX, destY,
1587 num_clips, 1, NULL);
1591 int vmw_kms_present(struct vmw_private *dev_priv,
1592 struct drm_file *file_priv,
1593 struct vmw_framebuffer *vfb,
1594 struct vmw_surface *surface,
1596 int32_t destX, int32_t destY,
1597 struct drm_vmw_rect *clips,
1602 switch (dev_priv->active_display_unit) {
1603 case vmw_du_screen_target:
1604 ret = vmw_kms_stdu_surface_dirty(dev_priv, vfb, NULL, clips,
1605 &surface->res, destX, destY,
1606 num_clips, 1, NULL);
1608 case vmw_du_screen_object:
1609 ret = vmw_kms_generic_present(dev_priv, file_priv, vfb, surface,
1610 sid, destX, destY, clips,
1615 "Present called with invalid display system.\n");
1622 vmw_fifo_flush(dev_priv, false);
1628 vmw_kms_create_hotplug_mode_update_property(struct vmw_private *dev_priv)
1630 if (dev_priv->hotplug_mode_update_property)
1633 dev_priv->hotplug_mode_update_property =
1634 drm_property_create_range(dev_priv->dev,
1635 DRM_MODE_PROP_IMMUTABLE,
1636 "hotplug_mode_update", 0, 1);
1638 if (!dev_priv->hotplug_mode_update_property)
1643 int vmw_kms_init(struct vmw_private *dev_priv)
1645 struct drm_device *dev = dev_priv->dev;
1648 drm_mode_config_init(dev);
1649 dev->mode_config.funcs = &vmw_kms_funcs;
1650 dev->mode_config.min_width = 1;
1651 dev->mode_config.min_height = 1;
1652 dev->mode_config.max_width = dev_priv->texture_max_width;
1653 dev->mode_config.max_height = dev_priv->texture_max_height;
1655 drm_mode_create_suggested_offset_properties(dev);
1656 vmw_kms_create_hotplug_mode_update_property(dev_priv);
1658 ret = vmw_kms_stdu_init_display(dev_priv);
1660 ret = vmw_kms_sou_init_display(dev_priv);
1661 if (ret) /* Fallback */
1662 ret = vmw_kms_ldu_init_display(dev_priv);
1668 int vmw_kms_close(struct vmw_private *dev_priv)
1673 * Docs says we should take the lock before calling this function
1674 * but since it destroys encoders and our destructor calls
1675 * drm_encoder_cleanup which takes the lock we deadlock.
1677 drm_mode_config_cleanup(dev_priv->dev);
1678 if (dev_priv->active_display_unit == vmw_du_screen_object)
1679 ret = vmw_kms_sou_close_display(dev_priv);
1680 else if (dev_priv->active_display_unit == vmw_du_screen_target)
1681 ret = vmw_kms_stdu_close_display(dev_priv);
1683 ret = vmw_kms_ldu_close_display(dev_priv);
1688 int vmw_kms_cursor_bypass_ioctl(struct drm_device *dev, void *data,
1689 struct drm_file *file_priv)
1691 struct drm_vmw_cursor_bypass_arg *arg = data;
1692 struct vmw_display_unit *du;
1693 struct drm_crtc *crtc;
1697 mutex_lock(&dev->mode_config.mutex);
1698 if (arg->flags & DRM_VMW_CURSOR_BYPASS_ALL) {
1700 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1701 du = vmw_crtc_to_du(crtc);
1702 du->hotspot_x = arg->xhot;
1703 du->hotspot_y = arg->yhot;
1706 mutex_unlock(&dev->mode_config.mutex);
1710 crtc = drm_crtc_find(dev, arg->crtc_id);
1716 du = vmw_crtc_to_du(crtc);
1718 du->hotspot_x = arg->xhot;
1719 du->hotspot_y = arg->yhot;
1722 mutex_unlock(&dev->mode_config.mutex);
1727 int vmw_kms_write_svga(struct vmw_private *vmw_priv,
1728 unsigned width, unsigned height, unsigned pitch,
1729 unsigned bpp, unsigned depth)
1731 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1732 vmw_write(vmw_priv, SVGA_REG_PITCHLOCK, pitch);
1733 else if (vmw_fifo_have_pitchlock(vmw_priv))
1734 vmw_mmio_write(pitch, vmw_priv->mmio_virt +
1735 SVGA_FIFO_PITCHLOCK);
1736 vmw_write(vmw_priv, SVGA_REG_WIDTH, width);
1737 vmw_write(vmw_priv, SVGA_REG_HEIGHT, height);
1738 vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, bpp);
1740 if (vmw_read(vmw_priv, SVGA_REG_DEPTH) != depth) {
1741 DRM_ERROR("Invalid depth %u for %u bpp, host expects %u\n",
1742 depth, bpp, vmw_read(vmw_priv, SVGA_REG_DEPTH));
1749 int vmw_kms_save_vga(struct vmw_private *vmw_priv)
1751 struct vmw_vga_topology_state *save;
1754 vmw_priv->vga_width = vmw_read(vmw_priv, SVGA_REG_WIDTH);
1755 vmw_priv->vga_height = vmw_read(vmw_priv, SVGA_REG_HEIGHT);
1756 vmw_priv->vga_bpp = vmw_read(vmw_priv, SVGA_REG_BITS_PER_PIXEL);
1757 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1758 vmw_priv->vga_pitchlock =
1759 vmw_read(vmw_priv, SVGA_REG_PITCHLOCK);
1760 else if (vmw_fifo_have_pitchlock(vmw_priv))
1761 vmw_priv->vga_pitchlock = vmw_mmio_read(vmw_priv->mmio_virt +
1762 SVGA_FIFO_PITCHLOCK);
1764 if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
1767 vmw_priv->num_displays = vmw_read(vmw_priv,
1768 SVGA_REG_NUM_GUEST_DISPLAYS);
1770 if (vmw_priv->num_displays == 0)
1771 vmw_priv->num_displays = 1;
1773 for (i = 0; i < vmw_priv->num_displays; ++i) {
1774 save = &vmw_priv->vga_save[i];
1775 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
1776 save->primary = vmw_read(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY);
1777 save->pos_x = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_X);
1778 save->pos_y = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y);
1779 save->width = vmw_read(vmw_priv, SVGA_REG_DISPLAY_WIDTH);
1780 save->height = vmw_read(vmw_priv, SVGA_REG_DISPLAY_HEIGHT);
1781 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
1782 if (i == 0 && vmw_priv->num_displays == 1 &&
1783 save->width == 0 && save->height == 0) {
1786 * It should be fairly safe to assume that these
1787 * values are uninitialized.
1790 save->width = vmw_priv->vga_width - save->pos_x;
1791 save->height = vmw_priv->vga_height - save->pos_y;
1798 int vmw_kms_restore_vga(struct vmw_private *vmw_priv)
1800 struct vmw_vga_topology_state *save;
1803 vmw_write(vmw_priv, SVGA_REG_WIDTH, vmw_priv->vga_width);
1804 vmw_write(vmw_priv, SVGA_REG_HEIGHT, vmw_priv->vga_height);
1805 vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, vmw_priv->vga_bpp);
1806 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1807 vmw_write(vmw_priv, SVGA_REG_PITCHLOCK,
1808 vmw_priv->vga_pitchlock);
1809 else if (vmw_fifo_have_pitchlock(vmw_priv))
1810 vmw_mmio_write(vmw_priv->vga_pitchlock,
1811 vmw_priv->mmio_virt + SVGA_FIFO_PITCHLOCK);
1813 if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
1816 for (i = 0; i < vmw_priv->num_displays; ++i) {
1817 save = &vmw_priv->vga_save[i];
1818 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
1819 vmw_write(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY, save->primary);
1820 vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_X, save->pos_x);
1821 vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y, save->pos_y);
1822 vmw_write(vmw_priv, SVGA_REG_DISPLAY_WIDTH, save->width);
1823 vmw_write(vmw_priv, SVGA_REG_DISPLAY_HEIGHT, save->height);
1824 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
1830 bool vmw_kms_validate_mode_vram(struct vmw_private *dev_priv,
1834 return ((u64) pitch * (u64) height) < (u64)
1835 ((dev_priv->active_display_unit == vmw_du_screen_target) ?
1836 dev_priv->prim_bb_mem : dev_priv->vram_size);
1841 * Function called by DRM code called with vbl_lock held.
1843 u32 vmw_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
1849 * Function called by DRM code called with vbl_lock held.
1851 int vmw_enable_vblank(struct drm_device *dev, unsigned int pipe)
1857 * Function called by DRM code called with vbl_lock held.
1859 void vmw_disable_vblank(struct drm_device *dev, unsigned int pipe)
1865 * Small shared kms functions.
1868 static int vmw_du_update_layout(struct vmw_private *dev_priv, unsigned num,
1869 struct drm_vmw_rect *rects)
1871 struct drm_device *dev = dev_priv->dev;
1872 struct vmw_display_unit *du;
1873 struct drm_connector *con;
1875 mutex_lock(&dev->mode_config.mutex);
1881 DRM_INFO("%s: new layout ", __func__);
1882 for (i = 0; i < num; i++)
1883 DRM_INFO("(%i, %i %ux%u) ", rects[i].x, rects[i].y,
1884 rects[i].w, rects[i].h);
1889 list_for_each_entry(con, &dev->mode_config.connector_list, head) {
1890 du = vmw_connector_to_du(con);
1891 if (num > du->unit) {
1892 du->pref_width = rects[du->unit].w;
1893 du->pref_height = rects[du->unit].h;
1894 du->pref_active = true;
1895 du->gui_x = rects[du->unit].x;
1896 du->gui_y = rects[du->unit].y;
1897 drm_object_property_set_value
1898 (&con->base, dev->mode_config.suggested_x_property,
1900 drm_object_property_set_value
1901 (&con->base, dev->mode_config.suggested_y_property,
1904 du->pref_width = 800;
1905 du->pref_height = 600;
1906 du->pref_active = false;
1907 drm_object_property_set_value
1908 (&con->base, dev->mode_config.suggested_x_property,
1910 drm_object_property_set_value
1911 (&con->base, dev->mode_config.suggested_y_property,
1914 con->status = vmw_du_connector_detect(con, true);
1917 mutex_unlock(&dev->mode_config.mutex);
1918 drm_sysfs_hotplug_event(dev);
1923 int vmw_du_crtc_gamma_set(struct drm_crtc *crtc,
1924 u16 *r, u16 *g, u16 *b,
1926 struct drm_modeset_acquire_ctx *ctx)
1928 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
1931 for (i = 0; i < size; i++) {
1932 DRM_DEBUG("%d r/g/b = 0x%04x / 0x%04x / 0x%04x\n", i,
1934 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 0, r[i] >> 8);
1935 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 1, g[i] >> 8);
1936 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 2, b[i] >> 8);
1942 int vmw_du_connector_dpms(struct drm_connector *connector, int mode)
1947 enum drm_connector_status
1948 vmw_du_connector_detect(struct drm_connector *connector, bool force)
1950 uint32_t num_displays;
1951 struct drm_device *dev = connector->dev;
1952 struct vmw_private *dev_priv = vmw_priv(dev);
1953 struct vmw_display_unit *du = vmw_connector_to_du(connector);
1955 num_displays = vmw_read(dev_priv, SVGA_REG_NUM_DISPLAYS);
1957 return ((vmw_connector_to_du(connector)->unit < num_displays &&
1959 connector_status_connected : connector_status_disconnected);
1962 static struct drm_display_mode vmw_kms_connector_builtin[] = {
1964 { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 25175, 640, 656,
1965 752, 800, 0, 480, 489, 492, 525, 0,
1966 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
1968 { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 40000, 800, 840,
1969 968, 1056, 0, 600, 601, 605, 628, 0,
1970 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1972 { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 65000, 1024, 1048,
1973 1184, 1344, 0, 768, 771, 777, 806, 0,
1974 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
1976 { DRM_MODE("1152x864", DRM_MODE_TYPE_DRIVER, 108000, 1152, 1216,
1977 1344, 1600, 0, 864, 865, 868, 900, 0,
1978 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1980 { DRM_MODE("1280x768", DRM_MODE_TYPE_DRIVER, 79500, 1280, 1344,
1981 1472, 1664, 0, 768, 771, 778, 798, 0,
1982 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1984 { DRM_MODE("1280x800", DRM_MODE_TYPE_DRIVER, 83500, 1280, 1352,
1985 1480, 1680, 0, 800, 803, 809, 831, 0,
1986 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
1988 { DRM_MODE("1280x960", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1376,
1989 1488, 1800, 0, 960, 961, 964, 1000, 0,
1990 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1991 /* 1280x1024@60Hz */
1992 { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1328,
1993 1440, 1688, 0, 1024, 1025, 1028, 1066, 0,
1994 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1996 { DRM_MODE("1360x768", DRM_MODE_TYPE_DRIVER, 85500, 1360, 1424,
1997 1536, 1792, 0, 768, 771, 777, 795, 0,
1998 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1999 /* 1440x1050@60Hz */
2000 { DRM_MODE("1400x1050", DRM_MODE_TYPE_DRIVER, 121750, 1400, 1488,
2001 1632, 1864, 0, 1050, 1053, 1057, 1089, 0,
2002 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2004 { DRM_MODE("1440x900", DRM_MODE_TYPE_DRIVER, 106500, 1440, 1520,
2005 1672, 1904, 0, 900, 903, 909, 934, 0,
2006 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2007 /* 1600x1200@60Hz */
2008 { DRM_MODE("1600x1200", DRM_MODE_TYPE_DRIVER, 162000, 1600, 1664,
2009 1856, 2160, 0, 1200, 1201, 1204, 1250, 0,
2010 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
2011 /* 1680x1050@60Hz */
2012 { DRM_MODE("1680x1050", DRM_MODE_TYPE_DRIVER, 146250, 1680, 1784,
2013 1960, 2240, 0, 1050, 1053, 1059, 1089, 0,
2014 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2015 /* 1792x1344@60Hz */
2016 { DRM_MODE("1792x1344", DRM_MODE_TYPE_DRIVER, 204750, 1792, 1920,
2017 2120, 2448, 0, 1344, 1345, 1348, 1394, 0,
2018 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2019 /* 1853x1392@60Hz */
2020 { DRM_MODE("1856x1392", DRM_MODE_TYPE_DRIVER, 218250, 1856, 1952,
2021 2176, 2528, 0, 1392, 1393, 1396, 1439, 0,
2022 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2023 /* 1920x1200@60Hz */
2024 { DRM_MODE("1920x1200", DRM_MODE_TYPE_DRIVER, 193250, 1920, 2056,
2025 2256, 2592, 0, 1200, 1203, 1209, 1245, 0,
2026 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2027 /* 1920x1440@60Hz */
2028 { DRM_MODE("1920x1440", DRM_MODE_TYPE_DRIVER, 234000, 1920, 2048,
2029 2256, 2600, 0, 1440, 1441, 1444, 1500, 0,
2030 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2031 /* 2560x1600@60Hz */
2032 { DRM_MODE("2560x1600", DRM_MODE_TYPE_DRIVER, 348500, 2560, 2752,
2033 3032, 3504, 0, 1600, 1603, 1609, 1658, 0,
2034 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2036 { DRM_MODE("", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) },
2040 * vmw_guess_mode_timing - Provide fake timings for a
2041 * 60Hz vrefresh mode.
2043 * @mode - Pointer to a struct drm_display_mode with hdisplay and vdisplay
2044 * members filled in.
2046 void vmw_guess_mode_timing(struct drm_display_mode *mode)
2048 mode->hsync_start = mode->hdisplay + 50;
2049 mode->hsync_end = mode->hsync_start + 50;
2050 mode->htotal = mode->hsync_end + 50;
2052 mode->vsync_start = mode->vdisplay + 50;
2053 mode->vsync_end = mode->vsync_start + 50;
2054 mode->vtotal = mode->vsync_end + 50;
2056 mode->clock = (u32)mode->htotal * (u32)mode->vtotal / 100 * 6;
2057 mode->vrefresh = drm_mode_vrefresh(mode);
2061 int vmw_du_connector_fill_modes(struct drm_connector *connector,
2062 uint32_t max_width, uint32_t max_height)
2064 struct vmw_display_unit *du = vmw_connector_to_du(connector);
2065 struct drm_device *dev = connector->dev;
2066 struct vmw_private *dev_priv = vmw_priv(dev);
2067 struct drm_display_mode *mode = NULL;
2068 struct drm_display_mode *bmode;
2069 struct drm_display_mode prefmode = { DRM_MODE("preferred",
2070 DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED,
2071 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2072 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC)
2075 u32 assumed_bpp = 4;
2077 if (dev_priv->assume_16bpp)
2080 if (dev_priv->active_display_unit == vmw_du_screen_target) {
2081 max_width = min(max_width, dev_priv->stdu_max_width);
2082 max_width = min(max_width, dev_priv->texture_max_width);
2084 max_height = min(max_height, dev_priv->stdu_max_height);
2085 max_height = min(max_height, dev_priv->texture_max_height);
2088 /* Add preferred mode */
2089 mode = drm_mode_duplicate(dev, &prefmode);
2092 mode->hdisplay = du->pref_width;
2093 mode->vdisplay = du->pref_height;
2094 vmw_guess_mode_timing(mode);
2096 if (vmw_kms_validate_mode_vram(dev_priv,
2097 mode->hdisplay * assumed_bpp,
2099 drm_mode_probed_add(connector, mode);
2101 drm_mode_destroy(dev, mode);
2105 if (du->pref_mode) {
2106 list_del_init(&du->pref_mode->head);
2107 drm_mode_destroy(dev, du->pref_mode);
2110 /* mode might be null here, this is intended */
2111 du->pref_mode = mode;
2113 for (i = 0; vmw_kms_connector_builtin[i].type != 0; i++) {
2114 bmode = &vmw_kms_connector_builtin[i];
2115 if (bmode->hdisplay > max_width ||
2116 bmode->vdisplay > max_height)
2119 if (!vmw_kms_validate_mode_vram(dev_priv,
2120 bmode->hdisplay * assumed_bpp,
2124 mode = drm_mode_duplicate(dev, bmode);
2127 mode->vrefresh = drm_mode_vrefresh(mode);
2129 drm_mode_probed_add(connector, mode);
2132 drm_mode_connector_list_update(connector);
2133 /* Move the prefered mode first, help apps pick the right mode. */
2134 drm_mode_sort(&connector->modes);
2139 int vmw_du_connector_set_property(struct drm_connector *connector,
2140 struct drm_property *property,
2143 struct vmw_display_unit *du = vmw_connector_to_du(connector);
2144 struct vmw_private *dev_priv = vmw_priv(connector->dev);
2146 if (property == dev_priv->implicit_placement_property)
2147 du->is_implicit = val;
2155 * vmw_du_connector_atomic_set_property - Atomic version of get property
2157 * @crtc - crtc the property is associated with
2160 * Zero on success, negative errno on failure.
2163 vmw_du_connector_atomic_set_property(struct drm_connector *connector,
2164 struct drm_connector_state *state,
2165 struct drm_property *property,
2168 struct vmw_private *dev_priv = vmw_priv(connector->dev);
2169 struct vmw_connector_state *vcs = vmw_connector_state_to_vcs(state);
2170 struct vmw_display_unit *du = vmw_connector_to_du(connector);
2173 if (property == dev_priv->implicit_placement_property) {
2174 vcs->is_implicit = val;
2177 * We should really be doing a drm_atomic_commit() to
2178 * commit the new state, but since this doesn't cause
2179 * an immedate state change, this is probably ok
2181 du->is_implicit = vcs->is_implicit;
2191 * vmw_du_connector_atomic_get_property - Atomic version of get property
2193 * @connector - connector the property is associated with
2196 * Zero on success, negative errno on failure.
2199 vmw_du_connector_atomic_get_property(struct drm_connector *connector,
2200 const struct drm_connector_state *state,
2201 struct drm_property *property,
2204 struct vmw_private *dev_priv = vmw_priv(connector->dev);
2205 struct vmw_connector_state *vcs = vmw_connector_state_to_vcs(state);
2207 if (property == dev_priv->implicit_placement_property)
2208 *val = vcs->is_implicit;
2210 DRM_ERROR("Invalid Property %s\n", property->name);
2218 int vmw_kms_update_layout_ioctl(struct drm_device *dev, void *data,
2219 struct drm_file *file_priv)
2221 struct vmw_private *dev_priv = vmw_priv(dev);
2222 struct drm_vmw_update_layout_arg *arg =
2223 (struct drm_vmw_update_layout_arg *)data;
2224 void __user *user_rects;
2225 struct drm_vmw_rect *rects;
2226 unsigned rects_size;
2229 u64 total_pixels = 0;
2230 struct drm_mode_config *mode_config = &dev->mode_config;
2231 struct drm_vmw_rect bounding_box = {0};
2233 if (!arg->num_outputs) {
2234 struct drm_vmw_rect def_rect = {0, 0, 800, 600};
2235 vmw_du_update_layout(dev_priv, 1, &def_rect);
2239 rects_size = arg->num_outputs * sizeof(struct drm_vmw_rect);
2240 rects = kcalloc(arg->num_outputs, sizeof(struct drm_vmw_rect),
2242 if (unlikely(!rects))
2245 user_rects = (void __user *)(unsigned long)arg->rects;
2246 ret = copy_from_user(rects, user_rects, rects_size);
2247 if (unlikely(ret != 0)) {
2248 DRM_ERROR("Failed to get rects.\n");
2253 for (i = 0; i < arg->num_outputs; ++i) {
2254 if (rects[i].x < 0 ||
2256 rects[i].x + rects[i].w > mode_config->max_width ||
2257 rects[i].y + rects[i].h > mode_config->max_height) {
2258 DRM_ERROR("Invalid GUI layout.\n");
2264 * bounding_box.w and bunding_box.h are used as
2265 * lower-right coordinates
2267 if (rects[i].x + rects[i].w > bounding_box.w)
2268 bounding_box.w = rects[i].x + rects[i].w;
2270 if (rects[i].y + rects[i].h > bounding_box.h)
2271 bounding_box.h = rects[i].y + rects[i].h;
2273 total_pixels += (u64) rects[i].w * (u64) rects[i].h;
2276 if (dev_priv->active_display_unit == vmw_du_screen_target) {
2278 * For Screen Targets, the limits for a toplogy are:
2279 * 1. Bounding box (assuming 32bpp) must be < prim_bb_mem
2280 * 2. Total pixels (assuming 32bpp) must be < prim_bb_mem
2282 u64 bb_mem = (u64) bounding_box.w * bounding_box.h * 4;
2283 u64 pixel_mem = total_pixels * 4;
2285 if (bb_mem > dev_priv->prim_bb_mem) {
2286 DRM_ERROR("Topology is beyond supported limits.\n");
2291 if (pixel_mem > dev_priv->prim_bb_mem) {
2292 DRM_ERROR("Combined output size too large\n");
2298 vmw_du_update_layout(dev_priv, arg->num_outputs, rects);
2306 * vmw_kms_helper_dirty - Helper to build commands and perform actions based
2307 * on a set of cliprects and a set of display units.
2309 * @dev_priv: Pointer to a device private structure.
2310 * @framebuffer: Pointer to the framebuffer on which to perform the actions.
2311 * @clips: A set of struct drm_clip_rect. Either this os @vclips must be NULL.
2312 * Cliprects are given in framebuffer coordinates.
2313 * @vclips: A set of struct drm_vmw_rect cliprects. Either this or @clips must
2314 * be NULL. Cliprects are given in source coordinates.
2315 * @dest_x: X coordinate offset for the crtc / destination clip rects.
2316 * @dest_y: Y coordinate offset for the crtc / destination clip rects.
2317 * @num_clips: Number of cliprects in the @clips or @vclips array.
2318 * @increment: Integer with which to increment the clip counter when looping.
2319 * Used to skip a predetermined number of clip rects.
2320 * @dirty: Closure structure. See the description of struct vmw_kms_dirty.
2322 int vmw_kms_helper_dirty(struct vmw_private *dev_priv,
2323 struct vmw_framebuffer *framebuffer,
2324 const struct drm_clip_rect *clips,
2325 const struct drm_vmw_rect *vclips,
2326 s32 dest_x, s32 dest_y,
2329 struct vmw_kms_dirty *dirty)
2331 struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
2332 struct drm_crtc *crtc;
2336 dirty->dev_priv = dev_priv;
2338 list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list, head) {
2339 if (crtc->primary->fb != &framebuffer->base)
2341 units[num_units++] = vmw_crtc_to_du(crtc);
2344 for (k = 0; k < num_units; k++) {
2345 struct vmw_display_unit *unit = units[k];
2346 s32 crtc_x = unit->crtc.x;
2347 s32 crtc_y = unit->crtc.y;
2348 s32 crtc_width = unit->crtc.mode.hdisplay;
2349 s32 crtc_height = unit->crtc.mode.vdisplay;
2350 const struct drm_clip_rect *clips_ptr = clips;
2351 const struct drm_vmw_rect *vclips_ptr = vclips;
2354 if (dirty->fifo_reserve_size > 0) {
2355 dirty->cmd = vmw_fifo_reserve(dev_priv,
2356 dirty->fifo_reserve_size);
2358 DRM_ERROR("Couldn't reserve fifo space "
2359 "for dirty blits.\n");
2362 memset(dirty->cmd, 0, dirty->fifo_reserve_size);
2364 dirty->num_hits = 0;
2365 for (i = 0; i < num_clips; i++, clips_ptr += increment,
2366 vclips_ptr += increment) {
2371 * Select clip array type. Note that integer type
2372 * in @clips is unsigned short, whereas in @vclips
2376 dirty->fb_x = (s32) clips_ptr->x1;
2377 dirty->fb_y = (s32) clips_ptr->y1;
2378 dirty->unit_x2 = (s32) clips_ptr->x2 + dest_x -
2380 dirty->unit_y2 = (s32) clips_ptr->y2 + dest_y -
2383 dirty->fb_x = vclips_ptr->x;
2384 dirty->fb_y = vclips_ptr->y;
2385 dirty->unit_x2 = dirty->fb_x + vclips_ptr->w +
2387 dirty->unit_y2 = dirty->fb_y + vclips_ptr->h +
2391 dirty->unit_x1 = dirty->fb_x + dest_x - crtc_x;
2392 dirty->unit_y1 = dirty->fb_y + dest_y - crtc_y;
2394 /* Skip this clip if it's outside the crtc region */
2395 if (dirty->unit_x1 >= crtc_width ||
2396 dirty->unit_y1 >= crtc_height ||
2397 dirty->unit_x2 <= 0 || dirty->unit_y2 <= 0)
2400 /* Clip right and bottom to crtc limits */
2401 dirty->unit_x2 = min_t(s32, dirty->unit_x2,
2403 dirty->unit_y2 = min_t(s32, dirty->unit_y2,
2406 /* Clip left and top to crtc limits */
2407 clip_left = min_t(s32, dirty->unit_x1, 0);
2408 clip_top = min_t(s32, dirty->unit_y1, 0);
2409 dirty->unit_x1 -= clip_left;
2410 dirty->unit_y1 -= clip_top;
2411 dirty->fb_x -= clip_left;
2412 dirty->fb_y -= clip_top;
2417 dirty->fifo_commit(dirty);
2424 * vmw_kms_helper_buffer_prepare - Reserve and validate a buffer object before
2425 * command submission.
2427 * @dev_priv. Pointer to a device private structure.
2428 * @buf: The buffer object
2429 * @interruptible: Whether to perform waits as interruptible.
2430 * @validate_as_mob: Whether the buffer should be validated as a MOB. If false,
2431 * The buffer will be validated as a GMR. Already pinned buffers will not be
2434 * Returns 0 on success, negative error code on failure, -ERESTARTSYS if
2435 * interrupted by a signal.
2437 int vmw_kms_helper_buffer_prepare(struct vmw_private *dev_priv,
2438 struct vmw_dma_buffer *buf,
2440 bool validate_as_mob)
2442 struct ttm_buffer_object *bo = &buf->base;
2445 ttm_bo_reserve(bo, false, false, NULL);
2446 ret = vmw_validate_single_buffer(dev_priv, bo, interruptible,
2449 ttm_bo_unreserve(bo);
2455 * vmw_kms_helper_buffer_revert - Undo the actions of
2456 * vmw_kms_helper_buffer_prepare.
2458 * @res: Pointer to the buffer object.
2460 * Helper to be used if an error forces the caller to undo the actions of
2461 * vmw_kms_helper_buffer_prepare.
2463 void vmw_kms_helper_buffer_revert(struct vmw_dma_buffer *buf)
2466 ttm_bo_unreserve(&buf->base);
2470 * vmw_kms_helper_buffer_finish - Unreserve and fence a buffer object after
2471 * kms command submission.
2473 * @dev_priv: Pointer to a device private structure.
2474 * @file_priv: Pointer to a struct drm_file representing the caller's
2475 * connection. Must be set to NULL if @user_fence_rep is NULL, and conversely
2476 * if non-NULL, @user_fence_rep must be non-NULL.
2477 * @buf: The buffer object.
2478 * @out_fence: Optional pointer to a fence pointer. If non-NULL, a
2479 * ref-counted fence pointer is returned here.
2480 * @user_fence_rep: Optional pointer to a user-space provided struct
2481 * drm_vmw_fence_rep. If provided, @file_priv must also be provided and the
2482 * function copies fence data to user-space in a fail-safe manner.
2484 void vmw_kms_helper_buffer_finish(struct vmw_private *dev_priv,
2485 struct drm_file *file_priv,
2486 struct vmw_dma_buffer *buf,
2487 struct vmw_fence_obj **out_fence,
2488 struct drm_vmw_fence_rep __user *
2491 struct vmw_fence_obj *fence;
2495 ret = vmw_execbuf_fence_commands(file_priv, dev_priv, &fence,
2496 file_priv ? &handle : NULL);
2498 vmw_fence_single_bo(&buf->base, fence);
2500 vmw_execbuf_copy_fence_user(dev_priv, vmw_fpriv(file_priv),
2501 ret, user_fence_rep, fence,
2506 vmw_fence_obj_unreference(&fence);
2508 vmw_kms_helper_buffer_revert(buf);
2513 * vmw_kms_helper_resource_revert - Undo the actions of
2514 * vmw_kms_helper_resource_prepare.
2516 * @res: Pointer to the resource. Typically a surface.
2518 * Helper to be used if an error forces the caller to undo the actions of
2519 * vmw_kms_helper_resource_prepare.
2521 void vmw_kms_helper_resource_revert(struct vmw_resource *res)
2523 vmw_kms_helper_buffer_revert(res->backup);
2524 vmw_resource_unreserve(res, false, NULL, 0);
2525 mutex_unlock(&res->dev_priv->cmdbuf_mutex);
2529 * vmw_kms_helper_resource_prepare - Reserve and validate a resource before
2530 * command submission.
2532 * @res: Pointer to the resource. Typically a surface.
2533 * @interruptible: Whether to perform waits as interruptible.
2535 * Reserves and validates also the backup buffer if a guest-backed resource.
2536 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
2537 * interrupted by a signal.
2539 int vmw_kms_helper_resource_prepare(struct vmw_resource *res,
2545 ret = mutex_lock_interruptible(&res->dev_priv->cmdbuf_mutex);
2547 mutex_lock(&res->dev_priv->cmdbuf_mutex);
2549 if (unlikely(ret != 0))
2550 return -ERESTARTSYS;
2552 ret = vmw_resource_reserve(res, interruptible, false);
2557 ret = vmw_kms_helper_buffer_prepare(res->dev_priv, res->backup,
2559 res->dev_priv->has_mob);
2563 ret = vmw_resource_validate(res);
2569 vmw_kms_helper_buffer_revert(res->backup);
2571 vmw_resource_unreserve(res, false, NULL, 0);
2573 mutex_unlock(&res->dev_priv->cmdbuf_mutex);
2578 * vmw_kms_helper_resource_finish - Unreserve and fence a resource after
2579 * kms command submission.
2581 * @res: Pointer to the resource. Typically a surface.
2582 * @out_fence: Optional pointer to a fence pointer. If non-NULL, a
2583 * ref-counted fence pointer is returned here.
2585 void vmw_kms_helper_resource_finish(struct vmw_resource *res,
2586 struct vmw_fence_obj **out_fence)
2588 if (res->backup || out_fence)
2589 vmw_kms_helper_buffer_finish(res->dev_priv, NULL, res->backup,
2592 vmw_resource_unreserve(res, false, NULL, 0);
2593 mutex_unlock(&res->dev_priv->cmdbuf_mutex);
2597 * vmw_kms_update_proxy - Helper function to update a proxy surface from
2600 * @res: Pointer to the surface resource
2601 * @clips: Clip rects in framebuffer (surface) space.
2602 * @num_clips: Number of clips in @clips.
2603 * @increment: Integer with which to increment the clip counter when looping.
2604 * Used to skip a predetermined number of clip rects.
2606 * This function makes sure the proxy surface is updated from its backing MOB
2607 * using the region given by @clips. The surface resource @res and its backing
2608 * MOB needs to be reserved and validated on call.
2610 int vmw_kms_update_proxy(struct vmw_resource *res,
2611 const struct drm_clip_rect *clips,
2615 struct vmw_private *dev_priv = res->dev_priv;
2616 struct drm_vmw_size *size = &vmw_res_to_srf(res)->base_size;
2618 SVGA3dCmdHeader header;
2619 SVGA3dCmdUpdateGBImage body;
2622 size_t copy_size = 0;
2628 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd) * num_clips);
2630 DRM_ERROR("Couldn't reserve fifo space for proxy surface "
2635 for (i = 0; i < num_clips; ++i, clips += increment, ++cmd) {
2636 box = &cmd->body.box;
2638 cmd->header.id = SVGA_3D_CMD_UPDATE_GB_IMAGE;
2639 cmd->header.size = sizeof(cmd->body);
2640 cmd->body.image.sid = res->id;
2641 cmd->body.image.face = 0;
2642 cmd->body.image.mipmap = 0;
2644 if (clips->x1 > size->width || clips->x2 > size->width ||
2645 clips->y1 > size->height || clips->y2 > size->height) {
2646 DRM_ERROR("Invalid clips outsize of framebuffer.\n");
2653 box->w = clips->x2 - clips->x1;
2654 box->h = clips->y2 - clips->y1;
2657 copy_size += sizeof(*cmd);
2660 vmw_fifo_commit(dev_priv, copy_size);
2665 int vmw_kms_fbdev_init_data(struct vmw_private *dev_priv,
2669 struct drm_connector **p_con,
2670 struct drm_crtc **p_crtc,
2671 struct drm_display_mode **p_mode)
2673 struct drm_connector *con;
2674 struct vmw_display_unit *du;
2675 struct drm_display_mode *mode;
2678 list_for_each_entry(con, &dev_priv->dev->mode_config.connector_list,
2687 DRM_ERROR("Could not find initial display unit.\n");
2691 if (list_empty(&con->modes))
2692 (void) vmw_du_connector_fill_modes(con, max_width, max_height);
2694 if (list_empty(&con->modes)) {
2695 DRM_ERROR("Could not find initial display mode.\n");
2699 du = vmw_connector_to_du(con);
2701 *p_crtc = &du->crtc;
2703 list_for_each_entry(mode, &con->modes, head) {
2704 if (mode->type & DRM_MODE_TYPE_PREFERRED)
2708 if (mode->type & DRM_MODE_TYPE_PREFERRED)
2711 WARN_ONCE(true, "Could not find initial preferred mode.\n");
2712 *p_mode = list_first_entry(&con->modes,
2713 struct drm_display_mode,
2721 * vmw_kms_del_active - unregister a crtc binding to the implicit framebuffer
2723 * @dev_priv: Pointer to a device private struct.
2724 * @du: The display unit of the crtc.
2726 void vmw_kms_del_active(struct vmw_private *dev_priv,
2727 struct vmw_display_unit *du)
2729 mutex_lock(&dev_priv->global_kms_state_mutex);
2730 if (du->active_implicit) {
2731 if (--(dev_priv->num_implicit) == 0)
2732 dev_priv->implicit_fb = NULL;
2733 du->active_implicit = false;
2735 mutex_unlock(&dev_priv->global_kms_state_mutex);
2739 * vmw_kms_add_active - register a crtc binding to an implicit framebuffer
2741 * @vmw_priv: Pointer to a device private struct.
2742 * @du: The display unit of the crtc.
2743 * @vfb: The implicit framebuffer
2745 * Registers a binding to an implicit framebuffer.
2747 void vmw_kms_add_active(struct vmw_private *dev_priv,
2748 struct vmw_display_unit *du,
2749 struct vmw_framebuffer *vfb)
2751 mutex_lock(&dev_priv->global_kms_state_mutex);
2752 WARN_ON_ONCE(!dev_priv->num_implicit && dev_priv->implicit_fb);
2754 if (!du->active_implicit && du->is_implicit) {
2755 dev_priv->implicit_fb = vfb;
2756 du->active_implicit = true;
2757 dev_priv->num_implicit++;
2759 mutex_unlock(&dev_priv->global_kms_state_mutex);
2763 * vmw_kms_screen_object_flippable - Check whether we can page-flip a crtc.
2765 * @dev_priv: Pointer to device-private struct.
2766 * @crtc: The crtc we want to flip.
2768 * Returns true or false depending whether it's OK to flip this crtc
2769 * based on the criterion that we must not have more than one implicit
2770 * frame-buffer at any one time.
2772 bool vmw_kms_crtc_flippable(struct vmw_private *dev_priv,
2773 struct drm_crtc *crtc)
2775 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
2778 mutex_lock(&dev_priv->global_kms_state_mutex);
2779 ret = !du->is_implicit || dev_priv->num_implicit == 1;
2780 mutex_unlock(&dev_priv->global_kms_state_mutex);
2786 * vmw_kms_update_implicit_fb - Update the implicit fb.
2788 * @dev_priv: Pointer to device-private struct.
2789 * @crtc: The crtc the new implicit frame-buffer is bound to.
2791 void vmw_kms_update_implicit_fb(struct vmw_private *dev_priv,
2792 struct drm_crtc *crtc)
2794 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
2795 struct vmw_framebuffer *vfb;
2797 mutex_lock(&dev_priv->global_kms_state_mutex);
2799 if (!du->is_implicit)
2802 vfb = vmw_framebuffer_to_vfb(crtc->primary->fb);
2803 WARN_ON_ONCE(dev_priv->num_implicit != 1 &&
2804 dev_priv->implicit_fb != vfb);
2806 dev_priv->implicit_fb = vfb;
2808 mutex_unlock(&dev_priv->global_kms_state_mutex);
2812 * vmw_kms_create_implicit_placement_proparty - Set up the implicit placement
2815 * @dev_priv: Pointer to a device private struct.
2816 * @immutable: Whether the property is immutable.
2818 * Sets up the implicit placement property unless it's already set up.
2821 vmw_kms_create_implicit_placement_property(struct vmw_private *dev_priv,
2824 if (dev_priv->implicit_placement_property)
2827 dev_priv->implicit_placement_property =
2828 drm_property_create_range(dev_priv->dev,
2830 DRM_MODE_PROP_IMMUTABLE : 0,
2831 "implicit_placement", 0, 1);
2837 * vmw_kms_set_config - Wrapper around drm_atomic_helper_set_config
2839 * @set: The configuration to set.
2841 * The vmwgfx Xorg driver doesn't assign the mode::type member, which
2842 * when drm_mode_set_crtcinfo is called as part of the configuration setting
2843 * causes it to return incorrect crtc dimensions causing severe problems in
2844 * the vmwgfx modesetting. So explicitly clear that member before calling
2845 * into drm_atomic_helper_set_config.
2847 int vmw_kms_set_config(struct drm_mode_set *set,
2848 struct drm_modeset_acquire_ctx *ctx)
2850 if (set && set->mode)
2851 set->mode->type = 0;
2853 return drm_atomic_helper_set_config(set, ctx);