// SPDX-License-Identifier: GPL-2.0 /* * Kunit tests for clk framework */ #include <linux/clk.h> #include <linux/clk-provider.h> /* Needed for clk_hw_get_clk() */ #include "clk.h" #include <kunit/test.h> static const struct clk_ops empty_clk_ops = …; #define DUMMY_CLOCK_INIT_RATE … #define DUMMY_CLOCK_RATE_1 … #define DUMMY_CLOCK_RATE_2 … struct clk_dummy_context { … }; static unsigned long clk_dummy_recalc_rate(struct clk_hw *hw, unsigned long parent_rate) { … } static int clk_dummy_determine_rate(struct clk_hw *hw, struct clk_rate_request *req) { … } static int clk_dummy_maximize_rate(struct clk_hw *hw, struct clk_rate_request *req) { … } static int clk_dummy_minimize_rate(struct clk_hw *hw, struct clk_rate_request *req) { … } static int clk_dummy_set_rate(struct clk_hw *hw, unsigned long rate, unsigned long parent_rate) { … } static int clk_dummy_single_set_parent(struct clk_hw *hw, u8 index) { … } static u8 clk_dummy_single_get_parent(struct clk_hw *hw) { … } static const struct clk_ops clk_dummy_rate_ops = …; static const struct clk_ops clk_dummy_maximize_rate_ops = …; static const struct clk_ops clk_dummy_minimize_rate_ops = …; static const struct clk_ops clk_dummy_single_parent_ops = …; struct clk_multiple_parent_ctx { … }; static int clk_multiple_parents_mux_set_parent(struct clk_hw *hw, u8 index) { … } static u8 clk_multiple_parents_mux_get_parent(struct clk_hw *hw) { … } static const struct clk_ops clk_multiple_parents_mux_ops = …; static const struct clk_ops clk_multiple_parents_no_reparent_mux_ops = …; static int clk_test_init_with_ops(struct kunit *test, const struct clk_ops *ops) { … } static int clk_test_init(struct kunit *test) { … } static int clk_maximize_test_init(struct kunit *test) { … } static int clk_minimize_test_init(struct kunit *test) { … } static void clk_test_exit(struct kunit *test) { … } /* * Test that the actual rate matches what is returned by clk_get_rate() */ static void clk_test_get_rate(struct kunit *test) { … } /* * Test that, after a call to clk_set_rate(), the rate returned by * clk_get_rate() matches. * * This assumes that clk_ops.determine_rate or clk_ops.round_rate won't * modify the requested rate, which is our case in clk_dummy_rate_ops. */ static void clk_test_set_get_rate(struct kunit *test) { … } /* * Test that, after several calls to clk_set_rate(), the rate returned * by clk_get_rate() matches the last one. * * This assumes that clk_ops.determine_rate or clk_ops.round_rate won't * modify the requested rate, which is our case in clk_dummy_rate_ops. */ static void clk_test_set_set_get_rate(struct kunit *test) { … } /* * Test that clk_round_rate and clk_set_rate are consitent and will * return the same frequency. */ static void clk_test_round_set_get_rate(struct kunit *test) { … } static struct kunit_case clk_test_cases[] = …; /* * Test suite for a basic rate clock, without any parent. * * These tests exercise the rate API with simple scenarios */ static struct kunit_suite clk_test_suite = …; static int clk_uncached_test_init(struct kunit *test) { … } /* * Test that for an uncached clock, the clock framework doesn't cache * the rate and clk_get_rate() will return the underlying clock rate * even if it changed. */ static void clk_test_uncached_get_rate(struct kunit *test) { … } /* * Test that for an uncached clock, clk_set_rate_range() will work * properly if the rate hasn't changed. */ static void clk_test_uncached_set_range(struct kunit *test) { … } /* * Test that for an uncached clock, clk_set_rate_range() will work * properly if the rate has changed in hardware. * * In this case, it means that if the rate wasn't initially in the range * we're trying to set, but got changed at some point into the range * without the kernel knowing about it, its rate shouldn't be affected. */ static void clk_test_uncached_updated_rate_set_range(struct kunit *test) { … } static struct kunit_case clk_uncached_test_cases[] = …; /* * Test suite for a basic, uncached, rate clock, without any parent. * * These tests exercise the rate API with simple scenarios */ static struct kunit_suite clk_uncached_test_suite = …; static int clk_multiple_parents_mux_test_init(struct kunit *test) { … } static void clk_multiple_parents_mux_test_exit(struct kunit *test) { … } /* * Test that for a clock with multiple parents, clk_get_parent() * actually returns the current one. */ static void clk_test_multiple_parents_mux_get_parent(struct kunit *test) { … } /* * Test that for a clock with a multiple parents, clk_has_parent() * actually reports all of them as parents. */ static void clk_test_multiple_parents_mux_has_parent(struct kunit *test) { … } /* * Test that for a clock with a multiple parents, if we set a range on * that clock and the parent is changed, its rate after the reparenting * is still within the range we asked for. * * FIXME: clk_set_parent() only does the reparenting but doesn't * reevaluate whether the new clock rate is within its boundaries or * not. */ static void clk_test_multiple_parents_mux_set_range_set_parent_get_rate(struct kunit *test) { … } static struct kunit_case clk_multiple_parents_mux_test_cases[] = …; /* * Test suite for a basic mux clock with two parents, with * CLK_SET_RATE_PARENT on the child. * * These tests exercise the consumer API and check that the state of the * child and parents are sane and consistent. */ static struct kunit_suite clk_multiple_parents_mux_test_suite = …; static int clk_orphan_transparent_multiple_parent_mux_test_init(struct kunit *test) { … } static void clk_orphan_transparent_multiple_parent_mux_test_exit(struct kunit *test) { … } /* * Test that, for a mux whose current parent hasn't been registered yet and is * thus orphan, clk_get_parent() will return NULL. */ static void clk_test_orphan_transparent_multiple_parent_mux_get_parent(struct kunit *test) { … } /* * Test that, for a mux whose current parent hasn't been registered yet, * calling clk_set_parent() to a valid parent will properly update the * mux parent and its orphan status. */ static void clk_test_orphan_transparent_multiple_parent_mux_set_parent(struct kunit *test) { … } /* * Test that, for a mux that started orphan but got switched to a valid * parent, calling clk_drop_range() on the mux won't affect the parent * rate. */ static void clk_test_orphan_transparent_multiple_parent_mux_set_parent_drop_range(struct kunit *test) { … } /* * Test that, for a mux that started orphan but got switched to a valid * parent, the rate of the mux and its new parent are consistent. */ static void clk_test_orphan_transparent_multiple_parent_mux_set_parent_get_rate(struct kunit *test) { … } /* * Test that, for a mux that started orphan but got switched to a valid * parent, calling clk_put() on the mux won't affect the parent rate. */ static void clk_test_orphan_transparent_multiple_parent_mux_set_parent_put(struct kunit *test) { … } /* * Test that, for a mux that started orphan but got switched to a valid * parent, calling clk_set_rate_range() will affect the parent state if * its rate is out of range. */ static void clk_test_orphan_transparent_multiple_parent_mux_set_parent_set_range_modified(struct kunit *test) { … } /* * Test that, for a mux that started orphan but got switched to a valid * parent, calling clk_set_rate_range() won't affect the parent state if * its rate is within range. */ static void clk_test_orphan_transparent_multiple_parent_mux_set_parent_set_range_untouched(struct kunit *test) { … } /* * Test that, for a mux whose current parent hasn't been registered yet, * calling clk_set_rate_range() will succeed, and will be taken into * account when rounding a rate. */ static void clk_test_orphan_transparent_multiple_parent_mux_set_range_round_rate(struct kunit *test) { … } /* * Test that, for a mux that started orphan, was assigned and rate and * then got switched to a valid parent, its rate is eventually within * range. * * FIXME: Even though we update the rate as part of clk_set_parent(), we * don't evaluate whether that new rate is within range and needs to be * adjusted. */ static void clk_test_orphan_transparent_multiple_parent_mux_set_range_set_parent_get_rate(struct kunit *test) { … } static struct kunit_case clk_orphan_transparent_multiple_parent_mux_test_cases[] = …; /* * Test suite for a basic mux clock with two parents. The default parent * isn't registered, only the second parent is. By default, the clock * will thus be orphan. * * These tests exercise the behaviour of the consumer API when dealing * with an orphan clock, and how we deal with the transition to a valid * parent. */ static struct kunit_suite clk_orphan_transparent_multiple_parent_mux_test_suite = …; struct clk_single_parent_ctx { … }; static int clk_single_parent_mux_test_init(struct kunit *test) { … } static void clk_single_parent_mux_test_exit(struct kunit *test) { … } /* * Test that for a clock with a single parent, clk_get_parent() actually * returns the parent. */ static void clk_test_single_parent_mux_get_parent(struct kunit *test) { … } /* * Test that for a clock with a single parent, clk_has_parent() actually * reports it as a parent. */ static void clk_test_single_parent_mux_has_parent(struct kunit *test) { … } /* * Test that for a clock that can't modify its rate and with a single * parent, if we set disjoints range on the parent and then the child, * the second will return an error. * * FIXME: clk_set_rate_range() only considers the current clock when * evaluating whether ranges are disjoints and not the upstream clocks * ranges. */ static void clk_test_single_parent_mux_set_range_disjoint_child_last(struct kunit *test) { … } /* * Test that for a clock that can't modify its rate and with a single * parent, if we set disjoints range on the child and then the parent, * the second will return an error. * * FIXME: clk_set_rate_range() only considers the current clock when * evaluating whether ranges are disjoints and not the downstream clocks * ranges. */ static void clk_test_single_parent_mux_set_range_disjoint_parent_last(struct kunit *test) { … } /* * Test that for a clock that can't modify its rate and with a single * parent, if we set a range on the parent and then call * clk_round_rate(), the boundaries of the parent are taken into * account. */ static void clk_test_single_parent_mux_set_range_round_rate_parent_only(struct kunit *test) { … } /* * Test that for a clock that can't modify its rate and with a single * parent, if we set a range on the parent and a more restrictive one on * the child, and then call clk_round_rate(), the boundaries of the * two clocks are taken into account. */ static void clk_test_single_parent_mux_set_range_round_rate_child_smaller(struct kunit *test) { … } /* * Test that for a clock that can't modify its rate and with a single * parent, if we set a range on the child and a more restrictive one on * the parent, and then call clk_round_rate(), the boundaries of the * two clocks are taken into account. */ static void clk_test_single_parent_mux_set_range_round_rate_parent_smaller(struct kunit *test) { … } static struct kunit_case clk_single_parent_mux_test_cases[] = …; /* * Test suite for a basic mux clock with one parent, with * CLK_SET_RATE_PARENT on the child. * * These tests exercise the consumer API and check that the state of the * child and parent are sane and consistent. */ static struct kunit_suite clk_single_parent_mux_test_suite = …; static int clk_orphan_transparent_single_parent_mux_test_init(struct kunit *test) { … } /* * Test that a mux-only clock, with an initial rate within a range, * will still have the same rate after the range has been enforced. * * See: * https://lore.kernel.org/linux-clk/[email protected]/ */ static void clk_test_orphan_transparent_parent_mux_set_range(struct kunit *test) { … } static struct kunit_case clk_orphan_transparent_single_parent_mux_test_cases[] = …; /* * Test suite for a basic mux clock with one parent. The parent is * registered after its child. The clock will thus be an orphan when * registered, but will no longer be when the tests run. * * These tests make sure a clock that used to be orphan has a sane, * consistent, behaviour. */ static struct kunit_suite clk_orphan_transparent_single_parent_test_suite = …; struct clk_single_parent_two_lvl_ctx { … }; static int clk_orphan_two_level_root_last_test_init(struct kunit *test) { … } static void clk_orphan_two_level_root_last_test_exit(struct kunit *test) { … } /* * Test that, for a clock whose parent used to be orphan, clk_get_rate() * will return the proper rate. */ static void clk_orphan_two_level_root_last_test_get_rate(struct kunit *test) { … } /* * Test that, for a clock whose parent used to be orphan, * clk_set_rate_range() won't affect its rate if it is already within * range. * * See (for Exynos 4210): * https://lore.kernel.org/linux-clk/[email protected]/ */ static void clk_orphan_two_level_root_last_test_set_range(struct kunit *test) { … } static struct kunit_case clk_orphan_two_level_root_last_test_cases[] = …; /* * Test suite for a basic, transparent, clock with a parent that is also * such a clock. The parent's parent is registered last, while the * parent and its child are registered in that order. The intermediate * and leaf clocks will thus be orphan when registered, but the leaf * clock itself will always have its parent and will never be * reparented. Indeed, it's only orphan because its parent is. * * These tests exercise the behaviour of the consumer API when dealing * with an orphan clock, and how we deal with the transition to a valid * parent. */ static struct kunit_suite clk_orphan_two_level_root_last_test_suite = …; /* * Test that clk_set_rate_range won't return an error for a valid range * and that it will make sure the rate of the clock is within the * boundaries. */ static void clk_range_test_set_range(struct kunit *test) { … } /* * Test that calling clk_set_rate_range with a minimum rate higher than * the maximum rate returns an error. */ static void clk_range_test_set_range_invalid(struct kunit *test) { … } /* * Test that users can't set multiple, disjoints, range that would be * impossible to meet. */ static void clk_range_test_multiple_disjoints_range(struct kunit *test) { … } /* * Test that if our clock has some boundaries and we try to round a rate * lower than the minimum, the returned rate will be within range. */ static void clk_range_test_set_range_round_rate_lower(struct kunit *test) { … } /* * Test that if our clock has some boundaries and we try to set a rate * higher than the maximum, the new rate will be within range. */ static void clk_range_test_set_range_set_rate_lower(struct kunit *test) { … } /* * Test that if our clock has some boundaries and we try to round and * set a rate lower than the minimum, the rate returned by * clk_round_rate() will be consistent with the new rate set by * clk_set_rate(). */ static void clk_range_test_set_range_set_round_rate_consistent_lower(struct kunit *test) { … } /* * Test that if our clock has some boundaries and we try to round a rate * higher than the maximum, the returned rate will be within range. */ static void clk_range_test_set_range_round_rate_higher(struct kunit *test) { … } /* * Test that if our clock has some boundaries and we try to set a rate * higher than the maximum, the new rate will be within range. */ static void clk_range_test_set_range_set_rate_higher(struct kunit *test) { … } /* * Test that if our clock has some boundaries and we try to round and * set a rate higher than the maximum, the rate returned by * clk_round_rate() will be consistent with the new rate set by * clk_set_rate(). */ static void clk_range_test_set_range_set_round_rate_consistent_higher(struct kunit *test) { … } /* * Test that if our clock has a rate lower than the minimum set by a * call to clk_set_rate_range(), the rate will be raised to match the * new minimum. * * This assumes that clk_ops.determine_rate or clk_ops.round_rate won't * modify the requested rate, which is our case in clk_dummy_rate_ops. */ static void clk_range_test_set_range_get_rate_raised(struct kunit *test) { … } /* * Test that if our clock has a rate higher than the maximum set by a * call to clk_set_rate_range(), the rate will be lowered to match the * new maximum. * * This assumes that clk_ops.determine_rate or clk_ops.round_rate won't * modify the requested rate, which is our case in clk_dummy_rate_ops. */ static void clk_range_test_set_range_get_rate_lowered(struct kunit *test) { … } static struct kunit_case clk_range_test_cases[] = …; /* * Test suite for a basic rate clock, without any parent. * * These tests exercise the rate range API: clk_set_rate_range(), * clk_set_min_rate(), clk_set_max_rate(), clk_drop_range(). */ static struct kunit_suite clk_range_test_suite = …; /* * Test that if we have several subsequent calls to * clk_set_rate_range(), the core will reevaluate whether a new rate is * needed each and every time. * * With clk_dummy_maximize_rate_ops, this means that the rate will * trail along the maximum as it evolves. */ static void clk_range_test_set_range_rate_maximized(struct kunit *test) { … } /* * Test that if we have several subsequent calls to * clk_set_rate_range(), across multiple users, the core will reevaluate * whether a new rate is needed each and every time. * * With clk_dummy_maximize_rate_ops, this means that the rate will * trail along the maximum as it evolves. */ static void clk_range_test_multiple_set_range_rate_maximized(struct kunit *test) { … } /* * Test that if we have several subsequent calls to * clk_set_rate_range(), across multiple users, the core will reevaluate * whether a new rate is needed, including when a user drop its clock. * * With clk_dummy_maximize_rate_ops, this means that the rate will * trail along the maximum as it evolves. */ static void clk_range_test_multiple_set_range_rate_put_maximized(struct kunit *test) { … } static struct kunit_case clk_range_maximize_test_cases[] = …; /* * Test suite for a basic rate clock, without any parent. * * These tests exercise the rate range API: clk_set_rate_range(), * clk_set_min_rate(), clk_set_max_rate(), clk_drop_range(), with a * driver that will always try to run at the highest possible rate. */ static struct kunit_suite clk_range_maximize_test_suite = …; /* * Test that if we have several subsequent calls to * clk_set_rate_range(), the core will reevaluate whether a new rate is * needed each and every time. * * With clk_dummy_minimize_rate_ops, this means that the rate will * trail along the minimum as it evolves. */ static void clk_range_test_set_range_rate_minimized(struct kunit *test) { … } /* * Test that if we have several subsequent calls to * clk_set_rate_range(), across multiple users, the core will reevaluate * whether a new rate is needed each and every time. * * With clk_dummy_minimize_rate_ops, this means that the rate will * trail along the minimum as it evolves. */ static void clk_range_test_multiple_set_range_rate_minimized(struct kunit *test) { … } /* * Test that if we have several subsequent calls to * clk_set_rate_range(), across multiple users, the core will reevaluate * whether a new rate is needed, including when a user drop its clock. * * With clk_dummy_minimize_rate_ops, this means that the rate will * trail along the minimum as it evolves. */ static void clk_range_test_multiple_set_range_rate_put_minimized(struct kunit *test) { … } static struct kunit_case clk_range_minimize_test_cases[] = …; /* * Test suite for a basic rate clock, without any parent. * * These tests exercise the rate range API: clk_set_rate_range(), * clk_set_min_rate(), clk_set_max_rate(), clk_drop_range(), with a * driver that will always try to run at the lowest possible rate. */ static struct kunit_suite clk_range_minimize_test_suite = …; struct clk_leaf_mux_ctx { … }; static int clk_leaf_mux_determine_rate(struct clk_hw *hw, struct clk_rate_request *req) { … } static const struct clk_ops clk_leaf_mux_set_rate_parent_ops = …; static int clk_leaf_mux_set_rate_parent_test_init(struct kunit *test) { … } static void clk_leaf_mux_set_rate_parent_test_exit(struct kunit *test) { … } struct clk_leaf_mux_set_rate_parent_determine_rate_test_case { … }; static void clk_leaf_mux_set_rate_parent_determine_rate_test_case_to_desc( const struct clk_leaf_mux_set_rate_parent_determine_rate_test_case *t, char *desc) { … } static const struct clk_leaf_mux_set_rate_parent_determine_rate_test_case clk_leaf_mux_set_rate_parent_determine_rate_test_cases[] = …; KUNIT_ARRAY_PARAM(clk_leaf_mux_set_rate_parent_determine_rate_test, clk_leaf_mux_set_rate_parent_determine_rate_test_cases, clk_leaf_mux_set_rate_parent_determine_rate_test_case_to_desc) /* * Test that when a clk that can't change rate itself calls a function like * __clk_determine_rate() on its parent it doesn't get back a clk_rate_request * structure that has the best_parent_hw pointer point to the clk_hw passed * into the determine rate function. See commit 262ca38f4b6e ("clk: Stop * forwarding clk_rate_requests to the parent") for more background. */ static void clk_leaf_mux_set_rate_parent_determine_rate_test(struct kunit *test) { … } static struct kunit_case clk_leaf_mux_set_rate_parent_test_cases[] = …; /* * Test suite for a clock whose parent is a pass-through clk whose parent is a * mux with multiple parents. The leaf and pass-through clocks have the * CLK_SET_RATE_PARENT flag, and will forward rate requests to the mux, which * will then select which parent is the best fit for a given rate. * * These tests exercise the behaviour of muxes, and the proper selection * of parents. */ static struct kunit_suite clk_leaf_mux_set_rate_parent_test_suite = …; struct clk_mux_notifier_rate_change { … }; struct clk_mux_notifier_ctx { … }; #define NOTIFIER_TIMEOUT_MS … static int clk_mux_notifier_callback(struct notifier_block *nb, unsigned long action, void *data) { … } static int clk_mux_notifier_test_init(struct kunit *test) { … } static void clk_mux_notifier_test_exit(struct kunit *test) { … } /* * Test that if the we have a notifier registered on a mux, the core * will notify us when we switch to another parent, and with the proper * old and new rates. */ static void clk_mux_notifier_set_parent_test(struct kunit *test) { … } static struct kunit_case clk_mux_notifier_test_cases[] = …; /* * Test suite for a mux with multiple parents, and a notifier registered * on the mux. * * These tests exercise the behaviour of notifiers. */ static struct kunit_suite clk_mux_notifier_test_suite = …; static int clk_mux_no_reparent_test_init(struct kunit *test) { … } static void clk_mux_no_reparent_test_exit(struct kunit *test) { … } /* * Test that if the we have a mux that cannot change parent and we call * clk_round_rate() on it with a rate that should cause it to change * parent, it won't. */ static void clk_mux_no_reparent_round_rate(struct kunit *test) { … } /* * Test that if the we have a mux that cannot change parent and we call * clk_set_rate() on it with a rate that should cause it to change * parent, it won't. */ static void clk_mux_no_reparent_set_rate(struct kunit *test) { … } static struct kunit_case clk_mux_no_reparent_test_cases[] = …; /* * Test suite for a clock mux that isn't allowed to change parent, using * the clk_hw_determine_rate_no_reparent() helper. * * These tests exercise that helper, and the proper selection of * rates and parents. */ static struct kunit_suite clk_mux_no_reparent_test_suite = …; kunit_test_suites(…); MODULE_DESCRIPTION(…) …; MODULE_LICENSE(…) …;