Skip to contentSkip to conversation

armv7m_systick: Rewrite to use ptimers

PATCH In review Peter Maydell 4 participants 8 messages Oct 15, 2020 – Oct 26, 2020

lore
Share & tools
lore
#0 lore raw

cover armv7m_systick: Rewrite to use ptimers

This patch series rewrites our implementation of the armv7m systick timer to use ptimers.

The armv7m systick timer is a 24-bit decrementing, wrap-on-zero, clear-on-write counter. Our current implementation has various bugs and dubious workarounds in it (for instance see
https://bugs.launchpad.net/qemu/+bug/1872237).

We have an implementation of a simple decrementing counter and we put a lot of effort into making sure it handles the interesting corner cases (like "spend a cycle at 0 before reloading"), so rather than trying to fix these all over again in systick's hand-rolled countdown code it's much simpler to just rewrite it to use a ptimer.

Unfortunately this is a migration compatibility break, which will affect all M-profile boards.

Among other bugs, this fixes
https://bugs.launchpad.net/qemu/+bug/1872237 : now writes to SYST_CVR when the timer is enabled correctly do nothing; when the timer is enabled via SYST_CSR.ENABLE, the ptimer code will (because of POLICY_NO_IMMEDIATE_RELOAD) arrange that after one timer tick the counter is reloaded from SYST_RVR and then counts down from there, as the architecture requires.

Side note: the trace from the test program in LP1872237 won't look quite like it does on the hardware: under QEMU the "waiting for 1000 ms" debug printing generally reports a SYST_CVR value of 0. This is because QEMU's emulated CPU is comparatively fast and our systick has a hard-wired value of 1MHz for the frequency of the 'external reference clock', which means that execution of the guest code reaches "read SYST_CVR" before the first tick of the timer clock after enabling of the timer (which is where the reload of SYST_CVR from SYST_RVR is required). The exception is the first iteration, where the time QEMU takes to translate the guest code is enough that the timer tick happens before the register read. You can also get the timer tick to win the race by fiddling around with the -icount option (which effectively is slowing down the emulated CPU speed).

Some day we should model both the 'system_clock_scale' (ie the CPU clock frequency) and the 'external reference clock' as QEMU clock source/sinks so that board code can specify the correct reference clock frequency.

Patch 1 is a minor tweak to the ptimer code to suppress a spurious warning message for the "timer callback function disabled the ptimer" case, which the systick timer uses. Patch 2 is the actual conversion.

thanks
-- PMM

Peter Maydell (2):
hw/core/ptimer: Support ptimer being disabled by timer callback hw/timer/armv7m_systick: Rewrite to use ptimers

 include/hw/timer/armv7m_systick.h |   3 +-
 hw/core/ptimer.c                  |   4 +
 hw/timer/armv7m_systick.c         | 124 +++++++++++++-----------------
 3 files changed, 58 insertions(+), 73 deletions(-)
Signature · 1 line
2.20.1
#1 lore raw

PATCH 1/2 hw/core/ptimer: Support ptimer being disabled by timer callback

In ptimer_reload(), we call the callback function provided by the timer device that is using the ptimer. This callback might disable the ptimer. The code mostly handles this correctly, except that we'll still print the warning about "Timer with delta zero, disabling" if the now-disabled timer happened to be set such that it would fire again immediately if it were enabled (eg because the limit/reload value is zero).

Suppress the spurious warning message and the unnecessary repeat-deletion of the underlying timer in this case.

---
 hw/core/ptimer.c | 4 ++++
 1 file changed, 4 insertions(+)
hw/core/ptimer.c +4 -0
index c6d2beb1dac..2aa97cb665c 100644
@@ -117,6 +117,10 @@ static void ptimer_reload(ptimer_state *s, int delta_adjust)
     }
 
     if (delta == 0) {
+        if (s->enabled == 0) {
+            /* trigger callback disabled the timer already */
+            return;
+        }
         if (!qtest_enabled()) {
             fprintf(stderr, "Timer with delta zero, disabling\n");
         }
Signature · 1 line
2.20.1
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] hw/core/ptimer: Support ptimer being disabled by timer callback

Quoted 8 lines · Peter Maydell

In ptimer_reload(), we call the callback function provided by the timer device that is using the ptimer. This callback might disable the ptimer. The code mostly handles this correctly, except that we'll still print the warning about "Timer with delta zero, disabling" if the now-disabled timer happened to be set such that it would fire again immediately if it were enabled (eg because the limit/reload value is zero).

Quoted 7 lines

Suppress the spurious warning message and the unnecessary repeat-deletion of the underlying timer in this case.

---
  hw/core/ptimer.c | 4 ++++
  1 file changed, 4 insertions(+)
#3 lore raw

PATCH 2/2 hw/timer/armv7m_systick: Rewrite to use ptimers

In reply to Peter Maydell

The armv7m systick timer is a 24-bit decrementing, wrap-on-zero, clear-on-write counter. Our current implementation has various bugs and dubious workarounds in it (for instance see
https://bugs.launchpad.net/qemu/+bug/1872237).

We have an implementation of a simple decrementing counter and we put a lot of effort into making sure it handles the interesting corner cases (like "spend a cycle at 0 before reloading") -- ptimer.

Rewrite the systick timer to use a ptimer rather than a raw QEMU timer.

Unfortunately this is a migration compatibility break, which will affect all M-profile boards.

Among other bugs, this fixes
https://bugs.launchpad.net/qemu/+bug/1872237 :
now writes to SYST_CVR when the timer is enabled correctly do nothing; when the timer is enabled via SYST_CSR.ENABLE, the ptimer code will (because of POLICY_NO_IMMEDIATE_RELOAD) arrange that after one timer tick the counter is reloaded from SYST_RVR and then counts down from there, as the architecture requires.

---
 include/hw/timer/armv7m_systick.h |   3 +-
 hw/timer/armv7m_systick.c         | 124 +++++++++++++-----------------
 2 files changed, 54 insertions(+), 73 deletions(-)
include/hw/timer/armv7m_systick.h +2 -1
index 97cb345ddb4..84496faaf96 100644
@@ -14,6 +14,7 @@
 
 #include "hw/sysbus.h"
 #include "qom/object.h"
+#include "hw/ptimer.h"
 
 #define TYPE_SYSTICK "armv7m_systick"
 
@@ -27,7 +28,7 @@ struct SysTickState {
     uint32_t control;
     uint32_t reload;
     int64_t tick;
-    QEMUTimer *timer;
+    ptimer_state *ptimer;
     MemoryRegion iomem;
     qemu_irq irq;
 };
hw/timer/armv7m_systick.c +52 -72

185 lines not on this page · raw message

Signature · 1 line
2.20.1
#4 lore raw

Re: [PATCH 2/2] hw/timer/armv7m_systick: Rewrite to use ptimers

Quoted 5 lines · Peter Maydell

The armv7m systick timer is a 24-bit decrementing, wrap-on-zero, clear-on-write counter. Our current implementation has various bugs and dubious workarounds in it (for instance see
https://bugs.launchpad.net/qemu/+bug/1872237).

...and 10 minutes after sending this I realized I'd forgotten to finish removing the no-longer-needed state struct fields. This should be squashed in:

(quoted diff) +0 -0

b/include/hw/timer/armv7m_systick.h
index 84496faaf96..d57e299fd89 100644

include/hw/timer/armv7m_systick.h +0 -2
@@ -26,8 +26,6 @@ struct SysTickState {
     /*< public >*/
 
     uint32_t control;
-    uint32_t reload;
-    int64_t tick;
     ptimer_state *ptimer;
     MemoryRegion iomem;
     qemu_irq irq;
hw/timer/armv7m_systick.c +0 -1
index 2f192011eb0..5ac3a8a7e81 100644
@@ -219,7 +219,6 @@ static const VMStateDescription vmstate_systick = {
     .minimum_version_id = 2,
     .fields = (VMStateField[]) {
         VMSTATE_UINT32(control, SysTickState),
-        VMSTATE_INT64(tick, SysTickState),
         VMSTATE_PTIMER(ptimer, SysTickState),
         VMSTATE_END_OF_LIST()
     }

thanks
-- PMM

#5 lore raw

Re: [PATCH 2/2] hw/timer/armv7m_systick: Rewrite to use ptimers

Quoted 6 lines · Peter Maydell
Quoted 5 lines · Peter Maydell

The armv7m systick timer is a 24-bit decrementing, wrap-on-zero, clear-on-write counter. Our current implementation has various bugs and dubious workarounds in it (for instance see
https://bugs.launchpad.net/qemu/+bug/1872237).

Nice cleanup :)

Nitpicking we should replace the 24-bit 0xffffff constant by a definition.

Quoted 24 lines

...and 10 minutes after sending this I realized I'd forgotten to finish removing the no-longer-needed state struct fields. This should be squashed in:

include/hw/timer/armv7m_systick.h +0 -2
index 84496faaf96..d57e299fd89 100644
@@ -26,8 +26,6 @@ struct SysTickState {
      /*< public >*/
 
      uint32_t control;
-    uint32_t reload;
-    int64_t tick;
      ptimer_state *ptimer;
      MemoryRegion iomem;
      qemu_irq irq;
hw/timer/armv7m_systick.c +0 -0
index 2f192011eb0..5ac3a8a7e81 100644

hw/timer/armv7m_systick.c static const VMStateDescription vmstate_systick = { · line 224

Quoted 7 lines
hw/timer/armv7m_systick.c +0 -1
@@ -219,7 +219,6 @@ static const VMStateDescription vmstate_systick = {
      .minimum_version_id = 2,
      .fields = (VMStateField[]) {
          VMSTATE_UINT32(control, SysTickState),
-        VMSTATE_INT64(tick, SysTickState),
          VMSTATE_PTIMER(ptimer, SysTickState),
          VMSTATE_END_OF_LIST()
      }

Patch + hunk:

#6 lore raw
In reply to Peter Maydell

Ping for review ?

thanks
-- PMM

Quoted 66 lines · Peter Maydell

This patch series rewrites our implementation of the armv7m systick timer to use ptimers.

The armv7m systick timer is a 24-bit decrementing, wrap-on-zero, clear-on-write counter. Our current implementation has various bugs and dubious workarounds in it (for instance see
https://bugs.launchpad.net/qemu/+bug/1872237).

We have an implementation of a simple decrementing counter and we put a lot of effort into making sure it handles the interesting corner cases (like "spend a cycle at 0 before reloading"), so rather than trying to fix these all over again in systick's hand-rolled countdown code it's much simpler to just rewrite it to use a ptimer.

Unfortunately this is a migration compatibility break, which will affect all M-profile boards.

Among other bugs, this fixes
https://bugs.launchpad.net/qemu/+bug/1872237 : now writes to SYST_CVR when the timer is enabled correctly do nothing; when the timer is enabled via SYST_CSR.ENABLE, the ptimer code will (because of POLICY_NO_IMMEDIATE_RELOAD) arrange that after one timer tick the counter is reloaded from SYST_RVR and then counts down from there, as the architecture requires.

Side note: the trace from the test program in LP1872237 won't look quite like it does on the hardware: under QEMU the "waiting for 1000 ms" debug printing generally reports a SYST_CVR value of 0. This is because QEMU's emulated CPU is comparatively fast and our systick has a hard-wired value of 1MHz for the frequency of the 'external reference clock', which means that execution of the guest code reaches "read SYST_CVR" before the first tick of the timer clock after enabling of the timer (which is where the reload of SYST_CVR from SYST_RVR is required). The exception is the first iteration, where the time QEMU takes to translate the guest code is enough that the timer tick happens before the register read. You can also get the timer tick to win the race by fiddling around with the -icount option (which effectively is slowing down the emulated CPU speed).

Some day we should model both the 'system_clock_scale' (ie the CPU clock frequency) and the 'external reference clock' as QEMU clock source/sinks so that board code can specify the correct reference clock frequency.

Patch 1 is a minor tweak to the ptimer code to suppress a spurious warning message for the "timer callback function disabled the ptimer" case, which the systick timer uses. Patch 2 is the actual conversion.

thanks
-- PMM

Peter Maydell (2):
hw/core/ptimer: Support ptimer being disabled by timer callback hw/timer/armv7m_systick: Rewrite to use ptimers

 include/hw/timer/armv7m_systick.h |   3 +-
 hw/core/ptimer.c                  |   4 +
 hw/timer/armv7m_systick.c         | 124 +++++++++++++-----------------
 3 files changed, 58 insertions(+), 73 deletions(-)

--
2.20.1

#7 lore raw
Quoted 3 lines · Peter Maydell

Ping for review ?

Maybe nobody can review this, anyway, is that possible add a test case for this?
I found https://github.com/oxidecomputer/qemu-systick-bug are simple enough.

Quoted 4 lines

thanks
-- PMM

On Thu, 15 Oct 2020 at 16:18, Peter Maydell <[email protected]>

Quoted 58 lines · wrote:
Quoted 58 lines

This patch series rewrites our implementation of the armv7m systick timer to use ptimers.

The armv7m systick timer is a 24-bit decrementing, wrap-on-zero, clear-on-write counter. Our current implementation has various bugs and dubious workarounds in it (for instance see
https://bugs.launchpad.net/qemu/+bug/1872237).

We have an implementation of a simple decrementing counter and we put a lot of effort into making sure it handles the interesting corner cases (like "spend a cycle at 0 before reloading"), so rather than trying to fix these all over again in systick's hand-rolled countdown code it's much simpler to just rewrite it to use a ptimer.

Unfortunately this is a migration compatibility break, which will affect all M-profile boards.

Among other bugs, this fixes
https://bugs.launchpad.net/qemu/+bug/1872237 : now writes to SYST_CVR when the timer is enabled correctly do nothing; when the timer is enabled via SYST_CSR.ENABLE, the ptimer code will (because of POLICY_NO_IMMEDIATE_RELOAD) arrange that after one timer tick the counter is reloaded from SYST_RVR and then counts down from there, as the architecture requires.

Side note: the trace from the test program in LP1872237 won't look quite like it does on the hardware: under QEMU the "waiting for 1000 ms" debug printing generally reports a SYST_CVR value of 0. This is because QEMU's emulated CPU is comparatively fast and our systick has a hard-wired value of 1MHz for the frequency of the 'external reference clock', which means that execution of the guest code reaches "read SYST_CVR" before the first tick of the timer clock after enabling of the timer (which is where the reload of SYST_CVR from SYST_RVR is required). The exception is the first iteration, where the time QEMU takes to translate the guest code is enough that the timer tick happens before the register read. You can also get the timer tick to win the race by fiddling around with the -icount option (which effectively is slowing down the emulated CPU speed).

Some day we should model both the 'system_clock_scale' (ie the CPU clock frequency) and the 'external reference clock' as QEMU clock source/sinks so that board code can specify the correct reference clock frequency.

Patch 1 is a minor tweak to the ptimer code to suppress a spurious warning message for the "timer callback function disabled the ptimer" case, which the systick timer uses. Patch 2 is the actual conversion.

thanks
-- PMM

Peter Maydell (2):
hw/core/ptimer: Support ptimer being disabled by timer callback hw/timer/armv7m_systick: Rewrite to use ptimers

Quoted 9 lines
Quoted 8 lines
 include/hw/timer/armv7m_systick.h |   3 +-
 hw/core/ptimer.c                  |   4 +
 hw/timer/armv7m_systick.c         | 124 +++++++++++++-----------------
 3 files changed, 58 insertions(+), 73 deletions(-)

--
2.20.1

--
此致
礼
罗勇刚
Yours
sincerely,
Yonggang Luo