1 /* 2 * Copyright 2008 Advanced Micro Devices, Inc. 3 * Copyright 2008 Red Hat Inc. 4 * Copyright 2009 Jerome Glisse. 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the "Software"), 8 * to deal in the Software without restriction, including without limitation 9 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 * and/or sell copies of the Software, and to permit persons to whom the 11 * Software is furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be included in 14 * all copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 * OTHER DEALINGS IN THE SOFTWARE. 23 * 24 * Authors: Dave Airlie 25 * Alex Deucher 26 * Jerome Glisse 27 */ 28 29 #include <linux/dma-fence-array.h> 30 #include <linux/interval_tree_generic.h> 31 #include <linux/idr.h> 32 #include <linux/dma-buf.h> 33 34 #include <drm/amdgpu_drm.h> 35 #include <drm/drm_drv.h> 36 #include <drm/ttm/ttm_tt.h> 37 #include <drm/drm_exec.h> 38 #include "amdgpu.h" 39 #include "amdgpu_vm.h" 40 #include "amdgpu_trace.h" 41 #include "amdgpu_amdkfd.h" 42 #include "amdgpu_gmc.h" 43 #include "amdgpu_xgmi.h" 44 #include "amdgpu_dma_buf.h" 45 #include "amdgpu_res_cursor.h" 46 #include "kfd_svm.h" 47 48 /** 49 * DOC: GPUVM 50 * 51 * GPUVM is the MMU functionality provided on the GPU. 52 * GPUVM is similar to the legacy GART on older asics, however 53 * rather than there being a single global GART table 54 * for the entire GPU, there can be multiple GPUVM page tables active 55 * at any given time. The GPUVM page tables can contain a mix 56 * VRAM pages and system pages (both memory and MMIO) and system pages 57 * can be mapped as snooped (cached system pages) or unsnooped 58 * (uncached system pages). 59 * 60 * Each active GPUVM has an ID associated with it and there is a page table 61 * linked with each VMID. When executing a command buffer, 62 * the kernel tells the engine what VMID to use for that command 63 * buffer. VMIDs are allocated dynamically as commands are submitted. 64 * The userspace drivers maintain their own address space and the kernel 65 * sets up their pages tables accordingly when they submit their 66 * command buffers and a VMID is assigned. 67 * The hardware supports up to 16 active GPUVMs at any given time. 68 * 69 * Each GPUVM is represented by a 1-2 or 1-5 level page table, depending 70 * on the ASIC family. GPUVM supports RWX attributes on each page as well 71 * as other features such as encryption and caching attributes. 72 * 73 * VMID 0 is special. It is the GPUVM used for the kernel driver. In 74 * addition to an aperture managed by a page table, VMID 0 also has 75 * several other apertures. There is an aperture for direct access to VRAM 76 * and there is a legacy AGP aperture which just forwards accesses directly 77 * to the matching system physical addresses (or IOVAs when an IOMMU is 78 * present). These apertures provide direct access to these memories without 79 * incurring the overhead of a page table. VMID 0 is used by the kernel 80 * driver for tasks like memory management. 81 * 82 * GPU clients (i.e., engines on the GPU) use GPUVM VMIDs to access memory. 83 * For user applications, each application can have their own unique GPUVM 84 * address space. The application manages the address space and the kernel 85 * driver manages the GPUVM page tables for each process. If an GPU client 86 * accesses an invalid page, it will generate a GPU page fault, similar to 87 * accessing an invalid page on a CPU. 88 */ 89 90 #define START(node) ((node)->start) 91 #define LAST(node) ((node)->last) 92 93 INTERVAL_TREE_DEFINE(struct amdgpu_bo_va_mapping, rb, uint64_t, __subtree_last, 94 START, LAST, static, amdgpu_vm_it) 95 96 #undef START 97 #undef LAST 98 99 /** 100 * struct amdgpu_prt_cb - Helper to disable partial resident texture feature from a fence callback 101 */ 102 struct amdgpu_prt_cb { 103 104 /** 105 * @adev: amdgpu device 106 */ 107 struct amdgpu_device *adev; 108 109 /** 110 * @cb: callback 111 */ 112 struct dma_fence_cb cb; 113 }; 114 115 /** 116 * struct amdgpu_vm_tlb_seq_struct - Helper to increment the TLB flush sequence 117 */ 118 struct amdgpu_vm_tlb_seq_struct { 119 /** 120 * @vm: pointer to the amdgpu_vm structure to set the fence sequence on 121 */ 122 struct amdgpu_vm *vm; 123 124 /** 125 * @cb: callback 126 */ 127 struct dma_fence_cb cb; 128 }; 129 130 /** 131 * amdgpu_vm_assert_locked - check if VM is correctly locked 132 * @vm: the VM which schould be tested 133 * 134 * Asserts that the VM root PD is locked. 135 */ 136 static void amdgpu_vm_assert_locked(struct amdgpu_vm *vm) 137 { 138 dma_resv_assert_held(vm->root.bo->tbo.base.resv); 139 } 140 141 /** 142 * amdgpu_vm_bo_evicted - vm_bo is evicted 143 * 144 * @vm_bo: vm_bo which is evicted 145 * 146 * State for PDs/PTs and per VM BOs which are not at the location they should 147 * be. 148 */ 149 static void amdgpu_vm_bo_evicted(struct amdgpu_vm_bo_base *vm_bo) 150 { 151 struct amdgpu_vm *vm = vm_bo->vm; 152 struct amdgpu_bo *bo = vm_bo->bo; 153 154 vm_bo->moved = true; 155 amdgpu_vm_assert_locked(vm); 156 if (bo->tbo.type == ttm_bo_type_kernel) 157 list_move(&vm_bo->vm_status, &vm->evicted); 158 else 159 list_move_tail(&vm_bo->vm_status, &vm->evicted); 160 } 161 /** 162 * amdgpu_vm_bo_moved - vm_bo is moved 163 * 164 * @vm_bo: vm_bo which is moved 165 * 166 * State for per VM BOs which are moved, but that change is not yet reflected 167 * in the page tables. 168 */ 169 static void amdgpu_vm_bo_moved(struct amdgpu_vm_bo_base *vm_bo) 170 { 171 amdgpu_vm_assert_locked(vm_bo->vm); 172 list_move(&vm_bo->vm_status, &vm_bo->vm->moved); 173 } 174 175 /** 176 * amdgpu_vm_bo_idle - vm_bo is idle 177 * 178 * @vm_bo: vm_bo which is now idle 179 * 180 * State for PDs/PTs and per VM BOs which have gone through the state machine 181 * and are now idle. 182 */ 183 static void amdgpu_vm_bo_idle(struct amdgpu_vm_bo_base *vm_bo) 184 { 185 amdgpu_vm_assert_locked(vm_bo->vm); 186 list_move(&vm_bo->vm_status, &vm_bo->vm->idle); 187 vm_bo->moved = false; 188 } 189 190 /** 191 * amdgpu_vm_bo_invalidated - vm_bo is invalidated 192 * 193 * @vm_bo: vm_bo which is now invalidated 194 * 195 * State for normal BOs which are invalidated and that change not yet reflected 196 * in the PTs. 197 */ 198 static void amdgpu_vm_bo_invalidated(struct amdgpu_vm_bo_base *vm_bo) 199 { 200 spin_lock(&vm_bo->vm->invalidated_lock); 201 list_move(&vm_bo->vm_status, &vm_bo->vm->invalidated); 202 spin_unlock(&vm_bo->vm->invalidated_lock); 203 } 204 205 /** 206 * amdgpu_vm_bo_evicted_user - vm_bo is evicted 207 * 208 * @vm_bo: vm_bo which is evicted 209 * 210 * State for BOs used by user mode queues which are not at the location they 211 * should be. 212 */ 213 static void amdgpu_vm_bo_evicted_user(struct amdgpu_vm_bo_base *vm_bo) 214 { 215 amdgpu_vm_assert_locked(vm_bo->vm); 216 vm_bo->moved = true; 217 list_move(&vm_bo->vm_status, &vm_bo->vm->evicted_user); 218 } 219 220 /** 221 * amdgpu_vm_bo_relocated - vm_bo is reloacted 222 * 223 * @vm_bo: vm_bo which is relocated 224 * 225 * State for PDs/PTs which needs to update their parent PD. 226 * For the root PD, just move to idle state. 227 */ 228 static void amdgpu_vm_bo_relocated(struct amdgpu_vm_bo_base *vm_bo) 229 { 230 amdgpu_vm_assert_locked(vm_bo->vm); 231 if (vm_bo->bo->parent) 232 list_move(&vm_bo->vm_status, &vm_bo->vm->relocated); 233 else 234 amdgpu_vm_bo_idle(vm_bo); 235 } 236 237 /** 238 * amdgpu_vm_bo_done - vm_bo is done 239 * 240 * @vm_bo: vm_bo which is now done 241 * 242 * State for normal BOs which are invalidated and that change has been updated 243 * in the PTs. 244 */ 245 static void amdgpu_vm_bo_done(struct amdgpu_vm_bo_base *vm_bo) 246 { 247 amdgpu_vm_assert_locked(vm_bo->vm); 248 list_move(&vm_bo->vm_status, &vm_bo->vm->done); 249 } 250 251 /** 252 * amdgpu_vm_bo_reset_state_machine - reset the vm_bo state machine 253 * @vm: the VM which state machine to reset 254 * 255 * Move all vm_bo object in the VM into a state where they will be updated 256 * again during validation. 257 */ 258 static void amdgpu_vm_bo_reset_state_machine(struct amdgpu_vm *vm) 259 { 260 struct amdgpu_vm_bo_base *vm_bo, *tmp; 261 262 spin_lock(&vm->invalidated_lock); 263 list_splice_init(&vm->done, &vm->invalidated); 264 list_for_each_entry(vm_bo, &vm->invalidated, vm_status) 265 vm_bo->moved = true; 266 spin_unlock(&vm->invalidated_lock); 267 268 amdgpu_vm_assert_locked(vm); 269 list_for_each_entry_safe(vm_bo, tmp, &vm->idle, vm_status) { 270 struct amdgpu_bo *bo = vm_bo->bo; 271 272 vm_bo->moved = true; 273 if (!bo || bo->tbo.type != ttm_bo_type_kernel) 274 list_move(&vm_bo->vm_status, &vm_bo->vm->moved); 275 else if (bo->parent) 276 list_move(&vm_bo->vm_status, &vm_bo->vm->relocated); 277 } 278 } 279 280 /** 281 * amdgpu_vm_update_shared - helper to update shared memory stat 282 * @base: base structure for tracking BO usage in a VM 283 * 284 * Takes the vm stats_lock and updates the shared memory stat. If the basic 285 * stat changed (e.g. buffer was moved) amdgpu_vm_update_stats need to be called 286 * as well. 287 */ 288 static void amdgpu_vm_update_shared(struct amdgpu_vm_bo_base *base) 289 { 290 struct amdgpu_vm *vm = base->vm; 291 struct amdgpu_bo *bo = base->bo; 292 uint64_t size = amdgpu_bo_size(bo); 293 uint32_t bo_memtype = amdgpu_bo_mem_stats_placement(bo); 294 bool shared; 295 296 dma_resv_assert_held(bo->tbo.base.resv); 297 spin_lock(&vm->stats_lock); 298 shared = drm_gem_object_is_shared_for_memory_stats(&bo->tbo.base); 299 if (base->shared != shared) { 300 base->shared = shared; 301 if (shared) { 302 vm->stats[bo_memtype].drm.shared += size; 303 vm->stats[bo_memtype].drm.private -= size; 304 } else { 305 vm->stats[bo_memtype].drm.shared -= size; 306 vm->stats[bo_memtype].drm.private += size; 307 } 308 } 309 spin_unlock(&vm->stats_lock); 310 } 311 312 /** 313 * amdgpu_vm_bo_update_shared - callback when bo gets shared/unshared 314 * @bo: amdgpu buffer object 315 * 316 * Update the per VM stats for all the vm if needed from private to shared or 317 * vice versa. 318 */ 319 void amdgpu_vm_bo_update_shared(struct amdgpu_bo *bo) 320 { 321 struct amdgpu_vm_bo_base *base; 322 323 for (base = bo->vm_bo; base; base = base->next) 324 amdgpu_vm_update_shared(base); 325 } 326 327 /** 328 * amdgpu_vm_update_stats_locked - helper to update normal memory stat 329 * @base: base structure for tracking BO usage in a VM 330 * @res: the ttm_resource to use for the purpose of accounting, may or may not 331 * be bo->tbo.resource 332 * @sign: if we should add (+1) or subtract (-1) from the stat 333 * 334 * Caller need to have the vm stats_lock held. Useful for when multiple update 335 * need to happen at the same time. 336 */ 337 static void amdgpu_vm_update_stats_locked(struct amdgpu_vm_bo_base *base, 338 struct ttm_resource *res, int sign) 339 { 340 struct amdgpu_vm *vm = base->vm; 341 struct amdgpu_bo *bo = base->bo; 342 int64_t size = sign * amdgpu_bo_size(bo); 343 uint32_t bo_memtype = amdgpu_bo_mem_stats_placement(bo); 344 345 /* For drm-total- and drm-shared-, BO are accounted by their preferred 346 * placement, see also amdgpu_bo_mem_stats_placement. 347 */ 348 if (base->shared) 349 vm->stats[bo_memtype].drm.shared += size; 350 else 351 vm->stats[bo_memtype].drm.private += size; 352 353 if (res && res->mem_type < __AMDGPU_PL_NUM) { 354 uint32_t res_memtype = res->mem_type; 355 356 vm->stats[res_memtype].drm.resident += size; 357 /* BO only count as purgeable if it is resident, 358 * since otherwise there's nothing to purge. 359 */ 360 if (bo->flags & AMDGPU_GEM_CREATE_DISCARDABLE) 361 vm->stats[res_memtype].drm.purgeable += size; 362 if (!(bo->preferred_domains & 363 amdgpu_mem_type_to_domain(res_memtype))) 364 vm->stats[bo_memtype].evicted += size; 365 } 366 } 367 368 /** 369 * amdgpu_vm_update_stats - helper to update normal memory stat 370 * @base: base structure for tracking BO usage in a VM 371 * @res: the ttm_resource to use for the purpose of accounting, may or may not 372 * be bo->tbo.resource 373 * @sign: if we should add (+1) or subtract (-1) from the stat 374 * 375 * Updates the basic memory stat when bo is added/deleted/moved. 376 */ 377 void amdgpu_vm_update_stats(struct amdgpu_vm_bo_base *base, 378 struct ttm_resource *res, int sign) 379 { 380 struct amdgpu_vm *vm = base->vm; 381 382 spin_lock(&vm->stats_lock); 383 amdgpu_vm_update_stats_locked(base, res, sign); 384 spin_unlock(&vm->stats_lock); 385 } 386 387 /** 388 * amdgpu_vm_bo_base_init - Adds bo to the list of bos associated with the vm 389 * 390 * @base: base structure for tracking BO usage in a VM 391 * @vm: vm to which bo is to be added 392 * @bo: amdgpu buffer object 393 * 394 * Initialize a bo_va_base structure and add it to the appropriate lists 395 * 396 */ 397 void amdgpu_vm_bo_base_init(struct amdgpu_vm_bo_base *base, 398 struct amdgpu_vm *vm, struct amdgpu_bo *bo) 399 { 400 base->vm = vm; 401 base->bo = bo; 402 base->next = NULL; 403 INIT_LIST_HEAD(&base->vm_status); 404 405 if (!bo) 406 return; 407 base->next = bo->vm_bo; 408 bo->vm_bo = base; 409 410 spin_lock(&vm->stats_lock); 411 base->shared = drm_gem_object_is_shared_for_memory_stats(&bo->tbo.base); 412 amdgpu_vm_update_stats_locked(base, bo->tbo.resource, +1); 413 spin_unlock(&vm->stats_lock); 414 415 if (!amdgpu_vm_is_bo_always_valid(vm, bo)) 416 return; 417 418 dma_resv_assert_held(vm->root.bo->tbo.base.resv); 419 420 ttm_bo_set_bulk_move(&bo->tbo, &vm->lru_bulk_move); 421 if (bo->tbo.type == ttm_bo_type_kernel && bo->parent) 422 amdgpu_vm_bo_relocated(base); 423 else 424 amdgpu_vm_bo_idle(base); 425 426 if (bo->preferred_domains & 427 amdgpu_mem_type_to_domain(bo->tbo.resource->mem_type)) 428 return; 429 430 /* 431 * we checked all the prerequisites, but it looks like this per vm bo 432 * is currently evicted. add the bo to the evicted list to make sure it 433 * is validated on next vm use to avoid fault. 434 * */ 435 amdgpu_vm_bo_evicted(base); 436 } 437 438 /** 439 * amdgpu_vm_lock_pd - lock PD in drm_exec 440 * 441 * @vm: vm providing the BOs 442 * @exec: drm execution context 443 * @num_fences: number of extra fences to reserve 444 * 445 * Lock the VM root PD in the DRM execution context. 446 */ 447 int amdgpu_vm_lock_pd(struct amdgpu_vm *vm, struct drm_exec *exec, 448 unsigned int num_fences) 449 { 450 /* We need at least two fences for the VM PD/PT updates */ 451 return drm_exec_prepare_obj(exec, &vm->root.bo->tbo.base, 452 2 + num_fences); 453 } 454 455 /** 456 * amdgpu_vm_lock_done_list - lock all BOs on the done list 457 * @vm: vm providing the BOs 458 * @exec: drm execution context 459 * @num_fences: number of extra fences to reserve 460 * 461 * Lock the BOs on the done list in the DRM execution context. 462 */ 463 int amdgpu_vm_lock_done_list(struct amdgpu_vm *vm, struct drm_exec *exec, 464 unsigned int num_fences) 465 { 466 struct list_head *prev = &vm->done; 467 struct amdgpu_bo_va *bo_va; 468 struct amdgpu_bo *bo; 469 int ret; 470 471 /* We can only trust prev->next while holding the lock */ 472 spin_lock(&vm->invalidated_lock); 473 while (!list_is_head(prev->next, &vm->done)) { 474 bo_va = list_entry(prev->next, typeof(*bo_va), base.vm_status); 475 spin_unlock(&vm->invalidated_lock); 476 477 bo = bo_va->base.bo; 478 if (bo) { 479 ret = drm_exec_prepare_obj(exec, &bo->tbo.base, 1); 480 if (unlikely(ret)) 481 return ret; 482 } 483 spin_lock(&vm->invalidated_lock); 484 prev = prev->next; 485 } 486 spin_unlock(&vm->invalidated_lock); 487 488 return 0; 489 } 490 491 /** 492 * amdgpu_vm_move_to_lru_tail - move all BOs to the end of LRU 493 * 494 * @adev: amdgpu device pointer 495 * @vm: vm providing the BOs 496 * 497 * Move all BOs to the end of LRU and remember their positions to put them 498 * together. 499 */ 500 void amdgpu_vm_move_to_lru_tail(struct amdgpu_device *adev, 501 struct amdgpu_vm *vm) 502 { 503 spin_lock(&adev->mman.bdev.lru_lock); 504 ttm_lru_bulk_move_tail(&vm->lru_bulk_move); 505 spin_unlock(&adev->mman.bdev.lru_lock); 506 } 507 508 /* Create scheduler entities for page table updates */ 509 static int amdgpu_vm_init_entities(struct amdgpu_device *adev, 510 struct amdgpu_vm *vm) 511 { 512 int r; 513 514 r = drm_sched_entity_init(&vm->immediate, DRM_SCHED_PRIORITY_NORMAL, 515 adev->vm_manager.vm_pte_scheds, 516 adev->vm_manager.vm_pte_num_scheds, NULL); 517 if (r) 518 goto error; 519 520 return drm_sched_entity_init(&vm->delayed, DRM_SCHED_PRIORITY_NORMAL, 521 adev->vm_manager.vm_pte_scheds, 522 adev->vm_manager.vm_pte_num_scheds, NULL); 523 524 error: 525 drm_sched_entity_destroy(&vm->immediate); 526 return r; 527 } 528 529 /* Destroy the entities for page table updates again */ 530 static void amdgpu_vm_fini_entities(struct amdgpu_vm *vm) 531 { 532 drm_sched_entity_destroy(&vm->immediate); 533 drm_sched_entity_destroy(&vm->delayed); 534 } 535 536 /** 537 * amdgpu_vm_generation - return the page table re-generation counter 538 * @adev: the amdgpu_device 539 * @vm: optional VM to check, might be NULL 540 * 541 * Returns a page table re-generation token to allow checking if submissions 542 * are still valid to use this VM. The VM parameter might be NULL in which case 543 * just the VRAM lost counter will be used. 544 */ 545 uint64_t amdgpu_vm_generation(struct amdgpu_device *adev, struct amdgpu_vm *vm) 546 { 547 uint64_t result = (u64)atomic_read(&adev->vram_lost_counter) << 32; 548 549 if (!vm) 550 return result; 551 552 result += lower_32_bits(vm->generation); 553 /* Add one if the page tables will be re-generated on next CS */ 554 if (drm_sched_entity_error(&vm->delayed)) 555 ++result; 556 557 return result; 558 } 559 560 /** 561 * amdgpu_vm_validate - validate evicted BOs tracked in the VM 562 * 563 * @adev: amdgpu device pointer 564 * @vm: vm providing the BOs 565 * @ticket: optional reservation ticket used to reserve the VM 566 * @validate: callback to do the validation 567 * @param: parameter for the validation callback 568 * 569 * Validate the page table BOs and per-VM BOs on command submission if 570 * necessary. If a ticket is given, also try to validate evicted user queue 571 * BOs. They must already be reserved with the given ticket. 572 * 573 * Returns: 574 * Validation result. 575 */ 576 int amdgpu_vm_validate(struct amdgpu_device *adev, struct amdgpu_vm *vm, 577 struct ww_acquire_ctx *ticket, 578 int (*validate)(void *p, struct amdgpu_bo *bo), 579 void *param) 580 { 581 uint64_t new_vm_generation = amdgpu_vm_generation(adev, vm); 582 struct amdgpu_vm_bo_base *bo_base, *tmp; 583 struct amdgpu_bo *bo; 584 int r; 585 586 if (vm->generation != new_vm_generation) { 587 vm->generation = new_vm_generation; 588 amdgpu_vm_bo_reset_state_machine(vm); 589 amdgpu_vm_fini_entities(vm); 590 r = amdgpu_vm_init_entities(adev, vm); 591 if (r) 592 return r; 593 } 594 595 list_for_each_entry_safe(bo_base, tmp, &vm->evicted, vm_status) { 596 bo = bo_base->bo; 597 598 r = validate(param, bo); 599 if (r) 600 return r; 601 602 if (bo->tbo.type != ttm_bo_type_kernel) { 603 amdgpu_vm_bo_moved(bo_base); 604 } else { 605 vm->update_funcs->map_table(to_amdgpu_bo_vm(bo)); 606 amdgpu_vm_bo_relocated(bo_base); 607 } 608 } 609 610 if (ticket) { 611 list_for_each_entry_safe(bo_base, tmp, &vm->evicted_user, 612 vm_status) { 613 bo = bo_base->bo; 614 dma_resv_assert_held(bo->tbo.base.resv); 615 616 r = validate(param, bo); 617 if (r) 618 return r; 619 620 amdgpu_vm_bo_invalidated(bo_base); 621 } 622 } 623 624 amdgpu_vm_eviction_lock(vm); 625 vm->evicting = false; 626 amdgpu_vm_eviction_unlock(vm); 627 628 return 0; 629 } 630 631 /** 632 * amdgpu_vm_ready - check VM is ready for updates 633 * 634 * @vm: VM to check 635 * 636 * Check if all VM PDs/PTs are ready for updates 637 * 638 * Returns: 639 * True if VM is not evicting and all VM entities are not stopped 640 */ 641 bool amdgpu_vm_ready(struct amdgpu_vm *vm) 642 { 643 bool ret; 644 645 amdgpu_vm_assert_locked(vm); 646 647 amdgpu_vm_eviction_lock(vm); 648 ret = !vm->evicting; 649 amdgpu_vm_eviction_unlock(vm); 650 651 ret &= list_empty(&vm->evicted); 652 653 spin_lock(&vm->immediate.lock); 654 ret &= !vm->immediate.stopped; 655 spin_unlock(&vm->immediate.lock); 656 657 spin_lock(&vm->delayed.lock); 658 ret &= !vm->delayed.stopped; 659 spin_unlock(&vm->delayed.lock); 660 661 return ret; 662 } 663 664 /** 665 * amdgpu_vm_check_compute_bug - check whether asic has compute vm bug 666 * 667 * @adev: amdgpu_device pointer 668 */ 669 void amdgpu_vm_check_compute_bug(struct amdgpu_device *adev) 670 { 671 const struct amdgpu_ip_block *ip_block; 672 bool has_compute_vm_bug; 673 struct amdgpu_ring *ring; 674 int i; 675 676 has_compute_vm_bug = false; 677 678 ip_block = amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_GFX); 679 if (ip_block) { 680 /* Compute has a VM bug for GFX version < 7. 681 Compute has a VM bug for GFX 8 MEC firmware version < 673.*/ 682 if (ip_block->version->major <= 7) 683 has_compute_vm_bug = true; 684 else if (ip_block->version->major == 8) 685 if (adev->gfx.mec_fw_version < 673) 686 has_compute_vm_bug = true; 687 } 688 689 for (i = 0; i < adev->num_rings; i++) { 690 ring = adev->rings[i]; 691 if (ring->funcs->type == AMDGPU_RING_TYPE_COMPUTE) 692 /* only compute rings */ 693 ring->has_compute_vm_bug = has_compute_vm_bug; 694 else 695 ring->has_compute_vm_bug = false; 696 } 697 } 698 699 /** 700 * amdgpu_vm_need_pipeline_sync - Check if pipe sync is needed for job. 701 * 702 * @ring: ring on which the job will be submitted 703 * @job: job to submit 704 * 705 * Returns: 706 * True if sync is needed. 707 */ 708 bool amdgpu_vm_need_pipeline_sync(struct amdgpu_ring *ring, 709 struct amdgpu_job *job) 710 { 711 struct amdgpu_device *adev = ring->adev; 712 unsigned vmhub = ring->vm_hub; 713 struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub]; 714 715 if (job->vmid == 0) 716 return false; 717 718 if (job->vm_needs_flush || ring->has_compute_vm_bug) 719 return true; 720 721 if (ring->funcs->emit_gds_switch && job->gds_switch_needed) 722 return true; 723 724 if (amdgpu_vmid_had_gpu_reset(adev, &id_mgr->ids[job->vmid])) 725 return true; 726 727 return false; 728 } 729 730 /** 731 * amdgpu_vm_flush - hardware flush the vm 732 * 733 * @ring: ring to use for flush 734 * @job: related job 735 * @need_pipe_sync: is pipe sync needed 736 * 737 * Emit a VM flush when it is necessary. 738 * 739 * Returns: 740 * 0 on success, errno otherwise. 741 */ 742 int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job, 743 bool need_pipe_sync) 744 { 745 struct amdgpu_device *adev = ring->adev; 746 struct amdgpu_isolation *isolation = &adev->isolation[ring->xcp_id]; 747 unsigned vmhub = ring->vm_hub; 748 struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub]; 749 struct amdgpu_vmid *id = &id_mgr->ids[job->vmid]; 750 bool spm_update_needed = job->spm_update_needed; 751 bool gds_switch_needed = ring->funcs->emit_gds_switch && 752 job->gds_switch_needed; 753 bool vm_flush_needed = job->vm_needs_flush; 754 bool cleaner_shader_needed = false; 755 bool pasid_mapping_needed = false; 756 struct dma_fence *fence = NULL; 757 struct amdgpu_fence *af; 758 unsigned int patch; 759 int r; 760 761 if (amdgpu_vmid_had_gpu_reset(adev, id)) { 762 gds_switch_needed = true; 763 vm_flush_needed = true; 764 pasid_mapping_needed = true; 765 spm_update_needed = true; 766 } 767 768 mutex_lock(&id_mgr->lock); 769 if (id->pasid != job->pasid || !id->pasid_mapping || 770 !dma_fence_is_signaled(id->pasid_mapping)) 771 pasid_mapping_needed = true; 772 mutex_unlock(&id_mgr->lock); 773 774 gds_switch_needed &= !!ring->funcs->emit_gds_switch; 775 vm_flush_needed &= !!ring->funcs->emit_vm_flush && 776 job->vm_pd_addr != AMDGPU_BO_INVALID_OFFSET; 777 pasid_mapping_needed &= adev->gmc.gmc_funcs->emit_pasid_mapping && 778 ring->funcs->emit_wreg; 779 780 cleaner_shader_needed = job->run_cleaner_shader && 781 adev->gfx.enable_cleaner_shader && 782 ring->funcs->emit_cleaner_shader && job->base.s_fence && 783 &job->base.s_fence->scheduled == isolation->spearhead; 784 785 if (!vm_flush_needed && !gds_switch_needed && !need_pipe_sync && 786 !cleaner_shader_needed) 787 return 0; 788 789 amdgpu_ring_ib_begin(ring); 790 if (ring->funcs->init_cond_exec) 791 patch = amdgpu_ring_init_cond_exec(ring, 792 ring->cond_exe_gpu_addr); 793 794 if (need_pipe_sync) 795 amdgpu_ring_emit_pipeline_sync(ring); 796 797 if (cleaner_shader_needed) 798 ring->funcs->emit_cleaner_shader(ring); 799 800 if (vm_flush_needed) { 801 trace_amdgpu_vm_flush(ring, job->vmid, job->vm_pd_addr); 802 amdgpu_ring_emit_vm_flush(ring, job->vmid, job->vm_pd_addr); 803 } 804 805 if (pasid_mapping_needed) 806 amdgpu_gmc_emit_pasid_mapping(ring, job->vmid, job->pasid); 807 808 if (spm_update_needed && adev->gfx.rlc.funcs->update_spm_vmid) 809 adev->gfx.rlc.funcs->update_spm_vmid(adev, ring, job->vmid); 810 811 if (ring->funcs->emit_gds_switch && 812 gds_switch_needed) { 813 amdgpu_ring_emit_gds_switch(ring, job->vmid, job->gds_base, 814 job->gds_size, job->gws_base, 815 job->gws_size, job->oa_base, 816 job->oa_size); 817 } 818 819 if (vm_flush_needed || pasid_mapping_needed || cleaner_shader_needed) { 820 r = amdgpu_fence_emit(ring, &fence, NULL, 0); 821 if (r) 822 return r; 823 /* this is part of the job's context */ 824 af = container_of(fence, struct amdgpu_fence, base); 825 af->context = job->base.s_fence ? job->base.s_fence->finished.context : 0; 826 } 827 828 if (vm_flush_needed) { 829 mutex_lock(&id_mgr->lock); 830 dma_fence_put(id->last_flush); 831 id->last_flush = dma_fence_get(fence); 832 id->current_gpu_reset_count = 833 atomic_read(&adev->gpu_reset_counter); 834 mutex_unlock(&id_mgr->lock); 835 } 836 837 if (pasid_mapping_needed) { 838 mutex_lock(&id_mgr->lock); 839 id->pasid = job->pasid; 840 dma_fence_put(id->pasid_mapping); 841 id->pasid_mapping = dma_fence_get(fence); 842 mutex_unlock(&id_mgr->lock); 843 } 844 845 /* 846 * Make sure that all other submissions wait for the cleaner shader to 847 * finish before we push them to the HW. 848 */ 849 if (cleaner_shader_needed) { 850 trace_amdgpu_cleaner_shader(ring, fence); 851 mutex_lock(&adev->enforce_isolation_mutex); 852 dma_fence_put(isolation->spearhead); 853 isolation->spearhead = dma_fence_get(fence); 854 mutex_unlock(&adev->enforce_isolation_mutex); 855 } 856 dma_fence_put(fence); 857 858 amdgpu_ring_patch_cond_exec(ring, patch); 859 860 /* the double SWITCH_BUFFER here *cannot* be skipped by COND_EXEC */ 861 if (ring->funcs->emit_switch_buffer) { 862 amdgpu_ring_emit_switch_buffer(ring); 863 amdgpu_ring_emit_switch_buffer(ring); 864 } 865 866 amdgpu_ring_ib_end(ring); 867 return 0; 868 } 869 870 /** 871 * amdgpu_vm_bo_find - find the bo_va for a specific vm & bo 872 * 873 * @vm: requested vm 874 * @bo: requested buffer object 875 * 876 * Find @bo inside the requested vm. 877 * Search inside the @bos vm list for the requested vm 878 * Returns the found bo_va or NULL if none is found 879 * 880 * Object has to be reserved! 881 * 882 * Returns: 883 * Found bo_va or NULL. 884 */ 885 struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm, 886 struct amdgpu_bo *bo) 887 { 888 struct amdgpu_vm_bo_base *base; 889 890 for (base = bo->vm_bo; base; base = base->next) { 891 if (base->vm != vm) 892 continue; 893 894 return container_of(base, struct amdgpu_bo_va, base); 895 } 896 return NULL; 897 } 898 899 /** 900 * amdgpu_vm_map_gart - Resolve gart mapping of addr 901 * 902 * @pages_addr: optional DMA address to use for lookup 903 * @addr: the unmapped addr 904 * 905 * Look up the physical address of the page that the pte resolves 906 * to. 907 * 908 * Returns: 909 * The pointer for the page table entry. 910 */ 911 uint64_t amdgpu_vm_map_gart(const dma_addr_t *pages_addr, uint64_t addr) 912 { 913 uint64_t result; 914 915 /* page table offset */ 916 result = pages_addr[addr >> PAGE_SHIFT]; 917 918 /* in case cpu page size != gpu page size*/ 919 result |= addr & (~PAGE_MASK); 920 921 result &= 0xFFFFFFFFFFFFF000ULL; 922 923 return result; 924 } 925 926 /** 927 * amdgpu_vm_update_pdes - make sure that all directories are valid 928 * 929 * @adev: amdgpu_device pointer 930 * @vm: requested vm 931 * @immediate: submit immediately to the paging queue 932 * 933 * Makes sure all directories are up to date. 934 * 935 * Returns: 936 * 0 for success, error for failure. 937 */ 938 int amdgpu_vm_update_pdes(struct amdgpu_device *adev, 939 struct amdgpu_vm *vm, bool immediate) 940 { 941 struct amdgpu_vm_update_params params; 942 struct amdgpu_vm_bo_base *entry, *tmp; 943 bool flush_tlb_needed = false; 944 int r, idx; 945 946 amdgpu_vm_assert_locked(vm); 947 948 if (list_empty(&vm->relocated)) 949 return 0; 950 951 if (!drm_dev_enter(adev_to_drm(adev), &idx)) 952 return -ENODEV; 953 954 memset(¶ms, 0, sizeof(params)); 955 params.adev = adev; 956 params.vm = vm; 957 params.immediate = immediate; 958 959 r = vm->update_funcs->prepare(¶ms, NULL, 960 AMDGPU_KERNEL_JOB_ID_VM_UPDATE_PDES); 961 if (r) 962 goto error; 963 964 list_for_each_entry(entry, &vm->relocated, vm_status) { 965 /* vm_flush_needed after updating moved PDEs */ 966 flush_tlb_needed |= entry->moved; 967 968 r = amdgpu_vm_pde_update(¶ms, entry); 969 if (r) 970 goto error; 971 } 972 973 r = vm->update_funcs->commit(¶ms, &vm->last_update); 974 if (r) 975 goto error; 976 977 if (flush_tlb_needed) 978 atomic64_inc(&vm->tlb_seq); 979 980 list_for_each_entry_safe(entry, tmp, &vm->relocated, vm_status) { 981 amdgpu_vm_bo_idle(entry); 982 } 983 984 error: 985 drm_dev_exit(idx); 986 return r; 987 } 988 989 /** 990 * amdgpu_vm_tlb_seq_cb - make sure to increment tlb sequence 991 * @fence: unused 992 * @cb: the callback structure 993 * 994 * Increments the tlb sequence to make sure that future CS execute a VM flush. 995 */ 996 static void amdgpu_vm_tlb_seq_cb(struct dma_fence *fence, 997 struct dma_fence_cb *cb) 998 { 999 struct amdgpu_vm_tlb_seq_struct *tlb_cb; 1000 1001 tlb_cb = container_of(cb, typeof(*tlb_cb), cb); 1002 atomic64_inc(&tlb_cb->vm->tlb_seq); 1003 kfree(tlb_cb); 1004 } 1005 1006 /** 1007 * amdgpu_vm_tlb_flush - prepare TLB flush 1008 * 1009 * @params: parameters for update 1010 * @fence: input fence to sync TLB flush with 1011 * @tlb_cb: the callback structure 1012 * 1013 * Increments the tlb sequence to make sure that future CS execute a VM flush. 1014 */ 1015 static void 1016 amdgpu_vm_tlb_flush(struct amdgpu_vm_update_params *params, 1017 struct dma_fence **fence, 1018 struct amdgpu_vm_tlb_seq_struct *tlb_cb) 1019 { 1020 struct amdgpu_vm *vm = params->vm; 1021 1022 tlb_cb->vm = vm; 1023 if (!fence || !*fence) { 1024 amdgpu_vm_tlb_seq_cb(NULL, &tlb_cb->cb); 1025 return; 1026 } 1027 1028 if (!dma_fence_add_callback(*fence, &tlb_cb->cb, 1029 amdgpu_vm_tlb_seq_cb)) { 1030 dma_fence_put(vm->last_tlb_flush); 1031 vm->last_tlb_flush = dma_fence_get(*fence); 1032 } else { 1033 amdgpu_vm_tlb_seq_cb(NULL, &tlb_cb->cb); 1034 } 1035 1036 /* Prepare a TLB flush fence to be attached to PTs */ 1037 if (!params->unlocked && vm->is_compute_context) { 1038 amdgpu_vm_tlb_fence_create(params->adev, vm, fence); 1039 1040 /* Makes sure no PD/PT is freed before the flush */ 1041 dma_resv_add_fence(vm->root.bo->tbo.base.resv, *fence, 1042 DMA_RESV_USAGE_BOOKKEEP); 1043 } 1044 } 1045 1046 /** 1047 * amdgpu_vm_update_range - update a range in the vm page table 1048 * 1049 * @adev: amdgpu_device pointer to use for commands 1050 * @vm: the VM to update the range 1051 * @immediate: immediate submission in a page fault 1052 * @unlocked: unlocked invalidation during MM callback 1053 * @flush_tlb: trigger tlb invalidation after update completed 1054 * @allow_override: change MTYPE for local NUMA nodes 1055 * @sync: fences we need to sync to 1056 * @start: start of mapped range 1057 * @last: last mapped entry 1058 * @flags: flags for the entries 1059 * @offset: offset into nodes and pages_addr 1060 * @vram_base: base for vram mappings 1061 * @res: ttm_resource to map 1062 * @pages_addr: DMA addresses to use for mapping 1063 * @fence: optional resulting fence 1064 * 1065 * Fill in the page table entries between @start and @last. 1066 * 1067 * Returns: 1068 * 0 for success, negative erro code for failure. 1069 */ 1070 int amdgpu_vm_update_range(struct amdgpu_device *adev, struct amdgpu_vm *vm, 1071 bool immediate, bool unlocked, bool flush_tlb, 1072 bool allow_override, struct amdgpu_sync *sync, 1073 uint64_t start, uint64_t last, uint64_t flags, 1074 uint64_t offset, uint64_t vram_base, 1075 struct ttm_resource *res, dma_addr_t *pages_addr, 1076 struct dma_fence **fence) 1077 { 1078 struct amdgpu_vm_tlb_seq_struct *tlb_cb; 1079 struct amdgpu_vm_update_params params; 1080 struct amdgpu_res_cursor cursor; 1081 int r, idx; 1082 1083 if (!drm_dev_enter(adev_to_drm(adev), &idx)) 1084 return -ENODEV; 1085 1086 tlb_cb = kmalloc(sizeof(*tlb_cb), GFP_KERNEL); 1087 if (!tlb_cb) { 1088 drm_dev_exit(idx); 1089 return -ENOMEM; 1090 } 1091 1092 /* Vega20+XGMI where PTEs get inadvertently cached in L2 texture cache, 1093 * heavy-weight flush TLB unconditionally. 1094 */ 1095 flush_tlb |= adev->gmc.xgmi.num_physical_nodes && 1096 amdgpu_ip_version(adev, GC_HWIP, 0) == IP_VERSION(9, 4, 0); 1097 1098 /* 1099 * On GFX8 and older any 8 PTE block with a valid bit set enters the TLB 1100 */ 1101 flush_tlb |= amdgpu_ip_version(adev, GC_HWIP, 0) < IP_VERSION(9, 0, 0); 1102 1103 memset(¶ms, 0, sizeof(params)); 1104 params.adev = adev; 1105 params.vm = vm; 1106 params.immediate = immediate; 1107 params.pages_addr = pages_addr; 1108 params.unlocked = unlocked; 1109 params.needs_flush = flush_tlb; 1110 params.allow_override = allow_override; 1111 INIT_LIST_HEAD(¶ms.tlb_flush_waitlist); 1112 1113 amdgpu_vm_eviction_lock(vm); 1114 if (vm->evicting) { 1115 r = -EBUSY; 1116 goto error_free; 1117 } 1118 1119 if (!unlocked && !dma_fence_is_signaled(vm->last_unlocked)) { 1120 struct dma_fence *tmp = dma_fence_get_stub(); 1121 1122 amdgpu_bo_fence(vm->root.bo, vm->last_unlocked, true); 1123 swap(vm->last_unlocked, tmp); 1124 dma_fence_put(tmp); 1125 } 1126 1127 r = vm->update_funcs->prepare(¶ms, sync, 1128 AMDGPU_KERNEL_JOB_ID_VM_UPDATE_RANGE); 1129 if (r) 1130 goto error_free; 1131 1132 amdgpu_res_first(pages_addr ? NULL : res, offset, 1133 (last - start + 1) * AMDGPU_GPU_PAGE_SIZE, &cursor); 1134 while (cursor.remaining) { 1135 uint64_t tmp, num_entries, addr; 1136 1137 num_entries = cursor.size >> AMDGPU_GPU_PAGE_SHIFT; 1138 if (pages_addr) { 1139 bool contiguous = true; 1140 1141 if (num_entries > AMDGPU_GPU_PAGES_IN_CPU_PAGE) { 1142 uint64_t pfn = cursor.start >> PAGE_SHIFT; 1143 uint64_t count; 1144 1145 contiguous = pages_addr[pfn + 1] == 1146 pages_addr[pfn] + PAGE_SIZE; 1147 1148 tmp = num_entries / 1149 AMDGPU_GPU_PAGES_IN_CPU_PAGE; 1150 for (count = 2; count < tmp; ++count) { 1151 uint64_t idx = pfn + count; 1152 1153 if (contiguous != (pages_addr[idx] == 1154 pages_addr[idx - 1] + PAGE_SIZE)) 1155 break; 1156 } 1157 if (!contiguous) 1158 count--; 1159 num_entries = count * 1160 AMDGPU_GPU_PAGES_IN_CPU_PAGE; 1161 } 1162 1163 if (!contiguous) { 1164 addr = cursor.start; 1165 params.pages_addr = pages_addr; 1166 } else { 1167 addr = pages_addr[cursor.start >> PAGE_SHIFT]; 1168 params.pages_addr = NULL; 1169 } 1170 1171 } else if (flags & (AMDGPU_PTE_VALID | AMDGPU_PTE_PRT_FLAG(adev))) { 1172 addr = vram_base + cursor.start; 1173 } else { 1174 addr = 0; 1175 } 1176 1177 tmp = start + num_entries; 1178 r = amdgpu_vm_ptes_update(¶ms, start, tmp, addr, flags); 1179 if (r) 1180 goto error_free; 1181 1182 amdgpu_res_next(&cursor, num_entries * AMDGPU_GPU_PAGE_SIZE); 1183 start = tmp; 1184 } 1185 1186 r = vm->update_funcs->commit(¶ms, fence); 1187 if (r) 1188 goto error_free; 1189 1190 if (params.needs_flush) { 1191 amdgpu_vm_tlb_flush(¶ms, fence, tlb_cb); 1192 tlb_cb = NULL; 1193 } 1194 1195 amdgpu_vm_pt_free_list(adev, ¶ms); 1196 1197 error_free: 1198 kfree(tlb_cb); 1199 amdgpu_vm_eviction_unlock(vm); 1200 drm_dev_exit(idx); 1201 return r; 1202 } 1203 1204 void amdgpu_vm_get_memory(struct amdgpu_vm *vm, 1205 struct amdgpu_mem_stats stats[__AMDGPU_PL_NUM]) 1206 { 1207 spin_lock(&vm->stats_lock); 1208 memcpy(stats, vm->stats, sizeof(*stats) * __AMDGPU_PL_NUM); 1209 spin_unlock(&vm->stats_lock); 1210 } 1211 1212 /** 1213 * amdgpu_vm_bo_update - update all BO mappings in the vm page table 1214 * 1215 * @adev: amdgpu_device pointer 1216 * @bo_va: requested BO and VM object 1217 * @clear: if true clear the entries 1218 * 1219 * Fill in the page table entries for @bo_va. 1220 * 1221 * Returns: 1222 * 0 for success, -EINVAL for failure. 1223 */ 1224 int amdgpu_vm_bo_update(struct amdgpu_device *adev, struct amdgpu_bo_va *bo_va, 1225 bool clear) 1226 { 1227 struct amdgpu_bo *bo = bo_va->base.bo; 1228 struct amdgpu_vm *vm = bo_va->base.vm; 1229 struct amdgpu_bo_va_mapping *mapping; 1230 struct dma_fence **last_update; 1231 dma_addr_t *pages_addr = NULL; 1232 struct ttm_resource *mem; 1233 struct amdgpu_sync sync; 1234 bool flush_tlb = clear; 1235 uint64_t vram_base; 1236 uint64_t flags; 1237 bool uncached; 1238 int r; 1239 1240 amdgpu_sync_create(&sync); 1241 if (clear) { 1242 mem = NULL; 1243 1244 /* Implicitly sync to command submissions in the same VM before 1245 * unmapping. 1246 */ 1247 r = amdgpu_sync_resv(adev, &sync, vm->root.bo->tbo.base.resv, 1248 AMDGPU_SYNC_EQ_OWNER, vm); 1249 if (r) 1250 goto error_free; 1251 if (bo) { 1252 r = amdgpu_sync_kfd(&sync, bo->tbo.base.resv); 1253 if (r) 1254 goto error_free; 1255 } 1256 } else if (!bo) { 1257 mem = NULL; 1258 1259 /* PRT map operations don't need to sync to anything. */ 1260 1261 } else { 1262 struct drm_gem_object *obj = &bo->tbo.base; 1263 1264 if (drm_gem_is_imported(obj) && bo_va->is_xgmi) { 1265 struct dma_buf *dma_buf = obj->import_attach->dmabuf; 1266 struct drm_gem_object *gobj = dma_buf->priv; 1267 struct amdgpu_bo *abo = gem_to_amdgpu_bo(gobj); 1268 1269 if (abo->tbo.resource && 1270 abo->tbo.resource->mem_type == TTM_PL_VRAM) 1271 bo = gem_to_amdgpu_bo(gobj); 1272 } 1273 mem = bo->tbo.resource; 1274 if (mem && (mem->mem_type == TTM_PL_TT || 1275 mem->mem_type == AMDGPU_PL_PREEMPT)) 1276 pages_addr = bo->tbo.ttm->dma_address; 1277 1278 /* Implicitly sync to moving fences before mapping anything */ 1279 r = amdgpu_sync_resv(adev, &sync, bo->tbo.base.resv, 1280 AMDGPU_SYNC_EXPLICIT, vm); 1281 if (r) 1282 goto error_free; 1283 } 1284 1285 if (bo) { 1286 struct amdgpu_device *bo_adev; 1287 1288 flags = amdgpu_ttm_tt_pte_flags(adev, bo->tbo.ttm, mem); 1289 1290 if (amdgpu_bo_encrypted(bo)) 1291 flags |= AMDGPU_PTE_TMZ; 1292 1293 bo_adev = amdgpu_ttm_adev(bo->tbo.bdev); 1294 vram_base = bo_adev->vm_manager.vram_base_offset; 1295 uncached = (bo->flags & AMDGPU_GEM_CREATE_UNCACHED) != 0; 1296 } else { 1297 flags = 0x0; 1298 vram_base = 0; 1299 uncached = false; 1300 } 1301 1302 if (clear || amdgpu_vm_is_bo_always_valid(vm, bo)) 1303 last_update = &vm->last_update; 1304 else 1305 last_update = &bo_va->last_pt_update; 1306 1307 if (!clear && bo_va->base.moved) { 1308 flush_tlb = true; 1309 list_splice_init(&bo_va->valids, &bo_va->invalids); 1310 1311 } else if (bo_va->cleared != clear) { 1312 list_splice_init(&bo_va->valids, &bo_va->invalids); 1313 } 1314 1315 list_for_each_entry(mapping, &bo_va->invalids, list) { 1316 uint64_t update_flags = flags; 1317 1318 /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here 1319 * but in case of something, we filter the flags in first place 1320 */ 1321 if (!(mapping->flags & AMDGPU_VM_PAGE_READABLE)) 1322 update_flags &= ~AMDGPU_PTE_READABLE; 1323 if (!(mapping->flags & AMDGPU_VM_PAGE_WRITEABLE)) 1324 update_flags &= ~AMDGPU_PTE_WRITEABLE; 1325 1326 /* Apply ASIC specific mapping flags */ 1327 amdgpu_gmc_get_vm_pte(adev, vm, bo, mapping->flags, 1328 &update_flags); 1329 1330 trace_amdgpu_vm_bo_update(mapping); 1331 1332 r = amdgpu_vm_update_range(adev, vm, false, false, flush_tlb, 1333 !uncached, &sync, mapping->start, 1334 mapping->last, update_flags, 1335 mapping->offset, vram_base, mem, 1336 pages_addr, last_update); 1337 if (r) 1338 goto error_free; 1339 } 1340 1341 /* If the BO is not in its preferred location add it back to 1342 * the evicted list so that it gets validated again on the 1343 * next command submission. 1344 */ 1345 if (amdgpu_vm_is_bo_always_valid(vm, bo)) { 1346 if (bo->tbo.resource && 1347 !(bo->preferred_domains & 1348 amdgpu_mem_type_to_domain(bo->tbo.resource->mem_type))) 1349 amdgpu_vm_bo_evicted(&bo_va->base); 1350 else 1351 amdgpu_vm_bo_idle(&bo_va->base); 1352 } else { 1353 amdgpu_vm_bo_done(&bo_va->base); 1354 } 1355 1356 list_splice_init(&bo_va->invalids, &bo_va->valids); 1357 bo_va->cleared = clear; 1358 bo_va->base.moved = false; 1359 1360 if (trace_amdgpu_vm_bo_mapping_enabled()) { 1361 list_for_each_entry(mapping, &bo_va->valids, list) 1362 trace_amdgpu_vm_bo_mapping(mapping); 1363 } 1364 1365 error_free: 1366 amdgpu_sync_free(&sync); 1367 return r; 1368 } 1369 1370 /** 1371 * amdgpu_vm_update_prt_state - update the global PRT state 1372 * 1373 * @adev: amdgpu_device pointer 1374 */ 1375 static void amdgpu_vm_update_prt_state(struct amdgpu_device *adev) 1376 { 1377 unsigned long flags; 1378 bool enable; 1379 1380 spin_lock_irqsave(&adev->vm_manager.prt_lock, flags); 1381 enable = !!atomic_read(&adev->vm_manager.num_prt_users); 1382 adev->gmc.gmc_funcs->set_prt(adev, enable); 1383 spin_unlock_irqrestore(&adev->vm_manager.prt_lock, flags); 1384 } 1385 1386 /** 1387 * amdgpu_vm_prt_get - add a PRT user 1388 * 1389 * @adev: amdgpu_device pointer 1390 */ 1391 static void amdgpu_vm_prt_get(struct amdgpu_device *adev) 1392 { 1393 if (!adev->gmc.gmc_funcs->set_prt) 1394 return; 1395 1396 if (atomic_inc_return(&adev->vm_manager.num_prt_users) == 1) 1397 amdgpu_vm_update_prt_state(adev); 1398 } 1399 1400 /** 1401 * amdgpu_vm_prt_put - drop a PRT user 1402 * 1403 * @adev: amdgpu_device pointer 1404 */ 1405 static void amdgpu_vm_prt_put(struct amdgpu_device *adev) 1406 { 1407 if (atomic_dec_return(&adev->vm_manager.num_prt_users) == 0) 1408 amdgpu_vm_update_prt_state(adev); 1409 } 1410 1411 /** 1412 * amdgpu_vm_prt_cb - callback for updating the PRT status 1413 * 1414 * @fence: fence for the callback 1415 * @_cb: the callback function 1416 */ 1417 static void amdgpu_vm_prt_cb(struct dma_fence *fence, struct dma_fence_cb *_cb) 1418 { 1419 struct amdgpu_prt_cb *cb = container_of(_cb, struct amdgpu_prt_cb, cb); 1420 1421 amdgpu_vm_prt_put(cb->adev); 1422 kfree(cb); 1423 } 1424 1425 /** 1426 * amdgpu_vm_add_prt_cb - add callback for updating the PRT status 1427 * 1428 * @adev: amdgpu_device pointer 1429 * @fence: fence for the callback 1430 */ 1431 static void amdgpu_vm_add_prt_cb(struct amdgpu_device *adev, 1432 struct dma_fence *fence) 1433 { 1434 struct amdgpu_prt_cb *cb; 1435 1436 if (!adev->gmc.gmc_funcs->set_prt) 1437 return; 1438 1439 cb = kmalloc(sizeof(struct amdgpu_prt_cb), GFP_KERNEL); 1440 if (!cb) { 1441 /* Last resort when we are OOM */ 1442 if (fence) 1443 dma_fence_wait(fence, false); 1444 1445 amdgpu_vm_prt_put(adev); 1446 } else { 1447 cb->adev = adev; 1448 if (!fence || dma_fence_add_callback(fence, &cb->cb, 1449 amdgpu_vm_prt_cb)) 1450 amdgpu_vm_prt_cb(fence, &cb->cb); 1451 } 1452 } 1453 1454 /** 1455 * amdgpu_vm_free_mapping - free a mapping 1456 * 1457 * @adev: amdgpu_device pointer 1458 * @vm: requested vm 1459 * @mapping: mapping to be freed 1460 * @fence: fence of the unmap operation 1461 * 1462 * Free a mapping and make sure we decrease the PRT usage count if applicable. 1463 */ 1464 static void amdgpu_vm_free_mapping(struct amdgpu_device *adev, 1465 struct amdgpu_vm *vm, 1466 struct amdgpu_bo_va_mapping *mapping, 1467 struct dma_fence *fence) 1468 { 1469 if (mapping->flags & AMDGPU_VM_PAGE_PRT) 1470 amdgpu_vm_add_prt_cb(adev, fence); 1471 kfree(mapping); 1472 } 1473 1474 /** 1475 * amdgpu_vm_prt_fini - finish all prt mappings 1476 * 1477 * @adev: amdgpu_device pointer 1478 * @vm: requested vm 1479 * 1480 * Register a cleanup callback to disable PRT support after VM dies. 1481 */ 1482 static void amdgpu_vm_prt_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm) 1483 { 1484 struct dma_resv *resv = vm->root.bo->tbo.base.resv; 1485 struct dma_resv_iter cursor; 1486 struct dma_fence *fence; 1487 1488 dma_resv_for_each_fence(&cursor, resv, DMA_RESV_USAGE_BOOKKEEP, fence) { 1489 /* Add a callback for each fence in the reservation object */ 1490 amdgpu_vm_prt_get(adev); 1491 amdgpu_vm_add_prt_cb(adev, fence); 1492 } 1493 } 1494 1495 /** 1496 * amdgpu_vm_clear_freed - clear freed BOs in the PT 1497 * 1498 * @adev: amdgpu_device pointer 1499 * @vm: requested vm 1500 * @fence: optional resulting fence (unchanged if no work needed to be done 1501 * or if an error occurred) 1502 * 1503 * Make sure all freed BOs are cleared in the PT. 1504 * PTs have to be reserved and mutex must be locked! 1505 * 1506 * Returns: 1507 * 0 for success. 1508 * 1509 */ 1510 int amdgpu_vm_clear_freed(struct amdgpu_device *adev, 1511 struct amdgpu_vm *vm, 1512 struct dma_fence **fence) 1513 { 1514 struct amdgpu_bo_va_mapping *mapping; 1515 struct dma_fence *f = NULL; 1516 struct amdgpu_sync sync; 1517 int r; 1518 1519 1520 /* 1521 * Implicitly sync to command submissions in the same VM before 1522 * unmapping. 1523 */ 1524 amdgpu_sync_create(&sync); 1525 r = amdgpu_sync_resv(adev, &sync, vm->root.bo->tbo.base.resv, 1526 AMDGPU_SYNC_EQ_OWNER, vm); 1527 if (r) 1528 goto error_free; 1529 1530 while (!list_empty(&vm->freed)) { 1531 mapping = list_first_entry(&vm->freed, 1532 struct amdgpu_bo_va_mapping, list); 1533 list_del(&mapping->list); 1534 1535 r = amdgpu_vm_update_range(adev, vm, false, false, true, false, 1536 &sync, mapping->start, mapping->last, 1537 0, 0, 0, NULL, NULL, &f); 1538 amdgpu_vm_free_mapping(adev, vm, mapping, f); 1539 if (r) { 1540 dma_fence_put(f); 1541 goto error_free; 1542 } 1543 } 1544 1545 if (fence && f) { 1546 dma_fence_put(*fence); 1547 *fence = f; 1548 } else { 1549 dma_fence_put(f); 1550 } 1551 1552 error_free: 1553 amdgpu_sync_free(&sync); 1554 return r; 1555 1556 } 1557 1558 /** 1559 * amdgpu_vm_handle_moved - handle moved BOs in the PT 1560 * 1561 * @adev: amdgpu_device pointer 1562 * @vm: requested vm 1563 * @ticket: optional reservation ticket used to reserve the VM 1564 * 1565 * Make sure all BOs which are moved are updated in the PTs. 1566 * 1567 * Returns: 1568 * 0 for success. 1569 * 1570 * PTs have to be reserved! 1571 */ 1572 int amdgpu_vm_handle_moved(struct amdgpu_device *adev, 1573 struct amdgpu_vm *vm, 1574 struct ww_acquire_ctx *ticket) 1575 { 1576 struct amdgpu_bo_va *bo_va, *tmp; 1577 struct dma_resv *resv; 1578 bool clear, unlock; 1579 int r; 1580 1581 list_for_each_entry_safe(bo_va, tmp, &vm->moved, base.vm_status) { 1582 /* Per VM BOs never need to bo cleared in the page tables */ 1583 r = amdgpu_vm_bo_update(adev, bo_va, false); 1584 if (r) 1585 return r; 1586 } 1587 1588 spin_lock(&vm->invalidated_lock); 1589 while (!list_empty(&vm->invalidated)) { 1590 bo_va = list_first_entry(&vm->invalidated, struct amdgpu_bo_va, 1591 base.vm_status); 1592 resv = bo_va->base.bo->tbo.base.resv; 1593 spin_unlock(&vm->invalidated_lock); 1594 1595 /* Try to reserve the BO to avoid clearing its ptes */ 1596 if (!adev->debug_vm && dma_resv_trylock(resv)) { 1597 clear = false; 1598 unlock = true; 1599 /* The caller is already holding the reservation lock */ 1600 } else if (ticket && dma_resv_locking_ctx(resv) == ticket) { 1601 clear = false; 1602 unlock = false; 1603 /* Somebody else is using the BO right now */ 1604 } else { 1605 clear = true; 1606 unlock = false; 1607 } 1608 1609 r = amdgpu_vm_bo_update(adev, bo_va, clear); 1610 1611 if (unlock) 1612 dma_resv_unlock(resv); 1613 if (r) 1614 return r; 1615 1616 /* Remember evicted DMABuf imports in compute VMs for later 1617 * validation 1618 */ 1619 if (vm->is_compute_context && 1620 drm_gem_is_imported(&bo_va->base.bo->tbo.base) && 1621 (!bo_va->base.bo->tbo.resource || 1622 bo_va->base.bo->tbo.resource->mem_type == TTM_PL_SYSTEM)) 1623 amdgpu_vm_bo_evicted_user(&bo_va->base); 1624 1625 spin_lock(&vm->invalidated_lock); 1626 } 1627 spin_unlock(&vm->invalidated_lock); 1628 1629 return 0; 1630 } 1631 1632 /** 1633 * amdgpu_vm_flush_compute_tlb - Flush TLB on compute VM 1634 * 1635 * @adev: amdgpu_device pointer 1636 * @vm: requested vm 1637 * @flush_type: flush type 1638 * @xcc_mask: mask of XCCs that belong to the compute partition in need of a TLB flush. 1639 * 1640 * Flush TLB if needed for a compute VM. 1641 * 1642 * Returns: 1643 * 0 for success. 1644 */ 1645 int amdgpu_vm_flush_compute_tlb(struct amdgpu_device *adev, 1646 struct amdgpu_vm *vm, 1647 uint32_t flush_type, 1648 uint32_t xcc_mask) 1649 { 1650 uint64_t tlb_seq = amdgpu_vm_tlb_seq(vm); 1651 bool all_hub = false; 1652 int xcc = 0, r = 0; 1653 1654 WARN_ON_ONCE(!vm->is_compute_context); 1655 1656 /* 1657 * It can be that we race and lose here, but that is extremely unlikely 1658 * and the worst thing which could happen is that we flush the changes 1659 * into the TLB once more which is harmless. 1660 */ 1661 if (atomic64_xchg(&vm->kfd_last_flushed_seq, tlb_seq) == tlb_seq) 1662 return 0; 1663 1664 if (adev->family == AMDGPU_FAMILY_AI || 1665 adev->family == AMDGPU_FAMILY_RV) 1666 all_hub = true; 1667 1668 for_each_inst(xcc, xcc_mask) { 1669 r = amdgpu_gmc_flush_gpu_tlb_pasid(adev, vm->pasid, flush_type, 1670 all_hub, xcc); 1671 if (r) 1672 break; 1673 } 1674 return r; 1675 } 1676 1677 /** 1678 * amdgpu_vm_bo_add - add a bo to a specific vm 1679 * 1680 * @adev: amdgpu_device pointer 1681 * @vm: requested vm 1682 * @bo: amdgpu buffer object 1683 * 1684 * Add @bo into the requested vm. 1685 * Add @bo to the list of bos associated with the vm 1686 * 1687 * Returns: 1688 * Newly added bo_va or NULL for failure 1689 * 1690 * Object has to be reserved! 1691 */ 1692 struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev, 1693 struct amdgpu_vm *vm, 1694 struct amdgpu_bo *bo) 1695 { 1696 struct amdgpu_bo_va *bo_va; 1697 1698 bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL); 1699 if (bo_va == NULL) { 1700 return NULL; 1701 } 1702 amdgpu_vm_bo_base_init(&bo_va->base, vm, bo); 1703 1704 bo_va->ref_count = 1; 1705 bo_va->last_pt_update = dma_fence_get_stub(); 1706 INIT_LIST_HEAD(&bo_va->valids); 1707 INIT_LIST_HEAD(&bo_va->invalids); 1708 1709 if (!bo) 1710 return bo_va; 1711 1712 dma_resv_assert_held(bo->tbo.base.resv); 1713 if (amdgpu_dmabuf_is_xgmi_accessible(adev, bo)) { 1714 bo_va->is_xgmi = true; 1715 /* Power up XGMI if it can be potentially used */ 1716 amdgpu_xgmi_set_pstate(adev, AMDGPU_XGMI_PSTATE_MAX_VEGA20); 1717 } 1718 1719 return bo_va; 1720 } 1721 1722 1723 /** 1724 * amdgpu_vm_bo_insert_map - insert a new mapping 1725 * 1726 * @adev: amdgpu_device pointer 1727 * @bo_va: bo_va to store the address 1728 * @mapping: the mapping to insert 1729 * 1730 * Insert a new mapping into all structures. 1731 */ 1732 static void amdgpu_vm_bo_insert_map(struct amdgpu_device *adev, 1733 struct amdgpu_bo_va *bo_va, 1734 struct amdgpu_bo_va_mapping *mapping) 1735 { 1736 struct amdgpu_vm *vm = bo_va->base.vm; 1737 struct amdgpu_bo *bo = bo_va->base.bo; 1738 1739 mapping->bo_va = bo_va; 1740 list_add(&mapping->list, &bo_va->invalids); 1741 amdgpu_vm_it_insert(mapping, &vm->va); 1742 1743 if (mapping->flags & AMDGPU_VM_PAGE_PRT) 1744 amdgpu_vm_prt_get(adev); 1745 1746 if (amdgpu_vm_is_bo_always_valid(vm, bo) && !bo_va->base.moved) 1747 amdgpu_vm_bo_moved(&bo_va->base); 1748 1749 trace_amdgpu_vm_bo_map(bo_va, mapping); 1750 } 1751 1752 /* Validate operation parameters to prevent potential abuse */ 1753 static int amdgpu_vm_verify_parameters(struct amdgpu_device *adev, 1754 struct amdgpu_bo *bo, 1755 uint64_t saddr, 1756 uint64_t offset, 1757 uint64_t size) 1758 { 1759 uint64_t tmp, lpfn; 1760 1761 if (saddr & AMDGPU_GPU_PAGE_MASK 1762 || offset & AMDGPU_GPU_PAGE_MASK 1763 || size & AMDGPU_GPU_PAGE_MASK) 1764 return -EINVAL; 1765 1766 if (check_add_overflow(saddr, size, &tmp) 1767 || check_add_overflow(offset, size, &tmp) 1768 || size == 0 /* which also leads to end < begin */) 1769 return -EINVAL; 1770 1771 /* make sure object fit at this offset */ 1772 if (bo && offset + size > amdgpu_bo_size(bo)) 1773 return -EINVAL; 1774 1775 /* Ensure last pfn not exceed max_pfn */ 1776 lpfn = (saddr + size - 1) >> AMDGPU_GPU_PAGE_SHIFT; 1777 if (lpfn >= adev->vm_manager.max_pfn) 1778 return -EINVAL; 1779 1780 return 0; 1781 } 1782 1783 /** 1784 * amdgpu_vm_bo_map - map bo inside a vm 1785 * 1786 * @adev: amdgpu_device pointer 1787 * @bo_va: bo_va to store the address 1788 * @saddr: where to map the BO 1789 * @offset: requested offset in the BO 1790 * @size: BO size in bytes 1791 * @flags: attributes of pages (read/write/valid/etc.) 1792 * 1793 * Add a mapping of the BO at the specefied addr into the VM. 1794 * 1795 * Returns: 1796 * 0 for success, error for failure. 1797 * 1798 * Object has to be reserved and unreserved outside! 1799 */ 1800 int amdgpu_vm_bo_map(struct amdgpu_device *adev, 1801 struct amdgpu_bo_va *bo_va, 1802 uint64_t saddr, uint64_t offset, 1803 uint64_t size, uint32_t flags) 1804 { 1805 struct amdgpu_bo_va_mapping *mapping, *tmp; 1806 struct amdgpu_bo *bo = bo_va->base.bo; 1807 struct amdgpu_vm *vm = bo_va->base.vm; 1808 uint64_t eaddr; 1809 int r; 1810 1811 r = amdgpu_vm_verify_parameters(adev, bo, saddr, offset, size); 1812 if (r) 1813 return r; 1814 1815 saddr /= AMDGPU_GPU_PAGE_SIZE; 1816 eaddr = saddr + (size - 1) / AMDGPU_GPU_PAGE_SIZE; 1817 1818 tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr); 1819 if (tmp) { 1820 /* bo and tmp overlap, invalid addr */ 1821 dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with " 1822 "0x%010Lx-0x%010Lx\n", bo, saddr, eaddr, 1823 tmp->start, tmp->last + 1); 1824 return -EINVAL; 1825 } 1826 1827 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL); 1828 if (!mapping) 1829 return -ENOMEM; 1830 1831 mapping->start = saddr; 1832 mapping->last = eaddr; 1833 mapping->offset = offset; 1834 mapping->flags = flags; 1835 1836 amdgpu_vm_bo_insert_map(adev, bo_va, mapping); 1837 1838 return 0; 1839 } 1840 1841 /** 1842 * amdgpu_vm_bo_replace_map - map bo inside a vm, replacing existing mappings 1843 * 1844 * @adev: amdgpu_device pointer 1845 * @bo_va: bo_va to store the address 1846 * @saddr: where to map the BO 1847 * @offset: requested offset in the BO 1848 * @size: BO size in bytes 1849 * @flags: attributes of pages (read/write/valid/etc.) 1850 * 1851 * Add a mapping of the BO at the specefied addr into the VM. Replace existing 1852 * mappings as we do so. 1853 * 1854 * Returns: 1855 * 0 for success, error for failure. 1856 * 1857 * Object has to be reserved and unreserved outside! 1858 */ 1859 int amdgpu_vm_bo_replace_map(struct amdgpu_device *adev, 1860 struct amdgpu_bo_va *bo_va, 1861 uint64_t saddr, uint64_t offset, 1862 uint64_t size, uint32_t flags) 1863 { 1864 struct amdgpu_bo_va_mapping *mapping; 1865 struct amdgpu_bo *bo = bo_va->base.bo; 1866 uint64_t eaddr; 1867 int r; 1868 1869 r = amdgpu_vm_verify_parameters(adev, bo, saddr, offset, size); 1870 if (r) 1871 return r; 1872 1873 /* Allocate all the needed memory */ 1874 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL); 1875 if (!mapping) 1876 return -ENOMEM; 1877 1878 r = amdgpu_vm_bo_clear_mappings(adev, bo_va->base.vm, saddr, size); 1879 if (r) { 1880 kfree(mapping); 1881 return r; 1882 } 1883 1884 saddr /= AMDGPU_GPU_PAGE_SIZE; 1885 eaddr = saddr + (size - 1) / AMDGPU_GPU_PAGE_SIZE; 1886 1887 mapping->start = saddr; 1888 mapping->last = eaddr; 1889 mapping->offset = offset; 1890 mapping->flags = flags; 1891 1892 amdgpu_vm_bo_insert_map(adev, bo_va, mapping); 1893 1894 return 0; 1895 } 1896 1897 /** 1898 * amdgpu_vm_bo_unmap - remove bo mapping from vm 1899 * 1900 * @adev: amdgpu_device pointer 1901 * @bo_va: bo_va to remove the address from 1902 * @saddr: where to the BO is mapped 1903 * 1904 * Remove a mapping of the BO at the specefied addr from the VM. 1905 * 1906 * Returns: 1907 * 0 for success, error for failure. 1908 * 1909 * Object has to be reserved and unreserved outside! 1910 */ 1911 int amdgpu_vm_bo_unmap(struct amdgpu_device *adev, 1912 struct amdgpu_bo_va *bo_va, 1913 uint64_t saddr) 1914 { 1915 struct amdgpu_bo_va_mapping *mapping; 1916 struct amdgpu_vm *vm = bo_va->base.vm; 1917 bool valid = true; 1918 1919 saddr /= AMDGPU_GPU_PAGE_SIZE; 1920 1921 list_for_each_entry(mapping, &bo_va->valids, list) { 1922 if (mapping->start == saddr) 1923 break; 1924 } 1925 1926 if (&mapping->list == &bo_va->valids) { 1927 valid = false; 1928 1929 list_for_each_entry(mapping, &bo_va->invalids, list) { 1930 if (mapping->start == saddr) 1931 break; 1932 } 1933 1934 if (&mapping->list == &bo_va->invalids) 1935 return -ENOENT; 1936 } 1937 1938 list_del(&mapping->list); 1939 amdgpu_vm_it_remove(mapping, &vm->va); 1940 mapping->bo_va = NULL; 1941 trace_amdgpu_vm_bo_unmap(bo_va, mapping); 1942 1943 if (valid) 1944 list_add(&mapping->list, &vm->freed); 1945 else 1946 amdgpu_vm_free_mapping(adev, vm, mapping, 1947 bo_va->last_pt_update); 1948 1949 return 0; 1950 } 1951 1952 /** 1953 * amdgpu_vm_bo_clear_mappings - remove all mappings in a specific range 1954 * 1955 * @adev: amdgpu_device pointer 1956 * @vm: VM structure to use 1957 * @saddr: start of the range 1958 * @size: size of the range 1959 * 1960 * Remove all mappings in a range, split them as appropriate. 1961 * 1962 * Returns: 1963 * 0 for success, error for failure. 1964 */ 1965 int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev, 1966 struct amdgpu_vm *vm, 1967 uint64_t saddr, uint64_t size) 1968 { 1969 struct amdgpu_bo_va_mapping *before, *after, *tmp, *next; 1970 LIST_HEAD(removed); 1971 uint64_t eaddr; 1972 int r; 1973 1974 r = amdgpu_vm_verify_parameters(adev, NULL, saddr, 0, size); 1975 if (r) 1976 return r; 1977 1978 saddr /= AMDGPU_GPU_PAGE_SIZE; 1979 eaddr = saddr + (size - 1) / AMDGPU_GPU_PAGE_SIZE; 1980 1981 /* Allocate all the needed memory */ 1982 before = kzalloc(sizeof(*before), GFP_KERNEL); 1983 if (!before) 1984 return -ENOMEM; 1985 INIT_LIST_HEAD(&before->list); 1986 1987 after = kzalloc(sizeof(*after), GFP_KERNEL); 1988 if (!after) { 1989 kfree(before); 1990 return -ENOMEM; 1991 } 1992 INIT_LIST_HEAD(&after->list); 1993 1994 /* Now gather all removed mappings */ 1995 tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr); 1996 while (tmp) { 1997 /* Remember mapping split at the start */ 1998 if (tmp->start < saddr) { 1999 before->start = tmp->start; 2000 before->last = saddr - 1; 2001 before->offset = tmp->offset; 2002 before->flags = tmp->flags; 2003 before->bo_va = tmp->bo_va; 2004 list_add(&before->list, &tmp->bo_va->invalids); 2005 } 2006 2007 /* Remember mapping split at the end */ 2008 if (tmp->last > eaddr) { 2009 after->start = eaddr + 1; 2010 after->last = tmp->last; 2011 after->offset = tmp->offset; 2012 after->offset += (after->start - tmp->start) << PAGE_SHIFT; 2013 after->flags = tmp->flags; 2014 after->bo_va = tmp->bo_va; 2015 list_add(&after->list, &tmp->bo_va->invalids); 2016 } 2017 2018 list_del(&tmp->list); 2019 list_add(&tmp->list, &removed); 2020 2021 tmp = amdgpu_vm_it_iter_next(tmp, saddr, eaddr); 2022 } 2023 2024 /* And free them up */ 2025 list_for_each_entry_safe(tmp, next, &removed, list) { 2026 amdgpu_vm_it_remove(tmp, &vm->va); 2027 list_del(&tmp->list); 2028 2029 if (tmp->start < saddr) 2030 tmp->start = saddr; 2031 if (tmp->last > eaddr) 2032 tmp->last = eaddr; 2033 2034 tmp->bo_va = NULL; 2035 list_add(&tmp->list, &vm->freed); 2036 trace_amdgpu_vm_bo_unmap(NULL, tmp); 2037 } 2038 2039 /* Insert partial mapping before the range */ 2040 if (!list_empty(&before->list)) { 2041 struct amdgpu_bo *bo = before->bo_va->base.bo; 2042 2043 amdgpu_vm_it_insert(before, &vm->va); 2044 if (before->flags & AMDGPU_PTE_PRT_FLAG(adev)) 2045 amdgpu_vm_prt_get(adev); 2046 2047 if (amdgpu_vm_is_bo_always_valid(vm, bo) && 2048 !before->bo_va->base.moved) 2049 amdgpu_vm_bo_moved(&before->bo_va->base); 2050 } else { 2051 kfree(before); 2052 } 2053 2054 /* Insert partial mapping after the range */ 2055 if (!list_empty(&after->list)) { 2056 struct amdgpu_bo *bo = after->bo_va->base.bo; 2057 2058 amdgpu_vm_it_insert(after, &vm->va); 2059 if (after->flags & AMDGPU_PTE_PRT_FLAG(adev)) 2060 amdgpu_vm_prt_get(adev); 2061 2062 if (amdgpu_vm_is_bo_always_valid(vm, bo) && 2063 !after->bo_va->base.moved) 2064 amdgpu_vm_bo_moved(&after->bo_va->base); 2065 } else { 2066 kfree(after); 2067 } 2068 2069 return 0; 2070 } 2071 2072 /** 2073 * amdgpu_vm_bo_lookup_mapping - find mapping by address 2074 * 2075 * @vm: the requested VM 2076 * @addr: the address 2077 * 2078 * Find a mapping by it's address. 2079 * 2080 * Returns: 2081 * The amdgpu_bo_va_mapping matching for addr or NULL 2082 * 2083 */ 2084 struct amdgpu_bo_va_mapping *amdgpu_vm_bo_lookup_mapping(struct amdgpu_vm *vm, 2085 uint64_t addr) 2086 { 2087 return amdgpu_vm_it_iter_first(&vm->va, addr, addr); 2088 } 2089 2090 /** 2091 * amdgpu_vm_bo_trace_cs - trace all reserved mappings 2092 * 2093 * @vm: the requested vm 2094 * @ticket: CS ticket 2095 * 2096 * Trace all mappings of BOs reserved during a command submission. 2097 */ 2098 void amdgpu_vm_bo_trace_cs(struct amdgpu_vm *vm, struct ww_acquire_ctx *ticket) 2099 { 2100 struct amdgpu_bo_va_mapping *mapping; 2101 2102 if (!trace_amdgpu_vm_bo_cs_enabled()) 2103 return; 2104 2105 for (mapping = amdgpu_vm_it_iter_first(&vm->va, 0, U64_MAX); mapping; 2106 mapping = amdgpu_vm_it_iter_next(mapping, 0, U64_MAX)) { 2107 if (mapping->bo_va && mapping->bo_va->base.bo) { 2108 struct amdgpu_bo *bo; 2109 2110 bo = mapping->bo_va->base.bo; 2111 if (dma_resv_locking_ctx(bo->tbo.base.resv) != 2112 ticket) 2113 continue; 2114 } 2115 2116 trace_amdgpu_vm_bo_cs(mapping); 2117 } 2118 } 2119 2120 /** 2121 * amdgpu_vm_bo_del - remove a bo from a specific vm 2122 * 2123 * @adev: amdgpu_device pointer 2124 * @bo_va: requested bo_va 2125 * 2126 * Remove @bo_va->bo from the requested vm. 2127 * 2128 * Object have to be reserved! 2129 */ 2130 void amdgpu_vm_bo_del(struct amdgpu_device *adev, 2131 struct amdgpu_bo_va *bo_va) 2132 { 2133 struct amdgpu_bo_va_mapping *mapping, *next; 2134 struct amdgpu_bo *bo = bo_va->base.bo; 2135 struct amdgpu_vm *vm = bo_va->base.vm; 2136 struct amdgpu_vm_bo_base **base; 2137 2138 dma_resv_assert_held(vm->root.bo->tbo.base.resv); 2139 2140 if (bo) { 2141 dma_resv_assert_held(bo->tbo.base.resv); 2142 if (amdgpu_vm_is_bo_always_valid(vm, bo)) 2143 ttm_bo_set_bulk_move(&bo->tbo, NULL); 2144 2145 for (base = &bo_va->base.bo->vm_bo; *base; 2146 base = &(*base)->next) { 2147 if (*base != &bo_va->base) 2148 continue; 2149 2150 amdgpu_vm_update_stats(*base, bo->tbo.resource, -1); 2151 *base = bo_va->base.next; 2152 break; 2153 } 2154 } 2155 2156 spin_lock(&vm->invalidated_lock); 2157 list_del(&bo_va->base.vm_status); 2158 spin_unlock(&vm->invalidated_lock); 2159 2160 list_for_each_entry_safe(mapping, next, &bo_va->valids, list) { 2161 list_del(&mapping->list); 2162 amdgpu_vm_it_remove(mapping, &vm->va); 2163 mapping->bo_va = NULL; 2164 trace_amdgpu_vm_bo_unmap(bo_va, mapping); 2165 list_add(&mapping->list, &vm->freed); 2166 } 2167 list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) { 2168 list_del(&mapping->list); 2169 amdgpu_vm_it_remove(mapping, &vm->va); 2170 amdgpu_vm_free_mapping(adev, vm, mapping, 2171 bo_va->last_pt_update); 2172 } 2173 2174 dma_fence_put(bo_va->last_pt_update); 2175 2176 if (bo && bo_va->is_xgmi) 2177 amdgpu_xgmi_set_pstate(adev, AMDGPU_XGMI_PSTATE_MIN); 2178 2179 kfree(bo_va); 2180 } 2181 2182 /** 2183 * amdgpu_vm_evictable - check if we can evict a VM 2184 * 2185 * @bo: A page table of the VM. 2186 * 2187 * Check if it is possible to evict a VM. 2188 */ 2189 bool amdgpu_vm_evictable(struct amdgpu_bo *bo) 2190 { 2191 struct amdgpu_vm_bo_base *bo_base = bo->vm_bo; 2192 2193 /* Page tables of a destroyed VM can go away immediately */ 2194 if (!bo_base || !bo_base->vm) 2195 return true; 2196 2197 /* Don't evict VM page tables while they are busy */ 2198 if (!dma_resv_test_signaled(bo->tbo.base.resv, DMA_RESV_USAGE_BOOKKEEP)) 2199 return false; 2200 2201 /* Try to block ongoing updates */ 2202 if (!amdgpu_vm_eviction_trylock(bo_base->vm)) 2203 return false; 2204 2205 /* Don't evict VM page tables while they are updated */ 2206 if (!dma_fence_is_signaled(bo_base->vm->last_unlocked)) { 2207 amdgpu_vm_eviction_unlock(bo_base->vm); 2208 return false; 2209 } 2210 2211 bo_base->vm->evicting = true; 2212 amdgpu_vm_eviction_unlock(bo_base->vm); 2213 return true; 2214 } 2215 2216 /** 2217 * amdgpu_vm_bo_invalidate - mark the bo as invalid 2218 * 2219 * @bo: amdgpu buffer object 2220 * @evicted: is the BO evicted 2221 * 2222 * Mark @bo as invalid. 2223 */ 2224 void amdgpu_vm_bo_invalidate(struct amdgpu_bo *bo, bool evicted) 2225 { 2226 struct amdgpu_vm_bo_base *bo_base; 2227 2228 for (bo_base = bo->vm_bo; bo_base; bo_base = bo_base->next) { 2229 struct amdgpu_vm *vm = bo_base->vm; 2230 2231 if (evicted && amdgpu_vm_is_bo_always_valid(vm, bo)) { 2232 amdgpu_vm_bo_evicted(bo_base); 2233 continue; 2234 } 2235 2236 if (bo_base->moved) 2237 continue; 2238 bo_base->moved = true; 2239 2240 if (bo->tbo.type == ttm_bo_type_kernel) 2241 amdgpu_vm_bo_relocated(bo_base); 2242 else if (amdgpu_vm_is_bo_always_valid(vm, bo)) 2243 amdgpu_vm_bo_moved(bo_base); 2244 else 2245 amdgpu_vm_bo_invalidated(bo_base); 2246 } 2247 } 2248 2249 /** 2250 * amdgpu_vm_bo_move - handle BO move 2251 * 2252 * @bo: amdgpu buffer object 2253 * @new_mem: the new placement of the BO move 2254 * @evicted: is the BO evicted 2255 * 2256 * Update the memory stats for the new placement and mark @bo as invalid. 2257 */ 2258 void amdgpu_vm_bo_move(struct amdgpu_bo *bo, struct ttm_resource *new_mem, 2259 bool evicted) 2260 { 2261 struct amdgpu_vm_bo_base *bo_base; 2262 2263 for (bo_base = bo->vm_bo; bo_base; bo_base = bo_base->next) { 2264 struct amdgpu_vm *vm = bo_base->vm; 2265 2266 spin_lock(&vm->stats_lock); 2267 amdgpu_vm_update_stats_locked(bo_base, bo->tbo.resource, -1); 2268 amdgpu_vm_update_stats_locked(bo_base, new_mem, +1); 2269 spin_unlock(&vm->stats_lock); 2270 } 2271 2272 amdgpu_vm_bo_invalidate(bo, evicted); 2273 } 2274 2275 /** 2276 * amdgpu_vm_get_block_size - calculate VM page table size as power of two 2277 * 2278 * @vm_size: VM size 2279 * 2280 * Returns: 2281 * VM page table as power of two 2282 */ 2283 static uint32_t amdgpu_vm_get_block_size(uint64_t vm_size) 2284 { 2285 /* Total bits covered by PD + PTs */ 2286 unsigned bits = ilog2(vm_size) + 18; 2287 2288 /* Make sure the PD is 4K in size up to 8GB address space. 2289 Above that split equal between PD and PTs */ 2290 if (vm_size <= 8) 2291 return (bits - 9); 2292 else 2293 return ((bits + 3) / 2); 2294 } 2295 2296 /** 2297 * amdgpu_vm_adjust_size - adjust vm size, block size and fragment size 2298 * 2299 * @adev: amdgpu_device pointer 2300 * @min_vm_size: the minimum vm size in GB if it's set auto 2301 * @fragment_size_default: Default PTE fragment size 2302 * @max_level: max VMPT level 2303 * @max_bits: max address space size in bits 2304 * 2305 */ 2306 void amdgpu_vm_adjust_size(struct amdgpu_device *adev, uint32_t min_vm_size, 2307 uint32_t fragment_size_default, unsigned max_level, 2308 unsigned max_bits) 2309 { 2310 unsigned int max_size = 1 << (max_bits - 30); 2311 unsigned int vm_size; 2312 uint64_t tmp; 2313 2314 /* adjust vm size first */ 2315 if (amdgpu_vm_size != -1) { 2316 vm_size = amdgpu_vm_size; 2317 if (vm_size > max_size) { 2318 dev_warn(adev->dev, "VM size (%d) too large, max is %u GB\n", 2319 amdgpu_vm_size, max_size); 2320 vm_size = max_size; 2321 } 2322 } else { 2323 struct sysinfo si; 2324 unsigned int phys_ram_gb; 2325 2326 /* Optimal VM size depends on the amount of physical 2327 * RAM available. Underlying requirements and 2328 * assumptions: 2329 * 2330 * - Need to map system memory and VRAM from all GPUs 2331 * - VRAM from other GPUs not known here 2332 * - Assume VRAM <= system memory 2333 * - On GFX8 and older, VM space can be segmented for 2334 * different MTYPEs 2335 * - Need to allow room for fragmentation, guard pages etc. 2336 * 2337 * This adds up to a rough guess of system memory x3. 2338 * Round up to power of two to maximize the available 2339 * VM size with the given page table size. 2340 */ 2341 si_meminfo(&si); 2342 phys_ram_gb = ((uint64_t)si.totalram * si.mem_unit + 2343 (1 << 30) - 1) >> 30; 2344 vm_size = roundup_pow_of_two( 2345 clamp(phys_ram_gb * 3, min_vm_size, max_size)); 2346 } 2347 2348 adev->vm_manager.max_pfn = (uint64_t)vm_size << 18; 2349 2350 tmp = roundup_pow_of_two(adev->vm_manager.max_pfn); 2351 if (amdgpu_vm_block_size != -1) 2352 tmp >>= amdgpu_vm_block_size - 9; 2353 tmp = DIV_ROUND_UP(fls64(tmp) - 1, 9) - 1; 2354 adev->vm_manager.num_level = min_t(unsigned int, max_level, tmp); 2355 switch (adev->vm_manager.num_level) { 2356 case 3: 2357 adev->vm_manager.root_level = AMDGPU_VM_PDB2; 2358 break; 2359 case 2: 2360 adev->vm_manager.root_level = AMDGPU_VM_PDB1; 2361 break; 2362 case 1: 2363 adev->vm_manager.root_level = AMDGPU_VM_PDB0; 2364 break; 2365 default: 2366 dev_err(adev->dev, "VMPT only supports 2~4+1 levels\n"); 2367 } 2368 /* block size depends on vm size and hw setup*/ 2369 if (amdgpu_vm_block_size != -1) 2370 adev->vm_manager.block_size = 2371 min((unsigned)amdgpu_vm_block_size, max_bits 2372 - AMDGPU_GPU_PAGE_SHIFT 2373 - 9 * adev->vm_manager.num_level); 2374 else if (adev->vm_manager.num_level > 1) 2375 adev->vm_manager.block_size = 9; 2376 else 2377 adev->vm_manager.block_size = amdgpu_vm_get_block_size(tmp); 2378 2379 if (amdgpu_vm_fragment_size == -1) 2380 adev->vm_manager.fragment_size = fragment_size_default; 2381 else 2382 adev->vm_manager.fragment_size = amdgpu_vm_fragment_size; 2383 2384 dev_info( 2385 adev->dev, 2386 "vm size is %u GB, %u levels, block size is %u-bit, fragment size is %u-bit\n", 2387 vm_size, adev->vm_manager.num_level + 1, 2388 adev->vm_manager.block_size, adev->vm_manager.fragment_size); 2389 } 2390 2391 /** 2392 * amdgpu_vm_wait_idle - wait for the VM to become idle 2393 * 2394 * @vm: VM object to wait for 2395 * @timeout: timeout to wait for VM to become idle 2396 */ 2397 long amdgpu_vm_wait_idle(struct amdgpu_vm *vm, long timeout) 2398 { 2399 timeout = drm_sched_entity_flush(&vm->immediate, timeout); 2400 if (timeout <= 0) 2401 return timeout; 2402 2403 return drm_sched_entity_flush(&vm->delayed, timeout); 2404 } 2405 2406 static void amdgpu_vm_destroy_task_info(struct kref *kref) 2407 { 2408 struct amdgpu_task_info *ti = container_of(kref, struct amdgpu_task_info, refcount); 2409 2410 kfree(ti); 2411 } 2412 2413 static inline struct amdgpu_vm * 2414 amdgpu_vm_get_vm_from_pasid(struct amdgpu_device *adev, u32 pasid) 2415 { 2416 struct amdgpu_vm *vm; 2417 unsigned long flags; 2418 2419 xa_lock_irqsave(&adev->vm_manager.pasids, flags); 2420 vm = xa_load(&adev->vm_manager.pasids, pasid); 2421 xa_unlock_irqrestore(&adev->vm_manager.pasids, flags); 2422 2423 return vm; 2424 } 2425 2426 /** 2427 * amdgpu_vm_put_task_info - reference down the vm task_info ptr 2428 * 2429 * @task_info: task_info struct under discussion. 2430 * 2431 * frees the vm task_info ptr at the last put 2432 */ 2433 void amdgpu_vm_put_task_info(struct amdgpu_task_info *task_info) 2434 { 2435 if (task_info) 2436 kref_put(&task_info->refcount, amdgpu_vm_destroy_task_info); 2437 } 2438 2439 /** 2440 * amdgpu_vm_get_task_info_vm - Extracts task info for a vm. 2441 * 2442 * @vm: VM to get info from 2443 * 2444 * Returns the reference counted task_info structure, which must be 2445 * referenced down with amdgpu_vm_put_task_info. 2446 */ 2447 struct amdgpu_task_info * 2448 amdgpu_vm_get_task_info_vm(struct amdgpu_vm *vm) 2449 { 2450 struct amdgpu_task_info *ti = NULL; 2451 2452 if (vm) { 2453 ti = vm->task_info; 2454 kref_get(&vm->task_info->refcount); 2455 } 2456 2457 return ti; 2458 } 2459 2460 /** 2461 * amdgpu_vm_get_task_info_pasid - Extracts task info for a PASID. 2462 * 2463 * @adev: drm device pointer 2464 * @pasid: PASID identifier for VM 2465 * 2466 * Returns the reference counted task_info structure, which must be 2467 * referenced down with amdgpu_vm_put_task_info. 2468 */ 2469 struct amdgpu_task_info * 2470 amdgpu_vm_get_task_info_pasid(struct amdgpu_device *adev, u32 pasid) 2471 { 2472 return amdgpu_vm_get_task_info_vm( 2473 amdgpu_vm_get_vm_from_pasid(adev, pasid)); 2474 } 2475 2476 static int amdgpu_vm_create_task_info(struct amdgpu_vm *vm) 2477 { 2478 vm->task_info = kzalloc(sizeof(struct amdgpu_task_info), GFP_KERNEL); 2479 if (!vm->task_info) 2480 return -ENOMEM; 2481 2482 kref_init(&vm->task_info->refcount); 2483 return 0; 2484 } 2485 2486 /** 2487 * amdgpu_vm_set_task_info - Sets VMs task info. 2488 * 2489 * @vm: vm for which to set the info 2490 */ 2491 void amdgpu_vm_set_task_info(struct amdgpu_vm *vm) 2492 { 2493 if (!vm->task_info) 2494 return; 2495 2496 if (vm->task_info->task.pid == current->pid) 2497 return; 2498 2499 vm->task_info->task.pid = current->pid; 2500 get_task_comm(vm->task_info->task.comm, current); 2501 2502 if (current->group_leader->mm != current->mm) 2503 return; 2504 2505 vm->task_info->tgid = current->group_leader->pid; 2506 get_task_comm(vm->task_info->process_name, current->group_leader); 2507 } 2508 2509 /** 2510 * amdgpu_vm_init - initialize a vm instance 2511 * 2512 * @adev: amdgpu_device pointer 2513 * @vm: requested vm 2514 * @xcp_id: GPU partition selection id 2515 * @pasid: the pasid the VM is using on this GPU 2516 * 2517 * Init @vm fields. 2518 * 2519 * Returns: 2520 * 0 for success, error for failure. 2521 */ 2522 int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm, 2523 int32_t xcp_id, uint32_t pasid) 2524 { 2525 struct amdgpu_bo *root_bo; 2526 struct amdgpu_bo_vm *root; 2527 int r, i; 2528 2529 vm->va = RB_ROOT_CACHED; 2530 for (i = 0; i < AMDGPU_MAX_VMHUBS; i++) 2531 vm->reserved_vmid[i] = NULL; 2532 INIT_LIST_HEAD(&vm->evicted); 2533 INIT_LIST_HEAD(&vm->evicted_user); 2534 INIT_LIST_HEAD(&vm->relocated); 2535 INIT_LIST_HEAD(&vm->moved); 2536 INIT_LIST_HEAD(&vm->idle); 2537 spin_lock_init(&vm->invalidated_lock); 2538 INIT_LIST_HEAD(&vm->invalidated); 2539 INIT_LIST_HEAD(&vm->freed); 2540 INIT_LIST_HEAD(&vm->done); 2541 INIT_KFIFO(vm->faults); 2542 spin_lock_init(&vm->stats_lock); 2543 2544 r = amdgpu_vm_init_entities(adev, vm); 2545 if (r) 2546 return r; 2547 2548 ttm_lru_bulk_move_init(&vm->lru_bulk_move); 2549 2550 vm->is_compute_context = false; 2551 2552 vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode & 2553 AMDGPU_VM_USE_CPU_FOR_GFX); 2554 2555 dev_dbg(adev->dev, "VM update mode is %s\n", 2556 vm->use_cpu_for_update ? "CPU" : "SDMA"); 2557 WARN_ONCE((vm->use_cpu_for_update && 2558 !amdgpu_gmc_vram_full_visible(&adev->gmc)), 2559 "CPU update of VM recommended only for large BAR system\n"); 2560 2561 if (vm->use_cpu_for_update) 2562 vm->update_funcs = &amdgpu_vm_cpu_funcs; 2563 else 2564 vm->update_funcs = &amdgpu_vm_sdma_funcs; 2565 2566 vm->last_update = dma_fence_get_stub(); 2567 vm->last_unlocked = dma_fence_get_stub(); 2568 vm->last_tlb_flush = dma_fence_get_stub(); 2569 vm->generation = amdgpu_vm_generation(adev, NULL); 2570 2571 mutex_init(&vm->eviction_lock); 2572 vm->evicting = false; 2573 vm->tlb_fence_context = dma_fence_context_alloc(1); 2574 2575 r = amdgpu_vm_pt_create(adev, vm, adev->vm_manager.root_level, 2576 false, &root, xcp_id); 2577 if (r) 2578 goto error_free_delayed; 2579 2580 root_bo = amdgpu_bo_ref(&root->bo); 2581 r = amdgpu_bo_reserve(root_bo, true); 2582 if (r) { 2583 amdgpu_bo_unref(&root_bo); 2584 goto error_free_delayed; 2585 } 2586 2587 amdgpu_vm_bo_base_init(&vm->root, vm, root_bo); 2588 r = dma_resv_reserve_fences(root_bo->tbo.base.resv, 1); 2589 if (r) 2590 goto error_free_root; 2591 2592 r = amdgpu_vm_pt_clear(adev, vm, root, false); 2593 if (r) 2594 goto error_free_root; 2595 2596 r = amdgpu_vm_create_task_info(vm); 2597 if (r) 2598 dev_dbg(adev->dev, "Failed to create task info for VM\n"); 2599 2600 /* Store new PASID in XArray (if non-zero) */ 2601 if (pasid != 0) { 2602 r = xa_err(xa_store_irq(&adev->vm_manager.pasids, pasid, vm, GFP_KERNEL)); 2603 if (r < 0) 2604 goto error_free_root; 2605 2606 vm->pasid = pasid; 2607 } 2608 2609 amdgpu_bo_unreserve(vm->root.bo); 2610 amdgpu_bo_unref(&root_bo); 2611 2612 return 0; 2613 2614 error_free_root: 2615 /* If PASID was partially set, erase it from XArray before failing */ 2616 if (vm->pasid != 0) { 2617 xa_erase_irq(&adev->vm_manager.pasids, vm->pasid); 2618 vm->pasid = 0; 2619 } 2620 amdgpu_vm_pt_free_root(adev, vm); 2621 amdgpu_bo_unreserve(vm->root.bo); 2622 amdgpu_bo_unref(&root_bo); 2623 2624 error_free_delayed: 2625 dma_fence_put(vm->last_tlb_flush); 2626 dma_fence_put(vm->last_unlocked); 2627 ttm_lru_bulk_move_fini(&adev->mman.bdev, &vm->lru_bulk_move); 2628 amdgpu_vm_fini_entities(vm); 2629 2630 return r; 2631 } 2632 2633 /** 2634 * amdgpu_vm_make_compute - Turn a GFX VM into a compute VM 2635 * 2636 * @adev: amdgpu_device pointer 2637 * @vm: requested vm 2638 * 2639 * This only works on GFX VMs that don't have any BOs added and no 2640 * page tables allocated yet. 2641 * 2642 * Changes the following VM parameters: 2643 * - use_cpu_for_update 2644 * - pte_supports_ats 2645 * 2646 * Reinitializes the page directory to reflect the changed ATS 2647 * setting. 2648 * 2649 * Returns: 2650 * 0 for success, -errno for errors. 2651 */ 2652 int amdgpu_vm_make_compute(struct amdgpu_device *adev, struct amdgpu_vm *vm) 2653 { 2654 int r; 2655 2656 r = amdgpu_bo_reserve(vm->root.bo, true); 2657 if (r) 2658 return r; 2659 2660 /* Update VM state */ 2661 vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode & 2662 AMDGPU_VM_USE_CPU_FOR_COMPUTE); 2663 dev_dbg(adev->dev, "VM update mode is %s\n", 2664 vm->use_cpu_for_update ? "CPU" : "SDMA"); 2665 WARN_ONCE((vm->use_cpu_for_update && 2666 !amdgpu_gmc_vram_full_visible(&adev->gmc)), 2667 "CPU update of VM recommended only for large BAR system\n"); 2668 2669 if (vm->use_cpu_for_update) { 2670 /* Sync with last SDMA update/clear before switching to CPU */ 2671 r = amdgpu_bo_sync_wait(vm->root.bo, 2672 AMDGPU_FENCE_OWNER_UNDEFINED, true); 2673 if (r) 2674 goto unreserve_bo; 2675 2676 vm->update_funcs = &amdgpu_vm_cpu_funcs; 2677 r = amdgpu_vm_pt_map_tables(adev, vm); 2678 if (r) 2679 goto unreserve_bo; 2680 2681 } else { 2682 vm->update_funcs = &amdgpu_vm_sdma_funcs; 2683 } 2684 2685 dma_fence_put(vm->last_update); 2686 vm->last_update = dma_fence_get_stub(); 2687 vm->is_compute_context = true; 2688 2689 unreserve_bo: 2690 amdgpu_bo_unreserve(vm->root.bo); 2691 return r; 2692 } 2693 2694 static int amdgpu_vm_stats_is_zero(struct amdgpu_vm *vm) 2695 { 2696 for (int i = 0; i < __AMDGPU_PL_NUM; ++i) { 2697 if (!(drm_memory_stats_is_zero(&vm->stats[i].drm) && 2698 vm->stats[i].evicted == 0)) 2699 return false; 2700 } 2701 return true; 2702 } 2703 2704 /** 2705 * amdgpu_vm_fini - tear down a vm instance 2706 * 2707 * @adev: amdgpu_device pointer 2708 * @vm: requested vm 2709 * 2710 * Tear down @vm. 2711 * Unbind the VM and remove all bos from the vm bo list 2712 */ 2713 void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm) 2714 { 2715 struct amdgpu_bo_va_mapping *mapping, *tmp; 2716 bool prt_fini_needed = !!adev->gmc.gmc_funcs->set_prt; 2717 struct amdgpu_bo *root; 2718 unsigned long flags; 2719 int i; 2720 2721 amdgpu_amdkfd_gpuvm_destroy_cb(adev, vm); 2722 2723 root = amdgpu_bo_ref(vm->root.bo); 2724 amdgpu_bo_reserve(root, true); 2725 /* Remove PASID mapping before destroying VM */ 2726 if (vm->pasid != 0) { 2727 xa_erase_irq(&adev->vm_manager.pasids, vm->pasid); 2728 vm->pasid = 0; 2729 } 2730 dma_fence_wait(vm->last_unlocked, false); 2731 dma_fence_put(vm->last_unlocked); 2732 dma_fence_wait(vm->last_tlb_flush, false); 2733 /* Make sure that all fence callbacks have completed */ 2734 spin_lock_irqsave(vm->last_tlb_flush->lock, flags); 2735 spin_unlock_irqrestore(vm->last_tlb_flush->lock, flags); 2736 dma_fence_put(vm->last_tlb_flush); 2737 2738 list_for_each_entry_safe(mapping, tmp, &vm->freed, list) { 2739 if (mapping->flags & AMDGPU_VM_PAGE_PRT && prt_fini_needed) { 2740 amdgpu_vm_prt_fini(adev, vm); 2741 prt_fini_needed = false; 2742 } 2743 2744 list_del(&mapping->list); 2745 amdgpu_vm_free_mapping(adev, vm, mapping, NULL); 2746 } 2747 2748 amdgpu_vm_pt_free_root(adev, vm); 2749 amdgpu_bo_unreserve(root); 2750 amdgpu_bo_unref(&root); 2751 WARN_ON(vm->root.bo); 2752 2753 amdgpu_vm_fini_entities(vm); 2754 2755 if (!RB_EMPTY_ROOT(&vm->va.rb_root)) { 2756 dev_err(adev->dev, "still active bo inside vm\n"); 2757 } 2758 rbtree_postorder_for_each_entry_safe(mapping, tmp, 2759 &vm->va.rb_root, rb) { 2760 /* Don't remove the mapping here, we don't want to trigger a 2761 * rebalance and the tree is about to be destroyed anyway. 2762 */ 2763 list_del(&mapping->list); 2764 kfree(mapping); 2765 } 2766 2767 dma_fence_put(vm->last_update); 2768 2769 for (i = 0; i < AMDGPU_MAX_VMHUBS; i++) { 2770 amdgpu_vmid_free_reserved(adev, vm, i); 2771 } 2772 2773 ttm_lru_bulk_move_fini(&adev->mman.bdev, &vm->lru_bulk_move); 2774 2775 if (!amdgpu_vm_stats_is_zero(vm)) { 2776 struct amdgpu_task_info *ti = vm->task_info; 2777 2778 dev_warn(adev->dev, 2779 "VM memory stats for proc %s(%d) task %s(%d) is non-zero when fini\n", 2780 ti->process_name, ti->task.pid, ti->task.comm, ti->tgid); 2781 } 2782 2783 amdgpu_vm_put_task_info(vm->task_info); 2784 } 2785 2786 /** 2787 * amdgpu_vm_manager_init - init the VM manager 2788 * 2789 * @adev: amdgpu_device pointer 2790 * 2791 * Initialize the VM manager structures 2792 */ 2793 void amdgpu_vm_manager_init(struct amdgpu_device *adev) 2794 { 2795 unsigned i; 2796 2797 /* Concurrent flushes are only possible starting with Vega10 and 2798 * are broken on Navi10 and Navi14. 2799 */ 2800 adev->vm_manager.concurrent_flush = !(adev->asic_type < CHIP_VEGA10 || 2801 adev->asic_type == CHIP_NAVI10 || 2802 adev->asic_type == CHIP_NAVI14); 2803 amdgpu_vmid_mgr_init(adev); 2804 2805 adev->vm_manager.fence_context = 2806 dma_fence_context_alloc(AMDGPU_MAX_RINGS); 2807 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) 2808 adev->vm_manager.seqno[i] = 0; 2809 2810 spin_lock_init(&adev->vm_manager.prt_lock); 2811 atomic_set(&adev->vm_manager.num_prt_users, 0); 2812 2813 /* If not overridden by the user, by default, only in large BAR systems 2814 * Compute VM tables will be updated by CPU 2815 */ 2816 #ifdef CONFIG_X86_64 2817 if (amdgpu_vm_update_mode == -1) { 2818 /* For asic with VF MMIO access protection 2819 * avoid using CPU for VM table updates 2820 */ 2821 if (amdgpu_gmc_vram_full_visible(&adev->gmc) && 2822 !amdgpu_sriov_vf_mmio_access_protection(adev)) 2823 adev->vm_manager.vm_update_mode = 2824 AMDGPU_VM_USE_CPU_FOR_COMPUTE; 2825 else 2826 adev->vm_manager.vm_update_mode = 0; 2827 } else 2828 adev->vm_manager.vm_update_mode = amdgpu_vm_update_mode; 2829 #else 2830 adev->vm_manager.vm_update_mode = 0; 2831 #endif 2832 2833 xa_init_flags(&adev->vm_manager.pasids, XA_FLAGS_LOCK_IRQ); 2834 } 2835 2836 /** 2837 * amdgpu_vm_manager_fini - cleanup VM manager 2838 * 2839 * @adev: amdgpu_device pointer 2840 * 2841 * Cleanup the VM manager and free resources. 2842 */ 2843 void amdgpu_vm_manager_fini(struct amdgpu_device *adev) 2844 { 2845 WARN_ON(!xa_empty(&adev->vm_manager.pasids)); 2846 xa_destroy(&adev->vm_manager.pasids); 2847 2848 amdgpu_vmid_mgr_fini(adev); 2849 } 2850 2851 /** 2852 * amdgpu_vm_ioctl - Manages VMID reservation for vm hubs. 2853 * 2854 * @dev: drm device pointer 2855 * @data: drm_amdgpu_vm 2856 * @filp: drm file pointer 2857 * 2858 * Returns: 2859 * 0 for success, -errno for errors. 2860 */ 2861 int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp) 2862 { 2863 union drm_amdgpu_vm *args = data; 2864 struct amdgpu_device *adev = drm_to_adev(dev); 2865 struct amdgpu_fpriv *fpriv = filp->driver_priv; 2866 struct amdgpu_vm *vm = &fpriv->vm; 2867 2868 /* No valid flags defined yet */ 2869 if (args->in.flags) 2870 return -EINVAL; 2871 2872 switch (args->in.op) { 2873 case AMDGPU_VM_OP_RESERVE_VMID: 2874 /* We only have requirement to reserve vmid from gfxhub */ 2875 amdgpu_vmid_alloc_reserved(adev, vm, AMDGPU_GFXHUB(0)); 2876 break; 2877 case AMDGPU_VM_OP_UNRESERVE_VMID: 2878 amdgpu_vmid_free_reserved(adev, vm, AMDGPU_GFXHUB(0)); 2879 break; 2880 default: 2881 return -EINVAL; 2882 } 2883 2884 return 0; 2885 } 2886 2887 /** 2888 * amdgpu_vm_handle_fault - graceful handling of VM faults. 2889 * @adev: amdgpu device pointer 2890 * @pasid: PASID of the VM 2891 * @ts: Timestamp of the fault 2892 * @vmid: VMID, only used for GFX 9.4.3. 2893 * @node_id: Node_id received in IH cookie. Only applicable for 2894 * GFX 9.4.3. 2895 * @addr: Address of the fault 2896 * @write_fault: true is write fault, false is read fault 2897 * 2898 * Try to gracefully handle a VM fault. Return true if the fault was handled and 2899 * shouldn't be reported any more. 2900 */ 2901 bool amdgpu_vm_handle_fault(struct amdgpu_device *adev, u32 pasid, 2902 u32 vmid, u32 node_id, uint64_t addr, uint64_t ts, 2903 bool write_fault) 2904 { 2905 bool is_compute_context = false; 2906 struct amdgpu_bo *root; 2907 unsigned long irqflags; 2908 uint64_t value, flags; 2909 struct amdgpu_vm *vm; 2910 int r; 2911 2912 xa_lock_irqsave(&adev->vm_manager.pasids, irqflags); 2913 vm = xa_load(&adev->vm_manager.pasids, pasid); 2914 if (vm) { 2915 root = amdgpu_bo_ref(vm->root.bo); 2916 is_compute_context = vm->is_compute_context; 2917 } else { 2918 root = NULL; 2919 } 2920 xa_unlock_irqrestore(&adev->vm_manager.pasids, irqflags); 2921 2922 if (!root) 2923 return false; 2924 2925 addr /= AMDGPU_GPU_PAGE_SIZE; 2926 2927 if (is_compute_context && !svm_range_restore_pages(adev, pasid, vmid, 2928 node_id, addr, ts, write_fault)) { 2929 amdgpu_bo_unref(&root); 2930 return true; 2931 } 2932 2933 r = amdgpu_bo_reserve(root, true); 2934 if (r) 2935 goto error_unref; 2936 2937 /* Double check that the VM still exists */ 2938 xa_lock_irqsave(&adev->vm_manager.pasids, irqflags); 2939 vm = xa_load(&adev->vm_manager.pasids, pasid); 2940 if (vm && vm->root.bo != root) 2941 vm = NULL; 2942 xa_unlock_irqrestore(&adev->vm_manager.pasids, irqflags); 2943 if (!vm) 2944 goto error_unlock; 2945 2946 flags = AMDGPU_PTE_VALID | AMDGPU_PTE_SNOOPED | 2947 AMDGPU_PTE_SYSTEM; 2948 2949 if (is_compute_context) { 2950 /* Intentionally setting invalid PTE flag 2951 * combination to force a no-retry-fault 2952 */ 2953 flags = AMDGPU_VM_NORETRY_FLAGS; 2954 value = 0; 2955 } else if (amdgpu_vm_fault_stop == AMDGPU_VM_FAULT_STOP_NEVER) { 2956 /* Redirect the access to the dummy page */ 2957 value = adev->dummy_page_addr; 2958 flags |= AMDGPU_PTE_EXECUTABLE | AMDGPU_PTE_READABLE | 2959 AMDGPU_PTE_WRITEABLE; 2960 2961 } else { 2962 /* Let the hw retry silently on the PTE */ 2963 value = 0; 2964 } 2965 2966 r = dma_resv_reserve_fences(root->tbo.base.resv, 1); 2967 if (r) { 2968 pr_debug("failed %d to reserve fence slot\n", r); 2969 goto error_unlock; 2970 } 2971 2972 r = amdgpu_vm_update_range(adev, vm, true, false, false, false, 2973 NULL, addr, addr, flags, value, 0, NULL, NULL, NULL); 2974 if (r) 2975 goto error_unlock; 2976 2977 r = amdgpu_vm_update_pdes(adev, vm, true); 2978 2979 error_unlock: 2980 amdgpu_bo_unreserve(root); 2981 if (r < 0) 2982 dev_err(adev->dev, "Can't handle page fault (%d)\n", r); 2983 2984 error_unref: 2985 amdgpu_bo_unref(&root); 2986 2987 return false; 2988 } 2989 2990 #if defined(CONFIG_DEBUG_FS) 2991 /** 2992 * amdgpu_debugfs_vm_bo_info - print BO info for the VM 2993 * 2994 * @vm: Requested VM for printing BO info 2995 * @m: debugfs file 2996 * 2997 * Print BO information in debugfs file for the VM 2998 */ 2999 void amdgpu_debugfs_vm_bo_info(struct amdgpu_vm *vm, struct seq_file *m) 3000 { 3001 struct amdgpu_bo_va *bo_va, *tmp; 3002 u64 total_idle = 0; 3003 u64 total_evicted = 0; 3004 u64 total_relocated = 0; 3005 u64 total_moved = 0; 3006 u64 total_invalidated = 0; 3007 u64 total_done = 0; 3008 unsigned int total_idle_objs = 0; 3009 unsigned int total_evicted_objs = 0; 3010 unsigned int total_relocated_objs = 0; 3011 unsigned int total_moved_objs = 0; 3012 unsigned int total_invalidated_objs = 0; 3013 unsigned int total_done_objs = 0; 3014 unsigned int id = 0; 3015 3016 amdgpu_vm_assert_locked(vm); 3017 3018 seq_puts(m, "\tIdle BOs:\n"); 3019 list_for_each_entry_safe(bo_va, tmp, &vm->idle, base.vm_status) { 3020 if (!bo_va->base.bo) 3021 continue; 3022 total_idle += amdgpu_bo_print_info(id++, bo_va->base.bo, m); 3023 } 3024 total_idle_objs = id; 3025 id = 0; 3026 3027 seq_puts(m, "\tEvicted BOs:\n"); 3028 list_for_each_entry_safe(bo_va, tmp, &vm->evicted, base.vm_status) { 3029 if (!bo_va->base.bo) 3030 continue; 3031 total_evicted += amdgpu_bo_print_info(id++, bo_va->base.bo, m); 3032 } 3033 total_evicted_objs = id; 3034 id = 0; 3035 3036 seq_puts(m, "\tRelocated BOs:\n"); 3037 list_for_each_entry_safe(bo_va, tmp, &vm->relocated, base.vm_status) { 3038 if (!bo_va->base.bo) 3039 continue; 3040 total_relocated += amdgpu_bo_print_info(id++, bo_va->base.bo, m); 3041 } 3042 total_relocated_objs = id; 3043 id = 0; 3044 3045 seq_puts(m, "\tMoved BOs:\n"); 3046 list_for_each_entry_safe(bo_va, tmp, &vm->moved, base.vm_status) { 3047 if (!bo_va->base.bo) 3048 continue; 3049 total_moved += amdgpu_bo_print_info(id++, bo_va->base.bo, m); 3050 } 3051 total_moved_objs = id; 3052 id = 0; 3053 3054 seq_puts(m, "\tInvalidated BOs:\n"); 3055 spin_lock(&vm->invalidated_lock); 3056 list_for_each_entry_safe(bo_va, tmp, &vm->invalidated, base.vm_status) { 3057 if (!bo_va->base.bo) 3058 continue; 3059 total_invalidated += amdgpu_bo_print_info(id++, bo_va->base.bo, m); 3060 } 3061 spin_unlock(&vm->invalidated_lock); 3062 total_invalidated_objs = id; 3063 id = 0; 3064 3065 seq_puts(m, "\tDone BOs:\n"); 3066 list_for_each_entry_safe(bo_va, tmp, &vm->done, base.vm_status) { 3067 if (!bo_va->base.bo) 3068 continue; 3069 total_done += amdgpu_bo_print_info(id++, bo_va->base.bo, m); 3070 } 3071 total_done_objs = id; 3072 3073 seq_printf(m, "\tTotal idle size: %12lld\tobjs:\t%d\n", total_idle, 3074 total_idle_objs); 3075 seq_printf(m, "\tTotal evicted size: %12lld\tobjs:\t%d\n", total_evicted, 3076 total_evicted_objs); 3077 seq_printf(m, "\tTotal relocated size: %12lld\tobjs:\t%d\n", total_relocated, 3078 total_relocated_objs); 3079 seq_printf(m, "\tTotal moved size: %12lld\tobjs:\t%d\n", total_moved, 3080 total_moved_objs); 3081 seq_printf(m, "\tTotal invalidated size: %12lld\tobjs:\t%d\n", total_invalidated, 3082 total_invalidated_objs); 3083 seq_printf(m, "\tTotal done size: %12lld\tobjs:\t%d\n", total_done, 3084 total_done_objs); 3085 } 3086 #endif 3087 3088 /** 3089 * amdgpu_vm_update_fault_cache - update cached fault into. 3090 * @adev: amdgpu device pointer 3091 * @pasid: PASID of the VM 3092 * @addr: Address of the fault 3093 * @status: GPUVM fault status register 3094 * @vmhub: which vmhub got the fault 3095 * 3096 * Cache the fault info for later use by userspace in debugging. 3097 */ 3098 void amdgpu_vm_update_fault_cache(struct amdgpu_device *adev, 3099 unsigned int pasid, 3100 uint64_t addr, 3101 uint32_t status, 3102 unsigned int vmhub) 3103 { 3104 struct amdgpu_vm *vm; 3105 unsigned long flags; 3106 3107 xa_lock_irqsave(&adev->vm_manager.pasids, flags); 3108 3109 vm = xa_load(&adev->vm_manager.pasids, pasid); 3110 /* Don't update the fault cache if status is 0. In the multiple 3111 * fault case, subsequent faults will return a 0 status which is 3112 * useless for userspace and replaces the useful fault status, so 3113 * only update if status is non-0. 3114 */ 3115 if (vm && status) { 3116 vm->fault_info.addr = addr; 3117 vm->fault_info.status = status; 3118 /* 3119 * Update the fault information globally for later usage 3120 * when vm could be stale or freed. 3121 */ 3122 adev->vm_manager.fault_info.addr = addr; 3123 adev->vm_manager.fault_info.vmhub = vmhub; 3124 adev->vm_manager.fault_info.status = status; 3125 3126 if (AMDGPU_IS_GFXHUB(vmhub)) { 3127 vm->fault_info.vmhub = AMDGPU_VMHUB_TYPE_GFX; 3128 vm->fault_info.vmhub |= 3129 (vmhub - AMDGPU_GFXHUB_START) << AMDGPU_VMHUB_IDX_SHIFT; 3130 } else if (AMDGPU_IS_MMHUB0(vmhub)) { 3131 vm->fault_info.vmhub = AMDGPU_VMHUB_TYPE_MM0; 3132 vm->fault_info.vmhub |= 3133 (vmhub - AMDGPU_MMHUB0_START) << AMDGPU_VMHUB_IDX_SHIFT; 3134 } else if (AMDGPU_IS_MMHUB1(vmhub)) { 3135 vm->fault_info.vmhub = AMDGPU_VMHUB_TYPE_MM1; 3136 vm->fault_info.vmhub |= 3137 (vmhub - AMDGPU_MMHUB1_START) << AMDGPU_VMHUB_IDX_SHIFT; 3138 } else { 3139 WARN_ONCE(1, "Invalid vmhub %u\n", vmhub); 3140 } 3141 } 3142 xa_unlock_irqrestore(&adev->vm_manager.pasids, flags); 3143 } 3144 3145 /** 3146 * amdgpu_vm_is_bo_always_valid - check if the BO is VM always valid 3147 * 3148 * @vm: VM to test against. 3149 * @bo: BO to be tested. 3150 * 3151 * Returns true if the BO shares the dma_resv object with the root PD and is 3152 * always guaranteed to be valid inside the VM. 3153 */ 3154 bool amdgpu_vm_is_bo_always_valid(struct amdgpu_vm *vm, struct amdgpu_bo *bo) 3155 { 3156 return bo && bo->tbo.base.resv == vm->root.bo->tbo.base.resv; 3157 } 3158 3159 void amdgpu_vm_print_task_info(struct amdgpu_device *adev, 3160 struct amdgpu_task_info *task_info) 3161 { 3162 dev_err(adev->dev, 3163 " Process %s pid %d thread %s pid %d\n", 3164 task_info->process_name, task_info->tgid, 3165 task_info->task.comm, task_info->task.pid); 3166 } 3167