When estimating the clock frequency based on the RTC, take seconds into account in case the Update In Progress (UIP) bit wasn't seen. This can happen in virtual machines (which may get pre-empted by the hypervisor at inopportune times) with QEMU emulating the RTC (and in fact not setting the UIP bit for very long), especially on slow hosts such as FPGA systems and emulators. This results in several seconds actually having passed before seeing the UIP bit instead of just one second, and exaggerated timer frequencies. Signed-off-by: James Hogan <james.hogan@xxxxxxxxxx> Cc: Ralf Baechle <ralf@xxxxxxxxxxxxxx> Cc: Steven J. Hill <Steven.Hill@xxxxxxxxxx> Cc: linux-mips@xxxxxxxxxxxxxx --- arch/mips/mti-malta/malta-time.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/arch/mips/mti-malta/malta-time.c b/arch/mips/mti-malta/malta-time.c index a030d41eb5a1..1aaa56162ba0 100644 --- a/arch/mips/mti-malta/malta-time.c +++ b/arch/mips/mti-malta/malta-time.c @@ -21,6 +21,7 @@ #include <linux/i8253.h> #include <linux/init.h> #include <linux/kernel_stat.h> +#include <linux/math64.h> #include <linux/sched.h> #include <linux/spinlock.h> #include <linux/interrupt.h> @@ -72,6 +73,8 @@ static void __init estimate_frequencies(void) { unsigned long flags; unsigned int count, start; + unsigned char secs1, secs2, ctrl; + int secs; cycle_t giccount = 0, gicstart = 0; #if defined(CONFIG_KVM_GUEST) && CONFIG_KVM_GUEST_TIMER_FREQ @@ -91,6 +94,7 @@ static void __init estimate_frequencies(void) gic_start_count(); gicstart = gic_read_count(); } + secs1 = CMOS_READ(RTC_SECONDS); /* Read counter exactly on falling edge of update flag. */ while (CMOS_READ(RTC_REG_A) & RTC_UIP); @@ -99,14 +103,25 @@ static void __init estimate_frequencies(void) count = read_c0_count(); if (gic_present) giccount = gic_read_count(); + secs2 = CMOS_READ(RTC_SECONDS); + ctrl = CMOS_READ(RTC_CONTROL); local_irq_restore(flags); + if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD) { + secs1 = bcd2bin(secs1); + secs2 = bcd2bin(secs2); + } + secs = secs2 - secs1; + if (secs < 1) + secs += 60; + count -= start; + count /= secs; mips_hpt_frequency = count; if (gic_present) { - giccount -= gicstart; + giccount = div_u64(giccount - gicstart, secs); gic_frequency = giccount; } } -- 2.3.6