blob: 10eeb307700c77ed604e8d028b7a304e279f2148 [file] [log] [blame]
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001/*
2 * Copyright 2008 Advanced Micro Devices, Inc.
3 * Copyright 2008 Red Hat Inc.
4 * Copyright 2009 Jerome Glisse.
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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.
23 *
24 * Authors: Dave Airlie
25 * Alex Deucher
26 * Jerome Glisse
27 */
28#include <drm/drmP.h>
29#include <drm/amdgpu_drm.h>
Laura Abbotted3ba072017-05-08 15:58:17 -070030#ifdef CONFIG_X86
31#include <asm/set_memory.h>
32#endif
Alex Deucherd38ceaf2015-04-20 16:55:21 -040033#include "amdgpu.h"
34
35/*
36 * GART
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.
42 *
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.
51 *
52 * This file handles the common internal GART management.
53 */
54
55/*
56 * Common GART table functions.
57 */
Christian König011d4bb2017-06-26 11:37:49 +020058
59/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -040060 * amdgpu_gart_table_vram_alloc - allocate vram for gart page table
61 *
62 * @adev: amdgpu_device pointer
63 *
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.
68 */
69int amdgpu_gart_table_vram_alloc(struct amdgpu_device *adev)
70{
Monk Liuc79ee7d2017-11-14 11:52:35 +080071 return amdgpu_bo_create_kernel(adev, adev->gart.table_size, PAGE_SIZE,
72 AMDGPU_GEM_DOMAIN_VRAM, &adev->gart.robj,
73 &adev->gart.table_addr, &adev->gart.ptr);
Alex Deucherd38ceaf2015-04-20 16:55:21 -040074}
75
76/**
77 * amdgpu_gart_table_vram_free - free gart page table vram
78 *
79 * @adev: amdgpu_device pointer
80 *
81 * Free the video memory used for the GART page table
82 * (pcie r4xx, r5xx+). These asics require the gart table to
83 * be in video memory.
84 */
85void amdgpu_gart_table_vram_free(struct amdgpu_device *adev)
86{
Monk Liuc79ee7d2017-11-14 11:52:35 +080087 amdgpu_bo_free_kernel(&adev->gart.robj,
88 &adev->gart.table_addr,
89 &adev->gart.ptr);
Alex Deucherd38ceaf2015-04-20 16:55:21 -040090}
91
92/*
93 * Common gart functions.
94 */
95/**
96 * amdgpu_gart_unbind - unbind pages from the gart page table
97 *
98 * @adev: amdgpu_device pointer
99 * @offset: offset into the GPU's gart aperture
100 * @pages: number of pages to unbind
101 *
102 * Unbinds the requested pages from the gart page table and
103 * replaces them with the dummy page (all asics).
Roger.He738f64c2017-05-05 13:27:10 +0800104 * Returns 0 for success, -EINVAL for failure.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400105 */
Roger.He738f64c2017-05-05 13:27:10 +0800106int amdgpu_gart_unbind(struct amdgpu_device *adev, uint64_t offset,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400107 int pages)
108{
109 unsigned t;
110 unsigned p;
111 int i, j;
112 u64 page_base;
Alex Deuchera0676f62017-03-03 16:42:27 -0500113 /* Starting from VEGA10, system bit must be 0 to mean invalid. */
114 uint64_t flags = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400115
116 if (!adev->gart.ready) {
117 WARN(1, "trying to unbind memory from uninitialized GART !\n");
Roger.He738f64c2017-05-05 13:27:10 +0800118 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400119 }
120
121 t = offset / AMDGPU_GPU_PAGE_SIZE;
122 p = t / (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE);
123 for (i = 0; i < pages; i++, p++) {
Christian König186294f2016-09-25 16:10:06 +0200124#ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
Christian Königa1d29472016-03-30 14:42:57 +0200125 adev->gart.pages[p] = NULL;
126#endif
127 page_base = adev->dummy_page.addr;
128 if (!adev->gart.ptr)
129 continue;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400130
Christian Königa1d29472016-03-30 14:42:57 +0200131 for (j = 0; j < (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE); j++, t++) {
132 amdgpu_gart_set_pte_pde(adev, adev->gart.ptr,
133 t, page_base, flags);
134 page_base += AMDGPU_GPU_PAGE_SIZE;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400135 }
136 }
137 mb();
138 amdgpu_gart_flush_gpu_tlb(adev, 0);
Roger.He738f64c2017-05-05 13:27:10 +0800139 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400140}
141
142/**
Christian König0c2c4212017-06-29 17:24:26 +0200143 * amdgpu_gart_map - map dma_addresses into GART entries
144 *
145 * @adev: amdgpu_device pointer
146 * @offset: offset into the GPU's gart aperture
147 * @pages: number of pages to bind
148 * @dma_addr: DMA addresses of pages
149 *
150 * Map the dma_addresses into GART entries (all asics).
151 * Returns 0 for success, -EINVAL for failure.
152 */
153int amdgpu_gart_map(struct amdgpu_device *adev, uint64_t offset,
154 int pages, dma_addr_t *dma_addr, uint64_t flags,
155 void *dst)
156{
157 uint64_t page_base;
158 unsigned i, j, t;
159
160 if (!adev->gart.ready) {
161 WARN(1, "trying to bind memory to uninitialized GART !\n");
162 return -EINVAL;
163 }
164
165 t = offset / AMDGPU_GPU_PAGE_SIZE;
166
167 for (i = 0; i < pages; i++) {
168 page_base = dma_addr[i];
169 for (j = 0; j < (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE); j++, t++) {
170 amdgpu_gart_set_pte_pde(adev, dst, t, page_base, flags);
171 page_base += AMDGPU_GPU_PAGE_SIZE;
172 }
173 }
174 return 0;
175}
176
177/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400178 * amdgpu_gart_bind - bind pages into the gart page table
179 *
180 * @adev: amdgpu_device pointer
181 * @offset: offset into the GPU's gart aperture
182 * @pages: number of pages to bind
183 * @pagelist: pages to bind
184 * @dma_addr: DMA addresses of pages
185 *
186 * Binds the requested pages to the gart page table
187 * (all asics).
188 * Returns 0 for success, -EINVAL for failure.
189 */
Felix Kuehlingcab0b8d2016-08-12 19:25:21 -0400190int amdgpu_gart_bind(struct amdgpu_device *adev, uint64_t offset,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400191 int pages, struct page **pagelist, dma_addr_t *dma_addr,
Chunming Zhou6b777602016-09-21 16:19:19 +0800192 uint64_t flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400193{
Christian König0c2c4212017-06-29 17:24:26 +0200194#ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
195 unsigned i,t,p;
196#endif
197 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400198
199 if (!adev->gart.ready) {
200 WARN(1, "trying to bind memory to uninitialized GART !\n");
201 return -EINVAL;
202 }
203
Christian König0c2c4212017-06-29 17:24:26 +0200204#ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400205 t = offset / AMDGPU_GPU_PAGE_SIZE;
206 p = t / (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE);
Christian König0c2c4212017-06-29 17:24:26 +0200207 for (i = 0; i < pages; i++, p++)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400208 adev->gart.pages[p] = pagelist[i];
Christian Königa1d29472016-03-30 14:42:57 +0200209#endif
Christian König0c2c4212017-06-29 17:24:26 +0200210
Christian Königfa2cd032017-10-16 17:37:06 +0200211 if (!adev->gart.ptr)
212 return 0;
213
214 r = amdgpu_gart_map(adev, offset, pages, dma_addr, flags,
215 adev->gart.ptr);
216 if (r)
217 return r;
Christian König0c2c4212017-06-29 17:24:26 +0200218
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400219 mb();
220 amdgpu_gart_flush_gpu_tlb(adev, 0);
221 return 0;
222}
223
224/**
225 * amdgpu_gart_init - init the driver info for managing the gart
226 *
227 * @adev: amdgpu_device pointer
228 *
229 * Allocate the dummy page and init the gart driver info (all asics).
230 * Returns 0 for success, error for failure.
231 */
232int amdgpu_gart_init(struct amdgpu_device *adev)
233{
Christian König43251982016-03-30 10:54:16 +0200234 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400235
Christian Königa1d29472016-03-30 14:42:57 +0200236 if (adev->dummy_page.page)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400237 return 0;
Christian Königa1d29472016-03-30 14:42:57 +0200238
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400239 /* We need PAGE_SIZE >= AMDGPU_GPU_PAGE_SIZE */
240 if (PAGE_SIZE < AMDGPU_GPU_PAGE_SIZE) {
241 DRM_ERROR("Page size is smaller than GPU page size!\n");
242 return -EINVAL;
243 }
244 r = amdgpu_dummy_page_init(adev);
245 if (r)
246 return r;
247 /* Compute table size */
Christian König6f02a692017-07-07 11:56:59 +0200248 adev->gart.num_cpu_pages = adev->mc.gart_size / PAGE_SIZE;
249 adev->gart.num_gpu_pages = adev->mc.gart_size / AMDGPU_GPU_PAGE_SIZE;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400250 DRM_INFO("GART: num cpu pages %u, num gpu pages %u\n",
251 adev->gart.num_cpu_pages, adev->gart.num_gpu_pages);
Christian Königa1d29472016-03-30 14:42:57 +0200252
Christian König186294f2016-09-25 16:10:06 +0200253#ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400254 /* Allocate pages table */
255 adev->gart.pages = vzalloc(sizeof(void *) * adev->gart.num_cpu_pages);
256 if (adev->gart.pages == NULL) {
257 amdgpu_gart_fini(adev);
258 return -ENOMEM;
259 }
Christian Königa1d29472016-03-30 14:42:57 +0200260#endif
261
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400262 return 0;
263}
264
265/**
266 * amdgpu_gart_fini - tear down the driver info for managing the gart
267 *
268 * @adev: amdgpu_device pointer
269 *
270 * Tear down the gart driver info and free the dummy page (all asics).
271 */
272void amdgpu_gart_fini(struct amdgpu_device *adev)
273{
Christian Königa1d29472016-03-30 14:42:57 +0200274 if (adev->gart.ready) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400275 /* unbind pages */
276 amdgpu_gart_unbind(adev, 0, adev->gart.num_cpu_pages);
277 }
278 adev->gart.ready = false;
Christian König186294f2016-09-25 16:10:06 +0200279#ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400280 vfree(adev->gart.pages);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400281 adev->gart.pages = NULL;
Christian Königa1d29472016-03-30 14:42:57 +0200282#endif
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400283 amdgpu_dummy_page_fini(adev);
284}