blob: 0fa98032e895a48e9f02aacf51b0bc8bddfb8d47 [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 Wilson92e457d2017-09-08 11:33:15 +0100210static void test_inflight_external(int fd)
Chris Wilson9bbf6422016-11-18 08:50:33 +0000211{
212 struct drm_i915_gem_execbuffer2 execbuf;
Chris Wilson92e457d2017-09-08 11:33:15 +0100213 struct drm_i915_gem_exec_object2 obj;
Chris Wilson9bbf6422016-11-18 08:50:33 +0000214 uint32_t bbe = MI_BATCH_BUFFER_END;
215 igt_hang_t hang;
Chris Wilson92e457d2017-09-08 11:33:15 +0100216 int timeline, fence;
217
218 igt_require_sw_sync();
219 igt_require(gem_has_exec_fence(fd));
220
221 timeline = sw_sync_timeline_create();
222 fence = sw_sync_timeline_create_fence(timeline, 1);
Chris Wilson9bbf6422016-11-18 08:50:33 +0000223
224 igt_require(i915_reset_control(false));
225 hang = igt_hang_ring(fd, I915_EXEC_DEFAULT);
226
Chris Wilson92e457d2017-09-08 11:33:15 +0100227 memset(&obj, 0, sizeof(obj));
228 obj.handle = gem_create(fd, 4096);
229 gem_write(fd, obj.handle, 0, &bbe, sizeof(bbe));
Chris Wilson9bbf6422016-11-18 08:50:33 +0000230
231 memset(&execbuf, 0, sizeof(execbuf));
Chris Wilson92e457d2017-09-08 11:33:15 +0100232 execbuf.buffers_ptr = to_user_pointer(&obj);
233 execbuf.buffer_count = 1;
234 execbuf.flags = I915_EXEC_FENCE_IN | I915_EXEC_FENCE_OUT;
235 execbuf.rsvd2 = (uint32_t)fence;
Chris Wilson9bbf6422016-11-18 08:50:33 +0000236
Chris Wilson92e457d2017-09-08 11:33:15 +0100237 gem_execbuf_wr(fd, &execbuf);
238 close(fence);
Chris Wilson9bbf6422016-11-18 08:50:33 +0000239
Chris Wilson92e457d2017-09-08 11:33:15 +0100240 fence = execbuf.rsvd2 >> 32;
241 igt_assert(fence != -1);
Chris Wilson9bbf6422016-11-18 08:50:33 +0000242
Chris Wilson92e457d2017-09-08 11:33:15 +0100243 igt_post_hang_ring(fd, hang); /* wedged, with an unready batch */
244 sw_sync_timeline_inc(timeline, 1); /* only now submit our batches */
245
246 igt_assert_eq(__gem_wait(fd, obj.handle, -1), 0);
247 igt_assert_eq(sync_fence_status(fence), -EIO);
248 close(fence);
249
250 igt_assert(i915_reset_control(true));
Chris Wilson9bbf6422016-11-18 08:50:33 +0000251 trigger_reset(fd);
Chris Wilson92e457d2017-09-08 11:33:15 +0100252 close(timeline);
Chris Wilson9bbf6422016-11-18 08:50:33 +0000253}
254
Chris Wilson639d6402017-09-08 13:45:21 +0100255static void test_inflight_internal(int fd)
256{
257 struct drm_i915_gem_execbuffer2 execbuf;
258 struct drm_i915_gem_exec_object2 obj[2];
259 uint32_t bbe = MI_BATCH_BUFFER_END;
260 unsigned engine, nfence = 0;
261 int fences[16];
262 igt_hang_t hang;
263
264 igt_require(gem_has_exec_fence(fd));
265
266 igt_require(i915_reset_control(false));
267 hang = igt_hang_ring(fd, I915_EXEC_DEFAULT);
268
269 memset(obj, 0, sizeof(obj));
270 obj[0].handle = hang.handle;
271 obj[0].flags = EXEC_OBJECT_WRITE;
272 obj[1].handle = gem_create(fd, 4096);
273 gem_write(fd, obj[1].handle, 0, &bbe, sizeof(bbe));
274
275 memset(&execbuf, 0, sizeof(execbuf));
276 execbuf.buffers_ptr = to_user_pointer(obj);
277 execbuf.buffer_count = 2;
278 for_each_engine(fd, engine) {
279 execbuf.flags = engine | I915_EXEC_FENCE_OUT;
280
281 gem_execbuf_wr(fd, &execbuf);
282
283 fences[nfence] = execbuf.rsvd2 >> 32;
284 igt_assert(fences[nfence] != -1);
285 nfence++;
286 }
287
288 igt_post_hang_ring(fd, hang); /* wedged, with an unready batch */
289
290 igt_assert_eq(__gem_wait(fd, obj[1].handle, -1), 0);
291 while (nfence--) {
292 igt_assert_eq(sync_fence_status(fences[nfence]), -EIO);
293 close(fences[nfence]);
294 }
295
296 igt_assert(i915_reset_control(true));
297 trigger_reset(fd);
298}
299
300#define HAVE_EXECLISTS 0x1
301#define HAVE_GUC 0x2
302#define HAVE_SEMAPHORES 0x4
303
304static unsigned print_welcome(int fd)
305{
306 unsigned flags = 0;
307 bool active;
308 int dir;
309
310 dir = igt_sysfs_open_parameters(fd);
311 if (dir < 0)
312 return 0;
313
314 active = igt_sysfs_get_boolean(dir, "enable_guc_submission");
315 if (active) {
316 igt_info("Using GuC submission\n");
317 flags |= HAVE_GUC | HAVE_EXECLISTS;
318 goto out;
319 }
320
321 active = igt_sysfs_get_boolean(dir, "enable_execlists");
322 if (active) {
323 igt_info("Using Execlists submission\n");
324 flags |= HAVE_EXECLISTS;
325 goto out;
326 }
327
328 active = igt_sysfs_get_boolean(dir, "semaphores");
329 if (active)
330 flags |= HAVE_SEMAPHORES;
331 igt_info("Using Legacy submission%s\n",
332 active ? ", with semaphores" : "");
333
334out:
335 close(dir);
336 return flags;
337}
338
Chris Wilson4f082c32017-09-08 13:48:05 +0100339static int fd = -1;
340
341static void
342exit_handler(int sig)
343{
344 i915_reset_control(true);
345 igt_force_gpu_reset(fd);
346}
347
Chris Wilsoneb7d60e2015-06-17 18:29:49 +0100348igt_main
349{
Chris Wilson639d6402017-09-08 13:45:21 +0100350 unsigned int caps = 0;
Chris Wilsoneb7d60e2015-06-17 18:29:49 +0100351
352 igt_skip_on_simulation();
353
354 igt_fixture {
Micah Fedkec81d2932015-07-22 21:54:02 +0000355 fd = drm_open_driver(DRIVER_INTEL);
Chris Wilson4f082c32017-09-08 13:48:05 +0100356
357 igt_require(i915_reset_control(true));
358 igt_force_gpu_reset(fd);
359 igt_install_exit_handler(exit_handler);
360
Chris Wilson639d6402017-09-08 13:45:21 +0100361 caps = print_welcome(fd);
Chris Wilson28a2f142017-03-08 12:22:13 +0000362 igt_require_gem(fd);
Chris Wilson92caf132015-12-16 09:23:56 +0000363 igt_require_hang_ring(fd, I915_EXEC_DEFAULT);
Chris Wilsoneb7d60e2015-06-17 18:29:49 +0100364 }
365
366 igt_subtest("throttle")
367 test_throttle(fd);
368
369 igt_subtest("execbuf")
370 test_execbuf(fd);
371
Chris Wilson32c89882015-07-15 16:18:10 +0100372 igt_subtest("wait")
373 test_wait(fd);
374
Chris Wilson4b0e0bf2017-09-11 16:41:13 +0100375 igt_subtest("in-flight")
376 test_inflight(fd);
377
Chris Wilson92e457d2017-09-08 11:33:15 +0100378 igt_subtest("in-flight-external")
379 test_inflight_external(fd);
Chris Wilson639d6402017-09-08 13:45:21 +0100380
381 igt_subtest("in-flight-internal") {
382 igt_skip_on(caps & HAVE_SEMAPHORES);
383 test_inflight_internal(fd);
384 }
Chris Wilsoneb7d60e2015-06-17 18:29:49 +0100385}