xref: /dflybsd-src/sys/dev/drm/radeon/radeon_test.c (revision 4cd92098975238a3e2cfccf057598cf2a5e54b55)
1926deccbSFrançois Tigeot /*
2926deccbSFrançois Tigeot  * Copyright 2009 VMware, Inc.
3926deccbSFrançois Tigeot  *
4926deccbSFrançois Tigeot  * Permission is hereby granted, free of charge, to any person obtaining a
5926deccbSFrançois Tigeot  * copy of this software and associated documentation files (the "Software"),
6926deccbSFrançois Tigeot  * to deal in the Software without restriction, including without limitation
7926deccbSFrançois Tigeot  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8926deccbSFrançois Tigeot  * and/or sell copies of the Software, and to permit persons to whom the
9926deccbSFrançois Tigeot  * Software is furnished to do so, subject to the following conditions:
10926deccbSFrançois Tigeot  *
11926deccbSFrançois Tigeot  * The above copyright notice and this permission notice shall be included in
12926deccbSFrançois Tigeot  * all copies or substantial portions of the Software.
13926deccbSFrançois Tigeot  *
14926deccbSFrançois Tigeot  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15926deccbSFrançois Tigeot  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16926deccbSFrançois Tigeot  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17926deccbSFrançois Tigeot  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18926deccbSFrançois Tigeot  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19926deccbSFrançois Tigeot  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20926deccbSFrançois Tigeot  * OTHER DEALINGS IN THE SOFTWARE.
21926deccbSFrançois Tigeot  *
22926deccbSFrançois Tigeot  * Authors: Michel Dänzer
23926deccbSFrançois Tigeot  *
24926deccbSFrançois Tigeot  * $FreeBSD: head/sys/dev/drm2/radeon/radeon_test.c 254885 2013-08-25 19:37:15Z dumbbell $
25926deccbSFrançois Tigeot  */
26926deccbSFrançois Tigeot 
27926deccbSFrançois Tigeot #include <drm/drmP.h>
28926deccbSFrançois Tigeot #include <uapi_drm/radeon_drm.h>
29926deccbSFrançois Tigeot #include "radeon_reg.h"
30926deccbSFrançois Tigeot #include "radeon.h"
31926deccbSFrançois Tigeot 
32926deccbSFrançois Tigeot #define RADEON_TEST_COPY_BLIT 1
33926deccbSFrançois Tigeot #define RADEON_TEST_COPY_DMA  0
34926deccbSFrançois Tigeot 
35926deccbSFrançois Tigeot 
36926deccbSFrançois Tigeot /* Test BO GTT->VRAM and VRAM->GTT GPU copies across the whole GTT aperture */
37926deccbSFrançois Tigeot static void radeon_do_test_moves(struct radeon_device *rdev, int flag)
38926deccbSFrançois Tigeot {
39926deccbSFrançois Tigeot 	struct radeon_bo *vram_obj = NULL;
40926deccbSFrançois Tigeot 	struct radeon_bo **gtt_obj = NULL;
41926deccbSFrançois Tigeot 	struct radeon_fence *fence = NULL;
42926deccbSFrançois Tigeot 	uint64_t gtt_addr, vram_addr;
43*4cd92098Szrj 	unsigned n, size;
44*4cd92098Szrj 	int i, r, ring;
45926deccbSFrançois Tigeot 
46926deccbSFrançois Tigeot 	switch (flag) {
47926deccbSFrançois Tigeot 	case RADEON_TEST_COPY_DMA:
48926deccbSFrançois Tigeot 		ring = radeon_copy_dma_ring_index(rdev);
49926deccbSFrançois Tigeot 		break;
50926deccbSFrançois Tigeot 	case RADEON_TEST_COPY_BLIT:
51926deccbSFrançois Tigeot 		ring = radeon_copy_blit_ring_index(rdev);
52926deccbSFrançois Tigeot 		break;
53926deccbSFrançois Tigeot 	default:
54926deccbSFrançois Tigeot 		DRM_ERROR("Unknown copy method\n");
55926deccbSFrançois Tigeot 		return;
56926deccbSFrançois Tigeot 	}
57926deccbSFrançois Tigeot 
58926deccbSFrançois Tigeot 	size = 1024 * 1024;
59926deccbSFrançois Tigeot 
60926deccbSFrançois Tigeot 	/* Number of tests =
61926deccbSFrançois Tigeot 	 * (Total GTT - IB pool - writeback page - ring buffers) / test size
62926deccbSFrançois Tigeot 	 */
63926deccbSFrançois Tigeot 	n = rdev->mc.gtt_size - RADEON_IB_POOL_SIZE*64*1024;
64926deccbSFrançois Tigeot 	for (i = 0; i < RADEON_NUM_RINGS; ++i)
65926deccbSFrançois Tigeot 		n -= rdev->ring[i].ring_size;
66926deccbSFrançois Tigeot 	if (rdev->wb.wb_obj)
67926deccbSFrançois Tigeot 		n -= RADEON_GPU_PAGE_SIZE;
68926deccbSFrançois Tigeot 	if (rdev->ih.ring_obj)
69926deccbSFrançois Tigeot 		n -= rdev->ih.ring_size;
70926deccbSFrançois Tigeot 	n /= size;
71926deccbSFrançois Tigeot 
72c4ef309bSzrj 	gtt_obj = kzalloc(n * sizeof(*gtt_obj), GFP_KERNEL);
73926deccbSFrançois Tigeot 	if (!gtt_obj) {
74926deccbSFrançois Tigeot 		DRM_ERROR("Failed to allocate %d pointers\n", n);
75926deccbSFrançois Tigeot 		r = 1;
76926deccbSFrançois Tigeot 		goto out_cleanup;
77926deccbSFrançois Tigeot 	}
78926deccbSFrançois Tigeot 
79926deccbSFrançois Tigeot 	r = radeon_bo_create(rdev, size, PAGE_SIZE, true, RADEON_GEM_DOMAIN_VRAM,
80926deccbSFrançois Tigeot 			     NULL, &vram_obj);
81926deccbSFrançois Tigeot 	if (r) {
82926deccbSFrançois Tigeot 		DRM_ERROR("Failed to create VRAM object\n");
83926deccbSFrançois Tigeot 		goto out_cleanup;
84926deccbSFrançois Tigeot 	}
85926deccbSFrançois Tigeot 	r = radeon_bo_reserve(vram_obj, false);
86926deccbSFrançois Tigeot 	if (unlikely(r != 0))
87926deccbSFrançois Tigeot 		goto out_cleanup;
88926deccbSFrançois Tigeot 	r = radeon_bo_pin(vram_obj, RADEON_GEM_DOMAIN_VRAM, &vram_addr);
89926deccbSFrançois Tigeot 	if (r) {
90926deccbSFrançois Tigeot 		DRM_ERROR("Failed to pin VRAM object\n");
91926deccbSFrançois Tigeot 		goto out_cleanup;
92926deccbSFrançois Tigeot 	}
93926deccbSFrançois Tigeot 	for (i = 0; i < n; i++) {
94926deccbSFrançois Tigeot 		void *gtt_map, *vram_map;
95926deccbSFrançois Tigeot 		void **gtt_start, **gtt_end;
96926deccbSFrançois Tigeot 		void **vram_start, **vram_end;
97926deccbSFrançois Tigeot 
98926deccbSFrançois Tigeot 		r = radeon_bo_create(rdev, size, PAGE_SIZE, true,
99926deccbSFrançois Tigeot 				     RADEON_GEM_DOMAIN_GTT, NULL, gtt_obj + i);
100926deccbSFrançois Tigeot 		if (r) {
101926deccbSFrançois Tigeot 			DRM_ERROR("Failed to create GTT object %d\n", i);
102926deccbSFrançois Tigeot 			goto out_cleanup;
103926deccbSFrançois Tigeot 		}
104926deccbSFrançois Tigeot 
105926deccbSFrançois Tigeot 		r = radeon_bo_reserve(gtt_obj[i], false);
106926deccbSFrançois Tigeot 		if (unlikely(r != 0))
107926deccbSFrançois Tigeot 			goto out_cleanup;
108926deccbSFrançois Tigeot 		r = radeon_bo_pin(gtt_obj[i], RADEON_GEM_DOMAIN_GTT, &gtt_addr);
109926deccbSFrançois Tigeot 		if (r) {
110926deccbSFrançois Tigeot 			DRM_ERROR("Failed to pin GTT object %d\n", i);
111926deccbSFrançois Tigeot 			goto out_cleanup;
112926deccbSFrançois Tigeot 		}
113926deccbSFrançois Tigeot 
114926deccbSFrançois Tigeot 		r = radeon_bo_kmap(gtt_obj[i], &gtt_map);
115926deccbSFrançois Tigeot 		if (r) {
116926deccbSFrançois Tigeot 			DRM_ERROR("Failed to map GTT object %d\n", i);
117926deccbSFrançois Tigeot 			goto out_cleanup;
118926deccbSFrançois Tigeot 		}
119926deccbSFrançois Tigeot 
120926deccbSFrançois Tigeot 		for (gtt_start = gtt_map, gtt_end = (void *)((uintptr_t)gtt_map + size);
121926deccbSFrançois Tigeot 		     gtt_start < gtt_end;
122926deccbSFrançois Tigeot 		     gtt_start++)
123926deccbSFrançois Tigeot 			*gtt_start = gtt_start;
124926deccbSFrançois Tigeot 
125926deccbSFrançois Tigeot 		radeon_bo_kunmap(gtt_obj[i]);
126926deccbSFrançois Tigeot 
127926deccbSFrançois Tigeot 		if (ring == R600_RING_TYPE_DMA_INDEX)
128926deccbSFrançois Tigeot 			r = radeon_copy_dma(rdev, gtt_addr, vram_addr, size / RADEON_GPU_PAGE_SIZE, &fence);
129926deccbSFrançois Tigeot 		else
130926deccbSFrançois Tigeot 			r = radeon_copy_blit(rdev, gtt_addr, vram_addr, size / RADEON_GPU_PAGE_SIZE, &fence);
131926deccbSFrançois Tigeot 		if (r) {
132926deccbSFrançois Tigeot 			DRM_ERROR("Failed GTT->VRAM copy %d\n", i);
133926deccbSFrançois Tigeot 			goto out_cleanup;
134926deccbSFrançois Tigeot 		}
135926deccbSFrançois Tigeot 
136926deccbSFrançois Tigeot 		r = radeon_fence_wait(fence, false);
137926deccbSFrançois Tigeot 		if (r) {
138926deccbSFrançois Tigeot 			DRM_ERROR("Failed to wait for GTT->VRAM fence %d\n", i);
139926deccbSFrançois Tigeot 			goto out_cleanup;
140926deccbSFrançois Tigeot 		}
141926deccbSFrançois Tigeot 
142926deccbSFrançois Tigeot 		radeon_fence_unref(&fence);
143926deccbSFrançois Tigeot 
144926deccbSFrançois Tigeot 		r = radeon_bo_kmap(vram_obj, &vram_map);
145926deccbSFrançois Tigeot 		if (r) {
146926deccbSFrançois Tigeot 			DRM_ERROR("Failed to map VRAM object after copy %d\n", i);
147926deccbSFrançois Tigeot 			goto out_cleanup;
148926deccbSFrançois Tigeot 		}
149926deccbSFrançois Tigeot 
150926deccbSFrançois Tigeot 		for (gtt_start = gtt_map, gtt_end = (void *)((uintptr_t)gtt_map + size),
151926deccbSFrançois Tigeot 		     vram_start = vram_map, vram_end = (void *)((uintptr_t)vram_map + size);
152926deccbSFrançois Tigeot 		     vram_start < vram_end;
153926deccbSFrançois Tigeot 		     gtt_start++, vram_start++) {
154926deccbSFrançois Tigeot 			if (*vram_start != gtt_start) {
155926deccbSFrançois Tigeot 				DRM_ERROR("Incorrect GTT->VRAM copy %d: Got 0x%p, "
156926deccbSFrançois Tigeot 					  "expected 0x%p (GTT/VRAM offset "
157926deccbSFrançois Tigeot 					  "0x%16llx/0x%16llx)\n",
158926deccbSFrançois Tigeot 					  i, *vram_start, gtt_start,
159926deccbSFrançois Tigeot 					  (unsigned long long)
160926deccbSFrançois Tigeot 					  ((uintptr_t)gtt_addr - (uintptr_t)rdev->mc.gtt_start +
161926deccbSFrançois Tigeot 					   (uintptr_t)gtt_start - (uintptr_t)gtt_map),
162926deccbSFrançois Tigeot 					  (unsigned long long)
163926deccbSFrançois Tigeot 					  ((uintptr_t)vram_addr - (uintptr_t)rdev->mc.vram_start +
164926deccbSFrançois Tigeot 					   (uintptr_t)gtt_start - (uintptr_t)gtt_map));
165926deccbSFrançois Tigeot 				radeon_bo_kunmap(vram_obj);
166926deccbSFrançois Tigeot 				goto out_cleanup;
167926deccbSFrançois Tigeot 			}
168926deccbSFrançois Tigeot 			*vram_start = vram_start;
169926deccbSFrançois Tigeot 		}
170926deccbSFrançois Tigeot 
171926deccbSFrançois Tigeot 		radeon_bo_kunmap(vram_obj);
172926deccbSFrançois Tigeot 
173926deccbSFrançois Tigeot 		if (ring == R600_RING_TYPE_DMA_INDEX)
174926deccbSFrançois Tigeot 			r = radeon_copy_dma(rdev, vram_addr, gtt_addr, size / RADEON_GPU_PAGE_SIZE, &fence);
175926deccbSFrançois Tigeot 		else
176926deccbSFrançois Tigeot 			r = radeon_copy_blit(rdev, vram_addr, gtt_addr, size / RADEON_GPU_PAGE_SIZE, &fence);
177926deccbSFrançois Tigeot 		if (r) {
178926deccbSFrançois Tigeot 			DRM_ERROR("Failed VRAM->GTT copy %d\n", i);
179926deccbSFrançois Tigeot 			goto out_cleanup;
180926deccbSFrançois Tigeot 		}
181926deccbSFrançois Tigeot 
182926deccbSFrançois Tigeot 		r = radeon_fence_wait(fence, false);
183926deccbSFrançois Tigeot 		if (r) {
184926deccbSFrançois Tigeot 			DRM_ERROR("Failed to wait for VRAM->GTT fence %d\n", i);
185926deccbSFrançois Tigeot 			goto out_cleanup;
186926deccbSFrançois Tigeot 		}
187926deccbSFrançois Tigeot 
188926deccbSFrançois Tigeot 		radeon_fence_unref(&fence);
189926deccbSFrançois Tigeot 
190926deccbSFrançois Tigeot 		r = radeon_bo_kmap(gtt_obj[i], &gtt_map);
191926deccbSFrançois Tigeot 		if (r) {
192926deccbSFrançois Tigeot 			DRM_ERROR("Failed to map GTT object after copy %d\n", i);
193926deccbSFrançois Tigeot 			goto out_cleanup;
194926deccbSFrançois Tigeot 		}
195926deccbSFrançois Tigeot 
196926deccbSFrançois Tigeot 		for (gtt_start = gtt_map, gtt_end = (void *)((uintptr_t)gtt_map + size),
197926deccbSFrançois Tigeot 		     vram_start = vram_map, vram_end = (void *)((uintptr_t)vram_map + size);
198926deccbSFrançois Tigeot 		     gtt_start < gtt_end;
199926deccbSFrançois Tigeot 		     gtt_start++, vram_start++) {
200926deccbSFrançois Tigeot 			if (*gtt_start != vram_start) {
201926deccbSFrançois Tigeot 				DRM_ERROR("Incorrect VRAM->GTT copy %d: Got 0x%p, "
202926deccbSFrançois Tigeot 					  "expected 0x%p (VRAM/GTT offset "
203926deccbSFrançois Tigeot 					  "0x%16llx/0x%16llx)\n",
204926deccbSFrançois Tigeot 					  i, *gtt_start, vram_start,
205926deccbSFrançois Tigeot 					  (unsigned long long)
206926deccbSFrançois Tigeot 					  ((uintptr_t)vram_addr - (uintptr_t)rdev->mc.vram_start +
207926deccbSFrançois Tigeot 					   (uintptr_t)vram_start - (uintptr_t)vram_map),
208926deccbSFrançois Tigeot 					  (unsigned long long)
209926deccbSFrançois Tigeot 					  ((uintptr_t)gtt_addr - (uintptr_t)rdev->mc.gtt_start +
210926deccbSFrançois Tigeot 					   (uintptr_t)vram_start - (uintptr_t)vram_map));
211926deccbSFrançois Tigeot 				radeon_bo_kunmap(gtt_obj[i]);
212926deccbSFrançois Tigeot 				goto out_cleanup;
213926deccbSFrançois Tigeot 			}
214926deccbSFrançois Tigeot 		}
215926deccbSFrançois Tigeot 
216926deccbSFrançois Tigeot 		radeon_bo_kunmap(gtt_obj[i]);
217926deccbSFrançois Tigeot 
218926deccbSFrançois Tigeot 		DRM_INFO("Tested GTT->VRAM and VRAM->GTT copy for GTT offset 0x%jx\n",
219926deccbSFrançois Tigeot 			 (uintmax_t)gtt_addr - rdev->mc.gtt_start);
220926deccbSFrançois Tigeot 	}
221926deccbSFrançois Tigeot 
222926deccbSFrançois Tigeot out_cleanup:
223926deccbSFrançois Tigeot 	if (vram_obj) {
224926deccbSFrançois Tigeot 		if (radeon_bo_is_reserved(vram_obj)) {
225926deccbSFrançois Tigeot 			radeon_bo_unpin(vram_obj);
226926deccbSFrançois Tigeot 			radeon_bo_unreserve(vram_obj);
227926deccbSFrançois Tigeot 		}
228926deccbSFrançois Tigeot 		radeon_bo_unref(&vram_obj);
229926deccbSFrançois Tigeot 	}
230926deccbSFrançois Tigeot 	if (gtt_obj) {
231926deccbSFrançois Tigeot 		for (i = 0; i < n; i++) {
232926deccbSFrançois Tigeot 			if (gtt_obj[i]) {
233926deccbSFrançois Tigeot 				if (radeon_bo_is_reserved(gtt_obj[i])) {
234926deccbSFrançois Tigeot 					radeon_bo_unpin(gtt_obj[i]);
235926deccbSFrançois Tigeot 					radeon_bo_unreserve(gtt_obj[i]);
236926deccbSFrançois Tigeot 				}
237926deccbSFrançois Tigeot 				radeon_bo_unref(&gtt_obj[i]);
238926deccbSFrançois Tigeot 			}
239926deccbSFrançois Tigeot 		}
240c4ef309bSzrj 		kfree(gtt_obj);
241926deccbSFrançois Tigeot 	}
242926deccbSFrançois Tigeot 	if (fence) {
243926deccbSFrançois Tigeot 		radeon_fence_unref(&fence);
244926deccbSFrançois Tigeot 	}
245926deccbSFrançois Tigeot 	if (r) {
246c4ef309bSzrj 		printk(KERN_WARNING "Error while testing BO move.\n");
247926deccbSFrançois Tigeot 	}
248926deccbSFrançois Tigeot }
249926deccbSFrançois Tigeot 
250926deccbSFrançois Tigeot void radeon_test_moves(struct radeon_device *rdev)
251926deccbSFrançois Tigeot {
252926deccbSFrançois Tigeot 	if (rdev->asic->copy.dma)
253926deccbSFrançois Tigeot 		radeon_do_test_moves(rdev, RADEON_TEST_COPY_DMA);
254926deccbSFrançois Tigeot 	if (rdev->asic->copy.blit)
255926deccbSFrançois Tigeot 		radeon_do_test_moves(rdev, RADEON_TEST_COPY_BLIT);
256926deccbSFrançois Tigeot }
257926deccbSFrançois Tigeot 
258f43cf1b1SMichael Neumann static int radeon_test_create_and_emit_fence(struct radeon_device *rdev,
259f43cf1b1SMichael Neumann 					     struct radeon_ring *ring,
260f43cf1b1SMichael Neumann 					     struct radeon_fence **fence)
261f43cf1b1SMichael Neumann {
262f43cf1b1SMichael Neumann 	int r;
263f43cf1b1SMichael Neumann 
264f43cf1b1SMichael Neumann 	if (ring->idx == R600_RING_TYPE_UVD_INDEX) {
265f43cf1b1SMichael Neumann 		r = radeon_uvd_get_create_msg(rdev, ring->idx, 1, NULL);
266f43cf1b1SMichael Neumann 		if (r) {
267f43cf1b1SMichael Neumann 			DRM_ERROR("Failed to get dummy create msg\n");
268f43cf1b1SMichael Neumann 			return r;
269f43cf1b1SMichael Neumann 		}
270f43cf1b1SMichael Neumann 
271f43cf1b1SMichael Neumann 		r = radeon_uvd_get_destroy_msg(rdev, ring->idx, 1, fence);
272f43cf1b1SMichael Neumann 		if (r) {
273f43cf1b1SMichael Neumann 			DRM_ERROR("Failed to get dummy destroy msg\n");
274f43cf1b1SMichael Neumann 			return r;
275f43cf1b1SMichael Neumann 		}
276f43cf1b1SMichael Neumann 	} else {
277f43cf1b1SMichael Neumann 		r = radeon_ring_lock(rdev, ring, 64);
278f43cf1b1SMichael Neumann 		if (r) {
279f43cf1b1SMichael Neumann 			DRM_ERROR("Failed to lock ring A %d\n", ring->idx);
280f43cf1b1SMichael Neumann 			return r;
281f43cf1b1SMichael Neumann 		}
282f43cf1b1SMichael Neumann 		radeon_fence_emit(rdev, fence, ring->idx);
283f43cf1b1SMichael Neumann 		radeon_ring_unlock_commit(rdev, ring);
284f43cf1b1SMichael Neumann 	}
285f43cf1b1SMichael Neumann 	return 0;
286f43cf1b1SMichael Neumann }
287f43cf1b1SMichael Neumann 
288926deccbSFrançois Tigeot void radeon_test_ring_sync(struct radeon_device *rdev,
289926deccbSFrançois Tigeot 			   struct radeon_ring *ringA,
290926deccbSFrançois Tigeot 			   struct radeon_ring *ringB)
291926deccbSFrançois Tigeot {
292926deccbSFrançois Tigeot 	struct radeon_fence *fence1 = NULL, *fence2 = NULL;
293926deccbSFrançois Tigeot 	struct radeon_semaphore *semaphore = NULL;
294926deccbSFrançois Tigeot 	int r;
295926deccbSFrançois Tigeot 
296926deccbSFrançois Tigeot 	r = radeon_semaphore_create(rdev, &semaphore);
297926deccbSFrançois Tigeot 	if (r) {
298926deccbSFrançois Tigeot 		DRM_ERROR("Failed to create semaphore\n");
299926deccbSFrançois Tigeot 		goto out_cleanup;
300926deccbSFrançois Tigeot 	}
301926deccbSFrançois Tigeot 
302926deccbSFrançois Tigeot 	r = radeon_ring_lock(rdev, ringA, 64);
303926deccbSFrançois Tigeot 	if (r) {
304926deccbSFrançois Tigeot 		DRM_ERROR("Failed to lock ring A %d\n", ringA->idx);
305926deccbSFrançois Tigeot 		goto out_cleanup;
306926deccbSFrançois Tigeot 	}
307926deccbSFrançois Tigeot 	radeon_semaphore_emit_wait(rdev, ringA->idx, semaphore);
308f43cf1b1SMichael Neumann 	radeon_ring_unlock_commit(rdev, ringA);
309f43cf1b1SMichael Neumann 
310f43cf1b1SMichael Neumann 	r = radeon_test_create_and_emit_fence(rdev, ringA, &fence1);
311f43cf1b1SMichael Neumann 	if (r)
312f43cf1b1SMichael Neumann 		goto out_cleanup;
313f43cf1b1SMichael Neumann 
314f43cf1b1SMichael Neumann 	r = radeon_ring_lock(rdev, ringA, 64);
315926deccbSFrançois Tigeot 	if (r) {
316f43cf1b1SMichael Neumann 		DRM_ERROR("Failed to lock ring A %d\n", ringA->idx);
317926deccbSFrançois Tigeot 		goto out_cleanup;
318926deccbSFrançois Tigeot 	}
319926deccbSFrançois Tigeot 	radeon_semaphore_emit_wait(rdev, ringA->idx, semaphore);
320926deccbSFrançois Tigeot 	radeon_ring_unlock_commit(rdev, ringA);
321926deccbSFrançois Tigeot 
322f43cf1b1SMichael Neumann 	r = radeon_test_create_and_emit_fence(rdev, ringA, &fence2);
323f43cf1b1SMichael Neumann 	if (r)
324f43cf1b1SMichael Neumann 		goto out_cleanup;
325f43cf1b1SMichael Neumann 
326c4ef309bSzrj 	mdelay(1000);
327926deccbSFrançois Tigeot 
328926deccbSFrançois Tigeot 	if (radeon_fence_signaled(fence1)) {
329926deccbSFrançois Tigeot 		DRM_ERROR("Fence 1 signaled without waiting for semaphore.\n");
330926deccbSFrançois Tigeot 		goto out_cleanup;
331926deccbSFrançois Tigeot 	}
332926deccbSFrançois Tigeot 
333926deccbSFrançois Tigeot 	r = radeon_ring_lock(rdev, ringB, 64);
334926deccbSFrançois Tigeot 	if (r) {
335926deccbSFrançois Tigeot 		DRM_ERROR("Failed to lock ring B %p\n", ringB);
336926deccbSFrançois Tigeot 		goto out_cleanup;
337926deccbSFrançois Tigeot 	}
338926deccbSFrançois Tigeot 	radeon_semaphore_emit_signal(rdev, ringB->idx, semaphore);
339926deccbSFrançois Tigeot 	radeon_ring_unlock_commit(rdev, ringB);
340926deccbSFrançois Tigeot 
341926deccbSFrançois Tigeot 	r = radeon_fence_wait(fence1, false);
342926deccbSFrançois Tigeot 	if (r) {
343926deccbSFrançois Tigeot 		DRM_ERROR("Failed to wait for sync fence 1\n");
344926deccbSFrançois Tigeot 		goto out_cleanup;
345926deccbSFrançois Tigeot 	}
346926deccbSFrançois Tigeot 
347c4ef309bSzrj 	mdelay(1000);
348926deccbSFrançois Tigeot 
349926deccbSFrançois Tigeot 	if (radeon_fence_signaled(fence2)) {
350926deccbSFrançois Tigeot 		DRM_ERROR("Fence 2 signaled without waiting for semaphore.\n");
351926deccbSFrançois Tigeot 		goto out_cleanup;
352926deccbSFrançois Tigeot 	}
353926deccbSFrançois Tigeot 
354926deccbSFrançois Tigeot 	r = radeon_ring_lock(rdev, ringB, 64);
355926deccbSFrançois Tigeot 	if (r) {
356926deccbSFrançois Tigeot 		DRM_ERROR("Failed to lock ring B %p\n", ringB);
357926deccbSFrançois Tigeot 		goto out_cleanup;
358926deccbSFrançois Tigeot 	}
359926deccbSFrançois Tigeot 	radeon_semaphore_emit_signal(rdev, ringB->idx, semaphore);
360926deccbSFrançois Tigeot 	radeon_ring_unlock_commit(rdev, ringB);
361926deccbSFrançois Tigeot 
362926deccbSFrançois Tigeot 	r = radeon_fence_wait(fence2, false);
363926deccbSFrançois Tigeot 	if (r) {
364926deccbSFrançois Tigeot 		DRM_ERROR("Failed to wait for sync fence 1\n");
365926deccbSFrançois Tigeot 		goto out_cleanup;
366926deccbSFrançois Tigeot 	}
367926deccbSFrançois Tigeot 
368926deccbSFrançois Tigeot out_cleanup:
369926deccbSFrançois Tigeot 	radeon_semaphore_free(rdev, &semaphore, NULL);
370926deccbSFrançois Tigeot 
371926deccbSFrançois Tigeot 	if (fence1)
372926deccbSFrançois Tigeot 		radeon_fence_unref(&fence1);
373926deccbSFrançois Tigeot 
374926deccbSFrançois Tigeot 	if (fence2)
375926deccbSFrançois Tigeot 		radeon_fence_unref(&fence2);
376926deccbSFrançois Tigeot 
377926deccbSFrançois Tigeot 	if (r)
378c4ef309bSzrj 		printk(KERN_WARNING "Error while testing ring sync (%d).\n", r);
379926deccbSFrançois Tigeot }
380926deccbSFrançois Tigeot 
381926deccbSFrançois Tigeot static void radeon_test_ring_sync2(struct radeon_device *rdev,
382926deccbSFrançois Tigeot 			    struct radeon_ring *ringA,
383926deccbSFrançois Tigeot 			    struct radeon_ring *ringB,
384926deccbSFrançois Tigeot 			    struct radeon_ring *ringC)
385926deccbSFrançois Tigeot {
386926deccbSFrançois Tigeot 	struct radeon_fence *fenceA = NULL, *fenceB = NULL;
387926deccbSFrançois Tigeot 	struct radeon_semaphore *semaphore = NULL;
388926deccbSFrançois Tigeot 	bool sigA, sigB;
389926deccbSFrançois Tigeot 	int i, r;
390926deccbSFrançois Tigeot 
391926deccbSFrançois Tigeot 	r = radeon_semaphore_create(rdev, &semaphore);
392926deccbSFrançois Tigeot 	if (r) {
393926deccbSFrançois Tigeot 		DRM_ERROR("Failed to create semaphore\n");
394926deccbSFrançois Tigeot 		goto out_cleanup;
395926deccbSFrançois Tigeot 	}
396926deccbSFrançois Tigeot 
397926deccbSFrançois Tigeot 	r = radeon_ring_lock(rdev, ringA, 64);
398926deccbSFrançois Tigeot 	if (r) {
399926deccbSFrançois Tigeot 		DRM_ERROR("Failed to lock ring A %d\n", ringA->idx);
400926deccbSFrançois Tigeot 		goto out_cleanup;
401926deccbSFrançois Tigeot 	}
402926deccbSFrançois Tigeot 	radeon_semaphore_emit_wait(rdev, ringA->idx, semaphore);
403926deccbSFrançois Tigeot 	radeon_ring_unlock_commit(rdev, ringA);
404926deccbSFrançois Tigeot 
405f43cf1b1SMichael Neumann 	r = radeon_test_create_and_emit_fence(rdev, ringA, &fenceA);
406f43cf1b1SMichael Neumann 	if (r)
407f43cf1b1SMichael Neumann 		goto out_cleanup;
408f43cf1b1SMichael Neumann 
409926deccbSFrançois Tigeot 	r = radeon_ring_lock(rdev, ringB, 64);
410926deccbSFrançois Tigeot 	if (r) {
411926deccbSFrançois Tigeot 		DRM_ERROR("Failed to lock ring B %d\n", ringB->idx);
412926deccbSFrançois Tigeot 		goto out_cleanup;
413926deccbSFrançois Tigeot 	}
414926deccbSFrançois Tigeot 	radeon_semaphore_emit_wait(rdev, ringB->idx, semaphore);
415926deccbSFrançois Tigeot 	radeon_ring_unlock_commit(rdev, ringB);
416f43cf1b1SMichael Neumann 	r = radeon_test_create_and_emit_fence(rdev, ringB, &fenceB);
417f43cf1b1SMichael Neumann 	if (r)
418f43cf1b1SMichael Neumann 		goto out_cleanup;
419926deccbSFrançois Tigeot 
420c4ef309bSzrj 	mdelay(1000);
421926deccbSFrançois Tigeot 
422926deccbSFrançois Tigeot 	if (radeon_fence_signaled(fenceA)) {
423926deccbSFrançois Tigeot 		DRM_ERROR("Fence A signaled without waiting for semaphore.\n");
424926deccbSFrançois Tigeot 		goto out_cleanup;
425926deccbSFrançois Tigeot 	}
426926deccbSFrançois Tigeot 	if (radeon_fence_signaled(fenceB)) {
427f43cf1b1SMichael Neumann 		DRM_ERROR("Fence B signaled without waiting for semaphore.\n");
428926deccbSFrançois Tigeot 		goto out_cleanup;
429926deccbSFrançois Tigeot 	}
430926deccbSFrançois Tigeot 
431926deccbSFrançois Tigeot 	r = radeon_ring_lock(rdev, ringC, 64);
432926deccbSFrançois Tigeot 	if (r) {
433926deccbSFrançois Tigeot 		DRM_ERROR("Failed to lock ring B %p\n", ringC);
434926deccbSFrançois Tigeot 		goto out_cleanup;
435926deccbSFrançois Tigeot 	}
436926deccbSFrançois Tigeot 	radeon_semaphore_emit_signal(rdev, ringC->idx, semaphore);
437926deccbSFrançois Tigeot 	radeon_ring_unlock_commit(rdev, ringC);
438926deccbSFrançois Tigeot 
439926deccbSFrançois Tigeot 	for (i = 0; i < 30; ++i) {
440c4ef309bSzrj 		mdelay(100);
441926deccbSFrançois Tigeot 		sigA = radeon_fence_signaled(fenceA);
442926deccbSFrançois Tigeot 		sigB = radeon_fence_signaled(fenceB);
443926deccbSFrançois Tigeot 		if (sigA || sigB)
444926deccbSFrançois Tigeot 			break;
445926deccbSFrançois Tigeot 	}
446926deccbSFrançois Tigeot 
447926deccbSFrançois Tigeot 	if (!sigA && !sigB) {
448926deccbSFrançois Tigeot 		DRM_ERROR("Neither fence A nor B has been signaled\n");
449926deccbSFrançois Tigeot 		goto out_cleanup;
450926deccbSFrançois Tigeot 	} else if (sigA && sigB) {
451926deccbSFrançois Tigeot 		DRM_ERROR("Both fence A and B has been signaled\n");
452926deccbSFrançois Tigeot 		goto out_cleanup;
453926deccbSFrançois Tigeot 	}
454926deccbSFrançois Tigeot 
455926deccbSFrançois Tigeot 	DRM_INFO("Fence %c was first signaled\n", sigA ? 'A' : 'B');
456926deccbSFrançois Tigeot 
457926deccbSFrançois Tigeot 	r = radeon_ring_lock(rdev, ringC, 64);
458926deccbSFrançois Tigeot 	if (r) {
459926deccbSFrançois Tigeot 		DRM_ERROR("Failed to lock ring B %p\n", ringC);
460926deccbSFrançois Tigeot 		goto out_cleanup;
461926deccbSFrançois Tigeot 	}
462926deccbSFrançois Tigeot 	radeon_semaphore_emit_signal(rdev, ringC->idx, semaphore);
463926deccbSFrançois Tigeot 	radeon_ring_unlock_commit(rdev, ringC);
464926deccbSFrançois Tigeot 
465c4ef309bSzrj 	mdelay(1000);
466926deccbSFrançois Tigeot 
467926deccbSFrançois Tigeot 	r = radeon_fence_wait(fenceA, false);
468926deccbSFrançois Tigeot 	if (r) {
469926deccbSFrançois Tigeot 		DRM_ERROR("Failed to wait for sync fence A\n");
470926deccbSFrançois Tigeot 		goto out_cleanup;
471926deccbSFrançois Tigeot 	}
472926deccbSFrançois Tigeot 	r = radeon_fence_wait(fenceB, false);
473926deccbSFrançois Tigeot 	if (r) {
474926deccbSFrançois Tigeot 		DRM_ERROR("Failed to wait for sync fence B\n");
475926deccbSFrançois Tigeot 		goto out_cleanup;
476926deccbSFrançois Tigeot 	}
477926deccbSFrançois Tigeot 
478926deccbSFrançois Tigeot out_cleanup:
479926deccbSFrançois Tigeot 	radeon_semaphore_free(rdev, &semaphore, NULL);
480926deccbSFrançois Tigeot 
481926deccbSFrançois Tigeot 	if (fenceA)
482926deccbSFrançois Tigeot 		radeon_fence_unref(&fenceA);
483926deccbSFrançois Tigeot 
484926deccbSFrançois Tigeot 	if (fenceB)
485926deccbSFrançois Tigeot 		radeon_fence_unref(&fenceB);
486926deccbSFrançois Tigeot 
487926deccbSFrançois Tigeot 	if (r)
488c4ef309bSzrj 		printk(KERN_WARNING "Error while testing ring sync (%d).\n", r);
489926deccbSFrançois Tigeot }
490926deccbSFrançois Tigeot 
491926deccbSFrançois Tigeot void radeon_test_syncing(struct radeon_device *rdev)
492926deccbSFrançois Tigeot {
493926deccbSFrançois Tigeot 	int i, j, k;
494926deccbSFrançois Tigeot 
495926deccbSFrançois Tigeot 	for (i = 1; i < RADEON_NUM_RINGS; ++i) {
496926deccbSFrançois Tigeot 		struct radeon_ring *ringA = &rdev->ring[i];
497926deccbSFrançois Tigeot 		if (!ringA->ready)
498926deccbSFrançois Tigeot 			continue;
499926deccbSFrançois Tigeot 
500926deccbSFrançois Tigeot 		for (j = 0; j < i; ++j) {
501926deccbSFrançois Tigeot 			struct radeon_ring *ringB = &rdev->ring[j];
502926deccbSFrançois Tigeot 			if (!ringB->ready)
503926deccbSFrançois Tigeot 				continue;
504926deccbSFrançois Tigeot 
505926deccbSFrançois Tigeot 			DRM_INFO("Testing syncing between rings %d and %d...\n", i, j);
506926deccbSFrançois Tigeot 			radeon_test_ring_sync(rdev, ringA, ringB);
507926deccbSFrançois Tigeot 
508926deccbSFrançois Tigeot 			DRM_INFO("Testing syncing between rings %d and %d...\n", j, i);
509926deccbSFrançois Tigeot 			radeon_test_ring_sync(rdev, ringB, ringA);
510926deccbSFrançois Tigeot 
511926deccbSFrançois Tigeot 			for (k = 0; k < j; ++k) {
512926deccbSFrançois Tigeot 				struct radeon_ring *ringC = &rdev->ring[k];
513926deccbSFrançois Tigeot 				if (!ringC->ready)
514926deccbSFrançois Tigeot 					continue;
515926deccbSFrançois Tigeot 
516926deccbSFrançois Tigeot 				DRM_INFO("Testing syncing between rings %d, %d and %d...\n", i, j, k);
517926deccbSFrançois Tigeot 				radeon_test_ring_sync2(rdev, ringA, ringB, ringC);
518926deccbSFrançois Tigeot 
519926deccbSFrançois Tigeot 				DRM_INFO("Testing syncing between rings %d, %d and %d...\n", i, k, j);
520926deccbSFrançois Tigeot 				radeon_test_ring_sync2(rdev, ringA, ringC, ringB);
521926deccbSFrançois Tigeot 
522926deccbSFrançois Tigeot 				DRM_INFO("Testing syncing between rings %d, %d and %d...\n", j, i, k);
523926deccbSFrançois Tigeot 				radeon_test_ring_sync2(rdev, ringB, ringA, ringC);
524926deccbSFrançois Tigeot 
525926deccbSFrançois Tigeot 				DRM_INFO("Testing syncing between rings %d, %d and %d...\n", j, k, i);
526926deccbSFrançois Tigeot 				radeon_test_ring_sync2(rdev, ringB, ringC, ringA);
527926deccbSFrançois Tigeot 
528926deccbSFrançois Tigeot 				DRM_INFO("Testing syncing between rings %d, %d and %d...\n", k, i, j);
529926deccbSFrançois Tigeot 				radeon_test_ring_sync2(rdev, ringC, ringA, ringB);
530926deccbSFrançois Tigeot 
531926deccbSFrançois Tigeot 				DRM_INFO("Testing syncing between rings %d, %d and %d...\n", k, j, i);
532926deccbSFrançois Tigeot 				radeon_test_ring_sync2(rdev, ringC, ringB, ringA);
533926deccbSFrançois Tigeot 			}
534926deccbSFrançois Tigeot 		}
535926deccbSFrançois Tigeot 	}
536926deccbSFrançois Tigeot }
537