1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /**************************************************************************
4 * Copyright © 2011-2018 VMware, Inc., Palo Alto, CA., USA
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sub license, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial portions
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
22 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
23 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
25 * USE OR OTHER DEALINGS IN THE SOFTWARE.
27 **************************************************************************/
29 #include <drm/ttm/ttm_placement.h>
31 #include "vmwgfx_drv.h"
32 #include "ttm_object.h"
36 * struct vmw_user_buffer_object - User-space-visible buffer object
38 * @prime: The prime object providing user visibility.
39 * @vbo: The struct vmw_buffer_object
41 struct vmw_user_buffer_object {
42 struct ttm_prime_object prime;
43 struct vmw_buffer_object vbo;
48 * vmw_buffer_object - Convert a struct ttm_buffer_object to a struct
51 * @bo: Pointer to the TTM buffer object.
52 * Return: Pointer to the struct vmw_buffer_object embedding the
55 static struct vmw_buffer_object *
56 vmw_buffer_object(struct ttm_buffer_object *bo)
58 return container_of(bo, struct vmw_buffer_object, base);
63 * vmw_user_buffer_object - Convert a struct ttm_buffer_object to a struct
64 * vmw_user_buffer_object.
66 * @bo: Pointer to the TTM buffer object.
67 * Return: Pointer to the struct vmw_buffer_object embedding the TTM buffer
70 static struct vmw_user_buffer_object *
71 vmw_user_buffer_object(struct ttm_buffer_object *bo)
73 struct vmw_buffer_object *vmw_bo = vmw_buffer_object(bo);
75 return container_of(vmw_bo, struct vmw_user_buffer_object, vbo);
80 * vmw_bo_pin_in_placement - Validate a buffer to placement.
82 * @dev_priv: Driver private.
83 * @buf: DMA buffer to move.
84 * @placement: The placement to pin it.
85 * @interruptible: Use interruptible wait.
86 * Return: Zero on success, Negative error code on failure. In particular
87 * -ERESTARTSYS if interrupted by a signal
89 int vmw_bo_pin_in_placement(struct vmw_private *dev_priv,
90 struct vmw_buffer_object *buf,
91 struct ttm_placement *placement,
94 struct ttm_operation_ctx ctx = {interruptible, false };
95 struct ttm_buffer_object *bo = &buf->base;
99 vmw_execbuf_release_pinned_bo(dev_priv);
101 ret = ttm_bo_reserve(bo, interruptible, false, NULL);
102 if (unlikely(ret != 0))
105 if (buf->base.pin_count > 0)
106 ret = ttm_bo_mem_compat(placement, bo->resource,
107 &new_flags) == true ? 0 : -EINVAL;
109 ret = ttm_bo_validate(bo, placement, &ctx);
112 vmw_bo_pin_reserved(buf, true);
114 ttm_bo_unreserve(bo);
121 * vmw_bo_pin_in_vram_or_gmr - Move a buffer to vram or gmr.
123 * This function takes the reservation_sem in write mode.
124 * Flushes and unpins the query bo to avoid failures.
126 * @dev_priv: Driver private.
127 * @buf: DMA buffer to move.
128 * @interruptible: Use interruptible wait.
129 * Return: Zero on success, Negative error code on failure. In particular
130 * -ERESTARTSYS if interrupted by a signal
132 int vmw_bo_pin_in_vram_or_gmr(struct vmw_private *dev_priv,
133 struct vmw_buffer_object *buf,
136 struct ttm_operation_ctx ctx = {interruptible, false };
137 struct ttm_buffer_object *bo = &buf->base;
141 vmw_execbuf_release_pinned_bo(dev_priv);
143 ret = ttm_bo_reserve(bo, interruptible, false, NULL);
144 if (unlikely(ret != 0))
147 if (buf->base.pin_count > 0) {
148 ret = ttm_bo_mem_compat(&vmw_vram_gmr_placement, bo->resource,
149 &new_flags) == true ? 0 : -EINVAL;
153 ret = ttm_bo_validate(bo, &vmw_vram_gmr_placement, &ctx);
154 if (likely(ret == 0) || ret == -ERESTARTSYS)
157 ret = ttm_bo_validate(bo, &vmw_vram_placement, &ctx);
161 vmw_bo_pin_reserved(buf, true);
163 ttm_bo_unreserve(bo);
170 * vmw_bo_pin_in_vram - Move a buffer to vram.
172 * This function takes the reservation_sem in write mode.
173 * Flushes and unpins the query bo to avoid failures.
175 * @dev_priv: Driver private.
176 * @buf: DMA buffer to move.
177 * @interruptible: Use interruptible wait.
178 * Return: Zero on success, Negative error code on failure. In particular
179 * -ERESTARTSYS if interrupted by a signal
181 int vmw_bo_pin_in_vram(struct vmw_private *dev_priv,
182 struct vmw_buffer_object *buf,
185 return vmw_bo_pin_in_placement(dev_priv, buf, &vmw_vram_placement,
191 * vmw_bo_pin_in_start_of_vram - Move a buffer to start of vram.
193 * This function takes the reservation_sem in write mode.
194 * Flushes and unpins the query bo to avoid failures.
196 * @dev_priv: Driver private.
197 * @buf: DMA buffer to pin.
198 * @interruptible: Use interruptible wait.
199 * Return: Zero on success, Negative error code on failure. In particular
200 * -ERESTARTSYS if interrupted by a signal
202 int vmw_bo_pin_in_start_of_vram(struct vmw_private *dev_priv,
203 struct vmw_buffer_object *buf,
206 struct ttm_operation_ctx ctx = {interruptible, false };
207 struct ttm_buffer_object *bo = &buf->base;
208 struct ttm_placement placement;
209 struct ttm_place place;
213 place = vmw_vram_placement.placement[0];
214 place.lpfn = bo->resource->num_pages;
215 placement.num_placement = 1;
216 placement.placement = &place;
217 placement.num_busy_placement = 1;
218 placement.busy_placement = &place;
220 vmw_execbuf_release_pinned_bo(dev_priv);
221 ret = ttm_bo_reserve(bo, interruptible, false, NULL);
222 if (unlikely(ret != 0))
226 * Is this buffer already in vram but not at the start of it?
227 * In that case, evict it first because TTM isn't good at handling
230 if (bo->resource->mem_type == TTM_PL_VRAM &&
231 bo->resource->start < bo->resource->num_pages &&
232 bo->resource->start > 0 &&
233 buf->base.pin_count == 0) {
234 ctx.interruptible = false;
235 (void) ttm_bo_validate(bo, &vmw_sys_placement, &ctx);
238 if (buf->base.pin_count > 0)
239 ret = ttm_bo_mem_compat(&placement, bo->resource,
240 &new_flags) == true ? 0 : -EINVAL;
242 ret = ttm_bo_validate(bo, &placement, &ctx);
244 /* For some reason we didn't end up at the start of vram */
245 WARN_ON(ret == 0 && bo->resource->start != 0);
247 vmw_bo_pin_reserved(buf, true);
249 ttm_bo_unreserve(bo);
257 * vmw_bo_unpin - Unpin the buffer given buffer, does not move the buffer.
259 * This function takes the reservation_sem in write mode.
261 * @dev_priv: Driver private.
262 * @buf: DMA buffer to unpin.
263 * @interruptible: Use interruptible wait.
264 * Return: Zero on success, Negative error code on failure. In particular
265 * -ERESTARTSYS if interrupted by a signal
267 int vmw_bo_unpin(struct vmw_private *dev_priv,
268 struct vmw_buffer_object *buf,
271 struct ttm_buffer_object *bo = &buf->base;
274 ret = ttm_bo_reserve(bo, interruptible, false, NULL);
275 if (unlikely(ret != 0))
278 vmw_bo_pin_reserved(buf, false);
280 ttm_bo_unreserve(bo);
287 * vmw_bo_get_guest_ptr - Get the guest ptr representing the current placement
290 * @bo: Pointer to a struct ttm_buffer_object. Must be pinned or reserved.
291 * @ptr: SVGAGuestPtr returning the result.
293 void vmw_bo_get_guest_ptr(const struct ttm_buffer_object *bo,
296 if (bo->resource->mem_type == TTM_PL_VRAM) {
297 ptr->gmrId = SVGA_GMR_FRAMEBUFFER;
298 ptr->offset = bo->resource->start << PAGE_SHIFT;
300 ptr->gmrId = bo->resource->start;
307 * vmw_bo_pin_reserved - Pin or unpin a buffer object without moving it.
309 * @vbo: The buffer object. Must be reserved.
310 * @pin: Whether to pin or unpin.
313 void vmw_bo_pin_reserved(struct vmw_buffer_object *vbo, bool pin)
315 struct ttm_operation_ctx ctx = { false, true };
317 struct ttm_placement placement;
318 struct ttm_buffer_object *bo = &vbo->base;
319 uint32_t old_mem_type = bo->resource->mem_type;
322 dma_resv_assert_held(bo->base.resv);
324 if (pin == !!bo->pin_count)
329 pl.mem_type = bo->resource->mem_type;
330 pl.flags = bo->resource->placement;
332 memset(&placement, 0, sizeof(placement));
333 placement.num_placement = 1;
334 placement.placement = &pl;
336 ret = ttm_bo_validate(bo, &placement, &ctx);
338 BUG_ON(ret != 0 || bo->resource->mem_type != old_mem_type);
347 * vmw_bo_map_and_cache - Map a buffer object and cache the map
349 * @vbo: The buffer object to map
350 * Return: A kernel virtual address or NULL if mapping failed.
352 * This function maps a buffer object into the kernel address space, or
353 * returns the virtual kernel address of an already existing map. The virtual
354 * address remains valid as long as the buffer object is pinned or reserved.
355 * The cached map is torn down on either
356 * 1) Buffer object move
357 * 2) Buffer object swapout
358 * 3) Buffer object destruction
361 void *vmw_bo_map_and_cache(struct vmw_buffer_object *vbo)
363 struct ttm_buffer_object *bo = &vbo->base;
368 virtual = ttm_kmap_obj_virtual(&vbo->map, ¬_used);
372 ret = ttm_bo_kmap(bo, 0, bo->resource->num_pages, &vbo->map);
374 DRM_ERROR("Buffer object map failed: %d.\n", ret);
376 return ttm_kmap_obj_virtual(&vbo->map, ¬_used);
381 * vmw_bo_unmap - Tear down a cached buffer object map.
383 * @vbo: The buffer object whose map we are tearing down.
385 * This function tears down a cached map set up using
386 * vmw_buffer_object_map_and_cache().
388 void vmw_bo_unmap(struct vmw_buffer_object *vbo)
390 if (vbo->map.bo == NULL)
393 ttm_bo_kunmap(&vbo->map);
398 * vmw_bo_acc_size - Calculate the pinned memory usage of buffers
400 * @dev_priv: Pointer to a struct vmw_private identifying the device.
401 * @size: The requested buffer size.
402 * @user: Whether this is an ordinary dma buffer or a user dma buffer.
404 static size_t vmw_bo_acc_size(struct vmw_private *dev_priv, size_t size,
407 static size_t struct_size, user_struct_size;
408 size_t num_pages = PAGE_ALIGN(size) >> PAGE_SHIFT;
409 size_t page_array_size = ttm_round_pot(num_pages * sizeof(void *));
411 if (unlikely(struct_size == 0)) {
412 size_t backend_size = ttm_round_pot(vmw_tt_size);
414 struct_size = backend_size +
415 ttm_round_pot(sizeof(struct vmw_buffer_object));
416 user_struct_size = backend_size +
417 ttm_round_pot(sizeof(struct vmw_user_buffer_object)) +
421 if (dev_priv->map_mode == vmw_dma_alloc_coherent)
423 ttm_round_pot(num_pages * sizeof(dma_addr_t));
425 return ((user) ? user_struct_size : struct_size) +
431 * vmw_bo_bo_free - vmw buffer object destructor
433 * @bo: Pointer to the embedded struct ttm_buffer_object
435 void vmw_bo_bo_free(struct ttm_buffer_object *bo)
437 struct vmw_buffer_object *vmw_bo = vmw_buffer_object(bo);
439 WARN_ON(vmw_bo->dirty);
440 WARN_ON(!RB_EMPTY_ROOT(&vmw_bo->res_tree));
441 vmw_bo_unmap(vmw_bo);
442 dma_resv_fini(&bo->base._resv);
448 * vmw_user_bo_destroy - vmw buffer object destructor
450 * @bo: Pointer to the embedded struct ttm_buffer_object
452 static void vmw_user_bo_destroy(struct ttm_buffer_object *bo)
454 struct vmw_user_buffer_object *vmw_user_bo = vmw_user_buffer_object(bo);
455 struct vmw_buffer_object *vbo = &vmw_user_bo->vbo;
458 WARN_ON(!RB_EMPTY_ROOT(&vbo->res_tree));
460 ttm_prime_object_kfree(vmw_user_bo, prime);
464 * vmw_bo_create_kernel - Create a pinned BO for internal kernel use.
466 * @dev_priv: Pointer to the device private struct
467 * @size: size of the BO we need
468 * @placement: where to put it
469 * @p_bo: resulting BO
471 * Creates and pin a simple BO for in kernel use.
473 int vmw_bo_create_kernel(struct vmw_private *dev_priv, unsigned long size,
474 struct ttm_placement *placement,
475 struct ttm_buffer_object **p_bo)
477 unsigned npages = PAGE_ALIGN(size) >> PAGE_SHIFT;
478 struct ttm_operation_ctx ctx = { false, false };
479 struct ttm_buffer_object *bo;
483 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
487 acc_size = ttm_round_pot(sizeof(*bo));
488 acc_size += ttm_round_pot(npages * sizeof(void *));
489 acc_size += ttm_round_pot(sizeof(struct ttm_tt));
491 ret = ttm_mem_global_alloc(&ttm_mem_glob, acc_size, &ctx);
496 bo->base.size = size;
497 dma_resv_init(&bo->base._resv);
498 drm_vma_node_reset(&bo->base.vma_node);
500 ret = ttm_bo_init_reserved(&dev_priv->bdev, bo, size,
501 ttm_bo_type_device, placement, 0,
502 &ctx, NULL, NULL, NULL);
507 ttm_bo_unreserve(bo);
513 ttm_mem_global_free(&ttm_mem_glob, acc_size);
521 * vmw_bo_init - Initialize a vmw buffer object
523 * @dev_priv: Pointer to the device private struct
524 * @vmw_bo: Pointer to the struct vmw_buffer_object to initialize.
525 * @size: Buffer object size in bytes.
526 * @placement: Initial placement.
527 * @interruptible: Whether waits should be performed interruptible.
528 * @pin: If the BO should be created pinned at a fixed location.
529 * @bo_free: The buffer object destructor.
530 * Returns: Zero on success, negative error code on error.
532 * Note that on error, the code will free the buffer object.
534 int vmw_bo_init(struct vmw_private *dev_priv,
535 struct vmw_buffer_object *vmw_bo,
536 size_t size, struct ttm_placement *placement,
537 bool interruptible, bool pin,
538 void (*bo_free)(struct ttm_buffer_object *bo))
540 struct ttm_operation_ctx ctx = { interruptible, false };
541 struct ttm_device *bdev = &dev_priv->bdev;
544 bool user = (bo_free == &vmw_user_bo_destroy);
546 WARN_ON_ONCE(!bo_free && (!user && (bo_free != vmw_bo_bo_free)));
548 acc_size = vmw_bo_acc_size(dev_priv, size, user);
549 memset(vmw_bo, 0, sizeof(*vmw_bo));
550 BUILD_BUG_ON(TTM_MAX_BO_PRIORITY <= 3);
551 vmw_bo->base.priority = 3;
552 vmw_bo->res_tree = RB_ROOT;
554 ret = ttm_mem_global_alloc(&ttm_mem_glob, acc_size, &ctx);
558 vmw_bo->base.base.size = size;
559 dma_resv_init(&vmw_bo->base.base._resv);
560 drm_vma_node_reset(&vmw_bo->base.base.vma_node);
562 ret = ttm_bo_init_reserved(bdev, &vmw_bo->base, size,
563 ttm_bo_type_device, placement,
564 0, &ctx, NULL, NULL, bo_free);
566 ttm_mem_global_free(&ttm_mem_glob, acc_size);
571 ttm_bo_pin(&vmw_bo->base);
572 ttm_bo_unreserve(&vmw_bo->base);
578 * vmw_user_bo_release - TTM reference base object release callback for
579 * vmw user buffer objects
581 * @p_base: The TTM base object pointer about to be unreferenced.
583 * Clears the TTM base object pointer and drops the reference the
584 * base object has on the underlying struct vmw_buffer_object.
586 static void vmw_user_bo_release(struct ttm_base_object **p_base)
588 struct vmw_user_buffer_object *vmw_user_bo;
589 struct ttm_base_object *base = *p_base;
593 if (unlikely(base == NULL))
596 vmw_user_bo = container_of(base, struct vmw_user_buffer_object,
598 ttm_bo_put(&vmw_user_bo->vbo.base);
603 * vmw_user_bo_ref_obj_release - TTM synccpu reference object release callback
604 * for vmw user buffer objects
606 * @base: Pointer to the TTM base object
607 * @ref_type: Reference type of the reference reaching zero.
609 * Called when user-space drops its last synccpu reference on the buffer
610 * object, Either explicitly or as part of a cleanup file close.
612 static void vmw_user_bo_ref_obj_release(struct ttm_base_object *base,
613 enum ttm_ref_type ref_type)
615 struct vmw_user_buffer_object *user_bo;
617 user_bo = container_of(base, struct vmw_user_buffer_object, prime.base);
620 case TTM_REF_SYNCCPU_WRITE:
621 atomic_dec(&user_bo->vbo.cpu_writers);
624 WARN_ONCE(true, "Undefined buffer object reference release.\n");
630 * vmw_user_bo_alloc - Allocate a user buffer object
632 * @dev_priv: Pointer to a struct device private.
633 * @tfile: Pointer to a struct ttm_object_file on which to register the user
635 * @size: Size of the buffer object.
636 * @shareable: Boolean whether the buffer is shareable with other open files.
637 * @handle: Pointer to where the handle value should be assigned.
638 * @p_vbo: Pointer to where the refcounted struct vmw_buffer_object pointer
639 * should be assigned.
640 * @p_base: The TTM base object pointer about to be allocated.
641 * Return: Zero on success, negative error code on error.
643 int vmw_user_bo_alloc(struct vmw_private *dev_priv,
644 struct ttm_object_file *tfile,
648 struct vmw_buffer_object **p_vbo,
649 struct ttm_base_object **p_base)
651 struct vmw_user_buffer_object *user_bo;
654 user_bo = kzalloc(sizeof(*user_bo), GFP_KERNEL);
655 if (unlikely(!user_bo)) {
656 DRM_ERROR("Failed to allocate a buffer.\n");
660 ret = vmw_bo_init(dev_priv, &user_bo->vbo, size,
661 (dev_priv->has_mob) ?
663 &vmw_vram_sys_placement, true, false,
664 &vmw_user_bo_destroy);
665 if (unlikely(ret != 0))
668 ttm_bo_get(&user_bo->vbo.base);
669 ret = ttm_prime_object_init(tfile,
674 &vmw_user_bo_release,
675 &vmw_user_bo_ref_obj_release);
676 if (unlikely(ret != 0)) {
677 ttm_bo_put(&user_bo->vbo.base);
678 goto out_no_base_object;
681 *p_vbo = &user_bo->vbo;
683 *p_base = &user_bo->prime.base;
684 kref_get(&(*p_base)->refcount);
686 *handle = user_bo->prime.base.handle;
694 * vmw_user_bo_verify_access - verify access permissions on this
697 * @bo: Pointer to the buffer object being accessed
698 * @tfile: Identifying the caller.
700 int vmw_user_bo_verify_access(struct ttm_buffer_object *bo,
701 struct ttm_object_file *tfile)
703 struct vmw_user_buffer_object *vmw_user_bo;
705 if (unlikely(bo->destroy != vmw_user_bo_destroy))
708 vmw_user_bo = vmw_user_buffer_object(bo);
710 /* Check that the caller has opened the object. */
711 if (likely(ttm_ref_object_exists(tfile, &vmw_user_bo->prime.base)))
714 DRM_ERROR("Could not grant buffer access.\n");
720 * vmw_user_bo_synccpu_grab - Grab a struct vmw_user_buffer_object for cpu
721 * access, idling previous GPU operations on the buffer and optionally
722 * blocking it for further command submissions.
724 * @user_bo: Pointer to the buffer object being grabbed for CPU access
725 * @tfile: Identifying the caller.
726 * @flags: Flags indicating how the grab should be performed.
727 * Return: Zero on success, Negative error code on error. In particular,
728 * -EBUSY will be returned if a dontblock operation is requested and the
729 * buffer object is busy, and -ERESTARTSYS will be returned if a wait is
730 * interrupted by a signal.
732 * A blocking grab will be automatically released when @tfile is closed.
734 static int vmw_user_bo_synccpu_grab(struct vmw_user_buffer_object *user_bo,
735 struct ttm_object_file *tfile,
738 bool nonblock = !!(flags & drm_vmw_synccpu_dontblock);
739 struct ttm_buffer_object *bo = &user_bo->vbo.base;
743 if (flags & drm_vmw_synccpu_allow_cs) {
746 lret = dma_resv_wait_timeout(bo->base.resv, true, true,
748 MAX_SCHEDULE_TIMEOUT);
756 ret = ttm_bo_reserve(bo, true, nonblock, NULL);
757 if (unlikely(ret != 0))
760 ret = ttm_bo_wait(bo, true, nonblock);
761 if (likely(ret == 0))
762 atomic_inc(&user_bo->vbo.cpu_writers);
764 ttm_bo_unreserve(bo);
765 if (unlikely(ret != 0))
768 ret = ttm_ref_object_add(tfile, &user_bo->prime.base,
769 TTM_REF_SYNCCPU_WRITE, &existed, false);
770 if (ret != 0 || existed)
771 atomic_dec(&user_bo->vbo.cpu_writers);
777 * vmw_user_bo_synccpu_release - Release a previous grab for CPU access,
778 * and unblock command submission on the buffer if blocked.
780 * @handle: Handle identifying the buffer object.
781 * @tfile: Identifying the caller.
782 * @flags: Flags indicating the type of release.
784 static int vmw_user_bo_synccpu_release(uint32_t handle,
785 struct ttm_object_file *tfile,
788 if (!(flags & drm_vmw_synccpu_allow_cs))
789 return ttm_ref_object_base_unref(tfile, handle,
790 TTM_REF_SYNCCPU_WRITE);
797 * vmw_user_bo_synccpu_ioctl - ioctl function implementing the synccpu
800 * @dev: Identifies the drm device.
801 * @data: Pointer to the ioctl argument.
802 * @file_priv: Identifies the caller.
803 * Return: Zero on success, negative error code on error.
805 * This function checks the ioctl arguments for validity and calls the
806 * relevant synccpu functions.
808 int vmw_user_bo_synccpu_ioctl(struct drm_device *dev, void *data,
809 struct drm_file *file_priv)
811 struct drm_vmw_synccpu_arg *arg =
812 (struct drm_vmw_synccpu_arg *) data;
813 struct vmw_buffer_object *vbo;
814 struct vmw_user_buffer_object *user_bo;
815 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
816 struct ttm_base_object *buffer_base;
819 if ((arg->flags & (drm_vmw_synccpu_read | drm_vmw_synccpu_write)) == 0
820 || (arg->flags & ~(drm_vmw_synccpu_read | drm_vmw_synccpu_write |
821 drm_vmw_synccpu_dontblock |
822 drm_vmw_synccpu_allow_cs)) != 0) {
823 DRM_ERROR("Illegal synccpu flags.\n");
828 case drm_vmw_synccpu_grab:
829 ret = vmw_user_bo_lookup(tfile, arg->handle, &vbo,
831 if (unlikely(ret != 0))
834 user_bo = container_of(vbo, struct vmw_user_buffer_object,
836 ret = vmw_user_bo_synccpu_grab(user_bo, tfile, arg->flags);
837 vmw_bo_unreference(&vbo);
838 ttm_base_object_unref(&buffer_base);
839 if (unlikely(ret != 0 && ret != -ERESTARTSYS &&
841 DRM_ERROR("Failed synccpu grab on handle 0x%08x.\n",
842 (unsigned int) arg->handle);
846 case drm_vmw_synccpu_release:
847 ret = vmw_user_bo_synccpu_release(arg->handle, tfile,
849 if (unlikely(ret != 0)) {
850 DRM_ERROR("Failed synccpu release on handle 0x%08x.\n",
851 (unsigned int) arg->handle);
856 DRM_ERROR("Invalid synccpu operation.\n");
865 * vmw_bo_alloc_ioctl - ioctl function implementing the buffer object
866 * allocation functionality.
868 * @dev: Identifies the drm device.
869 * @data: Pointer to the ioctl argument.
870 * @file_priv: Identifies the caller.
871 * Return: Zero on success, negative error code on error.
873 * This function checks the ioctl arguments for validity and allocates a
874 * struct vmw_user_buffer_object bo.
876 int vmw_bo_alloc_ioctl(struct drm_device *dev, void *data,
877 struct drm_file *file_priv)
879 struct vmw_private *dev_priv = vmw_priv(dev);
880 union drm_vmw_alloc_dmabuf_arg *arg =
881 (union drm_vmw_alloc_dmabuf_arg *)data;
882 struct drm_vmw_alloc_dmabuf_req *req = &arg->req;
883 struct drm_vmw_dmabuf_rep *rep = &arg->rep;
884 struct vmw_buffer_object *vbo;
888 ret = vmw_user_bo_alloc(dev_priv, vmw_fpriv(file_priv)->tfile,
889 req->size, false, &handle, &vbo,
891 if (unlikely(ret != 0))
894 rep->handle = handle;
895 rep->map_handle = drm_vma_node_offset_addr(&vbo->base.base.vma_node);
896 rep->cur_gmr_id = handle;
897 rep->cur_gmr_offset = 0;
899 vmw_bo_unreference(&vbo);
908 * vmw_bo_unref_ioctl - Generic handle close ioctl.
910 * @dev: Identifies the drm device.
911 * @data: Pointer to the ioctl argument.
912 * @file_priv: Identifies the caller.
913 * Return: Zero on success, negative error code on error.
915 * This function checks the ioctl arguments for validity and closes a
916 * handle to a TTM base object, optionally freeing the object.
918 int vmw_bo_unref_ioctl(struct drm_device *dev, void *data,
919 struct drm_file *file_priv)
921 struct drm_vmw_unref_dmabuf_arg *arg =
922 (struct drm_vmw_unref_dmabuf_arg *)data;
924 return ttm_ref_object_base_unref(vmw_fpriv(file_priv)->tfile,
931 * vmw_user_bo_lookup - Look up a vmw user buffer object from a handle.
933 * @tfile: The TTM object file the handle is registered with.
934 * @handle: The user buffer object handle
935 * @out: Pointer to a where a pointer to the embedded
936 * struct vmw_buffer_object should be placed.
937 * @p_base: Pointer to where a pointer to the TTM base object should be
938 * placed, or NULL if no such pointer is required.
939 * Return: Zero on success, Negative error code on error.
941 * Both the output base object pointer and the vmw buffer object pointer
942 * will be refcounted.
944 int vmw_user_bo_lookup(struct ttm_object_file *tfile,
945 uint32_t handle, struct vmw_buffer_object **out,
946 struct ttm_base_object **p_base)
948 struct vmw_user_buffer_object *vmw_user_bo;
949 struct ttm_base_object *base;
951 base = ttm_base_object_lookup(tfile, handle);
952 if (unlikely(base == NULL)) {
953 DRM_ERROR("Invalid buffer object handle 0x%08lx.\n",
954 (unsigned long)handle);
958 if (unlikely(ttm_base_object_type(base) != ttm_buffer_type)) {
959 ttm_base_object_unref(&base);
960 DRM_ERROR("Invalid buffer object handle 0x%08lx.\n",
961 (unsigned long)handle);
965 vmw_user_bo = container_of(base, struct vmw_user_buffer_object,
967 ttm_bo_get(&vmw_user_bo->vbo.base);
971 ttm_base_object_unref(&base);
972 *out = &vmw_user_bo->vbo;
978 * vmw_user_bo_noref_lookup - Look up a vmw user buffer object without reference
979 * @tfile: The TTM object file the handle is registered with.
980 * @handle: The user buffer object handle.
982 * This function looks up a struct vmw_user_bo and returns a pointer to the
983 * struct vmw_buffer_object it derives from without refcounting the pointer.
984 * The returned pointer is only valid until vmw_user_bo_noref_release() is
985 * called, and the object pointed to by the returned pointer may be doomed.
986 * Any persistent usage of the object requires a refcount to be taken using
987 * ttm_bo_reference_unless_doomed(). Iff this function returns successfully it
988 * needs to be paired with vmw_user_bo_noref_release() and no sleeping-
989 * or scheduling functions may be called inbetween these function calls.
991 * Return: A struct vmw_buffer_object pointer if successful or negative
992 * error pointer on failure.
994 struct vmw_buffer_object *
995 vmw_user_bo_noref_lookup(struct ttm_object_file *tfile, u32 handle)
997 struct vmw_user_buffer_object *vmw_user_bo;
998 struct ttm_base_object *base;
1000 base = ttm_base_object_noref_lookup(tfile, handle);
1002 DRM_ERROR("Invalid buffer object handle 0x%08lx.\n",
1003 (unsigned long)handle);
1004 return ERR_PTR(-ESRCH);
1007 if (unlikely(ttm_base_object_type(base) != ttm_buffer_type)) {
1008 ttm_base_object_noref_release();
1009 DRM_ERROR("Invalid buffer object handle 0x%08lx.\n",
1010 (unsigned long)handle);
1011 return ERR_PTR(-EINVAL);
1014 vmw_user_bo = container_of(base, struct vmw_user_buffer_object,
1016 return &vmw_user_bo->vbo;
1020 * vmw_user_bo_reference - Open a handle to a vmw user buffer object.
1022 * @tfile: The TTM object file to register the handle with.
1023 * @vbo: The embedded vmw buffer object.
1024 * @handle: Pointer to where the new handle should be placed.
1025 * Return: Zero on success, Negative error code on error.
1027 int vmw_user_bo_reference(struct ttm_object_file *tfile,
1028 struct vmw_buffer_object *vbo,
1031 struct vmw_user_buffer_object *user_bo;
1033 if (vbo->base.destroy != vmw_user_bo_destroy)
1036 user_bo = container_of(vbo, struct vmw_user_buffer_object, vbo);
1038 *handle = user_bo->prime.base.handle;
1039 return ttm_ref_object_add(tfile, &user_bo->prime.base,
1040 TTM_REF_USAGE, NULL, false);
1045 * vmw_bo_fence_single - Utility function to fence a single TTM buffer
1046 * object without unreserving it.
1048 * @bo: Pointer to the struct ttm_buffer_object to fence.
1049 * @fence: Pointer to the fence. If NULL, this function will
1050 * insert a fence into the command stream..
1052 * Contrary to the ttm_eu version of this function, it takes only
1053 * a single buffer object instead of a list, and it also doesn't
1054 * unreserve the buffer object, which needs to be done separately.
1056 void vmw_bo_fence_single(struct ttm_buffer_object *bo,
1057 struct vmw_fence_obj *fence)
1059 struct ttm_device *bdev = bo->bdev;
1061 struct vmw_private *dev_priv =
1062 container_of(bdev, struct vmw_private, bdev);
1064 if (fence == NULL) {
1065 vmw_execbuf_fence_commands(NULL, dev_priv, &fence, NULL);
1066 dma_resv_add_excl_fence(bo->base.resv, &fence->base);
1067 dma_fence_put(&fence->base);
1069 dma_resv_add_excl_fence(bo->base.resv, &fence->base);
1074 * vmw_dumb_create - Create a dumb kms buffer
1076 * @file_priv: Pointer to a struct drm_file identifying the caller.
1077 * @dev: Pointer to the drm device.
1078 * @args: Pointer to a struct drm_mode_create_dumb structure
1079 * Return: Zero on success, negative error code on failure.
1081 * This is a driver callback for the core drm create_dumb functionality.
1082 * Note that this is very similar to the vmw_bo_alloc ioctl, except
1083 * that the arguments have a different format.
1085 int vmw_dumb_create(struct drm_file *file_priv,
1086 struct drm_device *dev,
1087 struct drm_mode_create_dumb *args)
1089 struct vmw_private *dev_priv = vmw_priv(dev);
1090 struct vmw_buffer_object *vbo;
1093 args->pitch = args->width * ((args->bpp + 7) / 8);
1094 args->size = args->pitch * args->height;
1096 ret = vmw_user_bo_alloc(dev_priv, vmw_fpriv(file_priv)->tfile,
1097 args->size, false, &args->handle,
1099 if (unlikely(ret != 0))
1102 vmw_bo_unreference(&vbo);
1109 * vmw_dumb_map_offset - Return the address space offset of a dumb buffer
1111 * @file_priv: Pointer to a struct drm_file identifying the caller.
1112 * @dev: Pointer to the drm device.
1113 * @handle: Handle identifying the dumb buffer.
1114 * @offset: The address space offset returned.
1115 * Return: Zero on success, negative error code on failure.
1117 * This is a driver callback for the core drm dumb_map_offset functionality.
1119 int vmw_dumb_map_offset(struct drm_file *file_priv,
1120 struct drm_device *dev, uint32_t handle,
1123 struct ttm_object_file *tfile = vmw_fpriv(file_priv)->tfile;
1124 struct vmw_buffer_object *out_buf;
1127 ret = vmw_user_bo_lookup(tfile, handle, &out_buf, NULL);
1131 *offset = drm_vma_node_offset_addr(&out_buf->base.base.vma_node);
1132 vmw_bo_unreference(&out_buf);
1138 * vmw_dumb_destroy - Destroy a dumb boffer
1140 * @file_priv: Pointer to a struct drm_file identifying the caller.
1141 * @dev: Pointer to the drm device.
1142 * @handle: Handle identifying the dumb buffer.
1143 * Return: Zero on success, negative error code on failure.
1145 * This is a driver callback for the core drm dumb_destroy functionality.
1147 int vmw_dumb_destroy(struct drm_file *file_priv,
1148 struct drm_device *dev,
1151 return ttm_ref_object_base_unref(vmw_fpriv(file_priv)->tfile,
1152 handle, TTM_REF_USAGE);
1157 * vmw_bo_swap_notify - swapout notify callback.
1159 * @bo: The buffer object to be swapped out.
1161 void vmw_bo_swap_notify(struct ttm_buffer_object *bo)
1163 /* Is @bo embedded in a struct vmw_buffer_object? */
1164 if (bo->destroy != vmw_bo_bo_free &&
1165 bo->destroy != vmw_user_bo_destroy)
1168 /* Kill any cached kernel maps before swapout */
1169 vmw_bo_unmap(vmw_buffer_object(bo));
1174 * vmw_bo_move_notify - TTM move_notify_callback
1176 * @bo: The TTM buffer object about to move.
1177 * @mem: The struct ttm_resource indicating to what memory
1178 * region the move is taking place.
1180 * Detaches cached maps and device bindings that require that the
1181 * buffer doesn't move.
1183 void vmw_bo_move_notify(struct ttm_buffer_object *bo,
1184 struct ttm_resource *mem)
1186 struct vmw_buffer_object *vbo;
1188 /* Make sure @bo is embedded in a struct vmw_buffer_object? */
1189 if (bo->destroy != vmw_bo_bo_free &&
1190 bo->destroy != vmw_user_bo_destroy)
1193 vbo = container_of(bo, struct vmw_buffer_object, base);
1196 * Kill any cached kernel maps before move to or from VRAM.
1197 * With other types of moves, the underlying pages stay the same,
1198 * and the map can be kept.
1200 if (mem->mem_type == TTM_PL_VRAM || bo->resource->mem_type == TTM_PL_VRAM)
1204 * If we're moving a backup MOB out of MOB placement, then make sure we
1205 * read back all resource content first, and unbind the MOB from
1208 if (mem->mem_type != VMW_PL_MOB && bo->resource->mem_type == VMW_PL_MOB)
1209 vmw_resource_unbind_list(vbo);