mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Linus Torvalds <torvalds@linux-foundation.org>
To: Jesper Krogh <jesper@krogh.cc>
Cc: john stultz <johnstul@us.ibm.com>,
	Thomas Gleixner <tglx@linutronix.de>,
	Linux Kernel Mailing List <linux-kernel@vger.kernel.org>,
	Len Brown <len.brown@intel.com>
Subject: Re: Linux 2.6.29-rc6
Date: Sat, 14 Mar 2009 18:19:29 -0700 (PDT)	[thread overview]
Message-ID: <alpine.LFD.2.00.0903141814180.7421@localhost.localdomain> (raw)
In-Reply-To: <49B57F3D.5030008@krogh.cc>



Jesper, here's a patch that actually tries to take teh TSC error really 
into account, and which I suspect will result (on your machine) in failing 
the fast PIT calibration. 

It also has a few extra printk's for debugging, and to see just what the 
values are on your machine.

The idea behind the patch is to just keep track of how big the difference 
was in TSC values between two successive reads of the PIT timer. We only 
really care about the difference when the MSB turns around, and we only 
really care about the two end points. The maximum error in TSC estimation 
will simply be the sum of the differences at those points (d1 and d2).

We can then compare the maximum error with the actual TSC differences 
between those points, and see if the max error is within 500 ppm. That 
_should_ mean that it all works - assuming that the PIT itself is running 
at the correct frequency, of course!

Regardless of whether is succeeds or not, it will print out some debug 
messages, which will be interesting to see.

What's nice about this is that it really should make that whole "yes, it's 
really within 500ppm" assertion have some solid legs to stand on. Rather 
than depend on us being able to read the PIT a certain number of times, we 
can literally give an estimation of the max error.

		Linus

---
 arch/x86/kernel/tsc.c |   41 +++++++++++++++++++++++++++++------------
 1 files changed, 29 insertions(+), 12 deletions(-)

diff --git a/arch/x86/kernel/tsc.c b/arch/x86/kernel/tsc.c
index 599e581..8e1db42 100644
--- a/arch/x86/kernel/tsc.c
+++ b/arch/x86/kernel/tsc.c
@@ -273,17 +273,26 @@ static unsigned long pit_calibrate_tsc(u32 latch, unsigned long ms, int loopmin)
  * use the TSC value at the transitions to calculate a pretty
  * good value for the TSC frequencty.
  */
-static inline int pit_expect_msb(unsigned char val)
+static unsigned long 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++) {
 		/* Ignore LSB */
 		inb(0x42);
 		if (inb(0x42) != val)
 			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;
 }
 
 /*
@@ -297,6 +306,10 @@ static inline int pit_expect_msb(unsigned char val)
 
 static unsigned long quick_pit_calibrate(void)
 {
+	u64 t1, t2;
+	unsigned long d1, d2;
+	unsigned char expect = 0xff;
+
 	/* Set the Gate high, disable speaker */
 	outb((inb(0x61) & ~0x02) | 0x01, 0x61);
 
@@ -315,22 +328,24 @@ static unsigned long quick_pit_calibrate(void)
 	outb(0xff, 0x42);
 	outb(0xff, 0x42);
 
-	if (pit_expect_msb(0xff)) {
+	if (pit_expect_msb(0xff, &t1, &d1)) {
 		int i;
-		u64 t1, t2, delta;
-		unsigned char expect = 0xfe;
+		u64 delta;
 
-		t1 = get_cycles();
+		expect--;
 		for (i = 0; i < QUICK_PIT_ITERATIONS; i++, expect--) {
-			if (!pit_expect_msb(expect))
+			if (!pit_expect_msb(expect, &t2, &d2))
 				goto failed;
 		}
-		t2 = get_cycles();
 
 		/*
-		 * Make sure we can rely on the second TSC timestamp:
+		 * We require the max error on the calibration to be
+		 * within 500 ppm, since that's the limit of ntpd
+		 * drift correction. So the TSC delta must be more
+		 * than 2000x the possible error term (d1+d2).
 		 */
-		if (!pit_expect_msb(expect))
+		delta = t2 - t1;
+		if (d1+d2 > delta >> 11)
 			goto failed;
 
 		/*
@@ -347,12 +362,14 @@ static unsigned long quick_pit_calibrate(void)
 		 * kHz = (t2 - t1) / (QPI * 256 / PIT_TICK_RATE) / 1000
 		 * kHz = ((t2 - t1) * PIT_TICK_RATE) / (QPI * 256 * 1000)
 		 */
-		delta = (t2 - t1)*PIT_TICK_RATE;
+		printk("Fast TSC delta=%lld, error=%lu+%lu=%lu\n", delta, d1, d2, d1+d2);
+		delta *= 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 at %u %llu(%lu) %llu(%lu)\n", expect, t1, d1, t2, d2);
 	return 0;
 }
 

  parent reply	other threads:[~2009-03-15  1:22 UTC|newest]

Thread overview: 81+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-02-23  4:31 Linus Torvalds
2009-02-23 14:07 ` Linux 2.6.29-rc6 - Fix oops in i915_gem_retire_requests Karsten Wiese
2009-02-26 11:15 ` Linux 2.6.29-rc6 Jesper Krogh
2009-02-26 17:17   ` MTD_CK804XROM warning (Was: Linux 2.6.29-rc6) Marcin Slusarz
2009-02-26 17:53   ` Linux 2.6.29-rc6 Linus Torvalds
2009-02-26 19:22     ` David Woodhouse
2009-02-26 19:31     ` Jesper Krogh
2009-02-26 19:36       ` David Woodhouse
2009-02-26 19:46         ` Jesper Krogh
2009-02-26 19:49           ` David Woodhouse
2009-02-26 20:53         ` Carl-Daniel Hailfinger
2009-02-26 20:32       ` Linus Torvalds
2009-02-26 19:55 ` Jesper Krogh
2009-02-26 20:33   ` Linus Torvalds
2009-02-26 20:43     ` Jesper Krogh
2009-02-26 21:19       ` john stultz
2009-02-26 21:35         ` Jesper Krogh
2009-02-26 21:46           ` john stultz
2009-02-26 21:54             ` Thomas Gleixner
2009-02-26 22:04               ` Jesper Krogh
2009-02-27  6:30             ` Jesper Krogh
2009-03-01 13:51             ` Jesper Krogh
2009-02-26 21:49           ` Linus Torvalds
2009-03-01 15:04             ` Jesper Krogh
2009-02-26 21:54           ` john stultz
2009-02-26 22:06             ` Thomas Gleixner
2009-02-26 22:24               ` Linus Torvalds
2009-02-26 22:31                 ` Linus Torvalds
2009-02-26 22:31               ` john stultz
2009-02-26 22:40                 ` Linus Torvalds
2009-02-26 22:59                   ` john stultz
2009-02-27  7:33                     ` Ingo Molnar
2009-02-27 20:50                       ` john stultz
2009-02-27  6:47                 ` Jesper Krogh
2009-02-27 20:35                   ` john stultz
2009-03-01 20:13                     ` Jesper Krogh
2009-03-02  9:53                     ` Jesper Krogh
2009-03-02 21:27                       ` john stultz
2009-03-03  6:04                         ` Jesper Krogh
2009-03-03 19:53                           ` john stultz
2009-03-03 20:19                             ` Jesper Krogh
2009-03-03 22:22                               ` john stultz
2009-03-04 15:30                                 ` Jesper Krogh
2009-03-04 18:36                                   ` Jesper Krogh
2009-03-04 18:57                                     ` John Stultz
2009-03-05  2:39                                       ` john stultz
2009-03-05  2:52                                         ` john stultz
2009-03-05  8:43                                           ` Ingo Molnar
2009-03-06  3:13                                             ` john stultz
2009-03-06  3:54                                               ` john stultz
2009-03-06 11:34                                                 ` Ingo Molnar
2009-03-09 20:42                                           ` Jesper Krogh
2009-03-10  4:26                                             ` Linus Torvalds
2009-03-10 11:29                                               ` Thomas Gleixner
2009-03-10 19:42                                                 ` Jesper Krogh
2009-03-10 22:22                                                   ` Thomas Gleixner
2009-03-15 19:53                                                     ` Jesper Krogh
2009-03-16 18:40                                                       ` Jesper Krogh
2009-03-15  1:19                                             ` Linus Torvalds [this message]
2009-03-15 15:44                                               ` Jesper Krogh
2009-03-15 18:09                                                 ` Linus Torvalds
2009-03-15 18:38                                                   ` Jesper Krogh
2009-03-15 19:02                                                     ` Linus Torvalds
2009-03-15 19:52                                                       ` Jesper Krogh
2009-03-16 18:59                                                         ` Jesper Krogh
2009-03-16 19:32                                                           ` Linus Torvalds
2009-03-17  1:43                                                             ` john stultz
2009-03-17  8:14                                                             ` Ingo Molnar
2009-03-17 15:48                                                               ` Linus Torvalds
2009-03-17 16:13                                                                 ` Ingo Molnar
2009-03-17 16:28                                                                   ` Linus Torvalds
2009-03-17 16:40                                                                     ` Ingo Molnar
2009-03-17 17:28                                                                   ` Olivier Galibert
2009-03-21  9:11                                                             ` Jesper Krogh
2009-03-21 10:06                                                               ` Ingo Molnar
2009-03-15 20:32                                                     ` Linus Torvalds
2009-03-03 20:39                             ` Jesper Krogh
2009-03-03 22:16                               ` john stultz
2009-03-04  5:36                                 ` Jesper Krogh
2009-03-01 15:09   ` Jesper Krogh
2009-03-01 15:44     ` Linux 2.6.29-rc6 (clocksource) Sitsofe Wheeler

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=alpine.LFD.2.00.0903141814180.7421@localhost.localdomain \
    --to=torvalds@linux-foundation.org \
    --cc=jesper@krogh.cc \
    --cc=johnstul@us.ibm.com \
    --cc=len.brown@intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=tglx@linutronix.de \
    /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

all inboxes | Powered by JetHome®