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/radeon_drm.h>
31 #include "radeon_reg.h"
35 * The GART (Graphics Aperture Remapping Table) is an aperture
36 * in the GPU's address space. System pages can be mapped into
37 * the aperture and look like contiguous pages from the GPU's
38 * perspective. A page table maps the pages in the aperture
39 * to the actual backing pages in system memory.
41 * Radeon GPUs support both an internal GART, as described above,
42 * and AGP. AGP works similarly, but the GART table is configured
43 * and maintained by the northbridge rather than the driver.
44 * Radeon hw has a separate AGP aperture that is programmed to
45 * point to the AGP aperture provided by the northbridge and the
46 * requests are passed through to the northbridge aperture.
47 * Both AGP and internal GART can be used at the same time, however
48 * that is not currently supported by the driver.
50 * This file handles the common internal GART management.
54 * Common GART table functions.
57 * radeon_gart_table_ram_alloc - allocate system ram for gart page table
59 * @rdev: radeon_device pointer
61 * Allocate system memory for GART page table
62 * (r1xx-r3xx, non-pcie r4xx, rs400). These asics require the
63 * gart table to be in system memory.
64 * Returns 0 for success, -ENOMEM for failure.
66 int radeon_gart_table_ram_alloc(struct radeon_device *rdev)
70 ptr = pci_alloc_consistent(rdev->pdev, rdev->gart.table_size,
71 &rdev->gart.table_addr);
76 if (rdev->family == CHIP_RS400 || rdev->family == CHIP_RS480 ||
77 rdev->family == CHIP_RS690 || rdev->family == CHIP_RS740) {
78 set_memory_uc((unsigned long)ptr,
79 rdev->gart.table_size >> PAGE_SHIFT);
83 memset((void *)rdev->gart.ptr, 0, rdev->gart.table_size);
88 * radeon_gart_table_ram_free - free system ram for gart page table
90 * @rdev: radeon_device pointer
92 * Free system memory for GART page table
93 * (r1xx-r3xx, non-pcie r4xx, rs400). These asics require the
94 * gart table to be in system memory.
96 void radeon_gart_table_ram_free(struct radeon_device *rdev)
98 if (rdev->gart.ptr == NULL) {
102 if (rdev->family == CHIP_RS400 || rdev->family == CHIP_RS480 ||
103 rdev->family == CHIP_RS690 || rdev->family == CHIP_RS740) {
104 set_memory_wb((unsigned long)rdev->gart.ptr,
105 rdev->gart.table_size >> PAGE_SHIFT);
108 pci_free_consistent(rdev->pdev, rdev->gart.table_size,
109 (void *)rdev->gart.ptr,
110 rdev->gart.table_addr);
111 rdev->gart.ptr = NULL;
112 rdev->gart.table_addr = 0;
116 * radeon_gart_table_vram_alloc - allocate vram for gart page table
118 * @rdev: radeon_device pointer
120 * Allocate video memory for GART page table
121 * (pcie r4xx, r5xx+). These asics require the
122 * gart table to be in video memory.
123 * Returns 0 for success, error for failure.
125 int radeon_gart_table_vram_alloc(struct radeon_device *rdev)
129 if (rdev->gart.robj == NULL) {
130 r = radeon_bo_create(rdev, rdev->gart.table_size,
131 PAGE_SIZE, true, RADEON_GEM_DOMAIN_VRAM,
132 NULL, &rdev->gart.robj);
141 * radeon_gart_table_vram_pin - pin gart page table in vram
143 * @rdev: radeon_device pointer
145 * Pin the GART page table in vram so it will not be moved
146 * by the memory manager (pcie r4xx, r5xx+). These asics require the
147 * gart table to be in video memory.
148 * Returns 0 for success, error for failure.
150 int radeon_gart_table_vram_pin(struct radeon_device *rdev)
155 r = radeon_bo_reserve(rdev->gart.robj, false);
156 if (unlikely(r != 0))
158 r = radeon_bo_pin(rdev->gart.robj,
159 RADEON_GEM_DOMAIN_VRAM, &gpu_addr);
161 radeon_bo_unreserve(rdev->gart.robj);
164 r = radeon_bo_kmap(rdev->gart.robj, &rdev->gart.ptr);
166 radeon_bo_unpin(rdev->gart.robj);
167 radeon_bo_unreserve(rdev->gart.robj);
168 rdev->gart.table_addr = gpu_addr;
173 * radeon_gart_table_vram_unpin - unpin gart page table in vram
175 * @rdev: radeon_device pointer
177 * Unpin the GART page table in vram (pcie r4xx, r5xx+).
178 * These asics require the gart table to be in video memory.
180 void radeon_gart_table_vram_unpin(struct radeon_device *rdev)
184 if (rdev->gart.robj == NULL) {
187 r = radeon_bo_reserve(rdev->gart.robj, false);
188 if (likely(r == 0)) {
189 radeon_bo_kunmap(rdev->gart.robj);
190 radeon_bo_unpin(rdev->gart.robj);
191 radeon_bo_unreserve(rdev->gart.robj);
192 rdev->gart.ptr = NULL;
197 * radeon_gart_table_vram_free - free gart page table vram
199 * @rdev: radeon_device pointer
201 * Free the video memory used for the GART page table
202 * (pcie r4xx, r5xx+). These asics require the gart table to
203 * be in video memory.
205 void radeon_gart_table_vram_free(struct radeon_device *rdev)
207 if (rdev->gart.robj == NULL) {
210 radeon_gart_table_vram_unpin(rdev);
211 radeon_bo_unref(&rdev->gart.robj);
215 * Common gart functions.
218 * radeon_gart_unbind - unbind pages from the gart page table
220 * @rdev: radeon_device pointer
221 * @offset: offset into the GPU's gart aperture
222 * @pages: number of pages to unbind
224 * Unbinds the requested pages from the gart page table and
225 * replaces them with the dummy page (all asics).
227 void radeon_gart_unbind(struct radeon_device *rdev, unsigned offset,
235 if (!rdev->gart.ready) {
236 WARN(1, "trying to unbind memory from uninitialized GART !\n");
239 t = offset / RADEON_GPU_PAGE_SIZE;
240 p = t / (PAGE_SIZE / RADEON_GPU_PAGE_SIZE);
241 for (i = 0; i < pages; i++, p++) {
242 if (rdev->gart.pages[p]) {
243 rdev->gart.pages[p] = NULL;
244 rdev->gart.pages_addr[p] = rdev->dummy_page.addr;
245 page_base = rdev->gart.pages_addr[p];
246 for (j = 0; j < (PAGE_SIZE / RADEON_GPU_PAGE_SIZE); j++, t++) {
247 if (rdev->gart.ptr) {
248 radeon_gart_set_page(rdev, t, page_base);
250 page_base += RADEON_GPU_PAGE_SIZE;
255 radeon_gart_tlb_flush(rdev);
259 * radeon_gart_bind - bind pages into the gart page table
261 * @rdev: radeon_device pointer
262 * @offset: offset into the GPU's gart aperture
263 * @pages: number of pages to bind
264 * @pagelist: pages to bind
265 * @dma_addr: DMA addresses of pages
267 * Binds the requested pages to the gart page table
269 * Returns 0 for success, -EINVAL for failure.
271 int radeon_gart_bind(struct radeon_device *rdev, unsigned offset,
272 int pages, struct page **pagelist, dma_addr_t *dma_addr)
279 if (!rdev->gart.ready) {
280 WARN(1, "trying to bind memory to uninitialized GART !\n");
283 t = offset / RADEON_GPU_PAGE_SIZE;
284 p = t / (PAGE_SIZE / RADEON_GPU_PAGE_SIZE);
286 for (i = 0; i < pages; i++, p++) {
287 rdev->gart.pages_addr[p] = dma_addr[i];
288 rdev->gart.pages[p] = pagelist[i];
289 if (rdev->gart.ptr) {
290 page_base = rdev->gart.pages_addr[p];
291 for (j = 0; j < (PAGE_SIZE / RADEON_GPU_PAGE_SIZE); j++, t++) {
292 radeon_gart_set_page(rdev, t, page_base);
293 page_base += RADEON_GPU_PAGE_SIZE;
298 radeon_gart_tlb_flush(rdev);
303 * radeon_gart_restore - bind all pages in the gart page table
305 * @rdev: radeon_device pointer
307 * Binds all pages in the gart page table (all asics).
308 * Used to rebuild the gart table on device startup or resume.
310 void radeon_gart_restore(struct radeon_device *rdev)
315 if (!rdev->gart.ptr) {
318 for (i = 0, t = 0; i < rdev->gart.num_cpu_pages; i++) {
319 page_base = rdev->gart.pages_addr[i];
320 for (j = 0; j < (PAGE_SIZE / RADEON_GPU_PAGE_SIZE); j++, t++) {
321 radeon_gart_set_page(rdev, t, page_base);
322 page_base += RADEON_GPU_PAGE_SIZE;
326 radeon_gart_tlb_flush(rdev);
330 * radeon_gart_init - init the driver info for managing the gart
332 * @rdev: radeon_device pointer
334 * Allocate the dummy page and init the gart driver info (all asics).
335 * Returns 0 for success, error for failure.
337 int radeon_gart_init(struct radeon_device *rdev)
341 if (rdev->gart.pages) {
344 /* We need PAGE_SIZE >= RADEON_GPU_PAGE_SIZE */
345 if (PAGE_SIZE < RADEON_GPU_PAGE_SIZE) {
346 DRM_ERROR("Page size is smaller than GPU page size!\n");
349 r = radeon_dummy_page_init(rdev);
352 /* Compute table size */
353 rdev->gart.num_cpu_pages = rdev->mc.gtt_size / PAGE_SIZE;
354 rdev->gart.num_gpu_pages = rdev->mc.gtt_size / RADEON_GPU_PAGE_SIZE;
355 DRM_INFO("GART: num cpu pages %u, num gpu pages %u\n",
356 rdev->gart.num_cpu_pages, rdev->gart.num_gpu_pages);
357 /* Allocate pages table */
358 rdev->gart.pages = kzalloc(sizeof(void *) * rdev->gart.num_cpu_pages,
360 if (rdev->gart.pages == NULL) {
361 radeon_gart_fini(rdev);
364 rdev->gart.pages_addr = kzalloc(sizeof(dma_addr_t) *
365 rdev->gart.num_cpu_pages, GFP_KERNEL);
366 if (rdev->gart.pages_addr == NULL) {
367 radeon_gart_fini(rdev);
370 /* set GART entry to point to the dummy page by default */
371 for (i = 0; i < rdev->gart.num_cpu_pages; i++) {
372 rdev->gart.pages_addr[i] = rdev->dummy_page.addr;
378 * radeon_gart_fini - tear down the driver info for managing the gart
380 * @rdev: radeon_device pointer
382 * Tear down the gart driver info and free the dummy page (all asics).
384 void radeon_gart_fini(struct radeon_device *rdev)
386 if (rdev->gart.pages && rdev->gart.pages_addr && rdev->gart.ready) {
388 radeon_gart_unbind(rdev, 0, rdev->gart.num_cpu_pages);
390 rdev->gart.ready = false;
391 kfree(rdev->gart.pages);
392 kfree(rdev->gart.pages_addr);
393 rdev->gart.pages = NULL;
394 rdev->gart.pages_addr = NULL;
396 radeon_dummy_page_fini(rdev);
401 * GPUVM is similar to the legacy gart on older asics, however
402 * rather than there being a single global gart table
403 * for the entire GPU, there are multiple VM page tables active
404 * at any given time. The VM page tables can contain a mix
405 * vram pages and system memory pages and system memory pages
406 * can be mapped as snooped (cached system pages) or unsnooped
407 * (uncached system pages).
408 * Each VM has an ID associated with it and there is a page table
409 * associated with each VMID. When execting a command buffer,
410 * the kernel tells the the ring what VMID to use for that command
411 * buffer. VMIDs are allocated dynamically as commands are submitted.
412 * The userspace drivers maintain their own address space and the kernel
413 * sets up their pages tables accordingly when they submit their
414 * command buffers and a VMID is assigned.
415 * Cayman/Trinity support up to 8 active VMs at any given time;
422 * TODO bind a default page at vm initialization for default address
426 * radeon_vm_directory_size - returns the size of the page directory in bytes
428 * @rdev: radeon_device pointer
430 * Calculate the size of the page directory in bytes (cayman+).
432 static unsigned radeon_vm_directory_size(struct radeon_device *rdev)
434 return (rdev->vm_manager.max_pfn >> RADEON_VM_BLOCK_SIZE) * 8;
438 * radeon_vm_manager_init - init the vm manager
440 * @rdev: radeon_device pointer
442 * Init the vm manager (cayman+).
443 * Returns 0 for success, error for failure.
445 int radeon_vm_manager_init(struct radeon_device *rdev)
447 struct radeon_vm *vm;
448 struct radeon_bo_va *bo_va;
452 if (!rdev->vm_manager.enabled) {
453 /* allocate enough for 2 full VM pts */
454 size = RADEON_GPU_PAGE_ALIGN(radeon_vm_directory_size(rdev));
455 size += RADEON_GPU_PAGE_ALIGN(rdev->vm_manager.max_pfn * 8);
457 r = radeon_sa_bo_manager_init(rdev, &rdev->vm_manager.sa_manager,
459 RADEON_GEM_DOMAIN_VRAM);
461 dev_err(rdev->dev, "failed to allocate vm bo (%dKB)\n",
462 (rdev->vm_manager.max_pfn * 8) >> 10);
466 r = radeon_asic_vm_init(rdev);
470 rdev->vm_manager.enabled = true;
472 r = radeon_sa_bo_manager_start(rdev, &rdev->vm_manager.sa_manager);
477 /* restore page table */
478 list_for_each_entry(vm, &rdev->vm_manager.lru_vm, list) {
479 if (vm->sa_bo == NULL)
482 list_for_each_entry(bo_va, &vm->va, vm_list) {
483 bo_va->valid = false;
490 * radeon_vm_free_pt - free the page table for a specific vm
492 * @rdev: radeon_device pointer
495 * Free the page table of a specific vm (cayman+).
497 * Global and local mutex must be lock!
499 static void radeon_vm_free_pt(struct radeon_device *rdev,
500 struct radeon_vm *vm)
502 struct radeon_bo_va *bo_va;
507 list_del_init(&vm->list);
508 radeon_sa_bo_free(rdev, &vm->sa_bo, vm->fence);
510 list_for_each_entry(bo_va, &vm->va, vm_list) {
511 bo_va->valid = false;
516 * radeon_vm_manager_fini - tear down the vm manager
518 * @rdev: radeon_device pointer
520 * Tear down the VM manager (cayman+).
522 void radeon_vm_manager_fini(struct radeon_device *rdev)
524 struct radeon_vm *vm, *tmp;
527 if (!rdev->vm_manager.enabled)
530 mutex_lock(&rdev->vm_manager.lock);
531 /* free all allocated page tables */
532 list_for_each_entry_safe(vm, tmp, &rdev->vm_manager.lru_vm, list) {
533 mutex_lock(&vm->mutex);
534 radeon_vm_free_pt(rdev, vm);
535 mutex_unlock(&vm->mutex);
537 for (i = 0; i < RADEON_NUM_VM; ++i) {
538 radeon_fence_unref(&rdev->vm_manager.active[i]);
540 radeon_asic_vm_fini(rdev);
541 mutex_unlock(&rdev->vm_manager.lock);
543 radeon_sa_bo_manager_suspend(rdev, &rdev->vm_manager.sa_manager);
544 radeon_sa_bo_manager_fini(rdev, &rdev->vm_manager.sa_manager);
545 rdev->vm_manager.enabled = false;
549 * radeon_vm_alloc_pt - allocates a page table for a VM
551 * @rdev: radeon_device pointer
554 * Allocate a page table for the requested vm (cayman+).
555 * Also starts to populate the page table.
556 * Returns 0 for success, error for failure.
558 * Global and local mutex must be locked!
560 int radeon_vm_alloc_pt(struct radeon_device *rdev, struct radeon_vm *vm)
562 struct radeon_vm *vm_evict;
571 /* allocate enough to cover the current VM size */
572 tables_size = RADEON_GPU_PAGE_ALIGN(radeon_vm_directory_size(rdev));
573 tables_size += RADEON_GPU_PAGE_ALIGN(vm->last_pfn * 8);
575 if (vm->sa_bo != NULL) {
577 list_del_init(&vm->list);
578 list_add_tail(&vm->list, &rdev->vm_manager.lru_vm);
583 r = radeon_sa_bo_new(rdev, &rdev->vm_manager.sa_manager, &vm->sa_bo,
584 tables_size, RADEON_GPU_PAGE_SIZE, false);
586 if (list_empty(&rdev->vm_manager.lru_vm)) {
589 vm_evict = list_first_entry(&rdev->vm_manager.lru_vm, struct radeon_vm, list);
590 mutex_lock(&vm_evict->mutex);
591 radeon_vm_free_pt(rdev, vm_evict);
592 mutex_unlock(&vm_evict->mutex);
599 pd_addr = radeon_sa_bo_cpu_addr(vm->sa_bo);
600 vm->pd_gpu_addr = radeon_sa_bo_gpu_addr(vm->sa_bo);
601 memset(pd_addr, 0, tables_size);
603 list_add_tail(&vm->list, &rdev->vm_manager.lru_vm);
604 return radeon_vm_bo_update_pte(rdev, vm, rdev->ring_tmp_bo.bo,
605 &rdev->ring_tmp_bo.bo->tbo.mem);
609 * radeon_vm_grab_id - allocate the next free VMID
611 * @rdev: radeon_device pointer
612 * @vm: vm to allocate id for
613 * @ring: ring we want to submit job to
615 * Allocate an id for the vm (cayman+).
616 * Returns the fence we need to sync to (if any).
618 * Global and local mutex must be locked!
620 struct radeon_fence *radeon_vm_grab_id(struct radeon_device *rdev,
621 struct radeon_vm *vm, int ring)
623 struct radeon_fence *best[RADEON_NUM_RINGS] = {};
624 unsigned choices[2] = {};
627 /* check if the id is still valid */
628 if (vm->fence && vm->fence == rdev->vm_manager.active[vm->id])
631 /* we definately need to flush */
632 radeon_fence_unref(&vm->last_flush);
634 /* skip over VMID 0, since it is the system VM */
635 for (i = 1; i < rdev->vm_manager.nvm; ++i) {
636 struct radeon_fence *fence = rdev->vm_manager.active[i];
639 /* found a free one */
644 if (radeon_fence_is_earlier(fence, best[fence->ring])) {
645 best[fence->ring] = fence;
646 choices[fence->ring == ring ? 0 : 1] = i;
650 for (i = 0; i < 2; ++i) {
653 return rdev->vm_manager.active[choices[i]];
657 /* should never happen */
663 * radeon_vm_fence - remember fence for vm
665 * @rdev: radeon_device pointer
666 * @vm: vm we want to fence
667 * @fence: fence to remember
669 * Fence the vm (cayman+).
670 * Set the fence used to protect page table and id.
672 * Global and local mutex must be locked!
674 void radeon_vm_fence(struct radeon_device *rdev,
675 struct radeon_vm *vm,
676 struct radeon_fence *fence)
678 radeon_fence_unref(&rdev->vm_manager.active[vm->id]);
679 rdev->vm_manager.active[vm->id] = radeon_fence_ref(fence);
681 radeon_fence_unref(&vm->fence);
682 vm->fence = radeon_fence_ref(fence);
686 * radeon_vm_bo_find - find the bo_va for a specific vm & bo
689 * @bo: requested buffer object
691 * Find @bo inside the requested vm (cayman+).
692 * Search inside the @bos vm list for the requested vm
693 * Returns the found bo_va or NULL if none is found
695 * Object has to be reserved!
697 struct radeon_bo_va *radeon_vm_bo_find(struct radeon_vm *vm,
698 struct radeon_bo *bo)
700 struct radeon_bo_va *bo_va;
702 list_for_each_entry(bo_va, &bo->va, bo_list) {
703 if (bo_va->vm == vm) {
711 * radeon_vm_bo_add - add a bo to a specific vm
713 * @rdev: radeon_device pointer
715 * @bo: radeon buffer object
717 * Add @bo into the requested vm (cayman+).
718 * Add @bo to the list of bos associated with the vm
719 * Returns newly added bo_va or NULL for failure
721 * Object has to be reserved!
723 struct radeon_bo_va *radeon_vm_bo_add(struct radeon_device *rdev,
724 struct radeon_vm *vm,
725 struct radeon_bo *bo)
727 struct radeon_bo_va *bo_va;
729 bo_va = kzalloc(sizeof(struct radeon_bo_va), GFP_KERNEL);
738 bo_va->valid = false;
739 bo_va->ref_count = 1;
740 INIT_LIST_HEAD(&bo_va->bo_list);
741 INIT_LIST_HEAD(&bo_va->vm_list);
743 mutex_lock(&vm->mutex);
744 list_add(&bo_va->vm_list, &vm->va);
745 list_add_tail(&bo_va->bo_list, &bo->va);
746 mutex_unlock(&vm->mutex);
752 * radeon_vm_bo_set_addr - set bos virtual address inside a vm
754 * @rdev: radeon_device pointer
755 * @bo_va: bo_va to store the address
756 * @soffset: requested offset of the buffer in the VM address space
757 * @flags: attributes of pages (read/write/valid/etc.)
759 * Set offset of @bo_va (cayman+).
760 * Validate and set the offset requested within the vm address space.
761 * Returns 0 for success, error for failure.
763 * Object has to be reserved!
765 int radeon_vm_bo_set_addr(struct radeon_device *rdev,
766 struct radeon_bo_va *bo_va,
770 uint64_t size = radeon_bo_size(bo_va->bo);
771 uint64_t eoffset, last_offset = 0;
772 struct radeon_vm *vm = bo_va->vm;
773 struct radeon_bo_va *tmp;
774 struct list_head *head;
778 /* make sure object fit at this offset */
779 eoffset = soffset + size;
780 if (soffset >= eoffset) {
784 last_pfn = eoffset / RADEON_GPU_PAGE_SIZE;
785 if (last_pfn > rdev->vm_manager.max_pfn) {
786 dev_err(rdev->dev, "va above limit (0x%08X > 0x%08X)\n",
787 last_pfn, rdev->vm_manager.max_pfn);
792 eoffset = last_pfn = 0;
795 mutex_lock(&vm->mutex);
796 if (last_pfn > vm->last_pfn) {
797 /* release mutex and lock in right order */
798 mutex_unlock(&vm->mutex);
799 mutex_lock(&rdev->vm_manager.lock);
800 mutex_lock(&vm->mutex);
801 /* and check again */
802 if (last_pfn > vm->last_pfn) {
803 /* grow va space 32M by 32M */
804 unsigned align = ((32 << 20) >> 12) - 1;
805 radeon_vm_free_pt(rdev, vm);
806 vm->last_pfn = (last_pfn + align) & ~align;
808 mutex_unlock(&rdev->vm_manager.lock);
812 list_for_each_entry(tmp, &vm->va, vm_list) {
814 /* skip over currently modified bo */
818 if (soffset >= last_offset && eoffset <= tmp->soffset) {
819 /* bo can be added before this one */
822 if (eoffset > tmp->soffset && soffset < tmp->eoffset) {
823 /* bo and tmp overlap, invalid offset */
824 dev_err(rdev->dev, "bo %p va 0x%08X conflict with (bo %p 0x%08X 0x%08X)\n",
825 bo_va->bo, (unsigned)bo_va->soffset, tmp->bo,
826 (unsigned)tmp->soffset, (unsigned)tmp->eoffset);
827 mutex_unlock(&vm->mutex);
830 last_offset = tmp->eoffset;
831 head = &tmp->vm_list;
834 bo_va->soffset = soffset;
835 bo_va->eoffset = eoffset;
836 bo_va->flags = flags;
837 bo_va->valid = false;
838 list_move(&bo_va->vm_list, head);
840 mutex_unlock(&vm->mutex);
845 * radeon_vm_map_gart - get the physical address of a gart page
847 * @rdev: radeon_device pointer
848 * @addr: the unmapped addr
850 * Look up the physical address of the page that the pte resolves
852 * Returns the physical address of the page.
854 uint64_t radeon_vm_map_gart(struct radeon_device *rdev, uint64_t addr)
858 /* page table offset */
859 result = rdev->gart.pages_addr[addr >> PAGE_SHIFT];
861 /* in case cpu page size != gpu page size*/
862 result |= addr & (~PAGE_MASK);
868 * radeon_vm_bo_update_pte - map a bo into the vm page table
870 * @rdev: radeon_device pointer
872 * @bo: radeon buffer object
875 * Fill in the page table entries for @bo (cayman+).
876 * Returns 0 for success, -EINVAL for failure.
878 * Object have to be reserved & global and local mutex must be locked!
880 int radeon_vm_bo_update_pte(struct radeon_device *rdev,
881 struct radeon_vm *vm,
882 struct radeon_bo *bo,
883 struct ttm_mem_reg *mem)
885 unsigned ridx = rdev->asic->vm.pt_ring_index;
886 struct radeon_ring *ring = &rdev->ring[ridx];
887 struct radeon_semaphore *sem = NULL;
888 struct radeon_bo_va *bo_va;
889 unsigned nptes, npdes, ndw;
894 /* nothing to do if vm isn't bound */
895 if (vm->sa_bo == NULL)
898 bo_va = radeon_vm_bo_find(vm, bo);
900 dev_err(rdev->dev, "bo %p not in vm %p\n", bo, vm);
904 if (!bo_va->soffset) {
905 dev_err(rdev->dev, "bo %p don't has a mapping in vm %p\n",
910 if ((bo_va->valid && mem) || (!bo_va->valid && mem == NULL))
913 bo_va->flags &= ~RADEON_VM_PAGE_VALID;
914 bo_va->flags &= ~RADEON_VM_PAGE_SYSTEM;
916 addr = mem->start << PAGE_SHIFT;
917 if (mem->mem_type != TTM_PL_SYSTEM) {
918 bo_va->flags |= RADEON_VM_PAGE_VALID;
921 if (mem->mem_type == TTM_PL_TT) {
922 bo_va->flags |= RADEON_VM_PAGE_SYSTEM;
924 addr += rdev->vm_manager.vram_base_offset;
928 bo_va->valid = false;
931 if (vm->fence && radeon_fence_signaled(vm->fence)) {
932 radeon_fence_unref(&vm->fence);
935 if (vm->fence && vm->fence->ring != ridx) {
936 r = radeon_semaphore_create(rdev, &sem);
942 /* estimate number of dw needed */
943 /* reserve space for 32-bit padding */
946 nptes = radeon_bo_ngpu_pages(bo);
948 pfn = (bo_va->soffset / RADEON_GPU_PAGE_SIZE);
950 /* handle cases where a bo spans several pdes */
951 npdes = (ALIGN(pfn + nptes, RADEON_VM_PTE_COUNT) -
952 (pfn & ~(RADEON_VM_PTE_COUNT - 1))) >> RADEON_VM_BLOCK_SIZE;
954 /* reserve space for one header for every 2k dwords */
955 ndw += (nptes >> 11) * 3;
956 /* reserve space for pte addresses */
959 /* reserve space for one header for every 2k dwords */
960 ndw += (npdes >> 11) * 3;
961 /* reserve space for pde addresses */
964 r = radeon_ring_lock(rdev, ring, ndw);
969 if (sem && radeon_fence_need_sync(vm->fence, ridx)) {
970 radeon_semaphore_sync_rings(rdev, sem, vm->fence->ring, ridx);
971 radeon_fence_note_sync(vm->fence, ridx);
974 /* update page table entries */
975 pe = vm->pd_gpu_addr;
976 pe += radeon_vm_directory_size(rdev);
977 pe += (bo_va->soffset / RADEON_GPU_PAGE_SIZE) * 8;
979 radeon_asic_vm_set_page(rdev, pe, addr, nptes,
980 RADEON_GPU_PAGE_SIZE, bo_va->flags);
982 /* update page directory entries */
985 pe = vm->pd_gpu_addr;
986 pe += ((bo_va->soffset / RADEON_GPU_PAGE_SIZE) >> RADEON_VM_BLOCK_SIZE) * 8;
988 radeon_asic_vm_set_page(rdev, pe, addr, npdes,
989 RADEON_VM_PTE_COUNT * 8, RADEON_VM_PAGE_VALID);
991 radeon_fence_unref(&vm->fence);
992 r = radeon_fence_emit(rdev, &vm->fence, ridx);
994 radeon_ring_unlock_undo(rdev, ring);
997 radeon_ring_unlock_commit(rdev, ring);
998 radeon_semaphore_free(rdev, &sem, vm->fence);
999 radeon_fence_unref(&vm->last_flush);
1004 * radeon_vm_bo_rmv - remove a bo to a specific vm
1006 * @rdev: radeon_device pointer
1007 * @bo_va: requested bo_va
1009 * Remove @bo_va->bo from the requested vm (cayman+).
1010 * Remove @bo_va->bo from the list of bos associated with the bo_va->vm and
1011 * remove the ptes for @bo_va in the page table.
1012 * Returns 0 for success.
1014 * Object have to be reserved!
1016 int radeon_vm_bo_rmv(struct radeon_device *rdev,
1017 struct radeon_bo_va *bo_va)
1021 mutex_lock(&rdev->vm_manager.lock);
1022 mutex_lock(&bo_va->vm->mutex);
1023 r = radeon_vm_bo_update_pte(rdev, bo_va->vm, bo_va->bo, NULL);
1024 mutex_unlock(&rdev->vm_manager.lock);
1025 list_del(&bo_va->vm_list);
1026 mutex_unlock(&bo_va->vm->mutex);
1027 list_del(&bo_va->bo_list);
1034 * radeon_vm_bo_invalidate - mark the bo as invalid
1036 * @rdev: radeon_device pointer
1038 * @bo: radeon buffer object
1040 * Mark @bo as invalid (cayman+).
1042 void radeon_vm_bo_invalidate(struct radeon_device *rdev,
1043 struct radeon_bo *bo)
1045 struct radeon_bo_va *bo_va;
1047 BUG_ON(!atomic_read(&bo->tbo.reserved));
1048 list_for_each_entry(bo_va, &bo->va, bo_list) {
1049 bo_va->valid = false;
1054 * radeon_vm_init - initialize a vm instance
1056 * @rdev: radeon_device pointer
1059 * Init @vm (cayman+).
1060 * Map the IB pool and any other shared objects into the VM
1061 * by default as it's used by all VMs.
1062 * Returns 0 for success, error for failure.
1064 int radeon_vm_init(struct radeon_device *rdev, struct radeon_vm *vm)
1066 struct radeon_bo_va *bo_va;
1072 mutex_init(&vm->mutex);
1073 INIT_LIST_HEAD(&vm->list);
1074 INIT_LIST_HEAD(&vm->va);
1076 /* map the ib pool buffer at 0 in virtual address space, set
1079 bo_va = radeon_vm_bo_add(rdev, vm, rdev->ring_tmp_bo.bo);
1080 r = radeon_vm_bo_set_addr(rdev, bo_va, RADEON_VA_IB_OFFSET,
1081 RADEON_VM_PAGE_READABLE |
1082 RADEON_VM_PAGE_SNOOPED);
1087 * radeon_vm_fini - tear down a vm instance
1089 * @rdev: radeon_device pointer
1092 * Tear down @vm (cayman+).
1093 * Unbind the VM and remove all bos from the vm bo list
1095 void radeon_vm_fini(struct radeon_device *rdev, struct radeon_vm *vm)
1097 struct radeon_bo_va *bo_va, *tmp;
1100 mutex_lock(&rdev->vm_manager.lock);
1101 mutex_lock(&vm->mutex);
1102 radeon_vm_free_pt(rdev, vm);
1103 mutex_unlock(&rdev->vm_manager.lock);
1105 /* remove all bo at this point non are busy any more because unbind
1106 * waited for the last vm fence to signal
1108 r = radeon_bo_reserve(rdev->ring_tmp_bo.bo, false);
1110 bo_va = radeon_vm_bo_find(vm, rdev->ring_tmp_bo.bo);
1111 list_del_init(&bo_va->bo_list);
1112 list_del_init(&bo_va->vm_list);
1113 radeon_bo_unreserve(rdev->ring_tmp_bo.bo);
1116 if (!list_empty(&vm->va)) {
1117 dev_err(rdev->dev, "still active bo inside vm\n");
1119 list_for_each_entry_safe(bo_va, tmp, &vm->va, vm_list) {
1120 list_del_init(&bo_va->vm_list);
1121 r = radeon_bo_reserve(bo_va->bo, false);
1123 list_del_init(&bo_va->bo_list);
1124 radeon_bo_unreserve(bo_va->bo);
1128 radeon_fence_unref(&vm->fence);
1129 radeon_fence_unref(&vm->last_flush);
1130 mutex_unlock(&vm->mutex);