igt/gem_userptr_blits: Fix forked access test
[platform/upstream/intel-gpu-tools.git] / tests / gem_reset_stats.c
index 3e7460f..3f5a01f 100644 (file)
@@ -242,38 +242,10 @@ static int exec_valid(int fd, int ctx)
        return exec_valid_ring(fd, ctx, current_ring->exec);
 }
 
-static void stop_rings(const int mask)
-{
-       int fd;
-       char buf[80];
-
-       igt_assert((mask & ~((1 << NUM_RINGS) - 1)) == 0);
-       igt_assert(snprintf(buf, sizeof(buf), "0x%02x", mask) == 4);
-       fd = igt_debugfs_open("i915_ring_stop", O_WRONLY);
-       igt_assert(fd >= 0);
-
-       igt_assert(write(fd, buf, 4) == 4);
-       close(fd);
-}
-
 #define BUFSIZE (4 * 1024)
 #define ITEMS   (BUFSIZE >> 2)
 
-static int ring_to_mask(int ring)
-{
-       for (unsigned i = 0; i < NUM_RINGS; i++) {
-               const struct target_ring *r = &rings[i];
-
-               if (r->exec == ring)
-                       return (1 << i);
-       }
-
-       igt_assert(0);
-
-       return -1;
-}
-
-static int inject_hang_ring(int fd, int ctx, int ring)
+static int inject_hang_ring(int fd, int ctx, int ring, bool ignore_ban_error)
 {
        struct drm_i915_gem_execbuffer2 execbuf;
        struct drm_i915_gem_exec_object2 exec;
@@ -281,6 +253,7 @@ static int inject_hang_ring(int fd, int ctx, int ring)
        uint32_t *buf;
        int roff, i;
        unsigned cmd_len = 2;
+       enum stop_ring_flags flags;
 
        srandom(time(NULL));
 
@@ -322,18 +295,18 @@ static int inject_hang_ring(int fd, int ctx, int ring)
        for (i = 0; i < ITEMS; i++)
                buf[i] = MI_NOOP;
 
-       roff = random() % (ITEMS - cmd_len);
+       roff = random() % (ITEMS - cmd_len - 1);
        buf[roff] = MI_BATCH_BUFFER_START | (cmd_len - 2);
        buf[roff + 1] = (gtt_off & 0xfffffffc) + (roff << 2);
        if (cmd_len == 3)
-               buf[roff + 2] = gtt_off & 0xffffffff00000000ull;
-
-#ifdef VERBOSE
-       printf("loop injected at 0x%lx (off 0x%x, bo_start 0x%lx, bo_end 0x%lx)\n",
-              (long unsigned int)((roff << 2) + gtt_off),
-              roff << 2, (long unsigned int)gtt_off,
-              (long unsigned int)(gtt_off + BUFSIZE - 1));
-#endif
+               buf[roff + 2] = (gtt_off & 0xffffffff00000000ull) >> 32;
+
+       buf[roff + cmd_len] = MI_BATCH_BUFFER_END;
+
+       igt_debug("loop injected at 0x%lx (off 0x%x, bo_start 0x%lx, bo_end 0x%lx)\n",
+                 (long unsigned int)((roff << 2) + gtt_off),
+                 roff << 2, (long unsigned int)gtt_off,
+                 (long unsigned int)(gtt_off + BUFSIZE - 1));
        gem_write(fd, exec.handle, 0, buf, BUFSIZE);
 
        exec.relocation_count = 0;
@@ -362,14 +335,26 @@ static int inject_hang_ring(int fd, int ctx, int ring)
 
        free(buf);
 
-       stop_rings(ring_to_mask(ring));
+       flags = igt_to_stop_ring_flag(ring);
+
+       flags |= STOP_RING_ALLOW_BAN;
+
+       if (!ignore_ban_error)
+               flags |= STOP_RING_ALLOW_ERRORS;
+
+       igt_set_stop_rings(flags);
 
        return exec.handle;
 }
 
 static int inject_hang(int fd, int ctx)
 {
-       return inject_hang_ring(fd, ctx, current_ring->exec);
+       return inject_hang_ring(fd, ctx, current_ring->exec, false);
+}
+
+static int inject_hang_no_ban_error(int fd, int ctx)
+{
+       return inject_hang_ring(fd, ctx, current_ring->exec, true);
 }
 
 static int _assert_reset_status(int fd, int ctx, int status)
@@ -378,14 +363,14 @@ static int _assert_reset_status(int fd, int ctx, int status)
 
        rs = gem_reset_status(fd, ctx);
        if (rs < 0) {
-               printf("reset status for %d ctx %d returned %d\n",
-                      fd, ctx, rs);
+               igt_info("reset status for %d ctx %d returned %d\n",
+                        fd, ctx, rs);
                return rs;
        }
 
        if (rs != status) {
-               printf("%d:%d reset status %d differs from assumed %d\n",
-                      fd, ctx, rs, status);
+               igt_info("%d:%d reset status %d differs from assumed %d\n",
+                        fd, ctx, rs, status);
 
                return 1;
        }
@@ -551,7 +536,7 @@ static void test_ban(void)
        assert_reset_status(fd_bad, 0, RS_NO_ERROR);
        assert_reset_status(fd_good, 0, RS_NO_ERROR);
 
-       h2 = inject_hang(fd_bad, 0);
+       h2 = inject_hang_no_ban_error(fd_bad, 0);
        igt_assert(h2 >= 0);
        active_count++;
        /* Second hang will be pending for this */
@@ -561,7 +546,7 @@ static void test_ban(void)
        h7 = exec_valid(fd_good, 0);
 
         while (retry--) {
-                h3 = inject_hang(fd_bad, 0);
+                h3 = inject_hang_no_ban_error(fd_bad, 0);
                 igt_assert(h3 >= 0);
                 gem_sync(fd_bad, h3);
                active_count++;
@@ -581,7 +566,7 @@ static void test_ban(void)
                 gem_close(fd_bad, h3);
                 gem_close(fd_bad, h4);
 
-                printf("retrying for ban (%d)\n", retry);
+                igt_info("retrying for ban (%d)\n", retry);
         }
 
        igt_assert(h4 == -EIO);
@@ -643,7 +628,7 @@ static void test_ban_ctx(void)
        assert_reset_status(fd, ctx_good, RS_NO_ERROR);
        assert_reset_status(fd, ctx_bad, RS_NO_ERROR);
 
-       h2 = inject_hang(fd, ctx_bad);
+       h2 = inject_hang_no_ban_error(fd, ctx_bad);
        igt_assert(h2 >= 0);
        active_count++;
        /* Second hang will be pending for this */
@@ -653,7 +638,7 @@ static void test_ban_ctx(void)
        h7 = exec_valid(fd, ctx_good);
 
         while (retry--) {
-                h3 = inject_hang(fd, ctx_bad);
+                h3 = inject_hang_no_ban_error(fd, ctx_bad);
                 igt_assert(h3 >= 0);
                 gem_sync(fd, h3);
                active_count++;
@@ -673,7 +658,7 @@ static void test_ban_ctx(void)
                 gem_close(fd, h3);
                 gem_close(fd, h4);
 
-                printf("retrying for ban (%d)\n", retry);
+                igt_info("retrying for ban (%d)\n", retry);
         }
 
        igt_assert(h4 == -EIO);
@@ -1072,16 +1057,41 @@ static bool gem_has_reset_stats(int fd)
        return false;
 }
 
-#define RING_HAS_CONTEXTS (current_ring->contexts(current_ring))
-#define RUN_CTX_TEST(...) do { igt_skip_on(RING_HAS_CONTEXTS == false); __VA_ARGS__; } while (0)
+static void check_gpu_ok(void)
+{
+       int retry_count = 30;
+       enum stop_ring_flags flags;
+       int fd;
+
+       igt_debug("checking gpu state\n");
+
+       while (retry_count--) {
+               flags = igt_get_stop_rings();
+               if (flags == 0)
+                       break;
+
+               igt_debug("waiting previous hang to clear\n");
+               sleep(1);
+       }
+
+       igt_assert(flags == 0);
+
+       fd = drm_open_any();
+       gem_quiescent_gpu(fd);
+       close(fd);
+}
 
-static int fd;
+#define RING_HAS_CONTEXTS (current_ring->contexts(current_ring))
+#define RUN_TEST(...) do { check_gpu_ok(); __VA_ARGS__; check_gpu_ok(); } while (0)
+#define RUN_CTX_TEST(...) do { igt_skip_on(RING_HAS_CONTEXTS == false); RUN_TEST(__VA_ARGS__); } while (0)
 
 igt_main
 {
        igt_skip_on_simulation();
 
        igt_fixture {
+               int fd;
+
                bool has_reset_stats;
                fd = drm_open_any();
                devid = intel_get_drm_devid(fd);
@@ -1089,6 +1099,8 @@ igt_main
                hw_contexts = gem_has_hw_contexts(fd);
                has_reset_stats = gem_has_reset_stats(fd);
 
+               close(fd);
+
                igt_require_f(has_reset_stats,
                              "No reset stats ioctl support. Too old kernel?\n");
        }
@@ -1102,8 +1114,11 @@ igt_main
                current_ring = &rings[i];
                name = current_ring->name;
 
-               igt_fixture
+               igt_fixture {
+                       int fd = drm_open_any();
                        gem_require_ring(fd, current_ring->exec);
+                       close(fd);
+               }
 
                igt_fixture
                        igt_require_f(intel_gen(devid) >= 4,
@@ -1113,19 +1128,19 @@ igt_main
                        RUN_CTX_TEST(test_params_ctx());
 
                igt_subtest_f("reset-stats-%s", name)
-                       test_rs(4, 1, 0);
+                       RUN_TEST(test_rs(4, 1, 0));
 
                igt_subtest_f("reset-stats-ctx-%s", name)
                        RUN_CTX_TEST(test_rs_ctx(4, 4, 1, 2));
 
                igt_subtest_f("ban-%s", name)
-                       test_ban();
+                       RUN_TEST(test_ban());
 
                igt_subtest_f("ban-ctx-%s", name)
                        RUN_CTX_TEST(test_ban_ctx());
 
                igt_subtest_f("reset-count-%s", name)
-                       test_reset_count(false);
+                       RUN_TEST(test_reset_count(false));
 
                igt_subtest_f("reset-count-ctx-%s", name)
                        RUN_CTX_TEST(test_reset_count(true));
@@ -1133,22 +1148,16 @@ igt_main
                igt_subtest_f("unrelated-ctx-%s", name)
                        RUN_CTX_TEST(test_unrelated_ctx());
 
-               igt_subtest_f("close-pending-%s", name) {
-                       test_close_pending();
-                       gem_quiescent_gpu(fd);
-               }
+               igt_subtest_f("close-pending-%s", name)
+                       RUN_TEST(test_close_pending());
 
-               igt_subtest_f("close-pending-ctx-%s", name) {
+               igt_subtest_f("close-pending-ctx-%s", name)
                        RUN_CTX_TEST(test_close_pending_ctx());
-                       gem_quiescent_gpu(fd);
-               }
 
-               igt_subtest_f("close-pending-fork-%s", name) {
-                       test_close_pending_fork(true);
-                       test_close_pending_fork(false);
-               }
-       }
+               igt_subtest_f("close-pending-fork-%s", name)
+                       RUN_TEST(test_close_pending_fork(false));
 
-       igt_fixture
-               close(fd);
+               igt_subtest_f("close-pending-fork-reverse-%s", name)
+                       RUN_TEST(test_close_pending_fork(true));
+       }
 }