blob: 15989616ad4e1c38676f21d98676bd6f49e19bdd [file] [log] [blame]
Chris Wilson5615ab22016-03-11 10:48:47 +00001/*
2 * Copyright © 2009 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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
21 * IN THE SOFTWARE.
22 *
23 */
24
25/** @file gem_exec_whisper.c
26 *
27 * Pass around a value to write into a scratch buffer between lots of batches
28 */
29
30#include "igt.h"
31#include "igt_gt.h"
Chris Wilson58de7852017-03-24 18:11:08 +000032#include "igt_debugfs.h"
Chris Wilsonc1172132017-09-24 17:14:36 +010033#include "igt_rand.h"
Chris Wilsonc6e26e42016-07-22 12:58:54 +010034#include "igt_sysfs.h"
Chris Wilson5615ab22016-03-11 10:48:47 +000035
36#define LOCAL_I915_EXEC_NO_RELOC (1<<11)
37#define LOCAL_I915_EXEC_HANDLE_LUT (1<<12)
38
39#define LOCAL_I915_EXEC_BSD_SHIFT (13)
40#define LOCAL_I915_EXEC_BSD_MASK (3 << LOCAL_I915_EXEC_BSD_SHIFT)
41
42#define ENGINE_MASK (I915_EXEC_RING_MASK | LOCAL_I915_EXEC_BSD_MASK)
43
Chris Wilson8f6ed9e2016-03-12 11:20:03 +000044#define VERIFY 0
45
Chris Wilson58de7852017-03-24 18:11:08 +000046static void write_seqno(int dir, unsigned offset)
Chris Wilson5615ab22016-03-11 10:48:47 +000047{
48 uint32_t seqno = UINT32_MAX - offset;
Chris Wilson5615ab22016-03-11 10:48:47 +000049
Chris Wilson58de7852017-03-24 18:11:08 +000050 igt_sysfs_printf(dir, "i915_next_seqno", "0x%x", seqno);
Chris Wilson5615ab22016-03-11 10:48:47 +000051
52 igt_debug("next seqno set to: 0x%x\n", seqno);
53}
54
55static void check_bo(int fd, uint32_t handle)
56{
57 uint32_t *map;
58 int i;
59
60 igt_debug("Verifying result\n");
61 map = gem_mmap__cpu(fd, handle, 0, 4096, PROT_READ);
62 gem_set_domain(fd, handle, I915_GEM_DOMAIN_CPU, 0);
63 for (i = 0; i < 1024; i++)
64 igt_assert_eq(map[i], i);
65 munmap(map, 4096);
66}
67
Chris Wilson8f6ed9e2016-03-12 11:20:03 +000068static void verify_reloc(int fd, uint32_t handle,
69 const struct drm_i915_gem_relocation_entry *reloc)
70{
71 if (VERIFY) {
72 uint64_t target = 0;
73 if (intel_gen(intel_get_drm_devid(fd)) >= 8)
74 gem_read(fd, handle, reloc->offset, &target, 8);
75 else
76 gem_read(fd, handle, reloc->offset, &target, 4);
77 igt_assert_eq_u64(target,
78 reloc->presumed_offset + reloc->delta);
79 }
80}
81
Chris Wilson5615ab22016-03-11 10:48:47 +000082static int __gem_context_create(int fd, uint32_t *ctx_id)
83{
84 struct drm_i915_gem_context_create arg;
85 int ret = 0;
86
87 memset(&arg, 0, sizeof(arg));
88 if (drmIoctl(fd, DRM_IOCTL_I915_GEM_CONTEXT_CREATE, &arg))
89 ret = -errno;
90
91 *ctx_id = arg.ctx_id;
92 return ret;
93}
94
Chris Wilsonbc787762017-05-18 12:11:59 +010095static bool ignore_engine(int fd, unsigned engine)
Chris Wilson5615ab22016-03-11 10:48:47 +000096{
Chris Wilson5df2de72016-03-15 09:52:57 +000097 if (engine == 0)
98 return true;
99
Chris Wilsonbc787762017-05-18 12:11:59 +0100100 if (!gem_can_store_dword(fd, engine))
Chris Wilson5df2de72016-03-15 09:52:57 +0000101 return true;
102
103 return false;
Chris Wilson5615ab22016-03-11 10:48:47 +0000104}
105
106#define CONTEXTS 0x1
Chris Wilson9eacbd22016-03-11 14:31:56 +0000107#define FDS 0x2
Chris Wilson83cfed82016-03-19 14:21:34 +0000108#define INTERRUPTIBLE 0x4
Chris Wilson7b34c512016-06-04 16:12:28 +0100109#define CHAIN 0x8
Chris Wilson587753b2016-09-12 16:06:48 +0100110#define FORKED 0x10
Chris Wilson1a76d882016-10-01 16:50:51 +0100111#define HANG 0x20
Chris Wilson38dc1582017-04-05 17:09:15 +0100112#define SYNC 0x40
Chris Wilsonc1172132017-09-24 17:14:36 +0100113#define PRIORITY 0x80
Chris Wilson1a76d882016-10-01 16:50:51 +0100114
115struct hang {
116 struct drm_i915_gem_exec_object2 obj;
117 struct drm_i915_gem_relocation_entry reloc;
118 struct drm_i915_gem_execbuffer2 execbuf;
119 int fd;
120};
121
122static void init_hang(struct hang *h)
123{
124 uint32_t *batch;
125 int i, gen;
126
127 h->fd = drm_open_driver(DRIVER_INTEL);
128 igt_allow_hang(h->fd, 0, 0);
129
130 gen = intel_gen(intel_get_drm_devid(h->fd));
131
132 memset(&h->execbuf, 0, sizeof(h->execbuf));
Chris Wilson4de67b22017-01-02 11:05:21 +0000133 h->execbuf.buffers_ptr = to_user_pointer(&h->obj);
Chris Wilson1a76d882016-10-01 16:50:51 +0100134 h->execbuf.buffer_count = 1;
135
136 memset(&h->obj, 0, sizeof(h->obj));
137 h->obj.handle = gem_create(h->fd, 4096);
138
Chris Wilson4de67b22017-01-02 11:05:21 +0000139 h->obj.relocs_ptr = to_user_pointer(&h->reloc);
Chris Wilson1a76d882016-10-01 16:50:51 +0100140 h->obj.relocation_count = 1;
141 memset(&h->reloc, 0, sizeof(h->reloc));
142
143 batch = gem_mmap__cpu(h->fd, h->obj.handle, 0, 4096, PROT_WRITE);
144 gem_set_domain(h->fd, h->obj.handle,
145 I915_GEM_DOMAIN_CPU, I915_GEM_DOMAIN_CPU);
146
147 h->reloc.target_handle = h->obj.handle; /* recurse */
148 h->reloc.presumed_offset = 0;
149 h->reloc.offset = 5*sizeof(uint32_t);
150 h->reloc.delta = 0;
151 h->reloc.read_domains = I915_GEM_DOMAIN_COMMAND;
152 h->reloc.write_domain = 0;
153
154 i = 0;
155 batch[i++] = 0xffffffff;
156 batch[i++] = 0xdeadbeef;
157 batch[i++] = 0xc00fee00;
158 batch[i++] = 0x00c00fee;
159 batch[i] = MI_BATCH_BUFFER_START;
160 if (gen >= 8) {
161 batch[i] |= 1 << 8 | 1;
162 batch[++i] = 0;
163 batch[++i] = 0;
164 } else if (gen >= 6) {
165 batch[i] |= 1 << 8;
166 batch[++i] = 0;
167 } else {
168 batch[i] |= 2 << 6;
169 batch[++i] = 0;
170 if (gen < 4) {
171 batch[i] |= 1;
172 h->reloc.delta = 1;
173 }
174 }
175 munmap(batch, 4096);
176}
177
Chris Wilson38dc1582017-04-05 17:09:15 +0100178static void submit_hang(struct hang *h, unsigned *engines, int nengine, unsigned flags)
Chris Wilson1a76d882016-10-01 16:50:51 +0100179{
180 while (nengine--) {
181 h->execbuf.flags &= ~ENGINE_MASK;
182 h->execbuf.flags |= *engines++;
183 gem_execbuf(h->fd, &h->execbuf);
184 }
Chris Wilson38dc1582017-04-05 17:09:15 +0100185 if (flags & SYNC)
186 gem_sync(h->fd, h->obj.handle);
Chris Wilson1a76d882016-10-01 16:50:51 +0100187}
188
189static void fini_hang(struct hang *h)
190{
191 close(h->fd);
192}
Chris Wilson5615ab22016-03-11 10:48:47 +0000193
Chris Wilsonc1172132017-09-24 17:14:36 +0100194#define LOCAL_PARAM_HAS_SCHEDULER 41
195#define LOCAL_CONTEXT_PARAM_PRIORITY 6
196
197static bool __has_scheduler(int fd)
198{
199 drm_i915_getparam_t gp;
200 int has = -1;
201
202 gp.param = LOCAL_PARAM_HAS_SCHEDULER;
203 gp.value = &has;
204 drmIoctl(fd, DRM_IOCTL_I915_GETPARAM, &gp);
205
206 return has > 0;
207}
208
209static int __ctx_set_priority(int fd, uint32_t ctx, int prio)
210{
211 struct local_i915_gem_context_param param;
212
213 memset(&param, 0, sizeof(param));
214 param.context = ctx;
215 param.size = 0;
216 param.param = LOCAL_CONTEXT_PARAM_PRIORITY;
217 param.value = prio;
218
219 return __gem_context_set_param(fd, &param);
220}
221
222static void ctx_set_priority(int fd, uint32_t ctx)
223{
224 int prio = hars_petruska_f54_1_random_unsafe_max(1024) - 512;
225 igt_assert_eq(__ctx_set_priority(fd, ctx, prio), 0);
226};
227
Chris Wilson2fdf5ac2016-03-14 13:41:15 +0000228static void whisper(int fd, unsigned engine, unsigned flags)
Chris Wilson5615ab22016-03-11 10:48:47 +0000229{
Chris Wilson3e765842016-07-02 11:51:29 +0100230 const uint32_t bbe = MI_BATCH_BUFFER_END;
Chris Wilson5615ab22016-03-11 10:48:47 +0000231 const int gen = intel_gen(intel_get_drm_devid(fd));
232 struct drm_i915_gem_exec_object2 batches[1024];
233 struct drm_i915_gem_relocation_entry inter[1024];
234 struct drm_i915_gem_relocation_entry reloc;
Chris Wilson8f6ed9e2016-03-12 11:20:03 +0000235 struct drm_i915_gem_exec_object2 store, scratch;
236 struct drm_i915_gem_exec_object2 tmp[2];
237 struct drm_i915_gem_execbuffer2 execbuf;
Chris Wilson1a76d882016-10-01 16:50:51 +0100238 struct hang hang;
Chris Wilson9eacbd22016-03-11 14:31:56 +0000239 int fds[64];
Chris Wilson5615ab22016-03-11 10:48:47 +0000240 uint32_t contexts[64];
Chris Wilson5615ab22016-03-11 10:48:47 +0000241 unsigned engines[16];
242 unsigned nengine;
Chris Wilson5615ab22016-03-11 10:48:47 +0000243 uint32_t batch[16];
244 int i, n, pass, loc;
Chris Wilson5dcb0262016-03-26 09:48:16 +0000245 unsigned int relocations = 0;
Chris Wilson488dc3b2016-03-15 09:16:30 +0000246 unsigned int reloc_migrations = 0;
Chris Wilsond5e550f2016-03-21 11:58:21 +0000247 unsigned int reloc_interruptions = 0;
Chris Wilson488dc3b2016-03-15 09:16:30 +0000248 unsigned int eb_migrations = 0;
Chris Wilson5dcb0262016-03-26 09:48:16 +0000249 uint64_t old_offset;
Chris Wilson58de7852017-03-24 18:11:08 +0000250 int debugfs;
251
Chris Wilsonc1172132017-09-24 17:14:36 +0100252 if (flags & PRIORITY) {
253 int __fd = drm_open_driver(DRIVER_INTEL);
254 bool has_scheduler = __has_scheduler(__fd);
255 bool ctx_has_priority =
256 __ctx_set_priority(__fd, 0, 1) == 0;
257 close(__fd);
258
259 igt_require(has_scheduler && ctx_has_priority);
260 }
261
Chris Wilson58de7852017-03-24 18:11:08 +0000262 debugfs = igt_debugfs_dir(fd);
Chris Wilson5615ab22016-03-11 10:48:47 +0000263
264 nengine = 0;
Chris Wilson2fdf5ac2016-03-14 13:41:15 +0000265 if (engine == -1) {
266 for_each_engine(fd, engine) {
Chris Wilsonbc787762017-05-18 12:11:59 +0100267 if (!ignore_engine(fd, engine))
Chris Wilson2fdf5ac2016-03-14 13:41:15 +0000268 engines[nengine++] = engine;
269 }
270 } else {
271 igt_require(gem_has_ring(fd, engine));
Chris Wilsonbc787762017-05-18 12:11:59 +0100272 igt_require(gem_can_store_dword(fd, engine));
Chris Wilson2fdf5ac2016-03-14 13:41:15 +0000273 engines[nengine++] = engine;
274 }
Chris Wilson5615ab22016-03-11 10:48:47 +0000275 igt_require(nengine);
276
Chris Wilson875c27c2017-04-08 13:54:12 +0100277 if (flags & FDS)
278 igt_require(gen >= 6);
279
Chris Wilsonfe97e032017-03-23 10:48:32 +0000280 if (flags & CONTEXTS) {
281 igt_require(__gem_context_create(fd, &contexts[0]) == 0);
282 gem_context_destroy(fd, contexts[0]);
283 }
284
Chris Wilson1a76d882016-10-01 16:50:51 +0100285 if (flags & HANG)
286 init_hang(&hang);
287
Chris Wilson587753b2016-09-12 16:06:48 +0100288 intel_detect_and_clear_missed_interrupts(fd);
289 igt_fork(child, flags & FORKED ? sysconf(_SC_NPROCESSORS_ONLN) : 1) {
290 memset(&scratch, 0, sizeof(scratch));
291 scratch.handle = gem_create(fd, 4096);
292 scratch.flags = EXEC_OBJECT_WRITE;
Chris Wilson8f6ed9e2016-03-12 11:20:03 +0000293
Chris Wilson587753b2016-09-12 16:06:48 +0100294 memset(&store, 0, sizeof(store));
295 store.handle = gem_create(fd, 4096);
Chris Wilson4de67b22017-01-02 11:05:21 +0000296 store.relocs_ptr = to_user_pointer(&reloc);
Chris Wilson587753b2016-09-12 16:06:48 +0100297 store.relocation_count = 1;
Chris Wilson5615ab22016-03-11 10:48:47 +0000298
Chris Wilson587753b2016-09-12 16:06:48 +0100299 memset(&reloc, 0, sizeof(reloc));
300 reloc.offset = sizeof(uint32_t);
301 if (gen < 8 && gen >= 4)
302 reloc.offset += sizeof(uint32_t);
303 loc = 8;
304 if (gen >= 4)
305 loc += 4;
306 reloc.read_domains = I915_GEM_DOMAIN_INSTRUCTION;
307 reloc.write_domain = I915_GEM_DOMAIN_INSTRUCTION;
Chris Wilson5615ab22016-03-11 10:48:47 +0000308
Chris Wilson587753b2016-09-12 16:06:48 +0100309 {
310 tmp[0] = scratch;
311 tmp[1] = store;
312 gem_write(fd, store.handle, 0, &bbe, sizeof(bbe));
Chris Wilson8f6ed9e2016-03-12 11:20:03 +0000313
Chris Wilson587753b2016-09-12 16:06:48 +0100314 memset(&execbuf, 0, sizeof(execbuf));
Chris Wilson4de67b22017-01-02 11:05:21 +0000315 execbuf.buffers_ptr = to_user_pointer(tmp);
Chris Wilson587753b2016-09-12 16:06:48 +0100316 execbuf.buffer_count = 2;
317 execbuf.flags = LOCAL_I915_EXEC_HANDLE_LUT;
318 execbuf.flags |= LOCAL_I915_EXEC_NO_RELOC;
319 if (gen < 6)
320 execbuf.flags |= I915_EXEC_SECURE;
321 igt_require(__gem_execbuf(fd, &execbuf) == 0);
322 scratch = tmp[0];
323 store = tmp[1];
Chris Wilson3e765842016-07-02 11:51:29 +0100324 }
325
Chris Wilson587753b2016-09-12 16:06:48 +0100326 i = 0;
327 batch[i] = MI_STORE_DWORD_IMM | (gen < 6 ? 1 << 22 : 0);
328 if (gen >= 8) {
329 batch[++i] = store.offset + loc;
330 batch[++i] = (store.offset + loc) >> 32;
331 } else if (gen >= 4) {
332 batch[++i] = 0;
333 batch[++i] = store.offset + loc;
334 } else {
335 batch[i]--;
336 batch[++i] = store.offset + loc;
337 }
338 batch[++i] = 0xc0ffee;
339 igt_assert(loc == sizeof(uint32_t) * i);
340 batch[++i] = MI_BATCH_BUFFER_END;
Chris Wilson3e765842016-07-02 11:51:29 +0100341
Chris Wilson587753b2016-09-12 16:06:48 +0100342 if (flags & CONTEXTS) {
Chris Wilsonfe97e032017-03-23 10:48:32 +0000343 for (n = 0; n < 64; n++)
Chris Wilson587753b2016-09-12 16:06:48 +0100344 contexts[n] = gem_context_create(fd);
345 }
346 if (flags & FDS) {
Chris Wilson587753b2016-09-12 16:06:48 +0100347 for (n = 0; n < 64; n++)
348 fds[n] = drm_open_driver(DRIVER_INTEL);
349 }
Chris Wilson5615ab22016-03-11 10:48:47 +0000350
Chris Wilson587753b2016-09-12 16:06:48 +0100351 memset(batches, 0, sizeof(batches));
352 for (n = 0; n < 1024; n++) {
353 batches[n].handle = gem_create(fd, 4096);
354 gem_write(fd, batches[n].handle, 0, &bbe, sizeof(bbe));
355 }
Chris Wilson4de67b22017-01-02 11:05:21 +0000356 execbuf.buffers_ptr = to_user_pointer(batches);
Chris Wilson587753b2016-09-12 16:06:48 +0100357 execbuf.buffer_count = 1024;
358 gem_execbuf(fd, &execbuf);
Chris Wilson5615ab22016-03-11 10:48:47 +0000359
Chris Wilson4de67b22017-01-02 11:05:21 +0000360 execbuf.buffers_ptr = to_user_pointer(tmp);
Chris Wilson587753b2016-09-12 16:06:48 +0100361 execbuf.buffer_count = 2;
Chris Wilson5615ab22016-03-11 10:48:47 +0000362
Chris Wilson587753b2016-09-12 16:06:48 +0100363 old_offset = store.offset;
364 for (n = 0; n < 1024; n++) {
Chris Wilson83cfed82016-03-19 14:21:34 +0000365 if (gen >= 8) {
Chris Wilson587753b2016-09-12 16:06:48 +0100366 batch[1] = old_offset + loc;
367 batch[2] = (old_offset + loc) >> 32;
Chris Wilson83cfed82016-03-19 14:21:34 +0000368 } else if (gen >= 4) {
Chris Wilson587753b2016-09-12 16:06:48 +0100369 batch[2] = old_offset + loc;
Chris Wilson83cfed82016-03-19 14:21:34 +0000370 } else {
Chris Wilson587753b2016-09-12 16:06:48 +0100371 batch[1] = old_offset + loc;
Chris Wilson9eacbd22016-03-11 14:31:56 +0000372 }
373
Chris Wilson587753b2016-09-12 16:06:48 +0100374 inter[n] = reloc;
375 inter[n].presumed_offset = old_offset;
376 inter[n].delta = loc;
Chris Wilson4de67b22017-01-02 11:05:21 +0000377 batches[n].relocs_ptr = to_user_pointer(&inter[n]);
Chris Wilson587753b2016-09-12 16:06:48 +0100378 batches[n].relocation_count = 1;
379 gem_write(fd, batches[n].handle, 0, batch, sizeof(batch));
Chris Wilson83cfed82016-03-19 14:21:34 +0000380
Chris Wilson587753b2016-09-12 16:06:48 +0100381 old_offset = batches[n].offset;
382 }
Chris Wilson83cfed82016-03-19 14:21:34 +0000383
Chris Wilson587753b2016-09-12 16:06:48 +0100384 igt_while_interruptible(flags & INTERRUPTIBLE) {
385 for (pass = 0; pass < 1024; pass++) {
386 uint64_t offset;
Chris Wilson83cfed82016-03-19 14:21:34 +0000387
Chris Wilson125e27e2016-10-05 17:25:20 +0100388 if (!(flags & FORKED))
Chris Wilson58de7852017-03-24 18:11:08 +0000389 write_seqno(debugfs, pass);
Chris Wilson83cfed82016-03-19 14:21:34 +0000390
Chris Wilson1a76d882016-10-01 16:50:51 +0100391 if (flags & HANG)
Chris Wilson38dc1582017-04-05 17:09:15 +0100392 submit_hang(&hang, engines, nengine, flags);
Chris Wilson1a76d882016-10-01 16:50:51 +0100393
Chris Wilson587753b2016-09-12 16:06:48 +0100394 if (flags & CHAIN) {
Chris Wilson7b34c512016-06-04 16:12:28 +0100395 execbuf.flags &= ~ENGINE_MASK;
396 execbuf.flags |= engines[rand() % nengine];
397 }
Chris Wilson587753b2016-09-12 16:06:48 +0100398
399 reloc.presumed_offset = scratch.offset;
400 reloc.delta = 4*pass;
401 offset = reloc.presumed_offset + reloc.delta;
402
403 i = 0;
404 if (gen >= 8) {
405 batch[++i] = offset;
406 batch[++i] = offset >> 32;
407 } else if (gen >= 4) {
408 batch[++i] = 0;
409 batch[++i] = offset;
410 } else {
411 batch[++i] = offset;
412 }
413 batch[++i] = ~pass;
414 gem_write(fd, store.handle, 0, batch, sizeof(batch));
415
416 tmp[0] = scratch;
417 igt_assert(tmp[0].flags & EXEC_OBJECT_WRITE);
418 tmp[1] = store;
419 verify_reloc(fd, store.handle, &reloc);
Chris Wilson4de67b22017-01-02 11:05:21 +0000420 execbuf.buffers_ptr = to_user_pointer(tmp);
Chris Wilson587753b2016-09-12 16:06:48 +0100421 gem_execbuf(fd, &execbuf);
422 igt_assert_eq_u64(reloc.presumed_offset, tmp[0].offset);
Chris Wilson38dc1582017-04-05 17:09:15 +0100423 if (flags & SYNC)
424 gem_sync(fd, tmp[0].handle);
Chris Wilson587753b2016-09-12 16:06:48 +0100425 scratch = tmp[0];
426
427 gem_write(fd, batches[1023].handle, loc, &pass, sizeof(pass));
428 for (n = 1024; --n >= 1; ) {
429 int this_fd = fd;
430 uint32_t handle[2];
431
Chris Wilson4de67b22017-01-02 11:05:21 +0000432 execbuf.buffers_ptr = to_user_pointer(&batches[n-1]);
Chris Wilson587753b2016-09-12 16:06:48 +0100433 reloc_migrations += batches[n-1].offset != inter[n].presumed_offset;
434 batches[n-1].offset = inter[n].presumed_offset;
435 old_offset = inter[n].presumed_offset;
436 batches[n-1].relocation_count = 0;
437 batches[n-1].flags |= EXEC_OBJECT_WRITE;
438 verify_reloc(fd, batches[n].handle, &inter[n]);
439
440 if (flags & FDS) {
441 this_fd = fds[rand() % 64];
442 handle[0] = batches[n-1].handle;
443 handle[1] = batches[n].handle;
444 batches[n-1].handle =
445 gem_open(this_fd,
446 gem_flink(fd, handle[0]));
447 batches[n].handle =
448 gem_open(this_fd,
449 gem_flink(fd, handle[1]));
Chris Wilsonc1172132017-09-24 17:14:36 +0100450 if (flags & PRIORITY)
451 ctx_set_priority(this_fd, 0);
Chris Wilson587753b2016-09-12 16:06:48 +0100452 }
453
454 if (!(flags & CHAIN)) {
455 execbuf.flags &= ~ENGINE_MASK;
456 execbuf.flags |= engines[rand() % nengine];
457 }
Chris Wilsonc1172132017-09-24 17:14:36 +0100458 if (flags & CONTEXTS) {
Chris Wilson587753b2016-09-12 16:06:48 +0100459 execbuf.rsvd1 = contexts[rand() % 64];
Chris Wilsonc1172132017-09-24 17:14:36 +0100460 if (flags & PRIORITY)
461 ctx_set_priority(this_fd, execbuf.rsvd1);
462 }
463
Chris Wilson587753b2016-09-12 16:06:48 +0100464 gem_execbuf(this_fd, &execbuf);
465 if (inter[n].presumed_offset == -1) {
466 reloc_interruptions++;
467 inter[n].presumed_offset = batches[n-1].offset;
468 }
469 igt_assert_eq_u64(inter[n].presumed_offset, batches[n-1].offset);
Chris Wilson38dc1582017-04-05 17:09:15 +0100470
471 if (flags & SYNC)
472 gem_sync(this_fd, batches[n-1].handle);
Chris Wilson587753b2016-09-12 16:06:48 +0100473 relocations += inter[n].presumed_offset != old_offset;
474
475 batches[n-1].relocation_count = 1;
476 batches[n-1].flags &= ~EXEC_OBJECT_WRITE;
477
478 if (this_fd != fd) {
479 gem_close(this_fd, batches[n-1].handle);
480 batches[n-1].handle = handle[0];
481
482 gem_close(this_fd, batches[n].handle);
483 batches[n].handle = handle[1];
484 }
485 }
486 execbuf.flags &= ~ENGINE_MASK;
487 execbuf.rsvd1 = 0;
Chris Wilson4de67b22017-01-02 11:05:21 +0000488 execbuf.buffers_ptr = to_user_pointer(&tmp);
Chris Wilson587753b2016-09-12 16:06:48 +0100489
490 tmp[0] = tmp[1];
491 tmp[0].relocation_count = 0;
492 tmp[0].flags = EXEC_OBJECT_WRITE;
493 reloc_migrations += tmp[0].offset != inter[0].presumed_offset;
494 tmp[0].offset = inter[0].presumed_offset;
495 old_offset = tmp[0].offset;
496 tmp[1] = batches[0];
497 verify_reloc(fd, batches[0].handle, &inter[0]);
498 gem_execbuf(fd, &execbuf);
499 if (inter[0].presumed_offset == -1) {
Chris Wilson5dcb0262016-03-26 09:48:16 +0000500 reloc_interruptions++;
Chris Wilson587753b2016-09-12 16:06:48 +0100501 inter[0].presumed_offset = tmp[0].offset;
Chris Wilson5dcb0262016-03-26 09:48:16 +0000502 }
Chris Wilson587753b2016-09-12 16:06:48 +0100503 igt_assert_eq_u64(inter[0].presumed_offset, tmp[0].offset);
504 relocations += inter[0].presumed_offset != old_offset;
505 batches[0] = tmp[1];
Chris Wilson83cfed82016-03-19 14:21:34 +0000506
Chris Wilson587753b2016-09-12 16:06:48 +0100507 tmp[1] = tmp[0];
508 tmp[0] = scratch;
509 igt_assert(tmp[0].flags & EXEC_OBJECT_WRITE);
510 igt_assert_eq_u64(reloc.presumed_offset, tmp[0].offset);
Chris Wilson4de67b22017-01-02 11:05:21 +0000511 igt_assert(tmp[1].relocs_ptr == to_user_pointer(&reloc));
Chris Wilson587753b2016-09-12 16:06:48 +0100512 tmp[1].relocation_count = 1;
513 tmp[1].flags &= ~EXEC_OBJECT_WRITE;
514 verify_reloc(fd, store.handle, &reloc);
515 gem_execbuf(fd, &execbuf);
516 eb_migrations += tmp[0].offset != scratch.offset;
517 eb_migrations += tmp[1].offset != store.offset;
518 igt_assert_eq_u64(reloc.presumed_offset, tmp[0].offset);
Chris Wilson38dc1582017-04-05 17:09:15 +0100519 if (flags & SYNC)
520 gem_sync(fd, tmp[0].handle);
521
Chris Wilson587753b2016-09-12 16:06:48 +0100522 store = tmp[1];
523 scratch = tmp[0];
Chris Wilson83cfed82016-03-19 14:21:34 +0000524 }
Chris Wilson62916782016-03-11 14:16:13 +0000525 }
Chris Wilson587753b2016-09-12 16:06:48 +0100526 igt_info("Number of migrations for execbuf: %d\n", eb_migrations);
527 igt_info("Number of migrations for reloc: %d, interrupted %d, patched %d\n", reloc_migrations, reloc_interruptions, relocations);
Chris Wilson5615ab22016-03-11 10:48:47 +0000528
Chris Wilson587753b2016-09-12 16:06:48 +0100529 check_bo(fd, scratch.handle);
530 gem_close(fd, scratch.handle);
531 gem_close(fd, store.handle);
532
533 if (flags & FDS) {
534 for (n = 0; n < 64; n++)
535 close(fds[n]);
536 }
537 if (flags & CONTEXTS) {
538 for (n = 0; n < 64; n++)
539 gem_context_destroy(fd, contexts[n]);
540 }
541 for (n = 0; n < 1024; n++)
542 gem_close(fd, batches[n].handle);
543 }
544
545 igt_waitchildren();
Chris Wilson1a76d882016-10-01 16:50:51 +0100546
547 if (flags & HANG)
548 fini_hang(&hang);
549 else
550 igt_assert_eq(intel_detect_and_clear_missed_interrupts(fd), 0);
Chris Wilson58de7852017-03-24 18:11:08 +0000551
552 close(debugfs);
Chris Wilson5615ab22016-03-11 10:48:47 +0000553}
554
Chris Wilsonc6e26e42016-07-22 12:58:54 +0100555static void print_welcome(int fd)
556{
557 bool active;
Chris Wilsonc6e26e42016-07-22 12:58:54 +0100558 int dir;
559
560 dir = igt_sysfs_open_parameters(fd);
561 if (dir < 0)
562 return;
563
Chris Wilsonb64d10c2016-07-22 17:53:51 +0100564 active = igt_sysfs_get_boolean(dir, "enable_guc_submission");
Chris Wilsonc6e26e42016-07-22 12:58:54 +0100565 if (active) {
566 igt_info("Using GuC submission\n");
567 goto out;
568 }
569
Chris Wilsonb64d10c2016-07-22 17:53:51 +0100570 active = igt_sysfs_get_boolean(dir, "enable_execlists");
Chris Wilsonc6e26e42016-07-22 12:58:54 +0100571 if (active) {
572 igt_info("Using Execlists submission\n");
573 goto out;
574 }
575
Chris Wilsonb64d10c2016-07-22 17:53:51 +0100576 active = igt_sysfs_get_boolean(dir, "semaphores");
Chris Wilsonf565b6c2016-09-08 20:59:55 +0100577 igt_info("Using Legacy submission%s\n",
Chris Wilsonc6e26e42016-07-22 12:58:54 +0100578 active ? ", with semaphores" : "");
579
580out:
581 close(dir);
582}
583
Chris Wilson5615ab22016-03-11 10:48:47 +0000584igt_main
585{
Chris Wilson83cfed82016-03-19 14:21:34 +0000586 const struct mode {
587 const char *name;
588 unsigned flags;
589 } modes[] = {
Chris Wilson2b4c35e2016-05-20 11:20:49 +0100590 { "normal", 0 },
Chris Wilson1a722fa2016-03-26 10:54:43 +0000591 { "interruptible", INTERRUPTIBLE },
Chris Wilson587753b2016-09-12 16:06:48 +0100592 { "forked", FORKED },
Chris Wilson38dc1582017-04-05 17:09:15 +0100593 { "sync", SYNC },
Chris Wilson7b34c512016-06-04 16:12:28 +0100594 { "chain", CHAIN },
Chris Wilson587753b2016-09-12 16:06:48 +0100595 { "chain-forked", CHAIN | FORKED },
596 { "chain-interruptible", CHAIN | INTERRUPTIBLE },
Chris Wilson38dc1582017-04-05 17:09:15 +0100597 { "chain-sync", CHAIN | SYNC },
Chris Wilson83cfed82016-03-19 14:21:34 +0000598 { "contexts", CONTEXTS },
599 { "contexts-interruptible", CONTEXTS | INTERRUPTIBLE},
Chris Wilson587753b2016-09-12 16:06:48 +0100600 { "contexts-forked", CONTEXTS | FORKED},
Chris Wilsonc1172132017-09-24 17:14:36 +0100601 { "contexts-priority", CONTEXTS | FORKED | PRIORITY },
Chris Wilson7b34c512016-06-04 16:12:28 +0100602 { "contexts-chain", CONTEXTS | CHAIN },
Chris Wilson38dc1582017-04-05 17:09:15 +0100603 { "contexts-sync", CONTEXTS | SYNC },
Chris Wilson83cfed82016-03-19 14:21:34 +0000604 { "fds", FDS },
605 { "fds-interruptible", FDS | INTERRUPTIBLE},
Chris Wilson587753b2016-09-12 16:06:48 +0100606 { "fds-forked", FDS | FORKED},
Chris Wilsonc1172132017-09-24 17:14:36 +0100607 { "fds-priority", FDS | FORKED | PRIORITY },
Chris Wilson7b34c512016-06-04 16:12:28 +0100608 { "fds-chain", FDS | CHAIN},
Chris Wilson38dc1582017-04-05 17:09:15 +0100609 { "fds-sync", FDS | SYNC},
Chris Wilson83cfed82016-03-19 14:21:34 +0000610 { NULL }
611 };
Chris Wilson5615ab22016-03-11 10:48:47 +0000612 int fd;
613
Daniel Vetterbe21fc02016-06-17 16:04:09 +0200614 igt_fixture {
Chris Wilson5615ab22016-03-11 10:48:47 +0000615 fd = drm_open_driver_master(DRIVER_INTEL);
Chris Wilson9518cb52017-02-22 15:24:54 +0000616 igt_require_gem(fd);
Chris Wilsonbc787762017-05-18 12:11:59 +0100617 igt_require(gem_can_store_dword(fd, 0));
Chris Wilsonc6e26e42016-07-22 12:58:54 +0100618 print_welcome(fd);
Chris Wilson5615ab22016-03-11 10:48:47 +0000619
Daniel Vetterbe21fc02016-06-17 16:04:09 +0200620 igt_fork_hang_detector(fd);
621 }
Chris Wilson756f3e02016-03-22 11:33:41 +0000622
Chris Wilson83cfed82016-03-19 14:21:34 +0000623 for (const struct mode *m = modes; m->name; m++)
Chris Wilson2b4c35e2016-05-20 11:20:49 +0100624 igt_subtest_f("%s", m->name)
Chris Wilson83cfed82016-03-19 14:21:34 +0000625 whisper(fd, -1, m->flags);
Chris Wilson2fdf5ac2016-03-14 13:41:15 +0000626
627 for (const struct intel_execution_engine *e = intel_execution_engines;
628 e->name; e++) {
Chris Wilson7b34c512016-06-04 16:12:28 +0100629 for (const struct mode *m = modes; m->name; m++) {
630 if (m->flags & CHAIN)
631 continue;
632
Chris Wilson2b4c35e2016-05-20 11:20:49 +0100633 igt_subtest_f("%s-%s", e->name, m->name)
Chris Wilson83cfed82016-03-19 14:21:34 +0000634 whisper(fd, e->exec_id | e->flags, m->flags);
Chris Wilson7b34c512016-06-04 16:12:28 +0100635 }
Chris Wilson2fdf5ac2016-03-14 13:41:15 +0000636 }
Chris Wilson9eacbd22016-03-11 14:31:56 +0000637
Daniel Vetterbe21fc02016-06-17 16:04:09 +0200638 igt_fixture {
639 igt_stop_hang_detector();
Chris Wilson1a76d882016-10-01 16:50:51 +0100640 }
641
642 igt_subtest_group {
643 for (const struct mode *m = modes; m->name; m++) {
644 if (m->flags & INTERRUPTIBLE)
645 continue;
646 igt_subtest_f("hang-%s", m->name)
647 whisper(fd, -1, m->flags | HANG);
648 }
649 }
650
651 igt_fixture {
Chris Wilson5615ab22016-03-11 10:48:47 +0000652 close(fd);
Daniel Vetterbe21fc02016-06-17 16:04:09 +0200653 }
Chris Wilson5615ab22016-03-11 10:48:47 +0000654}