1 From e22b0f50781bc8507c1e3b8f775ac69d406bd155 Mon Sep 17 00:00:00 2001
2 From: Maxime Ripard <maxime@cerno.tech>
3 Date: Fri, 25 Mar 2022 11:27:28 +0100
4 Subject: [PATCH] clk: test: Test clk_set_rate_range on orphan mux
6 A bug recently affected the Tegra30 where calling clk_set_rate_range()
7 on a clock would make it change its rate to the minimum.
9 This was due to the clock in question being a mux that was orphan at
10 registration, which lead to the clk_core req_rate being 0, and the
11 clk_set_rate_range() function then calling clk_set_rate() with req_rate,
12 effectively making that clock running at the minimum rate allowed, even
13 though the initial rate was within that range.
15 Make a test suite to create a mux initially orphan, and then make sure
16 that if our clock rate was initially within a given range, then
17 enforcing that range won't affect it.
19 Signed-off-by: Maxime Ripard <maxime@cerno.tech>
21 drivers/clk/clk_test.c | 105 +++++++++++++++++++++++++++++++++++++++++
22 1 file changed, 105 insertions(+)
24 --- a/drivers/clk/clk_test.c
25 +++ b/drivers/clk/clk_test.c
26 @@ -72,6 +72,19 @@ static int clk_dummy_set_rate(struct clk
30 +static int clk_dummy_single_set_parent(struct clk_hw *hw, u8 index)
32 + if (index >= clk_hw_get_num_parents(hw))
38 +static u8 clk_dummy_single_get_parent(struct clk_hw *hw)
43 static const struct clk_ops clk_dummy_rate_ops = {
44 .recalc_rate = clk_dummy_recalc_rate,
45 .determine_rate = clk_dummy_determine_rate,
46 @@ -90,6 +103,11 @@ static const struct clk_ops clk_dummy_mi
47 .set_rate = clk_dummy_set_rate,
50 +static const struct clk_ops clk_dummy_single_parent_ops = {
51 + .set_parent = clk_dummy_single_set_parent,
52 + .get_parent = clk_dummy_single_get_parent,
55 static int clk_test_init_with_ops(struct kunit *test, const struct clk_ops *ops)
57 struct clk_dummy_context *ctx;
58 @@ -239,6 +257,92 @@ static struct kunit_suite clk_test_suite
59 .test_cases = clk_test_cases,
62 +struct clk_single_parent_ctx {
63 + struct clk_dummy_context parent_ctx;
67 +static int clk_orphan_transparent_single_parent_mux_test_init(struct kunit *test)
69 + struct clk_single_parent_ctx *ctx;
70 + struct clk_init_data init = { };
71 + const char *parents[] = { "orphan_parent" };
74 + ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
79 + init.name = "test_orphan_dummy_parent";
80 + init.ops = &clk_dummy_single_parent_ops;
81 + init.parent_names = parents;
82 + init.num_parents = ARRAY_SIZE(parents);
83 + init.flags = CLK_SET_RATE_PARENT;
84 + ctx->hw.init = &init;
86 + ret = clk_hw_register(NULL, &ctx->hw);
90 + memset(&init, 0, sizeof(init));
91 + init.name = "orphan_parent";
92 + init.ops = &clk_dummy_rate_ops;
93 + ctx->parent_ctx.hw.init = &init;
94 + ctx->parent_ctx.rate = DUMMY_CLOCK_INIT_RATE;
96 + ret = clk_hw_register(NULL, &ctx->parent_ctx.hw);
103 +static void clk_orphan_transparent_single_parent_mux_test_exit(struct kunit *test)
105 + struct clk_single_parent_ctx *ctx = test->priv;
107 + clk_hw_unregister(&ctx->hw);
108 + clk_hw_unregister(&ctx->parent_ctx.hw);
112 + * Test that a mux-only clock, with an initial rate within a range,
113 + * will still have the same rate after the range has been enforced.
115 +static void clk_test_orphan_transparent_parent_mux_set_range(struct kunit *test)
117 + struct clk_single_parent_ctx *ctx = test->priv;
118 + struct clk_hw *hw = &ctx->hw;
119 + struct clk *clk = hw->clk;
120 + unsigned long rate, new_rate;
122 + rate = clk_get_rate(clk);
123 + KUNIT_ASSERT_GT(test, rate, 0);
125 + KUNIT_ASSERT_EQ(test,
126 + clk_set_rate_range(clk,
127 + ctx->parent_ctx.rate - 1000,
128 + ctx->parent_ctx.rate + 1000),
131 + new_rate = clk_get_rate(clk);
132 + KUNIT_ASSERT_GT(test, new_rate, 0);
133 + KUNIT_EXPECT_EQ(test, rate, new_rate);
136 +static struct kunit_case clk_orphan_transparent_single_parent_mux_test_cases[] = {
137 + KUNIT_CASE(clk_test_orphan_transparent_parent_mux_set_range),
141 +static struct kunit_suite clk_orphan_transparent_single_parent_test_suite = {
142 + .name = "clk-orphan-transparent-single-parent-test",
143 + .init = clk_orphan_transparent_single_parent_mux_test_init,
144 + .exit = clk_orphan_transparent_single_parent_mux_test_exit,
145 + .test_cases = clk_orphan_transparent_single_parent_mux_test_cases,
149 * Test that clk_set_rate_range won't return an error for a valid range
150 * and that it will make sure the rate of the clock is within the
151 @@ -788,6 +892,7 @@ static struct kunit_suite clk_range_mini
155 + &clk_orphan_transparent_single_parent_test_suite,
156 &clk_range_test_suite,
157 &clk_range_maximize_test_suite,
158 &clk_range_minimize_test_suite