Check that dpll_pin_register() rejects a DPLL of another owner and a
DPLL that is not registered, that dpll_pin_on_pin_register() rejects a
parent pin of another owner and a parent pin registered with a DPLL
that is not registered any more, and that a pin of the owner is still
shared by its DPLLs and registered under its parent pin.

The checks are WARN_ON_ONCE(), so the tests suppress the warning without
counting it. A registration that is not rejected is undone, so the next
case does not get the registered pin and the failure stays in its case.

Signed-off-by: Ivan Vecera <[email protected]>
---
 drivers/dpll/Kconfig            |   4 +-
 drivers/dpll/tests/dpll_kunit.c | 150 +++++++++++++++++++++++++++++---
 2 files changed, 142 insertions(+), 12 deletions(-)

diff --git a/drivers/dpll/Kconfig b/drivers/dpll/Kconfig
index d57696652d450a..9be76330a1d1de 100644
--- a/drivers/dpll/Kconfig
+++ b/drivers/dpll/Kconfig
@@ -31,7 +31,9 @@ config DPLL_KUNIT_TEST
        help
          KUnit tests for the DPLL core. They use a mock driver to check
          that links between pins of two devices are reported and notified
-         while the pins at both ends are registered.
+         while the pins at both ends are registered, and that a pin is
+         registered only with the registered devices and the parent pins
+         of its owner.
 
          The mock devices and pins are registered with the DPLL subsystem,
          so user space sees them and their create and delete notifications
diff --git a/drivers/dpll/tests/dpll_kunit.c b/drivers/dpll/tests/dpll_kunit.c
index 1804e5836f7db2..20bda0934acb1d 100644
--- a/drivers/dpll/tests/dpll_kunit.c
+++ b/drivers/dpll/tests/dpll_kunit.c
@@ -1,11 +1,12 @@
 // SPDX-License-Identifier: GPL-2.0
 /*
- * KUnit tests for links between DPLL pins.
+ * KUnit tests for links between DPLL pins and for the owner checks of pin
+ * registrations.
  *
  * Each test starts with two owners that differ in clock_id:
  *
  * Owner A: devices A0 and A1, pin P with firmware node F, pin U with
- *          neither a firmware node nor a link
+ *          neither a firmware node nor a link, MUX pin M
  * Owner B: device B0, pin C with a link to F
  *
  * The devices are registered, the pins are not.
@@ -40,9 +41,9 @@ struct dt_pin {
 
 struct dt_ctx {
        struct dt_dev a0, a1, b0;
-       struct dt_pin p, u, c;
+       struct dt_pin p, u, c, m;
        struct fwnode_handle *fwnode;
-       bool p_a0, p_a1, u_a0, c_b0;    /* registrations */
+       bool p_a0, p_a1, u_a0, c_b0, m_a0, u_m; /* registrations */
 };
 
 static int dt_mode_get(const struct dpll_device *dpll, void *dpll_priv,
@@ -89,10 +90,29 @@ static const struct dpll_pin_ops dt_pin_ops = {
        .direction_get = dt_direction_get,
 };
 
+static int dt_state_on_pin_get(const struct dpll_pin *pin, void *pin_priv,
+                              const struct dpll_pin *parent_pin,
+                              void *parent_pin_priv,
+                              enum dpll_pin_state *state,
+                              struct netlink_ext_ack *extack)
+{
+       *state = DPLL_PIN_STATE_CONNECTED;
+       return 0;
+}
+
+static const struct dpll_pin_ops dt_child_ops = {
+       .state_on_pin_get = dt_state_on_pin_get,
+       .direction_get = dt_direction_get,
+};
+
 static const struct dpll_pin_properties dt_pin_prop = {
        .type = DPLL_PIN_TYPE_EXT,
 };
 
+static const struct dpll_pin_properties dt_mux_prop = {
+       .type = DPLL_PIN_TYPE_MUX,
+};
+
 /* Stub of __dpll_pin_change_ntf() that counts the notifications */
 static int dt_pin_change_ntf(struct dpll_pin *pin)
 {
@@ -130,9 +150,9 @@ static void dt_dev_fini(struct dt_dev *dev)
 }
 
 static void dt_pin_get(struct kunit *test, struct dt_pin *pin, u64 clock_id,
-                      u32 idx)
+                      u32 idx, const struct dpll_pin_properties *prop)
 {
-       pin->pin = dpll_pin_get(clock_id, idx, THIS_MODULE, &dt_pin_prop,
+       pin->pin = dpll_pin_get(clock_id, idx, THIS_MODULE, prop,
                                &pin->tracker);
        KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pin->pin);
 }
@@ -223,9 +243,10 @@ static int dt_init(struct kunit *test)
        dt_dev_init(test, &ctx->a0, DT_CLOCK_ID_A, 0);
        dt_dev_init(test, &ctx->a1, DT_CLOCK_ID_A, 1);
        dt_dev_init(test, &ctx->b0, DT_CLOCK_ID_B, 0);
-       dt_pin_get(test, &ctx->p, DT_CLOCK_ID_A, 0);
-       dt_pin_get(test, &ctx->u, DT_CLOCK_ID_A, 1);
-       dt_pin_get(test, &ctx->c, DT_CLOCK_ID_B, 0);
+       dt_pin_get(test, &ctx->p, DT_CLOCK_ID_A, 0, &dt_pin_prop);
+       dt_pin_get(test, &ctx->u, DT_CLOCK_ID_A, 1, &dt_pin_prop);
+       dt_pin_get(test, &ctx->m, DT_CLOCK_ID_A, 2, &dt_mux_prop);
+       dt_pin_get(test, &ctx->c, DT_CLOCK_ID_B, 0, &dt_pin_prop);
        dpll_pin_fwnode_set(ctx->p.pin, ctx->fwnode);
        ret = dpll_pin_link_add(ctx->c.pin, ctx->fwnode);
        KUNIT_ASSERT_EQ(test, ret, 0);
@@ -242,11 +263,16 @@ static void dt_exit(struct kunit *test)
 
        if (!ctx)
                return;
+       if (ctx->u_m)
+               dpll_pin_on_pin_unregister(ctx->m.pin, ctx->u.pin,
+                                          &dt_child_ops, &ctx->u);
+       dt_unreg(&ctx->a0, &ctx->m, &ctx->m_a0);
        dt_unreg(&ctx->a0, &ctx->p, &ctx->p_a0);
        dt_unreg(&ctx->a1, &ctx->p, &ctx->p_a1);
        dt_unreg(&ctx->a0, &ctx->u, &ctx->u_a0);
        dt_unreg(&ctx->b0, &ctx->c, &ctx->c_b0);
        dt_pin_put(&ctx->c);
+       dt_pin_put(&ctx->m);
        dt_pin_put(&ctx->u);
        dt_pin_put(&ctx->p);
        dt_dev_fini(&ctx->b0);
@@ -404,7 +430,109 @@ static struct kunit_suite dt_suite = {
        .test_cases = dt_cases,
 };
 
-kunit_test_suite(dt_suite);
+/*
+ * The checks are WARN_ON_ONCE(), so the tests suppress the warning without
+ * counting it. A registration that is not rejected is undone, otherwise the
+ * next case would get the registered pin from dpll_pin_get() and fail too.
+ */
+
+/* Expect the registration of @pin with @dev to be rejected */
+static void dt_expect_reg_rejected(struct kunit *test, struct dt_dev *dev,
+                                  struct dt_pin *pin)
+{
+       int ret;
+
+       kunit_warning_suppress(test) {
+               ret = dpll_pin_register(dev->dpll, pin->pin, &dt_pin_ops, pin);
+               KUNIT_EXPECT_EQ(test, ret, -EINVAL);
+               if (!ret)
+                       dpll_pin_unregister(dev->dpll, pin->pin, &dt_pin_ops,
+                                           pin);
+       }
+}
+
+/* Expect the registration of @pin under @parent to be rejected */
+static void dt_expect_on_pin_rejected(struct kunit *test,
+                                     struct dt_pin *parent,
+                                     struct dt_pin *pin)
+{
+       int ret;
+
+       kunit_warning_suppress(test) {
+               ret = dpll_pin_on_pin_register(parent->pin, pin->pin,
+                                              &dt_child_ops, pin);
+               KUNIT_EXPECT_EQ(test, ret, -EINVAL);
+               if (!ret)
+                       dpll_pin_on_pin_unregister(parent->pin, pin->pin,
+                                                  &dt_child_ops, pin);
+       }
+}
+
+/* A pin is registered with the DPLLs of its owner only */
+static void dt_test_owner_register(struct kunit *test)
+{
+       struct dt_ctx *ctx = test->priv;
+
+       dt_expect_reg_rejected(test, &ctx->b0, &ctx->p);
+       dt_reg(test, &ctx->a0, &ctx->p, &ctx->p_a0);
+       dt_reg(test, &ctx->a1, &ctx->p, &ctx->p_a1);
+}
+
+/* A pin is registered under the parent pins of its owner only */
+static void dt_test_owner_on_pin(struct kunit *test)
+{
+       struct dt_ctx *ctx = test->priv;
+       int ret;
+
+       dt_reg(test, &ctx->a0, &ctx->m, &ctx->m_a0);
+       dt_expect_on_pin_rejected(test, &ctx->m, &ctx->c);
+
+       ret = dpll_pin_on_pin_register(ctx->m.pin, ctx->u.pin, &dt_child_ops,
+                                      &ctx->u);
+       KUNIT_ASSERT_EQ(test, ret, 0);
+       ctx->u_m = true;
+}
+
+/* A pin is registered with a registered DPLL only */
+static void dt_test_unregistered_dpll(struct kunit *test)
+{
+       struct dt_ctx *ctx = test->priv;
+       struct dt_dev a2 = {};
+
+       a2.dpll = dpll_device_get(DT_CLOCK_ID_A, 2, THIS_MODULE, &a2.tracker);
+       KUNIT_ASSERT_NOT_ERR_OR_NULL(test, a2.dpll);
+       dt_expect_reg_rejected(test, &a2, &ctx->p);
+       dt_dev_fini(&a2);
+}
+
+/* A pin is registered under a parent pin with registered DPLLs only */
+static void dt_test_unregistered_parent_dpll(struct kunit *test)
+{
+       struct dt_ctx *ctx = test->priv;
+
+       /* The parent pin stays registered with a0 after a0 goes, as on E810 */
+       dt_reg(test, &ctx->a0, &ctx->m, &ctx->m_a0);
+       dpll_device_unregister(ctx->a0.dpll, &dt_dev_ops, &ctx->a0);
+       ctx->a0.registered = false;
+       dt_expect_on_pin_rejected(test, &ctx->m, &ctx->u);
+}
+
+static struct kunit_case dt_owner_cases[] = {
+       KUNIT_CASE(dt_test_owner_register),
+       KUNIT_CASE(dt_test_owner_on_pin),
+       KUNIT_CASE(dt_test_unregistered_dpll),
+       KUNIT_CASE(dt_test_unregistered_parent_dpll),
+       {}
+};
+
+static struct kunit_suite dt_owner_suite = {
+       .name = "dpll_pin_owner",
+       .init = dt_init,
+       .exit = dt_exit,
+       .test_cases = dt_owner_cases,
+};
+
+kunit_test_suites(&dt_suite, &dt_owner_suite);
 
-MODULE_DESCRIPTION("KUnit tests for links between DPLL pins");
+MODULE_DESCRIPTION("KUnit tests for links and owners of DPLL pins");
 MODULE_LICENSE("GPL");
-- 
2.56.1

Reply via email to