xref: /linux/include/drm/drm_gpusvm.h (revision b0a3a811634aa7422f8a92196db47a06a70bda16)
1 /* SPDX-License-Identifier: GPL-2.0-only OR MIT */
2 /*
3  * Copyright © 2024 Intel Corporation
4  */
5 
6 #ifndef __DRM_GPUSVM_H__
7 #define __DRM_GPUSVM_H__
8 
9 #include <linux/dma-mapping.h>
10 #include <linux/kref.h>
11 #include <linux/interval_tree.h>
12 #include <linux/mmu_notifier.h>
13 #include <drm/drm_pagemap.h>
14 
15 struct dev_pagemap_ops;
16 struct drm_device;
17 struct drm_gpusvm;
18 struct drm_gpusvm_notifier;
19 struct drm_gpusvm_ops;
20 struct drm_gpusvm_range;
21 struct drm_pagemap;
22 
23 /**
24  * struct drm_gpusvm_ops - Operations structure for GPU SVM
25  *
26  * This structure defines the operations for GPU Shared Virtual Memory (SVM).
27  * These operations are provided by the GPU driver to manage SVM ranges and
28  * notifiers.
29  */
30 struct drm_gpusvm_ops {
31 	/**
32 	 * @notifier_alloc: Allocate a GPU SVM notifier (optional)
33 	 *
34 	 * Allocate a GPU SVM notifier.
35 	 *
36 	 * Return: Pointer to the allocated GPU SVM notifier on success, NULL on failure.
37 	 */
38 	struct drm_gpusvm_notifier *(*notifier_alloc)(void);
39 
40 	/**
41 	 * @notifier_free: Free a GPU SVM notifier (optional)
42 	 * @notifier: Pointer to the GPU SVM notifier to be freed
43 	 *
44 	 * Free a GPU SVM notifier.
45 	 */
46 	void (*notifier_free)(struct drm_gpusvm_notifier *notifier);
47 
48 	/**
49 	 * @range_alloc: Allocate a GPU SVM range (optional)
50 	 * @gpusvm: Pointer to the GPU SVM
51 	 *
52 	 * Allocate a GPU SVM range.
53 	 *
54 	 * Return: Pointer to the allocated GPU SVM range on success, NULL on failure.
55 	 */
56 	struct drm_gpusvm_range *(*range_alloc)(struct drm_gpusvm *gpusvm);
57 
58 	/**
59 	 * @range_free: Free a GPU SVM range (optional)
60 	 * @range: Pointer to the GPU SVM range to be freed
61 	 *
62 	 * Free a GPU SVM range.
63 	 */
64 	void (*range_free)(struct drm_gpusvm_range *range);
65 
66 	/**
67 	 * @invalidate: Invalidate GPU SVM notifier (required)
68 	 * @gpusvm: Pointer to the GPU SVM
69 	 * @notifier: Pointer to the GPU SVM notifier
70 	 * @mmu_range: Pointer to the mmu_notifier_range structure
71 	 *
72 	 * Invalidate the GPU page tables. It can safely walk the notifier range
73 	 * RB tree/list in this function. Called while holding the notifier lock.
74 	 */
75 	void (*invalidate)(struct drm_gpusvm *gpusvm,
76 			   struct drm_gpusvm_notifier *notifier,
77 			   const struct mmu_notifier_range *mmu_range);
78 };
79 
80 /**
81  * struct drm_gpusvm_notifier - Structure representing a GPU SVM notifier
82  *
83  * @gpusvm: Pointer to the GPU SVM structure
84  * @notifier: MMU interval notifier
85  * @itree: Interval tree node for the notifier (inserted in GPU SVM)
86  * @entry: List entry to fast interval tree traversal
87  * @root: Cached root node of the RB tree containing ranges
88  * @range_list: List head containing of ranges in the same order they appear in
89  *              interval tree. This is useful to keep iterating ranges while
90  *              doing modifications to RB tree.
91  * @flags: Flags for notifier
92  * @flags.removed: Flag indicating whether the MMU interval notifier has been
93  *                 removed
94  *
95  * This structure represents a GPU SVM notifier.
96  */
97 struct drm_gpusvm_notifier {
98 	struct drm_gpusvm *gpusvm;
99 	struct mmu_interval_notifier notifier;
100 	struct interval_tree_node itree;
101 	struct list_head entry;
102 	struct rb_root_cached root;
103 	struct list_head range_list;
104 	struct {
105 		u32 removed : 1;
106 	} flags;
107 };
108 
109 /**
110  * struct drm_gpusvm_pages_flags - Structure representing a GPU SVM pages flags
111  *
112  * @unmapped: Flag indicating if the pages has been unmapped
113  * @has_devmem_pages: Flag indicating if the pages has devmem pages
114  * @has_dma_mapping: Flag indicating if the pages has a DMA mapping
115  * @inline_dma_mapping: Flag indicating if the pages have an inline DMA mapping
116  * @__flags: Flags for pages in u16 form (used for READ_ONCE)
117  */
118 struct drm_gpusvm_pages_flags {
119 	union {
120 		struct {
121 			/* All flags below must be set / cleared under notifier lock */
122 			u16 unmapped : 1;
123 			u16 has_devmem_pages : 1;
124 			u16 has_dma_mapping : 1;
125 			u16 inline_dma_mapping : 1;
126 		};
127 		u16 __flags;
128 	};
129 };
130 
131 /**
132  * struct drm_gpusvm_pages - Structure representing a GPU SVM mapped pages
133  *
134  * @drm: The DRM device that owns the dma mappings
135  * @dma_addr: Device address array, valid while @flags.inline_dma_mapping is
136  *            not set
137  * @inline_addr: Device address inline address, valid while
138  *               @flags.inline_dma_mapping is set
139  * @dpagemap: The struct drm_pagemap of the device pages we're dma-mapping.
140  *            Note this is assuming only one drm_pagemap per range is allowed.
141  * @state: DMA IOVA state for mapping.
142  * @state_offset: DMA IOVA offset for mapping.
143  * @notifier_seq: Notifier sequence number of the range's pages
144  * @flags: Flags for the range; see &struct drm_gpusvm_pages_flags
145  *
146  * @dma_addr and @inline_addr share storage, discriminated by
147  * @flags.inline_dma_mapping. Driver should use drm_gpusvm_pages_first_dma()
148  * to access the correct DMA address.
149  */
150 struct drm_gpusvm_pages {
151 	struct drm_device *drm;
152 	union {
153 		struct drm_pagemap_addr *dma_addr;
154 		struct drm_pagemap_addr inline_addr;
155 	};
156 	struct drm_pagemap *dpagemap;
157 	struct dma_iova_state state;
158 	unsigned long state_offset;
159 	unsigned long notifier_seq;
160 	struct drm_gpusvm_pages_flags flags;
161 };
162 
163 /**
164  * struct drm_gpusvm_range_flags - Range-level GPU SVM flags
165  *
166  * @migrate_devmem: Flag indicating whether the range can be migrated to device memory
167  * @unmapped: Flag indicating if the range has been unmapped
168  * @partial_unmap: Flag indicating if the range has been partially unmapped
169  * @__flags: All flags in u16 form (used for READ_ONCE)
170  */
171 struct drm_gpusvm_range_flags {
172 	union {
173 		struct {
174 			/* All flags below must be set upon creation */
175 			u16 migrate_devmem	: 1;
176 			/* All flags below must be set / cleared under notifier lock */
177 			u16 unmapped		: 1;
178 			u16 partial_unmap	: 1;
179 		};
180 		u16 __flags;
181 	};
182 };
183 
184 /**
185  * struct drm_gpusvm_range - Structure representing a GPU SVM range
186  *
187  * @gpusvm: Pointer to the GPU SVM structure
188  * @notifier: Pointer to the GPU SVM notifier
189  * @refcount: Reference count for the range
190  * @itree: Interval tree node for the range (inserted in GPU SVM notifier)
191  * @entry: List entry to fast interval tree traversal
192  * @flags: Flags for range see &struct drm_gpusvm_range_flags
193  *
194  * This structure represents a GPU SVM range used for tracking memory ranges
195  * mapped in a DRM device.
196  */
197 struct drm_gpusvm_range {
198 	struct drm_gpusvm *gpusvm;
199 	struct drm_gpusvm_notifier *notifier;
200 	struct kref refcount;
201 	struct interval_tree_node itree;
202 	struct list_head entry;
203 	struct drm_gpusvm_range_flags flags;
204 };
205 
206 /**
207  * struct drm_gpusvm - GPU SVM structure
208  *
209  * @name: Name of the GPU SVM
210  * @mm: Pointer to the mm_struct for the address space
211  * @mm_start: Start address of GPU SVM
212  * @mm_range: Range of the GPU SVM
213  * @notifier_size: Size of individual notifiers
214  * @ops: Pointer to the operations structure for GPU SVM
215  * @chunk_sizes: Pointer to the array of chunk sizes used in range allocation.
216  *               Entries should be powers of 2 in descending order.
217  * @num_chunks: Number of chunks
218  * @notifier_lock: Read-write semaphore for protecting notifier operations
219  * @root: Cached root node of the Red-Black tree containing GPU SVM notifiers
220  * @notifier_list: list head containing of notifiers in the same order they
221  *                 appear in interval tree. This is useful to keep iterating
222  *                 notifiers while doing modifications to RB tree.
223  *
224  * This structure represents a GPU SVM (Shared Virtual Memory) used for tracking
225  * memory ranges mapped in a DRM (Direct Rendering Manager) device.
226  *
227  * No reference counting is provided, as this is expected to be embedded in the
228  * driver VM structure along with the struct drm_gpuvm, which handles reference
229  * counting.
230  */
231 struct drm_gpusvm {
232 	const char *name;
233 	struct mm_struct *mm;
234 	unsigned long mm_start;
235 	unsigned long mm_range;
236 	unsigned long notifier_size;
237 	const struct drm_gpusvm_ops *ops;
238 	const unsigned long *chunk_sizes;
239 	int num_chunks;
240 	struct rw_semaphore notifier_lock;
241 	struct rb_root_cached root;
242 	struct list_head notifier_list;
243 #ifdef CONFIG_LOCKDEP
244 	/**
245 	 * @lock_dep_map: Annotates drm_gpusvm_range_find_or_insert and
246 	 * drm_gpusvm_range_remove with a driver provided lock.
247 	 */
248 	struct lockdep_map *lock_dep_map;
249 #endif
250 };
251 
252 /**
253  * struct drm_gpusvm_ctx - DRM GPU SVM context
254  *
255  * @device_private_page_owner: The device-private page owner to use for
256  * this operation
257  * @check_pages_threshold: Check CPU pages for present if chunk is less than or
258  *                         equal to threshold. If not present, reduce chunk
259  *                         size.
260  * @timeslice_ms: The timeslice MS which in minimum time a piece of memory
261  *		  remains with either exclusive GPU or CPU access.
262  * @in_notifier: entering from a MMU notifier
263  * @read_only: operating on read-only memory
264  * @devmem_possible: possible to use device memory
265  * @devmem_only: use only device memory
266  * @allow_mixed: Allow mixed mappings in get pages. Mixing between system and
267  *               single dpagemap is supported, mixing between multiple dpagemap
268  *               is unsupported.
269  * @no_dma_map: Only fault the CPU pages for the range; skip the device DMA
270  *              mapping step. Used by drivers that consume the faulted pages
271  *              without needing a DMA mapping. In this mode @drm on the
272  *              drm_gpusvm_pages is not required, no mapping state is recorded
273  *              and drm_gpusvm_pages_valid() therefore never reports these
274  *              pages as valid; the caller revalidates the snapshot itself, see
275  *              drm_gpusvm_get_pages(). @devmem_only is rejected and no page
276  *              type check is performed, so @allow_mixed has no effect.
277  *
278  * Context that is DRM GPUSVM is operating in (i.e. user arguments).
279  */
280 struct drm_gpusvm_ctx {
281 	void *device_private_page_owner;
282 	unsigned long check_pages_threshold;
283 	unsigned long timeslice_ms;
284 	unsigned int in_notifier :1;
285 	unsigned int read_only :1;
286 	unsigned int devmem_possible :1;
287 	unsigned int devmem_only :1;
288 	unsigned int allow_mixed :1;
289 	unsigned int no_dma_map :1;
290 };
291 
292 int drm_gpusvm_init(struct drm_gpusvm *gpusvm,
293 		    const char *name,
294 		    struct mm_struct *mm,
295 		    unsigned long mm_start, unsigned long mm_range,
296 		    unsigned long notifier_size,
297 		    const struct drm_gpusvm_ops *ops,
298 		    const unsigned long *chunk_sizes, int num_chunks);
299 
300 void drm_gpusvm_fini(struct drm_gpusvm *gpusvm);
301 
302 void drm_gpusvm_free(struct drm_gpusvm *gpusvm);
303 
304 unsigned long
305 drm_gpusvm_find_vma_start(struct drm_gpusvm *gpusvm,
306 			  unsigned long start,
307 			  unsigned long end);
308 
309 struct drm_gpusvm_range *
310 drm_gpusvm_range_find_or_insert(struct drm_gpusvm *gpusvm,
311 				unsigned long fault_addr,
312 				unsigned long gpuva_start,
313 				unsigned long gpuva_end,
314 				const struct drm_gpusvm_ctx *ctx);
315 
316 void drm_gpusvm_range_remove(struct drm_gpusvm *gpusvm,
317 			     struct drm_gpusvm_range *range);
318 
319 int drm_gpusvm_range_evict(struct drm_gpusvm *gpusvm,
320 			   struct drm_gpusvm_range *range);
321 
322 struct drm_gpusvm_range *
323 drm_gpusvm_range_get(struct drm_gpusvm_range *range);
324 
325 void drm_gpusvm_range_put(struct drm_gpusvm_range *range);
326 
327 bool drm_gpusvm_pages_valid(struct drm_gpusvm *gpusvm,
328 			    struct drm_gpusvm_pages *svm_pages);
329 
330 bool drm_gpusvm_has_mapping(struct drm_gpusvm *gpusvm, unsigned long start,
331 			    unsigned long end);
332 
333 struct drm_gpusvm_notifier *
334 drm_gpusvm_notifier_find(struct drm_gpusvm *gpusvm, unsigned long start,
335 			 unsigned long end);
336 
337 struct drm_gpusvm_range *
338 drm_gpusvm_range_find(struct drm_gpusvm_notifier *notifier, unsigned long start,
339 		      unsigned long end);
340 
341 void drm_gpusvm_range_set_unmapped(struct drm_gpusvm_range *range,
342 				   struct drm_gpusvm_pages *pages,
343 				   unsigned int pages_count,
344 				   const struct mmu_notifier_range *mmu_range);
345 
346 int drm_gpusvm_get_pages(struct drm_gpusvm *gpusvm,
347 			 struct drm_gpusvm_pages *svm_pages,
348 			 unsigned int num_pages,
349 			 struct mm_struct *mm,
350 			 struct mmu_interval_notifier *notifier,
351 			 unsigned long pages_start, unsigned long pages_end,
352 			 const struct drm_gpusvm_ctx *ctx);
353 
354 void drm_gpusvm_unmap_pages(struct drm_gpusvm *gpusvm,
355 			    struct drm_gpusvm_pages *svm_pages,
356 			    unsigned long npages,
357 			    const struct drm_gpusvm_ctx *ctx);
358 
359 void drm_gpusvm_free_pages(struct drm_gpusvm *gpusvm,
360 			   struct drm_gpusvm_pages *svm_pages,
361 			   unsigned long npages);
362 
363 /**
364  * drm_gpusvm_init_pages() - Initialize a freshly allocated drm_gpusvm_pages
365  * @svm_pages: Pointer to the drm_gpusvm_pages to initialize.
366  * @drm: The DRM device that will own DMA mappings for this pages object.
367  *
368  * Drivers that embed one or more drm_gpusvm_pages in their own range
369  * structure must call this once on each pages instance after allocation,
370  * before the first drm_gpusvm_get_pages() / unmap / free.
371  */
372 static inline void drm_gpusvm_init_pages(struct drm_gpusvm_pages *svm_pages,
373 					 struct drm_device *drm)
374 {
375 	memset(svm_pages, 0, sizeof(*svm_pages));
376 	svm_pages->drm = drm;
377 	svm_pages->notifier_seq = LONG_MAX;
378 }
379 
380 /**
381  * drm_gpusvm_pages_first_dma() - Resolve the device address array
382  * @svm_pages: Pointer to the drm_gpusvm_pages.
383  * @contiguous: Where to store whether one entry spans the whole range, or NULL
384  *
385  * drm_gpusvm_pages use unions to optimize the storage of DMA addresses,
386  * this function abstracts the access to the first device address. The driver
387  * should use this helper instead of reading dma_addr directly to prevent
388  * array out of bounds access.
389  *
390  * @contiguous comes from the same read of the flags as the array itself, so a
391  * caller cannot see the two disagree and walk past that single entry into the
392  * fields behind it. When it is set the length comes from the range rather than
393  * from the order. The order still states what one PTE may cover: the range
394  * length for a huge page, PAGE_SIZE for an IOVA mapped range of single pages.
395  *
396  * Only get_pages() and the free path switch between the two union members.
397  * Both hold the notifier lock for read, so taking that lock does not stop
398  * them; callers need the driver lock that does, which every reader of the
399  * addresses holds anyway. The notifier never touches the union, so the
400  * pointer returned here stays good and can then be used under the notifier
401  * lock.
402  *
403  * Return: Pointer to the first device address, NULL if none is populated.
404  */
405 static inline const struct drm_pagemap_addr *
406 drm_gpusvm_pages_first_dma(const struct drm_gpusvm_pages *svm_pages,
407 			   bool *contiguous)
408 {
409 	struct drm_gpusvm_pages_flags flags = {
410 		/* READ_ONCE pairs with the WRITE_ONCE of the flag writers */
411 		.__flags = READ_ONCE(svm_pages->flags.__flags),
412 	};
413 
414 	if (contiguous)
415 		*contiguous = flags.inline_dma_mapping;
416 
417 	if (flags.inline_dma_mapping)
418 		return &svm_pages->inline_addr;
419 
420 	return READ_ONCE(svm_pages->dma_addr);
421 }
422 
423 /**
424  * enum drm_gpusvm_scan_result - Scan result from the drm_gpusvm_scan_mm() function.
425  * @DRM_GPUSVM_SCAN_UNPOPULATED: At least one page was not present or inaccessible.
426  * @DRM_GPUSVM_SCAN_EQUAL: All pages belong to the struct dev_pagemap indicated as
427  * the @pagemap argument to the drm_gpusvm_scan_mm() function.
428  * @DRM_GPUSVM_SCAN_OTHER: All pages belong to exactly one dev_pagemap, which is
429  * *NOT* the @pagemap argument to the drm_gpusvm_scan_mm(). All pages belong to
430  * the same device private owner.
431  * @DRM_GPUSVM_SCAN_SYSTEM: All pages are present and system pages.
432  * @DRM_GPUSVM_SCAN_MIXED_DEVICE: All pages are device pages and belong to at least
433  * two different struct dev_pagemaps. All pages belong to the same device private
434  * owner.
435  * @DRM_GPUSVM_SCAN_MIXED: Pages are present and are a mix of system pages
436  * and device-private pages. All device-private pages belong to the same device
437  * private owner.
438  */
439 enum drm_gpusvm_scan_result {
440 	DRM_GPUSVM_SCAN_UNPOPULATED,
441 	DRM_GPUSVM_SCAN_EQUAL,
442 	DRM_GPUSVM_SCAN_OTHER,
443 	DRM_GPUSVM_SCAN_SYSTEM,
444 	DRM_GPUSVM_SCAN_MIXED_DEVICE,
445 	DRM_GPUSVM_SCAN_MIXED,
446 };
447 
448 enum drm_gpusvm_scan_result drm_gpusvm_scan_mm(struct drm_gpusvm_range *range,
449 					       void *dev_private_owner,
450 					       const struct dev_pagemap *pagemap);
451 
452 #ifdef CONFIG_LOCKDEP
453 /**
454  * drm_gpusvm_driver_set_lock() - Set the lock protecting accesses to GPU SVM
455  * @gpusvm: Pointer to the GPU SVM structure.
456  * @lock: the lock used to protect the gpuva list. The locking primitive
457  * must contain a dep_map field.
458  *
459  * Call this to annotate drm_gpusvm_range_find_or_insert and
460  * drm_gpusvm_range_remove.
461  */
462 #define drm_gpusvm_driver_set_lock(gpusvm, lock) \
463 	do { \
464 		if (!WARN((gpusvm)->lock_dep_map, \
465 			  "GPUSVM range lock should be set only once."))\
466 			(gpusvm)->lock_dep_map = &(lock)->dep_map;	\
467 	} while (0)
468 #else
469 #define drm_gpusvm_driver_set_lock(gpusvm, lock) do {} while (0)
470 #endif
471 
472 /**
473  * drm_gpusvm_notifier_lock() - Lock GPU SVM notifier
474  * @gpusvm__: Pointer to the GPU SVM structure.
475  *
476  * Abstract client usage GPU SVM notifier lock, take lock
477  */
478 #define drm_gpusvm_notifier_lock(gpusvm__)	\
479 	down_read(&(gpusvm__)->notifier_lock)
480 
481 /**
482  * drm_gpusvm_notifier_unlock() - Unlock GPU SVM notifier
483  * @gpusvm__: Pointer to the GPU SVM structure.
484  *
485  * Abstract client usage GPU SVM notifier lock, drop lock
486  */
487 #define drm_gpusvm_notifier_unlock(gpusvm__)	\
488 	up_read(&(gpusvm__)->notifier_lock)
489 
490 /**
491  * drm_gpusvm_range_start() - GPU SVM range start address
492  * @range: Pointer to the GPU SVM range
493  *
494  * Return: GPU SVM range start address
495  */
496 static inline unsigned long
497 drm_gpusvm_range_start(struct drm_gpusvm_range *range)
498 {
499 	return range->itree.start;
500 }
501 
502 /**
503  * drm_gpusvm_range_end() - GPU SVM range end address
504  * @range: Pointer to the GPU SVM range
505  *
506  * Return: GPU SVM range end address
507  */
508 static inline unsigned long
509 drm_gpusvm_range_end(struct drm_gpusvm_range *range)
510 {
511 	return range->itree.last + 1;
512 }
513 
514 /**
515  * drm_gpusvm_range_size() - GPU SVM range size
516  * @range: Pointer to the GPU SVM range
517  *
518  * Return: GPU SVM range size
519  */
520 static inline unsigned long
521 drm_gpusvm_range_size(struct drm_gpusvm_range *range)
522 {
523 	return drm_gpusvm_range_end(range) - drm_gpusvm_range_start(range);
524 }
525 
526 /**
527  * drm_gpusvm_notifier_start() - GPU SVM notifier start address
528  * @notifier: Pointer to the GPU SVM notifier
529  *
530  * Return: GPU SVM notifier start address
531  */
532 static inline unsigned long
533 drm_gpusvm_notifier_start(struct drm_gpusvm_notifier *notifier)
534 {
535 	return notifier->itree.start;
536 }
537 
538 /**
539  * drm_gpusvm_notifier_end() - GPU SVM notifier end address
540  * @notifier: Pointer to the GPU SVM notifier
541  *
542  * Return: GPU SVM notifier end address
543  */
544 static inline unsigned long
545 drm_gpusvm_notifier_end(struct drm_gpusvm_notifier *notifier)
546 {
547 	return notifier->itree.last + 1;
548 }
549 
550 /**
551  * drm_gpusvm_notifier_size() - GPU SVM notifier size
552  * @notifier: Pointer to the GPU SVM notifier
553  *
554  * Return: GPU SVM notifier size
555  */
556 static inline unsigned long
557 drm_gpusvm_notifier_size(struct drm_gpusvm_notifier *notifier)
558 {
559 	return drm_gpusvm_notifier_end(notifier) -
560 		drm_gpusvm_notifier_start(notifier);
561 }
562 
563 /**
564  * __drm_gpusvm_range_next() - Get the next GPU SVM range in the list
565  * @range: a pointer to the current GPU SVM range
566  *
567  * Return: A pointer to the next drm_gpusvm_range if available, or NULL if the
568  *         current range is the last one or if the input range is NULL.
569  */
570 static inline struct drm_gpusvm_range *
571 __drm_gpusvm_range_next(struct drm_gpusvm_range *range)
572 {
573 	if (range && !list_is_last(&range->entry,
574 				   &range->notifier->range_list))
575 		return list_next_entry(range, entry);
576 
577 	return NULL;
578 }
579 
580 /**
581  * drm_gpusvm_for_each_range() - Iterate over GPU SVM ranges in a notifier
582  * @range__: Iterator variable for the ranges. If set, it indicates the start of
583  *	     the iterator. If NULL, call drm_gpusvm_range_find() to get the range.
584  * @notifier__: Pointer to the GPU SVM notifier
585  * @start__: Start address of the range
586  * @end__: End address of the range
587  *
588  * This macro is used to iterate over GPU SVM ranges in a notifier. It is safe
589  * to use while holding the driver SVM lock or the notifier lock.
590  */
591 #define drm_gpusvm_for_each_range(range__, notifier__, start__, end__)	\
592 	for ((range__) = (range__) ?:					\
593 	     drm_gpusvm_range_find((notifier__), (start__), (end__));	\
594 	     (range__) && (drm_gpusvm_range_start(range__) < (end__));	\
595 	     (range__) = __drm_gpusvm_range_next(range__))
596 
597 /**
598  * drm_gpusvm_for_each_range_safe() - Safely iterate over GPU SVM ranges in a notifier
599  * @range__: Iterator variable for the ranges
600  * @next__: Iterator variable for the ranges temporay storage
601  * @notifier__: Pointer to the GPU SVM notifier
602  * @start__: Start address of the range
603  * @end__: End address of the range
604  *
605  * This macro is used to iterate over GPU SVM ranges in a notifier while
606  * removing ranges from it.
607  */
608 #define drm_gpusvm_for_each_range_safe(range__, next__, notifier__, start__, end__)	\
609 	for ((range__) = drm_gpusvm_range_find((notifier__), (start__), (end__)),	\
610 	     (next__) = __drm_gpusvm_range_next(range__);				\
611 	     (range__) && (drm_gpusvm_range_start(range__) < (end__));			\
612 	     (range__) = (next__), (next__) = __drm_gpusvm_range_next(range__))
613 
614 /**
615  * __drm_gpusvm_notifier_next() - get the next drm_gpusvm_notifier in the list
616  * @notifier: a pointer to the current drm_gpusvm_notifier
617  *
618  * Return: A pointer to the next drm_gpusvm_notifier if available, or NULL if
619  *         the current notifier is the last one or if the input notifier is
620  *         NULL.
621  */
622 static inline struct drm_gpusvm_notifier *
623 __drm_gpusvm_notifier_next(struct drm_gpusvm_notifier *notifier)
624 {
625 	if (notifier && !list_is_last(&notifier->entry,
626 				      &notifier->gpusvm->notifier_list))
627 		return list_next_entry(notifier, entry);
628 
629 	return NULL;
630 }
631 
632 /**
633  * drm_gpusvm_for_each_notifier() - Iterate over GPU SVM notifiers in a gpusvm
634  * @notifier__: Iterator variable for the notifiers
635  * @gpusvm__: Pointer to the GPU SVM notifier
636  * @start__: Start address of the notifier
637  * @end__: End address of the notifier
638  *
639  * This macro is used to iterate over GPU SVM notifiers in a gpusvm.
640  */
641 #define drm_gpusvm_for_each_notifier(notifier__, gpusvm__, start__, end__)		\
642 	for ((notifier__) = drm_gpusvm_notifier_find((gpusvm__), (start__), (end__));	\
643 	     (notifier__) && (drm_gpusvm_notifier_start(notifier__) < (end__));		\
644 	     (notifier__) = __drm_gpusvm_notifier_next(notifier__))
645 
646 /**
647  * drm_gpusvm_for_each_notifier_safe() - Safely iterate over GPU SVM notifiers in a gpusvm
648  * @notifier__: Iterator variable for the notifiers
649  * @next__: Iterator variable for the notifiers temporay storage
650  * @gpusvm__: Pointer to the GPU SVM notifier
651  * @start__: Start address of the notifier
652  * @end__: End address of the notifier
653  *
654  * This macro is used to iterate over GPU SVM notifiers in a gpusvm while
655  * removing notifiers from it.
656  */
657 #define drm_gpusvm_for_each_notifier_safe(notifier__, next__, gpusvm__, start__, end__)	\
658 	for ((notifier__) = drm_gpusvm_notifier_find((gpusvm__), (start__), (end__)),	\
659 	     (next__) = __drm_gpusvm_notifier_next(notifier__);				\
660 	     (notifier__) && (drm_gpusvm_notifier_start(notifier__) < (end__));		\
661 	     (notifier__) = (next__), (next__) = __drm_gpusvm_notifier_next(notifier__))
662 
663 #endif /* __DRM_GPUSVM_H__ */
664