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>
37 #include <sys/ioctl.h>
43 #include "intel_bufmgr.h"
44 #include "intel_batchbuffer.h"
45 #include "intel_gpu_tools.h"
46 #include "rendercopy.h"
47 #include "igt_debugfs.h"
50 #define RS_BATCH_ACTIVE (1 << 0)
51 #define RS_BATCH_PENDING (1 << 1)
52 #define RS_UNKNOWN (1 << 2)
54 struct local_drm_i915_reset_stats {
63 struct local_drm_i915_gem_context_create {
68 struct local_drm_i915_gem_context_destroy {
75 #define CONTEXT_CREATE_IOCTL DRM_IOWR(DRM_COMMAND_BASE + 0x2d, struct local_drm_i915_gem_context_create)
76 #define CONTEXT_DESTROY_IOCTL DRM_IOWR(DRM_COMMAND_BASE + 0x2e, struct local_drm_i915_gem_context_destroy)
77 #define GET_RESET_STATS_IOCTL DRM_IOWR(DRM_COMMAND_BASE + 0x32, struct local_drm_i915_reset_stats)
79 static igt_debugfs_t dfs;
81 static uint32_t context_create(int fd)
83 struct local_drm_i915_gem_context_create create;
86 create.ctx_id = rand();
89 ret = drmIoctl(fd, CONTEXT_CREATE_IOCTL, &create);
95 static int context_destroy(int fd, uint32_t ctx_id)
98 struct local_drm_i915_gem_context_destroy destroy;
100 destroy.ctx_id = ctx_id;
101 destroy.pad = rand();
103 ret = drmIoctl(fd, CONTEXT_DESTROY_IOCTL, &destroy);
110 static int gem_reset_stats(int fd, int ctx_id,
111 struct local_drm_i915_reset_stats *rs)
117 rs->reset_count = rand();
118 rs->batch_active = rand();
119 rs->batch_pending = rand();
123 ret = ioctl(fd, GET_RESET_STATS_IOCTL, rs);
124 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
132 static int gem_reset_status(int fd, int ctx_id)
135 struct local_drm_i915_reset_stats rs;
137 ret = gem_reset_stats(fd, ctx_id, &rs);
142 return RS_BATCH_ACTIVE;
143 if (rs.batch_pending)
144 return RS_BATCH_PENDING;
149 static int gem_exec(int fd, struct drm_i915_gem_execbuffer2 *execbuf)
154 DRM_IOCTL_I915_GEM_EXECBUFFER2,
163 static int exec_valid(int fd, int ctx)
165 struct drm_i915_gem_execbuffer2 execbuf;
166 struct drm_i915_gem_exec_object2 exec;
169 uint32_t buf[2] = { MI_BATCH_BUFFER_END, 0 };
171 exec.handle = gem_create(fd, 4096);
172 gem_write(fd, exec.handle, 0, buf, sizeof(buf));
173 exec.relocation_count = 0;
181 execbuf.buffers_ptr = (uintptr_t)&exec;
182 execbuf.buffer_count = 1;
183 execbuf.batch_start_offset = 0;
184 execbuf.batch_len = sizeof(buf);
185 execbuf.cliprects_ptr = 0;
186 execbuf.num_cliprects = 0;
190 i915_execbuffer2_set_context_id(execbuf, ctx);
193 ret = gem_exec(fd, &execbuf);
200 static void stop_rings(void)
204 fd = igt_debugfs_open(&dfs, "i915_ring_stop", O_WRONLY);
207 igt_assert(write(fd, "0xff", 4) == 4);
211 #define BUFSIZE (4 * 1024)
212 #define ITEMS (BUFSIZE >> 2)
214 static int inject_hang(int fd, int ctx)
216 struct drm_i915_gem_execbuffer2 execbuf;
217 struct drm_i915_gem_exec_object2 exec;
221 unsigned cmd_len = 2;
225 if (intel_gen(intel_get_drm_devid(fd)) >= 8)
228 buf = malloc(BUFSIZE);
229 igt_assert(buf != NULL);
231 buf[0] = MI_BATCH_BUFFER_END;
234 exec.handle = gem_create(fd, BUFSIZE);
235 gem_write(fd, exec.handle, 0, buf, BUFSIZE);
236 exec.relocation_count = 0;
244 execbuf.buffers_ptr = (uintptr_t)&exec;
245 execbuf.buffer_count = 1;
246 execbuf.batch_start_offset = 0;
247 execbuf.batch_len = BUFSIZE;
248 execbuf.cliprects_ptr = 0;
249 execbuf.num_cliprects = 0;
253 i915_execbuffer2_set_context_id(execbuf, ctx);
256 igt_assert(gem_exec(fd, &execbuf) == 0);
258 gtt_off = exec.offset;
260 for (i = 0; i < ITEMS; i++)
263 roff = random() % (ITEMS - cmd_len);
264 buf[roff] = MI_BATCH_BUFFER_START | (cmd_len - 2);
265 buf[roff + 1] = (gtt_off & 0xfffffffc) + (roff << 2);
267 buf[roff + 2] = gtt_off & 0xffffffff00000000ull;
270 printf("loop injected at 0x%lx (off 0x%x, bo_start 0x%lx, bo_end 0x%lx)\n",
271 (long unsigned int)((roff << 2) + gtt_off),
272 roff << 2, (long unsigned int)gtt_off,
273 (long unsigned int)(gtt_off + BUFSIZE - 1));
275 gem_write(fd, exec.handle, 0, buf, BUFSIZE);
277 exec.relocation_count = 0;
285 execbuf.buffers_ptr = (uintptr_t)&exec;
286 execbuf.buffer_count = 1;
287 execbuf.batch_start_offset = 0;
288 execbuf.batch_len = BUFSIZE;
289 execbuf.cliprects_ptr = 0;
290 execbuf.num_cliprects = 0;
294 i915_execbuffer2_set_context_id(execbuf, ctx);
297 igt_assert(gem_exec(fd, &execbuf) == 0);
299 igt_assert(gtt_off == exec.offset);
308 static int _assert_reset_status(int fd, int ctx, int status)
312 rs = gem_reset_status(fd, ctx);
314 printf("reset status for %d ctx %d returned %d\n",
320 printf("%d:%d reset status %d differs from assumed %d\n",
321 fd, ctx, rs, status);
329 #define assert_reset_status(fd, ctx, status) \
330 igt_assert(_assert_reset_status(fd, ctx, status) == 0)
332 static void test_rs(int num_fds, int hang_index, int rs_assumed_no_hang)
338 igt_assert (num_fds <= MAX_FD);
339 igt_assert (hang_index < MAX_FD);
341 for (i = 0; i < num_fds; i++) {
342 fd[i] = drm_open_any();
346 for (i = 0; i < num_fds; i++)
347 assert_reset_status(fd[i], 0, RS_NO_ERROR);
349 for (i = 0; i < num_fds; i++) {
351 h[i] = inject_hang(fd[i], 0);
353 h[i] = exec_valid(fd[i], 0);
356 gem_sync(fd[num_fds - 1], h[num_fds - 1]);
358 for (i = 0; i < num_fds; i++) {
359 if (hang_index < 0) {
360 assert_reset_status(fd[i], 0, rs_assumed_no_hang);
365 assert_reset_status(fd[i], 0, RS_NO_ERROR);
367 assert_reset_status(fd[i], 0, RS_BATCH_ACTIVE);
369 assert_reset_status(fd[i], 0, RS_BATCH_PENDING);
372 for (i = 0; i < num_fds; i++) {
373 gem_close(fd[i], h[i]);
379 static void test_rs_ctx(int num_fds, int num_ctx, int hang_index,
384 int h[MAX_FD][MAX_CTX];
385 int ctx[MAX_FD][MAX_CTX];
387 igt_assert (num_fds <= MAX_FD);
388 igt_assert (hang_index < MAX_FD);
390 igt_assert (num_ctx <= MAX_CTX);
391 igt_assert (hang_context < MAX_CTX);
393 test_rs(num_fds, -1, RS_NO_ERROR);
395 for (i = 0; i < num_fds; i++) {
396 fd[i] = drm_open_any();
398 assert_reset_status(fd[i], 0, RS_NO_ERROR);
400 for (j = 0; j < num_ctx; j++) {
401 ctx[i][j] = context_create(fd[i]);
405 assert_reset_status(fd[i], 0, RS_NO_ERROR);
408 for (i = 0; i < num_fds; i++) {
410 assert_reset_status(fd[i], 0, RS_NO_ERROR);
412 for (j = 0; j < num_ctx; j++)
413 assert_reset_status(fd[i], ctx[i][j], RS_NO_ERROR);
415 assert_reset_status(fd[i], 0, RS_NO_ERROR);
418 for (i = 0; i < num_fds; i++) {
419 for (j = 0; j < num_ctx; j++) {
420 if (i == hang_index && j == hang_context)
421 h[i][j] = inject_hang(fd[i], ctx[i][j]);
423 h[i][j] = exec_valid(fd[i], ctx[i][j]);
427 gem_sync(fd[num_fds - 1], ctx[num_fds - 1][num_ctx - 1]);
429 for (i = 0; i < num_fds; i++)
430 assert_reset_status(fd[i], 0, RS_NO_ERROR);
432 for (i = 0; i < num_fds; i++) {
433 for (j = 0; j < num_ctx; j++) {
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], RS_NO_ERROR);
438 if (i == hang_index && j == hang_context)
439 assert_reset_status(fd[i], ctx[i][j],
441 if (i == hang_index && j > hang_context)
442 assert_reset_status(fd[i], ctx[i][j],
445 assert_reset_status(fd[i], ctx[i][j],
450 for (i = 0; i < num_fds; i++) {
451 for (j = 0; j < num_ctx; j++) {
452 gem_close(fd[i], h[i][j]);
453 igt_assert(context_destroy(fd[i], ctx[i][j]) == 0);
456 assert_reset_status(fd[i], 0, RS_NO_ERROR);
462 static void test_ban(void)
464 int h1,h2,h3,h4,h5,h6,h7;
465 int ctx_good, ctx_bad;
468 int active_count = 0, pending_count = 0;
469 struct local_drm_i915_reset_stats rs_bad, rs_good;
474 assert_reset_status(fd, 0, RS_NO_ERROR);
476 ctx_good = context_create(fd);
477 ctx_bad = context_create(fd);
479 assert_reset_status(fd, 0, RS_NO_ERROR);
480 assert_reset_status(fd, ctx_good, RS_NO_ERROR);
481 assert_reset_status(fd, ctx_bad, RS_NO_ERROR);
483 h1 = exec_valid(fd, ctx_bad);
485 h5 = exec_valid(fd, ctx_good);
488 assert_reset_status(fd, ctx_good, RS_NO_ERROR);
489 assert_reset_status(fd, ctx_bad, RS_NO_ERROR);
491 h2 = inject_hang(fd, ctx_bad);
494 /* Second hang will be pending for this */
497 h6 = exec_valid(fd, ctx_good);
498 h7 = exec_valid(fd, ctx_good);
501 h3 = inject_hang(fd, ctx_bad);
505 /* This second hand will count as pending */
506 assert_reset_status(fd, ctx_bad, RS_BATCH_ACTIVE);
508 h4 = exec_valid(fd, ctx_bad);
514 /* Should not happen often but sometimes hang is declared too slow
515 * due to our way of faking hang using loop */
521 printf("retrying for ban (%d)\n", retry);
524 igt_assert(h4 == -EIO);
525 assert_reset_status(fd, ctx_bad, RS_BATCH_ACTIVE);
528 assert_reset_status(fd, ctx_good, RS_BATCH_PENDING);
530 igt_assert(gem_reset_stats(fd, ctx_good, &rs_good) == 0);
531 igt_assert(gem_reset_stats(fd, ctx_bad, &rs_bad) == 0);
533 igt_assert(rs_bad.batch_active == active_count);
534 igt_assert(rs_bad.batch_pending == pending_count);
535 igt_assert(rs_good.batch_active == 0);
536 igt_assert(rs_good.batch_pending == 2);
543 h1 = exec_valid(fd, ctx_good);
547 igt_assert(context_destroy(fd, ctx_good) == 0);
548 igt_assert(context_destroy(fd, ctx_bad) == 0);
549 igt_assert(gem_reset_status(fd, ctx_good) < 0);
550 igt_assert(gem_reset_status(fd, ctx_bad) < 0);
551 igt_assert(exec_valid(fd, ctx_good) < 0);
552 igt_assert(exec_valid(fd, ctx_bad) < 0);
557 static void test_nonrelated_hang(void)
561 int ctx_guilty, ctx_unrelated;
563 fd1 = drm_open_any();
564 fd2 = drm_open_any();
565 assert_reset_status(fd1, 0, RS_NO_ERROR);
566 assert_reset_status(fd2, 0, RS_NO_ERROR);
567 ctx_guilty = context_create(fd1);
568 ctx_unrelated = context_create(fd2);
570 assert_reset_status(fd1, ctx_guilty, RS_NO_ERROR);
571 assert_reset_status(fd2, ctx_unrelated, RS_NO_ERROR);
573 h1 = inject_hang(fd1, ctx_guilty);
576 assert_reset_status(fd1, ctx_guilty, RS_BATCH_ACTIVE);
577 assert_reset_status(fd2, ctx_unrelated, RS_NO_ERROR);
579 h2 = exec_valid(fd2, ctx_unrelated);
582 assert_reset_status(fd1, ctx_guilty, RS_BATCH_ACTIVE);
583 assert_reset_status(fd2, ctx_unrelated, RS_NO_ERROR);
587 igt_assert(context_destroy(fd1, ctx_guilty) == 0);
588 igt_assert(context_destroy(fd2, ctx_unrelated) == 0);
594 static int get_reset_count(int fd, int ctx)
597 struct local_drm_i915_reset_stats rs;
599 ret = gem_reset_stats(fd, ctx, &rs);
603 return rs.reset_count;
606 static void test_double_destroy_pending(void)
613 ctx = context_create(fd);
615 assert_reset_status(fd, ctx, RS_NO_ERROR);
617 h = inject_hang(fd, ctx);
619 igt_assert(context_destroy(fd, ctx) == 0);
620 igt_assert(context_destroy(fd, ctx) == -ENOENT);
626 static void test_close_pending(void)
633 assert_reset_status(fd, 0, RS_NO_ERROR);
635 h = inject_hang(fd, 0);
642 #define LOCAL_I915_EXEC_VEBOX (4 << 0)
644 static const struct target_ring {
649 { I915_EXEC_BLT, gem_has_blt },
650 { I915_EXEC_BSD, gem_has_bsd },
651 { LOCAL_I915_EXEC_VEBOX, gem_has_vebox },
654 #define NUM_RINGS (sizeof(rings)/sizeof(struct target_ring))
656 static void exec_noop_on_each_ring(int fd, const bool reverse)
658 uint32_t batch[2] = {MI_BATCH_BUFFER_END, 0};
660 struct drm_i915_gem_execbuffer2 execbuf;
661 struct drm_i915_gem_exec_object2 exec[1];
663 handle = gem_create(fd, 4096);
664 gem_write(fd, handle, 0, batch, sizeof(batch));
666 exec[0].handle = handle;
667 exec[0].relocation_count = 0;
668 exec[0].relocs_ptr = 0;
669 exec[0].alignment = 0;
675 execbuf.buffers_ptr = (uintptr_t)exec;
676 execbuf.buffer_count = 1;
677 execbuf.batch_start_offset = 0;
678 execbuf.batch_len = 8;
679 execbuf.cliprects_ptr = 0;
680 execbuf.num_cliprects = 0;
684 i915_execbuffer2_set_context_id(execbuf, 0);
687 for (unsigned i = 0; i < NUM_RINGS; i++) {
688 const struct target_ring *ring;
690 ring = reverse ? &rings[NUM_RINGS - 1 - i] : &rings[i];
692 if (!ring->avail || (ring->avail && ring->avail(fd))) {
693 execbuf.flags = ring->exec;
694 do_ioctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &execbuf);
698 gem_sync(fd, handle);
699 gem_close(fd, handle);
702 static void test_close_pending_fork(const bool reverse)
710 assert_reset_status(fd, 0, RS_NO_ERROR);
712 h = inject_hang(fd, 0);
717 /* Avoid helpers as we need to kill the child
718 * without any extra signal handling on behalf of
723 const int fd2 = drm_open_any();
724 igt_assert(fd2 >= 0);
726 /* The crucial component is that we schedule the same noop batch
727 * on each ring. This exercises batch_obj reference counting,
728 * when gpu is reset and ring lists are cleared.
730 exec_noop_on_each_ring(fd2, reverse);
738 /* Kill the child to reduce refcounts on
746 /* Then we just wait on hang to happen */
750 h = exec_valid(fd, 0);
758 static void drop_root(void)
760 igt_assert(getuid() == 0);
762 igt_assert(setgid(2) == 0);
763 igt_assert(setuid(2) == 0);
765 igt_assert(getgid() == 2);
766 igt_assert(getuid() == 2);
769 static void __test_count(const bool create_ctx)
777 ctx = context_create(fd);
781 assert_reset_status(fd, ctx, RS_NO_ERROR);
783 c1 = get_reset_count(fd, ctx);
786 h = inject_hang(fd, ctx);
790 assert_reset_status(fd, ctx, RS_BATCH_ACTIVE);
791 c2 = get_reset_count(fd, ctx);
793 igt_assert(c2 == (c1 + 1));
798 c2 = get_reset_count(fd, ctx);
801 igt_assert(c2 == -EPERM);
811 context_destroy(fd, ctx);
816 static void test_count(void)
818 return __test_count(false);
821 static void test_count_context(void)
823 return __test_count(true);
826 static void test_global_reset_count(void)
829 test_count_context();
832 static int _test_params(int fd, int ctx, uint32_t flags, uint32_t pad)
834 struct local_drm_i915_reset_stats rs;
839 rs.reset_count = rand();
840 rs.batch_active = rand();
841 rs.batch_pending = rand();
845 ret = ioctl(fd, GET_RESET_STATS_IOCTL, &rs);
846 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
854 typedef enum { root = 0, user } cap_t;
856 static void test_param_ctx(const int fd, const int ctx, const cap_t cap)
858 const uint32_t bad = rand() + 1;
862 igt_assert(_test_params(fd, ctx, 0, 0) == 0);
864 igt_assert(_test_params(fd, ctx, 0, 0) == -EPERM);
867 igt_assert(_test_params(fd, ctx, 0, bad) == -EINVAL);
868 igt_assert(_test_params(fd, ctx, bad, 0) == -EINVAL);
869 igt_assert(_test_params(fd, ctx, bad, bad) == -EINVAL);
872 static void check_params(const int fd, const int ctx, cap_t cap)
874 igt_assert(ioctl(fd, GET_RESET_STATS_IOCTL, 0) == -1);
875 igt_assert(_test_params(fd, 0xbadbad, 0, 0) == -ENOENT);
877 test_param_ctx(fd, 0, cap);
878 test_param_ctx(fd, ctx, cap);
881 static void _test_param(const int fd, const int ctx)
883 check_params(fd, ctx, root);
886 check_params(fd, ctx, root);
890 check_params(fd, ctx, user);
893 check_params(fd, ctx, root);
898 static void test_params(void)
904 ctx = context_create(fd);
906 _test_param(fd, ctx);
914 struct local_drm_i915_gem_context_create create;
919 igt_skip_on_simulation();
923 devid = intel_get_drm_devid(fd);
924 igt_require_f(intel_gen(devid) >= 4,
925 "Architecture %d too old\n", intel_gen(devid));
927 ret = drmIoctl(fd, CONTEXT_CREATE_IOCTL, &create);
928 igt_skip_on_f(ret != 0 && (errno == ENODEV || errno == EINVAL),
929 "Kernel is too old, or contexts not supported: %s\n",
932 assert(igt_debugfs_init(&dfs) == 0);
937 igt_subtest("basic-reset-status")
940 igt_subtest("context-reset-status")
941 test_rs_ctx(4, 4, 1, 2);
946 igt_subtest("ctx-unrelated")
947 test_nonrelated_hang();
949 igt_subtest("global-count")
950 test_global_reset_count();
952 igt_subtest("double-destroy-pending")
953 test_double_destroy_pending();
955 igt_subtest("close-pending")
956 test_close_pending();
958 igt_subtest("close-pending-fork") {
959 test_close_pending_fork(true);
960 test_close_pending_fork(false);
963 igt_subtest("params")