1 /* 2 * Copyright 2008 Advanced Micro Devices, Inc. 3 * Copyright 2008 Red Hat Inc. 4 * Copyright 2009 Jerome Glisse. 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the "Software"), 8 * to deal in the Software without restriction, including without limitation 9 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 * and/or sell copies of the Software, and to permit persons to whom the 11 * Software is furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be included in 14 * all copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 * OTHER DEALINGS IN THE SOFTWARE. 23 * 24 * Authors: Dave Airlie 25 * Alex Deucher 26 * Jerome Glisse 27 */ 28 29 #include <linux/dma-fence-array.h> 30 #include <linux/interval_tree_generic.h> 31 #include <linux/idr.h> 32 #include <linux/dma-buf.h> 33 34 #include <drm/amdgpu_drm.h> 35 #include <drm/drm_drv.h> 36 #include "amdgpu.h" 37 #include "amdgpu_trace.h" 38 #include "amdgpu_amdkfd.h" 39 #include "amdgpu_gmc.h" 40 #include "amdgpu_xgmi.h" 41 #include "amdgpu_dma_buf.h" 42 #include "amdgpu_res_cursor.h" 43 #include "kfd_svm.h" 44 45 /** 46 * DOC: GPUVM 47 * 48 * GPUVM is similar to the legacy gart on older asics, however 49 * rather than there being a single global gart table 50 * for the entire GPU, there are multiple VM page tables active 51 * at any given time. The VM page tables can contain a mix 52 * vram pages and system memory pages and system memory pages 53 * can be mapped as snooped (cached system pages) or unsnooped 54 * (uncached system pages). 55 * Each VM has an ID associated with it and there is a page table 56 * associated with each VMID. When execting a command buffer, 57 * the kernel tells the the ring what VMID to use for that command 58 * buffer. VMIDs are allocated dynamically as commands are submitted. 59 * The userspace drivers maintain their own address space and the kernel 60 * sets up their pages tables accordingly when they submit their 61 * command buffers and a VMID is assigned. 62 * Cayman/Trinity support up to 8 active VMs at any given time; 63 * SI supports 16. 64 */ 65 66 #define START(node) ((node)->start) 67 #define LAST(node) ((node)->last) 68 69 INTERVAL_TREE_DEFINE(struct amdgpu_bo_va_mapping, rb, uint64_t, __subtree_last, 70 START, LAST, static, amdgpu_vm_it) 71 72 #undef START 73 #undef LAST 74 75 /** 76 * struct amdgpu_prt_cb - Helper to disable partial resident texture feature from a fence callback 77 */ 78 struct amdgpu_prt_cb { 79 80 /** 81 * @adev: amdgpu device 82 */ 83 struct amdgpu_device *adev; 84 85 /** 86 * @cb: callback 87 */ 88 struct dma_fence_cb cb; 89 }; 90 91 /* 92 * vm eviction_lock can be taken in MMU notifiers. Make sure no reclaim-FS 93 * happens while holding this lock anywhere to prevent deadlocks when 94 * an MMU notifier runs in reclaim-FS context. 95 */ 96 static inline void amdgpu_vm_eviction_lock(struct amdgpu_vm *vm) 97 { 98 mutex_lock(&vm->eviction_lock); 99 vm->saved_flags = memalloc_noreclaim_save(); 100 } 101 102 static inline int amdgpu_vm_eviction_trylock(struct amdgpu_vm *vm) 103 { 104 if (mutex_trylock(&vm->eviction_lock)) { 105 vm->saved_flags = memalloc_noreclaim_save(); 106 return 1; 107 } 108 return 0; 109 } 110 111 static inline void amdgpu_vm_eviction_unlock(struct amdgpu_vm *vm) 112 { 113 memalloc_noreclaim_restore(vm->saved_flags); 114 mutex_unlock(&vm->eviction_lock); 115 } 116 117 /** 118 * amdgpu_vm_level_shift - return the addr shift for each level 119 * 120 * @adev: amdgpu_device pointer 121 * @level: VMPT level 122 * 123 * Returns: 124 * The number of bits the pfn needs to be right shifted for a level. 125 */ 126 static unsigned amdgpu_vm_level_shift(struct amdgpu_device *adev, 127 unsigned level) 128 { 129 switch (level) { 130 case AMDGPU_VM_PDB2: 131 case AMDGPU_VM_PDB1: 132 case AMDGPU_VM_PDB0: 133 return 9 * (AMDGPU_VM_PDB0 - level) + 134 adev->vm_manager.block_size; 135 case AMDGPU_VM_PTB: 136 return 0; 137 default: 138 return ~0; 139 } 140 } 141 142 /** 143 * amdgpu_vm_num_entries - return the number of entries in a PD/PT 144 * 145 * @adev: amdgpu_device pointer 146 * @level: VMPT level 147 * 148 * Returns: 149 * The number of entries in a page directory or page table. 150 */ 151 static unsigned amdgpu_vm_num_entries(struct amdgpu_device *adev, 152 unsigned level) 153 { 154 unsigned shift = amdgpu_vm_level_shift(adev, 155 adev->vm_manager.root_level); 156 157 if (level == adev->vm_manager.root_level) 158 /* For the root directory */ 159 return round_up(adev->vm_manager.max_pfn, 1ULL << shift) 160 >> shift; 161 else if (level != AMDGPU_VM_PTB) 162 /* Everything in between */ 163 return 512; 164 else 165 /* For the page tables on the leaves */ 166 return AMDGPU_VM_PTE_COUNT(adev); 167 } 168 169 /** 170 * amdgpu_vm_num_ats_entries - return the number of ATS entries in the root PD 171 * 172 * @adev: amdgpu_device pointer 173 * 174 * Returns: 175 * The number of entries in the root page directory which needs the ATS setting. 176 */ 177 static unsigned amdgpu_vm_num_ats_entries(struct amdgpu_device *adev) 178 { 179 unsigned shift; 180 181 shift = amdgpu_vm_level_shift(adev, adev->vm_manager.root_level); 182 return AMDGPU_GMC_HOLE_START >> (shift + AMDGPU_GPU_PAGE_SHIFT); 183 } 184 185 /** 186 * amdgpu_vm_entries_mask - the mask to get the entry number of a PD/PT 187 * 188 * @adev: amdgpu_device pointer 189 * @level: VMPT level 190 * 191 * Returns: 192 * The mask to extract the entry number of a PD/PT from an address. 193 */ 194 static uint32_t amdgpu_vm_entries_mask(struct amdgpu_device *adev, 195 unsigned int level) 196 { 197 if (level <= adev->vm_manager.root_level) 198 return 0xffffffff; 199 else if (level != AMDGPU_VM_PTB) 200 return 0x1ff; 201 else 202 return AMDGPU_VM_PTE_COUNT(adev) - 1; 203 } 204 205 /** 206 * amdgpu_vm_bo_size - returns the size of the BOs in bytes 207 * 208 * @adev: amdgpu_device pointer 209 * @level: VMPT level 210 * 211 * Returns: 212 * The size of the BO for a page directory or page table in bytes. 213 */ 214 static unsigned amdgpu_vm_bo_size(struct amdgpu_device *adev, unsigned level) 215 { 216 return AMDGPU_GPU_PAGE_ALIGN(amdgpu_vm_num_entries(adev, level) * 8); 217 } 218 219 /** 220 * amdgpu_vm_bo_evicted - vm_bo is evicted 221 * 222 * @vm_bo: vm_bo which is evicted 223 * 224 * State for PDs/PTs and per VM BOs which are not at the location they should 225 * be. 226 */ 227 static void amdgpu_vm_bo_evicted(struct amdgpu_vm_bo_base *vm_bo) 228 { 229 struct amdgpu_vm *vm = vm_bo->vm; 230 struct amdgpu_bo *bo = vm_bo->bo; 231 232 vm_bo->moved = true; 233 if (bo->tbo.type == ttm_bo_type_kernel) 234 list_move(&vm_bo->vm_status, &vm->evicted); 235 else 236 list_move_tail(&vm_bo->vm_status, &vm->evicted); 237 } 238 /** 239 * amdgpu_vm_bo_moved - vm_bo is moved 240 * 241 * @vm_bo: vm_bo which is moved 242 * 243 * State for per VM BOs which are moved, but that change is not yet reflected 244 * in the page tables. 245 */ 246 static void amdgpu_vm_bo_moved(struct amdgpu_vm_bo_base *vm_bo) 247 { 248 list_move(&vm_bo->vm_status, &vm_bo->vm->moved); 249 } 250 251 /** 252 * amdgpu_vm_bo_idle - vm_bo is idle 253 * 254 * @vm_bo: vm_bo which is now idle 255 * 256 * State for PDs/PTs and per VM BOs which have gone through the state machine 257 * and are now idle. 258 */ 259 static void amdgpu_vm_bo_idle(struct amdgpu_vm_bo_base *vm_bo) 260 { 261 list_move(&vm_bo->vm_status, &vm_bo->vm->idle); 262 vm_bo->moved = false; 263 } 264 265 /** 266 * amdgpu_vm_bo_invalidated - vm_bo is invalidated 267 * 268 * @vm_bo: vm_bo which is now invalidated 269 * 270 * State for normal BOs which are invalidated and that change not yet reflected 271 * in the PTs. 272 */ 273 static void amdgpu_vm_bo_invalidated(struct amdgpu_vm_bo_base *vm_bo) 274 { 275 spin_lock(&vm_bo->vm->invalidated_lock); 276 list_move(&vm_bo->vm_status, &vm_bo->vm->invalidated); 277 spin_unlock(&vm_bo->vm->invalidated_lock); 278 } 279 280 /** 281 * amdgpu_vm_bo_relocated - vm_bo is reloacted 282 * 283 * @vm_bo: vm_bo which is relocated 284 * 285 * State for PDs/PTs which needs to update their parent PD. 286 * For the root PD, just move to idle state. 287 */ 288 static void amdgpu_vm_bo_relocated(struct amdgpu_vm_bo_base *vm_bo) 289 { 290 if (vm_bo->bo->parent) 291 list_move(&vm_bo->vm_status, &vm_bo->vm->relocated); 292 else 293 amdgpu_vm_bo_idle(vm_bo); 294 } 295 296 /** 297 * amdgpu_vm_bo_done - vm_bo is done 298 * 299 * @vm_bo: vm_bo which is now done 300 * 301 * State for normal BOs which are invalidated and that change has been updated 302 * in the PTs. 303 */ 304 static void amdgpu_vm_bo_done(struct amdgpu_vm_bo_base *vm_bo) 305 { 306 spin_lock(&vm_bo->vm->invalidated_lock); 307 list_move(&vm_bo->vm_status, &vm_bo->vm->done); 308 spin_unlock(&vm_bo->vm->invalidated_lock); 309 } 310 311 /** 312 * amdgpu_vm_bo_base_init - Adds bo to the list of bos associated with the vm 313 * 314 * @base: base structure for tracking BO usage in a VM 315 * @vm: vm to which bo is to be added 316 * @bo: amdgpu buffer object 317 * 318 * Initialize a bo_va_base structure and add it to the appropriate lists 319 * 320 */ 321 static void amdgpu_vm_bo_base_init(struct amdgpu_vm_bo_base *base, 322 struct amdgpu_vm *vm, 323 struct amdgpu_bo *bo) 324 { 325 base->vm = vm; 326 base->bo = bo; 327 base->next = NULL; 328 INIT_LIST_HEAD(&base->vm_status); 329 330 if (!bo) 331 return; 332 base->next = bo->vm_bo; 333 bo->vm_bo = base; 334 335 if (bo->tbo.base.resv != vm->root.base.bo->tbo.base.resv) 336 return; 337 338 vm->bulk_moveable = false; 339 if (bo->tbo.type == ttm_bo_type_kernel && bo->parent) 340 amdgpu_vm_bo_relocated(base); 341 else 342 amdgpu_vm_bo_idle(base); 343 344 if (bo->preferred_domains & 345 amdgpu_mem_type_to_domain(bo->tbo.resource->mem_type)) 346 return; 347 348 /* 349 * we checked all the prerequisites, but it looks like this per vm bo 350 * is currently evicted. add the bo to the evicted list to make sure it 351 * is validated on next vm use to avoid fault. 352 * */ 353 amdgpu_vm_bo_evicted(base); 354 } 355 356 /** 357 * amdgpu_vm_pt_parent - get the parent page directory 358 * 359 * @pt: child page table 360 * 361 * Helper to get the parent entry for the child page table. NULL if we are at 362 * the root page directory. 363 */ 364 static struct amdgpu_vm_pt *amdgpu_vm_pt_parent(struct amdgpu_vm_pt *pt) 365 { 366 struct amdgpu_bo *parent = pt->base.bo->parent; 367 368 if (!parent) 369 return NULL; 370 371 return container_of(parent->vm_bo, struct amdgpu_vm_pt, base); 372 } 373 374 /* 375 * amdgpu_vm_pt_cursor - state for for_each_amdgpu_vm_pt 376 */ 377 struct amdgpu_vm_pt_cursor { 378 uint64_t pfn; 379 struct amdgpu_vm_pt *parent; 380 struct amdgpu_vm_pt *entry; 381 unsigned level; 382 }; 383 384 /** 385 * amdgpu_vm_pt_start - start PD/PT walk 386 * 387 * @adev: amdgpu_device pointer 388 * @vm: amdgpu_vm structure 389 * @start: start address of the walk 390 * @cursor: state to initialize 391 * 392 * Initialize a amdgpu_vm_pt_cursor to start a walk. 393 */ 394 static void amdgpu_vm_pt_start(struct amdgpu_device *adev, 395 struct amdgpu_vm *vm, uint64_t start, 396 struct amdgpu_vm_pt_cursor *cursor) 397 { 398 cursor->pfn = start; 399 cursor->parent = NULL; 400 cursor->entry = &vm->root; 401 cursor->level = adev->vm_manager.root_level; 402 } 403 404 /** 405 * amdgpu_vm_pt_descendant - go to child node 406 * 407 * @adev: amdgpu_device pointer 408 * @cursor: current state 409 * 410 * Walk to the child node of the current node. 411 * Returns: 412 * True if the walk was possible, false otherwise. 413 */ 414 static bool amdgpu_vm_pt_descendant(struct amdgpu_device *adev, 415 struct amdgpu_vm_pt_cursor *cursor) 416 { 417 unsigned mask, shift, idx; 418 419 if (!cursor->entry->entries) 420 return false; 421 422 BUG_ON(!cursor->entry->base.bo); 423 mask = amdgpu_vm_entries_mask(adev, cursor->level); 424 shift = amdgpu_vm_level_shift(adev, cursor->level); 425 426 ++cursor->level; 427 idx = (cursor->pfn >> shift) & mask; 428 cursor->parent = cursor->entry; 429 cursor->entry = &cursor->entry->entries[idx]; 430 return true; 431 } 432 433 /** 434 * amdgpu_vm_pt_sibling - go to sibling node 435 * 436 * @adev: amdgpu_device pointer 437 * @cursor: current state 438 * 439 * Walk to the sibling node of the current node. 440 * Returns: 441 * True if the walk was possible, false otherwise. 442 */ 443 static bool amdgpu_vm_pt_sibling(struct amdgpu_device *adev, 444 struct amdgpu_vm_pt_cursor *cursor) 445 { 446 unsigned shift, num_entries; 447 448 /* Root doesn't have a sibling */ 449 if (!cursor->parent) 450 return false; 451 452 /* Go to our parents and see if we got a sibling */ 453 shift = amdgpu_vm_level_shift(adev, cursor->level - 1); 454 num_entries = amdgpu_vm_num_entries(adev, cursor->level - 1); 455 456 if (cursor->entry == &cursor->parent->entries[num_entries - 1]) 457 return false; 458 459 cursor->pfn += 1ULL << shift; 460 cursor->pfn &= ~((1ULL << shift) - 1); 461 ++cursor->entry; 462 return true; 463 } 464 465 /** 466 * amdgpu_vm_pt_ancestor - go to parent node 467 * 468 * @cursor: current state 469 * 470 * Walk to the parent node of the current node. 471 * Returns: 472 * True if the walk was possible, false otherwise. 473 */ 474 static bool amdgpu_vm_pt_ancestor(struct amdgpu_vm_pt_cursor *cursor) 475 { 476 if (!cursor->parent) 477 return false; 478 479 --cursor->level; 480 cursor->entry = cursor->parent; 481 cursor->parent = amdgpu_vm_pt_parent(cursor->parent); 482 return true; 483 } 484 485 /** 486 * amdgpu_vm_pt_next - get next PD/PT in hieratchy 487 * 488 * @adev: amdgpu_device pointer 489 * @cursor: current state 490 * 491 * Walk the PD/PT tree to the next node. 492 */ 493 static void amdgpu_vm_pt_next(struct amdgpu_device *adev, 494 struct amdgpu_vm_pt_cursor *cursor) 495 { 496 /* First try a newborn child */ 497 if (amdgpu_vm_pt_descendant(adev, cursor)) 498 return; 499 500 /* If that didn't worked try to find a sibling */ 501 while (!amdgpu_vm_pt_sibling(adev, cursor)) { 502 /* No sibling, go to our parents and grandparents */ 503 if (!amdgpu_vm_pt_ancestor(cursor)) { 504 cursor->pfn = ~0ll; 505 return; 506 } 507 } 508 } 509 510 /** 511 * amdgpu_vm_pt_first_dfs - start a deep first search 512 * 513 * @adev: amdgpu_device structure 514 * @vm: amdgpu_vm structure 515 * @start: optional cursor to start with 516 * @cursor: state to initialize 517 * 518 * Starts a deep first traversal of the PD/PT tree. 519 */ 520 static void amdgpu_vm_pt_first_dfs(struct amdgpu_device *adev, 521 struct amdgpu_vm *vm, 522 struct amdgpu_vm_pt_cursor *start, 523 struct amdgpu_vm_pt_cursor *cursor) 524 { 525 if (start) 526 *cursor = *start; 527 else 528 amdgpu_vm_pt_start(adev, vm, 0, cursor); 529 while (amdgpu_vm_pt_descendant(adev, cursor)); 530 } 531 532 /** 533 * amdgpu_vm_pt_continue_dfs - check if the deep first search should continue 534 * 535 * @start: starting point for the search 536 * @entry: current entry 537 * 538 * Returns: 539 * True when the search should continue, false otherwise. 540 */ 541 static bool amdgpu_vm_pt_continue_dfs(struct amdgpu_vm_pt_cursor *start, 542 struct amdgpu_vm_pt *entry) 543 { 544 return entry && (!start || entry != start->entry); 545 } 546 547 /** 548 * amdgpu_vm_pt_next_dfs - get the next node for a deep first search 549 * 550 * @adev: amdgpu_device structure 551 * @cursor: current state 552 * 553 * Move the cursor to the next node in a deep first search. 554 */ 555 static void amdgpu_vm_pt_next_dfs(struct amdgpu_device *adev, 556 struct amdgpu_vm_pt_cursor *cursor) 557 { 558 if (!cursor->entry) 559 return; 560 561 if (!cursor->parent) 562 cursor->entry = NULL; 563 else if (amdgpu_vm_pt_sibling(adev, cursor)) 564 while (amdgpu_vm_pt_descendant(adev, cursor)); 565 else 566 amdgpu_vm_pt_ancestor(cursor); 567 } 568 569 /* 570 * for_each_amdgpu_vm_pt_dfs_safe - safe deep first search of all PDs/PTs 571 */ 572 #define for_each_amdgpu_vm_pt_dfs_safe(adev, vm, start, cursor, entry) \ 573 for (amdgpu_vm_pt_first_dfs((adev), (vm), (start), &(cursor)), \ 574 (entry) = (cursor).entry, amdgpu_vm_pt_next_dfs((adev), &(cursor));\ 575 amdgpu_vm_pt_continue_dfs((start), (entry)); \ 576 (entry) = (cursor).entry, amdgpu_vm_pt_next_dfs((adev), &(cursor))) 577 578 /** 579 * amdgpu_vm_get_pd_bo - add the VM PD to a validation list 580 * 581 * @vm: vm providing the BOs 582 * @validated: head of validation list 583 * @entry: entry to add 584 * 585 * Add the page directory to the list of BOs to 586 * validate for command submission. 587 */ 588 void amdgpu_vm_get_pd_bo(struct amdgpu_vm *vm, 589 struct list_head *validated, 590 struct amdgpu_bo_list_entry *entry) 591 { 592 entry->priority = 0; 593 entry->tv.bo = &vm->root.base.bo->tbo; 594 /* Two for VM updates, one for TTM and one for the CS job */ 595 entry->tv.num_shared = 4; 596 entry->user_pages = NULL; 597 list_add(&entry->tv.head, validated); 598 } 599 600 /** 601 * amdgpu_vm_del_from_lru_notify - update bulk_moveable flag 602 * 603 * @bo: BO which was removed from the LRU 604 * 605 * Make sure the bulk_moveable flag is updated when a BO is removed from the 606 * LRU. 607 */ 608 void amdgpu_vm_del_from_lru_notify(struct ttm_buffer_object *bo) 609 { 610 struct amdgpu_bo *abo; 611 struct amdgpu_vm_bo_base *bo_base; 612 613 if (!amdgpu_bo_is_amdgpu_bo(bo)) 614 return; 615 616 if (bo->pin_count) 617 return; 618 619 abo = ttm_to_amdgpu_bo(bo); 620 if (!abo->parent) 621 return; 622 for (bo_base = abo->vm_bo; bo_base; bo_base = bo_base->next) { 623 struct amdgpu_vm *vm = bo_base->vm; 624 625 if (abo->tbo.base.resv == vm->root.base.bo->tbo.base.resv) 626 vm->bulk_moveable = false; 627 } 628 629 } 630 /** 631 * amdgpu_vm_move_to_lru_tail - move all BOs to the end of LRU 632 * 633 * @adev: amdgpu device pointer 634 * @vm: vm providing the BOs 635 * 636 * Move all BOs to the end of LRU and remember their positions to put them 637 * together. 638 */ 639 void amdgpu_vm_move_to_lru_tail(struct amdgpu_device *adev, 640 struct amdgpu_vm *vm) 641 { 642 struct amdgpu_vm_bo_base *bo_base; 643 644 if (vm->bulk_moveable) { 645 spin_lock(&adev->mman.bdev.lru_lock); 646 ttm_bo_bulk_move_lru_tail(&vm->lru_bulk_move); 647 spin_unlock(&adev->mman.bdev.lru_lock); 648 return; 649 } 650 651 memset(&vm->lru_bulk_move, 0, sizeof(vm->lru_bulk_move)); 652 653 spin_lock(&adev->mman.bdev.lru_lock); 654 list_for_each_entry(bo_base, &vm->idle, vm_status) { 655 struct amdgpu_bo *bo = bo_base->bo; 656 657 if (!bo->parent) 658 continue; 659 660 ttm_bo_move_to_lru_tail(&bo->tbo, bo->tbo.resource, 661 &vm->lru_bulk_move); 662 if (bo->shadow) 663 ttm_bo_move_to_lru_tail(&bo->shadow->tbo, 664 bo->shadow->tbo.resource, 665 &vm->lru_bulk_move); 666 } 667 spin_unlock(&adev->mman.bdev.lru_lock); 668 669 vm->bulk_moveable = true; 670 } 671 672 /** 673 * amdgpu_vm_validate_pt_bos - validate the page table BOs 674 * 675 * @adev: amdgpu device pointer 676 * @vm: vm providing the BOs 677 * @validate: callback to do the validation 678 * @param: parameter for the validation callback 679 * 680 * Validate the page table BOs on command submission if neccessary. 681 * 682 * Returns: 683 * Validation result. 684 */ 685 int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm, 686 int (*validate)(void *p, struct amdgpu_bo *bo), 687 void *param) 688 { 689 struct amdgpu_vm_bo_base *bo_base, *tmp; 690 int r; 691 692 vm->bulk_moveable &= list_empty(&vm->evicted); 693 694 list_for_each_entry_safe(bo_base, tmp, &vm->evicted, vm_status) { 695 struct amdgpu_bo *bo = bo_base->bo; 696 697 r = validate(param, bo); 698 if (r) 699 return r; 700 701 if (bo->tbo.type != ttm_bo_type_kernel) { 702 amdgpu_vm_bo_moved(bo_base); 703 } else { 704 vm->update_funcs->map_table(bo); 705 amdgpu_vm_bo_relocated(bo_base); 706 } 707 } 708 709 amdgpu_vm_eviction_lock(vm); 710 vm->evicting = false; 711 amdgpu_vm_eviction_unlock(vm); 712 713 return 0; 714 } 715 716 /** 717 * amdgpu_vm_ready - check VM is ready for updates 718 * 719 * @vm: VM to check 720 * 721 * Check if all VM PDs/PTs are ready for updates 722 * 723 * Returns: 724 * True if eviction list is empty. 725 */ 726 bool amdgpu_vm_ready(struct amdgpu_vm *vm) 727 { 728 return list_empty(&vm->evicted); 729 } 730 731 /** 732 * amdgpu_vm_clear_bo - initially clear the PDs/PTs 733 * 734 * @adev: amdgpu_device pointer 735 * @vm: VM to clear BO from 736 * @bo: BO to clear 737 * @immediate: use an immediate update 738 * 739 * Root PD needs to be reserved when calling this. 740 * 741 * Returns: 742 * 0 on success, errno otherwise. 743 */ 744 static int amdgpu_vm_clear_bo(struct amdgpu_device *adev, 745 struct amdgpu_vm *vm, 746 struct amdgpu_bo *bo, 747 bool immediate) 748 { 749 struct ttm_operation_ctx ctx = { true, false }; 750 unsigned level = adev->vm_manager.root_level; 751 struct amdgpu_vm_update_params params; 752 struct amdgpu_bo *ancestor = bo; 753 unsigned entries, ats_entries; 754 uint64_t addr; 755 int r; 756 757 /* Figure out our place in the hierarchy */ 758 if (ancestor->parent) { 759 ++level; 760 while (ancestor->parent->parent) { 761 ++level; 762 ancestor = ancestor->parent; 763 } 764 } 765 766 entries = amdgpu_bo_size(bo) / 8; 767 if (!vm->pte_support_ats) { 768 ats_entries = 0; 769 770 } else if (!bo->parent) { 771 ats_entries = amdgpu_vm_num_ats_entries(adev); 772 ats_entries = min(ats_entries, entries); 773 entries -= ats_entries; 774 775 } else { 776 struct amdgpu_vm_pt *pt; 777 778 pt = container_of(ancestor->vm_bo, struct amdgpu_vm_pt, base); 779 ats_entries = amdgpu_vm_num_ats_entries(adev); 780 if ((pt - vm->root.entries) >= ats_entries) { 781 ats_entries = 0; 782 } else { 783 ats_entries = entries; 784 entries = 0; 785 } 786 } 787 788 r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx); 789 if (r) 790 return r; 791 792 if (bo->shadow) { 793 r = ttm_bo_validate(&bo->shadow->tbo, &bo->shadow->placement, 794 &ctx); 795 if (r) 796 return r; 797 } 798 799 r = vm->update_funcs->map_table(bo); 800 if (r) 801 return r; 802 803 memset(¶ms, 0, sizeof(params)); 804 params.adev = adev; 805 params.vm = vm; 806 params.immediate = immediate; 807 808 r = vm->update_funcs->prepare(¶ms, NULL, AMDGPU_SYNC_EXPLICIT); 809 if (r) 810 return r; 811 812 addr = 0; 813 if (ats_entries) { 814 uint64_t value = 0, flags; 815 816 flags = AMDGPU_PTE_DEFAULT_ATC; 817 if (level != AMDGPU_VM_PTB) { 818 /* Handle leaf PDEs as PTEs */ 819 flags |= AMDGPU_PDE_PTE; 820 amdgpu_gmc_get_vm_pde(adev, level, &value, &flags); 821 } 822 823 r = vm->update_funcs->update(¶ms, bo, addr, 0, ats_entries, 824 value, flags); 825 if (r) 826 return r; 827 828 addr += ats_entries * 8; 829 } 830 831 if (entries) { 832 uint64_t value = 0, flags = 0; 833 834 if (adev->asic_type >= CHIP_VEGA10) { 835 if (level != AMDGPU_VM_PTB) { 836 /* Handle leaf PDEs as PTEs */ 837 flags |= AMDGPU_PDE_PTE; 838 amdgpu_gmc_get_vm_pde(adev, level, 839 &value, &flags); 840 } else { 841 /* Workaround for fault priority problem on GMC9 */ 842 flags = AMDGPU_PTE_EXECUTABLE; 843 } 844 } 845 846 r = vm->update_funcs->update(¶ms, bo, addr, 0, entries, 847 value, flags); 848 if (r) 849 return r; 850 } 851 852 return vm->update_funcs->commit(¶ms, NULL); 853 } 854 855 /** 856 * amdgpu_vm_pt_create - create bo for PD/PT 857 * 858 * @adev: amdgpu_device pointer 859 * @vm: requesting vm 860 * @level: the page table level 861 * @immediate: use a immediate update 862 * @bo: pointer to the buffer object pointer 863 */ 864 static int amdgpu_vm_pt_create(struct amdgpu_device *adev, 865 struct amdgpu_vm *vm, 866 int level, bool immediate, 867 struct amdgpu_bo **bo) 868 { 869 struct amdgpu_bo_param bp; 870 int r; 871 872 memset(&bp, 0, sizeof(bp)); 873 874 bp.size = amdgpu_vm_bo_size(adev, level); 875 bp.byte_align = AMDGPU_GPU_PAGE_SIZE; 876 bp.domain = AMDGPU_GEM_DOMAIN_VRAM; 877 bp.domain = amdgpu_bo_get_preferred_pin_domain(adev, bp.domain); 878 bp.flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS | 879 AMDGPU_GEM_CREATE_CPU_GTT_USWC; 880 bp.bo_ptr_size = sizeof(struct amdgpu_bo); 881 if (vm->use_cpu_for_update) 882 bp.flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED; 883 884 bp.type = ttm_bo_type_kernel; 885 bp.no_wait_gpu = immediate; 886 if (vm->root.base.bo) 887 bp.resv = vm->root.base.bo->tbo.base.resv; 888 889 r = amdgpu_bo_create(adev, &bp, bo); 890 if (r) 891 return r; 892 893 if (vm->is_compute_context && (adev->flags & AMD_IS_APU)) 894 return 0; 895 896 if (!bp.resv) 897 WARN_ON(dma_resv_lock((*bo)->tbo.base.resv, 898 NULL)); 899 r = amdgpu_bo_create_shadow(adev, bp.size, *bo); 900 901 if (!bp.resv) 902 dma_resv_unlock((*bo)->tbo.base.resv); 903 904 if (r) { 905 amdgpu_bo_unref(bo); 906 return r; 907 } 908 909 return 0; 910 } 911 912 /** 913 * amdgpu_vm_alloc_pts - Allocate a specific page table 914 * 915 * @adev: amdgpu_device pointer 916 * @vm: VM to allocate page tables for 917 * @cursor: Which page table to allocate 918 * @immediate: use an immediate update 919 * 920 * Make sure a specific page table or directory is allocated. 921 * 922 * Returns: 923 * 1 if page table needed to be allocated, 0 if page table was already 924 * allocated, negative errno if an error occurred. 925 */ 926 static int amdgpu_vm_alloc_pts(struct amdgpu_device *adev, 927 struct amdgpu_vm *vm, 928 struct amdgpu_vm_pt_cursor *cursor, 929 bool immediate) 930 { 931 struct amdgpu_vm_pt *entry = cursor->entry; 932 struct amdgpu_bo *pt; 933 int r; 934 935 if (cursor->level < AMDGPU_VM_PTB && !entry->entries) { 936 unsigned num_entries; 937 938 num_entries = amdgpu_vm_num_entries(adev, cursor->level); 939 entry->entries = kvmalloc_array(num_entries, 940 sizeof(*entry->entries), 941 GFP_KERNEL | __GFP_ZERO); 942 if (!entry->entries) 943 return -ENOMEM; 944 } 945 946 if (entry->base.bo) 947 return 0; 948 949 r = amdgpu_vm_pt_create(adev, vm, cursor->level, immediate, &pt); 950 if (r) 951 return r; 952 953 /* Keep a reference to the root directory to avoid 954 * freeing them up in the wrong order. 955 */ 956 pt->parent = amdgpu_bo_ref(cursor->parent->base.bo); 957 amdgpu_vm_bo_base_init(&entry->base, vm, pt); 958 959 r = amdgpu_vm_clear_bo(adev, vm, pt, immediate); 960 if (r) 961 goto error_free_pt; 962 963 return 0; 964 965 error_free_pt: 966 amdgpu_bo_unref(&pt->shadow); 967 amdgpu_bo_unref(&pt); 968 return r; 969 } 970 971 /** 972 * amdgpu_vm_free_table - fre one PD/PT 973 * 974 * @entry: PDE to free 975 */ 976 static void amdgpu_vm_free_table(struct amdgpu_vm_pt *entry) 977 { 978 if (entry->base.bo) { 979 entry->base.bo->vm_bo = NULL; 980 list_del(&entry->base.vm_status); 981 amdgpu_bo_unref(&entry->base.bo->shadow); 982 amdgpu_bo_unref(&entry->base.bo); 983 } 984 kvfree(entry->entries); 985 entry->entries = NULL; 986 } 987 988 /** 989 * amdgpu_vm_free_pts - free PD/PT levels 990 * 991 * @adev: amdgpu device structure 992 * @vm: amdgpu vm structure 993 * @start: optional cursor where to start freeing PDs/PTs 994 * 995 * Free the page directory or page table level and all sub levels. 996 */ 997 static void amdgpu_vm_free_pts(struct amdgpu_device *adev, 998 struct amdgpu_vm *vm, 999 struct amdgpu_vm_pt_cursor *start) 1000 { 1001 struct amdgpu_vm_pt_cursor cursor; 1002 struct amdgpu_vm_pt *entry; 1003 1004 vm->bulk_moveable = false; 1005 1006 for_each_amdgpu_vm_pt_dfs_safe(adev, vm, start, cursor, entry) 1007 amdgpu_vm_free_table(entry); 1008 1009 if (start) 1010 amdgpu_vm_free_table(start->entry); 1011 } 1012 1013 /** 1014 * amdgpu_vm_check_compute_bug - check whether asic has compute vm bug 1015 * 1016 * @adev: amdgpu_device pointer 1017 */ 1018 void amdgpu_vm_check_compute_bug(struct amdgpu_device *adev) 1019 { 1020 const struct amdgpu_ip_block *ip_block; 1021 bool has_compute_vm_bug; 1022 struct amdgpu_ring *ring; 1023 int i; 1024 1025 has_compute_vm_bug = false; 1026 1027 ip_block = amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_GFX); 1028 if (ip_block) { 1029 /* Compute has a VM bug for GFX version < 7. 1030 Compute has a VM bug for GFX 8 MEC firmware version < 673.*/ 1031 if (ip_block->version->major <= 7) 1032 has_compute_vm_bug = true; 1033 else if (ip_block->version->major == 8) 1034 if (adev->gfx.mec_fw_version < 673) 1035 has_compute_vm_bug = true; 1036 } 1037 1038 for (i = 0; i < adev->num_rings; i++) { 1039 ring = adev->rings[i]; 1040 if (ring->funcs->type == AMDGPU_RING_TYPE_COMPUTE) 1041 /* only compute rings */ 1042 ring->has_compute_vm_bug = has_compute_vm_bug; 1043 else 1044 ring->has_compute_vm_bug = false; 1045 } 1046 } 1047 1048 /** 1049 * amdgpu_vm_need_pipeline_sync - Check if pipe sync is needed for job. 1050 * 1051 * @ring: ring on which the job will be submitted 1052 * @job: job to submit 1053 * 1054 * Returns: 1055 * True if sync is needed. 1056 */ 1057 bool amdgpu_vm_need_pipeline_sync(struct amdgpu_ring *ring, 1058 struct amdgpu_job *job) 1059 { 1060 struct amdgpu_device *adev = ring->adev; 1061 unsigned vmhub = ring->funcs->vmhub; 1062 struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub]; 1063 struct amdgpu_vmid *id; 1064 bool gds_switch_needed; 1065 bool vm_flush_needed = job->vm_needs_flush || ring->has_compute_vm_bug; 1066 1067 if (job->vmid == 0) 1068 return false; 1069 id = &id_mgr->ids[job->vmid]; 1070 gds_switch_needed = ring->funcs->emit_gds_switch && ( 1071 id->gds_base != job->gds_base || 1072 id->gds_size != job->gds_size || 1073 id->gws_base != job->gws_base || 1074 id->gws_size != job->gws_size || 1075 id->oa_base != job->oa_base || 1076 id->oa_size != job->oa_size); 1077 1078 if (amdgpu_vmid_had_gpu_reset(adev, id)) 1079 return true; 1080 1081 return vm_flush_needed || gds_switch_needed; 1082 } 1083 1084 /** 1085 * amdgpu_vm_flush - hardware flush the vm 1086 * 1087 * @ring: ring to use for flush 1088 * @job: related job 1089 * @need_pipe_sync: is pipe sync needed 1090 * 1091 * Emit a VM flush when it is necessary. 1092 * 1093 * Returns: 1094 * 0 on success, errno otherwise. 1095 */ 1096 int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job, 1097 bool need_pipe_sync) 1098 { 1099 struct amdgpu_device *adev = ring->adev; 1100 unsigned vmhub = ring->funcs->vmhub; 1101 struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub]; 1102 struct amdgpu_vmid *id = &id_mgr->ids[job->vmid]; 1103 bool gds_switch_needed = ring->funcs->emit_gds_switch && ( 1104 id->gds_base != job->gds_base || 1105 id->gds_size != job->gds_size || 1106 id->gws_base != job->gws_base || 1107 id->gws_size != job->gws_size || 1108 id->oa_base != job->oa_base || 1109 id->oa_size != job->oa_size); 1110 bool vm_flush_needed = job->vm_needs_flush; 1111 struct dma_fence *fence = NULL; 1112 bool pasid_mapping_needed = false; 1113 unsigned patch_offset = 0; 1114 bool update_spm_vmid_needed = (job->vm && (job->vm->reserved_vmid[vmhub] != NULL)); 1115 int r; 1116 1117 if (update_spm_vmid_needed && adev->gfx.rlc.funcs->update_spm_vmid) 1118 adev->gfx.rlc.funcs->update_spm_vmid(adev, job->vmid); 1119 1120 if (amdgpu_vmid_had_gpu_reset(adev, id)) { 1121 gds_switch_needed = true; 1122 vm_flush_needed = true; 1123 pasid_mapping_needed = true; 1124 } 1125 1126 mutex_lock(&id_mgr->lock); 1127 if (id->pasid != job->pasid || !id->pasid_mapping || 1128 !dma_fence_is_signaled(id->pasid_mapping)) 1129 pasid_mapping_needed = true; 1130 mutex_unlock(&id_mgr->lock); 1131 1132 gds_switch_needed &= !!ring->funcs->emit_gds_switch; 1133 vm_flush_needed &= !!ring->funcs->emit_vm_flush && 1134 job->vm_pd_addr != AMDGPU_BO_INVALID_OFFSET; 1135 pasid_mapping_needed &= adev->gmc.gmc_funcs->emit_pasid_mapping && 1136 ring->funcs->emit_wreg; 1137 1138 if (!vm_flush_needed && !gds_switch_needed && !need_pipe_sync) 1139 return 0; 1140 1141 if (ring->funcs->init_cond_exec) 1142 patch_offset = amdgpu_ring_init_cond_exec(ring); 1143 1144 if (need_pipe_sync) 1145 amdgpu_ring_emit_pipeline_sync(ring); 1146 1147 if (vm_flush_needed) { 1148 trace_amdgpu_vm_flush(ring, job->vmid, job->vm_pd_addr); 1149 amdgpu_ring_emit_vm_flush(ring, job->vmid, job->vm_pd_addr); 1150 } 1151 1152 if (pasid_mapping_needed) 1153 amdgpu_gmc_emit_pasid_mapping(ring, job->vmid, job->pasid); 1154 1155 if (vm_flush_needed || pasid_mapping_needed) { 1156 r = amdgpu_fence_emit(ring, &fence, 0); 1157 if (r) 1158 return r; 1159 } 1160 1161 if (vm_flush_needed) { 1162 mutex_lock(&id_mgr->lock); 1163 dma_fence_put(id->last_flush); 1164 id->last_flush = dma_fence_get(fence); 1165 id->current_gpu_reset_count = 1166 atomic_read(&adev->gpu_reset_counter); 1167 mutex_unlock(&id_mgr->lock); 1168 } 1169 1170 if (pasid_mapping_needed) { 1171 mutex_lock(&id_mgr->lock); 1172 id->pasid = job->pasid; 1173 dma_fence_put(id->pasid_mapping); 1174 id->pasid_mapping = dma_fence_get(fence); 1175 mutex_unlock(&id_mgr->lock); 1176 } 1177 dma_fence_put(fence); 1178 1179 if (ring->funcs->emit_gds_switch && gds_switch_needed) { 1180 id->gds_base = job->gds_base; 1181 id->gds_size = job->gds_size; 1182 id->gws_base = job->gws_base; 1183 id->gws_size = job->gws_size; 1184 id->oa_base = job->oa_base; 1185 id->oa_size = job->oa_size; 1186 amdgpu_ring_emit_gds_switch(ring, job->vmid, job->gds_base, 1187 job->gds_size, job->gws_base, 1188 job->gws_size, job->oa_base, 1189 job->oa_size); 1190 } 1191 1192 if (ring->funcs->patch_cond_exec) 1193 amdgpu_ring_patch_cond_exec(ring, patch_offset); 1194 1195 /* the double SWITCH_BUFFER here *cannot* be skipped by COND_EXEC */ 1196 if (ring->funcs->emit_switch_buffer) { 1197 amdgpu_ring_emit_switch_buffer(ring); 1198 amdgpu_ring_emit_switch_buffer(ring); 1199 } 1200 return 0; 1201 } 1202 1203 /** 1204 * amdgpu_vm_bo_find - find the bo_va for a specific vm & bo 1205 * 1206 * @vm: requested vm 1207 * @bo: requested buffer object 1208 * 1209 * Find @bo inside the requested vm. 1210 * Search inside the @bos vm list for the requested vm 1211 * Returns the found bo_va or NULL if none is found 1212 * 1213 * Object has to be reserved! 1214 * 1215 * Returns: 1216 * Found bo_va or NULL. 1217 */ 1218 struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm, 1219 struct amdgpu_bo *bo) 1220 { 1221 struct amdgpu_vm_bo_base *base; 1222 1223 for (base = bo->vm_bo; base; base = base->next) { 1224 if (base->vm != vm) 1225 continue; 1226 1227 return container_of(base, struct amdgpu_bo_va, base); 1228 } 1229 return NULL; 1230 } 1231 1232 /** 1233 * amdgpu_vm_map_gart - Resolve gart mapping of addr 1234 * 1235 * @pages_addr: optional DMA address to use for lookup 1236 * @addr: the unmapped addr 1237 * 1238 * Look up the physical address of the page that the pte resolves 1239 * to. 1240 * 1241 * Returns: 1242 * The pointer for the page table entry. 1243 */ 1244 uint64_t amdgpu_vm_map_gart(const dma_addr_t *pages_addr, uint64_t addr) 1245 { 1246 uint64_t result; 1247 1248 /* page table offset */ 1249 result = pages_addr[addr >> PAGE_SHIFT]; 1250 1251 /* in case cpu page size != gpu page size*/ 1252 result |= addr & (~PAGE_MASK); 1253 1254 result &= 0xFFFFFFFFFFFFF000ULL; 1255 1256 return result; 1257 } 1258 1259 /** 1260 * amdgpu_vm_update_pde - update a single level in the hierarchy 1261 * 1262 * @params: parameters for the update 1263 * @vm: requested vm 1264 * @entry: entry to update 1265 * 1266 * Makes sure the requested entry in parent is up to date. 1267 */ 1268 static int amdgpu_vm_update_pde(struct amdgpu_vm_update_params *params, 1269 struct amdgpu_vm *vm, 1270 struct amdgpu_vm_pt *entry) 1271 { 1272 struct amdgpu_vm_pt *parent = amdgpu_vm_pt_parent(entry); 1273 struct amdgpu_bo *bo = parent->base.bo, *pbo; 1274 uint64_t pde, pt, flags; 1275 unsigned level; 1276 1277 for (level = 0, pbo = bo->parent; pbo; ++level) 1278 pbo = pbo->parent; 1279 1280 level += params->adev->vm_manager.root_level; 1281 amdgpu_gmc_get_pde_for_bo(entry->base.bo, level, &pt, &flags); 1282 pde = (entry - parent->entries) * 8; 1283 return vm->update_funcs->update(params, bo, pde, pt, 1, 0, flags); 1284 } 1285 1286 /** 1287 * amdgpu_vm_invalidate_pds - mark all PDs as invalid 1288 * 1289 * @adev: amdgpu_device pointer 1290 * @vm: related vm 1291 * 1292 * Mark all PD level as invalid after an error. 1293 */ 1294 static void amdgpu_vm_invalidate_pds(struct amdgpu_device *adev, 1295 struct amdgpu_vm *vm) 1296 { 1297 struct amdgpu_vm_pt_cursor cursor; 1298 struct amdgpu_vm_pt *entry; 1299 1300 for_each_amdgpu_vm_pt_dfs_safe(adev, vm, NULL, cursor, entry) 1301 if (entry->base.bo && !entry->base.moved) 1302 amdgpu_vm_bo_relocated(&entry->base); 1303 } 1304 1305 /** 1306 * amdgpu_vm_update_pdes - make sure that all directories are valid 1307 * 1308 * @adev: amdgpu_device pointer 1309 * @vm: requested vm 1310 * @immediate: submit immediately to the paging queue 1311 * 1312 * Makes sure all directories are up to date. 1313 * 1314 * Returns: 1315 * 0 for success, error for failure. 1316 */ 1317 int amdgpu_vm_update_pdes(struct amdgpu_device *adev, 1318 struct amdgpu_vm *vm, bool immediate) 1319 { 1320 struct amdgpu_vm_update_params params; 1321 int r; 1322 1323 if (list_empty(&vm->relocated)) 1324 return 0; 1325 1326 memset(¶ms, 0, sizeof(params)); 1327 params.adev = adev; 1328 params.vm = vm; 1329 params.immediate = immediate; 1330 1331 r = vm->update_funcs->prepare(¶ms, NULL, AMDGPU_SYNC_EXPLICIT); 1332 if (r) 1333 return r; 1334 1335 while (!list_empty(&vm->relocated)) { 1336 struct amdgpu_vm_pt *entry; 1337 1338 entry = list_first_entry(&vm->relocated, struct amdgpu_vm_pt, 1339 base.vm_status); 1340 amdgpu_vm_bo_idle(&entry->base); 1341 1342 r = amdgpu_vm_update_pde(¶ms, vm, entry); 1343 if (r) 1344 goto error; 1345 } 1346 1347 r = vm->update_funcs->commit(¶ms, &vm->last_update); 1348 if (r) 1349 goto error; 1350 return 0; 1351 1352 error: 1353 amdgpu_vm_invalidate_pds(adev, vm); 1354 return r; 1355 } 1356 1357 /* 1358 * amdgpu_vm_update_flags - figure out flags for PTE updates 1359 * 1360 * Make sure to set the right flags for the PTEs at the desired level. 1361 */ 1362 static void amdgpu_vm_update_flags(struct amdgpu_vm_update_params *params, 1363 struct amdgpu_bo *bo, unsigned level, 1364 uint64_t pe, uint64_t addr, 1365 unsigned count, uint32_t incr, 1366 uint64_t flags) 1367 1368 { 1369 if (level != AMDGPU_VM_PTB) { 1370 flags |= AMDGPU_PDE_PTE; 1371 amdgpu_gmc_get_vm_pde(params->adev, level, &addr, &flags); 1372 1373 } else if (params->adev->asic_type >= CHIP_VEGA10 && 1374 !(flags & AMDGPU_PTE_VALID) && 1375 !(flags & AMDGPU_PTE_PRT)) { 1376 1377 /* Workaround for fault priority problem on GMC9 */ 1378 flags |= AMDGPU_PTE_EXECUTABLE; 1379 } 1380 1381 params->vm->update_funcs->update(params, bo, pe, addr, count, incr, 1382 flags); 1383 } 1384 1385 /** 1386 * amdgpu_vm_fragment - get fragment for PTEs 1387 * 1388 * @params: see amdgpu_vm_update_params definition 1389 * @start: first PTE to handle 1390 * @end: last PTE to handle 1391 * @flags: hw mapping flags 1392 * @frag: resulting fragment size 1393 * @frag_end: end of this fragment 1394 * 1395 * Returns the first possible fragment for the start and end address. 1396 */ 1397 static void amdgpu_vm_fragment(struct amdgpu_vm_update_params *params, 1398 uint64_t start, uint64_t end, uint64_t flags, 1399 unsigned int *frag, uint64_t *frag_end) 1400 { 1401 /** 1402 * The MC L1 TLB supports variable sized pages, based on a fragment 1403 * field in the PTE. When this field is set to a non-zero value, page 1404 * granularity is increased from 4KB to (1 << (12 + frag)). The PTE 1405 * flags are considered valid for all PTEs within the fragment range 1406 * and corresponding mappings are assumed to be physically contiguous. 1407 * 1408 * The L1 TLB can store a single PTE for the whole fragment, 1409 * significantly increasing the space available for translation 1410 * caching. This leads to large improvements in throughput when the 1411 * TLB is under pressure. 1412 * 1413 * The L2 TLB distributes small and large fragments into two 1414 * asymmetric partitions. The large fragment cache is significantly 1415 * larger. Thus, we try to use large fragments wherever possible. 1416 * Userspace can support this by aligning virtual base address and 1417 * allocation size to the fragment size. 1418 * 1419 * Starting with Vega10 the fragment size only controls the L1. The L2 1420 * is now directly feed with small/huge/giant pages from the walker. 1421 */ 1422 unsigned max_frag; 1423 1424 if (params->adev->asic_type < CHIP_VEGA10) 1425 max_frag = params->adev->vm_manager.fragment_size; 1426 else 1427 max_frag = 31; 1428 1429 /* system pages are non continuously */ 1430 if (params->pages_addr) { 1431 *frag = 0; 1432 *frag_end = end; 1433 return; 1434 } 1435 1436 /* This intentionally wraps around if no bit is set */ 1437 *frag = min((unsigned)ffs(start) - 1, (unsigned)fls64(end - start) - 1); 1438 if (*frag >= max_frag) { 1439 *frag = max_frag; 1440 *frag_end = end & ~((1ULL << max_frag) - 1); 1441 } else { 1442 *frag_end = start + (1 << *frag); 1443 } 1444 } 1445 1446 /** 1447 * amdgpu_vm_update_ptes - make sure that page tables are valid 1448 * 1449 * @params: see amdgpu_vm_update_params definition 1450 * @start: start of GPU address range 1451 * @end: end of GPU address range 1452 * @dst: destination address to map to, the next dst inside the function 1453 * @flags: mapping flags 1454 * 1455 * Update the page tables in the range @start - @end. 1456 * 1457 * Returns: 1458 * 0 for success, -EINVAL for failure. 1459 */ 1460 static int amdgpu_vm_update_ptes(struct amdgpu_vm_update_params *params, 1461 uint64_t start, uint64_t end, 1462 uint64_t dst, uint64_t flags) 1463 { 1464 struct amdgpu_device *adev = params->adev; 1465 struct amdgpu_vm_pt_cursor cursor; 1466 uint64_t frag_start = start, frag_end; 1467 unsigned int frag; 1468 int r; 1469 1470 /* figure out the initial fragment */ 1471 amdgpu_vm_fragment(params, frag_start, end, flags, &frag, &frag_end); 1472 1473 /* walk over the address space and update the PTs */ 1474 amdgpu_vm_pt_start(adev, params->vm, start, &cursor); 1475 while (cursor.pfn < end) { 1476 unsigned shift, parent_shift, mask; 1477 uint64_t incr, entry_end, pe_start; 1478 struct amdgpu_bo *pt; 1479 1480 if (!params->unlocked) { 1481 /* make sure that the page tables covering the 1482 * address range are actually allocated 1483 */ 1484 r = amdgpu_vm_alloc_pts(params->adev, params->vm, 1485 &cursor, params->immediate); 1486 if (r) 1487 return r; 1488 } 1489 1490 shift = amdgpu_vm_level_shift(adev, cursor.level); 1491 parent_shift = amdgpu_vm_level_shift(adev, cursor.level - 1); 1492 if (params->unlocked) { 1493 /* Unlocked updates are only allowed on the leaves */ 1494 if (amdgpu_vm_pt_descendant(adev, &cursor)) 1495 continue; 1496 } else if (adev->asic_type < CHIP_VEGA10 && 1497 (flags & AMDGPU_PTE_VALID)) { 1498 /* No huge page support before GMC v9 */ 1499 if (cursor.level != AMDGPU_VM_PTB) { 1500 if (!amdgpu_vm_pt_descendant(adev, &cursor)) 1501 return -ENOENT; 1502 continue; 1503 } 1504 } else if (frag < shift) { 1505 /* We can't use this level when the fragment size is 1506 * smaller than the address shift. Go to the next 1507 * child entry and try again. 1508 */ 1509 if (amdgpu_vm_pt_descendant(adev, &cursor)) 1510 continue; 1511 } else if (frag >= parent_shift) { 1512 /* If the fragment size is even larger than the parent 1513 * shift we should go up one level and check it again. 1514 */ 1515 if (!amdgpu_vm_pt_ancestor(&cursor)) 1516 return -EINVAL; 1517 continue; 1518 } 1519 1520 pt = cursor.entry->base.bo; 1521 if (!pt) { 1522 /* We need all PDs and PTs for mapping something, */ 1523 if (flags & AMDGPU_PTE_VALID) 1524 return -ENOENT; 1525 1526 /* but unmapping something can happen at a higher 1527 * level. 1528 */ 1529 if (!amdgpu_vm_pt_ancestor(&cursor)) 1530 return -EINVAL; 1531 1532 pt = cursor.entry->base.bo; 1533 shift = parent_shift; 1534 frag_end = max(frag_end, ALIGN(frag_start + 1, 1535 1ULL << shift)); 1536 } 1537 1538 /* Looks good so far, calculate parameters for the update */ 1539 incr = (uint64_t)AMDGPU_GPU_PAGE_SIZE << shift; 1540 mask = amdgpu_vm_entries_mask(adev, cursor.level); 1541 pe_start = ((cursor.pfn >> shift) & mask) * 8; 1542 entry_end = ((uint64_t)mask + 1) << shift; 1543 entry_end += cursor.pfn & ~(entry_end - 1); 1544 entry_end = min(entry_end, end); 1545 1546 do { 1547 struct amdgpu_vm *vm = params->vm; 1548 uint64_t upd_end = min(entry_end, frag_end); 1549 unsigned nptes = (upd_end - frag_start) >> shift; 1550 uint64_t upd_flags = flags | AMDGPU_PTE_FRAG(frag); 1551 1552 /* This can happen when we set higher level PDs to 1553 * silent to stop fault floods. 1554 */ 1555 nptes = max(nptes, 1u); 1556 1557 trace_amdgpu_vm_update_ptes(params, frag_start, upd_end, 1558 nptes, dst, incr, upd_flags, 1559 vm->task_info.pid, 1560 vm->immediate.fence_context); 1561 amdgpu_vm_update_flags(params, pt, cursor.level, 1562 pe_start, dst, nptes, incr, 1563 upd_flags); 1564 1565 pe_start += nptes * 8; 1566 dst += nptes * incr; 1567 1568 frag_start = upd_end; 1569 if (frag_start >= frag_end) { 1570 /* figure out the next fragment */ 1571 amdgpu_vm_fragment(params, frag_start, end, 1572 flags, &frag, &frag_end); 1573 if (frag < shift) 1574 break; 1575 } 1576 } while (frag_start < entry_end); 1577 1578 if (amdgpu_vm_pt_descendant(adev, &cursor)) { 1579 /* Free all child entries. 1580 * Update the tables with the flags and addresses and free up subsequent 1581 * tables in the case of huge pages or freed up areas. 1582 * This is the maximum you can free, because all other page tables are not 1583 * completely covered by the range and so potentially still in use. 1584 */ 1585 while (cursor.pfn < frag_start) { 1586 amdgpu_vm_free_pts(adev, params->vm, &cursor); 1587 amdgpu_vm_pt_next(adev, &cursor); 1588 params->table_freed = true; 1589 } 1590 1591 } else if (frag >= shift) { 1592 /* or just move on to the next on the same level. */ 1593 amdgpu_vm_pt_next(adev, &cursor); 1594 } 1595 } 1596 1597 return 0; 1598 } 1599 1600 /** 1601 * amdgpu_vm_bo_update_mapping - update a mapping in the vm page table 1602 * 1603 * @adev: amdgpu_device pointer of the VM 1604 * @bo_adev: amdgpu_device pointer of the mapped BO 1605 * @vm: requested vm 1606 * @immediate: immediate submission in a page fault 1607 * @unlocked: unlocked invalidation during MM callback 1608 * @resv: fences we need to sync to 1609 * @start: start of mapped range 1610 * @last: last mapped entry 1611 * @flags: flags for the entries 1612 * @offset: offset into nodes and pages_addr 1613 * @res: ttm_resource to map 1614 * @pages_addr: DMA addresses to use for mapping 1615 * @fence: optional resulting fence 1616 * @table_freed: return true if page table is freed 1617 * 1618 * Fill in the page table entries between @start and @last. 1619 * 1620 * Returns: 1621 * 0 for success, -EINVAL for failure. 1622 */ 1623 int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev, 1624 struct amdgpu_device *bo_adev, 1625 struct amdgpu_vm *vm, bool immediate, 1626 bool unlocked, struct dma_resv *resv, 1627 uint64_t start, uint64_t last, 1628 uint64_t flags, uint64_t offset, 1629 struct ttm_resource *res, 1630 dma_addr_t *pages_addr, 1631 struct dma_fence **fence, 1632 bool *table_freed) 1633 { 1634 struct amdgpu_vm_update_params params; 1635 struct amdgpu_res_cursor cursor; 1636 enum amdgpu_sync_mode sync_mode; 1637 int r, idx; 1638 1639 if (!drm_dev_enter(&adev->ddev, &idx)) 1640 return -ENODEV; 1641 1642 memset(¶ms, 0, sizeof(params)); 1643 params.adev = adev; 1644 params.vm = vm; 1645 params.immediate = immediate; 1646 params.pages_addr = pages_addr; 1647 params.unlocked = unlocked; 1648 1649 /* Implicitly sync to command submissions in the same VM before 1650 * unmapping. Sync to moving fences before mapping. 1651 */ 1652 if (!(flags & AMDGPU_PTE_VALID)) 1653 sync_mode = AMDGPU_SYNC_EQ_OWNER; 1654 else 1655 sync_mode = AMDGPU_SYNC_EXPLICIT; 1656 1657 amdgpu_vm_eviction_lock(vm); 1658 if (vm->evicting) { 1659 r = -EBUSY; 1660 goto error_unlock; 1661 } 1662 1663 if (!unlocked && !dma_fence_is_signaled(vm->last_unlocked)) { 1664 struct dma_fence *tmp = dma_fence_get_stub(); 1665 1666 amdgpu_bo_fence(vm->root.base.bo, vm->last_unlocked, true); 1667 swap(vm->last_unlocked, tmp); 1668 dma_fence_put(tmp); 1669 } 1670 1671 r = vm->update_funcs->prepare(¶ms, resv, sync_mode); 1672 if (r) 1673 goto error_unlock; 1674 1675 amdgpu_res_first(res, offset, (last - start + 1) * AMDGPU_GPU_PAGE_SIZE, 1676 &cursor); 1677 while (cursor.remaining) { 1678 uint64_t tmp, num_entries, addr; 1679 1680 num_entries = cursor.size >> AMDGPU_GPU_PAGE_SHIFT; 1681 if (pages_addr) { 1682 bool contiguous = true; 1683 1684 if (num_entries > AMDGPU_GPU_PAGES_IN_CPU_PAGE) { 1685 uint64_t pfn = cursor.start >> PAGE_SHIFT; 1686 uint64_t count; 1687 1688 contiguous = pages_addr[pfn + 1] == 1689 pages_addr[pfn] + PAGE_SIZE; 1690 1691 tmp = num_entries / 1692 AMDGPU_GPU_PAGES_IN_CPU_PAGE; 1693 for (count = 2; count < tmp; ++count) { 1694 uint64_t idx = pfn + count; 1695 1696 if (contiguous != (pages_addr[idx] == 1697 pages_addr[idx - 1] + PAGE_SIZE)) 1698 break; 1699 } 1700 num_entries = count * 1701 AMDGPU_GPU_PAGES_IN_CPU_PAGE; 1702 } 1703 1704 if (!contiguous) { 1705 addr = cursor.start; 1706 params.pages_addr = pages_addr; 1707 } else { 1708 addr = pages_addr[cursor.start >> PAGE_SHIFT]; 1709 params.pages_addr = NULL; 1710 } 1711 1712 } else if (flags & (AMDGPU_PTE_VALID | AMDGPU_PTE_PRT)) { 1713 addr = bo_adev->vm_manager.vram_base_offset + 1714 cursor.start; 1715 } else { 1716 addr = 0; 1717 } 1718 1719 tmp = start + num_entries; 1720 r = amdgpu_vm_update_ptes(¶ms, start, tmp, addr, flags); 1721 if (r) 1722 goto error_unlock; 1723 1724 amdgpu_res_next(&cursor, num_entries * AMDGPU_GPU_PAGE_SIZE); 1725 start = tmp; 1726 }; 1727 1728 r = vm->update_funcs->commit(¶ms, fence); 1729 1730 if (table_freed) 1731 *table_freed = params.table_freed; 1732 1733 error_unlock: 1734 amdgpu_vm_eviction_unlock(vm); 1735 drm_dev_exit(idx); 1736 return r; 1737 } 1738 1739 void amdgpu_vm_get_memory(struct amdgpu_vm *vm, uint64_t *vram_mem, 1740 uint64_t *gtt_mem, uint64_t *cpu_mem) 1741 { 1742 struct amdgpu_bo_va *bo_va, *tmp; 1743 1744 list_for_each_entry_safe(bo_va, tmp, &vm->idle, base.vm_status) { 1745 if (!bo_va->base.bo) 1746 continue; 1747 amdgpu_bo_get_memory(bo_va->base.bo, vram_mem, 1748 gtt_mem, cpu_mem); 1749 } 1750 list_for_each_entry_safe(bo_va, tmp, &vm->evicted, base.vm_status) { 1751 if (!bo_va->base.bo) 1752 continue; 1753 amdgpu_bo_get_memory(bo_va->base.bo, vram_mem, 1754 gtt_mem, cpu_mem); 1755 } 1756 list_for_each_entry_safe(bo_va, tmp, &vm->relocated, base.vm_status) { 1757 if (!bo_va->base.bo) 1758 continue; 1759 amdgpu_bo_get_memory(bo_va->base.bo, vram_mem, 1760 gtt_mem, cpu_mem); 1761 } 1762 list_for_each_entry_safe(bo_va, tmp, &vm->moved, base.vm_status) { 1763 if (!bo_va->base.bo) 1764 continue; 1765 amdgpu_bo_get_memory(bo_va->base.bo, vram_mem, 1766 gtt_mem, cpu_mem); 1767 } 1768 spin_lock(&vm->invalidated_lock); 1769 list_for_each_entry_safe(bo_va, tmp, &vm->invalidated, base.vm_status) { 1770 if (!bo_va->base.bo) 1771 continue; 1772 amdgpu_bo_get_memory(bo_va->base.bo, vram_mem, 1773 gtt_mem, cpu_mem); 1774 } 1775 list_for_each_entry_safe(bo_va, tmp, &vm->done, base.vm_status) { 1776 if (!bo_va->base.bo) 1777 continue; 1778 amdgpu_bo_get_memory(bo_va->base.bo, vram_mem, 1779 gtt_mem, cpu_mem); 1780 } 1781 spin_unlock(&vm->invalidated_lock); 1782 } 1783 /** 1784 * amdgpu_vm_bo_update - update all BO mappings in the vm page table 1785 * 1786 * @adev: amdgpu_device pointer 1787 * @bo_va: requested BO and VM object 1788 * @clear: if true clear the entries 1789 * 1790 * Fill in the page table entries for @bo_va. 1791 * 1792 * Returns: 1793 * 0 for success, -EINVAL for failure. 1794 */ 1795 int amdgpu_vm_bo_update(struct amdgpu_device *adev, struct amdgpu_bo_va *bo_va, 1796 bool clear) 1797 { 1798 struct amdgpu_bo *bo = bo_va->base.bo; 1799 struct amdgpu_vm *vm = bo_va->base.vm; 1800 struct amdgpu_bo_va_mapping *mapping; 1801 dma_addr_t *pages_addr = NULL; 1802 struct ttm_resource *mem; 1803 struct dma_fence **last_update; 1804 struct dma_resv *resv; 1805 uint64_t flags; 1806 struct amdgpu_device *bo_adev = adev; 1807 int r; 1808 1809 if (clear || !bo) { 1810 mem = NULL; 1811 resv = vm->root.base.bo->tbo.base.resv; 1812 } else { 1813 struct drm_gem_object *obj = &bo->tbo.base; 1814 1815 resv = bo->tbo.base.resv; 1816 if (obj->import_attach && bo_va->is_xgmi) { 1817 struct dma_buf *dma_buf = obj->import_attach->dmabuf; 1818 struct drm_gem_object *gobj = dma_buf->priv; 1819 struct amdgpu_bo *abo = gem_to_amdgpu_bo(gobj); 1820 1821 if (abo->tbo.resource->mem_type == TTM_PL_VRAM) 1822 bo = gem_to_amdgpu_bo(gobj); 1823 } 1824 mem = bo->tbo.resource; 1825 if (mem->mem_type == TTM_PL_TT) 1826 pages_addr = bo->tbo.ttm->dma_address; 1827 } 1828 1829 if (bo) { 1830 flags = amdgpu_ttm_tt_pte_flags(adev, bo->tbo.ttm, mem); 1831 1832 if (amdgpu_bo_encrypted(bo)) 1833 flags |= AMDGPU_PTE_TMZ; 1834 1835 bo_adev = amdgpu_ttm_adev(bo->tbo.bdev); 1836 } else { 1837 flags = 0x0; 1838 } 1839 1840 if (clear || (bo && bo->tbo.base.resv == 1841 vm->root.base.bo->tbo.base.resv)) 1842 last_update = &vm->last_update; 1843 else 1844 last_update = &bo_va->last_pt_update; 1845 1846 if (!clear && bo_va->base.moved) { 1847 bo_va->base.moved = false; 1848 list_splice_init(&bo_va->valids, &bo_va->invalids); 1849 1850 } else if (bo_va->cleared != clear) { 1851 list_splice_init(&bo_va->valids, &bo_va->invalids); 1852 } 1853 1854 list_for_each_entry(mapping, &bo_va->invalids, list) { 1855 uint64_t update_flags = flags; 1856 1857 /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here 1858 * but in case of something, we filter the flags in first place 1859 */ 1860 if (!(mapping->flags & AMDGPU_PTE_READABLE)) 1861 update_flags &= ~AMDGPU_PTE_READABLE; 1862 if (!(mapping->flags & AMDGPU_PTE_WRITEABLE)) 1863 update_flags &= ~AMDGPU_PTE_WRITEABLE; 1864 1865 /* Apply ASIC specific mapping flags */ 1866 amdgpu_gmc_get_vm_pte(adev, mapping, &update_flags); 1867 1868 trace_amdgpu_vm_bo_update(mapping); 1869 1870 r = amdgpu_vm_bo_update_mapping(adev, bo_adev, vm, false, false, 1871 resv, mapping->start, 1872 mapping->last, update_flags, 1873 mapping->offset, mem, 1874 pages_addr, last_update, NULL); 1875 if (r) 1876 return r; 1877 } 1878 1879 /* If the BO is not in its preferred location add it back to 1880 * the evicted list so that it gets validated again on the 1881 * next command submission. 1882 */ 1883 if (bo && bo->tbo.base.resv == vm->root.base.bo->tbo.base.resv) { 1884 uint32_t mem_type = bo->tbo.resource->mem_type; 1885 1886 if (!(bo->preferred_domains & 1887 amdgpu_mem_type_to_domain(mem_type))) 1888 amdgpu_vm_bo_evicted(&bo_va->base); 1889 else 1890 amdgpu_vm_bo_idle(&bo_va->base); 1891 } else { 1892 amdgpu_vm_bo_done(&bo_va->base); 1893 } 1894 1895 list_splice_init(&bo_va->invalids, &bo_va->valids); 1896 bo_va->cleared = clear; 1897 1898 if (trace_amdgpu_vm_bo_mapping_enabled()) { 1899 list_for_each_entry(mapping, &bo_va->valids, list) 1900 trace_amdgpu_vm_bo_mapping(mapping); 1901 } 1902 1903 return 0; 1904 } 1905 1906 /** 1907 * amdgpu_vm_update_prt_state - update the global PRT state 1908 * 1909 * @adev: amdgpu_device pointer 1910 */ 1911 static void amdgpu_vm_update_prt_state(struct amdgpu_device *adev) 1912 { 1913 unsigned long flags; 1914 bool enable; 1915 1916 spin_lock_irqsave(&adev->vm_manager.prt_lock, flags); 1917 enable = !!atomic_read(&adev->vm_manager.num_prt_users); 1918 adev->gmc.gmc_funcs->set_prt(adev, enable); 1919 spin_unlock_irqrestore(&adev->vm_manager.prt_lock, flags); 1920 } 1921 1922 /** 1923 * amdgpu_vm_prt_get - add a PRT user 1924 * 1925 * @adev: amdgpu_device pointer 1926 */ 1927 static void amdgpu_vm_prt_get(struct amdgpu_device *adev) 1928 { 1929 if (!adev->gmc.gmc_funcs->set_prt) 1930 return; 1931 1932 if (atomic_inc_return(&adev->vm_manager.num_prt_users) == 1) 1933 amdgpu_vm_update_prt_state(adev); 1934 } 1935 1936 /** 1937 * amdgpu_vm_prt_put - drop a PRT user 1938 * 1939 * @adev: amdgpu_device pointer 1940 */ 1941 static void amdgpu_vm_prt_put(struct amdgpu_device *adev) 1942 { 1943 if (atomic_dec_return(&adev->vm_manager.num_prt_users) == 0) 1944 amdgpu_vm_update_prt_state(adev); 1945 } 1946 1947 /** 1948 * amdgpu_vm_prt_cb - callback for updating the PRT status 1949 * 1950 * @fence: fence for the callback 1951 * @_cb: the callback function 1952 */ 1953 static void amdgpu_vm_prt_cb(struct dma_fence *fence, struct dma_fence_cb *_cb) 1954 { 1955 struct amdgpu_prt_cb *cb = container_of(_cb, struct amdgpu_prt_cb, cb); 1956 1957 amdgpu_vm_prt_put(cb->adev); 1958 kfree(cb); 1959 } 1960 1961 /** 1962 * amdgpu_vm_add_prt_cb - add callback for updating the PRT status 1963 * 1964 * @adev: amdgpu_device pointer 1965 * @fence: fence for the callback 1966 */ 1967 static void amdgpu_vm_add_prt_cb(struct amdgpu_device *adev, 1968 struct dma_fence *fence) 1969 { 1970 struct amdgpu_prt_cb *cb; 1971 1972 if (!adev->gmc.gmc_funcs->set_prt) 1973 return; 1974 1975 cb = kmalloc(sizeof(struct amdgpu_prt_cb), GFP_KERNEL); 1976 if (!cb) { 1977 /* Last resort when we are OOM */ 1978 if (fence) 1979 dma_fence_wait(fence, false); 1980 1981 amdgpu_vm_prt_put(adev); 1982 } else { 1983 cb->adev = adev; 1984 if (!fence || dma_fence_add_callback(fence, &cb->cb, 1985 amdgpu_vm_prt_cb)) 1986 amdgpu_vm_prt_cb(fence, &cb->cb); 1987 } 1988 } 1989 1990 /** 1991 * amdgpu_vm_free_mapping - free a mapping 1992 * 1993 * @adev: amdgpu_device pointer 1994 * @vm: requested vm 1995 * @mapping: mapping to be freed 1996 * @fence: fence of the unmap operation 1997 * 1998 * Free a mapping and make sure we decrease the PRT usage count if applicable. 1999 */ 2000 static void amdgpu_vm_free_mapping(struct amdgpu_device *adev, 2001 struct amdgpu_vm *vm, 2002 struct amdgpu_bo_va_mapping *mapping, 2003 struct dma_fence *fence) 2004 { 2005 if (mapping->flags & AMDGPU_PTE_PRT) 2006 amdgpu_vm_add_prt_cb(adev, fence); 2007 kfree(mapping); 2008 } 2009 2010 /** 2011 * amdgpu_vm_prt_fini - finish all prt mappings 2012 * 2013 * @adev: amdgpu_device pointer 2014 * @vm: requested vm 2015 * 2016 * Register a cleanup callback to disable PRT support after VM dies. 2017 */ 2018 static void amdgpu_vm_prt_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm) 2019 { 2020 struct dma_resv *resv = vm->root.base.bo->tbo.base.resv; 2021 struct dma_fence *excl, **shared; 2022 unsigned i, shared_count; 2023 int r; 2024 2025 r = dma_resv_get_fences_rcu(resv, &excl, 2026 &shared_count, &shared); 2027 if (r) { 2028 /* Not enough memory to grab the fence list, as last resort 2029 * block for all the fences to complete. 2030 */ 2031 dma_resv_wait_timeout_rcu(resv, true, false, 2032 MAX_SCHEDULE_TIMEOUT); 2033 return; 2034 } 2035 2036 /* Add a callback for each fence in the reservation object */ 2037 amdgpu_vm_prt_get(adev); 2038 amdgpu_vm_add_prt_cb(adev, excl); 2039 2040 for (i = 0; i < shared_count; ++i) { 2041 amdgpu_vm_prt_get(adev); 2042 amdgpu_vm_add_prt_cb(adev, shared[i]); 2043 } 2044 2045 kfree(shared); 2046 } 2047 2048 /** 2049 * amdgpu_vm_clear_freed - clear freed BOs in the PT 2050 * 2051 * @adev: amdgpu_device pointer 2052 * @vm: requested vm 2053 * @fence: optional resulting fence (unchanged if no work needed to be done 2054 * or if an error occurred) 2055 * 2056 * Make sure all freed BOs are cleared in the PT. 2057 * PTs have to be reserved and mutex must be locked! 2058 * 2059 * Returns: 2060 * 0 for success. 2061 * 2062 */ 2063 int amdgpu_vm_clear_freed(struct amdgpu_device *adev, 2064 struct amdgpu_vm *vm, 2065 struct dma_fence **fence) 2066 { 2067 struct dma_resv *resv = vm->root.base.bo->tbo.base.resv; 2068 struct amdgpu_bo_va_mapping *mapping; 2069 uint64_t init_pte_value = 0; 2070 struct dma_fence *f = NULL; 2071 int r; 2072 2073 while (!list_empty(&vm->freed)) { 2074 mapping = list_first_entry(&vm->freed, 2075 struct amdgpu_bo_va_mapping, list); 2076 list_del(&mapping->list); 2077 2078 if (vm->pte_support_ats && 2079 mapping->start < AMDGPU_GMC_HOLE_START) 2080 init_pte_value = AMDGPU_PTE_DEFAULT_ATC; 2081 2082 r = amdgpu_vm_bo_update_mapping(adev, adev, vm, false, false, 2083 resv, mapping->start, 2084 mapping->last, init_pte_value, 2085 0, NULL, NULL, &f, NULL); 2086 amdgpu_vm_free_mapping(adev, vm, mapping, f); 2087 if (r) { 2088 dma_fence_put(f); 2089 return r; 2090 } 2091 } 2092 2093 if (fence && f) { 2094 dma_fence_put(*fence); 2095 *fence = f; 2096 } else { 2097 dma_fence_put(f); 2098 } 2099 2100 return 0; 2101 2102 } 2103 2104 /** 2105 * amdgpu_vm_handle_moved - handle moved BOs in the PT 2106 * 2107 * @adev: amdgpu_device pointer 2108 * @vm: requested vm 2109 * 2110 * Make sure all BOs which are moved are updated in the PTs. 2111 * 2112 * Returns: 2113 * 0 for success. 2114 * 2115 * PTs have to be reserved! 2116 */ 2117 int amdgpu_vm_handle_moved(struct amdgpu_device *adev, 2118 struct amdgpu_vm *vm) 2119 { 2120 struct amdgpu_bo_va *bo_va, *tmp; 2121 struct dma_resv *resv; 2122 bool clear; 2123 int r; 2124 2125 list_for_each_entry_safe(bo_va, tmp, &vm->moved, base.vm_status) { 2126 /* Per VM BOs never need to bo cleared in the page tables */ 2127 r = amdgpu_vm_bo_update(adev, bo_va, false); 2128 if (r) 2129 return r; 2130 } 2131 2132 spin_lock(&vm->invalidated_lock); 2133 while (!list_empty(&vm->invalidated)) { 2134 bo_va = list_first_entry(&vm->invalidated, struct amdgpu_bo_va, 2135 base.vm_status); 2136 resv = bo_va->base.bo->tbo.base.resv; 2137 spin_unlock(&vm->invalidated_lock); 2138 2139 /* Try to reserve the BO to avoid clearing its ptes */ 2140 if (!amdgpu_vm_debug && dma_resv_trylock(resv)) 2141 clear = false; 2142 /* Somebody else is using the BO right now */ 2143 else 2144 clear = true; 2145 2146 r = amdgpu_vm_bo_update(adev, bo_va, clear); 2147 if (r) 2148 return r; 2149 2150 if (!clear) 2151 dma_resv_unlock(resv); 2152 spin_lock(&vm->invalidated_lock); 2153 } 2154 spin_unlock(&vm->invalidated_lock); 2155 2156 return 0; 2157 } 2158 2159 /** 2160 * amdgpu_vm_bo_add - add a bo to a specific vm 2161 * 2162 * @adev: amdgpu_device pointer 2163 * @vm: requested vm 2164 * @bo: amdgpu buffer object 2165 * 2166 * Add @bo into the requested vm. 2167 * Add @bo to the list of bos associated with the vm 2168 * 2169 * Returns: 2170 * Newly added bo_va or NULL for failure 2171 * 2172 * Object has to be reserved! 2173 */ 2174 struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev, 2175 struct amdgpu_vm *vm, 2176 struct amdgpu_bo *bo) 2177 { 2178 struct amdgpu_bo_va *bo_va; 2179 2180 bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL); 2181 if (bo_va == NULL) { 2182 return NULL; 2183 } 2184 amdgpu_vm_bo_base_init(&bo_va->base, vm, bo); 2185 2186 bo_va->ref_count = 1; 2187 INIT_LIST_HEAD(&bo_va->valids); 2188 INIT_LIST_HEAD(&bo_va->invalids); 2189 2190 if (!bo) 2191 return bo_va; 2192 2193 if (amdgpu_dmabuf_is_xgmi_accessible(adev, bo)) { 2194 bo_va->is_xgmi = true; 2195 /* Power up XGMI if it can be potentially used */ 2196 amdgpu_xgmi_set_pstate(adev, AMDGPU_XGMI_PSTATE_MAX_VEGA20); 2197 } 2198 2199 return bo_va; 2200 } 2201 2202 2203 /** 2204 * amdgpu_vm_bo_insert_map - insert a new mapping 2205 * 2206 * @adev: amdgpu_device pointer 2207 * @bo_va: bo_va to store the address 2208 * @mapping: the mapping to insert 2209 * 2210 * Insert a new mapping into all structures. 2211 */ 2212 static void amdgpu_vm_bo_insert_map(struct amdgpu_device *adev, 2213 struct amdgpu_bo_va *bo_va, 2214 struct amdgpu_bo_va_mapping *mapping) 2215 { 2216 struct amdgpu_vm *vm = bo_va->base.vm; 2217 struct amdgpu_bo *bo = bo_va->base.bo; 2218 2219 mapping->bo_va = bo_va; 2220 list_add(&mapping->list, &bo_va->invalids); 2221 amdgpu_vm_it_insert(mapping, &vm->va); 2222 2223 if (mapping->flags & AMDGPU_PTE_PRT) 2224 amdgpu_vm_prt_get(adev); 2225 2226 if (bo && bo->tbo.base.resv == vm->root.base.bo->tbo.base.resv && 2227 !bo_va->base.moved) { 2228 list_move(&bo_va->base.vm_status, &vm->moved); 2229 } 2230 trace_amdgpu_vm_bo_map(bo_va, mapping); 2231 } 2232 2233 /** 2234 * amdgpu_vm_bo_map - map bo inside a vm 2235 * 2236 * @adev: amdgpu_device pointer 2237 * @bo_va: bo_va to store the address 2238 * @saddr: where to map the BO 2239 * @offset: requested offset in the BO 2240 * @size: BO size in bytes 2241 * @flags: attributes of pages (read/write/valid/etc.) 2242 * 2243 * Add a mapping of the BO at the specefied addr into the VM. 2244 * 2245 * Returns: 2246 * 0 for success, error for failure. 2247 * 2248 * Object has to be reserved and unreserved outside! 2249 */ 2250 int amdgpu_vm_bo_map(struct amdgpu_device *adev, 2251 struct amdgpu_bo_va *bo_va, 2252 uint64_t saddr, uint64_t offset, 2253 uint64_t size, uint64_t flags) 2254 { 2255 struct amdgpu_bo_va_mapping *mapping, *tmp; 2256 struct amdgpu_bo *bo = bo_va->base.bo; 2257 struct amdgpu_vm *vm = bo_va->base.vm; 2258 uint64_t eaddr; 2259 2260 /* validate the parameters */ 2261 if (saddr & ~PAGE_MASK || offset & ~PAGE_MASK || 2262 size == 0 || size & ~PAGE_MASK) 2263 return -EINVAL; 2264 2265 /* make sure object fit at this offset */ 2266 eaddr = saddr + size - 1; 2267 if (saddr >= eaddr || 2268 (bo && offset + size > amdgpu_bo_size(bo)) || 2269 (eaddr >= adev->vm_manager.max_pfn << AMDGPU_GPU_PAGE_SHIFT)) 2270 return -EINVAL; 2271 2272 saddr /= AMDGPU_GPU_PAGE_SIZE; 2273 eaddr /= AMDGPU_GPU_PAGE_SIZE; 2274 2275 tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr); 2276 if (tmp) { 2277 /* bo and tmp overlap, invalid addr */ 2278 dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with " 2279 "0x%010Lx-0x%010Lx\n", bo, saddr, eaddr, 2280 tmp->start, tmp->last + 1); 2281 return -EINVAL; 2282 } 2283 2284 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL); 2285 if (!mapping) 2286 return -ENOMEM; 2287 2288 mapping->start = saddr; 2289 mapping->last = eaddr; 2290 mapping->offset = offset; 2291 mapping->flags = flags; 2292 2293 amdgpu_vm_bo_insert_map(adev, bo_va, mapping); 2294 2295 return 0; 2296 } 2297 2298 /** 2299 * amdgpu_vm_bo_replace_map - map bo inside a vm, replacing existing mappings 2300 * 2301 * @adev: amdgpu_device pointer 2302 * @bo_va: bo_va to store the address 2303 * @saddr: where to map the BO 2304 * @offset: requested offset in the BO 2305 * @size: BO size in bytes 2306 * @flags: attributes of pages (read/write/valid/etc.) 2307 * 2308 * Add a mapping of the BO at the specefied addr into the VM. Replace existing 2309 * mappings as we do so. 2310 * 2311 * Returns: 2312 * 0 for success, error for failure. 2313 * 2314 * Object has to be reserved and unreserved outside! 2315 */ 2316 int amdgpu_vm_bo_replace_map(struct amdgpu_device *adev, 2317 struct amdgpu_bo_va *bo_va, 2318 uint64_t saddr, uint64_t offset, 2319 uint64_t size, uint64_t flags) 2320 { 2321 struct amdgpu_bo_va_mapping *mapping; 2322 struct amdgpu_bo *bo = bo_va->base.bo; 2323 uint64_t eaddr; 2324 int r; 2325 2326 /* validate the parameters */ 2327 if (saddr & ~PAGE_MASK || offset & ~PAGE_MASK || 2328 size == 0 || size & ~PAGE_MASK) 2329 return -EINVAL; 2330 2331 /* make sure object fit at this offset */ 2332 eaddr = saddr + size - 1; 2333 if (saddr >= eaddr || 2334 (bo && offset + size > amdgpu_bo_size(bo)) || 2335 (eaddr >= adev->vm_manager.max_pfn << AMDGPU_GPU_PAGE_SHIFT)) 2336 return -EINVAL; 2337 2338 /* Allocate all the needed memory */ 2339 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL); 2340 if (!mapping) 2341 return -ENOMEM; 2342 2343 r = amdgpu_vm_bo_clear_mappings(adev, bo_va->base.vm, saddr, size); 2344 if (r) { 2345 kfree(mapping); 2346 return r; 2347 } 2348 2349 saddr /= AMDGPU_GPU_PAGE_SIZE; 2350 eaddr /= AMDGPU_GPU_PAGE_SIZE; 2351 2352 mapping->start = saddr; 2353 mapping->last = eaddr; 2354 mapping->offset = offset; 2355 mapping->flags = flags; 2356 2357 amdgpu_vm_bo_insert_map(adev, bo_va, mapping); 2358 2359 return 0; 2360 } 2361 2362 /** 2363 * amdgpu_vm_bo_unmap - remove bo mapping from vm 2364 * 2365 * @adev: amdgpu_device pointer 2366 * @bo_va: bo_va to remove the address from 2367 * @saddr: where to the BO is mapped 2368 * 2369 * Remove a mapping of the BO at the specefied addr from the VM. 2370 * 2371 * Returns: 2372 * 0 for success, error for failure. 2373 * 2374 * Object has to be reserved and unreserved outside! 2375 */ 2376 int amdgpu_vm_bo_unmap(struct amdgpu_device *adev, 2377 struct amdgpu_bo_va *bo_va, 2378 uint64_t saddr) 2379 { 2380 struct amdgpu_bo_va_mapping *mapping; 2381 struct amdgpu_vm *vm = bo_va->base.vm; 2382 bool valid = true; 2383 2384 saddr /= AMDGPU_GPU_PAGE_SIZE; 2385 2386 list_for_each_entry(mapping, &bo_va->valids, list) { 2387 if (mapping->start == saddr) 2388 break; 2389 } 2390 2391 if (&mapping->list == &bo_va->valids) { 2392 valid = false; 2393 2394 list_for_each_entry(mapping, &bo_va->invalids, list) { 2395 if (mapping->start == saddr) 2396 break; 2397 } 2398 2399 if (&mapping->list == &bo_va->invalids) 2400 return -ENOENT; 2401 } 2402 2403 list_del(&mapping->list); 2404 amdgpu_vm_it_remove(mapping, &vm->va); 2405 mapping->bo_va = NULL; 2406 trace_amdgpu_vm_bo_unmap(bo_va, mapping); 2407 2408 if (valid) 2409 list_add(&mapping->list, &vm->freed); 2410 else 2411 amdgpu_vm_free_mapping(adev, vm, mapping, 2412 bo_va->last_pt_update); 2413 2414 return 0; 2415 } 2416 2417 /** 2418 * amdgpu_vm_bo_clear_mappings - remove all mappings in a specific range 2419 * 2420 * @adev: amdgpu_device pointer 2421 * @vm: VM structure to use 2422 * @saddr: start of the range 2423 * @size: size of the range 2424 * 2425 * Remove all mappings in a range, split them as appropriate. 2426 * 2427 * Returns: 2428 * 0 for success, error for failure. 2429 */ 2430 int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev, 2431 struct amdgpu_vm *vm, 2432 uint64_t saddr, uint64_t size) 2433 { 2434 struct amdgpu_bo_va_mapping *before, *after, *tmp, *next; 2435 LIST_HEAD(removed); 2436 uint64_t eaddr; 2437 2438 eaddr = saddr + size - 1; 2439 saddr /= AMDGPU_GPU_PAGE_SIZE; 2440 eaddr /= AMDGPU_GPU_PAGE_SIZE; 2441 2442 /* Allocate all the needed memory */ 2443 before = kzalloc(sizeof(*before), GFP_KERNEL); 2444 if (!before) 2445 return -ENOMEM; 2446 INIT_LIST_HEAD(&before->list); 2447 2448 after = kzalloc(sizeof(*after), GFP_KERNEL); 2449 if (!after) { 2450 kfree(before); 2451 return -ENOMEM; 2452 } 2453 INIT_LIST_HEAD(&after->list); 2454 2455 /* Now gather all removed mappings */ 2456 tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr); 2457 while (tmp) { 2458 /* Remember mapping split at the start */ 2459 if (tmp->start < saddr) { 2460 before->start = tmp->start; 2461 before->last = saddr - 1; 2462 before->offset = tmp->offset; 2463 before->flags = tmp->flags; 2464 before->bo_va = tmp->bo_va; 2465 list_add(&before->list, &tmp->bo_va->invalids); 2466 } 2467 2468 /* Remember mapping split at the end */ 2469 if (tmp->last > eaddr) { 2470 after->start = eaddr + 1; 2471 after->last = tmp->last; 2472 after->offset = tmp->offset; 2473 after->offset += (after->start - tmp->start) << PAGE_SHIFT; 2474 after->flags = tmp->flags; 2475 after->bo_va = tmp->bo_va; 2476 list_add(&after->list, &tmp->bo_va->invalids); 2477 } 2478 2479 list_del(&tmp->list); 2480 list_add(&tmp->list, &removed); 2481 2482 tmp = amdgpu_vm_it_iter_next(tmp, saddr, eaddr); 2483 } 2484 2485 /* And free them up */ 2486 list_for_each_entry_safe(tmp, next, &removed, list) { 2487 amdgpu_vm_it_remove(tmp, &vm->va); 2488 list_del(&tmp->list); 2489 2490 if (tmp->start < saddr) 2491 tmp->start = saddr; 2492 if (tmp->last > eaddr) 2493 tmp->last = eaddr; 2494 2495 tmp->bo_va = NULL; 2496 list_add(&tmp->list, &vm->freed); 2497 trace_amdgpu_vm_bo_unmap(NULL, tmp); 2498 } 2499 2500 /* Insert partial mapping before the range */ 2501 if (!list_empty(&before->list)) { 2502 amdgpu_vm_it_insert(before, &vm->va); 2503 if (before->flags & AMDGPU_PTE_PRT) 2504 amdgpu_vm_prt_get(adev); 2505 } else { 2506 kfree(before); 2507 } 2508 2509 /* Insert partial mapping after the range */ 2510 if (!list_empty(&after->list)) { 2511 amdgpu_vm_it_insert(after, &vm->va); 2512 if (after->flags & AMDGPU_PTE_PRT) 2513 amdgpu_vm_prt_get(adev); 2514 } else { 2515 kfree(after); 2516 } 2517 2518 return 0; 2519 } 2520 2521 /** 2522 * amdgpu_vm_bo_lookup_mapping - find mapping by address 2523 * 2524 * @vm: the requested VM 2525 * @addr: the address 2526 * 2527 * Find a mapping by it's address. 2528 * 2529 * Returns: 2530 * The amdgpu_bo_va_mapping matching for addr or NULL 2531 * 2532 */ 2533 struct amdgpu_bo_va_mapping *amdgpu_vm_bo_lookup_mapping(struct amdgpu_vm *vm, 2534 uint64_t addr) 2535 { 2536 return amdgpu_vm_it_iter_first(&vm->va, addr, addr); 2537 } 2538 2539 /** 2540 * amdgpu_vm_bo_trace_cs - trace all reserved mappings 2541 * 2542 * @vm: the requested vm 2543 * @ticket: CS ticket 2544 * 2545 * Trace all mappings of BOs reserved during a command submission. 2546 */ 2547 void amdgpu_vm_bo_trace_cs(struct amdgpu_vm *vm, struct ww_acquire_ctx *ticket) 2548 { 2549 struct amdgpu_bo_va_mapping *mapping; 2550 2551 if (!trace_amdgpu_vm_bo_cs_enabled()) 2552 return; 2553 2554 for (mapping = amdgpu_vm_it_iter_first(&vm->va, 0, U64_MAX); mapping; 2555 mapping = amdgpu_vm_it_iter_next(mapping, 0, U64_MAX)) { 2556 if (mapping->bo_va && mapping->bo_va->base.bo) { 2557 struct amdgpu_bo *bo; 2558 2559 bo = mapping->bo_va->base.bo; 2560 if (dma_resv_locking_ctx(bo->tbo.base.resv) != 2561 ticket) 2562 continue; 2563 } 2564 2565 trace_amdgpu_vm_bo_cs(mapping); 2566 } 2567 } 2568 2569 /** 2570 * amdgpu_vm_bo_rmv - remove a bo to a specific vm 2571 * 2572 * @adev: amdgpu_device pointer 2573 * @bo_va: requested bo_va 2574 * 2575 * Remove @bo_va->bo from the requested vm. 2576 * 2577 * Object have to be reserved! 2578 */ 2579 void amdgpu_vm_bo_rmv(struct amdgpu_device *adev, 2580 struct amdgpu_bo_va *bo_va) 2581 { 2582 struct amdgpu_bo_va_mapping *mapping, *next; 2583 struct amdgpu_bo *bo = bo_va->base.bo; 2584 struct amdgpu_vm *vm = bo_va->base.vm; 2585 struct amdgpu_vm_bo_base **base; 2586 2587 if (bo) { 2588 if (bo->tbo.base.resv == vm->root.base.bo->tbo.base.resv) 2589 vm->bulk_moveable = false; 2590 2591 for (base = &bo_va->base.bo->vm_bo; *base; 2592 base = &(*base)->next) { 2593 if (*base != &bo_va->base) 2594 continue; 2595 2596 *base = bo_va->base.next; 2597 break; 2598 } 2599 } 2600 2601 spin_lock(&vm->invalidated_lock); 2602 list_del(&bo_va->base.vm_status); 2603 spin_unlock(&vm->invalidated_lock); 2604 2605 list_for_each_entry_safe(mapping, next, &bo_va->valids, list) { 2606 list_del(&mapping->list); 2607 amdgpu_vm_it_remove(mapping, &vm->va); 2608 mapping->bo_va = NULL; 2609 trace_amdgpu_vm_bo_unmap(bo_va, mapping); 2610 list_add(&mapping->list, &vm->freed); 2611 } 2612 list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) { 2613 list_del(&mapping->list); 2614 amdgpu_vm_it_remove(mapping, &vm->va); 2615 amdgpu_vm_free_mapping(adev, vm, mapping, 2616 bo_va->last_pt_update); 2617 } 2618 2619 dma_fence_put(bo_va->last_pt_update); 2620 2621 if (bo && bo_va->is_xgmi) 2622 amdgpu_xgmi_set_pstate(adev, AMDGPU_XGMI_PSTATE_MIN); 2623 2624 kfree(bo_va); 2625 } 2626 2627 /** 2628 * amdgpu_vm_evictable - check if we can evict a VM 2629 * 2630 * @bo: A page table of the VM. 2631 * 2632 * Check if it is possible to evict a VM. 2633 */ 2634 bool amdgpu_vm_evictable(struct amdgpu_bo *bo) 2635 { 2636 struct amdgpu_vm_bo_base *bo_base = bo->vm_bo; 2637 2638 /* Page tables of a destroyed VM can go away immediately */ 2639 if (!bo_base || !bo_base->vm) 2640 return true; 2641 2642 /* Don't evict VM page tables while they are busy */ 2643 if (!dma_resv_test_signaled_rcu(bo->tbo.base.resv, true)) 2644 return false; 2645 2646 /* Try to block ongoing updates */ 2647 if (!amdgpu_vm_eviction_trylock(bo_base->vm)) 2648 return false; 2649 2650 /* Don't evict VM page tables while they are updated */ 2651 if (!dma_fence_is_signaled(bo_base->vm->last_unlocked)) { 2652 amdgpu_vm_eviction_unlock(bo_base->vm); 2653 return false; 2654 } 2655 2656 bo_base->vm->evicting = true; 2657 amdgpu_vm_eviction_unlock(bo_base->vm); 2658 return true; 2659 } 2660 2661 /** 2662 * amdgpu_vm_bo_invalidate - mark the bo as invalid 2663 * 2664 * @adev: amdgpu_device pointer 2665 * @bo: amdgpu buffer object 2666 * @evicted: is the BO evicted 2667 * 2668 * Mark @bo as invalid. 2669 */ 2670 void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev, 2671 struct amdgpu_bo *bo, bool evicted) 2672 { 2673 struct amdgpu_vm_bo_base *bo_base; 2674 2675 /* shadow bo doesn't have bo base, its validation needs its parent */ 2676 if (bo->parent && bo->parent->shadow == bo) 2677 bo = bo->parent; 2678 2679 for (bo_base = bo->vm_bo; bo_base; bo_base = bo_base->next) { 2680 struct amdgpu_vm *vm = bo_base->vm; 2681 2682 if (evicted && bo->tbo.base.resv == vm->root.base.bo->tbo.base.resv) { 2683 amdgpu_vm_bo_evicted(bo_base); 2684 continue; 2685 } 2686 2687 if (bo_base->moved) 2688 continue; 2689 bo_base->moved = true; 2690 2691 if (bo->tbo.type == ttm_bo_type_kernel) 2692 amdgpu_vm_bo_relocated(bo_base); 2693 else if (bo->tbo.base.resv == vm->root.base.bo->tbo.base.resv) 2694 amdgpu_vm_bo_moved(bo_base); 2695 else 2696 amdgpu_vm_bo_invalidated(bo_base); 2697 } 2698 } 2699 2700 /** 2701 * amdgpu_vm_get_block_size - calculate VM page table size as power of two 2702 * 2703 * @vm_size: VM size 2704 * 2705 * Returns: 2706 * VM page table as power of two 2707 */ 2708 static uint32_t amdgpu_vm_get_block_size(uint64_t vm_size) 2709 { 2710 /* Total bits covered by PD + PTs */ 2711 unsigned bits = ilog2(vm_size) + 18; 2712 2713 /* Make sure the PD is 4K in size up to 8GB address space. 2714 Above that split equal between PD and PTs */ 2715 if (vm_size <= 8) 2716 return (bits - 9); 2717 else 2718 return ((bits + 3) / 2); 2719 } 2720 2721 /** 2722 * amdgpu_vm_adjust_size - adjust vm size, block size and fragment size 2723 * 2724 * @adev: amdgpu_device pointer 2725 * @min_vm_size: the minimum vm size in GB if it's set auto 2726 * @fragment_size_default: Default PTE fragment size 2727 * @max_level: max VMPT level 2728 * @max_bits: max address space size in bits 2729 * 2730 */ 2731 void amdgpu_vm_adjust_size(struct amdgpu_device *adev, uint32_t min_vm_size, 2732 uint32_t fragment_size_default, unsigned max_level, 2733 unsigned max_bits) 2734 { 2735 unsigned int max_size = 1 << (max_bits - 30); 2736 unsigned int vm_size; 2737 uint64_t tmp; 2738 2739 /* adjust vm size first */ 2740 if (amdgpu_vm_size != -1) { 2741 vm_size = amdgpu_vm_size; 2742 if (vm_size > max_size) { 2743 dev_warn(adev->dev, "VM size (%d) too large, max is %u GB\n", 2744 amdgpu_vm_size, max_size); 2745 vm_size = max_size; 2746 } 2747 } else { 2748 struct sysinfo si; 2749 unsigned int phys_ram_gb; 2750 2751 /* Optimal VM size depends on the amount of physical 2752 * RAM available. Underlying requirements and 2753 * assumptions: 2754 * 2755 * - Need to map system memory and VRAM from all GPUs 2756 * - VRAM from other GPUs not known here 2757 * - Assume VRAM <= system memory 2758 * - On GFX8 and older, VM space can be segmented for 2759 * different MTYPEs 2760 * - Need to allow room for fragmentation, guard pages etc. 2761 * 2762 * This adds up to a rough guess of system memory x3. 2763 * Round up to power of two to maximize the available 2764 * VM size with the given page table size. 2765 */ 2766 si_meminfo(&si); 2767 phys_ram_gb = ((uint64_t)si.totalram * si.mem_unit + 2768 (1 << 30) - 1) >> 30; 2769 vm_size = roundup_pow_of_two( 2770 min(max(phys_ram_gb * 3, min_vm_size), max_size)); 2771 } 2772 2773 adev->vm_manager.max_pfn = (uint64_t)vm_size << 18; 2774 2775 tmp = roundup_pow_of_two(adev->vm_manager.max_pfn); 2776 if (amdgpu_vm_block_size != -1) 2777 tmp >>= amdgpu_vm_block_size - 9; 2778 tmp = DIV_ROUND_UP(fls64(tmp) - 1, 9) - 1; 2779 adev->vm_manager.num_level = min(max_level, (unsigned)tmp); 2780 switch (adev->vm_manager.num_level) { 2781 case 3: 2782 adev->vm_manager.root_level = AMDGPU_VM_PDB2; 2783 break; 2784 case 2: 2785 adev->vm_manager.root_level = AMDGPU_VM_PDB1; 2786 break; 2787 case 1: 2788 adev->vm_manager.root_level = AMDGPU_VM_PDB0; 2789 break; 2790 default: 2791 dev_err(adev->dev, "VMPT only supports 2~4+1 levels\n"); 2792 } 2793 /* block size depends on vm size and hw setup*/ 2794 if (amdgpu_vm_block_size != -1) 2795 adev->vm_manager.block_size = 2796 min((unsigned)amdgpu_vm_block_size, max_bits 2797 - AMDGPU_GPU_PAGE_SHIFT 2798 - 9 * adev->vm_manager.num_level); 2799 else if (adev->vm_manager.num_level > 1) 2800 adev->vm_manager.block_size = 9; 2801 else 2802 adev->vm_manager.block_size = amdgpu_vm_get_block_size(tmp); 2803 2804 if (amdgpu_vm_fragment_size == -1) 2805 adev->vm_manager.fragment_size = fragment_size_default; 2806 else 2807 adev->vm_manager.fragment_size = amdgpu_vm_fragment_size; 2808 2809 DRM_INFO("vm size is %u GB, %u levels, block size is %u-bit, fragment size is %u-bit\n", 2810 vm_size, adev->vm_manager.num_level + 1, 2811 adev->vm_manager.block_size, 2812 adev->vm_manager.fragment_size); 2813 } 2814 2815 /** 2816 * amdgpu_vm_wait_idle - wait for the VM to become idle 2817 * 2818 * @vm: VM object to wait for 2819 * @timeout: timeout to wait for VM to become idle 2820 */ 2821 long amdgpu_vm_wait_idle(struct amdgpu_vm *vm, long timeout) 2822 { 2823 timeout = dma_resv_wait_timeout_rcu(vm->root.base.bo->tbo.base.resv, 2824 true, true, timeout); 2825 if (timeout <= 0) 2826 return timeout; 2827 2828 return dma_fence_wait_timeout(vm->last_unlocked, true, timeout); 2829 } 2830 2831 /** 2832 * amdgpu_vm_init - initialize a vm instance 2833 * 2834 * @adev: amdgpu_device pointer 2835 * @vm: requested vm 2836 * @pasid: Process address space identifier 2837 * 2838 * Init @vm fields. 2839 * 2840 * Returns: 2841 * 0 for success, error for failure. 2842 */ 2843 int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm, u32 pasid) 2844 { 2845 struct amdgpu_bo *root; 2846 int r, i; 2847 2848 vm->va = RB_ROOT_CACHED; 2849 for (i = 0; i < AMDGPU_MAX_VMHUBS; i++) 2850 vm->reserved_vmid[i] = NULL; 2851 INIT_LIST_HEAD(&vm->evicted); 2852 INIT_LIST_HEAD(&vm->relocated); 2853 INIT_LIST_HEAD(&vm->moved); 2854 INIT_LIST_HEAD(&vm->idle); 2855 INIT_LIST_HEAD(&vm->invalidated); 2856 spin_lock_init(&vm->invalidated_lock); 2857 INIT_LIST_HEAD(&vm->freed); 2858 INIT_LIST_HEAD(&vm->done); 2859 2860 /* create scheduler entities for page table updates */ 2861 r = drm_sched_entity_init(&vm->immediate, DRM_SCHED_PRIORITY_NORMAL, 2862 adev->vm_manager.vm_pte_scheds, 2863 adev->vm_manager.vm_pte_num_scheds, NULL); 2864 if (r) 2865 return r; 2866 2867 r = drm_sched_entity_init(&vm->delayed, DRM_SCHED_PRIORITY_NORMAL, 2868 adev->vm_manager.vm_pte_scheds, 2869 adev->vm_manager.vm_pte_num_scheds, NULL); 2870 if (r) 2871 goto error_free_immediate; 2872 2873 vm->pte_support_ats = false; 2874 vm->is_compute_context = false; 2875 2876 vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode & 2877 AMDGPU_VM_USE_CPU_FOR_GFX); 2878 2879 DRM_DEBUG_DRIVER("VM update mode is %s\n", 2880 vm->use_cpu_for_update ? "CPU" : "SDMA"); 2881 WARN_ONCE((vm->use_cpu_for_update && 2882 !amdgpu_gmc_vram_full_visible(&adev->gmc)), 2883 "CPU update of VM recommended only for large BAR system\n"); 2884 2885 if (vm->use_cpu_for_update) 2886 vm->update_funcs = &amdgpu_vm_cpu_funcs; 2887 else 2888 vm->update_funcs = &amdgpu_vm_sdma_funcs; 2889 vm->last_update = NULL; 2890 vm->last_unlocked = dma_fence_get_stub(); 2891 2892 mutex_init(&vm->eviction_lock); 2893 vm->evicting = false; 2894 2895 r = amdgpu_vm_pt_create(adev, vm, adev->vm_manager.root_level, 2896 false, &root); 2897 if (r) 2898 goto error_free_delayed; 2899 2900 r = amdgpu_bo_reserve(root, true); 2901 if (r) 2902 goto error_free_root; 2903 2904 r = dma_resv_reserve_shared(root->tbo.base.resv, 1); 2905 if (r) 2906 goto error_unreserve; 2907 2908 amdgpu_vm_bo_base_init(&vm->root.base, vm, root); 2909 2910 r = amdgpu_vm_clear_bo(adev, vm, root, false); 2911 if (r) 2912 goto error_unreserve; 2913 2914 amdgpu_bo_unreserve(vm->root.base.bo); 2915 2916 if (pasid) { 2917 unsigned long flags; 2918 2919 spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags); 2920 r = idr_alloc(&adev->vm_manager.pasid_idr, vm, pasid, pasid + 1, 2921 GFP_ATOMIC); 2922 spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags); 2923 if (r < 0) 2924 goto error_free_root; 2925 2926 vm->pasid = pasid; 2927 } 2928 2929 INIT_KFIFO(vm->faults); 2930 2931 return 0; 2932 2933 error_unreserve: 2934 amdgpu_bo_unreserve(vm->root.base.bo); 2935 2936 error_free_root: 2937 amdgpu_bo_unref(&vm->root.base.bo->shadow); 2938 amdgpu_bo_unref(&vm->root.base.bo); 2939 vm->root.base.bo = NULL; 2940 2941 error_free_delayed: 2942 dma_fence_put(vm->last_unlocked); 2943 drm_sched_entity_destroy(&vm->delayed); 2944 2945 error_free_immediate: 2946 drm_sched_entity_destroy(&vm->immediate); 2947 2948 return r; 2949 } 2950 2951 /** 2952 * amdgpu_vm_check_clean_reserved - check if a VM is clean 2953 * 2954 * @adev: amdgpu_device pointer 2955 * @vm: the VM to check 2956 * 2957 * check all entries of the root PD, if any subsequent PDs are allocated, 2958 * it means there are page table creating and filling, and is no a clean 2959 * VM 2960 * 2961 * Returns: 2962 * 0 if this VM is clean 2963 */ 2964 static int amdgpu_vm_check_clean_reserved(struct amdgpu_device *adev, 2965 struct amdgpu_vm *vm) 2966 { 2967 enum amdgpu_vm_level root = adev->vm_manager.root_level; 2968 unsigned int entries = amdgpu_vm_num_entries(adev, root); 2969 unsigned int i = 0; 2970 2971 if (!(vm->root.entries)) 2972 return 0; 2973 2974 for (i = 0; i < entries; i++) { 2975 if (vm->root.entries[i].base.bo) 2976 return -EINVAL; 2977 } 2978 2979 return 0; 2980 } 2981 2982 /** 2983 * amdgpu_vm_make_compute - Turn a GFX VM into a compute VM 2984 * 2985 * @adev: amdgpu_device pointer 2986 * @vm: requested vm 2987 * @pasid: pasid to use 2988 * 2989 * This only works on GFX VMs that don't have any BOs added and no 2990 * page tables allocated yet. 2991 * 2992 * Changes the following VM parameters: 2993 * - use_cpu_for_update 2994 * - pte_supports_ats 2995 * - pasid (old PASID is released, because compute manages its own PASIDs) 2996 * 2997 * Reinitializes the page directory to reflect the changed ATS 2998 * setting. 2999 * 3000 * Returns: 3001 * 0 for success, -errno for errors. 3002 */ 3003 int amdgpu_vm_make_compute(struct amdgpu_device *adev, struct amdgpu_vm *vm, 3004 u32 pasid) 3005 { 3006 bool pte_support_ats = (adev->asic_type == CHIP_RAVEN); 3007 int r; 3008 3009 r = amdgpu_bo_reserve(vm->root.base.bo, true); 3010 if (r) 3011 return r; 3012 3013 /* Sanity checks */ 3014 r = amdgpu_vm_check_clean_reserved(adev, vm); 3015 if (r) 3016 goto unreserve_bo; 3017 3018 if (pasid) { 3019 unsigned long flags; 3020 3021 spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags); 3022 r = idr_alloc(&adev->vm_manager.pasid_idr, vm, pasid, pasid + 1, 3023 GFP_ATOMIC); 3024 spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags); 3025 3026 if (r == -ENOSPC) 3027 goto unreserve_bo; 3028 r = 0; 3029 } 3030 3031 /* Check if PD needs to be reinitialized and do it before 3032 * changing any other state, in case it fails. 3033 */ 3034 if (pte_support_ats != vm->pte_support_ats) { 3035 vm->pte_support_ats = pte_support_ats; 3036 r = amdgpu_vm_clear_bo(adev, vm, vm->root.base.bo, false); 3037 if (r) 3038 goto free_idr; 3039 } 3040 3041 /* Update VM state */ 3042 vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode & 3043 AMDGPU_VM_USE_CPU_FOR_COMPUTE); 3044 DRM_DEBUG_DRIVER("VM update mode is %s\n", 3045 vm->use_cpu_for_update ? "CPU" : "SDMA"); 3046 WARN_ONCE((vm->use_cpu_for_update && 3047 !amdgpu_gmc_vram_full_visible(&adev->gmc)), 3048 "CPU update of VM recommended only for large BAR system\n"); 3049 3050 if (vm->use_cpu_for_update) { 3051 /* Sync with last SDMA update/clear before switching to CPU */ 3052 r = amdgpu_bo_sync_wait(vm->root.base.bo, 3053 AMDGPU_FENCE_OWNER_UNDEFINED, true); 3054 if (r) 3055 goto free_idr; 3056 3057 vm->update_funcs = &amdgpu_vm_cpu_funcs; 3058 } else { 3059 vm->update_funcs = &amdgpu_vm_sdma_funcs; 3060 } 3061 dma_fence_put(vm->last_update); 3062 vm->last_update = NULL; 3063 vm->is_compute_context = true; 3064 3065 if (vm->pasid) { 3066 unsigned long flags; 3067 3068 spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags); 3069 idr_remove(&adev->vm_manager.pasid_idr, vm->pasid); 3070 spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags); 3071 3072 /* Free the original amdgpu allocated pasid 3073 * Will be replaced with kfd allocated pasid 3074 */ 3075 amdgpu_pasid_free(vm->pasid); 3076 vm->pasid = 0; 3077 } 3078 3079 /* Free the shadow bo for compute VM */ 3080 amdgpu_bo_unref(&vm->root.base.bo->shadow); 3081 3082 if (pasid) 3083 vm->pasid = pasid; 3084 3085 goto unreserve_bo; 3086 3087 free_idr: 3088 if (pasid) { 3089 unsigned long flags; 3090 3091 spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags); 3092 idr_remove(&adev->vm_manager.pasid_idr, pasid); 3093 spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags); 3094 } 3095 unreserve_bo: 3096 amdgpu_bo_unreserve(vm->root.base.bo); 3097 return r; 3098 } 3099 3100 /** 3101 * amdgpu_vm_release_compute - release a compute vm 3102 * @adev: amdgpu_device pointer 3103 * @vm: a vm turned into compute vm by calling amdgpu_vm_make_compute 3104 * 3105 * This is a correspondant of amdgpu_vm_make_compute. It decouples compute 3106 * pasid from vm. Compute should stop use of vm after this call. 3107 */ 3108 void amdgpu_vm_release_compute(struct amdgpu_device *adev, struct amdgpu_vm *vm) 3109 { 3110 if (vm->pasid) { 3111 unsigned long flags; 3112 3113 spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags); 3114 idr_remove(&adev->vm_manager.pasid_idr, vm->pasid); 3115 spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags); 3116 } 3117 vm->pasid = 0; 3118 vm->is_compute_context = false; 3119 } 3120 3121 /** 3122 * amdgpu_vm_fini - tear down a vm instance 3123 * 3124 * @adev: amdgpu_device pointer 3125 * @vm: requested vm 3126 * 3127 * Tear down @vm. 3128 * Unbind the VM and remove all bos from the vm bo list 3129 */ 3130 void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm) 3131 { 3132 struct amdgpu_bo_va_mapping *mapping, *tmp; 3133 bool prt_fini_needed = !!adev->gmc.gmc_funcs->set_prt; 3134 struct amdgpu_bo *root; 3135 int i; 3136 3137 amdgpu_amdkfd_gpuvm_destroy_cb(adev, vm); 3138 3139 root = amdgpu_bo_ref(vm->root.base.bo); 3140 amdgpu_bo_reserve(root, true); 3141 if (vm->pasid) { 3142 unsigned long flags; 3143 3144 spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags); 3145 idr_remove(&adev->vm_manager.pasid_idr, vm->pasid); 3146 spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags); 3147 vm->pasid = 0; 3148 } 3149 3150 dma_fence_wait(vm->last_unlocked, false); 3151 dma_fence_put(vm->last_unlocked); 3152 3153 list_for_each_entry_safe(mapping, tmp, &vm->freed, list) { 3154 if (mapping->flags & AMDGPU_PTE_PRT && prt_fini_needed) { 3155 amdgpu_vm_prt_fini(adev, vm); 3156 prt_fini_needed = false; 3157 } 3158 3159 list_del(&mapping->list); 3160 amdgpu_vm_free_mapping(adev, vm, mapping, NULL); 3161 } 3162 3163 amdgpu_vm_free_pts(adev, vm, NULL); 3164 amdgpu_bo_unreserve(root); 3165 amdgpu_bo_unref(&root); 3166 WARN_ON(vm->root.base.bo); 3167 3168 drm_sched_entity_destroy(&vm->immediate); 3169 drm_sched_entity_destroy(&vm->delayed); 3170 3171 if (!RB_EMPTY_ROOT(&vm->va.rb_root)) { 3172 dev_err(adev->dev, "still active bo inside vm\n"); 3173 } 3174 rbtree_postorder_for_each_entry_safe(mapping, tmp, 3175 &vm->va.rb_root, rb) { 3176 /* Don't remove the mapping here, we don't want to trigger a 3177 * rebalance and the tree is about to be destroyed anyway. 3178 */ 3179 list_del(&mapping->list); 3180 kfree(mapping); 3181 } 3182 3183 dma_fence_put(vm->last_update); 3184 for (i = 0; i < AMDGPU_MAX_VMHUBS; i++) 3185 amdgpu_vmid_free_reserved(adev, vm, i); 3186 } 3187 3188 /** 3189 * amdgpu_vm_manager_init - init the VM manager 3190 * 3191 * @adev: amdgpu_device pointer 3192 * 3193 * Initialize the VM manager structures 3194 */ 3195 void amdgpu_vm_manager_init(struct amdgpu_device *adev) 3196 { 3197 unsigned i; 3198 3199 /* Concurrent flushes are only possible starting with Vega10 and 3200 * are broken on Navi10 and Navi14. 3201 */ 3202 adev->vm_manager.concurrent_flush = !(adev->asic_type < CHIP_VEGA10 || 3203 adev->asic_type == CHIP_NAVI10 || 3204 adev->asic_type == CHIP_NAVI14); 3205 amdgpu_vmid_mgr_init(adev); 3206 3207 adev->vm_manager.fence_context = 3208 dma_fence_context_alloc(AMDGPU_MAX_RINGS); 3209 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) 3210 adev->vm_manager.seqno[i] = 0; 3211 3212 spin_lock_init(&adev->vm_manager.prt_lock); 3213 atomic_set(&adev->vm_manager.num_prt_users, 0); 3214 3215 /* If not overridden by the user, by default, only in large BAR systems 3216 * Compute VM tables will be updated by CPU 3217 */ 3218 #ifdef CONFIG_X86_64 3219 if (amdgpu_vm_update_mode == -1) { 3220 if (amdgpu_gmc_vram_full_visible(&adev->gmc)) 3221 adev->vm_manager.vm_update_mode = 3222 AMDGPU_VM_USE_CPU_FOR_COMPUTE; 3223 else 3224 adev->vm_manager.vm_update_mode = 0; 3225 } else 3226 adev->vm_manager.vm_update_mode = amdgpu_vm_update_mode; 3227 #else 3228 adev->vm_manager.vm_update_mode = 0; 3229 #endif 3230 3231 idr_init(&adev->vm_manager.pasid_idr); 3232 spin_lock_init(&adev->vm_manager.pasid_lock); 3233 } 3234 3235 /** 3236 * amdgpu_vm_manager_fini - cleanup VM manager 3237 * 3238 * @adev: amdgpu_device pointer 3239 * 3240 * Cleanup the VM manager and free resources. 3241 */ 3242 void amdgpu_vm_manager_fini(struct amdgpu_device *adev) 3243 { 3244 WARN_ON(!idr_is_empty(&adev->vm_manager.pasid_idr)); 3245 idr_destroy(&adev->vm_manager.pasid_idr); 3246 3247 amdgpu_vmid_mgr_fini(adev); 3248 } 3249 3250 /** 3251 * amdgpu_vm_ioctl - Manages VMID reservation for vm hubs. 3252 * 3253 * @dev: drm device pointer 3254 * @data: drm_amdgpu_vm 3255 * @filp: drm file pointer 3256 * 3257 * Returns: 3258 * 0 for success, -errno for errors. 3259 */ 3260 int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp) 3261 { 3262 union drm_amdgpu_vm *args = data; 3263 struct amdgpu_device *adev = drm_to_adev(dev); 3264 struct amdgpu_fpriv *fpriv = filp->driver_priv; 3265 long timeout = msecs_to_jiffies(2000); 3266 int r; 3267 3268 switch (args->in.op) { 3269 case AMDGPU_VM_OP_RESERVE_VMID: 3270 /* We only have requirement to reserve vmid from gfxhub */ 3271 r = amdgpu_vmid_alloc_reserved(adev, &fpriv->vm, 3272 AMDGPU_GFXHUB_0); 3273 if (r) 3274 return r; 3275 break; 3276 case AMDGPU_VM_OP_UNRESERVE_VMID: 3277 if (amdgpu_sriov_runtime(adev)) 3278 timeout = 8 * timeout; 3279 3280 /* Wait vm idle to make sure the vmid set in SPM_VMID is 3281 * not referenced anymore. 3282 */ 3283 r = amdgpu_bo_reserve(fpriv->vm.root.base.bo, true); 3284 if (r) 3285 return r; 3286 3287 r = amdgpu_vm_wait_idle(&fpriv->vm, timeout); 3288 if (r < 0) 3289 return r; 3290 3291 amdgpu_bo_unreserve(fpriv->vm.root.base.bo); 3292 amdgpu_vmid_free_reserved(adev, &fpriv->vm, AMDGPU_GFXHUB_0); 3293 break; 3294 default: 3295 return -EINVAL; 3296 } 3297 3298 return 0; 3299 } 3300 3301 /** 3302 * amdgpu_vm_get_task_info - Extracts task info for a PASID. 3303 * 3304 * @adev: drm device pointer 3305 * @pasid: PASID identifier for VM 3306 * @task_info: task_info to fill. 3307 */ 3308 void amdgpu_vm_get_task_info(struct amdgpu_device *adev, u32 pasid, 3309 struct amdgpu_task_info *task_info) 3310 { 3311 struct amdgpu_vm *vm; 3312 unsigned long flags; 3313 3314 spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags); 3315 3316 vm = idr_find(&adev->vm_manager.pasid_idr, pasid); 3317 if (vm) 3318 *task_info = vm->task_info; 3319 3320 spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags); 3321 } 3322 3323 /** 3324 * amdgpu_vm_set_task_info - Sets VMs task info. 3325 * 3326 * @vm: vm for which to set the info 3327 */ 3328 void amdgpu_vm_set_task_info(struct amdgpu_vm *vm) 3329 { 3330 if (vm->task_info.pid) 3331 return; 3332 3333 vm->task_info.pid = current->pid; 3334 get_task_comm(vm->task_info.task_name, current); 3335 3336 if (current->group_leader->mm != current->mm) 3337 return; 3338 3339 vm->task_info.tgid = current->group_leader->pid; 3340 get_task_comm(vm->task_info.process_name, current->group_leader); 3341 } 3342 3343 /** 3344 * amdgpu_vm_handle_fault - graceful handling of VM faults. 3345 * @adev: amdgpu device pointer 3346 * @pasid: PASID of the VM 3347 * @addr: Address of the fault 3348 * 3349 * Try to gracefully handle a VM fault. Return true if the fault was handled and 3350 * shouldn't be reported any more. 3351 */ 3352 bool amdgpu_vm_handle_fault(struct amdgpu_device *adev, u32 pasid, 3353 uint64_t addr) 3354 { 3355 bool is_compute_context = false; 3356 struct amdgpu_bo *root; 3357 uint64_t value, flags; 3358 struct amdgpu_vm *vm; 3359 int r; 3360 3361 spin_lock(&adev->vm_manager.pasid_lock); 3362 vm = idr_find(&adev->vm_manager.pasid_idr, pasid); 3363 if (vm) { 3364 root = amdgpu_bo_ref(vm->root.base.bo); 3365 is_compute_context = vm->is_compute_context; 3366 } else { 3367 root = NULL; 3368 } 3369 spin_unlock(&adev->vm_manager.pasid_lock); 3370 3371 if (!root) 3372 return false; 3373 3374 addr /= AMDGPU_GPU_PAGE_SIZE; 3375 3376 if (is_compute_context && 3377 !svm_range_restore_pages(adev, pasid, addr)) { 3378 amdgpu_bo_unref(&root); 3379 return true; 3380 } 3381 3382 r = amdgpu_bo_reserve(root, true); 3383 if (r) 3384 goto error_unref; 3385 3386 /* Double check that the VM still exists */ 3387 spin_lock(&adev->vm_manager.pasid_lock); 3388 vm = idr_find(&adev->vm_manager.pasid_idr, pasid); 3389 if (vm && vm->root.base.bo != root) 3390 vm = NULL; 3391 spin_unlock(&adev->vm_manager.pasid_lock); 3392 if (!vm) 3393 goto error_unlock; 3394 3395 flags = AMDGPU_PTE_VALID | AMDGPU_PTE_SNOOPED | 3396 AMDGPU_PTE_SYSTEM; 3397 3398 if (is_compute_context) { 3399 /* Intentionally setting invalid PTE flag 3400 * combination to force a no-retry-fault 3401 */ 3402 flags = AMDGPU_PTE_EXECUTABLE | AMDGPU_PDE_PTE | 3403 AMDGPU_PTE_TF; 3404 value = 0; 3405 } else if (amdgpu_vm_fault_stop == AMDGPU_VM_FAULT_STOP_NEVER) { 3406 /* Redirect the access to the dummy page */ 3407 value = adev->dummy_page_addr; 3408 flags |= AMDGPU_PTE_EXECUTABLE | AMDGPU_PTE_READABLE | 3409 AMDGPU_PTE_WRITEABLE; 3410 3411 } else { 3412 /* Let the hw retry silently on the PTE */ 3413 value = 0; 3414 } 3415 3416 r = dma_resv_reserve_shared(root->tbo.base.resv, 1); 3417 if (r) { 3418 pr_debug("failed %d to reserve fence slot\n", r); 3419 goto error_unlock; 3420 } 3421 3422 r = amdgpu_vm_bo_update_mapping(adev, adev, vm, true, false, NULL, addr, 3423 addr, flags, value, NULL, NULL, NULL, 3424 NULL); 3425 if (r) 3426 goto error_unlock; 3427 3428 r = amdgpu_vm_update_pdes(adev, vm, true); 3429 3430 error_unlock: 3431 amdgpu_bo_unreserve(root); 3432 if (r < 0) 3433 DRM_ERROR("Can't handle page fault (%d)\n", r); 3434 3435 error_unref: 3436 amdgpu_bo_unref(&root); 3437 3438 return false; 3439 } 3440 3441 #if defined(CONFIG_DEBUG_FS) 3442 /** 3443 * amdgpu_debugfs_vm_bo_info - print BO info for the VM 3444 * 3445 * @vm: Requested VM for printing BO info 3446 * @m: debugfs file 3447 * 3448 * Print BO information in debugfs file for the VM 3449 */ 3450 void amdgpu_debugfs_vm_bo_info(struct amdgpu_vm *vm, struct seq_file *m) 3451 { 3452 struct amdgpu_bo_va *bo_va, *tmp; 3453 u64 total_idle = 0; 3454 u64 total_evicted = 0; 3455 u64 total_relocated = 0; 3456 u64 total_moved = 0; 3457 u64 total_invalidated = 0; 3458 u64 total_done = 0; 3459 unsigned int total_idle_objs = 0; 3460 unsigned int total_evicted_objs = 0; 3461 unsigned int total_relocated_objs = 0; 3462 unsigned int total_moved_objs = 0; 3463 unsigned int total_invalidated_objs = 0; 3464 unsigned int total_done_objs = 0; 3465 unsigned int id = 0; 3466 3467 seq_puts(m, "\tIdle BOs:\n"); 3468 list_for_each_entry_safe(bo_va, tmp, &vm->idle, base.vm_status) { 3469 if (!bo_va->base.bo) 3470 continue; 3471 total_idle += amdgpu_bo_print_info(id++, bo_va->base.bo, m); 3472 } 3473 total_idle_objs = id; 3474 id = 0; 3475 3476 seq_puts(m, "\tEvicted BOs:\n"); 3477 list_for_each_entry_safe(bo_va, tmp, &vm->evicted, base.vm_status) { 3478 if (!bo_va->base.bo) 3479 continue; 3480 total_evicted += amdgpu_bo_print_info(id++, bo_va->base.bo, m); 3481 } 3482 total_evicted_objs = id; 3483 id = 0; 3484 3485 seq_puts(m, "\tRelocated BOs:\n"); 3486 list_for_each_entry_safe(bo_va, tmp, &vm->relocated, base.vm_status) { 3487 if (!bo_va->base.bo) 3488 continue; 3489 total_relocated += amdgpu_bo_print_info(id++, bo_va->base.bo, m); 3490 } 3491 total_relocated_objs = id; 3492 id = 0; 3493 3494 seq_puts(m, "\tMoved BOs:\n"); 3495 list_for_each_entry_safe(bo_va, tmp, &vm->moved, base.vm_status) { 3496 if (!bo_va->base.bo) 3497 continue; 3498 total_moved += amdgpu_bo_print_info(id++, bo_va->base.bo, m); 3499 } 3500 total_moved_objs = id; 3501 id = 0; 3502 3503 seq_puts(m, "\tInvalidated BOs:\n"); 3504 spin_lock(&vm->invalidated_lock); 3505 list_for_each_entry_safe(bo_va, tmp, &vm->invalidated, base.vm_status) { 3506 if (!bo_va->base.bo) 3507 continue; 3508 total_invalidated += amdgpu_bo_print_info(id++, bo_va->base.bo, m); 3509 } 3510 total_invalidated_objs = id; 3511 id = 0; 3512 3513 seq_puts(m, "\tDone BOs:\n"); 3514 list_for_each_entry_safe(bo_va, tmp, &vm->done, base.vm_status) { 3515 if (!bo_va->base.bo) 3516 continue; 3517 total_done += amdgpu_bo_print_info(id++, bo_va->base.bo, m); 3518 } 3519 spin_unlock(&vm->invalidated_lock); 3520 total_done_objs = id; 3521 3522 seq_printf(m, "\tTotal idle size: %12lld\tobjs:\t%d\n", total_idle, 3523 total_idle_objs); 3524 seq_printf(m, "\tTotal evicted size: %12lld\tobjs:\t%d\n", total_evicted, 3525 total_evicted_objs); 3526 seq_printf(m, "\tTotal relocated size: %12lld\tobjs:\t%d\n", total_relocated, 3527 total_relocated_objs); 3528 seq_printf(m, "\tTotal moved size: %12lld\tobjs:\t%d\n", total_moved, 3529 total_moved_objs); 3530 seq_printf(m, "\tTotal invalidated size: %12lld\tobjs:\t%d\n", total_invalidated, 3531 total_invalidated_objs); 3532 seq_printf(m, "\tTotal done size: %12lld\tobjs:\t%d\n", total_done, 3533 total_done_objs); 3534 } 3535 #endif 3536