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
29 #include <linux/dma-fence-array.h>
30 #include <linux/interval_tree_generic.h>
31 #include <linux/idr.h>
32 #include <linux/dma-buf.h>
34 #include <drm/amdgpu_drm.h>
35 #include <drm/drm_drv.h>
37 #include "amdgpu_trace.h"
38 #include "amdgpu_amdkfd.h"
39 #include "amdgpu_gmc.h"
40 #include "amdgpu_xgmi.h"
41 #include "amdgpu_dma_buf.h"
42 #include "amdgpu_res_cursor.h"
48 * GPUVM is similar to the legacy gart on older asics, however
49 * rather than there being a single global gart table
50 * for the entire GPU, there are multiple VM page tables active
51 * at any given time. The VM page tables can contain a mix
52 * vram pages and system memory pages and system memory pages
53 * can be mapped as snooped (cached system pages) or unsnooped
54 * (uncached system pages).
55 * Each VM has an ID associated with it and there is a page table
56 * associated with each VMID. When executing a command buffer,
57 * the kernel tells the ring what VMID to use for that command
58 * buffer. VMIDs are allocated dynamically as commands are submitted.
59 * The userspace drivers maintain their own address space and the kernel
60 * sets up their pages tables accordingly when they submit their
61 * command buffers and a VMID is assigned.
62 * Cayman/Trinity support up to 8 active VMs at any given time;
66 #define START(node) ((node)->start)
67 #define LAST(node) ((node)->last)
69 INTERVAL_TREE_DEFINE(struct amdgpu_bo_va_mapping, rb, uint64_t, __subtree_last,
70 START, LAST, static, amdgpu_vm_it)
76 * struct amdgpu_prt_cb - Helper to disable partial resident texture feature from a fence callback
78 struct amdgpu_prt_cb {
81 * @adev: amdgpu device
83 struct amdgpu_device *adev;
88 struct dma_fence_cb cb;
92 * struct amdgpu_vm_tlb_seq_cb - Helper to increment the TLB flush sequence
94 struct amdgpu_vm_tlb_seq_cb {
96 * @vm: pointer to the amdgpu_vm structure to set the fence sequence on
103 struct dma_fence_cb cb;
107 * amdgpu_vm_set_pasid - manage pasid and vm ptr mapping
109 * @adev: amdgpu_device pointer
110 * @vm: amdgpu_vm pointer
111 * @pasid: the pasid the VM is using on this GPU
113 * Set the pasid this VM is using on this GPU, can also be used to remove the
114 * pasid by passing in zero.
117 int amdgpu_vm_set_pasid(struct amdgpu_device *adev, struct amdgpu_vm *vm,
122 if (vm->pasid == pasid)
126 r = xa_err(xa_erase_irq(&adev->vm_manager.pasids, vm->pasid));
134 r = xa_err(xa_store_irq(&adev->vm_manager.pasids, pasid, vm,
147 * vm eviction_lock can be taken in MMU notifiers. Make sure no reclaim-FS
148 * happens while holding this lock anywhere to prevent deadlocks when
149 * an MMU notifier runs in reclaim-FS context.
151 static inline void amdgpu_vm_eviction_lock(struct amdgpu_vm *vm)
153 mutex_lock(&vm->eviction_lock);
154 vm->saved_flags = memalloc_noreclaim_save();
157 static inline int amdgpu_vm_eviction_trylock(struct amdgpu_vm *vm)
159 if (mutex_trylock(&vm->eviction_lock)) {
160 vm->saved_flags = memalloc_noreclaim_save();
166 static inline void amdgpu_vm_eviction_unlock(struct amdgpu_vm *vm)
168 memalloc_noreclaim_restore(vm->saved_flags);
169 mutex_unlock(&vm->eviction_lock);
173 * amdgpu_vm_bo_evicted - vm_bo is evicted
175 * @vm_bo: vm_bo which is evicted
177 * State for PDs/PTs and per VM BOs which are not at the location they should
180 static void amdgpu_vm_bo_evicted(struct amdgpu_vm_bo_base *vm_bo)
182 struct amdgpu_vm *vm = vm_bo->vm;
183 struct amdgpu_bo *bo = vm_bo->bo;
186 spin_lock(&vm_bo->vm->status_lock);
187 if (bo->tbo.type == ttm_bo_type_kernel)
188 list_move(&vm_bo->vm_status, &vm->evicted);
190 list_move_tail(&vm_bo->vm_status, &vm->evicted);
191 spin_unlock(&vm_bo->vm->status_lock);
194 * amdgpu_vm_bo_moved - vm_bo is moved
196 * @vm_bo: vm_bo which is moved
198 * State for per VM BOs which are moved, but that change is not yet reflected
199 * in the page tables.
201 static void amdgpu_vm_bo_moved(struct amdgpu_vm_bo_base *vm_bo)
203 spin_lock(&vm_bo->vm->status_lock);
204 list_move(&vm_bo->vm_status, &vm_bo->vm->moved);
205 spin_unlock(&vm_bo->vm->status_lock);
209 * amdgpu_vm_bo_idle - vm_bo is idle
211 * @vm_bo: vm_bo which is now idle
213 * State for PDs/PTs and per VM BOs which have gone through the state machine
216 static void amdgpu_vm_bo_idle(struct amdgpu_vm_bo_base *vm_bo)
218 spin_lock(&vm_bo->vm->status_lock);
219 list_move(&vm_bo->vm_status, &vm_bo->vm->idle);
220 spin_unlock(&vm_bo->vm->status_lock);
221 vm_bo->moved = false;
225 * amdgpu_vm_bo_invalidated - vm_bo is invalidated
227 * @vm_bo: vm_bo which is now invalidated
229 * State for normal BOs which are invalidated and that change not yet reflected
232 static void amdgpu_vm_bo_invalidated(struct amdgpu_vm_bo_base *vm_bo)
234 spin_lock(&vm_bo->vm->status_lock);
235 list_move(&vm_bo->vm_status, &vm_bo->vm->invalidated);
236 spin_unlock(&vm_bo->vm->status_lock);
240 * amdgpu_vm_bo_relocated - vm_bo is reloacted
242 * @vm_bo: vm_bo which is relocated
244 * State for PDs/PTs which needs to update their parent PD.
245 * For the root PD, just move to idle state.
247 static void amdgpu_vm_bo_relocated(struct amdgpu_vm_bo_base *vm_bo)
249 if (vm_bo->bo->parent) {
250 spin_lock(&vm_bo->vm->status_lock);
251 list_move(&vm_bo->vm_status, &vm_bo->vm->relocated);
252 spin_unlock(&vm_bo->vm->status_lock);
254 amdgpu_vm_bo_idle(vm_bo);
259 * amdgpu_vm_bo_done - vm_bo is done
261 * @vm_bo: vm_bo which is now done
263 * State for normal BOs which are invalidated and that change has been updated
266 static void amdgpu_vm_bo_done(struct amdgpu_vm_bo_base *vm_bo)
268 spin_lock(&vm_bo->vm->status_lock);
269 list_move(&vm_bo->vm_status, &vm_bo->vm->done);
270 spin_unlock(&vm_bo->vm->status_lock);
274 * amdgpu_vm_bo_base_init - Adds bo to the list of bos associated with the vm
276 * @base: base structure for tracking BO usage in a VM
277 * @vm: vm to which bo is to be added
278 * @bo: amdgpu buffer object
280 * Initialize a bo_va_base structure and add it to the appropriate lists
283 void amdgpu_vm_bo_base_init(struct amdgpu_vm_bo_base *base,
284 struct amdgpu_vm *vm, struct amdgpu_bo *bo)
289 INIT_LIST_HEAD(&base->vm_status);
293 base->next = bo->vm_bo;
296 if (bo->tbo.base.resv != vm->root.bo->tbo.base.resv)
299 dma_resv_assert_held(vm->root.bo->tbo.base.resv);
301 ttm_bo_set_bulk_move(&bo->tbo, &vm->lru_bulk_move);
302 if (bo->tbo.type == ttm_bo_type_kernel && bo->parent)
303 amdgpu_vm_bo_relocated(base);
305 amdgpu_vm_bo_idle(base);
307 if (bo->preferred_domains &
308 amdgpu_mem_type_to_domain(bo->tbo.resource->mem_type))
312 * we checked all the prerequisites, but it looks like this per vm bo
313 * is currently evicted. add the bo to the evicted list to make sure it
314 * is validated on next vm use to avoid fault.
316 amdgpu_vm_bo_evicted(base);
320 * amdgpu_vm_get_pd_bo - add the VM PD to a validation list
322 * @vm: vm providing the BOs
323 * @validated: head of validation list
324 * @entry: entry to add
326 * Add the page directory to the list of BOs to
327 * validate for command submission.
329 void amdgpu_vm_get_pd_bo(struct amdgpu_vm *vm,
330 struct list_head *validated,
331 struct amdgpu_bo_list_entry *entry)
334 entry->tv.bo = &vm->root.bo->tbo;
335 /* Two for VM updates, one for TTM and one for the CS job */
336 entry->tv.num_shared = 4;
337 entry->user_pages = NULL;
338 list_add(&entry->tv.head, validated);
342 * amdgpu_vm_move_to_lru_tail - move all BOs to the end of LRU
344 * @adev: amdgpu device pointer
345 * @vm: vm providing the BOs
347 * Move all BOs to the end of LRU and remember their positions to put them
350 void amdgpu_vm_move_to_lru_tail(struct amdgpu_device *adev,
351 struct amdgpu_vm *vm)
353 spin_lock(&adev->mman.bdev.lru_lock);
354 ttm_lru_bulk_move_tail(&vm->lru_bulk_move);
355 spin_unlock(&adev->mman.bdev.lru_lock);
359 * amdgpu_vm_validate_pt_bos - validate the page table BOs
361 * @adev: amdgpu device pointer
362 * @vm: vm providing the BOs
363 * @validate: callback to do the validation
364 * @param: parameter for the validation callback
366 * Validate the page table BOs on command submission if neccessary.
371 int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
372 int (*validate)(void *p, struct amdgpu_bo *bo),
375 struct amdgpu_vm_bo_base *bo_base;
376 struct amdgpu_bo *shadow;
377 struct amdgpu_bo *bo;
380 spin_lock(&vm->status_lock);
381 while (!list_empty(&vm->evicted)) {
382 bo_base = list_first_entry(&vm->evicted,
383 struct amdgpu_vm_bo_base,
385 spin_unlock(&vm->status_lock);
388 shadow = amdgpu_bo_shadowed(bo);
390 r = validate(param, bo);
394 r = validate(param, shadow);
399 if (bo->tbo.type != ttm_bo_type_kernel) {
400 amdgpu_vm_bo_moved(bo_base);
402 vm->update_funcs->map_table(to_amdgpu_bo_vm(bo));
403 amdgpu_vm_bo_relocated(bo_base);
405 spin_lock(&vm->status_lock);
407 spin_unlock(&vm->status_lock);
409 amdgpu_vm_eviction_lock(vm);
410 vm->evicting = false;
411 amdgpu_vm_eviction_unlock(vm);
417 * amdgpu_vm_ready - check VM is ready for updates
421 * Check if all VM PDs/PTs are ready for updates
424 * True if VM is not evicting.
426 bool amdgpu_vm_ready(struct amdgpu_vm *vm)
431 amdgpu_vm_eviction_lock(vm);
433 amdgpu_vm_eviction_unlock(vm);
435 spin_lock(&vm->status_lock);
436 empty = list_empty(&vm->evicted);
437 spin_unlock(&vm->status_lock);
443 * amdgpu_vm_check_compute_bug - check whether asic has compute vm bug
445 * @adev: amdgpu_device pointer
447 void amdgpu_vm_check_compute_bug(struct amdgpu_device *adev)
449 const struct amdgpu_ip_block *ip_block;
450 bool has_compute_vm_bug;
451 struct amdgpu_ring *ring;
454 has_compute_vm_bug = false;
456 ip_block = amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_GFX);
458 /* Compute has a VM bug for GFX version < 7.
459 Compute has a VM bug for GFX 8 MEC firmware version < 673.*/
460 if (ip_block->version->major <= 7)
461 has_compute_vm_bug = true;
462 else if (ip_block->version->major == 8)
463 if (adev->gfx.mec_fw_version < 673)
464 has_compute_vm_bug = true;
467 for (i = 0; i < adev->num_rings; i++) {
468 ring = adev->rings[i];
469 if (ring->funcs->type == AMDGPU_RING_TYPE_COMPUTE)
470 /* only compute rings */
471 ring->has_compute_vm_bug = has_compute_vm_bug;
473 ring->has_compute_vm_bug = false;
478 * amdgpu_vm_need_pipeline_sync - Check if pipe sync is needed for job.
480 * @ring: ring on which the job will be submitted
481 * @job: job to submit
484 * True if sync is needed.
486 bool amdgpu_vm_need_pipeline_sync(struct amdgpu_ring *ring,
487 struct amdgpu_job *job)
489 struct amdgpu_device *adev = ring->adev;
490 unsigned vmhub = ring->funcs->vmhub;
491 struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
492 struct amdgpu_vmid *id;
493 bool gds_switch_needed;
494 bool vm_flush_needed = job->vm_needs_flush || ring->has_compute_vm_bug;
498 id = &id_mgr->ids[job->vmid];
499 gds_switch_needed = ring->funcs->emit_gds_switch && (
500 id->gds_base != job->gds_base ||
501 id->gds_size != job->gds_size ||
502 id->gws_base != job->gws_base ||
503 id->gws_size != job->gws_size ||
504 id->oa_base != job->oa_base ||
505 id->oa_size != job->oa_size);
507 if (amdgpu_vmid_had_gpu_reset(adev, id))
510 return vm_flush_needed || gds_switch_needed;
514 * amdgpu_vm_flush - hardware flush the vm
516 * @ring: ring to use for flush
518 * @need_pipe_sync: is pipe sync needed
520 * Emit a VM flush when it is necessary.
523 * 0 on success, errno otherwise.
525 int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job,
528 struct amdgpu_device *adev = ring->adev;
529 unsigned vmhub = ring->funcs->vmhub;
530 struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
531 struct amdgpu_vmid *id = &id_mgr->ids[job->vmid];
532 bool gds_switch_needed = ring->funcs->emit_gds_switch && (
533 id->gds_base != job->gds_base ||
534 id->gds_size != job->gds_size ||
535 id->gws_base != job->gws_base ||
536 id->gws_size != job->gws_size ||
537 id->oa_base != job->oa_base ||
538 id->oa_size != job->oa_size);
539 bool vm_flush_needed = job->vm_needs_flush;
540 struct dma_fence *fence = NULL;
541 bool pasid_mapping_needed = false;
542 unsigned patch_offset = 0;
543 bool update_spm_vmid_needed = (job->vm && (job->vm->reserved_vmid[vmhub] != NULL));
546 if (update_spm_vmid_needed && adev->gfx.rlc.funcs->update_spm_vmid)
547 adev->gfx.rlc.funcs->update_spm_vmid(adev, job->vmid);
549 if (amdgpu_vmid_had_gpu_reset(adev, id)) {
550 gds_switch_needed = true;
551 vm_flush_needed = true;
552 pasid_mapping_needed = true;
555 mutex_lock(&id_mgr->lock);
556 if (id->pasid != job->pasid || !id->pasid_mapping ||
557 !dma_fence_is_signaled(id->pasid_mapping))
558 pasid_mapping_needed = true;
559 mutex_unlock(&id_mgr->lock);
561 gds_switch_needed &= !!ring->funcs->emit_gds_switch;
562 vm_flush_needed &= !!ring->funcs->emit_vm_flush &&
563 job->vm_pd_addr != AMDGPU_BO_INVALID_OFFSET;
564 pasid_mapping_needed &= adev->gmc.gmc_funcs->emit_pasid_mapping &&
565 ring->funcs->emit_wreg;
567 if (!vm_flush_needed && !gds_switch_needed && !need_pipe_sync)
570 if (ring->funcs->init_cond_exec)
571 patch_offset = amdgpu_ring_init_cond_exec(ring);
574 amdgpu_ring_emit_pipeline_sync(ring);
576 if (vm_flush_needed) {
577 trace_amdgpu_vm_flush(ring, job->vmid, job->vm_pd_addr);
578 amdgpu_ring_emit_vm_flush(ring, job->vmid, job->vm_pd_addr);
581 if (pasid_mapping_needed)
582 amdgpu_gmc_emit_pasid_mapping(ring, job->vmid, job->pasid);
584 if (vm_flush_needed || pasid_mapping_needed) {
585 r = amdgpu_fence_emit(ring, &fence, NULL, 0);
590 if (vm_flush_needed) {
591 mutex_lock(&id_mgr->lock);
592 dma_fence_put(id->last_flush);
593 id->last_flush = dma_fence_get(fence);
594 id->current_gpu_reset_count =
595 atomic_read(&adev->gpu_reset_counter);
596 mutex_unlock(&id_mgr->lock);
599 if (pasid_mapping_needed) {
600 mutex_lock(&id_mgr->lock);
601 id->pasid = job->pasid;
602 dma_fence_put(id->pasid_mapping);
603 id->pasid_mapping = dma_fence_get(fence);
604 mutex_unlock(&id_mgr->lock);
606 dma_fence_put(fence);
608 if (!ring->is_mes_queue && ring->funcs->emit_gds_switch &&
610 id->gds_base = job->gds_base;
611 id->gds_size = job->gds_size;
612 id->gws_base = job->gws_base;
613 id->gws_size = job->gws_size;
614 id->oa_base = job->oa_base;
615 id->oa_size = job->oa_size;
616 amdgpu_ring_emit_gds_switch(ring, job->vmid, job->gds_base,
617 job->gds_size, job->gws_base,
618 job->gws_size, job->oa_base,
622 if (ring->funcs->patch_cond_exec)
623 amdgpu_ring_patch_cond_exec(ring, patch_offset);
625 /* the double SWITCH_BUFFER here *cannot* be skipped by COND_EXEC */
626 if (ring->funcs->emit_switch_buffer) {
627 amdgpu_ring_emit_switch_buffer(ring);
628 amdgpu_ring_emit_switch_buffer(ring);
634 * amdgpu_vm_bo_find - find the bo_va for a specific vm & bo
637 * @bo: requested buffer object
639 * Find @bo inside the requested vm.
640 * Search inside the @bos vm list for the requested vm
641 * Returns the found bo_va or NULL if none is found
643 * Object has to be reserved!
646 * Found bo_va or NULL.
648 struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
649 struct amdgpu_bo *bo)
651 struct amdgpu_vm_bo_base *base;
653 for (base = bo->vm_bo; base; base = base->next) {
657 return container_of(base, struct amdgpu_bo_va, base);
663 * amdgpu_vm_map_gart - Resolve gart mapping of addr
665 * @pages_addr: optional DMA address to use for lookup
666 * @addr: the unmapped addr
668 * Look up the physical address of the page that the pte resolves
672 * The pointer for the page table entry.
674 uint64_t amdgpu_vm_map_gart(const dma_addr_t *pages_addr, uint64_t addr)
678 /* page table offset */
679 result = pages_addr[addr >> PAGE_SHIFT];
681 /* in case cpu page size != gpu page size*/
682 result |= addr & (~PAGE_MASK);
684 result &= 0xFFFFFFFFFFFFF000ULL;
690 * amdgpu_vm_update_pdes - make sure that all directories are valid
692 * @adev: amdgpu_device pointer
694 * @immediate: submit immediately to the paging queue
696 * Makes sure all directories are up to date.
699 * 0 for success, error for failure.
701 int amdgpu_vm_update_pdes(struct amdgpu_device *adev,
702 struct amdgpu_vm *vm, bool immediate)
704 struct amdgpu_vm_update_params params;
705 struct amdgpu_vm_bo_base *entry;
706 bool flush_tlb_needed = false;
707 LIST_HEAD(relocated);
710 spin_lock(&vm->status_lock);
711 list_splice_init(&vm->relocated, &relocated);
712 spin_unlock(&vm->status_lock);
714 if (list_empty(&relocated))
717 if (!drm_dev_enter(adev_to_drm(adev), &idx))
720 memset(¶ms, 0, sizeof(params));
723 params.immediate = immediate;
725 r = vm->update_funcs->prepare(¶ms, NULL, AMDGPU_SYNC_EXPLICIT);
729 list_for_each_entry(entry, &relocated, vm_status) {
730 /* vm_flush_needed after updating moved PDEs */
731 flush_tlb_needed |= entry->moved;
733 r = amdgpu_vm_pde_update(¶ms, entry);
738 r = vm->update_funcs->commit(¶ms, &vm->last_update);
742 if (flush_tlb_needed)
743 atomic64_inc(&vm->tlb_seq);
745 while (!list_empty(&relocated)) {
746 entry = list_first_entry(&relocated, struct amdgpu_vm_bo_base,
748 amdgpu_vm_bo_idle(entry);
757 * amdgpu_vm_tlb_seq_cb - make sure to increment tlb sequence
759 * @cb: the callback structure
761 * Increments the tlb sequence to make sure that future CS execute a VM flush.
763 static void amdgpu_vm_tlb_seq_cb(struct dma_fence *fence,
764 struct dma_fence_cb *cb)
766 struct amdgpu_vm_tlb_seq_cb *tlb_cb;
768 tlb_cb = container_of(cb, typeof(*tlb_cb), cb);
769 atomic64_inc(&tlb_cb->vm->tlb_seq);
774 * amdgpu_vm_update_range - update a range in the vm page table
776 * @adev: amdgpu_device pointer to use for commands
777 * @vm: the VM to update the range
778 * @immediate: immediate submission in a page fault
779 * @unlocked: unlocked invalidation during MM callback
780 * @flush_tlb: trigger tlb invalidation after update completed
781 * @resv: fences we need to sync to
782 * @start: start of mapped range
783 * @last: last mapped entry
784 * @flags: flags for the entries
785 * @offset: offset into nodes and pages_addr
786 * @vram_base: base for vram mappings
787 * @res: ttm_resource to map
788 * @pages_addr: DMA addresses to use for mapping
789 * @fence: optional resulting fence
791 * Fill in the page table entries between @start and @last.
794 * 0 for success, negative erro code for failure.
796 int amdgpu_vm_update_range(struct amdgpu_device *adev, struct amdgpu_vm *vm,
797 bool immediate, bool unlocked, bool flush_tlb,
798 struct dma_resv *resv, uint64_t start, uint64_t last,
799 uint64_t flags, uint64_t offset, uint64_t vram_base,
800 struct ttm_resource *res, dma_addr_t *pages_addr,
801 struct dma_fence **fence)
803 struct amdgpu_vm_update_params params;
804 struct amdgpu_vm_tlb_seq_cb *tlb_cb;
805 struct amdgpu_res_cursor cursor;
806 enum amdgpu_sync_mode sync_mode;
809 if (!drm_dev_enter(adev_to_drm(adev), &idx))
812 tlb_cb = kmalloc(sizeof(*tlb_cb), GFP_KERNEL);
818 /* Vega20+XGMI where PTEs get inadvertently cached in L2 texture cache,
819 * heavy-weight flush TLB unconditionally.
821 flush_tlb |= adev->gmc.xgmi.num_physical_nodes &&
822 adev->ip_versions[GC_HWIP][0] == IP_VERSION(9, 4, 0);
825 * On GFX8 and older any 8 PTE block with a valid bit set enters the TLB
827 flush_tlb |= adev->ip_versions[GC_HWIP][0] < IP_VERSION(9, 0, 0);
829 memset(¶ms, 0, sizeof(params));
832 params.immediate = immediate;
833 params.pages_addr = pages_addr;
834 params.unlocked = unlocked;
836 /* Implicitly sync to command submissions in the same VM before
837 * unmapping. Sync to moving fences before mapping.
839 if (!(flags & AMDGPU_PTE_VALID))
840 sync_mode = AMDGPU_SYNC_EQ_OWNER;
842 sync_mode = AMDGPU_SYNC_EXPLICIT;
844 amdgpu_vm_eviction_lock(vm);
850 if (!unlocked && !dma_fence_is_signaled(vm->last_unlocked)) {
851 struct dma_fence *tmp = dma_fence_get_stub();
853 amdgpu_bo_fence(vm->root.bo, vm->last_unlocked, true);
854 swap(vm->last_unlocked, tmp);
858 r = vm->update_funcs->prepare(¶ms, resv, sync_mode);
862 amdgpu_res_first(pages_addr ? NULL : res, offset,
863 (last - start + 1) * AMDGPU_GPU_PAGE_SIZE, &cursor);
864 while (cursor.remaining) {
865 uint64_t tmp, num_entries, addr;
867 num_entries = cursor.size >> AMDGPU_GPU_PAGE_SHIFT;
869 bool contiguous = true;
871 if (num_entries > AMDGPU_GPU_PAGES_IN_CPU_PAGE) {
872 uint64_t pfn = cursor.start >> PAGE_SHIFT;
875 contiguous = pages_addr[pfn + 1] ==
876 pages_addr[pfn] + PAGE_SIZE;
879 AMDGPU_GPU_PAGES_IN_CPU_PAGE;
880 for (count = 2; count < tmp; ++count) {
881 uint64_t idx = pfn + count;
883 if (contiguous != (pages_addr[idx] ==
884 pages_addr[idx - 1] + PAGE_SIZE))
887 num_entries = count *
888 AMDGPU_GPU_PAGES_IN_CPU_PAGE;
893 params.pages_addr = pages_addr;
895 addr = pages_addr[cursor.start >> PAGE_SHIFT];
896 params.pages_addr = NULL;
899 } else if (flags & (AMDGPU_PTE_VALID | AMDGPU_PTE_PRT)) {
900 addr = vram_base + cursor.start;
905 tmp = start + num_entries;
906 r = amdgpu_vm_ptes_update(¶ms, start, tmp, addr, flags);
910 amdgpu_res_next(&cursor, num_entries * AMDGPU_GPU_PAGE_SIZE);
914 r = vm->update_funcs->commit(¶ms, fence);
916 if (flush_tlb || params.table_freed) {
918 if (fence && *fence &&
919 !dma_fence_add_callback(*fence, &tlb_cb->cb,
920 amdgpu_vm_tlb_seq_cb)) {
921 dma_fence_put(vm->last_tlb_flush);
922 vm->last_tlb_flush = dma_fence_get(*fence);
924 amdgpu_vm_tlb_seq_cb(NULL, &tlb_cb->cb);
933 amdgpu_vm_eviction_unlock(vm);
938 void amdgpu_vm_get_memory(struct amdgpu_vm *vm, uint64_t *vram_mem,
939 uint64_t *gtt_mem, uint64_t *cpu_mem)
941 struct amdgpu_bo_va *bo_va, *tmp;
943 spin_lock(&vm->status_lock);
944 list_for_each_entry_safe(bo_va, tmp, &vm->idle, base.vm_status) {
947 amdgpu_bo_get_memory(bo_va->base.bo, vram_mem,
950 list_for_each_entry_safe(bo_va, tmp, &vm->evicted, base.vm_status) {
953 amdgpu_bo_get_memory(bo_va->base.bo, vram_mem,
956 list_for_each_entry_safe(bo_va, tmp, &vm->relocated, base.vm_status) {
959 amdgpu_bo_get_memory(bo_va->base.bo, vram_mem,
962 list_for_each_entry_safe(bo_va, tmp, &vm->moved, base.vm_status) {
965 amdgpu_bo_get_memory(bo_va->base.bo, vram_mem,
968 list_for_each_entry_safe(bo_va, tmp, &vm->invalidated, base.vm_status) {
971 amdgpu_bo_get_memory(bo_va->base.bo, vram_mem,
974 list_for_each_entry_safe(bo_va, tmp, &vm->done, base.vm_status) {
977 amdgpu_bo_get_memory(bo_va->base.bo, vram_mem,
980 spin_unlock(&vm->status_lock);
983 * amdgpu_vm_bo_update - update all BO mappings in the vm page table
985 * @adev: amdgpu_device pointer
986 * @bo_va: requested BO and VM object
987 * @clear: if true clear the entries
989 * Fill in the page table entries for @bo_va.
992 * 0 for success, -EINVAL for failure.
994 int amdgpu_vm_bo_update(struct amdgpu_device *adev, struct amdgpu_bo_va *bo_va,
997 struct amdgpu_bo *bo = bo_va->base.bo;
998 struct amdgpu_vm *vm = bo_va->base.vm;
999 struct amdgpu_bo_va_mapping *mapping;
1000 dma_addr_t *pages_addr = NULL;
1001 struct ttm_resource *mem;
1002 struct dma_fence **last_update;
1003 bool flush_tlb = clear;
1004 struct dma_resv *resv;
1011 resv = vm->root.bo->tbo.base.resv;
1013 struct drm_gem_object *obj = &bo->tbo.base;
1015 resv = bo->tbo.base.resv;
1016 if (obj->import_attach && bo_va->is_xgmi) {
1017 struct dma_buf *dma_buf = obj->import_attach->dmabuf;
1018 struct drm_gem_object *gobj = dma_buf->priv;
1019 struct amdgpu_bo *abo = gem_to_amdgpu_bo(gobj);
1021 if (abo->tbo.resource->mem_type == TTM_PL_VRAM)
1022 bo = gem_to_amdgpu_bo(gobj);
1024 mem = bo->tbo.resource;
1025 if (mem->mem_type == TTM_PL_TT ||
1026 mem->mem_type == AMDGPU_PL_PREEMPT)
1027 pages_addr = bo->tbo.ttm->dma_address;
1031 struct amdgpu_device *bo_adev;
1033 flags = amdgpu_ttm_tt_pte_flags(adev, bo->tbo.ttm, mem);
1035 if (amdgpu_bo_encrypted(bo))
1036 flags |= AMDGPU_PTE_TMZ;
1038 bo_adev = amdgpu_ttm_adev(bo->tbo.bdev);
1039 vram_base = bo_adev->vm_manager.vram_base_offset;
1045 if (clear || (bo && bo->tbo.base.resv ==
1046 vm->root.bo->tbo.base.resv))
1047 last_update = &vm->last_update;
1049 last_update = &bo_va->last_pt_update;
1051 if (!clear && bo_va->base.moved) {
1053 list_splice_init(&bo_va->valids, &bo_va->invalids);
1055 } else if (bo_va->cleared != clear) {
1056 list_splice_init(&bo_va->valids, &bo_va->invalids);
1059 list_for_each_entry(mapping, &bo_va->invalids, list) {
1060 uint64_t update_flags = flags;
1062 /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here
1063 * but in case of something, we filter the flags in first place
1065 if (!(mapping->flags & AMDGPU_PTE_READABLE))
1066 update_flags &= ~AMDGPU_PTE_READABLE;
1067 if (!(mapping->flags & AMDGPU_PTE_WRITEABLE))
1068 update_flags &= ~AMDGPU_PTE_WRITEABLE;
1070 /* Apply ASIC specific mapping flags */
1071 amdgpu_gmc_get_vm_pte(adev, mapping, &update_flags);
1073 trace_amdgpu_vm_bo_update(mapping);
1075 r = amdgpu_vm_update_range(adev, vm, false, false, flush_tlb,
1076 resv, mapping->start, mapping->last,
1077 update_flags, mapping->offset,
1078 vram_base, mem, pages_addr,
1084 /* If the BO is not in its preferred location add it back to
1085 * the evicted list so that it gets validated again on the
1086 * next command submission.
1088 if (bo && bo->tbo.base.resv == vm->root.bo->tbo.base.resv) {
1089 uint32_t mem_type = bo->tbo.resource->mem_type;
1091 if (!(bo->preferred_domains &
1092 amdgpu_mem_type_to_domain(mem_type)))
1093 amdgpu_vm_bo_evicted(&bo_va->base);
1095 amdgpu_vm_bo_idle(&bo_va->base);
1097 amdgpu_vm_bo_done(&bo_va->base);
1100 list_splice_init(&bo_va->invalids, &bo_va->valids);
1101 bo_va->cleared = clear;
1102 bo_va->base.moved = false;
1104 if (trace_amdgpu_vm_bo_mapping_enabled()) {
1105 list_for_each_entry(mapping, &bo_va->valids, list)
1106 trace_amdgpu_vm_bo_mapping(mapping);
1113 * amdgpu_vm_update_prt_state - update the global PRT state
1115 * @adev: amdgpu_device pointer
1117 static void amdgpu_vm_update_prt_state(struct amdgpu_device *adev)
1119 unsigned long flags;
1122 spin_lock_irqsave(&adev->vm_manager.prt_lock, flags);
1123 enable = !!atomic_read(&adev->vm_manager.num_prt_users);
1124 adev->gmc.gmc_funcs->set_prt(adev, enable);
1125 spin_unlock_irqrestore(&adev->vm_manager.prt_lock, flags);
1129 * amdgpu_vm_prt_get - add a PRT user
1131 * @adev: amdgpu_device pointer
1133 static void amdgpu_vm_prt_get(struct amdgpu_device *adev)
1135 if (!adev->gmc.gmc_funcs->set_prt)
1138 if (atomic_inc_return(&adev->vm_manager.num_prt_users) == 1)
1139 amdgpu_vm_update_prt_state(adev);
1143 * amdgpu_vm_prt_put - drop a PRT user
1145 * @adev: amdgpu_device pointer
1147 static void amdgpu_vm_prt_put(struct amdgpu_device *adev)
1149 if (atomic_dec_return(&adev->vm_manager.num_prt_users) == 0)
1150 amdgpu_vm_update_prt_state(adev);
1154 * amdgpu_vm_prt_cb - callback for updating the PRT status
1156 * @fence: fence for the callback
1157 * @_cb: the callback function
1159 static void amdgpu_vm_prt_cb(struct dma_fence *fence, struct dma_fence_cb *_cb)
1161 struct amdgpu_prt_cb *cb = container_of(_cb, struct amdgpu_prt_cb, cb);
1163 amdgpu_vm_prt_put(cb->adev);
1168 * amdgpu_vm_add_prt_cb - add callback for updating the PRT status
1170 * @adev: amdgpu_device pointer
1171 * @fence: fence for the callback
1173 static void amdgpu_vm_add_prt_cb(struct amdgpu_device *adev,
1174 struct dma_fence *fence)
1176 struct amdgpu_prt_cb *cb;
1178 if (!adev->gmc.gmc_funcs->set_prt)
1181 cb = kmalloc(sizeof(struct amdgpu_prt_cb), GFP_KERNEL);
1183 /* Last resort when we are OOM */
1185 dma_fence_wait(fence, false);
1187 amdgpu_vm_prt_put(adev);
1190 if (!fence || dma_fence_add_callback(fence, &cb->cb,
1192 amdgpu_vm_prt_cb(fence, &cb->cb);
1197 * amdgpu_vm_free_mapping - free a mapping
1199 * @adev: amdgpu_device pointer
1201 * @mapping: mapping to be freed
1202 * @fence: fence of the unmap operation
1204 * Free a mapping and make sure we decrease the PRT usage count if applicable.
1206 static void amdgpu_vm_free_mapping(struct amdgpu_device *adev,
1207 struct amdgpu_vm *vm,
1208 struct amdgpu_bo_va_mapping *mapping,
1209 struct dma_fence *fence)
1211 if (mapping->flags & AMDGPU_PTE_PRT)
1212 amdgpu_vm_add_prt_cb(adev, fence);
1217 * amdgpu_vm_prt_fini - finish all prt mappings
1219 * @adev: amdgpu_device pointer
1222 * Register a cleanup callback to disable PRT support after VM dies.
1224 static void amdgpu_vm_prt_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1226 struct dma_resv *resv = vm->root.bo->tbo.base.resv;
1227 struct dma_resv_iter cursor;
1228 struct dma_fence *fence;
1230 dma_resv_for_each_fence(&cursor, resv, DMA_RESV_USAGE_BOOKKEEP, fence) {
1231 /* Add a callback for each fence in the reservation object */
1232 amdgpu_vm_prt_get(adev);
1233 amdgpu_vm_add_prt_cb(adev, fence);
1238 * amdgpu_vm_clear_freed - clear freed BOs in the PT
1240 * @adev: amdgpu_device pointer
1242 * @fence: optional resulting fence (unchanged if no work needed to be done
1243 * or if an error occurred)
1245 * Make sure all freed BOs are cleared in the PT.
1246 * PTs have to be reserved and mutex must be locked!
1252 int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
1253 struct amdgpu_vm *vm,
1254 struct dma_fence **fence)
1256 struct dma_resv *resv = vm->root.bo->tbo.base.resv;
1257 struct amdgpu_bo_va_mapping *mapping;
1258 uint64_t init_pte_value = 0;
1259 struct dma_fence *f = NULL;
1262 while (!list_empty(&vm->freed)) {
1263 mapping = list_first_entry(&vm->freed,
1264 struct amdgpu_bo_va_mapping, list);
1265 list_del(&mapping->list);
1267 if (vm->pte_support_ats &&
1268 mapping->start < AMDGPU_GMC_HOLE_START)
1269 init_pte_value = AMDGPU_PTE_DEFAULT_ATC;
1271 r = amdgpu_vm_update_range(adev, vm, false, false, true, resv,
1272 mapping->start, mapping->last,
1273 init_pte_value, 0, 0, NULL, NULL,
1275 amdgpu_vm_free_mapping(adev, vm, mapping, f);
1283 dma_fence_put(*fence);
1294 * amdgpu_vm_handle_moved - handle moved BOs in the PT
1296 * @adev: amdgpu_device pointer
1299 * Make sure all BOs which are moved are updated in the PTs.
1304 * PTs have to be reserved!
1306 int amdgpu_vm_handle_moved(struct amdgpu_device *adev,
1307 struct amdgpu_vm *vm)
1309 struct amdgpu_bo_va *bo_va;
1310 struct dma_resv *resv;
1314 spin_lock(&vm->status_lock);
1315 while (!list_empty(&vm->moved)) {
1316 bo_va = list_first_entry(&vm->moved, struct amdgpu_bo_va,
1318 spin_unlock(&vm->status_lock);
1320 /* Per VM BOs never need to bo cleared in the page tables */
1321 r = amdgpu_vm_bo_update(adev, bo_va, false);
1324 spin_lock(&vm->status_lock);
1327 while (!list_empty(&vm->invalidated)) {
1328 bo_va = list_first_entry(&vm->invalidated, struct amdgpu_bo_va,
1330 resv = bo_va->base.bo->tbo.base.resv;
1331 spin_unlock(&vm->status_lock);
1333 /* Try to reserve the BO to avoid clearing its ptes */
1334 if (!amdgpu_vm_debug && dma_resv_trylock(resv))
1336 /* Somebody else is using the BO right now */
1340 r = amdgpu_vm_bo_update(adev, bo_va, clear);
1345 dma_resv_unlock(resv);
1346 spin_lock(&vm->status_lock);
1348 spin_unlock(&vm->status_lock);
1354 * amdgpu_vm_bo_add - add a bo to a specific vm
1356 * @adev: amdgpu_device pointer
1358 * @bo: amdgpu buffer object
1360 * Add @bo into the requested vm.
1361 * Add @bo to the list of bos associated with the vm
1364 * Newly added bo_va or NULL for failure
1366 * Object has to be reserved!
1368 struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
1369 struct amdgpu_vm *vm,
1370 struct amdgpu_bo *bo)
1372 struct amdgpu_bo_va *bo_va;
1374 bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL);
1375 if (bo_va == NULL) {
1378 amdgpu_vm_bo_base_init(&bo_va->base, vm, bo);
1380 bo_va->ref_count = 1;
1381 INIT_LIST_HEAD(&bo_va->valids);
1382 INIT_LIST_HEAD(&bo_va->invalids);
1387 dma_resv_assert_held(bo->tbo.base.resv);
1388 if (amdgpu_dmabuf_is_xgmi_accessible(adev, bo)) {
1389 bo_va->is_xgmi = true;
1390 /* Power up XGMI if it can be potentially used */
1391 amdgpu_xgmi_set_pstate(adev, AMDGPU_XGMI_PSTATE_MAX_VEGA20);
1399 * amdgpu_vm_bo_insert_map - insert a new mapping
1401 * @adev: amdgpu_device pointer
1402 * @bo_va: bo_va to store the address
1403 * @mapping: the mapping to insert
1405 * Insert a new mapping into all structures.
1407 static void amdgpu_vm_bo_insert_map(struct amdgpu_device *adev,
1408 struct amdgpu_bo_va *bo_va,
1409 struct amdgpu_bo_va_mapping *mapping)
1411 struct amdgpu_vm *vm = bo_va->base.vm;
1412 struct amdgpu_bo *bo = bo_va->base.bo;
1414 mapping->bo_va = bo_va;
1415 list_add(&mapping->list, &bo_va->invalids);
1416 amdgpu_vm_it_insert(mapping, &vm->va);
1418 if (mapping->flags & AMDGPU_PTE_PRT)
1419 amdgpu_vm_prt_get(adev);
1421 if (bo && bo->tbo.base.resv == vm->root.bo->tbo.base.resv &&
1422 !bo_va->base.moved) {
1423 amdgpu_vm_bo_moved(&bo_va->base);
1425 trace_amdgpu_vm_bo_map(bo_va, mapping);
1429 * amdgpu_vm_bo_map - map bo inside a vm
1431 * @adev: amdgpu_device pointer
1432 * @bo_va: bo_va to store the address
1433 * @saddr: where to map the BO
1434 * @offset: requested offset in the BO
1435 * @size: BO size in bytes
1436 * @flags: attributes of pages (read/write/valid/etc.)
1438 * Add a mapping of the BO at the specefied addr into the VM.
1441 * 0 for success, error for failure.
1443 * Object has to be reserved and unreserved outside!
1445 int amdgpu_vm_bo_map(struct amdgpu_device *adev,
1446 struct amdgpu_bo_va *bo_va,
1447 uint64_t saddr, uint64_t offset,
1448 uint64_t size, uint64_t flags)
1450 struct amdgpu_bo_va_mapping *mapping, *tmp;
1451 struct amdgpu_bo *bo = bo_va->base.bo;
1452 struct amdgpu_vm *vm = bo_va->base.vm;
1455 /* validate the parameters */
1456 if (saddr & ~PAGE_MASK || offset & ~PAGE_MASK ||
1457 size == 0 || size & ~PAGE_MASK)
1460 /* make sure object fit at this offset */
1461 eaddr = saddr + size - 1;
1462 if (saddr >= eaddr ||
1463 (bo && offset + size > amdgpu_bo_size(bo)) ||
1464 (eaddr >= adev->vm_manager.max_pfn << AMDGPU_GPU_PAGE_SHIFT))
1467 saddr /= AMDGPU_GPU_PAGE_SIZE;
1468 eaddr /= AMDGPU_GPU_PAGE_SIZE;
1470 tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr);
1472 /* bo and tmp overlap, invalid addr */
1473 dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with "
1474 "0x%010Lx-0x%010Lx\n", bo, saddr, eaddr,
1475 tmp->start, tmp->last + 1);
1479 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
1483 mapping->start = saddr;
1484 mapping->last = eaddr;
1485 mapping->offset = offset;
1486 mapping->flags = flags;
1488 amdgpu_vm_bo_insert_map(adev, bo_va, mapping);
1494 * amdgpu_vm_bo_replace_map - map bo inside a vm, replacing existing mappings
1496 * @adev: amdgpu_device pointer
1497 * @bo_va: bo_va to store the address
1498 * @saddr: where to map the BO
1499 * @offset: requested offset in the BO
1500 * @size: BO size in bytes
1501 * @flags: attributes of pages (read/write/valid/etc.)
1503 * Add a mapping of the BO at the specefied addr into the VM. Replace existing
1504 * mappings as we do so.
1507 * 0 for success, error for failure.
1509 * Object has to be reserved and unreserved outside!
1511 int amdgpu_vm_bo_replace_map(struct amdgpu_device *adev,
1512 struct amdgpu_bo_va *bo_va,
1513 uint64_t saddr, uint64_t offset,
1514 uint64_t size, uint64_t flags)
1516 struct amdgpu_bo_va_mapping *mapping;
1517 struct amdgpu_bo *bo = bo_va->base.bo;
1521 /* validate the parameters */
1522 if (saddr & ~PAGE_MASK || offset & ~PAGE_MASK ||
1523 size == 0 || size & ~PAGE_MASK)
1526 /* make sure object fit at this offset */
1527 eaddr = saddr + size - 1;
1528 if (saddr >= eaddr ||
1529 (bo && offset + size > amdgpu_bo_size(bo)) ||
1530 (eaddr >= adev->vm_manager.max_pfn << AMDGPU_GPU_PAGE_SHIFT))
1533 /* Allocate all the needed memory */
1534 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
1538 r = amdgpu_vm_bo_clear_mappings(adev, bo_va->base.vm, saddr, size);
1544 saddr /= AMDGPU_GPU_PAGE_SIZE;
1545 eaddr /= AMDGPU_GPU_PAGE_SIZE;
1547 mapping->start = saddr;
1548 mapping->last = eaddr;
1549 mapping->offset = offset;
1550 mapping->flags = flags;
1552 amdgpu_vm_bo_insert_map(adev, bo_va, mapping);
1558 * amdgpu_vm_bo_unmap - remove bo mapping from vm
1560 * @adev: amdgpu_device pointer
1561 * @bo_va: bo_va to remove the address from
1562 * @saddr: where to the BO is mapped
1564 * Remove a mapping of the BO at the specefied addr from the VM.
1567 * 0 for success, error for failure.
1569 * Object has to be reserved and unreserved outside!
1571 int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
1572 struct amdgpu_bo_va *bo_va,
1575 struct amdgpu_bo_va_mapping *mapping;
1576 struct amdgpu_vm *vm = bo_va->base.vm;
1579 saddr /= AMDGPU_GPU_PAGE_SIZE;
1581 list_for_each_entry(mapping, &bo_va->valids, list) {
1582 if (mapping->start == saddr)
1586 if (&mapping->list == &bo_va->valids) {
1589 list_for_each_entry(mapping, &bo_va->invalids, list) {
1590 if (mapping->start == saddr)
1594 if (&mapping->list == &bo_va->invalids)
1598 list_del(&mapping->list);
1599 amdgpu_vm_it_remove(mapping, &vm->va);
1600 mapping->bo_va = NULL;
1601 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
1604 list_add(&mapping->list, &vm->freed);
1606 amdgpu_vm_free_mapping(adev, vm, mapping,
1607 bo_va->last_pt_update);
1613 * amdgpu_vm_bo_clear_mappings - remove all mappings in a specific range
1615 * @adev: amdgpu_device pointer
1616 * @vm: VM structure to use
1617 * @saddr: start of the range
1618 * @size: size of the range
1620 * Remove all mappings in a range, split them as appropriate.
1623 * 0 for success, error for failure.
1625 int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev,
1626 struct amdgpu_vm *vm,
1627 uint64_t saddr, uint64_t size)
1629 struct amdgpu_bo_va_mapping *before, *after, *tmp, *next;
1633 eaddr = saddr + size - 1;
1634 saddr /= AMDGPU_GPU_PAGE_SIZE;
1635 eaddr /= AMDGPU_GPU_PAGE_SIZE;
1637 /* Allocate all the needed memory */
1638 before = kzalloc(sizeof(*before), GFP_KERNEL);
1641 INIT_LIST_HEAD(&before->list);
1643 after = kzalloc(sizeof(*after), GFP_KERNEL);
1648 INIT_LIST_HEAD(&after->list);
1650 /* Now gather all removed mappings */
1651 tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr);
1653 /* Remember mapping split at the start */
1654 if (tmp->start < saddr) {
1655 before->start = tmp->start;
1656 before->last = saddr - 1;
1657 before->offset = tmp->offset;
1658 before->flags = tmp->flags;
1659 before->bo_va = tmp->bo_va;
1660 list_add(&before->list, &tmp->bo_va->invalids);
1663 /* Remember mapping split at the end */
1664 if (tmp->last > eaddr) {
1665 after->start = eaddr + 1;
1666 after->last = tmp->last;
1667 after->offset = tmp->offset;
1668 after->offset += (after->start - tmp->start) << PAGE_SHIFT;
1669 after->flags = tmp->flags;
1670 after->bo_va = tmp->bo_va;
1671 list_add(&after->list, &tmp->bo_va->invalids);
1674 list_del(&tmp->list);
1675 list_add(&tmp->list, &removed);
1677 tmp = amdgpu_vm_it_iter_next(tmp, saddr, eaddr);
1680 /* And free them up */
1681 list_for_each_entry_safe(tmp, next, &removed, list) {
1682 amdgpu_vm_it_remove(tmp, &vm->va);
1683 list_del(&tmp->list);
1685 if (tmp->start < saddr)
1687 if (tmp->last > eaddr)
1691 list_add(&tmp->list, &vm->freed);
1692 trace_amdgpu_vm_bo_unmap(NULL, tmp);
1695 /* Insert partial mapping before the range */
1696 if (!list_empty(&before->list)) {
1697 amdgpu_vm_it_insert(before, &vm->va);
1698 if (before->flags & AMDGPU_PTE_PRT)
1699 amdgpu_vm_prt_get(adev);
1704 /* Insert partial mapping after the range */
1705 if (!list_empty(&after->list)) {
1706 amdgpu_vm_it_insert(after, &vm->va);
1707 if (after->flags & AMDGPU_PTE_PRT)
1708 amdgpu_vm_prt_get(adev);
1717 * amdgpu_vm_bo_lookup_mapping - find mapping by address
1719 * @vm: the requested VM
1720 * @addr: the address
1722 * Find a mapping by it's address.
1725 * The amdgpu_bo_va_mapping matching for addr or NULL
1728 struct amdgpu_bo_va_mapping *amdgpu_vm_bo_lookup_mapping(struct amdgpu_vm *vm,
1731 return amdgpu_vm_it_iter_first(&vm->va, addr, addr);
1735 * amdgpu_vm_bo_trace_cs - trace all reserved mappings
1737 * @vm: the requested vm
1738 * @ticket: CS ticket
1740 * Trace all mappings of BOs reserved during a command submission.
1742 void amdgpu_vm_bo_trace_cs(struct amdgpu_vm *vm, struct ww_acquire_ctx *ticket)
1744 struct amdgpu_bo_va_mapping *mapping;
1746 if (!trace_amdgpu_vm_bo_cs_enabled())
1749 for (mapping = amdgpu_vm_it_iter_first(&vm->va, 0, U64_MAX); mapping;
1750 mapping = amdgpu_vm_it_iter_next(mapping, 0, U64_MAX)) {
1751 if (mapping->bo_va && mapping->bo_va->base.bo) {
1752 struct amdgpu_bo *bo;
1754 bo = mapping->bo_va->base.bo;
1755 if (dma_resv_locking_ctx(bo->tbo.base.resv) !=
1760 trace_amdgpu_vm_bo_cs(mapping);
1765 * amdgpu_vm_bo_del - remove a bo from a specific vm
1767 * @adev: amdgpu_device pointer
1768 * @bo_va: requested bo_va
1770 * Remove @bo_va->bo from the requested vm.
1772 * Object have to be reserved!
1774 void amdgpu_vm_bo_del(struct amdgpu_device *adev,
1775 struct amdgpu_bo_va *bo_va)
1777 struct amdgpu_bo_va_mapping *mapping, *next;
1778 struct amdgpu_bo *bo = bo_va->base.bo;
1779 struct amdgpu_vm *vm = bo_va->base.vm;
1780 struct amdgpu_vm_bo_base **base;
1782 dma_resv_assert_held(vm->root.bo->tbo.base.resv);
1785 dma_resv_assert_held(bo->tbo.base.resv);
1786 if (bo->tbo.base.resv == vm->root.bo->tbo.base.resv)
1787 ttm_bo_set_bulk_move(&bo->tbo, NULL);
1789 for (base = &bo_va->base.bo->vm_bo; *base;
1790 base = &(*base)->next) {
1791 if (*base != &bo_va->base)
1794 *base = bo_va->base.next;
1799 spin_lock(&vm->status_lock);
1800 list_del(&bo_va->base.vm_status);
1801 spin_unlock(&vm->status_lock);
1803 list_for_each_entry_safe(mapping, next, &bo_va->valids, list) {
1804 list_del(&mapping->list);
1805 amdgpu_vm_it_remove(mapping, &vm->va);
1806 mapping->bo_va = NULL;
1807 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
1808 list_add(&mapping->list, &vm->freed);
1810 list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) {
1811 list_del(&mapping->list);
1812 amdgpu_vm_it_remove(mapping, &vm->va);
1813 amdgpu_vm_free_mapping(adev, vm, mapping,
1814 bo_va->last_pt_update);
1817 dma_fence_put(bo_va->last_pt_update);
1819 if (bo && bo_va->is_xgmi)
1820 amdgpu_xgmi_set_pstate(adev, AMDGPU_XGMI_PSTATE_MIN);
1826 * amdgpu_vm_evictable - check if we can evict a VM
1828 * @bo: A page table of the VM.
1830 * Check if it is possible to evict a VM.
1832 bool amdgpu_vm_evictable(struct amdgpu_bo *bo)
1834 struct amdgpu_vm_bo_base *bo_base = bo->vm_bo;
1836 /* Page tables of a destroyed VM can go away immediately */
1837 if (!bo_base || !bo_base->vm)
1840 /* Don't evict VM page tables while they are busy */
1841 if (!dma_resv_test_signaled(bo->tbo.base.resv, DMA_RESV_USAGE_BOOKKEEP))
1844 /* Try to block ongoing updates */
1845 if (!amdgpu_vm_eviction_trylock(bo_base->vm))
1848 /* Don't evict VM page tables while they are updated */
1849 if (!dma_fence_is_signaled(bo_base->vm->last_unlocked)) {
1850 amdgpu_vm_eviction_unlock(bo_base->vm);
1854 bo_base->vm->evicting = true;
1855 amdgpu_vm_eviction_unlock(bo_base->vm);
1860 * amdgpu_vm_bo_invalidate - mark the bo as invalid
1862 * @adev: amdgpu_device pointer
1863 * @bo: amdgpu buffer object
1864 * @evicted: is the BO evicted
1866 * Mark @bo as invalid.
1868 void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
1869 struct amdgpu_bo *bo, bool evicted)
1871 struct amdgpu_vm_bo_base *bo_base;
1873 /* shadow bo doesn't have bo base, its validation needs its parent */
1874 if (bo->parent && (amdgpu_bo_shadowed(bo->parent) == bo))
1877 for (bo_base = bo->vm_bo; bo_base; bo_base = bo_base->next) {
1878 struct amdgpu_vm *vm = bo_base->vm;
1880 if (evicted && bo->tbo.base.resv == vm->root.bo->tbo.base.resv) {
1881 amdgpu_vm_bo_evicted(bo_base);
1887 bo_base->moved = true;
1889 if (bo->tbo.type == ttm_bo_type_kernel)
1890 amdgpu_vm_bo_relocated(bo_base);
1891 else if (bo->tbo.base.resv == vm->root.bo->tbo.base.resv)
1892 amdgpu_vm_bo_moved(bo_base);
1894 amdgpu_vm_bo_invalidated(bo_base);
1899 * amdgpu_vm_get_block_size - calculate VM page table size as power of two
1904 * VM page table as power of two
1906 static uint32_t amdgpu_vm_get_block_size(uint64_t vm_size)
1908 /* Total bits covered by PD + PTs */
1909 unsigned bits = ilog2(vm_size) + 18;
1911 /* Make sure the PD is 4K in size up to 8GB address space.
1912 Above that split equal between PD and PTs */
1916 return ((bits + 3) / 2);
1920 * amdgpu_vm_adjust_size - adjust vm size, block size and fragment size
1922 * @adev: amdgpu_device pointer
1923 * @min_vm_size: the minimum vm size in GB if it's set auto
1924 * @fragment_size_default: Default PTE fragment size
1925 * @max_level: max VMPT level
1926 * @max_bits: max address space size in bits
1929 void amdgpu_vm_adjust_size(struct amdgpu_device *adev, uint32_t min_vm_size,
1930 uint32_t fragment_size_default, unsigned max_level,
1933 unsigned int max_size = 1 << (max_bits - 30);
1934 unsigned int vm_size;
1937 /* adjust vm size first */
1938 if (amdgpu_vm_size != -1) {
1939 vm_size = amdgpu_vm_size;
1940 if (vm_size > max_size) {
1941 dev_warn(adev->dev, "VM size (%d) too large, max is %u GB\n",
1942 amdgpu_vm_size, max_size);
1947 unsigned int phys_ram_gb;
1949 /* Optimal VM size depends on the amount of physical
1950 * RAM available. Underlying requirements and
1953 * - Need to map system memory and VRAM from all GPUs
1954 * - VRAM from other GPUs not known here
1955 * - Assume VRAM <= system memory
1956 * - On GFX8 and older, VM space can be segmented for
1958 * - Need to allow room for fragmentation, guard pages etc.
1960 * This adds up to a rough guess of system memory x3.
1961 * Round up to power of two to maximize the available
1962 * VM size with the given page table size.
1965 phys_ram_gb = ((uint64_t)si.totalram * si.mem_unit +
1966 (1 << 30) - 1) >> 30;
1967 vm_size = roundup_pow_of_two(
1968 min(max(phys_ram_gb * 3, min_vm_size), max_size));
1971 adev->vm_manager.max_pfn = (uint64_t)vm_size << 18;
1973 tmp = roundup_pow_of_two(adev->vm_manager.max_pfn);
1974 if (amdgpu_vm_block_size != -1)
1975 tmp >>= amdgpu_vm_block_size - 9;
1976 tmp = DIV_ROUND_UP(fls64(tmp) - 1, 9) - 1;
1977 adev->vm_manager.num_level = min(max_level, (unsigned)tmp);
1978 switch (adev->vm_manager.num_level) {
1980 adev->vm_manager.root_level = AMDGPU_VM_PDB2;
1983 adev->vm_manager.root_level = AMDGPU_VM_PDB1;
1986 adev->vm_manager.root_level = AMDGPU_VM_PDB0;
1989 dev_err(adev->dev, "VMPT only supports 2~4+1 levels\n");
1991 /* block size depends on vm size and hw setup*/
1992 if (amdgpu_vm_block_size != -1)
1993 adev->vm_manager.block_size =
1994 min((unsigned)amdgpu_vm_block_size, max_bits
1995 - AMDGPU_GPU_PAGE_SHIFT
1996 - 9 * adev->vm_manager.num_level);
1997 else if (adev->vm_manager.num_level > 1)
1998 adev->vm_manager.block_size = 9;
2000 adev->vm_manager.block_size = amdgpu_vm_get_block_size(tmp);
2002 if (amdgpu_vm_fragment_size == -1)
2003 adev->vm_manager.fragment_size = fragment_size_default;
2005 adev->vm_manager.fragment_size = amdgpu_vm_fragment_size;
2007 DRM_INFO("vm size is %u GB, %u levels, block size is %u-bit, fragment size is %u-bit\n",
2008 vm_size, adev->vm_manager.num_level + 1,
2009 adev->vm_manager.block_size,
2010 adev->vm_manager.fragment_size);
2014 * amdgpu_vm_wait_idle - wait for the VM to become idle
2016 * @vm: VM object to wait for
2017 * @timeout: timeout to wait for VM to become idle
2019 long amdgpu_vm_wait_idle(struct amdgpu_vm *vm, long timeout)
2021 timeout = dma_resv_wait_timeout(vm->root.bo->tbo.base.resv,
2022 DMA_RESV_USAGE_BOOKKEEP,
2027 return dma_fence_wait_timeout(vm->last_unlocked, true, timeout);
2031 * amdgpu_vm_init - initialize a vm instance
2033 * @adev: amdgpu_device pointer
2039 * 0 for success, error for failure.
2041 int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm)
2043 struct amdgpu_bo *root_bo;
2044 struct amdgpu_bo_vm *root;
2047 vm->va = RB_ROOT_CACHED;
2048 for (i = 0; i < AMDGPU_MAX_VMHUBS; i++)
2049 vm->reserved_vmid[i] = NULL;
2050 INIT_LIST_HEAD(&vm->evicted);
2051 INIT_LIST_HEAD(&vm->relocated);
2052 INIT_LIST_HEAD(&vm->moved);
2053 INIT_LIST_HEAD(&vm->idle);
2054 INIT_LIST_HEAD(&vm->invalidated);
2055 spin_lock_init(&vm->status_lock);
2056 INIT_LIST_HEAD(&vm->freed);
2057 INIT_LIST_HEAD(&vm->done);
2058 INIT_LIST_HEAD(&vm->pt_freed);
2059 INIT_WORK(&vm->pt_free_work, amdgpu_vm_pt_free_work);
2061 /* create scheduler entities for page table updates */
2062 r = drm_sched_entity_init(&vm->immediate, DRM_SCHED_PRIORITY_NORMAL,
2063 adev->vm_manager.vm_pte_scheds,
2064 adev->vm_manager.vm_pte_num_scheds, NULL);
2068 r = drm_sched_entity_init(&vm->delayed, DRM_SCHED_PRIORITY_NORMAL,
2069 adev->vm_manager.vm_pte_scheds,
2070 adev->vm_manager.vm_pte_num_scheds, NULL);
2072 goto error_free_immediate;
2074 vm->pte_support_ats = false;
2075 vm->is_compute_context = false;
2077 vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode &
2078 AMDGPU_VM_USE_CPU_FOR_GFX);
2080 DRM_DEBUG_DRIVER("VM update mode is %s\n",
2081 vm->use_cpu_for_update ? "CPU" : "SDMA");
2082 WARN_ONCE((vm->use_cpu_for_update &&
2083 !amdgpu_gmc_vram_full_visible(&adev->gmc)),
2084 "CPU update of VM recommended only for large BAR system\n");
2086 if (vm->use_cpu_for_update)
2087 vm->update_funcs = &amdgpu_vm_cpu_funcs;
2089 vm->update_funcs = &amdgpu_vm_sdma_funcs;
2090 vm->last_update = NULL;
2091 vm->last_unlocked = dma_fence_get_stub();
2092 vm->last_tlb_flush = dma_fence_get_stub();
2094 mutex_init(&vm->eviction_lock);
2095 vm->evicting = false;
2097 r = amdgpu_vm_pt_create(adev, vm, adev->vm_manager.root_level,
2100 goto error_free_delayed;
2101 root_bo = &root->bo;
2102 r = amdgpu_bo_reserve(root_bo, true);
2104 goto error_free_root;
2106 r = dma_resv_reserve_fences(root_bo->tbo.base.resv, 1);
2108 goto error_unreserve;
2110 amdgpu_vm_bo_base_init(&vm->root, vm, root_bo);
2112 r = amdgpu_vm_pt_clear(adev, vm, root, false);
2114 goto error_unreserve;
2116 amdgpu_bo_unreserve(vm->root.bo);
2118 INIT_KFIFO(vm->faults);
2123 amdgpu_bo_unreserve(vm->root.bo);
2126 amdgpu_bo_unref(&root->shadow);
2127 amdgpu_bo_unref(&root_bo);
2131 dma_fence_put(vm->last_tlb_flush);
2132 dma_fence_put(vm->last_unlocked);
2133 drm_sched_entity_destroy(&vm->delayed);
2135 error_free_immediate:
2136 drm_sched_entity_destroy(&vm->immediate);
2142 * amdgpu_vm_make_compute - Turn a GFX VM into a compute VM
2144 * @adev: amdgpu_device pointer
2147 * This only works on GFX VMs that don't have any BOs added and no
2148 * page tables allocated yet.
2150 * Changes the following VM parameters:
2151 * - use_cpu_for_update
2152 * - pte_supports_ats
2154 * Reinitializes the page directory to reflect the changed ATS
2158 * 0 for success, -errno for errors.
2160 int amdgpu_vm_make_compute(struct amdgpu_device *adev, struct amdgpu_vm *vm)
2162 bool pte_support_ats = (adev->asic_type == CHIP_RAVEN);
2165 r = amdgpu_bo_reserve(vm->root.bo, true);
2170 if (!amdgpu_vm_pt_is_root_clean(adev, vm)) {
2175 /* Check if PD needs to be reinitialized and do it before
2176 * changing any other state, in case it fails.
2178 if (pte_support_ats != vm->pte_support_ats) {
2179 vm->pte_support_ats = pte_support_ats;
2180 r = amdgpu_vm_pt_clear(adev, vm, to_amdgpu_bo_vm(vm->root.bo),
2186 /* Update VM state */
2187 vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode &
2188 AMDGPU_VM_USE_CPU_FOR_COMPUTE);
2189 DRM_DEBUG_DRIVER("VM update mode is %s\n",
2190 vm->use_cpu_for_update ? "CPU" : "SDMA");
2191 WARN_ONCE((vm->use_cpu_for_update &&
2192 !amdgpu_gmc_vram_full_visible(&adev->gmc)),
2193 "CPU update of VM recommended only for large BAR system\n");
2195 if (vm->use_cpu_for_update) {
2196 /* Sync with last SDMA update/clear before switching to CPU */
2197 r = amdgpu_bo_sync_wait(vm->root.bo,
2198 AMDGPU_FENCE_OWNER_UNDEFINED, true);
2202 vm->update_funcs = &amdgpu_vm_cpu_funcs;
2204 vm->update_funcs = &amdgpu_vm_sdma_funcs;
2207 * Make sure root PD gets mapped. As vm_update_mode could be changed
2208 * when turning a GFX VM into a compute VM.
2210 r = vm->update_funcs->map_table(to_amdgpu_bo_vm(vm->root.bo));
2214 dma_fence_put(vm->last_update);
2215 vm->last_update = NULL;
2216 vm->is_compute_context = true;
2218 /* Free the shadow bo for compute VM */
2219 amdgpu_bo_unref(&to_amdgpu_bo_vm(vm->root.bo)->shadow);
2224 amdgpu_bo_unreserve(vm->root.bo);
2229 * amdgpu_vm_release_compute - release a compute vm
2230 * @adev: amdgpu_device pointer
2231 * @vm: a vm turned into compute vm by calling amdgpu_vm_make_compute
2233 * This is a correspondant of amdgpu_vm_make_compute. It decouples compute
2234 * pasid from vm. Compute should stop use of vm after this call.
2236 void amdgpu_vm_release_compute(struct amdgpu_device *adev, struct amdgpu_vm *vm)
2238 amdgpu_vm_set_pasid(adev, vm, 0);
2239 vm->is_compute_context = false;
2243 * amdgpu_vm_fini - tear down a vm instance
2245 * @adev: amdgpu_device pointer
2249 * Unbind the VM and remove all bos from the vm bo list
2251 void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
2253 struct amdgpu_bo_va_mapping *mapping, *tmp;
2254 bool prt_fini_needed = !!adev->gmc.gmc_funcs->set_prt;
2255 struct amdgpu_bo *root;
2256 unsigned long flags;
2259 amdgpu_amdkfd_gpuvm_destroy_cb(adev, vm);
2261 flush_work(&vm->pt_free_work);
2263 root = amdgpu_bo_ref(vm->root.bo);
2264 amdgpu_bo_reserve(root, true);
2265 amdgpu_vm_set_pasid(adev, vm, 0);
2266 dma_fence_wait(vm->last_unlocked, false);
2267 dma_fence_put(vm->last_unlocked);
2268 dma_fence_wait(vm->last_tlb_flush, false);
2269 /* Make sure that all fence callbacks have completed */
2270 spin_lock_irqsave(vm->last_tlb_flush->lock, flags);
2271 spin_unlock_irqrestore(vm->last_tlb_flush->lock, flags);
2272 dma_fence_put(vm->last_tlb_flush);
2274 list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
2275 if (mapping->flags & AMDGPU_PTE_PRT && prt_fini_needed) {
2276 amdgpu_vm_prt_fini(adev, vm);
2277 prt_fini_needed = false;
2280 list_del(&mapping->list);
2281 amdgpu_vm_free_mapping(adev, vm, mapping, NULL);
2284 amdgpu_vm_pt_free_root(adev, vm);
2285 amdgpu_bo_unreserve(root);
2286 amdgpu_bo_unref(&root);
2287 WARN_ON(vm->root.bo);
2289 drm_sched_entity_destroy(&vm->immediate);
2290 drm_sched_entity_destroy(&vm->delayed);
2292 if (!RB_EMPTY_ROOT(&vm->va.rb_root)) {
2293 dev_err(adev->dev, "still active bo inside vm\n");
2295 rbtree_postorder_for_each_entry_safe(mapping, tmp,
2296 &vm->va.rb_root, rb) {
2297 /* Don't remove the mapping here, we don't want to trigger a
2298 * rebalance and the tree is about to be destroyed anyway.
2300 list_del(&mapping->list);
2304 dma_fence_put(vm->last_update);
2305 for (i = 0; i < AMDGPU_MAX_VMHUBS; i++)
2306 amdgpu_vmid_free_reserved(adev, vm, i);
2310 * amdgpu_vm_manager_init - init the VM manager
2312 * @adev: amdgpu_device pointer
2314 * Initialize the VM manager structures
2316 void amdgpu_vm_manager_init(struct amdgpu_device *adev)
2320 /* Concurrent flushes are only possible starting with Vega10 and
2321 * are broken on Navi10 and Navi14.
2323 adev->vm_manager.concurrent_flush = !(adev->asic_type < CHIP_VEGA10 ||
2324 adev->asic_type == CHIP_NAVI10 ||
2325 adev->asic_type == CHIP_NAVI14);
2326 amdgpu_vmid_mgr_init(adev);
2328 adev->vm_manager.fence_context =
2329 dma_fence_context_alloc(AMDGPU_MAX_RINGS);
2330 for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
2331 adev->vm_manager.seqno[i] = 0;
2333 spin_lock_init(&adev->vm_manager.prt_lock);
2334 atomic_set(&adev->vm_manager.num_prt_users, 0);
2336 /* If not overridden by the user, by default, only in large BAR systems
2337 * Compute VM tables will be updated by CPU
2339 #ifdef CONFIG_X86_64
2340 if (amdgpu_vm_update_mode == -1) {
2341 if (amdgpu_gmc_vram_full_visible(&adev->gmc))
2342 adev->vm_manager.vm_update_mode =
2343 AMDGPU_VM_USE_CPU_FOR_COMPUTE;
2345 adev->vm_manager.vm_update_mode = 0;
2347 adev->vm_manager.vm_update_mode = amdgpu_vm_update_mode;
2349 adev->vm_manager.vm_update_mode = 0;
2352 xa_init_flags(&adev->vm_manager.pasids, XA_FLAGS_LOCK_IRQ);
2356 * amdgpu_vm_manager_fini - cleanup VM manager
2358 * @adev: amdgpu_device pointer
2360 * Cleanup the VM manager and free resources.
2362 void amdgpu_vm_manager_fini(struct amdgpu_device *adev)
2364 WARN_ON(!xa_empty(&adev->vm_manager.pasids));
2365 xa_destroy(&adev->vm_manager.pasids);
2367 amdgpu_vmid_mgr_fini(adev);
2371 * amdgpu_vm_ioctl - Manages VMID reservation for vm hubs.
2373 * @dev: drm device pointer
2374 * @data: drm_amdgpu_vm
2375 * @filp: drm file pointer
2378 * 0 for success, -errno for errors.
2380 int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
2382 union drm_amdgpu_vm *args = data;
2383 struct amdgpu_device *adev = drm_to_adev(dev);
2384 struct amdgpu_fpriv *fpriv = filp->driver_priv;
2385 long timeout = msecs_to_jiffies(2000);
2388 switch (args->in.op) {
2389 case AMDGPU_VM_OP_RESERVE_VMID:
2390 /* We only have requirement to reserve vmid from gfxhub */
2391 r = amdgpu_vmid_alloc_reserved(adev, &fpriv->vm,
2396 case AMDGPU_VM_OP_UNRESERVE_VMID:
2397 if (amdgpu_sriov_runtime(adev))
2398 timeout = 8 * timeout;
2400 /* Wait vm idle to make sure the vmid set in SPM_VMID is
2401 * not referenced anymore.
2403 r = amdgpu_bo_reserve(fpriv->vm.root.bo, true);
2407 r = amdgpu_vm_wait_idle(&fpriv->vm, timeout);
2411 amdgpu_bo_unreserve(fpriv->vm.root.bo);
2412 amdgpu_vmid_free_reserved(adev, &fpriv->vm, AMDGPU_GFXHUB_0);
2422 * amdgpu_vm_get_task_info - Extracts task info for a PASID.
2424 * @adev: drm device pointer
2425 * @pasid: PASID identifier for VM
2426 * @task_info: task_info to fill.
2428 void amdgpu_vm_get_task_info(struct amdgpu_device *adev, u32 pasid,
2429 struct amdgpu_task_info *task_info)
2431 struct amdgpu_vm *vm;
2432 unsigned long flags;
2434 xa_lock_irqsave(&adev->vm_manager.pasids, flags);
2436 vm = xa_load(&adev->vm_manager.pasids, pasid);
2438 *task_info = vm->task_info;
2440 xa_unlock_irqrestore(&adev->vm_manager.pasids, flags);
2444 * amdgpu_vm_set_task_info - Sets VMs task info.
2446 * @vm: vm for which to set the info
2448 void amdgpu_vm_set_task_info(struct amdgpu_vm *vm)
2450 if (vm->task_info.pid)
2453 vm->task_info.pid = current->pid;
2454 get_task_comm(vm->task_info.task_name, current);
2456 if (current->group_leader->mm != current->mm)
2459 vm->task_info.tgid = current->group_leader->pid;
2460 get_task_comm(vm->task_info.process_name, current->group_leader);
2464 * amdgpu_vm_handle_fault - graceful handling of VM faults.
2465 * @adev: amdgpu device pointer
2466 * @pasid: PASID of the VM
2467 * @addr: Address of the fault
2468 * @write_fault: true is write fault, false is read fault
2470 * Try to gracefully handle a VM fault. Return true if the fault was handled and
2471 * shouldn't be reported any more.
2473 bool amdgpu_vm_handle_fault(struct amdgpu_device *adev, u32 pasid,
2474 uint64_t addr, bool write_fault)
2476 bool is_compute_context = false;
2477 struct amdgpu_bo *root;
2478 unsigned long irqflags;
2479 uint64_t value, flags;
2480 struct amdgpu_vm *vm;
2483 xa_lock_irqsave(&adev->vm_manager.pasids, irqflags);
2484 vm = xa_load(&adev->vm_manager.pasids, pasid);
2486 root = amdgpu_bo_ref(vm->root.bo);
2487 is_compute_context = vm->is_compute_context;
2491 xa_unlock_irqrestore(&adev->vm_manager.pasids, irqflags);
2496 addr /= AMDGPU_GPU_PAGE_SIZE;
2498 if (is_compute_context &&
2499 !svm_range_restore_pages(adev, pasid, addr, write_fault)) {
2500 amdgpu_bo_unref(&root);
2504 r = amdgpu_bo_reserve(root, true);
2508 /* Double check that the VM still exists */
2509 xa_lock_irqsave(&adev->vm_manager.pasids, irqflags);
2510 vm = xa_load(&adev->vm_manager.pasids, pasid);
2511 if (vm && vm->root.bo != root)
2513 xa_unlock_irqrestore(&adev->vm_manager.pasids, irqflags);
2517 flags = AMDGPU_PTE_VALID | AMDGPU_PTE_SNOOPED |
2520 if (is_compute_context) {
2521 /* Intentionally setting invalid PTE flag
2522 * combination to force a no-retry-fault
2524 flags = AMDGPU_PTE_SNOOPED | AMDGPU_PTE_PRT;
2526 } else if (amdgpu_vm_fault_stop == AMDGPU_VM_FAULT_STOP_NEVER) {
2527 /* Redirect the access to the dummy page */
2528 value = adev->dummy_page_addr;
2529 flags |= AMDGPU_PTE_EXECUTABLE | AMDGPU_PTE_READABLE |
2530 AMDGPU_PTE_WRITEABLE;
2533 /* Let the hw retry silently on the PTE */
2537 r = dma_resv_reserve_fences(root->tbo.base.resv, 1);
2539 pr_debug("failed %d to reserve fence slot\n", r);
2543 r = amdgpu_vm_update_range(adev, vm, true, false, false, NULL, addr,
2544 addr, flags, value, 0, NULL, NULL, NULL);
2548 r = amdgpu_vm_update_pdes(adev, vm, true);
2551 amdgpu_bo_unreserve(root);
2553 DRM_ERROR("Can't handle page fault (%d)\n", r);
2556 amdgpu_bo_unref(&root);
2561 #if defined(CONFIG_DEBUG_FS)
2563 * amdgpu_debugfs_vm_bo_info - print BO info for the VM
2565 * @vm: Requested VM for printing BO info
2568 * Print BO information in debugfs file for the VM
2570 void amdgpu_debugfs_vm_bo_info(struct amdgpu_vm *vm, struct seq_file *m)
2572 struct amdgpu_bo_va *bo_va, *tmp;
2574 u64 total_evicted = 0;
2575 u64 total_relocated = 0;
2576 u64 total_moved = 0;
2577 u64 total_invalidated = 0;
2579 unsigned int total_idle_objs = 0;
2580 unsigned int total_evicted_objs = 0;
2581 unsigned int total_relocated_objs = 0;
2582 unsigned int total_moved_objs = 0;
2583 unsigned int total_invalidated_objs = 0;
2584 unsigned int total_done_objs = 0;
2585 unsigned int id = 0;
2587 spin_lock(&vm->status_lock);
2588 seq_puts(m, "\tIdle BOs:\n");
2589 list_for_each_entry_safe(bo_va, tmp, &vm->idle, base.vm_status) {
2590 if (!bo_va->base.bo)
2592 total_idle += amdgpu_bo_print_info(id++, bo_va->base.bo, m);
2594 total_idle_objs = id;
2597 seq_puts(m, "\tEvicted BOs:\n");
2598 list_for_each_entry_safe(bo_va, tmp, &vm->evicted, base.vm_status) {
2599 if (!bo_va->base.bo)
2601 total_evicted += amdgpu_bo_print_info(id++, bo_va->base.bo, m);
2603 total_evicted_objs = id;
2606 seq_puts(m, "\tRelocated BOs:\n");
2607 list_for_each_entry_safe(bo_va, tmp, &vm->relocated, base.vm_status) {
2608 if (!bo_va->base.bo)
2610 total_relocated += amdgpu_bo_print_info(id++, bo_va->base.bo, m);
2612 total_relocated_objs = id;
2615 seq_puts(m, "\tMoved BOs:\n");
2616 list_for_each_entry_safe(bo_va, tmp, &vm->moved, base.vm_status) {
2617 if (!bo_va->base.bo)
2619 total_moved += amdgpu_bo_print_info(id++, bo_va->base.bo, m);
2621 total_moved_objs = id;
2624 seq_puts(m, "\tInvalidated BOs:\n");
2625 list_for_each_entry_safe(bo_va, tmp, &vm->invalidated, base.vm_status) {
2626 if (!bo_va->base.bo)
2628 total_invalidated += amdgpu_bo_print_info(id++, bo_va->base.bo, m);
2630 total_invalidated_objs = id;
2633 seq_puts(m, "\tDone BOs:\n");
2634 list_for_each_entry_safe(bo_va, tmp, &vm->done, base.vm_status) {
2635 if (!bo_va->base.bo)
2637 total_done += amdgpu_bo_print_info(id++, bo_va->base.bo, m);
2639 spin_unlock(&vm->status_lock);
2640 total_done_objs = id;
2642 seq_printf(m, "\tTotal idle size: %12lld\tobjs:\t%d\n", total_idle,
2644 seq_printf(m, "\tTotal evicted size: %12lld\tobjs:\t%d\n", total_evicted,
2645 total_evicted_objs);
2646 seq_printf(m, "\tTotal relocated size: %12lld\tobjs:\t%d\n", total_relocated,
2647 total_relocated_objs);
2648 seq_printf(m, "\tTotal moved size: %12lld\tobjs:\t%d\n", total_moved,
2650 seq_printf(m, "\tTotal invalidated size: %12lld\tobjs:\t%d\n", total_invalidated,
2651 total_invalidated_objs);
2652 seq_printf(m, "\tTotal done size: %12lld\tobjs:\t%d\n", total_done,