radeon_test.c 14.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*
 * Copyright 2009 VMware, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * Authors: Michel Dänzer
 */
#include <drm/drmP.h>
#include <drm/radeon_drm.h>
#include "radeon_reg.h"
#include "radeon.h"

29 30 31
#define RADEON_TEST_COPY_BLIT 1
#define RADEON_TEST_COPY_DMA  0

32 33

/* Test BO GTT->VRAM and VRAM->GTT GPU copies across the whole GTT aperture */
34
static void radeon_do_test_moves(struct radeon_device *rdev, int flag)
35
{
36 37
	struct radeon_bo *vram_obj = NULL;
	struct radeon_bo **gtt_obj = NULL;
38
	uint64_t gtt_addr, vram_addr;
39 40
	unsigned n, size;
	int i, r, ring;
41 42 43 44 45 46 47 48 49 50 51 52

	switch (flag) {
	case RADEON_TEST_COPY_DMA:
		ring = radeon_copy_dma_ring_index(rdev);
		break;
	case RADEON_TEST_COPY_BLIT:
		ring = radeon_copy_blit_ring_index(rdev);
		break;
	default:
		DRM_ERROR("Unknown copy method\n");
		return;
	}
53 54 55 56

	size = 1024 * 1024;

	/* Number of tests =
57
	 * (Total GTT - IB pool - writeback page - ring buffers) / test size
58
	 */
59
	n = rdev->mc.gtt_size - RADEON_IB_POOL_SIZE*64*1024;
60
	for (i = 0; i < RADEON_NUM_RINGS; ++i)
61
		n -= rdev->ring[i].ring_size;
62 63 64 65 66
	if (rdev->wb.wb_obj)
		n -= RADEON_GPU_PAGE_SIZE;
	if (rdev->ih.ring_obj)
		n -= rdev->ih.ring_size;
	n /= size;
67 68 69 70 71 72 73 74

	gtt_obj = kzalloc(n * sizeof(*gtt_obj), GFP_KERNEL);
	if (!gtt_obj) {
		DRM_ERROR("Failed to allocate %d pointers\n", n);
		r = 1;
		goto out_cleanup;
	}

75
	r = radeon_bo_create(rdev, size, PAGE_SIZE, true, RADEON_GEM_DOMAIN_VRAM,
76
			     0, NULL, &vram_obj);
77 78 79 80
	if (r) {
		DRM_ERROR("Failed to create VRAM object\n");
		goto out_cleanup;
	}
81 82
	r = radeon_bo_reserve(vram_obj, false);
	if (unlikely(r != 0))
83
		goto out_unref;
84
	r = radeon_bo_pin(vram_obj, RADEON_GEM_DOMAIN_VRAM, &vram_addr);
85 86
	if (r) {
		DRM_ERROR("Failed to pin VRAM object\n");
87
		goto out_unres;
88 89 90 91 92
	}
	for (i = 0; i < n; i++) {
		void *gtt_map, *vram_map;
		void **gtt_start, **gtt_end;
		void **vram_start, **vram_end;
93
		struct radeon_fence *fence = NULL;
94

95
		r = radeon_bo_create(rdev, size, PAGE_SIZE, true,
96
				     RADEON_GEM_DOMAIN_GTT, 0, NULL, gtt_obj + i);
97 98
		if (r) {
			DRM_ERROR("Failed to create GTT object %d\n", i);
99
			goto out_lclean;
100 101
		}

102 103
		r = radeon_bo_reserve(gtt_obj[i], false);
		if (unlikely(r != 0))
104
			goto out_lclean_unref;
105
		r = radeon_bo_pin(gtt_obj[i], RADEON_GEM_DOMAIN_GTT, &gtt_addr);
106 107
		if (r) {
			DRM_ERROR("Failed to pin GTT object %d\n", i);
108
			goto out_lclean_unres;
109 110
		}

111
		r = radeon_bo_kmap(gtt_obj[i], &gtt_map);
112 113
		if (r) {
			DRM_ERROR("Failed to map GTT object %d\n", i);
114
			goto out_lclean_unpin;
115 116 117 118 119 120 121
		}

		for (gtt_start = gtt_map, gtt_end = gtt_map + size;
		     gtt_start < gtt_end;
		     gtt_start++)
			*gtt_start = gtt_start;

122
		radeon_bo_kunmap(gtt_obj[i]);
123

124 125 126 127
		if (ring == R600_RING_TYPE_DMA_INDEX)
			r = radeon_copy_dma(rdev, gtt_addr, vram_addr, size / RADEON_GPU_PAGE_SIZE, &fence);
		else
			r = radeon_copy_blit(rdev, gtt_addr, vram_addr, size / RADEON_GPU_PAGE_SIZE, &fence);
128 129
		if (r) {
			DRM_ERROR("Failed GTT->VRAM copy %d\n", i);
130
			goto out_lclean_unpin;
131 132 133 134 135
		}

		r = radeon_fence_wait(fence, false);
		if (r) {
			DRM_ERROR("Failed to wait for GTT->VRAM fence %d\n", i);
136
			goto out_lclean_unpin;
137 138 139 140
		}

		radeon_fence_unref(&fence);

141
		r = radeon_bo_kmap(vram_obj, &vram_map);
142 143
		if (r) {
			DRM_ERROR("Failed to map VRAM object after copy %d\n", i);
144
			goto out_lclean_unpin;
145 146 147 148 149 150 151 152
		}

		for (gtt_start = gtt_map, gtt_end = gtt_map + size,
		     vram_start = vram_map, vram_end = vram_map + size;
		     vram_start < vram_end;
		     gtt_start++, vram_start++) {
			if (*vram_start != gtt_start) {
				DRM_ERROR("Incorrect GTT->VRAM copy %d: Got 0x%p, "
153 154 155 156 157 158 159 160 161
					  "expected 0x%p (GTT/VRAM offset "
					  "0x%16llx/0x%16llx)\n",
					  i, *vram_start, gtt_start,
					  (unsigned long long)
					  (gtt_addr - rdev->mc.gtt_start +
					   (void*)gtt_start - gtt_map),
					  (unsigned long long)
					  (vram_addr - rdev->mc.vram_start +
					   (void*)gtt_start - gtt_map));
162
				radeon_bo_kunmap(vram_obj);
163
				goto out_lclean_unpin;
164 165 166 167
			}
			*vram_start = vram_start;
		}

168
		radeon_bo_kunmap(vram_obj);
169

170 171 172 173
		if (ring == R600_RING_TYPE_DMA_INDEX)
			r = radeon_copy_dma(rdev, vram_addr, gtt_addr, size / RADEON_GPU_PAGE_SIZE, &fence);
		else
			r = radeon_copy_blit(rdev, vram_addr, gtt_addr, size / RADEON_GPU_PAGE_SIZE, &fence);
174 175
		if (r) {
			DRM_ERROR("Failed VRAM->GTT copy %d\n", i);
176
			goto out_lclean_unpin;
177 178 179 180 181
		}

		r = radeon_fence_wait(fence, false);
		if (r) {
			DRM_ERROR("Failed to wait for VRAM->GTT fence %d\n", i);
182
			goto out_lclean_unpin;
183 184 185 186
		}

		radeon_fence_unref(&fence);

187
		r = radeon_bo_kmap(gtt_obj[i], &gtt_map);
188 189
		if (r) {
			DRM_ERROR("Failed to map GTT object after copy %d\n", i);
190
			goto out_lclean_unpin;
191 192 193 194 195 196 197 198
		}

		for (gtt_start = gtt_map, gtt_end = gtt_map + size,
		     vram_start = vram_map, vram_end = vram_map + size;
		     gtt_start < gtt_end;
		     gtt_start++, vram_start++) {
			if (*gtt_start != vram_start) {
				DRM_ERROR("Incorrect VRAM->GTT copy %d: Got 0x%p, "
199 200 201 202 203 204 205 206 207
					  "expected 0x%p (VRAM/GTT offset "
					  "0x%16llx/0x%16llx)\n",
					  i, *gtt_start, vram_start,
					  (unsigned long long)
					  (vram_addr - rdev->mc.vram_start +
					   (void*)vram_start - vram_map),
					  (unsigned long long)
					  (gtt_addr - rdev->mc.gtt_start +
					   (void*)vram_start - vram_map));
208
				radeon_bo_kunmap(gtt_obj[i]);
209
				goto out_lclean_unpin;
210 211 212
			}
		}

213
		radeon_bo_kunmap(gtt_obj[i]);
214 215

		DRM_INFO("Tested GTT->VRAM and VRAM->GTT copy for GTT offset 0x%llx\n",
216
			 gtt_addr - rdev->mc.gtt_start);
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
		continue;

out_lclean_unpin:
		radeon_bo_unpin(gtt_obj[i]);
out_lclean_unres:
		radeon_bo_unreserve(gtt_obj[i]);
out_lclean_unref:
		radeon_bo_unref(&gtt_obj[i]);
out_lclean:
		for (--i; i >= 0; --i) {
			radeon_bo_unpin(gtt_obj[i]);
			radeon_bo_unreserve(gtt_obj[i]);
			radeon_bo_unref(&gtt_obj[i]);
		}
		if (fence)
			radeon_fence_unref(&fence);
		break;
234 235
	}

236 237 238 239 240
	radeon_bo_unpin(vram_obj);
out_unres:
	radeon_bo_unreserve(vram_obj);
out_unref:
	radeon_bo_unref(&vram_obj);
241
out_cleanup:
242
	kfree(gtt_obj);
243 244 245 246
	if (r) {
		printk(KERN_WARNING "Error while testing BO move.\n");
	}
}
247

248 249 250 251 252 253 254 255
void radeon_test_moves(struct radeon_device *rdev)
{
	if (rdev->asic->copy.dma)
		radeon_do_test_moves(rdev, RADEON_TEST_COPY_DMA);
	if (rdev->asic->copy.blit)
		radeon_do_test_moves(rdev, RADEON_TEST_COPY_BLIT);
}

C
Christian König 已提交
256 257 258 259
static int radeon_test_create_and_emit_fence(struct radeon_device *rdev,
					     struct radeon_ring *ring,
					     struct radeon_fence **fence)
{
260
	uint32_t handle = ring->idx ^ 0xdeafbeef;
C
Christian König 已提交
261 262 263
	int r;

	if (ring->idx == R600_RING_TYPE_UVD_INDEX) {
264
		r = radeon_uvd_get_create_msg(rdev, ring->idx, handle, NULL);
C
Christian König 已提交
265 266 267 268 269
		if (r) {
			DRM_ERROR("Failed to get dummy create msg\n");
			return r;
		}

270
		r = radeon_uvd_get_destroy_msg(rdev, ring->idx, handle, fence);
C
Christian König 已提交
271 272 273 274
		if (r) {
			DRM_ERROR("Failed to get dummy destroy msg\n");
			return r;
		}
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289

	} else if (ring->idx == TN_RING_TYPE_VCE1_INDEX ||
		   ring->idx == TN_RING_TYPE_VCE2_INDEX) {
		r = radeon_vce_get_create_msg(rdev, ring->idx, handle, NULL);
		if (r) {
			DRM_ERROR("Failed to get dummy create msg\n");
			return r;
		}

		r = radeon_vce_get_destroy_msg(rdev, ring->idx, handle, fence);
		if (r) {
			DRM_ERROR("Failed to get dummy destroy msg\n");
			return r;
		}

C
Christian König 已提交
290 291 292 293 294 295 296 297 298 299 300 301
	} else {
		r = radeon_ring_lock(rdev, ring, 64);
		if (r) {
			DRM_ERROR("Failed to lock ring A %d\n", ring->idx);
			return r;
		}
		radeon_fence_emit(rdev, fence, ring->idx);
		radeon_ring_unlock_commit(rdev, ring);
	}
	return 0;
}

302
void radeon_test_ring_sync(struct radeon_device *rdev,
303 304
			   struct radeon_ring *ringA,
			   struct radeon_ring *ringB)
305
{
306
	struct radeon_fence *fence1 = NULL, *fence2 = NULL;
307 308 309 310 311 312 313 314 315
	struct radeon_semaphore *semaphore = NULL;
	int r;

	r = radeon_semaphore_create(rdev, &semaphore);
	if (r) {
		DRM_ERROR("Failed to create semaphore\n");
		goto out_cleanup;
	}

316
	r = radeon_ring_lock(rdev, ringA, 64);
317
	if (r) {
318
		DRM_ERROR("Failed to lock ring A %d\n", ringA->idx);
319 320
		goto out_cleanup;
	}
321
	radeon_semaphore_emit_wait(rdev, ringA->idx, semaphore);
C
Christian König 已提交
322 323 324 325
	radeon_ring_unlock_commit(rdev, ringA);

	r = radeon_test_create_and_emit_fence(rdev, ringA, &fence1);
	if (r)
326
		goto out_cleanup;
C
Christian König 已提交
327 328

	r = radeon_ring_lock(rdev, ringA, 64);
329
	if (r) {
C
Christian König 已提交
330
		DRM_ERROR("Failed to lock ring A %d\n", ringA->idx);
331 332
		goto out_cleanup;
	}
C
Christian König 已提交
333
	radeon_semaphore_emit_wait(rdev, ringA->idx, semaphore);
334
	radeon_ring_unlock_commit(rdev, ringA);
335

C
Christian König 已提交
336 337 338 339
	r = radeon_test_create_and_emit_fence(rdev, ringA, &fence2);
	if (r)
		goto out_cleanup;

340 341
	mdelay(1000);

342 343
	if (radeon_fence_signaled(fence1)) {
		DRM_ERROR("Fence 1 signaled without waiting for semaphore.\n");
344 345 346
		goto out_cleanup;
	}

347
	r = radeon_ring_lock(rdev, ringB, 64);
348
	if (r) {
349
		DRM_ERROR("Failed to lock ring B %p\n", ringB);
350 351
		goto out_cleanup;
	}
352
	radeon_semaphore_emit_signal(rdev, ringB->idx, semaphore);
353
	radeon_ring_unlock_commit(rdev, ringB);
354

355 356 357 358 359 360 361 362 363 364 365 366 367 368
	r = radeon_fence_wait(fence1, false);
	if (r) {
		DRM_ERROR("Failed to wait for sync fence 1\n");
		goto out_cleanup;
	}

	mdelay(1000);

	if (radeon_fence_signaled(fence2)) {
		DRM_ERROR("Fence 2 signaled without waiting for semaphore.\n");
		goto out_cleanup;
	}

	r = radeon_ring_lock(rdev, ringB, 64);
369
	if (r) {
370
		DRM_ERROR("Failed to lock ring B %p\n", ringB);
371 372
		goto out_cleanup;
	}
373
	radeon_semaphore_emit_signal(rdev, ringB->idx, semaphore);
374
	radeon_ring_unlock_commit(rdev, ringB);
375

376 377 378 379 380
	r = radeon_fence_wait(fence2, false);
	if (r) {
		DRM_ERROR("Failed to wait for sync fence 1\n");
		goto out_cleanup;
	}
381 382

out_cleanup:
383
	radeon_semaphore_free(rdev, &semaphore, NULL);
384

385 386 387 388 389 390 391 392 393 394
	if (fence1)
		radeon_fence_unref(&fence1);

	if (fence2)
		radeon_fence_unref(&fence2);

	if (r)
		printk(KERN_WARNING "Error while testing ring sync (%d).\n", r);
}

395
static void radeon_test_ring_sync2(struct radeon_device *rdev,
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
			    struct radeon_ring *ringA,
			    struct radeon_ring *ringB,
			    struct radeon_ring *ringC)
{
	struct radeon_fence *fenceA = NULL, *fenceB = NULL;
	struct radeon_semaphore *semaphore = NULL;
	bool sigA, sigB;
	int i, r;

	r = radeon_semaphore_create(rdev, &semaphore);
	if (r) {
		DRM_ERROR("Failed to create semaphore\n");
		goto out_cleanup;
	}

	r = radeon_ring_lock(rdev, ringA, 64);
	if (r) {
413
		DRM_ERROR("Failed to lock ring A %d\n", ringA->idx);
414 415
		goto out_cleanup;
	}
416
	radeon_semaphore_emit_wait(rdev, ringA->idx, semaphore);
417 418
	radeon_ring_unlock_commit(rdev, ringA);

C
Christian König 已提交
419 420 421 422
	r = radeon_test_create_and_emit_fence(rdev, ringA, &fenceA);
	if (r)
		goto out_cleanup;

423 424
	r = radeon_ring_lock(rdev, ringB, 64);
	if (r) {
425
		DRM_ERROR("Failed to lock ring B %d\n", ringB->idx);
426 427
		goto out_cleanup;
	}
428
	radeon_semaphore_emit_wait(rdev, ringB->idx, semaphore);
429
	radeon_ring_unlock_commit(rdev, ringB);
C
Christian König 已提交
430 431 432
	r = radeon_test_create_and_emit_fence(rdev, ringB, &fenceB);
	if (r)
		goto out_cleanup;
433 434 435 436 437 438 439 440

	mdelay(1000);

	if (radeon_fence_signaled(fenceA)) {
		DRM_ERROR("Fence A signaled without waiting for semaphore.\n");
		goto out_cleanup;
	}
	if (radeon_fence_signaled(fenceB)) {
C
Christian König 已提交
441
		DRM_ERROR("Fence B signaled without waiting for semaphore.\n");
442 443 444 445 446 447 448 449
		goto out_cleanup;
	}

	r = radeon_ring_lock(rdev, ringC, 64);
	if (r) {
		DRM_ERROR("Failed to lock ring B %p\n", ringC);
		goto out_cleanup;
	}
450
	radeon_semaphore_emit_signal(rdev, ringC->idx, semaphore);
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
	radeon_ring_unlock_commit(rdev, ringC);

	for (i = 0; i < 30; ++i) {
		mdelay(100);
		sigA = radeon_fence_signaled(fenceA);
		sigB = radeon_fence_signaled(fenceB);
		if (sigA || sigB)
			break;
	}

	if (!sigA && !sigB) {
		DRM_ERROR("Neither fence A nor B has been signaled\n");
		goto out_cleanup;
	} else if (sigA && sigB) {
		DRM_ERROR("Both fence A and B has been signaled\n");
		goto out_cleanup;
	}

	DRM_INFO("Fence %c was first signaled\n", sigA ? 'A' : 'B');

	r = radeon_ring_lock(rdev, ringC, 64);
	if (r) {
		DRM_ERROR("Failed to lock ring B %p\n", ringC);
		goto out_cleanup;
	}
476
	radeon_semaphore_emit_signal(rdev, ringC->idx, semaphore);
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
	radeon_ring_unlock_commit(rdev, ringC);

	mdelay(1000);

	r = radeon_fence_wait(fenceA, false);
	if (r) {
		DRM_ERROR("Failed to wait for sync fence A\n");
		goto out_cleanup;
	}
	r = radeon_fence_wait(fenceB, false);
	if (r) {
		DRM_ERROR("Failed to wait for sync fence B\n");
		goto out_cleanup;
	}

out_cleanup:
493
	radeon_semaphore_free(rdev, &semaphore, NULL);
494 495 496 497 498 499

	if (fenceA)
		radeon_fence_unref(&fenceA);

	if (fenceB)
		radeon_fence_unref(&fenceB);
500 501 502 503 504

	if (r)
		printk(KERN_WARNING "Error while testing ring sync (%d).\n", r);
}

505 506 507 508 509 510 511 512 513 514
static bool radeon_test_sync_possible(struct radeon_ring *ringA,
				      struct radeon_ring *ringB)
{
	if (ringA->idx == TN_RING_TYPE_VCE2_INDEX &&
	    ringB->idx == TN_RING_TYPE_VCE1_INDEX)
		return false;

	return true;
}

515 516
void radeon_test_syncing(struct radeon_device *rdev)
{
517
	int i, j, k;
518 519

	for (i = 1; i < RADEON_NUM_RINGS; ++i) {
520 521
		struct radeon_ring *ringA = &rdev->ring[i];
		if (!ringA->ready)
522 523 524
			continue;

		for (j = 0; j < i; ++j) {
525 526
			struct radeon_ring *ringB = &rdev->ring[j];
			if (!ringB->ready)
527 528
				continue;

529 530 531
			if (!radeon_test_sync_possible(ringA, ringB))
				continue;

532
			DRM_INFO("Testing syncing between rings %d and %d...\n", i, j);
533
			radeon_test_ring_sync(rdev, ringA, ringB);
534

535
			DRM_INFO("Testing syncing between rings %d and %d...\n", j, i);
536
			radeon_test_ring_sync(rdev, ringB, ringA);
537 538 539

			for (k = 0; k < j; ++k) {
				struct radeon_ring *ringC = &rdev->ring[k];
540 541
				if (!ringC->ready)
					continue;
542

543 544 545 546 547 548
				if (!radeon_test_sync_possible(ringA, ringC))
					continue;

				if (!radeon_test_sync_possible(ringB, ringC))
					continue;

549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
				DRM_INFO("Testing syncing between rings %d, %d and %d...\n", i, j, k);
				radeon_test_ring_sync2(rdev, ringA, ringB, ringC);

				DRM_INFO("Testing syncing between rings %d, %d and %d...\n", i, k, j);
				radeon_test_ring_sync2(rdev, ringA, ringC, ringB);

				DRM_INFO("Testing syncing between rings %d, %d and %d...\n", j, i, k);
				radeon_test_ring_sync2(rdev, ringB, ringA, ringC);

				DRM_INFO("Testing syncing between rings %d, %d and %d...\n", j, k, i);
				radeon_test_ring_sync2(rdev, ringB, ringC, ringA);

				DRM_INFO("Testing syncing between rings %d, %d and %d...\n", k, i, j);
				radeon_test_ring_sync2(rdev, ringC, ringA, ringB);

				DRM_INFO("Testing syncing between rings %d, %d and %d...\n", k, j, i);
				radeon_test_ring_sync2(rdev, ringC, ringB, ringA);
			}
567 568 569
		}
	}
}