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 the MMU functionality provided on the GPU.
49 * GPUVM is similar to the legacy GART on older asics, however
50 * rather than there being a single global GART table
51 * for the entire GPU, there can be multiple GPUVM page tables active
52 * at any given time. The GPUVM page tables can contain a mix
53 * VRAM pages and system pages (both memory and MMIO) and system pages
54 * can be mapped as snooped (cached system pages) or unsnooped
55 * (uncached system pages).
57 * Each active GPUVM has an ID associated with it and there is a page table
58 * linked with each VMID. When executing a command buffer,
59 * the kernel tells the engine what VMID to use for that command
60 * buffer. VMIDs are allocated dynamically as commands are submitted.
61 * The userspace drivers maintain their own address space and the kernel
62 * sets up their pages tables accordingly when they submit their
63 * command buffers and a VMID is assigned.
64 * The hardware supports up to 16 active GPUVMs at any given time.
66 * Each GPUVM is represented by a 1-2 or 1-5 level page table, depending
67 * on the ASIC family. GPUVM supports RWX attributes on each page as well
68 * as other features such as encryption and caching attributes.
70 * VMID 0 is special. It is the GPUVM used for the kernel driver. In
71 * addition to an aperture managed by a page table, VMID 0 also has
72 * several other apertures. There is an aperture for direct access to VRAM
73 * and there is a legacy AGP aperture which just forwards accesses directly
74 * to the matching system physical addresses (or IOVAs when an IOMMU is
75 * present). These apertures provide direct access to these memories without
76 * incurring the overhead of a page table. VMID 0 is used by the kernel
77 * driver for tasks like memory management.
79 * GPU clients (i.e., engines on the GPU) use GPUVM VMIDs to access memory.
80 * For user applications, each application can have their own unique GPUVM
81 * address space. The application manages the address space and the kernel
82 * driver manages the GPUVM page tables for each process. If an GPU client
83 * accesses an invalid page, it will generate a GPU page fault, similar to
84 * accessing an invalid page on a CPU.
87 #define START(node) ((node)->start)
88 #define LAST(node) ((node)->last)
90 INTERVAL_TREE_DEFINE(struct amdgpu_bo_va_mapping, rb, uint64_t, __subtree_last,
91 START, LAST, static, amdgpu_vm_it)
97 * struct amdgpu_prt_cb - Helper to disable partial resident texture feature from a fence callback
99 struct amdgpu_prt_cb {
102 * @adev: amdgpu device
104 struct amdgpu_device *adev;
109 struct dma_fence_cb cb;
113 * struct amdgpu_vm_tlb_seq_cb - Helper to increment the TLB flush sequence
115 struct amdgpu_vm_tlb_seq_cb {
117 * @vm: pointer to the amdgpu_vm structure to set the fence sequence on
119 struct amdgpu_vm *vm;
124 struct dma_fence_cb cb;
128 * amdgpu_vm_set_pasid - manage pasid and vm ptr mapping
130 * @adev: amdgpu_device pointer
131 * @vm: amdgpu_vm pointer
132 * @pasid: the pasid the VM is using on this GPU
134 * Set the pasid this VM is using on this GPU, can also be used to remove the
135 * pasid by passing in zero.
138 int amdgpu_vm_set_pasid(struct amdgpu_device *adev, struct amdgpu_vm *vm,
143 if (vm->pasid == pasid)
147 r = xa_err(xa_erase_irq(&adev->vm_manager.pasids, vm->pasid));
155 r = xa_err(xa_store_irq(&adev->vm_manager.pasids, pasid, vm,
168 * amdgpu_vm_bo_evicted - vm_bo is evicted
170 * @vm_bo: vm_bo which is evicted
172 * State for PDs/PTs and per VM BOs which are not at the location they should
175 static void amdgpu_vm_bo_evicted(struct amdgpu_vm_bo_base *vm_bo)
177 struct amdgpu_vm *vm = vm_bo->vm;
178 struct amdgpu_bo *bo = vm_bo->bo;
181 spin_lock(&vm_bo->vm->status_lock);
182 if (bo->tbo.type == ttm_bo_type_kernel)
183 list_move(&vm_bo->vm_status, &vm->evicted);
185 list_move_tail(&vm_bo->vm_status, &vm->evicted);
186 spin_unlock(&vm_bo->vm->status_lock);
189 * amdgpu_vm_bo_moved - vm_bo is moved
191 * @vm_bo: vm_bo which is moved
193 * State for per VM BOs which are moved, but that change is not yet reflected
194 * in the page tables.
196 static void amdgpu_vm_bo_moved(struct amdgpu_vm_bo_base *vm_bo)
198 spin_lock(&vm_bo->vm->status_lock);
199 list_move(&vm_bo->vm_status, &vm_bo->vm->moved);
200 spin_unlock(&vm_bo->vm->status_lock);
204 * amdgpu_vm_bo_idle - vm_bo is idle
206 * @vm_bo: vm_bo which is now idle
208 * State for PDs/PTs and per VM BOs which have gone through the state machine
211 static void amdgpu_vm_bo_idle(struct amdgpu_vm_bo_base *vm_bo)
213 spin_lock(&vm_bo->vm->status_lock);
214 list_move(&vm_bo->vm_status, &vm_bo->vm->idle);
215 spin_unlock(&vm_bo->vm->status_lock);
216 vm_bo->moved = false;
220 * amdgpu_vm_bo_invalidated - vm_bo is invalidated
222 * @vm_bo: vm_bo which is now invalidated
224 * State for normal BOs which are invalidated and that change not yet reflected
227 static void amdgpu_vm_bo_invalidated(struct amdgpu_vm_bo_base *vm_bo)
229 spin_lock(&vm_bo->vm->status_lock);
230 list_move(&vm_bo->vm_status, &vm_bo->vm->invalidated);
231 spin_unlock(&vm_bo->vm->status_lock);
235 * amdgpu_vm_bo_relocated - vm_bo is reloacted
237 * @vm_bo: vm_bo which is relocated
239 * State for PDs/PTs which needs to update their parent PD.
240 * For the root PD, just move to idle state.
242 static void amdgpu_vm_bo_relocated(struct amdgpu_vm_bo_base *vm_bo)
244 if (vm_bo->bo->parent) {
245 spin_lock(&vm_bo->vm->status_lock);
246 list_move(&vm_bo->vm_status, &vm_bo->vm->relocated);
247 spin_unlock(&vm_bo->vm->status_lock);
249 amdgpu_vm_bo_idle(vm_bo);
254 * amdgpu_vm_bo_done - vm_bo is done
256 * @vm_bo: vm_bo which is now done
258 * State for normal BOs which are invalidated and that change has been updated
261 static void amdgpu_vm_bo_done(struct amdgpu_vm_bo_base *vm_bo)
263 spin_lock(&vm_bo->vm->status_lock);
264 list_move(&vm_bo->vm_status, &vm_bo->vm->done);
265 spin_unlock(&vm_bo->vm->status_lock);
269 * amdgpu_vm_bo_base_init - Adds bo to the list of bos associated with the vm
271 * @base: base structure for tracking BO usage in a VM
272 * @vm: vm to which bo is to be added
273 * @bo: amdgpu buffer object
275 * Initialize a bo_va_base structure and add it to the appropriate lists
278 void amdgpu_vm_bo_base_init(struct amdgpu_vm_bo_base *base,
279 struct amdgpu_vm *vm, struct amdgpu_bo *bo)
284 INIT_LIST_HEAD(&base->vm_status);
288 base->next = bo->vm_bo;
291 if (bo->tbo.base.resv != vm->root.bo->tbo.base.resv)
294 dma_resv_assert_held(vm->root.bo->tbo.base.resv);
296 ttm_bo_set_bulk_move(&bo->tbo, &vm->lru_bulk_move);
297 if (bo->tbo.type == ttm_bo_type_kernel && bo->parent)
298 amdgpu_vm_bo_relocated(base);
300 amdgpu_vm_bo_idle(base);
302 if (bo->preferred_domains &
303 amdgpu_mem_type_to_domain(bo->tbo.resource->mem_type))
307 * we checked all the prerequisites, but it looks like this per vm bo
308 * is currently evicted. add the bo to the evicted list to make sure it
309 * is validated on next vm use to avoid fault.
311 amdgpu_vm_bo_evicted(base);
315 * amdgpu_vm_get_pd_bo - add the VM PD to a validation list
317 * @vm: vm providing the BOs
318 * @validated: head of validation list
319 * @entry: entry to add
321 * Add the page directory to the list of BOs to
322 * validate for command submission.
324 void amdgpu_vm_get_pd_bo(struct amdgpu_vm *vm,
325 struct list_head *validated,
326 struct amdgpu_bo_list_entry *entry)
329 entry->tv.bo = &vm->root.bo->tbo;
330 /* Two for VM updates, one for TTM and one for the CS job */
331 entry->tv.num_shared = 4;
332 entry->user_pages = NULL;
333 list_add(&entry->tv.head, validated);
337 * amdgpu_vm_move_to_lru_tail - move all BOs to the end of LRU
339 * @adev: amdgpu device pointer
340 * @vm: vm providing the BOs
342 * Move all BOs to the end of LRU and remember their positions to put them
345 void amdgpu_vm_move_to_lru_tail(struct amdgpu_device *adev,
346 struct amdgpu_vm *vm)
348 spin_lock(&adev->mman.bdev.lru_lock);
349 ttm_lru_bulk_move_tail(&vm->lru_bulk_move);
350 spin_unlock(&adev->mman.bdev.lru_lock);
354 * amdgpu_vm_validate_pt_bos - validate the page table BOs
356 * @adev: amdgpu device pointer
357 * @vm: vm providing the BOs
358 * @validate: callback to do the validation
359 * @param: parameter for the validation callback
361 * Validate the page table BOs on command submission if neccessary.
366 int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
367 int (*validate)(void *p, struct amdgpu_bo *bo),
370 struct amdgpu_vm_bo_base *bo_base;
371 struct amdgpu_bo *shadow;
372 struct amdgpu_bo *bo;
375 spin_lock(&vm->status_lock);
376 while (!list_empty(&vm->evicted)) {
377 bo_base = list_first_entry(&vm->evicted,
378 struct amdgpu_vm_bo_base,
380 spin_unlock(&vm->status_lock);
383 shadow = amdgpu_bo_shadowed(bo);
385 r = validate(param, bo);
389 r = validate(param, shadow);
394 if (bo->tbo.type != ttm_bo_type_kernel) {
395 amdgpu_vm_bo_moved(bo_base);
397 vm->update_funcs->map_table(to_amdgpu_bo_vm(bo));
398 amdgpu_vm_bo_relocated(bo_base);
400 spin_lock(&vm->status_lock);
402 spin_unlock(&vm->status_lock);
404 amdgpu_vm_eviction_lock(vm);
405 vm->evicting = false;
406 amdgpu_vm_eviction_unlock(vm);
412 * amdgpu_vm_ready - check VM is ready for updates
416 * Check if all VM PDs/PTs are ready for updates
419 * True if VM is not evicting.
421 bool amdgpu_vm_ready(struct amdgpu_vm *vm)
426 amdgpu_vm_eviction_lock(vm);
428 amdgpu_vm_eviction_unlock(vm);
430 spin_lock(&vm->status_lock);
431 empty = list_empty(&vm->evicted);
432 spin_unlock(&vm->status_lock);
438 * amdgpu_vm_check_compute_bug - check whether asic has compute vm bug
440 * @adev: amdgpu_device pointer
442 void amdgpu_vm_check_compute_bug(struct amdgpu_device *adev)
444 const struct amdgpu_ip_block *ip_block;
445 bool has_compute_vm_bug;
446 struct amdgpu_ring *ring;
449 has_compute_vm_bug = false;
451 ip_block = amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_GFX);
453 /* Compute has a VM bug for GFX version < 7.
454 Compute has a VM bug for GFX 8 MEC firmware version < 673.*/
455 if (ip_block->version->major <= 7)
456 has_compute_vm_bug = true;
457 else if (ip_block->version->major == 8)
458 if (adev->gfx.mec_fw_version < 673)
459 has_compute_vm_bug = true;
462 for (i = 0; i < adev->num_rings; i++) {
463 ring = adev->rings[i];
464 if (ring->funcs->type == AMDGPU_RING_TYPE_COMPUTE)
465 /* only compute rings */
466 ring->has_compute_vm_bug = has_compute_vm_bug;
468 ring->has_compute_vm_bug = false;
473 * amdgpu_vm_need_pipeline_sync - Check if pipe sync is needed for job.
475 * @ring: ring on which the job will be submitted
476 * @job: job to submit
479 * True if sync is needed.
481 bool amdgpu_vm_need_pipeline_sync(struct amdgpu_ring *ring,
482 struct amdgpu_job *job)
484 struct amdgpu_device *adev = ring->adev;
485 unsigned vmhub = ring->funcs->vmhub;
486 struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
487 struct amdgpu_vmid *id;
488 bool gds_switch_needed;
489 bool vm_flush_needed = job->vm_needs_flush || ring->has_compute_vm_bug;
493 id = &id_mgr->ids[job->vmid];
494 gds_switch_needed = ring->funcs->emit_gds_switch && (
495 id->gds_base != job->gds_base ||
496 id->gds_size != job->gds_size ||
497 id->gws_base != job->gws_base ||
498 id->gws_size != job->gws_size ||
499 id->oa_base != job->oa_base ||
500 id->oa_size != job->oa_size);
502 if (amdgpu_vmid_had_gpu_reset(adev, id))
505 return vm_flush_needed || gds_switch_needed;
509 * amdgpu_vm_flush - hardware flush the vm
511 * @ring: ring to use for flush
513 * @need_pipe_sync: is pipe sync needed
515 * Emit a VM flush when it is necessary.
518 * 0 on success, errno otherwise.
520 int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job,
523 struct amdgpu_device *adev = ring->adev;
524 unsigned vmhub = ring->funcs->vmhub;
525 struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
526 struct amdgpu_vmid *id = &id_mgr->ids[job->vmid];
527 bool gds_switch_needed = ring->funcs->emit_gds_switch && (
528 id->gds_base != job->gds_base ||
529 id->gds_size != job->gds_size ||
530 id->gws_base != job->gws_base ||
531 id->gws_size != job->gws_size ||
532 id->oa_base != job->oa_base ||
533 id->oa_size != job->oa_size);
534 bool vm_flush_needed = job->vm_needs_flush;
535 struct dma_fence *fence = NULL;
536 bool pasid_mapping_needed = false;
537 unsigned patch_offset = 0;
538 bool update_spm_vmid_needed = (job->vm && (job->vm->reserved_vmid[vmhub] != NULL));
541 if (update_spm_vmid_needed && adev->gfx.rlc.funcs->update_spm_vmid)
542 adev->gfx.rlc.funcs->update_spm_vmid(adev, job->vmid);
544 if (amdgpu_vmid_had_gpu_reset(adev, id)) {
545 gds_switch_needed = true;
546 vm_flush_needed = true;
547 pasid_mapping_needed = true;
550 mutex_lock(&id_mgr->lock);
551 if (id->pasid != job->pasid || !id->pasid_mapping ||
552 !dma_fence_is_signaled(id->pasid_mapping))
553 pasid_mapping_needed = true;
554 mutex_unlock(&id_mgr->lock);
556 gds_switch_needed &= !!ring->funcs->emit_gds_switch;
557 vm_flush_needed &= !!ring->funcs->emit_vm_flush &&
558 job->vm_pd_addr != AMDGPU_BO_INVALID_OFFSET;
559 pasid_mapping_needed &= adev->gmc.gmc_funcs->emit_pasid_mapping &&
560 ring->funcs->emit_wreg;
562 if (!vm_flush_needed && !gds_switch_needed && !need_pipe_sync)
565 amdgpu_ring_ib_begin(ring);
566 if (ring->funcs->init_cond_exec)
567 patch_offset = amdgpu_ring_init_cond_exec(ring);
570 amdgpu_ring_emit_pipeline_sync(ring);
572 if (vm_flush_needed) {
573 trace_amdgpu_vm_flush(ring, job->vmid, job->vm_pd_addr);
574 amdgpu_ring_emit_vm_flush(ring, job->vmid, job->vm_pd_addr);
577 if (pasid_mapping_needed)
578 amdgpu_gmc_emit_pasid_mapping(ring, job->vmid, job->pasid);
580 if (vm_flush_needed || pasid_mapping_needed) {
581 r = amdgpu_fence_emit(ring, &fence, NULL, 0);
586 if (vm_flush_needed) {
587 mutex_lock(&id_mgr->lock);
588 dma_fence_put(id->last_flush);
589 id->last_flush = dma_fence_get(fence);
590 id->current_gpu_reset_count =
591 atomic_read(&adev->gpu_reset_counter);
592 mutex_unlock(&id_mgr->lock);
595 if (pasid_mapping_needed) {
596 mutex_lock(&id_mgr->lock);
597 id->pasid = job->pasid;
598 dma_fence_put(id->pasid_mapping);
599 id->pasid_mapping = dma_fence_get(fence);
600 mutex_unlock(&id_mgr->lock);
602 dma_fence_put(fence);
604 if (!ring->is_mes_queue && ring->funcs->emit_gds_switch &&
606 id->gds_base = job->gds_base;
607 id->gds_size = job->gds_size;
608 id->gws_base = job->gws_base;
609 id->gws_size = job->gws_size;
610 id->oa_base = job->oa_base;
611 id->oa_size = job->oa_size;
612 amdgpu_ring_emit_gds_switch(ring, job->vmid, job->gds_base,
613 job->gds_size, job->gws_base,
614 job->gws_size, job->oa_base,
618 if (ring->funcs->patch_cond_exec)
619 amdgpu_ring_patch_cond_exec(ring, patch_offset);
621 /* the double SWITCH_BUFFER here *cannot* be skipped by COND_EXEC */
622 if (ring->funcs->emit_switch_buffer) {
623 amdgpu_ring_emit_switch_buffer(ring);
624 amdgpu_ring_emit_switch_buffer(ring);
626 amdgpu_ring_ib_end(ring);
631 * amdgpu_vm_bo_find - find the bo_va for a specific vm & bo
634 * @bo: requested buffer object
636 * Find @bo inside the requested vm.
637 * Search inside the @bos vm list for the requested vm
638 * Returns the found bo_va or NULL if none is found
640 * Object has to be reserved!
643 * Found bo_va or NULL.
645 struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
646 struct amdgpu_bo *bo)
648 struct amdgpu_vm_bo_base *base;
650 for (base = bo->vm_bo; base; base = base->next) {
654 return container_of(base, struct amdgpu_bo_va, base);
660 * amdgpu_vm_map_gart - Resolve gart mapping of addr
662 * @pages_addr: optional DMA address to use for lookup
663 * @addr: the unmapped addr
665 * Look up the physical address of the page that the pte resolves
669 * The pointer for the page table entry.
671 uint64_t amdgpu_vm_map_gart(const dma_addr_t *pages_addr, uint64_t addr)
675 /* page table offset */
676 result = pages_addr[addr >> PAGE_SHIFT];
678 /* in case cpu page size != gpu page size*/
679 result |= addr & (~PAGE_MASK);
681 result &= 0xFFFFFFFFFFFFF000ULL;
687 * amdgpu_vm_update_pdes - make sure that all directories are valid
689 * @adev: amdgpu_device pointer
691 * @immediate: submit immediately to the paging queue
693 * Makes sure all directories are up to date.
696 * 0 for success, error for failure.
698 int amdgpu_vm_update_pdes(struct amdgpu_device *adev,
699 struct amdgpu_vm *vm, bool immediate)
701 struct amdgpu_vm_update_params params;
702 struct amdgpu_vm_bo_base *entry;
703 bool flush_tlb_needed = false;
704 LIST_HEAD(relocated);
707 spin_lock(&vm->status_lock);
708 list_splice_init(&vm->relocated, &relocated);
709 spin_unlock(&vm->status_lock);
711 if (list_empty(&relocated))
714 if (!drm_dev_enter(adev_to_drm(adev), &idx))
717 memset(¶ms, 0, sizeof(params));
720 params.immediate = immediate;
722 r = vm->update_funcs->prepare(¶ms, NULL, AMDGPU_SYNC_EXPLICIT);
726 list_for_each_entry(entry, &relocated, vm_status) {
727 /* vm_flush_needed after updating moved PDEs */
728 flush_tlb_needed |= entry->moved;
730 r = amdgpu_vm_pde_update(¶ms, entry);
735 r = vm->update_funcs->commit(¶ms, &vm->last_update);
739 if (flush_tlb_needed)
740 atomic64_inc(&vm->tlb_seq);
742 while (!list_empty(&relocated)) {
743 entry = list_first_entry(&relocated, struct amdgpu_vm_bo_base,
745 amdgpu_vm_bo_idle(entry);
754 * amdgpu_vm_tlb_seq_cb - make sure to increment tlb sequence
756 * @cb: the callback structure
758 * Increments the tlb sequence to make sure that future CS execute a VM flush.
760 static void amdgpu_vm_tlb_seq_cb(struct dma_fence *fence,
761 struct dma_fence_cb *cb)
763 struct amdgpu_vm_tlb_seq_cb *tlb_cb;
765 tlb_cb = container_of(cb, typeof(*tlb_cb), cb);
766 atomic64_inc(&tlb_cb->vm->tlb_seq);
771 * amdgpu_vm_update_range - update a range in the vm page table
773 * @adev: amdgpu_device pointer to use for commands
774 * @vm: the VM to update the range
775 * @immediate: immediate submission in a page fault
776 * @unlocked: unlocked invalidation during MM callback
777 * @flush_tlb: trigger tlb invalidation after update completed
778 * @resv: fences we need to sync to
779 * @start: start of mapped range
780 * @last: last mapped entry
781 * @flags: flags for the entries
782 * @offset: offset into nodes and pages_addr
783 * @vram_base: base for vram mappings
784 * @res: ttm_resource to map
785 * @pages_addr: DMA addresses to use for mapping
786 * @fence: optional resulting fence
788 * Fill in the page table entries between @start and @last.
791 * 0 for success, negative erro code for failure.
793 int amdgpu_vm_update_range(struct amdgpu_device *adev, struct amdgpu_vm *vm,
794 bool immediate, bool unlocked, bool flush_tlb,
795 struct dma_resv *resv, uint64_t start, uint64_t last,
796 uint64_t flags, uint64_t offset, uint64_t vram_base,
797 struct ttm_resource *res, dma_addr_t *pages_addr,
798 struct dma_fence **fence)
800 struct amdgpu_vm_update_params params;
801 struct amdgpu_vm_tlb_seq_cb *tlb_cb;
802 struct amdgpu_res_cursor cursor;
803 enum amdgpu_sync_mode sync_mode;
806 if (!drm_dev_enter(adev_to_drm(adev), &idx))
809 tlb_cb = kmalloc(sizeof(*tlb_cb), GFP_KERNEL);
815 /* Vega20+XGMI where PTEs get inadvertently cached in L2 texture cache,
816 * heavy-weight flush TLB unconditionally.
818 flush_tlb |= adev->gmc.xgmi.num_physical_nodes &&
819 adev->ip_versions[GC_HWIP][0] == IP_VERSION(9, 4, 0);
822 * On GFX8 and older any 8 PTE block with a valid bit set enters the TLB
824 flush_tlb |= adev->ip_versions[GC_HWIP][0] < IP_VERSION(9, 0, 0);
826 memset(¶ms, 0, sizeof(params));
829 params.immediate = immediate;
830 params.pages_addr = pages_addr;
831 params.unlocked = unlocked;
833 /* Implicitly sync to command submissions in the same VM before
834 * unmapping. Sync to moving fences before mapping.
836 if (!(flags & AMDGPU_PTE_VALID))
837 sync_mode = AMDGPU_SYNC_EQ_OWNER;
839 sync_mode = AMDGPU_SYNC_EXPLICIT;
841 amdgpu_vm_eviction_lock(vm);
847 if (!unlocked && !dma_fence_is_signaled(vm->last_unlocked)) {
848 struct dma_fence *tmp = dma_fence_get_stub();
850 amdgpu_bo_fence(vm->root.bo, vm->last_unlocked, true);
851 swap(vm->last_unlocked, tmp);
855 r = vm->update_funcs->prepare(¶ms, resv, sync_mode);
859 amdgpu_res_first(pages_addr ? NULL : res, offset,
860 (last - start + 1) * AMDGPU_GPU_PAGE_SIZE, &cursor);
861 while (cursor.remaining) {
862 uint64_t tmp, num_entries, addr;
864 num_entries = cursor.size >> AMDGPU_GPU_PAGE_SHIFT;
866 bool contiguous = true;
868 if (num_entries > AMDGPU_GPU_PAGES_IN_CPU_PAGE) {
869 uint64_t pfn = cursor.start >> PAGE_SHIFT;
872 contiguous = pages_addr[pfn + 1] ==
873 pages_addr[pfn] + PAGE_SIZE;
876 AMDGPU_GPU_PAGES_IN_CPU_PAGE;
877 for (count = 2; count < tmp; ++count) {
878 uint64_t idx = pfn + count;
880 if (contiguous != (pages_addr[idx] ==
881 pages_addr[idx - 1] + PAGE_SIZE))
884 num_entries = count *
885 AMDGPU_GPU_PAGES_IN_CPU_PAGE;
890 params.pages_addr = pages_addr;
892 addr = pages_addr[cursor.start >> PAGE_SHIFT];
893 params.pages_addr = NULL;
896 } else if (flags & (AMDGPU_PTE_VALID | AMDGPU_PTE_PRT)) {
897 addr = vram_base + cursor.start;
902 tmp = start + num_entries;
903 r = amdgpu_vm_ptes_update(¶ms, start, tmp, addr, flags);
907 amdgpu_res_next(&cursor, num_entries * AMDGPU_GPU_PAGE_SIZE);
911 r = vm->update_funcs->commit(¶ms, fence);
913 if (flush_tlb || params.table_freed) {
915 if (fence && *fence &&
916 !dma_fence_add_callback(*fence, &tlb_cb->cb,
917 amdgpu_vm_tlb_seq_cb)) {
918 dma_fence_put(vm->last_tlb_flush);
919 vm->last_tlb_flush = dma_fence_get(*fence);
921 amdgpu_vm_tlb_seq_cb(NULL, &tlb_cb->cb);
930 amdgpu_vm_eviction_unlock(vm);
935 void amdgpu_vm_get_memory(struct amdgpu_vm *vm, uint64_t *vram_mem,
936 uint64_t *gtt_mem, uint64_t *cpu_mem)
938 struct amdgpu_bo_va *bo_va, *tmp;
940 spin_lock(&vm->status_lock);
941 list_for_each_entry_safe(bo_va, tmp, &vm->idle, base.vm_status) {
944 amdgpu_bo_get_memory(bo_va->base.bo, vram_mem,
947 list_for_each_entry_safe(bo_va, tmp, &vm->evicted, base.vm_status) {
950 amdgpu_bo_get_memory(bo_va->base.bo, vram_mem,
953 list_for_each_entry_safe(bo_va, tmp, &vm->relocated, base.vm_status) {
956 amdgpu_bo_get_memory(bo_va->base.bo, vram_mem,
959 list_for_each_entry_safe(bo_va, tmp, &vm->moved, base.vm_status) {
962 amdgpu_bo_get_memory(bo_va->base.bo, vram_mem,
965 list_for_each_entry_safe(bo_va, tmp, &vm->invalidated, base.vm_status) {
968 amdgpu_bo_get_memory(bo_va->base.bo, vram_mem,
971 list_for_each_entry_safe(bo_va, tmp, &vm->done, base.vm_status) {
974 amdgpu_bo_get_memory(bo_va->base.bo, vram_mem,
977 spin_unlock(&vm->status_lock);
980 * amdgpu_vm_bo_update - update all BO mappings in the vm page table
982 * @adev: amdgpu_device pointer
983 * @bo_va: requested BO and VM object
984 * @clear: if true clear the entries
986 * Fill in the page table entries for @bo_va.
989 * 0 for success, -EINVAL for failure.
991 int amdgpu_vm_bo_update(struct amdgpu_device *adev, struct amdgpu_bo_va *bo_va,
994 struct amdgpu_bo *bo = bo_va->base.bo;
995 struct amdgpu_vm *vm = bo_va->base.vm;
996 struct amdgpu_bo_va_mapping *mapping;
997 dma_addr_t *pages_addr = NULL;
998 struct ttm_resource *mem;
999 struct dma_fence **last_update;
1000 bool flush_tlb = clear;
1001 struct dma_resv *resv;
1008 resv = vm->root.bo->tbo.base.resv;
1010 struct drm_gem_object *obj = &bo->tbo.base;
1012 resv = bo->tbo.base.resv;
1013 if (obj->import_attach && bo_va->is_xgmi) {
1014 struct dma_buf *dma_buf = obj->import_attach->dmabuf;
1015 struct drm_gem_object *gobj = dma_buf->priv;
1016 struct amdgpu_bo *abo = gem_to_amdgpu_bo(gobj);
1018 if (abo->tbo.resource->mem_type == TTM_PL_VRAM)
1019 bo = gem_to_amdgpu_bo(gobj);
1021 mem = bo->tbo.resource;
1022 if (mem->mem_type == TTM_PL_TT ||
1023 mem->mem_type == AMDGPU_PL_PREEMPT)
1024 pages_addr = bo->tbo.ttm->dma_address;
1028 struct amdgpu_device *bo_adev;
1030 flags = amdgpu_ttm_tt_pte_flags(adev, bo->tbo.ttm, mem);
1032 if (amdgpu_bo_encrypted(bo))
1033 flags |= AMDGPU_PTE_TMZ;
1035 bo_adev = amdgpu_ttm_adev(bo->tbo.bdev);
1036 vram_base = bo_adev->vm_manager.vram_base_offset;
1042 if (clear || (bo && bo->tbo.base.resv ==
1043 vm->root.bo->tbo.base.resv))
1044 last_update = &vm->last_update;
1046 last_update = &bo_va->last_pt_update;
1048 if (!clear && bo_va->base.moved) {
1050 list_splice_init(&bo_va->valids, &bo_va->invalids);
1052 } else if (bo_va->cleared != clear) {
1053 list_splice_init(&bo_va->valids, &bo_va->invalids);
1056 list_for_each_entry(mapping, &bo_va->invalids, list) {
1057 uint64_t update_flags = flags;
1059 /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here
1060 * but in case of something, we filter the flags in first place
1062 if (!(mapping->flags & AMDGPU_PTE_READABLE))
1063 update_flags &= ~AMDGPU_PTE_READABLE;
1064 if (!(mapping->flags & AMDGPU_PTE_WRITEABLE))
1065 update_flags &= ~AMDGPU_PTE_WRITEABLE;
1067 /* Apply ASIC specific mapping flags */
1068 amdgpu_gmc_get_vm_pte(adev, mapping, &update_flags);
1070 trace_amdgpu_vm_bo_update(mapping);
1072 r = amdgpu_vm_update_range(adev, vm, false, false, flush_tlb,
1073 resv, mapping->start, mapping->last,
1074 update_flags, mapping->offset,
1075 vram_base, mem, pages_addr,
1081 /* If the BO is not in its preferred location add it back to
1082 * the evicted list so that it gets validated again on the
1083 * next command submission.
1085 if (bo && bo->tbo.base.resv == vm->root.bo->tbo.base.resv) {
1086 uint32_t mem_type = bo->tbo.resource->mem_type;
1088 if (!(bo->preferred_domains &
1089 amdgpu_mem_type_to_domain(mem_type)))
1090 amdgpu_vm_bo_evicted(&bo_va->base);
1092 amdgpu_vm_bo_idle(&bo_va->base);
1094 amdgpu_vm_bo_done(&bo_va->base);
1097 list_splice_init(&bo_va->invalids, &bo_va->valids);
1098 bo_va->cleared = clear;
1099 bo_va->base.moved = false;
1101 if (trace_amdgpu_vm_bo_mapping_enabled()) {
1102 list_for_each_entry(mapping, &bo_va->valids, list)
1103 trace_amdgpu_vm_bo_mapping(mapping);
1110 * amdgpu_vm_update_prt_state - update the global PRT state
1112 * @adev: amdgpu_device pointer
1114 static void amdgpu_vm_update_prt_state(struct amdgpu_device *adev)
1116 unsigned long flags;
1119 spin_lock_irqsave(&adev->vm_manager.prt_lock, flags);
1120 enable = !!atomic_read(&adev->vm_manager.num_prt_users);
1121 adev->gmc.gmc_funcs->set_prt(adev, enable);
1122 spin_unlock_irqrestore(&adev->vm_manager.prt_lock, flags);
1126 * amdgpu_vm_prt_get - add a PRT user
1128 * @adev: amdgpu_device pointer
1130 static void amdgpu_vm_prt_get(struct amdgpu_device *adev)
1132 if (!adev->gmc.gmc_funcs->set_prt)
1135 if (atomic_inc_return(&adev->vm_manager.num_prt_users) == 1)
1136 amdgpu_vm_update_prt_state(adev);
1140 * amdgpu_vm_prt_put - drop a PRT user
1142 * @adev: amdgpu_device pointer
1144 static void amdgpu_vm_prt_put(struct amdgpu_device *adev)
1146 if (atomic_dec_return(&adev->vm_manager.num_prt_users) == 0)
1147 amdgpu_vm_update_prt_state(adev);
1151 * amdgpu_vm_prt_cb - callback for updating the PRT status
1153 * @fence: fence for the callback
1154 * @_cb: the callback function
1156 static void amdgpu_vm_prt_cb(struct dma_fence *fence, struct dma_fence_cb *_cb)
1158 struct amdgpu_prt_cb *cb = container_of(_cb, struct amdgpu_prt_cb, cb);
1160 amdgpu_vm_prt_put(cb->adev);
1165 * amdgpu_vm_add_prt_cb - add callback for updating the PRT status
1167 * @adev: amdgpu_device pointer
1168 * @fence: fence for the callback
1170 static void amdgpu_vm_add_prt_cb(struct amdgpu_device *adev,
1171 struct dma_fence *fence)
1173 struct amdgpu_prt_cb *cb;
1175 if (!adev->gmc.gmc_funcs->set_prt)
1178 cb = kmalloc(sizeof(struct amdgpu_prt_cb), GFP_KERNEL);
1180 /* Last resort when we are OOM */
1182 dma_fence_wait(fence, false);
1184 amdgpu_vm_prt_put(adev);
1187 if (!fence || dma_fence_add_callback(fence, &cb->cb,
1189 amdgpu_vm_prt_cb(fence, &cb->cb);
1194 * amdgpu_vm_free_mapping - free a mapping
1196 * @adev: amdgpu_device pointer
1198 * @mapping: mapping to be freed
1199 * @fence: fence of the unmap operation
1201 * Free a mapping and make sure we decrease the PRT usage count if applicable.
1203 static void amdgpu_vm_free_mapping(struct amdgpu_device *adev,
1204 struct amdgpu_vm *vm,
1205 struct amdgpu_bo_va_mapping *mapping,
1206 struct dma_fence *fence)
1208 if (mapping->flags & AMDGPU_PTE_PRT)
1209 amdgpu_vm_add_prt_cb(adev, fence);
1214 * amdgpu_vm_prt_fini - finish all prt mappings
1216 * @adev: amdgpu_device pointer
1219 * Register a cleanup callback to disable PRT support after VM dies.
1221 static void amdgpu_vm_prt_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1223 struct dma_resv *resv = vm->root.bo->tbo.base.resv;
1224 struct dma_resv_iter cursor;
1225 struct dma_fence *fence;
1227 dma_resv_for_each_fence(&cursor, resv, DMA_RESV_USAGE_BOOKKEEP, fence) {
1228 /* Add a callback for each fence in the reservation object */
1229 amdgpu_vm_prt_get(adev);
1230 amdgpu_vm_add_prt_cb(adev, fence);
1235 * amdgpu_vm_clear_freed - clear freed BOs in the PT
1237 * @adev: amdgpu_device pointer
1239 * @fence: optional resulting fence (unchanged if no work needed to be done
1240 * or if an error occurred)
1242 * Make sure all freed BOs are cleared in the PT.
1243 * PTs have to be reserved and mutex must be locked!
1249 int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
1250 struct amdgpu_vm *vm,
1251 struct dma_fence **fence)
1253 struct dma_resv *resv = vm->root.bo->tbo.base.resv;
1254 struct amdgpu_bo_va_mapping *mapping;
1255 uint64_t init_pte_value = 0;
1256 struct dma_fence *f = NULL;
1259 while (!list_empty(&vm->freed)) {
1260 mapping = list_first_entry(&vm->freed,
1261 struct amdgpu_bo_va_mapping, list);
1262 list_del(&mapping->list);
1264 if (vm->pte_support_ats &&
1265 mapping->start < AMDGPU_GMC_HOLE_START)
1266 init_pte_value = AMDGPU_PTE_DEFAULT_ATC;
1268 r = amdgpu_vm_update_range(adev, vm, false, false, true, resv,
1269 mapping->start, mapping->last,
1270 init_pte_value, 0, 0, NULL, NULL,
1272 amdgpu_vm_free_mapping(adev, vm, mapping, f);
1280 dma_fence_put(*fence);
1291 * amdgpu_vm_handle_moved - handle moved BOs in the PT
1293 * @adev: amdgpu_device pointer
1296 * Make sure all BOs which are moved are updated in the PTs.
1301 * PTs have to be reserved!
1303 int amdgpu_vm_handle_moved(struct amdgpu_device *adev,
1304 struct amdgpu_vm *vm)
1306 struct amdgpu_bo_va *bo_va;
1307 struct dma_resv *resv;
1311 spin_lock(&vm->status_lock);
1312 while (!list_empty(&vm->moved)) {
1313 bo_va = list_first_entry(&vm->moved, struct amdgpu_bo_va,
1315 spin_unlock(&vm->status_lock);
1317 /* Per VM BOs never need to bo cleared in the page tables */
1318 r = amdgpu_vm_bo_update(adev, bo_va, false);
1321 spin_lock(&vm->status_lock);
1324 while (!list_empty(&vm->invalidated)) {
1325 bo_va = list_first_entry(&vm->invalidated, struct amdgpu_bo_va,
1327 resv = bo_va->base.bo->tbo.base.resv;
1328 spin_unlock(&vm->status_lock);
1330 /* Try to reserve the BO to avoid clearing its ptes */
1331 if (!amdgpu_vm_debug && dma_resv_trylock(resv))
1333 /* Somebody else is using the BO right now */
1337 r = amdgpu_vm_bo_update(adev, bo_va, clear);
1342 dma_resv_unlock(resv);
1343 spin_lock(&vm->status_lock);
1345 spin_unlock(&vm->status_lock);
1351 * amdgpu_vm_bo_add - add a bo to a specific vm
1353 * @adev: amdgpu_device pointer
1355 * @bo: amdgpu buffer object
1357 * Add @bo into the requested vm.
1358 * Add @bo to the list of bos associated with the vm
1361 * Newly added bo_va or NULL for failure
1363 * Object has to be reserved!
1365 struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
1366 struct amdgpu_vm *vm,
1367 struct amdgpu_bo *bo)
1369 struct amdgpu_bo_va *bo_va;
1371 bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL);
1372 if (bo_va == NULL) {
1375 amdgpu_vm_bo_base_init(&bo_va->base, vm, bo);
1377 bo_va->ref_count = 1;
1378 INIT_LIST_HEAD(&bo_va->valids);
1379 INIT_LIST_HEAD(&bo_va->invalids);
1384 dma_resv_assert_held(bo->tbo.base.resv);
1385 if (amdgpu_dmabuf_is_xgmi_accessible(adev, bo)) {
1386 bo_va->is_xgmi = true;
1387 /* Power up XGMI if it can be potentially used */
1388 amdgpu_xgmi_set_pstate(adev, AMDGPU_XGMI_PSTATE_MAX_VEGA20);
1396 * amdgpu_vm_bo_insert_map - insert a new mapping
1398 * @adev: amdgpu_device pointer
1399 * @bo_va: bo_va to store the address
1400 * @mapping: the mapping to insert
1402 * Insert a new mapping into all structures.
1404 static void amdgpu_vm_bo_insert_map(struct amdgpu_device *adev,
1405 struct amdgpu_bo_va *bo_va,
1406 struct amdgpu_bo_va_mapping *mapping)
1408 struct amdgpu_vm *vm = bo_va->base.vm;
1409 struct amdgpu_bo *bo = bo_va->base.bo;
1411 mapping->bo_va = bo_va;
1412 list_add(&mapping->list, &bo_va->invalids);
1413 amdgpu_vm_it_insert(mapping, &vm->va);
1415 if (mapping->flags & AMDGPU_PTE_PRT)
1416 amdgpu_vm_prt_get(adev);
1418 if (bo && bo->tbo.base.resv == vm->root.bo->tbo.base.resv &&
1419 !bo_va->base.moved) {
1420 amdgpu_vm_bo_moved(&bo_va->base);
1422 trace_amdgpu_vm_bo_map(bo_va, mapping);
1426 * amdgpu_vm_bo_map - map bo inside a vm
1428 * @adev: amdgpu_device pointer
1429 * @bo_va: bo_va to store the address
1430 * @saddr: where to map the BO
1431 * @offset: requested offset in the BO
1432 * @size: BO size in bytes
1433 * @flags: attributes of pages (read/write/valid/etc.)
1435 * Add a mapping of the BO at the specefied addr into the VM.
1438 * 0 for success, error for failure.
1440 * Object has to be reserved and unreserved outside!
1442 int amdgpu_vm_bo_map(struct amdgpu_device *adev,
1443 struct amdgpu_bo_va *bo_va,
1444 uint64_t saddr, uint64_t offset,
1445 uint64_t size, uint64_t flags)
1447 struct amdgpu_bo_va_mapping *mapping, *tmp;
1448 struct amdgpu_bo *bo = bo_va->base.bo;
1449 struct amdgpu_vm *vm = bo_va->base.vm;
1452 /* validate the parameters */
1453 if (saddr & ~PAGE_MASK || offset & ~PAGE_MASK ||
1454 size == 0 || size & ~PAGE_MASK)
1457 /* make sure object fit at this offset */
1458 eaddr = saddr + size - 1;
1459 if (saddr >= eaddr ||
1460 (bo && offset + size > amdgpu_bo_size(bo)) ||
1461 (eaddr >= adev->vm_manager.max_pfn << AMDGPU_GPU_PAGE_SHIFT))
1464 saddr /= AMDGPU_GPU_PAGE_SIZE;
1465 eaddr /= AMDGPU_GPU_PAGE_SIZE;
1467 tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr);
1469 /* bo and tmp overlap, invalid addr */
1470 dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with "
1471 "0x%010Lx-0x%010Lx\n", bo, saddr, eaddr,
1472 tmp->start, tmp->last + 1);
1476 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
1480 mapping->start = saddr;
1481 mapping->last = eaddr;
1482 mapping->offset = offset;
1483 mapping->flags = flags;
1485 amdgpu_vm_bo_insert_map(adev, bo_va, mapping);
1491 * amdgpu_vm_bo_replace_map - map bo inside a vm, replacing existing mappings
1493 * @adev: amdgpu_device pointer
1494 * @bo_va: bo_va to store the address
1495 * @saddr: where to map the BO
1496 * @offset: requested offset in the BO
1497 * @size: BO size in bytes
1498 * @flags: attributes of pages (read/write/valid/etc.)
1500 * Add a mapping of the BO at the specefied addr into the VM. Replace existing
1501 * mappings as we do so.
1504 * 0 for success, error for failure.
1506 * Object has to be reserved and unreserved outside!
1508 int amdgpu_vm_bo_replace_map(struct amdgpu_device *adev,
1509 struct amdgpu_bo_va *bo_va,
1510 uint64_t saddr, uint64_t offset,
1511 uint64_t size, uint64_t flags)
1513 struct amdgpu_bo_va_mapping *mapping;
1514 struct amdgpu_bo *bo = bo_va->base.bo;
1518 /* validate the parameters */
1519 if (saddr & ~PAGE_MASK || offset & ~PAGE_MASK ||
1520 size == 0 || size & ~PAGE_MASK)
1523 /* make sure object fit at this offset */
1524 eaddr = saddr + size - 1;
1525 if (saddr >= eaddr ||
1526 (bo && offset + size > amdgpu_bo_size(bo)) ||
1527 (eaddr >= adev->vm_manager.max_pfn << AMDGPU_GPU_PAGE_SHIFT))
1530 /* Allocate all the needed memory */
1531 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
1535 r = amdgpu_vm_bo_clear_mappings(adev, bo_va->base.vm, saddr, size);
1541 saddr /= AMDGPU_GPU_PAGE_SIZE;
1542 eaddr /= AMDGPU_GPU_PAGE_SIZE;
1544 mapping->start = saddr;
1545 mapping->last = eaddr;
1546 mapping->offset = offset;
1547 mapping->flags = flags;
1549 amdgpu_vm_bo_insert_map(adev, bo_va, mapping);
1555 * amdgpu_vm_bo_unmap - remove bo mapping from vm
1557 * @adev: amdgpu_device pointer
1558 * @bo_va: bo_va to remove the address from
1559 * @saddr: where to the BO is mapped
1561 * Remove a mapping of the BO at the specefied addr from the VM.
1564 * 0 for success, error for failure.
1566 * Object has to be reserved and unreserved outside!
1568 int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
1569 struct amdgpu_bo_va *bo_va,
1572 struct amdgpu_bo_va_mapping *mapping;
1573 struct amdgpu_vm *vm = bo_va->base.vm;
1576 saddr /= AMDGPU_GPU_PAGE_SIZE;
1578 list_for_each_entry(mapping, &bo_va->valids, list) {
1579 if (mapping->start == saddr)
1583 if (&mapping->list == &bo_va->valids) {
1586 list_for_each_entry(mapping, &bo_va->invalids, list) {
1587 if (mapping->start == saddr)
1591 if (&mapping->list == &bo_va->invalids)
1595 list_del(&mapping->list);
1596 amdgpu_vm_it_remove(mapping, &vm->va);
1597 mapping->bo_va = NULL;
1598 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
1601 list_add(&mapping->list, &vm->freed);
1603 amdgpu_vm_free_mapping(adev, vm, mapping,
1604 bo_va->last_pt_update);
1610 * amdgpu_vm_bo_clear_mappings - remove all mappings in a specific range
1612 * @adev: amdgpu_device pointer
1613 * @vm: VM structure to use
1614 * @saddr: start of the range
1615 * @size: size of the range
1617 * Remove all mappings in a range, split them as appropriate.
1620 * 0 for success, error for failure.
1622 int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev,
1623 struct amdgpu_vm *vm,
1624 uint64_t saddr, uint64_t size)
1626 struct amdgpu_bo_va_mapping *before, *after, *tmp, *next;
1630 eaddr = saddr + size - 1;
1631 saddr /= AMDGPU_GPU_PAGE_SIZE;
1632 eaddr /= AMDGPU_GPU_PAGE_SIZE;
1634 /* Allocate all the needed memory */
1635 before = kzalloc(sizeof(*before), GFP_KERNEL);
1638 INIT_LIST_HEAD(&before->list);
1640 after = kzalloc(sizeof(*after), GFP_KERNEL);
1645 INIT_LIST_HEAD(&after->list);
1647 /* Now gather all removed mappings */
1648 tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr);
1650 /* Remember mapping split at the start */
1651 if (tmp->start < saddr) {
1652 before->start = tmp->start;
1653 before->last = saddr - 1;
1654 before->offset = tmp->offset;
1655 before->flags = tmp->flags;
1656 before->bo_va = tmp->bo_va;
1657 list_add(&before->list, &tmp->bo_va->invalids);
1660 /* Remember mapping split at the end */
1661 if (tmp->last > eaddr) {
1662 after->start = eaddr + 1;
1663 after->last = tmp->last;
1664 after->offset = tmp->offset;
1665 after->offset += (after->start - tmp->start) << PAGE_SHIFT;
1666 after->flags = tmp->flags;
1667 after->bo_va = tmp->bo_va;
1668 list_add(&after->list, &tmp->bo_va->invalids);
1671 list_del(&tmp->list);
1672 list_add(&tmp->list, &removed);
1674 tmp = amdgpu_vm_it_iter_next(tmp, saddr, eaddr);
1677 /* And free them up */
1678 list_for_each_entry_safe(tmp, next, &removed, list) {
1679 amdgpu_vm_it_remove(tmp, &vm->va);
1680 list_del(&tmp->list);
1682 if (tmp->start < saddr)
1684 if (tmp->last > eaddr)
1688 list_add(&tmp->list, &vm->freed);
1689 trace_amdgpu_vm_bo_unmap(NULL, tmp);
1692 /* Insert partial mapping before the range */
1693 if (!list_empty(&before->list)) {
1694 amdgpu_vm_it_insert(before, &vm->va);
1695 if (before->flags & AMDGPU_PTE_PRT)
1696 amdgpu_vm_prt_get(adev);
1701 /* Insert partial mapping after the range */
1702 if (!list_empty(&after->list)) {
1703 amdgpu_vm_it_insert(after, &vm->va);
1704 if (after->flags & AMDGPU_PTE_PRT)
1705 amdgpu_vm_prt_get(adev);
1714 * amdgpu_vm_bo_lookup_mapping - find mapping by address
1716 * @vm: the requested VM
1717 * @addr: the address
1719 * Find a mapping by it's address.
1722 * The amdgpu_bo_va_mapping matching for addr or NULL
1725 struct amdgpu_bo_va_mapping *amdgpu_vm_bo_lookup_mapping(struct amdgpu_vm *vm,
1728 return amdgpu_vm_it_iter_first(&vm->va, addr, addr);
1732 * amdgpu_vm_bo_trace_cs - trace all reserved mappings
1734 * @vm: the requested vm
1735 * @ticket: CS ticket
1737 * Trace all mappings of BOs reserved during a command submission.
1739 void amdgpu_vm_bo_trace_cs(struct amdgpu_vm *vm, struct ww_acquire_ctx *ticket)
1741 struct amdgpu_bo_va_mapping *mapping;
1743 if (!trace_amdgpu_vm_bo_cs_enabled())
1746 for (mapping = amdgpu_vm_it_iter_first(&vm->va, 0, U64_MAX); mapping;
1747 mapping = amdgpu_vm_it_iter_next(mapping, 0, U64_MAX)) {
1748 if (mapping->bo_va && mapping->bo_va->base.bo) {
1749 struct amdgpu_bo *bo;
1751 bo = mapping->bo_va->base.bo;
1752 if (dma_resv_locking_ctx(bo->tbo.base.resv) !=
1757 trace_amdgpu_vm_bo_cs(mapping);
1762 * amdgpu_vm_bo_del - remove a bo from a specific vm
1764 * @adev: amdgpu_device pointer
1765 * @bo_va: requested bo_va
1767 * Remove @bo_va->bo from the requested vm.
1769 * Object have to be reserved!
1771 void amdgpu_vm_bo_del(struct amdgpu_device *adev,
1772 struct amdgpu_bo_va *bo_va)
1774 struct amdgpu_bo_va_mapping *mapping, *next;
1775 struct amdgpu_bo *bo = bo_va->base.bo;
1776 struct amdgpu_vm *vm = bo_va->base.vm;
1777 struct amdgpu_vm_bo_base **base;
1779 dma_resv_assert_held(vm->root.bo->tbo.base.resv);
1782 dma_resv_assert_held(bo->tbo.base.resv);
1783 if (bo->tbo.base.resv == vm->root.bo->tbo.base.resv)
1784 ttm_bo_set_bulk_move(&bo->tbo, NULL);
1786 for (base = &bo_va->base.bo->vm_bo; *base;
1787 base = &(*base)->next) {
1788 if (*base != &bo_va->base)
1791 *base = bo_va->base.next;
1796 spin_lock(&vm->status_lock);
1797 list_del(&bo_va->base.vm_status);
1798 spin_unlock(&vm->status_lock);
1800 list_for_each_entry_safe(mapping, next, &bo_va->valids, list) {
1801 list_del(&mapping->list);
1802 amdgpu_vm_it_remove(mapping, &vm->va);
1803 mapping->bo_va = NULL;
1804 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
1805 list_add(&mapping->list, &vm->freed);
1807 list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) {
1808 list_del(&mapping->list);
1809 amdgpu_vm_it_remove(mapping, &vm->va);
1810 amdgpu_vm_free_mapping(adev, vm, mapping,
1811 bo_va->last_pt_update);
1814 dma_fence_put(bo_va->last_pt_update);
1816 if (bo && bo_va->is_xgmi)
1817 amdgpu_xgmi_set_pstate(adev, AMDGPU_XGMI_PSTATE_MIN);
1823 * amdgpu_vm_evictable - check if we can evict a VM
1825 * @bo: A page table of the VM.
1827 * Check if it is possible to evict a VM.
1829 bool amdgpu_vm_evictable(struct amdgpu_bo *bo)
1831 struct amdgpu_vm_bo_base *bo_base = bo->vm_bo;
1833 /* Page tables of a destroyed VM can go away immediately */
1834 if (!bo_base || !bo_base->vm)
1837 /* Don't evict VM page tables while they are busy */
1838 if (!dma_resv_test_signaled(bo->tbo.base.resv, DMA_RESV_USAGE_BOOKKEEP))
1841 /* Try to block ongoing updates */
1842 if (!amdgpu_vm_eviction_trylock(bo_base->vm))
1845 /* Don't evict VM page tables while they are updated */
1846 if (!dma_fence_is_signaled(bo_base->vm->last_unlocked)) {
1847 amdgpu_vm_eviction_unlock(bo_base->vm);
1851 bo_base->vm->evicting = true;
1852 amdgpu_vm_eviction_unlock(bo_base->vm);
1857 * amdgpu_vm_bo_invalidate - mark the bo as invalid
1859 * @adev: amdgpu_device pointer
1860 * @bo: amdgpu buffer object
1861 * @evicted: is the BO evicted
1863 * Mark @bo as invalid.
1865 void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
1866 struct amdgpu_bo *bo, bool evicted)
1868 struct amdgpu_vm_bo_base *bo_base;
1870 /* shadow bo doesn't have bo base, its validation needs its parent */
1871 if (bo->parent && (amdgpu_bo_shadowed(bo->parent) == bo))
1874 for (bo_base = bo->vm_bo; bo_base; bo_base = bo_base->next) {
1875 struct amdgpu_vm *vm = bo_base->vm;
1877 if (evicted && bo->tbo.base.resv == vm->root.bo->tbo.base.resv) {
1878 amdgpu_vm_bo_evicted(bo_base);
1884 bo_base->moved = true;
1886 if (bo->tbo.type == ttm_bo_type_kernel)
1887 amdgpu_vm_bo_relocated(bo_base);
1888 else if (bo->tbo.base.resv == vm->root.bo->tbo.base.resv)
1889 amdgpu_vm_bo_moved(bo_base);
1891 amdgpu_vm_bo_invalidated(bo_base);
1896 * amdgpu_vm_get_block_size - calculate VM page table size as power of two
1901 * VM page table as power of two
1903 static uint32_t amdgpu_vm_get_block_size(uint64_t vm_size)
1905 /* Total bits covered by PD + PTs */
1906 unsigned bits = ilog2(vm_size) + 18;
1908 /* Make sure the PD is 4K in size up to 8GB address space.
1909 Above that split equal between PD and PTs */
1913 return ((bits + 3) / 2);
1917 * amdgpu_vm_adjust_size - adjust vm size, block size and fragment size
1919 * @adev: amdgpu_device pointer
1920 * @min_vm_size: the minimum vm size in GB if it's set auto
1921 * @fragment_size_default: Default PTE fragment size
1922 * @max_level: max VMPT level
1923 * @max_bits: max address space size in bits
1926 void amdgpu_vm_adjust_size(struct amdgpu_device *adev, uint32_t min_vm_size,
1927 uint32_t fragment_size_default, unsigned max_level,
1930 unsigned int max_size = 1 << (max_bits - 30);
1931 unsigned int vm_size;
1934 /* adjust vm size first */
1935 if (amdgpu_vm_size != -1) {
1936 vm_size = amdgpu_vm_size;
1937 if (vm_size > max_size) {
1938 dev_warn(adev->dev, "VM size (%d) too large, max is %u GB\n",
1939 amdgpu_vm_size, max_size);
1944 unsigned int phys_ram_gb;
1946 /* Optimal VM size depends on the amount of physical
1947 * RAM available. Underlying requirements and
1950 * - Need to map system memory and VRAM from all GPUs
1951 * - VRAM from other GPUs not known here
1952 * - Assume VRAM <= system memory
1953 * - On GFX8 and older, VM space can be segmented for
1955 * - Need to allow room for fragmentation, guard pages etc.
1957 * This adds up to a rough guess of system memory x3.
1958 * Round up to power of two to maximize the available
1959 * VM size with the given page table size.
1962 phys_ram_gb = ((uint64_t)si.totalram * si.mem_unit +
1963 (1 << 30) - 1) >> 30;
1964 vm_size = roundup_pow_of_two(
1965 min(max(phys_ram_gb * 3, min_vm_size), max_size));
1968 adev->vm_manager.max_pfn = (uint64_t)vm_size << 18;
1970 tmp = roundup_pow_of_two(adev->vm_manager.max_pfn);
1971 if (amdgpu_vm_block_size != -1)
1972 tmp >>= amdgpu_vm_block_size - 9;
1973 tmp = DIV_ROUND_UP(fls64(tmp) - 1, 9) - 1;
1974 adev->vm_manager.num_level = min(max_level, (unsigned)tmp);
1975 switch (adev->vm_manager.num_level) {
1977 adev->vm_manager.root_level = AMDGPU_VM_PDB2;
1980 adev->vm_manager.root_level = AMDGPU_VM_PDB1;
1983 adev->vm_manager.root_level = AMDGPU_VM_PDB0;
1986 dev_err(adev->dev, "VMPT only supports 2~4+1 levels\n");
1988 /* block size depends on vm size and hw setup*/
1989 if (amdgpu_vm_block_size != -1)
1990 adev->vm_manager.block_size =
1991 min((unsigned)amdgpu_vm_block_size, max_bits
1992 - AMDGPU_GPU_PAGE_SHIFT
1993 - 9 * adev->vm_manager.num_level);
1994 else if (adev->vm_manager.num_level > 1)
1995 adev->vm_manager.block_size = 9;
1997 adev->vm_manager.block_size = amdgpu_vm_get_block_size(tmp);
1999 if (amdgpu_vm_fragment_size == -1)
2000 adev->vm_manager.fragment_size = fragment_size_default;
2002 adev->vm_manager.fragment_size = amdgpu_vm_fragment_size;
2004 DRM_INFO("vm size is %u GB, %u levels, block size is %u-bit, fragment size is %u-bit\n",
2005 vm_size, adev->vm_manager.num_level + 1,
2006 adev->vm_manager.block_size,
2007 adev->vm_manager.fragment_size);
2011 * amdgpu_vm_wait_idle - wait for the VM to become idle
2013 * @vm: VM object to wait for
2014 * @timeout: timeout to wait for VM to become idle
2016 long amdgpu_vm_wait_idle(struct amdgpu_vm *vm, long timeout)
2018 timeout = dma_resv_wait_timeout(vm->root.bo->tbo.base.resv,
2019 DMA_RESV_USAGE_BOOKKEEP,
2024 return dma_fence_wait_timeout(vm->last_unlocked, true, timeout);
2028 * amdgpu_vm_init - initialize a vm instance
2030 * @adev: amdgpu_device pointer
2036 * 0 for success, error for failure.
2038 int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm)
2040 struct amdgpu_bo *root_bo;
2041 struct amdgpu_bo_vm *root;
2044 vm->va = RB_ROOT_CACHED;
2045 for (i = 0; i < AMDGPU_MAX_VMHUBS; i++)
2046 vm->reserved_vmid[i] = NULL;
2047 INIT_LIST_HEAD(&vm->evicted);
2048 INIT_LIST_HEAD(&vm->relocated);
2049 INIT_LIST_HEAD(&vm->moved);
2050 INIT_LIST_HEAD(&vm->idle);
2051 INIT_LIST_HEAD(&vm->invalidated);
2052 spin_lock_init(&vm->status_lock);
2053 INIT_LIST_HEAD(&vm->freed);
2054 INIT_LIST_HEAD(&vm->done);
2055 INIT_LIST_HEAD(&vm->pt_freed);
2056 INIT_WORK(&vm->pt_free_work, amdgpu_vm_pt_free_work);
2058 /* create scheduler entities for page table updates */
2059 r = drm_sched_entity_init(&vm->immediate, DRM_SCHED_PRIORITY_NORMAL,
2060 adev->vm_manager.vm_pte_scheds,
2061 adev->vm_manager.vm_pte_num_scheds, NULL);
2065 r = drm_sched_entity_init(&vm->delayed, DRM_SCHED_PRIORITY_NORMAL,
2066 adev->vm_manager.vm_pte_scheds,
2067 adev->vm_manager.vm_pte_num_scheds, NULL);
2069 goto error_free_immediate;
2071 vm->pte_support_ats = false;
2072 vm->is_compute_context = false;
2074 vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode &
2075 AMDGPU_VM_USE_CPU_FOR_GFX);
2077 DRM_DEBUG_DRIVER("VM update mode is %s\n",
2078 vm->use_cpu_for_update ? "CPU" : "SDMA");
2079 WARN_ONCE((vm->use_cpu_for_update &&
2080 !amdgpu_gmc_vram_full_visible(&adev->gmc)),
2081 "CPU update of VM recommended only for large BAR system\n");
2083 if (vm->use_cpu_for_update)
2084 vm->update_funcs = &amdgpu_vm_cpu_funcs;
2086 vm->update_funcs = &amdgpu_vm_sdma_funcs;
2087 vm->last_update = NULL;
2088 vm->last_unlocked = dma_fence_get_stub();
2089 vm->last_tlb_flush = dma_fence_get_stub();
2091 mutex_init(&vm->eviction_lock);
2092 vm->evicting = false;
2094 r = amdgpu_vm_pt_create(adev, vm, adev->vm_manager.root_level,
2097 goto error_free_delayed;
2098 root_bo = &root->bo;
2099 r = amdgpu_bo_reserve(root_bo, true);
2101 goto error_free_root;
2103 r = dma_resv_reserve_fences(root_bo->tbo.base.resv, 1);
2105 goto error_unreserve;
2107 amdgpu_vm_bo_base_init(&vm->root, vm, root_bo);
2109 r = amdgpu_vm_pt_clear(adev, vm, root, false);
2111 goto error_unreserve;
2113 amdgpu_bo_unreserve(vm->root.bo);
2115 INIT_KFIFO(vm->faults);
2120 amdgpu_bo_unreserve(vm->root.bo);
2123 amdgpu_bo_unref(&root->shadow);
2124 amdgpu_bo_unref(&root_bo);
2128 dma_fence_put(vm->last_tlb_flush);
2129 dma_fence_put(vm->last_unlocked);
2130 drm_sched_entity_destroy(&vm->delayed);
2132 error_free_immediate:
2133 drm_sched_entity_destroy(&vm->immediate);
2139 * amdgpu_vm_make_compute - Turn a GFX VM into a compute VM
2141 * @adev: amdgpu_device pointer
2144 * This only works on GFX VMs that don't have any BOs added and no
2145 * page tables allocated yet.
2147 * Changes the following VM parameters:
2148 * - use_cpu_for_update
2149 * - pte_supports_ats
2151 * Reinitializes the page directory to reflect the changed ATS
2155 * 0 for success, -errno for errors.
2157 int amdgpu_vm_make_compute(struct amdgpu_device *adev, struct amdgpu_vm *vm)
2159 bool pte_support_ats = (adev->asic_type == CHIP_RAVEN);
2162 r = amdgpu_bo_reserve(vm->root.bo, true);
2167 if (!amdgpu_vm_pt_is_root_clean(adev, vm)) {
2172 /* Check if PD needs to be reinitialized and do it before
2173 * changing any other state, in case it fails.
2175 if (pte_support_ats != vm->pte_support_ats) {
2176 vm->pte_support_ats = pte_support_ats;
2177 r = amdgpu_vm_pt_clear(adev, vm, to_amdgpu_bo_vm(vm->root.bo),
2183 /* Update VM state */
2184 vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode &
2185 AMDGPU_VM_USE_CPU_FOR_COMPUTE);
2186 DRM_DEBUG_DRIVER("VM update mode is %s\n",
2187 vm->use_cpu_for_update ? "CPU" : "SDMA");
2188 WARN_ONCE((vm->use_cpu_for_update &&
2189 !amdgpu_gmc_vram_full_visible(&adev->gmc)),
2190 "CPU update of VM recommended only for large BAR system\n");
2192 if (vm->use_cpu_for_update) {
2193 /* Sync with last SDMA update/clear before switching to CPU */
2194 r = amdgpu_bo_sync_wait(vm->root.bo,
2195 AMDGPU_FENCE_OWNER_UNDEFINED, true);
2199 vm->update_funcs = &amdgpu_vm_cpu_funcs;
2201 vm->update_funcs = &amdgpu_vm_sdma_funcs;
2204 * Make sure root PD gets mapped. As vm_update_mode could be changed
2205 * when turning a GFX VM into a compute VM.
2207 r = vm->update_funcs->map_table(to_amdgpu_bo_vm(vm->root.bo));
2211 dma_fence_put(vm->last_update);
2212 vm->last_update = NULL;
2213 vm->is_compute_context = true;
2215 /* Free the shadow bo for compute VM */
2216 amdgpu_bo_unref(&to_amdgpu_bo_vm(vm->root.bo)->shadow);
2221 amdgpu_bo_unreserve(vm->root.bo);
2226 * amdgpu_vm_release_compute - release a compute vm
2227 * @adev: amdgpu_device pointer
2228 * @vm: a vm turned into compute vm by calling amdgpu_vm_make_compute
2230 * This is a correspondant of amdgpu_vm_make_compute. It decouples compute
2231 * pasid from vm. Compute should stop use of vm after this call.
2233 void amdgpu_vm_release_compute(struct amdgpu_device *adev, struct amdgpu_vm *vm)
2235 amdgpu_vm_set_pasid(adev, vm, 0);
2236 vm->is_compute_context = false;
2240 * amdgpu_vm_fini - tear down a vm instance
2242 * @adev: amdgpu_device pointer
2246 * Unbind the VM and remove all bos from the vm bo list
2248 void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
2250 struct amdgpu_bo_va_mapping *mapping, *tmp;
2251 bool prt_fini_needed = !!adev->gmc.gmc_funcs->set_prt;
2252 struct amdgpu_bo *root;
2253 unsigned long flags;
2256 amdgpu_amdkfd_gpuvm_destroy_cb(adev, vm);
2258 flush_work(&vm->pt_free_work);
2260 root = amdgpu_bo_ref(vm->root.bo);
2261 amdgpu_bo_reserve(root, true);
2262 amdgpu_vm_set_pasid(adev, vm, 0);
2263 dma_fence_wait(vm->last_unlocked, false);
2264 dma_fence_put(vm->last_unlocked);
2265 dma_fence_wait(vm->last_tlb_flush, false);
2266 /* Make sure that all fence callbacks have completed */
2267 spin_lock_irqsave(vm->last_tlb_flush->lock, flags);
2268 spin_unlock_irqrestore(vm->last_tlb_flush->lock, flags);
2269 dma_fence_put(vm->last_tlb_flush);
2271 list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
2272 if (mapping->flags & AMDGPU_PTE_PRT && prt_fini_needed) {
2273 amdgpu_vm_prt_fini(adev, vm);
2274 prt_fini_needed = false;
2277 list_del(&mapping->list);
2278 amdgpu_vm_free_mapping(adev, vm, mapping, NULL);
2281 amdgpu_vm_pt_free_root(adev, vm);
2282 amdgpu_bo_unreserve(root);
2283 amdgpu_bo_unref(&root);
2284 WARN_ON(vm->root.bo);
2286 drm_sched_entity_destroy(&vm->immediate);
2287 drm_sched_entity_destroy(&vm->delayed);
2289 if (!RB_EMPTY_ROOT(&vm->va.rb_root)) {
2290 dev_err(adev->dev, "still active bo inside vm\n");
2292 rbtree_postorder_for_each_entry_safe(mapping, tmp,
2293 &vm->va.rb_root, rb) {
2294 /* Don't remove the mapping here, we don't want to trigger a
2295 * rebalance and the tree is about to be destroyed anyway.
2297 list_del(&mapping->list);
2301 dma_fence_put(vm->last_update);
2302 for (i = 0; i < AMDGPU_MAX_VMHUBS; i++)
2303 amdgpu_vmid_free_reserved(adev, vm, i);
2307 * amdgpu_vm_manager_init - init the VM manager
2309 * @adev: amdgpu_device pointer
2311 * Initialize the VM manager structures
2313 void amdgpu_vm_manager_init(struct amdgpu_device *adev)
2317 /* Concurrent flushes are only possible starting with Vega10 and
2318 * are broken on Navi10 and Navi14.
2320 adev->vm_manager.concurrent_flush = !(adev->asic_type < CHIP_VEGA10 ||
2321 adev->asic_type == CHIP_NAVI10 ||
2322 adev->asic_type == CHIP_NAVI14);
2323 amdgpu_vmid_mgr_init(adev);
2325 adev->vm_manager.fence_context =
2326 dma_fence_context_alloc(AMDGPU_MAX_RINGS);
2327 for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
2328 adev->vm_manager.seqno[i] = 0;
2330 spin_lock_init(&adev->vm_manager.prt_lock);
2331 atomic_set(&adev->vm_manager.num_prt_users, 0);
2333 /* If not overridden by the user, by default, only in large BAR systems
2334 * Compute VM tables will be updated by CPU
2336 #ifdef CONFIG_X86_64
2337 if (amdgpu_vm_update_mode == -1) {
2338 /* For asic with VF MMIO access protection
2339 * avoid using CPU for VM table updates
2341 if (amdgpu_gmc_vram_full_visible(&adev->gmc) &&
2342 !amdgpu_sriov_vf_mmio_access_protection(adev))
2343 adev->vm_manager.vm_update_mode =
2344 AMDGPU_VM_USE_CPU_FOR_COMPUTE;
2346 adev->vm_manager.vm_update_mode = 0;
2348 adev->vm_manager.vm_update_mode = amdgpu_vm_update_mode;
2350 adev->vm_manager.vm_update_mode = 0;
2353 xa_init_flags(&adev->vm_manager.pasids, XA_FLAGS_LOCK_IRQ);
2357 * amdgpu_vm_manager_fini - cleanup VM manager
2359 * @adev: amdgpu_device pointer
2361 * Cleanup the VM manager and free resources.
2363 void amdgpu_vm_manager_fini(struct amdgpu_device *adev)
2365 WARN_ON(!xa_empty(&adev->vm_manager.pasids));
2366 xa_destroy(&adev->vm_manager.pasids);
2368 amdgpu_vmid_mgr_fini(adev);
2372 * amdgpu_vm_ioctl - Manages VMID reservation for vm hubs.
2374 * @dev: drm device pointer
2375 * @data: drm_amdgpu_vm
2376 * @filp: drm file pointer
2379 * 0 for success, -errno for errors.
2381 int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
2383 union drm_amdgpu_vm *args = data;
2384 struct amdgpu_device *adev = drm_to_adev(dev);
2385 struct amdgpu_fpriv *fpriv = filp->driver_priv;
2386 long timeout = msecs_to_jiffies(2000);
2389 switch (args->in.op) {
2390 case AMDGPU_VM_OP_RESERVE_VMID:
2391 /* We only have requirement to reserve vmid from gfxhub */
2392 r = amdgpu_vmid_alloc_reserved(adev, &fpriv->vm,
2397 case AMDGPU_VM_OP_UNRESERVE_VMID:
2398 if (amdgpu_sriov_runtime(adev))
2399 timeout = 8 * timeout;
2401 /* Wait vm idle to make sure the vmid set in SPM_VMID is
2402 * not referenced anymore.
2404 r = amdgpu_bo_reserve(fpriv->vm.root.bo, true);
2408 r = amdgpu_vm_wait_idle(&fpriv->vm, timeout);
2412 amdgpu_bo_unreserve(fpriv->vm.root.bo);
2413 amdgpu_vmid_free_reserved(adev, &fpriv->vm, AMDGPU_GFXHUB_0);
2423 * amdgpu_vm_get_task_info - Extracts task info for a PASID.
2425 * @adev: drm device pointer
2426 * @pasid: PASID identifier for VM
2427 * @task_info: task_info to fill.
2429 void amdgpu_vm_get_task_info(struct amdgpu_device *adev, u32 pasid,
2430 struct amdgpu_task_info *task_info)
2432 struct amdgpu_vm *vm;
2433 unsigned long flags;
2435 xa_lock_irqsave(&adev->vm_manager.pasids, flags);
2437 vm = xa_load(&adev->vm_manager.pasids, pasid);
2439 *task_info = vm->task_info;
2441 xa_unlock_irqrestore(&adev->vm_manager.pasids, flags);
2445 * amdgpu_vm_set_task_info - Sets VMs task info.
2447 * @vm: vm for which to set the info
2449 void amdgpu_vm_set_task_info(struct amdgpu_vm *vm)
2451 if (vm->task_info.pid)
2454 vm->task_info.pid = current->pid;
2455 get_task_comm(vm->task_info.task_name, current);
2457 if (current->group_leader->mm != current->mm)
2460 vm->task_info.tgid = current->group_leader->pid;
2461 get_task_comm(vm->task_info.process_name, current->group_leader);
2465 * amdgpu_vm_handle_fault - graceful handling of VM faults.
2466 * @adev: amdgpu device pointer
2467 * @pasid: PASID of the VM
2468 * @addr: Address of the fault
2469 * @write_fault: true is write fault, false is read fault
2471 * Try to gracefully handle a VM fault. Return true if the fault was handled and
2472 * shouldn't be reported any more.
2474 bool amdgpu_vm_handle_fault(struct amdgpu_device *adev, u32 pasid,
2475 uint64_t addr, bool write_fault)
2477 bool is_compute_context = false;
2478 struct amdgpu_bo *root;
2479 unsigned long irqflags;
2480 uint64_t value, flags;
2481 struct amdgpu_vm *vm;
2484 xa_lock_irqsave(&adev->vm_manager.pasids, irqflags);
2485 vm = xa_load(&adev->vm_manager.pasids, pasid);
2487 root = amdgpu_bo_ref(vm->root.bo);
2488 is_compute_context = vm->is_compute_context;
2492 xa_unlock_irqrestore(&adev->vm_manager.pasids, irqflags);
2497 addr /= AMDGPU_GPU_PAGE_SIZE;
2499 if (is_compute_context &&
2500 !svm_range_restore_pages(adev, pasid, addr, write_fault)) {
2501 amdgpu_bo_unref(&root);
2505 r = amdgpu_bo_reserve(root, true);
2509 /* Double check that the VM still exists */
2510 xa_lock_irqsave(&adev->vm_manager.pasids, irqflags);
2511 vm = xa_load(&adev->vm_manager.pasids, pasid);
2512 if (vm && vm->root.bo != root)
2514 xa_unlock_irqrestore(&adev->vm_manager.pasids, irqflags);
2518 flags = AMDGPU_PTE_VALID | AMDGPU_PTE_SNOOPED |
2521 if (is_compute_context) {
2522 /* Intentionally setting invalid PTE flag
2523 * combination to force a no-retry-fault
2525 flags = AMDGPU_PTE_SNOOPED | AMDGPU_PTE_PRT;
2527 } else if (amdgpu_vm_fault_stop == AMDGPU_VM_FAULT_STOP_NEVER) {
2528 /* Redirect the access to the dummy page */
2529 value = adev->dummy_page_addr;
2530 flags |= AMDGPU_PTE_EXECUTABLE | AMDGPU_PTE_READABLE |
2531 AMDGPU_PTE_WRITEABLE;
2534 /* Let the hw retry silently on the PTE */
2538 r = dma_resv_reserve_fences(root->tbo.base.resv, 1);
2540 pr_debug("failed %d to reserve fence slot\n", r);
2544 r = amdgpu_vm_update_range(adev, vm, true, false, false, NULL, addr,
2545 addr, flags, value, 0, NULL, NULL, NULL);
2549 r = amdgpu_vm_update_pdes(adev, vm, true);
2552 amdgpu_bo_unreserve(root);
2554 DRM_ERROR("Can't handle page fault (%d)\n", r);
2557 amdgpu_bo_unref(&root);
2562 #if defined(CONFIG_DEBUG_FS)
2564 * amdgpu_debugfs_vm_bo_info - print BO info for the VM
2566 * @vm: Requested VM for printing BO info
2569 * Print BO information in debugfs file for the VM
2571 void amdgpu_debugfs_vm_bo_info(struct amdgpu_vm *vm, struct seq_file *m)
2573 struct amdgpu_bo_va *bo_va, *tmp;
2575 u64 total_evicted = 0;
2576 u64 total_relocated = 0;
2577 u64 total_moved = 0;
2578 u64 total_invalidated = 0;
2580 unsigned int total_idle_objs = 0;
2581 unsigned int total_evicted_objs = 0;
2582 unsigned int total_relocated_objs = 0;
2583 unsigned int total_moved_objs = 0;
2584 unsigned int total_invalidated_objs = 0;
2585 unsigned int total_done_objs = 0;
2586 unsigned int id = 0;
2588 spin_lock(&vm->status_lock);
2589 seq_puts(m, "\tIdle BOs:\n");
2590 list_for_each_entry_safe(bo_va, tmp, &vm->idle, base.vm_status) {
2591 if (!bo_va->base.bo)
2593 total_idle += amdgpu_bo_print_info(id++, bo_va->base.bo, m);
2595 total_idle_objs = id;
2598 seq_puts(m, "\tEvicted BOs:\n");
2599 list_for_each_entry_safe(bo_va, tmp, &vm->evicted, base.vm_status) {
2600 if (!bo_va->base.bo)
2602 total_evicted += amdgpu_bo_print_info(id++, bo_va->base.bo, m);
2604 total_evicted_objs = id;
2607 seq_puts(m, "\tRelocated BOs:\n");
2608 list_for_each_entry_safe(bo_va, tmp, &vm->relocated, base.vm_status) {
2609 if (!bo_va->base.bo)
2611 total_relocated += amdgpu_bo_print_info(id++, bo_va->base.bo, m);
2613 total_relocated_objs = id;
2616 seq_puts(m, "\tMoved BOs:\n");
2617 list_for_each_entry_safe(bo_va, tmp, &vm->moved, base.vm_status) {
2618 if (!bo_va->base.bo)
2620 total_moved += amdgpu_bo_print_info(id++, bo_va->base.bo, m);
2622 total_moved_objs = id;
2625 seq_puts(m, "\tInvalidated BOs:\n");
2626 list_for_each_entry_safe(bo_va, tmp, &vm->invalidated, base.vm_status) {
2627 if (!bo_va->base.bo)
2629 total_invalidated += amdgpu_bo_print_info(id++, bo_va->base.bo, m);
2631 total_invalidated_objs = id;
2634 seq_puts(m, "\tDone BOs:\n");
2635 list_for_each_entry_safe(bo_va, tmp, &vm->done, base.vm_status) {
2636 if (!bo_va->base.bo)
2638 total_done += amdgpu_bo_print_info(id++, bo_va->base.bo, m);
2640 spin_unlock(&vm->status_lock);
2641 total_done_objs = id;
2643 seq_printf(m, "\tTotal idle size: %12lld\tobjs:\t%d\n", total_idle,
2645 seq_printf(m, "\tTotal evicted size: %12lld\tobjs:\t%d\n", total_evicted,
2646 total_evicted_objs);
2647 seq_printf(m, "\tTotal relocated size: %12lld\tobjs:\t%d\n", total_relocated,
2648 total_relocated_objs);
2649 seq_printf(m, "\tTotal moved size: %12lld\tobjs:\t%d\n", total_moved,
2651 seq_printf(m, "\tTotal invalidated size: %12lld\tobjs:\t%d\n", total_invalidated,
2652 total_invalidated_objs);
2653 seq_printf(m, "\tTotal done size: %12lld\tobjs:\t%d\n", total_done,