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