1 /*
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
29 #include <linux/pci.h>
30 #include <linux/vmalloc.h>
31
32 #include <drm/amdgpu_drm.h>
33 #ifdef CONFIG_X86
34 #include <asm/set_memory.h>
35 #endif
36 #include "amdgpu.h"
37 #include "amdgpu_reset.h"
38 #include <drm/drm_drv.h>
39 #include <drm/ttm/ttm_tt.h>
40
41 /*
42 * GART
43 * The GART (Graphics Aperture Remapping Table) is an aperture
44 * in the GPU's address space. System pages can be mapped into
45 * the aperture and look like contiguous pages from the GPU's
46 * perspective. A page table maps the pages in the aperture
47 * to the actual backing pages in system memory.
48 *
49 * Radeon GPUs support both an internal GART, as described above,
50 * and AGP. AGP works similarly, but the GART table is configured
51 * and maintained by the northbridge rather than the driver.
52 * Radeon hw has a separate AGP aperture that is programmed to
53 * point to the AGP aperture provided by the northbridge and the
54 * requests are passed through to the northbridge aperture.
55 * Both AGP and internal GART can be used at the same time, however
56 * that is not currently supported by the driver.
57 *
58 * This file handles the common internal GART management.
59 */
60
61 /*
62 * Common GART table functions.
63 */
64
65 /**
66 * amdgpu_gart_dummy_page_init - init dummy page used by the driver
67 *
68 * @adev: amdgpu_device pointer
69 *
70 * Allocate the dummy page used by the driver (all asics).
71 * This dummy page is used by the driver as a filler for gart entries
72 * when pages are taken out of the GART
73 * Returns 0 on sucess, -ENOMEM on failure.
74 */
amdgpu_gart_dummy_page_init(struct amdgpu_device * adev)75 static int amdgpu_gart_dummy_page_init(struct amdgpu_device *adev)
76 {
77 struct vm_page *dummy_page = ttm_glob.dummy_read_page;
78
79 if (adev->dummy_page_addr)
80 return 0;
81 adev->dummy_page_addr = dma_map_page(&adev->pdev->dev, dummy_page, 0,
82 PAGE_SIZE, DMA_BIDIRECTIONAL);
83 if (dma_mapping_error(&adev->pdev->dev, adev->dummy_page_addr)) {
84 dev_err(&adev->pdev->dev, "Failed to DMA MAP the dummy page\n");
85 adev->dummy_page_addr = 0;
86 return -ENOMEM;
87 }
88 return 0;
89 }
90
91 /**
92 * amdgpu_gart_dummy_page_fini - free dummy page used by the driver
93 *
94 * @adev: amdgpu_device pointer
95 *
96 * Frees the dummy page used by the driver (all asics).
97 */
amdgpu_gart_dummy_page_fini(struct amdgpu_device * adev)98 void amdgpu_gart_dummy_page_fini(struct amdgpu_device *adev)
99 {
100 if (!adev->dummy_page_addr)
101 return;
102 dma_unmap_page(&adev->pdev->dev, adev->dummy_page_addr, PAGE_SIZE,
103 DMA_BIDIRECTIONAL);
104 adev->dummy_page_addr = 0;
105 }
106
107 /**
108 * amdgpu_gart_table_ram_alloc - allocate system ram for gart page table
109 *
110 * @adev: amdgpu_device pointer
111 *
112 * Allocate system memory for GART page table for ASICs that don't have
113 * dedicated VRAM.
114 * Returns 0 for success, error for failure.
115 */
amdgpu_gart_table_ram_alloc(struct amdgpu_device * adev)116 int amdgpu_gart_table_ram_alloc(struct amdgpu_device *adev)
117 {
118 STUB();
119 return -ENOSYS;
120 #ifdef notyet
121 unsigned int order = get_order(adev->gart.table_size);
122 gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO;
123 struct amdgpu_bo *bo = NULL;
124 struct sg_table *sg = NULL;
125 struct amdgpu_bo_param bp;
126 dma_addr_t dma_addr;
127 struct vm_page *p;
128 unsigned long x;
129 int ret;
130
131 if (adev->gart.bo != NULL)
132 return 0;
133
134 p = alloc_pages(gfp_flags, order);
135 if (!p)
136 return -ENOMEM;
137
138 /* assign pages to this device */
139 for (x = 0; x < (1UL << order); x++)
140 p[x].mapping = adev->mman.bdev.dev_mapping;
141
142 /* If the hardware does not support UTCL2 snooping of the CPU caches
143 * then set_memory_wc() could be used as a workaround to mark the pages
144 * as write combine memory.
145 */
146 dma_addr = dma_map_page(&adev->pdev->dev, p, 0, adev->gart.table_size,
147 DMA_BIDIRECTIONAL);
148 if (dma_mapping_error(&adev->pdev->dev, dma_addr)) {
149 dev_err(&adev->pdev->dev, "Failed to DMA MAP the GART BO page\n");
150 __free_pages(p, order);
151 p = NULL;
152 return -EFAULT;
153 }
154
155 dev_info(adev->dev, "%s dma_addr:%pad\n", __func__, &dma_addr);
156 /* Create SG table */
157 sg = kmalloc(sizeof(*sg), GFP_KERNEL);
158 if (!sg) {
159 ret = -ENOMEM;
160 goto error;
161 }
162 ret = sg_alloc_table(sg, 1, GFP_KERNEL);
163 if (ret)
164 goto error;
165
166 sg_dma_address(sg->sgl) = dma_addr;
167 sg->sgl->length = adev->gart.table_size;
168 #ifdef CONFIG_NEED_SG_DMA_LENGTH
169 sg->sgl->dma_length = adev->gart.table_size;
170 #endif
171 /* Create SG BO */
172 memset(&bp, 0, sizeof(bp));
173 bp.size = adev->gart.table_size;
174 bp.byte_align = PAGE_SIZE;
175 bp.domain = AMDGPU_GEM_DOMAIN_CPU;
176 bp.type = ttm_bo_type_sg;
177 bp.resv = NULL;
178 bp.bo_ptr_size = sizeof(struct amdgpu_bo);
179 bp.flags = 0;
180 ret = amdgpu_bo_create(adev, &bp, &bo);
181 if (ret)
182 goto error;
183
184 bo->tbo.sg = sg;
185 bo->tbo.ttm->sg = sg;
186 bo->allowed_domains = AMDGPU_GEM_DOMAIN_GTT;
187 bo->preferred_domains = AMDGPU_GEM_DOMAIN_GTT;
188
189 ret = amdgpu_bo_reserve(bo, true);
190 if (ret) {
191 dev_err(adev->dev, "(%d) failed to reserve bo for GART system bo\n", ret);
192 goto error;
193 }
194
195 ret = amdgpu_bo_pin(bo, AMDGPU_GEM_DOMAIN_GTT);
196 WARN(ret, "Pinning the GART table failed");
197 if (ret)
198 goto error_resv;
199
200 adev->gart.bo = bo;
201 adev->gart.ptr = page_to_virt(p);
202 /* Make GART table accessible in VMID0 */
203 ret = amdgpu_ttm_alloc_gart(&adev->gart.bo->tbo);
204 if (ret)
205 amdgpu_gart_table_ram_free(adev);
206 amdgpu_bo_unreserve(bo);
207
208 return 0;
209
210 error_resv:
211 amdgpu_bo_unreserve(bo);
212 error:
213 amdgpu_bo_unref(&bo);
214 if (sg) {
215 sg_free_table(sg);
216 kfree(sg);
217 }
218 __free_pages(p, order);
219 return ret;
220 #endif
221 }
222
223 /**
224 * amdgpu_gart_table_ram_free - free gart page table system ram
225 *
226 * @adev: amdgpu_device pointer
227 *
228 * Free the system memory used for the GART page tableon ASICs that don't
229 * have dedicated VRAM.
230 */
amdgpu_gart_table_ram_free(struct amdgpu_device * adev)231 void amdgpu_gart_table_ram_free(struct amdgpu_device *adev)
232 {
233 unsigned int order = get_order(adev->gart.table_size);
234 struct sg_table *sg = adev->gart.bo->tbo.sg;
235 struct vm_page *p;
236 unsigned long x;
237 int ret;
238
239 ret = amdgpu_bo_reserve(adev->gart.bo, false);
240 if (!ret) {
241 amdgpu_bo_unpin(adev->gart.bo);
242 amdgpu_bo_unreserve(adev->gart.bo);
243 }
244 amdgpu_bo_unref(&adev->gart.bo);
245 sg_free_table(sg);
246 kfree(sg);
247 p = virt_to_page(adev->gart.ptr);
248 #ifdef __linux__
249 for (x = 0; x < (1UL << order); x++)
250 p[x].mapping = NULL;
251 #endif
252 __free_pages(p, order);
253
254 adev->gart.ptr = NULL;
255 }
256
257 /**
258 * amdgpu_gart_table_vram_alloc - allocate vram for gart page table
259 *
260 * @adev: amdgpu_device pointer
261 *
262 * Allocate video memory for GART page table
263 * (pcie r4xx, r5xx+). These asics require the
264 * gart table to be in video memory.
265 * Returns 0 for success, error for failure.
266 */
amdgpu_gart_table_vram_alloc(struct amdgpu_device * adev)267 int amdgpu_gart_table_vram_alloc(struct amdgpu_device *adev)
268 {
269 if (adev->gart.bo != NULL)
270 return 0;
271
272 return amdgpu_bo_create_kernel(adev, adev->gart.table_size, PAGE_SIZE,
273 AMDGPU_GEM_DOMAIN_VRAM, &adev->gart.bo,
274 NULL, (void *)&adev->gart.ptr);
275 }
276
277 /**
278 * amdgpu_gart_table_vram_free - free gart page table vram
279 *
280 * @adev: amdgpu_device pointer
281 *
282 * Free the video memory used for the GART page table
283 * (pcie r4xx, r5xx+). These asics require the gart table to
284 * be in video memory.
285 */
amdgpu_gart_table_vram_free(struct amdgpu_device * adev)286 void amdgpu_gart_table_vram_free(struct amdgpu_device *adev)
287 {
288 amdgpu_bo_free_kernel(&adev->gart.bo, NULL, (void *)&adev->gart.ptr);
289 }
290
291 /*
292 * Common gart functions.
293 */
294 /**
295 * amdgpu_gart_unbind - unbind pages from the gart page table
296 *
297 * @adev: amdgpu_device pointer
298 * @offset: offset into the GPU's gart aperture
299 * @pages: number of pages to unbind
300 *
301 * Unbinds the requested pages from the gart page table and
302 * replaces them with the dummy page (all asics).
303 * Returns 0 for success, -EINVAL for failure.
304 */
amdgpu_gart_unbind(struct amdgpu_device * adev,uint64_t offset,int pages)305 void amdgpu_gart_unbind(struct amdgpu_device *adev, uint64_t offset,
306 int pages)
307 {
308 unsigned t;
309 unsigned p;
310 int i, j;
311 u64 page_base;
312 /* Starting from VEGA10, system bit must be 0 to mean invalid. */
313 uint64_t flags = 0;
314 int idx;
315
316 if (!adev->gart.ptr)
317 return;
318
319 if (!drm_dev_enter(adev_to_drm(adev), &idx))
320 return;
321
322 t = offset / AMDGPU_GPU_PAGE_SIZE;
323 p = t / AMDGPU_GPU_PAGES_IN_CPU_PAGE;
324 for (i = 0; i < pages; i++, p++) {
325 page_base = adev->dummy_page_addr;
326 if (!adev->gart.ptr)
327 continue;
328
329 for (j = 0; j < AMDGPU_GPU_PAGES_IN_CPU_PAGE; j++, t++) {
330 amdgpu_gmc_set_pte_pde(adev, adev->gart.ptr,
331 t, page_base, flags);
332 page_base += AMDGPU_GPU_PAGE_SIZE;
333 }
334 }
335 amdgpu_gart_invalidate_tlb(adev);
336
337 drm_dev_exit(idx);
338 }
339
340 /**
341 * amdgpu_gart_map - map dma_addresses into GART entries
342 *
343 * @adev: amdgpu_device pointer
344 * @offset: offset into the GPU's gart aperture
345 * @pages: number of pages to bind
346 * @dma_addr: DMA addresses of pages
347 * @flags: page table entry flags
348 * @dst: CPU address of the gart table
349 *
350 * Map the dma_addresses into GART entries (all asics).
351 * Returns 0 for success, -EINVAL for failure.
352 */
amdgpu_gart_map(struct amdgpu_device * adev,uint64_t offset,int pages,dma_addr_t * dma_addr,uint64_t flags,void * dst)353 void amdgpu_gart_map(struct amdgpu_device *adev, uint64_t offset,
354 int pages, dma_addr_t *dma_addr, uint64_t flags,
355 void *dst)
356 {
357 uint64_t page_base;
358 unsigned i, j, t;
359 int idx;
360
361 if (!drm_dev_enter(adev_to_drm(adev), &idx))
362 return;
363
364 t = offset / AMDGPU_GPU_PAGE_SIZE;
365
366 for (i = 0; i < pages; i++) {
367 page_base = dma_addr[i];
368 for (j = 0; j < AMDGPU_GPU_PAGES_IN_CPU_PAGE; j++, t++) {
369 amdgpu_gmc_set_pte_pde(adev, dst, t, page_base, flags);
370 page_base += AMDGPU_GPU_PAGE_SIZE;
371 }
372 }
373 drm_dev_exit(idx);
374 }
375
376 /**
377 * amdgpu_gart_bind - bind pages into the gart page table
378 *
379 * @adev: amdgpu_device pointer
380 * @offset: offset into the GPU's gart aperture
381 * @pages: number of pages to bind
382 * @dma_addr: DMA addresses of pages
383 * @flags: page table entry flags
384 *
385 * Binds the requested pages to the gart page table
386 * (all asics).
387 * Returns 0 for success, -EINVAL for failure.
388 */
amdgpu_gart_bind(struct amdgpu_device * adev,uint64_t offset,int pages,dma_addr_t * dma_addr,uint64_t flags)389 void amdgpu_gart_bind(struct amdgpu_device *adev, uint64_t offset,
390 int pages, dma_addr_t *dma_addr,
391 uint64_t flags)
392 {
393 if (!adev->gart.ptr)
394 return;
395
396 amdgpu_gart_map(adev, offset, pages, dma_addr, flags, adev->gart.ptr);
397 }
398
399 /**
400 * amdgpu_gart_invalidate_tlb - invalidate gart TLB
401 *
402 * @adev: amdgpu device driver pointer
403 *
404 * Invalidate gart TLB which can be use as a way to flush gart changes
405 *
406 */
amdgpu_gart_invalidate_tlb(struct amdgpu_device * adev)407 void amdgpu_gart_invalidate_tlb(struct amdgpu_device *adev)
408 {
409 int i;
410
411 if (!adev->gart.ptr)
412 return;
413
414 mb();
415 if (down_read_trylock(&adev->reset_domain->sem)) {
416 amdgpu_device_flush_hdp(adev, NULL);
417 up_read(&adev->reset_domain->sem);
418 }
419 for_each_set_bit(i, adev->vmhubs_mask, AMDGPU_MAX_VMHUBS)
420 amdgpu_gmc_flush_gpu_tlb(adev, 0, i, 0);
421 }
422
423 /**
424 * amdgpu_gart_init - init the driver info for managing the gart
425 *
426 * @adev: amdgpu_device pointer
427 *
428 * Allocate the dummy page and init the gart driver info (all asics).
429 * Returns 0 for success, error for failure.
430 */
amdgpu_gart_init(struct amdgpu_device * adev)431 int amdgpu_gart_init(struct amdgpu_device *adev)
432 {
433 int r;
434
435 if (adev->dummy_page_addr)
436 return 0;
437
438 /* We need PAGE_SIZE >= AMDGPU_GPU_PAGE_SIZE */
439 if (PAGE_SIZE < AMDGPU_GPU_PAGE_SIZE) {
440 DRM_ERROR("Page size is smaller than GPU page size!\n");
441 return -EINVAL;
442 }
443 r = amdgpu_gart_dummy_page_init(adev);
444 if (r)
445 return r;
446 /* Compute table size */
447 adev->gart.num_cpu_pages = adev->gmc.gart_size / PAGE_SIZE;
448 adev->gart.num_gpu_pages = adev->gmc.gart_size / AMDGPU_GPU_PAGE_SIZE;
449 DRM_INFO("GART: num cpu pages %u, num gpu pages %u\n",
450 adev->gart.num_cpu_pages, adev->gart.num_gpu_pages);
451
452 return 0;
453 }
454