1 // SPDX-License-Identifier: GPL-2.0 OR MIT 2 /* 3 * Copyright 2022 Advanced Micro Devices, Inc. 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining a 6 * copy of this software and associated documentation files (the "Software"), 7 * to deal in the Software without restriction, including without limitation 8 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 9 * and/or sell copies of the Software, and to permit persons to whom the 10 * Software is furnished to do so, subject to the following conditions: 11 * 12 * The above copyright notice and this permission notice shall be included in 13 * all copies or substantial portions of the Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 21 * OTHER DEALINGS IN THE SOFTWARE. 22 */ 23 24 #include <drm/drm_drv.h> 25 26 #include "amdgpu.h" 27 #include "amdgpu_trace.h" 28 #include "amdgpu_vm.h" 29 30 /* 31 * amdgpu_vm_pt_cursor - state for for_each_amdgpu_vm_pt 32 */ 33 struct amdgpu_vm_pt_cursor { 34 uint64_t pfn; 35 struct amdgpu_vm_bo_base *parent; 36 struct amdgpu_vm_bo_base *entry; 37 unsigned int level; 38 }; 39 40 /** 41 * amdgpu_vm_pt_level_shift - return the addr shift for each level 42 * 43 * @adev: amdgpu_device pointer 44 * @level: VMPT level 45 * 46 * Returns: 47 * The number of bits the pfn needs to be right shifted for a level. 48 */ 49 static unsigned int amdgpu_vm_pt_level_shift(struct amdgpu_device *adev, 50 unsigned int level) 51 { 52 switch (level) { 53 case AMDGPU_VM_PDB2: 54 case AMDGPU_VM_PDB1: 55 case AMDGPU_VM_PDB0: 56 return 9 * (AMDGPU_VM_PDB0 - level) + 57 adev->vm_manager.block_size; 58 case AMDGPU_VM_PTB: 59 return 0; 60 default: 61 return ~0; 62 } 63 } 64 65 /** 66 * amdgpu_vm_pt_num_entries - return the number of entries in a PD/PT 67 * 68 * @adev: amdgpu_device pointer 69 * @level: VMPT level 70 * 71 * Returns: 72 * The number of entries in a page directory or page table. 73 */ 74 static unsigned int amdgpu_vm_pt_num_entries(struct amdgpu_device *adev, 75 unsigned int level) 76 { 77 unsigned int shift; 78 79 shift = amdgpu_vm_pt_level_shift(adev, adev->vm_manager.root_level); 80 if (level == adev->vm_manager.root_level) 81 /* For the root directory */ 82 return round_up(adev->vm_manager.max_pfn, 1ULL << shift) 83 >> shift; 84 else if (level != AMDGPU_VM_PTB) 85 /* Everything in between */ 86 return 512; 87 88 /* For the page tables on the leaves */ 89 return AMDGPU_VM_PTE_COUNT(adev); 90 } 91 92 /** 93 * amdgpu_vm_pt_entries_mask - the mask to get the entry number of a PD/PT 94 * 95 * @adev: amdgpu_device pointer 96 * @level: VMPT level 97 * 98 * Returns: 99 * The mask to extract the entry number of a PD/PT from an address. 100 */ 101 static uint32_t amdgpu_vm_pt_entries_mask(struct amdgpu_device *adev, 102 unsigned int level) 103 { 104 if (level <= adev->vm_manager.root_level) 105 return 0xffffffff; 106 else if (level != AMDGPU_VM_PTB) 107 return 0x1ff; 108 else 109 return AMDGPU_VM_PTE_COUNT(adev) - 1; 110 } 111 112 /** 113 * amdgpu_vm_pt_size - returns the size of the page table in bytes 114 * 115 * @adev: amdgpu_device pointer 116 * @level: VMPT level 117 * 118 * Returns: 119 * The size of the BO for a page directory or page table in bytes. 120 */ 121 static unsigned int amdgpu_vm_pt_size(struct amdgpu_device *adev, 122 unsigned int level) 123 { 124 return AMDGPU_GPU_PAGE_ALIGN(amdgpu_vm_pt_num_entries(adev, level) * 8); 125 } 126 127 /** 128 * amdgpu_vm_pt_parent - get the parent page directory 129 * 130 * @pt: child page table 131 * 132 * Helper to get the parent entry for the child page table. NULL if we are at 133 * the root page directory. 134 */ 135 static struct amdgpu_vm_bo_base * 136 amdgpu_vm_pt_parent(struct amdgpu_vm_bo_base *pt) 137 { 138 struct amdgpu_bo *parent = pt->bo->parent; 139 140 if (!parent) 141 return NULL; 142 143 return parent->vm_bo; 144 } 145 146 /** 147 * amdgpu_vm_pt_start - start PD/PT walk 148 * 149 * @adev: amdgpu_device pointer 150 * @vm: amdgpu_vm structure 151 * @start: start address of the walk 152 * @cursor: state to initialize 153 * 154 * Initialize a amdgpu_vm_pt_cursor to start a walk. 155 */ 156 static void amdgpu_vm_pt_start(struct amdgpu_device *adev, 157 struct amdgpu_vm *vm, uint64_t start, 158 struct amdgpu_vm_pt_cursor *cursor) 159 { 160 cursor->pfn = start; 161 cursor->parent = NULL; 162 cursor->entry = &vm->root; 163 cursor->level = adev->vm_manager.root_level; 164 } 165 166 /** 167 * amdgpu_vm_pt_descendant - go to child node 168 * 169 * @adev: amdgpu_device pointer 170 * @cursor: current state 171 * 172 * Walk to the child node of the current node. 173 * Returns: 174 * True if the walk was possible, false otherwise. 175 */ 176 static bool amdgpu_vm_pt_descendant(struct amdgpu_device *adev, 177 struct amdgpu_vm_pt_cursor *cursor) 178 { 179 unsigned int mask, shift, idx; 180 181 if ((cursor->level == AMDGPU_VM_PTB) || !cursor->entry || 182 !cursor->entry->bo) 183 return false; 184 185 mask = amdgpu_vm_pt_entries_mask(adev, cursor->level); 186 shift = amdgpu_vm_pt_level_shift(adev, cursor->level); 187 188 ++cursor->level; 189 idx = (cursor->pfn >> shift) & mask; 190 cursor->parent = cursor->entry; 191 cursor->entry = &to_amdgpu_bo_vm(cursor->entry->bo)->entries[idx]; 192 return true; 193 } 194 195 /** 196 * amdgpu_vm_pt_sibling - go to sibling node 197 * 198 * @adev: amdgpu_device pointer 199 * @cursor: current state 200 * 201 * Walk to the sibling node of the current node. 202 * Returns: 203 * True if the walk was possible, false otherwise. 204 */ 205 static bool amdgpu_vm_pt_sibling(struct amdgpu_device *adev, 206 struct amdgpu_vm_pt_cursor *cursor) 207 { 208 209 unsigned int shift, num_entries; 210 struct amdgpu_bo_vm *parent; 211 212 /* Root doesn't have a sibling */ 213 if (!cursor->parent) 214 return false; 215 216 /* Go to our parents and see if we got a sibling */ 217 shift = amdgpu_vm_pt_level_shift(adev, cursor->level - 1); 218 num_entries = amdgpu_vm_pt_num_entries(adev, cursor->level - 1); 219 parent = to_amdgpu_bo_vm(cursor->parent->bo); 220 221 if (cursor->entry == &parent->entries[num_entries - 1]) 222 return false; 223 224 cursor->pfn += 1ULL << shift; 225 cursor->pfn &= ~((1ULL << shift) - 1); 226 ++cursor->entry; 227 return true; 228 } 229 230 /** 231 * amdgpu_vm_pt_ancestor - go to parent node 232 * 233 * @cursor: current state 234 * 235 * Walk to the parent node of the current node. 236 * Returns: 237 * True if the walk was possible, false otherwise. 238 */ 239 static bool amdgpu_vm_pt_ancestor(struct amdgpu_vm_pt_cursor *cursor) 240 { 241 if (!cursor->parent) 242 return false; 243 244 --cursor->level; 245 cursor->entry = cursor->parent; 246 cursor->parent = amdgpu_vm_pt_parent(cursor->parent); 247 return true; 248 } 249 250 /** 251 * amdgpu_vm_pt_next - get next PD/PT in hieratchy 252 * 253 * @adev: amdgpu_device pointer 254 * @cursor: current state 255 * 256 * Walk the PD/PT tree to the next node. 257 */ 258 static void amdgpu_vm_pt_next(struct amdgpu_device *adev, 259 struct amdgpu_vm_pt_cursor *cursor) 260 { 261 /* First try a newborn child */ 262 if (amdgpu_vm_pt_descendant(adev, cursor)) 263 return; 264 265 /* If that didn't worked try to find a sibling */ 266 while (!amdgpu_vm_pt_sibling(adev, cursor)) { 267 /* No sibling, go to our parents and grandparents */ 268 if (!amdgpu_vm_pt_ancestor(cursor)) { 269 cursor->pfn = ~0ll; 270 return; 271 } 272 } 273 } 274 275 /** 276 * amdgpu_vm_pt_first_dfs - start a deep first search 277 * 278 * @adev: amdgpu_device structure 279 * @vm: amdgpu_vm structure 280 * @start: optional cursor to start with 281 * @cursor: state to initialize 282 * 283 * Starts a deep first traversal of the PD/PT tree. 284 */ 285 static void amdgpu_vm_pt_first_dfs(struct amdgpu_device *adev, 286 struct amdgpu_vm *vm, 287 struct amdgpu_vm_pt_cursor *start, 288 struct amdgpu_vm_pt_cursor *cursor) 289 { 290 if (start) 291 *cursor = *start; 292 else 293 amdgpu_vm_pt_start(adev, vm, 0, cursor); 294 295 while (amdgpu_vm_pt_descendant(adev, cursor)) 296 ; 297 } 298 299 /** 300 * amdgpu_vm_pt_continue_dfs - check if the deep first search should continue 301 * 302 * @start: starting point for the search 303 * @entry: current entry 304 * 305 * Returns: 306 * True when the search should continue, false otherwise. 307 */ 308 static bool amdgpu_vm_pt_continue_dfs(struct amdgpu_vm_pt_cursor *start, 309 struct amdgpu_vm_bo_base *entry) 310 { 311 return entry && (!start || entry != start->entry); 312 } 313 314 /** 315 * amdgpu_vm_pt_next_dfs - get the next node for a deep first search 316 * 317 * @adev: amdgpu_device structure 318 * @cursor: current state 319 * 320 * Move the cursor to the next node in a deep first search. 321 */ 322 static void amdgpu_vm_pt_next_dfs(struct amdgpu_device *adev, 323 struct amdgpu_vm_pt_cursor *cursor) 324 { 325 if (!cursor->entry) 326 return; 327 328 if (!cursor->parent) 329 cursor->entry = NULL; 330 else if (amdgpu_vm_pt_sibling(adev, cursor)) 331 while (amdgpu_vm_pt_descendant(adev, cursor)) 332 ; 333 else 334 amdgpu_vm_pt_ancestor(cursor); 335 } 336 337 /* 338 * for_each_amdgpu_vm_pt_dfs_safe - safe deep first search of all PDs/PTs 339 */ 340 #define for_each_amdgpu_vm_pt_dfs_safe(adev, vm, start, cursor, entry) \ 341 for (amdgpu_vm_pt_first_dfs((adev), (vm), (start), &(cursor)), \ 342 (entry) = (cursor).entry, amdgpu_vm_pt_next_dfs((adev), &(cursor));\ 343 amdgpu_vm_pt_continue_dfs((start), (entry)); \ 344 (entry) = (cursor).entry, amdgpu_vm_pt_next_dfs((adev), &(cursor))) 345 346 /** 347 * amdgpu_vm_pt_clear - initially clear the PDs/PTs 348 * 349 * @adev: amdgpu_device pointer 350 * @vm: VM to clear BO from 351 * @vmbo: BO to clear 352 * @immediate: use an immediate update 353 * 354 * Root PD needs to be reserved when calling this. 355 * 356 * Returns: 357 * 0 on success, errno otherwise. 358 */ 359 int amdgpu_vm_pt_clear(struct amdgpu_device *adev, struct amdgpu_vm *vm, 360 struct amdgpu_bo_vm *vmbo, bool immediate) 361 { 362 unsigned int level = adev->vm_manager.root_level; 363 struct ttm_operation_ctx ctx = { true, false }; 364 struct amdgpu_vm_update_params params; 365 struct amdgpu_bo *ancestor = &vmbo->bo; 366 unsigned int entries; 367 struct amdgpu_bo *bo = &vmbo->bo; 368 uint64_t addr; 369 int r, idx; 370 371 /* Figure out our place in the hierarchy */ 372 if (ancestor->parent) { 373 ++level; 374 while (ancestor->parent->parent) { 375 ++level; 376 ancestor = ancestor->parent; 377 } 378 } 379 380 entries = amdgpu_bo_size(bo) / 8; 381 382 r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx); 383 if (r) 384 return r; 385 386 if (vmbo->shadow) { 387 struct amdgpu_bo *shadow = vmbo->shadow; 388 389 r = ttm_bo_validate(&shadow->tbo, &shadow->placement, &ctx); 390 if (r) 391 return r; 392 } 393 394 if (!drm_dev_enter(adev_to_drm(adev), &idx)) 395 return -ENODEV; 396 397 r = vm->update_funcs->map_table(vmbo); 398 if (r) 399 goto exit; 400 401 memset(¶ms, 0, sizeof(params)); 402 params.adev = adev; 403 params.vm = vm; 404 params.immediate = immediate; 405 406 r = vm->update_funcs->prepare(¶ms, NULL, AMDGPU_SYNC_EXPLICIT); 407 if (r) 408 goto exit; 409 410 addr = 0; 411 412 if (entries) { 413 uint64_t value = 0, flags = 0; 414 415 if (adev->asic_type >= CHIP_VEGA10) { 416 if (level != AMDGPU_VM_PTB) { 417 /* Handle leaf PDEs as PTEs */ 418 flags |= AMDGPU_PDE_PTE; 419 amdgpu_gmc_get_vm_pde(adev, level, 420 &value, &flags); 421 } else { 422 /* Workaround for fault priority problem on GMC9 */ 423 flags = AMDGPU_PTE_EXECUTABLE; 424 } 425 } 426 427 r = vm->update_funcs->update(¶ms, vmbo, addr, 0, entries, 428 value, flags); 429 if (r) 430 goto exit; 431 } 432 433 r = vm->update_funcs->commit(¶ms, NULL); 434 exit: 435 drm_dev_exit(idx); 436 return r; 437 } 438 439 /** 440 * amdgpu_vm_pt_create - create bo for PD/PT 441 * 442 * @adev: amdgpu_device pointer 443 * @vm: requesting vm 444 * @level: the page table level 445 * @immediate: use a immediate update 446 * @vmbo: pointer to the buffer object pointer 447 * @xcp_id: GPU partition id 448 */ 449 int amdgpu_vm_pt_create(struct amdgpu_device *adev, struct amdgpu_vm *vm, 450 int level, bool immediate, struct amdgpu_bo_vm **vmbo, 451 int32_t xcp_id) 452 { 453 struct amdgpu_bo_param bp; 454 struct amdgpu_bo *bo; 455 struct dma_resv *resv; 456 unsigned int num_entries; 457 int r; 458 459 memset(&bp, 0, sizeof(bp)); 460 461 bp.size = amdgpu_vm_pt_size(adev, level); 462 bp.byte_align = AMDGPU_GPU_PAGE_SIZE; 463 464 if (!adev->gmc.is_app_apu) 465 bp.domain = AMDGPU_GEM_DOMAIN_VRAM; 466 else 467 bp.domain = AMDGPU_GEM_DOMAIN_GTT; 468 469 bp.domain = amdgpu_bo_get_preferred_domain(adev, bp.domain); 470 bp.flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS | 471 AMDGPU_GEM_CREATE_CPU_GTT_USWC; 472 473 if (level < AMDGPU_VM_PTB) 474 num_entries = amdgpu_vm_pt_num_entries(adev, level); 475 else 476 num_entries = 0; 477 478 bp.bo_ptr_size = struct_size((*vmbo), entries, num_entries); 479 480 if (vm->use_cpu_for_update) 481 bp.flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED; 482 483 bp.type = ttm_bo_type_kernel; 484 bp.no_wait_gpu = immediate; 485 bp.xcp_id_plus1 = xcp_id + 1; 486 487 if (vm->root.bo) 488 bp.resv = vm->root.bo->tbo.base.resv; 489 490 r = amdgpu_bo_create_vm(adev, &bp, vmbo); 491 if (r) 492 return r; 493 494 bo = &(*vmbo)->bo; 495 if (vm->is_compute_context || (adev->flags & AMD_IS_APU)) { 496 (*vmbo)->shadow = NULL; 497 return 0; 498 } 499 500 if (!bp.resv) 501 WARN_ON(dma_resv_lock(bo->tbo.base.resv, 502 NULL)); 503 resv = bp.resv; 504 memset(&bp, 0, sizeof(bp)); 505 bp.size = amdgpu_vm_pt_size(adev, level); 506 bp.domain = AMDGPU_GEM_DOMAIN_GTT; 507 bp.flags = AMDGPU_GEM_CREATE_CPU_GTT_USWC; 508 bp.type = ttm_bo_type_kernel; 509 bp.resv = bo->tbo.base.resv; 510 bp.bo_ptr_size = sizeof(struct amdgpu_bo); 511 bp.xcp_id_plus1 = xcp_id + 1; 512 513 r = amdgpu_bo_create(adev, &bp, &(*vmbo)->shadow); 514 515 if (!resv) 516 dma_resv_unlock(bo->tbo.base.resv); 517 518 if (r) { 519 amdgpu_bo_unref(&bo); 520 return r; 521 } 522 523 amdgpu_bo_add_to_shadow_list(*vmbo); 524 525 return 0; 526 } 527 528 /** 529 * amdgpu_vm_pt_alloc - Allocate a specific page table 530 * 531 * @adev: amdgpu_device pointer 532 * @vm: VM to allocate page tables for 533 * @cursor: Which page table to allocate 534 * @immediate: use an immediate update 535 * 536 * Make sure a specific page table or directory is allocated. 537 * 538 * Returns: 539 * 1 if page table needed to be allocated, 0 if page table was already 540 * allocated, negative errno if an error occurred. 541 */ 542 static int amdgpu_vm_pt_alloc(struct amdgpu_device *adev, 543 struct amdgpu_vm *vm, 544 struct amdgpu_vm_pt_cursor *cursor, 545 bool immediate) 546 { 547 struct amdgpu_vm_bo_base *entry = cursor->entry; 548 struct amdgpu_bo *pt_bo; 549 struct amdgpu_bo_vm *pt; 550 int r; 551 552 if (entry->bo) 553 return 0; 554 555 amdgpu_vm_eviction_unlock(vm); 556 r = amdgpu_vm_pt_create(adev, vm, cursor->level, immediate, &pt, 557 vm->root.bo->xcp_id); 558 amdgpu_vm_eviction_lock(vm); 559 if (r) 560 return r; 561 562 /* Keep a reference to the root directory to avoid 563 * freeing them up in the wrong order. 564 */ 565 pt_bo = &pt->bo; 566 pt_bo->parent = amdgpu_bo_ref(cursor->parent->bo); 567 amdgpu_vm_bo_base_init(entry, vm, pt_bo); 568 r = amdgpu_vm_pt_clear(adev, vm, pt, immediate); 569 if (r) 570 goto error_free_pt; 571 572 return 0; 573 574 error_free_pt: 575 amdgpu_bo_unref(&pt->shadow); 576 amdgpu_bo_unref(&pt_bo); 577 return r; 578 } 579 580 /** 581 * amdgpu_vm_pt_free - free one PD/PT 582 * 583 * @entry: PDE to free 584 */ 585 static void amdgpu_vm_pt_free(struct amdgpu_vm_bo_base *entry) 586 { 587 struct amdgpu_bo *shadow; 588 589 if (!entry->bo) 590 return; 591 592 entry->bo->vm_bo = NULL; 593 shadow = amdgpu_bo_shadowed(entry->bo); 594 if (shadow) { 595 ttm_bo_set_bulk_move(&shadow->tbo, NULL); 596 amdgpu_bo_unref(&shadow); 597 } 598 ttm_bo_set_bulk_move(&entry->bo->tbo, NULL); 599 600 spin_lock(&entry->vm->status_lock); 601 list_del(&entry->vm_status); 602 spin_unlock(&entry->vm->status_lock); 603 amdgpu_bo_unref(&entry->bo); 604 } 605 606 void amdgpu_vm_pt_free_work(struct work_struct *work) 607 { 608 struct amdgpu_vm_bo_base *entry, *next; 609 struct amdgpu_vm *vm; 610 LIST_HEAD(pt_freed); 611 612 vm = container_of(work, struct amdgpu_vm, pt_free_work); 613 614 spin_lock(&vm->status_lock); 615 list_splice_init(&vm->pt_freed, &pt_freed); 616 spin_unlock(&vm->status_lock); 617 618 /* flush_work in amdgpu_vm_fini ensure vm->root.bo is valid. */ 619 amdgpu_bo_reserve(vm->root.bo, true); 620 621 list_for_each_entry_safe(entry, next, &pt_freed, vm_status) 622 amdgpu_vm_pt_free(entry); 623 624 amdgpu_bo_unreserve(vm->root.bo); 625 } 626 627 /** 628 * amdgpu_vm_pt_free_dfs - free PD/PT levels 629 * 630 * @adev: amdgpu device structure 631 * @vm: amdgpu vm structure 632 * @start: optional cursor where to start freeing PDs/PTs 633 * @unlocked: vm resv unlock status 634 * 635 * Free the page directory or page table level and all sub levels. 636 */ 637 static void amdgpu_vm_pt_free_dfs(struct amdgpu_device *adev, 638 struct amdgpu_vm *vm, 639 struct amdgpu_vm_pt_cursor *start, 640 bool unlocked) 641 { 642 struct amdgpu_vm_pt_cursor cursor; 643 struct amdgpu_vm_bo_base *entry; 644 645 if (unlocked) { 646 spin_lock(&vm->status_lock); 647 for_each_amdgpu_vm_pt_dfs_safe(adev, vm, start, cursor, entry) 648 list_move(&entry->vm_status, &vm->pt_freed); 649 650 if (start) 651 list_move(&start->entry->vm_status, &vm->pt_freed); 652 spin_unlock(&vm->status_lock); 653 schedule_work(&vm->pt_free_work); 654 return; 655 } 656 657 for_each_amdgpu_vm_pt_dfs_safe(adev, vm, start, cursor, entry) 658 amdgpu_vm_pt_free(entry); 659 660 if (start) 661 amdgpu_vm_pt_free(start->entry); 662 } 663 664 /** 665 * amdgpu_vm_pt_free_root - free root PD 666 * @adev: amdgpu device structure 667 * @vm: amdgpu vm structure 668 * 669 * Free the root page directory and everything below it. 670 */ 671 void amdgpu_vm_pt_free_root(struct amdgpu_device *adev, struct amdgpu_vm *vm) 672 { 673 amdgpu_vm_pt_free_dfs(adev, vm, NULL, false); 674 } 675 676 /** 677 * amdgpu_vm_pt_is_root_clean - check if a root PD is clean 678 * 679 * @adev: amdgpu_device pointer 680 * @vm: the VM to check 681 * 682 * Check all entries of the root PD, if any subsequent PDs are allocated, 683 * it means there are page table creating and filling, and is no a clean 684 * VM 685 * 686 * Returns: 687 * 0 if this VM is clean 688 */ 689 bool amdgpu_vm_pt_is_root_clean(struct amdgpu_device *adev, 690 struct amdgpu_vm *vm) 691 { 692 enum amdgpu_vm_level root = adev->vm_manager.root_level; 693 unsigned int entries = amdgpu_vm_pt_num_entries(adev, root); 694 unsigned int i = 0; 695 696 for (i = 0; i < entries; i++) { 697 if (to_amdgpu_bo_vm(vm->root.bo)->entries[i].bo) 698 return false; 699 } 700 return true; 701 } 702 703 /** 704 * amdgpu_vm_pde_update - update a single level in the hierarchy 705 * 706 * @params: parameters for the update 707 * @entry: entry to update 708 * 709 * Makes sure the requested entry in parent is up to date. 710 */ 711 int amdgpu_vm_pde_update(struct amdgpu_vm_update_params *params, 712 struct amdgpu_vm_bo_base *entry) 713 { 714 struct amdgpu_vm_bo_base *parent = amdgpu_vm_pt_parent(entry); 715 struct amdgpu_bo *bo = parent->bo, *pbo; 716 struct amdgpu_vm *vm = params->vm; 717 uint64_t pde, pt, flags; 718 unsigned int level; 719 720 for (level = 0, pbo = bo->parent; pbo; ++level) 721 pbo = pbo->parent; 722 723 level += params->adev->vm_manager.root_level; 724 amdgpu_gmc_get_pde_for_bo(entry->bo, level, &pt, &flags); 725 pde = (entry - to_amdgpu_bo_vm(parent->bo)->entries) * 8; 726 return vm->update_funcs->update(params, to_amdgpu_bo_vm(bo), pde, pt, 727 1, 0, flags); 728 } 729 730 /** 731 * amdgpu_vm_pte_update_noretry_flags - Update PTE no-retry flags 732 * 733 * @adev: amdgpu_device pointer 734 * @flags: pointer to PTE flags 735 * 736 * Update PTE no-retry flags when TF is enabled. 737 */ 738 static void amdgpu_vm_pte_update_noretry_flags(struct amdgpu_device *adev, 739 uint64_t *flags) 740 { 741 /* 742 * Update no-retry flags with the corresponding TF 743 * no-retry combination. 744 */ 745 if ((*flags & AMDGPU_VM_NORETRY_FLAGS) == AMDGPU_VM_NORETRY_FLAGS) { 746 *flags &= ~AMDGPU_VM_NORETRY_FLAGS; 747 *flags |= adev->gmc.noretry_flags; 748 } 749 } 750 751 /* 752 * amdgpu_vm_pte_update_flags - figure out flags for PTE updates 753 * 754 * Make sure to set the right flags for the PTEs at the desired level. 755 */ 756 static void amdgpu_vm_pte_update_flags(struct amdgpu_vm_update_params *params, 757 struct amdgpu_bo_vm *pt, 758 unsigned int level, 759 uint64_t pe, uint64_t addr, 760 unsigned int count, uint32_t incr, 761 uint64_t flags) 762 { 763 struct amdgpu_device *adev = params->adev; 764 765 if (level != AMDGPU_VM_PTB) { 766 flags |= AMDGPU_PDE_PTE; 767 amdgpu_gmc_get_vm_pde(adev, level, &addr, &flags); 768 769 } else if (adev->asic_type >= CHIP_VEGA10 && 770 !(flags & AMDGPU_PTE_VALID) && 771 !(flags & AMDGPU_PTE_PRT)) { 772 773 /* Workaround for fault priority problem on GMC9 */ 774 flags |= AMDGPU_PTE_EXECUTABLE; 775 } 776 777 /* 778 * Update no-retry flags to use the no-retry flag combination 779 * with TF enabled. The AMDGPU_VM_NORETRY_FLAGS flag combination 780 * does not work when TF is enabled. So, replace them with 781 * AMDGPU_VM_NORETRY_FLAGS_TF flag combination which works for 782 * all cases. 783 */ 784 if (level == AMDGPU_VM_PTB) 785 amdgpu_vm_pte_update_noretry_flags(adev, &flags); 786 787 /* APUs mapping system memory may need different MTYPEs on different 788 * NUMA nodes. Only do this for contiguous ranges that can be assumed 789 * to be on the same NUMA node. 790 */ 791 if ((flags & AMDGPU_PTE_SYSTEM) && (adev->flags & AMD_IS_APU) && 792 adev->gmc.gmc_funcs->override_vm_pte_flags && 793 num_possible_nodes() > 1 && !params->pages_addr && params->allow_override) 794 amdgpu_gmc_override_vm_pte_flags(adev, params->vm, addr, &flags); 795 796 params->vm->update_funcs->update(params, pt, pe, addr, count, incr, 797 flags); 798 } 799 800 /** 801 * amdgpu_vm_pte_fragment - get fragment for PTEs 802 * 803 * @params: see amdgpu_vm_update_params definition 804 * @start: first PTE to handle 805 * @end: last PTE to handle 806 * @flags: hw mapping flags 807 * @frag: resulting fragment size 808 * @frag_end: end of this fragment 809 * 810 * Returns the first possible fragment for the start and end address. 811 */ 812 static void amdgpu_vm_pte_fragment(struct amdgpu_vm_update_params *params, 813 uint64_t start, uint64_t end, uint64_t flags, 814 unsigned int *frag, uint64_t *frag_end) 815 { 816 /** 817 * The MC L1 TLB supports variable sized pages, based on a fragment 818 * field in the PTE. When this field is set to a non-zero value, page 819 * granularity is increased from 4KB to (1 << (12 + frag)). The PTE 820 * flags are considered valid for all PTEs within the fragment range 821 * and corresponding mappings are assumed to be physically contiguous. 822 * 823 * The L1 TLB can store a single PTE for the whole fragment, 824 * significantly increasing the space available for translation 825 * caching. This leads to large improvements in throughput when the 826 * TLB is under pressure. 827 * 828 * The L2 TLB distributes small and large fragments into two 829 * asymmetric partitions. The large fragment cache is significantly 830 * larger. Thus, we try to use large fragments wherever possible. 831 * Userspace can support this by aligning virtual base address and 832 * allocation size to the fragment size. 833 * 834 * Starting with Vega10 the fragment size only controls the L1. The L2 835 * is now directly feed with small/huge/giant pages from the walker. 836 */ 837 unsigned int max_frag; 838 839 if (params->adev->asic_type < CHIP_VEGA10) 840 max_frag = params->adev->vm_manager.fragment_size; 841 else 842 max_frag = 31; 843 844 /* system pages are non continuously */ 845 if (params->pages_addr) { 846 *frag = 0; 847 *frag_end = end; 848 return; 849 } 850 851 /* This intentionally wraps around if no bit is set */ 852 *frag = min_t(unsigned int, ffs(start) - 1, fls64(end - start) - 1); 853 if (*frag >= max_frag) { 854 *frag = max_frag; 855 *frag_end = end & ~((1ULL << max_frag) - 1); 856 } else { 857 *frag_end = start + (1 << *frag); 858 } 859 } 860 861 /** 862 * amdgpu_vm_ptes_update - make sure that page tables are valid 863 * 864 * @params: see amdgpu_vm_update_params definition 865 * @start: start of GPU address range 866 * @end: end of GPU address range 867 * @dst: destination address to map to, the next dst inside the function 868 * @flags: mapping flags 869 * 870 * Update the page tables in the range @start - @end. 871 * 872 * Returns: 873 * 0 for success, -EINVAL for failure. 874 */ 875 int amdgpu_vm_ptes_update(struct amdgpu_vm_update_params *params, 876 uint64_t start, uint64_t end, 877 uint64_t dst, uint64_t flags) 878 { 879 struct amdgpu_device *adev = params->adev; 880 struct amdgpu_vm_pt_cursor cursor; 881 uint64_t frag_start = start, frag_end; 882 unsigned int frag; 883 int r; 884 885 /* figure out the initial fragment */ 886 amdgpu_vm_pte_fragment(params, frag_start, end, flags, &frag, 887 &frag_end); 888 889 /* walk over the address space and update the PTs */ 890 amdgpu_vm_pt_start(adev, params->vm, start, &cursor); 891 while (cursor.pfn < end) { 892 unsigned int shift, parent_shift, mask; 893 uint64_t incr, entry_end, pe_start; 894 struct amdgpu_bo *pt; 895 896 if (!params->unlocked) { 897 /* make sure that the page tables covering the 898 * address range are actually allocated 899 */ 900 r = amdgpu_vm_pt_alloc(params->adev, params->vm, 901 &cursor, params->immediate); 902 if (r) 903 return r; 904 } 905 906 shift = amdgpu_vm_pt_level_shift(adev, cursor.level); 907 parent_shift = amdgpu_vm_pt_level_shift(adev, cursor.level - 1); 908 if (params->unlocked) { 909 /* Unlocked updates are only allowed on the leaves */ 910 if (amdgpu_vm_pt_descendant(adev, &cursor)) 911 continue; 912 } else if (adev->asic_type < CHIP_VEGA10 && 913 (flags & AMDGPU_PTE_VALID)) { 914 /* No huge page support before GMC v9 */ 915 if (cursor.level != AMDGPU_VM_PTB) { 916 if (!amdgpu_vm_pt_descendant(adev, &cursor)) 917 return -ENOENT; 918 continue; 919 } 920 } else if (frag < shift) { 921 /* We can't use this level when the fragment size is 922 * smaller than the address shift. Go to the next 923 * child entry and try again. 924 */ 925 if (amdgpu_vm_pt_descendant(adev, &cursor)) 926 continue; 927 } else if (frag >= parent_shift) { 928 /* If the fragment size is even larger than the parent 929 * shift we should go up one level and check it again. 930 */ 931 if (!amdgpu_vm_pt_ancestor(&cursor)) 932 return -EINVAL; 933 continue; 934 } 935 936 pt = cursor.entry->bo; 937 if (!pt) { 938 /* We need all PDs and PTs for mapping something, */ 939 if (flags & AMDGPU_PTE_VALID) 940 return -ENOENT; 941 942 /* but unmapping something can happen at a higher 943 * level. 944 */ 945 if (!amdgpu_vm_pt_ancestor(&cursor)) 946 return -EINVAL; 947 948 pt = cursor.entry->bo; 949 shift = parent_shift; 950 frag_end = max(frag_end, ALIGN(frag_start + 1, 951 1ULL << shift)); 952 } 953 954 /* Looks good so far, calculate parameters for the update */ 955 incr = (uint64_t)AMDGPU_GPU_PAGE_SIZE << shift; 956 mask = amdgpu_vm_pt_entries_mask(adev, cursor.level); 957 pe_start = ((cursor.pfn >> shift) & mask) * 8; 958 entry_end = ((uint64_t)mask + 1) << shift; 959 entry_end += cursor.pfn & ~(entry_end - 1); 960 entry_end = min(entry_end, end); 961 962 do { 963 struct amdgpu_vm *vm = params->vm; 964 uint64_t upd_end = min(entry_end, frag_end); 965 unsigned int nptes = (upd_end - frag_start) >> shift; 966 uint64_t upd_flags = flags | AMDGPU_PTE_FRAG(frag); 967 968 /* This can happen when we set higher level PDs to 969 * silent to stop fault floods. 970 */ 971 nptes = max(nptes, 1u); 972 973 trace_amdgpu_vm_update_ptes(params, frag_start, upd_end, 974 min(nptes, 32u), dst, incr, 975 upd_flags, 976 vm->task_info.tgid, 977 vm->immediate.fence_context); 978 amdgpu_vm_pte_update_flags(params, to_amdgpu_bo_vm(pt), 979 cursor.level, pe_start, dst, 980 nptes, incr, upd_flags); 981 982 pe_start += nptes * 8; 983 dst += nptes * incr; 984 985 frag_start = upd_end; 986 if (frag_start >= frag_end) { 987 /* figure out the next fragment */ 988 amdgpu_vm_pte_fragment(params, frag_start, end, 989 flags, &frag, &frag_end); 990 if (frag < shift) 991 break; 992 } 993 } while (frag_start < entry_end); 994 995 if (amdgpu_vm_pt_descendant(adev, &cursor)) { 996 /* Free all child entries. 997 * Update the tables with the flags and addresses and free up subsequent 998 * tables in the case of huge pages or freed up areas. 999 * This is the maximum you can free, because all other page tables are not 1000 * completely covered by the range and so potentially still in use. 1001 */ 1002 while (cursor.pfn < frag_start) { 1003 /* Make sure previous mapping is freed */ 1004 if (cursor.entry->bo) { 1005 params->table_freed = true; 1006 amdgpu_vm_pt_free_dfs(adev, params->vm, 1007 &cursor, 1008 params->unlocked); 1009 } 1010 amdgpu_vm_pt_next(adev, &cursor); 1011 } 1012 1013 } else if (frag >= shift) { 1014 /* or just move on to the next on the same level. */ 1015 amdgpu_vm_pt_next(adev, &cursor); 1016 } 1017 } 1018 1019 return 0; 1020 } 1021 1022 /** 1023 * amdgpu_vm_pt_map_tables - have bo of root PD cpu accessible 1024 * @adev: amdgpu device structure 1025 * @vm: amdgpu vm structure 1026 * 1027 * make root page directory and everything below it cpu accessible. 1028 */ 1029 int amdgpu_vm_pt_map_tables(struct amdgpu_device *adev, struct amdgpu_vm *vm) 1030 { 1031 struct amdgpu_vm_pt_cursor cursor; 1032 struct amdgpu_vm_bo_base *entry; 1033 1034 for_each_amdgpu_vm_pt_dfs_safe(adev, vm, NULL, cursor, entry) { 1035 1036 struct amdgpu_bo_vm *bo; 1037 int r; 1038 1039 if (entry->bo) { 1040 bo = to_amdgpu_bo_vm(entry->bo); 1041 r = vm->update_funcs->map_table(bo); 1042 if (r) 1043 return r; 1044 } 1045 } 1046 1047 return 0; 1048 } 1049