mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Linus Torvalds <torvalds@linux-foundation.org>
To: Thomas Gleixner <tglx@linutronix.de>
Cc: Larry Finger <Larry.Finger@lwfinger.net>,
	LKML <linux-kernel@vger.kernel.org>,
	"Rafael J. Wysocki" <rjw@sisk.pl>,
	Alok Kataria <akataria@vmware.com>, Michael Buesch <mb@bu3sch.de>
Subject: Re: [PATCH] Fix TSC calibration issues
Date: Tue, 2 Sep 2008 19:14:06 -0700 (PDT)	[thread overview]
Message-ID: <alpine.LFD.1.10.0809021906250.3259@nehalem.linux-foundation.org> (raw)
In-Reply-To: <alpine.LFD.1.10.0809022358370.3243@apollo.tec.linutronix.de>



On Wed, 3 Sep 2008, Thomas Gleixner wrote:
>
> +	/*
> +	 * Run 5 calibration loops to get the lowest frequency value
> +	 * (the best estimate). We use two different calibration modes
> +	 * here:
> +	 *
> +	 * 1) PIT loop. We set the PIT Channel 2 to oneshot mode and
> +	 * load a timeout of 50ms. We read the time right after we
> +	 * started the timer and wait until the PIT count down reaches
> +	 * zero. In each wait loop iteration we read the TSC and check
> +	 * the delta to the previous read. We keep track of the min
> +	 * and max values of that delta. The delta is mostly defined
> +	 * by the IO time of the PIT access, so we can detect when a
> +	 * SMI/SMM disturbance happend between the two reads. If the
> +	 * maximum time is significantly larger than the minimum time,
> +	 * then we discard the result and have another try.
> +	 *
> +	 * 2) Reference counter. If available we use the HPET or the
> +	 * PMTIMER as a reference to check the sanity of that value.
> +	 * We use separate TSC readouts and check inside of the
> +	 * reference read for a SMI/SMM disturbance. We dicard
> +	 * disturbed values here as well. We do that around the PIT
> +	 * calibration delay loop as we have to wait for a certain
> +	 * amount of time anyway.
> +	 */
> +	for (i = 0; i < 5; i++) {
> +		tscmin = ULONG_MAX;
> +		tscmax = 0;
> +		pitcnt = 0;
> +
> +		local_irq_save(flags);
> +
> +		/*
> +		 * Read the start value and the reference count of
> +		 * hpet/pmtimer when available:
> +		 */
> +		tsc1 = tsc_read_refs(&pm1, hpet ? &hpet1 : NULL);

This is "wrongish".

You really should do the

	tsc1 = tsc_read_refs(&pm1, hpet ? &hpet1 : NULL);   
	...
	tsc2 = tsc_read_refs(&pm2, hpet ? &hpet2 : NULL);

around the whole loop, because they get more exact with more time inside, 
and they don't improve from looping around.

Also, that code is already _too_ unreadable. How about starting by just 
moving the PIT calibration into a function of its own, like the appended 
patch. And then as a separate patch, improving the heuristics for just the 
PIT calibration.

The others are independent of this issue..

		Linus

---
 arch/x86/kernel/tsc.c |   37 ++++++++++++++++++++-----------------
 1 files changed, 20 insertions(+), 17 deletions(-)

diff --git a/arch/x86/kernel/tsc.c b/arch/x86/kernel/tsc.c
index 8e786b0..bf7ff5a 100644
--- a/arch/x86/kernel/tsc.c
+++ b/arch/x86/kernel/tsc.c
@@ -122,19 +122,9 @@ static u64 tsc_read_refs(u64 *pm, u64 *hpet)
 	return ULLONG_MAX;
 }
 
-/**
- * native_calibrate_tsc - calibrate the tsc on boot
- */
-unsigned long native_calibrate_tsc(void)
+static unsigned long PIT_calibrate_tsc(void)
 {
-	unsigned long flags;
-	u64 tsc1, tsc2, tr1, tr2, delta, pm1, pm2, hpet1, hpet2;
-	int hpet = is_hpet_enabled();
-	unsigned int tsc_khz_val = 0;
-
-	local_irq_save(flags);
-
-	tsc1 = tsc_read_refs(&pm1, hpet ? &hpet1 : NULL);
+	u64 tr1, tr2, delta;
 
 	outb((inb(0x61) & ~0x02) | 0x01, 0x61);
 
@@ -145,17 +135,30 @@ unsigned long native_calibrate_tsc(void)
 	while ((inb(0x61) & 0x20) == 0);
 	tr2 = get_cycles();
 
-	tsc2 = tsc_read_refs(&pm2, hpet ? &hpet2 : NULL);
-
-	local_irq_restore(flags);
-
 	/*
 	 * Preset the result with the raw and inaccurate PIT
 	 * calibration value
 	 */
 	delta = (tr2 - tr1);
 	do_div(delta, 50);
-	tsc_khz_val = delta;
+	return delta;
+}
+
+/**
+ * native_calibrate_tsc - calibrate the tsc on boot
+ */
+unsigned long native_calibrate_tsc(void)
+{
+	unsigned long flags;
+	u64 tsc1, tsc2, pm1, pm2, hpet1, hpet2;
+	int hpet = is_hpet_enabled();
+	unsigned int tsc_khz_val;
+
+	local_irq_save(flags);
+	tsc1 = tsc_read_refs(&pm1, hpet ? &hpet1 : NULL);
+	tsc_khz_val = PIT_calibrate_tsc();
+	tsc2 = tsc_read_refs(&pm2, hpet ? &hpet2 : NULL);
+	local_irq_restore(flags);
 
 	/* hpet or pmtimer available ? */
 	if (!hpet && !pm1 && !pm2) {

  reply	other threads:[~2008-09-03  2:15 UTC|newest]

Thread overview: 56+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-08-31 22:54 Regression in 2.6.27 caused by commit bfc0f59 Larry Finger
2008-09-01 11:14 ` Thomas Gleixner
2008-09-01 15:37   ` Larry Finger
2008-09-01 17:49     ` Thomas Gleixner
2008-09-01 17:44   ` Larry Finger
2008-09-01 18:31     ` Thomas Gleixner
2008-09-01 19:10       ` Linus Torvalds
2008-09-01 20:07         ` Thomas Gleixner
2008-09-01 21:30           ` Thomas Gleixner
2008-09-01 22:02           ` Linus Torvalds
2008-09-01 22:33             ` Thomas Gleixner
2008-09-01 22:56               ` Linus Torvalds
2008-09-01 23:24                 ` Thomas Gleixner
2008-09-02  6:37                   ` Andi Kleen
2008-09-02 12:21                     ` Thomas Gleixner
2008-09-01 22:16           ` Linus Torvalds
2008-09-01 23:16             ` Thomas Gleixner
2008-09-02  3:18               ` Linus Torvalds
2008-09-02  3:35                 ` Linus Torvalds
2008-09-02  4:54                   ` Larry Finger
2008-09-02  9:17                   ` Alan Cox
2008-09-02 12:15                   ` Thomas Gleixner
2008-09-02 15:09                     ` Linus Torvalds
2008-09-02 18:14                       ` Thomas Gleixner
2008-09-02 18:41                         ` Alok Kataria
2008-09-02 21:16                           ` Thomas Gleixner
2008-09-02 18:42                         ` Linus Torvalds
2008-09-02 21:13                           ` Thomas Gleixner
2008-09-02 22:21                             ` Linus Torvalds
2008-09-02 23:10                               ` Thomas Gleixner
2008-09-03  1:49                                 ` Linus Torvalds
2008-09-02 22:54                           ` [PATCH] Fix TSC calibration issues Thomas Gleixner
2008-09-03  2:14                             ` Linus Torvalds [this message]
2008-09-03  9:11                               ` Thomas Gleixner
2008-09-04  1:14                                 ` Alok Kataria
2008-09-04  2:56                                   ` Linus Torvalds
2008-09-04  3:16                                     ` Arjan van de Ven
2008-09-04  3:59                                       ` Linus Torvalds
2008-09-04  4:10                                         ` Arjan van de Ven
2008-09-04  4:20                                           ` Linus Torvalds
2008-09-04  4:27                                             ` Arjan van de Ven
2008-09-04  4:25                                         ` Willy Tarreau
2008-09-04  4:53                                           ` Linus Torvalds
2008-09-04  5:09                                             ` Willy Tarreau
2008-09-04  1:18                                 ` [PATCH] Change warning message in TSC calibration Alok Kataria
2008-09-03  2:51                             ` [PATCH] Fix TSC calibration issues Larry Finger
2008-09-03  4:00                               ` Linus Torvalds
2008-09-03  4:34                                 ` Larry Finger
2008-09-05 13:45                       ` Regression in 2.6.27 caused by commit bfc0f59 Mark Lord
2008-09-02 17:17                 ` Bill Davidsen
2008-09-01 19:36       ` Larry Finger
2008-09-01 20:09         ` Thomas Gleixner
2008-09-01 20:23           ` Larry Finger
2008-09-01 20:45             ` Thomas Gleixner
2008-09-01 18:42     ` Linus Torvalds
2008-09-01 19:08       ` Thomas Gleixner

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.1.10.0809021906250.3259@nehalem.linux-foundation.org \
    --to=torvalds@linux-foundation.org \
    --cc=Larry.Finger@lwfinger.net \
    --cc=akataria@vmware.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mb@bu3sch.de \
    --cc=rjw@sisk.pl \
    --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

Powered by JetHome