amdgpu_test.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. /*
  2. * Copyright 2009 VMware, Inc.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  18. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  19. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. * OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * Authors: Michel Dänzer
  23. */
  24. #include <drm/drmP.h>
  25. #include <drm/amdgpu_drm.h>
  26. #include "amdgpu.h"
  27. #include "amdgpu_uvd.h"
  28. #include "amdgpu_vce.h"
  29. /* Test BO GTT->VRAM and VRAM->GTT GPU copies across the whole GTT aperture */
  30. static void amdgpu_do_test_moves(struct amdgpu_device *adev)
  31. {
  32. struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
  33. struct amdgpu_bo *vram_obj = NULL;
  34. struct amdgpu_bo **gtt_obj = NULL;
  35. uint64_t gtt_addr, vram_addr;
  36. unsigned n, size;
  37. int i, r;
  38. size = 1024 * 1024;
  39. /* Number of tests =
  40. * (Total GTT - IB pool - writeback page - ring buffers) / test size
  41. */
  42. n = adev->mc.gtt_size - AMDGPU_IB_POOL_SIZE*64*1024;
  43. for (i = 0; i < AMDGPU_MAX_RINGS; ++i)
  44. if (adev->rings[i])
  45. n -= adev->rings[i]->ring_size;
  46. if (adev->wb.wb_obj)
  47. n -= AMDGPU_GPU_PAGE_SIZE;
  48. if (adev->irq.ih.ring_obj)
  49. n -= adev->irq.ih.ring_size;
  50. n /= size;
  51. gtt_obj = kzalloc(n * sizeof(*gtt_obj), GFP_KERNEL);
  52. if (!gtt_obj) {
  53. DRM_ERROR("Failed to allocate %d pointers\n", n);
  54. r = 1;
  55. goto out_cleanup;
  56. }
  57. r = amdgpu_bo_create(adev, size, PAGE_SIZE, true,
  58. AMDGPU_GEM_DOMAIN_VRAM, 0,
  59. NULL, NULL, &vram_obj);
  60. if (r) {
  61. DRM_ERROR("Failed to create VRAM object\n");
  62. goto out_cleanup;
  63. }
  64. r = amdgpu_bo_reserve(vram_obj, false);
  65. if (unlikely(r != 0))
  66. goto out_unref;
  67. r = amdgpu_bo_pin(vram_obj, AMDGPU_GEM_DOMAIN_VRAM, &vram_addr);
  68. if (r) {
  69. DRM_ERROR("Failed to pin VRAM object\n");
  70. goto out_unres;
  71. }
  72. for (i = 0; i < n; i++) {
  73. void *gtt_map, *vram_map;
  74. void **gtt_start, **gtt_end;
  75. void **vram_start, **vram_end;
  76. struct fence *fence = NULL;
  77. r = amdgpu_bo_create(adev, size, PAGE_SIZE, true,
  78. AMDGPU_GEM_DOMAIN_GTT, 0, NULL,
  79. NULL, gtt_obj + i);
  80. if (r) {
  81. DRM_ERROR("Failed to create GTT object %d\n", i);
  82. goto out_lclean;
  83. }
  84. r = amdgpu_bo_reserve(gtt_obj[i], false);
  85. if (unlikely(r != 0))
  86. goto out_lclean_unref;
  87. r = amdgpu_bo_pin(gtt_obj[i], AMDGPU_GEM_DOMAIN_GTT, &gtt_addr);
  88. if (r) {
  89. DRM_ERROR("Failed to pin GTT object %d\n", i);
  90. goto out_lclean_unres;
  91. }
  92. r = amdgpu_bo_kmap(gtt_obj[i], &gtt_map);
  93. if (r) {
  94. DRM_ERROR("Failed to map GTT object %d\n", i);
  95. goto out_lclean_unpin;
  96. }
  97. for (gtt_start = gtt_map, gtt_end = gtt_map + size;
  98. gtt_start < gtt_end;
  99. gtt_start++)
  100. *gtt_start = gtt_start;
  101. amdgpu_bo_kunmap(gtt_obj[i]);
  102. r = amdgpu_copy_buffer(ring, gtt_addr, vram_addr,
  103. size, NULL, &fence);
  104. if (r) {
  105. DRM_ERROR("Failed GTT->VRAM copy %d\n", i);
  106. goto out_lclean_unpin;
  107. }
  108. r = fence_wait(fence, false);
  109. if (r) {
  110. DRM_ERROR("Failed to wait for GTT->VRAM fence %d\n", i);
  111. goto out_lclean_unpin;
  112. }
  113. fence_put(fence);
  114. r = amdgpu_bo_kmap(vram_obj, &vram_map);
  115. if (r) {
  116. DRM_ERROR("Failed to map VRAM object after copy %d\n", i);
  117. goto out_lclean_unpin;
  118. }
  119. for (gtt_start = gtt_map, gtt_end = gtt_map + size,
  120. vram_start = vram_map, vram_end = vram_map + size;
  121. vram_start < vram_end;
  122. gtt_start++, vram_start++) {
  123. if (*vram_start != gtt_start) {
  124. DRM_ERROR("Incorrect GTT->VRAM copy %d: Got 0x%p, "
  125. "expected 0x%p (GTT/VRAM offset "
  126. "0x%16llx/0x%16llx)\n",
  127. i, *vram_start, gtt_start,
  128. (unsigned long long)
  129. (gtt_addr - adev->mc.gtt_start +
  130. (void*)gtt_start - gtt_map),
  131. (unsigned long long)
  132. (vram_addr - adev->mc.vram_start +
  133. (void*)gtt_start - gtt_map));
  134. amdgpu_bo_kunmap(vram_obj);
  135. goto out_lclean_unpin;
  136. }
  137. *vram_start = vram_start;
  138. }
  139. amdgpu_bo_kunmap(vram_obj);
  140. r = amdgpu_copy_buffer(ring, vram_addr, gtt_addr,
  141. size, NULL, &fence);
  142. if (r) {
  143. DRM_ERROR("Failed VRAM->GTT copy %d\n", i);
  144. goto out_lclean_unpin;
  145. }
  146. r = fence_wait(fence, false);
  147. if (r) {
  148. DRM_ERROR("Failed to wait for VRAM->GTT fence %d\n", i);
  149. goto out_lclean_unpin;
  150. }
  151. fence_put(fence);
  152. r = amdgpu_bo_kmap(gtt_obj[i], &gtt_map);
  153. if (r) {
  154. DRM_ERROR("Failed to map GTT object after copy %d\n", i);
  155. goto out_lclean_unpin;
  156. }
  157. for (gtt_start = gtt_map, gtt_end = gtt_map + size,
  158. vram_start = vram_map, vram_end = vram_map + size;
  159. gtt_start < gtt_end;
  160. gtt_start++, vram_start++) {
  161. if (*gtt_start != vram_start) {
  162. DRM_ERROR("Incorrect VRAM->GTT copy %d: Got 0x%p, "
  163. "expected 0x%p (VRAM/GTT offset "
  164. "0x%16llx/0x%16llx)\n",
  165. i, *gtt_start, vram_start,
  166. (unsigned long long)
  167. (vram_addr - adev->mc.vram_start +
  168. (void*)vram_start - vram_map),
  169. (unsigned long long)
  170. (gtt_addr - adev->mc.gtt_start +
  171. (void*)vram_start - vram_map));
  172. amdgpu_bo_kunmap(gtt_obj[i]);
  173. goto out_lclean_unpin;
  174. }
  175. }
  176. amdgpu_bo_kunmap(gtt_obj[i]);
  177. DRM_INFO("Tested GTT->VRAM and VRAM->GTT copy for GTT offset 0x%llx\n",
  178. gtt_addr - adev->mc.gtt_start);
  179. continue;
  180. out_lclean_unpin:
  181. amdgpu_bo_unpin(gtt_obj[i]);
  182. out_lclean_unres:
  183. amdgpu_bo_unreserve(gtt_obj[i]);
  184. out_lclean_unref:
  185. amdgpu_bo_unref(&gtt_obj[i]);
  186. out_lclean:
  187. for (--i; i >= 0; --i) {
  188. amdgpu_bo_unpin(gtt_obj[i]);
  189. amdgpu_bo_unreserve(gtt_obj[i]);
  190. amdgpu_bo_unref(&gtt_obj[i]);
  191. }
  192. if (fence)
  193. fence_put(fence);
  194. break;
  195. }
  196. amdgpu_bo_unpin(vram_obj);
  197. out_unres:
  198. amdgpu_bo_unreserve(vram_obj);
  199. out_unref:
  200. amdgpu_bo_unref(&vram_obj);
  201. out_cleanup:
  202. kfree(gtt_obj);
  203. if (r) {
  204. printk(KERN_WARNING "Error while testing BO move.\n");
  205. }
  206. }
  207. void amdgpu_test_moves(struct amdgpu_device *adev)
  208. {
  209. if (adev->mman.buffer_funcs)
  210. amdgpu_do_test_moves(adev);
  211. }
  212. static int amdgpu_test_create_and_emit_fence(struct amdgpu_device *adev,
  213. struct amdgpu_ring *ring,
  214. struct fence **fence)
  215. {
  216. uint32_t handle = ring->idx ^ 0xdeafbeef;
  217. int r;
  218. if (ring == &adev->uvd.ring) {
  219. r = amdgpu_uvd_get_create_msg(ring, handle, NULL);
  220. if (r) {
  221. DRM_ERROR("Failed to get dummy create msg\n");
  222. return r;
  223. }
  224. r = amdgpu_uvd_get_destroy_msg(ring, handle, fence);
  225. if (r) {
  226. DRM_ERROR("Failed to get dummy destroy msg\n");
  227. return r;
  228. }
  229. } else if (ring == &adev->vce.ring[0] ||
  230. ring == &adev->vce.ring[1]) {
  231. r = amdgpu_vce_get_create_msg(ring, handle, NULL);
  232. if (r) {
  233. DRM_ERROR("Failed to get dummy create msg\n");
  234. return r;
  235. }
  236. r = amdgpu_vce_get_destroy_msg(ring, handle, fence);
  237. if (r) {
  238. DRM_ERROR("Failed to get dummy destroy msg\n");
  239. return r;
  240. }
  241. } else {
  242. struct amdgpu_fence *a_fence = NULL;
  243. r = amdgpu_ring_lock(ring, 64);
  244. if (r) {
  245. DRM_ERROR("Failed to lock ring A %d\n", ring->idx);
  246. return r;
  247. }
  248. amdgpu_fence_emit(ring, AMDGPU_FENCE_OWNER_UNDEFINED, &a_fence);
  249. amdgpu_ring_unlock_commit(ring);
  250. *fence = &a_fence->base;
  251. }
  252. return 0;
  253. }
  254. void amdgpu_test_ring_sync(struct amdgpu_device *adev,
  255. struct amdgpu_ring *ringA,
  256. struct amdgpu_ring *ringB)
  257. {
  258. struct fence *fence1 = NULL, *fence2 = NULL;
  259. struct amdgpu_semaphore *semaphore = NULL;
  260. int r;
  261. r = amdgpu_semaphore_create(adev, &semaphore);
  262. if (r) {
  263. DRM_ERROR("Failed to create semaphore\n");
  264. goto out_cleanup;
  265. }
  266. r = amdgpu_ring_lock(ringA, 64);
  267. if (r) {
  268. DRM_ERROR("Failed to lock ring A %d\n", ringA->idx);
  269. goto out_cleanup;
  270. }
  271. amdgpu_semaphore_emit_wait(ringA, semaphore);
  272. amdgpu_ring_unlock_commit(ringA);
  273. r = amdgpu_test_create_and_emit_fence(adev, ringA, &fence1);
  274. if (r)
  275. goto out_cleanup;
  276. r = amdgpu_ring_lock(ringA, 64);
  277. if (r) {
  278. DRM_ERROR("Failed to lock ring A %d\n", ringA->idx);
  279. goto out_cleanup;
  280. }
  281. amdgpu_semaphore_emit_wait(ringA, semaphore);
  282. amdgpu_ring_unlock_commit(ringA);
  283. r = amdgpu_test_create_and_emit_fence(adev, ringA, &fence2);
  284. if (r)
  285. goto out_cleanup;
  286. mdelay(1000);
  287. if (fence_is_signaled(fence1)) {
  288. DRM_ERROR("Fence 1 signaled without waiting for semaphore.\n");
  289. goto out_cleanup;
  290. }
  291. r = amdgpu_ring_lock(ringB, 64);
  292. if (r) {
  293. DRM_ERROR("Failed to lock ring B %p\n", ringB);
  294. goto out_cleanup;
  295. }
  296. amdgpu_semaphore_emit_signal(ringB, semaphore);
  297. amdgpu_ring_unlock_commit(ringB);
  298. r = fence_wait(fence1, false);
  299. if (r) {
  300. DRM_ERROR("Failed to wait for sync fence 1\n");
  301. goto out_cleanup;
  302. }
  303. mdelay(1000);
  304. if (fence_is_signaled(fence2)) {
  305. DRM_ERROR("Fence 2 signaled without waiting for semaphore.\n");
  306. goto out_cleanup;
  307. }
  308. r = amdgpu_ring_lock(ringB, 64);
  309. if (r) {
  310. DRM_ERROR("Failed to lock ring B %p\n", ringB);
  311. goto out_cleanup;
  312. }
  313. amdgpu_semaphore_emit_signal(ringB, semaphore);
  314. amdgpu_ring_unlock_commit(ringB);
  315. r = fence_wait(fence2, false);
  316. if (r) {
  317. DRM_ERROR("Failed to wait for sync fence 1\n");
  318. goto out_cleanup;
  319. }
  320. out_cleanup:
  321. amdgpu_semaphore_free(adev, &semaphore, NULL);
  322. if (fence1)
  323. fence_put(fence1);
  324. if (fence2)
  325. fence_put(fence2);
  326. if (r)
  327. printk(KERN_WARNING "Error while testing ring sync (%d).\n", r);
  328. }
  329. static void amdgpu_test_ring_sync2(struct amdgpu_device *adev,
  330. struct amdgpu_ring *ringA,
  331. struct amdgpu_ring *ringB,
  332. struct amdgpu_ring *ringC)
  333. {
  334. struct fence *fenceA = NULL, *fenceB = NULL;
  335. struct amdgpu_semaphore *semaphore = NULL;
  336. bool sigA, sigB;
  337. int i, r;
  338. r = amdgpu_semaphore_create(adev, &semaphore);
  339. if (r) {
  340. DRM_ERROR("Failed to create semaphore\n");
  341. goto out_cleanup;
  342. }
  343. r = amdgpu_ring_lock(ringA, 64);
  344. if (r) {
  345. DRM_ERROR("Failed to lock ring A %d\n", ringA->idx);
  346. goto out_cleanup;
  347. }
  348. amdgpu_semaphore_emit_wait(ringA, semaphore);
  349. amdgpu_ring_unlock_commit(ringA);
  350. r = amdgpu_test_create_and_emit_fence(adev, ringA, &fenceA);
  351. if (r)
  352. goto out_cleanup;
  353. r = amdgpu_ring_lock(ringB, 64);
  354. if (r) {
  355. DRM_ERROR("Failed to lock ring B %d\n", ringB->idx);
  356. goto out_cleanup;
  357. }
  358. amdgpu_semaphore_emit_wait(ringB, semaphore);
  359. amdgpu_ring_unlock_commit(ringB);
  360. r = amdgpu_test_create_and_emit_fence(adev, ringB, &fenceB);
  361. if (r)
  362. goto out_cleanup;
  363. mdelay(1000);
  364. if (fence_is_signaled(fenceA)) {
  365. DRM_ERROR("Fence A signaled without waiting for semaphore.\n");
  366. goto out_cleanup;
  367. }
  368. if (fence_is_signaled(fenceB)) {
  369. DRM_ERROR("Fence B signaled without waiting for semaphore.\n");
  370. goto out_cleanup;
  371. }
  372. r = amdgpu_ring_lock(ringC, 64);
  373. if (r) {
  374. DRM_ERROR("Failed to lock ring B %p\n", ringC);
  375. goto out_cleanup;
  376. }
  377. amdgpu_semaphore_emit_signal(ringC, semaphore);
  378. amdgpu_ring_unlock_commit(ringC);
  379. for (i = 0; i < 30; ++i) {
  380. mdelay(100);
  381. sigA = fence_is_signaled(fenceA);
  382. sigB = fence_is_signaled(fenceB);
  383. if (sigA || sigB)
  384. break;
  385. }
  386. if (!sigA && !sigB) {
  387. DRM_ERROR("Neither fence A nor B has been signaled\n");
  388. goto out_cleanup;
  389. } else if (sigA && sigB) {
  390. DRM_ERROR("Both fence A and B has been signaled\n");
  391. goto out_cleanup;
  392. }
  393. DRM_INFO("Fence %c was first signaled\n", sigA ? 'A' : 'B');
  394. r = amdgpu_ring_lock(ringC, 64);
  395. if (r) {
  396. DRM_ERROR("Failed to lock ring B %p\n", ringC);
  397. goto out_cleanup;
  398. }
  399. amdgpu_semaphore_emit_signal(ringC, semaphore);
  400. amdgpu_ring_unlock_commit(ringC);
  401. mdelay(1000);
  402. r = fence_wait(fenceA, false);
  403. if (r) {
  404. DRM_ERROR("Failed to wait for sync fence A\n");
  405. goto out_cleanup;
  406. }
  407. r = fence_wait(fenceB, false);
  408. if (r) {
  409. DRM_ERROR("Failed to wait for sync fence B\n");
  410. goto out_cleanup;
  411. }
  412. out_cleanup:
  413. amdgpu_semaphore_free(adev, &semaphore, NULL);
  414. if (fenceA)
  415. fence_put(fenceA);
  416. if (fenceB)
  417. fence_put(fenceB);
  418. if (r)
  419. printk(KERN_WARNING "Error while testing ring sync (%d).\n", r);
  420. }
  421. static bool amdgpu_test_sync_possible(struct amdgpu_ring *ringA,
  422. struct amdgpu_ring *ringB)
  423. {
  424. if (ringA == &ringA->adev->vce.ring[0] &&
  425. ringB == &ringB->adev->vce.ring[1])
  426. return false;
  427. return true;
  428. }
  429. void amdgpu_test_syncing(struct amdgpu_device *adev)
  430. {
  431. int i, j, k;
  432. for (i = 1; i < AMDGPU_MAX_RINGS; ++i) {
  433. struct amdgpu_ring *ringA = adev->rings[i];
  434. if (!ringA || !ringA->ready)
  435. continue;
  436. for (j = 0; j < i; ++j) {
  437. struct amdgpu_ring *ringB = adev->rings[j];
  438. if (!ringB || !ringB->ready)
  439. continue;
  440. if (!amdgpu_test_sync_possible(ringA, ringB))
  441. continue;
  442. DRM_INFO("Testing syncing between rings %d and %d...\n", i, j);
  443. amdgpu_test_ring_sync(adev, ringA, ringB);
  444. DRM_INFO("Testing syncing between rings %d and %d...\n", j, i);
  445. amdgpu_test_ring_sync(adev, ringB, ringA);
  446. for (k = 0; k < j; ++k) {
  447. struct amdgpu_ring *ringC = adev->rings[k];
  448. if (!ringC || !ringC->ready)
  449. continue;
  450. if (!amdgpu_test_sync_possible(ringA, ringC))
  451. continue;
  452. if (!amdgpu_test_sync_possible(ringB, ringC))
  453. continue;
  454. DRM_INFO("Testing syncing between rings %d, %d and %d...\n", i, j, k);
  455. amdgpu_test_ring_sync2(adev, ringA, ringB, ringC);
  456. DRM_INFO("Testing syncing between rings %d, %d and %d...\n", i, k, j);
  457. amdgpu_test_ring_sync2(adev, ringA, ringC, ringB);
  458. DRM_INFO("Testing syncing between rings %d, %d and %d...\n", j, i, k);
  459. amdgpu_test_ring_sync2(adev, ringB, ringA, ringC);
  460. DRM_INFO("Testing syncing between rings %d, %d and %d...\n", j, k, i);
  461. amdgpu_test_ring_sync2(adev, ringB, ringC, ringA);
  462. DRM_INFO("Testing syncing between rings %d, %d and %d...\n", k, i, j);
  463. amdgpu_test_ring_sync2(adev, ringC, ringA, ringB);
  464. DRM_INFO("Testing syncing between rings %d, %d and %d...\n", k, j, i);
  465. amdgpu_test_ring_sync2(adev, ringC, ringB, ringA);
  466. }
  467. }
  468. }
  469. }