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 adev->mc.gart_size = (uint64_t)amdgpu_gart_size << 20;
72 * amdgpu_gart_table_ram_alloc - allocate system ram for gart page table
74 * @adev: amdgpu_device pointer
76 * Allocate system memory for GART page table
77 * (r1xx-r3xx, non-pcie r4xx, rs400). These asics require the
78 * gart table to be in system memory.
79 * Returns 0 for success, -ENOMEM for failure.
81 int amdgpu_gart_table_ram_alloc(struct amdgpu_device *adev)
85 ptr = pci_alloc_consistent(adev->pdev, adev->gart.table_size,
86 &adev->gart.table_addr);
92 set_memory_uc((unsigned long)ptr,
93 adev->gart.table_size >> PAGE_SHIFT);
97 memset((void *)adev->gart.ptr, 0, adev->gart.table_size);
102 * amdgpu_gart_table_ram_free - free system ram for gart page table
104 * @adev: amdgpu_device pointer
106 * Free system memory for GART page table
107 * (r1xx-r3xx, non-pcie r4xx, rs400). These asics require the
108 * gart table to be in system memory.
110 void amdgpu_gart_table_ram_free(struct amdgpu_device *adev)
112 if (adev->gart.ptr == NULL) {
117 set_memory_wb((unsigned long)adev->gart.ptr,
118 adev->gart.table_size >> PAGE_SHIFT);
121 pci_free_consistent(adev->pdev, adev->gart.table_size,
122 (void *)adev->gart.ptr,
123 adev->gart.table_addr);
124 adev->gart.ptr = NULL;
125 adev->gart.table_addr = 0;
129 * amdgpu_gart_table_vram_alloc - allocate vram for gart page table
131 * @adev: amdgpu_device pointer
133 * Allocate video memory for GART page table
134 * (pcie r4xx, r5xx+). These asics require the
135 * gart table to be in video memory.
136 * Returns 0 for success, error for failure.
138 int amdgpu_gart_table_vram_alloc(struct amdgpu_device *adev)
142 if (adev->gart.robj == NULL) {
143 r = amdgpu_bo_create(adev, adev->gart.table_size,
144 PAGE_SIZE, true, AMDGPU_GEM_DOMAIN_VRAM,
145 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
146 AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS,
147 NULL, NULL, 0, &adev->gart.robj);
156 * amdgpu_gart_table_vram_pin - pin gart page table in vram
158 * @adev: amdgpu_device pointer
160 * Pin the GART page table in vram so it will not be moved
161 * by the memory manager (pcie r4xx, r5xx+). These asics require the
162 * gart table to be in video memory.
163 * Returns 0 for success, error for failure.
165 int amdgpu_gart_table_vram_pin(struct amdgpu_device *adev)
170 r = amdgpu_bo_reserve(adev->gart.robj, false);
171 if (unlikely(r != 0))
173 r = amdgpu_bo_pin(adev->gart.robj,
174 AMDGPU_GEM_DOMAIN_VRAM, &gpu_addr);
176 amdgpu_bo_unreserve(adev->gart.robj);
179 r = amdgpu_bo_kmap(adev->gart.robj, &adev->gart.ptr);
181 amdgpu_bo_unpin(adev->gart.robj);
182 amdgpu_bo_unreserve(adev->gart.robj);
183 adev->gart.table_addr = gpu_addr;
188 * amdgpu_gart_table_vram_unpin - unpin gart page table in vram
190 * @adev: amdgpu_device pointer
192 * Unpin the GART page table in vram (pcie r4xx, r5xx+).
193 * These asics require the gart table to be in video memory.
195 void amdgpu_gart_table_vram_unpin(struct amdgpu_device *adev)
199 if (adev->gart.robj == NULL) {
202 r = amdgpu_bo_reserve(adev->gart.robj, true);
203 if (likely(r == 0)) {
204 amdgpu_bo_kunmap(adev->gart.robj);
205 amdgpu_bo_unpin(adev->gart.robj);
206 amdgpu_bo_unreserve(adev->gart.robj);
207 adev->gart.ptr = NULL;
212 * amdgpu_gart_table_vram_free - free gart page table vram
214 * @adev: amdgpu_device pointer
216 * Free the video memory used for the GART page table
217 * (pcie r4xx, r5xx+). These asics require the gart table to
218 * be in video memory.
220 void amdgpu_gart_table_vram_free(struct amdgpu_device *adev)
222 if (adev->gart.robj == NULL) {
225 amdgpu_bo_unref(&adev->gart.robj);
229 * Common gart functions.
232 * amdgpu_gart_unbind - unbind pages from the gart page table
234 * @adev: amdgpu_device pointer
235 * @offset: offset into the GPU's gart aperture
236 * @pages: number of pages to unbind
238 * Unbinds the requested pages from the gart page table and
239 * replaces them with the dummy page (all asics).
240 * Returns 0 for success, -EINVAL for failure.
242 int amdgpu_gart_unbind(struct amdgpu_device *adev, uint64_t offset,
249 /* Starting from VEGA10, system bit must be 0 to mean invalid. */
252 if (!adev->gart.ready) {
253 WARN(1, "trying to unbind memory from uninitialized GART !\n");
257 t = offset / AMDGPU_GPU_PAGE_SIZE;
258 p = t / (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE);
259 for (i = 0; i < pages; i++, p++) {
260 #ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
261 adev->gart.pages[p] = NULL;
263 page_base = adev->dummy_page.addr;
267 for (j = 0; j < (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE); j++, t++) {
268 amdgpu_gart_set_pte_pde(adev, adev->gart.ptr,
269 t, page_base, flags);
270 page_base += AMDGPU_GPU_PAGE_SIZE;
274 amdgpu_gart_flush_gpu_tlb(adev, 0);
279 * amdgpu_gart_map - map dma_addresses into GART entries
281 * @adev: amdgpu_device pointer
282 * @offset: offset into the GPU's gart aperture
283 * @pages: number of pages to bind
284 * @dma_addr: DMA addresses of pages
286 * Map the dma_addresses into GART entries (all asics).
287 * Returns 0 for success, -EINVAL for failure.
289 int amdgpu_gart_map(struct amdgpu_device *adev, uint64_t offset,
290 int pages, dma_addr_t *dma_addr, uint64_t flags,
296 if (!adev->gart.ready) {
297 WARN(1, "trying to bind memory to uninitialized GART !\n");
301 t = offset / AMDGPU_GPU_PAGE_SIZE;
303 for (i = 0; i < pages; i++) {
304 page_base = dma_addr[i];
305 for (j = 0; j < (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE); j++, t++) {
306 amdgpu_gart_set_pte_pde(adev, dst, t, page_base, flags);
307 page_base += AMDGPU_GPU_PAGE_SIZE;
314 * amdgpu_gart_bind - bind pages into the gart page table
316 * @adev: amdgpu_device pointer
317 * @offset: offset into the GPU's gart aperture
318 * @pages: number of pages to bind
319 * @pagelist: pages to bind
320 * @dma_addr: DMA addresses of pages
322 * Binds the requested pages to the gart page table
324 * Returns 0 for success, -EINVAL for failure.
326 int amdgpu_gart_bind(struct amdgpu_device *adev, uint64_t offset,
327 int pages, struct page **pagelist, dma_addr_t *dma_addr,
330 #ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
335 if (!adev->gart.ready) {
336 WARN(1, "trying to bind memory to uninitialized GART !\n");
340 #ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
341 t = offset / AMDGPU_GPU_PAGE_SIZE;
342 p = t / (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE);
343 for (i = 0; i < pages; i++, p++)
344 adev->gart.pages[p] = pagelist[i];
347 if (adev->gart.ptr) {
348 r = amdgpu_gart_map(adev, offset, pages, dma_addr, flags,
355 amdgpu_gart_flush_gpu_tlb(adev, 0);
360 * amdgpu_gart_init - init the driver info for managing the gart
362 * @adev: amdgpu_device pointer
364 * Allocate the dummy page and init the gart driver info (all asics).
365 * Returns 0 for success, error for failure.
367 int amdgpu_gart_init(struct amdgpu_device *adev)
371 if (adev->dummy_page.page)
374 /* We need PAGE_SIZE >= AMDGPU_GPU_PAGE_SIZE */
375 if (PAGE_SIZE < AMDGPU_GPU_PAGE_SIZE) {
376 DRM_ERROR("Page size is smaller than GPU page size!\n");
379 r = amdgpu_dummy_page_init(adev);
382 /* Compute table size */
383 adev->gart.num_cpu_pages = adev->mc.gart_size / PAGE_SIZE;
384 adev->gart.num_gpu_pages = adev->mc.gart_size / AMDGPU_GPU_PAGE_SIZE;
385 DRM_INFO("GART: num cpu pages %u, num gpu pages %u\n",
386 adev->gart.num_cpu_pages, adev->gart.num_gpu_pages);
388 #ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
389 /* Allocate pages table */
390 adev->gart.pages = vzalloc(sizeof(void *) * adev->gart.num_cpu_pages);
391 if (adev->gart.pages == NULL) {
392 amdgpu_gart_fini(adev);
401 * amdgpu_gart_fini - tear down the driver info for managing the gart
403 * @adev: amdgpu_device pointer
405 * Tear down the gart driver info and free the dummy page (all asics).
407 void amdgpu_gart_fini(struct amdgpu_device *adev)
409 if (adev->gart.ready) {
411 amdgpu_gart_unbind(adev, 0, adev->gart.num_cpu_pages);
413 adev->gart.ready = false;
414 #ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
415 vfree(adev->gart.pages);
416 adev->gart.pages = NULL;
418 amdgpu_dummy_page_fini(adev);