1 // SPDX-License-Identifier: GPL-2.0 OR MIT
3 * Copyright 2022 Advanced Micro Devices, Inc.
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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.
24 #include <drm/drm_drv.h>
27 #include "amdgpu_trace.h"
28 #include "amdgpu_vm.h"
31 * amdgpu_vm_pt_cursor - state for for_each_amdgpu_vm_pt
33 struct amdgpu_vm_pt_cursor {
35 struct amdgpu_vm_bo_base *parent;
36 struct amdgpu_vm_bo_base *entry;
41 * amdgpu_vm_pt_level_shift - return the addr shift for each level
43 * @adev: amdgpu_device pointer
47 * The number of bits the pfn needs to be right shifted for a level.
49 static unsigned int amdgpu_vm_pt_level_shift(struct amdgpu_device *adev,
56 return 9 * (AMDGPU_VM_PDB0 - level) +
57 adev->vm_manager.block_size;
66 * amdgpu_vm_pt_num_entries - return the number of entries in a PD/PT
68 * @adev: amdgpu_device pointer
72 * The number of entries in a page directory or page table.
74 static unsigned int amdgpu_vm_pt_num_entries(struct amdgpu_device *adev,
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)
84 else if (level != AMDGPU_VM_PTB)
85 /* Everything in between */
88 /* For the page tables on the leaves */
89 return AMDGPU_VM_PTE_COUNT(adev);
93 * amdgpu_vm_pt_num_ats_entries - return the number of ATS entries in the root PD
95 * @adev: amdgpu_device pointer
98 * The number of entries in the root page directory which needs the ATS setting.
100 static unsigned int amdgpu_vm_pt_num_ats_entries(struct amdgpu_device *adev)
104 shift = amdgpu_vm_pt_level_shift(adev, adev->vm_manager.root_level);
105 return AMDGPU_GMC_HOLE_START >> (shift + AMDGPU_GPU_PAGE_SHIFT);
109 * amdgpu_vm_pt_entries_mask - the mask to get the entry number of a PD/PT
111 * @adev: amdgpu_device pointer
115 * The mask to extract the entry number of a PD/PT from an address.
117 static uint32_t amdgpu_vm_pt_entries_mask(struct amdgpu_device *adev,
120 if (level <= adev->vm_manager.root_level)
122 else if (level != AMDGPU_VM_PTB)
125 return AMDGPU_VM_PTE_COUNT(adev) - 1;
129 * amdgpu_vm_pt_size - returns the size of the page table in bytes
131 * @adev: amdgpu_device pointer
135 * The size of the BO for a page directory or page table in bytes.
137 static unsigned int amdgpu_vm_pt_size(struct amdgpu_device *adev,
140 return AMDGPU_GPU_PAGE_ALIGN(amdgpu_vm_pt_num_entries(adev, level) * 8);
144 * amdgpu_vm_pt_parent - get the parent page directory
146 * @pt: child page table
148 * Helper to get the parent entry for the child page table. NULL if we are at
149 * the root page directory.
151 static struct amdgpu_vm_bo_base *
152 amdgpu_vm_pt_parent(struct amdgpu_vm_bo_base *pt)
154 struct amdgpu_bo *parent = pt->bo->parent;
159 return parent->vm_bo;
163 * amdgpu_vm_pt_start - start PD/PT walk
165 * @adev: amdgpu_device pointer
166 * @vm: amdgpu_vm structure
167 * @start: start address of the walk
168 * @cursor: state to initialize
170 * Initialize a amdgpu_vm_pt_cursor to start a walk.
172 static void amdgpu_vm_pt_start(struct amdgpu_device *adev,
173 struct amdgpu_vm *vm, uint64_t start,
174 struct amdgpu_vm_pt_cursor *cursor)
177 cursor->parent = NULL;
178 cursor->entry = &vm->root;
179 cursor->level = adev->vm_manager.root_level;
183 * amdgpu_vm_pt_descendant - go to child node
185 * @adev: amdgpu_device pointer
186 * @cursor: current state
188 * Walk to the child node of the current node.
190 * True if the walk was possible, false otherwise.
192 static bool amdgpu_vm_pt_descendant(struct amdgpu_device *adev,
193 struct amdgpu_vm_pt_cursor *cursor)
195 unsigned int mask, shift, idx;
197 if ((cursor->level == AMDGPU_VM_PTB) || !cursor->entry ||
201 mask = amdgpu_vm_pt_entries_mask(adev, cursor->level);
202 shift = amdgpu_vm_pt_level_shift(adev, cursor->level);
205 idx = (cursor->pfn >> shift) & mask;
206 cursor->parent = cursor->entry;
207 cursor->entry = &to_amdgpu_bo_vm(cursor->entry->bo)->entries[idx];
212 * amdgpu_vm_pt_sibling - go to sibling node
214 * @adev: amdgpu_device pointer
215 * @cursor: current state
217 * Walk to the sibling node of the current node.
219 * True if the walk was possible, false otherwise.
221 static bool amdgpu_vm_pt_sibling(struct amdgpu_device *adev,
222 struct amdgpu_vm_pt_cursor *cursor)
225 unsigned int shift, num_entries;
226 struct amdgpu_bo_vm *parent;
228 /* Root doesn't have a sibling */
232 /* Go to our parents and see if we got a sibling */
233 shift = amdgpu_vm_pt_level_shift(adev, cursor->level - 1);
234 num_entries = amdgpu_vm_pt_num_entries(adev, cursor->level - 1);
235 parent = to_amdgpu_bo_vm(cursor->parent->bo);
237 if (cursor->entry == &parent->entries[num_entries - 1])
240 cursor->pfn += 1ULL << shift;
241 cursor->pfn &= ~((1ULL << shift) - 1);
247 * amdgpu_vm_pt_ancestor - go to parent node
249 * @cursor: current state
251 * Walk to the parent node of the current node.
253 * True if the walk was possible, false otherwise.
255 static bool amdgpu_vm_pt_ancestor(struct amdgpu_vm_pt_cursor *cursor)
261 cursor->entry = cursor->parent;
262 cursor->parent = amdgpu_vm_pt_parent(cursor->parent);
267 * amdgpu_vm_pt_next - get next PD/PT in hieratchy
269 * @adev: amdgpu_device pointer
270 * @cursor: current state
272 * Walk the PD/PT tree to the next node.
274 static void amdgpu_vm_pt_next(struct amdgpu_device *adev,
275 struct amdgpu_vm_pt_cursor *cursor)
277 /* First try a newborn child */
278 if (amdgpu_vm_pt_descendant(adev, cursor))
281 /* If that didn't worked try to find a sibling */
282 while (!amdgpu_vm_pt_sibling(adev, cursor)) {
283 /* No sibling, go to our parents and grandparents */
284 if (!amdgpu_vm_pt_ancestor(cursor)) {
292 * amdgpu_vm_pt_first_dfs - start a deep first search
294 * @adev: amdgpu_device structure
295 * @vm: amdgpu_vm structure
296 * @start: optional cursor to start with
297 * @cursor: state to initialize
299 * Starts a deep first traversal of the PD/PT tree.
301 static void amdgpu_vm_pt_first_dfs(struct amdgpu_device *adev,
302 struct amdgpu_vm *vm,
303 struct amdgpu_vm_pt_cursor *start,
304 struct amdgpu_vm_pt_cursor *cursor)
309 amdgpu_vm_pt_start(adev, vm, 0, cursor);
311 while (amdgpu_vm_pt_descendant(adev, cursor))
316 * amdgpu_vm_pt_continue_dfs - check if the deep first search should continue
318 * @start: starting point for the search
319 * @entry: current entry
322 * True when the search should continue, false otherwise.
324 static bool amdgpu_vm_pt_continue_dfs(struct amdgpu_vm_pt_cursor *start,
325 struct amdgpu_vm_bo_base *entry)
327 return entry && (!start || entry != start->entry);
331 * amdgpu_vm_pt_next_dfs - get the next node for a deep first search
333 * @adev: amdgpu_device structure
334 * @cursor: current state
336 * Move the cursor to the next node in a deep first search.
338 static void amdgpu_vm_pt_next_dfs(struct amdgpu_device *adev,
339 struct amdgpu_vm_pt_cursor *cursor)
345 cursor->entry = NULL;
346 else if (amdgpu_vm_pt_sibling(adev, cursor))
347 while (amdgpu_vm_pt_descendant(adev, cursor))
350 amdgpu_vm_pt_ancestor(cursor);
354 * for_each_amdgpu_vm_pt_dfs_safe - safe deep first search of all PDs/PTs
356 #define for_each_amdgpu_vm_pt_dfs_safe(adev, vm, start, cursor, entry) \
357 for (amdgpu_vm_pt_first_dfs((adev), (vm), (start), &(cursor)), \
358 (entry) = (cursor).entry, amdgpu_vm_pt_next_dfs((adev), &(cursor));\
359 amdgpu_vm_pt_continue_dfs((start), (entry)); \
360 (entry) = (cursor).entry, amdgpu_vm_pt_next_dfs((adev), &(cursor)))
363 * amdgpu_vm_pt_clear - initially clear the PDs/PTs
365 * @adev: amdgpu_device pointer
366 * @vm: VM to clear BO from
368 * @immediate: use an immediate update
370 * Root PD needs to be reserved when calling this.
373 * 0 on success, errno otherwise.
375 int amdgpu_vm_pt_clear(struct amdgpu_device *adev, struct amdgpu_vm *vm,
376 struct amdgpu_bo_vm *vmbo, bool immediate)
378 unsigned int level = adev->vm_manager.root_level;
379 struct ttm_operation_ctx ctx = { true, false };
380 struct amdgpu_vm_update_params params;
381 struct amdgpu_bo *ancestor = &vmbo->bo;
382 unsigned int entries, ats_entries;
383 struct amdgpu_bo *bo = &vmbo->bo;
387 /* Figure out our place in the hierarchy */
388 if (ancestor->parent) {
390 while (ancestor->parent->parent) {
392 ancestor = ancestor->parent;
396 entries = amdgpu_bo_size(bo) / 8;
397 if (!vm->pte_support_ats) {
400 } else if (!bo->parent) {
401 ats_entries = amdgpu_vm_pt_num_ats_entries(adev);
402 ats_entries = min(ats_entries, entries);
403 entries -= ats_entries;
406 struct amdgpu_vm_bo_base *pt;
408 pt = ancestor->vm_bo;
409 ats_entries = amdgpu_vm_pt_num_ats_entries(adev);
410 if ((pt - to_amdgpu_bo_vm(vm->root.bo)->entries) >=
414 ats_entries = entries;
419 r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
424 struct amdgpu_bo *shadow = vmbo->shadow;
426 r = ttm_bo_validate(&shadow->tbo, &shadow->placement, &ctx);
431 if (!drm_dev_enter(adev_to_drm(adev), &idx))
434 r = vm->update_funcs->map_table(vmbo);
438 memset(¶ms, 0, sizeof(params));
441 params.immediate = immediate;
443 r = vm->update_funcs->prepare(¶ms, NULL, AMDGPU_SYNC_EXPLICIT);
449 uint64_t value = 0, flags;
451 flags = AMDGPU_PTE_DEFAULT_ATC;
452 if (level != AMDGPU_VM_PTB) {
453 /* Handle leaf PDEs as PTEs */
454 flags |= AMDGPU_PDE_PTE;
455 amdgpu_gmc_get_vm_pde(adev, level, &value, &flags);
458 r = vm->update_funcs->update(¶ms, vmbo, addr, 0,
459 ats_entries, value, flags);
463 addr += ats_entries * 8;
467 uint64_t value = 0, flags = 0;
469 if (adev->asic_type >= CHIP_VEGA10) {
470 if (level != AMDGPU_VM_PTB) {
471 /* Handle leaf PDEs as PTEs */
472 flags |= AMDGPU_PDE_PTE;
473 amdgpu_gmc_get_vm_pde(adev, level,
476 /* Workaround for fault priority problem on GMC9 */
477 flags = AMDGPU_PTE_EXECUTABLE;
481 r = vm->update_funcs->update(¶ms, vmbo, addr, 0, entries,
487 r = vm->update_funcs->commit(¶ms, NULL);
494 * amdgpu_vm_pt_create - create bo for PD/PT
496 * @adev: amdgpu_device pointer
498 * @level: the page table level
499 * @immediate: use a immediate update
500 * @vmbo: pointer to the buffer object pointer
502 int amdgpu_vm_pt_create(struct amdgpu_device *adev, struct amdgpu_vm *vm,
503 int level, bool immediate, struct amdgpu_bo_vm **vmbo)
505 struct amdgpu_bo_param bp;
506 struct amdgpu_bo *bo;
507 struct dma_resv *resv;
508 unsigned int num_entries;
511 memset(&bp, 0, sizeof(bp));
513 bp.size = amdgpu_vm_pt_size(adev, level);
514 bp.byte_align = AMDGPU_GPU_PAGE_SIZE;
515 bp.domain = AMDGPU_GEM_DOMAIN_VRAM;
516 bp.domain = amdgpu_bo_get_preferred_domain(adev, bp.domain);
517 bp.flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS |
518 AMDGPU_GEM_CREATE_CPU_GTT_USWC;
520 if (level < AMDGPU_VM_PTB)
521 num_entries = amdgpu_vm_pt_num_entries(adev, level);
525 bp.bo_ptr_size = struct_size((*vmbo), entries, num_entries);
527 if (vm->use_cpu_for_update)
528 bp.flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
530 bp.type = ttm_bo_type_kernel;
531 bp.no_wait_gpu = immediate;
533 bp.resv = vm->root.bo->tbo.base.resv;
535 r = amdgpu_bo_create_vm(adev, &bp, vmbo);
540 if (vm->is_compute_context || (adev->flags & AMD_IS_APU)) {
541 (*vmbo)->shadow = NULL;
546 WARN_ON(dma_resv_lock(bo->tbo.base.resv,
549 memset(&bp, 0, sizeof(bp));
550 bp.size = amdgpu_vm_pt_size(adev, level);
551 bp.domain = AMDGPU_GEM_DOMAIN_GTT;
552 bp.flags = AMDGPU_GEM_CREATE_CPU_GTT_USWC;
553 bp.type = ttm_bo_type_kernel;
554 bp.resv = bo->tbo.base.resv;
555 bp.bo_ptr_size = sizeof(struct amdgpu_bo);
557 r = amdgpu_bo_create(adev, &bp, &(*vmbo)->shadow);
560 dma_resv_unlock(bo->tbo.base.resv);
563 amdgpu_bo_unref(&bo);
567 (*vmbo)->shadow->parent = amdgpu_bo_ref(bo);
568 amdgpu_bo_add_to_shadow_list(*vmbo);
574 * amdgpu_vm_pt_alloc - Allocate a specific page table
576 * @adev: amdgpu_device pointer
577 * @vm: VM to allocate page tables for
578 * @cursor: Which page table to allocate
579 * @immediate: use an immediate update
581 * Make sure a specific page table or directory is allocated.
584 * 1 if page table needed to be allocated, 0 if page table was already
585 * allocated, negative errno if an error occurred.
587 static int amdgpu_vm_pt_alloc(struct amdgpu_device *adev,
588 struct amdgpu_vm *vm,
589 struct amdgpu_vm_pt_cursor *cursor,
592 struct amdgpu_vm_bo_base *entry = cursor->entry;
593 struct amdgpu_bo *pt_bo;
594 struct amdgpu_bo_vm *pt;
600 r = amdgpu_vm_pt_create(adev, vm, cursor->level, immediate, &pt);
604 /* Keep a reference to the root directory to avoid
605 * freeing them up in the wrong order.
608 pt_bo->parent = amdgpu_bo_ref(cursor->parent->bo);
609 amdgpu_vm_bo_base_init(entry, vm, pt_bo);
610 r = amdgpu_vm_pt_clear(adev, vm, pt, immediate);
617 amdgpu_bo_unref(&pt->shadow);
618 amdgpu_bo_unref(&pt_bo);
623 * amdgpu_vm_pt_free - free one PD/PT
625 * @entry: PDE to free
627 static void amdgpu_vm_pt_free(struct amdgpu_vm_bo_base *entry)
629 struct amdgpu_bo *shadow;
633 shadow = amdgpu_bo_shadowed(entry->bo);
635 ttm_bo_set_bulk_move(&shadow->tbo, NULL);
636 amdgpu_bo_unref(&shadow);
638 ttm_bo_set_bulk_move(&entry->bo->tbo, NULL);
639 entry->bo->vm_bo = NULL;
640 list_del(&entry->vm_status);
641 amdgpu_bo_unref(&entry->bo);
645 * amdgpu_vm_pt_free_dfs - free PD/PT levels
647 * @adev: amdgpu device structure
648 * @vm: amdgpu vm structure
649 * @start: optional cursor where to start freeing PDs/PTs
651 * Free the page directory or page table level and all sub levels.
653 static void amdgpu_vm_pt_free_dfs(struct amdgpu_device *adev,
654 struct amdgpu_vm *vm,
655 struct amdgpu_vm_pt_cursor *start)
657 struct amdgpu_vm_pt_cursor cursor;
658 struct amdgpu_vm_bo_base *entry;
660 for_each_amdgpu_vm_pt_dfs_safe(adev, vm, start, cursor, entry)
661 amdgpu_vm_pt_free(entry);
664 amdgpu_vm_pt_free(start->entry);
668 * amdgpu_vm_pt_free_root - free root PD
669 * @adev: amdgpu device structure
670 * @vm: amdgpu vm structure
672 * Free the root page directory and everything below it.
674 void amdgpu_vm_pt_free_root(struct amdgpu_device *adev, struct amdgpu_vm *vm)
676 amdgpu_vm_pt_free_dfs(adev, vm, NULL);
680 * amdgpu_vm_pt_is_root_clean - check if a root PD is clean
682 * @adev: amdgpu_device pointer
683 * @vm: the VM to check
685 * Check all entries of the root PD, if any subsequent PDs are allocated,
686 * it means there are page table creating and filling, and is no a clean
690 * 0 if this VM is clean
692 bool amdgpu_vm_pt_is_root_clean(struct amdgpu_device *adev,
693 struct amdgpu_vm *vm)
695 enum amdgpu_vm_level root = adev->vm_manager.root_level;
696 unsigned int entries = amdgpu_vm_pt_num_entries(adev, root);
699 for (i = 0; i < entries; i++) {
700 if (to_amdgpu_bo_vm(vm->root.bo)->entries[i].bo)
707 * amdgpu_vm_pde_update - update a single level in the hierarchy
709 * @params: parameters for the update
710 * @entry: entry to update
712 * Makes sure the requested entry in parent is up to date.
714 int amdgpu_vm_pde_update(struct amdgpu_vm_update_params *params,
715 struct amdgpu_vm_bo_base *entry)
717 struct amdgpu_vm_bo_base *parent = amdgpu_vm_pt_parent(entry);
718 struct amdgpu_bo *bo = parent->bo, *pbo;
719 struct amdgpu_vm *vm = params->vm;
720 uint64_t pde, pt, flags;
723 for (level = 0, pbo = bo->parent; pbo; ++level)
726 level += params->adev->vm_manager.root_level;
727 amdgpu_gmc_get_pde_for_bo(entry->bo, level, &pt, &flags);
728 pde = (entry - to_amdgpu_bo_vm(parent->bo)->entries) * 8;
729 return vm->update_funcs->update(params, to_amdgpu_bo_vm(bo), pde, pt,
734 * amdgpu_vm_pte_update_flags - figure out flags for PTE updates
736 * Make sure to set the right flags for the PTEs at the desired level.
738 static void amdgpu_vm_pte_update_flags(struct amdgpu_vm_update_params *params,
739 struct amdgpu_bo_vm *pt,
741 uint64_t pe, uint64_t addr,
742 unsigned int count, uint32_t incr,
746 if (level != AMDGPU_VM_PTB) {
747 flags |= AMDGPU_PDE_PTE;
748 amdgpu_gmc_get_vm_pde(params->adev, level, &addr, &flags);
750 } else if (params->adev->asic_type >= CHIP_VEGA10 &&
751 !(flags & AMDGPU_PTE_VALID) &&
752 !(flags & AMDGPU_PTE_PRT)) {
754 /* Workaround for fault priority problem on GMC9 */
755 flags |= AMDGPU_PTE_EXECUTABLE;
758 params->vm->update_funcs->update(params, pt, pe, addr, count, incr,
763 * amdgpu_vm_pte_fragment - get fragment for PTEs
765 * @params: see amdgpu_vm_update_params definition
766 * @start: first PTE to handle
767 * @end: last PTE to handle
768 * @flags: hw mapping flags
769 * @frag: resulting fragment size
770 * @frag_end: end of this fragment
772 * Returns the first possible fragment for the start and end address.
774 static void amdgpu_vm_pte_fragment(struct amdgpu_vm_update_params *params,
775 uint64_t start, uint64_t end, uint64_t flags,
776 unsigned int *frag, uint64_t *frag_end)
779 * The MC L1 TLB supports variable sized pages, based on a fragment
780 * field in the PTE. When this field is set to a non-zero value, page
781 * granularity is increased from 4KB to (1 << (12 + frag)). The PTE
782 * flags are considered valid for all PTEs within the fragment range
783 * and corresponding mappings are assumed to be physically contiguous.
785 * The L1 TLB can store a single PTE for the whole fragment,
786 * significantly increasing the space available for translation
787 * caching. This leads to large improvements in throughput when the
788 * TLB is under pressure.
790 * The L2 TLB distributes small and large fragments into two
791 * asymmetric partitions. The large fragment cache is significantly
792 * larger. Thus, we try to use large fragments wherever possible.
793 * Userspace can support this by aligning virtual base address and
794 * allocation size to the fragment size.
796 * Starting with Vega10 the fragment size only controls the L1. The L2
797 * is now directly feed with small/huge/giant pages from the walker.
799 unsigned int max_frag;
801 if (params->adev->asic_type < CHIP_VEGA10)
802 max_frag = params->adev->vm_manager.fragment_size;
806 /* system pages are non continuously */
807 if (params->pages_addr) {
813 /* This intentionally wraps around if no bit is set */
814 *frag = min_t(unsigned int, ffs(start) - 1, fls64(end - start) - 1);
815 if (*frag >= max_frag) {
817 *frag_end = end & ~((1ULL << max_frag) - 1);
819 *frag_end = start + (1 << *frag);
824 * amdgpu_vm_ptes_update - make sure that page tables are valid
826 * @params: see amdgpu_vm_update_params definition
827 * @start: start of GPU address range
828 * @end: end of GPU address range
829 * @dst: destination address to map to, the next dst inside the function
830 * @flags: mapping flags
832 * Update the page tables in the range @start - @end.
835 * 0 for success, -EINVAL for failure.
837 int amdgpu_vm_ptes_update(struct amdgpu_vm_update_params *params,
838 uint64_t start, uint64_t end,
839 uint64_t dst, uint64_t flags)
841 struct amdgpu_device *adev = params->adev;
842 struct amdgpu_vm_pt_cursor cursor;
843 uint64_t frag_start = start, frag_end;
847 /* figure out the initial fragment */
848 amdgpu_vm_pte_fragment(params, frag_start, end, flags, &frag,
851 /* walk over the address space and update the PTs */
852 amdgpu_vm_pt_start(adev, params->vm, start, &cursor);
853 while (cursor.pfn < end) {
854 unsigned int shift, parent_shift, mask;
855 uint64_t incr, entry_end, pe_start;
856 struct amdgpu_bo *pt;
858 if (!params->unlocked) {
859 /* make sure that the page tables covering the
860 * address range are actually allocated
862 r = amdgpu_vm_pt_alloc(params->adev, params->vm,
863 &cursor, params->immediate);
868 shift = amdgpu_vm_pt_level_shift(adev, cursor.level);
869 parent_shift = amdgpu_vm_pt_level_shift(adev, cursor.level - 1);
870 if (params->unlocked) {
871 /* Unlocked updates are only allowed on the leaves */
872 if (amdgpu_vm_pt_descendant(adev, &cursor))
874 } else if (adev->asic_type < CHIP_VEGA10 &&
875 (flags & AMDGPU_PTE_VALID)) {
876 /* No huge page support before GMC v9 */
877 if (cursor.level != AMDGPU_VM_PTB) {
878 if (!amdgpu_vm_pt_descendant(adev, &cursor))
882 } else if (frag < shift) {
883 /* We can't use this level when the fragment size is
884 * smaller than the address shift. Go to the next
885 * child entry and try again.
887 if (amdgpu_vm_pt_descendant(adev, &cursor))
889 } else if (frag >= parent_shift) {
890 /* If the fragment size is even larger than the parent
891 * shift we should go up one level and check it again.
893 if (!amdgpu_vm_pt_ancestor(&cursor))
898 pt = cursor.entry->bo;
900 /* We need all PDs and PTs for mapping something, */
901 if (flags & AMDGPU_PTE_VALID)
904 /* but unmapping something can happen at a higher
907 if (!amdgpu_vm_pt_ancestor(&cursor))
910 pt = cursor.entry->bo;
911 shift = parent_shift;
912 frag_end = max(frag_end, ALIGN(frag_start + 1,
916 /* Looks good so far, calculate parameters for the update */
917 incr = (uint64_t)AMDGPU_GPU_PAGE_SIZE << shift;
918 mask = amdgpu_vm_pt_entries_mask(adev, cursor.level);
919 pe_start = ((cursor.pfn >> shift) & mask) * 8;
920 entry_end = ((uint64_t)mask + 1) << shift;
921 entry_end += cursor.pfn & ~(entry_end - 1);
922 entry_end = min(entry_end, end);
925 struct amdgpu_vm *vm = params->vm;
926 uint64_t upd_end = min(entry_end, frag_end);
927 unsigned int nptes = (upd_end - frag_start) >> shift;
928 uint64_t upd_flags = flags | AMDGPU_PTE_FRAG(frag);
930 /* This can happen when we set higher level PDs to
931 * silent to stop fault floods.
933 nptes = max(nptes, 1u);
935 trace_amdgpu_vm_update_ptes(params, frag_start, upd_end,
936 min(nptes, 32u), dst, incr,
939 vm->immediate.fence_context);
940 amdgpu_vm_pte_update_flags(params, to_amdgpu_bo_vm(pt),
941 cursor.level, pe_start, dst,
942 nptes, incr, upd_flags);
944 pe_start += nptes * 8;
947 frag_start = upd_end;
948 if (frag_start >= frag_end) {
949 /* figure out the next fragment */
950 amdgpu_vm_pte_fragment(params, frag_start, end,
951 flags, &frag, &frag_end);
955 } while (frag_start < entry_end);
957 if (amdgpu_vm_pt_descendant(adev, &cursor)) {
958 /* Free all child entries.
959 * Update the tables with the flags and addresses and free up subsequent
960 * tables in the case of huge pages or freed up areas.
961 * This is the maximum you can free, because all other page tables are not
962 * completely covered by the range and so potentially still in use.
964 while (cursor.pfn < frag_start) {
965 /* Make sure previous mapping is freed */
966 if (cursor.entry->bo) {
967 params->table_freed = true;
968 amdgpu_vm_pt_free_dfs(adev, params->vm,
971 amdgpu_vm_pt_next(adev, &cursor);
974 } else if (frag >= shift) {
975 /* or just move on to the next on the same level. */
976 amdgpu_vm_pt_next(adev, &cursor);