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 igt_subtest_init(argc, argv);
387 igt_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 gem_set_caching(fd, src, 0);
402 gem_set_caching(fd, dst, 0);
404 igt_subtest("uncached-copy-correctness")
405 test_copy(fd, src, dst, tmp, object_size);
406 igt_subtest("uncached-copy-performance") {
407 for (count = 1; count <= 1<<17; count <<= 1) {
408 struct timeval start, end;
410 gettimeofday(&start, NULL);
411 copy(fd, src, dst, tmp, object_size, count);
412 gettimeofday(&end, NULL);
413 printf("Time to uncached copy %d bytes x %6d: %7.3fµs, %s\n",
415 elapsed(&start, &end, count),
416 bytes_per_sec((char *)buf, object_size/elapsed(&start, &end, count)*1e6));
421 igt_subtest("uncached-pwrite-blt-gtt_mmap-correctness")
422 test_as_gtt_mmap(fd, src, dst, object_size);
423 igt_subtest("uncached-pwrite-blt-gtt_mmap-performance") {
424 for (count = 1; count <= 1<<17; count <<= 1) {
425 struct timeval start, end;
427 gettimeofday(&start, NULL);
428 as_gtt_mmap(fd, src, dst, tmp, object_size, count);
429 gettimeofday(&end, NULL);
430 printf("** mmap uncached copy %d bytes x %6d: %7.3fµs, %s\n",
432 elapsed(&start, &end, count),
433 bytes_per_sec((char *)buf, object_size/elapsed(&start, &end, count)*1e6));
438 gem_set_caching(fd, src, 1);
439 gem_set_caching(fd, dst, 1);
441 igt_subtest("snooped-copy-correctness")
442 test_copy(fd, src, dst, tmp, object_size);
443 igt_subtest("snooped-copy-performance") {
444 for (count = 1; count <= 1<<17; count <<= 1) {
445 struct timeval start, end;
447 gettimeofday(&start, NULL);
448 copy(fd, src, dst, tmp, object_size, count);
449 gettimeofday(&end, NULL);
450 printf("Time to snooped copy %d bytes x %6d: %7.3fµs, %s\n",
452 elapsed(&start, &end, count),
453 bytes_per_sec((char *)buf, object_size/elapsed(&start, &end, count)*1e6));
458 igt_subtest("snooped-pwrite-blt-cpu_mmap-correctness")
459 test_as_cpu_mmap(fd, src, dst, object_size);
460 igt_subtest("snooped-pwrite-blt-cpu_mmap-performance") {
461 for (count = 1; count <= 1<<17; count <<= 1) {
462 struct timeval start, end;
464 gettimeofday(&start, NULL);
465 as_cpu_mmap(fd, src, dst, tmp, object_size, count);
466 gettimeofday(&end, NULL);
467 printf("** mmap snooped copy %d bytes x %6d: %7.3fµs, %s\n",
469 elapsed(&start, &end, count),
470 bytes_per_sec((char *)buf, object_size/elapsed(&start, &end, count)*1e6));
475 gem_set_caching(fd, src, 2);
476 gem_set_caching(fd, dst, 2);
478 igt_subtest("display-copy-correctness")
479 test_copy(fd, src, dst, tmp, object_size);
480 igt_subtest("display-copy-performance") {
481 for (count = 1; count <= 1<<17; count <<= 1) {
482 struct timeval start, end;
484 gettimeofday(&start, NULL);
485 copy(fd, src, dst, tmp, object_size, count);
486 gettimeofday(&end, NULL);
487 printf("Time to display copy %d bytes x %6d: %7.3fµs, %s\n",
489 elapsed(&start, &end, count),
490 bytes_per_sec((char *)buf, object_size/elapsed(&start, &end, count)*1e6));
495 igt_subtest("display-pwrite-blt-gtt_mmap-correctness")
496 test_as_gtt_mmap(fd, src, dst, object_size);
497 igt_subtest("display-pwrite-blt-gtt_mmap-performance") {
498 for (count = 1; count <= 1<<17; count <<= 1) {
499 struct timeval start, end;
501 gettimeofday(&start, NULL);
502 as_gtt_mmap(fd, src, dst, tmp, object_size, count);
503 gettimeofday(&end, NULL);
504 printf("** mmap display copy %d bytes x %6d: %7.3fµs, %s\n",
506 elapsed(&start, &end, count),
507 bytes_per_sec((char *)buf, object_size/elapsed(&start, &end, count)*1e6));