clk: do not trust cached rates for disabled clocks
PATCH 1/1 clk: do not trust cached rates for disabled clocks
From: Randolph Sapp <[email protected]>
Recalculate the clock rate for unprepared clocks. This cached value can vary depending on the clocking architecture. On platforms with clocks that have shared management it's possible that:
- Previously disabled clocks have been enabled by other entities
- Rates calculated during clock tree initialization could have changed
---
I'm hoping this will start a bit of a discussion. I'm still curious why people would want to read the rate of an unprepared clock, but there were so many logged operations on my test platforms that I assumed it must have some purpose.
Either way, I don't believe cached values should ever be trusted in this scenario.
drivers/clk/clk.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-)
drivers/clk/clk.c +10 -2
@@ -1971,8 +1971,16 @@ static void __clk_recalc_rates(struct clk_core *core, bool update_req,static unsigned long clk_core_get_rate_recalc (struct clk_core *core) { -if (core && (core->flags &CLK_GET_RATE_NOCACHE )) -__clk_recalc_rates (core, false,0 ); +if (core) { +bool prepared =clk_core_is_prepared (core); + +if (core->flags &CLK_GET_RATE_NOCACHE || !prepared) { +if (!prepared) +pr_debug ("%s: rate requested for unprepared clock %s\n" , + __func__, core->name); +__clk_recalc_rates (core, false,0 ); + } + }return clk_core_get_rate_nolock (core); }
Signature · 1 line
2.51.0
Quoted 10 lines · rs
From: Randolph Sapp <[email protected]>
Recalculate the clock rate for unprepared clocks. This cached value can vary depending on the clocking architecture. On platforms with clocks that have shared management it's possible that:
- Previously disabled clocks have been enabled by other entities
- Rates calculated during clock tree initialization could have changedSigned-off-by Randolph Sapp <[email protected]>
Quoted 8 lines
---
I'm hoping this will start a bit of a discussion. I'm still curious why people would want to read the rate of an unprepared clock, but there were so many logged operations on my test platforms that I assumed it must have some purpose.
Either way, I don't believe cached values should ever be trusted in this scenario.
Ignoring the typo in the subject (s/disabled/unprepared/), was there any feedback regarding this patch?
Quoted 26 lines
drivers/clk/clk.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-)drivers/clk/clk.c +10
-2@@ -1971,8 +1971,16 @@ static void __clk_recalc_rates(struct clk_core *core, bool update_req,static unsigned long clk_core_get_rate_recalc (struct clk_core *core) { -if (core && (core->flags &CLK_GET_RATE_NOCACHE )) -__clk_recalc_rates (core, false,0 ); +if (core) { +bool prepared =clk_core_is_prepared (core); + +if (core->flags &CLK_GET_RATE_NOCACHE || !prepared) { +if (!prepared) +pr_debug ("%s: rate requested for unprepared clock %s\n" , + __func__, core->name); +__clk_recalc_rates (core, false,0 ); + } + }return clk_core_get_rate_nolock (core); }
Hi,
Quoted 32 lines · rs
From: Randolph Sapp <[email protected]>
Recalculate the clock rate for unprepared clocks. This cached value can vary depending on the clocking architecture. On platforms with clocks that have shared management it's possible that:
- Previously disabled clocks have been enabled by other entities
- Rates calculated during clock tree initialization could have changedSigned-off-by Randolph Sapp <[email protected]>---
I'm hoping this will start a bit of a discussion. I'm still curious why people would want to read the rate of an unprepared clock, but there were so many logged operations on my test platforms that I assumed it must have some purpose.
Either way, I don't believe cached values should ever be trusted in this scenario.
drivers/clk/clk.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-)drivers/clk/clk.c +0
-2@@ -1971,8 +1971,16 @@ static void __clk_recalc_rates(struct clk_core *core, bool update_req,static unsigned long clk_core_get_rate_recalc (struct clk_core *core) { -if (core && (core->flags &CLK_GET_RATE_NOCACHE )) -__clk_recalc_rates (core, false,0 );
drivers/clk/clk.c static void __clk_recalc_rates(struct clk_core *core, bool update_req, · line 1983
Quoted 10 lines
drivers/clk/clk.c +10
-0+if (core) { +bool prepared =clk_core_is_prepared (core); + +if (core->flags &CLK_GET_RATE_NOCACHE || !prepared) { +if (!prepared) +pr_debug ("%s: rate requested for unprepared clock %s\n" , + __func__, core->name); +__clk_recalc_rates (core, false,0 ); + } + }
I'm not sure this patch is correct. In case the clock is not prepared, the rate is still cached in __clk_recalc_rates(). Thus, I'd expect the following sequence to return a wrong rate:
# assuming clock is unprepared and will return 0 clk_get_rate() // this will fetch the (wrong) rate and cache it clk_prepare_enable() clk_get_rate() // this will then return the cached rate
Or do I miss something here?
-michael
Quoted 3 lines
drivers/clk/clk.c +0
-0return clk_core_get_rate_nolock (core); }
Quoted 50 lines · Michael Walle
Hi,
Quoted 32 lines · rs
From: Randolph Sapp <[email protected]>
Recalculate the clock rate for unprepared clocks. This cached value can vary depending on the clocking architecture. On platforms with clocks that have shared management it's possible that:
- Previously disabled clocks have been enabled by other entities
- Rates calculated during clock tree initialization could have changedSigned-off-by Randolph Sapp <[email protected]>---
I'm hoping this will start a bit of a discussion. I'm still curious why people would want to read the rate of an unprepared clock, but there were so many logged operations on my test platforms that I assumed it must have some purpose.
Either way, I don't believe cached values should ever be trusted in this scenario.
drivers/clk/clk.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-)drivers/clk/clk.c +0
-2@@ -1971,8 +1971,16 @@ static void __clk_recalc_rates(struct clk_core *core, bool update_req,static unsigned long clk_core_get_rate_recalc (struct clk_core *core) { -if (core && (core->flags &CLK_GET_RATE_NOCACHE )) -__clk_recalc_rates (core, false,0 );drivers/clk/clk.c static void __clk_recalc_rates(struct clk_core *core, bool update_req, · line 1983
Quoted 10 lines
drivers/clk/clk.c +10
-0+if (core) { +bool prepared =clk_core_is_prepared (core); + +if (core->flags &CLK_GET_RATE_NOCACHE || !prepared) { +if (!prepared) +pr_debug ("%s: rate requested for unprepared clock %s\n" , + __func__, core->name); +__clk_recalc_rates (core, false,0 ); + } + }I'm not sure this patch is correct. In case the clock is not prepared, the rate is still cached in
__clk_recalc_rates(). Thus, I'd expect the following sequence to return a wrong rate:
Quoted 8 lines
# assuming clock is unprepared and will return 0 clk_get_rate() // this will fetch the (wrong) rate and cache it clk_prepare_enable() clk_get_rate() // this will then return the cached rateOr do I miss something here?
-michael
Ah, that's a really good point. I guess since my test is only looking at instances where people are doing a clk_get_rate (occurring outside of driver), clk_prepare_enable, and clk_set_rate it works right now. Need to adjust that.
Am I correct in my assumption about running clk_get_rate on unprepared clocks though? (That it shouldn't be allowed or, if it is, that the result shouldn't be cached.)
- Randolph
Quoted 3 lines
Quoted 3 lines
drivers/clk/clk.c +0
-0return clk_core_get_rate_nolock (core); }
Quoted 64 lines · Randolph Sapp
Quoted 50 lines · Michael Walle
Hi,
Quoted 32 lines · rs
From: Randolph Sapp <[email protected]>
Recalculate the clock rate for unprepared clocks. This cached value can vary depending on the clocking architecture. On platforms with clocks that have shared management it's possible that:
- Previously disabled clocks have been enabled by other entities
- Rates calculated during clock tree initialization could have changedSigned-off-by Randolph Sapp <[email protected]>---
I'm hoping this will start a bit of a discussion. I'm still curious why people would want to read the rate of an unprepared clock, but there were so many logged operations on my test platforms that I assumed it must have some purpose.
Either way, I don't believe cached values should ever be trusted in this scenario.
drivers/clk/clk.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-)drivers/clk/clk.c +0
-2@@ -1971,8 +1971,16 @@ static void __clk_recalc_rates(struct clk_core *core, bool update_req,static unsigned long clk_core_get_rate_recalc (struct clk_core *core) { -if (core && (core->flags &CLK_GET_RATE_NOCACHE )) -__clk_recalc_rates (core, false,0 );drivers/clk/clk.c static void __clk_recalc_rates(struct clk_core *core, bool update_req, · line 1983
Quoted 10 lines
drivers/clk/clk.c +10
-0+if (core) { +bool prepared =clk_core_is_prepared (core); + +if (core->flags &CLK_GET_RATE_NOCACHE || !prepared) { +if (!prepared) +pr_debug ("%s: rate requested for unprepared clock %s\n" , + __func__, core->name); +__clk_recalc_rates (core, false,0 ); + } + }I'm not sure this patch is correct. In case the clock is not prepared, the rate is still cached in
__clk_recalc_rates(). Thus, I'd expect the following sequence to return a wrong rate:Quoted 8 lines
# assuming clock is unprepared and will return 0 clk_get_rate() // this will fetch the (wrong) rate and cache it clk_prepare_enable() clk_get_rate() // this will then return the cached rateOr do I miss something here?
-michael
Ah, that's a really good point. I guess since my test is only looking at instances where people are doing a
clk_get_rate(occurring outside of driver),clk_prepare_enable, andclk_set_rateit works right now. Need to adjust that.
Quoted 5 lines
Am I correct in my assumption about running
clk_get_rateon unprepared clocks though? (That it shouldn't be allowed or, if it is, that the result shouldn't be cached.)- Randolph
Any follow up to this Michael? I wanted to be sure this was something the subsystem should allow before I look into further workarounds.
- Randolph
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
drivers/clk/clk.c +0
-0return clk_core_get_rate_nolock (core); }
Hi,
Quoted 6 lines
Quoted 4 lines
Am I correct in my assumption about running
clk_get_rateon unprepared clocks though? (That it shouldn't be allowed or, if it is, that the result shouldn't be cached.)Any follow up to this Michael? I wanted to be sure this was something the subsystem should allow before I look into further workarounds.
I don't know. I'm not that familar with the ccs. My first reaction was that it's asymmetrical that a .set is allowed (and works for tisci) and that .get is not allowed. That way you can't read the hardware clock (or think of a fixed clock, where you want to get the frequency) before enabling it. I could imagine some devices which needs to be configured first, before you might turn the clock on.
OTOH Maxime pointed out the comment in the kdoc of clk_get_rate() which clearly states that it's only valid if the clock is on.
-michael
Quoted 16 lines · Michael Walle
Hi,
Quoted 6 lines
Quoted 4 lines
Am I correct in my assumption about running
clk_get_rateon unprepared clocks though? (That it shouldn't be allowed or, if it is, that the result shouldn't be cached.)Any follow up to this Michael? I wanted to be sure this was something the subsystem should allow before I look into further workarounds.
I don't know. I'm not that familar with the ccs. My first reaction was that it's asymmetrical that a .set is allowed (and works for tisci) and that .get is not allowed. That way you can't read the hardware clock (or think of a fixed clock, where you want to get the frequency) before enabling it. I could imagine some devices which needs to be configured first, before you might turn the clock on.
Quoted 4 lines
OTOH Maxime pointed out the comment in the kdoc of
clk_get_rate()which clearly states that it's only valid if the clock is on.-michael
Yeah, I still don't like the way we handle clock in firmware but I've already been shut down on that front.
Regardless, there are quite a few drivers right now that use clk_get_rate prior to preparing the clock. If the kdoc reports clk_get_rate is only valid if the clock is enabled, should we report a proper warning when this occurs?
- Randolph
Quoted 17 lines · Randolph Sapp
Quoted 16 lines · Michael Walle
Hi,
Quoted 6 lines
Quoted 4 lines
Am I correct in my assumption about running
clk_get_rateon unprepared clocks though? (That it shouldn't be allowed or, if it is, that the result shouldn't be cached.)Any follow up to this Michael? I wanted to be sure this was something the subsystem should allow before I look into further workarounds.
I don't know. I'm not that familar with the ccs. My first reaction was that it's asymmetrical that a .set is allowed (and works for tisci) and that .get is not allowed. That way you can't read the hardware clock (or think of a fixed clock, where you want to get the frequency) before enabling it. I could imagine some devices which needs to be configured first, before you might turn the clock on.
Quoted 7 lines
Quoted 4 lines
OTOH Maxime pointed out the comment in the kdoc of
clk_get_rate()which clearly states that it's only valid if the clock is on.-michael
Yeah, I still don't like the way we handle clock in firmware but I've already been shut down on that front.
TBC, I was talking about the CCS set/get API in general.
-michael
Quoted 5 lines
Regardless, there are quite a few drivers right now that use
clk_get_rateprior to preparing the clock. If the kdoc reportsclk_get_rateis only valid if the clock is enabled, should we report a proper warning when this occurs?- Randolph
Quoted 20 lines · Randolph Sapp
Quoted 19 lines · Michael Walle
Hi,
Quoted 6 lines
Quoted 4 lines
Am I correct in my assumption about running
clk_get_rateon unprepared clocks though? (That it shouldn't be allowed or, if it is, that the result shouldn't be cached.)Any follow up to this Michael? I wanted to be sure this was something the subsystem should allow before I look into further workarounds.
I don't know. I'm not that familar with the ccs. My first reaction was that it's asymmetrical that a .set is allowed (and works for tisci) and that .get is not allowed. That way you can't read the hardware clock (or think of a fixed clock, where you want to get the frequency) before enabling it. I could imagine some devices which needs to be configured first, before you might turn the clock on.
OTOH Maxime pointed out the comment in the kdoc of
clk_get_rate()which clearly states that it's only valid if the clock is on.
Quoted 8 lines
Quoted 1 line
-michael
Yeah, I still don't like the way we handle clock in firmware but I've already been shut down on that front.
Regardless, there are quite a few drivers right now that use
clk_get_rateprior to preparing the clock. If the kdoc reportsclk_get_rateis only valid if the clock is enabled, should we report a proper warning when this occurs?
It's more complicated than that.
The clock API documentation mentions that, and the CCF is one implementation of that API. It's now the dominant implementation, but the CCF itself never mentioned or required it.
And thus drivers started to rely on the CCF behaviour.
Plus, leaving the documentation part aside, being able to call clk_get_rate when the clock is disabled has value.
How can you setup a clock before enabling it to avoid any frequency change while the device operates otherwise?
Or how do you make sure the clock is in its operating range and thus the device *can* operate?
There's a reason people have started using it. And it might be abstracted away by the firmware in some cases, but not all platforms use a firmware, so you can't rely on that either.
Maxime
Quoted 36 lines · Maxime Ripard
Quoted 20 lines · Randolph Sapp
Quoted 19 lines · Michael Walle
Hi,
Quoted 6 lines
Quoted 4 lines
Am I correct in my assumption about running
clk_get_rateon unprepared clocks though? (That it shouldn't be allowed or, if it is, that the result shouldn't be cached.)Any follow up to this Michael? I wanted to be sure this was something the subsystem should allow before I look into further workarounds.
I don't know. I'm not that familar with the ccs. My first reaction was that it's asymmetrical that a .set is allowed (and works for tisci) and that .get is not allowed. That way you can't read the hardware clock (or think of a fixed clock, where you want to get the frequency) before enabling it. I could imagine some devices which needs to be configured first, before you might turn the clock on.
OTOH Maxime pointed out the comment in the kdoc of
clk_get_rate()which clearly states that it's only valid if the clock is on.Quoted 8 lines
Quoted 1 line
-michael
Yeah, I still don't like the way we handle clock in firmware but I've already been shut down on that front.
Regardless, there are quite a few drivers right now that use
clk_get_rateprior to preparing the clock. If the kdoc reportsclk_get_rateis only valid if the clock is enabled, should we report a proper warning when this occurs?It's more complicated than that.
The clock API documentation mentions that, and the CCF is one implementation of that API. It's now the dominant implementation, but the CCF itself never mentioned or required it.
Quoted 7 lines
And thus drivers started to rely on the CCF behaviour.
Plus, leaving the documentation part aside, being able to call
clk_get_ratewhen the clock is disabled has value.How can you setup a clock before enabling it to avoid any frequency change while the device operates otherwise?
Why would enabling a clock change it's rate unless the current rate wasn't actually valid? I can only see a change explicitly occurring if the clock parent has decided that the associated rate was not acceptable.
If some device always resets the rate when enabled, isn't that already problematic?
Quoted 2 lines
Or how do you make sure the clock is in its operating range and thus the device *can* operate?
Well, if enabling a clock doesn't change it's rate there's nothing stopping people from enabling the clock prior to getting the rate.
Quoted 5 lines
There's a reason people have started using it. And it might be abstracted away by the firmware in some cases, but not all platforms use a firmware, so you can't rely on that either.
Maxime
Thanks for taking the time to talk with me about this. I assume there is some specific thing that violates my understanding of how this should work, but I feel like things are too loosely defined as is.
- Randolph
Quoted 39 lines · Randolph Sapp
Quoted 38 lines · Maxime Ripard
Quoted 20 lines · Randolph Sapp
Quoted 19 lines · Michael Walle
Hi,
Quoted 6 lines
Quoted 4 lines
Am I correct in my assumption about running
clk_get_rateon unprepared clocks though? (That it shouldn't be allowed or, if it is, that the result shouldn't be cached.)Any follow up to this Michael? I wanted to be sure this was something the subsystem should allow before I look into further workarounds.
I don't know. I'm not that familar with the ccs. My first reaction was that it's asymmetrical that a .set is allowed (and works for tisci) and that .get is not allowed. That way you can't read the hardware clock (or think of a fixed clock, where you want to get the frequency) before enabling it. I could imagine some devices which needs to be configured first, before you might turn the clock on.
OTOH Maxime pointed out the comment in the kdoc of
clk_get_rate()which clearly states that it's only valid if the clock is on.Quoted 8 lines
Quoted 1 line
-michael
Yeah, I still don't like the way we handle clock in firmware but I've already been shut down on that front.
Regardless, there are quite a few drivers right now that use
clk_get_rateprior to preparing the clock. If the kdoc reportsclk_get_rateis only valid if the clock is enabled, should we report a proper warning when this occurs?It's more complicated than that.
The clock API documentation mentions that, and the CCF is one implementation of that API. It's now the dominant implementation, but the CCF itself never mentioned or required it.
And thus drivers started to rely on the CCF behaviour.
Quoted 8 lines
Quoted 5 lines
Plus, leaving the documentation part aside, being able to call
clk_get_ratewhen the clock is disabled has value.How can you setup a clock before enabling it to avoid any frequency change while the device operates otherwise?
Why would enabling a clock change it's rate unless the current rate wasn't actually valid?
That's not what I said, and it's also not something that enable is allowed to do anyway.
Consider this: the clock feeding a hardware component is out of its operating range. You enable it. The device gets stuck. How do you recover from that?
Quoted 5 lines
I can only see a change explicitly occurring if the clock parent has decided that the associated rate was not acceptable.
If some device always resets the rate when enabled, isn't that already problematic?
Resets the rate to what?
Quoted 5 lines
Quoted 2 lines
Or how do you make sure the clock is in its operating range and thus the device *can* operate?
Well, if enabling a clock doesn't change it's rate there's nothing stopping people from enabling the clock prior to getting the rate.
The first thing clk_set_rate is doing is a clk_get_rate. If you want to actually enforce a rule that hasn't been applied for 15y, go ahead, but that means also mandating that you can only make clk_set_rate calls once the clock has been enabled.
Quoted 7 lines
Quoted 3 lines
There's a reason people have started using it. And it might be abstracted away by the firmware in some cases, but not all platforms use a firmware, so you can't rely on that either.
Thanks for taking the time to talk with me about this. I assume there is some specific thing that violates my understanding of how this should work, but I feel like things are too loosely defined as is.
I mean, I kind of agree, but also, the clock framework is newer and has been more liberal than the clock API. And the clock framework is by far the dominant implementation now, so I'm not sure what the big deal is.
Maxime
Quoted 56 lines · Maxime Ripard
Quoted 39 lines · Randolph Sapp
Quoted 38 lines · Maxime Ripard
Quoted 20 lines · Randolph Sapp
Quoted 19 lines · Michael Walle
Hi,
Quoted 6 lines
Quoted 4 lines
Am I correct in my assumption about running
clk_get_rateon unprepared clocks though? (That it shouldn't be allowed or, if it is, that the result shouldn't be cached.)Any follow up to this Michael? I wanted to be sure this was something the subsystem should allow before I look into further workarounds.
I don't know. I'm not that familar with the ccs. My first reaction was that it's asymmetrical that a .set is allowed (and works for tisci) and that .get is not allowed. That way you can't read the hardware clock (or think of a fixed clock, where you want to get the frequency) before enabling it. I could imagine some devices which needs to be configured first, before you might turn the clock on.
OTOH Maxime pointed out the comment in the kdoc of
clk_get_rate()which clearly states that it's only valid if the clock is on.Quoted 8 lines
Quoted 1 line
-michael
Yeah, I still don't like the way we handle clock in firmware but I've already been shut down on that front.
Regardless, there are quite a few drivers right now that use
clk_get_rateprior to preparing the clock. If the kdoc reportsclk_get_rateis only valid if the clock is enabled, should we report a proper warning when this occurs?It's more complicated than that.
The clock API documentation mentions that, and the CCF is one implementation of that API. It's now the dominant implementation, but the CCF itself never mentioned or required it.
And thus drivers started to rely on the CCF behaviour.
Quoted 8 lines
Quoted 5 lines
Plus, leaving the documentation part aside, being able to call
clk_get_ratewhen the clock is disabled has value.How can you setup a clock before enabling it to avoid any frequency change while the device operates otherwise?
Why would enabling a clock change it's rate unless the current rate wasn't actually valid?
That's not what I said, and it's also not something that enable is allowed to do anyway.
Consider this: the clock feeding a hardware component is out of its operating range. You enable it. The device gets stuck. How do you recover from that?
Quoted 7 lines
Quoted 5 lines
I can only see a change explicitly occurring if the clock parent has decided that the associated rate was not acceptable.
If some device always resets the rate when enabled, isn't that already problematic?
Resets the rate to what?
Doesn't matter. The initial comment was about the rate changing when the clock was enabled. I said reset here because any change on power on would assume some default state it was returning to.
Quoted 10 lines
Quoted 5 lines
Quoted 2 lines
Or how do you make sure the clock is in its operating range and thus the device *can* operate?
Well, if enabling a clock doesn't change it's rate there's nothing stopping people from enabling the clock prior to getting the rate.
The first thing
clk_set_rateis doing is aclk_get_rate. If you want to actually enforce a rule that hasn't been applied for 15y, go ahead, but that means also mandating that you can only makeclk_set_ratecalls once the clock has been enabled.
The clk_set_rate only runs clk_get_rate to see if any change needs to occur. If the clock isn't enabled then this is likely part of startup or resume. The likelihood of needing to change the rate in this scenario would be high anyway.
Quoted 8 lines
Quoted 7 lines
Quoted 3 lines
There's a reason people have started using it. And it might be abstracted away by the firmware in some cases, but not all platforms use a firmware, so you can't rely on that either.
Thanks for taking the time to talk with me about this. I assume there is some specific thing that violates my understanding of how this should work, but I feel like things are too loosely defined as is.
Quoted 5 lines
I mean, I kind of agree, but also, the clock framework is newer and has been more liberal than the clock API. And the clock framework is by far the dominant implementation now, so I'm not sure what the big deal is.
Maxime
There's no big deal, I was curious if there was a consensus around clock initialization that could push this fix in a different direction.
Considering there doesn't seem to be any consensus I'm fine with moving forward with the initial cached rate workaround. I just need to rework it a bit to cover the initial corner case outlined.
- Randolph
Quoted 96 lines · Randolph Sapp
Quoted 56 lines · Maxime Ripard
Quoted 39 lines · Randolph Sapp
Quoted 38 lines · Maxime Ripard
Quoted 20 lines · Randolph Sapp
Quoted 19 lines · Michael Walle
Hi,
Quoted 6 lines
Quoted 4 lines
Am I correct in my assumption about running
clk_get_rateon unprepared clocks though? (That it shouldn't be allowed or, if it is, that the result shouldn't be cached.)Any follow up to this Michael? I wanted to be sure this was something the subsystem should allow before I look into further workarounds.
I don't know. I'm not that familar with the ccs. My first reaction was that it's asymmetrical that a .set is allowed (and works for tisci) and that .get is not allowed. That way you can't read the hardware clock (or think of a fixed clock, where you want to get the frequency) before enabling it. I could imagine some devices which needs to be configured first, before you might turn the clock on.
OTOH Maxime pointed out the comment in the kdoc of
clk_get_rate()which clearly states that it's only valid if the clock is on.Quoted 8 lines
Quoted 1 line
-michael
Yeah, I still don't like the way we handle clock in firmware but I've already been shut down on that front.
Regardless, there are quite a few drivers right now that use
clk_get_rateprior to preparing the clock. If the kdoc reportsclk_get_rateis only valid if the clock is enabled, should we report a proper warning when this occurs?It's more complicated than that.
The clock API documentation mentions that, and the CCF is one implementation of that API. It's now the dominant implementation, but the CCF itself never mentioned or required it.
And thus drivers started to rely on the CCF behaviour.
Quoted 8 lines
Quoted 5 lines
Plus, leaving the documentation part aside, being able to call
clk_get_ratewhen the clock is disabled has value.How can you setup a clock before enabling it to avoid any frequency change while the device operates otherwise?
Why would enabling a clock change it's rate unless the current rate wasn't actually valid?
That's not what I said, and it's also not something that enable is allowed to do anyway.
Consider this: the clock feeding a hardware component is out of its operating range. You enable it. The device gets stuck. How do you recover from that?
Quoted 7 lines
Quoted 5 lines
I can only see a change explicitly occurring if the clock parent has decided that the associated rate was not acceptable.
If some device always resets the rate when enabled, isn't that already problematic?
Resets the rate to what?
Doesn't matter. The initial comment was about the rate changing when the clock was enabled. I said reset here because any change on power on would assume some default state it was returning to.
Quoted 10 lines
Quoted 5 lines
Quoted 2 lines
Or how do you make sure the clock is in its operating range and thus the device *can* operate?
Well, if enabling a clock doesn't change it's rate there's nothing stopping people from enabling the clock prior to getting the rate.
The first thing
clk_set_rateis doing is aclk_get_rate. If you want to actually enforce a rule that hasn't been applied for 15y, go ahead, but that means also mandating that you can only makeclk_set_ratecalls once the clock has been enabled.The
clk_set_rateonly runsclk_get_rateto see if any change needs to occur. If the clock isn't enabled then this is likely part of startup or resume. The likelihood of needing to change the rate in this scenario would be high anyway.Quoted 11 lines
Quoted 7 lines
Quoted 3 lines
There's a reason people have started using it. And it might be abstracted away by the firmware in some cases, but not all platforms use a firmware, so you can't rely on that either.
Thanks for taking the time to talk with me about this. I assume there is some specific thing that violates my understanding of how this should work, but I feel like things are too loosely defined as is.
I mean, I kind of agree, but also, the clock framework is newer and has been more liberal than the clock API. And the clock framework is by far the dominant implementation now, so I'm not sure what the big deal is.
Quoted 6 lines
There's no big deal, I was curious if there was a consensus around clock initialization that could push this fix in a different direction.
Considering there doesn't seem to be any consensus I'm fine with moving forward with the initial cached rate workaround. I just need to rework it a bit to cover the initial corner case outlined.
Don't get me wrong, I would like some clarification as well, but I guess what I was trying to say is that clk_get_rate is such a mess when it comes to what it's allowed to return or not already than trying to clean up when it's allowed to be called is going to be a nightmare and you'll break tons of platforms in the process.
Last time I tried, I ended up in a philosophical discussion with another platform maintainer that went nowhere, and we ended up reverting the patches.
So unless you get a maintainer that steps in and say what needs to be done, and enforces it, my advice is you should workaround the issue.
Maxime
Quoted 110 lines · Maxime Ripard
Quoted 96 lines · Randolph Sapp
Quoted 56 lines · Maxime Ripard
Quoted 39 lines · Randolph Sapp
Quoted 38 lines · Maxime Ripard
Quoted 20 lines · Randolph Sapp
Quoted 19 lines · Michael Walle
Hi,
Quoted 6 lines
Quoted 4 lines
Am I correct in my assumption about running
clk_get_rateon unprepared clocks though? (That it shouldn't be allowed or, if it is, that the result shouldn't be cached.)Any follow up to this Michael? I wanted to be sure this was something the subsystem should allow before I look into further workarounds.
I don't know. I'm not that familar with the ccs. My first reaction was that it's asymmetrical that a .set is allowed (and works for tisci) and that .get is not allowed. That way you can't read the hardware clock (or think of a fixed clock, where you want to get the frequency) before enabling it. I could imagine some devices which needs to be configured first, before you might turn the clock on.
OTOH Maxime pointed out the comment in the kdoc of
clk_get_rate()which clearly states that it's only valid if the clock is on.Quoted 8 lines
Quoted 1 line
-michael
Yeah, I still don't like the way we handle clock in firmware but I've already been shut down on that front.
Regardless, there are quite a few drivers right now that use
clk_get_rateprior to preparing the clock. If the kdoc reportsclk_get_rateis only valid if the clock is enabled, should we report a proper warning when this occurs?It's more complicated than that.
The clock API documentation mentions that, and the CCF is one implementation of that API. It's now the dominant implementation, but the CCF itself never mentioned or required it.
And thus drivers started to rely on the CCF behaviour.
Quoted 8 lines
Quoted 5 lines
Plus, leaving the documentation part aside, being able to call
clk_get_ratewhen the clock is disabled has value.How can you setup a clock before enabling it to avoid any frequency change while the device operates otherwise?
Why would enabling a clock change it's rate unless the current rate wasn't actually valid?
That's not what I said, and it's also not something that enable is allowed to do anyway.
Consider this: the clock feeding a hardware component is out of its operating range. You enable it. The device gets stuck. How do you recover from that?
Quoted 7 lines
Quoted 5 lines
I can only see a change explicitly occurring if the clock parent has decided that the associated rate was not acceptable.
If some device always resets the rate when enabled, isn't that already problematic?
Resets the rate to what?
Doesn't matter. The initial comment was about the rate changing when the clock was enabled. I said reset here because any change on power on would assume some default state it was returning to.
Quoted 10 lines
Quoted 5 lines
Quoted 2 lines
Or how do you make sure the clock is in its operating range and thus the device *can* operate?
Well, if enabling a clock doesn't change it's rate there's nothing stopping people from enabling the clock prior to getting the rate.
The first thing
clk_set_rateis doing is aclk_get_rate. If you want to actually enforce a rule that hasn't been applied for 15y, go ahead, but that means also mandating that you can only makeclk_set_ratecalls once the clock has been enabled.The
clk_set_rateonly runsclk_get_rateto see if any change needs to occur. If the clock isn't enabled then this is likely part of startup or resume. The likelihood of needing to change the rate in this scenario would be high anyway.Quoted 11 lines
Quoted 7 lines
Quoted 3 lines
There's a reason people have started using it. And it might be abstracted away by the firmware in some cases, but not all platforms use a firmware, so you can't rely on that either.
Thanks for taking the time to talk with me about this. I assume there is some specific thing that violates my understanding of how this should work, but I feel like things are too loosely defined as is.
I mean, I kind of agree, but also, the clock framework is newer and has been more liberal than the clock API. And the clock framework is by far the dominant implementation now, so I'm not sure what the big deal is.
Quoted 6 lines
There's no big deal, I was curious if there was a consensus around clock initialization that could push this fix in a different direction.
Considering there doesn't seem to be any consensus I'm fine with moving forward with the initial cached rate workaround. I just need to rework it a bit to cover the initial corner case outlined.
Don't get me wrong, I would like some clarification as well, but I guess what I was trying to say is that
clk_get_rateis such a mess when it comes to what it's allowed to return or not already than trying to clean up when it's allowed to be called is going to be a nightmare and you'll break tons of platforms in the process.
Quoted 8 lines
Last time I tried, I ended up in a philosophical discussion with another platform maintainer that went nowhere, and we ended up reverting the patches.
So unless you get a maintainer that steps in and say what needs to be done, and enforces it, my advice is you should workaround the issue.
Maxime
That was my conclusion as well. If we purposefully aren't enforcing some *suggestions* of the common clock framework and our clocks don't play by the current rules then we need to reevaluate.
Kicked all involved parties on our end again. We'll see what the consensus is. I have a feeling we're going to have to implement some horribly hacky selective caching solution to make everyone happy. I say horribly hacky because it will still have requirements about the ordering of particular calls that will need to be manually enforced in all corresponding drivers.
Well, this will be fun.
- Randolph