@@ -0,0 +1,419 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * KUnit tests for links between DPLL pins.
+ *
+ * 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
+ * Owner B: device B0, pin C with a link to F
+ *
+ * The devices are registered, the pins are not.
+ */
+
+#include <kunit/fwnode.h>
+#include <kunit/test.h>
+#include <linux/dpll.h>
+#include <linux/notifier.h>
+#include <net/netlink.h>
+
+#include "../dpll_core.h"
+#include "../dpll_netlink.h"
+
+MODULE_IMPORT_NS("EXPORTED_FOR_KUNIT_TESTING");
+
+#define DT_CLOCK_ID_A 0xd011a
+#define DT_CLOCK_ID_B 0xd011b
+#define DT_MAX_LINKS 4
+
+struct dt_dev {
+ struct dpll_device *dpll;
+ dpll_tracker tracker;
+ bool registered;
+};
+
+struct dt_pin {
+ struct dpll_pin *pin;
+ dpll_tracker tracker;
+ int changes; /* DPLL_PIN_CHANGED notifications */
+};
+
+struct dt_ctx {
+ struct dt_dev a0, a1, b0;
+ struct dt_pin p, u, c;
+ struct fwnode_handle *fwnode;
+ struct notifier_block nb;
+ bool nb_registered;
+ bool p_a0, p_a1, u_a0, c_b0; /* registrations */
+};
+
+static int dt_mode_get(const struct dpll_device *dpll, void *dpll_priv,
+ enum dpll_mode *mode, struct netlink_ext_ack *extack)
+{
+ *mode = DPLL_MODE_MANUAL;
+ return 0;
+}
+
+static int dt_lock_status_get(const struct dpll_device *dpll, void *dpll_priv,
+ enum dpll_lock_status *status,
+ enum dpll_lock_status_error *status_error,
+ struct netlink_ext_ack *extack)
+{
+ *status = DPLL_LOCK_STATUS_UNLOCKED;
+ return 0;
+}
+
+static const struct dpll_device_ops dt_dev_ops = {
+ .mode_get = dt_mode_get,
+ .lock_status_get = dt_lock_status_get,
+};
+
+static int dt_state_get(const struct dpll_pin *pin, void *pin_priv,
+ const struct dpll_device *dpll, void *dpll_priv,
+ enum dpll_pin_state *state,
+ struct netlink_ext_ack *extack)
+{
+ *state = DPLL_PIN_STATE_CONNECTED;
+ return 0;
+}
+
+static int dt_direction_get(const struct dpll_pin *pin, void *pin_priv,
+ const struct dpll_device *dpll, void *dpll_priv,
+ enum dpll_pin_direction *direction,
+ struct netlink_ext_ack *extack)
+{
+ *direction = DPLL_PIN_DIRECTION_INPUT;
+ return 0;
+}
+
+static const struct dpll_pin_ops dt_pin_ops = {
+ .state_on_dpll_get = dt_state_get,
+ .direction_get = dt_direction_get,
+};
+
+static const struct dpll_pin_properties dt_pin_prop = {
+ .type = DPLL_PIN_TYPE_EXT,
+};
+
+static int dt_notify(struct notifier_block *nb, unsigned long action,
+ void *data)
+{
+ struct dt_ctx *ctx = container_of(nb, struct dt_ctx, nb);
+ struct dpll_pin_notifier_info *info = data;
+
+ if (action != DPLL_PIN_CHANGED)
+ return NOTIFY_DONE;
+ if (info->pin == ctx->p.pin)
+ ctx->p.changes++;
+ else if (info->pin == ctx->u.pin)
+ ctx->u.changes++;
+ else if (info->pin == ctx->c.pin)
+ ctx->c.changes++;
+
+ return NOTIFY_OK;
+}
+
+static void dt_dev_init(struct kunit *test, struct dt_dev *dev, u64 clock_id,
+ u32 idx)
+{
+ int ret;
+
+ dev->dpll = dpll_device_get(clock_id, idx, THIS_MODULE, &dev->tracker);
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dev->dpll);
+ ret = dpll_device_register(dev->dpll, DPLL_TYPE_EEC, &dt_dev_ops, dev);
+ KUNIT_ASSERT_EQ(test, ret, 0);
+ dev->registered = true;
+}
+
+static void dt_dev_fini(struct dt_dev *dev)
+{
+ if (IS_ERR_OR_NULL(dev->dpll))
+ return;
+ if (dev->registered)
+ dpll_device_unregister(dev->dpll, &dt_dev_ops, dev);
+ dpll_device_put(dev->dpll, &dev->tracker);
+}
+
+static void dt_pin_get(struct kunit *test, struct dt_pin *pin, u64 clock_id,
+ u32 idx)
+{
+ pin->pin = dpll_pin_get(clock_id, idx, THIS_MODULE, &dt_pin_prop,
+ &pin->tracker);
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, pin->pin);
+}
+
+static void dt_pin_put(struct dt_pin *pin)
+{
+ if (!IS_ERR_OR_NULL(pin->pin))
+ dpll_pin_put(pin->pin, &pin->tracker);
+}
+
+static void dt_reg(struct kunit *test, struct dt_dev *dev, struct dt_pin *pin,
+ bool *registered)
+{
+ int ret;
+
+ ret = dpll_pin_register(dev->dpll, pin->pin, &dt_pin_ops, pin);
+ KUNIT_ASSERT_EQ(test, ret, 0);
+ *registered = true;
+}
+
+static void dt_unreg(struct dt_dev *dev, struct dt_pin *pin, bool *registered)
+{
+ if (!*registered)
+ return;
+ dpll_pin_unregister(dev->dpll, pin->pin, &dt_pin_ops, pin);
+ *registered = false;
+}
+
+/* Fill @ids with the pins reported as linked with @pin, return their count */
+static int dt_links(struct kunit *test, struct dt_pin *pin, u32 *ids)
+{
+ struct sk_buff *msg;
+ struct nlattr *attr;
+ int n = 0, rem, ret;
+
+ msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
+ KUNIT_ASSERT_NOT_NULL(test, msg);
+ mutex_lock(&dpll_lock);
+ ret = dpll_cmd_pin_get_one(msg, pin->pin, NULL);
+ mutex_unlock(&dpll_lock);
+ if (!ret)
+ nla_for_each_attr_type(attr, DPLL_A_PIN_LINK,
+ (struct nlattr *)msg->data, msg->len,
+ rem) {
+ struct nlattr *id = nla_find_nested(attr,
+ DPLL_A_PIN_ID);
+
+ if (id && n < DT_MAX_LINKS)
+ ids[n] = nla_get_u32(id);
+ n++;
+ }
+ nlmsg_free(msg);
+ KUNIT_ASSERT_EQ(test, ret, 0);
+ KUNIT_ASSERT_LE(test, n, DT_MAX_LINKS);
+
+ return n;
+}
+
+/* Check that @pin is reported as linked with @peer only */
+static void dt_expect_link(struct kunit *test, struct dt_pin *pin,
+ struct dt_pin *peer)
+{
+ u32 ids[DT_MAX_LINKS];
+
+ KUNIT_ASSERT_EQ(test, dt_links(test, pin, ids), 1);
+ KUNIT_EXPECT_EQ(test, ids[0], peer->pin->id);
+}
+
+static void dt_expect_no_link(struct kunit *test, struct dt_pin *pin)
+{
+ u32 ids[DT_MAX_LINKS];
+
+ KUNIT_EXPECT_EQ(test, dt_links(test, pin, ids), 0);
+}
+
+static int dt_init(struct kunit *test)
+{
+ struct dt_ctx *ctx;
+ int ret;
+
+ ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
+ KUNIT_ASSERT_NOT_NULL(test, ctx);
+ test->priv = ctx;
+
+ ctx->fwnode = kunit_fwnode_create_software_node(test, NULL, NULL);
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ctx->fwnode);
+
+ 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);
+ 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);
+
+ ctx->nb.notifier_call = dt_notify;
+ ret = register_dpll_notifier(&ctx->nb);
+ KUNIT_ASSERT_EQ(test, ret, 0);
+ ctx->nb_registered = true;
+
+ return 0;
+}
+
+static void dt_exit(struct kunit *test)
+{
+ struct dt_ctx *ctx = test->priv;
+
+ if (!ctx)
+ return;
+ if (ctx->nb_registered)
+ unregister_dpll_notifier(&ctx->nb);
+ 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->u);
+ dt_pin_put(&ctx->p);
+ dt_dev_fini(&ctx->b0);
+ dt_dev_fini(&ctx->a1);
+ dt_dev_fini(&ctx->a0);
+}
+
+/* The pin with the link is registered before the pin it names */
+static void dt_test_link_first(struct kunit *test)
+{
+ struct dt_ctx *ctx = test->priv;
+
+ dt_reg(test, &ctx->b0, &ctx->c, &ctx->c_b0);
+ dt_expect_no_link(test, &ctx->c);
+
+ dt_reg(test, &ctx->a0, &ctx->p, &ctx->p_a0);
+ KUNIT_EXPECT_EQ(test, ctx->c.changes, 1);
+ dt_expect_link(test, &ctx->c, &ctx->p);
+ dt_expect_link(test, &ctx->p, &ctx->c);
+}
+
+/* The pin named by the link is registered first */
+static void dt_test_link_last(struct kunit *test)
+{
+ struct dt_ctx *ctx = test->priv;
+
+ dt_reg(test, &ctx->a0, &ctx->p, &ctx->p_a0);
+ dt_expect_no_link(test, &ctx->p);
+
+ dt_reg(test, &ctx->b0, &ctx->c, &ctx->c_b0);
+ KUNIT_EXPECT_EQ(test, ctx->p.changes, 1);
+ dt_expect_link(test, &ctx->p, &ctx->c);
+ dt_expect_link(test, &ctx->c, &ctx->p);
+}
+
+/* The pin named by the link goes away and comes back */
+static void dt_test_peer_reregister(struct kunit *test)
+{
+ struct dt_ctx *ctx = test->priv;
+
+ dt_reg(test, &ctx->b0, &ctx->c, &ctx->c_b0);
+ dt_reg(test, &ctx->a0, &ctx->p, &ctx->p_a0);
+ KUNIT_EXPECT_EQ(test, ctx->c.changes, 1);
+
+ dt_unreg(&ctx->a0, &ctx->p, &ctx->p_a0);
+ KUNIT_EXPECT_EQ(test, ctx->c.changes, 2);
+ dt_expect_no_link(test, &ctx->c);
+
+ dt_reg(test, &ctx->a0, &ctx->p, &ctx->p_a0);
+ KUNIT_EXPECT_EQ(test, ctx->c.changes, 3);
+ dt_expect_link(test, &ctx->c, &ctx->p);
+}
+
+/* The link stays with its pin when the pin is unregistered */
+static void dt_test_link_reregister(struct kunit *test)
+{
+ struct dt_ctx *ctx = test->priv;
+
+ dt_reg(test, &ctx->a0, &ctx->p, &ctx->p_a0);
+ dt_reg(test, &ctx->b0, &ctx->c, &ctx->c_b0);
+ KUNIT_EXPECT_EQ(test, ctx->p.changes, 1);
+
+ dt_unreg(&ctx->b0, &ctx->c, &ctx->c_b0);
+ KUNIT_EXPECT_EQ(test, ctx->p.changes, 2);
+ dt_expect_no_link(test, &ctx->p);
+
+ dt_reg(test, &ctx->b0, &ctx->c, &ctx->c_b0);
+ KUNIT_EXPECT_EQ(test, ctx->p.changes, 3);
+ dt_expect_link(test, &ctx->p, &ctx->c);
+}
+
+/* Only the first registration and the last unregistration change the link */
+static void dt_test_peer_two_dplls(struct kunit *test)
+{
+ struct dt_ctx *ctx = test->priv;
+
+ dt_reg(test, &ctx->b0, &ctx->c, &ctx->c_b0);
+ dt_reg(test, &ctx->a0, &ctx->p, &ctx->p_a0);
+ dt_reg(test, &ctx->a1, &ctx->p, &ctx->p_a1);
+ KUNIT_EXPECT_EQ(test, ctx->c.changes, 1);
+
+ dt_unreg(&ctx->a0, &ctx->p, &ctx->p_a0);
+ KUNIT_EXPECT_EQ(test, ctx->c.changes, 1);
+ dt_expect_link(test, &ctx->c, &ctx->p);
+
+ dt_unreg(&ctx->a1, &ctx->p, &ctx->p_a1);
+ KUNIT_EXPECT_EQ(test, ctx->c.changes, 2);
+ dt_expect_no_link(test, &ctx->c);
+}
+
+/* A pin without a link and a firmware node is not linked or notified */
+static void dt_test_unrelated(struct kunit *test)
+{
+ struct dt_ctx *ctx = test->priv;
+
+ dt_reg(test, &ctx->a0, &ctx->u, &ctx->u_a0);
+ dt_reg(test, &ctx->b0, &ctx->c, &ctx->c_b0);
+ dt_reg(test, &ctx->a0, &ctx->p, &ctx->p_a0);
+ dt_unreg(&ctx->a0, &ctx->p, &ctx->p_a0);
+
+ KUNIT_EXPECT_EQ(test, ctx->u.changes, 0);
+ dt_expect_no_link(test, &ctx->u);
+ dt_expect_no_link(test, &ctx->c);
+}
+
+/* A duplicate link is rejected, also on a registered pin */
+static void dt_test_add_duplicate(struct kunit *test)
+{
+ struct dt_ctx *ctx = test->priv;
+ int ret;
+
+ ret = dpll_pin_link_add(ctx->c.pin, ctx->fwnode);
+ KUNIT_EXPECT_EQ(test, ret, -EEXIST);
+ dt_reg(test, &ctx->b0, &ctx->c, &ctx->c_b0);
+
+ kunit_warning_suppress(test) {
+ ret = dpll_pin_link_add(ctx->c.pin, ctx->fwnode);
+ KUNIT_EXPECT_EQ(test, ret, -EEXIST);
+ KUNIT_EXPECT_SUPPRESSED_WARNING_COUNT(test, 0);
+ }
+}
+
+static void dt_test_add_registered(struct kunit *test)
+{
+ struct dt_ctx *ctx = test->priv;
+ struct fwnode_handle *other;
+
+ other = kunit_fwnode_create_software_node(test, NULL, NULL);
+ KUNIT_ASSERT_NOT_ERR_OR_NULL(test, other);
+ dt_reg(test, &ctx->b0, &ctx->c, &ctx->c_b0);
+
+ kunit_warning_suppress(test) {
+ KUNIT_EXPECT_EQ(test, dpll_pin_link_add(ctx->c.pin, other),
+ -EBUSY);
+ KUNIT_EXPECT_SUPPRESSED_WARNING_COUNT(test, 1);
+ }
+}
+
+static struct kunit_case dt_cases[] = {
+ KUNIT_CASE(dt_test_link_first),
+ KUNIT_CASE(dt_test_link_last),
+ KUNIT_CASE(dt_test_peer_reregister),
+ KUNIT_CASE(dt_test_link_reregister),
+ KUNIT_CASE(dt_test_peer_two_dplls),
+ KUNIT_CASE(dt_test_unrelated),
+ KUNIT_CASE(dt_test_add_duplicate),
+ KUNIT_CASE(dt_test_add_registered),
+ {}
+};
+
+static struct kunit_suite dt_suite = {
+ .name = "dpll_pin_link",
+ .init = dt_init,
+ .exit = dt_exit,
+ .test_cases = dt_cases,
+};
+
+kunit_test_suite(dt_suite);
+
+MODULE_DESCRIPTION("KUnit tests for links between DPLL pins");
+MODULE_LICENSE("GPL");