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