]> Git Repo - linux.git/blob - drivers/gpu/drm/amd/amdgpu/amdgpu_gart.c
drm/amdgpu: consistent name all GART related parts
[linux.git] / drivers / gpu / drm / amd / amdgpu / amdgpu_gart.c
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 #include <drm/drmP.h>
29 #include <drm/amdgpu_drm.h>
30 #ifdef CONFIG_X86
31 #include <asm/set_memory.h>
32 #endif
33 #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  */
58
59 /**
60  * amdgpu_gart_set_defaults - set the default gart_size
61  *
62  * @adev: amdgpu_device pointer
63  *
64  * Set the default gart_size based on parameters and available VRAM.
65  */
66 void amdgpu_gart_set_defaults(struct amdgpu_device *adev)
67 {
68         /* unless the user had overridden it, set the gart
69          * size equal to the 1024 or vram, whichever is larger.
70          */
71         if (amdgpu_gart_size == -1)
72                 adev->mc.gart_size = max((AMDGPU_DEFAULT_GTT_SIZE_MB << 20),
73                                         adev->mc.mc_vram_size);
74         else
75                 adev->mc.gart_size = (uint64_t)amdgpu_gart_size << 20;
76 }
77
78 /**
79  * amdgpu_gart_table_ram_alloc - allocate system ram for gart page table
80  *
81  * @adev: amdgpu_device pointer
82  *
83  * Allocate system memory for GART page table
84  * (r1xx-r3xx, non-pcie r4xx, rs400).  These asics require the
85  * gart table to be in system memory.
86  * Returns 0 for success, -ENOMEM for failure.
87  */
88 int amdgpu_gart_table_ram_alloc(struct amdgpu_device *adev)
89 {
90         void *ptr;
91
92         ptr = pci_alloc_consistent(adev->pdev, adev->gart.table_size,
93                                    &adev->gart.table_addr);
94         if (ptr == NULL) {
95                 return -ENOMEM;
96         }
97 #ifdef CONFIG_X86
98         if (0) {
99                 set_memory_uc((unsigned long)ptr,
100                               adev->gart.table_size >> PAGE_SHIFT);
101         }
102 #endif
103         adev->gart.ptr = ptr;
104         memset((void *)adev->gart.ptr, 0, adev->gart.table_size);
105         return 0;
106 }
107
108 /**
109  * amdgpu_gart_table_ram_free - free system ram for gart page table
110  *
111  * @adev: amdgpu_device pointer
112  *
113  * Free system memory for GART page table
114  * (r1xx-r3xx, non-pcie r4xx, rs400).  These asics require the
115  * gart table to be in system memory.
116  */
117 void amdgpu_gart_table_ram_free(struct amdgpu_device *adev)
118 {
119         if (adev->gart.ptr == NULL) {
120                 return;
121         }
122 #ifdef CONFIG_X86
123         if (0) {
124                 set_memory_wb((unsigned long)adev->gart.ptr,
125                               adev->gart.table_size >> PAGE_SHIFT);
126         }
127 #endif
128         pci_free_consistent(adev->pdev, adev->gart.table_size,
129                             (void *)adev->gart.ptr,
130                             adev->gart.table_addr);
131         adev->gart.ptr = NULL;
132         adev->gart.table_addr = 0;
133 }
134
135 /**
136  * amdgpu_gart_table_vram_alloc - allocate vram for gart page table
137  *
138  * @adev: amdgpu_device pointer
139  *
140  * Allocate video memory for GART page table
141  * (pcie r4xx, r5xx+).  These asics require the
142  * gart table to be in video memory.
143  * Returns 0 for success, error for failure.
144  */
145 int amdgpu_gart_table_vram_alloc(struct amdgpu_device *adev)
146 {
147         int r;
148
149         if (adev->gart.robj == NULL) {
150                 r = amdgpu_bo_create(adev, adev->gart.table_size,
151                                      PAGE_SIZE, true, AMDGPU_GEM_DOMAIN_VRAM,
152                                      AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
153                                      AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS,
154                                      NULL, NULL, &adev->gart.robj);
155                 if (r) {
156                         return r;
157                 }
158         }
159         return 0;
160 }
161
162 /**
163  * amdgpu_gart_table_vram_pin - pin gart page table in vram
164  *
165  * @adev: amdgpu_device pointer
166  *
167  * Pin the GART page table in vram so it will not be moved
168  * by the memory manager (pcie r4xx, r5xx+).  These asics require the
169  * gart table to be in video memory.
170  * Returns 0 for success, error for failure.
171  */
172 int amdgpu_gart_table_vram_pin(struct amdgpu_device *adev)
173 {
174         uint64_t gpu_addr;
175         int r;
176
177         r = amdgpu_bo_reserve(adev->gart.robj, false);
178         if (unlikely(r != 0))
179                 return r;
180         r = amdgpu_bo_pin(adev->gart.robj,
181                                 AMDGPU_GEM_DOMAIN_VRAM, &gpu_addr);
182         if (r) {
183                 amdgpu_bo_unreserve(adev->gart.robj);
184                 return r;
185         }
186         r = amdgpu_bo_kmap(adev->gart.robj, &adev->gart.ptr);
187         if (r)
188                 amdgpu_bo_unpin(adev->gart.robj);
189         amdgpu_bo_unreserve(adev->gart.robj);
190         adev->gart.table_addr = gpu_addr;
191         return r;
192 }
193
194 /**
195  * amdgpu_gart_table_vram_unpin - unpin gart page table in vram
196  *
197  * @adev: amdgpu_device pointer
198  *
199  * Unpin the GART page table in vram (pcie r4xx, r5xx+).
200  * These asics require the gart table to be in video memory.
201  */
202 void amdgpu_gart_table_vram_unpin(struct amdgpu_device *adev)
203 {
204         int r;
205
206         if (adev->gart.robj == NULL) {
207                 return;
208         }
209         r = amdgpu_bo_reserve(adev->gart.robj, true);
210         if (likely(r == 0)) {
211                 amdgpu_bo_kunmap(adev->gart.robj);
212                 amdgpu_bo_unpin(adev->gart.robj);
213                 amdgpu_bo_unreserve(adev->gart.robj);
214                 adev->gart.ptr = NULL;
215         }
216 }
217
218 /**
219  * amdgpu_gart_table_vram_free - free gart page table vram
220  *
221  * @adev: amdgpu_device pointer
222  *
223  * Free the video memory used for the GART page table
224  * (pcie r4xx, r5xx+).  These asics require the gart table to
225  * be in video memory.
226  */
227 void amdgpu_gart_table_vram_free(struct amdgpu_device *adev)
228 {
229         if (adev->gart.robj == NULL) {
230                 return;
231         }
232         amdgpu_bo_unref(&adev->gart.robj);
233 }
234
235 /*
236  * Common gart functions.
237  */
238 /**
239  * amdgpu_gart_unbind - unbind pages from the gart page table
240  *
241  * @adev: amdgpu_device pointer
242  * @offset: offset into the GPU's gart aperture
243  * @pages: number of pages to unbind
244  *
245  * Unbinds the requested pages from the gart page table and
246  * replaces them with the dummy page (all asics).
247  * Returns 0 for success, -EINVAL for failure.
248  */
249 int amdgpu_gart_unbind(struct amdgpu_device *adev, uint64_t offset,
250                         int pages)
251 {
252         unsigned t;
253         unsigned p;
254         int i, j;
255         u64 page_base;
256         /* Starting from VEGA10, system bit must be 0 to mean invalid. */
257         uint64_t flags = 0;
258
259         if (!adev->gart.ready) {
260                 WARN(1, "trying to unbind memory from uninitialized GART !\n");
261                 return -EINVAL;
262         }
263
264         t = offset / AMDGPU_GPU_PAGE_SIZE;
265         p = t / (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE);
266         for (i = 0; i < pages; i++, p++) {
267 #ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
268                 adev->gart.pages[p] = NULL;
269 #endif
270                 page_base = adev->dummy_page.addr;
271                 if (!adev->gart.ptr)
272                         continue;
273
274                 for (j = 0; j < (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE); j++, t++) {
275                         amdgpu_gart_set_pte_pde(adev, adev->gart.ptr,
276                                                 t, page_base, flags);
277                         page_base += AMDGPU_GPU_PAGE_SIZE;
278                 }
279         }
280         mb();
281         amdgpu_gart_flush_gpu_tlb(adev, 0);
282         return 0;
283 }
284
285 /**
286  * amdgpu_gart_map - map dma_addresses into GART entries
287  *
288  * @adev: amdgpu_device pointer
289  * @offset: offset into the GPU's gart aperture
290  * @pages: number of pages to bind
291  * @dma_addr: DMA addresses of pages
292  *
293  * Map the dma_addresses into GART entries (all asics).
294  * Returns 0 for success, -EINVAL for failure.
295  */
296 int amdgpu_gart_map(struct amdgpu_device *adev, uint64_t offset,
297                     int pages, dma_addr_t *dma_addr, uint64_t flags,
298                     void *dst)
299 {
300         uint64_t page_base;
301         unsigned i, j, t;
302
303         if (!adev->gart.ready) {
304                 WARN(1, "trying to bind memory to uninitialized GART !\n");
305                 return -EINVAL;
306         }
307
308         t = offset / AMDGPU_GPU_PAGE_SIZE;
309
310         for (i = 0; i < pages; i++) {
311                 page_base = dma_addr[i];
312                 for (j = 0; j < (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE); j++, t++) {
313                         amdgpu_gart_set_pte_pde(adev, dst, t, page_base, flags);
314                         page_base += AMDGPU_GPU_PAGE_SIZE;
315                 }
316         }
317         return 0;
318 }
319
320 /**
321  * amdgpu_gart_bind - bind pages into the gart page table
322  *
323  * @adev: amdgpu_device pointer
324  * @offset: offset into the GPU's gart aperture
325  * @pages: number of pages to bind
326  * @pagelist: pages to bind
327  * @dma_addr: DMA addresses of pages
328  *
329  * Binds the requested pages to the gart page table
330  * (all asics).
331  * Returns 0 for success, -EINVAL for failure.
332  */
333 int amdgpu_gart_bind(struct amdgpu_device *adev, uint64_t offset,
334                      int pages, struct page **pagelist, dma_addr_t *dma_addr,
335                      uint64_t flags)
336 {
337 #ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
338         unsigned i,t,p;
339 #endif
340         int r;
341
342         if (!adev->gart.ready) {
343                 WARN(1, "trying to bind memory to uninitialized GART !\n");
344                 return -EINVAL;
345         }
346
347 #ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
348         t = offset / AMDGPU_GPU_PAGE_SIZE;
349         p = t / (PAGE_SIZE / AMDGPU_GPU_PAGE_SIZE);
350         for (i = 0; i < pages; i++, p++)
351                 adev->gart.pages[p] = pagelist[i];
352 #endif
353
354         if (adev->gart.ptr) {
355                 r = amdgpu_gart_map(adev, offset, pages, dma_addr, flags,
356                             adev->gart.ptr);
357                 if (r)
358                         return r;
359         }
360
361         mb();
362         amdgpu_gart_flush_gpu_tlb(adev, 0);
363         return 0;
364 }
365
366 /**
367  * amdgpu_gart_init - init the driver info for managing the gart
368  *
369  * @adev: amdgpu_device pointer
370  *
371  * Allocate the dummy page and init the gart driver info (all asics).
372  * Returns 0 for success, error for failure.
373  */
374 int amdgpu_gart_init(struct amdgpu_device *adev)
375 {
376         int r;
377
378         if (adev->dummy_page.page)
379                 return 0;
380
381         /* We need PAGE_SIZE >= AMDGPU_GPU_PAGE_SIZE */
382         if (PAGE_SIZE < AMDGPU_GPU_PAGE_SIZE) {
383                 DRM_ERROR("Page size is smaller than GPU page size!\n");
384                 return -EINVAL;
385         }
386         r = amdgpu_dummy_page_init(adev);
387         if (r)
388                 return r;
389         /* Compute table size */
390         adev->gart.num_cpu_pages = adev->mc.gart_size / PAGE_SIZE;
391         adev->gart.num_gpu_pages = adev->mc.gart_size / AMDGPU_GPU_PAGE_SIZE;
392         DRM_INFO("GART: num cpu pages %u, num gpu pages %u\n",
393                  adev->gart.num_cpu_pages, adev->gart.num_gpu_pages);
394
395 #ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
396         /* Allocate pages table */
397         adev->gart.pages = vzalloc(sizeof(void *) * adev->gart.num_cpu_pages);
398         if (adev->gart.pages == NULL) {
399                 amdgpu_gart_fini(adev);
400                 return -ENOMEM;
401         }
402 #endif
403
404         return 0;
405 }
406
407 /**
408  * amdgpu_gart_fini - tear down the driver info for managing the gart
409  *
410  * @adev: amdgpu_device pointer
411  *
412  * Tear down the gart driver info and free the dummy page (all asics).
413  */
414 void amdgpu_gart_fini(struct amdgpu_device *adev)
415 {
416         if (adev->gart.ready) {
417                 /* unbind pages */
418                 amdgpu_gart_unbind(adev, 0, adev->gart.num_cpu_pages);
419         }
420         adev->gart.ready = false;
421 #ifdef CONFIG_DRM_AMDGPU_GART_DEBUGFS
422         vfree(adev->gart.pages);
423         adev->gart.pages = NULL;
424 #endif
425         amdgpu_dummy_page_fini(adev);
426 }
This page took 0.056691 seconds and 4 git commands to generate.