1 /**************************************************************************
3 * Copyright © 2009-2015 VMware, Inc., Palo Alto, CA., USA
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 * USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
28 #include "vmwgfx_kms.h"
29 #include <drm/drm_plane_helper.h>
30 #include <drm/drm_atomic.h>
31 #include <drm/drm_atomic_helper.h>
32 #include <drm/drm_rect.h>
34 /* Might need a hrtimer here? */
35 #define VMWGFX_PRESENT_RATE ((HZ / 60 > 0) ? HZ / 60 : 1)
37 void vmw_du_cleanup(struct vmw_display_unit *du)
39 drm_plane_cleanup(&du->primary);
40 drm_plane_cleanup(&du->cursor);
42 drm_connector_unregister(&du->connector);
43 drm_crtc_cleanup(&du->crtc);
44 drm_encoder_cleanup(&du->encoder);
45 drm_connector_cleanup(&du->connector);
49 * Display Unit Cursor functions
52 static int vmw_cursor_update_image(struct vmw_private *dev_priv,
53 u32 *image, u32 width, u32 height,
54 u32 hotspotX, u32 hotspotY)
58 SVGAFifoCmdDefineAlphaCursor cursor;
60 u32 image_size = width * height * 4;
61 u32 cmd_size = sizeof(*cmd) + image_size;
66 cmd = vmw_fifo_reserve(dev_priv, cmd_size);
67 if (unlikely(cmd == NULL)) {
68 DRM_ERROR("Fifo reserve failed.\n");
72 memset(cmd, 0, sizeof(*cmd));
74 memcpy(&cmd[1], image, image_size);
76 cmd->cmd = SVGA_CMD_DEFINE_ALPHA_CURSOR;
78 cmd->cursor.width = width;
79 cmd->cursor.height = height;
80 cmd->cursor.hotspotX = hotspotX;
81 cmd->cursor.hotspotY = hotspotY;
83 vmw_fifo_commit_flush(dev_priv, cmd_size);
88 static int vmw_cursor_update_dmabuf(struct vmw_private *dev_priv,
89 struct vmw_dma_buffer *dmabuf,
90 u32 width, u32 height,
91 u32 hotspotX, u32 hotspotY)
93 struct ttm_bo_kmap_obj map;
94 unsigned long kmap_offset;
95 unsigned long kmap_num;
101 kmap_num = (width*height*4 + PAGE_SIZE - 1) >> PAGE_SHIFT;
103 ret = ttm_bo_reserve(&dmabuf->base, true, false, NULL);
104 if (unlikely(ret != 0)) {
105 DRM_ERROR("reserve failed\n");
109 ret = ttm_bo_kmap(&dmabuf->base, kmap_offset, kmap_num, &map);
110 if (unlikely(ret != 0))
113 virtual = ttm_kmap_obj_virtual(&map, &dummy);
114 ret = vmw_cursor_update_image(dev_priv, virtual, width, height,
119 ttm_bo_unreserve(&dmabuf->base);
125 static void vmw_cursor_update_position(struct vmw_private *dev_priv,
126 bool show, int x, int y)
128 u32 *fifo_mem = dev_priv->mmio_virt;
131 spin_lock(&dev_priv->cursor_lock);
132 vmw_mmio_write(show ? 1 : 0, fifo_mem + SVGA_FIFO_CURSOR_ON);
133 vmw_mmio_write(x, fifo_mem + SVGA_FIFO_CURSOR_X);
134 vmw_mmio_write(y, fifo_mem + SVGA_FIFO_CURSOR_Y);
135 count = vmw_mmio_read(fifo_mem + SVGA_FIFO_CURSOR_COUNT);
136 vmw_mmio_write(++count, fifo_mem + SVGA_FIFO_CURSOR_COUNT);
137 spin_unlock(&dev_priv->cursor_lock);
141 void vmw_kms_cursor_snoop(struct vmw_surface *srf,
142 struct ttm_object_file *tfile,
143 struct ttm_buffer_object *bo,
144 SVGA3dCmdHeader *header)
146 struct ttm_bo_kmap_obj map;
147 unsigned long kmap_offset;
148 unsigned long kmap_num;
154 SVGA3dCmdHeader header;
155 SVGA3dCmdSurfaceDMA dma;
159 cmd = container_of(header, struct vmw_dma_cmd, header);
161 /* No snooper installed */
162 if (!srf->snooper.image)
165 if (cmd->dma.host.face != 0 || cmd->dma.host.mipmap != 0) {
166 DRM_ERROR("face and mipmap for cursors should never != 0\n");
170 if (cmd->header.size < 64) {
171 DRM_ERROR("at least one full copy box must be given\n");
175 box = (SVGA3dCopyBox *)&cmd[1];
176 box_count = (cmd->header.size - sizeof(SVGA3dCmdSurfaceDMA)) /
177 sizeof(SVGA3dCopyBox);
179 if (cmd->dma.guest.ptr.offset % PAGE_SIZE ||
180 box->x != 0 || box->y != 0 || box->z != 0 ||
181 box->srcx != 0 || box->srcy != 0 || box->srcz != 0 ||
182 box->d != 1 || box_count != 1) {
183 /* TODO handle none page aligned offsets */
184 /* TODO handle more dst & src != 0 */
185 /* TODO handle more then one copy */
186 DRM_ERROR("Cant snoop dma request for cursor!\n");
187 DRM_ERROR("(%u, %u, %u) (%u, %u, %u) (%ux%ux%u) %u %u\n",
188 box->srcx, box->srcy, box->srcz,
189 box->x, box->y, box->z,
190 box->w, box->h, box->d, box_count,
191 cmd->dma.guest.ptr.offset);
195 kmap_offset = cmd->dma.guest.ptr.offset >> PAGE_SHIFT;
196 kmap_num = (64*64*4) >> PAGE_SHIFT;
198 ret = ttm_bo_reserve(bo, true, false, NULL);
199 if (unlikely(ret != 0)) {
200 DRM_ERROR("reserve failed\n");
204 ret = ttm_bo_kmap(bo, kmap_offset, kmap_num, &map);
205 if (unlikely(ret != 0))
208 virtual = ttm_kmap_obj_virtual(&map, &dummy);
210 if (box->w == 64 && cmd->dma.guest.pitch == 64*4) {
211 memcpy(srf->snooper.image, virtual, 64*64*4);
213 /* Image is unsigned pointer. */
214 for (i = 0; i < box->h; i++)
215 memcpy(srf->snooper.image + i * 64,
216 virtual + i * cmd->dma.guest.pitch,
224 ttm_bo_unreserve(bo);
228 * vmw_kms_legacy_hotspot_clear - Clear legacy hotspots
230 * @dev_priv: Pointer to the device private struct.
232 * Clears all legacy hotspots.
234 void vmw_kms_legacy_hotspot_clear(struct vmw_private *dev_priv)
236 struct drm_device *dev = dev_priv->dev;
237 struct vmw_display_unit *du;
238 struct drm_crtc *crtc;
240 drm_modeset_lock_all(dev);
241 drm_for_each_crtc(crtc, dev) {
242 du = vmw_crtc_to_du(crtc);
247 drm_modeset_unlock_all(dev);
250 void vmw_kms_cursor_post_execbuf(struct vmw_private *dev_priv)
252 struct drm_device *dev = dev_priv->dev;
253 struct vmw_display_unit *du;
254 struct drm_crtc *crtc;
256 mutex_lock(&dev->mode_config.mutex);
258 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
259 du = vmw_crtc_to_du(crtc);
260 if (!du->cursor_surface ||
261 du->cursor_age == du->cursor_surface->snooper.age)
264 du->cursor_age = du->cursor_surface->snooper.age;
265 vmw_cursor_update_image(dev_priv,
266 du->cursor_surface->snooper.image,
268 du->hotspot_x + du->core_hotspot_x,
269 du->hotspot_y + du->core_hotspot_y);
272 mutex_unlock(&dev->mode_config.mutex);
276 void vmw_du_cursor_plane_destroy(struct drm_plane *plane)
278 vmw_cursor_update_position(plane->dev->dev_private, false, 0, 0);
280 drm_plane_cleanup(plane);
284 void vmw_du_primary_plane_destroy(struct drm_plane *plane)
286 drm_plane_cleanup(plane);
288 /* Planes are static in our case so we don't free it */
293 * vmw_du_vps_unpin_surf - unpins resource associated with a framebuffer surface
295 * @vps: plane state associated with the display surface
296 * @unreference: true if we also want to unreference the display.
298 void vmw_du_plane_unpin_surf(struct vmw_plane_state *vps,
303 vmw_resource_unpin(&vps->surf->res);
309 DRM_ERROR("Surface still pinned\n");
310 vmw_surface_unreference(&vps->surf);
317 * vmw_du_plane_cleanup_fb - Unpins the cursor
319 * @plane: display plane
320 * @old_state: Contains the FB to clean up
322 * Unpins the framebuffer surface
324 * Returns 0 on success
327 vmw_du_plane_cleanup_fb(struct drm_plane *plane,
328 struct drm_plane_state *old_state)
330 struct vmw_plane_state *vps = vmw_plane_state_to_vps(old_state);
332 vmw_du_plane_unpin_surf(vps, false);
337 * vmw_du_cursor_plane_prepare_fb - Readies the cursor by referencing it
339 * @plane: display plane
340 * @new_state: info on the new plane state, including the FB
342 * Returns 0 on success
345 vmw_du_cursor_plane_prepare_fb(struct drm_plane *plane,
346 struct drm_plane_state *new_state)
348 struct drm_framebuffer *fb = new_state->fb;
349 struct vmw_plane_state *vps = vmw_plane_state_to_vps(new_state);
353 vmw_surface_unreference(&vps->surf);
356 vmw_dmabuf_unreference(&vps->dmabuf);
359 if (vmw_framebuffer_to_vfb(fb)->dmabuf) {
360 vps->dmabuf = vmw_framebuffer_to_vfbd(fb)->buffer;
361 vmw_dmabuf_reference(vps->dmabuf);
363 vps->surf = vmw_framebuffer_to_vfbs(fb)->surface;
364 vmw_surface_reference(vps->surf);
373 vmw_du_cursor_plane_atomic_update(struct drm_plane *plane,
374 struct drm_plane_state *old_state)
376 struct drm_crtc *crtc = plane->state->crtc ?: old_state->crtc;
377 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
378 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
379 struct vmw_plane_state *vps = vmw_plane_state_to_vps(plane->state);
380 s32 hotspot_x, hotspot_y;
384 hotspot_x = du->hotspot_x;
385 hotspot_y = du->hotspot_y;
388 hotspot_x += plane->fb->hot_x;
389 hotspot_y += plane->fb->hot_y;
392 du->cursor_surface = vps->surf;
393 du->cursor_dmabuf = vps->dmabuf;
395 /* setup new image */
397 du->cursor_age = du->cursor_surface->snooper.age;
399 ret = vmw_cursor_update_image(dev_priv,
400 vps->surf->snooper.image,
401 64, 64, hotspot_x, hotspot_y);
402 } else if (vps->dmabuf) {
403 ret = vmw_cursor_update_dmabuf(dev_priv, vps->dmabuf,
404 plane->state->crtc_w,
405 plane->state->crtc_h,
406 hotspot_x, hotspot_y);
408 vmw_cursor_update_position(dev_priv, false, 0, 0);
413 du->cursor_x = plane->state->crtc_x + du->set_gui_x;
414 du->cursor_y = plane->state->crtc_y + du->set_gui_y;
416 vmw_cursor_update_position(dev_priv, true,
417 du->cursor_x + hotspot_x,
418 du->cursor_y + hotspot_y);
420 du->core_hotspot_x = hotspot_x - du->hotspot_x;
421 du->core_hotspot_y = hotspot_y - du->hotspot_y;
423 DRM_ERROR("Failed to update cursor image\n");
429 * vmw_du_primary_plane_atomic_check - check if the new state is okay
431 * @plane: display plane
432 * @state: info on the new plane state, including the FB
434 * Check if the new state is settable given the current state. Other
435 * than what the atomic helper checks, we care about crtc fitting
436 * the FB and maintaining one active framebuffer.
438 * Returns 0 on success
440 int vmw_du_primary_plane_atomic_check(struct drm_plane *plane,
441 struct drm_plane_state *state)
443 struct drm_framebuffer *new_fb = state->fb;
446 struct drm_rect src = {
449 .x2 = state->src_x + state->src_w,
450 .y2 = state->src_y + state->src_h,
452 struct drm_rect dest = {
455 .x2 = state->crtc_x + state->crtc_w,
456 .y2 = state->crtc_y + state->crtc_h,
458 struct drm_rect clip = dest;
461 ret = drm_plane_helper_check_update(plane, state->crtc, new_fb,
464 DRM_PLANE_HELPER_NO_SCALING,
465 DRM_PLANE_HELPER_NO_SCALING,
466 false, true, &visible);
469 if (!ret && new_fb) {
470 struct drm_crtc *crtc = state->crtc;
471 struct vmw_connector_state *vcs;
472 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
473 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
474 struct vmw_framebuffer *vfb = vmw_framebuffer_to_vfb(new_fb);
476 vcs = vmw_connector_state_to_vcs(du->connector.state);
478 if ((dest.x2 > new_fb->width ||
479 dest.y2 > new_fb->height)) {
480 DRM_ERROR("CRTC area outside of framebuffer\n");
484 /* Only one active implicit framebuffer at a time. */
485 mutex_lock(&dev_priv->global_kms_state_mutex);
486 if (vcs->is_implicit && dev_priv->implicit_fb &&
487 !(dev_priv->num_implicit == 1 && du->active_implicit)
488 && dev_priv->implicit_fb != vfb) {
489 DRM_ERROR("Multiple implicit framebuffers "
493 mutex_unlock(&dev_priv->global_kms_state_mutex);
502 * vmw_du_cursor_plane_atomic_check - check if the new state is okay
504 * @plane: cursor plane
505 * @state: info on the new plane state
507 * This is a chance to fail if the new cursor state does not fit
510 * Returns 0 on success
512 int vmw_du_cursor_plane_atomic_check(struct drm_plane *plane,
513 struct drm_plane_state *new_state)
516 struct vmw_surface *surface = NULL;
517 struct drm_framebuffer *fb = new_state->fb;
524 /* A lot of the code assumes this */
525 if (new_state->crtc_w != 64 || new_state->crtc_h != 64) {
526 DRM_ERROR("Invalid cursor dimensions (%d, %d)\n",
527 new_state->crtc_w, new_state->crtc_h);
531 if (!vmw_framebuffer_to_vfb(fb)->dmabuf)
532 surface = vmw_framebuffer_to_vfbs(fb)->surface;
534 if (surface && !surface->snooper.image) {
535 DRM_ERROR("surface not suitable for cursor\n");
543 int vmw_du_crtc_atomic_check(struct drm_crtc *crtc,
544 struct drm_crtc_state *new_state)
546 struct vmw_display_unit *du = vmw_crtc_to_du(new_state->crtc);
547 int connector_mask = 1 << drm_connector_index(&du->connector);
548 bool has_primary = new_state->plane_mask &
549 BIT(drm_plane_index(crtc->primary));
551 /* We always want to have an active plane with an active CRTC */
552 if (has_primary != new_state->enable)
556 if (new_state->connector_mask != connector_mask &&
557 new_state->connector_mask != 0) {
558 DRM_ERROR("Invalid connectors configuration\n");
563 * Our virtual device does not have a dot clock, so use the logical
564 * clock value as the dot clock.
566 if (new_state->mode.crtc_clock == 0)
567 new_state->adjusted_mode.crtc_clock = new_state->mode.clock;
573 void vmw_du_crtc_atomic_begin(struct drm_crtc *crtc,
574 struct drm_crtc_state *old_crtc_state)
579 void vmw_du_crtc_atomic_flush(struct drm_crtc *crtc,
580 struct drm_crtc_state *old_crtc_state)
582 struct drm_pending_vblank_event *event = crtc->state->event;
585 crtc->state->event = NULL;
587 spin_lock_irq(&crtc->dev->event_lock);
588 if (drm_crtc_vblank_get(crtc) == 0)
589 drm_crtc_arm_vblank_event(crtc, event);
591 drm_crtc_send_vblank_event(crtc, event);
592 spin_unlock_irq(&crtc->dev->event_lock);
599 * vmw_du_crtc_duplicate_state - duplicate crtc state
602 * Allocates and returns a copy of the crtc state (both common and
603 * vmw-specific) for the specified crtc.
605 * Returns: The newly allocated crtc state, or NULL on failure.
607 struct drm_crtc_state *
608 vmw_du_crtc_duplicate_state(struct drm_crtc *crtc)
610 struct drm_crtc_state *state;
611 struct vmw_crtc_state *vcs;
613 if (WARN_ON(!crtc->state))
616 vcs = kmemdup(crtc->state, sizeof(*vcs), GFP_KERNEL);
623 __drm_atomic_helper_crtc_duplicate_state(crtc, state);
630 * vmw_du_crtc_reset - creates a blank vmw crtc state
633 * Resets the atomic state for @crtc by freeing the state pointer (which
634 * might be NULL, e.g. at driver load time) and allocating a new empty state
637 void vmw_du_crtc_reset(struct drm_crtc *crtc)
639 struct vmw_crtc_state *vcs;
643 __drm_atomic_helper_crtc_destroy_state(crtc->state);
645 kfree(vmw_crtc_state_to_vcs(crtc->state));
648 vcs = kzalloc(sizeof(*vcs), GFP_KERNEL);
651 DRM_ERROR("Cannot allocate vmw_crtc_state\n");
655 crtc->state = &vcs->base;
656 crtc->state->crtc = crtc;
661 * vmw_du_crtc_destroy_state - destroy crtc state
663 * @state: state object to destroy
665 * Destroys the crtc state (both common and vmw-specific) for the
669 vmw_du_crtc_destroy_state(struct drm_crtc *crtc,
670 struct drm_crtc_state *state)
672 drm_atomic_helper_crtc_destroy_state(crtc, state);
677 * vmw_du_plane_duplicate_state - duplicate plane state
680 * Allocates and returns a copy of the plane state (both common and
681 * vmw-specific) for the specified plane.
683 * Returns: The newly allocated plane state, or NULL on failure.
685 struct drm_plane_state *
686 vmw_du_plane_duplicate_state(struct drm_plane *plane)
688 struct drm_plane_state *state;
689 struct vmw_plane_state *vps;
691 vps = kmemdup(plane->state, sizeof(*vps), GFP_KERNEL);
698 /* Mapping is managed by prepare_fb/cleanup_fb */
699 memset(&vps->host_map, 0, sizeof(vps->host_map));
702 /* Each ref counted resource needs to be acquired again */
704 (void) vmw_surface_reference(vps->surf);
707 (void) vmw_dmabuf_reference(vps->dmabuf);
711 __drm_atomic_helper_plane_duplicate_state(plane, state);
718 * vmw_du_plane_reset - creates a blank vmw plane state
721 * Resets the atomic state for @plane by freeing the state pointer (which might
722 * be NULL, e.g. at driver load time) and allocating a new empty state object.
724 void vmw_du_plane_reset(struct drm_plane *plane)
726 struct vmw_plane_state *vps;
730 vmw_du_plane_destroy_state(plane, plane->state);
732 vps = kzalloc(sizeof(*vps), GFP_KERNEL);
735 DRM_ERROR("Cannot allocate vmw_plane_state\n");
739 plane->state = &vps->base;
740 plane->state->plane = plane;
741 plane->state->rotation = DRM_MODE_ROTATE_0;
746 * vmw_du_plane_destroy_state - destroy plane state
748 * @state: state object to destroy
750 * Destroys the plane state (both common and vmw-specific) for the
754 vmw_du_plane_destroy_state(struct drm_plane *plane,
755 struct drm_plane_state *state)
757 struct vmw_plane_state *vps = vmw_plane_state_to_vps(state);
760 /* Should have been freed by cleanup_fb */
761 if (vps->host_map.virtual) {
762 DRM_ERROR("Host mapping not freed\n");
763 ttm_bo_kunmap(&vps->host_map);
767 vmw_surface_unreference(&vps->surf);
770 vmw_dmabuf_unreference(&vps->dmabuf);
772 drm_atomic_helper_plane_destroy_state(plane, state);
777 * vmw_du_connector_duplicate_state - duplicate connector state
778 * @connector: DRM connector
780 * Allocates and returns a copy of the connector state (both common and
781 * vmw-specific) for the specified connector.
783 * Returns: The newly allocated connector state, or NULL on failure.
785 struct drm_connector_state *
786 vmw_du_connector_duplicate_state(struct drm_connector *connector)
788 struct drm_connector_state *state;
789 struct vmw_connector_state *vcs;
791 if (WARN_ON(!connector->state))
794 vcs = kmemdup(connector->state, sizeof(*vcs), GFP_KERNEL);
801 __drm_atomic_helper_connector_duplicate_state(connector, state);
808 * vmw_du_connector_reset - creates a blank vmw connector state
809 * @connector: DRM connector
811 * Resets the atomic state for @connector by freeing the state pointer (which
812 * might be NULL, e.g. at driver load time) and allocating a new empty state
815 void vmw_du_connector_reset(struct drm_connector *connector)
817 struct vmw_connector_state *vcs;
820 if (connector->state) {
821 __drm_atomic_helper_connector_destroy_state(connector->state);
823 kfree(vmw_connector_state_to_vcs(connector->state));
826 vcs = kzalloc(sizeof(*vcs), GFP_KERNEL);
829 DRM_ERROR("Cannot allocate vmw_connector_state\n");
833 __drm_atomic_helper_connector_reset(connector, &vcs->base);
838 * vmw_du_connector_destroy_state - destroy connector state
839 * @connector: DRM connector
840 * @state: state object to destroy
842 * Destroys the connector state (both common and vmw-specific) for the
846 vmw_du_connector_destroy_state(struct drm_connector *connector,
847 struct drm_connector_state *state)
849 drm_atomic_helper_connector_destroy_state(connector, state);
852 * Generic framebuffer code
856 * Surface framebuffer code
859 static void vmw_framebuffer_surface_destroy(struct drm_framebuffer *framebuffer)
861 struct vmw_framebuffer_surface *vfbs =
862 vmw_framebuffer_to_vfbs(framebuffer);
864 drm_framebuffer_cleanup(framebuffer);
865 vmw_surface_unreference(&vfbs->surface);
866 if (vfbs->base.user_obj)
867 ttm_base_object_unref(&vfbs->base.user_obj);
872 static int vmw_framebuffer_surface_dirty(struct drm_framebuffer *framebuffer,
873 struct drm_file *file_priv,
874 unsigned flags, unsigned color,
875 struct drm_clip_rect *clips,
878 struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
879 struct vmw_framebuffer_surface *vfbs =
880 vmw_framebuffer_to_vfbs(framebuffer);
881 struct drm_clip_rect norect;
884 /* Legacy Display Unit does not support 3D */
885 if (dev_priv->active_display_unit == vmw_du_legacy)
888 drm_modeset_lock_all(dev_priv->dev);
890 ret = ttm_read_lock(&dev_priv->reservation_sem, true);
891 if (unlikely(ret != 0)) {
892 drm_modeset_unlock_all(dev_priv->dev);
899 norect.x1 = norect.y1 = 0;
900 norect.x2 = framebuffer->width;
901 norect.y2 = framebuffer->height;
902 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
904 inc = 2; /* skip source rects */
907 if (dev_priv->active_display_unit == vmw_du_screen_object)
908 ret = vmw_kms_sou_do_surface_dirty(dev_priv, &vfbs->base,
909 clips, NULL, NULL, 0, 0,
910 num_clips, inc, NULL);
912 ret = vmw_kms_stdu_surface_dirty(dev_priv, &vfbs->base,
913 clips, NULL, NULL, 0, 0,
914 num_clips, inc, NULL);
916 vmw_fifo_flush(dev_priv, false);
917 ttm_read_unlock(&dev_priv->reservation_sem);
919 drm_modeset_unlock_all(dev_priv->dev);
925 * vmw_kms_readback - Perform a readback from the screen system to
926 * a dma-buffer backed framebuffer.
928 * @dev_priv: Pointer to the device private structure.
929 * @file_priv: Pointer to a struct drm_file identifying the caller.
930 * Must be set to NULL if @user_fence_rep is NULL.
931 * @vfb: Pointer to the dma-buffer backed framebuffer.
932 * @user_fence_rep: User-space provided structure for fence information.
933 * Must be set to non-NULL if @file_priv is non-NULL.
934 * @vclips: Array of clip rects.
935 * @num_clips: Number of clip rects in @vclips.
937 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
940 int vmw_kms_readback(struct vmw_private *dev_priv,
941 struct drm_file *file_priv,
942 struct vmw_framebuffer *vfb,
943 struct drm_vmw_fence_rep __user *user_fence_rep,
944 struct drm_vmw_rect *vclips,
947 switch (dev_priv->active_display_unit) {
948 case vmw_du_screen_object:
949 return vmw_kms_sou_readback(dev_priv, file_priv, vfb,
950 user_fence_rep, vclips, num_clips);
951 case vmw_du_screen_target:
952 return vmw_kms_stdu_dma(dev_priv, file_priv, vfb,
953 user_fence_rep, NULL, vclips, num_clips,
957 "Readback called with invalid display system.\n");
964 static const struct drm_framebuffer_funcs vmw_framebuffer_surface_funcs = {
965 .destroy = vmw_framebuffer_surface_destroy,
966 .dirty = vmw_framebuffer_surface_dirty,
969 static int vmw_kms_new_framebuffer_surface(struct vmw_private *dev_priv,
970 struct vmw_surface *surface,
971 struct vmw_framebuffer **out,
972 const struct drm_mode_fb_cmd2
974 bool is_dmabuf_proxy)
977 struct drm_device *dev = dev_priv->dev;
978 struct vmw_framebuffer_surface *vfbs;
979 enum SVGA3dSurfaceFormat format;
981 struct drm_format_name_buf format_name;
983 /* 3D is only supported on HWv8 and newer hosts */
984 if (dev_priv->active_display_unit == vmw_du_legacy)
991 /* Surface must be marked as a scanout. */
992 if (unlikely(!surface->scanout))
995 if (unlikely(surface->mip_levels[0] != 1 ||
996 surface->num_sizes != 1 ||
997 surface->base_size.width < mode_cmd->width ||
998 surface->base_size.height < mode_cmd->height ||
999 surface->base_size.depth != 1)) {
1000 DRM_ERROR("Incompatible surface dimensions "
1001 "for requested mode.\n");
1005 switch (mode_cmd->pixel_format) {
1006 case DRM_FORMAT_ARGB8888:
1007 format = SVGA3D_A8R8G8B8;
1009 case DRM_FORMAT_XRGB8888:
1010 format = SVGA3D_X8R8G8B8;
1012 case DRM_FORMAT_RGB565:
1013 format = SVGA3D_R5G6B5;
1015 case DRM_FORMAT_XRGB1555:
1016 format = SVGA3D_A1R5G5B5;
1019 DRM_ERROR("Invalid pixel format: %s\n",
1020 drm_get_format_name(mode_cmd->pixel_format, &format_name));
1025 * For DX, surface format validation is done when surface->scanout
1028 if (!dev_priv->has_dx && format != surface->format) {
1029 DRM_ERROR("Invalid surface format for requested mode.\n");
1033 vfbs = kzalloc(sizeof(*vfbs), GFP_KERNEL);
1039 drm_helper_mode_fill_fb_struct(dev, &vfbs->base.base, mode_cmd);
1040 vfbs->surface = vmw_surface_reference(surface);
1041 vfbs->base.user_handle = mode_cmd->handles[0];
1042 vfbs->is_dmabuf_proxy = is_dmabuf_proxy;
1046 ret = drm_framebuffer_init(dev, &vfbs->base.base,
1047 &vmw_framebuffer_surface_funcs);
1054 vmw_surface_unreference(&surface);
1061 * Dmabuf framebuffer code
1064 static void vmw_framebuffer_dmabuf_destroy(struct drm_framebuffer *framebuffer)
1066 struct vmw_framebuffer_dmabuf *vfbd =
1067 vmw_framebuffer_to_vfbd(framebuffer);
1069 drm_framebuffer_cleanup(framebuffer);
1070 vmw_dmabuf_unreference(&vfbd->buffer);
1071 if (vfbd->base.user_obj)
1072 ttm_base_object_unref(&vfbd->base.user_obj);
1077 static int vmw_framebuffer_dmabuf_dirty(struct drm_framebuffer *framebuffer,
1078 struct drm_file *file_priv,
1079 unsigned flags, unsigned color,
1080 struct drm_clip_rect *clips,
1083 struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
1084 struct vmw_framebuffer_dmabuf *vfbd =
1085 vmw_framebuffer_to_vfbd(framebuffer);
1086 struct drm_clip_rect norect;
1087 int ret, increment = 1;
1089 drm_modeset_lock_all(dev_priv->dev);
1091 ret = ttm_read_lock(&dev_priv->reservation_sem, true);
1092 if (unlikely(ret != 0)) {
1093 drm_modeset_unlock_all(dev_priv->dev);
1100 norect.x1 = norect.y1 = 0;
1101 norect.x2 = framebuffer->width;
1102 norect.y2 = framebuffer->height;
1103 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
1108 switch (dev_priv->active_display_unit) {
1109 case vmw_du_screen_target:
1110 ret = vmw_kms_stdu_dma(dev_priv, NULL, &vfbd->base, NULL,
1111 clips, NULL, num_clips, increment,
1114 case vmw_du_screen_object:
1115 ret = vmw_kms_sou_do_dmabuf_dirty(dev_priv, &vfbd->base,
1116 clips, NULL, num_clips,
1117 increment, true, NULL);
1120 ret = vmw_kms_ldu_do_dmabuf_dirty(dev_priv, &vfbd->base, 0, 0,
1121 clips, num_clips, increment);
1125 WARN_ONCE(true, "Dirty called with invalid display system.\n");
1129 vmw_fifo_flush(dev_priv, false);
1130 ttm_read_unlock(&dev_priv->reservation_sem);
1132 drm_modeset_unlock_all(dev_priv->dev);
1137 static const struct drm_framebuffer_funcs vmw_framebuffer_dmabuf_funcs = {
1138 .destroy = vmw_framebuffer_dmabuf_destroy,
1139 .dirty = vmw_framebuffer_dmabuf_dirty,
1143 * Pin the dmabuffer to the start of vram.
1145 static int vmw_framebuffer_pin(struct vmw_framebuffer *vfb)
1147 struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
1148 struct vmw_dma_buffer *buf;
1151 buf = vfb->dmabuf ? vmw_framebuffer_to_vfbd(&vfb->base)->buffer :
1152 vmw_framebuffer_to_vfbs(&vfb->base)->surface->res.backup;
1157 switch (dev_priv->active_display_unit) {
1159 vmw_overlay_pause_all(dev_priv);
1160 ret = vmw_dmabuf_pin_in_start_of_vram(dev_priv, buf, false);
1161 vmw_overlay_resume_all(dev_priv);
1163 case vmw_du_screen_object:
1164 case vmw_du_screen_target:
1166 return vmw_dmabuf_pin_in_vram_or_gmr(dev_priv, buf,
1169 return vmw_dmabuf_pin_in_placement(dev_priv, buf,
1170 &vmw_mob_placement, false);
1178 static int vmw_framebuffer_unpin(struct vmw_framebuffer *vfb)
1180 struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
1181 struct vmw_dma_buffer *buf;
1183 buf = vfb->dmabuf ? vmw_framebuffer_to_vfbd(&vfb->base)->buffer :
1184 vmw_framebuffer_to_vfbs(&vfb->base)->surface->res.backup;
1189 return vmw_dmabuf_unpin(dev_priv, buf, false);
1193 * vmw_create_dmabuf_proxy - create a proxy surface for the DMA buf
1196 * @mode_cmd: parameters for the new surface
1197 * @dmabuf_mob: MOB backing the DMA buf
1198 * @srf_out: newly created surface
1200 * When the content FB is a DMA buf, we create a surface as a proxy to the
1201 * same buffer. This way we can do a surface copy rather than a surface DMA.
1202 * This is a more efficient approach
1205 * 0 on success, error code otherwise
1207 static int vmw_create_dmabuf_proxy(struct drm_device *dev,
1208 const struct drm_mode_fb_cmd2 *mode_cmd,
1209 struct vmw_dma_buffer *dmabuf_mob,
1210 struct vmw_surface **srf_out)
1213 struct drm_vmw_size content_base_size = {0};
1214 struct vmw_resource *res;
1215 unsigned int bytes_pp;
1216 struct drm_format_name_buf format_name;
1219 switch (mode_cmd->pixel_format) {
1220 case DRM_FORMAT_ARGB8888:
1221 case DRM_FORMAT_XRGB8888:
1222 format = SVGA3D_X8R8G8B8;
1226 case DRM_FORMAT_RGB565:
1227 case DRM_FORMAT_XRGB1555:
1228 format = SVGA3D_R5G6B5;
1238 DRM_ERROR("Invalid framebuffer format %s\n",
1239 drm_get_format_name(mode_cmd->pixel_format, &format_name));
1243 content_base_size.width = mode_cmd->pitches[0] / bytes_pp;
1244 content_base_size.height = mode_cmd->height;
1245 content_base_size.depth = 1;
1247 ret = vmw_surface_gb_priv_define(dev,
1248 0, /* kernel visible only */
1251 true, /* can be a scanout buffer */
1252 1, /* num of mip levels */
1258 DRM_ERROR("Failed to allocate proxy content buffer\n");
1262 res = &(*srf_out)->res;
1264 /* Reserve and switch the backing mob. */
1265 mutex_lock(&res->dev_priv->cmdbuf_mutex);
1266 (void) vmw_resource_reserve(res, false, true);
1267 vmw_dmabuf_unreference(&res->backup);
1268 res->backup = vmw_dmabuf_reference(dmabuf_mob);
1269 res->backup_offset = 0;
1270 vmw_resource_unreserve(res, false, NULL, 0);
1271 mutex_unlock(&res->dev_priv->cmdbuf_mutex);
1278 static int vmw_kms_new_framebuffer_dmabuf(struct vmw_private *dev_priv,
1279 struct vmw_dma_buffer *dmabuf,
1280 struct vmw_framebuffer **out,
1281 const struct drm_mode_fb_cmd2
1285 struct drm_device *dev = dev_priv->dev;
1286 struct vmw_framebuffer_dmabuf *vfbd;
1287 unsigned int requested_size;
1288 struct drm_format_name_buf format_name;
1291 requested_size = mode_cmd->height * mode_cmd->pitches[0];
1292 if (unlikely(requested_size > dmabuf->base.num_pages * PAGE_SIZE)) {
1293 DRM_ERROR("Screen buffer object size is too small "
1294 "for requested mode.\n");
1298 /* Limited framebuffer color depth support for screen objects */
1299 if (dev_priv->active_display_unit == vmw_du_screen_object) {
1300 switch (mode_cmd->pixel_format) {
1301 case DRM_FORMAT_XRGB8888:
1302 case DRM_FORMAT_ARGB8888:
1304 case DRM_FORMAT_XRGB1555:
1305 case DRM_FORMAT_RGB565:
1308 DRM_ERROR("Invalid pixel format: %s\n",
1309 drm_get_format_name(mode_cmd->pixel_format, &format_name));
1314 vfbd = kzalloc(sizeof(*vfbd), GFP_KERNEL);
1320 drm_helper_mode_fill_fb_struct(dev, &vfbd->base.base, mode_cmd);
1321 vfbd->base.dmabuf = true;
1322 vfbd->buffer = vmw_dmabuf_reference(dmabuf);
1323 vfbd->base.user_handle = mode_cmd->handles[0];
1326 ret = drm_framebuffer_init(dev, &vfbd->base.base,
1327 &vmw_framebuffer_dmabuf_funcs);
1334 vmw_dmabuf_unreference(&dmabuf);
1342 * vmw_kms_srf_ok - check if a surface can be created
1344 * @width: requested width
1345 * @height: requested height
1347 * Surfaces need to be less than texture size
1350 vmw_kms_srf_ok(struct vmw_private *dev_priv, uint32_t width, uint32_t height)
1352 if (width > dev_priv->texture_max_width ||
1353 height > dev_priv->texture_max_height)
1360 * vmw_kms_new_framebuffer - Create a new framebuffer.
1362 * @dev_priv: Pointer to device private struct.
1363 * @dmabuf: Pointer to dma buffer to wrap the kms framebuffer around.
1364 * Either @dmabuf or @surface must be NULL.
1365 * @surface: Pointer to a surface to wrap the kms framebuffer around.
1366 * Either @dmabuf or @surface must be NULL.
1367 * @only_2d: No presents will occur to this dma buffer based framebuffer. This
1368 * Helps the code to do some important optimizations.
1369 * @mode_cmd: Frame-buffer metadata.
1371 struct vmw_framebuffer *
1372 vmw_kms_new_framebuffer(struct vmw_private *dev_priv,
1373 struct vmw_dma_buffer *dmabuf,
1374 struct vmw_surface *surface,
1376 const struct drm_mode_fb_cmd2 *mode_cmd)
1378 struct vmw_framebuffer *vfb = NULL;
1379 bool is_dmabuf_proxy = false;
1383 * We cannot use the SurfaceDMA command in an non-accelerated VM,
1384 * therefore, wrap the DMA buf in a surface so we can use the
1385 * SurfaceCopy command.
1387 if (vmw_kms_srf_ok(dev_priv, mode_cmd->width, mode_cmd->height) &&
1388 dmabuf && only_2d &&
1389 mode_cmd->width > 64 && /* Don't create a proxy for cursor */
1390 dev_priv->active_display_unit == vmw_du_screen_target) {
1391 ret = vmw_create_dmabuf_proxy(dev_priv->dev, mode_cmd,
1394 return ERR_PTR(ret);
1396 is_dmabuf_proxy = true;
1399 /* Create the new framebuffer depending one what we have */
1401 ret = vmw_kms_new_framebuffer_surface(dev_priv, surface, &vfb,
1406 * vmw_create_dmabuf_proxy() adds a reference that is no longer
1409 if (is_dmabuf_proxy)
1410 vmw_surface_unreference(&surface);
1411 } else if (dmabuf) {
1412 ret = vmw_kms_new_framebuffer_dmabuf(dev_priv, dmabuf, &vfb,
1419 return ERR_PTR(ret);
1421 vfb->pin = vmw_framebuffer_pin;
1422 vfb->unpin = vmw_framebuffer_unpin;
1428 * Generic Kernel modesetting functions
1431 static struct drm_framebuffer *vmw_kms_fb_create(struct drm_device *dev,
1432 struct drm_file *file_priv,
1433 const struct drm_mode_fb_cmd2 *mode_cmd)
1435 struct vmw_private *dev_priv = vmw_priv(dev);
1436 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
1437 struct vmw_framebuffer *vfb = NULL;
1438 struct vmw_surface *surface = NULL;
1439 struct vmw_dma_buffer *bo = NULL;
1440 struct ttm_base_object *user_obj;
1444 * This code should be conditioned on Screen Objects not being used.
1445 * If screen objects are used, we can allocate a GMR to hold the
1446 * requested framebuffer.
1449 if (!vmw_kms_validate_mode_vram(dev_priv,
1450 mode_cmd->pitches[0],
1451 mode_cmd->height)) {
1452 DRM_ERROR("Requested mode exceed bounding box limit.\n");
1453 return ERR_PTR(-ENOMEM);
1457 * Take a reference on the user object of the resource
1458 * backing the kms fb. This ensures that user-space handle
1459 * lookups on that resource will always work as long as
1460 * it's registered with a kms framebuffer. This is important,
1461 * since vmw_execbuf_process identifies resources in the
1462 * command stream using user-space handles.
1465 user_obj = ttm_base_object_lookup(tfile, mode_cmd->handles[0]);
1466 if (unlikely(user_obj == NULL)) {
1467 DRM_ERROR("Could not locate requested kms frame buffer.\n");
1468 return ERR_PTR(-ENOENT);
1472 * End conditioned code.
1475 /* returns either a dmabuf or surface */
1476 ret = vmw_user_lookup_handle(dev_priv, tfile,
1477 mode_cmd->handles[0],
1484 !vmw_kms_srf_ok(dev_priv, mode_cmd->width, mode_cmd->height)) {
1485 DRM_ERROR("Surface size cannot exceed %dx%d",
1486 dev_priv->texture_max_width,
1487 dev_priv->texture_max_height);
1492 vfb = vmw_kms_new_framebuffer(dev_priv, bo, surface,
1493 !(dev_priv->capabilities & SVGA_CAP_3D),
1501 /* vmw_user_lookup_handle takes one ref so does new_fb */
1503 vmw_dmabuf_unreference(&bo);
1505 vmw_surface_unreference(&surface);
1508 DRM_ERROR("failed to create vmw_framebuffer: %i\n", ret);
1509 ttm_base_object_unref(&user_obj);
1510 return ERR_PTR(ret);
1512 vfb->user_obj = user_obj;
1520 * vmw_kms_atomic_check_modeset- validate state object for modeset changes
1523 * @state: the driver state object
1525 * This is a simple wrapper around drm_atomic_helper_check_modeset() for
1526 * us to assign a value to mode->crtc_clock so that
1527 * drm_calc_timestamping_constants() won't throw an error message
1530 * Zero for success or -errno
1533 vmw_kms_atomic_check_modeset(struct drm_device *dev,
1534 struct drm_atomic_state *state)
1536 struct drm_crtc_state *crtc_state;
1537 struct drm_crtc *crtc;
1538 struct vmw_private *dev_priv = vmw_priv(dev);
1541 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
1542 unsigned long requested_bb_mem = 0;
1544 if (dev_priv->active_display_unit == vmw_du_screen_target) {
1545 if (crtc->primary->fb) {
1546 int cpp = crtc->primary->fb->pitches[0] /
1547 crtc->primary->fb->width;
1549 requested_bb_mem += crtc->mode.hdisplay * cpp *
1550 crtc->mode.vdisplay;
1553 if (requested_bb_mem > dev_priv->prim_bb_mem)
1558 return drm_atomic_helper_check(dev, state);
1563 * vmw_kms_atomic_commit - Perform an atomic state commit
1566 * @state: the driver state object
1567 * @nonblock: Whether nonblocking behaviour is requested
1569 * This is a simple wrapper around drm_atomic_helper_commit() for
1570 * us to clear the nonblocking value.
1572 * Nonblocking commits currently cause synchronization issues
1576 * Zero for success or negative error code on failure.
1578 int vmw_kms_atomic_commit(struct drm_device *dev,
1579 struct drm_atomic_state *state,
1582 return drm_atomic_helper_commit(dev, state, false);
1586 static const struct drm_mode_config_funcs vmw_kms_funcs = {
1587 .fb_create = vmw_kms_fb_create,
1588 .atomic_check = vmw_kms_atomic_check_modeset,
1589 .atomic_commit = vmw_kms_atomic_commit,
1592 static int vmw_kms_generic_present(struct vmw_private *dev_priv,
1593 struct drm_file *file_priv,
1594 struct vmw_framebuffer *vfb,
1595 struct vmw_surface *surface,
1597 int32_t destX, int32_t destY,
1598 struct drm_vmw_rect *clips,
1601 return vmw_kms_sou_do_surface_dirty(dev_priv, vfb, NULL, clips,
1602 &surface->res, destX, destY,
1603 num_clips, 1, NULL);
1607 int vmw_kms_present(struct vmw_private *dev_priv,
1608 struct drm_file *file_priv,
1609 struct vmw_framebuffer *vfb,
1610 struct vmw_surface *surface,
1612 int32_t destX, int32_t destY,
1613 struct drm_vmw_rect *clips,
1618 switch (dev_priv->active_display_unit) {
1619 case vmw_du_screen_target:
1620 ret = vmw_kms_stdu_surface_dirty(dev_priv, vfb, NULL, clips,
1621 &surface->res, destX, destY,
1622 num_clips, 1, NULL);
1624 case vmw_du_screen_object:
1625 ret = vmw_kms_generic_present(dev_priv, file_priv, vfb, surface,
1626 sid, destX, destY, clips,
1631 "Present called with invalid display system.\n");
1638 vmw_fifo_flush(dev_priv, false);
1644 vmw_kms_create_hotplug_mode_update_property(struct vmw_private *dev_priv)
1646 if (dev_priv->hotplug_mode_update_property)
1649 dev_priv->hotplug_mode_update_property =
1650 drm_property_create_range(dev_priv->dev,
1651 DRM_MODE_PROP_IMMUTABLE,
1652 "hotplug_mode_update", 0, 1);
1654 if (!dev_priv->hotplug_mode_update_property)
1659 int vmw_kms_init(struct vmw_private *dev_priv)
1661 struct drm_device *dev = dev_priv->dev;
1664 drm_mode_config_init(dev);
1665 dev->mode_config.funcs = &vmw_kms_funcs;
1666 dev->mode_config.min_width = 1;
1667 dev->mode_config.min_height = 1;
1668 dev->mode_config.max_width = dev_priv->texture_max_width;
1669 dev->mode_config.max_height = dev_priv->texture_max_height;
1671 drm_mode_create_suggested_offset_properties(dev);
1672 vmw_kms_create_hotplug_mode_update_property(dev_priv);
1674 ret = vmw_kms_stdu_init_display(dev_priv);
1676 ret = vmw_kms_sou_init_display(dev_priv);
1677 if (ret) /* Fallback */
1678 ret = vmw_kms_ldu_init_display(dev_priv);
1684 int vmw_kms_close(struct vmw_private *dev_priv)
1689 * Docs says we should take the lock before calling this function
1690 * but since it destroys encoders and our destructor calls
1691 * drm_encoder_cleanup which takes the lock we deadlock.
1693 drm_mode_config_cleanup(dev_priv->dev);
1694 if (dev_priv->active_display_unit == vmw_du_legacy)
1695 ret = vmw_kms_ldu_close_display(dev_priv);
1700 int vmw_kms_cursor_bypass_ioctl(struct drm_device *dev, void *data,
1701 struct drm_file *file_priv)
1703 struct drm_vmw_cursor_bypass_arg *arg = data;
1704 struct vmw_display_unit *du;
1705 struct drm_crtc *crtc;
1709 mutex_lock(&dev->mode_config.mutex);
1710 if (arg->flags & DRM_VMW_CURSOR_BYPASS_ALL) {
1712 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1713 du = vmw_crtc_to_du(crtc);
1714 du->hotspot_x = arg->xhot;
1715 du->hotspot_y = arg->yhot;
1718 mutex_unlock(&dev->mode_config.mutex);
1722 crtc = drm_crtc_find(dev, arg->crtc_id);
1728 du = vmw_crtc_to_du(crtc);
1730 du->hotspot_x = arg->xhot;
1731 du->hotspot_y = arg->yhot;
1734 mutex_unlock(&dev->mode_config.mutex);
1739 int vmw_kms_write_svga(struct vmw_private *vmw_priv,
1740 unsigned width, unsigned height, unsigned pitch,
1741 unsigned bpp, unsigned depth)
1743 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1744 vmw_write(vmw_priv, SVGA_REG_PITCHLOCK, pitch);
1745 else if (vmw_fifo_have_pitchlock(vmw_priv))
1746 vmw_mmio_write(pitch, vmw_priv->mmio_virt +
1747 SVGA_FIFO_PITCHLOCK);
1748 vmw_write(vmw_priv, SVGA_REG_WIDTH, width);
1749 vmw_write(vmw_priv, SVGA_REG_HEIGHT, height);
1750 vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, bpp);
1752 if (vmw_read(vmw_priv, SVGA_REG_DEPTH) != depth) {
1753 DRM_ERROR("Invalid depth %u for %u bpp, host expects %u\n",
1754 depth, bpp, vmw_read(vmw_priv, SVGA_REG_DEPTH));
1761 int vmw_kms_save_vga(struct vmw_private *vmw_priv)
1763 struct vmw_vga_topology_state *save;
1766 vmw_priv->vga_width = vmw_read(vmw_priv, SVGA_REG_WIDTH);
1767 vmw_priv->vga_height = vmw_read(vmw_priv, SVGA_REG_HEIGHT);
1768 vmw_priv->vga_bpp = vmw_read(vmw_priv, SVGA_REG_BITS_PER_PIXEL);
1769 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1770 vmw_priv->vga_pitchlock =
1771 vmw_read(vmw_priv, SVGA_REG_PITCHLOCK);
1772 else if (vmw_fifo_have_pitchlock(vmw_priv))
1773 vmw_priv->vga_pitchlock = vmw_mmio_read(vmw_priv->mmio_virt +
1774 SVGA_FIFO_PITCHLOCK);
1776 if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
1779 vmw_priv->num_displays = vmw_read(vmw_priv,
1780 SVGA_REG_NUM_GUEST_DISPLAYS);
1782 if (vmw_priv->num_displays == 0)
1783 vmw_priv->num_displays = 1;
1785 for (i = 0; i < vmw_priv->num_displays; ++i) {
1786 save = &vmw_priv->vga_save[i];
1787 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
1788 save->primary = vmw_read(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY);
1789 save->pos_x = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_X);
1790 save->pos_y = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y);
1791 save->width = vmw_read(vmw_priv, SVGA_REG_DISPLAY_WIDTH);
1792 save->height = vmw_read(vmw_priv, SVGA_REG_DISPLAY_HEIGHT);
1793 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
1794 if (i == 0 && vmw_priv->num_displays == 1 &&
1795 save->width == 0 && save->height == 0) {
1798 * It should be fairly safe to assume that these
1799 * values are uninitialized.
1802 save->width = vmw_priv->vga_width - save->pos_x;
1803 save->height = vmw_priv->vga_height - save->pos_y;
1810 int vmw_kms_restore_vga(struct vmw_private *vmw_priv)
1812 struct vmw_vga_topology_state *save;
1815 vmw_write(vmw_priv, SVGA_REG_WIDTH, vmw_priv->vga_width);
1816 vmw_write(vmw_priv, SVGA_REG_HEIGHT, vmw_priv->vga_height);
1817 vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, vmw_priv->vga_bpp);
1818 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1819 vmw_write(vmw_priv, SVGA_REG_PITCHLOCK,
1820 vmw_priv->vga_pitchlock);
1821 else if (vmw_fifo_have_pitchlock(vmw_priv))
1822 vmw_mmio_write(vmw_priv->vga_pitchlock,
1823 vmw_priv->mmio_virt + SVGA_FIFO_PITCHLOCK);
1825 if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
1828 for (i = 0; i < vmw_priv->num_displays; ++i) {
1829 save = &vmw_priv->vga_save[i];
1830 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
1831 vmw_write(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY, save->primary);
1832 vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_X, save->pos_x);
1833 vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y, save->pos_y);
1834 vmw_write(vmw_priv, SVGA_REG_DISPLAY_WIDTH, save->width);
1835 vmw_write(vmw_priv, SVGA_REG_DISPLAY_HEIGHT, save->height);
1836 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
1842 bool vmw_kms_validate_mode_vram(struct vmw_private *dev_priv,
1846 return ((u64) pitch * (u64) height) < (u64)
1847 ((dev_priv->active_display_unit == vmw_du_screen_target) ?
1848 dev_priv->prim_bb_mem : dev_priv->vram_size);
1853 * Function called by DRM code called with vbl_lock held.
1855 u32 vmw_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
1861 * Function called by DRM code called with vbl_lock held.
1863 int vmw_enable_vblank(struct drm_device *dev, unsigned int pipe)
1869 * Function called by DRM code called with vbl_lock held.
1871 void vmw_disable_vblank(struct drm_device *dev, unsigned int pipe)
1877 * Small shared kms functions.
1880 static int vmw_du_update_layout(struct vmw_private *dev_priv, unsigned num,
1881 struct drm_vmw_rect *rects)
1883 struct drm_device *dev = dev_priv->dev;
1884 struct vmw_display_unit *du;
1885 struct drm_connector *con;
1887 mutex_lock(&dev->mode_config.mutex);
1893 DRM_INFO("%s: new layout ", __func__);
1894 for (i = 0; i < num; i++)
1895 DRM_INFO("(%i, %i %ux%u) ", rects[i].x, rects[i].y,
1896 rects[i].w, rects[i].h);
1901 list_for_each_entry(con, &dev->mode_config.connector_list, head) {
1902 du = vmw_connector_to_du(con);
1903 if (num > du->unit) {
1904 du->pref_width = rects[du->unit].w;
1905 du->pref_height = rects[du->unit].h;
1906 du->pref_active = true;
1907 du->gui_x = rects[du->unit].x;
1908 du->gui_y = rects[du->unit].y;
1909 drm_object_property_set_value
1910 (&con->base, dev->mode_config.suggested_x_property,
1912 drm_object_property_set_value
1913 (&con->base, dev->mode_config.suggested_y_property,
1916 du->pref_width = 800;
1917 du->pref_height = 600;
1918 du->pref_active = false;
1919 drm_object_property_set_value
1920 (&con->base, dev->mode_config.suggested_x_property,
1922 drm_object_property_set_value
1923 (&con->base, dev->mode_config.suggested_y_property,
1926 con->status = vmw_du_connector_detect(con, true);
1929 mutex_unlock(&dev->mode_config.mutex);
1930 drm_sysfs_hotplug_event(dev);
1935 int vmw_du_crtc_gamma_set(struct drm_crtc *crtc,
1936 u16 *r, u16 *g, u16 *b,
1938 struct drm_modeset_acquire_ctx *ctx)
1940 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
1943 for (i = 0; i < size; i++) {
1944 DRM_DEBUG("%d r/g/b = 0x%04x / 0x%04x / 0x%04x\n", i,
1946 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 0, r[i] >> 8);
1947 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 1, g[i] >> 8);
1948 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 2, b[i] >> 8);
1954 int vmw_du_connector_dpms(struct drm_connector *connector, int mode)
1959 enum drm_connector_status
1960 vmw_du_connector_detect(struct drm_connector *connector, bool force)
1962 uint32_t num_displays;
1963 struct drm_device *dev = connector->dev;
1964 struct vmw_private *dev_priv = vmw_priv(dev);
1965 struct vmw_display_unit *du = vmw_connector_to_du(connector);
1967 num_displays = vmw_read(dev_priv, SVGA_REG_NUM_DISPLAYS);
1969 return ((vmw_connector_to_du(connector)->unit < num_displays &&
1971 connector_status_connected : connector_status_disconnected);
1974 static struct drm_display_mode vmw_kms_connector_builtin[] = {
1976 { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 25175, 640, 656,
1977 752, 800, 0, 480, 489, 492, 525, 0,
1978 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
1980 { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 40000, 800, 840,
1981 968, 1056, 0, 600, 601, 605, 628, 0,
1982 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1984 { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 65000, 1024, 1048,
1985 1184, 1344, 0, 768, 771, 777, 806, 0,
1986 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
1988 { DRM_MODE("1152x864", DRM_MODE_TYPE_DRIVER, 108000, 1152, 1216,
1989 1344, 1600, 0, 864, 865, 868, 900, 0,
1990 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1992 { DRM_MODE("1280x768", DRM_MODE_TYPE_DRIVER, 79500, 1280, 1344,
1993 1472, 1664, 0, 768, 771, 778, 798, 0,
1994 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1996 { DRM_MODE("1280x800", DRM_MODE_TYPE_DRIVER, 83500, 1280, 1352,
1997 1480, 1680, 0, 800, 803, 809, 831, 0,
1998 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
2000 { DRM_MODE("1280x960", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1376,
2001 1488, 1800, 0, 960, 961, 964, 1000, 0,
2002 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
2003 /* 1280x1024@60Hz */
2004 { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1328,
2005 1440, 1688, 0, 1024, 1025, 1028, 1066, 0,
2006 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
2008 { DRM_MODE("1360x768", DRM_MODE_TYPE_DRIVER, 85500, 1360, 1424,
2009 1536, 1792, 0, 768, 771, 777, 795, 0,
2010 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
2011 /* 1440x1050@60Hz */
2012 { DRM_MODE("1400x1050", DRM_MODE_TYPE_DRIVER, 121750, 1400, 1488,
2013 1632, 1864, 0, 1050, 1053, 1057, 1089, 0,
2014 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2016 { DRM_MODE("1440x900", DRM_MODE_TYPE_DRIVER, 106500, 1440, 1520,
2017 1672, 1904, 0, 900, 903, 909, 934, 0,
2018 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2019 /* 1600x1200@60Hz */
2020 { DRM_MODE("1600x1200", DRM_MODE_TYPE_DRIVER, 162000, 1600, 1664,
2021 1856, 2160, 0, 1200, 1201, 1204, 1250, 0,
2022 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
2023 /* 1680x1050@60Hz */
2024 { DRM_MODE("1680x1050", DRM_MODE_TYPE_DRIVER, 146250, 1680, 1784,
2025 1960, 2240, 0, 1050, 1053, 1059, 1089, 0,
2026 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2027 /* 1792x1344@60Hz */
2028 { DRM_MODE("1792x1344", DRM_MODE_TYPE_DRIVER, 204750, 1792, 1920,
2029 2120, 2448, 0, 1344, 1345, 1348, 1394, 0,
2030 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2031 /* 1853x1392@60Hz */
2032 { DRM_MODE("1856x1392", DRM_MODE_TYPE_DRIVER, 218250, 1856, 1952,
2033 2176, 2528, 0, 1392, 1393, 1396, 1439, 0,
2034 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2035 /* 1920x1200@60Hz */
2036 { DRM_MODE("1920x1200", DRM_MODE_TYPE_DRIVER, 193250, 1920, 2056,
2037 2256, 2592, 0, 1200, 1203, 1209, 1245, 0,
2038 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2039 /* 1920x1440@60Hz */
2040 { DRM_MODE("1920x1440", DRM_MODE_TYPE_DRIVER, 234000, 1920, 2048,
2041 2256, 2600, 0, 1440, 1441, 1444, 1500, 0,
2042 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2043 /* 2560x1600@60Hz */
2044 { DRM_MODE("2560x1600", DRM_MODE_TYPE_DRIVER, 348500, 2560, 2752,
2045 3032, 3504, 0, 1600, 1603, 1609, 1658, 0,
2046 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2048 { DRM_MODE("", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) },
2052 * vmw_guess_mode_timing - Provide fake timings for a
2053 * 60Hz vrefresh mode.
2055 * @mode - Pointer to a struct drm_display_mode with hdisplay and vdisplay
2056 * members filled in.
2058 void vmw_guess_mode_timing(struct drm_display_mode *mode)
2060 mode->hsync_start = mode->hdisplay + 50;
2061 mode->hsync_end = mode->hsync_start + 50;
2062 mode->htotal = mode->hsync_end + 50;
2064 mode->vsync_start = mode->vdisplay + 50;
2065 mode->vsync_end = mode->vsync_start + 50;
2066 mode->vtotal = mode->vsync_end + 50;
2068 mode->clock = (u32)mode->htotal * (u32)mode->vtotal / 100 * 6;
2069 mode->vrefresh = drm_mode_vrefresh(mode);
2073 int vmw_du_connector_fill_modes(struct drm_connector *connector,
2074 uint32_t max_width, uint32_t max_height)
2076 struct vmw_display_unit *du = vmw_connector_to_du(connector);
2077 struct drm_device *dev = connector->dev;
2078 struct vmw_private *dev_priv = vmw_priv(dev);
2079 struct drm_display_mode *mode = NULL;
2080 struct drm_display_mode *bmode;
2081 struct drm_display_mode prefmode = { DRM_MODE("preferred",
2082 DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED,
2083 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2084 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC)
2087 u32 assumed_bpp = 4;
2089 if (dev_priv->assume_16bpp)
2092 if (dev_priv->active_display_unit == vmw_du_screen_target) {
2093 max_width = min(max_width, dev_priv->stdu_max_width);
2094 max_width = min(max_width, dev_priv->texture_max_width);
2096 max_height = min(max_height, dev_priv->stdu_max_height);
2097 max_height = min(max_height, dev_priv->texture_max_height);
2100 /* Add preferred mode */
2101 mode = drm_mode_duplicate(dev, &prefmode);
2104 mode->hdisplay = du->pref_width;
2105 mode->vdisplay = du->pref_height;
2106 vmw_guess_mode_timing(mode);
2108 if (vmw_kms_validate_mode_vram(dev_priv,
2109 mode->hdisplay * assumed_bpp,
2111 drm_mode_probed_add(connector, mode);
2113 drm_mode_destroy(dev, mode);
2117 if (du->pref_mode) {
2118 list_del_init(&du->pref_mode->head);
2119 drm_mode_destroy(dev, du->pref_mode);
2122 /* mode might be null here, this is intended */
2123 du->pref_mode = mode;
2125 for (i = 0; vmw_kms_connector_builtin[i].type != 0; i++) {
2126 bmode = &vmw_kms_connector_builtin[i];
2127 if (bmode->hdisplay > max_width ||
2128 bmode->vdisplay > max_height)
2131 if (!vmw_kms_validate_mode_vram(dev_priv,
2132 bmode->hdisplay * assumed_bpp,
2136 mode = drm_mode_duplicate(dev, bmode);
2139 mode->vrefresh = drm_mode_vrefresh(mode);
2141 drm_mode_probed_add(connector, mode);
2144 drm_mode_connector_list_update(connector);
2145 /* Move the prefered mode first, help apps pick the right mode. */
2146 drm_mode_sort(&connector->modes);
2151 int vmw_du_connector_set_property(struct drm_connector *connector,
2152 struct drm_property *property,
2155 struct vmw_display_unit *du = vmw_connector_to_du(connector);
2156 struct vmw_private *dev_priv = vmw_priv(connector->dev);
2158 if (property == dev_priv->implicit_placement_property)
2159 du->is_implicit = val;
2167 * vmw_du_connector_atomic_set_property - Atomic version of get property
2169 * @crtc - crtc the property is associated with
2172 * Zero on success, negative errno on failure.
2175 vmw_du_connector_atomic_set_property(struct drm_connector *connector,
2176 struct drm_connector_state *state,
2177 struct drm_property *property,
2180 struct vmw_private *dev_priv = vmw_priv(connector->dev);
2181 struct vmw_connector_state *vcs = vmw_connector_state_to_vcs(state);
2182 struct vmw_display_unit *du = vmw_connector_to_du(connector);
2185 if (property == dev_priv->implicit_placement_property) {
2186 vcs->is_implicit = val;
2189 * We should really be doing a drm_atomic_commit() to
2190 * commit the new state, but since this doesn't cause
2191 * an immedate state change, this is probably ok
2193 du->is_implicit = vcs->is_implicit;
2203 * vmw_du_connector_atomic_get_property - Atomic version of get property
2205 * @connector - connector the property is associated with
2208 * Zero on success, negative errno on failure.
2211 vmw_du_connector_atomic_get_property(struct drm_connector *connector,
2212 const struct drm_connector_state *state,
2213 struct drm_property *property,
2216 struct vmw_private *dev_priv = vmw_priv(connector->dev);
2217 struct vmw_connector_state *vcs = vmw_connector_state_to_vcs(state);
2219 if (property == dev_priv->implicit_placement_property)
2220 *val = vcs->is_implicit;
2222 DRM_ERROR("Invalid Property %s\n", property->name);
2230 int vmw_kms_update_layout_ioctl(struct drm_device *dev, void *data,
2231 struct drm_file *file_priv)
2233 struct vmw_private *dev_priv = vmw_priv(dev);
2234 struct drm_vmw_update_layout_arg *arg =
2235 (struct drm_vmw_update_layout_arg *)data;
2236 void __user *user_rects;
2237 struct drm_vmw_rect *rects;
2238 unsigned rects_size;
2241 u64 total_pixels = 0;
2242 struct drm_mode_config *mode_config = &dev->mode_config;
2243 struct drm_vmw_rect bounding_box = {0};
2245 if (!arg->num_outputs) {
2246 struct drm_vmw_rect def_rect = {0, 0, 800, 600};
2247 vmw_du_update_layout(dev_priv, 1, &def_rect);
2251 rects_size = arg->num_outputs * sizeof(struct drm_vmw_rect);
2252 rects = kcalloc(arg->num_outputs, sizeof(struct drm_vmw_rect),
2254 if (unlikely(!rects))
2257 user_rects = (void __user *)(unsigned long)arg->rects;
2258 ret = copy_from_user(rects, user_rects, rects_size);
2259 if (unlikely(ret != 0)) {
2260 DRM_ERROR("Failed to get rects.\n");
2265 for (i = 0; i < arg->num_outputs; ++i) {
2266 if (rects[i].x < 0 ||
2268 rects[i].x + rects[i].w > mode_config->max_width ||
2269 rects[i].y + rects[i].h > mode_config->max_height) {
2270 DRM_ERROR("Invalid GUI layout.\n");
2276 * bounding_box.w and bunding_box.h are used as
2277 * lower-right coordinates
2279 if (rects[i].x + rects[i].w > bounding_box.w)
2280 bounding_box.w = rects[i].x + rects[i].w;
2282 if (rects[i].y + rects[i].h > bounding_box.h)
2283 bounding_box.h = rects[i].y + rects[i].h;
2285 total_pixels += (u64) rects[i].w * (u64) rects[i].h;
2288 if (dev_priv->active_display_unit == vmw_du_screen_target) {
2290 * For Screen Targets, the limits for a toplogy are:
2291 * 1. Bounding box (assuming 32bpp) must be < prim_bb_mem
2292 * 2. Total pixels (assuming 32bpp) must be < prim_bb_mem
2294 u64 bb_mem = (u64) bounding_box.w * bounding_box.h * 4;
2295 u64 pixel_mem = total_pixels * 4;
2297 if (bb_mem > dev_priv->prim_bb_mem) {
2298 DRM_ERROR("Topology is beyond supported limits.\n");
2303 if (pixel_mem > dev_priv->prim_bb_mem) {
2304 DRM_ERROR("Combined output size too large\n");
2310 vmw_du_update_layout(dev_priv, arg->num_outputs, rects);
2318 * vmw_kms_helper_dirty - Helper to build commands and perform actions based
2319 * on a set of cliprects and a set of display units.
2321 * @dev_priv: Pointer to a device private structure.
2322 * @framebuffer: Pointer to the framebuffer on which to perform the actions.
2323 * @clips: A set of struct drm_clip_rect. Either this os @vclips must be NULL.
2324 * Cliprects are given in framebuffer coordinates.
2325 * @vclips: A set of struct drm_vmw_rect cliprects. Either this or @clips must
2326 * be NULL. Cliprects are given in source coordinates.
2327 * @dest_x: X coordinate offset for the crtc / destination clip rects.
2328 * @dest_y: Y coordinate offset for the crtc / destination clip rects.
2329 * @num_clips: Number of cliprects in the @clips or @vclips array.
2330 * @increment: Integer with which to increment the clip counter when looping.
2331 * Used to skip a predetermined number of clip rects.
2332 * @dirty: Closure structure. See the description of struct vmw_kms_dirty.
2334 int vmw_kms_helper_dirty(struct vmw_private *dev_priv,
2335 struct vmw_framebuffer *framebuffer,
2336 const struct drm_clip_rect *clips,
2337 const struct drm_vmw_rect *vclips,
2338 s32 dest_x, s32 dest_y,
2341 struct vmw_kms_dirty *dirty)
2343 struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
2344 struct drm_crtc *crtc;
2348 dirty->dev_priv = dev_priv;
2350 list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list, head) {
2351 if (crtc->primary->fb != &framebuffer->base)
2353 units[num_units++] = vmw_crtc_to_du(crtc);
2356 for (k = 0; k < num_units; k++) {
2357 struct vmw_display_unit *unit = units[k];
2358 s32 crtc_x = unit->crtc.x;
2359 s32 crtc_y = unit->crtc.y;
2360 s32 crtc_width = unit->crtc.mode.hdisplay;
2361 s32 crtc_height = unit->crtc.mode.vdisplay;
2362 const struct drm_clip_rect *clips_ptr = clips;
2363 const struct drm_vmw_rect *vclips_ptr = vclips;
2366 if (dirty->fifo_reserve_size > 0) {
2367 dirty->cmd = vmw_fifo_reserve(dev_priv,
2368 dirty->fifo_reserve_size);
2370 DRM_ERROR("Couldn't reserve fifo space "
2371 "for dirty blits.\n");
2374 memset(dirty->cmd, 0, dirty->fifo_reserve_size);
2376 dirty->num_hits = 0;
2377 for (i = 0; i < num_clips; i++, clips_ptr += increment,
2378 vclips_ptr += increment) {
2383 * Select clip array type. Note that integer type
2384 * in @clips is unsigned short, whereas in @vclips
2388 dirty->fb_x = (s32) clips_ptr->x1;
2389 dirty->fb_y = (s32) clips_ptr->y1;
2390 dirty->unit_x2 = (s32) clips_ptr->x2 + dest_x -
2392 dirty->unit_y2 = (s32) clips_ptr->y2 + dest_y -
2395 dirty->fb_x = vclips_ptr->x;
2396 dirty->fb_y = vclips_ptr->y;
2397 dirty->unit_x2 = dirty->fb_x + vclips_ptr->w +
2399 dirty->unit_y2 = dirty->fb_y + vclips_ptr->h +
2403 dirty->unit_x1 = dirty->fb_x + dest_x - crtc_x;
2404 dirty->unit_y1 = dirty->fb_y + dest_y - crtc_y;
2406 /* Skip this clip if it's outside the crtc region */
2407 if (dirty->unit_x1 >= crtc_width ||
2408 dirty->unit_y1 >= crtc_height ||
2409 dirty->unit_x2 <= 0 || dirty->unit_y2 <= 0)
2412 /* Clip right and bottom to crtc limits */
2413 dirty->unit_x2 = min_t(s32, dirty->unit_x2,
2415 dirty->unit_y2 = min_t(s32, dirty->unit_y2,
2418 /* Clip left and top to crtc limits */
2419 clip_left = min_t(s32, dirty->unit_x1, 0);
2420 clip_top = min_t(s32, dirty->unit_y1, 0);
2421 dirty->unit_x1 -= clip_left;
2422 dirty->unit_y1 -= clip_top;
2423 dirty->fb_x -= clip_left;
2424 dirty->fb_y -= clip_top;
2429 dirty->fifo_commit(dirty);
2436 * vmw_kms_helper_buffer_prepare - Reserve and validate a buffer object before
2437 * command submission.
2439 * @dev_priv. Pointer to a device private structure.
2440 * @buf: The buffer object
2441 * @interruptible: Whether to perform waits as interruptible.
2442 * @validate_as_mob: Whether the buffer should be validated as a MOB. If false,
2443 * The buffer will be validated as a GMR. Already pinned buffers will not be
2446 * Returns 0 on success, negative error code on failure, -ERESTARTSYS if
2447 * interrupted by a signal.
2449 int vmw_kms_helper_buffer_prepare(struct vmw_private *dev_priv,
2450 struct vmw_dma_buffer *buf,
2452 bool validate_as_mob)
2454 struct ttm_buffer_object *bo = &buf->base;
2457 ttm_bo_reserve(bo, false, false, NULL);
2458 ret = vmw_validate_single_buffer(dev_priv, bo, interruptible,
2461 ttm_bo_unreserve(bo);
2467 * vmw_kms_helper_buffer_revert - Undo the actions of
2468 * vmw_kms_helper_buffer_prepare.
2470 * @res: Pointer to the buffer object.
2472 * Helper to be used if an error forces the caller to undo the actions of
2473 * vmw_kms_helper_buffer_prepare.
2475 void vmw_kms_helper_buffer_revert(struct vmw_dma_buffer *buf)
2478 ttm_bo_unreserve(&buf->base);
2482 * vmw_kms_helper_buffer_finish - Unreserve and fence a buffer object after
2483 * kms command submission.
2485 * @dev_priv: Pointer to a device private structure.
2486 * @file_priv: Pointer to a struct drm_file representing the caller's
2487 * connection. Must be set to NULL if @user_fence_rep is NULL, and conversely
2488 * if non-NULL, @user_fence_rep must be non-NULL.
2489 * @buf: The buffer object.
2490 * @out_fence: Optional pointer to a fence pointer. If non-NULL, a
2491 * ref-counted fence pointer is returned here.
2492 * @user_fence_rep: Optional pointer to a user-space provided struct
2493 * drm_vmw_fence_rep. If provided, @file_priv must also be provided and the
2494 * function copies fence data to user-space in a fail-safe manner.
2496 void vmw_kms_helper_buffer_finish(struct vmw_private *dev_priv,
2497 struct drm_file *file_priv,
2498 struct vmw_dma_buffer *buf,
2499 struct vmw_fence_obj **out_fence,
2500 struct drm_vmw_fence_rep __user *
2503 struct vmw_fence_obj *fence;
2507 ret = vmw_execbuf_fence_commands(file_priv, dev_priv, &fence,
2508 file_priv ? &handle : NULL);
2510 vmw_fence_single_bo(&buf->base, fence);
2512 vmw_execbuf_copy_fence_user(dev_priv, vmw_fpriv(file_priv),
2513 ret, user_fence_rep, fence,
2518 vmw_fence_obj_unreference(&fence);
2520 vmw_kms_helper_buffer_revert(buf);
2525 * vmw_kms_helper_resource_revert - Undo the actions of
2526 * vmw_kms_helper_resource_prepare.
2528 * @res: Pointer to the resource. Typically a surface.
2530 * Helper to be used if an error forces the caller to undo the actions of
2531 * vmw_kms_helper_resource_prepare.
2533 void vmw_kms_helper_resource_revert(struct vmw_validation_ctx *ctx)
2535 struct vmw_resource *res = ctx->res;
2537 vmw_kms_helper_buffer_revert(ctx->buf);
2538 vmw_dmabuf_unreference(&ctx->buf);
2539 vmw_resource_unreserve(res, false, NULL, 0);
2540 mutex_unlock(&res->dev_priv->cmdbuf_mutex);
2544 * vmw_kms_helper_resource_prepare - Reserve and validate a resource before
2545 * command submission.
2547 * @res: Pointer to the resource. Typically a surface.
2548 * @interruptible: Whether to perform waits as interruptible.
2550 * Reserves and validates also the backup buffer if a guest-backed resource.
2551 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
2552 * interrupted by a signal.
2554 int vmw_kms_helper_resource_prepare(struct vmw_resource *res,
2556 struct vmw_validation_ctx *ctx)
2564 ret = mutex_lock_interruptible(&res->dev_priv->cmdbuf_mutex);
2566 mutex_lock(&res->dev_priv->cmdbuf_mutex);
2568 if (unlikely(ret != 0))
2569 return -ERESTARTSYS;
2571 ret = vmw_resource_reserve(res, interruptible, false);
2576 ret = vmw_kms_helper_buffer_prepare(res->dev_priv, res->backup,
2578 res->dev_priv->has_mob);
2582 ctx->buf = vmw_dmabuf_reference(res->backup);
2584 ret = vmw_resource_validate(res);
2590 vmw_kms_helper_buffer_revert(ctx->buf);
2592 vmw_resource_unreserve(res, false, NULL, 0);
2594 mutex_unlock(&res->dev_priv->cmdbuf_mutex);
2599 * vmw_kms_helper_resource_finish - Unreserve and fence a resource after
2600 * kms command submission.
2602 * @res: Pointer to the resource. Typically a surface.
2603 * @out_fence: Optional pointer to a fence pointer. If non-NULL, a
2604 * ref-counted fence pointer is returned here.
2606 void vmw_kms_helper_resource_finish(struct vmw_validation_ctx *ctx,
2607 struct vmw_fence_obj **out_fence)
2609 struct vmw_resource *res = ctx->res;
2611 if (ctx->buf || out_fence)
2612 vmw_kms_helper_buffer_finish(res->dev_priv, NULL, ctx->buf,
2615 vmw_dmabuf_unreference(&ctx->buf);
2616 vmw_resource_unreserve(res, false, NULL, 0);
2617 mutex_unlock(&res->dev_priv->cmdbuf_mutex);
2621 * vmw_kms_update_proxy - Helper function to update a proxy surface from
2624 * @res: Pointer to the surface resource
2625 * @clips: Clip rects in framebuffer (surface) space.
2626 * @num_clips: Number of clips in @clips.
2627 * @increment: Integer with which to increment the clip counter when looping.
2628 * Used to skip a predetermined number of clip rects.
2630 * This function makes sure the proxy surface is updated from its backing MOB
2631 * using the region given by @clips. The surface resource @res and its backing
2632 * MOB needs to be reserved and validated on call.
2634 int vmw_kms_update_proxy(struct vmw_resource *res,
2635 const struct drm_clip_rect *clips,
2639 struct vmw_private *dev_priv = res->dev_priv;
2640 struct drm_vmw_size *size = &vmw_res_to_srf(res)->base_size;
2642 SVGA3dCmdHeader header;
2643 SVGA3dCmdUpdateGBImage body;
2646 size_t copy_size = 0;
2652 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd) * num_clips);
2654 DRM_ERROR("Couldn't reserve fifo space for proxy surface "
2659 for (i = 0; i < num_clips; ++i, clips += increment, ++cmd) {
2660 box = &cmd->body.box;
2662 cmd->header.id = SVGA_3D_CMD_UPDATE_GB_IMAGE;
2663 cmd->header.size = sizeof(cmd->body);
2664 cmd->body.image.sid = res->id;
2665 cmd->body.image.face = 0;
2666 cmd->body.image.mipmap = 0;
2668 if (clips->x1 > size->width || clips->x2 > size->width ||
2669 clips->y1 > size->height || clips->y2 > size->height) {
2670 DRM_ERROR("Invalid clips outsize of framebuffer.\n");
2677 box->w = clips->x2 - clips->x1;
2678 box->h = clips->y2 - clips->y1;
2681 copy_size += sizeof(*cmd);
2684 vmw_fifo_commit(dev_priv, copy_size);
2689 int vmw_kms_fbdev_init_data(struct vmw_private *dev_priv,
2693 struct drm_connector **p_con,
2694 struct drm_crtc **p_crtc,
2695 struct drm_display_mode **p_mode)
2697 struct drm_connector *con;
2698 struct vmw_display_unit *du;
2699 struct drm_display_mode *mode;
2702 list_for_each_entry(con, &dev_priv->dev->mode_config.connector_list,
2710 if (&con->head == &dev_priv->dev->mode_config.connector_list) {
2711 DRM_ERROR("Could not find initial display unit.\n");
2715 if (list_empty(&con->modes))
2716 (void) vmw_du_connector_fill_modes(con, max_width, max_height);
2718 if (list_empty(&con->modes)) {
2719 DRM_ERROR("Could not find initial display mode.\n");
2723 du = vmw_connector_to_du(con);
2725 *p_crtc = &du->crtc;
2727 list_for_each_entry(mode, &con->modes, head) {
2728 if (mode->type & DRM_MODE_TYPE_PREFERRED)
2732 if (&mode->head == &con->modes) {
2733 WARN_ONCE(true, "Could not find initial preferred mode.\n");
2734 *p_mode = list_first_entry(&con->modes,
2735 struct drm_display_mode,
2745 * vmw_kms_del_active - unregister a crtc binding to the implicit framebuffer
2747 * @dev_priv: Pointer to a device private struct.
2748 * @du: The display unit of the crtc.
2750 void vmw_kms_del_active(struct vmw_private *dev_priv,
2751 struct vmw_display_unit *du)
2753 mutex_lock(&dev_priv->global_kms_state_mutex);
2754 if (du->active_implicit) {
2755 if (--(dev_priv->num_implicit) == 0)
2756 dev_priv->implicit_fb = NULL;
2757 du->active_implicit = false;
2759 mutex_unlock(&dev_priv->global_kms_state_mutex);
2763 * vmw_kms_add_active - register a crtc binding to an implicit framebuffer
2765 * @vmw_priv: Pointer to a device private struct.
2766 * @du: The display unit of the crtc.
2767 * @vfb: The implicit framebuffer
2769 * Registers a binding to an implicit framebuffer.
2771 void vmw_kms_add_active(struct vmw_private *dev_priv,
2772 struct vmw_display_unit *du,
2773 struct vmw_framebuffer *vfb)
2775 mutex_lock(&dev_priv->global_kms_state_mutex);
2776 WARN_ON_ONCE(!dev_priv->num_implicit && dev_priv->implicit_fb);
2778 if (!du->active_implicit && du->is_implicit) {
2779 dev_priv->implicit_fb = vfb;
2780 du->active_implicit = true;
2781 dev_priv->num_implicit++;
2783 mutex_unlock(&dev_priv->global_kms_state_mutex);
2787 * vmw_kms_screen_object_flippable - Check whether we can page-flip a crtc.
2789 * @dev_priv: Pointer to device-private struct.
2790 * @crtc: The crtc we want to flip.
2792 * Returns true or false depending whether it's OK to flip this crtc
2793 * based on the criterion that we must not have more than one implicit
2794 * frame-buffer at any one time.
2796 bool vmw_kms_crtc_flippable(struct vmw_private *dev_priv,
2797 struct drm_crtc *crtc)
2799 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
2802 mutex_lock(&dev_priv->global_kms_state_mutex);
2803 ret = !du->is_implicit || dev_priv->num_implicit == 1;
2804 mutex_unlock(&dev_priv->global_kms_state_mutex);
2810 * vmw_kms_update_implicit_fb - Update the implicit fb.
2812 * @dev_priv: Pointer to device-private struct.
2813 * @crtc: The crtc the new implicit frame-buffer is bound to.
2815 void vmw_kms_update_implicit_fb(struct vmw_private *dev_priv,
2816 struct drm_crtc *crtc)
2818 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
2819 struct vmw_framebuffer *vfb;
2821 mutex_lock(&dev_priv->global_kms_state_mutex);
2823 if (!du->is_implicit)
2826 vfb = vmw_framebuffer_to_vfb(crtc->primary->fb);
2827 WARN_ON_ONCE(dev_priv->num_implicit != 1 &&
2828 dev_priv->implicit_fb != vfb);
2830 dev_priv->implicit_fb = vfb;
2832 mutex_unlock(&dev_priv->global_kms_state_mutex);
2836 * vmw_kms_create_implicit_placement_proparty - Set up the implicit placement
2839 * @dev_priv: Pointer to a device private struct.
2840 * @immutable: Whether the property is immutable.
2842 * Sets up the implicit placement property unless it's already set up.
2845 vmw_kms_create_implicit_placement_property(struct vmw_private *dev_priv,
2848 if (dev_priv->implicit_placement_property)
2851 dev_priv->implicit_placement_property =
2852 drm_property_create_range(dev_priv->dev,
2854 DRM_MODE_PROP_IMMUTABLE : 0,
2855 "implicit_placement", 0, 1);
2861 * vmw_kms_set_config - Wrapper around drm_atomic_helper_set_config
2863 * @set: The configuration to set.
2865 * The vmwgfx Xorg driver doesn't assign the mode::type member, which
2866 * when drm_mode_set_crtcinfo is called as part of the configuration setting
2867 * causes it to return incorrect crtc dimensions causing severe problems in
2868 * the vmwgfx modesetting. So explicitly clear that member before calling
2869 * into drm_atomic_helper_set_config.
2871 int vmw_kms_set_config(struct drm_mode_set *set,
2872 struct drm_modeset_acquire_ctx *ctx)
2874 if (set && set->mode)
2875 set->mode->type = 0;
2877 return drm_atomic_helper_set_config(set, ctx);
2882 * vmw_kms_lost_device - Notify kms that modesetting capabilities will be lost
2884 * @dev: Pointer to the drm device
2886 void vmw_kms_lost_device(struct drm_device *dev)
2888 drm_atomic_helper_shutdown(dev);