From: Martin Wilck <martin.wilck@fujitsu-siemens.com>
To: Cyrill Gorcunov <gorcunov@gmail.com>
Cc: Thomas Gleixner <tglx@linutronix.de>,
"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
"H. Peter Anvin" <hpa@zytor.com>,
"Wichert, Gerhard" <Gerhard.Wichert@fujitsu-siemens.com>,
"Maciej W. Rozycki" <macro@linux-mips.org>
Subject: Re: [PATCH] x86 (64): make calibrate_APIC_clock() SMI-safe (take 3)
Date: Fri, 25 Jul 2008 17:13:21 +0200 [thread overview]
Message-ID: <4889ED91.7050800@fujitsu-siemens.com> (raw)
In-Reply-To: <20080725150104.GD28466@lenovo>
[-- Attachment #1: Type: text/plain, Size: 469 bytes --]
Cyrill Gorcunov wrote:
> Martin, it seems you forgot to attach the patch :)
I am sorry, here it is. Thanks :)
Martin
--
Martin Wilck
PRIMERGY System Software Engineer
FSC IP ESP DEV 6
Fujitsu Siemens Computers GmbH
Heinz-Nixdorf-Ring 1
33106 Paderborn
Germany
Tel: ++49 5251 8 15113
Fax: ++49 5251 8 20209
Email: mailto:martin.wilck@fujitsu-siemens.com
Internet: http://www.fujitsu-siemens.com
Company Details: http://www.fujitsu-siemens.com/imprint.html
[-- Attachment #2: calibrate_APIC_clock-4a.diff --]
[-- Type: text/x-patch, Size: 3034 bytes --]
[PATCH] x86 (64): make calibrate_APIC_clock() SMI-safe (take 3)
Non-maskable asynchronous events (e.g. SMIs) which occur during the APIC
timer calibration can cause timer miscalibrations, sometimes by large amounts.
This patch fixes this by making sure that no significant interruption occurs
between APIC and TSC reads. SMIs may still occur at some stage in the
calibration loop, causing the loop to last longer than intended. This
doesn't matter though, as long as the start and end values are both
taken simultaneously.
Changed wrt take 2: Use max. possible start value for the APIC timer
to avoid underflow.
Signed-off-by: Martin Wilck <martin.wilck@fujitsu-siemens.com>
Signed-off-by: Gerhard Wichert <gerhard.wichert@fujitsu-siemens.com>
--- arch/x86/kernel/apic_64.c 2008-07-25 15:39:51.000000000 +0200
+++ arch/x86/kernel/apic_64.c.new 2008-07-25 15:55:08.000000000 +0200
@@ -300,6 +300,31 @@ static void setup_APIC_timer(void)
}
/*
+ * Helper function for calibrate_APIC_clock(): Make sure that
+ * APIC TMCTT and TSC are read at the same time, to reasonable
+ * accuracy. On any sane system, the retry loop won't need more
+ * than a single retry, given that the rdtsc/apic_read/rdtsc
+ * sequence won't take more than a few cycles.
+ */
+
+#define MAX_DIFFERENCE 1000UL
+#define MAX_ITER 10
+static inline int
+__read_tsc_and_apic(unsigned long *tsc, unsigned *apic)
+{
+ unsigned long tsc0, tsc1, diff;
+ int i = 0;
+ do {
+ rdtscll(tsc0);
+ *apic = apic_read(APIC_TMCCT);
+ rdtscll(tsc1);
+ diff = tsc1 - tsc0;
+ } while (diff > MAX_DIFFERENCE && ++i < MAX_ITER);
+ *tsc = tsc0 + (diff >> 1);
+ return diff > MAX_DIFFERENCE ? -EIO : 0;
+}
+
+/*
* In this function we calibrate APIC bus clocks to the external
* timer. Unfortunately we cannot use jiffies and the timer irq
* to calibrate, since some later bootup code depends on getting
@@ -318,7 +343,7 @@ static void __init calibrate_APIC_clock(
{
unsigned apic, apic_start;
unsigned long tsc, tsc_start;
- int result;
+ int result, err_start, err;
local_irq_disable();
@@ -329,25 +354,27 @@ static void __init calibrate_APIC_clock(
*
* No interrupt enable !
*/
- __setup_APIC_LVTT(250000000, 0, 0);
+ __setup_APIC_LVTT(0xffffffff, 0, 0);
- apic_start = apic_read(APIC_TMCCT);
#ifdef CONFIG_X86_PM_TIMER
if (apic_calibrate_pmtmr && pmtmr_ioport) {
+ apic_start = apic_read(APIC_TMCCT);
pmtimer_wait(5000); /* 5ms wait */
apic = apic_read(APIC_TMCCT);
result = (apic_start - apic) * 1000L / 5;
} else
#endif
{
- rdtscll(tsc_start);
+ err_start = __read_tsc_and_apic(&tsc_start, &apic_start);
do {
- apic = apic_read(APIC_TMCCT);
- rdtscll(tsc);
+ err = __read_tsc_and_apic(&tsc, &apic);
} while ((tsc - tsc_start) < TICK_COUNT &&
(apic_start - apic) < TICK_COUNT);
+ if (err_start || err)
+ printk(KERN_CRIT "calibrate_APIC_clock: SMI flood - "
+ "the APIC timer calibration may be wrong!\n");
result = (apic_start - apic) * 1000L * tsc_khz /
(tsc - tsc_start);
}
next prev parent reply other threads:[~2008-07-25 15:13 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2008-07-24 10:47 [PATCH] x86 (64): make calibrate_APIC_clock() smp-safe Martin Wilck
2008-07-24 11:16 ` Cyrill Gorcunov
2008-07-24 11:58 ` Martin Wilck
2008-07-24 12:05 ` Cyrill Gorcunov
2008-07-24 13:55 ` [PATCH] x86 (64): make calibrate_APIC_clock() SMI-safe Martin Wilck
2008-07-24 14:31 ` Cyrill Gorcunov
2008-07-24 15:01 ` Cyrill Gorcunov
2008-07-24 15:13 ` Martin Wilck
2008-07-25 9:02 ` [PATCH] x86 (64): make calibrate_APIC_clock() SMI-safe (take 2) Martin Wilck
2008-07-25 10:08 ` Cyrill Gorcunov
2008-07-25 12:29 ` Martin Wilck
2008-07-25 12:59 ` Cyrill Gorcunov
2008-07-25 13:38 ` Martin Wilck
2008-07-25 13:48 ` Cyrill Gorcunov
2008-07-25 14:01 ` [PATCH] x86 (64): make calibrate_APIC_clock() SMI-safe (take 3) Martin Wilck
2008-07-25 14:15 ` Cyrill Gorcunov
2008-07-25 15:01 ` Cyrill Gorcunov
2008-07-25 15:13 ` Martin Wilck [this message]
2008-07-25 15:39 ` Cyrill Gorcunov
2008-07-26 15:40 ` Ingo Molnar
2009-03-12 9:41 ` Jean Delvare
2009-03-12 13:38 ` Martin Wilck
2008-07-25 16:51 ` [PATCH] x86 (64): make calibrate_APIC_clock() SMI-safe (take 2) Olaf Dabrunz
2008-07-24 13:31 ` [PATCH] x86 (64): make calibrate_APIC_clock() smp-safe H. Peter Anvin
2008-07-24 13:42 ` [PATCH] x86 (64): make calibrate_APIC_clock() SMI-safe Martin Wilck
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=4889ED91.7050800@fujitsu-siemens.com \
--to=martin.wilck@fujitsu-siemens.com \
--cc=Gerhard.Wichert@fujitsu-siemens.com \
--cc=gorcunov@gmail.com \
--cc=hpa@zytor.com \
--cc=linux-kernel@vger.kernel.org \
--cc=macro@linux-mips.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