blob: 62a5360525ae567e729e2be9416e5a44720598ba [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
Arkadiusz Hiler21bfc9f2017-04-19 12:50:06 +0200142#if !defined(ANDROID) || ANDROID_HAS_CAIRO
Tvrtko Ursulin8e4cfb22017-04-13 14:11:05 +0100143#define assert_within_epsilon(x, ref, tolerance) \
144 igt_assert_f((x) <= (1.0 + tolerance) * ref && \
145 (x) >= (1.0 - tolerance) * ref, \
146 "'%s' != '%s' (%f not within %f%% tolerance of %f)\n",\
147 #x, #ref, x, tolerance * 100.0, ref)
148
149static void headless(int fd, uint32_t handle)
150{
151 unsigned int nr_connected = 0;
152 drmModeConnector *connector;
153 drmModeRes *res;
154 double n_display, n_headless;
155
156 res = drmModeGetResources(fd);
157 igt_assert(res);
158
159 /* require at least one connected connector for the test */
160 for (int i = 0; i < res->count_connectors; i++) {
161 connector = drmModeGetConnectorCurrent(fd, res->connectors[i]);
162 if (connector->connection == DRM_MODE_CONNECTED)
163 nr_connected++;
164 drmModeFreeConnector(connector);
165 }
166 igt_require(nr_connected > 0);
167
168 /* set graphics mode to prevent blanking */
169 kmstest_set_vt_graphics_mode();
170
171 /* benchmark nops */
172 n_display = stable_nop_on_ring(fd, handle, I915_EXEC_DEFAULT, 1, 5);
Chris Wilsonf6a31ad2017-04-19 13:38:32 +0100173 igt_info("With one display connected: %.2fus\n",
174 n_display * 1e6);
Tvrtko Ursulin8e4cfb22017-04-13 14:11:05 +0100175
176 /* force all connectors off */
177 kmstest_unset_all_crtcs(fd, res);
178
179 /* benchmark nops again */
180 n_headless = stable_nop_on_ring(fd, handle, I915_EXEC_DEFAULT, 1, 5);
Chris Wilsonf6a31ad2017-04-19 13:38:32 +0100181 igt_info("Without a display connected (headless): %.2fus\n",
182 n_headless * 1e6);
Tvrtko Ursulin8e4cfb22017-04-13 14:11:05 +0100183
184 /* check that the two execution speeds are roughly the same */
185 assert_within_epsilon(n_headless, n_display, 0.1f);
186}
Arkadiusz Hiler21bfc9f2017-04-19 12:50:06 +0200187#endif
Tvrtko Ursulin8e4cfb22017-04-13 14:11:05 +0100188
Chris Wilson0aacdac2016-03-09 21:06:16 +0000189static bool ignore_engine(int fd, unsigned engine)
190{
191 if (engine == 0)
192 return true;
193
194 if (gem_has_bsd2(fd) && engine == I915_EXEC_BSD)
195 return true;
196
197 return false;
198}
199
Chris Wilson4cce7152016-09-08 13:43:17 +0100200static void parallel(int fd, uint32_t handle, int timeout)
201{
202 struct drm_i915_gem_execbuffer2 execbuf;
203 struct drm_i915_gem_exec_object2 obj;
Chris Wilson4cce7152016-09-08 13:43:17 +0100204 unsigned engines[16];
205 const char *names[16];
206 unsigned nengine;
207 unsigned engine;
208 unsigned long count;
209 double time, sum;
210
211 sum = 0;
212 nengine = 0;
213 for_each_engine(fd, engine) {
214 if (ignore_engine(fd, engine))
215 continue;
216
217 engines[nengine] = engine;
218 names[nengine] = e__->name;
219 nengine++;
220
221 time = nop_on_ring(fd, handle, engine, 1, &count) / count;
222 sum += time;
223 igt_debug("%s: %.3fus\n", e__->name, 1e6*time);
224 }
225 igt_require(nengine);
226 igt_info("average (individually): %.3fus\n", sum/nengine*1e6);
227
228 memset(&obj, 0, sizeof(obj));
229 obj.handle = handle;
230
231 memset(&execbuf, 0, sizeof(execbuf));
Chris Wilson4de67b22017-01-02 11:05:21 +0000232 execbuf.buffers_ptr = to_user_pointer(&obj);
Chris Wilson4cce7152016-09-08 13:43:17 +0100233 execbuf.buffer_count = 1;
234 execbuf.flags |= LOCAL_I915_EXEC_HANDLE_LUT;
235 execbuf.flags |= LOCAL_I915_EXEC_NO_RELOC;
236 if (__gem_execbuf(fd, &execbuf)) {
237 execbuf.flags = 0;
238 gem_execbuf(fd, &execbuf);
239 }
Chris Wilson4cce7152016-09-08 13:43:17 +0100240 intel_detect_and_clear_missed_interrupts(fd);
241
242 igt_fork(child, nengine) {
Chris Wilsonf565b6c2016-09-08 20:59:55 +0100243 struct timespec start, now;
244
Chris Wilson4cce7152016-09-08 13:43:17 +0100245 execbuf.flags &= ~ENGINE_FLAGS;
246 execbuf.flags |= engines[child];
247
248 count = 0;
249 clock_gettime(CLOCK_MONOTONIC, &start);
250 do {
251 for (int loop = 0; loop < 1024; loop++)
252 gem_execbuf(fd, &execbuf);
253 count += 1024;
254 clock_gettime(CLOCK_MONOTONIC, &now);
255 } while (elapsed(&start, &now) < timeout);
Chris Wilson4cce7152016-09-08 13:43:17 +0100256 time = elapsed(&start, &now) / count;
257 igt_info("%s: %ld cycles, %.3fus\n", names[child], count, 1e6*time);
258 }
259
260 igt_waitchildren();
261 igt_assert_eq(intel_detect_and_clear_missed_interrupts(fd), 0);
262
263}
264
265static void series(int fd, uint32_t handle, int timeout)
Chris Wilson3a7325e2016-03-08 11:43:31 +0000266{
267 struct drm_i915_gem_execbuffer2 execbuf;
268 struct drm_i915_gem_exec_object2 obj;
Chris Wilsonf565b6c2016-09-08 20:59:55 +0100269 struct timespec start, now, sync;
Chris Wilson3a7325e2016-03-08 11:43:31 +0000270 unsigned engines[16];
271 unsigned nengine;
272 unsigned engine;
Chris Wilson870c7742016-03-28 15:29:46 +0100273 unsigned long count;
Chris Wilson41a26b52016-03-28 16:26:01 +0100274 double time, max = 0, min = HUGE_VAL, sum = 0;
Chris Wilson870c7742016-03-28 15:29:46 +0100275 const char *name;
Chris Wilson3a7325e2016-03-08 11:43:31 +0000276
277 nengine = 0;
Chris Wilson870c7742016-03-28 15:29:46 +0100278 for_each_engine(fd, engine) {
279 if (ignore_engine(fd, engine))
280 continue;
281
282 time = nop_on_ring(fd, handle, engine, 1, &count) / count;
283 if (time > max) {
284 name = e__->name;
285 max = time;
286 }
Chris Wilson41a26b52016-03-28 16:26:01 +0100287 if (time < min)
288 min = time;
Chris Wilson870c7742016-03-28 15:29:46 +0100289 sum += time;
290 engines[nengine++] = engine;
291 }
Chris Wilson0aacdac2016-03-09 21:06:16 +0000292 igt_require(nengine);
Chris Wilsonf565b6c2016-09-08 20:59:55 +0100293 igt_info("Maximum execution latency on %s, %.3fus, min %.3fus, total %.3fus per cycle, average %.3fus\n",
294 name, max*1e6, min*1e6, sum*1e6, sum/nengine*1e6);
Chris Wilson3a7325e2016-03-08 11:43:31 +0000295
296 memset(&obj, 0, sizeof(obj));
297 obj.handle = handle;
298
299 memset(&execbuf, 0, sizeof(execbuf));
Chris Wilson4de67b22017-01-02 11:05:21 +0000300 execbuf.buffers_ptr = to_user_pointer(&obj);
Chris Wilson3a7325e2016-03-08 11:43:31 +0000301 execbuf.buffer_count = 1;
302 execbuf.flags |= LOCAL_I915_EXEC_HANDLE_LUT;
303 execbuf.flags |= LOCAL_I915_EXEC_NO_RELOC;
Chris Wilson3e2443f2016-03-10 11:50:53 +0000304 if (__gem_execbuf(fd, &execbuf)) {
Chris Wilson3a7325e2016-03-08 11:43:31 +0000305 execbuf.flags = 0;
306 gem_execbuf(fd, &execbuf);
Chris Wilson07d59b32011-01-20 22:10:10 +0000307 }
Chris Wilson71f41532016-04-22 16:55:29 +0100308 intel_detect_and_clear_missed_interrupts(fd);
Chris Wilson3a7325e2016-03-08 11:43:31 +0000309
Chris Wilson870c7742016-03-28 15:29:46 +0100310 count = 0;
Chris Wilson3a7325e2016-03-08 11:43:31 +0000311 clock_gettime(CLOCK_MONOTONIC, &start);
312 do {
313 for (int loop = 0; loop < 1024; loop++) {
314 for (int n = 0; n < nengine; n++) {
315 execbuf.flags &= ~ENGINE_FLAGS;
316 execbuf.flags |= engines[n];
317 gem_execbuf(fd, &execbuf);
318 }
319 }
320 count += nengine * 1024;
321 clock_gettime(CLOCK_MONOTONIC, &now);
Chris Wilson772393e2016-03-14 14:31:36 +0000322 } while (elapsed(&start, &now) < timeout); /* Hang detection ~120s */
Chris Wilson3a7325e2016-03-08 11:43:31 +0000323 gem_sync(fd, handle);
Chris Wilsonf565b6c2016-09-08 20:59:55 +0100324 clock_gettime(CLOCK_MONOTONIC, &sync);
325 igt_debug("sync time: %.3fus\n", elapsed(&now, &sync)*1e6);
Chris Wilson71f41532016-04-22 16:55:29 +0100326 igt_assert_eq(intel_detect_and_clear_missed_interrupts(fd), 0);
Chris Wilson3a7325e2016-03-08 11:43:31 +0000327
Chris Wilson870c7742016-03-28 15:29:46 +0100328 time = elapsed(&start, &now) / count;
Chris Wilsonf565b6c2016-09-08 20:59:55 +0100329 igt_info("All (%d engines): %'lu cycles, average %.3fus per cycle [expected %.3fus]\n",
Chris Wilson61b19a42016-09-08 14:50:32 +0100330 nengine, count, 1e6*time, 1e6*((max-min)/nengine+min));
Chris Wilson41a26b52016-03-28 16:26:01 +0100331
Chris Wilsona0eebbd2016-09-08 13:29:31 +0100332 /* The rate limiting step should be how fast the slowest engine can
333 * execute its queue of requests, as when we wait upon a full ring all
334 * dispatch is frozen. So in general we cannot go faster than the
335 * slowest engine (but as all engines are in lockstep, they should all
336 * be executing in parallel and so the average should be max/nengines),
337 * but we should equally not go any slower.
338 *
339 * However, that depends upon being able to submit fast enough, and
340 * that in turns depends upon debugging turned off and no bottlenecks
341 * within the driver. We cannot assert that we hit ideal conditions
342 * across all engines, so we only look for an outrageous error
343 * condition.
Chris Wilson41a26b52016-03-28 16:26:01 +0100344 */
Chris Wilsona0eebbd2016-09-08 13:29:31 +0100345 igt_assert_f(time < 2*sum,
346 "Average time (%.3fus) exceeds expectation for parallel execution (min %.3fus, max %.3fus; limit set at %.3fus)\n",
347 1e6*time, 1e6*min, 1e6*max, 1e6*sum*2);
Daniel Vetterd9d95782012-12-04 17:13:05 +0100348}
Daniel Vetter8f5387e2013-08-13 13:20:58 +0200349
Chris Wilson9fbf72b2017-01-22 18:24:37 +0000350static void xchg(void *array, unsigned i, unsigned j)
Chris Wilson817d57f2017-01-20 17:17:42 +0000351{
Chris Wilson9fbf72b2017-01-22 18:24:37 +0000352 unsigned *u = array;
353 unsigned tmp = u[i];
354 u[i] = u[j];
355 u[j] = tmp;
356}
357
358static int __gem_context_create(int fd, uint32_t *ctx_id)
359{
360 struct drm_i915_gem_context_create arg;
361 int ret = 0;
362
363 memset(&arg, 0, sizeof(arg));
364 if (drmIoctl(fd, DRM_IOCTL_I915_GEM_CONTEXT_CREATE, &arg))
365 ret = -errno;
366
367 *ctx_id = arg.ctx_id;
368 return ret;
369}
Chris Wilsone10c48d2017-05-12 12:33:32 +0100370
Chris Wilson9fbf72b2017-01-22 18:24:37 +0000371static void sequential(int fd, uint32_t handle, unsigned flags, int timeout)
372{
373 const int ncpus = flags & FORKED ? sysconf(_SC_NPROCESSORS_ONLN) : 1;
Chris Wilson817d57f2017-01-20 17:17:42 +0000374 struct drm_i915_gem_execbuffer2 execbuf;
375 struct drm_i915_gem_exec_object2 obj[2];
Chris Wilson817d57f2017-01-20 17:17:42 +0000376 unsigned engines[16];
377 unsigned nengine;
Chris Wilson9fbf72b2017-01-22 18:24:37 +0000378 double *results;
Chris Wilson817d57f2017-01-20 17:17:42 +0000379 double time, sum;
Chris Wilson9fbf72b2017-01-22 18:24:37 +0000380 unsigned n;
381
382 results = mmap(NULL, 4096, PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);
383 igt_assert(results != MAP_FAILED);
Chris Wilson817d57f2017-01-20 17:17:42 +0000384
385 nengine = 0;
386 sum = 0;
Chris Wilson9fbf72b2017-01-22 18:24:37 +0000387 for_each_engine(fd, n) {
388 unsigned long count;
389
390 if (ignore_engine(fd, n))
Chris Wilson817d57f2017-01-20 17:17:42 +0000391 continue;
392
Chris Wilson9fbf72b2017-01-22 18:24:37 +0000393 time = nop_on_ring(fd, handle, n, 1, &count) / count;
Chris Wilson817d57f2017-01-20 17:17:42 +0000394 sum += time;
395 igt_debug("%s: %.3fus\n", e__->name, 1e6*time);
396
Chris Wilson9fbf72b2017-01-22 18:24:37 +0000397 engines[nengine++] = n;
Chris Wilson817d57f2017-01-20 17:17:42 +0000398 }
399 igt_require(nengine);
400 igt_info("Total (individual) execution latency %.3fus per cycle\n",
401 1e6*sum);
402
403 memset(obj, 0, sizeof(obj));
404 obj[0].handle = gem_create(fd, 4096);
405 obj[0].flags = EXEC_OBJECT_WRITE;
406 obj[1].handle = handle;
407
408 memset(&execbuf, 0, sizeof(execbuf));
409 execbuf.buffers_ptr = to_user_pointer(obj);
410 execbuf.buffer_count = 2;
411 execbuf.flags |= LOCAL_I915_EXEC_HANDLE_LUT;
412 execbuf.flags |= LOCAL_I915_EXEC_NO_RELOC;
413 igt_require(__gem_execbuf(fd, &execbuf) == 0);
414
Chris Wilson9fbf72b2017-01-22 18:24:37 +0000415 if (flags & CONTEXT) {
416 uint32_t id;
417
418 igt_require(__gem_context_create(fd, &id) == 0);
419 execbuf.rsvd1 = id;
420 }
421
422 for (n = 0; n < nengine; n++) {
423 execbuf.flags &= ~ENGINE_FLAGS;
424 execbuf.flags |= engines[n];
425 igt_require(__gem_execbuf(fd, &execbuf) == 0);
426 }
427
Chris Wilson817d57f2017-01-20 17:17:42 +0000428 intel_detect_and_clear_missed_interrupts(fd);
429
Chris Wilson9fbf72b2017-01-22 18:24:37 +0000430 igt_fork(child, ncpus) {
431 struct timespec start, now;
432 unsigned long count;
433
434 obj[0].handle = gem_create(fd, 4096);
435 gem_execbuf(fd, &execbuf);
436
437 if (flags & CONTEXT)
438 execbuf.rsvd1 = gem_context_create(fd);
439
440 hars_petruska_f54_1_random_perturb(child);
441
442 count = 0;
443 clock_gettime(CLOCK_MONOTONIC, &start);
444 do {
445 igt_permute_array(engines, nengine, xchg);
446 if (flags & CHAINED) {
447 for (n = 0; n < nengine; n++) {
448 execbuf.flags &= ~ENGINE_FLAGS;
449 execbuf.flags |= engines[n];
450 for (int loop = 0; loop < 1024; loop++)
451 gem_execbuf(fd, &execbuf);
452 }
453 } else {
454 for (int loop = 0; loop < 1024; loop++) {
455 for (n = 0; n < nengine; n++) {
456 execbuf.flags &= ~ENGINE_FLAGS;
457 execbuf.flags |= engines[n];
458 gem_execbuf(fd, &execbuf);
459 }
460 }
Chris Wilson817d57f2017-01-20 17:17:42 +0000461 }
Chris Wilson9fbf72b2017-01-22 18:24:37 +0000462 count += 1024;
463 clock_gettime(CLOCK_MONOTONIC, &now);
464 } while (elapsed(&start, &now) < timeout); /* Hang detection ~120s */
Chris Wilsone7a0d062017-03-21 13:12:07 +0000465
466 gem_sync(fd, obj[0].handle);
467 clock_gettime(CLOCK_MONOTONIC, &now);
Chris Wilson9fbf72b2017-01-22 18:24:37 +0000468 results[child] = elapsed(&start, &now) / count;
469
470 if (flags & CONTEXT)
471 gem_context_destroy(fd, execbuf.rsvd1);
472
473 gem_close(fd, obj[0].handle);
474 }
475 igt_waitchildren();
Chris Wilson817d57f2017-01-20 17:17:42 +0000476 igt_assert_eq(intel_detect_and_clear_missed_interrupts(fd), 0);
477
Chris Wilson9fbf72b2017-01-22 18:24:37 +0000478 results[ncpus] = 0;
479 for (n = 0; n < ncpus; n++)
480 results[ncpus] += results[n];
481 results[ncpus] /= ncpus;
482
483 igt_info("Sequential (%d engines, %d processes): average %.3fus per cycle [expected %.3fus]\n",
484 nengine, ncpus, 1e6*results[ncpus], 1e6*sum*ncpus);
485
486 if (flags & CONTEXT)
487 gem_context_destroy(fd, execbuf.rsvd1);
Chris Wilson817d57f2017-01-20 17:17:42 +0000488
489 gem_close(fd, obj[0].handle);
Chris Wilson9fbf72b2017-01-22 18:24:37 +0000490 munmap(results, 4096);
Chris Wilson817d57f2017-01-20 17:17:42 +0000491}
492
Chris Wilsone10c48d2017-05-12 12:33:32 +0100493#define LOCAL_EXEC_FENCE_OUT (1 << 17)
Chris Wilsone10c48d2017-05-12 12:33:32 +0100494static bool fence_enable_signaling(int fence)
495{
496 return poll(&(struct pollfd){fence, POLLIN}, 1, 0) == 0;
497}
498
499static bool fence_wait(int fence)
500{
501 return poll(&(struct pollfd){fence, POLLIN}, 1, -1) == 1;
502}
503
Arkadiusz Hiler09894782017-05-16 12:59:50 +0200504static void fence_signal(int fd, uint32_t handle,
505 unsigned ring_id, const char *ring_name,
506 int timeout)
Chris Wilsone10c48d2017-05-12 12:33:32 +0100507{
508#define NFENCES 512
509 struct drm_i915_gem_execbuffer2 execbuf;
510 struct drm_i915_gem_exec_object2 obj;
511 struct timespec start, now;
512 unsigned engines[16];
513 unsigned nengine;
514 int *fences, n;
515 unsigned long count, signal;
516
517 igt_require(gem_has_exec_fence(fd));
518
519 nengine = 0;
520 if (ring_id == -1) {
521 for_each_engine(fd, n) {
522 if (ignore_engine(fd, n))
523 continue;
524
525 engines[nengine++] = n;
526 }
527 } else {
Chris Wilson842e6a42017-05-13 13:27:52 +0100528 gem_require_ring(fd, ring_id);
Chris Wilsone10c48d2017-05-12 12:33:32 +0100529 engines[nengine++] = ring_id;
530 }
531 igt_require(nengine);
532
533 fences = malloc(sizeof(*fences) * NFENCES);
534 igt_assert(fences);
535 memset(fences, -1, sizeof(*fences) * NFENCES);
536
537 memset(&obj, 0, sizeof(obj));
538 obj.handle = handle;
539
540 memset(&execbuf, 0, sizeof(execbuf));
541 execbuf.buffers_ptr = to_user_pointer(&obj);
542 execbuf.buffer_count = 1;
543 execbuf.flags = LOCAL_EXEC_FENCE_OUT;
544
545 n = 0;
546 count = 0;
547 signal = 0;
548
549 intel_detect_and_clear_missed_interrupts(fd);
550 clock_gettime(CLOCK_MONOTONIC, &start);
551 do {
552 for (int loop = 0; loop < 1024; loop++) {
553 for (int e = 0; e < nengine; e++) {
554 if (fences[n] != -1) {
555 igt_assert(fence_wait(fences[n]));
556 close(fences[n]);
557 }
558
559 execbuf.flags &= ~ENGINE_FLAGS;
560 execbuf.flags |= engines[e];
561 gem_execbuf_wr(fd, &execbuf);
562
563 /* Enable signaling by doing a poll() */
564 fences[n] = execbuf.rsvd2 >> 32;
565 signal += fence_enable_signaling(fences[n]);
566
567 n = (n + 1) % NFENCES;
568 }
569 }
570
571 count += 1024 * nengine;
572 clock_gettime(CLOCK_MONOTONIC, &now);
573 } while (elapsed(&start, &now) < timeout);
574 igt_assert_eq(intel_detect_and_clear_missed_interrupts(fd), 0);
575
576 for (n = 0; n < NFENCES; n++)
577 if (fences[n] != -1)
578 close(fences[n]);
579 free(fences);
580
581 igt_info("Signal %s: %'lu cycles (%'lu signals): %.3fus\n",
582 ring_name, count, signal, elapsed(&start, &now) * 1e6 / count);
583}
584
Chris Wilsonc6e26e42016-07-22 12:58:54 +0100585static void print_welcome(int fd)
586{
587 bool active;
Chris Wilsonc6e26e42016-07-22 12:58:54 +0100588 int dir;
589
590 dir = igt_sysfs_open_parameters(fd);
591 if (dir < 0)
592 return;
593
Chris Wilsonb64d10c2016-07-22 17:53:51 +0100594 active = igt_sysfs_get_boolean(dir, "enable_guc_submission");
Chris Wilsonc6e26e42016-07-22 12:58:54 +0100595 if (active) {
596 igt_info("Using GuC submission\n");
597 goto out;
598 }
599
Chris Wilsonb64d10c2016-07-22 17:53:51 +0100600 active = igt_sysfs_get_boolean(dir, "enable_execlists");
Chris Wilsonc6e26e42016-07-22 12:58:54 +0100601 if (active) {
602 igt_info("Using Execlists submission\n");
603 goto out;
604 }
605
Chris Wilsonb64d10c2016-07-22 17:53:51 +0100606 active = igt_sysfs_get_boolean(dir, "semaphores");
Chris Wilsonf565b6c2016-09-08 20:59:55 +0100607 igt_info("Using Legacy submission%s\n",
Chris Wilsonc6e26e42016-07-22 12:58:54 +0100608 active ? ", with semaphores" : "");
609
610out:
611 close(dir);
612}
613
Daniel Vetter071e9ca2013-10-31 16:23:26 +0100614igt_main
Daniel Vetterd9d95782012-12-04 17:13:05 +0100615{
Chris Wilson7e0853c2016-01-27 14:17:53 +0000616 const struct intel_execution_engine *e;
Chris Wilson2659cbb2015-03-26 12:09:57 +0000617 uint32_t handle = 0;
Chris Wilson3a7325e2016-03-08 11:43:31 +0000618 int device = -1;
Daniel Vetterd9d95782012-12-04 17:13:05 +0100619
Chris Wilson2659cbb2015-03-26 12:09:57 +0000620 igt_fixture {
Chris Wilson3a7325e2016-03-08 11:43:31 +0000621 const uint32_t bbe = MI_BATCH_BUFFER_END;
622
Micah Fedkec81d2932015-07-22 21:54:02 +0000623 device = drm_open_driver(DRIVER_INTEL);
Chris Wilson9518cb52017-02-22 15:24:54 +0000624 igt_require_gem(device);
Chris Wilsonc6e26e42016-07-22 12:58:54 +0100625 print_welcome(device);
626
Chris Wilson2659cbb2015-03-26 12:09:57 +0000627 handle = gem_create(device, 4096);
Chris Wilson3a7325e2016-03-08 11:43:31 +0000628 gem_write(device, handle, 0, &bbe, sizeof(bbe));
Daniel Vetterd9d95782012-12-04 17:13:05 +0100629
Daniel Vetterbe21fc02016-06-17 16:04:09 +0200630 igt_fork_hang_detector(device);
631 }
Chris Wilson9d61a682016-03-25 18:22:54 +0000632
Chris Wilson4cce7152016-09-08 13:43:17 +0100633 igt_subtest("basic-series")
Chris Wilson69b29f82016-10-18 10:23:49 +0100634 series(device, handle, 5);
Chris Wilson4cce7152016-09-08 13:43:17 +0100635
636 igt_subtest("basic-parallel")
Chris Wilson69b29f82016-10-18 10:23:49 +0100637 parallel(device, handle, 5);
Chris Wilson772393e2016-03-14 14:31:36 +0000638
Chris Wilson817d57f2017-01-20 17:17:42 +0000639 igt_subtest("basic-sequential")
Chris Wilson9fbf72b2017-01-22 18:24:37 +0000640 sequential(device, handle, 0, 5);
Chris Wilson817d57f2017-01-20 17:17:42 +0000641
Chris Wilsone10c48d2017-05-12 12:33:32 +0100642 for (e = intel_execution_engines; e->name; e++) {
Chris Wilson7e0853c2016-01-27 14:17:53 +0000643 igt_subtest_f("%s", e->name)
Chris Wilson3a7325e2016-03-08 11:43:31 +0000644 single(device, handle, e->exec_id | e->flags, e->name);
Chris Wilsone10c48d2017-05-12 12:33:32 +0100645 igt_subtest_f("signal-%s", e->name)
Arkadiusz Hiler09894782017-05-16 12:59:50 +0200646 fence_signal(device, handle, e->exec_id | e->flags, e->name, 5);
Chris Wilsone10c48d2017-05-12 12:33:32 +0100647 }
648
649 igt_subtest("signal-all")
Arkadiusz Hiler09894782017-05-16 12:59:50 +0200650 fence_signal(device, handle, -1, "all", 150);
Chris Wilson3a7325e2016-03-08 11:43:31 +0000651
Chris Wilson4cce7152016-09-08 13:43:17 +0100652 igt_subtest("series")
653 series(device, handle, 150);
654
655 igt_subtest("parallel")
656 parallel(device, handle, 150);
Daniel Vetterd9d95782012-12-04 17:13:05 +0100657
Chris Wilson817d57f2017-01-20 17:17:42 +0000658 igt_subtest("sequential")
Chris Wilson9fbf72b2017-01-22 18:24:37 +0000659 sequential(device, handle, 0, 150);
660
661 igt_subtest("forked-sequential")
662 sequential(device, handle, FORKED, 150);
663
664 igt_subtest("chained-sequential")
665 sequential(device, handle, FORKED | CHAINED, 150);
666
667 igt_subtest("context-sequential")
668 sequential(device, handle, FORKED | CONTEXT, 150);
Chris Wilson817d57f2017-01-20 17:17:42 +0000669
Arkadiusz Hiler21bfc9f2017-04-19 12:50:06 +0200670#if !defined(ANDROID) || ANDROID_HAS_CAIRO
Tvrtko Ursulin8e4cfb22017-04-13 14:11:05 +0100671 igt_subtest("headless")
672 headless(device, handle);
Arkadiusz Hiler21bfc9f2017-04-19 12:50:06 +0200673#endif
Tvrtko Ursulin8e4cfb22017-04-13 14:11:05 +0100674
Daniel Vetterb3880d32013-08-14 18:02:46 +0200675 igt_fixture {
Daniel Vetterbe21fc02016-06-17 16:04:09 +0200676 igt_stop_hang_detector();
Chris Wilson2659cbb2015-03-26 12:09:57 +0000677 gem_close(device, handle);
Chris Wilson2659cbb2015-03-26 12:09:57 +0000678 close(device);
Daniel Vetterb3880d32013-08-14 18:02:46 +0200679 }
Chris Wilson07d59b32011-01-20 22:10:10 +0000680}