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_set_defaults - set the default gart_size
62 * @adev: amdgpu_device pointer
64 * Set the default gart_size based on parameters and available VRAM.
66 void amdgpu_gart_set_defaults(struct amdgpu_device *adev)
68 /* unless the user had overridden it, set the gart
69 * size equal to the 1024 or vram, whichever is larger.
71 if (amdgpu_gart_size == -1)
72 adev->mc.gart_size = max((AMDGPU_DEFAULT_GTT_SIZE_MB << 20),
73 adev->mc.mc_vram_size);
75 adev->mc.gart_size = (uint64_t)amdgpu_gart_size << 20;
79 * amdgpu_gart_table_ram_alloc - allocate system ram for gart page table
81 * @adev: amdgpu_device pointer
83 * Allocate system memory for GART page table
84 * (r1xx-r3xx, non-pcie r4xx, rs400). These asics require the
85 * gart table to be in system memory.
86 * Returns 0 for success, -ENOMEM for failure.
88 int amdgpu_gart_table_ram_alloc(struct amdgpu_device *adev)
92 ptr = pci_alloc_consistent(adev->pdev, adev->gart.table_size,
93 &adev->gart.table_addr);
99 set_memory_uc((unsigned long)ptr,
100 adev->gart.table_size >> PAGE_SHIFT);
103 adev->gart.ptr = ptr;
104 memset((void *)adev->gart.ptr, 0, adev->gart.table_size);
109 * amdgpu_gart_table_ram_free - free system ram for gart page table
111 * @adev: amdgpu_device pointer
113 * Free system memory for GART page table
114 * (r1xx-r3xx, non-pcie r4xx, rs400). These asics require the
115 * gart table to be in system memory.
117 void amdgpu_gart_table_ram_free(struct amdgpu_device *adev)
119 if (adev->gart.ptr == NULL) {
124 set_memory_wb((unsigned long)adev->gart.ptr,
125 adev->gart.table_size >> PAGE_SHIFT);
128 pci_free_consistent(adev->pdev, adev->gart.table_size,
129 (void *)adev->gart.ptr,
130 adev->gart.table_addr);
131 adev->gart.ptr = NULL;
132 adev->gart.table_addr = 0;
136 * amdgpu_gart_table_vram_alloc - allocate vram for gart page table
138 * @adev: amdgpu_device pointer
140 * Allocate video memory for GART page table
141 * (pcie r4xx, r5xx+). These asics require the
142 * gart table to be in video memory.
143 * Returns 0 for success, error for failure.
145 int amdgpu_gart_table_vram_alloc(struct amdgpu_device *adev)
149 if (adev->gart.robj == NULL) {
150 r = amdgpu_bo_create(adev, adev->gart.table_size,
151 PAGE_SIZE, true, AMDGPU_GEM_DOMAIN_VRAM,
152 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
153 AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS,
154 NULL, NULL, &adev->gart.robj);
163 * amdgpu_gart_table_vram_pin - pin gart page table in vram
165 * @adev: amdgpu_device pointer
167 * Pin the GART page table in vram so it will not be moved
168 * by the memory manager (pcie r4xx, r5xx+). These asics require the
169 * gart table to be in video memory.
170 * Returns 0 for success, error for failure.
172 int amdgpu_gart_table_vram_pin(struct amdgpu_device *adev)
177 r = amdgpu_bo_reserve(adev->gart.robj, false);
178 if (unlikely(r != 0))
180 r = amdgpu_bo_pin(adev->gart.robj,
181 AMDGPU_GEM_DOMAIN_VRAM, &gpu_addr);
183 amdgpu_bo_unreserve(adev->gart.robj);
186 r = amdgpu_bo_kmap(adev->gart.robj, &adev->gart.ptr);
188 amdgpu_bo_unpin(adev->gart.robj);
189 amdgpu_bo_unreserve(adev->gart.robj);
190 adev->gart.table_addr = gpu_addr;
195 * amdgpu_gart_table_vram_unpin - unpin gart page table in vram
197 * @adev: amdgpu_device pointer
199 * Unpin the GART page table in vram (pcie r4xx, r5xx+).
200 * These asics require the gart table to be in video memory.
202 void amdgpu_gart_table_vram_unpin(struct amdgpu_device *adev)
206 if (adev->gart.robj == NULL) {
209 r = amdgpu_bo_reserve(adev->gart.robj, true);
210 if (likely(r == 0)) {
211 amdgpu_bo_kunmap(adev->gart.robj);
212 amdgpu_bo_unpin(adev->gart.robj);
213 amdgpu_bo_unreserve(adev->gart.robj);
214 adev->gart.ptr = NULL;
219 * amdgpu_gart_table_vram_free - free gart page table vram
221 * @adev: amdgpu_device pointer
223 * Free the video memory used for the GART page table
224 * (pcie r4xx, r5xx+). These asics require the gart table to
225 * be in video memory.
227 void amdgpu_gart_table_vram_free(struct amdgpu_device *adev)
229 if (adev->gart.robj == NULL) {
232 amdgpu_bo_unref(&adev->gart.robj);
236 * Common gart functions.
239 * amdgpu_gart_unbind - unbind pages from the gart page table
241 * @adev: amdgpu_device pointer
242 * @offset: offset into the GPU's gart aperture
243 * @pages: number of pages to unbind
245 * Unbinds the requested pages from the gart page table and
246 * replaces them with the dummy page (all asics).
247 * Returns 0 for success, -EINVAL for failure.
249 int amdgpu_gart_unbind(struct amdgpu_device *adev, uint64_t offset,
256 /* Starting from VEGA10, system bit must be 0 to mean invalid. */
259 if (!adev->gart.ready) {
260 WARN(1, "trying to unbind memory from uninitialized GART !\n");
264 t = offset / AMDGPU_GPU_PAGE_SIZE;
265 p = t / (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE);
266 for (i = 0; i < pages; i++, p++) {
267 #ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
268 adev->gart.pages[p] = NULL;
270 page_base = adev->dummy_page.addr;
274 for (j = 0; j < (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE); j++, t++) {
275 amdgpu_gart_set_pte_pde(adev, adev->gart.ptr,
276 t, page_base, flags);
277 page_base += AMDGPU_GPU_PAGE_SIZE;
281 amdgpu_gart_flush_gpu_tlb(adev, 0);
286 * amdgpu_gart_map - map dma_addresses into GART entries
288 * @adev: amdgpu_device pointer
289 * @offset: offset into the GPU's gart aperture
290 * @pages: number of pages to bind
291 * @dma_addr: DMA addresses of pages
293 * Map the dma_addresses into GART entries (all asics).
294 * Returns 0 for success, -EINVAL for failure.
296 int amdgpu_gart_map(struct amdgpu_device *adev, uint64_t offset,
297 int pages, dma_addr_t *dma_addr, uint64_t flags,
303 if (!adev->gart.ready) {
304 WARN(1, "trying to bind memory to uninitialized GART !\n");
308 t = offset / AMDGPU_GPU_PAGE_SIZE;
310 for (i = 0; i < pages; i++) {
311 page_base = dma_addr[i];
312 for (j = 0; j < (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE); j++, t++) {
313 amdgpu_gart_set_pte_pde(adev, dst, t, page_base, flags);
314 page_base += AMDGPU_GPU_PAGE_SIZE;
321 * amdgpu_gart_bind - bind pages into the gart page table
323 * @adev: amdgpu_device pointer
324 * @offset: offset into the GPU's gart aperture
325 * @pages: number of pages to bind
326 * @pagelist: pages to bind
327 * @dma_addr: DMA addresses of pages
329 * Binds the requested pages to the gart page table
331 * Returns 0 for success, -EINVAL for failure.
333 int amdgpu_gart_bind(struct amdgpu_device *adev, uint64_t offset,
334 int pages, struct page **pagelist, dma_addr_t *dma_addr,
337 #ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
342 if (!adev->gart.ready) {
343 WARN(1, "trying to bind memory to uninitialized GART !\n");
347 #ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
348 t = offset / AMDGPU_GPU_PAGE_SIZE;
349 p = t / (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE);
350 for (i = 0; i < pages; i++, p++)
351 adev->gart.pages[p] = pagelist[i];
354 if (adev->gart.ptr) {
355 r = amdgpu_gart_map(adev, offset, pages, dma_addr, flags,
362 amdgpu_gart_flush_gpu_tlb(adev, 0);
367 * amdgpu_gart_init - init the driver info for managing the gart
369 * @adev: amdgpu_device pointer
371 * Allocate the dummy page and init the gart driver info (all asics).
372 * Returns 0 for success, error for failure.
374 int amdgpu_gart_init(struct amdgpu_device *adev)
378 if (adev->dummy_page.page)
381 /* We need PAGE_SIZE >= AMDGPU_GPU_PAGE_SIZE */
382 if (PAGE_SIZE < AMDGPU_GPU_PAGE_SIZE) {
383 DRM_ERROR("Page size is smaller than GPU page size!\n");
386 r = amdgpu_dummy_page_init(adev);
389 /* Compute table size */
390 adev->gart.num_cpu_pages = adev->mc.gart_size / PAGE_SIZE;
391 adev->gart.num_gpu_pages = adev->mc.gart_size / AMDGPU_GPU_PAGE_SIZE;
392 DRM_INFO("GART: num cpu pages %u, num gpu pages %u\n",
393 adev->gart.num_cpu_pages, adev->gart.num_gpu_pages);
395 #ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
396 /* Allocate pages table */
397 adev->gart.pages = vzalloc(sizeof(void *) * adev->gart.num_cpu_pages);
398 if (adev->gart.pages == NULL) {
399 amdgpu_gart_fini(adev);
408 * amdgpu_gart_fini - tear down the driver info for managing the gart
410 * @adev: amdgpu_device pointer
412 * Tear down the gart driver info and free the dummy page (all asics).
414 void amdgpu_gart_fini(struct amdgpu_device *adev)
416 if (adev->gart.ready) {
418 amdgpu_gart_unbind(adev, 0, adev->gart.num_cpu_pages);
420 adev->gart.ready = false;
421 #ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
422 vfree(adev->gart.pages);
423 adev->gart.pages = NULL;
425 amdgpu_dummy_page_fini(adev);