1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2022 Red Hat.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
24 * Danilo Krummrich <dakr@redhat.com>
28 #include <drm/drm_gpuva_mgr.h>
30 #include <linux/interval_tree_generic.h>
36 * The DRM GPU VA Manager, represented by struct drm_gpuva_manager keeps track
37 * of a GPU's virtual address (VA) space and manages the corresponding virtual
38 * mappings represented by &drm_gpuva objects. It also keeps track of the
39 * mapping's backing &drm_gem_object buffers.
41 * &drm_gem_object buffers maintain a list of &drm_gpuva objects representing
42 * all existent GPU VA mappings using this &drm_gem_object as backing buffer.
44 * GPU VAs can be flagged as sparse, such that drivers may use GPU VAs to also
45 * keep track of sparse PTEs in order to support Vulkan 'Sparse Resources'.
47 * The GPU VA manager internally uses a rb-tree to manage the
48 * &drm_gpuva mappings within a GPU's virtual address space.
50 * The &drm_gpuva_manager contains a special &drm_gpuva representing the
51 * portion of VA space reserved by the kernel. This node is initialized together
52 * with the GPU VA manager instance and removed when the GPU VA manager is
55 * In a typical application drivers would embed struct drm_gpuva_manager and
56 * struct drm_gpuva within their own driver specific structures, there won't be
57 * any memory allocations of its own nor memory allocations of &drm_gpuva
60 * The data structures needed to store &drm_gpuvas within the &drm_gpuva_manager
61 * are contained within struct drm_gpuva already. Hence, for inserting
62 * &drm_gpuva entries from within dma-fence signalling critical sections it is
63 * enough to pre-allocate the &drm_gpuva structures.
67 * DOC: Split and Merge
69 * Besides its capability to manage and represent a GPU VA space, the
70 * &drm_gpuva_manager also provides functions to let the &drm_gpuva_manager
71 * calculate a sequence of operations to satisfy a given map or unmap request.
73 * Therefore the DRM GPU VA manager provides an algorithm implementing splitting
74 * and merging of existent GPU VA mappings with the ones that are requested to
75 * be mapped or unmapped. This feature is required by the Vulkan API to
76 * implement Vulkan 'Sparse Memory Bindings' - drivers UAPIs often refer to this
79 * Drivers can call drm_gpuva_sm_map() to receive a sequence of callbacks
80 * containing map, unmap and remap operations for a given newly requested
81 * mapping. The sequence of callbacks represents the set of operations to
82 * execute in order to integrate the new mapping cleanly into the current state
83 * of the GPU VA space.
85 * Depending on how the new GPU VA mapping intersects with the existent mappings
86 * of the GPU VA space the &drm_gpuva_fn_ops callbacks contain an arbitrary
87 * amount of unmap operations, a maximum of two remap operations and a single
88 * map operation. The caller might receive no callback at all if no operation is
89 * required, e.g. if the requested mapping already exists in the exact same way.
91 * The single map operation represents the original map operation requested by
94 * &drm_gpuva_op_unmap contains a 'keep' field, which indicates whether the
95 * &drm_gpuva to unmap is physically contiguous with the original mapping
96 * request. Optionally, if 'keep' is set, drivers may keep the actual page table
97 * entries for this &drm_gpuva, adding the missing page table entries only and
98 * update the &drm_gpuva_manager's view of things accordingly.
100 * Drivers may do the same optimization, namely delta page table updates, also
101 * for remap operations. This is possible since &drm_gpuva_op_remap consists of
102 * one unmap operation and one or two map operations, such that drivers can
103 * derive the page table update delta accordingly.
105 * Note that there can't be more than two existent mappings to split up, one at
106 * the beginning and one at the end of the new mapping, hence there is a
107 * maximum of two remap operations.
109 * Analogous to drm_gpuva_sm_map() drm_gpuva_sm_unmap() uses &drm_gpuva_fn_ops
110 * to call back into the driver in order to unmap a range of GPU VA space. The
111 * logic behind this function is way simpler though: For all existent mappings
112 * enclosed by the given range unmap operations are created. For mappings which
113 * are only partically located within the given range, remap operations are
114 * created such that those mappings are split up and re-mapped partically.
116 * As an alternative to drm_gpuva_sm_map() and drm_gpuva_sm_unmap(),
117 * drm_gpuva_sm_map_ops_create() and drm_gpuva_sm_unmap_ops_create() can be used
118 * to directly obtain an instance of struct drm_gpuva_ops containing a list of
119 * &drm_gpuva_op, which can be iterated with drm_gpuva_for_each_op(). This list
120 * contains the &drm_gpuva_ops analogous to the callbacks one would receive when
121 * calling drm_gpuva_sm_map() or drm_gpuva_sm_unmap(). While this way requires
122 * more memory (to allocate the &drm_gpuva_ops), it provides drivers a way to
123 * iterate the &drm_gpuva_op multiple times, e.g. once in a context where memory
124 * allocations are possible (e.g. to allocate GPU page tables) and once in the
125 * dma-fence signalling critical path.
127 * To update the &drm_gpuva_manager's view of the GPU VA space
128 * drm_gpuva_insert() and drm_gpuva_remove() may be used. These functions can
129 * safely be used from &drm_gpuva_fn_ops callbacks originating from
130 * drm_gpuva_sm_map() or drm_gpuva_sm_unmap(). However, it might be more
131 * convenient to use the provided helper functions drm_gpuva_map(),
132 * drm_gpuva_remap() and drm_gpuva_unmap() instead.
134 * The following diagram depicts the basic relationships of existent GPU VA
135 * mappings, a newly requested mapping and the resulting mappings as implemented
136 * by drm_gpuva_sm_map() - it doesn't cover any arbitrary combinations of these.
138 * 1) Requested mapping is identical. Replace it, but indicate the backing PTEs
144 * old: |-----------| (bo_offset=n)
147 * req: |-----------| (bo_offset=n)
150 * new: |-----------| (bo_offset=n)
153 * 2) Requested mapping is identical, except for the BO offset, hence replace
159 * old: |-----------| (bo_offset=n)
162 * req: |-----------| (bo_offset=m)
165 * new: |-----------| (bo_offset=m)
168 * 3) Requested mapping is identical, except for the backing BO, hence replace
174 * old: |-----------| (bo_offset=n)
177 * req: |-----------| (bo_offset=n)
180 * new: |-----------| (bo_offset=n)
183 * 4) Existent mapping is a left aligned subset of the requested one, hence
184 * replace the existent one.
189 * old: |-----| (bo_offset=n)
192 * req: |-----------| (bo_offset=n)
195 * new: |-----------| (bo_offset=n)
198 * We expect to see the same result for a request with a different BO
199 * and/or non-contiguous BO offset.
202 * 5) Requested mapping's range is a left aligned subset of the existent one,
203 * but backed by a different BO. Hence, map the requested mapping and split
204 * the existent one adjusting its BO offset.
209 * old: |-----------| (bo_offset=n)
212 * req: |-----| (bo_offset=n)
215 * new: |-----|-----| (b.bo_offset=n, a.bo_offset=n+1)
218 * We expect to see the same result for a request with a different BO
219 * and/or non-contiguous BO offset.
222 * 6) Existent mapping is a superset of the requested mapping. Split it up, but
223 * indicate that the backing PTEs could be kept.
228 * old: |-----------| (bo_offset=n)
231 * req: |-----| (bo_offset=n)
234 * new: |-----|-----| (a.bo_offset=n, a'.bo_offset=n+1)
237 * 7) Requested mapping's range is a right aligned subset of the existent one,
238 * but backed by a different BO. Hence, map the requested mapping and split
239 * the existent one, without adjusting the BO offset.
244 * old: |-----------| (bo_offset=n)
247 * req: |-----| (bo_offset=m)
250 * new: |-----|-----| (a.bo_offset=n,b.bo_offset=m)
253 * 8) Existent mapping is a superset of the requested mapping. Split it up, but
254 * indicate that the backing PTEs could be kept.
259 * old: |-----------| (bo_offset=n)
262 * req: |-----| (bo_offset=n+1)
265 * new: |-----|-----| (a'.bo_offset=n, a.bo_offset=n+1)
268 * 9) Existent mapping is overlapped at the end by the requested mapping backed
269 * by a different BO. Hence, map the requested mapping and split up the
270 * existent one, without adjusting the BO offset.
275 * old: |-----------| (bo_offset=n)
278 * req: |-----------| (bo_offset=m)
281 * new: |-----|-----------| (a.bo_offset=n,b.bo_offset=m)
284 * 10) Existent mapping is overlapped by the requested mapping, both having the
285 * same backing BO with a contiguous offset. Indicate the backing PTEs of
286 * the old mapping could be kept.
291 * old: |-----------| (bo_offset=n)
294 * req: |-----------| (bo_offset=n+1)
297 * new: |-----|-----------| (a'.bo_offset=n, a.bo_offset=n+1)
300 * 11) Requested mapping's range is a centered subset of the existent one
301 * having a different backing BO. Hence, map the requested mapping and split
302 * up the existent one in two mappings, adjusting the BO offset of the right
308 * old: |-----------------| (bo_offset=n)
311 * req: |-----| (bo_offset=m)
314 * new: |-----|-----|-----| (a.bo_offset=n,b.bo_offset=m,a'.bo_offset=n+2)
317 * 12) Requested mapping is a contiguous subset of the existent one. Split it
318 * up, but indicate that the backing PTEs could be kept.
323 * old: |-----------------| (bo_offset=n)
326 * req: |-----| (bo_offset=n+1)
329 * old: |-----|-----|-----| (a'.bo_offset=n, a.bo_offset=n+1, a''.bo_offset=n+2)
332 * 13) Existent mapping is a right aligned subset of the requested one, hence
333 * replace the existent one.
338 * old: |-----| (bo_offset=n+1)
341 * req: |-----------| (bo_offset=n)
344 * new: |-----------| (bo_offset=n)
347 * We expect to see the same result for a request with a different bo
348 * and/or non-contiguous bo_offset.
351 * 14) Existent mapping is a centered subset of the requested one, hence
352 * replace the existent one.
357 * old: |-----| (bo_offset=n+1)
360 * req: |----------------| (bo_offset=n)
363 * new: |----------------| (bo_offset=n)
366 * We expect to see the same result for a request with a different bo
367 * and/or non-contiguous bo_offset.
370 * 15) Existent mappings is overlapped at the beginning by the requested mapping
371 * backed by a different BO. Hence, map the requested mapping and split up
372 * the existent one, adjusting its BO offset accordingly.
377 * old: |-----------| (bo_offset=n)
380 * req: |-----------| (bo_offset=m)
383 * new: |-----------|-----| (b.bo_offset=m,a.bo_offset=n+2)
389 * Generally, the GPU VA manager does not take care of locking itself, it is
390 * the drivers responsibility to take care about locking. Drivers might want to
391 * protect the following operations: inserting, removing and iterating
392 * &drm_gpuva objects as well as generating all kinds of operations, such as
393 * split / merge or prefetch.
395 * The GPU VA manager also does not take care of the locking of the backing
396 * &drm_gem_object buffers GPU VA lists by itself; drivers are responsible to
397 * enforce mutual exclusion using either the GEMs dma_resv lock or alternatively
398 * a driver specific external lock. For the latter see also
399 * drm_gem_gpuva_set_lock().
401 * However, the GPU VA manager contains lockdep checks to ensure callers of its
402 * API hold the corresponding lock whenever the &drm_gem_objects GPU VA list is
403 * accessed by functions such as drm_gpuva_link() or drm_gpuva_unlink().
409 * This section gives two examples on how to let the DRM GPUVA Manager generate
410 * &drm_gpuva_op in order to satisfy a given map or unmap request and how to
413 * The below code is strictly limited to illustrate the generic usage pattern.
414 * To maintain simplicitly, it doesn't make use of any abstractions for common
415 * code, different (asyncronous) stages with fence signalling critical paths,
416 * any other helpers or error handling in terms of freeing memory and dropping
417 * previously taken locks.
419 * 1) Obtain a list of &drm_gpuva_op to create a new mapping::
421 * // Allocates a new &drm_gpuva.
422 * struct drm_gpuva * driver_gpuva_alloc(void);
424 * // Typically drivers would embedd the &drm_gpuva_manager and &drm_gpuva
425 * // structure in individual driver structures and lock the dma-resv with
426 * // drm_exec or similar helpers.
427 * int driver_mapping_create(struct drm_gpuva_manager *mgr,
428 * u64 addr, u64 range,
429 * struct drm_gem_object *obj, u64 offset)
431 * struct drm_gpuva_ops *ops;
432 * struct drm_gpuva_op *op
434 * driver_lock_va_space();
435 * ops = drm_gpuva_sm_map_ops_create(mgr, addr, range,
438 * return PTR_ERR(ops);
440 * drm_gpuva_for_each_op(op, ops) {
441 * struct drm_gpuva *va;
444 * case DRM_GPUVA_OP_MAP:
445 * va = driver_gpuva_alloc();
447 * ; // unwind previous VA space updates,
448 * // free memory and unlock
451 * drm_gpuva_map(mgr, va, &op->map);
452 * drm_gpuva_link(va);
455 * case DRM_GPUVA_OP_REMAP: {
456 * struct drm_gpuva *prev = NULL, *next = NULL;
458 * va = op->remap.unmap->va;
460 * if (op->remap.prev) {
461 * prev = driver_gpuva_alloc();
463 * ; // unwind previous VA space
464 * // updates, free memory and
468 * if (op->remap.next) {
469 * next = driver_gpuva_alloc();
471 * ; // unwind previous VA space
472 * // updates, free memory and
477 * drm_gpuva_remap(prev, next, &op->remap);
479 * drm_gpuva_unlink(va);
481 * drm_gpuva_link(prev);
483 * drm_gpuva_link(next);
487 * case DRM_GPUVA_OP_UNMAP:
488 * va = op->unmap->va;
491 * drm_gpuva_unlink(va);
492 * drm_gpuva_unmap(&op->unmap);
499 * driver_unlock_va_space();
504 * 2) Receive a callback for each &drm_gpuva_op to create a new mapping::
506 * struct driver_context {
507 * struct drm_gpuva_manager *mgr;
508 * struct drm_gpuva *new_va;
509 * struct drm_gpuva *prev_va;
510 * struct drm_gpuva *next_va;
513 * // ops to pass to drm_gpuva_manager_init()
514 * static const struct drm_gpuva_fn_ops driver_gpuva_ops = {
515 * .sm_step_map = driver_gpuva_map,
516 * .sm_step_remap = driver_gpuva_remap,
517 * .sm_step_unmap = driver_gpuva_unmap,
520 * // Typically drivers would embedd the &drm_gpuva_manager and &drm_gpuva
521 * // structure in individual driver structures and lock the dma-resv with
522 * // drm_exec or similar helpers.
523 * int driver_mapping_create(struct drm_gpuva_manager *mgr,
524 * u64 addr, u64 range,
525 * struct drm_gem_object *obj, u64 offset)
527 * struct driver_context ctx;
528 * struct drm_gpuva_ops *ops;
529 * struct drm_gpuva_op *op;
534 * ctx.new_va = kzalloc(sizeof(*ctx.new_va), GFP_KERNEL);
535 * ctx.prev_va = kzalloc(sizeof(*ctx.prev_va), GFP_KERNEL);
536 * ctx.next_va = kzalloc(sizeof(*ctx.next_va), GFP_KERNEL);
537 * if (!ctx.new_va || !ctx.prev_va || !ctx.next_va) {
542 * driver_lock_va_space();
543 * ret = drm_gpuva_sm_map(mgr, &ctx, addr, range, obj, offset);
544 * driver_unlock_va_space();
548 * kfree(ctx.prev_va);
549 * kfree(ctx.next_va);
553 * int driver_gpuva_map(struct drm_gpuva_op *op, void *__ctx)
555 * struct driver_context *ctx = __ctx;
557 * drm_gpuva_map(ctx->mgr, ctx->new_va, &op->map);
559 * drm_gpuva_link(ctx->new_va);
561 * // prevent the new GPUVA from being freed in
562 * // driver_mapping_create()
563 * ctx->new_va = NULL;
568 * int driver_gpuva_remap(struct drm_gpuva_op *op, void *__ctx)
570 * struct driver_context *ctx = __ctx;
572 * drm_gpuva_remap(ctx->prev_va, ctx->next_va, &op->remap);
574 * drm_gpuva_unlink(op->remap.unmap->va);
575 * kfree(op->remap.unmap->va);
577 * if (op->remap.prev) {
578 * drm_gpuva_link(ctx->prev_va);
579 * ctx->prev_va = NULL;
582 * if (op->remap.next) {
583 * drm_gpuva_link(ctx->next_va);
584 * ctx->next_va = NULL;
590 * int driver_gpuva_unmap(struct drm_gpuva_op *op, void *__ctx)
592 * drm_gpuva_unlink(op->unmap.va);
593 * drm_gpuva_unmap(&op->unmap);
594 * kfree(op->unmap.va);
600 #define to_drm_gpuva(__node) container_of((__node), struct drm_gpuva, rb.node)
602 #define GPUVA_START(node) ((node)->va.addr)
603 #define GPUVA_LAST(node) ((node)->va.addr + (node)->va.range - 1)
605 /* We do not actually use drm_gpuva_it_next(), tell the compiler to not complain
608 INTERVAL_TREE_DEFINE(struct drm_gpuva, rb.node, u64, rb.__subtree_last,
609 GPUVA_START, GPUVA_LAST, static __maybe_unused,
612 static int __drm_gpuva_insert(struct drm_gpuva_manager *mgr,
613 struct drm_gpuva *va);
614 static void __drm_gpuva_remove(struct drm_gpuva *va);
617 drm_gpuva_check_overflow(u64 addr, u64 range)
621 return WARN(check_add_overflow(addr, range, &end),
622 "GPUVA address limited to %zu bytes.\n", sizeof(end));
626 drm_gpuva_in_mm_range(struct drm_gpuva_manager *mgr, u64 addr, u64 range)
628 u64 end = addr + range;
629 u64 mm_start = mgr->mm_start;
630 u64 mm_end = mm_start + mgr->mm_range;
632 return addr >= mm_start && end <= mm_end;
636 drm_gpuva_in_kernel_node(struct drm_gpuva_manager *mgr, u64 addr, u64 range)
638 u64 end = addr + range;
639 u64 kstart = mgr->kernel_alloc_node.va.addr;
640 u64 krange = mgr->kernel_alloc_node.va.range;
641 u64 kend = kstart + krange;
643 return krange && addr < kend && kstart < end;
647 drm_gpuva_range_valid(struct drm_gpuva_manager *mgr,
650 return !drm_gpuva_check_overflow(addr, range) &&
651 drm_gpuva_in_mm_range(mgr, addr, range) &&
652 !drm_gpuva_in_kernel_node(mgr, addr, range);
656 * drm_gpuva_manager_init() - initialize a &drm_gpuva_manager
657 * @mgr: pointer to the &drm_gpuva_manager to initialize
658 * @name: the name of the GPU VA space
659 * @start_offset: the start offset of the GPU VA space
660 * @range: the size of the GPU VA space
661 * @reserve_offset: the start of the kernel reserved GPU VA area
662 * @reserve_range: the size of the kernel reserved GPU VA area
663 * @ops: &drm_gpuva_fn_ops called on &drm_gpuva_sm_map / &drm_gpuva_sm_unmap
665 * The &drm_gpuva_manager must be initialized with this function before use.
667 * Note that @mgr must be cleared to 0 before calling this function. The given
668 * &name is expected to be managed by the surrounding driver structures.
671 drm_gpuva_manager_init(struct drm_gpuva_manager *mgr,
673 u64 start_offset, u64 range,
674 u64 reserve_offset, u64 reserve_range,
675 const struct drm_gpuva_fn_ops *ops)
677 mgr->rb.tree = RB_ROOT_CACHED;
678 INIT_LIST_HEAD(&mgr->rb.list);
680 drm_gpuva_check_overflow(start_offset, range);
681 mgr->mm_start = start_offset;
682 mgr->mm_range = range;
684 mgr->name = name ? name : "unknown";
687 memset(&mgr->kernel_alloc_node, 0, sizeof(struct drm_gpuva));
690 mgr->kernel_alloc_node.va.addr = reserve_offset;
691 mgr->kernel_alloc_node.va.range = reserve_range;
693 if (likely(!drm_gpuva_check_overflow(reserve_offset,
695 __drm_gpuva_insert(mgr, &mgr->kernel_alloc_node);
698 EXPORT_SYMBOL_GPL(drm_gpuva_manager_init);
701 * drm_gpuva_manager_destroy() - cleanup a &drm_gpuva_manager
702 * @mgr: pointer to the &drm_gpuva_manager to clean up
704 * Note that it is a bug to call this function on a manager that still
705 * holds GPU VA mappings.
708 drm_gpuva_manager_destroy(struct drm_gpuva_manager *mgr)
712 if (mgr->kernel_alloc_node.va.range)
713 __drm_gpuva_remove(&mgr->kernel_alloc_node);
715 WARN(!RB_EMPTY_ROOT(&mgr->rb.tree.rb_root),
716 "GPUVA tree is not empty, potentially leaking memory.");
718 EXPORT_SYMBOL_GPL(drm_gpuva_manager_destroy);
721 __drm_gpuva_insert(struct drm_gpuva_manager *mgr,
722 struct drm_gpuva *va)
724 struct rb_node *node;
725 struct list_head *head;
727 if (drm_gpuva_it_iter_first(&mgr->rb.tree,
734 drm_gpuva_it_insert(va, &mgr->rb.tree);
736 node = rb_prev(&va->rb.node);
738 head = &(to_drm_gpuva(node))->rb.entry;
740 head = &mgr->rb.list;
742 list_add(&va->rb.entry, head);
748 * drm_gpuva_insert() - insert a &drm_gpuva
749 * @mgr: the &drm_gpuva_manager to insert the &drm_gpuva in
750 * @va: the &drm_gpuva to insert
752 * Insert a &drm_gpuva with a given address and range into a
753 * &drm_gpuva_manager.
755 * It is safe to use this function using the safe versions of iterating the GPU
756 * VA space, such as drm_gpuva_for_each_va_safe() and
757 * drm_gpuva_for_each_va_range_safe().
759 * Returns: 0 on success, negative error code on failure.
762 drm_gpuva_insert(struct drm_gpuva_manager *mgr,
763 struct drm_gpuva *va)
765 u64 addr = va->va.addr;
766 u64 range = va->va.range;
768 if (unlikely(!drm_gpuva_range_valid(mgr, addr, range)))
771 return __drm_gpuva_insert(mgr, va);
773 EXPORT_SYMBOL_GPL(drm_gpuva_insert);
776 __drm_gpuva_remove(struct drm_gpuva *va)
778 drm_gpuva_it_remove(va, &va->mgr->rb.tree);
779 list_del_init(&va->rb.entry);
783 * drm_gpuva_remove() - remove a &drm_gpuva
784 * @va: the &drm_gpuva to remove
786 * This removes the given &va from the underlaying tree.
788 * It is safe to use this function using the safe versions of iterating the GPU
789 * VA space, such as drm_gpuva_for_each_va_safe() and
790 * drm_gpuva_for_each_va_range_safe().
793 drm_gpuva_remove(struct drm_gpuva *va)
795 struct drm_gpuva_manager *mgr = va->mgr;
797 if (unlikely(va == &mgr->kernel_alloc_node)) {
798 WARN(1, "Can't destroy kernel reserved node.\n");
802 __drm_gpuva_remove(va);
804 EXPORT_SYMBOL_GPL(drm_gpuva_remove);
807 * drm_gpuva_link() - link a &drm_gpuva
808 * @va: the &drm_gpuva to link
810 * This adds the given &va to the GPU VA list of the &drm_gem_object it is
813 * This function expects the caller to protect the GEM's GPUVA list against
814 * concurrent access using the GEMs dma_resv lock.
817 drm_gpuva_link(struct drm_gpuva *va)
819 struct drm_gem_object *obj = va->gem.obj;
824 drm_gem_gpuva_assert_lock_held(obj);
826 list_add_tail(&va->gem.entry, &obj->gpuva.list);
828 EXPORT_SYMBOL_GPL(drm_gpuva_link);
831 * drm_gpuva_unlink() - unlink a &drm_gpuva
832 * @va: the &drm_gpuva to unlink
834 * This removes the given &va from the GPU VA list of the &drm_gem_object it is
837 * This function expects the caller to protect the GEM's GPUVA list against
838 * concurrent access using the GEMs dma_resv lock.
841 drm_gpuva_unlink(struct drm_gpuva *va)
843 struct drm_gem_object *obj = va->gem.obj;
848 drm_gem_gpuva_assert_lock_held(obj);
850 list_del_init(&va->gem.entry);
852 EXPORT_SYMBOL_GPL(drm_gpuva_unlink);
855 * drm_gpuva_find_first() - find the first &drm_gpuva in the given range
856 * @mgr: the &drm_gpuva_manager to search in
857 * @addr: the &drm_gpuvas address
858 * @range: the &drm_gpuvas range
860 * Returns: the first &drm_gpuva within the given range
863 drm_gpuva_find_first(struct drm_gpuva_manager *mgr,
866 u64 last = addr + range - 1;
868 return drm_gpuva_it_iter_first(&mgr->rb.tree, addr, last);
870 EXPORT_SYMBOL_GPL(drm_gpuva_find_first);
873 * drm_gpuva_find() - find a &drm_gpuva
874 * @mgr: the &drm_gpuva_manager to search in
875 * @addr: the &drm_gpuvas address
876 * @range: the &drm_gpuvas range
878 * Returns: the &drm_gpuva at a given &addr and with a given &range
881 drm_gpuva_find(struct drm_gpuva_manager *mgr,
884 struct drm_gpuva *va;
886 va = drm_gpuva_find_first(mgr, addr, range);
890 if (va->va.addr != addr ||
891 va->va.range != range)
899 EXPORT_SYMBOL_GPL(drm_gpuva_find);
902 * drm_gpuva_find_prev() - find the &drm_gpuva before the given address
903 * @mgr: the &drm_gpuva_manager to search in
904 * @start: the given GPU VA's start address
906 * Find the adjacent &drm_gpuva before the GPU VA with given &start address.
908 * Note that if there is any free space between the GPU VA mappings no mapping
911 * Returns: a pointer to the found &drm_gpuva or NULL if none was found
914 drm_gpuva_find_prev(struct drm_gpuva_manager *mgr, u64 start)
916 if (!drm_gpuva_range_valid(mgr, start - 1, 1))
919 return drm_gpuva_it_iter_first(&mgr->rb.tree, start - 1, start);
921 EXPORT_SYMBOL_GPL(drm_gpuva_find_prev);
924 * drm_gpuva_find_next() - find the &drm_gpuva after the given address
925 * @mgr: the &drm_gpuva_manager to search in
926 * @end: the given GPU VA's end address
928 * Find the adjacent &drm_gpuva after the GPU VA with given &end address.
930 * Note that if there is any free space between the GPU VA mappings no mapping
933 * Returns: a pointer to the found &drm_gpuva or NULL if none was found
936 drm_gpuva_find_next(struct drm_gpuva_manager *mgr, u64 end)
938 if (!drm_gpuva_range_valid(mgr, end, 1))
941 return drm_gpuva_it_iter_first(&mgr->rb.tree, end, end + 1);
943 EXPORT_SYMBOL_GPL(drm_gpuva_find_next);
946 * drm_gpuva_interval_empty() - indicate whether a given interval of the VA space
948 * @mgr: the &drm_gpuva_manager to check the range for
949 * @addr: the start address of the range
950 * @range: the range of the interval
952 * Returns: true if the interval is empty, false otherwise
955 drm_gpuva_interval_empty(struct drm_gpuva_manager *mgr, u64 addr, u64 range)
957 return !drm_gpuva_find_first(mgr, addr, range);
959 EXPORT_SYMBOL_GPL(drm_gpuva_interval_empty);
962 * drm_gpuva_map() - helper to insert a &drm_gpuva according to a
964 * @mgr: the &drm_gpuva_manager
965 * @va: the &drm_gpuva to insert
966 * @op: the &drm_gpuva_op_map to initialize @va with
968 * Initializes the @va from the @op and inserts it into the given @mgr.
971 drm_gpuva_map(struct drm_gpuva_manager *mgr,
972 struct drm_gpuva *va,
973 struct drm_gpuva_op_map *op)
975 drm_gpuva_init_from_op(va, op);
976 drm_gpuva_insert(mgr, va);
978 EXPORT_SYMBOL_GPL(drm_gpuva_map);
981 * drm_gpuva_remap() - helper to remap a &drm_gpuva according to a
982 * &drm_gpuva_op_remap
983 * @prev: the &drm_gpuva to remap when keeping the start of a mapping
984 * @next: the &drm_gpuva to remap when keeping the end of a mapping
985 * @op: the &drm_gpuva_op_remap to initialize @prev and @next with
987 * Removes the currently mapped &drm_gpuva and remaps it using @prev and/or
991 drm_gpuva_remap(struct drm_gpuva *prev,
992 struct drm_gpuva *next,
993 struct drm_gpuva_op_remap *op)
995 struct drm_gpuva *curr = op->unmap->va;
996 struct drm_gpuva_manager *mgr = curr->mgr;
998 drm_gpuva_remove(curr);
1001 drm_gpuva_init_from_op(prev, op->prev);
1002 drm_gpuva_insert(mgr, prev);
1006 drm_gpuva_init_from_op(next, op->next);
1007 drm_gpuva_insert(mgr, next);
1010 EXPORT_SYMBOL_GPL(drm_gpuva_remap);
1013 * drm_gpuva_unmap() - helper to remove a &drm_gpuva according to a
1014 * &drm_gpuva_op_unmap
1015 * @op: the &drm_gpuva_op_unmap specifying the &drm_gpuva to remove
1017 * Removes the &drm_gpuva associated with the &drm_gpuva_op_unmap.
1020 drm_gpuva_unmap(struct drm_gpuva_op_unmap *op)
1022 drm_gpuva_remove(op->va);
1024 EXPORT_SYMBOL_GPL(drm_gpuva_unmap);
1027 op_map_cb(const struct drm_gpuva_fn_ops *fn, void *priv,
1028 u64 addr, u64 range,
1029 struct drm_gem_object *obj, u64 offset)
1031 struct drm_gpuva_op op = {};
1033 op.op = DRM_GPUVA_OP_MAP;
1034 op.map.va.addr = addr;
1035 op.map.va.range = range;
1036 op.map.gem.obj = obj;
1037 op.map.gem.offset = offset;
1039 return fn->sm_step_map(&op, priv);
1043 op_remap_cb(const struct drm_gpuva_fn_ops *fn, void *priv,
1044 struct drm_gpuva_op_map *prev,
1045 struct drm_gpuva_op_map *next,
1046 struct drm_gpuva_op_unmap *unmap)
1048 struct drm_gpuva_op op = {};
1049 struct drm_gpuva_op_remap *r;
1051 op.op = DRM_GPUVA_OP_REMAP;
1057 return fn->sm_step_remap(&op, priv);
1061 op_unmap_cb(const struct drm_gpuva_fn_ops *fn, void *priv,
1062 struct drm_gpuva *va, bool merge)
1064 struct drm_gpuva_op op = {};
1066 op.op = DRM_GPUVA_OP_UNMAP;
1068 op.unmap.keep = merge;
1070 return fn->sm_step_unmap(&op, priv);
1074 __drm_gpuva_sm_map(struct drm_gpuva_manager *mgr,
1075 const struct drm_gpuva_fn_ops *ops, void *priv,
1076 u64 req_addr, u64 req_range,
1077 struct drm_gem_object *req_obj, u64 req_offset)
1079 struct drm_gpuva *va, *next;
1080 u64 req_end = req_addr + req_range;
1083 if (unlikely(!drm_gpuva_range_valid(mgr, req_addr, req_range)))
1086 drm_gpuva_for_each_va_range_safe(va, next, mgr, req_addr, req_end) {
1087 struct drm_gem_object *obj = va->gem.obj;
1088 u64 offset = va->gem.offset;
1089 u64 addr = va->va.addr;
1090 u64 range = va->va.range;
1091 u64 end = addr + range;
1092 bool merge = !!va->gem.obj;
1094 if (addr == req_addr) {
1095 merge &= obj == req_obj &&
1096 offset == req_offset;
1098 if (end == req_end) {
1099 ret = op_unmap_cb(ops, priv, va, merge);
1105 if (end < req_end) {
1106 ret = op_unmap_cb(ops, priv, va, merge);
1112 if (end > req_end) {
1113 struct drm_gpuva_op_map n = {
1115 .va.range = range - req_range,
1117 .gem.offset = offset + req_range,
1119 struct drm_gpuva_op_unmap u = {
1124 ret = op_remap_cb(ops, priv, NULL, &n, &u);
1129 } else if (addr < req_addr) {
1130 u64 ls_range = req_addr - addr;
1131 struct drm_gpuva_op_map p = {
1133 .va.range = ls_range,
1135 .gem.offset = offset,
1137 struct drm_gpuva_op_unmap u = { .va = va };
1139 merge &= obj == req_obj &&
1140 offset + ls_range == req_offset;
1143 if (end == req_end) {
1144 ret = op_remap_cb(ops, priv, &p, NULL, &u);
1150 if (end < req_end) {
1151 ret = op_remap_cb(ops, priv, &p, NULL, &u);
1157 if (end > req_end) {
1158 struct drm_gpuva_op_map n = {
1160 .va.range = end - req_end,
1162 .gem.offset = offset + ls_range +
1166 ret = op_remap_cb(ops, priv, &p, &n, &u);
1171 } else if (addr > req_addr) {
1172 merge &= obj == req_obj &&
1173 offset == req_offset +
1176 if (end == req_end) {
1177 ret = op_unmap_cb(ops, priv, va, merge);
1183 if (end < req_end) {
1184 ret = op_unmap_cb(ops, priv, va, merge);
1190 if (end > req_end) {
1191 struct drm_gpuva_op_map n = {
1193 .va.range = end - req_end,
1195 .gem.offset = offset + req_end - addr,
1197 struct drm_gpuva_op_unmap u = {
1202 ret = op_remap_cb(ops, priv, NULL, &n, &u);
1210 return op_map_cb(ops, priv,
1211 req_addr, req_range,
1212 req_obj, req_offset);
1216 __drm_gpuva_sm_unmap(struct drm_gpuva_manager *mgr,
1217 const struct drm_gpuva_fn_ops *ops, void *priv,
1218 u64 req_addr, u64 req_range)
1220 struct drm_gpuva *va, *next;
1221 u64 req_end = req_addr + req_range;
1224 if (unlikely(!drm_gpuva_range_valid(mgr, req_addr, req_range)))
1227 drm_gpuva_for_each_va_range_safe(va, next, mgr, req_addr, req_end) {
1228 struct drm_gpuva_op_map prev = {}, next = {};
1229 bool prev_split = false, next_split = false;
1230 struct drm_gem_object *obj = va->gem.obj;
1231 u64 offset = va->gem.offset;
1232 u64 addr = va->va.addr;
1233 u64 range = va->va.range;
1234 u64 end = addr + range;
1236 if (addr < req_addr) {
1237 prev.va.addr = addr;
1238 prev.va.range = req_addr - addr;
1240 prev.gem.offset = offset;
1245 if (end > req_end) {
1246 next.va.addr = req_end;
1247 next.va.range = end - req_end;
1249 next.gem.offset = offset + (req_end - addr);
1254 if (prev_split || next_split) {
1255 struct drm_gpuva_op_unmap unmap = { .va = va };
1257 ret = op_remap_cb(ops, priv,
1258 prev_split ? &prev : NULL,
1259 next_split ? &next : NULL,
1264 ret = op_unmap_cb(ops, priv, va, false);
1274 * drm_gpuva_sm_map() - creates the &drm_gpuva_op split/merge steps
1275 * @mgr: the &drm_gpuva_manager representing the GPU VA space
1276 * @req_addr: the start address of the new mapping
1277 * @req_range: the range of the new mapping
1278 * @req_obj: the &drm_gem_object to map
1279 * @req_offset: the offset within the &drm_gem_object
1280 * @priv: pointer to a driver private data structure
1282 * This function iterates the given range of the GPU VA space. It utilizes the
1283 * &drm_gpuva_fn_ops to call back into the driver providing the split and merge
1286 * Drivers may use these callbacks to update the GPU VA space right away within
1287 * the callback. In case the driver decides to copy and store the operations for
1288 * later processing neither this function nor &drm_gpuva_sm_unmap is allowed to
1289 * be called before the &drm_gpuva_manager's view of the GPU VA space was
1290 * updated with the previous set of operations. To update the
1291 * &drm_gpuva_manager's view of the GPU VA space drm_gpuva_insert(),
1292 * drm_gpuva_destroy_locked() and/or drm_gpuva_destroy_unlocked() should be
1295 * A sequence of callbacks can contain map, unmap and remap operations, but
1296 * the sequence of callbacks might also be empty if no operation is required,
1297 * e.g. if the requested mapping already exists in the exact same way.
1299 * There can be an arbitrary amount of unmap operations, a maximum of two remap
1300 * operations and a single map operation. The latter one represents the original
1301 * map operation requested by the caller.
1303 * Returns: 0 on success or a negative error code
1306 drm_gpuva_sm_map(struct drm_gpuva_manager *mgr, void *priv,
1307 u64 req_addr, u64 req_range,
1308 struct drm_gem_object *req_obj, u64 req_offset)
1310 const struct drm_gpuva_fn_ops *ops = mgr->ops;
1312 if (unlikely(!(ops && ops->sm_step_map &&
1313 ops->sm_step_remap &&
1314 ops->sm_step_unmap)))
1317 return __drm_gpuva_sm_map(mgr, ops, priv,
1318 req_addr, req_range,
1319 req_obj, req_offset);
1321 EXPORT_SYMBOL_GPL(drm_gpuva_sm_map);
1324 * drm_gpuva_sm_unmap() - creates the &drm_gpuva_ops to split on unmap
1325 * @mgr: the &drm_gpuva_manager representing the GPU VA space
1326 * @priv: pointer to a driver private data structure
1327 * @req_addr: the start address of the range to unmap
1328 * @req_range: the range of the mappings to unmap
1330 * This function iterates the given range of the GPU VA space. It utilizes the
1331 * &drm_gpuva_fn_ops to call back into the driver providing the operations to
1332 * unmap and, if required, split existent mappings.
1334 * Drivers may use these callbacks to update the GPU VA space right away within
1335 * the callback. In case the driver decides to copy and store the operations for
1336 * later processing neither this function nor &drm_gpuva_sm_map is allowed to be
1337 * called before the &drm_gpuva_manager's view of the GPU VA space was updated
1338 * with the previous set of operations. To update the &drm_gpuva_manager's view
1339 * of the GPU VA space drm_gpuva_insert(), drm_gpuva_destroy_locked() and/or
1340 * drm_gpuva_destroy_unlocked() should be used.
1342 * A sequence of callbacks can contain unmap and remap operations, depending on
1343 * whether there are actual overlapping mappings to split.
1345 * There can be an arbitrary amount of unmap operations and a maximum of two
1348 * Returns: 0 on success or a negative error code
1351 drm_gpuva_sm_unmap(struct drm_gpuva_manager *mgr, void *priv,
1352 u64 req_addr, u64 req_range)
1354 const struct drm_gpuva_fn_ops *ops = mgr->ops;
1356 if (unlikely(!(ops && ops->sm_step_remap &&
1357 ops->sm_step_unmap)))
1360 return __drm_gpuva_sm_unmap(mgr, ops, priv,
1361 req_addr, req_range);
1363 EXPORT_SYMBOL_GPL(drm_gpuva_sm_unmap);
1365 static struct drm_gpuva_op *
1366 gpuva_op_alloc(struct drm_gpuva_manager *mgr)
1368 const struct drm_gpuva_fn_ops *fn = mgr->ops;
1369 struct drm_gpuva_op *op;
1371 if (fn && fn->op_alloc)
1372 op = fn->op_alloc();
1374 op = kzalloc(sizeof(*op), GFP_KERNEL);
1383 gpuva_op_free(struct drm_gpuva_manager *mgr,
1384 struct drm_gpuva_op *op)
1386 const struct drm_gpuva_fn_ops *fn = mgr->ops;
1388 if (fn && fn->op_free)
1395 drm_gpuva_sm_step(struct drm_gpuva_op *__op,
1399 struct drm_gpuva_manager *mgr;
1400 struct drm_gpuva_ops *ops;
1402 struct drm_gpuva_manager *mgr = args->mgr;
1403 struct drm_gpuva_ops *ops = args->ops;
1404 struct drm_gpuva_op *op;
1406 op = gpuva_op_alloc(mgr);
1410 memcpy(op, __op, sizeof(*op));
1412 if (op->op == DRM_GPUVA_OP_REMAP) {
1413 struct drm_gpuva_op_remap *__r = &__op->remap;
1414 struct drm_gpuva_op_remap *r = &op->remap;
1416 r->unmap = kmemdup(__r->unmap, sizeof(*r->unmap),
1418 if (unlikely(!r->unmap))
1422 r->prev = kmemdup(__r->prev, sizeof(*r->prev),
1424 if (unlikely(!r->prev))
1425 goto err_free_unmap;
1429 r->next = kmemdup(__r->next, sizeof(*r->next),
1431 if (unlikely(!r->next))
1436 list_add_tail(&op->entry, &ops->list);
1441 kfree(op->remap.unmap);
1443 kfree(op->remap.prev);
1445 gpuva_op_free(mgr, op);
1450 static const struct drm_gpuva_fn_ops gpuva_list_ops = {
1451 .sm_step_map = drm_gpuva_sm_step,
1452 .sm_step_remap = drm_gpuva_sm_step,
1453 .sm_step_unmap = drm_gpuva_sm_step,
1457 * drm_gpuva_sm_map_ops_create() - creates the &drm_gpuva_ops to split and merge
1458 * @mgr: the &drm_gpuva_manager representing the GPU VA space
1459 * @req_addr: the start address of the new mapping
1460 * @req_range: the range of the new mapping
1461 * @req_obj: the &drm_gem_object to map
1462 * @req_offset: the offset within the &drm_gem_object
1464 * This function creates a list of operations to perform splitting and merging
1465 * of existent mapping(s) with the newly requested one.
1467 * The list can be iterated with &drm_gpuva_for_each_op and must be processed
1468 * in the given order. It can contain map, unmap and remap operations, but it
1469 * also can be empty if no operation is required, e.g. if the requested mapping
1470 * already exists is the exact same way.
1472 * There can be an arbitrary amount of unmap operations, a maximum of two remap
1473 * operations and a single map operation. The latter one represents the original
1474 * map operation requested by the caller.
1476 * Note that before calling this function again with another mapping request it
1477 * is necessary to update the &drm_gpuva_manager's view of the GPU VA space. The
1478 * previously obtained operations must be either processed or abandoned. To
1479 * update the &drm_gpuva_manager's view of the GPU VA space drm_gpuva_insert(),
1480 * drm_gpuva_destroy_locked() and/or drm_gpuva_destroy_unlocked() should be
1483 * After the caller finished processing the returned &drm_gpuva_ops, they must
1484 * be freed with &drm_gpuva_ops_free.
1486 * Returns: a pointer to the &drm_gpuva_ops on success, an ERR_PTR on failure
1488 struct drm_gpuva_ops *
1489 drm_gpuva_sm_map_ops_create(struct drm_gpuva_manager *mgr,
1490 u64 req_addr, u64 req_range,
1491 struct drm_gem_object *req_obj, u64 req_offset)
1493 struct drm_gpuva_ops *ops;
1495 struct drm_gpuva_manager *mgr;
1496 struct drm_gpuva_ops *ops;
1500 ops = kzalloc(sizeof(*ops), GFP_KERNEL);
1502 return ERR_PTR(-ENOMEM);
1504 INIT_LIST_HEAD(&ops->list);
1509 ret = __drm_gpuva_sm_map(mgr, &gpuva_list_ops, &args,
1510 req_addr, req_range,
1511 req_obj, req_offset);
1518 drm_gpuva_ops_free(mgr, ops);
1519 return ERR_PTR(ret);
1521 EXPORT_SYMBOL_GPL(drm_gpuva_sm_map_ops_create);
1524 * drm_gpuva_sm_unmap_ops_create() - creates the &drm_gpuva_ops to split on
1526 * @mgr: the &drm_gpuva_manager representing the GPU VA space
1527 * @req_addr: the start address of the range to unmap
1528 * @req_range: the range of the mappings to unmap
1530 * This function creates a list of operations to perform unmapping and, if
1531 * required, splitting of the mappings overlapping the unmap range.
1533 * The list can be iterated with &drm_gpuva_for_each_op and must be processed
1534 * in the given order. It can contain unmap and remap operations, depending on
1535 * whether there are actual overlapping mappings to split.
1537 * There can be an arbitrary amount of unmap operations and a maximum of two
1540 * Note that before calling this function again with another range to unmap it
1541 * is necessary to update the &drm_gpuva_manager's view of the GPU VA space. The
1542 * previously obtained operations must be processed or abandoned. To update the
1543 * &drm_gpuva_manager's view of the GPU VA space drm_gpuva_insert(),
1544 * drm_gpuva_destroy_locked() and/or drm_gpuva_destroy_unlocked() should be
1547 * After the caller finished processing the returned &drm_gpuva_ops, they must
1548 * be freed with &drm_gpuva_ops_free.
1550 * Returns: a pointer to the &drm_gpuva_ops on success, an ERR_PTR on failure
1552 struct drm_gpuva_ops *
1553 drm_gpuva_sm_unmap_ops_create(struct drm_gpuva_manager *mgr,
1554 u64 req_addr, u64 req_range)
1556 struct drm_gpuva_ops *ops;
1558 struct drm_gpuva_manager *mgr;
1559 struct drm_gpuva_ops *ops;
1563 ops = kzalloc(sizeof(*ops), GFP_KERNEL);
1565 return ERR_PTR(-ENOMEM);
1567 INIT_LIST_HEAD(&ops->list);
1572 ret = __drm_gpuva_sm_unmap(mgr, &gpuva_list_ops, &args,
1573 req_addr, req_range);
1580 drm_gpuva_ops_free(mgr, ops);
1581 return ERR_PTR(ret);
1583 EXPORT_SYMBOL_GPL(drm_gpuva_sm_unmap_ops_create);
1586 * drm_gpuva_prefetch_ops_create() - creates the &drm_gpuva_ops to prefetch
1587 * @mgr: the &drm_gpuva_manager representing the GPU VA space
1588 * @addr: the start address of the range to prefetch
1589 * @range: the range of the mappings to prefetch
1591 * This function creates a list of operations to perform prefetching.
1593 * The list can be iterated with &drm_gpuva_for_each_op and must be processed
1594 * in the given order. It can contain prefetch operations.
1596 * There can be an arbitrary amount of prefetch operations.
1598 * After the caller finished processing the returned &drm_gpuva_ops, they must
1599 * be freed with &drm_gpuva_ops_free.
1601 * Returns: a pointer to the &drm_gpuva_ops on success, an ERR_PTR on failure
1603 struct drm_gpuva_ops *
1604 drm_gpuva_prefetch_ops_create(struct drm_gpuva_manager *mgr,
1605 u64 addr, u64 range)
1607 struct drm_gpuva_ops *ops;
1608 struct drm_gpuva_op *op;
1609 struct drm_gpuva *va;
1610 u64 end = addr + range;
1613 ops = kzalloc(sizeof(*ops), GFP_KERNEL);
1615 return ERR_PTR(-ENOMEM);
1617 INIT_LIST_HEAD(&ops->list);
1619 drm_gpuva_for_each_va_range(va, mgr, addr, end) {
1620 op = gpuva_op_alloc(mgr);
1626 op->op = DRM_GPUVA_OP_PREFETCH;
1627 op->prefetch.va = va;
1628 list_add_tail(&op->entry, &ops->list);
1634 drm_gpuva_ops_free(mgr, ops);
1635 return ERR_PTR(ret);
1637 EXPORT_SYMBOL_GPL(drm_gpuva_prefetch_ops_create);
1640 * drm_gpuva_gem_unmap_ops_create() - creates the &drm_gpuva_ops to unmap a GEM
1641 * @mgr: the &drm_gpuva_manager representing the GPU VA space
1642 * @obj: the &drm_gem_object to unmap
1644 * This function creates a list of operations to perform unmapping for every
1645 * GPUVA attached to a GEM.
1647 * The list can be iterated with &drm_gpuva_for_each_op and consists out of an
1648 * arbitrary amount of unmap operations.
1650 * After the caller finished processing the returned &drm_gpuva_ops, they must
1651 * be freed with &drm_gpuva_ops_free.
1653 * It is the callers responsibility to protect the GEMs GPUVA list against
1654 * concurrent access using the GEMs dma_resv lock.
1656 * Returns: a pointer to the &drm_gpuva_ops on success, an ERR_PTR on failure
1658 struct drm_gpuva_ops *
1659 drm_gpuva_gem_unmap_ops_create(struct drm_gpuva_manager *mgr,
1660 struct drm_gem_object *obj)
1662 struct drm_gpuva_ops *ops;
1663 struct drm_gpuva_op *op;
1664 struct drm_gpuva *va;
1667 drm_gem_gpuva_assert_lock_held(obj);
1669 ops = kzalloc(sizeof(*ops), GFP_KERNEL);
1671 return ERR_PTR(-ENOMEM);
1673 INIT_LIST_HEAD(&ops->list);
1675 drm_gem_for_each_gpuva(va, obj) {
1676 op = gpuva_op_alloc(mgr);
1682 op->op = DRM_GPUVA_OP_UNMAP;
1684 list_add_tail(&op->entry, &ops->list);
1690 drm_gpuva_ops_free(mgr, ops);
1691 return ERR_PTR(ret);
1693 EXPORT_SYMBOL_GPL(drm_gpuva_gem_unmap_ops_create);
1696 * drm_gpuva_ops_free() - free the given &drm_gpuva_ops
1697 * @mgr: the &drm_gpuva_manager the ops were created for
1698 * @ops: the &drm_gpuva_ops to free
1700 * Frees the given &drm_gpuva_ops structure including all the ops associated
1704 drm_gpuva_ops_free(struct drm_gpuva_manager *mgr,
1705 struct drm_gpuva_ops *ops)
1707 struct drm_gpuva_op *op, *next;
1709 drm_gpuva_for_each_op_safe(op, next, ops) {
1710 list_del(&op->entry);
1712 if (op->op == DRM_GPUVA_OP_REMAP) {
1713 kfree(op->remap.prev);
1714 kfree(op->remap.next);
1715 kfree(op->remap.unmap);
1718 gpuva_op_free(mgr, op);
1723 EXPORT_SYMBOL_GPL(drm_gpuva_ops_free);