diff mbox series

[v5,05/10] drm/tests: bridge: add KUnit tests for DRM bridges (init and destroy)

Message ID 20241231-hotplug-drm-bridge-v5-5-173065a1ece1@bootlin.com (mailing list archive)
State New
Headers show
Series Add support for hot-pluggable DRM bridges | expand

Commit Message

Luca Ceresoli Dec. 31, 2024, 10:39 a.m. UTC
Add two simple KUnit tests for the newly introduced drm_bridge_init() and
the corresponding .destroy deallocation function.

Signed-off-by: Luca Ceresoli <luca.ceresoli@bootlin.com>

---

This patch was added in v5.
---
 drivers/gpu/drm/tests/Makefile          |   1 +
 drivers/gpu/drm/tests/drm_bridge_test.c | 128 ++++++++++++++++++++++++++++++++
 2 files changed, 129 insertions(+)
diff mbox series

Patch

diff --git a/drivers/gpu/drm/tests/Makefile b/drivers/gpu/drm/tests/Makefile
index 56dab563abd7a7ee7c147bd6b4927e2436b82e1d..909f98a132bb1d057b2666e8b891683ffb11cca4 100644
--- a/drivers/gpu/drm/tests/Makefile
+++ b/drivers/gpu/drm/tests/Makefile
@@ -4,6 +4,7 @@  obj-$(CONFIG_DRM_KUNIT_TEST_HELPERS) += \
 	drm_kunit_helpers.o
 
 obj-$(CONFIG_DRM_KUNIT_TEST) += \
+	drm_bridge_test.o \
 	drm_buddy_test.o \
 	drm_cmdline_parser_test.o \
 	drm_connector_test.o \
diff --git a/drivers/gpu/drm/tests/drm_bridge_test.c b/drivers/gpu/drm/tests/drm_bridge_test.c
new file mode 100644
index 0000000000000000000000000000000000000000..b42e6b81a904abccfe1b3e88999b64cc6b2d4946
--- /dev/null
+++ b/drivers/gpu/drm/tests/drm_bridge_test.c
@@ -0,0 +1,128 @@ 
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Kunit test for DRM bridges
+ */
+
+#include <drm/drm_bridge.h>
+#include <drm/drm_kunit_helpers.h>
+
+#include <kunit/test.h>
+
+struct drm_bridge_init_priv {
+	struct drm_device drm;
+	bool destroyed;
+};
+
+/*
+ * Mimick the typical struct defined by a bridge driver, which embeds a
+ * bridge plus other fields.
+ */
+struct dummy_drm_bridge {
+	struct drm_bridge_init_priv *init_priv;
+	struct drm_bridge bridge;
+};
+
+static struct dummy_drm_bridge *bridge_to_dummy(struct drm_bridge *bridge)
+{
+	return container_of(bridge, struct dummy_drm_bridge, bridge);
+}
+
+static void dummy_drm_bridge_destroy(struct drm_bridge *bridge)
+{
+	struct dummy_drm_bridge *dummy = bridge_to_dummy(bridge);
+
+	dummy->init_priv->destroyed = true;
+	kfree(dummy);
+}
+
+static const struct drm_bridge_funcs drm_bridge_dummy_funcs = {
+	.destroy		= dummy_drm_bridge_destroy,
+};
+
+static int drm_test_bridge_init(struct kunit *test)
+{
+	struct drm_bridge_init_priv *priv;
+	struct device *dev;
+
+	dev = drm_kunit_helper_alloc_device(test);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dev);
+
+	priv = drm_kunit_helper_alloc_drm_device(test, dev,
+						 struct drm_bridge_init_priv, drm,
+						 DRIVER_MODESET | DRIVER_ATOMIC);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, priv);
+
+	test->priv = priv;
+	return 0;
+}
+
+/*
+ * Test that the allocation and initialization of a bridge works as
+ * expected and doesn't report any error.
+ */
+static void drm_test_drm_bridge_init(struct kunit *test)
+{
+	struct drm_bridge_init_priv *priv = test->priv;
+	struct dummy_drm_bridge *dummy;
+	int ret;
+
+	dummy = kzalloc(sizeof(*dummy), GFP_KERNEL);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dummy);
+
+	dummy->init_priv = priv;
+
+	ret = drm_bridge_init(priv->drm.dev,
+			      &dummy->bridge,
+			      &drm_bridge_dummy_funcs);
+	KUNIT_EXPECT_EQ(test, ret, 0);
+
+	KUNIT_EXPECT_FALSE(test, priv->destroyed);
+}
+
+/*
+ * Test that the allocation and initialization of a bridge works as expected
+ * and doesn't report any error, and that the destroy func is called at the
+ * last drm_bridge_put().
+ */
+static void drm_test_drm_bridge_put(struct kunit *test)
+{
+	struct drm_bridge_init_priv *priv = test->priv;
+	struct dummy_drm_bridge *dummy;
+	int ret;
+
+	dummy = kzalloc(sizeof(*dummy), GFP_KERNEL);
+	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dummy);
+
+	dummy->init_priv = priv;
+
+	ret = drm_bridge_init(priv->drm.dev,
+			      &dummy->bridge,
+			      &drm_bridge_dummy_funcs);
+	KUNIT_EXPECT_EQ(test, ret, 0);
+
+	drm_bridge_get(&dummy->bridge);
+	drm_bridge_put(&dummy->bridge);
+	KUNIT_EXPECT_FALSE(test, priv->destroyed);
+	drm_bridge_put(&dummy->bridge);
+	KUNIT_EXPECT_TRUE(test, priv->destroyed);
+}
+
+static struct kunit_case drm_bridge_init_tests[] = {
+	KUNIT_CASE(drm_test_drm_bridge_init),
+	KUNIT_CASE(drm_test_drm_bridge_put),
+	{ }
+};
+
+static struct kunit_suite drm_bridge_init_test_suite = {
+	.name = "drm_bridge_init",
+	.init = drm_test_bridge_init,
+	.test_cases = drm_bridge_init_tests,
+};
+
+kunit_test_suites(
+	&drm_bridge_init_test_suite,
+);
+
+MODULE_AUTHOR("Luca Ceresoli <luca.ceresoli@bootlin.com>");
+MODULE_DESCRIPTION("Kunit test for drm_bridge functions");
+MODULE_LICENSE("GPL");