Skip to contentSkip to conversation

hw/rtc/mc146818: Set PF independently of PIE

PATCH In review Philippe Mathieu-Daudé 2 participants 2 messages Nov 19 – Nov 20

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 hw/rtc/mc146818: Set PF independently of PIE

The MC146818A datasheet says PF is latched independently of PIE; PIE only makes PF raise IRQF and the IRQ line. QEMU skipped the periodic timer when PIE was clear, so PF never latched.

Compute the period from register A regardless of PIE, and latch PF without raising an interrupt when PIE is clear. Stop the timer while PF is pending so idle guests are not woken every period, then re-arm it when register C is read. Preserve the PF cadence for a late read, and restrict SLEW coalescing to PIE=1 so it cannot inject interrupts while periodic interrupts are disabled.

The qtest covers PF latching and re-arming after a delayed register C read, and verifies that PIE still controls IRQF and the IRQ line.

AI-used-for: Regression tests

Fixes 369b41359af ("mc146818rtc: precisely count the clock for periodic timer")

---
v2: https://lists.nongnu.org/archive/html/qemu-devel/2021-06/msg05307.html

Cc Tai Yunfang <[email protected]>
Cc Xiao Guangrong <[email protected]>
Cc Jason Thorpe <[email protected]>
Cc Paolo Bonzini <[email protected]>
---
 hw/rtc/mc146818rtc.c   | 35 ++++++++++++++++++++++++-----
 tests/qtest/rtc-test.c | 51 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 81 insertions(+), 5 deletions(-)
hw/rtc/mc146818rtc.c +30 -5
index 887a8b075825..a6d760fecd00 100644
@@ -134,10 +134,6 @@ static uint32_t rtc_periodic_clock_ticks(MC146818RtcState *s)
 {
     int period_code;
 
-    if (!(s->cmos_data[RTC_REG_B] & REG_B_PIE)) {
-        return 0;
-     }
-
     period_code = s->cmos_data[RTC_REG_A] & 0x0f;
 
     return periodic_period_to_clock(period_code);
@@ -162,6 +158,18 @@ static void periodic_timer_update(MC146818RtcState *s, int64_t current_time,
         return;
     }
 
+    /*
+     * When the periodic interrupt is disabled the periodic timer only
+     * serves to set the PF flag in register C. Once it has been set
+     * there is nothing to do until the flag is cleared by reading
+     * register C, so keep the timer stopped to avoid useless wakeups.
+     */
+    if (!(s->cmos_data[RTC_REG_B] & REG_B_PIE) &&
+        (s->cmos_data[RTC_REG_C] & REG_C_PF)) {
+        timer_del(s->periodic_timer);
+        return;
+    }
+
     /* compute 32 khz clock */
     cur_clock =
         muldiv64(current_time, RTC_CLOCK_RATE, NANOSECONDS_PER_SECOND);
@@ -194,7 +202,8 @@ static void periodic_timer_update(MC146818RtcState *s, int64_t current_time,
      *    to a shorter period, scale up the missing ticks since the
      *    OS handler will treat past delayed ticks as shorter.
      */
-    if (s->lost_tick_policy == LOST_TICK_POLICY_SLEW) {
+    if (s->lost_tick_policy == LOST_TICK_POLICY_SLEW &&
+        (s->cmos_data[RTC_REG_B] & REG_B_PIE)) {
         uint32_t old_irq_coalesced = s->irq_coalesced;
 
         lost_clock += old_irq_coalesced * old_period;
@@ -241,6 +250,13 @@ static void rtc_periodic_timer(void *opaque)
             }
         } else
             qemu_irq_raise(s->irq);
+    } else {
+        /*
+         * Only the PF flag is affected when the periodic interrupt is
+         * disabled. Stop the timer until PF is cleared by a read of
+         * register C, so that an idle guest is not woken up every period.
+         */
+        timer_del(s->periodic_timer);
     }
 }
 
@@ -702,6 +718,15 @@ static uint64_t cmos_ioport_read(void *opaque, hwaddr addr,
             if (ret & (REG_C_UF | REG_C_AF)) {
                 check_update_timer(s);
             }
+            /*
+             * Clearing PF re-arms the periodic timer: it may have been
+             * stopped while PF was pending with the periodic interrupt
+             * disabled.
+             */
+            if ((ret & REG_C_PF) && !(s->cmos_data[RTC_REG_B] & REG_B_PIE)) {
+                periodic_timer_update(s, qemu_clock_get_ns(rtc_clock),
+                                      s->period, true);
+            }
 
             if(s->irq_coalesced &&
                     (s->cmos_data[RTC_REG_B] & REG_B_PIE) &&
tests/qtest/rtc-test.c +51 -0

63 lines not on this page · raw message

Signature · 1 line
2.55.0
#1 lore raw
Quoted 11 lines · Philippe Mathieu-Daudé

The MC146818A datasheet says PF is latched independently of PIE; PIE only makes PF raise IRQF and the IRQ line. QEMU skipped the periodic timer when PIE was clear, so PF never latched.

Compute the period from register A regardless of PIE, and latch PF without raising an interrupt when PIE is clear. Stop the timer while PF is pending so idle guests are not woken every period, then re-arm it when register C is read. Preserve the PF cadence for a late read, and restrict SLEW coalescing to PIE=1 so it cannot inject interrupts while periodic interrupts are disabled.

Quoted 4 lines

The qtest covers PF latching and re-arming after a delayed register C read, and verifies that PIE still controls IRQF and the IRQ line.

AI-used-for: Regression tests

I have already written a qtest by hand, so I'll send a v4. :)

Paolo

Quoted 157 lines
Fixes 369b41359af ("mc146818rtc: precisely count the clock for periodic timer")

---
v2: https://lists.nongnu.org/archive/html/qemu-devel/2021-06/msg05307.html

Cc Tai Yunfang <[email protected]>
Cc Xiao Guangrong <[email protected]>
Cc Jason Thorpe <[email protected]>
Cc Paolo Bonzini <[email protected]>
---
  hw/rtc/mc146818rtc.c   | 35 ++++++++++++++++++++++++-----
  tests/qtest/rtc-test.c | 51 ++++++++++++++++++++++++++++++++++++++++++
  2 files changed, 81 insertions(+), 5 deletions(-)
hw/rtc/mc146818rtc.c +30 -5
index 887a8b075825..a6d760fecd00 100644
@@ -134,10 +134,6 @@ static uint32_t rtc_periodic_clock_ticks(MC146818RtcState *s)
  {
      int period_code;
 
-    if (!(s->cmos_data[RTC_REG_B] & REG_B_PIE)) {
-        return 0;
-     }
-
      period_code = s->cmos_data[RTC_REG_A] & 0x0f;
 
      return periodic_period_to_clock(period_code);
@@ -162,6 +158,18 @@ static void periodic_timer_update(MC146818RtcState *s, int64_t current_time,
          return;
      }
 
+    /*
+     * When the periodic interrupt is disabled the periodic timer only
+     * serves to set the PF flag in register C. Once it has been set
+     * there is nothing to do until the flag is cleared by reading
+     * register C, so keep the timer stopped to avoid useless wakeups.
+     */
+    if (!(s->cmos_data[RTC_REG_B] & REG_B_PIE) &&
+        (s->cmos_data[RTC_REG_C] & REG_C_PF)) {
+        timer_del(s->periodic_timer);
+        return;
+    }
+
      /* compute 32 khz clock */
      cur_clock =
          muldiv64(current_time, RTC_CLOCK_RATE, NANOSECONDS_PER_SECOND);
@@ -194,7 +202,8 @@ static void periodic_timer_update(MC146818RtcState *s, int64_t current_time,
       *    to a shorter period, scale up the missing ticks since the
       *    OS handler will treat past delayed ticks as shorter.
       */
-    if (s->lost_tick_policy == LOST_TICK_POLICY_SLEW) {
+    if (s->lost_tick_policy == LOST_TICK_POLICY_SLEW &&
+        (s->cmos_data[RTC_REG_B] & REG_B_PIE)) {
          uint32_t old_irq_coalesced = s->irq_coalesced;
 
          lost_clock += old_irq_coalesced * old_period;
@@ -241,6 +250,13 @@ static void rtc_periodic_timer(void *opaque)
              }
          } else
              qemu_irq_raise(s->irq);
+    } else {
+        /*
+         * Only the PF flag is affected when the periodic interrupt is
+         * disabled. Stop the timer until PF is cleared by a read of
+         * register C, so that an idle guest is not woken up every period.
+         */
+        timer_del(s->periodic_timer);
      }
  }
 
@@ -702,6 +718,15 @@ static uint64_t cmos_ioport_read(void *opaque, hwaddr addr,
              if (ret & (REG_C_UF | REG_C_AF)) {
                  check_update_timer(s);
              }
+            /*
+             * Clearing PF re-arms the periodic timer: it may have been
+             * stopped while PF was pending with the periodic interrupt
+             * disabled.
+             */
+            if ((ret & REG_C_PF) && !(s->cmos_data[RTC_REG_B] & REG_B_PIE)) {
+                periodic_timer_update(s, qemu_clock_get_ns(rtc_clock),
+                                      s->period, true);
+            }
 
              if(s->irq_coalesced &&
                      (s->cmos_data[RTC_REG_B] & REG_B_PIE) &&
tests/qtest/rtc-test.c +51 -0
index 02ed4e123859..5eca6f65ea95 100644
@@ -540,6 +540,56 @@ static void am_pm_alarm(void)
      g_assert((cmos_read(RTC_REG_C) & REG_C_AF) == 0);
  }
 
+/*
+ * The manual says the selected periodic tap sets PF in Register C whether
+ * or not PIE is set (PIE only additionally gates IRQF). With PIE clear
+ * the tap must still latch PF, without raising the IRQ line, and reading
+ * Register C must clear PF and let the next period latch it again.
+ */
+static void pf_without_pie(void)
+{
+    uint64_t first, second, period;
+    uint8_t c;
+
+    cmos_write(RTC_REG_B, REG_B_24H);
+    cmos_write(RTC_REG_A, 15);          /* RS 1111: 2 Hz */
+    (void)cmos_read(RTC_REG_C);
+    g_assert(!get_irq(RTC_ISA_IRQ));
+    period = periodic_clock_to_ns(periodic_period_to_clock(15));
+
+    /* PIE is clear: PF latches but no IRQF and no IRQ line.  */
+    do {
+        first = clock_step_next();
+        c = cmos_read(RTC_REG_C);
+    } while (!(c & REG_C_PF));
+    g_assert_cmpint(c & REG_C_IRQF, ==, 0);
+    g_assert(!get_irq(RTC_ISA_IRQ));
+
+    /* Reading Register C late must not delay the next PF edge.  */
+    clock_step(period / 4);
+    g_assert_cmpint(cmos_read(RTC_REG_C) & REG_C_PF, ==, 0);
+
+    /* ...and the next period latches it again.  */
+    do {
+        second = clock_step_next();
+        c = cmos_read(RTC_REG_C);
+    } while (!(c & REG_C_PF));
+    g_assert_cmpint(c & REG_C_IRQF, ==, 0);
+    g_assert(!get_irq(RTC_ISA_IRQ));
+    g_assert_cmpint(ABS((int64_t)(second - first - period)), <=,
+                    period / 4);
+
+    /* enabling PIE makes the next period raise IRQF and the IRQ line.  */
+    cmos_write(RTC_REG_B, cmos_read(RTC_REG_B) | REG_B_PIE);
+    while (!get_irq(RTC_ISA_IRQ)) {
+        clock_step_next();
+    }
+    g_assert_cmpint(cmos_read(RTC_REG_C) & (REG_C_PF | REG_C_IRQF), ==,
+                    REG_C_PF | REG_C_IRQF);
+
+    (void)cmos_read(RTC_REG_C);
+    cmos_write(RTC_REG_B, cmos_read(RTC_REG_B) & ~REG_B_PIE);
+}
  /* success if no crash or abort */
  static void fuzz_registers(void)
  {
@@ -709,6 +759,7 @@ int main(int argc, char **argv)
      qtest_add_func("/rtc/update/uip-stuck", uip_stuck);
      qtest_add_func("/rtc/misc/fuzz-registers", fuzz_registers);
      qtest_add_func("/rtc/periodic/interrupt", periodic_timer);
+    qtest_add_func("/rtc/registers/pf-without-pie", pf_without_pie);
 
      ret = g_test_run();