qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Paolo Bonzini <pbonzini@redhat.com>
To: qemu-devel@nongnu.org
Cc: Xiao Guangrong <xiaoguangrong@tencent.com>
Subject: [Qemu-devel] [PULL 25/33] qtest: add rtc periodic timer test
Date: Thu,  1 Jun 2017 14:41:43 +0200	[thread overview]
Message-ID: <1496320911-51305-26-git-send-email-pbonzini@redhat.com> (raw)
In-Reply-To: <1496320911-51305-1-git-send-email-pbonzini@redhat.com>

From: Xiao Guangrong <xiaoguangrong@tencent.com>

It tests the accuracy of rtc periodic timer which is recently
improved & fixed by commit 7ffcb539a3 ("mc146818rtc: precisely count
the clock for periodic timer", 2017-05-19).

Signed-off-by: Xiao Guangrong <xiaoguangrong@tencent.com>
Message-Id: <20170527025301.23499-1-xiaoguangrong@tencent.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 hw/timer/mc146818rtc.c         | 15 +++----------
 include/hw/timer/mc146818rtc.h | 19 ++++++++++++++++
 tests/rtc-test.c               | 49 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 71 insertions(+), 12 deletions(-)

diff --git a/hw/timer/mc146818rtc.c b/hw/timer/mc146818rtc.c
index 542cd09..1b8d3d7 100644
--- a/hw/timer/mc146818rtc.c
+++ b/hw/timer/mc146818rtc.c
@@ -120,7 +120,7 @@ static void rtc_coalesced_timer_update(RTCState *s)
         /* divide each RTC interval to 2 - 8 smaller intervals */
         int c = MIN(s->irq_coalesced, 7) + 1; 
         int64_t next_clock = qemu_clock_get_ns(rtc_clock) +
-            muldiv64(s->period / c, NANOSECONDS_PER_SECOND, RTC_CLOCK_RATE);
+            periodic_clock_to_ns(s->period / c);
         timer_mod(s->coalesced_timer, next_clock);
     }
 }
@@ -178,16 +178,8 @@ static uint32_t rtc_periodic_clock_ticks(RTCState *s)
      }
 
     period_code = s->cmos_data[RTC_REG_A] & 0x0f;
-    if (!period_code) {
-        return 0;
-    }
-
-    if (period_code <= 2) {
-        period_code += 7;
-    }
 
-    /* period in 32 Khz cycles */
-    return 1 << (period_code - 1);
+    return periodic_period_to_clock(period_code);
 }
 
 /*
@@ -260,8 +252,7 @@ periodic_timer_update(RTCState *s, int64_t current_time, uint32_t old_period)
         assert(lost_clock >= 0 && lost_clock <= period);
 
         next_irq_clock = cur_clock + period - lost_clock;
-        s->next_periodic_time = muldiv64(next_irq_clock, NANOSECONDS_PER_SECOND,
-                                         RTC_CLOCK_RATE) + 1;
+        s->next_periodic_time = periodic_clock_to_ns(next_irq_clock) + 1;
         timer_mod(s->periodic_timer, s->next_periodic_time);
     } else {
         s->irq_coalesced = 0;
diff --git a/include/hw/timer/mc146818rtc.h b/include/hw/timer/mc146818rtc.h
index 7c8e64b..f23e734 100644
--- a/include/hw/timer/mc146818rtc.h
+++ b/include/hw/timer/mc146818rtc.h
@@ -10,4 +10,23 @@ ISADevice *rtc_init(ISABus *bus, int base_year, qemu_irq intercept_irq);
 void rtc_set_memory(ISADevice *dev, int addr, int val);
 int rtc_get_memory(ISADevice *dev, int addr);
 
+static inline uint32_t periodic_period_to_clock(int period_code)
+{
+    if (!period_code) {
+        return 0;
+   }
+
+    if (period_code <= 2) {
+        period_code += 7;
+    }
+    /* period in 32 Khz cycles */
+   return 1 << (period_code - 1);
+}
+
+#define RTC_CLOCK_RATE            32768
+
+static inline int64_t periodic_clock_to_ns(int64_t clocks)
+{
+    return muldiv64(clocks, NANOSECONDS_PER_SECOND, RTC_CLOCK_RATE);
+}
 #endif /* MC146818RTC_H */
diff --git a/tests/rtc-test.c b/tests/rtc-test.c
index a086efd..0500686 100644
--- a/tests/rtc-test.c
+++ b/tests/rtc-test.c
@@ -14,6 +14,7 @@
 #include "qemu/osdep.h"
 
 #include "libqtest.h"
+#include "hw/timer/mc146818rtc.h"
 #include "hw/timer/mc146818rtc_regs.h"
 
 static uint8_t base = 0x70;
@@ -542,6 +543,52 @@ static void register_b_set_flag(void)
     g_assert_cmpint(cmos_read(RTC_CENTURY), ==, 0x20);
 }
 
+#define RTC_PERIOD_CODE1    13   /* 8 Hz */
+#define RTC_PERIOD_CODE2    15   /* 2 Hz */
+
+#define RTC_PERIOD_TEST_NR  50
+
+static uint64_t wait_periodic_interrupt(uint64_t real_time)
+{
+    while (!get_irq(RTC_ISA_IRQ)) {
+        real_time = clock_step_next();
+    }
+
+    g_assert((cmos_read(RTC_REG_C) & REG_C_PF) != 0);
+    return real_time;
+}
+
+static void periodic_timer(void)
+{
+    int i;
+    uint64_t period_clocks, period_time, start_time, real_time;
+
+    /* disable all interrupts. */
+    cmos_write(RTC_REG_B, cmos_read(RTC_REG_B) &
+                                   ~(REG_B_PIE | REG_B_AIE | REG_B_UIE));
+    cmos_write(RTC_REG_A, RTC_PERIOD_CODE1);
+    /* enable periodic interrupt after properly configure the period. */
+    cmos_write(RTC_REG_B, cmos_read(RTC_REG_B) | REG_B_PIE);
+
+    start_time = real_time = clock_step_next();
+
+    for (i = 0; i < RTC_PERIOD_TEST_NR; i++) {
+        cmos_write(RTC_REG_A, RTC_PERIOD_CODE1);
+        real_time = wait_periodic_interrupt(real_time);
+        cmos_write(RTC_REG_A, RTC_PERIOD_CODE2);
+        real_time = wait_periodic_interrupt(real_time);
+    }
+
+    period_clocks = periodic_period_to_clock(RTC_PERIOD_CODE1) +
+                       periodic_period_to_clock(RTC_PERIOD_CODE2);
+    period_clocks *= RTC_PERIOD_TEST_NR;
+    period_time = periodic_clock_to_ns(period_clocks);
+
+    real_time -= start_time;
+    g_assert_cmpint(ABS((int64_t)(real_time - period_time)), <=,
+                    NANOSECONDS_PER_SECOND * 0.5);
+}
+
 int main(int argc, char **argv)
 {
     QTestState *s = NULL;
@@ -564,6 +611,8 @@ int main(int argc, char **argv)
     qtest_add_func("/rtc/set-year/1980", set_year_1980);
     qtest_add_func("/rtc/misc/register_b_set_flag", register_b_set_flag);
     qtest_add_func("/rtc/misc/fuzz-registers", fuzz_registers);
+    qtest_add_func("/rtc/periodic/interrupt", periodic_timer);
+
     ret = g_test_run();
 
     if (s) {
-- 
1.8.3.1

  parent reply	other threads:[~2017-06-01 12:42 UTC|newest]

Thread overview: 44+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-06-01 12:41 [Qemu-devel] [PULL 00/33] Misc patches for 2017-06-01 Paolo Bonzini
2017-06-01 12:41 ` [Qemu-devel] [PULL 01/33] mc146818rtc: update periodic timer only if it is needed Paolo Bonzini
2017-06-01 12:41 ` [Qemu-devel] [PULL 02/33] mc146818rtc: precisely count the clock for periodic timer Paolo Bonzini
2017-06-01 12:41 ` [Qemu-devel] [PULL 03/33] mc146818rtc: ensure LOST_TICK_POLICY_SLEW is only enabled on TARGET_I386 Paolo Bonzini
2017-06-01 12:41 ` [Qemu-devel] [PULL 04/33] mc146818rtc: drop unnecessary '#ifdef TARGET_I386' Paolo Bonzini
2017-06-01 12:41 ` [Qemu-devel] [PULL 05/33] mc146818rtc: embrace all x86 specific code Paolo Bonzini
2017-06-01 12:41 ` [Qemu-devel] [PULL 06/33] kvm: irqchip: trace changes on msi add/remove Paolo Bonzini
2017-06-01 12:41 ` [Qemu-devel] [PULL 07/33] msix: trace control bit write op Paolo Bonzini
2017-06-01 12:41 ` [Qemu-devel] [PULL 08/33] kvm: irqchip: skip update msi when disabled Paolo Bonzini
2017-06-01 12:41 ` [Qemu-devel] [PULL 09/33] Check the return value of fcntl in qemu_set_cloexec Paolo Bonzini
2017-06-01 12:41 ` [Qemu-devel] [PULL 10/33] nbd: strict nbd_wr_syncv Paolo Bonzini
2017-06-01 12:41 ` [Qemu-devel] [PULL 11/33] nbd: read_sync and friends: return 0 on success Paolo Bonzini
2017-06-01 12:41 ` [Qemu-devel] [PULL 12/33] nbd: add errp parameter to nbd_wr_syncv() Paolo Bonzini
2017-06-01 12:41 ` [Qemu-devel] [PULL 13/33] nbd: add errp to read_sync, write_sync and drop_sync Paolo Bonzini
2017-06-01 12:41 ` [Qemu-devel] [PULL 14/33] nbd/client.c: use errp instead of LOG Paolo Bonzini
2017-06-01 12:41 ` [Qemu-devel] [PULL 15/33] exec: simplify phys_page_find() params Paolo Bonzini
2017-06-01 12:41 ` [Qemu-devel] [PULL 16/33] virtio-scsi: Unset hotplug handler when unrealize Paolo Bonzini
2017-06-01 12:41 ` [Qemu-devel] [PULL 17/33] vhost-user-scsi: Introduce vhost-user-scsi host device Paolo Bonzini
2017-06-05 16:28   ` Eric Blake
2017-06-05 16:38     ` Felipe Franciosi
2017-06-05 16:41       ` Eric Blake
2017-06-05 16:50         ` Paolo Bonzini
2017-06-01 12:41 ` [Qemu-devel] [PULL 18/33] vhost-user-scsi: Introduce a vhost-user-scsi sample application Paolo Bonzini
2017-06-01 12:41 ` [Qemu-devel] [PULL 19/33] target/i386: enable A20 automatically in system management mode Paolo Bonzini
2017-06-01 12:41 ` [Qemu-devel] [PULL 20/33] target/i386: use multiple CPU AddressSpaces Paolo Bonzini
2017-06-01 12:41 ` [Qemu-devel] [PULL 21/33] i386: fix read/write cr with icount option Paolo Bonzini
2017-06-01 12:41 ` [Qemu-devel] [PULL 22/33] sockets: improve error reporting if UNIX socket path is too long Paolo Bonzini
2017-06-13 16:10   ` Peter Maydell
2017-06-14  8:05     ` Daniel P. Berrange
2017-06-01 12:41 ` [Qemu-devel] [PULL 23/33] exec: fix address_space_get_iotlb_entry page mask Paolo Bonzini
2017-06-02  9:59   ` Peter Xu
2017-06-01 12:41 ` [Qemu-devel] [PULL 24/33] nbd: Fully initialize client in case of failed negotiation Paolo Bonzini
2017-06-01 15:15   ` Eric Blake
2017-06-01 12:41 ` Paolo Bonzini [this message]
2017-06-01 12:41 ` [Qemu-devel] [PULL 26/33] kvmclock: update system_time_msr address forcibly Paolo Bonzini
2017-06-01 12:41 ` [Qemu-devel] [PULL 27/33] linuxboot_dma: compile for i486 Paolo Bonzini
2017-06-01 12:41 ` [Qemu-devel] [PULL 28/33] edu: fix memory leak on msi_broken platforms Paolo Bonzini
2017-06-01 12:41 ` [Qemu-devel] [PULL 29/33] i386/kvm: do not zero out segment flags if segment is unusable or not present Paolo Bonzini
2017-06-01 12:41 ` [Qemu-devel] [PULL 30/33] target/i386: Add GDB XML description for SSE registers Paolo Bonzini
2017-06-01 12:41 ` [Qemu-devel] [PULL 31/33] hw/core: nmi.c can be compiled as common-obj nowadays Paolo Bonzini
2017-06-01 12:41 ` [Qemu-devel] [PULL 32/33] nbd: make it thread-safe, fix qcow2 over nbd Paolo Bonzini
2017-06-01 12:41 ` [Qemu-devel] [PULL 33/33] kvm: don't register smram_listener when smm is off Paolo Bonzini
2017-06-01 14:26 ` [Qemu-devel] [PULL 00/33] Misc patches for 2017-06-01 no-reply
2017-06-01 15:09 ` no-reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1496320911-51305-26-git-send-email-pbonzini@redhat.com \
    --to=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=xiaoguangrong@tencent.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).