hw/rtc/mc146818rtc.c | 32 +++++++++++++++++++++--------- tests/qtest/rtc-test.c | 45 ++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 66 insertions(+), 11 deletions(-)
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 set so idle guests are not woken every period, then re-arm
it when register C is read.
Lost tick compensation is limited to PIE=1 so that the RTC does
not count ticks with the periodic interrupt disabled as lost.
Cc: Tai Yunfang <yunfangtai@tencent.com>
Cc: Xiao Guangrong <xiaoguangrong@tencent.com>
Cc: Jason Thorpe <thorpej@me.com>
Fixes: 369b41359af ("mc146818rtc: precisely count the clock for periodic timer")
Co-authored-by: Philippe Mathieu-Daudé <philmd@oss.qualcomm.com>
Signed-off-by: Philippe Mathieu-Daudé <philmd@oss.qualcomm.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
hw/rtc/mc146818rtc.c | 32 +++++++++++++++++++++---------
tests/qtest/rtc-test.c | 45 ++++++++++++++++++++++++++++++++++++++++--
2 files changed, 66 insertions(+), 11 deletions(-)
diff --git a/hw/rtc/mc146818rtc.c b/hw/rtc/mc146818rtc.c
index 887a8b07582..ae12e3f875e 100644
--- a/hw/rtc/mc146818rtc.c
+++ b/hw/rtc/mc146818rtc.c
@@ -132,13 +132,7 @@ static void rtc_coalesced_timer(void *opaque)
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;
+ int period_code = s->cmos_data[RTC_REG_A] & 0x0f;
return periodic_period_to_clock(period_code);
}
@@ -153,6 +147,18 @@ static void periodic_timer_update(MC146818RtcState *s, int64_t current_time,
uint32_t period;
int64_t cur_clock, next_irq_clock, lost_clock = 0;
+ /*
+ * 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;
+ }
+
period = rtc_periodic_clock_ticks(s);
s->period = period;
@@ -194,7 +200,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;
@@ -226,8 +233,8 @@ static void rtc_periodic_timer(void *opaque)
{
MC146818RtcState *s = opaque;
- periodic_timer_update(s, s->next_periodic_time, s->period, false);
s->cmos_data[RTC_REG_C] |= REG_C_PF;
+ periodic_timer_update(s, s->next_periodic_time, s->period, false);
if (s->cmos_data[RTC_REG_B] & REG_B_PIE) {
s->cmos_data[RTC_REG_C] |= REG_C_IRQF;
if (s->lost_tick_policy == LOST_TICK_POLICY_SLEW) {
@@ -699,10 +706,17 @@ static uint64_t cmos_ioport_read(void *opaque, hwaddr addr,
ret = s->cmos_data[s->cmos_index];
qemu_irq_lower(s->irq);
s->cmos_data[RTC_REG_C] = 0x00;
+
+ /* Rearm the timers for bits that have been cleared. */
if (ret & (REG_C_UF | REG_C_AF)) {
check_update_timer(s);
}
+ 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, false);
+ }
+
if(s->irq_coalesced &&
(s->cmos_data[RTC_REG_B] & REG_B_PIE) &&
s->irq_reinject_on_ack_count < RTC_REINJECT_ON_ACK_COUNT) {
diff --git a/tests/qtest/rtc-test.c b/tests/qtest/rtc-test.c
index 02ed4e12385..b320a91d950 100644
--- a/tests/qtest/rtc-test.c
+++ b/tests/qtest/rtc-test.c
@@ -643,6 +643,46 @@ static void uip_stuck(void)
#define RTC_PERIOD_TEST_NR 50
+static uint64_t wait_periodic_timer(uint64_t real_time, int period_code)
+{
+ uint64_t periodic_time = real_time;
+ periodic_time += NANOSECONDS_PER_SECOND >> (16 - period_code);
+
+ cmos_write(RTC_REG_A, period_code);
+ while (!(cmos_read(RTC_REG_C) & REG_C_PF)) {
+ real_time = clock_step_next();
+ g_assert_cmpint(real_time, <, periodic_time + 100);
+ }
+
+ g_assert_cmpint(real_time, <, periodic_time + 100);
+ g_assert_cmpint(real_time, >, periodic_time - 100);
+ return real_time;
+}
+
+static void periodic_timer(void)
+{
+ int i;
+ uint64_t real_time;
+
+ /* disable all interrupts. */
+ cmos_write(RTC_REG_A, 0);
+ cmos_write(RTC_REG_B, cmos_read(RTC_REG_B) &
+ ~(REG_B_PIE | REG_B_AIE | REG_B_UIE));
+
+ /* wait for the first timer without assertions, to synchronize and clear PF */
+ cmos_read(RTC_REG_C);
+ cmos_write(RTC_REG_A, 10);
+ real_time = clock_step_next();
+ while (!(cmos_read(RTC_REG_C) & REG_C_PF)) {
+ real_time = clock_step_next();
+ }
+
+ for (i = 0; i < RTC_PERIOD_TEST_NR; i++) {
+ real_time = wait_periodic_timer(real_time, RTC_PERIOD_CODE1);
+ real_time = wait_periodic_timer(real_time, RTC_PERIOD_CODE2);
+ }
+}
+
static uint64_t wait_periodic_interrupt(uint64_t real_time)
{
while (!get_irq(RTC_ISA_IRQ)) {
@@ -653,7 +693,7 @@ static uint64_t wait_periodic_interrupt(uint64_t real_time)
return real_time;
}
-static void periodic_timer(void)
+static void periodic_interrupt(void)
{
int i;
uint64_t period_clocks, period_time, start_time, real_time;
@@ -708,7 +748,8 @@ int main(int argc, char **argv)
qtest_add_func("/rtc/update/divider-reset", divider_reset);
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/periodic/timer", periodic_timer);
+ qtest_add_func("/rtc/periodic/interrupt", periodic_interrupt);
ret = g_test_run();
--
2.56.0
© 2016 - 2026 Red Hat, Inc.