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