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()
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.
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
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.
19 Also add a few tests to make sure this case is covered.
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>
26 drivers/clk/clk.c | 45 +++++++++++------
27 drivers/clk/clk_test.c | 108 +++++++++++++++++++++++++++++++++++++++++
28 2 files changed, 139 insertions(+), 14 deletions(-)
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
34 EXPORT_SYMBOL_GPL(clk_set_rate_exclusive);
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
42 - * Returns success (0) or negative errno.
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,
50 unsigned long old_min, old_max, rate;
52 + lockdep_assert_held(&prepare_lock);
57 @@ -2356,8 +2352,6 @@ int clk_set_rate_range(struct clk *clk,
63 if (clk->exclusive_count)
64 clk_core_rate_unprotect(clk->core);
66 @@ -2401,6 +2395,28 @@ out:
67 if (clk->exclusive_count)
68 clk_core_rate_protect(clk->core);
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
79 + * Return: 0 for success or negative errno on failure.
81 +int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max)
90 + ret = clk_set_rate_range_nolock(clk, min, max);
95 @@ -4360,9 +4376,10 @@ void __clk_put(struct clk *clk)
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);
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);
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_
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.
120 + * With clk_dummy_maximize_rate_ops, this means that the rate will
121 + * trail along the maximum as it evolves.
123 +static void clk_range_test_multiple_set_range_rate_put_maximized(struct kunit *test)
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;
131 + user1 = clk_hw_get_clk(hw, NULL);
132 + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user1);
134 + user2 = clk_hw_get_clk(hw, NULL);
135 + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user2);
137 + KUNIT_ASSERT_EQ(test,
138 + clk_set_rate(clk, DUMMY_CLOCK_RATE_2 + 1000),
141 + KUNIT_ASSERT_EQ(test,
142 + clk_set_rate_range(user1,
144 + DUMMY_CLOCK_RATE_2),
147 + rate = clk_get_rate(clk);
148 + KUNIT_ASSERT_GT(test, rate, 0);
149 + KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
151 + KUNIT_ASSERT_EQ(test,
152 + clk_set_rate_range(user2,
154 + DUMMY_CLOCK_RATE_1),
157 + rate = clk_get_rate(clk);
158 + KUNIT_ASSERT_GT(test, rate, 0);
159 + KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
163 + rate = clk_get_rate(clk);
164 + KUNIT_ASSERT_GT(test, rate, 0);
165 + KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
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),
177 @@ -877,9 +933,61 @@ static void clk_range_test_multiple_set_
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.
186 + * With clk_dummy_minimize_rate_ops, this means that the rate will
187 + * trail along the minimum as it evolves.
189 +static void clk_range_test_multiple_set_range_rate_put_minimized(struct kunit *test)
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;
197 + user1 = clk_hw_get_clk(hw, NULL);
198 + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user1);
200 + user2 = clk_hw_get_clk(hw, NULL);
201 + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, user2);
203 + KUNIT_ASSERT_EQ(test,
204 + clk_set_rate_range(user1,
205 + DUMMY_CLOCK_RATE_1,
209 + rate = clk_get_rate(clk);
210 + KUNIT_ASSERT_GT(test, rate, 0);
211 + KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
213 + KUNIT_ASSERT_EQ(test,
214 + clk_set_rate_range(user2,
215 + DUMMY_CLOCK_RATE_2,
219 + rate = clk_get_rate(clk);
220 + KUNIT_ASSERT_GT(test, rate, 0);
221 + KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_2);
225 + rate = clk_get_rate(clk);
226 + KUNIT_ASSERT_GT(test, rate, 0);
227 + KUNIT_EXPECT_EQ(test, rate, DUMMY_CLOCK_RATE_1);
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),