xref: /openbmc/linux/arch/x86/kernel/tsc.c (revision 931db6a3)
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>
12bfc0f594SAlok Kataria 
13bfc0f594SAlok Kataria #include <asm/hpet.h>
148fbbc4b4SAlok Kataria #include <asm/timer.h>
158fbbc4b4SAlok Kataria #include <asm/vgtod.h>
168fbbc4b4SAlok Kataria #include <asm/time.h>
178fbbc4b4SAlok Kataria #include <asm/delay.h>
1888b094fbSAlok Kataria #include <asm/hypervisor.h>
190ef95533SAlok Kataria 
20f24ade3aSIngo Molnar unsigned int __read_mostly cpu_khz;	/* TSC clocks / usec, not used here */
210ef95533SAlok Kataria EXPORT_SYMBOL(cpu_khz);
22f24ade3aSIngo Molnar 
23f24ade3aSIngo Molnar unsigned int __read_mostly tsc_khz;
240ef95533SAlok Kataria EXPORT_SYMBOL(tsc_khz);
250ef95533SAlok Kataria 
260ef95533SAlok Kataria /*
270ef95533SAlok Kataria  * TSC can be unstable due to cpufreq or due to unsynced TSCs
280ef95533SAlok Kataria  */
29f24ade3aSIngo Molnar static int __read_mostly tsc_unstable;
300ef95533SAlok Kataria 
310ef95533SAlok Kataria /* native_sched_clock() is called before tsc_init(), so
320ef95533SAlok Kataria    we must start with the TSC soft disabled to prevent
330ef95533SAlok Kataria    erroneous rdtsc usage on !cpu_has_tsc processors */
34f24ade3aSIngo Molnar static int __read_mostly tsc_disabled = -1;
350ef95533SAlok Kataria 
36395628efSAlok Kataria static int tsc_clocksource_reliable;
370ef95533SAlok Kataria /*
380ef95533SAlok Kataria  * Scheduler clock - returns current time in nanosec units.
390ef95533SAlok Kataria  */
400ef95533SAlok Kataria u64 native_sched_clock(void)
410ef95533SAlok Kataria {
420ef95533SAlok Kataria 	u64 this_offset;
430ef95533SAlok Kataria 
440ef95533SAlok Kataria 	/*
450ef95533SAlok Kataria 	 * Fall back to jiffies if there's no TSC available:
460ef95533SAlok Kataria 	 * ( But note that we still use it if the TSC is marked
470ef95533SAlok Kataria 	 *   unstable. We do this because unlike Time Of Day,
480ef95533SAlok Kataria 	 *   the scheduler clock tolerates small errors and it's
490ef95533SAlok Kataria 	 *   very important for it to be as fast as the platform
500ef95533SAlok Kataria 	 *   can achive it. )
510ef95533SAlok Kataria 	 */
520ef95533SAlok Kataria 	if (unlikely(tsc_disabled)) {
530ef95533SAlok Kataria 		/* No locking but a rare wrong value is not a big deal: */
540ef95533SAlok Kataria 		return (jiffies_64 - INITIAL_JIFFIES) * (1000000000 / HZ);
550ef95533SAlok Kataria 	}
560ef95533SAlok Kataria 
570ef95533SAlok Kataria 	/* read the Time Stamp Counter: */
580ef95533SAlok Kataria 	rdtscll(this_offset);
590ef95533SAlok Kataria 
600ef95533SAlok Kataria 	/* return the value in ns */
617cbaef9cSIngo Molnar 	return __cycles_2_ns(this_offset);
620ef95533SAlok Kataria }
630ef95533SAlok Kataria 
640ef95533SAlok Kataria /* We need to define a real function for sched_clock, to override the
650ef95533SAlok Kataria    weak default version */
660ef95533SAlok Kataria #ifdef CONFIG_PARAVIRT
670ef95533SAlok Kataria unsigned long long sched_clock(void)
680ef95533SAlok Kataria {
690ef95533SAlok Kataria 	return paravirt_sched_clock();
700ef95533SAlok Kataria }
710ef95533SAlok Kataria #else
720ef95533SAlok Kataria unsigned long long
730ef95533SAlok Kataria sched_clock(void) __attribute__((alias("native_sched_clock")));
740ef95533SAlok Kataria #endif
750ef95533SAlok Kataria 
760ef95533SAlok Kataria int check_tsc_unstable(void)
770ef95533SAlok Kataria {
780ef95533SAlok Kataria 	return tsc_unstable;
790ef95533SAlok Kataria }
800ef95533SAlok Kataria EXPORT_SYMBOL_GPL(check_tsc_unstable);
810ef95533SAlok Kataria 
820ef95533SAlok Kataria #ifdef CONFIG_X86_TSC
830ef95533SAlok Kataria int __init notsc_setup(char *str)
840ef95533SAlok Kataria {
850ef95533SAlok Kataria 	printk(KERN_WARNING "notsc: Kernel compiled with CONFIG_X86_TSC, "
860ef95533SAlok Kataria 			"cannot disable TSC completely.\n");
870ef95533SAlok Kataria 	tsc_disabled = 1;
880ef95533SAlok Kataria 	return 1;
890ef95533SAlok Kataria }
900ef95533SAlok Kataria #else
910ef95533SAlok Kataria /*
920ef95533SAlok Kataria  * disable flag for tsc. Takes effect by clearing the TSC cpu flag
930ef95533SAlok Kataria  * in cpu/common.c
940ef95533SAlok Kataria  */
950ef95533SAlok Kataria int __init notsc_setup(char *str)
960ef95533SAlok Kataria {
970ef95533SAlok Kataria 	setup_clear_cpu_cap(X86_FEATURE_TSC);
980ef95533SAlok Kataria 	return 1;
990ef95533SAlok Kataria }
1000ef95533SAlok Kataria #endif
1010ef95533SAlok Kataria 
1020ef95533SAlok Kataria __setup("notsc", notsc_setup);
103bfc0f594SAlok Kataria 
104395628efSAlok Kataria static int __init tsc_setup(char *str)
105395628efSAlok Kataria {
106395628efSAlok Kataria 	if (!strcmp(str, "reliable"))
107395628efSAlok Kataria 		tsc_clocksource_reliable = 1;
108395628efSAlok Kataria 	return 1;
109395628efSAlok Kataria }
110395628efSAlok Kataria 
111395628efSAlok Kataria __setup("tsc=", tsc_setup);
112395628efSAlok Kataria 
113bfc0f594SAlok Kataria #define MAX_RETRIES     5
114bfc0f594SAlok Kataria #define SMI_TRESHOLD    50000
115bfc0f594SAlok Kataria 
116bfc0f594SAlok Kataria /*
117bfc0f594SAlok Kataria  * Read TSC and the reference counters. Take care of SMI disturbance
118bfc0f594SAlok Kataria  */
119827014beSThomas Gleixner static u64 tsc_read_refs(u64 *p, int hpet)
120bfc0f594SAlok Kataria {
121bfc0f594SAlok Kataria 	u64 t1, t2;
122bfc0f594SAlok Kataria 	int i;
123bfc0f594SAlok Kataria 
124bfc0f594SAlok Kataria 	for (i = 0; i < MAX_RETRIES; i++) {
125bfc0f594SAlok Kataria 		t1 = get_cycles();
126bfc0f594SAlok Kataria 		if (hpet)
127827014beSThomas Gleixner 			*p = hpet_readl(HPET_COUNTER) & 0xFFFFFFFF;
128bfc0f594SAlok Kataria 		else
129827014beSThomas Gleixner 			*p = acpi_pm_read_early();
130bfc0f594SAlok Kataria 		t2 = get_cycles();
131bfc0f594SAlok Kataria 		if ((t2 - t1) < SMI_TRESHOLD)
132bfc0f594SAlok Kataria 			return t2;
133bfc0f594SAlok Kataria 	}
134bfc0f594SAlok Kataria 	return ULLONG_MAX;
135bfc0f594SAlok Kataria }
136bfc0f594SAlok Kataria 
137ec0c15afSLinus Torvalds /*
138d683ef7aSThomas Gleixner  * Calculate the TSC frequency from HPET reference
139d683ef7aSThomas Gleixner  */
140d683ef7aSThomas Gleixner static unsigned long calc_hpet_ref(u64 deltatsc, u64 hpet1, u64 hpet2)
141d683ef7aSThomas Gleixner {
142d683ef7aSThomas Gleixner 	u64 tmp;
143d683ef7aSThomas Gleixner 
144d683ef7aSThomas Gleixner 	if (hpet2 < hpet1)
145d683ef7aSThomas Gleixner 		hpet2 += 0x100000000ULL;
146d683ef7aSThomas Gleixner 	hpet2 -= hpet1;
147d683ef7aSThomas Gleixner 	tmp = ((u64)hpet2 * hpet_readl(HPET_PERIOD));
148d683ef7aSThomas Gleixner 	do_div(tmp, 1000000);
149d683ef7aSThomas Gleixner 	do_div(deltatsc, tmp);
150d683ef7aSThomas Gleixner 
151d683ef7aSThomas Gleixner 	return (unsigned long) deltatsc;
152d683ef7aSThomas Gleixner }
153d683ef7aSThomas Gleixner 
154d683ef7aSThomas Gleixner /*
155d683ef7aSThomas Gleixner  * Calculate the TSC frequency from PMTimer reference
156d683ef7aSThomas Gleixner  */
157d683ef7aSThomas Gleixner static unsigned long calc_pmtimer_ref(u64 deltatsc, u64 pm1, u64 pm2)
158d683ef7aSThomas Gleixner {
159d683ef7aSThomas Gleixner 	u64 tmp;
160d683ef7aSThomas Gleixner 
161d683ef7aSThomas Gleixner 	if (!pm1 && !pm2)
162d683ef7aSThomas Gleixner 		return ULONG_MAX;
163d683ef7aSThomas Gleixner 
164d683ef7aSThomas Gleixner 	if (pm2 < pm1)
165d683ef7aSThomas Gleixner 		pm2 += (u64)ACPI_PM_OVRRUN;
166d683ef7aSThomas Gleixner 	pm2 -= pm1;
167d683ef7aSThomas Gleixner 	tmp = pm2 * 1000000000LL;
168d683ef7aSThomas Gleixner 	do_div(tmp, PMTMR_TICKS_PER_SEC);
169d683ef7aSThomas Gleixner 	do_div(deltatsc, tmp);
170d683ef7aSThomas Gleixner 
171d683ef7aSThomas Gleixner 	return (unsigned long) deltatsc;
172d683ef7aSThomas Gleixner }
173d683ef7aSThomas Gleixner 
174a977c400SThomas Gleixner #define CAL_MS		10
175cce3e057SThomas Gleixner #define CAL_LATCH	(CLOCK_TICK_RATE / (1000 / CAL_MS))
176a977c400SThomas Gleixner #define CAL_PIT_LOOPS	1000
177a977c400SThomas Gleixner 
178a977c400SThomas Gleixner #define CAL2_MS		50
179a977c400SThomas Gleixner #define CAL2_LATCH	(CLOCK_TICK_RATE / (1000 / CAL2_MS))
180a977c400SThomas Gleixner #define CAL2_PIT_LOOPS	5000
181a977c400SThomas Gleixner 
182cce3e057SThomas Gleixner 
183ec0c15afSLinus Torvalds /*
184ec0c15afSLinus Torvalds  * Try to calibrate the TSC against the Programmable
185ec0c15afSLinus Torvalds  * Interrupt Timer and return the frequency of the TSC
186ec0c15afSLinus Torvalds  * in kHz.
187ec0c15afSLinus Torvalds  *
188ec0c15afSLinus Torvalds  * Return ULONG_MAX on failure to calibrate.
189ec0c15afSLinus Torvalds  */
190a977c400SThomas Gleixner static unsigned long pit_calibrate_tsc(u32 latch, unsigned long ms, int loopmin)
191ec0c15afSLinus Torvalds {
192ec0c15afSLinus Torvalds 	u64 tsc, t1, t2, delta;
193ec0c15afSLinus Torvalds 	unsigned long tscmin, tscmax;
194ec0c15afSLinus Torvalds 	int pitcnt;
195ec0c15afSLinus Torvalds 
196ec0c15afSLinus Torvalds 	/* Set the Gate high, disable speaker */
197ec0c15afSLinus Torvalds 	outb((inb(0x61) & ~0x02) | 0x01, 0x61);
198ec0c15afSLinus Torvalds 
199ec0c15afSLinus Torvalds 	/*
200ec0c15afSLinus Torvalds 	 * Setup CTC channel 2* for mode 0, (interrupt on terminal
201ec0c15afSLinus Torvalds 	 * count mode), binary count. Set the latch register to 50ms
202ec0c15afSLinus Torvalds 	 * (LSB then MSB) to begin countdown.
203ec0c15afSLinus Torvalds 	 */
204ec0c15afSLinus Torvalds 	outb(0xb0, 0x43);
205a977c400SThomas Gleixner 	outb(latch & 0xff, 0x42);
206a977c400SThomas Gleixner 	outb(latch >> 8, 0x42);
207ec0c15afSLinus Torvalds 
208ec0c15afSLinus Torvalds 	tsc = t1 = t2 = get_cycles();
209ec0c15afSLinus Torvalds 
210ec0c15afSLinus Torvalds 	pitcnt = 0;
211ec0c15afSLinus Torvalds 	tscmax = 0;
212ec0c15afSLinus Torvalds 	tscmin = ULONG_MAX;
213ec0c15afSLinus Torvalds 	while ((inb(0x61) & 0x20) == 0) {
214ec0c15afSLinus Torvalds 		t2 = get_cycles();
215ec0c15afSLinus Torvalds 		delta = t2 - tsc;
216ec0c15afSLinus Torvalds 		tsc = t2;
217ec0c15afSLinus Torvalds 		if ((unsigned long) delta < tscmin)
218ec0c15afSLinus Torvalds 			tscmin = (unsigned int) delta;
219ec0c15afSLinus Torvalds 		if ((unsigned long) delta > tscmax)
220ec0c15afSLinus Torvalds 			tscmax = (unsigned int) delta;
221ec0c15afSLinus Torvalds 		pitcnt++;
222ec0c15afSLinus Torvalds 	}
223ec0c15afSLinus Torvalds 
224ec0c15afSLinus Torvalds 	/*
225ec0c15afSLinus Torvalds 	 * Sanity checks:
226ec0c15afSLinus Torvalds 	 *
227a977c400SThomas Gleixner 	 * If we were not able to read the PIT more than loopmin
228ec0c15afSLinus Torvalds 	 * times, then we have been hit by a massive SMI
229ec0c15afSLinus Torvalds 	 *
230ec0c15afSLinus Torvalds 	 * If the maximum is 10 times larger than the minimum,
231ec0c15afSLinus Torvalds 	 * then we got hit by an SMI as well.
232ec0c15afSLinus Torvalds 	 */
233a977c400SThomas Gleixner 	if (pitcnt < loopmin || tscmax > 10 * tscmin)
234ec0c15afSLinus Torvalds 		return ULONG_MAX;
235ec0c15afSLinus Torvalds 
236ec0c15afSLinus Torvalds 	/* Calculate the PIT value */
237ec0c15afSLinus Torvalds 	delta = t2 - t1;
238a977c400SThomas Gleixner 	do_div(delta, ms);
239ec0c15afSLinus Torvalds 	return delta;
240ec0c15afSLinus Torvalds }
241ec0c15afSLinus Torvalds 
2426ac40ed0SLinus Torvalds /*
2436ac40ed0SLinus Torvalds  * This reads the current MSB of the PIT counter, and
2446ac40ed0SLinus Torvalds  * checks if we are running on sufficiently fast and
2456ac40ed0SLinus Torvalds  * non-virtualized hardware.
2466ac40ed0SLinus Torvalds  *
2476ac40ed0SLinus Torvalds  * Our expectations are:
2486ac40ed0SLinus Torvalds  *
2496ac40ed0SLinus Torvalds  *  - the PIT is running at roughly 1.19MHz
2506ac40ed0SLinus Torvalds  *
2516ac40ed0SLinus Torvalds  *  - each IO is going to take about 1us on real hardware,
2526ac40ed0SLinus Torvalds  *    but we allow it to be much faster (by a factor of 10) or
2536ac40ed0SLinus Torvalds  *    _slightly_ slower (ie we allow up to a 2us read+counter
2546ac40ed0SLinus Torvalds  *    update - anything else implies a unacceptably slow CPU
2556ac40ed0SLinus Torvalds  *    or PIT for the fast calibration to work.
2566ac40ed0SLinus Torvalds  *
2576ac40ed0SLinus Torvalds  *  - with 256 PIT ticks to read the value, we have 214us to
2586ac40ed0SLinus Torvalds  *    see the same MSB (and overhead like doing a single TSC
2596ac40ed0SLinus Torvalds  *    read per MSB value etc).
2606ac40ed0SLinus Torvalds  *
2616ac40ed0SLinus Torvalds  *  - We're doing 2 reads per loop (LSB, MSB), and we expect
2626ac40ed0SLinus Torvalds  *    them each to take about a microsecond on real hardware.
2636ac40ed0SLinus Torvalds  *    So we expect a count value of around 100. But we'll be
2646ac40ed0SLinus Torvalds  *    generous, and accept anything over 50.
2656ac40ed0SLinus Torvalds  *
2666ac40ed0SLinus Torvalds  *  - if the PIT is stuck, and we see *many* more reads, we
2676ac40ed0SLinus Torvalds  *    return early (and the next caller of pit_expect_msb()
2686ac40ed0SLinus Torvalds  *    then consider it a failure when they don't see the
2696ac40ed0SLinus Torvalds  *    next expected value).
2706ac40ed0SLinus Torvalds  *
2716ac40ed0SLinus Torvalds  * These expectations mean that we know that we have seen the
2726ac40ed0SLinus Torvalds  * transition from one expected value to another with a fairly
2736ac40ed0SLinus Torvalds  * high accuracy, and we didn't miss any events. We can thus
2746ac40ed0SLinus Torvalds  * use the TSC value at the transitions to calculate a pretty
2756ac40ed0SLinus Torvalds  * good value for the TSC frequencty.
2766ac40ed0SLinus Torvalds  */
2779e8912e0SLinus Torvalds static inline int pit_expect_msb(unsigned char val, u64 *tscp, unsigned long *deltap)
2786ac40ed0SLinus Torvalds {
2799e8912e0SLinus Torvalds 	int count;
2809e8912e0SLinus Torvalds 	u64 tsc = 0;
2816ac40ed0SLinus Torvalds 
2826ac40ed0SLinus Torvalds 	for (count = 0; count < 50000; count++) {
2836ac40ed0SLinus Torvalds 		/* Ignore LSB */
2846ac40ed0SLinus Torvalds 		inb(0x42);
2856ac40ed0SLinus Torvalds 		if (inb(0x42) != val)
2866ac40ed0SLinus Torvalds 			break;
2879e8912e0SLinus Torvalds 		tsc = get_cycles();
2886ac40ed0SLinus Torvalds 	}
2899e8912e0SLinus Torvalds 	*deltap = get_cycles() - tsc;
2909e8912e0SLinus Torvalds 	*tscp = tsc;
2919e8912e0SLinus Torvalds 
2929e8912e0SLinus Torvalds 	/*
2939e8912e0SLinus Torvalds 	 * We require _some_ success, but the quality control
2949e8912e0SLinus Torvalds 	 * will be based on the error terms on the TSC values.
2959e8912e0SLinus Torvalds 	 */
2969e8912e0SLinus Torvalds 	return count > 5;
2976ac40ed0SLinus Torvalds }
2986ac40ed0SLinus Torvalds 
2996ac40ed0SLinus Torvalds /*
3009e8912e0SLinus Torvalds  * How many MSB values do we want to see? We aim for
3019e8912e0SLinus Torvalds  * a maximum error rate of 500ppm (in practice the
3029e8912e0SLinus Torvalds  * real error is much smaller), but refuse to spend
3039e8912e0SLinus Torvalds  * more than 25ms on it.
3046ac40ed0SLinus Torvalds  */
3059e8912e0SLinus Torvalds #define MAX_QUICK_PIT_MS 25
3069e8912e0SLinus Torvalds #define MAX_QUICK_PIT_ITERATIONS (MAX_QUICK_PIT_MS * PIT_TICK_RATE / 1000 / 256)
3076ac40ed0SLinus Torvalds 
3086ac40ed0SLinus Torvalds static unsigned long quick_pit_calibrate(void)
3096ac40ed0SLinus Torvalds {
3109e8912e0SLinus Torvalds 	int i;
3119e8912e0SLinus Torvalds 	u64 tsc, delta;
3129e8912e0SLinus Torvalds 	unsigned long d1, d2;
3139e8912e0SLinus Torvalds 
3146ac40ed0SLinus Torvalds 	/* Set the Gate high, disable speaker */
3156ac40ed0SLinus Torvalds 	outb((inb(0x61) & ~0x02) | 0x01, 0x61);
3166ac40ed0SLinus Torvalds 
3176ac40ed0SLinus Torvalds 	/*
3186ac40ed0SLinus Torvalds 	 * Counter 2, mode 0 (one-shot), binary count
3196ac40ed0SLinus Torvalds 	 *
3206ac40ed0SLinus Torvalds 	 * NOTE! Mode 2 decrements by two (and then the
3216ac40ed0SLinus Torvalds 	 * output is flipped each time, giving the same
3226ac40ed0SLinus Torvalds 	 * final output frequency as a decrement-by-one),
3236ac40ed0SLinus Torvalds 	 * so mode 0 is much better when looking at the
3246ac40ed0SLinus Torvalds 	 * individual counts.
3256ac40ed0SLinus Torvalds 	 */
3266ac40ed0SLinus Torvalds 	outb(0xb0, 0x43);
3276ac40ed0SLinus Torvalds 
3286ac40ed0SLinus Torvalds 	/* Start at 0xffff */
3296ac40ed0SLinus Torvalds 	outb(0xff, 0x42);
3306ac40ed0SLinus Torvalds 	outb(0xff, 0x42);
3316ac40ed0SLinus Torvalds 
332a6a80e1dSLinus Torvalds 	/*
333a6a80e1dSLinus Torvalds 	 * The PIT starts counting at the next edge, so we
334a6a80e1dSLinus Torvalds 	 * need to delay for a microsecond. The easiest way
335a6a80e1dSLinus Torvalds 	 * to do that is to just read back the 16-bit counter
336a6a80e1dSLinus Torvalds 	 * once from the PIT.
337a6a80e1dSLinus Torvalds 	 */
338a6a80e1dSLinus Torvalds 	inb(0x42);
339a6a80e1dSLinus Torvalds 	inb(0x42);
340a6a80e1dSLinus Torvalds 
3419e8912e0SLinus Torvalds 	if (pit_expect_msb(0xff, &tsc, &d1)) {
3429e8912e0SLinus Torvalds 		for (i = 1; i <= MAX_QUICK_PIT_ITERATIONS; i++) {
3439e8912e0SLinus Torvalds 			if (!pit_expect_msb(0xff-i, &delta, &d2))
3449e8912e0SLinus Torvalds 				break;
3456ac40ed0SLinus Torvalds 
3466ac40ed0SLinus Torvalds 			/*
3479e8912e0SLinus Torvalds 			 * Iterate until the error is less than 500 ppm
3484156e9a8SIngo Molnar 			 */
3499e8912e0SLinus Torvalds 			delta -= tsc;
3509e8912e0SLinus Torvalds 			if (d1+d2 < delta >> 11)
3519e8912e0SLinus Torvalds 				goto success;
3529e8912e0SLinus Torvalds 		}
3539e8912e0SLinus Torvalds 	}
3549e8912e0SLinus Torvalds 	printk("Fast TSC calibration failed\n");
3559e8912e0SLinus Torvalds 	return 0;
3564156e9a8SIngo Molnar 
3579e8912e0SLinus Torvalds success:
3584156e9a8SIngo Molnar 	/*
3596ac40ed0SLinus Torvalds 	 * Ok, if we get here, then we've seen the
3609e8912e0SLinus Torvalds 	 * MSB of the PIT decrement 'i' times, and the
3619e8912e0SLinus Torvalds 	 * error has shrunk to less than 500 ppm.
3626ac40ed0SLinus Torvalds 	 *
3636ac40ed0SLinus Torvalds 	 * As a result, we can depend on there not being
3646ac40ed0SLinus Torvalds 	 * any odd delays anywhere, and the TSC reads are
3659e8912e0SLinus Torvalds 	 * reliable (within the error). We also adjust the
3669e8912e0SLinus Torvalds 	 * delta to the middle of the error bars, just
3679e8912e0SLinus Torvalds 	 * because it looks nicer.
3686ac40ed0SLinus Torvalds 	 *
3696ac40ed0SLinus Torvalds 	 * kHz = ticks / time-in-seconds / 1000;
3709e8912e0SLinus Torvalds 	 * kHz = (t2 - t1) / (I * 256 / PIT_TICK_RATE) / 1000
3719e8912e0SLinus Torvalds 	 * kHz = ((t2 - t1) * PIT_TICK_RATE) / (I * 256 * 1000)
3726ac40ed0SLinus Torvalds 	 */
3739e8912e0SLinus Torvalds 	delta += (long)(d2 - d1)/2;
3749e8912e0SLinus Torvalds 	delta *= PIT_TICK_RATE;
3759e8912e0SLinus Torvalds 	do_div(delta, i*256*1000);
3766ac40ed0SLinus Torvalds 	printk("Fast TSC calibration using PIT\n");
3776ac40ed0SLinus Torvalds 	return delta;
3786ac40ed0SLinus Torvalds }
379ec0c15afSLinus Torvalds 
380bfc0f594SAlok Kataria /**
381e93ef949SAlok Kataria  * native_calibrate_tsc - calibrate the tsc on boot
382bfc0f594SAlok Kataria  */
383e93ef949SAlok Kataria unsigned long native_calibrate_tsc(void)
384bfc0f594SAlok Kataria {
385827014beSThomas Gleixner 	u64 tsc1, tsc2, delta, ref1, ref2;
386fbb16e24SThomas Gleixner 	unsigned long tsc_pit_min = ULONG_MAX, tsc_ref_min = ULONG_MAX;
3872c1b284eSJaswinder Singh Rajput 	unsigned long flags, latch, ms, fast_calibrate, hv_tsc_khz;
388a977c400SThomas Gleixner 	int hpet = is_hpet_enabled(), i, loopmin;
389bfc0f594SAlok Kataria 
3902c1b284eSJaswinder Singh Rajput 	hv_tsc_khz = get_hypervisor_tsc_freq();
3912c1b284eSJaswinder Singh Rajput 	if (hv_tsc_khz) {
39288b094fbSAlok Kataria 		printk(KERN_INFO "TSC: Frequency read from the hypervisor\n");
3932c1b284eSJaswinder Singh Rajput 		return hv_tsc_khz;
39488b094fbSAlok Kataria 	}
39588b094fbSAlok Kataria 
396bfc0f594SAlok Kataria 	local_irq_save(flags);
3976ac40ed0SLinus Torvalds 	fast_calibrate = quick_pit_calibrate();
398bfc0f594SAlok Kataria 	local_irq_restore(flags);
3996ac40ed0SLinus Torvalds 	if (fast_calibrate)
4006ac40ed0SLinus Torvalds 		return fast_calibrate;
401fbb16e24SThomas Gleixner 
402fbb16e24SThomas Gleixner 	/*
403fbb16e24SThomas Gleixner 	 * Run 5 calibration loops to get the lowest frequency value
404fbb16e24SThomas Gleixner 	 * (the best estimate). We use two different calibration modes
405fbb16e24SThomas Gleixner 	 * here:
406fbb16e24SThomas Gleixner 	 *
407fbb16e24SThomas Gleixner 	 * 1) PIT loop. We set the PIT Channel 2 to oneshot mode and
408fbb16e24SThomas Gleixner 	 * load a timeout of 50ms. We read the time right after we
409fbb16e24SThomas Gleixner 	 * started the timer and wait until the PIT count down reaches
410fbb16e24SThomas Gleixner 	 * zero. In each wait loop iteration we read the TSC and check
411fbb16e24SThomas Gleixner 	 * the delta to the previous read. We keep track of the min
412fbb16e24SThomas Gleixner 	 * and max values of that delta. The delta is mostly defined
413fbb16e24SThomas Gleixner 	 * by the IO time of the PIT access, so we can detect when a
414fbb16e24SThomas Gleixner 	 * SMI/SMM disturbance happend between the two reads. If the
415fbb16e24SThomas Gleixner 	 * maximum time is significantly larger than the minimum time,
416fbb16e24SThomas Gleixner 	 * then we discard the result and have another try.
417fbb16e24SThomas Gleixner 	 *
418fbb16e24SThomas Gleixner 	 * 2) Reference counter. If available we use the HPET or the
419fbb16e24SThomas Gleixner 	 * PMTIMER as a reference to check the sanity of that value.
420fbb16e24SThomas Gleixner 	 * We use separate TSC readouts and check inside of the
421fbb16e24SThomas Gleixner 	 * reference read for a SMI/SMM disturbance. We dicard
422fbb16e24SThomas Gleixner 	 * disturbed values here as well. We do that around the PIT
423fbb16e24SThomas Gleixner 	 * calibration delay loop as we have to wait for a certain
424fbb16e24SThomas Gleixner 	 * amount of time anyway.
425fbb16e24SThomas Gleixner 	 */
426a977c400SThomas Gleixner 
427a977c400SThomas Gleixner 	/* Preset PIT loop values */
428a977c400SThomas Gleixner 	latch = CAL_LATCH;
429a977c400SThomas Gleixner 	ms = CAL_MS;
430a977c400SThomas Gleixner 	loopmin = CAL_PIT_LOOPS;
431a977c400SThomas Gleixner 
432a977c400SThomas Gleixner 	for (i = 0; i < 3; i++) {
433ec0c15afSLinus Torvalds 		unsigned long tsc_pit_khz;
434bfc0f594SAlok Kataria 
435fbb16e24SThomas Gleixner 		/*
436fbb16e24SThomas Gleixner 		 * Read the start value and the reference count of
437ec0c15afSLinus Torvalds 		 * hpet/pmtimer when available. Then do the PIT
438ec0c15afSLinus Torvalds 		 * calibration, which will take at least 50ms, and
439ec0c15afSLinus Torvalds 		 * read the end value.
440fbb16e24SThomas Gleixner 		 */
441ec0c15afSLinus Torvalds 		local_irq_save(flags);
442827014beSThomas Gleixner 		tsc1 = tsc_read_refs(&ref1, hpet);
443a977c400SThomas Gleixner 		tsc_pit_khz = pit_calibrate_tsc(latch, ms, loopmin);
444827014beSThomas Gleixner 		tsc2 = tsc_read_refs(&ref2, hpet);
445bfc0f594SAlok Kataria 		local_irq_restore(flags);
446bfc0f594SAlok Kataria 
447ec0c15afSLinus Torvalds 		/* Pick the lowest PIT TSC calibration so far */
448ec0c15afSLinus Torvalds 		tsc_pit_min = min(tsc_pit_min, tsc_pit_khz);
449bfc0f594SAlok Kataria 
450bfc0f594SAlok Kataria 		/* hpet or pmtimer available ? */
451827014beSThomas Gleixner 		if (!hpet && !ref1 && !ref2)
452fbb16e24SThomas Gleixner 			continue;
453bfc0f594SAlok Kataria 
454bfc0f594SAlok Kataria 		/* Check, whether the sampling was disturbed by an SMI */
455fbb16e24SThomas Gleixner 		if (tsc1 == ULLONG_MAX || tsc2 == ULLONG_MAX)
456fbb16e24SThomas Gleixner 			continue;
457bfc0f594SAlok Kataria 
458bfc0f594SAlok Kataria 		tsc2 = (tsc2 - tsc1) * 1000000LL;
459d683ef7aSThomas Gleixner 		if (hpet)
460827014beSThomas Gleixner 			tsc2 = calc_hpet_ref(tsc2, ref1, ref2);
461d683ef7aSThomas Gleixner 		else
462827014beSThomas Gleixner 			tsc2 = calc_pmtimer_ref(tsc2, ref1, ref2);
463bfc0f594SAlok Kataria 
464fbb16e24SThomas Gleixner 		tsc_ref_min = min(tsc_ref_min, (unsigned long) tsc2);
465a977c400SThomas Gleixner 
466a977c400SThomas Gleixner 		/* Check the reference deviation */
467a977c400SThomas Gleixner 		delta = ((u64) tsc_pit_min) * 100;
468a977c400SThomas Gleixner 		do_div(delta, tsc_ref_min);
469a977c400SThomas Gleixner 
470a977c400SThomas Gleixner 		/*
471a977c400SThomas Gleixner 		 * If both calibration results are inside a 10% window
472a977c400SThomas Gleixner 		 * then we can be sure, that the calibration
473a977c400SThomas Gleixner 		 * succeeded. We break out of the loop right away. We
474a977c400SThomas Gleixner 		 * use the reference value, as it is more precise.
475a977c400SThomas Gleixner 		 */
476a977c400SThomas Gleixner 		if (delta >= 90 && delta <= 110) {
477a977c400SThomas Gleixner 			printk(KERN_INFO
478a977c400SThomas Gleixner 			       "TSC: PIT calibration matches %s. %d loops\n",
479a977c400SThomas Gleixner 			       hpet ? "HPET" : "PMTIMER", i + 1);
480a977c400SThomas Gleixner 			return tsc_ref_min;
481bfc0f594SAlok Kataria 		}
482bfc0f594SAlok Kataria 
483a977c400SThomas Gleixner 		/*
484a977c400SThomas Gleixner 		 * Check whether PIT failed more than once. This
485a977c400SThomas Gleixner 		 * happens in virtualized environments. We need to
486a977c400SThomas Gleixner 		 * give the virtual PC a slightly longer timeframe for
487a977c400SThomas Gleixner 		 * the HPET/PMTIMER to make the result precise.
488a977c400SThomas Gleixner 		 */
489a977c400SThomas Gleixner 		if (i == 1 && tsc_pit_min == ULONG_MAX) {
490a977c400SThomas Gleixner 			latch = CAL2_LATCH;
491a977c400SThomas Gleixner 			ms = CAL2_MS;
492a977c400SThomas Gleixner 			loopmin = CAL2_PIT_LOOPS;
493a977c400SThomas Gleixner 		}
494bfc0f594SAlok Kataria 	}
495bfc0f594SAlok Kataria 
496fbb16e24SThomas Gleixner 	/*
497fbb16e24SThomas Gleixner 	 * Now check the results.
498fbb16e24SThomas Gleixner 	 */
499fbb16e24SThomas Gleixner 	if (tsc_pit_min == ULONG_MAX) {
500fbb16e24SThomas Gleixner 		/* PIT gave no useful value */
501de014d61SAlok N Kataria 		printk(KERN_WARNING "TSC: Unable to calibrate against PIT\n");
502fbb16e24SThomas Gleixner 
503fbb16e24SThomas Gleixner 		/* We don't have an alternative source, disable TSC */
504827014beSThomas Gleixner 		if (!hpet && !ref1 && !ref2) {
505fbb16e24SThomas Gleixner 			printk("TSC: No reference (HPET/PMTIMER) available\n");
506fbb16e24SThomas Gleixner 			return 0;
507fbb16e24SThomas Gleixner 		}
508fbb16e24SThomas Gleixner 
509fbb16e24SThomas Gleixner 		/* The alternative source failed as well, disable TSC */
510fbb16e24SThomas Gleixner 		if (tsc_ref_min == ULONG_MAX) {
511fbb16e24SThomas Gleixner 			printk(KERN_WARNING "TSC: HPET/PMTIMER calibration "
512a977c400SThomas Gleixner 			       "failed.\n");
513fbb16e24SThomas Gleixner 			return 0;
514fbb16e24SThomas Gleixner 		}
515fbb16e24SThomas Gleixner 
516fbb16e24SThomas Gleixner 		/* Use the alternative source */
517fbb16e24SThomas Gleixner 		printk(KERN_INFO "TSC: using %s reference calibration\n",
518fbb16e24SThomas Gleixner 		       hpet ? "HPET" : "PMTIMER");
519fbb16e24SThomas Gleixner 
520fbb16e24SThomas Gleixner 		return tsc_ref_min;
521fbb16e24SThomas Gleixner 	}
522fbb16e24SThomas Gleixner 
523fbb16e24SThomas Gleixner 	/* We don't have an alternative source, use the PIT calibration value */
524827014beSThomas Gleixner 	if (!hpet && !ref1 && !ref2) {
525fbb16e24SThomas Gleixner 		printk(KERN_INFO "TSC: Using PIT calibration value\n");
526fbb16e24SThomas Gleixner 		return tsc_pit_min;
527fbb16e24SThomas Gleixner 	}
528fbb16e24SThomas Gleixner 
529fbb16e24SThomas Gleixner 	/* The alternative source failed, use the PIT calibration value */
530fbb16e24SThomas Gleixner 	if (tsc_ref_min == ULONG_MAX) {
531a977c400SThomas Gleixner 		printk(KERN_WARNING "TSC: HPET/PMTIMER calibration failed. "
532a977c400SThomas Gleixner 		       "Using PIT calibration\n");
533fbb16e24SThomas Gleixner 		return tsc_pit_min;
534fbb16e24SThomas Gleixner 	}
535fbb16e24SThomas Gleixner 
536fbb16e24SThomas Gleixner 	/*
537fbb16e24SThomas Gleixner 	 * The calibration values differ too much. In doubt, we use
538fbb16e24SThomas Gleixner 	 * the PIT value as we know that there are PMTIMERs around
539a977c400SThomas Gleixner 	 * running at double speed. At least we let the user know:
540fbb16e24SThomas Gleixner 	 */
541a977c400SThomas Gleixner 	printk(KERN_WARNING "TSC: PIT calibration deviates from %s: %lu %lu.\n",
542a977c400SThomas Gleixner 	       hpet ? "HPET" : "PMTIMER", tsc_pit_min, tsc_ref_min);
543fbb16e24SThomas Gleixner 	printk(KERN_INFO "TSC: Using PIT calibration value\n");
544fbb16e24SThomas Gleixner 	return tsc_pit_min;
545fbb16e24SThomas Gleixner }
546bfc0f594SAlok Kataria 
547bfc0f594SAlok Kataria int recalibrate_cpu_khz(void)
548bfc0f594SAlok Kataria {
549bfc0f594SAlok Kataria #ifndef CONFIG_SMP
550bfc0f594SAlok Kataria 	unsigned long cpu_khz_old = cpu_khz;
551bfc0f594SAlok Kataria 
552bfc0f594SAlok Kataria 	if (cpu_has_tsc) {
553e93ef949SAlok Kataria 		tsc_khz = calibrate_tsc();
554e93ef949SAlok Kataria 		cpu_khz = tsc_khz;
555bfc0f594SAlok Kataria 		cpu_data(0).loops_per_jiffy =
556bfc0f594SAlok Kataria 			cpufreq_scale(cpu_data(0).loops_per_jiffy,
557bfc0f594SAlok Kataria 					cpu_khz_old, cpu_khz);
558bfc0f594SAlok Kataria 		return 0;
559bfc0f594SAlok Kataria 	} else
560bfc0f594SAlok Kataria 		return -ENODEV;
561bfc0f594SAlok Kataria #else
562bfc0f594SAlok Kataria 	return -ENODEV;
563bfc0f594SAlok Kataria #endif
564bfc0f594SAlok Kataria }
565bfc0f594SAlok Kataria 
566bfc0f594SAlok Kataria EXPORT_SYMBOL(recalibrate_cpu_khz);
567bfc0f594SAlok Kataria 
5682dbe06faSAlok Kataria 
5692dbe06faSAlok Kataria /* Accelerators for sched_clock()
5702dbe06faSAlok Kataria  * convert from cycles(64bits) => nanoseconds (64bits)
5712dbe06faSAlok Kataria  *  basic equation:
5722dbe06faSAlok Kataria  *              ns = cycles / (freq / ns_per_sec)
5732dbe06faSAlok Kataria  *              ns = cycles * (ns_per_sec / freq)
5742dbe06faSAlok Kataria  *              ns = cycles * (10^9 / (cpu_khz * 10^3))
5752dbe06faSAlok Kataria  *              ns = cycles * (10^6 / cpu_khz)
5762dbe06faSAlok Kataria  *
5772dbe06faSAlok Kataria  *      Then we use scaling math (suggested by george@mvista.com) to get:
5782dbe06faSAlok Kataria  *              ns = cycles * (10^6 * SC / cpu_khz) / SC
5792dbe06faSAlok Kataria  *              ns = cycles * cyc2ns_scale / SC
5802dbe06faSAlok Kataria  *
5812dbe06faSAlok Kataria  *      And since SC is a constant power of two, we can convert the div
5822dbe06faSAlok Kataria  *  into a shift.
5832dbe06faSAlok Kataria  *
5842dbe06faSAlok Kataria  *  We can use khz divisor instead of mhz to keep a better precision, since
5852dbe06faSAlok Kataria  *  cyc2ns_scale is limited to 10^6 * 2^10, which fits in 32 bits.
5862dbe06faSAlok Kataria  *  (mathieu.desnoyers@polymtl.ca)
5872dbe06faSAlok Kataria  *
5882dbe06faSAlok Kataria  *                      -johnstul@us.ibm.com "math is hard, lets go shopping!"
5892dbe06faSAlok Kataria  */
5902dbe06faSAlok Kataria 
5912dbe06faSAlok Kataria DEFINE_PER_CPU(unsigned long, cyc2ns);
5922dbe06faSAlok Kataria 
5938fbbc4b4SAlok Kataria static void set_cyc2ns_scale(unsigned long cpu_khz, int cpu)
5942dbe06faSAlok Kataria {
5952dbe06faSAlok Kataria 	unsigned long long tsc_now, ns_now;
5962dbe06faSAlok Kataria 	unsigned long flags, *scale;
5972dbe06faSAlok Kataria 
5982dbe06faSAlok Kataria 	local_irq_save(flags);
5992dbe06faSAlok Kataria 	sched_clock_idle_sleep_event();
6002dbe06faSAlok Kataria 
6012dbe06faSAlok Kataria 	scale = &per_cpu(cyc2ns, cpu);
6022dbe06faSAlok Kataria 
6032dbe06faSAlok Kataria 	rdtscll(tsc_now);
6042dbe06faSAlok Kataria 	ns_now = __cycles_2_ns(tsc_now);
6052dbe06faSAlok Kataria 
6062dbe06faSAlok Kataria 	if (cpu_khz)
6072dbe06faSAlok Kataria 		*scale = (NSEC_PER_MSEC << CYC2NS_SCALE_FACTOR)/cpu_khz;
6082dbe06faSAlok Kataria 
6092dbe06faSAlok Kataria 	sched_clock_idle_wakeup_event(0);
6102dbe06faSAlok Kataria 	local_irq_restore(flags);
6112dbe06faSAlok Kataria }
6122dbe06faSAlok Kataria 
6132dbe06faSAlok Kataria #ifdef CONFIG_CPU_FREQ
6142dbe06faSAlok Kataria 
6152dbe06faSAlok Kataria /* Frequency scaling support. Adjust the TSC based timer when the cpu frequency
6162dbe06faSAlok Kataria  * changes.
6172dbe06faSAlok Kataria  *
6182dbe06faSAlok Kataria  * RED-PEN: On SMP we assume all CPUs run with the same frequency.  It's
6192dbe06faSAlok Kataria  * not that important because current Opteron setups do not support
6202dbe06faSAlok Kataria  * scaling on SMP anyroads.
6212dbe06faSAlok Kataria  *
6222dbe06faSAlok Kataria  * Should fix up last_tsc too. Currently gettimeofday in the
6232dbe06faSAlok Kataria  * first tick after the change will be slightly wrong.
6242dbe06faSAlok Kataria  */
6252dbe06faSAlok Kataria 
6262dbe06faSAlok Kataria static unsigned int  ref_freq;
6272dbe06faSAlok Kataria static unsigned long loops_per_jiffy_ref;
6282dbe06faSAlok Kataria static unsigned long tsc_khz_ref;
6292dbe06faSAlok Kataria 
6302dbe06faSAlok Kataria static int time_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
6312dbe06faSAlok Kataria 				void *data)
6322dbe06faSAlok Kataria {
6332dbe06faSAlok Kataria 	struct cpufreq_freqs *freq = data;
634931db6a3SDave Jones 	unsigned long *lpj;
6352dbe06faSAlok Kataria 
6362dbe06faSAlok Kataria 	if (cpu_has(&cpu_data(freq->cpu), X86_FEATURE_CONSTANT_TSC))
6372dbe06faSAlok Kataria 		return 0;
6382dbe06faSAlok Kataria 
6392dbe06faSAlok Kataria 	lpj = &boot_cpu_data.loops_per_jiffy;
640931db6a3SDave Jones #ifdef CONFIG_SMP
641931db6a3SDave Jones 	if (!(freq->flags & CPUFREQ_CONST_LOOPS))
642931db6a3SDave Jones 		lpj = &cpu_data(freq->cpu).loops_per_jiffy;
6432dbe06faSAlok Kataria #endif
6442dbe06faSAlok Kataria 
6452dbe06faSAlok Kataria 	if (!ref_freq) {
6462dbe06faSAlok Kataria 		ref_freq = freq->old;
6472dbe06faSAlok Kataria 		loops_per_jiffy_ref = *lpj;
6482dbe06faSAlok Kataria 		tsc_khz_ref = tsc_khz;
6492dbe06faSAlok Kataria 	}
6502dbe06faSAlok Kataria 	if ((val == CPUFREQ_PRECHANGE  && freq->old < freq->new) ||
6512dbe06faSAlok Kataria 			(val == CPUFREQ_POSTCHANGE && freq->old > freq->new) ||
6522dbe06faSAlok Kataria 			(val == CPUFREQ_RESUMECHANGE)) {
6532dbe06faSAlok Kataria 		*lpj = 	cpufreq_scale(loops_per_jiffy_ref, ref_freq, freq->new);
6542dbe06faSAlok Kataria 
6552dbe06faSAlok Kataria 		tsc_khz = cpufreq_scale(tsc_khz_ref, ref_freq, freq->new);
6562dbe06faSAlok Kataria 		if (!(freq->flags & CPUFREQ_CONST_LOOPS))
6572dbe06faSAlok Kataria 			mark_tsc_unstable("cpufreq changes");
6582dbe06faSAlok Kataria 	}
6592dbe06faSAlok Kataria 
66052a8968cSPeter Zijlstra 	set_cyc2ns_scale(tsc_khz, freq->cpu);
6612dbe06faSAlok Kataria 
6622dbe06faSAlok Kataria 	return 0;
6632dbe06faSAlok Kataria }
6642dbe06faSAlok Kataria 
6652dbe06faSAlok Kataria static struct notifier_block time_cpufreq_notifier_block = {
6662dbe06faSAlok Kataria 	.notifier_call  = time_cpufreq_notifier
6672dbe06faSAlok Kataria };
6682dbe06faSAlok Kataria 
6692dbe06faSAlok Kataria static int __init cpufreq_tsc(void)
6702dbe06faSAlok Kataria {
671060700b5SLinus Torvalds 	if (!cpu_has_tsc)
672060700b5SLinus Torvalds 		return 0;
673060700b5SLinus Torvalds 	if (boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
674060700b5SLinus Torvalds 		return 0;
6752dbe06faSAlok Kataria 	cpufreq_register_notifier(&time_cpufreq_notifier_block,
6762dbe06faSAlok Kataria 				CPUFREQ_TRANSITION_NOTIFIER);
6772dbe06faSAlok Kataria 	return 0;
6782dbe06faSAlok Kataria }
6792dbe06faSAlok Kataria 
6802dbe06faSAlok Kataria core_initcall(cpufreq_tsc);
6812dbe06faSAlok Kataria 
6822dbe06faSAlok Kataria #endif /* CONFIG_CPU_FREQ */
6838fbbc4b4SAlok Kataria 
6848fbbc4b4SAlok Kataria /* clocksource code */
6858fbbc4b4SAlok Kataria 
6868fbbc4b4SAlok Kataria static struct clocksource clocksource_tsc;
6878fbbc4b4SAlok Kataria 
6888fbbc4b4SAlok Kataria /*
6898fbbc4b4SAlok Kataria  * We compare the TSC to the cycle_last value in the clocksource
6908fbbc4b4SAlok Kataria  * structure to avoid a nasty time-warp. This can be observed in a
6918fbbc4b4SAlok Kataria  * very small window right after one CPU updated cycle_last under
6928fbbc4b4SAlok Kataria  * xtime/vsyscall_gtod lock and the other CPU reads a TSC value which
6938fbbc4b4SAlok Kataria  * is smaller than the cycle_last reference value due to a TSC which
6948fbbc4b4SAlok Kataria  * is slighty behind. This delta is nowhere else observable, but in
6958fbbc4b4SAlok Kataria  * that case it results in a forward time jump in the range of hours
6968fbbc4b4SAlok Kataria  * due to the unsigned delta calculation of the time keeping core
6978fbbc4b4SAlok Kataria  * code, which is necessary to support wrapping clocksources like pm
6988fbbc4b4SAlok Kataria  * timer.
6998fbbc4b4SAlok Kataria  */
7008e19608eSMagnus Damm static cycle_t read_tsc(struct clocksource *cs)
7018fbbc4b4SAlok Kataria {
7028fbbc4b4SAlok Kataria 	cycle_t ret = (cycle_t)get_cycles();
7038fbbc4b4SAlok Kataria 
7048fbbc4b4SAlok Kataria 	return ret >= clocksource_tsc.cycle_last ?
7058fbbc4b4SAlok Kataria 		ret : clocksource_tsc.cycle_last;
7068fbbc4b4SAlok Kataria }
7078fbbc4b4SAlok Kataria 
708431ceb83SThomas Gleixner #ifdef CONFIG_X86_64
7098fbbc4b4SAlok Kataria static cycle_t __vsyscall_fn vread_tsc(void)
7108fbbc4b4SAlok Kataria {
7117d96fd41SPetr Tesarik 	cycle_t ret;
7127d96fd41SPetr Tesarik 
7137d96fd41SPetr Tesarik 	/*
7147d96fd41SPetr Tesarik 	 * Surround the RDTSC by barriers, to make sure it's not
7157d96fd41SPetr Tesarik 	 * speculated to outside the seqlock critical section and
7167d96fd41SPetr Tesarik 	 * does not cause time warps:
7177d96fd41SPetr Tesarik 	 */
7187d96fd41SPetr Tesarik 	rdtsc_barrier();
7197d96fd41SPetr Tesarik 	ret = (cycle_t)vget_cycles();
7207d96fd41SPetr Tesarik 	rdtsc_barrier();
7218fbbc4b4SAlok Kataria 
7228fbbc4b4SAlok Kataria 	return ret >= __vsyscall_gtod_data.clock.cycle_last ?
7238fbbc4b4SAlok Kataria 		ret : __vsyscall_gtod_data.clock.cycle_last;
7248fbbc4b4SAlok Kataria }
725431ceb83SThomas Gleixner #endif
7268fbbc4b4SAlok Kataria 
7278fbbc4b4SAlok Kataria static struct clocksource clocksource_tsc = {
7288fbbc4b4SAlok Kataria 	.name                   = "tsc",
7298fbbc4b4SAlok Kataria 	.rating                 = 300,
7308fbbc4b4SAlok Kataria 	.read                   = read_tsc,
7318fbbc4b4SAlok Kataria 	.mask                   = CLOCKSOURCE_MASK(64),
7328fbbc4b4SAlok Kataria 	.shift                  = 22,
7338fbbc4b4SAlok Kataria 	.flags                  = CLOCK_SOURCE_IS_CONTINUOUS |
7348fbbc4b4SAlok Kataria 				  CLOCK_SOURCE_MUST_VERIFY,
7358fbbc4b4SAlok Kataria #ifdef CONFIG_X86_64
7368fbbc4b4SAlok Kataria 	.vread                  = vread_tsc,
7378fbbc4b4SAlok Kataria #endif
7388fbbc4b4SAlok Kataria };
7398fbbc4b4SAlok Kataria 
7408fbbc4b4SAlok Kataria void mark_tsc_unstable(char *reason)
7418fbbc4b4SAlok Kataria {
7428fbbc4b4SAlok Kataria 	if (!tsc_unstable) {
7438fbbc4b4SAlok Kataria 		tsc_unstable = 1;
7448fbbc4b4SAlok Kataria 		printk("Marking TSC unstable due to %s\n", reason);
7458fbbc4b4SAlok Kataria 		/* Change only the rating, when not registered */
7468fbbc4b4SAlok Kataria 		if (clocksource_tsc.mult)
7478fbbc4b4SAlok Kataria 			clocksource_change_rating(&clocksource_tsc, 0);
7488fbbc4b4SAlok Kataria 		else
7498fbbc4b4SAlok Kataria 			clocksource_tsc.rating = 0;
7508fbbc4b4SAlok Kataria 	}
7518fbbc4b4SAlok Kataria }
7528fbbc4b4SAlok Kataria 
7538fbbc4b4SAlok Kataria EXPORT_SYMBOL_GPL(mark_tsc_unstable);
7548fbbc4b4SAlok Kataria 
7558fbbc4b4SAlok Kataria static int __init dmi_mark_tsc_unstable(const struct dmi_system_id *d)
7568fbbc4b4SAlok Kataria {
7578fbbc4b4SAlok Kataria 	printk(KERN_NOTICE "%s detected: marking TSC unstable.\n",
7588fbbc4b4SAlok Kataria 			d->ident);
7598fbbc4b4SAlok Kataria 	tsc_unstable = 1;
7608fbbc4b4SAlok Kataria 	return 0;
7618fbbc4b4SAlok Kataria }
7628fbbc4b4SAlok Kataria 
7638fbbc4b4SAlok Kataria /* List of systems that have known TSC problems */
7648fbbc4b4SAlok Kataria static struct dmi_system_id __initdata bad_tsc_dmi_table[] = {
7658fbbc4b4SAlok Kataria 	{
7668fbbc4b4SAlok Kataria 		.callback = dmi_mark_tsc_unstable,
7678fbbc4b4SAlok Kataria 		.ident = "IBM Thinkpad 380XD",
7688fbbc4b4SAlok Kataria 		.matches = {
7698fbbc4b4SAlok Kataria 			DMI_MATCH(DMI_BOARD_VENDOR, "IBM"),
7708fbbc4b4SAlok Kataria 			DMI_MATCH(DMI_BOARD_NAME, "2635FA0"),
7718fbbc4b4SAlok Kataria 		},
7728fbbc4b4SAlok Kataria 	},
7738fbbc4b4SAlok Kataria 	{}
7748fbbc4b4SAlok Kataria };
7758fbbc4b4SAlok Kataria 
776395628efSAlok Kataria static void __init check_system_tsc_reliable(void)
777395628efSAlok Kataria {
7788fbbc4b4SAlok Kataria #ifdef CONFIG_MGEODE_LX
7798fbbc4b4SAlok Kataria 	/* RTSC counts during suspend */
7808fbbc4b4SAlok Kataria #define RTSC_SUSP 0x100
7818fbbc4b4SAlok Kataria 	unsigned long res_low, res_high;
7828fbbc4b4SAlok Kataria 
7838fbbc4b4SAlok Kataria 	rdmsr_safe(MSR_GEODE_BUSCONT_CONF0, &res_low, &res_high);
784395628efSAlok Kataria 	/* Geode_LX - the OLPC CPU has a possibly a very reliable TSC */
7858fbbc4b4SAlok Kataria 	if (res_low & RTSC_SUSP)
786395628efSAlok Kataria 		tsc_clocksource_reliable = 1;
7878fbbc4b4SAlok Kataria #endif
788395628efSAlok Kataria 	if (boot_cpu_has(X86_FEATURE_TSC_RELIABLE))
789395628efSAlok Kataria 		tsc_clocksource_reliable = 1;
790395628efSAlok Kataria }
7918fbbc4b4SAlok Kataria 
7928fbbc4b4SAlok Kataria /*
7938fbbc4b4SAlok Kataria  * Make an educated guess if the TSC is trustworthy and synchronized
7948fbbc4b4SAlok Kataria  * over all CPUs.
7958fbbc4b4SAlok Kataria  */
7968fbbc4b4SAlok Kataria __cpuinit int unsynchronized_tsc(void)
7978fbbc4b4SAlok Kataria {
7988fbbc4b4SAlok Kataria 	if (!cpu_has_tsc || tsc_unstable)
7998fbbc4b4SAlok Kataria 		return 1;
8008fbbc4b4SAlok Kataria 
8013e5095d1SIngo Molnar #ifdef CONFIG_SMP
8028fbbc4b4SAlok Kataria 	if (apic_is_clustered_box())
8038fbbc4b4SAlok Kataria 		return 1;
8048fbbc4b4SAlok Kataria #endif
8058fbbc4b4SAlok Kataria 
8068fbbc4b4SAlok Kataria 	if (boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
8078fbbc4b4SAlok Kataria 		return 0;
8088fbbc4b4SAlok Kataria 	/*
8098fbbc4b4SAlok Kataria 	 * Intel systems are normally all synchronized.
8108fbbc4b4SAlok Kataria 	 * Exceptions must mark TSC as unstable:
8118fbbc4b4SAlok Kataria 	 */
8128fbbc4b4SAlok Kataria 	if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL) {
8138fbbc4b4SAlok Kataria 		/* assume multi socket systems are not synchronized: */
8148fbbc4b4SAlok Kataria 		if (num_possible_cpus() > 1)
8158fbbc4b4SAlok Kataria 			tsc_unstable = 1;
8168fbbc4b4SAlok Kataria 	}
8178fbbc4b4SAlok Kataria 
8188fbbc4b4SAlok Kataria 	return tsc_unstable;
8198fbbc4b4SAlok Kataria }
8208fbbc4b4SAlok Kataria 
8218fbbc4b4SAlok Kataria static void __init init_tsc_clocksource(void)
8228fbbc4b4SAlok Kataria {
8238fbbc4b4SAlok Kataria 	clocksource_tsc.mult = clocksource_khz2mult(tsc_khz,
8248fbbc4b4SAlok Kataria 			clocksource_tsc.shift);
825395628efSAlok Kataria 	if (tsc_clocksource_reliable)
826395628efSAlok Kataria 		clocksource_tsc.flags &= ~CLOCK_SOURCE_MUST_VERIFY;
8278fbbc4b4SAlok Kataria 	/* lower the rating if we already know its unstable: */
8288fbbc4b4SAlok Kataria 	if (check_tsc_unstable()) {
8298fbbc4b4SAlok Kataria 		clocksource_tsc.rating = 0;
8308fbbc4b4SAlok Kataria 		clocksource_tsc.flags &= ~CLOCK_SOURCE_IS_CONTINUOUS;
8318fbbc4b4SAlok Kataria 	}
8328fbbc4b4SAlok Kataria 	clocksource_register(&clocksource_tsc);
8338fbbc4b4SAlok Kataria }
8348fbbc4b4SAlok Kataria 
8358fbbc4b4SAlok Kataria void __init tsc_init(void)
8368fbbc4b4SAlok Kataria {
8378fbbc4b4SAlok Kataria 	u64 lpj;
8388fbbc4b4SAlok Kataria 	int cpu;
8398fbbc4b4SAlok Kataria 
8408fbbc4b4SAlok Kataria 	if (!cpu_has_tsc)
8418fbbc4b4SAlok Kataria 		return;
8428fbbc4b4SAlok Kataria 
843e93ef949SAlok Kataria 	tsc_khz = calibrate_tsc();
844e93ef949SAlok Kataria 	cpu_khz = tsc_khz;
8458fbbc4b4SAlok Kataria 
846e93ef949SAlok Kataria 	if (!tsc_khz) {
8478fbbc4b4SAlok Kataria 		mark_tsc_unstable("could not calculate TSC khz");
8488fbbc4b4SAlok Kataria 		return;
8498fbbc4b4SAlok Kataria 	}
8508fbbc4b4SAlok Kataria 
8518fbbc4b4SAlok Kataria #ifdef CONFIG_X86_64
8528fbbc4b4SAlok Kataria 	if (cpu_has(&boot_cpu_data, X86_FEATURE_CONSTANT_TSC) &&
8538fbbc4b4SAlok Kataria 			(boot_cpu_data.x86_vendor == X86_VENDOR_AMD))
8548fbbc4b4SAlok Kataria 		cpu_khz = calibrate_cpu();
8558fbbc4b4SAlok Kataria #endif
8568fbbc4b4SAlok Kataria 
8578fbbc4b4SAlok Kataria 	printk("Detected %lu.%03lu MHz processor.\n",
8588fbbc4b4SAlok Kataria 			(unsigned long)cpu_khz / 1000,
8598fbbc4b4SAlok Kataria 			(unsigned long)cpu_khz % 1000);
8608fbbc4b4SAlok Kataria 
8618fbbc4b4SAlok Kataria 	/*
8628fbbc4b4SAlok Kataria 	 * Secondary CPUs do not run through tsc_init(), so set up
8638fbbc4b4SAlok Kataria 	 * all the scale factors for all CPUs, assuming the same
8648fbbc4b4SAlok Kataria 	 * speed as the bootup CPU. (cpufreq notifiers will fix this
8658fbbc4b4SAlok Kataria 	 * up if their speed diverges)
8668fbbc4b4SAlok Kataria 	 */
8678fbbc4b4SAlok Kataria 	for_each_possible_cpu(cpu)
8688fbbc4b4SAlok Kataria 		set_cyc2ns_scale(cpu_khz, cpu);
8698fbbc4b4SAlok Kataria 
8708fbbc4b4SAlok Kataria 	if (tsc_disabled > 0)
8718fbbc4b4SAlok Kataria 		return;
8728fbbc4b4SAlok Kataria 
8738fbbc4b4SAlok Kataria 	/* now allow native_sched_clock() to use rdtsc */
8748fbbc4b4SAlok Kataria 	tsc_disabled = 0;
8758fbbc4b4SAlok Kataria 
87670de9a97SAlok Kataria 	lpj = ((u64)tsc_khz * 1000);
87770de9a97SAlok Kataria 	do_div(lpj, HZ);
87870de9a97SAlok Kataria 	lpj_fine = lpj;
87970de9a97SAlok Kataria 
8808fbbc4b4SAlok Kataria 	use_tsc_delay();
8818fbbc4b4SAlok Kataria 	/* Check and install the TSC clocksource */
8828fbbc4b4SAlok Kataria 	dmi_check_system(bad_tsc_dmi_table);
8838fbbc4b4SAlok Kataria 
8848fbbc4b4SAlok Kataria 	if (unsynchronized_tsc())
8858fbbc4b4SAlok Kataria 		mark_tsc_unstable("TSCs unsynchronized");
8868fbbc4b4SAlok Kataria 
887395628efSAlok Kataria 	check_system_tsc_reliable();
8888fbbc4b4SAlok Kataria 	init_tsc_clocksource();
8898fbbc4b4SAlok Kataria }
8908fbbc4b4SAlok Kataria 
891