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_bo_unref(&rdev
->gart
.robj
);
214 * Common gart functions.
217 * radeon_gart_unbind - unbind pages from the gart page table
219 * @rdev: radeon_device pointer
220 * @offset: offset into the GPU's gart aperture
221 * @pages: number of pages to unbind
223 * Unbinds the requested pages from the gart page table and
224 * replaces them with the dummy page (all asics).
226 void radeon_gart_unbind(struct radeon_device
*rdev
, unsigned offset
,
234 if (!rdev
->gart
.ready
) {
235 WARN(1, "trying to unbind memory from uninitialized GART !\n");
238 t
= offset
/ RADEON_GPU_PAGE_SIZE
;
239 p
= t
/ (PAGE_SIZE
/ RADEON_GPU_PAGE_SIZE
);
240 for (i
= 0; i
< pages
; i
++, p
++) {
241 if (rdev
->gart
.pages
[p
]) {
242 rdev
->gart
.pages
[p
] = NULL
;
243 rdev
->gart
.pages_addr
[p
] = rdev
->dummy_page
.addr
;
244 page_base
= rdev
->gart
.pages_addr
[p
];
245 for (j
= 0; j
< (PAGE_SIZE
/ RADEON_GPU_PAGE_SIZE
); j
++, t
++) {
246 if (rdev
->gart
.ptr
) {
247 radeon_gart_set_page(rdev
, t
, page_base
);
249 page_base
+= RADEON_GPU_PAGE_SIZE
;
254 radeon_gart_tlb_flush(rdev
);
258 * radeon_gart_bind - bind pages into the gart page table
260 * @rdev: radeon_device pointer
261 * @offset: offset into the GPU's gart aperture
262 * @pages: number of pages to bind
263 * @pagelist: pages to bind
264 * @dma_addr: DMA addresses of pages
266 * Binds the requested pages to the gart page table
268 * Returns 0 for success, -EINVAL for failure.
270 int radeon_gart_bind(struct radeon_device
*rdev
, unsigned offset
,
271 int pages
, struct page
**pagelist
, dma_addr_t
*dma_addr
)
278 if (!rdev
->gart
.ready
) {
279 WARN(1, "trying to bind memory to uninitialized GART !\n");
282 t
= offset
/ RADEON_GPU_PAGE_SIZE
;
283 p
= t
/ (PAGE_SIZE
/ RADEON_GPU_PAGE_SIZE
);
285 for (i
= 0; i
< pages
; i
++, p
++) {
286 rdev
->gart
.pages_addr
[p
] = dma_addr
[i
];
287 rdev
->gart
.pages
[p
] = pagelist
[i
];
288 if (rdev
->gart
.ptr
) {
289 page_base
= rdev
->gart
.pages_addr
[p
];
290 for (j
= 0; j
< (PAGE_SIZE
/ RADEON_GPU_PAGE_SIZE
); j
++, t
++) {
291 radeon_gart_set_page(rdev
, t
, page_base
);
292 page_base
+= RADEON_GPU_PAGE_SIZE
;
297 radeon_gart_tlb_flush(rdev
);
302 * radeon_gart_restore - bind all pages in the gart page table
304 * @rdev: radeon_device pointer
306 * Binds all pages in the gart page table (all asics).
307 * Used to rebuild the gart table on device startup or resume.
309 void radeon_gart_restore(struct radeon_device
*rdev
)
314 if (!rdev
->gart
.ptr
) {
317 for (i
= 0, t
= 0; i
< rdev
->gart
.num_cpu_pages
; i
++) {
318 page_base
= rdev
->gart
.pages_addr
[i
];
319 for (j
= 0; j
< (PAGE_SIZE
/ RADEON_GPU_PAGE_SIZE
); j
++, t
++) {
320 radeon_gart_set_page(rdev
, t
, page_base
);
321 page_base
+= RADEON_GPU_PAGE_SIZE
;
325 radeon_gart_tlb_flush(rdev
);
329 * radeon_gart_init - init the driver info for managing the gart
331 * @rdev: radeon_device pointer
333 * Allocate the dummy page and init the gart driver info (all asics).
334 * Returns 0 for success, error for failure.
336 int radeon_gart_init(struct radeon_device
*rdev
)
340 if (rdev
->gart
.pages
) {
343 /* We need PAGE_SIZE >= RADEON_GPU_PAGE_SIZE */
344 if (PAGE_SIZE
< RADEON_GPU_PAGE_SIZE
) {
345 DRM_ERROR("Page size is smaller than GPU page size!\n");
348 r
= radeon_dummy_page_init(rdev
);
351 /* Compute table size */
352 rdev
->gart
.num_cpu_pages
= rdev
->mc
.gtt_size
/ PAGE_SIZE
;
353 rdev
->gart
.num_gpu_pages
= rdev
->mc
.gtt_size
/ RADEON_GPU_PAGE_SIZE
;
354 DRM_INFO("GART: num cpu pages %u, num gpu pages %u\n",
355 rdev
->gart
.num_cpu_pages
, rdev
->gart
.num_gpu_pages
);
356 /* Allocate pages table */
357 rdev
->gart
.pages
= vzalloc(sizeof(void *) * rdev
->gart
.num_cpu_pages
);
358 if (rdev
->gart
.pages
== NULL
) {
359 radeon_gart_fini(rdev
);
362 rdev
->gart
.pages_addr
= vzalloc(sizeof(dma_addr_t
) *
363 rdev
->gart
.num_cpu_pages
);
364 if (rdev
->gart
.pages_addr
== NULL
) {
365 radeon_gart_fini(rdev
);
368 /* set GART entry to point to the dummy page by default */
369 for (i
= 0; i
< rdev
->gart
.num_cpu_pages
; i
++) {
370 rdev
->gart
.pages_addr
[i
] = rdev
->dummy_page
.addr
;
376 * radeon_gart_fini - tear down the driver info for managing the gart
378 * @rdev: radeon_device pointer
380 * Tear down the gart driver info and free the dummy page (all asics).
382 void radeon_gart_fini(struct radeon_device
*rdev
)
384 if (rdev
->gart
.pages
&& rdev
->gart
.pages_addr
&& rdev
->gart
.ready
) {
386 radeon_gart_unbind(rdev
, 0, rdev
->gart
.num_cpu_pages
);
388 rdev
->gart
.ready
= false;
389 vfree(rdev
->gart
.pages
);
390 vfree(rdev
->gart
.pages_addr
);
391 rdev
->gart
.pages
= NULL
;
392 rdev
->gart
.pages_addr
= NULL
;
394 radeon_dummy_page_fini(rdev
);
399 * GPUVM is similar to the legacy gart on older asics, however
400 * rather than there being a single global gart table
401 * for the entire GPU, there are multiple VM page tables active
402 * at any given time. The VM page tables can contain a mix
403 * vram pages and system memory pages and system memory pages
404 * can be mapped as snooped (cached system pages) or unsnooped
405 * (uncached system pages).
406 * Each VM has an ID associated with it and there is a page table
407 * associated with each VMID. When execting a command buffer,
408 * the kernel tells the the ring what VMID to use for that command
409 * buffer. VMIDs are allocated dynamically as commands are submitted.
410 * The userspace drivers maintain their own address space and the kernel
411 * sets up their pages tables accordingly when they submit their
412 * command buffers and a VMID is assigned.
413 * Cayman/Trinity support up to 8 active VMs at any given time;
420 * TODO bind a default page at vm initialization for default address
424 * radeon_vm_num_pde - return the number of page directory entries
426 * @rdev: radeon_device pointer
428 * Calculate the number of page directory entries (cayman+).
430 static unsigned radeon_vm_num_pdes(struct radeon_device
*rdev
)
432 return rdev
->vm_manager
.max_pfn
>> RADEON_VM_BLOCK_SIZE
;
436 * radeon_vm_directory_size - returns the size of the page directory in bytes
438 * @rdev: radeon_device pointer
440 * Calculate the size of the page directory in bytes (cayman+).
442 static unsigned radeon_vm_directory_size(struct radeon_device
*rdev
)
444 return RADEON_GPU_PAGE_ALIGN(radeon_vm_num_pdes(rdev
) * 8);
448 * radeon_vm_manager_init - init the vm manager
450 * @rdev: radeon_device pointer
452 * Init the vm manager (cayman+).
453 * Returns 0 for success, error for failure.
455 int radeon_vm_manager_init(struct radeon_device
*rdev
)
457 struct radeon_vm
*vm
;
458 struct radeon_bo_va
*bo_va
;
462 if (!rdev
->vm_manager
.enabled
) {
463 /* allocate enough for 2 full VM pts */
464 size
= radeon_vm_directory_size(rdev
);
465 size
+= rdev
->vm_manager
.max_pfn
* 8;
467 r
= radeon_sa_bo_manager_init(rdev
, &rdev
->vm_manager
.sa_manager
,
468 RADEON_GPU_PAGE_ALIGN(size
),
469 RADEON_VM_PTB_ALIGN_SIZE
,
470 RADEON_GEM_DOMAIN_VRAM
);
472 dev_err(rdev
->dev
, "failed to allocate vm bo (%dKB)\n",
473 (rdev
->vm_manager
.max_pfn
* 8) >> 10);
477 r
= radeon_asic_vm_init(rdev
);
481 rdev
->vm_manager
.enabled
= true;
483 r
= radeon_sa_bo_manager_start(rdev
, &rdev
->vm_manager
.sa_manager
);
488 /* restore page table */
489 list_for_each_entry(vm
, &rdev
->vm_manager
.lru_vm
, list
) {
490 if (vm
->page_directory
== NULL
)
493 list_for_each_entry(bo_va
, &vm
->va
, vm_list
) {
494 bo_va
->valid
= false;
501 * radeon_vm_free_pt - free the page table for a specific vm
503 * @rdev: radeon_device pointer
506 * Free the page table of a specific vm (cayman+).
508 * Global and local mutex must be lock!
510 static void radeon_vm_free_pt(struct radeon_device
*rdev
,
511 struct radeon_vm
*vm
)
513 struct radeon_bo_va
*bo_va
;
516 if (!vm
->page_directory
)
519 list_del_init(&vm
->list
);
520 radeon_sa_bo_free(rdev
, &vm
->page_directory
, vm
->fence
);
522 list_for_each_entry(bo_va
, &vm
->va
, vm_list
) {
523 bo_va
->valid
= false;
526 if (vm
->page_tables
== NULL
)
529 for (i
= 0; i
< radeon_vm_num_pdes(rdev
); i
++)
530 radeon_sa_bo_free(rdev
, &vm
->page_tables
[i
], vm
->fence
);
532 kfree(vm
->page_tables
);
536 * radeon_vm_manager_fini - tear down the vm manager
538 * @rdev: radeon_device pointer
540 * Tear down the VM manager (cayman+).
542 void radeon_vm_manager_fini(struct radeon_device
*rdev
)
544 struct radeon_vm
*vm
, *tmp
;
547 if (!rdev
->vm_manager
.enabled
)
550 mutex_lock(&rdev
->vm_manager
.lock
);
551 /* free all allocated page tables */
552 list_for_each_entry_safe(vm
, tmp
, &rdev
->vm_manager
.lru_vm
, list
) {
553 mutex_lock(&vm
->mutex
);
554 radeon_vm_free_pt(rdev
, vm
);
555 mutex_unlock(&vm
->mutex
);
557 for (i
= 0; i
< RADEON_NUM_VM
; ++i
) {
558 radeon_fence_unref(&rdev
->vm_manager
.active
[i
]);
560 radeon_asic_vm_fini(rdev
);
561 mutex_unlock(&rdev
->vm_manager
.lock
);
563 radeon_sa_bo_manager_suspend(rdev
, &rdev
->vm_manager
.sa_manager
);
564 radeon_sa_bo_manager_fini(rdev
, &rdev
->vm_manager
.sa_manager
);
565 rdev
->vm_manager
.enabled
= false;
569 * radeon_vm_evict - evict page table to make room for new one
571 * @rdev: radeon_device pointer
572 * @vm: VM we want to allocate something for
574 * Evict a VM from the lru, making sure that it isn't @vm. (cayman+).
575 * Returns 0 for success, -ENOMEM for failure.
577 * Global and local mutex must be locked!
579 static int radeon_vm_evict(struct radeon_device
*rdev
, struct radeon_vm
*vm
)
581 struct radeon_vm
*vm_evict
;
583 if (list_empty(&rdev
->vm_manager
.lru_vm
))
586 vm_evict
= list_first_entry(&rdev
->vm_manager
.lru_vm
,
587 struct radeon_vm
, list
);
591 mutex_lock(&vm_evict
->mutex
);
592 radeon_vm_free_pt(rdev
, vm_evict
);
593 mutex_unlock(&vm_evict
->mutex
);
598 * radeon_vm_alloc_pt - allocates a page table for a VM
600 * @rdev: radeon_device pointer
603 * Allocate a page table for the requested vm (cayman+).
604 * Returns 0 for success, error for failure.
606 * Global and local mutex must be locked!
608 int radeon_vm_alloc_pt(struct radeon_device
*rdev
, struct radeon_vm
*vm
)
610 unsigned pd_size
, pts_size
;
618 if (vm
->page_directory
!= NULL
) {
623 pd_size
= radeon_vm_directory_size(rdev
);
624 r
= radeon_sa_bo_new(rdev
, &rdev
->vm_manager
.sa_manager
,
625 &vm
->page_directory
, pd_size
,
626 RADEON_VM_PTB_ALIGN_SIZE
, false);
628 r
= radeon_vm_evict(rdev
, vm
);
637 vm
->pd_gpu_addr
= radeon_sa_bo_gpu_addr(vm
->page_directory
);
639 /* Initially clear the page directory */
640 pd_addr
= radeon_sa_bo_cpu_addr(vm
->page_directory
);
641 memset(pd_addr
, 0, pd_size
);
643 pts_size
= radeon_vm_num_pdes(rdev
) * sizeof(struct radeon_sa_bo
*);
644 vm
->page_tables
= kzalloc(pts_size
, GFP_KERNEL
);
646 if (vm
->page_tables
== NULL
) {
647 DRM_ERROR("Cannot allocate memory for page table array\n");
648 radeon_sa_bo_free(rdev
, &vm
->page_directory
, vm
->fence
);
656 * radeon_vm_add_to_lru - add VMs page table to LRU list
658 * @rdev: radeon_device pointer
659 * @vm: vm to add to LRU
661 * Add the allocated page table to the LRU list (cayman+).
663 * Global mutex must be locked!
665 void radeon_vm_add_to_lru(struct radeon_device
*rdev
, struct radeon_vm
*vm
)
667 list_del_init(&vm
->list
);
668 list_add_tail(&vm
->list
, &rdev
->vm_manager
.lru_vm
);
672 * radeon_vm_grab_id - allocate the next free VMID
674 * @rdev: radeon_device pointer
675 * @vm: vm to allocate id for
676 * @ring: ring we want to submit job to
678 * Allocate an id for the vm (cayman+).
679 * Returns the fence we need to sync to (if any).
681 * Global and local mutex must be locked!
683 struct radeon_fence
*radeon_vm_grab_id(struct radeon_device
*rdev
,
684 struct radeon_vm
*vm
, int ring
)
686 struct radeon_fence
*best
[RADEON_NUM_RINGS
] = {};
687 unsigned choices
[2] = {};
690 /* check if the id is still valid */
691 if (vm
->fence
&& vm
->fence
== rdev
->vm_manager
.active
[vm
->id
])
694 /* we definately need to flush */
695 radeon_fence_unref(&vm
->last_flush
);
697 /* skip over VMID 0, since it is the system VM */
698 for (i
= 1; i
< rdev
->vm_manager
.nvm
; ++i
) {
699 struct radeon_fence
*fence
= rdev
->vm_manager
.active
[i
];
702 /* found a free one */
707 if (radeon_fence_is_earlier(fence
, best
[fence
->ring
])) {
708 best
[fence
->ring
] = fence
;
709 choices
[fence
->ring
== ring
? 0 : 1] = i
;
713 for (i
= 0; i
< 2; ++i
) {
716 return rdev
->vm_manager
.active
[choices
[i
]];
720 /* should never happen */
726 * radeon_vm_fence - remember fence for vm
728 * @rdev: radeon_device pointer
729 * @vm: vm we want to fence
730 * @fence: fence to remember
732 * Fence the vm (cayman+).
733 * Set the fence used to protect page table and id.
735 * Global and local mutex must be locked!
737 void radeon_vm_fence(struct radeon_device
*rdev
,
738 struct radeon_vm
*vm
,
739 struct radeon_fence
*fence
)
741 radeon_fence_unref(&rdev
->vm_manager
.active
[vm
->id
]);
742 rdev
->vm_manager
.active
[vm
->id
] = radeon_fence_ref(fence
);
744 radeon_fence_unref(&vm
->fence
);
745 vm
->fence
= radeon_fence_ref(fence
);
749 * radeon_vm_bo_find - find the bo_va for a specific vm & bo
752 * @bo: requested buffer object
754 * Find @bo inside the requested vm (cayman+).
755 * Search inside the @bos vm list for the requested vm
756 * Returns the found bo_va or NULL if none is found
758 * Object has to be reserved!
760 struct radeon_bo_va
*radeon_vm_bo_find(struct radeon_vm
*vm
,
761 struct radeon_bo
*bo
)
763 struct radeon_bo_va
*bo_va
;
765 list_for_each_entry(bo_va
, &bo
->va
, bo_list
) {
766 if (bo_va
->vm
== vm
) {
774 * radeon_vm_bo_add - add a bo to a specific vm
776 * @rdev: radeon_device pointer
778 * @bo: radeon buffer object
780 * Add @bo into the requested vm (cayman+).
781 * Add @bo to the list of bos associated with the vm
782 * Returns newly added bo_va or NULL for failure
784 * Object has to be reserved!
786 struct radeon_bo_va
*radeon_vm_bo_add(struct radeon_device
*rdev
,
787 struct radeon_vm
*vm
,
788 struct radeon_bo
*bo
)
790 struct radeon_bo_va
*bo_va
;
792 bo_va
= kzalloc(sizeof(struct radeon_bo_va
), GFP_KERNEL
);
801 bo_va
->valid
= false;
802 bo_va
->ref_count
= 1;
803 INIT_LIST_HEAD(&bo_va
->bo_list
);
804 INIT_LIST_HEAD(&bo_va
->vm_list
);
806 mutex_lock(&vm
->mutex
);
807 list_add(&bo_va
->vm_list
, &vm
->va
);
808 list_add_tail(&bo_va
->bo_list
, &bo
->va
);
809 mutex_unlock(&vm
->mutex
);
815 * radeon_vm_bo_set_addr - set bos virtual address inside a vm
817 * @rdev: radeon_device pointer
818 * @bo_va: bo_va to store the address
819 * @soffset: requested offset of the buffer in the VM address space
820 * @flags: attributes of pages (read/write/valid/etc.)
822 * Set offset of @bo_va (cayman+).
823 * Validate and set the offset requested within the vm address space.
824 * Returns 0 for success, error for failure.
826 * Object has to be reserved!
828 int radeon_vm_bo_set_addr(struct radeon_device
*rdev
,
829 struct radeon_bo_va
*bo_va
,
833 uint64_t size
= radeon_bo_size(bo_va
->bo
);
834 uint64_t eoffset
, last_offset
= 0;
835 struct radeon_vm
*vm
= bo_va
->vm
;
836 struct radeon_bo_va
*tmp
;
837 struct list_head
*head
;
841 /* make sure object fit at this offset */
842 eoffset
= soffset
+ size
;
843 if (soffset
>= eoffset
) {
847 last_pfn
= eoffset
/ RADEON_GPU_PAGE_SIZE
;
848 if (last_pfn
> rdev
->vm_manager
.max_pfn
) {
849 dev_err(rdev
->dev
, "va above limit (0x%08X > 0x%08X)\n",
850 last_pfn
, rdev
->vm_manager
.max_pfn
);
855 eoffset
= last_pfn
= 0;
858 mutex_lock(&vm
->mutex
);
861 list_for_each_entry(tmp
, &vm
->va
, vm_list
) {
863 /* skip over currently modified bo */
867 if (soffset
>= last_offset
&& eoffset
<= tmp
->soffset
) {
868 /* bo can be added before this one */
871 if (eoffset
> tmp
->soffset
&& soffset
< tmp
->eoffset
) {
872 /* bo and tmp overlap, invalid offset */
873 dev_err(rdev
->dev
, "bo %p va 0x%08X conflict with (bo %p 0x%08X 0x%08X)\n",
874 bo_va
->bo
, (unsigned)bo_va
->soffset
, tmp
->bo
,
875 (unsigned)tmp
->soffset
, (unsigned)tmp
->eoffset
);
876 mutex_unlock(&vm
->mutex
);
879 last_offset
= tmp
->eoffset
;
880 head
= &tmp
->vm_list
;
883 bo_va
->soffset
= soffset
;
884 bo_va
->eoffset
= eoffset
;
885 bo_va
->flags
= flags
;
886 bo_va
->valid
= false;
887 list_move(&bo_va
->vm_list
, head
);
889 mutex_unlock(&vm
->mutex
);
894 * radeon_vm_map_gart - get the physical address of a gart page
896 * @rdev: radeon_device pointer
897 * @addr: the unmapped addr
899 * Look up the physical address of the page that the pte resolves
901 * Returns the physical address of the page.
903 uint64_t radeon_vm_map_gart(struct radeon_device
*rdev
, uint64_t addr
)
907 /* page table offset */
908 result
= rdev
->gart
.pages_addr
[addr
>> PAGE_SHIFT
];
910 /* in case cpu page size != gpu page size*/
911 result
|= addr
& (~PAGE_MASK
);
917 * radeon_vm_update_pdes - make sure that page directory is valid
919 * @rdev: radeon_device pointer
921 * @start: start of GPU address range
922 * @end: end of GPU address range
924 * Allocates new page tables if necessary
925 * and updates the page directory (cayman+).
926 * Returns 0 for success, error for failure.
928 * Global and local mutex must be locked!
930 static int radeon_vm_update_pdes(struct radeon_device
*rdev
,
931 struct radeon_vm
*vm
,
932 struct radeon_ib
*ib
,
933 uint64_t start
, uint64_t end
)
935 static const uint32_t incr
= RADEON_VM_PTE_COUNT
* 8;
937 uint64_t last_pde
= ~0, last_pt
= ~0;
942 start
= (start
/ RADEON_GPU_PAGE_SIZE
) >> RADEON_VM_BLOCK_SIZE
;
943 end
= (end
/ RADEON_GPU_PAGE_SIZE
) >> RADEON_VM_BLOCK_SIZE
;
945 /* walk over the address space and update the page directory */
946 for (pt_idx
= start
; pt_idx
<= end
; ++pt_idx
) {
949 if (vm
->page_tables
[pt_idx
])
953 r
= radeon_sa_bo_new(rdev
, &rdev
->vm_manager
.sa_manager
,
954 &vm
->page_tables
[pt_idx
],
955 RADEON_VM_PTE_COUNT
* 8,
956 RADEON_GPU_PAGE_SIZE
, false);
959 r
= radeon_vm_evict(rdev
, vm
);
967 pde
= vm
->pd_gpu_addr
+ pt_idx
* 8;
969 pt
= radeon_sa_bo_gpu_addr(vm
->page_tables
[pt_idx
]);
971 if (((last_pde
+ 8 * count
) != pde
) ||
972 ((last_pt
+ incr
* count
) != pt
)) {
975 radeon_asic_vm_set_page(rdev
, ib
, last_pde
,
976 last_pt
, count
, incr
,
977 RADEON_VM_PAGE_VALID
);
989 radeon_asic_vm_set_page(rdev
, ib
, last_pde
, last_pt
, count
,
990 incr
, RADEON_VM_PAGE_VALID
);
998 * radeon_vm_update_ptes - make sure that page tables are valid
1000 * @rdev: radeon_device pointer
1002 * @start: start of GPU address range
1003 * @end: end of GPU address range
1004 * @dst: destination address to map to
1005 * @flags: mapping flags
1007 * Update the page tables in the range @start - @end (cayman+).
1009 * Global and local mutex must be locked!
1011 static void radeon_vm_update_ptes(struct radeon_device
*rdev
,
1012 struct radeon_vm
*vm
,
1013 struct radeon_ib
*ib
,
1014 uint64_t start
, uint64_t end
,
1015 uint64_t dst
, uint32_t flags
)
1017 static const uint64_t mask
= RADEON_VM_PTE_COUNT
- 1;
1019 uint64_t last_pte
= ~0, last_dst
= ~0;
1023 start
= start
/ RADEON_GPU_PAGE_SIZE
;
1024 end
= end
/ RADEON_GPU_PAGE_SIZE
;
1026 /* walk over the address space and update the page tables */
1027 for (addr
= start
; addr
< end
; ) {
1028 uint64_t pt_idx
= addr
>> RADEON_VM_BLOCK_SIZE
;
1032 if ((addr
& ~mask
) == (end
& ~mask
))
1035 nptes
= RADEON_VM_PTE_COUNT
- (addr
& mask
);
1037 pte
= radeon_sa_bo_gpu_addr(vm
->page_tables
[pt_idx
]);
1038 pte
+= (addr
& mask
) * 8;
1040 if ((last_pte
+ 8 * count
) != pte
) {
1043 radeon_asic_vm_set_page(rdev
, ib
, last_pte
,
1045 RADEON_GPU_PAGE_SIZE
,
1057 dst
+= nptes
* RADEON_GPU_PAGE_SIZE
;
1061 radeon_asic_vm_set_page(rdev
, ib
, last_pte
,
1063 RADEON_GPU_PAGE_SIZE
, flags
);
1068 * radeon_vm_bo_update_pte - map a bo into the vm page table
1070 * @rdev: radeon_device pointer
1072 * @bo: radeon buffer object
1075 * Fill in the page table entries for @bo (cayman+).
1076 * Returns 0 for success, -EINVAL for failure.
1078 * Object have to be reserved & global and local mutex must be locked!
1080 int radeon_vm_bo_update_pte(struct radeon_device
*rdev
,
1081 struct radeon_vm
*vm
,
1082 struct radeon_bo
*bo
,
1083 struct ttm_mem_reg
*mem
)
1085 unsigned ridx
= rdev
->asic
->vm
.pt_ring_index
;
1086 struct radeon_ib ib
;
1087 struct radeon_bo_va
*bo_va
;
1088 unsigned nptes
, npdes
, ndw
;
1092 /* nothing to do if vm isn't bound */
1093 if (vm
->page_directory
== NULL
)
1096 bo_va
= radeon_vm_bo_find(vm
, bo
);
1097 if (bo_va
== NULL
) {
1098 dev_err(rdev
->dev
, "bo %p not in vm %p\n", bo
, vm
);
1102 if (!bo_va
->soffset
) {
1103 dev_err(rdev
->dev
, "bo %p don't has a mapping in vm %p\n",
1108 if ((bo_va
->valid
&& mem
) || (!bo_va
->valid
&& mem
== NULL
))
1111 bo_va
->flags
&= ~RADEON_VM_PAGE_VALID
;
1112 bo_va
->flags
&= ~RADEON_VM_PAGE_SYSTEM
;
1114 addr
= mem
->start
<< PAGE_SHIFT
;
1115 if (mem
->mem_type
!= TTM_PL_SYSTEM
) {
1116 bo_va
->flags
|= RADEON_VM_PAGE_VALID
;
1117 bo_va
->valid
= true;
1119 if (mem
->mem_type
== TTM_PL_TT
) {
1120 bo_va
->flags
|= RADEON_VM_PAGE_SYSTEM
;
1122 addr
+= rdev
->vm_manager
.vram_base_offset
;
1126 bo_va
->valid
= false;
1129 nptes
= radeon_bo_ngpu_pages(bo
);
1131 /* assume two extra pdes in case the mapping overlaps the borders */
1132 npdes
= (nptes
>> RADEON_VM_BLOCK_SIZE
) + 2;
1137 if (RADEON_VM_BLOCK_SIZE
> 11)
1138 /* reserve space for one header for every 2k dwords */
1139 ndw
+= (nptes
>> 11) * 4;
1141 /* reserve space for one header for
1142 every (1 << BLOCK_SIZE) entries */
1143 ndw
+= (nptes
>> RADEON_VM_BLOCK_SIZE
) * 4;
1145 /* reserve space for pte addresses */
1148 /* reserve space for one header for every 2k dwords */
1149 ndw
+= (npdes
>> 11) * 4;
1151 /* reserve space for pde addresses */
1154 /* update too big for an IB */
1158 r
= radeon_ib_get(rdev
, ridx
, &ib
, NULL
, ndw
* 4);
1163 r
= radeon_vm_update_pdes(rdev
, vm
, &ib
, bo_va
->soffset
, bo_va
->eoffset
);
1165 radeon_ib_free(rdev
, &ib
);
1169 radeon_vm_update_ptes(rdev
, vm
, &ib
, bo_va
->soffset
, bo_va
->eoffset
,
1170 addr
, bo_va
->flags
);
1172 radeon_ib_sync_to(&ib
, vm
->fence
);
1173 r
= radeon_ib_schedule(rdev
, &ib
, NULL
);
1175 radeon_ib_free(rdev
, &ib
);
1178 radeon_fence_unref(&vm
->fence
);
1179 vm
->fence
= radeon_fence_ref(ib
.fence
);
1180 radeon_ib_free(rdev
, &ib
);
1181 radeon_fence_unref(&vm
->last_flush
);
1187 * radeon_vm_bo_rmv - remove a bo to a specific vm
1189 * @rdev: radeon_device pointer
1190 * @bo_va: requested bo_va
1192 * Remove @bo_va->bo from the requested vm (cayman+).
1193 * Remove @bo_va->bo from the list of bos associated with the bo_va->vm and
1194 * remove the ptes for @bo_va in the page table.
1195 * Returns 0 for success.
1197 * Object have to be reserved!
1199 int radeon_vm_bo_rmv(struct radeon_device
*rdev
,
1200 struct radeon_bo_va
*bo_va
)
1204 mutex_lock(&rdev
->vm_manager
.lock
);
1205 mutex_lock(&bo_va
->vm
->mutex
);
1206 if (bo_va
->soffset
) {
1207 r
= radeon_vm_bo_update_pte(rdev
, bo_va
->vm
, bo_va
->bo
, NULL
);
1209 mutex_unlock(&rdev
->vm_manager
.lock
);
1210 list_del(&bo_va
->vm_list
);
1211 mutex_unlock(&bo_va
->vm
->mutex
);
1212 list_del(&bo_va
->bo_list
);
1219 * radeon_vm_bo_invalidate - mark the bo as invalid
1221 * @rdev: radeon_device pointer
1223 * @bo: radeon buffer object
1225 * Mark @bo as invalid (cayman+).
1227 void radeon_vm_bo_invalidate(struct radeon_device
*rdev
,
1228 struct radeon_bo
*bo
)
1230 struct radeon_bo_va
*bo_va
;
1232 list_for_each_entry(bo_va
, &bo
->va
, bo_list
) {
1233 bo_va
->valid
= false;
1238 * radeon_vm_init - initialize a vm instance
1240 * @rdev: radeon_device pointer
1243 * Init @vm fields (cayman+).
1245 void radeon_vm_init(struct radeon_device
*rdev
, struct radeon_vm
*vm
)
1249 mutex_init(&vm
->mutex
);
1250 INIT_LIST_HEAD(&vm
->list
);
1251 INIT_LIST_HEAD(&vm
->va
);
1255 * radeon_vm_fini - tear down a vm instance
1257 * @rdev: radeon_device pointer
1260 * Tear down @vm (cayman+).
1261 * Unbind the VM and remove all bos from the vm bo list
1263 void radeon_vm_fini(struct radeon_device
*rdev
, struct radeon_vm
*vm
)
1265 struct radeon_bo_va
*bo_va
, *tmp
;
1268 mutex_lock(&rdev
->vm_manager
.lock
);
1269 mutex_lock(&vm
->mutex
);
1270 radeon_vm_free_pt(rdev
, vm
);
1271 mutex_unlock(&rdev
->vm_manager
.lock
);
1273 if (!list_empty(&vm
->va
)) {
1274 dev_err(rdev
->dev
, "still active bo inside vm\n");
1276 list_for_each_entry_safe(bo_va
, tmp
, &vm
->va
, vm_list
) {
1277 list_del_init(&bo_va
->vm_list
);
1278 r
= radeon_bo_reserve(bo_va
->bo
, false);
1280 list_del_init(&bo_va
->bo_list
);
1281 radeon_bo_unreserve(bo_va
->bo
);
1285 radeon_fence_unref(&vm
->fence
);
1286 radeon_fence_unref(&vm
->last_flush
);
1287 mutex_unlock(&vm
->mutex
);