1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #ifndef __DRM_GPUVA_MGR_H__
4 #define __DRM_GPUVA_MGR_H__
7 * Copyright (c) 2022 Red Hat.
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
23 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 * OTHER DEALINGS IN THE SOFTWARE.
28 #include <linux/list.h>
29 #include <linux/rbtree.h>
30 #include <linux/types.h>
32 #include <drm/drm_gem.h>
34 struct drm_gpuva_manager;
35 struct drm_gpuva_fn_ops;
38 * enum drm_gpuva_flags - flags for struct drm_gpuva
40 enum drm_gpuva_flags {
42 * @DRM_GPUVA_INVALIDATED:
44 * Flag indicating that the &drm_gpuva's backing GEM is invalidated.
46 DRM_GPUVA_INVALIDATED = (1 << 0),
51 * Flag indicating that the &drm_gpuva is a sparse mapping.
53 DRM_GPUVA_SPARSE = (1 << 1),
56 * @DRM_GPUVA_USERBITS: user defined bits
58 DRM_GPUVA_USERBITS = (1 << 2),
62 * struct drm_gpuva - structure to track a GPU VA mapping
64 * This structure represents a GPU VA mapping and is associated with a
67 * Typically, this structure is embedded in bigger driver structures.
71 * @mgr: the &drm_gpuva_manager this object is associated with
73 struct drm_gpuva_manager *mgr;
76 * @flags: the &drm_gpuva_flags for this mapping
78 enum drm_gpuva_flags flags;
81 * @va: structure containing the address and range of the &drm_gpuva
85 * @addr: the start address
96 * @gem: structure containing the &drm_gem_object and it's offset
100 * @offset: the offset within the &drm_gem_object
105 * @obj: the mapped &drm_gem_object
107 struct drm_gem_object *obj;
110 * @entry: the &list_head to attach this object to a &drm_gem_object
112 struct list_head entry;
116 * @rb: structure containing data to store &drm_gpuvas in a rb-tree
120 * @rb: the rb-tree node
125 * @entry: The &list_head to additionally connect &drm_gpuvas
126 * in the same order they appear in the interval tree. This is
127 * useful to keep iterating &drm_gpuvas from a start node found
128 * through the rb-tree while doing modifications on the rb-tree
131 struct list_head entry;
134 * @__subtree_last: needed by the interval tree, holding last-in-subtree
140 int drm_gpuva_insert(struct drm_gpuva_manager *mgr, struct drm_gpuva *va);
141 void drm_gpuva_remove(struct drm_gpuva *va);
143 void drm_gpuva_link(struct drm_gpuva *va);
144 void drm_gpuva_unlink(struct drm_gpuva *va);
146 struct drm_gpuva *drm_gpuva_find(struct drm_gpuva_manager *mgr,
147 u64 addr, u64 range);
148 struct drm_gpuva *drm_gpuva_find_first(struct drm_gpuva_manager *mgr,
149 u64 addr, u64 range);
150 struct drm_gpuva *drm_gpuva_find_prev(struct drm_gpuva_manager *mgr, u64 start);
151 struct drm_gpuva *drm_gpuva_find_next(struct drm_gpuva_manager *mgr, u64 end);
153 bool drm_gpuva_interval_empty(struct drm_gpuva_manager *mgr, u64 addr, u64 range);
155 static inline void drm_gpuva_init(struct drm_gpuva *va, u64 addr, u64 range,
156 struct drm_gem_object *obj, u64 offset)
159 va->va.range = range;
161 va->gem.offset = offset;
165 * drm_gpuva_invalidate() - sets whether the backing GEM of this &drm_gpuva is
167 * @va: the &drm_gpuva to set the invalidate flag for
168 * @invalidate: indicates whether the &drm_gpuva is invalidated
170 static inline void drm_gpuva_invalidate(struct drm_gpuva *va, bool invalidate)
173 va->flags |= DRM_GPUVA_INVALIDATED;
175 va->flags &= ~DRM_GPUVA_INVALIDATED;
179 * drm_gpuva_invalidated() - indicates whether the backing BO of this &drm_gpuva
181 * @va: the &drm_gpuva to check
183 static inline bool drm_gpuva_invalidated(struct drm_gpuva *va)
185 return va->flags & DRM_GPUVA_INVALIDATED;
189 * struct drm_gpuva_manager - DRM GPU VA Manager
191 * The DRM GPU VA Manager keeps track of a GPU's virtual address space by using
192 * &maple_tree structures. Typically, this structure is embedded in bigger
195 * Drivers can pass addresses and ranges in an arbitrary unit, e.g. bytes or
198 * There should be one manager instance per GPU virtual address space.
200 struct drm_gpuva_manager {
202 * @name: the name of the DRM GPU VA space
207 * @mm_start: start of the VA space
212 * @mm_range: length of the VA space
217 * @rb: structures to track &drm_gpuva entries
221 * @tree: the rb-tree to track GPU VA mappings
223 struct rb_root_cached tree;
226 * @list: the &list_head to track GPU VA mappings
228 struct list_head list;
232 * @kernel_alloc_node:
234 * &drm_gpuva representing the address space cutout reserved for
237 struct drm_gpuva kernel_alloc_node;
240 * @ops: &drm_gpuva_fn_ops providing the split/merge steps to drivers
242 const struct drm_gpuva_fn_ops *ops;
245 void drm_gpuva_manager_init(struct drm_gpuva_manager *mgr,
247 u64 start_offset, u64 range,
248 u64 reserve_offset, u64 reserve_range,
249 const struct drm_gpuva_fn_ops *ops);
250 void drm_gpuva_manager_destroy(struct drm_gpuva_manager *mgr);
252 static inline struct drm_gpuva *
253 __drm_gpuva_next(struct drm_gpuva *va)
255 if (va && !list_is_last(&va->rb.entry, &va->mgr->rb.list))
256 return list_next_entry(va, rb.entry);
262 * drm_gpuva_for_each_va_range() - iterate over a range of &drm_gpuvas
263 * @va__: &drm_gpuva structure to assign to in each iteration step
264 * @mgr__: &drm_gpuva_manager to walk over
265 * @start__: starting offset, the first gpuva will overlap this
266 * @end__: ending offset, the last gpuva will start before this (but may
269 * This iterator walks over all &drm_gpuvas in the &drm_gpuva_manager that lie
270 * between @start__ and @end__. It is implemented similarly to list_for_each(),
271 * but is using the &drm_gpuva_manager's internal interval tree to accelerate
272 * the search for the starting &drm_gpuva, and hence isn't safe against removal
273 * of elements. It assumes that @end__ is within (or is the upper limit of) the
274 * &drm_gpuva_manager. This iterator does not skip over the &drm_gpuva_manager's
275 * @kernel_alloc_node.
277 #define drm_gpuva_for_each_va_range(va__, mgr__, start__, end__) \
278 for (va__ = drm_gpuva_find_first((mgr__), (start__), (end__) - (start__)); \
279 va__ && (va__->va.addr < (end__)); \
280 va__ = __drm_gpuva_next(va__))
283 * drm_gpuva_for_each_va_range_safe() - safely iterate over a range of
285 * @va__: &drm_gpuva to assign to in each iteration step
286 * @next__: another &drm_gpuva to use as temporary storage
287 * @mgr__: &drm_gpuva_manager to walk over
288 * @start__: starting offset, the first gpuva will overlap this
289 * @end__: ending offset, the last gpuva will start before this (but may
292 * This iterator walks over all &drm_gpuvas in the &drm_gpuva_manager that lie
293 * between @start__ and @end__. It is implemented similarly to
294 * list_for_each_safe(), but is using the &drm_gpuva_manager's internal interval
295 * tree to accelerate the search for the starting &drm_gpuva, and hence is safe
296 * against removal of elements. It assumes that @end__ is within (or is the
297 * upper limit of) the &drm_gpuva_manager. This iterator does not skip over the
298 * &drm_gpuva_manager's @kernel_alloc_node.
300 #define drm_gpuva_for_each_va_range_safe(va__, next__, mgr__, start__, end__) \
301 for (va__ = drm_gpuva_find_first((mgr__), (start__), (end__) - (start__)), \
302 next__ = __drm_gpuva_next(va__); \
303 va__ && (va__->va.addr < (end__)); \
304 va__ = next__, next__ = __drm_gpuva_next(va__))
307 * drm_gpuva_for_each_va() - iterate over all &drm_gpuvas
308 * @va__: &drm_gpuva to assign to in each iteration step
309 * @mgr__: &drm_gpuva_manager to walk over
311 * This iterator walks over all &drm_gpuva structures associated with the given
312 * &drm_gpuva_manager.
314 #define drm_gpuva_for_each_va(va__, mgr__) \
315 list_for_each_entry(va__, &(mgr__)->rb.list, rb.entry)
318 * drm_gpuva_for_each_va_safe() - safely iterate over all &drm_gpuvas
319 * @va__: &drm_gpuva to assign to in each iteration step
320 * @next__: another &drm_gpuva to use as temporary storage
321 * @mgr__: &drm_gpuva_manager to walk over
323 * This iterator walks over all &drm_gpuva structures associated with the given
324 * &drm_gpuva_manager. It is implemented with list_for_each_entry_safe(), and
325 * hence safe against the removal of elements.
327 #define drm_gpuva_for_each_va_safe(va__, next__, mgr__) \
328 list_for_each_entry_safe(va__, next__, &(mgr__)->rb.list, rb.entry)
331 * enum drm_gpuva_op_type - GPU VA operation type
333 * Operations to alter the GPU VA mappings tracked by the &drm_gpuva_manager.
335 enum drm_gpuva_op_type {
337 * @DRM_GPUVA_OP_MAP: the map op type
342 * @DRM_GPUVA_OP_REMAP: the remap op type
347 * @DRM_GPUVA_OP_UNMAP: the unmap op type
352 * @DRM_GPUVA_OP_PREFETCH: the prefetch op type
354 DRM_GPUVA_OP_PREFETCH,
358 * struct drm_gpuva_op_map - GPU VA map operation
360 * This structure represents a single map operation generated by the
361 * DRM GPU VA manager.
363 struct drm_gpuva_op_map {
365 * @va: structure containing address and range of a map
370 * @addr: the base address of the new mapping
375 * @range: the range of the new mapping
381 * @gem: structure containing the &drm_gem_object and it's offset
385 * @offset: the offset within the &drm_gem_object
390 * @obj: the &drm_gem_object to map
392 struct drm_gem_object *obj;
397 * struct drm_gpuva_op_unmap - GPU VA unmap operation
399 * This structure represents a single unmap operation generated by the
400 * DRM GPU VA manager.
402 struct drm_gpuva_op_unmap {
404 * @va: the &drm_gpuva to unmap
406 struct drm_gpuva *va;
411 * Indicates whether this &drm_gpuva is physically contiguous with the
412 * original mapping request.
414 * Optionally, if &keep is set, drivers may keep the actual page table
415 * mappings for this &drm_gpuva, adding the missing page table entries
416 * only and update the &drm_gpuva_manager accordingly.
422 * struct drm_gpuva_op_remap - GPU VA remap operation
424 * This represents a single remap operation generated by the DRM GPU VA manager.
426 * A remap operation is generated when an existing GPU VA mmapping is split up
427 * by inserting a new GPU VA mapping or by partially unmapping existent
428 * mapping(s), hence it consists of a maximum of two map and one unmap
431 * The @unmap operation takes care of removing the original existing mapping.
432 * @prev is used to remap the preceding part, @next the subsequent part.
434 * If either a new mapping's start address is aligned with the start address
435 * of the old mapping or the new mapping's end address is aligned with the
436 * end address of the old mapping, either @prev or @next is NULL.
438 * Note, the reason for a dedicated remap operation, rather than arbitrary
439 * unmap and map operations, is to give drivers the chance of extracting driver
440 * specific data for creating the new mappings from the unmap operations's
441 * &drm_gpuva structure which typically is embedded in larger driver specific
444 struct drm_gpuva_op_remap {
446 * @prev: the preceding part of a split mapping
448 struct drm_gpuva_op_map *prev;
451 * @next: the subsequent part of a split mapping
453 struct drm_gpuva_op_map *next;
456 * @unmap: the unmap operation for the original existing mapping
458 struct drm_gpuva_op_unmap *unmap;
462 * struct drm_gpuva_op_prefetch - GPU VA prefetch operation
464 * This structure represents a single prefetch operation generated by the
465 * DRM GPU VA manager.
467 struct drm_gpuva_op_prefetch {
469 * @va: the &drm_gpuva to prefetch
471 struct drm_gpuva *va;
475 * struct drm_gpuva_op - GPU VA operation
477 * This structure represents a single generic operation.
479 * The particular type of the operation is defined by @op.
481 struct drm_gpuva_op {
485 * The &list_head used to distribute instances of this struct within
488 struct list_head entry;
491 * @op: the type of the operation
493 enum drm_gpuva_op_type op;
497 * @map: the map operation
499 struct drm_gpuva_op_map map;
502 * @remap: the remap operation
504 struct drm_gpuva_op_remap remap;
507 * @unmap: the unmap operation
509 struct drm_gpuva_op_unmap unmap;
512 * @prefetch: the prefetch operation
514 struct drm_gpuva_op_prefetch prefetch;
519 * struct drm_gpuva_ops - wraps a list of &drm_gpuva_op
521 struct drm_gpuva_ops {
523 * @list: the &list_head
525 struct list_head list;
529 * drm_gpuva_for_each_op() - iterator to walk over &drm_gpuva_ops
530 * @op: &drm_gpuva_op to assign in each iteration step
531 * @ops: &drm_gpuva_ops to walk
533 * This iterator walks over all ops within a given list of operations.
535 #define drm_gpuva_for_each_op(op, ops) list_for_each_entry(op, &(ops)->list, entry)
538 * drm_gpuva_for_each_op_safe() - iterator to safely walk over &drm_gpuva_ops
539 * @op: &drm_gpuva_op to assign in each iteration step
540 * @next: &next &drm_gpuva_op to store the next step
541 * @ops: &drm_gpuva_ops to walk
543 * This iterator walks over all ops within a given list of operations. It is
544 * implemented with list_for_each_safe(), so save against removal of elements.
546 #define drm_gpuva_for_each_op_safe(op, next, ops) \
547 list_for_each_entry_safe(op, next, &(ops)->list, entry)
550 * drm_gpuva_for_each_op_from_reverse() - iterate backwards from the given point
551 * @op: &drm_gpuva_op to assign in each iteration step
552 * @ops: &drm_gpuva_ops to walk
554 * This iterator walks over all ops within a given list of operations beginning
555 * from the given operation in reverse order.
557 #define drm_gpuva_for_each_op_from_reverse(op, ops) \
558 list_for_each_entry_from_reverse(op, &(ops)->list, entry)
561 * drm_gpuva_first_op() - returns the first &drm_gpuva_op from &drm_gpuva_ops
562 * @ops: the &drm_gpuva_ops to get the fist &drm_gpuva_op from
564 #define drm_gpuva_first_op(ops) \
565 list_first_entry(&(ops)->list, struct drm_gpuva_op, entry)
568 * drm_gpuva_last_op() - returns the last &drm_gpuva_op from &drm_gpuva_ops
569 * @ops: the &drm_gpuva_ops to get the last &drm_gpuva_op from
571 #define drm_gpuva_last_op(ops) \
572 list_last_entry(&(ops)->list, struct drm_gpuva_op, entry)
575 * drm_gpuva_prev_op() - previous &drm_gpuva_op in the list
576 * @op: the current &drm_gpuva_op
578 #define drm_gpuva_prev_op(op) list_prev_entry(op, entry)
581 * drm_gpuva_next_op() - next &drm_gpuva_op in the list
582 * @op: the current &drm_gpuva_op
584 #define drm_gpuva_next_op(op) list_next_entry(op, entry)
586 struct drm_gpuva_ops *
587 drm_gpuva_sm_map_ops_create(struct drm_gpuva_manager *mgr,
589 struct drm_gem_object *obj, u64 offset);
590 struct drm_gpuva_ops *
591 drm_gpuva_sm_unmap_ops_create(struct drm_gpuva_manager *mgr,
592 u64 addr, u64 range);
594 struct drm_gpuva_ops *
595 drm_gpuva_prefetch_ops_create(struct drm_gpuva_manager *mgr,
596 u64 addr, u64 range);
598 struct drm_gpuva_ops *
599 drm_gpuva_gem_unmap_ops_create(struct drm_gpuva_manager *mgr,
600 struct drm_gem_object *obj);
602 void drm_gpuva_ops_free(struct drm_gpuva_manager *mgr,
603 struct drm_gpuva_ops *ops);
605 static inline void drm_gpuva_init_from_op(struct drm_gpuva *va,
606 struct drm_gpuva_op_map *op)
608 drm_gpuva_init(va, op->va.addr, op->va.range,
609 op->gem.obj, op->gem.offset);
613 * struct drm_gpuva_fn_ops - callbacks for split/merge steps
615 * This structure defines the callbacks used by &drm_gpuva_sm_map and
616 * &drm_gpuva_sm_unmap to provide the split/merge steps for map and unmap
617 * operations to drivers.
619 struct drm_gpuva_fn_ops {
621 * @op_alloc: called when the &drm_gpuva_manager allocates
622 * a struct drm_gpuva_op
624 * Some drivers may want to embed struct drm_gpuva_op into driver
625 * specific structures. By implementing this callback drivers can
626 * allocate memory accordingly.
628 * This callback is optional.
630 struct drm_gpuva_op *(*op_alloc)(void);
633 * @op_free: called when the &drm_gpuva_manager frees a
634 * struct drm_gpuva_op
636 * Some drivers may want to embed struct drm_gpuva_op into driver
637 * specific structures. By implementing this callback drivers can
638 * free the previously allocated memory accordingly.
640 * This callback is optional.
642 void (*op_free)(struct drm_gpuva_op *op);
645 * @sm_step_map: called from &drm_gpuva_sm_map to finally insert the
646 * mapping once all previous steps were completed
648 * The &priv pointer matches the one the driver passed to
649 * &drm_gpuva_sm_map or &drm_gpuva_sm_unmap, respectively.
651 * Can be NULL if &drm_gpuva_sm_map is used.
653 int (*sm_step_map)(struct drm_gpuva_op *op, void *priv);
656 * @sm_step_remap: called from &drm_gpuva_sm_map and
657 * &drm_gpuva_sm_unmap to split up an existent mapping
659 * This callback is called when existent mapping needs to be split up.
660 * This is the case when either a newly requested mapping overlaps or
661 * is enclosed by an existent mapping or a partial unmap of an existent
662 * mapping is requested.
664 * The &priv pointer matches the one the driver passed to
665 * &drm_gpuva_sm_map or &drm_gpuva_sm_unmap, respectively.
667 * Can be NULL if neither &drm_gpuva_sm_map nor &drm_gpuva_sm_unmap is
670 int (*sm_step_remap)(struct drm_gpuva_op *op, void *priv);
673 * @sm_step_unmap: called from &drm_gpuva_sm_map and
674 * &drm_gpuva_sm_unmap to unmap an existent mapping
676 * This callback is called when existent mapping needs to be unmapped.
677 * This is the case when either a newly requested mapping encloses an
678 * existent mapping or an unmap of an existent mapping is requested.
680 * The &priv pointer matches the one the driver passed to
681 * &drm_gpuva_sm_map or &drm_gpuva_sm_unmap, respectively.
683 * Can be NULL if neither &drm_gpuva_sm_map nor &drm_gpuva_sm_unmap is
686 int (*sm_step_unmap)(struct drm_gpuva_op *op, void *priv);
689 int drm_gpuva_sm_map(struct drm_gpuva_manager *mgr, void *priv,
691 struct drm_gem_object *obj, u64 offset);
693 int drm_gpuva_sm_unmap(struct drm_gpuva_manager *mgr, void *priv,
694 u64 addr, u64 range);
696 void drm_gpuva_map(struct drm_gpuva_manager *mgr,
697 struct drm_gpuva *va,
698 struct drm_gpuva_op_map *op);
700 void drm_gpuva_remap(struct drm_gpuva *prev,
701 struct drm_gpuva *next,
702 struct drm_gpuva_op_remap *op);
704 void drm_gpuva_unmap(struct drm_gpuva_op_unmap *op);
706 #endif /* __DRM_GPUVA_MGR_H__ */