radeon_gart.c revision 1.5.32.1 1 /* $NetBSD: radeon_gart.c,v 1.5.32.1 2019/06/10 22:08:26 christos 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: radeon_gart.c,v 1.5.32.1 2019/06/10 22:08:26 christos Exp $");
32
33 #include <drm/drmP.h>
34 #include <drm/radeon_drm.h>
35 #include "radeon.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 * radeon_gart_table_ram_alloc - allocate system ram for gart page table
62 *
63 * @rdev: radeon_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 radeon_gart_table_ram_alloc(struct radeon_device *rdev)
71 {
72 #ifdef __NetBSD__
73 int rsegs;
74 int error;
75
76 error = bus_dmamem_alloc(rdev->ddev->dmat, rdev->gart.table_size,
77 PAGE_SIZE, 0, &rdev->gart.rg_table_seg, 1, &rsegs, BUS_DMA_WAITOK);
78 if (error)
79 goto fail0;
80 KASSERT(rsegs == 1);
81 error = bus_dmamap_create(rdev->ddev->dmat, rdev->gart.table_size, 1,
82 rdev->gart.table_size, 0, BUS_DMA_WAITOK,
83 &rdev->gart.rg_table_map);
84 if (error)
85 goto fail1;
86 error = bus_dmamem_map(rdev->ddev->dmat, &rdev->gart.rg_table_seg, 1,
87 rdev->gart.table_size, &rdev->gart.ptr,
88 BUS_DMA_WAITOK|BUS_DMA_NOCACHE);
89 if (error)
90 goto fail2;
91 error = bus_dmamap_load(rdev->ddev->dmat, rdev->gart.rg_table_map,
92 rdev->gart.ptr, rdev->gart.table_size, NULL, BUS_DMA_WAITOK);
93 if (error)
94 goto fail3;
95
96 /* Success! */
97 rdev->gart.table_addr = rdev->gart.rg_table_map->dm_segs[0].ds_addr;
98 return 0;
99
100 fail4: __unused
101 bus_dmamap_unload(rdev->ddev->dmat, rdev->gart.rg_table_map);
102 fail3: bus_dmamem_unmap(rdev->ddev->dmat, rdev->gart.ptr,
103 rdev->gart.table_size);
104 fail2: bus_dmamap_destroy(rdev->ddev->dmat, rdev->gart.rg_table_map);
105 fail1: bus_dmamem_free(rdev->ddev->dmat, &rdev->gart.rg_table_seg, 1);
106 fail0: KASSERT(error);
107 /* XXX errno NetBSD->Linux */
108 return -error;
109 #else
110 void *ptr;
111
112 ptr = pci_alloc_consistent(rdev->pdev, rdev->gart.table_size,
113 &rdev->gart.table_addr);
114 if (ptr == NULL) {
115 return -ENOMEM;
116 }
117 #ifdef CONFIG_X86
118 if (rdev->family == CHIP_RS400 || rdev->family == CHIP_RS480 ||
119 rdev->family == CHIP_RS690 || rdev->family == CHIP_RS740) {
120 set_memory_uc((unsigned long)ptr,
121 rdev->gart.table_size >> PAGE_SHIFT);
122 }
123 #endif
124 rdev->gart.ptr = ptr;
125 memset((void *)rdev->gart.ptr, 0, rdev->gart.table_size);
126 return 0;
127 #endif
128 }
129
130 /**
131 * radeon_gart_table_ram_free - free system ram for gart page table
132 *
133 * @rdev: radeon_device pointer
134 *
135 * Free system memory for GART page table
136 * (r1xx-r3xx, non-pcie r4xx, rs400). These asics require the
137 * gart table to be in system memory.
138 */
139 void radeon_gart_table_ram_free(struct radeon_device *rdev)
140 {
141 if (rdev->gart.ptr == NULL) {
142 return;
143 }
144 #ifdef __NetBSD__
145 bus_dmamap_unload(rdev->ddev->dmat, rdev->gart.rg_table_map);
146 bus_dmamem_unmap(rdev->ddev->dmat, rdev->gart.ptr,
147 rdev->gart.table_size);
148 bus_dmamap_destroy(rdev->ddev->dmat, rdev->gart.rg_table_map);
149 bus_dmamem_free(rdev->ddev->dmat, &rdev->gart.rg_table_seg, 1);
150 #else
151 #ifdef CONFIG_X86
152 if (rdev->family == CHIP_RS400 || rdev->family == CHIP_RS480 ||
153 rdev->family == CHIP_RS690 || rdev->family == CHIP_RS740) {
154 set_memory_wb((unsigned long)rdev->gart.ptr,
155 rdev->gart.table_size >> PAGE_SHIFT);
156 }
157 #endif
158 pci_free_consistent(rdev->pdev, rdev->gart.table_size,
159 (void *)rdev->gart.ptr,
160 rdev->gart.table_addr);
161 rdev->gart.ptr = NULL;
162 rdev->gart.table_addr = 0;
163 #endif
164 }
165
166 /**
167 * radeon_gart_table_vram_alloc - allocate vram for gart page table
168 *
169 * @rdev: radeon_device pointer
170 *
171 * Allocate video memory for GART page table
172 * (pcie r4xx, r5xx+). These asics require the
173 * gart table to be in video memory.
174 * Returns 0 for success, error for failure.
175 */
176 int radeon_gart_table_vram_alloc(struct radeon_device *rdev)
177 {
178 int r;
179
180 if (rdev->gart.robj == NULL) {
181 r = radeon_bo_create(rdev, rdev->gart.table_size,
182 PAGE_SIZE, true, RADEON_GEM_DOMAIN_VRAM,
183 0, NULL, NULL, &rdev->gart.robj);
184 if (r) {
185 return r;
186 }
187 }
188 return 0;
189 }
190
191 /**
192 * radeon_gart_table_vram_pin - pin gart page table in vram
193 *
194 * @rdev: radeon_device pointer
195 *
196 * Pin the GART page table in vram so it will not be moved
197 * by the memory manager (pcie r4xx, r5xx+). These asics require the
198 * gart table to be in video memory.
199 * Returns 0 for success, error for failure.
200 */
201 int radeon_gart_table_vram_pin(struct radeon_device *rdev)
202 {
203 uint64_t gpu_addr;
204 int r;
205
206 r = radeon_bo_reserve(rdev->gart.robj, false);
207 if (unlikely(r != 0))
208 return r;
209 r = radeon_bo_pin(rdev->gart.robj,
210 RADEON_GEM_DOMAIN_VRAM, &gpu_addr);
211 if (r) {
212 radeon_bo_unreserve(rdev->gart.robj);
213 return r;
214 }
215 r = radeon_bo_kmap(rdev->gart.robj, &rdev->gart.ptr);
216 if (r)
217 radeon_bo_unpin(rdev->gart.robj);
218 radeon_bo_unreserve(rdev->gart.robj);
219 rdev->gart.table_addr = gpu_addr;
220
221 if (!r) {
222 int i;
223
224 /* We might have dropped some GART table updates while it wasn't
225 * mapped, restore all entries
226 */
227 for (i = 0; i < rdev->gart.num_gpu_pages; i++)
228 radeon_gart_set_page(rdev, i, rdev->gart.pages_entry[i]);
229 mb();
230 radeon_gart_tlb_flush(rdev);
231 }
232
233 return r;
234 }
235
236 /**
237 * radeon_gart_table_vram_unpin - unpin gart page table in vram
238 *
239 * @rdev: radeon_device pointer
240 *
241 * Unpin the GART page table in vram (pcie r4xx, r5xx+).
242 * These asics require the gart table to be in video memory.
243 */
244 void radeon_gart_table_vram_unpin(struct radeon_device *rdev)
245 {
246 int r;
247
248 if (rdev->gart.robj == NULL) {
249 return;
250 }
251 r = radeon_bo_reserve(rdev->gart.robj, false);
252 if (likely(r == 0)) {
253 radeon_bo_kunmap(rdev->gart.robj);
254 radeon_bo_unpin(rdev->gart.robj);
255 radeon_bo_unreserve(rdev->gart.robj);
256 rdev->gart.ptr = NULL;
257 }
258 }
259
260 /**
261 * radeon_gart_table_vram_free - free gart page table vram
262 *
263 * @rdev: radeon_device pointer
264 *
265 * Free the video memory used for the GART page table
266 * (pcie r4xx, r5xx+). These asics require the gart table to
267 * be in video memory.
268 */
269 void radeon_gart_table_vram_free(struct radeon_device *rdev)
270 {
271 if (rdev->gart.robj == NULL) {
272 return;
273 }
274 radeon_bo_unref(&rdev->gart.robj);
275 }
276
277 #ifdef __NetBSD__
278 static void
279 radeon_gart_pre_update(struct radeon_device *rdev, unsigned gpu_pgstart,
280 unsigned gpu_npages)
281 {
282
283 if (rdev->gart.rg_table_map != NULL) {
284 const unsigned entsize =
285 rdev->gart.table_size / rdev->gart.num_gpu_pages;
286
287 bus_dmamap_sync(rdev->ddev->dmat, rdev->gart.rg_table_map,
288 gpu_pgstart*entsize, gpu_npages*entsize,
289 BUS_DMASYNC_POSTWRITE);
290 }
291 }
292
293 static void
294 radeon_gart_post_update(struct radeon_device *rdev, unsigned gpu_pgstart,
295 unsigned gpu_npages)
296 {
297
298 if (rdev->gart.rg_table_map != NULL) {
299 const unsigned entsize =
300 rdev->gart.table_size / rdev->gart.num_gpu_pages;
301
302 bus_dmamap_sync(rdev->ddev->dmat, rdev->gart.rg_table_map,
303 gpu_pgstart*entsize, gpu_npages*entsize,
304 BUS_DMASYNC_PREWRITE);
305 }
306 if (rdev->gart.ptr != NULL) {
307 membar_sync(); /* XXX overkill */
308 radeon_gart_tlb_flush(rdev);
309 }
310 }
311 #endif
312
313 /*
314 * Common gart functions.
315 */
316 #ifdef __NetBSD__
317 void
318 radeon_gart_unbind(struct radeon_device *rdev, unsigned gpu_start,
319 unsigned npages)
320 {
321 const unsigned gpu_per_cpu = (PAGE_SIZE / RADEON_GPU_PAGE_SIZE);
322 const unsigned gpu_npages = (npages * gpu_per_cpu);
323 const unsigned gpu_pgstart = (gpu_start / RADEON_GPU_PAGE_SIZE);
324 const unsigned pgstart = (gpu_pgstart / gpu_per_cpu);
325 unsigned pgno, gpu_pgno;
326
327 KASSERT(pgstart == (gpu_start / PAGE_SIZE));
328 KASSERT(npages <= rdev->gart.num_cpu_pages);
329 KASSERT(gpu_npages <= rdev->gart.num_cpu_pages);
330
331 if (!rdev->gart.ready) {
332 WARN(1, "trying to bind memory to uninitialized GART !\n");
333 return;
334 }
335
336 radeon_gart_pre_update(rdev, gpu_pgstart, gpu_npages);
337 for (pgno = 0; pgno < npages; pgno++) {
338 if (rdev->gart.pages[pgstart + pgno] == NULL)
339 continue;
340 rdev->gart.pages[pgstart + pgno] = NULL;
341 for (gpu_pgno = 0; gpu_pgno < gpu_per_cpu; gpu_pgno++) {
342 const unsigned t = gpu_pgstart + gpu_per_cpu*pgno +
343 gpu_pgno;
344 rdev->gart.pages_entry[t] = rdev->dummy_page.entry;
345 if (rdev->gart.ptr == NULL)
346 continue;
347 radeon_gart_set_page(rdev, t, rdev->dummy_page.entry);
348 }
349 }
350 radeon_gart_post_update(rdev, gpu_pgstart, gpu_npages);
351 }
352 #else
353 /**
354 * radeon_gart_unbind - unbind pages from the gart page table
355 *
356 * @rdev: radeon_device pointer
357 * @offset: offset into the GPU's gart aperture
358 * @pages: number of pages to unbind
359 *
360 * Unbinds the requested pages from the gart page table and
361 * replaces them with the dummy page (all asics).
362 */
363 void radeon_gart_unbind(struct radeon_device *rdev, unsigned offset,
364 int pages)
365 {
366 unsigned t;
367 unsigned p;
368 int i, j;
369
370 if (!rdev->gart.ready) {
371 WARN(1, "trying to unbind memory from uninitialized GART !\n");
372 return;
373 }
374 t = offset / RADEON_GPU_PAGE_SIZE;
375 p = t / (PAGE_SIZE / RADEON_GPU_PAGE_SIZE);
376 for (i = 0; i < pages; i++, p++) {
377 if (rdev->gart.pages[p]) {
378 rdev->gart.pages[p] = NULL;
379 for (j = 0; j < (PAGE_SIZE / RADEON_GPU_PAGE_SIZE); j++, t++) {
380 rdev->gart.pages_entry[t] = rdev->dummy_page.entry;
381 if (rdev->gart.ptr) {
382 radeon_gart_set_page(rdev, t,
383 rdev->dummy_page.entry);
384 }
385 }
386 }
387 }
388 if (rdev->gart.ptr) {
389 mb();
390 radeon_gart_tlb_flush(rdev);
391 }
392 }
393 #endif
394
395 #ifdef __NetBSD__
396 int
397 radeon_gart_bind(struct radeon_device *rdev, unsigned gpu_start,
398 unsigned npages, struct page **pages, bus_dmamap_t dmamap, uint32_t flags)
399 {
400 const unsigned gpu_per_cpu = (PAGE_SIZE / RADEON_GPU_PAGE_SIZE);
401 const unsigned gpu_npages = (npages * gpu_per_cpu);
402 const unsigned gpu_pgstart = (gpu_start / RADEON_GPU_PAGE_SIZE);
403 const unsigned pgstart = (gpu_pgstart / gpu_per_cpu);
404 unsigned pgno, gpu_pgno;
405 uint64_t page_entry;
406
407 KASSERT(pgstart == (gpu_start / PAGE_SIZE));
408 KASSERT(npages == dmamap->dm_nsegs);
409 KASSERT(npages <= rdev->gart.num_cpu_pages);
410 KASSERT(gpu_npages <= rdev->gart.num_cpu_pages);
411
412 if (!rdev->gart.ready) {
413 WARN(1, "trying to bind memory to uninitialized GART !\n");
414 return -EINVAL;
415 }
416
417 radeon_gart_pre_update(rdev, gpu_pgstart, gpu_npages);
418 for (pgno = 0; pgno < npages; pgno++) {
419 const bus_addr_t addr = dmamap->dm_segs[pgno].ds_addr;
420
421 KASSERT(dmamap->dm_segs[pgno].ds_len == PAGE_SIZE);
422 rdev->gart.pages[pgstart + pgno] = pages[pgno];
423 for (gpu_pgno = 0; gpu_pgno < gpu_per_cpu; gpu_pgno++) {
424 const unsigned i = gpu_pgstart + gpu_per_cpu*pgno +
425 gpu_pgno;
426 page_entry = radeon_gart_get_page_entry(
427 addr + gpu_pgno*RADEON_GPU_PAGE_SIZE, flags);
428 rdev->gart.pages_entry[i] = page_entry;
429 if (rdev->gart.ptr == NULL)
430 continue;
431 radeon_gart_set_page(rdev, i, page_entry);
432 }
433 }
434 radeon_gart_post_update(rdev, gpu_pgstart, gpu_npages);
435
436 return 0;
437 }
438 #else
439 /**
440 * radeon_gart_bind - bind pages into the gart page table
441 *
442 * @rdev: radeon_device pointer
443 * @offset: offset into the GPU's gart aperture
444 * @pages: number of pages to bind
445 * @pagelist: pages to bind
446 * @dma_addr: DMA addresses of pages
447 * @flags: RADEON_GART_PAGE_* flags
448 *
449 * Binds the requested pages to the gart page table
450 * (all asics).
451 * Returns 0 for success, -EINVAL for failure.
452 */
453 int radeon_gart_bind(struct radeon_device *rdev, unsigned offset,
454 int pages, struct page **pagelist, dma_addr_t *dma_addr,
455 uint32_t flags)
456 {
457 unsigned t;
458 unsigned p;
459 uint64_t page_base, page_entry;
460 int i, j;
461
462 if (!rdev->gart.ready) {
463 WARN(1, "trying to bind memory to uninitialized GART !\n");
464 return -EINVAL;
465 }
466 t = offset / RADEON_GPU_PAGE_SIZE;
467 p = t / (PAGE_SIZE / RADEON_GPU_PAGE_SIZE);
468
469 for (i = 0; i < pages; i++, p++) {
470 rdev->gart.pages[p] = pagelist[i];
471 page_base = dma_addr[i];
472 for (j = 0; j < (PAGE_SIZE / RADEON_GPU_PAGE_SIZE); j++, t++) {
473 page_entry = radeon_gart_get_page_entry(page_base, flags);
474 rdev->gart.pages_entry[t] = page_entry;
475 if (rdev->gart.ptr) {
476 radeon_gart_set_page(rdev, t, page_entry);
477 }
478 page_base += RADEON_GPU_PAGE_SIZE;
479 }
480 }
481 if (rdev->gart.ptr) {
482 mb();
483 radeon_gart_tlb_flush(rdev);
484 }
485 return 0;
486 }
487 #endif
488
489 /**
490 * radeon_gart_init - init the driver info for managing the gart
491 *
492 * @rdev: radeon_device pointer
493 *
494 * Allocate the dummy page and init the gart driver info (all asics).
495 * Returns 0 for success, error for failure.
496 */
497 int radeon_gart_init(struct radeon_device *rdev)
498 {
499 int r, i;
500
501 if (rdev->gart.pages) {
502 return 0;
503 }
504 /* We need PAGE_SIZE >= RADEON_GPU_PAGE_SIZE */
505 if (PAGE_SIZE < RADEON_GPU_PAGE_SIZE) {
506 DRM_ERROR("Page size is smaller than GPU page size!\n");
507 return -EINVAL;
508 }
509 r = radeon_dummy_page_init(rdev);
510 if (r)
511 return r;
512 /* Compute table size */
513 rdev->gart.num_cpu_pages = rdev->mc.gtt_size / PAGE_SIZE;
514 rdev->gart.num_gpu_pages = rdev->mc.gtt_size / RADEON_GPU_PAGE_SIZE;
515 DRM_INFO("GART: num cpu pages %u, num gpu pages %u\n",
516 rdev->gart.num_cpu_pages, rdev->gart.num_gpu_pages);
517 /* Allocate pages table */
518 rdev->gart.pages = vzalloc(sizeof(void *) * rdev->gart.num_cpu_pages);
519 if (rdev->gart.pages == NULL) {
520 radeon_gart_fini(rdev);
521 return -ENOMEM;
522 }
523 rdev->gart.pages_entry = vmalloc(sizeof(uint64_t) *
524 rdev->gart.num_gpu_pages);
525 if (rdev->gart.pages_entry == NULL) {
526 radeon_gart_fini(rdev);
527 return -ENOMEM;
528 }
529 /* set GART entry to point to the dummy page by default */
530 for (i = 0; i < rdev->gart.num_gpu_pages; i++)
531 rdev->gart.pages_entry[i] = rdev->dummy_page.entry;
532 return 0;
533 }
534
535 /**
536 * radeon_gart_fini - tear down the driver info for managing the gart
537 *
538 * @rdev: radeon_device pointer
539 *
540 * Tear down the gart driver info and free the dummy page (all asics).
541 */
542 void radeon_gart_fini(struct radeon_device *rdev)
543 {
544 if (rdev->gart.ready) {
545 /* unbind pages */
546 radeon_gart_unbind(rdev, 0, rdev->gart.num_cpu_pages);
547 }
548 rdev->gart.ready = false;
549 vfree(rdev->gart.pages);
550 vfree(rdev->gart.pages_entry);
551 rdev->gart.pages = NULL;
552 rdev->gart.pages_entry = NULL;
553
554 radeon_dummy_page_fini(rdev);
555 }
556