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->funcs->emit_gds_switch && gds_switch_needed) { 643 id->gds_base = job->gds_base; 644 id->gds_size = job->gds_size; 645 id->gws_base = job->gws_base; 646 id->gws_size = job->gws_size; 647 id->oa_base = job->oa_base; 648 id->oa_size = job->oa_size; 649 amdgpu_ring_emit_gds_switch(ring, job->vmid, job->gds_base, 650 job->gds_size, job->gws_base, 651 job->gws_size, job->oa_base, 652 job->oa_size); 653 } 654 655 if (ring->funcs->patch_cond_exec) 656 amdgpu_ring_patch_cond_exec(ring, patch_offset); 657 658 /* the double SWITCH_BUFFER here *cannot* be skipped by COND_EXEC */ 659 if (ring->funcs->emit_switch_buffer) { 660 amdgpu_ring_emit_switch_buffer(ring); 661 amdgpu_ring_emit_switch_buffer(ring); 662 } 663 return 0; 664 } 665 666 /** 667 * amdgpu_vm_bo_find - find the bo_va for a specific vm & bo 668 * 669 * @vm: requested vm 670 * @bo: requested buffer object 671 * 672 * Find @bo inside the requested vm. 673 * Search inside the @bos vm list for the requested vm 674 * Returns the found bo_va or NULL if none is found 675 * 676 * Object has to be reserved! 677 * 678 * Returns: 679 * Found bo_va or NULL. 680 */ 681 struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm, 682 struct amdgpu_bo *bo) 683 { 684 struct amdgpu_vm_bo_base *base; 685 686 for (base = bo->vm_bo; base; base = base->next) { 687 if (base->vm != vm) 688 continue; 689 690 return container_of(base, struct amdgpu_bo_va, base); 691 } 692 return NULL; 693 } 694 695 /** 696 * amdgpu_vm_map_gart - Resolve gart mapping of addr 697 * 698 * @pages_addr: optional DMA address to use for lookup 699 * @addr: the unmapped addr 700 * 701 * Look up the physical address of the page that the pte resolves 702 * to. 703 * 704 * Returns: 705 * The pointer for the page table entry. 706 */ 707 uint64_t amdgpu_vm_map_gart(const dma_addr_t *pages_addr, uint64_t addr) 708 { 709 uint64_t result; 710 711 /* page table offset */ 712 result = pages_addr[addr >> PAGE_SHIFT]; 713 714 /* in case cpu page size != gpu page size*/ 715 result |= addr & (~PAGE_MASK); 716 717 result &= 0xFFFFFFFFFFFFF000ULL; 718 719 return result; 720 } 721 722 /** 723 * amdgpu_vm_update_pdes - make sure that all directories are valid 724 * 725 * @adev: amdgpu_device pointer 726 * @vm: requested vm 727 * @immediate: submit immediately to the paging queue 728 * 729 * Makes sure all directories are up to date. 730 * 731 * Returns: 732 * 0 for success, error for failure. 733 */ 734 int amdgpu_vm_update_pdes(struct amdgpu_device *adev, 735 struct amdgpu_vm *vm, bool immediate) 736 { 737 struct amdgpu_vm_update_params params; 738 struct amdgpu_vm_bo_base *entry; 739 int r, idx; 740 741 if (list_empty(&vm->relocated)) 742 return 0; 743 744 if (!drm_dev_enter(adev_to_drm(adev), &idx)) 745 return -ENODEV; 746 747 memset(¶ms, 0, sizeof(params)); 748 params.adev = adev; 749 params.vm = vm; 750 params.immediate = immediate; 751 752 r = vm->update_funcs->prepare(¶ms, NULL, AMDGPU_SYNC_EXPLICIT); 753 if (r) 754 goto error; 755 756 list_for_each_entry(entry, &vm->relocated, vm_status) { 757 r = amdgpu_vm_pde_update(¶ms, entry); 758 if (r) 759 goto error; 760 } 761 762 r = vm->update_funcs->commit(¶ms, &vm->last_update); 763 if (r) 764 goto error; 765 766 while (!list_empty(&vm->relocated)) { 767 entry = list_first_entry(&vm->relocated, 768 struct amdgpu_vm_bo_base, 769 vm_status); 770 amdgpu_vm_bo_idle(entry); 771 } 772 773 error: 774 drm_dev_exit(idx); 775 return r; 776 } 777 778 /** 779 * amdgpu_vm_tlb_seq_cb - make sure to increment tlb sequence 780 * @fence: unused 781 * @cb: the callback structure 782 * 783 * Increments the tlb sequence to make sure that future CS execute a VM flush. 784 */ 785 static void amdgpu_vm_tlb_seq_cb(struct dma_fence *fence, 786 struct dma_fence_cb *cb) 787 { 788 struct amdgpu_vm_tlb_seq_cb *tlb_cb; 789 790 tlb_cb = container_of(cb, typeof(*tlb_cb), cb); 791 atomic64_inc(&tlb_cb->vm->tlb_seq); 792 kfree(tlb_cb); 793 } 794 795 /** 796 * amdgpu_vm_update_range - update a range in the vm page table 797 * 798 * @adev: amdgpu_device pointer to use for commands 799 * @vm: the VM to update the range 800 * @immediate: immediate submission in a page fault 801 * @unlocked: unlocked invalidation during MM callback 802 * @flush_tlb: trigger tlb invalidation after update completed 803 * @resv: fences we need to sync to 804 * @start: start of mapped range 805 * @last: last mapped entry 806 * @flags: flags for the entries 807 * @offset: offset into nodes and pages_addr 808 * @vram_base: base for vram mappings 809 * @res: ttm_resource to map 810 * @pages_addr: DMA addresses to use for mapping 811 * @fence: optional resulting fence 812 * 813 * Fill in the page table entries between @start and @last. 814 * 815 * Returns: 816 * 0 for success, negative erro code for failure. 817 */ 818 int amdgpu_vm_update_range(struct amdgpu_device *adev, struct amdgpu_vm *vm, 819 bool immediate, bool unlocked, bool flush_tlb, 820 struct dma_resv *resv, uint64_t start, uint64_t last, 821 uint64_t flags, uint64_t offset, uint64_t vram_base, 822 struct ttm_resource *res, dma_addr_t *pages_addr, 823 struct dma_fence **fence) 824 { 825 struct amdgpu_vm_update_params params; 826 struct amdgpu_vm_tlb_seq_cb *tlb_cb; 827 struct amdgpu_res_cursor cursor; 828 enum amdgpu_sync_mode sync_mode; 829 int r, idx; 830 831 if (!drm_dev_enter(adev_to_drm(adev), &idx)) 832 return -ENODEV; 833 834 tlb_cb = kmalloc(sizeof(*tlb_cb), GFP_KERNEL); 835 if (!tlb_cb) { 836 r = -ENOMEM; 837 goto error_unlock; 838 } 839 840 /* Vega20+XGMI where PTEs get inadvertently cached in L2 texture cache, 841 * heavy-weight flush TLB unconditionally. 842 */ 843 flush_tlb |= adev->gmc.xgmi.num_physical_nodes && 844 adev->ip_versions[GC_HWIP][0] == IP_VERSION(9, 4, 0); 845 846 memset(¶ms, 0, sizeof(params)); 847 params.adev = adev; 848 params.vm = vm; 849 params.immediate = immediate; 850 params.pages_addr = pages_addr; 851 params.unlocked = unlocked; 852 853 /* Implicitly sync to command submissions in the same VM before 854 * unmapping. Sync to moving fences before mapping. 855 */ 856 if (!(flags & AMDGPU_PTE_VALID)) 857 sync_mode = AMDGPU_SYNC_EQ_OWNER; 858 else 859 sync_mode = AMDGPU_SYNC_EXPLICIT; 860 861 amdgpu_vm_eviction_lock(vm); 862 if (vm->evicting) { 863 r = -EBUSY; 864 goto error_free; 865 } 866 867 if (!unlocked && !dma_fence_is_signaled(vm->last_unlocked)) { 868 struct dma_fence *tmp = dma_fence_get_stub(); 869 870 amdgpu_bo_fence(vm->root.bo, vm->last_unlocked, true); 871 swap(vm->last_unlocked, tmp); 872 dma_fence_put(tmp); 873 } 874 875 r = vm->update_funcs->prepare(¶ms, resv, sync_mode); 876 if (r) 877 goto error_free; 878 879 amdgpu_res_first(pages_addr ? NULL : res, offset, 880 (last - start + 1) * AMDGPU_GPU_PAGE_SIZE, &cursor); 881 while (cursor.remaining) { 882 uint64_t tmp, num_entries, addr; 883 884 num_entries = cursor.size >> AMDGPU_GPU_PAGE_SHIFT; 885 if (pages_addr) { 886 bool contiguous = true; 887 888 if (num_entries > AMDGPU_GPU_PAGES_IN_CPU_PAGE) { 889 uint64_t pfn = cursor.start >> PAGE_SHIFT; 890 uint64_t count; 891 892 contiguous = pages_addr[pfn + 1] == 893 pages_addr[pfn] + PAGE_SIZE; 894 895 tmp = num_entries / 896 AMDGPU_GPU_PAGES_IN_CPU_PAGE; 897 for (count = 2; count < tmp; ++count) { 898 uint64_t idx = pfn + count; 899 900 if (contiguous != (pages_addr[idx] == 901 pages_addr[idx - 1] + PAGE_SIZE)) 902 break; 903 } 904 num_entries = count * 905 AMDGPU_GPU_PAGES_IN_CPU_PAGE; 906 } 907 908 if (!contiguous) { 909 addr = cursor.start; 910 params.pages_addr = pages_addr; 911 } else { 912 addr = pages_addr[cursor.start >> PAGE_SHIFT]; 913 params.pages_addr = NULL; 914 } 915 916 } else if (flags & (AMDGPU_PTE_VALID | AMDGPU_PTE_PRT)) { 917 addr = vram_base + cursor.start; 918 } else { 919 addr = 0; 920 } 921 922 tmp = start + num_entries; 923 r = amdgpu_vm_ptes_update(¶ms, start, tmp, addr, flags); 924 if (r) 925 goto error_free; 926 927 amdgpu_res_next(&cursor, num_entries * AMDGPU_GPU_PAGE_SIZE); 928 start = tmp; 929 } 930 931 r = vm->update_funcs->commit(¶ms, fence); 932 933 if (flush_tlb || params.table_freed) { 934 tlb_cb->vm = vm; 935 if (!fence || !*fence || 936 dma_fence_add_callback(*fence, &tlb_cb->cb, 937 amdgpu_vm_tlb_seq_cb)) 938 amdgpu_vm_tlb_seq_cb(NULL, &tlb_cb->cb); 939 tlb_cb = NULL; 940 } 941 942 error_free: 943 kfree(tlb_cb); 944 945 error_unlock: 946 amdgpu_vm_eviction_unlock(vm); 947 drm_dev_exit(idx); 948 return r; 949 } 950 951 void amdgpu_vm_get_memory(struct amdgpu_vm *vm, uint64_t *vram_mem, 952 uint64_t *gtt_mem, uint64_t *cpu_mem) 953 { 954 struct amdgpu_bo_va *bo_va, *tmp; 955 956 list_for_each_entry_safe(bo_va, tmp, &vm->idle, 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->evicted, 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->relocated, 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->moved, 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_lock(&vm->invalidated_lock); 981 list_for_each_entry_safe(bo_va, tmp, &vm->invalidated, base.vm_status) { 982 if (!bo_va->base.bo) 983 continue; 984 amdgpu_bo_get_memory(bo_va->base.bo, vram_mem, 985 gtt_mem, cpu_mem); 986 } 987 list_for_each_entry_safe(bo_va, tmp, &vm->done, base.vm_status) { 988 if (!bo_va->base.bo) 989 continue; 990 amdgpu_bo_get_memory(bo_va->base.bo, vram_mem, 991 gtt_mem, cpu_mem); 992 } 993 spin_unlock(&vm->invalidated_lock); 994 } 995 /** 996 * amdgpu_vm_bo_update - update all BO mappings in the vm page table 997 * 998 * @adev: amdgpu_device pointer 999 * @bo_va: requested BO and VM object 1000 * @clear: if true clear the entries 1001 * 1002 * Fill in the page table entries for @bo_va. 1003 * 1004 * Returns: 1005 * 0 for success, -EINVAL for failure. 1006 */ 1007 int amdgpu_vm_bo_update(struct amdgpu_device *adev, struct amdgpu_bo_va *bo_va, 1008 bool clear) 1009 { 1010 struct amdgpu_bo *bo = bo_va->base.bo; 1011 struct amdgpu_vm *vm = bo_va->base.vm; 1012 struct amdgpu_bo_va_mapping *mapping; 1013 dma_addr_t *pages_addr = NULL; 1014 struct ttm_resource *mem; 1015 struct dma_fence **last_update; 1016 bool flush_tlb = clear; 1017 struct dma_resv *resv; 1018 uint64_t vram_base; 1019 uint64_t flags; 1020 int r; 1021 1022 if (clear || !bo) { 1023 mem = NULL; 1024 resv = vm->root.bo->tbo.base.resv; 1025 } else { 1026 struct drm_gem_object *obj = &bo->tbo.base; 1027 1028 resv = bo->tbo.base.resv; 1029 if (obj->import_attach && bo_va->is_xgmi) { 1030 struct dma_buf *dma_buf = obj->import_attach->dmabuf; 1031 struct drm_gem_object *gobj = dma_buf->priv; 1032 struct amdgpu_bo *abo = gem_to_amdgpu_bo(gobj); 1033 1034 if (abo->tbo.resource->mem_type == TTM_PL_VRAM) 1035 bo = gem_to_amdgpu_bo(gobj); 1036 } 1037 mem = bo->tbo.resource; 1038 if (mem->mem_type == TTM_PL_TT || 1039 mem->mem_type == AMDGPU_PL_PREEMPT) 1040 pages_addr = bo->tbo.ttm->dma_address; 1041 } 1042 1043 if (bo) { 1044 struct amdgpu_device *bo_adev; 1045 1046 flags = amdgpu_ttm_tt_pte_flags(adev, bo->tbo.ttm, mem); 1047 1048 if (amdgpu_bo_encrypted(bo)) 1049 flags |= AMDGPU_PTE_TMZ; 1050 1051 bo_adev = amdgpu_ttm_adev(bo->tbo.bdev); 1052 vram_base = bo_adev->vm_manager.vram_base_offset; 1053 } else { 1054 flags = 0x0; 1055 vram_base = 0; 1056 } 1057 1058 if (clear || (bo && bo->tbo.base.resv == 1059 vm->root.bo->tbo.base.resv)) 1060 last_update = &vm->last_update; 1061 else 1062 last_update = &bo_va->last_pt_update; 1063 1064 if (!clear && bo_va->base.moved) { 1065 flush_tlb = true; 1066 list_splice_init(&bo_va->valids, &bo_va->invalids); 1067 1068 } else if (bo_va->cleared != clear) { 1069 list_splice_init(&bo_va->valids, &bo_va->invalids); 1070 } 1071 1072 list_for_each_entry(mapping, &bo_va->invalids, list) { 1073 uint64_t update_flags = flags; 1074 1075 /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here 1076 * but in case of something, we filter the flags in first place 1077 */ 1078 if (!(mapping->flags & AMDGPU_PTE_READABLE)) 1079 update_flags &= ~AMDGPU_PTE_READABLE; 1080 if (!(mapping->flags & AMDGPU_PTE_WRITEABLE)) 1081 update_flags &= ~AMDGPU_PTE_WRITEABLE; 1082 1083 /* Apply ASIC specific mapping flags */ 1084 amdgpu_gmc_get_vm_pte(adev, mapping, &update_flags); 1085 1086 trace_amdgpu_vm_bo_update(mapping); 1087 1088 r = amdgpu_vm_update_range(adev, vm, false, false, flush_tlb, 1089 resv, mapping->start, mapping->last, 1090 update_flags, mapping->offset, 1091 vram_base, mem, pages_addr, 1092 last_update); 1093 if (r) 1094 return r; 1095 } 1096 1097 /* If the BO is not in its preferred location add it back to 1098 * the evicted list so that it gets validated again on the 1099 * next command submission. 1100 */ 1101 if (bo && bo->tbo.base.resv == vm->root.bo->tbo.base.resv) { 1102 uint32_t mem_type = bo->tbo.resource->mem_type; 1103 1104 if (!(bo->preferred_domains & 1105 amdgpu_mem_type_to_domain(mem_type))) 1106 amdgpu_vm_bo_evicted(&bo_va->base); 1107 else 1108 amdgpu_vm_bo_idle(&bo_va->base); 1109 } else { 1110 amdgpu_vm_bo_done(&bo_va->base); 1111 } 1112 1113 list_splice_init(&bo_va->invalids, &bo_va->valids); 1114 bo_va->cleared = clear; 1115 bo_va->base.moved = false; 1116 1117 if (trace_amdgpu_vm_bo_mapping_enabled()) { 1118 list_for_each_entry(mapping, &bo_va->valids, list) 1119 trace_amdgpu_vm_bo_mapping(mapping); 1120 } 1121 1122 return 0; 1123 } 1124 1125 /** 1126 * amdgpu_vm_update_prt_state - update the global PRT state 1127 * 1128 * @adev: amdgpu_device pointer 1129 */ 1130 static void amdgpu_vm_update_prt_state(struct amdgpu_device *adev) 1131 { 1132 unsigned long flags; 1133 bool enable; 1134 1135 spin_lock_irqsave(&adev->vm_manager.prt_lock, flags); 1136 enable = !!atomic_read(&adev->vm_manager.num_prt_users); 1137 adev->gmc.gmc_funcs->set_prt(adev, enable); 1138 spin_unlock_irqrestore(&adev->vm_manager.prt_lock, flags); 1139 } 1140 1141 /** 1142 * amdgpu_vm_prt_get - add a PRT user 1143 * 1144 * @adev: amdgpu_device pointer 1145 */ 1146 static void amdgpu_vm_prt_get(struct amdgpu_device *adev) 1147 { 1148 if (!adev->gmc.gmc_funcs->set_prt) 1149 return; 1150 1151 if (atomic_inc_return(&adev->vm_manager.num_prt_users) == 1) 1152 amdgpu_vm_update_prt_state(adev); 1153 } 1154 1155 /** 1156 * amdgpu_vm_prt_put - drop a PRT user 1157 * 1158 * @adev: amdgpu_device pointer 1159 */ 1160 static void amdgpu_vm_prt_put(struct amdgpu_device *adev) 1161 { 1162 if (atomic_dec_return(&adev->vm_manager.num_prt_users) == 0) 1163 amdgpu_vm_update_prt_state(adev); 1164 } 1165 1166 /** 1167 * amdgpu_vm_prt_cb - callback for updating the PRT status 1168 * 1169 * @fence: fence for the callback 1170 * @_cb: the callback function 1171 */ 1172 static void amdgpu_vm_prt_cb(struct dma_fence *fence, struct dma_fence_cb *_cb) 1173 { 1174 struct amdgpu_prt_cb *cb = container_of(_cb, struct amdgpu_prt_cb, cb); 1175 1176 amdgpu_vm_prt_put(cb->adev); 1177 kfree(cb); 1178 } 1179 1180 /** 1181 * amdgpu_vm_add_prt_cb - add callback for updating the PRT status 1182 * 1183 * @adev: amdgpu_device pointer 1184 * @fence: fence for the callback 1185 */ 1186 static void amdgpu_vm_add_prt_cb(struct amdgpu_device *adev, 1187 struct dma_fence *fence) 1188 { 1189 struct amdgpu_prt_cb *cb; 1190 1191 if (!adev->gmc.gmc_funcs->set_prt) 1192 return; 1193 1194 cb = kmalloc(sizeof(struct amdgpu_prt_cb), GFP_KERNEL); 1195 if (!cb) { 1196 /* Last resort when we are OOM */ 1197 if (fence) 1198 dma_fence_wait(fence, false); 1199 1200 amdgpu_vm_prt_put(adev); 1201 } else { 1202 cb->adev = adev; 1203 if (!fence || dma_fence_add_callback(fence, &cb->cb, 1204 amdgpu_vm_prt_cb)) 1205 amdgpu_vm_prt_cb(fence, &cb->cb); 1206 } 1207 } 1208 1209 /** 1210 * amdgpu_vm_free_mapping - free a mapping 1211 * 1212 * @adev: amdgpu_device pointer 1213 * @vm: requested vm 1214 * @mapping: mapping to be freed 1215 * @fence: fence of the unmap operation 1216 * 1217 * Free a mapping and make sure we decrease the PRT usage count if applicable. 1218 */ 1219 static void amdgpu_vm_free_mapping(struct amdgpu_device *adev, 1220 struct amdgpu_vm *vm, 1221 struct amdgpu_bo_va_mapping *mapping, 1222 struct dma_fence *fence) 1223 { 1224 if (mapping->flags & AMDGPU_PTE_PRT) 1225 amdgpu_vm_add_prt_cb(adev, fence); 1226 kfree(mapping); 1227 } 1228 1229 /** 1230 * amdgpu_vm_prt_fini - finish all prt mappings 1231 * 1232 * @adev: amdgpu_device pointer 1233 * @vm: requested vm 1234 * 1235 * Register a cleanup callback to disable PRT support after VM dies. 1236 */ 1237 static void amdgpu_vm_prt_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm) 1238 { 1239 struct dma_resv *resv = vm->root.bo->tbo.base.resv; 1240 struct dma_resv_iter cursor; 1241 struct dma_fence *fence; 1242 1243 dma_resv_for_each_fence(&cursor, resv, true, fence) { 1244 /* Add a callback for each fence in the reservation object */ 1245 amdgpu_vm_prt_get(adev); 1246 amdgpu_vm_add_prt_cb(adev, fence); 1247 } 1248 } 1249 1250 /** 1251 * amdgpu_vm_clear_freed - clear freed BOs in the PT 1252 * 1253 * @adev: amdgpu_device pointer 1254 * @vm: requested vm 1255 * @fence: optional resulting fence (unchanged if no work needed to be done 1256 * or if an error occurred) 1257 * 1258 * Make sure all freed BOs are cleared in the PT. 1259 * PTs have to be reserved and mutex must be locked! 1260 * 1261 * Returns: 1262 * 0 for success. 1263 * 1264 */ 1265 int amdgpu_vm_clear_freed(struct amdgpu_device *adev, 1266 struct amdgpu_vm *vm, 1267 struct dma_fence **fence) 1268 { 1269 struct dma_resv *resv = vm->root.bo->tbo.base.resv; 1270 struct amdgpu_bo_va_mapping *mapping; 1271 uint64_t init_pte_value = 0; 1272 struct dma_fence *f = NULL; 1273 int r; 1274 1275 while (!list_empty(&vm->freed)) { 1276 mapping = list_first_entry(&vm->freed, 1277 struct amdgpu_bo_va_mapping, list); 1278 list_del(&mapping->list); 1279 1280 if (vm->pte_support_ats && 1281 mapping->start < AMDGPU_GMC_HOLE_START) 1282 init_pte_value = AMDGPU_PTE_DEFAULT_ATC; 1283 1284 r = amdgpu_vm_update_range(adev, vm, false, false, true, resv, 1285 mapping->start, mapping->last, 1286 init_pte_value, 0, 0, NULL, NULL, 1287 &f); 1288 amdgpu_vm_free_mapping(adev, vm, mapping, f); 1289 if (r) { 1290 dma_fence_put(f); 1291 return r; 1292 } 1293 } 1294 1295 if (fence && f) { 1296 dma_fence_put(*fence); 1297 *fence = f; 1298 } else { 1299 dma_fence_put(f); 1300 } 1301 1302 return 0; 1303 1304 } 1305 1306 /** 1307 * amdgpu_vm_handle_moved - handle moved BOs in the PT 1308 * 1309 * @adev: amdgpu_device pointer 1310 * @vm: requested vm 1311 * 1312 * Make sure all BOs which are moved are updated in the PTs. 1313 * 1314 * Returns: 1315 * 0 for success. 1316 * 1317 * PTs have to be reserved! 1318 */ 1319 int amdgpu_vm_handle_moved(struct amdgpu_device *adev, 1320 struct amdgpu_vm *vm) 1321 { 1322 struct amdgpu_bo_va *bo_va, *tmp; 1323 struct dma_resv *resv; 1324 bool clear; 1325 int r; 1326 1327 list_for_each_entry_safe(bo_va, tmp, &vm->moved, base.vm_status) { 1328 /* Per VM BOs never need to bo cleared in the page tables */ 1329 r = amdgpu_vm_bo_update(adev, bo_va, false); 1330 if (r) 1331 return r; 1332 } 1333 1334 spin_lock(&vm->invalidated_lock); 1335 while (!list_empty(&vm->invalidated)) { 1336 bo_va = list_first_entry(&vm->invalidated, struct amdgpu_bo_va, 1337 base.vm_status); 1338 resv = bo_va->base.bo->tbo.base.resv; 1339 spin_unlock(&vm->invalidated_lock); 1340 1341 /* Try to reserve the BO to avoid clearing its ptes */ 1342 if (!amdgpu_vm_debug && dma_resv_trylock(resv)) 1343 clear = false; 1344 /* Somebody else is using the BO right now */ 1345 else 1346 clear = true; 1347 1348 r = amdgpu_vm_bo_update(adev, bo_va, clear); 1349 if (r) 1350 return r; 1351 1352 if (!clear) 1353 dma_resv_unlock(resv); 1354 spin_lock(&vm->invalidated_lock); 1355 } 1356 spin_unlock(&vm->invalidated_lock); 1357 1358 return 0; 1359 } 1360 1361 /** 1362 * amdgpu_vm_bo_add - add a bo to a specific vm 1363 * 1364 * @adev: amdgpu_device pointer 1365 * @vm: requested vm 1366 * @bo: amdgpu buffer object 1367 * 1368 * Add @bo into the requested vm. 1369 * Add @bo to the list of bos associated with the vm 1370 * 1371 * Returns: 1372 * Newly added bo_va or NULL for failure 1373 * 1374 * Object has to be reserved! 1375 */ 1376 struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev, 1377 struct amdgpu_vm *vm, 1378 struct amdgpu_bo *bo) 1379 { 1380 struct amdgpu_bo_va *bo_va; 1381 1382 bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL); 1383 if (bo_va == NULL) { 1384 return NULL; 1385 } 1386 amdgpu_vm_bo_base_init(&bo_va->base, vm, bo); 1387 1388 bo_va->ref_count = 1; 1389 INIT_LIST_HEAD(&bo_va->valids); 1390 INIT_LIST_HEAD(&bo_va->invalids); 1391 1392 if (!bo) 1393 return bo_va; 1394 1395 dma_resv_assert_held(bo->tbo.base.resv); 1396 if (amdgpu_dmabuf_is_xgmi_accessible(adev, bo)) { 1397 bo_va->is_xgmi = true; 1398 /* Power up XGMI if it can be potentially used */ 1399 amdgpu_xgmi_set_pstate(adev, AMDGPU_XGMI_PSTATE_MAX_VEGA20); 1400 } 1401 1402 return bo_va; 1403 } 1404 1405 1406 /** 1407 * amdgpu_vm_bo_insert_map - insert a new mapping 1408 * 1409 * @adev: amdgpu_device pointer 1410 * @bo_va: bo_va to store the address 1411 * @mapping: the mapping to insert 1412 * 1413 * Insert a new mapping into all structures. 1414 */ 1415 static void amdgpu_vm_bo_insert_map(struct amdgpu_device *adev, 1416 struct amdgpu_bo_va *bo_va, 1417 struct amdgpu_bo_va_mapping *mapping) 1418 { 1419 struct amdgpu_vm *vm = bo_va->base.vm; 1420 struct amdgpu_bo *bo = bo_va->base.bo; 1421 1422 mapping->bo_va = bo_va; 1423 list_add(&mapping->list, &bo_va->invalids); 1424 amdgpu_vm_it_insert(mapping, &vm->va); 1425 1426 if (mapping->flags & AMDGPU_PTE_PRT) 1427 amdgpu_vm_prt_get(adev); 1428 1429 if (bo && bo->tbo.base.resv == vm->root.bo->tbo.base.resv && 1430 !bo_va->base.moved) { 1431 list_move(&bo_va->base.vm_status, &vm->moved); 1432 } 1433 trace_amdgpu_vm_bo_map(bo_va, mapping); 1434 } 1435 1436 /** 1437 * amdgpu_vm_bo_map - map bo inside a vm 1438 * 1439 * @adev: amdgpu_device pointer 1440 * @bo_va: bo_va to store the address 1441 * @saddr: where to map the BO 1442 * @offset: requested offset in the BO 1443 * @size: BO size in bytes 1444 * @flags: attributes of pages (read/write/valid/etc.) 1445 * 1446 * Add a mapping of the BO at the specefied addr into the VM. 1447 * 1448 * Returns: 1449 * 0 for success, error for failure. 1450 * 1451 * Object has to be reserved and unreserved outside! 1452 */ 1453 int amdgpu_vm_bo_map(struct amdgpu_device *adev, 1454 struct amdgpu_bo_va *bo_va, 1455 uint64_t saddr, uint64_t offset, 1456 uint64_t size, uint64_t flags) 1457 { 1458 struct amdgpu_bo_va_mapping *mapping, *tmp; 1459 struct amdgpu_bo *bo = bo_va->base.bo; 1460 struct amdgpu_vm *vm = bo_va->base.vm; 1461 uint64_t eaddr; 1462 1463 /* validate the parameters */ 1464 if (saddr & ~PAGE_MASK || offset & ~PAGE_MASK || 1465 size == 0 || size & ~PAGE_MASK) 1466 return -EINVAL; 1467 1468 /* make sure object fit at this offset */ 1469 eaddr = saddr + size - 1; 1470 if (saddr >= eaddr || 1471 (bo && offset + size > amdgpu_bo_size(bo)) || 1472 (eaddr >= adev->vm_manager.max_pfn << AMDGPU_GPU_PAGE_SHIFT)) 1473 return -EINVAL; 1474 1475 saddr /= AMDGPU_GPU_PAGE_SIZE; 1476 eaddr /= AMDGPU_GPU_PAGE_SIZE; 1477 1478 tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr); 1479 if (tmp) { 1480 /* bo and tmp overlap, invalid addr */ 1481 dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with " 1482 "0x%010Lx-0x%010Lx\n", bo, saddr, eaddr, 1483 tmp->start, tmp->last + 1); 1484 return -EINVAL; 1485 } 1486 1487 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL); 1488 if (!mapping) 1489 return -ENOMEM; 1490 1491 mapping->start = saddr; 1492 mapping->last = eaddr; 1493 mapping->offset = offset; 1494 mapping->flags = flags; 1495 1496 amdgpu_vm_bo_insert_map(adev, bo_va, mapping); 1497 1498 return 0; 1499 } 1500 1501 /** 1502 * amdgpu_vm_bo_replace_map - map bo inside a vm, replacing existing mappings 1503 * 1504 * @adev: amdgpu_device pointer 1505 * @bo_va: bo_va to store the address 1506 * @saddr: where to map the BO 1507 * @offset: requested offset in the BO 1508 * @size: BO size in bytes 1509 * @flags: attributes of pages (read/write/valid/etc.) 1510 * 1511 * Add a mapping of the BO at the specefied addr into the VM. Replace existing 1512 * mappings as we do so. 1513 * 1514 * Returns: 1515 * 0 for success, error for failure. 1516 * 1517 * Object has to be reserved and unreserved outside! 1518 */ 1519 int amdgpu_vm_bo_replace_map(struct amdgpu_device *adev, 1520 struct amdgpu_bo_va *bo_va, 1521 uint64_t saddr, uint64_t offset, 1522 uint64_t size, uint64_t flags) 1523 { 1524 struct amdgpu_bo_va_mapping *mapping; 1525 struct amdgpu_bo *bo = bo_va->base.bo; 1526 uint64_t eaddr; 1527 int r; 1528 1529 /* validate the parameters */ 1530 if (saddr & ~PAGE_MASK || offset & ~PAGE_MASK || 1531 size == 0 || size & ~PAGE_MASK) 1532 return -EINVAL; 1533 1534 /* make sure object fit at this offset */ 1535 eaddr = saddr + size - 1; 1536 if (saddr >= eaddr || 1537 (bo && offset + size > amdgpu_bo_size(bo)) || 1538 (eaddr >= adev->vm_manager.max_pfn << AMDGPU_GPU_PAGE_SHIFT)) 1539 return -EINVAL; 1540 1541 /* Allocate all the needed memory */ 1542 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL); 1543 if (!mapping) 1544 return -ENOMEM; 1545 1546 r = amdgpu_vm_bo_clear_mappings(adev, bo_va->base.vm, saddr, size); 1547 if (r) { 1548 kfree(mapping); 1549 return r; 1550 } 1551 1552 saddr /= AMDGPU_GPU_PAGE_SIZE; 1553 eaddr /= AMDGPU_GPU_PAGE_SIZE; 1554 1555 mapping->start = saddr; 1556 mapping->last = eaddr; 1557 mapping->offset = offset; 1558 mapping->flags = flags; 1559 1560 amdgpu_vm_bo_insert_map(adev, bo_va, mapping); 1561 1562 return 0; 1563 } 1564 1565 /** 1566 * amdgpu_vm_bo_unmap - remove bo mapping from vm 1567 * 1568 * @adev: amdgpu_device pointer 1569 * @bo_va: bo_va to remove the address from 1570 * @saddr: where to the BO is mapped 1571 * 1572 * Remove a mapping of the BO at the specefied addr from the VM. 1573 * 1574 * Returns: 1575 * 0 for success, error for failure. 1576 * 1577 * Object has to be reserved and unreserved outside! 1578 */ 1579 int amdgpu_vm_bo_unmap(struct amdgpu_device *adev, 1580 struct amdgpu_bo_va *bo_va, 1581 uint64_t saddr) 1582 { 1583 struct amdgpu_bo_va_mapping *mapping; 1584 struct amdgpu_vm *vm = bo_va->base.vm; 1585 bool valid = true; 1586 1587 saddr /= AMDGPU_GPU_PAGE_SIZE; 1588 1589 list_for_each_entry(mapping, &bo_va->valids, list) { 1590 if (mapping->start == saddr) 1591 break; 1592 } 1593 1594 if (&mapping->list == &bo_va->valids) { 1595 valid = false; 1596 1597 list_for_each_entry(mapping, &bo_va->invalids, list) { 1598 if (mapping->start == saddr) 1599 break; 1600 } 1601 1602 if (&mapping->list == &bo_va->invalids) 1603 return -ENOENT; 1604 } 1605 1606 list_del(&mapping->list); 1607 amdgpu_vm_it_remove(mapping, &vm->va); 1608 mapping->bo_va = NULL; 1609 trace_amdgpu_vm_bo_unmap(bo_va, mapping); 1610 1611 if (valid) 1612 list_add(&mapping->list, &vm->freed); 1613 else 1614 amdgpu_vm_free_mapping(adev, vm, mapping, 1615 bo_va->last_pt_update); 1616 1617 return 0; 1618 } 1619 1620 /** 1621 * amdgpu_vm_bo_clear_mappings - remove all mappings in a specific range 1622 * 1623 * @adev: amdgpu_device pointer 1624 * @vm: VM structure to use 1625 * @saddr: start of the range 1626 * @size: size of the range 1627 * 1628 * Remove all mappings in a range, split them as appropriate. 1629 * 1630 * Returns: 1631 * 0 for success, error for failure. 1632 */ 1633 int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev, 1634 struct amdgpu_vm *vm, 1635 uint64_t saddr, uint64_t size) 1636 { 1637 struct amdgpu_bo_va_mapping *before, *after, *tmp, *next; 1638 LIST_HEAD(removed); 1639 uint64_t eaddr; 1640 1641 eaddr = saddr + size - 1; 1642 saddr /= AMDGPU_GPU_PAGE_SIZE; 1643 eaddr /= AMDGPU_GPU_PAGE_SIZE; 1644 1645 /* Allocate all the needed memory */ 1646 before = kzalloc(sizeof(*before), GFP_KERNEL); 1647 if (!before) 1648 return -ENOMEM; 1649 INIT_LIST_HEAD(&before->list); 1650 1651 after = kzalloc(sizeof(*after), GFP_KERNEL); 1652 if (!after) { 1653 kfree(before); 1654 return -ENOMEM; 1655 } 1656 INIT_LIST_HEAD(&after->list); 1657 1658 /* Now gather all removed mappings */ 1659 tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr); 1660 while (tmp) { 1661 /* Remember mapping split at the start */ 1662 if (tmp->start < saddr) { 1663 before->start = tmp->start; 1664 before->last = saddr - 1; 1665 before->offset = tmp->offset; 1666 before->flags = tmp->flags; 1667 before->bo_va = tmp->bo_va; 1668 list_add(&before->list, &tmp->bo_va->invalids); 1669 } 1670 1671 /* Remember mapping split at the end */ 1672 if (tmp->last > eaddr) { 1673 after->start = eaddr + 1; 1674 after->last = tmp->last; 1675 after->offset = tmp->offset; 1676 after->offset += (after->start - tmp->start) << PAGE_SHIFT; 1677 after->flags = tmp->flags; 1678 after->bo_va = tmp->bo_va; 1679 list_add(&after->list, &tmp->bo_va->invalids); 1680 } 1681 1682 list_del(&tmp->list); 1683 list_add(&tmp->list, &removed); 1684 1685 tmp = amdgpu_vm_it_iter_next(tmp, saddr, eaddr); 1686 } 1687 1688 /* And free them up */ 1689 list_for_each_entry_safe(tmp, next, &removed, list) { 1690 amdgpu_vm_it_remove(tmp, &vm->va); 1691 list_del(&tmp->list); 1692 1693 if (tmp->start < saddr) 1694 tmp->start = saddr; 1695 if (tmp->last > eaddr) 1696 tmp->last = eaddr; 1697 1698 tmp->bo_va = NULL; 1699 list_add(&tmp->list, &vm->freed); 1700 trace_amdgpu_vm_bo_unmap(NULL, tmp); 1701 } 1702 1703 /* Insert partial mapping before the range */ 1704 if (!list_empty(&before->list)) { 1705 amdgpu_vm_it_insert(before, &vm->va); 1706 if (before->flags & AMDGPU_PTE_PRT) 1707 amdgpu_vm_prt_get(adev); 1708 } else { 1709 kfree(before); 1710 } 1711 1712 /* Insert partial mapping after the range */ 1713 if (!list_empty(&after->list)) { 1714 amdgpu_vm_it_insert(after, &vm->va); 1715 if (after->flags & AMDGPU_PTE_PRT) 1716 amdgpu_vm_prt_get(adev); 1717 } else { 1718 kfree(after); 1719 } 1720 1721 return 0; 1722 } 1723 1724 /** 1725 * amdgpu_vm_bo_lookup_mapping - find mapping by address 1726 * 1727 * @vm: the requested VM 1728 * @addr: the address 1729 * 1730 * Find a mapping by it's address. 1731 * 1732 * Returns: 1733 * The amdgpu_bo_va_mapping matching for addr or NULL 1734 * 1735 */ 1736 struct amdgpu_bo_va_mapping *amdgpu_vm_bo_lookup_mapping(struct amdgpu_vm *vm, 1737 uint64_t addr) 1738 { 1739 return amdgpu_vm_it_iter_first(&vm->va, addr, addr); 1740 } 1741 1742 /** 1743 * amdgpu_vm_bo_trace_cs - trace all reserved mappings 1744 * 1745 * @vm: the requested vm 1746 * @ticket: CS ticket 1747 * 1748 * Trace all mappings of BOs reserved during a command submission. 1749 */ 1750 void amdgpu_vm_bo_trace_cs(struct amdgpu_vm *vm, struct ww_acquire_ctx *ticket) 1751 { 1752 struct amdgpu_bo_va_mapping *mapping; 1753 1754 if (!trace_amdgpu_vm_bo_cs_enabled()) 1755 return; 1756 1757 for (mapping = amdgpu_vm_it_iter_first(&vm->va, 0, U64_MAX); mapping; 1758 mapping = amdgpu_vm_it_iter_next(mapping, 0, U64_MAX)) { 1759 if (mapping->bo_va && mapping->bo_va->base.bo) { 1760 struct amdgpu_bo *bo; 1761 1762 bo = mapping->bo_va->base.bo; 1763 if (dma_resv_locking_ctx(bo->tbo.base.resv) != 1764 ticket) 1765 continue; 1766 } 1767 1768 trace_amdgpu_vm_bo_cs(mapping); 1769 } 1770 } 1771 1772 /** 1773 * amdgpu_vm_bo_del - remove a bo from a specific vm 1774 * 1775 * @adev: amdgpu_device pointer 1776 * @bo_va: requested bo_va 1777 * 1778 * Remove @bo_va->bo from the requested vm. 1779 * 1780 * Object have to be reserved! 1781 */ 1782 void amdgpu_vm_bo_del(struct amdgpu_device *adev, 1783 struct amdgpu_bo_va *bo_va) 1784 { 1785 struct amdgpu_bo_va_mapping *mapping, *next; 1786 struct amdgpu_bo *bo = bo_va->base.bo; 1787 struct amdgpu_vm *vm = bo_va->base.vm; 1788 struct amdgpu_vm_bo_base **base; 1789 1790 dma_resv_assert_held(vm->root.bo->tbo.base.resv); 1791 1792 if (bo) { 1793 dma_resv_assert_held(bo->tbo.base.resv); 1794 if (bo->tbo.base.resv == vm->root.bo->tbo.base.resv) 1795 vm->bulk_moveable = false; 1796 1797 for (base = &bo_va->base.bo->vm_bo; *base; 1798 base = &(*base)->next) { 1799 if (*base != &bo_va->base) 1800 continue; 1801 1802 *base = bo_va->base.next; 1803 break; 1804 } 1805 } 1806 1807 spin_lock(&vm->invalidated_lock); 1808 list_del(&bo_va->base.vm_status); 1809 spin_unlock(&vm->invalidated_lock); 1810 1811 list_for_each_entry_safe(mapping, next, &bo_va->valids, list) { 1812 list_del(&mapping->list); 1813 amdgpu_vm_it_remove(mapping, &vm->va); 1814 mapping->bo_va = NULL; 1815 trace_amdgpu_vm_bo_unmap(bo_va, mapping); 1816 list_add(&mapping->list, &vm->freed); 1817 } 1818 list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) { 1819 list_del(&mapping->list); 1820 amdgpu_vm_it_remove(mapping, &vm->va); 1821 amdgpu_vm_free_mapping(adev, vm, mapping, 1822 bo_va->last_pt_update); 1823 } 1824 1825 dma_fence_put(bo_va->last_pt_update); 1826 1827 if (bo && bo_va->is_xgmi) 1828 amdgpu_xgmi_set_pstate(adev, AMDGPU_XGMI_PSTATE_MIN); 1829 1830 kfree(bo_va); 1831 } 1832 1833 /** 1834 * amdgpu_vm_evictable - check if we can evict a VM 1835 * 1836 * @bo: A page table of the VM. 1837 * 1838 * Check if it is possible to evict a VM. 1839 */ 1840 bool amdgpu_vm_evictable(struct amdgpu_bo *bo) 1841 { 1842 struct amdgpu_vm_bo_base *bo_base = bo->vm_bo; 1843 1844 /* Page tables of a destroyed VM can go away immediately */ 1845 if (!bo_base || !bo_base->vm) 1846 return true; 1847 1848 /* Don't evict VM page tables while they are busy */ 1849 if (!dma_resv_test_signaled(bo->tbo.base.resv, true)) 1850 return false; 1851 1852 /* Try to block ongoing updates */ 1853 if (!amdgpu_vm_eviction_trylock(bo_base->vm)) 1854 return false; 1855 1856 /* Don't evict VM page tables while they are updated */ 1857 if (!dma_fence_is_signaled(bo_base->vm->last_unlocked)) { 1858 amdgpu_vm_eviction_unlock(bo_base->vm); 1859 return false; 1860 } 1861 1862 bo_base->vm->evicting = true; 1863 amdgpu_vm_eviction_unlock(bo_base->vm); 1864 return true; 1865 } 1866 1867 /** 1868 * amdgpu_vm_bo_invalidate - mark the bo as invalid 1869 * 1870 * @adev: amdgpu_device pointer 1871 * @bo: amdgpu buffer object 1872 * @evicted: is the BO evicted 1873 * 1874 * Mark @bo as invalid. 1875 */ 1876 void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev, 1877 struct amdgpu_bo *bo, bool evicted) 1878 { 1879 struct amdgpu_vm_bo_base *bo_base; 1880 1881 /* shadow bo doesn't have bo base, its validation needs its parent */ 1882 if (bo->parent && (amdgpu_bo_shadowed(bo->parent) == bo)) 1883 bo = bo->parent; 1884 1885 for (bo_base = bo->vm_bo; bo_base; bo_base = bo_base->next) { 1886 struct amdgpu_vm *vm = bo_base->vm; 1887 1888 if (evicted && bo->tbo.base.resv == vm->root.bo->tbo.base.resv) { 1889 amdgpu_vm_bo_evicted(bo_base); 1890 continue; 1891 } 1892 1893 if (bo_base->moved) 1894 continue; 1895 bo_base->moved = true; 1896 1897 if (bo->tbo.type == ttm_bo_type_kernel) 1898 amdgpu_vm_bo_relocated(bo_base); 1899 else if (bo->tbo.base.resv == vm->root.bo->tbo.base.resv) 1900 amdgpu_vm_bo_moved(bo_base); 1901 else 1902 amdgpu_vm_bo_invalidated(bo_base); 1903 } 1904 } 1905 1906 /** 1907 * amdgpu_vm_get_block_size - calculate VM page table size as power of two 1908 * 1909 * @vm_size: VM size 1910 * 1911 * Returns: 1912 * VM page table as power of two 1913 */ 1914 static uint32_t amdgpu_vm_get_block_size(uint64_t vm_size) 1915 { 1916 /* Total bits covered by PD + PTs */ 1917 unsigned bits = ilog2(vm_size) + 18; 1918 1919 /* Make sure the PD is 4K in size up to 8GB address space. 1920 Above that split equal between PD and PTs */ 1921 if (vm_size <= 8) 1922 return (bits - 9); 1923 else 1924 return ((bits + 3) / 2); 1925 } 1926 1927 /** 1928 * amdgpu_vm_adjust_size - adjust vm size, block size and fragment size 1929 * 1930 * @adev: amdgpu_device pointer 1931 * @min_vm_size: the minimum vm size in GB if it's set auto 1932 * @fragment_size_default: Default PTE fragment size 1933 * @max_level: max VMPT level 1934 * @max_bits: max address space size in bits 1935 * 1936 */ 1937 void amdgpu_vm_adjust_size(struct amdgpu_device *adev, uint32_t min_vm_size, 1938 uint32_t fragment_size_default, unsigned max_level, 1939 unsigned max_bits) 1940 { 1941 unsigned int max_size = 1 << (max_bits - 30); 1942 unsigned int vm_size; 1943 uint64_t tmp; 1944 1945 /* adjust vm size first */ 1946 if (amdgpu_vm_size != -1) { 1947 vm_size = amdgpu_vm_size; 1948 if (vm_size > max_size) { 1949 dev_warn(adev->dev, "VM size (%d) too large, max is %u GB\n", 1950 amdgpu_vm_size, max_size); 1951 vm_size = max_size; 1952 } 1953 } else { 1954 struct sysinfo si; 1955 unsigned int phys_ram_gb; 1956 1957 /* Optimal VM size depends on the amount of physical 1958 * RAM available. Underlying requirements and 1959 * assumptions: 1960 * 1961 * - Need to map system memory and VRAM from all GPUs 1962 * - VRAM from other GPUs not known here 1963 * - Assume VRAM <= system memory 1964 * - On GFX8 and older, VM space can be segmented for 1965 * different MTYPEs 1966 * - Need to allow room for fragmentation, guard pages etc. 1967 * 1968 * This adds up to a rough guess of system memory x3. 1969 * Round up to power of two to maximize the available 1970 * VM size with the given page table size. 1971 */ 1972 si_meminfo(&si); 1973 phys_ram_gb = ((uint64_t)si.totalram * si.mem_unit + 1974 (1 << 30) - 1) >> 30; 1975 vm_size = roundup_pow_of_two( 1976 min(max(phys_ram_gb * 3, min_vm_size), max_size)); 1977 } 1978 1979 adev->vm_manager.max_pfn = (uint64_t)vm_size << 18; 1980 1981 tmp = roundup_pow_of_two(adev->vm_manager.max_pfn); 1982 if (amdgpu_vm_block_size != -1) 1983 tmp >>= amdgpu_vm_block_size - 9; 1984 tmp = DIV_ROUND_UP(fls64(tmp) - 1, 9) - 1; 1985 adev->vm_manager.num_level = min(max_level, (unsigned)tmp); 1986 switch (adev->vm_manager.num_level) { 1987 case 3: 1988 adev->vm_manager.root_level = AMDGPU_VM_PDB2; 1989 break; 1990 case 2: 1991 adev->vm_manager.root_level = AMDGPU_VM_PDB1; 1992 break; 1993 case 1: 1994 adev->vm_manager.root_level = AMDGPU_VM_PDB0; 1995 break; 1996 default: 1997 dev_err(adev->dev, "VMPT only supports 2~4+1 levels\n"); 1998 } 1999 /* block size depends on vm size and hw setup*/ 2000 if (amdgpu_vm_block_size != -1) 2001 adev->vm_manager.block_size = 2002 min((unsigned)amdgpu_vm_block_size, max_bits 2003 - AMDGPU_GPU_PAGE_SHIFT 2004 - 9 * adev->vm_manager.num_level); 2005 else if (adev->vm_manager.num_level > 1) 2006 adev->vm_manager.block_size = 9; 2007 else 2008 adev->vm_manager.block_size = amdgpu_vm_get_block_size(tmp); 2009 2010 if (amdgpu_vm_fragment_size == -1) 2011 adev->vm_manager.fragment_size = fragment_size_default; 2012 else 2013 adev->vm_manager.fragment_size = amdgpu_vm_fragment_size; 2014 2015 DRM_INFO("vm size is %u GB, %u levels, block size is %u-bit, fragment size is %u-bit\n", 2016 vm_size, adev->vm_manager.num_level + 1, 2017 adev->vm_manager.block_size, 2018 adev->vm_manager.fragment_size); 2019 } 2020 2021 /** 2022 * amdgpu_vm_wait_idle - wait for the VM to become idle 2023 * 2024 * @vm: VM object to wait for 2025 * @timeout: timeout to wait for VM to become idle 2026 */ 2027 long amdgpu_vm_wait_idle(struct amdgpu_vm *vm, long timeout) 2028 { 2029 timeout = dma_resv_wait_timeout(vm->root.bo->tbo.base.resv, true, 2030 true, timeout); 2031 if (timeout <= 0) 2032 return timeout; 2033 2034 return dma_fence_wait_timeout(vm->last_unlocked, true, timeout); 2035 } 2036 2037 /** 2038 * amdgpu_vm_init - initialize a vm instance 2039 * 2040 * @adev: amdgpu_device pointer 2041 * @vm: requested vm 2042 * 2043 * Init @vm fields. 2044 * 2045 * Returns: 2046 * 0 for success, error for failure. 2047 */ 2048 int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm) 2049 { 2050 struct amdgpu_bo *root_bo; 2051 struct amdgpu_bo_vm *root; 2052 int r, i; 2053 2054 vm->va = RB_ROOT_CACHED; 2055 for (i = 0; i < AMDGPU_MAX_VMHUBS; i++) 2056 vm->reserved_vmid[i] = NULL; 2057 INIT_LIST_HEAD(&vm->evicted); 2058 INIT_LIST_HEAD(&vm->relocated); 2059 INIT_LIST_HEAD(&vm->moved); 2060 INIT_LIST_HEAD(&vm->idle); 2061 INIT_LIST_HEAD(&vm->invalidated); 2062 spin_lock_init(&vm->invalidated_lock); 2063 INIT_LIST_HEAD(&vm->freed); 2064 INIT_LIST_HEAD(&vm->done); 2065 2066 /* create scheduler entities for page table updates */ 2067 r = drm_sched_entity_init(&vm->immediate, DRM_SCHED_PRIORITY_NORMAL, 2068 adev->vm_manager.vm_pte_scheds, 2069 adev->vm_manager.vm_pte_num_scheds, NULL); 2070 if (r) 2071 return r; 2072 2073 r = drm_sched_entity_init(&vm->delayed, DRM_SCHED_PRIORITY_NORMAL, 2074 adev->vm_manager.vm_pte_scheds, 2075 adev->vm_manager.vm_pte_num_scheds, NULL); 2076 if (r) 2077 goto error_free_immediate; 2078 2079 vm->pte_support_ats = false; 2080 vm->is_compute_context = false; 2081 2082 vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode & 2083 AMDGPU_VM_USE_CPU_FOR_GFX); 2084 2085 DRM_DEBUG_DRIVER("VM update mode is %s\n", 2086 vm->use_cpu_for_update ? "CPU" : "SDMA"); 2087 WARN_ONCE((vm->use_cpu_for_update && 2088 !amdgpu_gmc_vram_full_visible(&adev->gmc)), 2089 "CPU update of VM recommended only for large BAR system\n"); 2090 2091 if (vm->use_cpu_for_update) 2092 vm->update_funcs = &amdgpu_vm_cpu_funcs; 2093 else 2094 vm->update_funcs = &amdgpu_vm_sdma_funcs; 2095 vm->last_update = NULL; 2096 vm->last_unlocked = dma_fence_get_stub(); 2097 2098 mutex_init(&vm->eviction_lock); 2099 vm->evicting = false; 2100 2101 r = amdgpu_vm_pt_create(adev, vm, adev->vm_manager.root_level, 2102 false, &root); 2103 if (r) 2104 goto error_free_delayed; 2105 root_bo = &root->bo; 2106 r = amdgpu_bo_reserve(root_bo, true); 2107 if (r) 2108 goto error_free_root; 2109 2110 r = dma_resv_reserve_shared(root_bo->tbo.base.resv, 1); 2111 if (r) 2112 goto error_unreserve; 2113 2114 amdgpu_vm_bo_base_init(&vm->root, vm, root_bo); 2115 2116 r = amdgpu_vm_pt_clear(adev, vm, root, false); 2117 if (r) 2118 goto error_unreserve; 2119 2120 amdgpu_bo_unreserve(vm->root.bo); 2121 2122 INIT_KFIFO(vm->faults); 2123 2124 return 0; 2125 2126 error_unreserve: 2127 amdgpu_bo_unreserve(vm->root.bo); 2128 2129 error_free_root: 2130 amdgpu_bo_unref(&root->shadow); 2131 amdgpu_bo_unref(&root_bo); 2132 vm->root.bo = NULL; 2133 2134 error_free_delayed: 2135 dma_fence_put(vm->last_unlocked); 2136 drm_sched_entity_destroy(&vm->delayed); 2137 2138 error_free_immediate: 2139 drm_sched_entity_destroy(&vm->immediate); 2140 2141 return r; 2142 } 2143 2144 /** 2145 * amdgpu_vm_make_compute - Turn a GFX VM into a compute VM 2146 * 2147 * @adev: amdgpu_device pointer 2148 * @vm: requested vm 2149 * 2150 * This only works on GFX VMs that don't have any BOs added and no 2151 * page tables allocated yet. 2152 * 2153 * Changes the following VM parameters: 2154 * - use_cpu_for_update 2155 * - pte_supports_ats 2156 * 2157 * Reinitializes the page directory to reflect the changed ATS 2158 * setting. 2159 * 2160 * Returns: 2161 * 0 for success, -errno for errors. 2162 */ 2163 int amdgpu_vm_make_compute(struct amdgpu_device *adev, struct amdgpu_vm *vm) 2164 { 2165 bool pte_support_ats = (adev->asic_type == CHIP_RAVEN); 2166 int r; 2167 2168 r = amdgpu_bo_reserve(vm->root.bo, true); 2169 if (r) 2170 return r; 2171 2172 /* Sanity checks */ 2173 if (!amdgpu_vm_pt_is_root_clean(adev, vm)) { 2174 r = -EINVAL; 2175 goto unreserve_bo; 2176 } 2177 2178 /* Check if PD needs to be reinitialized and do it before 2179 * changing any other state, in case it fails. 2180 */ 2181 if (pte_support_ats != vm->pte_support_ats) { 2182 vm->pte_support_ats = pte_support_ats; 2183 r = amdgpu_vm_pt_clear(adev, vm, to_amdgpu_bo_vm(vm->root.bo), 2184 false); 2185 if (r) 2186 goto unreserve_bo; 2187 } 2188 2189 /* Update VM state */ 2190 vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode & 2191 AMDGPU_VM_USE_CPU_FOR_COMPUTE); 2192 DRM_DEBUG_DRIVER("VM update mode is %s\n", 2193 vm->use_cpu_for_update ? "CPU" : "SDMA"); 2194 WARN_ONCE((vm->use_cpu_for_update && 2195 !amdgpu_gmc_vram_full_visible(&adev->gmc)), 2196 "CPU update of VM recommended only for large BAR system\n"); 2197 2198 if (vm->use_cpu_for_update) { 2199 /* Sync with last SDMA update/clear before switching to CPU */ 2200 r = amdgpu_bo_sync_wait(vm->root.bo, 2201 AMDGPU_FENCE_OWNER_UNDEFINED, true); 2202 if (r) 2203 goto unreserve_bo; 2204 2205 vm->update_funcs = &amdgpu_vm_cpu_funcs; 2206 } else { 2207 vm->update_funcs = &amdgpu_vm_sdma_funcs; 2208 } 2209 dma_fence_put(vm->last_update); 2210 vm->last_update = NULL; 2211 vm->is_compute_context = true; 2212 2213 /* Free the shadow bo for compute VM */ 2214 amdgpu_bo_unref(&to_amdgpu_bo_vm(vm->root.bo)->shadow); 2215 2216 goto unreserve_bo; 2217 2218 unreserve_bo: 2219 amdgpu_bo_unreserve(vm->root.bo); 2220 return r; 2221 } 2222 2223 /** 2224 * amdgpu_vm_release_compute - release a compute vm 2225 * @adev: amdgpu_device pointer 2226 * @vm: a vm turned into compute vm by calling amdgpu_vm_make_compute 2227 * 2228 * This is a correspondant of amdgpu_vm_make_compute. It decouples compute 2229 * pasid from vm. Compute should stop use of vm after this call. 2230 */ 2231 void amdgpu_vm_release_compute(struct amdgpu_device *adev, struct amdgpu_vm *vm) 2232 { 2233 amdgpu_vm_set_pasid(adev, vm, 0); 2234 vm->is_compute_context = false; 2235 } 2236 2237 /** 2238 * amdgpu_vm_fini - tear down a vm instance 2239 * 2240 * @adev: amdgpu_device pointer 2241 * @vm: requested vm 2242 * 2243 * Tear down @vm. 2244 * Unbind the VM and remove all bos from the vm bo list 2245 */ 2246 void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm) 2247 { 2248 struct amdgpu_bo_va_mapping *mapping, *tmp; 2249 bool prt_fini_needed = !!adev->gmc.gmc_funcs->set_prt; 2250 struct amdgpu_bo *root; 2251 int i; 2252 2253 amdgpu_amdkfd_gpuvm_destroy_cb(adev, vm); 2254 2255 root = amdgpu_bo_ref(vm->root.bo); 2256 amdgpu_bo_reserve(root, true); 2257 amdgpu_vm_set_pasid(adev, vm, 0); 2258 dma_fence_wait(vm->last_unlocked, false); 2259 dma_fence_put(vm->last_unlocked); 2260 2261 list_for_each_entry_safe(mapping, tmp, &vm->freed, list) { 2262 if (mapping->flags & AMDGPU_PTE_PRT && prt_fini_needed) { 2263 amdgpu_vm_prt_fini(adev, vm); 2264 prt_fini_needed = false; 2265 } 2266 2267 list_del(&mapping->list); 2268 amdgpu_vm_free_mapping(adev, vm, mapping, NULL); 2269 } 2270 2271 amdgpu_vm_pt_free_root(adev, vm); 2272 amdgpu_bo_unreserve(root); 2273 amdgpu_bo_unref(&root); 2274 WARN_ON(vm->root.bo); 2275 2276 drm_sched_entity_destroy(&vm->immediate); 2277 drm_sched_entity_destroy(&vm->delayed); 2278 2279 if (!RB_EMPTY_ROOT(&vm->va.rb_root)) { 2280 dev_err(adev->dev, "still active bo inside vm\n"); 2281 } 2282 rbtree_postorder_for_each_entry_safe(mapping, tmp, 2283 &vm->va.rb_root, rb) { 2284 /* Don't remove the mapping here, we don't want to trigger a 2285 * rebalance and the tree is about to be destroyed anyway. 2286 */ 2287 list_del(&mapping->list); 2288 kfree(mapping); 2289 } 2290 2291 dma_fence_put(vm->last_update); 2292 for (i = 0; i < AMDGPU_MAX_VMHUBS; i++) 2293 amdgpu_vmid_free_reserved(adev, vm, i); 2294 } 2295 2296 /** 2297 * amdgpu_vm_manager_init - init the VM manager 2298 * 2299 * @adev: amdgpu_device pointer 2300 * 2301 * Initialize the VM manager structures 2302 */ 2303 void amdgpu_vm_manager_init(struct amdgpu_device *adev) 2304 { 2305 unsigned i; 2306 2307 /* Concurrent flushes are only possible starting with Vega10 and 2308 * are broken on Navi10 and Navi14. 2309 */ 2310 adev->vm_manager.concurrent_flush = !(adev->asic_type < CHIP_VEGA10 || 2311 adev->asic_type == CHIP_NAVI10 || 2312 adev->asic_type == CHIP_NAVI14); 2313 amdgpu_vmid_mgr_init(adev); 2314 2315 adev->vm_manager.fence_context = 2316 dma_fence_context_alloc(AMDGPU_MAX_RINGS); 2317 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) 2318 adev->vm_manager.seqno[i] = 0; 2319 2320 spin_lock_init(&adev->vm_manager.prt_lock); 2321 atomic_set(&adev->vm_manager.num_prt_users, 0); 2322 2323 /* If not overridden by the user, by default, only in large BAR systems 2324 * Compute VM tables will be updated by CPU 2325 */ 2326 #ifdef CONFIG_X86_64 2327 if (amdgpu_vm_update_mode == -1) { 2328 if (amdgpu_gmc_vram_full_visible(&adev->gmc)) 2329 adev->vm_manager.vm_update_mode = 2330 AMDGPU_VM_USE_CPU_FOR_COMPUTE; 2331 else 2332 adev->vm_manager.vm_update_mode = 0; 2333 } else 2334 adev->vm_manager.vm_update_mode = amdgpu_vm_update_mode; 2335 #else 2336 adev->vm_manager.vm_update_mode = 0; 2337 #endif 2338 2339 xa_init_flags(&adev->vm_manager.pasids, XA_FLAGS_LOCK_IRQ); 2340 } 2341 2342 /** 2343 * amdgpu_vm_manager_fini - cleanup VM manager 2344 * 2345 * @adev: amdgpu_device pointer 2346 * 2347 * Cleanup the VM manager and free resources. 2348 */ 2349 void amdgpu_vm_manager_fini(struct amdgpu_device *adev) 2350 { 2351 WARN_ON(!xa_empty(&adev->vm_manager.pasids)); 2352 xa_destroy(&adev->vm_manager.pasids); 2353 2354 amdgpu_vmid_mgr_fini(adev); 2355 } 2356 2357 /** 2358 * amdgpu_vm_ioctl - Manages VMID reservation for vm hubs. 2359 * 2360 * @dev: drm device pointer 2361 * @data: drm_amdgpu_vm 2362 * @filp: drm file pointer 2363 * 2364 * Returns: 2365 * 0 for success, -errno for errors. 2366 */ 2367 int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp) 2368 { 2369 union drm_amdgpu_vm *args = data; 2370 struct amdgpu_device *adev = drm_to_adev(dev); 2371 struct amdgpu_fpriv *fpriv = filp->driver_priv; 2372 long timeout = msecs_to_jiffies(2000); 2373 int r; 2374 2375 switch (args->in.op) { 2376 case AMDGPU_VM_OP_RESERVE_VMID: 2377 /* We only have requirement to reserve vmid from gfxhub */ 2378 r = amdgpu_vmid_alloc_reserved(adev, &fpriv->vm, 2379 AMDGPU_GFXHUB_0); 2380 if (r) 2381 return r; 2382 break; 2383 case AMDGPU_VM_OP_UNRESERVE_VMID: 2384 if (amdgpu_sriov_runtime(adev)) 2385 timeout = 8 * timeout; 2386 2387 /* Wait vm idle to make sure the vmid set in SPM_VMID is 2388 * not referenced anymore. 2389 */ 2390 r = amdgpu_bo_reserve(fpriv->vm.root.bo, true); 2391 if (r) 2392 return r; 2393 2394 r = amdgpu_vm_wait_idle(&fpriv->vm, timeout); 2395 if (r < 0) 2396 return r; 2397 2398 amdgpu_bo_unreserve(fpriv->vm.root.bo); 2399 amdgpu_vmid_free_reserved(adev, &fpriv->vm, AMDGPU_GFXHUB_0); 2400 break; 2401 default: 2402 return -EINVAL; 2403 } 2404 2405 return 0; 2406 } 2407 2408 /** 2409 * amdgpu_vm_get_task_info - Extracts task info for a PASID. 2410 * 2411 * @adev: drm device pointer 2412 * @pasid: PASID identifier for VM 2413 * @task_info: task_info to fill. 2414 */ 2415 void amdgpu_vm_get_task_info(struct amdgpu_device *adev, u32 pasid, 2416 struct amdgpu_task_info *task_info) 2417 { 2418 struct amdgpu_vm *vm; 2419 unsigned long flags; 2420 2421 xa_lock_irqsave(&adev->vm_manager.pasids, flags); 2422 2423 vm = xa_load(&adev->vm_manager.pasids, pasid); 2424 if (vm) 2425 *task_info = vm->task_info; 2426 2427 xa_unlock_irqrestore(&adev->vm_manager.pasids, flags); 2428 } 2429 2430 /** 2431 * amdgpu_vm_set_task_info - Sets VMs task info. 2432 * 2433 * @vm: vm for which to set the info 2434 */ 2435 void amdgpu_vm_set_task_info(struct amdgpu_vm *vm) 2436 { 2437 if (vm->task_info.pid) 2438 return; 2439 2440 vm->task_info.pid = current->pid; 2441 get_task_comm(vm->task_info.task_name, current); 2442 2443 if (current->group_leader->mm != current->mm) 2444 return; 2445 2446 vm->task_info.tgid = current->group_leader->pid; 2447 get_task_comm(vm->task_info.process_name, current->group_leader); 2448 } 2449 2450 /** 2451 * amdgpu_vm_handle_fault - graceful handling of VM faults. 2452 * @adev: amdgpu device pointer 2453 * @pasid: PASID of the VM 2454 * @addr: Address of the fault 2455 * @write_fault: true is write fault, false is read fault 2456 * 2457 * Try to gracefully handle a VM fault. Return true if the fault was handled and 2458 * shouldn't be reported any more. 2459 */ 2460 bool amdgpu_vm_handle_fault(struct amdgpu_device *adev, u32 pasid, 2461 uint64_t addr, bool write_fault) 2462 { 2463 bool is_compute_context = false; 2464 struct amdgpu_bo *root; 2465 unsigned long irqflags; 2466 uint64_t value, flags; 2467 struct amdgpu_vm *vm; 2468 int r; 2469 2470 xa_lock_irqsave(&adev->vm_manager.pasids, irqflags); 2471 vm = xa_load(&adev->vm_manager.pasids, pasid); 2472 if (vm) { 2473 root = amdgpu_bo_ref(vm->root.bo); 2474 is_compute_context = vm->is_compute_context; 2475 } else { 2476 root = NULL; 2477 } 2478 xa_unlock_irqrestore(&adev->vm_manager.pasids, irqflags); 2479 2480 if (!root) 2481 return false; 2482 2483 addr /= AMDGPU_GPU_PAGE_SIZE; 2484 2485 if (is_compute_context && 2486 !svm_range_restore_pages(adev, pasid, addr, write_fault)) { 2487 amdgpu_bo_unref(&root); 2488 return true; 2489 } 2490 2491 r = amdgpu_bo_reserve(root, true); 2492 if (r) 2493 goto error_unref; 2494 2495 /* Double check that the VM still exists */ 2496 xa_lock_irqsave(&adev->vm_manager.pasids, irqflags); 2497 vm = xa_load(&adev->vm_manager.pasids, pasid); 2498 if (vm && vm->root.bo != root) 2499 vm = NULL; 2500 xa_unlock_irqrestore(&adev->vm_manager.pasids, irqflags); 2501 if (!vm) 2502 goto error_unlock; 2503 2504 flags = AMDGPU_PTE_VALID | AMDGPU_PTE_SNOOPED | 2505 AMDGPU_PTE_SYSTEM; 2506 2507 if (is_compute_context) { 2508 /* Intentionally setting invalid PTE flag 2509 * combination to force a no-retry-fault 2510 */ 2511 flags = AMDGPU_PTE_EXECUTABLE | AMDGPU_PDE_PTE | 2512 AMDGPU_PTE_TF; 2513 value = 0; 2514 } else if (amdgpu_vm_fault_stop == AMDGPU_VM_FAULT_STOP_NEVER) { 2515 /* Redirect the access to the dummy page */ 2516 value = adev->dummy_page_addr; 2517 flags |= AMDGPU_PTE_EXECUTABLE | AMDGPU_PTE_READABLE | 2518 AMDGPU_PTE_WRITEABLE; 2519 2520 } else { 2521 /* Let the hw retry silently on the PTE */ 2522 value = 0; 2523 } 2524 2525 r = dma_resv_reserve_shared(root->tbo.base.resv, 1); 2526 if (r) { 2527 pr_debug("failed %d to reserve fence slot\n", r); 2528 goto error_unlock; 2529 } 2530 2531 r = amdgpu_vm_update_range(adev, vm, true, false, false, NULL, addr, 2532 addr, flags, value, 0, NULL, NULL, NULL); 2533 if (r) 2534 goto error_unlock; 2535 2536 r = amdgpu_vm_update_pdes(adev, vm, true); 2537 2538 error_unlock: 2539 amdgpu_bo_unreserve(root); 2540 if (r < 0) 2541 DRM_ERROR("Can't handle page fault (%d)\n", r); 2542 2543 error_unref: 2544 amdgpu_bo_unref(&root); 2545 2546 return false; 2547 } 2548 2549 #if defined(CONFIG_DEBUG_FS) 2550 /** 2551 * amdgpu_debugfs_vm_bo_info - print BO info for the VM 2552 * 2553 * @vm: Requested VM for printing BO info 2554 * @m: debugfs file 2555 * 2556 * Print BO information in debugfs file for the VM 2557 */ 2558 void amdgpu_debugfs_vm_bo_info(struct amdgpu_vm *vm, struct seq_file *m) 2559 { 2560 struct amdgpu_bo_va *bo_va, *tmp; 2561 u64 total_idle = 0; 2562 u64 total_evicted = 0; 2563 u64 total_relocated = 0; 2564 u64 total_moved = 0; 2565 u64 total_invalidated = 0; 2566 u64 total_done = 0; 2567 unsigned int total_idle_objs = 0; 2568 unsigned int total_evicted_objs = 0; 2569 unsigned int total_relocated_objs = 0; 2570 unsigned int total_moved_objs = 0; 2571 unsigned int total_invalidated_objs = 0; 2572 unsigned int total_done_objs = 0; 2573 unsigned int id = 0; 2574 2575 seq_puts(m, "\tIdle BOs:\n"); 2576 list_for_each_entry_safe(bo_va, tmp, &vm->idle, base.vm_status) { 2577 if (!bo_va->base.bo) 2578 continue; 2579 total_idle += amdgpu_bo_print_info(id++, bo_va->base.bo, m); 2580 } 2581 total_idle_objs = id; 2582 id = 0; 2583 2584 seq_puts(m, "\tEvicted BOs:\n"); 2585 list_for_each_entry_safe(bo_va, tmp, &vm->evicted, base.vm_status) { 2586 if (!bo_va->base.bo) 2587 continue; 2588 total_evicted += amdgpu_bo_print_info(id++, bo_va->base.bo, m); 2589 } 2590 total_evicted_objs = id; 2591 id = 0; 2592 2593 seq_puts(m, "\tRelocated BOs:\n"); 2594 list_for_each_entry_safe(bo_va, tmp, &vm->relocated, base.vm_status) { 2595 if (!bo_va->base.bo) 2596 continue; 2597 total_relocated += amdgpu_bo_print_info(id++, bo_va->base.bo, m); 2598 } 2599 total_relocated_objs = id; 2600 id = 0; 2601 2602 seq_puts(m, "\tMoved BOs:\n"); 2603 list_for_each_entry_safe(bo_va, tmp, &vm->moved, base.vm_status) { 2604 if (!bo_va->base.bo) 2605 continue; 2606 total_moved += amdgpu_bo_print_info(id++, bo_va->base.bo, m); 2607 } 2608 total_moved_objs = id; 2609 id = 0; 2610 2611 seq_puts(m, "\tInvalidated BOs:\n"); 2612 spin_lock(&vm->invalidated_lock); 2613 list_for_each_entry_safe(bo_va, tmp, &vm->invalidated, base.vm_status) { 2614 if (!bo_va->base.bo) 2615 continue; 2616 total_invalidated += amdgpu_bo_print_info(id++, bo_va->base.bo, m); 2617 } 2618 total_invalidated_objs = id; 2619 id = 0; 2620 2621 seq_puts(m, "\tDone BOs:\n"); 2622 list_for_each_entry_safe(bo_va, tmp, &vm->done, base.vm_status) { 2623 if (!bo_va->base.bo) 2624 continue; 2625 total_done += amdgpu_bo_print_info(id++, bo_va->base.bo, m); 2626 } 2627 spin_unlock(&vm->invalidated_lock); 2628 total_done_objs = id; 2629 2630 seq_printf(m, "\tTotal idle size: %12lld\tobjs:\t%d\n", total_idle, 2631 total_idle_objs); 2632 seq_printf(m, "\tTotal evicted size: %12lld\tobjs:\t%d\n", total_evicted, 2633 total_evicted_objs); 2634 seq_printf(m, "\tTotal relocated size: %12lld\tobjs:\t%d\n", total_relocated, 2635 total_relocated_objs); 2636 seq_printf(m, "\tTotal moved size: %12lld\tobjs:\t%d\n", total_moved, 2637 total_moved_objs); 2638 seq_printf(m, "\tTotal invalidated size: %12lld\tobjs:\t%d\n", total_invalidated, 2639 total_invalidated_objs); 2640 seq_printf(m, "\tTotal done size: %12lld\tobjs:\t%d\n", total_done, 2641 total_done_objs); 2642 } 2643 #endif 2644