bpf: Prevent memory disambiguation attack
[linux/fpc-iii.git] / drivers / gpu / drm / vmwgfx / vmwgfx_kms.c
blobaf9fd49c22e650000ead6735ad88cfb75701f312
1 /**************************************************************************
3 * Copyright © 2009-2015 VMware, Inc., Palo Alto, CA., USA
4 * All Rights Reserved.
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
16 * of the Software.
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)
56 struct {
57 u32 cmd;
58 SVGAFifoCmdDefineAlphaCursor cursor;
59 } *cmd;
60 u32 image_size = width * height * 4;
61 u32 cmd_size = sizeof(*cmd) + image_size;
63 if (!image)
64 return -EINVAL;
66 cmd = vmw_fifo_reserve(dev_priv, cmd_size);
67 if (unlikely(cmd == NULL)) {
68 DRM_ERROR("Fifo reserve failed.\n");
69 return -ENOMEM;
72 memset(cmd, 0, sizeof(*cmd));
74 memcpy(&cmd[1], image, image_size);
76 cmd->cmd = SVGA_CMD_DEFINE_ALPHA_CURSOR;
77 cmd->cursor.id = 0;
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);
85 return 0;
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;
96 void *virtual;
97 bool dummy;
98 int ret;
100 kmap_offset = 0;
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");
106 return -EINVAL;
109 ret = ttm_bo_kmap(&dmabuf->base, kmap_offset, kmap_num, &map);
110 if (unlikely(ret != 0))
111 goto err_unreserve;
113 virtual = ttm_kmap_obj_virtual(&map, &dummy);
114 ret = vmw_cursor_update_image(dev_priv, virtual, width, height,
115 hotspotX, hotspotY);
117 ttm_bo_kunmap(&map);
118 err_unreserve:
119 ttm_bo_unreserve(&dmabuf->base);
121 return ret;
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;
129 uint32_t count;
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;
149 SVGA3dCopyBox *box;
150 unsigned box_count;
151 void *virtual;
152 bool dummy;
153 struct vmw_dma_cmd {
154 SVGA3dCmdHeader header;
155 SVGA3dCmdSurfaceDMA dma;
156 } *cmd;
157 int i, ret;
159 cmd = container_of(header, struct vmw_dma_cmd, header);
161 /* No snooper installed */
162 if (!srf->snooper.image)
163 return;
165 if (cmd->dma.host.face != 0 || cmd->dma.host.mipmap != 0) {
166 DRM_ERROR("face and mipmap for cursors should never != 0\n");
167 return;
170 if (cmd->header.size < 64) {
171 DRM_ERROR("at least one full copy box must be given\n");
172 return;
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);
192 return;
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");
201 return;
204 ret = ttm_bo_kmap(bo, kmap_offset, kmap_num, &map);
205 if (unlikely(ret != 0))
206 goto err_unreserve;
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);
212 } else {
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,
217 box->w * 4);
220 srf->snooper.age++;
222 ttm_bo_kunmap(&map);
223 err_unreserve:
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);
244 du->hotspot_x = 0;
245 du->hotspot_y = 0;
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)
262 continue;
264 du->cursor_age = du->cursor_surface->snooper.age;
265 vmw_cursor_update_image(dev_priv,
266 du->cursor_surface->snooper.image,
267 64, 64,
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,
299 bool unreference)
301 if (vps->surf) {
302 if (vps->pinned) {
303 vmw_resource_unpin(&vps->surf->res);
304 vps->pinned--;
307 if (unreference) {
308 if (vps->pinned)
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
326 void
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);
352 if (vps->surf)
353 vmw_surface_unreference(&vps->surf);
355 if (vps->dmabuf)
356 vmw_dmabuf_unreference(&vps->dmabuf);
358 if (fb) {
359 if (vmw_framebuffer_to_vfb(fb)->dmabuf) {
360 vps->dmabuf = vmw_framebuffer_to_vfbd(fb)->buffer;
361 vmw_dmabuf_reference(vps->dmabuf);
362 } else {
363 vps->surf = vmw_framebuffer_to_vfbs(fb)->surface;
364 vmw_surface_reference(vps->surf);
368 return 0;
372 void
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;
381 int ret = 0;
384 hotspot_x = du->hotspot_x;
385 hotspot_y = du->hotspot_y;
387 if (plane->fb) {
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 */
396 if (vps->surf) {
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);
407 } else {
408 vmw_cursor_update_position(dev_priv, false, 0, 0);
409 return;
412 if (!ret) {
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;
422 } else {
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_crtc_state *crtc_state = NULL;
444 struct drm_framebuffer *new_fb = state->fb;
445 struct drm_rect clip = {};
446 int ret;
448 if (state->crtc)
449 crtc_state = drm_atomic_get_new_crtc_state(state->state, state->crtc);
451 if (crtc_state && crtc_state->enable) {
452 clip.x2 = crtc_state->adjusted_mode.hdisplay;
453 clip.y2 = crtc_state->adjusted_mode.vdisplay;
456 ret = drm_atomic_helper_check_plane_state(state, crtc_state, &clip,
457 DRM_PLANE_HELPER_NO_SCALING,
458 DRM_PLANE_HELPER_NO_SCALING,
459 false, true);
461 if (!ret && new_fb) {
462 struct drm_crtc *crtc = state->crtc;
463 struct vmw_connector_state *vcs;
464 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
465 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
466 struct vmw_framebuffer *vfb = vmw_framebuffer_to_vfb(new_fb);
468 vcs = vmw_connector_state_to_vcs(du->connector.state);
470 /* Only one active implicit framebuffer at a time. */
471 mutex_lock(&dev_priv->global_kms_state_mutex);
472 if (vcs->is_implicit && dev_priv->implicit_fb &&
473 !(dev_priv->num_implicit == 1 && du->active_implicit)
474 && dev_priv->implicit_fb != vfb) {
475 DRM_ERROR("Multiple implicit framebuffers "
476 "not supported.\n");
477 ret = -EINVAL;
479 mutex_unlock(&dev_priv->global_kms_state_mutex);
483 return ret;
488 * vmw_du_cursor_plane_atomic_check - check if the new state is okay
490 * @plane: cursor plane
491 * @state: info on the new plane state
493 * This is a chance to fail if the new cursor state does not fit
494 * our requirements.
496 * Returns 0 on success
498 int vmw_du_cursor_plane_atomic_check(struct drm_plane *plane,
499 struct drm_plane_state *new_state)
501 int ret = 0;
502 struct vmw_surface *surface = NULL;
503 struct drm_framebuffer *fb = new_state->fb;
506 /* Turning off */
507 if (!fb)
508 return ret;
510 /* A lot of the code assumes this */
511 if (new_state->crtc_w != 64 || new_state->crtc_h != 64) {
512 DRM_ERROR("Invalid cursor dimensions (%d, %d)\n",
513 new_state->crtc_w, new_state->crtc_h);
514 ret = -EINVAL;
517 if (!vmw_framebuffer_to_vfb(fb)->dmabuf)
518 surface = vmw_framebuffer_to_vfbs(fb)->surface;
520 if (surface && !surface->snooper.image) {
521 DRM_ERROR("surface not suitable for cursor\n");
522 ret = -EINVAL;
525 return ret;
529 int vmw_du_crtc_atomic_check(struct drm_crtc *crtc,
530 struct drm_crtc_state *new_state)
532 struct vmw_display_unit *du = vmw_crtc_to_du(new_state->crtc);
533 int connector_mask = 1 << drm_connector_index(&du->connector);
534 bool has_primary = new_state->plane_mask &
535 BIT(drm_plane_index(crtc->primary));
537 /* We always want to have an active plane with an active CRTC */
538 if (has_primary != new_state->enable)
539 return -EINVAL;
542 if (new_state->connector_mask != connector_mask &&
543 new_state->connector_mask != 0) {
544 DRM_ERROR("Invalid connectors configuration\n");
545 return -EINVAL;
549 * Our virtual device does not have a dot clock, so use the logical
550 * clock value as the dot clock.
552 if (new_state->mode.crtc_clock == 0)
553 new_state->adjusted_mode.crtc_clock = new_state->mode.clock;
555 return 0;
559 void vmw_du_crtc_atomic_begin(struct drm_crtc *crtc,
560 struct drm_crtc_state *old_crtc_state)
565 void vmw_du_crtc_atomic_flush(struct drm_crtc *crtc,
566 struct drm_crtc_state *old_crtc_state)
568 struct drm_pending_vblank_event *event = crtc->state->event;
570 if (event) {
571 crtc->state->event = NULL;
573 spin_lock_irq(&crtc->dev->event_lock);
574 if (drm_crtc_vblank_get(crtc) == 0)
575 drm_crtc_arm_vblank_event(crtc, event);
576 else
577 drm_crtc_send_vblank_event(crtc, event);
578 spin_unlock_irq(&crtc->dev->event_lock);
585 * vmw_du_crtc_duplicate_state - duplicate crtc state
586 * @crtc: DRM crtc
588 * Allocates and returns a copy of the crtc state (both common and
589 * vmw-specific) for the specified crtc.
591 * Returns: The newly allocated crtc state, or NULL on failure.
593 struct drm_crtc_state *
594 vmw_du_crtc_duplicate_state(struct drm_crtc *crtc)
596 struct drm_crtc_state *state;
597 struct vmw_crtc_state *vcs;
599 if (WARN_ON(!crtc->state))
600 return NULL;
602 vcs = kmemdup(crtc->state, sizeof(*vcs), GFP_KERNEL);
604 if (!vcs)
605 return NULL;
607 state = &vcs->base;
609 __drm_atomic_helper_crtc_duplicate_state(crtc, state);
611 return state;
616 * vmw_du_crtc_reset - creates a blank vmw crtc state
617 * @crtc: DRM crtc
619 * Resets the atomic state for @crtc by freeing the state pointer (which
620 * might be NULL, e.g. at driver load time) and allocating a new empty state
621 * object.
623 void vmw_du_crtc_reset(struct drm_crtc *crtc)
625 struct vmw_crtc_state *vcs;
628 if (crtc->state) {
629 __drm_atomic_helper_crtc_destroy_state(crtc->state);
631 kfree(vmw_crtc_state_to_vcs(crtc->state));
634 vcs = kzalloc(sizeof(*vcs), GFP_KERNEL);
636 if (!vcs) {
637 DRM_ERROR("Cannot allocate vmw_crtc_state\n");
638 return;
641 crtc->state = &vcs->base;
642 crtc->state->crtc = crtc;
647 * vmw_du_crtc_destroy_state - destroy crtc state
648 * @crtc: DRM crtc
649 * @state: state object to destroy
651 * Destroys the crtc state (both common and vmw-specific) for the
652 * specified plane.
654 void
655 vmw_du_crtc_destroy_state(struct drm_crtc *crtc,
656 struct drm_crtc_state *state)
658 drm_atomic_helper_crtc_destroy_state(crtc, state);
663 * vmw_du_plane_duplicate_state - duplicate plane state
664 * @plane: drm plane
666 * Allocates and returns a copy of the plane state (both common and
667 * vmw-specific) for the specified plane.
669 * Returns: The newly allocated plane state, or NULL on failure.
671 struct drm_plane_state *
672 vmw_du_plane_duplicate_state(struct drm_plane *plane)
674 struct drm_plane_state *state;
675 struct vmw_plane_state *vps;
677 vps = kmemdup(plane->state, sizeof(*vps), GFP_KERNEL);
679 if (!vps)
680 return NULL;
682 vps->pinned = 0;
684 /* Mapping is managed by prepare_fb/cleanup_fb */
685 memset(&vps->host_map, 0, sizeof(vps->host_map));
686 vps->cpp = 0;
688 /* Each ref counted resource needs to be acquired again */
689 if (vps->surf)
690 (void) vmw_surface_reference(vps->surf);
692 if (vps->dmabuf)
693 (void) vmw_dmabuf_reference(vps->dmabuf);
695 state = &vps->base;
697 __drm_atomic_helper_plane_duplicate_state(plane, state);
699 return state;
704 * vmw_du_plane_reset - creates a blank vmw plane state
705 * @plane: drm plane
707 * Resets the atomic state for @plane by freeing the state pointer (which might
708 * be NULL, e.g. at driver load time) and allocating a new empty state object.
710 void vmw_du_plane_reset(struct drm_plane *plane)
712 struct vmw_plane_state *vps;
715 if (plane->state)
716 vmw_du_plane_destroy_state(plane, plane->state);
718 vps = kzalloc(sizeof(*vps), GFP_KERNEL);
720 if (!vps) {
721 DRM_ERROR("Cannot allocate vmw_plane_state\n");
722 return;
725 plane->state = &vps->base;
726 plane->state->plane = plane;
727 plane->state->rotation = DRM_MODE_ROTATE_0;
732 * vmw_du_plane_destroy_state - destroy plane state
733 * @plane: DRM plane
734 * @state: state object to destroy
736 * Destroys the plane state (both common and vmw-specific) for the
737 * specified plane.
739 void
740 vmw_du_plane_destroy_state(struct drm_plane *plane,
741 struct drm_plane_state *state)
743 struct vmw_plane_state *vps = vmw_plane_state_to_vps(state);
746 /* Should have been freed by cleanup_fb */
747 if (vps->host_map.virtual) {
748 DRM_ERROR("Host mapping not freed\n");
749 ttm_bo_kunmap(&vps->host_map);
752 if (vps->surf)
753 vmw_surface_unreference(&vps->surf);
755 if (vps->dmabuf)
756 vmw_dmabuf_unreference(&vps->dmabuf);
758 drm_atomic_helper_plane_destroy_state(plane, state);
763 * vmw_du_connector_duplicate_state - duplicate connector state
764 * @connector: DRM connector
766 * Allocates and returns a copy of the connector state (both common and
767 * vmw-specific) for the specified connector.
769 * Returns: The newly allocated connector state, or NULL on failure.
771 struct drm_connector_state *
772 vmw_du_connector_duplicate_state(struct drm_connector *connector)
774 struct drm_connector_state *state;
775 struct vmw_connector_state *vcs;
777 if (WARN_ON(!connector->state))
778 return NULL;
780 vcs = kmemdup(connector->state, sizeof(*vcs), GFP_KERNEL);
782 if (!vcs)
783 return NULL;
785 state = &vcs->base;
787 __drm_atomic_helper_connector_duplicate_state(connector, state);
789 return state;
794 * vmw_du_connector_reset - creates a blank vmw connector state
795 * @connector: DRM connector
797 * Resets the atomic state for @connector by freeing the state pointer (which
798 * might be NULL, e.g. at driver load time) and allocating a new empty state
799 * object.
801 void vmw_du_connector_reset(struct drm_connector *connector)
803 struct vmw_connector_state *vcs;
806 if (connector->state) {
807 __drm_atomic_helper_connector_destroy_state(connector->state);
809 kfree(vmw_connector_state_to_vcs(connector->state));
812 vcs = kzalloc(sizeof(*vcs), GFP_KERNEL);
814 if (!vcs) {
815 DRM_ERROR("Cannot allocate vmw_connector_state\n");
816 return;
819 __drm_atomic_helper_connector_reset(connector, &vcs->base);
824 * vmw_du_connector_destroy_state - destroy connector state
825 * @connector: DRM connector
826 * @state: state object to destroy
828 * Destroys the connector state (both common and vmw-specific) for the
829 * specified plane.
831 void
832 vmw_du_connector_destroy_state(struct drm_connector *connector,
833 struct drm_connector_state *state)
835 drm_atomic_helper_connector_destroy_state(connector, state);
838 * Generic framebuffer code
842 * Surface framebuffer code
845 static void vmw_framebuffer_surface_destroy(struct drm_framebuffer *framebuffer)
847 struct vmw_framebuffer_surface *vfbs =
848 vmw_framebuffer_to_vfbs(framebuffer);
850 drm_framebuffer_cleanup(framebuffer);
851 vmw_surface_unreference(&vfbs->surface);
852 if (vfbs->base.user_obj)
853 ttm_base_object_unref(&vfbs->base.user_obj);
855 kfree(vfbs);
858 static int vmw_framebuffer_surface_dirty(struct drm_framebuffer *framebuffer,
859 struct drm_file *file_priv,
860 unsigned flags, unsigned color,
861 struct drm_clip_rect *clips,
862 unsigned num_clips)
864 struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
865 struct vmw_framebuffer_surface *vfbs =
866 vmw_framebuffer_to_vfbs(framebuffer);
867 struct drm_clip_rect norect;
868 int ret, inc = 1;
870 /* Legacy Display Unit does not support 3D */
871 if (dev_priv->active_display_unit == vmw_du_legacy)
872 return -EINVAL;
874 drm_modeset_lock_all(dev_priv->dev);
876 ret = ttm_read_lock(&dev_priv->reservation_sem, true);
877 if (unlikely(ret != 0)) {
878 drm_modeset_unlock_all(dev_priv->dev);
879 return ret;
882 if (!num_clips) {
883 num_clips = 1;
884 clips = &norect;
885 norect.x1 = norect.y1 = 0;
886 norect.x2 = framebuffer->width;
887 norect.y2 = framebuffer->height;
888 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
889 num_clips /= 2;
890 inc = 2; /* skip source rects */
893 if (dev_priv->active_display_unit == vmw_du_screen_object)
894 ret = vmw_kms_sou_do_surface_dirty(dev_priv, &vfbs->base,
895 clips, NULL, NULL, 0, 0,
896 num_clips, inc, NULL);
897 else
898 ret = vmw_kms_stdu_surface_dirty(dev_priv, &vfbs->base,
899 clips, NULL, NULL, 0, 0,
900 num_clips, inc, NULL);
902 vmw_fifo_flush(dev_priv, false);
903 ttm_read_unlock(&dev_priv->reservation_sem);
905 drm_modeset_unlock_all(dev_priv->dev);
907 return 0;
911 * vmw_kms_readback - Perform a readback from the screen system to
912 * a dma-buffer backed framebuffer.
914 * @dev_priv: Pointer to the device private structure.
915 * @file_priv: Pointer to a struct drm_file identifying the caller.
916 * Must be set to NULL if @user_fence_rep is NULL.
917 * @vfb: Pointer to the dma-buffer backed framebuffer.
918 * @user_fence_rep: User-space provided structure for fence information.
919 * Must be set to non-NULL if @file_priv is non-NULL.
920 * @vclips: Array of clip rects.
921 * @num_clips: Number of clip rects in @vclips.
923 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
924 * interrupted.
926 int vmw_kms_readback(struct vmw_private *dev_priv,
927 struct drm_file *file_priv,
928 struct vmw_framebuffer *vfb,
929 struct drm_vmw_fence_rep __user *user_fence_rep,
930 struct drm_vmw_rect *vclips,
931 uint32_t num_clips)
933 switch (dev_priv->active_display_unit) {
934 case vmw_du_screen_object:
935 return vmw_kms_sou_readback(dev_priv, file_priv, vfb,
936 user_fence_rep, vclips, num_clips);
937 case vmw_du_screen_target:
938 return vmw_kms_stdu_dma(dev_priv, file_priv, vfb,
939 user_fence_rep, NULL, vclips, num_clips,
940 1, false, true);
941 default:
942 WARN_ONCE(true,
943 "Readback called with invalid display system.\n");
946 return -ENOSYS;
950 static const struct drm_framebuffer_funcs vmw_framebuffer_surface_funcs = {
951 .destroy = vmw_framebuffer_surface_destroy,
952 .dirty = vmw_framebuffer_surface_dirty,
955 static int vmw_kms_new_framebuffer_surface(struct vmw_private *dev_priv,
956 struct vmw_surface *surface,
957 struct vmw_framebuffer **out,
958 const struct drm_mode_fb_cmd2
959 *mode_cmd,
960 bool is_dmabuf_proxy)
963 struct drm_device *dev = dev_priv->dev;
964 struct vmw_framebuffer_surface *vfbs;
965 enum SVGA3dSurfaceFormat format;
966 int ret;
967 struct drm_format_name_buf format_name;
969 /* 3D is only supported on HWv8 and newer hosts */
970 if (dev_priv->active_display_unit == vmw_du_legacy)
971 return -ENOSYS;
974 * Sanity checks.
977 /* Surface must be marked as a scanout. */
978 if (unlikely(!surface->scanout))
979 return -EINVAL;
981 if (unlikely(surface->mip_levels[0] != 1 ||
982 surface->num_sizes != 1 ||
983 surface->base_size.width < mode_cmd->width ||
984 surface->base_size.height < mode_cmd->height ||
985 surface->base_size.depth != 1)) {
986 DRM_ERROR("Incompatible surface dimensions "
987 "for requested mode.\n");
988 return -EINVAL;
991 switch (mode_cmd->pixel_format) {
992 case DRM_FORMAT_ARGB8888:
993 format = SVGA3D_A8R8G8B8;
994 break;
995 case DRM_FORMAT_XRGB8888:
996 format = SVGA3D_X8R8G8B8;
997 break;
998 case DRM_FORMAT_RGB565:
999 format = SVGA3D_R5G6B5;
1000 break;
1001 case DRM_FORMAT_XRGB1555:
1002 format = SVGA3D_A1R5G5B5;
1003 break;
1004 default:
1005 DRM_ERROR("Invalid pixel format: %s\n",
1006 drm_get_format_name(mode_cmd->pixel_format, &format_name));
1007 return -EINVAL;
1011 * For DX, surface format validation is done when surface->scanout
1012 * is set.
1014 if (!dev_priv->has_dx && format != surface->format) {
1015 DRM_ERROR("Invalid surface format for requested mode.\n");
1016 return -EINVAL;
1019 vfbs = kzalloc(sizeof(*vfbs), GFP_KERNEL);
1020 if (!vfbs) {
1021 ret = -ENOMEM;
1022 goto out_err1;
1025 drm_helper_mode_fill_fb_struct(dev, &vfbs->base.base, mode_cmd);
1026 vfbs->surface = vmw_surface_reference(surface);
1027 vfbs->base.user_handle = mode_cmd->handles[0];
1028 vfbs->is_dmabuf_proxy = is_dmabuf_proxy;
1030 *out = &vfbs->base;
1032 ret = drm_framebuffer_init(dev, &vfbs->base.base,
1033 &vmw_framebuffer_surface_funcs);
1034 if (ret)
1035 goto out_err2;
1037 return 0;
1039 out_err2:
1040 vmw_surface_unreference(&surface);
1041 kfree(vfbs);
1042 out_err1:
1043 return ret;
1047 * Dmabuf framebuffer code
1050 static void vmw_framebuffer_dmabuf_destroy(struct drm_framebuffer *framebuffer)
1052 struct vmw_framebuffer_dmabuf *vfbd =
1053 vmw_framebuffer_to_vfbd(framebuffer);
1055 drm_framebuffer_cleanup(framebuffer);
1056 vmw_dmabuf_unreference(&vfbd->buffer);
1057 if (vfbd->base.user_obj)
1058 ttm_base_object_unref(&vfbd->base.user_obj);
1060 kfree(vfbd);
1063 static int vmw_framebuffer_dmabuf_dirty(struct drm_framebuffer *framebuffer,
1064 struct drm_file *file_priv,
1065 unsigned flags, unsigned color,
1066 struct drm_clip_rect *clips,
1067 unsigned num_clips)
1069 struct vmw_private *dev_priv = vmw_priv(framebuffer->dev);
1070 struct vmw_framebuffer_dmabuf *vfbd =
1071 vmw_framebuffer_to_vfbd(framebuffer);
1072 struct drm_clip_rect norect;
1073 int ret, increment = 1;
1075 drm_modeset_lock_all(dev_priv->dev);
1077 ret = ttm_read_lock(&dev_priv->reservation_sem, true);
1078 if (unlikely(ret != 0)) {
1079 drm_modeset_unlock_all(dev_priv->dev);
1080 return ret;
1083 if (!num_clips) {
1084 num_clips = 1;
1085 clips = &norect;
1086 norect.x1 = norect.y1 = 0;
1087 norect.x2 = framebuffer->width;
1088 norect.y2 = framebuffer->height;
1089 } else if (flags & DRM_MODE_FB_DIRTY_ANNOTATE_COPY) {
1090 num_clips /= 2;
1091 increment = 2;
1094 switch (dev_priv->active_display_unit) {
1095 case vmw_du_screen_target:
1096 ret = vmw_kms_stdu_dma(dev_priv, NULL, &vfbd->base, NULL,
1097 clips, NULL, num_clips, increment,
1098 true, true);
1099 break;
1100 case vmw_du_screen_object:
1101 ret = vmw_kms_sou_do_dmabuf_dirty(dev_priv, &vfbd->base,
1102 clips, NULL, num_clips,
1103 increment, true, NULL);
1104 break;
1105 case vmw_du_legacy:
1106 ret = vmw_kms_ldu_do_dmabuf_dirty(dev_priv, &vfbd->base, 0, 0,
1107 clips, num_clips, increment);
1108 break;
1109 default:
1110 ret = -EINVAL;
1111 WARN_ONCE(true, "Dirty called with invalid display system.\n");
1112 break;
1115 vmw_fifo_flush(dev_priv, false);
1116 ttm_read_unlock(&dev_priv->reservation_sem);
1118 drm_modeset_unlock_all(dev_priv->dev);
1120 return ret;
1123 static const struct drm_framebuffer_funcs vmw_framebuffer_dmabuf_funcs = {
1124 .destroy = vmw_framebuffer_dmabuf_destroy,
1125 .dirty = vmw_framebuffer_dmabuf_dirty,
1129 * Pin the dmabuffer to the start of vram.
1131 static int vmw_framebuffer_pin(struct vmw_framebuffer *vfb)
1133 struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
1134 struct vmw_dma_buffer *buf;
1135 int ret;
1137 buf = vfb->dmabuf ? vmw_framebuffer_to_vfbd(&vfb->base)->buffer :
1138 vmw_framebuffer_to_vfbs(&vfb->base)->surface->res.backup;
1140 if (!buf)
1141 return 0;
1143 switch (dev_priv->active_display_unit) {
1144 case vmw_du_legacy:
1145 vmw_overlay_pause_all(dev_priv);
1146 ret = vmw_dmabuf_pin_in_start_of_vram(dev_priv, buf, false);
1147 vmw_overlay_resume_all(dev_priv);
1148 break;
1149 case vmw_du_screen_object:
1150 case vmw_du_screen_target:
1151 if (vfb->dmabuf)
1152 return vmw_dmabuf_pin_in_vram_or_gmr(dev_priv, buf,
1153 false);
1155 return vmw_dmabuf_pin_in_placement(dev_priv, buf,
1156 &vmw_mob_placement, false);
1157 default:
1158 return -EINVAL;
1161 return ret;
1164 static int vmw_framebuffer_unpin(struct vmw_framebuffer *vfb)
1166 struct vmw_private *dev_priv = vmw_priv(vfb->base.dev);
1167 struct vmw_dma_buffer *buf;
1169 buf = vfb->dmabuf ? vmw_framebuffer_to_vfbd(&vfb->base)->buffer :
1170 vmw_framebuffer_to_vfbs(&vfb->base)->surface->res.backup;
1172 if (WARN_ON(!buf))
1173 return 0;
1175 return vmw_dmabuf_unpin(dev_priv, buf, false);
1179 * vmw_create_dmabuf_proxy - create a proxy surface for the DMA buf
1181 * @dev: DRM device
1182 * @mode_cmd: parameters for the new surface
1183 * @dmabuf_mob: MOB backing the DMA buf
1184 * @srf_out: newly created surface
1186 * When the content FB is a DMA buf, we create a surface as a proxy to the
1187 * same buffer. This way we can do a surface copy rather than a surface DMA.
1188 * This is a more efficient approach
1190 * RETURNS:
1191 * 0 on success, error code otherwise
1193 static int vmw_create_dmabuf_proxy(struct drm_device *dev,
1194 const struct drm_mode_fb_cmd2 *mode_cmd,
1195 struct vmw_dma_buffer *dmabuf_mob,
1196 struct vmw_surface **srf_out)
1198 uint32_t format;
1199 struct drm_vmw_size content_base_size = {0};
1200 struct vmw_resource *res;
1201 unsigned int bytes_pp;
1202 struct drm_format_name_buf format_name;
1203 int ret;
1205 switch (mode_cmd->pixel_format) {
1206 case DRM_FORMAT_ARGB8888:
1207 case DRM_FORMAT_XRGB8888:
1208 format = SVGA3D_X8R8G8B8;
1209 bytes_pp = 4;
1210 break;
1212 case DRM_FORMAT_RGB565:
1213 case DRM_FORMAT_XRGB1555:
1214 format = SVGA3D_R5G6B5;
1215 bytes_pp = 2;
1216 break;
1218 case 8:
1219 format = SVGA3D_P8;
1220 bytes_pp = 1;
1221 break;
1223 default:
1224 DRM_ERROR("Invalid framebuffer format %s\n",
1225 drm_get_format_name(mode_cmd->pixel_format, &format_name));
1226 return -EINVAL;
1229 content_base_size.width = mode_cmd->pitches[0] / bytes_pp;
1230 content_base_size.height = mode_cmd->height;
1231 content_base_size.depth = 1;
1233 ret = vmw_surface_gb_priv_define(dev,
1234 0, /* kernel visible only */
1235 0, /* flags */
1236 format,
1237 true, /* can be a scanout buffer */
1238 1, /* num of mip levels */
1241 content_base_size,
1242 srf_out);
1243 if (ret) {
1244 DRM_ERROR("Failed to allocate proxy content buffer\n");
1245 return ret;
1248 res = &(*srf_out)->res;
1250 /* Reserve and switch the backing mob. */
1251 mutex_lock(&res->dev_priv->cmdbuf_mutex);
1252 (void) vmw_resource_reserve(res, false, true);
1253 vmw_dmabuf_unreference(&res->backup);
1254 res->backup = vmw_dmabuf_reference(dmabuf_mob);
1255 res->backup_offset = 0;
1256 vmw_resource_unreserve(res, false, NULL, 0);
1257 mutex_unlock(&res->dev_priv->cmdbuf_mutex);
1259 return 0;
1264 static int vmw_kms_new_framebuffer_dmabuf(struct vmw_private *dev_priv,
1265 struct vmw_dma_buffer *dmabuf,
1266 struct vmw_framebuffer **out,
1267 const struct drm_mode_fb_cmd2
1268 *mode_cmd)
1271 struct drm_device *dev = dev_priv->dev;
1272 struct vmw_framebuffer_dmabuf *vfbd;
1273 unsigned int requested_size;
1274 struct drm_format_name_buf format_name;
1275 int ret;
1277 requested_size = mode_cmd->height * mode_cmd->pitches[0];
1278 if (unlikely(requested_size > dmabuf->base.num_pages * PAGE_SIZE)) {
1279 DRM_ERROR("Screen buffer object size is too small "
1280 "for requested mode.\n");
1281 return -EINVAL;
1284 /* Limited framebuffer color depth support for screen objects */
1285 if (dev_priv->active_display_unit == vmw_du_screen_object) {
1286 switch (mode_cmd->pixel_format) {
1287 case DRM_FORMAT_XRGB8888:
1288 case DRM_FORMAT_ARGB8888:
1289 break;
1290 case DRM_FORMAT_XRGB1555:
1291 case DRM_FORMAT_RGB565:
1292 break;
1293 default:
1294 DRM_ERROR("Invalid pixel format: %s\n",
1295 drm_get_format_name(mode_cmd->pixel_format, &format_name));
1296 return -EINVAL;
1300 vfbd = kzalloc(sizeof(*vfbd), GFP_KERNEL);
1301 if (!vfbd) {
1302 ret = -ENOMEM;
1303 goto out_err1;
1306 drm_helper_mode_fill_fb_struct(dev, &vfbd->base.base, mode_cmd);
1307 vfbd->base.dmabuf = true;
1308 vfbd->buffer = vmw_dmabuf_reference(dmabuf);
1309 vfbd->base.user_handle = mode_cmd->handles[0];
1310 *out = &vfbd->base;
1312 ret = drm_framebuffer_init(dev, &vfbd->base.base,
1313 &vmw_framebuffer_dmabuf_funcs);
1314 if (ret)
1315 goto out_err2;
1317 return 0;
1319 out_err2:
1320 vmw_dmabuf_unreference(&dmabuf);
1321 kfree(vfbd);
1322 out_err1:
1323 return ret;
1328 * vmw_kms_srf_ok - check if a surface can be created
1330 * @width: requested width
1331 * @height: requested height
1333 * Surfaces need to be less than texture size
1335 static bool
1336 vmw_kms_srf_ok(struct vmw_private *dev_priv, uint32_t width, uint32_t height)
1338 if (width > dev_priv->texture_max_width ||
1339 height > dev_priv->texture_max_height)
1340 return false;
1342 return true;
1346 * vmw_kms_new_framebuffer - Create a new framebuffer.
1348 * @dev_priv: Pointer to device private struct.
1349 * @dmabuf: Pointer to dma buffer to wrap the kms framebuffer around.
1350 * Either @dmabuf or @surface must be NULL.
1351 * @surface: Pointer to a surface to wrap the kms framebuffer around.
1352 * Either @dmabuf or @surface must be NULL.
1353 * @only_2d: No presents will occur to this dma buffer based framebuffer. This
1354 * Helps the code to do some important optimizations.
1355 * @mode_cmd: Frame-buffer metadata.
1357 struct vmw_framebuffer *
1358 vmw_kms_new_framebuffer(struct vmw_private *dev_priv,
1359 struct vmw_dma_buffer *dmabuf,
1360 struct vmw_surface *surface,
1361 bool only_2d,
1362 const struct drm_mode_fb_cmd2 *mode_cmd)
1364 struct vmw_framebuffer *vfb = NULL;
1365 bool is_dmabuf_proxy = false;
1366 int ret;
1369 * We cannot use the SurfaceDMA command in an non-accelerated VM,
1370 * therefore, wrap the DMA buf in a surface so we can use the
1371 * SurfaceCopy command.
1373 if (vmw_kms_srf_ok(dev_priv, mode_cmd->width, mode_cmd->height) &&
1374 dmabuf && only_2d &&
1375 mode_cmd->width > 64 && /* Don't create a proxy for cursor */
1376 dev_priv->active_display_unit == vmw_du_screen_target) {
1377 ret = vmw_create_dmabuf_proxy(dev_priv->dev, mode_cmd,
1378 dmabuf, &surface);
1379 if (ret)
1380 return ERR_PTR(ret);
1382 is_dmabuf_proxy = true;
1385 /* Create the new framebuffer depending one what we have */
1386 if (surface) {
1387 ret = vmw_kms_new_framebuffer_surface(dev_priv, surface, &vfb,
1388 mode_cmd,
1389 is_dmabuf_proxy);
1392 * vmw_create_dmabuf_proxy() adds a reference that is no longer
1393 * needed
1395 if (is_dmabuf_proxy)
1396 vmw_surface_unreference(&surface);
1397 } else if (dmabuf) {
1398 ret = vmw_kms_new_framebuffer_dmabuf(dev_priv, dmabuf, &vfb,
1399 mode_cmd);
1400 } else {
1401 BUG();
1404 if (ret)
1405 return ERR_PTR(ret);
1407 vfb->pin = vmw_framebuffer_pin;
1408 vfb->unpin = vmw_framebuffer_unpin;
1410 return vfb;
1414 * Generic Kernel modesetting functions
1417 static struct drm_framebuffer *vmw_kms_fb_create(struct drm_device *dev,
1418 struct drm_file *file_priv,
1419 const struct drm_mode_fb_cmd2 *mode_cmd)
1421 struct vmw_private *dev_priv = vmw_priv(dev);
1422 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
1423 struct vmw_framebuffer *vfb = NULL;
1424 struct vmw_surface *surface = NULL;
1425 struct vmw_dma_buffer *bo = NULL;
1426 struct ttm_base_object *user_obj;
1427 int ret;
1430 * This code should be conditioned on Screen Objects not being used.
1431 * If screen objects are used, we can allocate a GMR to hold the
1432 * requested framebuffer.
1435 if (!vmw_kms_validate_mode_vram(dev_priv,
1436 mode_cmd->pitches[0],
1437 mode_cmd->height)) {
1438 DRM_ERROR("Requested mode exceed bounding box limit.\n");
1439 return ERR_PTR(-ENOMEM);
1443 * Take a reference on the user object of the resource
1444 * backing the kms fb. This ensures that user-space handle
1445 * lookups on that resource will always work as long as
1446 * it's registered with a kms framebuffer. This is important,
1447 * since vmw_execbuf_process identifies resources in the
1448 * command stream using user-space handles.
1451 user_obj = ttm_base_object_lookup(tfile, mode_cmd->handles[0]);
1452 if (unlikely(user_obj == NULL)) {
1453 DRM_ERROR("Could not locate requested kms frame buffer.\n");
1454 return ERR_PTR(-ENOENT);
1458 * End conditioned code.
1461 /* returns either a dmabuf or surface */
1462 ret = vmw_user_lookup_handle(dev_priv, tfile,
1463 mode_cmd->handles[0],
1464 &surface, &bo);
1465 if (ret)
1466 goto err_out;
1469 if (!bo &&
1470 !vmw_kms_srf_ok(dev_priv, mode_cmd->width, mode_cmd->height)) {
1471 DRM_ERROR("Surface size cannot exceed %dx%d",
1472 dev_priv->texture_max_width,
1473 dev_priv->texture_max_height);
1474 goto err_out;
1478 vfb = vmw_kms_new_framebuffer(dev_priv, bo, surface,
1479 !(dev_priv->capabilities & SVGA_CAP_3D),
1480 mode_cmd);
1481 if (IS_ERR(vfb)) {
1482 ret = PTR_ERR(vfb);
1483 goto err_out;
1486 err_out:
1487 /* vmw_user_lookup_handle takes one ref so does new_fb */
1488 if (bo)
1489 vmw_dmabuf_unreference(&bo);
1490 if (surface)
1491 vmw_surface_unreference(&surface);
1493 if (ret) {
1494 DRM_ERROR("failed to create vmw_framebuffer: %i\n", ret);
1495 ttm_base_object_unref(&user_obj);
1496 return ERR_PTR(ret);
1497 } else
1498 vfb->user_obj = user_obj;
1500 return &vfb->base;
1506 * vmw_kms_atomic_check_modeset- validate state object for modeset changes
1508 * @dev: DRM device
1509 * @state: the driver state object
1511 * This is a simple wrapper around drm_atomic_helper_check_modeset() for
1512 * us to assign a value to mode->crtc_clock so that
1513 * drm_calc_timestamping_constants() won't throw an error message
1515 * RETURNS
1516 * Zero for success or -errno
1518 static int
1519 vmw_kms_atomic_check_modeset(struct drm_device *dev,
1520 struct drm_atomic_state *state)
1522 struct drm_crtc_state *crtc_state;
1523 struct drm_crtc *crtc;
1524 struct vmw_private *dev_priv = vmw_priv(dev);
1525 int i;
1527 for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
1528 unsigned long requested_bb_mem = 0;
1530 if (dev_priv->active_display_unit == vmw_du_screen_target) {
1531 if (crtc->primary->fb) {
1532 int cpp = crtc->primary->fb->pitches[0] /
1533 crtc->primary->fb->width;
1535 requested_bb_mem += crtc->mode.hdisplay * cpp *
1536 crtc->mode.vdisplay;
1539 if (requested_bb_mem > dev_priv->prim_bb_mem)
1540 return -EINVAL;
1544 return drm_atomic_helper_check(dev, state);
1549 * vmw_kms_atomic_commit - Perform an atomic state commit
1551 * @dev: DRM device
1552 * @state: the driver state object
1553 * @nonblock: Whether nonblocking behaviour is requested
1555 * This is a simple wrapper around drm_atomic_helper_commit() for
1556 * us to clear the nonblocking value.
1558 * Nonblocking commits currently cause synchronization issues
1559 * for vmwgfx.
1561 * RETURNS
1562 * Zero for success or negative error code on failure.
1564 int vmw_kms_atomic_commit(struct drm_device *dev,
1565 struct drm_atomic_state *state,
1566 bool nonblock)
1568 return drm_atomic_helper_commit(dev, state, false);
1572 static const struct drm_mode_config_funcs vmw_kms_funcs = {
1573 .fb_create = vmw_kms_fb_create,
1574 .atomic_check = vmw_kms_atomic_check_modeset,
1575 .atomic_commit = vmw_kms_atomic_commit,
1578 static int vmw_kms_generic_present(struct vmw_private *dev_priv,
1579 struct drm_file *file_priv,
1580 struct vmw_framebuffer *vfb,
1581 struct vmw_surface *surface,
1582 uint32_t sid,
1583 int32_t destX, int32_t destY,
1584 struct drm_vmw_rect *clips,
1585 uint32_t num_clips)
1587 return vmw_kms_sou_do_surface_dirty(dev_priv, vfb, NULL, clips,
1588 &surface->res, destX, destY,
1589 num_clips, 1, NULL);
1593 int vmw_kms_present(struct vmw_private *dev_priv,
1594 struct drm_file *file_priv,
1595 struct vmw_framebuffer *vfb,
1596 struct vmw_surface *surface,
1597 uint32_t sid,
1598 int32_t destX, int32_t destY,
1599 struct drm_vmw_rect *clips,
1600 uint32_t num_clips)
1602 int ret;
1604 switch (dev_priv->active_display_unit) {
1605 case vmw_du_screen_target:
1606 ret = vmw_kms_stdu_surface_dirty(dev_priv, vfb, NULL, clips,
1607 &surface->res, destX, destY,
1608 num_clips, 1, NULL);
1609 break;
1610 case vmw_du_screen_object:
1611 ret = vmw_kms_generic_present(dev_priv, file_priv, vfb, surface,
1612 sid, destX, destY, clips,
1613 num_clips);
1614 break;
1615 default:
1616 WARN_ONCE(true,
1617 "Present called with invalid display system.\n");
1618 ret = -ENOSYS;
1619 break;
1621 if (ret)
1622 return ret;
1624 vmw_fifo_flush(dev_priv, false);
1626 return 0;
1629 static void
1630 vmw_kms_create_hotplug_mode_update_property(struct vmw_private *dev_priv)
1632 if (dev_priv->hotplug_mode_update_property)
1633 return;
1635 dev_priv->hotplug_mode_update_property =
1636 drm_property_create_range(dev_priv->dev,
1637 DRM_MODE_PROP_IMMUTABLE,
1638 "hotplug_mode_update", 0, 1);
1640 if (!dev_priv->hotplug_mode_update_property)
1641 return;
1645 int vmw_kms_init(struct vmw_private *dev_priv)
1647 struct drm_device *dev = dev_priv->dev;
1648 int ret;
1650 drm_mode_config_init(dev);
1651 dev->mode_config.funcs = &vmw_kms_funcs;
1652 dev->mode_config.min_width = 1;
1653 dev->mode_config.min_height = 1;
1654 dev->mode_config.max_width = dev_priv->texture_max_width;
1655 dev->mode_config.max_height = dev_priv->texture_max_height;
1657 drm_mode_create_suggested_offset_properties(dev);
1658 vmw_kms_create_hotplug_mode_update_property(dev_priv);
1660 ret = vmw_kms_stdu_init_display(dev_priv);
1661 if (ret) {
1662 ret = vmw_kms_sou_init_display(dev_priv);
1663 if (ret) /* Fallback */
1664 ret = vmw_kms_ldu_init_display(dev_priv);
1667 return ret;
1670 int vmw_kms_close(struct vmw_private *dev_priv)
1672 int ret = 0;
1675 * Docs says we should take the lock before calling this function
1676 * but since it destroys encoders and our destructor calls
1677 * drm_encoder_cleanup which takes the lock we deadlock.
1679 drm_mode_config_cleanup(dev_priv->dev);
1680 if (dev_priv->active_display_unit == vmw_du_legacy)
1681 ret = vmw_kms_ldu_close_display(dev_priv);
1683 return ret;
1686 int vmw_kms_cursor_bypass_ioctl(struct drm_device *dev, void *data,
1687 struct drm_file *file_priv)
1689 struct drm_vmw_cursor_bypass_arg *arg = data;
1690 struct vmw_display_unit *du;
1691 struct drm_crtc *crtc;
1692 int ret = 0;
1695 mutex_lock(&dev->mode_config.mutex);
1696 if (arg->flags & DRM_VMW_CURSOR_BYPASS_ALL) {
1698 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1699 du = vmw_crtc_to_du(crtc);
1700 du->hotspot_x = arg->xhot;
1701 du->hotspot_y = arg->yhot;
1704 mutex_unlock(&dev->mode_config.mutex);
1705 return 0;
1708 crtc = drm_crtc_find(dev, file_priv, arg->crtc_id);
1709 if (!crtc) {
1710 ret = -ENOENT;
1711 goto out;
1714 du = vmw_crtc_to_du(crtc);
1716 du->hotspot_x = arg->xhot;
1717 du->hotspot_y = arg->yhot;
1719 out:
1720 mutex_unlock(&dev->mode_config.mutex);
1722 return ret;
1725 int vmw_kms_write_svga(struct vmw_private *vmw_priv,
1726 unsigned width, unsigned height, unsigned pitch,
1727 unsigned bpp, unsigned depth)
1729 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1730 vmw_write(vmw_priv, SVGA_REG_PITCHLOCK, pitch);
1731 else if (vmw_fifo_have_pitchlock(vmw_priv))
1732 vmw_mmio_write(pitch, vmw_priv->mmio_virt +
1733 SVGA_FIFO_PITCHLOCK);
1734 vmw_write(vmw_priv, SVGA_REG_WIDTH, width);
1735 vmw_write(vmw_priv, SVGA_REG_HEIGHT, height);
1736 vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, bpp);
1738 if (vmw_read(vmw_priv, SVGA_REG_DEPTH) != depth) {
1739 DRM_ERROR("Invalid depth %u for %u bpp, host expects %u\n",
1740 depth, bpp, vmw_read(vmw_priv, SVGA_REG_DEPTH));
1741 return -EINVAL;
1744 return 0;
1747 int vmw_kms_save_vga(struct vmw_private *vmw_priv)
1749 struct vmw_vga_topology_state *save;
1750 uint32_t i;
1752 vmw_priv->vga_width = vmw_read(vmw_priv, SVGA_REG_WIDTH);
1753 vmw_priv->vga_height = vmw_read(vmw_priv, SVGA_REG_HEIGHT);
1754 vmw_priv->vga_bpp = vmw_read(vmw_priv, SVGA_REG_BITS_PER_PIXEL);
1755 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1756 vmw_priv->vga_pitchlock =
1757 vmw_read(vmw_priv, SVGA_REG_PITCHLOCK);
1758 else if (vmw_fifo_have_pitchlock(vmw_priv))
1759 vmw_priv->vga_pitchlock = vmw_mmio_read(vmw_priv->mmio_virt +
1760 SVGA_FIFO_PITCHLOCK);
1762 if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
1763 return 0;
1765 vmw_priv->num_displays = vmw_read(vmw_priv,
1766 SVGA_REG_NUM_GUEST_DISPLAYS);
1768 if (vmw_priv->num_displays == 0)
1769 vmw_priv->num_displays = 1;
1771 for (i = 0; i < vmw_priv->num_displays; ++i) {
1772 save = &vmw_priv->vga_save[i];
1773 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
1774 save->primary = vmw_read(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY);
1775 save->pos_x = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_X);
1776 save->pos_y = vmw_read(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y);
1777 save->width = vmw_read(vmw_priv, SVGA_REG_DISPLAY_WIDTH);
1778 save->height = vmw_read(vmw_priv, SVGA_REG_DISPLAY_HEIGHT);
1779 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
1780 if (i == 0 && vmw_priv->num_displays == 1 &&
1781 save->width == 0 && save->height == 0) {
1784 * It should be fairly safe to assume that these
1785 * values are uninitialized.
1788 save->width = vmw_priv->vga_width - save->pos_x;
1789 save->height = vmw_priv->vga_height - save->pos_y;
1793 return 0;
1796 int vmw_kms_restore_vga(struct vmw_private *vmw_priv)
1798 struct vmw_vga_topology_state *save;
1799 uint32_t i;
1801 vmw_write(vmw_priv, SVGA_REG_WIDTH, vmw_priv->vga_width);
1802 vmw_write(vmw_priv, SVGA_REG_HEIGHT, vmw_priv->vga_height);
1803 vmw_write(vmw_priv, SVGA_REG_BITS_PER_PIXEL, vmw_priv->vga_bpp);
1804 if (vmw_priv->capabilities & SVGA_CAP_PITCHLOCK)
1805 vmw_write(vmw_priv, SVGA_REG_PITCHLOCK,
1806 vmw_priv->vga_pitchlock);
1807 else if (vmw_fifo_have_pitchlock(vmw_priv))
1808 vmw_mmio_write(vmw_priv->vga_pitchlock,
1809 vmw_priv->mmio_virt + SVGA_FIFO_PITCHLOCK);
1811 if (!(vmw_priv->capabilities & SVGA_CAP_DISPLAY_TOPOLOGY))
1812 return 0;
1814 for (i = 0; i < vmw_priv->num_displays; ++i) {
1815 save = &vmw_priv->vga_save[i];
1816 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, i);
1817 vmw_write(vmw_priv, SVGA_REG_DISPLAY_IS_PRIMARY, save->primary);
1818 vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_X, save->pos_x);
1819 vmw_write(vmw_priv, SVGA_REG_DISPLAY_POSITION_Y, save->pos_y);
1820 vmw_write(vmw_priv, SVGA_REG_DISPLAY_WIDTH, save->width);
1821 vmw_write(vmw_priv, SVGA_REG_DISPLAY_HEIGHT, save->height);
1822 vmw_write(vmw_priv, SVGA_REG_DISPLAY_ID, SVGA_ID_INVALID);
1825 return 0;
1828 bool vmw_kms_validate_mode_vram(struct vmw_private *dev_priv,
1829 uint32_t pitch,
1830 uint32_t height)
1832 return ((u64) pitch * (u64) height) < (u64)
1833 ((dev_priv->active_display_unit == vmw_du_screen_target) ?
1834 dev_priv->prim_bb_mem : dev_priv->vram_size);
1839 * Function called by DRM code called with vbl_lock held.
1841 u32 vmw_get_vblank_counter(struct drm_device *dev, unsigned int pipe)
1843 return 0;
1847 * Function called by DRM code called with vbl_lock held.
1849 int vmw_enable_vblank(struct drm_device *dev, unsigned int pipe)
1851 return -EINVAL;
1855 * Function called by DRM code called with vbl_lock held.
1857 void vmw_disable_vblank(struct drm_device *dev, unsigned int pipe)
1863 * Small shared kms functions.
1866 static int vmw_du_update_layout(struct vmw_private *dev_priv, unsigned num,
1867 struct drm_vmw_rect *rects)
1869 struct drm_device *dev = dev_priv->dev;
1870 struct vmw_display_unit *du;
1871 struct drm_connector *con;
1873 mutex_lock(&dev->mode_config.mutex);
1875 #if 0
1877 unsigned int i;
1879 DRM_INFO("%s: new layout ", __func__);
1880 for (i = 0; i < num; i++)
1881 DRM_INFO("(%i, %i %ux%u) ", rects[i].x, rects[i].y,
1882 rects[i].w, rects[i].h);
1883 DRM_INFO("\n");
1885 #endif
1887 list_for_each_entry(con, &dev->mode_config.connector_list, head) {
1888 du = vmw_connector_to_du(con);
1889 if (num > du->unit) {
1890 du->pref_width = rects[du->unit].w;
1891 du->pref_height = rects[du->unit].h;
1892 du->pref_active = true;
1893 du->gui_x = rects[du->unit].x;
1894 du->gui_y = rects[du->unit].y;
1895 drm_object_property_set_value
1896 (&con->base, dev->mode_config.suggested_x_property,
1897 du->gui_x);
1898 drm_object_property_set_value
1899 (&con->base, dev->mode_config.suggested_y_property,
1900 du->gui_y);
1901 } else {
1902 du->pref_width = 800;
1903 du->pref_height = 600;
1904 du->pref_active = false;
1905 drm_object_property_set_value
1906 (&con->base, dev->mode_config.suggested_x_property,
1908 drm_object_property_set_value
1909 (&con->base, dev->mode_config.suggested_y_property,
1912 con->status = vmw_du_connector_detect(con, true);
1915 mutex_unlock(&dev->mode_config.mutex);
1916 drm_sysfs_hotplug_event(dev);
1918 return 0;
1921 int vmw_du_crtc_gamma_set(struct drm_crtc *crtc,
1922 u16 *r, u16 *g, u16 *b,
1923 uint32_t size,
1924 struct drm_modeset_acquire_ctx *ctx)
1926 struct vmw_private *dev_priv = vmw_priv(crtc->dev);
1927 int i;
1929 for (i = 0; i < size; i++) {
1930 DRM_DEBUG("%d r/g/b = 0x%04x / 0x%04x / 0x%04x\n", i,
1931 r[i], g[i], b[i]);
1932 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 0, r[i] >> 8);
1933 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 1, g[i] >> 8);
1934 vmw_write(dev_priv, SVGA_PALETTE_BASE + i * 3 + 2, b[i] >> 8);
1937 return 0;
1940 int vmw_du_connector_dpms(struct drm_connector *connector, int mode)
1942 return 0;
1945 enum drm_connector_status
1946 vmw_du_connector_detect(struct drm_connector *connector, bool force)
1948 uint32_t num_displays;
1949 struct drm_device *dev = connector->dev;
1950 struct vmw_private *dev_priv = vmw_priv(dev);
1951 struct vmw_display_unit *du = vmw_connector_to_du(connector);
1953 num_displays = vmw_read(dev_priv, SVGA_REG_NUM_DISPLAYS);
1955 return ((vmw_connector_to_du(connector)->unit < num_displays &&
1956 du->pref_active) ?
1957 connector_status_connected : connector_status_disconnected);
1960 static struct drm_display_mode vmw_kms_connector_builtin[] = {
1961 /* 640x480@60Hz */
1962 { DRM_MODE("640x480", DRM_MODE_TYPE_DRIVER, 25175, 640, 656,
1963 752, 800, 0, 480, 489, 492, 525, 0,
1964 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
1965 /* 800x600@60Hz */
1966 { DRM_MODE("800x600", DRM_MODE_TYPE_DRIVER, 40000, 800, 840,
1967 968, 1056, 0, 600, 601, 605, 628, 0,
1968 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1969 /* 1024x768@60Hz */
1970 { DRM_MODE("1024x768", DRM_MODE_TYPE_DRIVER, 65000, 1024, 1048,
1971 1184, 1344, 0, 768, 771, 777, 806, 0,
1972 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC) },
1973 /* 1152x864@75Hz */
1974 { DRM_MODE("1152x864", DRM_MODE_TYPE_DRIVER, 108000, 1152, 1216,
1975 1344, 1600, 0, 864, 865, 868, 900, 0,
1976 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1977 /* 1280x768@60Hz */
1978 { DRM_MODE("1280x768", DRM_MODE_TYPE_DRIVER, 79500, 1280, 1344,
1979 1472, 1664, 0, 768, 771, 778, 798, 0,
1980 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
1981 /* 1280x800@60Hz */
1982 { DRM_MODE("1280x800", DRM_MODE_TYPE_DRIVER, 83500, 1280, 1352,
1983 1480, 1680, 0, 800, 803, 809, 831, 0,
1984 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NVSYNC) },
1985 /* 1280x960@60Hz */
1986 { DRM_MODE("1280x960", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1376,
1987 1488, 1800, 0, 960, 961, 964, 1000, 0,
1988 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1989 /* 1280x1024@60Hz */
1990 { DRM_MODE("1280x1024", DRM_MODE_TYPE_DRIVER, 108000, 1280, 1328,
1991 1440, 1688, 0, 1024, 1025, 1028, 1066, 0,
1992 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1993 /* 1360x768@60Hz */
1994 { DRM_MODE("1360x768", DRM_MODE_TYPE_DRIVER, 85500, 1360, 1424,
1995 1536, 1792, 0, 768, 771, 777, 795, 0,
1996 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
1997 /* 1440x1050@60Hz */
1998 { DRM_MODE("1400x1050", DRM_MODE_TYPE_DRIVER, 121750, 1400, 1488,
1999 1632, 1864, 0, 1050, 1053, 1057, 1089, 0,
2000 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2001 /* 1440x900@60Hz */
2002 { DRM_MODE("1440x900", DRM_MODE_TYPE_DRIVER, 106500, 1440, 1520,
2003 1672, 1904, 0, 900, 903, 909, 934, 0,
2004 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2005 /* 1600x1200@60Hz */
2006 { DRM_MODE("1600x1200", DRM_MODE_TYPE_DRIVER, 162000, 1600, 1664,
2007 1856, 2160, 0, 1200, 1201, 1204, 1250, 0,
2008 DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_PVSYNC) },
2009 /* 1680x1050@60Hz */
2010 { DRM_MODE("1680x1050", DRM_MODE_TYPE_DRIVER, 146250, 1680, 1784,
2011 1960, 2240, 0, 1050, 1053, 1059, 1089, 0,
2012 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2013 /* 1792x1344@60Hz */
2014 { DRM_MODE("1792x1344", DRM_MODE_TYPE_DRIVER, 204750, 1792, 1920,
2015 2120, 2448, 0, 1344, 1345, 1348, 1394, 0,
2016 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2017 /* 1853x1392@60Hz */
2018 { DRM_MODE("1856x1392", DRM_MODE_TYPE_DRIVER, 218250, 1856, 1952,
2019 2176, 2528, 0, 1392, 1393, 1396, 1439, 0,
2020 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2021 /* 1920x1200@60Hz */
2022 { DRM_MODE("1920x1200", DRM_MODE_TYPE_DRIVER, 193250, 1920, 2056,
2023 2256, 2592, 0, 1200, 1203, 1209, 1245, 0,
2024 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2025 /* 1920x1440@60Hz */
2026 { DRM_MODE("1920x1440", DRM_MODE_TYPE_DRIVER, 234000, 1920, 2048,
2027 2256, 2600, 0, 1440, 1441, 1444, 1500, 0,
2028 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2029 /* 2560x1600@60Hz */
2030 { DRM_MODE("2560x1600", DRM_MODE_TYPE_DRIVER, 348500, 2560, 2752,
2031 3032, 3504, 0, 1600, 1603, 1609, 1658, 0,
2032 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC) },
2033 /* Terminate */
2034 { DRM_MODE("", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) },
2038 * vmw_guess_mode_timing - Provide fake timings for a
2039 * 60Hz vrefresh mode.
2041 * @mode - Pointer to a struct drm_display_mode with hdisplay and vdisplay
2042 * members filled in.
2044 void vmw_guess_mode_timing(struct drm_display_mode *mode)
2046 mode->hsync_start = mode->hdisplay + 50;
2047 mode->hsync_end = mode->hsync_start + 50;
2048 mode->htotal = mode->hsync_end + 50;
2050 mode->vsync_start = mode->vdisplay + 50;
2051 mode->vsync_end = mode->vsync_start + 50;
2052 mode->vtotal = mode->vsync_end + 50;
2054 mode->clock = (u32)mode->htotal * (u32)mode->vtotal / 100 * 6;
2055 mode->vrefresh = drm_mode_vrefresh(mode);
2059 int vmw_du_connector_fill_modes(struct drm_connector *connector,
2060 uint32_t max_width, uint32_t max_height)
2062 struct vmw_display_unit *du = vmw_connector_to_du(connector);
2063 struct drm_device *dev = connector->dev;
2064 struct vmw_private *dev_priv = vmw_priv(dev);
2065 struct drm_display_mode *mode = NULL;
2066 struct drm_display_mode *bmode;
2067 struct drm_display_mode prefmode = { DRM_MODE("preferred",
2068 DRM_MODE_TYPE_DRIVER | DRM_MODE_TYPE_PREFERRED,
2069 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2070 DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_PVSYNC)
2072 int i;
2073 u32 assumed_bpp = 4;
2075 if (dev_priv->assume_16bpp)
2076 assumed_bpp = 2;
2078 if (dev_priv->active_display_unit == vmw_du_screen_target) {
2079 max_width = min(max_width, dev_priv->stdu_max_width);
2080 max_width = min(max_width, dev_priv->texture_max_width);
2082 max_height = min(max_height, dev_priv->stdu_max_height);
2083 max_height = min(max_height, dev_priv->texture_max_height);
2086 /* Add preferred mode */
2087 mode = drm_mode_duplicate(dev, &prefmode);
2088 if (!mode)
2089 return 0;
2090 mode->hdisplay = du->pref_width;
2091 mode->vdisplay = du->pref_height;
2092 vmw_guess_mode_timing(mode);
2094 if (vmw_kms_validate_mode_vram(dev_priv,
2095 mode->hdisplay * assumed_bpp,
2096 mode->vdisplay)) {
2097 drm_mode_probed_add(connector, mode);
2098 } else {
2099 drm_mode_destroy(dev, mode);
2100 mode = NULL;
2103 if (du->pref_mode) {
2104 list_del_init(&du->pref_mode->head);
2105 drm_mode_destroy(dev, du->pref_mode);
2108 /* mode might be null here, this is intended */
2109 du->pref_mode = mode;
2111 for (i = 0; vmw_kms_connector_builtin[i].type != 0; i++) {
2112 bmode = &vmw_kms_connector_builtin[i];
2113 if (bmode->hdisplay > max_width ||
2114 bmode->vdisplay > max_height)
2115 continue;
2117 if (!vmw_kms_validate_mode_vram(dev_priv,
2118 bmode->hdisplay * assumed_bpp,
2119 bmode->vdisplay))
2120 continue;
2122 mode = drm_mode_duplicate(dev, bmode);
2123 if (!mode)
2124 return 0;
2125 mode->vrefresh = drm_mode_vrefresh(mode);
2127 drm_mode_probed_add(connector, mode);
2130 drm_mode_connector_list_update(connector);
2131 /* Move the prefered mode first, help apps pick the right mode. */
2132 drm_mode_sort(&connector->modes);
2134 return 1;
2137 int vmw_du_connector_set_property(struct drm_connector *connector,
2138 struct drm_property *property,
2139 uint64_t val)
2141 struct vmw_display_unit *du = vmw_connector_to_du(connector);
2142 struct vmw_private *dev_priv = vmw_priv(connector->dev);
2144 if (property == dev_priv->implicit_placement_property)
2145 du->is_implicit = val;
2147 return 0;
2153 * vmw_du_connector_atomic_set_property - Atomic version of get property
2155 * @crtc - crtc the property is associated with
2157 * Returns:
2158 * Zero on success, negative errno on failure.
2161 vmw_du_connector_atomic_set_property(struct drm_connector *connector,
2162 struct drm_connector_state *state,
2163 struct drm_property *property,
2164 uint64_t val)
2166 struct vmw_private *dev_priv = vmw_priv(connector->dev);
2167 struct vmw_connector_state *vcs = vmw_connector_state_to_vcs(state);
2168 struct vmw_display_unit *du = vmw_connector_to_du(connector);
2171 if (property == dev_priv->implicit_placement_property) {
2172 vcs->is_implicit = val;
2175 * We should really be doing a drm_atomic_commit() to
2176 * commit the new state, but since this doesn't cause
2177 * an immedate state change, this is probably ok
2179 du->is_implicit = vcs->is_implicit;
2180 } else {
2181 return -EINVAL;
2184 return 0;
2189 * vmw_du_connector_atomic_get_property - Atomic version of get property
2191 * @connector - connector the property is associated with
2193 * Returns:
2194 * Zero on success, negative errno on failure.
2197 vmw_du_connector_atomic_get_property(struct drm_connector *connector,
2198 const struct drm_connector_state *state,
2199 struct drm_property *property,
2200 uint64_t *val)
2202 struct vmw_private *dev_priv = vmw_priv(connector->dev);
2203 struct vmw_connector_state *vcs = vmw_connector_state_to_vcs(state);
2205 if (property == dev_priv->implicit_placement_property)
2206 *val = vcs->is_implicit;
2207 else {
2208 DRM_ERROR("Invalid Property %s\n", property->name);
2209 return -EINVAL;
2212 return 0;
2216 int vmw_kms_update_layout_ioctl(struct drm_device *dev, void *data,
2217 struct drm_file *file_priv)
2219 struct vmw_private *dev_priv = vmw_priv(dev);
2220 struct drm_vmw_update_layout_arg *arg =
2221 (struct drm_vmw_update_layout_arg *)data;
2222 void __user *user_rects;
2223 struct drm_vmw_rect *rects;
2224 unsigned rects_size;
2225 int ret;
2226 int i;
2227 u64 total_pixels = 0;
2228 struct drm_mode_config *mode_config = &dev->mode_config;
2229 struct drm_vmw_rect bounding_box = {0};
2231 if (!arg->num_outputs) {
2232 struct drm_vmw_rect def_rect = {0, 0, 800, 600};
2233 vmw_du_update_layout(dev_priv, 1, &def_rect);
2234 return 0;
2237 rects_size = arg->num_outputs * sizeof(struct drm_vmw_rect);
2238 rects = kcalloc(arg->num_outputs, sizeof(struct drm_vmw_rect),
2239 GFP_KERNEL);
2240 if (unlikely(!rects))
2241 return -ENOMEM;
2243 user_rects = (void __user *)(unsigned long)arg->rects;
2244 ret = copy_from_user(rects, user_rects, rects_size);
2245 if (unlikely(ret != 0)) {
2246 DRM_ERROR("Failed to get rects.\n");
2247 ret = -EFAULT;
2248 goto out_free;
2251 for (i = 0; i < arg->num_outputs; ++i) {
2252 if (rects[i].x < 0 ||
2253 rects[i].y < 0 ||
2254 rects[i].x + rects[i].w > mode_config->max_width ||
2255 rects[i].y + rects[i].h > mode_config->max_height) {
2256 DRM_ERROR("Invalid GUI layout.\n");
2257 ret = -EINVAL;
2258 goto out_free;
2262 * bounding_box.w and bunding_box.h are used as
2263 * lower-right coordinates
2265 if (rects[i].x + rects[i].w > bounding_box.w)
2266 bounding_box.w = rects[i].x + rects[i].w;
2268 if (rects[i].y + rects[i].h > bounding_box.h)
2269 bounding_box.h = rects[i].y + rects[i].h;
2271 total_pixels += (u64) rects[i].w * (u64) rects[i].h;
2274 if (dev_priv->active_display_unit == vmw_du_screen_target) {
2276 * For Screen Targets, the limits for a toplogy are:
2277 * 1. Bounding box (assuming 32bpp) must be < prim_bb_mem
2278 * 2. Total pixels (assuming 32bpp) must be < prim_bb_mem
2280 u64 bb_mem = (u64) bounding_box.w * bounding_box.h * 4;
2281 u64 pixel_mem = total_pixels * 4;
2283 if (bb_mem > dev_priv->prim_bb_mem) {
2284 DRM_ERROR("Topology is beyond supported limits.\n");
2285 ret = -EINVAL;
2286 goto out_free;
2289 if (pixel_mem > dev_priv->prim_bb_mem) {
2290 DRM_ERROR("Combined output size too large\n");
2291 ret = -EINVAL;
2292 goto out_free;
2296 vmw_du_update_layout(dev_priv, arg->num_outputs, rects);
2298 out_free:
2299 kfree(rects);
2300 return ret;
2304 * vmw_kms_helper_dirty - Helper to build commands and perform actions based
2305 * on a set of cliprects and a set of display units.
2307 * @dev_priv: Pointer to a device private structure.
2308 * @framebuffer: Pointer to the framebuffer on which to perform the actions.
2309 * @clips: A set of struct drm_clip_rect. Either this os @vclips must be NULL.
2310 * Cliprects are given in framebuffer coordinates.
2311 * @vclips: A set of struct drm_vmw_rect cliprects. Either this or @clips must
2312 * be NULL. Cliprects are given in source coordinates.
2313 * @dest_x: X coordinate offset for the crtc / destination clip rects.
2314 * @dest_y: Y coordinate offset for the crtc / destination clip rects.
2315 * @num_clips: Number of cliprects in the @clips or @vclips array.
2316 * @increment: Integer with which to increment the clip counter when looping.
2317 * Used to skip a predetermined number of clip rects.
2318 * @dirty: Closure structure. See the description of struct vmw_kms_dirty.
2320 int vmw_kms_helper_dirty(struct vmw_private *dev_priv,
2321 struct vmw_framebuffer *framebuffer,
2322 const struct drm_clip_rect *clips,
2323 const struct drm_vmw_rect *vclips,
2324 s32 dest_x, s32 dest_y,
2325 int num_clips,
2326 int increment,
2327 struct vmw_kms_dirty *dirty)
2329 struct vmw_display_unit *units[VMWGFX_NUM_DISPLAY_UNITS];
2330 struct drm_crtc *crtc;
2331 u32 num_units = 0;
2332 u32 i, k;
2334 dirty->dev_priv = dev_priv;
2336 list_for_each_entry(crtc, &dev_priv->dev->mode_config.crtc_list, head) {
2337 if (crtc->primary->fb != &framebuffer->base)
2338 continue;
2339 units[num_units++] = vmw_crtc_to_du(crtc);
2342 for (k = 0; k < num_units; k++) {
2343 struct vmw_display_unit *unit = units[k];
2344 s32 crtc_x = unit->crtc.x;
2345 s32 crtc_y = unit->crtc.y;
2346 s32 crtc_width = unit->crtc.mode.hdisplay;
2347 s32 crtc_height = unit->crtc.mode.vdisplay;
2348 const struct drm_clip_rect *clips_ptr = clips;
2349 const struct drm_vmw_rect *vclips_ptr = vclips;
2351 dirty->unit = unit;
2352 if (dirty->fifo_reserve_size > 0) {
2353 dirty->cmd = vmw_fifo_reserve(dev_priv,
2354 dirty->fifo_reserve_size);
2355 if (!dirty->cmd) {
2356 DRM_ERROR("Couldn't reserve fifo space "
2357 "for dirty blits.\n");
2358 return -ENOMEM;
2360 memset(dirty->cmd, 0, dirty->fifo_reserve_size);
2362 dirty->num_hits = 0;
2363 for (i = 0; i < num_clips; i++, clips_ptr += increment,
2364 vclips_ptr += increment) {
2365 s32 clip_left;
2366 s32 clip_top;
2369 * Select clip array type. Note that integer type
2370 * in @clips is unsigned short, whereas in @vclips
2371 * it's 32-bit.
2373 if (clips) {
2374 dirty->fb_x = (s32) clips_ptr->x1;
2375 dirty->fb_y = (s32) clips_ptr->y1;
2376 dirty->unit_x2 = (s32) clips_ptr->x2 + dest_x -
2377 crtc_x;
2378 dirty->unit_y2 = (s32) clips_ptr->y2 + dest_y -
2379 crtc_y;
2380 } else {
2381 dirty->fb_x = vclips_ptr->x;
2382 dirty->fb_y = vclips_ptr->y;
2383 dirty->unit_x2 = dirty->fb_x + vclips_ptr->w +
2384 dest_x - crtc_x;
2385 dirty->unit_y2 = dirty->fb_y + vclips_ptr->h +
2386 dest_y - crtc_y;
2389 dirty->unit_x1 = dirty->fb_x + dest_x - crtc_x;
2390 dirty->unit_y1 = dirty->fb_y + dest_y - crtc_y;
2392 /* Skip this clip if it's outside the crtc region */
2393 if (dirty->unit_x1 >= crtc_width ||
2394 dirty->unit_y1 >= crtc_height ||
2395 dirty->unit_x2 <= 0 || dirty->unit_y2 <= 0)
2396 continue;
2398 /* Clip right and bottom to crtc limits */
2399 dirty->unit_x2 = min_t(s32, dirty->unit_x2,
2400 crtc_width);
2401 dirty->unit_y2 = min_t(s32, dirty->unit_y2,
2402 crtc_height);
2404 /* Clip left and top to crtc limits */
2405 clip_left = min_t(s32, dirty->unit_x1, 0);
2406 clip_top = min_t(s32, dirty->unit_y1, 0);
2407 dirty->unit_x1 -= clip_left;
2408 dirty->unit_y1 -= clip_top;
2409 dirty->fb_x -= clip_left;
2410 dirty->fb_y -= clip_top;
2412 dirty->clip(dirty);
2415 dirty->fifo_commit(dirty);
2418 return 0;
2422 * vmw_kms_helper_buffer_prepare - Reserve and validate a buffer object before
2423 * command submission.
2425 * @dev_priv. Pointer to a device private structure.
2426 * @buf: The buffer object
2427 * @interruptible: Whether to perform waits as interruptible.
2428 * @validate_as_mob: Whether the buffer should be validated as a MOB. If false,
2429 * The buffer will be validated as a GMR. Already pinned buffers will not be
2430 * validated.
2432 * Returns 0 on success, negative error code on failure, -ERESTARTSYS if
2433 * interrupted by a signal.
2435 int vmw_kms_helper_buffer_prepare(struct vmw_private *dev_priv,
2436 struct vmw_dma_buffer *buf,
2437 bool interruptible,
2438 bool validate_as_mob)
2440 struct ttm_buffer_object *bo = &buf->base;
2441 int ret;
2443 ttm_bo_reserve(bo, false, false, NULL);
2444 ret = vmw_validate_single_buffer(dev_priv, bo, interruptible,
2445 validate_as_mob);
2446 if (ret)
2447 ttm_bo_unreserve(bo);
2449 return ret;
2453 * vmw_kms_helper_buffer_revert - Undo the actions of
2454 * vmw_kms_helper_buffer_prepare.
2456 * @res: Pointer to the buffer object.
2458 * Helper to be used if an error forces the caller to undo the actions of
2459 * vmw_kms_helper_buffer_prepare.
2461 void vmw_kms_helper_buffer_revert(struct vmw_dma_buffer *buf)
2463 if (buf)
2464 ttm_bo_unreserve(&buf->base);
2468 * vmw_kms_helper_buffer_finish - Unreserve and fence a buffer object after
2469 * kms command submission.
2471 * @dev_priv: Pointer to a device private structure.
2472 * @file_priv: Pointer to a struct drm_file representing the caller's
2473 * connection. Must be set to NULL if @user_fence_rep is NULL, and conversely
2474 * if non-NULL, @user_fence_rep must be non-NULL.
2475 * @buf: The buffer object.
2476 * @out_fence: Optional pointer to a fence pointer. If non-NULL, a
2477 * ref-counted fence pointer is returned here.
2478 * @user_fence_rep: Optional pointer to a user-space provided struct
2479 * drm_vmw_fence_rep. If provided, @file_priv must also be provided and the
2480 * function copies fence data to user-space in a fail-safe manner.
2482 void vmw_kms_helper_buffer_finish(struct vmw_private *dev_priv,
2483 struct drm_file *file_priv,
2484 struct vmw_dma_buffer *buf,
2485 struct vmw_fence_obj **out_fence,
2486 struct drm_vmw_fence_rep __user *
2487 user_fence_rep)
2489 struct vmw_fence_obj *fence;
2490 uint32_t handle;
2491 int ret;
2493 ret = vmw_execbuf_fence_commands(file_priv, dev_priv, &fence,
2494 file_priv ? &handle : NULL);
2495 if (buf)
2496 vmw_fence_single_bo(&buf->base, fence);
2497 if (file_priv)
2498 vmw_execbuf_copy_fence_user(dev_priv, vmw_fpriv(file_priv),
2499 ret, user_fence_rep, fence,
2500 handle, -1, NULL);
2501 if (out_fence)
2502 *out_fence = fence;
2503 else
2504 vmw_fence_obj_unreference(&fence);
2506 vmw_kms_helper_buffer_revert(buf);
2511 * vmw_kms_helper_resource_revert - Undo the actions of
2512 * vmw_kms_helper_resource_prepare.
2514 * @res: Pointer to the resource. Typically a surface.
2516 * Helper to be used if an error forces the caller to undo the actions of
2517 * vmw_kms_helper_resource_prepare.
2519 void vmw_kms_helper_resource_revert(struct vmw_validation_ctx *ctx)
2521 struct vmw_resource *res = ctx->res;
2523 vmw_kms_helper_buffer_revert(ctx->buf);
2524 vmw_dmabuf_unreference(&ctx->buf);
2525 vmw_resource_unreserve(res, false, NULL, 0);
2526 mutex_unlock(&res->dev_priv->cmdbuf_mutex);
2530 * vmw_kms_helper_resource_prepare - Reserve and validate a resource before
2531 * command submission.
2533 * @res: Pointer to the resource. Typically a surface.
2534 * @interruptible: Whether to perform waits as interruptible.
2536 * Reserves and validates also the backup buffer if a guest-backed resource.
2537 * Returns 0 on success, negative error code on failure. -ERESTARTSYS if
2538 * interrupted by a signal.
2540 int vmw_kms_helper_resource_prepare(struct vmw_resource *res,
2541 bool interruptible,
2542 struct vmw_validation_ctx *ctx)
2544 int ret = 0;
2546 ctx->buf = NULL;
2547 ctx->res = res;
2549 if (interruptible)
2550 ret = mutex_lock_interruptible(&res->dev_priv->cmdbuf_mutex);
2551 else
2552 mutex_lock(&res->dev_priv->cmdbuf_mutex);
2554 if (unlikely(ret != 0))
2555 return -ERESTARTSYS;
2557 ret = vmw_resource_reserve(res, interruptible, false);
2558 if (ret)
2559 goto out_unlock;
2561 if (res->backup) {
2562 ret = vmw_kms_helper_buffer_prepare(res->dev_priv, res->backup,
2563 interruptible,
2564 res->dev_priv->has_mob);
2565 if (ret)
2566 goto out_unreserve;
2568 ctx->buf = vmw_dmabuf_reference(res->backup);
2570 ret = vmw_resource_validate(res);
2571 if (ret)
2572 goto out_revert;
2573 return 0;
2575 out_revert:
2576 vmw_kms_helper_buffer_revert(ctx->buf);
2577 out_unreserve:
2578 vmw_resource_unreserve(res, false, NULL, 0);
2579 out_unlock:
2580 mutex_unlock(&res->dev_priv->cmdbuf_mutex);
2581 return ret;
2585 * vmw_kms_helper_resource_finish - Unreserve and fence a resource after
2586 * kms command submission.
2588 * @res: Pointer to the resource. Typically a surface.
2589 * @out_fence: Optional pointer to a fence pointer. If non-NULL, a
2590 * ref-counted fence pointer is returned here.
2592 void vmw_kms_helper_resource_finish(struct vmw_validation_ctx *ctx,
2593 struct vmw_fence_obj **out_fence)
2595 struct vmw_resource *res = ctx->res;
2597 if (ctx->buf || out_fence)
2598 vmw_kms_helper_buffer_finish(res->dev_priv, NULL, ctx->buf,
2599 out_fence, NULL);
2601 vmw_dmabuf_unreference(&ctx->buf);
2602 vmw_resource_unreserve(res, false, NULL, 0);
2603 mutex_unlock(&res->dev_priv->cmdbuf_mutex);
2607 * vmw_kms_update_proxy - Helper function to update a proxy surface from
2608 * its backing MOB.
2610 * @res: Pointer to the surface resource
2611 * @clips: Clip rects in framebuffer (surface) space.
2612 * @num_clips: Number of clips in @clips.
2613 * @increment: Integer with which to increment the clip counter when looping.
2614 * Used to skip a predetermined number of clip rects.
2616 * This function makes sure the proxy surface is updated from its backing MOB
2617 * using the region given by @clips. The surface resource @res and its backing
2618 * MOB needs to be reserved and validated on call.
2620 int vmw_kms_update_proxy(struct vmw_resource *res,
2621 const struct drm_clip_rect *clips,
2622 unsigned num_clips,
2623 int increment)
2625 struct vmw_private *dev_priv = res->dev_priv;
2626 struct drm_vmw_size *size = &vmw_res_to_srf(res)->base_size;
2627 struct {
2628 SVGA3dCmdHeader header;
2629 SVGA3dCmdUpdateGBImage body;
2630 } *cmd;
2631 SVGA3dBox *box;
2632 size_t copy_size = 0;
2633 int i;
2635 if (!clips)
2636 return 0;
2638 cmd = vmw_fifo_reserve(dev_priv, sizeof(*cmd) * num_clips);
2639 if (!cmd) {
2640 DRM_ERROR("Couldn't reserve fifo space for proxy surface "
2641 "update.\n");
2642 return -ENOMEM;
2645 for (i = 0; i < num_clips; ++i, clips += increment, ++cmd) {
2646 box = &cmd->body.box;
2648 cmd->header.id = SVGA_3D_CMD_UPDATE_GB_IMAGE;
2649 cmd->header.size = sizeof(cmd->body);
2650 cmd->body.image.sid = res->id;
2651 cmd->body.image.face = 0;
2652 cmd->body.image.mipmap = 0;
2654 if (clips->x1 > size->width || clips->x2 > size->width ||
2655 clips->y1 > size->height || clips->y2 > size->height) {
2656 DRM_ERROR("Invalid clips outsize of framebuffer.\n");
2657 return -EINVAL;
2660 box->x = clips->x1;
2661 box->y = clips->y1;
2662 box->z = 0;
2663 box->w = clips->x2 - clips->x1;
2664 box->h = clips->y2 - clips->y1;
2665 box->d = 1;
2667 copy_size += sizeof(*cmd);
2670 vmw_fifo_commit(dev_priv, copy_size);
2672 return 0;
2675 int vmw_kms_fbdev_init_data(struct vmw_private *dev_priv,
2676 unsigned unit,
2677 u32 max_width,
2678 u32 max_height,
2679 struct drm_connector **p_con,
2680 struct drm_crtc **p_crtc,
2681 struct drm_display_mode **p_mode)
2683 struct drm_connector *con;
2684 struct vmw_display_unit *du;
2685 struct drm_display_mode *mode;
2686 int i = 0;
2688 list_for_each_entry(con, &dev_priv->dev->mode_config.connector_list,
2689 head) {
2690 if (i == unit)
2691 break;
2693 ++i;
2696 if (i != unit) {
2697 DRM_ERROR("Could not find initial display unit.\n");
2698 return -EINVAL;
2701 if (list_empty(&con->modes))
2702 (void) vmw_du_connector_fill_modes(con, max_width, max_height);
2704 if (list_empty(&con->modes)) {
2705 DRM_ERROR("Could not find initial display mode.\n");
2706 return -EINVAL;
2709 du = vmw_connector_to_du(con);
2710 *p_con = con;
2711 *p_crtc = &du->crtc;
2713 list_for_each_entry(mode, &con->modes, head) {
2714 if (mode->type & DRM_MODE_TYPE_PREFERRED)
2715 break;
2718 if (mode->type & DRM_MODE_TYPE_PREFERRED)
2719 *p_mode = mode;
2720 else {
2721 WARN_ONCE(true, "Could not find initial preferred mode.\n");
2722 *p_mode = list_first_entry(&con->modes,
2723 struct drm_display_mode,
2724 head);
2727 return 0;
2731 * vmw_kms_del_active - unregister a crtc binding to the implicit framebuffer
2733 * @dev_priv: Pointer to a device private struct.
2734 * @du: The display unit of the crtc.
2736 void vmw_kms_del_active(struct vmw_private *dev_priv,
2737 struct vmw_display_unit *du)
2739 mutex_lock(&dev_priv->global_kms_state_mutex);
2740 if (du->active_implicit) {
2741 if (--(dev_priv->num_implicit) == 0)
2742 dev_priv->implicit_fb = NULL;
2743 du->active_implicit = false;
2745 mutex_unlock(&dev_priv->global_kms_state_mutex);
2749 * vmw_kms_add_active - register a crtc binding to an implicit framebuffer
2751 * @vmw_priv: Pointer to a device private struct.
2752 * @du: The display unit of the crtc.
2753 * @vfb: The implicit framebuffer
2755 * Registers a binding to an implicit framebuffer.
2757 void vmw_kms_add_active(struct vmw_private *dev_priv,
2758 struct vmw_display_unit *du,
2759 struct vmw_framebuffer *vfb)
2761 mutex_lock(&dev_priv->global_kms_state_mutex);
2762 WARN_ON_ONCE(!dev_priv->num_implicit && dev_priv->implicit_fb);
2764 if (!du->active_implicit && du->is_implicit) {
2765 dev_priv->implicit_fb = vfb;
2766 du->active_implicit = true;
2767 dev_priv->num_implicit++;
2769 mutex_unlock(&dev_priv->global_kms_state_mutex);
2773 * vmw_kms_screen_object_flippable - Check whether we can page-flip a crtc.
2775 * @dev_priv: Pointer to device-private struct.
2776 * @crtc: The crtc we want to flip.
2778 * Returns true or false depending whether it's OK to flip this crtc
2779 * based on the criterion that we must not have more than one implicit
2780 * frame-buffer at any one time.
2782 bool vmw_kms_crtc_flippable(struct vmw_private *dev_priv,
2783 struct drm_crtc *crtc)
2785 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
2786 bool ret;
2788 mutex_lock(&dev_priv->global_kms_state_mutex);
2789 ret = !du->is_implicit || dev_priv->num_implicit == 1;
2790 mutex_unlock(&dev_priv->global_kms_state_mutex);
2792 return ret;
2796 * vmw_kms_update_implicit_fb - Update the implicit fb.
2798 * @dev_priv: Pointer to device-private struct.
2799 * @crtc: The crtc the new implicit frame-buffer is bound to.
2801 void vmw_kms_update_implicit_fb(struct vmw_private *dev_priv,
2802 struct drm_crtc *crtc)
2804 struct vmw_display_unit *du = vmw_crtc_to_du(crtc);
2805 struct vmw_framebuffer *vfb;
2807 mutex_lock(&dev_priv->global_kms_state_mutex);
2809 if (!du->is_implicit)
2810 goto out_unlock;
2812 vfb = vmw_framebuffer_to_vfb(crtc->primary->fb);
2813 WARN_ON_ONCE(dev_priv->num_implicit != 1 &&
2814 dev_priv->implicit_fb != vfb);
2816 dev_priv->implicit_fb = vfb;
2817 out_unlock:
2818 mutex_unlock(&dev_priv->global_kms_state_mutex);
2822 * vmw_kms_create_implicit_placement_proparty - Set up the implicit placement
2823 * property.
2825 * @dev_priv: Pointer to a device private struct.
2826 * @immutable: Whether the property is immutable.
2828 * Sets up the implicit placement property unless it's already set up.
2830 void
2831 vmw_kms_create_implicit_placement_property(struct vmw_private *dev_priv,
2832 bool immutable)
2834 if (dev_priv->implicit_placement_property)
2835 return;
2837 dev_priv->implicit_placement_property =
2838 drm_property_create_range(dev_priv->dev,
2839 immutable ?
2840 DRM_MODE_PROP_IMMUTABLE : 0,
2841 "implicit_placement", 0, 1);
2847 * vmw_kms_set_config - Wrapper around drm_atomic_helper_set_config
2849 * @set: The configuration to set.
2851 * The vmwgfx Xorg driver doesn't assign the mode::type member, which
2852 * when drm_mode_set_crtcinfo is called as part of the configuration setting
2853 * causes it to return incorrect crtc dimensions causing severe problems in
2854 * the vmwgfx modesetting. So explicitly clear that member before calling
2855 * into drm_atomic_helper_set_config.
2857 int vmw_kms_set_config(struct drm_mode_set *set,
2858 struct drm_modeset_acquire_ctx *ctx)
2860 if (set && set->mode)
2861 set->mode->type = 0;
2863 return drm_atomic_helper_set_config(set, ctx);
2868 * vmw_kms_lost_device - Notify kms that modesetting capabilities will be lost
2870 * @dev: Pointer to the drm device
2872 void vmw_kms_lost_device(struct drm_device *dev)
2874 drm_atomic_helper_shutdown(dev);