2 * Copyright 2008 Advanced Micro Devices, Inc.
3 * Copyright 2008 Red Hat Inc.
4 * Copyright 2009 Jerome Glisse.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
24 * Authors: Dave Airlie
28 #include <linux/dma-fence-array.h>
29 #include <linux/interval_tree_generic.h>
30 #include <linux/idr.h>
32 #include <drm/amdgpu_drm.h>
34 #include "amdgpu_trace.h"
35 #include "amdgpu_amdkfd.h"
39 * GPUVM is similar to the legacy gart on older asics, however
40 * rather than there being a single global gart table
41 * for the entire GPU, there are multiple VM page tables active
42 * at any given time. The VM page tables can contain a mix
43 * vram pages and system memory pages and system memory pages
44 * can be mapped as snooped (cached system pages) or unsnooped
45 * (uncached system pages).
46 * Each VM has an ID associated with it and there is a page table
47 * associated with each VMID. When execting a command buffer,
48 * the kernel tells the the ring what VMID to use for that command
49 * buffer. VMIDs are allocated dynamically as commands are submitted.
50 * The userspace drivers maintain their own address space and the kernel
51 * sets up their pages tables accordingly when they submit their
52 * command buffers and a VMID is assigned.
53 * Cayman/Trinity support up to 8 active VMs at any given time;
57 #define START(node) ((node)->start)
58 #define LAST(node) ((node)->last)
60 INTERVAL_TREE_DEFINE(struct amdgpu_bo_va_mapping, rb, uint64_t, __subtree_last,
61 START, LAST, static, amdgpu_vm_it)
66 /* Local structure. Encapsulate some VM table update parameters to reduce
67 * the number of function parameters
69 struct amdgpu_pte_update_params {
70 /* amdgpu device we do this update for */
71 struct amdgpu_device *adev;
72 /* optional amdgpu_vm we do this update for */
74 /* address where to copy page table entries from */
76 /* indirect buffer to fill with commands */
78 /* Function which actually does the update */
79 void (*func)(struct amdgpu_pte_update_params *params,
80 struct amdgpu_bo *bo, uint64_t pe,
81 uint64_t addr, unsigned count, uint32_t incr,
83 /* The next two are used during VM update by CPU
84 * DMA addresses to use for mapping
85 * Kernel pointer of PD/PT BO that needs to be updated
87 dma_addr_t *pages_addr;
91 /* Helper to disable partial resident texture feature from a fence callback */
92 struct amdgpu_prt_cb {
93 struct amdgpu_device *adev;
94 struct dma_fence_cb cb;
97 static void amdgpu_vm_bo_base_init(struct amdgpu_vm_bo_base *base,
103 INIT_LIST_HEAD(&base->bo_list);
104 INIT_LIST_HEAD(&base->vm_status);
108 list_add_tail(&base->bo_list, &bo->va);
110 if (bo->tbo.resv != vm->root.base.bo->tbo.resv)
113 if (bo->preferred_domains &
114 amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type))
118 * we checked all the prerequisites, but it looks like this per vm bo
119 * is currently evicted. add the bo to the evicted list to make sure it
120 * is validated on next vm use to avoid fault.
122 list_move_tail(&base->vm_status, &vm->evicted);
126 * amdgpu_vm_level_shift - return the addr shift for each level
128 * @adev: amdgpu_device pointer
130 * Returns the number of bits the pfn needs to be right shifted for a level.
132 static unsigned amdgpu_vm_level_shift(struct amdgpu_device *adev,
135 unsigned shift = 0xff;
141 shift = 9 * (AMDGPU_VM_PDB0 - level) +
142 adev->vm_manager.block_size;
148 dev_err(adev->dev, "the level%d isn't supported.\n", level);
155 * amdgpu_vm_num_entries - return the number of entries in a PD/PT
157 * @adev: amdgpu_device pointer
159 * Calculate the number of entries in a page directory or page table.
161 static unsigned amdgpu_vm_num_entries(struct amdgpu_device *adev,
164 unsigned shift = amdgpu_vm_level_shift(adev,
165 adev->vm_manager.root_level);
167 if (level == adev->vm_manager.root_level)
168 /* For the root directory */
169 return round_up(adev->vm_manager.max_pfn, 1 << shift) >> shift;
170 else if (level != AMDGPU_VM_PTB)
171 /* Everything in between */
174 /* For the page tables on the leaves */
175 return AMDGPU_VM_PTE_COUNT(adev);
179 * amdgpu_vm_bo_size - returns the size of the BOs in bytes
181 * @adev: amdgpu_device pointer
183 * Calculate the size of the BO for a page directory or page table in bytes.
185 static unsigned amdgpu_vm_bo_size(struct amdgpu_device *adev, unsigned level)
187 return AMDGPU_GPU_PAGE_ALIGN(amdgpu_vm_num_entries(adev, level) * 8);
191 * amdgpu_vm_get_pd_bo - add the VM PD to a validation list
193 * @vm: vm providing the BOs
194 * @validated: head of validation list
195 * @entry: entry to add
197 * Add the page directory to the list of BOs to
198 * validate for command submission.
200 void amdgpu_vm_get_pd_bo(struct amdgpu_vm *vm,
201 struct list_head *validated,
202 struct amdgpu_bo_list_entry *entry)
204 entry->robj = vm->root.base.bo;
206 entry->tv.bo = &entry->robj->tbo;
207 entry->tv.shared = true;
208 entry->user_pages = NULL;
209 list_add(&entry->tv.head, validated);
213 * amdgpu_vm_validate_pt_bos - validate the page table BOs
215 * @adev: amdgpu device pointer
216 * @vm: vm providing the BOs
217 * @validate: callback to do the validation
218 * @param: parameter for the validation callback
220 * Validate the page table BOs on command submission if neccessary.
222 int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
223 int (*validate)(void *p, struct amdgpu_bo *bo),
226 struct ttm_bo_global *glob = adev->mman.bdev.glob;
227 struct amdgpu_vm_bo_base *bo_base, *tmp;
230 list_for_each_entry_safe(bo_base, tmp, &vm->evicted, vm_status) {
231 struct amdgpu_bo *bo = bo_base->bo;
234 r = validate(param, bo);
238 spin_lock(&glob->lru_lock);
239 ttm_bo_move_to_lru_tail(&bo->tbo);
241 ttm_bo_move_to_lru_tail(&bo->shadow->tbo);
242 spin_unlock(&glob->lru_lock);
245 if (bo->tbo.type != ttm_bo_type_kernel) {
246 spin_lock(&vm->moved_lock);
247 list_move(&bo_base->vm_status, &vm->moved);
248 spin_unlock(&vm->moved_lock);
250 list_move(&bo_base->vm_status, &vm->relocated);
258 * amdgpu_vm_ready - check VM is ready for updates
262 * Check if all VM PDs/PTs are ready for updates
264 bool amdgpu_vm_ready(struct amdgpu_vm *vm)
266 return list_empty(&vm->evicted);
270 * amdgpu_vm_clear_bo - initially clear the PDs/PTs
272 * @adev: amdgpu_device pointer
274 * @level: level this BO is at
276 * Root PD needs to be reserved when calling this.
278 static int amdgpu_vm_clear_bo(struct amdgpu_device *adev,
279 struct amdgpu_vm *vm, struct amdgpu_bo *bo,
280 unsigned level, bool pte_support_ats)
282 struct ttm_operation_ctx ctx = { true, false };
283 struct dma_fence *fence = NULL;
284 unsigned entries, ats_entries;
285 struct amdgpu_ring *ring;
286 struct amdgpu_job *job;
290 addr = amdgpu_bo_gpu_offset(bo);
291 entries = amdgpu_bo_size(bo) / 8;
293 if (pte_support_ats) {
294 if (level == adev->vm_manager.root_level) {
295 ats_entries = amdgpu_vm_level_shift(adev, level);
296 ats_entries += AMDGPU_GPU_PAGE_SHIFT;
297 ats_entries = AMDGPU_VA_HOLE_START >> ats_entries;
298 ats_entries = min(ats_entries, entries);
299 entries -= ats_entries;
301 ats_entries = entries;
308 ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
310 r = reservation_object_reserve_shared(bo->tbo.resv);
314 r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
318 r = amdgpu_job_alloc_with_ib(adev, 64, &job);
325 ats_value = AMDGPU_PTE_DEFAULT_ATC;
326 if (level != AMDGPU_VM_PTB)
327 ats_value |= AMDGPU_PDE_PTE;
329 amdgpu_vm_set_pte_pde(adev, &job->ibs[0], addr, 0,
330 ats_entries, 0, ats_value);
331 addr += ats_entries * 8;
335 amdgpu_vm_set_pte_pde(adev, &job->ibs[0], addr, 0,
338 amdgpu_ring_pad_ib(ring, &job->ibs[0]);
340 WARN_ON(job->ibs[0].length_dw > 64);
341 r = amdgpu_sync_resv(adev, &job->sync, bo->tbo.resv,
342 AMDGPU_FENCE_OWNER_UNDEFINED, false);
346 r = amdgpu_job_submit(job, ring, &vm->entity,
347 AMDGPU_FENCE_OWNER_UNDEFINED, &fence);
351 amdgpu_bo_fence(bo, fence, true);
352 dma_fence_put(fence);
355 return amdgpu_vm_clear_bo(adev, vm, bo->shadow,
356 level, pte_support_ats);
361 amdgpu_job_free(job);
368 * amdgpu_vm_alloc_levels - allocate the PD/PT levels
370 * @adev: amdgpu_device pointer
372 * @saddr: start of the address range
373 * @eaddr: end of the address range
375 * Make sure the page directories and page tables are allocated
377 static int amdgpu_vm_alloc_levels(struct amdgpu_device *adev,
378 struct amdgpu_vm *vm,
379 struct amdgpu_vm_pt *parent,
380 uint64_t saddr, uint64_t eaddr,
381 unsigned level, bool ats)
383 unsigned shift = amdgpu_vm_level_shift(adev, level);
384 unsigned pt_idx, from, to;
388 if (!parent->entries) {
389 unsigned num_entries = amdgpu_vm_num_entries(adev, level);
391 parent->entries = kvmalloc_array(num_entries,
392 sizeof(struct amdgpu_vm_pt),
393 GFP_KERNEL | __GFP_ZERO);
394 if (!parent->entries)
396 memset(parent->entries, 0 , sizeof(struct amdgpu_vm_pt));
399 from = saddr >> shift;
401 if (from >= amdgpu_vm_num_entries(adev, level) ||
402 to >= amdgpu_vm_num_entries(adev, level))
406 saddr = saddr & ((1 << shift) - 1);
407 eaddr = eaddr & ((1 << shift) - 1);
409 flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
410 if (vm->use_cpu_for_update)
411 flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
413 flags |= (AMDGPU_GEM_CREATE_NO_CPU_ACCESS |
414 AMDGPU_GEM_CREATE_SHADOW);
416 /* walk over the address space and allocate the page tables */
417 for (pt_idx = from; pt_idx <= to; ++pt_idx) {
418 struct reservation_object *resv = vm->root.base.bo->tbo.resv;
419 struct amdgpu_vm_pt *entry = &parent->entries[pt_idx];
420 struct amdgpu_bo *pt;
422 if (!entry->base.bo) {
423 struct amdgpu_bo_param bp;
425 memset(&bp, 0, sizeof(bp));
426 bp.size = amdgpu_vm_bo_size(adev, level);
427 bp.byte_align = AMDGPU_GPU_PAGE_SIZE;
428 bp.domain = AMDGPU_GEM_DOMAIN_VRAM;
430 bp.type = ttm_bo_type_kernel;
432 r = amdgpu_bo_create(adev, &bp, &pt);
436 r = amdgpu_vm_clear_bo(adev, vm, pt, level, ats);
438 amdgpu_bo_unref(&pt->shadow);
439 amdgpu_bo_unref(&pt);
443 if (vm->use_cpu_for_update) {
444 r = amdgpu_bo_kmap(pt, NULL);
446 amdgpu_bo_unref(&pt->shadow);
447 amdgpu_bo_unref(&pt);
452 /* Keep a reference to the root directory to avoid
453 * freeing them up in the wrong order.
455 pt->parent = amdgpu_bo_ref(parent->base.bo);
457 amdgpu_vm_bo_base_init(&entry->base, vm, pt);
458 list_move(&entry->base.vm_status, &vm->relocated);
461 if (level < AMDGPU_VM_PTB) {
462 uint64_t sub_saddr = (pt_idx == from) ? saddr : 0;
463 uint64_t sub_eaddr = (pt_idx == to) ? eaddr :
465 r = amdgpu_vm_alloc_levels(adev, vm, entry, sub_saddr,
466 sub_eaddr, level, ats);
476 * amdgpu_vm_alloc_pts - Allocate page tables.
478 * @adev: amdgpu_device pointer
479 * @vm: VM to allocate page tables for
480 * @saddr: Start address which needs to be allocated
481 * @size: Size from start address we need.
483 * Make sure the page tables are allocated.
485 int amdgpu_vm_alloc_pts(struct amdgpu_device *adev,
486 struct amdgpu_vm *vm,
487 uint64_t saddr, uint64_t size)
492 /* validate the parameters */
493 if (saddr & AMDGPU_GPU_PAGE_MASK || size & AMDGPU_GPU_PAGE_MASK)
496 eaddr = saddr + size - 1;
498 if (vm->pte_support_ats)
499 ats = saddr < AMDGPU_VA_HOLE_START;
501 saddr /= AMDGPU_GPU_PAGE_SIZE;
502 eaddr /= AMDGPU_GPU_PAGE_SIZE;
504 if (eaddr >= adev->vm_manager.max_pfn) {
505 dev_err(adev->dev, "va above limit (0x%08llX >= 0x%08llX)\n",
506 eaddr, adev->vm_manager.max_pfn);
510 return amdgpu_vm_alloc_levels(adev, vm, &vm->root, saddr, eaddr,
511 adev->vm_manager.root_level, ats);
515 * amdgpu_vm_check_compute_bug - check whether asic has compute vm bug
517 * @adev: amdgpu_device pointer
519 void amdgpu_vm_check_compute_bug(struct amdgpu_device *adev)
521 const struct amdgpu_ip_block *ip_block;
522 bool has_compute_vm_bug;
523 struct amdgpu_ring *ring;
526 has_compute_vm_bug = false;
528 ip_block = amdgpu_device_ip_get_ip_block(adev, AMD_IP_BLOCK_TYPE_GFX);
530 /* Compute has a VM bug for GFX version < 7.
531 Compute has a VM bug for GFX 8 MEC firmware version < 673.*/
532 if (ip_block->version->major <= 7)
533 has_compute_vm_bug = true;
534 else if (ip_block->version->major == 8)
535 if (adev->gfx.mec_fw_version < 673)
536 has_compute_vm_bug = true;
539 for (i = 0; i < adev->num_rings; i++) {
540 ring = adev->rings[i];
541 if (ring->funcs->type == AMDGPU_RING_TYPE_COMPUTE)
542 /* only compute rings */
543 ring->has_compute_vm_bug = has_compute_vm_bug;
545 ring->has_compute_vm_bug = false;
549 bool amdgpu_vm_need_pipeline_sync(struct amdgpu_ring *ring,
550 struct amdgpu_job *job)
552 struct amdgpu_device *adev = ring->adev;
553 unsigned vmhub = ring->funcs->vmhub;
554 struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
555 struct amdgpu_vmid *id;
556 bool gds_switch_needed;
557 bool vm_flush_needed = job->vm_needs_flush || ring->has_compute_vm_bug;
561 id = &id_mgr->ids[job->vmid];
562 gds_switch_needed = ring->funcs->emit_gds_switch && (
563 id->gds_base != job->gds_base ||
564 id->gds_size != job->gds_size ||
565 id->gws_base != job->gws_base ||
566 id->gws_size != job->gws_size ||
567 id->oa_base != job->oa_base ||
568 id->oa_size != job->oa_size);
570 if (amdgpu_vmid_had_gpu_reset(adev, id))
573 return vm_flush_needed || gds_switch_needed;
576 static bool amdgpu_vm_is_large_bar(struct amdgpu_device *adev)
578 return (adev->gmc.real_vram_size == adev->gmc.visible_vram_size);
582 * amdgpu_vm_flush - hardware flush the vm
584 * @ring: ring to use for flush
585 * @vmid: vmid number to use
586 * @pd_addr: address of the page directory
588 * Emit a VM flush when it is necessary.
590 int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job, bool need_pipe_sync)
592 struct amdgpu_device *adev = ring->adev;
593 unsigned vmhub = ring->funcs->vmhub;
594 struct amdgpu_vmid_mgr *id_mgr = &adev->vm_manager.id_mgr[vmhub];
595 struct amdgpu_vmid *id = &id_mgr->ids[job->vmid];
596 bool gds_switch_needed = ring->funcs->emit_gds_switch && (
597 id->gds_base != job->gds_base ||
598 id->gds_size != job->gds_size ||
599 id->gws_base != job->gws_base ||
600 id->gws_size != job->gws_size ||
601 id->oa_base != job->oa_base ||
602 id->oa_size != job->oa_size);
603 bool vm_flush_needed = job->vm_needs_flush;
604 bool pasid_mapping_needed = id->pasid != job->pasid ||
605 !id->pasid_mapping ||
606 !dma_fence_is_signaled(id->pasid_mapping);
607 struct dma_fence *fence = NULL;
608 unsigned patch_offset = 0;
611 if (amdgpu_vmid_had_gpu_reset(adev, id)) {
612 gds_switch_needed = true;
613 vm_flush_needed = true;
614 pasid_mapping_needed = true;
617 gds_switch_needed &= !!ring->funcs->emit_gds_switch;
618 vm_flush_needed &= !!ring->funcs->emit_vm_flush;
619 pasid_mapping_needed &= adev->gmc.gmc_funcs->emit_pasid_mapping &&
620 ring->funcs->emit_wreg;
622 if (!vm_flush_needed && !gds_switch_needed && !need_pipe_sync)
625 if (ring->funcs->init_cond_exec)
626 patch_offset = amdgpu_ring_init_cond_exec(ring);
629 amdgpu_ring_emit_pipeline_sync(ring);
631 if (vm_flush_needed) {
632 trace_amdgpu_vm_flush(ring, job->vmid, job->vm_pd_addr);
633 amdgpu_ring_emit_vm_flush(ring, job->vmid, job->vm_pd_addr);
636 if (pasid_mapping_needed)
637 amdgpu_gmc_emit_pasid_mapping(ring, job->vmid, job->pasid);
639 if (vm_flush_needed || pasid_mapping_needed) {
640 r = amdgpu_fence_emit(ring, &fence, 0);
645 if (vm_flush_needed) {
646 mutex_lock(&id_mgr->lock);
647 dma_fence_put(id->last_flush);
648 id->last_flush = dma_fence_get(fence);
649 id->current_gpu_reset_count =
650 atomic_read(&adev->gpu_reset_counter);
651 mutex_unlock(&id_mgr->lock);
654 if (pasid_mapping_needed) {
655 id->pasid = job->pasid;
656 dma_fence_put(id->pasid_mapping);
657 id->pasid_mapping = dma_fence_get(fence);
659 dma_fence_put(fence);
661 if (ring->funcs->emit_gds_switch && gds_switch_needed) {
662 id->gds_base = job->gds_base;
663 id->gds_size = job->gds_size;
664 id->gws_base = job->gws_base;
665 id->gws_size = job->gws_size;
666 id->oa_base = job->oa_base;
667 id->oa_size = job->oa_size;
668 amdgpu_ring_emit_gds_switch(ring, job->vmid, job->gds_base,
669 job->gds_size, job->gws_base,
670 job->gws_size, job->oa_base,
674 if (ring->funcs->patch_cond_exec)
675 amdgpu_ring_patch_cond_exec(ring, patch_offset);
677 /* the double SWITCH_BUFFER here *cannot* be skipped by COND_EXEC */
678 if (ring->funcs->emit_switch_buffer) {
679 amdgpu_ring_emit_switch_buffer(ring);
680 amdgpu_ring_emit_switch_buffer(ring);
686 * amdgpu_vm_bo_find - find the bo_va for a specific vm & bo
689 * @bo: requested buffer object
691 * Find @bo inside the requested vm.
692 * Search inside the @bos vm list for the requested vm
693 * Returns the found bo_va or NULL if none is found
695 * Object has to be reserved!
697 struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
698 struct amdgpu_bo *bo)
700 struct amdgpu_bo_va *bo_va;
702 list_for_each_entry(bo_va, &bo->va, base.bo_list) {
703 if (bo_va->base.vm == vm) {
711 * amdgpu_vm_do_set_ptes - helper to call the right asic function
713 * @params: see amdgpu_pte_update_params definition
714 * @bo: PD/PT to update
715 * @pe: addr of the page entry
716 * @addr: dst addr to write into pe
717 * @count: number of page entries to update
718 * @incr: increase next addr by incr bytes
719 * @flags: hw access flags
721 * Traces the parameters and calls the right asic functions
722 * to setup the page table using the DMA.
724 static void amdgpu_vm_do_set_ptes(struct amdgpu_pte_update_params *params,
725 struct amdgpu_bo *bo,
726 uint64_t pe, uint64_t addr,
727 unsigned count, uint32_t incr,
730 pe += amdgpu_bo_gpu_offset(bo);
731 trace_amdgpu_vm_set_ptes(pe, addr, count, incr, flags);
734 amdgpu_vm_write_pte(params->adev, params->ib, pe,
735 addr | flags, count, incr);
738 amdgpu_vm_set_pte_pde(params->adev, params->ib, pe, addr,
744 * amdgpu_vm_do_copy_ptes - copy the PTEs from the GART
746 * @params: see amdgpu_pte_update_params definition
747 * @bo: PD/PT to update
748 * @pe: addr of the page entry
749 * @addr: dst addr to write into pe
750 * @count: number of page entries to update
751 * @incr: increase next addr by incr bytes
752 * @flags: hw access flags
754 * Traces the parameters and calls the DMA function to copy the PTEs.
756 static void amdgpu_vm_do_copy_ptes(struct amdgpu_pte_update_params *params,
757 struct amdgpu_bo *bo,
758 uint64_t pe, uint64_t addr,
759 unsigned count, uint32_t incr,
762 uint64_t src = (params->src + (addr >> 12) * 8);
764 pe += amdgpu_bo_gpu_offset(bo);
765 trace_amdgpu_vm_copy_ptes(pe, src, count);
767 amdgpu_vm_copy_pte(params->adev, params->ib, pe, src, count);
771 * amdgpu_vm_map_gart - Resolve gart mapping of addr
773 * @pages_addr: optional DMA address to use for lookup
774 * @addr: the unmapped addr
776 * Look up the physical address of the page that the pte resolves
777 * to and return the pointer for the page table entry.
779 static uint64_t amdgpu_vm_map_gart(const dma_addr_t *pages_addr, uint64_t addr)
783 /* page table offset */
784 result = pages_addr[addr >> PAGE_SHIFT];
786 /* in case cpu page size != gpu page size*/
787 result |= addr & (~PAGE_MASK);
789 result &= 0xFFFFFFFFFFFFF000ULL;
795 * amdgpu_vm_cpu_set_ptes - helper to update page tables via CPU
797 * @params: see amdgpu_pte_update_params definition
798 * @bo: PD/PT to update
799 * @pe: kmap addr of the page entry
800 * @addr: dst addr to write into pe
801 * @count: number of page entries to update
802 * @incr: increase next addr by incr bytes
803 * @flags: hw access flags
805 * Write count number of PT/PD entries directly.
807 static void amdgpu_vm_cpu_set_ptes(struct amdgpu_pte_update_params *params,
808 struct amdgpu_bo *bo,
809 uint64_t pe, uint64_t addr,
810 unsigned count, uint32_t incr,
816 pe += (unsigned long)amdgpu_bo_kptr(bo);
818 trace_amdgpu_vm_set_ptes(pe, addr, count, incr, flags);
820 for (i = 0; i < count; i++) {
821 value = params->pages_addr ?
822 amdgpu_vm_map_gart(params->pages_addr, addr) :
824 amdgpu_gmc_set_pte_pde(params->adev, (void *)(uintptr_t)pe,
830 static int amdgpu_vm_wait_pd(struct amdgpu_device *adev, struct amdgpu_vm *vm,
833 struct amdgpu_sync sync;
836 amdgpu_sync_create(&sync);
837 amdgpu_sync_resv(adev, &sync, vm->root.base.bo->tbo.resv, owner, false);
838 r = amdgpu_sync_wait(&sync, true);
839 amdgpu_sync_free(&sync);
845 * amdgpu_vm_update_pde - update a single level in the hierarchy
847 * @param: parameters for the update
849 * @parent: parent directory
850 * @entry: entry to update
852 * Makes sure the requested entry in parent is up to date.
854 static void amdgpu_vm_update_pde(struct amdgpu_pte_update_params *params,
855 struct amdgpu_vm *vm,
856 struct amdgpu_vm_pt *parent,
857 struct amdgpu_vm_pt *entry)
859 struct amdgpu_bo *bo = parent->base.bo, *pbo;
860 uint64_t pde, pt, flags;
863 /* Don't update huge pages here */
867 for (level = 0, pbo = bo->parent; pbo; ++level)
870 level += params->adev->vm_manager.root_level;
871 pt = amdgpu_bo_gpu_offset(entry->base.bo);
872 flags = AMDGPU_PTE_VALID;
873 amdgpu_gmc_get_vm_pde(params->adev, level, &pt, &flags);
874 pde = (entry - parent->entries) * 8;
876 params->func(params, bo->shadow, pde, pt, 1, 0, flags);
877 params->func(params, bo, pde, pt, 1, 0, flags);
881 * amdgpu_vm_invalidate_level - mark all PD levels as invalid
885 * Mark all PD level as invalid after an error.
887 static void amdgpu_vm_invalidate_level(struct amdgpu_device *adev,
888 struct amdgpu_vm *vm,
889 struct amdgpu_vm_pt *parent,
892 unsigned pt_idx, num_entries;
895 * Recurse into the subdirectories. This recursion is harmless because
896 * we only have a maximum of 5 layers.
898 num_entries = amdgpu_vm_num_entries(adev, level);
899 for (pt_idx = 0; pt_idx < num_entries; ++pt_idx) {
900 struct amdgpu_vm_pt *entry = &parent->entries[pt_idx];
905 if (list_empty(&entry->base.vm_status))
906 list_add(&entry->base.vm_status, &vm->relocated);
907 amdgpu_vm_invalidate_level(adev, vm, entry, level + 1);
912 * amdgpu_vm_update_directories - make sure that all directories are valid
914 * @adev: amdgpu_device pointer
917 * Makes sure all directories are up to date.
918 * Returns 0 for success, error for failure.
920 int amdgpu_vm_update_directories(struct amdgpu_device *adev,
921 struct amdgpu_vm *vm)
923 struct amdgpu_pte_update_params params;
924 struct amdgpu_job *job;
928 if (list_empty(&vm->relocated))
932 memset(¶ms, 0, sizeof(params));
935 if (vm->use_cpu_for_update) {
936 struct amdgpu_vm_bo_base *bo_base;
938 list_for_each_entry(bo_base, &vm->relocated, vm_status) {
939 r = amdgpu_bo_kmap(bo_base->bo, NULL);
944 r = amdgpu_vm_wait_pd(adev, vm, AMDGPU_FENCE_OWNER_VM);
948 params.func = amdgpu_vm_cpu_set_ptes;
951 r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
955 params.ib = &job->ibs[0];
956 params.func = amdgpu_vm_do_set_ptes;
959 while (!list_empty(&vm->relocated)) {
960 struct amdgpu_vm_bo_base *bo_base, *parent;
961 struct amdgpu_vm_pt *pt, *entry;
962 struct amdgpu_bo *bo;
964 bo_base = list_first_entry(&vm->relocated,
965 struct amdgpu_vm_bo_base,
967 list_del_init(&bo_base->vm_status);
969 bo = bo_base->bo->parent;
973 parent = list_first_entry(&bo->va, struct amdgpu_vm_bo_base,
975 pt = container_of(parent, struct amdgpu_vm_pt, base);
976 entry = container_of(bo_base, struct amdgpu_vm_pt, base);
978 amdgpu_vm_update_pde(¶ms, vm, pt, entry);
980 if (!vm->use_cpu_for_update &&
981 (ndw - params.ib->length_dw) < 32)
985 if (vm->use_cpu_for_update) {
988 amdgpu_asic_flush_hdp(adev, NULL);
989 } else if (params.ib->length_dw == 0) {
990 amdgpu_job_free(job);
992 struct amdgpu_bo *root = vm->root.base.bo;
993 struct amdgpu_ring *ring;
994 struct dma_fence *fence;
996 ring = container_of(vm->entity.sched, struct amdgpu_ring,
999 amdgpu_ring_pad_ib(ring, params.ib);
1000 amdgpu_sync_resv(adev, &job->sync, root->tbo.resv,
1001 AMDGPU_FENCE_OWNER_VM, false);
1002 WARN_ON(params.ib->length_dw > ndw);
1003 r = amdgpu_job_submit(job, ring, &vm->entity,
1004 AMDGPU_FENCE_OWNER_VM, &fence);
1008 amdgpu_bo_fence(root, fence, true);
1009 dma_fence_put(vm->last_update);
1010 vm->last_update = fence;
1013 if (!list_empty(&vm->relocated))
1019 amdgpu_vm_invalidate_level(adev, vm, &vm->root,
1020 adev->vm_manager.root_level);
1021 amdgpu_job_free(job);
1026 * amdgpu_vm_find_entry - find the entry for an address
1028 * @p: see amdgpu_pte_update_params definition
1029 * @addr: virtual address in question
1030 * @entry: resulting entry or NULL
1031 * @parent: parent entry
1033 * Find the vm_pt entry and it's parent for the given address.
1035 void amdgpu_vm_get_entry(struct amdgpu_pte_update_params *p, uint64_t addr,
1036 struct amdgpu_vm_pt **entry,
1037 struct amdgpu_vm_pt **parent)
1039 unsigned level = p->adev->vm_manager.root_level;
1042 *entry = &p->vm->root;
1043 while ((*entry)->entries) {
1044 unsigned shift = amdgpu_vm_level_shift(p->adev, level++);
1047 *entry = &(*entry)->entries[addr >> shift];
1048 addr &= (1ULL << shift) - 1;
1051 if (level != AMDGPU_VM_PTB)
1056 * amdgpu_vm_handle_huge_pages - handle updating the PD with huge pages
1058 * @p: see amdgpu_pte_update_params definition
1059 * @entry: vm_pt entry to check
1060 * @parent: parent entry
1061 * @nptes: number of PTEs updated with this operation
1062 * @dst: destination address where the PTEs should point to
1063 * @flags: access flags fro the PTEs
1065 * Check if we can update the PD with a huge page.
1067 static void amdgpu_vm_handle_huge_pages(struct amdgpu_pte_update_params *p,
1068 struct amdgpu_vm_pt *entry,
1069 struct amdgpu_vm_pt *parent,
1070 unsigned nptes, uint64_t dst,
1075 /* In the case of a mixed PT the PDE must point to it*/
1076 if (p->adev->asic_type >= CHIP_VEGA10 && !p->src &&
1077 nptes == AMDGPU_VM_PTE_COUNT(p->adev)) {
1078 /* Set the huge page flag to stop scanning at this PDE */
1079 flags |= AMDGPU_PDE_PTE;
1082 if (!(flags & AMDGPU_PDE_PTE)) {
1084 /* Add the entry to the relocated list to update it. */
1085 entry->huge = false;
1086 list_move(&entry->base.vm_status, &p->vm->relocated);
1092 amdgpu_gmc_get_vm_pde(p->adev, AMDGPU_VM_PDB0, &dst, &flags);
1094 pde = (entry - parent->entries) * 8;
1095 if (parent->base.bo->shadow)
1096 p->func(p, parent->base.bo->shadow, pde, dst, 1, 0, flags);
1097 p->func(p, parent->base.bo, pde, dst, 1, 0, flags);
1101 * amdgpu_vm_update_ptes - make sure that page tables are valid
1103 * @params: see amdgpu_pte_update_params definition
1105 * @start: start of GPU address range
1106 * @end: end of GPU address range
1107 * @dst: destination address to map to, the next dst inside the function
1108 * @flags: mapping flags
1110 * Update the page tables in the range @start - @end.
1111 * Returns 0 for success, -EINVAL for failure.
1113 static int amdgpu_vm_update_ptes(struct amdgpu_pte_update_params *params,
1114 uint64_t start, uint64_t end,
1115 uint64_t dst, uint64_t flags)
1117 struct amdgpu_device *adev = params->adev;
1118 const uint64_t mask = AMDGPU_VM_PTE_COUNT(adev) - 1;
1120 uint64_t addr, pe_start;
1121 struct amdgpu_bo *pt;
1124 /* walk over the address space and update the page tables */
1125 for (addr = start; addr < end; addr += nptes,
1126 dst += nptes * AMDGPU_GPU_PAGE_SIZE) {
1127 struct amdgpu_vm_pt *entry, *parent;
1129 amdgpu_vm_get_entry(params, addr, &entry, &parent);
1133 if ((addr & ~mask) == (end & ~mask))
1136 nptes = AMDGPU_VM_PTE_COUNT(adev) - (addr & mask);
1138 amdgpu_vm_handle_huge_pages(params, entry, parent,
1140 /* We don't need to update PTEs for huge pages */
1144 pt = entry->base.bo;
1145 pe_start = (addr & mask) * 8;
1147 params->func(params, pt->shadow, pe_start, dst, nptes,
1148 AMDGPU_GPU_PAGE_SIZE, flags);
1149 params->func(params, pt, pe_start, dst, nptes,
1150 AMDGPU_GPU_PAGE_SIZE, flags);
1157 * amdgpu_vm_frag_ptes - add fragment information to PTEs
1159 * @params: see amdgpu_pte_update_params definition
1161 * @start: first PTE to handle
1162 * @end: last PTE to handle
1163 * @dst: addr those PTEs should point to
1164 * @flags: hw mapping flags
1165 * Returns 0 for success, -EINVAL for failure.
1167 static int amdgpu_vm_frag_ptes(struct amdgpu_pte_update_params *params,
1168 uint64_t start, uint64_t end,
1169 uint64_t dst, uint64_t flags)
1172 * The MC L1 TLB supports variable sized pages, based on a fragment
1173 * field in the PTE. When this field is set to a non-zero value, page
1174 * granularity is increased from 4KB to (1 << (12 + frag)). The PTE
1175 * flags are considered valid for all PTEs within the fragment range
1176 * and corresponding mappings are assumed to be physically contiguous.
1178 * The L1 TLB can store a single PTE for the whole fragment,
1179 * significantly increasing the space available for translation
1180 * caching. This leads to large improvements in throughput when the
1181 * TLB is under pressure.
1183 * The L2 TLB distributes small and large fragments into two
1184 * asymmetric partitions. The large fragment cache is significantly
1185 * larger. Thus, we try to use large fragments wherever possible.
1186 * Userspace can support this by aligning virtual base address and
1187 * allocation size to the fragment size.
1189 unsigned max_frag = params->adev->vm_manager.fragment_size;
1192 /* system pages are non continuously */
1193 if (params->src || !(flags & AMDGPU_PTE_VALID))
1194 return amdgpu_vm_update_ptes(params, start, end, dst, flags);
1196 while (start != end) {
1197 uint64_t frag_flags, frag_end;
1200 /* This intentionally wraps around if no bit is set */
1201 frag = min((unsigned)ffs(start) - 1,
1202 (unsigned)fls64(end - start) - 1);
1203 if (frag >= max_frag) {
1204 frag_flags = AMDGPU_PTE_FRAG(max_frag);
1205 frag_end = end & ~((1ULL << max_frag) - 1);
1207 frag_flags = AMDGPU_PTE_FRAG(frag);
1208 frag_end = start + (1 << frag);
1211 r = amdgpu_vm_update_ptes(params, start, frag_end, dst,
1212 flags | frag_flags);
1216 dst += (frag_end - start) * AMDGPU_GPU_PAGE_SIZE;
1224 * amdgpu_vm_bo_update_mapping - update a mapping in the vm page table
1226 * @adev: amdgpu_device pointer
1227 * @exclusive: fence we need to sync to
1228 * @pages_addr: DMA addresses to use for mapping
1230 * @start: start of mapped range
1231 * @last: last mapped entry
1232 * @flags: flags for the entries
1233 * @addr: addr to set the area to
1234 * @fence: optional resulting fence
1236 * Fill in the page table entries between @start and @last.
1237 * Returns 0 for success, -EINVAL for failure.
1239 static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
1240 struct dma_fence *exclusive,
1241 dma_addr_t *pages_addr,
1242 struct amdgpu_vm *vm,
1243 uint64_t start, uint64_t last,
1244 uint64_t flags, uint64_t addr,
1245 struct dma_fence **fence)
1247 struct amdgpu_ring *ring;
1248 void *owner = AMDGPU_FENCE_OWNER_VM;
1249 unsigned nptes, ncmds, ndw;
1250 struct amdgpu_job *job;
1251 struct amdgpu_pte_update_params params;
1252 struct dma_fence *f = NULL;
1255 memset(¶ms, 0, sizeof(params));
1259 /* sync to everything on unmapping */
1260 if (!(flags & AMDGPU_PTE_VALID))
1261 owner = AMDGPU_FENCE_OWNER_UNDEFINED;
1263 if (vm->use_cpu_for_update) {
1264 /* params.src is used as flag to indicate system Memory */
1268 /* Wait for PT BOs to be free. PTs share the same resv. object
1271 r = amdgpu_vm_wait_pd(adev, vm, owner);
1275 params.func = amdgpu_vm_cpu_set_ptes;
1276 params.pages_addr = pages_addr;
1277 return amdgpu_vm_frag_ptes(¶ms, start, last + 1,
1281 ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
1283 nptes = last - start + 1;
1286 * reserve space for two commands every (1 << BLOCK_SIZE)
1287 * entries or 2k dwords (whatever is smaller)
1289 * The second command is for the shadow pagetables.
1291 if (vm->root.base.bo->shadow)
1292 ncmds = ((nptes >> min(adev->vm_manager.block_size, 11u)) + 1) * 2;
1294 ncmds = ((nptes >> min(adev->vm_manager.block_size, 11u)) + 1);
1300 /* copy commands needed */
1301 ndw += ncmds * adev->vm_manager.vm_pte_funcs->copy_pte_num_dw;
1306 params.func = amdgpu_vm_do_copy_ptes;
1309 /* set page commands needed */
1312 /* extra commands for begin/end fragments */
1313 ndw += 2 * 10 * adev->vm_manager.fragment_size;
1315 params.func = amdgpu_vm_do_set_ptes;
1318 r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
1322 params.ib = &job->ibs[0];
1328 /* Put the PTEs at the end of the IB. */
1329 i = ndw - nptes * 2;
1330 pte= (uint64_t *)&(job->ibs->ptr[i]);
1331 params.src = job->ibs->gpu_addr + i * 4;
1333 for (i = 0; i < nptes; ++i) {
1334 pte[i] = amdgpu_vm_map_gart(pages_addr, addr + i *
1335 AMDGPU_GPU_PAGE_SIZE);
1341 r = amdgpu_sync_fence(adev, &job->sync, exclusive, false);
1345 r = amdgpu_sync_resv(adev, &job->sync, vm->root.base.bo->tbo.resv,
1350 r = reservation_object_reserve_shared(vm->root.base.bo->tbo.resv);
1354 r = amdgpu_vm_frag_ptes(¶ms, start, last + 1, addr, flags);
1358 amdgpu_ring_pad_ib(ring, params.ib);
1359 WARN_ON(params.ib->length_dw > ndw);
1360 r = amdgpu_job_submit(job, ring, &vm->entity,
1361 AMDGPU_FENCE_OWNER_VM, &f);
1365 amdgpu_bo_fence(vm->root.base.bo, f, true);
1366 dma_fence_put(*fence);
1371 amdgpu_job_free(job);
1376 * amdgpu_vm_bo_split_mapping - split a mapping into smaller chunks
1378 * @adev: amdgpu_device pointer
1379 * @exclusive: fence we need to sync to
1380 * @pages_addr: DMA addresses to use for mapping
1382 * @mapping: mapped range and flags to use for the update
1383 * @flags: HW flags for the mapping
1384 * @nodes: array of drm_mm_nodes with the MC addresses
1385 * @fence: optional resulting fence
1387 * Split the mapping into smaller chunks so that each update fits
1389 * Returns 0 for success, -EINVAL for failure.
1391 static int amdgpu_vm_bo_split_mapping(struct amdgpu_device *adev,
1392 struct dma_fence *exclusive,
1393 dma_addr_t *pages_addr,
1394 struct amdgpu_vm *vm,
1395 struct amdgpu_bo_va_mapping *mapping,
1397 struct drm_mm_node *nodes,
1398 struct dma_fence **fence)
1400 unsigned min_linear_pages = 1 << adev->vm_manager.fragment_size;
1401 uint64_t pfn, start = mapping->start;
1404 /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here
1405 * but in case of something, we filter the flags in first place
1407 if (!(mapping->flags & AMDGPU_PTE_READABLE))
1408 flags &= ~AMDGPU_PTE_READABLE;
1409 if (!(mapping->flags & AMDGPU_PTE_WRITEABLE))
1410 flags &= ~AMDGPU_PTE_WRITEABLE;
1412 flags &= ~AMDGPU_PTE_EXECUTABLE;
1413 flags |= mapping->flags & AMDGPU_PTE_EXECUTABLE;
1415 flags &= ~AMDGPU_PTE_MTYPE_MASK;
1416 flags |= (mapping->flags & AMDGPU_PTE_MTYPE_MASK);
1418 if ((mapping->flags & AMDGPU_PTE_PRT) &&
1419 (adev->asic_type >= CHIP_VEGA10)) {
1420 flags |= AMDGPU_PTE_PRT;
1421 flags &= ~AMDGPU_PTE_VALID;
1424 trace_amdgpu_vm_bo_update(mapping);
1426 pfn = mapping->offset >> PAGE_SHIFT;
1428 while (pfn >= nodes->size) {
1435 dma_addr_t *dma_addr = NULL;
1436 uint64_t max_entries;
1437 uint64_t addr, last;
1440 addr = nodes->start << PAGE_SHIFT;
1441 max_entries = (nodes->size - pfn) *
1442 (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE);
1445 max_entries = S64_MAX;
1451 max_entries = min(max_entries, 16ull * 1024ull);
1452 for (count = 1; count < max_entries; ++count) {
1453 uint64_t idx = pfn + count;
1455 if (pages_addr[idx] !=
1456 (pages_addr[idx - 1] + PAGE_SIZE))
1460 if (count < min_linear_pages) {
1461 addr = pfn << PAGE_SHIFT;
1462 dma_addr = pages_addr;
1464 addr = pages_addr[pfn];
1465 max_entries = count;
1468 } else if (flags & AMDGPU_PTE_VALID) {
1469 addr += adev->vm_manager.vram_base_offset;
1470 addr += pfn << PAGE_SHIFT;
1473 last = min((uint64_t)mapping->last, start + max_entries - 1);
1474 r = amdgpu_vm_bo_update_mapping(adev, exclusive, dma_addr, vm,
1475 start, last, flags, addr,
1480 pfn += last - start + 1;
1481 if (nodes && nodes->size == pfn) {
1487 } while (unlikely(start != mapping->last + 1));
1493 * amdgpu_vm_bo_update - update all BO mappings in the vm page table
1495 * @adev: amdgpu_device pointer
1496 * @bo_va: requested BO and VM object
1497 * @clear: if true clear the entries
1499 * Fill in the page table entries for @bo_va.
1500 * Returns 0 for success, -EINVAL for failure.
1502 int amdgpu_vm_bo_update(struct amdgpu_device *adev,
1503 struct amdgpu_bo_va *bo_va,
1506 struct amdgpu_bo *bo = bo_va->base.bo;
1507 struct amdgpu_vm *vm = bo_va->base.vm;
1508 struct amdgpu_bo_va_mapping *mapping;
1509 dma_addr_t *pages_addr = NULL;
1510 struct ttm_mem_reg *mem;
1511 struct drm_mm_node *nodes;
1512 struct dma_fence *exclusive, **last_update;
1516 if (clear || !bo_va->base.bo) {
1521 struct ttm_dma_tt *ttm;
1523 mem = &bo_va->base.bo->tbo.mem;
1524 nodes = mem->mm_node;
1525 if (mem->mem_type == TTM_PL_TT) {
1526 ttm = container_of(bo_va->base.bo->tbo.ttm,
1527 struct ttm_dma_tt, ttm);
1528 pages_addr = ttm->dma_address;
1530 exclusive = reservation_object_get_excl(bo->tbo.resv);
1534 flags = amdgpu_ttm_tt_pte_flags(adev, bo->tbo.ttm, mem);
1538 if (clear || (bo && bo->tbo.resv == vm->root.base.bo->tbo.resv))
1539 last_update = &vm->last_update;
1541 last_update = &bo_va->last_pt_update;
1543 if (!clear && bo_va->base.moved) {
1544 bo_va->base.moved = false;
1545 list_splice_init(&bo_va->valids, &bo_va->invalids);
1547 } else if (bo_va->cleared != clear) {
1548 list_splice_init(&bo_va->valids, &bo_va->invalids);
1551 list_for_each_entry(mapping, &bo_va->invalids, list) {
1552 r = amdgpu_vm_bo_split_mapping(adev, exclusive, pages_addr, vm,
1553 mapping, flags, nodes,
1559 if (vm->use_cpu_for_update) {
1562 amdgpu_asic_flush_hdp(adev, NULL);
1565 spin_lock(&vm->moved_lock);
1566 list_del_init(&bo_va->base.vm_status);
1567 spin_unlock(&vm->moved_lock);
1569 /* If the BO is not in its preferred location add it back to
1570 * the evicted list so that it gets validated again on the
1571 * next command submission.
1573 if (bo && bo->tbo.resv == vm->root.base.bo->tbo.resv &&
1574 !(bo->preferred_domains &
1575 amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type)))
1576 list_add_tail(&bo_va->base.vm_status, &vm->evicted);
1578 list_splice_init(&bo_va->invalids, &bo_va->valids);
1579 bo_va->cleared = clear;
1581 if (trace_amdgpu_vm_bo_mapping_enabled()) {
1582 list_for_each_entry(mapping, &bo_va->valids, list)
1583 trace_amdgpu_vm_bo_mapping(mapping);
1590 * amdgpu_vm_update_prt_state - update the global PRT state
1592 static void amdgpu_vm_update_prt_state(struct amdgpu_device *adev)
1594 unsigned long flags;
1597 spin_lock_irqsave(&adev->vm_manager.prt_lock, flags);
1598 enable = !!atomic_read(&adev->vm_manager.num_prt_users);
1599 adev->gmc.gmc_funcs->set_prt(adev, enable);
1600 spin_unlock_irqrestore(&adev->vm_manager.prt_lock, flags);
1604 * amdgpu_vm_prt_get - add a PRT user
1606 static void amdgpu_vm_prt_get(struct amdgpu_device *adev)
1608 if (!adev->gmc.gmc_funcs->set_prt)
1611 if (atomic_inc_return(&adev->vm_manager.num_prt_users) == 1)
1612 amdgpu_vm_update_prt_state(adev);
1616 * amdgpu_vm_prt_put - drop a PRT user
1618 static void amdgpu_vm_prt_put(struct amdgpu_device *adev)
1620 if (atomic_dec_return(&adev->vm_manager.num_prt_users) == 0)
1621 amdgpu_vm_update_prt_state(adev);
1625 * amdgpu_vm_prt_cb - callback for updating the PRT status
1627 static void amdgpu_vm_prt_cb(struct dma_fence *fence, struct dma_fence_cb *_cb)
1629 struct amdgpu_prt_cb *cb = container_of(_cb, struct amdgpu_prt_cb, cb);
1631 amdgpu_vm_prt_put(cb->adev);
1636 * amdgpu_vm_add_prt_cb - add callback for updating the PRT status
1638 static void amdgpu_vm_add_prt_cb(struct amdgpu_device *adev,
1639 struct dma_fence *fence)
1641 struct amdgpu_prt_cb *cb;
1643 if (!adev->gmc.gmc_funcs->set_prt)
1646 cb = kmalloc(sizeof(struct amdgpu_prt_cb), GFP_KERNEL);
1648 /* Last resort when we are OOM */
1650 dma_fence_wait(fence, false);
1652 amdgpu_vm_prt_put(adev);
1655 if (!fence || dma_fence_add_callback(fence, &cb->cb,
1657 amdgpu_vm_prt_cb(fence, &cb->cb);
1662 * amdgpu_vm_free_mapping - free a mapping
1664 * @adev: amdgpu_device pointer
1666 * @mapping: mapping to be freed
1667 * @fence: fence of the unmap operation
1669 * Free a mapping and make sure we decrease the PRT usage count if applicable.
1671 static void amdgpu_vm_free_mapping(struct amdgpu_device *adev,
1672 struct amdgpu_vm *vm,
1673 struct amdgpu_bo_va_mapping *mapping,
1674 struct dma_fence *fence)
1676 if (mapping->flags & AMDGPU_PTE_PRT)
1677 amdgpu_vm_add_prt_cb(adev, fence);
1682 * amdgpu_vm_prt_fini - finish all prt mappings
1684 * @adev: amdgpu_device pointer
1687 * Register a cleanup callback to disable PRT support after VM dies.
1689 static void amdgpu_vm_prt_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1691 struct reservation_object *resv = vm->root.base.bo->tbo.resv;
1692 struct dma_fence *excl, **shared;
1693 unsigned i, shared_count;
1696 r = reservation_object_get_fences_rcu(resv, &excl,
1697 &shared_count, &shared);
1699 /* Not enough memory to grab the fence list, as last resort
1700 * block for all the fences to complete.
1702 reservation_object_wait_timeout_rcu(resv, true, false,
1703 MAX_SCHEDULE_TIMEOUT);
1707 /* Add a callback for each fence in the reservation object */
1708 amdgpu_vm_prt_get(adev);
1709 amdgpu_vm_add_prt_cb(adev, excl);
1711 for (i = 0; i < shared_count; ++i) {
1712 amdgpu_vm_prt_get(adev);
1713 amdgpu_vm_add_prt_cb(adev, shared[i]);
1720 * amdgpu_vm_clear_freed - clear freed BOs in the PT
1722 * @adev: amdgpu_device pointer
1724 * @fence: optional resulting fence (unchanged if no work needed to be done
1725 * or if an error occurred)
1727 * Make sure all freed BOs are cleared in the PT.
1728 * Returns 0 for success.
1730 * PTs have to be reserved and mutex must be locked!
1732 int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
1733 struct amdgpu_vm *vm,
1734 struct dma_fence **fence)
1736 struct amdgpu_bo_va_mapping *mapping;
1737 uint64_t init_pte_value = 0;
1738 struct dma_fence *f = NULL;
1741 while (!list_empty(&vm->freed)) {
1742 mapping = list_first_entry(&vm->freed,
1743 struct amdgpu_bo_va_mapping, list);
1744 list_del(&mapping->list);
1746 if (vm->pte_support_ats && mapping->start < AMDGPU_VA_HOLE_START)
1747 init_pte_value = AMDGPU_PTE_DEFAULT_ATC;
1749 r = amdgpu_vm_bo_update_mapping(adev, NULL, NULL, vm,
1750 mapping->start, mapping->last,
1751 init_pte_value, 0, &f);
1752 amdgpu_vm_free_mapping(adev, vm, mapping, f);
1760 dma_fence_put(*fence);
1771 * amdgpu_vm_handle_moved - handle moved BOs in the PT
1773 * @adev: amdgpu_device pointer
1775 * @sync: sync object to add fences to
1777 * Make sure all BOs which are moved are updated in the PTs.
1778 * Returns 0 for success.
1780 * PTs have to be reserved!
1782 int amdgpu_vm_handle_moved(struct amdgpu_device *adev,
1783 struct amdgpu_vm *vm)
1785 struct amdgpu_bo_va *bo_va, *tmp;
1786 struct list_head moved;
1790 INIT_LIST_HEAD(&moved);
1791 spin_lock(&vm->moved_lock);
1792 list_splice_init(&vm->moved, &moved);
1793 spin_unlock(&vm->moved_lock);
1795 list_for_each_entry_safe(bo_va, tmp, &moved, base.vm_status) {
1796 struct reservation_object *resv = bo_va->base.bo->tbo.resv;
1798 /* Per VM BOs never need to bo cleared in the page tables */
1799 if (resv == vm->root.base.bo->tbo.resv)
1801 /* Try to reserve the BO to avoid clearing its ptes */
1802 else if (!amdgpu_vm_debug && reservation_object_trylock(resv))
1804 /* Somebody else is using the BO right now */
1808 r = amdgpu_vm_bo_update(adev, bo_va, clear);
1810 spin_lock(&vm->moved_lock);
1811 list_splice(&moved, &vm->moved);
1812 spin_unlock(&vm->moved_lock);
1816 if (!clear && resv != vm->root.base.bo->tbo.resv)
1817 reservation_object_unlock(resv);
1825 * amdgpu_vm_bo_add - add a bo to a specific vm
1827 * @adev: amdgpu_device pointer
1829 * @bo: amdgpu buffer object
1831 * Add @bo into the requested vm.
1832 * Add @bo to the list of bos associated with the vm
1833 * Returns newly added bo_va or NULL for failure
1835 * Object has to be reserved!
1837 struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
1838 struct amdgpu_vm *vm,
1839 struct amdgpu_bo *bo)
1841 struct amdgpu_bo_va *bo_va;
1843 bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL);
1844 if (bo_va == NULL) {
1847 amdgpu_vm_bo_base_init(&bo_va->base, vm, bo);
1849 bo_va->ref_count = 1;
1850 INIT_LIST_HEAD(&bo_va->valids);
1851 INIT_LIST_HEAD(&bo_va->invalids);
1858 * amdgpu_vm_bo_insert_mapping - insert a new mapping
1860 * @adev: amdgpu_device pointer
1861 * @bo_va: bo_va to store the address
1862 * @mapping: the mapping to insert
1864 * Insert a new mapping into all structures.
1866 static void amdgpu_vm_bo_insert_map(struct amdgpu_device *adev,
1867 struct amdgpu_bo_va *bo_va,
1868 struct amdgpu_bo_va_mapping *mapping)
1870 struct amdgpu_vm *vm = bo_va->base.vm;
1871 struct amdgpu_bo *bo = bo_va->base.bo;
1873 mapping->bo_va = bo_va;
1874 list_add(&mapping->list, &bo_va->invalids);
1875 amdgpu_vm_it_insert(mapping, &vm->va);
1877 if (mapping->flags & AMDGPU_PTE_PRT)
1878 amdgpu_vm_prt_get(adev);
1880 if (bo && bo->tbo.resv == vm->root.base.bo->tbo.resv) {
1881 spin_lock(&vm->moved_lock);
1882 if (list_empty(&bo_va->base.vm_status))
1883 list_add(&bo_va->base.vm_status, &vm->moved);
1884 spin_unlock(&vm->moved_lock);
1886 trace_amdgpu_vm_bo_map(bo_va, mapping);
1890 * amdgpu_vm_bo_map - map bo inside a vm
1892 * @adev: amdgpu_device pointer
1893 * @bo_va: bo_va to store the address
1894 * @saddr: where to map the BO
1895 * @offset: requested offset in the BO
1896 * @flags: attributes of pages (read/write/valid/etc.)
1898 * Add a mapping of the BO at the specefied addr into the VM.
1899 * Returns 0 for success, error for failure.
1901 * Object has to be reserved and unreserved outside!
1903 int amdgpu_vm_bo_map(struct amdgpu_device *adev,
1904 struct amdgpu_bo_va *bo_va,
1905 uint64_t saddr, uint64_t offset,
1906 uint64_t size, uint64_t flags)
1908 struct amdgpu_bo_va_mapping *mapping, *tmp;
1909 struct amdgpu_bo *bo = bo_va->base.bo;
1910 struct amdgpu_vm *vm = bo_va->base.vm;
1913 /* validate the parameters */
1914 if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
1915 size == 0 || size & AMDGPU_GPU_PAGE_MASK)
1918 /* make sure object fit at this offset */
1919 eaddr = saddr + size - 1;
1920 if (saddr >= eaddr ||
1921 (bo && offset + size > amdgpu_bo_size(bo)))
1924 saddr /= AMDGPU_GPU_PAGE_SIZE;
1925 eaddr /= AMDGPU_GPU_PAGE_SIZE;
1927 tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr);
1929 /* bo and tmp overlap, invalid addr */
1930 dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with "
1931 "0x%010Lx-0x%010Lx\n", bo, saddr, eaddr,
1932 tmp->start, tmp->last + 1);
1936 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
1940 mapping->start = saddr;
1941 mapping->last = eaddr;
1942 mapping->offset = offset;
1943 mapping->flags = flags;
1945 amdgpu_vm_bo_insert_map(adev, bo_va, mapping);
1951 * amdgpu_vm_bo_replace_map - map bo inside a vm, replacing existing mappings
1953 * @adev: amdgpu_device pointer
1954 * @bo_va: bo_va to store the address
1955 * @saddr: where to map the BO
1956 * @offset: requested offset in the BO
1957 * @flags: attributes of pages (read/write/valid/etc.)
1959 * Add a mapping of the BO at the specefied addr into the VM. Replace existing
1960 * mappings as we do so.
1961 * Returns 0 for success, error for failure.
1963 * Object has to be reserved and unreserved outside!
1965 int amdgpu_vm_bo_replace_map(struct amdgpu_device *adev,
1966 struct amdgpu_bo_va *bo_va,
1967 uint64_t saddr, uint64_t offset,
1968 uint64_t size, uint64_t flags)
1970 struct amdgpu_bo_va_mapping *mapping;
1971 struct amdgpu_bo *bo = bo_va->base.bo;
1975 /* validate the parameters */
1976 if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
1977 size == 0 || size & AMDGPU_GPU_PAGE_MASK)
1980 /* make sure object fit at this offset */
1981 eaddr = saddr + size - 1;
1982 if (saddr >= eaddr ||
1983 (bo && offset + size > amdgpu_bo_size(bo)))
1986 /* Allocate all the needed memory */
1987 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
1991 r = amdgpu_vm_bo_clear_mappings(adev, bo_va->base.vm, saddr, size);
1997 saddr /= AMDGPU_GPU_PAGE_SIZE;
1998 eaddr /= AMDGPU_GPU_PAGE_SIZE;
2000 mapping->start = saddr;
2001 mapping->last = eaddr;
2002 mapping->offset = offset;
2003 mapping->flags = flags;
2005 amdgpu_vm_bo_insert_map(adev, bo_va, mapping);
2011 * amdgpu_vm_bo_unmap - remove bo mapping from vm
2013 * @adev: amdgpu_device pointer
2014 * @bo_va: bo_va to remove the address from
2015 * @saddr: where to the BO is mapped
2017 * Remove a mapping of the BO at the specefied addr from the VM.
2018 * Returns 0 for success, error for failure.
2020 * Object has to be reserved and unreserved outside!
2022 int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
2023 struct amdgpu_bo_va *bo_va,
2026 struct amdgpu_bo_va_mapping *mapping;
2027 struct amdgpu_vm *vm = bo_va->base.vm;
2030 saddr /= AMDGPU_GPU_PAGE_SIZE;
2032 list_for_each_entry(mapping, &bo_va->valids, list) {
2033 if (mapping->start == saddr)
2037 if (&mapping->list == &bo_va->valids) {
2040 list_for_each_entry(mapping, &bo_va->invalids, list) {
2041 if (mapping->start == saddr)
2045 if (&mapping->list == &bo_va->invalids)
2049 list_del(&mapping->list);
2050 amdgpu_vm_it_remove(mapping, &vm->va);
2051 mapping->bo_va = NULL;
2052 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
2055 list_add(&mapping->list, &vm->freed);
2057 amdgpu_vm_free_mapping(adev, vm, mapping,
2058 bo_va->last_pt_update);
2064 * amdgpu_vm_bo_clear_mappings - remove all mappings in a specific range
2066 * @adev: amdgpu_device pointer
2067 * @vm: VM structure to use
2068 * @saddr: start of the range
2069 * @size: size of the range
2071 * Remove all mappings in a range, split them as appropriate.
2072 * Returns 0 for success, error for failure.
2074 int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev,
2075 struct amdgpu_vm *vm,
2076 uint64_t saddr, uint64_t size)
2078 struct amdgpu_bo_va_mapping *before, *after, *tmp, *next;
2082 eaddr = saddr + size - 1;
2083 saddr /= AMDGPU_GPU_PAGE_SIZE;
2084 eaddr /= AMDGPU_GPU_PAGE_SIZE;
2086 /* Allocate all the needed memory */
2087 before = kzalloc(sizeof(*before), GFP_KERNEL);
2090 INIT_LIST_HEAD(&before->list);
2092 after = kzalloc(sizeof(*after), GFP_KERNEL);
2097 INIT_LIST_HEAD(&after->list);
2099 /* Now gather all removed mappings */
2100 tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr);
2102 /* Remember mapping split at the start */
2103 if (tmp->start < saddr) {
2104 before->start = tmp->start;
2105 before->last = saddr - 1;
2106 before->offset = tmp->offset;
2107 before->flags = tmp->flags;
2108 list_add(&before->list, &tmp->list);
2111 /* Remember mapping split at the end */
2112 if (tmp->last > eaddr) {
2113 after->start = eaddr + 1;
2114 after->last = tmp->last;
2115 after->offset = tmp->offset;
2116 after->offset += after->start - tmp->start;
2117 after->flags = tmp->flags;
2118 list_add(&after->list, &tmp->list);
2121 list_del(&tmp->list);
2122 list_add(&tmp->list, &removed);
2124 tmp = amdgpu_vm_it_iter_next(tmp, saddr, eaddr);
2127 /* And free them up */
2128 list_for_each_entry_safe(tmp, next, &removed, list) {
2129 amdgpu_vm_it_remove(tmp, &vm->va);
2130 list_del(&tmp->list);
2132 if (tmp->start < saddr)
2134 if (tmp->last > eaddr)
2138 list_add(&tmp->list, &vm->freed);
2139 trace_amdgpu_vm_bo_unmap(NULL, tmp);
2142 /* Insert partial mapping before the range */
2143 if (!list_empty(&before->list)) {
2144 amdgpu_vm_it_insert(before, &vm->va);
2145 if (before->flags & AMDGPU_PTE_PRT)
2146 amdgpu_vm_prt_get(adev);
2151 /* Insert partial mapping after the range */
2152 if (!list_empty(&after->list)) {
2153 amdgpu_vm_it_insert(after, &vm->va);
2154 if (after->flags & AMDGPU_PTE_PRT)
2155 amdgpu_vm_prt_get(adev);
2164 * amdgpu_vm_bo_lookup_mapping - find mapping by address
2166 * @vm: the requested VM
2168 * Find a mapping by it's address.
2170 struct amdgpu_bo_va_mapping *amdgpu_vm_bo_lookup_mapping(struct amdgpu_vm *vm,
2173 return amdgpu_vm_it_iter_first(&vm->va, addr, addr);
2177 * amdgpu_vm_bo_rmv - remove a bo to a specific vm
2179 * @adev: amdgpu_device pointer
2180 * @bo_va: requested bo_va
2182 * Remove @bo_va->bo from the requested vm.
2184 * Object have to be reserved!
2186 void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
2187 struct amdgpu_bo_va *bo_va)
2189 struct amdgpu_bo_va_mapping *mapping, *next;
2190 struct amdgpu_vm *vm = bo_va->base.vm;
2192 list_del(&bo_va->base.bo_list);
2194 spin_lock(&vm->moved_lock);
2195 list_del(&bo_va->base.vm_status);
2196 spin_unlock(&vm->moved_lock);
2198 list_for_each_entry_safe(mapping, next, &bo_va->valids, list) {
2199 list_del(&mapping->list);
2200 amdgpu_vm_it_remove(mapping, &vm->va);
2201 mapping->bo_va = NULL;
2202 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
2203 list_add(&mapping->list, &vm->freed);
2205 list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) {
2206 list_del(&mapping->list);
2207 amdgpu_vm_it_remove(mapping, &vm->va);
2208 amdgpu_vm_free_mapping(adev, vm, mapping,
2209 bo_va->last_pt_update);
2212 dma_fence_put(bo_va->last_pt_update);
2217 * amdgpu_vm_bo_invalidate - mark the bo as invalid
2219 * @adev: amdgpu_device pointer
2221 * @bo: amdgpu buffer object
2223 * Mark @bo as invalid.
2225 void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
2226 struct amdgpu_bo *bo, bool evicted)
2228 struct amdgpu_vm_bo_base *bo_base;
2230 /* shadow bo doesn't have bo base, its validation needs its parent */
2231 if (bo->parent && bo->parent->shadow == bo)
2234 list_for_each_entry(bo_base, &bo->va, bo_list) {
2235 struct amdgpu_vm *vm = bo_base->vm;
2237 bo_base->moved = true;
2238 if (evicted && bo->tbo.resv == vm->root.base.bo->tbo.resv) {
2239 if (bo->tbo.type == ttm_bo_type_kernel)
2240 list_move(&bo_base->vm_status, &vm->evicted);
2242 list_move_tail(&bo_base->vm_status,
2247 if (bo->tbo.type == ttm_bo_type_kernel) {
2248 if (list_empty(&bo_base->vm_status))
2249 list_add(&bo_base->vm_status, &vm->relocated);
2253 spin_lock(&bo_base->vm->moved_lock);
2254 if (list_empty(&bo_base->vm_status))
2255 list_add(&bo_base->vm_status, &vm->moved);
2256 spin_unlock(&bo_base->vm->moved_lock);
2260 static uint32_t amdgpu_vm_get_block_size(uint64_t vm_size)
2262 /* Total bits covered by PD + PTs */
2263 unsigned bits = ilog2(vm_size) + 18;
2265 /* Make sure the PD is 4K in size up to 8GB address space.
2266 Above that split equal between PD and PTs */
2270 return ((bits + 3) / 2);
2274 * amdgpu_vm_adjust_size - adjust vm size, block size and fragment size
2276 * @adev: amdgpu_device pointer
2277 * @vm_size: the default vm size if it's set auto
2279 void amdgpu_vm_adjust_size(struct amdgpu_device *adev, uint32_t vm_size,
2280 uint32_t fragment_size_default, unsigned max_level,
2285 /* adjust vm size first */
2286 if (amdgpu_vm_size != -1) {
2287 unsigned max_size = 1 << (max_bits - 30);
2289 vm_size = amdgpu_vm_size;
2290 if (vm_size > max_size) {
2291 dev_warn(adev->dev, "VM size (%d) too large, max is %u GB\n",
2292 amdgpu_vm_size, max_size);
2297 adev->vm_manager.max_pfn = (uint64_t)vm_size << 18;
2299 tmp = roundup_pow_of_two(adev->vm_manager.max_pfn);
2300 if (amdgpu_vm_block_size != -1)
2301 tmp >>= amdgpu_vm_block_size - 9;
2302 tmp = DIV_ROUND_UP(fls64(tmp) - 1, 9) - 1;
2303 adev->vm_manager.num_level = min(max_level, (unsigned)tmp);
2304 switch (adev->vm_manager.num_level) {
2306 adev->vm_manager.root_level = AMDGPU_VM_PDB2;
2309 adev->vm_manager.root_level = AMDGPU_VM_PDB1;
2312 adev->vm_manager.root_level = AMDGPU_VM_PDB0;
2315 dev_err(adev->dev, "VMPT only supports 2~4+1 levels\n");
2317 /* block size depends on vm size and hw setup*/
2318 if (amdgpu_vm_block_size != -1)
2319 adev->vm_manager.block_size =
2320 min((unsigned)amdgpu_vm_block_size, max_bits
2321 - AMDGPU_GPU_PAGE_SHIFT
2322 - 9 * adev->vm_manager.num_level);
2323 else if (adev->vm_manager.num_level > 1)
2324 adev->vm_manager.block_size = 9;
2326 adev->vm_manager.block_size = amdgpu_vm_get_block_size(tmp);
2328 if (amdgpu_vm_fragment_size == -1)
2329 adev->vm_manager.fragment_size = fragment_size_default;
2331 adev->vm_manager.fragment_size = amdgpu_vm_fragment_size;
2333 DRM_INFO("vm size is %u GB, %u levels, block size is %u-bit, fragment size is %u-bit\n",
2334 vm_size, adev->vm_manager.num_level + 1,
2335 adev->vm_manager.block_size,
2336 adev->vm_manager.fragment_size);
2340 * amdgpu_vm_init - initialize a vm instance
2342 * @adev: amdgpu_device pointer
2344 * @vm_context: Indicates if it GFX or Compute context
2348 int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
2349 int vm_context, unsigned int pasid)
2351 struct amdgpu_bo_param bp;
2352 struct amdgpu_bo *root;
2353 const unsigned align = min(AMDGPU_VM_PTB_ALIGN_SIZE,
2354 AMDGPU_VM_PTE_COUNT(adev) * 8);
2355 unsigned ring_instance;
2356 struct amdgpu_ring *ring;
2357 struct drm_sched_rq *rq;
2362 vm->va = RB_ROOT_CACHED;
2363 for (i = 0; i < AMDGPU_MAX_VMHUBS; i++)
2364 vm->reserved_vmid[i] = NULL;
2365 INIT_LIST_HEAD(&vm->evicted);
2366 INIT_LIST_HEAD(&vm->relocated);
2367 spin_lock_init(&vm->moved_lock);
2368 INIT_LIST_HEAD(&vm->moved);
2369 INIT_LIST_HEAD(&vm->freed);
2371 /* create scheduler entity for page table updates */
2373 ring_instance = atomic_inc_return(&adev->vm_manager.vm_pte_next_ring);
2374 ring_instance %= adev->vm_manager.vm_pte_num_rings;
2375 ring = adev->vm_manager.vm_pte_rings[ring_instance];
2376 rq = &ring->sched.sched_rq[DRM_SCHED_PRIORITY_KERNEL];
2377 r = drm_sched_entity_init(&ring->sched, &vm->entity,
2382 vm->pte_support_ats = false;
2384 if (vm_context == AMDGPU_VM_CONTEXT_COMPUTE) {
2385 vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode &
2386 AMDGPU_VM_USE_CPU_FOR_COMPUTE);
2388 if (adev->asic_type == CHIP_RAVEN)
2389 vm->pte_support_ats = true;
2391 vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode &
2392 AMDGPU_VM_USE_CPU_FOR_GFX);
2394 DRM_DEBUG_DRIVER("VM update mode is %s\n",
2395 vm->use_cpu_for_update ? "CPU" : "SDMA");
2396 WARN_ONCE((vm->use_cpu_for_update & !amdgpu_vm_is_large_bar(adev)),
2397 "CPU update of VM recommended only for large BAR system\n");
2398 vm->last_update = NULL;
2400 flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
2401 if (vm->use_cpu_for_update)
2402 flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
2404 flags |= AMDGPU_GEM_CREATE_SHADOW;
2406 size = amdgpu_vm_bo_size(adev, adev->vm_manager.root_level);
2407 memset(&bp, 0, sizeof(bp));
2409 bp.byte_align = align;
2410 bp.domain = AMDGPU_GEM_DOMAIN_VRAM;
2412 bp.type = ttm_bo_type_kernel;
2414 r = amdgpu_bo_create(adev, &bp, &root);
2416 goto error_free_sched_entity;
2418 r = amdgpu_bo_reserve(root, true);
2420 goto error_free_root;
2422 r = amdgpu_vm_clear_bo(adev, vm, root,
2423 adev->vm_manager.root_level,
2424 vm->pte_support_ats);
2426 goto error_unreserve;
2428 amdgpu_vm_bo_base_init(&vm->root.base, vm, root);
2429 amdgpu_bo_unreserve(vm->root.base.bo);
2432 unsigned long flags;
2434 spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags);
2435 r = idr_alloc(&adev->vm_manager.pasid_idr, vm, pasid, pasid + 1,
2437 spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags);
2439 goto error_free_root;
2444 INIT_KFIFO(vm->faults);
2445 vm->fault_credit = 16;
2450 amdgpu_bo_unreserve(vm->root.base.bo);
2453 amdgpu_bo_unref(&vm->root.base.bo->shadow);
2454 amdgpu_bo_unref(&vm->root.base.bo);
2455 vm->root.base.bo = NULL;
2457 error_free_sched_entity:
2458 drm_sched_entity_fini(&ring->sched, &vm->entity);
2464 * amdgpu_vm_make_compute - Turn a GFX VM into a compute VM
2466 * This only works on GFX VMs that don't have any BOs added and no
2467 * page tables allocated yet.
2469 * Changes the following VM parameters:
2470 * - use_cpu_for_update
2471 * - pte_supports_ats
2472 * - pasid (old PASID is released, because compute manages its own PASIDs)
2474 * Reinitializes the page directory to reflect the changed ATS
2475 * setting. May leave behind an unused shadow BO for the page
2476 * directory when switching from SDMA updates to CPU updates.
2478 * Returns 0 for success, -errno for errors.
2480 int amdgpu_vm_make_compute(struct amdgpu_device *adev, struct amdgpu_vm *vm)
2482 bool pte_support_ats = (adev->asic_type == CHIP_RAVEN);
2485 r = amdgpu_bo_reserve(vm->root.base.bo, true);
2490 if (!RB_EMPTY_ROOT(&vm->va.rb_root) || vm->root.entries) {
2495 /* Check if PD needs to be reinitialized and do it before
2496 * changing any other state, in case it fails.
2498 if (pte_support_ats != vm->pte_support_ats) {
2499 r = amdgpu_vm_clear_bo(adev, vm, vm->root.base.bo,
2500 adev->vm_manager.root_level,
2506 /* Update VM state */
2507 vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode &
2508 AMDGPU_VM_USE_CPU_FOR_COMPUTE);
2509 vm->pte_support_ats = pte_support_ats;
2510 DRM_DEBUG_DRIVER("VM update mode is %s\n",
2511 vm->use_cpu_for_update ? "CPU" : "SDMA");
2512 WARN_ONCE((vm->use_cpu_for_update & !amdgpu_vm_is_large_bar(adev)),
2513 "CPU update of VM recommended only for large BAR system\n");
2516 unsigned long flags;
2518 spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags);
2519 idr_remove(&adev->vm_manager.pasid_idr, vm->pasid);
2520 spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags);
2526 amdgpu_bo_unreserve(vm->root.base.bo);
2531 * amdgpu_vm_free_levels - free PD/PT levels
2533 * @adev: amdgpu device structure
2534 * @parent: PD/PT starting level to free
2535 * @level: level of parent structure
2537 * Free the page directory or page table level and all sub levels.
2539 static void amdgpu_vm_free_levels(struct amdgpu_device *adev,
2540 struct amdgpu_vm_pt *parent,
2543 unsigned i, num_entries = amdgpu_vm_num_entries(adev, level);
2545 if (parent->base.bo) {
2546 list_del(&parent->base.bo_list);
2547 list_del(&parent->base.vm_status);
2548 amdgpu_bo_unref(&parent->base.bo->shadow);
2549 amdgpu_bo_unref(&parent->base.bo);
2552 if (parent->entries)
2553 for (i = 0; i < num_entries; i++)
2554 amdgpu_vm_free_levels(adev, &parent->entries[i],
2557 kvfree(parent->entries);
2561 * amdgpu_vm_fini - tear down a vm instance
2563 * @adev: amdgpu_device pointer
2567 * Unbind the VM and remove all bos from the vm bo list
2569 void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
2571 struct amdgpu_bo_va_mapping *mapping, *tmp;
2572 bool prt_fini_needed = !!adev->gmc.gmc_funcs->set_prt;
2573 struct amdgpu_bo *root;
2577 amdgpu_amdkfd_gpuvm_destroy_cb(adev, vm);
2579 /* Clear pending page faults from IH when the VM is destroyed */
2580 while (kfifo_get(&vm->faults, &fault))
2581 amdgpu_ih_clear_fault(adev, fault);
2584 unsigned long flags;
2586 spin_lock_irqsave(&adev->vm_manager.pasid_lock, flags);
2587 idr_remove(&adev->vm_manager.pasid_idr, vm->pasid);
2588 spin_unlock_irqrestore(&adev->vm_manager.pasid_lock, flags);
2591 drm_sched_entity_fini(vm->entity.sched, &vm->entity);
2593 if (!RB_EMPTY_ROOT(&vm->va.rb_root)) {
2594 dev_err(adev->dev, "still active bo inside vm\n");
2596 rbtree_postorder_for_each_entry_safe(mapping, tmp,
2597 &vm->va.rb_root, rb) {
2598 list_del(&mapping->list);
2599 amdgpu_vm_it_remove(mapping, &vm->va);
2602 list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
2603 if (mapping->flags & AMDGPU_PTE_PRT && prt_fini_needed) {
2604 amdgpu_vm_prt_fini(adev, vm);
2605 prt_fini_needed = false;
2608 list_del(&mapping->list);
2609 amdgpu_vm_free_mapping(adev, vm, mapping, NULL);
2612 root = amdgpu_bo_ref(vm->root.base.bo);
2613 r = amdgpu_bo_reserve(root, true);
2615 dev_err(adev->dev, "Leaking page tables because BO reservation failed\n");
2617 amdgpu_vm_free_levels(adev, &vm->root,
2618 adev->vm_manager.root_level);
2619 amdgpu_bo_unreserve(root);
2621 amdgpu_bo_unref(&root);
2622 dma_fence_put(vm->last_update);
2623 for (i = 0; i < AMDGPU_MAX_VMHUBS; i++)
2624 amdgpu_vmid_free_reserved(adev, vm, i);
2628 * amdgpu_vm_pasid_fault_credit - Check fault credit for given PASID
2630 * @adev: amdgpu_device pointer
2631 * @pasid: PASID do identify the VM
2633 * This function is expected to be called in interrupt context. Returns
2634 * true if there was fault credit, false otherwise
2636 bool amdgpu_vm_pasid_fault_credit(struct amdgpu_device *adev,
2639 struct amdgpu_vm *vm;
2641 spin_lock(&adev->vm_manager.pasid_lock);
2642 vm = idr_find(&adev->vm_manager.pasid_idr, pasid);
2644 /* VM not found, can't track fault credit */
2645 spin_unlock(&adev->vm_manager.pasid_lock);
2649 /* No lock needed. only accessed by IRQ handler */
2650 if (!vm->fault_credit) {
2651 /* Too many faults in this VM */
2652 spin_unlock(&adev->vm_manager.pasid_lock);
2657 spin_unlock(&adev->vm_manager.pasid_lock);
2662 * amdgpu_vm_manager_init - init the VM manager
2664 * @adev: amdgpu_device pointer
2666 * Initialize the VM manager structures
2668 void amdgpu_vm_manager_init(struct amdgpu_device *adev)
2672 amdgpu_vmid_mgr_init(adev);
2674 adev->vm_manager.fence_context =
2675 dma_fence_context_alloc(AMDGPU_MAX_RINGS);
2676 for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
2677 adev->vm_manager.seqno[i] = 0;
2679 atomic_set(&adev->vm_manager.vm_pte_next_ring, 0);
2680 spin_lock_init(&adev->vm_manager.prt_lock);
2681 atomic_set(&adev->vm_manager.num_prt_users, 0);
2683 /* If not overridden by the user, by default, only in large BAR systems
2684 * Compute VM tables will be updated by CPU
2686 #ifdef CONFIG_X86_64
2687 if (amdgpu_vm_update_mode == -1) {
2688 if (amdgpu_vm_is_large_bar(adev))
2689 adev->vm_manager.vm_update_mode =
2690 AMDGPU_VM_USE_CPU_FOR_COMPUTE;
2692 adev->vm_manager.vm_update_mode = 0;
2694 adev->vm_manager.vm_update_mode = amdgpu_vm_update_mode;
2696 adev->vm_manager.vm_update_mode = 0;
2699 idr_init(&adev->vm_manager.pasid_idr);
2700 spin_lock_init(&adev->vm_manager.pasid_lock);
2704 * amdgpu_vm_manager_fini - cleanup VM manager
2706 * @adev: amdgpu_device pointer
2708 * Cleanup the VM manager and free resources.
2710 void amdgpu_vm_manager_fini(struct amdgpu_device *adev)
2712 WARN_ON(!idr_is_empty(&adev->vm_manager.pasid_idr));
2713 idr_destroy(&adev->vm_manager.pasid_idr);
2715 amdgpu_vmid_mgr_fini(adev);
2718 int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
2720 union drm_amdgpu_vm *args = data;
2721 struct amdgpu_device *adev = dev->dev_private;
2722 struct amdgpu_fpriv *fpriv = filp->driver_priv;
2725 switch (args->in.op) {
2726 case AMDGPU_VM_OP_RESERVE_VMID:
2727 /* current, we only have requirement to reserve vmid from gfxhub */
2728 r = amdgpu_vmid_alloc_reserved(adev, &fpriv->vm, AMDGPU_GFXHUB);
2732 case AMDGPU_VM_OP_UNRESERVE_VMID:
2733 amdgpu_vmid_free_reserved(adev, &fpriv->vm, AMDGPU_GFXHUB);