Commit 9e8912e0 authored by Linus Torvalds's avatar Linus Torvalds

Fast TSC calibration: calculate proper frequency error bounds

In order for ntpd to correctly synchronize the clocks, the frequency of
the system clock must not be off by more than 500 ppm (or, put another
way, 1:2000), or ntpd will end up giving up on trying to synchronize
properly, and ends up reseting the clock in jumps instead.

The fast TSC PIT calibration sometimes failed this test - it was
assuming that the PIT reads always took about one microsecond each (2us
for the two reads to get a 16-bit timer), and that calibrating TSC to
the PIT over 15ms should thus be sufficient to get much closer than
500ppm (max 2us error on both sides giving 4us over 15ms: a 270 ppm
error value).

However, that assumption does not always hold: apparently some hardware
is either very much slower at reading the PIT registers, or there was
other noise causing at least one machine to get 700+ ppm errors.

So instead of using a fixed 15ms timing loop, this changes the fast PIT
calibration to read the TSC delta over the individual PIT timer reads,
and use the result to calculate the error bars on the PIT read timing
properly.  We then successfully calibrate the TSC only if the maximum
error bars fall below 500ppm.

In the process, we also relax the timing to allow up to 25ms for the
calibration, although it can happen much faster depending on hardware.
Reported-and-tested-by: default avatarJesper Krogh <jesper@krogh.cc>
Cc: john stultz <johnstul@us.ibm.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Acked-by: default avatarIngo Molnar <mingo@elte.hu>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent a6a80e1d
...@@ -273,30 +273,43 @@ static unsigned long pit_calibrate_tsc(u32 latch, unsigned long ms, int loopmin) ...@@ -273,30 +273,43 @@ static unsigned long pit_calibrate_tsc(u32 latch, unsigned long ms, int loopmin)
* use the TSC value at the transitions to calculate a pretty * use the TSC value at the transitions to calculate a pretty
* good value for the TSC frequencty. * good value for the TSC frequencty.
*/ */
static inline int pit_expect_msb(unsigned char val) static inline int pit_expect_msb(unsigned char val, u64 *tscp, unsigned long *deltap)
{ {
int count = 0; int count;
u64 tsc = 0;
for (count = 0; count < 50000; count++) { for (count = 0; count < 50000; count++) {
/* Ignore LSB */ /* Ignore LSB */
inb(0x42); inb(0x42);
if (inb(0x42) != val) if (inb(0x42) != val)
break; break;
tsc = get_cycles();
} }
return count > 50; *deltap = get_cycles() - tsc;
*tscp = tsc;
/*
* We require _some_ success, but the quality control
* will be based on the error terms on the TSC values.
*/
return count > 5;
} }
/* /*
* How many MSB values do we want to see? We aim for a * How many MSB values do we want to see? We aim for
* 15ms calibration, which assuming a 2us counter read * a maximum error rate of 500ppm (in practice the
* error should give us roughly 150 ppm precision for * real error is much smaller), but refuse to spend
* the calibration. * more than 25ms on it.
*/ */
#define QUICK_PIT_MS 15 #define MAX_QUICK_PIT_MS 25
#define QUICK_PIT_ITERATIONS (QUICK_PIT_MS * PIT_TICK_RATE / 1000 / 256) #define MAX_QUICK_PIT_ITERATIONS (MAX_QUICK_PIT_MS * PIT_TICK_RATE / 1000 / 256)
static unsigned long quick_pit_calibrate(void) static unsigned long quick_pit_calibrate(void)
{ {
int i;
u64 tsc, delta;
unsigned long d1, d2;
/* Set the Gate high, disable speaker */ /* Set the Gate high, disable speaker */
outb((inb(0x61) & ~0x02) | 0x01, 0x61); outb((inb(0x61) & ~0x02) | 0x01, 0x61);
...@@ -324,45 +337,43 @@ static unsigned long quick_pit_calibrate(void) ...@@ -324,45 +337,43 @@ static unsigned long quick_pit_calibrate(void)
inb(0x42); inb(0x42);
inb(0x42); inb(0x42);
if (pit_expect_msb(0xff)) { if (pit_expect_msb(0xff, &tsc, &d1)) {
int i; for (i = 1; i <= MAX_QUICK_PIT_ITERATIONS; i++) {
u64 t1, t2, delta; if (!pit_expect_msb(0xff-i, &delta, &d2))
unsigned char expect = 0xfe; break;
t1 = get_cycles(); /*
for (i = 0; i < QUICK_PIT_ITERATIONS; i++, expect--) { * Iterate until the error is less than 500 ppm
if (!pit_expect_msb(expect)) */
goto failed; delta -= tsc;
if (d1+d2 < delta >> 11)
goto success;
} }
t2 = get_cycles();
/*
* Make sure we can rely on the second TSC timestamp:
*/
if (!pit_expect_msb(expect))
goto failed;
/*
* Ok, if we get here, then we've seen the
* MSB of the PIT decrement QUICK_PIT_ITERATIONS
* times, and each MSB had many hits, so we never
* had any sudden jumps.
*
* As a result, we can depend on there not being
* any odd delays anywhere, and the TSC reads are
* reliable.
*
* kHz = ticks / time-in-seconds / 1000;
* kHz = (t2 - t1) / (QPI * 256 / PIT_TICK_RATE) / 1000
* kHz = ((t2 - t1) * PIT_TICK_RATE) / (QPI * 256 * 1000)
*/
delta = (t2 - t1)*PIT_TICK_RATE;
do_div(delta, QUICK_PIT_ITERATIONS*256*1000);
printk("Fast TSC calibration using PIT\n");
return delta;
} }
failed: printk("Fast TSC calibration failed\n");
return 0; return 0;
success:
/*
* Ok, if we get here, then we've seen the
* MSB of the PIT decrement 'i' times, and the
* error has shrunk to less than 500 ppm.
*
* As a result, we can depend on there not being
* any odd delays anywhere, and the TSC reads are
* reliable (within the error). We also adjust the
* delta to the middle of the error bars, just
* because it looks nicer.
*
* kHz = ticks / time-in-seconds / 1000;
* kHz = (t2 - t1) / (I * 256 / PIT_TICK_RATE) / 1000
* kHz = ((t2 - t1) * PIT_TICK_RATE) / (I * 256 * 1000)
*/
delta += (long)(d2 - d1)/2;
delta *= PIT_TICK_RATE;
do_div(delta, i*256*1000);
printk("Fast TSC calibration using PIT\n");
return delta;
} }
/** /**
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment