2 * Copyright 2008 Advanced Micro Devices, Inc.
3 * Copyright 2008 Red Hat Inc.
4 * Copyright 2009 Jerome Glisse.
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
24 * Authors: Dave Airlie
28 #include <linux/dma-fence-array.h>
29 #include <linux/interval_tree_generic.h>
30 #include <linux/idr.h>
32 #include <drm/amdgpu_drm.h>
34 #include "amdgpu_trace.h"
35 #include "amdgpu_amdkfd.h"
36 #include "amdgpu_gmc.h"
37 #include "amdgpu_xgmi.h"
42 * GPUVM is similar to the legacy gart on older asics, however
43 * rather than there being a single global gart table
44 * for the entire GPU, there are multiple VM page tables active
45 * at any given time. The VM page tables can contain a mix
46 * vram pages and system memory pages and system memory pages
47 * can be mapped as snooped (cached system pages) or unsnooped
48 * (uncached system pages).
49 * Each VM has an ID associated with it and there is a page table
50 * associated with each VMID. When execting a command buffer,
51 * the kernel tells the the ring what VMID to use for that command
52 * buffer. VMIDs are allocated dynamically as commands are submitted.
53 * The userspace drivers maintain their own address space and the kernel
54 * sets up their pages tables accordingly when they submit their
55 * command buffers and a VMID is assigned.
56 * Cayman/Trinity support up to 8 active VMs at any given time;
60 #define START(node) ((node)->start)
61 #define LAST(node) ((node)->last)
63 INTERVAL_TREE_DEFINE(struct amdgpu_bo_va_mapping, rb, uint64_t, __subtree_last,
64 START, LAST, static, amdgpu_vm_it)
70 * struct amdgpu_prt_cb - Helper to disable partial resident texture feature from a fence callback
72 struct amdgpu_prt_cb {
75 * @adev: amdgpu device
77 struct amdgpu_device *adev;
82 struct dma_fence_cb cb;
86 * amdgpu_vm_level_shift - return the addr shift for each level
88 * @adev: amdgpu_device pointer
92 * The number of bits the pfn needs to be right shifted for a level.
94 static unsigned amdgpu_vm_level_shift(struct amdgpu_device *adev,
97 unsigned shift = 0xff;
103 shift = 9 * (AMDGPU_VM_PDB0 - level) +
104 adev->vm_manager.block_size;
110 dev_err(adev->dev, "the level%d isn't supported.\n", level);
117 * amdgpu_vm_num_entries - return the number of entries in a PD/PT
119 * @adev: amdgpu_device pointer
123 * The number of entries in a page directory or page table.
125 static unsigned amdgpu_vm_num_entries(struct amdgpu_device *adev,
128 unsigned shift = amdgpu_vm_level_shift(adev,
129 adev->vm_manager.root_level);
131 if (level == adev->vm_manager.root_level)
132 /* For the root directory */
133 return round_up(adev->vm_manager.max_pfn, 1ULL << shift)
135 else if (level != AMDGPU_VM_PTB)
136 /* Everything in between */
139 /* For the page tables on the leaves */
140 return AMDGPU_VM_PTE_COUNT(adev);
144 * amdgpu_vm_num_ats_entries - return the number of ATS entries in the root PD
146 * @adev: amdgpu_device pointer
149 * The number of entries in the root page directory which needs the ATS setting.
151 static unsigned amdgpu_vm_num_ats_entries(struct amdgpu_device *adev)
155 shift = amdgpu_vm_level_shift(adev, adev->vm_manager.root_level);
156 return AMDGPU_GMC_HOLE_START >> (shift + AMDGPU_GPU_PAGE_SHIFT);
160 * amdgpu_vm_entries_mask - the mask to get the entry number of a PD/PT
162 * @adev: amdgpu_device pointer
166 * The mask to extract the entry number of a PD/PT from an address.
168 static uint32_t amdgpu_vm_entries_mask(struct amdgpu_device *adev,
171 if (level <= adev->vm_manager.root_level)
173 else if (level != AMDGPU_VM_PTB)
176 return AMDGPU_VM_PTE_COUNT(adev) - 1;
180 * amdgpu_vm_bo_size - returns the size of the BOs in bytes
182 * @adev: amdgpu_device pointer
186 * The size of the BO for a page directory or page table in bytes.
188 static unsigned amdgpu_vm_bo_size(struct amdgpu_device *adev, unsigned level)
190 return AMDGPU_GPU_PAGE_ALIGN(amdgpu_vm_num_entries(adev, level) * 8);
194 * amdgpu_vm_bo_evicted - vm_bo is evicted
196 * @vm_bo: vm_bo which is evicted
198 * State for PDs/PTs and per VM BOs which are not at the location they should
201 static void amdgpu_vm_bo_evicted(struct amdgpu_vm_bo_base *vm_bo)
203 struct amdgpu_vm *vm = vm_bo->vm;
204 struct amdgpu_bo *bo = vm_bo->bo;
207 if (bo->tbo.type == ttm_bo_type_kernel)
208 list_move(&vm_bo->vm_status, &vm->evicted);
210 list_move_tail(&vm_bo->vm_status, &vm->evicted);
214 * amdgpu_vm_bo_relocated - vm_bo is reloacted
216 * @vm_bo: vm_bo which is relocated
218 * State for PDs/PTs which needs to update their parent PD.
220 static void amdgpu_vm_bo_relocated(struct amdgpu_vm_bo_base *vm_bo)
222 list_move(&vm_bo->vm_status, &vm_bo->vm->relocated);
226 * amdgpu_vm_bo_moved - vm_bo is moved
228 * @vm_bo: vm_bo which is moved
230 * State for per VM BOs which are moved, but that change is not yet reflected
231 * in the page tables.
233 static void amdgpu_vm_bo_moved(struct amdgpu_vm_bo_base *vm_bo)
235 list_move(&vm_bo->vm_status, &vm_bo->vm->moved);
239 * amdgpu_vm_bo_idle - vm_bo is idle
241 * @vm_bo: vm_bo which is now idle
243 * State for PDs/PTs and per VM BOs which have gone through the state machine
246 static void amdgpu_vm_bo_idle(struct amdgpu_vm_bo_base *vm_bo)
248 list_move(&vm_bo->vm_status, &vm_bo->vm->idle);
249 vm_bo->moved = false;
253 * amdgpu_vm_bo_invalidated - vm_bo is invalidated
255 * @vm_bo: vm_bo which is now invalidated
257 * State for normal BOs which are invalidated and that change not yet reflected
260 static void amdgpu_vm_bo_invalidated(struct amdgpu_vm_bo_base *vm_bo)
262 spin_lock(&vm_bo->vm->invalidated_lock);
263 list_move(&vm_bo->vm_status, &vm_bo->vm->invalidated);
264 spin_unlock(&vm_bo->vm->invalidated_lock);
268 * amdgpu_vm_bo_done - vm_bo is done
270 * @vm_bo: vm_bo which is now done
272 * State for normal BOs which are invalidated and that change has been updated
275 static void amdgpu_vm_bo_done(struct amdgpu_vm_bo_base *vm_bo)
277 spin_lock(&vm_bo->vm->invalidated_lock);
278 list_del_init(&vm_bo->vm_status);
279 spin_unlock(&vm_bo->vm->invalidated_lock);
283 * amdgpu_vm_bo_base_init - Adds bo to the list of bos associated with the vm
285 * @base: base structure for tracking BO usage in a VM
286 * @vm: vm to which bo is to be added
287 * @bo: amdgpu buffer object
289 * Initialize a bo_va_base structure and add it to the appropriate lists
292 static void amdgpu_vm_bo_base_init(struct amdgpu_vm_bo_base *base,
293 struct amdgpu_vm *vm,
294 struct amdgpu_bo *bo)
299 INIT_LIST_HEAD(&base->vm_status);
303 base->next = bo->vm_bo;
306 if (bo->tbo.base.resv != vm->root.base.bo->tbo.base.resv)
309 vm->bulk_moveable = false;
310 if (bo->tbo.type == ttm_bo_type_kernel && bo->parent)
311 amdgpu_vm_bo_relocated(base);
313 amdgpu_vm_bo_idle(base);
315 if (bo->preferred_domains &
316 amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type))
320 * we checked all the prerequisites, but it looks like this per vm bo
321 * is currently evicted. add the bo to the evicted list to make sure it
322 * is validated on next vm use to avoid fault.
324 amdgpu_vm_bo_evicted(base);
328 * amdgpu_vm_pt_parent - get the parent page directory
330 * @pt: child page table
332 * Helper to get the parent entry for the child page table. NULL if we are at
333 * the root page directory.
335 static struct amdgpu_vm_pt *amdgpu_vm_pt_parent(struct amdgpu_vm_pt *pt)
337 struct amdgpu_bo *parent = pt->base.bo->parent;
342 return container_of(parent->vm_bo, struct amdgpu_vm_pt, base);
346 * amdgpu_vm_pt_cursor - state for for_each_amdgpu_vm_pt
348 struct amdgpu_vm_pt_cursor {
350 struct amdgpu_vm_pt *parent;
351 struct amdgpu_vm_pt *entry;
356 * amdgpu_vm_pt_start - start PD/PT walk
358 * @adev: amdgpu_device pointer
359 * @vm: amdgpu_vm structure
360 * @start: start address of the walk
361 * @cursor: state to initialize
363 * Initialize a amdgpu_vm_pt_cursor to start a walk.
365 static void amdgpu_vm_pt_start(struct amdgpu_device *adev,
366 struct amdgpu_vm *vm, uint64_t start,
367 struct amdgpu_vm_pt_cursor *cursor)
370 cursor->parent = NULL;
371 cursor->entry = &vm->root;
372 cursor->level = adev->vm_manager.root_level;
376 * amdgpu_vm_pt_descendant - go to child node
378 * @adev: amdgpu_device pointer
379 * @cursor: current state
381 * Walk to the child node of the current node.
383 * True if the walk was possible, false otherwise.
385 static bool amdgpu_vm_pt_descendant(struct amdgpu_device *adev,
386 struct amdgpu_vm_pt_cursor *cursor)
388 unsigned mask, shift, idx;
390 if (!cursor->entry->entries)
393 BUG_ON(!cursor->entry->base.bo);
394 mask = amdgpu_vm_entries_mask(adev, cursor->level);
395 shift = amdgpu_vm_level_shift(adev, cursor->level);
398 idx = (cursor->pfn >> shift) & mask;
399 cursor->parent = cursor->entry;
400 cursor->entry = &cursor->entry->entries[idx];
405 * amdgpu_vm_pt_sibling - go to sibling node
407 * @adev: amdgpu_device pointer
408 * @cursor: current state
410 * Walk to the sibling node of the current node.
412 * True if the walk was possible, false otherwise.
414 static bool amdgpu_vm_pt_sibling(struct amdgpu_device *adev,
415 struct amdgpu_vm_pt_cursor *cursor)
417 unsigned shift, num_entries;
419 /* Root doesn't have a sibling */
423 /* Go to our parents and see if we got a sibling */
424 shift = amdgpu_vm_level_shift(adev, cursor->level - 1);
425 num_entries = amdgpu_vm_num_entries(adev, cursor->level - 1);
427 if (cursor->entry == &cursor->parent->entries[num_entries - 1])
430 cursor->pfn += 1ULL << shift;
431 cursor->pfn &= ~((1ULL << shift) - 1);
437 * amdgpu_vm_pt_ancestor - go to parent node
439 * @cursor: current state
441 * Walk to the parent node of the current node.
443 * True if the walk was possible, false otherwise.
445 static bool amdgpu_vm_pt_ancestor(struct amdgpu_vm_pt_cursor *cursor)
451 cursor->entry = cursor->parent;
452 cursor->parent = amdgpu_vm_pt_parent(cursor->parent);
457 * amdgpu_vm_pt_next - get next PD/PT in hieratchy
459 * @adev: amdgpu_device pointer
460 * @cursor: current state
462 * Walk the PD/PT tree to the next node.
464 static void amdgpu_vm_pt_next(struct amdgpu_device *adev,
465 struct amdgpu_vm_pt_cursor *cursor)
467 /* First try a newborn child */
468 if (amdgpu_vm_pt_descendant(adev, cursor))
471 /* If that didn't worked try to find a sibling */
472 while (!amdgpu_vm_pt_sibling(adev, cursor)) {
473 /* No sibling, go to our parents and grandparents */
474 if (!amdgpu_vm_pt_ancestor(cursor)) {
482 * amdgpu_vm_pt_first_dfs - start a deep first search
484 * @adev: amdgpu_device structure
485 * @vm: amdgpu_vm structure
486 * @start: optional cursor to start with
487 * @cursor: state to initialize
489 * Starts a deep first traversal of the PD/PT tree.
491 static void amdgpu_vm_pt_first_dfs(struct amdgpu_device *adev,
492 struct amdgpu_vm *vm,
493 struct amdgpu_vm_pt_cursor *start,
494 struct amdgpu_vm_pt_cursor *cursor)
499 amdgpu_vm_pt_start(adev, vm, 0, cursor);
500 while (amdgpu_vm_pt_descendant(adev, cursor));
504 * amdgpu_vm_pt_continue_dfs - check if the deep first search should continue
506 * @start: starting point for the search
507 * @entry: current entry
510 * True when the search should continue, false otherwise.
512 static bool amdgpu_vm_pt_continue_dfs(struct amdgpu_vm_pt_cursor *start,
513 struct amdgpu_vm_pt *entry)
515 return entry && (!start || entry != start->entry);
519 * amdgpu_vm_pt_next_dfs - get the next node for a deep first search
521 * @adev: amdgpu_device structure
522 * @cursor: current state
524 * Move the cursor to the next node in a deep first search.
526 static void amdgpu_vm_pt_next_dfs(struct amdgpu_device *adev,
527 struct amdgpu_vm_pt_cursor *cursor)
533 cursor->entry = NULL;
534 else if (amdgpu_vm_pt_sibling(adev, cursor))
535 while (amdgpu_vm_pt_descendant(adev, cursor));
537 amdgpu_vm_pt_ancestor(cursor);
541 * for_each_amdgpu_vm_pt_dfs_safe - safe deep first search of all PDs/PTs
543 #define for_each_amdgpu_vm_pt_dfs_safe(adev, vm, start, cursor, entry) \
544 for (amdgpu_vm_pt_first_dfs((adev), (vm), (start), &(cursor)), \
545 (entry) = (cursor).entry, amdgpu_vm_pt_next_dfs((adev), &(cursor));\
546 amdgpu_vm_pt_continue_dfs((start), (entry)); \
547 (entry) = (cursor).entry, amdgpu_vm_pt_next_dfs((adev), &(cursor)))
550 * amdgpu_vm_get_pd_bo - add the VM PD to a validation list
552 * @vm: vm providing the BOs
553 * @validated: head of validation list
554 * @entry: entry to add
556 * Add the page directory to the list of BOs to
557 * validate for command submission.
559 void amdgpu_vm_get_pd_bo(struct amdgpu_vm *vm,
560 struct list_head *validated,
561 struct amdgpu_bo_list_entry *entry)
564 entry->tv.bo = &vm->root.base.bo->tbo;
565 /* One for the VM updates, one for TTM and one for the CS job */
566 entry->tv.num_shared = 3;
567 entry->user_pages = NULL;
568 list_add(&entry->tv.head, validated);
572 * amdgpu_vm_del_from_lru_notify - update bulk_moveable flag
574 * @bo: BO which was removed from the LRU
576 * Make sure the bulk_moveable flag is updated when a BO is removed from the
579 void amdgpu_vm_del_from_lru_notify(struct ttm_buffer_object *bo)
581 struct amdgpu_bo *abo;
582 struct amdgpu_vm_bo_base *bo_base;
584 if (!amdgpu_bo_is_amdgpu_bo(bo))
587 if (bo->mem.placement & TTM_PL_FLAG_NO_EVICT)
590 abo = ttm_to_amdgpu_bo(bo);
593 for (bo_base = abo->vm_bo; bo_base; bo_base = bo_base->next) {
594 struct amdgpu_vm *vm = bo_base->vm;
596 if (abo->tbo.base.resv == vm->root.base.bo->tbo.base.resv)
597 vm->bulk_moveable = false;
602 * amdgpu_vm_move_to_lru_tail - move all BOs to the end of LRU
604 * @adev: amdgpu device pointer
605 * @vm: vm providing the BOs
607 * Move all BOs to the end of LRU and remember their positions to put them
610 void amdgpu_vm_move_to_lru_tail(struct amdgpu_device *adev,
611 struct amdgpu_vm *vm)
613 struct amdgpu_vm_bo_base *bo_base;
615 if (vm->bulk_moveable) {
616 spin_lock(&ttm_bo_glob.lru_lock);
617 ttm_bo_bulk_move_lru_tail(&vm->lru_bulk_move);
618 spin_unlock(&ttm_bo_glob.lru_lock);
622 memset(&vm->lru_bulk_move, 0, sizeof(vm->lru_bulk_move));
624 spin_lock(&ttm_bo_glob.lru_lock);
625 list_for_each_entry(bo_base, &vm->idle, vm_status) {
626 struct amdgpu_bo *bo = bo_base->bo;
631 ttm_bo_move_to_lru_tail(&bo->tbo, &vm->lru_bulk_move);
633 ttm_bo_move_to_lru_tail(&bo->shadow->tbo,
636 spin_unlock(&ttm_bo_glob.lru_lock);
638 vm->bulk_moveable = true;
642 * amdgpu_vm_validate_pt_bos - validate the page table BOs
644 * @adev: amdgpu device pointer
645 * @vm: vm providing the BOs
646 * @validate: callback to do the validation
647 * @param: parameter for the validation callback
649 * Validate the page table BOs on command submission if neccessary.
654 int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
655 int (*validate)(void *p, struct amdgpu_bo *bo),
658 struct amdgpu_vm_bo_base *bo_base, *tmp;
661 vm->bulk_moveable &= list_empty(&vm->evicted);
663 list_for_each_entry_safe(bo_base, tmp, &vm->evicted, vm_status) {
664 struct amdgpu_bo *bo = bo_base->bo;
666 r = validate(param, bo);
670 if (bo->tbo.type != ttm_bo_type_kernel) {
671 amdgpu_vm_bo_moved(bo_base);
673 vm->update_funcs->map_table(bo);
675 amdgpu_vm_bo_relocated(bo_base);
677 amdgpu_vm_bo_idle(bo_base);
685 * amdgpu_vm_ready - check VM is ready for updates
689 * Check if all VM PDs/PTs are ready for updates
692 * True if eviction list is empty.
694 bool amdgpu_vm_ready(struct amdgpu_vm *vm)
696 return list_empty(&vm->evicted);
700 * amdgpu_vm_clear_bo - initially clear the PDs/PTs
702 * @adev: amdgpu_device pointer
703 * @vm: VM to clear BO from
705 * @direct: use a direct update
707 * Root PD needs to be reserved when calling this.
710 * 0 on success, errno otherwise.
712 static int amdgpu_vm_clear_bo(struct amdgpu_device *adev,
713 struct amdgpu_vm *vm,
714 struct amdgpu_bo *bo,
717 struct ttm_operation_ctx ctx = { true, false };
718 unsigned level = adev->vm_manager.root_level;
719 struct amdgpu_vm_update_params params;
720 struct amdgpu_bo *ancestor = bo;
721 unsigned entries, ats_entries;
725 /* Figure out our place in the hierarchy */
726 if (ancestor->parent) {
728 while (ancestor->parent->parent) {
730 ancestor = ancestor->parent;
734 entries = amdgpu_bo_size(bo) / 8;
735 if (!vm->pte_support_ats) {
738 } else if (!bo->parent) {
739 ats_entries = amdgpu_vm_num_ats_entries(adev);
740 ats_entries = min(ats_entries, entries);
741 entries -= ats_entries;
744 struct amdgpu_vm_pt *pt;
746 pt = container_of(ancestor->vm_bo, struct amdgpu_vm_pt, base);
747 ats_entries = amdgpu_vm_num_ats_entries(adev);
748 if ((pt - vm->root.entries) >= ats_entries) {
751 ats_entries = entries;
756 r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
761 r = ttm_bo_validate(&bo->shadow->tbo, &bo->shadow->placement,
767 r = vm->update_funcs->map_table(bo);
771 memset(¶ms, 0, sizeof(params));
774 params.direct = direct;
776 r = vm->update_funcs->prepare(¶ms, AMDGPU_FENCE_OWNER_KFD, NULL);
782 uint64_t value = 0, flags;
784 flags = AMDGPU_PTE_DEFAULT_ATC;
785 if (level != AMDGPU_VM_PTB) {
786 /* Handle leaf PDEs as PTEs */
787 flags |= AMDGPU_PDE_PTE;
788 amdgpu_gmc_get_vm_pde(adev, level, &value, &flags);
791 r = vm->update_funcs->update(¶ms, bo, addr, 0, ats_entries,
796 addr += ats_entries * 8;
800 uint64_t value = 0, flags = 0;
802 if (adev->asic_type >= CHIP_VEGA10) {
803 if (level != AMDGPU_VM_PTB) {
804 /* Handle leaf PDEs as PTEs */
805 flags |= AMDGPU_PDE_PTE;
806 amdgpu_gmc_get_vm_pde(adev, level,
809 /* Workaround for fault priority problem on GMC9 */
810 flags = AMDGPU_PTE_EXECUTABLE;
814 r = vm->update_funcs->update(¶ms, bo, addr, 0, entries,
820 return vm->update_funcs->commit(¶ms, NULL);
824 * amdgpu_vm_bo_param - fill in parameters for PD/PT allocation
826 * @adev: amdgpu_device pointer
828 * @level: the page table level
829 * @direct: use a direct update
830 * @bp: resulting BO allocation parameters
832 static void amdgpu_vm_bo_param(struct amdgpu_device *adev, struct amdgpu_vm *vm,
833 int level, bool direct,
834 struct amdgpu_bo_param *bp)
836 memset(bp, 0, sizeof(*bp));
838 bp->size = amdgpu_vm_bo_size(adev, level);
839 bp->byte_align = AMDGPU_GPU_PAGE_SIZE;
840 bp->domain = AMDGPU_GEM_DOMAIN_VRAM;
841 bp->domain = amdgpu_bo_get_preferred_pin_domain(adev, bp->domain);
842 bp->flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS |
843 AMDGPU_GEM_CREATE_CPU_GTT_USWC;
844 if (vm->use_cpu_for_update)
845 bp->flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
846 else if (!vm->root.base.bo || vm->root.base.bo->shadow)
847 bp->flags |= AMDGPU_GEM_CREATE_SHADOW;
848 bp->type = ttm_bo_type_kernel;
849 bp->no_wait_gpu = direct;
850 if (vm->root.base.bo)
851 bp->resv = vm->root.base.bo->tbo.base.resv;
855 * amdgpu_vm_alloc_pts - Allocate a specific page table
857 * @adev: amdgpu_device pointer
858 * @vm: VM to allocate page tables for
859 * @cursor: Which page table to allocate
860 * @direct: use a direct update
862 * Make sure a specific page table or directory is allocated.
865 * 1 if page table needed to be allocated, 0 if page table was already
866 * allocated, negative errno if an error occurred.
868 static int amdgpu_vm_alloc_pts(struct amdgpu_device *adev,
869 struct amdgpu_vm *vm,
870 struct amdgpu_vm_pt_cursor *cursor,
873 struct amdgpu_vm_pt *entry = cursor->entry;
874 struct amdgpu_bo_param bp;
875 struct amdgpu_bo *pt;
878 if (cursor->level < AMDGPU_VM_PTB && !entry->entries) {
879 unsigned num_entries;
881 num_entries = amdgpu_vm_num_entries(adev, cursor->level);
882 entry->entries = kvmalloc_array(num_entries,
883 sizeof(*entry->entries),
884 GFP_KERNEL | __GFP_ZERO);
892 amdgpu_vm_bo_param(adev, vm, cursor->level, direct, &bp);
894 r = amdgpu_bo_create(adev, &bp, &pt);
898 /* Keep a reference to the root directory to avoid
899 * freeing them up in the wrong order.
901 pt->parent = amdgpu_bo_ref(cursor->parent->base.bo);
902 amdgpu_vm_bo_base_init(&entry->base, vm, pt);
904 r = amdgpu_vm_clear_bo(adev, vm, pt, direct);
911 amdgpu_bo_unref(&pt->shadow);
912 amdgpu_bo_unref(&pt);
917 * amdgpu_vm_free_table - fre one PD/PT
919 * @entry: PDE to free
921 static void amdgpu_vm_free_table(struct amdgpu_vm_pt *entry)
923 if (entry->base.bo) {
924 entry->base.bo->vm_bo = NULL;
925 list_del(&entry->base.vm_status);
926 amdgpu_bo_unref(&entry->base.bo->shadow);
927 amdgpu_bo_unref(&entry->base.bo);
929 kvfree(entry->entries);
930 entry->entries = NULL;
934 * amdgpu_vm_free_pts - free PD/PT levels
936 * @adev: amdgpu device structure
937 * @vm: amdgpu vm structure
938 * @start: optional cursor where to start freeing PDs/PTs
940 * Free the page directory or page table level and all sub levels.
942 static void amdgpu_vm_free_pts(struct amdgpu_device *adev,
943 struct amdgpu_vm *vm,
944 struct amdgpu_vm_pt_cursor *start)
946 struct amdgpu_vm_pt_cursor cursor;
947 struct amdgpu_vm_pt *entry;
949 vm->bulk_moveable = false;
951 for_each_amdgpu_vm_pt_dfs_safe(adev, vm, start, cursor, entry)
952 amdgpu_vm_free_table(entry);
955 amdgpu_vm_free_table(start->entry);
959 * amdgpu_vm_check_compute_bug - check whether asic has compute vm bug
961 * @adev: amdgpu_device pointer
963 void amdgpu_vm_check_compute_bug(struct amdgpu_device *adev)
965 const struct amdgpu_ip_block *ip_block;
966 bool has_compute_vm_bug;
967 struct amdgpu_ring *ring;
970 has_compute_vm_bug = false;
972 ip_block = amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_GFX);
974 /* Compute has a VM bug for GFX version < 7.
975 Compute has a VM bug for GFX 8 MEC firmware version < 673.*/
976 if (ip_block->version->major <= 7)
977 has_compute_vm_bug = true;
978 else if (ip_block->version->major == 8)
979 if (adev->gfx.mec_fw_version < 673)
980 has_compute_vm_bug = true;
983 for (i = 0; i < adev->num_rings; i++) {
984 ring = adev->rings[i];
985 if (ring->funcs->type == AMDGPU_RING_TYPE_COMPUTE)
986 /* only compute rings */
987 ring->has_compute_vm_bug = has_compute_vm_bug;
989 ring->has_compute_vm_bug = false;
994 * amdgpu_vm_need_pipeline_sync - Check if pipe sync is needed for job.
996 * @ring: ring on which the job will be submitted
997 * @job: job to submit
1000 * True if sync is needed.
1002 bool amdgpu_vm_need_pipeline_sync(struct amdgpu_ring *ring,
1003 struct amdgpu_job *job)
1005 struct amdgpu_device *adev = ring->adev;
1006 unsigned vmhub = ring->funcs->vmhub;
1007 struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
1008 struct amdgpu_vmid *id;
1009 bool gds_switch_needed;
1010 bool vm_flush_needed = job->vm_needs_flush || ring->has_compute_vm_bug;
1014 id = &id_mgr->ids[job->vmid];
1015 gds_switch_needed = ring->funcs->emit_gds_switch && (
1016 id->gds_base != job->gds_base ||
1017 id->gds_size != job->gds_size ||
1018 id->gws_base != job->gws_base ||
1019 id->gws_size != job->gws_size ||
1020 id->oa_base != job->oa_base ||
1021 id->oa_size != job->oa_size);
1023 if (amdgpu_vmid_had_gpu_reset(adev, id))
1026 return vm_flush_needed || gds_switch_needed;
1030 * amdgpu_vm_flush - hardware flush the vm
1032 * @ring: ring to use for flush
1034 * @need_pipe_sync: is pipe sync needed
1036 * Emit a VM flush when it is necessary.
1039 * 0 on success, errno otherwise.
1041 int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job,
1042 bool need_pipe_sync)
1044 struct amdgpu_device *adev = ring->adev;
1045 unsigned vmhub = ring->funcs->vmhub;
1046 struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
1047 struct amdgpu_vmid *id = &id_mgr->ids[job->vmid];
1048 bool gds_switch_needed = ring->funcs->emit_gds_switch && (
1049 id->gds_base != job->gds_base ||
1050 id->gds_size != job->gds_size ||
1051 id->gws_base != job->gws_base ||
1052 id->gws_size != job->gws_size ||
1053 id->oa_base != job->oa_base ||
1054 id->oa_size != job->oa_size);
1055 bool vm_flush_needed = job->vm_needs_flush;
1056 struct dma_fence *fence = NULL;
1057 bool pasid_mapping_needed = false;
1058 unsigned patch_offset = 0;
1061 if (amdgpu_vmid_had_gpu_reset(adev, id)) {
1062 gds_switch_needed = true;
1063 vm_flush_needed = true;
1064 pasid_mapping_needed = true;
1067 mutex_lock(&id_mgr->lock);
1068 if (id->pasid != job->pasid || !id->pasid_mapping ||
1069 !dma_fence_is_signaled(id->pasid_mapping))
1070 pasid_mapping_needed = true;
1071 mutex_unlock(&id_mgr->lock);
1073 gds_switch_needed &= !!ring->funcs->emit_gds_switch;
1074 vm_flush_needed &= !!ring->funcs->emit_vm_flush &&
1075 job->vm_pd_addr != AMDGPU_BO_INVALID_OFFSET;
1076 pasid_mapping_needed &= adev->gmc.gmc_funcs->emit_pasid_mapping &&
1077 ring->funcs->emit_wreg;
1079 if (!vm_flush_needed && !gds_switch_needed && !need_pipe_sync)
1082 if (ring->funcs->init_cond_exec)
1083 patch_offset = amdgpu_ring_init_cond_exec(ring);
1086 amdgpu_ring_emit_pipeline_sync(ring);
1088 if (vm_flush_needed) {
1089 trace_amdgpu_vm_flush(ring, job->vmid, job->vm_pd_addr);
1090 amdgpu_ring_emit_vm_flush(ring, job->vmid, job->vm_pd_addr);
1093 if (pasid_mapping_needed)
1094 amdgpu_gmc_emit_pasid_mapping(ring, job->vmid, job->pasid);
1096 if (vm_flush_needed || pasid_mapping_needed) {
1097 r = amdgpu_fence_emit(ring, &fence, 0);
1102 if (vm_flush_needed) {
1103 mutex_lock(&id_mgr->lock);
1104 dma_fence_put(id->last_flush);
1105 id->last_flush = dma_fence_get(fence);
1106 id->current_gpu_reset_count =
1107 atomic_read(&adev->gpu_reset_counter);
1108 mutex_unlock(&id_mgr->lock);
1111 if (pasid_mapping_needed) {
1112 mutex_lock(&id_mgr->lock);
1113 id->pasid = job->pasid;
1114 dma_fence_put(id->pasid_mapping);
1115 id->pasid_mapping = dma_fence_get(fence);
1116 mutex_unlock(&id_mgr->lock);
1118 dma_fence_put(fence);
1120 if (ring->funcs->emit_gds_switch && gds_switch_needed) {
1121 id->gds_base = job->gds_base;
1122 id->gds_size = job->gds_size;
1123 id->gws_base = job->gws_base;
1124 id->gws_size = job->gws_size;
1125 id->oa_base = job->oa_base;
1126 id->oa_size = job->oa_size;
1127 amdgpu_ring_emit_gds_switch(ring, job->vmid, job->gds_base,
1128 job->gds_size, job->gws_base,
1129 job->gws_size, job->oa_base,
1133 if (ring->funcs->patch_cond_exec)
1134 amdgpu_ring_patch_cond_exec(ring, patch_offset);
1136 /* the double SWITCH_BUFFER here *cannot* be skipped by COND_EXEC */
1137 if (ring->funcs->emit_switch_buffer) {
1138 amdgpu_ring_emit_switch_buffer(ring);
1139 amdgpu_ring_emit_switch_buffer(ring);
1145 * amdgpu_vm_bo_find - find the bo_va for a specific vm & bo
1148 * @bo: requested buffer object
1150 * Find @bo inside the requested vm.
1151 * Search inside the @bos vm list for the requested vm
1152 * Returns the found bo_va or NULL if none is found
1154 * Object has to be reserved!
1157 * Found bo_va or NULL.
1159 struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
1160 struct amdgpu_bo *bo)
1162 struct amdgpu_vm_bo_base *base;
1164 for (base = bo->vm_bo; base; base = base->next) {
1168 return container_of(base, struct amdgpu_bo_va, base);
1174 * amdgpu_vm_map_gart - Resolve gart mapping of addr
1176 * @pages_addr: optional DMA address to use for lookup
1177 * @addr: the unmapped addr
1179 * Look up the physical address of the page that the pte resolves
1183 * The pointer for the page table entry.
1185 uint64_t amdgpu_vm_map_gart(const dma_addr_t *pages_addr, uint64_t addr)
1189 /* page table offset */
1190 result = pages_addr[addr >> PAGE_SHIFT];
1192 /* in case cpu page size != gpu page size*/
1193 result |= addr & (~PAGE_MASK);
1195 result &= 0xFFFFFFFFFFFFF000ULL;
1201 * amdgpu_vm_update_pde - update a single level in the hierarchy
1203 * @params: parameters for the update
1205 * @entry: entry to update
1207 * Makes sure the requested entry in parent is up to date.
1209 static int amdgpu_vm_update_pde(struct amdgpu_vm_update_params *params,
1210 struct amdgpu_vm *vm,
1211 struct amdgpu_vm_pt *entry)
1213 struct amdgpu_vm_pt *parent = amdgpu_vm_pt_parent(entry);
1214 struct amdgpu_bo *bo = parent->base.bo, *pbo;
1215 uint64_t pde, pt, flags;
1218 for (level = 0, pbo = bo->parent; pbo; ++level)
1221 level += params->adev->vm_manager.root_level;
1222 amdgpu_gmc_get_pde_for_bo(entry->base.bo, level, &pt, &flags);
1223 pde = (entry - parent->entries) * 8;
1224 return vm->update_funcs->update(params, bo, pde, pt, 1, 0, flags);
1228 * amdgpu_vm_invalidate_pds - mark all PDs as invalid
1230 * @adev: amdgpu_device pointer
1233 * Mark all PD level as invalid after an error.
1235 static void amdgpu_vm_invalidate_pds(struct amdgpu_device *adev,
1236 struct amdgpu_vm *vm)
1238 struct amdgpu_vm_pt_cursor cursor;
1239 struct amdgpu_vm_pt *entry;
1241 for_each_amdgpu_vm_pt_dfs_safe(adev, vm, NULL, cursor, entry)
1242 if (entry->base.bo && !entry->base.moved)
1243 amdgpu_vm_bo_relocated(&entry->base);
1247 * amdgpu_vm_update_pdes - make sure that all directories are valid
1249 * @adev: amdgpu_device pointer
1251 * @direct: submit directly to the paging queue
1253 * Makes sure all directories are up to date.
1256 * 0 for success, error for failure.
1258 int amdgpu_vm_update_pdes(struct amdgpu_device *adev,
1259 struct amdgpu_vm *vm, bool direct)
1261 struct amdgpu_vm_update_params params;
1264 if (list_empty(&vm->relocated))
1267 memset(¶ms, 0, sizeof(params));
1270 params.direct = direct;
1272 r = vm->update_funcs->prepare(¶ms, AMDGPU_FENCE_OWNER_VM, NULL);
1276 while (!list_empty(&vm->relocated)) {
1277 struct amdgpu_vm_pt *entry;
1279 entry = list_first_entry(&vm->relocated, struct amdgpu_vm_pt,
1281 amdgpu_vm_bo_idle(&entry->base);
1283 r = amdgpu_vm_update_pde(¶ms, vm, entry);
1288 r = vm->update_funcs->commit(¶ms, &vm->last_update);
1294 amdgpu_vm_invalidate_pds(adev, vm);
1299 * amdgpu_vm_update_flags - figure out flags for PTE updates
1301 * Make sure to set the right flags for the PTEs at the desired level.
1303 static void amdgpu_vm_update_flags(struct amdgpu_vm_update_params *params,
1304 struct amdgpu_bo *bo, unsigned level,
1305 uint64_t pe, uint64_t addr,
1306 unsigned count, uint32_t incr,
1310 if (level != AMDGPU_VM_PTB) {
1311 flags |= AMDGPU_PDE_PTE;
1312 amdgpu_gmc_get_vm_pde(params->adev, level, &addr, &flags);
1314 } else if (params->adev->asic_type >= CHIP_VEGA10 &&
1315 !(flags & AMDGPU_PTE_VALID) &&
1316 !(flags & AMDGPU_PTE_PRT)) {
1318 /* Workaround for fault priority problem on GMC9 */
1319 flags |= AMDGPU_PTE_EXECUTABLE;
1322 params->vm->update_funcs->update(params, bo, pe, addr, count, incr,
1327 * amdgpu_vm_fragment - get fragment for PTEs
1329 * @params: see amdgpu_vm_update_params definition
1330 * @start: first PTE to handle
1331 * @end: last PTE to handle
1332 * @flags: hw mapping flags
1333 * @frag: resulting fragment size
1334 * @frag_end: end of this fragment
1336 * Returns the first possible fragment for the start and end address.
1338 static void amdgpu_vm_fragment(struct amdgpu_vm_update_params *params,
1339 uint64_t start, uint64_t end, uint64_t flags,
1340 unsigned int *frag, uint64_t *frag_end)
1343 * The MC L1 TLB supports variable sized pages, based on a fragment
1344 * field in the PTE. When this field is set to a non-zero value, page
1345 * granularity is increased from 4KB to (1 << (12 + frag)). The PTE
1346 * flags are considered valid for all PTEs within the fragment range
1347 * and corresponding mappings are assumed to be physically contiguous.
1349 * The L1 TLB can store a single PTE for the whole fragment,
1350 * significantly increasing the space available for translation
1351 * caching. This leads to large improvements in throughput when the
1352 * TLB is under pressure.
1354 * The L2 TLB distributes small and large fragments into two
1355 * asymmetric partitions. The large fragment cache is significantly
1356 * larger. Thus, we try to use large fragments wherever possible.
1357 * Userspace can support this by aligning virtual base address and
1358 * allocation size to the fragment size.
1360 * Starting with Vega10 the fragment size only controls the L1. The L2
1361 * is now directly feed with small/huge/giant pages from the walker.
1365 if (params->adev->asic_type < CHIP_VEGA10)
1366 max_frag = params->adev->vm_manager.fragment_size;
1370 /* system pages are non continuously */
1371 if (params->pages_addr) {
1377 /* This intentionally wraps around if no bit is set */
1378 *frag = min((unsigned)ffs(start) - 1, (unsigned)fls64(end - start) - 1);
1379 if (*frag >= max_frag) {
1381 *frag_end = end & ~((1ULL << max_frag) - 1);
1383 *frag_end = start + (1 << *frag);
1388 * amdgpu_vm_update_ptes - make sure that page tables are valid
1390 * @params: see amdgpu_vm_update_params definition
1391 * @start: start of GPU address range
1392 * @end: end of GPU address range
1393 * @dst: destination address to map to, the next dst inside the function
1394 * @flags: mapping flags
1396 * Update the page tables in the range @start - @end.
1399 * 0 for success, -EINVAL for failure.
1401 static int amdgpu_vm_update_ptes(struct amdgpu_vm_update_params *params,
1402 uint64_t start, uint64_t end,
1403 uint64_t dst, uint64_t flags)
1405 struct amdgpu_device *adev = params->adev;
1406 struct amdgpu_vm_pt_cursor cursor;
1407 uint64_t frag_start = start, frag_end;
1411 /* figure out the initial fragment */
1412 amdgpu_vm_fragment(params, frag_start, end, flags, &frag, &frag_end);
1414 /* walk over the address space and update the PTs */
1415 amdgpu_vm_pt_start(adev, params->vm, start, &cursor);
1416 while (cursor.pfn < end) {
1417 unsigned shift, parent_shift, mask;
1418 uint64_t incr, entry_end, pe_start;
1419 struct amdgpu_bo *pt;
1421 /* make sure that the page tables covering the address range are
1422 * actually allocated
1424 r = amdgpu_vm_alloc_pts(params->adev, params->vm, &cursor,
1429 pt = cursor.entry->base.bo;
1431 /* The root level can't be a huge page */
1432 if (cursor.level == adev->vm_manager.root_level) {
1433 if (!amdgpu_vm_pt_descendant(adev, &cursor))
1438 shift = amdgpu_vm_level_shift(adev, cursor.level);
1439 parent_shift = amdgpu_vm_level_shift(adev, cursor.level - 1);
1440 if (adev->asic_type < CHIP_VEGA10 &&
1441 (flags & AMDGPU_PTE_VALID)) {
1442 /* No huge page support before GMC v9 */
1443 if (cursor.level != AMDGPU_VM_PTB) {
1444 if (!amdgpu_vm_pt_descendant(adev, &cursor))
1448 } else if (frag < shift) {
1449 /* We can't use this level when the fragment size is
1450 * smaller than the address shift. Go to the next
1451 * child entry and try again.
1453 if (!amdgpu_vm_pt_descendant(adev, &cursor))
1456 } else if (frag >= parent_shift &&
1457 cursor.level - 1 != adev->vm_manager.root_level) {
1458 /* If the fragment size is even larger than the parent
1459 * shift we should go up one level and check it again
1460 * unless one level up is the root level.
1462 if (!amdgpu_vm_pt_ancestor(&cursor))
1467 /* Looks good so far, calculate parameters for the update */
1468 incr = (uint64_t)AMDGPU_GPU_PAGE_SIZE << shift;
1469 mask = amdgpu_vm_entries_mask(adev, cursor.level);
1470 pe_start = ((cursor.pfn >> shift) & mask) * 8;
1471 entry_end = (uint64_t)(mask + 1) << shift;
1472 entry_end += cursor.pfn & ~(entry_end - 1);
1473 entry_end = min(entry_end, end);
1476 uint64_t upd_end = min(entry_end, frag_end);
1477 unsigned nptes = (upd_end - frag_start) >> shift;
1479 amdgpu_vm_update_flags(params, pt, cursor.level,
1480 pe_start, dst, nptes, incr,
1481 flags | AMDGPU_PTE_FRAG(frag));
1483 pe_start += nptes * 8;
1484 dst += (uint64_t)nptes * AMDGPU_GPU_PAGE_SIZE << shift;
1486 frag_start = upd_end;
1487 if (frag_start >= frag_end) {
1488 /* figure out the next fragment */
1489 amdgpu_vm_fragment(params, frag_start, end,
1490 flags, &frag, &frag_end);
1494 } while (frag_start < entry_end);
1496 if (amdgpu_vm_pt_descendant(adev, &cursor)) {
1497 /* Free all child entries.
1498 * Update the tables with the flags and addresses and free up subsequent
1499 * tables in the case of huge pages or freed up areas.
1500 * This is the maximum you can free, because all other page tables are not
1501 * completely covered by the range and so potentially still in use.
1503 while (cursor.pfn < frag_start) {
1504 amdgpu_vm_free_pts(adev, params->vm, &cursor);
1505 amdgpu_vm_pt_next(adev, &cursor);
1508 } else if (frag >= shift) {
1509 /* or just move on to the next on the same level. */
1510 amdgpu_vm_pt_next(adev, &cursor);
1518 * amdgpu_vm_bo_update_mapping - update a mapping in the vm page table
1520 * @adev: amdgpu_device pointer
1522 * @direct: direct submission in a page fault
1523 * @exclusive: fence we need to sync to
1524 * @start: start of mapped range
1525 * @last: last mapped entry
1526 * @flags: flags for the entries
1527 * @addr: addr to set the area to
1528 * @pages_addr: DMA addresses to use for mapping
1529 * @fence: optional resulting fence
1531 * Fill in the page table entries between @start and @last.
1534 * 0 for success, -EINVAL for failure.
1536 static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
1537 struct amdgpu_vm *vm, bool direct,
1538 struct dma_fence *exclusive,
1539 uint64_t start, uint64_t last,
1540 uint64_t flags, uint64_t addr,
1541 dma_addr_t *pages_addr,
1542 struct dma_fence **fence)
1544 struct amdgpu_vm_update_params params;
1545 void *owner = AMDGPU_FENCE_OWNER_VM;
1548 memset(¶ms, 0, sizeof(params));
1551 params.direct = direct;
1552 params.pages_addr = pages_addr;
1554 /* sync to everything except eviction fences on unmapping */
1555 if (!(flags & AMDGPU_PTE_VALID))
1556 owner = AMDGPU_FENCE_OWNER_KFD;
1558 r = vm->update_funcs->prepare(¶ms, owner, exclusive);
1562 r = amdgpu_vm_update_ptes(¶ms, start, last + 1, addr, flags);
1566 return vm->update_funcs->commit(¶ms, fence);
1570 * amdgpu_vm_bo_split_mapping - split a mapping into smaller chunks
1572 * @adev: amdgpu_device pointer
1573 * @exclusive: fence we need to sync to
1574 * @pages_addr: DMA addresses to use for mapping
1576 * @mapping: mapped range and flags to use for the update
1577 * @flags: HW flags for the mapping
1578 * @bo_adev: amdgpu_device pointer that bo actually been allocated
1579 * @nodes: array of drm_mm_nodes with the MC addresses
1580 * @fence: optional resulting fence
1582 * Split the mapping into smaller chunks so that each update fits
1586 * 0 for success, -EINVAL for failure.
1588 static int amdgpu_vm_bo_split_mapping(struct amdgpu_device *adev,
1589 struct dma_fence *exclusive,
1590 dma_addr_t *pages_addr,
1591 struct amdgpu_vm *vm,
1592 struct amdgpu_bo_va_mapping *mapping,
1594 struct amdgpu_device *bo_adev,
1595 struct drm_mm_node *nodes,
1596 struct dma_fence **fence)
1598 unsigned min_linear_pages = 1 << adev->vm_manager.fragment_size;
1599 uint64_t pfn, start = mapping->start;
1602 /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here
1603 * but in case of something, we filter the flags in first place
1605 if (!(mapping->flags & AMDGPU_PTE_READABLE))
1606 flags &= ~AMDGPU_PTE_READABLE;
1607 if (!(mapping->flags & AMDGPU_PTE_WRITEABLE))
1608 flags &= ~AMDGPU_PTE_WRITEABLE;
1610 /* Apply ASIC specific mapping flags */
1611 amdgpu_gmc_get_vm_pte(adev, mapping, &flags);
1613 trace_amdgpu_vm_bo_update(mapping);
1615 pfn = mapping->offset >> PAGE_SHIFT;
1617 while (pfn >= nodes->size) {
1624 dma_addr_t *dma_addr = NULL;
1625 uint64_t max_entries;
1626 uint64_t addr, last;
1629 addr = nodes->start << PAGE_SHIFT;
1630 max_entries = (nodes->size - pfn) *
1631 AMDGPU_GPU_PAGES_IN_CPU_PAGE;
1634 max_entries = S64_MAX;
1641 count < max_entries / AMDGPU_GPU_PAGES_IN_CPU_PAGE;
1643 uint64_t idx = pfn + count;
1645 if (pages_addr[idx] !=
1646 (pages_addr[idx - 1] + PAGE_SIZE))
1650 if (count < min_linear_pages) {
1651 addr = pfn << PAGE_SHIFT;
1652 dma_addr = pages_addr;
1654 addr = pages_addr[pfn];
1655 max_entries = count *
1656 AMDGPU_GPU_PAGES_IN_CPU_PAGE;
1659 } else if (flags & AMDGPU_PTE_VALID) {
1660 addr += bo_adev->vm_manager.vram_base_offset;
1661 addr += pfn << PAGE_SHIFT;
1664 last = min((uint64_t)mapping->last, start + max_entries - 1);
1665 r = amdgpu_vm_bo_update_mapping(adev, vm, false, exclusive,
1666 start, last, flags, addr,
1671 pfn += (last - start + 1) / AMDGPU_GPU_PAGES_IN_CPU_PAGE;
1672 if (nodes && nodes->size == pfn) {
1678 } while (unlikely(start != mapping->last + 1));
1684 * amdgpu_vm_bo_update - update all BO mappings in the vm page table
1686 * @adev: amdgpu_device pointer
1687 * @bo_va: requested BO and VM object
1688 * @clear: if true clear the entries
1690 * Fill in the page table entries for @bo_va.
1693 * 0 for success, -EINVAL for failure.
1695 int amdgpu_vm_bo_update(struct amdgpu_device *adev, struct amdgpu_bo_va *bo_va,
1698 struct amdgpu_bo *bo = bo_va->base.bo;
1699 struct amdgpu_vm *vm = bo_va->base.vm;
1700 struct amdgpu_bo_va_mapping *mapping;
1701 dma_addr_t *pages_addr = NULL;
1702 struct ttm_mem_reg *mem;
1703 struct drm_mm_node *nodes;
1704 struct dma_fence *exclusive, **last_update;
1706 struct amdgpu_device *bo_adev = adev;
1714 struct ttm_dma_tt *ttm;
1717 nodes = mem->mm_node;
1718 if (mem->mem_type == TTM_PL_TT) {
1719 ttm = container_of(bo->tbo.ttm, struct ttm_dma_tt, ttm);
1720 pages_addr = ttm->dma_address;
1722 exclusive = bo->tbo.moving;
1726 flags = amdgpu_ttm_tt_pte_flags(adev, bo->tbo.ttm, mem);
1727 bo_adev = amdgpu_ttm_adev(bo->tbo.bdev);
1732 if (clear || (bo && bo->tbo.base.resv == vm->root.base.bo->tbo.base.resv))
1733 last_update = &vm->last_update;
1735 last_update = &bo_va->last_pt_update;
1737 if (!clear && bo_va->base.moved) {
1738 bo_va->base.moved = false;
1739 list_splice_init(&bo_va->valids, &bo_va->invalids);
1741 } else if (bo_va->cleared != clear) {
1742 list_splice_init(&bo_va->valids, &bo_va->invalids);
1745 list_for_each_entry(mapping, &bo_va->invalids, list) {
1746 r = amdgpu_vm_bo_split_mapping(adev, exclusive, pages_addr, vm,
1747 mapping, flags, bo_adev, nodes,
1753 /* If the BO is not in its preferred location add it back to
1754 * the evicted list so that it gets validated again on the
1755 * next command submission.
1757 if (bo && bo->tbo.base.resv == vm->root.base.bo->tbo.base.resv) {
1758 uint32_t mem_type = bo->tbo.mem.mem_type;
1760 if (!(bo->preferred_domains &
1761 amdgpu_mem_type_to_domain(mem_type)))
1762 amdgpu_vm_bo_evicted(&bo_va->base);
1764 amdgpu_vm_bo_idle(&bo_va->base);
1766 amdgpu_vm_bo_done(&bo_va->base);
1769 list_splice_init(&bo_va->invalids, &bo_va->valids);
1770 bo_va->cleared = clear;
1772 if (trace_amdgpu_vm_bo_mapping_enabled()) {
1773 list_for_each_entry(mapping, &bo_va->valids, list)
1774 trace_amdgpu_vm_bo_mapping(mapping);
1781 * amdgpu_vm_update_prt_state - update the global PRT state
1783 * @adev: amdgpu_device pointer
1785 static void amdgpu_vm_update_prt_state(struct amdgpu_device *adev)
1787 unsigned long flags;
1790 spin_lock_irqsave(&adev->vm_manager.prt_lock, flags);
1791 enable = !!atomic_read(&adev->vm_manager.num_prt_users);
1792 adev->gmc.gmc_funcs->set_prt(adev, enable);
1793 spin_unlock_irqrestore(&adev->vm_manager.prt_lock, flags);
1797 * amdgpu_vm_prt_get - add a PRT user
1799 * @adev: amdgpu_device pointer
1801 static void amdgpu_vm_prt_get(struct amdgpu_device *adev)
1803 if (!adev->gmc.gmc_funcs->set_prt)
1806 if (atomic_inc_return(&adev->vm_manager.num_prt_users) == 1)
1807 amdgpu_vm_update_prt_state(adev);
1811 * amdgpu_vm_prt_put - drop a PRT user
1813 * @adev: amdgpu_device pointer
1815 static void amdgpu_vm_prt_put(struct amdgpu_device *adev)
1817 if (atomic_dec_return(&adev->vm_manager.num_prt_users) == 0)
1818 amdgpu_vm_update_prt_state(adev);
1822 * amdgpu_vm_prt_cb - callback for updating the PRT status
1824 * @fence: fence for the callback
1825 * @_cb: the callback function
1827 static void amdgpu_vm_prt_cb(struct dma_fence *fence, struct dma_fence_cb *_cb)
1829 struct amdgpu_prt_cb *cb = container_of(_cb, struct amdgpu_prt_cb, cb);
1831 amdgpu_vm_prt_put(cb->adev);
1836 * amdgpu_vm_add_prt_cb - add callback for updating the PRT status
1838 * @adev: amdgpu_device pointer
1839 * @fence: fence for the callback
1841 static void amdgpu_vm_add_prt_cb(struct amdgpu_device *adev,
1842 struct dma_fence *fence)
1844 struct amdgpu_prt_cb *cb;
1846 if (!adev->gmc.gmc_funcs->set_prt)
1849 cb = kmalloc(sizeof(struct amdgpu_prt_cb), GFP_KERNEL);
1851 /* Last resort when we are OOM */
1853 dma_fence_wait(fence, false);
1855 amdgpu_vm_prt_put(adev);
1858 if (!fence || dma_fence_add_callback(fence, &cb->cb,
1860 amdgpu_vm_prt_cb(fence, &cb->cb);
1865 * amdgpu_vm_free_mapping - free a mapping
1867 * @adev: amdgpu_device pointer
1869 * @mapping: mapping to be freed
1870 * @fence: fence of the unmap operation
1872 * Free a mapping and make sure we decrease the PRT usage count if applicable.
1874 static void amdgpu_vm_free_mapping(struct amdgpu_device *adev,
1875 struct amdgpu_vm *vm,
1876 struct amdgpu_bo_va_mapping *mapping,
1877 struct dma_fence *fence)
1879 if (mapping->flags & AMDGPU_PTE_PRT)
1880 amdgpu_vm_add_prt_cb(adev, fence);
1885 * amdgpu_vm_prt_fini - finish all prt mappings
1887 * @adev: amdgpu_device pointer
1890 * Register a cleanup callback to disable PRT support after VM dies.
1892 static void amdgpu_vm_prt_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1894 struct dma_resv *resv = vm->root.base.bo->tbo.base.resv;
1895 struct dma_fence *excl, **shared;
1896 unsigned i, shared_count;
1899 r = dma_resv_get_fences_rcu(resv, &excl,
1900 &shared_count, &shared);
1902 /* Not enough memory to grab the fence list, as last resort
1903 * block for all the fences to complete.
1905 dma_resv_wait_timeout_rcu(resv, true, false,
1906 MAX_SCHEDULE_TIMEOUT);
1910 /* Add a callback for each fence in the reservation object */
1911 amdgpu_vm_prt_get(adev);
1912 amdgpu_vm_add_prt_cb(adev, excl);
1914 for (i = 0; i < shared_count; ++i) {
1915 amdgpu_vm_prt_get(adev);
1916 amdgpu_vm_add_prt_cb(adev, shared[i]);
1923 * amdgpu_vm_clear_freed - clear freed BOs in the PT
1925 * @adev: amdgpu_device pointer
1927 * @fence: optional resulting fence (unchanged if no work needed to be done
1928 * or if an error occurred)
1930 * Make sure all freed BOs are cleared in the PT.
1931 * PTs have to be reserved and mutex must be locked!
1937 int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
1938 struct amdgpu_vm *vm,
1939 struct dma_fence **fence)
1941 struct amdgpu_bo_va_mapping *mapping;
1942 uint64_t init_pte_value = 0;
1943 struct dma_fence *f = NULL;
1946 while (!list_empty(&vm->freed)) {
1947 mapping = list_first_entry(&vm->freed,
1948 struct amdgpu_bo_va_mapping, list);
1949 list_del(&mapping->list);
1951 if (vm->pte_support_ats &&
1952 mapping->start < AMDGPU_GMC_HOLE_START)
1953 init_pte_value = AMDGPU_PTE_DEFAULT_ATC;
1955 r = amdgpu_vm_bo_update_mapping(adev, vm, false, NULL,
1956 mapping->start, mapping->last,
1957 init_pte_value, 0, NULL, &f);
1958 amdgpu_vm_free_mapping(adev, vm, mapping, f);
1966 dma_fence_put(*fence);
1977 * amdgpu_vm_handle_moved - handle moved BOs in the PT
1979 * @adev: amdgpu_device pointer
1982 * Make sure all BOs which are moved are updated in the PTs.
1987 * PTs have to be reserved!
1989 int amdgpu_vm_handle_moved(struct amdgpu_device *adev,
1990 struct amdgpu_vm *vm)
1992 struct amdgpu_bo_va *bo_va, *tmp;
1993 struct dma_resv *resv;
1997 list_for_each_entry_safe(bo_va, tmp, &vm->moved, base.vm_status) {
1998 /* Per VM BOs never need to bo cleared in the page tables */
1999 r = amdgpu_vm_bo_update(adev, bo_va, false);
2004 spin_lock(&vm->invalidated_lock);
2005 while (!list_empty(&vm->invalidated)) {
2006 bo_va = list_first_entry(&vm->invalidated, struct amdgpu_bo_va,
2008 resv = bo_va->base.bo->tbo.base.resv;
2009 spin_unlock(&vm->invalidated_lock);
2011 /* Try to reserve the BO to avoid clearing its ptes */
2012 if (!amdgpu_vm_debug && dma_resv_trylock(resv))
2014 /* Somebody else is using the BO right now */
2018 r = amdgpu_vm_bo_update(adev, bo_va, clear);
2023 dma_resv_unlock(resv);
2024 spin_lock(&vm->invalidated_lock);
2026 spin_unlock(&vm->invalidated_lock);
2032 * amdgpu_vm_bo_add - add a bo to a specific vm
2034 * @adev: amdgpu_device pointer
2036 * @bo: amdgpu buffer object
2038 * Add @bo into the requested vm.
2039 * Add @bo to the list of bos associated with the vm
2042 * Newly added bo_va or NULL for failure
2044 * Object has to be reserved!
2046 struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
2047 struct amdgpu_vm *vm,
2048 struct amdgpu_bo *bo)
2050 struct amdgpu_bo_va *bo_va;
2052 bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL);
2053 if (bo_va == NULL) {
2056 amdgpu_vm_bo_base_init(&bo_va->base, vm, bo);
2058 bo_va->ref_count = 1;
2059 INIT_LIST_HEAD(&bo_va->valids);
2060 INIT_LIST_HEAD(&bo_va->invalids);
2062 if (bo && amdgpu_xgmi_same_hive(adev, amdgpu_ttm_adev(bo->tbo.bdev)) &&
2063 (bo->preferred_domains & AMDGPU_GEM_DOMAIN_VRAM)) {
2064 bo_va->is_xgmi = true;
2065 mutex_lock(&adev->vm_manager.lock_pstate);
2066 /* Power up XGMI if it can be potentially used */
2067 if (++adev->vm_manager.xgmi_map_counter == 1)
2068 amdgpu_xgmi_set_pstate(adev, 1);
2069 mutex_unlock(&adev->vm_manager.lock_pstate);
2077 * amdgpu_vm_bo_insert_mapping - insert a new mapping
2079 * @adev: amdgpu_device pointer
2080 * @bo_va: bo_va to store the address
2081 * @mapping: the mapping to insert
2083 * Insert a new mapping into all structures.
2085 static void amdgpu_vm_bo_insert_map(struct amdgpu_device *adev,
2086 struct amdgpu_bo_va *bo_va,
2087 struct amdgpu_bo_va_mapping *mapping)
2089 struct amdgpu_vm *vm = bo_va->base.vm;
2090 struct amdgpu_bo *bo = bo_va->base.bo;
2092 mapping->bo_va = bo_va;
2093 list_add(&mapping->list, &bo_va->invalids);
2094 amdgpu_vm_it_insert(mapping, &vm->va);
2096 if (mapping->flags & AMDGPU_PTE_PRT)
2097 amdgpu_vm_prt_get(adev);
2099 if (bo && bo->tbo.base.resv == vm->root.base.bo->tbo.base.resv &&
2100 !bo_va->base.moved) {
2101 list_move(&bo_va->base.vm_status, &vm->moved);
2103 trace_amdgpu_vm_bo_map(bo_va, mapping);
2107 * amdgpu_vm_bo_map - map bo inside a vm
2109 * @adev: amdgpu_device pointer
2110 * @bo_va: bo_va to store the address
2111 * @saddr: where to map the BO
2112 * @offset: requested offset in the BO
2113 * @size: BO size in bytes
2114 * @flags: attributes of pages (read/write/valid/etc.)
2116 * Add a mapping of the BO at the specefied addr into the VM.
2119 * 0 for success, error for failure.
2121 * Object has to be reserved and unreserved outside!
2123 int amdgpu_vm_bo_map(struct amdgpu_device *adev,
2124 struct amdgpu_bo_va *bo_va,
2125 uint64_t saddr, uint64_t offset,
2126 uint64_t size, uint64_t flags)
2128 struct amdgpu_bo_va_mapping *mapping, *tmp;
2129 struct amdgpu_bo *bo = bo_va->base.bo;
2130 struct amdgpu_vm *vm = bo_va->base.vm;
2133 /* validate the parameters */
2134 if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
2135 size == 0 || size & AMDGPU_GPU_PAGE_MASK)
2138 /* make sure object fit at this offset */
2139 eaddr = saddr + size - 1;
2140 if (saddr >= eaddr ||
2141 (bo && offset + size > amdgpu_bo_size(bo)))
2144 saddr /= AMDGPU_GPU_PAGE_SIZE;
2145 eaddr /= AMDGPU_GPU_PAGE_SIZE;
2147 tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr);
2149 /* bo and tmp overlap, invalid addr */
2150 dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with "
2151 "0x%010Lx-0x%010Lx\n", bo, saddr, eaddr,
2152 tmp->start, tmp->last + 1);
2156 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
2160 mapping->start = saddr;
2161 mapping->last = eaddr;
2162 mapping->offset = offset;
2163 mapping->flags = flags;
2165 amdgpu_vm_bo_insert_map(adev, bo_va, mapping);
2171 * amdgpu_vm_bo_replace_map - map bo inside a vm, replacing existing mappings
2173 * @adev: amdgpu_device pointer
2174 * @bo_va: bo_va to store the address
2175 * @saddr: where to map the BO
2176 * @offset: requested offset in the BO
2177 * @size: BO size in bytes
2178 * @flags: attributes of pages (read/write/valid/etc.)
2180 * Add a mapping of the BO at the specefied addr into the VM. Replace existing
2181 * mappings as we do so.
2184 * 0 for success, error for failure.
2186 * Object has to be reserved and unreserved outside!
2188 int amdgpu_vm_bo_replace_map(struct amdgpu_device *adev,
2189 struct amdgpu_bo_va *bo_va,
2190 uint64_t saddr, uint64_t offset,
2191 uint64_t size, uint64_t flags)
2193 struct amdgpu_bo_va_mapping *mapping;
2194 struct amdgpu_bo *bo = bo_va->base.bo;
2198 /* validate the parameters */
2199 if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
2200 size == 0 || size & AMDGPU_GPU_PAGE_MASK)
2203 /* make sure object fit at this offset */
2204 eaddr = saddr + size - 1;
2205 if (saddr >= eaddr ||
2206 (bo && offset + size > amdgpu_bo_size(bo)))
2209 /* Allocate all the needed memory */
2210 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
2214 r = amdgpu_vm_bo_clear_mappings(adev, bo_va->base.vm, saddr, size);
2220 saddr /= AMDGPU_GPU_PAGE_SIZE;
2221 eaddr /= AMDGPU_GPU_PAGE_SIZE;
2223 mapping->start = saddr;
2224 mapping->last = eaddr;
2225 mapping->offset = offset;
2226 mapping->flags = flags;
2228 amdgpu_vm_bo_insert_map(adev, bo_va, mapping);
2234 * amdgpu_vm_bo_unmap - remove bo mapping from vm
2236 * @adev: amdgpu_device pointer
2237 * @bo_va: bo_va to remove the address from
2238 * @saddr: where to the BO is mapped
2240 * Remove a mapping of the BO at the specefied addr from the VM.
2243 * 0 for success, error for failure.
2245 * Object has to be reserved and unreserved outside!
2247 int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
2248 struct amdgpu_bo_va *bo_va,
2251 struct amdgpu_bo_va_mapping *mapping;
2252 struct amdgpu_vm *vm = bo_va->base.vm;
2255 saddr /= AMDGPU_GPU_PAGE_SIZE;
2257 list_for_each_entry(mapping, &bo_va->valids, list) {
2258 if (mapping->start == saddr)
2262 if (&mapping->list == &bo_va->valids) {
2265 list_for_each_entry(mapping, &bo_va->invalids, list) {
2266 if (mapping->start == saddr)
2270 if (&mapping->list == &bo_va->invalids)
2274 list_del(&mapping->list);
2275 amdgpu_vm_it_remove(mapping, &vm->va);
2276 mapping->bo_va = NULL;
2277 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
2280 list_add(&mapping->list, &vm->freed);
2282 amdgpu_vm_free_mapping(adev, vm, mapping,
2283 bo_va->last_pt_update);
2289 * amdgpu_vm_bo_clear_mappings - remove all mappings in a specific range
2291 * @adev: amdgpu_device pointer
2292 * @vm: VM structure to use
2293 * @saddr: start of the range
2294 * @size: size of the range
2296 * Remove all mappings in a range, split them as appropriate.
2299 * 0 for success, error for failure.
2301 int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev,
2302 struct amdgpu_vm *vm,
2303 uint64_t saddr, uint64_t size)
2305 struct amdgpu_bo_va_mapping *before, *after, *tmp, *next;
2309 eaddr = saddr + size - 1;
2310 saddr /= AMDGPU_GPU_PAGE_SIZE;
2311 eaddr /= AMDGPU_GPU_PAGE_SIZE;
2313 /* Allocate all the needed memory */
2314 before = kzalloc(sizeof(*before), GFP_KERNEL);
2317 INIT_LIST_HEAD(&before->list);
2319 after = kzalloc(sizeof(*after), GFP_KERNEL);
2324 INIT_LIST_HEAD(&after->list);
2326 /* Now gather all removed mappings */
2327 tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr);
2329 /* Remember mapping split at the start */
2330 if (tmp->start < saddr) {
2331 before->start = tmp->start;
2332 before->last = saddr - 1;
2333 before->offset = tmp->offset;
2334 before->flags = tmp->flags;
2335 before->bo_va = tmp->bo_va;
2336 list_add(&before->list, &tmp->bo_va->invalids);
2339 /* Remember mapping split at the end */
2340 if (tmp->last > eaddr) {
2341 after->start = eaddr + 1;
2342 after->last = tmp->last;
2343 after->offset = tmp->offset;
2344 after->offset += after->start - tmp->start;
2345 after->flags = tmp->flags;
2346 after->bo_va = tmp->bo_va;
2347 list_add(&after->list, &tmp->bo_va->invalids);
2350 list_del(&tmp->list);
2351 list_add(&tmp->list, &removed);
2353 tmp = amdgpu_vm_it_iter_next(tmp, saddr, eaddr);
2356 /* And free them up */
2357 list_for_each_entry_safe(tmp, next, &removed, list) {
2358 amdgpu_vm_it_remove(tmp, &vm->va);
2359 list_del(&tmp->list);
2361 if (tmp->start < saddr)
2363 if (tmp->last > eaddr)
2367 list_add(&tmp->list, &vm->freed);
2368 trace_amdgpu_vm_bo_unmap(NULL, tmp);
2371 /* Insert partial mapping before the range */
2372 if (!list_empty(&before->list)) {
2373 amdgpu_vm_it_insert(before, &vm->va);
2374 if (before->flags & AMDGPU_PTE_PRT)
2375 amdgpu_vm_prt_get(adev);
2380 /* Insert partial mapping after the range */
2381 if (!list_empty(&after->list)) {
2382 amdgpu_vm_it_insert(after, &vm->va);
2383 if (after->flags & AMDGPU_PTE_PRT)
2384 amdgpu_vm_prt_get(adev);
2393 * amdgpu_vm_bo_lookup_mapping - find mapping by address
2395 * @vm: the requested VM
2396 * @addr: the address
2398 * Find a mapping by it's address.
2401 * The amdgpu_bo_va_mapping matching for addr or NULL
2404 struct amdgpu_bo_va_mapping *amdgpu_vm_bo_lookup_mapping(struct amdgpu_vm *vm,
2407 return amdgpu_vm_it_iter_first(&vm->va, addr, addr);
2411 * amdgpu_vm_bo_trace_cs - trace all reserved mappings
2413 * @vm: the requested vm
2414 * @ticket: CS ticket
2416 * Trace all mappings of BOs reserved during a command submission.
2418 void amdgpu_vm_bo_trace_cs(struct amdgpu_vm *vm, struct ww_acquire_ctx *ticket)
2420 struct amdgpu_bo_va_mapping *mapping;
2422 if (!trace_amdgpu_vm_bo_cs_enabled())
2425 for (mapping = amdgpu_vm_it_iter_first(&vm->va, 0, U64_MAX); mapping;
2426 mapping = amdgpu_vm_it_iter_next(mapping, 0, U64_MAX)) {
2427 if (mapping->bo_va && mapping->bo_va->base.bo) {
2428 struct amdgpu_bo *bo;
2430 bo = mapping->bo_va->base.bo;
2431 if (dma_resv_locking_ctx(bo->tbo.base.resv) !=
2436 trace_amdgpu_vm_bo_cs(mapping);
2441 * amdgpu_vm_bo_rmv - remove a bo to a specific vm
2443 * @adev: amdgpu_device pointer
2444 * @bo_va: requested bo_va
2446 * Remove @bo_va->bo from the requested vm.
2448 * Object have to be reserved!
2450 void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
2451 struct amdgpu_bo_va *bo_va)
2453 struct amdgpu_bo_va_mapping *mapping, *next;
2454 struct amdgpu_bo *bo = bo_va->base.bo;
2455 struct amdgpu_vm *vm = bo_va->base.vm;
2456 struct amdgpu_vm_bo_base **base;
2459 if (bo->tbo.base.resv == vm->root.base.bo->tbo.base.resv)
2460 vm->bulk_moveable = false;
2462 for (base = &bo_va->base.bo->vm_bo; *base;
2463 base = &(*base)->next) {
2464 if (*base != &bo_va->base)
2467 *base = bo_va->base.next;
2472 spin_lock(&vm->invalidated_lock);
2473 list_del(&bo_va->base.vm_status);
2474 spin_unlock(&vm->invalidated_lock);
2476 list_for_each_entry_safe(mapping, next, &bo_va->valids, list) {
2477 list_del(&mapping->list);
2478 amdgpu_vm_it_remove(mapping, &vm->va);
2479 mapping->bo_va = NULL;
2480 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
2481 list_add(&mapping->list, &vm->freed);
2483 list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) {
2484 list_del(&mapping->list);
2485 amdgpu_vm_it_remove(mapping, &vm->va);
2486 amdgpu_vm_free_mapping(adev, vm, mapping,
2487 bo_va->last_pt_update);
2490 dma_fence_put(bo_va->last_pt_update);
2492 if (bo && bo_va->is_xgmi) {
2493 mutex_lock(&adev->vm_manager.lock_pstate);
2494 if (--adev->vm_manager.xgmi_map_counter == 0)
2495 amdgpu_xgmi_set_pstate(adev, 0);
2496 mutex_unlock(&adev->vm_manager.lock_pstate);
2503 * amdgpu_vm_bo_invalidate - mark the bo as invalid
2505 * @adev: amdgpu_device pointer
2506 * @bo: amdgpu buffer object
2507 * @evicted: is the BO evicted
2509 * Mark @bo as invalid.
2511 void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
2512 struct amdgpu_bo *bo, bool evicted)
2514 struct amdgpu_vm_bo_base *bo_base;
2516 /* shadow bo doesn't have bo base, its validation needs its parent */
2517 if (bo->parent && bo->parent->shadow == bo)
2520 for (bo_base = bo->vm_bo; bo_base; bo_base = bo_base->next) {
2521 struct amdgpu_vm *vm = bo_base->vm;
2523 if (evicted && bo->tbo.base.resv == vm->root.base.bo->tbo.base.resv) {
2524 amdgpu_vm_bo_evicted(bo_base);
2530 bo_base->moved = true;
2532 if (bo->tbo.type == ttm_bo_type_kernel)
2533 amdgpu_vm_bo_relocated(bo_base);
2534 else if (bo->tbo.base.resv == vm->root.base.bo->tbo.base.resv)
2535 amdgpu_vm_bo_moved(bo_base);
2537 amdgpu_vm_bo_invalidated(bo_base);
2542 * amdgpu_vm_get_block_size - calculate VM page table size as power of two
2547 * VM page table as power of two
2549 static uint32_t amdgpu_vm_get_block_size(uint64_t vm_size)
2551 /* Total bits covered by PD + PTs */
2552 unsigned bits = ilog2(vm_size) + 18;
2554 /* Make sure the PD is 4K in size up to 8GB address space.
2555 Above that split equal between PD and PTs */
2559 return ((bits + 3) / 2);
2563 * amdgpu_vm_adjust_size - adjust vm size, block size and fragment size
2565 * @adev: amdgpu_device pointer
2566 * @min_vm_size: the minimum vm size in GB if it's set auto
2567 * @fragment_size_default: Default PTE fragment size
2568 * @max_level: max VMPT level
2569 * @max_bits: max address space size in bits
2572 void amdgpu_vm_adjust_size(struct amdgpu_device *adev, uint32_t min_vm_size,
2573 uint32_t fragment_size_default, unsigned max_level,
2576 unsigned int max_size = 1 << (max_bits - 30);
2577 unsigned int vm_size;
2580 /* adjust vm size first */
2581 if (amdgpu_vm_size != -1) {
2582 vm_size = amdgpu_vm_size;
2583 if (vm_size > max_size) {
2584 dev_warn(adev->dev, "VM size (%d) too large, max is %u GB\n",
2585 amdgpu_vm_size, max_size);
2590 unsigned int phys_ram_gb;
2592 /* Optimal VM size depends on the amount of physical
2593 * RAM available. Underlying requirements and
2596 * - Need to map system memory and VRAM from all GPUs
2597 * - VRAM from other GPUs not known here
2598 * - Assume VRAM <= system memory
2599 * - On GFX8 and older, VM space can be segmented for
2601 * - Need to allow room for fragmentation, guard pages etc.
2603 * This adds up to a rough guess of system memory x3.
2604 * Round up to power of two to maximize the available
2605 * VM size with the given page table size.
2608 phys_ram_gb = ((uint64_t)si.totalram * si.mem_unit +
2609 (1 << 30) - 1) >> 30;
2610 vm_size = roundup_pow_of_two(
2611 min(max(phys_ram_gb * 3, min_vm_size), max_size));
2614 adev->vm_manager.max_pfn = (uint64_t)vm_size << 18;
2616 tmp = roundup_pow_of_two(adev->vm_manager.max_pfn);
2617 if (amdgpu_vm_block_size != -1)
2618 tmp >>= amdgpu_vm_block_size - 9;
2619 tmp = DIV_ROUND_UP(fls64(tmp) - 1, 9) - 1;
2620 adev->vm_manager.num_level = min(max_level, (unsigned)tmp);
2621 switch (adev->vm_manager.num_level) {
2623 adev->vm_manager.root_level = AMDGPU_VM_PDB2;
2626 adev->vm_manager.root_level = AMDGPU_VM_PDB1;
2629 adev->vm_manager.root_level = AMDGPU_VM_PDB0;
2632 dev_err(adev->dev, "VMPT only supports 2~4+1 levels\n");
2634 /* block size depends on vm size and hw setup*/
2635 if (amdgpu_vm_block_size != -1)
2636 adev->vm_manager.block_size =
2637 min((unsigned)amdgpu_vm_block_size, max_bits
2638 - AMDGPU_GPU_PAGE_SHIFT
2639 - 9 * adev->vm_manager.num_level);
2640 else if (adev->vm_manager.num_level > 1)
2641 adev->vm_manager.block_size = 9;
2643 adev->vm_manager.block_size = amdgpu_vm_get_block_size(tmp);
2645 if (amdgpu_vm_fragment_size == -1)
2646 adev->vm_manager.fragment_size = fragment_size_default;
2648 adev->vm_manager.fragment_size = amdgpu_vm_fragment_size;
2650 DRM_INFO("vm size is %u GB, %u levels, block size is %u-bit, fragment size is %u-bit\n",
2651 vm_size, adev->vm_manager.num_level + 1,
2652 adev->vm_manager.block_size,
2653 adev->vm_manager.fragment_size);
2657 * amdgpu_vm_wait_idle - wait for the VM to become idle
2659 * @vm: VM object to wait for
2660 * @timeout: timeout to wait for VM to become idle
2662 long amdgpu_vm_wait_idle(struct amdgpu_vm *vm, long timeout)
2664 return dma_resv_wait_timeout_rcu(vm->root.base.bo->tbo.base.resv,
2665 true, true, timeout);
2669 * amdgpu_vm_init - initialize a vm instance
2671 * @adev: amdgpu_device pointer
2673 * @vm_context: Indicates if it GFX or Compute context
2674 * @pasid: Process address space identifier
2679 * 0 for success, error for failure.
2681 int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
2682 int vm_context, unsigned int pasid)
2684 struct amdgpu_bo_param bp;
2685 struct amdgpu_bo *root;
2688 vm->va = RB_ROOT_CACHED;
2689 for (i = 0; i < AMDGPU_MAX_VMHUBS; i++)
2690 vm->reserved_vmid[i] = NULL;
2691 INIT_LIST_HEAD(&vm->evicted);
2692 INIT_LIST_HEAD(&vm->relocated);
2693 INIT_LIST_HEAD(&vm->moved);
2694 INIT_LIST_HEAD(&vm->idle);
2695 INIT_LIST_HEAD(&vm->invalidated);
2696 spin_lock_init(&vm->invalidated_lock);
2697 INIT_LIST_HEAD(&vm->freed);
2699 /* create scheduler entities for page table updates */
2700 r = drm_sched_entity_init(&vm->direct, adev->vm_manager.vm_pte_rqs,
2701 adev->vm_manager.vm_pte_num_rqs, NULL);
2705 r = drm_sched_entity_init(&vm->delayed, adev->vm_manager.vm_pte_rqs,
2706 adev->vm_manager.vm_pte_num_rqs, NULL);
2708 goto error_free_direct;
2710 vm->pte_support_ats = false;
2712 if (vm_context == AMDGPU_VM_CONTEXT_COMPUTE) {
2713 vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode &
2714 AMDGPU_VM_USE_CPU_FOR_COMPUTE);
2716 if (adev->asic_type == CHIP_RAVEN)
2717 vm->pte_support_ats = true;
2719 vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode &
2720 AMDGPU_VM_USE_CPU_FOR_GFX);
2722 DRM_DEBUG_DRIVER("VM update mode is %s\n",
2723 vm->use_cpu_for_update ? "CPU" : "SDMA");
2724 WARN_ONCE((vm->use_cpu_for_update &&
2725 !amdgpu_gmc_vram_full_visible(&adev->gmc)),
2726 "CPU update of VM recommended only for large BAR system\n");
2728 if (vm->use_cpu_for_update)
2729 vm->update_funcs = &amdgpu_vm_cpu_funcs;
2731 vm->update_funcs = &amdgpu_vm_sdma_funcs;
2732 vm->last_update = NULL;
2734 amdgpu_vm_bo_param(adev, vm, adev->vm_manager.root_level, false, &bp);
2735 if (vm_context == AMDGPU_VM_CONTEXT_COMPUTE)
2736 bp.flags &= ~AMDGPU_GEM_CREATE_SHADOW;
2737 r = amdgpu_bo_create(adev, &bp, &root);
2739 goto error_free_delayed;
2741 r = amdgpu_bo_reserve(root, true);
2743 goto error_free_root;
2745 r = dma_resv_reserve_shared(root->tbo.base.resv, 1);
2747 goto error_unreserve;
2749 amdgpu_vm_bo_base_init(&vm->root.base, vm, root);
2751 r = amdgpu_vm_clear_bo(adev, vm, root, false);
2753 goto error_unreserve;
2755 amdgpu_bo_unreserve(vm->root.base.bo);
2758 unsigned long flags;
2760 spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags);
2761 r = idr_alloc(&adev->vm_manager.pasid_idr, vm, pasid, pasid + 1,
2763 spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags);
2765 goto error_free_root;
2770 INIT_KFIFO(vm->faults);
2775 amdgpu_bo_unreserve(vm->root.base.bo);
2778 amdgpu_bo_unref(&vm->root.base.bo->shadow);
2779 amdgpu_bo_unref(&vm->root.base.bo);
2780 vm->root.base.bo = NULL;
2783 drm_sched_entity_destroy(&vm->delayed);
2786 drm_sched_entity_destroy(&vm->direct);
2792 * amdgpu_vm_check_clean_reserved - check if a VM is clean
2794 * @adev: amdgpu_device pointer
2795 * @vm: the VM to check
2797 * check all entries of the root PD, if any subsequent PDs are allocated,
2798 * it means there are page table creating and filling, and is no a clean
2802 * 0 if this VM is clean
2804 static int amdgpu_vm_check_clean_reserved(struct amdgpu_device *adev,
2805 struct amdgpu_vm *vm)
2807 enum amdgpu_vm_level root = adev->vm_manager.root_level;
2808 unsigned int entries = amdgpu_vm_num_entries(adev, root);
2811 if (!(vm->root.entries))
2814 for (i = 0; i < entries; i++) {
2815 if (vm->root.entries[i].base.bo)
2823 * amdgpu_vm_make_compute - Turn a GFX VM into a compute VM
2825 * @adev: amdgpu_device pointer
2827 * @pasid: pasid to use
2829 * This only works on GFX VMs that don't have any BOs added and no
2830 * page tables allocated yet.
2832 * Changes the following VM parameters:
2833 * - use_cpu_for_update
2834 * - pte_supports_ats
2835 * - pasid (old PASID is released, because compute manages its own PASIDs)
2837 * Reinitializes the page directory to reflect the changed ATS
2841 * 0 for success, -errno for errors.
2843 int amdgpu_vm_make_compute(struct amdgpu_device *adev, struct amdgpu_vm *vm,
2846 bool pte_support_ats = (adev->asic_type == CHIP_RAVEN);
2849 r = amdgpu_bo_reserve(vm->root.base.bo, true);
2854 r = amdgpu_vm_check_clean_reserved(adev, vm);
2859 unsigned long flags;
2861 spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags);
2862 r = idr_alloc(&adev->vm_manager.pasid_idr, vm, pasid, pasid + 1,
2864 spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags);
2871 /* Check if PD needs to be reinitialized and do it before
2872 * changing any other state, in case it fails.
2874 if (pte_support_ats != vm->pte_support_ats) {
2875 vm->pte_support_ats = pte_support_ats;
2876 r = amdgpu_vm_clear_bo(adev, vm, vm->root.base.bo, false);
2881 /* Update VM state */
2882 vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode &
2883 AMDGPU_VM_USE_CPU_FOR_COMPUTE);
2884 DRM_DEBUG_DRIVER("VM update mode is %s\n",
2885 vm->use_cpu_for_update ? "CPU" : "SDMA");
2886 WARN_ONCE((vm->use_cpu_for_update &&
2887 !amdgpu_gmc_vram_full_visible(&adev->gmc)),
2888 "CPU update of VM recommended only for large BAR system\n");
2890 if (vm->use_cpu_for_update)
2891 vm->update_funcs = &amdgpu_vm_cpu_funcs;
2893 vm->update_funcs = &amdgpu_vm_sdma_funcs;
2894 dma_fence_put(vm->last_update);
2895 vm->last_update = NULL;
2898 unsigned long flags;
2900 spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags);
2901 idr_remove(&adev->vm_manager.pasid_idr, vm->pasid);
2902 spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags);
2904 /* Free the original amdgpu allocated pasid
2905 * Will be replaced with kfd allocated pasid
2907 amdgpu_pasid_free(vm->pasid);
2911 /* Free the shadow bo for compute VM */
2912 amdgpu_bo_unref(&vm->root.base.bo->shadow);
2921 unsigned long flags;
2923 spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags);
2924 idr_remove(&adev->vm_manager.pasid_idr, pasid);
2925 spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags);
2928 amdgpu_bo_unreserve(vm->root.base.bo);
2933 * amdgpu_vm_release_compute - release a compute vm
2934 * @adev: amdgpu_device pointer
2935 * @vm: a vm turned into compute vm by calling amdgpu_vm_make_compute
2937 * This is a correspondant of amdgpu_vm_make_compute. It decouples compute
2938 * pasid from vm. Compute should stop use of vm after this call.
2940 void amdgpu_vm_release_compute(struct amdgpu_device *adev, struct amdgpu_vm *vm)
2943 unsigned long flags;
2945 spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags);
2946 idr_remove(&adev->vm_manager.pasid_idr, vm->pasid);
2947 spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags);
2953 * amdgpu_vm_fini - tear down a vm instance
2955 * @adev: amdgpu_device pointer
2959 * Unbind the VM and remove all bos from the vm bo list
2961 void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
2963 struct amdgpu_bo_va_mapping *mapping, *tmp;
2964 bool prt_fini_needed = !!adev->gmc.gmc_funcs->set_prt;
2965 struct amdgpu_bo *root;
2968 amdgpu_amdkfd_gpuvm_destroy_cb(adev, vm);
2970 root = amdgpu_bo_ref(vm->root.base.bo);
2971 amdgpu_bo_reserve(root, true);
2973 unsigned long flags;
2975 spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags);
2976 idr_remove(&adev->vm_manager.pasid_idr, vm->pasid);
2977 spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags);
2981 list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
2982 if (mapping->flags & AMDGPU_PTE_PRT && prt_fini_needed) {
2983 amdgpu_vm_prt_fini(adev, vm);
2984 prt_fini_needed = false;
2987 list_del(&mapping->list);
2988 amdgpu_vm_free_mapping(adev, vm, mapping, NULL);
2991 amdgpu_vm_free_pts(adev, vm, NULL);
2992 amdgpu_bo_unreserve(root);
2993 amdgpu_bo_unref(&root);
2994 WARN_ON(vm->root.base.bo);
2996 drm_sched_entity_destroy(&vm->direct);
2997 drm_sched_entity_destroy(&vm->delayed);
2999 if (!RB_EMPTY_ROOT(&vm->va.rb_root)) {
3000 dev_err(adev->dev, "still active bo inside vm\n");
3002 rbtree_postorder_for_each_entry_safe(mapping, tmp,
3003 &vm->va.rb_root, rb) {
3004 /* Don't remove the mapping here, we don't want to trigger a
3005 * rebalance and the tree is about to be destroyed anyway.
3007 list_del(&mapping->list);
3011 dma_fence_put(vm->last_update);
3012 for (i = 0; i < AMDGPU_MAX_VMHUBS; i++)
3013 amdgpu_vmid_free_reserved(adev, vm, i);
3017 * amdgpu_vm_manager_init - init the VM manager
3019 * @adev: amdgpu_device pointer
3021 * Initialize the VM manager structures
3023 void amdgpu_vm_manager_init(struct amdgpu_device *adev)
3027 amdgpu_vmid_mgr_init(adev);
3029 adev->vm_manager.fence_context =
3030 dma_fence_context_alloc(AMDGPU_MAX_RINGS);
3031 for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
3032 adev->vm_manager.seqno[i] = 0;
3034 spin_lock_init(&adev->vm_manager.prt_lock);
3035 atomic_set(&adev->vm_manager.num_prt_users, 0);
3037 /* If not overridden by the user, by default, only in large BAR systems
3038 * Compute VM tables will be updated by CPU
3040 #ifdef CONFIG_X86_64
3041 if (amdgpu_vm_update_mode == -1) {
3042 if (amdgpu_gmc_vram_full_visible(&adev->gmc))
3043 adev->vm_manager.vm_update_mode =
3044 AMDGPU_VM_USE_CPU_FOR_COMPUTE;
3046 adev->vm_manager.vm_update_mode = 0;
3048 adev->vm_manager.vm_update_mode = amdgpu_vm_update_mode;
3050 adev->vm_manager.vm_update_mode = 0;
3053 idr_init(&adev->vm_manager.pasid_idr);
3054 spin_lock_init(&adev->vm_manager.pasid_lock);
3056 adev->vm_manager.xgmi_map_counter = 0;
3057 mutex_init(&adev->vm_manager.lock_pstate);
3061 * amdgpu_vm_manager_fini - cleanup VM manager
3063 * @adev: amdgpu_device pointer
3065 * Cleanup the VM manager and free resources.
3067 void amdgpu_vm_manager_fini(struct amdgpu_device *adev)
3069 WARN_ON(!idr_is_empty(&adev->vm_manager.pasid_idr));
3070 idr_destroy(&adev->vm_manager.pasid_idr);
3072 amdgpu_vmid_mgr_fini(adev);
3076 * amdgpu_vm_ioctl - Manages VMID reservation for vm hubs.
3078 * @dev: drm device pointer
3079 * @data: drm_amdgpu_vm
3080 * @filp: drm file pointer
3083 * 0 for success, -errno for errors.
3085 int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
3087 union drm_amdgpu_vm *args = data;
3088 struct amdgpu_device *adev = dev->dev_private;
3089 struct amdgpu_fpriv *fpriv = filp->driver_priv;
3092 switch (args->in.op) {
3093 case AMDGPU_VM_OP_RESERVE_VMID:
3094 /* We only have requirement to reserve vmid from gfxhub */
3095 r = amdgpu_vmid_alloc_reserved(adev, &fpriv->vm,
3100 case AMDGPU_VM_OP_UNRESERVE_VMID:
3101 amdgpu_vmid_free_reserved(adev, &fpriv->vm, AMDGPU_GFXHUB_0);
3111 * amdgpu_vm_get_task_info - Extracts task info for a PASID.
3113 * @adev: drm device pointer
3114 * @pasid: PASID identifier for VM
3115 * @task_info: task_info to fill.
3117 void amdgpu_vm_get_task_info(struct amdgpu_device *adev, unsigned int pasid,
3118 struct amdgpu_task_info *task_info)
3120 struct amdgpu_vm *vm;
3121 unsigned long flags;
3123 spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags);
3125 vm = idr_find(&adev->vm_manager.pasid_idr, pasid);
3127 *task_info = vm->task_info;
3129 spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags);
3133 * amdgpu_vm_set_task_info - Sets VMs task info.
3135 * @vm: vm for which to set the info
3137 void amdgpu_vm_set_task_info(struct amdgpu_vm *vm)
3139 if (vm->task_info.pid)
3142 vm->task_info.pid = current->pid;
3143 get_task_comm(vm->task_info.task_name, current);
3145 if (current->group_leader->mm != current->mm)
3148 vm->task_info.tgid = current->group_leader->pid;
3149 get_task_comm(vm->task_info.process_name, current->group_leader);
3153 * amdgpu_vm_handle_fault - graceful handling of VM faults.
3154 * @adev: amdgpu device pointer
3155 * @pasid: PASID of the VM
3156 * @addr: Address of the fault
3158 * Try to gracefully handle a VM fault. Return true if the fault was handled and
3159 * shouldn't be reported any more.
3161 bool amdgpu_vm_handle_fault(struct amdgpu_device *adev, unsigned int pasid,
3164 struct amdgpu_bo *root;
3165 uint64_t value, flags;
3166 struct amdgpu_vm *vm;
3169 spin_lock(&adev->vm_manager.pasid_lock);
3170 vm = idr_find(&adev->vm_manager.pasid_idr, pasid);
3172 root = amdgpu_bo_ref(vm->root.base.bo);
3175 spin_unlock(&adev->vm_manager.pasid_lock);
3180 r = amdgpu_bo_reserve(root, true);
3184 /* Double check that the VM still exists */
3185 spin_lock(&adev->vm_manager.pasid_lock);
3186 vm = idr_find(&adev->vm_manager.pasid_idr, pasid);
3187 if (vm && vm->root.base.bo != root)
3189 spin_unlock(&adev->vm_manager.pasid_lock);
3193 addr /= AMDGPU_GPU_PAGE_SIZE;
3194 flags = AMDGPU_PTE_VALID | AMDGPU_PTE_SNOOPED |
3197 if (amdgpu_vm_fault_stop == AMDGPU_VM_FAULT_STOP_NEVER) {
3198 /* Redirect the access to the dummy page */
3199 value = adev->dummy_page_addr;
3200 flags |= AMDGPU_PTE_EXECUTABLE | AMDGPU_PTE_READABLE |
3201 AMDGPU_PTE_WRITEABLE;
3203 /* Let the hw retry silently on the PTE */
3207 r = amdgpu_vm_bo_update_mapping(adev, vm, true, NULL, addr, addr + 1,
3208 flags, value, NULL, NULL);
3212 r = amdgpu_vm_update_pdes(adev, vm, true);
3215 amdgpu_bo_unreserve(root);
3217 DRM_ERROR("Can't handle page fault (%ld)\n", r);
3220 amdgpu_bo_unref(&root);