1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
|
/*
* Copyright © 2017 Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*
*/
#include "igt.h"
#include "igt_rand.h"
#include "igt_sysfs.h"
#include "igt_vgem.h"
#include <sys/ioctl.h>
#include <sys/signal.h>
#define LOCAL_I915_EXEC_NO_RELOC (1<<11)
#define LOCAL_I915_EXEC_HANDLE_LUT (1<<12)
#define LOCAL_I915_EXEC_BSD_SHIFT (13)
#define LOCAL_I915_EXEC_BSD_MASK (3 << LOCAL_I915_EXEC_BSD_SHIFT)
#define ENGINE_FLAGS (I915_EXEC_RING_MASK | LOCAL_I915_EXEC_BSD_MASK)
static double elapsed(const struct timespec *start, const struct timespec *end)
{
return ((end->tv_sec - start->tv_sec) +
(end->tv_nsec - start->tv_nsec)*1e-9);
}
static bool ignore_engine(int fd, unsigned engine)
{
if (engine == 0)
return true;
if (gem_has_bsd2(fd) && engine == I915_EXEC_BSD)
return true;
return false;
}
static uint32_t __gem_context_create(int fd)
{
struct drm_i915_gem_context_create arg;
memset(&arg, 0, sizeof(arg));
drmIoctl(fd, DRM_IOCTL_I915_GEM_CONTEXT_CREATE, &arg);
return arg.ctx_id;
}
static void xchg_obj(void *array, unsigned i, unsigned j)
{
struct drm_i915_gem_exec_object2 *obj = array;
uint64_t tmp;
tmp = obj[i].handle;
obj[i].handle = obj[j].handle;
obj[j].handle = tmp;
tmp = obj[i].offset;
obj[i].offset = obj[j].offset;
obj[j].offset = tmp;
}
#define CONTEXTS 0x1
static void wide(int fd, int ring_size, int timeout, unsigned int flags)
{
const uint32_t bbe = MI_BATCH_BUFFER_END;
const int gen = intel_gen(intel_get_drm_devid(fd));
struct {
struct drm_i915_gem_exec_object2 *obj;
struct drm_i915_gem_exec_object2 exec[2];
struct drm_i915_gem_relocation_entry reloc;
struct drm_i915_gem_execbuffer2 execbuf;
uint32_t *cmd;
} *exec;
struct drm_i915_gem_exec_object2 *obj;
struct drm_i915_gem_execbuffer2 execbuf;
unsigned engines[16];
unsigned nengine, engine;
unsigned long count;
double time;
nengine = 0;
for_each_engine(fd, engine) {
if (ignore_engine(fd, engine))
continue;
engines[nengine++] = engine;
}
igt_require(nengine);
exec = calloc(nengine, sizeof(*exec));
igt_assert(exec);
intel_require_memory(nengine*(2 + ring_size), 4096, CHECK_RAM);
obj = calloc(nengine*ring_size + 1, sizeof(*obj));
igt_assert(obj);
for (unsigned e = 0; e < nengine; e++) {
exec[e].obj = calloc(ring_size, sizeof(*exec[e].obj));
igt_assert(exec[e].obj);
for (unsigned n = 0; n < ring_size; n++) {
exec[e].obj[n].handle = gem_create(fd, 4096);
exec[e].obj[n].flags = EXEC_OBJECT_WRITE;
obj[e*ring_size + n].handle = exec[e].obj[n].handle;
}
exec[e].execbuf.buffers_ptr = to_user_pointer(exec[e].exec);
exec[e].execbuf.buffer_count = 1;
exec[e].execbuf.flags = (engines[e] |
LOCAL_I915_EXEC_NO_RELOC |
LOCAL_I915_EXEC_HANDLE_LUT);
if (flags & CONTEXTS) {
exec[e].execbuf.rsvd1 = __gem_context_create(fd);
igt_require(exec[e].execbuf.rsvd1);
}
exec[e].exec[0].handle = gem_create(fd, 4096);
exec[e].cmd = gem_mmap__wc(fd, exec[e].exec[0].handle,
0, 4096, PROT_WRITE);
gem_set_domain(fd, exec[e].exec[0].handle,
I915_GEM_DOMAIN_WC, I915_GEM_DOMAIN_WC);
exec[e].cmd[0] = MI_BATCH_BUFFER_END;
gem_execbuf(fd, &exec[e].execbuf);
exec[e].exec[1] = exec[e].exec[0];
exec[e].execbuf.buffer_count = 2;
exec[e].reloc.target_handle = 1; /* recurse */
exec[e].reloc.offset = sizeof(uint32_t);
exec[e].reloc.read_domains = I915_GEM_DOMAIN_COMMAND;
if (gen < 4)
exec[e].reloc.delta = 1;
exec[e].exec[1].relocs_ptr = to_user_pointer(&exec[e].reloc);
exec[e].exec[1].relocation_count = 1;
}
obj[nengine*ring_size].handle = gem_create(fd, 4096);
gem_write(fd, obj[nengine*ring_size].handle, 0, &bbe, sizeof(bbe));
memset(&execbuf, 0, sizeof(execbuf));
execbuf.buffers_ptr = to_user_pointer(obj);
execbuf.buffer_count = nengine*ring_size + 1;
intel_detect_and_clear_missed_interrupts(fd);
time = 0;
count = 0;
igt_until_timeout(timeout) {
struct timespec start, now;
for (unsigned e = 0; e < nengine; e++) {
uint64_t address;
int i;
if (flags & CONTEXTS) {
gem_context_destroy(fd, exec[e].execbuf.rsvd1);
exec[e].execbuf.rsvd1 = __gem_context_create(fd);
}
exec[e].reloc.presumed_offset = exec[e].exec[1].offset;
address = (exec[e].reloc.presumed_offset +
exec[e].reloc.delta);
gem_set_domain(fd, exec[e].exec[1].handle,
I915_GEM_DOMAIN_WC, I915_GEM_DOMAIN_WC);
i = 0;
exec[e].cmd[i] = MI_BATCH_BUFFER_START;
if (gen >= 8) {
exec[e].cmd[i] |= 1 << 8 | 1;
exec[e].cmd[++i] = address;
exec[e].cmd[++i] = address >> 32;
} else if (gen >= 6) {
exec[e].cmd[i] |= 1 << 8;
exec[e].cmd[++i] = address;
} else {
exec[e].cmd[i] |= 2 << 6;
exec[e].cmd[++i] = address;
}
exec[e].exec[0] = obj[nengine*ring_size];
gem_execbuf(fd, &exec[e].execbuf);
for (unsigned n = 0; n < ring_size; n++) {
exec[e].exec[0] = exec[e].obj[n];
gem_execbuf(fd, &exec[e].execbuf);
exec[e].obj[n].offset = exec[e].exec[0].offset;
}
}
igt_permute_array(obj, nengine*ring_size, xchg_obj);
clock_gettime(CLOCK_MONOTONIC, &start);
for (unsigned e = 0; e < nengine; e++) {
execbuf.flags = (engines[e] |
LOCAL_I915_EXEC_NO_RELOC |
LOCAL_I915_EXEC_HANDLE_LUT);
gem_execbuf(fd, &execbuf);
}
clock_gettime(CLOCK_MONOTONIC, &now);
time += elapsed(&start, &now);
count += nengine;
for (unsigned e = 0; e < nengine; e++)
exec[e].cmd[0] = MI_BATCH_BUFFER_END;
}
igt_assert_eq(intel_detect_and_clear_missed_interrupts(fd), 0);
igt_info("%s: %'lu cycles: %.3fus\n",
__func__, count, time*1e6 / count);
gem_close(fd, obj[nengine*ring_size].handle);
free(obj);
for (unsigned e = 0; e < nengine; e++) {
if (flags & CONTEXTS)
gem_context_destroy(fd, exec[e].execbuf.rsvd1);
for (unsigned n = 0; n < ring_size; n++)
gem_close(fd, exec[e].obj[n].handle);
free(exec[e].obj);
munmap(exec[e].cmd, 4096);
gem_close(fd, exec[e].exec[1].handle);
}
free(exec);
}
struct cork {
int device;
uint32_t handle;
uint32_t fence;
};
static void plug(int fd, struct cork *c)
{
struct vgem_bo bo;
int dmabuf;
c->device = drm_open_driver(DRIVER_VGEM);
bo.width = bo.height = 1;
bo.bpp = 4;
vgem_create(c->device, &bo);
c->fence = vgem_fence_attach(c->device, &bo, VGEM_FENCE_WRITE);
dmabuf = prime_handle_to_fd(c->device, bo.handle);
c->handle = prime_fd_to_handle(fd, dmabuf);
close(dmabuf);
}
static void unplug(struct cork *c)
{
vgem_fence_signal(c->device, c->fence);
close(c->device);
}
static void alarm_handler(int sig)
{
}
static int __execbuf(int fd, struct drm_i915_gem_execbuffer2 *execbuf)
{
return ioctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, execbuf);
}
static unsigned int measure_ring_size(int fd)
{
struct sigaction sa = { .sa_handler = alarm_handler };
struct drm_i915_gem_exec_object2 obj[2];
struct drm_i915_gem_execbuffer2 execbuf;
const uint32_t bbe = MI_BATCH_BUFFER_END;
unsigned int count, last;
struct itimerval itv;
struct cork c;
memset(obj, 0, sizeof(obj));
obj[1].handle = gem_create(fd, 4096);
gem_write(fd, obj[1].handle, 0, &bbe, sizeof(bbe));
plug(fd, &c);
obj[0].handle = c.handle;
memset(&execbuf, 0, sizeof(execbuf));
execbuf.buffers_ptr = to_user_pointer(obj);
execbuf.buffer_count = 2;
sigaction(SIGALRM, &sa, NULL);
itv.it_interval.tv_sec = 0;
itv.it_interval.tv_usec = 100;
itv.it_value.tv_sec = 0;
itv.it_value.tv_usec = 1000;
setitimer(ITIMER_REAL, &itv, NULL);
last = count = 0;
do {
if (__execbuf(fd, &execbuf) == 0) {
count++;
continue;
}
if (last == count)
break;
last = count;
} while (1);
memset(&itv, 0, sizeof(itv));
setitimer(ITIMER_REAL, &itv, NULL);
unplug(&c);
gem_close(fd, obj[1].handle);
return count;
}
igt_main
{
int ring_size = 0;
int device = -1;
igt_fixture {
device = drm_open_driver(DRIVER_INTEL);
igt_require_gem(device);
gem_submission_print_method(device);
ring_size = measure_ring_size(device) - 10;
if (!gem_has_execlists(device))
ring_size /= 2;
igt_info("Ring size: %d batches\n", ring_size);
igt_require(ring_size > 0);
igt_fork_hang_detector(device);
}
igt_subtest("wide-all")
wide(device, ring_size, 20, 0);
igt_subtest("wide-contexts")
wide(device, ring_size, 20, CONTEXTS);
igt_fixture {
igt_stop_hang_detector();
close(device);
}
}
|