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> 200ef95533SAlok Kataria 21f24ade3aSIngo Molnar unsigned int __read_mostly cpu_khz; /* TSC clocks / usec, not used here */ 220ef95533SAlok Kataria EXPORT_SYMBOL(cpu_khz); 23f24ade3aSIngo Molnar 24f24ade3aSIngo Molnar unsigned int __read_mostly tsc_khz; 250ef95533SAlok Kataria EXPORT_SYMBOL(tsc_khz); 260ef95533SAlok Kataria 270ef95533SAlok Kataria /* 280ef95533SAlok Kataria * TSC can be unstable due to cpufreq or due to unsynced TSCs 290ef95533SAlok Kataria */ 30f24ade3aSIngo Molnar static int __read_mostly tsc_unstable; 310ef95533SAlok Kataria 320ef95533SAlok Kataria /* native_sched_clock() is called before tsc_init(), so 330ef95533SAlok Kataria we must start with the TSC soft disabled to prevent 340ef95533SAlok Kataria erroneous rdtsc usage on !cpu_has_tsc processors */ 35f24ade3aSIngo Molnar static int __read_mostly tsc_disabled = -1; 360ef95533SAlok Kataria 37395628efSAlok Kataria static int tsc_clocksource_reliable; 380ef95533SAlok Kataria /* 390ef95533SAlok Kataria * Scheduler clock - returns current time in nanosec units. 400ef95533SAlok Kataria */ 410ef95533SAlok Kataria u64 native_sched_clock(void) 420ef95533SAlok Kataria { 430ef95533SAlok Kataria u64 this_offset; 440ef95533SAlok Kataria 450ef95533SAlok Kataria /* 460ef95533SAlok Kataria * Fall back to jiffies if there's no TSC available: 470ef95533SAlok Kataria * ( But note that we still use it if the TSC is marked 480ef95533SAlok Kataria * unstable. We do this because unlike Time Of Day, 490ef95533SAlok Kataria * the scheduler clock tolerates small errors and it's 500ef95533SAlok Kataria * very important for it to be as fast as the platform 510ef95533SAlok Kataria * can achive it. ) 520ef95533SAlok Kataria */ 530ef95533SAlok Kataria if (unlikely(tsc_disabled)) { 540ef95533SAlok Kataria /* No locking but a rare wrong value is not a big deal: */ 550ef95533SAlok Kataria return (jiffies_64 - INITIAL_JIFFIES) * (1000000000 / HZ); 560ef95533SAlok Kataria } 570ef95533SAlok Kataria 580ef95533SAlok Kataria /* read the Time Stamp Counter: */ 590ef95533SAlok Kataria rdtscll(this_offset); 600ef95533SAlok Kataria 610ef95533SAlok Kataria /* return the value in ns */ 627cbaef9cSIngo Molnar return __cycles_2_ns(this_offset); 630ef95533SAlok Kataria } 640ef95533SAlok Kataria 650ef95533SAlok Kataria /* We need to define a real function for sched_clock, to override the 660ef95533SAlok Kataria weak default version */ 670ef95533SAlok Kataria #ifdef CONFIG_PARAVIRT 680ef95533SAlok Kataria unsigned long long sched_clock(void) 690ef95533SAlok Kataria { 700ef95533SAlok Kataria return paravirt_sched_clock(); 710ef95533SAlok Kataria } 720ef95533SAlok Kataria #else 730ef95533SAlok Kataria unsigned long long 740ef95533SAlok Kataria sched_clock(void) __attribute__((alias("native_sched_clock"))); 750ef95533SAlok Kataria #endif 760ef95533SAlok Kataria 770ef95533SAlok Kataria int check_tsc_unstable(void) 780ef95533SAlok Kataria { 790ef95533SAlok Kataria return tsc_unstable; 800ef95533SAlok Kataria } 810ef95533SAlok Kataria EXPORT_SYMBOL_GPL(check_tsc_unstable); 820ef95533SAlok Kataria 830ef95533SAlok Kataria #ifdef CONFIG_X86_TSC 840ef95533SAlok Kataria int __init notsc_setup(char *str) 850ef95533SAlok Kataria { 860ef95533SAlok Kataria printk(KERN_WARNING "notsc: Kernel compiled with CONFIG_X86_TSC, " 870ef95533SAlok Kataria "cannot disable TSC completely.\n"); 880ef95533SAlok Kataria tsc_disabled = 1; 890ef95533SAlok Kataria return 1; 900ef95533SAlok Kataria } 910ef95533SAlok Kataria #else 920ef95533SAlok Kataria /* 930ef95533SAlok Kataria * disable flag for tsc. Takes effect by clearing the TSC cpu flag 940ef95533SAlok Kataria * in cpu/common.c 950ef95533SAlok Kataria */ 960ef95533SAlok Kataria int __init notsc_setup(char *str) 970ef95533SAlok Kataria { 980ef95533SAlok Kataria setup_clear_cpu_cap(X86_FEATURE_TSC); 990ef95533SAlok Kataria return 1; 1000ef95533SAlok Kataria } 1010ef95533SAlok Kataria #endif 1020ef95533SAlok Kataria 1030ef95533SAlok Kataria __setup("notsc", notsc_setup); 104bfc0f594SAlok Kataria 105395628efSAlok Kataria static int __init tsc_setup(char *str) 106395628efSAlok Kataria { 107395628efSAlok Kataria if (!strcmp(str, "reliable")) 108395628efSAlok Kataria tsc_clocksource_reliable = 1; 109395628efSAlok Kataria return 1; 110395628efSAlok Kataria } 111395628efSAlok Kataria 112395628efSAlok Kataria __setup("tsc=", tsc_setup); 113395628efSAlok Kataria 114bfc0f594SAlok Kataria #define MAX_RETRIES 5 115bfc0f594SAlok Kataria #define SMI_TRESHOLD 50000 116bfc0f594SAlok Kataria 117bfc0f594SAlok Kataria /* 118bfc0f594SAlok Kataria * Read TSC and the reference counters. Take care of SMI disturbance 119bfc0f594SAlok Kataria */ 120827014beSThomas Gleixner static u64 tsc_read_refs(u64 *p, int hpet) 121bfc0f594SAlok Kataria { 122bfc0f594SAlok Kataria u64 t1, t2; 123bfc0f594SAlok Kataria int i; 124bfc0f594SAlok Kataria 125bfc0f594SAlok Kataria for (i = 0; i < MAX_RETRIES; i++) { 126bfc0f594SAlok Kataria t1 = get_cycles(); 127bfc0f594SAlok Kataria if (hpet) 128827014beSThomas Gleixner *p = hpet_readl(HPET_COUNTER) & 0xFFFFFFFF; 129bfc0f594SAlok Kataria else 130827014beSThomas Gleixner *p = acpi_pm_read_early(); 131bfc0f594SAlok Kataria t2 = get_cycles(); 132bfc0f594SAlok Kataria if ((t2 - t1) < SMI_TRESHOLD) 133bfc0f594SAlok Kataria return t2; 134bfc0f594SAlok Kataria } 135bfc0f594SAlok Kataria return ULLONG_MAX; 136bfc0f594SAlok Kataria } 137bfc0f594SAlok Kataria 138ec0c15afSLinus Torvalds /* 139d683ef7aSThomas Gleixner * Calculate the TSC frequency from HPET reference 140d683ef7aSThomas Gleixner */ 141d683ef7aSThomas Gleixner static unsigned long calc_hpet_ref(u64 deltatsc, u64 hpet1, u64 hpet2) 142d683ef7aSThomas Gleixner { 143d683ef7aSThomas Gleixner u64 tmp; 144d683ef7aSThomas Gleixner 145d683ef7aSThomas Gleixner if (hpet2 < hpet1) 146d683ef7aSThomas Gleixner hpet2 += 0x100000000ULL; 147d683ef7aSThomas Gleixner hpet2 -= hpet1; 148d683ef7aSThomas Gleixner tmp = ((u64)hpet2 * hpet_readl(HPET_PERIOD)); 149d683ef7aSThomas Gleixner do_div(tmp, 1000000); 150d683ef7aSThomas Gleixner do_div(deltatsc, tmp); 151d683ef7aSThomas Gleixner 152d683ef7aSThomas Gleixner return (unsigned long) deltatsc; 153d683ef7aSThomas Gleixner } 154d683ef7aSThomas Gleixner 155d683ef7aSThomas Gleixner /* 156d683ef7aSThomas Gleixner * Calculate the TSC frequency from PMTimer reference 157d683ef7aSThomas Gleixner */ 158d683ef7aSThomas Gleixner static unsigned long calc_pmtimer_ref(u64 deltatsc, u64 pm1, u64 pm2) 159d683ef7aSThomas Gleixner { 160d683ef7aSThomas Gleixner u64 tmp; 161d683ef7aSThomas Gleixner 162d683ef7aSThomas Gleixner if (!pm1 && !pm2) 163d683ef7aSThomas Gleixner return ULONG_MAX; 164d683ef7aSThomas Gleixner 165d683ef7aSThomas Gleixner if (pm2 < pm1) 166d683ef7aSThomas Gleixner pm2 += (u64)ACPI_PM_OVRRUN; 167d683ef7aSThomas Gleixner pm2 -= pm1; 168d683ef7aSThomas Gleixner tmp = pm2 * 1000000000LL; 169d683ef7aSThomas Gleixner do_div(tmp, PMTMR_TICKS_PER_SEC); 170d683ef7aSThomas Gleixner do_div(deltatsc, tmp); 171d683ef7aSThomas Gleixner 172d683ef7aSThomas Gleixner return (unsigned long) deltatsc; 173d683ef7aSThomas Gleixner } 174d683ef7aSThomas Gleixner 175a977c400SThomas Gleixner #define CAL_MS 10 176cce3e057SThomas Gleixner #define CAL_LATCH (CLOCK_TICK_RATE / (1000 / CAL_MS)) 177a977c400SThomas Gleixner #define CAL_PIT_LOOPS 1000 178a977c400SThomas Gleixner 179a977c400SThomas Gleixner #define CAL2_MS 50 180a977c400SThomas Gleixner #define CAL2_LATCH (CLOCK_TICK_RATE / (1000 / CAL2_MS)) 181a977c400SThomas Gleixner #define CAL2_PIT_LOOPS 5000 182a977c400SThomas Gleixner 183cce3e057SThomas Gleixner 184ec0c15afSLinus Torvalds /* 185ec0c15afSLinus Torvalds * Try to calibrate the TSC against the Programmable 186ec0c15afSLinus Torvalds * Interrupt Timer and return the frequency of the TSC 187ec0c15afSLinus Torvalds * in kHz. 188ec0c15afSLinus Torvalds * 189ec0c15afSLinus Torvalds * Return ULONG_MAX on failure to calibrate. 190ec0c15afSLinus Torvalds */ 191a977c400SThomas Gleixner static unsigned long pit_calibrate_tsc(u32 latch, unsigned long ms, int loopmin) 192ec0c15afSLinus Torvalds { 193ec0c15afSLinus Torvalds u64 tsc, t1, t2, delta; 194ec0c15afSLinus Torvalds unsigned long tscmin, tscmax; 195ec0c15afSLinus Torvalds int pitcnt; 196ec0c15afSLinus Torvalds 197ec0c15afSLinus Torvalds /* Set the Gate high, disable speaker */ 198ec0c15afSLinus Torvalds outb((inb(0x61) & ~0x02) | 0x01, 0x61); 199ec0c15afSLinus Torvalds 200ec0c15afSLinus Torvalds /* 201ec0c15afSLinus Torvalds * Setup CTC channel 2* for mode 0, (interrupt on terminal 202ec0c15afSLinus Torvalds * count mode), binary count. Set the latch register to 50ms 203ec0c15afSLinus Torvalds * (LSB then MSB) to begin countdown. 204ec0c15afSLinus Torvalds */ 205ec0c15afSLinus Torvalds outb(0xb0, 0x43); 206a977c400SThomas Gleixner outb(latch & 0xff, 0x42); 207a977c400SThomas Gleixner outb(latch >> 8, 0x42); 208ec0c15afSLinus Torvalds 209ec0c15afSLinus Torvalds tsc = t1 = t2 = get_cycles(); 210ec0c15afSLinus Torvalds 211ec0c15afSLinus Torvalds pitcnt = 0; 212ec0c15afSLinus Torvalds tscmax = 0; 213ec0c15afSLinus Torvalds tscmin = ULONG_MAX; 214ec0c15afSLinus Torvalds while ((inb(0x61) & 0x20) == 0) { 215ec0c15afSLinus Torvalds t2 = get_cycles(); 216ec0c15afSLinus Torvalds delta = t2 - tsc; 217ec0c15afSLinus Torvalds tsc = t2; 218ec0c15afSLinus Torvalds if ((unsigned long) delta < tscmin) 219ec0c15afSLinus Torvalds tscmin = (unsigned int) delta; 220ec0c15afSLinus Torvalds if ((unsigned long) delta > tscmax) 221ec0c15afSLinus Torvalds tscmax = (unsigned int) delta; 222ec0c15afSLinus Torvalds pitcnt++; 223ec0c15afSLinus Torvalds } 224ec0c15afSLinus Torvalds 225ec0c15afSLinus Torvalds /* 226ec0c15afSLinus Torvalds * Sanity checks: 227ec0c15afSLinus Torvalds * 228a977c400SThomas Gleixner * If we were not able to read the PIT more than loopmin 229ec0c15afSLinus Torvalds * times, then we have been hit by a massive SMI 230ec0c15afSLinus Torvalds * 231ec0c15afSLinus Torvalds * If the maximum is 10 times larger than the minimum, 232ec0c15afSLinus Torvalds * then we got hit by an SMI as well. 233ec0c15afSLinus Torvalds */ 234a977c400SThomas Gleixner if (pitcnt < loopmin || tscmax > 10 * tscmin) 235ec0c15afSLinus Torvalds return ULONG_MAX; 236ec0c15afSLinus Torvalds 237ec0c15afSLinus Torvalds /* Calculate the PIT value */ 238ec0c15afSLinus Torvalds delta = t2 - t1; 239a977c400SThomas Gleixner do_div(delta, ms); 240ec0c15afSLinus Torvalds return delta; 241ec0c15afSLinus Torvalds } 242ec0c15afSLinus Torvalds 2436ac40ed0SLinus Torvalds /* 2446ac40ed0SLinus Torvalds * This reads the current MSB of the PIT counter, and 2456ac40ed0SLinus Torvalds * checks if we are running on sufficiently fast and 2466ac40ed0SLinus Torvalds * non-virtualized hardware. 2476ac40ed0SLinus Torvalds * 2486ac40ed0SLinus Torvalds * Our expectations are: 2496ac40ed0SLinus Torvalds * 2506ac40ed0SLinus Torvalds * - the PIT is running at roughly 1.19MHz 2516ac40ed0SLinus Torvalds * 2526ac40ed0SLinus Torvalds * - each IO is going to take about 1us on real hardware, 2536ac40ed0SLinus Torvalds * but we allow it to be much faster (by a factor of 10) or 2546ac40ed0SLinus Torvalds * _slightly_ slower (ie we allow up to a 2us read+counter 2556ac40ed0SLinus Torvalds * update - anything else implies a unacceptably slow CPU 2566ac40ed0SLinus Torvalds * or PIT for the fast calibration to work. 2576ac40ed0SLinus Torvalds * 2586ac40ed0SLinus Torvalds * - with 256 PIT ticks to read the value, we have 214us to 2596ac40ed0SLinus Torvalds * see the same MSB (and overhead like doing a single TSC 2606ac40ed0SLinus Torvalds * read per MSB value etc). 2616ac40ed0SLinus Torvalds * 2626ac40ed0SLinus Torvalds * - We're doing 2 reads per loop (LSB, MSB), and we expect 2636ac40ed0SLinus Torvalds * them each to take about a microsecond on real hardware. 2646ac40ed0SLinus Torvalds * So we expect a count value of around 100. But we'll be 2656ac40ed0SLinus Torvalds * generous, and accept anything over 50. 2666ac40ed0SLinus Torvalds * 2676ac40ed0SLinus Torvalds * - if the PIT is stuck, and we see *many* more reads, we 2686ac40ed0SLinus Torvalds * return early (and the next caller of pit_expect_msb() 2696ac40ed0SLinus Torvalds * then consider it a failure when they don't see the 2706ac40ed0SLinus Torvalds * next expected value). 2716ac40ed0SLinus Torvalds * 2726ac40ed0SLinus Torvalds * These expectations mean that we know that we have seen the 2736ac40ed0SLinus Torvalds * transition from one expected value to another with a fairly 2746ac40ed0SLinus Torvalds * high accuracy, and we didn't miss any events. We can thus 2756ac40ed0SLinus Torvalds * use the TSC value at the transitions to calculate a pretty 2766ac40ed0SLinus Torvalds * good value for the TSC frequencty. 2776ac40ed0SLinus Torvalds */ 278b6e61eefSLinus Torvalds static inline int pit_verify_msb(unsigned char val) 279b6e61eefSLinus Torvalds { 280b6e61eefSLinus Torvalds /* Ignore LSB */ 281b6e61eefSLinus Torvalds inb(0x42); 282b6e61eefSLinus Torvalds return inb(0x42) == val; 283b6e61eefSLinus Torvalds } 284b6e61eefSLinus Torvalds 2859e8912e0SLinus Torvalds static inline int pit_expect_msb(unsigned char val, u64 *tscp, unsigned long *deltap) 2866ac40ed0SLinus Torvalds { 2879e8912e0SLinus Torvalds int count; 2889e8912e0SLinus Torvalds u64 tsc = 0; 2896ac40ed0SLinus Torvalds 2906ac40ed0SLinus Torvalds for (count = 0; count < 50000; count++) { 291b6e61eefSLinus Torvalds if (!pit_verify_msb(val)) 2926ac40ed0SLinus Torvalds break; 2939e8912e0SLinus Torvalds tsc = get_cycles(); 2946ac40ed0SLinus Torvalds } 2959e8912e0SLinus Torvalds *deltap = get_cycles() - tsc; 2969e8912e0SLinus Torvalds *tscp = tsc; 2979e8912e0SLinus Torvalds 2989e8912e0SLinus Torvalds /* 2999e8912e0SLinus Torvalds * We require _some_ success, but the quality control 3009e8912e0SLinus Torvalds * will be based on the error terms on the TSC values. 3019e8912e0SLinus Torvalds */ 3029e8912e0SLinus Torvalds return count > 5; 3036ac40ed0SLinus Torvalds } 3046ac40ed0SLinus Torvalds 3056ac40ed0SLinus Torvalds /* 3069e8912e0SLinus Torvalds * How many MSB values do we want to see? We aim for 3079e8912e0SLinus Torvalds * a maximum error rate of 500ppm (in practice the 3089e8912e0SLinus Torvalds * real error is much smaller), but refuse to spend 3099e8912e0SLinus Torvalds * more than 25ms on it. 3106ac40ed0SLinus Torvalds */ 3119e8912e0SLinus Torvalds #define MAX_QUICK_PIT_MS 25 3129e8912e0SLinus Torvalds #define MAX_QUICK_PIT_ITERATIONS (MAX_QUICK_PIT_MS * PIT_TICK_RATE / 1000 / 256) 3136ac40ed0SLinus Torvalds 3146ac40ed0SLinus Torvalds static unsigned long quick_pit_calibrate(void) 3156ac40ed0SLinus Torvalds { 3169e8912e0SLinus Torvalds int i; 3179e8912e0SLinus Torvalds u64 tsc, delta; 3189e8912e0SLinus Torvalds unsigned long d1, d2; 3199e8912e0SLinus Torvalds 3206ac40ed0SLinus Torvalds /* Set the Gate high, disable speaker */ 3216ac40ed0SLinus Torvalds outb((inb(0x61) & ~0x02) | 0x01, 0x61); 3226ac40ed0SLinus Torvalds 3236ac40ed0SLinus Torvalds /* 3246ac40ed0SLinus Torvalds * Counter 2, mode 0 (one-shot), binary count 3256ac40ed0SLinus Torvalds * 3266ac40ed0SLinus Torvalds * NOTE! Mode 2 decrements by two (and then the 3276ac40ed0SLinus Torvalds * output is flipped each time, giving the same 3286ac40ed0SLinus Torvalds * final output frequency as a decrement-by-one), 3296ac40ed0SLinus Torvalds * so mode 0 is much better when looking at the 3306ac40ed0SLinus Torvalds * individual counts. 3316ac40ed0SLinus Torvalds */ 3326ac40ed0SLinus Torvalds outb(0xb0, 0x43); 3336ac40ed0SLinus Torvalds 3346ac40ed0SLinus Torvalds /* Start at 0xffff */ 3356ac40ed0SLinus Torvalds outb(0xff, 0x42); 3366ac40ed0SLinus Torvalds outb(0xff, 0x42); 3376ac40ed0SLinus Torvalds 338a6a80e1dSLinus Torvalds /* 339a6a80e1dSLinus Torvalds * The PIT starts counting at the next edge, so we 340a6a80e1dSLinus Torvalds * need to delay for a microsecond. The easiest way 341a6a80e1dSLinus Torvalds * to do that is to just read back the 16-bit counter 342a6a80e1dSLinus Torvalds * once from the PIT. 343a6a80e1dSLinus Torvalds */ 344b6e61eefSLinus Torvalds pit_verify_msb(0); 345a6a80e1dSLinus Torvalds 3469e8912e0SLinus Torvalds if (pit_expect_msb(0xff, &tsc, &d1)) { 3479e8912e0SLinus Torvalds for (i = 1; i <= MAX_QUICK_PIT_ITERATIONS; i++) { 3489e8912e0SLinus Torvalds if (!pit_expect_msb(0xff-i, &delta, &d2)) 3499e8912e0SLinus Torvalds break; 3506ac40ed0SLinus Torvalds 3516ac40ed0SLinus Torvalds /* 3529e8912e0SLinus Torvalds * Iterate until the error is less than 500 ppm 3534156e9a8SIngo Molnar */ 3549e8912e0SLinus Torvalds delta -= tsc; 355b6e61eefSLinus Torvalds if (d1+d2 >= delta >> 11) 356b6e61eefSLinus Torvalds continue; 357b6e61eefSLinus Torvalds 358b6e61eefSLinus Torvalds /* 359b6e61eefSLinus Torvalds * Check the PIT one more time to verify that 360b6e61eefSLinus Torvalds * all TSC reads were stable wrt the PIT. 361b6e61eefSLinus Torvalds * 362b6e61eefSLinus Torvalds * This also guarantees serialization of the 363b6e61eefSLinus Torvalds * last cycle read ('d2') in pit_expect_msb. 364b6e61eefSLinus Torvalds */ 365b6e61eefSLinus Torvalds if (!pit_verify_msb(0xfe - i)) 366b6e61eefSLinus Torvalds break; 3679e8912e0SLinus Torvalds goto success; 3689e8912e0SLinus Torvalds } 3699e8912e0SLinus Torvalds } 3709e8912e0SLinus Torvalds printk("Fast TSC calibration failed\n"); 3719e8912e0SLinus Torvalds return 0; 3724156e9a8SIngo Molnar 3739e8912e0SLinus Torvalds success: 3744156e9a8SIngo Molnar /* 3756ac40ed0SLinus Torvalds * Ok, if we get here, then we've seen the 3769e8912e0SLinus Torvalds * MSB of the PIT decrement 'i' times, and the 3779e8912e0SLinus Torvalds * error has shrunk to less than 500 ppm. 3786ac40ed0SLinus Torvalds * 3796ac40ed0SLinus Torvalds * As a result, we can depend on there not being 3806ac40ed0SLinus Torvalds * any odd delays anywhere, and the TSC reads are 3819e8912e0SLinus Torvalds * reliable (within the error). We also adjust the 3829e8912e0SLinus Torvalds * delta to the middle of the error bars, just 3839e8912e0SLinus Torvalds * because it looks nicer. 3846ac40ed0SLinus Torvalds * 3856ac40ed0SLinus Torvalds * kHz = ticks / time-in-seconds / 1000; 3869e8912e0SLinus Torvalds * kHz = (t2 - t1) / (I * 256 / PIT_TICK_RATE) / 1000 3879e8912e0SLinus Torvalds * kHz = ((t2 - t1) * PIT_TICK_RATE) / (I * 256 * 1000) 3886ac40ed0SLinus Torvalds */ 3899e8912e0SLinus Torvalds delta += (long)(d2 - d1)/2; 3909e8912e0SLinus Torvalds delta *= PIT_TICK_RATE; 3919e8912e0SLinus Torvalds do_div(delta, i*256*1000); 3926ac40ed0SLinus Torvalds printk("Fast TSC calibration using PIT\n"); 3936ac40ed0SLinus Torvalds return delta; 3946ac40ed0SLinus Torvalds } 395ec0c15afSLinus Torvalds 396bfc0f594SAlok Kataria /** 397e93ef949SAlok Kataria * native_calibrate_tsc - calibrate the tsc on boot 398bfc0f594SAlok Kataria */ 399e93ef949SAlok Kataria unsigned long native_calibrate_tsc(void) 400bfc0f594SAlok Kataria { 401827014beSThomas Gleixner u64 tsc1, tsc2, delta, ref1, ref2; 402fbb16e24SThomas Gleixner unsigned long tsc_pit_min = ULONG_MAX, tsc_ref_min = ULONG_MAX; 4032c1b284eSJaswinder Singh Rajput unsigned long flags, latch, ms, fast_calibrate, hv_tsc_khz; 404a977c400SThomas Gleixner int hpet = is_hpet_enabled(), i, loopmin; 405bfc0f594SAlok Kataria 4062c1b284eSJaswinder Singh Rajput hv_tsc_khz = get_hypervisor_tsc_freq(); 4072c1b284eSJaswinder Singh Rajput if (hv_tsc_khz) { 40888b094fbSAlok Kataria printk(KERN_INFO "TSC: Frequency read from the hypervisor\n"); 4092c1b284eSJaswinder Singh Rajput return hv_tsc_khz; 41088b094fbSAlok Kataria } 41188b094fbSAlok 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) { 569e93ef949SAlok Kataria tsc_khz = 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 6332dbe06faSAlok Kataria #ifdef CONFIG_CPU_FREQ 6342dbe06faSAlok Kataria 6352dbe06faSAlok Kataria /* Frequency scaling support. Adjust the TSC based timer when the cpu frequency 6362dbe06faSAlok Kataria * changes. 6372dbe06faSAlok Kataria * 6382dbe06faSAlok Kataria * RED-PEN: On SMP we assume all CPUs run with the same frequency. It's 6392dbe06faSAlok Kataria * not that important because current Opteron setups do not support 6402dbe06faSAlok Kataria * scaling on SMP anyroads. 6412dbe06faSAlok Kataria * 6422dbe06faSAlok Kataria * Should fix up last_tsc too. Currently gettimeofday in the 6432dbe06faSAlok Kataria * first tick after the change will be slightly wrong. 6442dbe06faSAlok Kataria */ 6452dbe06faSAlok Kataria 6462dbe06faSAlok Kataria static unsigned int ref_freq; 6472dbe06faSAlok Kataria static unsigned long loops_per_jiffy_ref; 6482dbe06faSAlok Kataria static unsigned long tsc_khz_ref; 6492dbe06faSAlok Kataria 6502dbe06faSAlok Kataria static int time_cpufreq_notifier(struct notifier_block *nb, unsigned long val, 6512dbe06faSAlok Kataria void *data) 6522dbe06faSAlok Kataria { 6532dbe06faSAlok Kataria struct cpufreq_freqs *freq = data; 654931db6a3SDave Jones unsigned long *lpj; 6552dbe06faSAlok Kataria 6562dbe06faSAlok Kataria if (cpu_has(&cpu_data(freq->cpu), X86_FEATURE_CONSTANT_TSC)) 6572dbe06faSAlok Kataria return 0; 6582dbe06faSAlok Kataria 6592dbe06faSAlok Kataria lpj = &boot_cpu_data.loops_per_jiffy; 660931db6a3SDave Jones #ifdef CONFIG_SMP 661931db6a3SDave Jones if (!(freq->flags & CPUFREQ_CONST_LOOPS)) 662931db6a3SDave Jones lpj = &cpu_data(freq->cpu).loops_per_jiffy; 6632dbe06faSAlok Kataria #endif 6642dbe06faSAlok Kataria 6652dbe06faSAlok Kataria if (!ref_freq) { 6662dbe06faSAlok Kataria ref_freq = freq->old; 6672dbe06faSAlok Kataria loops_per_jiffy_ref = *lpj; 6682dbe06faSAlok Kataria tsc_khz_ref = tsc_khz; 6692dbe06faSAlok Kataria } 6702dbe06faSAlok Kataria if ((val == CPUFREQ_PRECHANGE && freq->old < freq->new) || 6712dbe06faSAlok Kataria (val == CPUFREQ_POSTCHANGE && freq->old > freq->new) || 6722dbe06faSAlok Kataria (val == CPUFREQ_RESUMECHANGE)) { 6732dbe06faSAlok Kataria *lpj = cpufreq_scale(loops_per_jiffy_ref, ref_freq, freq->new); 6742dbe06faSAlok Kataria 6752dbe06faSAlok Kataria tsc_khz = cpufreq_scale(tsc_khz_ref, ref_freq, freq->new); 6762dbe06faSAlok Kataria if (!(freq->flags & CPUFREQ_CONST_LOOPS)) 6772dbe06faSAlok Kataria mark_tsc_unstable("cpufreq changes"); 6782dbe06faSAlok Kataria } 6792dbe06faSAlok Kataria 68052a8968cSPeter Zijlstra set_cyc2ns_scale(tsc_khz, freq->cpu); 6812dbe06faSAlok Kataria 6822dbe06faSAlok Kataria return 0; 6832dbe06faSAlok Kataria } 6842dbe06faSAlok Kataria 6852dbe06faSAlok Kataria static struct notifier_block time_cpufreq_notifier_block = { 6862dbe06faSAlok Kataria .notifier_call = time_cpufreq_notifier 6872dbe06faSAlok Kataria }; 6882dbe06faSAlok Kataria 6892dbe06faSAlok Kataria static int __init cpufreq_tsc(void) 6902dbe06faSAlok Kataria { 691060700b5SLinus Torvalds if (!cpu_has_tsc) 692060700b5SLinus Torvalds return 0; 693060700b5SLinus Torvalds if (boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) 694060700b5SLinus Torvalds return 0; 6952dbe06faSAlok Kataria cpufreq_register_notifier(&time_cpufreq_notifier_block, 6962dbe06faSAlok Kataria CPUFREQ_TRANSITION_NOTIFIER); 6972dbe06faSAlok Kataria return 0; 6982dbe06faSAlok Kataria } 6992dbe06faSAlok Kataria 7002dbe06faSAlok Kataria core_initcall(cpufreq_tsc); 7012dbe06faSAlok Kataria 7022dbe06faSAlok Kataria #endif /* CONFIG_CPU_FREQ */ 7038fbbc4b4SAlok Kataria 7048fbbc4b4SAlok Kataria /* clocksource code */ 7058fbbc4b4SAlok Kataria 7068fbbc4b4SAlok Kataria static struct clocksource clocksource_tsc; 7078fbbc4b4SAlok Kataria 7088fbbc4b4SAlok Kataria /* 7098fbbc4b4SAlok Kataria * We compare the TSC to the cycle_last value in the clocksource 7108fbbc4b4SAlok Kataria * structure to avoid a nasty time-warp. This can be observed in a 7118fbbc4b4SAlok Kataria * very small window right after one CPU updated cycle_last under 7128fbbc4b4SAlok Kataria * xtime/vsyscall_gtod lock and the other CPU reads a TSC value which 7138fbbc4b4SAlok Kataria * is smaller than the cycle_last reference value due to a TSC which 7148fbbc4b4SAlok Kataria * is slighty behind. This delta is nowhere else observable, but in 7158fbbc4b4SAlok Kataria * that case it results in a forward time jump in the range of hours 7168fbbc4b4SAlok Kataria * due to the unsigned delta calculation of the time keeping core 7178fbbc4b4SAlok Kataria * code, which is necessary to support wrapping clocksources like pm 7188fbbc4b4SAlok Kataria * timer. 7198fbbc4b4SAlok Kataria */ 7208e19608eSMagnus Damm static cycle_t read_tsc(struct clocksource *cs) 7218fbbc4b4SAlok Kataria { 7228fbbc4b4SAlok Kataria cycle_t ret = (cycle_t)get_cycles(); 7238fbbc4b4SAlok Kataria 7248fbbc4b4SAlok Kataria return ret >= clocksource_tsc.cycle_last ? 7258fbbc4b4SAlok Kataria ret : clocksource_tsc.cycle_last; 7268fbbc4b4SAlok Kataria } 7278fbbc4b4SAlok Kataria 728431ceb83SThomas Gleixner #ifdef CONFIG_X86_64 7298fbbc4b4SAlok Kataria static cycle_t __vsyscall_fn vread_tsc(void) 7308fbbc4b4SAlok Kataria { 7317d96fd41SPetr Tesarik cycle_t ret; 7327d96fd41SPetr Tesarik 7337d96fd41SPetr Tesarik /* 7347d96fd41SPetr Tesarik * Surround the RDTSC by barriers, to make sure it's not 7357d96fd41SPetr Tesarik * speculated to outside the seqlock critical section and 7367d96fd41SPetr Tesarik * does not cause time warps: 7377d96fd41SPetr Tesarik */ 7387d96fd41SPetr Tesarik rdtsc_barrier(); 7397d96fd41SPetr Tesarik ret = (cycle_t)vget_cycles(); 7407d96fd41SPetr Tesarik rdtsc_barrier(); 7418fbbc4b4SAlok Kataria 7428fbbc4b4SAlok Kataria return ret >= __vsyscall_gtod_data.clock.cycle_last ? 7438fbbc4b4SAlok Kataria ret : __vsyscall_gtod_data.clock.cycle_last; 7448fbbc4b4SAlok Kataria } 745431ceb83SThomas Gleixner #endif 7468fbbc4b4SAlok Kataria 7478fbbc4b4SAlok Kataria static struct clocksource clocksource_tsc = { 7488fbbc4b4SAlok Kataria .name = "tsc", 7498fbbc4b4SAlok Kataria .rating = 300, 7508fbbc4b4SAlok Kataria .read = read_tsc, 7518fbbc4b4SAlok Kataria .mask = CLOCKSOURCE_MASK(64), 7528fbbc4b4SAlok Kataria .shift = 22, 7538fbbc4b4SAlok Kataria .flags = CLOCK_SOURCE_IS_CONTINUOUS | 7548fbbc4b4SAlok Kataria CLOCK_SOURCE_MUST_VERIFY, 7558fbbc4b4SAlok Kataria #ifdef CONFIG_X86_64 7568fbbc4b4SAlok Kataria .vread = vread_tsc, 7578fbbc4b4SAlok Kataria #endif 7588fbbc4b4SAlok Kataria }; 7598fbbc4b4SAlok Kataria 7608fbbc4b4SAlok Kataria void mark_tsc_unstable(char *reason) 7618fbbc4b4SAlok Kataria { 7628fbbc4b4SAlok Kataria if (!tsc_unstable) { 7638fbbc4b4SAlok Kataria tsc_unstable = 1; 7648fbbc4b4SAlok Kataria printk("Marking TSC unstable due to %s\n", reason); 7658fbbc4b4SAlok Kataria /* Change only the rating, when not registered */ 7668fbbc4b4SAlok Kataria if (clocksource_tsc.mult) 7678fbbc4b4SAlok Kataria clocksource_change_rating(&clocksource_tsc, 0); 7688fbbc4b4SAlok Kataria else 7698fbbc4b4SAlok Kataria clocksource_tsc.rating = 0; 7708fbbc4b4SAlok Kataria } 7718fbbc4b4SAlok Kataria } 7728fbbc4b4SAlok Kataria 7738fbbc4b4SAlok Kataria EXPORT_SYMBOL_GPL(mark_tsc_unstable); 7748fbbc4b4SAlok Kataria 7758fbbc4b4SAlok Kataria static int __init dmi_mark_tsc_unstable(const struct dmi_system_id *d) 7768fbbc4b4SAlok Kataria { 7778fbbc4b4SAlok Kataria printk(KERN_NOTICE "%s detected: marking TSC unstable.\n", 7788fbbc4b4SAlok Kataria d->ident); 7798fbbc4b4SAlok Kataria tsc_unstable = 1; 7808fbbc4b4SAlok Kataria return 0; 7818fbbc4b4SAlok Kataria } 7828fbbc4b4SAlok Kataria 7838fbbc4b4SAlok Kataria /* List of systems that have known TSC problems */ 7848fbbc4b4SAlok Kataria static struct dmi_system_id __initdata bad_tsc_dmi_table[] = { 7858fbbc4b4SAlok Kataria { 7868fbbc4b4SAlok Kataria .callback = dmi_mark_tsc_unstable, 7878fbbc4b4SAlok Kataria .ident = "IBM Thinkpad 380XD", 7888fbbc4b4SAlok Kataria .matches = { 7898fbbc4b4SAlok Kataria DMI_MATCH(DMI_BOARD_VENDOR, "IBM"), 7908fbbc4b4SAlok Kataria DMI_MATCH(DMI_BOARD_NAME, "2635FA0"), 7918fbbc4b4SAlok Kataria }, 7928fbbc4b4SAlok Kataria }, 7938fbbc4b4SAlok Kataria {} 7948fbbc4b4SAlok Kataria }; 7958fbbc4b4SAlok Kataria 796395628efSAlok Kataria static void __init check_system_tsc_reliable(void) 797395628efSAlok Kataria { 7988fbbc4b4SAlok Kataria #ifdef CONFIG_MGEODE_LX 7998fbbc4b4SAlok Kataria /* RTSC counts during suspend */ 8008fbbc4b4SAlok Kataria #define RTSC_SUSP 0x100 8018fbbc4b4SAlok Kataria unsigned long res_low, res_high; 8028fbbc4b4SAlok Kataria 8038fbbc4b4SAlok Kataria rdmsr_safe(MSR_GEODE_BUSCONT_CONF0, &res_low, &res_high); 804395628efSAlok Kataria /* Geode_LX - the OLPC CPU has a possibly a very reliable TSC */ 8058fbbc4b4SAlok Kataria if (res_low & RTSC_SUSP) 806395628efSAlok Kataria tsc_clocksource_reliable = 1; 8078fbbc4b4SAlok Kataria #endif 808395628efSAlok Kataria if (boot_cpu_has(X86_FEATURE_TSC_RELIABLE)) 809395628efSAlok Kataria tsc_clocksource_reliable = 1; 810395628efSAlok Kataria } 8118fbbc4b4SAlok Kataria 8128fbbc4b4SAlok Kataria /* 8138fbbc4b4SAlok Kataria * Make an educated guess if the TSC is trustworthy and synchronized 8148fbbc4b4SAlok Kataria * over all CPUs. 8158fbbc4b4SAlok Kataria */ 8168fbbc4b4SAlok Kataria __cpuinit int unsynchronized_tsc(void) 8178fbbc4b4SAlok Kataria { 8188fbbc4b4SAlok Kataria if (!cpu_has_tsc || tsc_unstable) 8198fbbc4b4SAlok Kataria return 1; 8208fbbc4b4SAlok Kataria 8213e5095d1SIngo Molnar #ifdef CONFIG_SMP 8228fbbc4b4SAlok Kataria if (apic_is_clustered_box()) 8238fbbc4b4SAlok Kataria return 1; 8248fbbc4b4SAlok Kataria #endif 8258fbbc4b4SAlok Kataria 8268fbbc4b4SAlok Kataria if (boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) 8278fbbc4b4SAlok Kataria return 0; 828d3b8f889Sjohn stultz 829d3b8f889Sjohn stultz if (tsc_clocksource_reliable) 830d3b8f889Sjohn stultz return 0; 8318fbbc4b4SAlok Kataria /* 8328fbbc4b4SAlok Kataria * Intel systems are normally all synchronized. 8338fbbc4b4SAlok Kataria * Exceptions must mark TSC as unstable: 8348fbbc4b4SAlok Kataria */ 8358fbbc4b4SAlok Kataria if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL) { 8368fbbc4b4SAlok Kataria /* assume multi socket systems are not synchronized: */ 8378fbbc4b4SAlok Kataria if (num_possible_cpus() > 1) 838d3b8f889Sjohn stultz return 1; 8398fbbc4b4SAlok Kataria } 8408fbbc4b4SAlok Kataria 841d3b8f889Sjohn stultz return 0; 8428fbbc4b4SAlok Kataria } 8438fbbc4b4SAlok Kataria 8448fbbc4b4SAlok Kataria static void __init init_tsc_clocksource(void) 8458fbbc4b4SAlok Kataria { 8468fbbc4b4SAlok Kataria clocksource_tsc.mult = clocksource_khz2mult(tsc_khz, 8478fbbc4b4SAlok Kataria clocksource_tsc.shift); 848395628efSAlok Kataria if (tsc_clocksource_reliable) 849395628efSAlok Kataria clocksource_tsc.flags &= ~CLOCK_SOURCE_MUST_VERIFY; 8508fbbc4b4SAlok Kataria /* lower the rating if we already know its unstable: */ 8518fbbc4b4SAlok Kataria if (check_tsc_unstable()) { 8528fbbc4b4SAlok Kataria clocksource_tsc.rating = 0; 8538fbbc4b4SAlok Kataria clocksource_tsc.flags &= ~CLOCK_SOURCE_IS_CONTINUOUS; 8548fbbc4b4SAlok Kataria } 8558fbbc4b4SAlok Kataria clocksource_register(&clocksource_tsc); 8568fbbc4b4SAlok Kataria } 8578fbbc4b4SAlok Kataria 8588fbbc4b4SAlok Kataria void __init tsc_init(void) 8598fbbc4b4SAlok Kataria { 8608fbbc4b4SAlok Kataria u64 lpj; 8618fbbc4b4SAlok Kataria int cpu; 8628fbbc4b4SAlok Kataria 8638fbbc4b4SAlok Kataria if (!cpu_has_tsc) 8648fbbc4b4SAlok Kataria return; 8658fbbc4b4SAlok Kataria 866e93ef949SAlok Kataria tsc_khz = calibrate_tsc(); 867e93ef949SAlok Kataria cpu_khz = tsc_khz; 8688fbbc4b4SAlok Kataria 869e93ef949SAlok Kataria if (!tsc_khz) { 8708fbbc4b4SAlok Kataria mark_tsc_unstable("could not calculate TSC khz"); 8718fbbc4b4SAlok Kataria return; 8728fbbc4b4SAlok Kataria } 8738fbbc4b4SAlok Kataria 8748fbbc4b4SAlok Kataria #ifdef CONFIG_X86_64 8758fbbc4b4SAlok Kataria if (cpu_has(&boot_cpu_data, X86_FEATURE_CONSTANT_TSC) && 8768fbbc4b4SAlok Kataria (boot_cpu_data.x86_vendor == X86_VENDOR_AMD)) 8778fbbc4b4SAlok Kataria cpu_khz = calibrate_cpu(); 8788fbbc4b4SAlok Kataria #endif 8798fbbc4b4SAlok Kataria 8808fbbc4b4SAlok Kataria printk("Detected %lu.%03lu MHz processor.\n", 8818fbbc4b4SAlok Kataria (unsigned long)cpu_khz / 1000, 8828fbbc4b4SAlok Kataria (unsigned long)cpu_khz % 1000); 8838fbbc4b4SAlok Kataria 8848fbbc4b4SAlok Kataria /* 8858fbbc4b4SAlok Kataria * Secondary CPUs do not run through tsc_init(), so set up 8868fbbc4b4SAlok Kataria * all the scale factors for all CPUs, assuming the same 8878fbbc4b4SAlok Kataria * speed as the bootup CPU. (cpufreq notifiers will fix this 8888fbbc4b4SAlok Kataria * up if their speed diverges) 8898fbbc4b4SAlok Kataria */ 8908fbbc4b4SAlok Kataria for_each_possible_cpu(cpu) 8918fbbc4b4SAlok Kataria set_cyc2ns_scale(cpu_khz, cpu); 8928fbbc4b4SAlok Kataria 8938fbbc4b4SAlok Kataria if (tsc_disabled > 0) 8948fbbc4b4SAlok Kataria return; 8958fbbc4b4SAlok Kataria 8968fbbc4b4SAlok Kataria /* now allow native_sched_clock() to use rdtsc */ 8978fbbc4b4SAlok Kataria tsc_disabled = 0; 8988fbbc4b4SAlok Kataria 89970de9a97SAlok Kataria lpj = ((u64)tsc_khz * 1000); 90070de9a97SAlok Kataria do_div(lpj, HZ); 90170de9a97SAlok Kataria lpj_fine = lpj; 90270de9a97SAlok Kataria 9038fbbc4b4SAlok Kataria use_tsc_delay(); 9048fbbc4b4SAlok Kataria /* Check and install the TSC clocksource */ 9058fbbc4b4SAlok Kataria dmi_check_system(bad_tsc_dmi_table); 9068fbbc4b4SAlok Kataria 9078fbbc4b4SAlok Kataria if (unsynchronized_tsc()) 9088fbbc4b4SAlok Kataria mark_tsc_unstable("TSCs unsynchronized"); 9098fbbc4b4SAlok Kataria 910395628efSAlok Kataria check_system_tsc_reliable(); 9118fbbc4b4SAlok Kataria init_tsc_clocksource(); 9128fbbc4b4SAlok Kataria } 9138fbbc4b4SAlok Kataria 914