2 * Copyright © 2011 Intel Corporation
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:
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
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
24 * Chris Wilson <chris@chris-wilson.co.uk>
38 #include <sys/ioctl.h>
44 #include "intel_bufmgr.h"
45 #include "intel_batchbuffer.h"
46 #include "intel_gpu_tools.h"
48 #define OBJECT_SIZE 16384
50 #define COPY_BLT_CMD (2<<29|0x53<<22|0x6)
51 #define BLT_WRITE_ALPHA (1<<21)
52 #define BLT_WRITE_RGB (1<<20)
53 #define BLT_SRC_TILED (1<<15)
54 #define BLT_DST_TILED (1<<11)
56 static void copy(int fd, uint32_t src, uint32_t dst, void *buf, int len, int loops)
58 struct drm_i915_gem_relocation_entry reloc[] = {
59 { dst, 0, 4*sizeof(uint32_t), 0, I915_GEM_DOMAIN_RENDER, I915_GEM_DOMAIN_RENDER },
60 { src, 0, 7*sizeof(uint32_t), 0, I915_GEM_DOMAIN_RENDER, 0 },
62 struct drm_i915_gem_exec_object2 exec[] = {
65 { gem_create(fd, 4096), 2, (uintptr_t)reloc }
68 COPY_BLT_CMD | BLT_WRITE_ALPHA | BLT_WRITE_RGB,
69 0xcc << 16 | 1 << 25 | 1 << 24 | len,
79 struct drm_i915_gem_execbuffer2 execbuf = {
83 HAS_BLT_RING(intel_get_drm_devid(fd)) ? I915_EXEC_BLT : 0,
85 gem_write(fd, exec[2].handle, 0, batch, sizeof(batch));
88 gem_write(fd, src, 0, buf, len);
89 do_or_die(drmIoctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &execbuf));
90 gem_read(fd, dst, 0, buf, len);
93 gem_close(fd, exec[2].handle);
96 static void as_gtt_mmap(int fd, uint32_t src, uint32_t dst, void *buf, int len, int loops)
98 struct drm_i915_gem_relocation_entry reloc[] = {
99 { dst, 0, 4*sizeof(uint32_t), 0, I915_GEM_DOMAIN_RENDER, I915_GEM_DOMAIN_RENDER },
100 { src, 0, 7*sizeof(uint32_t), 0, I915_GEM_DOMAIN_RENDER, 0 },
102 struct drm_i915_gem_exec_object2 exec[] = {
105 { gem_create(fd, 4096), 2, (uintptr_t)reloc }
108 COPY_BLT_CMD | BLT_WRITE_ALPHA | BLT_WRITE_RGB,
109 0xcc << 16 | 1 << 25 | 1 << 24 | len,
119 struct drm_i915_gem_execbuffer2 execbuf = {
123 HAS_BLT_RING(intel_get_drm_devid(fd)) ? I915_EXEC_BLT : 0,
125 uint32_t *src_ptr, *dst_ptr;
127 gem_write(fd, exec[2].handle, 0, batch, sizeof(batch));
129 src_ptr = gem_mmap__gtt(fd, src, OBJECT_SIZE, PROT_WRITE);
130 dst_ptr = gem_mmap__gtt(fd, dst, OBJECT_SIZE, PROT_READ);
133 gem_set_domain(fd, src,
134 I915_GEM_DOMAIN_GTT, I915_GEM_DOMAIN_GTT);
135 memcpy(src_ptr, buf, len);
137 do_or_die(drmIoctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &execbuf));
138 gem_set_domain(fd, dst,
139 I915_GEM_DOMAIN_GTT, 0);
140 memcpy(buf, dst_ptr, len);
143 munmap(dst_ptr, len);
144 munmap(src_ptr, len);
145 gem_close(fd, exec[2].handle);
149 static void as_cpu_mmap(int fd, uint32_t src, uint32_t dst, void *buf, int len, int loops)
151 struct drm_i915_gem_relocation_entry reloc[] = {
152 { dst, 0, 4*sizeof(uint32_t), 0, I915_GEM_DOMAIN_RENDER, I915_GEM_DOMAIN_RENDER },
153 { src, 0, 7*sizeof(uint32_t), 0, I915_GEM_DOMAIN_RENDER, 0 },
155 struct drm_i915_gem_exec_object2 exec[] = {
158 { gem_create(fd, 4096), 2, (uintptr_t)reloc }
161 COPY_BLT_CMD | BLT_WRITE_ALPHA | BLT_WRITE_RGB,
162 0xcc << 16 | 1 << 25 | 1 << 24 | len,
172 struct drm_i915_gem_execbuffer2 execbuf = {
176 HAS_BLT_RING(intel_get_drm_devid(fd)) ? I915_EXEC_BLT : 0,
178 uint32_t *src_ptr, *dst_ptr;
180 gem_write(fd, exec[2].handle, 0, batch, sizeof(batch));
182 src_ptr = gem_mmap__cpu(fd, src, OBJECT_SIZE, PROT_WRITE);
183 dst_ptr = gem_mmap__cpu(fd, dst, OBJECT_SIZE, PROT_READ);
186 gem_set_domain(fd, src,
187 I915_GEM_DOMAIN_CPU, I915_GEM_DOMAIN_CPU);
188 memcpy(src_ptr, buf, len);
190 do_or_die(drmIoctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &execbuf));
191 gem_set_domain(fd, dst,
192 I915_GEM_DOMAIN_CPU, 0);
193 memcpy(buf, dst_ptr, len);
196 munmap(dst_ptr, len);
197 munmap(src_ptr, len);
198 gem_close(fd, exec[2].handle);
201 static void test_copy(int fd, uint32_t src, uint32_t dst, uint32_t *buf, int len)
203 struct drm_i915_gem_relocation_entry reloc[] = {
204 { dst, 0, 4*sizeof(uint32_t), 0, I915_GEM_DOMAIN_RENDER, I915_GEM_DOMAIN_RENDER },
205 { src, 0, 7*sizeof(uint32_t), 0, I915_GEM_DOMAIN_RENDER, 0 },
207 struct drm_i915_gem_exec_object2 exec[] = {
210 { gem_create(fd, 4096), 2, (uintptr_t)reloc }
213 COPY_BLT_CMD | BLT_WRITE_ALPHA | BLT_WRITE_RGB,
214 0xcc << 16 | 1 << 25 | 1 << 24 | len,
224 struct drm_i915_gem_execbuffer2 execbuf = {
228 HAS_BLT_RING(intel_get_drm_devid(fd)) ? I915_EXEC_BLT : 0,
232 gem_write(fd, exec[2].handle, 0, batch, sizeof(batch));
234 for (i = 0; i < len/4; i++)
237 gem_write(fd, src, 0, buf, len);
240 do_or_die(drmIoctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &execbuf));
241 gem_read(fd, dst, 0, buf, len);
243 gem_close(fd, exec[2].handle);
245 for (i = 0; i < len/4; i++)
249 static void test_as_gtt_mmap(int fd, uint32_t src, uint32_t dst, int len)
251 struct drm_i915_gem_relocation_entry reloc[] = {
252 { dst, 0, 4*sizeof(uint32_t), 0, I915_GEM_DOMAIN_RENDER, I915_GEM_DOMAIN_RENDER },
253 { src, 0, 7*sizeof(uint32_t), 0, I915_GEM_DOMAIN_RENDER, 0 },
255 struct drm_i915_gem_exec_object2 exec[] = {
258 { gem_create(fd, 4096), 2, (uintptr_t)reloc }
261 COPY_BLT_CMD | BLT_WRITE_ALPHA | BLT_WRITE_RGB,
262 0xcc << 16 | 1 << 25 | 1 << 24 | len,
272 struct drm_i915_gem_execbuffer2 execbuf = {
276 HAS_BLT_RING(intel_get_drm_devid(fd)) ? I915_EXEC_BLT : 0,
278 uint32_t *src_ptr, *dst_ptr;
281 gem_write(fd, exec[2].handle, 0, batch, sizeof(batch));
283 src_ptr = gem_mmap__gtt(fd, src, OBJECT_SIZE, PROT_WRITE);
284 dst_ptr = gem_mmap__gtt(fd, dst, OBJECT_SIZE, PROT_READ);
286 gem_set_domain(fd, src, I915_GEM_DOMAIN_GTT, I915_GEM_DOMAIN_GTT);
287 for (i = 0; i < len/4; i++)
290 do_or_die(drmIoctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &execbuf));
291 gem_close(fd, exec[2].handle);
293 gem_set_domain(fd, dst, I915_GEM_DOMAIN_GTT, 0);
294 for (i = 0; i < len/4; i++)
295 assert(dst_ptr[i] == i);
297 munmap(dst_ptr, len);
298 munmap(src_ptr, len);
301 static void test_as_cpu_mmap(int fd, uint32_t src, uint32_t dst, int len)
303 struct drm_i915_gem_relocation_entry reloc[] = {
304 { dst, 0, 4*sizeof(uint32_t), 0, I915_GEM_DOMAIN_RENDER, I915_GEM_DOMAIN_RENDER },
305 { src, 0, 7*sizeof(uint32_t), 0, I915_GEM_DOMAIN_RENDER, 0 },
307 struct drm_i915_gem_exec_object2 exec[] = {
310 { gem_create(fd, 4096), 2, (uintptr_t)reloc }
313 COPY_BLT_CMD | BLT_WRITE_ALPHA | BLT_WRITE_RGB,
314 0xcc << 16 | 1 << 25 | 1 << 24 | len,
324 struct drm_i915_gem_execbuffer2 execbuf = {
328 HAS_BLT_RING(intel_get_drm_devid(fd)) ? I915_EXEC_BLT : 0,
330 uint32_t *src_ptr, *dst_ptr;
333 gem_write(fd, exec[2].handle, 0, batch, sizeof(batch));
335 src_ptr = gem_mmap__cpu(fd, src, OBJECT_SIZE, PROT_WRITE);
336 dst_ptr = gem_mmap__cpu(fd, dst, OBJECT_SIZE, PROT_READ);
338 gem_set_domain(fd, src, I915_GEM_DOMAIN_CPU, I915_GEM_DOMAIN_CPU);
339 for (i = 0; i < len/4; i++)
342 do_or_die(drmIoctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &execbuf));
343 gem_close(fd, exec[2].handle);
345 gem_set_domain(fd, dst, I915_GEM_DOMAIN_CPU, 0);
346 for (i = 0; i < len/4; i++)
347 assert(dst_ptr[i] == i);
349 munmap(dst_ptr, len);
350 munmap(src_ptr, len);
353 static double elapsed(const struct timeval *start,
354 const struct timeval *end,
357 return (1e6*(end->tv_sec - start->tv_sec) + (end->tv_usec - start->tv_usec))/loop;
360 static const char *bytes_per_sec(char *buf, double v)
362 const char *order[] = {
371 while (v > 1000 && o[1]) {
375 sprintf(buf, "%.1f%s/s", v, *o);
379 int main(int argc, char **argv)
383 uint32_t *tmp, src, dst;
386 drmtest_subtest_init(argc, argv);
387 drmtest_skip_on_simulation();
390 object_size = atoi(argv[1]);
391 if (object_size == 0)
392 object_size = OBJECT_SIZE;
393 object_size = (object_size + 3) & -4;
397 dst = gem_create(fd, object_size);
398 src = gem_create(fd, object_size);
399 tmp = malloc(object_size);
401 if (gem_set_caching(fd, src, 0) == 0 &&
402 gem_set_caching(fd, dst, 0) == 0) {
403 if (drmtest_run_subtest("uncached-copy-correctness"))
404 test_copy(fd, src, dst, tmp, object_size);
405 if (drmtest_run_subtest("uncached-copy-performance")) {
406 for (count = 1; count <= 1<<17; count <<= 1) {
407 struct timeval start, end;
409 gettimeofday(&start, NULL);
410 copy(fd, src, dst, tmp, object_size, count);
411 gettimeofday(&end, NULL);
412 printf("Time to uncached copy %d bytes x %6d: %7.3fµs, %s\n",
414 elapsed(&start, &end, count),
415 bytes_per_sec((char *)buf, object_size/elapsed(&start, &end, count)*1e6));
420 if (drmtest_run_subtest("uncached-pwrite-blt-gtt_mmap-correctness"))
421 test_as_gtt_mmap(fd, src, dst, object_size);
422 if (drmtest_run_subtest("uncached-pwrite-blt-gtt_mmap-performance")) {
423 for (count = 1; count <= 1<<17; count <<= 1) {
424 struct timeval start, end;
426 gettimeofday(&start, NULL);
427 as_gtt_mmap(fd, src, dst, tmp, object_size, count);
428 gettimeofday(&end, NULL);
429 printf("** mmap uncached copy %d bytes x %6d: %7.3fµs, %s\n",
431 elapsed(&start, &end, count),
432 bytes_per_sec((char *)buf, object_size/elapsed(&start, &end, count)*1e6));
438 if (gem_set_caching(fd, src, 1) == 0 &&
439 gem_set_caching(fd, dst, 1) == 0) {
440 if (drmtest_run_subtest("snooped-copy-correctness"))
441 test_copy(fd, src, dst, tmp, object_size);
442 if (drmtest_run_subtest("snooped-copy-performance")) {
443 for (count = 1; count <= 1<<17; count <<= 1) {
444 struct timeval start, end;
446 gettimeofday(&start, NULL);
447 copy(fd, src, dst, tmp, object_size, count);
448 gettimeofday(&end, NULL);
449 printf("Time to snooped copy %d bytes x %6d: %7.3fµs, %s\n",
451 elapsed(&start, &end, count),
452 bytes_per_sec((char *)buf, object_size/elapsed(&start, &end, count)*1e6));
457 if (drmtest_run_subtest("snooped-pwrite-blt-cpu_mmap-correctness"))
458 test_as_cpu_mmap(fd, src, dst, object_size);
459 if (drmtest_run_subtest("snooped-pwrite-blt-cpu_mmap-performance")) {
460 for (count = 1; count <= 1<<17; count <<= 1) {
461 struct timeval start, end;
463 gettimeofday(&start, NULL);
464 as_cpu_mmap(fd, src, dst, tmp, object_size, count);
465 gettimeofday(&end, NULL);
466 printf("** mmap snooped copy %d bytes x %6d: %7.3fµs, %s\n",
468 elapsed(&start, &end, count),
469 bytes_per_sec((char *)buf, object_size/elapsed(&start, &end, count)*1e6));
475 if (gem_set_caching(fd, src, 2) == 0 &&
476 gem_set_caching(fd, dst, 2) == 0) {
477 if (drmtest_run_subtest("display-copy-correctness"))
478 test_copy(fd, src, dst, tmp, object_size);
479 if (drmtest_run_subtest("display-copy-performance")) {
480 for (count = 1; count <= 1<<17; count <<= 1) {
481 struct timeval start, end;
483 gettimeofday(&start, NULL);
484 copy(fd, src, dst, tmp, object_size, count);
485 gettimeofday(&end, NULL);
486 printf("Time to display copy %d bytes x %6d: %7.3fµs, %s\n",
488 elapsed(&start, &end, count),
489 bytes_per_sec((char *)buf, object_size/elapsed(&start, &end, count)*1e6));
494 if (drmtest_run_subtest("display-pwrite-blt-gtt_mmap-correctness"))
495 test_as_gtt_mmap(fd, src, dst, object_size);
496 if (drmtest_run_subtest("display-pwrite-blt-gtt_mmap-performance")) {
497 for (count = 1; count <= 1<<17; count <<= 1) {
498 struct timeval start, end;
500 gettimeofday(&start, NULL);
501 as_gtt_mmap(fd, src, dst, tmp, object_size, count);
502 gettimeofday(&end, NULL);
503 printf("** mmap display copy %d bytes x %6d: %7.3fµs, %s\n",
505 elapsed(&start, &end, count),
506 bytes_per_sec((char *)buf, object_size/elapsed(&start, &end, count)*1e6));