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>
31 #include <drm/amdgpu_drm.h>
33 #include "amdgpu_trace.h"
37 * GPUVM is similar to the legacy gart on older asics, however
38 * rather than there being a single global gart table
39 * for the entire GPU, there are multiple VM page tables active
40 * at any given time. The VM page tables can contain a mix
41 * vram pages and system memory pages and system memory pages
42 * can be mapped as snooped (cached system pages) or unsnooped
43 * (uncached system pages).
44 * Each VM has an ID associated with it and there is a page table
45 * associated with each VMID. When execting a command buffer,
46 * the kernel tells the the ring what VMID to use for that command
47 * buffer. VMIDs are allocated dynamically as commands are submitted.
48 * The userspace drivers maintain their own address space and the kernel
49 * sets up their pages tables accordingly when they submit their
50 * command buffers and a VMID is assigned.
51 * Cayman/Trinity support up to 8 active VMs at any given time;
55 #define START(node) ((node)->start)
56 #define LAST(node) ((node)->last)
58 INTERVAL_TREE_DEFINE(struct amdgpu_bo_va_mapping, rb, uint64_t, __subtree_last,
59 START, LAST, static, amdgpu_vm_it)
64 /* Local structure. Encapsulate some VM table update parameters to reduce
65 * the number of function parameters
67 struct amdgpu_pte_update_params {
68 /* amdgpu device we do this update for */
69 struct amdgpu_device *adev;
70 /* optional amdgpu_vm we do this update for */
72 /* address where to copy page table entries from */
74 /* indirect buffer to fill with commands */
76 /* Function which actually does the update */
77 void (*func)(struct amdgpu_pte_update_params *params, uint64_t pe,
78 uint64_t addr, unsigned count, uint32_t incr,
80 /* The next two are used during VM update by CPU
81 * DMA addresses to use for mapping
82 * Kernel pointer of PD/PT BO that needs to be updated
84 dma_addr_t *pages_addr;
88 /* Helper to disable partial resident texture feature from a fence callback */
89 struct amdgpu_prt_cb {
90 struct amdgpu_device *adev;
91 struct dma_fence_cb cb;
95 * amdgpu_vm_num_entries - return the number of entries in a PD/PT
97 * @adev: amdgpu_device pointer
99 * Calculate the number of entries in a page directory or page table.
101 static unsigned amdgpu_vm_num_entries(struct amdgpu_device *adev,
105 /* For the root directory */
106 return adev->vm_manager.max_pfn >>
107 (adev->vm_manager.block_size *
108 adev->vm_manager.num_level);
109 else if (level == adev->vm_manager.num_level)
110 /* For the page tables on the leaves */
111 return AMDGPU_VM_PTE_COUNT(adev);
113 /* Everything in between */
114 return 1 << adev->vm_manager.block_size;
118 * amdgpu_vm_bo_size - returns the size of the BOs in bytes
120 * @adev: amdgpu_device pointer
122 * Calculate the size of the BO for a page directory or page table in bytes.
124 static unsigned amdgpu_vm_bo_size(struct amdgpu_device *adev, unsigned level)
126 return AMDGPU_GPU_PAGE_ALIGN(amdgpu_vm_num_entries(adev, level) * 8);
130 * amdgpu_vm_get_pd_bo - add the VM PD to a validation list
132 * @vm: vm providing the BOs
133 * @validated: head of validation list
134 * @entry: entry to add
136 * Add the page directory to the list of BOs to
137 * validate for command submission.
139 void amdgpu_vm_get_pd_bo(struct amdgpu_vm *vm,
140 struct list_head *validated,
141 struct amdgpu_bo_list_entry *entry)
143 entry->robj = vm->root.bo;
145 entry->tv.bo = &entry->robj->tbo;
146 entry->tv.shared = true;
147 entry->user_pages = NULL;
148 list_add(&entry->tv.head, validated);
152 * amdgpu_vm_validate_layer - validate a single page table level
154 * @parent: parent page table level
155 * @validate: callback to do the validation
156 * @param: parameter for the validation callback
158 * Validate the page table BOs on command submission if neccessary.
160 static int amdgpu_vm_validate_level(struct amdgpu_vm_pt *parent,
161 int (*validate)(void *, struct amdgpu_bo *),
167 if (!parent->entries)
170 for (i = 0; i <= parent->last_entry_used; ++i) {
171 struct amdgpu_vm_pt *entry = &parent->entries[i];
176 r = validate(param, entry->bo);
181 * Recurse into the sub directory. This is harmless because we
182 * have only a maximum of 5 layers.
184 r = amdgpu_vm_validate_level(entry, validate, param);
193 * amdgpu_vm_validate_pt_bos - validate the page table BOs
195 * @adev: amdgpu device pointer
196 * @vm: vm providing the BOs
197 * @validate: callback to do the validation
198 * @param: parameter for the validation callback
200 * Validate the page table BOs on command submission if neccessary.
202 int amdgpu_vm_validate_pt_bos(struct amdgpu_device *adev, struct amdgpu_vm *vm,
203 int (*validate)(void *p, struct amdgpu_bo *bo),
206 uint64_t num_evictions;
208 /* We only need to validate the page tables
209 * if they aren't already valid.
211 num_evictions = atomic64_read(&adev->num_evictions);
212 if (num_evictions == vm->last_eviction_counter)
215 return amdgpu_vm_validate_level(&vm->root, validate, param);
219 * amdgpu_vm_move_level_in_lru - move one level of PT BOs to the LRU tail
221 * @adev: amdgpu device instance
222 * @vm: vm providing the BOs
224 * Move the PT BOs to the tail of the LRU.
226 static void amdgpu_vm_move_level_in_lru(struct amdgpu_vm_pt *parent)
230 if (!parent->entries)
233 for (i = 0; i <= parent->last_entry_used; ++i) {
234 struct amdgpu_vm_pt *entry = &parent->entries[i];
239 ttm_bo_move_to_lru_tail(&entry->bo->tbo);
240 amdgpu_vm_move_level_in_lru(entry);
245 * amdgpu_vm_move_pt_bos_in_lru - move the PT BOs to the LRU tail
247 * @adev: amdgpu device instance
248 * @vm: vm providing the BOs
250 * Move the PT BOs to the tail of the LRU.
252 void amdgpu_vm_move_pt_bos_in_lru(struct amdgpu_device *adev,
253 struct amdgpu_vm *vm)
255 struct ttm_bo_global *glob = adev->mman.bdev.glob;
257 spin_lock(&glob->lru_lock);
258 amdgpu_vm_move_level_in_lru(&vm->root);
259 spin_unlock(&glob->lru_lock);
263 * amdgpu_vm_alloc_levels - allocate the PD/PT levels
265 * @adev: amdgpu_device pointer
267 * @saddr: start of the address range
268 * @eaddr: end of the address range
270 * Make sure the page directories and page tables are allocated
272 static int amdgpu_vm_alloc_levels(struct amdgpu_device *adev,
273 struct amdgpu_vm *vm,
274 struct amdgpu_vm_pt *parent,
275 uint64_t saddr, uint64_t eaddr,
278 unsigned shift = (adev->vm_manager.num_level - level) *
279 adev->vm_manager.block_size;
280 unsigned pt_idx, from, to;
284 if (!parent->entries) {
285 unsigned num_entries = amdgpu_vm_num_entries(adev, level);
287 parent->entries = kvmalloc_array(num_entries,
288 sizeof(struct amdgpu_vm_pt),
289 GFP_KERNEL | __GFP_ZERO);
290 if (!parent->entries)
292 memset(parent->entries, 0 , sizeof(struct amdgpu_vm_pt));
295 from = saddr >> shift;
297 if (from >= amdgpu_vm_num_entries(adev, level) ||
298 to >= amdgpu_vm_num_entries(adev, level))
301 if (to > parent->last_entry_used)
302 parent->last_entry_used = to;
305 saddr = saddr & ((1 << shift) - 1);
306 eaddr = eaddr & ((1 << shift) - 1);
308 flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS |
309 AMDGPU_GEM_CREATE_VRAM_CLEARED;
310 if (vm->use_cpu_for_update)
311 flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
313 flags |= (AMDGPU_GEM_CREATE_NO_CPU_ACCESS |
314 AMDGPU_GEM_CREATE_SHADOW);
316 /* walk over the address space and allocate the page tables */
317 for (pt_idx = from; pt_idx <= to; ++pt_idx) {
318 struct reservation_object *resv = vm->root.bo->tbo.resv;
319 struct amdgpu_vm_pt *entry = &parent->entries[pt_idx];
320 struct amdgpu_bo *pt;
323 r = amdgpu_bo_create(adev,
324 amdgpu_vm_bo_size(adev, level),
325 AMDGPU_GPU_PAGE_SIZE, true,
326 AMDGPU_GEM_DOMAIN_VRAM,
332 /* Keep a reference to the root directory to avoid
333 * freeing them up in the wrong order.
335 pt->parent = amdgpu_bo_ref(vm->root.bo);
341 if (level < adev->vm_manager.num_level) {
342 uint64_t sub_saddr = (pt_idx == from) ? saddr : 0;
343 uint64_t sub_eaddr = (pt_idx == to) ? eaddr :
345 r = amdgpu_vm_alloc_levels(adev, vm, entry, sub_saddr,
356 * amdgpu_vm_alloc_pts - Allocate page tables.
358 * @adev: amdgpu_device pointer
359 * @vm: VM to allocate page tables for
360 * @saddr: Start address which needs to be allocated
361 * @size: Size from start address we need.
363 * Make sure the page tables are allocated.
365 int amdgpu_vm_alloc_pts(struct amdgpu_device *adev,
366 struct amdgpu_vm *vm,
367 uint64_t saddr, uint64_t size)
372 /* validate the parameters */
373 if (saddr & AMDGPU_GPU_PAGE_MASK || size & AMDGPU_GPU_PAGE_MASK)
376 eaddr = saddr + size - 1;
377 last_pfn = eaddr / AMDGPU_GPU_PAGE_SIZE;
378 if (last_pfn >= adev->vm_manager.max_pfn) {
379 dev_err(adev->dev, "va above limit (0x%08llX >= 0x%08llX)\n",
380 last_pfn, adev->vm_manager.max_pfn);
384 saddr /= AMDGPU_GPU_PAGE_SIZE;
385 eaddr /= AMDGPU_GPU_PAGE_SIZE;
387 return amdgpu_vm_alloc_levels(adev, vm, &vm->root, saddr, eaddr, 0);
391 * amdgpu_vm_had_gpu_reset - check if reset occured since last use
393 * @adev: amdgpu_device pointer
394 * @id: VMID structure
396 * Check if GPU reset occured since last use of the VMID.
398 static bool amdgpu_vm_had_gpu_reset(struct amdgpu_device *adev,
399 struct amdgpu_vm_id *id)
401 return id->current_gpu_reset_count !=
402 atomic_read(&adev->gpu_reset_counter);
405 static bool amdgpu_vm_reserved_vmid_ready(struct amdgpu_vm *vm, unsigned vmhub)
407 return !!vm->reserved_vmid[vmhub];
410 /* idr_mgr->lock must be held */
411 static int amdgpu_vm_grab_reserved_vmid_locked(struct amdgpu_vm *vm,
412 struct amdgpu_ring *ring,
413 struct amdgpu_sync *sync,
414 struct dma_fence *fence,
415 struct amdgpu_job *job)
417 struct amdgpu_device *adev = ring->adev;
418 unsigned vmhub = ring->funcs->vmhub;
419 uint64_t fence_context = adev->fence_context + ring->idx;
420 struct amdgpu_vm_id *id = vm->reserved_vmid[vmhub];
421 struct amdgpu_vm_id_manager *id_mgr = &adev->vm_manager.id_mgr[vmhub];
422 struct dma_fence *updates = sync->last_vm_update;
424 struct dma_fence *flushed, *tmp;
425 bool needs_flush = false;
427 flushed = id->flushed_updates;
428 if ((amdgpu_vm_had_gpu_reset(adev, id)) ||
429 (atomic64_read(&id->owner) != vm->client_id) ||
430 (job->vm_pd_addr != id->pd_gpu_addr) ||
431 (updates && (!flushed || updates->context != flushed->context ||
432 dma_fence_is_later(updates, flushed))) ||
433 (!id->last_flush || (id->last_flush->context != fence_context &&
434 !dma_fence_is_signaled(id->last_flush)))) {
436 /* to prevent one context starved by another context */
438 tmp = amdgpu_sync_peek_fence(&id->active, ring);
440 r = amdgpu_sync_fence(adev, sync, tmp);
445 /* Good we can use this VMID. Remember this submission as
448 r = amdgpu_sync_fence(ring->adev, &id->active, fence);
452 if (updates && (!flushed || updates->context != flushed->context ||
453 dma_fence_is_later(updates, flushed))) {
454 dma_fence_put(id->flushed_updates);
455 id->flushed_updates = dma_fence_get(updates);
457 id->pd_gpu_addr = job->vm_pd_addr;
458 atomic64_set(&id->owner, vm->client_id);
459 job->vm_needs_flush = needs_flush;
461 dma_fence_put(id->last_flush);
462 id->last_flush = NULL;
464 job->vm_id = id - id_mgr->ids;
465 trace_amdgpu_vm_grab_id(vm, ring, job);
471 * amdgpu_vm_grab_id - allocate the next free VMID
473 * @vm: vm to allocate id for
474 * @ring: ring we want to submit job to
475 * @sync: sync object where we add dependencies
476 * @fence: fence protecting ID from reuse
478 * Allocate an id for the vm, adding fences to the sync obj as necessary.
480 int amdgpu_vm_grab_id(struct amdgpu_vm *vm, struct amdgpu_ring *ring,
481 struct amdgpu_sync *sync, struct dma_fence *fence,
482 struct amdgpu_job *job)
484 struct amdgpu_device *adev = ring->adev;
485 unsigned vmhub = ring->funcs->vmhub;
486 struct amdgpu_vm_id_manager *id_mgr = &adev->vm_manager.id_mgr[vmhub];
487 uint64_t fence_context = adev->fence_context + ring->idx;
488 struct dma_fence *updates = sync->last_vm_update;
489 struct amdgpu_vm_id *id, *idle;
490 struct dma_fence **fences;
494 mutex_lock(&id_mgr->lock);
495 if (amdgpu_vm_reserved_vmid_ready(vm, vmhub)) {
496 r = amdgpu_vm_grab_reserved_vmid_locked(vm, ring, sync, fence, job);
497 mutex_unlock(&id_mgr->lock);
500 fences = kmalloc_array(sizeof(void *), id_mgr->num_ids, GFP_KERNEL);
502 mutex_unlock(&id_mgr->lock);
505 /* Check if we have an idle VMID */
507 list_for_each_entry(idle, &id_mgr->ids_lru, list) {
508 fences[i] = amdgpu_sync_peek_fence(&idle->active, ring);
514 /* If we can't find a idle VMID to use, wait till one becomes available */
515 if (&idle->list == &id_mgr->ids_lru) {
516 u64 fence_context = adev->vm_manager.fence_context + ring->idx;
517 unsigned seqno = ++adev->vm_manager.seqno[ring->idx];
518 struct dma_fence_array *array;
521 for (j = 0; j < i; ++j)
522 dma_fence_get(fences[j]);
524 array = dma_fence_array_create(i, fences, fence_context,
527 for (j = 0; j < i; ++j)
528 dma_fence_put(fences[j]);
535 r = amdgpu_sync_fence(ring->adev, sync, &array->base);
536 dma_fence_put(&array->base);
540 mutex_unlock(&id_mgr->lock);
546 job->vm_needs_flush = false;
547 /* Check if we can use a VMID already assigned to this VM */
548 list_for_each_entry_reverse(id, &id_mgr->ids_lru, list) {
549 struct dma_fence *flushed;
550 bool needs_flush = false;
552 /* Check all the prerequisites to using this VMID */
553 if (amdgpu_vm_had_gpu_reset(adev, id))
556 if (atomic64_read(&id->owner) != vm->client_id)
559 if (job->vm_pd_addr != id->pd_gpu_addr)
562 if (!id->last_flush ||
563 (id->last_flush->context != fence_context &&
564 !dma_fence_is_signaled(id->last_flush)))
567 flushed = id->flushed_updates;
568 if (updates && (!flushed || dma_fence_is_later(updates, flushed)))
571 /* Concurrent flushes are only possible starting with Vega10 */
572 if (adev->asic_type < CHIP_VEGA10 && needs_flush)
575 /* Good we can use this VMID. Remember this submission as
578 r = amdgpu_sync_fence(ring->adev, &id->active, fence);
582 if (updates && (!flushed || dma_fence_is_later(updates, flushed))) {
583 dma_fence_put(id->flushed_updates);
584 id->flushed_updates = dma_fence_get(updates);
590 goto no_flush_needed;
594 /* Still no ID to use? Then use the idle one found earlier */
597 /* Remember this submission as user of the VMID */
598 r = amdgpu_sync_fence(ring->adev, &id->active, fence);
602 id->pd_gpu_addr = job->vm_pd_addr;
603 dma_fence_put(id->flushed_updates);
604 id->flushed_updates = dma_fence_get(updates);
605 atomic64_set(&id->owner, vm->client_id);
608 job->vm_needs_flush = true;
609 dma_fence_put(id->last_flush);
610 id->last_flush = NULL;
613 list_move_tail(&id->list, &id_mgr->ids_lru);
615 job->vm_id = id - id_mgr->ids;
616 trace_amdgpu_vm_grab_id(vm, ring, job);
619 mutex_unlock(&id_mgr->lock);
623 static void amdgpu_vm_free_reserved_vmid(struct amdgpu_device *adev,
624 struct amdgpu_vm *vm,
627 struct amdgpu_vm_id_manager *id_mgr = &adev->vm_manager.id_mgr[vmhub];
629 mutex_lock(&id_mgr->lock);
630 if (vm->reserved_vmid[vmhub]) {
631 list_add(&vm->reserved_vmid[vmhub]->list,
633 vm->reserved_vmid[vmhub] = NULL;
634 atomic_dec(&id_mgr->reserved_vmid_num);
636 mutex_unlock(&id_mgr->lock);
639 static int amdgpu_vm_alloc_reserved_vmid(struct amdgpu_device *adev,
640 struct amdgpu_vm *vm,
643 struct amdgpu_vm_id_manager *id_mgr;
644 struct amdgpu_vm_id *idle;
647 id_mgr = &adev->vm_manager.id_mgr[vmhub];
648 mutex_lock(&id_mgr->lock);
649 if (vm->reserved_vmid[vmhub])
651 if (atomic_inc_return(&id_mgr->reserved_vmid_num) >
652 AMDGPU_VM_MAX_RESERVED_VMID) {
653 DRM_ERROR("Over limitation of reserved vmid\n");
654 atomic_dec(&id_mgr->reserved_vmid_num);
658 /* Select the first entry VMID */
659 idle = list_first_entry(&id_mgr->ids_lru, struct amdgpu_vm_id, list);
660 list_del_init(&idle->list);
661 vm->reserved_vmid[vmhub] = idle;
662 mutex_unlock(&id_mgr->lock);
666 mutex_unlock(&id_mgr->lock);
671 * amdgpu_vm_check_compute_bug - check whether asic has compute vm bug
673 * @adev: amdgpu_device pointer
675 void amdgpu_vm_check_compute_bug(struct amdgpu_device *adev)
677 const struct amdgpu_ip_block *ip_block;
678 bool has_compute_vm_bug;
679 struct amdgpu_ring *ring;
682 has_compute_vm_bug = false;
684 ip_block = amdgpu_get_ip_block(adev, AMD_IP_BLOCK_TYPE_GFX);
686 /* Compute has a VM bug for GFX version < 7.
687 Compute has a VM bug for GFX 8 MEC firmware version < 673.*/
688 if (ip_block->version->major <= 7)
689 has_compute_vm_bug = true;
690 else if (ip_block->version->major == 8)
691 if (adev->gfx.mec_fw_version < 673)
692 has_compute_vm_bug = true;
695 for (i = 0; i < adev->num_rings; i++) {
696 ring = adev->rings[i];
697 if (ring->funcs->type == AMDGPU_RING_TYPE_COMPUTE)
698 /* only compute rings */
699 ring->has_compute_vm_bug = has_compute_vm_bug;
701 ring->has_compute_vm_bug = false;
705 bool amdgpu_vm_need_pipeline_sync(struct amdgpu_ring *ring,
706 struct amdgpu_job *job)
708 struct amdgpu_device *adev = ring->adev;
709 unsigned vmhub = ring->funcs->vmhub;
710 struct amdgpu_vm_id_manager *id_mgr = &adev->vm_manager.id_mgr[vmhub];
711 struct amdgpu_vm_id *id;
712 bool gds_switch_needed;
713 bool vm_flush_needed = job->vm_needs_flush || ring->has_compute_vm_bug;
717 id = &id_mgr->ids[job->vm_id];
718 gds_switch_needed = ring->funcs->emit_gds_switch && (
719 id->gds_base != job->gds_base ||
720 id->gds_size != job->gds_size ||
721 id->gws_base != job->gws_base ||
722 id->gws_size != job->gws_size ||
723 id->oa_base != job->oa_base ||
724 id->oa_size != job->oa_size);
726 if (amdgpu_vm_had_gpu_reset(adev, id))
729 return vm_flush_needed || gds_switch_needed;
732 static bool amdgpu_vm_is_large_bar(struct amdgpu_device *adev)
734 return (adev->mc.real_vram_size == adev->mc.visible_vram_size);
738 * amdgpu_vm_flush - hardware flush the vm
740 * @ring: ring to use for flush
741 * @vm_id: vmid number to use
742 * @pd_addr: address of the page directory
744 * Emit a VM flush when it is necessary.
746 int amdgpu_vm_flush(struct amdgpu_ring *ring, struct amdgpu_job *job, bool need_pipe_sync)
748 struct amdgpu_device *adev = ring->adev;
749 unsigned vmhub = ring->funcs->vmhub;
750 struct amdgpu_vm_id_manager *id_mgr = &adev->vm_manager.id_mgr[vmhub];
751 struct amdgpu_vm_id *id = &id_mgr->ids[job->vm_id];
752 bool gds_switch_needed = ring->funcs->emit_gds_switch && (
753 id->gds_base != job->gds_base ||
754 id->gds_size != job->gds_size ||
755 id->gws_base != job->gws_base ||
756 id->gws_size != job->gws_size ||
757 id->oa_base != job->oa_base ||
758 id->oa_size != job->oa_size);
759 bool vm_flush_needed = job->vm_needs_flush;
760 unsigned patch_offset = 0;
763 if (amdgpu_vm_had_gpu_reset(adev, id)) {
764 gds_switch_needed = true;
765 vm_flush_needed = true;
768 if (!vm_flush_needed && !gds_switch_needed && !need_pipe_sync)
771 if (ring->funcs->init_cond_exec)
772 patch_offset = amdgpu_ring_init_cond_exec(ring);
775 amdgpu_ring_emit_pipeline_sync(ring);
777 if (ring->funcs->emit_vm_flush && vm_flush_needed) {
778 struct dma_fence *fence;
780 trace_amdgpu_vm_flush(ring, job->vm_id, job->vm_pd_addr);
781 amdgpu_ring_emit_vm_flush(ring, job->vm_id, job->vm_pd_addr);
783 r = amdgpu_fence_emit(ring, &fence);
787 mutex_lock(&id_mgr->lock);
788 dma_fence_put(id->last_flush);
789 id->last_flush = fence;
790 id->current_gpu_reset_count = atomic_read(&adev->gpu_reset_counter);
791 mutex_unlock(&id_mgr->lock);
794 if (ring->funcs->emit_gds_switch && gds_switch_needed) {
795 id->gds_base = job->gds_base;
796 id->gds_size = job->gds_size;
797 id->gws_base = job->gws_base;
798 id->gws_size = job->gws_size;
799 id->oa_base = job->oa_base;
800 id->oa_size = job->oa_size;
801 amdgpu_ring_emit_gds_switch(ring, job->vm_id, job->gds_base,
802 job->gds_size, job->gws_base,
803 job->gws_size, job->oa_base,
807 if (ring->funcs->patch_cond_exec)
808 amdgpu_ring_patch_cond_exec(ring, patch_offset);
810 /* the double SWITCH_BUFFER here *cannot* be skipped by COND_EXEC */
811 if (ring->funcs->emit_switch_buffer) {
812 amdgpu_ring_emit_switch_buffer(ring);
813 amdgpu_ring_emit_switch_buffer(ring);
819 * amdgpu_vm_reset_id - reset VMID to zero
821 * @adev: amdgpu device structure
822 * @vm_id: vmid number to use
824 * Reset saved GDW, GWS and OA to force switch on next flush.
826 void amdgpu_vm_reset_id(struct amdgpu_device *adev, unsigned vmhub,
829 struct amdgpu_vm_id_manager *id_mgr = &adev->vm_manager.id_mgr[vmhub];
830 struct amdgpu_vm_id *id = &id_mgr->ids[vmid];
832 atomic64_set(&id->owner, 0);
842 * amdgpu_vm_reset_all_id - reset VMID to zero
844 * @adev: amdgpu device structure
846 * Reset VMID to force flush on next use
848 void amdgpu_vm_reset_all_ids(struct amdgpu_device *adev)
852 for (i = 0; i < AMDGPU_MAX_VMHUBS; ++i) {
853 struct amdgpu_vm_id_manager *id_mgr =
854 &adev->vm_manager.id_mgr[i];
856 for (j = 1; j < id_mgr->num_ids; ++j)
857 amdgpu_vm_reset_id(adev, i, j);
862 * amdgpu_vm_bo_find - find the bo_va for a specific vm & bo
865 * @bo: requested buffer object
867 * Find @bo inside the requested vm.
868 * Search inside the @bos vm list for the requested vm
869 * Returns the found bo_va or NULL if none is found
871 * Object has to be reserved!
873 struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
874 struct amdgpu_bo *bo)
876 struct amdgpu_bo_va *bo_va;
878 list_for_each_entry(bo_va, &bo->va, bo_list) {
879 if (bo_va->vm == vm) {
887 * amdgpu_vm_do_set_ptes - helper to call the right asic function
889 * @params: see amdgpu_pte_update_params definition
890 * @pe: addr of the page entry
891 * @addr: dst addr to write into pe
892 * @count: number of page entries to update
893 * @incr: increase next addr by incr bytes
894 * @flags: hw access flags
896 * Traces the parameters and calls the right asic functions
897 * to setup the page table using the DMA.
899 static void amdgpu_vm_do_set_ptes(struct amdgpu_pte_update_params *params,
900 uint64_t pe, uint64_t addr,
901 unsigned count, uint32_t incr,
904 trace_amdgpu_vm_set_ptes(pe, addr, count, incr, flags);
907 amdgpu_vm_write_pte(params->adev, params->ib, pe,
908 addr | flags, count, incr);
911 amdgpu_vm_set_pte_pde(params->adev, params->ib, pe, addr,
917 * amdgpu_vm_do_copy_ptes - copy the PTEs from the GART
919 * @params: see amdgpu_pte_update_params definition
920 * @pe: addr of the page entry
921 * @addr: dst addr to write into pe
922 * @count: number of page entries to update
923 * @incr: increase next addr by incr bytes
924 * @flags: hw access flags
926 * Traces the parameters and calls the DMA function to copy the PTEs.
928 static void amdgpu_vm_do_copy_ptes(struct amdgpu_pte_update_params *params,
929 uint64_t pe, uint64_t addr,
930 unsigned count, uint32_t incr,
933 uint64_t src = (params->src + (addr >> 12) * 8);
936 trace_amdgpu_vm_copy_ptes(pe, src, count);
938 amdgpu_vm_copy_pte(params->adev, params->ib, pe, src, count);
942 * amdgpu_vm_map_gart - Resolve gart mapping of addr
944 * @pages_addr: optional DMA address to use for lookup
945 * @addr: the unmapped addr
947 * Look up the physical address of the page that the pte resolves
948 * to and return the pointer for the page table entry.
950 static uint64_t amdgpu_vm_map_gart(const dma_addr_t *pages_addr, uint64_t addr)
954 /* page table offset */
955 result = pages_addr[addr >> PAGE_SHIFT];
957 /* in case cpu page size != gpu page size*/
958 result |= addr & (~PAGE_MASK);
960 result &= 0xFFFFFFFFFFFFF000ULL;
966 * amdgpu_vm_cpu_set_ptes - helper to update page tables via CPU
968 * @params: see amdgpu_pte_update_params definition
969 * @pe: kmap addr of the page entry
970 * @addr: dst addr to write into pe
971 * @count: number of page entries to update
972 * @incr: increase next addr by incr bytes
973 * @flags: hw access flags
975 * Write count number of PT/PD entries directly.
977 static void amdgpu_vm_cpu_set_ptes(struct amdgpu_pte_update_params *params,
978 uint64_t pe, uint64_t addr,
979 unsigned count, uint32_t incr,
985 for (i = 0; i < count; i++) {
986 value = params->pages_addr ?
987 amdgpu_vm_map_gart(params->pages_addr, addr) :
989 amdgpu_gart_set_pte_pde(params->adev, (void *)(uintptr_t)pe,
996 amdgpu_gart_flush_gpu_tlb(params->adev, 0);
999 static int amdgpu_vm_bo_wait(struct amdgpu_device *adev, struct amdgpu_bo *bo)
1001 struct amdgpu_sync sync;
1004 amdgpu_sync_create(&sync);
1005 amdgpu_sync_resv(adev, &sync, bo->tbo.resv, AMDGPU_FENCE_OWNER_VM);
1006 r = amdgpu_sync_wait(&sync, true);
1007 amdgpu_sync_free(&sync);
1013 * amdgpu_vm_update_level - update a single level in the hierarchy
1015 * @adev: amdgpu_device pointer
1017 * @parent: parent directory
1019 * Makes sure all entries in @parent are up to date.
1020 * Returns 0 for success, error for failure.
1022 static int amdgpu_vm_update_level(struct amdgpu_device *adev,
1023 struct amdgpu_vm *vm,
1024 struct amdgpu_vm_pt *parent,
1027 struct amdgpu_bo *shadow;
1028 struct amdgpu_ring *ring = NULL;
1029 uint64_t pd_addr, shadow_addr = 0;
1030 uint32_t incr = amdgpu_vm_bo_size(adev, level + 1);
1031 uint64_t last_pde = ~0, last_pt = ~0, last_shadow = ~0;
1032 unsigned count = 0, pt_idx, ndw = 0;
1033 struct amdgpu_job *job;
1034 struct amdgpu_pte_update_params params;
1035 struct dma_fence *fence = NULL;
1039 if (!parent->entries)
1042 memset(¶ms, 0, sizeof(params));
1044 shadow = parent->bo->shadow;
1046 WARN_ON(vm->use_cpu_for_update && shadow);
1047 if (vm->use_cpu_for_update && !shadow) {
1048 r = amdgpu_bo_kmap(parent->bo, (void **)&pd_addr);
1051 r = amdgpu_vm_bo_wait(adev, parent->bo);
1053 amdgpu_bo_kunmap(parent->bo);
1056 params.func = amdgpu_vm_cpu_set_ptes;
1059 r = amdgpu_ttm_bind(&shadow->tbo, &shadow->tbo.mem);
1063 ring = container_of(vm->entity.sched, struct amdgpu_ring,
1069 /* assume the worst case */
1070 ndw += parent->last_entry_used * 6;
1072 pd_addr = amdgpu_bo_gpu_offset(parent->bo);
1075 shadow_addr = amdgpu_bo_gpu_offset(shadow);
1081 r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
1085 params.ib = &job->ibs[0];
1086 params.func = amdgpu_vm_do_set_ptes;
1090 /* walk over the address space and update the directory */
1091 for (pt_idx = 0; pt_idx <= parent->last_entry_used; ++pt_idx) {
1092 struct amdgpu_bo *bo = parent->entries[pt_idx].bo;
1099 struct amdgpu_bo *pt_shadow = bo->shadow;
1101 r = amdgpu_ttm_bind(&pt_shadow->tbo,
1102 &pt_shadow->tbo.mem);
1107 pt = amdgpu_bo_gpu_offset(bo);
1108 pt = amdgpu_gart_get_vm_pde(adev, pt);
1109 if (parent->entries[pt_idx].addr == pt)
1112 parent->entries[pt_idx].addr = pt;
1114 pde = pd_addr + pt_idx * 8;
1115 if (((last_pde + 8 * count) != pde) ||
1116 ((last_pt + incr * count) != pt) ||
1117 (count == AMDGPU_VM_MAX_UPDATE_SIZE)) {
1121 params.func(¶ms,
1127 params.func(¶ms, last_pde,
1128 last_pt, count, incr,
1134 last_shadow = shadow_addr + pt_idx * 8;
1142 if (vm->root.bo->shadow)
1143 params.func(¶ms, last_shadow, last_pt,
1144 count, incr, AMDGPU_PTE_VALID);
1146 params.func(¶ms, last_pde, last_pt,
1147 count, incr, AMDGPU_PTE_VALID);
1150 if (params.func == amdgpu_vm_cpu_set_ptes)
1151 amdgpu_bo_kunmap(parent->bo);
1152 else if (params.ib->length_dw == 0) {
1153 amdgpu_job_free(job);
1155 amdgpu_ring_pad_ib(ring, params.ib);
1156 amdgpu_sync_resv(adev, &job->sync, parent->bo->tbo.resv,
1157 AMDGPU_FENCE_OWNER_VM);
1159 amdgpu_sync_resv(adev, &job->sync, shadow->tbo.resv,
1160 AMDGPU_FENCE_OWNER_VM);
1162 WARN_ON(params.ib->length_dw > ndw);
1163 r = amdgpu_job_submit(job, ring, &vm->entity,
1164 AMDGPU_FENCE_OWNER_VM, &fence);
1168 amdgpu_bo_fence(parent->bo, fence, true);
1169 dma_fence_put(vm->last_dir_update);
1170 vm->last_dir_update = dma_fence_get(fence);
1171 dma_fence_put(fence);
1174 * Recurse into the subdirectories. This recursion is harmless because
1175 * we only have a maximum of 5 layers.
1177 for (pt_idx = 0; pt_idx <= parent->last_entry_used; ++pt_idx) {
1178 struct amdgpu_vm_pt *entry = &parent->entries[pt_idx];
1183 r = amdgpu_vm_update_level(adev, vm, entry, level + 1);
1191 amdgpu_job_free(job);
1196 * amdgpu_vm_invalidate_level - mark all PD levels as invalid
1198 * @parent: parent PD
1200 * Mark all PD level as invalid after an error.
1202 static void amdgpu_vm_invalidate_level(struct amdgpu_vm_pt *parent)
1207 * Recurse into the subdirectories. This recursion is harmless because
1208 * we only have a maximum of 5 layers.
1210 for (pt_idx = 0; pt_idx <= parent->last_entry_used; ++pt_idx) {
1211 struct amdgpu_vm_pt *entry = &parent->entries[pt_idx];
1216 entry->addr = ~0ULL;
1217 amdgpu_vm_invalidate_level(entry);
1222 * amdgpu_vm_update_directories - make sure that all directories are valid
1224 * @adev: amdgpu_device pointer
1227 * Makes sure all directories are up to date.
1228 * Returns 0 for success, error for failure.
1230 int amdgpu_vm_update_directories(struct amdgpu_device *adev,
1231 struct amdgpu_vm *vm)
1235 r = amdgpu_vm_update_level(adev, vm, &vm->root, 0);
1237 amdgpu_vm_invalidate_level(&vm->root);
1243 * amdgpu_vm_find_pt - find the page table for an address
1245 * @p: see amdgpu_pte_update_params definition
1246 * @addr: virtual address in question
1248 * Find the page table BO for a virtual address, return NULL when none found.
1250 static struct amdgpu_bo *amdgpu_vm_get_pt(struct amdgpu_pte_update_params *p,
1253 struct amdgpu_vm_pt *entry = &p->vm->root;
1254 unsigned idx, level = p->adev->vm_manager.num_level;
1256 while (entry->entries) {
1257 idx = addr >> (p->adev->vm_manager.block_size * level--);
1258 idx %= amdgpu_bo_size(entry->bo) / 8;
1259 entry = &entry->entries[idx];
1269 * amdgpu_vm_update_ptes - make sure that page tables are valid
1271 * @params: see amdgpu_pte_update_params definition
1273 * @start: start of GPU address range
1274 * @end: end of GPU address range
1275 * @dst: destination address to map to, the next dst inside the function
1276 * @flags: mapping flags
1278 * Update the page tables in the range @start - @end.
1279 * Returns 0 for success, -EINVAL for failure.
1281 static int amdgpu_vm_update_ptes(struct amdgpu_pte_update_params *params,
1282 uint64_t start, uint64_t end,
1283 uint64_t dst, uint64_t flags)
1285 struct amdgpu_device *adev = params->adev;
1286 const uint64_t mask = AMDGPU_VM_PTE_COUNT(adev) - 1;
1288 uint64_t addr, pe_start;
1289 struct amdgpu_bo *pt;
1292 bool use_cpu_update = (params->func == amdgpu_vm_cpu_set_ptes);
1295 /* walk over the address space and update the page tables */
1296 for (addr = start; addr < end; addr += nptes) {
1297 pt = amdgpu_vm_get_pt(params, addr);
1299 pr_err("PT not found, aborting update_ptes\n");
1303 if ((addr & ~mask) == (end & ~mask))
1306 nptes = AMDGPU_VM_PTE_COUNT(adev) - (addr & mask);
1308 if (use_cpu_update) {
1309 r = amdgpu_bo_kmap(pt, (void *)&pe_start);
1313 WARN_ONCE(pt->shadow,
1314 "CPU VM update doesn't support shadow pages");
1317 pe_start = amdgpu_bo_gpu_offset(pt->shadow);
1318 pe_start += (addr & mask) * 8;
1319 params->func(params, pe_start, dst, nptes,
1320 AMDGPU_GPU_PAGE_SIZE, flags);
1322 pe_start = amdgpu_bo_gpu_offset(pt);
1325 pe_start += (addr & mask) * 8;
1326 params->func(params, pe_start, dst, nptes,
1327 AMDGPU_GPU_PAGE_SIZE, flags);
1329 dst += nptes * AMDGPU_GPU_PAGE_SIZE;
1332 amdgpu_bo_kunmap(pt);
1339 * amdgpu_vm_frag_ptes - add fragment information to PTEs
1341 * @params: see amdgpu_pte_update_params definition
1343 * @start: first PTE to handle
1344 * @end: last PTE to handle
1345 * @dst: addr those PTEs should point to
1346 * @flags: hw mapping flags
1347 * Returns 0 for success, -EINVAL for failure.
1349 static int amdgpu_vm_frag_ptes(struct amdgpu_pte_update_params *params,
1350 uint64_t start, uint64_t end,
1351 uint64_t dst, uint64_t flags)
1356 * The MC L1 TLB supports variable sized pages, based on a fragment
1357 * field in the PTE. When this field is set to a non-zero value, page
1358 * granularity is increased from 4KB to (1 << (12 + frag)). The PTE
1359 * flags are considered valid for all PTEs within the fragment range
1360 * and corresponding mappings are assumed to be physically contiguous.
1362 * The L1 TLB can store a single PTE for the whole fragment,
1363 * significantly increasing the space available for translation
1364 * caching. This leads to large improvements in throughput when the
1365 * TLB is under pressure.
1367 * The L2 TLB distributes small and large fragments into two
1368 * asymmetric partitions. The large fragment cache is significantly
1369 * larger. Thus, we try to use large fragments wherever possible.
1370 * Userspace can support this by aligning virtual base address and
1371 * allocation size to the fragment size.
1374 /* SI and newer are optimized for 64KB */
1375 uint64_t frag_flags = AMDGPU_PTE_FRAG(AMDGPU_LOG2_PAGES_PER_FRAG);
1376 uint64_t frag_align = 1 << AMDGPU_LOG2_PAGES_PER_FRAG;
1378 uint64_t frag_start = ALIGN(start, frag_align);
1379 uint64_t frag_end = end & ~(frag_align - 1);
1381 /* system pages are non continuously */
1382 if (params->src || !(flags & AMDGPU_PTE_VALID) ||
1383 (frag_start >= frag_end))
1384 return amdgpu_vm_update_ptes(params, start, end, dst, flags);
1386 /* handle the 4K area at the beginning */
1387 if (start != frag_start) {
1388 r = amdgpu_vm_update_ptes(params, start, frag_start,
1392 dst += (frag_start - start) * AMDGPU_GPU_PAGE_SIZE;
1395 /* handle the area in the middle */
1396 r = amdgpu_vm_update_ptes(params, frag_start, frag_end, dst,
1397 flags | frag_flags);
1401 /* handle the 4K area at the end */
1402 if (frag_end != end) {
1403 dst += (frag_end - frag_start) * AMDGPU_GPU_PAGE_SIZE;
1404 r = amdgpu_vm_update_ptes(params, frag_end, end, dst, flags);
1410 * amdgpu_vm_bo_update_mapping - update a mapping in the vm page table
1412 * @adev: amdgpu_device pointer
1413 * @exclusive: fence we need to sync to
1414 * @src: address where to copy page table entries from
1415 * @pages_addr: DMA addresses to use for mapping
1417 * @start: start of mapped range
1418 * @last: last mapped entry
1419 * @flags: flags for the entries
1420 * @addr: addr to set the area to
1421 * @fence: optional resulting fence
1423 * Fill in the page table entries between @start and @last.
1424 * Returns 0 for success, -EINVAL for failure.
1426 static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
1427 struct dma_fence *exclusive,
1429 dma_addr_t *pages_addr,
1430 struct amdgpu_vm *vm,
1431 uint64_t start, uint64_t last,
1432 uint64_t flags, uint64_t addr,
1433 struct dma_fence **fence)
1435 struct amdgpu_ring *ring;
1436 void *owner = AMDGPU_FENCE_OWNER_VM;
1437 unsigned nptes, ncmds, ndw;
1438 struct amdgpu_job *job;
1439 struct amdgpu_pte_update_params params;
1440 struct dma_fence *f = NULL;
1443 memset(¶ms, 0, sizeof(params));
1448 if (vm->use_cpu_for_update) {
1449 /* params.src is used as flag to indicate system Memory */
1453 /* Wait for PT BOs to be free. PTs share the same resv. object
1456 r = amdgpu_vm_bo_wait(adev, vm->root.bo);
1460 params.func = amdgpu_vm_cpu_set_ptes;
1461 params.pages_addr = pages_addr;
1462 return amdgpu_vm_frag_ptes(¶ms, start, last + 1,
1466 ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
1468 /* sync to everything on unmapping */
1469 if (!(flags & AMDGPU_PTE_VALID))
1470 owner = AMDGPU_FENCE_OWNER_UNDEFINED;
1472 nptes = last - start + 1;
1475 * reserve space for one command every (1 << BLOCK_SIZE)
1476 * entries or 2k dwords (whatever is smaller)
1478 ncmds = (nptes >> min(adev->vm_manager.block_size, 11u)) + 1;
1484 /* only copy commands needed */
1487 params.func = amdgpu_vm_do_copy_ptes;
1489 } else if (pages_addr) {
1490 /* copy commands needed */
1496 params.func = amdgpu_vm_do_copy_ptes;
1499 /* set page commands needed */
1502 /* two extra commands for begin/end of fragment */
1505 params.func = amdgpu_vm_do_set_ptes;
1508 r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
1512 params.ib = &job->ibs[0];
1514 if (!src && pages_addr) {
1518 /* Put the PTEs at the end of the IB. */
1519 i = ndw - nptes * 2;
1520 pte= (uint64_t *)&(job->ibs->ptr[i]);
1521 params.src = job->ibs->gpu_addr + i * 4;
1523 for (i = 0; i < nptes; ++i) {
1524 pte[i] = amdgpu_vm_map_gart(pages_addr, addr + i *
1525 AMDGPU_GPU_PAGE_SIZE);
1531 r = amdgpu_sync_fence(adev, &job->sync, exclusive);
1535 r = amdgpu_sync_resv(adev, &job->sync, vm->root.bo->tbo.resv,
1540 r = reservation_object_reserve_shared(vm->root.bo->tbo.resv);
1544 r = amdgpu_vm_frag_ptes(¶ms, start, last + 1, addr, flags);
1548 amdgpu_ring_pad_ib(ring, params.ib);
1549 WARN_ON(params.ib->length_dw > ndw);
1550 r = amdgpu_job_submit(job, ring, &vm->entity,
1551 AMDGPU_FENCE_OWNER_VM, &f);
1555 amdgpu_bo_fence(vm->root.bo, f, true);
1556 dma_fence_put(*fence);
1561 amdgpu_job_free(job);
1566 * amdgpu_vm_bo_split_mapping - split a mapping into smaller chunks
1568 * @adev: amdgpu_device pointer
1569 * @exclusive: fence we need to sync to
1570 * @gtt_flags: flags as they are used for GTT
1571 * @pages_addr: DMA addresses to use for mapping
1573 * @mapping: mapped range and flags to use for the update
1574 * @flags: HW flags for the mapping
1575 * @nodes: array of drm_mm_nodes with the MC addresses
1576 * @fence: optional resulting fence
1578 * Split the mapping into smaller chunks so that each update fits
1580 * Returns 0 for success, -EINVAL for failure.
1582 static int amdgpu_vm_bo_split_mapping(struct amdgpu_device *adev,
1583 struct dma_fence *exclusive,
1585 dma_addr_t *pages_addr,
1586 struct amdgpu_vm *vm,
1587 struct amdgpu_bo_va_mapping *mapping,
1589 struct drm_mm_node *nodes,
1590 struct dma_fence **fence)
1592 uint64_t pfn, src = 0, start = mapping->start;
1595 /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here
1596 * but in case of something, we filter the flags in first place
1598 if (!(mapping->flags & AMDGPU_PTE_READABLE))
1599 flags &= ~AMDGPU_PTE_READABLE;
1600 if (!(mapping->flags & AMDGPU_PTE_WRITEABLE))
1601 flags &= ~AMDGPU_PTE_WRITEABLE;
1603 flags &= ~AMDGPU_PTE_EXECUTABLE;
1604 flags |= mapping->flags & AMDGPU_PTE_EXECUTABLE;
1606 flags &= ~AMDGPU_PTE_MTYPE_MASK;
1607 flags |= (mapping->flags & AMDGPU_PTE_MTYPE_MASK);
1609 if ((mapping->flags & AMDGPU_PTE_PRT) &&
1610 (adev->asic_type >= CHIP_VEGA10)) {
1611 flags |= AMDGPU_PTE_PRT;
1612 flags &= ~AMDGPU_PTE_VALID;
1615 trace_amdgpu_vm_bo_update(mapping);
1617 pfn = mapping->offset >> PAGE_SHIFT;
1619 while (pfn >= nodes->size) {
1626 uint64_t max_entries;
1627 uint64_t addr, last;
1630 addr = nodes->start << PAGE_SHIFT;
1631 max_entries = (nodes->size - pfn) *
1632 (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE);
1635 max_entries = S64_MAX;
1639 if (flags == gtt_flags)
1640 src = adev->gart.table_addr +
1641 (addr >> AMDGPU_GPU_PAGE_SHIFT) * 8;
1643 max_entries = min(max_entries, 16ull * 1024ull);
1645 } else if (flags & AMDGPU_PTE_VALID) {
1646 addr += adev->vm_manager.vram_base_offset;
1648 addr += pfn << PAGE_SHIFT;
1650 last = min((uint64_t)mapping->last, start + max_entries - 1);
1651 r = amdgpu_vm_bo_update_mapping(adev, exclusive,
1652 src, pages_addr, vm,
1653 start, last, flags, addr,
1658 pfn += last - start + 1;
1659 if (nodes && nodes->size == pfn) {
1665 } while (unlikely(start != mapping->last + 1));
1671 * amdgpu_vm_bo_update - update all BO mappings in the vm page table
1673 * @adev: amdgpu_device pointer
1674 * @bo_va: requested BO and VM object
1675 * @clear: if true clear the entries
1677 * Fill in the page table entries for @bo_va.
1678 * Returns 0 for success, -EINVAL for failure.
1680 int amdgpu_vm_bo_update(struct amdgpu_device *adev,
1681 struct amdgpu_bo_va *bo_va,
1684 struct amdgpu_vm *vm = bo_va->vm;
1685 struct amdgpu_bo_va_mapping *mapping;
1686 dma_addr_t *pages_addr = NULL;
1687 uint64_t gtt_flags, flags;
1688 struct ttm_mem_reg *mem;
1689 struct drm_mm_node *nodes;
1690 struct dma_fence *exclusive;
1693 if (clear || !bo_va->bo) {
1698 struct ttm_dma_tt *ttm;
1700 mem = &bo_va->bo->tbo.mem;
1701 nodes = mem->mm_node;
1702 if (mem->mem_type == TTM_PL_TT) {
1703 ttm = container_of(bo_va->bo->tbo.ttm, struct
1705 pages_addr = ttm->dma_address;
1707 exclusive = reservation_object_get_excl(bo_va->bo->tbo.resv);
1711 flags = amdgpu_ttm_tt_pte_flags(adev, bo_va->bo->tbo.ttm, mem);
1712 gtt_flags = (amdgpu_ttm_is_bound(bo_va->bo->tbo.ttm) &&
1713 adev == amdgpu_ttm_adev(bo_va->bo->tbo.bdev)) ?
1720 spin_lock(&vm->status_lock);
1721 if (!list_empty(&bo_va->vm_status))
1722 list_splice_init(&bo_va->valids, &bo_va->invalids);
1723 spin_unlock(&vm->status_lock);
1725 list_for_each_entry(mapping, &bo_va->invalids, list) {
1726 r = amdgpu_vm_bo_split_mapping(adev, exclusive,
1727 gtt_flags, pages_addr, vm,
1728 mapping, flags, nodes,
1729 &bo_va->last_pt_update);
1734 if (trace_amdgpu_vm_bo_mapping_enabled()) {
1735 list_for_each_entry(mapping, &bo_va->valids, list)
1736 trace_amdgpu_vm_bo_mapping(mapping);
1738 list_for_each_entry(mapping, &bo_va->invalids, list)
1739 trace_amdgpu_vm_bo_mapping(mapping);
1742 spin_lock(&vm->status_lock);
1743 list_splice_init(&bo_va->invalids, &bo_va->valids);
1744 list_del_init(&bo_va->vm_status);
1746 list_add(&bo_va->vm_status, &vm->cleared);
1747 spin_unlock(&vm->status_lock);
1753 * amdgpu_vm_update_prt_state - update the global PRT state
1755 static void amdgpu_vm_update_prt_state(struct amdgpu_device *adev)
1757 unsigned long flags;
1760 spin_lock_irqsave(&adev->vm_manager.prt_lock, flags);
1761 enable = !!atomic_read(&adev->vm_manager.num_prt_users);
1762 adev->gart.gart_funcs->set_prt(adev, enable);
1763 spin_unlock_irqrestore(&adev->vm_manager.prt_lock, flags);
1767 * amdgpu_vm_prt_get - add a PRT user
1769 static void amdgpu_vm_prt_get(struct amdgpu_device *adev)
1771 if (!adev->gart.gart_funcs->set_prt)
1774 if (atomic_inc_return(&adev->vm_manager.num_prt_users) == 1)
1775 amdgpu_vm_update_prt_state(adev);
1779 * amdgpu_vm_prt_put - drop a PRT user
1781 static void amdgpu_vm_prt_put(struct amdgpu_device *adev)
1783 if (atomic_dec_return(&adev->vm_manager.num_prt_users) == 0)
1784 amdgpu_vm_update_prt_state(adev);
1788 * amdgpu_vm_prt_cb - callback for updating the PRT status
1790 static void amdgpu_vm_prt_cb(struct dma_fence *fence, struct dma_fence_cb *_cb)
1792 struct amdgpu_prt_cb *cb = container_of(_cb, struct amdgpu_prt_cb, cb);
1794 amdgpu_vm_prt_put(cb->adev);
1799 * amdgpu_vm_add_prt_cb - add callback for updating the PRT status
1801 static void amdgpu_vm_add_prt_cb(struct amdgpu_device *adev,
1802 struct dma_fence *fence)
1804 struct amdgpu_prt_cb *cb;
1806 if (!adev->gart.gart_funcs->set_prt)
1809 cb = kmalloc(sizeof(struct amdgpu_prt_cb), GFP_KERNEL);
1811 /* Last resort when we are OOM */
1813 dma_fence_wait(fence, false);
1815 amdgpu_vm_prt_put(adev);
1818 if (!fence || dma_fence_add_callback(fence, &cb->cb,
1820 amdgpu_vm_prt_cb(fence, &cb->cb);
1825 * amdgpu_vm_free_mapping - free a mapping
1827 * @adev: amdgpu_device pointer
1829 * @mapping: mapping to be freed
1830 * @fence: fence of the unmap operation
1832 * Free a mapping and make sure we decrease the PRT usage count if applicable.
1834 static void amdgpu_vm_free_mapping(struct amdgpu_device *adev,
1835 struct amdgpu_vm *vm,
1836 struct amdgpu_bo_va_mapping *mapping,
1837 struct dma_fence *fence)
1839 if (mapping->flags & AMDGPU_PTE_PRT)
1840 amdgpu_vm_add_prt_cb(adev, fence);
1845 * amdgpu_vm_prt_fini - finish all prt mappings
1847 * @adev: amdgpu_device pointer
1850 * Register a cleanup callback to disable PRT support after VM dies.
1852 static void amdgpu_vm_prt_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1854 struct reservation_object *resv = vm->root.bo->tbo.resv;
1855 struct dma_fence *excl, **shared;
1856 unsigned i, shared_count;
1859 r = reservation_object_get_fences_rcu(resv, &excl,
1860 &shared_count, &shared);
1862 /* Not enough memory to grab the fence list, as last resort
1863 * block for all the fences to complete.
1865 reservation_object_wait_timeout_rcu(resv, true, false,
1866 MAX_SCHEDULE_TIMEOUT);
1870 /* Add a callback for each fence in the reservation object */
1871 amdgpu_vm_prt_get(adev);
1872 amdgpu_vm_add_prt_cb(adev, excl);
1874 for (i = 0; i < shared_count; ++i) {
1875 amdgpu_vm_prt_get(adev);
1876 amdgpu_vm_add_prt_cb(adev, shared[i]);
1883 * amdgpu_vm_clear_freed - clear freed BOs in the PT
1885 * @adev: amdgpu_device pointer
1887 * @fence: optional resulting fence (unchanged if no work needed to be done
1888 * or if an error occurred)
1890 * Make sure all freed BOs are cleared in the PT.
1891 * Returns 0 for success.
1893 * PTs have to be reserved and mutex must be locked!
1895 int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
1896 struct amdgpu_vm *vm,
1897 struct dma_fence **fence)
1899 struct amdgpu_bo_va_mapping *mapping;
1900 struct dma_fence *f = NULL;
1903 while (!list_empty(&vm->freed)) {
1904 mapping = list_first_entry(&vm->freed,
1905 struct amdgpu_bo_va_mapping, list);
1906 list_del(&mapping->list);
1908 r = amdgpu_vm_bo_update_mapping(adev, NULL, 0, NULL, vm,
1909 mapping->start, mapping->last,
1911 amdgpu_vm_free_mapping(adev, vm, mapping, f);
1919 dma_fence_put(*fence);
1930 * amdgpu_vm_clear_invalids - clear invalidated BOs in the PT
1932 * @adev: amdgpu_device pointer
1935 * Make sure all invalidated BOs are cleared in the PT.
1936 * Returns 0 for success.
1938 * PTs have to be reserved and mutex must be locked!
1940 int amdgpu_vm_clear_invalids(struct amdgpu_device *adev,
1941 struct amdgpu_vm *vm, struct amdgpu_sync *sync)
1943 struct amdgpu_bo_va *bo_va = NULL;
1946 spin_lock(&vm->status_lock);
1947 while (!list_empty(&vm->invalidated)) {
1948 bo_va = list_first_entry(&vm->invalidated,
1949 struct amdgpu_bo_va, vm_status);
1950 spin_unlock(&vm->status_lock);
1952 r = amdgpu_vm_bo_update(adev, bo_va, true);
1956 spin_lock(&vm->status_lock);
1958 spin_unlock(&vm->status_lock);
1961 r = amdgpu_sync_fence(adev, sync, bo_va->last_pt_update);
1967 * amdgpu_vm_bo_add - add a bo to a specific vm
1969 * @adev: amdgpu_device pointer
1971 * @bo: amdgpu buffer object
1973 * Add @bo into the requested vm.
1974 * Add @bo to the list of bos associated with the vm
1975 * Returns newly added bo_va or NULL for failure
1977 * Object has to be reserved!
1979 struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
1980 struct amdgpu_vm *vm,
1981 struct amdgpu_bo *bo)
1983 struct amdgpu_bo_va *bo_va;
1985 bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL);
1986 if (bo_va == NULL) {
1991 bo_va->ref_count = 1;
1992 INIT_LIST_HEAD(&bo_va->bo_list);
1993 INIT_LIST_HEAD(&bo_va->valids);
1994 INIT_LIST_HEAD(&bo_va->invalids);
1995 INIT_LIST_HEAD(&bo_va->vm_status);
1998 list_add_tail(&bo_va->bo_list, &bo->va);
2004 * amdgpu_vm_bo_map - map bo inside a vm
2006 * @adev: amdgpu_device pointer
2007 * @bo_va: bo_va to store the address
2008 * @saddr: where to map the BO
2009 * @offset: requested offset in the BO
2010 * @flags: attributes of pages (read/write/valid/etc.)
2012 * Add a mapping of the BO at the specefied addr into the VM.
2013 * Returns 0 for success, error for failure.
2015 * Object has to be reserved and unreserved outside!
2017 int amdgpu_vm_bo_map(struct amdgpu_device *adev,
2018 struct amdgpu_bo_va *bo_va,
2019 uint64_t saddr, uint64_t offset,
2020 uint64_t size, uint64_t flags)
2022 struct amdgpu_bo_va_mapping *mapping, *tmp;
2023 struct amdgpu_vm *vm = bo_va->vm;
2026 /* validate the parameters */
2027 if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
2028 size == 0 || size & AMDGPU_GPU_PAGE_MASK)
2031 /* make sure object fit at this offset */
2032 eaddr = saddr + size - 1;
2033 if (saddr >= eaddr ||
2034 (bo_va->bo && offset + size > amdgpu_bo_size(bo_va->bo)))
2037 saddr /= AMDGPU_GPU_PAGE_SIZE;
2038 eaddr /= AMDGPU_GPU_PAGE_SIZE;
2040 tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr);
2042 /* bo and tmp overlap, invalid addr */
2043 dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with "
2044 "0x%010Lx-0x%010Lx\n", bo_va->bo, saddr, eaddr,
2045 tmp->start, tmp->last + 1);
2049 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
2053 INIT_LIST_HEAD(&mapping->list);
2054 mapping->start = saddr;
2055 mapping->last = eaddr;
2056 mapping->offset = offset;
2057 mapping->flags = flags;
2059 list_add(&mapping->list, &bo_va->invalids);
2060 amdgpu_vm_it_insert(mapping, &vm->va);
2062 if (flags & AMDGPU_PTE_PRT)
2063 amdgpu_vm_prt_get(adev);
2069 * amdgpu_vm_bo_replace_map - map bo inside a vm, replacing existing mappings
2071 * @adev: amdgpu_device pointer
2072 * @bo_va: bo_va to store the address
2073 * @saddr: where to map the BO
2074 * @offset: requested offset in the BO
2075 * @flags: attributes of pages (read/write/valid/etc.)
2077 * Add a mapping of the BO at the specefied addr into the VM. Replace existing
2078 * mappings as we do so.
2079 * Returns 0 for success, error for failure.
2081 * Object has to be reserved and unreserved outside!
2083 int amdgpu_vm_bo_replace_map(struct amdgpu_device *adev,
2084 struct amdgpu_bo_va *bo_va,
2085 uint64_t saddr, uint64_t offset,
2086 uint64_t size, uint64_t flags)
2088 struct amdgpu_bo_va_mapping *mapping;
2089 struct amdgpu_vm *vm = bo_va->vm;
2093 /* validate the parameters */
2094 if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
2095 size == 0 || size & AMDGPU_GPU_PAGE_MASK)
2098 /* make sure object fit at this offset */
2099 eaddr = saddr + size - 1;
2100 if (saddr >= eaddr ||
2101 (bo_va->bo && offset + size > amdgpu_bo_size(bo_va->bo)))
2104 /* Allocate all the needed memory */
2105 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
2109 r = amdgpu_vm_bo_clear_mappings(adev, bo_va->vm, saddr, size);
2115 saddr /= AMDGPU_GPU_PAGE_SIZE;
2116 eaddr /= AMDGPU_GPU_PAGE_SIZE;
2118 mapping->start = saddr;
2119 mapping->last = eaddr;
2120 mapping->offset = offset;
2121 mapping->flags = flags;
2123 list_add(&mapping->list, &bo_va->invalids);
2124 amdgpu_vm_it_insert(mapping, &vm->va);
2126 if (flags & AMDGPU_PTE_PRT)
2127 amdgpu_vm_prt_get(adev);
2133 * amdgpu_vm_bo_unmap - remove bo mapping from vm
2135 * @adev: amdgpu_device pointer
2136 * @bo_va: bo_va to remove the address from
2137 * @saddr: where to the BO is mapped
2139 * Remove a mapping of the BO at the specefied addr from the VM.
2140 * Returns 0 for success, error for failure.
2142 * Object has to be reserved and unreserved outside!
2144 int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
2145 struct amdgpu_bo_va *bo_va,
2148 struct amdgpu_bo_va_mapping *mapping;
2149 struct amdgpu_vm *vm = bo_va->vm;
2152 saddr /= AMDGPU_GPU_PAGE_SIZE;
2154 list_for_each_entry(mapping, &bo_va->valids, list) {
2155 if (mapping->start == saddr)
2159 if (&mapping->list == &bo_va->valids) {
2162 list_for_each_entry(mapping, &bo_va->invalids, list) {
2163 if (mapping->start == saddr)
2167 if (&mapping->list == &bo_va->invalids)
2171 list_del(&mapping->list);
2172 amdgpu_vm_it_remove(mapping, &vm->va);
2173 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
2176 list_add(&mapping->list, &vm->freed);
2178 amdgpu_vm_free_mapping(adev, vm, mapping,
2179 bo_va->last_pt_update);
2185 * amdgpu_vm_bo_clear_mappings - remove all mappings in a specific range
2187 * @adev: amdgpu_device pointer
2188 * @vm: VM structure to use
2189 * @saddr: start of the range
2190 * @size: size of the range
2192 * Remove all mappings in a range, split them as appropriate.
2193 * Returns 0 for success, error for failure.
2195 int amdgpu_vm_bo_clear_mappings(struct amdgpu_device *adev,
2196 struct amdgpu_vm *vm,
2197 uint64_t saddr, uint64_t size)
2199 struct amdgpu_bo_va_mapping *before, *after, *tmp, *next;
2203 eaddr = saddr + size - 1;
2204 saddr /= AMDGPU_GPU_PAGE_SIZE;
2205 eaddr /= AMDGPU_GPU_PAGE_SIZE;
2207 /* Allocate all the needed memory */
2208 before = kzalloc(sizeof(*before), GFP_KERNEL);
2211 INIT_LIST_HEAD(&before->list);
2213 after = kzalloc(sizeof(*after), GFP_KERNEL);
2218 INIT_LIST_HEAD(&after->list);
2220 /* Now gather all removed mappings */
2221 tmp = amdgpu_vm_it_iter_first(&vm->va, saddr, eaddr);
2223 /* Remember mapping split at the start */
2224 if (tmp->start < saddr) {
2225 before->start = tmp->start;
2226 before->last = saddr - 1;
2227 before->offset = tmp->offset;
2228 before->flags = tmp->flags;
2229 list_add(&before->list, &tmp->list);
2232 /* Remember mapping split at the end */
2233 if (tmp->last > eaddr) {
2234 after->start = eaddr + 1;
2235 after->last = tmp->last;
2236 after->offset = tmp->offset;
2237 after->offset += after->start - tmp->start;
2238 after->flags = tmp->flags;
2239 list_add(&after->list, &tmp->list);
2242 list_del(&tmp->list);
2243 list_add(&tmp->list, &removed);
2245 tmp = amdgpu_vm_it_iter_next(tmp, saddr, eaddr);
2248 /* And free them up */
2249 list_for_each_entry_safe(tmp, next, &removed, list) {
2250 amdgpu_vm_it_remove(tmp, &vm->va);
2251 list_del(&tmp->list);
2253 if (tmp->start < saddr)
2255 if (tmp->last > eaddr)
2258 list_add(&tmp->list, &vm->freed);
2259 trace_amdgpu_vm_bo_unmap(NULL, tmp);
2262 /* Insert partial mapping before the range */
2263 if (!list_empty(&before->list)) {
2264 amdgpu_vm_it_insert(before, &vm->va);
2265 if (before->flags & AMDGPU_PTE_PRT)
2266 amdgpu_vm_prt_get(adev);
2271 /* Insert partial mapping after the range */
2272 if (!list_empty(&after->list)) {
2273 amdgpu_vm_it_insert(after, &vm->va);
2274 if (after->flags & AMDGPU_PTE_PRT)
2275 amdgpu_vm_prt_get(adev);
2284 * amdgpu_vm_bo_rmv - remove a bo to a specific vm
2286 * @adev: amdgpu_device pointer
2287 * @bo_va: requested bo_va
2289 * Remove @bo_va->bo from the requested vm.
2291 * Object have to be reserved!
2293 void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
2294 struct amdgpu_bo_va *bo_va)
2296 struct amdgpu_bo_va_mapping *mapping, *next;
2297 struct amdgpu_vm *vm = bo_va->vm;
2299 list_del(&bo_va->bo_list);
2301 spin_lock(&vm->status_lock);
2302 list_del(&bo_va->vm_status);
2303 spin_unlock(&vm->status_lock);
2305 list_for_each_entry_safe(mapping, next, &bo_va->valids, list) {
2306 list_del(&mapping->list);
2307 amdgpu_vm_it_remove(mapping, &vm->va);
2308 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
2309 list_add(&mapping->list, &vm->freed);
2311 list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) {
2312 list_del(&mapping->list);
2313 amdgpu_vm_it_remove(mapping, &vm->va);
2314 amdgpu_vm_free_mapping(adev, vm, mapping,
2315 bo_va->last_pt_update);
2318 dma_fence_put(bo_va->last_pt_update);
2323 * amdgpu_vm_bo_invalidate - mark the bo as invalid
2325 * @adev: amdgpu_device pointer
2327 * @bo: amdgpu buffer object
2329 * Mark @bo as invalid.
2331 void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
2332 struct amdgpu_bo *bo)
2334 struct amdgpu_bo_va *bo_va;
2336 list_for_each_entry(bo_va, &bo->va, bo_list) {
2337 spin_lock(&bo_va->vm->status_lock);
2338 if (list_empty(&bo_va->vm_status))
2339 list_add(&bo_va->vm_status, &bo_va->vm->invalidated);
2340 spin_unlock(&bo_va->vm->status_lock);
2344 static uint32_t amdgpu_vm_get_block_size(uint64_t vm_size)
2346 /* Total bits covered by PD + PTs */
2347 unsigned bits = ilog2(vm_size) + 18;
2349 /* Make sure the PD is 4K in size up to 8GB address space.
2350 Above that split equal between PD and PTs */
2354 return ((bits + 3) / 2);
2358 * amdgpu_vm_adjust_size - adjust vm size and block size
2360 * @adev: amdgpu_device pointer
2361 * @vm_size: the default vm size if it's set auto
2363 void amdgpu_vm_adjust_size(struct amdgpu_device *adev, uint64_t vm_size)
2365 /* adjust vm size firstly */
2366 if (amdgpu_vm_size == -1)
2367 adev->vm_manager.vm_size = vm_size;
2369 adev->vm_manager.vm_size = amdgpu_vm_size;
2371 /* block size depends on vm size */
2372 if (amdgpu_vm_block_size == -1)
2373 adev->vm_manager.block_size =
2374 amdgpu_vm_get_block_size(adev->vm_manager.vm_size);
2376 adev->vm_manager.block_size = amdgpu_vm_block_size;
2378 DRM_INFO("vm size is %llu GB, block size is %u-bit\n",
2379 adev->vm_manager.vm_size, adev->vm_manager.block_size);
2383 * amdgpu_vm_init - initialize a vm instance
2385 * @adev: amdgpu_device pointer
2387 * @vm_context: Indicates if it GFX or Compute context
2391 int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm,
2394 const unsigned align = min(AMDGPU_VM_PTB_ALIGN_SIZE,
2395 AMDGPU_VM_PTE_COUNT(adev) * 8);
2396 unsigned ring_instance;
2397 struct amdgpu_ring *ring;
2398 struct amd_sched_rq *rq;
2403 vm->client_id = atomic64_inc_return(&adev->vm_manager.client_counter);
2404 for (i = 0; i < AMDGPU_MAX_VMHUBS; i++)
2405 vm->reserved_vmid[i] = NULL;
2406 spin_lock_init(&vm->status_lock);
2407 INIT_LIST_HEAD(&vm->invalidated);
2408 INIT_LIST_HEAD(&vm->cleared);
2409 INIT_LIST_HEAD(&vm->freed);
2411 /* create scheduler entity for page table updates */
2413 ring_instance = atomic_inc_return(&adev->vm_manager.vm_pte_next_ring);
2414 ring_instance %= adev->vm_manager.vm_pte_num_rings;
2415 ring = adev->vm_manager.vm_pte_rings[ring_instance];
2416 rq = &ring->sched.sched_rq[AMD_SCHED_PRIORITY_KERNEL];
2417 r = amd_sched_entity_init(&ring->sched, &vm->entity,
2418 rq, amdgpu_sched_jobs);
2422 if (vm_context == AMDGPU_VM_CONTEXT_COMPUTE)
2423 vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode &
2424 AMDGPU_VM_USE_CPU_FOR_COMPUTE);
2426 vm->use_cpu_for_update = !!(adev->vm_manager.vm_update_mode &
2427 AMDGPU_VM_USE_CPU_FOR_GFX);
2428 DRM_DEBUG_DRIVER("VM update mode is %s\n",
2429 vm->use_cpu_for_update ? "CPU" : "SDMA");
2430 WARN_ONCE((vm->use_cpu_for_update & !amdgpu_vm_is_large_bar(adev)),
2431 "CPU update of VM recommended only for large BAR system\n");
2432 vm->last_dir_update = NULL;
2434 flags = AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS |
2435 AMDGPU_GEM_CREATE_VRAM_CLEARED;
2436 if (vm->use_cpu_for_update)
2437 flags |= AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
2439 flags |= (AMDGPU_GEM_CREATE_NO_CPU_ACCESS |
2440 AMDGPU_GEM_CREATE_SHADOW);
2442 r = amdgpu_bo_create(adev, amdgpu_vm_bo_size(adev, 0), align, true,
2443 AMDGPU_GEM_DOMAIN_VRAM,
2445 NULL, NULL, &vm->root.bo);
2447 goto error_free_sched_entity;
2449 r = amdgpu_bo_reserve(vm->root.bo, false);
2451 goto error_free_root;
2453 vm->last_eviction_counter = atomic64_read(&adev->num_evictions);
2454 amdgpu_bo_unreserve(vm->root.bo);
2459 amdgpu_bo_unref(&vm->root.bo->shadow);
2460 amdgpu_bo_unref(&vm->root.bo);
2463 error_free_sched_entity:
2464 amd_sched_entity_fini(&ring->sched, &vm->entity);
2470 * amdgpu_vm_free_levels - free PD/PT levels
2472 * @level: PD/PT starting level to free
2474 * Free the page directory or page table level and all sub levels.
2476 static void amdgpu_vm_free_levels(struct amdgpu_vm_pt *level)
2481 amdgpu_bo_unref(&level->bo->shadow);
2482 amdgpu_bo_unref(&level->bo);
2486 for (i = 0; i <= level->last_entry_used; i++)
2487 amdgpu_vm_free_levels(&level->entries[i]);
2489 kvfree(level->entries);
2493 * amdgpu_vm_fini - tear down a vm instance
2495 * @adev: amdgpu_device pointer
2499 * Unbind the VM and remove all bos from the vm bo list
2501 void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
2503 struct amdgpu_bo_va_mapping *mapping, *tmp;
2504 bool prt_fini_needed = !!adev->gart.gart_funcs->set_prt;
2507 amd_sched_entity_fini(vm->entity.sched, &vm->entity);
2509 if (!RB_EMPTY_ROOT(&vm->va)) {
2510 dev_err(adev->dev, "still active bo inside vm\n");
2512 rbtree_postorder_for_each_entry_safe(mapping, tmp, &vm->va, rb) {
2513 list_del(&mapping->list);
2514 amdgpu_vm_it_remove(mapping, &vm->va);
2517 list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
2518 if (mapping->flags & AMDGPU_PTE_PRT && prt_fini_needed) {
2519 amdgpu_vm_prt_fini(adev, vm);
2520 prt_fini_needed = false;
2523 list_del(&mapping->list);
2524 amdgpu_vm_free_mapping(adev, vm, mapping, NULL);
2527 amdgpu_vm_free_levels(&vm->root);
2528 dma_fence_put(vm->last_dir_update);
2529 for (i = 0; i < AMDGPU_MAX_VMHUBS; i++)
2530 amdgpu_vm_free_reserved_vmid(adev, vm, i);
2534 * amdgpu_vm_manager_init - init the VM manager
2536 * @adev: amdgpu_device pointer
2538 * Initialize the VM manager structures
2540 void amdgpu_vm_manager_init(struct amdgpu_device *adev)
2544 for (i = 0; i < AMDGPU_MAX_VMHUBS; ++i) {
2545 struct amdgpu_vm_id_manager *id_mgr =
2546 &adev->vm_manager.id_mgr[i];
2548 mutex_init(&id_mgr->lock);
2549 INIT_LIST_HEAD(&id_mgr->ids_lru);
2550 atomic_set(&id_mgr->reserved_vmid_num, 0);
2552 /* skip over VMID 0, since it is the system VM */
2553 for (j = 1; j < id_mgr->num_ids; ++j) {
2554 amdgpu_vm_reset_id(adev, i, j);
2555 amdgpu_sync_create(&id_mgr->ids[i].active);
2556 list_add_tail(&id_mgr->ids[j].list, &id_mgr->ids_lru);
2560 adev->vm_manager.fence_context =
2561 dma_fence_context_alloc(AMDGPU_MAX_RINGS);
2562 for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
2563 adev->vm_manager.seqno[i] = 0;
2565 atomic_set(&adev->vm_manager.vm_pte_next_ring, 0);
2566 atomic64_set(&adev->vm_manager.client_counter, 0);
2567 spin_lock_init(&adev->vm_manager.prt_lock);
2568 atomic_set(&adev->vm_manager.num_prt_users, 0);
2570 /* If not overridden by the user, by default, only in large BAR systems
2571 * Compute VM tables will be updated by CPU
2573 #ifdef CONFIG_X86_64
2574 if (amdgpu_vm_update_mode == -1) {
2575 if (amdgpu_vm_is_large_bar(adev))
2576 adev->vm_manager.vm_update_mode =
2577 AMDGPU_VM_USE_CPU_FOR_COMPUTE;
2579 adev->vm_manager.vm_update_mode = 0;
2581 adev->vm_manager.vm_update_mode = amdgpu_vm_update_mode;
2583 adev->vm_manager.vm_update_mode = 0;
2589 * amdgpu_vm_manager_fini - cleanup VM manager
2591 * @adev: amdgpu_device pointer
2593 * Cleanup the VM manager and free resources.
2595 void amdgpu_vm_manager_fini(struct amdgpu_device *adev)
2599 for (i = 0; i < AMDGPU_MAX_VMHUBS; ++i) {
2600 struct amdgpu_vm_id_manager *id_mgr =
2601 &adev->vm_manager.id_mgr[i];
2603 mutex_destroy(&id_mgr->lock);
2604 for (j = 0; j < AMDGPU_NUM_VM; ++j) {
2605 struct amdgpu_vm_id *id = &id_mgr->ids[j];
2607 amdgpu_sync_free(&id->active);
2608 dma_fence_put(id->flushed_updates);
2609 dma_fence_put(id->last_flush);
2614 int amdgpu_vm_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
2616 union drm_amdgpu_vm *args = data;
2617 struct amdgpu_device *adev = dev->dev_private;
2618 struct amdgpu_fpriv *fpriv = filp->driver_priv;
2621 switch (args->in.op) {
2622 case AMDGPU_VM_OP_RESERVE_VMID:
2623 /* current, we only have requirement to reserve vmid from gfxhub */
2624 r = amdgpu_vm_alloc_reserved_vmid(adev, &fpriv->vm,
2629 case AMDGPU_VM_OP_UNRESERVE_VMID:
2630 amdgpu_vm_free_reserved_vmid(adev, &fpriv->vm, AMDGPU_GFXHUB);