1 // SPDX-License-Identifier: GPL-2.0-only OR MIT
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_gpuvm.h>
30 #include <linux/interval_tree_generic.h>
36 * The DRM GPU VA Manager, represented by struct drm_gpuvm keeps track of a
37 * 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_gpuvm structure 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_gpuvm 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_gpuvm are
61 * contained within struct drm_gpuva already. Hence, for inserting &drm_gpuva
62 * entries from within dma-fence signalling critical sections it is enough to
63 * pre-allocate the &drm_gpuva structures.
65 * &drm_gem_objects which are private to a single VM can share a common
66 * &dma_resv in order to improve locking efficiency (e.g. with &drm_exec).
67 * For this purpose drivers must pass a &drm_gem_object to drm_gpuvm_init(), in
68 * the following called 'resv object', which serves as the container of the
69 * GPUVM's shared &dma_resv. This resv object can be a driver specific
70 * &drm_gem_object, such as the &drm_gem_object containing the root page table,
71 * but it can also be a 'dummy' object, which can be allocated with
72 * drm_gpuvm_resv_object_alloc().
74 * In order to connect a struct drm_gpuva its backing &drm_gem_object each
75 * &drm_gem_object maintains a list of &drm_gpuvm_bo structures, and each
76 * &drm_gpuvm_bo contains a list of &drm_gpuva structures.
78 * A &drm_gpuvm_bo is an abstraction that represents a combination of a
79 * &drm_gpuvm and a &drm_gem_object. Every such combination should be unique.
80 * This is ensured by the API through drm_gpuvm_bo_obtain() and
81 * drm_gpuvm_bo_obtain_prealloc() which first look into the corresponding
82 * &drm_gem_object list of &drm_gpuvm_bos for an existing instance of this
83 * particular combination. If not existent a new instance is created and linked
84 * to the &drm_gem_object.
86 * &drm_gpuvm_bo structures, since unique for a given &drm_gpuvm, are also used
87 * as entry for the &drm_gpuvm's lists of external and evicted objects. Those
88 * lists are maintained in order to accelerate locking of dma-resv locks and
89 * validation of evicted objects bound in a &drm_gpuvm. For instance, all
90 * &drm_gem_object's &dma_resv of a given &drm_gpuvm can be locked by calling
91 * drm_gpuvm_exec_lock(). Once locked drivers can call drm_gpuvm_validate() in
92 * order to validate all evicted &drm_gem_objects. It is also possible to lock
93 * additional &drm_gem_objects by providing the corresponding parameters to
94 * drm_gpuvm_exec_lock() as well as open code the &drm_exec loop while making
95 * use of helper functions such as drm_gpuvm_prepare_range() or
96 * drm_gpuvm_prepare_objects().
98 * Every bound &drm_gem_object is treated as external object when its &dma_resv
99 * structure is different than the &drm_gpuvm's common &dma_resv structure.
103 * DOC: Split and Merge
105 * Besides its capability to manage and represent a GPU VA space, the
106 * GPU VA manager also provides functions to let the &drm_gpuvm calculate a
107 * sequence of operations to satisfy a given map or unmap request.
109 * Therefore the DRM GPU VA manager provides an algorithm implementing splitting
110 * and merging of existent GPU VA mappings with the ones that are requested to
111 * be mapped or unmapped. This feature is required by the Vulkan API to
112 * implement Vulkan 'Sparse Memory Bindings' - drivers UAPIs often refer to this
115 * Drivers can call drm_gpuvm_sm_map() to receive a sequence of callbacks
116 * containing map, unmap and remap operations for a given newly requested
117 * mapping. The sequence of callbacks represents the set of operations to
118 * execute in order to integrate the new mapping cleanly into the current state
119 * of the GPU VA space.
121 * Depending on how the new GPU VA mapping intersects with the existent mappings
122 * of the GPU VA space the &drm_gpuvm_ops callbacks contain an arbitrary amount
123 * of unmap operations, a maximum of two remap operations and a single map
124 * operation. The caller might receive no callback at all if no operation is
125 * required, e.g. if the requested mapping already exists in the exact same way.
127 * The single map operation represents the original map operation requested by
130 * &drm_gpuva_op_unmap contains a 'keep' field, which indicates whether the
131 * &drm_gpuva to unmap is physically contiguous with the original mapping
132 * request. Optionally, if 'keep' is set, drivers may keep the actual page table
133 * entries for this &drm_gpuva, adding the missing page table entries only and
134 * update the &drm_gpuvm's view of things accordingly.
136 * Drivers may do the same optimization, namely delta page table updates, also
137 * for remap operations. This is possible since &drm_gpuva_op_remap consists of
138 * one unmap operation and one or two map operations, such that drivers can
139 * derive the page table update delta accordingly.
141 * Note that there can't be more than two existent mappings to split up, one at
142 * the beginning and one at the end of the new mapping, hence there is a
143 * maximum of two remap operations.
145 * Analogous to drm_gpuvm_sm_map() drm_gpuvm_sm_unmap() uses &drm_gpuvm_ops to
146 * call back into the driver in order to unmap a range of GPU VA space. The
147 * logic behind this function is way simpler though: For all existent mappings
148 * enclosed by the given range unmap operations are created. For mappings which
149 * are only partically located within the given range, remap operations are
150 * created such that those mappings are split up and re-mapped partically.
152 * As an alternative to drm_gpuvm_sm_map() and drm_gpuvm_sm_unmap(),
153 * drm_gpuvm_sm_map_ops_create() and drm_gpuvm_sm_unmap_ops_create() can be used
154 * to directly obtain an instance of struct drm_gpuva_ops containing a list of
155 * &drm_gpuva_op, which can be iterated with drm_gpuva_for_each_op(). This list
156 * contains the &drm_gpuva_ops analogous to the callbacks one would receive when
157 * calling drm_gpuvm_sm_map() or drm_gpuvm_sm_unmap(). While this way requires
158 * more memory (to allocate the &drm_gpuva_ops), it provides drivers a way to
159 * iterate the &drm_gpuva_op multiple times, e.g. once in a context where memory
160 * allocations are possible (e.g. to allocate GPU page tables) and once in the
161 * dma-fence signalling critical path.
163 * To update the &drm_gpuvm's view of the GPU VA space drm_gpuva_insert() and
164 * drm_gpuva_remove() may be used. These functions can safely be used from
165 * &drm_gpuvm_ops callbacks originating from drm_gpuvm_sm_map() or
166 * drm_gpuvm_sm_unmap(). However, it might be more convenient to use the
167 * provided helper functions drm_gpuva_map(), drm_gpuva_remap() and
168 * drm_gpuva_unmap() instead.
170 * The following diagram depicts the basic relationships of existent GPU VA
171 * mappings, a newly requested mapping and the resulting mappings as implemented
172 * by drm_gpuvm_sm_map() - it doesn't cover any arbitrary combinations of these.
174 * 1) Requested mapping is identical. Replace it, but indicate the backing PTEs
180 * old: |-----------| (bo_offset=n)
183 * req: |-----------| (bo_offset=n)
186 * new: |-----------| (bo_offset=n)
189 * 2) Requested mapping is identical, except for the BO offset, hence replace
195 * old: |-----------| (bo_offset=n)
198 * req: |-----------| (bo_offset=m)
201 * new: |-----------| (bo_offset=m)
204 * 3) Requested mapping is identical, except for the backing BO, hence replace
210 * old: |-----------| (bo_offset=n)
213 * req: |-----------| (bo_offset=n)
216 * new: |-----------| (bo_offset=n)
219 * 4) Existent mapping is a left aligned subset of the requested one, hence
220 * replace the existent one.
225 * old: |-----| (bo_offset=n)
228 * req: |-----------| (bo_offset=n)
231 * new: |-----------| (bo_offset=n)
234 * We expect to see the same result for a request with a different BO
235 * and/or non-contiguous BO offset.
238 * 5) Requested mapping's range is a left aligned subset of the existent one,
239 * but backed by a different BO. Hence, map the requested mapping and split
240 * the existent one adjusting its BO offset.
245 * old: |-----------| (bo_offset=n)
248 * req: |-----| (bo_offset=n)
251 * new: |-----|-----| (b.bo_offset=n, a.bo_offset=n+1)
254 * We expect to see the same result for a request with a different BO
255 * and/or non-contiguous BO offset.
258 * 6) Existent mapping is a superset of the requested mapping. Split it up, but
259 * indicate that the backing PTEs could be kept.
264 * old: |-----------| (bo_offset=n)
267 * req: |-----| (bo_offset=n)
270 * new: |-----|-----| (a.bo_offset=n, a'.bo_offset=n+1)
273 * 7) Requested mapping's range is a right aligned subset of the existent one,
274 * but backed by a different BO. Hence, map the requested mapping and split
275 * the existent one, without adjusting the BO offset.
280 * old: |-----------| (bo_offset=n)
283 * req: |-----| (bo_offset=m)
286 * new: |-----|-----| (a.bo_offset=n,b.bo_offset=m)
289 * 8) Existent mapping is a superset of the requested mapping. Split it up, but
290 * indicate that the backing PTEs could be kept.
295 * old: |-----------| (bo_offset=n)
298 * req: |-----| (bo_offset=n+1)
301 * new: |-----|-----| (a'.bo_offset=n, a.bo_offset=n+1)
304 * 9) Existent mapping is overlapped at the end by the requested mapping backed
305 * by a different BO. Hence, map the requested mapping and split up the
306 * existent one, without adjusting the BO offset.
311 * old: |-----------| (bo_offset=n)
314 * req: |-----------| (bo_offset=m)
317 * new: |-----|-----------| (a.bo_offset=n,b.bo_offset=m)
320 * 10) Existent mapping is overlapped by the requested mapping, both having the
321 * same backing BO with a contiguous offset. Indicate the backing PTEs of
322 * the old mapping could be kept.
327 * old: |-----------| (bo_offset=n)
330 * req: |-----------| (bo_offset=n+1)
333 * new: |-----|-----------| (a'.bo_offset=n, a.bo_offset=n+1)
336 * 11) Requested mapping's range is a centered subset of the existent one
337 * having a different backing BO. Hence, map the requested mapping and split
338 * up the existent one in two mappings, adjusting the BO offset of the right
344 * old: |-----------------| (bo_offset=n)
347 * req: |-----| (bo_offset=m)
350 * new: |-----|-----|-----| (a.bo_offset=n,b.bo_offset=m,a'.bo_offset=n+2)
353 * 12) Requested mapping is a contiguous subset of the existent one. Split it
354 * up, but indicate that the backing PTEs could be kept.
359 * old: |-----------------| (bo_offset=n)
362 * req: |-----| (bo_offset=n+1)
365 * old: |-----|-----|-----| (a'.bo_offset=n, a.bo_offset=n+1, a''.bo_offset=n+2)
368 * 13) Existent mapping is a right aligned subset of the requested one, hence
369 * replace the existent one.
374 * old: |-----| (bo_offset=n+1)
377 * req: |-----------| (bo_offset=n)
380 * new: |-----------| (bo_offset=n)
383 * We expect to see the same result for a request with a different bo
384 * and/or non-contiguous bo_offset.
387 * 14) Existent mapping is a centered subset of the requested one, hence
388 * replace the existent one.
393 * old: |-----| (bo_offset=n+1)
396 * req: |----------------| (bo_offset=n)
399 * new: |----------------| (bo_offset=n)
402 * We expect to see the same result for a request with a different bo
403 * and/or non-contiguous bo_offset.
406 * 15) Existent mappings is overlapped at the beginning by the requested mapping
407 * backed by a different BO. Hence, map the requested mapping and split up
408 * the existent one, adjusting its BO offset accordingly.
413 * old: |-----------| (bo_offset=n)
416 * req: |-----------| (bo_offset=m)
419 * new: |-----------|-----| (b.bo_offset=m,a.bo_offset=n+2)
425 * In terms of managing &drm_gpuva entries DRM GPUVM does not take care of
426 * locking itself, it is the drivers responsibility to take care about locking.
427 * Drivers might want to protect the following operations: inserting, removing
428 * and iterating &drm_gpuva objects as well as generating all kinds of
429 * operations, such as split / merge or prefetch.
431 * DRM GPUVM also does not take care of the locking of the backing
432 * &drm_gem_object buffers GPU VA lists and &drm_gpuvm_bo abstractions by
433 * itself; drivers are responsible to enforce mutual exclusion using either the
434 * GEMs dma_resv lock or alternatively a driver specific external lock. For the
435 * latter see also drm_gem_gpuva_set_lock().
437 * However, DRM GPUVM contains lockdep checks to ensure callers of its API hold
438 * the corresponding lock whenever the &drm_gem_objects GPU VA list is accessed
439 * by functions such as drm_gpuva_link() or drm_gpuva_unlink(), but also
440 * drm_gpuvm_bo_obtain() and drm_gpuvm_bo_put().
442 * The latter is required since on creation and destruction of a &drm_gpuvm_bo
443 * the &drm_gpuvm_bo is attached / removed from the &drm_gem_objects gpuva list.
444 * Subsequent calls to drm_gpuvm_bo_obtain() for the same &drm_gpuvm and
445 * &drm_gem_object must be able to observe previous creations and destructions
446 * of &drm_gpuvm_bos in order to keep instances unique.
448 * The &drm_gpuvm's lists for keeping track of external and evicted objects are
449 * protected against concurrent insertion / removal and iteration internally.
451 * However, drivers still need ensure to protect concurrent calls to functions
452 * iterating those lists, namely drm_gpuvm_prepare_objects() and
453 * drm_gpuvm_validate().
455 * Alternatively, drivers can set the &DRM_GPUVM_RESV_PROTECTED flag to indicate
456 * that the corresponding &dma_resv locks are held in order to protect the
457 * lists. If &DRM_GPUVM_RESV_PROTECTED is set, internal locking is disabled and
458 * the corresponding lockdep checks are enabled. This is an optimization for
459 * drivers which are capable of taking the corresponding &dma_resv locks and
460 * hence do not require internal locking.
466 * This section gives two examples on how to let the DRM GPUVA Manager generate
467 * &drm_gpuva_op in order to satisfy a given map or unmap request and how to
470 * The below code is strictly limited to illustrate the generic usage pattern.
471 * To maintain simplicitly, it doesn't make use of any abstractions for common
472 * code, different (asyncronous) stages with fence signalling critical paths,
473 * any other helpers or error handling in terms of freeing memory and dropping
474 * previously taken locks.
476 * 1) Obtain a list of &drm_gpuva_op to create a new mapping::
478 * // Allocates a new &drm_gpuva.
479 * struct drm_gpuva * driver_gpuva_alloc(void);
481 * // Typically drivers would embedd the &drm_gpuvm and &drm_gpuva
482 * // structure in individual driver structures and lock the dma-resv with
483 * // drm_exec or similar helpers.
484 * int driver_mapping_create(struct drm_gpuvm *gpuvm,
485 * u64 addr, u64 range,
486 * struct drm_gem_object *obj, u64 offset)
488 * struct drm_gpuva_ops *ops;
489 * struct drm_gpuva_op *op
490 * struct drm_gpuvm_bo *vm_bo;
492 * driver_lock_va_space();
493 * ops = drm_gpuvm_sm_map_ops_create(gpuvm, addr, range,
496 * return PTR_ERR(ops);
498 * vm_bo = drm_gpuvm_bo_obtain(gpuvm, obj);
500 * return PTR_ERR(vm_bo);
502 * drm_gpuva_for_each_op(op, ops) {
503 * struct drm_gpuva *va;
506 * case DRM_GPUVA_OP_MAP:
507 * va = driver_gpuva_alloc();
509 * ; // unwind previous VA space updates,
510 * // free memory and unlock
513 * drm_gpuva_map(gpuvm, va, &op->map);
514 * drm_gpuva_link(va, vm_bo);
517 * case DRM_GPUVA_OP_REMAP: {
518 * struct drm_gpuva *prev = NULL, *next = NULL;
520 * va = op->remap.unmap->va;
522 * if (op->remap.prev) {
523 * prev = driver_gpuva_alloc();
525 * ; // unwind previous VA space
526 * // updates, free memory and
530 * if (op->remap.next) {
531 * next = driver_gpuva_alloc();
533 * ; // unwind previous VA space
534 * // updates, free memory and
539 * drm_gpuva_remap(prev, next, &op->remap);
542 * drm_gpuva_link(prev, va->vm_bo);
544 * drm_gpuva_link(next, va->vm_bo);
545 * drm_gpuva_unlink(va);
549 * case DRM_GPUVA_OP_UNMAP:
550 * va = op->unmap->va;
553 * drm_gpuva_unlink(va);
554 * drm_gpuva_unmap(&op->unmap);
561 * drm_gpuvm_bo_put(vm_bo);
562 * driver_unlock_va_space();
567 * 2) Receive a callback for each &drm_gpuva_op to create a new mapping::
569 * struct driver_context {
570 * struct drm_gpuvm *gpuvm;
571 * struct drm_gpuvm_bo *vm_bo;
572 * struct drm_gpuva *new_va;
573 * struct drm_gpuva *prev_va;
574 * struct drm_gpuva *next_va;
577 * // ops to pass to drm_gpuvm_init()
578 * static const struct drm_gpuvm_ops driver_gpuvm_ops = {
579 * .sm_step_map = driver_gpuva_map,
580 * .sm_step_remap = driver_gpuva_remap,
581 * .sm_step_unmap = driver_gpuva_unmap,
584 * // Typically drivers would embedd the &drm_gpuvm and &drm_gpuva
585 * // structure in individual driver structures and lock the dma-resv with
586 * // drm_exec or similar helpers.
587 * int driver_mapping_create(struct drm_gpuvm *gpuvm,
588 * u64 addr, u64 range,
589 * struct drm_gem_object *obj, u64 offset)
591 * struct driver_context ctx;
592 * struct drm_gpuvm_bo *vm_bo;
593 * struct drm_gpuva_ops *ops;
594 * struct drm_gpuva_op *op;
599 * ctx.new_va = kzalloc(sizeof(*ctx.new_va), GFP_KERNEL);
600 * ctx.prev_va = kzalloc(sizeof(*ctx.prev_va), GFP_KERNEL);
601 * ctx.next_va = kzalloc(sizeof(*ctx.next_va), GFP_KERNEL);
602 * ctx.vm_bo = drm_gpuvm_bo_create(gpuvm, obj);
603 * if (!ctx.new_va || !ctx.prev_va || !ctx.next_va || !vm_bo) {
608 * // Typically protected with a driver specific GEM gpuva lock
609 * // used in the fence signaling path for drm_gpuva_link() and
610 * // drm_gpuva_unlink(), hence pre-allocate.
611 * ctx.vm_bo = drm_gpuvm_bo_obtain_prealloc(ctx.vm_bo);
613 * driver_lock_va_space();
614 * ret = drm_gpuvm_sm_map(gpuvm, &ctx, addr, range, obj, offset);
615 * driver_unlock_va_space();
618 * drm_gpuvm_bo_put(ctx.vm_bo);
620 * kfree(ctx.prev_va);
621 * kfree(ctx.next_va);
625 * int driver_gpuva_map(struct drm_gpuva_op *op, void *__ctx)
627 * struct driver_context *ctx = __ctx;
629 * drm_gpuva_map(ctx->vm, ctx->new_va, &op->map);
631 * drm_gpuva_link(ctx->new_va, ctx->vm_bo);
633 * // prevent the new GPUVA from being freed in
634 * // driver_mapping_create()
635 * ctx->new_va = NULL;
640 * int driver_gpuva_remap(struct drm_gpuva_op *op, void *__ctx)
642 * struct driver_context *ctx = __ctx;
643 * struct drm_gpuva *va = op->remap.unmap->va;
645 * drm_gpuva_remap(ctx->prev_va, ctx->next_va, &op->remap);
647 * if (op->remap.prev) {
648 * drm_gpuva_link(ctx->prev_va, va->vm_bo);
649 * ctx->prev_va = NULL;
652 * if (op->remap.next) {
653 * drm_gpuva_link(ctx->next_va, va->vm_bo);
654 * ctx->next_va = NULL;
657 * drm_gpuva_unlink(va);
663 * int driver_gpuva_unmap(struct drm_gpuva_op *op, void *__ctx)
665 * drm_gpuva_unlink(op->unmap.va);
666 * drm_gpuva_unmap(&op->unmap);
667 * kfree(op->unmap.va);
674 * get_next_vm_bo_from_list() - get the next vm_bo element
675 * @__gpuvm: the &drm_gpuvm
676 * @__list_name: the name of the list we're iterating on
677 * @__local_list: a pointer to the local list used to store already iterated items
678 * @__prev_vm_bo: the previous element we got from get_next_vm_bo_from_list()
680 * This helper is here to provide lockless list iteration. Lockless as in, the
681 * iterator releases the lock immediately after picking the first element from
682 * the list, so list insertion deletion can happen concurrently.
684 * Elements popped from the original list are kept in a local list, so removal
685 * and is_empty checks can still happen while we're iterating the list.
687 #define get_next_vm_bo_from_list(__gpuvm, __list_name, __local_list, __prev_vm_bo) \
689 struct drm_gpuvm_bo *__vm_bo = NULL; \
691 drm_gpuvm_bo_put(__prev_vm_bo); \
693 spin_lock(&(__gpuvm)->__list_name.lock); \
694 if (!(__gpuvm)->__list_name.local_list) \
695 (__gpuvm)->__list_name.local_list = __local_list; \
697 drm_WARN_ON((__gpuvm)->drm, \
698 (__gpuvm)->__list_name.local_list != __local_list); \
700 while (!list_empty(&(__gpuvm)->__list_name.list)) { \
701 __vm_bo = list_first_entry(&(__gpuvm)->__list_name.list, \
702 struct drm_gpuvm_bo, \
703 list.entry.__list_name); \
704 if (kref_get_unless_zero(&__vm_bo->kref)) { \
705 list_move_tail(&(__vm_bo)->list.entry.__list_name, \
709 list_del_init(&(__vm_bo)->list.entry.__list_name); \
713 spin_unlock(&(__gpuvm)->__list_name.lock); \
719 * for_each_vm_bo_in_list() - internal vm_bo list iterator
720 * @__gpuvm: the &drm_gpuvm
721 * @__list_name: the name of the list we're iterating on
722 * @__local_list: a pointer to the local list used to store already iterated items
723 * @__vm_bo: the struct drm_gpuvm_bo to assign in each iteration step
725 * This helper is here to provide lockless list iteration. Lockless as in, the
726 * iterator releases the lock immediately after picking the first element from the
727 * list, hence list insertion and deletion can happen concurrently.
729 * It is not allowed to re-assign the vm_bo pointer from inside this loop.
733 * struct drm_gpuvm_bo *vm_bo;
734 * LIST_HEAD(my_local_list);
737 * for_each_vm_bo_in_list(gpuvm, <list_name>, &my_local_list, vm_bo) {
738 * ret = do_something_with_vm_bo(..., vm_bo);
742 * // Drop ref in case we break out of the loop.
743 * drm_gpuvm_bo_put(vm_bo);
744 * restore_vm_bo_list(gpuvm, <list_name>, &my_local_list);
747 * Only used for internal list iterations, not meant to be exposed to the outside
750 #define for_each_vm_bo_in_list(__gpuvm, __list_name, __local_list, __vm_bo) \
751 for (__vm_bo = get_next_vm_bo_from_list(__gpuvm, __list_name, \
752 __local_list, NULL); \
754 __vm_bo = get_next_vm_bo_from_list(__gpuvm, __list_name, \
755 __local_list, __vm_bo))
758 __restore_vm_bo_list(struct drm_gpuvm *gpuvm, spinlock_t *lock,
759 struct list_head *list, struct list_head **local_list)
761 /* Merge back the two lists, moving local list elements to the
762 * head to preserve previous ordering, in case it matters.
766 list_splice(*local_list, list);
773 * restore_vm_bo_list() - move vm_bo elements back to their original list
774 * @__gpuvm: the &drm_gpuvm
775 * @__list_name: the name of the list we're iterating on
777 * When we're done iterating a vm_bo list, we should call restore_vm_bo_list()
778 * to restore the original state and let new iterations take place.
780 #define restore_vm_bo_list(__gpuvm, __list_name) \
781 __restore_vm_bo_list((__gpuvm), &(__gpuvm)->__list_name.lock, \
782 &(__gpuvm)->__list_name.list, \
783 &(__gpuvm)->__list_name.local_list)
786 cond_spin_lock(spinlock_t *lock, bool cond)
793 cond_spin_unlock(spinlock_t *lock, bool cond)
800 __drm_gpuvm_bo_list_add(struct drm_gpuvm *gpuvm, spinlock_t *lock,
801 struct list_head *entry, struct list_head *list)
803 cond_spin_lock(lock, !!lock);
804 if (list_empty(entry))
805 list_add_tail(entry, list);
806 cond_spin_unlock(lock, !!lock);
810 * drm_gpuvm_bo_list_add() - insert a vm_bo into the given list
811 * @__vm_bo: the &drm_gpuvm_bo
812 * @__list_name: the name of the list to insert into
813 * @__lock: whether to lock with the internal spinlock
815 * Inserts the given @__vm_bo into the list specified by @__list_name.
817 #define drm_gpuvm_bo_list_add(__vm_bo, __list_name, __lock) \
818 __drm_gpuvm_bo_list_add((__vm_bo)->vm, \
819 __lock ? &(__vm_bo)->vm->__list_name.lock : \
821 &(__vm_bo)->list.entry.__list_name, \
822 &(__vm_bo)->vm->__list_name.list)
825 __drm_gpuvm_bo_list_del(struct drm_gpuvm *gpuvm, spinlock_t *lock,
826 struct list_head *entry, bool init)
828 cond_spin_lock(lock, !!lock);
830 if (!list_empty(entry))
831 list_del_init(entry);
835 cond_spin_unlock(lock, !!lock);
839 * drm_gpuvm_bo_list_del_init() - remove a vm_bo from the given list
840 * @__vm_bo: the &drm_gpuvm_bo
841 * @__list_name: the name of the list to insert into
842 * @__lock: whether to lock with the internal spinlock
844 * Removes the given @__vm_bo from the list specified by @__list_name.
846 #define drm_gpuvm_bo_list_del_init(__vm_bo, __list_name, __lock) \
847 __drm_gpuvm_bo_list_del((__vm_bo)->vm, \
848 __lock ? &(__vm_bo)->vm->__list_name.lock : \
850 &(__vm_bo)->list.entry.__list_name, \
854 * drm_gpuvm_bo_list_del() - remove a vm_bo from the given list
855 * @__vm_bo: the &drm_gpuvm_bo
856 * @__list_name: the name of the list to insert into
857 * @__lock: whether to lock with the internal spinlock
859 * Removes the given @__vm_bo from the list specified by @__list_name.
861 #define drm_gpuvm_bo_list_del(__vm_bo, __list_name, __lock) \
862 __drm_gpuvm_bo_list_del((__vm_bo)->vm, \
863 __lock ? &(__vm_bo)->vm->__list_name.lock : \
865 &(__vm_bo)->list.entry.__list_name, \
868 #define to_drm_gpuva(__node) container_of((__node), struct drm_gpuva, rb.node)
870 #define GPUVA_START(node) ((node)->va.addr)
871 #define GPUVA_LAST(node) ((node)->va.addr + (node)->va.range - 1)
873 /* We do not actually use drm_gpuva_it_next(), tell the compiler to not complain
876 INTERVAL_TREE_DEFINE(struct drm_gpuva, rb.node, u64, rb.__subtree_last,
877 GPUVA_START, GPUVA_LAST, static __maybe_unused,
880 static int __drm_gpuva_insert(struct drm_gpuvm *gpuvm,
881 struct drm_gpuva *va);
882 static void __drm_gpuva_remove(struct drm_gpuva *va);
885 drm_gpuvm_check_overflow(u64 addr, u64 range)
889 return check_add_overflow(addr, range, &end);
893 drm_gpuvm_warn_check_overflow(struct drm_gpuvm *gpuvm, u64 addr, u64 range)
895 return drm_WARN(gpuvm->drm, drm_gpuvm_check_overflow(addr, range),
896 "GPUVA address limited to %zu bytes.\n", sizeof(addr));
900 drm_gpuvm_in_mm_range(struct drm_gpuvm *gpuvm, u64 addr, u64 range)
902 u64 end = addr + range;
903 u64 mm_start = gpuvm->mm_start;
904 u64 mm_end = mm_start + gpuvm->mm_range;
906 return addr >= mm_start && end <= mm_end;
910 drm_gpuvm_in_kernel_node(struct drm_gpuvm *gpuvm, u64 addr, u64 range)
912 u64 end = addr + range;
913 u64 kstart = gpuvm->kernel_alloc_node.va.addr;
914 u64 krange = gpuvm->kernel_alloc_node.va.range;
915 u64 kend = kstart + krange;
917 return krange && addr < kend && kstart < end;
921 * drm_gpuvm_range_valid() - checks whether the given range is valid for the
923 * @gpuvm: the GPUVM to check the range for
924 * @addr: the base address
925 * @range: the range starting from the base address
927 * Checks whether the range is within the GPUVM's managed boundaries.
929 * Returns: true for a valid range, false otherwise
932 drm_gpuvm_range_valid(struct drm_gpuvm *gpuvm,
935 return !drm_gpuvm_check_overflow(addr, range) &&
936 drm_gpuvm_in_mm_range(gpuvm, addr, range) &&
937 !drm_gpuvm_in_kernel_node(gpuvm, addr, range);
939 EXPORT_SYMBOL_GPL(drm_gpuvm_range_valid);
942 drm_gpuvm_gem_object_free(struct drm_gem_object *obj)
944 drm_gem_object_release(obj);
948 static const struct drm_gem_object_funcs drm_gpuvm_object_funcs = {
949 .free = drm_gpuvm_gem_object_free,
953 * drm_gpuvm_resv_object_alloc() - allocate a dummy &drm_gem_object
954 * @drm: the drivers &drm_device
956 * Allocates a dummy &drm_gem_object which can be passed to drm_gpuvm_init() in
957 * order to serve as root GEM object providing the &drm_resv shared across
958 * &drm_gem_objects local to a single GPUVM.
960 * Returns: the &drm_gem_object on success, NULL on failure
962 struct drm_gem_object *
963 drm_gpuvm_resv_object_alloc(struct drm_device *drm)
965 struct drm_gem_object *obj;
967 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
971 obj->funcs = &drm_gpuvm_object_funcs;
972 drm_gem_private_object_init(drm, obj, 0);
976 EXPORT_SYMBOL_GPL(drm_gpuvm_resv_object_alloc);
979 * drm_gpuvm_init() - initialize a &drm_gpuvm
980 * @gpuvm: pointer to the &drm_gpuvm to initialize
981 * @name: the name of the GPU VA space
982 * @flags: the &drm_gpuvm_flags for this GPUVM
983 * @drm: the &drm_device this VM resides in
984 * @r_obj: the resv &drm_gem_object providing the GPUVM's common &dma_resv
985 * @start_offset: the start offset of the GPU VA space
986 * @range: the size of the GPU VA space
987 * @reserve_offset: the start of the kernel reserved GPU VA area
988 * @reserve_range: the size of the kernel reserved GPU VA area
989 * @ops: &drm_gpuvm_ops called on &drm_gpuvm_sm_map / &drm_gpuvm_sm_unmap
991 * The &drm_gpuvm must be initialized with this function before use.
993 * Note that @gpuvm must be cleared to 0 before calling this function. The given
994 * &name is expected to be managed by the surrounding driver structures.
997 drm_gpuvm_init(struct drm_gpuvm *gpuvm, const char *name,
998 enum drm_gpuvm_flags flags,
999 struct drm_device *drm,
1000 struct drm_gem_object *r_obj,
1001 u64 start_offset, u64 range,
1002 u64 reserve_offset, u64 reserve_range,
1003 const struct drm_gpuvm_ops *ops)
1005 gpuvm->rb.tree = RB_ROOT_CACHED;
1006 INIT_LIST_HEAD(&gpuvm->rb.list);
1008 INIT_LIST_HEAD(&gpuvm->extobj.list);
1009 spin_lock_init(&gpuvm->extobj.lock);
1011 INIT_LIST_HEAD(&gpuvm->evict.list);
1012 spin_lock_init(&gpuvm->evict.lock);
1014 kref_init(&gpuvm->kref);
1016 gpuvm->name = name ? name : "unknown";
1017 gpuvm->flags = flags;
1020 gpuvm->r_obj = r_obj;
1022 drm_gem_object_get(r_obj);
1024 drm_gpuvm_warn_check_overflow(gpuvm, start_offset, range);
1025 gpuvm->mm_start = start_offset;
1026 gpuvm->mm_range = range;
1028 memset(&gpuvm->kernel_alloc_node, 0, sizeof(struct drm_gpuva));
1029 if (reserve_range) {
1030 gpuvm->kernel_alloc_node.va.addr = reserve_offset;
1031 gpuvm->kernel_alloc_node.va.range = reserve_range;
1033 if (likely(!drm_gpuvm_warn_check_overflow(gpuvm, reserve_offset,
1035 __drm_gpuva_insert(gpuvm, &gpuvm->kernel_alloc_node);
1038 EXPORT_SYMBOL_GPL(drm_gpuvm_init);
1041 drm_gpuvm_fini(struct drm_gpuvm *gpuvm)
1045 if (gpuvm->kernel_alloc_node.va.range)
1046 __drm_gpuva_remove(&gpuvm->kernel_alloc_node);
1048 drm_WARN(gpuvm->drm, !RB_EMPTY_ROOT(&gpuvm->rb.tree.rb_root),
1049 "GPUVA tree is not empty, potentially leaking memory.\n");
1051 drm_WARN(gpuvm->drm, !list_empty(&gpuvm->extobj.list),
1052 "Extobj list should be empty.\n");
1053 drm_WARN(gpuvm->drm, !list_empty(&gpuvm->evict.list),
1054 "Evict list should be empty.\n");
1056 drm_gem_object_put(gpuvm->r_obj);
1060 drm_gpuvm_free(struct kref *kref)
1062 struct drm_gpuvm *gpuvm = container_of(kref, struct drm_gpuvm, kref);
1064 drm_gpuvm_fini(gpuvm);
1066 if (drm_WARN_ON(gpuvm->drm, !gpuvm->ops->vm_free))
1069 gpuvm->ops->vm_free(gpuvm);
1073 * drm_gpuvm_put() - drop a struct drm_gpuvm reference
1074 * @gpuvm: the &drm_gpuvm to release the reference of
1076 * This releases a reference to @gpuvm.
1078 * This function may be called from atomic context.
1081 drm_gpuvm_put(struct drm_gpuvm *gpuvm)
1084 kref_put(&gpuvm->kref, drm_gpuvm_free);
1086 EXPORT_SYMBOL_GPL(drm_gpuvm_put);
1089 exec_prepare_obj(struct drm_exec *exec, struct drm_gem_object *obj,
1090 unsigned int num_fences)
1092 return num_fences ? drm_exec_prepare_obj(exec, obj, num_fences) :
1093 drm_exec_lock_obj(exec, obj);
1097 * drm_gpuvm_prepare_vm() - prepare the GPUVMs common dma-resv
1098 * @gpuvm: the &drm_gpuvm
1099 * @exec: the &drm_exec context
1100 * @num_fences: the amount of &dma_fences to reserve
1102 * Calls drm_exec_prepare_obj() for the GPUVMs dummy &drm_gem_object; if
1103 * @num_fences is zero drm_exec_lock_obj() is called instead.
1105 * Using this function directly, it is the drivers responsibility to call
1106 * drm_exec_init() and drm_exec_fini() accordingly.
1108 * Returns: 0 on success, negative error code on failure.
1111 drm_gpuvm_prepare_vm(struct drm_gpuvm *gpuvm,
1112 struct drm_exec *exec,
1113 unsigned int num_fences)
1115 return exec_prepare_obj(exec, gpuvm->r_obj, num_fences);
1117 EXPORT_SYMBOL_GPL(drm_gpuvm_prepare_vm);
1120 __drm_gpuvm_prepare_objects(struct drm_gpuvm *gpuvm,
1121 struct drm_exec *exec,
1122 unsigned int num_fences)
1124 struct drm_gpuvm_bo *vm_bo;
1128 for_each_vm_bo_in_list(gpuvm, extobj, &extobjs, vm_bo) {
1129 ret = exec_prepare_obj(exec, vm_bo->obj, num_fences);
1133 /* Drop ref in case we break out of the loop. */
1134 drm_gpuvm_bo_put(vm_bo);
1135 restore_vm_bo_list(gpuvm, extobj);
1141 drm_gpuvm_prepare_objects_locked(struct drm_gpuvm *gpuvm,
1142 struct drm_exec *exec,
1143 unsigned int num_fences)
1145 struct drm_gpuvm_bo *vm_bo;
1148 drm_gpuvm_resv_assert_held(gpuvm);
1149 list_for_each_entry(vm_bo, &gpuvm->extobj.list, list.entry.extobj) {
1150 ret = exec_prepare_obj(exec, vm_bo->obj, num_fences);
1155 drm_gpuvm_bo_list_add(vm_bo, evict, false);
1162 * drm_gpuvm_prepare_objects() - prepare all assoiciated BOs
1163 * @gpuvm: the &drm_gpuvm
1164 * @exec: the &drm_exec locking context
1165 * @num_fences: the amount of &dma_fences to reserve
1167 * Calls drm_exec_prepare_obj() for all &drm_gem_objects the given
1168 * &drm_gpuvm contains mappings of; if @num_fences is zero drm_exec_lock_obj()
1169 * is called instead.
1171 * Using this function directly, it is the drivers responsibility to call
1172 * drm_exec_init() and drm_exec_fini() accordingly.
1174 * Note: This function is safe against concurrent insertion and removal of
1175 * external objects, however it is not safe against concurrent usage itself.
1177 * Drivers need to make sure to protect this case with either an outer VM lock
1178 * or by calling drm_gpuvm_prepare_vm() before this function within the
1179 * drm_exec_until_all_locked() loop, such that the GPUVM's dma-resv lock ensures
1182 * Returns: 0 on success, negative error code on failure.
1185 drm_gpuvm_prepare_objects(struct drm_gpuvm *gpuvm,
1186 struct drm_exec *exec,
1187 unsigned int num_fences)
1189 if (drm_gpuvm_resv_protected(gpuvm))
1190 return drm_gpuvm_prepare_objects_locked(gpuvm, exec,
1193 return __drm_gpuvm_prepare_objects(gpuvm, exec, num_fences);
1195 EXPORT_SYMBOL_GPL(drm_gpuvm_prepare_objects);
1198 * drm_gpuvm_prepare_range() - prepare all BOs mapped within a given range
1199 * @gpuvm: the &drm_gpuvm
1200 * @exec: the &drm_exec locking context
1201 * @addr: the start address within the VA space
1202 * @range: the range to iterate within the VA space
1203 * @num_fences: the amount of &dma_fences to reserve
1205 * Calls drm_exec_prepare_obj() for all &drm_gem_objects mapped between @addr
1206 * and @addr + @range; if @num_fences is zero drm_exec_lock_obj() is called
1209 * Returns: 0 on success, negative error code on failure.
1212 drm_gpuvm_prepare_range(struct drm_gpuvm *gpuvm, struct drm_exec *exec,
1213 u64 addr, u64 range, unsigned int num_fences)
1215 struct drm_gpuva *va;
1216 u64 end = addr + range;
1219 drm_gpuvm_for_each_va_range(va, gpuvm, addr, end) {
1220 struct drm_gem_object *obj = va->gem.obj;
1222 ret = exec_prepare_obj(exec, obj, num_fences);
1229 EXPORT_SYMBOL_GPL(drm_gpuvm_prepare_range);
1232 * drm_gpuvm_exec_lock() - lock all dma-resv of all assoiciated BOs
1233 * @vm_exec: the &drm_gpuvm_exec wrapper
1235 * Acquires all dma-resv locks of all &drm_gem_objects the given
1236 * &drm_gpuvm contains mappings of.
1238 * Addionally, when calling this function with struct drm_gpuvm_exec::extra
1239 * being set the driver receives the given @fn callback to lock additional
1240 * dma-resv in the context of the &drm_gpuvm_exec instance. Typically, drivers
1241 * would call drm_exec_prepare_obj() from within this callback.
1243 * Returns: 0 on success, negative error code on failure.
1246 drm_gpuvm_exec_lock(struct drm_gpuvm_exec *vm_exec)
1248 struct drm_gpuvm *gpuvm = vm_exec->vm;
1249 struct drm_exec *exec = &vm_exec->exec;
1250 unsigned int num_fences = vm_exec->num_fences;
1253 drm_exec_init(exec, vm_exec->flags, 0);
1255 drm_exec_until_all_locked(exec) {
1256 ret = drm_gpuvm_prepare_vm(gpuvm, exec, num_fences);
1257 drm_exec_retry_on_contention(exec);
1261 ret = drm_gpuvm_prepare_objects(gpuvm, exec, num_fences);
1262 drm_exec_retry_on_contention(exec);
1266 if (vm_exec->extra.fn) {
1267 ret = vm_exec->extra.fn(vm_exec);
1268 drm_exec_retry_on_contention(exec);
1277 drm_exec_fini(exec);
1280 EXPORT_SYMBOL_GPL(drm_gpuvm_exec_lock);
1283 fn_lock_array(struct drm_gpuvm_exec *vm_exec)
1286 struct drm_gem_object **objs;
1287 unsigned int num_objs;
1288 } *args = vm_exec->extra.priv;
1290 return drm_exec_prepare_array(&vm_exec->exec, args->objs,
1291 args->num_objs, vm_exec->num_fences);
1295 * drm_gpuvm_exec_lock_array() - lock all dma-resv of all assoiciated BOs
1296 * @vm_exec: the &drm_gpuvm_exec wrapper
1297 * @objs: additional &drm_gem_objects to lock
1298 * @num_objs: the number of additional &drm_gem_objects to lock
1300 * Acquires all dma-resv locks of all &drm_gem_objects the given &drm_gpuvm
1301 * contains mappings of, plus the ones given through @objs.
1303 * Returns: 0 on success, negative error code on failure.
1306 drm_gpuvm_exec_lock_array(struct drm_gpuvm_exec *vm_exec,
1307 struct drm_gem_object **objs,
1308 unsigned int num_objs)
1311 struct drm_gem_object **objs;
1312 unsigned int num_objs;
1316 args.num_objs = num_objs;
1318 vm_exec->extra.fn = fn_lock_array;
1319 vm_exec->extra.priv = &args;
1321 return drm_gpuvm_exec_lock(vm_exec);
1323 EXPORT_SYMBOL_GPL(drm_gpuvm_exec_lock_array);
1326 * drm_gpuvm_exec_lock_range() - prepare all BOs mapped within a given range
1327 * @vm_exec: the &drm_gpuvm_exec wrapper
1328 * @addr: the start address within the VA space
1329 * @range: the range to iterate within the VA space
1331 * Acquires all dma-resv locks of all &drm_gem_objects mapped between @addr and
1334 * Returns: 0 on success, negative error code on failure.
1337 drm_gpuvm_exec_lock_range(struct drm_gpuvm_exec *vm_exec,
1338 u64 addr, u64 range)
1340 struct drm_gpuvm *gpuvm = vm_exec->vm;
1341 struct drm_exec *exec = &vm_exec->exec;
1344 drm_exec_init(exec, vm_exec->flags, 0);
1346 drm_exec_until_all_locked(exec) {
1347 ret = drm_gpuvm_prepare_range(gpuvm, exec, addr, range,
1348 vm_exec->num_fences);
1349 drm_exec_retry_on_contention(exec);
1357 drm_exec_fini(exec);
1360 EXPORT_SYMBOL_GPL(drm_gpuvm_exec_lock_range);
1363 __drm_gpuvm_validate(struct drm_gpuvm *gpuvm, struct drm_exec *exec)
1365 const struct drm_gpuvm_ops *ops = gpuvm->ops;
1366 struct drm_gpuvm_bo *vm_bo;
1370 for_each_vm_bo_in_list(gpuvm, evict, &evict, vm_bo) {
1371 ret = ops->vm_bo_validate(vm_bo, exec);
1375 /* Drop ref in case we break out of the loop. */
1376 drm_gpuvm_bo_put(vm_bo);
1377 restore_vm_bo_list(gpuvm, evict);
1383 drm_gpuvm_validate_locked(struct drm_gpuvm *gpuvm, struct drm_exec *exec)
1385 const struct drm_gpuvm_ops *ops = gpuvm->ops;
1386 struct drm_gpuvm_bo *vm_bo, *next;
1389 drm_gpuvm_resv_assert_held(gpuvm);
1391 list_for_each_entry_safe(vm_bo, next, &gpuvm->evict.list,
1393 ret = ops->vm_bo_validate(vm_bo, exec);
1397 dma_resv_assert_held(vm_bo->obj->resv);
1398 if (!vm_bo->evicted)
1399 drm_gpuvm_bo_list_del_init(vm_bo, evict, false);
1406 * drm_gpuvm_validate() - validate all BOs marked as evicted
1407 * @gpuvm: the &drm_gpuvm to validate evicted BOs
1408 * @exec: the &drm_exec instance used for locking the GPUVM
1410 * Calls the &drm_gpuvm_ops::vm_bo_validate callback for all evicted buffer
1411 * objects being mapped in the given &drm_gpuvm.
1413 * Returns: 0 on success, negative error code on failure.
1416 drm_gpuvm_validate(struct drm_gpuvm *gpuvm, struct drm_exec *exec)
1418 const struct drm_gpuvm_ops *ops = gpuvm->ops;
1420 if (unlikely(!ops || !ops->vm_bo_validate))
1423 if (drm_gpuvm_resv_protected(gpuvm))
1424 return drm_gpuvm_validate_locked(gpuvm, exec);
1426 return __drm_gpuvm_validate(gpuvm, exec);
1428 EXPORT_SYMBOL_GPL(drm_gpuvm_validate);
1431 * drm_gpuvm_resv_add_fence - add fence to private and all extobj
1433 * @gpuvm: the &drm_gpuvm to add a fence to
1434 * @exec: the &drm_exec locking context
1435 * @fence: fence to add
1436 * @private_usage: private dma-resv usage
1437 * @extobj_usage: extobj dma-resv usage
1440 drm_gpuvm_resv_add_fence(struct drm_gpuvm *gpuvm,
1441 struct drm_exec *exec,
1442 struct dma_fence *fence,
1443 enum dma_resv_usage private_usage,
1444 enum dma_resv_usage extobj_usage)
1446 struct drm_gem_object *obj;
1447 unsigned long index;
1449 drm_exec_for_each_locked_object(exec, index, obj) {
1450 dma_resv_assert_held(obj->resv);
1451 dma_resv_add_fence(obj->resv, fence,
1452 drm_gpuvm_is_extobj(gpuvm, obj) ?
1453 extobj_usage : private_usage);
1456 EXPORT_SYMBOL_GPL(drm_gpuvm_resv_add_fence);
1459 * drm_gpuvm_bo_create() - create a new instance of struct drm_gpuvm_bo
1460 * @gpuvm: The &drm_gpuvm the @obj is mapped in.
1461 * @obj: The &drm_gem_object being mapped in the @gpuvm.
1463 * If provided by the driver, this function uses the &drm_gpuvm_ops
1464 * vm_bo_alloc() callback to allocate.
1466 * Returns: a pointer to the &drm_gpuvm_bo on success, NULL on failure
1468 struct drm_gpuvm_bo *
1469 drm_gpuvm_bo_create(struct drm_gpuvm *gpuvm,
1470 struct drm_gem_object *obj)
1472 const struct drm_gpuvm_ops *ops = gpuvm->ops;
1473 struct drm_gpuvm_bo *vm_bo;
1475 if (ops && ops->vm_bo_alloc)
1476 vm_bo = ops->vm_bo_alloc();
1478 vm_bo = kzalloc(sizeof(*vm_bo), GFP_KERNEL);
1480 if (unlikely(!vm_bo))
1483 vm_bo->vm = drm_gpuvm_get(gpuvm);
1485 drm_gem_object_get(obj);
1487 kref_init(&vm_bo->kref);
1488 INIT_LIST_HEAD(&vm_bo->list.gpuva);
1489 INIT_LIST_HEAD(&vm_bo->list.entry.gem);
1491 INIT_LIST_HEAD(&vm_bo->list.entry.extobj);
1492 INIT_LIST_HEAD(&vm_bo->list.entry.evict);
1496 EXPORT_SYMBOL_GPL(drm_gpuvm_bo_create);
1499 drm_gpuvm_bo_destroy(struct kref *kref)
1501 struct drm_gpuvm_bo *vm_bo = container_of(kref, struct drm_gpuvm_bo,
1503 struct drm_gpuvm *gpuvm = vm_bo->vm;
1504 const struct drm_gpuvm_ops *ops = gpuvm->ops;
1505 struct drm_gem_object *obj = vm_bo->obj;
1506 bool lock = !drm_gpuvm_resv_protected(gpuvm);
1509 drm_gpuvm_resv_assert_held(gpuvm);
1511 drm_gpuvm_bo_list_del(vm_bo, extobj, lock);
1512 drm_gpuvm_bo_list_del(vm_bo, evict, lock);
1514 drm_gem_gpuva_assert_lock_held(obj);
1515 list_del(&vm_bo->list.entry.gem);
1517 if (ops && ops->vm_bo_free)
1518 ops->vm_bo_free(vm_bo);
1522 drm_gpuvm_put(gpuvm);
1523 drm_gem_object_put(obj);
1527 * drm_gpuvm_bo_put() - drop a struct drm_gpuvm_bo reference
1528 * @vm_bo: the &drm_gpuvm_bo to release the reference of
1530 * This releases a reference to @vm_bo.
1532 * If the reference count drops to zero, the &gpuvm_bo is destroyed, which
1533 * includes removing it from the GEMs gpuva list. Hence, if a call to this
1534 * function can potentially let the reference count drop to zero the caller must
1535 * hold the dma-resv or driver specific GEM gpuva lock.
1537 * This function may only be called from non-atomic context.
1539 * Returns: true if vm_bo was destroyed, false otherwise.
1542 drm_gpuvm_bo_put(struct drm_gpuvm_bo *vm_bo)
1547 return !!kref_put(&vm_bo->kref, drm_gpuvm_bo_destroy);
1551 EXPORT_SYMBOL_GPL(drm_gpuvm_bo_put);
1553 static struct drm_gpuvm_bo *
1554 __drm_gpuvm_bo_find(struct drm_gpuvm *gpuvm,
1555 struct drm_gem_object *obj)
1557 struct drm_gpuvm_bo *vm_bo;
1559 drm_gem_gpuva_assert_lock_held(obj);
1560 drm_gem_for_each_gpuvm_bo(vm_bo, obj)
1561 if (vm_bo->vm == gpuvm)
1568 * drm_gpuvm_bo_find() - find the &drm_gpuvm_bo for the given
1569 * &drm_gpuvm and &drm_gem_object
1570 * @gpuvm: The &drm_gpuvm the @obj is mapped in.
1571 * @obj: The &drm_gem_object being mapped in the @gpuvm.
1573 * Find the &drm_gpuvm_bo representing the combination of the given
1574 * &drm_gpuvm and &drm_gem_object. If found, increases the reference
1575 * count of the &drm_gpuvm_bo accordingly.
1577 * Returns: a pointer to the &drm_gpuvm_bo on success, NULL on failure
1579 struct drm_gpuvm_bo *
1580 drm_gpuvm_bo_find(struct drm_gpuvm *gpuvm,
1581 struct drm_gem_object *obj)
1583 struct drm_gpuvm_bo *vm_bo = __drm_gpuvm_bo_find(gpuvm, obj);
1585 return vm_bo ? drm_gpuvm_bo_get(vm_bo) : NULL;
1587 EXPORT_SYMBOL_GPL(drm_gpuvm_bo_find);
1590 * drm_gpuvm_bo_obtain() - obtains and instance of the &drm_gpuvm_bo for the
1591 * given &drm_gpuvm and &drm_gem_object
1592 * @gpuvm: The &drm_gpuvm the @obj is mapped in.
1593 * @obj: The &drm_gem_object being mapped in the @gpuvm.
1595 * Find the &drm_gpuvm_bo representing the combination of the given
1596 * &drm_gpuvm and &drm_gem_object. If found, increases the reference
1597 * count of the &drm_gpuvm_bo accordingly. If not found, allocates a new
1600 * A new &drm_gpuvm_bo is added to the GEMs gpuva list.
1602 * Returns: a pointer to the &drm_gpuvm_bo on success, an ERR_PTR on failure
1604 struct drm_gpuvm_bo *
1605 drm_gpuvm_bo_obtain(struct drm_gpuvm *gpuvm,
1606 struct drm_gem_object *obj)
1608 struct drm_gpuvm_bo *vm_bo;
1610 vm_bo = drm_gpuvm_bo_find(gpuvm, obj);
1614 vm_bo = drm_gpuvm_bo_create(gpuvm, obj);
1616 return ERR_PTR(-ENOMEM);
1618 drm_gem_gpuva_assert_lock_held(obj);
1619 list_add_tail(&vm_bo->list.entry.gem, &obj->gpuva.list);
1623 EXPORT_SYMBOL_GPL(drm_gpuvm_bo_obtain);
1626 * drm_gpuvm_bo_obtain_prealloc() - obtains and instance of the &drm_gpuvm_bo
1627 * for the given &drm_gpuvm and &drm_gem_object
1628 * @__vm_bo: A pre-allocated struct drm_gpuvm_bo.
1630 * Find the &drm_gpuvm_bo representing the combination of the given
1631 * &drm_gpuvm and &drm_gem_object. If found, increases the reference
1632 * count of the found &drm_gpuvm_bo accordingly, while the @__vm_bo reference
1633 * count is decreased. If not found @__vm_bo is returned without further
1634 * increase of the reference count.
1636 * A new &drm_gpuvm_bo is added to the GEMs gpuva list.
1638 * Returns: a pointer to the found &drm_gpuvm_bo or @__vm_bo if no existing
1639 * &drm_gpuvm_bo was found
1641 struct drm_gpuvm_bo *
1642 drm_gpuvm_bo_obtain_prealloc(struct drm_gpuvm_bo *__vm_bo)
1644 struct drm_gpuvm *gpuvm = __vm_bo->vm;
1645 struct drm_gem_object *obj = __vm_bo->obj;
1646 struct drm_gpuvm_bo *vm_bo;
1648 vm_bo = drm_gpuvm_bo_find(gpuvm, obj);
1650 drm_gpuvm_bo_put(__vm_bo);
1654 drm_gem_gpuva_assert_lock_held(obj);
1655 list_add_tail(&__vm_bo->list.entry.gem, &obj->gpuva.list);
1659 EXPORT_SYMBOL_GPL(drm_gpuvm_bo_obtain_prealloc);
1662 * drm_gpuvm_bo_extobj_add() - adds the &drm_gpuvm_bo to its &drm_gpuvm's
1664 * @vm_bo: The &drm_gpuvm_bo to add to its &drm_gpuvm's the extobj list.
1666 * Adds the given @vm_bo to its &drm_gpuvm's extobj list if not on the list
1667 * already and if the corresponding &drm_gem_object is an external object,
1671 drm_gpuvm_bo_extobj_add(struct drm_gpuvm_bo *vm_bo)
1673 struct drm_gpuvm *gpuvm = vm_bo->vm;
1674 bool lock = !drm_gpuvm_resv_protected(gpuvm);
1677 drm_gpuvm_resv_assert_held(gpuvm);
1679 if (drm_gpuvm_is_extobj(gpuvm, vm_bo->obj))
1680 drm_gpuvm_bo_list_add(vm_bo, extobj, lock);
1682 EXPORT_SYMBOL_GPL(drm_gpuvm_bo_extobj_add);
1685 * drm_gpuvm_bo_evict() - add / remove a &drm_gpuvm_bo to / from the &drm_gpuvms
1687 * @vm_bo: the &drm_gpuvm_bo to add or remove
1688 * @evict: indicates whether the object is evicted
1690 * Adds a &drm_gpuvm_bo to or removes it from the &drm_gpuvms evicted list.
1693 drm_gpuvm_bo_evict(struct drm_gpuvm_bo *vm_bo, bool evict)
1695 struct drm_gpuvm *gpuvm = vm_bo->vm;
1696 struct drm_gem_object *obj = vm_bo->obj;
1697 bool lock = !drm_gpuvm_resv_protected(gpuvm);
1699 dma_resv_assert_held(obj->resv);
1700 vm_bo->evicted = evict;
1702 /* Can't add external objects to the evicted list directly if not using
1703 * internal spinlocks, since in this case the evicted list is protected
1704 * with the VM's common dma-resv lock.
1706 if (drm_gpuvm_is_extobj(gpuvm, obj) && !lock)
1710 drm_gpuvm_bo_list_add(vm_bo, evict, lock);
1712 drm_gpuvm_bo_list_del_init(vm_bo, evict, lock);
1714 EXPORT_SYMBOL_GPL(drm_gpuvm_bo_evict);
1717 __drm_gpuva_insert(struct drm_gpuvm *gpuvm,
1718 struct drm_gpuva *va)
1720 struct rb_node *node;
1721 struct list_head *head;
1723 if (drm_gpuva_it_iter_first(&gpuvm->rb.tree,
1730 drm_gpuva_it_insert(va, &gpuvm->rb.tree);
1732 node = rb_prev(&va->rb.node);
1734 head = &(to_drm_gpuva(node))->rb.entry;
1736 head = &gpuvm->rb.list;
1738 list_add(&va->rb.entry, head);
1744 * drm_gpuva_insert() - insert a &drm_gpuva
1745 * @gpuvm: the &drm_gpuvm to insert the &drm_gpuva in
1746 * @va: the &drm_gpuva to insert
1748 * Insert a &drm_gpuva with a given address and range into a
1751 * It is safe to use this function using the safe versions of iterating the GPU
1752 * VA space, such as drm_gpuvm_for_each_va_safe() and
1753 * drm_gpuvm_for_each_va_range_safe().
1755 * Returns: 0 on success, negative error code on failure.
1758 drm_gpuva_insert(struct drm_gpuvm *gpuvm,
1759 struct drm_gpuva *va)
1761 u64 addr = va->va.addr;
1762 u64 range = va->va.range;
1765 if (unlikely(!drm_gpuvm_range_valid(gpuvm, addr, range)))
1768 ret = __drm_gpuva_insert(gpuvm, va);
1770 /* Take a reference of the GPUVM for the successfully inserted
1771 * drm_gpuva. We can't take the reference in
1772 * __drm_gpuva_insert() itself, since we don't want to increse
1773 * the reference count for the GPUVM's kernel_alloc_node.
1775 drm_gpuvm_get(gpuvm);
1779 EXPORT_SYMBOL_GPL(drm_gpuva_insert);
1782 __drm_gpuva_remove(struct drm_gpuva *va)
1784 drm_gpuva_it_remove(va, &va->vm->rb.tree);
1785 list_del_init(&va->rb.entry);
1789 * drm_gpuva_remove() - remove a &drm_gpuva
1790 * @va: the &drm_gpuva to remove
1792 * This removes the given &va from the underlaying tree.
1794 * It is safe to use this function using the safe versions of iterating the GPU
1795 * VA space, such as drm_gpuvm_for_each_va_safe() and
1796 * drm_gpuvm_for_each_va_range_safe().
1799 drm_gpuva_remove(struct drm_gpuva *va)
1801 struct drm_gpuvm *gpuvm = va->vm;
1803 if (unlikely(va == &gpuvm->kernel_alloc_node)) {
1804 drm_WARN(gpuvm->drm, 1,
1805 "Can't destroy kernel reserved node.\n");
1809 __drm_gpuva_remove(va);
1810 drm_gpuvm_put(va->vm);
1812 EXPORT_SYMBOL_GPL(drm_gpuva_remove);
1815 * drm_gpuva_link() - link a &drm_gpuva
1816 * @va: the &drm_gpuva to link
1817 * @vm_bo: the &drm_gpuvm_bo to add the &drm_gpuva to
1819 * This adds the given &va to the GPU VA list of the &drm_gpuvm_bo and the
1820 * &drm_gpuvm_bo to the &drm_gem_object it is associated with.
1822 * For every &drm_gpuva entry added to the &drm_gpuvm_bo an additional
1823 * reference of the latter is taken.
1825 * This function expects the caller to protect the GEM's GPUVA list against
1826 * concurrent access using either the GEMs dma_resv lock or a driver specific
1827 * lock set through drm_gem_gpuva_set_lock().
1830 drm_gpuva_link(struct drm_gpuva *va, struct drm_gpuvm_bo *vm_bo)
1832 struct drm_gem_object *obj = va->gem.obj;
1833 struct drm_gpuvm *gpuvm = va->vm;
1838 drm_WARN_ON(gpuvm->drm, obj != vm_bo->obj);
1840 va->vm_bo = drm_gpuvm_bo_get(vm_bo);
1842 drm_gem_gpuva_assert_lock_held(obj);
1843 list_add_tail(&va->gem.entry, &vm_bo->list.gpuva);
1845 EXPORT_SYMBOL_GPL(drm_gpuva_link);
1848 * drm_gpuva_unlink() - unlink a &drm_gpuva
1849 * @va: the &drm_gpuva to unlink
1851 * This removes the given &va from the GPU VA list of the &drm_gem_object it is
1854 * This removes the given &va from the GPU VA list of the &drm_gpuvm_bo and
1855 * the &drm_gpuvm_bo from the &drm_gem_object it is associated with in case
1856 * this call unlinks the last &drm_gpuva from the &drm_gpuvm_bo.
1858 * For every &drm_gpuva entry removed from the &drm_gpuvm_bo a reference of
1859 * the latter is dropped.
1861 * This function expects the caller to protect the GEM's GPUVA list against
1862 * concurrent access using either the GEMs dma_resv lock or a driver specific
1863 * lock set through drm_gem_gpuva_set_lock().
1866 drm_gpuva_unlink(struct drm_gpuva *va)
1868 struct drm_gem_object *obj = va->gem.obj;
1869 struct drm_gpuvm_bo *vm_bo = va->vm_bo;
1874 drm_gem_gpuva_assert_lock_held(obj);
1875 list_del_init(&va->gem.entry);
1878 drm_gpuvm_bo_put(vm_bo);
1880 EXPORT_SYMBOL_GPL(drm_gpuva_unlink);
1883 * drm_gpuva_find_first() - find the first &drm_gpuva in the given range
1884 * @gpuvm: the &drm_gpuvm to search in
1885 * @addr: the &drm_gpuvas address
1886 * @range: the &drm_gpuvas range
1888 * Returns: the first &drm_gpuva within the given range
1891 drm_gpuva_find_first(struct drm_gpuvm *gpuvm,
1892 u64 addr, u64 range)
1894 u64 last = addr + range - 1;
1896 return drm_gpuva_it_iter_first(&gpuvm->rb.tree, addr, last);
1898 EXPORT_SYMBOL_GPL(drm_gpuva_find_first);
1901 * drm_gpuva_find() - find a &drm_gpuva
1902 * @gpuvm: the &drm_gpuvm to search in
1903 * @addr: the &drm_gpuvas address
1904 * @range: the &drm_gpuvas range
1906 * Returns: the &drm_gpuva at a given &addr and with a given &range
1909 drm_gpuva_find(struct drm_gpuvm *gpuvm,
1910 u64 addr, u64 range)
1912 struct drm_gpuva *va;
1914 va = drm_gpuva_find_first(gpuvm, addr, range);
1918 if (va->va.addr != addr ||
1919 va->va.range != range)
1927 EXPORT_SYMBOL_GPL(drm_gpuva_find);
1930 * drm_gpuva_find_prev() - find the &drm_gpuva before the given address
1931 * @gpuvm: the &drm_gpuvm to search in
1932 * @start: the given GPU VA's start address
1934 * Find the adjacent &drm_gpuva before the GPU VA with given &start address.
1936 * Note that if there is any free space between the GPU VA mappings no mapping
1939 * Returns: a pointer to the found &drm_gpuva or NULL if none was found
1942 drm_gpuva_find_prev(struct drm_gpuvm *gpuvm, u64 start)
1944 if (!drm_gpuvm_range_valid(gpuvm, start - 1, 1))
1947 return drm_gpuva_it_iter_first(&gpuvm->rb.tree, start - 1, start);
1949 EXPORT_SYMBOL_GPL(drm_gpuva_find_prev);
1952 * drm_gpuva_find_next() - find the &drm_gpuva after the given address
1953 * @gpuvm: the &drm_gpuvm to search in
1954 * @end: the given GPU VA's end address
1956 * Find the adjacent &drm_gpuva after the GPU VA with given &end address.
1958 * Note that if there is any free space between the GPU VA mappings no mapping
1961 * Returns: a pointer to the found &drm_gpuva or NULL if none was found
1964 drm_gpuva_find_next(struct drm_gpuvm *gpuvm, u64 end)
1966 if (!drm_gpuvm_range_valid(gpuvm, end, 1))
1969 return drm_gpuva_it_iter_first(&gpuvm->rb.tree, end, end + 1);
1971 EXPORT_SYMBOL_GPL(drm_gpuva_find_next);
1974 * drm_gpuvm_interval_empty() - indicate whether a given interval of the VA space
1976 * @gpuvm: the &drm_gpuvm to check the range for
1977 * @addr: the start address of the range
1978 * @range: the range of the interval
1980 * Returns: true if the interval is empty, false otherwise
1983 drm_gpuvm_interval_empty(struct drm_gpuvm *gpuvm, u64 addr, u64 range)
1985 return !drm_gpuva_find_first(gpuvm, addr, range);
1987 EXPORT_SYMBOL_GPL(drm_gpuvm_interval_empty);
1990 * drm_gpuva_map() - helper to insert a &drm_gpuva according to a
1992 * @gpuvm: the &drm_gpuvm
1993 * @va: the &drm_gpuva to insert
1994 * @op: the &drm_gpuva_op_map to initialize @va with
1996 * Initializes the @va from the @op and inserts it into the given @gpuvm.
1999 drm_gpuva_map(struct drm_gpuvm *gpuvm,
2000 struct drm_gpuva *va,
2001 struct drm_gpuva_op_map *op)
2003 drm_gpuva_init_from_op(va, op);
2004 drm_gpuva_insert(gpuvm, va);
2006 EXPORT_SYMBOL_GPL(drm_gpuva_map);
2009 * drm_gpuva_remap() - helper to remap a &drm_gpuva according to a
2010 * &drm_gpuva_op_remap
2011 * @prev: the &drm_gpuva to remap when keeping the start of a mapping
2012 * @next: the &drm_gpuva to remap when keeping the end of a mapping
2013 * @op: the &drm_gpuva_op_remap to initialize @prev and @next with
2015 * Removes the currently mapped &drm_gpuva and remaps it using @prev and/or
2019 drm_gpuva_remap(struct drm_gpuva *prev,
2020 struct drm_gpuva *next,
2021 struct drm_gpuva_op_remap *op)
2023 struct drm_gpuva *va = op->unmap->va;
2024 struct drm_gpuvm *gpuvm = va->vm;
2026 drm_gpuva_remove(va);
2029 drm_gpuva_init_from_op(prev, op->prev);
2030 drm_gpuva_insert(gpuvm, prev);
2034 drm_gpuva_init_from_op(next, op->next);
2035 drm_gpuva_insert(gpuvm, next);
2038 EXPORT_SYMBOL_GPL(drm_gpuva_remap);
2041 * drm_gpuva_unmap() - helper to remove a &drm_gpuva according to a
2042 * &drm_gpuva_op_unmap
2043 * @op: the &drm_gpuva_op_unmap specifying the &drm_gpuva to remove
2045 * Removes the &drm_gpuva associated with the &drm_gpuva_op_unmap.
2048 drm_gpuva_unmap(struct drm_gpuva_op_unmap *op)
2050 drm_gpuva_remove(op->va);
2052 EXPORT_SYMBOL_GPL(drm_gpuva_unmap);
2055 op_map_cb(const struct drm_gpuvm_ops *fn, void *priv,
2056 u64 addr, u64 range,
2057 struct drm_gem_object *obj, u64 offset)
2059 struct drm_gpuva_op op = {};
2061 op.op = DRM_GPUVA_OP_MAP;
2062 op.map.va.addr = addr;
2063 op.map.va.range = range;
2064 op.map.gem.obj = obj;
2065 op.map.gem.offset = offset;
2067 return fn->sm_step_map(&op, priv);
2071 op_remap_cb(const struct drm_gpuvm_ops *fn, void *priv,
2072 struct drm_gpuva_op_map *prev,
2073 struct drm_gpuva_op_map *next,
2074 struct drm_gpuva_op_unmap *unmap)
2076 struct drm_gpuva_op op = {};
2077 struct drm_gpuva_op_remap *r;
2079 op.op = DRM_GPUVA_OP_REMAP;
2085 return fn->sm_step_remap(&op, priv);
2089 op_unmap_cb(const struct drm_gpuvm_ops *fn, void *priv,
2090 struct drm_gpuva *va, bool merge)
2092 struct drm_gpuva_op op = {};
2094 op.op = DRM_GPUVA_OP_UNMAP;
2096 op.unmap.keep = merge;
2098 return fn->sm_step_unmap(&op, priv);
2102 __drm_gpuvm_sm_map(struct drm_gpuvm *gpuvm,
2103 const struct drm_gpuvm_ops *ops, void *priv,
2104 u64 req_addr, u64 req_range,
2105 struct drm_gem_object *req_obj, u64 req_offset)
2107 struct drm_gpuva *va, *next;
2108 u64 req_end = req_addr + req_range;
2111 if (unlikely(!drm_gpuvm_range_valid(gpuvm, req_addr, req_range)))
2114 drm_gpuvm_for_each_va_range_safe(va, next, gpuvm, req_addr, req_end) {
2115 struct drm_gem_object *obj = va->gem.obj;
2116 u64 offset = va->gem.offset;
2117 u64 addr = va->va.addr;
2118 u64 range = va->va.range;
2119 u64 end = addr + range;
2120 bool merge = !!va->gem.obj;
2122 if (addr == req_addr) {
2123 merge &= obj == req_obj &&
2124 offset == req_offset;
2126 if (end == req_end) {
2127 ret = op_unmap_cb(ops, priv, va, merge);
2133 if (end < req_end) {
2134 ret = op_unmap_cb(ops, priv, va, merge);
2140 if (end > req_end) {
2141 struct drm_gpuva_op_map n = {
2143 .va.range = range - req_range,
2145 .gem.offset = offset + req_range,
2147 struct drm_gpuva_op_unmap u = {
2152 ret = op_remap_cb(ops, priv, NULL, &n, &u);
2157 } else if (addr < req_addr) {
2158 u64 ls_range = req_addr - addr;
2159 struct drm_gpuva_op_map p = {
2161 .va.range = ls_range,
2163 .gem.offset = offset,
2165 struct drm_gpuva_op_unmap u = { .va = va };
2167 merge &= obj == req_obj &&
2168 offset + ls_range == req_offset;
2171 if (end == req_end) {
2172 ret = op_remap_cb(ops, priv, &p, NULL, &u);
2178 if (end < req_end) {
2179 ret = op_remap_cb(ops, priv, &p, NULL, &u);
2185 if (end > req_end) {
2186 struct drm_gpuva_op_map n = {
2188 .va.range = end - req_end,
2190 .gem.offset = offset + ls_range +
2194 ret = op_remap_cb(ops, priv, &p, &n, &u);
2199 } else if (addr > req_addr) {
2200 merge &= obj == req_obj &&
2201 offset == req_offset +
2204 if (end == req_end) {
2205 ret = op_unmap_cb(ops, priv, va, merge);
2211 if (end < req_end) {
2212 ret = op_unmap_cb(ops, priv, va, merge);
2218 if (end > req_end) {
2219 struct drm_gpuva_op_map n = {
2221 .va.range = end - req_end,
2223 .gem.offset = offset + req_end - addr,
2225 struct drm_gpuva_op_unmap u = {
2230 ret = op_remap_cb(ops, priv, NULL, &n, &u);
2238 return op_map_cb(ops, priv,
2239 req_addr, req_range,
2240 req_obj, req_offset);
2244 __drm_gpuvm_sm_unmap(struct drm_gpuvm *gpuvm,
2245 const struct drm_gpuvm_ops *ops, void *priv,
2246 u64 req_addr, u64 req_range)
2248 struct drm_gpuva *va, *next;
2249 u64 req_end = req_addr + req_range;
2252 if (unlikely(!drm_gpuvm_range_valid(gpuvm, req_addr, req_range)))
2255 drm_gpuvm_for_each_va_range_safe(va, next, gpuvm, req_addr, req_end) {
2256 struct drm_gpuva_op_map prev = {}, next = {};
2257 bool prev_split = false, next_split = false;
2258 struct drm_gem_object *obj = va->gem.obj;
2259 u64 offset = va->gem.offset;
2260 u64 addr = va->va.addr;
2261 u64 range = va->va.range;
2262 u64 end = addr + range;
2264 if (addr < req_addr) {
2265 prev.va.addr = addr;
2266 prev.va.range = req_addr - addr;
2268 prev.gem.offset = offset;
2273 if (end > req_end) {
2274 next.va.addr = req_end;
2275 next.va.range = end - req_end;
2277 next.gem.offset = offset + (req_end - addr);
2282 if (prev_split || next_split) {
2283 struct drm_gpuva_op_unmap unmap = { .va = va };
2285 ret = op_remap_cb(ops, priv,
2286 prev_split ? &prev : NULL,
2287 next_split ? &next : NULL,
2292 ret = op_unmap_cb(ops, priv, va, false);
2302 * drm_gpuvm_sm_map() - creates the &drm_gpuva_op split/merge steps
2303 * @gpuvm: the &drm_gpuvm representing the GPU VA space
2304 * @req_addr: the start address of the new mapping
2305 * @req_range: the range of the new mapping
2306 * @req_obj: the &drm_gem_object to map
2307 * @req_offset: the offset within the &drm_gem_object
2308 * @priv: pointer to a driver private data structure
2310 * This function iterates the given range of the GPU VA space. It utilizes the
2311 * &drm_gpuvm_ops to call back into the driver providing the split and merge
2314 * Drivers may use these callbacks to update the GPU VA space right away within
2315 * the callback. In case the driver decides to copy and store the operations for
2316 * later processing neither this function nor &drm_gpuvm_sm_unmap is allowed to
2317 * be called before the &drm_gpuvm's view of the GPU VA space was
2318 * updated with the previous set of operations. To update the
2319 * &drm_gpuvm's view of the GPU VA space drm_gpuva_insert(),
2320 * drm_gpuva_destroy_locked() and/or drm_gpuva_destroy_unlocked() should be
2323 * A sequence of callbacks can contain map, unmap and remap operations, but
2324 * the sequence of callbacks might also be empty if no operation is required,
2325 * e.g. if the requested mapping already exists in the exact same way.
2327 * There can be an arbitrary amount of unmap operations, a maximum of two remap
2328 * operations and a single map operation. The latter one represents the original
2329 * map operation requested by the caller.
2331 * Returns: 0 on success or a negative error code
2334 drm_gpuvm_sm_map(struct drm_gpuvm *gpuvm, void *priv,
2335 u64 req_addr, u64 req_range,
2336 struct drm_gem_object *req_obj, u64 req_offset)
2338 const struct drm_gpuvm_ops *ops = gpuvm->ops;
2340 if (unlikely(!(ops && ops->sm_step_map &&
2341 ops->sm_step_remap &&
2342 ops->sm_step_unmap)))
2345 return __drm_gpuvm_sm_map(gpuvm, ops, priv,
2346 req_addr, req_range,
2347 req_obj, req_offset);
2349 EXPORT_SYMBOL_GPL(drm_gpuvm_sm_map);
2352 * drm_gpuvm_sm_unmap() - creates the &drm_gpuva_ops to split on unmap
2353 * @gpuvm: the &drm_gpuvm representing the GPU VA space
2354 * @priv: pointer to a driver private data structure
2355 * @req_addr: the start address of the range to unmap
2356 * @req_range: the range of the mappings to unmap
2358 * This function iterates the given range of the GPU VA space. It utilizes the
2359 * &drm_gpuvm_ops to call back into the driver providing the operations to
2360 * unmap and, if required, split existent mappings.
2362 * Drivers may use these callbacks to update the GPU VA space right away within
2363 * the callback. In case the driver decides to copy and store the operations for
2364 * later processing neither this function nor &drm_gpuvm_sm_map is allowed to be
2365 * called before the &drm_gpuvm's view of the GPU VA space was updated
2366 * with the previous set of operations. To update the &drm_gpuvm's view
2367 * of the GPU VA space drm_gpuva_insert(), drm_gpuva_destroy_locked() and/or
2368 * drm_gpuva_destroy_unlocked() should be used.
2370 * A sequence of callbacks can contain unmap and remap operations, depending on
2371 * whether there are actual overlapping mappings to split.
2373 * There can be an arbitrary amount of unmap operations and a maximum of two
2376 * Returns: 0 on success or a negative error code
2379 drm_gpuvm_sm_unmap(struct drm_gpuvm *gpuvm, void *priv,
2380 u64 req_addr, u64 req_range)
2382 const struct drm_gpuvm_ops *ops = gpuvm->ops;
2384 if (unlikely(!(ops && ops->sm_step_remap &&
2385 ops->sm_step_unmap)))
2388 return __drm_gpuvm_sm_unmap(gpuvm, ops, priv,
2389 req_addr, req_range);
2391 EXPORT_SYMBOL_GPL(drm_gpuvm_sm_unmap);
2393 static struct drm_gpuva_op *
2394 gpuva_op_alloc(struct drm_gpuvm *gpuvm)
2396 const struct drm_gpuvm_ops *fn = gpuvm->ops;
2397 struct drm_gpuva_op *op;
2399 if (fn && fn->op_alloc)
2400 op = fn->op_alloc();
2402 op = kzalloc(sizeof(*op), GFP_KERNEL);
2411 gpuva_op_free(struct drm_gpuvm *gpuvm,
2412 struct drm_gpuva_op *op)
2414 const struct drm_gpuvm_ops *fn = gpuvm->ops;
2416 if (fn && fn->op_free)
2423 drm_gpuva_sm_step(struct drm_gpuva_op *__op,
2427 struct drm_gpuvm *vm;
2428 struct drm_gpuva_ops *ops;
2430 struct drm_gpuvm *gpuvm = args->vm;
2431 struct drm_gpuva_ops *ops = args->ops;
2432 struct drm_gpuva_op *op;
2434 op = gpuva_op_alloc(gpuvm);
2438 memcpy(op, __op, sizeof(*op));
2440 if (op->op == DRM_GPUVA_OP_REMAP) {
2441 struct drm_gpuva_op_remap *__r = &__op->remap;
2442 struct drm_gpuva_op_remap *r = &op->remap;
2444 r->unmap = kmemdup(__r->unmap, sizeof(*r->unmap),
2446 if (unlikely(!r->unmap))
2450 r->prev = kmemdup(__r->prev, sizeof(*r->prev),
2452 if (unlikely(!r->prev))
2453 goto err_free_unmap;
2457 r->next = kmemdup(__r->next, sizeof(*r->next),
2459 if (unlikely(!r->next))
2464 list_add_tail(&op->entry, &ops->list);
2469 kfree(op->remap.unmap);
2471 kfree(op->remap.prev);
2473 gpuva_op_free(gpuvm, op);
2478 static const struct drm_gpuvm_ops gpuvm_list_ops = {
2479 .sm_step_map = drm_gpuva_sm_step,
2480 .sm_step_remap = drm_gpuva_sm_step,
2481 .sm_step_unmap = drm_gpuva_sm_step,
2485 * drm_gpuvm_sm_map_ops_create() - creates the &drm_gpuva_ops to split and merge
2486 * @gpuvm: the &drm_gpuvm representing the GPU VA space
2487 * @req_addr: the start address of the new mapping
2488 * @req_range: the range of the new mapping
2489 * @req_obj: the &drm_gem_object to map
2490 * @req_offset: the offset within the &drm_gem_object
2492 * This function creates a list of operations to perform splitting and merging
2493 * of existent mapping(s) with the newly requested one.
2495 * The list can be iterated with &drm_gpuva_for_each_op and must be processed
2496 * in the given order. It can contain map, unmap and remap operations, but it
2497 * also can be empty if no operation is required, e.g. if the requested mapping
2498 * already exists is the exact same way.
2500 * There can be an arbitrary amount of unmap operations, a maximum of two remap
2501 * operations and a single map operation. The latter one represents the original
2502 * map operation requested by the caller.
2504 * Note that before calling this function again with another mapping request it
2505 * is necessary to update the &drm_gpuvm's view of the GPU VA space. The
2506 * previously obtained operations must be either processed or abandoned. To
2507 * update the &drm_gpuvm's view of the GPU VA space drm_gpuva_insert(),
2508 * drm_gpuva_destroy_locked() and/or drm_gpuva_destroy_unlocked() should be
2511 * After the caller finished processing the returned &drm_gpuva_ops, they must
2512 * be freed with &drm_gpuva_ops_free.
2514 * Returns: a pointer to the &drm_gpuva_ops on success, an ERR_PTR on failure
2516 struct drm_gpuva_ops *
2517 drm_gpuvm_sm_map_ops_create(struct drm_gpuvm *gpuvm,
2518 u64 req_addr, u64 req_range,
2519 struct drm_gem_object *req_obj, u64 req_offset)
2521 struct drm_gpuva_ops *ops;
2523 struct drm_gpuvm *vm;
2524 struct drm_gpuva_ops *ops;
2528 ops = kzalloc(sizeof(*ops), GFP_KERNEL);
2530 return ERR_PTR(-ENOMEM);
2532 INIT_LIST_HEAD(&ops->list);
2537 ret = __drm_gpuvm_sm_map(gpuvm, &gpuvm_list_ops, &args,
2538 req_addr, req_range,
2539 req_obj, req_offset);
2546 drm_gpuva_ops_free(gpuvm, ops);
2547 return ERR_PTR(ret);
2549 EXPORT_SYMBOL_GPL(drm_gpuvm_sm_map_ops_create);
2552 * drm_gpuvm_sm_unmap_ops_create() - creates the &drm_gpuva_ops to split on
2554 * @gpuvm: the &drm_gpuvm representing the GPU VA space
2555 * @req_addr: the start address of the range to unmap
2556 * @req_range: the range of the mappings to unmap
2558 * This function creates a list of operations to perform unmapping and, if
2559 * required, splitting of the mappings overlapping the unmap range.
2561 * The list can be iterated with &drm_gpuva_for_each_op and must be processed
2562 * in the given order. It can contain unmap and remap operations, depending on
2563 * whether there are actual overlapping mappings to split.
2565 * There can be an arbitrary amount of unmap operations and a maximum of two
2568 * Note that before calling this function again with another range to unmap it
2569 * is necessary to update the &drm_gpuvm's view of the GPU VA space. The
2570 * previously obtained operations must be processed or abandoned. To update the
2571 * &drm_gpuvm's view of the GPU VA space drm_gpuva_insert(),
2572 * drm_gpuva_destroy_locked() and/or drm_gpuva_destroy_unlocked() should be
2575 * After the caller finished processing the returned &drm_gpuva_ops, they must
2576 * be freed with &drm_gpuva_ops_free.
2578 * Returns: a pointer to the &drm_gpuva_ops on success, an ERR_PTR on failure
2580 struct drm_gpuva_ops *
2581 drm_gpuvm_sm_unmap_ops_create(struct drm_gpuvm *gpuvm,
2582 u64 req_addr, u64 req_range)
2584 struct drm_gpuva_ops *ops;
2586 struct drm_gpuvm *vm;
2587 struct drm_gpuva_ops *ops;
2591 ops = kzalloc(sizeof(*ops), GFP_KERNEL);
2593 return ERR_PTR(-ENOMEM);
2595 INIT_LIST_HEAD(&ops->list);
2600 ret = __drm_gpuvm_sm_unmap(gpuvm, &gpuvm_list_ops, &args,
2601 req_addr, req_range);
2608 drm_gpuva_ops_free(gpuvm, ops);
2609 return ERR_PTR(ret);
2611 EXPORT_SYMBOL_GPL(drm_gpuvm_sm_unmap_ops_create);
2614 * drm_gpuvm_prefetch_ops_create() - creates the &drm_gpuva_ops to prefetch
2615 * @gpuvm: the &drm_gpuvm representing the GPU VA space
2616 * @addr: the start address of the range to prefetch
2617 * @range: the range of the mappings to prefetch
2619 * This function creates a list of operations to perform prefetching.
2621 * The list can be iterated with &drm_gpuva_for_each_op and must be processed
2622 * in the given order. It can contain prefetch operations.
2624 * There can be an arbitrary amount of prefetch operations.
2626 * After the caller finished processing the returned &drm_gpuva_ops, they must
2627 * be freed with &drm_gpuva_ops_free.
2629 * Returns: a pointer to the &drm_gpuva_ops on success, an ERR_PTR on failure
2631 struct drm_gpuva_ops *
2632 drm_gpuvm_prefetch_ops_create(struct drm_gpuvm *gpuvm,
2633 u64 addr, u64 range)
2635 struct drm_gpuva_ops *ops;
2636 struct drm_gpuva_op *op;
2637 struct drm_gpuva *va;
2638 u64 end = addr + range;
2641 ops = kzalloc(sizeof(*ops), GFP_KERNEL);
2643 return ERR_PTR(-ENOMEM);
2645 INIT_LIST_HEAD(&ops->list);
2647 drm_gpuvm_for_each_va_range(va, gpuvm, addr, end) {
2648 op = gpuva_op_alloc(gpuvm);
2654 op->op = DRM_GPUVA_OP_PREFETCH;
2655 op->prefetch.va = va;
2656 list_add_tail(&op->entry, &ops->list);
2662 drm_gpuva_ops_free(gpuvm, ops);
2663 return ERR_PTR(ret);
2665 EXPORT_SYMBOL_GPL(drm_gpuvm_prefetch_ops_create);
2668 * drm_gpuvm_bo_unmap_ops_create() - creates the &drm_gpuva_ops to unmap a GEM
2669 * @vm_bo: the &drm_gpuvm_bo abstraction
2671 * This function creates a list of operations to perform unmapping for every
2672 * GPUVA attached to a GEM.
2674 * The list can be iterated with &drm_gpuva_for_each_op and consists out of an
2675 * arbitrary amount of unmap operations.
2677 * After the caller finished processing the returned &drm_gpuva_ops, they must
2678 * be freed with &drm_gpuva_ops_free.
2680 * It is the callers responsibility to protect the GEMs GPUVA list against
2681 * concurrent access using the GEMs dma_resv lock.
2683 * Returns: a pointer to the &drm_gpuva_ops on success, an ERR_PTR on failure
2685 struct drm_gpuva_ops *
2686 drm_gpuvm_bo_unmap_ops_create(struct drm_gpuvm_bo *vm_bo)
2688 struct drm_gpuva_ops *ops;
2689 struct drm_gpuva_op *op;
2690 struct drm_gpuva *va;
2693 drm_gem_gpuva_assert_lock_held(vm_bo->obj);
2695 ops = kzalloc(sizeof(*ops), GFP_KERNEL);
2697 return ERR_PTR(-ENOMEM);
2699 INIT_LIST_HEAD(&ops->list);
2701 drm_gpuvm_bo_for_each_va(va, vm_bo) {
2702 op = gpuva_op_alloc(vm_bo->vm);
2708 op->op = DRM_GPUVA_OP_UNMAP;
2710 list_add_tail(&op->entry, &ops->list);
2716 drm_gpuva_ops_free(vm_bo->vm, ops);
2717 return ERR_PTR(ret);
2719 EXPORT_SYMBOL_GPL(drm_gpuvm_bo_unmap_ops_create);
2722 * drm_gpuva_ops_free() - free the given &drm_gpuva_ops
2723 * @gpuvm: the &drm_gpuvm the ops were created for
2724 * @ops: the &drm_gpuva_ops to free
2726 * Frees the given &drm_gpuva_ops structure including all the ops associated
2730 drm_gpuva_ops_free(struct drm_gpuvm *gpuvm,
2731 struct drm_gpuva_ops *ops)
2733 struct drm_gpuva_op *op, *next;
2735 drm_gpuva_for_each_op_safe(op, next, ops) {
2736 list_del(&op->entry);
2738 if (op->op == DRM_GPUVA_OP_REMAP) {
2739 kfree(op->remap.prev);
2740 kfree(op->remap.next);
2741 kfree(op->remap.unmap);
2744 gpuva_op_free(gpuvm, op);
2749 EXPORT_SYMBOL_GPL(drm_gpuva_ops_free);
2751 MODULE_DESCRIPTION("DRM GPUVM");
2752 MODULE_LICENSE("GPL");