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