xref: /openbmc/linux/arch/x86/kernel/tsc.c (revision 0a3aee0d)
1bfc0f594SAlok Kataria #include <linux/kernel.h>
20ef95533SAlok Kataria #include <linux/sched.h>
30ef95533SAlok Kataria #include <linux/init.h>
40ef95533SAlok Kataria #include <linux/module.h>
50ef95533SAlok Kataria #include <linux/timer.h>
6bfc0f594SAlok Kataria #include <linux/acpi_pmtmr.h>
72dbe06faSAlok Kataria #include <linux/cpufreq.h>
88fbbc4b4SAlok Kataria #include <linux/dmi.h>
98fbbc4b4SAlok Kataria #include <linux/delay.h>
108fbbc4b4SAlok Kataria #include <linux/clocksource.h>
118fbbc4b4SAlok Kataria #include <linux/percpu.h>
1208604bd9SArnd Bergmann #include <linux/timex.h>
13bfc0f594SAlok Kataria 
14bfc0f594SAlok Kataria #include <asm/hpet.h>
158fbbc4b4SAlok Kataria #include <asm/timer.h>
168fbbc4b4SAlok Kataria #include <asm/vgtod.h>
178fbbc4b4SAlok Kataria #include <asm/time.h>
188fbbc4b4SAlok Kataria #include <asm/delay.h>
1988b094fbSAlok Kataria #include <asm/hypervisor.h>
2008047c4fSThomas Gleixner #include <asm/nmi.h>
212d826404SThomas Gleixner #include <asm/x86_init.h>
220ef95533SAlok Kataria 
23f24ade3aSIngo Molnar unsigned int __read_mostly cpu_khz;	/* TSC clocks / usec, not used here */
240ef95533SAlok Kataria EXPORT_SYMBOL(cpu_khz);
25f24ade3aSIngo Molnar 
26f24ade3aSIngo Molnar unsigned int __read_mostly tsc_khz;
270ef95533SAlok Kataria EXPORT_SYMBOL(tsc_khz);
280ef95533SAlok Kataria 
290ef95533SAlok Kataria /*
300ef95533SAlok Kataria  * TSC can be unstable due to cpufreq or due to unsynced TSCs
310ef95533SAlok Kataria  */
32f24ade3aSIngo Molnar static int __read_mostly tsc_unstable;
330ef95533SAlok Kataria 
340ef95533SAlok Kataria /* native_sched_clock() is called before tsc_init(), so
350ef95533SAlok Kataria    we must start with the TSC soft disabled to prevent
360ef95533SAlok Kataria    erroneous rdtsc usage on !cpu_has_tsc processors */
37f24ade3aSIngo Molnar static int __read_mostly tsc_disabled = -1;
380ef95533SAlok Kataria 
39395628efSAlok Kataria static int tsc_clocksource_reliable;
400ef95533SAlok Kataria /*
410ef95533SAlok Kataria  * Scheduler clock - returns current time in nanosec units.
420ef95533SAlok Kataria  */
430ef95533SAlok Kataria u64 native_sched_clock(void)
440ef95533SAlok Kataria {
450ef95533SAlok Kataria 	u64 this_offset;
460ef95533SAlok Kataria 
470ef95533SAlok Kataria 	/*
480ef95533SAlok Kataria 	 * Fall back to jiffies if there's no TSC available:
490ef95533SAlok Kataria 	 * ( But note that we still use it if the TSC is marked
500ef95533SAlok Kataria 	 *   unstable. We do this because unlike Time Of Day,
510ef95533SAlok Kataria 	 *   the scheduler clock tolerates small errors and it's
520ef95533SAlok Kataria 	 *   very important for it to be as fast as the platform
533ad2f3fbSDaniel Mack 	 *   can achieve it. )
540ef95533SAlok Kataria 	 */
550ef95533SAlok Kataria 	if (unlikely(tsc_disabled)) {
560ef95533SAlok Kataria 		/* No locking but a rare wrong value is not a big deal: */
570ef95533SAlok Kataria 		return (jiffies_64 - INITIAL_JIFFIES) * (1000000000 / HZ);
580ef95533SAlok Kataria 	}
590ef95533SAlok Kataria 
600ef95533SAlok Kataria 	/* read the Time Stamp Counter: */
610ef95533SAlok Kataria 	rdtscll(this_offset);
620ef95533SAlok Kataria 
630ef95533SAlok Kataria 	/* return the value in ns */
647cbaef9cSIngo Molnar 	return __cycles_2_ns(this_offset);
650ef95533SAlok Kataria }
660ef95533SAlok Kataria 
670ef95533SAlok Kataria /* We need to define a real function for sched_clock, to override the
680ef95533SAlok Kataria    weak default version */
690ef95533SAlok Kataria #ifdef CONFIG_PARAVIRT
700ef95533SAlok Kataria unsigned long long sched_clock(void)
710ef95533SAlok Kataria {
720ef95533SAlok Kataria 	return paravirt_sched_clock();
730ef95533SAlok Kataria }
740ef95533SAlok Kataria #else
750ef95533SAlok Kataria unsigned long long
760ef95533SAlok Kataria sched_clock(void) __attribute__((alias("native_sched_clock")));
770ef95533SAlok Kataria #endif
780ef95533SAlok Kataria 
790ef95533SAlok Kataria int check_tsc_unstable(void)
800ef95533SAlok Kataria {
810ef95533SAlok Kataria 	return tsc_unstable;
820ef95533SAlok Kataria }
830ef95533SAlok Kataria EXPORT_SYMBOL_GPL(check_tsc_unstable);
840ef95533SAlok Kataria 
850ef95533SAlok Kataria #ifdef CONFIG_X86_TSC
860ef95533SAlok Kataria int __init notsc_setup(char *str)
870ef95533SAlok Kataria {
880ef95533SAlok Kataria 	printk(KERN_WARNING "notsc: Kernel compiled with CONFIG_X86_TSC, "
890ef95533SAlok Kataria 			"cannot disable TSC completely.\n");
900ef95533SAlok Kataria 	tsc_disabled = 1;
910ef95533SAlok Kataria 	return 1;
920ef95533SAlok Kataria }
930ef95533SAlok Kataria #else
940ef95533SAlok Kataria /*
950ef95533SAlok Kataria  * disable flag for tsc. Takes effect by clearing the TSC cpu flag
960ef95533SAlok Kataria  * in cpu/common.c
970ef95533SAlok Kataria  */
980ef95533SAlok Kataria int __init notsc_setup(char *str)
990ef95533SAlok Kataria {
1000ef95533SAlok Kataria 	setup_clear_cpu_cap(X86_FEATURE_TSC);
1010ef95533SAlok Kataria 	return 1;
1020ef95533SAlok Kataria }
1030ef95533SAlok Kataria #endif
1040ef95533SAlok Kataria 
1050ef95533SAlok Kataria __setup("notsc", notsc_setup);
106bfc0f594SAlok Kataria 
107e82b8e4eSVenkatesh Pallipadi static int no_sched_irq_time;
108e82b8e4eSVenkatesh Pallipadi 
109395628efSAlok Kataria static int __init tsc_setup(char *str)
110395628efSAlok Kataria {
111395628efSAlok Kataria 	if (!strcmp(str, "reliable"))
112395628efSAlok Kataria 		tsc_clocksource_reliable = 1;
113e82b8e4eSVenkatesh Pallipadi 	if (!strncmp(str, "noirqtime", 9))
114e82b8e4eSVenkatesh Pallipadi 		no_sched_irq_time = 1;
115395628efSAlok Kataria 	return 1;
116395628efSAlok Kataria }
117395628efSAlok Kataria 
118395628efSAlok Kataria __setup("tsc=", tsc_setup);
119395628efSAlok Kataria 
120bfc0f594SAlok Kataria #define MAX_RETRIES     5
121bfc0f594SAlok Kataria #define SMI_TRESHOLD    50000
122bfc0f594SAlok Kataria 
123bfc0f594SAlok Kataria /*
124bfc0f594SAlok Kataria  * Read TSC and the reference counters. Take care of SMI disturbance
125bfc0f594SAlok Kataria  */
126827014beSThomas Gleixner static u64 tsc_read_refs(u64 *p, int hpet)
127bfc0f594SAlok Kataria {
128bfc0f594SAlok Kataria 	u64 t1, t2;
129bfc0f594SAlok Kataria 	int i;
130bfc0f594SAlok Kataria 
131bfc0f594SAlok Kataria 	for (i = 0; i < MAX_RETRIES; i++) {
132bfc0f594SAlok Kataria 		t1 = get_cycles();
133bfc0f594SAlok Kataria 		if (hpet)
134827014beSThomas Gleixner 			*p = hpet_readl(HPET_COUNTER) & 0xFFFFFFFF;
135bfc0f594SAlok Kataria 		else
136827014beSThomas Gleixner 			*p = acpi_pm_read_early();
137bfc0f594SAlok Kataria 		t2 = get_cycles();
138bfc0f594SAlok Kataria 		if ((t2 - t1) < SMI_TRESHOLD)
139bfc0f594SAlok Kataria 			return t2;
140bfc0f594SAlok Kataria 	}
141bfc0f594SAlok Kataria 	return ULLONG_MAX;
142bfc0f594SAlok Kataria }
143bfc0f594SAlok Kataria 
144ec0c15afSLinus Torvalds /*
145d683ef7aSThomas Gleixner  * Calculate the TSC frequency from HPET reference
146d683ef7aSThomas Gleixner  */
147d683ef7aSThomas Gleixner static unsigned long calc_hpet_ref(u64 deltatsc, u64 hpet1, u64 hpet2)
148d683ef7aSThomas Gleixner {
149d683ef7aSThomas Gleixner 	u64 tmp;
150d683ef7aSThomas Gleixner 
151d683ef7aSThomas Gleixner 	if (hpet2 < hpet1)
152d683ef7aSThomas Gleixner 		hpet2 += 0x100000000ULL;
153d683ef7aSThomas Gleixner 	hpet2 -= hpet1;
154d683ef7aSThomas Gleixner 	tmp = ((u64)hpet2 * hpet_readl(HPET_PERIOD));
155d683ef7aSThomas Gleixner 	do_div(tmp, 1000000);
156d683ef7aSThomas Gleixner 	do_div(deltatsc, tmp);
157d683ef7aSThomas Gleixner 
158d683ef7aSThomas Gleixner 	return (unsigned long) deltatsc;
159d683ef7aSThomas Gleixner }
160d683ef7aSThomas Gleixner 
161d683ef7aSThomas Gleixner /*
162d683ef7aSThomas Gleixner  * Calculate the TSC frequency from PMTimer reference
163d683ef7aSThomas Gleixner  */
164d683ef7aSThomas Gleixner static unsigned long calc_pmtimer_ref(u64 deltatsc, u64 pm1, u64 pm2)
165d683ef7aSThomas Gleixner {
166d683ef7aSThomas Gleixner 	u64 tmp;
167d683ef7aSThomas Gleixner 
168d683ef7aSThomas Gleixner 	if (!pm1 && !pm2)
169d683ef7aSThomas Gleixner 		return ULONG_MAX;
170d683ef7aSThomas Gleixner 
171d683ef7aSThomas Gleixner 	if (pm2 < pm1)
172d683ef7aSThomas Gleixner 		pm2 += (u64)ACPI_PM_OVRRUN;
173d683ef7aSThomas Gleixner 	pm2 -= pm1;
174d683ef7aSThomas Gleixner 	tmp = pm2 * 1000000000LL;
175d683ef7aSThomas Gleixner 	do_div(tmp, PMTMR_TICKS_PER_SEC);
176d683ef7aSThomas Gleixner 	do_div(deltatsc, tmp);
177d683ef7aSThomas Gleixner 
178d683ef7aSThomas Gleixner 	return (unsigned long) deltatsc;
179d683ef7aSThomas Gleixner }
180d683ef7aSThomas Gleixner 
181a977c400SThomas Gleixner #define CAL_MS		10
182cce3e057SThomas Gleixner #define CAL_LATCH	(CLOCK_TICK_RATE / (1000 / CAL_MS))
183a977c400SThomas Gleixner #define CAL_PIT_LOOPS	1000
184a977c400SThomas Gleixner 
185a977c400SThomas Gleixner #define CAL2_MS		50
186a977c400SThomas Gleixner #define CAL2_LATCH	(CLOCK_TICK_RATE / (1000 / CAL2_MS))
187a977c400SThomas Gleixner #define CAL2_PIT_LOOPS	5000
188a977c400SThomas Gleixner 
189cce3e057SThomas Gleixner 
190ec0c15afSLinus Torvalds /*
191ec0c15afSLinus Torvalds  * Try to calibrate the TSC against the Programmable
192ec0c15afSLinus Torvalds  * Interrupt Timer and return the frequency of the TSC
193ec0c15afSLinus Torvalds  * in kHz.
194ec0c15afSLinus Torvalds  *
195ec0c15afSLinus Torvalds  * Return ULONG_MAX on failure to calibrate.
196ec0c15afSLinus Torvalds  */
197a977c400SThomas Gleixner static unsigned long pit_calibrate_tsc(u32 latch, unsigned long ms, int loopmin)
198ec0c15afSLinus Torvalds {
199ec0c15afSLinus Torvalds 	u64 tsc, t1, t2, delta;
200ec0c15afSLinus Torvalds 	unsigned long tscmin, tscmax;
201ec0c15afSLinus Torvalds 	int pitcnt;
202ec0c15afSLinus Torvalds 
203ec0c15afSLinus Torvalds 	/* Set the Gate high, disable speaker */
204ec0c15afSLinus Torvalds 	outb((inb(0x61) & ~0x02) | 0x01, 0x61);
205ec0c15afSLinus Torvalds 
206ec0c15afSLinus Torvalds 	/*
207ec0c15afSLinus Torvalds 	 * Setup CTC channel 2* for mode 0, (interrupt on terminal
208ec0c15afSLinus Torvalds 	 * count mode), binary count. Set the latch register to 50ms
209ec0c15afSLinus Torvalds 	 * (LSB then MSB) to begin countdown.
210ec0c15afSLinus Torvalds 	 */
211ec0c15afSLinus Torvalds 	outb(0xb0, 0x43);
212a977c400SThomas Gleixner 	outb(latch & 0xff, 0x42);
213a977c400SThomas Gleixner 	outb(latch >> 8, 0x42);
214ec0c15afSLinus Torvalds 
215ec0c15afSLinus Torvalds 	tsc = t1 = t2 = get_cycles();
216ec0c15afSLinus Torvalds 
217ec0c15afSLinus Torvalds 	pitcnt = 0;
218ec0c15afSLinus Torvalds 	tscmax = 0;
219ec0c15afSLinus Torvalds 	tscmin = ULONG_MAX;
220ec0c15afSLinus Torvalds 	while ((inb(0x61) & 0x20) == 0) {
221ec0c15afSLinus Torvalds 		t2 = get_cycles();
222ec0c15afSLinus Torvalds 		delta = t2 - tsc;
223ec0c15afSLinus Torvalds 		tsc = t2;
224ec0c15afSLinus Torvalds 		if ((unsigned long) delta < tscmin)
225ec0c15afSLinus Torvalds 			tscmin = (unsigned int) delta;
226ec0c15afSLinus Torvalds 		if ((unsigned long) delta > tscmax)
227ec0c15afSLinus Torvalds 			tscmax = (unsigned int) delta;
228ec0c15afSLinus Torvalds 		pitcnt++;
229ec0c15afSLinus Torvalds 	}
230ec0c15afSLinus Torvalds 
231ec0c15afSLinus Torvalds 	/*
232ec0c15afSLinus Torvalds 	 * Sanity checks:
233ec0c15afSLinus Torvalds 	 *
234a977c400SThomas Gleixner 	 * If we were not able to read the PIT more than loopmin
235ec0c15afSLinus Torvalds 	 * times, then we have been hit by a massive SMI
236ec0c15afSLinus Torvalds 	 *
237ec0c15afSLinus Torvalds 	 * If the maximum is 10 times larger than the minimum,
238ec0c15afSLinus Torvalds 	 * then we got hit by an SMI as well.
239ec0c15afSLinus Torvalds 	 */
240a977c400SThomas Gleixner 	if (pitcnt < loopmin || tscmax > 10 * tscmin)
241ec0c15afSLinus Torvalds 		return ULONG_MAX;
242ec0c15afSLinus Torvalds 
243ec0c15afSLinus Torvalds 	/* Calculate the PIT value */
244ec0c15afSLinus Torvalds 	delta = t2 - t1;
245a977c400SThomas Gleixner 	do_div(delta, ms);
246ec0c15afSLinus Torvalds 	return delta;
247ec0c15afSLinus Torvalds }
248ec0c15afSLinus Torvalds 
2496ac40ed0SLinus Torvalds /*
2506ac40ed0SLinus Torvalds  * This reads the current MSB of the PIT counter, and
2516ac40ed0SLinus Torvalds  * checks if we are running on sufficiently fast and
2526ac40ed0SLinus Torvalds  * non-virtualized hardware.
2536ac40ed0SLinus Torvalds  *
2546ac40ed0SLinus Torvalds  * Our expectations are:
2556ac40ed0SLinus Torvalds  *
2566ac40ed0SLinus Torvalds  *  - the PIT is running at roughly 1.19MHz
2576ac40ed0SLinus Torvalds  *
2586ac40ed0SLinus Torvalds  *  - each IO is going to take about 1us on real hardware,
2596ac40ed0SLinus Torvalds  *    but we allow it to be much faster (by a factor of 10) or
2606ac40ed0SLinus Torvalds  *    _slightly_ slower (ie we allow up to a 2us read+counter
2616ac40ed0SLinus Torvalds  *    update - anything else implies a unacceptably slow CPU
2626ac40ed0SLinus Torvalds  *    or PIT for the fast calibration to work.
2636ac40ed0SLinus Torvalds  *
2646ac40ed0SLinus Torvalds  *  - with 256 PIT ticks to read the value, we have 214us to
2656ac40ed0SLinus Torvalds  *    see the same MSB (and overhead like doing a single TSC
2666ac40ed0SLinus Torvalds  *    read per MSB value etc).
2676ac40ed0SLinus Torvalds  *
2686ac40ed0SLinus Torvalds  *  - We're doing 2 reads per loop (LSB, MSB), and we expect
2696ac40ed0SLinus Torvalds  *    them each to take about a microsecond on real hardware.
2706ac40ed0SLinus Torvalds  *    So we expect a count value of around 100. But we'll be
2716ac40ed0SLinus Torvalds  *    generous, and accept anything over 50.
2726ac40ed0SLinus Torvalds  *
2736ac40ed0SLinus Torvalds  *  - if the PIT is stuck, and we see *many* more reads, we
2746ac40ed0SLinus Torvalds  *    return early (and the next caller of pit_expect_msb()
2756ac40ed0SLinus Torvalds  *    then consider it a failure when they don't see the
2766ac40ed0SLinus Torvalds  *    next expected value).
2776ac40ed0SLinus Torvalds  *
2786ac40ed0SLinus Torvalds  * These expectations mean that we know that we have seen the
2796ac40ed0SLinus Torvalds  * transition from one expected value to another with a fairly
2806ac40ed0SLinus Torvalds  * high accuracy, and we didn't miss any events. We can thus
2816ac40ed0SLinus Torvalds  * use the TSC value at the transitions to calculate a pretty
2826ac40ed0SLinus Torvalds  * good value for the TSC frequencty.
2836ac40ed0SLinus Torvalds  */
284b6e61eefSLinus Torvalds static inline int pit_verify_msb(unsigned char val)
285b6e61eefSLinus Torvalds {
286b6e61eefSLinus Torvalds 	/* Ignore LSB */
287b6e61eefSLinus Torvalds 	inb(0x42);
288b6e61eefSLinus Torvalds 	return inb(0x42) == val;
289b6e61eefSLinus Torvalds }
290b6e61eefSLinus Torvalds 
2919e8912e0SLinus Torvalds static inline int pit_expect_msb(unsigned char val, u64 *tscp, unsigned long *deltap)
2926ac40ed0SLinus Torvalds {
2939e8912e0SLinus Torvalds 	int count;
2949e8912e0SLinus Torvalds 	u64 tsc = 0;
2956ac40ed0SLinus Torvalds 
2966ac40ed0SLinus Torvalds 	for (count = 0; count < 50000; count++) {
297b6e61eefSLinus Torvalds 		if (!pit_verify_msb(val))
2986ac40ed0SLinus Torvalds 			break;
2999e8912e0SLinus Torvalds 		tsc = get_cycles();
3006ac40ed0SLinus Torvalds 	}
3019e8912e0SLinus Torvalds 	*deltap = get_cycles() - tsc;
3029e8912e0SLinus Torvalds 	*tscp = tsc;
3039e8912e0SLinus Torvalds 
3049e8912e0SLinus Torvalds 	/*
3059e8912e0SLinus Torvalds 	 * We require _some_ success, but the quality control
3069e8912e0SLinus Torvalds 	 * will be based on the error terms on the TSC values.
3079e8912e0SLinus Torvalds 	 */
3089e8912e0SLinus Torvalds 	return count > 5;
3096ac40ed0SLinus Torvalds }
3106ac40ed0SLinus Torvalds 
3116ac40ed0SLinus Torvalds /*
3129e8912e0SLinus Torvalds  * How many MSB values do we want to see? We aim for
3139e8912e0SLinus Torvalds  * a maximum error rate of 500ppm (in practice the
3149e8912e0SLinus Torvalds  * real error is much smaller), but refuse to spend
3159e8912e0SLinus Torvalds  * more than 25ms on it.
3166ac40ed0SLinus Torvalds  */
3179e8912e0SLinus Torvalds #define MAX_QUICK_PIT_MS 25
3189e8912e0SLinus Torvalds #define MAX_QUICK_PIT_ITERATIONS (MAX_QUICK_PIT_MS * PIT_TICK_RATE / 1000 / 256)
3196ac40ed0SLinus Torvalds 
3206ac40ed0SLinus Torvalds static unsigned long quick_pit_calibrate(void)
3216ac40ed0SLinus Torvalds {
3229e8912e0SLinus Torvalds 	int i;
3239e8912e0SLinus Torvalds 	u64 tsc, delta;
3249e8912e0SLinus Torvalds 	unsigned long d1, d2;
3259e8912e0SLinus Torvalds 
3266ac40ed0SLinus Torvalds 	/* Set the Gate high, disable speaker */
3276ac40ed0SLinus Torvalds 	outb((inb(0x61) & ~0x02) | 0x01, 0x61);
3286ac40ed0SLinus Torvalds 
3296ac40ed0SLinus Torvalds 	/*
3306ac40ed0SLinus Torvalds 	 * Counter 2, mode 0 (one-shot), binary count
3316ac40ed0SLinus Torvalds 	 *
3326ac40ed0SLinus Torvalds 	 * NOTE! Mode 2 decrements by two (and then the
3336ac40ed0SLinus Torvalds 	 * output is flipped each time, giving the same
3346ac40ed0SLinus Torvalds 	 * final output frequency as a decrement-by-one),
3356ac40ed0SLinus Torvalds 	 * so mode 0 is much better when looking at the
3366ac40ed0SLinus Torvalds 	 * individual counts.
3376ac40ed0SLinus Torvalds 	 */
3386ac40ed0SLinus Torvalds 	outb(0xb0, 0x43);
3396ac40ed0SLinus Torvalds 
3406ac40ed0SLinus Torvalds 	/* Start at 0xffff */
3416ac40ed0SLinus Torvalds 	outb(0xff, 0x42);
3426ac40ed0SLinus Torvalds 	outb(0xff, 0x42);
3436ac40ed0SLinus Torvalds 
344a6a80e1dSLinus Torvalds 	/*
345a6a80e1dSLinus Torvalds 	 * The PIT starts counting at the next edge, so we
346a6a80e1dSLinus Torvalds 	 * need to delay for a microsecond. The easiest way
347a6a80e1dSLinus Torvalds 	 * to do that is to just read back the 16-bit counter
348a6a80e1dSLinus Torvalds 	 * once from the PIT.
349a6a80e1dSLinus Torvalds 	 */
350b6e61eefSLinus Torvalds 	pit_verify_msb(0);
351a6a80e1dSLinus Torvalds 
3529e8912e0SLinus Torvalds 	if (pit_expect_msb(0xff, &tsc, &d1)) {
3539e8912e0SLinus Torvalds 		for (i = 1; i <= MAX_QUICK_PIT_ITERATIONS; i++) {
3549e8912e0SLinus Torvalds 			if (!pit_expect_msb(0xff-i, &delta, &d2))
3559e8912e0SLinus Torvalds 				break;
3566ac40ed0SLinus Torvalds 
3576ac40ed0SLinus Torvalds 			/*
3589e8912e0SLinus Torvalds 			 * Iterate until the error is less than 500 ppm
3594156e9a8SIngo Molnar 			 */
3609e8912e0SLinus Torvalds 			delta -= tsc;
361b6e61eefSLinus Torvalds 			if (d1+d2 >= delta >> 11)
362b6e61eefSLinus Torvalds 				continue;
363b6e61eefSLinus Torvalds 
364b6e61eefSLinus Torvalds 			/*
365b6e61eefSLinus Torvalds 			 * Check the PIT one more time to verify that
366b6e61eefSLinus Torvalds 			 * all TSC reads were stable wrt the PIT.
367b6e61eefSLinus Torvalds 			 *
368b6e61eefSLinus Torvalds 			 * This also guarantees serialization of the
369b6e61eefSLinus Torvalds 			 * last cycle read ('d2') in pit_expect_msb.
370b6e61eefSLinus Torvalds 			 */
371b6e61eefSLinus Torvalds 			if (!pit_verify_msb(0xfe - i))
372b6e61eefSLinus Torvalds 				break;
3739e8912e0SLinus Torvalds 			goto success;
3749e8912e0SLinus Torvalds 		}
3759e8912e0SLinus Torvalds 	}
3769e8912e0SLinus Torvalds 	printk("Fast TSC calibration failed\n");
3779e8912e0SLinus Torvalds 	return 0;
3784156e9a8SIngo Molnar 
3799e8912e0SLinus Torvalds success:
3804156e9a8SIngo Molnar 	/*
3816ac40ed0SLinus Torvalds 	 * Ok, if we get here, then we've seen the
3829e8912e0SLinus Torvalds 	 * MSB of the PIT decrement 'i' times, and the
3839e8912e0SLinus Torvalds 	 * error has shrunk to less than 500 ppm.
3846ac40ed0SLinus Torvalds 	 *
3856ac40ed0SLinus Torvalds 	 * As a result, we can depend on there not being
3866ac40ed0SLinus Torvalds 	 * any odd delays anywhere, and the TSC reads are
3879e8912e0SLinus Torvalds 	 * reliable (within the error). We also adjust the
3889e8912e0SLinus Torvalds 	 * delta to the middle of the error bars, just
3899e8912e0SLinus Torvalds 	 * because it looks nicer.
3906ac40ed0SLinus Torvalds 	 *
3916ac40ed0SLinus Torvalds 	 * kHz = ticks / time-in-seconds / 1000;
3929e8912e0SLinus Torvalds 	 * kHz = (t2 - t1) / (I * 256 / PIT_TICK_RATE) / 1000
3939e8912e0SLinus Torvalds 	 * kHz = ((t2 - t1) * PIT_TICK_RATE) / (I * 256 * 1000)
3946ac40ed0SLinus Torvalds 	 */
3959e8912e0SLinus Torvalds 	delta += (long)(d2 - d1)/2;
3969e8912e0SLinus Torvalds 	delta *= PIT_TICK_RATE;
3979e8912e0SLinus Torvalds 	do_div(delta, i*256*1000);
3986ac40ed0SLinus Torvalds 	printk("Fast TSC calibration using PIT\n");
3996ac40ed0SLinus Torvalds 	return delta;
4006ac40ed0SLinus Torvalds }
401ec0c15afSLinus Torvalds 
402bfc0f594SAlok Kataria /**
403e93ef949SAlok Kataria  * native_calibrate_tsc - calibrate the tsc on boot
404bfc0f594SAlok Kataria  */
405e93ef949SAlok Kataria unsigned long native_calibrate_tsc(void)
406bfc0f594SAlok Kataria {
407827014beSThomas Gleixner 	u64 tsc1, tsc2, delta, ref1, ref2;
408fbb16e24SThomas Gleixner 	unsigned long tsc_pit_min = ULONG_MAX, tsc_ref_min = ULONG_MAX;
4092d826404SThomas Gleixner 	unsigned long flags, latch, ms, fast_calibrate;
410a977c400SThomas Gleixner 	int hpet = is_hpet_enabled(), i, loopmin;
411bfc0f594SAlok Kataria 
412bfc0f594SAlok Kataria 	local_irq_save(flags);
4136ac40ed0SLinus Torvalds 	fast_calibrate = quick_pit_calibrate();
414bfc0f594SAlok Kataria 	local_irq_restore(flags);
4156ac40ed0SLinus Torvalds 	if (fast_calibrate)
4166ac40ed0SLinus Torvalds 		return fast_calibrate;
417fbb16e24SThomas Gleixner 
418fbb16e24SThomas Gleixner 	/*
419fbb16e24SThomas Gleixner 	 * Run 5 calibration loops to get the lowest frequency value
420fbb16e24SThomas Gleixner 	 * (the best estimate). We use two different calibration modes
421fbb16e24SThomas Gleixner 	 * here:
422fbb16e24SThomas Gleixner 	 *
423fbb16e24SThomas Gleixner 	 * 1) PIT loop. We set the PIT Channel 2 to oneshot mode and
424fbb16e24SThomas Gleixner 	 * load a timeout of 50ms. We read the time right after we
425fbb16e24SThomas Gleixner 	 * started the timer and wait until the PIT count down reaches
426fbb16e24SThomas Gleixner 	 * zero. In each wait loop iteration we read the TSC and check
427fbb16e24SThomas Gleixner 	 * the delta to the previous read. We keep track of the min
428fbb16e24SThomas Gleixner 	 * and max values of that delta. The delta is mostly defined
429fbb16e24SThomas Gleixner 	 * by the IO time of the PIT access, so we can detect when a
430fbb16e24SThomas Gleixner 	 * SMI/SMM disturbance happend between the two reads. If the
431fbb16e24SThomas Gleixner 	 * maximum time is significantly larger than the minimum time,
432fbb16e24SThomas Gleixner 	 * then we discard the result and have another try.
433fbb16e24SThomas Gleixner 	 *
434fbb16e24SThomas Gleixner 	 * 2) Reference counter. If available we use the HPET or the
435fbb16e24SThomas Gleixner 	 * PMTIMER as a reference to check the sanity of that value.
436fbb16e24SThomas Gleixner 	 * We use separate TSC readouts and check inside of the
437fbb16e24SThomas Gleixner 	 * reference read for a SMI/SMM disturbance. We dicard
438fbb16e24SThomas Gleixner 	 * disturbed values here as well. We do that around the PIT
439fbb16e24SThomas Gleixner 	 * calibration delay loop as we have to wait for a certain
440fbb16e24SThomas Gleixner 	 * amount of time anyway.
441fbb16e24SThomas Gleixner 	 */
442a977c400SThomas Gleixner 
443a977c400SThomas Gleixner 	/* Preset PIT loop values */
444a977c400SThomas Gleixner 	latch = CAL_LATCH;
445a977c400SThomas Gleixner 	ms = CAL_MS;
446a977c400SThomas Gleixner 	loopmin = CAL_PIT_LOOPS;
447a977c400SThomas Gleixner 
448a977c400SThomas Gleixner 	for (i = 0; i < 3; i++) {
449ec0c15afSLinus Torvalds 		unsigned long tsc_pit_khz;
450bfc0f594SAlok Kataria 
451fbb16e24SThomas Gleixner 		/*
452fbb16e24SThomas Gleixner 		 * Read the start value and the reference count of
453ec0c15afSLinus Torvalds 		 * hpet/pmtimer when available. Then do the PIT
454ec0c15afSLinus Torvalds 		 * calibration, which will take at least 50ms, and
455ec0c15afSLinus Torvalds 		 * read the end value.
456fbb16e24SThomas Gleixner 		 */
457ec0c15afSLinus Torvalds 		local_irq_save(flags);
458827014beSThomas Gleixner 		tsc1 = tsc_read_refs(&ref1, hpet);
459a977c400SThomas Gleixner 		tsc_pit_khz = pit_calibrate_tsc(latch, ms, loopmin);
460827014beSThomas Gleixner 		tsc2 = tsc_read_refs(&ref2, hpet);
461bfc0f594SAlok Kataria 		local_irq_restore(flags);
462bfc0f594SAlok Kataria 
463ec0c15afSLinus Torvalds 		/* Pick the lowest PIT TSC calibration so far */
464ec0c15afSLinus Torvalds 		tsc_pit_min = min(tsc_pit_min, tsc_pit_khz);
465bfc0f594SAlok Kataria 
466bfc0f594SAlok Kataria 		/* hpet or pmtimer available ? */
467827014beSThomas Gleixner 		if (!hpet && !ref1 && !ref2)
468fbb16e24SThomas Gleixner 			continue;
469bfc0f594SAlok Kataria 
470bfc0f594SAlok Kataria 		/* Check, whether the sampling was disturbed by an SMI */
471fbb16e24SThomas Gleixner 		if (tsc1 == ULLONG_MAX || tsc2 == ULLONG_MAX)
472fbb16e24SThomas Gleixner 			continue;
473bfc0f594SAlok Kataria 
474bfc0f594SAlok Kataria 		tsc2 = (tsc2 - tsc1) * 1000000LL;
475d683ef7aSThomas Gleixner 		if (hpet)
476827014beSThomas Gleixner 			tsc2 = calc_hpet_ref(tsc2, ref1, ref2);
477d683ef7aSThomas Gleixner 		else
478827014beSThomas Gleixner 			tsc2 = calc_pmtimer_ref(tsc2, ref1, ref2);
479bfc0f594SAlok Kataria 
480fbb16e24SThomas Gleixner 		tsc_ref_min = min(tsc_ref_min, (unsigned long) tsc2);
481a977c400SThomas Gleixner 
482a977c400SThomas Gleixner 		/* Check the reference deviation */
483a977c400SThomas Gleixner 		delta = ((u64) tsc_pit_min) * 100;
484a977c400SThomas Gleixner 		do_div(delta, tsc_ref_min);
485a977c400SThomas Gleixner 
486a977c400SThomas Gleixner 		/*
487a977c400SThomas Gleixner 		 * If both calibration results are inside a 10% window
488a977c400SThomas Gleixner 		 * then we can be sure, that the calibration
489a977c400SThomas Gleixner 		 * succeeded. We break out of the loop right away. We
490a977c400SThomas Gleixner 		 * use the reference value, as it is more precise.
491a977c400SThomas Gleixner 		 */
492a977c400SThomas Gleixner 		if (delta >= 90 && delta <= 110) {
493a977c400SThomas Gleixner 			printk(KERN_INFO
494a977c400SThomas Gleixner 			       "TSC: PIT calibration matches %s. %d loops\n",
495a977c400SThomas Gleixner 			       hpet ? "HPET" : "PMTIMER", i + 1);
496a977c400SThomas Gleixner 			return tsc_ref_min;
497bfc0f594SAlok Kataria 		}
498bfc0f594SAlok Kataria 
499a977c400SThomas Gleixner 		/*
500a977c400SThomas Gleixner 		 * Check whether PIT failed more than once. This
501a977c400SThomas Gleixner 		 * happens in virtualized environments. We need to
502a977c400SThomas Gleixner 		 * give the virtual PC a slightly longer timeframe for
503a977c400SThomas Gleixner 		 * the HPET/PMTIMER to make the result precise.
504a977c400SThomas Gleixner 		 */
505a977c400SThomas Gleixner 		if (i == 1 && tsc_pit_min == ULONG_MAX) {
506a977c400SThomas Gleixner 			latch = CAL2_LATCH;
507a977c400SThomas Gleixner 			ms = CAL2_MS;
508a977c400SThomas Gleixner 			loopmin = CAL2_PIT_LOOPS;
509a977c400SThomas Gleixner 		}
510bfc0f594SAlok Kataria 	}
511bfc0f594SAlok Kataria 
512fbb16e24SThomas Gleixner 	/*
513fbb16e24SThomas Gleixner 	 * Now check the results.
514fbb16e24SThomas Gleixner 	 */
515fbb16e24SThomas Gleixner 	if (tsc_pit_min == ULONG_MAX) {
516fbb16e24SThomas Gleixner 		/* PIT gave no useful value */
517de014d61SAlok N Kataria 		printk(KERN_WARNING "TSC: Unable to calibrate against PIT\n");
518fbb16e24SThomas Gleixner 
519fbb16e24SThomas Gleixner 		/* We don't have an alternative source, disable TSC */
520827014beSThomas Gleixner 		if (!hpet && !ref1 && !ref2) {
521fbb16e24SThomas Gleixner 			printk("TSC: No reference (HPET/PMTIMER) available\n");
522fbb16e24SThomas Gleixner 			return 0;
523fbb16e24SThomas Gleixner 		}
524fbb16e24SThomas Gleixner 
525fbb16e24SThomas Gleixner 		/* The alternative source failed as well, disable TSC */
526fbb16e24SThomas Gleixner 		if (tsc_ref_min == ULONG_MAX) {
527fbb16e24SThomas Gleixner 			printk(KERN_WARNING "TSC: HPET/PMTIMER calibration "
528a977c400SThomas Gleixner 			       "failed.\n");
529fbb16e24SThomas Gleixner 			return 0;
530fbb16e24SThomas Gleixner 		}
531fbb16e24SThomas Gleixner 
532fbb16e24SThomas Gleixner 		/* Use the alternative source */
533fbb16e24SThomas Gleixner 		printk(KERN_INFO "TSC: using %s reference calibration\n",
534fbb16e24SThomas Gleixner 		       hpet ? "HPET" : "PMTIMER");
535fbb16e24SThomas Gleixner 
536fbb16e24SThomas Gleixner 		return tsc_ref_min;
537fbb16e24SThomas Gleixner 	}
538fbb16e24SThomas Gleixner 
539fbb16e24SThomas Gleixner 	/* We don't have an alternative source, use the PIT calibration value */
540827014beSThomas Gleixner 	if (!hpet && !ref1 && !ref2) {
541fbb16e24SThomas Gleixner 		printk(KERN_INFO "TSC: Using PIT calibration value\n");
542fbb16e24SThomas Gleixner 		return tsc_pit_min;
543fbb16e24SThomas Gleixner 	}
544fbb16e24SThomas Gleixner 
545fbb16e24SThomas Gleixner 	/* The alternative source failed, use the PIT calibration value */
546fbb16e24SThomas Gleixner 	if (tsc_ref_min == ULONG_MAX) {
547a977c400SThomas Gleixner 		printk(KERN_WARNING "TSC: HPET/PMTIMER calibration failed. "
548a977c400SThomas Gleixner 		       "Using PIT calibration\n");
549fbb16e24SThomas Gleixner 		return tsc_pit_min;
550fbb16e24SThomas Gleixner 	}
551fbb16e24SThomas Gleixner 
552fbb16e24SThomas Gleixner 	/*
553fbb16e24SThomas Gleixner 	 * The calibration values differ too much. In doubt, we use
554fbb16e24SThomas Gleixner 	 * the PIT value as we know that there are PMTIMERs around
555a977c400SThomas Gleixner 	 * running at double speed. At least we let the user know:
556fbb16e24SThomas Gleixner 	 */
557a977c400SThomas Gleixner 	printk(KERN_WARNING "TSC: PIT calibration deviates from %s: %lu %lu.\n",
558a977c400SThomas Gleixner 	       hpet ? "HPET" : "PMTIMER", tsc_pit_min, tsc_ref_min);
559fbb16e24SThomas Gleixner 	printk(KERN_INFO "TSC: Using PIT calibration value\n");
560fbb16e24SThomas Gleixner 	return tsc_pit_min;
561fbb16e24SThomas Gleixner }
562bfc0f594SAlok Kataria 
563bfc0f594SAlok Kataria int recalibrate_cpu_khz(void)
564bfc0f594SAlok Kataria {
565bfc0f594SAlok Kataria #ifndef CONFIG_SMP
566bfc0f594SAlok Kataria 	unsigned long cpu_khz_old = cpu_khz;
567bfc0f594SAlok Kataria 
568bfc0f594SAlok Kataria 	if (cpu_has_tsc) {
5692d826404SThomas Gleixner 		tsc_khz = x86_platform.calibrate_tsc();
570e93ef949SAlok Kataria 		cpu_khz = tsc_khz;
571bfc0f594SAlok Kataria 		cpu_data(0).loops_per_jiffy =
572bfc0f594SAlok Kataria 			cpufreq_scale(cpu_data(0).loops_per_jiffy,
573bfc0f594SAlok Kataria 					cpu_khz_old, cpu_khz);
574bfc0f594SAlok Kataria 		return 0;
575bfc0f594SAlok Kataria 	} else
576bfc0f594SAlok Kataria 		return -ENODEV;
577bfc0f594SAlok Kataria #else
578bfc0f594SAlok Kataria 	return -ENODEV;
579bfc0f594SAlok Kataria #endif
580bfc0f594SAlok Kataria }
581bfc0f594SAlok Kataria 
582bfc0f594SAlok Kataria EXPORT_SYMBOL(recalibrate_cpu_khz);
583bfc0f594SAlok Kataria 
5842dbe06faSAlok Kataria 
5852dbe06faSAlok Kataria /* Accelerators for sched_clock()
5862dbe06faSAlok Kataria  * convert from cycles(64bits) => nanoseconds (64bits)
5872dbe06faSAlok Kataria  *  basic equation:
5882dbe06faSAlok Kataria  *              ns = cycles / (freq / ns_per_sec)
5892dbe06faSAlok Kataria  *              ns = cycles * (ns_per_sec / freq)
5902dbe06faSAlok Kataria  *              ns = cycles * (10^9 / (cpu_khz * 10^3))
5912dbe06faSAlok Kataria  *              ns = cycles * (10^6 / cpu_khz)
5922dbe06faSAlok Kataria  *
5932dbe06faSAlok Kataria  *      Then we use scaling math (suggested by george@mvista.com) to get:
5942dbe06faSAlok Kataria  *              ns = cycles * (10^6 * SC / cpu_khz) / SC
5952dbe06faSAlok Kataria  *              ns = cycles * cyc2ns_scale / SC
5962dbe06faSAlok Kataria  *
5972dbe06faSAlok Kataria  *      And since SC is a constant power of two, we can convert the div
5982dbe06faSAlok Kataria  *  into a shift.
5992dbe06faSAlok Kataria  *
6002dbe06faSAlok Kataria  *  We can use khz divisor instead of mhz to keep a better precision, since
6012dbe06faSAlok Kataria  *  cyc2ns_scale is limited to 10^6 * 2^10, which fits in 32 bits.
6022dbe06faSAlok Kataria  *  (mathieu.desnoyers@polymtl.ca)
6032dbe06faSAlok Kataria  *
6042dbe06faSAlok Kataria  *                      -johnstul@us.ibm.com "math is hard, lets go shopping!"
6052dbe06faSAlok Kataria  */
6062dbe06faSAlok Kataria 
6072dbe06faSAlok Kataria DEFINE_PER_CPU(unsigned long, cyc2ns);
60884599f8aSPeter Zijlstra DEFINE_PER_CPU(unsigned long long, cyc2ns_offset);
6092dbe06faSAlok Kataria 
6108fbbc4b4SAlok Kataria static void set_cyc2ns_scale(unsigned long cpu_khz, int cpu)
6112dbe06faSAlok Kataria {
61284599f8aSPeter Zijlstra 	unsigned long long tsc_now, ns_now, *offset;
6132dbe06faSAlok Kataria 	unsigned long flags, *scale;
6142dbe06faSAlok Kataria 
6152dbe06faSAlok Kataria 	local_irq_save(flags);
6162dbe06faSAlok Kataria 	sched_clock_idle_sleep_event();
6172dbe06faSAlok Kataria 
6182dbe06faSAlok Kataria 	scale = &per_cpu(cyc2ns, cpu);
61984599f8aSPeter Zijlstra 	offset = &per_cpu(cyc2ns_offset, cpu);
6202dbe06faSAlok Kataria 
6212dbe06faSAlok Kataria 	rdtscll(tsc_now);
6222dbe06faSAlok Kataria 	ns_now = __cycles_2_ns(tsc_now);
6232dbe06faSAlok Kataria 
62484599f8aSPeter Zijlstra 	if (cpu_khz) {
6252dbe06faSAlok Kataria 		*scale = (NSEC_PER_MSEC << CYC2NS_SCALE_FACTOR)/cpu_khz;
62684599f8aSPeter Zijlstra 		*offset = ns_now - (tsc_now * *scale >> CYC2NS_SCALE_FACTOR);
62784599f8aSPeter Zijlstra 	}
6282dbe06faSAlok Kataria 
6292dbe06faSAlok Kataria 	sched_clock_idle_wakeup_event(0);
6302dbe06faSAlok Kataria 	local_irq_restore(flags);
6312dbe06faSAlok Kataria }
6322dbe06faSAlok Kataria 
633cd7240c0SSuresh Siddha static unsigned long long cyc2ns_suspend;
634cd7240c0SSuresh Siddha 
635cd7240c0SSuresh Siddha void save_sched_clock_state(void)
636cd7240c0SSuresh Siddha {
637cd7240c0SSuresh Siddha 	if (!sched_clock_stable)
638cd7240c0SSuresh Siddha 		return;
639cd7240c0SSuresh Siddha 
640cd7240c0SSuresh Siddha 	cyc2ns_suspend = sched_clock();
641cd7240c0SSuresh Siddha }
642cd7240c0SSuresh Siddha 
643cd7240c0SSuresh Siddha /*
644cd7240c0SSuresh Siddha  * Even on processors with invariant TSC, TSC gets reset in some the
645cd7240c0SSuresh Siddha  * ACPI system sleep states. And in some systems BIOS seem to reinit TSC to
646cd7240c0SSuresh Siddha  * arbitrary value (still sync'd across cpu's) during resume from such sleep
647cd7240c0SSuresh Siddha  * states. To cope up with this, recompute the cyc2ns_offset for each cpu so
648cd7240c0SSuresh Siddha  * that sched_clock() continues from the point where it was left off during
649cd7240c0SSuresh Siddha  * suspend.
650cd7240c0SSuresh Siddha  */
651cd7240c0SSuresh Siddha void restore_sched_clock_state(void)
652cd7240c0SSuresh Siddha {
653cd7240c0SSuresh Siddha 	unsigned long long offset;
654cd7240c0SSuresh Siddha 	unsigned long flags;
655cd7240c0SSuresh Siddha 	int cpu;
656cd7240c0SSuresh Siddha 
657cd7240c0SSuresh Siddha 	if (!sched_clock_stable)
658cd7240c0SSuresh Siddha 		return;
659cd7240c0SSuresh Siddha 
660cd7240c0SSuresh Siddha 	local_irq_save(flags);
661cd7240c0SSuresh Siddha 
6620a3aee0dSTejun Heo 	__this_cpu_write(cyc2ns_offset, 0);
663cd7240c0SSuresh Siddha 	offset = cyc2ns_suspend - sched_clock();
664cd7240c0SSuresh Siddha 
665cd7240c0SSuresh Siddha 	for_each_possible_cpu(cpu)
666cd7240c0SSuresh Siddha 		per_cpu(cyc2ns_offset, cpu) = offset;
667cd7240c0SSuresh Siddha 
668cd7240c0SSuresh Siddha 	local_irq_restore(flags);
669cd7240c0SSuresh Siddha }
670cd7240c0SSuresh Siddha 
6712dbe06faSAlok Kataria #ifdef CONFIG_CPU_FREQ
6722dbe06faSAlok Kataria 
6732dbe06faSAlok Kataria /* Frequency scaling support. Adjust the TSC based timer when the cpu frequency
6742dbe06faSAlok Kataria  * changes.
6752dbe06faSAlok Kataria  *
6762dbe06faSAlok Kataria  * RED-PEN: On SMP we assume all CPUs run with the same frequency.  It's
6772dbe06faSAlok Kataria  * not that important because current Opteron setups do not support
6782dbe06faSAlok Kataria  * scaling on SMP anyroads.
6792dbe06faSAlok Kataria  *
6802dbe06faSAlok Kataria  * Should fix up last_tsc too. Currently gettimeofday in the
6812dbe06faSAlok Kataria  * first tick after the change will be slightly wrong.
6822dbe06faSAlok Kataria  */
6832dbe06faSAlok Kataria 
6842dbe06faSAlok Kataria static unsigned int  ref_freq;
6852dbe06faSAlok Kataria static unsigned long loops_per_jiffy_ref;
6862dbe06faSAlok Kataria static unsigned long tsc_khz_ref;
6872dbe06faSAlok Kataria 
6882dbe06faSAlok Kataria static int time_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
6892dbe06faSAlok Kataria 				void *data)
6902dbe06faSAlok Kataria {
6912dbe06faSAlok Kataria 	struct cpufreq_freqs *freq = data;
692931db6a3SDave Jones 	unsigned long *lpj;
6932dbe06faSAlok Kataria 
6942dbe06faSAlok Kataria 	if (cpu_has(&cpu_data(freq->cpu), X86_FEATURE_CONSTANT_TSC))
6952dbe06faSAlok Kataria 		return 0;
6962dbe06faSAlok Kataria 
6972dbe06faSAlok Kataria 	lpj = &boot_cpu_data.loops_per_jiffy;
698931db6a3SDave Jones #ifdef CONFIG_SMP
699931db6a3SDave Jones 	if (!(freq->flags & CPUFREQ_CONST_LOOPS))
700931db6a3SDave Jones 		lpj = &cpu_data(freq->cpu).loops_per_jiffy;
7012dbe06faSAlok Kataria #endif
7022dbe06faSAlok Kataria 
7032dbe06faSAlok Kataria 	if (!ref_freq) {
7042dbe06faSAlok Kataria 		ref_freq = freq->old;
7052dbe06faSAlok Kataria 		loops_per_jiffy_ref = *lpj;
7062dbe06faSAlok Kataria 		tsc_khz_ref = tsc_khz;
7072dbe06faSAlok Kataria 	}
7082dbe06faSAlok Kataria 	if ((val == CPUFREQ_PRECHANGE  && freq->old < freq->new) ||
7092dbe06faSAlok Kataria 			(val == CPUFREQ_POSTCHANGE && freq->old > freq->new) ||
7102dbe06faSAlok Kataria 			(val == CPUFREQ_RESUMECHANGE)) {
7112dbe06faSAlok Kataria 		*lpj = cpufreq_scale(loops_per_jiffy_ref, ref_freq, freq->new);
7122dbe06faSAlok Kataria 
7132dbe06faSAlok Kataria 		tsc_khz = cpufreq_scale(tsc_khz_ref, ref_freq, freq->new);
7142dbe06faSAlok Kataria 		if (!(freq->flags & CPUFREQ_CONST_LOOPS))
7152dbe06faSAlok Kataria 			mark_tsc_unstable("cpufreq changes");
7162dbe06faSAlok Kataria 	}
7172dbe06faSAlok Kataria 
71852a8968cSPeter Zijlstra 	set_cyc2ns_scale(tsc_khz, freq->cpu);
7192dbe06faSAlok Kataria 
7202dbe06faSAlok Kataria 	return 0;
7212dbe06faSAlok Kataria }
7222dbe06faSAlok Kataria 
7232dbe06faSAlok Kataria static struct notifier_block time_cpufreq_notifier_block = {
7242dbe06faSAlok Kataria 	.notifier_call  = time_cpufreq_notifier
7252dbe06faSAlok Kataria };
7262dbe06faSAlok Kataria 
7272dbe06faSAlok Kataria static int __init cpufreq_tsc(void)
7282dbe06faSAlok Kataria {
729060700b5SLinus Torvalds 	if (!cpu_has_tsc)
730060700b5SLinus Torvalds 		return 0;
731060700b5SLinus Torvalds 	if (boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
732060700b5SLinus Torvalds 		return 0;
7332dbe06faSAlok Kataria 	cpufreq_register_notifier(&time_cpufreq_notifier_block,
7342dbe06faSAlok Kataria 				CPUFREQ_TRANSITION_NOTIFIER);
7352dbe06faSAlok Kataria 	return 0;
7362dbe06faSAlok Kataria }
7372dbe06faSAlok Kataria 
7382dbe06faSAlok Kataria core_initcall(cpufreq_tsc);
7392dbe06faSAlok Kataria 
7402dbe06faSAlok Kataria #endif /* CONFIG_CPU_FREQ */
7418fbbc4b4SAlok Kataria 
7428fbbc4b4SAlok Kataria /* clocksource code */
7438fbbc4b4SAlok Kataria 
7448fbbc4b4SAlok Kataria static struct clocksource clocksource_tsc;
7458fbbc4b4SAlok Kataria 
7468fbbc4b4SAlok Kataria /*
7478fbbc4b4SAlok Kataria  * We compare the TSC to the cycle_last value in the clocksource
7488fbbc4b4SAlok Kataria  * structure to avoid a nasty time-warp. This can be observed in a
7498fbbc4b4SAlok Kataria  * very small window right after one CPU updated cycle_last under
7508fbbc4b4SAlok Kataria  * xtime/vsyscall_gtod lock and the other CPU reads a TSC value which
7518fbbc4b4SAlok Kataria  * is smaller than the cycle_last reference value due to a TSC which
7528fbbc4b4SAlok Kataria  * is slighty behind. This delta is nowhere else observable, but in
7538fbbc4b4SAlok Kataria  * that case it results in a forward time jump in the range of hours
7548fbbc4b4SAlok Kataria  * due to the unsigned delta calculation of the time keeping core
7558fbbc4b4SAlok Kataria  * code, which is necessary to support wrapping clocksources like pm
7568fbbc4b4SAlok Kataria  * timer.
7578fbbc4b4SAlok Kataria  */
7588e19608eSMagnus Damm static cycle_t read_tsc(struct clocksource *cs)
7598fbbc4b4SAlok Kataria {
7608fbbc4b4SAlok Kataria 	cycle_t ret = (cycle_t)get_cycles();
7618fbbc4b4SAlok Kataria 
7628fbbc4b4SAlok Kataria 	return ret >= clocksource_tsc.cycle_last ?
7638fbbc4b4SAlok Kataria 		ret : clocksource_tsc.cycle_last;
7648fbbc4b4SAlok Kataria }
7658fbbc4b4SAlok Kataria 
766431ceb83SThomas Gleixner #ifdef CONFIG_X86_64
7678fbbc4b4SAlok Kataria static cycle_t __vsyscall_fn vread_tsc(void)
7688fbbc4b4SAlok Kataria {
7697d96fd41SPetr Tesarik 	cycle_t ret;
7707d96fd41SPetr Tesarik 
7717d96fd41SPetr Tesarik 	/*
7727d96fd41SPetr Tesarik 	 * Surround the RDTSC by barriers, to make sure it's not
7737d96fd41SPetr Tesarik 	 * speculated to outside the seqlock critical section and
7747d96fd41SPetr Tesarik 	 * does not cause time warps:
7757d96fd41SPetr Tesarik 	 */
7767d96fd41SPetr Tesarik 	rdtsc_barrier();
7777d96fd41SPetr Tesarik 	ret = (cycle_t)vget_cycles();
7787d96fd41SPetr Tesarik 	rdtsc_barrier();
7798fbbc4b4SAlok Kataria 
7808fbbc4b4SAlok Kataria 	return ret >= __vsyscall_gtod_data.clock.cycle_last ?
7818fbbc4b4SAlok Kataria 		ret : __vsyscall_gtod_data.clock.cycle_last;
7828fbbc4b4SAlok Kataria }
783431ceb83SThomas Gleixner #endif
7848fbbc4b4SAlok Kataria 
78517622339SMagnus Damm static void resume_tsc(struct clocksource *cs)
7861be39679SMartin Schwidefsky {
7871be39679SMartin Schwidefsky 	clocksource_tsc.cycle_last = 0;
7881be39679SMartin Schwidefsky }
7891be39679SMartin Schwidefsky 
7908fbbc4b4SAlok Kataria static struct clocksource clocksource_tsc = {
7918fbbc4b4SAlok Kataria 	.name                   = "tsc",
7928fbbc4b4SAlok Kataria 	.rating                 = 300,
7938fbbc4b4SAlok Kataria 	.read                   = read_tsc,
7941be39679SMartin Schwidefsky 	.resume			= resume_tsc,
7958fbbc4b4SAlok Kataria 	.mask                   = CLOCKSOURCE_MASK(64),
7968fbbc4b4SAlok Kataria 	.flags                  = CLOCK_SOURCE_IS_CONTINUOUS |
7978fbbc4b4SAlok Kataria 				  CLOCK_SOURCE_MUST_VERIFY,
7988fbbc4b4SAlok Kataria #ifdef CONFIG_X86_64
7998fbbc4b4SAlok Kataria 	.vread                  = vread_tsc,
8008fbbc4b4SAlok Kataria #endif
8018fbbc4b4SAlok Kataria };
8028fbbc4b4SAlok Kataria 
8038fbbc4b4SAlok Kataria void mark_tsc_unstable(char *reason)
8048fbbc4b4SAlok Kataria {
8058fbbc4b4SAlok Kataria 	if (!tsc_unstable) {
8068fbbc4b4SAlok Kataria 		tsc_unstable = 1;
8076c56ccecSPallipadi, Venkatesh 		sched_clock_stable = 0;
808e82b8e4eSVenkatesh Pallipadi 		disable_sched_clock_irqtime();
8097285dd7fSThomas Gleixner 		printk(KERN_INFO "Marking TSC unstable due to %s\n", reason);
8108fbbc4b4SAlok Kataria 		/* Change only the rating, when not registered */
8118fbbc4b4SAlok Kataria 		if (clocksource_tsc.mult)
8127285dd7fSThomas Gleixner 			clocksource_mark_unstable(&clocksource_tsc);
8137285dd7fSThomas Gleixner 		else {
8147285dd7fSThomas Gleixner 			clocksource_tsc.flags |= CLOCK_SOURCE_UNSTABLE;
8158fbbc4b4SAlok Kataria 			clocksource_tsc.rating = 0;
8168fbbc4b4SAlok Kataria 		}
8178fbbc4b4SAlok Kataria 	}
8187285dd7fSThomas Gleixner }
8198fbbc4b4SAlok Kataria 
8208fbbc4b4SAlok Kataria EXPORT_SYMBOL_GPL(mark_tsc_unstable);
8218fbbc4b4SAlok Kataria 
8228fbbc4b4SAlok Kataria static int __init dmi_mark_tsc_unstable(const struct dmi_system_id *d)
8238fbbc4b4SAlok Kataria {
8248fbbc4b4SAlok Kataria 	printk(KERN_NOTICE "%s detected: marking TSC unstable.\n",
8258fbbc4b4SAlok Kataria 			d->ident);
8268fbbc4b4SAlok Kataria 	tsc_unstable = 1;
8278fbbc4b4SAlok Kataria 	return 0;
8288fbbc4b4SAlok Kataria }
8298fbbc4b4SAlok Kataria 
8308fbbc4b4SAlok Kataria /* List of systems that have known TSC problems */
8318fbbc4b4SAlok Kataria static struct dmi_system_id __initdata bad_tsc_dmi_table[] = {
8328fbbc4b4SAlok Kataria 	{
8338fbbc4b4SAlok Kataria 		.callback = dmi_mark_tsc_unstable,
8348fbbc4b4SAlok Kataria 		.ident = "IBM Thinkpad 380XD",
8358fbbc4b4SAlok Kataria 		.matches = {
8368fbbc4b4SAlok Kataria 			DMI_MATCH(DMI_BOARD_VENDOR, "IBM"),
8378fbbc4b4SAlok Kataria 			DMI_MATCH(DMI_BOARD_NAME, "2635FA0"),
8388fbbc4b4SAlok Kataria 		},
8398fbbc4b4SAlok Kataria 	},
8408fbbc4b4SAlok Kataria 	{}
8418fbbc4b4SAlok Kataria };
8428fbbc4b4SAlok Kataria 
843395628efSAlok Kataria static void __init check_system_tsc_reliable(void)
844395628efSAlok Kataria {
8458fbbc4b4SAlok Kataria #ifdef CONFIG_MGEODE_LX
8468fbbc4b4SAlok Kataria 	/* RTSC counts during suspend */
8478fbbc4b4SAlok Kataria #define RTSC_SUSP 0x100
8488fbbc4b4SAlok Kataria 	unsigned long res_low, res_high;
8498fbbc4b4SAlok Kataria 
8508fbbc4b4SAlok Kataria 	rdmsr_safe(MSR_GEODE_BUSCONT_CONF0, &res_low, &res_high);
85100097c4fSThadeu Lima de Souza Cascardo 	/* Geode_LX - the OLPC CPU has a very reliable TSC */
8528fbbc4b4SAlok Kataria 	if (res_low & RTSC_SUSP)
853395628efSAlok Kataria 		tsc_clocksource_reliable = 1;
8548fbbc4b4SAlok Kataria #endif
855395628efSAlok Kataria 	if (boot_cpu_has(X86_FEATURE_TSC_RELIABLE))
856395628efSAlok Kataria 		tsc_clocksource_reliable = 1;
857395628efSAlok Kataria }
8588fbbc4b4SAlok Kataria 
8598fbbc4b4SAlok Kataria /*
8608fbbc4b4SAlok Kataria  * Make an educated guess if the TSC is trustworthy and synchronized
8618fbbc4b4SAlok Kataria  * over all CPUs.
8628fbbc4b4SAlok Kataria  */
8638fbbc4b4SAlok Kataria __cpuinit int unsynchronized_tsc(void)
8648fbbc4b4SAlok Kataria {
8658fbbc4b4SAlok Kataria 	if (!cpu_has_tsc || tsc_unstable)
8668fbbc4b4SAlok Kataria 		return 1;
8678fbbc4b4SAlok Kataria 
8683e5095d1SIngo Molnar #ifdef CONFIG_SMP
8698fbbc4b4SAlok Kataria 	if (apic_is_clustered_box())
8708fbbc4b4SAlok Kataria 		return 1;
8718fbbc4b4SAlok Kataria #endif
8728fbbc4b4SAlok Kataria 
8738fbbc4b4SAlok Kataria 	if (boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
8748fbbc4b4SAlok Kataria 		return 0;
8758fbbc4b4SAlok Kataria 	/*
8768fbbc4b4SAlok Kataria 	 * Intel systems are normally all synchronized.
8778fbbc4b4SAlok Kataria 	 * Exceptions must mark TSC as unstable:
8788fbbc4b4SAlok Kataria 	 */
8798fbbc4b4SAlok Kataria 	if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL) {
8808fbbc4b4SAlok Kataria 		/* assume multi socket systems are not synchronized: */
8818fbbc4b4SAlok Kataria 		if (num_possible_cpus() > 1)
8828fbbc4b4SAlok Kataria 			tsc_unstable = 1;
8838fbbc4b4SAlok Kataria 	}
8848fbbc4b4SAlok Kataria 
8858fbbc4b4SAlok Kataria 	return tsc_unstable;
8868fbbc4b4SAlok Kataria }
8878fbbc4b4SAlok Kataria 
8888fbbc4b4SAlok Kataria static void __init init_tsc_clocksource(void)
8898fbbc4b4SAlok Kataria {
890395628efSAlok Kataria 	if (tsc_clocksource_reliable)
891395628efSAlok Kataria 		clocksource_tsc.flags &= ~CLOCK_SOURCE_MUST_VERIFY;
8928fbbc4b4SAlok Kataria 	/* lower the rating if we already know its unstable: */
8938fbbc4b4SAlok Kataria 	if (check_tsc_unstable()) {
8948fbbc4b4SAlok Kataria 		clocksource_tsc.rating = 0;
8958fbbc4b4SAlok Kataria 		clocksource_tsc.flags &= ~CLOCK_SOURCE_IS_CONTINUOUS;
8968fbbc4b4SAlok Kataria 	}
897f12a15beSJohn Stultz 	clocksource_register_khz(&clocksource_tsc, tsc_khz);
8988fbbc4b4SAlok Kataria }
8998fbbc4b4SAlok Kataria 
9008fbbc4b4SAlok Kataria void __init tsc_init(void)
9018fbbc4b4SAlok Kataria {
9028fbbc4b4SAlok Kataria 	u64 lpj;
9038fbbc4b4SAlok Kataria 	int cpu;
9048fbbc4b4SAlok Kataria 
905845b3944SThomas Gleixner 	x86_init.timers.tsc_pre_init();
906845b3944SThomas Gleixner 
9078fbbc4b4SAlok Kataria 	if (!cpu_has_tsc)
9088fbbc4b4SAlok Kataria 		return;
9098fbbc4b4SAlok Kataria 
9102d826404SThomas Gleixner 	tsc_khz = x86_platform.calibrate_tsc();
911e93ef949SAlok Kataria 	cpu_khz = tsc_khz;
9128fbbc4b4SAlok Kataria 
913e93ef949SAlok Kataria 	if (!tsc_khz) {
9148fbbc4b4SAlok Kataria 		mark_tsc_unstable("could not calculate TSC khz");
9158fbbc4b4SAlok Kataria 		return;
9168fbbc4b4SAlok Kataria 	}
9178fbbc4b4SAlok Kataria 
9188fbbc4b4SAlok Kataria 	printk("Detected %lu.%03lu MHz processor.\n",
9198fbbc4b4SAlok Kataria 			(unsigned long)cpu_khz / 1000,
9208fbbc4b4SAlok Kataria 			(unsigned long)cpu_khz % 1000);
9218fbbc4b4SAlok Kataria 
9228fbbc4b4SAlok Kataria 	/*
9238fbbc4b4SAlok Kataria 	 * Secondary CPUs do not run through tsc_init(), so set up
9248fbbc4b4SAlok Kataria 	 * all the scale factors for all CPUs, assuming the same
9258fbbc4b4SAlok Kataria 	 * speed as the bootup CPU. (cpufreq notifiers will fix this
9268fbbc4b4SAlok Kataria 	 * up if their speed diverges)
9278fbbc4b4SAlok Kataria 	 */
9288fbbc4b4SAlok Kataria 	for_each_possible_cpu(cpu)
9298fbbc4b4SAlok Kataria 		set_cyc2ns_scale(cpu_khz, cpu);
9308fbbc4b4SAlok Kataria 
9318fbbc4b4SAlok Kataria 	if (tsc_disabled > 0)
9328fbbc4b4SAlok Kataria 		return;
9338fbbc4b4SAlok Kataria 
9348fbbc4b4SAlok Kataria 	/* now allow native_sched_clock() to use rdtsc */
9358fbbc4b4SAlok Kataria 	tsc_disabled = 0;
9368fbbc4b4SAlok Kataria 
937e82b8e4eSVenkatesh Pallipadi 	if (!no_sched_irq_time)
938e82b8e4eSVenkatesh Pallipadi 		enable_sched_clock_irqtime();
939e82b8e4eSVenkatesh Pallipadi 
94070de9a97SAlok Kataria 	lpj = ((u64)tsc_khz * 1000);
94170de9a97SAlok Kataria 	do_div(lpj, HZ);
94270de9a97SAlok Kataria 	lpj_fine = lpj;
94370de9a97SAlok Kataria 
9448fbbc4b4SAlok Kataria 	use_tsc_delay();
9458fbbc4b4SAlok Kataria 	/* Check and install the TSC clocksource */
9468fbbc4b4SAlok Kataria 	dmi_check_system(bad_tsc_dmi_table);
9478fbbc4b4SAlok Kataria 
9488fbbc4b4SAlok Kataria 	if (unsynchronized_tsc())
9498fbbc4b4SAlok Kataria 		mark_tsc_unstable("TSCs unsynchronized");
9508fbbc4b4SAlok Kataria 
951395628efSAlok Kataria 	check_system_tsc_reliable();
9528fbbc4b4SAlok Kataria 	init_tsc_clocksource();
9538fbbc4b4SAlok Kataria }
9548fbbc4b4SAlok Kataria 
955