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.
59 * amdgpu_gart_table_ram_alloc - allocate system ram for gart page table
61 * @adev: amdgpu_device pointer
63 * Allocate system memory for GART page table
64 * (r1xx-r3xx, non-pcie r4xx, rs400). These asics require the
65 * gart table to be in system memory.
66 * Returns 0 for success, -ENOMEM for failure.
68 int amdgpu_gart_table_ram_alloc(struct amdgpu_device *adev)
72 ptr = pci_alloc_consistent(adev->pdev, adev->gart.table_size,
73 &adev->gart.table_addr);
79 set_memory_uc((unsigned long)ptr,
80 adev->gart.table_size >> PAGE_SHIFT);
84 memset((void *)adev->gart.ptr, 0, adev->gart.table_size);
89 * amdgpu_gart_table_ram_free - free system ram for gart page table
91 * @adev: amdgpu_device pointer
93 * Free system memory for GART page table
94 * (r1xx-r3xx, non-pcie r4xx, rs400). These asics require the
95 * gart table to be in system memory.
97 void amdgpu_gart_table_ram_free(struct amdgpu_device *adev)
99 if (adev->gart.ptr == NULL) {
104 set_memory_wb((unsigned long)adev->gart.ptr,
105 adev->gart.table_size >> PAGE_SHIFT);
108 pci_free_consistent(adev->pdev, adev->gart.table_size,
109 (void *)adev->gart.ptr,
110 adev->gart.table_addr);
111 adev->gart.ptr = NULL;
112 adev->gart.table_addr = 0;
116 * amdgpu_gart_table_vram_alloc - allocate vram for gart page table
118 * @adev: amdgpu_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 amdgpu_gart_table_vram_alloc(struct amdgpu_device *adev)
129 if (adev->gart.robj == NULL) {
130 r = amdgpu_bo_create(adev, adev->gart.table_size,
131 PAGE_SIZE, true, AMDGPU_GEM_DOMAIN_VRAM,
132 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
133 AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS,
134 NULL, NULL, &adev->gart.robj);
143 * amdgpu_gart_table_vram_pin - pin gart page table in vram
145 * @adev: amdgpu_device pointer
147 * Pin the GART page table in vram so it will not be moved
148 * by the memory manager (pcie r4xx, r5xx+). These asics require the
149 * gart table to be in video memory.
150 * Returns 0 for success, error for failure.
152 int amdgpu_gart_table_vram_pin(struct amdgpu_device *adev)
157 r = amdgpu_bo_reserve(adev->gart.robj, false);
158 if (unlikely(r != 0))
160 r = amdgpu_bo_pin(adev->gart.robj,
161 AMDGPU_GEM_DOMAIN_VRAM, &gpu_addr);
163 amdgpu_bo_unreserve(adev->gart.robj);
166 r = amdgpu_bo_kmap(adev->gart.robj, &adev->gart.ptr);
168 amdgpu_bo_unpin(adev->gart.robj);
169 amdgpu_bo_unreserve(adev->gart.robj);
170 adev->gart.table_addr = gpu_addr;
175 * amdgpu_gart_table_vram_unpin - unpin gart page table in vram
177 * @adev: amdgpu_device pointer
179 * Unpin the GART page table in vram (pcie r4xx, r5xx+).
180 * These asics require the gart table to be in video memory.
182 void amdgpu_gart_table_vram_unpin(struct amdgpu_device *adev)
186 if (adev->gart.robj == NULL) {
189 r = amdgpu_bo_reserve(adev->gart.robj, true);
190 if (likely(r == 0)) {
191 amdgpu_bo_kunmap(adev->gart.robj);
192 amdgpu_bo_unpin(adev->gart.robj);
193 amdgpu_bo_unreserve(adev->gart.robj);
194 adev->gart.ptr = NULL;
199 * amdgpu_gart_table_vram_free - free gart page table vram
201 * @adev: amdgpu_device pointer
203 * Free the video memory used for the GART page table
204 * (pcie r4xx, r5xx+). These asics require the gart table to
205 * be in video memory.
207 void amdgpu_gart_table_vram_free(struct amdgpu_device *adev)
209 if (adev->gart.robj == NULL) {
212 amdgpu_bo_unref(&adev->gart.robj);
216 * Common gart functions.
219 * amdgpu_gart_unbind - unbind pages from the gart page table
221 * @adev: amdgpu_device pointer
222 * @offset: offset into the GPU's gart aperture
223 * @pages: number of pages to unbind
225 * Unbinds the requested pages from the gart page table and
226 * replaces them with the dummy page (all asics).
227 * Returns 0 for success, -EINVAL for failure.
229 int amdgpu_gart_unbind(struct amdgpu_device *adev, uint64_t offset,
236 /* Starting from VEGA10, system bit must be 0 to mean invalid. */
239 if (!adev->gart.ready) {
240 WARN(1, "trying to unbind memory from uninitialized GART !\n");
244 t = offset / AMDGPU_GPU_PAGE_SIZE;
245 p = t / (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE);
246 for (i = 0; i < pages; i++, p++) {
247 #ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
248 adev->gart.pages[p] = NULL;
250 page_base = adev->dummy_page.addr;
254 for (j = 0; j < (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE); j++, t++) {
255 amdgpu_gart_set_pte_pde(adev, adev->gart.ptr,
256 t, page_base, flags);
257 page_base += AMDGPU_GPU_PAGE_SIZE;
261 amdgpu_gart_flush_gpu_tlb(adev, 0);
266 * amdgpu_gart_bind - bind pages into the gart page table
268 * @adev: amdgpu_device pointer
269 * @offset: offset into the GPU's gart aperture
270 * @pages: number of pages to bind
271 * @pagelist: pages to bind
272 * @dma_addr: DMA addresses of pages
274 * Binds the requested pages to the gart page table
276 * Returns 0 for success, -EINVAL for failure.
278 int amdgpu_gart_bind(struct amdgpu_device *adev, uint64_t offset,
279 int pages, struct page **pagelist, dma_addr_t *dma_addr,
287 if (!adev->gart.ready) {
288 WARN(1, "trying to bind memory to uninitialized GART !\n");
292 t = offset / AMDGPU_GPU_PAGE_SIZE;
293 p = t / (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE);
295 for (i = 0; i < pages; i++, p++) {
296 #ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
297 adev->gart.pages[p] = pagelist[i];
299 if (adev->gart.ptr) {
300 page_base = dma_addr[i];
301 for (j = 0; j < (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE); j++, t++) {
302 amdgpu_gart_set_pte_pde(adev, adev->gart.ptr, t, page_base, flags);
303 page_base += AMDGPU_GPU_PAGE_SIZE;
308 amdgpu_gart_flush_gpu_tlb(adev, 0);
313 * amdgpu_gart_init - init the driver info for managing the gart
315 * @adev: amdgpu_device pointer
317 * Allocate the dummy page and init the gart driver info (all asics).
318 * Returns 0 for success, error for failure.
320 int amdgpu_gart_init(struct amdgpu_device *adev)
324 if (adev->dummy_page.page)
327 /* We need PAGE_SIZE >= AMDGPU_GPU_PAGE_SIZE */
328 if (PAGE_SIZE < AMDGPU_GPU_PAGE_SIZE) {
329 DRM_ERROR("Page size is smaller than GPU page size!\n");
332 r = amdgpu_dummy_page_init(adev);
335 /* Compute table size */
336 adev->gart.num_cpu_pages = adev->mc.gtt_size / PAGE_SIZE;
337 adev->gart.num_gpu_pages = adev->mc.gtt_size / AMDGPU_GPU_PAGE_SIZE;
338 DRM_INFO("GART: num cpu pages %u, num gpu pages %u\n",
339 adev->gart.num_cpu_pages, adev->gart.num_gpu_pages);
341 #ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
342 /* Allocate pages table */
343 adev->gart.pages = vzalloc(sizeof(void *) * adev->gart.num_cpu_pages);
344 if (adev->gart.pages == NULL) {
345 amdgpu_gart_fini(adev);
354 * amdgpu_gart_fini - tear down the driver info for managing the gart
356 * @adev: amdgpu_device pointer
358 * Tear down the gart driver info and free the dummy page (all asics).
360 void amdgpu_gart_fini(struct amdgpu_device *adev)
362 if (adev->gart.ready) {
364 amdgpu_gart_unbind(adev, 0, adev->gart.num_cpu_pages);
366 adev->gart.ready = false;
367 #ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
368 vfree(adev->gart.pages);
369 adev->gart.pages = NULL;
371 amdgpu_dummy_page_fini(adev);