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)) {
69 DRM_ERROR("Fifo reserve failed.\n");
73 memset(cmd, 0, sizeof(*cmd));
75 memcpy(&cmd[1], image, image_size);
77 cmd->cmd = SVGA_CMD_DEFINE_ALPHA_CURSOR;
79 cmd->cursor.width = width;
80 cmd->cursor.height = height;
81 cmd->cursor.hotspotX = hotspotX;
82 cmd->cursor.hotspotY = hotspotY;
84 vmw_fifo_commit_flush(dev_priv, cmd_size);
89 static int vmw_cursor_update_bo(struct vmw_private *dev_priv,
90 struct vmw_buffer_object *bo,
91 u32 width, u32 height,
92 u32 hotspotX, u32 hotspotY)
94 struct ttm_bo_kmap_obj map;
95 unsigned long kmap_offset;
96 unsigned long kmap_num;
102 kmap_num = (width*height*4 + PAGE_SIZE - 1) >> PAGE_SHIFT;
104 ret = ttm_bo_reserve(&bo->base, true, false, NULL);
105 if (unlikely(ret != 0)) {
106 DRM_ERROR("reserve failed\n");
110 ret = ttm_bo_kmap(&bo->base, kmap_offset, kmap_num, &map);
111 if (unlikely(ret != 0))
114 virtual = ttm_kmap_obj_virtual(&map, &dummy);
115 ret = vmw_cursor_update_image(dev_priv, virtual, width, height,
120 ttm_bo_unreserve(&bo->base);
126 static void vmw_cursor_update_position(struct vmw_private *dev_priv,
127 bool show, int x, int y)
129 u32 *fifo_mem = dev_priv->mmio_virt;
132 spin_lock(&dev_priv->cursor_lock);
133 vmw_mmio_write(show ? 1 : 0, fifo_mem + SVGA_FIFO_CURSOR_ON);
134 vmw_mmio_write(x, fifo_mem + SVGA_FIFO_CURSOR_X);
135 vmw_mmio_write(y, fifo_mem + SVGA_FIFO_CURSOR_Y);
136 count = vmw_mmio_read(fifo_mem + SVGA_FIFO_CURSOR_COUNT);
137 vmw_mmio_write(++count, fifo_mem + SVGA_FIFO_CURSOR_COUNT);
138 spin_unlock(&dev_priv->cursor_lock);
142 void vmw_kms_cursor_snoop(struct vmw_surface *srf,
143 struct ttm_object_file *tfile,
144 struct ttm_buffer_object *bo,
145 SVGA3dCmdHeader *header)
147 struct ttm_bo_kmap_obj map;
148 unsigned long kmap_offset;
149 unsigned long kmap_num;
155 SVGA3dCmdHeader header;
156 SVGA3dCmdSurfaceDMA dma;
160 cmd = container_of(header, struct vmw_dma_cmd, header);
162 /* No snooper installed */
163 if (!srf->snooper.image)
166 if (cmd->dma.host.face != 0 || cmd->dma.host.mipmap != 0) {
167 DRM_ERROR("face and mipmap for cursors should never != 0\n");
171 if (cmd->header.size < 64) {
172 DRM_ERROR("at least one full copy box must be given\n");
176 box = (SVGA3dCopyBox *)&cmd[1];
177 box_count = (cmd->header.size - sizeof(SVGA3dCmdSurfaceDMA)) /
178 sizeof(SVGA3dCopyBox);
180 if (cmd->dma.guest.ptr.offset % PAGE_SIZE ||
181 box->x != 0 || box->y != 0 || box->z != 0 ||
182 box->srcx != 0 || box->srcy != 0 || box->srcz != 0 ||
183 box->d != 1 || box_count != 1) {
184 /* TODO handle none page aligned offsets */
185 /* TODO handle more dst & src != 0 */
186 /* TODO handle more then one copy */
187 DRM_ERROR("Cant snoop dma request for cursor!\n");
188 DRM_ERROR("(%u, %u, %u) (%u, %u, %u) (%ux%ux%u) %u %u\n",
189 box->srcx, box->srcy, box->srcz,
190 box->x, box->y, box->z,
191 box->w, box->h, box->d, box_count,
192 cmd->dma.guest.ptr.offset);
196 kmap_offset = cmd->dma.guest.ptr.offset >> PAGE_SHIFT;
197 kmap_num = (64*64*4) >> PAGE_SHIFT;
199 ret = ttm_bo_reserve(bo, true, false, NULL);
200 if (unlikely(ret != 0)) {
201 DRM_ERROR("reserve failed\n");
205 ret = ttm_bo_kmap(bo, kmap_offset, kmap_num, &map);
206 if (unlikely(ret != 0))
209 virtual = ttm_kmap_obj_virtual(&map, &dummy);
211 if (box->w == 64 && cmd->dma.guest.pitch == 64*4) {
212 memcpy(srf->snooper.image, virtual, 64*64*4);
214 /* Image is unsigned pointer. */
215 for (i = 0; i < box->h; i++)
216 memcpy(srf->snooper.image + i * 64,
217 virtual + i * cmd->dma.guest.pitch,
225 ttm_bo_unreserve(bo);
229 * vmw_kms_legacy_hotspot_clear - Clear legacy hotspots
231 * @dev_priv: Pointer to the device private struct.
233 * Clears all legacy hotspots.
235 void vmw_kms_legacy_hotspot_clear(struct vmw_private *dev_priv)
237 struct drm_device *dev = dev_priv->dev;
238 struct vmw_display_unit *du;
239 struct drm_crtc *crtc;
241 drm_modeset_lock_all(dev);
242 drm_for_each_crtc(crtc, dev) {
243 du = vmw_crtc_to_du(crtc);
248 drm_modeset_unlock_all(dev);
251 void vmw_kms_cursor_post_execbuf(struct vmw_private *dev_priv)
253 struct drm_device *dev = dev_priv->dev;
254 struct vmw_display_unit *du;
255 struct drm_crtc *crtc;
257 mutex_lock(&dev->mode_config.mutex);
259 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
260 du = vmw_crtc_to_du(crtc);
261 if (!du->cursor_surface ||
262 du->cursor_age == du->cursor_surface->snooper.age)
265 du->cursor_age = du->cursor_surface->snooper.age;
266 vmw_cursor_update_image(dev_priv,
267 du->cursor_surface->snooper.image,
269 du->hotspot_x + du->core_hotspot_x,
270 du->hotspot_y + du->core_hotspot_y);
273 mutex_unlock(&dev->mode_config.mutex);
277 void vmw_du_cursor_plane_destroy(struct drm_plane *plane)
279 vmw_cursor_update_position(plane->dev->dev_private, false, 0, 0);
281 drm_plane_cleanup(plane);
285 void vmw_du_primary_plane_destroy(struct drm_plane *plane)
287 drm_plane_cleanup(plane);
289 /* Planes are static in our case so we don't free it */
294 * vmw_du_vps_unpin_surf - unpins resource associated with a framebuffer surface
296 * @vps: plane state associated with the display surface
297 * @unreference: true if we also want to unreference the display.
299 void vmw_du_plane_unpin_surf(struct vmw_plane_state *vps,
304 vmw_resource_unpin(&vps->surf->res);
310 DRM_ERROR("Surface still pinned\n");
311 vmw_surface_unreference(&vps->surf);
318 * vmw_du_plane_cleanup_fb - Unpins the cursor
320 * @plane: display plane
321 * @old_state: Contains the FB to clean up
323 * Unpins the framebuffer surface
325 * Returns 0 on success
328 vmw_du_plane_cleanup_fb(struct drm_plane *plane,
329 struct drm_plane_state *old_state)
331 struct vmw_plane_state *vps = vmw_plane_state_to_vps(old_state);
333 vmw_du_plane_unpin_surf(vps, false);
338 * vmw_du_cursor_plane_prepare_fb - Readies the cursor by referencing it
340 * @plane: display plane
341 * @new_state: info on the new plane state, including the FB
343 * Returns 0 on success
346 vmw_du_cursor_plane_prepare_fb(struct drm_plane *plane,
347 struct drm_plane_state *new_state)
349 struct drm_framebuffer *fb = new_state->fb;
350 struct vmw_plane_state *vps = vmw_plane_state_to_vps(new_state);
354 vmw_surface_unreference(&vps->surf);
357 vmw_bo_unreference(&vps->bo);
360 if (vmw_framebuffer_to_vfb(fb)->bo) {
361 vps->bo = vmw_framebuffer_to_vfbd(fb)->buffer;
362 vmw_bo_reference(vps->bo);
364 vps->surf = vmw_framebuffer_to_vfbs(fb)->surface;
365 vmw_surface_reference(vps->surf);
374 vmw_du_cursor_plane_atomic_update(struct drm_plane *plane,
375 struct drm_plane_state *old_state)
377 struct drm_crtc *crtc = plane->state->crtc ?: old_state->crtc;
378 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
379 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
380 struct vmw_plane_state *vps = vmw_plane_state_to_vps(plane->state);
381 s32 hotspot_x, hotspot_y;
385 hotspot_x = du->hotspot_x;
386 hotspot_y = du->hotspot_y;
388 if (plane->state->fb) {
389 hotspot_x += plane->state->fb->hot_x;
390 hotspot_y += plane->state->fb->hot_y;
393 du->cursor_surface = vps->surf;
394 du->cursor_bo = vps->bo;
397 du->cursor_age = du->cursor_surface->snooper.age;
399 ret = vmw_cursor_update_image(dev_priv,
400 vps->surf->snooper.image,
403 } else if (vps->bo) {
404 ret = vmw_cursor_update_bo(dev_priv, vps->bo,
405 plane->state->crtc_w,
406 plane->state->crtc_h,
407 hotspot_x, hotspot_y);
409 vmw_cursor_update_position(dev_priv, false, 0, 0);
414 du->cursor_x = plane->state->crtc_x + du->set_gui_x;
415 du->cursor_y = plane->state->crtc_y + du->set_gui_y;
417 vmw_cursor_update_position(dev_priv, true,
418 du->cursor_x + hotspot_x,
419 du->cursor_y + hotspot_y);
421 du->core_hotspot_x = hotspot_x - du->hotspot_x;
422 du->core_hotspot_y = hotspot_y - du->hotspot_y;
424 DRM_ERROR("Failed to update cursor image\n");
430 * vmw_du_primary_plane_atomic_check - check if the new state is okay
432 * @plane: display plane
433 * @state: info on the new plane state, including the FB
435 * Check if the new state is settable given the current state. Other
436 * than what the atomic helper checks, we care about crtc fitting
437 * the FB and maintaining one active framebuffer.
439 * Returns 0 on success
441 int vmw_du_primary_plane_atomic_check(struct drm_plane *plane,
442 struct drm_plane_state *state)
444 struct drm_crtc_state *crtc_state = NULL;
445 struct drm_framebuffer *new_fb = state->fb;
449 crtc_state = drm_atomic_get_new_crtc_state(state->state, state->crtc);
451 ret = drm_atomic_helper_check_plane_state(state, crtc_state,
452 DRM_PLANE_HELPER_NO_SCALING,
453 DRM_PLANE_HELPER_NO_SCALING,
456 if (!ret && new_fb) {
457 struct drm_crtc *crtc = state->crtc;
458 struct vmw_connector_state *vcs;
459 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
461 vcs = vmw_connector_state_to_vcs(du->connector.state);
470 * vmw_du_cursor_plane_atomic_check - check if the new state is okay
472 * @plane: cursor plane
473 * @state: info on the new plane state
475 * This is a chance to fail if the new cursor state does not fit
478 * Returns 0 on success
480 int vmw_du_cursor_plane_atomic_check(struct drm_plane *plane,
481 struct drm_plane_state *new_state)
484 struct drm_crtc_state *crtc_state = NULL;
485 struct vmw_surface *surface = NULL;
486 struct drm_framebuffer *fb = new_state->fb;
489 crtc_state = drm_atomic_get_new_crtc_state(new_state->state,
492 ret = drm_atomic_helper_check_plane_state(new_state, crtc_state,
493 DRM_PLANE_HELPER_NO_SCALING,
494 DRM_PLANE_HELPER_NO_SCALING,
503 /* A lot of the code assumes this */
504 if (new_state->crtc_w != 64 || new_state->crtc_h != 64) {
505 DRM_ERROR("Invalid cursor dimensions (%d, %d)\n",
506 new_state->crtc_w, new_state->crtc_h);
510 if (!vmw_framebuffer_to_vfb(fb)->bo)
511 surface = vmw_framebuffer_to_vfbs(fb)->surface;
513 if (surface && !surface->snooper.image) {
514 DRM_ERROR("surface not suitable for cursor\n");
522 int vmw_du_crtc_atomic_check(struct drm_crtc *crtc,
523 struct drm_crtc_state *new_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_crtc_state *old_crtc_state)
558 void vmw_du_crtc_atomic_flush(struct drm_crtc *crtc,
559 struct drm_crtc_state *old_crtc_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 crtc->state = &vcs->base;
631 crtc->state->crtc = crtc;
636 * vmw_du_crtc_destroy_state - destroy crtc state
638 * @state: state object to destroy
640 * Destroys the crtc state (both common and vmw-specific) for the
644 vmw_du_crtc_destroy_state(struct drm_crtc *crtc,
645 struct drm_crtc_state *state)
647 drm_atomic_helper_crtc_destroy_state(crtc, state);
652 * vmw_du_plane_duplicate_state - duplicate plane state
655 * Allocates and returns a copy of the plane state (both common and
656 * vmw-specific) for the specified plane.
658 * Returns: The newly allocated plane state, or NULL on failure.
660 struct drm_plane_state *
661 vmw_du_plane_duplicate_state(struct drm_plane *plane)
663 struct drm_plane_state *state;
664 struct vmw_plane_state *vps;
666 vps = kmemdup(plane->state, sizeof(*vps), GFP_KERNEL);
674 /* Each ref counted resource needs to be acquired again */
676 (void) vmw_surface_reference(vps->surf);
679 (void) vmw_bo_reference(vps->bo);
683 __drm_atomic_helper_plane_duplicate_state(plane, state);
690 * vmw_du_plane_reset - creates a blank vmw plane state
693 * Resets the atomic state for @plane by freeing the state pointer (which might
694 * be NULL, e.g. at driver load time) and allocating a new empty state object.
696 void vmw_du_plane_reset(struct drm_plane *plane)
698 struct vmw_plane_state *vps;
702 vmw_du_plane_destroy_state(plane, plane->state);
704 vps = kzalloc(sizeof(*vps), GFP_KERNEL);
707 DRM_ERROR("Cannot allocate vmw_plane_state\n");
711 __drm_atomic_helper_plane_reset(plane, &vps->base);
716 * vmw_du_plane_destroy_state - destroy plane state
718 * @state: state object to destroy
720 * Destroys the plane state (both common and vmw-specific) for the
724 vmw_du_plane_destroy_state(struct drm_plane *plane,
725 struct drm_plane_state *state)
727 struct vmw_plane_state *vps = vmw_plane_state_to_vps(state);
730 /* Should have been freed by cleanup_fb */
732 vmw_surface_unreference(&vps->surf);
735 vmw_bo_unreference(&vps->bo);
737 drm_atomic_helper_plane_destroy_state(plane, state);
742 * vmw_du_connector_duplicate_state - duplicate connector state
743 * @connector: DRM connector
745 * Allocates and returns a copy of the connector state (both common and
746 * vmw-specific) for the specified connector.
748 * Returns: The newly allocated connector state, or NULL on failure.
750 struct drm_connector_state *
751 vmw_du_connector_duplicate_state(struct drm_connector *connector)
753 struct drm_connector_state *state;
754 struct vmw_connector_state *vcs;
756 if (WARN_ON(!connector->state))
759 vcs = kmemdup(connector->state, sizeof(*vcs), GFP_KERNEL);
766 __drm_atomic_helper_connector_duplicate_state(connector, state);
773 * vmw_du_connector_reset - creates a blank vmw connector state
774 * @connector: DRM connector
776 * Resets the atomic state for @connector by freeing the state pointer (which
777 * might be NULL, e.g. at driver load time) and allocating a new empty state
780 void vmw_du_connector_reset(struct drm_connector *connector)
782 struct vmw_connector_state *vcs;
785 if (connector->state) {
786 __drm_atomic_helper_connector_destroy_state(connector->state);
788 kfree(vmw_connector_state_to_vcs(connector->state));
791 vcs = kzalloc(sizeof(*vcs), GFP_KERNEL);
794 DRM_ERROR("Cannot allocate vmw_connector_state\n");
798 __drm_atomic_helper_connector_reset(connector, &vcs->base);
803 * vmw_du_connector_destroy_state - destroy connector state
804 * @connector: DRM connector
805 * @state: state object to destroy
807 * Destroys the connector state (both common and vmw-specific) for the
811 vmw_du_connector_destroy_state(struct drm_connector *connector,
812 struct drm_connector_state *state)
814 drm_atomic_helper_connector_destroy_state(connector, state);
817 * Generic framebuffer code
821 * Surface framebuffer code
824 static void vmw_framebuffer_surface_destroy(struct drm_framebuffer *framebuffer)
826 struct vmw_framebuffer_surface *vfbs =
827 vmw_framebuffer_to_vfbs(framebuffer);
829 drm_framebuffer_cleanup(framebuffer);
830 vmw_surface_unreference(&vfbs->surface);
831 if (vfbs->base.user_obj)
832 ttm_base_object_unref(&vfbs->base.user_obj);
838 * vmw_kms_readback - Perform a readback from the screen system to
839 * a buffer-object backed framebuffer.
841 * @dev_priv: Pointer to the device private structure.
842 * @file_priv: Pointer to a struct drm_file identifying the caller.
843 * Must be set to NULL if @user_fence_rep is NULL.
844 * @vfb: Pointer to the buffer-object backed framebuffer.
845 * @user_fence_rep: User-space provided structure for fence information.
846 * Must be set to non-NULL if @file_priv is non-NULL.
847 * @vclips: Array of clip rects.
848 * @num_clips: Number of clip rects in @vclips.
850 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
853 int vmw_kms_readback(struct vmw_private *dev_priv,
854 struct drm_file *file_priv,
855 struct vmw_framebuffer *vfb,
856 struct drm_vmw_fence_rep __user *user_fence_rep,
857 struct drm_vmw_rect *vclips,
860 switch (dev_priv->active_display_unit) {
861 case vmw_du_screen_object:
862 return vmw_kms_sou_readback(dev_priv, file_priv, vfb,
863 user_fence_rep, vclips, num_clips,
865 case vmw_du_screen_target:
866 return vmw_kms_stdu_dma(dev_priv, file_priv, vfb,
867 user_fence_rep, NULL, vclips, num_clips,
868 1, false, true, NULL);
871 "Readback called with invalid display system.\n");
878 static const struct drm_framebuffer_funcs vmw_framebuffer_surface_funcs = {
879 .destroy = vmw_framebuffer_surface_destroy,
880 .dirty = drm_atomic_helper_dirtyfb,
883 static int vmw_kms_new_framebuffer_surface(struct vmw_private *dev_priv,
884 struct vmw_surface *surface,
885 struct vmw_framebuffer **out,
886 const struct drm_mode_fb_cmd2
891 struct drm_device *dev = dev_priv->dev;
892 struct vmw_framebuffer_surface *vfbs;
893 enum SVGA3dSurfaceFormat format;
895 struct drm_format_name_buf format_name;
897 /* 3D is only supported on HWv8 and newer hosts */
898 if (dev_priv->active_display_unit == vmw_du_legacy)
905 /* Surface must be marked as a scanout. */
906 if (unlikely(!surface->scanout))
909 if (unlikely(surface->mip_levels[0] != 1 ||
910 surface->num_sizes != 1 ||
911 surface->base_size.width < mode_cmd->width ||
912 surface->base_size.height < mode_cmd->height ||
913 surface->base_size.depth != 1)) {
914 DRM_ERROR("Incompatible surface dimensions "
915 "for requested mode.\n");
919 switch (mode_cmd->pixel_format) {
920 case DRM_FORMAT_ARGB8888:
921 format = SVGA3D_A8R8G8B8;
923 case DRM_FORMAT_XRGB8888:
924 format = SVGA3D_X8R8G8B8;
926 case DRM_FORMAT_RGB565:
927 format = SVGA3D_R5G6B5;
929 case DRM_FORMAT_XRGB1555:
930 format = SVGA3D_A1R5G5B5;
933 DRM_ERROR("Invalid pixel format: %s\n",
934 drm_get_format_name(mode_cmd->pixel_format, &format_name));
939 * For DX, surface format validation is done when surface->scanout
942 if (!dev_priv->has_dx && format != surface->format) {
943 DRM_ERROR("Invalid surface format for requested mode.\n");
947 vfbs = kzalloc(sizeof(*vfbs), GFP_KERNEL);
953 drm_helper_mode_fill_fb_struct(dev, &vfbs->base.base, mode_cmd);
954 vfbs->surface = vmw_surface_reference(surface);
955 vfbs->base.user_handle = mode_cmd->handles[0];
956 vfbs->is_bo_proxy = is_bo_proxy;
960 ret = drm_framebuffer_init(dev, &vfbs->base.base,
961 &vmw_framebuffer_surface_funcs);
968 vmw_surface_unreference(&surface);
975 * Buffer-object framebuffer code
978 static void vmw_framebuffer_bo_destroy(struct drm_framebuffer *framebuffer)
980 struct vmw_framebuffer_bo *vfbd =
981 vmw_framebuffer_to_vfbd(framebuffer);
983 drm_framebuffer_cleanup(framebuffer);
984 vmw_bo_unreference(&vfbd->buffer);
985 if (vfbd->base.user_obj)
986 ttm_base_object_unref(&vfbd->base.user_obj);
991 static int vmw_framebuffer_bo_dirty(struct drm_framebuffer *framebuffer,
992 struct drm_file *file_priv,
993 unsigned int flags, unsigned int color,
994 struct drm_clip_rect *clips,
995 unsigned int num_clips)
997 struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
998 struct vmw_framebuffer_bo *vfbd =
999 vmw_framebuffer_to_vfbd(framebuffer);
1000 struct drm_clip_rect norect;
1001 int ret, increment = 1;
1003 drm_modeset_lock_all(dev_priv->dev);
1005 ret = ttm_read_lock(&dev_priv->reservation_sem, true);
1006 if (unlikely(ret != 0)) {
1007 drm_modeset_unlock_all(dev_priv->dev);
1014 norect.x1 = norect.y1 = 0;
1015 norect.x2 = framebuffer->width;
1016 norect.y2 = framebuffer->height;
1017 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
1022 switch (dev_priv->active_display_unit) {
1024 ret = vmw_kms_ldu_do_bo_dirty(dev_priv, &vfbd->base, 0, 0,
1025 clips, num_clips, increment);
1029 WARN_ONCE(true, "Dirty called with invalid display system.\n");
1033 vmw_fifo_flush(dev_priv, false);
1034 ttm_read_unlock(&dev_priv->reservation_sem);
1036 drm_modeset_unlock_all(dev_priv->dev);
1041 static int vmw_framebuffer_bo_dirty_ext(struct drm_framebuffer *framebuffer,
1042 struct drm_file *file_priv,
1043 unsigned int flags, unsigned int color,
1044 struct drm_clip_rect *clips,
1045 unsigned int num_clips)
1047 struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
1049 if (dev_priv->active_display_unit == vmw_du_legacy)
1050 return vmw_framebuffer_bo_dirty(framebuffer, file_priv, flags,
1051 color, clips, num_clips);
1053 return drm_atomic_helper_dirtyfb(framebuffer, file_priv, flags, color,
1057 static const struct drm_framebuffer_funcs vmw_framebuffer_bo_funcs = {
1058 .destroy = vmw_framebuffer_bo_destroy,
1059 .dirty = vmw_framebuffer_bo_dirty_ext,
1063 * Pin the bofer in a location suitable for access by the
1066 static int vmw_framebuffer_pin(struct vmw_framebuffer *vfb)
1068 struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
1069 struct vmw_buffer_object *buf;
1070 struct ttm_placement *placement;
1073 buf = vfb->bo ? vmw_framebuffer_to_vfbd(&vfb->base)->buffer :
1074 vmw_framebuffer_to_vfbs(&vfb->base)->surface->res.backup;
1079 switch (dev_priv->active_display_unit) {
1081 vmw_overlay_pause_all(dev_priv);
1082 ret = vmw_bo_pin_in_start_of_vram(dev_priv, buf, false);
1083 vmw_overlay_resume_all(dev_priv);
1085 case vmw_du_screen_object:
1086 case vmw_du_screen_target:
1088 if (dev_priv->capabilities & SVGA_CAP_3D) {
1090 * Use surface DMA to get content to
1091 * sreen target surface.
1093 placement = &vmw_vram_gmr_placement;
1096 placement = &vmw_sys_placement;
1099 /* Use surface / image update */
1100 placement = &vmw_mob_placement;
1103 return vmw_bo_pin_in_placement(dev_priv, buf, placement, false);
1111 static int vmw_framebuffer_unpin(struct vmw_framebuffer *vfb)
1113 struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
1114 struct vmw_buffer_object *buf;
1116 buf = vfb->bo ? vmw_framebuffer_to_vfbd(&vfb->base)->buffer :
1117 vmw_framebuffer_to_vfbs(&vfb->base)->surface->res.backup;
1122 return vmw_bo_unpin(dev_priv, buf, false);
1126 * vmw_create_bo_proxy - create a proxy surface for the buffer object
1129 * @mode_cmd: parameters for the new surface
1130 * @bo_mob: MOB backing the buffer object
1131 * @srf_out: newly created surface
1133 * When the content FB is a buffer object, we create a surface as a proxy to the
1134 * same buffer. This way we can do a surface copy rather than a surface DMA.
1135 * This is a more efficient approach
1138 * 0 on success, error code otherwise
1140 static int vmw_create_bo_proxy(struct drm_device *dev,
1141 const struct drm_mode_fb_cmd2 *mode_cmd,
1142 struct vmw_buffer_object *bo_mob,
1143 struct vmw_surface **srf_out)
1146 struct drm_vmw_size content_base_size = {0};
1147 struct vmw_resource *res;
1148 unsigned int bytes_pp;
1149 struct drm_format_name_buf format_name;
1152 switch (mode_cmd->pixel_format) {
1153 case DRM_FORMAT_ARGB8888:
1154 case DRM_FORMAT_XRGB8888:
1155 format = SVGA3D_X8R8G8B8;
1159 case DRM_FORMAT_RGB565:
1160 case DRM_FORMAT_XRGB1555:
1161 format = SVGA3D_R5G6B5;
1171 DRM_ERROR("Invalid framebuffer format %s\n",
1172 drm_get_format_name(mode_cmd->pixel_format, &format_name));
1176 content_base_size.width = mode_cmd->pitches[0] / bytes_pp;
1177 content_base_size.height = mode_cmd->height;
1178 content_base_size.depth = 1;
1180 ret = vmw_surface_gb_priv_define(dev,
1181 0, /* kernel visible only */
1184 true, /* can be a scanout buffer */
1185 1, /* num of mip levels */
1189 SVGA3D_MS_PATTERN_NONE,
1190 SVGA3D_MS_QUALITY_NONE,
1193 DRM_ERROR("Failed to allocate proxy content buffer\n");
1197 res = &(*srf_out)->res;
1199 /* Reserve and switch the backing mob. */
1200 mutex_lock(&res->dev_priv->cmdbuf_mutex);
1201 (void) vmw_resource_reserve(res, false, true);
1202 vmw_bo_unreference(&res->backup);
1203 res->backup = vmw_bo_reference(bo_mob);
1204 res->backup_offset = 0;
1205 vmw_resource_unreserve(res, false, NULL, 0);
1206 mutex_unlock(&res->dev_priv->cmdbuf_mutex);
1213 static int vmw_kms_new_framebuffer_bo(struct vmw_private *dev_priv,
1214 struct vmw_buffer_object *bo,
1215 struct vmw_framebuffer **out,
1216 const struct drm_mode_fb_cmd2
1220 struct drm_device *dev = dev_priv->dev;
1221 struct vmw_framebuffer_bo *vfbd;
1222 unsigned int requested_size;
1223 struct drm_format_name_buf format_name;
1226 requested_size = mode_cmd->height * mode_cmd->pitches[0];
1227 if (unlikely(requested_size > bo->base.num_pages * PAGE_SIZE)) {
1228 DRM_ERROR("Screen buffer object size is too small "
1229 "for requested mode.\n");
1233 /* Limited framebuffer color depth support for screen objects */
1234 if (dev_priv->active_display_unit == vmw_du_screen_object) {
1235 switch (mode_cmd->pixel_format) {
1236 case DRM_FORMAT_XRGB8888:
1237 case DRM_FORMAT_ARGB8888:
1239 case DRM_FORMAT_XRGB1555:
1240 case DRM_FORMAT_RGB565:
1243 DRM_ERROR("Invalid pixel format: %s\n",
1244 drm_get_format_name(mode_cmd->pixel_format, &format_name));
1249 vfbd = kzalloc(sizeof(*vfbd), GFP_KERNEL);
1255 drm_helper_mode_fill_fb_struct(dev, &vfbd->base.base, mode_cmd);
1256 vfbd->base.bo = true;
1257 vfbd->buffer = vmw_bo_reference(bo);
1258 vfbd->base.user_handle = mode_cmd->handles[0];
1261 ret = drm_framebuffer_init(dev, &vfbd->base.base,
1262 &vmw_framebuffer_bo_funcs);
1269 vmw_bo_unreference(&bo);
1277 * vmw_kms_srf_ok - check if a surface can be created
1279 * @width: requested width
1280 * @height: requested height
1282 * Surfaces need to be less than texture size
1285 vmw_kms_srf_ok(struct vmw_private *dev_priv, uint32_t width, uint32_t height)
1287 if (width > dev_priv->texture_max_width ||
1288 height > dev_priv->texture_max_height)
1295 * vmw_kms_new_framebuffer - Create a new framebuffer.
1297 * @dev_priv: Pointer to device private struct.
1298 * @bo: Pointer to buffer object to wrap the kms framebuffer around.
1299 * Either @bo or @surface must be NULL.
1300 * @surface: Pointer to a surface to wrap the kms framebuffer around.
1301 * Either @bo or @surface must be NULL.
1302 * @only_2d: No presents will occur to this buffer object based framebuffer.
1303 * This helps the code to do some important optimizations.
1304 * @mode_cmd: Frame-buffer metadata.
1306 struct vmw_framebuffer *
1307 vmw_kms_new_framebuffer(struct vmw_private *dev_priv,
1308 struct vmw_buffer_object *bo,
1309 struct vmw_surface *surface,
1311 const struct drm_mode_fb_cmd2 *mode_cmd)
1313 struct vmw_framebuffer *vfb = NULL;
1314 bool is_bo_proxy = false;
1318 * We cannot use the SurfaceDMA command in an non-accelerated VM,
1319 * therefore, wrap the buffer object in a surface so we can use the
1320 * SurfaceCopy command.
1322 if (vmw_kms_srf_ok(dev_priv, mode_cmd->width, mode_cmd->height) &&
1324 mode_cmd->width > 64 && /* Don't create a proxy for cursor */
1325 dev_priv->active_display_unit == vmw_du_screen_target) {
1326 ret = vmw_create_bo_proxy(dev_priv->dev, mode_cmd,
1329 return ERR_PTR(ret);
1334 /* Create the new framebuffer depending one what we have */
1336 ret = vmw_kms_new_framebuffer_surface(dev_priv, surface, &vfb,
1341 * vmw_create_bo_proxy() adds a reference that is no longer
1345 vmw_surface_unreference(&surface);
1347 ret = vmw_kms_new_framebuffer_bo(dev_priv, bo, &vfb,
1354 return ERR_PTR(ret);
1356 vfb->pin = vmw_framebuffer_pin;
1357 vfb->unpin = vmw_framebuffer_unpin;
1363 * Generic Kernel modesetting functions
1366 static struct drm_framebuffer *vmw_kms_fb_create(struct drm_device *dev,
1367 struct drm_file *file_priv,
1368 const struct drm_mode_fb_cmd2 *mode_cmd)
1370 struct vmw_private *dev_priv = vmw_priv(dev);
1371 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
1372 struct vmw_framebuffer *vfb = NULL;
1373 struct vmw_surface *surface = NULL;
1374 struct vmw_buffer_object *bo = NULL;
1375 struct ttm_base_object *user_obj;
1379 * Take a reference on the user object of the resource
1380 * backing the kms fb. This ensures that user-space handle
1381 * lookups on that resource will always work as long as
1382 * it's registered with a kms framebuffer. This is important,
1383 * since vmw_execbuf_process identifies resources in the
1384 * command stream using user-space handles.
1387 user_obj = ttm_base_object_lookup(tfile, mode_cmd->handles[0]);
1388 if (unlikely(user_obj == NULL)) {
1389 DRM_ERROR("Could not locate requested kms frame buffer.\n");
1390 return ERR_PTR(-ENOENT);
1394 * End conditioned code.
1397 /* returns either a bo or surface */
1398 ret = vmw_user_lookup_handle(dev_priv, tfile,
1399 mode_cmd->handles[0],
1406 !vmw_kms_srf_ok(dev_priv, mode_cmd->width, mode_cmd->height)) {
1407 DRM_ERROR("Surface size cannot exceed %dx%d",
1408 dev_priv->texture_max_width,
1409 dev_priv->texture_max_height);
1414 vfb = vmw_kms_new_framebuffer(dev_priv, bo, surface,
1415 !(dev_priv->capabilities & SVGA_CAP_3D),
1423 /* vmw_user_lookup_handle takes one ref so does new_fb */
1425 vmw_bo_unreference(&bo);
1427 vmw_surface_unreference(&surface);
1430 DRM_ERROR("failed to create vmw_framebuffer: %i\n", ret);
1431 ttm_base_object_unref(&user_obj);
1432 return ERR_PTR(ret);
1434 vfb->user_obj = user_obj;
1440 * vmw_kms_check_display_memory - Validates display memory required for a
1443 * @num_rects: number of drm_rect in rects
1444 * @rects: array of drm_rect representing the topology to validate indexed by
1448 * 0 on success otherwise negative error code
1450 static int vmw_kms_check_display_memory(struct drm_device *dev,
1452 struct drm_rect *rects)
1454 struct vmw_private *dev_priv = vmw_priv(dev);
1455 struct drm_rect bounding_box = {0};
1456 u64 total_pixels = 0, pixel_mem, bb_mem;
1459 for (i = 0; i < num_rects; i++) {
1461 * For STDU only individual screen (screen target) is limited by
1462 * SCREENTARGET_MAX_WIDTH/HEIGHT registers.
1464 if (dev_priv->active_display_unit == vmw_du_screen_target &&
1465 (drm_rect_width(&rects[i]) > dev_priv->stdu_max_width ||
1466 drm_rect_height(&rects[i]) > dev_priv->stdu_max_height)) {
1467 DRM_ERROR("Screen size not supported.\n");
1471 /* Bounding box upper left is at (0,0). */
1472 if (rects[i].x2 > bounding_box.x2)
1473 bounding_box.x2 = rects[i].x2;
1475 if (rects[i].y2 > bounding_box.y2)
1476 bounding_box.y2 = rects[i].y2;
1478 total_pixels += (u64) drm_rect_width(&rects[i]) *
1479 (u64) drm_rect_height(&rects[i]);
1482 /* Virtual svga device primary limits are always in 32-bpp. */
1483 pixel_mem = total_pixels * 4;
1486 * For HV10 and below prim_bb_mem is vram size. When
1487 * SVGA_REG_MAX_PRIMARY_BOUNDING_BOX_MEM is not present vram size is
1488 * limit on primary bounding box
1490 if (pixel_mem > dev_priv->prim_bb_mem) {
1491 DRM_ERROR("Combined output size too large.\n");
1495 /* SVGA_CAP_NO_BB_RESTRICTION is available for STDU only. */
1496 if (dev_priv->active_display_unit != vmw_du_screen_target ||
1497 !(dev_priv->capabilities & SVGA_CAP_NO_BB_RESTRICTION)) {
1498 bb_mem = (u64) bounding_box.x2 * bounding_box.y2 * 4;
1500 if (bb_mem > dev_priv->prim_bb_mem) {
1501 DRM_ERROR("Topology is beyond supported limits.\n");
1510 * vmw_crtc_state_and_lock - Return new or current crtc state with locked
1512 * @state: The atomic state pointer containing the new atomic state
1515 * This function returns the new crtc state if it's part of the state update.
1516 * Otherwise returns the current crtc state. It also makes sure that the
1517 * crtc mutex is locked.
1519 * Returns: A valid crtc state pointer or NULL. It may also return a
1520 * pointer error, in particular -EDEADLK if locking needs to be rerun.
1522 static struct drm_crtc_state *
1523 vmw_crtc_state_and_lock(struct drm_atomic_state *state, struct drm_crtc *crtc)
1525 struct drm_crtc_state *crtc_state;
1527 crtc_state = drm_atomic_get_new_crtc_state(state, crtc);
1529 lockdep_assert_held(&crtc->mutex.mutex.base);
1531 int ret = drm_modeset_lock(&crtc->mutex, state->acquire_ctx);
1533 if (ret != 0 && ret != -EALREADY)
1534 return ERR_PTR(ret);
1536 crtc_state = crtc->state;
1543 * vmw_kms_check_implicit - Verify that all implicit display units scan out
1544 * from the same fb after the new state is committed.
1545 * @dev: The drm_device.
1546 * @state: The new state to be checked.
1550 * -EINVAL on invalid state,
1551 * -EDEADLK if modeset locking needs to be rerun.
1553 static int vmw_kms_check_implicit(struct drm_device *dev,
1554 struct drm_atomic_state *state)
1556 struct drm_framebuffer *implicit_fb = NULL;
1557 struct drm_crtc *crtc;
1558 struct drm_crtc_state *crtc_state;
1559 struct drm_plane_state *plane_state;
1561 drm_for_each_crtc(crtc, dev) {
1562 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
1564 if (!du->is_implicit)
1567 crtc_state = vmw_crtc_state_and_lock(state, crtc);
1568 if (IS_ERR(crtc_state))
1569 return PTR_ERR(crtc_state);
1571 if (!crtc_state || !crtc_state->enable)
1575 * Can't move primary planes across crtcs, so this is OK.
1576 * It also means we don't need to take the plane mutex.
1578 plane_state = du->primary.state;
1579 if (plane_state->crtc != crtc)
1583 implicit_fb = plane_state->fb;
1584 else if (implicit_fb != plane_state->fb)
1592 * vmw_kms_check_topology - Validates topology in drm_atomic_state
1594 * @state: the driver state object
1597 * 0 on success otherwise negative error code
1599 static int vmw_kms_check_topology(struct drm_device *dev,
1600 struct drm_atomic_state *state)
1602 struct drm_crtc_state *old_crtc_state, *new_crtc_state;
1603 struct drm_rect *rects;
1604 struct drm_crtc *crtc;
1608 rects = kcalloc(dev->mode_config.num_crtc, sizeof(struct drm_rect),
1613 drm_for_each_crtc(crtc, dev) {
1614 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
1615 struct drm_crtc_state *crtc_state;
1617 i = drm_crtc_index(crtc);
1619 crtc_state = vmw_crtc_state_and_lock(state, crtc);
1620 if (IS_ERR(crtc_state)) {
1621 ret = PTR_ERR(crtc_state);
1628 if (crtc_state->enable) {
1629 rects[i].x1 = du->gui_x;
1630 rects[i].y1 = du->gui_y;
1631 rects[i].x2 = du->gui_x + crtc_state->mode.hdisplay;
1632 rects[i].y2 = du->gui_y + crtc_state->mode.vdisplay;
1641 /* Determine change to topology due to new atomic state */
1642 for_each_oldnew_crtc_in_state(state, crtc, old_crtc_state,
1643 new_crtc_state, i) {
1644 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
1645 struct drm_connector *connector;
1646 struct drm_connector_state *conn_state;
1647 struct vmw_connector_state *vmw_conn_state;
1649 if (!du->pref_active && new_crtc_state->enable) {
1655 * For vmwgfx each crtc has only one connector attached and it
1656 * is not changed so don't really need to check the
1657 * crtc->connector_mask and iterate over it.
1659 connector = &du->connector;
1660 conn_state = drm_atomic_get_connector_state(state, connector);
1661 if (IS_ERR(conn_state)) {
1662 ret = PTR_ERR(conn_state);
1666 vmw_conn_state = vmw_connector_state_to_vcs(conn_state);
1667 vmw_conn_state->gui_x = du->gui_x;
1668 vmw_conn_state->gui_y = du->gui_y;
1671 ret = vmw_kms_check_display_memory(dev, dev->mode_config.num_crtc,
1680 * vmw_kms_atomic_check_modeset- validate state object for modeset changes
1683 * @state: the driver state object
1685 * This is a simple wrapper around drm_atomic_helper_check_modeset() for
1686 * us to assign a value to mode->crtc_clock so that
1687 * drm_calc_timestamping_constants() won't throw an error message
1690 * Zero for success or -errno
1693 vmw_kms_atomic_check_modeset(struct drm_device *dev,
1694 struct drm_atomic_state *state)
1696 struct drm_crtc *crtc;
1697 struct drm_crtc_state *crtc_state;
1698 bool need_modeset = false;
1701 ret = drm_atomic_helper_check(dev, state);
1705 ret = vmw_kms_check_implicit(dev, state);
1709 if (!state->allow_modeset)
1713 * Legacy path do not set allow_modeset properly like
1714 * @drm_atomic_helper_update_plane, This will result in unnecessary call
1715 * to vmw_kms_check_topology. So extra set of check.
1717 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
1718 if (drm_atomic_crtc_needs_modeset(crtc_state))
1719 need_modeset = true;
1723 return vmw_kms_check_topology(dev, state);
1728 static const struct drm_mode_config_funcs vmw_kms_funcs = {
1729 .fb_create = vmw_kms_fb_create,
1730 .atomic_check = vmw_kms_atomic_check_modeset,
1731 .atomic_commit = drm_atomic_helper_commit,
1734 static int vmw_kms_generic_present(struct vmw_private *dev_priv,
1735 struct drm_file *file_priv,
1736 struct vmw_framebuffer *vfb,
1737 struct vmw_surface *surface,
1739 int32_t destX, int32_t destY,
1740 struct drm_vmw_rect *clips,
1743 return vmw_kms_sou_do_surface_dirty(dev_priv, vfb, NULL, clips,
1744 &surface->res, destX, destY,
1745 num_clips, 1, NULL, NULL);
1749 int vmw_kms_present(struct vmw_private *dev_priv,
1750 struct drm_file *file_priv,
1751 struct vmw_framebuffer *vfb,
1752 struct vmw_surface *surface,
1754 int32_t destX, int32_t destY,
1755 struct drm_vmw_rect *clips,
1760 switch (dev_priv->active_display_unit) {
1761 case vmw_du_screen_target:
1762 ret = vmw_kms_stdu_surface_dirty(dev_priv, vfb, NULL, clips,
1763 &surface->res, destX, destY,
1764 num_clips, 1, NULL, NULL);
1766 case vmw_du_screen_object:
1767 ret = vmw_kms_generic_present(dev_priv, file_priv, vfb, surface,
1768 sid, destX, destY, clips,
1773 "Present called with invalid display system.\n");
1780 vmw_fifo_flush(dev_priv, false);
1786 vmw_kms_create_hotplug_mode_update_property(struct vmw_private *dev_priv)
1788 if (dev_priv->hotplug_mode_update_property)
1791 dev_priv->hotplug_mode_update_property =
1792 drm_property_create_range(dev_priv->dev,
1793 DRM_MODE_PROP_IMMUTABLE,
1794 "hotplug_mode_update", 0, 1);
1796 if (!dev_priv->hotplug_mode_update_property)
1801 int vmw_kms_init(struct vmw_private *dev_priv)
1803 struct drm_device *dev = dev_priv->dev;
1806 drm_mode_config_init(dev);
1807 dev->mode_config.funcs = &vmw_kms_funcs;
1808 dev->mode_config.min_width = 1;
1809 dev->mode_config.min_height = 1;
1810 dev->mode_config.max_width = dev_priv->texture_max_width;
1811 dev->mode_config.max_height = dev_priv->texture_max_height;
1813 drm_mode_create_suggested_offset_properties(dev);
1814 vmw_kms_create_hotplug_mode_update_property(dev_priv);
1816 ret = vmw_kms_stdu_init_display(dev_priv);
1818 ret = vmw_kms_sou_init_display(dev_priv);
1819 if (ret) /* Fallback */
1820 ret = vmw_kms_ldu_init_display(dev_priv);
1826 int vmw_kms_close(struct vmw_private *dev_priv)
1831 * Docs says we should take the lock before calling this function
1832 * but since it destroys encoders and our destructor calls
1833 * drm_encoder_cleanup which takes the lock we deadlock.
1835 drm_mode_config_cleanup(dev_priv->dev);
1836 if (dev_priv->active_display_unit == vmw_du_legacy)
1837 ret = vmw_kms_ldu_close_display(dev_priv);
1842 int vmw_kms_cursor_bypass_ioctl(struct drm_device *dev, void *data,
1843 struct drm_file *file_priv)
1845 struct drm_vmw_cursor_bypass_arg *arg = data;
1846 struct vmw_display_unit *du;
1847 struct drm_crtc *crtc;
1851 mutex_lock(&dev->mode_config.mutex);
1852 if (arg->flags & DRM_VMW_CURSOR_BYPASS_ALL) {
1854 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1855 du = vmw_crtc_to_du(crtc);
1856 du->hotspot_x = arg->xhot;
1857 du->hotspot_y = arg->yhot;
1860 mutex_unlock(&dev->mode_config.mutex);
1864 crtc = drm_crtc_find(dev, file_priv, arg->crtc_id);
1870 du = vmw_crtc_to_du(crtc);
1872 du->hotspot_x = arg->xhot;
1873 du->hotspot_y = arg->yhot;
1876 mutex_unlock(&dev->mode_config.mutex);
1881 int vmw_kms_write_svga(struct vmw_private *vmw_priv,
1882 unsigned width, unsigned height, unsigned pitch,
1883 unsigned bpp, unsigned depth)
1885 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1886 vmw_write(vmw_priv, SVGA_REG_PITCHLOCK, pitch);
1887 else if (vmw_fifo_have_pitchlock(vmw_priv))
1888 vmw_mmio_write(pitch, vmw_priv->mmio_virt +
1889 SVGA_FIFO_PITCHLOCK);
1890 vmw_write(vmw_priv, SVGA_REG_WIDTH, width);
1891 vmw_write(vmw_priv, SVGA_REG_HEIGHT, height);
1892 vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, bpp);
1894 if (vmw_read(vmw_priv, SVGA_REG_DEPTH) != depth) {
1895 DRM_ERROR("Invalid depth %u for %u bpp, host expects %u\n",
1896 depth, bpp, vmw_read(vmw_priv, SVGA_REG_DEPTH));
1903 int vmw_kms_save_vga(struct vmw_private *vmw_priv)
1905 struct vmw_vga_topology_state *save;
1908 vmw_priv->vga_width = vmw_read(vmw_priv, SVGA_REG_WIDTH);
1909 vmw_priv->vga_height = vmw_read(vmw_priv, SVGA_REG_HEIGHT);
1910 vmw_priv->vga_bpp = vmw_read(vmw_priv, SVGA_REG_BITS_PER_PIXEL);
1911 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1912 vmw_priv->vga_pitchlock =
1913 vmw_read(vmw_priv, SVGA_REG_PITCHLOCK);
1914 else if (vmw_fifo_have_pitchlock(vmw_priv))
1915 vmw_priv->vga_pitchlock = vmw_mmio_read(vmw_priv->mmio_virt +
1916 SVGA_FIFO_PITCHLOCK);
1918 if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
1921 vmw_priv->num_displays = vmw_read(vmw_priv,
1922 SVGA_REG_NUM_GUEST_DISPLAYS);
1924 if (vmw_priv->num_displays == 0)
1925 vmw_priv->num_displays = 1;
1927 for (i = 0; i < vmw_priv->num_displays; ++i) {
1928 save = &vmw_priv->vga_save[i];
1929 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
1930 save->primary = vmw_read(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY);
1931 save->pos_x = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_X);
1932 save->pos_y = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y);
1933 save->width = vmw_read(vmw_priv, SVGA_REG_DISPLAY_WIDTH);
1934 save->height = vmw_read(vmw_priv, SVGA_REG_DISPLAY_HEIGHT);
1935 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
1936 if (i == 0 && vmw_priv->num_displays == 1 &&
1937 save->width == 0 && save->height == 0) {
1940 * It should be fairly safe to assume that these
1941 * values are uninitialized.
1944 save->width = vmw_priv->vga_width - save->pos_x;
1945 save->height = vmw_priv->vga_height - save->pos_y;
1952 int vmw_kms_restore_vga(struct vmw_private *vmw_priv)
1954 struct vmw_vga_topology_state *save;
1957 vmw_write(vmw_priv, SVGA_REG_WIDTH, vmw_priv->vga_width);
1958 vmw_write(vmw_priv, SVGA_REG_HEIGHT, vmw_priv->vga_height);
1959 vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, vmw_priv->vga_bpp);
1960 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1961 vmw_write(vmw_priv, SVGA_REG_PITCHLOCK,
1962 vmw_priv->vga_pitchlock);
1963 else if (vmw_fifo_have_pitchlock(vmw_priv))
1964 vmw_mmio_write(vmw_priv->vga_pitchlock,
1965 vmw_priv->mmio_virt + SVGA_FIFO_PITCHLOCK);
1967 if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
1970 for (i = 0; i < vmw_priv->num_displays; ++i) {
1971 save = &vmw_priv->vga_save[i];
1972 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
1973 vmw_write(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY, save->primary);
1974 vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_X, save->pos_x);
1975 vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y, save->pos_y);
1976 vmw_write(vmw_priv, SVGA_REG_DISPLAY_WIDTH, save->width);
1977 vmw_write(vmw_priv, SVGA_REG_DISPLAY_HEIGHT, save->height);
1978 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
1984 bool vmw_kms_validate_mode_vram(struct vmw_private *dev_priv,
1988 return ((u64) pitch * (u64) height) < (u64)
1989 ((dev_priv->active_display_unit == vmw_du_screen_target) ?
1990 dev_priv->prim_bb_mem : dev_priv->vram_size);
1995 * Function called by DRM code called with vbl_lock held.
1997 u32 vmw_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
2003 * Function called by DRM code called with vbl_lock held.
2005 int vmw_enable_vblank(struct drm_device *dev, unsigned int pipe)
2011 * Function called by DRM code called with vbl_lock held.
2013 void vmw_disable_vblank(struct drm_device *dev, unsigned int pipe)
2018 * vmw_du_update_layout - Update the display unit with topology from resolution
2019 * plugin and generate DRM uevent
2020 * @dev_priv: device private
2021 * @num_rects: number of drm_rect in rects
2022 * @rects: toplogy to update
2024 static int vmw_du_update_layout(struct vmw_private *dev_priv,
2025 unsigned int num_rects, struct drm_rect *rects)
2027 struct drm_device *dev = dev_priv->dev;
2028 struct vmw_display_unit *du;
2029 struct drm_connector *con;
2030 struct drm_connector_list_iter conn_iter;
2031 struct drm_modeset_acquire_ctx ctx;
2032 struct drm_crtc *crtc;
2035 /* Currently gui_x/y is protected with the crtc mutex */
2036 mutex_lock(&dev->mode_config.mutex);
2037 drm_modeset_acquire_init(&ctx, 0);
2039 drm_for_each_crtc(crtc, dev) {
2040 ret = drm_modeset_lock(&crtc->mutex, &ctx);
2042 if (ret == -EDEADLK) {
2043 drm_modeset_backoff(&ctx);
2050 drm_connector_list_iter_begin(dev, &conn_iter);
2051 drm_for_each_connector_iter(con, &conn_iter) {
2052 du = vmw_connector_to_du(con);
2053 if (num_rects > du->unit) {
2054 du->pref_width = drm_rect_width(&rects[du->unit]);
2055 du->pref_height = drm_rect_height(&rects[du->unit]);
2056 du->pref_active = true;
2057 du->gui_x = rects[du->unit].x1;
2058 du->gui_y = rects[du->unit].y1;
2060 du->pref_width = 800;
2061 du->pref_height = 600;
2062 du->pref_active = false;
2067 drm_connector_list_iter_end(&conn_iter);
2069 list_for_each_entry(con, &dev->mode_config.connector_list, head) {
2070 du = vmw_connector_to_du(con);
2071 if (num_rects > du->unit) {
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 drm_object_property_set_value
2080 (&con->base, dev->mode_config.suggested_x_property,
2082 drm_object_property_set_value
2083 (&con->base, dev->mode_config.suggested_y_property,
2086 con->status = vmw_du_connector_detect(con, true);
2089 drm_sysfs_hotplug_event(dev);
2091 drm_modeset_drop_locks(&ctx);
2092 drm_modeset_acquire_fini(&ctx);
2093 mutex_unlock(&dev->mode_config.mutex);
2098 int vmw_du_crtc_gamma_set(struct drm_crtc *crtc,
2099 u16 *r, u16 *g, u16 *b,
2101 struct drm_modeset_acquire_ctx *ctx)
2103 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
2106 for (i = 0; i < size; i++) {
2107 DRM_DEBUG("%d r/g/b = 0x%04x / 0x%04x / 0x%04x\n", i,
2109 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 0, r[i] >> 8);
2110 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 1, g[i] >> 8);
2111 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 2, b[i] >> 8);
2117 int vmw_du_connector_dpms(struct drm_connector *connector, int mode)
2122 enum drm_connector_status
2123 vmw_du_connector_detect(struct drm_connector *connector, bool force)
2125 uint32_t num_displays;
2126 struct drm_device *dev = connector->dev;
2127 struct vmw_private *dev_priv = vmw_priv(dev);
2128 struct vmw_display_unit *du = vmw_connector_to_du(connector);
2130 num_displays = vmw_read(dev_priv, SVGA_REG_NUM_DISPLAYS);
2132 return ((vmw_connector_to_du(connector)->unit < num_displays &&
2134 connector_status_connected : connector_status_disconnected);
2137 static struct drm_display_mode vmw_kms_connector_builtin[] = {
2139 { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 25175, 640, 656,
2140 752, 800, 0, 480, 489, 492, 525, 0,
2141 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
2143 { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 40000, 800, 840,
2144 968, 1056, 0, 600, 601, 605, 628, 0,
2145 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
2147 { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 65000, 1024, 1048,
2148 1184, 1344, 0, 768, 771, 777, 806, 0,
2149 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
2151 { DRM_MODE("1152x864", DRM_MODE_TYPE_DRIVER, 108000, 1152, 1216,
2152 1344, 1600, 0, 864, 865, 868, 900, 0,
2153 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
2155 { DRM_MODE("1280x768", DRM_MODE_TYPE_DRIVER, 79500, 1280, 1344,
2156 1472, 1664, 0, 768, 771, 778, 798, 0,
2157 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2159 { DRM_MODE("1280x800", DRM_MODE_TYPE_DRIVER, 83500, 1280, 1352,
2160 1480, 1680, 0, 800, 803, 809, 831, 0,
2161 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
2163 { DRM_MODE("1280x960", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1376,
2164 1488, 1800, 0, 960, 961, 964, 1000, 0,
2165 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
2166 /* 1280x1024@60Hz */
2167 { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1328,
2168 1440, 1688, 0, 1024, 1025, 1028, 1066, 0,
2169 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
2171 { DRM_MODE("1360x768", DRM_MODE_TYPE_DRIVER, 85500, 1360, 1424,
2172 1536, 1792, 0, 768, 771, 777, 795, 0,
2173 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
2174 /* 1440x1050@60Hz */
2175 { DRM_MODE("1400x1050", DRM_MODE_TYPE_DRIVER, 121750, 1400, 1488,
2176 1632, 1864, 0, 1050, 1053, 1057, 1089, 0,
2177 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2179 { DRM_MODE("1440x900", DRM_MODE_TYPE_DRIVER, 106500, 1440, 1520,
2180 1672, 1904, 0, 900, 903, 909, 934, 0,
2181 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2182 /* 1600x1200@60Hz */
2183 { DRM_MODE("1600x1200", DRM_MODE_TYPE_DRIVER, 162000, 1600, 1664,
2184 1856, 2160, 0, 1200, 1201, 1204, 1250, 0,
2185 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
2186 /* 1680x1050@60Hz */
2187 { DRM_MODE("1680x1050", DRM_MODE_TYPE_DRIVER, 146250, 1680, 1784,
2188 1960, 2240, 0, 1050, 1053, 1059, 1089, 0,
2189 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2190 /* 1792x1344@60Hz */
2191 { DRM_MODE("1792x1344", DRM_MODE_TYPE_DRIVER, 204750, 1792, 1920,
2192 2120, 2448, 0, 1344, 1345, 1348, 1394, 0,
2193 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2194 /* 1853x1392@60Hz */
2195 { DRM_MODE("1856x1392", DRM_MODE_TYPE_DRIVER, 218250, 1856, 1952,
2196 2176, 2528, 0, 1392, 1393, 1396, 1439, 0,
2197 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2198 /* 1920x1200@60Hz */
2199 { DRM_MODE("1920x1200", DRM_MODE_TYPE_DRIVER, 193250, 1920, 2056,
2200 2256, 2592, 0, 1200, 1203, 1209, 1245, 0,
2201 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2202 /* 1920x1440@60Hz */
2203 { DRM_MODE("1920x1440", DRM_MODE_TYPE_DRIVER, 234000, 1920, 2048,
2204 2256, 2600, 0, 1440, 1441, 1444, 1500, 0,
2205 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2206 /* 2560x1600@60Hz */
2207 { DRM_MODE("2560x1600", DRM_MODE_TYPE_DRIVER, 348500, 2560, 2752,
2208 3032, 3504, 0, 1600, 1603, 1609, 1658, 0,
2209 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2211 { DRM_MODE("", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) },
2215 * vmw_guess_mode_timing - Provide fake timings for a
2216 * 60Hz vrefresh mode.
2218 * @mode - Pointer to a struct drm_display_mode with hdisplay and vdisplay
2219 * members filled in.
2221 void vmw_guess_mode_timing(struct drm_display_mode *mode)
2223 mode->hsync_start = mode->hdisplay + 50;
2224 mode->hsync_end = mode->hsync_start + 50;
2225 mode->htotal = mode->hsync_end + 50;
2227 mode->vsync_start = mode->vdisplay + 50;
2228 mode->vsync_end = mode->vsync_start + 50;
2229 mode->vtotal = mode->vsync_end + 50;
2231 mode->clock = (u32)mode->htotal * (u32)mode->vtotal / 100 * 6;
2232 mode->vrefresh = drm_mode_vrefresh(mode);
2236 int vmw_du_connector_fill_modes(struct drm_connector *connector,
2237 uint32_t max_width, uint32_t max_height)
2239 struct vmw_display_unit *du = vmw_connector_to_du(connector);
2240 struct drm_device *dev = connector->dev;
2241 struct vmw_private *dev_priv = vmw_priv(dev);
2242 struct drm_display_mode *mode = NULL;
2243 struct drm_display_mode *bmode;
2244 struct drm_display_mode prefmode = { DRM_MODE("preferred",
2245 DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED,
2246 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2247 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC)
2250 u32 assumed_bpp = 4;
2252 if (dev_priv->assume_16bpp)
2255 max_width = min(max_width, dev_priv->texture_max_width);
2256 max_height = min(max_height, dev_priv->texture_max_height);
2259 * For STDU extra limit for a mode on SVGA_REG_SCREENTARGET_MAX_WIDTH/
2262 if (dev_priv->active_display_unit == vmw_du_screen_target) {
2263 max_width = min(max_width, dev_priv->stdu_max_width);
2264 max_height = min(max_height, dev_priv->stdu_max_height);
2267 /* Add preferred mode */
2268 mode = drm_mode_duplicate(dev, &prefmode);
2271 mode->hdisplay = du->pref_width;
2272 mode->vdisplay = du->pref_height;
2273 vmw_guess_mode_timing(mode);
2275 if (vmw_kms_validate_mode_vram(dev_priv,
2276 mode->hdisplay * assumed_bpp,
2278 drm_mode_probed_add(connector, mode);
2280 drm_mode_destroy(dev, mode);
2284 if (du->pref_mode) {
2285 list_del_init(&du->pref_mode->head);
2286 drm_mode_destroy(dev, du->pref_mode);
2289 /* mode might be null here, this is intended */
2290 du->pref_mode = mode;
2292 for (i = 0; vmw_kms_connector_builtin[i].type != 0; i++) {
2293 bmode = &vmw_kms_connector_builtin[i];
2294 if (bmode->hdisplay > max_width ||
2295 bmode->vdisplay > max_height)
2298 if (!vmw_kms_validate_mode_vram(dev_priv,
2299 bmode->hdisplay * assumed_bpp,
2303 mode = drm_mode_duplicate(dev, bmode);
2306 mode->vrefresh = drm_mode_vrefresh(mode);
2308 drm_mode_probed_add(connector, mode);
2311 drm_connector_list_update(connector);
2312 /* Move the prefered mode first, help apps pick the right mode. */
2313 drm_mode_sort(&connector->modes);
2319 * vmw_kms_update_layout_ioctl - Handler for DRM_VMW_UPDATE_LAYOUT ioctl
2320 * @dev: drm device for the ioctl
2321 * @data: data pointer for the ioctl
2322 * @file_priv: drm file for the ioctl call
2324 * Update preferred topology of display unit as per ioctl request. The topology
2325 * is expressed as array of drm_vmw_rect.
2327 * [0 0 640 480] [640 0 800 600] [0 480 640 480]
2330 * The x and y offset (upper left) in drm_vmw_rect cannot be less than 0. Beside
2331 * device limit on topology, x + w and y + h (lower right) cannot be greater
2332 * than INT_MAX. So topology beyond these limits will return with error.
2335 * Zero on success, negative errno on failure.
2337 int vmw_kms_update_layout_ioctl(struct drm_device *dev, void *data,
2338 struct drm_file *file_priv)
2340 struct vmw_private *dev_priv = vmw_priv(dev);
2341 struct drm_mode_config *mode_config = &dev->mode_config;
2342 struct drm_vmw_update_layout_arg *arg =
2343 (struct drm_vmw_update_layout_arg *)data;
2344 void __user *user_rects;
2345 struct drm_vmw_rect *rects;
2346 struct drm_rect *drm_rects;
2347 unsigned rects_size;
2350 if (!arg->num_outputs) {
2351 struct drm_rect def_rect = {0, 0, 800, 600};
2352 vmw_du_update_layout(dev_priv, 1, &def_rect);
2356 rects_size = arg->num_outputs * sizeof(struct drm_vmw_rect);
2357 rects = kcalloc(arg->num_outputs, sizeof(struct drm_vmw_rect),
2359 if (unlikely(!rects))
2362 user_rects = (void __user *)(unsigned long)arg->rects;
2363 ret = copy_from_user(rects, user_rects, rects_size);
2364 if (unlikely(ret != 0)) {
2365 DRM_ERROR("Failed to get rects.\n");
2370 drm_rects = (struct drm_rect *)rects;
2372 for (i = 0; i < arg->num_outputs; i++) {
2373 struct drm_vmw_rect curr_rect;
2375 /* Verify user-space for overflow as kernel use drm_rect */
2376 if ((rects[i].x + rects[i].w > INT_MAX) ||
2377 (rects[i].y + rects[i].h > INT_MAX)) {
2382 curr_rect = rects[i];
2383 drm_rects[i].x1 = curr_rect.x;
2384 drm_rects[i].y1 = curr_rect.y;
2385 drm_rects[i].x2 = curr_rect.x + curr_rect.w;
2386 drm_rects[i].y2 = curr_rect.y + curr_rect.h;
2389 * Currently this check is limiting the topology within
2390 * mode_config->max (which actually is max texture size
2391 * supported by virtual device). This limit is here to address
2392 * window managers that create a big framebuffer for whole
2395 if (drm_rects[i].x1 < 0 || drm_rects[i].y1 < 0 ||
2396 drm_rects[i].x2 > mode_config->max_width ||
2397 drm_rects[i].y2 > mode_config->max_height) {
2398 DRM_ERROR("Invalid GUI layout.\n");
2404 ret = vmw_kms_check_display_memory(dev, arg->num_outputs, drm_rects);
2407 vmw_du_update_layout(dev_priv, arg->num_outputs, drm_rects);
2415 * vmw_kms_helper_dirty - Helper to build commands and perform actions based
2416 * on a set of cliprects and a set of display units.
2418 * @dev_priv: Pointer to a device private structure.
2419 * @framebuffer: Pointer to the framebuffer on which to perform the actions.
2420 * @clips: A set of struct drm_clip_rect. Either this os @vclips must be NULL.
2421 * Cliprects are given in framebuffer coordinates.
2422 * @vclips: A set of struct drm_vmw_rect cliprects. Either this or @clips must
2423 * be NULL. Cliprects are given in source coordinates.
2424 * @dest_x: X coordinate offset for the crtc / destination clip rects.
2425 * @dest_y: Y coordinate offset for the crtc / destination clip rects.
2426 * @num_clips: Number of cliprects in the @clips or @vclips array.
2427 * @increment: Integer with which to increment the clip counter when looping.
2428 * Used to skip a predetermined number of clip rects.
2429 * @dirty: Closure structure. See the description of struct vmw_kms_dirty.
2431 int vmw_kms_helper_dirty(struct vmw_private *dev_priv,
2432 struct vmw_framebuffer *framebuffer,
2433 const struct drm_clip_rect *clips,
2434 const struct drm_vmw_rect *vclips,
2435 s32 dest_x, s32 dest_y,
2438 struct vmw_kms_dirty *dirty)
2440 struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
2441 struct drm_crtc *crtc;
2445 dirty->dev_priv = dev_priv;
2447 /* If crtc is passed, no need to iterate over other display units */
2449 units[num_units++] = vmw_crtc_to_du(dirty->crtc);
2451 list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list,
2453 struct drm_plane *plane = crtc->primary;
2455 if (plane->state->fb == &framebuffer->base)
2456 units[num_units++] = vmw_crtc_to_du(crtc);
2460 for (k = 0; k < num_units; k++) {
2461 struct vmw_display_unit *unit = units[k];
2462 s32 crtc_x = unit->crtc.x;
2463 s32 crtc_y = unit->crtc.y;
2464 s32 crtc_width = unit->crtc.mode.hdisplay;
2465 s32 crtc_height = unit->crtc.mode.vdisplay;
2466 const struct drm_clip_rect *clips_ptr = clips;
2467 const struct drm_vmw_rect *vclips_ptr = vclips;
2470 if (dirty->fifo_reserve_size > 0) {
2471 dirty->cmd = vmw_fifo_reserve(dev_priv,
2472 dirty->fifo_reserve_size);
2474 DRM_ERROR("Couldn't reserve fifo space "
2475 "for dirty blits.\n");
2478 memset(dirty->cmd, 0, dirty->fifo_reserve_size);
2480 dirty->num_hits = 0;
2481 for (i = 0; i < num_clips; i++, clips_ptr += increment,
2482 vclips_ptr += increment) {
2487 * Select clip array type. Note that integer type
2488 * in @clips is unsigned short, whereas in @vclips
2492 dirty->fb_x = (s32) clips_ptr->x1;
2493 dirty->fb_y = (s32) clips_ptr->y1;
2494 dirty->unit_x2 = (s32) clips_ptr->x2 + dest_x -
2496 dirty->unit_y2 = (s32) clips_ptr->y2 + dest_y -
2499 dirty->fb_x = vclips_ptr->x;
2500 dirty->fb_y = vclips_ptr->y;
2501 dirty->unit_x2 = dirty->fb_x + vclips_ptr->w +
2503 dirty->unit_y2 = dirty->fb_y + vclips_ptr->h +
2507 dirty->unit_x1 = dirty->fb_x + dest_x - crtc_x;
2508 dirty->unit_y1 = dirty->fb_y + dest_y - crtc_y;
2510 /* Skip this clip if it's outside the crtc region */
2511 if (dirty->unit_x1 >= crtc_width ||
2512 dirty->unit_y1 >= crtc_height ||
2513 dirty->unit_x2 <= 0 || dirty->unit_y2 <= 0)
2516 /* Clip right and bottom to crtc limits */
2517 dirty->unit_x2 = min_t(s32, dirty->unit_x2,
2519 dirty->unit_y2 = min_t(s32, dirty->unit_y2,
2522 /* Clip left and top to crtc limits */
2523 clip_left = min_t(s32, dirty->unit_x1, 0);
2524 clip_top = min_t(s32, dirty->unit_y1, 0);
2525 dirty->unit_x1 -= clip_left;
2526 dirty->unit_y1 -= clip_top;
2527 dirty->fb_x -= clip_left;
2528 dirty->fb_y -= clip_top;
2533 dirty->fifo_commit(dirty);
2540 * vmw_kms_helper_validation_finish - Helper for post KMS command submission
2541 * cleanup and fencing
2542 * @dev_priv: Pointer to the device-private struct
2543 * @file_priv: Pointer identifying the client when user-space fencing is used
2544 * @ctx: Pointer to the validation context
2545 * @out_fence: If non-NULL, returned refcounted fence-pointer
2546 * @user_fence_rep: If non-NULL, pointer to user-space address area
2547 * in which to copy user-space fence info
2549 void vmw_kms_helper_validation_finish(struct vmw_private *dev_priv,
2550 struct drm_file *file_priv,
2551 struct vmw_validation_context *ctx,
2552 struct vmw_fence_obj **out_fence,
2553 struct drm_vmw_fence_rep __user *
2556 struct vmw_fence_obj *fence = NULL;
2557 uint32_t handle = 0;
2560 if (file_priv || user_fence_rep || vmw_validation_has_bos(ctx) ||
2562 ret = vmw_execbuf_fence_commands(file_priv, dev_priv, &fence,
2563 file_priv ? &handle : NULL);
2564 vmw_validation_done(ctx, fence);
2566 vmw_execbuf_copy_fence_user(dev_priv, vmw_fpriv(file_priv),
2567 ret, user_fence_rep, fence,
2572 vmw_fence_obj_unreference(&fence);
2576 * vmw_kms_update_proxy - Helper function to update a proxy surface from
2579 * @res: Pointer to the surface resource
2580 * @clips: Clip rects in framebuffer (surface) space.
2581 * @num_clips: Number of clips in @clips.
2582 * @increment: Integer with which to increment the clip counter when looping.
2583 * Used to skip a predetermined number of clip rects.
2585 * This function makes sure the proxy surface is updated from its backing MOB
2586 * using the region given by @clips. The surface resource @res and its backing
2587 * MOB needs to be reserved and validated on call.
2589 int vmw_kms_update_proxy(struct vmw_resource *res,
2590 const struct drm_clip_rect *clips,
2594 struct vmw_private *dev_priv = res->dev_priv;
2595 struct drm_vmw_size *size = &vmw_res_to_srf(res)->base_size;
2597 SVGA3dCmdHeader header;
2598 SVGA3dCmdUpdateGBImage body;
2601 size_t copy_size = 0;
2607 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd) * num_clips);
2609 DRM_ERROR("Couldn't reserve fifo space for proxy surface "
2614 for (i = 0; i < num_clips; ++i, clips += increment, ++cmd) {
2615 box = &cmd->body.box;
2617 cmd->header.id = SVGA_3D_CMD_UPDATE_GB_IMAGE;
2618 cmd->header.size = sizeof(cmd->body);
2619 cmd->body.image.sid = res->id;
2620 cmd->body.image.face = 0;
2621 cmd->body.image.mipmap = 0;
2623 if (clips->x1 > size->width || clips->x2 > size->width ||
2624 clips->y1 > size->height || clips->y2 > size->height) {
2625 DRM_ERROR("Invalid clips outsize of framebuffer.\n");
2632 box->w = clips->x2 - clips->x1;
2633 box->h = clips->y2 - clips->y1;
2636 copy_size += sizeof(*cmd);
2639 vmw_fifo_commit(dev_priv, copy_size);
2644 int vmw_kms_fbdev_init_data(struct vmw_private *dev_priv,
2648 struct drm_connector **p_con,
2649 struct drm_crtc **p_crtc,
2650 struct drm_display_mode **p_mode)
2652 struct drm_connector *con;
2653 struct vmw_display_unit *du;
2654 struct drm_display_mode *mode;
2658 mutex_lock(&dev_priv->dev->mode_config.mutex);
2659 list_for_each_entry(con, &dev_priv->dev->mode_config.connector_list,
2668 DRM_ERROR("Could not find initial display unit.\n");
2673 if (list_empty(&con->modes))
2674 (void) vmw_du_connector_fill_modes(con, max_width, max_height);
2676 if (list_empty(&con->modes)) {
2677 DRM_ERROR("Could not find initial display mode.\n");
2682 du = vmw_connector_to_du(con);
2684 *p_crtc = &du->crtc;
2686 list_for_each_entry(mode, &con->modes, head) {
2687 if (mode->type & DRM_MODE_TYPE_PREFERRED)
2691 if (mode->type & DRM_MODE_TYPE_PREFERRED)
2694 WARN_ONCE(true, "Could not find initial preferred mode.\n");
2695 *p_mode = list_first_entry(&con->modes,
2696 struct drm_display_mode,
2701 mutex_unlock(&dev_priv->dev->mode_config.mutex);
2707 * vmw_kms_create_implicit_placement_proparty - Set up the implicit placement
2710 * @dev_priv: Pointer to a device private struct.
2712 * Sets up the implicit placement property unless it's already set up.
2715 vmw_kms_create_implicit_placement_property(struct vmw_private *dev_priv)
2717 if (dev_priv->implicit_placement_property)
2720 dev_priv->implicit_placement_property =
2721 drm_property_create_range(dev_priv->dev,
2722 DRM_MODE_PROP_IMMUTABLE,
2723 "implicit_placement", 0, 1);
2727 * vmw_kms_suspend - Save modesetting state and turn modesetting off.
2729 * @dev: Pointer to the drm device
2730 * Return: 0 on success. Negative error code on failure.
2732 int vmw_kms_suspend(struct drm_device *dev)
2734 struct vmw_private *dev_priv = vmw_priv(dev);
2736 dev_priv->suspend_state = drm_atomic_helper_suspend(dev);
2737 if (IS_ERR(dev_priv->suspend_state)) {
2738 int ret = PTR_ERR(dev_priv->suspend_state);
2740 DRM_ERROR("Failed kms suspend: %d\n", ret);
2741 dev_priv->suspend_state = NULL;
2751 * vmw_kms_resume - Re-enable modesetting and restore state
2753 * @dev: Pointer to the drm device
2754 * Return: 0 on success. Negative error code on failure.
2756 * State is resumed from a previous vmw_kms_suspend(). It's illegal
2757 * to call this function without a previous vmw_kms_suspend().
2759 int vmw_kms_resume(struct drm_device *dev)
2761 struct vmw_private *dev_priv = vmw_priv(dev);
2764 if (WARN_ON(!dev_priv->suspend_state))
2767 ret = drm_atomic_helper_resume(dev, dev_priv->suspend_state);
2768 dev_priv->suspend_state = NULL;
2774 * vmw_kms_lost_device - Notify kms that modesetting capabilities will be lost
2776 * @dev: Pointer to the drm device
2778 void vmw_kms_lost_device(struct drm_device *dev)
2780 drm_atomic_helper_shutdown(dev);
2784 * vmw_du_helper_plane_update - Helper to do plane update on a display unit.
2785 * @update: The closure structure.
2787 * Call this helper after setting callbacks in &vmw_du_update_plane to do plane
2788 * update on display unit.
2790 * Return: 0 on success or a negative error code on failure.
2792 int vmw_du_helper_plane_update(struct vmw_du_update_plane *update)
2794 struct drm_plane_state *state = update->plane->state;
2795 struct drm_plane_state *old_state = update->old_state;
2796 struct drm_atomic_helper_damage_iter iter;
2797 struct drm_rect clip;
2799 DECLARE_VAL_CONTEXT(val_ctx, NULL, 0);
2800 uint32_t reserved_size = 0;
2801 uint32_t submit_size = 0;
2802 uint32_t curr_size = 0;
2803 uint32_t num_hits = 0;
2809 * Iterate in advance to check if really need plane update and find the
2810 * number of clips that actually are in plane src for fifo allocation.
2812 drm_atomic_helper_damage_iter_init(&iter, old_state, state);
2813 drm_atomic_for_each_plane_damage(&iter, &clip)
2819 if (update->vfb->bo) {
2820 struct vmw_framebuffer_bo *vfbbo =
2821 container_of(update->vfb, typeof(*vfbbo), base);
2823 ret = vmw_validation_add_bo(&val_ctx, vfbbo->buffer, false,
2826 struct vmw_framebuffer_surface *vfbs =
2827 container_of(update->vfb, typeof(*vfbs), base);
2829 ret = vmw_validation_add_resource(&val_ctx, &vfbs->surface->res,
2836 ret = vmw_validation_prepare(&val_ctx, update->mutex, update->intr);
2840 reserved_size = update->calc_fifo_size(update, num_hits);
2841 cmd_start = vmw_fifo_reserve(update->dev_priv, reserved_size);
2847 cmd_next = cmd_start;
2849 if (update->post_prepare) {
2850 curr_size = update->post_prepare(update, cmd_next);
2851 cmd_next += curr_size;
2852 submit_size += curr_size;
2855 if (update->pre_clip) {
2856 curr_size = update->pre_clip(update, cmd_next, num_hits);
2857 cmd_next += curr_size;
2858 submit_size += curr_size;
2866 drm_atomic_helper_damage_iter_init(&iter, old_state, state);
2867 drm_atomic_for_each_plane_damage(&iter, &clip) {
2868 uint32_t fb_x = clip.x1;
2869 uint32_t fb_y = clip.y1;
2871 vmw_du_translate_to_crtc(state, &clip);
2873 curr_size = update->clip(update, cmd_next, &clip, fb_x,
2875 cmd_next += curr_size;
2876 submit_size += curr_size;
2878 bb.x1 = min_t(int, bb.x1, clip.x1);
2879 bb.y1 = min_t(int, bb.y1, clip.y1);
2880 bb.x2 = max_t(int, bb.x2, clip.x2);
2881 bb.y2 = max_t(int, bb.y2, clip.y2);
2884 curr_size = update->post_clip(update, cmd_next, &bb);
2885 submit_size += curr_size;
2887 if (reserved_size < submit_size)
2890 vmw_fifo_commit(update->dev_priv, submit_size);
2892 vmw_kms_helper_validation_finish(update->dev_priv, NULL, &val_ctx,
2893 update->out_fence, NULL);
2897 vmw_validation_revert(&val_ctx);
2900 vmw_validation_unref_lists(&val_ctx);