amdgpu_benchmark.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * Copyright 2009 Jerome Glisse.
  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: Jerome Glisse
  23. */
  24. #include <drm/drmP.h>
  25. #include <drm/amdgpu_drm.h>
  26. #include "amdgpu.h"
  27. #define AMDGPU_BENCHMARK_ITERATIONS 1024
  28. #define AMDGPU_BENCHMARK_COMMON_MODES_N 17
  29. static int amdgpu_benchmark_do_move(struct amdgpu_device *adev, unsigned size,
  30. uint64_t saddr, uint64_t daddr, int n)
  31. {
  32. unsigned long start_jiffies;
  33. unsigned long end_jiffies;
  34. struct dma_fence *fence = NULL;
  35. int i, r;
  36. start_jiffies = jiffies;
  37. for (i = 0; i < n; i++) {
  38. struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
  39. r = amdgpu_copy_buffer(ring, saddr, daddr, size, NULL, &fence,
  40. false, false);
  41. if (r)
  42. goto exit_do_move;
  43. r = dma_fence_wait(fence, false);
  44. if (r)
  45. goto exit_do_move;
  46. dma_fence_put(fence);
  47. }
  48. end_jiffies = jiffies;
  49. r = jiffies_to_msecs(end_jiffies - start_jiffies);
  50. exit_do_move:
  51. if (fence)
  52. dma_fence_put(fence);
  53. return r;
  54. }
  55. static void amdgpu_benchmark_log_results(int n, unsigned size,
  56. unsigned int time,
  57. unsigned sdomain, unsigned ddomain,
  58. char *kind)
  59. {
  60. unsigned int throughput = (n * (size >> 10)) / time;
  61. DRM_INFO("amdgpu: %s %u bo moves of %u kB from"
  62. " %d to %d in %u ms, throughput: %u Mb/s or %u MB/s\n",
  63. kind, n, size >> 10, sdomain, ddomain, time,
  64. throughput * 8, throughput);
  65. }
  66. static void amdgpu_benchmark_move(struct amdgpu_device *adev, unsigned size,
  67. unsigned sdomain, unsigned ddomain)
  68. {
  69. struct amdgpu_bo *dobj = NULL;
  70. struct amdgpu_bo *sobj = NULL;
  71. struct amdgpu_bo_param bp;
  72. uint64_t saddr, daddr;
  73. int r, n;
  74. int time;
  75. memset(&bp, 0, sizeof(bp));
  76. bp.size = size;
  77. bp.byte_align = PAGE_SIZE;
  78. bp.domain = sdomain;
  79. bp.flags = 0;
  80. bp.type = ttm_bo_type_kernel;
  81. bp.resv = NULL;
  82. n = AMDGPU_BENCHMARK_ITERATIONS;
  83. r = amdgpu_bo_create(adev, &bp, &sobj);
  84. if (r) {
  85. goto out_cleanup;
  86. }
  87. r = amdgpu_bo_reserve(sobj, false);
  88. if (unlikely(r != 0))
  89. goto out_cleanup;
  90. r = amdgpu_bo_pin(sobj, sdomain);
  91. if (r) {
  92. amdgpu_bo_unreserve(sobj);
  93. goto out_cleanup;
  94. }
  95. r = amdgpu_ttm_alloc_gart(&sobj->tbo);
  96. amdgpu_bo_unreserve(sobj);
  97. if (r) {
  98. goto out_cleanup;
  99. }
  100. saddr = amdgpu_bo_gpu_offset(sobj);
  101. bp.domain = ddomain;
  102. r = amdgpu_bo_create(adev, &bp, &dobj);
  103. if (r) {
  104. goto out_cleanup;
  105. }
  106. r = amdgpu_bo_reserve(dobj, false);
  107. if (unlikely(r != 0))
  108. goto out_cleanup;
  109. r = amdgpu_bo_pin(dobj, ddomain);
  110. if (r) {
  111. amdgpu_bo_unreserve(sobj);
  112. goto out_cleanup;
  113. }
  114. r = amdgpu_ttm_alloc_gart(&dobj->tbo);
  115. amdgpu_bo_unreserve(dobj);
  116. if (r) {
  117. goto out_cleanup;
  118. }
  119. daddr = amdgpu_bo_gpu_offset(dobj);
  120. if (adev->mman.buffer_funcs) {
  121. time = amdgpu_benchmark_do_move(adev, size, saddr, daddr, n);
  122. if (time < 0)
  123. goto out_cleanup;
  124. if (time > 0)
  125. amdgpu_benchmark_log_results(n, size, time,
  126. sdomain, ddomain, "dma");
  127. }
  128. out_cleanup:
  129. /* Check error value now. The value can be overwritten when clean up.*/
  130. if (r) {
  131. DRM_ERROR("Error while benchmarking BO move.\n");
  132. }
  133. if (sobj) {
  134. r = amdgpu_bo_reserve(sobj, true);
  135. if (likely(r == 0)) {
  136. amdgpu_bo_unpin(sobj);
  137. amdgpu_bo_unreserve(sobj);
  138. }
  139. amdgpu_bo_unref(&sobj);
  140. }
  141. if (dobj) {
  142. r = amdgpu_bo_reserve(dobj, true);
  143. if (likely(r == 0)) {
  144. amdgpu_bo_unpin(dobj);
  145. amdgpu_bo_unreserve(dobj);
  146. }
  147. amdgpu_bo_unref(&dobj);
  148. }
  149. }
  150. void amdgpu_benchmark(struct amdgpu_device *adev, int test_number)
  151. {
  152. int i;
  153. static const int common_modes[AMDGPU_BENCHMARK_COMMON_MODES_N] = {
  154. 640 * 480 * 4,
  155. 720 * 480 * 4,
  156. 800 * 600 * 4,
  157. 848 * 480 * 4,
  158. 1024 * 768 * 4,
  159. 1152 * 768 * 4,
  160. 1280 * 720 * 4,
  161. 1280 * 800 * 4,
  162. 1280 * 854 * 4,
  163. 1280 * 960 * 4,
  164. 1280 * 1024 * 4,
  165. 1440 * 900 * 4,
  166. 1400 * 1050 * 4,
  167. 1680 * 1050 * 4,
  168. 1600 * 1200 * 4,
  169. 1920 * 1080 * 4,
  170. 1920 * 1200 * 4
  171. };
  172. switch (test_number) {
  173. case 1:
  174. /* simple test, VRAM to GTT and GTT to VRAM */
  175. amdgpu_benchmark_move(adev, 1024*1024, AMDGPU_GEM_DOMAIN_GTT,
  176. AMDGPU_GEM_DOMAIN_VRAM);
  177. amdgpu_benchmark_move(adev, 1024*1024, AMDGPU_GEM_DOMAIN_VRAM,
  178. AMDGPU_GEM_DOMAIN_GTT);
  179. break;
  180. case 2:
  181. /* simple test, VRAM to VRAM */
  182. amdgpu_benchmark_move(adev, 1024*1024, AMDGPU_GEM_DOMAIN_VRAM,
  183. AMDGPU_GEM_DOMAIN_VRAM);
  184. break;
  185. case 3:
  186. /* GTT to VRAM, buffer size sweep, powers of 2 */
  187. for (i = 1; i <= 16384; i <<= 1)
  188. amdgpu_benchmark_move(adev, i * AMDGPU_GPU_PAGE_SIZE,
  189. AMDGPU_GEM_DOMAIN_GTT,
  190. AMDGPU_GEM_DOMAIN_VRAM);
  191. break;
  192. case 4:
  193. /* VRAM to GTT, buffer size sweep, powers of 2 */
  194. for (i = 1; i <= 16384; i <<= 1)
  195. amdgpu_benchmark_move(adev, i * AMDGPU_GPU_PAGE_SIZE,
  196. AMDGPU_GEM_DOMAIN_VRAM,
  197. AMDGPU_GEM_DOMAIN_GTT);
  198. break;
  199. case 5:
  200. /* VRAM to VRAM, buffer size sweep, powers of 2 */
  201. for (i = 1; i <= 16384; i <<= 1)
  202. amdgpu_benchmark_move(adev, i * AMDGPU_GPU_PAGE_SIZE,
  203. AMDGPU_GEM_DOMAIN_VRAM,
  204. AMDGPU_GEM_DOMAIN_VRAM);
  205. break;
  206. case 6:
  207. /* GTT to VRAM, buffer size sweep, common modes */
  208. for (i = 0; i < AMDGPU_BENCHMARK_COMMON_MODES_N; i++)
  209. amdgpu_benchmark_move(adev, common_modes[i],
  210. AMDGPU_GEM_DOMAIN_GTT,
  211. AMDGPU_GEM_DOMAIN_VRAM);
  212. break;
  213. case 7:
  214. /* VRAM to GTT, buffer size sweep, common modes */
  215. for (i = 0; i < AMDGPU_BENCHMARK_COMMON_MODES_N; i++)
  216. amdgpu_benchmark_move(adev, common_modes[i],
  217. AMDGPU_GEM_DOMAIN_VRAM,
  218. AMDGPU_GEM_DOMAIN_GTT);
  219. break;
  220. case 8:
  221. /* VRAM to VRAM, buffer size sweep, common modes */
  222. for (i = 0; i < AMDGPU_BENCHMARK_COMMON_MODES_N; i++)
  223. amdgpu_benchmark_move(adev, common_modes[i],
  224. AMDGPU_GEM_DOMAIN_VRAM,
  225. AMDGPU_GEM_DOMAIN_VRAM);
  226. break;
  227. default:
  228. DRM_ERROR("Unknown benchmark\n");
  229. }
  230. }