Skip to content

Commit 481f541

Browse files
mripardbebarino
authored andcommitted
clk: test: Test clk_set_rate_range on orphan mux
A bug recently affected the Tegra30 where calling clk_set_rate_range() on a clock would make it change its rate to the minimum. This was due to the clock in question being a mux that was orphan at registration, which lead to the clk_core req_rate being 0, and the clk_set_rate_range() function then calling clk_set_rate() with req_rate, effectively making that clock running at the minimum rate allowed, even though the initial rate was within that range. Make a test suite to create a mux initially orphan, and then make sure that if our clock rate was initially within a given range, then enforcing that range won't affect it. Signed-off-by: Maxime Ripard <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Stephen Boyd <[email protected]>
1 parent 5f7e2af commit 481f541

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed

drivers/clk/clk_test.c

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,19 @@ static int clk_dummy_set_rate(struct clk_hw *hw,
7272
return 0;
7373
}
7474

75+
static int clk_dummy_single_set_parent(struct clk_hw *hw, u8 index)
76+
{
77+
if (index >= clk_hw_get_num_parents(hw))
78+
return -EINVAL;
79+
80+
return 0;
81+
}
82+
83+
static u8 clk_dummy_single_get_parent(struct clk_hw *hw)
84+
{
85+
return 0;
86+
}
87+
7588
static const struct clk_ops clk_dummy_rate_ops = {
7689
.recalc_rate = clk_dummy_recalc_rate,
7790
.determine_rate = clk_dummy_determine_rate,
@@ -90,6 +103,11 @@ static const struct clk_ops clk_dummy_minimize_rate_ops = {
90103
.set_rate = clk_dummy_set_rate,
91104
};
92105

106+
static const struct clk_ops clk_dummy_single_parent_ops = {
107+
.set_parent = clk_dummy_single_set_parent,
108+
.get_parent = clk_dummy_single_get_parent,
109+
};
110+
93111
static int clk_test_init_with_ops(struct kunit *test, const struct clk_ops *ops)
94112
{
95113
struct clk_dummy_context *ctx;
@@ -239,6 +257,92 @@ static struct kunit_suite clk_test_suite = {
239257
.test_cases = clk_test_cases,
240258
};
241259

260+
struct clk_single_parent_ctx {
261+
struct clk_dummy_context parent_ctx;
262+
struct clk_hw hw;
263+
};
264+
265+
static int clk_orphan_transparent_single_parent_mux_test_init(struct kunit *test)
266+
{
267+
struct clk_single_parent_ctx *ctx;
268+
struct clk_init_data init = { };
269+
const char * const parents[] = { "orphan_parent" };
270+
int ret;
271+
272+
ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
273+
if (!ctx)
274+
return -ENOMEM;
275+
test->priv = ctx;
276+
277+
init.name = "test_orphan_dummy_parent";
278+
init.ops = &clk_dummy_single_parent_ops;
279+
init.parent_names = parents;
280+
init.num_parents = ARRAY_SIZE(parents);
281+
init.flags = CLK_SET_RATE_PARENT;
282+
ctx->hw.init = &init;
283+
284+
ret = clk_hw_register(NULL, &ctx->hw);
285+
if (ret)
286+
return ret;
287+
288+
memset(&init, 0, sizeof(init));
289+
init.name = "orphan_parent";
290+
init.ops = &clk_dummy_rate_ops;
291+
ctx->parent_ctx.hw.init = &init;
292+
ctx->parent_ctx.rate = DUMMY_CLOCK_INIT_RATE;
293+
294+
ret = clk_hw_register(NULL, &ctx->parent_ctx.hw);
295+
if (ret)
296+
return ret;
297+
298+
return 0;
299+
}
300+
301+
static void clk_orphan_transparent_single_parent_mux_test_exit(struct kunit *test)
302+
{
303+
struct clk_single_parent_ctx *ctx = test->priv;
304+
305+
clk_hw_unregister(&ctx->hw);
306+
clk_hw_unregister(&ctx->parent_ctx.hw);
307+
}
308+
309+
/*
310+
* Test that a mux-only clock, with an initial rate within a range,
311+
* will still have the same rate after the range has been enforced.
312+
*/
313+
static void clk_test_orphan_transparent_parent_mux_set_range(struct kunit *test)
314+
{
315+
struct clk_single_parent_ctx *ctx = test->priv;
316+
struct clk_hw *hw = &ctx->hw;
317+
struct clk *clk = hw->clk;
318+
unsigned long rate, new_rate;
319+
320+
rate = clk_get_rate(clk);
321+
KUNIT_ASSERT_GT(test, rate, 0);
322+
323+
KUNIT_ASSERT_EQ(test,
324+
clk_set_rate_range(clk,
325+
ctx->parent_ctx.rate - 1000,
326+
ctx->parent_ctx.rate + 1000),
327+
0);
328+
329+
new_rate = clk_get_rate(clk);
330+
KUNIT_ASSERT_GT(test, new_rate, 0);
331+
KUNIT_EXPECT_EQ(test, rate, new_rate);
332+
}
333+
334+
static struct kunit_case clk_orphan_transparent_single_parent_mux_test_cases[] = {
335+
KUNIT_CASE(clk_test_orphan_transparent_parent_mux_set_range),
336+
{}
337+
};
338+
339+
static struct kunit_suite clk_orphan_transparent_single_parent_test_suite = {
340+
.name = "clk-orphan-transparent-single-parent-test",
341+
.init = clk_orphan_transparent_single_parent_mux_test_init,
342+
.exit = clk_orphan_transparent_single_parent_mux_test_exit,
343+
.test_cases = clk_orphan_transparent_single_parent_mux_test_cases,
344+
};
345+
242346
/*
243347
* Test that clk_set_rate_range won't return an error for a valid range
244348
* and that it will make sure the rate of the clock is within the
@@ -788,6 +892,7 @@ static struct kunit_suite clk_range_minimize_test_suite = {
788892

789893
kunit_test_suites(
790894
&clk_test_suite,
895+
&clk_orphan_transparent_single_parent_test_suite,
791896
&clk_range_test_suite,
792897
&clk_range_maximize_test_suite,
793898
&clk_range_minimize_test_suite

0 commit comments

Comments
 (0)