blob: cb88d7e8b96b1e25bfd8fc931563ac0957a2eb35 [file] [log] [blame]
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001/*
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
28#define AMDGPU_BENCHMARK_ITERATIONS 1024
29#define AMDGPU_BENCHMARK_COMMON_MODES_N 17
30
31static int amdgpu_benchmark_do_move(struct amdgpu_device *adev, unsigned size,
32 uint64_t saddr, uint64_t daddr, int n)
33{
34 unsigned long start_jiffies;
35 unsigned long end_jiffies;
Chris Wilsonf54d1862016-10-25 13:00:45 +010036 struct dma_fence *fence = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -040037 int i, r;
38
39 start_jiffies = jiffies;
40 for (i = 0; i < n; i++) {
41 struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
Chunming Zhoue24db982016-08-15 10:46:04 +080042 r = amdgpu_copy_buffer(ring, saddr, daddr, size, NULL, &fence,
Christian Königfc9c8f52017-06-29 11:46:15 +020043 false, false);
Alex Deucherd38ceaf2015-04-20 16:55:21 -040044 if (r)
45 goto exit_do_move;
Chris Wilsonf54d1862016-10-25 13:00:45 +010046 r = dma_fence_wait(fence, false);
Alex Deucherd38ceaf2015-04-20 16:55:21 -040047 if (r)
48 goto exit_do_move;
Chris Wilsonf54d1862016-10-25 13:00:45 +010049 dma_fence_put(fence);
Alex Deucherd38ceaf2015-04-20 16:55:21 -040050 }
51 end_jiffies = jiffies;
52 r = jiffies_to_msecs(end_jiffies - start_jiffies);
53
54exit_do_move:
55 if (fence)
Chris Wilsonf54d1862016-10-25 13:00:45 +010056 dma_fence_put(fence);
Alex Deucherd38ceaf2015-04-20 16:55:21 -040057 return r;
58}
59
60
61static void amdgpu_benchmark_log_results(int n, unsigned size,
62 unsigned int time,
63 unsigned sdomain, unsigned ddomain,
64 char *kind)
65{
66 unsigned int throughput = (n * (size >> 10)) / time;
67 DRM_INFO("amdgpu: %s %u bo moves of %u kB from"
68 " %d to %d in %u ms, throughput: %u Mb/s or %u MB/s\n",
69 kind, n, size >> 10, sdomain, ddomain, time,
70 throughput * 8, throughput);
71}
72
73static void amdgpu_benchmark_move(struct amdgpu_device *adev, unsigned size,
74 unsigned sdomain, unsigned ddomain)
75{
76 struct amdgpu_bo *dobj = NULL;
77 struct amdgpu_bo *sobj = NULL;
Chunming Zhou3216c6b2018-04-16 18:27:50 +080078 struct amdgpu_bo_param bp;
Alex Deucherd38ceaf2015-04-20 16:55:21 -040079 uint64_t saddr, daddr;
80 int r, n;
81 int time;
82
Chunming Zhou3216c6b2018-04-16 18:27:50 +080083 memset(&bp, 0, sizeof(bp));
84 bp.size = size;
85 bp.byte_align = PAGE_SIZE;
86 bp.domain = sdomain;
87 bp.flags = 0;
88 bp.type = ttm_bo_type_kernel;
89 bp.resv = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -040090 n = AMDGPU_BENCHMARK_ITERATIONS;
Chunming Zhou3216c6b2018-04-16 18:27:50 +080091 r = amdgpu_bo_create(adev, &bp, &sobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -040092 if (r) {
93 goto out_cleanup;
94 }
95 r = amdgpu_bo_reserve(sobj, false);
96 if (unlikely(r != 0))
97 goto out_cleanup;
Junwei Zhang7b7c6c82018-06-25 12:51:14 +080098 r = amdgpu_bo_pin(sobj, sdomain);
99 saddr = amdgpu_bo_gpu_offset(sobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400100 amdgpu_bo_unreserve(sobj);
101 if (r) {
102 goto out_cleanup;
103 }
Chunming Zhou3216c6b2018-04-16 18:27:50 +0800104 bp.domain = ddomain;
105 r = amdgpu_bo_create(adev, &bp, &dobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400106 if (r) {
107 goto out_cleanup;
108 }
109 r = amdgpu_bo_reserve(dobj, false);
110 if (unlikely(r != 0))
111 goto out_cleanup;
Junwei Zhang7b7c6c82018-06-25 12:51:14 +0800112 r = amdgpu_bo_pin(dobj, ddomain);
113 daddr = amdgpu_bo_gpu_offset(dobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400114 amdgpu_bo_unreserve(dobj);
115 if (r) {
116 goto out_cleanup;
117 }
118
119 if (adev->mman.buffer_funcs) {
120 time = amdgpu_benchmark_do_move(adev, size, saddr, daddr, n);
121 if (time < 0)
122 goto out_cleanup;
123 if (time > 0)
124 amdgpu_benchmark_log_results(n, size, time,
125 sdomain, ddomain, "dma");
126 }
127
128out_cleanup:
Alex Xied159f262017-04-24 15:26:57 -0400129 /* 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
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400134 if (sobj) {
Alex Xie4a9ed102017-04-24 15:33:16 -0400135 r = amdgpu_bo_reserve(sobj, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400136 if (likely(r == 0)) {
137 amdgpu_bo_unpin(sobj);
138 amdgpu_bo_unreserve(sobj);
139 }
140 amdgpu_bo_unref(&sobj);
141 }
142 if (dobj) {
Alex Xie4a9ed102017-04-24 15:33:16 -0400143 r = amdgpu_bo_reserve(dobj, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400144 if (likely(r == 0)) {
145 amdgpu_bo_unpin(dobj);
146 amdgpu_bo_unreserve(dobj);
147 }
148 amdgpu_bo_unref(&dobj);
149 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400150}
151
152void amdgpu_benchmark(struct amdgpu_device *adev, int test_number)
153{
154 int i;
Nils Wallméniusaeba7092016-04-10 16:30:04 +0200155 static const int common_modes[AMDGPU_BENCHMARK_COMMON_MODES_N] = {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400156 640 * 480 * 4,
157 720 * 480 * 4,
158 800 * 600 * 4,
159 848 * 480 * 4,
160 1024 * 768 * 4,
161 1152 * 768 * 4,
162 1280 * 720 * 4,
163 1280 * 800 * 4,
164 1280 * 854 * 4,
165 1280 * 960 * 4,
166 1280 * 1024 * 4,
167 1440 * 900 * 4,
168 1400 * 1050 * 4,
169 1680 * 1050 * 4,
170 1600 * 1200 * 4,
171 1920 * 1080 * 4,
172 1920 * 1200 * 4
173 };
174
175 switch (test_number) {
176 case 1:
177 /* simple test, VRAM to GTT and GTT to VRAM */
178 amdgpu_benchmark_move(adev, 1024*1024, AMDGPU_GEM_DOMAIN_GTT,
179 AMDGPU_GEM_DOMAIN_VRAM);
180 amdgpu_benchmark_move(adev, 1024*1024, AMDGPU_GEM_DOMAIN_VRAM,
181 AMDGPU_GEM_DOMAIN_GTT);
182 break;
183 case 2:
184 /* simple test, VRAM to VRAM */
185 amdgpu_benchmark_move(adev, 1024*1024, AMDGPU_GEM_DOMAIN_VRAM,
186 AMDGPU_GEM_DOMAIN_VRAM);
187 break;
188 case 3:
189 /* GTT to VRAM, buffer size sweep, powers of 2 */
190 for (i = 1; i <= 16384; i <<= 1)
191 amdgpu_benchmark_move(adev, i * AMDGPU_GPU_PAGE_SIZE,
192 AMDGPU_GEM_DOMAIN_GTT,
193 AMDGPU_GEM_DOMAIN_VRAM);
194 break;
195 case 4:
196 /* VRAM to GTT, buffer size sweep, powers of 2 */
197 for (i = 1; i <= 16384; i <<= 1)
198 amdgpu_benchmark_move(adev, i * AMDGPU_GPU_PAGE_SIZE,
199 AMDGPU_GEM_DOMAIN_VRAM,
200 AMDGPU_GEM_DOMAIN_GTT);
201 break;
202 case 5:
203 /* VRAM to VRAM, buffer size sweep, powers of 2 */
204 for (i = 1; i <= 16384; i <<= 1)
205 amdgpu_benchmark_move(adev, i * AMDGPU_GPU_PAGE_SIZE,
206 AMDGPU_GEM_DOMAIN_VRAM,
207 AMDGPU_GEM_DOMAIN_VRAM);
208 break;
209 case 6:
210 /* GTT to VRAM, buffer size sweep, common modes */
211 for (i = 0; i < AMDGPU_BENCHMARK_COMMON_MODES_N; i++)
212 amdgpu_benchmark_move(adev, common_modes[i],
213 AMDGPU_GEM_DOMAIN_GTT,
214 AMDGPU_GEM_DOMAIN_VRAM);
215 break;
216 case 7:
217 /* VRAM to GTT, buffer size sweep, common modes */
218 for (i = 0; i < AMDGPU_BENCHMARK_COMMON_MODES_N; i++)
219 amdgpu_benchmark_move(adev, common_modes[i],
220 AMDGPU_GEM_DOMAIN_VRAM,
221 AMDGPU_GEM_DOMAIN_GTT);
222 break;
223 case 8:
224 /* VRAM to VRAM, buffer size sweep, common modes */
225 for (i = 0; i < AMDGPU_BENCHMARK_COMMON_MODES_N; i++)
226 amdgpu_benchmark_move(adev, common_modes[i],
227 AMDGPU_GEM_DOMAIN_VRAM,
228 AMDGPU_GEM_DOMAIN_VRAM);
229 break;
230
231 default:
232 DRM_ERROR("Unknown benchmark\n");
233 }
234}