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