2 * Copyright (c) 2013 Intel Corporation
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:
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
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
24 * Mika Kuoppala <mika.kuoppala@intel.com>
36 #include <sys/ioctl.h>
41 #include "intel_bufmgr.h"
42 #include "intel_batchbuffer.h"
43 #include "intel_gpu_tools.h"
44 #include "rendercopy.h"
45 #include "igt_debugfs.h"
48 #define RS_BATCH_ACTIVE (1 << 0)
49 #define RS_BATCH_PENDING (1 << 1)
50 #define RS_UNKNOWN (1 << 2)
52 struct local_drm_i915_reset_stats {
61 struct local_drm_i915_gem_context_create {
66 struct local_drm_i915_gem_context_destroy {
73 #define CONTEXT_CREATE_IOCTL DRM_IOWR(DRM_COMMAND_BASE + 0x2d, struct local_drm_i915_gem_context_create)
74 #define CONTEXT_DESTROY_IOCTL DRM_IOWR(DRM_COMMAND_BASE + 0x2e, struct local_drm_i915_gem_context_destroy)
75 #define GET_RESET_STATS_IOCTL DRM_IOWR(DRM_COMMAND_BASE + 0x32, struct local_drm_i915_reset_stats)
77 static igt_debugfs_t dfs;
79 static uint32_t context_create(int fd)
81 struct local_drm_i915_gem_context_create create;
84 create.ctx_id = rand();
87 ret = drmIoctl(fd, CONTEXT_CREATE_IOCTL, &create);
93 static int context_destroy(int fd, uint32_t ctx_id)
96 struct local_drm_i915_gem_context_destroy destroy;
98 destroy.ctx_id = ctx_id;
101 ret = drmIoctl(fd, CONTEXT_DESTROY_IOCTL, &destroy);
108 static int gem_reset_stats(int fd, int ctx_id,
109 struct local_drm_i915_reset_stats *rs)
115 rs->reset_count = rand();
116 rs->batch_active = rand();
117 rs->batch_pending = rand();
121 ret = ioctl(fd, GET_RESET_STATS_IOCTL, rs);
122 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
130 static int gem_reset_status(int fd, int ctx_id)
133 struct local_drm_i915_reset_stats rs;
135 ret = gem_reset_stats(fd, ctx_id, &rs);
140 return RS_BATCH_ACTIVE;
141 if (rs.batch_pending)
142 return RS_BATCH_PENDING;
147 static int gem_exec(int fd, struct drm_i915_gem_execbuffer2 *execbuf)
152 DRM_IOCTL_I915_GEM_EXECBUFFER2,
161 static int exec_valid(int fd, int ctx)
163 struct drm_i915_gem_execbuffer2 execbuf;
164 struct drm_i915_gem_exec_object2 exec;
167 uint32_t buf[2] = { MI_BATCH_BUFFER_END, 0 };
169 exec.handle = gem_create(fd, 4096);
170 gem_write(fd, exec.handle, 0, buf, sizeof(buf));
171 exec.relocation_count = 0;
179 execbuf.buffers_ptr = (uintptr_t)&exec;
180 execbuf.buffer_count = 1;
181 execbuf.batch_start_offset = 0;
182 execbuf.batch_len = sizeof(buf);
183 execbuf.cliprects_ptr = 0;
184 execbuf.num_cliprects = 0;
188 i915_execbuffer2_set_context_id(execbuf, ctx);
191 ret = gem_exec(fd, &execbuf);
198 static void stop_rings(void)
202 fd = igt_debugfs_open(&dfs, "i915_ring_stop", O_WRONLY);
205 igt_assert(write(fd, "0xff", 4) == 4);
209 #define BUFSIZE (4 * 1024)
210 #define ITEMS (BUFSIZE >> 2)
212 static int inject_hang(int fd, int ctx)
214 struct drm_i915_gem_execbuffer2 execbuf;
215 struct drm_i915_gem_exec_object2 exec;
219 unsigned cmd_len = 2;
223 if (intel_gen(intel_get_drm_devid(fd)) >= 8)
226 buf = malloc(BUFSIZE);
227 igt_assert(buf != NULL);
229 buf[0] = MI_BATCH_BUFFER_END;
232 exec.handle = gem_create(fd, BUFSIZE);
233 gem_write(fd, exec.handle, 0, buf, BUFSIZE);
234 exec.relocation_count = 0;
242 execbuf.buffers_ptr = (uintptr_t)&exec;
243 execbuf.buffer_count = 1;
244 execbuf.batch_start_offset = 0;
245 execbuf.batch_len = BUFSIZE;
246 execbuf.cliprects_ptr = 0;
247 execbuf.num_cliprects = 0;
251 i915_execbuffer2_set_context_id(execbuf, ctx);
254 igt_assert(gem_exec(fd, &execbuf) == 0);
256 gtt_off = exec.offset;
258 for (i = 0; i < ITEMS; i++)
261 roff = random() % (ITEMS - cmd_len);
262 buf[roff] = MI_BATCH_BUFFER_START | (cmd_len - 2);
263 buf[roff + 1] = (gtt_off & 0xfffffffc) + (roff << 2);
265 buf[roff + 2] = gtt_off & 0xffffffff00000000ull;
268 printf("loop injected at 0x%lx (off 0x%x, bo_start 0x%lx, bo_end 0x%lx)\n",
269 (long unsigned int)((roff << 2) + gtt_off),
270 roff << 2, (long unsigned int)gtt_off,
271 (long unsigned int)(gtt_off + BUFSIZE - 1));
273 gem_write(fd, exec.handle, 0, buf, BUFSIZE);
275 exec.relocation_count = 0;
283 execbuf.buffers_ptr = (uintptr_t)&exec;
284 execbuf.buffer_count = 1;
285 execbuf.batch_start_offset = 0;
286 execbuf.batch_len = BUFSIZE;
287 execbuf.cliprects_ptr = 0;
288 execbuf.num_cliprects = 0;
292 i915_execbuffer2_set_context_id(execbuf, ctx);
295 igt_assert(gem_exec(fd, &execbuf) == 0);
297 igt_assert(gtt_off == exec.offset);
306 static int _assert_reset_status(int fd, int ctx, int status)
310 rs = gem_reset_status(fd, ctx);
312 printf("reset status for %d ctx %d returned %d\n",
318 printf("%d:%d reset status %d differs from assumed %d\n",
319 fd, ctx, rs, status);
327 #define assert_reset_status(fd, ctx, status) \
328 igt_assert(_assert_reset_status(fd, ctx, status) == 0)
330 static void test_rs(int num_fds, int hang_index, int rs_assumed_no_hang)
336 igt_assert (num_fds <= MAX_FD);
337 igt_assert (hang_index < MAX_FD);
339 for (i = 0; i < num_fds; i++) {
340 fd[i] = drm_open_any();
344 for (i = 0; i < num_fds; i++)
345 assert_reset_status(fd[i], 0, RS_NO_ERROR);
347 for (i = 0; i < num_fds; i++) {
349 h[i] = inject_hang(fd[i], 0);
351 h[i] = exec_valid(fd[i], 0);
354 gem_sync(fd[num_fds - 1], h[num_fds - 1]);
356 for (i = 0; i < num_fds; i++) {
357 if (hang_index < 0) {
358 assert_reset_status(fd[i], 0, rs_assumed_no_hang);
363 assert_reset_status(fd[i], 0, RS_NO_ERROR);
365 assert_reset_status(fd[i], 0, RS_BATCH_ACTIVE);
367 assert_reset_status(fd[i], 0, RS_BATCH_PENDING);
370 for (i = 0; i < num_fds; i++) {
371 gem_close(fd[i], h[i]);
377 static void test_rs_ctx(int num_fds, int num_ctx, int hang_index,
382 int h[MAX_FD][MAX_CTX];
383 int ctx[MAX_FD][MAX_CTX];
385 igt_assert (num_fds <= MAX_FD);
386 igt_assert (hang_index < MAX_FD);
388 igt_assert (num_ctx <= MAX_CTX);
389 igt_assert (hang_context < MAX_CTX);
391 test_rs(num_fds, -1, RS_NO_ERROR);
393 for (i = 0; i < num_fds; i++) {
394 fd[i] = drm_open_any();
396 assert_reset_status(fd[i], 0, RS_NO_ERROR);
398 for (j = 0; j < num_ctx; j++) {
399 ctx[i][j] = context_create(fd[i]);
403 assert_reset_status(fd[i], 0, RS_NO_ERROR);
406 for (i = 0; i < num_fds; i++) {
408 assert_reset_status(fd[i], 0, RS_NO_ERROR);
410 for (j = 0; j < num_ctx; j++)
411 assert_reset_status(fd[i], ctx[i][j], RS_NO_ERROR);
413 assert_reset_status(fd[i], 0, RS_NO_ERROR);
416 for (i = 0; i < num_fds; i++) {
417 for (j = 0; j < num_ctx; j++) {
418 if (i == hang_index && j == hang_context)
419 h[i][j] = inject_hang(fd[i], ctx[i][j]);
421 h[i][j] = exec_valid(fd[i], ctx[i][j]);
425 gem_sync(fd[num_fds - 1], ctx[num_fds - 1][num_ctx - 1]);
427 for (i = 0; i < num_fds; i++)
428 assert_reset_status(fd[i], 0, RS_NO_ERROR);
430 for (i = 0; i < num_fds; i++) {
431 for (j = 0; j < num_ctx; j++) {
433 assert_reset_status(fd[i], ctx[i][j], RS_NO_ERROR);
434 if (i == hang_index && j < hang_context)
435 assert_reset_status(fd[i], ctx[i][j], RS_NO_ERROR);
436 if (i == hang_index && j == hang_context)
437 assert_reset_status(fd[i], ctx[i][j],
439 if (i == hang_index && j > hang_context)
440 assert_reset_status(fd[i], ctx[i][j],
443 assert_reset_status(fd[i], ctx[i][j],
448 for (i = 0; i < num_fds; i++) {
449 for (j = 0; j < num_ctx; j++) {
450 gem_close(fd[i], h[i][j]);
451 igt_assert(context_destroy(fd[i], ctx[i][j]) == 0);
454 assert_reset_status(fd[i], 0, RS_NO_ERROR);
460 static void test_ban(void)
462 int h1,h2,h3,h4,h5,h6,h7;
463 int ctx_good, ctx_bad;
466 int active_count = 0, pending_count = 0;
467 struct local_drm_i915_reset_stats rs_bad, rs_good;
472 assert_reset_status(fd, 0, RS_NO_ERROR);
474 ctx_good = context_create(fd);
475 ctx_bad = context_create(fd);
477 assert_reset_status(fd, 0, RS_NO_ERROR);
478 assert_reset_status(fd, ctx_good, RS_NO_ERROR);
479 assert_reset_status(fd, ctx_bad, RS_NO_ERROR);
481 h1 = exec_valid(fd, ctx_bad);
483 h5 = exec_valid(fd, ctx_good);
486 assert_reset_status(fd, ctx_good, RS_NO_ERROR);
487 assert_reset_status(fd, ctx_bad, RS_NO_ERROR);
489 h2 = inject_hang(fd, ctx_bad);
492 /* Second hang will be pending for this */
495 h6 = exec_valid(fd, ctx_good);
496 h7 = exec_valid(fd, ctx_good);
499 h3 = inject_hang(fd, ctx_bad);
503 /* This second hand will count as pending */
504 assert_reset_status(fd, ctx_bad, RS_BATCH_ACTIVE);
506 h4 = exec_valid(fd, ctx_bad);
512 /* Should not happen often but sometimes hang is declared too slow
513 * due to our way of faking hang using loop */
519 printf("retrying for ban (%d)\n", retry);
522 igt_assert(h4 == -EIO);
523 assert_reset_status(fd, ctx_bad, RS_BATCH_ACTIVE);
526 assert_reset_status(fd, ctx_good, RS_BATCH_PENDING);
528 igt_assert(gem_reset_stats(fd, ctx_good, &rs_good) == 0);
529 igt_assert(gem_reset_stats(fd, ctx_bad, &rs_bad) == 0);
531 igt_assert(rs_bad.batch_active == active_count);
532 igt_assert(rs_bad.batch_pending == pending_count);
533 igt_assert(rs_good.batch_active == 0);
534 igt_assert(rs_good.batch_pending == 2);
541 h1 = exec_valid(fd, ctx_good);
545 igt_assert(context_destroy(fd, ctx_good) == 0);
546 igt_assert(context_destroy(fd, ctx_bad) == 0);
547 igt_assert(gem_reset_status(fd, ctx_good) < 0);
548 igt_assert(gem_reset_status(fd, ctx_bad) < 0);
549 igt_assert(exec_valid(fd, ctx_good) < 0);
550 igt_assert(exec_valid(fd, ctx_bad) < 0);
555 static void test_nonrelated_hang(void)
559 int ctx_guilty, ctx_unrelated;
561 fd1 = drm_open_any();
562 fd2 = drm_open_any();
563 assert_reset_status(fd1, 0, RS_NO_ERROR);
564 assert_reset_status(fd2, 0, RS_NO_ERROR);
565 ctx_guilty = context_create(fd1);
566 ctx_unrelated = context_create(fd2);
568 assert_reset_status(fd1, ctx_guilty, RS_NO_ERROR);
569 assert_reset_status(fd2, ctx_unrelated, RS_NO_ERROR);
571 h1 = inject_hang(fd1, ctx_guilty);
574 assert_reset_status(fd1, ctx_guilty, RS_BATCH_ACTIVE);
575 assert_reset_status(fd2, ctx_unrelated, RS_NO_ERROR);
577 h2 = exec_valid(fd2, ctx_unrelated);
580 assert_reset_status(fd1, ctx_guilty, RS_BATCH_ACTIVE);
581 assert_reset_status(fd2, ctx_unrelated, RS_NO_ERROR);
585 igt_assert(context_destroy(fd1, ctx_guilty) == 0);
586 igt_assert(context_destroy(fd2, ctx_unrelated) == 0);
592 static int get_reset_count(int fd, int ctx)
595 struct local_drm_i915_reset_stats rs;
597 ret = gem_reset_stats(fd, ctx, &rs);
601 return rs.reset_count;
604 static void test_double_destroy_pending(void)
611 ctx = context_create(fd);
613 assert_reset_status(fd, ctx, RS_NO_ERROR);
615 h = inject_hang(fd, ctx);
617 igt_assert(context_destroy(fd, ctx) == 0);
618 igt_assert(context_destroy(fd, ctx) == -ENOENT);
624 static void test_close_pending(void)
631 assert_reset_status(fd, 0, RS_NO_ERROR);
633 h = inject_hang(fd, 0);
640 static void __test_count(const bool create_ctx)
648 ctx = context_create(fd);
652 assert_reset_status(fd, ctx, RS_NO_ERROR);
654 c1 = get_reset_count(fd, ctx);
657 h = inject_hang(fd, ctx);
661 assert_reset_status(fd, ctx, RS_BATCH_ACTIVE);
662 c2 = get_reset_count(fd, ctx);
665 igt_assert(c2 == (c1 + 1));
670 context_destroy(fd, ctx);
675 static void test_count(void)
677 return __test_count(false);
680 static void test_count_context(void)
682 return __test_count(true);
685 static void test_global_reset_count(void)
688 test_count_context();
691 static int _test_params(int fd, int ctx, uint32_t flags, uint32_t pad)
693 struct local_drm_i915_reset_stats rs;
698 rs.reset_count = rand();
699 rs.batch_active = rand();
700 rs.batch_pending = rand();
704 ret = ioctl(fd, GET_RESET_STATS_IOCTL, &rs);
705 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
713 static void test_param_ctx(int fd, int ctx)
715 const uint32_t bad = rand() + 1;
717 igt_assert(_test_params(fd, ctx, 0, 0) == 0);
718 igt_assert(_test_params(fd, ctx, 0, bad) == -EINVAL);
719 igt_assert(_test_params(fd, ctx, bad, 0) == -EINVAL);
720 igt_assert(_test_params(fd, ctx, bad, bad) == -EINVAL);
723 static void test_params(void)
729 ctx = context_create(fd);
731 igt_assert(ioctl(fd, GET_RESET_STATS_IOCTL, 0) == -1);
733 igt_assert(_test_params(fd, 0xbadbad, 0, 0) == -ENOENT);
735 test_param_ctx(fd, 0);
736 test_param_ctx(fd, ctx);
744 struct local_drm_i915_gem_context_create create;
749 igt_skip_on_simulation();
753 devid = intel_get_drm_devid(fd);
754 igt_require_f(intel_gen(devid) >= 4,
755 "Architecture %d too old\n", intel_gen(devid));
757 ret = drmIoctl(fd, CONTEXT_CREATE_IOCTL, &create);
758 igt_skip_on_f(ret != 0 && (errno == ENODEV || errno == EINVAL),
759 "Kernel is too old, or contexts not supported: %s\n",
762 assert(igt_debugfs_init(&dfs) == 0);
767 igt_subtest("basic-reset-status")
770 igt_subtest("context-reset-status")
771 test_rs_ctx(4, 4, 1, 2);
776 igt_subtest("ctx-unrelated")
777 test_nonrelated_hang();
779 igt_subtest("global-count")
780 test_global_reset_count();
782 igt_subtest("double-destroy-pending")
783 test_double_destroy_pending();
785 igt_subtest("close-pending")
786 test_close_pending();
788 igt_subtest("params")