1 // SPDX-License-Identifier: GPL-2.0 OR MIT
3 * Copyright 2009 VMware, Inc.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
23 * Authors: Michel Dänzer
26 #include <drm/radeon_drm.h>
27 #include "radeon_reg.h"
30 #define RADEON_TEST_COPY_BLIT 1
31 #define RADEON_TEST_COPY_DMA 0
34 /* Test BO GTT->VRAM and VRAM->GTT GPU copies across the whole GTT aperture */
35 static void radeon_do_test_moves(struct radeon_device
*rdev
, int flag
)
37 struct radeon_bo
*vram_obj
= NULL
;
38 struct radeon_bo
**gtt_obj
= NULL
;
39 uint64_t gtt_addr
, vram_addr
;
44 case RADEON_TEST_COPY_DMA
:
45 ring
= radeon_copy_dma_ring_index(rdev
);
47 case RADEON_TEST_COPY_BLIT
:
48 ring
= radeon_copy_blit_ring_index(rdev
);
51 DRM_ERROR("Unknown copy method\n");
58 * (Total GTT - IB pool - writeback page - ring buffers) / test size
60 n
= rdev
->mc
.gtt_size
- rdev
->gart_pin_size
;
63 gtt_obj
= kcalloc(n
, sizeof(*gtt_obj
), GFP_KERNEL
);
65 DRM_ERROR("Failed to allocate %d pointers\n", n
);
70 r
= radeon_bo_create(rdev
, size
, PAGE_SIZE
, true, RADEON_GEM_DOMAIN_VRAM
,
71 0, NULL
, NULL
, &vram_obj
);
73 DRM_ERROR("Failed to create VRAM object\n");
76 r
= radeon_bo_reserve(vram_obj
, false);
79 r
= radeon_bo_pin(vram_obj
, RADEON_GEM_DOMAIN_VRAM
, &vram_addr
);
81 DRM_ERROR("Failed to pin VRAM object\n");
84 for (i
= 0; i
< n
; i
++) {
85 void *gtt_map
, *vram_map
;
86 void **gtt_start
, **gtt_end
;
87 void **vram_start
, **vram_end
;
88 struct radeon_fence
*fence
= NULL
;
90 r
= radeon_bo_create(rdev
, size
, PAGE_SIZE
, true,
91 RADEON_GEM_DOMAIN_GTT
, 0, NULL
, NULL
,
94 DRM_ERROR("Failed to create GTT object %d\n", i
);
98 r
= radeon_bo_reserve(gtt_obj
[i
], false);
100 goto out_lclean_unref
;
101 r
= radeon_bo_pin(gtt_obj
[i
], RADEON_GEM_DOMAIN_GTT
, >t_addr
);
103 DRM_ERROR("Failed to pin GTT object %d\n", i
);
104 goto out_lclean_unres
;
107 r
= radeon_bo_kmap(gtt_obj
[i
], >t_map
);
109 DRM_ERROR("Failed to map GTT object %d\n", i
);
110 goto out_lclean_unpin
;
113 for (gtt_start
= gtt_map
, gtt_end
= gtt_map
+ size
;
116 *gtt_start
= gtt_start
;
118 radeon_bo_kunmap(gtt_obj
[i
]);
120 if (ring
== R600_RING_TYPE_DMA_INDEX
)
121 fence
= radeon_copy_dma(rdev
, gtt_addr
, vram_addr
,
122 size
/ RADEON_GPU_PAGE_SIZE
,
123 vram_obj
->tbo
.base
.resv
);
125 fence
= radeon_copy_blit(rdev
, gtt_addr
, vram_addr
,
126 size
/ RADEON_GPU_PAGE_SIZE
,
127 vram_obj
->tbo
.base
.resv
);
129 DRM_ERROR("Failed GTT->VRAM copy %d\n", i
);
131 goto out_lclean_unpin
;
134 r
= radeon_fence_wait(fence
, false);
136 DRM_ERROR("Failed to wait for GTT->VRAM fence %d\n", i
);
137 goto out_lclean_unpin
;
140 radeon_fence_unref(&fence
);
142 r
= radeon_bo_kmap(vram_obj
, &vram_map
);
144 DRM_ERROR("Failed to map VRAM object after copy %d\n", i
);
145 goto out_lclean_unpin
;
148 for (gtt_start
= gtt_map
, gtt_end
= gtt_map
+ size
,
149 vram_start
= vram_map
, vram_end
= vram_map
+ size
;
150 vram_start
< vram_end
;
151 gtt_start
++, vram_start
++) {
152 if (*vram_start
!= gtt_start
) {
153 DRM_ERROR("Incorrect GTT->VRAM copy %d: Got 0x%p, "
154 "expected 0x%p (GTT/VRAM offset "
155 "0x%16llx/0x%16llx)\n",
156 i
, *vram_start
, gtt_start
,
158 (gtt_addr
- rdev
->mc
.gtt_start
+
159 (void*)gtt_start
- gtt_map
),
161 (vram_addr
- rdev
->mc
.vram_start
+
162 (void*)gtt_start
- gtt_map
));
163 radeon_bo_kunmap(vram_obj
);
164 goto out_lclean_unpin
;
166 *vram_start
= vram_start
;
169 radeon_bo_kunmap(vram_obj
);
171 if (ring
== R600_RING_TYPE_DMA_INDEX
)
172 fence
= radeon_copy_dma(rdev
, vram_addr
, gtt_addr
,
173 size
/ RADEON_GPU_PAGE_SIZE
,
174 vram_obj
->tbo
.base
.resv
);
176 fence
= radeon_copy_blit(rdev
, vram_addr
, gtt_addr
,
177 size
/ RADEON_GPU_PAGE_SIZE
,
178 vram_obj
->tbo
.base
.resv
);
180 DRM_ERROR("Failed VRAM->GTT copy %d\n", i
);
182 goto out_lclean_unpin
;
185 r
= radeon_fence_wait(fence
, false);
187 DRM_ERROR("Failed to wait for VRAM->GTT fence %d\n", i
);
188 goto out_lclean_unpin
;
191 radeon_fence_unref(&fence
);
193 r
= radeon_bo_kmap(gtt_obj
[i
], >t_map
);
195 DRM_ERROR("Failed to map GTT object after copy %d\n", i
);
196 goto out_lclean_unpin
;
199 for (gtt_start
= gtt_map
, gtt_end
= gtt_map
+ size
,
200 vram_start
= vram_map
, vram_end
= vram_map
+ size
;
202 gtt_start
++, vram_start
++) {
203 if (*gtt_start
!= vram_start
) {
204 DRM_ERROR("Incorrect VRAM->GTT copy %d: Got 0x%p, "
205 "expected 0x%p (VRAM/GTT offset "
206 "0x%16llx/0x%16llx)\n",
207 i
, *gtt_start
, vram_start
,
209 (vram_addr
- rdev
->mc
.vram_start
+
210 (void*)vram_start
- vram_map
),
212 (gtt_addr
- rdev
->mc
.gtt_start
+
213 (void*)vram_start
- vram_map
));
214 radeon_bo_kunmap(gtt_obj
[i
]);
215 goto out_lclean_unpin
;
219 radeon_bo_kunmap(gtt_obj
[i
]);
221 DRM_INFO("Tested GTT->VRAM and VRAM->GTT copy for GTT offset 0x%llx\n",
222 gtt_addr
- rdev
->mc
.gtt_start
);
226 radeon_bo_unpin(gtt_obj
[i
]);
228 radeon_bo_unreserve(gtt_obj
[i
]);
230 radeon_bo_unref(>t_obj
[i
]);
232 for (--i
; i
>= 0; --i
) {
233 radeon_bo_unpin(gtt_obj
[i
]);
234 radeon_bo_unreserve(gtt_obj
[i
]);
235 radeon_bo_unref(>t_obj
[i
]);
237 if (fence
&& !IS_ERR(fence
))
238 radeon_fence_unref(&fence
);
242 radeon_bo_unpin(vram_obj
);
244 radeon_bo_unreserve(vram_obj
);
246 radeon_bo_unref(&vram_obj
);
250 pr_warn("Error while testing BO move\n");
254 void radeon_test_moves(struct radeon_device
*rdev
)
256 if (rdev
->asic
->copy
.dma
)
257 radeon_do_test_moves(rdev
, RADEON_TEST_COPY_DMA
);
258 if (rdev
->asic
->copy
.blit
)
259 radeon_do_test_moves(rdev
, RADEON_TEST_COPY_BLIT
);
262 static int radeon_test_create_and_emit_fence(struct radeon_device
*rdev
,
263 struct radeon_ring
*ring
,
264 struct radeon_fence
**fence
)
266 uint32_t handle
= ring
->idx
^ 0xdeafbeef;
269 if (ring
->idx
== R600_RING_TYPE_UVD_INDEX
) {
270 r
= radeon_uvd_get_create_msg(rdev
, ring
->idx
, handle
, NULL
);
272 DRM_ERROR("Failed to get dummy create msg\n");
276 r
= radeon_uvd_get_destroy_msg(rdev
, ring
->idx
, handle
, fence
);
278 DRM_ERROR("Failed to get dummy destroy msg\n");
282 } else if (ring
->idx
== TN_RING_TYPE_VCE1_INDEX
||
283 ring
->idx
== TN_RING_TYPE_VCE2_INDEX
) {
284 r
= radeon_vce_get_create_msg(rdev
, ring
->idx
, handle
, NULL
);
286 DRM_ERROR("Failed to get dummy create msg\n");
290 r
= radeon_vce_get_destroy_msg(rdev
, ring
->idx
, handle
, fence
);
292 DRM_ERROR("Failed to get dummy destroy msg\n");
297 r
= radeon_ring_lock(rdev
, ring
, 64);
299 DRM_ERROR("Failed to lock ring A %d\n", ring
->idx
);
302 r
= radeon_fence_emit(rdev
, fence
, ring
->idx
);
304 DRM_ERROR("Failed to emit fence\n");
305 radeon_ring_unlock_undo(rdev
, ring
);
308 radeon_ring_unlock_commit(rdev
, ring
, false);
313 void radeon_test_ring_sync(struct radeon_device
*rdev
,
314 struct radeon_ring
*ringA
,
315 struct radeon_ring
*ringB
)
317 struct radeon_fence
*fence1
= NULL
, *fence2
= NULL
;
318 struct radeon_semaphore
*semaphore
= NULL
;
321 r
= radeon_semaphore_create(rdev
, &semaphore
);
323 DRM_ERROR("Failed to create semaphore\n");
327 r
= radeon_ring_lock(rdev
, ringA
, 64);
329 DRM_ERROR("Failed to lock ring A %d\n", ringA
->idx
);
332 radeon_semaphore_emit_wait(rdev
, ringA
->idx
, semaphore
);
333 radeon_ring_unlock_commit(rdev
, ringA
, false);
335 r
= radeon_test_create_and_emit_fence(rdev
, ringA
, &fence1
);
339 r
= radeon_ring_lock(rdev
, ringA
, 64);
341 DRM_ERROR("Failed to lock ring A %d\n", ringA
->idx
);
344 radeon_semaphore_emit_wait(rdev
, ringA
->idx
, semaphore
);
345 radeon_ring_unlock_commit(rdev
, ringA
, false);
347 r
= radeon_test_create_and_emit_fence(rdev
, ringA
, &fence2
);
353 if (radeon_fence_signaled(fence1
)) {
354 DRM_ERROR("Fence 1 signaled without waiting for semaphore.\n");
358 r
= radeon_ring_lock(rdev
, ringB
, 64);
360 DRM_ERROR("Failed to lock ring B %p\n", ringB
);
363 radeon_semaphore_emit_signal(rdev
, ringB
->idx
, semaphore
);
364 radeon_ring_unlock_commit(rdev
, ringB
, false);
366 r
= radeon_fence_wait(fence1
, false);
368 DRM_ERROR("Failed to wait for sync fence 1\n");
374 if (radeon_fence_signaled(fence2
)) {
375 DRM_ERROR("Fence 2 signaled without waiting for semaphore.\n");
379 r
= radeon_ring_lock(rdev
, ringB
, 64);
381 DRM_ERROR("Failed to lock ring B %p\n", ringB
);
384 radeon_semaphore_emit_signal(rdev
, ringB
->idx
, semaphore
);
385 radeon_ring_unlock_commit(rdev
, ringB
, false);
387 r
= radeon_fence_wait(fence2
, false);
389 DRM_ERROR("Failed to wait for sync fence 1\n");
394 radeon_semaphore_free(rdev
, &semaphore
, NULL
);
397 radeon_fence_unref(&fence1
);
400 radeon_fence_unref(&fence2
);
403 pr_warn("Error while testing ring sync (%d)\n", r
);
406 static void radeon_test_ring_sync2(struct radeon_device
*rdev
,
407 struct radeon_ring
*ringA
,
408 struct radeon_ring
*ringB
,
409 struct radeon_ring
*ringC
)
411 struct radeon_fence
*fenceA
= NULL
, *fenceB
= NULL
;
412 struct radeon_semaphore
*semaphore
= NULL
;
416 r
= radeon_semaphore_create(rdev
, &semaphore
);
418 DRM_ERROR("Failed to create semaphore\n");
422 r
= radeon_ring_lock(rdev
, ringA
, 64);
424 DRM_ERROR("Failed to lock ring A %d\n", ringA
->idx
);
427 radeon_semaphore_emit_wait(rdev
, ringA
->idx
, semaphore
);
428 radeon_ring_unlock_commit(rdev
, ringA
, false);
430 r
= radeon_test_create_and_emit_fence(rdev
, ringA
, &fenceA
);
434 r
= radeon_ring_lock(rdev
, ringB
, 64);
436 DRM_ERROR("Failed to lock ring B %d\n", ringB
->idx
);
439 radeon_semaphore_emit_wait(rdev
, ringB
->idx
, semaphore
);
440 radeon_ring_unlock_commit(rdev
, ringB
, false);
441 r
= radeon_test_create_and_emit_fence(rdev
, ringB
, &fenceB
);
447 if (radeon_fence_signaled(fenceA
)) {
448 DRM_ERROR("Fence A signaled without waiting for semaphore.\n");
451 if (radeon_fence_signaled(fenceB
)) {
452 DRM_ERROR("Fence B signaled without waiting for semaphore.\n");
456 r
= radeon_ring_lock(rdev
, ringC
, 64);
458 DRM_ERROR("Failed to lock ring B %p\n", ringC
);
461 radeon_semaphore_emit_signal(rdev
, ringC
->idx
, semaphore
);
462 radeon_ring_unlock_commit(rdev
, ringC
, false);
464 for (i
= 0; i
< 30; ++i
) {
466 sigA
= radeon_fence_signaled(fenceA
);
467 sigB
= radeon_fence_signaled(fenceB
);
472 if (!sigA
&& !sigB
) {
473 DRM_ERROR("Neither fence A nor B has been signaled\n");
475 } else if (sigA
&& sigB
) {
476 DRM_ERROR("Both fence A and B has been signaled\n");
480 DRM_INFO("Fence %c was first signaled\n", sigA
? 'A' : 'B');
482 r
= radeon_ring_lock(rdev
, ringC
, 64);
484 DRM_ERROR("Failed to lock ring B %p\n", ringC
);
487 radeon_semaphore_emit_signal(rdev
, ringC
->idx
, semaphore
);
488 radeon_ring_unlock_commit(rdev
, ringC
, false);
492 r
= radeon_fence_wait(fenceA
, false);
494 DRM_ERROR("Failed to wait for sync fence A\n");
497 r
= radeon_fence_wait(fenceB
, false);
499 DRM_ERROR("Failed to wait for sync fence B\n");
504 radeon_semaphore_free(rdev
, &semaphore
, NULL
);
507 radeon_fence_unref(&fenceA
);
510 radeon_fence_unref(&fenceB
);
513 pr_warn("Error while testing ring sync (%d)\n", r
);
516 static bool radeon_test_sync_possible(struct radeon_ring
*ringA
,
517 struct radeon_ring
*ringB
)
519 if (ringA
->idx
== TN_RING_TYPE_VCE2_INDEX
&&
520 ringB
->idx
== TN_RING_TYPE_VCE1_INDEX
)
526 void radeon_test_syncing(struct radeon_device
*rdev
)
530 for (i
= 1; i
< RADEON_NUM_RINGS
; ++i
) {
531 struct radeon_ring
*ringA
= &rdev
->ring
[i
];
535 for (j
= 0; j
< i
; ++j
) {
536 struct radeon_ring
*ringB
= &rdev
->ring
[j
];
540 if (!radeon_test_sync_possible(ringA
, ringB
))
543 DRM_INFO("Testing syncing between rings %d and %d...\n", i
, j
);
544 radeon_test_ring_sync(rdev
, ringA
, ringB
);
546 DRM_INFO("Testing syncing between rings %d and %d...\n", j
, i
);
547 radeon_test_ring_sync(rdev
, ringB
, ringA
);
549 for (k
= 0; k
< j
; ++k
) {
550 struct radeon_ring
*ringC
= &rdev
->ring
[k
];
554 if (!radeon_test_sync_possible(ringA
, ringC
))
557 if (!radeon_test_sync_possible(ringB
, ringC
))
560 DRM_INFO("Testing syncing between rings %d, %d and %d...\n", i
, j
, k
);
561 radeon_test_ring_sync2(rdev
, ringA
, ringB
, ringC
);
563 DRM_INFO("Testing syncing between rings %d, %d and %d...\n", i
, k
, j
);
564 radeon_test_ring_sync2(rdev
, ringA
, ringC
, ringB
);
566 DRM_INFO("Testing syncing between rings %d, %d and %d...\n", j
, i
, k
);
567 radeon_test_ring_sync2(rdev
, ringB
, ringA
, ringC
);
569 DRM_INFO("Testing syncing between rings %d, %d and %d...\n", j
, k
, i
);
570 radeon_test_ring_sync2(rdev
, ringB
, ringC
, ringA
);
572 DRM_INFO("Testing syncing between rings %d, %d and %d...\n", k
, i
, j
);
573 radeon_test_ring_sync2(rdev
, ringC
, ringA
, ringB
);
575 DRM_INFO("Testing syncing between rings %d, %d and %d...\n", k
, j
, i
);
576 radeon_test_ring_sync2(rdev
, ringC
, ringB
, ringA
);