Skip to contentSkip to conversation

hw/clock: Propagate clock changes when STM32L4X5 MUX is updated

PATCH Superseded Philippe Mathieu-Daudé 3 participants 8 messages Mar 22, 2024 – Mar 25, 2024 Arm STM32

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover hw/clock: Propagate clock changes when STM32L4X5 MUX is updated

Per https://www.qemu.org/docs/master/devel/clocks.html#clock-multiplier-and-divider-settings:

Note that clock_set_mul_div() does not automatically call clock_propagate(). If you make a runtime change to the multiplier or divider you must call clock_propagate() yourself.

Fix what we forgot to do that in recent commit ec7d83acbd ("hw/misc/stm32l4x5_rcc: Add an internal clock multiplexer object")

Arnaud Minier (1):
hw/misc/stm32l4x5_rcc: Propagate period when enabling a clock

Philippe Mathieu-Daudé (1):
hw/clock: Let clock_set_mul_div() return boolean value

 include/hw/clock.h      | 4 +++-
 hw/core/clock.c         | 8 +++++++-
 hw/misc/stm32l4x5_rcc.c | 5 ++++-
 3 files changed, 14 insertions(+), 3 deletions(-)
Signature · 1 line
2.41.0
#1 lore raw

PATCH 1/2 hw/clock: Let clock_set_mul_div() return boolean value

Let clock_set_mul_div() return a boolean value whether the clock has been updated or not, similarly to clock_set().

---
 include/hw/clock.h | 4 +++-
 hw/core/clock.c    | 8 +++++++-
 2 files changed, 10 insertions(+), 2 deletions(-)
include/hw/clock.h +3 -1
index bb12117f67..eb58599131 100644
@@ -357,6 +357,8 @@ char *clock_display_freq(Clock *clk);
  * @multiplier: multiplier value
  * @divider: divider value
  *
+ * @return: true if the clock is changed.
+ *
  * By default, a Clock's children will all run with the same period
  * as their parent. This function allows you to adjust the multiplier
  * and divider used to derive the child clock frequency.
@@ -374,6 +376,6 @@ char *clock_display_freq(Clock *clk);
  * Note that this function does not call clock_propagate(); the
  * caller should do that if necessary.
  */
-void clock_set_mul_div(Clock *clk, uint32_t multiplier, uint32_t divider);
+bool clock_set_mul_div(Clock *clk, uint32_t multiplier, uint32_t divider);
 
 #endif /* QEMU_HW_CLOCK_H */
hw/core/clock.c +7 -1
index d82e44cd1a..a19c7db7df 100644
@@ -143,14 +143,20 @@ char *clock_display_freq(Clock *clk)
     return freq_to_str(clock_get_hz(clk));
 }
 
-void clock_set_mul_div(Clock *clk, uint32_t multiplier, uint32_t divider)
+bool clock_set_mul_div(Clock *clk, uint32_t multiplier, uint32_t divider)
 {
     assert(divider != 0);
 
+    if (clk->multiplier == multiplier && clk->divider == divider) {
+        return false;
+    }
+
     trace_clock_set_mul_div(CLOCK_PATH(clk), clk->multiplier, multiplier,
                             clk->divider, divider);
     clk->multiplier = multiplier;
     clk->divider = divider;
+
+    return true;
 }
 
 static void clock_initfn(Object *obj)
Signature · 1 line
2.41.0
#2 lore raw

Re: [PATCH-for-9.0 1/2] hw/clock: Let clock_set_mul_div() return boolean value

Quoted 9 lines · Philippe Mathieu-Daudé

Let clock_set_mul_div() return a boolean value whether the clock has been updated or not, similarly to clock_set().

---
 include/hw/clock.h | 4 +++-
 hw/core/clock.c    | 8 +++++++-
 2 files changed, 10 insertions(+), 2 deletions(-)

I guess this makes sense, especially since you often would like to know this to decide whether to call clock_propagate().

We should also update the docs/devel/clocks.rst to mention this.

thanks
-- PMM

Acked-by on v1 1/2
#3 lore raw

Re: [PATCH-for-9.0 1/2] hw/clock: Let clock_set_mul_div() return boolean value

In reply to Philippe Mathieu-Daudé
Quoted 4 lines · Philippe Mathieu-Daudé

Let clock_set_mul_div() return a boolean value whether the clock has been updated or not, similarly to clock_set().

Quoted 55 lines
---
 include/hw/clock.h | 4 +++-
 hw/core/clock.c    | 8 +++++++-
 2 files changed, 10 insertions(+), 2 deletions(-)
include/hw/clock.h +3 -1
index bb12117f67..eb58599131 100644
@@ -357,6 +357,8 @@ char *clock_display_freq(Clock *clk);
  * @multiplier: multiplier value
  * @divider: divider value
  *
+ * @return: true if the clock is changed.
+ *
  * By default, a Clock's children will all run with the same period
  * as their parent. This function allows you to adjust the multiplier
  * and divider used to derive the child clock frequency.
@@ -374,6 +376,6 @@ char *clock_display_freq(Clock *clk);
  * Note that this function does not call clock_propagate(); the
  * caller should do that if necessary.
  */
-void clock_set_mul_div(Clock *clk, uint32_t multiplier, uint32_t divider);
+bool clock_set_mul_div(Clock *clk, uint32_t multiplier, uint32_t divider);
 
 #endif /* QEMU_HW_CLOCK_H */
hw/core/clock.c +7 -1
index d82e44cd1a..a19c7db7df 100644
@@ -143,14 +143,20 @@ char *clock_display_freq(Clock *clk)
     return freq_to_str(clock_get_hz(clk));
 }
 
-void clock_set_mul_div(Clock *clk, uint32_t multiplier, uint32_t divider)
+bool clock_set_mul_div(Clock *clk, uint32_t multiplier, uint32_t divider)
 {
     assert(divider != 0);
 
+    if (clk->multiplier == multiplier && clk->divider == divider) {
+        return false;
+    }
+
     trace_clock_set_mul_div(CLOCK_PATH(clk), clk->multiplier, multiplier,
                             clk->divider, divider);
     clk->multiplier = multiplier;
     clk->divider = divider;
+
+    return true;
 }
 
 static void clock_initfn(Object *obj)
Signature · 1 line
2.41.0
#4 lore raw

Re: [PATCH-for-9.0 1/2] hw/clock: Let clock_set_mul_div() return boolean value

Quoted 7 lines · Luc Michel
Quoted 4 lines · Philippe Mathieu-Daudé

Let clock_set_mul_div() return a boolean value whether the clock has been updated or not, similarly to clock_set().

Sorry, I forgot, as Peter suggested, can you add a word in the doc about this?

Something in the vein of:

+ Similary to ``clock_set()``, ``clock_set_mul_div()`` returns ``true`` if
+ the clock state was modified, that it, if the multiplier or the diviser
+ or both were changed by the call.
+
Note that ``clock_set_mul_div()`` does not automatically
call ``clock_propagate()``. If you make a runtime change to the multiplier or divider you must call clock_propagate() yourself.

Thanks!

Signature · 1 line
Luc
Quoted 57 lines
Quoted 56 lines
---
 include/hw/clock.h | 4 +++-
 hw/core/clock.c    | 8 +++++++-
 2 files changed, 10 insertions(+), 2 deletions(-)
include/hw/clock.h +3 -1
index bb12117f67..eb58599131 100644
@@ -357,6 +357,8 @@ char *clock_display_freq(Clock *clk);
  * @multiplier: multiplier value
  * @divider: divider value
  *
+ * @return: true if the clock is changed.
+ *
  * By default, a Clock's children will all run with the same period
  * as their parent. This function allows you to adjust the multiplier
  * and divider used to derive the child clock frequency.
@@ -374,6 +376,6 @@ char *clock_display_freq(Clock *clk);
  * Note that this function does not call clock_propagate(); the
  * caller should do that if necessary.
  */
-void clock_set_mul_div(Clock *clk, uint32_t multiplier, uint32_t divider);
+bool clock_set_mul_div(Clock *clk, uint32_t multiplier, uint32_t divider);
 
 #endif /* QEMU_HW_CLOCK_H */
hw/core/clock.c +7 -1
index d82e44cd1a..a19c7db7df 100644
@@ -143,14 +143,20 @@ char *clock_display_freq(Clock *clk)
     return freq_to_str(clock_get_hz(clk));
 }
 
-void clock_set_mul_div(Clock *clk, uint32_t multiplier, uint32_t divider)
+bool clock_set_mul_div(Clock *clk, uint32_t multiplier, uint32_t divider)
 {
     assert(divider != 0);
 
+    if (clk->multiplier == multiplier && clk->divider == divider) {
+        return false;
+    }
+
     trace_clock_set_mul_div(CLOCK_PATH(clk), clk->multiplier, multiplier,
                             clk->divider, divider);
     clk->multiplier = multiplier;
     clk->divider = divider;
+
+    return true;
 }
 
 static void clock_initfn(Object *obj)
Signature · 1 line
2.41.0
#5 lore raw

PATCH 2/2 hw/misc/stm32l4x5_rcc: Propagate period when enabling a clock

In reply to Philippe Mathieu-Daudé

From: Arnaud Minier <[email protected]>

The "clock_set_mul_div" function doesn't propagate the clock period to the children if it is changed (e.g. by enabling/disabling a clock multiplexer).
This was overlooked during the implementation due to late changes.

This commit propagates the change if the multiplier or divider changes.

Fixes ec7d83acbd ("hw/misc/stm32l4x5_rcc: Add an internal clock multiplexer object")

Message-ID: <[email protected]>

[PMD: Check clock_set_mul_div() return value]
---
 hw/misc/stm32l4x5_rcc.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
hw/misc/stm32l4x5_rcc.c +4 -1
index bc2d63528b..7ad628b296 100644
@@ -59,7 +59,10 @@ static void clock_mux_update(RccClockMuxState *mux, bool bypass_source)
         freq_multiplier = mux->divider;
     }
 
-    clock_set_mul_div(mux->out, freq_multiplier, mux->multiplier);
+    if (clock_set_mul_div(mux->out, freq_multiplier, mux->multiplier)) {
+        clock_propagate(mux->out);
+    }
+
     clock_update(mux->out, clock_get(current_source));
 
     src_freq = clock_get_hz(current_source);
Signature · 1 line
2.41.0
#6 lore raw

Re: [PATCH-for-9.0 2/2] hw/misc/stm32l4x5_rcc: Propagate period when enabling a clock

Quoted 25 lines · Philippe Mathieu-Daudé

From: Arnaud Minier <[email protected]>

The "clock_set_mul_div" function doesn't propagate the clock period to the children if it is changed (e.g. by enabling/disabling a clock multiplexer).
This was overlooked during the implementation due to late changes.

This commit propagates the change if the multiplier or divider changes.

Fixes ec7d83acbd ("hw/misc/stm32l4x5_rcc: Add an internal clock multiplexer object")

Message-ID: <[email protected]>

[PMD: Check clock_set_mul_div() return value]
---
 hw/misc/stm32l4x5_rcc.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
hw/misc/stm32l4x5_rcc.c +0 -0
index bc2d63528b..7ad628b296 100644

hw/misc/stm32l4x5_rcc.c static void clock_mux_update(RccClockMuxState *mux, bool bypass_source) · line 66

Quoted 9 lines
hw/misc/stm32l4x5_rcc.c +4 -1
@@ -59,7 +59,10 @@ static void clock_mux_update(RccClockMuxState *mux, bool bypass_source)
         freq_multiplier = mux->divider;
     }
 
-    clock_set_mul_div(mux->out, freq_multiplier, mux->multiplier);
+    if (clock_set_mul_div(mux->out, freq_multiplier, mux->multiplier)) {
+        clock_propagate(mux->out);
+    }
+
     clock_update(mux->out, clock_get(current_source));

clock_update() also calls clock_propagate(), so this doesn't seem entirely right: shouldn't we figure out whether we need to do a clock_propagate() and do it once? (Maybe what seems odd to me is that clock_set() does clock_propagate() for you but clock_set_mul_div() does not...)

(Also I think we should have the information we need now to be able to do the "reduce log spam" in the comment -- if neither clock_set_mul_div() nor clock_update() needed to do anything then we didn't actually change the config.)

-- PMM

#7 lore raw

Re: [PATCH-for-9.0 2/2] hw/misc/stm32l4x5_rcc: Propagate period when enabling a clock

Quoted 29 lines · Peter Maydell
Quoted 28 lines · Philippe Mathieu-Daudé

From: Arnaud Minier <[email protected]>

The "clock_set_mul_div" function doesn't propagate the clock period to the children if it is changed (e.g. by enabling/disabling a clock multiplexer).
This was overlooked during the implementation due to late changes.

This commit propagates the change if the multiplier or divider changes.

Fixes ec7d83acbd ("hw/misc/stm32l4x5_rcc: Add an internal clock multiplexer object")

Message-ID: <[email protected]>

[PMD: Check clock_set_mul_div() return value]
---
 hw/misc/stm32l4x5_rcc.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
hw/misc/stm32l4x5_rcc.c +0 -0
index bc2d63528b..7ad628b296 100644
@@ -59,7 +59,10 @@ static void clock_mux_update(RccClockMuxState *mux, bool bypass_source)
         freq_multiplier = mux->divider;
     }
 
Quoted 12 lines

hw/misc/stm32l4x5_rcc.c static void clock_mux_update(RccClockMuxState *mux, bool bypass_source) · line 66

Quoted 6 lines
hw/misc/stm32l4x5_rcc.c +4 -1
-    clock_set_mul_div(mux->out, freq_multiplier, mux->multiplier);
+    if (clock_set_mul_div(mux->out, freq_multiplier, mux->multiplier)) {
+        clock_propagate(mux->out);
+    }
+
     clock_update(mux->out, clock_get(current_source));

clock_update() also calls clock_propagate(), so this doesn't seem entirely right: shouldn't we figure out whether we need to do a clock_propagate() and do it once? (Maybe what seems odd to me is that clock_set() does clock_propagate() for you but clock_set_mul_div() does not...)

clock_set() does not call clock_propagate(). clock_update() is a clock_set() followed by a clock_propagate() if the period changed.

I think this is where the problem comes from here. clock_update() call won't call clock_propagate() if the clock period does not change.

I think you'll want something like:

    bool changed;

    changed = clock_set_mul_div(mux->out, freq_multiplier, mux->multiplexer);
    changed ||= clock_set(clock_get(current_source));

    if (changed) {
        clock_propagate(mux->out);
    }

Thanks,

Signature · 1 line
Luc
Quoted 7 lines

(Also I think we should have the information we need now to be able to do the "reduce log spam" in the comment -- if neither clock_set_mul_div() nor clock_update() needed to do anything then we didn't actually change the config.)

-- PMM