Skip to content

Commit 24e1df6

Browse files
James Hoganralfbaechle
James Hogan
authored andcommitted
MIPS: malta-time: Take seconds into account
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 hardware emulators. This results in several seconds actually having elapsed before seeing the UIP bit instead of just one second, and exaggerated timer frequencies. While updating the comments, they're also fixed to match the code in that the rising edge of the update flag is detected first, not the falling edge. The rising edge gives a more precise point to read the counters in a virtualised system than the falling edge, resulting in a more accurate frequency. It does however mean that we have to also wait for the falling edge before doing the read of the RTC seconds register, otherwise it seems to be possible in slow hardware emulation to stray into the interval when the RTC time is undefined during the update (at least 244uS after the rising edge of the update flag). This can result in both seconds values reading the same, and it wrapping to 60 seconds, vastly underestimating the frequency. Signed-off-by: James Hogan <[email protected]> Cc: James Hogan <[email protected]> Cc: [email protected] Patchwork: https://patchwork.linux-mips.org/patch/13174/ Signed-off-by: Ralf Baechle <[email protected]>
1 parent aab4673 commit 24e1df6

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

arch/mips/mti-malta/malta-time.c

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <linux/i8253.h>
2222
#include <linux/init.h>
2323
#include <linux/kernel_stat.h>
24+
#include <linux/math64.h>
2425
#include <linux/sched.h>
2526
#include <linux/spinlock.h>
2627
#include <linux/interrupt.h>
@@ -72,6 +73,8 @@ static void __init estimate_frequencies(void)
7273
{
7374
unsigned long flags;
7475
unsigned int count, start;
76+
unsigned char secs1, secs2, ctrl;
77+
int secs;
7578
cycle_t giccount = 0, gicstart = 0;
7679

7780
#if defined(CONFIG_KVM_GUEST) && CONFIG_KVM_GUEST_TIMER_FREQ
@@ -84,29 +87,48 @@ static void __init estimate_frequencies(void)
8487
if (gic_present)
8588
gic_start_count();
8689

87-
/* Read counter exactly on falling edge of update flag. */
90+
/*
91+
* Read counters exactly on rising edge of update flag.
92+
* This helps get an accurate reading under virtualisation.
93+
*/
8894
while (CMOS_READ(RTC_REG_A) & RTC_UIP);
8995
while (!(CMOS_READ(RTC_REG_A) & RTC_UIP));
90-
9196
start = read_c0_count();
9297
if (gic_present)
9398
gicstart = gic_read_count();
9499

95-
/* Read counter exactly on falling edge of update flag. */
100+
/* Wait for falling edge before reading RTC. */
96101
while (CMOS_READ(RTC_REG_A) & RTC_UIP);
97-
while (!(CMOS_READ(RTC_REG_A) & RTC_UIP));
102+
secs1 = CMOS_READ(RTC_SECONDS);
98103

104+
/* Read counters again exactly on rising edge of update flag. */
105+
while (!(CMOS_READ(RTC_REG_A) & RTC_UIP));
99106
count = read_c0_count();
100107
if (gic_present)
101108
giccount = gic_read_count();
102109

110+
/* Wait for falling edge before reading RTC again. */
111+
while (CMOS_READ(RTC_REG_A) & RTC_UIP);
112+
secs2 = CMOS_READ(RTC_SECONDS);
113+
114+
ctrl = CMOS_READ(RTC_CONTROL);
115+
103116
local_irq_restore(flags);
104117

118+
if (!(ctrl & RTC_DM_BINARY) || RTC_ALWAYS_BCD) {
119+
secs1 = bcd2bin(secs1);
120+
secs2 = bcd2bin(secs2);
121+
}
122+
secs = secs2 - secs1;
123+
if (secs < 1)
124+
secs += 60;
125+
105126
count -= start;
127+
count /= secs;
106128
mips_hpt_frequency = count;
107129

108130
if (gic_present) {
109-
giccount -= gicstart;
131+
giccount = div_u64(giccount - gicstart, secs);
110132
gic_frequency = giccount;
111133
}
112134
}

0 commit comments

Comments
 (0)