1d38ceaf9SAlex Deucher /*
2d38ceaf9SAlex Deucher  * Copyright 2008 Advanced Micro Devices, Inc.
3d38ceaf9SAlex Deucher  * Copyright 2008 Red Hat Inc.
4d38ceaf9SAlex Deucher  * Copyright 2009 Jerome Glisse.
5d38ceaf9SAlex Deucher  *
6d38ceaf9SAlex Deucher  * Permission is hereby granted, free of charge, to any person obtaining a
7d38ceaf9SAlex Deucher  * copy of this software and associated documentation files (the "Software"),
8d38ceaf9SAlex Deucher  * to deal in the Software without restriction, including without limitation
9d38ceaf9SAlex Deucher  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10d38ceaf9SAlex Deucher  * and/or sell copies of the Software, and to permit persons to whom the
11d38ceaf9SAlex Deucher  * Software is furnished to do so, subject to the following conditions:
12d38ceaf9SAlex Deucher  *
13d38ceaf9SAlex Deucher  * The above copyright notice and this permission notice shall be included in
14d38ceaf9SAlex Deucher  * all copies or substantial portions of the Software.
15d38ceaf9SAlex Deucher  *
16d38ceaf9SAlex Deucher  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17d38ceaf9SAlex Deucher  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18d38ceaf9SAlex Deucher  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19d38ceaf9SAlex Deucher  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20d38ceaf9SAlex Deucher  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21d38ceaf9SAlex Deucher  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22d38ceaf9SAlex Deucher  * OTHER DEALINGS IN THE SOFTWARE.
23d38ceaf9SAlex Deucher  *
24d38ceaf9SAlex Deucher  * Authors: Dave Airlie
25d38ceaf9SAlex Deucher  *          Alex Deucher
26d38ceaf9SAlex Deucher  *          Jerome Glisse
27d38ceaf9SAlex Deucher  */
28fdf2f6c5SSam Ravnborg 
29fdf2f6c5SSam Ravnborg #include <linux/pci.h>
30fdf2f6c5SSam Ravnborg #include <linux/vmalloc.h>
31fdf2f6c5SSam Ravnborg 
32d38ceaf9SAlex Deucher #include <drm/amdgpu_drm.h>
33ed3ba079SLaura Abbott #ifdef CONFIG_X86
34ed3ba079SLaura Abbott #include <asm/set_memory.h>
35ed3ba079SLaura Abbott #endif
36d38ceaf9SAlex Deucher #include "amdgpu.h"
3718f2525dSYunxiang Li #include "amdgpu_reset.h"
38b2fe31cfSxinhui pan #include <drm/drm_drv.h>
39c9a502e9SFelix Kuehling #include <drm/ttm/ttm_tt.h>
40d38ceaf9SAlex Deucher 
41d38ceaf9SAlex Deucher /*
42d38ceaf9SAlex Deucher  * GART
43d38ceaf9SAlex Deucher  * The GART (Graphics Aperture Remapping Table) is an aperture
44d38ceaf9SAlex Deucher  * in the GPU's address space.  System pages can be mapped into
45d38ceaf9SAlex Deucher  * the aperture and look like contiguous pages from the GPU's
46d38ceaf9SAlex Deucher  * perspective.  A page table maps the pages in the aperture
47d38ceaf9SAlex Deucher  * to the actual backing pages in system memory.
48d38ceaf9SAlex Deucher  *
49d38ceaf9SAlex Deucher  * Radeon GPUs support both an internal GART, as described above,
50d38ceaf9SAlex Deucher  * and AGP.  AGP works similarly, but the GART table is configured
51d38ceaf9SAlex Deucher  * and maintained by the northbridge rather than the driver.
52d38ceaf9SAlex Deucher  * Radeon hw has a separate AGP aperture that is programmed to
53d38ceaf9SAlex Deucher  * point to the AGP aperture provided by the northbridge and the
54d38ceaf9SAlex Deucher  * requests are passed through to the northbridge aperture.
55d38ceaf9SAlex Deucher  * Both AGP and internal GART can be used at the same time, however
56d38ceaf9SAlex Deucher  * that is not currently supported by the driver.
57d38ceaf9SAlex Deucher  *
58d38ceaf9SAlex Deucher  * This file handles the common internal GART management.
59d38ceaf9SAlex Deucher  */
60d38ceaf9SAlex Deucher 
61d38ceaf9SAlex Deucher /*
62d38ceaf9SAlex Deucher  * Common GART table functions.
63d38ceaf9SAlex Deucher  */
64011d4bbeSChristian König 
65011d4bbeSChristian König /**
66777d9000SLee Jones  * amdgpu_gart_dummy_page_init - init dummy page used by the driver
6755e0037aSAlex Deucher  *
6855e0037aSAlex Deucher  * @adev: amdgpu_device pointer
6955e0037aSAlex Deucher  *
7055e0037aSAlex Deucher  * Allocate the dummy page used by the driver (all asics).
7155e0037aSAlex Deucher  * This dummy page is used by the driver as a filler for gart entries
7255e0037aSAlex Deucher  * when pages are taken out of the GART
7355e0037aSAlex Deucher  * Returns 0 on sucess, -ENOMEM on failure.
7455e0037aSAlex Deucher  */
amdgpu_gart_dummy_page_init(struct amdgpu_device * adev)7555e0037aSAlex Deucher static int amdgpu_gart_dummy_page_init(struct amdgpu_device *adev)
7655e0037aSAlex Deucher {
778af8a109SChristian König 	struct page *dummy_page = ttm_glob.dummy_read_page;
7892e71b06SChristian König 
7992e71b06SChristian König 	if (adev->dummy_page_addr)
8055e0037aSAlex Deucher 		return 0;
81*b7861293SPrike Liang 	adev->dummy_page_addr = dma_map_page_attrs(&adev->pdev->dev, dummy_page, 0,
82*b7861293SPrike Liang 							PAGE_SIZE, DMA_BIDIRECTIONAL,
83*b7861293SPrike Liang 							DMA_ATTR_SKIP_CPU_SYNC);
847dc7b65aSNirmoy Das 	if (dma_mapping_error(&adev->pdev->dev, adev->dummy_page_addr)) {
8555e0037aSAlex Deucher 		dev_err(&adev->pdev->dev, "Failed to DMA MAP the dummy page\n");
8692e71b06SChristian König 		adev->dummy_page_addr = 0;
8755e0037aSAlex Deucher 		return -ENOMEM;
8855e0037aSAlex Deucher 	}
8955e0037aSAlex Deucher 	return 0;
9055e0037aSAlex Deucher }
9155e0037aSAlex Deucher 
9255e0037aSAlex Deucher /**
93777d9000SLee Jones  * amdgpu_gart_dummy_page_fini - free dummy page used by the driver
9455e0037aSAlex Deucher  *
9555e0037aSAlex Deucher  * @adev: amdgpu_device pointer
9655e0037aSAlex Deucher  *
9755e0037aSAlex Deucher  * Frees the dummy page used by the driver (all asics).
9855e0037aSAlex Deucher  */
amdgpu_gart_dummy_page_fini(struct amdgpu_device * adev)99d10d0daaSAndrey Grodzovsky void amdgpu_gart_dummy_page_fini(struct amdgpu_device *adev)
10055e0037aSAlex Deucher {
10192e71b06SChristian König 	if (!adev->dummy_page_addr)
10255e0037aSAlex Deucher 		return;
103*b7861293SPrike Liang 	dma_unmap_page_attrs(&adev->pdev->dev, adev->dummy_page_addr, PAGE_SIZE,
104*b7861293SPrike Liang 				DMA_BIDIRECTIONAL,
105*b7861293SPrike Liang 				DMA_ATTR_SKIP_CPU_SYNC);
10692e71b06SChristian König 	adev->dummy_page_addr = 0;
10755e0037aSAlex Deucher }
10855e0037aSAlex Deucher 
10955e0037aSAlex Deucher /**
110c9a502e9SFelix Kuehling  * amdgpu_gart_table_ram_alloc - allocate system ram for gart page table
111c9a502e9SFelix Kuehling  *
112c9a502e9SFelix Kuehling  * @adev: amdgpu_device pointer
113c9a502e9SFelix Kuehling  *
114c9a502e9SFelix Kuehling  * Allocate system memory for GART page table for ASICs that don't have
115c9a502e9SFelix Kuehling  * dedicated VRAM.
116c9a502e9SFelix Kuehling  * Returns 0 for success, error for failure.
117c9a502e9SFelix Kuehling  */
amdgpu_gart_table_ram_alloc(struct amdgpu_device * adev)118c9a502e9SFelix Kuehling int amdgpu_gart_table_ram_alloc(struct amdgpu_device *adev)
119c9a502e9SFelix Kuehling {
120c9a502e9SFelix Kuehling 	unsigned int order = get_order(adev->gart.table_size);
121c9a502e9SFelix Kuehling 	gfp_t gfp_flags = GFP_KERNEL | __GFP_ZERO;
122c9a502e9SFelix Kuehling 	struct amdgpu_bo *bo = NULL;
123c9a502e9SFelix Kuehling 	struct sg_table *sg = NULL;
124c9a502e9SFelix Kuehling 	struct amdgpu_bo_param bp;
125c9a502e9SFelix Kuehling 	dma_addr_t dma_addr;
126c9a502e9SFelix Kuehling 	struct page *p;
127e7a8594cSTom St Denis 	unsigned long x;
128c9a502e9SFelix Kuehling 	int ret;
129c9a502e9SFelix Kuehling 
130c9a502e9SFelix Kuehling 	if (adev->gart.bo != NULL)
131c9a502e9SFelix Kuehling 		return 0;
132c9a502e9SFelix Kuehling 
133c9a502e9SFelix Kuehling 	p = alloc_pages(gfp_flags, order);
134c9a502e9SFelix Kuehling 	if (!p)
135c9a502e9SFelix Kuehling 		return -ENOMEM;
136c9a502e9SFelix Kuehling 
137e7a8594cSTom St Denis 	/* assign pages to this device */
138e7a8594cSTom St Denis 	for (x = 0; x < (1UL << order); x++)
139e7a8594cSTom St Denis 		p[x].mapping = adev->mman.bdev.dev_mapping;
140e7a8594cSTom St Denis 
141c9a502e9SFelix Kuehling 	/* If the hardware does not support UTCL2 snooping of the CPU caches
142c9a502e9SFelix Kuehling 	 * then set_memory_wc() could be used as a workaround to mark the pages
143c9a502e9SFelix Kuehling 	 * as write combine memory.
144c9a502e9SFelix Kuehling 	 */
145c9a502e9SFelix Kuehling 	dma_addr = dma_map_page(&adev->pdev->dev, p, 0, adev->gart.table_size,
146c9a502e9SFelix Kuehling 				DMA_BIDIRECTIONAL);
147c9a502e9SFelix Kuehling 	if (dma_mapping_error(&adev->pdev->dev, dma_addr)) {
148c9a502e9SFelix Kuehling 		dev_err(&adev->pdev->dev, "Failed to DMA MAP the GART BO page\n");
149c9a502e9SFelix Kuehling 		__free_pages(p, order);
150c9a502e9SFelix Kuehling 		p = NULL;
151c9a502e9SFelix Kuehling 		return -EFAULT;
152c9a502e9SFelix Kuehling 	}
153c9a502e9SFelix Kuehling 
1541f9bb94fSArnd Bergmann 	dev_info(adev->dev, "%s dma_addr:%pad\n", __func__, &dma_addr);
155c9a502e9SFelix Kuehling 	/* Create SG table */
156c9a502e9SFelix Kuehling 	sg = kmalloc(sizeof(*sg), GFP_KERNEL);
157c9a502e9SFelix Kuehling 	if (!sg) {
158c9a502e9SFelix Kuehling 		ret = -ENOMEM;
159c9a502e9SFelix Kuehling 		goto error;
160c9a502e9SFelix Kuehling 	}
161c9a502e9SFelix Kuehling 	ret = sg_alloc_table(sg, 1, GFP_KERNEL);
162c9a502e9SFelix Kuehling 	if (ret)
163c9a502e9SFelix Kuehling 		goto error;
164c9a502e9SFelix Kuehling 
165c9a502e9SFelix Kuehling 	sg_dma_address(sg->sgl) = dma_addr;
166c9a502e9SFelix Kuehling 	sg->sgl->length = adev->gart.table_size;
167c9a502e9SFelix Kuehling #ifdef CONFIG_NEED_SG_DMA_LENGTH
168c9a502e9SFelix Kuehling 	sg->sgl->dma_length = adev->gart.table_size;
169c9a502e9SFelix Kuehling #endif
170c9a502e9SFelix Kuehling 	/* Create SG BO */
171c9a502e9SFelix Kuehling 	memset(&bp, 0, sizeof(bp));
172c9a502e9SFelix Kuehling 	bp.size = adev->gart.table_size;
173c9a502e9SFelix Kuehling 	bp.byte_align = PAGE_SIZE;
174c9a502e9SFelix Kuehling 	bp.domain = AMDGPU_GEM_DOMAIN_CPU;
175c9a502e9SFelix Kuehling 	bp.type = ttm_bo_type_sg;
176c9a502e9SFelix Kuehling 	bp.resv = NULL;
177c9a502e9SFelix Kuehling 	bp.bo_ptr_size = sizeof(struct amdgpu_bo);
178c9a502e9SFelix Kuehling 	bp.flags = 0;
179c9a502e9SFelix Kuehling 	ret = amdgpu_bo_create(adev, &bp, &bo);
180c9a502e9SFelix Kuehling 	if (ret)
181c9a502e9SFelix Kuehling 		goto error;
182c9a502e9SFelix Kuehling 
183c9a502e9SFelix Kuehling 	bo->tbo.sg = sg;
184c9a502e9SFelix Kuehling 	bo->tbo.ttm->sg = sg;
185c9a502e9SFelix Kuehling 	bo->allowed_domains = AMDGPU_GEM_DOMAIN_GTT;
186c9a502e9SFelix Kuehling 	bo->preferred_domains = AMDGPU_GEM_DOMAIN_GTT;
187c9a502e9SFelix Kuehling 
188c9a502e9SFelix Kuehling 	ret = amdgpu_bo_reserve(bo, true);
189c9a502e9SFelix Kuehling 	if (ret) {
190c9a502e9SFelix Kuehling 		dev_err(adev->dev, "(%d) failed to reserve bo for GART system bo\n", ret);
191c9a502e9SFelix Kuehling 		goto error;
192c9a502e9SFelix Kuehling 	}
193c9a502e9SFelix Kuehling 
194c9a502e9SFelix Kuehling 	ret = amdgpu_bo_pin(bo, AMDGPU_GEM_DOMAIN_GTT);
195c9a502e9SFelix Kuehling 	WARN(ret, "Pinning the GART table failed");
196c9a502e9SFelix Kuehling 	if (ret)
197c9a502e9SFelix Kuehling 		goto error_resv;
198c9a502e9SFelix Kuehling 
199c9a502e9SFelix Kuehling 	adev->gart.bo = bo;
200c9a502e9SFelix Kuehling 	adev->gart.ptr = page_to_virt(p);
201c9a502e9SFelix Kuehling 	/* Make GART table accessible in VMID0 */
202c9a502e9SFelix Kuehling 	ret = amdgpu_ttm_alloc_gart(&adev->gart.bo->tbo);
203c9a502e9SFelix Kuehling 	if (ret)
204c9a502e9SFelix Kuehling 		amdgpu_gart_table_ram_free(adev);
205c9a502e9SFelix Kuehling 	amdgpu_bo_unreserve(bo);
206c9a502e9SFelix Kuehling 
207c9a502e9SFelix Kuehling 	return 0;
208c9a502e9SFelix Kuehling 
209c9a502e9SFelix Kuehling error_resv:
210c9a502e9SFelix Kuehling 	amdgpu_bo_unreserve(bo);
211c9a502e9SFelix Kuehling error:
212c9a502e9SFelix Kuehling 	amdgpu_bo_unref(&bo);
213c9a502e9SFelix Kuehling 	if (sg) {
214c9a502e9SFelix Kuehling 		sg_free_table(sg);
215c9a502e9SFelix Kuehling 		kfree(sg);
216c9a502e9SFelix Kuehling 	}
217c9a502e9SFelix Kuehling 	__free_pages(p, order);
218c9a502e9SFelix Kuehling 	return ret;
219c9a502e9SFelix Kuehling }
220c9a502e9SFelix Kuehling 
221c9a502e9SFelix Kuehling /**
222c9a502e9SFelix Kuehling  * amdgpu_gart_table_ram_free - free gart page table system ram
223c9a502e9SFelix Kuehling  *
224c9a502e9SFelix Kuehling  * @adev: amdgpu_device pointer
225c9a502e9SFelix Kuehling  *
226c9a502e9SFelix Kuehling  * Free the system memory used for the GART page tableon ASICs that don't
227c9a502e9SFelix Kuehling  * have dedicated VRAM.
228c9a502e9SFelix Kuehling  */
amdgpu_gart_table_ram_free(struct amdgpu_device * adev)229c9a502e9SFelix Kuehling void amdgpu_gart_table_ram_free(struct amdgpu_device *adev)
230c9a502e9SFelix Kuehling {
231c9a502e9SFelix Kuehling 	unsigned int order = get_order(adev->gart.table_size);
232c9a502e9SFelix Kuehling 	struct sg_table *sg = adev->gart.bo->tbo.sg;
233c9a502e9SFelix Kuehling 	struct page *p;
234e7a8594cSTom St Denis 	unsigned long x;
235c9a502e9SFelix Kuehling 	int ret;
236c9a502e9SFelix Kuehling 
237c9a502e9SFelix Kuehling 	ret = amdgpu_bo_reserve(adev->gart.bo, false);
238c9a502e9SFelix Kuehling 	if (!ret) {
239c9a502e9SFelix Kuehling 		amdgpu_bo_unpin(adev->gart.bo);
240c9a502e9SFelix Kuehling 		amdgpu_bo_unreserve(adev->gart.bo);
241c9a502e9SFelix Kuehling 	}
242c9a502e9SFelix Kuehling 	amdgpu_bo_unref(&adev->gart.bo);
243c9a502e9SFelix Kuehling 	sg_free_table(sg);
244c9a502e9SFelix Kuehling 	kfree(sg);
245c9a502e9SFelix Kuehling 	p = virt_to_page(adev->gart.ptr);
246e7a8594cSTom St Denis 	for (x = 0; x < (1UL << order); x++)
247e7a8594cSTom St Denis 		p[x].mapping = NULL;
248c9a502e9SFelix Kuehling 	__free_pages(p, order);
249c9a502e9SFelix Kuehling 
250c9a502e9SFelix Kuehling 	adev->gart.ptr = NULL;
251c9a502e9SFelix Kuehling }
252c9a502e9SFelix Kuehling 
253c9a502e9SFelix Kuehling /**
254d38ceaf9SAlex Deucher  * amdgpu_gart_table_vram_alloc - allocate vram for gart page table
255d38ceaf9SAlex Deucher  *
256d38ceaf9SAlex Deucher  * @adev: amdgpu_device pointer
257d38ceaf9SAlex Deucher  *
258d38ceaf9SAlex Deucher  * Allocate video memory for GART page table
259d38ceaf9SAlex Deucher  * (pcie r4xx, r5xx+).  These asics require the
260d38ceaf9SAlex Deucher  * gart table to be in video memory.
261d38ceaf9SAlex Deucher  * Returns 0 for success, error for failure.
262d38ceaf9SAlex Deucher  */
amdgpu_gart_table_vram_alloc(struct amdgpu_device * adev)263d38ceaf9SAlex Deucher int amdgpu_gart_table_vram_alloc(struct amdgpu_device *adev)
264d38ceaf9SAlex Deucher {
265575e55eeSNirmoy Das 	if (adev->gart.bo != NULL)
266ce1b1b66SMonk Liu 		return 0;
267ce1b1b66SMonk Liu 
268575e55eeSNirmoy Das 	return amdgpu_bo_create_kernel(adev,  adev->gart.table_size, PAGE_SIZE,
269575e55eeSNirmoy Das 				       AMDGPU_GEM_DOMAIN_VRAM, &adev->gart.bo,
270575e55eeSNirmoy Das 				       NULL, (void *)&adev->gart.ptr);
271d38ceaf9SAlex Deucher }
272d38ceaf9SAlex Deucher 
273d38ceaf9SAlex Deucher /**
274d38ceaf9SAlex Deucher  * amdgpu_gart_table_vram_free - free gart page table vram
275d38ceaf9SAlex Deucher  *
276d38ceaf9SAlex Deucher  * @adev: amdgpu_device pointer
277d38ceaf9SAlex Deucher  *
278d38ceaf9SAlex Deucher  * Free the video memory used for the GART page table
279d38ceaf9SAlex Deucher  * (pcie r4xx, r5xx+).  These asics require the gart table to
280d38ceaf9SAlex Deucher  * be in video memory.
281d38ceaf9SAlex Deucher  */
amdgpu_gart_table_vram_free(struct amdgpu_device * adev)282d38ceaf9SAlex Deucher void amdgpu_gart_table_vram_free(struct amdgpu_device *adev)
283d38ceaf9SAlex Deucher {
284575e55eeSNirmoy Das 	amdgpu_bo_free_kernel(&adev->gart.bo, NULL, (void *)&adev->gart.ptr);
285d38ceaf9SAlex Deucher }
286d38ceaf9SAlex Deucher 
287d38ceaf9SAlex Deucher /*
288d38ceaf9SAlex Deucher  * Common gart functions.
289d38ceaf9SAlex Deucher  */
290d38ceaf9SAlex Deucher /**
291d38ceaf9SAlex Deucher  * amdgpu_gart_unbind - unbind pages from the gart page table
292d38ceaf9SAlex Deucher  *
293d38ceaf9SAlex Deucher  * @adev: amdgpu_device pointer
294d38ceaf9SAlex Deucher  * @offset: offset into the GPU's gart aperture
295d38ceaf9SAlex Deucher  * @pages: number of pages to unbind
296d38ceaf9SAlex Deucher  *
297d38ceaf9SAlex Deucher  * Unbinds the requested pages from the gart page table and
298d38ceaf9SAlex Deucher  * replaces them with the dummy page (all asics).
299738f64ccSRoger.He  * Returns 0 for success, -EINVAL for failure.
300d38ceaf9SAlex Deucher  */
amdgpu_gart_unbind(struct amdgpu_device * adev,uint64_t offset,int pages)3011b08dfb8SChristian König void amdgpu_gart_unbind(struct amdgpu_device *adev, uint64_t offset,
302d38ceaf9SAlex Deucher 			int pages)
303d38ceaf9SAlex Deucher {
304d38ceaf9SAlex Deucher 	unsigned t;
305d38ceaf9SAlex Deucher 	unsigned p;
306d38ceaf9SAlex Deucher 	int i, j;
307d38ceaf9SAlex Deucher 	u64 page_base;
308a0676f60SAlex Deucher 	/* Starting from VEGA10, system bit must be 0 to mean invalid. */
309a0676f60SAlex Deucher 	uint64_t flags = 0;
310b2fe31cfSxinhui pan 	int idx;
311d38ceaf9SAlex Deucher 
312f9130b81SGuchun Chen 	if (!adev->gart.ptr)
3131b08dfb8SChristian König 		return;
314d38ceaf9SAlex Deucher 
315c58a863bSGuchun Chen 	if (!drm_dev_enter(adev_to_drm(adev), &idx))
3161b08dfb8SChristian König 		return;
317b2fe31cfSxinhui pan 
318d38ceaf9SAlex Deucher 	t = offset / AMDGPU_GPU_PAGE_SIZE;
319463d2fe8SMichel Dänzer 	p = t / AMDGPU_GPU_PAGES_IN_CPU_PAGE;
320d38ceaf9SAlex Deucher 	for (i = 0; i < pages; i++, p++) {
32192e71b06SChristian König 		page_base = adev->dummy_page_addr;
322d38ceaf9SAlex Deucher 		if (!adev->gart.ptr)
323d38ceaf9SAlex Deucher 			continue;
324d38ceaf9SAlex Deucher 
325463d2fe8SMichel Dänzer 		for (j = 0; j < AMDGPU_GPU_PAGES_IN_CPU_PAGE; j++, t++) {
326132f34e4SChristian König 			amdgpu_gmc_set_pte_pde(adev, adev->gart.ptr,
327d38ceaf9SAlex Deucher 					       t, page_base, flags);
328d38ceaf9SAlex Deucher 			page_base += AMDGPU_GPU_PAGE_SIZE;
329d38ceaf9SAlex Deucher 		}
330d38ceaf9SAlex Deucher 	}
331c1f9d82bSYunxiang Li 	amdgpu_gart_invalidate_tlb(adev);
3323ff98548SOak Zeng 
333b2fe31cfSxinhui pan 	drm_dev_exit(idx);
334d38ceaf9SAlex Deucher }
335d38ceaf9SAlex Deucher 
336d38ceaf9SAlex Deucher /**
3370c2c421eSChristian König  * amdgpu_gart_map - map dma_addresses into GART entries
3380c2c421eSChristian König  *
3390c2c421eSChristian König  * @adev: amdgpu_device pointer
3400c2c421eSChristian König  * @offset: offset into the GPU's gart aperture
3410c2c421eSChristian König  * @pages: number of pages to bind
3420c2c421eSChristian König  * @dma_addr: DMA addresses of pages
3435dcb668dSOak Zeng  * @flags: page table entry flags
3445dcb668dSOak Zeng  * @dst: CPU address of the gart table
3450c2c421eSChristian König  *
3460c2c421eSChristian König  * Map the dma_addresses into GART entries (all asics).
3470c2c421eSChristian König  * Returns 0 for success, -EINVAL for failure.
3480c2c421eSChristian König  */
amdgpu_gart_map(struct amdgpu_device * adev,uint64_t offset,int pages,dma_addr_t * dma_addr,uint64_t flags,void * dst)3491b08dfb8SChristian König void amdgpu_gart_map(struct amdgpu_device *adev, uint64_t offset,
3500c2c421eSChristian König 		    int pages, dma_addr_t *dma_addr, uint64_t flags,
3510c2c421eSChristian König 		    void *dst)
3520c2c421eSChristian König {
3530c2c421eSChristian König 	uint64_t page_base;
3540c2c421eSChristian König 	unsigned i, j, t;
355b2fe31cfSxinhui pan 	int idx;
3560c2c421eSChristian König 
357c58a863bSGuchun Chen 	if (!drm_dev_enter(adev_to_drm(adev), &idx))
3581b08dfb8SChristian König 		return;
359b2fe31cfSxinhui pan 
3600c2c421eSChristian König 	t = offset / AMDGPU_GPU_PAGE_SIZE;
3610c2c421eSChristian König 
3620c2c421eSChristian König 	for (i = 0; i < pages; i++) {
3630c2c421eSChristian König 		page_base = dma_addr[i];
364463d2fe8SMichel Dänzer 		for (j = 0; j < AMDGPU_GPU_PAGES_IN_CPU_PAGE; j++, t++) {
365132f34e4SChristian König 			amdgpu_gmc_set_pte_pde(adev, dst, t, page_base, flags);
3660c2c421eSChristian König 			page_base += AMDGPU_GPU_PAGE_SIZE;
3670c2c421eSChristian König 		}
3680c2c421eSChristian König 	}
369b2fe31cfSxinhui pan 	drm_dev_exit(idx);
3700c2c421eSChristian König }
3710c2c421eSChristian König 
3720c2c421eSChristian König /**
373d38ceaf9SAlex Deucher  * amdgpu_gart_bind - bind pages into the gart page table
374d38ceaf9SAlex Deucher  *
375d38ceaf9SAlex Deucher  * @adev: amdgpu_device pointer
376d38ceaf9SAlex Deucher  * @offset: offset into the GPU's gart aperture
377d38ceaf9SAlex Deucher  * @pages: number of pages to bind
378d38ceaf9SAlex Deucher  * @dma_addr: DMA addresses of pages
379e8b74035Syu kuai  * @flags: page table entry flags
380d38ceaf9SAlex Deucher  *
381d38ceaf9SAlex Deucher  * Binds the requested pages to the gart page table
382d38ceaf9SAlex Deucher  * (all asics).
383d38ceaf9SAlex Deucher  * Returns 0 for success, -EINVAL for failure.
384d38ceaf9SAlex Deucher  */
amdgpu_gart_bind(struct amdgpu_device * adev,uint64_t offset,int pages,dma_addr_t * dma_addr,uint64_t flags)3851b08dfb8SChristian König void amdgpu_gart_bind(struct amdgpu_device *adev, uint64_t offset,
386942ab769SYifan Zhang 		     int pages, dma_addr_t *dma_addr,
3876b777607SChunming Zhou 		     uint64_t flags)
388d38ceaf9SAlex Deucher {
389f9130b81SGuchun Chen 	if (!adev->gart.ptr)
3901b08dfb8SChristian König 		return;
391d38ceaf9SAlex Deucher 
3921b08dfb8SChristian König 	amdgpu_gart_map(adev, offset, pages, dma_addr, flags, adev->gart.ptr);
39319a1d935SNirmoy Das }
39419a1d935SNirmoy Das 
39519a1d935SNirmoy Das /**
39619a1d935SNirmoy Das  * amdgpu_gart_invalidate_tlb - invalidate gart TLB
39719a1d935SNirmoy Das  *
39819a1d935SNirmoy Das  * @adev: amdgpu device driver pointer
39919a1d935SNirmoy Das  *
40019a1d935SNirmoy Das  * Invalidate gart TLB which can be use as a way to flush gart changes
40119a1d935SNirmoy Das  *
40219a1d935SNirmoy Das  */
amdgpu_gart_invalidate_tlb(struct amdgpu_device * adev)40319a1d935SNirmoy Das void amdgpu_gart_invalidate_tlb(struct amdgpu_device *adev)
40419a1d935SNirmoy Das {
40519a1d935SNirmoy Das 	int i;
4060c2c421eSChristian König 
40729ba7b16SAaron Liu 	if (!adev->gart.ptr)
40829ba7b16SAaron Liu 		return;
40929ba7b16SAaron Liu 
410d38ceaf9SAlex Deucher 	mb();
41118f2525dSYunxiang Li 	if (down_read_trylock(&adev->reset_domain->sem)) {
412810085ddSEric Huang 		amdgpu_device_flush_hdp(adev, NULL);
41318f2525dSYunxiang Li 		up_read(&adev->reset_domain->sem);
41418f2525dSYunxiang Li 	}
415d9426c3dSLe Ma 	for_each_set_bit(i, adev->vmhubs_mask, AMDGPU_MAX_VMHUBS)
4163ff98548SOak Zeng 		amdgpu_gmc_flush_gpu_tlb(adev, 0, i, 0);
417d38ceaf9SAlex Deucher }
418d38ceaf9SAlex Deucher 
419d38ceaf9SAlex Deucher /**
420d38ceaf9SAlex Deucher  * amdgpu_gart_init - init the driver info for managing the gart
421d38ceaf9SAlex Deucher  *
422d38ceaf9SAlex Deucher  * @adev: amdgpu_device pointer
423d38ceaf9SAlex Deucher  *
424d38ceaf9SAlex Deucher  * Allocate the dummy page and init the gart driver info (all asics).
425d38ceaf9SAlex Deucher  * Returns 0 for success, error for failure.
426d38ceaf9SAlex Deucher  */
amdgpu_gart_init(struct amdgpu_device * adev)427d38ceaf9SAlex Deucher int amdgpu_gart_init(struct amdgpu_device *adev)
428d38ceaf9SAlex Deucher {
42943251981SChristian König 	int r;
430d38ceaf9SAlex Deucher 
43192e71b06SChristian König 	if (adev->dummy_page_addr)
432d38ceaf9SAlex Deucher 		return 0;
433a1d29476SChristian König 
434d38ceaf9SAlex Deucher 	/* We need PAGE_SIZE >= AMDGPU_GPU_PAGE_SIZE */
435d38ceaf9SAlex Deucher 	if (PAGE_SIZE < AMDGPU_GPU_PAGE_SIZE) {
436d38ceaf9SAlex Deucher 		DRM_ERROR("Page size is smaller than GPU page size!\n");
437d38ceaf9SAlex Deucher 		return -EINVAL;
438d38ceaf9SAlex Deucher 	}
43955e0037aSAlex Deucher 	r = amdgpu_gart_dummy_page_init(adev);
440d38ceaf9SAlex Deucher 	if (r)
441d38ceaf9SAlex Deucher 		return r;
442d38ceaf9SAlex Deucher 	/* Compute table size */
443770d13b1SChristian König 	adev->gart.num_cpu_pages = adev->gmc.gart_size / PAGE_SIZE;
444770d13b1SChristian König 	adev->gart.num_gpu_pages = adev->gmc.gart_size / AMDGPU_GPU_PAGE_SIZE;
445d38ceaf9SAlex Deucher 	DRM_INFO("GART: num cpu pages %u, num gpu pages %u\n",
446d38ceaf9SAlex Deucher 		 adev->gart.num_cpu_pages, adev->gart.num_gpu_pages);
447a1d29476SChristian König 
448d38ceaf9SAlex Deucher 	return 0;
449d38ceaf9SAlex Deucher }
450