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 "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>
33 #include <drm/drm_damage_helper.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))
71 memset(cmd, 0, sizeof(*cmd));
73 memcpy(&cmd[1], image, image_size);
75 cmd->cmd = SVGA_CMD_DEFINE_ALPHA_CURSOR;
77 cmd->cursor.width = width;
78 cmd->cursor.height = height;
79 cmd->cursor.hotspotX = hotspotX;
80 cmd->cursor.hotspotY = hotspotY;
82 vmw_fifo_commit_flush(dev_priv, cmd_size);
87 static int vmw_cursor_update_bo(struct vmw_private *dev_priv,
88 struct vmw_buffer_object *bo,
89 u32 width, u32 height,
90 u32 hotspotX, u32 hotspotY)
92 struct ttm_bo_kmap_obj map;
93 unsigned long kmap_offset;
94 unsigned long kmap_num;
100 kmap_num = (width*height*4 + PAGE_SIZE - 1) >> PAGE_SHIFT;
102 ret = ttm_bo_reserve(&bo->base, true, false, NULL);
103 if (unlikely(ret != 0)) {
104 DRM_ERROR("reserve failed\n");
108 ret = ttm_bo_kmap(&bo->base, kmap_offset, kmap_num, &map);
109 if (unlikely(ret != 0))
112 virtual = ttm_kmap_obj_virtual(&map, &dummy);
113 ret = vmw_cursor_update_image(dev_priv, virtual, width, height,
118 ttm_bo_unreserve(&bo->base);
124 static void vmw_cursor_update_position(struct vmw_private *dev_priv,
125 bool show, int x, int y)
127 u32 *fifo_mem = dev_priv->mmio_virt;
130 spin_lock(&dev_priv->cursor_lock);
131 vmw_mmio_write(show ? 1 : 0, fifo_mem + SVGA_FIFO_CURSOR_ON);
132 vmw_mmio_write(x, fifo_mem + SVGA_FIFO_CURSOR_X);
133 vmw_mmio_write(y, fifo_mem + SVGA_FIFO_CURSOR_Y);
134 count = vmw_mmio_read(fifo_mem + SVGA_FIFO_CURSOR_COUNT);
135 vmw_mmio_write(++count, fifo_mem + SVGA_FIFO_CURSOR_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("Cant 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->dev;
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->dev;
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_crtc_state *new_state)
523 struct vmw_display_unit *du = vmw_crtc_to_du(new_state->crtc);
524 int connector_mask = drm_connector_mask(&du->connector);
525 bool has_primary = new_state->plane_mask &
526 drm_plane_mask(crtc->primary);
528 /* We always want to have an active plane with an active CRTC */
529 if (has_primary != new_state->enable)
533 if (new_state->connector_mask != connector_mask &&
534 new_state->connector_mask != 0) {
535 DRM_ERROR("Invalid connectors configuration\n");
540 * Our virtual device does not have a dot clock, so use the logical
541 * clock value as the dot clock.
543 if (new_state->mode.crtc_clock == 0)
544 new_state->adjusted_mode.crtc_clock = new_state->mode.clock;
550 void vmw_du_crtc_atomic_begin(struct drm_crtc *crtc,
551 struct drm_crtc_state *old_crtc_state)
556 void vmw_du_crtc_atomic_flush(struct drm_crtc *crtc,
557 struct drm_crtc_state *old_crtc_state)
559 struct drm_pending_vblank_event *event = crtc->state->event;
562 crtc->state->event = NULL;
564 spin_lock_irq(&crtc->dev->event_lock);
565 drm_crtc_send_vblank_event(crtc, event);
566 spin_unlock_irq(&crtc->dev->event_lock);
572 * vmw_du_crtc_duplicate_state - duplicate crtc state
575 * Allocates and returns a copy of the crtc state (both common and
576 * vmw-specific) for the specified crtc.
578 * Returns: The newly allocated crtc state, or NULL on failure.
580 struct drm_crtc_state *
581 vmw_du_crtc_duplicate_state(struct drm_crtc *crtc)
583 struct drm_crtc_state *state;
584 struct vmw_crtc_state *vcs;
586 if (WARN_ON(!crtc->state))
589 vcs = kmemdup(crtc->state, sizeof(*vcs), GFP_KERNEL);
596 __drm_atomic_helper_crtc_duplicate_state(crtc, state);
603 * vmw_du_crtc_reset - creates a blank vmw crtc state
606 * Resets the atomic state for @crtc by freeing the state pointer (which
607 * might be NULL, e.g. at driver load time) and allocating a new empty state
610 void vmw_du_crtc_reset(struct drm_crtc *crtc)
612 struct vmw_crtc_state *vcs;
616 __drm_atomic_helper_crtc_destroy_state(crtc->state);
618 kfree(vmw_crtc_state_to_vcs(crtc->state));
621 vcs = kzalloc(sizeof(*vcs), GFP_KERNEL);
624 DRM_ERROR("Cannot allocate vmw_crtc_state\n");
628 crtc->state = &vcs->base;
629 crtc->state->crtc = crtc;
634 * vmw_du_crtc_destroy_state - destroy crtc state
636 * @state: state object to destroy
638 * Destroys the crtc state (both common and vmw-specific) for the
642 vmw_du_crtc_destroy_state(struct drm_crtc *crtc,
643 struct drm_crtc_state *state)
645 drm_atomic_helper_crtc_destroy_state(crtc, state);
650 * vmw_du_plane_duplicate_state - duplicate plane state
653 * Allocates and returns a copy of the plane state (both common and
654 * vmw-specific) for the specified plane.
656 * Returns: The newly allocated plane state, or NULL on failure.
658 struct drm_plane_state *
659 vmw_du_plane_duplicate_state(struct drm_plane *plane)
661 struct drm_plane_state *state;
662 struct vmw_plane_state *vps;
664 vps = kmemdup(plane->state, sizeof(*vps), GFP_KERNEL);
672 /* Each ref counted resource needs to be acquired again */
674 (void) vmw_surface_reference(vps->surf);
677 (void) vmw_bo_reference(vps->bo);
681 __drm_atomic_helper_plane_duplicate_state(plane, state);
688 * vmw_du_plane_reset - creates a blank vmw plane state
691 * Resets the atomic state for @plane by freeing the state pointer (which might
692 * be NULL, e.g. at driver load time) and allocating a new empty state object.
694 void vmw_du_plane_reset(struct drm_plane *plane)
696 struct vmw_plane_state *vps;
700 vmw_du_plane_destroy_state(plane, plane->state);
702 vps = kzalloc(sizeof(*vps), GFP_KERNEL);
705 DRM_ERROR("Cannot allocate vmw_plane_state\n");
709 __drm_atomic_helper_plane_reset(plane, &vps->base);
714 * vmw_du_plane_destroy_state - destroy plane state
716 * @state: state object to destroy
718 * Destroys the plane state (both common and vmw-specific) for the
722 vmw_du_plane_destroy_state(struct drm_plane *plane,
723 struct drm_plane_state *state)
725 struct vmw_plane_state *vps = vmw_plane_state_to_vps(state);
728 /* Should have been freed by cleanup_fb */
730 vmw_surface_unreference(&vps->surf);
733 vmw_bo_unreference(&vps->bo);
735 drm_atomic_helper_plane_destroy_state(plane, state);
740 * vmw_du_connector_duplicate_state - duplicate connector state
741 * @connector: DRM connector
743 * Allocates and returns a copy of the connector state (both common and
744 * vmw-specific) for the specified connector.
746 * Returns: The newly allocated connector state, or NULL on failure.
748 struct drm_connector_state *
749 vmw_du_connector_duplicate_state(struct drm_connector *connector)
751 struct drm_connector_state *state;
752 struct vmw_connector_state *vcs;
754 if (WARN_ON(!connector->state))
757 vcs = kmemdup(connector->state, sizeof(*vcs), GFP_KERNEL);
764 __drm_atomic_helper_connector_duplicate_state(connector, state);
771 * vmw_du_connector_reset - creates a blank vmw connector state
772 * @connector: DRM connector
774 * Resets the atomic state for @connector by freeing the state pointer (which
775 * might be NULL, e.g. at driver load time) and allocating a new empty state
778 void vmw_du_connector_reset(struct drm_connector *connector)
780 struct vmw_connector_state *vcs;
783 if (connector->state) {
784 __drm_atomic_helper_connector_destroy_state(connector->state);
786 kfree(vmw_connector_state_to_vcs(connector->state));
789 vcs = kzalloc(sizeof(*vcs), GFP_KERNEL);
792 DRM_ERROR("Cannot allocate vmw_connector_state\n");
796 __drm_atomic_helper_connector_reset(connector, &vcs->base);
801 * vmw_du_connector_destroy_state - destroy connector state
802 * @connector: DRM connector
803 * @state: state object to destroy
805 * Destroys the connector state (both common and vmw-specific) for the
809 vmw_du_connector_destroy_state(struct drm_connector *connector,
810 struct drm_connector_state *state)
812 drm_atomic_helper_connector_destroy_state(connector, state);
815 * Generic framebuffer code
819 * Surface framebuffer code
822 static void vmw_framebuffer_surface_destroy(struct drm_framebuffer *framebuffer)
824 struct vmw_framebuffer_surface *vfbs =
825 vmw_framebuffer_to_vfbs(framebuffer);
827 drm_framebuffer_cleanup(framebuffer);
828 vmw_surface_unreference(&vfbs->surface);
829 if (vfbs->base.user_obj)
830 ttm_base_object_unref(&vfbs->base.user_obj);
836 * vmw_kms_readback - Perform a readback from the screen system to
837 * a buffer-object backed framebuffer.
839 * @dev_priv: Pointer to the device private structure.
840 * @file_priv: Pointer to a struct drm_file identifying the caller.
841 * Must be set to NULL if @user_fence_rep is NULL.
842 * @vfb: Pointer to the buffer-object backed framebuffer.
843 * @user_fence_rep: User-space provided structure for fence information.
844 * Must be set to non-NULL if @file_priv is non-NULL.
845 * @vclips: Array of clip rects.
846 * @num_clips: Number of clip rects in @vclips.
848 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
851 int vmw_kms_readback(struct vmw_private *dev_priv,
852 struct drm_file *file_priv,
853 struct vmw_framebuffer *vfb,
854 struct drm_vmw_fence_rep __user *user_fence_rep,
855 struct drm_vmw_rect *vclips,
858 switch (dev_priv->active_display_unit) {
859 case vmw_du_screen_object:
860 return vmw_kms_sou_readback(dev_priv, file_priv, vfb,
861 user_fence_rep, vclips, num_clips,
863 case vmw_du_screen_target:
864 return vmw_kms_stdu_dma(dev_priv, file_priv, vfb,
865 user_fence_rep, NULL, vclips, num_clips,
866 1, false, true, NULL);
869 "Readback called with invalid display system.\n");
876 static const struct drm_framebuffer_funcs vmw_framebuffer_surface_funcs = {
877 .destroy = vmw_framebuffer_surface_destroy,
878 .dirty = drm_atomic_helper_dirtyfb,
881 static int vmw_kms_new_framebuffer_surface(struct vmw_private *dev_priv,
882 struct vmw_surface *surface,
883 struct vmw_framebuffer **out,
884 const struct drm_mode_fb_cmd2
889 struct drm_device *dev = dev_priv->dev;
890 struct vmw_framebuffer_surface *vfbs;
891 enum SVGA3dSurfaceFormat format;
893 struct drm_format_name_buf format_name;
895 /* 3D is only supported on HWv8 and newer hosts */
896 if (dev_priv->active_display_unit == vmw_du_legacy)
903 /* Surface must be marked as a scanout. */
904 if (unlikely(!surface->scanout))
907 if (unlikely(surface->mip_levels[0] != 1 ||
908 surface->num_sizes != 1 ||
909 surface->base_size.width < mode_cmd->width ||
910 surface->base_size.height < mode_cmd->height ||
911 surface->base_size.depth != 1)) {
912 DRM_ERROR("Incompatible surface dimensions "
913 "for requested mode.\n");
917 switch (mode_cmd->pixel_format) {
918 case DRM_FORMAT_ARGB8888:
919 format = SVGA3D_A8R8G8B8;
921 case DRM_FORMAT_XRGB8888:
922 format = SVGA3D_X8R8G8B8;
924 case DRM_FORMAT_RGB565:
925 format = SVGA3D_R5G6B5;
927 case DRM_FORMAT_XRGB1555:
928 format = SVGA3D_A1R5G5B5;
931 DRM_ERROR("Invalid pixel format: %s\n",
932 drm_get_format_name(mode_cmd->pixel_format, &format_name));
937 * For DX, surface format validation is done when surface->scanout
940 if (!dev_priv->has_dx && format != surface->format) {
941 DRM_ERROR("Invalid surface format for requested mode.\n");
945 vfbs = kzalloc(sizeof(*vfbs), GFP_KERNEL);
951 drm_helper_mode_fill_fb_struct(dev, &vfbs->base.base, mode_cmd);
952 vfbs->surface = vmw_surface_reference(surface);
953 vfbs->base.user_handle = mode_cmd->handles[0];
954 vfbs->is_bo_proxy = is_bo_proxy;
958 ret = drm_framebuffer_init(dev, &vfbs->base.base,
959 &vmw_framebuffer_surface_funcs);
966 vmw_surface_unreference(&surface);
973 * Buffer-object framebuffer code
976 static void vmw_framebuffer_bo_destroy(struct drm_framebuffer *framebuffer)
978 struct vmw_framebuffer_bo *vfbd =
979 vmw_framebuffer_to_vfbd(framebuffer);
981 drm_framebuffer_cleanup(framebuffer);
982 vmw_bo_unreference(&vfbd->buffer);
983 if (vfbd->base.user_obj)
984 ttm_base_object_unref(&vfbd->base.user_obj);
989 static int vmw_framebuffer_bo_dirty(struct drm_framebuffer *framebuffer,
990 struct drm_file *file_priv,
991 unsigned int flags, unsigned int color,
992 struct drm_clip_rect *clips,
993 unsigned int num_clips)
995 struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
996 struct vmw_framebuffer_bo *vfbd =
997 vmw_framebuffer_to_vfbd(framebuffer);
998 struct drm_clip_rect norect;
999 int ret, increment = 1;
1001 drm_modeset_lock_all(dev_priv->dev);
1003 ret = ttm_read_lock(&dev_priv->reservation_sem, true);
1004 if (unlikely(ret != 0)) {
1005 drm_modeset_unlock_all(dev_priv->dev);
1012 norect.x1 = norect.y1 = 0;
1013 norect.x2 = framebuffer->width;
1014 norect.y2 = framebuffer->height;
1015 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
1020 switch (dev_priv->active_display_unit) {
1022 ret = vmw_kms_ldu_do_bo_dirty(dev_priv, &vfbd->base, 0, 0,
1023 clips, num_clips, increment);
1027 WARN_ONCE(true, "Dirty called with invalid display system.\n");
1031 vmw_fifo_flush(dev_priv, false);
1032 ttm_read_unlock(&dev_priv->reservation_sem);
1034 drm_modeset_unlock_all(dev_priv->dev);
1039 static int vmw_framebuffer_bo_dirty_ext(struct drm_framebuffer *framebuffer,
1040 struct drm_file *file_priv,
1041 unsigned int flags, unsigned int color,
1042 struct drm_clip_rect *clips,
1043 unsigned int num_clips)
1045 struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
1047 if (dev_priv->active_display_unit == vmw_du_legacy)
1048 return vmw_framebuffer_bo_dirty(framebuffer, file_priv, flags,
1049 color, clips, num_clips);
1051 return drm_atomic_helper_dirtyfb(framebuffer, file_priv, flags, color,
1055 static const struct drm_framebuffer_funcs vmw_framebuffer_bo_funcs = {
1056 .destroy = vmw_framebuffer_bo_destroy,
1057 .dirty = vmw_framebuffer_bo_dirty_ext,
1061 * Pin the bofer in a location suitable for access by the
1064 static int vmw_framebuffer_pin(struct vmw_framebuffer *vfb)
1066 struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
1067 struct vmw_buffer_object *buf;
1068 struct ttm_placement *placement;
1071 buf = vfb->bo ? vmw_framebuffer_to_vfbd(&vfb->base)->buffer :
1072 vmw_framebuffer_to_vfbs(&vfb->base)->surface->res.backup;
1077 switch (dev_priv->active_display_unit) {
1079 vmw_overlay_pause_all(dev_priv);
1080 ret = vmw_bo_pin_in_start_of_vram(dev_priv, buf, false);
1081 vmw_overlay_resume_all(dev_priv);
1083 case vmw_du_screen_object:
1084 case vmw_du_screen_target:
1086 if (dev_priv->capabilities & SVGA_CAP_3D) {
1088 * Use surface DMA to get content to
1089 * sreen target surface.
1091 placement = &vmw_vram_gmr_placement;
1094 placement = &vmw_sys_placement;
1097 /* Use surface / image update */
1098 placement = &vmw_mob_placement;
1101 return vmw_bo_pin_in_placement(dev_priv, buf, placement, false);
1109 static int vmw_framebuffer_unpin(struct vmw_framebuffer *vfb)
1111 struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
1112 struct vmw_buffer_object *buf;
1114 buf = vfb->bo ? vmw_framebuffer_to_vfbd(&vfb->base)->buffer :
1115 vmw_framebuffer_to_vfbs(&vfb->base)->surface->res.backup;
1120 return vmw_bo_unpin(dev_priv, buf, false);
1124 * vmw_create_bo_proxy - create a proxy surface for the buffer object
1127 * @mode_cmd: parameters for the new surface
1128 * @bo_mob: MOB backing the buffer object
1129 * @srf_out: newly created surface
1131 * When the content FB is a buffer object, we create a surface as a proxy to the
1132 * same buffer. This way we can do a surface copy rather than a surface DMA.
1133 * This is a more efficient approach
1136 * 0 on success, error code otherwise
1138 static int vmw_create_bo_proxy(struct drm_device *dev,
1139 const struct drm_mode_fb_cmd2 *mode_cmd,
1140 struct vmw_buffer_object *bo_mob,
1141 struct vmw_surface **srf_out)
1144 struct drm_vmw_size content_base_size = {0};
1145 struct vmw_resource *res;
1146 unsigned int bytes_pp;
1147 struct drm_format_name_buf format_name;
1150 switch (mode_cmd->pixel_format) {
1151 case DRM_FORMAT_ARGB8888:
1152 case DRM_FORMAT_XRGB8888:
1153 format = SVGA3D_X8R8G8B8;
1157 case DRM_FORMAT_RGB565:
1158 case DRM_FORMAT_XRGB1555:
1159 format = SVGA3D_R5G6B5;
1169 DRM_ERROR("Invalid framebuffer format %s\n",
1170 drm_get_format_name(mode_cmd->pixel_format, &format_name));
1174 content_base_size.width = mode_cmd->pitches[0] / bytes_pp;
1175 content_base_size.height = mode_cmd->height;
1176 content_base_size.depth = 1;
1178 ret = vmw_surface_gb_priv_define(dev,
1179 0, /* kernel visible only */
1182 true, /* can be a scanout buffer */
1183 1, /* num of mip levels */
1187 SVGA3D_MS_PATTERN_NONE,
1188 SVGA3D_MS_QUALITY_NONE,
1191 DRM_ERROR("Failed to allocate proxy content buffer\n");
1195 res = &(*srf_out)->res;
1197 /* Reserve and switch the backing mob. */
1198 mutex_lock(&res->dev_priv->cmdbuf_mutex);
1199 (void) vmw_resource_reserve(res, false, true);
1200 vmw_bo_unreference(&res->backup);
1201 res->backup = vmw_bo_reference(bo_mob);
1202 res->backup_offset = 0;
1203 vmw_resource_unreserve(res, false, false, false, NULL, 0);
1204 mutex_unlock(&res->dev_priv->cmdbuf_mutex);
1211 static int vmw_kms_new_framebuffer_bo(struct vmw_private *dev_priv,
1212 struct vmw_buffer_object *bo,
1213 struct vmw_framebuffer **out,
1214 const struct drm_mode_fb_cmd2
1218 struct drm_device *dev = dev_priv->dev;
1219 struct vmw_framebuffer_bo *vfbd;
1220 unsigned int requested_size;
1221 struct drm_format_name_buf format_name;
1224 requested_size = mode_cmd->height * mode_cmd->pitches[0];
1225 if (unlikely(requested_size > bo->base.num_pages * PAGE_SIZE)) {
1226 DRM_ERROR("Screen buffer object size is too small "
1227 "for requested mode.\n");
1231 /* Limited framebuffer color depth support for screen objects */
1232 if (dev_priv->active_display_unit == vmw_du_screen_object) {
1233 switch (mode_cmd->pixel_format) {
1234 case DRM_FORMAT_XRGB8888:
1235 case DRM_FORMAT_ARGB8888:
1237 case DRM_FORMAT_XRGB1555:
1238 case DRM_FORMAT_RGB565:
1241 DRM_ERROR("Invalid pixel format: %s\n",
1242 drm_get_format_name(mode_cmd->pixel_format, &format_name));
1247 vfbd = kzalloc(sizeof(*vfbd), GFP_KERNEL);
1253 drm_helper_mode_fill_fb_struct(dev, &vfbd->base.base, mode_cmd);
1254 vfbd->base.bo = true;
1255 vfbd->buffer = vmw_bo_reference(bo);
1256 vfbd->base.user_handle = mode_cmd->handles[0];
1259 ret = drm_framebuffer_init(dev, &vfbd->base.base,
1260 &vmw_framebuffer_bo_funcs);
1267 vmw_bo_unreference(&bo);
1275 * vmw_kms_srf_ok - check if a surface can be created
1277 * @width: requested width
1278 * @height: requested height
1280 * Surfaces need to be less than texture size
1283 vmw_kms_srf_ok(struct vmw_private *dev_priv, uint32_t width, uint32_t height)
1285 if (width > dev_priv->texture_max_width ||
1286 height > dev_priv->texture_max_height)
1293 * vmw_kms_new_framebuffer - Create a new framebuffer.
1295 * @dev_priv: Pointer to device private struct.
1296 * @bo: Pointer to buffer object to wrap the kms framebuffer around.
1297 * Either @bo or @surface must be NULL.
1298 * @surface: Pointer to a surface to wrap the kms framebuffer around.
1299 * Either @bo or @surface must be NULL.
1300 * @only_2d: No presents will occur to this buffer object based framebuffer.
1301 * This helps the code to do some important optimizations.
1302 * @mode_cmd: Frame-buffer metadata.
1304 struct vmw_framebuffer *
1305 vmw_kms_new_framebuffer(struct vmw_private *dev_priv,
1306 struct vmw_buffer_object *bo,
1307 struct vmw_surface *surface,
1309 const struct drm_mode_fb_cmd2 *mode_cmd)
1311 struct vmw_framebuffer *vfb = NULL;
1312 bool is_bo_proxy = false;
1316 * We cannot use the SurfaceDMA command in an non-accelerated VM,
1317 * therefore, wrap the buffer object in a surface so we can use the
1318 * SurfaceCopy command.
1320 if (vmw_kms_srf_ok(dev_priv, mode_cmd->width, mode_cmd->height) &&
1322 mode_cmd->width > 64 && /* Don't create a proxy for cursor */
1323 dev_priv->active_display_unit == vmw_du_screen_target) {
1324 ret = vmw_create_bo_proxy(dev_priv->dev, mode_cmd,
1327 return ERR_PTR(ret);
1332 /* Create the new framebuffer depending one what we have */
1334 ret = vmw_kms_new_framebuffer_surface(dev_priv, surface, &vfb,
1339 * vmw_create_bo_proxy() adds a reference that is no longer
1343 vmw_surface_unreference(&surface);
1345 ret = vmw_kms_new_framebuffer_bo(dev_priv, bo, &vfb,
1352 return ERR_PTR(ret);
1354 vfb->pin = vmw_framebuffer_pin;
1355 vfb->unpin = vmw_framebuffer_unpin;
1361 * Generic Kernel modesetting functions
1364 static struct drm_framebuffer *vmw_kms_fb_create(struct drm_device *dev,
1365 struct drm_file *file_priv,
1366 const struct drm_mode_fb_cmd2 *mode_cmd)
1368 struct vmw_private *dev_priv = vmw_priv(dev);
1369 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
1370 struct vmw_framebuffer *vfb = NULL;
1371 struct vmw_surface *surface = NULL;
1372 struct vmw_buffer_object *bo = NULL;
1373 struct ttm_base_object *user_obj;
1377 * Take a reference on the user object of the resource
1378 * backing the kms fb. This ensures that user-space handle
1379 * lookups on that resource will always work as long as
1380 * it's registered with a kms framebuffer. This is important,
1381 * since vmw_execbuf_process identifies resources in the
1382 * command stream using user-space handles.
1385 user_obj = ttm_base_object_lookup(tfile, mode_cmd->handles[0]);
1386 if (unlikely(user_obj == NULL)) {
1387 DRM_ERROR("Could not locate requested kms frame buffer.\n");
1388 return ERR_PTR(-ENOENT);
1392 * End conditioned code.
1395 /* returns either a bo or surface */
1396 ret = vmw_user_lookup_handle(dev_priv, tfile,
1397 mode_cmd->handles[0],
1404 !vmw_kms_srf_ok(dev_priv, mode_cmd->width, mode_cmd->height)) {
1405 DRM_ERROR("Surface size cannot exceed %dx%d",
1406 dev_priv->texture_max_width,
1407 dev_priv->texture_max_height);
1412 vfb = vmw_kms_new_framebuffer(dev_priv, bo, surface,
1413 !(dev_priv->capabilities & SVGA_CAP_3D),
1421 /* vmw_user_lookup_handle takes one ref so does new_fb */
1423 vmw_bo_unreference(&bo);
1425 vmw_surface_unreference(&surface);
1428 DRM_ERROR("failed to create vmw_framebuffer: %i\n", ret);
1429 ttm_base_object_unref(&user_obj);
1430 return ERR_PTR(ret);
1432 vfb->user_obj = user_obj;
1438 * vmw_kms_check_display_memory - Validates display memory required for a
1441 * @num_rects: number of drm_rect in rects
1442 * @rects: array of drm_rect representing the topology to validate indexed by
1446 * 0 on success otherwise negative error code
1448 static int vmw_kms_check_display_memory(struct drm_device *dev,
1450 struct drm_rect *rects)
1452 struct vmw_private *dev_priv = vmw_priv(dev);
1453 struct drm_rect bounding_box = {0};
1454 u64 total_pixels = 0, pixel_mem, bb_mem;
1457 for (i = 0; i < num_rects; i++) {
1459 * For STDU only individual screen (screen target) is limited by
1460 * SCREENTARGET_MAX_WIDTH/HEIGHT registers.
1462 if (dev_priv->active_display_unit == vmw_du_screen_target &&
1463 (drm_rect_width(&rects[i]) > dev_priv->stdu_max_width ||
1464 drm_rect_height(&rects[i]) > dev_priv->stdu_max_height)) {
1465 VMW_DEBUG_KMS("Screen size not supported.\n");
1469 /* Bounding box upper left is at (0,0). */
1470 if (rects[i].x2 > bounding_box.x2)
1471 bounding_box.x2 = rects[i].x2;
1473 if (rects[i].y2 > bounding_box.y2)
1474 bounding_box.y2 = rects[i].y2;
1476 total_pixels += (u64) drm_rect_width(&rects[i]) *
1477 (u64) drm_rect_height(&rects[i]);
1480 /* Virtual svga device primary limits are always in 32-bpp. */
1481 pixel_mem = total_pixels * 4;
1484 * For HV10 and below prim_bb_mem is vram size. When
1485 * SVGA_REG_MAX_PRIMARY_BOUNDING_BOX_MEM is not present vram size is
1486 * limit on primary bounding box
1488 if (pixel_mem > dev_priv->prim_bb_mem) {
1489 VMW_DEBUG_KMS("Combined output size too large.\n");
1493 /* SVGA_CAP_NO_BB_RESTRICTION is available for STDU only. */
1494 if (dev_priv->active_display_unit != vmw_du_screen_target ||
1495 !(dev_priv->capabilities & SVGA_CAP_NO_BB_RESTRICTION)) {
1496 bb_mem = (u64) bounding_box.x2 * bounding_box.y2 * 4;
1498 if (bb_mem > dev_priv->prim_bb_mem) {
1499 VMW_DEBUG_KMS("Topology is beyond supported limits.\n");
1508 * vmw_crtc_state_and_lock - Return new or current crtc state with locked
1510 * @state: The atomic state pointer containing the new atomic state
1513 * This function returns the new crtc state if it's part of the state update.
1514 * Otherwise returns the current crtc state. It also makes sure that the
1515 * crtc mutex is locked.
1517 * Returns: A valid crtc state pointer or NULL. It may also return a
1518 * pointer error, in particular -EDEADLK if locking needs to be rerun.
1520 static struct drm_crtc_state *
1521 vmw_crtc_state_and_lock(struct drm_atomic_state *state, struct drm_crtc *crtc)
1523 struct drm_crtc_state *crtc_state;
1525 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
1527 lockdep_assert_held(&crtc->mutex.mutex.base);
1529 int ret = drm_modeset_lock(&crtc->mutex, state->acquire_ctx);
1531 if (ret != 0 && ret != -EALREADY)
1532 return ERR_PTR(ret);
1534 crtc_state = crtc->state;
1541 * vmw_kms_check_implicit - Verify that all implicit display units scan out
1542 * from the same fb after the new state is committed.
1543 * @dev: The drm_device.
1544 * @state: The new state to be checked.
1548 * -EINVAL on invalid state,
1549 * -EDEADLK if modeset locking needs to be rerun.
1551 static int vmw_kms_check_implicit(struct drm_device *dev,
1552 struct drm_atomic_state *state)
1554 struct drm_framebuffer *implicit_fb = NULL;
1555 struct drm_crtc *crtc;
1556 struct drm_crtc_state *crtc_state;
1557 struct drm_plane_state *plane_state;
1559 drm_for_each_crtc(crtc, dev) {
1560 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
1562 if (!du->is_implicit)
1565 crtc_state = vmw_crtc_state_and_lock(state, crtc);
1566 if (IS_ERR(crtc_state))
1567 return PTR_ERR(crtc_state);
1569 if (!crtc_state || !crtc_state->enable)
1573 * Can't move primary planes across crtcs, so this is OK.
1574 * It also means we don't need to take the plane mutex.
1576 plane_state = du->primary.state;
1577 if (plane_state->crtc != crtc)
1581 implicit_fb = plane_state->fb;
1582 else if (implicit_fb != plane_state->fb)
1590 * vmw_kms_check_topology - Validates topology in drm_atomic_state
1592 * @state: the driver state object
1595 * 0 on success otherwise negative error code
1597 static int vmw_kms_check_topology(struct drm_device *dev,
1598 struct drm_atomic_state *state)
1600 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
1601 struct drm_rect *rects;
1602 struct drm_crtc *crtc;
1606 rects = kcalloc(dev->mode_config.num_crtc, sizeof(struct drm_rect),
1611 drm_for_each_crtc(crtc, dev) {
1612 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
1613 struct drm_crtc_state *crtc_state;
1615 i = drm_crtc_index(crtc);
1617 crtc_state = vmw_crtc_state_and_lock(state, crtc);
1618 if (IS_ERR(crtc_state)) {
1619 ret = PTR_ERR(crtc_state);
1626 if (crtc_state->enable) {
1627 rects[i].x1 = du->gui_x;
1628 rects[i].y1 = du->gui_y;
1629 rects[i].x2 = du->gui_x + crtc_state->mode.hdisplay;
1630 rects[i].y2 = du->gui_y + crtc_state->mode.vdisplay;
1639 /* Determine change to topology due to new atomic state */
1640 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state,
1641 new_crtc_state, i) {
1642 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
1643 struct drm_connector *connector;
1644 struct drm_connector_state *conn_state;
1645 struct vmw_connector_state *vmw_conn_state;
1647 if (!du->pref_active && new_crtc_state->enable) {
1648 VMW_DEBUG_KMS("Enabling a disabled display unit\n");
1654 * For vmwgfx each crtc has only one connector attached and it
1655 * is not changed so don't really need to check the
1656 * crtc->connector_mask and iterate over it.
1658 connector = &du->connector;
1659 conn_state = drm_atomic_get_connector_state(state, connector);
1660 if (IS_ERR(conn_state)) {
1661 ret = PTR_ERR(conn_state);
1665 vmw_conn_state = vmw_connector_state_to_vcs(conn_state);
1666 vmw_conn_state->gui_x = du->gui_x;
1667 vmw_conn_state->gui_y = du->gui_y;
1670 ret = vmw_kms_check_display_memory(dev, dev->mode_config.num_crtc,
1679 * vmw_kms_atomic_check_modeset- validate state object for modeset changes
1682 * @state: the driver state object
1684 * This is a simple wrapper around drm_atomic_helper_check_modeset() for
1685 * us to assign a value to mode->crtc_clock so that
1686 * drm_calc_timestamping_constants() won't throw an error message
1689 * Zero for success or -errno
1692 vmw_kms_atomic_check_modeset(struct drm_device *dev,
1693 struct drm_atomic_state *state)
1695 struct drm_crtc *crtc;
1696 struct drm_crtc_state *crtc_state;
1697 bool need_modeset = false;
1700 ret = drm_atomic_helper_check(dev, state);
1704 ret = vmw_kms_check_implicit(dev, state);
1706 VMW_DEBUG_KMS("Invalid implicit state\n");
1710 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
1711 if (drm_atomic_crtc_needs_modeset(crtc_state))
1712 need_modeset = true;
1716 return vmw_kms_check_topology(dev, state);
1721 static const struct drm_mode_config_funcs vmw_kms_funcs = {
1722 .fb_create = vmw_kms_fb_create,
1723 .atomic_check = vmw_kms_atomic_check_modeset,
1724 .atomic_commit = drm_atomic_helper_commit,
1727 static int vmw_kms_generic_present(struct vmw_private *dev_priv,
1728 struct drm_file *file_priv,
1729 struct vmw_framebuffer *vfb,
1730 struct vmw_surface *surface,
1732 int32_t destX, int32_t destY,
1733 struct drm_vmw_rect *clips,
1736 return vmw_kms_sou_do_surface_dirty(dev_priv, vfb, NULL, clips,
1737 &surface->res, destX, destY,
1738 num_clips, 1, NULL, NULL);
1742 int vmw_kms_present(struct vmw_private *dev_priv,
1743 struct drm_file *file_priv,
1744 struct vmw_framebuffer *vfb,
1745 struct vmw_surface *surface,
1747 int32_t destX, int32_t destY,
1748 struct drm_vmw_rect *clips,
1753 switch (dev_priv->active_display_unit) {
1754 case vmw_du_screen_target:
1755 ret = vmw_kms_stdu_surface_dirty(dev_priv, vfb, NULL, clips,
1756 &surface->res, destX, destY,
1757 num_clips, 1, NULL, NULL);
1759 case vmw_du_screen_object:
1760 ret = vmw_kms_generic_present(dev_priv, file_priv, vfb, surface,
1761 sid, destX, destY, clips,
1766 "Present called with invalid display system.\n");
1773 vmw_fifo_flush(dev_priv, false);
1779 vmw_kms_create_hotplug_mode_update_property(struct vmw_private *dev_priv)
1781 if (dev_priv->hotplug_mode_update_property)
1784 dev_priv->hotplug_mode_update_property =
1785 drm_property_create_range(dev_priv->dev,
1786 DRM_MODE_PROP_IMMUTABLE,
1787 "hotplug_mode_update", 0, 1);
1789 if (!dev_priv->hotplug_mode_update_property)
1794 int vmw_kms_init(struct vmw_private *dev_priv)
1796 struct drm_device *dev = dev_priv->dev;
1799 drm_mode_config_init(dev);
1800 dev->mode_config.funcs = &vmw_kms_funcs;
1801 dev->mode_config.min_width = 1;
1802 dev->mode_config.min_height = 1;
1803 dev->mode_config.max_width = dev_priv->texture_max_width;
1804 dev->mode_config.max_height = dev_priv->texture_max_height;
1806 drm_mode_create_suggested_offset_properties(dev);
1807 vmw_kms_create_hotplug_mode_update_property(dev_priv);
1809 ret = vmw_kms_stdu_init_display(dev_priv);
1811 ret = vmw_kms_sou_init_display(dev_priv);
1812 if (ret) /* Fallback */
1813 ret = vmw_kms_ldu_init_display(dev_priv);
1819 int vmw_kms_close(struct vmw_private *dev_priv)
1824 * Docs says we should take the lock before calling this function
1825 * but since it destroys encoders and our destructor calls
1826 * drm_encoder_cleanup which takes the lock we deadlock.
1828 drm_mode_config_cleanup(dev_priv->dev);
1829 if (dev_priv->active_display_unit == vmw_du_legacy)
1830 ret = vmw_kms_ldu_close_display(dev_priv);
1835 int vmw_kms_cursor_bypass_ioctl(struct drm_device *dev, void *data,
1836 struct drm_file *file_priv)
1838 struct drm_vmw_cursor_bypass_arg *arg = data;
1839 struct vmw_display_unit *du;
1840 struct drm_crtc *crtc;
1844 mutex_lock(&dev->mode_config.mutex);
1845 if (arg->flags & DRM_VMW_CURSOR_BYPASS_ALL) {
1847 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1848 du = vmw_crtc_to_du(crtc);
1849 du->hotspot_x = arg->xhot;
1850 du->hotspot_y = arg->yhot;
1853 mutex_unlock(&dev->mode_config.mutex);
1857 crtc = drm_crtc_find(dev, file_priv, arg->crtc_id);
1863 du = vmw_crtc_to_du(crtc);
1865 du->hotspot_x = arg->xhot;
1866 du->hotspot_y = arg->yhot;
1869 mutex_unlock(&dev->mode_config.mutex);
1874 int vmw_kms_write_svga(struct vmw_private *vmw_priv,
1875 unsigned width, unsigned height, unsigned pitch,
1876 unsigned bpp, unsigned depth)
1878 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1879 vmw_write(vmw_priv, SVGA_REG_PITCHLOCK, pitch);
1880 else if (vmw_fifo_have_pitchlock(vmw_priv))
1881 vmw_mmio_write(pitch, vmw_priv->mmio_virt +
1882 SVGA_FIFO_PITCHLOCK);
1883 vmw_write(vmw_priv, SVGA_REG_WIDTH, width);
1884 vmw_write(vmw_priv, SVGA_REG_HEIGHT, height);
1885 vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, bpp);
1887 if (vmw_read(vmw_priv, SVGA_REG_DEPTH) != depth) {
1888 DRM_ERROR("Invalid depth %u for %u bpp, host expects %u\n",
1889 depth, bpp, vmw_read(vmw_priv, SVGA_REG_DEPTH));
1896 int vmw_kms_save_vga(struct vmw_private *vmw_priv)
1898 struct vmw_vga_topology_state *save;
1901 vmw_priv->vga_width = vmw_read(vmw_priv, SVGA_REG_WIDTH);
1902 vmw_priv->vga_height = vmw_read(vmw_priv, SVGA_REG_HEIGHT);
1903 vmw_priv->vga_bpp = vmw_read(vmw_priv, SVGA_REG_BITS_PER_PIXEL);
1904 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1905 vmw_priv->vga_pitchlock =
1906 vmw_read(vmw_priv, SVGA_REG_PITCHLOCK);
1907 else if (vmw_fifo_have_pitchlock(vmw_priv))
1908 vmw_priv->vga_pitchlock = vmw_mmio_read(vmw_priv->mmio_virt +
1909 SVGA_FIFO_PITCHLOCK);
1911 if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
1914 vmw_priv->num_displays = vmw_read(vmw_priv,
1915 SVGA_REG_NUM_GUEST_DISPLAYS);
1917 if (vmw_priv->num_displays == 0)
1918 vmw_priv->num_displays = 1;
1920 for (i = 0; i < vmw_priv->num_displays; ++i) {
1921 save = &vmw_priv->vga_save[i];
1922 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
1923 save->primary = vmw_read(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY);
1924 save->pos_x = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_X);
1925 save->pos_y = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y);
1926 save->width = vmw_read(vmw_priv, SVGA_REG_DISPLAY_WIDTH);
1927 save->height = vmw_read(vmw_priv, SVGA_REG_DISPLAY_HEIGHT);
1928 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
1929 if (i == 0 && vmw_priv->num_displays == 1 &&
1930 save->width == 0 && save->height == 0) {
1933 * It should be fairly safe to assume that these
1934 * values are uninitialized.
1937 save->width = vmw_priv->vga_width - save->pos_x;
1938 save->height = vmw_priv->vga_height - save->pos_y;
1945 int vmw_kms_restore_vga(struct vmw_private *vmw_priv)
1947 struct vmw_vga_topology_state *save;
1950 vmw_write(vmw_priv, SVGA_REG_WIDTH, vmw_priv->vga_width);
1951 vmw_write(vmw_priv, SVGA_REG_HEIGHT, vmw_priv->vga_height);
1952 vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, vmw_priv->vga_bpp);
1953 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1954 vmw_write(vmw_priv, SVGA_REG_PITCHLOCK,
1955 vmw_priv->vga_pitchlock);
1956 else if (vmw_fifo_have_pitchlock(vmw_priv))
1957 vmw_mmio_write(vmw_priv->vga_pitchlock,
1958 vmw_priv->mmio_virt + SVGA_FIFO_PITCHLOCK);
1960 if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
1963 for (i = 0; i < vmw_priv->num_displays; ++i) {
1964 save = &vmw_priv->vga_save[i];
1965 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
1966 vmw_write(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY, save->primary);
1967 vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_X, save->pos_x);
1968 vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y, save->pos_y);
1969 vmw_write(vmw_priv, SVGA_REG_DISPLAY_WIDTH, save->width);
1970 vmw_write(vmw_priv, SVGA_REG_DISPLAY_HEIGHT, save->height);
1971 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
1977 bool vmw_kms_validate_mode_vram(struct vmw_private *dev_priv,
1981 return ((u64) pitch * (u64) height) < (u64)
1982 ((dev_priv->active_display_unit == vmw_du_screen_target) ?
1983 dev_priv->prim_bb_mem : dev_priv->vram_size);
1988 * Function called by DRM code called with vbl_lock held.
1990 u32 vmw_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
1996 * Function called by DRM code called with vbl_lock held.
1998 int vmw_enable_vblank(struct drm_device *dev, unsigned int pipe)
2004 * Function called by DRM code called with vbl_lock held.
2006 void vmw_disable_vblank(struct drm_device *dev, unsigned int pipe)
2011 * vmw_du_update_layout - Update the display unit with topology from resolution
2012 * plugin and generate DRM uevent
2013 * @dev_priv: device private
2014 * @num_rects: number of drm_rect in rects
2015 * @rects: toplogy to update
2017 static int vmw_du_update_layout(struct vmw_private *dev_priv,
2018 unsigned int num_rects, struct drm_rect *rects)
2020 struct drm_device *dev = dev_priv->dev;
2021 struct vmw_display_unit *du;
2022 struct drm_connector *con;
2023 struct drm_connector_list_iter conn_iter;
2024 struct drm_modeset_acquire_ctx ctx;
2025 struct drm_crtc *crtc;
2028 /* Currently gui_x/y is protected with the crtc mutex */
2029 mutex_lock(&dev->mode_config.mutex);
2030 drm_modeset_acquire_init(&ctx, 0);
2032 drm_for_each_crtc(crtc, dev) {
2033 ret = drm_modeset_lock(&crtc->mutex, &ctx);
2035 if (ret == -EDEADLK) {
2036 drm_modeset_backoff(&ctx);
2043 drm_connector_list_iter_begin(dev, &conn_iter);
2044 drm_for_each_connector_iter(con, &conn_iter) {
2045 du = vmw_connector_to_du(con);
2046 if (num_rects > du->unit) {
2047 du->pref_width = drm_rect_width(&rects[du->unit]);
2048 du->pref_height = drm_rect_height(&rects[du->unit]);
2049 du->pref_active = true;
2050 du->gui_x = rects[du->unit].x1;
2051 du->gui_y = rects[du->unit].y1;
2053 du->pref_width = 800;
2054 du->pref_height = 600;
2055 du->pref_active = false;
2060 drm_connector_list_iter_end(&conn_iter);
2062 list_for_each_entry(con, &dev->mode_config.connector_list, head) {
2063 du = vmw_connector_to_du(con);
2064 if (num_rects > du->unit) {
2065 drm_object_property_set_value
2066 (&con->base, dev->mode_config.suggested_x_property,
2068 drm_object_property_set_value
2069 (&con->base, dev->mode_config.suggested_y_property,
2072 drm_object_property_set_value
2073 (&con->base, dev->mode_config.suggested_x_property,
2075 drm_object_property_set_value
2076 (&con->base, dev->mode_config.suggested_y_property,
2079 con->status = vmw_du_connector_detect(con, true);
2082 drm_sysfs_hotplug_event(dev);
2084 drm_modeset_drop_locks(&ctx);
2085 drm_modeset_acquire_fini(&ctx);
2086 mutex_unlock(&dev->mode_config.mutex);
2091 int vmw_du_crtc_gamma_set(struct drm_crtc *crtc,
2092 u16 *r, u16 *g, u16 *b,
2094 struct drm_modeset_acquire_ctx *ctx)
2096 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
2099 for (i = 0; i < size; i++) {
2100 DRM_DEBUG("%d r/g/b = 0x%04x / 0x%04x / 0x%04x\n", i,
2102 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 0, r[i] >> 8);
2103 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 1, g[i] >> 8);
2104 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 2, b[i] >> 8);
2110 int vmw_du_connector_dpms(struct drm_connector *connector, int mode)
2115 enum drm_connector_status
2116 vmw_du_connector_detect(struct drm_connector *connector, bool force)
2118 uint32_t num_displays;
2119 struct drm_device *dev = connector->dev;
2120 struct vmw_private *dev_priv = vmw_priv(dev);
2121 struct vmw_display_unit *du = vmw_connector_to_du(connector);
2123 num_displays = vmw_read(dev_priv, SVGA_REG_NUM_DISPLAYS);
2125 return ((vmw_connector_to_du(connector)->unit < num_displays &&
2127 connector_status_connected : connector_status_disconnected);
2130 static struct drm_display_mode vmw_kms_connector_builtin[] = {
2132 { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 25175, 640, 656,
2133 752, 800, 0, 480, 489, 492, 525, 0,
2134 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
2136 { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 40000, 800, 840,
2137 968, 1056, 0, 600, 601, 605, 628, 0,
2138 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
2140 { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 65000, 1024, 1048,
2141 1184, 1344, 0, 768, 771, 777, 806, 0,
2142 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
2144 { DRM_MODE("1152x864", DRM_MODE_TYPE_DRIVER, 108000, 1152, 1216,
2145 1344, 1600, 0, 864, 865, 868, 900, 0,
2146 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
2148 { DRM_MODE("1280x768", DRM_MODE_TYPE_DRIVER, 79500, 1280, 1344,
2149 1472, 1664, 0, 768, 771, 778, 798, 0,
2150 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2152 { DRM_MODE("1280x800", DRM_MODE_TYPE_DRIVER, 83500, 1280, 1352,
2153 1480, 1680, 0, 800, 803, 809, 831, 0,
2154 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
2156 { DRM_MODE("1280x960", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1376,
2157 1488, 1800, 0, 960, 961, 964, 1000, 0,
2158 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
2159 /* 1280x1024@60Hz */
2160 { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1328,
2161 1440, 1688, 0, 1024, 1025, 1028, 1066, 0,
2162 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
2164 { DRM_MODE("1360x768", DRM_MODE_TYPE_DRIVER, 85500, 1360, 1424,
2165 1536, 1792, 0, 768, 771, 777, 795, 0,
2166 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
2167 /* 1440x1050@60Hz */
2168 { DRM_MODE("1400x1050", DRM_MODE_TYPE_DRIVER, 121750, 1400, 1488,
2169 1632, 1864, 0, 1050, 1053, 1057, 1089, 0,
2170 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2172 { DRM_MODE("1440x900", DRM_MODE_TYPE_DRIVER, 106500, 1440, 1520,
2173 1672, 1904, 0, 900, 903, 909, 934, 0,
2174 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2175 /* 1600x1200@60Hz */
2176 { DRM_MODE("1600x1200", DRM_MODE_TYPE_DRIVER, 162000, 1600, 1664,
2177 1856, 2160, 0, 1200, 1201, 1204, 1250, 0,
2178 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
2179 /* 1680x1050@60Hz */
2180 { DRM_MODE("1680x1050", DRM_MODE_TYPE_DRIVER, 146250, 1680, 1784,
2181 1960, 2240, 0, 1050, 1053, 1059, 1089, 0,
2182 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2183 /* 1792x1344@60Hz */
2184 { DRM_MODE("1792x1344", DRM_MODE_TYPE_DRIVER, 204750, 1792, 1920,
2185 2120, 2448, 0, 1344, 1345, 1348, 1394, 0,
2186 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2187 /* 1853x1392@60Hz */
2188 { DRM_MODE("1856x1392", DRM_MODE_TYPE_DRIVER, 218250, 1856, 1952,
2189 2176, 2528, 0, 1392, 1393, 1396, 1439, 0,
2190 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2191 /* 1920x1200@60Hz */
2192 { DRM_MODE("1920x1200", DRM_MODE_TYPE_DRIVER, 193250, 1920, 2056,
2193 2256, 2592, 0, 1200, 1203, 1209, 1245, 0,
2194 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2195 /* 1920x1440@60Hz */
2196 { DRM_MODE("1920x1440", DRM_MODE_TYPE_DRIVER, 234000, 1920, 2048,
2197 2256, 2600, 0, 1440, 1441, 1444, 1500, 0,
2198 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2199 /* 2560x1600@60Hz */
2200 { DRM_MODE("2560x1600", DRM_MODE_TYPE_DRIVER, 348500, 2560, 2752,
2201 3032, 3504, 0, 1600, 1603, 1609, 1658, 0,
2202 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2204 { DRM_MODE("", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) },
2208 * vmw_guess_mode_timing - Provide fake timings for a
2209 * 60Hz vrefresh mode.
2211 * @mode - Pointer to a struct drm_display_mode with hdisplay and vdisplay
2212 * members filled in.
2214 void vmw_guess_mode_timing(struct drm_display_mode *mode)
2216 mode->hsync_start = mode->hdisplay + 50;
2217 mode->hsync_end = mode->hsync_start + 50;
2218 mode->htotal = mode->hsync_end + 50;
2220 mode->vsync_start = mode->vdisplay + 50;
2221 mode->vsync_end = mode->vsync_start + 50;
2222 mode->vtotal = mode->vsync_end + 50;
2224 mode->clock = (u32)mode->htotal * (u32)mode->vtotal / 100 * 6;
2225 mode->vrefresh = drm_mode_vrefresh(mode);
2229 int vmw_du_connector_fill_modes(struct drm_connector *connector,
2230 uint32_t max_width, uint32_t max_height)
2232 struct vmw_display_unit *du = vmw_connector_to_du(connector);
2233 struct drm_device *dev = connector->dev;
2234 struct vmw_private *dev_priv = vmw_priv(dev);
2235 struct drm_display_mode *mode = NULL;
2236 struct drm_display_mode *bmode;
2237 struct drm_display_mode prefmode = { DRM_MODE("preferred",
2238 DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED,
2239 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2240 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC)
2243 u32 assumed_bpp = 4;
2245 if (dev_priv->assume_16bpp)
2248 max_width = min(max_width, dev_priv->texture_max_width);
2249 max_height = min(max_height, dev_priv->texture_max_height);
2252 * For STDU extra limit for a mode on SVGA_REG_SCREENTARGET_MAX_WIDTH/
2255 if (dev_priv->active_display_unit == vmw_du_screen_target) {
2256 max_width = min(max_width, dev_priv->stdu_max_width);
2257 max_height = min(max_height, dev_priv->stdu_max_height);
2260 /* Add preferred mode */
2261 mode = drm_mode_duplicate(dev, &prefmode);
2264 mode->hdisplay = du->pref_width;
2265 mode->vdisplay = du->pref_height;
2266 vmw_guess_mode_timing(mode);
2268 if (vmw_kms_validate_mode_vram(dev_priv,
2269 mode->hdisplay * assumed_bpp,
2271 drm_mode_probed_add(connector, mode);
2273 drm_mode_destroy(dev, mode);
2277 if (du->pref_mode) {
2278 list_del_init(&du->pref_mode->head);
2279 drm_mode_destroy(dev, du->pref_mode);
2282 /* mode might be null here, this is intended */
2283 du->pref_mode = mode;
2285 for (i = 0; vmw_kms_connector_builtin[i].type != 0; i++) {
2286 bmode = &vmw_kms_connector_builtin[i];
2287 if (bmode->hdisplay > max_width ||
2288 bmode->vdisplay > max_height)
2291 if (!vmw_kms_validate_mode_vram(dev_priv,
2292 bmode->hdisplay * assumed_bpp,
2296 mode = drm_mode_duplicate(dev, bmode);
2299 mode->vrefresh = drm_mode_vrefresh(mode);
2301 drm_mode_probed_add(connector, mode);
2304 drm_connector_list_update(connector);
2305 /* Move the prefered mode first, help apps pick the right mode. */
2306 drm_mode_sort(&connector->modes);
2312 * vmw_kms_update_layout_ioctl - Handler for DRM_VMW_UPDATE_LAYOUT ioctl
2313 * @dev: drm device for the ioctl
2314 * @data: data pointer for the ioctl
2315 * @file_priv: drm file for the ioctl call
2317 * Update preferred topology of display unit as per ioctl request. The topology
2318 * is expressed as array of drm_vmw_rect.
2320 * [0 0 640 480] [640 0 800 600] [0 480 640 480]
2323 * The x and y offset (upper left) in drm_vmw_rect cannot be less than 0. Beside
2324 * device limit on topology, x + w and y + h (lower right) cannot be greater
2325 * than INT_MAX. So topology beyond these limits will return with error.
2328 * Zero on success, negative errno on failure.
2330 int vmw_kms_update_layout_ioctl(struct drm_device *dev, void *data,
2331 struct drm_file *file_priv)
2333 struct vmw_private *dev_priv = vmw_priv(dev);
2334 struct drm_mode_config *mode_config = &dev->mode_config;
2335 struct drm_vmw_update_layout_arg *arg =
2336 (struct drm_vmw_update_layout_arg *)data;
2337 void __user *user_rects;
2338 struct drm_vmw_rect *rects;
2339 struct drm_rect *drm_rects;
2340 unsigned rects_size;
2343 if (!arg->num_outputs) {
2344 struct drm_rect def_rect = {0, 0, 800, 600};
2345 VMW_DEBUG_KMS("Default layout x1 = %d y1 = %d x2 = %d y2 = %d\n",
2346 def_rect.x1, def_rect.y1,
2347 def_rect.x2, def_rect.y2);
2348 vmw_du_update_layout(dev_priv, 1, &def_rect);
2352 rects_size = arg->num_outputs * sizeof(struct drm_vmw_rect);
2353 rects = kcalloc(arg->num_outputs, sizeof(struct drm_vmw_rect),
2355 if (unlikely(!rects))
2358 user_rects = (void __user *)(unsigned long)arg->rects;
2359 ret = copy_from_user(rects, user_rects, rects_size);
2360 if (unlikely(ret != 0)) {
2361 DRM_ERROR("Failed to get rects.\n");
2366 drm_rects = (struct drm_rect *)rects;
2368 VMW_DEBUG_KMS("Layout count = %u\n", arg->num_outputs);
2369 for (i = 0; i < arg->num_outputs; i++) {
2370 struct drm_vmw_rect curr_rect;
2372 /* Verify user-space for overflow as kernel use drm_rect */
2373 if ((rects[i].x + rects[i].w > INT_MAX) ||
2374 (rects[i].y + rects[i].h > INT_MAX)) {
2379 curr_rect = rects[i];
2380 drm_rects[i].x1 = curr_rect.x;
2381 drm_rects[i].y1 = curr_rect.y;
2382 drm_rects[i].x2 = curr_rect.x + curr_rect.w;
2383 drm_rects[i].y2 = curr_rect.y + curr_rect.h;
2385 VMW_DEBUG_KMS(" x1 = %d y1 = %d x2 = %d y2 = %d\n",
2386 drm_rects[i].x1, drm_rects[i].y1,
2387 drm_rects[i].x2, drm_rects[i].y2);
2390 * Currently this check is limiting the topology within
2391 * mode_config->max (which actually is max texture size
2392 * supported by virtual device). This limit is here to address
2393 * window managers that create a big framebuffer for whole
2396 if (drm_rects[i].x1 < 0 || drm_rects[i].y1 < 0 ||
2397 drm_rects[i].x2 > mode_config->max_width ||
2398 drm_rects[i].y2 > mode_config->max_height) {
2399 VMW_DEBUG_KMS("Invalid layout %d %d %d %d\n",
2400 drm_rects[i].x1, drm_rects[i].y1,
2401 drm_rects[i].x2, drm_rects[i].y2);
2407 ret = vmw_kms_check_display_memory(dev, arg->num_outputs, drm_rects);
2410 vmw_du_update_layout(dev_priv, arg->num_outputs, drm_rects);
2418 * vmw_kms_helper_dirty - Helper to build commands and perform actions based
2419 * on a set of cliprects and a set of display units.
2421 * @dev_priv: Pointer to a device private structure.
2422 * @framebuffer: Pointer to the framebuffer on which to perform the actions.
2423 * @clips: A set of struct drm_clip_rect. Either this os @vclips must be NULL.
2424 * Cliprects are given in framebuffer coordinates.
2425 * @vclips: A set of struct drm_vmw_rect cliprects. Either this or @clips must
2426 * be NULL. Cliprects are given in source coordinates.
2427 * @dest_x: X coordinate offset for the crtc / destination clip rects.
2428 * @dest_y: Y coordinate offset for the crtc / destination clip rects.
2429 * @num_clips: Number of cliprects in the @clips or @vclips array.
2430 * @increment: Integer with which to increment the clip counter when looping.
2431 * Used to skip a predetermined number of clip rects.
2432 * @dirty: Closure structure. See the description of struct vmw_kms_dirty.
2434 int vmw_kms_helper_dirty(struct vmw_private *dev_priv,
2435 struct vmw_framebuffer *framebuffer,
2436 const struct drm_clip_rect *clips,
2437 const struct drm_vmw_rect *vclips,
2438 s32 dest_x, s32 dest_y,
2441 struct vmw_kms_dirty *dirty)
2443 struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
2444 struct drm_crtc *crtc;
2448 dirty->dev_priv = dev_priv;
2450 /* If crtc is passed, no need to iterate over other display units */
2452 units[num_units++] = vmw_crtc_to_du(dirty->crtc);
2454 list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list,
2456 struct drm_plane *plane = crtc->primary;
2458 if (plane->state->fb == &framebuffer->base)
2459 units[num_units++] = vmw_crtc_to_du(crtc);
2463 for (k = 0; k < num_units; k++) {
2464 struct vmw_display_unit *unit = units[k];
2465 s32 crtc_x = unit->crtc.x;
2466 s32 crtc_y = unit->crtc.y;
2467 s32 crtc_width = unit->crtc.mode.hdisplay;
2468 s32 crtc_height = unit->crtc.mode.vdisplay;
2469 const struct drm_clip_rect *clips_ptr = clips;
2470 const struct drm_vmw_rect *vclips_ptr = vclips;
2473 if (dirty->fifo_reserve_size > 0) {
2474 dirty->cmd = VMW_FIFO_RESERVE(dev_priv,
2475 dirty->fifo_reserve_size);
2479 memset(dirty->cmd, 0, dirty->fifo_reserve_size);
2481 dirty->num_hits = 0;
2482 for (i = 0; i < num_clips; i++, clips_ptr += increment,
2483 vclips_ptr += increment) {
2488 * Select clip array type. Note that integer type
2489 * in @clips is unsigned short, whereas in @vclips
2493 dirty->fb_x = (s32) clips_ptr->x1;
2494 dirty->fb_y = (s32) clips_ptr->y1;
2495 dirty->unit_x2 = (s32) clips_ptr->x2 + dest_x -
2497 dirty->unit_y2 = (s32) clips_ptr->y2 + dest_y -
2500 dirty->fb_x = vclips_ptr->x;
2501 dirty->fb_y = vclips_ptr->y;
2502 dirty->unit_x2 = dirty->fb_x + vclips_ptr->w +
2504 dirty->unit_y2 = dirty->fb_y + vclips_ptr->h +
2508 dirty->unit_x1 = dirty->fb_x + dest_x - crtc_x;
2509 dirty->unit_y1 = dirty->fb_y + dest_y - crtc_y;
2511 /* Skip this clip if it's outside the crtc region */
2512 if (dirty->unit_x1 >= crtc_width ||
2513 dirty->unit_y1 >= crtc_height ||
2514 dirty->unit_x2 <= 0 || dirty->unit_y2 <= 0)
2517 /* Clip right and bottom to crtc limits */
2518 dirty->unit_x2 = min_t(s32, dirty->unit_x2,
2520 dirty->unit_y2 = min_t(s32, dirty->unit_y2,
2523 /* Clip left and top to crtc limits */
2524 clip_left = min_t(s32, dirty->unit_x1, 0);
2525 clip_top = min_t(s32, dirty->unit_y1, 0);
2526 dirty->unit_x1 -= clip_left;
2527 dirty->unit_y1 -= clip_top;
2528 dirty->fb_x -= clip_left;
2529 dirty->fb_y -= clip_top;
2534 dirty->fifo_commit(dirty);
2541 * vmw_kms_helper_validation_finish - Helper for post KMS command submission
2542 * cleanup and fencing
2543 * @dev_priv: Pointer to the device-private struct
2544 * @file_priv: Pointer identifying the client when user-space fencing is used
2545 * @ctx: Pointer to the validation context
2546 * @out_fence: If non-NULL, returned refcounted fence-pointer
2547 * @user_fence_rep: If non-NULL, pointer to user-space address area
2548 * in which to copy user-space fence info
2550 void vmw_kms_helper_validation_finish(struct vmw_private *dev_priv,
2551 struct drm_file *file_priv,
2552 struct vmw_validation_context *ctx,
2553 struct vmw_fence_obj **out_fence,
2554 struct drm_vmw_fence_rep __user *
2557 struct vmw_fence_obj *fence = NULL;
2558 uint32_t handle = 0;
2561 if (file_priv || user_fence_rep || vmw_validation_has_bos(ctx) ||
2563 ret = vmw_execbuf_fence_commands(file_priv, dev_priv, &fence,
2564 file_priv ? &handle : NULL);
2565 vmw_validation_done(ctx, fence);
2567 vmw_execbuf_copy_fence_user(dev_priv, vmw_fpriv(file_priv),
2568 ret, user_fence_rep, fence,
2573 vmw_fence_obj_unreference(&fence);
2577 * vmw_kms_update_proxy - Helper function to update a proxy surface from
2580 * @res: Pointer to the surface resource
2581 * @clips: Clip rects in framebuffer (surface) space.
2582 * @num_clips: Number of clips in @clips.
2583 * @increment: Integer with which to increment the clip counter when looping.
2584 * Used to skip a predetermined number of clip rects.
2586 * This function makes sure the proxy surface is updated from its backing MOB
2587 * using the region given by @clips. The surface resource @res and its backing
2588 * MOB needs to be reserved and validated on call.
2590 int vmw_kms_update_proxy(struct vmw_resource *res,
2591 const struct drm_clip_rect *clips,
2595 struct vmw_private *dev_priv = res->dev_priv;
2596 struct drm_vmw_size *size = &vmw_res_to_srf(res)->base_size;
2598 SVGA3dCmdHeader header;
2599 SVGA3dCmdUpdateGBImage body;
2602 size_t copy_size = 0;
2608 cmd = VMW_FIFO_RESERVE(dev_priv, sizeof(*cmd) * num_clips);
2612 for (i = 0; i < num_clips; ++i, clips += increment, ++cmd) {
2613 box = &cmd->body.box;
2615 cmd->header.id = SVGA_3D_CMD_UPDATE_GB_IMAGE;
2616 cmd->header.size = sizeof(cmd->body);
2617 cmd->body.image.sid = res->id;
2618 cmd->body.image.face = 0;
2619 cmd->body.image.mipmap = 0;
2621 if (clips->x1 > size->width || clips->x2 > size->width ||
2622 clips->y1 > size->height || clips->y2 > size->height) {
2623 DRM_ERROR("Invalid clips outsize of framebuffer.\n");
2630 box->w = clips->x2 - clips->x1;
2631 box->h = clips->y2 - clips->y1;
2634 copy_size += sizeof(*cmd);
2637 vmw_fifo_commit(dev_priv, copy_size);
2642 int vmw_kms_fbdev_init_data(struct vmw_private *dev_priv,
2646 struct drm_connector **p_con,
2647 struct drm_crtc **p_crtc,
2648 struct drm_display_mode **p_mode)
2650 struct drm_connector *con;
2651 struct vmw_display_unit *du;
2652 struct drm_display_mode *mode;
2656 mutex_lock(&dev_priv->dev->mode_config.mutex);
2657 list_for_each_entry(con, &dev_priv->dev->mode_config.connector_list,
2666 DRM_ERROR("Could not find initial display unit.\n");
2671 if (list_empty(&con->modes))
2672 (void) vmw_du_connector_fill_modes(con, max_width, max_height);
2674 if (list_empty(&con->modes)) {
2675 DRM_ERROR("Could not find initial display mode.\n");
2680 du = vmw_connector_to_du(con);
2682 *p_crtc = &du->crtc;
2684 list_for_each_entry(mode, &con->modes, head) {
2685 if (mode->type & DRM_MODE_TYPE_PREFERRED)
2689 if (mode->type & DRM_MODE_TYPE_PREFERRED)
2692 WARN_ONCE(true, "Could not find initial preferred mode.\n");
2693 *p_mode = list_first_entry(&con->modes,
2694 struct drm_display_mode,
2699 mutex_unlock(&dev_priv->dev->mode_config.mutex);
2705 * vmw_kms_create_implicit_placement_proparty - Set up the implicit placement
2708 * @dev_priv: Pointer to a device private struct.
2710 * Sets up the implicit placement property unless it's already set up.
2713 vmw_kms_create_implicit_placement_property(struct vmw_private *dev_priv)
2715 if (dev_priv->implicit_placement_property)
2718 dev_priv->implicit_placement_property =
2719 drm_property_create_range(dev_priv->dev,
2720 DRM_MODE_PROP_IMMUTABLE,
2721 "implicit_placement", 0, 1);
2725 * vmw_kms_suspend - Save modesetting state and turn modesetting off.
2727 * @dev: Pointer to the drm device
2728 * Return: 0 on success. Negative error code on failure.
2730 int vmw_kms_suspend(struct drm_device *dev)
2732 struct vmw_private *dev_priv = vmw_priv(dev);
2734 dev_priv->suspend_state = drm_atomic_helper_suspend(dev);
2735 if (IS_ERR(dev_priv->suspend_state)) {
2736 int ret = PTR_ERR(dev_priv->suspend_state);
2738 DRM_ERROR("Failed kms suspend: %d\n", ret);
2739 dev_priv->suspend_state = NULL;
2749 * vmw_kms_resume - Re-enable modesetting and restore state
2751 * @dev: Pointer to the drm device
2752 * Return: 0 on success. Negative error code on failure.
2754 * State is resumed from a previous vmw_kms_suspend(). It's illegal
2755 * to call this function without a previous vmw_kms_suspend().
2757 int vmw_kms_resume(struct drm_device *dev)
2759 struct vmw_private *dev_priv = vmw_priv(dev);
2762 if (WARN_ON(!dev_priv->suspend_state))
2765 ret = drm_atomic_helper_resume(dev, dev_priv->suspend_state);
2766 dev_priv->suspend_state = NULL;
2772 * vmw_kms_lost_device - Notify kms that modesetting capabilities will be lost
2774 * @dev: Pointer to the drm device
2776 void vmw_kms_lost_device(struct drm_device *dev)
2778 drm_atomic_helper_shutdown(dev);
2782 * vmw_du_helper_plane_update - Helper to do plane update on a display unit.
2783 * @update: The closure structure.
2785 * Call this helper after setting callbacks in &vmw_du_update_plane to do plane
2786 * update on display unit.
2788 * Return: 0 on success or a negative error code on failure.
2790 int vmw_du_helper_plane_update(struct vmw_du_update_plane *update)
2792 struct drm_plane_state *state = update->plane->state;
2793 struct drm_plane_state *old_state = update->old_state;
2794 struct drm_atomic_helper_damage_iter iter;
2795 struct drm_rect clip;
2797 DECLARE_VAL_CONTEXT(val_ctx, NULL, 0);
2798 uint32_t reserved_size = 0;
2799 uint32_t submit_size = 0;
2800 uint32_t curr_size = 0;
2801 uint32_t num_hits = 0;
2807 * Iterate in advance to check if really need plane update and find the
2808 * number of clips that actually are in plane src for fifo allocation.
2810 drm_atomic_helper_damage_iter_init(&iter, old_state, state);
2811 drm_atomic_for_each_plane_damage(&iter, &clip)
2817 if (update->vfb->bo) {
2818 struct vmw_framebuffer_bo *vfbbo =
2819 container_of(update->vfb, typeof(*vfbbo), base);
2821 ret = vmw_validation_add_bo(&val_ctx, vfbbo->buffer, false,
2824 struct vmw_framebuffer_surface *vfbs =
2825 container_of(update->vfb, typeof(*vfbs), base);
2827 ret = vmw_validation_add_resource(&val_ctx, &vfbs->surface->res,
2828 0, VMW_RES_DIRTY_NONE, NULL,
2835 ret = vmw_validation_prepare(&val_ctx, update->mutex, update->intr);
2839 reserved_size = update->calc_fifo_size(update, num_hits);
2840 cmd_start = VMW_FIFO_RESERVE(update->dev_priv, reserved_size);
2846 cmd_next = cmd_start;
2848 if (update->post_prepare) {
2849 curr_size = update->post_prepare(update, cmd_next);
2850 cmd_next += curr_size;
2851 submit_size += curr_size;
2854 if (update->pre_clip) {
2855 curr_size = update->pre_clip(update, cmd_next, num_hits);
2856 cmd_next += curr_size;
2857 submit_size += curr_size;
2865 drm_atomic_helper_damage_iter_init(&iter, old_state, state);
2866 drm_atomic_for_each_plane_damage(&iter, &clip) {
2867 uint32_t fb_x = clip.x1;
2868 uint32_t fb_y = clip.y1;
2870 vmw_du_translate_to_crtc(state, &clip);
2872 curr_size = update->clip(update, cmd_next, &clip, fb_x,
2874 cmd_next += curr_size;
2875 submit_size += curr_size;
2877 bb.x1 = min_t(int, bb.x1, clip.x1);
2878 bb.y1 = min_t(int, bb.y1, clip.y1);
2879 bb.x2 = max_t(int, bb.x2, clip.x2);
2880 bb.y2 = max_t(int, bb.y2, clip.y2);
2883 curr_size = update->post_clip(update, cmd_next, &bb);
2884 submit_size += curr_size;
2886 if (reserved_size < submit_size)
2889 vmw_fifo_commit(update->dev_priv, submit_size);
2891 vmw_kms_helper_validation_finish(update->dev_priv, NULL, &val_ctx,
2892 update->out_fence, NULL);
2896 vmw_validation_revert(&val_ctx);
2899 vmw_validation_unref_lists(&val_ctx);