1 /*
2 * Copyright © 2011 Intel Corporation
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 (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
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 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Chris Wilson <[email protected]>
25 *
26 */
27
28 #include "igt.h"
29 #include <unistd.h>
30 #include <stdlib.h>
31 #include <stdint.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <fcntl.h>
35 #include <inttypes.h>
36 #include <errno.h>
37 #include <sys/stat.h>
38 #include <sys/ioctl.h>
39 #include <sys/time.h>
40 #include "drm.h"
41 #include "intel_bufmgr.h"
42
43 #define OBJECT_SIZE 16384
44
45 #define COPY_BLT_CMD (2<<29|0x53<<22)
46 #define BLT_WRITE_ALPHA (1<<21)
47 #define BLT_WRITE_RGB (1<<20)
48 #define BLT_SRC_TILED (1<<15)
49 #define BLT_DST_TILED (1<<11)
50
51 uint32_t is_64bit;
52 uint32_t exec_flags;
53
build_batch(uint32_t * batch,int len,uint32_t * batch_len)54 static inline void build_batch(uint32_t *batch, int len, uint32_t *batch_len)
55 {
56 unsigned int i = 0;
57
58 batch[i++] = COPY_BLT_CMD | BLT_WRITE_ALPHA | BLT_WRITE_RGB | (is_64bit ? 8 : 6);
59 batch[i++] = 0xcc << 16 | 1 << 25 | 1 << 24 | len;
60 batch[i++] = 0;
61 batch[i++] = 1 << 16 | (len / 4);
62 batch[i++] = 0; /* dst */
63 if (is_64bit)
64 batch[i++] = 0;
65 batch[i++] = 0;
66 batch[i++] = len;
67 batch[i++] = 0; /* src */
68 if (is_64bit)
69 batch[i++] = 0;
70 batch[i++] = MI_BATCH_BUFFER_END;
71 batch[i++] = 0;
72
73 *batch_len = i * 4;
74 }
75
76 #define BUILD_EXEC \
77 uint32_t batch[12]; \
78 struct drm_i915_gem_relocation_entry reloc[] = { \
79 { dst, 0, 4*sizeof(uint32_t), 0, I915_GEM_DOMAIN_RENDER, I915_GEM_DOMAIN_RENDER }, \
80 { src, 0, (is_64bit ? 8 : 7)*sizeof(uint32_t), 0, I915_GEM_DOMAIN_RENDER, 0 }, \
81 }; \
82 struct drm_i915_gem_exec_object2 exec[] = { \
83 { src }, \
84 { dst }, \
85 { gem_create(fd, 4096), 2, to_user_pointer(reloc) } \
86 }; \
87 struct drm_i915_gem_execbuffer2 execbuf = { \
88 to_user_pointer(exec), 3, \
89 0, 0, \
90 0, 0, 0, 0, \
91 exec_flags, \
92 }; \
93 build_batch(batch, len, &execbuf.batch_len); \
94 gem_write(fd, exec[2].handle, 0, batch, execbuf.batch_len);
95
96
copy(int fd,uint32_t src,uint32_t dst,void * buf,int len,int loops)97 static void copy(int fd, uint32_t src, uint32_t dst, void *buf, int len, int loops)
98 {
99 BUILD_EXEC;
100
101 while (loops--) {
102 gem_write(fd, src, 0, buf, len);
103 gem_execbuf(fd, &execbuf);
104 gem_read(fd, dst, 0, buf, len);
105 }
106
107 gem_close(fd, exec[2].handle);
108 }
109
as_gtt_mmap(int fd,uint32_t src,uint32_t dst,void * buf,int len,int loops)110 static void as_gtt_mmap(int fd, uint32_t src, uint32_t dst, void *buf, int len, int loops)
111 {
112 uint32_t *src_ptr, *dst_ptr;
113 BUILD_EXEC;
114
115 src_ptr = gem_mmap__gtt(fd, src, OBJECT_SIZE, PROT_WRITE);
116 dst_ptr = gem_mmap__gtt(fd, dst, OBJECT_SIZE, PROT_READ);
117
118 while (loops--) {
119 gem_set_domain(fd, src,
120 I915_GEM_DOMAIN_GTT, I915_GEM_DOMAIN_GTT);
121 memcpy(src_ptr, buf, len);
122
123 gem_execbuf(fd, &execbuf);
124 gem_set_domain(fd, dst,
125 I915_GEM_DOMAIN_GTT, 0);
126 memcpy(buf, dst_ptr, len);
127 }
128
129 munmap(dst_ptr, len);
130 munmap(src_ptr, len);
131 gem_close(fd, exec[2].handle);
132 }
133
134
as_cpu_mmap(int fd,uint32_t src,uint32_t dst,void * buf,int len,int loops)135 static void as_cpu_mmap(int fd, uint32_t src, uint32_t dst, void *buf, int len, int loops)
136 {
137 uint32_t *src_ptr, *dst_ptr;
138 BUILD_EXEC;
139
140 src_ptr = gem_mmap__cpu(fd, src, 0, OBJECT_SIZE, PROT_WRITE);
141 dst_ptr = gem_mmap__cpu(fd, dst, 0, OBJECT_SIZE, PROT_READ);
142
143 while (loops--) {
144 gem_set_domain(fd, src,
145 I915_GEM_DOMAIN_CPU, I915_GEM_DOMAIN_CPU);
146 memcpy(src_ptr, buf, len);
147
148 gem_execbuf(fd, &execbuf);
149 gem_set_domain(fd, dst,
150 I915_GEM_DOMAIN_CPU, 0);
151 memcpy(buf, dst_ptr, len);
152 }
153
154 munmap(dst_ptr, len);
155 munmap(src_ptr, len);
156 gem_close(fd, exec[2].handle);
157 }
158
test_copy(int fd,uint32_t src,uint32_t dst,uint32_t * buf,int len)159 static void test_copy(int fd, uint32_t src, uint32_t dst, uint32_t *buf, int len)
160 {
161 int i;
162 BUILD_EXEC;
163
164 for (i = 0; i < len/4; i++)
165 buf[i] = i;
166
167 gem_write(fd, src, 0, buf, len);
168 memset(buf, 0, len);
169
170 gem_execbuf(fd, &execbuf);
171 gem_read(fd, dst, 0, buf, len);
172
173 gem_close(fd, exec[2].handle);
174
175 for (i = 0; i < len/4; i++)
176 igt_assert(buf[i] == i);
177 }
178
test_as_gtt_mmap(int fd,uint32_t src,uint32_t dst,int len)179 static void test_as_gtt_mmap(int fd, uint32_t src, uint32_t dst, int len)
180 {
181 uint32_t *src_ptr, *dst_ptr;
182 int i;
183 BUILD_EXEC;
184
185 src_ptr = gem_mmap__gtt(fd, src, OBJECT_SIZE, PROT_WRITE);
186 dst_ptr = gem_mmap__gtt(fd, dst, OBJECT_SIZE, PROT_READ);
187
188 gem_set_domain(fd, src, I915_GEM_DOMAIN_GTT, I915_GEM_DOMAIN_GTT);
189 for (i = 0; i < len/4; i++)
190 src_ptr[i] = i;
191
192 gem_execbuf(fd, &execbuf);
193 gem_close(fd, exec[2].handle);
194
195 gem_set_domain(fd, dst, I915_GEM_DOMAIN_GTT, 0);
196 for (i = 0; i < len/4; i++)
197 igt_assert(dst_ptr[i] == i);
198
199 munmap(dst_ptr, len);
200 munmap(src_ptr, len);
201 }
202
test_as_cpu_mmap(int fd,uint32_t src,uint32_t dst,int len)203 static void test_as_cpu_mmap(int fd, uint32_t src, uint32_t dst, int len)
204 {
205 uint32_t *src_ptr, *dst_ptr;
206 int i;
207 BUILD_EXEC;
208
209 src_ptr = gem_mmap__cpu(fd, src, 0, OBJECT_SIZE, PROT_WRITE);
210 dst_ptr = gem_mmap__cpu(fd, dst, 0, OBJECT_SIZE, PROT_READ);
211
212 gem_set_domain(fd, src, I915_GEM_DOMAIN_CPU, I915_GEM_DOMAIN_CPU);
213 for (i = 0; i < len/4; i++)
214 src_ptr[i] = i;
215
216 gem_execbuf(fd, &execbuf);
217 gem_close(fd, exec[2].handle);
218
219 gem_set_domain(fd, dst, I915_GEM_DOMAIN_CPU, 0);
220 for (i = 0; i < len/4; i++)
221 igt_assert(dst_ptr[i] == i);
222
223 munmap(dst_ptr, len);
224 munmap(src_ptr, len);
225 }
226
elapsed(const struct timeval * start,const struct timeval * end,int loop)227 static double elapsed(const struct timeval *start,
228 const struct timeval *end,
229 int loop)
230 {
231 return (1e6*(end->tv_sec - start->tv_sec) + (end->tv_usec - start->tv_usec))/loop;
232 }
233
bytes_per_sec(char * buf,double v)234 static const char *bytes_per_sec(char *buf, double v)
235 {
236 const char *order[] = {
237 "",
238 "KiB",
239 "MiB",
240 "GiB",
241 "TiB",
242 NULL,
243 }, **o = order;
244
245 while (v > 1000 && o[1]) {
246 v /= 1000;
247 o++;
248 }
249 sprintf(buf, "%.1f%s/s", v, *o);
250 return buf;
251 }
252
253 uint32_t *tmp, src, dst;
254 int fd;
255 int object_size = 0;
256
opt_handler(int opt,int opt_index,void * data)257 static int opt_handler(int opt, int opt_index, void *data)
258 {
259 switch (opt) {
260 case 's':
261 object_size = atoi(optarg);
262 break;
263 default:
264 return IGT_OPT_HANDLER_ERROR;
265 }
266
267 return IGT_OPT_HANDLER_SUCCESS;
268 }
269
270 const char *help_str = " -s\tObject size in bytes\n";
271
272 igt_main_args("s:", NULL, help_str, opt_handler, NULL)
273 {
274 uint32_t buf[20];
275 int count;
276
277 igt_skip_on_simulation();
278
279 if (object_size == 0)
280 object_size = OBJECT_SIZE;
281 object_size = (object_size + 3) & -4;
282
283 igt_fixture {
284 uint32_t devid;
285
286 fd = drm_open_driver(DRIVER_INTEL);
287 igt_require_gem(fd);
288
289 dst = gem_create(fd, object_size);
290 src = gem_create(fd, object_size);
291 tmp = malloc(object_size);
292
293 gem_set_caching(fd, src, 0);
294 gem_set_caching(fd, dst, 0);
295
296 devid = intel_get_drm_devid(fd);
297 is_64bit = intel_gen(devid) >= 8;
298 exec_flags = HAS_BLT_RING(devid) ? I915_EXEC_BLT : 0;
299 }
300
301 igt_subtest("uncached-copy-correctness")
302 test_copy(fd, src, dst, tmp, object_size);
303 igt_subtest("uncached-copy-performance") {
304 for (count = 1; count <= 1<<17; count <<= 1) {
305 struct timeval start, end;
306
307 gettimeofday(&start, NULL);
308 copy(fd, src, dst, tmp, object_size, count);
309 gettimeofday(&end, NULL);
310 igt_info("Time to uncached copy %d bytes x %6d: %7.3fµs, %s\n",
311 object_size, count,
312 elapsed(&start, &end, count),
313 bytes_per_sec((char *)buf, object_size/elapsed(&start, &end, count)*1e6));
314 fflush(stdout);
315 }
316 }
317
318 igt_subtest("uncached-pwrite-blt-gtt_mmap-correctness")
319 test_as_gtt_mmap(fd, src, dst, object_size);
320 igt_subtest("uncached-pwrite-blt-gtt_mmap-performance") {
321 for (count = 1; count <= 1<<17; count <<= 1) {
322 struct timeval start, end;
323
324 gettimeofday(&start, NULL);
325 as_gtt_mmap(fd, src, dst, tmp, object_size, count);
326 gettimeofday(&end, NULL);
327 igt_info("** mmap uncached copy %d bytes x %6d: %7.3fµs, %s\n",
328 object_size, count,
329 elapsed(&start, &end, count),
330 bytes_per_sec((char *)buf, object_size/elapsed(&start, &end, count)*1e6));
331 fflush(stdout);
332 }
333 }
334
335 igt_fixture {
336 gem_set_caching(fd, src, 1);
337 gem_set_caching(fd, dst, 1);
338 }
339
340 igt_subtest("snooped-copy-correctness")
341 test_copy(fd, src, dst, tmp, object_size);
342 igt_subtest("snooped-copy-performance") {
343 for (count = 1; count <= 1<<17; count <<= 1) {
344 struct timeval start, end;
345
346 gettimeofday(&start, NULL);
347 copy(fd, src, dst, tmp, object_size, count);
348 gettimeofday(&end, NULL);
349 igt_info("Time to snooped copy %d bytes x %6d: %7.3fµs, %s\n",
350 object_size, count,
351 elapsed(&start, &end, count),
352 bytes_per_sec((char *)buf, object_size/elapsed(&start, &end, count)*1e6));
353 fflush(stdout);
354 }
355 }
356
357 igt_subtest("snooped-pwrite-blt-cpu_mmap-correctness")
358 test_as_cpu_mmap(fd, src, dst, object_size);
359 igt_subtest("snooped-pwrite-blt-cpu_mmap-performance") {
360 for (count = 1; count <= 1<<17; count <<= 1) {
361 struct timeval start, end;
362
363 gettimeofday(&start, NULL);
364 as_cpu_mmap(fd, src, dst, tmp, object_size, count);
365 gettimeofday(&end, NULL);
366 igt_info("** mmap snooped copy %d bytes x %6d: %7.3fµs, %s\n",
367 object_size, count,
368 elapsed(&start, &end, count),
369 bytes_per_sec((char *)buf, object_size/elapsed(&start, &end, count)*1e6));
370 fflush(stdout);
371 }
372 }
373
374 igt_fixture {
375 gem_set_caching(fd, src, 2);
376 gem_set_caching(fd, dst, 2);
377 }
378
379 igt_subtest("display-copy-correctness")
380 test_copy(fd, src, dst, tmp, object_size);
381 igt_subtest("display-copy-performance") {
382 for (count = 1; count <= 1<<17; count <<= 1) {
383 struct timeval start, end;
384
385 gettimeofday(&start, NULL);
386 copy(fd, src, dst, tmp, object_size, count);
387 gettimeofday(&end, NULL);
388 igt_info("Time to display copy %d bytes x %6d: %7.3fµs, %s\n",
389 object_size, count,
390 elapsed(&start, &end, count),
391 bytes_per_sec((char *)buf, object_size/elapsed(&start, &end, count)*1e6));
392 fflush(stdout);
393 }
394 }
395
396 igt_subtest("display-pwrite-blt-gtt_mmap-correctness")
397 test_as_gtt_mmap(fd, src, dst, object_size);
398 igt_subtest("display-pwrite-blt-gtt_mmap-performance") {
399 for (count = 1; count <= 1<<17; count <<= 1) {
400 struct timeval start, end;
401
402 gettimeofday(&start, NULL);
403 as_gtt_mmap(fd, src, dst, tmp, object_size, count);
404 gettimeofday(&end, NULL);
405 igt_info("** mmap display copy %d bytes x %6d: %7.3fµs, %s\n",
406 object_size, count,
407 elapsed(&start, &end, count),
408 bytes_per_sec((char *)buf, object_size/elapsed(&start, &end, count)*1e6));
409 fflush(stdout);
410 }
411 }
412
413 igt_fixture {
414 free(tmp);
415 gem_close(fd, src);
416 gem_close(fd, dst);
417
418 close(fd);
419 }
420 }
421