blob: 608b2dfd1e4ba94dd377122296611054e270f9dc [file] [log] [blame]
Chris Wilsoneb7d60e2015-06-17 18:29:49 +01001/*
2 * Copyright © 2015 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 */
24
25/*
26 * Testcase: Test that only specific ioctl report a wedged GPU.
27 *
28 */
29
30#include <stdlib.h>
31#include <stdio.h>
32#include <string.h>
33#include <unistd.h>
34#include <fcntl.h>
35#include <inttypes.h>
36#include <errno.h>
37#include <sys/ioctl.h>
38
39#include <drm.h>
40
Chris Wilson639d6402017-09-08 13:45:21 +010041#include "igt.h"
42#include "igt_sysfs.h"
Chris Wilson92e457d2017-09-08 11:33:15 +010043#include "sw_sync.h"
Chris Wilsoneb7d60e2015-06-17 18:29:49 +010044
45IGT_TEST_DESCRIPTION("Test that specific ioctls report a wedged GPU (EIO).");
46
47static bool i915_reset_control(bool enable)
48{
49 const char *path = "/sys/module/i915/parameters/reset";
50 int fd, ret;
51
52 igt_debug("%s GPU reset\n", enable ? "Enabling" : "Disabling");
53
54 fd = open(path, O_RDWR);
55 igt_require(fd >= 0);
56
Chris Wilson6b2dddd2017-07-25 17:11:16 +010057 ret = write(fd, &"01"[enable], 1) == 1;
Chris Wilsoneb7d60e2015-06-17 18:29:49 +010058 close(fd);
59
60 return ret;
61}
62
Chris Wilsoneb7d60e2015-06-17 18:29:49 +010063static void trigger_reset(int fd)
64{
Chris Wilson83884e92017-03-21 17:16:03 +000065 igt_force_gpu_reset(fd);
Chris Wilsoneb7d60e2015-06-17 18:29:49 +010066
67 /* And just check the gpu is indeed running again */
68 igt_debug("Checking that the GPU recovered\n");
69 gem_quiescent_gpu(fd);
70}
71
72static void wedge_gpu(int fd)
73{
74 /* First idle the GPU then disable GPU resets before injecting a hang */
75 gem_quiescent_gpu(fd);
76
77 igt_require(i915_reset_control(false));
78
79 igt_debug("Wedging GPU by injecting hang\n");
80 igt_post_hang_ring(fd, igt_hang_ring(fd, I915_EXEC_DEFAULT));
81
82 igt_assert(i915_reset_control(true));
83}
84
85static int __gem_throttle(int fd)
86{
87 int err = 0;
88 if (drmIoctl(fd, DRM_IOCTL_I915_GEM_THROTTLE, NULL))
89 err = -errno;
90 return err;
91}
92
93static void test_throttle(int fd)
94{
95 wedge_gpu(fd);
96
97 igt_assert_eq(__gem_throttle(fd), -EIO);
98
99 trigger_reset(fd);
100}
101
Chris Wilsoneb7d60e2015-06-17 18:29:49 +0100102static void test_execbuf(int fd)
103{
104 struct drm_i915_gem_execbuffer2 execbuf;
105 struct drm_i915_gem_exec_object2 exec;
106 uint32_t tmp[] = { MI_BATCH_BUFFER_END };
107
108 memset(&exec, 0, sizeof(exec));
109 memset(&execbuf, 0, sizeof(execbuf));
110
111 exec.handle = gem_create(fd, 4096);
112 gem_write(fd, exec.handle, 0, tmp, sizeof(tmp));
113
Chris Wilson4de67b22017-01-02 11:05:21 +0000114 execbuf.buffers_ptr = to_user_pointer(&exec);
Chris Wilsoneb7d60e2015-06-17 18:29:49 +0100115 execbuf.buffer_count = 1;
116
117 wedge_gpu(fd);
118
119 igt_assert_eq(__gem_execbuf(fd, &execbuf), -EIO);
120 gem_close(fd, exec.handle);
121
122 trigger_reset(fd);
123}
124
Chris Wilson32c89882015-07-15 16:18:10 +0100125static int __gem_wait(int fd, uint32_t handle, int64_t timeout)
126{
127 struct drm_i915_gem_wait wait;
128 int err = 0;
129
130 memset(&wait, 0, sizeof(wait));
131 wait.bo_handle = handle;
132 wait.timeout_ns = timeout;
133 if (drmIoctl(fd, DRM_IOCTL_I915_GEM_WAIT, &wait))
134 err = -errno;
135
136 return err;
137}
138
139static void test_wait(int fd)
140{
Chris Wilson0a1fc452016-09-13 11:13:14 +0100141 igt_hang_t hang;
Chris Wilson32c89882015-07-15 16:18:10 +0100142
Daniel Vetter40798ef2015-12-16 13:13:58 +0000143 /* If the request we wait on completes due to a hang (even for
144 * that request), the user expects the return value to 0 (success).
145 */
Chris Wilson32c89882015-07-15 16:18:10 +0100146 hang = igt_hang_ring(fd, I915_EXEC_DEFAULT);
Daniel Vetter40798ef2015-12-16 13:13:58 +0000147 igt_assert_eq(__gem_wait(fd, hang.handle, -1), 0);
Chris Wilson32c89882015-07-15 16:18:10 +0100148 igt_post_hang_ring(fd, hang);
149
Daniel Vetter40798ef2015-12-16 13:13:58 +0000150 /* If the GPU is wedged during the wait, again we expect the return
151 * value to be 0 (success).
152 */
153 igt_require(i915_reset_control(false));
154 hang = igt_hang_ring(fd, I915_EXEC_DEFAULT);
155 igt_assert_eq(__gem_wait(fd, hang.handle, -1), 0);
156 igt_post_hang_ring(fd, hang);
157 igt_require(i915_reset_control(true));
158
Chris Wilson32c89882015-07-15 16:18:10 +0100159 trigger_reset(fd);
160}
161
Chris Wilson4b0e0bf2017-09-11 16:41:13 +0100162static void test_inflight(int fd)
163{
164 struct drm_i915_gem_execbuffer2 execbuf;
165 struct drm_i915_gem_exec_object2 obj[2];
166 uint32_t bbe = MI_BATCH_BUFFER_END;
167 unsigned int engine;
168 int fence[64]; /* conservative estimate of ring size */
169
170 igt_require(gem_has_exec_fence(fd));
171
172 memset(obj, 0, sizeof(obj));
173 obj[0].flags = EXEC_OBJECT_WRITE;
174 obj[1].handle = gem_create(fd, 4096);
175 gem_write(fd, obj[1].handle, 0, &bbe, sizeof(bbe));
176
177 for_each_engine(fd, engine) {
178 igt_hang_t hang;
179
180 igt_debug("Starting %s on engine '%s'\n", __func__, e__->name);
181 igt_require(i915_reset_control(false));
182
183 hang = igt_hang_ring(fd, engine);
184 obj[0].handle = hang.handle;
185
186 memset(&execbuf, 0, sizeof(execbuf));
187 execbuf.buffers_ptr = to_user_pointer(obj);
188 execbuf.buffer_count = 2;
189 execbuf.flags = engine | I915_EXEC_FENCE_OUT;
190
191 for (unsigned int n = 0; n < ARRAY_SIZE(fence); n++) {
192 gem_execbuf_wr(fd, &execbuf);
193 fence[n] = execbuf.rsvd2 >> 32;
194 igt_assert(fence[n] != -1);
195 }
196
197 igt_post_hang_ring(fd, hang);
198
199 igt_assert_eq(__gem_wait(fd, obj[1].handle, -1), 0);
200 for (unsigned int n = 0; n < ARRAY_SIZE(fence); n++) {
201 igt_assert_eq(sync_fence_status(fence[n]), -EIO);
202 close(fence[n]);
203 }
204
205 igt_assert(i915_reset_control(true));
206 trigger_reset(fd);
207 }
208}
209
Chris Wilsonda197b52017-09-15 17:05:43 +0100210static uint32_t __gem_context_create(int fd)
211{
212 struct drm_i915_gem_context_create create;
213
214 memset(&create, 0, sizeof(create));
215 if (ioctl(fd, DRM_IOCTL_I915_GEM_CONTEXT_CREATE, &create))
216 return 0;
217
218 return create.ctx_id;
219}
220
221static void test_inflight_contexts(int fd)
222{
223 struct drm_i915_gem_execbuffer2 execbuf;
224 struct drm_i915_gem_exec_object2 obj[2];
225 uint32_t bbe = MI_BATCH_BUFFER_END;
226 unsigned int engine;
227 uint32_t ctx[64];
228 int fence[64];
229
230 igt_require(gem_has_exec_fence(fd));
231
232 ctx[0] = __gem_context_create(fd);
233 igt_require(ctx[0]);
234 for (unsigned int n = 1; n < ARRAY_SIZE(ctx); n++)
235 ctx[n] = gem_context_create(fd);
236
237 memset(obj, 0, sizeof(obj));
238 obj[0].flags = EXEC_OBJECT_WRITE;
239 obj[1].handle = gem_create(fd, 4096);
240 gem_write(fd, obj[1].handle, 0, &bbe, sizeof(bbe));
241
242 for_each_engine(fd, engine) {
243 igt_hang_t hang;
244
245 igt_debug("Starting %s on engine '%s'\n", __func__, e__->name);
246 igt_require(i915_reset_control(false));
247
248 hang = igt_hang_ring(fd, engine);
249 obj[0].handle = hang.handle;
250
251 memset(&execbuf, 0, sizeof(execbuf));
252 execbuf.buffers_ptr = to_user_pointer(obj);
253 execbuf.buffer_count = 2;
254 execbuf.flags = engine | I915_EXEC_FENCE_OUT;
255
256 for (unsigned int n = 0; n < ARRAY_SIZE(fence); n++) {
257 execbuf.rsvd1 = ctx[n];
258 gem_execbuf_wr(fd, &execbuf);
259 fence[n] = execbuf.rsvd2 >> 32;
260 igt_assert(fence[n] != -1);
261 }
262
263 igt_post_hang_ring(fd, hang);
264
265 igt_assert_eq(__gem_wait(fd, obj[1].handle, -1), 0);
266 for (unsigned int n = 0; n < ARRAY_SIZE(fence); n++) {
267 igt_assert_eq(sync_fence_status(fence[n]), -EIO);
268 close(fence[n]);
269 }
270
271 igt_assert(i915_reset_control(true));
272 trigger_reset(fd);
273 }
274
275 for (unsigned int n = 0; n < ARRAY_SIZE(ctx); n++)
276 gem_context_destroy(fd, ctx[n]);
277}
278
Chris Wilson92e457d2017-09-08 11:33:15 +0100279static void test_inflight_external(int fd)
Chris Wilson9bbf6422016-11-18 08:50:33 +0000280{
281 struct drm_i915_gem_execbuffer2 execbuf;
Chris Wilson92e457d2017-09-08 11:33:15 +0100282 struct drm_i915_gem_exec_object2 obj;
Chris Wilson9bbf6422016-11-18 08:50:33 +0000283 uint32_t bbe = MI_BATCH_BUFFER_END;
284 igt_hang_t hang;
Chris Wilson92e457d2017-09-08 11:33:15 +0100285 int timeline, fence;
286
287 igt_require_sw_sync();
288 igt_require(gem_has_exec_fence(fd));
289
290 timeline = sw_sync_timeline_create();
291 fence = sw_sync_timeline_create_fence(timeline, 1);
Chris Wilson9bbf6422016-11-18 08:50:33 +0000292
293 igt_require(i915_reset_control(false));
294 hang = igt_hang_ring(fd, I915_EXEC_DEFAULT);
295
Chris Wilson92e457d2017-09-08 11:33:15 +0100296 memset(&obj, 0, sizeof(obj));
297 obj.handle = gem_create(fd, 4096);
298 gem_write(fd, obj.handle, 0, &bbe, sizeof(bbe));
Chris Wilson9bbf6422016-11-18 08:50:33 +0000299
300 memset(&execbuf, 0, sizeof(execbuf));
Chris Wilson92e457d2017-09-08 11:33:15 +0100301 execbuf.buffers_ptr = to_user_pointer(&obj);
302 execbuf.buffer_count = 1;
303 execbuf.flags = I915_EXEC_FENCE_IN | I915_EXEC_FENCE_OUT;
304 execbuf.rsvd2 = (uint32_t)fence;
Chris Wilson9bbf6422016-11-18 08:50:33 +0000305
Chris Wilson92e457d2017-09-08 11:33:15 +0100306 gem_execbuf_wr(fd, &execbuf);
307 close(fence);
Chris Wilson9bbf6422016-11-18 08:50:33 +0000308
Chris Wilson92e457d2017-09-08 11:33:15 +0100309 fence = execbuf.rsvd2 >> 32;
310 igt_assert(fence != -1);
Chris Wilson9bbf6422016-11-18 08:50:33 +0000311
Chris Wilson92e457d2017-09-08 11:33:15 +0100312 igt_post_hang_ring(fd, hang); /* wedged, with an unready batch */
313 sw_sync_timeline_inc(timeline, 1); /* only now submit our batches */
314
315 igt_assert_eq(__gem_wait(fd, obj.handle, -1), 0);
316 igt_assert_eq(sync_fence_status(fence), -EIO);
317 close(fence);
318
319 igt_assert(i915_reset_control(true));
Chris Wilson9bbf6422016-11-18 08:50:33 +0000320 trigger_reset(fd);
Chris Wilson92e457d2017-09-08 11:33:15 +0100321 close(timeline);
Chris Wilson9bbf6422016-11-18 08:50:33 +0000322}
323
Chris Wilson639d6402017-09-08 13:45:21 +0100324static void test_inflight_internal(int fd)
325{
326 struct drm_i915_gem_execbuffer2 execbuf;
327 struct drm_i915_gem_exec_object2 obj[2];
328 uint32_t bbe = MI_BATCH_BUFFER_END;
329 unsigned engine, nfence = 0;
330 int fences[16];
331 igt_hang_t hang;
332
333 igt_require(gem_has_exec_fence(fd));
334
335 igt_require(i915_reset_control(false));
336 hang = igt_hang_ring(fd, I915_EXEC_DEFAULT);
337
338 memset(obj, 0, sizeof(obj));
339 obj[0].handle = hang.handle;
340 obj[0].flags = EXEC_OBJECT_WRITE;
341 obj[1].handle = gem_create(fd, 4096);
342 gem_write(fd, obj[1].handle, 0, &bbe, sizeof(bbe));
343
344 memset(&execbuf, 0, sizeof(execbuf));
345 execbuf.buffers_ptr = to_user_pointer(obj);
346 execbuf.buffer_count = 2;
347 for_each_engine(fd, engine) {
348 execbuf.flags = engine | I915_EXEC_FENCE_OUT;
349
350 gem_execbuf_wr(fd, &execbuf);
351
352 fences[nfence] = execbuf.rsvd2 >> 32;
353 igt_assert(fences[nfence] != -1);
354 nfence++;
355 }
356
357 igt_post_hang_ring(fd, hang); /* wedged, with an unready batch */
358
359 igt_assert_eq(__gem_wait(fd, obj[1].handle, -1), 0);
360 while (nfence--) {
361 igt_assert_eq(sync_fence_status(fences[nfence]), -EIO);
362 close(fences[nfence]);
363 }
364
365 igt_assert(i915_reset_control(true));
366 trigger_reset(fd);
367}
368
369#define HAVE_EXECLISTS 0x1
370#define HAVE_GUC 0x2
371#define HAVE_SEMAPHORES 0x4
372
373static unsigned print_welcome(int fd)
374{
375 unsigned flags = 0;
376 bool active;
377 int dir;
378
379 dir = igt_sysfs_open_parameters(fd);
380 if (dir < 0)
381 return 0;
382
383 active = igt_sysfs_get_boolean(dir, "enable_guc_submission");
384 if (active) {
385 igt_info("Using GuC submission\n");
386 flags |= HAVE_GUC | HAVE_EXECLISTS;
387 goto out;
388 }
389
390 active = igt_sysfs_get_boolean(dir, "enable_execlists");
391 if (active) {
392 igt_info("Using Execlists submission\n");
393 flags |= HAVE_EXECLISTS;
394 goto out;
395 }
396
397 active = igt_sysfs_get_boolean(dir, "semaphores");
398 if (active)
399 flags |= HAVE_SEMAPHORES;
400 igt_info("Using Legacy submission%s\n",
401 active ? ", with semaphores" : "");
402
403out:
404 close(dir);
405 return flags;
406}
407
Chris Wilson4f082c32017-09-08 13:48:05 +0100408static int fd = -1;
409
410static void
411exit_handler(int sig)
412{
413 i915_reset_control(true);
414 igt_force_gpu_reset(fd);
415}
416
Chris Wilsoneb7d60e2015-06-17 18:29:49 +0100417igt_main
418{
Chris Wilson639d6402017-09-08 13:45:21 +0100419 unsigned int caps = 0;
Chris Wilsoneb7d60e2015-06-17 18:29:49 +0100420
421 igt_skip_on_simulation();
422
423 igt_fixture {
Micah Fedkec81d2932015-07-22 21:54:02 +0000424 fd = drm_open_driver(DRIVER_INTEL);
Chris Wilson4f082c32017-09-08 13:48:05 +0100425
426 igt_require(i915_reset_control(true));
427 igt_force_gpu_reset(fd);
428 igt_install_exit_handler(exit_handler);
429
Chris Wilson639d6402017-09-08 13:45:21 +0100430 caps = print_welcome(fd);
Chris Wilson28a2f142017-03-08 12:22:13 +0000431 igt_require_gem(fd);
Chris Wilson92caf132015-12-16 09:23:56 +0000432 igt_require_hang_ring(fd, I915_EXEC_DEFAULT);
Chris Wilsoneb7d60e2015-06-17 18:29:49 +0100433 }
434
435 igt_subtest("throttle")
436 test_throttle(fd);
437
438 igt_subtest("execbuf")
439 test_execbuf(fd);
440
Chris Wilson32c89882015-07-15 16:18:10 +0100441 igt_subtest("wait")
442 test_wait(fd);
443
Chris Wilson4b0e0bf2017-09-11 16:41:13 +0100444 igt_subtest("in-flight")
445 test_inflight(fd);
446
Chris Wilsonda197b52017-09-15 17:05:43 +0100447 igt_subtest("in-flight-contexts")
448 test_inflight_contexts(fd);
449
Chris Wilson92e457d2017-09-08 11:33:15 +0100450 igt_subtest("in-flight-external")
451 test_inflight_external(fd);
Chris Wilson639d6402017-09-08 13:45:21 +0100452
453 igt_subtest("in-flight-internal") {
454 igt_skip_on(caps & HAVE_SEMAPHORES);
455 test_inflight_internal(fd);
456 }
Chris Wilsoneb7d60e2015-06-17 18:29:49 +0100457}