1c767a54bSJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 2c767a54bSJoe Perches 3bfc0f594SAlok Kataria #include <linux/kernel.h> 40ef95533SAlok Kataria #include <linux/sched.h> 50ef95533SAlok Kataria #include <linux/init.h> 60ef95533SAlok Kataria #include <linux/module.h> 70ef95533SAlok Kataria #include <linux/timer.h> 8bfc0f594SAlok Kataria #include <linux/acpi_pmtmr.h> 92dbe06faSAlok Kataria #include <linux/cpufreq.h> 108fbbc4b4SAlok Kataria #include <linux/delay.h> 118fbbc4b4SAlok Kataria #include <linux/clocksource.h> 128fbbc4b4SAlok Kataria #include <linux/percpu.h> 1308604bd9SArnd Bergmann #include <linux/timex.h> 14bfc0f594SAlok Kataria 15bfc0f594SAlok Kataria #include <asm/hpet.h> 168fbbc4b4SAlok Kataria #include <asm/timer.h> 178fbbc4b4SAlok Kataria #include <asm/vgtod.h> 188fbbc4b4SAlok Kataria #include <asm/time.h> 198fbbc4b4SAlok Kataria #include <asm/delay.h> 2088b094fbSAlok Kataria #include <asm/hypervisor.h> 2108047c4fSThomas Gleixner #include <asm/nmi.h> 222d826404SThomas Gleixner #include <asm/x86_init.h> 230ef95533SAlok Kataria 24f24ade3aSIngo Molnar unsigned int __read_mostly cpu_khz; /* TSC clocks / usec, not used here */ 250ef95533SAlok Kataria EXPORT_SYMBOL(cpu_khz); 26f24ade3aSIngo Molnar 27f24ade3aSIngo Molnar unsigned int __read_mostly tsc_khz; 280ef95533SAlok Kataria EXPORT_SYMBOL(tsc_khz); 290ef95533SAlok Kataria 300ef95533SAlok Kataria /* 310ef95533SAlok Kataria * TSC can be unstable due to cpufreq or due to unsynced TSCs 320ef95533SAlok Kataria */ 33f24ade3aSIngo Molnar static int __read_mostly tsc_unstable; 340ef95533SAlok Kataria 350ef95533SAlok Kataria /* native_sched_clock() is called before tsc_init(), so 360ef95533SAlok Kataria we must start with the TSC soft disabled to prevent 370ef95533SAlok Kataria erroneous rdtsc usage on !cpu_has_tsc processors */ 38f24ade3aSIngo Molnar static int __read_mostly tsc_disabled = -1; 390ef95533SAlok Kataria 4028a00184SSuresh Siddha int tsc_clocksource_reliable; 410ef95533SAlok Kataria /* 420ef95533SAlok Kataria * Scheduler clock - returns current time in nanosec units. 430ef95533SAlok Kataria */ 440ef95533SAlok Kataria u64 native_sched_clock(void) 450ef95533SAlok Kataria { 460ef95533SAlok Kataria u64 this_offset; 470ef95533SAlok Kataria 480ef95533SAlok Kataria /* 490ef95533SAlok Kataria * Fall back to jiffies if there's no TSC available: 500ef95533SAlok Kataria * ( But note that we still use it if the TSC is marked 510ef95533SAlok Kataria * unstable. We do this because unlike Time Of Day, 520ef95533SAlok Kataria * the scheduler clock tolerates small errors and it's 530ef95533SAlok Kataria * very important for it to be as fast as the platform 543ad2f3fbSDaniel Mack * can achieve it. ) 550ef95533SAlok Kataria */ 560ef95533SAlok Kataria if (unlikely(tsc_disabled)) { 570ef95533SAlok Kataria /* No locking but a rare wrong value is not a big deal: */ 580ef95533SAlok Kataria return (jiffies_64 - INITIAL_JIFFIES) * (1000000000 / HZ); 590ef95533SAlok Kataria } 600ef95533SAlok Kataria 610ef95533SAlok Kataria /* read the Time Stamp Counter: */ 620ef95533SAlok Kataria rdtscll(this_offset); 630ef95533SAlok Kataria 640ef95533SAlok Kataria /* return the value in ns */ 657cbaef9cSIngo Molnar return __cycles_2_ns(this_offset); 660ef95533SAlok Kataria } 670ef95533SAlok Kataria 680ef95533SAlok Kataria /* We need to define a real function for sched_clock, to override the 690ef95533SAlok Kataria weak default version */ 700ef95533SAlok Kataria #ifdef CONFIG_PARAVIRT 710ef95533SAlok Kataria unsigned long long sched_clock(void) 720ef95533SAlok Kataria { 730ef95533SAlok Kataria return paravirt_sched_clock(); 740ef95533SAlok Kataria } 750ef95533SAlok Kataria #else 760ef95533SAlok Kataria unsigned long long 770ef95533SAlok Kataria sched_clock(void) __attribute__((alias("native_sched_clock"))); 780ef95533SAlok Kataria #endif 790ef95533SAlok Kataria 80ce37f400SDavid Vrabel unsigned long long native_read_tsc(void) 81ce37f400SDavid Vrabel { 82ce37f400SDavid Vrabel return __native_read_tsc(); 83ce37f400SDavid Vrabel } 84ce37f400SDavid Vrabel EXPORT_SYMBOL(native_read_tsc); 85ce37f400SDavid Vrabel 860ef95533SAlok Kataria int check_tsc_unstable(void) 870ef95533SAlok Kataria { 880ef95533SAlok Kataria return tsc_unstable; 890ef95533SAlok Kataria } 900ef95533SAlok Kataria EXPORT_SYMBOL_GPL(check_tsc_unstable); 910ef95533SAlok Kataria 920ef95533SAlok Kataria #ifdef CONFIG_X86_TSC 930ef95533SAlok Kataria int __init notsc_setup(char *str) 940ef95533SAlok Kataria { 95c767a54bSJoe Perches pr_warn("Kernel compiled with CONFIG_X86_TSC, cannot disable TSC completely\n"); 960ef95533SAlok Kataria tsc_disabled = 1; 970ef95533SAlok Kataria return 1; 980ef95533SAlok Kataria } 990ef95533SAlok Kataria #else 1000ef95533SAlok Kataria /* 1010ef95533SAlok Kataria * disable flag for tsc. Takes effect by clearing the TSC cpu flag 1020ef95533SAlok Kataria * in cpu/common.c 1030ef95533SAlok Kataria */ 1040ef95533SAlok Kataria int __init notsc_setup(char *str) 1050ef95533SAlok Kataria { 1060ef95533SAlok Kataria setup_clear_cpu_cap(X86_FEATURE_TSC); 1070ef95533SAlok Kataria return 1; 1080ef95533SAlok Kataria } 1090ef95533SAlok Kataria #endif 1100ef95533SAlok Kataria 1110ef95533SAlok Kataria __setup("notsc", notsc_setup); 112bfc0f594SAlok Kataria 113e82b8e4eSVenkatesh Pallipadi static int no_sched_irq_time; 114e82b8e4eSVenkatesh Pallipadi 115395628efSAlok Kataria static int __init tsc_setup(char *str) 116395628efSAlok Kataria { 117395628efSAlok Kataria if (!strcmp(str, "reliable")) 118395628efSAlok Kataria tsc_clocksource_reliable = 1; 119e82b8e4eSVenkatesh Pallipadi if (!strncmp(str, "noirqtime", 9)) 120e82b8e4eSVenkatesh Pallipadi no_sched_irq_time = 1; 121395628efSAlok Kataria return 1; 122395628efSAlok Kataria } 123395628efSAlok Kataria 124395628efSAlok Kataria __setup("tsc=", tsc_setup); 125395628efSAlok Kataria 126bfc0f594SAlok Kataria #define MAX_RETRIES 5 127bfc0f594SAlok Kataria #define SMI_TRESHOLD 50000 128bfc0f594SAlok Kataria 129bfc0f594SAlok Kataria /* 130bfc0f594SAlok Kataria * Read TSC and the reference counters. Take care of SMI disturbance 131bfc0f594SAlok Kataria */ 132827014beSThomas Gleixner static u64 tsc_read_refs(u64 *p, int hpet) 133bfc0f594SAlok Kataria { 134bfc0f594SAlok Kataria u64 t1, t2; 135bfc0f594SAlok Kataria int i; 136bfc0f594SAlok Kataria 137bfc0f594SAlok Kataria for (i = 0; i < MAX_RETRIES; i++) { 138bfc0f594SAlok Kataria t1 = get_cycles(); 139bfc0f594SAlok Kataria if (hpet) 140827014beSThomas Gleixner *p = hpet_readl(HPET_COUNTER) & 0xFFFFFFFF; 141bfc0f594SAlok Kataria else 142827014beSThomas Gleixner *p = acpi_pm_read_early(); 143bfc0f594SAlok Kataria t2 = get_cycles(); 144bfc0f594SAlok Kataria if ((t2 - t1) < SMI_TRESHOLD) 145bfc0f594SAlok Kataria return t2; 146bfc0f594SAlok Kataria } 147bfc0f594SAlok Kataria return ULLONG_MAX; 148bfc0f594SAlok Kataria } 149bfc0f594SAlok Kataria 150ec0c15afSLinus Torvalds /* 151d683ef7aSThomas Gleixner * Calculate the TSC frequency from HPET reference 152d683ef7aSThomas Gleixner */ 153d683ef7aSThomas Gleixner static unsigned long calc_hpet_ref(u64 deltatsc, u64 hpet1, u64 hpet2) 154d683ef7aSThomas Gleixner { 155d683ef7aSThomas Gleixner u64 tmp; 156d683ef7aSThomas Gleixner 157d683ef7aSThomas Gleixner if (hpet2 < hpet1) 158d683ef7aSThomas Gleixner hpet2 += 0x100000000ULL; 159d683ef7aSThomas Gleixner hpet2 -= hpet1; 160d683ef7aSThomas Gleixner tmp = ((u64)hpet2 * hpet_readl(HPET_PERIOD)); 161d683ef7aSThomas Gleixner do_div(tmp, 1000000); 162d683ef7aSThomas Gleixner do_div(deltatsc, tmp); 163d683ef7aSThomas Gleixner 164d683ef7aSThomas Gleixner return (unsigned long) deltatsc; 165d683ef7aSThomas Gleixner } 166d683ef7aSThomas Gleixner 167d683ef7aSThomas Gleixner /* 168d683ef7aSThomas Gleixner * Calculate the TSC frequency from PMTimer reference 169d683ef7aSThomas Gleixner */ 170d683ef7aSThomas Gleixner static unsigned long calc_pmtimer_ref(u64 deltatsc, u64 pm1, u64 pm2) 171d683ef7aSThomas Gleixner { 172d683ef7aSThomas Gleixner u64 tmp; 173d683ef7aSThomas Gleixner 174d683ef7aSThomas Gleixner if (!pm1 && !pm2) 175d683ef7aSThomas Gleixner return ULONG_MAX; 176d683ef7aSThomas Gleixner 177d683ef7aSThomas Gleixner if (pm2 < pm1) 178d683ef7aSThomas Gleixner pm2 += (u64)ACPI_PM_OVRRUN; 179d683ef7aSThomas Gleixner pm2 -= pm1; 180d683ef7aSThomas Gleixner tmp = pm2 * 1000000000LL; 181d683ef7aSThomas Gleixner do_div(tmp, PMTMR_TICKS_PER_SEC); 182d683ef7aSThomas Gleixner do_div(deltatsc, tmp); 183d683ef7aSThomas Gleixner 184d683ef7aSThomas Gleixner return (unsigned long) deltatsc; 185d683ef7aSThomas Gleixner } 186d683ef7aSThomas Gleixner 187a977c400SThomas Gleixner #define CAL_MS 10 188b7743970SDeepak Saxena #define CAL_LATCH (PIT_TICK_RATE / (1000 / CAL_MS)) 189a977c400SThomas Gleixner #define CAL_PIT_LOOPS 1000 190a977c400SThomas Gleixner 191a977c400SThomas Gleixner #define CAL2_MS 50 192b7743970SDeepak Saxena #define CAL2_LATCH (PIT_TICK_RATE / (1000 / CAL2_MS)) 193a977c400SThomas Gleixner #define CAL2_PIT_LOOPS 5000 194a977c400SThomas Gleixner 195cce3e057SThomas Gleixner 196ec0c15afSLinus Torvalds /* 197ec0c15afSLinus Torvalds * Try to calibrate the TSC against the Programmable 198ec0c15afSLinus Torvalds * Interrupt Timer and return the frequency of the TSC 199ec0c15afSLinus Torvalds * in kHz. 200ec0c15afSLinus Torvalds * 201ec0c15afSLinus Torvalds * Return ULONG_MAX on failure to calibrate. 202ec0c15afSLinus Torvalds */ 203a977c400SThomas Gleixner static unsigned long pit_calibrate_tsc(u32 latch, unsigned long ms, int loopmin) 204ec0c15afSLinus Torvalds { 205ec0c15afSLinus Torvalds u64 tsc, t1, t2, delta; 206ec0c15afSLinus Torvalds unsigned long tscmin, tscmax; 207ec0c15afSLinus Torvalds int pitcnt; 208ec0c15afSLinus Torvalds 209ec0c15afSLinus Torvalds /* Set the Gate high, disable speaker */ 210ec0c15afSLinus Torvalds outb((inb(0x61) & ~0x02) | 0x01, 0x61); 211ec0c15afSLinus Torvalds 212ec0c15afSLinus Torvalds /* 213ec0c15afSLinus Torvalds * Setup CTC channel 2* for mode 0, (interrupt on terminal 214ec0c15afSLinus Torvalds * count mode), binary count. Set the latch register to 50ms 215ec0c15afSLinus Torvalds * (LSB then MSB) to begin countdown. 216ec0c15afSLinus Torvalds */ 217ec0c15afSLinus Torvalds outb(0xb0, 0x43); 218a977c400SThomas Gleixner outb(latch & 0xff, 0x42); 219a977c400SThomas Gleixner outb(latch >> 8, 0x42); 220ec0c15afSLinus Torvalds 221ec0c15afSLinus Torvalds tsc = t1 = t2 = get_cycles(); 222ec0c15afSLinus Torvalds 223ec0c15afSLinus Torvalds pitcnt = 0; 224ec0c15afSLinus Torvalds tscmax = 0; 225ec0c15afSLinus Torvalds tscmin = ULONG_MAX; 226ec0c15afSLinus Torvalds while ((inb(0x61) & 0x20) == 0) { 227ec0c15afSLinus Torvalds t2 = get_cycles(); 228ec0c15afSLinus Torvalds delta = t2 - tsc; 229ec0c15afSLinus Torvalds tsc = t2; 230ec0c15afSLinus Torvalds if ((unsigned long) delta < tscmin) 231ec0c15afSLinus Torvalds tscmin = (unsigned int) delta; 232ec0c15afSLinus Torvalds if ((unsigned long) delta > tscmax) 233ec0c15afSLinus Torvalds tscmax = (unsigned int) delta; 234ec0c15afSLinus Torvalds pitcnt++; 235ec0c15afSLinus Torvalds } 236ec0c15afSLinus Torvalds 237ec0c15afSLinus Torvalds /* 238ec0c15afSLinus Torvalds * Sanity checks: 239ec0c15afSLinus Torvalds * 240a977c400SThomas Gleixner * If we were not able to read the PIT more than loopmin 241ec0c15afSLinus Torvalds * times, then we have been hit by a massive SMI 242ec0c15afSLinus Torvalds * 243ec0c15afSLinus Torvalds * If the maximum is 10 times larger than the minimum, 244ec0c15afSLinus Torvalds * then we got hit by an SMI as well. 245ec0c15afSLinus Torvalds */ 246a977c400SThomas Gleixner if (pitcnt < loopmin || tscmax > 10 * tscmin) 247ec0c15afSLinus Torvalds return ULONG_MAX; 248ec0c15afSLinus Torvalds 249ec0c15afSLinus Torvalds /* Calculate the PIT value */ 250ec0c15afSLinus Torvalds delta = t2 - t1; 251a977c400SThomas Gleixner do_div(delta, ms); 252ec0c15afSLinus Torvalds return delta; 253ec0c15afSLinus Torvalds } 254ec0c15afSLinus Torvalds 2556ac40ed0SLinus Torvalds /* 2566ac40ed0SLinus Torvalds * This reads the current MSB of the PIT counter, and 2576ac40ed0SLinus Torvalds * checks if we are running on sufficiently fast and 2586ac40ed0SLinus Torvalds * non-virtualized hardware. 2596ac40ed0SLinus Torvalds * 2606ac40ed0SLinus Torvalds * Our expectations are: 2616ac40ed0SLinus Torvalds * 2626ac40ed0SLinus Torvalds * - the PIT is running at roughly 1.19MHz 2636ac40ed0SLinus Torvalds * 2646ac40ed0SLinus Torvalds * - each IO is going to take about 1us on real hardware, 2656ac40ed0SLinus Torvalds * but we allow it to be much faster (by a factor of 10) or 2666ac40ed0SLinus Torvalds * _slightly_ slower (ie we allow up to a 2us read+counter 2676ac40ed0SLinus Torvalds * update - anything else implies a unacceptably slow CPU 2686ac40ed0SLinus Torvalds * or PIT for the fast calibration to work. 2696ac40ed0SLinus Torvalds * 2706ac40ed0SLinus Torvalds * - with 256 PIT ticks to read the value, we have 214us to 2716ac40ed0SLinus Torvalds * see the same MSB (and overhead like doing a single TSC 2726ac40ed0SLinus Torvalds * read per MSB value etc). 2736ac40ed0SLinus Torvalds * 2746ac40ed0SLinus Torvalds * - We're doing 2 reads per loop (LSB, MSB), and we expect 2756ac40ed0SLinus Torvalds * them each to take about a microsecond on real hardware. 2766ac40ed0SLinus Torvalds * So we expect a count value of around 100. But we'll be 2776ac40ed0SLinus Torvalds * generous, and accept anything over 50. 2786ac40ed0SLinus Torvalds * 2796ac40ed0SLinus Torvalds * - if the PIT is stuck, and we see *many* more reads, we 2806ac40ed0SLinus Torvalds * return early (and the next caller of pit_expect_msb() 2816ac40ed0SLinus Torvalds * then consider it a failure when they don't see the 2826ac40ed0SLinus Torvalds * next expected value). 2836ac40ed0SLinus Torvalds * 2846ac40ed0SLinus Torvalds * These expectations mean that we know that we have seen the 2856ac40ed0SLinus Torvalds * transition from one expected value to another with a fairly 2866ac40ed0SLinus Torvalds * high accuracy, and we didn't miss any events. We can thus 2876ac40ed0SLinus Torvalds * use the TSC value at the transitions to calculate a pretty 2886ac40ed0SLinus Torvalds * good value for the TSC frequencty. 2896ac40ed0SLinus Torvalds */ 290b6e61eefSLinus Torvalds static inline int pit_verify_msb(unsigned char val) 291b6e61eefSLinus Torvalds { 292b6e61eefSLinus Torvalds /* Ignore LSB */ 293b6e61eefSLinus Torvalds inb(0x42); 294b6e61eefSLinus Torvalds return inb(0x42) == val; 295b6e61eefSLinus Torvalds } 296b6e61eefSLinus Torvalds 2979e8912e0SLinus Torvalds static inline int pit_expect_msb(unsigned char val, u64 *tscp, unsigned long *deltap) 2986ac40ed0SLinus Torvalds { 2999e8912e0SLinus Torvalds int count; 30068f30fbeSLinus Torvalds u64 tsc = 0, prev_tsc = 0; 3016ac40ed0SLinus Torvalds 3026ac40ed0SLinus Torvalds for (count = 0; count < 50000; count++) { 303b6e61eefSLinus Torvalds if (!pit_verify_msb(val)) 3046ac40ed0SLinus Torvalds break; 30568f30fbeSLinus Torvalds prev_tsc = tsc; 3069e8912e0SLinus Torvalds tsc = get_cycles(); 3076ac40ed0SLinus Torvalds } 30868f30fbeSLinus Torvalds *deltap = get_cycles() - prev_tsc; 3099e8912e0SLinus Torvalds *tscp = tsc; 3109e8912e0SLinus Torvalds 3119e8912e0SLinus Torvalds /* 3129e8912e0SLinus Torvalds * We require _some_ success, but the quality control 3139e8912e0SLinus Torvalds * will be based on the error terms on the TSC values. 3149e8912e0SLinus Torvalds */ 3159e8912e0SLinus Torvalds return count > 5; 3166ac40ed0SLinus Torvalds } 3176ac40ed0SLinus Torvalds 3186ac40ed0SLinus Torvalds /* 3199e8912e0SLinus Torvalds * How many MSB values do we want to see? We aim for 3209e8912e0SLinus Torvalds * a maximum error rate of 500ppm (in practice the 3219e8912e0SLinus Torvalds * real error is much smaller), but refuse to spend 32268f30fbeSLinus Torvalds * more than 50ms on it. 3236ac40ed0SLinus Torvalds */ 32468f30fbeSLinus Torvalds #define MAX_QUICK_PIT_MS 50 3259e8912e0SLinus Torvalds #define MAX_QUICK_PIT_ITERATIONS (MAX_QUICK_PIT_MS * PIT_TICK_RATE / 1000 / 256) 3266ac40ed0SLinus Torvalds 3276ac40ed0SLinus Torvalds static unsigned long quick_pit_calibrate(void) 3286ac40ed0SLinus Torvalds { 3299e8912e0SLinus Torvalds int i; 3309e8912e0SLinus Torvalds u64 tsc, delta; 3319e8912e0SLinus Torvalds unsigned long d1, d2; 3329e8912e0SLinus Torvalds 3336ac40ed0SLinus Torvalds /* Set the Gate high, disable speaker */ 3346ac40ed0SLinus Torvalds outb((inb(0x61) & ~0x02) | 0x01, 0x61); 3356ac40ed0SLinus Torvalds 3366ac40ed0SLinus Torvalds /* 3376ac40ed0SLinus Torvalds * Counter 2, mode 0 (one-shot), binary count 3386ac40ed0SLinus Torvalds * 3396ac40ed0SLinus Torvalds * NOTE! Mode 2 decrements by two (and then the 3406ac40ed0SLinus Torvalds * output is flipped each time, giving the same 3416ac40ed0SLinus Torvalds * final output frequency as a decrement-by-one), 3426ac40ed0SLinus Torvalds * so mode 0 is much better when looking at the 3436ac40ed0SLinus Torvalds * individual counts. 3446ac40ed0SLinus Torvalds */ 3456ac40ed0SLinus Torvalds outb(0xb0, 0x43); 3466ac40ed0SLinus Torvalds 3476ac40ed0SLinus Torvalds /* Start at 0xffff */ 3486ac40ed0SLinus Torvalds outb(0xff, 0x42); 3496ac40ed0SLinus Torvalds outb(0xff, 0x42); 3506ac40ed0SLinus Torvalds 351a6a80e1dSLinus Torvalds /* 352a6a80e1dSLinus Torvalds * The PIT starts counting at the next edge, so we 353a6a80e1dSLinus Torvalds * need to delay for a microsecond. The easiest way 354a6a80e1dSLinus Torvalds * to do that is to just read back the 16-bit counter 355a6a80e1dSLinus Torvalds * once from the PIT. 356a6a80e1dSLinus Torvalds */ 357b6e61eefSLinus Torvalds pit_verify_msb(0); 358a6a80e1dSLinus Torvalds 3599e8912e0SLinus Torvalds if (pit_expect_msb(0xff, &tsc, &d1)) { 3609e8912e0SLinus Torvalds for (i = 1; i <= MAX_QUICK_PIT_ITERATIONS; i++) { 3619e8912e0SLinus Torvalds if (!pit_expect_msb(0xff-i, &delta, &d2)) 3629e8912e0SLinus Torvalds break; 3636ac40ed0SLinus Torvalds 3646ac40ed0SLinus Torvalds /* 3659e8912e0SLinus Torvalds * Iterate until the error is less than 500 ppm 3664156e9a8SIngo Molnar */ 3679e8912e0SLinus Torvalds delta -= tsc; 368b6e61eefSLinus Torvalds if (d1+d2 >= delta >> 11) 369b6e61eefSLinus Torvalds continue; 370b6e61eefSLinus Torvalds 371b6e61eefSLinus Torvalds /* 372b6e61eefSLinus Torvalds * Check the PIT one more time to verify that 373b6e61eefSLinus Torvalds * all TSC reads were stable wrt the PIT. 374b6e61eefSLinus Torvalds * 375b6e61eefSLinus Torvalds * This also guarantees serialization of the 376b6e61eefSLinus Torvalds * last cycle read ('d2') in pit_expect_msb. 377b6e61eefSLinus Torvalds */ 378b6e61eefSLinus Torvalds if (!pit_verify_msb(0xfe - i)) 379b6e61eefSLinus Torvalds break; 3809e8912e0SLinus Torvalds goto success; 3819e8912e0SLinus Torvalds } 3829e8912e0SLinus Torvalds } 383c767a54bSJoe Perches pr_err("Fast TSC calibration failed\n"); 3849e8912e0SLinus Torvalds return 0; 3854156e9a8SIngo Molnar 3869e8912e0SLinus Torvalds success: 3874156e9a8SIngo Molnar /* 3886ac40ed0SLinus Torvalds * Ok, if we get here, then we've seen the 3899e8912e0SLinus Torvalds * MSB of the PIT decrement 'i' times, and the 3909e8912e0SLinus Torvalds * error has shrunk to less than 500 ppm. 3916ac40ed0SLinus Torvalds * 3926ac40ed0SLinus Torvalds * As a result, we can depend on there not being 3936ac40ed0SLinus Torvalds * any odd delays anywhere, and the TSC reads are 39468f30fbeSLinus Torvalds * reliable (within the error). 3956ac40ed0SLinus Torvalds * 3966ac40ed0SLinus Torvalds * kHz = ticks / time-in-seconds / 1000; 3979e8912e0SLinus Torvalds * kHz = (t2 - t1) / (I * 256 / PIT_TICK_RATE) / 1000 3989e8912e0SLinus Torvalds * kHz = ((t2 - t1) * PIT_TICK_RATE) / (I * 256 * 1000) 3996ac40ed0SLinus Torvalds */ 4009e8912e0SLinus Torvalds delta *= PIT_TICK_RATE; 4019e8912e0SLinus Torvalds do_div(delta, i*256*1000); 402c767a54bSJoe Perches pr_info("Fast TSC calibration using PIT\n"); 4036ac40ed0SLinus Torvalds return delta; 4046ac40ed0SLinus Torvalds } 405ec0c15afSLinus Torvalds 406bfc0f594SAlok Kataria /** 407e93ef949SAlok Kataria * native_calibrate_tsc - calibrate the tsc on boot 408bfc0f594SAlok Kataria */ 409e93ef949SAlok Kataria unsigned long native_calibrate_tsc(void) 410bfc0f594SAlok Kataria { 411827014beSThomas Gleixner u64 tsc1, tsc2, delta, ref1, ref2; 412fbb16e24SThomas Gleixner unsigned long tsc_pit_min = ULONG_MAX, tsc_ref_min = ULONG_MAX; 4132d826404SThomas Gleixner unsigned long flags, latch, ms, fast_calibrate; 414a977c400SThomas Gleixner int hpet = is_hpet_enabled(), i, loopmin; 415bfc0f594SAlok Kataria 416bfc0f594SAlok Kataria local_irq_save(flags); 4176ac40ed0SLinus Torvalds fast_calibrate = quick_pit_calibrate(); 418bfc0f594SAlok Kataria local_irq_restore(flags); 4196ac40ed0SLinus Torvalds if (fast_calibrate) 4206ac40ed0SLinus Torvalds return fast_calibrate; 421fbb16e24SThomas Gleixner 422fbb16e24SThomas Gleixner /* 423fbb16e24SThomas Gleixner * Run 5 calibration loops to get the lowest frequency value 424fbb16e24SThomas Gleixner * (the best estimate). We use two different calibration modes 425fbb16e24SThomas Gleixner * here: 426fbb16e24SThomas Gleixner * 427fbb16e24SThomas Gleixner * 1) PIT loop. We set the PIT Channel 2 to oneshot mode and 428fbb16e24SThomas Gleixner * load a timeout of 50ms. We read the time right after we 429fbb16e24SThomas Gleixner * started the timer and wait until the PIT count down reaches 430fbb16e24SThomas Gleixner * zero. In each wait loop iteration we read the TSC and check 431fbb16e24SThomas Gleixner * the delta to the previous read. We keep track of the min 432fbb16e24SThomas Gleixner * and max values of that delta. The delta is mostly defined 433fbb16e24SThomas Gleixner * by the IO time of the PIT access, so we can detect when a 4340d2eb44fSLucas De Marchi * SMI/SMM disturbance happened between the two reads. If the 435fbb16e24SThomas Gleixner * maximum time is significantly larger than the minimum time, 436fbb16e24SThomas Gleixner * then we discard the result and have another try. 437fbb16e24SThomas Gleixner * 438fbb16e24SThomas Gleixner * 2) Reference counter. If available we use the HPET or the 439fbb16e24SThomas Gleixner * PMTIMER as a reference to check the sanity of that value. 440fbb16e24SThomas Gleixner * We use separate TSC readouts and check inside of the 441fbb16e24SThomas Gleixner * reference read for a SMI/SMM disturbance. We dicard 442fbb16e24SThomas Gleixner * disturbed values here as well. We do that around the PIT 443fbb16e24SThomas Gleixner * calibration delay loop as we have to wait for a certain 444fbb16e24SThomas Gleixner * amount of time anyway. 445fbb16e24SThomas Gleixner */ 446a977c400SThomas Gleixner 447a977c400SThomas Gleixner /* Preset PIT loop values */ 448a977c400SThomas Gleixner latch = CAL_LATCH; 449a977c400SThomas Gleixner ms = CAL_MS; 450a977c400SThomas Gleixner loopmin = CAL_PIT_LOOPS; 451a977c400SThomas Gleixner 452a977c400SThomas Gleixner for (i = 0; i < 3; i++) { 453ec0c15afSLinus Torvalds unsigned long tsc_pit_khz; 454bfc0f594SAlok Kataria 455fbb16e24SThomas Gleixner /* 456fbb16e24SThomas Gleixner * Read the start value and the reference count of 457ec0c15afSLinus Torvalds * hpet/pmtimer when available. Then do the PIT 458ec0c15afSLinus Torvalds * calibration, which will take at least 50ms, and 459ec0c15afSLinus Torvalds * read the end value. 460fbb16e24SThomas Gleixner */ 461ec0c15afSLinus Torvalds local_irq_save(flags); 462827014beSThomas Gleixner tsc1 = tsc_read_refs(&ref1, hpet); 463a977c400SThomas Gleixner tsc_pit_khz = pit_calibrate_tsc(latch, ms, loopmin); 464827014beSThomas Gleixner tsc2 = tsc_read_refs(&ref2, hpet); 465bfc0f594SAlok Kataria local_irq_restore(flags); 466bfc0f594SAlok Kataria 467ec0c15afSLinus Torvalds /* Pick the lowest PIT TSC calibration so far */ 468ec0c15afSLinus Torvalds tsc_pit_min = min(tsc_pit_min, tsc_pit_khz); 469bfc0f594SAlok Kataria 470bfc0f594SAlok Kataria /* hpet or pmtimer available ? */ 47162627becSJohn Stultz if (ref1 == ref2) 472fbb16e24SThomas Gleixner continue; 473bfc0f594SAlok Kataria 474bfc0f594SAlok Kataria /* Check, whether the sampling was disturbed by an SMI */ 475fbb16e24SThomas Gleixner if (tsc1 == ULLONG_MAX || tsc2 == ULLONG_MAX) 476fbb16e24SThomas Gleixner continue; 477bfc0f594SAlok Kataria 478bfc0f594SAlok Kataria tsc2 = (tsc2 - tsc1) * 1000000LL; 479d683ef7aSThomas Gleixner if (hpet) 480827014beSThomas Gleixner tsc2 = calc_hpet_ref(tsc2, ref1, ref2); 481d683ef7aSThomas Gleixner else 482827014beSThomas Gleixner tsc2 = calc_pmtimer_ref(tsc2, ref1, ref2); 483bfc0f594SAlok Kataria 484fbb16e24SThomas Gleixner tsc_ref_min = min(tsc_ref_min, (unsigned long) tsc2); 485a977c400SThomas Gleixner 486a977c400SThomas Gleixner /* Check the reference deviation */ 487a977c400SThomas Gleixner delta = ((u64) tsc_pit_min) * 100; 488a977c400SThomas Gleixner do_div(delta, tsc_ref_min); 489a977c400SThomas Gleixner 490a977c400SThomas Gleixner /* 491a977c400SThomas Gleixner * If both calibration results are inside a 10% window 492a977c400SThomas Gleixner * then we can be sure, that the calibration 493a977c400SThomas Gleixner * succeeded. We break out of the loop right away. We 494a977c400SThomas Gleixner * use the reference value, as it is more precise. 495a977c400SThomas Gleixner */ 496a977c400SThomas Gleixner if (delta >= 90 && delta <= 110) { 497c767a54bSJoe Perches pr_info("PIT calibration matches %s. %d loops\n", 498a977c400SThomas Gleixner hpet ? "HPET" : "PMTIMER", i + 1); 499a977c400SThomas Gleixner return tsc_ref_min; 500bfc0f594SAlok Kataria } 501bfc0f594SAlok Kataria 502a977c400SThomas Gleixner /* 503a977c400SThomas Gleixner * Check whether PIT failed more than once. This 504a977c400SThomas Gleixner * happens in virtualized environments. We need to 505a977c400SThomas Gleixner * give the virtual PC a slightly longer timeframe for 506a977c400SThomas Gleixner * the HPET/PMTIMER to make the result precise. 507a977c400SThomas Gleixner */ 508a977c400SThomas Gleixner if (i == 1 && tsc_pit_min == ULONG_MAX) { 509a977c400SThomas Gleixner latch = CAL2_LATCH; 510a977c400SThomas Gleixner ms = CAL2_MS; 511a977c400SThomas Gleixner loopmin = CAL2_PIT_LOOPS; 512a977c400SThomas Gleixner } 513bfc0f594SAlok Kataria } 514bfc0f594SAlok Kataria 515fbb16e24SThomas Gleixner /* 516fbb16e24SThomas Gleixner * Now check the results. 517fbb16e24SThomas Gleixner */ 518fbb16e24SThomas Gleixner if (tsc_pit_min == ULONG_MAX) { 519fbb16e24SThomas Gleixner /* PIT gave no useful value */ 520c767a54bSJoe Perches pr_warn("Unable to calibrate against PIT\n"); 521fbb16e24SThomas Gleixner 522fbb16e24SThomas Gleixner /* We don't have an alternative source, disable TSC */ 523827014beSThomas Gleixner if (!hpet && !ref1 && !ref2) { 524c767a54bSJoe Perches pr_notice("No reference (HPET/PMTIMER) available\n"); 525fbb16e24SThomas Gleixner return 0; 526fbb16e24SThomas Gleixner } 527fbb16e24SThomas Gleixner 528fbb16e24SThomas Gleixner /* The alternative source failed as well, disable TSC */ 529fbb16e24SThomas Gleixner if (tsc_ref_min == ULONG_MAX) { 530c767a54bSJoe Perches pr_warn("HPET/PMTIMER calibration failed\n"); 531fbb16e24SThomas Gleixner return 0; 532fbb16e24SThomas Gleixner } 533fbb16e24SThomas Gleixner 534fbb16e24SThomas Gleixner /* Use the alternative source */ 535c767a54bSJoe Perches pr_info("using %s reference calibration\n", 536fbb16e24SThomas Gleixner hpet ? "HPET" : "PMTIMER"); 537fbb16e24SThomas Gleixner 538fbb16e24SThomas Gleixner return tsc_ref_min; 539fbb16e24SThomas Gleixner } 540fbb16e24SThomas Gleixner 541fbb16e24SThomas Gleixner /* We don't have an alternative source, use the PIT calibration value */ 542827014beSThomas Gleixner if (!hpet && !ref1 && !ref2) { 543c767a54bSJoe Perches pr_info("Using PIT calibration value\n"); 544fbb16e24SThomas Gleixner return tsc_pit_min; 545fbb16e24SThomas Gleixner } 546fbb16e24SThomas Gleixner 547fbb16e24SThomas Gleixner /* The alternative source failed, use the PIT calibration value */ 548fbb16e24SThomas Gleixner if (tsc_ref_min == ULONG_MAX) { 549c767a54bSJoe Perches pr_warn("HPET/PMTIMER calibration failed. Using PIT calibration.\n"); 550fbb16e24SThomas Gleixner return tsc_pit_min; 551fbb16e24SThomas Gleixner } 552fbb16e24SThomas Gleixner 553fbb16e24SThomas Gleixner /* 554fbb16e24SThomas Gleixner * The calibration values differ too much. In doubt, we use 555fbb16e24SThomas Gleixner * the PIT value as we know that there are PMTIMERs around 556a977c400SThomas Gleixner * running at double speed. At least we let the user know: 557fbb16e24SThomas Gleixner */ 558c767a54bSJoe Perches pr_warn("PIT calibration deviates from %s: %lu %lu\n", 559a977c400SThomas Gleixner hpet ? "HPET" : "PMTIMER", tsc_pit_min, tsc_ref_min); 560c767a54bSJoe Perches pr_info("Using PIT calibration value\n"); 561fbb16e24SThomas Gleixner return tsc_pit_min; 562fbb16e24SThomas Gleixner } 563bfc0f594SAlok Kataria 564bfc0f594SAlok Kataria int recalibrate_cpu_khz(void) 565bfc0f594SAlok Kataria { 566bfc0f594SAlok Kataria #ifndef CONFIG_SMP 567bfc0f594SAlok Kataria unsigned long cpu_khz_old = cpu_khz; 568bfc0f594SAlok Kataria 569bfc0f594SAlok Kataria if (cpu_has_tsc) { 5702d826404SThomas Gleixner tsc_khz = x86_platform.calibrate_tsc(); 571e93ef949SAlok Kataria cpu_khz = tsc_khz; 572bfc0f594SAlok Kataria cpu_data(0).loops_per_jiffy = 573bfc0f594SAlok Kataria cpufreq_scale(cpu_data(0).loops_per_jiffy, 574bfc0f594SAlok Kataria cpu_khz_old, cpu_khz); 575bfc0f594SAlok Kataria return 0; 576bfc0f594SAlok Kataria } else 577bfc0f594SAlok Kataria return -ENODEV; 578bfc0f594SAlok Kataria #else 579bfc0f594SAlok Kataria return -ENODEV; 580bfc0f594SAlok Kataria #endif 581bfc0f594SAlok Kataria } 582bfc0f594SAlok Kataria 583bfc0f594SAlok Kataria EXPORT_SYMBOL(recalibrate_cpu_khz); 584bfc0f594SAlok Kataria 5852dbe06faSAlok Kataria 5862dbe06faSAlok Kataria /* Accelerators for sched_clock() 5872dbe06faSAlok Kataria * convert from cycles(64bits) => nanoseconds (64bits) 5882dbe06faSAlok Kataria * basic equation: 5892dbe06faSAlok Kataria * ns = cycles / (freq / ns_per_sec) 5902dbe06faSAlok Kataria * ns = cycles * (ns_per_sec / freq) 5912dbe06faSAlok Kataria * ns = cycles * (10^9 / (cpu_khz * 10^3)) 5922dbe06faSAlok Kataria * ns = cycles * (10^6 / cpu_khz) 5932dbe06faSAlok Kataria * 5942dbe06faSAlok Kataria * Then we use scaling math (suggested by george@mvista.com) to get: 5952dbe06faSAlok Kataria * ns = cycles * (10^6 * SC / cpu_khz) / SC 5962dbe06faSAlok Kataria * ns = cycles * cyc2ns_scale / SC 5972dbe06faSAlok Kataria * 5982dbe06faSAlok Kataria * And since SC is a constant power of two, we can convert the div 5992dbe06faSAlok Kataria * into a shift. 6002dbe06faSAlok Kataria * 6012dbe06faSAlok Kataria * We can use khz divisor instead of mhz to keep a better precision, since 6022dbe06faSAlok Kataria * cyc2ns_scale is limited to 10^6 * 2^10, which fits in 32 bits. 6032dbe06faSAlok Kataria * (mathieu.desnoyers@polymtl.ca) 6042dbe06faSAlok Kataria * 6052dbe06faSAlok Kataria * -johnstul@us.ibm.com "math is hard, lets go shopping!" 6062dbe06faSAlok Kataria */ 6072dbe06faSAlok Kataria 6082dbe06faSAlok Kataria DEFINE_PER_CPU(unsigned long, cyc2ns); 60984599f8aSPeter Zijlstra DEFINE_PER_CPU(unsigned long long, cyc2ns_offset); 6102dbe06faSAlok Kataria 6118fbbc4b4SAlok Kataria static void set_cyc2ns_scale(unsigned long cpu_khz, int cpu) 6122dbe06faSAlok Kataria { 61384599f8aSPeter Zijlstra unsigned long long tsc_now, ns_now, *offset; 6142dbe06faSAlok Kataria unsigned long flags, *scale; 6152dbe06faSAlok Kataria 6162dbe06faSAlok Kataria local_irq_save(flags); 6172dbe06faSAlok Kataria sched_clock_idle_sleep_event(); 6182dbe06faSAlok Kataria 6192dbe06faSAlok Kataria scale = &per_cpu(cyc2ns, cpu); 62084599f8aSPeter Zijlstra offset = &per_cpu(cyc2ns_offset, cpu); 6212dbe06faSAlok Kataria 6222dbe06faSAlok Kataria rdtscll(tsc_now); 6232dbe06faSAlok Kataria ns_now = __cycles_2_ns(tsc_now); 6242dbe06faSAlok Kataria 62584599f8aSPeter Zijlstra if (cpu_khz) { 6262dbe06faSAlok Kataria *scale = (NSEC_PER_MSEC << CYC2NS_SCALE_FACTOR)/cpu_khz; 6279993bc63SSalman Qazi *offset = ns_now - mult_frac(tsc_now, *scale, 6289993bc63SSalman Qazi (1UL << CYC2NS_SCALE_FACTOR)); 62984599f8aSPeter Zijlstra } 6302dbe06faSAlok Kataria 6312dbe06faSAlok Kataria sched_clock_idle_wakeup_event(0); 6322dbe06faSAlok Kataria local_irq_restore(flags); 6332dbe06faSAlok Kataria } 6342dbe06faSAlok Kataria 635cd7240c0SSuresh Siddha static unsigned long long cyc2ns_suspend; 636cd7240c0SSuresh Siddha 637b74f05d6SMarcelo Tosatti void tsc_save_sched_clock_state(void) 638cd7240c0SSuresh Siddha { 639cd7240c0SSuresh Siddha if (!sched_clock_stable) 640cd7240c0SSuresh Siddha return; 641cd7240c0SSuresh Siddha 642cd7240c0SSuresh Siddha cyc2ns_suspend = sched_clock(); 643cd7240c0SSuresh Siddha } 644cd7240c0SSuresh Siddha 645cd7240c0SSuresh Siddha /* 646cd7240c0SSuresh Siddha * Even on processors with invariant TSC, TSC gets reset in some the 647cd7240c0SSuresh Siddha * ACPI system sleep states. And in some systems BIOS seem to reinit TSC to 648cd7240c0SSuresh Siddha * arbitrary value (still sync'd across cpu's) during resume from such sleep 649cd7240c0SSuresh Siddha * states. To cope up with this, recompute the cyc2ns_offset for each cpu so 650cd7240c0SSuresh Siddha * that sched_clock() continues from the point where it was left off during 651cd7240c0SSuresh Siddha * suspend. 652cd7240c0SSuresh Siddha */ 653b74f05d6SMarcelo Tosatti void tsc_restore_sched_clock_state(void) 654cd7240c0SSuresh Siddha { 655cd7240c0SSuresh Siddha unsigned long long offset; 656cd7240c0SSuresh Siddha unsigned long flags; 657cd7240c0SSuresh Siddha int cpu; 658cd7240c0SSuresh Siddha 659cd7240c0SSuresh Siddha if (!sched_clock_stable) 660cd7240c0SSuresh Siddha return; 661cd7240c0SSuresh Siddha 662cd7240c0SSuresh Siddha local_irq_save(flags); 663cd7240c0SSuresh Siddha 6640a3aee0dSTejun Heo __this_cpu_write(cyc2ns_offset, 0); 665cd7240c0SSuresh Siddha offset = cyc2ns_suspend - sched_clock(); 666cd7240c0SSuresh Siddha 667cd7240c0SSuresh Siddha for_each_possible_cpu(cpu) 668cd7240c0SSuresh Siddha per_cpu(cyc2ns_offset, cpu) = offset; 669cd7240c0SSuresh Siddha 670cd7240c0SSuresh Siddha local_irq_restore(flags); 671cd7240c0SSuresh Siddha } 672cd7240c0SSuresh Siddha 6732dbe06faSAlok Kataria #ifdef CONFIG_CPU_FREQ 6742dbe06faSAlok Kataria 6752dbe06faSAlok Kataria /* Frequency scaling support. Adjust the TSC based timer when the cpu frequency 6762dbe06faSAlok Kataria * changes. 6772dbe06faSAlok Kataria * 6782dbe06faSAlok Kataria * RED-PEN: On SMP we assume all CPUs run with the same frequency. It's 6792dbe06faSAlok Kataria * not that important because current Opteron setups do not support 6802dbe06faSAlok Kataria * scaling on SMP anyroads. 6812dbe06faSAlok Kataria * 6822dbe06faSAlok Kataria * Should fix up last_tsc too. Currently gettimeofday in the 6832dbe06faSAlok Kataria * first tick after the change will be slightly wrong. 6842dbe06faSAlok Kataria */ 6852dbe06faSAlok Kataria 6862dbe06faSAlok Kataria static unsigned int ref_freq; 6872dbe06faSAlok Kataria static unsigned long loops_per_jiffy_ref; 6882dbe06faSAlok Kataria static unsigned long tsc_khz_ref; 6892dbe06faSAlok Kataria 6902dbe06faSAlok Kataria static int time_cpufreq_notifier(struct notifier_block *nb, unsigned long val, 6912dbe06faSAlok Kataria void *data) 6922dbe06faSAlok Kataria { 6932dbe06faSAlok Kataria struct cpufreq_freqs *freq = data; 694931db6a3SDave Jones unsigned long *lpj; 6952dbe06faSAlok Kataria 6962dbe06faSAlok Kataria if (cpu_has(&cpu_data(freq->cpu), X86_FEATURE_CONSTANT_TSC)) 6972dbe06faSAlok Kataria return 0; 6982dbe06faSAlok Kataria 6992dbe06faSAlok Kataria lpj = &boot_cpu_data.loops_per_jiffy; 700931db6a3SDave Jones #ifdef CONFIG_SMP 701931db6a3SDave Jones if (!(freq->flags & CPUFREQ_CONST_LOOPS)) 702931db6a3SDave Jones lpj = &cpu_data(freq->cpu).loops_per_jiffy; 7032dbe06faSAlok Kataria #endif 7042dbe06faSAlok Kataria 7052dbe06faSAlok Kataria if (!ref_freq) { 7062dbe06faSAlok Kataria ref_freq = freq->old; 7072dbe06faSAlok Kataria loops_per_jiffy_ref = *lpj; 7082dbe06faSAlok Kataria tsc_khz_ref = tsc_khz; 7092dbe06faSAlok Kataria } 7102dbe06faSAlok Kataria if ((val == CPUFREQ_PRECHANGE && freq->old < freq->new) || 7112dbe06faSAlok Kataria (val == CPUFREQ_POSTCHANGE && freq->old > freq->new) || 7122dbe06faSAlok Kataria (val == CPUFREQ_RESUMECHANGE)) { 7132dbe06faSAlok Kataria *lpj = cpufreq_scale(loops_per_jiffy_ref, ref_freq, freq->new); 7142dbe06faSAlok Kataria 7152dbe06faSAlok Kataria tsc_khz = cpufreq_scale(tsc_khz_ref, ref_freq, freq->new); 7162dbe06faSAlok Kataria if (!(freq->flags & CPUFREQ_CONST_LOOPS)) 7172dbe06faSAlok Kataria mark_tsc_unstable("cpufreq changes"); 7182dbe06faSAlok Kataria } 7192dbe06faSAlok Kataria 72052a8968cSPeter Zijlstra set_cyc2ns_scale(tsc_khz, freq->cpu); 7212dbe06faSAlok Kataria 7222dbe06faSAlok Kataria return 0; 7232dbe06faSAlok Kataria } 7242dbe06faSAlok Kataria 7252dbe06faSAlok Kataria static struct notifier_block time_cpufreq_notifier_block = { 7262dbe06faSAlok Kataria .notifier_call = time_cpufreq_notifier 7272dbe06faSAlok Kataria }; 7282dbe06faSAlok Kataria 7292dbe06faSAlok Kataria static int __init cpufreq_tsc(void) 7302dbe06faSAlok Kataria { 731060700b5SLinus Torvalds if (!cpu_has_tsc) 732060700b5SLinus Torvalds return 0; 733060700b5SLinus Torvalds if (boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) 734060700b5SLinus Torvalds return 0; 7352dbe06faSAlok Kataria cpufreq_register_notifier(&time_cpufreq_notifier_block, 7362dbe06faSAlok Kataria CPUFREQ_TRANSITION_NOTIFIER); 7372dbe06faSAlok Kataria return 0; 7382dbe06faSAlok Kataria } 7392dbe06faSAlok Kataria 7402dbe06faSAlok Kataria core_initcall(cpufreq_tsc); 7412dbe06faSAlok Kataria 7422dbe06faSAlok Kataria #endif /* CONFIG_CPU_FREQ */ 7438fbbc4b4SAlok Kataria 7448fbbc4b4SAlok Kataria /* clocksource code */ 7458fbbc4b4SAlok Kataria 7468fbbc4b4SAlok Kataria static struct clocksource clocksource_tsc; 7478fbbc4b4SAlok Kataria 7488fbbc4b4SAlok Kataria /* 7498fbbc4b4SAlok Kataria * We compare the TSC to the cycle_last value in the clocksource 7508fbbc4b4SAlok Kataria * structure to avoid a nasty time-warp. This can be observed in a 7518fbbc4b4SAlok Kataria * very small window right after one CPU updated cycle_last under 7528fbbc4b4SAlok Kataria * xtime/vsyscall_gtod lock and the other CPU reads a TSC value which 7538fbbc4b4SAlok Kataria * is smaller than the cycle_last reference value due to a TSC which 7548fbbc4b4SAlok Kataria * is slighty behind. This delta is nowhere else observable, but in 7558fbbc4b4SAlok Kataria * that case it results in a forward time jump in the range of hours 7568fbbc4b4SAlok Kataria * due to the unsigned delta calculation of the time keeping core 7578fbbc4b4SAlok Kataria * code, which is necessary to support wrapping clocksources like pm 7588fbbc4b4SAlok Kataria * timer. 7598fbbc4b4SAlok Kataria */ 7608e19608eSMagnus Damm static cycle_t read_tsc(struct clocksource *cs) 7618fbbc4b4SAlok Kataria { 7628fbbc4b4SAlok Kataria cycle_t ret = (cycle_t)get_cycles(); 7638fbbc4b4SAlok Kataria 7648fbbc4b4SAlok Kataria return ret >= clocksource_tsc.cycle_last ? 7658fbbc4b4SAlok Kataria ret : clocksource_tsc.cycle_last; 7668fbbc4b4SAlok Kataria } 7678fbbc4b4SAlok Kataria 76817622339SMagnus Damm static void resume_tsc(struct clocksource *cs) 7691be39679SMartin Schwidefsky { 7701be39679SMartin Schwidefsky clocksource_tsc.cycle_last = 0; 7711be39679SMartin Schwidefsky } 7721be39679SMartin Schwidefsky 7738fbbc4b4SAlok Kataria static struct clocksource clocksource_tsc = { 7748fbbc4b4SAlok Kataria .name = "tsc", 7758fbbc4b4SAlok Kataria .rating = 300, 7768fbbc4b4SAlok Kataria .read = read_tsc, 7771be39679SMartin Schwidefsky .resume = resume_tsc, 7788fbbc4b4SAlok Kataria .mask = CLOCKSOURCE_MASK(64), 7798fbbc4b4SAlok Kataria .flags = CLOCK_SOURCE_IS_CONTINUOUS | 7808fbbc4b4SAlok Kataria CLOCK_SOURCE_MUST_VERIFY, 7818fbbc4b4SAlok Kataria #ifdef CONFIG_X86_64 78298d0ac38SAndy Lutomirski .archdata = { .vclock_mode = VCLOCK_TSC }, 7838fbbc4b4SAlok Kataria #endif 7848fbbc4b4SAlok Kataria }; 7858fbbc4b4SAlok Kataria 7868fbbc4b4SAlok Kataria void mark_tsc_unstable(char *reason) 7878fbbc4b4SAlok Kataria { 7888fbbc4b4SAlok Kataria if (!tsc_unstable) { 7898fbbc4b4SAlok Kataria tsc_unstable = 1; 7906c56ccecSPallipadi, Venkatesh sched_clock_stable = 0; 791e82b8e4eSVenkatesh Pallipadi disable_sched_clock_irqtime(); 792c767a54bSJoe Perches pr_info("Marking TSC unstable due to %s\n", reason); 7938fbbc4b4SAlok Kataria /* Change only the rating, when not registered */ 7948fbbc4b4SAlok Kataria if (clocksource_tsc.mult) 7957285dd7fSThomas Gleixner clocksource_mark_unstable(&clocksource_tsc); 7967285dd7fSThomas Gleixner else { 7977285dd7fSThomas Gleixner clocksource_tsc.flags |= CLOCK_SOURCE_UNSTABLE; 7988fbbc4b4SAlok Kataria clocksource_tsc.rating = 0; 7998fbbc4b4SAlok Kataria } 8008fbbc4b4SAlok Kataria } 8017285dd7fSThomas Gleixner } 8028fbbc4b4SAlok Kataria 8038fbbc4b4SAlok Kataria EXPORT_SYMBOL_GPL(mark_tsc_unstable); 8048fbbc4b4SAlok Kataria 805395628efSAlok Kataria static void __init check_system_tsc_reliable(void) 806395628efSAlok Kataria { 8078fbbc4b4SAlok Kataria #ifdef CONFIG_MGEODE_LX 8088fbbc4b4SAlok Kataria /* RTSC counts during suspend */ 8098fbbc4b4SAlok Kataria #define RTSC_SUSP 0x100 8108fbbc4b4SAlok Kataria unsigned long res_low, res_high; 8118fbbc4b4SAlok Kataria 8128fbbc4b4SAlok Kataria rdmsr_safe(MSR_GEODE_BUSCONT_CONF0, &res_low, &res_high); 81300097c4fSThadeu Lima de Souza Cascardo /* Geode_LX - the OLPC CPU has a very reliable TSC */ 8148fbbc4b4SAlok Kataria if (res_low & RTSC_SUSP) 815395628efSAlok Kataria tsc_clocksource_reliable = 1; 8168fbbc4b4SAlok Kataria #endif 817395628efSAlok Kataria if (boot_cpu_has(X86_FEATURE_TSC_RELIABLE)) 818395628efSAlok Kataria tsc_clocksource_reliable = 1; 819395628efSAlok Kataria } 8208fbbc4b4SAlok Kataria 8218fbbc4b4SAlok Kataria /* 8228fbbc4b4SAlok Kataria * Make an educated guess if the TSC is trustworthy and synchronized 8238fbbc4b4SAlok Kataria * over all CPUs. 8248fbbc4b4SAlok Kataria */ 8258fbbc4b4SAlok Kataria __cpuinit int unsynchronized_tsc(void) 8268fbbc4b4SAlok Kataria { 8278fbbc4b4SAlok Kataria if (!cpu_has_tsc || tsc_unstable) 8288fbbc4b4SAlok Kataria return 1; 8298fbbc4b4SAlok Kataria 8303e5095d1SIngo Molnar #ifdef CONFIG_SMP 8318fbbc4b4SAlok Kataria if (apic_is_clustered_box()) 8328fbbc4b4SAlok Kataria return 1; 8338fbbc4b4SAlok Kataria #endif 8348fbbc4b4SAlok Kataria 8358fbbc4b4SAlok Kataria if (boot_cpu_has(X86_FEATURE_CONSTANT_TSC)) 8368fbbc4b4SAlok Kataria return 0; 837d3b8f889Sjohn stultz 838d3b8f889Sjohn stultz if (tsc_clocksource_reliable) 839d3b8f889Sjohn stultz return 0; 8408fbbc4b4SAlok Kataria /* 8418fbbc4b4SAlok Kataria * Intel systems are normally all synchronized. 8428fbbc4b4SAlok Kataria * Exceptions must mark TSC as unstable: 8438fbbc4b4SAlok Kataria */ 8448fbbc4b4SAlok Kataria if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL) { 8458fbbc4b4SAlok Kataria /* assume multi socket systems are not synchronized: */ 8468fbbc4b4SAlok Kataria if (num_possible_cpus() > 1) 847d3b8f889Sjohn stultz return 1; 8488fbbc4b4SAlok Kataria } 8498fbbc4b4SAlok Kataria 850d3b8f889Sjohn stultz return 0; 8518fbbc4b4SAlok Kataria } 8528fbbc4b4SAlok Kataria 85308ec0c58SJohn Stultz 85408ec0c58SJohn Stultz static void tsc_refine_calibration_work(struct work_struct *work); 85508ec0c58SJohn Stultz static DECLARE_DELAYED_WORK(tsc_irqwork, tsc_refine_calibration_work); 85608ec0c58SJohn Stultz /** 85708ec0c58SJohn Stultz * tsc_refine_calibration_work - Further refine tsc freq calibration 85808ec0c58SJohn Stultz * @work - ignored. 85908ec0c58SJohn Stultz * 86008ec0c58SJohn Stultz * This functions uses delayed work over a period of a 86108ec0c58SJohn Stultz * second to further refine the TSC freq value. Since this is 86208ec0c58SJohn Stultz * timer based, instead of loop based, we don't block the boot 86308ec0c58SJohn Stultz * process while this longer calibration is done. 86408ec0c58SJohn Stultz * 8650d2eb44fSLucas De Marchi * If there are any calibration anomalies (too many SMIs, etc), 86608ec0c58SJohn Stultz * or the refined calibration is off by 1% of the fast early 86708ec0c58SJohn Stultz * calibration, we throw out the new calibration and use the 86808ec0c58SJohn Stultz * early calibration. 86908ec0c58SJohn Stultz */ 87008ec0c58SJohn Stultz static void tsc_refine_calibration_work(struct work_struct *work) 87108ec0c58SJohn Stultz { 87208ec0c58SJohn Stultz static u64 tsc_start = -1, ref_start; 87308ec0c58SJohn Stultz static int hpet; 87408ec0c58SJohn Stultz u64 tsc_stop, ref_stop, delta; 87508ec0c58SJohn Stultz unsigned long freq; 87608ec0c58SJohn Stultz 87708ec0c58SJohn Stultz /* Don't bother refining TSC on unstable systems */ 87808ec0c58SJohn Stultz if (check_tsc_unstable()) 87908ec0c58SJohn Stultz goto out; 88008ec0c58SJohn Stultz 88108ec0c58SJohn Stultz /* 88208ec0c58SJohn Stultz * Since the work is started early in boot, we may be 88308ec0c58SJohn Stultz * delayed the first time we expire. So set the workqueue 88408ec0c58SJohn Stultz * again once we know timers are working. 88508ec0c58SJohn Stultz */ 88608ec0c58SJohn Stultz if (tsc_start == -1) { 88708ec0c58SJohn Stultz /* 88808ec0c58SJohn Stultz * Only set hpet once, to avoid mixing hardware 88908ec0c58SJohn Stultz * if the hpet becomes enabled later. 89008ec0c58SJohn Stultz */ 89108ec0c58SJohn Stultz hpet = is_hpet_enabled(); 89208ec0c58SJohn Stultz schedule_delayed_work(&tsc_irqwork, HZ); 89308ec0c58SJohn Stultz tsc_start = tsc_read_refs(&ref_start, hpet); 89408ec0c58SJohn Stultz return; 89508ec0c58SJohn Stultz } 89608ec0c58SJohn Stultz 89708ec0c58SJohn Stultz tsc_stop = tsc_read_refs(&ref_stop, hpet); 89808ec0c58SJohn Stultz 89908ec0c58SJohn Stultz /* hpet or pmtimer available ? */ 90062627becSJohn Stultz if (ref_start == ref_stop) 90108ec0c58SJohn Stultz goto out; 90208ec0c58SJohn Stultz 90308ec0c58SJohn Stultz /* Check, whether the sampling was disturbed by an SMI */ 90408ec0c58SJohn Stultz if (tsc_start == ULLONG_MAX || tsc_stop == ULLONG_MAX) 90508ec0c58SJohn Stultz goto out; 90608ec0c58SJohn Stultz 90708ec0c58SJohn Stultz delta = tsc_stop - tsc_start; 90808ec0c58SJohn Stultz delta *= 1000000LL; 90908ec0c58SJohn Stultz if (hpet) 91008ec0c58SJohn Stultz freq = calc_hpet_ref(delta, ref_start, ref_stop); 91108ec0c58SJohn Stultz else 91208ec0c58SJohn Stultz freq = calc_pmtimer_ref(delta, ref_start, ref_stop); 91308ec0c58SJohn Stultz 91408ec0c58SJohn Stultz /* Make sure we're within 1% */ 91508ec0c58SJohn Stultz if (abs(tsc_khz - freq) > tsc_khz/100) 91608ec0c58SJohn Stultz goto out; 91708ec0c58SJohn Stultz 91808ec0c58SJohn Stultz tsc_khz = freq; 919c767a54bSJoe Perches pr_info("Refined TSC clocksource calibration: %lu.%03lu MHz\n", 920c767a54bSJoe Perches (unsigned long)tsc_khz / 1000, 92108ec0c58SJohn Stultz (unsigned long)tsc_khz % 1000); 92208ec0c58SJohn Stultz 92308ec0c58SJohn Stultz out: 92408ec0c58SJohn Stultz clocksource_register_khz(&clocksource_tsc, tsc_khz); 92508ec0c58SJohn Stultz } 92608ec0c58SJohn Stultz 92708ec0c58SJohn Stultz 92808ec0c58SJohn Stultz static int __init init_tsc_clocksource(void) 9298fbbc4b4SAlok Kataria { 93029fe359cSThomas Gleixner if (!cpu_has_tsc || tsc_disabled > 0 || !tsc_khz) 931a8760ecaSThomas Gleixner return 0; 932a8760ecaSThomas Gleixner 933395628efSAlok Kataria if (tsc_clocksource_reliable) 934395628efSAlok Kataria clocksource_tsc.flags &= ~CLOCK_SOURCE_MUST_VERIFY; 9358fbbc4b4SAlok Kataria /* lower the rating if we already know its unstable: */ 9368fbbc4b4SAlok Kataria if (check_tsc_unstable()) { 9378fbbc4b4SAlok Kataria clocksource_tsc.rating = 0; 9388fbbc4b4SAlok Kataria clocksource_tsc.flags &= ~CLOCK_SOURCE_IS_CONTINUOUS; 9398fbbc4b4SAlok Kataria } 94057779dc2SAlok Kataria 94157779dc2SAlok Kataria /* 94257779dc2SAlok Kataria * Trust the results of the earlier calibration on systems 94357779dc2SAlok Kataria * exporting a reliable TSC. 94457779dc2SAlok Kataria */ 94557779dc2SAlok Kataria if (boot_cpu_has(X86_FEATURE_TSC_RELIABLE)) { 94657779dc2SAlok Kataria clocksource_register_khz(&clocksource_tsc, tsc_khz); 94757779dc2SAlok Kataria return 0; 94857779dc2SAlok Kataria } 94957779dc2SAlok Kataria 95008ec0c58SJohn Stultz schedule_delayed_work(&tsc_irqwork, 0); 95108ec0c58SJohn Stultz return 0; 9528fbbc4b4SAlok Kataria } 95308ec0c58SJohn Stultz /* 95408ec0c58SJohn Stultz * We use device_initcall here, to ensure we run after the hpet 95508ec0c58SJohn Stultz * is fully initialized, which may occur at fs_initcall time. 95608ec0c58SJohn Stultz */ 95708ec0c58SJohn Stultz device_initcall(init_tsc_clocksource); 9588fbbc4b4SAlok Kataria 9598fbbc4b4SAlok Kataria void __init tsc_init(void) 9608fbbc4b4SAlok Kataria { 9618fbbc4b4SAlok Kataria u64 lpj; 9628fbbc4b4SAlok Kataria int cpu; 9638fbbc4b4SAlok Kataria 964845b3944SThomas Gleixner x86_init.timers.tsc_pre_init(); 965845b3944SThomas Gleixner 9668fbbc4b4SAlok Kataria if (!cpu_has_tsc) 9678fbbc4b4SAlok Kataria return; 9688fbbc4b4SAlok Kataria 9692d826404SThomas Gleixner tsc_khz = x86_platform.calibrate_tsc(); 970e93ef949SAlok Kataria cpu_khz = tsc_khz; 9718fbbc4b4SAlok Kataria 972e93ef949SAlok Kataria if (!tsc_khz) { 9738fbbc4b4SAlok Kataria mark_tsc_unstable("could not calculate TSC khz"); 9748fbbc4b4SAlok Kataria return; 9758fbbc4b4SAlok Kataria } 9768fbbc4b4SAlok Kataria 977c767a54bSJoe Perches pr_info("Detected %lu.%03lu MHz processor\n", 9788fbbc4b4SAlok Kataria (unsigned long)cpu_khz / 1000, 9798fbbc4b4SAlok Kataria (unsigned long)cpu_khz % 1000); 9808fbbc4b4SAlok Kataria 9818fbbc4b4SAlok Kataria /* 9828fbbc4b4SAlok Kataria * Secondary CPUs do not run through tsc_init(), so set up 9838fbbc4b4SAlok Kataria * all the scale factors for all CPUs, assuming the same 9848fbbc4b4SAlok Kataria * speed as the bootup CPU. (cpufreq notifiers will fix this 9858fbbc4b4SAlok Kataria * up if their speed diverges) 9868fbbc4b4SAlok Kataria */ 9878fbbc4b4SAlok Kataria for_each_possible_cpu(cpu) 9888fbbc4b4SAlok Kataria set_cyc2ns_scale(cpu_khz, cpu); 9898fbbc4b4SAlok Kataria 9908fbbc4b4SAlok Kataria if (tsc_disabled > 0) 9918fbbc4b4SAlok Kataria return; 9928fbbc4b4SAlok Kataria 9938fbbc4b4SAlok Kataria /* now allow native_sched_clock() to use rdtsc */ 9948fbbc4b4SAlok Kataria tsc_disabled = 0; 9958fbbc4b4SAlok Kataria 996e82b8e4eSVenkatesh Pallipadi if (!no_sched_irq_time) 997e82b8e4eSVenkatesh Pallipadi enable_sched_clock_irqtime(); 998e82b8e4eSVenkatesh Pallipadi 99970de9a97SAlok Kataria lpj = ((u64)tsc_khz * 1000); 100070de9a97SAlok Kataria do_div(lpj, HZ); 100170de9a97SAlok Kataria lpj_fine = lpj; 100270de9a97SAlok Kataria 10038fbbc4b4SAlok Kataria use_tsc_delay(); 10048fbbc4b4SAlok Kataria 10058fbbc4b4SAlok Kataria if (unsynchronized_tsc()) 10068fbbc4b4SAlok Kataria mark_tsc_unstable("TSCs unsynchronized"); 10078fbbc4b4SAlok Kataria 1008395628efSAlok Kataria check_system_tsc_reliable(); 10098fbbc4b4SAlok Kataria } 10108fbbc4b4SAlok Kataria 1011b565201cSJack Steiner #ifdef CONFIG_SMP 1012b565201cSJack Steiner /* 1013b565201cSJack Steiner * If we have a constant TSC and are using the TSC for the delay loop, 1014b565201cSJack Steiner * we can skip clock calibration if another cpu in the same socket has already 1015b565201cSJack Steiner * been calibrated. This assumes that CONSTANT_TSC applies to all 1016b565201cSJack Steiner * cpus in the socket - this should be a safe assumption. 1017b565201cSJack Steiner */ 1018b565201cSJack Steiner unsigned long __cpuinit calibrate_delay_is_known(void) 1019b565201cSJack Steiner { 1020b565201cSJack Steiner int i, cpu = smp_processor_id(); 1021b565201cSJack Steiner 1022b565201cSJack Steiner if (!tsc_disabled && !cpu_has(&cpu_data(cpu), X86_FEATURE_CONSTANT_TSC)) 1023b565201cSJack Steiner return 0; 1024b565201cSJack Steiner 1025b565201cSJack Steiner for_each_online_cpu(i) 1026b565201cSJack Steiner if (cpu_data(i).phys_proc_id == cpu_data(cpu).phys_proc_id) 1027b565201cSJack Steiner return cpu_data(i).loops_per_jiffy; 1028b565201cSJack Steiner return 0; 1029b565201cSJack Steiner } 1030b565201cSJack Steiner #endif 1031