xref: /dflybsd-src/sys/dev/drm/amd/amdgpu/amdgpu_test.c (revision b843c749addef9340ee7d4e250b09fdd492602a1)
1*b843c749SSergey Zigachev // SPDX-License-Identifier: GPL-2.0 OR MIT
2*b843c749SSergey Zigachev /*
3*b843c749SSergey Zigachev  * Copyright 2009 VMware, Inc.
4*b843c749SSergey Zigachev  *
5*b843c749SSergey Zigachev  * Permission is hereby granted, free of charge, to any person obtaining a
6*b843c749SSergey Zigachev  * copy of this software and associated documentation files (the "Software"),
7*b843c749SSergey Zigachev  * to deal in the Software without restriction, including without limitation
8*b843c749SSergey Zigachev  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9*b843c749SSergey Zigachev  * and/or sell copies of the Software, and to permit persons to whom the
10*b843c749SSergey Zigachev  * Software is furnished to do so, subject to the following conditions:
11*b843c749SSergey Zigachev  *
12*b843c749SSergey Zigachev  * The above copyright notice and this permission notice shall be included in
13*b843c749SSergey Zigachev  * all copies or substantial portions of the Software.
14*b843c749SSergey Zigachev  *
15*b843c749SSergey Zigachev  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16*b843c749SSergey Zigachev  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17*b843c749SSergey Zigachev  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18*b843c749SSergey Zigachev  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19*b843c749SSergey Zigachev  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20*b843c749SSergey Zigachev  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21*b843c749SSergey Zigachev  * OTHER DEALINGS IN THE SOFTWARE.
22*b843c749SSergey Zigachev  *
23*b843c749SSergey Zigachev  * Authors: Michel Dänzer
24*b843c749SSergey Zigachev  */
25*b843c749SSergey Zigachev #include <drm/drmP.h>
26*b843c749SSergey Zigachev #include <drm/amdgpu_drm.h>
27*b843c749SSergey Zigachev #include "amdgpu.h"
28*b843c749SSergey Zigachev #include "amdgpu_uvd.h"
29*b843c749SSergey Zigachev #include "amdgpu_vce.h"
30*b843c749SSergey Zigachev 
31*b843c749SSergey Zigachev /* Test BO GTT->VRAM and VRAM->GTT GPU copies across the whole GTT aperture */
amdgpu_do_test_moves(struct amdgpu_device * adev)32*b843c749SSergey Zigachev static void amdgpu_do_test_moves(struct amdgpu_device *adev)
33*b843c749SSergey Zigachev {
34*b843c749SSergey Zigachev 	struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
35*b843c749SSergey Zigachev 	struct amdgpu_bo *vram_obj = NULL;
36*b843c749SSergey Zigachev 	struct amdgpu_bo **gtt_obj = NULL;
37*b843c749SSergey Zigachev 	struct amdgpu_bo_param bp;
38*b843c749SSergey Zigachev 	uint64_t gart_addr, vram_addr;
39*b843c749SSergey Zigachev 	unsigned n, size;
40*b843c749SSergey Zigachev 	int i, r;
41*b843c749SSergey Zigachev 
42*b843c749SSergey Zigachev 	size = 1024 * 1024;
43*b843c749SSergey Zigachev 
44*b843c749SSergey Zigachev 	/* Number of tests =
45*b843c749SSergey Zigachev 	 * (Total GTT - IB pool - writeback page - ring buffers) / test size
46*b843c749SSergey Zigachev 	 */
47*b843c749SSergey Zigachev 	n = adev->gmc.gart_size - AMDGPU_IB_POOL_SIZE*64*1024;
48*b843c749SSergey Zigachev 	for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
49*b843c749SSergey Zigachev 		if (adev->rings[i])
50*b843c749SSergey Zigachev 			n -= adev->rings[i]->ring_size;
51*b843c749SSergey Zigachev 	if (adev->wb.wb_obj)
52*b843c749SSergey Zigachev 		n -= AMDGPU_GPU_PAGE_SIZE;
53*b843c749SSergey Zigachev 	if (adev->irq.ih.ring_obj)
54*b843c749SSergey Zigachev 		n -= adev->irq.ih.ring_size;
55*b843c749SSergey Zigachev 	n /= size;
56*b843c749SSergey Zigachev 
57*b843c749SSergey Zigachev 	gtt_obj = kcalloc(n, sizeof(*gtt_obj), GFP_KERNEL);
58*b843c749SSergey Zigachev 	if (!gtt_obj) {
59*b843c749SSergey Zigachev 		DRM_ERROR("Failed to allocate %d pointers\n", n);
60*b843c749SSergey Zigachev 		r = 1;
61*b843c749SSergey Zigachev 		goto out_cleanup;
62*b843c749SSergey Zigachev 	}
63*b843c749SSergey Zigachev 	memset(&bp, 0, sizeof(bp));
64*b843c749SSergey Zigachev 	bp.size = size;
65*b843c749SSergey Zigachev 	bp.byte_align = PAGE_SIZE;
66*b843c749SSergey Zigachev 	bp.domain = AMDGPU_GEM_DOMAIN_VRAM;
67*b843c749SSergey Zigachev 	bp.flags = 0;
68*b843c749SSergey Zigachev 	bp.type = ttm_bo_type_kernel;
69*b843c749SSergey Zigachev 	bp.resv = NULL;
70*b843c749SSergey Zigachev 
71*b843c749SSergey Zigachev 	r = amdgpu_bo_create(adev, &bp, &vram_obj);
72*b843c749SSergey Zigachev 	if (r) {
73*b843c749SSergey Zigachev 		DRM_ERROR("Failed to create VRAM object\n");
74*b843c749SSergey Zigachev 		goto out_cleanup;
75*b843c749SSergey Zigachev 	}
76*b843c749SSergey Zigachev 	r = amdgpu_bo_reserve(vram_obj, false);
77*b843c749SSergey Zigachev 	if (unlikely(r != 0))
78*b843c749SSergey Zigachev 		goto out_unref;
79*b843c749SSergey Zigachev 	r = amdgpu_bo_pin(vram_obj, AMDGPU_GEM_DOMAIN_VRAM);
80*b843c749SSergey Zigachev 	if (r) {
81*b843c749SSergey Zigachev 		DRM_ERROR("Failed to pin VRAM object\n");
82*b843c749SSergey Zigachev 		goto out_unres;
83*b843c749SSergey Zigachev 	}
84*b843c749SSergey Zigachev 	vram_addr = amdgpu_bo_gpu_offset(vram_obj);
85*b843c749SSergey Zigachev 	for (i = 0; i < n; i++) {
86*b843c749SSergey Zigachev 		void *gtt_map, *vram_map;
87*b843c749SSergey Zigachev 		void **gart_start, **gart_end;
88*b843c749SSergey Zigachev 		void **vram_start, **vram_end;
89*b843c749SSergey Zigachev 		struct dma_fence *fence = NULL;
90*b843c749SSergey Zigachev 
91*b843c749SSergey Zigachev 		bp.domain = AMDGPU_GEM_DOMAIN_GTT;
92*b843c749SSergey Zigachev 		r = amdgpu_bo_create(adev, &bp, gtt_obj + i);
93*b843c749SSergey Zigachev 		if (r) {
94*b843c749SSergey Zigachev 			DRM_ERROR("Failed to create GTT object %d\n", i);
95*b843c749SSergey Zigachev 			goto out_lclean;
96*b843c749SSergey Zigachev 		}
97*b843c749SSergey Zigachev 
98*b843c749SSergey Zigachev 		r = amdgpu_bo_reserve(gtt_obj[i], false);
99*b843c749SSergey Zigachev 		if (unlikely(r != 0))
100*b843c749SSergey Zigachev 			goto out_lclean_unref;
101*b843c749SSergey Zigachev 		r = amdgpu_bo_pin(gtt_obj[i], AMDGPU_GEM_DOMAIN_GTT);
102*b843c749SSergey Zigachev 		if (r) {
103*b843c749SSergey Zigachev 			DRM_ERROR("Failed to pin GTT object %d\n", i);
104*b843c749SSergey Zigachev 			goto out_lclean_unres;
105*b843c749SSergey Zigachev 		}
106*b843c749SSergey Zigachev 		r = amdgpu_ttm_alloc_gart(&gtt_obj[i]->tbo);
107*b843c749SSergey Zigachev 		if (r) {
108*b843c749SSergey Zigachev 			DRM_ERROR("%p bind failed\n", gtt_obj[i]);
109*b843c749SSergey Zigachev 			goto out_lclean_unpin;
110*b843c749SSergey Zigachev 		}
111*b843c749SSergey Zigachev 		gart_addr = amdgpu_bo_gpu_offset(gtt_obj[i]);
112*b843c749SSergey Zigachev 
113*b843c749SSergey Zigachev 		r = amdgpu_bo_kmap(gtt_obj[i], &gtt_map);
114*b843c749SSergey Zigachev 		if (r) {
115*b843c749SSergey Zigachev 			DRM_ERROR("Failed to map GTT object %d\n", i);
116*b843c749SSergey Zigachev 			goto out_lclean_unpin;
117*b843c749SSergey Zigachev 		}
118*b843c749SSergey Zigachev 
119*b843c749SSergey Zigachev 		for (gart_start = gtt_map, gart_end = gtt_map + size;
120*b843c749SSergey Zigachev 		     gart_start < gart_end;
121*b843c749SSergey Zigachev 		     gart_start++)
122*b843c749SSergey Zigachev 			*gart_start = gart_start;
123*b843c749SSergey Zigachev 
124*b843c749SSergey Zigachev 		amdgpu_bo_kunmap(gtt_obj[i]);
125*b843c749SSergey Zigachev 
126*b843c749SSergey Zigachev 		r = amdgpu_copy_buffer(ring, gart_addr, vram_addr,
127*b843c749SSergey Zigachev 				       size, NULL, &fence, false, false);
128*b843c749SSergey Zigachev 
129*b843c749SSergey Zigachev 		if (r) {
130*b843c749SSergey Zigachev 			DRM_ERROR("Failed GTT->VRAM copy %d\n", i);
131*b843c749SSergey Zigachev 			goto out_lclean_unpin;
132*b843c749SSergey Zigachev 		}
133*b843c749SSergey Zigachev 
134*b843c749SSergey Zigachev 		r = dma_fence_wait(fence, false);
135*b843c749SSergey Zigachev 		if (r) {
136*b843c749SSergey Zigachev 			DRM_ERROR("Failed to wait for GTT->VRAM fence %d\n", i);
137*b843c749SSergey Zigachev 			goto out_lclean_unpin;
138*b843c749SSergey Zigachev 		}
139*b843c749SSergey Zigachev 
140*b843c749SSergey Zigachev 		dma_fence_put(fence);
141*b843c749SSergey Zigachev 		fence = NULL;
142*b843c749SSergey Zigachev 
143*b843c749SSergey Zigachev 		r = amdgpu_bo_kmap(vram_obj, &vram_map);
144*b843c749SSergey Zigachev 		if (r) {
145*b843c749SSergey Zigachev 			DRM_ERROR("Failed to map VRAM object after copy %d\n", i);
146*b843c749SSergey Zigachev 			goto out_lclean_unpin;
147*b843c749SSergey Zigachev 		}
148*b843c749SSergey Zigachev 
149*b843c749SSergey Zigachev 		for (gart_start = gtt_map, gart_end = gtt_map + size,
150*b843c749SSergey Zigachev 		     vram_start = vram_map, vram_end = vram_map + size;
151*b843c749SSergey Zigachev 		     vram_start < vram_end;
152*b843c749SSergey Zigachev 		     gart_start++, vram_start++) {
153*b843c749SSergey Zigachev 			if (*vram_start != gart_start) {
154*b843c749SSergey Zigachev 				DRM_ERROR("Incorrect GTT->VRAM copy %d: Got 0x%p, "
155*b843c749SSergey Zigachev 					  "expected 0x%p (GTT/VRAM offset "
156*b843c749SSergey Zigachev 					  "0x%16llx/0x%16llx)\n",
157*b843c749SSergey Zigachev 					  i, *vram_start, gart_start,
158*b843c749SSergey Zigachev 					  (unsigned long long)
159*b843c749SSergey Zigachev 					  (gart_addr - adev->gmc.gart_start +
160*b843c749SSergey Zigachev 					   (void*)gart_start - gtt_map),
161*b843c749SSergey Zigachev 					  (unsigned long long)
162*b843c749SSergey Zigachev 					  (vram_addr - adev->gmc.vram_start +
163*b843c749SSergey Zigachev 					   (void*)gart_start - gtt_map));
164*b843c749SSergey Zigachev 				amdgpu_bo_kunmap(vram_obj);
165*b843c749SSergey Zigachev 				goto out_lclean_unpin;
166*b843c749SSergey Zigachev 			}
167*b843c749SSergey Zigachev 			*vram_start = vram_start;
168*b843c749SSergey Zigachev 		}
169*b843c749SSergey Zigachev 
170*b843c749SSergey Zigachev 		amdgpu_bo_kunmap(vram_obj);
171*b843c749SSergey Zigachev 
172*b843c749SSergey Zigachev 		r = amdgpu_copy_buffer(ring, vram_addr, gart_addr,
173*b843c749SSergey Zigachev 				       size, NULL, &fence, false, false);
174*b843c749SSergey Zigachev 
175*b843c749SSergey Zigachev 		if (r) {
176*b843c749SSergey Zigachev 			DRM_ERROR("Failed VRAM->GTT copy %d\n", i);
177*b843c749SSergey Zigachev 			goto out_lclean_unpin;
178*b843c749SSergey Zigachev 		}
179*b843c749SSergey Zigachev 
180*b843c749SSergey Zigachev 		r = dma_fence_wait(fence, false);
181*b843c749SSergey Zigachev 		if (r) {
182*b843c749SSergey Zigachev 			DRM_ERROR("Failed to wait for VRAM->GTT fence %d\n", i);
183*b843c749SSergey Zigachev 			goto out_lclean_unpin;
184*b843c749SSergey Zigachev 		}
185*b843c749SSergey Zigachev 
186*b843c749SSergey Zigachev 		dma_fence_put(fence);
187*b843c749SSergey Zigachev 		fence = NULL;
188*b843c749SSergey Zigachev 
189*b843c749SSergey Zigachev 		r = amdgpu_bo_kmap(gtt_obj[i], &gtt_map);
190*b843c749SSergey Zigachev 		if (r) {
191*b843c749SSergey Zigachev 			DRM_ERROR("Failed to map GTT object after copy %d\n", i);
192*b843c749SSergey Zigachev 			goto out_lclean_unpin;
193*b843c749SSergey Zigachev 		}
194*b843c749SSergey Zigachev 
195*b843c749SSergey Zigachev 		for (gart_start = gtt_map, gart_end = gtt_map + size,
196*b843c749SSergey Zigachev 		     vram_start = vram_map, vram_end = vram_map + size;
197*b843c749SSergey Zigachev 		     gart_start < gart_end;
198*b843c749SSergey Zigachev 		     gart_start++, vram_start++) {
199*b843c749SSergey Zigachev 			if (*gart_start != vram_start) {
200*b843c749SSergey Zigachev 				DRM_ERROR("Incorrect VRAM->GTT copy %d: Got 0x%p, "
201*b843c749SSergey Zigachev 					  "expected 0x%p (VRAM/GTT offset "
202*b843c749SSergey Zigachev 					  "0x%16llx/0x%16llx)\n",
203*b843c749SSergey Zigachev 					  i, *gart_start, vram_start,
204*b843c749SSergey Zigachev 					  (unsigned long long)
205*b843c749SSergey Zigachev 					  (vram_addr - adev->gmc.vram_start +
206*b843c749SSergey Zigachev 					   (void*)vram_start - vram_map),
207*b843c749SSergey Zigachev 					  (unsigned long long)
208*b843c749SSergey Zigachev 					  (gart_addr - adev->gmc.gart_start +
209*b843c749SSergey Zigachev 					   (void*)vram_start - vram_map));
210*b843c749SSergey Zigachev 				amdgpu_bo_kunmap(gtt_obj[i]);
211*b843c749SSergey Zigachev 				goto out_lclean_unpin;
212*b843c749SSergey Zigachev 			}
213*b843c749SSergey Zigachev 		}
214*b843c749SSergey Zigachev 
215*b843c749SSergey Zigachev 		amdgpu_bo_kunmap(gtt_obj[i]);
216*b843c749SSergey Zigachev 
217*b843c749SSergey Zigachev 		DRM_INFO("Tested GTT->VRAM and VRAM->GTT copy for GTT offset 0x%llx\n",
218*b843c749SSergey Zigachev 			 gart_addr - adev->gmc.gart_start);
219*b843c749SSergey Zigachev 		continue;
220*b843c749SSergey Zigachev 
221*b843c749SSergey Zigachev out_lclean_unpin:
222*b843c749SSergey Zigachev 		amdgpu_bo_unpin(gtt_obj[i]);
223*b843c749SSergey Zigachev out_lclean_unres:
224*b843c749SSergey Zigachev 		amdgpu_bo_unreserve(gtt_obj[i]);
225*b843c749SSergey Zigachev out_lclean_unref:
226*b843c749SSergey Zigachev 		amdgpu_bo_unref(&gtt_obj[i]);
227*b843c749SSergey Zigachev out_lclean:
228*b843c749SSergey Zigachev 		for (--i; i >= 0; --i) {
229*b843c749SSergey Zigachev 			amdgpu_bo_unpin(gtt_obj[i]);
230*b843c749SSergey Zigachev 			amdgpu_bo_unreserve(gtt_obj[i]);
231*b843c749SSergey Zigachev 			amdgpu_bo_unref(&gtt_obj[i]);
232*b843c749SSergey Zigachev 		}
233*b843c749SSergey Zigachev 		if (fence)
234*b843c749SSergey Zigachev 			dma_fence_put(fence);
235*b843c749SSergey Zigachev 		break;
236*b843c749SSergey Zigachev 	}
237*b843c749SSergey Zigachev 
238*b843c749SSergey Zigachev 	amdgpu_bo_unpin(vram_obj);
239*b843c749SSergey Zigachev out_unres:
240*b843c749SSergey Zigachev 	amdgpu_bo_unreserve(vram_obj);
241*b843c749SSergey Zigachev out_unref:
242*b843c749SSergey Zigachev 	amdgpu_bo_unref(&vram_obj);
243*b843c749SSergey Zigachev out_cleanup:
244*b843c749SSergey Zigachev 	kfree(gtt_obj);
245*b843c749SSergey Zigachev 	if (r) {
246*b843c749SSergey Zigachev 		pr_warn("Error while testing BO move\n");
247*b843c749SSergey Zigachev 	}
248*b843c749SSergey Zigachev }
249*b843c749SSergey Zigachev 
amdgpu_test_moves(struct amdgpu_device * adev)250*b843c749SSergey Zigachev void amdgpu_test_moves(struct amdgpu_device *adev)
251*b843c749SSergey Zigachev {
252*b843c749SSergey Zigachev 	if (adev->mman.buffer_funcs)
253*b843c749SSergey Zigachev 		amdgpu_do_test_moves(adev);
254*b843c749SSergey Zigachev }
255