blob: feb89ee37b131412b5640a4b9bccfe0662840c55 [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 Wilsoncf3e6832017-09-28 18:24:26 +010047#define BIT(x) (1ul << (x))
48
Chris Wilsoncd8d3802015-03-24 09:15:12 +000049#define LOCAL_I915_EXEC_NO_RELOC (1<<11)
50#define LOCAL_I915_EXEC_HANDLE_LUT (1<<12)
51
Chris Wilson3a7325e2016-03-08 11:43:31 +000052#define LOCAL_I915_EXEC_BSD_SHIFT (13)
53#define LOCAL_I915_EXEC_BSD_MASK (3 << LOCAL_I915_EXEC_BSD_SHIFT)
Daniel Vetter51f08302012-12-05 19:29:11 +010054
Chris Wilson3a7325e2016-03-08 11:43:31 +000055#define ENGINE_FLAGS (I915_EXEC_RING_MASK | LOCAL_I915_EXEC_BSD_MASK)
Chris Wilson2659cbb2015-03-26 12:09:57 +000056
Chris Wilsoncf3e6832017-09-28 18:24:26 +010057#define LOCAL_PARAM_HAS_SCHEDULER 41
58#define HAS_SCHEDULER BIT(0)
59#define HAS_PRIORITY BIT(1)
60#define HAS_PREEMPTION BIT(2)
61#define LOCAL_CONTEXT_PARAM_PRIORITY 6
62#define MAX_PRIO 1023
63#define MIN_PRIO -1023
64
Chris Wilson9fbf72b2017-01-22 18:24:37 +000065#define FORKED 1
66#define CHAINED 2
67#define CONTEXT 4
68
Chris Wilson3a7325e2016-03-08 11:43:31 +000069static double elapsed(const struct timespec *start, const struct timespec *end)
70{
71 return ((end->tv_sec - start->tv_sec) +
72 (end->tv_nsec - start->tv_nsec)*1e-9);
73}
74
Chris Wilson870c7742016-03-28 15:29:46 +010075static double nop_on_ring(int fd, uint32_t handle, unsigned ring_id,
76 int timeout, unsigned long *out)
Chris Wilson07d59b32011-01-20 22:10:10 +000077{
78 struct drm_i915_gem_execbuffer2 execbuf;
Chris Wilson3a7325e2016-03-08 11:43:31 +000079 struct drm_i915_gem_exec_object2 obj;
80 struct timespec start, now;
Chris Wilson870c7742016-03-28 15:29:46 +010081 unsigned long count;
Daniel Vetter8f5387e2013-08-13 13:20:58 +020082
Chris Wilson3a7325e2016-03-08 11:43:31 +000083 memset(&obj, 0, sizeof(obj));
84 obj.handle = handle;
Chris Wilson07d59b32011-01-20 22:10:10 +000085
Chris Wilsoncd8d3802015-03-24 09:15:12 +000086 memset(&execbuf, 0, sizeof(execbuf));
Chris Wilson4de67b22017-01-02 11:05:21 +000087 execbuf.buffers_ptr = to_user_pointer(&obj);
Chris Wilsoncd8d3802015-03-24 09:15:12 +000088 execbuf.buffer_count = 1;
89 execbuf.flags = ring_id;
90 execbuf.flags |= LOCAL_I915_EXEC_HANDLE_LUT;
91 execbuf.flags |= LOCAL_I915_EXEC_NO_RELOC;
Chris Wilson3e2443f2016-03-10 11:50:53 +000092 if (__gem_execbuf(fd, &execbuf)) {
Chris Wilsoncd8d3802015-03-24 09:15:12 +000093 execbuf.flags = ring_id;
Chris Wilson3a7325e2016-03-08 11:43:31 +000094 gem_execbuf(fd, &execbuf);
Chris Wilsoncd8d3802015-03-24 09:15:12 +000095 }
Chris Wilson71f41532016-04-22 16:55:29 +010096 intel_detect_and_clear_missed_interrupts(fd);
Chris Wilsoncd8d3802015-03-24 09:15:12 +000097
Chris Wilson870c7742016-03-28 15:29:46 +010098 count = 0;
Chris Wilson3a7325e2016-03-08 11:43:31 +000099 clock_gettime(CLOCK_MONOTONIC, &start);
100 do {
Chris Wilson870c7742016-03-28 15:29:46 +0100101 for (int loop = 0; loop < 1024; loop++)
Chris Wilson3a7325e2016-03-08 11:43:31 +0000102 gem_execbuf(fd, &execbuf);
Chris Wilson870c7742016-03-28 15:29:46 +0100103
104 count += 1024;
Chris Wilson3a7325e2016-03-08 11:43:31 +0000105 clock_gettime(CLOCK_MONOTONIC, &now);
Chris Wilson870c7742016-03-28 15:29:46 +0100106 } while (elapsed(&start, &now) < timeout);
Chris Wilson71f41532016-04-22 16:55:29 +0100107 igt_assert_eq(intel_detect_and_clear_missed_interrupts(fd), 0);
Chris Wilson3a7325e2016-03-08 11:43:31 +0000108
Chris Wilson870c7742016-03-28 15:29:46 +0100109 *out = count;
110 return elapsed(&start, &now);
111}
112
113static void single(int fd, uint32_t handle,
114 unsigned ring_id, const char *ring_name)
115{
116 double time;
117 unsigned long count;
118
119 gem_require_ring(fd, ring_id);
120
121 time = nop_on_ring(fd, handle, ring_id, 20, &count);
122 igt_info("%s: %'lu cycles: %.3fus\n",
123 ring_name, count, time*1e6 / count);
Chris Wilson3a7325e2016-03-08 11:43:31 +0000124}
125
Tvrtko Ursulin8e4cfb22017-04-13 14:11:05 +0100126static double
127stable_nop_on_ring(int fd, uint32_t handle, unsigned int engine,
128 int timeout, int reps)
129{
130 igt_stats_t s;
131 double n;
132
133 igt_assert(reps >= 5);
134
135 igt_stats_init_with_size(&s, reps);
136 s.is_float = true;
137
138 while (reps--) {
139 unsigned long count;
140 double time;
141
142 time = nop_on_ring(fd, handle, engine, timeout, &count);
143 igt_stats_push_float(&s, time / count);
144 }
145
146 n = igt_stats_get_median(&s);
147 igt_stats_fini(&s);
148
149 return n;
150}
151
Arkadiusz Hiler21bfc9f2017-04-19 12:50:06 +0200152#if !defined(ANDROID) || ANDROID_HAS_CAIRO
Tvrtko Ursulin8e4cfb22017-04-13 14:11:05 +0100153#define assert_within_epsilon(x, ref, tolerance) \
154 igt_assert_f((x) <= (1.0 + tolerance) * ref && \
155 (x) >= (1.0 - tolerance) * ref, \
156 "'%s' != '%s' (%f not within %f%% tolerance of %f)\n",\
157 #x, #ref, x, tolerance * 100.0, ref)
158
159static void headless(int fd, uint32_t handle)
160{
161 unsigned int nr_connected = 0;
162 drmModeConnector *connector;
163 drmModeRes *res;
164 double n_display, n_headless;
165
166 res = drmModeGetResources(fd);
167 igt_assert(res);
168
169 /* require at least one connected connector for the test */
170 for (int i = 0; i < res->count_connectors; i++) {
171 connector = drmModeGetConnectorCurrent(fd, res->connectors[i]);
172 if (connector->connection == DRM_MODE_CONNECTED)
173 nr_connected++;
174 drmModeFreeConnector(connector);
175 }
176 igt_require(nr_connected > 0);
177
178 /* set graphics mode to prevent blanking */
179 kmstest_set_vt_graphics_mode();
180
181 /* benchmark nops */
182 n_display = stable_nop_on_ring(fd, handle, I915_EXEC_DEFAULT, 1, 5);
Chris Wilsonf6a31ad2017-04-19 13:38:32 +0100183 igt_info("With one display connected: %.2fus\n",
184 n_display * 1e6);
Tvrtko Ursulin8e4cfb22017-04-13 14:11:05 +0100185
186 /* force all connectors off */
187 kmstest_unset_all_crtcs(fd, res);
188
189 /* benchmark nops again */
190 n_headless = stable_nop_on_ring(fd, handle, I915_EXEC_DEFAULT, 1, 5);
Chris Wilsonf6a31ad2017-04-19 13:38:32 +0100191 igt_info("Without a display connected (headless): %.2fus\n",
192 n_headless * 1e6);
Tvrtko Ursulin8e4cfb22017-04-13 14:11:05 +0100193
194 /* check that the two execution speeds are roughly the same */
195 assert_within_epsilon(n_headless, n_display, 0.1f);
196}
Arkadiusz Hiler21bfc9f2017-04-19 12:50:06 +0200197#endif
Tvrtko Ursulin8e4cfb22017-04-13 14:11:05 +0100198
Chris Wilson0aacdac2016-03-09 21:06:16 +0000199static bool ignore_engine(int fd, unsigned engine)
200{
201 if (engine == 0)
202 return true;
203
204 if (gem_has_bsd2(fd) && engine == I915_EXEC_BSD)
205 return true;
206
207 return false;
208}
209
Chris Wilson4cce7152016-09-08 13:43:17 +0100210static void parallel(int fd, uint32_t handle, int timeout)
211{
212 struct drm_i915_gem_execbuffer2 execbuf;
213 struct drm_i915_gem_exec_object2 obj;
Chris Wilson4cce7152016-09-08 13:43:17 +0100214 unsigned engines[16];
215 const char *names[16];
216 unsigned nengine;
217 unsigned engine;
218 unsigned long count;
219 double time, sum;
220
221 sum = 0;
222 nengine = 0;
223 for_each_engine(fd, engine) {
224 if (ignore_engine(fd, engine))
225 continue;
226
227 engines[nengine] = engine;
228 names[nengine] = e__->name;
229 nengine++;
230
231 time = nop_on_ring(fd, handle, engine, 1, &count) / count;
232 sum += time;
233 igt_debug("%s: %.3fus\n", e__->name, 1e6*time);
234 }
235 igt_require(nengine);
236 igt_info("average (individually): %.3fus\n", sum/nengine*1e6);
237
238 memset(&obj, 0, sizeof(obj));
239 obj.handle = handle;
240
241 memset(&execbuf, 0, sizeof(execbuf));
Chris Wilson4de67b22017-01-02 11:05:21 +0000242 execbuf.buffers_ptr = to_user_pointer(&obj);
Chris Wilson4cce7152016-09-08 13:43:17 +0100243 execbuf.buffer_count = 1;
244 execbuf.flags |= LOCAL_I915_EXEC_HANDLE_LUT;
245 execbuf.flags |= LOCAL_I915_EXEC_NO_RELOC;
246 if (__gem_execbuf(fd, &execbuf)) {
247 execbuf.flags = 0;
248 gem_execbuf(fd, &execbuf);
249 }
Chris Wilson4cce7152016-09-08 13:43:17 +0100250 intel_detect_and_clear_missed_interrupts(fd);
251
252 igt_fork(child, nengine) {
Chris Wilsonf565b6c2016-09-08 20:59:55 +0100253 struct timespec start, now;
254
Chris Wilson4cce7152016-09-08 13:43:17 +0100255 execbuf.flags &= ~ENGINE_FLAGS;
256 execbuf.flags |= engines[child];
257
258 count = 0;
259 clock_gettime(CLOCK_MONOTONIC, &start);
260 do {
261 for (int loop = 0; loop < 1024; loop++)
262 gem_execbuf(fd, &execbuf);
263 count += 1024;
264 clock_gettime(CLOCK_MONOTONIC, &now);
265 } while (elapsed(&start, &now) < timeout);
Chris Wilson4cce7152016-09-08 13:43:17 +0100266 time = elapsed(&start, &now) / count;
267 igt_info("%s: %ld cycles, %.3fus\n", names[child], count, 1e6*time);
268 }
269
270 igt_waitchildren();
271 igt_assert_eq(intel_detect_and_clear_missed_interrupts(fd), 0);
272
273}
274
275static void series(int fd, uint32_t handle, int timeout)
Chris Wilson3a7325e2016-03-08 11:43:31 +0000276{
277 struct drm_i915_gem_execbuffer2 execbuf;
278 struct drm_i915_gem_exec_object2 obj;
Chris Wilsonf565b6c2016-09-08 20:59:55 +0100279 struct timespec start, now, sync;
Chris Wilson3a7325e2016-03-08 11:43:31 +0000280 unsigned engines[16];
281 unsigned nengine;
282 unsigned engine;
Chris Wilson870c7742016-03-28 15:29:46 +0100283 unsigned long count;
Chris Wilson41a26b52016-03-28 16:26:01 +0100284 double time, max = 0, min = HUGE_VAL, sum = 0;
Chris Wilson870c7742016-03-28 15:29:46 +0100285 const char *name;
Chris Wilson3a7325e2016-03-08 11:43:31 +0000286
287 nengine = 0;
Chris Wilson870c7742016-03-28 15:29:46 +0100288 for_each_engine(fd, engine) {
289 if (ignore_engine(fd, engine))
290 continue;
291
292 time = nop_on_ring(fd, handle, engine, 1, &count) / count;
293 if (time > max) {
294 name = e__->name;
295 max = time;
296 }
Chris Wilson41a26b52016-03-28 16:26:01 +0100297 if (time < min)
298 min = time;
Chris Wilson870c7742016-03-28 15:29:46 +0100299 sum += time;
300 engines[nengine++] = engine;
301 }
Chris Wilson0aacdac2016-03-09 21:06:16 +0000302 igt_require(nengine);
Chris Wilsonf565b6c2016-09-08 20:59:55 +0100303 igt_info("Maximum execution latency on %s, %.3fus, min %.3fus, total %.3fus per cycle, average %.3fus\n",
304 name, max*1e6, min*1e6, sum*1e6, sum/nengine*1e6);
Chris Wilson3a7325e2016-03-08 11:43:31 +0000305
306 memset(&obj, 0, sizeof(obj));
307 obj.handle = handle;
308
309 memset(&execbuf, 0, sizeof(execbuf));
Chris Wilson4de67b22017-01-02 11:05:21 +0000310 execbuf.buffers_ptr = to_user_pointer(&obj);
Chris Wilson3a7325e2016-03-08 11:43:31 +0000311 execbuf.buffer_count = 1;
312 execbuf.flags |= LOCAL_I915_EXEC_HANDLE_LUT;
313 execbuf.flags |= LOCAL_I915_EXEC_NO_RELOC;
Chris Wilson3e2443f2016-03-10 11:50:53 +0000314 if (__gem_execbuf(fd, &execbuf)) {
Chris Wilson3a7325e2016-03-08 11:43:31 +0000315 execbuf.flags = 0;
316 gem_execbuf(fd, &execbuf);
Chris Wilson07d59b32011-01-20 22:10:10 +0000317 }
Chris Wilson71f41532016-04-22 16:55:29 +0100318 intel_detect_and_clear_missed_interrupts(fd);
Chris Wilson3a7325e2016-03-08 11:43:31 +0000319
Chris Wilson870c7742016-03-28 15:29:46 +0100320 count = 0;
Chris Wilson3a7325e2016-03-08 11:43:31 +0000321 clock_gettime(CLOCK_MONOTONIC, &start);
322 do {
323 for (int loop = 0; loop < 1024; loop++) {
324 for (int n = 0; n < nengine; n++) {
325 execbuf.flags &= ~ENGINE_FLAGS;
326 execbuf.flags |= engines[n];
327 gem_execbuf(fd, &execbuf);
328 }
329 }
330 count += nengine * 1024;
331 clock_gettime(CLOCK_MONOTONIC, &now);
Chris Wilson772393e2016-03-14 14:31:36 +0000332 } while (elapsed(&start, &now) < timeout); /* Hang detection ~120s */
Chris Wilson3a7325e2016-03-08 11:43:31 +0000333 gem_sync(fd, handle);
Chris Wilsonf565b6c2016-09-08 20:59:55 +0100334 clock_gettime(CLOCK_MONOTONIC, &sync);
335 igt_debug("sync time: %.3fus\n", elapsed(&now, &sync)*1e6);
Chris Wilson71f41532016-04-22 16:55:29 +0100336 igt_assert_eq(intel_detect_and_clear_missed_interrupts(fd), 0);
Chris Wilson3a7325e2016-03-08 11:43:31 +0000337
Chris Wilson870c7742016-03-28 15:29:46 +0100338 time = elapsed(&start, &now) / count;
Chris Wilsonf565b6c2016-09-08 20:59:55 +0100339 igt_info("All (%d engines): %'lu cycles, average %.3fus per cycle [expected %.3fus]\n",
Chris Wilson61b19a42016-09-08 14:50:32 +0100340 nengine, count, 1e6*time, 1e6*((max-min)/nengine+min));
Chris Wilson41a26b52016-03-28 16:26:01 +0100341
Chris Wilsona0eebbd2016-09-08 13:29:31 +0100342 /* The rate limiting step should be how fast the slowest engine can
343 * execute its queue of requests, as when we wait upon a full ring all
344 * dispatch is frozen. So in general we cannot go faster than the
345 * slowest engine (but as all engines are in lockstep, they should all
346 * be executing in parallel and so the average should be max/nengines),
347 * but we should equally not go any slower.
348 *
349 * However, that depends upon being able to submit fast enough, and
350 * that in turns depends upon debugging turned off and no bottlenecks
351 * within the driver. We cannot assert that we hit ideal conditions
352 * across all engines, so we only look for an outrageous error
353 * condition.
Chris Wilson41a26b52016-03-28 16:26:01 +0100354 */
Chris Wilsona0eebbd2016-09-08 13:29:31 +0100355 igt_assert_f(time < 2*sum,
356 "Average time (%.3fus) exceeds expectation for parallel execution (min %.3fus, max %.3fus; limit set at %.3fus)\n",
357 1e6*time, 1e6*min, 1e6*max, 1e6*sum*2);
Daniel Vetterd9d95782012-12-04 17:13:05 +0100358}
Daniel Vetter8f5387e2013-08-13 13:20:58 +0200359
Chris Wilson9fbf72b2017-01-22 18:24:37 +0000360static void xchg(void *array, unsigned i, unsigned j)
Chris Wilson817d57f2017-01-20 17:17:42 +0000361{
Chris Wilson9fbf72b2017-01-22 18:24:37 +0000362 unsigned *u = array;
363 unsigned tmp = u[i];
364 u[i] = u[j];
365 u[j] = tmp;
366}
367
368static int __gem_context_create(int fd, uint32_t *ctx_id)
369{
370 struct drm_i915_gem_context_create arg;
371 int ret = 0;
372
373 memset(&arg, 0, sizeof(arg));
374 if (drmIoctl(fd, DRM_IOCTL_I915_GEM_CONTEXT_CREATE, &arg))
375 ret = -errno;
376
377 *ctx_id = arg.ctx_id;
378 return ret;
379}
Chris Wilsone10c48d2017-05-12 12:33:32 +0100380
Chris Wilson9fbf72b2017-01-22 18:24:37 +0000381static void sequential(int fd, uint32_t handle, unsigned flags, int timeout)
382{
383 const int ncpus = flags & FORKED ? sysconf(_SC_NPROCESSORS_ONLN) : 1;
Chris Wilson817d57f2017-01-20 17:17:42 +0000384 struct drm_i915_gem_execbuffer2 execbuf;
385 struct drm_i915_gem_exec_object2 obj[2];
Chris Wilson817d57f2017-01-20 17:17:42 +0000386 unsigned engines[16];
387 unsigned nengine;
Chris Wilson9fbf72b2017-01-22 18:24:37 +0000388 double *results;
Chris Wilson817d57f2017-01-20 17:17:42 +0000389 double time, sum;
Chris Wilson9fbf72b2017-01-22 18:24:37 +0000390 unsigned n;
391
392 results = mmap(NULL, 4096, PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);
393 igt_assert(results != MAP_FAILED);
Chris Wilson817d57f2017-01-20 17:17:42 +0000394
395 nengine = 0;
396 sum = 0;
Chris Wilson9fbf72b2017-01-22 18:24:37 +0000397 for_each_engine(fd, n) {
398 unsigned long count;
399
400 if (ignore_engine(fd, n))
Chris Wilson817d57f2017-01-20 17:17:42 +0000401 continue;
402
Chris Wilson9fbf72b2017-01-22 18:24:37 +0000403 time = nop_on_ring(fd, handle, n, 1, &count) / count;
Chris Wilson817d57f2017-01-20 17:17:42 +0000404 sum += time;
405 igt_debug("%s: %.3fus\n", e__->name, 1e6*time);
406
Chris Wilson9fbf72b2017-01-22 18:24:37 +0000407 engines[nengine++] = n;
Chris Wilson817d57f2017-01-20 17:17:42 +0000408 }
409 igt_require(nengine);
410 igt_info("Total (individual) execution latency %.3fus per cycle\n",
411 1e6*sum);
412
413 memset(obj, 0, sizeof(obj));
414 obj[0].handle = gem_create(fd, 4096);
415 obj[0].flags = EXEC_OBJECT_WRITE;
416 obj[1].handle = handle;
417
418 memset(&execbuf, 0, sizeof(execbuf));
419 execbuf.buffers_ptr = to_user_pointer(obj);
420 execbuf.buffer_count = 2;
421 execbuf.flags |= LOCAL_I915_EXEC_HANDLE_LUT;
422 execbuf.flags |= LOCAL_I915_EXEC_NO_RELOC;
423 igt_require(__gem_execbuf(fd, &execbuf) == 0);
424
Chris Wilson9fbf72b2017-01-22 18:24:37 +0000425 if (flags & CONTEXT) {
426 uint32_t id;
427
428 igt_require(__gem_context_create(fd, &id) == 0);
429 execbuf.rsvd1 = id;
430 }
431
432 for (n = 0; n < nengine; n++) {
433 execbuf.flags &= ~ENGINE_FLAGS;
434 execbuf.flags |= engines[n];
435 igt_require(__gem_execbuf(fd, &execbuf) == 0);
436 }
437
Chris Wilson817d57f2017-01-20 17:17:42 +0000438 intel_detect_and_clear_missed_interrupts(fd);
439
Chris Wilson9fbf72b2017-01-22 18:24:37 +0000440 igt_fork(child, ncpus) {
441 struct timespec start, now;
442 unsigned long count;
443
444 obj[0].handle = gem_create(fd, 4096);
445 gem_execbuf(fd, &execbuf);
446
447 if (flags & CONTEXT)
448 execbuf.rsvd1 = gem_context_create(fd);
449
450 hars_petruska_f54_1_random_perturb(child);
451
452 count = 0;
453 clock_gettime(CLOCK_MONOTONIC, &start);
454 do {
455 igt_permute_array(engines, nengine, xchg);
456 if (flags & CHAINED) {
457 for (n = 0; n < nengine; n++) {
458 execbuf.flags &= ~ENGINE_FLAGS;
459 execbuf.flags |= engines[n];
460 for (int loop = 0; loop < 1024; loop++)
461 gem_execbuf(fd, &execbuf);
462 }
463 } else {
464 for (int loop = 0; loop < 1024; loop++) {
465 for (n = 0; n < nengine; n++) {
466 execbuf.flags &= ~ENGINE_FLAGS;
467 execbuf.flags |= engines[n];
468 gem_execbuf(fd, &execbuf);
469 }
470 }
Chris Wilson817d57f2017-01-20 17:17:42 +0000471 }
Chris Wilson9fbf72b2017-01-22 18:24:37 +0000472 count += 1024;
473 clock_gettime(CLOCK_MONOTONIC, &now);
474 } while (elapsed(&start, &now) < timeout); /* Hang detection ~120s */
Chris Wilsone7a0d062017-03-21 13:12:07 +0000475
476 gem_sync(fd, obj[0].handle);
477 clock_gettime(CLOCK_MONOTONIC, &now);
Chris Wilson9fbf72b2017-01-22 18:24:37 +0000478 results[child] = elapsed(&start, &now) / count;
479
480 if (flags & CONTEXT)
481 gem_context_destroy(fd, execbuf.rsvd1);
482
483 gem_close(fd, obj[0].handle);
484 }
485 igt_waitchildren();
Chris Wilson817d57f2017-01-20 17:17:42 +0000486 igt_assert_eq(intel_detect_and_clear_missed_interrupts(fd), 0);
487
Chris Wilson9fbf72b2017-01-22 18:24:37 +0000488 results[ncpus] = 0;
489 for (n = 0; n < ncpus; n++)
490 results[ncpus] += results[n];
491 results[ncpus] /= ncpus;
492
493 igt_info("Sequential (%d engines, %d processes): average %.3fus per cycle [expected %.3fus]\n",
494 nengine, ncpus, 1e6*results[ncpus], 1e6*sum*ncpus);
495
496 if (flags & CONTEXT)
497 gem_context_destroy(fd, execbuf.rsvd1);
Chris Wilson817d57f2017-01-20 17:17:42 +0000498
499 gem_close(fd, obj[0].handle);
Chris Wilson9fbf72b2017-01-22 18:24:37 +0000500 munmap(results, 4096);
Chris Wilson817d57f2017-01-20 17:17:42 +0000501}
502
Chris Wilsone10c48d2017-05-12 12:33:32 +0100503#define LOCAL_EXEC_FENCE_OUT (1 << 17)
Chris Wilsone10c48d2017-05-12 12:33:32 +0100504static bool fence_enable_signaling(int fence)
505{
506 return poll(&(struct pollfd){fence, POLLIN}, 1, 0) == 0;
507}
508
509static bool fence_wait(int fence)
510{
511 return poll(&(struct pollfd){fence, POLLIN}, 1, -1) == 1;
512}
513
Arkadiusz Hiler09894782017-05-16 12:59:50 +0200514static void fence_signal(int fd, uint32_t handle,
515 unsigned ring_id, const char *ring_name,
516 int timeout)
Chris Wilsone10c48d2017-05-12 12:33:32 +0100517{
518#define NFENCES 512
519 struct drm_i915_gem_execbuffer2 execbuf;
520 struct drm_i915_gem_exec_object2 obj;
521 struct timespec start, now;
522 unsigned engines[16];
523 unsigned nengine;
524 int *fences, n;
525 unsigned long count, signal;
526
527 igt_require(gem_has_exec_fence(fd));
528
529 nengine = 0;
530 if (ring_id == -1) {
531 for_each_engine(fd, n) {
532 if (ignore_engine(fd, n))
533 continue;
534
535 engines[nengine++] = n;
536 }
537 } else {
Chris Wilson842e6a42017-05-13 13:27:52 +0100538 gem_require_ring(fd, ring_id);
Chris Wilsone10c48d2017-05-12 12:33:32 +0100539 engines[nengine++] = ring_id;
540 }
541 igt_require(nengine);
542
543 fences = malloc(sizeof(*fences) * NFENCES);
544 igt_assert(fences);
545 memset(fences, -1, sizeof(*fences) * NFENCES);
546
547 memset(&obj, 0, sizeof(obj));
548 obj.handle = handle;
549
550 memset(&execbuf, 0, sizeof(execbuf));
551 execbuf.buffers_ptr = to_user_pointer(&obj);
552 execbuf.buffer_count = 1;
553 execbuf.flags = LOCAL_EXEC_FENCE_OUT;
554
555 n = 0;
556 count = 0;
557 signal = 0;
558
559 intel_detect_and_clear_missed_interrupts(fd);
560 clock_gettime(CLOCK_MONOTONIC, &start);
561 do {
562 for (int loop = 0; loop < 1024; loop++) {
563 for (int e = 0; e < nengine; e++) {
564 if (fences[n] != -1) {
565 igt_assert(fence_wait(fences[n]));
566 close(fences[n]);
567 }
568
569 execbuf.flags &= ~ENGINE_FLAGS;
570 execbuf.flags |= engines[e];
571 gem_execbuf_wr(fd, &execbuf);
572
573 /* Enable signaling by doing a poll() */
574 fences[n] = execbuf.rsvd2 >> 32;
575 signal += fence_enable_signaling(fences[n]);
576
577 n = (n + 1) % NFENCES;
578 }
579 }
580
581 count += 1024 * nengine;
582 clock_gettime(CLOCK_MONOTONIC, &now);
583 } while (elapsed(&start, &now) < timeout);
584 igt_assert_eq(intel_detect_and_clear_missed_interrupts(fd), 0);
585
586 for (n = 0; n < NFENCES; n++)
587 if (fences[n] != -1)
588 close(fences[n]);
589 free(fences);
590
591 igt_info("Signal %s: %'lu cycles (%'lu signals): %.3fus\n",
592 ring_name, count, signal, elapsed(&start, &now) * 1e6 / count);
593}
Chris Wilsoncf3e6832017-09-28 18:24:26 +0100594static int __ctx_set_priority(int fd, uint32_t ctx, int prio)
595{
596 struct local_i915_gem_context_param param;
597
598 memset(&param, 0, sizeof(param));
599 param.context = ctx;
600 param.size = 0;
601 param.param = LOCAL_CONTEXT_PARAM_PRIORITY;
602 param.value = prio;
603
604 return __gem_context_set_param(fd, &param);
605}
606
607static void ctx_set_priority(int fd, uint32_t ctx, int prio)
608{
609 igt_assert_eq(__ctx_set_priority(fd, ctx, prio), 0);
610}
611
612static void preempt(int fd, uint32_t handle,
613 unsigned ring_id, const char *ring_name)
614{
615 struct drm_i915_gem_execbuffer2 execbuf;
616 struct drm_i915_gem_exec_object2 obj;
617 struct timespec start, now;
618 unsigned long count;
619 uint32_t ctx[2];
620
621 gem_require_ring(fd, ring_id);
622
623 ctx[0] = gem_context_create(fd);
624 ctx_set_priority(fd, ctx[0], MIN_PRIO);
625
626 ctx[1] = gem_context_create(fd);
627 ctx_set_priority(fd, ctx[1], MAX_PRIO);
628
629 memset(&obj, 0, sizeof(obj));
630 obj.handle = handle;
631
632 memset(&execbuf, 0, sizeof(execbuf));
633 execbuf.buffers_ptr = to_user_pointer(&obj);
634 execbuf.buffer_count = 1;
635 execbuf.flags = ring_id;
636 execbuf.flags |= LOCAL_I915_EXEC_HANDLE_LUT;
637 execbuf.flags |= LOCAL_I915_EXEC_NO_RELOC;
638 if (__gem_execbuf(fd, &execbuf)) {
639 execbuf.flags = ring_id;
640 gem_execbuf(fd, &execbuf);
641 }
642 execbuf.rsvd1 = ctx[1];
643 intel_detect_and_clear_missed_interrupts(fd);
644
645 count = 0;
646 clock_gettime(CLOCK_MONOTONIC, &start);
647 do {
648 igt_spin_t *spin =
649 __igt_spin_batch_new(fd, ctx[0], ring_id, 0);
650
651 for (int loop = 0; loop < 1024; loop++)
652 gem_execbuf(fd, &execbuf);
653
654 igt_spin_batch_free(fd, spin);
655
656 count += 1024;
657 clock_gettime(CLOCK_MONOTONIC, &now);
658 } while (elapsed(&start, &now) < 20);
659 igt_assert_eq(intel_detect_and_clear_missed_interrupts(fd), 0);
660
661 gem_context_destroy(fd, ctx[1]);
662 gem_context_destroy(fd, ctx[0]);
663
664 igt_info("%s: %'lu cycles: %.3fus\n",
665 ring_name, count, elapsed(&start, &now)*1e6 / count);
666}
Chris Wilsone10c48d2017-05-12 12:33:32 +0100667
Chris Wilsoncf3e6832017-09-28 18:24:26 +0100668static unsigned int has_scheduler(int fd)
669{
670 drm_i915_getparam_t gp;
671 unsigned int caps = 0;
672
673 gp.param = LOCAL_PARAM_HAS_SCHEDULER;
674 gp.value = (int *)&caps;
675 drmIoctl(fd, DRM_IOCTL_I915_GETPARAM, &gp);
676
677 if (!caps)
678 return 0;
679
680 igt_info("Has kernel scheduler\n");
681 if (caps & HAS_PRIORITY)
682 igt_info(" - With priority sorting\n");
683 if (caps & HAS_PREEMPTION)
684 igt_info(" - With preemption enabled\n");
685
686 return caps;
687}
688
Daniel Vetter071e9ca2013-10-31 16:23:26 +0100689igt_main
Daniel Vetterd9d95782012-12-04 17:13:05 +0100690{
Chris Wilson7e0853c2016-01-27 14:17:53 +0000691 const struct intel_execution_engine *e;
Chris Wilsoncf3e6832017-09-28 18:24:26 +0100692 unsigned int sched_caps = 0;
Chris Wilson2659cbb2015-03-26 12:09:57 +0000693 uint32_t handle = 0;
Chris Wilson3a7325e2016-03-08 11:43:31 +0000694 int device = -1;
Daniel Vetterd9d95782012-12-04 17:13:05 +0100695
Chris Wilson2659cbb2015-03-26 12:09:57 +0000696 igt_fixture {
Chris Wilson3a7325e2016-03-08 11:43:31 +0000697 const uint32_t bbe = MI_BATCH_BUFFER_END;
698
Micah Fedkec81d2932015-07-22 21:54:02 +0000699 device = drm_open_driver(DRIVER_INTEL);
Chris Wilson9518cb52017-02-22 15:24:54 +0000700 igt_require_gem(device);
Michał Winiarskif6dfe552017-10-16 11:05:14 +0200701 gem_show_submission_method(device);
Chris Wilsoncf3e6832017-09-28 18:24:26 +0100702 sched_caps = has_scheduler(device);
Chris Wilsonc6e26e42016-07-22 12:58:54 +0100703
Chris Wilson2659cbb2015-03-26 12:09:57 +0000704 handle = gem_create(device, 4096);
Chris Wilson3a7325e2016-03-08 11:43:31 +0000705 gem_write(device, handle, 0, &bbe, sizeof(bbe));
Daniel Vetterd9d95782012-12-04 17:13:05 +0100706
Daniel Vetterbe21fc02016-06-17 16:04:09 +0200707 igt_fork_hang_detector(device);
708 }
Chris Wilson9d61a682016-03-25 18:22:54 +0000709
Chris Wilson4cce7152016-09-08 13:43:17 +0100710 igt_subtest("basic-series")
Chris Wilson69b29f82016-10-18 10:23:49 +0100711 series(device, handle, 5);
Chris Wilson4cce7152016-09-08 13:43:17 +0100712
713 igt_subtest("basic-parallel")
Chris Wilson69b29f82016-10-18 10:23:49 +0100714 parallel(device, handle, 5);
Chris Wilson772393e2016-03-14 14:31:36 +0000715
Chris Wilson817d57f2017-01-20 17:17:42 +0000716 igt_subtest("basic-sequential")
Chris Wilson9fbf72b2017-01-22 18:24:37 +0000717 sequential(device, handle, 0, 5);
Chris Wilson817d57f2017-01-20 17:17:42 +0000718
Chris Wilsone10c48d2017-05-12 12:33:32 +0100719 for (e = intel_execution_engines; e->name; e++) {
Chris Wilson7e0853c2016-01-27 14:17:53 +0000720 igt_subtest_f("%s", e->name)
Chris Wilson3a7325e2016-03-08 11:43:31 +0000721 single(device, handle, e->exec_id | e->flags, e->name);
Chris Wilsone10c48d2017-05-12 12:33:32 +0100722 igt_subtest_f("signal-%s", e->name)
Arkadiusz Hiler09894782017-05-16 12:59:50 +0200723 fence_signal(device, handle, e->exec_id | e->flags, e->name, 5);
Chris Wilsone10c48d2017-05-12 12:33:32 +0100724 }
725
726 igt_subtest("signal-all")
Arkadiusz Hiler09894782017-05-16 12:59:50 +0200727 fence_signal(device, handle, -1, "all", 150);
Chris Wilson3a7325e2016-03-08 11:43:31 +0000728
Chris Wilson4cce7152016-09-08 13:43:17 +0100729 igt_subtest("series")
730 series(device, handle, 150);
731
732 igt_subtest("parallel")
733 parallel(device, handle, 150);
Daniel Vetterd9d95782012-12-04 17:13:05 +0100734
Chris Wilson817d57f2017-01-20 17:17:42 +0000735 igt_subtest("sequential")
Chris Wilson9fbf72b2017-01-22 18:24:37 +0000736 sequential(device, handle, 0, 150);
737
738 igt_subtest("forked-sequential")
739 sequential(device, handle, FORKED, 150);
740
741 igt_subtest("chained-sequential")
742 sequential(device, handle, FORKED | CHAINED, 150);
743
744 igt_subtest("context-sequential")
745 sequential(device, handle, FORKED | CONTEXT, 150);
Chris Wilson817d57f2017-01-20 17:17:42 +0000746
Chris Wilsoncf3e6832017-09-28 18:24:26 +0100747 igt_subtest_group {
748 igt_fixture {
749 igt_require(sched_caps & HAS_PRIORITY);
750 igt_require(sched_caps & HAS_PREEMPTION);
751 }
752
753 for (e = intel_execution_engines; e->name; e++) {
754 igt_subtest_f("preempt-%s", e->name)
755 preempt(device, handle, e->exec_id | e->flags, e->name);
756 }
757 }
758
Arkadiusz Hiler21bfc9f2017-04-19 12:50:06 +0200759#if !defined(ANDROID) || ANDROID_HAS_CAIRO
Tvrtko Ursulin8e4cfb22017-04-13 14:11:05 +0100760 igt_subtest("headless")
761 headless(device, handle);
Arkadiusz Hiler21bfc9f2017-04-19 12:50:06 +0200762#endif
Tvrtko Ursulin8e4cfb22017-04-13 14:11:05 +0100763
Daniel Vetterb3880d32013-08-14 18:02:46 +0200764 igt_fixture {
Daniel Vetterbe21fc02016-06-17 16:04:09 +0200765 igt_stop_hang_detector();
Chris Wilson2659cbb2015-03-26 12:09:57 +0000766 gem_close(device, handle);
Chris Wilson2659cbb2015-03-26 12:09:57 +0000767 close(device);
Daniel Vetterb3880d32013-08-14 18:02:46 +0200768 }
Chris Wilson07d59b32011-01-20 22:10:10 +0000769}