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/kref.h> 10 #include <linux/interval_tree.h> 11 #include <linux/mmu_notifier.h> 12 13 struct dev_pagemap_ops; 14 struct drm_device; 15 struct drm_gpusvm; 16 struct drm_gpusvm_notifier; 17 struct drm_gpusvm_ops; 18 struct drm_gpusvm_range; 19 struct drm_pagemap; 20 struct drm_pagemap_addr; 21 22 /** 23 * struct drm_gpusvm_ops - Operations structure for GPU SVM 24 * 25 * This structure defines the operations for GPU Shared Virtual Memory (SVM). 26 * These operations are provided by the GPU driver to manage SVM ranges and 27 * notifiers. 28 */ 29 struct drm_gpusvm_ops { 30 /** 31 * @notifier_alloc: Allocate a GPU SVM notifier (optional) 32 * 33 * Allocate a GPU SVM notifier. 34 * 35 * Return: Pointer to the allocated GPU SVM notifier on success, NULL on failure. 36 */ 37 struct drm_gpusvm_notifier *(*notifier_alloc)(void); 38 39 /** 40 * @notifier_free: Free a GPU SVM notifier (optional) 41 * @notifier: Pointer to the GPU SVM notifier to be freed 42 * 43 * Free a GPU SVM notifier. 44 */ 45 void (*notifier_free)(struct drm_gpusvm_notifier *notifier); 46 47 /** 48 * @range_alloc: Allocate a GPU SVM range (optional) 49 * @gpusvm: Pointer to the GPU SVM 50 * 51 * Allocate a GPU SVM range. 52 * 53 * Return: Pointer to the allocated GPU SVM range on success, NULL on failure. 54 */ 55 struct drm_gpusvm_range *(*range_alloc)(struct drm_gpusvm *gpusvm); 56 57 /** 58 * @range_free: Free a GPU SVM range (optional) 59 * @range: Pointer to the GPU SVM range to be freed 60 * 61 * Free a GPU SVM range. 62 */ 63 void (*range_free)(struct drm_gpusvm_range *range); 64 65 /** 66 * @invalidate: Invalidate GPU SVM notifier (required) 67 * @gpusvm: Pointer to the GPU SVM 68 * @notifier: Pointer to the GPU SVM notifier 69 * @mmu_range: Pointer to the mmu_notifier_range structure 70 * 71 * Invalidate the GPU page tables. It can safely walk the notifier range 72 * RB tree/list in this function. Called while holding the notifier lock. 73 */ 74 void (*invalidate)(struct drm_gpusvm *gpusvm, 75 struct drm_gpusvm_notifier *notifier, 76 const struct mmu_notifier_range *mmu_range); 77 }; 78 79 /** 80 * struct drm_gpusvm_notifier - Structure representing a GPU SVM notifier 81 * 82 * @gpusvm: Pointer to the GPU SVM structure 83 * @notifier: MMU interval notifier 84 * @itree: Interval tree node for the notifier (inserted in GPU SVM) 85 * @entry: List entry to fast interval tree traversal 86 * @root: Cached root node of the RB tree containing ranges 87 * @range_list: List head containing of ranges in the same order they appear in 88 * interval tree. This is useful to keep iterating ranges while 89 * doing modifications to RB tree. 90 * @flags: Flags for notifier 91 * @flags.removed: Flag indicating whether the MMU interval notifier has been 92 * removed 93 * 94 * This structure represents a GPU SVM notifier. 95 */ 96 struct drm_gpusvm_notifier { 97 struct drm_gpusvm *gpusvm; 98 struct mmu_interval_notifier notifier; 99 struct interval_tree_node itree; 100 struct list_head entry; 101 struct rb_root_cached root; 102 struct list_head range_list; 103 struct { 104 u32 removed : 1; 105 } flags; 106 }; 107 108 /** 109 * struct drm_gpusvm_pages_flags - Structure representing a GPU SVM pages flags 110 * 111 * @migrate_devmem: Flag indicating whether the pages can be migrated to device memory 112 * @unmapped: Flag indicating if the pages has been unmapped 113 * @partial_unmap: Flag indicating if the pages has been partially unmapped 114 * @has_devmem_pages: Flag indicating if the pages has devmem pages 115 * @has_dma_mapping: Flag indicating if the pages has a 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 upon creation */ 122 u16 migrate_devmem : 1; 123 /* All flags below must be set / cleared under notifier lock */ 124 u16 unmapped : 1; 125 u16 partial_unmap : 1; 126 u16 has_devmem_pages : 1; 127 u16 has_dma_mapping : 1; 128 }; 129 u16 __flags; 130 }; 131 }; 132 133 /** 134 * struct drm_gpusvm_pages - Structure representing a GPU SVM mapped pages 135 * 136 * @dma_addr: Device address array 137 * @dpagemap: The struct drm_pagemap of the device pages we're dma-mapping. 138 * Note this is assuming only one drm_pagemap per range is allowed. 139 * @notifier_seq: Notifier sequence number of the range's pages 140 * @flags: Flags for range 141 * @flags.migrate_devmem: Flag indicating whether the range can be migrated to device memory 142 * @flags.unmapped: Flag indicating if the range has been unmapped 143 * @flags.partial_unmap: Flag indicating if the range has been partially unmapped 144 * @flags.has_devmem_pages: Flag indicating if the range has devmem pages 145 * @flags.has_dma_mapping: Flag indicating if the range has a DMA mapping 146 */ 147 struct drm_gpusvm_pages { 148 struct drm_pagemap_addr *dma_addr; 149 struct drm_pagemap *dpagemap; 150 unsigned long notifier_seq; 151 struct drm_gpusvm_pages_flags flags; 152 }; 153 154 /** 155 * struct drm_gpusvm_range - Structure representing a GPU SVM range 156 * 157 * @gpusvm: Pointer to the GPU SVM structure 158 * @notifier: Pointer to the GPU SVM notifier 159 * @refcount: Reference count for the range 160 * @itree: Interval tree node for the range (inserted in GPU SVM notifier) 161 * @entry: List entry to fast interval tree traversal 162 * @pages: The pages for this range. 163 * 164 * This structure represents a GPU SVM range used for tracking memory ranges 165 * mapped in a DRM device. 166 */ 167 struct drm_gpusvm_range { 168 struct drm_gpusvm *gpusvm; 169 struct drm_gpusvm_notifier *notifier; 170 struct kref refcount; 171 struct interval_tree_node itree; 172 struct list_head entry; 173 struct drm_gpusvm_pages pages; 174 }; 175 176 /** 177 * struct drm_gpusvm - GPU SVM structure 178 * 179 * @name: Name of the GPU SVM 180 * @drm: Pointer to the DRM device structure 181 * @mm: Pointer to the mm_struct for the address space 182 * @device_private_page_owner: Device private pages owner 183 * @mm_start: Start address of GPU SVM 184 * @mm_range: Range of the GPU SVM 185 * @notifier_size: Size of individual notifiers 186 * @ops: Pointer to the operations structure for GPU SVM 187 * @chunk_sizes: Pointer to the array of chunk sizes used in range allocation. 188 * Entries should be powers of 2 in descending order. 189 * @num_chunks: Number of chunks 190 * @notifier_lock: Read-write semaphore for protecting notifier operations 191 * @root: Cached root node of the Red-Black tree containing GPU SVM notifiers 192 * @notifier_list: list head containing of notifiers in the same order they 193 * appear in interval tree. This is useful to keep iterating 194 * notifiers while doing modifications to RB tree. 195 * 196 * This structure represents a GPU SVM (Shared Virtual Memory) used for tracking 197 * memory ranges mapped in a DRM (Direct Rendering Manager) device. 198 * 199 * No reference counting is provided, as this is expected to be embedded in the 200 * driver VM structure along with the struct drm_gpuvm, which handles reference 201 * counting. 202 */ 203 struct drm_gpusvm { 204 const char *name; 205 struct drm_device *drm; 206 struct mm_struct *mm; 207 void *device_private_page_owner; 208 unsigned long mm_start; 209 unsigned long mm_range; 210 unsigned long notifier_size; 211 const struct drm_gpusvm_ops *ops; 212 const unsigned long *chunk_sizes; 213 int num_chunks; 214 struct rw_semaphore notifier_lock; 215 struct rb_root_cached root; 216 struct list_head notifier_list; 217 #ifdef CONFIG_LOCKDEP 218 /** 219 * @lock_dep_map: Annotates drm_gpusvm_range_find_or_insert and 220 * drm_gpusvm_range_remove with a driver provided lock. 221 */ 222 struct lockdep_map *lock_dep_map; 223 #endif 224 }; 225 226 /** 227 * struct drm_gpusvm_ctx - DRM GPU SVM context 228 * 229 * @check_pages_threshold: Check CPU pages for present if chunk is less than or 230 * equal to threshold. If not present, reduce chunk 231 * size. 232 * @timeslice_ms: The timeslice MS which in minimum time a piece of memory 233 * remains with either exclusive GPU or CPU access. 234 * @in_notifier: entering from a MMU notifier 235 * @read_only: operating on read-only memory 236 * @devmem_possible: possible to use device memory 237 * @devmem_only: use only device memory 238 * 239 * Context that is DRM GPUSVM is operating in (i.e. user arguments). 240 */ 241 struct drm_gpusvm_ctx { 242 unsigned long check_pages_threshold; 243 unsigned long timeslice_ms; 244 unsigned int in_notifier :1; 245 unsigned int read_only :1; 246 unsigned int devmem_possible :1; 247 unsigned int devmem_only :1; 248 }; 249 250 int drm_gpusvm_init(struct drm_gpusvm *gpusvm, 251 const char *name, struct drm_device *drm, 252 struct mm_struct *mm, void *device_private_page_owner, 253 unsigned long mm_start, unsigned long mm_range, 254 unsigned long notifier_size, 255 const struct drm_gpusvm_ops *ops, 256 const unsigned long *chunk_sizes, int num_chunks); 257 258 void drm_gpusvm_fini(struct drm_gpusvm *gpusvm); 259 260 void drm_gpusvm_free(struct drm_gpusvm *gpusvm); 261 262 unsigned long 263 drm_gpusvm_find_vma_start(struct drm_gpusvm *gpusvm, 264 unsigned long start, 265 unsigned long end); 266 267 struct drm_gpusvm_range * 268 drm_gpusvm_range_find_or_insert(struct drm_gpusvm *gpusvm, 269 unsigned long fault_addr, 270 unsigned long gpuva_start, 271 unsigned long gpuva_end, 272 const struct drm_gpusvm_ctx *ctx); 273 274 void drm_gpusvm_range_remove(struct drm_gpusvm *gpusvm, 275 struct drm_gpusvm_range *range); 276 277 int drm_gpusvm_range_evict(struct drm_gpusvm *gpusvm, 278 struct drm_gpusvm_range *range); 279 280 struct drm_gpusvm_range * 281 drm_gpusvm_range_get(struct drm_gpusvm_range *range); 282 283 void drm_gpusvm_range_put(struct drm_gpusvm_range *range); 284 285 bool drm_gpusvm_range_pages_valid(struct drm_gpusvm *gpusvm, 286 struct drm_gpusvm_range *range); 287 288 int drm_gpusvm_range_get_pages(struct drm_gpusvm *gpusvm, 289 struct drm_gpusvm_range *range, 290 const struct drm_gpusvm_ctx *ctx); 291 292 void drm_gpusvm_range_unmap_pages(struct drm_gpusvm *gpusvm, 293 struct drm_gpusvm_range *range, 294 const struct drm_gpusvm_ctx *ctx); 295 296 bool drm_gpusvm_has_mapping(struct drm_gpusvm *gpusvm, unsigned long start, 297 unsigned long end); 298 299 struct drm_gpusvm_notifier * 300 drm_gpusvm_notifier_find(struct drm_gpusvm *gpusvm, unsigned long start, 301 unsigned long end); 302 303 struct drm_gpusvm_range * 304 drm_gpusvm_range_find(struct drm_gpusvm_notifier *notifier, unsigned long start, 305 unsigned long end); 306 307 void drm_gpusvm_range_set_unmapped(struct drm_gpusvm_range *range, 308 const struct mmu_notifier_range *mmu_range); 309 310 int drm_gpusvm_get_pages(struct drm_gpusvm *gpusvm, 311 struct drm_gpusvm_pages *svm_pages, 312 struct mm_struct *mm, 313 struct mmu_interval_notifier *notifier, 314 unsigned long pages_start, unsigned long pages_end, 315 const struct drm_gpusvm_ctx *ctx); 316 317 void drm_gpusvm_unmap_pages(struct drm_gpusvm *gpusvm, 318 struct drm_gpusvm_pages *svm_pages, 319 unsigned long npages, 320 const struct drm_gpusvm_ctx *ctx); 321 322 void drm_gpusvm_free_pages(struct drm_gpusvm *gpusvm, 323 struct drm_gpusvm_pages *svm_pages, 324 unsigned long npages); 325 326 #ifdef CONFIG_LOCKDEP 327 /** 328 * drm_gpusvm_driver_set_lock() - Set the lock protecting accesses to GPU SVM 329 * @gpusvm: Pointer to the GPU SVM structure. 330 * @lock: the lock used to protect the gpuva list. The locking primitive 331 * must contain a dep_map field. 332 * 333 * Call this to annotate drm_gpusvm_range_find_or_insert and 334 * drm_gpusvm_range_remove. 335 */ 336 #define drm_gpusvm_driver_set_lock(gpusvm, lock) \ 337 do { \ 338 if (!WARN((gpusvm)->lock_dep_map, \ 339 "GPUSVM range lock should be set only once."))\ 340 (gpusvm)->lock_dep_map = &(lock)->dep_map; \ 341 } while (0) 342 #else 343 #define drm_gpusvm_driver_set_lock(gpusvm, lock) do {} while (0) 344 #endif 345 346 /** 347 * drm_gpusvm_notifier_lock() - Lock GPU SVM notifier 348 * @gpusvm__: Pointer to the GPU SVM structure. 349 * 350 * Abstract client usage GPU SVM notifier lock, take lock 351 */ 352 #define drm_gpusvm_notifier_lock(gpusvm__) \ 353 down_read(&(gpusvm__)->notifier_lock) 354 355 /** 356 * drm_gpusvm_notifier_unlock() - Unlock GPU SVM notifier 357 * @gpusvm__: Pointer to the GPU SVM structure. 358 * 359 * Abstract client usage GPU SVM notifier lock, drop lock 360 */ 361 #define drm_gpusvm_notifier_unlock(gpusvm__) \ 362 up_read(&(gpusvm__)->notifier_lock) 363 364 /** 365 * drm_gpusvm_range_start() - GPU SVM range start address 366 * @range: Pointer to the GPU SVM range 367 * 368 * Return: GPU SVM range start address 369 */ 370 static inline unsigned long 371 drm_gpusvm_range_start(struct drm_gpusvm_range *range) 372 { 373 return range->itree.start; 374 } 375 376 /** 377 * drm_gpusvm_range_end() - GPU SVM range end address 378 * @range: Pointer to the GPU SVM range 379 * 380 * Return: GPU SVM range end address 381 */ 382 static inline unsigned long 383 drm_gpusvm_range_end(struct drm_gpusvm_range *range) 384 { 385 return range->itree.last + 1; 386 } 387 388 /** 389 * drm_gpusvm_range_size() - GPU SVM range size 390 * @range: Pointer to the GPU SVM range 391 * 392 * Return: GPU SVM range size 393 */ 394 static inline unsigned long 395 drm_gpusvm_range_size(struct drm_gpusvm_range *range) 396 { 397 return drm_gpusvm_range_end(range) - drm_gpusvm_range_start(range); 398 } 399 400 /** 401 * drm_gpusvm_notifier_start() - GPU SVM notifier start address 402 * @notifier: Pointer to the GPU SVM notifier 403 * 404 * Return: GPU SVM notifier start address 405 */ 406 static inline unsigned long 407 drm_gpusvm_notifier_start(struct drm_gpusvm_notifier *notifier) 408 { 409 return notifier->itree.start; 410 } 411 412 /** 413 * drm_gpusvm_notifier_end() - GPU SVM notifier end address 414 * @notifier: Pointer to the GPU SVM notifier 415 * 416 * Return: GPU SVM notifier end address 417 */ 418 static inline unsigned long 419 drm_gpusvm_notifier_end(struct drm_gpusvm_notifier *notifier) 420 { 421 return notifier->itree.last + 1; 422 } 423 424 /** 425 * drm_gpusvm_notifier_size() - GPU SVM notifier size 426 * @notifier: Pointer to the GPU SVM notifier 427 * 428 * Return: GPU SVM notifier size 429 */ 430 static inline unsigned long 431 drm_gpusvm_notifier_size(struct drm_gpusvm_notifier *notifier) 432 { 433 return drm_gpusvm_notifier_end(notifier) - 434 drm_gpusvm_notifier_start(notifier); 435 } 436 437 /** 438 * __drm_gpusvm_range_next() - Get the next GPU SVM range in the list 439 * @range: a pointer to the current GPU SVM range 440 * 441 * Return: A pointer to the next drm_gpusvm_range if available, or NULL if the 442 * current range is the last one or if the input range is NULL. 443 */ 444 static inline struct drm_gpusvm_range * 445 __drm_gpusvm_range_next(struct drm_gpusvm_range *range) 446 { 447 if (range && !list_is_last(&range->entry, 448 &range->notifier->range_list)) 449 return list_next_entry(range, entry); 450 451 return NULL; 452 } 453 454 /** 455 * drm_gpusvm_for_each_range() - Iterate over GPU SVM ranges in a notifier 456 * @range__: Iterator variable for the ranges. If set, it indicates the start of 457 * the iterator. If NULL, call drm_gpusvm_range_find() to get the range. 458 * @notifier__: Pointer to the GPU SVM notifier 459 * @start__: Start address of the range 460 * @end__: End address of the range 461 * 462 * This macro is used to iterate over GPU SVM ranges in a notifier. It is safe 463 * to use while holding the driver SVM lock or the notifier lock. 464 */ 465 #define drm_gpusvm_for_each_range(range__, notifier__, start__, end__) \ 466 for ((range__) = (range__) ?: \ 467 drm_gpusvm_range_find((notifier__), (start__), (end__)); \ 468 (range__) && (drm_gpusvm_range_start(range__) < (end__)); \ 469 (range__) = __drm_gpusvm_range_next(range__)) 470 471 /** 472 * drm_gpusvm_for_each_range_safe() - Safely iterate over GPU SVM ranges in a notifier 473 * @range__: Iterator variable for the ranges 474 * @next__: Iterator variable for the ranges temporay storage 475 * @notifier__: Pointer to the GPU SVM notifier 476 * @start__: Start address of the range 477 * @end__: End address of the range 478 * 479 * This macro is used to iterate over GPU SVM ranges in a notifier while 480 * removing ranges from it. 481 */ 482 #define drm_gpusvm_for_each_range_safe(range__, next__, notifier__, start__, end__) \ 483 for ((range__) = drm_gpusvm_range_find((notifier__), (start__), (end__)), \ 484 (next__) = __drm_gpusvm_range_next(range__); \ 485 (range__) && (drm_gpusvm_range_start(range__) < (end__)); \ 486 (range__) = (next__), (next__) = __drm_gpusvm_range_next(range__)) 487 488 /** 489 * __drm_gpusvm_notifier_next() - get the next drm_gpusvm_notifier in the list 490 * @notifier: a pointer to the current drm_gpusvm_notifier 491 * 492 * Return: A pointer to the next drm_gpusvm_notifier if available, or NULL if 493 * the current notifier is the last one or if the input notifier is 494 * NULL. 495 */ 496 static inline struct drm_gpusvm_notifier * 497 __drm_gpusvm_notifier_next(struct drm_gpusvm_notifier *notifier) 498 { 499 if (notifier && !list_is_last(¬ifier->entry, 500 ¬ifier->gpusvm->notifier_list)) 501 return list_next_entry(notifier, entry); 502 503 return NULL; 504 } 505 506 /** 507 * drm_gpusvm_for_each_notifier() - Iterate over GPU SVM notifiers in a gpusvm 508 * @notifier__: Iterator variable for the notifiers 509 * @gpusvm__: Pointer to the GPU SVM notifier 510 * @start__: Start address of the notifier 511 * @end__: End address of the notifier 512 * 513 * This macro is used to iterate over GPU SVM notifiers in a gpusvm. 514 */ 515 #define drm_gpusvm_for_each_notifier(notifier__, gpusvm__, start__, end__) \ 516 for ((notifier__) = drm_gpusvm_notifier_find((gpusvm__), (start__), (end__)); \ 517 (notifier__) && (drm_gpusvm_notifier_start(notifier__) < (end__)); \ 518 (notifier__) = __drm_gpusvm_notifier_next(notifier__)) 519 520 /** 521 * drm_gpusvm_for_each_notifier_safe() - Safely iterate over GPU SVM notifiers in a gpusvm 522 * @notifier__: Iterator variable for the notifiers 523 * @next__: Iterator variable for the notifiers temporay storage 524 * @gpusvm__: Pointer to the GPU SVM notifier 525 * @start__: Start address of the notifier 526 * @end__: End address of the notifier 527 * 528 * This macro is used to iterate over GPU SVM notifiers in a gpusvm while 529 * removing notifiers from it. 530 */ 531 #define drm_gpusvm_for_each_notifier_safe(notifier__, next__, gpusvm__, start__, end__) \ 532 for ((notifier__) = drm_gpusvm_notifier_find((gpusvm__), (start__), (end__)), \ 533 (next__) = __drm_gpusvm_notifier_next(notifier__); \ 534 (notifier__) && (drm_gpusvm_notifier_start(notifier__) < (end__)); \ 535 (notifier__) = (next__), (next__) = __drm_gpusvm_notifier_next(notifier__)) 536 537 #endif /* __DRM_GPUSVM_H__ */ 538