xref: /linux/drivers/gpu/drm/xe/xe_userptr.h (revision 68a052239fc4b351e961f698b824f7654a346091)
1 /* SPDX-License-Identifier: MIT */
2 /*
3  * Copyright © 2025 Intel Corporation
4  */
5 
6 #ifndef _XE_USERPTR_H_
7 #define _XE_USERPTR_H_
8 
9 #include <linux/list.h>
10 #include <linux/mutex.h>
11 #include <linux/notifier.h>
12 #include <linux/scatterlist.h>
13 #include <linux/spinlock.h>
14 
15 #include <drm/drm_gpusvm.h>
16 
17 struct xe_vm;
18 struct xe_vma;
19 struct xe_userptr_vma;
20 
21 /** struct xe_userptr_vm - User pointer VM level state */
22 struct xe_userptr_vm {
23 	/**
24 	 * @userptr.repin_list: list of VMAs which are user pointers,
25 	 * and needs repinning. Protected by @lock.
26 	 */
27 	struct list_head repin_list;
28 	/**
29 	 * @userptr.invalidated_lock: Protects the
30 	 * @userptr.invalidated list.
31 	 */
32 	spinlock_t invalidated_lock;
33 	/**
34 	 * @userptr.invalidated: List of invalidated userptrs, not yet
35 	 * picked
36 	 * up for revalidation. Protected from access with the
37 	 * @invalidated_lock. Removing items from the list
38 	 * additionally requires @lock in write mode, and adding
39 	 * items to the list requires either the @svm.gpusvm.notifier_lock in
40 	 * write mode, OR @lock in write mode.
41 	 */
42 	struct list_head invalidated;
43 };
44 
45 /** struct xe_userptr - User pointer */
46 struct xe_userptr {
47 	/** @invalidate_link: Link for the vm::userptr.invalidated list */
48 	struct list_head invalidate_link;
49 	/** @userptr: link into VM repin list if userptr. */
50 	struct list_head repin_link;
51 	/**
52 	 * @pages: gpusvm pages for this user pointer.
53 	 */
54 	struct drm_gpusvm_pages pages;
55 	/**
56 	 * @notifier: MMU notifier for user pointer (invalidation call back)
57 	 */
58 	struct mmu_interval_notifier notifier;
59 
60 	/**
61 	 * @initial_bind: user pointer has been bound at least once.
62 	 * write: vm->svm.gpusvm.notifier_lock in read mode and vm->resv held.
63 	 * read: vm->svm.gpusvm.notifier_lock in write mode or vm->resv held.
64 	 */
65 	bool initial_bind;
66 #if IS_ENABLED(CONFIG_DRM_XE_USERPTR_INVAL_INJECT)
67 	u32 divisor;
68 #endif
69 };
70 
71 #if IS_ENABLED(CONFIG_DRM_GPUSVM)
72 void xe_userptr_remove(struct xe_userptr_vma *uvma);
73 int xe_userptr_setup(struct xe_userptr_vma *uvma, unsigned long start,
74 		     unsigned long range);
75 void xe_userptr_destroy(struct xe_userptr_vma *uvma);
76 
77 int xe_vm_userptr_pin(struct xe_vm *vm);
78 int __xe_vm_userptr_needs_repin(struct xe_vm *vm);
79 int xe_vm_userptr_check_repin(struct xe_vm *vm);
80 int xe_vma_userptr_pin_pages(struct xe_userptr_vma *uvma);
81 int xe_vma_userptr_check_repin(struct xe_userptr_vma *uvma);
82 #else
83 static inline void xe_userptr_remove(struct xe_userptr_vma *uvma) {}
84 
85 static inline int xe_userptr_setup(struct xe_userptr_vma *uvma,
86 				   unsigned long start, unsigned long range)
87 {
88 	return -ENODEV;
89 }
90 
91 static inline void xe_userptr_destroy(struct xe_userptr_vma *uvma) {}
92 
93 static inline int xe_vm_userptr_pin(struct xe_vm *vm) { return 0; }
94 static inline int __xe_vm_userptr_needs_repin(struct xe_vm *vm) { return 0; }
95 static inline int xe_vm_userptr_check_repin(struct xe_vm *vm) { return 0; }
96 static inline int xe_vma_userptr_pin_pages(struct xe_userptr_vma *uvma) { return -ENODEV; }
97 static inline int xe_vma_userptr_check_repin(struct xe_userptr_vma *uvma) { return -ENODEV; };
98 #endif
99 
100 #if IS_ENABLED(CONFIG_DRM_XE_USERPTR_INVAL_INJECT)
101 void xe_vma_userptr_force_invalidate(struct xe_userptr_vma *uvma);
102 #else
103 static inline void xe_vma_userptr_force_invalidate(struct xe_userptr_vma *uvma)
104 {
105 }
106 #endif
107 #endif
108