blob: 440e35718c7be176f8c1f104d9421f75713cb09f [file] [log] [blame]
Chris Wilson07d59b32011-01-20 22:10:10 +00001/*
2 * Copyright © 2011 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 * Authors:
24 * Chris Wilson <chris@chris-wilson.co.uk>
25 *
26 */
27
Thomas Wood804e11f2015-08-17 17:57:43 +010028#include "igt.h"
Chris Wilson9fbf72b2017-01-22 18:24:37 +000029#include "igt_rand.h"
Chris Wilsonc6e26e42016-07-22 12:58:54 +010030#include "igt_sysfs.h"
Chris Wilson9fbf72b2017-01-22 18:24:37 +000031
Chris Wilson07d59b32011-01-20 22:10:10 +000032#include <unistd.h>
33#include <stdlib.h>
34#include <stdint.h>
35#include <stdio.h>
36#include <string.h>
Chris Wilson07d59b32011-01-20 22:10:10 +000037#include <fcntl.h>
38#include <inttypes.h>
39#include <errno.h>
40#include <sys/stat.h>
41#include <sys/ioctl.h>
Chris Wilsone10c48d2017-05-12 12:33:32 +010042#include <sys/poll.h>
Chris Wilson07d59b32011-01-20 22:10:10 +000043#include <sys/time.h>
Chris Wilsonb4307092015-07-01 13:53:07 +010044#include <time.h>
Chris Wilson07d59b32011-01-20 22:10:10 +000045#include "drm.h"
Chris Wilson07d59b32011-01-20 22:10:10 +000046
Chris Wilsoncd8d3802015-03-24 09:15:12 +000047#define LOCAL_I915_EXEC_NO_RELOC (1<<11)
48#define LOCAL_I915_EXEC_HANDLE_LUT (1<<12)
49
Chris Wilson3a7325e2016-03-08 11:43:31 +000050#define LOCAL_I915_EXEC_BSD_SHIFT (13)
51#define LOCAL_I915_EXEC_BSD_MASK (3 << LOCAL_I915_EXEC_BSD_SHIFT)
Daniel Vetter51f08302012-12-05 19:29:11 +010052
Chris Wilson3a7325e2016-03-08 11:43:31 +000053#define ENGINE_FLAGS (I915_EXEC_RING_MASK | LOCAL_I915_EXEC_BSD_MASK)
Chris Wilson2659cbb2015-03-26 12:09:57 +000054
Chris Wilson9fbf72b2017-01-22 18:24:37 +000055#define FORKED 1
56#define CHAINED 2
57#define CONTEXT 4
58
Chris Wilson3a7325e2016-03-08 11:43:31 +000059static double elapsed(const struct timespec *start, const struct timespec *end)
60{
61 return ((end->tv_sec - start->tv_sec) +
62 (end->tv_nsec - start->tv_nsec)*1e-9);
63}
64
Chris Wilson870c7742016-03-28 15:29:46 +010065static double nop_on_ring(int fd, uint32_t handle, unsigned ring_id,
66 int timeout, unsigned long *out)
Chris Wilson07d59b32011-01-20 22:10:10 +000067{
68 struct drm_i915_gem_execbuffer2 execbuf;
Chris Wilson3a7325e2016-03-08 11:43:31 +000069 struct drm_i915_gem_exec_object2 obj;
70 struct timespec start, now;
Chris Wilson870c7742016-03-28 15:29:46 +010071 unsigned long count;
Daniel Vetter8f5387e2013-08-13 13:20:58 +020072
Chris Wilson3a7325e2016-03-08 11:43:31 +000073 memset(&obj, 0, sizeof(obj));
74 obj.handle = handle;
Chris Wilson07d59b32011-01-20 22:10:10 +000075
Chris Wilsoncd8d3802015-03-24 09:15:12 +000076 memset(&execbuf, 0, sizeof(execbuf));
Chris Wilson4de67b22017-01-02 11:05:21 +000077 execbuf.buffers_ptr = to_user_pointer(&obj);
Chris Wilsoncd8d3802015-03-24 09:15:12 +000078 execbuf.buffer_count = 1;
79 execbuf.flags = ring_id;
80 execbuf.flags |= LOCAL_I915_EXEC_HANDLE_LUT;
81 execbuf.flags |= LOCAL_I915_EXEC_NO_RELOC;
Chris Wilson3e2443f2016-03-10 11:50:53 +000082 if (__gem_execbuf(fd, &execbuf)) {
Chris Wilsoncd8d3802015-03-24 09:15:12 +000083 execbuf.flags = ring_id;
Chris Wilson3a7325e2016-03-08 11:43:31 +000084 gem_execbuf(fd, &execbuf);
Chris Wilsoncd8d3802015-03-24 09:15:12 +000085 }
Chris Wilson71f41532016-04-22 16:55:29 +010086 intel_detect_and_clear_missed_interrupts(fd);
Chris Wilsoncd8d3802015-03-24 09:15:12 +000087
Chris Wilson870c7742016-03-28 15:29:46 +010088 count = 0;
Chris Wilson3a7325e2016-03-08 11:43:31 +000089 clock_gettime(CLOCK_MONOTONIC, &start);
90 do {
Chris Wilson870c7742016-03-28 15:29:46 +010091 for (int loop = 0; loop < 1024; loop++)
Chris Wilson3a7325e2016-03-08 11:43:31 +000092 gem_execbuf(fd, &execbuf);
Chris Wilson870c7742016-03-28 15:29:46 +010093
94 count += 1024;
Chris Wilson3a7325e2016-03-08 11:43:31 +000095 clock_gettime(CLOCK_MONOTONIC, &now);
Chris Wilson870c7742016-03-28 15:29:46 +010096 } while (elapsed(&start, &now) < timeout);
Chris Wilson71f41532016-04-22 16:55:29 +010097 igt_assert_eq(intel_detect_and_clear_missed_interrupts(fd), 0);
Chris Wilson3a7325e2016-03-08 11:43:31 +000098
Chris Wilson870c7742016-03-28 15:29:46 +010099 *out = count;
100 return elapsed(&start, &now);
101}
102
103static void single(int fd, uint32_t handle,
104 unsigned ring_id, const char *ring_name)
105{
106 double time;
107 unsigned long count;
108
109 gem_require_ring(fd, ring_id);
110
111 time = nop_on_ring(fd, handle, ring_id, 20, &count);
112 igt_info("%s: %'lu cycles: %.3fus\n",
113 ring_name, count, time*1e6 / count);
Chris Wilson3a7325e2016-03-08 11:43:31 +0000114}
115
Tvrtko Ursulin8e4cfb22017-04-13 14:11:05 +0100116static double
117stable_nop_on_ring(int fd, uint32_t handle, unsigned int engine,
118 int timeout, int reps)
119{
120 igt_stats_t s;
121 double n;
122
123 igt_assert(reps >= 5);
124
125 igt_stats_init_with_size(&s, reps);
126 s.is_float = true;
127
128 while (reps--) {
129 unsigned long count;
130 double time;
131
132 time = nop_on_ring(fd, handle, engine, timeout, &count);
133 igt_stats_push_float(&s, time / count);
134 }
135
136 n = igt_stats_get_median(&s);
137 igt_stats_fini(&s);
138
139 return n;
140}
141
142#define assert_within_epsilon(x, ref, tolerance) \
143 igt_assert_f((x) <= (1.0 + tolerance) * ref && \
144 (x) >= (1.0 - tolerance) * ref, \
145 "'%s' != '%s' (%f not within %f%% tolerance of %f)\n",\
146 #x, #ref, x, tolerance * 100.0, ref)
147
148static void headless(int fd, uint32_t handle)
149{
150 unsigned int nr_connected = 0;
151 drmModeConnector *connector;
152 drmModeRes *res;
153 double n_display, n_headless;
154
155 res = drmModeGetResources(fd);
156 igt_assert(res);
157
158 /* require at least one connected connector for the test */
159 for (int i = 0; i < res->count_connectors; i++) {
160 connector = drmModeGetConnectorCurrent(fd, res->connectors[i]);
161 if (connector->connection == DRM_MODE_CONNECTED)
162 nr_connected++;
163 drmModeFreeConnector(connector);
164 }
165 igt_require(nr_connected > 0);
166
167 /* set graphics mode to prevent blanking */
168 kmstest_set_vt_graphics_mode();
169
170 /* benchmark nops */
171 n_display = stable_nop_on_ring(fd, handle, I915_EXEC_DEFAULT, 1, 5);
Chris Wilsonf6a31ad2017-04-19 13:38:32 +0100172 igt_info("With one display connected: %.2fus\n",
173 n_display * 1e6);
Tvrtko Ursulin8e4cfb22017-04-13 14:11:05 +0100174
175 /* force all connectors off */
176 kmstest_unset_all_crtcs(fd, res);
177
178 /* benchmark nops again */
179 n_headless = stable_nop_on_ring(fd, handle, I915_EXEC_DEFAULT, 1, 5);
Chris Wilsonf6a31ad2017-04-19 13:38:32 +0100180 igt_info("Without a display connected (headless): %.2fus\n",
181 n_headless * 1e6);
Tvrtko Ursulin8e4cfb22017-04-13 14:11:05 +0100182
183 /* check that the two execution speeds are roughly the same */
184 assert_within_epsilon(n_headless, n_display, 0.1f);
185}
186
Chris Wilson0aacdac2016-03-09 21:06:16 +0000187static bool ignore_engine(int fd, unsigned engine)
188{
189 if (engine == 0)
190 return true;
191
192 if (gem_has_bsd2(fd) && engine == I915_EXEC_BSD)
193 return true;
194
195 return false;
196}
197
Chris Wilson4cce7152016-09-08 13:43:17 +0100198static void parallel(int fd, uint32_t handle, int timeout)
199{
200 struct drm_i915_gem_execbuffer2 execbuf;
201 struct drm_i915_gem_exec_object2 obj;
Chris Wilson4cce7152016-09-08 13:43:17 +0100202 unsigned engines[16];
203 const char *names[16];
204 unsigned nengine;
205 unsigned engine;
206 unsigned long count;
207 double time, sum;
208
209 sum = 0;
210 nengine = 0;
211 for_each_engine(fd, engine) {
212 if (ignore_engine(fd, engine))
213 continue;
214
215 engines[nengine] = engine;
216 names[nengine] = e__->name;
217 nengine++;
218
219 time = nop_on_ring(fd, handle, engine, 1, &count) / count;
220 sum += time;
221 igt_debug("%s: %.3fus\n", e__->name, 1e6*time);
222 }
223 igt_require(nengine);
224 igt_info("average (individually): %.3fus\n", sum/nengine*1e6);
225
226 memset(&obj, 0, sizeof(obj));
227 obj.handle = handle;
228
229 memset(&execbuf, 0, sizeof(execbuf));
Chris Wilson4de67b22017-01-02 11:05:21 +0000230 execbuf.buffers_ptr = to_user_pointer(&obj);
Chris Wilson4cce7152016-09-08 13:43:17 +0100231 execbuf.buffer_count = 1;
232 execbuf.flags |= LOCAL_I915_EXEC_HANDLE_LUT;
233 execbuf.flags |= LOCAL_I915_EXEC_NO_RELOC;
234 if (__gem_execbuf(fd, &execbuf)) {
235 execbuf.flags = 0;
236 gem_execbuf(fd, &execbuf);
237 }
Chris Wilson4cce7152016-09-08 13:43:17 +0100238 intel_detect_and_clear_missed_interrupts(fd);
239
240 igt_fork(child, nengine) {
Chris Wilsonf565b6c2016-09-08 20:59:55 +0100241 struct timespec start, now;
242
Chris Wilson4cce7152016-09-08 13:43:17 +0100243 execbuf.flags &= ~ENGINE_FLAGS;
244 execbuf.flags |= engines[child];
245
246 count = 0;
247 clock_gettime(CLOCK_MONOTONIC, &start);
248 do {
249 for (int loop = 0; loop < 1024; loop++)
250 gem_execbuf(fd, &execbuf);
251 count += 1024;
252 clock_gettime(CLOCK_MONOTONIC, &now);
253 } while (elapsed(&start, &now) < timeout);
Chris Wilson4cce7152016-09-08 13:43:17 +0100254 time = elapsed(&start, &now) / count;
255 igt_info("%s: %ld cycles, %.3fus\n", names[child], count, 1e6*time);
256 }
257
258 igt_waitchildren();
259 igt_assert_eq(intel_detect_and_clear_missed_interrupts(fd), 0);
260
261}
262
263static void series(int fd, uint32_t handle, int timeout)
Chris Wilson3a7325e2016-03-08 11:43:31 +0000264{
265 struct drm_i915_gem_execbuffer2 execbuf;
266 struct drm_i915_gem_exec_object2 obj;
Chris Wilsonf565b6c2016-09-08 20:59:55 +0100267 struct timespec start, now, sync;
Chris Wilson3a7325e2016-03-08 11:43:31 +0000268 unsigned engines[16];
269 unsigned nengine;
270 unsigned engine;
Chris Wilson870c7742016-03-28 15:29:46 +0100271 unsigned long count;
Chris Wilson41a26b52016-03-28 16:26:01 +0100272 double time, max = 0, min = HUGE_VAL, sum = 0;
Chris Wilson870c7742016-03-28 15:29:46 +0100273 const char *name;
Chris Wilson3a7325e2016-03-08 11:43:31 +0000274
275 nengine = 0;
Chris Wilson870c7742016-03-28 15:29:46 +0100276 for_each_engine(fd, engine) {
277 if (ignore_engine(fd, engine))
278 continue;
279
280 time = nop_on_ring(fd, handle, engine, 1, &count) / count;
281 if (time > max) {
282 name = e__->name;
283 max = time;
284 }
Chris Wilson41a26b52016-03-28 16:26:01 +0100285 if (time < min)
286 min = time;
Chris Wilson870c7742016-03-28 15:29:46 +0100287 sum += time;
288 engines[nengine++] = engine;
289 }
Chris Wilson0aacdac2016-03-09 21:06:16 +0000290 igt_require(nengine);
Chris Wilsonf565b6c2016-09-08 20:59:55 +0100291 igt_info("Maximum execution latency on %s, %.3fus, min %.3fus, total %.3fus per cycle, average %.3fus\n",
292 name, max*1e6, min*1e6, sum*1e6, sum/nengine*1e6);
Chris Wilson3a7325e2016-03-08 11:43:31 +0000293
294 memset(&obj, 0, sizeof(obj));
295 obj.handle = handle;
296
297 memset(&execbuf, 0, sizeof(execbuf));
Chris Wilson4de67b22017-01-02 11:05:21 +0000298 execbuf.buffers_ptr = to_user_pointer(&obj);
Chris Wilson3a7325e2016-03-08 11:43:31 +0000299 execbuf.buffer_count = 1;
300 execbuf.flags |= LOCAL_I915_EXEC_HANDLE_LUT;
301 execbuf.flags |= LOCAL_I915_EXEC_NO_RELOC;
Chris Wilson3e2443f2016-03-10 11:50:53 +0000302 if (__gem_execbuf(fd, &execbuf)) {
Chris Wilson3a7325e2016-03-08 11:43:31 +0000303 execbuf.flags = 0;
304 gem_execbuf(fd, &execbuf);
Chris Wilson07d59b32011-01-20 22:10:10 +0000305 }
Chris Wilson71f41532016-04-22 16:55:29 +0100306 intel_detect_and_clear_missed_interrupts(fd);
Chris Wilson3a7325e2016-03-08 11:43:31 +0000307
Chris Wilson870c7742016-03-28 15:29:46 +0100308 count = 0;
Chris Wilson3a7325e2016-03-08 11:43:31 +0000309 clock_gettime(CLOCK_MONOTONIC, &start);
310 do {
311 for (int loop = 0; loop < 1024; loop++) {
312 for (int n = 0; n < nengine; n++) {
313 execbuf.flags &= ~ENGINE_FLAGS;
314 execbuf.flags |= engines[n];
315 gem_execbuf(fd, &execbuf);
316 }
317 }
318 count += nengine * 1024;
319 clock_gettime(CLOCK_MONOTONIC, &now);
Chris Wilson772393e2016-03-14 14:31:36 +0000320 } while (elapsed(&start, &now) < timeout); /* Hang detection ~120s */
Chris Wilson3a7325e2016-03-08 11:43:31 +0000321 gem_sync(fd, handle);
Chris Wilsonf565b6c2016-09-08 20:59:55 +0100322 clock_gettime(CLOCK_MONOTONIC, &sync);
323 igt_debug("sync time: %.3fus\n", elapsed(&now, &sync)*1e6);
Chris Wilson71f41532016-04-22 16:55:29 +0100324 igt_assert_eq(intel_detect_and_clear_missed_interrupts(fd), 0);
Chris Wilson3a7325e2016-03-08 11:43:31 +0000325
Chris Wilson870c7742016-03-28 15:29:46 +0100326 time = elapsed(&start, &now) / count;
Chris Wilsonf565b6c2016-09-08 20:59:55 +0100327 igt_info("All (%d engines): %'lu cycles, average %.3fus per cycle [expected %.3fus]\n",
Chris Wilson61b19a42016-09-08 14:50:32 +0100328 nengine, count, 1e6*time, 1e6*((max-min)/nengine+min));
Chris Wilson41a26b52016-03-28 16:26:01 +0100329
Chris Wilsona0eebbd2016-09-08 13:29:31 +0100330 /* The rate limiting step should be how fast the slowest engine can
331 * execute its queue of requests, as when we wait upon a full ring all
332 * dispatch is frozen. So in general we cannot go faster than the
333 * slowest engine (but as all engines are in lockstep, they should all
334 * be executing in parallel and so the average should be max/nengines),
335 * but we should equally not go any slower.
336 *
337 * However, that depends upon being able to submit fast enough, and
338 * that in turns depends upon debugging turned off and no bottlenecks
339 * within the driver. We cannot assert that we hit ideal conditions
340 * across all engines, so we only look for an outrageous error
341 * condition.
Chris Wilson41a26b52016-03-28 16:26:01 +0100342 */
Chris Wilsona0eebbd2016-09-08 13:29:31 +0100343 igt_assert_f(time < 2*sum,
344 "Average time (%.3fus) exceeds expectation for parallel execution (min %.3fus, max %.3fus; limit set at %.3fus)\n",
345 1e6*time, 1e6*min, 1e6*max, 1e6*sum*2);
Daniel Vetterd9d95782012-12-04 17:13:05 +0100346}
Daniel Vetter8f5387e2013-08-13 13:20:58 +0200347
Chris Wilson9fbf72b2017-01-22 18:24:37 +0000348static void xchg(void *array, unsigned i, unsigned j)
Chris Wilson817d57f2017-01-20 17:17:42 +0000349{
Chris Wilson9fbf72b2017-01-22 18:24:37 +0000350 unsigned *u = array;
351 unsigned tmp = u[i];
352 u[i] = u[j];
353 u[j] = tmp;
354}
355
356static int __gem_context_create(int fd, uint32_t *ctx_id)
357{
358 struct drm_i915_gem_context_create arg;
359 int ret = 0;
360
361 memset(&arg, 0, sizeof(arg));
362 if (drmIoctl(fd, DRM_IOCTL_I915_GEM_CONTEXT_CREATE, &arg))
363 ret = -errno;
364
365 *ctx_id = arg.ctx_id;
366 return ret;
367}
Chris Wilsone10c48d2017-05-12 12:33:32 +0100368
Chris Wilson9fbf72b2017-01-22 18:24:37 +0000369static void sequential(int fd, uint32_t handle, unsigned flags, int timeout)
370{
371 const int ncpus = flags & FORKED ? sysconf(_SC_NPROCESSORS_ONLN) : 1;
Chris Wilson817d57f2017-01-20 17:17:42 +0000372 struct drm_i915_gem_execbuffer2 execbuf;
373 struct drm_i915_gem_exec_object2 obj[2];
Chris Wilson817d57f2017-01-20 17:17:42 +0000374 unsigned engines[16];
375 unsigned nengine;
Chris Wilson9fbf72b2017-01-22 18:24:37 +0000376 double *results;
Chris Wilson817d57f2017-01-20 17:17:42 +0000377 double time, sum;
Chris Wilson9fbf72b2017-01-22 18:24:37 +0000378 unsigned n;
379
380 results = mmap(NULL, 4096, PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);
381 igt_assert(results != MAP_FAILED);
Chris Wilson817d57f2017-01-20 17:17:42 +0000382
383 nengine = 0;
384 sum = 0;
Chris Wilson9fbf72b2017-01-22 18:24:37 +0000385 for_each_engine(fd, n) {
386 unsigned long count;
387
388 if (ignore_engine(fd, n))
Chris Wilson817d57f2017-01-20 17:17:42 +0000389 continue;
390
Chris Wilson9fbf72b2017-01-22 18:24:37 +0000391 time = nop_on_ring(fd, handle, n, 1, &count) / count;
Chris Wilson817d57f2017-01-20 17:17:42 +0000392 sum += time;
393 igt_debug("%s: %.3fus\n", e__->name, 1e6*time);
394
Chris Wilson9fbf72b2017-01-22 18:24:37 +0000395 engines[nengine++] = n;
Chris Wilson817d57f2017-01-20 17:17:42 +0000396 }
397 igt_require(nengine);
398 igt_info("Total (individual) execution latency %.3fus per cycle\n",
399 1e6*sum);
400
401 memset(obj, 0, sizeof(obj));
402 obj[0].handle = gem_create(fd, 4096);
403 obj[0].flags = EXEC_OBJECT_WRITE;
404 obj[1].handle = handle;
405
406 memset(&execbuf, 0, sizeof(execbuf));
407 execbuf.buffers_ptr = to_user_pointer(obj);
408 execbuf.buffer_count = 2;
409 execbuf.flags |= LOCAL_I915_EXEC_HANDLE_LUT;
410 execbuf.flags |= LOCAL_I915_EXEC_NO_RELOC;
411 igt_require(__gem_execbuf(fd, &execbuf) == 0);
412
Chris Wilson9fbf72b2017-01-22 18:24:37 +0000413 if (flags & CONTEXT) {
414 uint32_t id;
415
416 igt_require(__gem_context_create(fd, &id) == 0);
417 execbuf.rsvd1 = id;
418 }
419
420 for (n = 0; n < nengine; n++) {
421 execbuf.flags &= ~ENGINE_FLAGS;
422 execbuf.flags |= engines[n];
423 igt_require(__gem_execbuf(fd, &execbuf) == 0);
424 }
425
Chris Wilson817d57f2017-01-20 17:17:42 +0000426 intel_detect_and_clear_missed_interrupts(fd);
427
Chris Wilson9fbf72b2017-01-22 18:24:37 +0000428 igt_fork(child, ncpus) {
429 struct timespec start, now;
430 unsigned long count;
431
432 obj[0].handle = gem_create(fd, 4096);
433 gem_execbuf(fd, &execbuf);
434
435 if (flags & CONTEXT)
436 execbuf.rsvd1 = gem_context_create(fd);
437
438 hars_petruska_f54_1_random_perturb(child);
439
440 count = 0;
441 clock_gettime(CLOCK_MONOTONIC, &start);
442 do {
443 igt_permute_array(engines, nengine, xchg);
444 if (flags & CHAINED) {
445 for (n = 0; n < nengine; n++) {
446 execbuf.flags &= ~ENGINE_FLAGS;
447 execbuf.flags |= engines[n];
448 for (int loop = 0; loop < 1024; loop++)
449 gem_execbuf(fd, &execbuf);
450 }
451 } else {
452 for (int loop = 0; loop < 1024; loop++) {
453 for (n = 0; n < nengine; n++) {
454 execbuf.flags &= ~ENGINE_FLAGS;
455 execbuf.flags |= engines[n];
456 gem_execbuf(fd, &execbuf);
457 }
458 }
Chris Wilson817d57f2017-01-20 17:17:42 +0000459 }
Chris Wilson9fbf72b2017-01-22 18:24:37 +0000460 count += 1024;
461 clock_gettime(CLOCK_MONOTONIC, &now);
462 } while (elapsed(&start, &now) < timeout); /* Hang detection ~120s */
Chris Wilsone7a0d062017-03-21 13:12:07 +0000463
464 gem_sync(fd, obj[0].handle);
465 clock_gettime(CLOCK_MONOTONIC, &now);
Chris Wilson9fbf72b2017-01-22 18:24:37 +0000466 results[child] = elapsed(&start, &now) / count;
467
468 if (flags & CONTEXT)
469 gem_context_destroy(fd, execbuf.rsvd1);
470
471 gem_close(fd, obj[0].handle);
472 }
473 igt_waitchildren();
Chris Wilson817d57f2017-01-20 17:17:42 +0000474 igt_assert_eq(intel_detect_and_clear_missed_interrupts(fd), 0);
475
Chris Wilson9fbf72b2017-01-22 18:24:37 +0000476 results[ncpus] = 0;
477 for (n = 0; n < ncpus; n++)
478 results[ncpus] += results[n];
479 results[ncpus] /= ncpus;
480
481 igt_info("Sequential (%d engines, %d processes): average %.3fus per cycle [expected %.3fus]\n",
482 nengine, ncpus, 1e6*results[ncpus], 1e6*sum*ncpus);
483
484 if (flags & CONTEXT)
485 gem_context_destroy(fd, execbuf.rsvd1);
Chris Wilson817d57f2017-01-20 17:17:42 +0000486
487 gem_close(fd, obj[0].handle);
Chris Wilson9fbf72b2017-01-22 18:24:37 +0000488 munmap(results, 4096);
Chris Wilson817d57f2017-01-20 17:17:42 +0000489}
490
Chris Wilsone10c48d2017-05-12 12:33:32 +0100491#define LOCAL_EXEC_FENCE_OUT (1 << 17)
492#define LOCAL_IOCTL_I915_GEM_EXECBUFFER2_WR DRM_IOWR(DRM_COMMAND_BASE + DRM_I915_GEM_EXECBUFFER2, struct drm_i915_gem_execbuffer2)
493
494static int __gem_execbuf_wr(int fd, struct drm_i915_gem_execbuffer2 *execbuf)
495{
496 int err = 0;
497 if (igt_ioctl(fd, LOCAL_IOCTL_I915_GEM_EXECBUFFER2_WR, execbuf))
498 err = -errno;
499 errno = 0;
500 return err;
501}
502
503static void gem_execbuf_wr(int fd, struct drm_i915_gem_execbuffer2 *execbuf)
504{
505 igt_assert_eq(__gem_execbuf_wr(fd, execbuf), 0);
506}
507
508static bool fence_enable_signaling(int fence)
509{
510 return poll(&(struct pollfd){fence, POLLIN}, 1, 0) == 0;
511}
512
513static bool fence_wait(int fence)
514{
515 return poll(&(struct pollfd){fence, POLLIN}, 1, -1) == 1;
516}
517
518static void signal(int fd, uint32_t handle,
519 unsigned ring_id, const char *ring_name,
520 int timeout)
521{
522#define NFENCES 512
523 struct drm_i915_gem_execbuffer2 execbuf;
524 struct drm_i915_gem_exec_object2 obj;
525 struct timespec start, now;
526 unsigned engines[16];
527 unsigned nengine;
528 int *fences, n;
529 unsigned long count, signal;
530
531 igt_require(gem_has_exec_fence(fd));
532
533 nengine = 0;
534 if (ring_id == -1) {
535 for_each_engine(fd, n) {
536 if (ignore_engine(fd, n))
537 continue;
538
539 engines[nengine++] = n;
540 }
541 } else {
Chris Wilson842e6a42017-05-13 13:27:52 +0100542 gem_require_ring(fd, ring_id);
Chris Wilsone10c48d2017-05-12 12:33:32 +0100543 engines[nengine++] = ring_id;
544 }
545 igt_require(nengine);
546
547 fences = malloc(sizeof(*fences) * NFENCES);
548 igt_assert(fences);
549 memset(fences, -1, sizeof(*fences) * NFENCES);
550
551 memset(&obj, 0, sizeof(obj));
552 obj.handle = handle;
553
554 memset(&execbuf, 0, sizeof(execbuf));
555 execbuf.buffers_ptr = to_user_pointer(&obj);
556 execbuf.buffer_count = 1;
557 execbuf.flags = LOCAL_EXEC_FENCE_OUT;
558
559 n = 0;
560 count = 0;
561 signal = 0;
562
563 intel_detect_and_clear_missed_interrupts(fd);
564 clock_gettime(CLOCK_MONOTONIC, &start);
565 do {
566 for (int loop = 0; loop < 1024; loop++) {
567 for (int e = 0; e < nengine; e++) {
568 if (fences[n] != -1) {
569 igt_assert(fence_wait(fences[n]));
570 close(fences[n]);
571 }
572
573 execbuf.flags &= ~ENGINE_FLAGS;
574 execbuf.flags |= engines[e];
575 gem_execbuf_wr(fd, &execbuf);
576
577 /* Enable signaling by doing a poll() */
578 fences[n] = execbuf.rsvd2 >> 32;
579 signal += fence_enable_signaling(fences[n]);
580
581 n = (n + 1) % NFENCES;
582 }
583 }
584
585 count += 1024 * nengine;
586 clock_gettime(CLOCK_MONOTONIC, &now);
587 } while (elapsed(&start, &now) < timeout);
588 igt_assert_eq(intel_detect_and_clear_missed_interrupts(fd), 0);
589
590 for (n = 0; n < NFENCES; n++)
591 if (fences[n] != -1)
592 close(fences[n]);
593 free(fences);
594
595 igt_info("Signal %s: %'lu cycles (%'lu signals): %.3fus\n",
596 ring_name, count, signal, elapsed(&start, &now) * 1e6 / count);
597}
598
Chris Wilsonc6e26e42016-07-22 12:58:54 +0100599static void print_welcome(int fd)
600{
601 bool active;
Chris Wilsonc6e26e42016-07-22 12:58:54 +0100602 int dir;
603
604 dir = igt_sysfs_open_parameters(fd);
605 if (dir < 0)
606 return;
607
Chris Wilsonb64d10c2016-07-22 17:53:51 +0100608 active = igt_sysfs_get_boolean(dir, "enable_guc_submission");
Chris Wilsonc6e26e42016-07-22 12:58:54 +0100609 if (active) {
610 igt_info("Using GuC submission\n");
611 goto out;
612 }
613
Chris Wilsonb64d10c2016-07-22 17:53:51 +0100614 active = igt_sysfs_get_boolean(dir, "enable_execlists");
Chris Wilsonc6e26e42016-07-22 12:58:54 +0100615 if (active) {
616 igt_info("Using Execlists submission\n");
617 goto out;
618 }
619
Chris Wilsonb64d10c2016-07-22 17:53:51 +0100620 active = igt_sysfs_get_boolean(dir, "semaphores");
Chris Wilsonf565b6c2016-09-08 20:59:55 +0100621 igt_info("Using Legacy submission%s\n",
Chris Wilsonc6e26e42016-07-22 12:58:54 +0100622 active ? ", with semaphores" : "");
623
624out:
625 close(dir);
626}
627
Daniel Vetter071e9ca2013-10-31 16:23:26 +0100628igt_main
Daniel Vetterd9d95782012-12-04 17:13:05 +0100629{
Chris Wilson7e0853c2016-01-27 14:17:53 +0000630 const struct intel_execution_engine *e;
Chris Wilson2659cbb2015-03-26 12:09:57 +0000631 uint32_t handle = 0;
Chris Wilson3a7325e2016-03-08 11:43:31 +0000632 int device = -1;
Daniel Vetterd9d95782012-12-04 17:13:05 +0100633
Chris Wilson2659cbb2015-03-26 12:09:57 +0000634 igt_fixture {
Chris Wilson3a7325e2016-03-08 11:43:31 +0000635 const uint32_t bbe = MI_BATCH_BUFFER_END;
636
Micah Fedkec81d2932015-07-22 21:54:02 +0000637 device = drm_open_driver(DRIVER_INTEL);
Chris Wilson9518cb52017-02-22 15:24:54 +0000638 igt_require_gem(device);
Chris Wilsonc6e26e42016-07-22 12:58:54 +0100639 print_welcome(device);
640
Chris Wilson2659cbb2015-03-26 12:09:57 +0000641 handle = gem_create(device, 4096);
Chris Wilson3a7325e2016-03-08 11:43:31 +0000642 gem_write(device, handle, 0, &bbe, sizeof(bbe));
Daniel Vetterd9d95782012-12-04 17:13:05 +0100643
Daniel Vetterbe21fc02016-06-17 16:04:09 +0200644 igt_fork_hang_detector(device);
645 }
Chris Wilson9d61a682016-03-25 18:22:54 +0000646
Chris Wilson4cce7152016-09-08 13:43:17 +0100647 igt_subtest("basic-series")
Chris Wilson69b29f82016-10-18 10:23:49 +0100648 series(device, handle, 5);
Chris Wilson4cce7152016-09-08 13:43:17 +0100649
650 igt_subtest("basic-parallel")
Chris Wilson69b29f82016-10-18 10:23:49 +0100651 parallel(device, handle, 5);
Chris Wilson772393e2016-03-14 14:31:36 +0000652
Chris Wilson817d57f2017-01-20 17:17:42 +0000653 igt_subtest("basic-sequential")
Chris Wilson9fbf72b2017-01-22 18:24:37 +0000654 sequential(device, handle, 0, 5);
Chris Wilson817d57f2017-01-20 17:17:42 +0000655
Chris Wilsone10c48d2017-05-12 12:33:32 +0100656 for (e = intel_execution_engines; e->name; e++) {
Chris Wilson7e0853c2016-01-27 14:17:53 +0000657 igt_subtest_f("%s", e->name)
Chris Wilson3a7325e2016-03-08 11:43:31 +0000658 single(device, handle, e->exec_id | e->flags, e->name);
Chris Wilsone10c48d2017-05-12 12:33:32 +0100659 igt_subtest_f("signal-%s", e->name)
660 signal(device, handle, e->exec_id | e->flags, e->name, 5);
661 }
662
663 igt_subtest("signal-all")
664 signal(device, handle, -1, "all", 150);
Chris Wilson3a7325e2016-03-08 11:43:31 +0000665
Chris Wilson4cce7152016-09-08 13:43:17 +0100666 igt_subtest("series")
667 series(device, handle, 150);
668
669 igt_subtest("parallel")
670 parallel(device, handle, 150);
Daniel Vetterd9d95782012-12-04 17:13:05 +0100671
Chris Wilson817d57f2017-01-20 17:17:42 +0000672 igt_subtest("sequential")
Chris Wilson9fbf72b2017-01-22 18:24:37 +0000673 sequential(device, handle, 0, 150);
674
675 igt_subtest("forked-sequential")
676 sequential(device, handle, FORKED, 150);
677
678 igt_subtest("chained-sequential")
679 sequential(device, handle, FORKED | CHAINED, 150);
680
681 igt_subtest("context-sequential")
682 sequential(device, handle, FORKED | CONTEXT, 150);
Chris Wilson817d57f2017-01-20 17:17:42 +0000683
Tvrtko Ursulin8e4cfb22017-04-13 14:11:05 +0100684 igt_subtest("headless")
685 headless(device, handle);
686
Daniel Vetterb3880d32013-08-14 18:02:46 +0200687 igt_fixture {
Daniel Vetterbe21fc02016-06-17 16:04:09 +0200688 igt_stop_hang_detector();
Chris Wilson2659cbb2015-03-26 12:09:57 +0000689 gem_close(device, handle);
Chris Wilson2659cbb2015-03-26 12:09:57 +0000690 close(device);
Daniel Vetterb3880d32013-08-14 18:02:46 +0200691 }
Chris Wilson07d59b32011-01-20 22:10:10 +0000692}