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 <asm/set_memory.h>
37 * The GART (Graphics Aperture Remapping Table) is an aperture
38 * in the GPU's address space. System pages can be mapped into
39 * the aperture and look like contiguous pages from the GPU's
40 * perspective. A page table maps the pages in the aperture
41 * to the actual backing pages in system memory.
43 * Radeon GPUs support both an internal GART, as described above,
44 * and AGP. AGP works similarly, but the GART table is configured
45 * and maintained by the northbridge rather than the driver.
46 * Radeon hw has a separate AGP aperture that is programmed to
47 * point to the AGP aperture provided by the northbridge and the
48 * requests are passed through to the northbridge aperture.
49 * Both AGP and internal GART can be used at the same time, however
50 * that is not currently supported by the driver.
52 * This file handles the common internal GART management.
56 * Common GART table functions.
60 * amdgpu_gart_table_vram_alloc - allocate vram for gart page table
62 * @adev: amdgpu_device pointer
64 * Allocate video memory for GART page table
65 * (pcie r4xx, r5xx+). These asics require the
66 * gart table to be in video memory.
67 * Returns 0 for success, error for failure.
69 int amdgpu_gart_table_vram_alloc(struct amdgpu_device *adev)
73 if (adev->gart.robj == NULL) {
74 r = amdgpu_bo_create(adev, adev->gart.table_size,
75 PAGE_SIZE, true, AMDGPU_GEM_DOMAIN_VRAM,
76 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
77 AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS,
78 NULL, NULL, 0, &adev->gart.robj);
87 * amdgpu_gart_table_vram_pin - pin gart page table in vram
89 * @adev: amdgpu_device pointer
91 * Pin the GART page table in vram so it will not be moved
92 * by the memory manager (pcie r4xx, r5xx+). These asics require the
93 * gart table to be in video memory.
94 * Returns 0 for success, error for failure.
96 int amdgpu_gart_table_vram_pin(struct amdgpu_device *adev)
101 r = amdgpu_bo_reserve(adev->gart.robj, false);
102 if (unlikely(r != 0))
104 r = amdgpu_bo_pin(adev->gart.robj,
105 AMDGPU_GEM_DOMAIN_VRAM, &gpu_addr);
107 amdgpu_bo_unreserve(adev->gart.robj);
110 r = amdgpu_bo_kmap(adev->gart.robj, &adev->gart.ptr);
112 amdgpu_bo_unpin(adev->gart.robj);
113 amdgpu_bo_unreserve(adev->gart.robj);
114 adev->gart.table_addr = gpu_addr;
119 * amdgpu_gart_table_vram_unpin - unpin gart page table in vram
121 * @adev: amdgpu_device pointer
123 * Unpin the GART page table in vram (pcie r4xx, r5xx+).
124 * These asics require the gart table to be in video memory.
126 void amdgpu_gart_table_vram_unpin(struct amdgpu_device *adev)
130 if (adev->gart.robj == NULL) {
133 r = amdgpu_bo_reserve(adev->gart.robj, true);
134 if (likely(r == 0)) {
135 amdgpu_bo_kunmap(adev->gart.robj);
136 amdgpu_bo_unpin(adev->gart.robj);
137 amdgpu_bo_unreserve(adev->gart.robj);
138 adev->gart.ptr = NULL;
143 * amdgpu_gart_table_vram_free - free gart page table vram
145 * @adev: amdgpu_device pointer
147 * Free the video memory used for the GART page table
148 * (pcie r4xx, r5xx+). These asics require the gart table to
149 * be in video memory.
151 void amdgpu_gart_table_vram_free(struct amdgpu_device *adev)
153 if (adev->gart.robj == NULL) {
156 amdgpu_bo_unref(&adev->gart.robj);
160 * Common gart functions.
163 * amdgpu_gart_unbind - unbind pages from the gart page table
165 * @adev: amdgpu_device pointer
166 * @offset: offset into the GPU's gart aperture
167 * @pages: number of pages to unbind
169 * Unbinds the requested pages from the gart page table and
170 * replaces them with the dummy page (all asics).
171 * Returns 0 for success, -EINVAL for failure.
173 int amdgpu_gart_unbind(struct amdgpu_device *adev, uint64_t offset,
180 /* Starting from VEGA10, system bit must be 0 to mean invalid. */
183 if (!adev->gart.ready) {
184 WARN(1, "trying to unbind memory from uninitialized GART !\n");
188 t = offset / AMDGPU_GPU_PAGE_SIZE;
189 p = t / (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE);
190 for (i = 0; i < pages; i++, p++) {
191 #ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
192 adev->gart.pages[p] = NULL;
194 page_base = adev->dummy_page.addr;
198 for (j = 0; j < (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE); j++, t++) {
199 amdgpu_gart_set_pte_pde(adev, adev->gart.ptr,
200 t, page_base, flags);
201 page_base += AMDGPU_GPU_PAGE_SIZE;
205 amdgpu_gart_flush_gpu_tlb(adev, 0);
210 * amdgpu_gart_map - map dma_addresses into GART entries
212 * @adev: amdgpu_device pointer
213 * @offset: offset into the GPU's gart aperture
214 * @pages: number of pages to bind
215 * @dma_addr: DMA addresses of pages
217 * Map the dma_addresses into GART entries (all asics).
218 * Returns 0 for success, -EINVAL for failure.
220 int amdgpu_gart_map(struct amdgpu_device *adev, uint64_t offset,
221 int pages, dma_addr_t *dma_addr, uint64_t flags,
227 if (!adev->gart.ready) {
228 WARN(1, "trying to bind memory to uninitialized GART !\n");
232 t = offset / AMDGPU_GPU_PAGE_SIZE;
234 for (i = 0; i < pages; i++) {
235 page_base = dma_addr[i];
236 for (j = 0; j < (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE); j++, t++) {
237 amdgpu_gart_set_pte_pde(adev, dst, t, page_base, flags);
238 page_base += AMDGPU_GPU_PAGE_SIZE;
245 * amdgpu_gart_bind - bind pages into the gart page table
247 * @adev: amdgpu_device pointer
248 * @offset: offset into the GPU's gart aperture
249 * @pages: number of pages to bind
250 * @pagelist: pages to bind
251 * @dma_addr: DMA addresses of pages
253 * Binds the requested pages to the gart page table
255 * Returns 0 for success, -EINVAL for failure.
257 int amdgpu_gart_bind(struct amdgpu_device *adev, uint64_t offset,
258 int pages, struct page **pagelist, dma_addr_t *dma_addr,
261 #ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
266 if (!adev->gart.ready) {
267 WARN(1, "trying to bind memory to uninitialized GART !\n");
271 #ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
272 t = offset / AMDGPU_GPU_PAGE_SIZE;
273 p = t / (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE);
274 for (i = 0; i < pages; i++, p++)
275 adev->gart.pages[p] = pagelist[i];
281 r = amdgpu_gart_map(adev, offset, pages, dma_addr, flags,
287 amdgpu_gart_flush_gpu_tlb(adev, 0);
292 * amdgpu_gart_init - init the driver info for managing the gart
294 * @adev: amdgpu_device pointer
296 * Allocate the dummy page and init the gart driver info (all asics).
297 * Returns 0 for success, error for failure.
299 int amdgpu_gart_init(struct amdgpu_device *adev)
303 if (adev->dummy_page.page)
306 /* We need PAGE_SIZE >= AMDGPU_GPU_PAGE_SIZE */
307 if (PAGE_SIZE < AMDGPU_GPU_PAGE_SIZE) {
308 DRM_ERROR("Page size is smaller than GPU page size!\n");
311 r = amdgpu_dummy_page_init(adev);
314 /* Compute table size */
315 adev->gart.num_cpu_pages = adev->mc.gart_size / PAGE_SIZE;
316 adev->gart.num_gpu_pages = adev->mc.gart_size / AMDGPU_GPU_PAGE_SIZE;
317 DRM_INFO("GART: num cpu pages %u, num gpu pages %u\n",
318 adev->gart.num_cpu_pages, adev->gart.num_gpu_pages);
320 #ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
321 /* Allocate pages table */
322 adev->gart.pages = vzalloc(sizeof(void *) * adev->gart.num_cpu_pages);
323 if (adev->gart.pages == NULL)
331 * amdgpu_gart_fini - tear down the driver info for managing the gart
333 * @adev: amdgpu_device pointer
335 * Tear down the gart driver info and free the dummy page (all asics).
337 void amdgpu_gart_fini(struct amdgpu_device *adev)
339 #ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
340 vfree(adev->gart.pages);
341 adev->gart.pages = NULL;
343 amdgpu_dummy_page_fini(adev);