2 * Copyright © 2014 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>
37 #include <sys/ioctl.h>
45 #define BLT_WRITE_ALPHA (1<<21)
46 #define BLT_WRITE_RGB (1<<20)
48 static const uint32_t canary = 0xdeadbeef;
50 static double elapsed(const struct timeval *start,
51 const struct timeval *end)
53 return 1e6*(end->tv_sec - start->tv_sec) + (end->tv_usec - start->tv_usec);
56 static void busy(int fd, uint32_t handle, int size, int loops)
58 struct drm_i915_gem_relocation_entry reloc[20];
59 struct drm_i915_gem_exec_object2 gem_exec[2];
60 struct drm_i915_gem_execbuffer2 execbuf;
61 struct drm_i915_gem_pwrite gem_pwrite;
62 struct drm_i915_gem_create create;
63 uint32_t buf[102], *b;
66 memset(reloc, 0, sizeof(reloc));
67 memset(gem_exec, 0, sizeof(gem_exec));
68 memset(&execbuf, 0, sizeof(execbuf));
71 for (i = 0; i < 20; i++) {
72 *b++ = COLOR_BLT_CMD | BLT_WRITE_ALPHA | BLT_WRITE_RGB;
73 *b++ = 0xf0 << 16 | 1 << 25 | 1 << 24 | 4096;
74 *b++ = size >> 12 << 16 | 4096;
75 reloc[i].offset = (b - buf) * sizeof(uint32_t);
76 reloc[i].target_handle = handle;
77 reloc[i].read_domains = I915_GEM_DOMAIN_RENDER;
78 reloc[i].write_domain = I915_GEM_DOMAIN_RENDER;
82 *b++ = MI_BATCH_BUFFER_END;
85 gem_exec[0].handle = handle;
86 gem_exec[0].flags = EXEC_OBJECT_NEEDS_FENCE;
90 drmIoctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create);
91 gem_exec[1].handle = create.handle;
92 gem_exec[1].relocation_count = 20;
93 gem_exec[1].relocs_ptr = (uintptr_t)reloc;
95 execbuf.buffers_ptr = (uintptr_t)gem_exec;
96 execbuf.buffer_count = 2;
97 execbuf.batch_len = sizeof(buf);
98 execbuf.flags = 1 << 11;
99 if (HAS_BLT_RING(intel_get_drm_devid(fd)))
100 execbuf.flags |= I915_EXEC_BLT;
102 gem_pwrite.handle = gem_exec[1].handle;
103 gem_pwrite.offset = 0;
104 gem_pwrite.size = sizeof(buf);
105 gem_pwrite.data_ptr = (uintptr_t)buf;
106 if (drmIoctl(fd, DRM_IOCTL_I915_GEM_PWRITE, &gem_pwrite) == 0) {
108 drmIoctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &execbuf);
111 drmIoctl(fd, DRM_IOCTL_GEM_CLOSE, &create.handle);
114 static void run(int child)
116 const int size = 4096 * (256 + child * child);
117 const int tiling = child % 2;
118 const int write = child % 2;
119 int fd = drm_open_any();
120 uint32_t handle = gem_create(fd, size);
126 if (tiling != I915_TILING_NONE)
127 gem_set_tiling(fd, handle, tiling, 4096);
129 /* load up the unfaulted bo */
130 busy(fd, handle, size, 100);
132 /* Note that we ignore the API and rely on the implict
133 * set-to-gtt-domain within the fault handler.
136 ptr = gem_mmap(fd, handle, size, PROT_READ | PROT_WRITE);
137 ptr[rand() % (size / 4)] = canary;
139 ptr = gem_mmap(fd, handle, size, PROT_READ);
140 x = ptr[rand() % (size / 4)];
143 igt_assert(x == canary);
147 int main(int argc, char **argv)
149 struct timeval start, end;
154 igt_skip_on_simulation();
156 gettimeofday(&start, NULL);
157 for (n = 0; n < ARRAY_SIZE(children); n++) {
158 switch ((children[n] = fork())) {
159 case -1: igt_assert(0);
160 case 0: run(n); break;
165 for (n = 0; n < ARRAY_SIZE(children); n++) {
167 while (waitpid(children[n], &status, 0) == -1 &&
170 igt_assert(status == 0);
172 gettimeofday(&end, NULL);
173 printf("Time to execute %lu children: %7.3fms\n",
174 ARRAY_SIZE(children), elapsed(&start, &end) / 1000);