Skip to contentSkip to conversation

arm_mptimer: Fix timer shutdown

PATCH Superseded Dmitry Osipenko 2 participants 11 messages Jul 1, 2015 – Jul 3, 2015

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 arm_mptimer: Fix timer shutdown

Timer, running in periodic mode, can't be stopped or coming one-shot tick won't be canceled because timer control code just doesn't handle timer disabling. Fix it by checking enable bit and deleting timer if bit isn't set.

---
 hw/timer/arm_mptimer.c | 3 +++
 1 file changed, 3 insertions(+)
hw/timer/arm_mptimer.c +3 -0
index 8b93b3c..917a10f 100644
@@ -127,6 +127,9 @@ static void timerblock_write(void *opaque, hwaddr addr,
                 tb->count = tb->load;
             }
             timerblock_reload(tb, 1);
+        } else if (!(value & 1)) {
+            /* Shutdown timer.  */
+            timer_del(tb->timer);
         }
         break;
     case 12: /* Interrupt status.  */
Signature · 1 line
2.4.4
#1 lore raw
Quoted 14 lines · Dmitry Osipenko

Timer, running in periodic mode, can't be stopped or coming one-shot tick won't be canceled because timer control code just doesn't handle timer disabling. Fix it by checking enable bit and deleting timer if bit isn't set.

---
 hw/timer/arm_mptimer.c | 3 +++
 1 file changed, 3 insertions(+)
hw/timer/arm_mptimer.c +0 -0
index 8b93b3c..917a10f 100644

hw/timer/arm_mptimer.c static void timerblock_write(void *opaque, hwaddr addr, · line 135

Quoted 9 lines
hw/timer/arm_mptimer.c +3 -0
@@ -127,6 +127,9 @@ static void timerblock_write(void *opaque, hwaddr addr,
                 tb->count = tb->load;
             }
             timerblock_reload(tb, 1);
+        } else if (!(value & 1)) {
+            /* Shutdown timer.  */
+            timer_del(tb->timer);
         }
         break;
     case 12: /* Interrupt status.  */

Thanks; this does look like a bug. This change will mean we call timer_del() even if the timer was already disabled, though, so I think it would be slightly better to rearrange the existing logic something like this:

  if ((old & 1) != (value & 1)) {
     if (value & 1) {
         if (tb->count == 0 && (tb->control & 2)) {
             tb->count = tb->load;
         }
         timerblock_reload(tb, 1);
     } else {
         timer_del(tb->timer);
     }
  }

thanks
-- PMM

#2 lore raw

Hello Peter, thanks a lot for comment.

02.07.2015 12:27, Peter Maydell пишет:

Quoted 16 lines

Thanks; this does look like a bug. This change will mean we call timer_del() even if the timer was already disabled, though, so I think it would be slightly better to rearrange the existing logic something like this:

   if ((old & 1) != (value & 1)) {
      if (value & 1) {
          if (tb->count == 0 && (tb->control & 2)) {
              tb->count = tb->load;
          }
          timerblock_reload(tb, 1);
      } else {
          timer_del(tb->timer);
      }
   }
Quoted 3 lines

thanks
-- PMM

Calling timer_del() twice is safe, but I agree that it would be better to avoid it. I'll send V2.

Signature · 1 line
Dmitry
#3 lore raw

PATCH v2 1/1 arm_mptimer: Fix timer shutdown

Timer, running in periodic mode, can't be stopped or coming one-shot tick won't be canceled because timer control code just doesn't handle timer disabling. Fix it by deleting timer if enable bit isn't set.

---

v2: Avoid calling timer_del() if the timer was already disabled as per
Peter Maydell suggestion.

 hw/timer/arm_mptimer.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
hw/timer/arm_mptimer.c +7 -1
index 8b93b3c..51c18de 100644
@@ -122,11 +122,17 @@ static void timerblock_write(void *opaque, hwaddr addr,
     case 8: /* Control.  */
         old = tb->control;
         tb->control = value;
-        if (((old & 1) == 0) && (value & 1)) {
+        if (((old & 1) == 0) && ((value & 1) == 0)) {
+            break;
+        }
+        if (value & 1) {
             if (tb->count == 0 && (tb->control & 2)) {
                 tb->count = tb->load;
             }
             timerblock_reload(tb, 1);
+        } else {
+            /* Shutdown timer.  */
+            timer_del(tb->timer);
         }
         break;
     case 12: /* Interrupt status.  */
Signature · 1 line
2.4.4
#4 lore raw
Quoted 23 lines · Dmitry Osipenko

Timer, running in periodic mode, can't be stopped or coming one-shot tick won't be canceled because timer control code just doesn't handle timer disabling. Fix it by deleting timer if enable bit isn't set.

---

v2: Avoid calling timer_del() if the timer was already disabled as per
Peter Maydell suggestion.

 hw/timer/arm_mptimer.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
hw/timer/arm_mptimer.c +1 -1
index 8b93b3c..51c18de 100644
@@ -122,11 +122,17 @@ static void timerblock_write(void *opaque, hwaddr addr,
     case 8: /* Control.  */
         old = tb->control;
         tb->control = value;
-        if (((old & 1) == 0) && (value & 1)) {
+        if (((old & 1) == 0) && ((value & 1) == 0)) {

hw/timer/arm_mptimer.c static void timerblock_write(void *opaque, hwaddr addr, · line 135

Quoted 10 lines
hw/timer/arm_mptimer.c +6 -0
+            break;
+        }
+        if (value & 1) {
             if (tb->count == 0 && (tb->control & 2)) {
                 tb->count = tb->load;
             }
             timerblock_reload(tb, 1);
+        } else {
+            /* Shutdown timer.  */
+            timer_del(tb->timer);

This will now cause us to do the "reload the timer" logic if you write a 1 to the control bit when it was already 1, which we didn't do before.

The logic I suggested in my previous review comment gets this right...

-- PMM

#5 lore raw

02.07.2015 20:34, Peter Maydell пишет:

Quoted 40 lines · Dmitry Osipenko
Quoted 23 lines

Timer, running in periodic mode, can't be stopped or coming one-shot tick won't be canceled because timer control code just doesn't handle timer disabling. Fix it by deleting timer if enable bit isn't set.

---

v2: Avoid calling timer_del() if the timer was already disabled as per
Peter Maydell suggestion.

  hw/timer/arm_mptimer.c | 8 +++++++-
  1 file changed, 7 insertions(+), 1 deletion(-)
hw/timer/arm_mptimer.c +1 -1
index 8b93b3c..51c18de 100644
@@ -122,11 +122,17 @@ static void timerblock_write(void *opaque, hwaddr addr,
      case 8: /* Control.  */
          old = tb->control;
          tb->control = value;
-        if (((old & 1) == 0) && (value & 1)) {
+        if (((old & 1) == 0) && ((value & 1) == 0)) {

hw/timer/arm_mptimer.c static void timerblock_write(void *opaque, hwaddr addr, · line 135

Quoted 10 lines
hw/timer/arm_mptimer.c +6 -0
+            break;
+        }
+        if (value & 1) {
              if (tb->count == 0 && (tb->control & 2)) {
                  tb->count = tb->load;
              }
              timerblock_reload(tb, 1);
+        } else {
+            /* Shutdown timer.  */
+            timer_del(tb->timer);

This will now cause us to do the "reload the timer" logic if you write a 1 to the control bit when it was already 1, which we didn't do before.

Quoted 5 lines

The logic I suggested in my previous review comment gets this right...

-- PMM

Yep, you right. I overlooked it, let me try again.

Signature · 1 line
Dmitry
#6 lore raw
In reply to Peter Maydell

02.07.2015 20:34, Peter Maydell пишет:

Quoted 10 lines

This will now cause us to do the "reload the timer" logic if you write a 1 to the control bit when it was already 1, which we didn't do before.

The logic I suggested in my previous review comment gets this right...

-- PMM

The problem with code you suggested is that won't start periodic count after one-shot tick was completed.

Signature · 1 line
Dmitry
#7 lore raw
Quoted 7 lines · Dmitry Osipenko

02.07.2015 20:34, Peter Maydell пишет:

Quoted 6 lines

This will now cause us to do the "reload the timer" logic if you write a 1 to the control bit when it was already 1, which we didn't do before.

Quoted 8 lines
Quoted 5 lines

The logic I suggested in my previous review comment gets this right...

-- PMM

The problem with code you suggested is that won't start periodic count after one-shot tick was completed.

Can you give more detail? This code is only for when the guest writes to the control register, so it doesn't get run when a one-shot tick completes.

In any case, the code currently in master does:
old value new value action

      0           0          nothing
      0           1          reload timer
      1           0          nothing
      1           1          nothing

Your first patch did:

old value new value action

      0           0          delete timer
      0           1          reload timer
      1           0          delete timer
      1           1          nothing

Your second patch does:

old value new value action

      0           0          nothing
      0           1          reload timer
      1           0          delete timer
      1           1          reload timer

My suggestion was:

old value new value action

      0           0          nothing
      0           1          reload timer
      1           0          delete timer
      1           1          nothing

If you think that's wrong, then surely your first patch also has the same problem?

thanks
-- PMM

#8 lore raw

02.07.2015 21:09, Peter Maydell пишет:

Quoted 52 lines · Dmitry Osipenko
Quoted 7 lines

02.07.2015 20:34, Peter Maydell пишет:

Quoted 6 lines

This will now cause us to do the "reload the timer" logic if you write a 1 to the control bit when it was already 1, which we didn't do before.

Quoted 8 lines
Quoted 5 lines

The logic I suggested in my previous review comment gets this right...

-- PMM

The problem with code you suggested is that won't start periodic count after one-shot tick was completed.

Can you give more detail? This code is only for when the guest writes to the control register, so it doesn't get run when a one-shot tick completes.

In any case, the code currently in master does:
old value new value action

       0           0          nothing
       0           1          reload timer
       1           0          nothing
       1           1          nothing

Your first patch did:

old value new value action

       0           0          delete timer
       0           1          reload timer
       1           0          delete timer
       1           1          nothing

Your second patch does:

old value new value action

       0           0          nothing
       0           1          reload timer
       1           0          delete timer
       1           1          reload timer

My suggestion was:

old value new value action

       0           0          nothing
       0           1          reload timer
       1           0          delete timer
       1           1          nothing
Quoted 6 lines

If you think that's wrong, then surely your first patch also has the same problem?

thanks
-- PMM

Yes, my first patch has same problem. Noticed that issue couple hours ago, it's separate bug as I see now... Will make patch for it too.

To clarify new issue:
1) load TIMER_LOAD with some value
2) write (TIMER_CONTROL_ENABLE | TIMER_CONTROL_ONESHOT |
TIMER_CONTROL_IT_ENABLE) to TIMER_CONTROL
3) wait for one-shot complete
4) re-load TIMER_LOAD
5) write (TIMER_CONTROL_ENABLE | TIMER_CONTROL_PERIODIC |
TIMER_CONTROL_IT_ENABLE) to TIMER_CONTROL <---- it won't start, bug

Oh, and just noticed that timer code doesn't handle IT(interrupt enable) bit. Will fix it too.

Signature · 1 line
Dmitry
#9 lore raw

02.07.2015 21:43, Dmitry Osipenko пишет:

Quoted 3 lines

02.07.2015 21:09, Peter Maydell пишет:
TIMER_CONTROL_IT_ENABLE) to TIMER_CONTROL <---- it won't start, bug

s/it won't start/it won't start periodic/

Signature · 1 line
Dmitry
#10 lore raw
In reply to Dmitry Osipenko

02.07.2015 21:43, Dmitry Osipenko пишет:

Quoted 2 lines

4) re-load TIMER_LOAD

And 4-th step can be omitted.

Anyway, I already sent patches fixing this and IT issues.

Signature · 1 line
Dmitry