2 * Copyright © 2012,2013 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>
28 /* Exercises the basic execbuffer using theh andle LUT interface */
43 #define MI_BATCH_BUFFER_END (0xA<<23)
44 #define BATCH_SIZE (1024*1024)
46 #define LOCAL_I915_EXEC_HANDLE_LUT (1<<12)
52 static int exec(int fd, uint32_t handle, unsigned int flags)
54 struct drm_i915_gem_execbuffer2 execbuf;
55 struct drm_i915_gem_exec_object2 gem_exec[1];
56 struct drm_i915_gem_relocation_entry gem_reloc[1];
58 gem_reloc[0].offset = 1024;
59 gem_reloc[0].delta = 0;
60 gem_reloc[0].target_handle =
61 !!(flags & USE_LUT) ^ !!(flags & BROKEN) ? 0 : handle;
62 gem_reloc[0].read_domains = I915_GEM_DOMAIN_RENDER;
63 gem_reloc[0].write_domain = 0;
64 gem_reloc[0].presumed_offset = 0;
66 gem_exec[0].handle = handle;
67 gem_exec[0].relocation_count = 1;
68 gem_exec[0].relocs_ptr = (uintptr_t) gem_reloc;
69 gem_exec[0].alignment = 0;
70 gem_exec[0].offset = 0;
71 gem_exec[0].flags = 0;
72 gem_exec[0].rsvd1 = 0;
73 gem_exec[0].rsvd2 = 0;
75 execbuf.buffers_ptr = (uintptr_t)gem_exec;
76 execbuf.buffer_count = 1;
77 execbuf.batch_start_offset = 0;
78 execbuf.batch_len = 8;
79 execbuf.cliprects_ptr = 0;
80 execbuf.num_cliprects = 0;
83 execbuf.flags = flags & USE_LUT ? LOCAL_I915_EXEC_HANDLE_LUT : 0;
84 i915_execbuffer2_set_context_id(execbuf, 0);
88 DRM_IOCTL_I915_GEM_EXECBUFFER2,
92 static int many_exec(int fd, uint32_t batch, int num_exec, int num_reloc, unsigned flags)
94 struct drm_i915_gem_execbuffer2 execbuf;
95 struct drm_i915_gem_exec_object2 *gem_exec;
96 struct drm_i915_gem_relocation_entry *gem_reloc;
97 unsigned max_handle = batch;
100 gem_exec = calloc(num_exec+1, sizeof(*gem_exec));
101 gem_reloc = calloc(num_reloc, sizeof(*gem_reloc));
102 assert(gem_exec && gem_reloc);
104 for (n = 0; n < num_exec; n++) {
105 gem_exec[n].handle = gem_create(fd, 4096);
106 if (gem_exec[n].handle > max_handle)
107 max_handle = gem_exec[n].handle;
108 gem_exec[n].relocation_count = 0;
109 gem_exec[n].relocs_ptr = 0;
110 gem_exec[n].alignment = 0;
111 gem_exec[n].offset = 0;
112 gem_exec[n].flags = 0;
113 gem_exec[n].rsvd1 = 0;
114 gem_exec[n].rsvd2 = 0;
117 gem_exec[n].handle = batch;
118 gem_exec[n].relocation_count = num_reloc;
119 gem_exec[n].relocs_ptr = (uintptr_t) gem_reloc;
122 max_handle = num_exec + 1;
125 for (n = 0; n < num_reloc; n++) {
128 if (flags & BROKEN) {
129 target = -(rand() % 4096) - 1;
131 target = rand() % (num_exec + 1);
132 if ((flags & USE_LUT) == 0)
133 target = gem_exec[target].handle;
136 gem_reloc[n].offset = 1024;
137 gem_reloc[n].delta = 0;
138 gem_reloc[n].target_handle = target;
139 gem_reloc[n].read_domains = I915_GEM_DOMAIN_RENDER;
140 gem_reloc[n].write_domain = 0;
141 gem_reloc[n].presumed_offset = 0;
144 execbuf.buffers_ptr = (uintptr_t)gem_exec;
145 execbuf.buffer_count = num_exec + 1;
146 execbuf.batch_start_offset = 0;
147 execbuf.batch_len = 8;
148 execbuf.cliprects_ptr = 0;
149 execbuf.num_cliprects = 0;
152 execbuf.flags = flags & USE_LUT ? LOCAL_I915_EXEC_HANDLE_LUT : 0;
153 i915_execbuffer2_set_context_id(execbuf, 0);
157 DRM_IOCTL_I915_GEM_EXECBUFFER2,
160 for (n = 0; n < num_exec; n++)
161 gem_close(fd, gem_exec[n].handle);
169 #define _fail(x) ((x) == -1 && errno == ENOENT)
170 #define ASSERT(x) do { \
172 fprintf(stderr, "%s:%d failed, errno=%d\n", \
173 __FUNCTION__, __LINE__, errno); \
177 #define fail(x) ASSERT(_fail(x))
178 #define pass(x) ASSERT(!_fail(x))
180 int main(int argc, char **argv)
182 uint32_t batch[2] = {MI_BATCH_BUFFER_END};
188 handle = gem_create(fd, 4096);
189 gem_write(fd, handle, 0, batch, sizeof(batch));
191 do_or_die(exec(fd, handle, NORMAL));
192 fail(exec(fd, handle, BROKEN));
194 if (exec(fd, handle, USE_LUT))
197 do_or_die(exec(fd, handle, USE_LUT));
198 fail(exec(fd, handle, USE_LUT | BROKEN));
200 for (i = 2; i <= SLOW_QUICK(65536, 8); i *= 2) {
201 if (many_exec(fd, handle, i+1, i+1, NORMAL) == -1 &&
205 pass(many_exec(fd, handle, i-1, i-1, NORMAL));
206 pass(many_exec(fd, handle, i-1, i, NORMAL));
207 pass(many_exec(fd, handle, i-1, i+1, NORMAL));
208 pass(many_exec(fd, handle, i, i-1, NORMAL));
209 pass(many_exec(fd, handle, i, i, NORMAL));
210 pass(many_exec(fd, handle, i, i+1, NORMAL));
211 pass(many_exec(fd, handle, i+1, i-1, NORMAL));
212 pass(many_exec(fd, handle, i+1, i, NORMAL));
213 pass(many_exec(fd, handle, i+1, i+1, NORMAL));
215 fail(many_exec(fd, handle, i-1, i-1, NORMAL | BROKEN));
216 fail(many_exec(fd, handle, i-1, i, NORMAL | BROKEN));
217 fail(many_exec(fd, handle, i-1, i+1, NORMAL | BROKEN));
218 fail(many_exec(fd, handle, i, i-1, NORMAL | BROKEN));
219 fail(many_exec(fd, handle, i, i, NORMAL | BROKEN));
220 fail(many_exec(fd, handle, i, i+1, NORMAL | BROKEN));
221 fail(many_exec(fd, handle, i+1, i-1, NORMAL | BROKEN));
222 fail(many_exec(fd, handle, i+1, i, NORMAL | BROKEN));
223 fail(many_exec(fd, handle, i+1, i+1, NORMAL | BROKEN));
225 pass(many_exec(fd, handle, i-1, i-1, USE_LUT));
226 pass(many_exec(fd, handle, i-1, i, USE_LUT));
227 pass(many_exec(fd, handle, i-1, i+1, USE_LUT));
228 pass(many_exec(fd, handle, i, i-1, USE_LUT));
229 pass(many_exec(fd, handle, i, i, USE_LUT));
230 pass(many_exec(fd, handle, i, i+1, USE_LUT));
231 pass(many_exec(fd, handle, i+1, i-1, USE_LUT));
232 pass(many_exec(fd, handle, i+1, i, USE_LUT));
233 pass(many_exec(fd, handle, i+1, i+1, USE_LUT));
235 fail(many_exec(fd, handle, i-1, i-1, USE_LUT | BROKEN));
236 fail(many_exec(fd, handle, i-1, i, USE_LUT | BROKEN));
237 fail(many_exec(fd, handle, i-1, i+1, USE_LUT | BROKEN));
238 fail(many_exec(fd, handle, i, i-1, USE_LUT | BROKEN));
239 fail(many_exec(fd, handle, i, i, USE_LUT | BROKEN));
240 fail(many_exec(fd, handle, i, i+1, USE_LUT | BROKEN));
241 fail(many_exec(fd, handle, i+1, i-1, USE_LUT | BROKEN));
242 fail(many_exec(fd, handle, i+1, i, USE_LUT | BROKEN));
243 fail(many_exec(fd, handle, i+1, i+1, USE_LUT | BROKEN));