timer/aspeed: fix timer enablement when a reload is not set
PATCH 1/1 timer/aspeed: fix timer enablement when a reload is not set
When a timer is enabled before a reload value is set, the controller waits for a reload value to be set before starting decrementing. This fix tries to cover that case by changing the timer expiry only when a reload value is valid.
--- hw/timer/aspeed_timer.c | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-)
hw/timer/aspeed_timer.c +29 -8
@@ -130,15 +130,26 @@ static uint64_t calculate_next(struct AspeedTimer *t) next = seq[1 ]; }else if (now < seq[2 ]) { next = seq[2 ]; - }else { + }else if (t->reload) { reload_ns =muldiv64 (t->reload,NANOSECONDS_PER_SECOND , rate); t->start = now - ((now - t->start) % reload_ns); + }else { +/* no reload value, return 0 */ +break ; } }return next; } +static void aspeed_timer_mod (AspeedTimer *t) +{ +uint64_t next =calculate_next (t); +if (next) { +timer_mod (&t->timer, next); + } +} +static void aspeed_timer_expire (void *opaque) {AspeedTimer *t = opaque;
@@ -164,7 +175,7 @@ static void aspeed_timer_expire(void *opaque)qemu_set_irq (t->irq, t->level); } -timer_mod (&t->timer,calculate_next (t)); +aspeed_timer_mod (t); }static uint64_t aspeed_timer_get_value (AspeedTimer *t,int reg)
@@ -227,10 +238,23 @@ static void aspeed_timer_set_value(AspeedTimerCtrlState *s, int timer, int reg, uint32_t value) {AspeedTimer *t; +uint32_t old_reload;trace_aspeed_timer_set_value (timer, reg, value); t = &s->timers[timer];switch (reg) { +case TIMER_REG_RELOAD : + old_reload = t->reload; + t->reload = value; + +/* If the reload value was not previously set, or zero, and +* the current value is valid, try to start the timer if it is +* enabled. +*/ +if (old_reload || !t->reload) { +break ; + } +case TIMER_REG_STATUS :if (timer_enabled (t)) {uint64_t now =qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL );
@@ -238,17 +262,14 @@ static void aspeed_timer_set_value(AspeedTimerCtrlState *s, int timer, int reg,uint32_t rate =calculate_rate (t); t->start +=muldiv64 (delta,NANOSECONDS_PER_SECOND , rate); -timer_mod (&t->timer,calculate_next (t)); +aspeed_timer_mod (t); }break ; -case TIMER_REG_RELOAD : - t->reload = value; -break ;case TIMER_REG_MATCH_FIRST :case TIMER_REG_MATCH_SECOND : t->match[reg -2 ] = value;if (timer_enabled (t)) { -timer_mod (&t->timer,calculate_next (t)); +aspeed_timer_mod (t); }break ;default :
@@ -268,7 +289,7 @@ static void aspeed_timer_ctrl_enable(AspeedTimer *t, bool enable)trace_aspeed_timer_ctrl_enable (t->id, enable);if (enable) { t->start =qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL ); -timer_mod (&t->timer,calculate_next (t)); +aspeed_timer_mod (t); }else {timer_del (&t->timer); }
Signature · 1 line
2.7.4
Quoted 63 lines · Cédric Le Goater
When a timer is enabled before a reload value is set, the controller waits for a reload value to be set before starting decrementing. This fix tries to cover that case by changing the timer expiry only when a reload value is valid.
Quoted 1 line
Signed-off-by Cédric Le Goater <[email protected]>---
hw/timer/aspeed_timer.c | 37 +++++++++++++++++++++++++++++--------1 file changed, 29 insertions(+), 8 deletions(-)hw/timer/aspeed_timer.c +4
-1@@ -130,15 +130,26 @@ static uint64_t calculate_next(struct AspeedTimer *t) next = seq[1]; } else if (now < seq[2]) { next = seq[2]; - } else { + } else if (t->reload) { reload_ns = muldiv64(t->reload, NANOSECONDS_PER_SECOND, rate); t->start = now - ((now - t->start) % reload_ns); + } else { + /* no reload value, return 0 */ + break; } }
return next;
}hw/timer/aspeed_timer.c +8
-0+static void aspeed_timer_mod (AspeedTimer *t) +{ + uint64_t next = calculate_next(t); + if (next) { + timer_mod(&t->timer, next); + } +} +static void
aspeed_timer_expire(void *opaque)
{
AspeedTimer *t = opaque;hw/timer/aspeed_timer.c +0
-0@@ -164,7 +175,7 @@ static void aspeed_timer_expire(void *opaque) qemu_set_irq(t->irq, t->level); }
- timer_mod(&t->timer,calculate_next(t));
+ aspeed_timer_mod(t);
}
staticuint64_taspeed_timer_get_value(AspeedTimer *t, int reg)hw/timer/aspeed_timer.c +1
-0@@ -227,10 +238,23 @@ static void aspeed_timer_set_value(AspeedTimerCtrlState *s, int timer, int reg, uint32_t value) { AspeedTimer *t; + uint32_t old_reload;
trace_aspeed_timer_set_value(timer, reg, value); t = &s->timers[timer];
switch (reg) {
+ caseTIMER_REG_RELOAD:
+ old_reload =t->reload;
+ t->reload = value;
Quoted 8 lines
+
+ /* If the reload value was not previously set, or zero, and + * the current value is valid, try to start the timer if it is + * enabled.
+ */
+ if (old_reload|| !t->reload) {
+ break;
+ }
Maybe I need more caffeine, but I initially struggled to reconcile the condition with the comment, as the condition checks the inverse in order to break while the comment discusses the non-breaking case.
However, after trying for several minutes, I'm not sure there's an easy way to improve it.
Quoted 25 lines
+
caseTIMER_REG_STATUS:
if (timer_enabled(t)) {
uint64_t now =qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);hw/timer/aspeed_timer.c +0
-0@@ -238,17 +262,14 @@ static void aspeed_timer_set_value(AspeedTimerCtrlState *s, int timer, int reg, uint32_t rate = calculate_rate(t);
t->start +=muldiv64(delta, NANOSECONDS_PER_SECOND, rate); - timer_mod(&t->timer,calculate_next(t));
+ aspeed_timer_mod(t);
}
break;
- caseTIMER_REG_RELOAD:
- t->reload = value;
- break;
caseTIMER_REG_MATCH_FIRST:
caseTIMER_REG_MATCH_SECOND:
t->match[reg - 2] = value;
if (timer_enabled(t)) {
- timer_mod(&t->timer,calculate_next(t));
+ aspeed_timer_mod(t);
}
break;
default:
hw/timer/aspeed_timer.c static void aspeed_timer_ctrl_enable(AspeedTimer *t, bool enable) · line 295
Quoted 8 lines
hw/timer/aspeed_timer.c +1
-1@@ -268,7 +289,7 @@ static void aspeed_timer_ctrl_enable(AspeedTimer *t, bool enable) trace_aspeed_timer_ctrl_enable(t->id, enable); if (enable) { t->start = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); - timer_mod(&t->timer, calculate_next(t)); + aspeed_timer_mod(t); } else { timer_del(&t->timer); }
Quoted 68 lines · Andrew Jeffery
Quoted 67 lines · Cédric Le Goater
When a timer is enabled before a reload value is set, the controller waits for a reload value to be set before starting decrementing. This fix tries to cover that case by changing the timer expiry only when a reload value is valid.
Quoted 1 line
Signed-off-by Cédric Le Goater <[email protected]>--- hw/timer/aspeed_timer.c | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-)hw/timer/aspeed_timer.c +21
-2@@ -130,15 +130,26 @@ static uint64_t calculate_next(struct AspeedTimer *t) next = seq[1 ]; }else if (now < seq[2 ]) { next = seq[2 ]; - }else { + }else if (t->reload) { reload_ns =muldiv64 (t->reload,NANOSECONDS_PER_SECOND , rate); t->start = now - ((now - t->start) % reload_ns); + }else { +/* no reload value, return 0 */ +break ; } }return next; } +static void aspeed_timer_mod (AspeedTimer *t) +{ +uint64_t next =calculate_next (t); +if (next) { +timer_mod (&t->timer, next); + } +} +static void aspeed_timer_expire (void *opaque) {AspeedTimer *t = opaque;@@ -164,7 +175,7 @@ static void aspeed_timer_expire(void *opaque)qemu_set_irq (t->irq, t->level); } -timer_mod (&t->timer,calculate_next (t)); +aspeed_timer_mod (t); }static uint64_t aspeed_timer_get_value (AspeedTimer *t,int reg)@@ -227,10 +238,23 @@ static void aspeed_timer_set_value(AspeedTimerCtrlState *s, int timer, int reg, uint32_t value) {AspeedTimer *t; +uint32_t old_reload;trace_aspeed_timer_set_value (timer, reg, value); t = &s->timers[timer];switch (reg) { +case TIMER_REG_RELOAD : + old_reload = t->reload; + t->reload = value; + +/* If the reload value was not previously set, or zero, and +* the current value is valid, try to start the timer if it is +* enabled.
Quoted 8 lines
hw/timer/aspeed_timer.c static void aspeed_timer_set_value(AspeedTimerCtrlState *s, int timer, int reg, · line 256
Quoted 4 lines
hw/timer/aspeed_timer.c +4
-0+*/ +if (old_reload || !t->reload) { +break ; + }Maybe I need more caffeine, but I initially struggled to reconcile the condition with the comment, as the condition checks the inverse in order to break while the comment discusses the non-breaking case.
I agree. The reload "value" is used in a hidden way to the activate the timer.
Quoted 2 lines
However, after trying for several minutes, I'm not sure there's an easy way to improve it.
I tried a few things. May be, we could move the following code in its own routine and call it twice ?
Quoted 27 lines
Quoted 27 lines
hw/timer/aspeed_timer.c +3
-5+case TIMER_REG_STATUS :if (timer_enabled (t)) {uint64_t now =qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL );@@ -238,17 +262,14 @@ static void aspeed_timer_set_value(AspeedTimerCtrlState *s, int timer, int reg,uint32_t rate =calculate_rate (t); t->start +=muldiv64 (delta,NANOSECONDS_PER_SECOND , rate); -timer_mod (&t->timer,calculate_next (t)); +aspeed_timer_mod (t); }break ; -case TIMER_REG_RELOAD : - t->reload = value; -break ;case TIMER_REG_MATCH_FIRST :case TIMER_REG_MATCH_SECOND : t->match[reg -2 ] = value;if (timer_enabled (t)) { -timer_mod (&t->timer,calculate_next (t)); +aspeed_timer_mod (t); }break ;default :@@ -268,7 +289,7 @@ static void aspeed_timer_ctrl_enable(AspeedTimer *t, bool enable)trace_aspeed_timer_ctrl_enable (t->id, enable);if (enable) {
Quoted 8 lines
hw/timer/aspeed_timer.c static void aspeed_timer_ctrl_enable(AspeedTimer *t, bool enable) · line 295
Quoted 6 lines
hw/timer/aspeed_timer.c +1
-1t->start =qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL ); -timer_mod (&t->timer,calculate_next (t)); +aspeed_timer_mod (t); }else {timer_del (&t->timer); }Reviewed-by Andrew Jeffery <[email protected]>
Thanks,
C.
Quoted 79 lines · Cédric Le Goater
Quoted 69 lines · Andrew Jeffery
Quoted 68 lines · Cédric Le Goater
When a timer is enabled before a reload value is set, the controller waits for a reload value to be set before starting decrementing. This fix tries to cover that case by changing the timer expiry only when a reload value is valid.
Quoted 1 line
Signed-off-by Cédric Le Goater <[email protected]>---
hw/timer/aspeed_timer.c | 37 +++++++++++++++++++++++++++++--------1 file changed, 29 insertions(+), 8 deletions(-)hw/timer/aspeed_timer.c +4
-1@@ -130,15 +130,26 @@ static uint64_t calculate_next(struct AspeedTimer *t) next = seq[1]; } else if (now < seq[2]) { next = seq[2]; - } else { + } else if (t->reload) { reload_ns = muldiv64(t->reload, NANOSECONDS_PER_SECOND, rate); t->start = now - ((now - t->start) % reload_ns); + } else { + /* no reload value, return 0 */ + break; } }
return next;
}hw/timer/aspeed_timer.c +8
-0+static void aspeed_timer_mod (AspeedTimer *t) +{ + uint64_t next = calculate_next(t); + if (next) { + timer_mod(&t->timer, next); + } +} +static void
aspeed_timer_expire(void *opaque)
{
AspeedTimer *t = opaque;hw/timer/aspeed_timer.c +0
-0@@ -164,7 +175,7 @@ static void aspeed_timer_expire(void *opaque) qemu_set_irq(t->irq, t->level); }
- timer_mod(&t->timer,calculate_next(t));
+ aspeed_timer_mod(t);
}
staticuint64_taspeed_timer_get_value(AspeedTimer *t, int reg)hw/timer/aspeed_timer.c +1
-0@@ -227,10 +238,23 @@ static void aspeed_timer_set_value(AspeedTimerCtrlState *s, int timer, int reg, uint32_t value) { AspeedTimer *t; + uint32_t old_reload;
trace_aspeed_timer_set_value(timer, reg, value);
t = &s->timers[timer];
switch (reg) {
+ caseTIMER_REG_RELOAD:
+ old_reload =t->reload;
+ t->reload = value;
+
+ /* If the reload value was not previously set, or zero, and + * the current value is valid, try to start the timer if it is + * enabled.Quoted 8 lines
Quoted 4 lines
+ */
+ if (old_reload|| !t->reload) {
+ break;
+ }Maybe I need more caffeine, but I initially struggled to reconcile the condition with the comment, as the condition checks the inverse in order to break while the comment discusses the non-breaking case.
Quoted 8 lines
I agree. The reload "value" is used in a hidden way to the activate the timer.
Quoted 2 lines
However, after trying for several minutes, I'm not sure there's an easy way to improve it.
I tried a few things. May be, we could move the following code in its own routine and call it twice ?
I don't think it's necessary. The comment serves as enough warning - it should at least make people think before modifying the code.
Cheers,
Andrew
Quoted 40 lines
Quoted 27 lines
Quoted 27 lines
+
caseTIMER_REG_STATUS:
if (timer_enabled(t)) {
uint64_t now =qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);hw/timer/aspeed_timer.c +0
-0@@ -238,17 +262,14 @@ static void aspeed_timer_set_value(AspeedTimerCtrlState *s, int timer, int reg, uint32_t rate = calculate_rate(t);
t->start +=muldiv64(delta, NANOSECONDS_PER_SECOND, rate); - timer_mod(&t->timer,calculate_next(t));
+ aspeed_timer_mod(t);
}
break;
- caseTIMER_REG_RELOAD:
- t->reload = value;
- break;
caseTIMER_REG_MATCH_FIRST:
caseTIMER_REG_MATCH_SECOND:
t->match[reg - 2] = value;
if (timer_enabled(t)) {
- timer_mod(&t->timer,calculate_next(t));
+ aspeed_timer_mod(t);
}
break;
default:hw/timer/aspeed_timer.c +0
-0@@ -268,7 +289,7 @@ static void aspeed_timer_ctrl_enable(AspeedTimer *t, bool enable) trace_aspeed_timer_ctrl_enable(t->id, enable); if (enable) {Quoted 8 lines
hw/timer/aspeed_timer.c static void aspeed_timer_ctrl_enable(AspeedTimer *t, bool enable) · line 295
Quoted 6 lines
hw/timer/aspeed_timer.c +1
-1t->start = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); - timer_mod(&t->timer, calculate_next(t)); + aspeed_timer_mod(t); } else { timer_del(&t->timer); }Reviewed-by Andrew Jeffery <[email protected]>Thanks,
C.
Quoted 83 lines · Andrew Jeffery
Quoted 82 lines · Cédric Le Goater
Quoted 69 lines · Andrew Jeffery
Quoted 68 lines · Cédric Le Goater
When a timer is enabled before a reload value is set, the controller waits for a reload value to be set before starting decrementing. This fix tries to cover that case by changing the timer expiry only when a reload value is valid.
Quoted 1 line
Signed-off-by Cédric Le Goater <[email protected]>--- hw/timer/aspeed_timer.c | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-)hw/timer/aspeed_timer.c +21
-2@@ -130,15 +130,26 @@ static uint64_t calculate_next(struct AspeedTimer *t) next = seq[1 ]; }else if (now < seq[2 ]) { next = seq[2 ]; - }else { + }else if (t->reload) { reload_ns =muldiv64 (t->reload,NANOSECONDS_PER_SECOND , rate); t->start = now - ((now - t->start) % reload_ns); + }else { +/* no reload value, return 0 */ +break ; } }return next; } +static void aspeed_timer_mod (AspeedTimer *t) +{ +uint64_t next =calculate_next (t); +if (next) { +timer_mod (&t->timer, next); + } +} +static void aspeed_timer_expire (void *opaque) {AspeedTimer *t = opaque;@@ -164,7 +175,7 @@ static void aspeed_timer_expire(void *opaque)qemu_set_irq (t->irq, t->level); } -timer_mod (&t->timer,calculate_next (t)); +aspeed_timer_mod (t); }static uint64_t aspeed_timer_get_value (AspeedTimer *t,int reg)@@ -227,10 +238,23 @@ static void aspeed_timer_set_value(AspeedTimerCtrlState *s, int timer, int reg, uint32_t value) {AspeedTimer *t; +uint32_t old_reload;trace_aspeed_timer_set_value (timer, reg, value); t = &s->timers[timer];switch (reg) { +case TIMER_REG_RELOAD : + old_reload = t->reload; + t->reload = value; + +/* If the reload value was not previously set, or zero, and +* the current value is valid, try to start the timer if it is +* enabled. Quoted 8 lines
hw/timer/aspeed_timer.c static void aspeed_timer_set_value(AspeedTimerCtrlState *s, int timer, int reg, · line 256
Quoted 4 lines
hw/timer/aspeed_timer.c +4
-0+*/ +if (old_reload || !t->reload) { +break ; + }Maybe I need more caffeine, but I initially struggled to reconcile the condition with the comment, as the condition checks the inverse in order to break while the comment discusses the non-breaking case.
I agree. The reload "value" is used in a hidden way to the activate the timer.
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
However, after trying for several minutes, I'm not sure there's an easy way to improve it.
I tried a few things. May be, we could move the following code in its own routine and call it twice ?
I don't think it's necessary. The comment serves as enough warning - it should at least make people think before modifying the code.
OK. Let it be.
Peter,
The Moxa Art timer driver was recently merged into the Faraday FTTMR010 driver and the initial setup is slightly different, it enables the timer before setting the reload value, which breaks the current QEMU model.
Thanks,
C.
Quoted 44 lines
Cheers,
Andrew
Quoted 40 lines
Quoted 28 lines
Quoted 28 lines
hw/timer/aspeed_timer.c +3
-5+case TIMER_REG_STATUS :if (timer_enabled (t)) {uint64_t now =qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL );@@ -238,17 +262,14 @@ static void aspeed_timer_set_value(AspeedTimerCtrlState *s, int timer, int reg,uint32_t rate =calculate_rate (t); t->start +=muldiv64 (delta,NANOSECONDS_PER_SECOND , rate); -timer_mod (&t->timer,calculate_next (t)); +aspeed_timer_mod (t); }break ; -case TIMER_REG_RELOAD : - t->reload = value; -break ;case TIMER_REG_MATCH_FIRST :case TIMER_REG_MATCH_SECOND : t->match[reg -2 ] = value;if (timer_enabled (t)) { -timer_mod (&t->timer,calculate_next (t)); +aspeed_timer_mod (t); }break ;default :@@ -268,7 +289,7 @@ static void aspeed_timer_ctrl_enable(AspeedTimer *t, bool enable)trace_aspeed_timer_ctrl_enable (t->id, enable);if (enable) {Quoted 8 lines
hw/timer/aspeed_timer.c static void aspeed_timer_ctrl_enable(AspeedTimer *t, bool enable) · line 295
Quoted 6 lines
hw/timer/aspeed_timer.c +1
-1t->start =qemu_clock_get_ns (QEMU_CLOCK_VIRTUAL ); -timer_mod (&t->timer,calculate_next (t)); +aspeed_timer_mod (t); }else {timer_del (&t->timer); }Reviewed-by Andrew Jeffery <[email protected]>Thanks,
C.
Peter Maydell
AppliedQuoted 9 lines · Cédric Le Goater
When a timer is enabled before a reload value is set, the controller waits for a reload value to be set before starting decrementing. This fix tries to cover that case by changing the timer expiry only when a reload value is valid.
Signed-off-by Cédric Le Goater <[email protected]>--- hw/timer/aspeed_timer.c | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-)
Applied to target-arm.next, thanks.
-- PMM