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;
54 * amdgpu_vm_num_pde - return the number of page directory entries
56 * @adev: amdgpu_device pointer
58 * Calculate the number of page directory entries (cayman+).
60 static unsigned amdgpu_vm_num_pdes(struct amdgpu_device *adev)
62 return adev->vm_manager.max_pfn >> amdgpu_vm_block_size;
66 * amdgpu_vm_directory_size - returns the size of the page directory in bytes
68 * @adev: amdgpu_device pointer
70 * Calculate the size of the page directory in bytes (cayman+).
72 static unsigned amdgpu_vm_directory_size(struct amdgpu_device *adev)
74 return AMDGPU_GPU_PAGE_ALIGN(amdgpu_vm_num_pdes(adev) * 8);
78 * amdgpu_vm_get_bos - add the vm BOs to a validation list
80 * @vm: vm providing the BOs
81 * @head: head of validation list
83 * Add the page directory to the list of BOs to
84 * validate for command submission (cayman+).
86 struct amdgpu_bo_list_entry *amdgpu_vm_get_bos(struct amdgpu_device *adev,
88 struct list_head *head)
90 struct amdgpu_bo_list_entry *list;
93 list = drm_malloc_ab(vm->max_pde_used + 2,
94 sizeof(struct amdgpu_bo_list_entry));
98 /* add the vm page table to the list */
99 list[0].robj = vm->page_directory;
100 list[0].prefered_domains = AMDGPU_GEM_DOMAIN_VRAM;
101 list[0].allowed_domains = AMDGPU_GEM_DOMAIN_VRAM;
102 list[0].priority = 0;
103 list[0].tv.bo = &vm->page_directory->tbo;
104 list[0].tv.shared = true;
105 list_add(&list[0].tv.head, head);
107 for (i = 0, idx = 1; i <= vm->max_pde_used; i++) {
108 if (!vm->page_tables[i].bo)
111 list[idx].robj = vm->page_tables[i].bo;
112 list[idx].prefered_domains = AMDGPU_GEM_DOMAIN_VRAM;
113 list[idx].allowed_domains = AMDGPU_GEM_DOMAIN_VRAM;
114 list[idx].priority = 0;
115 list[idx].tv.bo = &list[idx].robj->tbo;
116 list[idx].tv.shared = true;
117 list_add(&list[idx++].tv.head, head);
124 * amdgpu_vm_grab_id - allocate the next free VMID
126 * @ring: ring we want to submit job to
127 * @vm: vm to allocate id for
129 * Allocate an id for the vm (cayman+).
130 * Returns the fence we need to sync to (if any).
132 * Global and local mutex must be locked!
134 struct amdgpu_fence *amdgpu_vm_grab_id(struct amdgpu_ring *ring,
135 struct amdgpu_vm *vm)
137 struct amdgpu_fence *best[AMDGPU_MAX_RINGS] = {};
138 struct amdgpu_vm_id *vm_id = &vm->ids[ring->idx];
139 struct amdgpu_device *adev = ring->adev;
141 unsigned choices[2] = {};
144 /* check if the id is still valid */
145 if (vm_id->id && vm_id->last_id_use &&
146 vm_id->last_id_use == adev->vm_manager.active[vm_id->id])
149 /* we definately need to flush */
150 vm_id->pd_gpu_addr = ~0ll;
152 /* skip over VMID 0, since it is the system VM */
153 for (i = 1; i < adev->vm_manager.nvm; ++i) {
154 struct amdgpu_fence *fence = adev->vm_manager.active[i];
157 /* found a free one */
159 trace_amdgpu_vm_grab_id(i, ring->idx);
163 if (amdgpu_fence_is_earlier(fence, best[fence->ring->idx])) {
164 best[fence->ring->idx] = fence;
165 choices[fence->ring == ring ? 0 : 1] = i;
169 for (i = 0; i < 2; ++i) {
171 vm_id->id = choices[i];
172 trace_amdgpu_vm_grab_id(choices[i], ring->idx);
173 return adev->vm_manager.active[choices[i]];
177 /* should never happen */
183 * amdgpu_vm_flush - hardware flush the vm
185 * @ring: ring to use for flush
186 * @vm: vm we want to flush
187 * @updates: last vm update that we waited for
189 * Flush the vm (cayman+).
191 * Global and local mutex must be locked!
193 void amdgpu_vm_flush(struct amdgpu_ring *ring,
194 struct amdgpu_vm *vm,
195 struct amdgpu_fence *updates)
197 uint64_t pd_addr = amdgpu_bo_gpu_offset(vm->page_directory);
198 struct amdgpu_vm_id *vm_id = &vm->ids[ring->idx];
200 if (pd_addr != vm_id->pd_gpu_addr || !vm_id->flushed_updates ||
201 amdgpu_fence_is_earlier(vm_id->flushed_updates, updates)) {
203 trace_amdgpu_vm_flush(pd_addr, ring->idx, vm_id->id);
204 amdgpu_fence_unref(&vm_id->flushed_updates);
205 vm_id->flushed_updates = amdgpu_fence_ref(updates);
206 vm_id->pd_gpu_addr = pd_addr;
207 amdgpu_ring_emit_vm_flush(ring, vm_id->id, vm_id->pd_gpu_addr);
212 * amdgpu_vm_fence - remember fence for vm
214 * @adev: amdgpu_device pointer
215 * @vm: vm we want to fence
216 * @fence: fence to remember
218 * Fence the vm (cayman+).
219 * Set the fence used to protect page table and id.
221 * Global and local mutex must be locked!
223 void amdgpu_vm_fence(struct amdgpu_device *adev,
224 struct amdgpu_vm *vm,
225 struct amdgpu_fence *fence)
227 unsigned ridx = fence->ring->idx;
228 unsigned vm_id = vm->ids[ridx].id;
230 amdgpu_fence_unref(&adev->vm_manager.active[vm_id]);
231 adev->vm_manager.active[vm_id] = amdgpu_fence_ref(fence);
233 amdgpu_fence_unref(&vm->ids[ridx].last_id_use);
234 vm->ids[ridx].last_id_use = amdgpu_fence_ref(fence);
238 * amdgpu_vm_bo_find - find the bo_va for a specific vm & bo
241 * @bo: requested buffer object
243 * Find @bo inside the requested vm (cayman+).
244 * Search inside the @bos vm list for the requested vm
245 * Returns the found bo_va or NULL if none is found
247 * Object has to be reserved!
249 struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
250 struct amdgpu_bo *bo)
252 struct amdgpu_bo_va *bo_va;
254 list_for_each_entry(bo_va, &bo->va, bo_list) {
255 if (bo_va->vm == vm) {
263 * amdgpu_vm_update_pages - helper to call the right asic function
265 * @adev: amdgpu_device pointer
266 * @ib: indirect buffer to fill with commands
267 * @pe: addr of the page entry
268 * @addr: dst addr to write into pe
269 * @count: number of page entries to update
270 * @incr: increase next addr by incr bytes
271 * @flags: hw access flags
272 * @gtt_flags: GTT hw access flags
274 * Traces the parameters and calls the right asic functions
275 * to setup the page table using the DMA.
277 static void amdgpu_vm_update_pages(struct amdgpu_device *adev,
278 struct amdgpu_ib *ib,
279 uint64_t pe, uint64_t addr,
280 unsigned count, uint32_t incr,
281 uint32_t flags, uint32_t gtt_flags)
283 trace_amdgpu_vm_set_page(pe, addr, count, incr, flags);
285 if ((flags & AMDGPU_PTE_SYSTEM) && (flags == gtt_flags)) {
286 uint64_t src = adev->gart.table_addr + (addr >> 12) * 8;
287 amdgpu_vm_copy_pte(adev, ib, pe, src, count);
289 } else if ((flags & AMDGPU_PTE_SYSTEM) || (count < 3)) {
290 amdgpu_vm_write_pte(adev, ib, pe, addr,
294 amdgpu_vm_set_pte_pde(adev, ib, pe, addr,
300 * amdgpu_vm_clear_bo - initially clear the page dir/table
302 * @adev: amdgpu_device pointer
305 static int amdgpu_vm_clear_bo(struct amdgpu_device *adev,
306 struct amdgpu_bo *bo)
308 struct amdgpu_ring *ring = adev->vm_manager.vm_pte_funcs_ring;
314 r = amdgpu_bo_reserve(bo, false);
318 r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
320 goto error_unreserve;
322 addr = amdgpu_bo_gpu_offset(bo);
323 entries = amdgpu_bo_size(bo) / 8;
325 r = amdgpu_ib_get(ring, NULL, entries * 2 + 64, &ib);
327 goto error_unreserve;
331 amdgpu_vm_update_pages(adev, &ib, addr, 0, entries, 0, 0, 0);
332 amdgpu_vm_pad_ib(adev, &ib);
333 WARN_ON(ib.length_dw > 64);
335 r = amdgpu_ib_schedule(adev, 1, &ib, AMDGPU_FENCE_OWNER_VM);
339 amdgpu_bo_fence(bo, ib.fence, false);
342 amdgpu_ib_free(adev, &ib);
345 amdgpu_bo_unreserve(bo);
350 * amdgpu_vm_map_gart - get the physical address of a gart page
352 * @adev: amdgpu_device pointer
353 * @addr: the unmapped addr
355 * Look up the physical address of the page that the pte resolves
357 * Returns the physical address of the page.
359 uint64_t amdgpu_vm_map_gart(struct amdgpu_device *adev, uint64_t addr)
363 /* page table offset */
364 result = adev->gart.pages_addr[addr >> PAGE_SHIFT];
366 /* in case cpu page size != gpu page size*/
367 result |= addr & (~PAGE_MASK);
373 * amdgpu_vm_update_pdes - make sure that page directory is valid
375 * @adev: amdgpu_device pointer
377 * @start: start of GPU address range
378 * @end: end of GPU address range
380 * Allocates new page tables if necessary
381 * and updates the page directory (cayman+).
382 * Returns 0 for success, error for failure.
384 * Global and local mutex must be locked!
386 int amdgpu_vm_update_page_directory(struct amdgpu_device *adev,
387 struct amdgpu_vm *vm)
389 struct amdgpu_ring *ring = adev->vm_manager.vm_pte_funcs_ring;
390 struct amdgpu_bo *pd = vm->page_directory;
391 uint64_t pd_addr = amdgpu_bo_gpu_offset(pd);
392 uint32_t incr = AMDGPU_VM_PTE_COUNT * 8;
393 uint64_t last_pde = ~0, last_pt = ~0;
394 unsigned count = 0, pt_idx, ndw;
401 /* assume the worst case */
402 ndw += vm->max_pde_used * 6;
404 /* update too big for an IB */
408 r = amdgpu_ib_get(ring, NULL, ndw * 4, &ib);
413 /* walk over the address space and update the page directory */
414 for (pt_idx = 0; pt_idx <= vm->max_pde_used; ++pt_idx) {
415 struct amdgpu_bo *bo = vm->page_tables[pt_idx].bo;
421 pt = amdgpu_bo_gpu_offset(bo);
422 if (vm->page_tables[pt_idx].addr == pt)
424 vm->page_tables[pt_idx].addr = pt;
426 pde = pd_addr + pt_idx * 8;
427 if (((last_pde + 8 * count) != pde) ||
428 ((last_pt + incr * count) != pt)) {
431 amdgpu_vm_update_pages(adev, &ib, last_pde,
432 last_pt, count, incr,
433 AMDGPU_PTE_VALID, 0);
445 amdgpu_vm_update_pages(adev, &ib, last_pde, last_pt, count,
446 incr, AMDGPU_PTE_VALID, 0);
448 if (ib.length_dw != 0) {
449 amdgpu_vm_pad_ib(adev, &ib);
450 amdgpu_sync_resv(adev, &ib.sync, pd->tbo.resv, AMDGPU_FENCE_OWNER_VM);
451 WARN_ON(ib.length_dw > ndw);
452 r = amdgpu_ib_schedule(adev, 1, &ib, AMDGPU_FENCE_OWNER_VM);
454 amdgpu_ib_free(adev, &ib);
457 amdgpu_bo_fence(pd, ib.fence, false);
459 amdgpu_ib_free(adev, &ib);
465 * amdgpu_vm_frag_ptes - add fragment information to PTEs
467 * @adev: amdgpu_device pointer
468 * @ib: IB for the update
469 * @pe_start: first PTE to handle
470 * @pe_end: last PTE to handle
471 * @addr: addr those PTEs should point to
472 * @flags: hw mapping flags
473 * @gtt_flags: GTT hw mapping flags
475 * Global and local mutex must be locked!
477 static void amdgpu_vm_frag_ptes(struct amdgpu_device *adev,
478 struct amdgpu_ib *ib,
479 uint64_t pe_start, uint64_t pe_end,
480 uint64_t addr, uint32_t flags,
484 * The MC L1 TLB supports variable sized pages, based on a fragment
485 * field in the PTE. When this field is set to a non-zero value, page
486 * granularity is increased from 4KB to (1 << (12 + frag)). The PTE
487 * flags are considered valid for all PTEs within the fragment range
488 * and corresponding mappings are assumed to be physically contiguous.
490 * The L1 TLB can store a single PTE for the whole fragment,
491 * significantly increasing the space available for translation
492 * caching. This leads to large improvements in throughput when the
493 * TLB is under pressure.
495 * The L2 TLB distributes small and large fragments into two
496 * asymmetric partitions. The large fragment cache is significantly
497 * larger. Thus, we try to use large fragments wherever possible.
498 * Userspace can support this by aligning virtual base address and
499 * allocation size to the fragment size.
502 /* SI and newer are optimized for 64KB */
503 uint64_t frag_flags = AMDGPU_PTE_FRAG_64KB;
504 uint64_t frag_align = 0x80;
506 uint64_t frag_start = ALIGN(pe_start, frag_align);
507 uint64_t frag_end = pe_end & ~(frag_align - 1);
511 /* system pages are non continuously */
512 if ((flags & AMDGPU_PTE_SYSTEM) || !(flags & AMDGPU_PTE_VALID) ||
513 (frag_start >= frag_end)) {
515 count = (pe_end - pe_start) / 8;
516 amdgpu_vm_update_pages(adev, ib, pe_start, addr, count,
517 AMDGPU_GPU_PAGE_SIZE, flags, gtt_flags);
521 /* handle the 4K area at the beginning */
522 if (pe_start != frag_start) {
523 count = (frag_start - pe_start) / 8;
524 amdgpu_vm_update_pages(adev, ib, pe_start, addr, count,
525 AMDGPU_GPU_PAGE_SIZE, flags, gtt_flags);
526 addr += AMDGPU_GPU_PAGE_SIZE * count;
529 /* handle the area in the middle */
530 count = (frag_end - frag_start) / 8;
531 amdgpu_vm_update_pages(adev, ib, frag_start, addr, count,
532 AMDGPU_GPU_PAGE_SIZE, flags | frag_flags,
535 /* handle the 4K area at the end */
536 if (frag_end != pe_end) {
537 addr += AMDGPU_GPU_PAGE_SIZE * count;
538 count = (pe_end - frag_end) / 8;
539 amdgpu_vm_update_pages(adev, ib, frag_end, addr, count,
540 AMDGPU_GPU_PAGE_SIZE, flags, gtt_flags);
545 * amdgpu_vm_update_ptes - make sure that page tables are valid
547 * @adev: amdgpu_device pointer
549 * @start: start of GPU address range
550 * @end: end of GPU address range
551 * @dst: destination address to map to
552 * @flags: mapping flags
554 * Update the page tables in the range @start - @end (cayman+).
556 * Global and local mutex must be locked!
558 static int amdgpu_vm_update_ptes(struct amdgpu_device *adev,
559 struct amdgpu_vm *vm,
560 struct amdgpu_ib *ib,
561 uint64_t start, uint64_t end,
562 uint64_t dst, uint32_t flags,
565 uint64_t mask = AMDGPU_VM_PTE_COUNT - 1;
566 uint64_t last_pte = ~0, last_dst = ~0;
570 /* walk over the address space and update the page tables */
571 for (addr = start; addr < end; ) {
572 uint64_t pt_idx = addr >> amdgpu_vm_block_size;
573 struct amdgpu_bo *pt = vm->page_tables[pt_idx].bo;
578 amdgpu_sync_resv(adev, &ib->sync, pt->tbo.resv,
579 AMDGPU_FENCE_OWNER_VM);
580 r = reservation_object_reserve_shared(pt->tbo.resv);
584 if ((addr & ~mask) == (end & ~mask))
587 nptes = AMDGPU_VM_PTE_COUNT - (addr & mask);
589 pte = amdgpu_bo_gpu_offset(pt);
590 pte += (addr & mask) * 8;
592 if ((last_pte + 8 * count) != pte) {
595 amdgpu_vm_frag_ptes(adev, ib, last_pte,
596 last_pte + 8 * count,
609 dst += nptes * AMDGPU_GPU_PAGE_SIZE;
613 amdgpu_vm_frag_ptes(adev, ib, last_pte,
614 last_pte + 8 * count,
615 last_dst, flags, gtt_flags);
622 * amdgpu_vm_fence_pts - fence page tables after an update
625 * @start: start of GPU address range
626 * @end: end of GPU address range
627 * @fence: fence to use
629 * Fence the page tables in the range @start - @end (cayman+).
631 * Global and local mutex must be locked!
633 static void amdgpu_vm_fence_pts(struct amdgpu_vm *vm,
634 uint64_t start, uint64_t end,
635 struct amdgpu_fence *fence)
639 start >>= amdgpu_vm_block_size;
640 end >>= amdgpu_vm_block_size;
642 for (i = start; i <= end; ++i)
643 amdgpu_bo_fence(vm->page_tables[i].bo, fence, true);
647 * amdgpu_vm_bo_update_mapping - update a mapping in the vm page table
649 * @adev: amdgpu_device pointer
651 * @mapping: mapped range and flags to use for the update
652 * @addr: addr to set the area to
653 * @gtt_flags: flags as they are used for GTT
654 * @fence: optional resulting fence
656 * Fill in the page table entries for @mapping.
657 * Returns 0 for success, -EINVAL for failure.
659 * Object have to be reserved and mutex must be locked!
661 static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
662 struct amdgpu_vm *vm,
663 struct amdgpu_bo_va_mapping *mapping,
664 uint64_t addr, uint32_t gtt_flags,
665 struct amdgpu_fence **fence)
667 struct amdgpu_ring *ring = adev->vm_manager.vm_pte_funcs_ring;
668 unsigned nptes, ncmds, ndw;
669 uint32_t flags = gtt_flags;
673 /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here
674 * but in case of something, we filter the flags in first place
676 if (!(mapping->flags & AMDGPU_PTE_READABLE))
677 flags &= ~AMDGPU_PTE_READABLE;
678 if (!(mapping->flags & AMDGPU_PTE_WRITEABLE))
679 flags &= ~AMDGPU_PTE_WRITEABLE;
681 trace_amdgpu_vm_bo_update(mapping);
683 nptes = mapping->it.last - mapping->it.start + 1;
686 * reserve space for one command every (1 << BLOCK_SIZE)
687 * entries or 2k dwords (whatever is smaller)
689 ncmds = (nptes >> min(amdgpu_vm_block_size, 11)) + 1;
694 if ((flags & AMDGPU_PTE_SYSTEM) && (flags == gtt_flags)) {
695 /* only copy commands needed */
698 } else if (flags & AMDGPU_PTE_SYSTEM) {
699 /* header for write data commands */
702 /* body of write data command */
706 /* set page commands needed */
709 /* two extra commands for begin/end of fragment */
713 /* update too big for an IB */
717 r = amdgpu_ib_get(ring, NULL, ndw * 4, &ib);
722 if (!(flags & AMDGPU_PTE_VALID)) {
725 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
726 struct amdgpu_fence *f = vm->ids[i].last_id_use;
727 amdgpu_sync_fence(&ib.sync, f);
731 r = amdgpu_vm_update_ptes(adev, vm, &ib, mapping->it.start,
732 mapping->it.last + 1, addr + mapping->offset,
736 amdgpu_ib_free(adev, &ib);
740 amdgpu_vm_pad_ib(adev, &ib);
741 WARN_ON(ib.length_dw > ndw);
743 r = amdgpu_ib_schedule(adev, 1, &ib, AMDGPU_FENCE_OWNER_VM);
745 amdgpu_ib_free(adev, &ib);
748 amdgpu_vm_fence_pts(vm, mapping->it.start,
749 mapping->it.last + 1, ib.fence);
751 amdgpu_fence_unref(fence);
752 *fence = amdgpu_fence_ref(ib.fence);
754 amdgpu_ib_free(adev, &ib);
760 * amdgpu_vm_bo_update - update all BO mappings in the vm page table
762 * @adev: amdgpu_device pointer
763 * @bo_va: requested BO and VM object
766 * Fill in the page table entries for @bo_va.
767 * Returns 0 for success, -EINVAL for failure.
769 * Object have to be reserved and mutex must be locked!
771 int amdgpu_vm_bo_update(struct amdgpu_device *adev,
772 struct amdgpu_bo_va *bo_va,
773 struct ttm_mem_reg *mem)
775 struct amdgpu_vm *vm = bo_va->vm;
776 struct amdgpu_bo_va_mapping *mapping;
782 addr = mem->start << PAGE_SHIFT;
783 if (mem->mem_type != TTM_PL_TT)
784 addr += adev->vm_manager.vram_base_offset;
789 if (addr == bo_va->addr)
792 flags = amdgpu_ttm_tt_pte_flags(adev, bo_va->bo->tbo.ttm, mem);
794 list_for_each_entry(mapping, &bo_va->mappings, list) {
795 r = amdgpu_vm_bo_update_mapping(adev, vm, mapping, addr,
796 flags, &bo_va->last_pt_update);
802 spin_lock(&vm->status_lock);
803 list_del_init(&bo_va->vm_status);
804 spin_unlock(&vm->status_lock);
810 * amdgpu_vm_clear_freed - clear freed BOs in the PT
812 * @adev: amdgpu_device pointer
815 * Make sure all freed BOs are cleared in the PT.
816 * Returns 0 for success.
818 * PTs have to be reserved and mutex must be locked!
820 int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
821 struct amdgpu_vm *vm)
823 struct amdgpu_bo_va_mapping *mapping;
826 while (!list_empty(&vm->freed)) {
827 mapping = list_first_entry(&vm->freed,
828 struct amdgpu_bo_va_mapping, list);
829 list_del(&mapping->list);
831 r = amdgpu_vm_bo_update_mapping(adev, vm, mapping, 0, 0, NULL);
842 * amdgpu_vm_clear_invalids - clear invalidated BOs in the PT
844 * @adev: amdgpu_device pointer
847 * Make sure all invalidated BOs are cleared in the PT.
848 * Returns 0 for success.
850 * PTs have to be reserved and mutex must be locked!
852 int amdgpu_vm_clear_invalids(struct amdgpu_device *adev,
853 struct amdgpu_vm *vm)
855 struct amdgpu_bo_va *bo_va;
858 spin_lock(&vm->status_lock);
859 while (!list_empty(&vm->invalidated)) {
860 bo_va = list_first_entry(&vm->invalidated,
861 struct amdgpu_bo_va, vm_status);
862 spin_unlock(&vm->status_lock);
864 r = amdgpu_vm_bo_update(adev, bo_va, NULL);
868 spin_lock(&vm->status_lock);
870 spin_unlock(&vm->status_lock);
876 * amdgpu_vm_bo_add - add a bo to a specific vm
878 * @adev: amdgpu_device pointer
880 * @bo: amdgpu buffer object
882 * Add @bo into the requested vm (cayman+).
883 * Add @bo to the list of bos associated with the vm
884 * Returns newly added bo_va or NULL for failure
886 * Object has to be reserved!
888 struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
889 struct amdgpu_vm *vm,
890 struct amdgpu_bo *bo)
892 struct amdgpu_bo_va *bo_va;
894 bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL);
901 bo_va->ref_count = 1;
902 INIT_LIST_HEAD(&bo_va->bo_list);
903 INIT_LIST_HEAD(&bo_va->mappings);
904 INIT_LIST_HEAD(&bo_va->vm_status);
906 mutex_lock(&vm->mutex);
907 list_add_tail(&bo_va->bo_list, &bo->va);
908 mutex_unlock(&vm->mutex);
914 * amdgpu_vm_bo_map - map bo inside a vm
916 * @adev: amdgpu_device pointer
917 * @bo_va: bo_va to store the address
918 * @saddr: where to map the BO
919 * @offset: requested offset in the BO
920 * @flags: attributes of pages (read/write/valid/etc.)
922 * Add a mapping of the BO at the specefied addr into the VM.
923 * Returns 0 for success, error for failure.
925 * Object has to be reserved and gets unreserved by this function!
927 int amdgpu_vm_bo_map(struct amdgpu_device *adev,
928 struct amdgpu_bo_va *bo_va,
929 uint64_t saddr, uint64_t offset,
930 uint64_t size, uint32_t flags)
932 struct amdgpu_bo_va_mapping *mapping;
933 struct amdgpu_vm *vm = bo_va->vm;
934 struct interval_tree_node *it;
935 unsigned last_pfn, pt_idx;
939 /* validate the parameters */
940 if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
941 size == 0 || size & AMDGPU_GPU_PAGE_MASK) {
942 amdgpu_bo_unreserve(bo_va->bo);
946 /* make sure object fit at this offset */
947 eaddr = saddr + size;
948 if ((saddr >= eaddr) || (offset + size > amdgpu_bo_size(bo_va->bo))) {
949 amdgpu_bo_unreserve(bo_va->bo);
953 last_pfn = eaddr / AMDGPU_GPU_PAGE_SIZE;
954 if (last_pfn > adev->vm_manager.max_pfn) {
955 dev_err(adev->dev, "va above limit (0x%08X > 0x%08X)\n",
956 last_pfn, adev->vm_manager.max_pfn);
957 amdgpu_bo_unreserve(bo_va->bo);
961 mutex_lock(&vm->mutex);
963 saddr /= AMDGPU_GPU_PAGE_SIZE;
964 eaddr /= AMDGPU_GPU_PAGE_SIZE;
966 it = interval_tree_iter_first(&vm->va, saddr, eaddr - 1);
968 struct amdgpu_bo_va_mapping *tmp;
969 tmp = container_of(it, struct amdgpu_bo_va_mapping, it);
970 /* bo and tmp overlap, invalid addr */
971 dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with "
972 "0x%010lx-0x%010lx\n", bo_va->bo, saddr, eaddr,
973 tmp->it.start, tmp->it.last + 1);
974 amdgpu_bo_unreserve(bo_va->bo);
979 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
981 amdgpu_bo_unreserve(bo_va->bo);
986 INIT_LIST_HEAD(&mapping->list);
987 mapping->it.start = saddr;
988 mapping->it.last = eaddr - 1;
989 mapping->offset = offset;
990 mapping->flags = flags;
992 list_add(&mapping->list, &bo_va->mappings);
993 interval_tree_insert(&mapping->it, &vm->va);
995 /* Make sure the page tables are allocated */
996 saddr >>= amdgpu_vm_block_size;
997 eaddr >>= amdgpu_vm_block_size;
999 BUG_ON(eaddr >= amdgpu_vm_num_pdes(adev));
1001 if (eaddr > vm->max_pde_used)
1002 vm->max_pde_used = eaddr;
1004 amdgpu_bo_unreserve(bo_va->bo);
1006 /* walk over the address space and allocate the page tables */
1007 for (pt_idx = saddr; pt_idx <= eaddr; ++pt_idx) {
1008 struct amdgpu_bo *pt;
1010 if (vm->page_tables[pt_idx].bo)
1013 /* drop mutex to allocate and clear page table */
1014 mutex_unlock(&vm->mutex);
1016 r = amdgpu_bo_create(adev, AMDGPU_VM_PTE_COUNT * 8,
1017 AMDGPU_GPU_PAGE_SIZE, true,
1018 AMDGPU_GEM_DOMAIN_VRAM, 0, NULL, &pt);
1022 r = amdgpu_vm_clear_bo(adev, pt);
1024 amdgpu_bo_unref(&pt);
1028 /* aquire mutex again */
1029 mutex_lock(&vm->mutex);
1030 if (vm->page_tables[pt_idx].bo) {
1031 /* someone else allocated the pt in the meantime */
1032 mutex_unlock(&vm->mutex);
1033 amdgpu_bo_unref(&pt);
1034 mutex_lock(&vm->mutex);
1038 vm->page_tables[pt_idx].addr = 0;
1039 vm->page_tables[pt_idx].bo = pt;
1042 mutex_unlock(&vm->mutex);
1046 mutex_lock(&vm->mutex);
1047 list_del(&mapping->list);
1048 interval_tree_remove(&mapping->it, &vm->va);
1052 mutex_unlock(&vm->mutex);
1057 * amdgpu_vm_bo_unmap - remove bo mapping from vm
1059 * @adev: amdgpu_device pointer
1060 * @bo_va: bo_va to remove the address from
1061 * @saddr: where to the BO is mapped
1063 * Remove a mapping of the BO at the specefied addr from the VM.
1064 * Returns 0 for success, error for failure.
1066 * Object has to be reserved and gets unreserved by this function!
1068 int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
1069 struct amdgpu_bo_va *bo_va,
1072 struct amdgpu_bo_va_mapping *mapping;
1073 struct amdgpu_vm *vm = bo_va->vm;
1075 list_for_each_entry(mapping, &bo_va->mappings, list) {
1076 if (mapping->it.start == saddr)
1080 if (&mapping->list == &bo_va->mappings) {
1081 amdgpu_bo_unreserve(bo_va->bo);
1085 mutex_lock(&vm->mutex);
1086 list_del(&mapping->list);
1087 interval_tree_remove(&mapping->it, &vm->va);
1090 /* clear the old address */
1091 list_add(&mapping->list, &vm->freed);
1095 mutex_unlock(&vm->mutex);
1096 amdgpu_bo_unreserve(bo_va->bo);
1102 * amdgpu_vm_bo_rmv - remove a bo to a specific vm
1104 * @adev: amdgpu_device pointer
1105 * @bo_va: requested bo_va
1107 * Remove @bo_va->bo from the requested vm (cayman+).
1109 * Object have to be reserved!
1111 void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
1112 struct amdgpu_bo_va *bo_va)
1114 struct amdgpu_bo_va_mapping *mapping, *next;
1115 struct amdgpu_vm *vm = bo_va->vm;
1117 list_del(&bo_va->bo_list);
1119 mutex_lock(&vm->mutex);
1121 spin_lock(&vm->status_lock);
1122 list_del(&bo_va->vm_status);
1123 spin_unlock(&vm->status_lock);
1125 list_for_each_entry_safe(mapping, next, &bo_va->mappings, list) {
1126 list_del(&mapping->list);
1127 interval_tree_remove(&mapping->it, &vm->va);
1129 list_add(&mapping->list, &vm->freed);
1133 amdgpu_fence_unref(&bo_va->last_pt_update);
1136 mutex_unlock(&vm->mutex);
1140 * amdgpu_vm_bo_invalidate - mark the bo as invalid
1142 * @adev: amdgpu_device pointer
1144 * @bo: amdgpu buffer object
1146 * Mark @bo as invalid (cayman+).
1148 void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
1149 struct amdgpu_bo *bo)
1151 struct amdgpu_bo_va *bo_va;
1153 list_for_each_entry(bo_va, &bo->va, bo_list) {
1155 spin_lock(&bo_va->vm->status_lock);
1156 list_del(&bo_va->vm_status);
1157 list_add(&bo_va->vm_status, &bo_va->vm->invalidated);
1158 spin_unlock(&bo_va->vm->status_lock);
1164 * amdgpu_vm_init - initialize a vm instance
1166 * @adev: amdgpu_device pointer
1169 * Init @vm fields (cayman+).
1171 int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1173 const unsigned align = min(AMDGPU_VM_PTB_ALIGN_SIZE,
1174 AMDGPU_VM_PTE_COUNT * 8);
1175 unsigned pd_size, pd_entries, pts_size;
1178 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
1180 vm->ids[i].flushed_updates = NULL;
1181 vm->ids[i].last_id_use = NULL;
1183 mutex_init(&vm->mutex);
1185 spin_lock_init(&vm->status_lock);
1186 INIT_LIST_HEAD(&vm->invalidated);
1187 INIT_LIST_HEAD(&vm->freed);
1189 pd_size = amdgpu_vm_directory_size(adev);
1190 pd_entries = amdgpu_vm_num_pdes(adev);
1192 /* allocate page table array */
1193 pts_size = pd_entries * sizeof(struct amdgpu_vm_pt);
1194 vm->page_tables = kzalloc(pts_size, GFP_KERNEL);
1195 if (vm->page_tables == NULL) {
1196 DRM_ERROR("Cannot allocate memory for page table array\n");
1200 r = amdgpu_bo_create(adev, pd_size, align, true,
1201 AMDGPU_GEM_DOMAIN_VRAM, 0,
1202 NULL, &vm->page_directory);
1206 r = amdgpu_vm_clear_bo(adev, vm->page_directory);
1208 amdgpu_bo_unref(&vm->page_directory);
1209 vm->page_directory = NULL;
1217 * amdgpu_vm_fini - tear down a vm instance
1219 * @adev: amdgpu_device pointer
1222 * Tear down @vm (cayman+).
1223 * Unbind the VM and remove all bos from the vm bo list
1225 void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1227 struct amdgpu_bo_va_mapping *mapping, *tmp;
1230 if (!RB_EMPTY_ROOT(&vm->va)) {
1231 dev_err(adev->dev, "still active bo inside vm\n");
1233 rbtree_postorder_for_each_entry_safe(mapping, tmp, &vm->va, it.rb) {
1234 list_del(&mapping->list);
1235 interval_tree_remove(&mapping->it, &vm->va);
1238 list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
1239 list_del(&mapping->list);
1243 for (i = 0; i < amdgpu_vm_num_pdes(adev); i++)
1244 amdgpu_bo_unref(&vm->page_tables[i].bo);
1245 kfree(vm->page_tables);
1247 amdgpu_bo_unref(&vm->page_directory);
1249 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
1250 amdgpu_fence_unref(&vm->ids[i].flushed_updates);
1251 amdgpu_fence_unref(&vm->ids[i].last_id_use);
1254 mutex_destroy(&vm->mutex);