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