1 // SPDX-License-Identifier: GPL-2.0-only OR MIT 2 /* 3 * Copyright (c) 2022 Red Hat. 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining a 6 * copy of this software and associated documentation files (the "Software"), 7 * to deal in the Software without restriction, including without limitation 8 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 9 * and/or sell copies of the Software, and to permit persons to whom the 10 * Software is furnished to do so, subject to the following conditions: 11 * 12 * The above copyright notice and this permission notice shall be included in 13 * all copies or substantial portions of the Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 21 * OTHER DEALINGS IN THE SOFTWARE. 22 * 23 * Authors: 24 * Danilo Krummrich <dakr@redhat.com> 25 * 26 */ 27 28 #include <drm/drm_gpuvm.h> 29 #include <drm/drm_print.h> 30 31 #include <linux/export.h> 32 #include <linux/interval_tree_generic.h> 33 #include <linux/mm.h> 34 35 /** 36 * DOC: Overview 37 * 38 * The DRM GPU VA Manager, represented by struct drm_gpuvm keeps track of a 39 * GPU's virtual address (VA) space and manages the corresponding virtual 40 * mappings represented by &drm_gpuva objects. It also keeps track of the 41 * mapping's backing &drm_gem_object buffers. 42 * 43 * &drm_gem_object buffers maintain a list of &drm_gpuva objects representing 44 * all existing GPU VA mappings using this &drm_gem_object as backing buffer. 45 * 46 * GPU VAs can be flagged as sparse, such that drivers may use GPU VAs to also 47 * keep track of sparse PTEs in order to support Vulkan 'Sparse Resources'. 48 * 49 * The GPU VA manager internally uses a rb-tree to manage the 50 * &drm_gpuva mappings within a GPU's virtual address space. 51 * 52 * The &drm_gpuvm structure contains a special &drm_gpuva representing the 53 * portion of VA space reserved by the kernel. This node is initialized together 54 * with the GPU VA manager instance and removed when the GPU VA manager is 55 * destroyed. 56 * 57 * In a typical application drivers would embed struct drm_gpuvm and 58 * struct drm_gpuva within their own driver specific structures, there won't be 59 * any memory allocations of its own nor memory allocations of &drm_gpuva 60 * entries. 61 * 62 * The data structures needed to store &drm_gpuvas within the &drm_gpuvm are 63 * contained within struct drm_gpuva already. Hence, for inserting &drm_gpuva 64 * entries from within dma-fence signalling critical sections it is enough to 65 * pre-allocate the &drm_gpuva structures. 66 * 67 * &drm_gem_objects which are private to a single VM can share a common 68 * &dma_resv in order to improve locking efficiency (e.g. with &drm_exec). 69 * For this purpose drivers must pass a &drm_gem_object to drm_gpuvm_init(), in 70 * the following called 'resv object', which serves as the container of the 71 * GPUVM's shared &dma_resv. This resv object can be a driver specific 72 * &drm_gem_object, such as the &drm_gem_object containing the root page table, 73 * but it can also be a 'dummy' object, which can be allocated with 74 * drm_gpuvm_resv_object_alloc(). 75 * 76 * In order to connect a struct drm_gpuva to its backing &drm_gem_object each 77 * &drm_gem_object maintains a list of &drm_gpuvm_bo structures, and each 78 * &drm_gpuvm_bo contains a list of &drm_gpuva structures. 79 * 80 * A &drm_gpuvm_bo is an abstraction that represents a combination of a 81 * &drm_gpuvm and a &drm_gem_object. Every such combination should be unique. 82 * This is ensured by the API through drm_gpuvm_bo_obtain() and 83 * drm_gpuvm_bo_obtain_prealloc() which first look into the corresponding 84 * &drm_gem_object list of &drm_gpuvm_bos for an existing instance of this 85 * particular combination. If not present, a new instance is created and linked 86 * to the &drm_gem_object. 87 * 88 * &drm_gpuvm_bo structures, since unique for a given &drm_gpuvm, are also used 89 * as entry for the &drm_gpuvm's lists of external and evicted objects. Those 90 * lists are maintained in order to accelerate locking of dma-resv locks and 91 * validation of evicted objects bound in a &drm_gpuvm. For instance, all 92 * &drm_gem_object's &dma_resv of a given &drm_gpuvm can be locked by calling 93 * drm_gpuvm_exec_lock(). Once locked drivers can call drm_gpuvm_validate() in 94 * order to validate all evicted &drm_gem_objects. It is also possible to lock 95 * additional &drm_gem_objects by providing the corresponding parameters to 96 * drm_gpuvm_exec_lock() as well as open code the &drm_exec loop while making 97 * use of helper functions such as drm_gpuvm_prepare_range() or 98 * drm_gpuvm_prepare_objects(). 99 * 100 * Every bound &drm_gem_object is treated as external object when its &dma_resv 101 * structure is different than the &drm_gpuvm's common &dma_resv structure. 102 */ 103 104 /** 105 * DOC: Split and Merge 106 * 107 * Besides its capability to manage and represent a GPU VA space, the 108 * GPU VA manager also provides functions to let the &drm_gpuvm calculate a 109 * sequence of operations to satisfy a given map or unmap request. 110 * 111 * Therefore the DRM GPU VA manager provides an algorithm implementing splitting 112 * and merging of existing GPU VA mappings with the ones that are requested to 113 * be mapped or unmapped. This feature is required by the Vulkan API to 114 * implement Vulkan 'Sparse Memory Bindings' - drivers UAPIs often refer to this 115 * as VM BIND. 116 * 117 * Drivers can call drm_gpuvm_sm_map() to receive a sequence of callbacks 118 * containing map, unmap and remap operations for a given newly requested 119 * mapping. The sequence of callbacks represents the set of operations to 120 * execute in order to integrate the new mapping cleanly into the current state 121 * of the GPU VA space. 122 * 123 * Depending on how the new GPU VA mapping intersects with the existing mappings 124 * of the GPU VA space the &drm_gpuvm_ops callbacks contain an arbitrary amount 125 * of unmap operations, a maximum of two remap operations and a single map 126 * operation. The caller might receive no callback at all if no operation is 127 * required, e.g. if the requested mapping already exists in the exact same way. 128 * 129 * The single map operation represents the original map operation requested by 130 * the caller. 131 * 132 * &drm_gpuva_op_unmap contains a 'keep' field, which indicates whether the 133 * &drm_gpuva to unmap is physically contiguous with the original mapping 134 * request. Optionally, if 'keep' is set, drivers may keep the actual page table 135 * entries for this &drm_gpuva, adding the missing page table entries only and 136 * update the &drm_gpuvm's view of things accordingly. 137 * 138 * Drivers may do the same optimization, namely delta page table updates, also 139 * for remap operations. This is possible since &drm_gpuva_op_remap consists of 140 * one unmap operation and one or two map operations, such that drivers can 141 * derive the page table update delta accordingly. 142 * 143 * Note that there can't be more than two existing mappings to split up, one at 144 * the beginning and one at the end of the new mapping, hence there is a 145 * maximum of two remap operations. 146 * 147 * Analogous to drm_gpuvm_sm_map() drm_gpuvm_sm_unmap() uses &drm_gpuvm_ops to 148 * call back into the driver in order to unmap a range of GPU VA space. The 149 * logic behind this function is way simpler though: For all existing mappings 150 * enclosed by the given range unmap operations are created. For mappings which 151 * are only partially located within the given range, remap operations are 152 * created such that those mappings are split up and re-mapped partially. 153 * 154 * As an alternative to drm_gpuvm_sm_map() and drm_gpuvm_sm_unmap(), 155 * drm_gpuvm_sm_map_ops_create() and drm_gpuvm_sm_unmap_ops_create() can be used 156 * to directly obtain an instance of struct drm_gpuva_ops containing a list of 157 * &drm_gpuva_op, which can be iterated with drm_gpuva_for_each_op(). This list 158 * contains the &drm_gpuva_ops analogous to the callbacks one would receive when 159 * calling drm_gpuvm_sm_map() or drm_gpuvm_sm_unmap(). While this way requires 160 * more memory (to allocate the &drm_gpuva_ops), it provides drivers a way to 161 * iterate the &drm_gpuva_op multiple times, e.g. once in a context where memory 162 * allocations are possible (e.g. to allocate GPU page tables) and once in the 163 * dma-fence signalling critical path. 164 * 165 * To update the &drm_gpuvm's view of the GPU VA space drm_gpuva_insert() and 166 * drm_gpuva_remove() may be used. These functions can safely be used from 167 * &drm_gpuvm_ops callbacks originating from drm_gpuvm_sm_map() or 168 * drm_gpuvm_sm_unmap(). However, it might be more convenient to use the 169 * provided helper functions drm_gpuva_map(), drm_gpuva_remap() and 170 * drm_gpuva_unmap() instead. 171 * 172 * The following diagram depicts the basic relationships of existing GPU VA 173 * mappings, a newly requested mapping and the resulting mappings as implemented 174 * by drm_gpuvm_sm_map() - it doesn't cover any arbitrary combinations of these. 175 * 176 * 1) Requested mapping is identical. Replace it, but indicate the backing PTEs 177 * could be kept. 178 * 179 * :: 180 * 181 * 0 a 1 182 * old: |-----------| (bo_offset=n) 183 * 184 * 0 a 1 185 * req: |-----------| (bo_offset=n) 186 * 187 * 0 a 1 188 * new: |-----------| (bo_offset=n) 189 * 190 * 191 * 2) Requested mapping is identical, except for the BO offset, hence replace 192 * the mapping. 193 * 194 * :: 195 * 196 * 0 a 1 197 * old: |-----------| (bo_offset=n) 198 * 199 * 0 a 1 200 * req: |-----------| (bo_offset=m) 201 * 202 * 0 a 1 203 * new: |-----------| (bo_offset=m) 204 * 205 * 206 * 3) Requested mapping is identical, except for the backing BO, hence replace 207 * the mapping. 208 * 209 * :: 210 * 211 * 0 a 1 212 * old: |-----------| (bo_offset=n) 213 * 214 * 0 b 1 215 * req: |-----------| (bo_offset=n) 216 * 217 * 0 b 1 218 * new: |-----------| (bo_offset=n) 219 * 220 * 221 * 4) Existent mapping is a left aligned subset of the requested one, hence 222 * replace the existing one. 223 * 224 * :: 225 * 226 * 0 a 1 227 * old: |-----| (bo_offset=n) 228 * 229 * 0 a 2 230 * req: |-----------| (bo_offset=n) 231 * 232 * 0 a 2 233 * new: |-----------| (bo_offset=n) 234 * 235 * .. note:: 236 * We expect to see the same result for a request with a different BO 237 * and/or non-contiguous BO offset. 238 * 239 * 240 * 5) Requested mapping's range is a left aligned subset of the existing one, 241 * but backed by a different BO. Hence, map the requested mapping and split 242 * the existing one adjusting its BO offset. 243 * 244 * :: 245 * 246 * 0 a 2 247 * old: |-----------| (bo_offset=n) 248 * 249 * 0 b 1 250 * req: |-----| (bo_offset=n) 251 * 252 * 0 b 1 a' 2 253 * new: |-----|-----| (b.bo_offset=n, a.bo_offset=n+1) 254 * 255 * .. note:: 256 * We expect to see the same result for a request with a different BO 257 * and/or non-contiguous BO offset. 258 * 259 * 260 * 6) Existent mapping is a superset of the requested mapping. Split it up, but 261 * indicate that the backing PTEs could be kept. 262 * 263 * :: 264 * 265 * 0 a 2 266 * old: |-----------| (bo_offset=n) 267 * 268 * 0 a 1 269 * req: |-----| (bo_offset=n) 270 * 271 * 0 a 1 a' 2 272 * new: |-----|-----| (a.bo_offset=n, a'.bo_offset=n+1) 273 * 274 * 275 * 7) Requested mapping's range is a right aligned subset of the existing one, 276 * but backed by a different BO. Hence, map the requested mapping and split 277 * the existing one, without adjusting the BO offset. 278 * 279 * :: 280 * 281 * 0 a 2 282 * old: |-----------| (bo_offset=n) 283 * 284 * 1 b 2 285 * req: |-----| (bo_offset=m) 286 * 287 * 0 a 1 b 2 288 * new: |-----|-----| (a.bo_offset=n,b.bo_offset=m) 289 * 290 * 291 * 8) Existent mapping is a superset of the requested mapping. Split it up, but 292 * indicate that the backing PTEs could be kept. 293 * 294 * :: 295 * 296 * 0 a 2 297 * old: |-----------| (bo_offset=n) 298 * 299 * 1 a 2 300 * req: |-----| (bo_offset=n+1) 301 * 302 * 0 a' 1 a 2 303 * new: |-----|-----| (a'.bo_offset=n, a.bo_offset=n+1) 304 * 305 * 306 * 9) Existent mapping is overlapped at the end by the requested mapping backed 307 * by a different BO. Hence, map the requested mapping and split up the 308 * existing one, without adjusting the BO offset. 309 * 310 * :: 311 * 312 * 0 a 2 313 * old: |-----------| (bo_offset=n) 314 * 315 * 1 b 3 316 * req: |-----------| (bo_offset=m) 317 * 318 * 0 a 1 b 3 319 * new: |-----|-----------| (a.bo_offset=n,b.bo_offset=m) 320 * 321 * 322 * 10) Existent mapping is overlapped by the requested mapping, both having the 323 * same backing BO with a contiguous offset. Indicate the backing PTEs of 324 * the old mapping could be kept. 325 * 326 * :: 327 * 328 * 0 a 2 329 * old: |-----------| (bo_offset=n) 330 * 331 * 1 a 3 332 * req: |-----------| (bo_offset=n+1) 333 * 334 * 0 a' 1 a 3 335 * new: |-----|-----------| (a'.bo_offset=n, a.bo_offset=n+1) 336 * 337 * 338 * 11) Requested mapping's range is a centered subset of the existing one 339 * having a different backing BO. Hence, map the requested mapping and split 340 * up the existing one in two mappings, adjusting the BO offset of the right 341 * one accordingly. 342 * 343 * :: 344 * 345 * 0 a 3 346 * old: |-----------------| (bo_offset=n) 347 * 348 * 1 b 2 349 * req: |-----| (bo_offset=m) 350 * 351 * 0 a 1 b 2 a' 3 352 * new: |-----|-----|-----| (a.bo_offset=n,b.bo_offset=m,a'.bo_offset=n+2) 353 * 354 * 355 * 12) Requested mapping is a contiguous subset of the existing one. Split it 356 * up, but indicate that the backing PTEs could be kept. 357 * 358 * :: 359 * 360 * 0 a 3 361 * old: |-----------------| (bo_offset=n) 362 * 363 * 1 a 2 364 * req: |-----| (bo_offset=n+1) 365 * 366 * 0 a' 1 a 2 a'' 3 367 * old: |-----|-----|-----| (a'.bo_offset=n, a.bo_offset=n+1, a''.bo_offset=n+2) 368 * 369 * 370 * 13) Existent mapping is a right aligned subset of the requested one, hence 371 * replace the existing one. 372 * 373 * :: 374 * 375 * 1 a 2 376 * old: |-----| (bo_offset=n+1) 377 * 378 * 0 a 2 379 * req: |-----------| (bo_offset=n) 380 * 381 * 0 a 2 382 * new: |-----------| (bo_offset=n) 383 * 384 * .. note:: 385 * We expect to see the same result for a request with a different bo 386 * and/or non-contiguous bo_offset. 387 * 388 * 389 * 14) Existent mapping is a centered subset of the requested one, hence 390 * replace the existing one. 391 * 392 * :: 393 * 394 * 1 a 2 395 * old: |-----| (bo_offset=n+1) 396 * 397 * 0 a 3 398 * req: |----------------| (bo_offset=n) 399 * 400 * 0 a 3 401 * new: |----------------| (bo_offset=n) 402 * 403 * .. note:: 404 * We expect to see the same result for a request with a different bo 405 * and/or non-contiguous bo_offset. 406 * 407 * 408 * 15) Existent mappings is overlapped at the beginning by the requested mapping 409 * backed by a different BO. Hence, map the requested mapping and split up 410 * the existing one, adjusting its BO offset accordingly. 411 * 412 * :: 413 * 414 * 1 a 3 415 * old: |-----------| (bo_offset=n) 416 * 417 * 0 b 2 418 * req: |-----------| (bo_offset=m) 419 * 420 * 0 b 2 a' 3 421 * new: |-----------|-----| (b.bo_offset=m,a.bo_offset=n+2) 422 */ 423 424 /** 425 * DOC: Madvise Logic - Splitting and Traversal 426 * 427 * This logic handles GPU VA range updates by generating remap and map operations 428 * without performing unmaps or merging existing mappings. 429 * 430 * 1) The requested range lies entirely within a single drm_gpuva. The logic splits 431 * the existing mapping at the start and end boundaries and inserts a new map. 432 * 433 * :: 434 * a start end b 435 * pre: |-----------------------| 436 * drm_gpuva1 437 * 438 * a start end b 439 * new: |-----|=========|-------| 440 * remap map remap 441 * 442 * one REMAP and one MAP : Same behaviour as SPLIT and MERGE 443 * 444 * 2) The requested range spans multiple drm_gpuva regions. The logic traverses 445 * across boundaries, remapping the start and end segments, and inserting two 446 * map operations to cover the full range. 447 * 448 * :: a start b c end d 449 * pre: |------------------|--------------|------------------| 450 * drm_gpuva1 drm_gpuva2 drm_gpuva3 451 * 452 * a start b c end d 453 * new: |-------|==========|--------------|========|---------| 454 * remap1 map1 drm_gpuva2 map2 remap2 455 * 456 * two REMAPS and two MAPS 457 * 458 * 3) Either start or end lies within a drm_gpuva. A single remap and map operation 459 * are generated to update the affected portion. 460 * 461 * 462 * :: a/start b c end d 463 * pre: |------------------|--------------|------------------| 464 * drm_gpuva1 drm_gpuva2 drm_gpuva3 465 * 466 * a/start b c end d 467 * new: |------------------|--------------|========|---------| 468 * drm_gpuva1 drm_gpuva2 map1 remap1 469 * 470 * :: a start b c/end d 471 * pre: |------------------|--------------|------------------| 472 * drm_gpuva1 drm_gpuva2 drm_gpuva3 473 * 474 * a start b c/end d 475 * new: |-------|==========|--------------|------------------| 476 * remap1 map1 drm_gpuva2 drm_gpuva3 477 * 478 * one REMAP and one MAP 479 * 480 * 4) Both start and end align with existing drm_gpuva boundaries. No operations 481 * are needed as the range is already covered. 482 * 483 * 5) No existing drm_gpuvas. No operations. 484 * 485 * Unlike drm_gpuvm_sm_map_ops_create, this logic avoids unmaps and merging, 486 * focusing solely on remap and map operations for efficient traversal and update. 487 */ 488 489 /** 490 * DOC: Locking 491 * 492 * In terms of managing &drm_gpuva entries DRM GPUVM does not take care of 493 * locking itself, it is the drivers responsibility to take care about locking. 494 * Drivers might want to protect the following operations: inserting, removing 495 * and iterating &drm_gpuva objects as well as generating all kinds of 496 * operations, such as split / merge or prefetch. 497 * 498 * DRM GPUVM also does not take care of the locking of the backing 499 * &drm_gem_object buffers GPU VA lists and &drm_gpuvm_bo abstractions by 500 * itself; drivers are responsible to enforce mutual exclusion using either the 501 * GEMs dma_resv lock or the GEMs gpuva.lock mutex. 502 * 503 * However, DRM GPUVM contains lockdep checks to ensure callers of its API hold 504 * the corresponding lock whenever the &drm_gem_objects GPU VA list is accessed 505 * by functions such as drm_gpuva_link() or drm_gpuva_unlink(), but also 506 * drm_gpuvm_bo_obtain() and drm_gpuvm_bo_put(). 507 * 508 * The latter is required since on creation and destruction of a &drm_gpuvm_bo 509 * the &drm_gpuvm_bo is attached / removed from the &drm_gem_objects gpuva list. 510 * Subsequent calls to drm_gpuvm_bo_obtain() for the same &drm_gpuvm and 511 * &drm_gem_object must be able to observe previous creations and destructions 512 * of &drm_gpuvm_bos in order to keep instances unique. 513 * 514 * The &drm_gpuvm's lists for keeping track of external and evicted objects are 515 * protected against concurrent insertion / removal and iteration internally. 516 * 517 * However, drivers still need ensure to protect concurrent calls to functions 518 * iterating those lists, namely drm_gpuvm_prepare_objects() and 519 * drm_gpuvm_validate(). 520 * 521 * Alternatively, drivers can set the &DRM_GPUVM_RESV_PROTECTED flag to indicate 522 * that the corresponding &dma_resv locks are held in order to protect the 523 * lists. If &DRM_GPUVM_RESV_PROTECTED is set, internal locking is disabled and 524 * the corresponding lockdep checks are enabled. This is an optimization for 525 * drivers which are capable of taking the corresponding &dma_resv locks and 526 * hence do not require internal locking. 527 */ 528 529 /** 530 * DOC: Examples 531 * 532 * This section gives two examples on how to let the DRM GPUVA Manager generate 533 * &drm_gpuva_op in order to satisfy a given map or unmap request and how to 534 * make use of them. 535 * 536 * The below code is strictly limited to illustrate the generic usage pattern. 537 * To maintain simplicity, it doesn't make use of any abstractions for common 538 * code, different (asynchronous) stages with fence signalling critical paths, 539 * any other helpers or error handling in terms of freeing memory and dropping 540 * previously taken locks. 541 * 542 * 1) Obtain a list of &drm_gpuva_op to create a new mapping:: 543 * 544 * // Allocates a new &drm_gpuva. 545 * struct drm_gpuva * driver_gpuva_alloc(void); 546 * 547 * // Typically drivers would embed the &drm_gpuvm and &drm_gpuva 548 * // structure in individual driver structures and lock the dma-resv with 549 * // drm_exec or similar helpers. 550 * int driver_mapping_create(struct drm_gpuvm *gpuvm, 551 * u64 addr, u64 range, 552 * struct drm_gem_object *obj, u64 offset) 553 * { 554 * struct drm_gpuvm_map_req map_req = { 555 * .map.va.addr = addr, 556 * .map.va.range = range, 557 * .map.gem.obj = obj, 558 * .map.gem.offset = offset, 559 * }; 560 * struct drm_gpuva_ops *ops; 561 * struct drm_gpuva_op *op 562 * struct drm_gpuvm_bo *vm_bo; 563 * 564 * driver_lock_va_space(); 565 * ops = drm_gpuvm_sm_map_ops_create(gpuvm, &map_req); 566 * if (IS_ERR(ops)) 567 * return PTR_ERR(ops); 568 * 569 * vm_bo = drm_gpuvm_bo_obtain(gpuvm, obj); 570 * if (IS_ERR(vm_bo)) 571 * return PTR_ERR(vm_bo); 572 * 573 * drm_gpuva_for_each_op(op, ops) { 574 * struct drm_gpuva *va; 575 * 576 * switch (op->op) { 577 * case DRM_GPUVA_OP_MAP: 578 * va = driver_gpuva_alloc(); 579 * if (!va) 580 * ; // unwind previous VA space updates, 581 * // free memory and unlock 582 * 583 * driver_vm_map(); 584 * drm_gpuva_map(gpuvm, va, &op->map); 585 * drm_gpuva_link(va, vm_bo); 586 * 587 * break; 588 * case DRM_GPUVA_OP_REMAP: { 589 * struct drm_gpuva *prev = NULL, *next = NULL; 590 * 591 * va = op->remap.unmap->va; 592 * 593 * if (op->remap.prev) { 594 * prev = driver_gpuva_alloc(); 595 * if (!prev) 596 * ; // unwind previous VA space 597 * // updates, free memory and 598 * // unlock 599 * } 600 * 601 * if (op->remap.next) { 602 * next = driver_gpuva_alloc(); 603 * if (!next) 604 * ; // unwind previous VA space 605 * // updates, free memory and 606 * // unlock 607 * } 608 * 609 * driver_vm_remap(); 610 * drm_gpuva_remap(prev, next, &op->remap); 611 * 612 * if (prev) 613 * drm_gpuva_link(prev, va->vm_bo); 614 * if (next) 615 * drm_gpuva_link(next, va->vm_bo); 616 * drm_gpuva_unlink(va); 617 * 618 * break; 619 * } 620 * case DRM_GPUVA_OP_UNMAP: 621 * va = op->unmap->va; 622 * 623 * driver_vm_unmap(); 624 * drm_gpuva_unlink(va); 625 * drm_gpuva_unmap(&op->unmap); 626 * 627 * break; 628 * default: 629 * break; 630 * } 631 * } 632 * drm_gpuvm_bo_put(vm_bo); 633 * driver_unlock_va_space(); 634 * 635 * return 0; 636 * } 637 * 638 * 2) Receive a callback for each &drm_gpuva_op to create a new mapping:: 639 * 640 * struct driver_context { 641 * struct drm_gpuvm *gpuvm; 642 * struct drm_gpuvm_bo *vm_bo; 643 * struct drm_gpuva *new_va; 644 * struct drm_gpuva *prev_va; 645 * struct drm_gpuva *next_va; 646 * }; 647 * 648 * // ops to pass to drm_gpuvm_init() 649 * static const struct drm_gpuvm_ops driver_gpuvm_ops = { 650 * .sm_step_map = driver_gpuva_map, 651 * .sm_step_remap = driver_gpuva_remap, 652 * .sm_step_unmap = driver_gpuva_unmap, 653 * }; 654 * 655 * // Typically drivers would embed the &drm_gpuvm and &drm_gpuva 656 * // structure in individual driver structures and lock the dma-resv with 657 * // drm_exec or similar helpers. 658 * int driver_mapping_create(struct drm_gpuvm *gpuvm, 659 * u64 addr, u64 range, 660 * struct drm_gem_object *obj, u64 offset) 661 * { 662 * struct driver_context ctx; 663 * struct drm_gpuvm_bo *vm_bo; 664 * struct drm_gpuva_ops *ops; 665 * struct drm_gpuva_op *op; 666 * int ret = 0; 667 * 668 * ctx.gpuvm = gpuvm; 669 * 670 * ctx.new_va = kzalloc(sizeof(*ctx.new_va), GFP_KERNEL); 671 * ctx.prev_va = kzalloc(sizeof(*ctx.prev_va), GFP_KERNEL); 672 * ctx.next_va = kzalloc(sizeof(*ctx.next_va), GFP_KERNEL); 673 * ctx.vm_bo = drm_gpuvm_bo_create(gpuvm, obj); 674 * if (!ctx.new_va || !ctx.prev_va || !ctx.next_va || !vm_bo) { 675 * ret = -ENOMEM; 676 * goto out; 677 * } 678 * 679 * // Typically protected with a driver specific GEM gpuva lock 680 * // used in the fence signaling path for drm_gpuva_link() and 681 * // drm_gpuva_unlink(), hence pre-allocate. 682 * ctx.vm_bo = drm_gpuvm_bo_obtain_prealloc(ctx.vm_bo); 683 * 684 * driver_lock_va_space(); 685 * ret = drm_gpuvm_sm_map(gpuvm, &ctx, addr, range, obj, offset); 686 * driver_unlock_va_space(); 687 * 688 * out: 689 * drm_gpuvm_bo_put(ctx.vm_bo); 690 * kfree(ctx.new_va); 691 * kfree(ctx.prev_va); 692 * kfree(ctx.next_va); 693 * return ret; 694 * } 695 * 696 * int driver_gpuva_map(struct drm_gpuva_op *op, void *__ctx) 697 * { 698 * struct driver_context *ctx = __ctx; 699 * 700 * drm_gpuva_map(ctx->vm, ctx->new_va, &op->map); 701 * 702 * drm_gpuva_link(ctx->new_va, ctx->vm_bo); 703 * 704 * // prevent the new GPUVA from being freed in 705 * // driver_mapping_create() 706 * ctx->new_va = NULL; 707 * 708 * return 0; 709 * } 710 * 711 * int driver_gpuva_remap(struct drm_gpuva_op *op, void *__ctx) 712 * { 713 * struct driver_context *ctx = __ctx; 714 * struct drm_gpuva *va = op->remap.unmap->va; 715 * 716 * drm_gpuva_remap(ctx->prev_va, ctx->next_va, &op->remap); 717 * 718 * if (op->remap.prev) { 719 * drm_gpuva_link(ctx->prev_va, va->vm_bo); 720 * ctx->prev_va = NULL; 721 * } 722 * 723 * if (op->remap.next) { 724 * drm_gpuva_link(ctx->next_va, va->vm_bo); 725 * ctx->next_va = NULL; 726 * } 727 * 728 * drm_gpuva_unlink(va); 729 * kfree(va); 730 * 731 * return 0; 732 * } 733 * 734 * int driver_gpuva_unmap(struct drm_gpuva_op *op, void *__ctx) 735 * { 736 * drm_gpuva_unlink(op->unmap.va); 737 * drm_gpuva_unmap(&op->unmap); 738 * kfree(op->unmap.va); 739 * 740 * return 0; 741 * } 742 */ 743 744 /** 745 * get_next_vm_bo_from_list() - get the next vm_bo element 746 * @__gpuvm: the &drm_gpuvm 747 * @__list_name: the name of the list we're iterating on 748 * @__local_list: a pointer to the local list used to store already iterated items 749 * @__prev_vm_bo: the previous element we got from get_next_vm_bo_from_list() 750 * 751 * This helper is here to provide lockless list iteration. Lockless as in, the 752 * iterator releases the lock immediately after picking the first element from 753 * the list, so list insertion and deletion can happen concurrently. 754 * 755 * Elements popped from the original list are kept in a local list, so removal 756 * and is_empty checks can still happen while we're iterating the list. 757 */ 758 #define get_next_vm_bo_from_list(__gpuvm, __list_name, __local_list, __prev_vm_bo) \ 759 ({ \ 760 struct drm_gpuvm_bo *__vm_bo = NULL; \ 761 \ 762 drm_gpuvm_bo_put(__prev_vm_bo); \ 763 \ 764 spin_lock(&(__gpuvm)->__list_name.lock); \ 765 if (!(__gpuvm)->__list_name.local_list) \ 766 (__gpuvm)->__list_name.local_list = __local_list; \ 767 else \ 768 drm_WARN_ON((__gpuvm)->drm, \ 769 (__gpuvm)->__list_name.local_list != __local_list); \ 770 \ 771 while (!list_empty(&(__gpuvm)->__list_name.list)) { \ 772 __vm_bo = list_first_entry(&(__gpuvm)->__list_name.list, \ 773 struct drm_gpuvm_bo, \ 774 list.entry.__list_name); \ 775 if (kref_get_unless_zero(&__vm_bo->kref)) { \ 776 list_move_tail(&(__vm_bo)->list.entry.__list_name, \ 777 __local_list); \ 778 break; \ 779 } else { \ 780 list_del_init(&(__vm_bo)->list.entry.__list_name); \ 781 __vm_bo = NULL; \ 782 } \ 783 } \ 784 spin_unlock(&(__gpuvm)->__list_name.lock); \ 785 \ 786 __vm_bo; \ 787 }) 788 789 /** 790 * for_each_vm_bo_in_list() - internal vm_bo list iterator 791 * @__gpuvm: the &drm_gpuvm 792 * @__list_name: the name of the list we're iterating on 793 * @__local_list: a pointer to the local list used to store already iterated items 794 * @__vm_bo: the struct drm_gpuvm_bo to assign in each iteration step 795 * 796 * This helper is here to provide lockless list iteration. Lockless as in, the 797 * iterator releases the lock immediately after picking the first element from the 798 * list, hence list insertion and deletion can happen concurrently. 799 * 800 * It is not allowed to re-assign the vm_bo pointer from inside this loop. 801 * 802 * Typical use: 803 * 804 * struct drm_gpuvm_bo *vm_bo; 805 * LIST_HEAD(my_local_list); 806 * 807 * ret = 0; 808 * for_each_vm_bo_in_list(gpuvm, <list_name>, &my_local_list, vm_bo) { 809 * ret = do_something_with_vm_bo(..., vm_bo); 810 * if (ret) 811 * break; 812 * } 813 * // Drop ref in case we break out of the loop. 814 * drm_gpuvm_bo_put(vm_bo); 815 * restore_vm_bo_list(gpuvm, <list_name>, &my_local_list); 816 * 817 * 818 * Only used for internal list iterations, not meant to be exposed to the outside 819 * world. 820 */ 821 #define for_each_vm_bo_in_list(__gpuvm, __list_name, __local_list, __vm_bo) \ 822 for (__vm_bo = get_next_vm_bo_from_list(__gpuvm, __list_name, \ 823 __local_list, NULL); \ 824 __vm_bo; \ 825 __vm_bo = get_next_vm_bo_from_list(__gpuvm, __list_name, \ 826 __local_list, __vm_bo)) 827 828 static void 829 __restore_vm_bo_list(struct drm_gpuvm *gpuvm, spinlock_t *lock, 830 struct list_head *list, struct list_head **local_list) 831 { 832 /* Merge back the two lists, moving local list elements to the 833 * head to preserve previous ordering, in case it matters. 834 */ 835 spin_lock(lock); 836 if (*local_list) { 837 list_splice(*local_list, list); 838 *local_list = NULL; 839 } 840 spin_unlock(lock); 841 } 842 843 /** 844 * restore_vm_bo_list() - move vm_bo elements back to their original list 845 * @__gpuvm: the &drm_gpuvm 846 * @__list_name: the name of the list we're iterating on 847 * 848 * When we're done iterating a vm_bo list, we should call restore_vm_bo_list() 849 * to restore the original state and let new iterations take place. 850 */ 851 #define restore_vm_bo_list(__gpuvm, __list_name) \ 852 __restore_vm_bo_list((__gpuvm), &(__gpuvm)->__list_name.lock, \ 853 &(__gpuvm)->__list_name.list, \ 854 &(__gpuvm)->__list_name.local_list) 855 856 static void 857 cond_spin_lock(spinlock_t *lock, bool cond) 858 { 859 if (cond) 860 spin_lock(lock); 861 } 862 863 static void 864 cond_spin_unlock(spinlock_t *lock, bool cond) 865 { 866 if (cond) 867 spin_unlock(lock); 868 } 869 870 static void 871 __drm_gpuvm_bo_list_add(struct drm_gpuvm *gpuvm, spinlock_t *lock, 872 struct list_head *entry, struct list_head *list) 873 { 874 cond_spin_lock(lock, !!lock); 875 if (list_empty(entry)) 876 list_add_tail(entry, list); 877 cond_spin_unlock(lock, !!lock); 878 } 879 880 /** 881 * drm_gpuvm_bo_list_add() - insert a vm_bo into the given list 882 * @__vm_bo: the &drm_gpuvm_bo 883 * @__list_name: the name of the list to insert into 884 * @__lock: whether to lock with the internal spinlock 885 * 886 * Inserts the given @__vm_bo into the list specified by @__list_name. 887 */ 888 #define drm_gpuvm_bo_list_add(__vm_bo, __list_name, __lock) \ 889 __drm_gpuvm_bo_list_add((__vm_bo)->vm, \ 890 __lock ? &(__vm_bo)->vm->__list_name.lock : \ 891 NULL, \ 892 &(__vm_bo)->list.entry.__list_name, \ 893 &(__vm_bo)->vm->__list_name.list) 894 895 static void 896 __drm_gpuvm_bo_list_del(struct drm_gpuvm *gpuvm, spinlock_t *lock, 897 struct list_head *entry, bool init) 898 { 899 cond_spin_lock(lock, !!lock); 900 if (init) { 901 if (!list_empty(entry)) 902 list_del_init(entry); 903 } else { 904 list_del(entry); 905 } 906 cond_spin_unlock(lock, !!lock); 907 } 908 909 /** 910 * drm_gpuvm_bo_list_del_init() - remove a vm_bo from the given list 911 * @__vm_bo: the &drm_gpuvm_bo 912 * @__list_name: the name of the list to insert into 913 * @__lock: whether to lock with the internal spinlock 914 * 915 * Removes the given @__vm_bo from the list specified by @__list_name. 916 */ 917 #define drm_gpuvm_bo_list_del_init(__vm_bo, __list_name, __lock) \ 918 __drm_gpuvm_bo_list_del((__vm_bo)->vm, \ 919 __lock ? &(__vm_bo)->vm->__list_name.lock : \ 920 NULL, \ 921 &(__vm_bo)->list.entry.__list_name, \ 922 true) 923 924 /** 925 * drm_gpuvm_bo_list_del() - remove a vm_bo from the given list 926 * @__vm_bo: the &drm_gpuvm_bo 927 * @__list_name: the name of the list to insert into 928 * @__lock: whether to lock with the internal spinlock 929 * 930 * Removes the given @__vm_bo from the list specified by @__list_name. 931 */ 932 #define drm_gpuvm_bo_list_del(__vm_bo, __list_name, __lock) \ 933 __drm_gpuvm_bo_list_del((__vm_bo)->vm, \ 934 __lock ? &(__vm_bo)->vm->__list_name.lock : \ 935 NULL, \ 936 &(__vm_bo)->list.entry.__list_name, \ 937 false) 938 939 #define to_drm_gpuva(__node) container_of((__node), struct drm_gpuva, rb.node) 940 941 #define GPUVA_START(node) ((node)->va.addr) 942 #define GPUVA_LAST(node) ((node)->va.addr + (node)->va.range - 1) 943 944 /* We do not actually use drm_gpuva_it_next(), tell the compiler to not complain 945 * about this. 946 */ 947 INTERVAL_TREE_DEFINE(struct drm_gpuva, rb.node, u64, rb.__subtree_last, 948 GPUVA_START, GPUVA_LAST, static __maybe_unused, 949 drm_gpuva_it) 950 951 static int __drm_gpuva_insert(struct drm_gpuvm *gpuvm, 952 struct drm_gpuva *va); 953 static void __drm_gpuva_remove(struct drm_gpuva *va); 954 955 static bool 956 drm_gpuvm_check_overflow(u64 addr, u64 range) 957 { 958 u64 end; 959 960 return check_add_overflow(addr, range, &end); 961 } 962 963 static bool 964 drm_gpuvm_warn_check_overflow(struct drm_gpuvm *gpuvm, u64 addr, u64 range) 965 { 966 return drm_WARN(gpuvm->drm, drm_gpuvm_check_overflow(addr, range), 967 "GPUVA address limited to %zu bytes.\n", sizeof(addr)); 968 } 969 970 static bool 971 drm_gpuvm_in_mm_range(struct drm_gpuvm *gpuvm, u64 addr, u64 range) 972 { 973 u64 end = addr + range; 974 u64 mm_start = gpuvm->mm_start; 975 u64 mm_end = mm_start + gpuvm->mm_range; 976 977 return addr >= mm_start && end <= mm_end; 978 } 979 980 static bool 981 drm_gpuvm_in_kernel_node(struct drm_gpuvm *gpuvm, u64 addr, u64 range) 982 { 983 u64 end = addr + range; 984 u64 kstart = gpuvm->kernel_alloc_node.va.addr; 985 u64 krange = gpuvm->kernel_alloc_node.va.range; 986 u64 kend = kstart + krange; 987 988 return krange && addr < kend && kstart < end; 989 } 990 991 /** 992 * drm_gpuvm_range_valid() - checks whether the given range is valid for the 993 * given &drm_gpuvm 994 * @gpuvm: the GPUVM to check the range for 995 * @addr: the base address 996 * @range: the range starting from the base address 997 * 998 * Checks whether the range is within the GPUVM's managed boundaries. 999 * 1000 * Returns: true for a valid range, false otherwise 1001 */ 1002 bool 1003 drm_gpuvm_range_valid(struct drm_gpuvm *gpuvm, 1004 u64 addr, u64 range) 1005 { 1006 return !drm_gpuvm_check_overflow(addr, range) && 1007 drm_gpuvm_in_mm_range(gpuvm, addr, range) && 1008 !drm_gpuvm_in_kernel_node(gpuvm, addr, range); 1009 } 1010 EXPORT_SYMBOL_GPL(drm_gpuvm_range_valid); 1011 1012 static void 1013 drm_gpuvm_gem_object_free(struct drm_gem_object *obj) 1014 { 1015 drm_gem_object_release(obj); 1016 kfree(obj); 1017 } 1018 1019 static const struct drm_gem_object_funcs drm_gpuvm_object_funcs = { 1020 .free = drm_gpuvm_gem_object_free, 1021 }; 1022 1023 /** 1024 * drm_gpuvm_resv_object_alloc() - allocate a dummy &drm_gem_object 1025 * @drm: the drivers &drm_device 1026 * 1027 * Allocates a dummy &drm_gem_object which can be passed to drm_gpuvm_init() in 1028 * order to serve as root GEM object providing the &drm_resv shared across 1029 * &drm_gem_objects local to a single GPUVM. 1030 * 1031 * Returns: the &drm_gem_object on success, NULL on failure 1032 */ 1033 struct drm_gem_object * 1034 drm_gpuvm_resv_object_alloc(struct drm_device *drm) 1035 { 1036 struct drm_gem_object *obj; 1037 1038 obj = kzalloc(sizeof(*obj), GFP_KERNEL); 1039 if (!obj) 1040 return NULL; 1041 1042 obj->funcs = &drm_gpuvm_object_funcs; 1043 drm_gem_private_object_init(drm, obj, 0); 1044 1045 return obj; 1046 } 1047 EXPORT_SYMBOL_GPL(drm_gpuvm_resv_object_alloc); 1048 1049 /** 1050 * drm_gpuvm_init() - initialize a &drm_gpuvm 1051 * @gpuvm: pointer to the &drm_gpuvm to initialize 1052 * @name: the name of the GPU VA space 1053 * @flags: the &drm_gpuvm_flags for this GPUVM 1054 * @drm: the &drm_device this VM resides in 1055 * @r_obj: the resv &drm_gem_object providing the GPUVM's common &dma_resv 1056 * @start_offset: the start offset of the GPU VA space 1057 * @range: the size of the GPU VA space 1058 * @reserve_offset: the start of the kernel reserved GPU VA area 1059 * @reserve_range: the size of the kernel reserved GPU VA area 1060 * @ops: &drm_gpuvm_ops called on &drm_gpuvm_sm_map / &drm_gpuvm_sm_unmap 1061 * 1062 * The &drm_gpuvm must be initialized with this function before use. 1063 * 1064 * Note that @gpuvm must be cleared to 0 before calling this function. The given 1065 * &name is expected to be managed by the surrounding driver structures. 1066 */ 1067 void 1068 drm_gpuvm_init(struct drm_gpuvm *gpuvm, const char *name, 1069 enum drm_gpuvm_flags flags, 1070 struct drm_device *drm, 1071 struct drm_gem_object *r_obj, 1072 u64 start_offset, u64 range, 1073 u64 reserve_offset, u64 reserve_range, 1074 const struct drm_gpuvm_ops *ops) 1075 { 1076 gpuvm->rb.tree = RB_ROOT_CACHED; 1077 INIT_LIST_HEAD(&gpuvm->rb.list); 1078 1079 INIT_LIST_HEAD(&gpuvm->extobj.list); 1080 spin_lock_init(&gpuvm->extobj.lock); 1081 1082 INIT_LIST_HEAD(&gpuvm->evict.list); 1083 spin_lock_init(&gpuvm->evict.lock); 1084 1085 kref_init(&gpuvm->kref); 1086 1087 gpuvm->name = name ? name : "unknown"; 1088 gpuvm->flags = flags; 1089 gpuvm->ops = ops; 1090 gpuvm->drm = drm; 1091 gpuvm->r_obj = r_obj; 1092 1093 drm_gem_object_get(r_obj); 1094 1095 drm_gpuvm_warn_check_overflow(gpuvm, start_offset, range); 1096 gpuvm->mm_start = start_offset; 1097 gpuvm->mm_range = range; 1098 1099 memset(&gpuvm->kernel_alloc_node, 0, sizeof(struct drm_gpuva)); 1100 if (reserve_range) { 1101 gpuvm->kernel_alloc_node.va.addr = reserve_offset; 1102 gpuvm->kernel_alloc_node.va.range = reserve_range; 1103 1104 if (likely(!drm_gpuvm_warn_check_overflow(gpuvm, reserve_offset, 1105 reserve_range))) 1106 __drm_gpuva_insert(gpuvm, &gpuvm->kernel_alloc_node); 1107 } 1108 } 1109 EXPORT_SYMBOL_GPL(drm_gpuvm_init); 1110 1111 static void 1112 drm_gpuvm_fini(struct drm_gpuvm *gpuvm) 1113 { 1114 gpuvm->name = NULL; 1115 1116 if (gpuvm->kernel_alloc_node.va.range) 1117 __drm_gpuva_remove(&gpuvm->kernel_alloc_node); 1118 1119 drm_WARN(gpuvm->drm, !RB_EMPTY_ROOT(&gpuvm->rb.tree.rb_root), 1120 "GPUVA tree is not empty, potentially leaking memory.\n"); 1121 1122 drm_WARN(gpuvm->drm, !list_empty(&gpuvm->extobj.list), 1123 "Extobj list should be empty.\n"); 1124 drm_WARN(gpuvm->drm, !list_empty(&gpuvm->evict.list), 1125 "Evict list should be empty.\n"); 1126 1127 drm_gem_object_put(gpuvm->r_obj); 1128 } 1129 1130 static void 1131 drm_gpuvm_free(struct kref *kref) 1132 { 1133 struct drm_gpuvm *gpuvm = container_of(kref, struct drm_gpuvm, kref); 1134 1135 drm_gpuvm_fini(gpuvm); 1136 1137 if (drm_WARN_ON(gpuvm->drm, !gpuvm->ops->vm_free)) 1138 return; 1139 1140 gpuvm->ops->vm_free(gpuvm); 1141 } 1142 1143 /** 1144 * drm_gpuvm_put() - drop a struct drm_gpuvm reference 1145 * @gpuvm: the &drm_gpuvm to release the reference of 1146 * 1147 * This releases a reference to @gpuvm. 1148 * 1149 * This function may be called from atomic context. 1150 */ 1151 void 1152 drm_gpuvm_put(struct drm_gpuvm *gpuvm) 1153 { 1154 if (gpuvm) 1155 kref_put(&gpuvm->kref, drm_gpuvm_free); 1156 } 1157 EXPORT_SYMBOL_GPL(drm_gpuvm_put); 1158 1159 static int 1160 exec_prepare_obj(struct drm_exec *exec, struct drm_gem_object *obj, 1161 unsigned int num_fences) 1162 { 1163 return num_fences ? drm_exec_prepare_obj(exec, obj, num_fences) : 1164 drm_exec_lock_obj(exec, obj); 1165 } 1166 1167 /** 1168 * drm_gpuvm_prepare_vm() - prepare the GPUVMs common dma-resv 1169 * @gpuvm: the &drm_gpuvm 1170 * @exec: the &drm_exec context 1171 * @num_fences: the amount of &dma_fences to reserve 1172 * 1173 * Calls drm_exec_prepare_obj() for the GPUVMs dummy &drm_gem_object; if 1174 * @num_fences is zero drm_exec_lock_obj() is called instead. 1175 * 1176 * Using this function directly, it is the drivers responsibility to call 1177 * drm_exec_init() and drm_exec_fini() accordingly. 1178 * 1179 * Returns: 0 on success, negative error code on failure. 1180 */ 1181 int 1182 drm_gpuvm_prepare_vm(struct drm_gpuvm *gpuvm, 1183 struct drm_exec *exec, 1184 unsigned int num_fences) 1185 { 1186 return exec_prepare_obj(exec, gpuvm->r_obj, num_fences); 1187 } 1188 EXPORT_SYMBOL_GPL(drm_gpuvm_prepare_vm); 1189 1190 static int 1191 __drm_gpuvm_prepare_objects(struct drm_gpuvm *gpuvm, 1192 struct drm_exec *exec, 1193 unsigned int num_fences) 1194 { 1195 struct drm_gpuvm_bo *vm_bo; 1196 LIST_HEAD(extobjs); 1197 int ret = 0; 1198 1199 for_each_vm_bo_in_list(gpuvm, extobj, &extobjs, vm_bo) { 1200 ret = exec_prepare_obj(exec, vm_bo->obj, num_fences); 1201 if (ret) 1202 break; 1203 } 1204 /* Drop ref in case we break out of the loop. */ 1205 drm_gpuvm_bo_put(vm_bo); 1206 restore_vm_bo_list(gpuvm, extobj); 1207 1208 return ret; 1209 } 1210 1211 static int 1212 drm_gpuvm_prepare_objects_locked(struct drm_gpuvm *gpuvm, 1213 struct drm_exec *exec, 1214 unsigned int num_fences) 1215 { 1216 struct drm_gpuvm_bo *vm_bo; 1217 int ret = 0; 1218 1219 drm_gpuvm_resv_assert_held(gpuvm); 1220 list_for_each_entry(vm_bo, &gpuvm->extobj.list, list.entry.extobj) { 1221 ret = exec_prepare_obj(exec, vm_bo->obj, num_fences); 1222 if (ret) 1223 break; 1224 1225 if (vm_bo->evicted) 1226 drm_gpuvm_bo_list_add(vm_bo, evict, false); 1227 } 1228 1229 return ret; 1230 } 1231 1232 /** 1233 * drm_gpuvm_prepare_objects() - prepare all associated BOs 1234 * @gpuvm: the &drm_gpuvm 1235 * @exec: the &drm_exec locking context 1236 * @num_fences: the amount of &dma_fences to reserve 1237 * 1238 * Calls drm_exec_prepare_obj() for all &drm_gem_objects the given 1239 * &drm_gpuvm contains mappings of; if @num_fences is zero drm_exec_lock_obj() 1240 * is called instead. 1241 * 1242 * Using this function directly, it is the drivers responsibility to call 1243 * drm_exec_init() and drm_exec_fini() accordingly. 1244 * 1245 * Note: This function is safe against concurrent insertion and removal of 1246 * external objects, however it is not safe against concurrent usage itself. 1247 * 1248 * Drivers need to make sure to protect this case with either an outer VM lock 1249 * or by calling drm_gpuvm_prepare_vm() before this function within the 1250 * drm_exec_until_all_locked() loop, such that the GPUVM's dma-resv lock ensures 1251 * mutual exclusion. 1252 * 1253 * Returns: 0 on success, negative error code on failure. 1254 */ 1255 int 1256 drm_gpuvm_prepare_objects(struct drm_gpuvm *gpuvm, 1257 struct drm_exec *exec, 1258 unsigned int num_fences) 1259 { 1260 if (drm_gpuvm_resv_protected(gpuvm)) 1261 return drm_gpuvm_prepare_objects_locked(gpuvm, exec, 1262 num_fences); 1263 else 1264 return __drm_gpuvm_prepare_objects(gpuvm, exec, num_fences); 1265 } 1266 EXPORT_SYMBOL_GPL(drm_gpuvm_prepare_objects); 1267 1268 /** 1269 * drm_gpuvm_prepare_range() - prepare all BOs mapped within a given range 1270 * @gpuvm: the &drm_gpuvm 1271 * @exec: the &drm_exec locking context 1272 * @addr: the start address within the VA space 1273 * @range: the range to iterate within the VA space 1274 * @num_fences: the amount of &dma_fences to reserve 1275 * 1276 * Calls drm_exec_prepare_obj() for all &drm_gem_objects mapped between @addr 1277 * and @addr + @range; if @num_fences is zero drm_exec_lock_obj() is called 1278 * instead. 1279 * 1280 * Returns: 0 on success, negative error code on failure. 1281 */ 1282 int 1283 drm_gpuvm_prepare_range(struct drm_gpuvm *gpuvm, struct drm_exec *exec, 1284 u64 addr, u64 range, unsigned int num_fences) 1285 { 1286 struct drm_gpuva *va; 1287 u64 end = addr + range; 1288 int ret; 1289 1290 drm_gpuvm_for_each_va_range(va, gpuvm, addr, end) { 1291 struct drm_gem_object *obj = va->gem.obj; 1292 1293 ret = exec_prepare_obj(exec, obj, num_fences); 1294 if (ret) 1295 return ret; 1296 } 1297 1298 return 0; 1299 } 1300 EXPORT_SYMBOL_GPL(drm_gpuvm_prepare_range); 1301 1302 /** 1303 * drm_gpuvm_exec_lock() - lock all dma-resv of all associated BOs 1304 * @vm_exec: the &drm_gpuvm_exec wrapper 1305 * 1306 * Acquires all dma-resv locks of all &drm_gem_objects the given 1307 * &drm_gpuvm contains mappings of. 1308 * 1309 * Additionally, when calling this function with struct drm_gpuvm_exec::extra 1310 * being set the driver receives the given @fn callback to lock additional 1311 * dma-resv in the context of the &drm_gpuvm_exec instance. Typically, drivers 1312 * would call drm_exec_prepare_obj() from within this callback. 1313 * 1314 * Returns: 0 on success, negative error code on failure. 1315 */ 1316 int 1317 drm_gpuvm_exec_lock(struct drm_gpuvm_exec *vm_exec) 1318 { 1319 struct drm_gpuvm *gpuvm = vm_exec->vm; 1320 struct drm_exec *exec = &vm_exec->exec; 1321 unsigned int num_fences = vm_exec->num_fences; 1322 int ret; 1323 1324 drm_exec_init(exec, vm_exec->flags, 0); 1325 1326 drm_exec_until_all_locked(exec) { 1327 ret = drm_gpuvm_prepare_vm(gpuvm, exec, num_fences); 1328 drm_exec_retry_on_contention(exec); 1329 if (ret) 1330 goto err; 1331 1332 ret = drm_gpuvm_prepare_objects(gpuvm, exec, num_fences); 1333 drm_exec_retry_on_contention(exec); 1334 if (ret) 1335 goto err; 1336 1337 if (vm_exec->extra.fn) { 1338 ret = vm_exec->extra.fn(vm_exec); 1339 drm_exec_retry_on_contention(exec); 1340 if (ret) 1341 goto err; 1342 } 1343 } 1344 1345 return 0; 1346 1347 err: 1348 drm_exec_fini(exec); 1349 return ret; 1350 } 1351 EXPORT_SYMBOL_GPL(drm_gpuvm_exec_lock); 1352 1353 static int 1354 fn_lock_array(struct drm_gpuvm_exec *vm_exec) 1355 { 1356 struct { 1357 struct drm_gem_object **objs; 1358 unsigned int num_objs; 1359 } *args = vm_exec->extra.priv; 1360 1361 return drm_exec_prepare_array(&vm_exec->exec, args->objs, 1362 args->num_objs, vm_exec->num_fences); 1363 } 1364 1365 /** 1366 * drm_gpuvm_exec_lock_array() - lock all dma-resv of all associated BOs 1367 * @vm_exec: the &drm_gpuvm_exec wrapper 1368 * @objs: additional &drm_gem_objects to lock 1369 * @num_objs: the number of additional &drm_gem_objects to lock 1370 * 1371 * Acquires all dma-resv locks of all &drm_gem_objects the given &drm_gpuvm 1372 * contains mappings of, plus the ones given through @objs. 1373 * 1374 * Returns: 0 on success, negative error code on failure. 1375 */ 1376 int 1377 drm_gpuvm_exec_lock_array(struct drm_gpuvm_exec *vm_exec, 1378 struct drm_gem_object **objs, 1379 unsigned int num_objs) 1380 { 1381 struct { 1382 struct drm_gem_object **objs; 1383 unsigned int num_objs; 1384 } args; 1385 1386 args.objs = objs; 1387 args.num_objs = num_objs; 1388 1389 vm_exec->extra.fn = fn_lock_array; 1390 vm_exec->extra.priv = &args; 1391 1392 return drm_gpuvm_exec_lock(vm_exec); 1393 } 1394 EXPORT_SYMBOL_GPL(drm_gpuvm_exec_lock_array); 1395 1396 /** 1397 * drm_gpuvm_exec_lock_range() - prepare all BOs mapped within a given range 1398 * @vm_exec: the &drm_gpuvm_exec wrapper 1399 * @addr: the start address within the VA space 1400 * @range: the range to iterate within the VA space 1401 * 1402 * Acquires all dma-resv locks of all &drm_gem_objects mapped between @addr and 1403 * @addr + @range. 1404 * 1405 * Returns: 0 on success, negative error code on failure. 1406 */ 1407 int 1408 drm_gpuvm_exec_lock_range(struct drm_gpuvm_exec *vm_exec, 1409 u64 addr, u64 range) 1410 { 1411 struct drm_gpuvm *gpuvm = vm_exec->vm; 1412 struct drm_exec *exec = &vm_exec->exec; 1413 int ret; 1414 1415 drm_exec_init(exec, vm_exec->flags, 0); 1416 1417 drm_exec_until_all_locked(exec) { 1418 ret = drm_gpuvm_prepare_range(gpuvm, exec, addr, range, 1419 vm_exec->num_fences); 1420 drm_exec_retry_on_contention(exec); 1421 if (ret) 1422 goto err; 1423 } 1424 1425 return ret; 1426 1427 err: 1428 drm_exec_fini(exec); 1429 return ret; 1430 } 1431 EXPORT_SYMBOL_GPL(drm_gpuvm_exec_lock_range); 1432 1433 static int 1434 __drm_gpuvm_validate(struct drm_gpuvm *gpuvm, struct drm_exec *exec) 1435 { 1436 const struct drm_gpuvm_ops *ops = gpuvm->ops; 1437 struct drm_gpuvm_bo *vm_bo; 1438 LIST_HEAD(evict); 1439 int ret = 0; 1440 1441 for_each_vm_bo_in_list(gpuvm, evict, &evict, vm_bo) { 1442 ret = ops->vm_bo_validate(vm_bo, exec); 1443 if (ret) 1444 break; 1445 } 1446 /* Drop ref in case we break out of the loop. */ 1447 drm_gpuvm_bo_put(vm_bo); 1448 restore_vm_bo_list(gpuvm, evict); 1449 1450 return ret; 1451 } 1452 1453 static int 1454 drm_gpuvm_validate_locked(struct drm_gpuvm *gpuvm, struct drm_exec *exec) 1455 { 1456 const struct drm_gpuvm_ops *ops = gpuvm->ops; 1457 struct drm_gpuvm_bo *vm_bo, *next; 1458 int ret = 0; 1459 1460 drm_gpuvm_resv_assert_held(gpuvm); 1461 1462 list_for_each_entry_safe(vm_bo, next, &gpuvm->evict.list, 1463 list.entry.evict) { 1464 ret = ops->vm_bo_validate(vm_bo, exec); 1465 if (ret) 1466 break; 1467 1468 dma_resv_assert_held(vm_bo->obj->resv); 1469 if (!vm_bo->evicted) 1470 drm_gpuvm_bo_list_del_init(vm_bo, evict, false); 1471 } 1472 1473 return ret; 1474 } 1475 1476 /** 1477 * drm_gpuvm_validate() - validate all BOs marked as evicted 1478 * @gpuvm: the &drm_gpuvm to validate evicted BOs 1479 * @exec: the &drm_exec instance used for locking the GPUVM 1480 * 1481 * Calls the &drm_gpuvm_ops::vm_bo_validate callback for all evicted buffer 1482 * objects being mapped in the given &drm_gpuvm. 1483 * 1484 * Returns: 0 on success, negative error code on failure. 1485 */ 1486 int 1487 drm_gpuvm_validate(struct drm_gpuvm *gpuvm, struct drm_exec *exec) 1488 { 1489 const struct drm_gpuvm_ops *ops = gpuvm->ops; 1490 1491 if (unlikely(!ops || !ops->vm_bo_validate)) 1492 return -EOPNOTSUPP; 1493 1494 if (drm_gpuvm_resv_protected(gpuvm)) 1495 return drm_gpuvm_validate_locked(gpuvm, exec); 1496 else 1497 return __drm_gpuvm_validate(gpuvm, exec); 1498 } 1499 EXPORT_SYMBOL_GPL(drm_gpuvm_validate); 1500 1501 /** 1502 * drm_gpuvm_resv_add_fence - add fence to private and all extobj 1503 * dma-resv 1504 * @gpuvm: the &drm_gpuvm to add a fence to 1505 * @exec: the &drm_exec locking context 1506 * @fence: fence to add 1507 * @private_usage: private dma-resv usage 1508 * @extobj_usage: extobj dma-resv usage 1509 */ 1510 void 1511 drm_gpuvm_resv_add_fence(struct drm_gpuvm *gpuvm, 1512 struct drm_exec *exec, 1513 struct dma_fence *fence, 1514 enum dma_resv_usage private_usage, 1515 enum dma_resv_usage extobj_usage) 1516 { 1517 struct drm_gem_object *obj; 1518 unsigned long index; 1519 1520 drm_exec_for_each_locked_object(exec, index, obj) { 1521 dma_resv_assert_held(obj->resv); 1522 dma_resv_add_fence(obj->resv, fence, 1523 drm_gpuvm_is_extobj(gpuvm, obj) ? 1524 extobj_usage : private_usage); 1525 } 1526 } 1527 EXPORT_SYMBOL_GPL(drm_gpuvm_resv_add_fence); 1528 1529 /** 1530 * drm_gpuvm_bo_create() - create a new instance of struct drm_gpuvm_bo 1531 * @gpuvm: The &drm_gpuvm the @obj is mapped in. 1532 * @obj: The &drm_gem_object being mapped in the @gpuvm. 1533 * 1534 * If provided by the driver, this function uses the &drm_gpuvm_ops 1535 * vm_bo_alloc() callback to allocate. 1536 * 1537 * Returns: a pointer to the &drm_gpuvm_bo on success, NULL on failure 1538 */ 1539 struct drm_gpuvm_bo * 1540 drm_gpuvm_bo_create(struct drm_gpuvm *gpuvm, 1541 struct drm_gem_object *obj) 1542 { 1543 const struct drm_gpuvm_ops *ops = gpuvm->ops; 1544 struct drm_gpuvm_bo *vm_bo; 1545 1546 if (ops && ops->vm_bo_alloc) 1547 vm_bo = ops->vm_bo_alloc(); 1548 else 1549 vm_bo = kzalloc(sizeof(*vm_bo), GFP_KERNEL); 1550 1551 if (unlikely(!vm_bo)) 1552 return NULL; 1553 1554 vm_bo->vm = drm_gpuvm_get(gpuvm); 1555 vm_bo->obj = obj; 1556 drm_gem_object_get(obj); 1557 1558 kref_init(&vm_bo->kref); 1559 INIT_LIST_HEAD(&vm_bo->list.gpuva); 1560 INIT_LIST_HEAD(&vm_bo->list.entry.gem); 1561 1562 INIT_LIST_HEAD(&vm_bo->list.entry.extobj); 1563 INIT_LIST_HEAD(&vm_bo->list.entry.evict); 1564 1565 return vm_bo; 1566 } 1567 EXPORT_SYMBOL_GPL(drm_gpuvm_bo_create); 1568 1569 static void 1570 drm_gpuvm_bo_destroy(struct kref *kref) 1571 { 1572 struct drm_gpuvm_bo *vm_bo = container_of(kref, struct drm_gpuvm_bo, 1573 kref); 1574 struct drm_gpuvm *gpuvm = vm_bo->vm; 1575 const struct drm_gpuvm_ops *ops = gpuvm->ops; 1576 struct drm_gem_object *obj = vm_bo->obj; 1577 bool lock = !drm_gpuvm_resv_protected(gpuvm); 1578 1579 if (!lock) 1580 drm_gpuvm_resv_assert_held(gpuvm); 1581 1582 drm_gpuvm_bo_list_del(vm_bo, extobj, lock); 1583 drm_gpuvm_bo_list_del(vm_bo, evict, lock); 1584 1585 drm_gem_gpuva_assert_lock_held(gpuvm, obj); 1586 list_del(&vm_bo->list.entry.gem); 1587 1588 if (ops && ops->vm_bo_free) 1589 ops->vm_bo_free(vm_bo); 1590 else 1591 kfree(vm_bo); 1592 1593 drm_gpuvm_put(gpuvm); 1594 drm_gem_object_put(obj); 1595 } 1596 1597 /** 1598 * drm_gpuvm_bo_put() - drop a struct drm_gpuvm_bo reference 1599 * @vm_bo: the &drm_gpuvm_bo to release the reference of 1600 * 1601 * This releases a reference to @vm_bo. 1602 * 1603 * If the reference count drops to zero, the &gpuvm_bo is destroyed, which 1604 * includes removing it from the GEMs gpuva list. Hence, if a call to this 1605 * function can potentially let the reference count drop to zero the caller must 1606 * hold the lock that the GEM uses for its gpuva list (either the GEM's 1607 * dma-resv or gpuva.lock mutex). 1608 * 1609 * This function may only be called from non-atomic context. 1610 * 1611 * Returns: true if vm_bo was destroyed, false otherwise. 1612 */ 1613 bool 1614 drm_gpuvm_bo_put(struct drm_gpuvm_bo *vm_bo) 1615 { 1616 might_sleep(); 1617 1618 if (vm_bo) 1619 return !!kref_put(&vm_bo->kref, drm_gpuvm_bo_destroy); 1620 1621 return false; 1622 } 1623 EXPORT_SYMBOL_GPL(drm_gpuvm_bo_put); 1624 1625 static struct drm_gpuvm_bo * 1626 __drm_gpuvm_bo_find(struct drm_gpuvm *gpuvm, 1627 struct drm_gem_object *obj) 1628 { 1629 struct drm_gpuvm_bo *vm_bo; 1630 1631 drm_gem_gpuva_assert_lock_held(gpuvm, obj); 1632 drm_gem_for_each_gpuvm_bo(vm_bo, obj) 1633 if (vm_bo->vm == gpuvm) 1634 return vm_bo; 1635 1636 return NULL; 1637 } 1638 1639 /** 1640 * drm_gpuvm_bo_find() - find the &drm_gpuvm_bo for the given 1641 * &drm_gpuvm and &drm_gem_object 1642 * @gpuvm: The &drm_gpuvm the @obj is mapped in. 1643 * @obj: The &drm_gem_object being mapped in the @gpuvm. 1644 * 1645 * Find the &drm_gpuvm_bo representing the combination of the given 1646 * &drm_gpuvm and &drm_gem_object. If found, increases the reference 1647 * count of the &drm_gpuvm_bo accordingly. 1648 * 1649 * Returns: a pointer to the &drm_gpuvm_bo on success, NULL on failure 1650 */ 1651 struct drm_gpuvm_bo * 1652 drm_gpuvm_bo_find(struct drm_gpuvm *gpuvm, 1653 struct drm_gem_object *obj) 1654 { 1655 struct drm_gpuvm_bo *vm_bo = __drm_gpuvm_bo_find(gpuvm, obj); 1656 1657 return vm_bo ? drm_gpuvm_bo_get(vm_bo) : NULL; 1658 } 1659 EXPORT_SYMBOL_GPL(drm_gpuvm_bo_find); 1660 1661 /** 1662 * drm_gpuvm_bo_obtain() - obtains an instance of the &drm_gpuvm_bo for the 1663 * given &drm_gpuvm and &drm_gem_object 1664 * @gpuvm: The &drm_gpuvm the @obj is mapped in. 1665 * @obj: The &drm_gem_object being mapped in the @gpuvm. 1666 * 1667 * Find the &drm_gpuvm_bo representing the combination of the given 1668 * &drm_gpuvm and &drm_gem_object. If found, increases the reference 1669 * count of the &drm_gpuvm_bo accordingly. If not found, allocates a new 1670 * &drm_gpuvm_bo. 1671 * 1672 * A new &drm_gpuvm_bo is added to the GEMs gpuva list. 1673 * 1674 * Returns: a pointer to the &drm_gpuvm_bo on success, an ERR_PTR on failure 1675 */ 1676 struct drm_gpuvm_bo * 1677 drm_gpuvm_bo_obtain(struct drm_gpuvm *gpuvm, 1678 struct drm_gem_object *obj) 1679 { 1680 struct drm_gpuvm_bo *vm_bo; 1681 1682 vm_bo = drm_gpuvm_bo_find(gpuvm, obj); 1683 if (vm_bo) 1684 return vm_bo; 1685 1686 vm_bo = drm_gpuvm_bo_create(gpuvm, obj); 1687 if (!vm_bo) 1688 return ERR_PTR(-ENOMEM); 1689 1690 drm_gem_gpuva_assert_lock_held(gpuvm, obj); 1691 list_add_tail(&vm_bo->list.entry.gem, &obj->gpuva.list); 1692 1693 return vm_bo; 1694 } 1695 EXPORT_SYMBOL_GPL(drm_gpuvm_bo_obtain); 1696 1697 /** 1698 * drm_gpuvm_bo_obtain_prealloc() - obtains an instance of the &drm_gpuvm_bo 1699 * for the given &drm_gpuvm and &drm_gem_object 1700 * @__vm_bo: A pre-allocated struct drm_gpuvm_bo. 1701 * 1702 * Find the &drm_gpuvm_bo representing the combination of the given 1703 * &drm_gpuvm and &drm_gem_object. If found, increases the reference 1704 * count of the found &drm_gpuvm_bo accordingly, while the @__vm_bo reference 1705 * count is decreased. If not found @__vm_bo is returned without further 1706 * increase of the reference count. 1707 * 1708 * A new &drm_gpuvm_bo is added to the GEMs gpuva list. 1709 * 1710 * Returns: a pointer to the found &drm_gpuvm_bo or @__vm_bo if no existing 1711 * &drm_gpuvm_bo was found 1712 */ 1713 struct drm_gpuvm_bo * 1714 drm_gpuvm_bo_obtain_prealloc(struct drm_gpuvm_bo *__vm_bo) 1715 { 1716 struct drm_gpuvm *gpuvm = __vm_bo->vm; 1717 struct drm_gem_object *obj = __vm_bo->obj; 1718 struct drm_gpuvm_bo *vm_bo; 1719 1720 vm_bo = drm_gpuvm_bo_find(gpuvm, obj); 1721 if (vm_bo) { 1722 drm_gpuvm_bo_put(__vm_bo); 1723 return vm_bo; 1724 } 1725 1726 drm_gem_gpuva_assert_lock_held(gpuvm, obj); 1727 list_add_tail(&__vm_bo->list.entry.gem, &obj->gpuva.list); 1728 1729 return __vm_bo; 1730 } 1731 EXPORT_SYMBOL_GPL(drm_gpuvm_bo_obtain_prealloc); 1732 1733 /** 1734 * drm_gpuvm_bo_extobj_add() - adds the &drm_gpuvm_bo to its &drm_gpuvm's 1735 * extobj list 1736 * @vm_bo: The &drm_gpuvm_bo to add to its &drm_gpuvm's the extobj list. 1737 * 1738 * Adds the given @vm_bo to its &drm_gpuvm's extobj list if not on the list 1739 * already and if the corresponding &drm_gem_object is an external object, 1740 * actually. 1741 */ 1742 void 1743 drm_gpuvm_bo_extobj_add(struct drm_gpuvm_bo *vm_bo) 1744 { 1745 struct drm_gpuvm *gpuvm = vm_bo->vm; 1746 bool lock = !drm_gpuvm_resv_protected(gpuvm); 1747 1748 if (!lock) 1749 drm_gpuvm_resv_assert_held(gpuvm); 1750 1751 if (drm_gpuvm_is_extobj(gpuvm, vm_bo->obj)) 1752 drm_gpuvm_bo_list_add(vm_bo, extobj, lock); 1753 } 1754 EXPORT_SYMBOL_GPL(drm_gpuvm_bo_extobj_add); 1755 1756 /** 1757 * drm_gpuvm_bo_evict() - add / remove a &drm_gpuvm_bo to / from the &drm_gpuvms 1758 * evicted list 1759 * @vm_bo: the &drm_gpuvm_bo to add or remove 1760 * @evict: indicates whether the object is evicted 1761 * 1762 * Adds a &drm_gpuvm_bo to or removes it from the &drm_gpuvm's evicted list. 1763 */ 1764 void 1765 drm_gpuvm_bo_evict(struct drm_gpuvm_bo *vm_bo, bool evict) 1766 { 1767 struct drm_gpuvm *gpuvm = vm_bo->vm; 1768 struct drm_gem_object *obj = vm_bo->obj; 1769 bool lock = !drm_gpuvm_resv_protected(gpuvm); 1770 1771 dma_resv_assert_held(obj->resv); 1772 vm_bo->evicted = evict; 1773 1774 /* Can't add external objects to the evicted list directly if not using 1775 * internal spinlocks, since in this case the evicted list is protected 1776 * with the VM's common dma-resv lock. 1777 */ 1778 if (drm_gpuvm_is_extobj(gpuvm, obj) && !lock) 1779 return; 1780 1781 if (evict) 1782 drm_gpuvm_bo_list_add(vm_bo, evict, lock); 1783 else 1784 drm_gpuvm_bo_list_del_init(vm_bo, evict, lock); 1785 } 1786 EXPORT_SYMBOL_GPL(drm_gpuvm_bo_evict); 1787 1788 static int 1789 __drm_gpuva_insert(struct drm_gpuvm *gpuvm, 1790 struct drm_gpuva *va) 1791 { 1792 struct rb_node *node; 1793 struct list_head *head; 1794 1795 if (drm_gpuva_it_iter_first(&gpuvm->rb.tree, 1796 GPUVA_START(va), 1797 GPUVA_LAST(va))) 1798 return -EEXIST; 1799 1800 va->vm = gpuvm; 1801 1802 drm_gpuva_it_insert(va, &gpuvm->rb.tree); 1803 1804 node = rb_prev(&va->rb.node); 1805 if (node) 1806 head = &(to_drm_gpuva(node))->rb.entry; 1807 else 1808 head = &gpuvm->rb.list; 1809 1810 list_add(&va->rb.entry, head); 1811 1812 return 0; 1813 } 1814 1815 /** 1816 * drm_gpuva_insert() - insert a &drm_gpuva 1817 * @gpuvm: the &drm_gpuvm to insert the &drm_gpuva in 1818 * @va: the &drm_gpuva to insert 1819 * 1820 * Insert a &drm_gpuva with a given address and range into a 1821 * &drm_gpuvm. 1822 * 1823 * It is safe to use this function using the safe versions of iterating the GPU 1824 * VA space, such as drm_gpuvm_for_each_va_safe() and 1825 * drm_gpuvm_for_each_va_range_safe(). 1826 * 1827 * Returns: 0 on success, negative error code on failure. 1828 */ 1829 int 1830 drm_gpuva_insert(struct drm_gpuvm *gpuvm, 1831 struct drm_gpuva *va) 1832 { 1833 u64 addr = va->va.addr; 1834 u64 range = va->va.range; 1835 int ret; 1836 1837 if (unlikely(!drm_gpuvm_range_valid(gpuvm, addr, range))) 1838 return -EINVAL; 1839 1840 ret = __drm_gpuva_insert(gpuvm, va); 1841 if (likely(!ret)) 1842 /* Take a reference of the GPUVM for the successfully inserted 1843 * drm_gpuva. We can't take the reference in 1844 * __drm_gpuva_insert() itself, since we don't want to increse 1845 * the reference count for the GPUVM's kernel_alloc_node. 1846 */ 1847 drm_gpuvm_get(gpuvm); 1848 1849 return ret; 1850 } 1851 EXPORT_SYMBOL_GPL(drm_gpuva_insert); 1852 1853 static void 1854 __drm_gpuva_remove(struct drm_gpuva *va) 1855 { 1856 drm_gpuva_it_remove(va, &va->vm->rb.tree); 1857 list_del_init(&va->rb.entry); 1858 } 1859 1860 /** 1861 * drm_gpuva_remove() - remove a &drm_gpuva 1862 * @va: the &drm_gpuva to remove 1863 * 1864 * This removes the given &va from the underlying tree. 1865 * 1866 * It is safe to use this function using the safe versions of iterating the GPU 1867 * VA space, such as drm_gpuvm_for_each_va_safe() and 1868 * drm_gpuvm_for_each_va_range_safe(). 1869 */ 1870 void 1871 drm_gpuva_remove(struct drm_gpuva *va) 1872 { 1873 struct drm_gpuvm *gpuvm = va->vm; 1874 1875 if (unlikely(va == &gpuvm->kernel_alloc_node)) { 1876 drm_WARN(gpuvm->drm, 1, 1877 "Can't destroy kernel reserved node.\n"); 1878 return; 1879 } 1880 1881 __drm_gpuva_remove(va); 1882 drm_gpuvm_put(va->vm); 1883 } 1884 EXPORT_SYMBOL_GPL(drm_gpuva_remove); 1885 1886 /** 1887 * drm_gpuva_link() - link a &drm_gpuva 1888 * @va: the &drm_gpuva to link 1889 * @vm_bo: the &drm_gpuvm_bo to add the &drm_gpuva to 1890 * 1891 * This adds the given &va to the GPU VA list of the &drm_gpuvm_bo and the 1892 * &drm_gpuvm_bo to the &drm_gem_object it is associated with. 1893 * 1894 * For every &drm_gpuva entry added to the &drm_gpuvm_bo an additional 1895 * reference of the latter is taken. 1896 * 1897 * This function expects the caller to protect the GEM's GPUVA list against 1898 * concurrent access using either the GEM's dma-resv or gpuva.lock mutex. 1899 */ 1900 void 1901 drm_gpuva_link(struct drm_gpuva *va, struct drm_gpuvm_bo *vm_bo) 1902 { 1903 struct drm_gem_object *obj = va->gem.obj; 1904 struct drm_gpuvm *gpuvm = va->vm; 1905 1906 if (unlikely(!obj)) 1907 return; 1908 1909 drm_WARN_ON(gpuvm->drm, obj != vm_bo->obj); 1910 1911 va->vm_bo = drm_gpuvm_bo_get(vm_bo); 1912 1913 drm_gem_gpuva_assert_lock_held(gpuvm, obj); 1914 list_add_tail(&va->gem.entry, &vm_bo->list.gpuva); 1915 } 1916 EXPORT_SYMBOL_GPL(drm_gpuva_link); 1917 1918 /** 1919 * drm_gpuva_unlink() - unlink a &drm_gpuva 1920 * @va: the &drm_gpuva to unlink 1921 * 1922 * This removes the given &va from the GPU VA list of the &drm_gem_object it is 1923 * associated with. 1924 * 1925 * This removes the given &va from the GPU VA list of the &drm_gpuvm_bo and 1926 * the &drm_gpuvm_bo from the &drm_gem_object it is associated with in case 1927 * this call unlinks the last &drm_gpuva from the &drm_gpuvm_bo. 1928 * 1929 * For every &drm_gpuva entry removed from the &drm_gpuvm_bo a reference of 1930 * the latter is dropped. 1931 * 1932 * This function expects the caller to protect the GEM's GPUVA list against 1933 * concurrent access using either the GEM's dma-resv or gpuva.lock mutex. 1934 */ 1935 void 1936 drm_gpuva_unlink(struct drm_gpuva *va) 1937 { 1938 struct drm_gem_object *obj = va->gem.obj; 1939 struct drm_gpuvm_bo *vm_bo = va->vm_bo; 1940 1941 if (unlikely(!obj)) 1942 return; 1943 1944 drm_gem_gpuva_assert_lock_held(va->vm, obj); 1945 list_del_init(&va->gem.entry); 1946 1947 va->vm_bo = NULL; 1948 drm_gpuvm_bo_put(vm_bo); 1949 } 1950 EXPORT_SYMBOL_GPL(drm_gpuva_unlink); 1951 1952 /** 1953 * drm_gpuva_find_first() - find the first &drm_gpuva in the given range 1954 * @gpuvm: the &drm_gpuvm to search in 1955 * @addr: the &drm_gpuvas address 1956 * @range: the &drm_gpuvas range 1957 * 1958 * Returns: the first &drm_gpuva within the given range 1959 */ 1960 struct drm_gpuva * 1961 drm_gpuva_find_first(struct drm_gpuvm *gpuvm, 1962 u64 addr, u64 range) 1963 { 1964 u64 last = addr + range - 1; 1965 1966 return drm_gpuva_it_iter_first(&gpuvm->rb.tree, addr, last); 1967 } 1968 EXPORT_SYMBOL_GPL(drm_gpuva_find_first); 1969 1970 /** 1971 * drm_gpuva_find() - find a &drm_gpuva 1972 * @gpuvm: the &drm_gpuvm to search in 1973 * @addr: the &drm_gpuvas address 1974 * @range: the &drm_gpuvas range 1975 * 1976 * Returns: the &drm_gpuva at a given &addr and with a given &range 1977 */ 1978 struct drm_gpuva * 1979 drm_gpuva_find(struct drm_gpuvm *gpuvm, 1980 u64 addr, u64 range) 1981 { 1982 struct drm_gpuva *va; 1983 1984 va = drm_gpuva_find_first(gpuvm, addr, range); 1985 if (!va) 1986 goto out; 1987 1988 if (va->va.addr != addr || 1989 va->va.range != range) 1990 goto out; 1991 1992 return va; 1993 1994 out: 1995 return NULL; 1996 } 1997 EXPORT_SYMBOL_GPL(drm_gpuva_find); 1998 1999 /** 2000 * drm_gpuva_find_prev() - find the &drm_gpuva before the given address 2001 * @gpuvm: the &drm_gpuvm to search in 2002 * @start: the given GPU VA's start address 2003 * 2004 * Find the adjacent &drm_gpuva before the GPU VA with given &start address. 2005 * 2006 * Note that if there is any free space between the GPU VA mappings no mapping 2007 * is returned. 2008 * 2009 * Returns: a pointer to the found &drm_gpuva or NULL if none was found 2010 */ 2011 struct drm_gpuva * 2012 drm_gpuva_find_prev(struct drm_gpuvm *gpuvm, u64 start) 2013 { 2014 if (!drm_gpuvm_range_valid(gpuvm, start - 1, 1)) 2015 return NULL; 2016 2017 return drm_gpuva_it_iter_first(&gpuvm->rb.tree, start - 1, start); 2018 } 2019 EXPORT_SYMBOL_GPL(drm_gpuva_find_prev); 2020 2021 /** 2022 * drm_gpuva_find_next() - find the &drm_gpuva after the given address 2023 * @gpuvm: the &drm_gpuvm to search in 2024 * @end: the given GPU VA's end address 2025 * 2026 * Find the adjacent &drm_gpuva after the GPU VA with given &end address. 2027 * 2028 * Note that if there is any free space between the GPU VA mappings no mapping 2029 * is returned. 2030 * 2031 * Returns: a pointer to the found &drm_gpuva or NULL if none was found 2032 */ 2033 struct drm_gpuva * 2034 drm_gpuva_find_next(struct drm_gpuvm *gpuvm, u64 end) 2035 { 2036 if (!drm_gpuvm_range_valid(gpuvm, end, 1)) 2037 return NULL; 2038 2039 return drm_gpuva_it_iter_first(&gpuvm->rb.tree, end, end + 1); 2040 } 2041 EXPORT_SYMBOL_GPL(drm_gpuva_find_next); 2042 2043 /** 2044 * drm_gpuvm_interval_empty() - indicate whether a given interval of the VA space 2045 * is empty 2046 * @gpuvm: the &drm_gpuvm to check the range for 2047 * @addr: the start address of the range 2048 * @range: the range of the interval 2049 * 2050 * Returns: true if the interval is empty, false otherwise 2051 */ 2052 bool 2053 drm_gpuvm_interval_empty(struct drm_gpuvm *gpuvm, u64 addr, u64 range) 2054 { 2055 return !drm_gpuva_find_first(gpuvm, addr, range); 2056 } 2057 EXPORT_SYMBOL_GPL(drm_gpuvm_interval_empty); 2058 2059 /** 2060 * drm_gpuva_map() - helper to insert a &drm_gpuva according to a 2061 * &drm_gpuva_op_map 2062 * @gpuvm: the &drm_gpuvm 2063 * @va: the &drm_gpuva to insert 2064 * @op: the &drm_gpuva_op_map to initialize @va with 2065 * 2066 * Initializes the @va from the @op and inserts it into the given @gpuvm. 2067 */ 2068 void 2069 drm_gpuva_map(struct drm_gpuvm *gpuvm, 2070 struct drm_gpuva *va, 2071 struct drm_gpuva_op_map *op) 2072 { 2073 drm_gpuva_init_from_op(va, op); 2074 drm_gpuva_insert(gpuvm, va); 2075 } 2076 EXPORT_SYMBOL_GPL(drm_gpuva_map); 2077 2078 /** 2079 * drm_gpuva_remap() - helper to remap a &drm_gpuva according to a 2080 * &drm_gpuva_op_remap 2081 * @prev: the &drm_gpuva to remap when keeping the start of a mapping 2082 * @next: the &drm_gpuva to remap when keeping the end of a mapping 2083 * @op: the &drm_gpuva_op_remap to initialize @prev and @next with 2084 * 2085 * Removes the currently mapped &drm_gpuva and remaps it using @prev and/or 2086 * @next. 2087 */ 2088 void 2089 drm_gpuva_remap(struct drm_gpuva *prev, 2090 struct drm_gpuva *next, 2091 struct drm_gpuva_op_remap *op) 2092 { 2093 struct drm_gpuva *va = op->unmap->va; 2094 struct drm_gpuvm *gpuvm = va->vm; 2095 2096 drm_gpuva_remove(va); 2097 2098 if (op->prev) { 2099 drm_gpuva_init_from_op(prev, op->prev); 2100 drm_gpuva_insert(gpuvm, prev); 2101 } 2102 2103 if (op->next) { 2104 drm_gpuva_init_from_op(next, op->next); 2105 drm_gpuva_insert(gpuvm, next); 2106 } 2107 } 2108 EXPORT_SYMBOL_GPL(drm_gpuva_remap); 2109 2110 /** 2111 * drm_gpuva_unmap() - helper to remove a &drm_gpuva according to a 2112 * &drm_gpuva_op_unmap 2113 * @op: the &drm_gpuva_op_unmap specifying the &drm_gpuva to remove 2114 * 2115 * Removes the &drm_gpuva associated with the &drm_gpuva_op_unmap. 2116 */ 2117 void 2118 drm_gpuva_unmap(struct drm_gpuva_op_unmap *op) 2119 { 2120 drm_gpuva_remove(op->va); 2121 } 2122 EXPORT_SYMBOL_GPL(drm_gpuva_unmap); 2123 2124 static int 2125 op_map_cb(const struct drm_gpuvm_ops *fn, void *priv, 2126 const struct drm_gpuvm_map_req *req) 2127 { 2128 struct drm_gpuva_op op = {}; 2129 2130 if (!req) 2131 return 0; 2132 2133 op.op = DRM_GPUVA_OP_MAP; 2134 op.map.va.addr = req->map.va.addr; 2135 op.map.va.range = req->map.va.range; 2136 op.map.gem.obj = req->map.gem.obj; 2137 op.map.gem.offset = req->map.gem.offset; 2138 2139 return fn->sm_step_map(&op, priv); 2140 } 2141 2142 static int 2143 op_remap_cb(const struct drm_gpuvm_ops *fn, void *priv, 2144 struct drm_gpuva_op_map *prev, 2145 struct drm_gpuva_op_map *next, 2146 struct drm_gpuva_op_unmap *unmap) 2147 { 2148 struct drm_gpuva_op op = {}; 2149 struct drm_gpuva_op_remap *r; 2150 2151 op.op = DRM_GPUVA_OP_REMAP; 2152 r = &op.remap; 2153 r->prev = prev; 2154 r->next = next; 2155 r->unmap = unmap; 2156 2157 return fn->sm_step_remap(&op, priv); 2158 } 2159 2160 static int 2161 op_unmap_cb(const struct drm_gpuvm_ops *fn, void *priv, 2162 struct drm_gpuva *va, bool merge, bool madvise) 2163 { 2164 struct drm_gpuva_op op = {}; 2165 2166 if (madvise) 2167 return 0; 2168 2169 op.op = DRM_GPUVA_OP_UNMAP; 2170 op.unmap.va = va; 2171 op.unmap.keep = merge; 2172 2173 return fn->sm_step_unmap(&op, priv); 2174 } 2175 2176 static int 2177 __drm_gpuvm_sm_map(struct drm_gpuvm *gpuvm, 2178 const struct drm_gpuvm_ops *ops, void *priv, 2179 const struct drm_gpuvm_map_req *req, 2180 bool madvise) 2181 { 2182 struct drm_gem_object *req_obj = req->map.gem.obj; 2183 const struct drm_gpuvm_map_req *op_map = madvise ? NULL : req; 2184 struct drm_gpuva *va, *next; 2185 u64 req_offset = req->map.gem.offset; 2186 u64 req_range = req->map.va.range; 2187 u64 req_addr = req->map.va.addr; 2188 u64 req_end = req_addr + req_range; 2189 int ret; 2190 2191 if (unlikely(!drm_gpuvm_range_valid(gpuvm, req_addr, req_range))) 2192 return -EINVAL; 2193 2194 drm_gpuvm_for_each_va_range_safe(va, next, gpuvm, req_addr, req_end) { 2195 struct drm_gem_object *obj = va->gem.obj; 2196 u64 offset = va->gem.offset; 2197 u64 addr = va->va.addr; 2198 u64 range = va->va.range; 2199 u64 end = addr + range; 2200 bool merge = !!va->gem.obj; 2201 2202 if (madvise && obj) 2203 continue; 2204 2205 if (addr == req_addr) { 2206 merge &= obj == req_obj && 2207 offset == req_offset; 2208 2209 if (end == req_end) { 2210 ret = op_unmap_cb(ops, priv, va, merge, madvise); 2211 if (ret) 2212 return ret; 2213 break; 2214 } 2215 2216 if (end < req_end) { 2217 ret = op_unmap_cb(ops, priv, va, merge, madvise); 2218 if (ret) 2219 return ret; 2220 continue; 2221 } 2222 2223 if (end > req_end) { 2224 struct drm_gpuva_op_map n = { 2225 .va.addr = req_end, 2226 .va.range = range - req_range, 2227 .gem.obj = obj, 2228 .gem.offset = offset + req_range, 2229 }; 2230 struct drm_gpuva_op_unmap u = { 2231 .va = va, 2232 .keep = merge, 2233 }; 2234 2235 ret = op_remap_cb(ops, priv, NULL, &n, &u); 2236 if (ret) 2237 return ret; 2238 2239 if (madvise) 2240 op_map = req; 2241 break; 2242 } 2243 } else if (addr < req_addr) { 2244 u64 ls_range = req_addr - addr; 2245 struct drm_gpuva_op_map p = { 2246 .va.addr = addr, 2247 .va.range = ls_range, 2248 .gem.obj = obj, 2249 .gem.offset = offset, 2250 }; 2251 struct drm_gpuva_op_unmap u = { .va = va }; 2252 2253 merge &= obj == req_obj && 2254 offset + ls_range == req_offset; 2255 u.keep = merge; 2256 2257 if (end == req_end) { 2258 ret = op_remap_cb(ops, priv, &p, NULL, &u); 2259 if (ret) 2260 return ret; 2261 2262 if (madvise) 2263 op_map = req; 2264 break; 2265 } 2266 2267 if (end < req_end) { 2268 ret = op_remap_cb(ops, priv, &p, NULL, &u); 2269 if (ret) 2270 return ret; 2271 2272 if (madvise) { 2273 struct drm_gpuvm_map_req map_req = { 2274 .map.va.addr = req_addr, 2275 .map.va.range = end - req_addr, 2276 }; 2277 2278 ret = op_map_cb(ops, priv, &map_req); 2279 if (ret) 2280 return ret; 2281 } 2282 2283 continue; 2284 } 2285 2286 if (end > req_end) { 2287 struct drm_gpuva_op_map n = { 2288 .va.addr = req_end, 2289 .va.range = end - req_end, 2290 .gem.obj = obj, 2291 .gem.offset = offset + ls_range + 2292 req_range, 2293 }; 2294 2295 ret = op_remap_cb(ops, priv, &p, &n, &u); 2296 if (ret) 2297 return ret; 2298 2299 if (madvise) 2300 op_map = req; 2301 break; 2302 } 2303 } else if (addr > req_addr) { 2304 merge &= obj == req_obj && 2305 offset == req_offset + 2306 (addr - req_addr); 2307 2308 if (end == req_end) { 2309 ret = op_unmap_cb(ops, priv, va, merge, madvise); 2310 if (ret) 2311 return ret; 2312 2313 break; 2314 } 2315 2316 if (end < req_end) { 2317 ret = op_unmap_cb(ops, priv, va, merge, madvise); 2318 if (ret) 2319 return ret; 2320 2321 continue; 2322 } 2323 2324 if (end > req_end) { 2325 struct drm_gpuva_op_map n = { 2326 .va.addr = req_end, 2327 .va.range = end - req_end, 2328 .gem.obj = obj, 2329 .gem.offset = offset + req_end - addr, 2330 }; 2331 struct drm_gpuva_op_unmap u = { 2332 .va = va, 2333 .keep = merge, 2334 }; 2335 2336 ret = op_remap_cb(ops, priv, NULL, &n, &u); 2337 if (ret) 2338 return ret; 2339 2340 if (madvise) { 2341 struct drm_gpuvm_map_req map_req = { 2342 .map.va.addr = addr, 2343 .map.va.range = req_end - addr, 2344 }; 2345 2346 return op_map_cb(ops, priv, &map_req); 2347 } 2348 break; 2349 } 2350 } 2351 } 2352 return op_map_cb(ops, priv, op_map); 2353 } 2354 2355 static int 2356 __drm_gpuvm_sm_unmap(struct drm_gpuvm *gpuvm, 2357 const struct drm_gpuvm_ops *ops, void *priv, 2358 u64 req_addr, u64 req_range) 2359 { 2360 struct drm_gpuva *va, *next; 2361 u64 req_end = req_addr + req_range; 2362 int ret; 2363 2364 if (unlikely(!drm_gpuvm_range_valid(gpuvm, req_addr, req_range))) 2365 return -EINVAL; 2366 2367 drm_gpuvm_for_each_va_range_safe(va, next, gpuvm, req_addr, req_end) { 2368 struct drm_gpuva_op_map prev = {}, next = {}; 2369 bool prev_split = false, next_split = false; 2370 struct drm_gem_object *obj = va->gem.obj; 2371 u64 offset = va->gem.offset; 2372 u64 addr = va->va.addr; 2373 u64 range = va->va.range; 2374 u64 end = addr + range; 2375 2376 if (addr < req_addr) { 2377 prev.va.addr = addr; 2378 prev.va.range = req_addr - addr; 2379 prev.gem.obj = obj; 2380 prev.gem.offset = offset; 2381 2382 prev_split = true; 2383 } 2384 2385 if (end > req_end) { 2386 next.va.addr = req_end; 2387 next.va.range = end - req_end; 2388 next.gem.obj = obj; 2389 next.gem.offset = offset + (req_end - addr); 2390 2391 next_split = true; 2392 } 2393 2394 if (prev_split || next_split) { 2395 struct drm_gpuva_op_unmap unmap = { .va = va }; 2396 2397 ret = op_remap_cb(ops, priv, 2398 prev_split ? &prev : NULL, 2399 next_split ? &next : NULL, 2400 &unmap); 2401 if (ret) 2402 return ret; 2403 } else { 2404 ret = op_unmap_cb(ops, priv, va, false, false); 2405 if (ret) 2406 return ret; 2407 } 2408 } 2409 2410 return 0; 2411 } 2412 2413 /** 2414 * drm_gpuvm_sm_map() - calls the &drm_gpuva_op split/merge steps 2415 * @gpuvm: the &drm_gpuvm representing the GPU VA space 2416 * @priv: pointer to a driver private data structure 2417 * @req: ptr to struct drm_gpuvm_map_req 2418 * 2419 * This function iterates the given range of the GPU VA space. It utilizes the 2420 * &drm_gpuvm_ops to call back into the driver providing the split and merge 2421 * steps. 2422 * 2423 * Drivers may use these callbacks to update the GPU VA space right away within 2424 * the callback. In case the driver decides to copy and store the operations for 2425 * later processing neither this function nor &drm_gpuvm_sm_unmap is allowed to 2426 * be called before the &drm_gpuvm's view of the GPU VA space was 2427 * updated with the previous set of operations. To update the 2428 * &drm_gpuvm's view of the GPU VA space drm_gpuva_insert(), 2429 * drm_gpuva_destroy_locked() and/or drm_gpuva_destroy_unlocked() should be 2430 * used. 2431 * 2432 * A sequence of callbacks can contain map, unmap and remap operations, but 2433 * the sequence of callbacks might also be empty if no operation is required, 2434 * e.g. if the requested mapping already exists in the exact same way. 2435 * 2436 * There can be an arbitrary amount of unmap operations, a maximum of two remap 2437 * operations and a single map operation. The latter one represents the original 2438 * map operation requested by the caller. 2439 * 2440 * Returns: 0 on success or a negative error code 2441 */ 2442 int 2443 drm_gpuvm_sm_map(struct drm_gpuvm *gpuvm, void *priv, 2444 const struct drm_gpuvm_map_req *req) 2445 { 2446 const struct drm_gpuvm_ops *ops = gpuvm->ops; 2447 2448 if (unlikely(!(ops && ops->sm_step_map && 2449 ops->sm_step_remap && 2450 ops->sm_step_unmap))) 2451 return -EINVAL; 2452 2453 return __drm_gpuvm_sm_map(gpuvm, ops, priv, req, false); 2454 } 2455 EXPORT_SYMBOL_GPL(drm_gpuvm_sm_map); 2456 2457 /** 2458 * drm_gpuvm_sm_unmap() - calls the &drm_gpuva_ops to split on unmap 2459 * @gpuvm: the &drm_gpuvm representing the GPU VA space 2460 * @priv: pointer to a driver private data structure 2461 * @req_addr: the start address of the range to unmap 2462 * @req_range: the range of the mappings to unmap 2463 * 2464 * This function iterates the given range of the GPU VA space. It utilizes the 2465 * &drm_gpuvm_ops to call back into the driver providing the operations to 2466 * unmap and, if required, split existing mappings. 2467 * 2468 * Drivers may use these callbacks to update the GPU VA space right away within 2469 * the callback. In case the driver decides to copy and store the operations for 2470 * later processing neither this function nor &drm_gpuvm_sm_map is allowed to be 2471 * called before the &drm_gpuvm's view of the GPU VA space was updated 2472 * with the previous set of operations. To update the &drm_gpuvm's view 2473 * of the GPU VA space drm_gpuva_insert(), drm_gpuva_destroy_locked() and/or 2474 * drm_gpuva_destroy_unlocked() should be used. 2475 * 2476 * A sequence of callbacks can contain unmap and remap operations, depending on 2477 * whether there are actual overlapping mappings to split. 2478 * 2479 * There can be an arbitrary amount of unmap operations and a maximum of two 2480 * remap operations. 2481 * 2482 * Returns: 0 on success or a negative error code 2483 */ 2484 int 2485 drm_gpuvm_sm_unmap(struct drm_gpuvm *gpuvm, void *priv, 2486 u64 req_addr, u64 req_range) 2487 { 2488 const struct drm_gpuvm_ops *ops = gpuvm->ops; 2489 2490 if (unlikely(!(ops && ops->sm_step_remap && 2491 ops->sm_step_unmap))) 2492 return -EINVAL; 2493 2494 return __drm_gpuvm_sm_unmap(gpuvm, ops, priv, 2495 req_addr, req_range); 2496 } 2497 EXPORT_SYMBOL_GPL(drm_gpuvm_sm_unmap); 2498 2499 static int 2500 drm_gpuva_sm_step_lock(struct drm_gpuva_op *op, void *priv) 2501 { 2502 struct drm_exec *exec = priv; 2503 2504 switch (op->op) { 2505 case DRM_GPUVA_OP_REMAP: 2506 if (op->remap.unmap->va->gem.obj) 2507 return drm_exec_lock_obj(exec, op->remap.unmap->va->gem.obj); 2508 return 0; 2509 case DRM_GPUVA_OP_UNMAP: 2510 if (op->unmap.va->gem.obj) 2511 return drm_exec_lock_obj(exec, op->unmap.va->gem.obj); 2512 return 0; 2513 default: 2514 return 0; 2515 } 2516 } 2517 2518 static const struct drm_gpuvm_ops lock_ops = { 2519 .sm_step_map = drm_gpuva_sm_step_lock, 2520 .sm_step_remap = drm_gpuva_sm_step_lock, 2521 .sm_step_unmap = drm_gpuva_sm_step_lock, 2522 }; 2523 2524 /** 2525 * drm_gpuvm_sm_map_exec_lock() - locks the objects touched by a drm_gpuvm_sm_map() 2526 * @gpuvm: the &drm_gpuvm representing the GPU VA space 2527 * @exec: the &drm_exec locking context 2528 * @num_fences: for newly mapped objects, the # of fences to reserve 2529 * @req: ptr to drm_gpuvm_map_req struct 2530 * 2531 * This function locks (drm_exec_lock_obj()) objects that will be unmapped/ 2532 * remapped, and locks+prepares (drm_exec_prepare_object()) objects that 2533 * will be newly mapped. 2534 * 2535 * The expected usage is:: 2536 * 2537 * vm_bind { 2538 * struct drm_exec exec; 2539 * 2540 * // IGNORE_DUPLICATES is required, INTERRUPTIBLE_WAIT is recommended: 2541 * drm_exec_init(&exec, IGNORE_DUPLICATES | INTERRUPTIBLE_WAIT, 0); 2542 * 2543 * drm_exec_until_all_locked (&exec) { 2544 * for_each_vm_bind_operation { 2545 * switch (op->op) { 2546 * case DRIVER_OP_UNMAP: 2547 * ret = drm_gpuvm_sm_unmap_exec_lock(gpuvm, &exec, op->addr, op->range); 2548 * break; 2549 * case DRIVER_OP_MAP: 2550 * ret = drm_gpuvm_sm_map_exec_lock(gpuvm, &exec, num_fences, &req); 2551 * break; 2552 * } 2553 * 2554 * drm_exec_retry_on_contention(&exec); 2555 * if (ret) 2556 * return ret; 2557 * } 2558 * } 2559 * } 2560 * 2561 * This enables all locking to be performed before the driver begins modifying 2562 * the VM. This is safe to do in the case of overlapping DRIVER_VM_BIND_OPs, 2563 * where an earlier op can alter the sequence of steps generated for a later 2564 * op, because the later altered step will involve the same GEM object(s) 2565 * already seen in the earlier locking step. For example: 2566 * 2567 * 1) An earlier driver DRIVER_OP_UNMAP op removes the need for a 2568 * DRM_GPUVA_OP_REMAP/UNMAP step. This is safe because we've already 2569 * locked the GEM object in the earlier DRIVER_OP_UNMAP op. 2570 * 2571 * 2) An earlier DRIVER_OP_MAP op overlaps with a later DRIVER_OP_MAP/UNMAP 2572 * op, introducing a DRM_GPUVA_OP_REMAP/UNMAP that wouldn't have been 2573 * required without the earlier DRIVER_OP_MAP. This is safe because we've 2574 * already locked the GEM object in the earlier DRIVER_OP_MAP step. 2575 * 2576 * Returns: 0 on success or a negative error code 2577 */ 2578 int 2579 drm_gpuvm_sm_map_exec_lock(struct drm_gpuvm *gpuvm, 2580 struct drm_exec *exec, unsigned int num_fences, 2581 struct drm_gpuvm_map_req *req) 2582 { 2583 struct drm_gem_object *req_obj = req->map.gem.obj; 2584 2585 if (req_obj) { 2586 int ret = drm_exec_prepare_obj(exec, req_obj, num_fences); 2587 if (ret) 2588 return ret; 2589 } 2590 2591 return __drm_gpuvm_sm_map(gpuvm, &lock_ops, exec, req, false); 2592 2593 } 2594 EXPORT_SYMBOL_GPL(drm_gpuvm_sm_map_exec_lock); 2595 2596 /** 2597 * drm_gpuvm_sm_unmap_exec_lock() - locks the objects touched by drm_gpuvm_sm_unmap() 2598 * @gpuvm: the &drm_gpuvm representing the GPU VA space 2599 * @exec: the &drm_exec locking context 2600 * @req_addr: the start address of the range to unmap 2601 * @req_range: the range of the mappings to unmap 2602 * 2603 * This function locks (drm_exec_lock_obj()) objects that will be unmapped/ 2604 * remapped by drm_gpuvm_sm_unmap(). 2605 * 2606 * See drm_gpuvm_sm_map_exec_lock() for expected usage. 2607 * 2608 * Returns: 0 on success or a negative error code 2609 */ 2610 int 2611 drm_gpuvm_sm_unmap_exec_lock(struct drm_gpuvm *gpuvm, struct drm_exec *exec, 2612 u64 req_addr, u64 req_range) 2613 { 2614 return __drm_gpuvm_sm_unmap(gpuvm, &lock_ops, exec, 2615 req_addr, req_range); 2616 } 2617 EXPORT_SYMBOL_GPL(drm_gpuvm_sm_unmap_exec_lock); 2618 2619 static struct drm_gpuva_op * 2620 gpuva_op_alloc(struct drm_gpuvm *gpuvm) 2621 { 2622 const struct drm_gpuvm_ops *fn = gpuvm->ops; 2623 struct drm_gpuva_op *op; 2624 2625 if (fn && fn->op_alloc) 2626 op = fn->op_alloc(); 2627 else 2628 op = kzalloc(sizeof(*op), GFP_KERNEL); 2629 2630 if (unlikely(!op)) 2631 return NULL; 2632 2633 return op; 2634 } 2635 2636 static void 2637 gpuva_op_free(struct drm_gpuvm *gpuvm, 2638 struct drm_gpuva_op *op) 2639 { 2640 const struct drm_gpuvm_ops *fn = gpuvm->ops; 2641 2642 if (fn && fn->op_free) 2643 fn->op_free(op); 2644 else 2645 kfree(op); 2646 } 2647 2648 static int 2649 drm_gpuva_sm_step(struct drm_gpuva_op *__op, 2650 void *priv) 2651 { 2652 struct { 2653 struct drm_gpuvm *vm; 2654 struct drm_gpuva_ops *ops; 2655 } *args = priv; 2656 struct drm_gpuvm *gpuvm = args->vm; 2657 struct drm_gpuva_ops *ops = args->ops; 2658 struct drm_gpuva_op *op; 2659 2660 op = gpuva_op_alloc(gpuvm); 2661 if (unlikely(!op)) 2662 goto err; 2663 2664 memcpy(op, __op, sizeof(*op)); 2665 2666 if (op->op == DRM_GPUVA_OP_REMAP) { 2667 struct drm_gpuva_op_remap *__r = &__op->remap; 2668 struct drm_gpuva_op_remap *r = &op->remap; 2669 2670 r->unmap = kmemdup(__r->unmap, sizeof(*r->unmap), 2671 GFP_KERNEL); 2672 if (unlikely(!r->unmap)) 2673 goto err_free_op; 2674 2675 if (__r->prev) { 2676 r->prev = kmemdup(__r->prev, sizeof(*r->prev), 2677 GFP_KERNEL); 2678 if (unlikely(!r->prev)) 2679 goto err_free_unmap; 2680 } 2681 2682 if (__r->next) { 2683 r->next = kmemdup(__r->next, sizeof(*r->next), 2684 GFP_KERNEL); 2685 if (unlikely(!r->next)) 2686 goto err_free_prev; 2687 } 2688 } 2689 2690 list_add_tail(&op->entry, &ops->list); 2691 2692 return 0; 2693 2694 err_free_unmap: 2695 kfree(op->remap.unmap); 2696 err_free_prev: 2697 kfree(op->remap.prev); 2698 err_free_op: 2699 gpuva_op_free(gpuvm, op); 2700 err: 2701 return -ENOMEM; 2702 } 2703 2704 static const struct drm_gpuvm_ops gpuvm_list_ops = { 2705 .sm_step_map = drm_gpuva_sm_step, 2706 .sm_step_remap = drm_gpuva_sm_step, 2707 .sm_step_unmap = drm_gpuva_sm_step, 2708 }; 2709 2710 static struct drm_gpuva_ops * 2711 __drm_gpuvm_sm_map_ops_create(struct drm_gpuvm *gpuvm, 2712 const struct drm_gpuvm_map_req *req, 2713 bool madvise) 2714 { 2715 struct drm_gpuva_ops *ops; 2716 struct { 2717 struct drm_gpuvm *vm; 2718 struct drm_gpuva_ops *ops; 2719 } args; 2720 int ret; 2721 2722 ops = kzalloc(sizeof(*ops), GFP_KERNEL); 2723 if (unlikely(!ops)) 2724 return ERR_PTR(-ENOMEM); 2725 2726 INIT_LIST_HEAD(&ops->list); 2727 2728 args.vm = gpuvm; 2729 args.ops = ops; 2730 2731 ret = __drm_gpuvm_sm_map(gpuvm, &gpuvm_list_ops, &args, req, madvise); 2732 if (ret) 2733 goto err_free_ops; 2734 2735 return ops; 2736 2737 err_free_ops: 2738 drm_gpuva_ops_free(gpuvm, ops); 2739 return ERR_PTR(ret); 2740 } 2741 2742 /** 2743 * drm_gpuvm_sm_map_ops_create() - creates the &drm_gpuva_ops to split and merge 2744 * @gpuvm: the &drm_gpuvm representing the GPU VA space 2745 * @req: map request arguments 2746 * 2747 * This function creates a list of operations to perform splitting and merging 2748 * of existing mapping(s) with the newly requested one. 2749 * 2750 * The list can be iterated with &drm_gpuva_for_each_op and must be processed 2751 * in the given order. It can contain map, unmap and remap operations, but it 2752 * also can be empty if no operation is required, e.g. if the requested mapping 2753 * already exists in the exact same way. 2754 * 2755 * There can be an arbitrary amount of unmap operations, a maximum of two remap 2756 * operations and a single map operation. The latter one represents the original 2757 * map operation requested by the caller. 2758 * 2759 * Note that before calling this function again with another mapping request it 2760 * is necessary to update the &drm_gpuvm's view of the GPU VA space. The 2761 * previously obtained operations must be either processed or abandoned. To 2762 * update the &drm_gpuvm's view of the GPU VA space drm_gpuva_insert(), 2763 * drm_gpuva_destroy_locked() and/or drm_gpuva_destroy_unlocked() should be 2764 * used. 2765 * 2766 * After the caller finished processing the returned &drm_gpuva_ops, they must 2767 * be freed with &drm_gpuva_ops_free. 2768 * 2769 * Returns: a pointer to the &drm_gpuva_ops on success, an ERR_PTR on failure 2770 */ 2771 struct drm_gpuva_ops * 2772 drm_gpuvm_sm_map_ops_create(struct drm_gpuvm *gpuvm, 2773 const struct drm_gpuvm_map_req *req) 2774 { 2775 return __drm_gpuvm_sm_map_ops_create(gpuvm, req, false); 2776 } 2777 EXPORT_SYMBOL_GPL(drm_gpuvm_sm_map_ops_create); 2778 2779 /** 2780 * drm_gpuvm_madvise_ops_create() - creates the &drm_gpuva_ops to split 2781 * @gpuvm: the &drm_gpuvm representing the GPU VA space 2782 * @req: map request arguments 2783 * 2784 * This function creates a list of operations to perform splitting 2785 * of existent mapping(s) at start or end, based on the request map. 2786 * 2787 * The list can be iterated with &drm_gpuva_for_each_op and must be processed 2788 * in the given order. It can contain map and remap operations, but it 2789 * also can be empty if no operation is required, e.g. if the requested mapping 2790 * already exists is the exact same way. 2791 * 2792 * There will be no unmap operations, a maximum of two remap operations and two 2793 * map operations. The two map operations correspond to: one from start to the 2794 * end of drm_gpuvaX, and another from the start of drm_gpuvaY to end. 2795 * 2796 * Note that before calling this function again with another mapping request it 2797 * is necessary to update the &drm_gpuvm's view of the GPU VA space. The 2798 * previously obtained operations must be either processed or abandoned. To 2799 * update the &drm_gpuvm's view of the GPU VA space drm_gpuva_insert(), 2800 * drm_gpuva_destroy_locked() and/or drm_gpuva_destroy_unlocked() should be 2801 * used. 2802 * 2803 * After the caller finished processing the returned &drm_gpuva_ops, they must 2804 * be freed with &drm_gpuva_ops_free. 2805 * 2806 * Returns: a pointer to the &drm_gpuva_ops on success, an ERR_PTR on failure 2807 */ 2808 struct drm_gpuva_ops * 2809 drm_gpuvm_madvise_ops_create(struct drm_gpuvm *gpuvm, 2810 const struct drm_gpuvm_map_req *req) 2811 { 2812 return __drm_gpuvm_sm_map_ops_create(gpuvm, req, true); 2813 } 2814 EXPORT_SYMBOL_GPL(drm_gpuvm_madvise_ops_create); 2815 2816 /** 2817 * drm_gpuvm_sm_unmap_ops_create() - creates the &drm_gpuva_ops to split on 2818 * unmap 2819 * @gpuvm: the &drm_gpuvm representing the GPU VA space 2820 * @req_addr: the start address of the range to unmap 2821 * @req_range: the range of the mappings to unmap 2822 * 2823 * This function creates a list of operations to perform unmapping and, if 2824 * required, splitting of the mappings overlapping the unmap range. 2825 * 2826 * The list can be iterated with &drm_gpuva_for_each_op and must be processed 2827 * in the given order. It can contain unmap and remap operations, depending on 2828 * whether there are actual overlapping mappings to split. 2829 * 2830 * There can be an arbitrary amount of unmap operations and a maximum of two 2831 * remap operations. 2832 * 2833 * Note that before calling this function again with another range to unmap it 2834 * is necessary to update the &drm_gpuvm's view of the GPU VA space. The 2835 * previously obtained operations must be processed or abandoned. To update the 2836 * &drm_gpuvm's view of the GPU VA space drm_gpuva_insert(), 2837 * drm_gpuva_destroy_locked() and/or drm_gpuva_destroy_unlocked() should be 2838 * used. 2839 * 2840 * After the caller finished processing the returned &drm_gpuva_ops, they must 2841 * be freed with &drm_gpuva_ops_free. 2842 * 2843 * Returns: a pointer to the &drm_gpuva_ops on success, an ERR_PTR on failure 2844 */ 2845 struct drm_gpuva_ops * 2846 drm_gpuvm_sm_unmap_ops_create(struct drm_gpuvm *gpuvm, 2847 u64 req_addr, u64 req_range) 2848 { 2849 struct drm_gpuva_ops *ops; 2850 struct { 2851 struct drm_gpuvm *vm; 2852 struct drm_gpuva_ops *ops; 2853 } args; 2854 int ret; 2855 2856 ops = kzalloc(sizeof(*ops), GFP_KERNEL); 2857 if (unlikely(!ops)) 2858 return ERR_PTR(-ENOMEM); 2859 2860 INIT_LIST_HEAD(&ops->list); 2861 2862 args.vm = gpuvm; 2863 args.ops = ops; 2864 2865 ret = __drm_gpuvm_sm_unmap(gpuvm, &gpuvm_list_ops, &args, 2866 req_addr, req_range); 2867 if (ret) 2868 goto err_free_ops; 2869 2870 return ops; 2871 2872 err_free_ops: 2873 drm_gpuva_ops_free(gpuvm, ops); 2874 return ERR_PTR(ret); 2875 } 2876 EXPORT_SYMBOL_GPL(drm_gpuvm_sm_unmap_ops_create); 2877 2878 /** 2879 * drm_gpuvm_prefetch_ops_create() - creates the &drm_gpuva_ops to prefetch 2880 * @gpuvm: the &drm_gpuvm representing the GPU VA space 2881 * @addr: the start address of the range to prefetch 2882 * @range: the range of the mappings to prefetch 2883 * 2884 * This function creates a list of operations to perform prefetching. 2885 * 2886 * The list can be iterated with &drm_gpuva_for_each_op and must be processed 2887 * in the given order. It can contain prefetch operations. 2888 * 2889 * There can be an arbitrary amount of prefetch operations. 2890 * 2891 * After the caller finished processing the returned &drm_gpuva_ops, they must 2892 * be freed with &drm_gpuva_ops_free. 2893 * 2894 * Returns: a pointer to the &drm_gpuva_ops on success, an ERR_PTR on failure 2895 */ 2896 struct drm_gpuva_ops * 2897 drm_gpuvm_prefetch_ops_create(struct drm_gpuvm *gpuvm, 2898 u64 addr, u64 range) 2899 { 2900 struct drm_gpuva_ops *ops; 2901 struct drm_gpuva_op *op; 2902 struct drm_gpuva *va; 2903 u64 end = addr + range; 2904 int ret; 2905 2906 ops = kzalloc(sizeof(*ops), GFP_KERNEL); 2907 if (!ops) 2908 return ERR_PTR(-ENOMEM); 2909 2910 INIT_LIST_HEAD(&ops->list); 2911 2912 drm_gpuvm_for_each_va_range(va, gpuvm, addr, end) { 2913 op = gpuva_op_alloc(gpuvm); 2914 if (!op) { 2915 ret = -ENOMEM; 2916 goto err_free_ops; 2917 } 2918 2919 op->op = DRM_GPUVA_OP_PREFETCH; 2920 op->prefetch.va = va; 2921 list_add_tail(&op->entry, &ops->list); 2922 } 2923 2924 return ops; 2925 2926 err_free_ops: 2927 drm_gpuva_ops_free(gpuvm, ops); 2928 return ERR_PTR(ret); 2929 } 2930 EXPORT_SYMBOL_GPL(drm_gpuvm_prefetch_ops_create); 2931 2932 /** 2933 * drm_gpuvm_bo_unmap_ops_create() - creates the &drm_gpuva_ops to unmap a GEM 2934 * @vm_bo: the &drm_gpuvm_bo abstraction 2935 * 2936 * This function creates a list of operations to perform unmapping for every 2937 * GPUVA attached to a GEM. 2938 * 2939 * The list can be iterated with &drm_gpuva_for_each_op and consists out of an 2940 * arbitrary amount of unmap operations. 2941 * 2942 * After the caller finished processing the returned &drm_gpuva_ops, they must 2943 * be freed with &drm_gpuva_ops_free. 2944 * 2945 * This function expects the caller to protect the GEM's GPUVA list against 2946 * concurrent access using either the GEM's dma-resv or gpuva.lock mutex. 2947 * 2948 * Returns: a pointer to the &drm_gpuva_ops on success, an ERR_PTR on failure 2949 */ 2950 struct drm_gpuva_ops * 2951 drm_gpuvm_bo_unmap_ops_create(struct drm_gpuvm_bo *vm_bo) 2952 { 2953 struct drm_gpuva_ops *ops; 2954 struct drm_gpuva_op *op; 2955 struct drm_gpuva *va; 2956 int ret; 2957 2958 drm_gem_gpuva_assert_lock_held(vm_bo->vm, vm_bo->obj); 2959 2960 ops = kzalloc(sizeof(*ops), GFP_KERNEL); 2961 if (!ops) 2962 return ERR_PTR(-ENOMEM); 2963 2964 INIT_LIST_HEAD(&ops->list); 2965 2966 drm_gpuvm_bo_for_each_va(va, vm_bo) { 2967 op = gpuva_op_alloc(vm_bo->vm); 2968 if (!op) { 2969 ret = -ENOMEM; 2970 goto err_free_ops; 2971 } 2972 2973 op->op = DRM_GPUVA_OP_UNMAP; 2974 op->unmap.va = va; 2975 list_add_tail(&op->entry, &ops->list); 2976 } 2977 2978 return ops; 2979 2980 err_free_ops: 2981 drm_gpuva_ops_free(vm_bo->vm, ops); 2982 return ERR_PTR(ret); 2983 } 2984 EXPORT_SYMBOL_GPL(drm_gpuvm_bo_unmap_ops_create); 2985 2986 /** 2987 * drm_gpuva_ops_free() - free the given &drm_gpuva_ops 2988 * @gpuvm: the &drm_gpuvm the ops were created for 2989 * @ops: the &drm_gpuva_ops to free 2990 * 2991 * Frees the given &drm_gpuva_ops structure including all the ops associated 2992 * with it. 2993 */ 2994 void 2995 drm_gpuva_ops_free(struct drm_gpuvm *gpuvm, 2996 struct drm_gpuva_ops *ops) 2997 { 2998 struct drm_gpuva_op *op, *next; 2999 3000 drm_gpuva_for_each_op_safe(op, next, ops) { 3001 list_del(&op->entry); 3002 3003 if (op->op == DRM_GPUVA_OP_REMAP) { 3004 kfree(op->remap.prev); 3005 kfree(op->remap.next); 3006 kfree(op->remap.unmap); 3007 } 3008 3009 gpuva_op_free(gpuvm, op); 3010 } 3011 3012 kfree(ops); 3013 } 3014 EXPORT_SYMBOL_GPL(drm_gpuva_ops_free); 3015 3016 MODULE_DESCRIPTION("DRM GPUVM"); 3017 MODULE_LICENSE("GPL"); 3018