blob: 78dccbbe3b29e987b7f8e957f988c7d8920ac705 [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 Wilsonc6e26e42016-07-22 12:58:54 +010029#include "igt_sysfs.h"
Chris Wilson07d59b32011-01-20 22:10:10 +000030#include <unistd.h>
31#include <stdlib.h>
32#include <stdint.h>
33#include <stdio.h>
34#include <string.h>
Chris Wilson07d59b32011-01-20 22:10:10 +000035#include <fcntl.h>
36#include <inttypes.h>
37#include <errno.h>
38#include <sys/stat.h>
39#include <sys/ioctl.h>
Chris Wilson07d59b32011-01-20 22:10:10 +000040#include <sys/time.h>
Chris Wilsonb4307092015-07-01 13:53:07 +010041#include <time.h>
Chris Wilson07d59b32011-01-20 22:10:10 +000042#include "drm.h"
Chris Wilson07d59b32011-01-20 22:10:10 +000043
Chris Wilsoncd8d3802015-03-24 09:15:12 +000044#define LOCAL_I915_EXEC_NO_RELOC (1<<11)
45#define LOCAL_I915_EXEC_HANDLE_LUT (1<<12)
46
Chris Wilson3a7325e2016-03-08 11:43:31 +000047#define LOCAL_I915_EXEC_BSD_SHIFT (13)
48#define LOCAL_I915_EXEC_BSD_MASK (3 << LOCAL_I915_EXEC_BSD_SHIFT)
Daniel Vetter51f08302012-12-05 19:29:11 +010049
Chris Wilson3a7325e2016-03-08 11:43:31 +000050#define ENGINE_FLAGS (I915_EXEC_RING_MASK | LOCAL_I915_EXEC_BSD_MASK)
Chris Wilson2659cbb2015-03-26 12:09:57 +000051
Chris Wilson3a7325e2016-03-08 11:43:31 +000052static double elapsed(const struct timespec *start, const struct timespec *end)
53{
54 return ((end->tv_sec - start->tv_sec) +
55 (end->tv_nsec - start->tv_nsec)*1e-9);
56}
57
Chris Wilson870c7742016-03-28 15:29:46 +010058static double nop_on_ring(int fd, uint32_t handle, unsigned ring_id,
59 int timeout, unsigned long *out)
Chris Wilson07d59b32011-01-20 22:10:10 +000060{
61 struct drm_i915_gem_execbuffer2 execbuf;
Chris Wilson3a7325e2016-03-08 11:43:31 +000062 struct drm_i915_gem_exec_object2 obj;
63 struct timespec start, now;
Chris Wilson870c7742016-03-28 15:29:46 +010064 unsigned long count;
Daniel Vetter8f5387e2013-08-13 13:20:58 +020065
Chris Wilson3a7325e2016-03-08 11:43:31 +000066 memset(&obj, 0, sizeof(obj));
67 obj.handle = handle;
Chris Wilson07d59b32011-01-20 22:10:10 +000068
Chris Wilsoncd8d3802015-03-24 09:15:12 +000069 memset(&execbuf, 0, sizeof(execbuf));
Chris Wilson4de67b22017-01-02 11:05:21 +000070 execbuf.buffers_ptr = to_user_pointer(&obj);
Chris Wilsoncd8d3802015-03-24 09:15:12 +000071 execbuf.buffer_count = 1;
72 execbuf.flags = ring_id;
73 execbuf.flags |= LOCAL_I915_EXEC_HANDLE_LUT;
74 execbuf.flags |= LOCAL_I915_EXEC_NO_RELOC;
Chris Wilson3e2443f2016-03-10 11:50:53 +000075 if (__gem_execbuf(fd, &execbuf)) {
Chris Wilsoncd8d3802015-03-24 09:15:12 +000076 execbuf.flags = ring_id;
Chris Wilson3a7325e2016-03-08 11:43:31 +000077 gem_execbuf(fd, &execbuf);
Chris Wilsoncd8d3802015-03-24 09:15:12 +000078 }
Chris Wilson71f41532016-04-22 16:55:29 +010079 intel_detect_and_clear_missed_interrupts(fd);
Chris Wilsoncd8d3802015-03-24 09:15:12 +000080
Chris Wilson870c7742016-03-28 15:29:46 +010081 count = 0;
Chris Wilson3a7325e2016-03-08 11:43:31 +000082 clock_gettime(CLOCK_MONOTONIC, &start);
83 do {
Chris Wilson870c7742016-03-28 15:29:46 +010084 for (int loop = 0; loop < 1024; loop++)
Chris Wilson3a7325e2016-03-08 11:43:31 +000085 gem_execbuf(fd, &execbuf);
Chris Wilson870c7742016-03-28 15:29:46 +010086
87 count += 1024;
Chris Wilson3a7325e2016-03-08 11:43:31 +000088 clock_gettime(CLOCK_MONOTONIC, &now);
Chris Wilson870c7742016-03-28 15:29:46 +010089 } while (elapsed(&start, &now) < timeout);
Chris Wilson71f41532016-04-22 16:55:29 +010090 igt_assert_eq(intel_detect_and_clear_missed_interrupts(fd), 0);
Chris Wilson3a7325e2016-03-08 11:43:31 +000091
Chris Wilson870c7742016-03-28 15:29:46 +010092 *out = count;
93 return elapsed(&start, &now);
94}
95
96static void single(int fd, uint32_t handle,
97 unsigned ring_id, const char *ring_name)
98{
99 double time;
100 unsigned long count;
101
102 gem_require_ring(fd, ring_id);
103
104 time = nop_on_ring(fd, handle, ring_id, 20, &count);
105 igt_info("%s: %'lu cycles: %.3fus\n",
106 ring_name, count, time*1e6 / count);
Chris Wilson3a7325e2016-03-08 11:43:31 +0000107}
108
Chris Wilson0aacdac2016-03-09 21:06:16 +0000109static bool ignore_engine(int fd, unsigned engine)
110{
111 if (engine == 0)
112 return true;
113
114 if (gem_has_bsd2(fd) && engine == I915_EXEC_BSD)
115 return true;
116
117 return false;
118}
119
Chris Wilson4cce7152016-09-08 13:43:17 +0100120static void parallel(int fd, uint32_t handle, int timeout)
121{
122 struct drm_i915_gem_execbuffer2 execbuf;
123 struct drm_i915_gem_exec_object2 obj;
Chris Wilson4cce7152016-09-08 13:43:17 +0100124 unsigned engines[16];
125 const char *names[16];
126 unsigned nengine;
127 unsigned engine;
128 unsigned long count;
129 double time, sum;
130
131 sum = 0;
132 nengine = 0;
133 for_each_engine(fd, engine) {
134 if (ignore_engine(fd, engine))
135 continue;
136
137 engines[nengine] = engine;
138 names[nengine] = e__->name;
139 nengine++;
140
141 time = nop_on_ring(fd, handle, engine, 1, &count) / count;
142 sum += time;
143 igt_debug("%s: %.3fus\n", e__->name, 1e6*time);
144 }
145 igt_require(nengine);
146 igt_info("average (individually): %.3fus\n", sum/nengine*1e6);
147
148 memset(&obj, 0, sizeof(obj));
149 obj.handle = handle;
150
151 memset(&execbuf, 0, sizeof(execbuf));
Chris Wilson4de67b22017-01-02 11:05:21 +0000152 execbuf.buffers_ptr = to_user_pointer(&obj);
Chris Wilson4cce7152016-09-08 13:43:17 +0100153 execbuf.buffer_count = 1;
154 execbuf.flags |= LOCAL_I915_EXEC_HANDLE_LUT;
155 execbuf.flags |= LOCAL_I915_EXEC_NO_RELOC;
156 if (__gem_execbuf(fd, &execbuf)) {
157 execbuf.flags = 0;
158 gem_execbuf(fd, &execbuf);
159 }
Chris Wilson4cce7152016-09-08 13:43:17 +0100160 intel_detect_and_clear_missed_interrupts(fd);
161
162 igt_fork(child, nengine) {
Chris Wilsonf565b6c2016-09-08 20:59:55 +0100163 struct timespec start, now;
164
Chris Wilson4cce7152016-09-08 13:43:17 +0100165 execbuf.flags &= ~ENGINE_FLAGS;
166 execbuf.flags |= engines[child];
167
168 count = 0;
169 clock_gettime(CLOCK_MONOTONIC, &start);
170 do {
171 for (int loop = 0; loop < 1024; loop++)
172 gem_execbuf(fd, &execbuf);
173 count += 1024;
174 clock_gettime(CLOCK_MONOTONIC, &now);
175 } while (elapsed(&start, &now) < timeout);
Chris Wilson4cce7152016-09-08 13:43:17 +0100176 time = elapsed(&start, &now) / count;
177 igt_info("%s: %ld cycles, %.3fus\n", names[child], count, 1e6*time);
178 }
179
180 igt_waitchildren();
181 igt_assert_eq(intel_detect_and_clear_missed_interrupts(fd), 0);
182
183}
184
185static void series(int fd, uint32_t handle, int timeout)
Chris Wilson3a7325e2016-03-08 11:43:31 +0000186{
187 struct drm_i915_gem_execbuffer2 execbuf;
188 struct drm_i915_gem_exec_object2 obj;
Chris Wilsonf565b6c2016-09-08 20:59:55 +0100189 struct timespec start, now, sync;
Chris Wilson3a7325e2016-03-08 11:43:31 +0000190 unsigned engines[16];
191 unsigned nengine;
192 unsigned engine;
Chris Wilson870c7742016-03-28 15:29:46 +0100193 unsigned long count;
Chris Wilson41a26b52016-03-28 16:26:01 +0100194 double time, max = 0, min = HUGE_VAL, sum = 0;
Chris Wilson870c7742016-03-28 15:29:46 +0100195 const char *name;
Chris Wilson3a7325e2016-03-08 11:43:31 +0000196
197 nengine = 0;
Chris Wilson870c7742016-03-28 15:29:46 +0100198 for_each_engine(fd, engine) {
199 if (ignore_engine(fd, engine))
200 continue;
201
202 time = nop_on_ring(fd, handle, engine, 1, &count) / count;
203 if (time > max) {
204 name = e__->name;
205 max = time;
206 }
Chris Wilson41a26b52016-03-28 16:26:01 +0100207 if (time < min)
208 min = time;
Chris Wilson870c7742016-03-28 15:29:46 +0100209 sum += time;
210 engines[nengine++] = engine;
211 }
Chris Wilson0aacdac2016-03-09 21:06:16 +0000212 igt_require(nengine);
Chris Wilsonf565b6c2016-09-08 20:59:55 +0100213 igt_info("Maximum execution latency on %s, %.3fus, min %.3fus, total %.3fus per cycle, average %.3fus\n",
214 name, max*1e6, min*1e6, sum*1e6, sum/nengine*1e6);
Chris Wilson3a7325e2016-03-08 11:43:31 +0000215
216 memset(&obj, 0, sizeof(obj));
217 obj.handle = handle;
218
219 memset(&execbuf, 0, sizeof(execbuf));
Chris Wilson4de67b22017-01-02 11:05:21 +0000220 execbuf.buffers_ptr = to_user_pointer(&obj);
Chris Wilson3a7325e2016-03-08 11:43:31 +0000221 execbuf.buffer_count = 1;
222 execbuf.flags |= LOCAL_I915_EXEC_HANDLE_LUT;
223 execbuf.flags |= LOCAL_I915_EXEC_NO_RELOC;
Chris Wilson3e2443f2016-03-10 11:50:53 +0000224 if (__gem_execbuf(fd, &execbuf)) {
Chris Wilson3a7325e2016-03-08 11:43:31 +0000225 execbuf.flags = 0;
226 gem_execbuf(fd, &execbuf);
Chris Wilson07d59b32011-01-20 22:10:10 +0000227 }
Chris Wilson71f41532016-04-22 16:55:29 +0100228 intel_detect_and_clear_missed_interrupts(fd);
Chris Wilson3a7325e2016-03-08 11:43:31 +0000229
Chris Wilson870c7742016-03-28 15:29:46 +0100230 count = 0;
Chris Wilson3a7325e2016-03-08 11:43:31 +0000231 clock_gettime(CLOCK_MONOTONIC, &start);
232 do {
233 for (int loop = 0; loop < 1024; loop++) {
234 for (int n = 0; n < nengine; n++) {
235 execbuf.flags &= ~ENGINE_FLAGS;
236 execbuf.flags |= engines[n];
237 gem_execbuf(fd, &execbuf);
238 }
239 }
240 count += nengine * 1024;
241 clock_gettime(CLOCK_MONOTONIC, &now);
Chris Wilson772393e2016-03-14 14:31:36 +0000242 } while (elapsed(&start, &now) < timeout); /* Hang detection ~120s */
Chris Wilson3a7325e2016-03-08 11:43:31 +0000243 gem_sync(fd, handle);
Chris Wilsonf565b6c2016-09-08 20:59:55 +0100244 clock_gettime(CLOCK_MONOTONIC, &sync);
245 igt_debug("sync time: %.3fus\n", elapsed(&now, &sync)*1e6);
Chris Wilson71f41532016-04-22 16:55:29 +0100246 igt_assert_eq(intel_detect_and_clear_missed_interrupts(fd), 0);
Chris Wilson3a7325e2016-03-08 11:43:31 +0000247
Chris Wilson870c7742016-03-28 15:29:46 +0100248 time = elapsed(&start, &now) / count;
Chris Wilsonf565b6c2016-09-08 20:59:55 +0100249 igt_info("All (%d engines): %'lu cycles, average %.3fus per cycle [expected %.3fus]\n",
Chris Wilson61b19a42016-09-08 14:50:32 +0100250 nengine, count, 1e6*time, 1e6*((max-min)/nengine+min));
Chris Wilson41a26b52016-03-28 16:26:01 +0100251
Chris Wilsona0eebbd2016-09-08 13:29:31 +0100252 /* The rate limiting step should be how fast the slowest engine can
253 * execute its queue of requests, as when we wait upon a full ring all
254 * dispatch is frozen. So in general we cannot go faster than the
255 * slowest engine (but as all engines are in lockstep, they should all
256 * be executing in parallel and so the average should be max/nengines),
257 * but we should equally not go any slower.
258 *
259 * However, that depends upon being able to submit fast enough, and
260 * that in turns depends upon debugging turned off and no bottlenecks
261 * within the driver. We cannot assert that we hit ideal conditions
262 * across all engines, so we only look for an outrageous error
263 * condition.
Chris Wilson41a26b52016-03-28 16:26:01 +0100264 */
Chris Wilsona0eebbd2016-09-08 13:29:31 +0100265 igt_assert_f(time < 2*sum,
266 "Average time (%.3fus) exceeds expectation for parallel execution (min %.3fus, max %.3fus; limit set at %.3fus)\n",
267 1e6*time, 1e6*min, 1e6*max, 1e6*sum*2);
Daniel Vetterd9d95782012-12-04 17:13:05 +0100268}
Daniel Vetter8f5387e2013-08-13 13:20:58 +0200269
Chris Wilsonc6e26e42016-07-22 12:58:54 +0100270static void print_welcome(int fd)
271{
272 bool active;
Chris Wilsonc6e26e42016-07-22 12:58:54 +0100273 int dir;
274
275 dir = igt_sysfs_open_parameters(fd);
276 if (dir < 0)
277 return;
278
Chris Wilsonb64d10c2016-07-22 17:53:51 +0100279 active = igt_sysfs_get_boolean(dir, "enable_guc_submission");
Chris Wilsonc6e26e42016-07-22 12:58:54 +0100280 if (active) {
281 igt_info("Using GuC submission\n");
282 goto out;
283 }
284
Chris Wilsonb64d10c2016-07-22 17:53:51 +0100285 active = igt_sysfs_get_boolean(dir, "enable_execlists");
Chris Wilsonc6e26e42016-07-22 12:58:54 +0100286 if (active) {
287 igt_info("Using Execlists submission\n");
288 goto out;
289 }
290
Chris Wilsonb64d10c2016-07-22 17:53:51 +0100291 active = igt_sysfs_get_boolean(dir, "semaphores");
Chris Wilsonf565b6c2016-09-08 20:59:55 +0100292 igt_info("Using Legacy submission%s\n",
Chris Wilsonc6e26e42016-07-22 12:58:54 +0100293 active ? ", with semaphores" : "");
294
295out:
296 close(dir);
297}
298
Daniel Vetter071e9ca2013-10-31 16:23:26 +0100299igt_main
Daniel Vetterd9d95782012-12-04 17:13:05 +0100300{
Chris Wilson7e0853c2016-01-27 14:17:53 +0000301 const struct intel_execution_engine *e;
Chris Wilson2659cbb2015-03-26 12:09:57 +0000302 uint32_t handle = 0;
Chris Wilson3a7325e2016-03-08 11:43:31 +0000303 int device = -1;
Daniel Vetterd9d95782012-12-04 17:13:05 +0100304
Chris Wilson2659cbb2015-03-26 12:09:57 +0000305 igt_fixture {
Chris Wilson3a7325e2016-03-08 11:43:31 +0000306 const uint32_t bbe = MI_BATCH_BUFFER_END;
307
Micah Fedkec81d2932015-07-22 21:54:02 +0000308 device = drm_open_driver(DRIVER_INTEL);
Chris Wilsonc6e26e42016-07-22 12:58:54 +0100309 print_welcome(device);
310
Chris Wilson2659cbb2015-03-26 12:09:57 +0000311 handle = gem_create(device, 4096);
Chris Wilson3a7325e2016-03-08 11:43:31 +0000312 gem_write(device, handle, 0, &bbe, sizeof(bbe));
Daniel Vetterd9d95782012-12-04 17:13:05 +0100313
Daniel Vetterbe21fc02016-06-17 16:04:09 +0200314 igt_fork_hang_detector(device);
315 }
Chris Wilson9d61a682016-03-25 18:22:54 +0000316
Chris Wilson4cce7152016-09-08 13:43:17 +0100317 igt_subtest("basic-series")
Chris Wilson69b29f82016-10-18 10:23:49 +0100318 series(device, handle, 5);
Chris Wilson4cce7152016-09-08 13:43:17 +0100319
320 igt_subtest("basic-parallel")
Chris Wilson69b29f82016-10-18 10:23:49 +0100321 parallel(device, handle, 5);
Chris Wilson772393e2016-03-14 14:31:36 +0000322
Chris Wilson7e0853c2016-01-27 14:17:53 +0000323 for (e = intel_execution_engines; e->name; e++)
324 igt_subtest_f("%s", e->name)
Chris Wilson3a7325e2016-03-08 11:43:31 +0000325 single(device, handle, e->exec_id | e->flags, e->name);
326
Chris Wilson4cce7152016-09-08 13:43:17 +0100327 igt_subtest("series")
328 series(device, handle, 150);
329
330 igt_subtest("parallel")
331 parallel(device, handle, 150);
Daniel Vetterd9d95782012-12-04 17:13:05 +0100332
Daniel Vetterb3880d32013-08-14 18:02:46 +0200333 igt_fixture {
Daniel Vetterbe21fc02016-06-17 16:04:09 +0200334 igt_stop_hang_detector();
Chris Wilson2659cbb2015-03-26 12:09:57 +0000335 gem_close(device, handle);
Chris Wilson2659cbb2015-03-26 12:09:57 +0000336 close(device);
Daniel Vetterb3880d32013-08-14 18:02:46 +0200337 }
Chris Wilson07d59b32011-01-20 22:10:10 +0000338}