bcm27xx: add support for linux v5.15
[openwrt/staging/wigyori.git] / target / linux / bcm27xx / patches-5.15 / 950-0836-clk-test-Test-clk_set_rate_range-on-orphan-mux.patch
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
5
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.
8
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.
14
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.
18
19 Signed-off-by: Maxime Ripard <maxime@cerno.tech>
20 ---
21 drivers/clk/clk_test.c | 105 +++++++++++++++++++++++++++++++++++++++++
22 1 file changed, 105 insertions(+)
23
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
27 return 0;
28 }
29
30 +static int clk_dummy_single_set_parent(struct clk_hw *hw, u8 index)
31 +{
32 + if (index >= clk_hw_get_num_parents(hw))
33 + return -EINVAL;
34 +
35 + return 0;
36 +}
37 +
38 +static u8 clk_dummy_single_get_parent(struct clk_hw *hw)
39 +{
40 + return 0;
41 +}
42 +
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,
48 };
49
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,
53 +};
54 +
55 static int clk_test_init_with_ops(struct kunit *test, const struct clk_ops *ops)
56 {
57 struct clk_dummy_context *ctx;
58 @@ -239,6 +257,92 @@ static struct kunit_suite clk_test_suite
59 .test_cases = clk_test_cases,
60 };
61
62 +struct clk_single_parent_ctx {
63 + struct clk_dummy_context parent_ctx;
64 + struct clk_hw hw;
65 +};
66 +
67 +static int clk_orphan_transparent_single_parent_mux_test_init(struct kunit *test)
68 +{
69 + struct clk_single_parent_ctx *ctx;
70 + struct clk_init_data init = { };
71 + const char *parents[] = { "orphan_parent" };
72 + int ret;
73 +
74 + ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
75 + if (!ctx)
76 + return -ENOMEM;
77 + test->priv = ctx;
78 +
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;
85 +
86 + ret = clk_hw_register(NULL, &ctx->hw);
87 + if (ret)
88 + return ret;
89 +
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;
95 +
96 + ret = clk_hw_register(NULL, &ctx->parent_ctx.hw);
97 + if (ret)
98 + return ret;
99 +
100 + return 0;
101 +}
102 +
103 +static void clk_orphan_transparent_single_parent_mux_test_exit(struct kunit *test)
104 +{
105 + struct clk_single_parent_ctx *ctx = test->priv;
106 +
107 + clk_hw_unregister(&ctx->hw);
108 + clk_hw_unregister(&ctx->parent_ctx.hw);
109 +}
110 +
111 +/*
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.
114 + */
115 +static void clk_test_orphan_transparent_parent_mux_set_range(struct kunit *test)
116 +{
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;
121 +
122 + rate = clk_get_rate(clk);
123 + KUNIT_ASSERT_GT(test, rate, 0);
124 +
125 + KUNIT_ASSERT_EQ(test,
126 + clk_set_rate_range(clk,
127 + ctx->parent_ctx.rate - 1000,
128 + ctx->parent_ctx.rate + 1000),
129 + 0);
130 +
131 + new_rate = clk_get_rate(clk);
132 + KUNIT_ASSERT_GT(test, new_rate, 0);
133 + KUNIT_EXPECT_EQ(test, rate, new_rate);
134 +}
135 +
136 +static struct kunit_case clk_orphan_transparent_single_parent_mux_test_cases[] = {
137 + KUNIT_CASE(clk_test_orphan_transparent_parent_mux_set_range),
138 + {}
139 +};
140 +
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,
146 +};
147 +
148 /*
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
152
153 kunit_test_suites(
154 &clk_test_suite,
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