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 mutex_lock(&vm->mutex);
94 list = drm_malloc_ab(vm->max_pde_used + 2,
95 sizeof(struct amdgpu_bo_list_entry));
97 mutex_unlock(&vm->mutex);
101 /* add the vm page table to the list */
102 list[0].robj = vm->page_directory;
103 list[0].prefered_domains = AMDGPU_GEM_DOMAIN_VRAM;
104 list[0].allowed_domains = AMDGPU_GEM_DOMAIN_VRAM;
105 list[0].priority = 0;
106 list[0].tv.bo = &vm->page_directory->tbo;
107 list[0].tv.shared = true;
108 list_add(&list[0].tv.head, head);
110 for (i = 0, idx = 1; i <= vm->max_pde_used; i++) {
111 if (!vm->page_tables[i].bo)
114 list[idx].robj = vm->page_tables[i].bo;
115 list[idx].prefered_domains = AMDGPU_GEM_DOMAIN_VRAM;
116 list[idx].allowed_domains = AMDGPU_GEM_DOMAIN_VRAM;
117 list[idx].priority = 0;
118 list[idx].tv.bo = &list[idx].robj->tbo;
119 list[idx].tv.shared = true;
120 list_add(&list[idx++].tv.head, head);
122 mutex_unlock(&vm->mutex);
128 * amdgpu_vm_grab_id - allocate the next free VMID
130 * @vm: vm to allocate id for
131 * @ring: ring we want to submit job to
132 * @sync: sync object where we add dependencies
134 * Allocate an id for the vm, adding fences to the sync obj as necessary.
136 * Global mutex must be locked!
138 int amdgpu_vm_grab_id(struct amdgpu_vm *vm, struct amdgpu_ring *ring,
139 struct amdgpu_sync *sync)
141 struct amdgpu_fence *best[AMDGPU_MAX_RINGS] = {};
142 struct amdgpu_vm_id *vm_id = &vm->ids[ring->idx];
143 struct amdgpu_device *adev = ring->adev;
145 unsigned choices[2] = {};
148 /* check if the id is still valid */
149 if (vm_id->id && vm_id->last_id_use &&
150 vm_id->last_id_use == adev->vm_manager.active[vm_id->id])
153 /* we definately need to flush */
154 vm_id->pd_gpu_addr = ~0ll;
156 /* skip over VMID 0, since it is the system VM */
157 for (i = 1; i < adev->vm_manager.nvm; ++i) {
158 struct amdgpu_fence *fence = adev->vm_manager.active[i];
161 /* found a free one */
163 trace_amdgpu_vm_grab_id(i, ring->idx);
167 if (amdgpu_fence_is_earlier(fence, best[fence->ring->idx])) {
168 best[fence->ring->idx] = fence;
169 choices[fence->ring == ring ? 0 : 1] = i;
173 for (i = 0; i < 2; ++i) {
175 struct amdgpu_fence *fence;
177 fence = adev->vm_manager.active[choices[i]];
178 vm_id->id = choices[i];
180 trace_amdgpu_vm_grab_id(choices[i], ring->idx);
181 return amdgpu_sync_fence(ring->adev, sync, &fence->base);
185 /* should never happen */
191 * amdgpu_vm_flush - hardware flush the vm
193 * @ring: ring to use for flush
194 * @vm: vm we want to flush
195 * @updates: last vm update that we waited for
197 * Flush the vm (cayman+).
199 * Global and local mutex must be locked!
201 void amdgpu_vm_flush(struct amdgpu_ring *ring,
202 struct amdgpu_vm *vm,
203 struct amdgpu_fence *updates)
205 uint64_t pd_addr = amdgpu_bo_gpu_offset(vm->page_directory);
206 struct amdgpu_vm_id *vm_id = &vm->ids[ring->idx];
207 struct amdgpu_fence *flushed_updates = vm_id->flushed_updates;
209 if (pd_addr != vm_id->pd_gpu_addr || !flushed_updates ||
210 (updates && amdgpu_fence_is_earlier(flushed_updates, updates))) {
212 trace_amdgpu_vm_flush(pd_addr, ring->idx, vm_id->id);
213 vm_id->flushed_updates = amdgpu_fence_ref(
214 amdgpu_fence_later(flushed_updates, updates));
215 amdgpu_fence_unref(&flushed_updates);
216 vm_id->pd_gpu_addr = pd_addr;
217 amdgpu_ring_emit_vm_flush(ring, vm_id->id, vm_id->pd_gpu_addr);
222 * amdgpu_vm_fence - remember fence for vm
224 * @adev: amdgpu_device pointer
225 * @vm: vm we want to fence
226 * @fence: fence to remember
228 * Fence the vm (cayman+).
229 * Set the fence used to protect page table and id.
231 * Global and local mutex must be locked!
233 void amdgpu_vm_fence(struct amdgpu_device *adev,
234 struct amdgpu_vm *vm,
235 struct amdgpu_fence *fence)
237 unsigned ridx = fence->ring->idx;
238 unsigned vm_id = vm->ids[ridx].id;
240 amdgpu_fence_unref(&adev->vm_manager.active[vm_id]);
241 adev->vm_manager.active[vm_id] = amdgpu_fence_ref(fence);
243 amdgpu_fence_unref(&vm->ids[ridx].last_id_use);
244 vm->ids[ridx].last_id_use = amdgpu_fence_ref(fence);
248 * amdgpu_vm_bo_find - find the bo_va for a specific vm & bo
251 * @bo: requested buffer object
253 * Find @bo inside the requested vm (cayman+).
254 * Search inside the @bos vm list for the requested vm
255 * Returns the found bo_va or NULL if none is found
257 * Object has to be reserved!
259 struct amdgpu_bo_va *amdgpu_vm_bo_find(struct amdgpu_vm *vm,
260 struct amdgpu_bo *bo)
262 struct amdgpu_bo_va *bo_va;
264 list_for_each_entry(bo_va, &bo->va, bo_list) {
265 if (bo_va->vm == vm) {
273 * amdgpu_vm_update_pages - helper to call the right asic function
275 * @adev: amdgpu_device pointer
276 * @ib: indirect buffer to fill with commands
277 * @pe: addr of the page entry
278 * @addr: dst addr to write into pe
279 * @count: number of page entries to update
280 * @incr: increase next addr by incr bytes
281 * @flags: hw access flags
282 * @gtt_flags: GTT hw access flags
284 * Traces the parameters and calls the right asic functions
285 * to setup the page table using the DMA.
287 static void amdgpu_vm_update_pages(struct amdgpu_device *adev,
288 struct amdgpu_ib *ib,
289 uint64_t pe, uint64_t addr,
290 unsigned count, uint32_t incr,
291 uint32_t flags, uint32_t gtt_flags)
293 trace_amdgpu_vm_set_page(pe, addr, count, incr, flags);
295 if ((flags & AMDGPU_PTE_SYSTEM) && (flags == gtt_flags)) {
296 uint64_t src = adev->gart.table_addr + (addr >> 12) * 8;
297 amdgpu_vm_copy_pte(adev, ib, pe, src, count);
299 } else if ((flags & AMDGPU_PTE_SYSTEM) || (count < 3)) {
300 amdgpu_vm_write_pte(adev, ib, pe, addr,
304 amdgpu_vm_set_pte_pde(adev, ib, pe, addr,
309 static int amdgpu_vm_free_job(
310 struct amdgpu_cs_parser *sched_job)
313 for (i = 0; i < sched_job->num_ibs; i++)
314 amdgpu_ib_free(sched_job->adev, &sched_job->ibs[i]);
315 kfree(sched_job->ibs);
320 * amdgpu_vm_clear_bo - initially clear the page dir/table
322 * @adev: amdgpu_device pointer
325 static int amdgpu_vm_clear_bo(struct amdgpu_device *adev,
326 struct amdgpu_bo *bo)
328 struct amdgpu_ring *ring = adev->vm_manager.vm_pte_funcs_ring;
329 struct fence *fence = NULL;
330 struct amdgpu_ib *ib;
335 r = amdgpu_bo_reserve(bo, false);
339 r = reservation_object_reserve_shared(bo->tbo.resv);
343 r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
345 goto error_unreserve;
347 addr = amdgpu_bo_gpu_offset(bo);
348 entries = amdgpu_bo_size(bo) / 8;
350 ib = kzalloc(sizeof(struct amdgpu_ib), GFP_KERNEL);
352 goto error_unreserve;
354 r = amdgpu_ib_get(ring, NULL, entries * 2 + 64, ib);
360 amdgpu_vm_update_pages(adev, ib, addr, 0, entries, 0, 0, 0);
361 amdgpu_vm_pad_ib(adev, ib);
362 WARN_ON(ib->length_dw > 64);
363 r = amdgpu_sched_ib_submit_kernel_helper(adev, ring, ib, 1,
365 AMDGPU_FENCE_OWNER_VM,
368 amdgpu_bo_fence(bo, fence, true);
370 if (amdgpu_enable_scheduler) {
371 amdgpu_bo_unreserve(bo);
375 amdgpu_ib_free(adev, ib);
379 amdgpu_bo_unreserve(bo);
384 * amdgpu_vm_map_gart - get the physical address of a gart page
386 * @adev: amdgpu_device pointer
387 * @addr: the unmapped addr
389 * Look up the physical address of the page that the pte resolves
391 * Returns the physical address of the page.
393 uint64_t amdgpu_vm_map_gart(struct amdgpu_device *adev, uint64_t addr)
397 /* page table offset */
398 result = adev->gart.pages_addr[addr >> PAGE_SHIFT];
400 /* in case cpu page size != gpu page size*/
401 result |= addr & (~PAGE_MASK);
407 * amdgpu_vm_update_pdes - make sure that page directory is valid
409 * @adev: amdgpu_device pointer
411 * @start: start of GPU address range
412 * @end: end of GPU address range
414 * Allocates new page tables if necessary
415 * and updates the page directory (cayman+).
416 * Returns 0 for success, error for failure.
418 * Global and local mutex must be locked!
420 int amdgpu_vm_update_page_directory(struct amdgpu_device *adev,
421 struct amdgpu_vm *vm)
423 struct amdgpu_ring *ring = adev->vm_manager.vm_pte_funcs_ring;
424 struct amdgpu_bo *pd = vm->page_directory;
425 uint64_t pd_addr = amdgpu_bo_gpu_offset(pd);
426 uint32_t incr = AMDGPU_VM_PTE_COUNT * 8;
427 uint64_t last_pde = ~0, last_pt = ~0;
428 unsigned count = 0, pt_idx, ndw;
429 struct amdgpu_ib *ib;
430 struct fence *fence = NULL;
437 /* assume the worst case */
438 ndw += vm->max_pde_used * 6;
440 /* update too big for an IB */
444 ib = kzalloc(sizeof(struct amdgpu_ib), GFP_KERNEL);
448 r = amdgpu_ib_get(ring, NULL, ndw * 4, ib);
453 /* walk over the address space and update the page directory */
454 for (pt_idx = 0; pt_idx <= vm->max_pde_used; ++pt_idx) {
455 struct amdgpu_bo *bo = vm->page_tables[pt_idx].bo;
461 pt = amdgpu_bo_gpu_offset(bo);
462 if (vm->page_tables[pt_idx].addr == pt)
464 vm->page_tables[pt_idx].addr = pt;
466 pde = pd_addr + pt_idx * 8;
467 if (((last_pde + 8 * count) != pde) ||
468 ((last_pt + incr * count) != pt)) {
471 amdgpu_vm_update_pages(adev, ib, last_pde,
472 last_pt, count, incr,
473 AMDGPU_PTE_VALID, 0);
485 amdgpu_vm_update_pages(adev, ib, last_pde, last_pt, count,
486 incr, AMDGPU_PTE_VALID, 0);
488 if (ib->length_dw != 0) {
489 amdgpu_vm_pad_ib(adev, ib);
490 amdgpu_sync_resv(adev, &ib->sync, pd->tbo.resv, AMDGPU_FENCE_OWNER_VM);
491 WARN_ON(ib->length_dw > ndw);
492 r = amdgpu_sched_ib_submit_kernel_helper(adev, ring, ib, 1,
494 AMDGPU_FENCE_OWNER_VM,
499 amdgpu_bo_fence(pd, fence, true);
500 fence_put(vm->page_directory_fence);
501 vm->page_directory_fence = fence_get(fence);
505 if (!amdgpu_enable_scheduler || ib->length_dw == 0) {
506 amdgpu_ib_free(adev, ib);
513 amdgpu_ib_free(adev, ib);
519 * amdgpu_vm_frag_ptes - add fragment information to PTEs
521 * @adev: amdgpu_device pointer
522 * @ib: IB for the update
523 * @pe_start: first PTE to handle
524 * @pe_end: last PTE to handle
525 * @addr: addr those PTEs should point to
526 * @flags: hw mapping flags
527 * @gtt_flags: GTT hw mapping flags
529 * Global and local mutex must be locked!
531 static void amdgpu_vm_frag_ptes(struct amdgpu_device *adev,
532 struct amdgpu_ib *ib,
533 uint64_t pe_start, uint64_t pe_end,
534 uint64_t addr, uint32_t flags,
538 * The MC L1 TLB supports variable sized pages, based on a fragment
539 * field in the PTE. When this field is set to a non-zero value, page
540 * granularity is increased from 4KB to (1 << (12 + frag)). The PTE
541 * flags are considered valid for all PTEs within the fragment range
542 * and corresponding mappings are assumed to be physically contiguous.
544 * The L1 TLB can store a single PTE for the whole fragment,
545 * significantly increasing the space available for translation
546 * caching. This leads to large improvements in throughput when the
547 * TLB is under pressure.
549 * The L2 TLB distributes small and large fragments into two
550 * asymmetric partitions. The large fragment cache is significantly
551 * larger. Thus, we try to use large fragments wherever possible.
552 * Userspace can support this by aligning virtual base address and
553 * allocation size to the fragment size.
556 /* SI and newer are optimized for 64KB */
557 uint64_t frag_flags = AMDGPU_PTE_FRAG_64KB;
558 uint64_t frag_align = 0x80;
560 uint64_t frag_start = ALIGN(pe_start, frag_align);
561 uint64_t frag_end = pe_end & ~(frag_align - 1);
565 /* system pages are non continuously */
566 if ((flags & AMDGPU_PTE_SYSTEM) || !(flags & AMDGPU_PTE_VALID) ||
567 (frag_start >= frag_end)) {
569 count = (pe_end - pe_start) / 8;
570 amdgpu_vm_update_pages(adev, ib, pe_start, addr, count,
571 AMDGPU_GPU_PAGE_SIZE, flags, gtt_flags);
575 /* handle the 4K area at the beginning */
576 if (pe_start != frag_start) {
577 count = (frag_start - pe_start) / 8;
578 amdgpu_vm_update_pages(adev, ib, pe_start, addr, count,
579 AMDGPU_GPU_PAGE_SIZE, flags, gtt_flags);
580 addr += AMDGPU_GPU_PAGE_SIZE * count;
583 /* handle the area in the middle */
584 count = (frag_end - frag_start) / 8;
585 amdgpu_vm_update_pages(adev, ib, frag_start, addr, count,
586 AMDGPU_GPU_PAGE_SIZE, flags | frag_flags,
589 /* handle the 4K area at the end */
590 if (frag_end != pe_end) {
591 addr += AMDGPU_GPU_PAGE_SIZE * count;
592 count = (pe_end - frag_end) / 8;
593 amdgpu_vm_update_pages(adev, ib, frag_end, addr, count,
594 AMDGPU_GPU_PAGE_SIZE, flags, gtt_flags);
599 * amdgpu_vm_update_ptes - make sure that page tables are valid
601 * @adev: amdgpu_device pointer
603 * @start: start of GPU address range
604 * @end: end of GPU address range
605 * @dst: destination address to map to
606 * @flags: mapping flags
608 * Update the page tables in the range @start - @end (cayman+).
610 * Global and local mutex must be locked!
612 static int amdgpu_vm_update_ptes(struct amdgpu_device *adev,
613 struct amdgpu_vm *vm,
614 struct amdgpu_ib *ib,
615 uint64_t start, uint64_t end,
616 uint64_t dst, uint32_t flags,
619 uint64_t mask = AMDGPU_VM_PTE_COUNT - 1;
620 uint64_t last_pte = ~0, last_dst = ~0;
624 /* walk over the address space and update the page tables */
625 for (addr = start; addr < end; ) {
626 uint64_t pt_idx = addr >> amdgpu_vm_block_size;
627 struct amdgpu_bo *pt = vm->page_tables[pt_idx].bo;
632 amdgpu_sync_resv(adev, &ib->sync, pt->tbo.resv,
633 AMDGPU_FENCE_OWNER_VM);
634 r = reservation_object_reserve_shared(pt->tbo.resv);
638 if ((addr & ~mask) == (end & ~mask))
641 nptes = AMDGPU_VM_PTE_COUNT - (addr & mask);
643 pte = amdgpu_bo_gpu_offset(pt);
644 pte += (addr & mask) * 8;
646 if ((last_pte + 8 * count) != pte) {
649 amdgpu_vm_frag_ptes(adev, ib, last_pte,
650 last_pte + 8 * count,
663 dst += nptes * AMDGPU_GPU_PAGE_SIZE;
667 amdgpu_vm_frag_ptes(adev, ib, last_pte,
668 last_pte + 8 * count,
669 last_dst, flags, gtt_flags);
676 * amdgpu_vm_fence_pts - fence page tables after an update
679 * @start: start of GPU address range
680 * @end: end of GPU address range
681 * @fence: fence to use
683 * Fence the page tables in the range @start - @end (cayman+).
685 * Global and local mutex must be locked!
687 static void amdgpu_vm_fence_pts(struct amdgpu_vm *vm,
688 uint64_t start, uint64_t end,
693 start >>= amdgpu_vm_block_size;
694 end >>= amdgpu_vm_block_size;
696 for (i = start; i <= end; ++i)
697 amdgpu_bo_fence(vm->page_tables[i].bo, fence, true);
701 * amdgpu_vm_bo_update_mapping - update a mapping in the vm page table
703 * @adev: amdgpu_device pointer
705 * @mapping: mapped range and flags to use for the update
706 * @addr: addr to set the area to
707 * @gtt_flags: flags as they are used for GTT
708 * @fence: optional resulting fence
710 * Fill in the page table entries for @mapping.
711 * Returns 0 for success, -EINVAL for failure.
713 * Object have to be reserved and mutex must be locked!
715 static int amdgpu_vm_bo_update_mapping(struct amdgpu_device *adev,
716 struct amdgpu_vm *vm,
717 struct amdgpu_bo_va_mapping *mapping,
718 uint64_t addr, uint32_t gtt_flags,
719 struct fence **fence)
721 struct amdgpu_ring *ring = adev->vm_manager.vm_pte_funcs_ring;
722 unsigned nptes, ncmds, ndw;
723 uint32_t flags = gtt_flags;
724 struct amdgpu_ib *ib;
725 struct fence *f = NULL;
728 /* normally,bo_va->flags only contians READABLE and WIRTEABLE bit go here
729 * but in case of something, we filter the flags in first place
731 if (!(mapping->flags & AMDGPU_PTE_READABLE))
732 flags &= ~AMDGPU_PTE_READABLE;
733 if (!(mapping->flags & AMDGPU_PTE_WRITEABLE))
734 flags &= ~AMDGPU_PTE_WRITEABLE;
736 trace_amdgpu_vm_bo_update(mapping);
738 nptes = mapping->it.last - mapping->it.start + 1;
741 * reserve space for one command every (1 << BLOCK_SIZE)
742 * entries or 2k dwords (whatever is smaller)
744 ncmds = (nptes >> min(amdgpu_vm_block_size, 11)) + 1;
749 if ((flags & AMDGPU_PTE_SYSTEM) && (flags == gtt_flags)) {
750 /* only copy commands needed */
753 } else if (flags & AMDGPU_PTE_SYSTEM) {
754 /* header for write data commands */
757 /* body of write data command */
761 /* set page commands needed */
764 /* two extra commands for begin/end of fragment */
768 /* update too big for an IB */
772 ib = kzalloc(sizeof(struct amdgpu_ib), GFP_KERNEL);
776 r = amdgpu_ib_get(ring, NULL, ndw * 4, ib);
784 if (!(flags & AMDGPU_PTE_VALID)) {
787 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
788 struct amdgpu_fence *f = vm->ids[i].last_id_use;
789 r = amdgpu_sync_fence(adev, &ib->sync, &f->base);
795 r = amdgpu_vm_update_ptes(adev, vm, ib, mapping->it.start,
796 mapping->it.last + 1, addr + mapping->offset,
800 amdgpu_ib_free(adev, ib);
805 amdgpu_vm_pad_ib(adev, ib);
806 WARN_ON(ib->length_dw > ndw);
807 r = amdgpu_sched_ib_submit_kernel_helper(adev, ring, ib, 1,
809 AMDGPU_FENCE_OWNER_VM,
814 amdgpu_vm_fence_pts(vm, mapping->it.start,
815 mapping->it.last + 1, f);
818 *fence = fence_get(f);
821 if (!amdgpu_enable_scheduler) {
822 amdgpu_ib_free(adev, ib);
828 amdgpu_ib_free(adev, ib);
834 * amdgpu_vm_bo_update - update all BO mappings in the vm page table
836 * @adev: amdgpu_device pointer
837 * @bo_va: requested BO and VM object
840 * Fill in the page table entries for @bo_va.
841 * Returns 0 for success, -EINVAL for failure.
843 * Object have to be reserved and mutex must be locked!
845 int amdgpu_vm_bo_update(struct amdgpu_device *adev,
846 struct amdgpu_bo_va *bo_va,
847 struct ttm_mem_reg *mem)
849 struct amdgpu_vm *vm = bo_va->vm;
850 struct amdgpu_bo_va_mapping *mapping;
856 addr = mem->start << PAGE_SHIFT;
857 if (mem->mem_type != TTM_PL_TT)
858 addr += adev->vm_manager.vram_base_offset;
863 flags = amdgpu_ttm_tt_pte_flags(adev, bo_va->bo->tbo.ttm, mem);
865 spin_lock(&vm->status_lock);
866 if (!list_empty(&bo_va->vm_status))
867 list_splice_init(&bo_va->valids, &bo_va->invalids);
868 spin_unlock(&vm->status_lock);
870 list_for_each_entry(mapping, &bo_va->invalids, list) {
871 r = amdgpu_vm_bo_update_mapping(adev, vm, mapping, addr,
872 flags, &bo_va->last_pt_update);
877 spin_lock(&vm->status_lock);
878 list_splice_init(&bo_va->invalids, &bo_va->valids);
879 list_del_init(&bo_va->vm_status);
881 list_add(&bo_va->vm_status, &vm->cleared);
882 spin_unlock(&vm->status_lock);
888 * amdgpu_vm_clear_freed - clear freed BOs in the PT
890 * @adev: amdgpu_device pointer
893 * Make sure all freed BOs are cleared in the PT.
894 * Returns 0 for success.
896 * PTs have to be reserved and mutex must be locked!
898 int amdgpu_vm_clear_freed(struct amdgpu_device *adev,
899 struct amdgpu_vm *vm)
901 struct amdgpu_bo_va_mapping *mapping;
904 while (!list_empty(&vm->freed)) {
905 mapping = list_first_entry(&vm->freed,
906 struct amdgpu_bo_va_mapping, list);
907 list_del(&mapping->list);
909 r = amdgpu_vm_bo_update_mapping(adev, vm, mapping, 0, 0, NULL);
920 * amdgpu_vm_clear_invalids - clear invalidated BOs in the PT
922 * @adev: amdgpu_device pointer
925 * Make sure all invalidated BOs are cleared in the PT.
926 * Returns 0 for success.
928 * PTs have to be reserved and mutex must be locked!
930 int amdgpu_vm_clear_invalids(struct amdgpu_device *adev,
931 struct amdgpu_vm *vm, struct amdgpu_sync *sync)
933 struct amdgpu_bo_va *bo_va = NULL;
936 spin_lock(&vm->status_lock);
937 while (!list_empty(&vm->invalidated)) {
938 bo_va = list_first_entry(&vm->invalidated,
939 struct amdgpu_bo_va, vm_status);
940 spin_unlock(&vm->status_lock);
942 r = amdgpu_vm_bo_update(adev, bo_va, NULL);
946 spin_lock(&vm->status_lock);
948 spin_unlock(&vm->status_lock);
951 r = amdgpu_sync_fence(adev, sync, bo_va->last_pt_update);
957 * amdgpu_vm_bo_add - add a bo to a specific vm
959 * @adev: amdgpu_device pointer
961 * @bo: amdgpu buffer object
963 * Add @bo into the requested vm (cayman+).
964 * Add @bo to the list of bos associated with the vm
965 * Returns newly added bo_va or NULL for failure
967 * Object has to be reserved!
969 struct amdgpu_bo_va *amdgpu_vm_bo_add(struct amdgpu_device *adev,
970 struct amdgpu_vm *vm,
971 struct amdgpu_bo *bo)
973 struct amdgpu_bo_va *bo_va;
975 bo_va = kzalloc(sizeof(struct amdgpu_bo_va), GFP_KERNEL);
981 bo_va->ref_count = 1;
982 INIT_LIST_HEAD(&bo_va->bo_list);
983 INIT_LIST_HEAD(&bo_va->valids);
984 INIT_LIST_HEAD(&bo_va->invalids);
985 INIT_LIST_HEAD(&bo_va->vm_status);
987 mutex_lock(&vm->mutex);
988 list_add_tail(&bo_va->bo_list, &bo->va);
989 mutex_unlock(&vm->mutex);
995 * amdgpu_vm_bo_map - map bo inside a vm
997 * @adev: amdgpu_device pointer
998 * @bo_va: bo_va to store the address
999 * @saddr: where to map the BO
1000 * @offset: requested offset in the BO
1001 * @flags: attributes of pages (read/write/valid/etc.)
1003 * Add a mapping of the BO at the specefied addr into the VM.
1004 * Returns 0 for success, error for failure.
1006 * Object has to be reserved and gets unreserved by this function!
1008 int amdgpu_vm_bo_map(struct amdgpu_device *adev,
1009 struct amdgpu_bo_va *bo_va,
1010 uint64_t saddr, uint64_t offset,
1011 uint64_t size, uint32_t flags)
1013 struct amdgpu_bo_va_mapping *mapping;
1014 struct amdgpu_vm *vm = bo_va->vm;
1015 struct interval_tree_node *it;
1016 unsigned last_pfn, pt_idx;
1020 /* validate the parameters */
1021 if (saddr & AMDGPU_GPU_PAGE_MASK || offset & AMDGPU_GPU_PAGE_MASK ||
1022 size == 0 || size & AMDGPU_GPU_PAGE_MASK) {
1023 amdgpu_bo_unreserve(bo_va->bo);
1027 /* make sure object fit at this offset */
1028 eaddr = saddr + size;
1029 if ((saddr >= eaddr) || (offset + size > amdgpu_bo_size(bo_va->bo))) {
1030 amdgpu_bo_unreserve(bo_va->bo);
1034 last_pfn = eaddr / AMDGPU_GPU_PAGE_SIZE;
1035 if (last_pfn > adev->vm_manager.max_pfn) {
1036 dev_err(adev->dev, "va above limit (0x%08X > 0x%08X)\n",
1037 last_pfn, adev->vm_manager.max_pfn);
1038 amdgpu_bo_unreserve(bo_va->bo);
1042 mutex_lock(&vm->mutex);
1044 saddr /= AMDGPU_GPU_PAGE_SIZE;
1045 eaddr /= AMDGPU_GPU_PAGE_SIZE;
1047 it = interval_tree_iter_first(&vm->va, saddr, eaddr - 1);
1049 struct amdgpu_bo_va_mapping *tmp;
1050 tmp = container_of(it, struct amdgpu_bo_va_mapping, it);
1051 /* bo and tmp overlap, invalid addr */
1052 dev_err(adev->dev, "bo %p va 0x%010Lx-0x%010Lx conflict with "
1053 "0x%010lx-0x%010lx\n", bo_va->bo, saddr, eaddr,
1054 tmp->it.start, tmp->it.last + 1);
1055 amdgpu_bo_unreserve(bo_va->bo);
1060 mapping = kmalloc(sizeof(*mapping), GFP_KERNEL);
1062 amdgpu_bo_unreserve(bo_va->bo);
1067 INIT_LIST_HEAD(&mapping->list);
1068 mapping->it.start = saddr;
1069 mapping->it.last = eaddr - 1;
1070 mapping->offset = offset;
1071 mapping->flags = flags;
1073 list_add(&mapping->list, &bo_va->invalids);
1074 interval_tree_insert(&mapping->it, &vm->va);
1075 trace_amdgpu_vm_bo_map(bo_va, mapping);
1077 /* Make sure the page tables are allocated */
1078 saddr >>= amdgpu_vm_block_size;
1079 eaddr >>= amdgpu_vm_block_size;
1081 BUG_ON(eaddr >= amdgpu_vm_num_pdes(adev));
1083 if (eaddr > vm->max_pde_used)
1084 vm->max_pde_used = eaddr;
1086 amdgpu_bo_unreserve(bo_va->bo);
1088 /* walk over the address space and allocate the page tables */
1089 for (pt_idx = saddr; pt_idx <= eaddr; ++pt_idx) {
1090 struct amdgpu_bo *pt;
1092 if (vm->page_tables[pt_idx].bo)
1095 /* drop mutex to allocate and clear page table */
1096 mutex_unlock(&vm->mutex);
1098 r = amdgpu_bo_create(adev, AMDGPU_VM_PTE_COUNT * 8,
1099 AMDGPU_GPU_PAGE_SIZE, true,
1100 AMDGPU_GEM_DOMAIN_VRAM, 0, NULL, &pt);
1104 r = amdgpu_vm_clear_bo(adev, pt);
1106 amdgpu_bo_unref(&pt);
1110 /* aquire mutex again */
1111 mutex_lock(&vm->mutex);
1112 if (vm->page_tables[pt_idx].bo) {
1113 /* someone else allocated the pt in the meantime */
1114 mutex_unlock(&vm->mutex);
1115 amdgpu_bo_unref(&pt);
1116 mutex_lock(&vm->mutex);
1120 vm->page_tables[pt_idx].addr = 0;
1121 vm->page_tables[pt_idx].bo = pt;
1124 mutex_unlock(&vm->mutex);
1128 mutex_lock(&vm->mutex);
1129 list_del(&mapping->list);
1130 interval_tree_remove(&mapping->it, &vm->va);
1131 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
1135 mutex_unlock(&vm->mutex);
1140 * amdgpu_vm_bo_unmap - remove bo mapping from vm
1142 * @adev: amdgpu_device pointer
1143 * @bo_va: bo_va to remove the address from
1144 * @saddr: where to the BO is mapped
1146 * Remove a mapping of the BO at the specefied addr from the VM.
1147 * Returns 0 for success, error for failure.
1149 * Object has to be reserved and gets unreserved by this function!
1151 int amdgpu_vm_bo_unmap(struct amdgpu_device *adev,
1152 struct amdgpu_bo_va *bo_va,
1155 struct amdgpu_bo_va_mapping *mapping;
1156 struct amdgpu_vm *vm = bo_va->vm;
1159 saddr /= AMDGPU_GPU_PAGE_SIZE;
1161 list_for_each_entry(mapping, &bo_va->valids, list) {
1162 if (mapping->it.start == saddr)
1166 if (&mapping->list == &bo_va->valids) {
1169 list_for_each_entry(mapping, &bo_va->invalids, list) {
1170 if (mapping->it.start == saddr)
1174 if (&mapping->list == &bo_va->invalids) {
1175 amdgpu_bo_unreserve(bo_va->bo);
1180 mutex_lock(&vm->mutex);
1181 list_del(&mapping->list);
1182 interval_tree_remove(&mapping->it, &vm->va);
1183 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
1186 list_add(&mapping->list, &vm->freed);
1189 mutex_unlock(&vm->mutex);
1190 amdgpu_bo_unreserve(bo_va->bo);
1196 * amdgpu_vm_bo_rmv - remove a bo to a specific vm
1198 * @adev: amdgpu_device pointer
1199 * @bo_va: requested bo_va
1201 * Remove @bo_va->bo from the requested vm (cayman+).
1203 * Object have to be reserved!
1205 void amdgpu_vm_bo_rmv(struct amdgpu_device *adev,
1206 struct amdgpu_bo_va *bo_va)
1208 struct amdgpu_bo_va_mapping *mapping, *next;
1209 struct amdgpu_vm *vm = bo_va->vm;
1211 list_del(&bo_va->bo_list);
1213 mutex_lock(&vm->mutex);
1215 spin_lock(&vm->status_lock);
1216 list_del(&bo_va->vm_status);
1217 spin_unlock(&vm->status_lock);
1219 list_for_each_entry_safe(mapping, next, &bo_va->valids, list) {
1220 list_del(&mapping->list);
1221 interval_tree_remove(&mapping->it, &vm->va);
1222 trace_amdgpu_vm_bo_unmap(bo_va, mapping);
1223 list_add(&mapping->list, &vm->freed);
1225 list_for_each_entry_safe(mapping, next, &bo_va->invalids, list) {
1226 list_del(&mapping->list);
1227 interval_tree_remove(&mapping->it, &vm->va);
1231 fence_put(bo_va->last_pt_update);
1234 mutex_unlock(&vm->mutex);
1238 * amdgpu_vm_bo_invalidate - mark the bo as invalid
1240 * @adev: amdgpu_device pointer
1242 * @bo: amdgpu buffer object
1244 * Mark @bo as invalid (cayman+).
1246 void amdgpu_vm_bo_invalidate(struct amdgpu_device *adev,
1247 struct amdgpu_bo *bo)
1249 struct amdgpu_bo_va *bo_va;
1251 list_for_each_entry(bo_va, &bo->va, bo_list) {
1252 spin_lock(&bo_va->vm->status_lock);
1253 if (list_empty(&bo_va->vm_status))
1254 list_add(&bo_va->vm_status, &bo_va->vm->invalidated);
1255 spin_unlock(&bo_va->vm->status_lock);
1260 * amdgpu_vm_init - initialize a vm instance
1262 * @adev: amdgpu_device pointer
1265 * Init @vm fields (cayman+).
1267 int amdgpu_vm_init(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1269 const unsigned align = min(AMDGPU_VM_PTB_ALIGN_SIZE,
1270 AMDGPU_VM_PTE_COUNT * 8);
1271 unsigned pd_size, pd_entries, pts_size;
1274 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
1276 vm->ids[i].flushed_updates = NULL;
1277 vm->ids[i].last_id_use = NULL;
1279 mutex_init(&vm->mutex);
1281 spin_lock_init(&vm->status_lock);
1282 INIT_LIST_HEAD(&vm->invalidated);
1283 INIT_LIST_HEAD(&vm->cleared);
1284 INIT_LIST_HEAD(&vm->freed);
1286 pd_size = amdgpu_vm_directory_size(adev);
1287 pd_entries = amdgpu_vm_num_pdes(adev);
1289 /* allocate page table array */
1290 pts_size = pd_entries * sizeof(struct amdgpu_vm_pt);
1291 vm->page_tables = kzalloc(pts_size, GFP_KERNEL);
1292 if (vm->page_tables == NULL) {
1293 DRM_ERROR("Cannot allocate memory for page table array\n");
1297 vm->page_directory_fence = NULL;
1299 r = amdgpu_bo_create(adev, pd_size, align, true,
1300 AMDGPU_GEM_DOMAIN_VRAM, 0,
1301 NULL, &vm->page_directory);
1305 r = amdgpu_vm_clear_bo(adev, vm->page_directory);
1307 amdgpu_bo_unref(&vm->page_directory);
1308 vm->page_directory = NULL;
1316 * amdgpu_vm_fini - tear down a vm instance
1318 * @adev: amdgpu_device pointer
1321 * Tear down @vm (cayman+).
1322 * Unbind the VM and remove all bos from the vm bo list
1324 void amdgpu_vm_fini(struct amdgpu_device *adev, struct amdgpu_vm *vm)
1326 struct amdgpu_bo_va_mapping *mapping, *tmp;
1329 if (!RB_EMPTY_ROOT(&vm->va)) {
1330 dev_err(adev->dev, "still active bo inside vm\n");
1332 rbtree_postorder_for_each_entry_safe(mapping, tmp, &vm->va, it.rb) {
1333 list_del(&mapping->list);
1334 interval_tree_remove(&mapping->it, &vm->va);
1337 list_for_each_entry_safe(mapping, tmp, &vm->freed, list) {
1338 list_del(&mapping->list);
1342 for (i = 0; i < amdgpu_vm_num_pdes(adev); i++)
1343 amdgpu_bo_unref(&vm->page_tables[i].bo);
1344 kfree(vm->page_tables);
1346 amdgpu_bo_unref(&vm->page_directory);
1347 fence_put(vm->page_directory_fence);
1349 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
1350 amdgpu_fence_unref(&vm->ids[i].flushed_updates);
1351 amdgpu_fence_unref(&vm->ids[i].last_id_use);
1354 mutex_destroy(&vm->mutex);