1304 lines · 35.1 KB · C
RawDownload
/* SPDX-License-Identifier: GPL-2.0-only OR MIT */ #ifndef __DRM_GPUVM_H__#define __DRM_GPUVM_H__ /* * Copyright (c) 2022 Red Hat. * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. */ #include <linux/dma-resv.h>#include <linux/list.h>#include <linux/llist.h>#include <linux/rbtree.h>#include <linux/types.h> #include <drm/drm_device.h>#include <drm/drm_gem.h>#include <drm/drm_exec.h> struct drm_gpuvm;struct drm_gpuvm_bo;struct drm_gpuvm_ops; /** * enum drm_gpuva_flags - flags for struct drm_gpuva */enum drm_gpuva_flags {	/**	 * @DRM_GPUVA_INVALIDATED:	 *	 * Flag indicating that the &drm_gpuva's backing GEM is invalidated.	 */	DRM_GPUVA_INVALIDATED = (1 << 0), 	/**	 * @DRM_GPUVA_SPARSE:	 *	 * Flag indicating that the &drm_gpuva is a sparse mapping.	 */	DRM_GPUVA_SPARSE = (1 << 1), 	/**	 * @DRM_GPUVA_USERBITS: user defined bits	 */	DRM_GPUVA_USERBITS = (1 << 2),}; /** * struct drm_gpuva - structure to track a GPU VA mapping * * This structure represents a GPU VA mapping and is associated with a * &drm_gpuvm. * * Typically, this structure is embedded in bigger driver structures. */struct drm_gpuva {	/**	 * @vm: the &drm_gpuvm this object is associated with	 */	struct drm_gpuvm *vm; 	/**	 * @vm_bo: the &drm_gpuvm_bo abstraction for the mapped	 * &drm_gem_object	 */	struct drm_gpuvm_bo *vm_bo; 	/**	 * @flags: the &drm_gpuva_flags for this mapping	 */	enum drm_gpuva_flags flags; 	/**	 * @va: structure containing the address and range of the &drm_gpuva	 */	struct {		/**		 * @va.addr: the start address		 */		u64 addr; 		/*		 * @range: the range		 */		u64 range;	} va; 	/**	 * @gem: structure containing the &drm_gem_object and its offset	 */	struct {		/**		 * @gem.offset: the offset within the &drm_gem_object		 */		u64 offset; 		/**		 * @gem.obj: the mapped &drm_gem_object		 */		struct drm_gem_object *obj; 		/**		 * @gem.entry: the &list_head to attach this object to a &drm_gpuvm_bo		 */		struct list_head entry;	} gem; 	/**	 * @rb: structure containing data to store &drm_gpuvas in a rb-tree	 */	struct {		/**		 * @rb.node: the rb-tree node		 */		struct rb_node node; 		/**		 * @rb.entry: The &list_head to additionally connect &drm_gpuvas		 * in the same order they appear in the interval tree. This is		 * useful to keep iterating &drm_gpuvas from a start node found		 * through the rb-tree while doing modifications on the rb-tree		 * itself.		 */		struct list_head entry; 		/**		 * @rb.__subtree_last: needed by the interval tree, holding last-in-subtree		 */		u64 __subtree_last;	} rb;}; int drm_gpuva_insert(struct drm_gpuvm *gpuvm, struct drm_gpuva *va);void drm_gpuva_remove(struct drm_gpuva *va); void drm_gpuva_link(struct drm_gpuva *va, struct drm_gpuvm_bo *vm_bo);void drm_gpuva_unlink(struct drm_gpuva *va);void drm_gpuva_unlink_defer(struct drm_gpuva *va); struct drm_gpuva *drm_gpuva_find(struct drm_gpuvm *gpuvm,				 u64 addr, u64 range);struct drm_gpuva *drm_gpuva_find_first(struct drm_gpuvm *gpuvm,				       u64 addr, u64 range);struct drm_gpuva *drm_gpuva_find_prev(struct drm_gpuvm *gpuvm, u64 start);struct drm_gpuva *drm_gpuva_find_next(struct drm_gpuvm *gpuvm, u64 end); /** * drm_gpuva_invalidate() - sets whether the backing GEM of this &drm_gpuva is * invalidated * @va: the &drm_gpuva to set the invalidate flag for * @invalidate: indicates whether the &drm_gpuva is invalidated */static inline void drm_gpuva_invalidate(struct drm_gpuva *va, bool invalidate){	if (invalidate)		va->flags |= DRM_GPUVA_INVALIDATED;	else		va->flags &= ~DRM_GPUVA_INVALIDATED;} /** * drm_gpuva_invalidated() - indicates whether the backing BO of this &drm_gpuva * is invalidated * @va: the &drm_gpuva to check * * Returns: %true if the GPU VA is invalidated, %false otherwise */static inline bool drm_gpuva_invalidated(struct drm_gpuva *va){	return va->flags & DRM_GPUVA_INVALIDATED;} /** * enum drm_gpuvm_flags - flags for struct drm_gpuvm */enum drm_gpuvm_flags {	/**	 * @DRM_GPUVM_RESV_PROTECTED: GPUVM is protected externally by the	 * GPUVM's &dma_resv lock	 */	DRM_GPUVM_RESV_PROTECTED = BIT(0), 	/**	 * @DRM_GPUVM_IMMEDIATE_MODE: use the locking scheme for GEMs designed	 * for modifying the GPUVM during the fence signalling path	 *	 * When set, gpuva.lock is used to protect gpuva.list in all GEM	 * objects associated with this GPUVM. Otherwise, the GEMs dma-resv is	 * used.	 */	DRM_GPUVM_IMMEDIATE_MODE = BIT(1), 	/**	 * @DRM_GPUVM_USERBITS: user defined bits	 */	DRM_GPUVM_USERBITS = BIT(2),}; /** * struct drm_gpuvm - DRM GPU VA Manager * * The DRM GPU VA Manager keeps track of a GPU's virtual address space by using * &maple_tree structures. Typically, this structure is embedded in bigger * driver structures. * * Drivers can pass addresses and ranges in an arbitrary unit, e.g. bytes or * pages. * * There should be one manager instance per GPU virtual address space. */struct drm_gpuvm {	/**	 * @name: the name of the DRM GPU VA space	 */	const char *name; 	/**	 * @flags: the &drm_gpuvm_flags of this GPUVM	 */	enum drm_gpuvm_flags flags; 	/**	 * @drm: the &drm_device this VM lives in	 */	struct drm_device *drm; 	/**	 * @mm_start: start of the VA space	 */	u64 mm_start; 	/**	 * @mm_range: length of the VA space	 */	u64 mm_range; 	/**	 * @rb: structures to track &drm_gpuva entries	 */	struct {		/**		 * @rb.tree: the rb-tree to track GPU VA mappings		 */		struct rb_root_cached tree; 		/**		 * @rb.list: the &list_head to track GPU VA mappings		 */		struct list_head list;	} rb; 	/**	 * @kref: reference count of this object	 */	struct kref kref; 	/**	 * @kernel_alloc_node:	 *	 * &drm_gpuva representing the address space cutout reserved for	 * the kernel	 */	struct drm_gpuva kernel_alloc_node; 	/**	 * @ops: &drm_gpuvm_ops providing the split/merge steps to drivers	 */	const struct drm_gpuvm_ops *ops; 	/**	 * @r_obj: Resv GEM object; representing the GPUVM's common &dma_resv.	 */	struct drm_gem_object *r_obj; 	/**	 * @extobj: structure holding the extobj list	 */	struct {		/**		 * @extobj.list: &list_head storing &drm_gpuvm_bos serving as		 * external object		 */		struct list_head list; 		/**		 * @extobj.local_list: pointer to the local list temporarily		 * storing entries from the external object list		 */		struct list_head *local_list; 		/**		 * @extobj.lock: spinlock to protect the extobj list		 */		spinlock_t lock;	} extobj; 	/**	 * @evict: structure holding the evict list and evict list lock	 */	struct {		/**		 * @evict.list: &list_head storing &drm_gpuvm_bos currently		 * being evicted		 */		struct list_head list; 		/**		 * @evict.local_list: pointer to the local list temporarily		 * storing entries from the evicted object list		 */		struct list_head *local_list; 		/**		 * @evict.lock: spinlock to protect the evict list		 */		spinlock_t lock;	} evict; 	/**	 * @bo_defer: structure holding vm_bos that need to be destroyed	 */	struct llist_head bo_defer;}; void drm_gpuvm_init(struct drm_gpuvm *gpuvm, const char *name,		    enum drm_gpuvm_flags flags,		    struct drm_device *drm,		    struct drm_gem_object *r_obj,		    u64 start_offset, u64 range,		    u64 reserve_offset, u64 reserve_range,		    const struct drm_gpuvm_ops *ops); /** * drm_gpuvm_get() - acquire a struct drm_gpuvm reference * @gpuvm: the &drm_gpuvm to acquire the reference of * * This function acquires an additional reference to @gpuvm. It is illegal to * call this without already holding a reference. No locks required. * * Returns: the &struct drm_gpuvm pointer */static inline struct drm_gpuvm *drm_gpuvm_get(struct drm_gpuvm *gpuvm){	kref_get(&gpuvm->kref); 	return gpuvm;} void drm_gpuvm_put(struct drm_gpuvm *gpuvm); bool drm_gpuvm_range_valid(struct drm_gpuvm *gpuvm, u64 addr, u64 range);bool drm_gpuvm_interval_empty(struct drm_gpuvm *gpuvm, u64 addr, u64 range); struct drm_gem_object *drm_gpuvm_resv_object_alloc(struct drm_device *drm); /** * drm_gpuvm_resv_protected() - indicates whether &DRM_GPUVM_RESV_PROTECTED is * set * @gpuvm: the &drm_gpuvm * * Returns: true if &DRM_GPUVM_RESV_PROTECTED is set, false otherwise. */static inline booldrm_gpuvm_resv_protected(struct drm_gpuvm *gpuvm){	return gpuvm->flags & DRM_GPUVM_RESV_PROTECTED;} /** * drm_gpuvm_immediate_mode() - indicates whether &DRM_GPUVM_IMMEDIATE_MODE is * set * @gpuvm: the &drm_gpuvm * * Returns: true if &DRM_GPUVM_IMMEDIATE_MODE is set, false otherwise. */static inline booldrm_gpuvm_immediate_mode(struct drm_gpuvm *gpuvm){	return gpuvm->flags & DRM_GPUVM_IMMEDIATE_MODE;} /** * drm_gpuvm_resv() - returns the &drm_gpuvm's &dma_resv * @gpuvm__: the &drm_gpuvm * * Returns: a pointer to the &drm_gpuvm's shared &dma_resv */#define drm_gpuvm_resv(gpuvm__) ((gpuvm__)->r_obj->resv) /** * drm_gpuvm_resv_obj() - returns the &drm_gem_object holding the &drm_gpuvm's * &dma_resv * @gpuvm__: the &drm_gpuvm * * Returns: a pointer to the &drm_gem_object holding the &drm_gpuvm's shared * &dma_resv */#define drm_gpuvm_resv_obj(gpuvm__) ((gpuvm__)->r_obj) #define drm_gpuvm_resv_held(gpuvm__) \	dma_resv_held(drm_gpuvm_resv(gpuvm__)) #define drm_gpuvm_resv_assert_held(gpuvm__) \	dma_resv_assert_held(drm_gpuvm_resv(gpuvm__)) #define drm_gpuvm_resv_held(gpuvm__) \	dma_resv_held(drm_gpuvm_resv(gpuvm__)) #define drm_gpuvm_resv_assert_held(gpuvm__) \	dma_resv_assert_held(drm_gpuvm_resv(gpuvm__)) /** * drm_gpuvm_is_extobj() - indicates whether the given &drm_gem_object is an * external object * @gpuvm: the &drm_gpuvm to check * @obj: the &drm_gem_object to check * * Returns: true if the &drm_gem_object &dma_resv differs from the * &drm_gpuvms &dma_resv, false otherwise */static inline booldrm_gpuvm_is_extobj(struct drm_gpuvm *gpuvm,		    struct drm_gem_object *obj){	return obj && obj->resv != drm_gpuvm_resv(gpuvm);} static inline struct drm_gpuva *__drm_gpuva_next(struct drm_gpuva *va){	if (va && !list_is_last(&va->rb.entry, &va->vm->rb.list))		return list_next_entry(va, rb.entry); 	return NULL;} /** * drm_gpuvm_for_each_va_range() - iterate over a range of &drm_gpuvas * @va__: &drm_gpuva structure to assign to in each iteration step * @gpuvm__: &drm_gpuvm to walk over * @start__: starting offset, the first gpuva will overlap this * @end__: ending offset, the last gpuva will start before this (but may * overlap) * * This iterator walks over all &drm_gpuvas in the &drm_gpuvm that lie * between @start__ and @end__. It is implemented similarly to list_for_each(), * but is using the &drm_gpuvm's internal interval tree to accelerate * the search for the starting &drm_gpuva, and hence isn't safe against removal * of elements. It assumes that @end__ is within (or is the upper limit of) the * &drm_gpuvm. This iterator does not skip over the &drm_gpuvm's * @kernel_alloc_node. */#define drm_gpuvm_for_each_va_range(va__, gpuvm__, start__, end__) \	for (va__ = drm_gpuva_find_first((gpuvm__), (start__), (end__) - (start__)); \	     va__ && (va__->va.addr < (end__)); \	     va__ = __drm_gpuva_next(va__)) /** * drm_gpuvm_for_each_va_range_safe() - safely iterate over a range of * &drm_gpuvas * @va__: &drm_gpuva to assign to in each iteration step * @next__: another &drm_gpuva to use as temporary storage * @gpuvm__: &drm_gpuvm to walk over * @start__: starting offset, the first gpuva will overlap this * @end__: ending offset, the last gpuva will start before this (but may * overlap) * * This iterator walks over all &drm_gpuvas in the &drm_gpuvm that lie * between @start__ and @end__. It is implemented similarly to * list_for_each_safe(), but is using the &drm_gpuvm's internal interval * tree to accelerate the search for the starting &drm_gpuva, and hence is safe * against removal of elements. It assumes that @end__ is within (or is the * upper limit of) the &drm_gpuvm. This iterator does not skip over the * &drm_gpuvm's @kernel_alloc_node. */#define drm_gpuvm_for_each_va_range_safe(va__, next__, gpuvm__, start__, end__) \	for (va__ = drm_gpuva_find_first((gpuvm__), (start__), (end__) - (start__)), \	     next__ = __drm_gpuva_next(va__); \	     va__ && (va__->va.addr < (end__)); \	     va__ = next__, next__ = __drm_gpuva_next(va__))

Lines 1–500 of 1304.