2 * Copyright 2008 Advanced Micro Devices, Inc.
3 * Copyright 2008 Red Hat Inc.
4 * Copyright 2009 Jerome Glisse.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
24 * Authors: Dave Airlie
29 #include <drm/amdgpu_drm.h>
31 #include "amdgpu_trace.h"
35 * GPUVM is similar to the legacy gart on older asics, however
36 * rather than there being a single global gart table
37 * for the entire GPU, there are multiple VM page tables active
38 * at any given time. The VM page tables can contain a mix
39 * vram pages and system memory pages and system memory pages
40 * can be mapped as snooped (cached system pages) or unsnooped
41 * (uncached system pages).
42 * Each VM has an ID associated with it and there is a page table
43 * associated with each VMID. When execting a command buffer,
44 * the kernel tells the the ring what VMID to use for that command
45 * buffer. VMIDs are allocated dynamically as commands are submitted.
46 * The userspace drivers maintain their own address space and the kernel
47 * sets up their pages tables accordingly when they submit their
48 * command buffers and a VMID is assigned.
49 * Cayman/Trinity support up to 8 active VMs at any given time;
53 /* Special value that no flush is necessary */
54 #define AMDGPU_VM_NO_FLUSH (~0ll)
57 * amdgpu_vm_num_pde - return the number of page directory entries
59 * @adev: amdgpu_device pointer
61 * Calculate the number of page directory entries.
63 static unsigned amdgpu_vm_num_pdes(struct amdgpu_device *adev)
65 return adev->vm_manager.max_pfn >> amdgpu_vm_block_size;
69 * amdgpu_vm_directory_size - returns the size of the page directory in bytes
71 * @adev: amdgpu_device pointer
73 * Calculate the size of the page directory in bytes.
75 static unsigned amdgpu_vm_directory_size(struct amdgpu_device *adev)
77 return AMDGPU_GPU_PAGE_ALIGN(amdgpu_vm_num_pdes(adev) * 8);
81 * amdgpu_vm_get_pd_bo - add the VM PD to a validation list
83 * @vm: vm providing the BOs
84 * @validated: head of validation list
85 * @entry: entry to add
87 * Add the page directory to the list of BOs to
88 * validate for command submission.
90 void amdgpu_vm_get_pd_bo(struct amdgpu_vm *vm,
91 struct list_head *validated,
92 struct amdgpu_bo_list_entry *entry)
94 entry->robj = vm->page_directory;
96 entry->tv.bo = &vm->page_directory->tbo;
97 entry->tv.shared = true;
98 entry->user_pages = NULL;
99 list_add(&entry->tv.head, validated);
103 * amdgpu_vm_get_bos - add the vm BOs to a duplicates list
105 * @vm: vm providing the BOs
106 * @duplicates: head of duplicates list
108 * Add the page directory to the BO duplicates list
109 * for command submission.
111 void amdgpu_vm_get_pt_bos(struct amdgpu_vm *vm, struct list_head *duplicates)
115 /* add the vm page table to the list */
116 for (i = 0; i <= vm->max_pde_used; ++i) {
117 struct amdgpu_bo_list_entry *entry = &vm->page_tables[i].entry;
122 list_add(&entry->tv.head, duplicates);
128 * amdgpu_vm_move_pt_bos_in_lru - move the PT BOs to the LRU tail
130 * @adev: amdgpu device instance
131 * @vm: vm providing the BOs
133 * Move the PT BOs to the tail of the LRU.
135 void amdgpu_vm_move_pt_bos_in_lru(struct amdgpu_device *adev,
136 struct amdgpu_vm *vm)
138 struct ttm_bo_global *glob = adev->mman.bdev.glob;
141 spin_lock(&glob->lru_lock);
142 for (i = 0; i <= vm->max_pde_used; ++i) {
143 struct amdgpu_bo_list_entry *entry = &vm->page_tables[i].entry;
148 ttm_bo_move_to_lru_tail(&entry->robj->tbo);
150 spin_unlock(&glob->lru_lock);
154 * amdgpu_vm_grab_id - allocate the next free VMID
156 * @vm: vm to allocate id for
157 * @ring: ring we want to submit job to
158 * @sync: sync object where we add dependencies
159 * @fence: fence protecting ID from reuse
161 * Allocate an id for the vm, adding fences to the sync obj as necessary.
163 int amdgpu_vm_grab_id(struct amdgpu_vm *vm, struct amdgpu_ring *ring,
164 struct amdgpu_sync *sync, struct fence *fence,
165 unsigned *vm_id, uint64_t *vm_pd_addr)
167 uint64_t pd_addr = amdgpu_bo_gpu_offset(vm->page_directory);
168 struct amdgpu_device *adev = ring->adev;
169 struct amdgpu_vm_id *id = &vm->ids[ring->idx];
170 struct fence *updates = sync->last_vm_update;
173 mutex_lock(&adev->vm_manager.lock);
175 /* check if the id is still valid */
177 struct fence *flushed = id->flushed_updates;
186 is_later = fence_is_later(updates, flushed);
188 owner = atomic_long_read(&id->mgr_id->owner);
189 if (!is_later && owner == (long)id &&
190 pd_addr == id->pd_gpu_addr) {
192 r = amdgpu_sync_fence(ring->adev, sync,
195 mutex_unlock(&adev->vm_manager.lock);
199 fence_put(id->mgr_id->active);
200 id->mgr_id->active = fence_get(fence);
202 list_move_tail(&id->mgr_id->list,
203 &adev->vm_manager.ids_lru);
205 *vm_id = id->mgr_id - adev->vm_manager.ids;
206 *vm_pd_addr = AMDGPU_VM_NO_FLUSH;
207 trace_amdgpu_vm_grab_id(vm, ring->idx, *vm_id,
210 mutex_unlock(&adev->vm_manager.lock);
215 id->mgr_id = list_first_entry(&adev->vm_manager.ids_lru,
216 struct amdgpu_vm_manager_id,
219 r = amdgpu_sync_fence(ring->adev, sync, id->mgr_id->active);
221 fence_put(id->mgr_id->active);
222 id->mgr_id->active = fence_get(fence);
224 fence_put(id->flushed_updates);
225 id->flushed_updates = fence_get(updates);
227 id->pd_gpu_addr = pd_addr;
229 list_move_tail(&id->mgr_id->list, &adev->vm_manager.ids_lru);
230 atomic_long_set(&id->mgr_id->owner, (long)id);
232 *vm_id = id->mgr_id - adev->vm_manager.ids;
233 *vm_pd_addr = pd_addr;
234 trace_amdgpu_vm_grab_id(vm, ring->idx, *vm_id, *vm_pd_addr);
237 mutex_unlock(&adev->vm_manager.lock);
242 * amdgpu_vm_flush - hardware flush the vm
244 * @ring: ring to use for flush
245 * @vm_id: vmid number to use
246 * @pd_addr: address of the page directory
248 * Emit a VM flush when it is necessary.
250 void amdgpu_vm_flush(struct amdgpu_ring *ring,
251 unsigned vm_id, uint64_t pd_addr,
252 uint32_t gds_base, uint32_t gds_size,
253 uint32_t gws_base, uint32_t gws_size,
254 uint32_t oa_base, uint32_t oa_size)
256 struct amdgpu_device *adev = ring->adev;
257 struct amdgpu_vm_manager_id *mgr_id = &adev->vm_manager.ids[vm_id];
258 bool gds_switch_needed = ring->funcs->emit_gds_switch && (
259 mgr_id->gds_base != gds_base ||
260 mgr_id->gds_size != gds_size ||
261 mgr_id->gws_base != gws_base ||
262 mgr_id->gws_size != gws_size ||
263 mgr_id->oa_base != oa_base ||
264 mgr_id->oa_size != oa_size);
266 if (ring->funcs->emit_pipeline_sync && (
267 pd_addr != AMDGPU_VM_NO_FLUSH || gds_switch_needed))
268 amdgpu_ring_emit_pipeline_sync(ring);
270 if (pd_addr != AMDGPU_VM_NO_FLUSH) {
271 trace_amdgpu_vm_flush(pd_addr, ring->idx, vm_id);
272 amdgpu_ring_emit_vm_flush(ring, vm_id, pd_addr);
275 if (gds_switch_needed) {
276 mgr_id->gds_base = gds_base;
277 mgr_id->gds_size = gds_size;
278 mgr_id->gws_base = gws_base;
279 mgr_id->gws_size = gws_size;
280 mgr_id->oa_base = oa_base;
281 mgr_id->oa_size = oa_size;
282 amdgpu_ring_emit_gds_switch(ring, vm_id,
290 * amdgpu_vm_reset_id - reset VMID to zero
292 * @adev: amdgpu device structure
293 * @vm_id: vmid number to use
295 * Reset saved GDW, GWS and OA to force switch on next flush.
297 void amdgpu_vm_reset_id(struct amdgpu_device *adev, unsigned vm_id)
299 struct amdgpu_vm_manager_id *mgr_id = &adev->vm_manager.ids[vm_id];
301 mgr_id->gds_base = 0;
302 mgr_id->gds_size = 0;
303 mgr_id->gws_base = 0;
304 mgr_id->gws_size = 0;
310 * amdgpu_vm_bo_find - find the bo_va for a specific vm & bo
313 * @bo: requested buffer object
315 * Find @bo inside the requested vm.
316 * Search inside the @bos vm list for the requested vm
317 * Returns the found bo_va or NULL if none is found
319 * Object has to be reserved!
321 struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
322 struct amdgpu_bo *bo)
324 struct amdgpu_bo_va *bo_va;
326 list_for_each_entry(bo_va, &bo->va, bo_list) {
327 if (bo_va->vm == vm) {
335 * amdgpu_vm_update_pages - helper to call the right asic function
337 * @adev: amdgpu_device pointer
338 * @gtt: GART instance to use for mapping
339 * @gtt_flags: GTT hw access flags
340 * @ib: indirect buffer to fill with commands
341 * @pe: addr of the page entry
342 * @addr: dst addr to write into pe
343 * @count: number of page entries to update
344 * @incr: increase next addr by incr bytes
345 * @flags: hw access flags
347 * Traces the parameters and calls the right asic functions
348 * to setup the page table using the DMA.
350 static void amdgpu_vm_update_pages(struct amdgpu_device *adev,
351 struct amdgpu_gart *gtt,
353 struct amdgpu_ib *ib,
354 uint64_t pe, uint64_t addr,
355 unsigned count, uint32_t incr,
358 trace_amdgpu_vm_set_page(pe, addr, count, incr, flags);
360 if ((gtt == &adev->gart) && (flags == gtt_flags)) {
361 uint64_t src = gtt->table_addr + (addr >> 12) * 8;
362 amdgpu_vm_copy_pte(adev, ib, pe, src, count);
365 dma_addr_t *pages_addr = gtt->pages_addr;
366 amdgpu_vm_write_pte(adev, ib, pages_addr, pe, addr,
369 } else if (count < 3) {
370 amdgpu_vm_write_pte(adev, ib, NULL, pe, addr,
374 amdgpu_vm_set_pte_pde(adev, ib, pe, addr,
380 * amdgpu_vm_clear_bo - initially clear the page dir/table
382 * @adev: amdgpu_device pointer
385 * need to reserve bo first before calling it.
387 static int amdgpu_vm_clear_bo(struct amdgpu_device *adev,
388 struct amdgpu_vm *vm,
389 struct amdgpu_bo *bo)
391 struct amdgpu_ring *ring;
392 struct fence *fence = NULL;
393 struct amdgpu_job *job;
398 ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
400 r = reservation_object_reserve_shared(bo->tbo.resv);
404 r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
408 addr = amdgpu_bo_gpu_offset(bo);
409 entries = amdgpu_bo_size(bo) / 8;
411 r = amdgpu_job_alloc_with_ib(adev, 64, &job);
415 amdgpu_vm_update_pages(adev, NULL, 0, &job->ibs[0], addr, 0, entries,
417 amdgpu_ring_pad_ib(ring, &job->ibs[0]);
419 WARN_ON(job->ibs[0].length_dw > 64);
420 r = amdgpu_job_submit(job, ring, &vm->entity,
421 AMDGPU_FENCE_OWNER_VM, &fence);
425 amdgpu_bo_fence(bo, fence, true);
430 amdgpu_job_free(job);
437 * amdgpu_vm_map_gart - Resolve gart mapping of addr
439 * @pages_addr: optional DMA address to use for lookup
440 * @addr: the unmapped addr
442 * Look up the physical address of the page that the pte resolves
443 * to and return the pointer for the page table entry.
445 uint64_t amdgpu_vm_map_gart(const dma_addr_t *pages_addr, uint64_t addr)
450 /* page table offset */
451 result = pages_addr[addr >> PAGE_SHIFT];
453 /* in case cpu page size != gpu page size*/
454 result |= addr & (~PAGE_MASK);
457 /* No mapping required */
461 result &= 0xFFFFFFFFFFFFF000ULL;
467 * amdgpu_vm_update_pdes - make sure that page directory is valid
469 * @adev: amdgpu_device pointer
471 * @start: start of GPU address range
472 * @end: end of GPU address range
474 * Allocates new page tables if necessary
475 * and updates the page directory.
476 * Returns 0 for success, error for failure.
478 int amdgpu_vm_update_page_directory(struct amdgpu_device *adev,
479 struct amdgpu_vm *vm)
481 struct amdgpu_ring *ring;
482 struct amdgpu_bo *pd = vm->page_directory;
483 uint64_t pd_addr = amdgpu_bo_gpu_offset(pd);
484 uint32_t incr = AMDGPU_VM_PTE_COUNT * 8;
485 uint64_t last_pde = ~0, last_pt = ~0;
486 unsigned count = 0, pt_idx, ndw;
487 struct amdgpu_job *job;
488 struct amdgpu_ib *ib;
489 struct fence *fence = NULL;
493 ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
498 /* assume the worst case */
499 ndw += vm->max_pde_used * 6;
501 r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
507 /* walk over the address space and update the page directory */
508 for (pt_idx = 0; pt_idx <= vm->max_pde_used; ++pt_idx) {
509 struct amdgpu_bo *bo = vm->page_tables[pt_idx].entry.robj;
515 pt = amdgpu_bo_gpu_offset(bo);
516 if (vm->page_tables[pt_idx].addr == pt)
518 vm->page_tables[pt_idx].addr = pt;
520 pde = pd_addr + pt_idx * 8;
521 if (((last_pde + 8 * count) != pde) ||
522 ((last_pt + incr * count) != pt)) {
525 amdgpu_vm_update_pages(adev, NULL, 0, ib,
540 amdgpu_vm_update_pages(adev, NULL, 0, ib, last_pde, last_pt,
541 count, incr, AMDGPU_PTE_VALID);
543 if (ib->length_dw != 0) {
544 amdgpu_ring_pad_ib(ring, ib);
545 amdgpu_sync_resv(adev, &job->sync, pd->tbo.resv,
546 AMDGPU_FENCE_OWNER_VM);
547 WARN_ON(ib->length_dw > ndw);
548 r = amdgpu_job_submit(job, ring, &vm->entity,
549 AMDGPU_FENCE_OWNER_VM, &fence);
553 amdgpu_bo_fence(pd, fence, true);
554 fence_put(vm->page_directory_fence);
555 vm->page_directory_fence = fence_get(fence);
559 amdgpu_job_free(job);
565 amdgpu_job_free(job);
570 * amdgpu_vm_frag_ptes - add fragment information to PTEs
572 * @adev: amdgpu_device pointer
573 * @gtt: GART instance to use for mapping
574 * @gtt_flags: GTT hw mapping flags
575 * @ib: IB for the update
576 * @pe_start: first PTE to handle
577 * @pe_end: last PTE to handle
578 * @addr: addr those PTEs should point to
579 * @flags: hw mapping flags
581 static void amdgpu_vm_frag_ptes(struct amdgpu_device *adev,
582 struct amdgpu_gart *gtt,
584 struct amdgpu_ib *ib,
585 uint64_t pe_start, uint64_t pe_end,
586 uint64_t addr, uint32_t flags)
589 * The MC L1 TLB supports variable sized pages, based on a fragment
590 * field in the PTE. When this field is set to a non-zero value, page
591 * granularity is increased from 4KB to (1 << (12 + frag)). The PTE
592 * flags are considered valid for all PTEs within the fragment range
593 * and corresponding mappings are assumed to be physically contiguous.
595 * The L1 TLB can store a single PTE for the whole fragment,
596 * significantly increasing the space available for translation
597 * caching. This leads to large improvements in throughput when the
598 * TLB is under pressure.
600 * The L2 TLB distributes small and large fragments into two
601 * asymmetric partitions. The large fragment cache is significantly
602 * larger. Thus, we try to use large fragments wherever possible.
603 * Userspace can support this by aligning virtual base address and
604 * allocation size to the fragment size.
607 /* SI and newer are optimized for 64KB */
608 uint64_t frag_flags = AMDGPU_PTE_FRAG_64KB;
609 uint64_t frag_align = 0x80;
611 uint64_t frag_start = ALIGN(pe_start, frag_align);
612 uint64_t frag_end = pe_end & ~(frag_align - 1);
616 /* Abort early if there isn't anything to do */
617 if (pe_start == pe_end)
620 /* system pages are non continuously */
621 if (gtt || !(flags & AMDGPU_PTE_VALID) || (frag_start >= frag_end)) {
623 count = (pe_end - pe_start) / 8;
624 amdgpu_vm_update_pages(adev, gtt, gtt_flags, ib, pe_start,
625 addr, count, AMDGPU_GPU_PAGE_SIZE,
630 /* handle the 4K area at the beginning */
631 if (pe_start != frag_start) {
632 count = (frag_start - pe_start) / 8;
633 amdgpu_vm_update_pages(adev, NULL, 0, ib, pe_start, addr,
634 count, AMDGPU_GPU_PAGE_SIZE, flags);
635 addr += AMDGPU_GPU_PAGE_SIZE * count;
638 /* handle the area in the middle */
639 count = (frag_end - frag_start) / 8;
640 amdgpu_vm_update_pages(adev, NULL, 0, ib, frag_start, addr, count,
641 AMDGPU_GPU_PAGE_SIZE, flags | frag_flags);
643 /* handle the 4K area at the end */
644 if (frag_end != pe_end) {
645 addr += AMDGPU_GPU_PAGE_SIZE * count;
646 count = (pe_end - frag_end) / 8;
647 amdgpu_vm_update_pages(adev, NULL, 0, ib, frag_end, addr,
648 count, AMDGPU_GPU_PAGE_SIZE, flags);
653 * amdgpu_vm_update_ptes - make sure that page tables are valid
655 * @adev: amdgpu_device pointer
656 * @gtt: GART instance to use for mapping
657 * @gtt_flags: GTT hw mapping flags
659 * @start: start of GPU address range
660 * @end: end of GPU address range
661 * @dst: destination address to map to
662 * @flags: mapping flags
664 * Update the page tables in the range @start - @end.
666 static void amdgpu_vm_update_ptes(struct amdgpu_device *adev,
667 struct amdgpu_gart *gtt,
669 struct amdgpu_vm *vm,
670 struct amdgpu_ib *ib,
671 uint64_t start, uint64_t end,
672 uint64_t dst, uint32_t flags)
674 const uint64_t mask = AMDGPU_VM_PTE_COUNT - 1;
676 uint64_t last_pe_start = ~0, last_pe_end = ~0, last_dst = ~0;
679 /* walk over the address space and update the page tables */
680 for (addr = start; addr < end; ) {
681 uint64_t pt_idx = addr >> amdgpu_vm_block_size;
682 struct amdgpu_bo *pt = vm->page_tables[pt_idx].entry.robj;
686 if ((addr & ~mask) == (end & ~mask))
689 nptes = AMDGPU_VM_PTE_COUNT - (addr & mask);
691 pe_start = amdgpu_bo_gpu_offset(pt);
692 pe_start += (addr & mask) * 8;
694 if (last_pe_end != pe_start) {
696 amdgpu_vm_frag_ptes(adev, gtt, gtt_flags, ib,
697 last_pe_start, last_pe_end,
700 last_pe_start = pe_start;
701 last_pe_end = pe_start + 8 * nptes;
704 last_pe_end += 8 * nptes;
708 dst += nptes * AMDGPU_GPU_PAGE_SIZE;
711 amdgpu_vm_frag_ptes(adev, gtt, gtt_flags, ib,
712 last_pe_start, last_pe_end,
717 * amdgpu_vm_bo_update_mapping - update a mapping in the vm page table
719 * @adev: amdgpu_device pointer
720 * @gtt: GART instance to use for mapping
721 * @gtt_flags: flags as they are used for GTT
723 * @start: start of mapped range
724 * @last: last mapped entry
725 * @flags: flags for the entries
726 * @addr: addr to set the area to
727 * @fence: optional resulting fence
729 * Fill in the page table entries between @start and @last.
730 * Returns 0 for success, -EINVAL for failure.
732 static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
733 struct amdgpu_gart *gtt,
735 struct amdgpu_vm *vm,
736 uint64_t start, uint64_t last,
737 uint32_t flags, uint64_t addr,
738 struct fence **fence)
740 struct amdgpu_ring *ring;
741 void *owner = AMDGPU_FENCE_OWNER_VM;
742 unsigned nptes, ncmds, ndw;
743 struct amdgpu_job *job;
744 struct amdgpu_ib *ib;
745 struct fence *f = NULL;
748 ring = container_of(vm->entity.sched, struct amdgpu_ring, sched);
750 /* sync to everything on unmapping */
751 if (!(flags & AMDGPU_PTE_VALID))
752 owner = AMDGPU_FENCE_OWNER_UNDEFINED;
754 nptes = last - start + 1;
757 * reserve space for one command every (1 << BLOCK_SIZE)
758 * entries or 2k dwords (whatever is smaller)
760 ncmds = (nptes >> min(amdgpu_vm_block_size, 11)) + 1;
765 if ((gtt == &adev->gart) && (flags == gtt_flags)) {
766 /* only copy commands needed */
770 /* header for write data commands */
773 /* body of write data command */
777 /* set page commands needed */
780 /* two extra commands for begin/end of fragment */
784 r = amdgpu_job_alloc_with_ib(adev, ndw * 4, &job);
790 r = amdgpu_sync_resv(adev, &job->sync, vm->page_directory->tbo.resv,
795 r = reservation_object_reserve_shared(vm->page_directory->tbo.resv);
799 amdgpu_vm_update_ptes(adev, gtt, gtt_flags, vm, ib, start, last + 1,
802 amdgpu_ring_pad_ib(ring, ib);
803 WARN_ON(ib->length_dw > ndw);
804 r = amdgpu_job_submit(job, ring, &vm->entity,
805 AMDGPU_FENCE_OWNER_VM, &f);
809 amdgpu_bo_fence(vm->page_directory, f, true);
812 *fence = fence_get(f);
818 amdgpu_job_free(job);
823 * amdgpu_vm_bo_split_mapping - split a mapping into smaller chunks
825 * @adev: amdgpu_device pointer
826 * @gtt: GART instance to use for mapping
828 * @mapping: mapped range and flags to use for the update
829 * @addr: addr to set the area to
830 * @gtt_flags: flags as they are used for GTT
831 * @fence: optional resulting fence
833 * Split the mapping into smaller chunks so that each update fits
835 * Returns 0 for success, -EINVAL for failure.
837 static int amdgpu_vm_bo_split_mapping(struct amdgpu_device *adev,
838 struct amdgpu_gart *gtt,
840 struct amdgpu_vm *vm,
841 struct amdgpu_bo_va_mapping *mapping,
842 uint64_t addr, struct fence **fence)
844 const uint64_t max_size = 64ULL * 1024ULL * 1024ULL / AMDGPU_GPU_PAGE_SIZE;
846 uint64_t start = mapping->it.start;
847 uint32_t flags = gtt_flags;
850 /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here
851 * but in case of something, we filter the flags in first place
853 if (!(mapping->flags & AMDGPU_PTE_READABLE))
854 flags &= ~AMDGPU_PTE_READABLE;
855 if (!(mapping->flags & AMDGPU_PTE_WRITEABLE))
856 flags &= ~AMDGPU_PTE_WRITEABLE;
858 trace_amdgpu_vm_bo_update(mapping);
860 addr += mapping->offset;
862 if (!gtt || ((gtt == &adev->gart) && (flags == gtt_flags)))
863 return amdgpu_vm_bo_update_mapping(adev, gtt, gtt_flags, vm,
864 start, mapping->it.last,
867 while (start != mapping->it.last + 1) {
870 last = min((uint64_t)mapping->it.last, start + max_size);
871 r = amdgpu_vm_bo_update_mapping(adev, gtt, gtt_flags, vm,
872 start, last, flags, addr,
885 * amdgpu_vm_bo_update - update all BO mappings in the vm page table
887 * @adev: amdgpu_device pointer
888 * @bo_va: requested BO and VM object
891 * Fill in the page table entries for @bo_va.
892 * Returns 0 for success, -EINVAL for failure.
894 * Object have to be reserved and mutex must be locked!
896 int amdgpu_vm_bo_update(struct amdgpu_device *adev,
897 struct amdgpu_bo_va *bo_va,
898 struct ttm_mem_reg *mem)
900 struct amdgpu_vm *vm = bo_va->vm;
901 struct amdgpu_bo_va_mapping *mapping;
902 struct amdgpu_gart *gtt = NULL;
908 addr = (u64)mem->start << PAGE_SHIFT;
909 switch (mem->mem_type) {
911 gtt = &bo_va->bo->adev->gart;
915 addr += adev->vm_manager.vram_base_offset;
925 flags = amdgpu_ttm_tt_pte_flags(adev, bo_va->bo->tbo.ttm, mem);
927 spin_lock(&vm->status_lock);
928 if (!list_empty(&bo_va->vm_status))
929 list_splice_init(&bo_va->valids, &bo_va->invalids);
930 spin_unlock(&vm->status_lock);
932 list_for_each_entry(mapping, &bo_va->invalids, list) {
933 r = amdgpu_vm_bo_split_mapping(adev, gtt, flags, vm, mapping, addr,
934 &bo_va->last_pt_update);
939 if (trace_amdgpu_vm_bo_mapping_enabled()) {
940 list_for_each_entry(mapping, &bo_va->valids, list)
941 trace_amdgpu_vm_bo_mapping(mapping);
943 list_for_each_entry(mapping, &bo_va->invalids, list)
944 trace_amdgpu_vm_bo_mapping(mapping);
947 spin_lock(&vm->status_lock);
948 list_splice_init(&bo_va->invalids, &bo_va->valids);
949 list_del_init(&bo_va->vm_status);
951 list_add(&bo_va->vm_status, &vm->cleared);
952 spin_unlock(&vm->status_lock);
958 * amdgpu_vm_clear_freed - clear freed BOs in the PT
960 * @adev: amdgpu_device pointer
963 * Make sure all freed BOs are cleared in the PT.
964 * Returns 0 for success.
966 * PTs have to be reserved and mutex must be locked!
968 int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
969 struct amdgpu_vm *vm)
971 struct amdgpu_bo_va_mapping *mapping;
974 spin_lock(&vm->freed_lock);
975 while (!list_empty(&vm->freed)) {
976 mapping = list_first_entry(&vm->freed,
977 struct amdgpu_bo_va_mapping, list);
978 list_del(&mapping->list);
979 spin_unlock(&vm->freed_lock);
980 r = amdgpu_vm_bo_split_mapping(adev, NULL, 0, vm, mapping,
986 spin_lock(&vm->freed_lock);
988 spin_unlock(&vm->freed_lock);
995 * amdgpu_vm_clear_invalids - clear invalidated BOs in the PT
997 * @adev: amdgpu_device pointer
1000 * Make sure all invalidated BOs are cleared in the PT.
1001 * Returns 0 for success.
1003 * PTs have to be reserved and mutex must be locked!
1005 int amdgpu_vm_clear_invalids(struct amdgpu_device *adev,
1006 struct amdgpu_vm *vm, struct amdgpu_sync *sync)
1008 struct amdgpu_bo_va *bo_va = NULL;
1011 spin_lock(&vm->status_lock);
1012 while (!list_empty(&vm->invalidated)) {
1013 bo_va = list_first_entry(&vm->invalidated,
1014 struct amdgpu_bo_va, vm_status);
1015 spin_unlock(&vm->status_lock);
1016 mutex_lock(&bo_va->mutex);
1017 r = amdgpu_vm_bo_update(adev, bo_va, NULL);
1018 mutex_unlock(&bo_va->mutex);
1022 spin_lock(&vm->status_lock);
1024 spin_unlock(&vm->status_lock);
1027 r = amdgpu_sync_fence(adev, sync, bo_va->last_pt_update);
1033 * amdgpu_vm_bo_add - add a bo to a specific vm
1035 * @adev: amdgpu_device pointer
1037 * @bo: amdgpu buffer object
1039 * Add @bo into the requested vm.
1040 * Add @bo to the list of bos associated with the vm
1041 * Returns newly added bo_va or NULL for failure
1043 * Object has to be reserved!
1045 struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
1046 struct amdgpu_vm *vm,
1047 struct amdgpu_bo *bo)
1049 struct amdgpu_bo_va *bo_va;
1051 bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL);
1052 if (bo_va == NULL) {
1057 bo_va->ref_count = 1;
1058 INIT_LIST_HEAD(&bo_va->bo_list);
1059 INIT_LIST_HEAD(&bo_va->valids);
1060 INIT_LIST_HEAD(&bo_va->invalids);
1061 INIT_LIST_HEAD(&bo_va->vm_status);
1062 mutex_init(&bo_va->mutex);
1063 list_add_tail(&bo_va->bo_list, &bo->va);
1069 * amdgpu_vm_bo_map - map bo inside a vm
1071 * @adev: amdgpu_device pointer
1072 * @bo_va: bo_va to store the address
1073 * @saddr: where to map the BO
1074 * @offset: requested offset in the BO
1075 * @flags: attributes of pages (read/write/valid/etc.)
1077 * Add a mapping of the BO at the specefied addr into the VM.
1078 * Returns 0 for success, error for failure.
1080 * Object has to be reserved and unreserved outside!
1082 int amdgpu_vm_bo_map(struct amdgpu_device *adev,
1083 struct amdgpu_bo_va *bo_va,
1084 uint64_t saddr, uint64_t offset,
1085 uint64_t size, uint32_t flags)
1087 struct amdgpu_bo_va_mapping *mapping;
1088 struct amdgpu_vm *vm = bo_va->vm;
1089 struct interval_tree_node *it;
1090 unsigned last_pfn, pt_idx;
1094 /* validate the parameters */
1095 if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
1096 size == 0 || size & AMDGPU_GPU_PAGE_MASK)
1099 /* make sure object fit at this offset */
1100 eaddr = saddr + size - 1;
1101 if ((saddr >= eaddr) || (offset + size > amdgpu_bo_size(bo_va->bo)))
1104 last_pfn = eaddr / AMDGPU_GPU_PAGE_SIZE;
1105 if (last_pfn >= adev->vm_manager.max_pfn) {
1106 dev_err(adev->dev, "va above limit (0x%08X >= 0x%08X)\n",
1107 last_pfn, adev->vm_manager.max_pfn);
1111 saddr /= AMDGPU_GPU_PAGE_SIZE;
1112 eaddr /= AMDGPU_GPU_PAGE_SIZE;
1114 spin_lock(&vm->it_lock);
1115 it = interval_tree_iter_first(&vm->va, saddr, eaddr);
1116 spin_unlock(&vm->it_lock);
1118 struct amdgpu_bo_va_mapping *tmp;
1119 tmp = container_of(it, struct amdgpu_bo_va_mapping, it);
1120 /* bo and tmp overlap, invalid addr */
1121 dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with "
1122 "0x%010lx-0x%010lx\n", bo_va->bo, saddr, eaddr,
1123 tmp->it.start, tmp->it.last + 1);
1128 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
1134 INIT_LIST_HEAD(&mapping->list);
1135 mapping->it.start = saddr;
1136 mapping->it.last = eaddr;
1137 mapping->offset = offset;
1138 mapping->flags = flags;
1140 mutex_lock(&bo_va->mutex);
1141 list_add(&mapping->list, &bo_va->invalids);
1142 mutex_unlock(&bo_va->mutex);
1143 spin_lock(&vm->it_lock);
1144 interval_tree_insert(&mapping->it, &vm->va);
1145 spin_unlock(&vm->it_lock);
1146 trace_amdgpu_vm_bo_map(bo_va, mapping);
1148 /* Make sure the page tables are allocated */
1149 saddr >>= amdgpu_vm_block_size;
1150 eaddr >>= amdgpu_vm_block_size;
1152 BUG_ON(eaddr >= amdgpu_vm_num_pdes(adev));
1154 if (eaddr > vm->max_pde_used)
1155 vm->max_pde_used = eaddr;
1157 /* walk over the address space and allocate the page tables */
1158 for (pt_idx = saddr; pt_idx <= eaddr; ++pt_idx) {
1159 struct reservation_object *resv = vm->page_directory->tbo.resv;
1160 struct amdgpu_bo_list_entry *entry;
1161 struct amdgpu_bo *pt;
1163 entry = &vm->page_tables[pt_idx].entry;
1167 r = amdgpu_bo_create(adev, AMDGPU_VM_PTE_COUNT * 8,
1168 AMDGPU_GPU_PAGE_SIZE, true,
1169 AMDGPU_GEM_DOMAIN_VRAM,
1170 AMDGPU_GEM_CREATE_NO_CPU_ACCESS,
1175 /* Keep a reference to the page table to avoid freeing
1176 * them up in the wrong order.
1178 pt->parent = amdgpu_bo_ref(vm->page_directory);
1180 r = amdgpu_vm_clear_bo(adev, vm, pt);
1182 amdgpu_bo_unref(&pt);
1187 entry->priority = 0;
1188 entry->tv.bo = &entry->robj->tbo;
1189 entry->tv.shared = true;
1190 entry->user_pages = NULL;
1191 vm->page_tables[pt_idx].addr = 0;
1197 list_del(&mapping->list);
1198 spin_lock(&vm->it_lock);
1199 interval_tree_remove(&mapping->it, &vm->va);
1200 spin_unlock(&vm->it_lock);
1201 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
1209 * amdgpu_vm_bo_unmap - remove bo mapping from vm
1211 * @adev: amdgpu_device pointer
1212 * @bo_va: bo_va to remove the address from
1213 * @saddr: where to the BO is mapped
1215 * Remove a mapping of the BO at the specefied addr from the VM.
1216 * Returns 0 for success, error for failure.
1218 * Object has to be reserved and unreserved outside!
1220 int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
1221 struct amdgpu_bo_va *bo_va,
1224 struct amdgpu_bo_va_mapping *mapping;
1225 struct amdgpu_vm *vm = bo_va->vm;
1228 saddr /= AMDGPU_GPU_PAGE_SIZE;
1229 mutex_lock(&bo_va->mutex);
1230 list_for_each_entry(mapping, &bo_va->valids, list) {
1231 if (mapping->it.start == saddr)
1235 if (&mapping->list == &bo_va->valids) {
1238 list_for_each_entry(mapping, &bo_va->invalids, list) {
1239 if (mapping->it.start == saddr)
1243 if (&mapping->list == &bo_va->invalids) {
1244 mutex_unlock(&bo_va->mutex);
1248 mutex_unlock(&bo_va->mutex);
1249 list_del(&mapping->list);
1250 spin_lock(&vm->it_lock);
1251 interval_tree_remove(&mapping->it, &vm->va);
1252 spin_unlock(&vm->it_lock);
1253 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
1256 spin_lock(&vm->freed_lock);
1257 list_add(&mapping->list, &vm->freed);
1258 spin_unlock(&vm->freed_lock);
1267 * amdgpu_vm_bo_rmv - remove a bo to a specific vm
1269 * @adev: amdgpu_device pointer
1270 * @bo_va: requested bo_va
1272 * Remove @bo_va->bo from the requested vm.
1274 * Object have to be reserved!
1276 void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
1277 struct amdgpu_bo_va *bo_va)
1279 struct amdgpu_bo_va_mapping *mapping, *next;
1280 struct amdgpu_vm *vm = bo_va->vm;
1282 list_del(&bo_va->bo_list);
1284 spin_lock(&vm->status_lock);
1285 list_del(&bo_va->vm_status);
1286 spin_unlock(&vm->status_lock);
1288 list_for_each_entry_safe(mapping, next, &bo_va->valids, list) {
1289 list_del(&mapping->list);
1290 spin_lock(&vm->it_lock);
1291 interval_tree_remove(&mapping->it, &vm->va);
1292 spin_unlock(&vm->it_lock);
1293 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
1294 spin_lock(&vm->freed_lock);
1295 list_add(&mapping->list, &vm->freed);
1296 spin_unlock(&vm->freed_lock);
1298 list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) {
1299 list_del(&mapping->list);
1300 spin_lock(&vm->it_lock);
1301 interval_tree_remove(&mapping->it, &vm->va);
1302 spin_unlock(&vm->it_lock);
1305 fence_put(bo_va->last_pt_update);
1306 mutex_destroy(&bo_va->mutex);
1311 * amdgpu_vm_bo_invalidate - mark the bo as invalid
1313 * @adev: amdgpu_device pointer
1315 * @bo: amdgpu buffer object
1317 * Mark @bo as invalid.
1319 void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
1320 struct amdgpu_bo *bo)
1322 struct amdgpu_bo_va *bo_va;
1324 list_for_each_entry(bo_va, &bo->va, bo_list) {
1325 spin_lock(&bo_va->vm->status_lock);
1326 if (list_empty(&bo_va->vm_status))
1327 list_add(&bo_va->vm_status, &bo_va->vm->invalidated);
1328 spin_unlock(&bo_va->vm->status_lock);
1333 * amdgpu_vm_init - initialize a vm instance
1335 * @adev: amdgpu_device pointer
1340 int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1342 const unsigned align = min(AMDGPU_VM_PTB_ALIGN_SIZE,
1343 AMDGPU_VM_PTE_COUNT * 8);
1344 unsigned pd_size, pd_entries;
1345 unsigned ring_instance;
1346 struct amdgpu_ring *ring;
1347 struct amd_sched_rq *rq;
1350 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
1351 vm->ids[i].mgr_id = NULL;
1352 vm->ids[i].flushed_updates = NULL;
1355 spin_lock_init(&vm->status_lock);
1356 INIT_LIST_HEAD(&vm->invalidated);
1357 INIT_LIST_HEAD(&vm->cleared);
1358 INIT_LIST_HEAD(&vm->freed);
1359 spin_lock_init(&vm->it_lock);
1360 spin_lock_init(&vm->freed_lock);
1361 pd_size = amdgpu_vm_directory_size(adev);
1362 pd_entries = amdgpu_vm_num_pdes(adev);
1364 /* allocate page table array */
1365 vm->page_tables = drm_calloc_large(pd_entries, sizeof(struct amdgpu_vm_pt));
1366 if (vm->page_tables == NULL) {
1367 DRM_ERROR("Cannot allocate memory for page table array\n");
1371 /* create scheduler entity for page table updates */
1373 ring_instance = atomic_inc_return(&adev->vm_manager.vm_pte_next_ring);
1374 ring_instance %= adev->vm_manager.vm_pte_num_rings;
1375 ring = adev->vm_manager.vm_pte_rings[ring_instance];
1376 rq = &ring->sched.sched_rq[AMD_SCHED_PRIORITY_KERNEL];
1377 r = amd_sched_entity_init(&ring->sched, &vm->entity,
1378 rq, amdgpu_sched_jobs);
1382 vm->page_directory_fence = NULL;
1384 r = amdgpu_bo_create(adev, pd_size, align, true,
1385 AMDGPU_GEM_DOMAIN_VRAM,
1386 AMDGPU_GEM_CREATE_NO_CPU_ACCESS,
1387 NULL, NULL, &vm->page_directory);
1389 goto error_free_sched_entity;
1391 r = amdgpu_bo_reserve(vm->page_directory, false);
1393 goto error_free_page_directory;
1395 r = amdgpu_vm_clear_bo(adev, vm, vm->page_directory);
1396 amdgpu_bo_unreserve(vm->page_directory);
1398 goto error_free_page_directory;
1402 error_free_page_directory:
1403 amdgpu_bo_unref(&vm->page_directory);
1404 vm->page_directory = NULL;
1406 error_free_sched_entity:
1407 amd_sched_entity_fini(&ring->sched, &vm->entity);
1413 * amdgpu_vm_fini - tear down a vm instance
1415 * @adev: amdgpu_device pointer
1419 * Unbind the VM and remove all bos from the vm bo list
1421 void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1423 struct amdgpu_bo_va_mapping *mapping, *tmp;
1426 amd_sched_entity_fini(vm->entity.sched, &vm->entity);
1428 if (!RB_EMPTY_ROOT(&vm->va)) {
1429 dev_err(adev->dev, "still active bo inside vm\n");
1431 rbtree_postorder_for_each_entry_safe(mapping, tmp, &vm->va, it.rb) {
1432 list_del(&mapping->list);
1433 interval_tree_remove(&mapping->it, &vm->va);
1436 list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
1437 list_del(&mapping->list);
1441 for (i = 0; i < amdgpu_vm_num_pdes(adev); i++)
1442 amdgpu_bo_unref(&vm->page_tables[i].entry.robj);
1443 drm_free_large(vm->page_tables);
1445 amdgpu_bo_unref(&vm->page_directory);
1446 fence_put(vm->page_directory_fence);
1447 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
1448 struct amdgpu_vm_id *id = &vm->ids[i];
1451 atomic_long_cmpxchg(&id->mgr_id->owner,
1453 fence_put(id->flushed_updates);
1458 * amdgpu_vm_manager_init - init the VM manager
1460 * @adev: amdgpu_device pointer
1462 * Initialize the VM manager structures
1464 void amdgpu_vm_manager_init(struct amdgpu_device *adev)
1468 INIT_LIST_HEAD(&adev->vm_manager.ids_lru);
1470 /* skip over VMID 0, since it is the system VM */
1471 for (i = 1; i < adev->vm_manager.num_ids; ++i) {
1472 amdgpu_vm_reset_id(adev, i);
1473 list_add_tail(&adev->vm_manager.ids[i].list,
1474 &adev->vm_manager.ids_lru);
1477 atomic_set(&adev->vm_manager.vm_pte_next_ring, 0);
1481 * amdgpu_vm_manager_fini - cleanup VM manager
1483 * @adev: amdgpu_device pointer
1485 * Cleanup the VM manager and free resources.
1487 void amdgpu_vm_manager_fini(struct amdgpu_device *adev)
1491 for (i = 0; i < AMDGPU_NUM_VM; ++i)
1492 fence_put(adev->vm_manager.ids[i].active);