clk: Improve clock range handling
Subsystems (7)
v7 cover clk: Improve clock range handling
Hi,
This is a follow-up of the discussion here:
https://lore.kernel.org/linux-clk/20210319150355.xzw7ikwdaga2dwhv@gilmour/
and here:
https://lore.kernel.org/all/[email protected]/
While the initial proposal implemented a new API to temporarily raise and lower clock rates based on consumer workloads, Stephen suggested an alternative approach implemented here.
The main issue that needed to be addressed in our case was that in a situation where we would have multiple calls to clk_set_rate_range, we would end up with a clock at the maximum of the minimums being set. This would be expected, but the issue was that if one of the users was to relax or drop its requirements, the rate would be left unchanged, even though the ideal rate would have changed.
So something like
clk_set_rate(user1_clk, 1000);clk_set_min_rate(user1_clk, 2000);clk_set_min_rate(user2_clk, 3000);clk_set_min_rate(user2_clk, 1000);
Would leave the clock running at 3000Hz, while the minimum would now be 2000Hz.
This was mostly due to the fact that the core only triggers a rate change in clk_set_rate_range() if the current rate is outside of the boundaries, but not if it's within the new boundaries.
That series changes that and will trigger a rate change on every call, with the former rate being tried again. This way, providers have a chance to follow whatever policy they see fit for a given clock each time the boundaries change.
This series also implements some kunit tests, first to test a few rate related functions in the CCF, and then extends it to make sure that behaviour has some test coverage.
Let me know what you think
Maxime
Changes from v6:
- Removed unused header
- Used more fitting KUNIT macros
- Enhanced some comments
- Reworded a commit message
Changes from v5:
- Changed clk_hw_create_clk for clk_hw_get_clk since the former isn't exported to modules.
- Added fix for clk_hw_get_clk
Changes from v4:
- Rename the test file
- Move all the tests to the first patch, and fix them up as fixes are done
- Improved the test conditions
- Added more tests
- Improved commit messages
- Fixed a regression where two disjoints clock ranges would now be accepted
Changes from v3:
- Renamed the test file and Kconfig option
- Add option to .kunitconfig
- Switch to kunit_kzalloc
- Use KUNIT_EXPECT_* instead of KUNIT_ASSERT_* where relevant
- Test directly relevant calls instead of going through a temporary variable
- Switch to more precise KUNIT_ASSERT_* macros where relevant
Changes from v2:
- Rebased on current next
- Rewrote the whole thing according to Stephen reviews
- Implemented some kunit tests
Changes from v1:
- Return NULL in clk_request_start if clk pointer is NULL
- Test for clk_req pointer in clk_request_done
- Add another user in vc4
- Rebased on top of v5.15-rc1
Maxime Ripard (12):
clk: Fix clk_hw_get_clk() when dev is NULL
clk: Introduce Kunit Tests for the framework
clk: Enforce that disjoints limits are invalid
clk: Always clamp the rounded rate
clk: Use clamp instead of open-coding our own
clk: Always set the rate on clk_set_range_rate
clk: Add clk_drop_range
clk: bcm: rpi: Add variant structure
clk: bcm: rpi: Set a default minimum rate
clk: bcm: rpi: Run some clocks at the minimum rate allowed drm/vc4: Add logging and comments
drm/vc4: hdmi: Remove clock rate initialization
drivers/clk/.kunitconfig | 1 + drivers/clk/Kconfig | 7 + drivers/clk/Makefile | 1 + drivers/clk/bcm/clk-raspberrypi.c | 125 ++++- drivers/clk/clk.c | 76 ++- drivers/clk/clk_test.c | 795 ++++++++++++++++++++++++++++++ drivers/gpu/drm/vc4/vc4_hdmi.c | 13 - drivers/gpu/drm/vc4/vc4_kms.c | 11 + include/linux/clk.h | 11 + 9 files changed, 985 insertions(+), 55 deletions(-) create mode 100644 drivers/clk/clk_test.c
Signature · 1 line
2.35.1
PATCH v7 1/12 clk: Fix clk_hw_get_clk() when dev is NULL
Any registered clk_core structure can have a NULL pointer in its dev field. While never actually documented, this is evidenced by the wide usage of clk_register and clk_hw_register with a NULL device pointer, and the fact that the core of_clk_hw_register() function also passes a NULL device pointer.
A call to clk_hw_get_clk() on a clk_hw struct whose clk_core is in that case will result in a NULL pointer derefence when it calls dev_name() on that NULL device pointer.
Add a test for this case and use NULL as the dev_id if the device pointer is NULL.
--- drivers/clk/clk.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/clk/clk.c +2 -1
@@ -3773,8 +3773,9 @@ struct clk *clk_hw_create_clk(struct device *dev, struct clk_hw *hw,struct clk *clk_hw_get_clk (struct clk_hw *hw,const char *con_id) {struct device *dev = hw->core->dev; +const char *name = dev ?dev_name (dev) :NULL ; -return clk_hw_create_clk (dev, hw,dev_name (dev), con_id); +return clk_hw_create_clk (dev, hw, name, con_id); }EXPORT_SYMBOL (clk_hw_get_clk);
Signature · 1 line
2.35.1
PATCH v7 2/12 clk: Introduce Kunit Tests for the framework
In reply to Maxime RipardLet's test various parts of the rate-related clock API with the kunit testing framework.
--- drivers/clk/.kunitconfig | 1 + drivers/clk/Kconfig | 7 + drivers/clk/Makefile | 1 + drivers/clk/clk_test.c | 787 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 796 insertions(+) create mode 100644 drivers/clk/clk_test.c
drivers/clk/.kunitconfig +1 -0
@@ -1,3 +1,4 @@ CONFIG_KUNIT=y CONFIG_COMMON_CLK=y +CONFIG_CLK_KUNIT_TEST=y CONFIG_CLK_GATE_KUNIT_TEST=y
drivers/clk/Kconfig +7 -0
@@ -429,6 +429,13 @@ source "drivers/clk/xilinx/Kconfig" source "drivers/clk/zynqmp/Kconfig" # Kunit test cases +config CLK_KUNIT_TEST + tristate "Basic Clock Framework Kunit Tests" if !KUNIT_ALL_TESTS + depends on KUNIT + default KUNIT_ALL_TESTS + help + Kunit tests for the common clock framework. + config CLK_GATE_KUNIT_TEST tristate "Basic gate type Kunit test" if !KUNIT_ALL_TESTS depends on KUNIT
drivers/clk/Makefile +1 -0
@@ -2,6 +2,7 @@ # common clock types obj-$(CONFIG_HAVE_CLK) += clk-devres.o clk-bulk.o clkdev.o obj-$(CONFIG_COMMON_CLK) += clk.o +obj-$(CONFIG_CLK_KUNIT_TEST) += clk_test.o obj-$(CONFIG_COMMON_CLK) += clk-divider.o obj-$(CONFIG_COMMON_CLK) += clk-fixed-factor.o obj-$(CONFIG_COMMON_CLK) += clk-fixed-rate.o
drivers/clk/clk_test.c new file +787 -0
787 lines not on this page · raw message
Signature · 1 line
2.35.1
PATCH v7 3/12 clk: Enforce that disjoints limits are invalid
In reply to Maxime RipardIf we were to have two users of the same clock, doing something like:
clk_set_rate_range(user1, 1000, 2000);clk_set_rate_range(user2, 3000, 4000);
The second call would fail with -EINVAL, preventing from getting in a situation where we end up with impossible limits.
However, this is never explicitly checked against and enforced, and works by relying on an undocumented behaviour of clk_set_rate().
Indeed, on the first clk_set_rate_range will make sure the current clock rate is within the new range, so it will be between 1000 and 2000Hz. On the second clk_set_rate_range(), it will consider (rightfully), that our current clock is outside of the 3000-4000Hz range, and will call clk_core_set_rate_nolock() to set it to 3000Hz.
clk_core_set_rate_nolock() will then call clk_calc_new_rates() that will eventually check that our rate 3000Hz rate is outside the min 3000Hz max 2000Hz range, will bail out, the error will propagate and we'll eventually return -EINVAL.
This solely relies on the fact that clk_calc_new_rates(), and in particular clk_core_determine_round_nolock(), won't modify the new rate allowing the error to be reported. That assumption won't be true for all drivers, and most importantly we'll break that assumption in a later patch.
It can also be argued that we shouldn't even reach the point where we're calling clk_core_set_rate_nolock().
Let's make an explicit check for disjoints range before we're doing anything.
--- drivers/clk/clk.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+)
drivers/clk/clk.c +24 -0
@@ -632,6 +632,24 @@ static void clk_core_get_boundaries(struct clk_core *core, *max_rate =min (*max_rate, clk_user->max_rate); } +static bool clk_core_check_boundaries (struct clk_core *core, +unsigned long min_rate, +unsigned long max_rate) +{ +struct clk *user; + +lockdep_assert_held (&prepare_lock); + +if (min_rate > core->max_rate || max_rate < core->min_rate) +return false; + +hlist_for_each_entry (user, &core->clks, clks_node) +if (min_rate > user->max_rate || max_rate < user->min_rate) +return false; + +return true; +} +void clk_hw_set_rate_range (struct clk_hw *hw,unsigned long min_rate,unsigned long max_rate) {
@@ -2348,6 +2366,11 @@ int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max) clk->min_rate = min; clk->max_rate = max; +if (!clk_core_check_boundaries (clk->core, min, max)) { + ret = -EINVAL ; + goto out; + } + rate =clk_core_get_rate_nolock (clk->core);if (rate < min || rate > max) {/*
@@ -2376,6 +2399,7 @@ int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max) } } +out:if (clk->exclusive_count)clk_core_rate_protect (clk->core);
Signature · 1 line
2.35.1
PATCH v7 4/12 clk: Always clamp the rounded rate
In reply to Maxime RipardThe current core while setting the min and max rate properly in the clk_request structure will not make sure that the requested rate is within these boundaries, leaving it to each and every driver to make sure it is.
It's not clear if this was on purpose or not, but this introduces some inconsistencies within the API.
For example, a user setting a range and then calling clk_round_rate() with a value outside of that range will get the same value back (ignoring any driver adjustements), effectively ignoring the range that was just set.
Another one, arguably worse, is that it also makes clk_round_rate() and clk_set_rate() behave differently if there's a range and the rate being used for both is outside that range. As we have seen, the rate will be returned unchanged by clk_round_rate(), but clk_set_rate() will error out returning -EINVAL.
Let's make sure the framework will always clamp the rate to the current range found on the clock, which will fix both these inconsistencies.
--- drivers/clk/clk.c | 2 ++ drivers/clk/clk_test.c | 50 +++++++++++++++++++++++++++--------------- 2 files changed, 34 insertions(+), 18 deletions(-)
drivers/clk/clk.c +2 -0
@@ -1348,6 +1348,8 @@ static int clk_core_determine_round_nolock(struct clk_core *core,if (!core)return 0 ; + req->rate =clamp (req->rate, req->min_rate, req->max_rate); +/* * At this point, core protection will be disabled * - if the provider is not protected at all
drivers/clk/clk_test.c +32 -18
134 lines not on this page · raw message
Signature · 1 line
2.35.1
PATCH v7 5/12 clk: Use clamp instead of open-coding our own
In reply to Maxime RipardThe code in clk_set_rate_range() will, if the current rate is outside of the new range, force it to the minimum or maximum.
Since it's running under the condition that the rate is either lower than the minimum, or higher than the maximum, this is equivalent to using clamp, while being less readable. Let's switch to using clamp instead.
--- drivers/clk/clk.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-)
drivers/clk/clk.c +1 -5
@@ -2388,11 +2388,7 @@ int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max)* this corner case when determining the rate */ -if (rate < min) - rate = min; -else - rate = max; - + rate =clamp (clk->core->req_rate, min, max); ret =clk_core_set_rate_nolock (clk->core, rate);if (ret) {/* rollback the changes */
Signature · 1 line
2.35.1
PATCH v7 6/12 clk: Always set the rate on clk_set_range_rate
In reply to Maxime RipardWhen we change a clock minimum or maximum using clk_set_rate_range(), clk_set_min_rate() or clk_set_max_rate(), the current code will only trigger a new rate change if the rate is outside of the new boundaries.
However, a clock driver might want to always keep the clock rate to one of its boundary, for example the minimum to keep the power consumption as low as possible.
Since they don't always get called though, clock providers don't have the opportunity to implement this behaviour.
Let's trigger a clk_set_rate() on the previous requested rate every time clk_set_rate_range() is called. That way, providers that care about the new boundaries have a chance to adjust the rate, while providers that don't care about those new boundaries will return the same rate than before, which will be ignored by clk_set_rate() and won't result in a new rate change.
--- drivers/clk/clk.c | 45 ++++++++++++++++---------------- drivers/clk/clk_test.c | 58 +++++++++++++++++++----------------------- 2 files changed, 49 insertions(+), 54 deletions(-)
drivers/clk/clk.c +23 -22
@@ -2373,28 +2373,29 @@ int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max) goto out; } - rate =clk_core_get_rate_nolock (clk->core); -if (rate < min || rate > max) { -/* -* FIXME: -* We are in bit of trouble here, current rate is outside the -* the requested range. We are going try to request appropriate -* range boundary but there is a catch. It may fail for the -* usual reason (clock broken, clock protected, etc) but also -* because: -* - round_rate() was not favorable and fell on the wrong -* side of the boundary -* - the determine_rate() callback does not really check for -* this corner case when determining the rate -*/ - - rate =clamp (clk->core->req_rate, min, max); - ret =clk_core_set_rate_nolock (clk->core, rate); -if (ret) { -/* rollback the changes */ - clk->min_rate = old_min; - clk->max_rate = old_max; - } +/* +* Since the boundaries have been changed, let's give the +* opportunity to the provider to adjust the clock rate based on +* the new boundaries. +* +* We also need to handle the case where the clock is currently +* outside of the boundaries. Clamping the last requested rate +* to the current minimum and maximum will also handle this. +* +* FIXME: +* There is a catch. It may fail for the usual reason (clock +* broken, clock protected, etc) but also because: +* - round_rate() was not favorable and fell on the wrong +* side of the boundary +* - the determine_rate() callback does not really check for +* this corner case when determining the rate +*/ + rate =clamp (clk->core->req_rate, min, max); + ret =clk_core_set_rate_nolock (clk->core, rate); +if (ret) { +/* rollback the changes */ + clk->min_rate = old_min; + clk->max_rate = old_max; } out:
drivers/clk/clk_test.c +26 -32
104 lines not on this page · raw message
Signature · 1 line
2.35.1
Re: [PATCH v7 06/12] clk: Always set the rate on clk_set_range_rate
Quoted 71 lines · Maxime Ripard
When we change a clock minimum or maximum using
clk_set_rate_range(),clk_set_min_rate()orclk_set_max_rate(), the current code will only trigger a new rate change if the rate is outside of the new boundaries.However, a clock driver might want to always keep the clock rate to one of its boundary, for example the minimum to keep the power consumption as low as possible.
Since they don't always get called though, clock providers don't have the opportunity to implement this behaviour.
Let's trigger a
clk_set_rate()on the previous requested rate every timeclk_set_rate_range()is called. That way, providers that care about the new boundaries have a chance to adjust the rate, while providers that don't care about those new boundaries will return the same rate than before, which will be ignored byclk_set_rate()and won't result in a new rate change.Suggested-by Stephen Boyd <[email protected]>Signed-off-by Maxime Ripard <[email protected]>--- drivers/clk/clk.c | 45 ++++++++++++++++---------------- drivers/clk/clk_test.c | 58 +++++++++++++++++++----------------------- 2 files changed, 49 insertions(+), 54 deletions(-)drivers/clk/clk.c +16
-22@@ -2373,28 +2373,29 @@ int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max) goto out; } - rate =clk_core_get_rate_nolock (clk->core); -if (rate < min || rate > max) { -/* -* FIXME: -* We are in bit of trouble here, current rate is outside the -* the requested range. We are going try to request appropriate -* range boundary but there is a catch. It may fail for the -* usual reason (clock broken, clock protected, etc) but also -* because: -* - round_rate() was not favorable and fell on the wrong -* side of the boundary -* - the determine_rate() callback does not really check for -* this corner case when determining the rate -*/ - - rate =clamp (clk->core->req_rate, min, max); - ret =clk_core_set_rate_nolock (clk->core, rate); -if (ret) { -/* rollback the changes */ - clk->min_rate = old_min; - clk->max_rate = old_max; - } +/* +* Since the boundaries have been changed, let's give the +* opportunity to the provider to adjust the clock rate based on +* the new boundaries. +* +* We also need to handle the case where the clock is currently +* outside of the boundaries. Clamping the last requested rate +* to the current minimum and maximum will also handle this. +* +* FIXME: +* There is a catch. It may fail for the usual reason (clock +* broken, clock protected, etc) but also because: +* - round_rate() was not favorable and fell on the wrong +* side of the boundary +* - the determine_rate() callback does not really check for +* this corner case when determining the rate
drivers/clk/clk.c int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max) · line 2401
Quoted 10 lines
drivers/clk/clk.c +7
-0+*/ + rate =clamp (clk->core->req_rate, min, max); + ret =clk_core_set_rate_nolock (clk->core, rate); +if (ret) { +/* rollback the changes */ + clk->min_rate = old_min; + clk->max_rate = old_max; } out:
Hi,
NVIDIA Tegra30 no longer boots with this change.
You can't assume that rate was requested by clk_set_rate() before clk_set_rate_range() is called, see what [1] does. T30 memory rate now drops to min on boot when clk debug range is inited innocuously and CPU no longer can make any progress because display controller takes out whole memory bandwidth.
If clk_set_rate() wasn't ever invoked and req_rate=0, then you must not change the clk rate if it's within the new range. Please revert this patch, thanks.
Re: [PATCH v7 06/12] clk: Always set the rate on clk_set_range_rate
Hi,
Quoted 81 lines · Dmitry Osipenko
Quoted 80 lines · Maxime Ripard
When we change a clock minimum or maximum using
clk_set_rate_range(),clk_set_min_rate()orclk_set_max_rate(), the current code will only trigger a new rate change if the rate is outside of the new boundaries.However, a clock driver might want to always keep the clock rate to one of its boundary, for example the minimum to keep the power consumption as low as possible.
Since they don't always get called though, clock providers don't have the opportunity to implement this behaviour.
Let's trigger a
clk_set_rate()on the previous requested rate every timeclk_set_rate_range()is called. That way, providers that care about the new boundaries have a chance to adjust the rate, while providers that don't care about those new boundaries will return the same rate than before, which will be ignored byclk_set_rate()and won't result in a new rate change.Suggested-by Stephen Boyd <[email protected]>Signed-off-by Maxime Ripard <[email protected]>--- drivers/clk/clk.c | 45 ++++++++++++++++---------------- drivers/clk/clk_test.c | 58 +++++++++++++++++++----------------------- 2 files changed, 49 insertions(+), 54 deletions(-)drivers/clk/clk.c +23
-22@@ -2373,28 +2373,29 @@ int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max) goto out; } - rate =clk_core_get_rate_nolock (clk->core); -if (rate < min || rate > max) { -/* -* FIXME: -* We are in bit of trouble here, current rate is outside the -* the requested range. We are going try to request appropriate -* range boundary but there is a catch. It may fail for the -* usual reason (clock broken, clock protected, etc) but also -* because: -* - round_rate() was not favorable and fell on the wrong -* side of the boundary -* - the determine_rate() callback does not really check for -* this corner case when determining the rate -*/ - - rate =clamp (clk->core->req_rate, min, max); - ret =clk_core_set_rate_nolock (clk->core, rate); -if (ret) { -/* rollback the changes */ - clk->min_rate = old_min; - clk->max_rate = old_max; - } +/* +* Since the boundaries have been changed, let's give the +* opportunity to the provider to adjust the clock rate based on +* the new boundaries. +* +* We also need to handle the case where the clock is currently +* outside of the boundaries. Clamping the last requested rate +* to the current minimum and maximum will also handle this. +* +* FIXME: +* There is a catch. It may fail for the usual reason (clock +* broken, clock protected, etc) but also because: +* - round_rate() was not favorable and fell on the wrong +* side of the boundary +* - the determine_rate() callback does not really check for +* this corner case when determining the rate +*/ + rate =clamp (clk->core->req_rate, min, max); + ret =clk_core_set_rate_nolock (clk->core, rate); +if (ret) { +/* rollback the changes */ + clk->min_rate = old_min; + clk->max_rate = old_max; }
Quoted 6 lines
drivers/clk/clk.c int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max) · line 2401
Quoted 1 line
drivers/clk/clk.c +0
-0out:NVIDIA Tegra30 no longer boots with this change.
You can't assume that rate was requested by
clk_set_rate()beforeclk_set_rate_range()is called, see what [1] does.
We don't, and it would be bad indeed.
We even have (multiple) tests to cover that case:
https://git.kernel.org/pub/scm/linux/kernel/git/clk/linux.git/tree/drivers/clk/clk_test.c?h=clk-range&id=a9b269310ad9abb2f206fe814fd3afcadddce3aa#n242
Quoted 10 lines
T30 memory rate now drops to min on boot when clk debug range is inited innocuously and CPU no longer can make any progress because display controller takes out whole memory bandwidth.
If
clk_set_rate()wasn't ever invoked and req_rate=0, then you must not change the clk rate if it's within the new range. Please revert this patch, thanks.
The whole point of this patch is to give an opportunity to every driver to change the rate whenever the boundaries have changed, so we very much want to have the option to change it if clk_set_rate() has never been called.
However, I think the issue is why req_rate would be 0 in the first place?
req_rate is initialized to what recalc_rate returns:
https://elixir.bootlin.com/linux/latest/source/drivers/clk/clk.c#L3607
So the case where req_rate is 0 shouldn't occur unless you had an explicit clk_set_rate to 0, or if your clock was orphaned at some point.
Judging from the code, it seems like the latter is the most plausible. Indeed, __clk_core_init() will set req_rate to 0 if the clock is orphaned (just like rate and accuracy), and clk_core_reparent_orphans_nolock will be in charge of updating them when the clock is no longer an orphan.
However, clk_core_reparent_orphans_nolock() will update rate by calling __clk_recalc_rate and accuracy by calling __clk_recalc_accuracies, but it never sets req_rate.
I'm not sure if this is the right patch, Stephen will tell, but could you test:
------------------------ >8 ------------------------
drivers/clk/clk.c +1 -0
@@ -3479,6 +3479,7 @@ static void clk_core_reparent_orphans_nolock(void)__clk_set_parent_after (orphan, parent,NULL );__clk_recalc_accuracies (orphan);__clk_recalc_rates (orphan,0 ); + orphan->req_rate = orphan->rate; } } }
------------------------ >8 ------------------------
Maxime
Stephen Boyd
maintainerRe: [PATCH v7 06/12] clk: Always set the rate on clk_set_range_rate
Quoting Maxime Ripard (2022-03-23 01:51:40)
Quoted 23 lines
Hi,
The whole point of this patch is to give an opportunity to every driver to change the rate whenever the boundaries have changed, so we very much want to have the option to change it if
clk_set_rate()has never been called.However, I think the issue is why
req_ratewould be 0 in the first place?
req_rateis initialized to whatrecalc_ratereturns:
https://elixir.bootlin.com/linux/latest/source/drivers/clk/clk.c#L3607So the case where
req_rateis 0 shouldn't occur unless you had an explicitclk_set_rateto 0, or if your clock was orphaned at some point.Judging from the code, it seems like the latter is the most plausible. Indeed,
__clk_core_init()will setreq_rateto 0 if the clock is orphaned (just like rate and accuracy), andclk_core_reparent_orphans_nolockwill be in charge of updating them when the clock is no longer an orphan.
Quoted 6 lines
However,
clk_core_reparent_orphans_nolock()will update rate by calling__clk_recalc_rateand accuracy by calling__clk_recalc_accuracies, but it never setsreq_rate.I'm not sure if this is the right patch, Stephen will tell, but could you test:
It looks correct to me. Would be helpful to have some comment of course that we're setting a default req_rate because we want a clk_set_rate_range() before clk_set_rate() to work properly when this clk is initially an orphan. We should be able to code up a test case for that too by registering an orphan and then registering the parent and then calling clk_set_rate_range().
Dmitry Osipenko
Tested-byRe: [PATCH v7 06/12] clk: Always set the rate on clk_set_range_rate
In reply to Maxime RipardQuoted 141 lines · Maxime Ripard
Hi,
Quoted 81 lines · Dmitry Osipenko
Quoted 80 lines · Maxime Ripard
When we change a clock minimum or maximum using
clk_set_rate_range(),clk_set_min_rate()orclk_set_max_rate(), the current code will only trigger a new rate change if the rate is outside of the new boundaries.However, a clock driver might want to always keep the clock rate to one of its boundary, for example the minimum to keep the power consumption as low as possible.
Since they don't always get called though, clock providers don't have the opportunity to implement this behaviour.
Let's trigger a
clk_set_rate()on the previous requested rate every timeclk_set_rate_range()is called. That way, providers that care about the new boundaries have a chance to adjust the rate, while providers that don't care about those new boundaries will return the same rate than before, which will be ignored byclk_set_rate()and won't result in a new rate change.Suggested-by Stephen Boyd <[email protected]>Signed-off-by Maxime Ripard <[email protected]>--- drivers/clk/clk.c | 45 ++++++++++++++++---------------- drivers/clk/clk_test.c | 58 +++++++++++++++++++----------------------- 2 files changed, 49 insertions(+), 54 deletions(-)drivers/clk/clk.c +23
-22@@ -2373,28 +2373,29 @@ int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max) goto out; } - rate =clk_core_get_rate_nolock (clk->core); -if (rate < min || rate > max) { -/* -* FIXME: -* We are in bit of trouble here, current rate is outside the -* the requested range. We are going try to request appropriate -* range boundary but there is a catch. It may fail for the -* usual reason (clock broken, clock protected, etc) but also -* because: -* - round_rate() was not favorable and fell on the wrong -* side of the boundary -* - the determine_rate() callback does not really check for -* this corner case when determining the rate -*/ - - rate =clamp (clk->core->req_rate, min, max); - ret =clk_core_set_rate_nolock (clk->core, rate); -if (ret) { -/* rollback the changes */ - clk->min_rate = old_min; - clk->max_rate = old_max; - } +/* +* Since the boundaries have been changed, let's give the +* opportunity to the provider to adjust the clock rate based on +* the new boundaries. +* +* We also need to handle the case where the clock is currently +* outside of the boundaries. Clamping the last requested rate +* to the current minimum and maximum will also handle this. +* +* FIXME: +* There is a catch. It may fail for the usual reason (clock +* broken, clock protected, etc) but also because: +* - round_rate() was not favorable and fell on the wrong +* side of the boundary +* - the determine_rate() callback does not really check for +* this corner case when determining the rate +*/ + rate =clamp (clk->core->req_rate, min, max); + ret =clk_core_set_rate_nolock (clk->core, rate); +if (ret) { +/* rollback the changes */ + clk->min_rate = old_min; + clk->max_rate = old_max; }Quoted 6 lines
drivers/clk/clk.c int clk_set_rate_range(struct clk *clk, unsigned long min, unsigned long max) · line 2401
Quoted 1 line
drivers/clk/clk.c +0
-0out:NVIDIA Tegra30 no longer boots with this change.
You can't assume that rate was requested by
clk_set_rate()beforeclk_set_rate_range()is called, see what [1] does.We don't, and it would be bad indeed.
We even have (multiple) tests to cover that case:
https://git.kernel.org/pub/scm/linux/kernel/git/clk/linux.git/tree/drivers/clk/clk_test.c?h=clk-range&id=a9b269310ad9abb2f206fe814fd3afcadddce3aa#n242Quoted 10 lines
T30 memory rate now drops to min on boot when clk debug range is inited innocuously and CPU no longer can make any progress because display controller takes out whole memory bandwidth.
If
clk_set_rate()wasn't ever invoked and req_rate=0, then you must not change the clk rate if it's within the new range. Please revert this patch, thanks.The whole point of this patch is to give an opportunity to every driver to change the rate whenever the boundaries have changed, so we very much want to have the option to change it if
clk_set_rate()has never been called.However, I think the issue is why
req_ratewould be 0 in the first place?
req_rateis initialized to whatrecalc_ratereturns:
https://elixir.bootlin.com/linux/latest/source/drivers/clk/clk.c#L3607So the case where
req_rateis 0 shouldn't occur unless you had an explicitclk_set_rateto 0, or if your clock was orphaned at some point.Judging from the code, it seems like the latter is the most plausible. Indeed,
__clk_core_init()will setreq_rateto 0 if the clock is orphaned (just like rate and accuracy), andclk_core_reparent_orphans_nolockwill be in charge of updating them when the clock is no longer an orphan.However,
clk_core_reparent_orphans_nolock()will update rate by calling__clk_recalc_rateand accuracy by calling__clk_recalc_accuracies, but it never setsreq_rate.I'm not sure if this is the right patch, Stephen will tell, but could you test:
------------------------ >8 ------------------------
drivers/clk/clk.c +0
-0
drivers/clk/clk.c static void clk_core_reparent_orphans_nolock(void)
Quoted 9 lines
drivers/clk/clk.c +1
-1@@ -3479,6 +3479,7 @@ static void clk_core_reparent_orphans_nolock(void)__clk_set_parent_after (orphan, parent,NULL );__clk_recalc_accuracies (orphan);__clk_recalc_rates (orphan,0 ); + orphan->req_rate = orphan->rate; } } } ------------------------ >8 ------------------------
It works, thank you!
PATCH v7 7/12 clk: Add clk_drop_range
In reply to Maxime RipardIn order to reset the range on a clock, we need to call clk_set_rate_range with a minimum of 0 and a maximum of ULONG_MAX. Since it's fairly inconvenient, let's introduce a clk_drop_range() function that will do just this.
--- drivers/clk/clk_test.c | 4 ++-- include/linux/clk.h | 11 +++++++++++ 2 files changed, 13 insertions(+), 2 deletions(-)
drivers/clk/clk_test.c +2 -2
@@ -645,7 +645,7 @@ static void clk_range_test_multiple_set_range_rate_maximized(struct kunit *test)KUNIT_EXPECT_EQ (test, rate,DUMMY_CLOCK_RATE_1 );KUNIT_ASSERT_EQ (test, -clk_set_rate_range (user2,0 ,ULONG_MAX ), +clk_drop_range (user2),0 ); rate =clk_get_rate (clk);
@@ -762,7 +762,7 @@ static void clk_range_test_multiple_set_range_rate_minimized(struct kunit *test)KUNIT_EXPECT_EQ (test, rate,DUMMY_CLOCK_RATE_2 );KUNIT_ASSERT_EQ (test, -clk_set_rate_range (user2,0 ,ULONG_MAX ), +clk_drop_range (user2),0 ); rate =clk_get_rate (clk);
include/linux/clk.h +11 -0
@@ -986,6 +986,17 @@ static inline void clk_bulk_disable_unprepare(int num_clks,clk_bulk_unprepare (num_clks, clks); } +/** +* clk_drop_range - Reset any range set on that clock +* @clk: clock source +* +* Returns success (0) or negative errno. +*/ +static inline int clk_drop_range (struct clk *clk) +{ +return clk_set_rate_range (clk,0 ,ULONG_MAX ); +} +/** * clk_get_optional - lookup and obtain a reference to an optional clock * producer.
Signature · 1 line
2.35.1
PATCH v7 8/12 clk: bcm: rpi: Add variant structure
In reply to Maxime RipardWe only export a bunch of firmware clocks, and some of them require special treatment.
This has been do so far using some tests on the clock id in various places, but this is fairly hard to extend and doesn't scale very well.
Since we'll need some more cases in the next patches, let's switch to a variant structure that defines the behaviour we need to have for a given clock.
--- drivers/clk/bcm/clk-raspberrypi.c | 62 +++++++++++++++++++++++-------- 1 file changed, 46 insertions(+), 16 deletions(-)
drivers/clk/bcm/clk-raspberrypi.c +46 -16
@@ -56,6 +56,8 @@ static char *rpi_firmware_clk_names[] = {#define RPI_FIRMWARE_STATE_ENABLE_BIT BIT(0)#define RPI_FIRMWARE_STATE_WAIT_BIT BIT(1) +struct raspberrypi_clk_variant ; +struct raspberrypi_clk {struct device *dev;struct rpi_firmware *firmware;
@@ -66,10 +68,36 @@ struct raspberrypi_clk_data {struct clk_hw hw;unsigned int id; +struct raspberrypi_clk_variant *variant;struct raspberrypi_clk *rpi; }; +struct raspberrypi_clk_variant { +bool export; +char *clkdev; +}; + +static struct raspberrypi_clk_variant +raspberrypi_clk_variants[RPI_FIRMWARE_NUM_CLK_ID ] = { + [RPI_FIRMWARE_ARM_CLK_ID ] = { + .export = true, + .clkdev ="cpu0" , + }, + [RPI_FIRMWARE_CORE_CLK_ID ] = { + .export = true, + }, + [RPI_FIRMWARE_M2MC_CLK_ID ] = { + .export = true, + }, + [RPI_FIRMWARE_V3D_CLK_ID ] = { + .export = true, + }, + [RPI_FIRMWARE_PIXEL_BVB_CLK_ID ] = { + .export = true, + }, +}; +/* * Structure of the message passed to Raspberry Pi's firmware in order to * change clock rates. The 'disable_turbo' option is only available to the ARM
@@ -183,7 +211,8 @@ static const struct clk_ops raspberrypi_firmware_clk_ops = {static struct clk_hw *raspberrypi_clk_register (struct raspberrypi_clk *rpi,unsigned int parent, -unsigned int id) +unsigned int id, +struct raspberrypi_clk_variant *variant) {struct raspberrypi_clk_data *data;struct clk_init_data init = {};
@@ -195,6 +224,7 @@ static struct clk_hw *raspberrypi_clk_register(struct raspberrypi_clk *rpi,return ERR_PTR (-ENOMEM ); data->rpi = rpi; data->id = id; + data->variant = variant; init.name = devm_kasprintf(rpi->dev, GFP_KERNEL, "fw-clk-%s",
@@ -228,9 +258,9 @@ static struct clk_hw *raspberrypi_clk_register(struct raspberrypi_clk *rpi,clk_hw_set_rate_range (&data->hw, min_rate, max_rate); -if (id ==RPI_FIRMWARE_ARM_CLK_ID ) { +if (variant->clkdev) { ret =devm_clk_hw_register_clkdev (rpi->dev, &data->hw, -NULL ,"cpu0" ); +NULL , variant->clkdev);if (ret) {dev_err (rpi->dev,"Failed to initialize clkdev\n" );return ERR_PTR (ret);
@@ -264,27 +294,27 @@ static int raspberrypi_discover_clocks(struct raspberrypi_clk *rpi,return ret;while (clks->id) { -struct clk_hw *hw; +struct raspberrypi_clk_variant *variant; + +if (clks->id >RPI_FIRMWARE_NUM_CLK_ID ) { +dev_err (rpi->dev,"Unknown clock id: %u" , clks->id); +return -EINVAL ; + } + + variant = &raspberrypi_clk_variants[clks->id]; +if (variant->export) { +struct clk_hw *hw; -switch (clks->id) { -case RPI_FIRMWARE_ARM_CLK_ID : -case RPI_FIRMWARE_CORE_CLK_ID : -case RPI_FIRMWARE_M2MC_CLK_ID : -case RPI_FIRMWARE_V3D_CLK_ID : -case RPI_FIRMWARE_PIXEL_BVB_CLK_ID : hw =raspberrypi_clk_register (rpi, clks->parent, - clks->id); + clks->id, variant);if (IS_ERR (hw))return PTR_ERR (hw); data->hws[clks->id] = hw; data->num = clks->id +1 ; - fallthrough; - -default : - clks++; -break ; } + + clks++; }return 0 ;
Signature · 1 line
2.35.1
PATCH v7 9/12 clk: bcm: rpi: Set a default minimum rate
In reply to Maxime RipardThe M2MC clock provides the state machine clock for both HDMI controllers.
However, if no HDMI monitor is plugged in at boot, its clock rate will be left at 0 by the firmware and will make any register access end up in a CPU stall, even though the clock was enabled.
We had some code in the HDMI controller to deal with this before, but it makes more sense to have it in the clock driver. Move it there.
--- drivers/clk/bcm/clk-raspberrypi.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+)
drivers/clk/bcm/clk-raspberrypi.c +26 -0
@@ -76,6 +76,7 @@ struct raspberrypi_clk_data {struct raspberrypi_clk_variant {bool export;char *clkdev; +unsigned long min_rate; }; static struct raspberrypi_clk_variant
@@ -89,6 +90,18 @@ raspberrypi_clk_variants[RPI_FIRMWARE_NUM_CLK_ID] = { }, [RPI_FIRMWARE_M2MC_CLK_ID] = { .export = true, + +/* +* If we boot without any cable connected to any of the +* HDMI connector, the firmware will skip the HSM +* initialization and leave it with a rate of 0, +* resulting in a bus lockup when we're accessing the +* registers even if it's enabled. +* +* Let's put a sensible default so that we don't end up +* in this situation. +*/ + .min_rate = 120000000, }, [RPI_FIRMWARE_V3D_CLK_ID] = { .export = true,
@@ -267,6 +280,19 @@ static struct clk_hw *raspberrypi_clk_register(struct raspberrypi_clk *rpi, } } +if (variant->min_rate) { +unsigned long rate; + +clk_hw_set_rate_range (&data->hw, variant->min_rate, max_rate); + + rate =raspberrypi_fw_get_rate (&data->hw,0 ); +if (rate < variant->min_rate) { + ret =raspberrypi_fw_set_rate (&data->hw, variant->min_rate,0 ); +if (ret) +return ERR_PTR (ret); + } + } +return &data->hw; }
Signature · 1 line
2.35.1
PATCH v7 10/12 clk: bcm: rpi: Run some clocks at the minimum rate allowed
In reply to Maxime RipardThe core clock and M2MC clocks are shared between some devices (Unicam controllers and the HVS, and the HDMI controllers, respectively) that will have various, varying, requirements depending on their current work load.
Since those loads can require a fairly high clock rate in extreme conditions (up to ~600MHz), we can end up running those clocks at their maximum frequency even though we no longer require such a high rate.
Fortunately, those devices don't require an exact rate but a minimum rate, and all the drivers are using clk_set_min_rate. Thus, we can just rely on the fact that the clk_request minimum (which is the aggregated minimum of all the clock users) is what we want at all times.
--- drivers/clk/bcm/clk-raspberrypi.c | 37 +++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+)
drivers/clk/bcm/clk-raspberrypi.c +37 -0
@@ -77,6 +77,7 @@ struct raspberrypi_clk_variant {bool export;char *clkdev;unsigned long min_rate; +bool minimize; }; static struct raspberrypi_clk_variant
@@ -87,6 +88,18 @@ raspberrypi_clk_variants[RPI_FIRMWARE_NUM_CLK_ID] = { }, [RPI_FIRMWARE_CORE_CLK_ID] = { .export = true, + +/* +* The clock is shared between the HVS and the CSI +* controllers, on the BCM2711 and will change depending +* on the pixels composited on the HVS and the capture +* resolution on Unicam. +* +* Since the rate can get quite large, and we need to +* coordinate between both driver instances, let's +* always use the minimum the drivers will let us. +*/ + .minimize = true, }, [RPI_FIRMWARE_M2MC_CLK_ID] = { .export = true,
@@ -102,6 +115,16 @@ raspberrypi_clk_variants[RPI_FIRMWARE_NUM_CLK_ID] = {* in this situation. */ .min_rate = 120000000, + +/* +* The clock is shared between the two HDMI controllers +* on the BCM2711 and will change depending on the +* resolution output on each. Since the rate can get +* quite large, and we need to coordinate between both +* driver instances, let's always use the minimum the +* drivers will let us. +*/ + .minimize = true, }, [RPI_FIRMWARE_V3D_CLK_ID] = { .export = true,
@@ -206,12 +229,26 @@ static int raspberrypi_fw_set_rate(struct clk_hw *hw, unsigned long rate,static int raspberrypi_fw_dumb_determine_rate (struct clk_hw *hw,struct clk_rate_request *req) { +struct raspberrypi_clk_data *data = + container_of(hw, struct raspberrypi_clk_data, hw); +struct raspberrypi_clk_variant *variant = data->variant; +/* * The firmware will do the rounding but that isn't part of * the interface with the firmware, so we just do our best * here. */ + req->rate =clamp (req->rate, req->min_rate, req->max_rate); + +/* +* We want to aggressively reduce the clock rate here, so let's +* just ignore the requested rate and return the bare minimum +* rate we can get away with. +*/ +if (variant->minimize && req->min_rate >0 ) + req->rate = req->min_rate; +return 0 ; }
Signature · 1 line
2.35.1
PATCH v7 11/12 drm/vc4: Add logging and comments
In reply to Maxime RipardThe HVS core clock isn't really obvious, so let's add a bunch more comments and some logging for easier debugging.
--- drivers/gpu/drm/vc4/vc4_kms.c | 11 +++++++++++ 1 file changed, 11 insertions(+)
drivers/gpu/drm/vc4/vc4_kms.c +11 -0
@@ -389,8 +389,15 @@ static void vc4_atomic_commit_tail(struct drm_atomic_state *state)500000000 , new_hvs_state->core_clock_rate); +drm_dbg (dev,"Raising the core clock at %lu Hz\n" , core_rate); + +/* +* Do a temporary request on the core clock during the +* modeset. +*/ clk_set_min_rate (hvs->core_clk, core_rate); } +drm_atomic_helper_commit_modeset_disables (dev, state);vc4_ctm_commit (vc4, state);
@@ -416,6 +423,10 @@ static void vc4_atomic_commit_tail(struct drm_atomic_state *state)drm_dbg (dev,"Running the core clock at %lu Hz\n" , new_hvs_state->core_clock_rate); +/* +* Request a clock rate based on the current HVS +* requirements. +*/ clk_set_min_rate (hvs->core_clk, new_hvs_state->core_clock_rate); } }
Signature · 1 line
2.35.1
Re: [PATCH v7 11/12] drm/vc4: Add logging and comments
Am 25.02.22 um 15:35 schrieb Maxime Ripard:
Quoted 4 lines
The HVS core clock isn't really obvious, so let's add a bunch more comments and some logging for easier debugging.
Signed-off-by Maxime Ripard <[email protected]>
Quoted 35 lines
--- drivers/gpu/drm/vc4/vc4_kms.c | 11 +++++++++++ 1 file changed, 11 insertions(+)drivers/gpu/drm/vc4/vc4_kms.c +11
-0@@ -389,8 +389,15 @@ static void vc4_atomic_commit_tail(struct drm_atomic_state *state)500000000 , new_hvs_state->core_clock_rate); +drm_dbg (dev,"Raising the core clock at %lu Hz\n" , core_rate); + +/* +* Do a temporary request on the core clock during the +* modeset. +*/ clk_set_min_rate (hvs->core_clk, core_rate); } +drm_atomic_helper_commit_modeset_disables (dev, state);vc4_ctm_commit (vc4, state);@@ -416,6 +423,10 @@ static void vc4_atomic_commit_tail(struct drm_atomic_state *state)drm_dbg (dev,"Running the core clock at %lu Hz\n" , new_hvs_state->core_clock_rate); +/* +* Request a clock rate based on the current HVS +* requirements. +*/ clk_set_min_rate (hvs->core_clk, new_hvs_state->core_clock_rate); } }
Signature · 6 lines
Thomas Zimmermann Graphics Driver Developer SUSE Software Solutions Germany GmbH Maxfeldstr. 5, 90409 Nürnberg, Germany (HRB 36809, AG Nürnberg) Geschäftsführer: Ivo Totev
PATCH v7 12/12 drm/vc4: hdmi: Remove clock rate initialization
In reply to Maxime RipardNow that the clock driver makes sure we never end up with a rate of 0, the HDMI driver doesn't need to care anymore.
--- drivers/gpu/drm/vc4/vc4_hdmi.c | 13 ------------- 1 file changed, 13 deletions(-)
drivers/gpu/drm/vc4/vc4_hdmi.c +0 -13
@@ -2576,19 +2576,6 @@ static int vc4_hdmi_bind(struct device *dev, struct device *master, void *data) vc4_hdmi->disable_4kp60 = true; } -/* -* If we boot without any cable connected to the HDMI connector, -* the firmware will skip the HSM initialization and leave it -* with a rate of 0, resulting in a bus lockup when we're -* accessing the registers even if it's enabled. -* -* Let's put a sensible default at runtime_resume so that we -* don't end up in this situation. -*/ - ret =clk_set_min_rate (vc4_hdmi->hsm_clock,HSM_MIN_CLOCK_FREQ ); -if (ret) - goto err_put_ddc; -/* * We need to have the device powered up at this point to call * our reset hook and for the CEC init.
Signature · 1 line
2.35.1
Re: [PATCH v7 12/12] drm/vc4: hdmi: Remove clock rate initialization
Am 25.02.22 um 15:35 schrieb Maxime Ripard:
Quoted 4 lines
Now that the clock driver makes sure we never end up with a rate of 0, the HDMI driver doesn't need to care anymore.
Signed-off-by Maxime Ripard <[email protected]>
Quoted 28 lines
--- drivers/gpu/drm/vc4/vc4_hdmi.c | 13 ------------- 1 file changed, 13 deletions(-)drivers/gpu/drm/vc4/vc4_hdmi.c +0
-13@@ -2576,19 +2576,6 @@ static int vc4_hdmi_bind(struct device *dev, struct device *master, void *data) vc4_hdmi->disable_4kp60 = true; } -/* -* If we boot without any cable connected to the HDMI connector, -* the firmware will skip the HSM initialization and leave it -* with a rate of 0, resulting in a bus lockup when we're -* accessing the registers even if it's enabled. -* -* Let's put a sensible default at runtime_resume so that we -* don't end up in this situation. -*/ - ret =clk_set_min_rate (vc4_hdmi->hsm_clock,HSM_MIN_CLOCK_FREQ ); -if (ret) - goto err_put_ddc; -/* * We need to have the device powered up at this point to call * our reset hook and for the CEC init.
Signature · 6 lines
Thomas Zimmermann Graphics Driver Developer SUSE Software Solutions Germany GmbH Maxfeldstr. 5, 90409 Nürnberg, Germany (HRB 36809, AG Nürnberg) Geschäftsführer: Ivo Totev
Stephen Boyd
maintainerQuoting Maxime Ripard (2022-02-25 06:35:22)
Quoted 38 lines
Hi,
This is a follow-up of the discussion here:
https://lore.kernel.org/linux-clk/20210319150355.xzw7ikwdaga2dwhv@gilmour/and here:
https://lore.kernel.org/all/[email protected]/While the initial proposal implemented a new API to temporarily raise and lower clock rates based on consumer workloads, Stephen suggested an alternative approach implemented here.
The main issue that needed to be addressed in our case was that in a situation where we would have multiple calls to
clk_set_rate_range, we would end up with a clock at the maximum of the minimums being set. This would be expected, but the issue was that if one of the users was to relax or drop its requirements, the rate would be left unchanged, even though the ideal rate would have changed.So something like
clk_set_rate(user1_clk, 1000);clk_set_min_rate(user1_clk, 2000);clk_set_min_rate(user2_clk, 3000);clk_set_min_rate(user2_clk, 1000);Would leave the clock running at 3000Hz, while the minimum would now be 2000Hz.
This was mostly due to the fact that the core only triggers a rate change in
clk_set_rate_range()if the current rate is outside of the boundaries, but not if it's within the new boundaries.That series changes that and will trigger a rate change on every call, with the former rate being tried again. This way, providers have a chance to follow whatever policy they see fit for a given clock each time the boundaries change.
Quoted 5 lines
This series also implements some kunit tests, first to test a few rate related functions in the CCF, and then extends it to make sure that behaviour has some test coverage.
Let me know what you think
Thanks. I'm going to apply this to clk-next but not the last two drm patches. That is OK?
Hi,
Quoted 39 lines · Stephen Boyd
Quoting Maxime Ripard (2022-02-25 06:35:22)
Quoted 38 lines
Hi,
This is a follow-up of the discussion here:
https://lore.kernel.org/linux-clk/20210319150355.xzw7ikwdaga2dwhv@gilmour/and here:
https://lore.kernel.org/all/[email protected]/While the initial proposal implemented a new API to temporarily raise and lower clock rates based on consumer workloads, Stephen suggested an alternative approach implemented here.
The main issue that needed to be addressed in our case was that in a situation where we would have multiple calls to
clk_set_rate_range, we would end up with a clock at the maximum of the minimums being set. This would be expected, but the issue was that if one of the users was to relax or drop its requirements, the rate would be left unchanged, even though the ideal rate would have changed.So something like
clk_set_rate(user1_clk, 1000);clk_set_min_rate(user1_clk, 2000);clk_set_min_rate(user2_clk, 3000);clk_set_min_rate(user2_clk, 1000);Would leave the clock running at 3000Hz, while the minimum would now be 2000Hz.
This was mostly due to the fact that the core only triggers a rate change in
clk_set_rate_range()if the current rate is outside of the boundaries, but not if it's within the new boundaries.That series changes that and will trigger a rate change on every call, with the former rate being tried again. This way, providers have a chance to follow whatever policy they see fit for a given clock each time the boundaries change.
Quoted 8 lines
Quoted 5 lines
This series also implements some kunit tests, first to test a few rate related functions in the CCF, and then extends it to make sure that behaviour has some test coverage.
Let me know what you think
Thanks. I'm going to apply this to clk-next but not the last two drm patches. That is OK?
Yes, that will be perfect, thanks!
Maxime
Maxime Ripard
AppliedQuoted 11 lines · Maxime Ripard
This is a follow-up of the discussion here:
https://lore.kernel.org/linux-clk/20210319150355.xzw7ikwdaga2dwhv@gilmour/and here:
https://lore.kernel.org/all/[email protected]/While the initial proposal implemented a new API to temporarily raise and lower clock rates based on consumer workloads, Stephen suggested an alternative approach implemented here.
[...]
Applied to drm/drm-misc (drm-misc-next).
Thanks!
Maxime