Skip to contentSkip to conversation

cmsdk-apb-timer: various bugfixes

PATCH In review Peter Maydell 3 participants 7 messages Jul 3, 2018

lore
Share & tools
lore
#0 lore raw

cover cmsdk-apb-timer: various bugfixes

This patchseries includes Guenter's recent patch to suppress warning messages from the ptimer layer that otherwise occur from the way the Linux driver for this timer device operates.

The other patches here fix more obscure things:
* the timer interrupt is only supposed to trigger when the counter counts down from 1 to 0, not if it is at 0 for some other reason like a direct write of 0 to the VALUE register. Handling this requires adding a new policy option to the ptimer code, which is what patch 1 does.
* we were incorrectly setting the NO_IMMEDIATE_TRIGGER ptimer policy, which meant we would trigger the interrupt one timer clock too late (when we did the timer reload, rather than on the 1-to-0 counter transition)
* if the ptimer had already disabled itself because it was in one-shot mode and had expired, writing to RELOAD or VALUE needs to cause it to start counting again

Tested with a uCLinux/mps2 kernel and buildroot initfs.

thanks
-- PMM

Guenter Roeck (1):
hw/timer/cmsdk-apb-timer: Correctly identify and set one-shot mode

Peter Maydell (3):
ptimer: Add TRIGGER_ONLY_ON_DECREMENT policy option hw/timer/cmsdk-apb-timer: Correct ptimer policy settings hw/timer/cmsdk-apb-timer: run or stop timer on writes to RELOAD and
VALUE

 include/hw/ptimer.h        |  9 +++++++++
 hw/core/ptimer.c           | 22 +++++++++++++++++++++-
 hw/timer/cmsdk-apb-timer.c | 20 ++++++++++++++++++--
 tests/ptimer-test.c        | 25 +++++++++++++++++++------
 4 files changed, 67 insertions(+), 9 deletions(-)
Signature · 1 line
2.17.1
#1 lore raw

PATCH 1/4 ptimer: Add TRIGGER_ONLY_ON_DECREMENT policy option

The CMSDK timer behaviour is that an interrupt is triggered when the counter counts down from 1 to 0; however one is not triggered if the counter is manually set to 0 by a guest write to the counter register. Currently ptimer can't handle this; add a policy option to allow a ptimer user to request this behaviour.

---
 include/hw/ptimer.h |  9 +++++++++
 hw/core/ptimer.c    | 22 +++++++++++++++++++++-
 tests/ptimer-test.c | 25 +++++++++++++++++++------
 3 files changed, 49 insertions(+), 7 deletions(-)
include/hw/ptimer.h +9 -0
index fc4ef5cc1d2..0731d9aef19 100644
@@ -69,6 +69,15 @@
  * not the one less.  */
 #define PTIMER_POLICY_NO_COUNTER_ROUND_DOWN (1 << 4)
 
+/*
+ * Starting to run with a zero counter, or setting the counter to "0" via
+ * ptimer_set_count() or ptimer_set_limit() will not trigger the timer
+ * (though it will cause a reload). Only a counter decrement to "0"
+ * will cause a trigger. Not compatible with NO_IMMEDIATE_TRIGGER;
+ * ptimer_init() will assert() that you don't set both.
+ */
+#define PTIMER_POLICY_TRIGGER_ONLY_ON_DECREMENT (1 << 5)
+
 /* ptimer.c */
 typedef struct ptimer_state ptimer_state;
 typedef void (*ptimer_cb)(void *opaque);
hw/core/ptimer.c +21 -1
index 7221c68a984..170fd34d8b5 100644
@@ -45,8 +45,20 @@ static void ptimer_reload(ptimer_state *s, int delta_adjust)
     uint32_t period_frac = s->period_frac;
     uint64_t period = s->period;
     uint64_t delta = s->delta;
+    bool suppress_trigger = false;
 
-    if (delta == 0 && !(s->policy_mask & PTIMER_POLICY_NO_IMMEDIATE_TRIGGER)) {
+    /*
+     * Note that if delta_adjust is 0 then we must be here because of
+     * a count register write or timer start, not because of timer expiry.
+     * In that case the policy might require us to suppress the timer trigger
+     * that we would otherwise generate for a zero delta.
+     */
+    if (delta_adjust == 0 &&
+        (s->policy_mask & PTIMER_POLICY_TRIGGER_ONLY_ON_DECREMENT)) {
+        suppress_trigger = true;
+    }
+    if (delta == 0 && !(s->policy_mask & PTIMER_POLICY_NO_IMMEDIATE_TRIGGER)
+        && !suppress_trigger) {
         ptimer_trigger(s);
     }
 
@@ -353,6 +365,14 @@ ptimer_state *ptimer_init(QEMUBH *bh, uint8_t policy_mask)
     s->bh = bh;
     s->timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, ptimer_tick, s);
     s->policy_mask = policy_mask;
+
+    /*
+     * These two policies are incompatible -- trigger-on-decrement implies
+     * a timer trigger when the count becomes 0, but no-immediate-trigger
+     * implies a trigger when the count stops being 0.
+     */
+    assert(!((policy_mask & PTIMER_POLICY_TRIGGER_ONLY_ON_DECREMENT) &&
+             (policy_mask & PTIMER_POLICY_NO_IMMEDIATE_TRIGGER)));
     return s;
 }
 
tests/ptimer-test.c +19 -6
index 41488896f76..b30aad07372 100644
@@ -208,6 +208,7 @@ static void check_periodic(gconstpointer arg)
     bool no_immediate_trigger = (*policy & PTIMER_POLICY_NO_IMMEDIATE_TRIGGER);
     bool no_immediate_reload = (*policy & PTIMER_POLICY_NO_IMMEDIATE_RELOAD);
     bool no_round_down = (*policy & PTIMER_POLICY_NO_COUNTER_ROUND_DOWN);
+    bool trig_only_on_dec = (*policy & PTIMER_POLICY_TRIGGER_ONLY_ON_DECREMENT);
 
     triggered = false;
 
@@ -311,7 +312,7 @@ static void check_periodic(gconstpointer arg)
     g_assert_cmpuint(ptimer_get_count(ptimer), ==,
                      no_immediate_reload ? 0 : 10);
 
-    if (no_immediate_trigger) {
+    if (no_immediate_trigger || trig_only_on_dec) {
         g_assert_false(triggered);
     } else {
         g_assert_true(triggered);
@@ -506,6 +507,7 @@ static void check_run_with_delta_0(gconstpointer arg)
     bool no_immediate_trigger = (*policy & PTIMER_POLICY_NO_IMMEDIATE_TRIGGER);
     bool no_immediate_reload = (*policy & PTIMER_POLICY_NO_IMMEDIATE_RELOAD);
     bool no_round_down = (*policy & PTIMER_POLICY_NO_COUNTER_ROUND_DOWN);
+    bool trig_only_on_dec = (*policy & PTIMER_POLICY_TRIGGER_ONLY_ON_DECREMENT);
 
     triggered = false;
 
@@ -515,7 +517,7 @@ static void check_run_with_delta_0(gconstpointer arg)
     g_assert_cmpuint(ptimer_get_count(ptimer), ==,
                      no_immediate_reload ? 0 : 99);
 
-    if (no_immediate_trigger) {
+    if (no_immediate_trigger || trig_only_on_dec) {
         g_assert_false(triggered);
     } else {
         g_assert_true(triggered);
@@ -563,7 +565,7 @@ static void check_run_with_delta_0(gconstpointer arg)
     g_assert_cmpuint(ptimer_get_count(ptimer), ==,
                      no_immediate_reload ? 0 : 99);
 
-    if (no_immediate_trigger) {
+    if (no_immediate_trigger || trig_only_on_dec) {
         g_assert_false(triggered);
     } else {
         g_assert_true(triggered);
@@ -609,6 +611,7 @@ static void check_periodic_with_load_0(gconstpointer arg)
     ptimer_state *ptimer = ptimer_init(bh, *policy);
     bool continuous_trigger = (*policy & PTIMER_POLICY_CONTINUOUS_TRIGGER);
     bool no_immediate_trigger = (*policy & PTIMER_POLICY_NO_IMMEDIATE_TRIGGER);
+    bool trig_only_on_dec = (*policy & PTIMER_POLICY_TRIGGER_ONLY_ON_DECREMENT);
 
     triggered = false;
 
@@ -617,7 +620,7 @@ static void check_periodic_with_load_0(gconstpointer arg)
 
     g_assert_cmpuint(ptimer_get_count(ptimer), ==, 0);
 
-    if (no_immediate_trigger) {
+    if (no_immediate_trigger || trig_only_on_dec) {
         g_assert_false(triggered);
     } else {
         g_assert_true(triggered);
@@ -667,6 +670,7 @@ static void check_oneshot_with_load_0(gconstpointer arg)
     QEMUBH *bh = qemu_bh_new(ptimer_trigger, NULL);
     ptimer_state *ptimer = ptimer_init(bh, *policy);
     bool no_immediate_trigger = (*policy & PTIMER_POLICY_NO_IMMEDIATE_TRIGGER);
+    bool trig_only_on_dec = (*policy & PTIMER_POLICY_TRIGGER_ONLY_ON_DECREMENT);
 
     triggered = false;
 
@@ -675,7 +679,7 @@ static void check_oneshot_with_load_0(gconstpointer arg)
 
     g_assert_cmpuint(ptimer_get_count(ptimer), ==, 0);
 
-    if (no_immediate_trigger) {
+    if (no_immediate_trigger || trig_only_on_dec) {
         g_assert_false(triggered);
     } else {
         g_assert_true(triggered);
@@ -725,6 +729,10 @@ static void add_ptimer_tests(uint8_t policy)
         g_strlcat(policy_name, "no_counter_rounddown,", 256);
     }
 
+    if (policy & PTIMER_POLICY_TRIGGER_ONLY_ON_DECREMENT) {
+        g_strlcat(policy_name, "trigger_only_on_decrement,", 256);
+    }
+
     g_test_add_data_func_full(
         tmp = g_strdup_printf("/ptimer/set_count policy=%s", policy_name),
         g_memdup(&policy, 1), check_set_count, g_free);
@@ -790,10 +798,15 @@ static void add_ptimer_tests(uint8_t policy)
 
 static void add_all_ptimer_policies_comb_tests(void)
 {
-    int last_policy = PTIMER_POLICY_NO_COUNTER_ROUND_DOWN;
+    int last_policy = PTIMER_POLICY_TRIGGER_ONLY_ON_DECREMENT;
     int policy = PTIMER_POLICY_DEFAULT;
 
     for (; policy < (last_policy << 1); policy++) {
+        if ((policy & PTIMER_POLICY_TRIGGER_ONLY_ON_DECREMENT) &&
+            (policy & PTIMER_POLICY_NO_IMMEDIATE_TRIGGER)) {
+            /* Incompatible policy flag settings -- don't try to test them */
+            continue;
+        }
         add_ptimer_tests(policy);
     }
 }
Signature · 1 line
2.17.1
#2 lore raw

PATCH 2/4 hw/timer/cmsdk-apb-timer: Correct ptimer policy settings

In reply to Peter Maydell

The CMSDK timer interrupt triggers when the counter goes from 1 to 0, so we want to trigger immediately, rather than waiting for a clock cycle. Drop the incorrect NO_IMMEDIATE_TRIGGER setting. We also do not want to get an interrupt if the guest sets the counter directly to zero, so use the new TRIGGER_ONLY_ON_DECREMENT policy.

---
 hw/timer/cmsdk-apb-timer.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/timer/cmsdk-apb-timer.c +1 -1
index 9878746609a..1f99081db1a 100644
@@ -201,7 +201,7 @@ static void cmsdk_apb_timer_realize(DeviceState *dev, Error **errp)
     bh = qemu_bh_new(cmsdk_apb_timer_tick, s);
     s->timer = ptimer_init(bh,
                            PTIMER_POLICY_WRAP_AFTER_ONE_PERIOD |
-                           PTIMER_POLICY_NO_IMMEDIATE_TRIGGER |
+                           PTIMER_POLICY_TRIGGER_ONLY_ON_DECREMENT |
                            PTIMER_POLICY_NO_IMMEDIATE_RELOAD |
                            PTIMER_POLICY_NO_COUNTER_ROUND_DOWN);
 
Signature · 1 line
2.17.1
#3 lore raw

PATCH 3/4 hw/timer/cmsdk-apb-timer: Correctly identify and set one-shot mode

In reply to Peter Maydell

From: Guenter Roeck <[email protected]>

The CMSDK APB timer is currently always configured as periodic timer. This results in the following messages when trying to boot Linux.

Timer with delta zero, disabling

If the timer limit set with the RELOAD command is 0, the timer needs to be enabled as one-shot timer.

Message-id: [email protected]

---
 hw/timer/cmsdk-apb-timer.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/timer/cmsdk-apb-timer.c +1 -1
index 1f99081db1a..3ebdc7be408 100644
@@ -119,7 +119,7 @@ static void cmsdk_apb_timer_write(void *opaque, hwaddr offset, uint64_t value,
         }
         s->ctrl = value & 0xf;
         if (s->ctrl & R_CTRL_EN_MASK) {
-            ptimer_run(s->timer, 0);
+            ptimer_run(s->timer, ptimer_get_limit(s->timer) == 0);
         } else {
             ptimer_stop(s->timer);
         }
Signature · 1 line
2.17.1
#4 lore raw

PATCH 4/4 hw/timer/cmsdk-apb-timer: run or stop timer on writes to RELOAD and VALUE

In reply to Peter Maydell

If the CMSDK APB timer is set up with a zero RELOAD value then it will count down to zero, fire once and then stay at zero. From the point of view of the ptimer system, the timer is disabled; but the enable bit in the CTRL register is still set and if the guest subsequently writes to the RELOAD or VALUE registers this should cause the timer to start counting down again.

Add code to the write paths for RELOAD and VALUE so that we correctly restart the timer in this situation.

Conversely, if the new RELOAD and VALUE are both zero, we should stop the ptimer.

---
 hw/timer/cmsdk-apb-timer.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
hw/timer/cmsdk-apb-timer.c +16 -0
index 3ebdc7be408..801d1dba741 100644
@@ -126,10 +126,26 @@ static void cmsdk_apb_timer_write(void *opaque, hwaddr offset, uint64_t value,
         break;
     case A_RELOAD:
         /* Writing to reload also sets the current timer value */
+        if (!value) {
+            ptimer_stop(s->timer);
+        }
         ptimer_set_limit(s->timer, value, 1);
+        if (value && (s->ctrl & R_CTRL_EN_MASK)) {
+            /*
+             * Make sure timer is running (it might have stopped if this
+             * was an expired one-shot timer)
+             */
+            ptimer_run(s->timer, 0);
+        }
         break;
     case A_VALUE:
+        if (!value && !ptimer_get_limit(s->timer)) {
+            ptimer_stop(s->timer);
+        }
         ptimer_set_count(s->timer, value);
+        if (value && (s->ctrl & R_CTRL_EN_MASK)) {
+            ptimer_run(s->timer, ptimer_get_limit(s->timer) == 0);
+        }
         break;
     case A_INTSTATUS:
         /* Just one bit, which is W1C. */
Signature · 1 line
2.17.1
Reviewed-by on the v1 series
#5 lore raw
In reply to Peter Maydell
Quoted 8 lines · Peter Maydell

Guenter Roeck (1):
hw/timer/cmsdk-apb-timer: Correctly identify and set one-shot mode

Peter Maydell (3):
ptimer: Add TRIGGER_ONLY_ON_DECREMENT policy option hw/timer/cmsdk-apb-timer: Correct ptimer policy settings hw/timer/cmsdk-apb-timer: run or stop timer on writes to RELOAD and
VALUE

r~

Tested-by on the v1 series
#6 lore raw
In reply to Peter Maydell
Quoted 18 lines · Peter Maydell

This patchseries includes Guenter's recent patch to suppress warning messages from the ptimer layer that otherwise occur from the way the Linux driver for this timer device operates.

The other patches here fix more obscure things:
* the timer interrupt is only supposed to trigger when the counter counts down from 1 to 0, not if it is at 0 for some other reason like a direct write of 0 to the VALUE register. Handling this requires adding a new policy option to the ptimer code, which is what patch 1 does.
* we were incorrectly setting the NO_IMMEDIATE_TRIGGER ptimer policy, which meant we would trigger the interrupt one timer clock too late (when we did the timer reload, rather than on the 1-to-0 counter transition)
* if the ptimer had already disabled itself because it was in one-shot mode and had expired, writing to RELOAD or VALUE needs to cause it to start counting again

Quoted 2 lines

Tested with a uCLinux/mps2 kernel and buildroot initfs.

For the series:

also with uCLinux/mps2 kernel and buildroot initfs.

Thanks,
Guenter

Quoted 21 lines

thanks
-- PMM

Guenter Roeck (1):
hw/timer/cmsdk-apb-timer: Correctly identify and set one-shot mode

Peter Maydell (3):
ptimer: Add TRIGGER_ONLY_ON_DECREMENT policy option hw/timer/cmsdk-apb-timer: Correct ptimer policy settings hw/timer/cmsdk-apb-timer: run or stop timer on writes to RELOAD and
VALUE

 include/hw/ptimer.h        |  9 +++++++++
 hw/core/ptimer.c           | 22 +++++++++++++++++++++-
 hw/timer/cmsdk-apb-timer.c | 20 ++++++++++++++++++--
 tests/ptimer-test.c        | 25 +++++++++++++++++++------
 4 files changed, 67 insertions(+), 9 deletions(-)
Signature · 1 line
2.17.1