1 /* SPDX-License-Identifier: GPL-2.0 OR MIT */
3 #ifndef __DRM_GPUVM_H__
4 #define __DRM_GPUVM_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_device.h>
33 #include <drm/drm_gem.h>
39 * enum drm_gpuva_flags - flags for struct drm_gpuva
41 enum drm_gpuva_flags {
43 * @DRM_GPUVA_INVALIDATED:
45 * Flag indicating that the &drm_gpuva's backing GEM is invalidated.
47 DRM_GPUVA_INVALIDATED = (1 << 0),
52 * Flag indicating that the &drm_gpuva is a sparse mapping.
54 DRM_GPUVA_SPARSE = (1 << 1),
57 * @DRM_GPUVA_USERBITS: user defined bits
59 DRM_GPUVA_USERBITS = (1 << 2),
63 * struct drm_gpuva - structure to track a GPU VA mapping
65 * This structure represents a GPU VA mapping and is associated with a
68 * Typically, this structure is embedded in bigger driver structures.
72 * @vm: the &drm_gpuvm this object is associated with
77 * @flags: the &drm_gpuva_flags for this mapping
79 enum drm_gpuva_flags flags;
82 * @va: structure containing the address and range of the &drm_gpuva
86 * @addr: the start address
97 * @gem: structure containing the &drm_gem_object and it's offset
101 * @offset: the offset within the &drm_gem_object
106 * @obj: the mapped &drm_gem_object
108 struct drm_gem_object *obj;
111 * @entry: the &list_head to attach this object to a &drm_gem_object
113 struct list_head entry;
117 * @rb: structure containing data to store &drm_gpuvas in a rb-tree
121 * @rb: the rb-tree node
126 * @entry: The &list_head to additionally connect &drm_gpuvas
127 * in the same order they appear in the interval tree. This is
128 * useful to keep iterating &drm_gpuvas from a start node found
129 * through the rb-tree while doing modifications on the rb-tree
132 struct list_head entry;
135 * @__subtree_last: needed by the interval tree, holding last-in-subtree
141 int drm_gpuva_insert(struct drm_gpuvm *gpuvm, struct drm_gpuva *va);
142 void drm_gpuva_remove(struct drm_gpuva *va);
144 void drm_gpuva_link(struct drm_gpuva *va);
145 void drm_gpuva_unlink(struct drm_gpuva *va);
147 struct drm_gpuva *drm_gpuva_find(struct drm_gpuvm *gpuvm,
148 u64 addr, u64 range);
149 struct drm_gpuva *drm_gpuva_find_first(struct drm_gpuvm *gpuvm,
150 u64 addr, u64 range);
151 struct drm_gpuva *drm_gpuva_find_prev(struct drm_gpuvm *gpuvm, u64 start);
152 struct drm_gpuva *drm_gpuva_find_next(struct drm_gpuvm *gpuvm, u64 end);
154 static inline void drm_gpuva_init(struct drm_gpuva *va, u64 addr, u64 range,
155 struct drm_gem_object *obj, u64 offset)
158 va->va.range = range;
160 va->gem.offset = offset;
164 * drm_gpuva_invalidate() - sets whether the backing GEM of this &drm_gpuva is
166 * @va: the &drm_gpuva to set the invalidate flag for
167 * @invalidate: indicates whether the &drm_gpuva is invalidated
169 static inline void drm_gpuva_invalidate(struct drm_gpuva *va, bool invalidate)
172 va->flags |= DRM_GPUVA_INVALIDATED;
174 va->flags &= ~DRM_GPUVA_INVALIDATED;
178 * drm_gpuva_invalidated() - indicates whether the backing BO of this &drm_gpuva
180 * @va: the &drm_gpuva to check
182 static inline bool drm_gpuva_invalidated(struct drm_gpuva *va)
184 return va->flags & DRM_GPUVA_INVALIDATED;
188 * struct drm_gpuvm - DRM GPU VA Manager
190 * The DRM GPU VA Manager keeps track of a GPU's virtual address space by using
191 * &maple_tree structures. Typically, this structure is embedded in bigger
194 * Drivers can pass addresses and ranges in an arbitrary unit, e.g. bytes or
197 * There should be one manager instance per GPU virtual address space.
201 * @name: the name of the DRM GPU VA space
206 * @drm: the &drm_device this VM lives in
208 struct drm_device *drm;
211 * @mm_start: start of the VA space
216 * @mm_range: length of the VA space
221 * @rb: structures to track &drm_gpuva entries
225 * @tree: the rb-tree to track GPU VA mappings
227 struct rb_root_cached tree;
230 * @list: the &list_head to track GPU VA mappings
232 struct list_head list;
236 * @kernel_alloc_node:
238 * &drm_gpuva representing the address space cutout reserved for
241 struct drm_gpuva kernel_alloc_node;
244 * @ops: &drm_gpuvm_ops providing the split/merge steps to drivers
246 const struct drm_gpuvm_ops *ops;
249 void drm_gpuvm_init(struct drm_gpuvm *gpuvm, const char *name,
250 struct drm_device *drm,
251 u64 start_offset, u64 range,
252 u64 reserve_offset, u64 reserve_range,
253 const struct drm_gpuvm_ops *ops);
254 void drm_gpuvm_destroy(struct drm_gpuvm *gpuvm);
256 bool drm_gpuvm_range_valid(struct drm_gpuvm *gpuvm, u64 addr, u64 range);
257 bool drm_gpuvm_interval_empty(struct drm_gpuvm *gpuvm, u64 addr, u64 range);
259 static inline struct drm_gpuva *
260 __drm_gpuva_next(struct drm_gpuva *va)
262 if (va && !list_is_last(&va->rb.entry, &va->vm->rb.list))
263 return list_next_entry(va, rb.entry);
269 * drm_gpuvm_for_each_va_range() - iterate over a range of &drm_gpuvas
270 * @va__: &drm_gpuva structure to assign to in each iteration step
271 * @gpuvm__: &drm_gpuvm to walk over
272 * @start__: starting offset, the first gpuva will overlap this
273 * @end__: ending offset, the last gpuva will start before this (but may
276 * This iterator walks over all &drm_gpuvas in the &drm_gpuvm that lie
277 * between @start__ and @end__. It is implemented similarly to list_for_each(),
278 * but is using the &drm_gpuvm's internal interval tree to accelerate
279 * the search for the starting &drm_gpuva, and hence isn't safe against removal
280 * of elements. It assumes that @end__ is within (or is the upper limit of) the
281 * &drm_gpuvm. This iterator does not skip over the &drm_gpuvm's
282 * @kernel_alloc_node.
284 #define drm_gpuvm_for_each_va_range(va__, gpuvm__, start__, end__) \
285 for (va__ = drm_gpuva_find_first((gpuvm__), (start__), (end__) - (start__)); \
286 va__ && (va__->va.addr < (end__)); \
287 va__ = __drm_gpuva_next(va__))
290 * drm_gpuvm_for_each_va_range_safe() - safely iterate over a range of
292 * @va__: &drm_gpuva to assign to in each iteration step
293 * @next__: another &drm_gpuva to use as temporary storage
294 * @gpuvm__: &drm_gpuvm to walk over
295 * @start__: starting offset, the first gpuva will overlap this
296 * @end__: ending offset, the last gpuva will start before this (but may
299 * This iterator walks over all &drm_gpuvas in the &drm_gpuvm that lie
300 * between @start__ and @end__. It is implemented similarly to
301 * list_for_each_safe(), but is using the &drm_gpuvm's internal interval
302 * tree to accelerate the search for the starting &drm_gpuva, and hence is safe
303 * against removal of elements. It assumes that @end__ is within (or is the
304 * upper limit of) the &drm_gpuvm. This iterator does not skip over the
305 * &drm_gpuvm's @kernel_alloc_node.
307 #define drm_gpuvm_for_each_va_range_safe(va__, next__, gpuvm__, start__, end__) \
308 for (va__ = drm_gpuva_find_first((gpuvm__), (start__), (end__) - (start__)), \
309 next__ = __drm_gpuva_next(va__); \
310 va__ && (va__->va.addr < (end__)); \
311 va__ = next__, next__ = __drm_gpuva_next(va__))
314 * drm_gpuvm_for_each_va() - iterate over all &drm_gpuvas
315 * @va__: &drm_gpuva to assign to in each iteration step
316 * @gpuvm__: &drm_gpuvm to walk over
318 * This iterator walks over all &drm_gpuva structures associated with the given
321 #define drm_gpuvm_for_each_va(va__, gpuvm__) \
322 list_for_each_entry(va__, &(gpuvm__)->rb.list, rb.entry)
325 * drm_gpuvm_for_each_va_safe() - safely iterate over all &drm_gpuvas
326 * @va__: &drm_gpuva to assign to in each iteration step
327 * @next__: another &drm_gpuva to use as temporary storage
328 * @gpuvm__: &drm_gpuvm to walk over
330 * This iterator walks over all &drm_gpuva structures associated with the given
331 * &drm_gpuvm. It is implemented with list_for_each_entry_safe(), and
332 * hence safe against the removal of elements.
334 #define drm_gpuvm_for_each_va_safe(va__, next__, gpuvm__) \
335 list_for_each_entry_safe(va__, next__, &(gpuvm__)->rb.list, rb.entry)
338 * enum drm_gpuva_op_type - GPU VA operation type
340 * Operations to alter the GPU VA mappings tracked by the &drm_gpuvm.
342 enum drm_gpuva_op_type {
344 * @DRM_GPUVA_OP_MAP: the map op type
349 * @DRM_GPUVA_OP_REMAP: the remap op type
354 * @DRM_GPUVA_OP_UNMAP: the unmap op type
359 * @DRM_GPUVA_OP_PREFETCH: the prefetch op type
361 DRM_GPUVA_OP_PREFETCH,
365 * struct drm_gpuva_op_map - GPU VA map operation
367 * This structure represents a single map operation generated by the
368 * DRM GPU VA manager.
370 struct drm_gpuva_op_map {
372 * @va: structure containing address and range of a map
377 * @addr: the base address of the new mapping
382 * @range: the range of the new mapping
388 * @gem: structure containing the &drm_gem_object and it's offset
392 * @offset: the offset within the &drm_gem_object
397 * @obj: the &drm_gem_object to map
399 struct drm_gem_object *obj;
404 * struct drm_gpuva_op_unmap - GPU VA unmap operation
406 * This structure represents a single unmap operation generated by the
407 * DRM GPU VA manager.
409 struct drm_gpuva_op_unmap {
411 * @va: the &drm_gpuva to unmap
413 struct drm_gpuva *va;
418 * Indicates whether this &drm_gpuva is physically contiguous with the
419 * original mapping request.
421 * Optionally, if &keep is set, drivers may keep the actual page table
422 * mappings for this &drm_gpuva, adding the missing page table entries
423 * only and update the &drm_gpuvm accordingly.
429 * struct drm_gpuva_op_remap - GPU VA remap operation
431 * This represents a single remap operation generated by the DRM GPU VA manager.
433 * A remap operation is generated when an existing GPU VA mmapping is split up
434 * by inserting a new GPU VA mapping or by partially unmapping existent
435 * mapping(s), hence it consists of a maximum of two map and one unmap
438 * The @unmap operation takes care of removing the original existing mapping.
439 * @prev is used to remap the preceding part, @next the subsequent part.
441 * If either a new mapping's start address is aligned with the start address
442 * of the old mapping or the new mapping's end address is aligned with the
443 * end address of the old mapping, either @prev or @next is NULL.
445 * Note, the reason for a dedicated remap operation, rather than arbitrary
446 * unmap and map operations, is to give drivers the chance of extracting driver
447 * specific data for creating the new mappings from the unmap operations's
448 * &drm_gpuva structure which typically is embedded in larger driver specific
451 struct drm_gpuva_op_remap {
453 * @prev: the preceding part of a split mapping
455 struct drm_gpuva_op_map *prev;
458 * @next: the subsequent part of a split mapping
460 struct drm_gpuva_op_map *next;
463 * @unmap: the unmap operation for the original existing mapping
465 struct drm_gpuva_op_unmap *unmap;
469 * struct drm_gpuva_op_prefetch - GPU VA prefetch operation
471 * This structure represents a single prefetch operation generated by the
472 * DRM GPU VA manager.
474 struct drm_gpuva_op_prefetch {
476 * @va: the &drm_gpuva to prefetch
478 struct drm_gpuva *va;
482 * struct drm_gpuva_op - GPU VA operation
484 * This structure represents a single generic operation.
486 * The particular type of the operation is defined by @op.
488 struct drm_gpuva_op {
492 * The &list_head used to distribute instances of this struct within
495 struct list_head entry;
498 * @op: the type of the operation
500 enum drm_gpuva_op_type op;
504 * @map: the map operation
506 struct drm_gpuva_op_map map;
509 * @remap: the remap operation
511 struct drm_gpuva_op_remap remap;
514 * @unmap: the unmap operation
516 struct drm_gpuva_op_unmap unmap;
519 * @prefetch: the prefetch operation
521 struct drm_gpuva_op_prefetch prefetch;
526 * struct drm_gpuva_ops - wraps a list of &drm_gpuva_op
528 struct drm_gpuva_ops {
530 * @list: the &list_head
532 struct list_head list;
536 * drm_gpuva_for_each_op() - iterator to walk over &drm_gpuva_ops
537 * @op: &drm_gpuva_op to assign in each iteration step
538 * @ops: &drm_gpuva_ops to walk
540 * This iterator walks over all ops within a given list of operations.
542 #define drm_gpuva_for_each_op(op, ops) list_for_each_entry(op, &(ops)->list, entry)
545 * drm_gpuva_for_each_op_safe() - iterator to safely walk over &drm_gpuva_ops
546 * @op: &drm_gpuva_op to assign in each iteration step
547 * @next: &next &drm_gpuva_op to store the next step
548 * @ops: &drm_gpuva_ops to walk
550 * This iterator walks over all ops within a given list of operations. It is
551 * implemented with list_for_each_safe(), so save against removal of elements.
553 #define drm_gpuva_for_each_op_safe(op, next, ops) \
554 list_for_each_entry_safe(op, next, &(ops)->list, entry)
557 * drm_gpuva_for_each_op_from_reverse() - iterate backwards from the given point
558 * @op: &drm_gpuva_op to assign in each iteration step
559 * @ops: &drm_gpuva_ops to walk
561 * This iterator walks over all ops within a given list of operations beginning
562 * from the given operation in reverse order.
564 #define drm_gpuva_for_each_op_from_reverse(op, ops) \
565 list_for_each_entry_from_reverse(op, &(ops)->list, entry)
568 * drm_gpuva_first_op() - returns the first &drm_gpuva_op from &drm_gpuva_ops
569 * @ops: the &drm_gpuva_ops to get the fist &drm_gpuva_op from
571 #define drm_gpuva_first_op(ops) \
572 list_first_entry(&(ops)->list, struct drm_gpuva_op, entry)
575 * drm_gpuva_last_op() - returns the last &drm_gpuva_op from &drm_gpuva_ops
576 * @ops: the &drm_gpuva_ops to get the last &drm_gpuva_op from
578 #define drm_gpuva_last_op(ops) \
579 list_last_entry(&(ops)->list, struct drm_gpuva_op, entry)
582 * drm_gpuva_prev_op() - previous &drm_gpuva_op in the list
583 * @op: the current &drm_gpuva_op
585 #define drm_gpuva_prev_op(op) list_prev_entry(op, entry)
588 * drm_gpuva_next_op() - next &drm_gpuva_op in the list
589 * @op: the current &drm_gpuva_op
591 #define drm_gpuva_next_op(op) list_next_entry(op, entry)
593 struct drm_gpuva_ops *
594 drm_gpuvm_sm_map_ops_create(struct drm_gpuvm *gpuvm,
596 struct drm_gem_object *obj, u64 offset);
597 struct drm_gpuva_ops *
598 drm_gpuvm_sm_unmap_ops_create(struct drm_gpuvm *gpuvm,
599 u64 addr, u64 range);
601 struct drm_gpuva_ops *
602 drm_gpuvm_prefetch_ops_create(struct drm_gpuvm *gpuvm,
603 u64 addr, u64 range);
605 struct drm_gpuva_ops *
606 drm_gpuvm_gem_unmap_ops_create(struct drm_gpuvm *gpuvm,
607 struct drm_gem_object *obj);
609 void drm_gpuva_ops_free(struct drm_gpuvm *gpuvm,
610 struct drm_gpuva_ops *ops);
612 static inline void drm_gpuva_init_from_op(struct drm_gpuva *va,
613 struct drm_gpuva_op_map *op)
615 drm_gpuva_init(va, op->va.addr, op->va.range,
616 op->gem.obj, op->gem.offset);
620 * struct drm_gpuvm_ops - callbacks for split/merge steps
622 * This structure defines the callbacks used by &drm_gpuvm_sm_map and
623 * &drm_gpuvm_sm_unmap to provide the split/merge steps for map and unmap
624 * operations to drivers.
626 struct drm_gpuvm_ops {
628 * @op_alloc: called when the &drm_gpuvm allocates
629 * a struct drm_gpuva_op
631 * Some drivers may want to embed struct drm_gpuva_op into driver
632 * specific structures. By implementing this callback drivers can
633 * allocate memory accordingly.
635 * This callback is optional.
637 struct drm_gpuva_op *(*op_alloc)(void);
640 * @op_free: called when the &drm_gpuvm frees a
641 * struct drm_gpuva_op
643 * Some drivers may want to embed struct drm_gpuva_op into driver
644 * specific structures. By implementing this callback drivers can
645 * free the previously allocated memory accordingly.
647 * This callback is optional.
649 void (*op_free)(struct drm_gpuva_op *op);
652 * @sm_step_map: called from &drm_gpuvm_sm_map to finally insert the
653 * mapping once all previous steps were completed
655 * The &priv pointer matches the one the driver passed to
656 * &drm_gpuvm_sm_map or &drm_gpuvm_sm_unmap, respectively.
658 * Can be NULL if &drm_gpuvm_sm_map is used.
660 int (*sm_step_map)(struct drm_gpuva_op *op, void *priv);
663 * @sm_step_remap: called from &drm_gpuvm_sm_map and
664 * &drm_gpuvm_sm_unmap to split up an existent mapping
666 * This callback is called when existent mapping needs to be split up.
667 * This is the case when either a newly requested mapping overlaps or
668 * is enclosed by an existent mapping or a partial unmap of an existent
669 * mapping is requested.
671 * The &priv pointer matches the one the driver passed to
672 * &drm_gpuvm_sm_map or &drm_gpuvm_sm_unmap, respectively.
674 * Can be NULL if neither &drm_gpuvm_sm_map nor &drm_gpuvm_sm_unmap is
677 int (*sm_step_remap)(struct drm_gpuva_op *op, void *priv);
680 * @sm_step_unmap: called from &drm_gpuvm_sm_map and
681 * &drm_gpuvm_sm_unmap to unmap an existent mapping
683 * This callback is called when existent mapping needs to be unmapped.
684 * This is the case when either a newly requested mapping encloses an
685 * existent mapping or an unmap of an existent mapping is requested.
687 * The &priv pointer matches the one the driver passed to
688 * &drm_gpuvm_sm_map or &drm_gpuvm_sm_unmap, respectively.
690 * Can be NULL if neither &drm_gpuvm_sm_map nor &drm_gpuvm_sm_unmap is
693 int (*sm_step_unmap)(struct drm_gpuva_op *op, void *priv);
696 int drm_gpuvm_sm_map(struct drm_gpuvm *gpuvm, void *priv,
698 struct drm_gem_object *obj, u64 offset);
700 int drm_gpuvm_sm_unmap(struct drm_gpuvm *gpuvm, void *priv,
701 u64 addr, u64 range);
703 void drm_gpuva_map(struct drm_gpuvm *gpuvm,
704 struct drm_gpuva *va,
705 struct drm_gpuva_op_map *op);
707 void drm_gpuva_remap(struct drm_gpuva *prev,
708 struct drm_gpuva *next,
709 struct drm_gpuva_op_remap *op);
711 void drm_gpuva_unmap(struct drm_gpuva_op_unmap *op);
713 #endif /* __DRM_GPUVM_H__ */