drm/i915: Add unit tests for the breadcrumb rbtree, insert/remove
authorChris Wilson <chris@chris-wilson.co.uk>
Mon, 13 Feb 2017 17:15:14 +0000 (17:15 +0000)
committerChris Wilson <chris@chris-wilson.co.uk>
Mon, 13 Feb 2017 20:45:24 +0000 (20:45 +0000)
First retroactive test, make sure that the waiters are in global seqno
order after random inserts and removals.

Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk>
Reviewed-by: Tvrtko Ursulin <tvrtko.ursulin@intel.com>
Link: http://patchwork.freedesktop.org/patch/msgid/20170213171558.20942-3-chris@chris-wilson.co.uk
drivers/gpu/drm/i915/intel_breadcrumbs.c
drivers/gpu/drm/i915/intel_engine_cs.c
drivers/gpu/drm/i915/intel_ringbuffer.h
drivers/gpu/drm/i915/selftests/i915_mock_selftests.h
drivers/gpu/drm/i915/selftests/intel_breadcrumbs.c [new file with mode: 0644]
drivers/gpu/drm/i915/selftests/mock_engine.c [new file with mode: 0644]
drivers/gpu/drm/i915/selftests/mock_engine.h [new file with mode: 0644]

index f5e05343110af3e3b7d1384d20b3587321169ec1..74cb7b91b5dbd26c988ba5ad9b220d2c5ff99a88 100644 (file)
@@ -107,6 +107,18 @@ static void __intel_breadcrumbs_enable_irq(struct intel_breadcrumbs *b)
        if (b->rpm_wakelock)
                return;
 
+       if (I915_SELFTEST_ONLY(b->mock)) {
+               /* For our mock objects we want to avoid interaction
+                * with the real hardware (which is not set up). So
+                * we simply pretend we have enabled the powerwell
+                * and the irq, and leave it up to the mock
+                * implementation to call intel_engine_wakeup()
+                * itself when it wants to simulate a user interrupt,
+                */
+               b->rpm_wakelock = true;
+               return;
+       }
+
        /* Since we are waiting on a request, the GPU should be busy
         * and should have its own rpm reference. For completeness,
         * record an rpm reference for ourselves to cover the
@@ -142,6 +154,11 @@ static void __intel_breadcrumbs_disable_irq(struct intel_breadcrumbs *b)
        if (!b->rpm_wakelock)
                return;
 
+       if (I915_SELFTEST_ONLY(b->mock)) {
+               b->rpm_wakelock = false;
+               return;
+       }
+
        if (b->irq_enabled) {
                irq_disable(engine);
                b->irq_enabled = false;
@@ -664,3 +681,7 @@ unsigned int intel_breadcrumbs_busy(struct drm_i915_private *i915)
 
        return mask;
 }
+
+#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
+#include "selftests/intel_breadcrumbs.c"
+#endif
index 69a6416d12236db4042546e03856cae494c1f2c7..538d845d72513abf94013a9f5e8bd48336cfd7ed 100644 (file)
@@ -524,3 +524,7 @@ void intel_engine_get_instdone(struct intel_engine_cs *engine,
                break;
        }
 }
+
+#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
+#include "selftests/mock_engine.c"
+#endif
index b122c3cc6dbb044f98b734630094b751e2f889e9..cc62e89010d350a9c46eb800a5136acb8d482f80 100644 (file)
@@ -5,6 +5,7 @@
 #include "i915_gem_batch_pool.h"
 #include "i915_gem_request.h"
 #include "i915_gem_timeline.h"
+#include "i915_selftest.h"
 
 #define I915_CMD_HASH_ORDER 9
 
@@ -249,6 +250,7 @@ struct intel_engine_cs {
 
                bool irq_enabled : 1;
                bool rpm_wakelock : 1;
+               I915_SELFTEST_DECLARE(bool mock : 1);
        } breadcrumbs;
 
        /*
index 5f0bdda42ed86930e5379b89cbc9a306c51b87d3..80458e2a2b049745257362ac535513a975f7c617 100644 (file)
@@ -10,3 +10,4 @@
  */
 selftest(sanitycheck, i915_mock_sanitycheck) /* keep first (igt selfcheck) */
 selftest(scatterlist, scatterlist_mock_selftests)
+selftest(breadcrumbs, intel_breadcrumbs_mock_selftests)
diff --git a/drivers/gpu/drm/i915/selftests/intel_breadcrumbs.c b/drivers/gpu/drm/i915/selftests/intel_breadcrumbs.c
new file mode 100644 (file)
index 0000000..6b5acf9
--- /dev/null
@@ -0,0 +1,172 @@
+/*
+ * Copyright © 2016 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ *
+ */
+
+#include "../i915_selftest.h"
+#include "i915_random.h"
+
+#include "mock_engine.h"
+
+static int check_rbtree(struct intel_engine_cs *engine,
+                       const unsigned long *bitmap,
+                       const struct intel_wait *waiters,
+                       const int count)
+{
+       struct intel_breadcrumbs *b = &engine->breadcrumbs;
+       struct rb_node *rb;
+       int n;
+
+       if (&b->first_wait->node != rb_first(&b->waiters)) {
+               pr_err("First waiter does not match first element of wait-tree\n");
+               return -EINVAL;
+       }
+
+       n = find_first_bit(bitmap, count);
+       for (rb = rb_first(&b->waiters); rb; rb = rb_next(rb)) {
+               struct intel_wait *w = container_of(rb, typeof(*w), node);
+               int idx = w - waiters;
+
+               if (!test_bit(idx, bitmap)) {
+                       pr_err("waiter[%d, seqno=%d] removed but still in wait-tree\n",
+                              idx, w->seqno);
+                       return -EINVAL;
+               }
+
+               if (n != idx) {
+                       pr_err("waiter[%d, seqno=%d] does not match expected next element in tree [%d]\n",
+                              idx, w->seqno, n);
+                       return -EINVAL;
+               }
+
+               n = find_next_bit(bitmap, count, n + 1);
+       }
+
+       return 0;
+}
+
+static int check_rbtree_empty(struct intel_engine_cs *engine)
+{
+       struct intel_breadcrumbs *b = &engine->breadcrumbs;
+
+       if (b->first_wait) {
+               pr_err("Empty breadcrumbs still has a waiter\n");
+               return -EINVAL;
+       }
+
+       if (!RB_EMPTY_ROOT(&b->waiters)) {
+               pr_err("Empty breadcrumbs, but wait-tree not empty\n");
+               return -EINVAL;
+       }
+
+       return 0;
+}
+
+static int igt_random_insert_remove(void *arg)
+{
+       const u32 seqno_bias = 0x1000;
+       I915_RND_STATE(prng);
+       struct intel_engine_cs *engine = arg;
+       struct intel_wait *waiters;
+       const int count = 4096;
+       unsigned int *order;
+       unsigned long *bitmap;
+       int err = -ENOMEM;
+       int n;
+
+       mock_engine_reset(engine);
+
+       waiters = drm_malloc_gfp(count, sizeof(*waiters), GFP_TEMPORARY);
+       if (!waiters)
+               goto out_engines;
+
+       bitmap = kcalloc(DIV_ROUND_UP(count, BITS_PER_LONG), sizeof(*bitmap),
+                        GFP_TEMPORARY);
+       if (!bitmap)
+               goto out_waiters;
+
+       order = i915_random_order(count, &prng);
+       if (!order)
+               goto out_bitmap;
+
+       for (n = 0; n < count; n++)
+               intel_wait_init(&waiters[n], seqno_bias + n);
+
+       err = check_rbtree(engine, bitmap, waiters, count);
+       if (err)
+               goto out_order;
+
+       /* Add and remove waiters into the rbtree in random order. At each
+        * step, we verify that the rbtree is correctly ordered.
+        */
+       for (n = 0; n < count; n++) {
+               int i = order[n];
+
+               intel_engine_add_wait(engine, &waiters[i]);
+               __set_bit(i, bitmap);
+
+               err = check_rbtree(engine, bitmap, waiters, count);
+               if (err)
+                       goto out_order;
+       }
+
+       i915_random_reorder(order, count, &prng);
+       for (n = 0; n < count; n++) {
+               int i = order[n];
+
+               intel_engine_remove_wait(engine, &waiters[i]);
+               __clear_bit(i, bitmap);
+
+               err = check_rbtree(engine, bitmap, waiters, count);
+               if (err)
+                       goto out_order;
+       }
+
+       err = check_rbtree_empty(engine);
+out_order:
+       kfree(order);
+out_bitmap:
+       kfree(bitmap);
+out_waiters:
+       drm_free_large(waiters);
+out_engines:
+       mock_engine_flush(engine);
+       return err;
+}
+
+int intel_breadcrumbs_mock_selftests(void)
+{
+       static const struct i915_subtest tests[] = {
+               SUBTEST(igt_random_insert_remove),
+       };
+       struct intel_engine_cs *engine;
+       int err;
+
+       engine = mock_engine("mock");
+       if (!engine)
+               return -ENOMEM;
+
+       err = i915_subtests(tests, engine);
+       kfree(engine);
+
+       return err;
+}
diff --git a/drivers/gpu/drm/i915/selftests/mock_engine.c b/drivers/gpu/drm/i915/selftests/mock_engine.c
new file mode 100644 (file)
index 0000000..4a090bb
--- /dev/null
@@ -0,0 +1,55 @@
+/*
+ * Copyright © 2016 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ *
+ */
+
+#include "mock_engine.h"
+
+struct intel_engine_cs *mock_engine(const char *name)
+{
+       struct intel_engine_cs *engine;
+       static int id;
+
+       engine = kzalloc(sizeof(*engine) + PAGE_SIZE, GFP_KERNEL);
+       if (!engine)
+               return NULL;
+
+       /* minimal engine setup for seqno */
+       engine->name = name;
+       engine->id = id++;
+       engine->status_page.page_addr = (void *)(engine + 1);
+
+       /* minimal breadcrumbs init */
+       spin_lock_init(&engine->breadcrumbs.lock);
+       engine->breadcrumbs.mock = true;
+
+       return engine;
+}
+
+void mock_engine_flush(struct intel_engine_cs *engine)
+{
+}
+
+void mock_engine_reset(struct intel_engine_cs *engine)
+{
+       intel_write_status_page(engine, I915_GEM_HWS_INDEX, 0);
+}
diff --git a/drivers/gpu/drm/i915/selftests/mock_engine.h b/drivers/gpu/drm/i915/selftests/mock_engine.h
new file mode 100644 (file)
index 0000000..0ae9a94
--- /dev/null
@@ -0,0 +1,32 @@
+/*
+ * Copyright © 2016 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ *
+ */
+
+#ifndef __MOCK_ENGINE_H__
+#define __MOCK_ENGINE_H__
+
+struct intel_engine_cs *mock_engine(const char *name);
+void mock_engine_flush(struct intel_engine_cs *engine);
+void mock_engine_reset(struct intel_engine_cs *engine);
+
+#endif /* !__MOCK_ENGINE_H__ */