1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /**************************************************************************
4 * 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 <drm/drm_atomic.h>
29 #include <drm/drm_atomic_helper.h>
30 #include <drm/drm_damage_helper.h>
31 #include <drm/drm_fourcc.h>
32 #include <drm/drm_plane_helper.h>
33 #include <drm/drm_rect.h>
34 #include <drm/drm_sysfs.h>
35 #include <drm/drm_vblank.h>
37 #include "vmwgfx_kms.h"
39 void vmw_du_cleanup(struct vmw_display_unit *du)
41 drm_plane_cleanup(&du->primary);
42 drm_plane_cleanup(&du->cursor);
44 drm_connector_unregister(&du->connector);
45 drm_crtc_cleanup(&du->crtc);
46 drm_encoder_cleanup(&du->encoder);
47 drm_connector_cleanup(&du->connector);
51 * Display Unit Cursor functions
54 static int vmw_cursor_update_image(struct vmw_private *dev_priv,
55 u32 *image, u32 width, u32 height,
56 u32 hotspotX, u32 hotspotY)
60 SVGAFifoCmdDefineAlphaCursor cursor;
62 u32 image_size = width * height * 4;
63 u32 cmd_size = sizeof(*cmd) + image_size;
68 cmd = VMW_CMD_RESERVE(dev_priv, cmd_size);
69 if (unlikely(cmd == NULL))
72 memset(cmd, 0, sizeof(*cmd));
74 memcpy(&cmd[1], image, image_size);
76 cmd->cmd = SVGA_CMD_DEFINE_ALPHA_CURSOR;
78 cmd->cursor.width = width;
79 cmd->cursor.height = height;
80 cmd->cursor.hotspotX = hotspotX;
81 cmd->cursor.hotspotY = hotspotY;
83 vmw_cmd_commit_flush(dev_priv, cmd_size);
88 static int vmw_cursor_update_bo(struct vmw_private *dev_priv,
89 struct vmw_buffer_object *bo,
90 u32 width, u32 height,
91 u32 hotspotX, u32 hotspotY)
93 struct ttm_bo_kmap_obj map;
94 unsigned long kmap_offset;
95 unsigned long kmap_num;
101 kmap_num = (width*height*4 + PAGE_SIZE - 1) >> PAGE_SHIFT;
103 ret = ttm_bo_reserve(&bo->base, true, false, NULL);
104 if (unlikely(ret != 0)) {
105 DRM_ERROR("reserve failed\n");
109 ret = ttm_bo_kmap(&bo->base, kmap_offset, kmap_num, &map);
110 if (unlikely(ret != 0))
113 virtual = ttm_kmap_obj_virtual(&map, &dummy);
114 ret = vmw_cursor_update_image(dev_priv, virtual, width, height,
119 ttm_bo_unreserve(&bo->base);
125 static void vmw_cursor_update_position(struct vmw_private *dev_priv,
126 bool show, int x, int y)
130 spin_lock(&dev_priv->cursor_lock);
131 vmw_fifo_mem_write(dev_priv, SVGA_FIFO_CURSOR_ON, show ? 1 : 0);
132 vmw_fifo_mem_write(dev_priv, SVGA_FIFO_CURSOR_X, x);
133 vmw_fifo_mem_write(dev_priv, SVGA_FIFO_CURSOR_Y, y);
134 count = vmw_fifo_mem_read(dev_priv, SVGA_FIFO_CURSOR_COUNT);
135 vmw_fifo_mem_write(dev_priv, SVGA_FIFO_CURSOR_COUNT, ++count);
136 spin_unlock(&dev_priv->cursor_lock);
140 void vmw_kms_cursor_snoop(struct vmw_surface *srf,
141 struct ttm_object_file *tfile,
142 struct ttm_buffer_object *bo,
143 SVGA3dCmdHeader *header)
145 struct ttm_bo_kmap_obj map;
146 unsigned long kmap_offset;
147 unsigned long kmap_num;
153 SVGA3dCmdHeader header;
154 SVGA3dCmdSurfaceDMA dma;
158 cmd = container_of(header, struct vmw_dma_cmd, header);
160 /* No snooper installed */
161 if (!srf->snooper.image)
164 if (cmd->dma.host.face != 0 || cmd->dma.host.mipmap != 0) {
165 DRM_ERROR("face and mipmap for cursors should never != 0\n");
169 if (cmd->header.size < 64) {
170 DRM_ERROR("at least one full copy box must be given\n");
174 box = (SVGA3dCopyBox *)&cmd[1];
175 box_count = (cmd->header.size - sizeof(SVGA3dCmdSurfaceDMA)) /
176 sizeof(SVGA3dCopyBox);
178 if (cmd->dma.guest.ptr.offset % PAGE_SIZE ||
179 box->x != 0 || box->y != 0 || box->z != 0 ||
180 box->srcx != 0 || box->srcy != 0 || box->srcz != 0 ||
181 box->d != 1 || box_count != 1) {
182 /* TODO handle none page aligned offsets */
183 /* TODO handle more dst & src != 0 */
184 /* TODO handle more then one copy */
185 DRM_ERROR("Can't snoop dma request for cursor!\n");
186 DRM_ERROR("(%u, %u, %u) (%u, %u, %u) (%ux%ux%u) %u %u\n",
187 box->srcx, box->srcy, box->srcz,
188 box->x, box->y, box->z,
189 box->w, box->h, box->d, box_count,
190 cmd->dma.guest.ptr.offset);
194 kmap_offset = cmd->dma.guest.ptr.offset >> PAGE_SHIFT;
195 kmap_num = (64*64*4) >> PAGE_SHIFT;
197 ret = ttm_bo_reserve(bo, true, false, NULL);
198 if (unlikely(ret != 0)) {
199 DRM_ERROR("reserve failed\n");
203 ret = ttm_bo_kmap(bo, kmap_offset, kmap_num, &map);
204 if (unlikely(ret != 0))
207 virtual = ttm_kmap_obj_virtual(&map, &dummy);
209 if (box->w == 64 && cmd->dma.guest.pitch == 64*4) {
210 memcpy(srf->snooper.image, virtual, 64*64*4);
212 /* Image is unsigned pointer. */
213 for (i = 0; i < box->h; i++)
214 memcpy(srf->snooper.image + i * 64,
215 virtual + i * cmd->dma.guest.pitch,
223 ttm_bo_unreserve(bo);
227 * vmw_kms_legacy_hotspot_clear - Clear legacy hotspots
229 * @dev_priv: Pointer to the device private struct.
231 * Clears all legacy hotspots.
233 void vmw_kms_legacy_hotspot_clear(struct vmw_private *dev_priv)
235 struct drm_device *dev = &dev_priv->drm;
236 struct vmw_display_unit *du;
237 struct drm_crtc *crtc;
239 drm_modeset_lock_all(dev);
240 drm_for_each_crtc(crtc, dev) {
241 du = vmw_crtc_to_du(crtc);
246 drm_modeset_unlock_all(dev);
249 void vmw_kms_cursor_post_execbuf(struct vmw_private *dev_priv)
251 struct drm_device *dev = &dev_priv->drm;
252 struct vmw_display_unit *du;
253 struct drm_crtc *crtc;
255 mutex_lock(&dev->mode_config.mutex);
257 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
258 du = vmw_crtc_to_du(crtc);
259 if (!du->cursor_surface ||
260 du->cursor_age == du->cursor_surface->snooper.age)
263 du->cursor_age = du->cursor_surface->snooper.age;
264 vmw_cursor_update_image(dev_priv,
265 du->cursor_surface->snooper.image,
267 du->hotspot_x + du->core_hotspot_x,
268 du->hotspot_y + du->core_hotspot_y);
271 mutex_unlock(&dev->mode_config.mutex);
275 void vmw_du_cursor_plane_destroy(struct drm_plane *plane)
277 vmw_cursor_update_position(plane->dev->dev_private, false, 0, 0);
279 drm_plane_cleanup(plane);
283 void vmw_du_primary_plane_destroy(struct drm_plane *plane)
285 drm_plane_cleanup(plane);
287 /* Planes are static in our case so we don't free it */
292 * vmw_du_vps_unpin_surf - unpins resource associated with a framebuffer surface
294 * @vps: plane state associated with the display surface
295 * @unreference: true if we also want to unreference the display.
297 void vmw_du_plane_unpin_surf(struct vmw_plane_state *vps,
302 vmw_resource_unpin(&vps->surf->res);
308 DRM_ERROR("Surface still pinned\n");
309 vmw_surface_unreference(&vps->surf);
316 * vmw_du_plane_cleanup_fb - Unpins the cursor
318 * @plane: display plane
319 * @old_state: Contains the FB to clean up
321 * Unpins the framebuffer surface
323 * Returns 0 on success
326 vmw_du_plane_cleanup_fb(struct drm_plane *plane,
327 struct drm_plane_state *old_state)
329 struct vmw_plane_state *vps = vmw_plane_state_to_vps(old_state);
331 vmw_du_plane_unpin_surf(vps, false);
336 * vmw_du_cursor_plane_prepare_fb - Readies the cursor by referencing it
338 * @plane: display plane
339 * @new_state: info on the new plane state, including the FB
341 * Returns 0 on success
344 vmw_du_cursor_plane_prepare_fb(struct drm_plane *plane,
345 struct drm_plane_state *new_state)
347 struct drm_framebuffer *fb = new_state->fb;
348 struct vmw_plane_state *vps = vmw_plane_state_to_vps(new_state);
352 vmw_surface_unreference(&vps->surf);
355 vmw_bo_unreference(&vps->bo);
358 if (vmw_framebuffer_to_vfb(fb)->bo) {
359 vps->bo = vmw_framebuffer_to_vfbd(fb)->buffer;
360 vmw_bo_reference(vps->bo);
362 vps->surf = vmw_framebuffer_to_vfbs(fb)->surface;
363 vmw_surface_reference(vps->surf);
372 vmw_du_cursor_plane_atomic_update(struct drm_plane *plane,
373 struct drm_plane_state *old_state)
375 struct drm_crtc *crtc = plane->state->crtc ?: old_state->crtc;
376 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
377 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
378 struct vmw_plane_state *vps = vmw_plane_state_to_vps(plane->state);
379 s32 hotspot_x, hotspot_y;
383 hotspot_x = du->hotspot_x;
384 hotspot_y = du->hotspot_y;
386 if (plane->state->fb) {
387 hotspot_x += plane->state->fb->hot_x;
388 hotspot_y += plane->state->fb->hot_y;
391 du->cursor_surface = vps->surf;
392 du->cursor_bo = vps->bo;
395 du->cursor_age = du->cursor_surface->snooper.age;
397 ret = vmw_cursor_update_image(dev_priv,
398 vps->surf->snooper.image,
401 } else if (vps->bo) {
402 ret = vmw_cursor_update_bo(dev_priv, vps->bo,
403 plane->state->crtc_w,
404 plane->state->crtc_h,
405 hotspot_x, hotspot_y);
407 vmw_cursor_update_position(dev_priv, false, 0, 0);
412 du->cursor_x = plane->state->crtc_x + du->set_gui_x;
413 du->cursor_y = plane->state->crtc_y + du->set_gui_y;
415 vmw_cursor_update_position(dev_priv, true,
416 du->cursor_x + hotspot_x,
417 du->cursor_y + hotspot_y);
419 du->core_hotspot_x = hotspot_x - du->hotspot_x;
420 du->core_hotspot_y = hotspot_y - du->hotspot_y;
422 DRM_ERROR("Failed to update cursor image\n");
428 * vmw_du_primary_plane_atomic_check - check if the new state is okay
430 * @plane: display plane
431 * @state: info on the new plane state, including the FB
433 * Check if the new state is settable given the current state. Other
434 * than what the atomic helper checks, we care about crtc fitting
435 * the FB and maintaining one active framebuffer.
437 * Returns 0 on success
439 int vmw_du_primary_plane_atomic_check(struct drm_plane *plane,
440 struct drm_plane_state *state)
442 struct drm_crtc_state *crtc_state = NULL;
443 struct drm_framebuffer *new_fb = state->fb;
447 crtc_state = drm_atomic_get_new_crtc_state(state->state, state->crtc);
449 ret = drm_atomic_helper_check_plane_state(state, crtc_state,
450 DRM_PLANE_HELPER_NO_SCALING,
451 DRM_PLANE_HELPER_NO_SCALING,
454 if (!ret && new_fb) {
455 struct drm_crtc *crtc = state->crtc;
456 struct vmw_connector_state *vcs;
457 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
459 vcs = vmw_connector_state_to_vcs(du->connector.state);
468 * vmw_du_cursor_plane_atomic_check - check if the new state is okay
470 * @plane: cursor plane
471 * @state: info on the new plane state
473 * This is a chance to fail if the new cursor state does not fit
476 * Returns 0 on success
478 int vmw_du_cursor_plane_atomic_check(struct drm_plane *plane,
479 struct drm_plane_state *new_state)
482 struct drm_crtc_state *crtc_state = NULL;
483 struct vmw_surface *surface = NULL;
484 struct drm_framebuffer *fb = new_state->fb;
487 crtc_state = drm_atomic_get_new_crtc_state(new_state->state,
490 ret = drm_atomic_helper_check_plane_state(new_state, crtc_state,
491 DRM_PLANE_HELPER_NO_SCALING,
492 DRM_PLANE_HELPER_NO_SCALING,
501 /* A lot of the code assumes this */
502 if (new_state->crtc_w != 64 || new_state->crtc_h != 64) {
503 DRM_ERROR("Invalid cursor dimensions (%d, %d)\n",
504 new_state->crtc_w, new_state->crtc_h);
508 if (!vmw_framebuffer_to_vfb(fb)->bo)
509 surface = vmw_framebuffer_to_vfbs(fb)->surface;
511 if (surface && !surface->snooper.image) {
512 DRM_ERROR("surface not suitable for cursor\n");
520 int vmw_du_crtc_atomic_check(struct drm_crtc *crtc,
521 struct drm_atomic_state *state)
523 struct drm_crtc_state *new_state = drm_atomic_get_new_crtc_state(state,
525 struct vmw_display_unit *du = vmw_crtc_to_du(new_state->crtc);
526 int connector_mask = drm_connector_mask(&du->connector);
527 bool has_primary = new_state->plane_mask &
528 drm_plane_mask(crtc->primary);
530 /* We always want to have an active plane with an active CRTC */
531 if (has_primary != new_state->enable)
535 if (new_state->connector_mask != connector_mask &&
536 new_state->connector_mask != 0) {
537 DRM_ERROR("Invalid connectors configuration\n");
542 * Our virtual device does not have a dot clock, so use the logical
543 * clock value as the dot clock.
545 if (new_state->mode.crtc_clock == 0)
546 new_state->adjusted_mode.crtc_clock = new_state->mode.clock;
552 void vmw_du_crtc_atomic_begin(struct drm_crtc *crtc,
553 struct drm_atomic_state *state)
558 void vmw_du_crtc_atomic_flush(struct drm_crtc *crtc,
559 struct drm_atomic_state *state)
561 struct drm_pending_vblank_event *event = crtc->state->event;
564 crtc->state->event = NULL;
566 spin_lock_irq(&crtc->dev->event_lock);
567 drm_crtc_send_vblank_event(crtc, event);
568 spin_unlock_irq(&crtc->dev->event_lock);
574 * vmw_du_crtc_duplicate_state - duplicate crtc state
577 * Allocates and returns a copy of the crtc state (both common and
578 * vmw-specific) for the specified crtc.
580 * Returns: The newly allocated crtc state, or NULL on failure.
582 struct drm_crtc_state *
583 vmw_du_crtc_duplicate_state(struct drm_crtc *crtc)
585 struct drm_crtc_state *state;
586 struct vmw_crtc_state *vcs;
588 if (WARN_ON(!crtc->state))
591 vcs = kmemdup(crtc->state, sizeof(*vcs), GFP_KERNEL);
598 __drm_atomic_helper_crtc_duplicate_state(crtc, state);
605 * vmw_du_crtc_reset - creates a blank vmw crtc state
608 * Resets the atomic state for @crtc by freeing the state pointer (which
609 * might be NULL, e.g. at driver load time) and allocating a new empty state
612 void vmw_du_crtc_reset(struct drm_crtc *crtc)
614 struct vmw_crtc_state *vcs;
618 __drm_atomic_helper_crtc_destroy_state(crtc->state);
620 kfree(vmw_crtc_state_to_vcs(crtc->state));
623 vcs = kzalloc(sizeof(*vcs), GFP_KERNEL);
626 DRM_ERROR("Cannot allocate vmw_crtc_state\n");
630 __drm_atomic_helper_crtc_reset(crtc, &vcs->base);
635 * vmw_du_crtc_destroy_state - destroy crtc state
637 * @state: state object to destroy
639 * Destroys the crtc state (both common and vmw-specific) for the
643 vmw_du_crtc_destroy_state(struct drm_crtc *crtc,
644 struct drm_crtc_state *state)
646 drm_atomic_helper_crtc_destroy_state(crtc, state);
651 * vmw_du_plane_duplicate_state - duplicate plane state
654 * Allocates and returns a copy of the plane state (both common and
655 * vmw-specific) for the specified plane.
657 * Returns: The newly allocated plane state, or NULL on failure.
659 struct drm_plane_state *
660 vmw_du_plane_duplicate_state(struct drm_plane *plane)
662 struct drm_plane_state *state;
663 struct vmw_plane_state *vps;
665 vps = kmemdup(plane->state, sizeof(*vps), GFP_KERNEL);
673 /* Each ref counted resource needs to be acquired again */
675 (void) vmw_surface_reference(vps->surf);
678 (void) vmw_bo_reference(vps->bo);
682 __drm_atomic_helper_plane_duplicate_state(plane, state);
689 * vmw_du_plane_reset - creates a blank vmw plane state
692 * Resets the atomic state for @plane by freeing the state pointer (which might
693 * be NULL, e.g. at driver load time) and allocating a new empty state object.
695 void vmw_du_plane_reset(struct drm_plane *plane)
697 struct vmw_plane_state *vps;
701 vmw_du_plane_destroy_state(plane, plane->state);
703 vps = kzalloc(sizeof(*vps), GFP_KERNEL);
706 DRM_ERROR("Cannot allocate vmw_plane_state\n");
710 __drm_atomic_helper_plane_reset(plane, &vps->base);
715 * vmw_du_plane_destroy_state - destroy plane state
717 * @state: state object to destroy
719 * Destroys the plane state (both common and vmw-specific) for the
723 vmw_du_plane_destroy_state(struct drm_plane *plane,
724 struct drm_plane_state *state)
726 struct vmw_plane_state *vps = vmw_plane_state_to_vps(state);
729 /* Should have been freed by cleanup_fb */
731 vmw_surface_unreference(&vps->surf);
734 vmw_bo_unreference(&vps->bo);
736 drm_atomic_helper_plane_destroy_state(plane, state);
741 * vmw_du_connector_duplicate_state - duplicate connector state
742 * @connector: DRM connector
744 * Allocates and returns a copy of the connector state (both common and
745 * vmw-specific) for the specified connector.
747 * Returns: The newly allocated connector state, or NULL on failure.
749 struct drm_connector_state *
750 vmw_du_connector_duplicate_state(struct drm_connector *connector)
752 struct drm_connector_state *state;
753 struct vmw_connector_state *vcs;
755 if (WARN_ON(!connector->state))
758 vcs = kmemdup(connector->state, sizeof(*vcs), GFP_KERNEL);
765 __drm_atomic_helper_connector_duplicate_state(connector, state);
772 * vmw_du_connector_reset - creates a blank vmw connector state
773 * @connector: DRM connector
775 * Resets the atomic state for @connector by freeing the state pointer (which
776 * might be NULL, e.g. at driver load time) and allocating a new empty state
779 void vmw_du_connector_reset(struct drm_connector *connector)
781 struct vmw_connector_state *vcs;
784 if (connector->state) {
785 __drm_atomic_helper_connector_destroy_state(connector->state);
787 kfree(vmw_connector_state_to_vcs(connector->state));
790 vcs = kzalloc(sizeof(*vcs), GFP_KERNEL);
793 DRM_ERROR("Cannot allocate vmw_connector_state\n");
797 __drm_atomic_helper_connector_reset(connector, &vcs->base);
802 * vmw_du_connector_destroy_state - destroy connector state
803 * @connector: DRM connector
804 * @state: state object to destroy
806 * Destroys the connector state (both common and vmw-specific) for the
810 vmw_du_connector_destroy_state(struct drm_connector *connector,
811 struct drm_connector_state *state)
813 drm_atomic_helper_connector_destroy_state(connector, state);
816 * Generic framebuffer code
820 * Surface framebuffer code
823 static void vmw_framebuffer_surface_destroy(struct drm_framebuffer *framebuffer)
825 struct vmw_framebuffer_surface *vfbs =
826 vmw_framebuffer_to_vfbs(framebuffer);
828 drm_framebuffer_cleanup(framebuffer);
829 vmw_surface_unreference(&vfbs->surface);
830 if (vfbs->base.user_obj)
831 ttm_base_object_unref(&vfbs->base.user_obj);
837 * vmw_kms_readback - Perform a readback from the screen system to
838 * a buffer-object backed framebuffer.
840 * @dev_priv: Pointer to the device private structure.
841 * @file_priv: Pointer to a struct drm_file identifying the caller.
842 * Must be set to NULL if @user_fence_rep is NULL.
843 * @vfb: Pointer to the buffer-object backed framebuffer.
844 * @user_fence_rep: User-space provided structure for fence information.
845 * Must be set to non-NULL if @file_priv is non-NULL.
846 * @vclips: Array of clip rects.
847 * @num_clips: Number of clip rects in @vclips.
849 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
852 int vmw_kms_readback(struct vmw_private *dev_priv,
853 struct drm_file *file_priv,
854 struct vmw_framebuffer *vfb,
855 struct drm_vmw_fence_rep __user *user_fence_rep,
856 struct drm_vmw_rect *vclips,
859 switch (dev_priv->active_display_unit) {
860 case vmw_du_screen_object:
861 return vmw_kms_sou_readback(dev_priv, file_priv, vfb,
862 user_fence_rep, vclips, num_clips,
864 case vmw_du_screen_target:
865 return vmw_kms_stdu_dma(dev_priv, file_priv, vfb,
866 user_fence_rep, NULL, vclips, num_clips,
867 1, false, true, NULL);
870 "Readback called with invalid display system.\n");
877 static const struct drm_framebuffer_funcs vmw_framebuffer_surface_funcs = {
878 .destroy = vmw_framebuffer_surface_destroy,
879 .dirty = drm_atomic_helper_dirtyfb,
882 static int vmw_kms_new_framebuffer_surface(struct vmw_private *dev_priv,
883 struct vmw_surface *surface,
884 struct vmw_framebuffer **out,
885 const struct drm_mode_fb_cmd2
890 struct drm_device *dev = &dev_priv->drm;
891 struct vmw_framebuffer_surface *vfbs;
892 enum SVGA3dSurfaceFormat format;
894 struct drm_format_name_buf format_name;
896 /* 3D is only supported on HWv8 and newer hosts */
897 if (dev_priv->active_display_unit == vmw_du_legacy)
904 /* Surface must be marked as a scanout. */
905 if (unlikely(!surface->metadata.scanout))
908 if (unlikely(surface->metadata.mip_levels[0] != 1 ||
909 surface->metadata.num_sizes != 1 ||
910 surface->metadata.base_size.width < mode_cmd->width ||
911 surface->metadata.base_size.height < mode_cmd->height ||
912 surface->metadata.base_size.depth != 1)) {
913 DRM_ERROR("Incompatible surface dimensions "
914 "for requested mode.\n");
918 switch (mode_cmd->pixel_format) {
919 case DRM_FORMAT_ARGB8888:
920 format = SVGA3D_A8R8G8B8;
922 case DRM_FORMAT_XRGB8888:
923 format = SVGA3D_X8R8G8B8;
925 case DRM_FORMAT_RGB565:
926 format = SVGA3D_R5G6B5;
928 case DRM_FORMAT_XRGB1555:
929 format = SVGA3D_A1R5G5B5;
932 DRM_ERROR("Invalid pixel format: %s\n",
933 drm_get_format_name(mode_cmd->pixel_format, &format_name));
938 * For DX, surface format validation is done when surface->scanout
941 if (!has_sm4_context(dev_priv) && format != surface->metadata.format) {
942 DRM_ERROR("Invalid surface format for requested mode.\n");
946 vfbs = kzalloc(sizeof(*vfbs), GFP_KERNEL);
952 drm_helper_mode_fill_fb_struct(dev, &vfbs->base.base, mode_cmd);
953 vfbs->surface = vmw_surface_reference(surface);
954 vfbs->base.user_handle = mode_cmd->handles[0];
955 vfbs->is_bo_proxy = is_bo_proxy;
959 ret = drm_framebuffer_init(dev, &vfbs->base.base,
960 &vmw_framebuffer_surface_funcs);
967 vmw_surface_unreference(&surface);
974 * Buffer-object framebuffer code
977 static void vmw_framebuffer_bo_destroy(struct drm_framebuffer *framebuffer)
979 struct vmw_framebuffer_bo *vfbd =
980 vmw_framebuffer_to_vfbd(framebuffer);
982 drm_framebuffer_cleanup(framebuffer);
983 vmw_bo_unreference(&vfbd->buffer);
984 if (vfbd->base.user_obj)
985 ttm_base_object_unref(&vfbd->base.user_obj);
990 static int vmw_framebuffer_bo_dirty(struct drm_framebuffer *framebuffer,
991 struct drm_file *file_priv,
992 unsigned int flags, unsigned int color,
993 struct drm_clip_rect *clips,
994 unsigned int num_clips)
996 struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
997 struct vmw_framebuffer_bo *vfbd =
998 vmw_framebuffer_to_vfbd(framebuffer);
999 struct drm_clip_rect norect;
1000 int ret, increment = 1;
1002 drm_modeset_lock_all(&dev_priv->drm);
1004 ret = ttm_read_lock(&dev_priv->reservation_sem, true);
1005 if (unlikely(ret != 0)) {
1006 drm_modeset_unlock_all(&dev_priv->drm);
1013 norect.x1 = norect.y1 = 0;
1014 norect.x2 = framebuffer->width;
1015 norect.y2 = framebuffer->height;
1016 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
1021 switch (dev_priv->active_display_unit) {
1023 ret = vmw_kms_ldu_do_bo_dirty(dev_priv, &vfbd->base, 0, 0,
1024 clips, num_clips, increment);
1028 WARN_ONCE(true, "Dirty called with invalid display system.\n");
1032 vmw_cmd_flush(dev_priv, false);
1033 ttm_read_unlock(&dev_priv->reservation_sem);
1035 drm_modeset_unlock_all(&dev_priv->drm);
1040 static int vmw_framebuffer_bo_dirty_ext(struct drm_framebuffer *framebuffer,
1041 struct drm_file *file_priv,
1042 unsigned int flags, unsigned int color,
1043 struct drm_clip_rect *clips,
1044 unsigned int num_clips)
1046 struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
1048 if (dev_priv->active_display_unit == vmw_du_legacy)
1049 return vmw_framebuffer_bo_dirty(framebuffer, file_priv, flags,
1050 color, clips, num_clips);
1052 return drm_atomic_helper_dirtyfb(framebuffer, file_priv, flags, color,
1056 static const struct drm_framebuffer_funcs vmw_framebuffer_bo_funcs = {
1057 .destroy = vmw_framebuffer_bo_destroy,
1058 .dirty = vmw_framebuffer_bo_dirty_ext,
1062 * Pin the bofer in a location suitable for access by the
1065 static int vmw_framebuffer_pin(struct vmw_framebuffer *vfb)
1067 struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
1068 struct vmw_buffer_object *buf;
1069 struct ttm_placement *placement;
1072 buf = vfb->bo ? vmw_framebuffer_to_vfbd(&vfb->base)->buffer :
1073 vmw_framebuffer_to_vfbs(&vfb->base)->surface->res.backup;
1078 switch (dev_priv->active_display_unit) {
1080 vmw_overlay_pause_all(dev_priv);
1081 ret = vmw_bo_pin_in_start_of_vram(dev_priv, buf, false);
1082 vmw_overlay_resume_all(dev_priv);
1084 case vmw_du_screen_object:
1085 case vmw_du_screen_target:
1087 if (dev_priv->capabilities & SVGA_CAP_3D) {
1089 * Use surface DMA to get content to
1090 * sreen target surface.
1092 placement = &vmw_vram_gmr_placement;
1095 placement = &vmw_sys_placement;
1098 /* Use surface / image update */
1099 placement = &vmw_mob_placement;
1102 return vmw_bo_pin_in_placement(dev_priv, buf, placement, false);
1110 static int vmw_framebuffer_unpin(struct vmw_framebuffer *vfb)
1112 struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
1113 struct vmw_buffer_object *buf;
1115 buf = vfb->bo ? vmw_framebuffer_to_vfbd(&vfb->base)->buffer :
1116 vmw_framebuffer_to_vfbs(&vfb->base)->surface->res.backup;
1121 return vmw_bo_unpin(dev_priv, buf, false);
1125 * vmw_create_bo_proxy - create a proxy surface for the buffer object
1128 * @mode_cmd: parameters for the new surface
1129 * @bo_mob: MOB backing the buffer object
1130 * @srf_out: newly created surface
1132 * When the content FB is a buffer object, we create a surface as a proxy to the
1133 * same buffer. This way we can do a surface copy rather than a surface DMA.
1134 * This is a more efficient approach
1137 * 0 on success, error code otherwise
1139 static int vmw_create_bo_proxy(struct drm_device *dev,
1140 const struct drm_mode_fb_cmd2 *mode_cmd,
1141 struct vmw_buffer_object *bo_mob,
1142 struct vmw_surface **srf_out)
1144 struct vmw_surface_metadata metadata = {0};
1146 struct vmw_resource *res;
1147 unsigned int bytes_pp;
1148 struct drm_format_name_buf format_name;
1151 switch (mode_cmd->pixel_format) {
1152 case DRM_FORMAT_ARGB8888:
1153 case DRM_FORMAT_XRGB8888:
1154 format = SVGA3D_X8R8G8B8;
1158 case DRM_FORMAT_RGB565:
1159 case DRM_FORMAT_XRGB1555:
1160 format = SVGA3D_R5G6B5;
1170 DRM_ERROR("Invalid framebuffer format %s\n",
1171 drm_get_format_name(mode_cmd->pixel_format, &format_name));
1175 metadata.format = format;
1176 metadata.mip_levels[0] = 1;
1177 metadata.num_sizes = 1;
1178 metadata.base_size.width = mode_cmd->pitches[0] / bytes_pp;
1179 metadata.base_size.height = mode_cmd->height;
1180 metadata.base_size.depth = 1;
1181 metadata.scanout = true;
1183 ret = vmw_gb_surface_define(vmw_priv(dev), 0, &metadata, srf_out);
1185 DRM_ERROR("Failed to allocate proxy content buffer\n");
1189 res = &(*srf_out)->res;
1191 /* Reserve and switch the backing mob. */
1192 mutex_lock(&res->dev_priv->cmdbuf_mutex);
1193 (void) vmw_resource_reserve(res, false, true);
1194 vmw_bo_unreference(&res->backup);
1195 res->backup = vmw_bo_reference(bo_mob);
1196 res->backup_offset = 0;
1197 vmw_resource_unreserve(res, false, false, false, NULL, 0);
1198 mutex_unlock(&res->dev_priv->cmdbuf_mutex);
1205 static int vmw_kms_new_framebuffer_bo(struct vmw_private *dev_priv,
1206 struct vmw_buffer_object *bo,
1207 struct vmw_framebuffer **out,
1208 const struct drm_mode_fb_cmd2
1212 struct drm_device *dev = &dev_priv->drm;
1213 struct vmw_framebuffer_bo *vfbd;
1214 unsigned int requested_size;
1215 struct drm_format_name_buf format_name;
1218 requested_size = mode_cmd->height * mode_cmd->pitches[0];
1219 if (unlikely(requested_size > bo->base.base.size)) {
1220 DRM_ERROR("Screen buffer object size is too small "
1221 "for requested mode.\n");
1225 /* Limited framebuffer color depth support for screen objects */
1226 if (dev_priv->active_display_unit == vmw_du_screen_object) {
1227 switch (mode_cmd->pixel_format) {
1228 case DRM_FORMAT_XRGB8888:
1229 case DRM_FORMAT_ARGB8888:
1231 case DRM_FORMAT_XRGB1555:
1232 case DRM_FORMAT_RGB565:
1235 DRM_ERROR("Invalid pixel format: %s\n",
1236 drm_get_format_name(mode_cmd->pixel_format, &format_name));
1241 vfbd = kzalloc(sizeof(*vfbd), GFP_KERNEL);
1247 drm_helper_mode_fill_fb_struct(dev, &vfbd->base.base, mode_cmd);
1248 vfbd->base.bo = true;
1249 vfbd->buffer = vmw_bo_reference(bo);
1250 vfbd->base.user_handle = mode_cmd->handles[0];
1253 ret = drm_framebuffer_init(dev, &vfbd->base.base,
1254 &vmw_framebuffer_bo_funcs);
1261 vmw_bo_unreference(&bo);
1269 * vmw_kms_srf_ok - check if a surface can be created
1271 * @width: requested width
1272 * @height: requested height
1274 * Surfaces need to be less than texture size
1277 vmw_kms_srf_ok(struct vmw_private *dev_priv, uint32_t width, uint32_t height)
1279 if (width > dev_priv->texture_max_width ||
1280 height > dev_priv->texture_max_height)
1287 * vmw_kms_new_framebuffer - Create a new framebuffer.
1289 * @dev_priv: Pointer to device private struct.
1290 * @bo: Pointer to buffer object to wrap the kms framebuffer around.
1291 * Either @bo or @surface must be NULL.
1292 * @surface: Pointer to a surface to wrap the kms framebuffer around.
1293 * Either @bo or @surface must be NULL.
1294 * @only_2d: No presents will occur to this buffer object based framebuffer.
1295 * This helps the code to do some important optimizations.
1296 * @mode_cmd: Frame-buffer metadata.
1298 struct vmw_framebuffer *
1299 vmw_kms_new_framebuffer(struct vmw_private *dev_priv,
1300 struct vmw_buffer_object *bo,
1301 struct vmw_surface *surface,
1303 const struct drm_mode_fb_cmd2 *mode_cmd)
1305 struct vmw_framebuffer *vfb = NULL;
1306 bool is_bo_proxy = false;
1310 * We cannot use the SurfaceDMA command in an non-accelerated VM,
1311 * therefore, wrap the buffer object in a surface so we can use the
1312 * SurfaceCopy command.
1314 if (vmw_kms_srf_ok(dev_priv, mode_cmd->width, mode_cmd->height) &&
1316 mode_cmd->width > 64 && /* Don't create a proxy for cursor */
1317 dev_priv->active_display_unit == vmw_du_screen_target) {
1318 ret = vmw_create_bo_proxy(&dev_priv->drm, mode_cmd,
1321 return ERR_PTR(ret);
1326 /* Create the new framebuffer depending one what we have */
1328 ret = vmw_kms_new_framebuffer_surface(dev_priv, surface, &vfb,
1333 * vmw_create_bo_proxy() adds a reference that is no longer
1337 vmw_surface_unreference(&surface);
1339 ret = vmw_kms_new_framebuffer_bo(dev_priv, bo, &vfb,
1346 return ERR_PTR(ret);
1348 vfb->pin = vmw_framebuffer_pin;
1349 vfb->unpin = vmw_framebuffer_unpin;
1355 * Generic Kernel modesetting functions
1358 static struct drm_framebuffer *vmw_kms_fb_create(struct drm_device *dev,
1359 struct drm_file *file_priv,
1360 const struct drm_mode_fb_cmd2 *mode_cmd)
1362 struct vmw_private *dev_priv = vmw_priv(dev);
1363 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
1364 struct vmw_framebuffer *vfb = NULL;
1365 struct vmw_surface *surface = NULL;
1366 struct vmw_buffer_object *bo = NULL;
1367 struct ttm_base_object *user_obj;
1371 * Take a reference on the user object of the resource
1372 * backing the kms fb. This ensures that user-space handle
1373 * lookups on that resource will always work as long as
1374 * it's registered with a kms framebuffer. This is important,
1375 * since vmw_execbuf_process identifies resources in the
1376 * command stream using user-space handles.
1379 user_obj = ttm_base_object_lookup(tfile, mode_cmd->handles[0]);
1380 if (unlikely(user_obj == NULL)) {
1381 DRM_ERROR("Could not locate requested kms frame buffer.\n");
1382 return ERR_PTR(-ENOENT);
1386 * End conditioned code.
1389 /* returns either a bo or surface */
1390 ret = vmw_user_lookup_handle(dev_priv, tfile,
1391 mode_cmd->handles[0],
1398 !vmw_kms_srf_ok(dev_priv, mode_cmd->width, mode_cmd->height)) {
1399 DRM_ERROR("Surface size cannot exceed %dx%d",
1400 dev_priv->texture_max_width,
1401 dev_priv->texture_max_height);
1406 vfb = vmw_kms_new_framebuffer(dev_priv, bo, surface,
1407 !(dev_priv->capabilities & SVGA_CAP_3D),
1415 /* vmw_user_lookup_handle takes one ref so does new_fb */
1417 vmw_bo_unreference(&bo);
1419 vmw_surface_unreference(&surface);
1422 DRM_ERROR("failed to create vmw_framebuffer: %i\n", ret);
1423 ttm_base_object_unref(&user_obj);
1424 return ERR_PTR(ret);
1426 vfb->user_obj = user_obj;
1432 * vmw_kms_check_display_memory - Validates display memory required for a
1435 * @num_rects: number of drm_rect in rects
1436 * @rects: array of drm_rect representing the topology to validate indexed by
1440 * 0 on success otherwise negative error code
1442 static int vmw_kms_check_display_memory(struct drm_device *dev,
1444 struct drm_rect *rects)
1446 struct vmw_private *dev_priv = vmw_priv(dev);
1447 struct drm_rect bounding_box = {0};
1448 u64 total_pixels = 0, pixel_mem, bb_mem;
1451 for (i = 0; i < num_rects; i++) {
1453 * For STDU only individual screen (screen target) is limited by
1454 * SCREENTARGET_MAX_WIDTH/HEIGHT registers.
1456 if (dev_priv->active_display_unit == vmw_du_screen_target &&
1457 (drm_rect_width(&rects[i]) > dev_priv->stdu_max_width ||
1458 drm_rect_height(&rects[i]) > dev_priv->stdu_max_height)) {
1459 VMW_DEBUG_KMS("Screen size not supported.\n");
1463 /* Bounding box upper left is at (0,0). */
1464 if (rects[i].x2 > bounding_box.x2)
1465 bounding_box.x2 = rects[i].x2;
1467 if (rects[i].y2 > bounding_box.y2)
1468 bounding_box.y2 = rects[i].y2;
1470 total_pixels += (u64) drm_rect_width(&rects[i]) *
1471 (u64) drm_rect_height(&rects[i]);
1474 /* Virtual svga device primary limits are always in 32-bpp. */
1475 pixel_mem = total_pixels * 4;
1478 * For HV10 and below prim_bb_mem is vram size. When
1479 * SVGA_REG_MAX_PRIMARY_BOUNDING_BOX_MEM is not present vram size is
1480 * limit on primary bounding box
1482 if (pixel_mem > dev_priv->prim_bb_mem) {
1483 VMW_DEBUG_KMS("Combined output size too large.\n");
1487 /* SVGA_CAP_NO_BB_RESTRICTION is available for STDU only. */
1488 if (dev_priv->active_display_unit != vmw_du_screen_target ||
1489 !(dev_priv->capabilities & SVGA_CAP_NO_BB_RESTRICTION)) {
1490 bb_mem = (u64) bounding_box.x2 * bounding_box.y2 * 4;
1492 if (bb_mem > dev_priv->prim_bb_mem) {
1493 VMW_DEBUG_KMS("Topology is beyond supported limits.\n");
1502 * vmw_crtc_state_and_lock - Return new or current crtc state with locked
1504 * @state: The atomic state pointer containing the new atomic state
1507 * This function returns the new crtc state if it's part of the state update.
1508 * Otherwise returns the current crtc state. It also makes sure that the
1509 * crtc mutex is locked.
1511 * Returns: A valid crtc state pointer or NULL. It may also return a
1512 * pointer error, in particular -EDEADLK if locking needs to be rerun.
1514 static struct drm_crtc_state *
1515 vmw_crtc_state_and_lock(struct drm_atomic_state *state, struct drm_crtc *crtc)
1517 struct drm_crtc_state *crtc_state;
1519 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
1521 lockdep_assert_held(&crtc->mutex.mutex.base);
1523 int ret = drm_modeset_lock(&crtc->mutex, state->acquire_ctx);
1525 if (ret != 0 && ret != -EALREADY)
1526 return ERR_PTR(ret);
1528 crtc_state = crtc->state;
1535 * vmw_kms_check_implicit - Verify that all implicit display units scan out
1536 * from the same fb after the new state is committed.
1537 * @dev: The drm_device.
1538 * @state: The new state to be checked.
1542 * -EINVAL on invalid state,
1543 * -EDEADLK if modeset locking needs to be rerun.
1545 static int vmw_kms_check_implicit(struct drm_device *dev,
1546 struct drm_atomic_state *state)
1548 struct drm_framebuffer *implicit_fb = NULL;
1549 struct drm_crtc *crtc;
1550 struct drm_crtc_state *crtc_state;
1551 struct drm_plane_state *plane_state;
1553 drm_for_each_crtc(crtc, dev) {
1554 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
1556 if (!du->is_implicit)
1559 crtc_state = vmw_crtc_state_and_lock(state, crtc);
1560 if (IS_ERR(crtc_state))
1561 return PTR_ERR(crtc_state);
1563 if (!crtc_state || !crtc_state->enable)
1567 * Can't move primary planes across crtcs, so this is OK.
1568 * It also means we don't need to take the plane mutex.
1570 plane_state = du->primary.state;
1571 if (plane_state->crtc != crtc)
1575 implicit_fb = plane_state->fb;
1576 else if (implicit_fb != plane_state->fb)
1584 * vmw_kms_check_topology - Validates topology in drm_atomic_state
1586 * @state: the driver state object
1589 * 0 on success otherwise negative error code
1591 static int vmw_kms_check_topology(struct drm_device *dev,
1592 struct drm_atomic_state *state)
1594 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
1595 struct drm_rect *rects;
1596 struct drm_crtc *crtc;
1600 rects = kcalloc(dev->mode_config.num_crtc, sizeof(struct drm_rect),
1605 drm_for_each_crtc(crtc, dev) {
1606 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
1607 struct drm_crtc_state *crtc_state;
1609 i = drm_crtc_index(crtc);
1611 crtc_state = vmw_crtc_state_and_lock(state, crtc);
1612 if (IS_ERR(crtc_state)) {
1613 ret = PTR_ERR(crtc_state);
1620 if (crtc_state->enable) {
1621 rects[i].x1 = du->gui_x;
1622 rects[i].y1 = du->gui_y;
1623 rects[i].x2 = du->gui_x + crtc_state->mode.hdisplay;
1624 rects[i].y2 = du->gui_y + crtc_state->mode.vdisplay;
1633 /* Determine change to topology due to new atomic state */
1634 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state,
1635 new_crtc_state, i) {
1636 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
1637 struct drm_connector *connector;
1638 struct drm_connector_state *conn_state;
1639 struct vmw_connector_state *vmw_conn_state;
1641 if (!du->pref_active && new_crtc_state->enable) {
1642 VMW_DEBUG_KMS("Enabling a disabled display unit\n");
1648 * For vmwgfx each crtc has only one connector attached and it
1649 * is not changed so don't really need to check the
1650 * crtc->connector_mask and iterate over it.
1652 connector = &du->connector;
1653 conn_state = drm_atomic_get_connector_state(state, connector);
1654 if (IS_ERR(conn_state)) {
1655 ret = PTR_ERR(conn_state);
1659 vmw_conn_state = vmw_connector_state_to_vcs(conn_state);
1660 vmw_conn_state->gui_x = du->gui_x;
1661 vmw_conn_state->gui_y = du->gui_y;
1664 ret = vmw_kms_check_display_memory(dev, dev->mode_config.num_crtc,
1673 * vmw_kms_atomic_check_modeset- validate state object for modeset changes
1676 * @state: the driver state object
1678 * This is a simple wrapper around drm_atomic_helper_check_modeset() for
1679 * us to assign a value to mode->crtc_clock so that
1680 * drm_calc_timestamping_constants() won't throw an error message
1683 * Zero for success or -errno
1686 vmw_kms_atomic_check_modeset(struct drm_device *dev,
1687 struct drm_atomic_state *state)
1689 struct drm_crtc *crtc;
1690 struct drm_crtc_state *crtc_state;
1691 bool need_modeset = false;
1694 ret = drm_atomic_helper_check(dev, state);
1698 ret = vmw_kms_check_implicit(dev, state);
1700 VMW_DEBUG_KMS("Invalid implicit state\n");
1704 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
1705 if (drm_atomic_crtc_needs_modeset(crtc_state))
1706 need_modeset = true;
1710 return vmw_kms_check_topology(dev, state);
1715 static const struct drm_mode_config_funcs vmw_kms_funcs = {
1716 .fb_create = vmw_kms_fb_create,
1717 .atomic_check = vmw_kms_atomic_check_modeset,
1718 .atomic_commit = drm_atomic_helper_commit,
1721 static int vmw_kms_generic_present(struct vmw_private *dev_priv,
1722 struct drm_file *file_priv,
1723 struct vmw_framebuffer *vfb,
1724 struct vmw_surface *surface,
1726 int32_t destX, int32_t destY,
1727 struct drm_vmw_rect *clips,
1730 return vmw_kms_sou_do_surface_dirty(dev_priv, vfb, NULL, clips,
1731 &surface->res, destX, destY,
1732 num_clips, 1, NULL, NULL);
1736 int vmw_kms_present(struct vmw_private *dev_priv,
1737 struct drm_file *file_priv,
1738 struct vmw_framebuffer *vfb,
1739 struct vmw_surface *surface,
1741 int32_t destX, int32_t destY,
1742 struct drm_vmw_rect *clips,
1747 switch (dev_priv->active_display_unit) {
1748 case vmw_du_screen_target:
1749 ret = vmw_kms_stdu_surface_dirty(dev_priv, vfb, NULL, clips,
1750 &surface->res, destX, destY,
1751 num_clips, 1, NULL, NULL);
1753 case vmw_du_screen_object:
1754 ret = vmw_kms_generic_present(dev_priv, file_priv, vfb, surface,
1755 sid, destX, destY, clips,
1760 "Present called with invalid display system.\n");
1767 vmw_cmd_flush(dev_priv, false);
1773 vmw_kms_create_hotplug_mode_update_property(struct vmw_private *dev_priv)
1775 if (dev_priv->hotplug_mode_update_property)
1778 dev_priv->hotplug_mode_update_property =
1779 drm_property_create_range(&dev_priv->drm,
1780 DRM_MODE_PROP_IMMUTABLE,
1781 "hotplug_mode_update", 0, 1);
1783 if (!dev_priv->hotplug_mode_update_property)
1788 int vmw_kms_init(struct vmw_private *dev_priv)
1790 struct drm_device *dev = &dev_priv->drm;
1793 drm_mode_config_init(dev);
1794 dev->mode_config.funcs = &vmw_kms_funcs;
1795 dev->mode_config.min_width = 1;
1796 dev->mode_config.min_height = 1;
1797 dev->mode_config.max_width = dev_priv->texture_max_width;
1798 dev->mode_config.max_height = dev_priv->texture_max_height;
1800 drm_mode_create_suggested_offset_properties(dev);
1801 vmw_kms_create_hotplug_mode_update_property(dev_priv);
1803 ret = vmw_kms_stdu_init_display(dev_priv);
1805 ret = vmw_kms_sou_init_display(dev_priv);
1806 if (ret) /* Fallback */
1807 ret = vmw_kms_ldu_init_display(dev_priv);
1813 int vmw_kms_close(struct vmw_private *dev_priv)
1818 * Docs says we should take the lock before calling this function
1819 * but since it destroys encoders and our destructor calls
1820 * drm_encoder_cleanup which takes the lock we deadlock.
1822 drm_mode_config_cleanup(&dev_priv->drm);
1823 if (dev_priv->active_display_unit == vmw_du_legacy)
1824 ret = vmw_kms_ldu_close_display(dev_priv);
1829 int vmw_kms_cursor_bypass_ioctl(struct drm_device *dev, void *data,
1830 struct drm_file *file_priv)
1832 struct drm_vmw_cursor_bypass_arg *arg = data;
1833 struct vmw_display_unit *du;
1834 struct drm_crtc *crtc;
1838 mutex_lock(&dev->mode_config.mutex);
1839 if (arg->flags & DRM_VMW_CURSOR_BYPASS_ALL) {
1841 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1842 du = vmw_crtc_to_du(crtc);
1843 du->hotspot_x = arg->xhot;
1844 du->hotspot_y = arg->yhot;
1847 mutex_unlock(&dev->mode_config.mutex);
1851 crtc = drm_crtc_find(dev, file_priv, arg->crtc_id);
1857 du = vmw_crtc_to_du(crtc);
1859 du->hotspot_x = arg->xhot;
1860 du->hotspot_y = arg->yhot;
1863 mutex_unlock(&dev->mode_config.mutex);
1868 int vmw_kms_write_svga(struct vmw_private *vmw_priv,
1869 unsigned width, unsigned height, unsigned pitch,
1870 unsigned bpp, unsigned depth)
1872 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1873 vmw_write(vmw_priv, SVGA_REG_PITCHLOCK, pitch);
1874 else if (vmw_fifo_have_pitchlock(vmw_priv))
1875 vmw_fifo_mem_write(vmw_priv, SVGA_FIFO_PITCHLOCK, pitch);
1876 vmw_write(vmw_priv, SVGA_REG_WIDTH, width);
1877 vmw_write(vmw_priv, SVGA_REG_HEIGHT, height);
1878 if ((vmw_priv->capabilities & SVGA_CAP_8BIT_EMULATION) != 0)
1879 vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, bpp);
1881 if (vmw_read(vmw_priv, SVGA_REG_DEPTH) != depth) {
1882 DRM_ERROR("Invalid depth %u for %u bpp, host expects %u\n",
1883 depth, bpp, vmw_read(vmw_priv, SVGA_REG_DEPTH));
1890 bool vmw_kms_validate_mode_vram(struct vmw_private *dev_priv,
1894 return ((u64) pitch * (u64) height) < (u64)
1895 ((dev_priv->active_display_unit == vmw_du_screen_target) ?
1896 dev_priv->prim_bb_mem : dev_priv->vram_size);
1901 * Function called by DRM code called with vbl_lock held.
1903 u32 vmw_get_vblank_counter(struct drm_crtc *crtc)
1909 * Function called by DRM code called with vbl_lock held.
1911 int vmw_enable_vblank(struct drm_crtc *crtc)
1917 * Function called by DRM code called with vbl_lock held.
1919 void vmw_disable_vblank(struct drm_crtc *crtc)
1924 * vmw_du_update_layout - Update the display unit with topology from resolution
1925 * plugin and generate DRM uevent
1926 * @dev_priv: device private
1927 * @num_rects: number of drm_rect in rects
1928 * @rects: toplogy to update
1930 static int vmw_du_update_layout(struct vmw_private *dev_priv,
1931 unsigned int num_rects, struct drm_rect *rects)
1933 struct drm_device *dev = &dev_priv->drm;
1934 struct vmw_display_unit *du;
1935 struct drm_connector *con;
1936 struct drm_connector_list_iter conn_iter;
1937 struct drm_modeset_acquire_ctx ctx;
1938 struct drm_crtc *crtc;
1941 /* Currently gui_x/y is protected with the crtc mutex */
1942 mutex_lock(&dev->mode_config.mutex);
1943 drm_modeset_acquire_init(&ctx, 0);
1945 drm_for_each_crtc(crtc, dev) {
1946 ret = drm_modeset_lock(&crtc->mutex, &ctx);
1948 if (ret == -EDEADLK) {
1949 drm_modeset_backoff(&ctx);
1956 drm_connector_list_iter_begin(dev, &conn_iter);
1957 drm_for_each_connector_iter(con, &conn_iter) {
1958 du = vmw_connector_to_du(con);
1959 if (num_rects > du->unit) {
1960 du->pref_width = drm_rect_width(&rects[du->unit]);
1961 du->pref_height = drm_rect_height(&rects[du->unit]);
1962 du->pref_active = true;
1963 du->gui_x = rects[du->unit].x1;
1964 du->gui_y = rects[du->unit].y1;
1966 du->pref_width = 800;
1967 du->pref_height = 600;
1968 du->pref_active = false;
1973 drm_connector_list_iter_end(&conn_iter);
1975 list_for_each_entry(con, &dev->mode_config.connector_list, head) {
1976 du = vmw_connector_to_du(con);
1977 if (num_rects > du->unit) {
1978 drm_object_property_set_value
1979 (&con->base, dev->mode_config.suggested_x_property,
1981 drm_object_property_set_value
1982 (&con->base, dev->mode_config.suggested_y_property,
1985 drm_object_property_set_value
1986 (&con->base, dev->mode_config.suggested_x_property,
1988 drm_object_property_set_value
1989 (&con->base, dev->mode_config.suggested_y_property,
1992 con->status = vmw_du_connector_detect(con, true);
1995 drm_sysfs_hotplug_event(dev);
1997 drm_modeset_drop_locks(&ctx);
1998 drm_modeset_acquire_fini(&ctx);
1999 mutex_unlock(&dev->mode_config.mutex);
2004 int vmw_du_crtc_gamma_set(struct drm_crtc *crtc,
2005 u16 *r, u16 *g, u16 *b,
2007 struct drm_modeset_acquire_ctx *ctx)
2009 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
2012 for (i = 0; i < size; i++) {
2013 DRM_DEBUG("%d r/g/b = 0x%04x / 0x%04x / 0x%04x\n", i,
2015 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 0, r[i] >> 8);
2016 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 1, g[i] >> 8);
2017 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 2, b[i] >> 8);
2023 int vmw_du_connector_dpms(struct drm_connector *connector, int mode)
2028 enum drm_connector_status
2029 vmw_du_connector_detect(struct drm_connector *connector, bool force)
2031 uint32_t num_displays;
2032 struct drm_device *dev = connector->dev;
2033 struct vmw_private *dev_priv = vmw_priv(dev);
2034 struct vmw_display_unit *du = vmw_connector_to_du(connector);
2036 num_displays = vmw_read(dev_priv, SVGA_REG_NUM_DISPLAYS);
2038 return ((vmw_connector_to_du(connector)->unit < num_displays &&
2040 connector_status_connected : connector_status_disconnected);
2043 static struct drm_display_mode vmw_kms_connector_builtin[] = {
2045 { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 25175, 640, 656,
2046 752, 800, 0, 480, 489, 492, 525, 0,
2047 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
2049 { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 40000, 800, 840,
2050 968, 1056, 0, 600, 601, 605, 628, 0,
2051 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
2053 { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 65000, 1024, 1048,
2054 1184, 1344, 0, 768, 771, 777, 806, 0,
2055 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
2057 { DRM_MODE("1152x864", DRM_MODE_TYPE_DRIVER, 108000, 1152, 1216,
2058 1344, 1600, 0, 864, 865, 868, 900, 0,
2059 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
2061 { DRM_MODE("1280x768", DRM_MODE_TYPE_DRIVER, 79500, 1280, 1344,
2062 1472, 1664, 0, 768, 771, 778, 798, 0,
2063 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2065 { DRM_MODE("1280x800", DRM_MODE_TYPE_DRIVER, 83500, 1280, 1352,
2066 1480, 1680, 0, 800, 803, 809, 831, 0,
2067 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
2069 { DRM_MODE("1280x960", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1376,
2070 1488, 1800, 0, 960, 961, 964, 1000, 0,
2071 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
2072 /* 1280x1024@60Hz */
2073 { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1328,
2074 1440, 1688, 0, 1024, 1025, 1028, 1066, 0,
2075 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
2077 { DRM_MODE("1360x768", DRM_MODE_TYPE_DRIVER, 85500, 1360, 1424,
2078 1536, 1792, 0, 768, 771, 777, 795, 0,
2079 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
2080 /* 1440x1050@60Hz */
2081 { DRM_MODE("1400x1050", DRM_MODE_TYPE_DRIVER, 121750, 1400, 1488,
2082 1632, 1864, 0, 1050, 1053, 1057, 1089, 0,
2083 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2085 { DRM_MODE("1440x900", DRM_MODE_TYPE_DRIVER, 106500, 1440, 1520,
2086 1672, 1904, 0, 900, 903, 909, 934, 0,
2087 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2088 /* 1600x1200@60Hz */
2089 { DRM_MODE("1600x1200", DRM_MODE_TYPE_DRIVER, 162000, 1600, 1664,
2090 1856, 2160, 0, 1200, 1201, 1204, 1250, 0,
2091 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
2092 /* 1680x1050@60Hz */
2093 { DRM_MODE("1680x1050", DRM_MODE_TYPE_DRIVER, 146250, 1680, 1784,
2094 1960, 2240, 0, 1050, 1053, 1059, 1089, 0,
2095 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2096 /* 1792x1344@60Hz */
2097 { DRM_MODE("1792x1344", DRM_MODE_TYPE_DRIVER, 204750, 1792, 1920,
2098 2120, 2448, 0, 1344, 1345, 1348, 1394, 0,
2099 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2100 /* 1853x1392@60Hz */
2101 { DRM_MODE("1856x1392", DRM_MODE_TYPE_DRIVER, 218250, 1856, 1952,
2102 2176, 2528, 0, 1392, 1393, 1396, 1439, 0,
2103 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2104 /* 1920x1200@60Hz */
2105 { DRM_MODE("1920x1200", DRM_MODE_TYPE_DRIVER, 193250, 1920, 2056,
2106 2256, 2592, 0, 1200, 1203, 1209, 1245, 0,
2107 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2108 /* 1920x1440@60Hz */
2109 { DRM_MODE("1920x1440", DRM_MODE_TYPE_DRIVER, 234000, 1920, 2048,
2110 2256, 2600, 0, 1440, 1441, 1444, 1500, 0,
2111 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2112 /* 2560x1600@60Hz */
2113 { DRM_MODE("2560x1600", DRM_MODE_TYPE_DRIVER, 348500, 2560, 2752,
2114 3032, 3504, 0, 1600, 1603, 1609, 1658, 0,
2115 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2117 { DRM_MODE("", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) },
2121 * vmw_guess_mode_timing - Provide fake timings for a
2122 * 60Hz vrefresh mode.
2124 * @mode - Pointer to a struct drm_display_mode with hdisplay and vdisplay
2125 * members filled in.
2127 void vmw_guess_mode_timing(struct drm_display_mode *mode)
2129 mode->hsync_start = mode->hdisplay + 50;
2130 mode->hsync_end = mode->hsync_start + 50;
2131 mode->htotal = mode->hsync_end + 50;
2133 mode->vsync_start = mode->vdisplay + 50;
2134 mode->vsync_end = mode->vsync_start + 50;
2135 mode->vtotal = mode->vsync_end + 50;
2137 mode->clock = (u32)mode->htotal * (u32)mode->vtotal / 100 * 6;
2141 int vmw_du_connector_fill_modes(struct drm_connector *connector,
2142 uint32_t max_width, uint32_t max_height)
2144 struct vmw_display_unit *du = vmw_connector_to_du(connector);
2145 struct drm_device *dev = connector->dev;
2146 struct vmw_private *dev_priv = vmw_priv(dev);
2147 struct drm_display_mode *mode = NULL;
2148 struct drm_display_mode *bmode;
2149 struct drm_display_mode prefmode = { DRM_MODE("preferred",
2150 DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED,
2151 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2152 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC)
2155 u32 assumed_bpp = 4;
2157 if (dev_priv->assume_16bpp)
2160 max_width = min(max_width, dev_priv->texture_max_width);
2161 max_height = min(max_height, dev_priv->texture_max_height);
2164 * For STDU extra limit for a mode on SVGA_REG_SCREENTARGET_MAX_WIDTH/
2167 if (dev_priv->active_display_unit == vmw_du_screen_target) {
2168 max_width = min(max_width, dev_priv->stdu_max_width);
2169 max_height = min(max_height, dev_priv->stdu_max_height);
2172 /* Add preferred mode */
2173 mode = drm_mode_duplicate(dev, &prefmode);
2176 mode->hdisplay = du->pref_width;
2177 mode->vdisplay = du->pref_height;
2178 vmw_guess_mode_timing(mode);
2180 if (vmw_kms_validate_mode_vram(dev_priv,
2181 mode->hdisplay * assumed_bpp,
2183 drm_mode_probed_add(connector, mode);
2185 drm_mode_destroy(dev, mode);
2189 if (du->pref_mode) {
2190 list_del_init(&du->pref_mode->head);
2191 drm_mode_destroy(dev, du->pref_mode);
2194 /* mode might be null here, this is intended */
2195 du->pref_mode = mode;
2197 for (i = 0; vmw_kms_connector_builtin[i].type != 0; i++) {
2198 bmode = &vmw_kms_connector_builtin[i];
2199 if (bmode->hdisplay > max_width ||
2200 bmode->vdisplay > max_height)
2203 if (!vmw_kms_validate_mode_vram(dev_priv,
2204 bmode->hdisplay * assumed_bpp,
2208 mode = drm_mode_duplicate(dev, bmode);
2212 drm_mode_probed_add(connector, mode);
2215 drm_connector_list_update(connector);
2216 /* Move the prefered mode first, help apps pick the right mode. */
2217 drm_mode_sort(&connector->modes);
2223 * vmw_kms_update_layout_ioctl - Handler for DRM_VMW_UPDATE_LAYOUT ioctl
2224 * @dev: drm device for the ioctl
2225 * @data: data pointer for the ioctl
2226 * @file_priv: drm file for the ioctl call
2228 * Update preferred topology of display unit as per ioctl request. The topology
2229 * is expressed as array of drm_vmw_rect.
2231 * [0 0 640 480] [640 0 800 600] [0 480 640 480]
2234 * The x and y offset (upper left) in drm_vmw_rect cannot be less than 0. Beside
2235 * device limit on topology, x + w and y + h (lower right) cannot be greater
2236 * than INT_MAX. So topology beyond these limits will return with error.
2239 * Zero on success, negative errno on failure.
2241 int vmw_kms_update_layout_ioctl(struct drm_device *dev, void *data,
2242 struct drm_file *file_priv)
2244 struct vmw_private *dev_priv = vmw_priv(dev);
2245 struct drm_mode_config *mode_config = &dev->mode_config;
2246 struct drm_vmw_update_layout_arg *arg =
2247 (struct drm_vmw_update_layout_arg *)data;
2248 void __user *user_rects;
2249 struct drm_vmw_rect *rects;
2250 struct drm_rect *drm_rects;
2251 unsigned rects_size;
2254 if (!arg->num_outputs) {
2255 struct drm_rect def_rect = {0, 0, 800, 600};
2256 VMW_DEBUG_KMS("Default layout x1 = %d y1 = %d x2 = %d y2 = %d\n",
2257 def_rect.x1, def_rect.y1,
2258 def_rect.x2, def_rect.y2);
2259 vmw_du_update_layout(dev_priv, 1, &def_rect);
2263 rects_size = arg->num_outputs * sizeof(struct drm_vmw_rect);
2264 rects = kcalloc(arg->num_outputs, sizeof(struct drm_vmw_rect),
2266 if (unlikely(!rects))
2269 user_rects = (void __user *)(unsigned long)arg->rects;
2270 ret = copy_from_user(rects, user_rects, rects_size);
2271 if (unlikely(ret != 0)) {
2272 DRM_ERROR("Failed to get rects.\n");
2277 drm_rects = (struct drm_rect *)rects;
2279 VMW_DEBUG_KMS("Layout count = %u\n", arg->num_outputs);
2280 for (i = 0; i < arg->num_outputs; i++) {
2281 struct drm_vmw_rect curr_rect;
2283 /* Verify user-space for overflow as kernel use drm_rect */
2284 if ((rects[i].x + rects[i].w > INT_MAX) ||
2285 (rects[i].y + rects[i].h > INT_MAX)) {
2290 curr_rect = rects[i];
2291 drm_rects[i].x1 = curr_rect.x;
2292 drm_rects[i].y1 = curr_rect.y;
2293 drm_rects[i].x2 = curr_rect.x + curr_rect.w;
2294 drm_rects[i].y2 = curr_rect.y + curr_rect.h;
2296 VMW_DEBUG_KMS(" x1 = %d y1 = %d x2 = %d y2 = %d\n",
2297 drm_rects[i].x1, drm_rects[i].y1,
2298 drm_rects[i].x2, drm_rects[i].y2);
2301 * Currently this check is limiting the topology within
2302 * mode_config->max (which actually is max texture size
2303 * supported by virtual device). This limit is here to address
2304 * window managers that create a big framebuffer for whole
2307 if (drm_rects[i].x1 < 0 || drm_rects[i].y1 < 0 ||
2308 drm_rects[i].x2 > mode_config->max_width ||
2309 drm_rects[i].y2 > mode_config->max_height) {
2310 VMW_DEBUG_KMS("Invalid layout %d %d %d %d\n",
2311 drm_rects[i].x1, drm_rects[i].y1,
2312 drm_rects[i].x2, drm_rects[i].y2);
2318 ret = vmw_kms_check_display_memory(dev, arg->num_outputs, drm_rects);
2321 vmw_du_update_layout(dev_priv, arg->num_outputs, drm_rects);
2329 * vmw_kms_helper_dirty - Helper to build commands and perform actions based
2330 * on a set of cliprects and a set of display units.
2332 * @dev_priv: Pointer to a device private structure.
2333 * @framebuffer: Pointer to the framebuffer on which to perform the actions.
2334 * @clips: A set of struct drm_clip_rect. Either this os @vclips must be NULL.
2335 * Cliprects are given in framebuffer coordinates.
2336 * @vclips: A set of struct drm_vmw_rect cliprects. Either this or @clips must
2337 * be NULL. Cliprects are given in source coordinates.
2338 * @dest_x: X coordinate offset for the crtc / destination clip rects.
2339 * @dest_y: Y coordinate offset for the crtc / destination clip rects.
2340 * @num_clips: Number of cliprects in the @clips or @vclips array.
2341 * @increment: Integer with which to increment the clip counter when looping.
2342 * Used to skip a predetermined number of clip rects.
2343 * @dirty: Closure structure. See the description of struct vmw_kms_dirty.
2345 int vmw_kms_helper_dirty(struct vmw_private *dev_priv,
2346 struct vmw_framebuffer *framebuffer,
2347 const struct drm_clip_rect *clips,
2348 const struct drm_vmw_rect *vclips,
2349 s32 dest_x, s32 dest_y,
2352 struct vmw_kms_dirty *dirty)
2354 struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
2355 struct drm_crtc *crtc;
2359 dirty->dev_priv = dev_priv;
2361 /* If crtc is passed, no need to iterate over other display units */
2363 units[num_units++] = vmw_crtc_to_du(dirty->crtc);
2365 list_for_each_entry(crtc, &dev_priv->drm.mode_config.crtc_list,
2367 struct drm_plane *plane = crtc->primary;
2369 if (plane->state->fb == &framebuffer->base)
2370 units[num_units++] = vmw_crtc_to_du(crtc);
2374 for (k = 0; k < num_units; k++) {
2375 struct vmw_display_unit *unit = units[k];
2376 s32 crtc_x = unit->crtc.x;
2377 s32 crtc_y = unit->crtc.y;
2378 s32 crtc_width = unit->crtc.mode.hdisplay;
2379 s32 crtc_height = unit->crtc.mode.vdisplay;
2380 const struct drm_clip_rect *clips_ptr = clips;
2381 const struct drm_vmw_rect *vclips_ptr = vclips;
2384 if (dirty->fifo_reserve_size > 0) {
2385 dirty->cmd = VMW_CMD_RESERVE(dev_priv,
2386 dirty->fifo_reserve_size);
2390 memset(dirty->cmd, 0, dirty->fifo_reserve_size);
2392 dirty->num_hits = 0;
2393 for (i = 0; i < num_clips; i++, clips_ptr += increment,
2394 vclips_ptr += increment) {
2399 * Select clip array type. Note that integer type
2400 * in @clips is unsigned short, whereas in @vclips
2404 dirty->fb_x = (s32) clips_ptr->x1;
2405 dirty->fb_y = (s32) clips_ptr->y1;
2406 dirty->unit_x2 = (s32) clips_ptr->x2 + dest_x -
2408 dirty->unit_y2 = (s32) clips_ptr->y2 + dest_y -
2411 dirty->fb_x = vclips_ptr->x;
2412 dirty->fb_y = vclips_ptr->y;
2413 dirty->unit_x2 = dirty->fb_x + vclips_ptr->w +
2415 dirty->unit_y2 = dirty->fb_y + vclips_ptr->h +
2419 dirty->unit_x1 = dirty->fb_x + dest_x - crtc_x;
2420 dirty->unit_y1 = dirty->fb_y + dest_y - crtc_y;
2422 /* Skip this clip if it's outside the crtc region */
2423 if (dirty->unit_x1 >= crtc_width ||
2424 dirty->unit_y1 >= crtc_height ||
2425 dirty->unit_x2 <= 0 || dirty->unit_y2 <= 0)
2428 /* Clip right and bottom to crtc limits */
2429 dirty->unit_x2 = min_t(s32, dirty->unit_x2,
2431 dirty->unit_y2 = min_t(s32, dirty->unit_y2,
2434 /* Clip left and top to crtc limits */
2435 clip_left = min_t(s32, dirty->unit_x1, 0);
2436 clip_top = min_t(s32, dirty->unit_y1, 0);
2437 dirty->unit_x1 -= clip_left;
2438 dirty->unit_y1 -= clip_top;
2439 dirty->fb_x -= clip_left;
2440 dirty->fb_y -= clip_top;
2445 dirty->fifo_commit(dirty);
2452 * vmw_kms_helper_validation_finish - Helper for post KMS command submission
2453 * cleanup and fencing
2454 * @dev_priv: Pointer to the device-private struct
2455 * @file_priv: Pointer identifying the client when user-space fencing is used
2456 * @ctx: Pointer to the validation context
2457 * @out_fence: If non-NULL, returned refcounted fence-pointer
2458 * @user_fence_rep: If non-NULL, pointer to user-space address area
2459 * in which to copy user-space fence info
2461 void vmw_kms_helper_validation_finish(struct vmw_private *dev_priv,
2462 struct drm_file *file_priv,
2463 struct vmw_validation_context *ctx,
2464 struct vmw_fence_obj **out_fence,
2465 struct drm_vmw_fence_rep __user *
2468 struct vmw_fence_obj *fence = NULL;
2469 uint32_t handle = 0;
2472 if (file_priv || user_fence_rep || vmw_validation_has_bos(ctx) ||
2474 ret = vmw_execbuf_fence_commands(file_priv, dev_priv, &fence,
2475 file_priv ? &handle : NULL);
2476 vmw_validation_done(ctx, fence);
2478 vmw_execbuf_copy_fence_user(dev_priv, vmw_fpriv(file_priv),
2479 ret, user_fence_rep, fence,
2484 vmw_fence_obj_unreference(&fence);
2488 * vmw_kms_update_proxy - Helper function to update a proxy surface from
2491 * @res: Pointer to the surface resource
2492 * @clips: Clip rects in framebuffer (surface) space.
2493 * @num_clips: Number of clips in @clips.
2494 * @increment: Integer with which to increment the clip counter when looping.
2495 * Used to skip a predetermined number of clip rects.
2497 * This function makes sure the proxy surface is updated from its backing MOB
2498 * using the region given by @clips. The surface resource @res and its backing
2499 * MOB needs to be reserved and validated on call.
2501 int vmw_kms_update_proxy(struct vmw_resource *res,
2502 const struct drm_clip_rect *clips,
2506 struct vmw_private *dev_priv = res->dev_priv;
2507 struct drm_vmw_size *size = &vmw_res_to_srf(res)->metadata.base_size;
2509 SVGA3dCmdHeader header;
2510 SVGA3dCmdUpdateGBImage body;
2513 size_t copy_size = 0;
2519 cmd = VMW_CMD_RESERVE(dev_priv, sizeof(*cmd) * num_clips);
2523 for (i = 0; i < num_clips; ++i, clips += increment, ++cmd) {
2524 box = &cmd->body.box;
2526 cmd->header.id = SVGA_3D_CMD_UPDATE_GB_IMAGE;
2527 cmd->header.size = sizeof(cmd->body);
2528 cmd->body.image.sid = res->id;
2529 cmd->body.image.face = 0;
2530 cmd->body.image.mipmap = 0;
2532 if (clips->x1 > size->width || clips->x2 > size->width ||
2533 clips->y1 > size->height || clips->y2 > size->height) {
2534 DRM_ERROR("Invalid clips outsize of framebuffer.\n");
2541 box->w = clips->x2 - clips->x1;
2542 box->h = clips->y2 - clips->y1;
2545 copy_size += sizeof(*cmd);
2548 vmw_cmd_commit(dev_priv, copy_size);
2553 int vmw_kms_fbdev_init_data(struct vmw_private *dev_priv,
2557 struct drm_connector **p_con,
2558 struct drm_crtc **p_crtc,
2559 struct drm_display_mode **p_mode)
2561 struct drm_connector *con;
2562 struct vmw_display_unit *du;
2563 struct drm_display_mode *mode;
2567 mutex_lock(&dev_priv->drm.mode_config.mutex);
2568 list_for_each_entry(con, &dev_priv->drm.mode_config.connector_list,
2576 if (&con->head == &dev_priv->drm.mode_config.connector_list) {
2577 DRM_ERROR("Could not find initial display unit.\n");
2582 if (list_empty(&con->modes))
2583 (void) vmw_du_connector_fill_modes(con, max_width, max_height);
2585 if (list_empty(&con->modes)) {
2586 DRM_ERROR("Could not find initial display mode.\n");
2591 du = vmw_connector_to_du(con);
2593 *p_crtc = &du->crtc;
2595 list_for_each_entry(mode, &con->modes, head) {
2596 if (mode->type & DRM_MODE_TYPE_PREFERRED)
2600 if (&mode->head == &con->modes) {
2601 WARN_ONCE(true, "Could not find initial preferred mode.\n");
2602 *p_mode = list_first_entry(&con->modes,
2603 struct drm_display_mode,
2610 mutex_unlock(&dev_priv->drm.mode_config.mutex);
2616 * vmw_kms_create_implicit_placement_proparty - Set up the implicit placement
2619 * @dev_priv: Pointer to a device private struct.
2621 * Sets up the implicit placement property unless it's already set up.
2624 vmw_kms_create_implicit_placement_property(struct vmw_private *dev_priv)
2626 if (dev_priv->implicit_placement_property)
2629 dev_priv->implicit_placement_property =
2630 drm_property_create_range(&dev_priv->drm,
2631 DRM_MODE_PROP_IMMUTABLE,
2632 "implicit_placement", 0, 1);
2636 * vmw_kms_suspend - Save modesetting state and turn modesetting off.
2638 * @dev: Pointer to the drm device
2639 * Return: 0 on success. Negative error code on failure.
2641 int vmw_kms_suspend(struct drm_device *dev)
2643 struct vmw_private *dev_priv = vmw_priv(dev);
2645 dev_priv->suspend_state = drm_atomic_helper_suspend(dev);
2646 if (IS_ERR(dev_priv->suspend_state)) {
2647 int ret = PTR_ERR(dev_priv->suspend_state);
2649 DRM_ERROR("Failed kms suspend: %d\n", ret);
2650 dev_priv->suspend_state = NULL;
2660 * vmw_kms_resume - Re-enable modesetting and restore state
2662 * @dev: Pointer to the drm device
2663 * Return: 0 on success. Negative error code on failure.
2665 * State is resumed from a previous vmw_kms_suspend(). It's illegal
2666 * to call this function without a previous vmw_kms_suspend().
2668 int vmw_kms_resume(struct drm_device *dev)
2670 struct vmw_private *dev_priv = vmw_priv(dev);
2673 if (WARN_ON(!dev_priv->suspend_state))
2676 ret = drm_atomic_helper_resume(dev, dev_priv->suspend_state);
2677 dev_priv->suspend_state = NULL;
2683 * vmw_kms_lost_device - Notify kms that modesetting capabilities will be lost
2685 * @dev: Pointer to the drm device
2687 void vmw_kms_lost_device(struct drm_device *dev)
2689 drm_atomic_helper_shutdown(dev);
2693 * vmw_du_helper_plane_update - Helper to do plane update on a display unit.
2694 * @update: The closure structure.
2696 * Call this helper after setting callbacks in &vmw_du_update_plane to do plane
2697 * update on display unit.
2699 * Return: 0 on success or a negative error code on failure.
2701 int vmw_du_helper_plane_update(struct vmw_du_update_plane *update)
2703 struct drm_plane_state *state = update->plane->state;
2704 struct drm_plane_state *old_state = update->old_state;
2705 struct drm_atomic_helper_damage_iter iter;
2706 struct drm_rect clip;
2708 DECLARE_VAL_CONTEXT(val_ctx, NULL, 0);
2709 uint32_t reserved_size = 0;
2710 uint32_t submit_size = 0;
2711 uint32_t curr_size = 0;
2712 uint32_t num_hits = 0;
2718 * Iterate in advance to check if really need plane update and find the
2719 * number of clips that actually are in plane src for fifo allocation.
2721 drm_atomic_helper_damage_iter_init(&iter, old_state, state);
2722 drm_atomic_for_each_plane_damage(&iter, &clip)
2728 if (update->vfb->bo) {
2729 struct vmw_framebuffer_bo *vfbbo =
2730 container_of(update->vfb, typeof(*vfbbo), base);
2732 ret = vmw_validation_add_bo(&val_ctx, vfbbo->buffer, false,
2735 struct vmw_framebuffer_surface *vfbs =
2736 container_of(update->vfb, typeof(*vfbs), base);
2738 ret = vmw_validation_add_resource(&val_ctx, &vfbs->surface->res,
2739 0, VMW_RES_DIRTY_NONE, NULL,
2746 ret = vmw_validation_prepare(&val_ctx, update->mutex, update->intr);
2750 reserved_size = update->calc_fifo_size(update, num_hits);
2751 cmd_start = VMW_CMD_RESERVE(update->dev_priv, reserved_size);
2757 cmd_next = cmd_start;
2759 if (update->post_prepare) {
2760 curr_size = update->post_prepare(update, cmd_next);
2761 cmd_next += curr_size;
2762 submit_size += curr_size;
2765 if (update->pre_clip) {
2766 curr_size = update->pre_clip(update, cmd_next, num_hits);
2767 cmd_next += curr_size;
2768 submit_size += curr_size;
2776 drm_atomic_helper_damage_iter_init(&iter, old_state, state);
2777 drm_atomic_for_each_plane_damage(&iter, &clip) {
2778 uint32_t fb_x = clip.x1;
2779 uint32_t fb_y = clip.y1;
2781 vmw_du_translate_to_crtc(state, &clip);
2783 curr_size = update->clip(update, cmd_next, &clip, fb_x,
2785 cmd_next += curr_size;
2786 submit_size += curr_size;
2788 bb.x1 = min_t(int, bb.x1, clip.x1);
2789 bb.y1 = min_t(int, bb.y1, clip.y1);
2790 bb.x2 = max_t(int, bb.x2, clip.x2);
2791 bb.y2 = max_t(int, bb.y2, clip.y2);
2794 curr_size = update->post_clip(update, cmd_next, &bb);
2795 submit_size += curr_size;
2797 if (reserved_size < submit_size)
2800 vmw_cmd_commit(update->dev_priv, submit_size);
2802 vmw_kms_helper_validation_finish(update->dev_priv, NULL, &val_ctx,
2803 update->out_fence, NULL);
2807 vmw_validation_revert(&val_ctx);
2810 vmw_validation_unref_lists(&val_ctx);