bcm27xx: add support for linux v5.15
[openwrt/staging/chunkeey.git] / target / linux / bcm27xx / patches-5.15 / 950-0837-clk-Drop-the-rate-range-on-clk_put.patch
1 From 06f02ffffc5aa432e119d094f019ba97ccd8bb89 Mon Sep 17 00:00:00 2001
2 From: Maxime Ripard <maxime@cerno.tech>
3 Date: Fri, 25 Mar 2022 17:11:44 +0100
4 Subject: [PATCH] clk: Drop the rate range on clk_put()
5
6 When clk_put() is called we don't make another clk_set_rate() call to
7 re-evaluate the rate boundaries. This is unlike clk_set_rate_range()
8 that evaluates the rate again each time it is called.
9
10 However, clk_put() is essentially equivalent to clk_set_rate_range()
11 since after clk_put() completes the consumer's boundaries shouldn't be
12 enforced anymore.
13
14 Let's add a call to clk_set_rate_range() in clk_put() to make sure those
15 rate boundaries are dropped and the clock provider drivers can react. In
16 order to be as non-intrusive as possible, we'll just make that call if
17 the clock had non-default boundaries.
18
19 Also add a few tests to make sure this case is covered.
20
21 Fixes: c80ac50cbb37 ("clk: Always set the rate on clk_set_range_rate")
22 Tested-by: Alexander Stein <alexander.stein@ew.tq-group.com> # imx8mp
23 Tested-by: Marek Szyprowski <m.szyprowski@samsung.com> # exynos4210, meson g12b
24 Signed-off-by: Maxime Ripard <maxime@cerno.tech>
25 ---
26 drivers/clk/clk.c | 45 +++++++++++------
27 drivers/clk/clk_test.c | 108 +++++++++++++++++++++++++++++++++++++++++
28 2 files changed, 139 insertions(+), 14 deletions(-)
29
30 --- a/drivers/clk/clk.c
31 +++ b/drivers/clk/clk.c
32 @@ -2331,19 +2331,15 @@ int clk_set_rate_exclusive(struct clk *c
33 }
34 EXPORT_SYMBOL_GPL(clk_set_rate_exclusive);
35
36 -/**
37 - * clk_set_rate_range - set a rate range for a clock source
38 - * @clk: clock source
39 - * @min: desired minimum clock rate in Hz, inclusive
40 - * @max: desired maximum clock rate in Hz, inclusive
41 - *
42 - * Returns success (0) or negative errno.
43 - */
44 -int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max)
45 +static int clk_set_rate_range_nolock(struct clk *clk,
46 + unsigned long min,
47 + unsigned long max)
48 {
49 int ret = 0;
50 unsigned long old_min, old_max, rate;
51
52 + lockdep_assert_held(&prepare_lock);
53 +
54 if (!clk)
55 return 0;
56
57 @@ -2356,8 +2352,6 @@ int clk_set_rate_range(struct clk *clk,
58 return -EINVAL;
59 }
60
61 - clk_prepare_lock();
62 -
63 if (clk->exclusive_count)
64 clk_core_rate_unprotect(clk->core);
65
66 @@ -2401,6 +2395,28 @@ out:
67 if (clk->exclusive_count)
68 clk_core_rate_protect(clk->core);
69
70 + return ret;
71 +}
72 +
73 +/**
74 + * clk_set_rate_range - set a rate range for a clock source
75 + * @clk: clock source
76 + * @min: desired minimum clock rate in Hz, inclusive
77 + * @max: desired maximum clock rate in Hz, inclusive
78 + *
79 + * Return: 0 for success or negative errno on failure.
80 + */
81 +int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max)
82 +{
83 + int ret;
84 +
85 + if (!clk)
86 + return 0;
87 +
88 + clk_prepare_lock();
89 +
90 + ret = clk_set_rate_range_nolock(clk, min, max);
91 +
92 clk_prepare_unlock();
93
94 return ret;
95 @@ -4360,9 +4376,10 @@ void __clk_put(struct clk *clk)
96 }
97
98 hlist_del(&clk->clks_node);
99 - if (clk->min_rate > clk->core->req_rate ||
100 - clk->max_rate < clk->core->req_rate)
101 - clk_core_set_rate_nolock(clk->core, clk->core->req_rate);
102 +
103 + /* If we had any boundaries on that clock, let's drop them. */
104 + if (clk->min_rate > 0 || clk->max_rate < ULONG_MAX)
105 + clk_set_rate_range_nolock(clk, 0, ULONG_MAX);
106
107 owner = clk->core->owner;
108 kref_put(&clk->core->ref, __clk_release);
109 --- a/drivers/clk/clk_test.c
110 +++ b/drivers/clk/clk_test.c
111 @@ -760,9 +760,65 @@ static void clk_range_test_multiple_set_
112 clk_put(user1);
113 }
114
115 +/*
116 + * Test that if we have several subsequent calls to
117 + * clk_set_rate_range(), across multiple users, the core will reevaluate
118 + * whether a new rate is needed, including when a user drop its clock.
119 + *
120 + * With clk_dummy_maximize_rate_ops, this means that the rate will
121 + * trail along the maximum as it evolves.
122 + */
123 +static void clk_range_test_multiple_set_range_rate_put_maximized(struct kunit *test)
124 +{
125 + struct clk_dummy_context *ctx = test->priv;
126 + struct clk_hw *hw = &ctx->hw;
127 + struct clk *clk = hw->clk;
128 + struct clk *user1, *user2;
129 + unsigned long rate;
130 +
131 + user1 = clk_hw_get_clk(hw, NULL);
132 + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user1);
133 +
134 + user2 = clk_hw_get_clk(hw, NULL);
135 + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user2);
136 +
137 + KUNIT_ASSERT_EQ(test,
138 + clk_set_rate(clk, DUMMY_CLOCK_RATE_2 + 1000),
139 + 0);
140 +
141 + KUNIT_ASSERT_EQ(test,
142 + clk_set_rate_range(user1,
143 + 0,
144 + DUMMY_CLOCK_RATE_2),
145 + 0);
146 +
147 + rate = clk_get_rate(clk);
148 + KUNIT_ASSERT_GT(test, rate, 0);
149 + KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
150 +
151 + KUNIT_ASSERT_EQ(test,
152 + clk_set_rate_range(user2,
153 + 0,
154 + DUMMY_CLOCK_RATE_1),
155 + 0);
156 +
157 + rate = clk_get_rate(clk);
158 + KUNIT_ASSERT_GT(test, rate, 0);
159 + KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
160 +
161 + clk_put(user2);
162 +
163 + rate = clk_get_rate(clk);
164 + KUNIT_ASSERT_GT(test, rate, 0);
165 + KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
166 +
167 + clk_put(user1);
168 +}
169 +
170 static struct kunit_case clk_range_maximize_test_cases[] = {
171 KUNIT_CASE(clk_range_test_set_range_rate_maximized),
172 KUNIT_CASE(clk_range_test_multiple_set_range_rate_maximized),
173 + KUNIT_CASE(clk_range_test_multiple_set_range_rate_put_maximized),
174 {}
175 };
176
177 @@ -877,9 +933,61 @@ static void clk_range_test_multiple_set_
178 clk_put(user1);
179 }
180
181 +/*
182 + * Test that if we have several subsequent calls to
183 + * clk_set_rate_range(), across multiple users, the core will reevaluate
184 + * whether a new rate is needed, including when a user drop its clock.
185 + *
186 + * With clk_dummy_minimize_rate_ops, this means that the rate will
187 + * trail along the minimum as it evolves.
188 + */
189 +static void clk_range_test_multiple_set_range_rate_put_minimized(struct kunit *test)
190 +{
191 + struct clk_dummy_context *ctx = test->priv;
192 + struct clk_hw *hw = &ctx->hw;
193 + struct clk *clk = hw->clk;
194 + struct clk *user1, *user2;
195 + unsigned long rate;
196 +
197 + user1 = clk_hw_get_clk(hw, NULL);
198 + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user1);
199 +
200 + user2 = clk_hw_get_clk(hw, NULL);
201 + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user2);
202 +
203 + KUNIT_ASSERT_EQ(test,
204 + clk_set_rate_range(user1,
205 + DUMMY_CLOCK_RATE_1,
206 + ULONG_MAX),
207 + 0);
208 +
209 + rate = clk_get_rate(clk);
210 + KUNIT_ASSERT_GT(test, rate, 0);
211 + KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
212 +
213 + KUNIT_ASSERT_EQ(test,
214 + clk_set_rate_range(user2,
215 + DUMMY_CLOCK_RATE_2,
216 + ULONG_MAX),
217 + 0);
218 +
219 + rate = clk_get_rate(clk);
220 + KUNIT_ASSERT_GT(test, rate, 0);
221 + KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
222 +
223 + clk_put(user2);
224 +
225 + rate = clk_get_rate(clk);
226 + KUNIT_ASSERT_GT(test, rate, 0);
227 + KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
228 +
229 + clk_put(user1);
230 +}
231 +
232 static struct kunit_case clk_range_minimize_test_cases[] = {
233 KUNIT_CASE(clk_range_test_set_range_rate_minimized),
234 KUNIT_CASE(clk_range_test_multiple_set_range_rate_minimized),
235 + KUNIT_CASE(clk_range_test_multiple_set_range_rate_put_minimized),
236 {}
237 };
238