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 || size & ~LINUX_PAGE_MASK) 1493 return -EINVAL; 1494 if (saddr + size <= saddr || offset + size <= offset) 1495 return -EINVAL; 1496 1497 /* make sure object fit at this offset */ 1498 eaddr = saddr + size - 1; 1499 if ((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 || size & ~LINUX_PAGE_MASK) 1559 return -EINVAL; 1560 if (saddr + size <= saddr || offset + size <= offset) 1561 return -EINVAL; 1562 1563 /* make sure object fit at this offset */ 1564 eaddr = saddr + size - 1; 1565 if ((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 struct amdgpu_bo *bo = before->bo_va->base.bo; 1734 1735 amdgpu_vm_it_insert(before, &vm->va); 1736 if (before->flags & AMDGPU_PTE_PRT) 1737 amdgpu_vm_prt_get(adev); 1738 1739 if (bo && bo->tbo.base.resv == vm->root.bo->tbo.base.resv && 1740 !before->bo_va->base.moved) 1741 amdgpu_vm_bo_moved(&before->bo_va->base); 1742 } else { 1743 kfree(before); 1744 } 1745 1746 /* Insert partial mapping after the range */ 1747 if (!list_empty(&after->list)) { 1748 struct amdgpu_bo *bo = after->bo_va->base.bo; 1749 1750 amdgpu_vm_it_insert(after, &vm->va); 1751 if (after->flags & AMDGPU_PTE_PRT) 1752 amdgpu_vm_prt_get(adev); 1753 1754 if (bo && bo->tbo.base.resv == vm->root.bo->tbo.base.resv && 1755 !after->bo_va->base.moved) 1756 amdgpu_vm_bo_moved(&after->bo_va->base); 1757 } else { 1758 kfree(after); 1759 } 1760 1761 return 0; 1762 } 1763 1764 /** 1765 * amdgpu_vm_bo_lookup_mapping - find mapping by address 1766 * 1767 * @vm: the requested VM 1768 * @addr: the address 1769 * 1770 * Find a mapping by it's address. 1771 * 1772 * Returns: 1773 * The amdgpu_bo_va_mapping matching for addr or NULL 1774 * 1775 */ 1776 struct amdgpu_bo_va_mapping *amdgpu_vm_bo_lookup_mapping(struct amdgpu_vm *vm, 1777 uint64_t addr) 1778 { 1779 return amdgpu_vm_it_iter_first(&vm->va, addr, addr); 1780 } 1781 1782 /** 1783 * amdgpu_vm_bo_trace_cs - trace all reserved mappings 1784 * 1785 * @vm: the requested vm 1786 * @ticket: CS ticket 1787 * 1788 * Trace all mappings of BOs reserved during a command submission. 1789 */ 1790 void amdgpu_vm_bo_trace_cs(struct amdgpu_vm *vm, struct ww_acquire_ctx *ticket) 1791 { 1792 struct amdgpu_bo_va_mapping *mapping; 1793 1794 if (!trace_amdgpu_vm_bo_cs_enabled()) 1795 return; 1796 1797 for (mapping = amdgpu_vm_it_iter_first(&vm->va, 0, U64_MAX); mapping; 1798 mapping = amdgpu_vm_it_iter_next(mapping, 0, U64_MAX)) { 1799 if (mapping->bo_va && mapping->bo_va->base.bo) { 1800 struct amdgpu_bo *bo; 1801 1802 bo = mapping->bo_va->base.bo; 1803 if (dma_resv_locking_ctx(bo->tbo.base.resv) != 1804 ticket) 1805 continue; 1806 } 1807 1808 trace_amdgpu_vm_bo_cs(mapping); 1809 } 1810 } 1811 1812 /** 1813 * amdgpu_vm_bo_del - remove a bo from a specific vm 1814 * 1815 * @adev: amdgpu_device pointer 1816 * @bo_va: requested bo_va 1817 * 1818 * Remove @bo_va->bo from the requested vm. 1819 * 1820 * Object have to be reserved! 1821 */ 1822 void amdgpu_vm_bo_del(struct amdgpu_device *adev, 1823 struct amdgpu_bo_va *bo_va) 1824 { 1825 struct amdgpu_bo_va_mapping *mapping, *next; 1826 struct amdgpu_bo *bo = bo_va->base.bo; 1827 struct amdgpu_vm *vm = bo_va->base.vm; 1828 struct amdgpu_vm_bo_base **base; 1829 1830 dma_resv_assert_held(vm->root.bo->tbo.base.resv); 1831 1832 if (bo) { 1833 dma_resv_assert_held(bo->tbo.base.resv); 1834 if (bo->tbo.base.resv == vm->root.bo->tbo.base.resv) 1835 ttm_bo_set_bulk_move(&bo->tbo, NULL); 1836 1837 for (base = &bo_va->base.bo->vm_bo; *base; 1838 base = &(*base)->next) { 1839 if (*base != &bo_va->base) 1840 continue; 1841 1842 *base = bo_va->base.next; 1843 break; 1844 } 1845 } 1846 1847 spin_lock(&vm->status_lock); 1848 list_del(&bo_va->base.vm_status); 1849 spin_unlock(&vm->status_lock); 1850 1851 list_for_each_entry_safe(mapping, next, &bo_va->valids, list) { 1852 list_del(&mapping->list); 1853 amdgpu_vm_it_remove(mapping, &vm->va); 1854 mapping->bo_va = NULL; 1855 trace_amdgpu_vm_bo_unmap(bo_va, mapping); 1856 list_add(&mapping->list, &vm->freed); 1857 } 1858 list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) { 1859 list_del(&mapping->list); 1860 amdgpu_vm_it_remove(mapping, &vm->va); 1861 amdgpu_vm_free_mapping(adev, vm, mapping, 1862 bo_va->last_pt_update); 1863 } 1864 1865 dma_fence_put(bo_va->last_pt_update); 1866 1867 if (bo && bo_va->is_xgmi) 1868 amdgpu_xgmi_set_pstate(adev, AMDGPU_XGMI_PSTATE_MIN); 1869 1870 kfree(bo_va); 1871 } 1872 1873 /** 1874 * amdgpu_vm_evictable - check if we can evict a VM 1875 * 1876 * @bo: A page table of the VM. 1877 * 1878 * Check if it is possible to evict a VM. 1879 */ 1880 bool amdgpu_vm_evictable(struct amdgpu_bo *bo) 1881 { 1882 struct amdgpu_vm_bo_base *bo_base = bo->vm_bo; 1883 1884 /* Page tables of a destroyed VM can go away immediately */ 1885 if (!bo_base || !bo_base->vm) 1886 return true; 1887 1888 /* Don't evict VM page tables while they are busy */ 1889 if (!dma_resv_test_signaled(bo->tbo.base.resv, DMA_RESV_USAGE_BOOKKEEP)) 1890 return false; 1891 1892 /* Try to block ongoing updates */ 1893 if (!amdgpu_vm_eviction_trylock(bo_base->vm)) 1894 return false; 1895 1896 /* Don't evict VM page tables while they are updated */ 1897 if (!dma_fence_is_signaled(bo_base->vm->last_unlocked)) { 1898 amdgpu_vm_eviction_unlock(bo_base->vm); 1899 return false; 1900 } 1901 1902 bo_base->vm->evicting = true; 1903 amdgpu_vm_eviction_unlock(bo_base->vm); 1904 return true; 1905 } 1906 1907 /** 1908 * amdgpu_vm_bo_invalidate - mark the bo as invalid 1909 * 1910 * @adev: amdgpu_device pointer 1911 * @bo: amdgpu buffer object 1912 * @evicted: is the BO evicted 1913 * 1914 * Mark @bo as invalid. 1915 */ 1916 void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev, 1917 struct amdgpu_bo *bo, bool evicted) 1918 { 1919 struct amdgpu_vm_bo_base *bo_base; 1920 1921 /* shadow bo doesn't have bo base, its validation needs its parent */ 1922 if (bo->parent && (amdgpu_bo_shadowed(bo->parent) == bo)) 1923 bo = bo->parent; 1924 1925 for (bo_base = bo->vm_bo; bo_base; bo_base = bo_base->next) { 1926 struct amdgpu_vm *vm = bo_base->vm; 1927 1928 if (evicted && bo->tbo.base.resv == vm->root.bo->tbo.base.resv) { 1929 amdgpu_vm_bo_evicted(bo_base); 1930 continue; 1931 } 1932 1933 if (bo_base->moved) 1934 continue; 1935 bo_base->moved = true; 1936 1937 if (bo->tbo.type == ttm_bo_type_kernel) 1938 amdgpu_vm_bo_relocated(bo_base); 1939 else if (bo->tbo.base.resv == vm->root.bo->tbo.base.resv) 1940 amdgpu_vm_bo_moved(bo_base); 1941 else 1942 amdgpu_vm_bo_invalidated(bo_base); 1943 } 1944 } 1945 1946 /** 1947 * amdgpu_vm_get_block_size - calculate VM page table size as power of two 1948 * 1949 * @vm_size: VM size 1950 * 1951 * Returns: 1952 * VM page table as power of two 1953 */ 1954 static uint32_t amdgpu_vm_get_block_size(uint64_t vm_size) 1955 { 1956 /* Total bits covered by PD + PTs */ 1957 unsigned bits = ilog2(vm_size) + 18; 1958 1959 /* Make sure the PD is 4K in size up to 8GB address space. 1960 Above that split equal between PD and PTs */ 1961 if (vm_size <= 8) 1962 return (bits - 9); 1963 else 1964 return ((bits + 3) / 2); 1965 } 1966 1967 /** 1968 * amdgpu_vm_adjust_size - adjust vm size, block size and fragment size 1969 * 1970 * @adev: amdgpu_device pointer 1971 * @min_vm_size: the minimum vm size in GB if it's set auto 1972 * @fragment_size_default: Default PTE fragment size 1973 * @max_level: max VMPT level 1974 * @max_bits: max address space size in bits 1975 * 1976 */ 1977 void amdgpu_vm_adjust_size(struct amdgpu_device *adev, uint32_t min_vm_size, 1978 uint32_t fragment_size_default, unsigned max_level, 1979 unsigned max_bits) 1980 { 1981 unsigned int max_size = 1 << (max_bits - 30); 1982 unsigned int vm_size; 1983 uint64_t tmp; 1984 1985 /* adjust vm size first */ 1986 if (amdgpu_vm_size != -1) { 1987 vm_size = amdgpu_vm_size; 1988 if (vm_size > max_size) { 1989 dev_warn(adev->dev, "VM size (%d) too large, max is %u GB\n", 1990 amdgpu_vm_size, max_size); 1991 vm_size = max_size; 1992 } 1993 } else { 1994 #ifdef __linux__ 1995 struct sysinfo si; 1996 #endif 1997 unsigned int phys_ram_gb; 1998 1999 /* Optimal VM size depends on the amount of physical 2000 * RAM available. Underlying requirements and 2001 * assumptions: 2002 * 2003 * - Need to map system memory and VRAM from all GPUs 2004 * - VRAM from other GPUs not known here 2005 * - Assume VRAM <= system memory 2006 * - On GFX8 and older, VM space can be segmented for 2007 * different MTYPEs 2008 * - Need to allow room for fragmentation, guard pages etc. 2009 * 2010 * This adds up to a rough guess of system memory x3. 2011 * Round up to power of two to maximize the available 2012 * VM size with the given page table size. 2013 */ 2014 #ifdef __linux__ 2015 si_meminfo(&si); 2016 phys_ram_gb = ((uint64_t)si.totalram * si.mem_unit + 2017 (1 << 30) - 1) >> 30; 2018 #else 2019 phys_ram_gb = ((uint64_t)ptoa(physmem) + 2020 (1 << 30) - 1) >> 30; 2021 #endif 2022 vm_size = roundup_pow_of_two( 2023 min(max(phys_ram_gb * 3, min_vm_size), max_size)); 2024 } 2025 2026 adev->vm_manager.max_pfn = (uint64_t)vm_size << 18; 2027 2028 tmp = roundup_pow_of_two(adev->vm_manager.max_pfn); 2029 if (amdgpu_vm_block_size != -1) 2030 tmp >>= amdgpu_vm_block_size - 9; 2031 tmp = DIV_ROUND_UP(fls64(tmp) - 1, 9) - 1; 2032 adev->vm_manager.num_level = min(max_level, (unsigned)tmp); 2033 switch (adev->vm_manager.num_level) { 2034 case 3: 2035 adev->vm_manager.root_level = AMDGPU_VM_PDB2; 2036 break; 2037 case 2: 2038 adev->vm_manager.root_level = AMDGPU_VM_PDB1; 2039 break; 2040 case 1: 2041 adev->vm_manager.root_level = AMDGPU_VM_PDB0; 2042 break; 2043 default: 2044 dev_err(adev->dev, "VMPT only supports 2~4+1 levels\n"); 2045 } 2046 /* block size depends on vm size and hw setup*/ 2047 if (amdgpu_vm_block_size != -1) 2048 adev->vm_manager.block_size = 2049 min((unsigned)amdgpu_vm_block_size, max_bits 2050 - AMDGPU_GPU_PAGE_SHIFT 2051 - 9 * adev->vm_manager.num_level); 2052 else if (adev->vm_manager.num_level > 1) 2053 adev->vm_manager.block_size = 9; 2054 else 2055 adev->vm_manager.block_size = amdgpu_vm_get_block_size(tmp); 2056 2057 if (amdgpu_vm_fragment_size == -1) 2058 adev->vm_manager.fragment_size = fragment_size_default; 2059 else 2060 adev->vm_manager.fragment_size = amdgpu_vm_fragment_size; 2061 2062 DRM_INFO("vm size is %u GB, %u levels, block size is %u-bit, fragment size is %u-bit\n", 2063 vm_size, adev->vm_manager.num_level + 1, 2064 adev->vm_manager.block_size, 2065 adev->vm_manager.fragment_size); 2066 } 2067 2068 /** 2069 * amdgpu_vm_wait_idle - wait for the VM to become idle 2070 * 2071 * @vm: VM object to wait for 2072 * @timeout: timeout to wait for VM to become idle 2073 */ 2074 long amdgpu_vm_wait_idle(struct amdgpu_vm *vm, long timeout) 2075 { 2076 timeout = dma_resv_wait_timeout(vm->root.bo->tbo.base.resv, 2077 DMA_RESV_USAGE_BOOKKEEP, 2078 true, timeout); 2079 if (timeout <= 0) 2080 return timeout; 2081 2082 return dma_fence_wait_timeout(vm->last_unlocked, true, timeout); 2083 } 2084 2085 /** 2086 * amdgpu_vm_init - initialize a vm instance 2087 * 2088 * @adev: amdgpu_device pointer 2089 * @vm: requested vm 2090 * 2091 * Init @vm fields. 2092 * 2093 * Returns: 2094 * 0 for success, error for failure. 2095 */ 2096 int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm) 2097 { 2098 struct amdgpu_bo *root_bo; 2099 struct amdgpu_bo_vm *root; 2100 int r, i; 2101 2102 vm->va = RB_ROOT_CACHED; 2103 for (i = 0; i < AMDGPU_MAX_VMHUBS; i++) 2104 vm->reserved_vmid[i] = NULL; 2105 INIT_LIST_HEAD(&vm->evicted); 2106 INIT_LIST_HEAD(&vm->relocated); 2107 INIT_LIST_HEAD(&vm->moved); 2108 INIT_LIST_HEAD(&vm->idle); 2109 INIT_LIST_HEAD(&vm->invalidated); 2110 mtx_init(&vm->status_lock, IPL_NONE); 2111 INIT_LIST_HEAD(&vm->freed); 2112 INIT_LIST_HEAD(&vm->done); 2113 INIT_LIST_HEAD(&vm->pt_freed); 2114 INIT_WORK(&vm->pt_free_work, amdgpu_vm_pt_free_work); 2115 2116 /* create scheduler entities for page table updates */ 2117 r = drm_sched_entity_init(&vm->immediate, DRM_SCHED_PRIORITY_NORMAL, 2118 adev->vm_manager.vm_pte_scheds, 2119 adev->vm_manager.vm_pte_num_scheds, NULL); 2120 if (r) 2121 return r; 2122 2123 r = drm_sched_entity_init(&vm->delayed, DRM_SCHED_PRIORITY_NORMAL, 2124 adev->vm_manager.vm_pte_scheds, 2125 adev->vm_manager.vm_pte_num_scheds, NULL); 2126 if (r) 2127 goto error_free_immediate; 2128 2129 vm->pte_support_ats = false; 2130 vm->is_compute_context = false; 2131 2132 vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode & 2133 AMDGPU_VM_USE_CPU_FOR_GFX); 2134 2135 DRM_DEBUG_DRIVER("VM update mode is %s\n", 2136 vm->use_cpu_for_update ? "CPU" : "SDMA"); 2137 WARN_ONCE((vm->use_cpu_for_update && 2138 !amdgpu_gmc_vram_full_visible(&adev->gmc)), 2139 "CPU update of VM recommended only for large BAR system\n"); 2140 2141 if (vm->use_cpu_for_update) 2142 vm->update_funcs = &amdgpu_vm_cpu_funcs; 2143 else 2144 vm->update_funcs = &amdgpu_vm_sdma_funcs; 2145 vm->last_update = NULL; 2146 vm->last_unlocked = dma_fence_get_stub(); 2147 vm->last_tlb_flush = dma_fence_get_stub(); 2148 2149 rw_init(&vm->eviction_lock, "avmev"); 2150 vm->evicting = false; 2151 2152 r = amdgpu_vm_pt_create(adev, vm, adev->vm_manager.root_level, 2153 false, &root); 2154 if (r) 2155 goto error_free_delayed; 2156 root_bo = &root->bo; 2157 r = amdgpu_bo_reserve(root_bo, true); 2158 if (r) 2159 goto error_free_root; 2160 2161 r = dma_resv_reserve_fences(root_bo->tbo.base.resv, 1); 2162 if (r) 2163 goto error_unreserve; 2164 2165 amdgpu_vm_bo_base_init(&vm->root, vm, root_bo); 2166 2167 r = amdgpu_vm_pt_clear(adev, vm, root, false); 2168 if (r) 2169 goto error_unreserve; 2170 2171 amdgpu_bo_unreserve(vm->root.bo); 2172 2173 #ifdef __linux__ 2174 INIT_KFIFO(vm->faults); 2175 #else 2176 SIMPLEQ_INIT(&vm->faults); 2177 #endif 2178 2179 return 0; 2180 2181 error_unreserve: 2182 amdgpu_bo_unreserve(vm->root.bo); 2183 2184 error_free_root: 2185 amdgpu_bo_unref(&root->shadow); 2186 amdgpu_bo_unref(&root_bo); 2187 vm->root.bo = NULL; 2188 2189 error_free_delayed: 2190 dma_fence_put(vm->last_tlb_flush); 2191 dma_fence_put(vm->last_unlocked); 2192 drm_sched_entity_destroy(&vm->delayed); 2193 2194 error_free_immediate: 2195 drm_sched_entity_destroy(&vm->immediate); 2196 2197 return r; 2198 } 2199 2200 /** 2201 * amdgpu_vm_make_compute - Turn a GFX VM into a compute VM 2202 * 2203 * @adev: amdgpu_device pointer 2204 * @vm: requested vm 2205 * 2206 * This only works on GFX VMs that don't have any BOs added and no 2207 * page tables allocated yet. 2208 * 2209 * Changes the following VM parameters: 2210 * - use_cpu_for_update 2211 * - pte_supports_ats 2212 * 2213 * Reinitializes the page directory to reflect the changed ATS 2214 * setting. 2215 * 2216 * Returns: 2217 * 0 for success, -errno for errors. 2218 */ 2219 int amdgpu_vm_make_compute(struct amdgpu_device *adev, struct amdgpu_vm *vm) 2220 { 2221 bool pte_support_ats = (adev->asic_type == CHIP_RAVEN); 2222 int r; 2223 2224 r = amdgpu_bo_reserve(vm->root.bo, true); 2225 if (r) 2226 return r; 2227 2228 /* Sanity checks */ 2229 if (!amdgpu_vm_pt_is_root_clean(adev, vm)) { 2230 r = -EINVAL; 2231 goto unreserve_bo; 2232 } 2233 2234 /* Check if PD needs to be reinitialized and do it before 2235 * changing any other state, in case it fails. 2236 */ 2237 if (pte_support_ats != vm->pte_support_ats) { 2238 vm->pte_support_ats = pte_support_ats; 2239 r = amdgpu_vm_pt_clear(adev, vm, to_amdgpu_bo_vm(vm->root.bo), 2240 false); 2241 if (r) 2242 goto unreserve_bo; 2243 } 2244 2245 /* Update VM state */ 2246 vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode & 2247 AMDGPU_VM_USE_CPU_FOR_COMPUTE); 2248 DRM_DEBUG_DRIVER("VM update mode is %s\n", 2249 vm->use_cpu_for_update ? "CPU" : "SDMA"); 2250 WARN_ONCE((vm->use_cpu_for_update && 2251 !amdgpu_gmc_vram_full_visible(&adev->gmc)), 2252 "CPU update of VM recommended only for large BAR system\n"); 2253 2254 if (vm->use_cpu_for_update) { 2255 /* Sync with last SDMA update/clear before switching to CPU */ 2256 r = amdgpu_bo_sync_wait(vm->root.bo, 2257 AMDGPU_FENCE_OWNER_UNDEFINED, true); 2258 if (r) 2259 goto unreserve_bo; 2260 2261 vm->update_funcs = &amdgpu_vm_cpu_funcs; 2262 } else { 2263 vm->update_funcs = &amdgpu_vm_sdma_funcs; 2264 } 2265 /* 2266 * Make sure root PD gets mapped. As vm_update_mode could be changed 2267 * when turning a GFX VM into a compute VM. 2268 */ 2269 r = vm->update_funcs->map_table(to_amdgpu_bo_vm(vm->root.bo)); 2270 if (r) 2271 goto unreserve_bo; 2272 2273 dma_fence_put(vm->last_update); 2274 vm->last_update = NULL; 2275 vm->is_compute_context = true; 2276 2277 /* Free the shadow bo for compute VM */ 2278 amdgpu_bo_unref(&to_amdgpu_bo_vm(vm->root.bo)->shadow); 2279 2280 goto unreserve_bo; 2281 2282 unreserve_bo: 2283 amdgpu_bo_unreserve(vm->root.bo); 2284 return r; 2285 } 2286 2287 /** 2288 * amdgpu_vm_release_compute - release a compute vm 2289 * @adev: amdgpu_device pointer 2290 * @vm: a vm turned into compute vm by calling amdgpu_vm_make_compute 2291 * 2292 * This is a correspondant of amdgpu_vm_make_compute. It decouples compute 2293 * pasid from vm. Compute should stop use of vm after this call. 2294 */ 2295 void amdgpu_vm_release_compute(struct amdgpu_device *adev, struct amdgpu_vm *vm) 2296 { 2297 amdgpu_vm_set_pasid(adev, vm, 0); 2298 vm->is_compute_context = false; 2299 } 2300 2301 /** 2302 * amdgpu_vm_fini - tear down a vm instance 2303 * 2304 * @adev: amdgpu_device pointer 2305 * @vm: requested vm 2306 * 2307 * Tear down @vm. 2308 * Unbind the VM and remove all bos from the vm bo list 2309 */ 2310 void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm) 2311 { 2312 struct amdgpu_bo_va_mapping *mapping, *tmp; 2313 bool prt_fini_needed = !!adev->gmc.gmc_funcs->set_prt; 2314 struct amdgpu_bo *root; 2315 unsigned long flags; 2316 int i; 2317 2318 amdgpu_amdkfd_gpuvm_destroy_cb(adev, vm); 2319 2320 flush_work(&vm->pt_free_work); 2321 2322 root = amdgpu_bo_ref(vm->root.bo); 2323 amdgpu_bo_reserve(root, true); 2324 amdgpu_vm_set_pasid(adev, vm, 0); 2325 dma_fence_wait(vm->last_unlocked, false); 2326 dma_fence_put(vm->last_unlocked); 2327 dma_fence_wait(vm->last_tlb_flush, false); 2328 /* Make sure that all fence callbacks have completed */ 2329 spin_lock_irqsave(vm->last_tlb_flush->lock, flags); 2330 spin_unlock_irqrestore(vm->last_tlb_flush->lock, flags); 2331 dma_fence_put(vm->last_tlb_flush); 2332 2333 list_for_each_entry_safe(mapping, tmp, &vm->freed, list) { 2334 if (mapping->flags & AMDGPU_PTE_PRT && prt_fini_needed) { 2335 amdgpu_vm_prt_fini(adev, vm); 2336 prt_fini_needed = false; 2337 } 2338 2339 list_del(&mapping->list); 2340 amdgpu_vm_free_mapping(adev, vm, mapping, NULL); 2341 } 2342 2343 amdgpu_vm_pt_free_root(adev, vm); 2344 amdgpu_bo_unreserve(root); 2345 amdgpu_bo_unref(&root); 2346 WARN_ON(vm->root.bo); 2347 2348 drm_sched_entity_destroy(&vm->immediate); 2349 drm_sched_entity_destroy(&vm->delayed); 2350 2351 if (!RB_EMPTY_ROOT(&vm->va.rb_root)) { 2352 dev_err(adev->dev, "still active bo inside vm\n"); 2353 } 2354 rbtree_postorder_for_each_entry_safe(mapping, tmp, 2355 &vm->va.rb_root, rb) { 2356 /* Don't remove the mapping here, we don't want to trigger a 2357 * rebalance and the tree is about to be destroyed anyway. 2358 */ 2359 list_del(&mapping->list); 2360 kfree(mapping); 2361 } 2362 2363 dma_fence_put(vm->last_update); 2364 for (i = 0; i < AMDGPU_MAX_VMHUBS; i++) 2365 amdgpu_vmid_free_reserved(adev, vm, i); 2366 } 2367 2368 /** 2369 * amdgpu_vm_manager_init - init the VM manager 2370 * 2371 * @adev: amdgpu_device pointer 2372 * 2373 * Initialize the VM manager structures 2374 */ 2375 void amdgpu_vm_manager_init(struct amdgpu_device *adev) 2376 { 2377 unsigned i; 2378 2379 /* Concurrent flushes are only possible starting with Vega10 and 2380 * are broken on Navi10 and Navi14. 2381 */ 2382 adev->vm_manager.concurrent_flush = !(adev->asic_type < CHIP_VEGA10 || 2383 adev->asic_type == CHIP_NAVI10 || 2384 adev->asic_type == CHIP_NAVI14); 2385 amdgpu_vmid_mgr_init(adev); 2386 2387 adev->vm_manager.fence_context = 2388 dma_fence_context_alloc(AMDGPU_MAX_RINGS); 2389 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) 2390 adev->vm_manager.seqno[i] = 0; 2391 2392 mtx_init(&adev->vm_manager.prt_lock, IPL_TTY); 2393 atomic_set(&adev->vm_manager.num_prt_users, 0); 2394 2395 /* If not overridden by the user, by default, only in large BAR systems 2396 * Compute VM tables will be updated by CPU 2397 */ 2398 #ifdef CONFIG_X86_64 2399 if (amdgpu_vm_update_mode == -1) { 2400 /* For asic with VF MMIO access protection 2401 * avoid using CPU for VM table updates 2402 */ 2403 if (amdgpu_gmc_vram_full_visible(&adev->gmc) && 2404 !amdgpu_sriov_vf_mmio_access_protection(adev)) 2405 adev->vm_manager.vm_update_mode = 2406 AMDGPU_VM_USE_CPU_FOR_COMPUTE; 2407 else 2408 adev->vm_manager.vm_update_mode = 0; 2409 } else 2410 adev->vm_manager.vm_update_mode = amdgpu_vm_update_mode; 2411 #else 2412 adev->vm_manager.vm_update_mode = 0; 2413 #endif 2414 2415 xa_init_flags(&adev->vm_manager.pasids, XA_FLAGS_LOCK_IRQ); 2416 } 2417 2418 /** 2419 * amdgpu_vm_manager_fini - cleanup VM manager 2420 * 2421 * @adev: amdgpu_device pointer 2422 * 2423 * Cleanup the VM manager and free resources. 2424 */ 2425 void amdgpu_vm_manager_fini(struct amdgpu_device *adev) 2426 { 2427 WARN_ON(!xa_empty(&adev->vm_manager.pasids)); 2428 xa_destroy(&adev->vm_manager.pasids); 2429 2430 amdgpu_vmid_mgr_fini(adev); 2431 } 2432 2433 /** 2434 * amdgpu_vm_ioctl - Manages VMID reservation for vm hubs. 2435 * 2436 * @dev: drm device pointer 2437 * @data: drm_amdgpu_vm 2438 * @filp: drm file pointer 2439 * 2440 * Returns: 2441 * 0 for success, -errno for errors. 2442 */ 2443 int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp) 2444 { 2445 union drm_amdgpu_vm *args = data; 2446 struct amdgpu_device *adev = drm_to_adev(dev); 2447 struct amdgpu_fpriv *fpriv = filp->driver_priv; 2448 long timeout = msecs_to_jiffies(2000); 2449 int r; 2450 2451 /* No valid flags defined yet */ 2452 if (args->in.flags) 2453 return -EINVAL; 2454 2455 switch (args->in.op) { 2456 case AMDGPU_VM_OP_RESERVE_VMID: 2457 /* We only have requirement to reserve vmid from gfxhub */ 2458 r = amdgpu_vmid_alloc_reserved(adev, &fpriv->vm, 2459 AMDGPU_GFXHUB_0); 2460 if (r) 2461 return r; 2462 break; 2463 case AMDGPU_VM_OP_UNRESERVE_VMID: 2464 if (amdgpu_sriov_runtime(adev)) 2465 timeout = 8 * timeout; 2466 2467 /* Wait vm idle to make sure the vmid set in SPM_VMID is 2468 * not referenced anymore. 2469 */ 2470 r = amdgpu_bo_reserve(fpriv->vm.root.bo, true); 2471 if (r) 2472 return r; 2473 2474 r = amdgpu_vm_wait_idle(&fpriv->vm, timeout); 2475 if (r < 0) 2476 return r; 2477 2478 amdgpu_bo_unreserve(fpriv->vm.root.bo); 2479 amdgpu_vmid_free_reserved(adev, &fpriv->vm, AMDGPU_GFXHUB_0); 2480 break; 2481 default: 2482 return -EINVAL; 2483 } 2484 2485 return 0; 2486 } 2487 2488 /** 2489 * amdgpu_vm_get_task_info - Extracts task info for a PASID. 2490 * 2491 * @adev: drm device pointer 2492 * @pasid: PASID identifier for VM 2493 * @task_info: task_info to fill. 2494 */ 2495 void amdgpu_vm_get_task_info(struct amdgpu_device *adev, u32 pasid, 2496 struct amdgpu_task_info *task_info) 2497 { 2498 struct amdgpu_vm *vm; 2499 unsigned long flags; 2500 2501 xa_lock_irqsave(&adev->vm_manager.pasids, flags); 2502 2503 vm = xa_load(&adev->vm_manager.pasids, pasid); 2504 if (vm) 2505 *task_info = vm->task_info; 2506 2507 xa_unlock_irqrestore(&adev->vm_manager.pasids, flags); 2508 } 2509 2510 /** 2511 * amdgpu_vm_set_task_info - Sets VMs task info. 2512 * 2513 * @vm: vm for which to set the info 2514 */ 2515 void amdgpu_vm_set_task_info(struct amdgpu_vm *vm) 2516 { 2517 if (vm->task_info.pid) 2518 return; 2519 2520 #ifdef __linux__ 2521 vm->task_info.pid = current->pid; 2522 get_task_comm(vm->task_info.task_name, current); 2523 2524 if (current->group_leader->mm != current->mm) 2525 return; 2526 2527 vm->task_info.tgid = current->group_leader->pid; 2528 get_task_comm(vm->task_info.process_name, current->group_leader); 2529 #else 2530 /* thread */ 2531 vm->task_info.pid = curproc->p_tid; 2532 strlcpy(vm->task_info.task_name, curproc->p_p->ps_comm, 2533 sizeof(vm->task_info.task_name)); 2534 2535 /* process */ 2536 vm->task_info.tgid = curproc->p_p->ps_pid; 2537 strlcpy(vm->task_info.process_name, curproc->p_p->ps_comm, 2538 sizeof(vm->task_info.process_name)); 2539 #endif 2540 } 2541 2542 /** 2543 * amdgpu_vm_handle_fault - graceful handling of VM faults. 2544 * @adev: amdgpu device pointer 2545 * @pasid: PASID of the VM 2546 * @addr: Address of the fault 2547 * @write_fault: true is write fault, false is read fault 2548 * 2549 * Try to gracefully handle a VM fault. Return true if the fault was handled and 2550 * shouldn't be reported any more. 2551 */ 2552 bool amdgpu_vm_handle_fault(struct amdgpu_device *adev, u32 pasid, 2553 uint64_t addr, bool write_fault) 2554 { 2555 bool is_compute_context = false; 2556 struct amdgpu_bo *root; 2557 unsigned long irqflags; 2558 uint64_t value, flags; 2559 struct amdgpu_vm *vm; 2560 int r; 2561 2562 xa_lock_irqsave(&adev->vm_manager.pasids, irqflags); 2563 vm = xa_load(&adev->vm_manager.pasids, pasid); 2564 if (vm) { 2565 root = amdgpu_bo_ref(vm->root.bo); 2566 is_compute_context = vm->is_compute_context; 2567 } else { 2568 root = NULL; 2569 } 2570 xa_unlock_irqrestore(&adev->vm_manager.pasids, irqflags); 2571 2572 if (!root) 2573 return false; 2574 2575 addr /= AMDGPU_GPU_PAGE_SIZE; 2576 2577 if (is_compute_context && 2578 !svm_range_restore_pages(adev, pasid, addr, write_fault)) { 2579 amdgpu_bo_unref(&root); 2580 return true; 2581 } 2582 2583 r = amdgpu_bo_reserve(root, true); 2584 if (r) 2585 goto error_unref; 2586 2587 /* Double check that the VM still exists */ 2588 xa_lock_irqsave(&adev->vm_manager.pasids, irqflags); 2589 vm = xa_load(&adev->vm_manager.pasids, pasid); 2590 if (vm && vm->root.bo != root) 2591 vm = NULL; 2592 xa_unlock_irqrestore(&adev->vm_manager.pasids, irqflags); 2593 if (!vm) 2594 goto error_unlock; 2595 2596 flags = AMDGPU_PTE_VALID | AMDGPU_PTE_SNOOPED | 2597 AMDGPU_PTE_SYSTEM; 2598 2599 if (is_compute_context) { 2600 /* Intentionally setting invalid PTE flag 2601 * combination to force a no-retry-fault 2602 */ 2603 flags = AMDGPU_PTE_SNOOPED | AMDGPU_PTE_PRT; 2604 value = 0; 2605 } else if (amdgpu_vm_fault_stop == AMDGPU_VM_FAULT_STOP_NEVER) { 2606 /* Redirect the access to the dummy page */ 2607 value = adev->dummy_page_addr; 2608 flags |= AMDGPU_PTE_EXECUTABLE | AMDGPU_PTE_READABLE | 2609 AMDGPU_PTE_WRITEABLE; 2610 2611 } else { 2612 /* Let the hw retry silently on the PTE */ 2613 value = 0; 2614 } 2615 2616 r = dma_resv_reserve_fences(root->tbo.base.resv, 1); 2617 if (r) { 2618 pr_debug("failed %d to reserve fence slot\n", r); 2619 goto error_unlock; 2620 } 2621 2622 r = amdgpu_vm_update_range(adev, vm, true, false, false, NULL, addr, 2623 addr, flags, value, 0, NULL, NULL, NULL); 2624 if (r) 2625 goto error_unlock; 2626 2627 r = amdgpu_vm_update_pdes(adev, vm, true); 2628 2629 error_unlock: 2630 amdgpu_bo_unreserve(root); 2631 if (r < 0) 2632 DRM_ERROR("Can't handle page fault (%d)\n", r); 2633 2634 error_unref: 2635 amdgpu_bo_unref(&root); 2636 2637 return false; 2638 } 2639 2640 #if defined(CONFIG_DEBUG_FS) 2641 /** 2642 * amdgpu_debugfs_vm_bo_info - print BO info for the VM 2643 * 2644 * @vm: Requested VM for printing BO info 2645 * @m: debugfs file 2646 * 2647 * Print BO information in debugfs file for the VM 2648 */ 2649 void amdgpu_debugfs_vm_bo_info(struct amdgpu_vm *vm, struct seq_file *m) 2650 { 2651 struct amdgpu_bo_va *bo_va, *tmp; 2652 u64 total_idle = 0; 2653 u64 total_evicted = 0; 2654 u64 total_relocated = 0; 2655 u64 total_moved = 0; 2656 u64 total_invalidated = 0; 2657 u64 total_done = 0; 2658 unsigned int total_idle_objs = 0; 2659 unsigned int total_evicted_objs = 0; 2660 unsigned int total_relocated_objs = 0; 2661 unsigned int total_moved_objs = 0; 2662 unsigned int total_invalidated_objs = 0; 2663 unsigned int total_done_objs = 0; 2664 unsigned int id = 0; 2665 2666 spin_lock(&vm->status_lock); 2667 seq_puts(m, "\tIdle BOs:\n"); 2668 list_for_each_entry_safe(bo_va, tmp, &vm->idle, base.vm_status) { 2669 if (!bo_va->base.bo) 2670 continue; 2671 total_idle += amdgpu_bo_print_info(id++, bo_va->base.bo, m); 2672 } 2673 total_idle_objs = id; 2674 id = 0; 2675 2676 seq_puts(m, "\tEvicted BOs:\n"); 2677 list_for_each_entry_safe(bo_va, tmp, &vm->evicted, base.vm_status) { 2678 if (!bo_va->base.bo) 2679 continue; 2680 total_evicted += amdgpu_bo_print_info(id++, bo_va->base.bo, m); 2681 } 2682 total_evicted_objs = id; 2683 id = 0; 2684 2685 seq_puts(m, "\tRelocated BOs:\n"); 2686 list_for_each_entry_safe(bo_va, tmp, &vm->relocated, base.vm_status) { 2687 if (!bo_va->base.bo) 2688 continue; 2689 total_relocated += amdgpu_bo_print_info(id++, bo_va->base.bo, m); 2690 } 2691 total_relocated_objs = id; 2692 id = 0; 2693 2694 seq_puts(m, "\tMoved BOs:\n"); 2695 list_for_each_entry_safe(bo_va, tmp, &vm->moved, base.vm_status) { 2696 if (!bo_va->base.bo) 2697 continue; 2698 total_moved += amdgpu_bo_print_info(id++, bo_va->base.bo, m); 2699 } 2700 total_moved_objs = id; 2701 id = 0; 2702 2703 seq_puts(m, "\tInvalidated BOs:\n"); 2704 list_for_each_entry_safe(bo_va, tmp, &vm->invalidated, base.vm_status) { 2705 if (!bo_va->base.bo) 2706 continue; 2707 total_invalidated += amdgpu_bo_print_info(id++, bo_va->base.bo, m); 2708 } 2709 total_invalidated_objs = id; 2710 id = 0; 2711 2712 seq_puts(m, "\tDone BOs:\n"); 2713 list_for_each_entry_safe(bo_va, tmp, &vm->done, base.vm_status) { 2714 if (!bo_va->base.bo) 2715 continue; 2716 total_done += amdgpu_bo_print_info(id++, bo_va->base.bo, m); 2717 } 2718 spin_unlock(&vm->status_lock); 2719 total_done_objs = id; 2720 2721 seq_printf(m, "\tTotal idle size: %12lld\tobjs:\t%d\n", total_idle, 2722 total_idle_objs); 2723 seq_printf(m, "\tTotal evicted size: %12lld\tobjs:\t%d\n", total_evicted, 2724 total_evicted_objs); 2725 seq_printf(m, "\tTotal relocated size: %12lld\tobjs:\t%d\n", total_relocated, 2726 total_relocated_objs); 2727 seq_printf(m, "\tTotal moved size: %12lld\tobjs:\t%d\n", total_moved, 2728 total_moved_objs); 2729 seq_printf(m, "\tTotal invalidated size: %12lld\tobjs:\t%d\n", total_invalidated, 2730 total_invalidated_objs); 2731 seq_printf(m, "\tTotal done size: %12lld\tobjs:\t%d\n", total_done, 2732 total_done_objs); 2733 } 2734 #endif 2735