amdgpu_gart.c revision 1.2 1 /* $NetBSD: amdgpu_gart.c,v 1.2 2018/08/27 04:58:19 riastradh Exp $ */
2
3 /*
4 * Copyright 2008 Advanced Micro Devices, Inc.
5 * Copyright 2008 Red Hat Inc.
6 * Copyright 2009 Jerome Glisse.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
22 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 * OTHER DEALINGS IN THE SOFTWARE.
25 *
26 * Authors: Dave Airlie
27 * Alex Deucher
28 * Jerome Glisse
29 */
30 #include <sys/cdefs.h>
31 __KERNEL_RCSID(0, "$NetBSD: amdgpu_gart.c,v 1.2 2018/08/27 04:58:19 riastradh Exp $");
32
33 #include <drm/drmP.h>
34 #include <drm/amdgpu_drm.h>
35 #include "amdgpu.h"
36
37 /*
38 * GART
39 * The GART (Graphics Aperture Remapping Table) is an aperture
40 * in the GPU's address space. System pages can be mapped into
41 * the aperture and look like contiguous pages from the GPU's
42 * perspective. A page table maps the pages in the aperture
43 * to the actual backing pages in system memory.
44 *
45 * Radeon GPUs support both an internal GART, as described above,
46 * and AGP. AGP works similarly, but the GART table is configured
47 * and maintained by the northbridge rather than the driver.
48 * Radeon hw has a separate AGP aperture that is programmed to
49 * point to the AGP aperture provided by the northbridge and the
50 * requests are passed through to the northbridge aperture.
51 * Both AGP and internal GART can be used at the same time, however
52 * that is not currently supported by the driver.
53 *
54 * This file handles the common internal GART management.
55 */
56
57 /*
58 * Common GART table functions.
59 */
60 /**
61 * amdgpu_gart_table_ram_alloc - allocate system ram for gart page table
62 *
63 * @adev: amdgpu_device pointer
64 *
65 * Allocate system memory for GART page table
66 * (r1xx-r3xx, non-pcie r4xx, rs400). These asics require the
67 * gart table to be in system memory.
68 * Returns 0 for success, -ENOMEM for failure.
69 */
70 int amdgpu_gart_table_ram_alloc(struct amdgpu_device *adev)
71 {
72 void *ptr;
73
74 ptr = pci_alloc_consistent(adev->pdev, adev->gart.table_size,
75 &adev->gart.table_addr);
76 if (ptr == NULL) {
77 return -ENOMEM;
78 }
79 #ifdef CONFIG_X86
80 if (0) {
81 set_memory_uc((unsigned long)ptr,
82 adev->gart.table_size >> PAGE_SHIFT);
83 }
84 #endif
85 adev->gart.ptr = ptr;
86 memset((void *)adev->gart.ptr, 0, adev->gart.table_size);
87 return 0;
88 }
89
90 /**
91 * amdgpu_gart_table_ram_free - free system ram for gart page table
92 *
93 * @adev: amdgpu_device pointer
94 *
95 * Free system memory for GART page table
96 * (r1xx-r3xx, non-pcie r4xx, rs400). These asics require the
97 * gart table to be in system memory.
98 */
99 void amdgpu_gart_table_ram_free(struct amdgpu_device *adev)
100 {
101 if (adev->gart.ptr == NULL) {
102 return;
103 }
104 #ifdef CONFIG_X86
105 if (0) {
106 set_memory_wb((unsigned long)adev->gart.ptr,
107 adev->gart.table_size >> PAGE_SHIFT);
108 }
109 #endif
110 pci_free_consistent(adev->pdev, adev->gart.table_size,
111 (void *)adev->gart.ptr,
112 adev->gart.table_addr);
113 adev->gart.ptr = NULL;
114 adev->gart.table_addr = 0;
115 }
116
117 /**
118 * amdgpu_gart_table_vram_alloc - allocate vram for gart page table
119 *
120 * @adev: amdgpu_device pointer
121 *
122 * Allocate video memory for GART page table
123 * (pcie r4xx, r5xx+). These asics require the
124 * gart table to be in video memory.
125 * Returns 0 for success, error for failure.
126 */
127 int amdgpu_gart_table_vram_alloc(struct amdgpu_device *adev)
128 {
129 int r;
130
131 if (adev->gart.robj == NULL) {
132 r = amdgpu_bo_create(adev, adev->gart.table_size,
133 PAGE_SIZE, true, AMDGPU_GEM_DOMAIN_VRAM,
134 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED,
135 NULL, NULL, &adev->gart.robj);
136 if (r) {
137 return r;
138 }
139 }
140 return 0;
141 }
142
143 /**
144 * amdgpu_gart_table_vram_pin - pin gart page table in vram
145 *
146 * @adev: amdgpu_device pointer
147 *
148 * Pin the GART page table in vram so it will not be moved
149 * by the memory manager (pcie r4xx, r5xx+). These asics require the
150 * gart table to be in video memory.
151 * Returns 0 for success, error for failure.
152 */
153 int amdgpu_gart_table_vram_pin(struct amdgpu_device *adev)
154 {
155 uint64_t gpu_addr;
156 int r;
157
158 r = amdgpu_bo_reserve(adev->gart.robj, false);
159 if (unlikely(r != 0))
160 return r;
161 r = amdgpu_bo_pin(adev->gart.robj,
162 AMDGPU_GEM_DOMAIN_VRAM, &gpu_addr);
163 if (r) {
164 amdgpu_bo_unreserve(adev->gart.robj);
165 return r;
166 }
167 r = amdgpu_bo_kmap(adev->gart.robj, &adev->gart.ptr);
168 if (r)
169 amdgpu_bo_unpin(adev->gart.robj);
170 amdgpu_bo_unreserve(adev->gart.robj);
171 adev->gart.table_addr = gpu_addr;
172 return r;
173 }
174
175 /**
176 * amdgpu_gart_table_vram_unpin - unpin gart page table in vram
177 *
178 * @adev: amdgpu_device pointer
179 *
180 * Unpin the GART page table in vram (pcie r4xx, r5xx+).
181 * These asics require the gart table to be in video memory.
182 */
183 void amdgpu_gart_table_vram_unpin(struct amdgpu_device *adev)
184 {
185 int r;
186
187 if (adev->gart.robj == NULL) {
188 return;
189 }
190 r = amdgpu_bo_reserve(adev->gart.robj, false);
191 if (likely(r == 0)) {
192 amdgpu_bo_kunmap(adev->gart.robj);
193 amdgpu_bo_unpin(adev->gart.robj);
194 amdgpu_bo_unreserve(adev->gart.robj);
195 adev->gart.ptr = NULL;
196 }
197 }
198
199 /**
200 * amdgpu_gart_table_vram_free - free gart page table vram
201 *
202 * @adev: amdgpu_device pointer
203 *
204 * Free the video memory used for the GART page table
205 * (pcie r4xx, r5xx+). These asics require the gart table to
206 * be in video memory.
207 */
208 void amdgpu_gart_table_vram_free(struct amdgpu_device *adev)
209 {
210 if (adev->gart.robj == NULL) {
211 return;
212 }
213 amdgpu_bo_unref(&adev->gart.robj);
214 }
215
216 /*
217 * Common gart functions.
218 */
219 /**
220 * amdgpu_gart_unbind - unbind pages from the gart page table
221 *
222 * @adev: amdgpu_device pointer
223 * @offset: offset into the GPU's gart aperture
224 * @pages: number of pages to unbind
225 *
226 * Unbinds the requested pages from the gart page table and
227 * replaces them with the dummy page (all asics).
228 */
229 void amdgpu_gart_unbind(struct amdgpu_device *adev, uint64_t offset,
230 int pages)
231 {
232 unsigned t;
233 unsigned p;
234 int i, j;
235 u64 page_base;
236 uint32_t flags = AMDGPU_PTE_SYSTEM;
237
238 if (!adev->gart.ready) {
239 WARN(1, "trying to unbind memory from uninitialized GART !\n");
240 return;
241 }
242
243 t = offset / AMDGPU_GPU_PAGE_SIZE;
244 p = t / (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE);
245 for (i = 0; i < pages; i++, p++) {
246 if (adev->gart.pages[p]) {
247 adev->gart.pages[p] = NULL;
248 adev->gart.pages_addr[p] = adev->dummy_page.addr;
249 page_base = adev->gart.pages_addr[p];
250 if (!adev->gart.ptr)
251 continue;
252
253 for (j = 0; j < (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE); j++, t++) {
254 amdgpu_gart_set_pte_pde(adev, adev->gart.ptr,
255 t, page_base, flags);
256 page_base += AMDGPU_GPU_PAGE_SIZE;
257 }
258 }
259 }
260 mb();
261 amdgpu_gart_flush_gpu_tlb(adev, 0);
262 }
263
264 /**
265 * amdgpu_gart_bind - bind pages into the gart page table
266 *
267 * @adev: amdgpu_device pointer
268 * @offset: offset into the GPU's gart aperture
269 * @pages: number of pages to bind
270 * @pagelist: pages to bind
271 * @dma_addr: DMA addresses of pages
272 *
273 * Binds the requested pages to the gart page table
274 * (all asics).
275 * Returns 0 for success, -EINVAL for failure.
276 */
277 int amdgpu_gart_bind(struct amdgpu_device *adev, uint64_t offset,
278 int pages, struct page **pagelist, dma_addr_t *dma_addr,
279 uint32_t flags)
280 {
281 unsigned t;
282 unsigned p;
283 uint64_t page_base;
284 int i, j;
285
286 if (!adev->gart.ready) {
287 WARN(1, "trying to bind memory to uninitialized GART !\n");
288 return -EINVAL;
289 }
290
291 t = offset / AMDGPU_GPU_PAGE_SIZE;
292 p = t / (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE);
293
294 for (i = 0; i < pages; i++, p++) {
295 adev->gart.pages_addr[p] = dma_addr[i];
296 adev->gart.pages[p] = pagelist[i];
297 if (adev->gart.ptr) {
298 page_base = adev->gart.pages_addr[p];
299 for (j = 0; j < (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE); j++, t++) {
300 amdgpu_gart_set_pte_pde(adev, adev->gart.ptr, t, page_base, flags);
301 page_base += AMDGPU_GPU_PAGE_SIZE;
302 }
303 }
304 }
305 mb();
306 amdgpu_gart_flush_gpu_tlb(adev, 0);
307 return 0;
308 }
309
310 /**
311 * amdgpu_gart_init - init the driver info for managing the gart
312 *
313 * @adev: amdgpu_device pointer
314 *
315 * Allocate the dummy page and init the gart driver info (all asics).
316 * Returns 0 for success, error for failure.
317 */
318 int amdgpu_gart_init(struct amdgpu_device *adev)
319 {
320 int r, i;
321
322 if (adev->gart.pages) {
323 return 0;
324 }
325 /* We need PAGE_SIZE >= AMDGPU_GPU_PAGE_SIZE */
326 if (PAGE_SIZE < AMDGPU_GPU_PAGE_SIZE) {
327 DRM_ERROR("Page size is smaller than GPU page size!\n");
328 return -EINVAL;
329 }
330 r = amdgpu_dummy_page_init(adev);
331 if (r)
332 return r;
333 /* Compute table size */
334 adev->gart.num_cpu_pages = adev->mc.gtt_size / PAGE_SIZE;
335 adev->gart.num_gpu_pages = adev->mc.gtt_size / AMDGPU_GPU_PAGE_SIZE;
336 DRM_INFO("GART: num cpu pages %u, num gpu pages %u\n",
337 adev->gart.num_cpu_pages, adev->gart.num_gpu_pages);
338 /* Allocate pages table */
339 adev->gart.pages = vzalloc(sizeof(void *) * adev->gart.num_cpu_pages);
340 if (adev->gart.pages == NULL) {
341 amdgpu_gart_fini(adev);
342 return -ENOMEM;
343 }
344 adev->gart.pages_addr = vzalloc(sizeof(dma_addr_t) *
345 adev->gart.num_cpu_pages);
346 if (adev->gart.pages_addr == NULL) {
347 amdgpu_gart_fini(adev);
348 return -ENOMEM;
349 }
350 /* set GART entry to point to the dummy page by default */
351 for (i = 0; i < adev->gart.num_cpu_pages; i++) {
352 adev->gart.pages_addr[i] = adev->dummy_page.addr;
353 }
354 return 0;
355 }
356
357 /**
358 * amdgpu_gart_fini - tear down the driver info for managing the gart
359 *
360 * @adev: amdgpu_device pointer
361 *
362 * Tear down the gart driver info and free the dummy page (all asics).
363 */
364 void amdgpu_gart_fini(struct amdgpu_device *adev)
365 {
366 if (adev->gart.pages && adev->gart.pages_addr && adev->gart.ready) {
367 /* unbind pages */
368 amdgpu_gart_unbind(adev, 0, adev->gart.num_cpu_pages);
369 }
370 adev->gart.ready = false;
371 vfree(adev->gart.pages);
372 vfree(adev->gart.pages_addr);
373 adev->gart.pages = NULL;
374 adev->gart.pages_addr = NULL;
375
376 amdgpu_dummy_page_fini(adev);
377 }
378