x86: TSC make the calibration loop smarter
[linux-2.6-block.git] / arch / x86 / kernel / tsc.c
CommitLineData
bfc0f594 1#include <linux/kernel.h>
0ef95533
AK
2#include <linux/sched.h>
3#include <linux/init.h>
4#include <linux/module.h>
5#include <linux/timer.h>
bfc0f594 6#include <linux/acpi_pmtmr.h>
2dbe06fa 7#include <linux/cpufreq.h>
8fbbc4b4
AK
8#include <linux/dmi.h>
9#include <linux/delay.h>
10#include <linux/clocksource.h>
11#include <linux/percpu.h>
bfc0f594
AK
12
13#include <asm/hpet.h>
8fbbc4b4
AK
14#include <asm/timer.h>
15#include <asm/vgtod.h>
16#include <asm/time.h>
17#include <asm/delay.h>
0ef95533
AK
18
19unsigned int cpu_khz; /* TSC clocks / usec, not used here */
20EXPORT_SYMBOL(cpu_khz);
21unsigned int tsc_khz;
22EXPORT_SYMBOL(tsc_khz);
23
24/*
25 * TSC can be unstable due to cpufreq or due to unsynced TSCs
26 */
8fbbc4b4 27static int tsc_unstable;
0ef95533
AK
28
29/* native_sched_clock() is called before tsc_init(), so
30 we must start with the TSC soft disabled to prevent
31 erroneous rdtsc usage on !cpu_has_tsc processors */
8fbbc4b4 32static int tsc_disabled = -1;
0ef95533
AK
33
34/*
35 * Scheduler clock - returns current time in nanosec units.
36 */
37u64 native_sched_clock(void)
38{
39 u64 this_offset;
40
41 /*
42 * Fall back to jiffies if there's no TSC available:
43 * ( But note that we still use it if the TSC is marked
44 * unstable. We do this because unlike Time Of Day,
45 * the scheduler clock tolerates small errors and it's
46 * very important for it to be as fast as the platform
47 * can achive it. )
48 */
49 if (unlikely(tsc_disabled)) {
50 /* No locking but a rare wrong value is not a big deal: */
51 return (jiffies_64 - INITIAL_JIFFIES) * (1000000000 / HZ);
52 }
53
54 /* read the Time Stamp Counter: */
55 rdtscll(this_offset);
56
57 /* return the value in ns */
58 return cycles_2_ns(this_offset);
59}
60
61/* We need to define a real function for sched_clock, to override the
62 weak default version */
63#ifdef CONFIG_PARAVIRT
64unsigned long long sched_clock(void)
65{
66 return paravirt_sched_clock();
67}
68#else
69unsigned long long
70sched_clock(void) __attribute__((alias("native_sched_clock")));
71#endif
72
73int check_tsc_unstable(void)
74{
75 return tsc_unstable;
76}
77EXPORT_SYMBOL_GPL(check_tsc_unstable);
78
79#ifdef CONFIG_X86_TSC
80int __init notsc_setup(char *str)
81{
82 printk(KERN_WARNING "notsc: Kernel compiled with CONFIG_X86_TSC, "
83 "cannot disable TSC completely.\n");
84 tsc_disabled = 1;
85 return 1;
86}
87#else
88/*
89 * disable flag for tsc. Takes effect by clearing the TSC cpu flag
90 * in cpu/common.c
91 */
92int __init notsc_setup(char *str)
93{
94 setup_clear_cpu_cap(X86_FEATURE_TSC);
95 return 1;
96}
97#endif
98
99__setup("notsc", notsc_setup);
bfc0f594
AK
100
101#define MAX_RETRIES 5
102#define SMI_TRESHOLD 50000
103
104/*
105 * Read TSC and the reference counters. Take care of SMI disturbance
106 */
827014be 107static u64 tsc_read_refs(u64 *p, int hpet)
bfc0f594
AK
108{
109 u64 t1, t2;
110 int i;
111
112 for (i = 0; i < MAX_RETRIES; i++) {
113 t1 = get_cycles();
114 if (hpet)
827014be 115 *p = hpet_readl(HPET_COUNTER) & 0xFFFFFFFF;
bfc0f594 116 else
827014be 117 *p = acpi_pm_read_early();
bfc0f594
AK
118 t2 = get_cycles();
119 if ((t2 - t1) < SMI_TRESHOLD)
120 return t2;
121 }
122 return ULLONG_MAX;
123}
124
d683ef7a
TG
125/*
126 * Calculate the TSC frequency from HPET reference
127 */
128static unsigned long calc_hpet_ref(u64 deltatsc, u64 hpet1, u64 hpet2)
129{
130 u64 tmp;
131
132 if (hpet2 < hpet1)
133 hpet2 += 0x100000000ULL;
134 hpet2 -= hpet1;
135 tmp = ((u64)hpet2 * hpet_readl(HPET_PERIOD));
136 do_div(tmp, 1000000);
137 do_div(deltatsc, tmp);
138
139 return (unsigned long) deltatsc;
140}
141
142/*
143 * Calculate the TSC frequency from PMTimer reference
144 */
145static unsigned long calc_pmtimer_ref(u64 deltatsc, u64 pm1, u64 pm2)
146{
147 u64 tmp;
148
149 if (!pm1 && !pm2)
150 return ULONG_MAX;
151
152 if (pm2 < pm1)
153 pm2 += (u64)ACPI_PM_OVRRUN;
154 pm2 -= pm1;
155 tmp = pm2 * 1000000000LL;
156 do_div(tmp, PMTMR_TICKS_PER_SEC);
157 do_div(deltatsc, tmp);
158
159 return (unsigned long) deltatsc;
160}
161
a977c400 162#define CAL_MS 10
cce3e057 163#define CAL_LATCH (CLOCK_TICK_RATE / (1000 / CAL_MS))
a977c400
TG
164#define CAL_PIT_LOOPS 1000
165
166#define CAL2_MS 50
167#define CAL2_LATCH (CLOCK_TICK_RATE / (1000 / CAL2_MS))
168#define CAL2_PIT_LOOPS 5000
169
cce3e057 170
ec0c15af
LT
171/*
172 * Try to calibrate the TSC against the Programmable
173 * Interrupt Timer and return the frequency of the TSC
174 * in kHz.
175 *
176 * Return ULONG_MAX on failure to calibrate.
177 */
a977c400 178static unsigned long pit_calibrate_tsc(u32 latch, unsigned long ms, int loopmin)
ec0c15af
LT
179{
180 u64 tsc, t1, t2, delta;
181 unsigned long tscmin, tscmax;
182 int pitcnt;
183
184 /* Set the Gate high, disable speaker */
185 outb((inb(0x61) & ~0x02) | 0x01, 0x61);
186
187 /*
188 * Setup CTC channel 2* for mode 0, (interrupt on terminal
189 * count mode), binary count. Set the latch register to 50ms
190 * (LSB then MSB) to begin countdown.
191 */
192 outb(0xb0, 0x43);
a977c400
TG
193 outb(latch & 0xff, 0x42);
194 outb(latch >> 8, 0x42);
ec0c15af
LT
195
196 tsc = t1 = t2 = get_cycles();
197
198 pitcnt = 0;
199 tscmax = 0;
200 tscmin = ULONG_MAX;
201 while ((inb(0x61) & 0x20) == 0) {
202 t2 = get_cycles();
203 delta = t2 - tsc;
204 tsc = t2;
205 if ((unsigned long) delta < tscmin)
206 tscmin = (unsigned int) delta;
207 if ((unsigned long) delta > tscmax)
208 tscmax = (unsigned int) delta;
209 pitcnt++;
210 }
211
212 /*
213 * Sanity checks:
214 *
a977c400 215 * If we were not able to read the PIT more than loopmin
ec0c15af
LT
216 * times, then we have been hit by a massive SMI
217 *
218 * If the maximum is 10 times larger than the minimum,
219 * then we got hit by an SMI as well.
220 */
a977c400 221 if (pitcnt < loopmin || tscmax > 10 * tscmin)
ec0c15af
LT
222 return ULONG_MAX;
223
224 /* Calculate the PIT value */
225 delta = t2 - t1;
a977c400 226 do_div(delta, ms);
ec0c15af
LT
227 return delta;
228}
229
230
bfc0f594 231/**
e93ef949 232 * native_calibrate_tsc - calibrate the tsc on boot
bfc0f594 233 */
e93ef949 234unsigned long native_calibrate_tsc(void)
bfc0f594 235{
827014be 236 u64 tsc1, tsc2, delta, ref1, ref2;
fbb16e24 237 unsigned long tsc_pit_min = ULONG_MAX, tsc_ref_min = ULONG_MAX;
a977c400
TG
238 unsigned long flags, latch, ms;
239 int hpet = is_hpet_enabled(), i, loopmin;
bfc0f594 240
fbb16e24
TG
241 /*
242 * Run 5 calibration loops to get the lowest frequency value
243 * (the best estimate). We use two different calibration modes
244 * here:
245 *
246 * 1) PIT loop. We set the PIT Channel 2 to oneshot mode and
247 * load a timeout of 50ms. We read the time right after we
248 * started the timer and wait until the PIT count down reaches
249 * zero. In each wait loop iteration we read the TSC and check
250 * the delta to the previous read. We keep track of the min
251 * and max values of that delta. The delta is mostly defined
252 * by the IO time of the PIT access, so we can detect when a
253 * SMI/SMM disturbance happend between the two reads. If the
254 * maximum time is significantly larger than the minimum time,
255 * then we discard the result and have another try.
256 *
257 * 2) Reference counter. If available we use the HPET or the
258 * PMTIMER as a reference to check the sanity of that value.
259 * We use separate TSC readouts and check inside of the
260 * reference read for a SMI/SMM disturbance. We dicard
261 * disturbed values here as well. We do that around the PIT
262 * calibration delay loop as we have to wait for a certain
263 * amount of time anyway.
264 */
a977c400
TG
265
266 /* Preset PIT loop values */
267 latch = CAL_LATCH;
268 ms = CAL_MS;
269 loopmin = CAL_PIT_LOOPS;
270
271 for (i = 0; i < 3; i++) {
ec0c15af 272 unsigned long tsc_pit_khz;
fbb16e24
TG
273
274 /*
275 * Read the start value and the reference count of
ec0c15af
LT
276 * hpet/pmtimer when available. Then do the PIT
277 * calibration, which will take at least 50ms, and
278 * read the end value.
fbb16e24 279 */
ec0c15af 280 local_irq_save(flags);
827014be 281 tsc1 = tsc_read_refs(&ref1, hpet);
a977c400 282 tsc_pit_khz = pit_calibrate_tsc(latch, ms, loopmin);
827014be 283 tsc2 = tsc_read_refs(&ref2, hpet);
fbb16e24
TG
284 local_irq_restore(flags);
285
ec0c15af
LT
286 /* Pick the lowest PIT TSC calibration so far */
287 tsc_pit_min = min(tsc_pit_min, tsc_pit_khz);
fbb16e24
TG
288
289 /* hpet or pmtimer available ? */
827014be 290 if (!hpet && !ref1 && !ref2)
fbb16e24
TG
291 continue;
292
293 /* Check, whether the sampling was disturbed by an SMI */
294 if (tsc1 == ULLONG_MAX || tsc2 == ULLONG_MAX)
295 continue;
296
297 tsc2 = (tsc2 - tsc1) * 1000000LL;
d683ef7a 298 if (hpet)
827014be 299 tsc2 = calc_hpet_ref(tsc2, ref1, ref2);
d683ef7a 300 else
827014be 301 tsc2 = calc_pmtimer_ref(tsc2, ref1, ref2);
fbb16e24 302
fbb16e24 303 tsc_ref_min = min(tsc_ref_min, (unsigned long) tsc2);
a977c400
TG
304
305 /* Check the reference deviation */
306 delta = ((u64) tsc_pit_min) * 100;
307 do_div(delta, tsc_ref_min);
308
309 /*
310 * If both calibration results are inside a 10% window
311 * then we can be sure, that the calibration
312 * succeeded. We break out of the loop right away. We
313 * use the reference value, as it is more precise.
314 */
315 if (delta >= 90 && delta <= 110) {
316 printk(KERN_INFO
317 "TSC: PIT calibration matches %s. %d loops\n",
318 hpet ? "HPET" : "PMTIMER", i + 1);
319 return tsc_ref_min;
320 }
321
322 /*
323 * Check whether PIT failed more than once. This
324 * happens in virtualized environments. We need to
325 * give the virtual PC a slightly longer timeframe for
326 * the HPET/PMTIMER to make the result precise.
327 */
328 if (i == 1 && tsc_pit_min == ULONG_MAX) {
329 latch = CAL2_LATCH;
330 ms = CAL2_MS;
331 loopmin = CAL2_PIT_LOOPS;
332 }
fbb16e24 333 }
bfc0f594
AK
334
335 /*
fbb16e24 336 * Now check the results.
bfc0f594 337 */
fbb16e24
TG
338 if (tsc_pit_min == ULONG_MAX) {
339 /* PIT gave no useful value */
340 printk(KERN_WARNING "TSC: PIT calibration failed due to "
341 "SMI disturbance.\n");
342
343 /* We don't have an alternative source, disable TSC */
827014be 344 if (!hpet && !ref1 && !ref2) {
fbb16e24
TG
345 printk("TSC: No reference (HPET/PMTIMER) available\n");
346 return 0;
347 }
348
349 /* The alternative source failed as well, disable TSC */
350 if (tsc_ref_min == ULONG_MAX) {
351 printk(KERN_WARNING "TSC: HPET/PMTIMER calibration "
a977c400 352 "failed.\n");
fbb16e24
TG
353 return 0;
354 }
355
356 /* Use the alternative source */
357 printk(KERN_INFO "TSC: using %s reference calibration\n",
358 hpet ? "HPET" : "PMTIMER");
359
360 return tsc_ref_min;
361 }
bfc0f594 362
fbb16e24 363 /* We don't have an alternative source, use the PIT calibration value */
827014be 364 if (!hpet && !ref1 && !ref2) {
fbb16e24
TG
365 printk(KERN_INFO "TSC: Using PIT calibration value\n");
366 return tsc_pit_min;
bfc0f594
AK
367 }
368
fbb16e24
TG
369 /* The alternative source failed, use the PIT calibration value */
370 if (tsc_ref_min == ULONG_MAX) {
a977c400
TG
371 printk(KERN_WARNING "TSC: HPET/PMTIMER calibration failed. "
372 "Using PIT calibration\n");
fbb16e24 373 return tsc_pit_min;
bfc0f594
AK
374 }
375
fbb16e24
TG
376 /*
377 * The calibration values differ too much. In doubt, we use
378 * the PIT value as we know that there are PMTIMERs around
a977c400 379 * running at double speed. At least we let the user know:
fbb16e24 380 */
a977c400
TG
381 printk(KERN_WARNING "TSC: PIT calibration deviates from %s: %lu %lu.\n",
382 hpet ? "HPET" : "PMTIMER", tsc_pit_min, tsc_ref_min);
fbb16e24
TG
383 printk(KERN_INFO "TSC: Using PIT calibration value\n");
384 return tsc_pit_min;
bfc0f594
AK
385}
386
bfc0f594
AK
387#ifdef CONFIG_X86_32
388/* Only called from the Powernow K7 cpu freq driver */
389int recalibrate_cpu_khz(void)
390{
391#ifndef CONFIG_SMP
392 unsigned long cpu_khz_old = cpu_khz;
393
394 if (cpu_has_tsc) {
e93ef949
AK
395 tsc_khz = calibrate_tsc();
396 cpu_khz = tsc_khz;
bfc0f594
AK
397 cpu_data(0).loops_per_jiffy =
398 cpufreq_scale(cpu_data(0).loops_per_jiffy,
399 cpu_khz_old, cpu_khz);
400 return 0;
401 } else
402 return -ENODEV;
403#else
404 return -ENODEV;
405#endif
406}
407
408EXPORT_SYMBOL(recalibrate_cpu_khz);
409
410#endif /* CONFIG_X86_32 */
2dbe06fa
AK
411
412/* Accelerators for sched_clock()
413 * convert from cycles(64bits) => nanoseconds (64bits)
414 * basic equation:
415 * ns = cycles / (freq / ns_per_sec)
416 * ns = cycles * (ns_per_sec / freq)
417 * ns = cycles * (10^9 / (cpu_khz * 10^3))
418 * ns = cycles * (10^6 / cpu_khz)
419 *
420 * Then we use scaling math (suggested by george@mvista.com) to get:
421 * ns = cycles * (10^6 * SC / cpu_khz) / SC
422 * ns = cycles * cyc2ns_scale / SC
423 *
424 * And since SC is a constant power of two, we can convert the div
425 * into a shift.
426 *
427 * We can use khz divisor instead of mhz to keep a better precision, since
428 * cyc2ns_scale is limited to 10^6 * 2^10, which fits in 32 bits.
429 * (mathieu.desnoyers@polymtl.ca)
430 *
431 * -johnstul@us.ibm.com "math is hard, lets go shopping!"
432 */
433
434DEFINE_PER_CPU(unsigned long, cyc2ns);
435
8fbbc4b4 436static void set_cyc2ns_scale(unsigned long cpu_khz, int cpu)
2dbe06fa
AK
437{
438 unsigned long long tsc_now, ns_now;
439 unsigned long flags, *scale;
440
441 local_irq_save(flags);
442 sched_clock_idle_sleep_event();
443
444 scale = &per_cpu(cyc2ns, cpu);
445
446 rdtscll(tsc_now);
447 ns_now = __cycles_2_ns(tsc_now);
448
449 if (cpu_khz)
450 *scale = (NSEC_PER_MSEC << CYC2NS_SCALE_FACTOR)/cpu_khz;
451
452 sched_clock_idle_wakeup_event(0);
453 local_irq_restore(flags);
454}
455
456#ifdef CONFIG_CPU_FREQ
457
458/* Frequency scaling support. Adjust the TSC based timer when the cpu frequency
459 * changes.
460 *
461 * RED-PEN: On SMP we assume all CPUs run with the same frequency. It's
462 * not that important because current Opteron setups do not support
463 * scaling on SMP anyroads.
464 *
465 * Should fix up last_tsc too. Currently gettimeofday in the
466 * first tick after the change will be slightly wrong.
467 */
468
469static unsigned int ref_freq;
470static unsigned long loops_per_jiffy_ref;
471static unsigned long tsc_khz_ref;
472
473static int time_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
474 void *data)
475{
476 struct cpufreq_freqs *freq = data;
477 unsigned long *lpj, dummy;
478
479 if (cpu_has(&cpu_data(freq->cpu), X86_FEATURE_CONSTANT_TSC))
480 return 0;
481
482 lpj = &dummy;
483 if (!(freq->flags & CPUFREQ_CONST_LOOPS))
484#ifdef CONFIG_SMP
485 lpj = &cpu_data(freq->cpu).loops_per_jiffy;
486#else
487 lpj = &boot_cpu_data.loops_per_jiffy;
488#endif
489
490 if (!ref_freq) {
491 ref_freq = freq->old;
492 loops_per_jiffy_ref = *lpj;
493 tsc_khz_ref = tsc_khz;
494 }
495 if ((val == CPUFREQ_PRECHANGE && freq->old < freq->new) ||
496 (val == CPUFREQ_POSTCHANGE && freq->old > freq->new) ||
497 (val == CPUFREQ_RESUMECHANGE)) {
498 *lpj = cpufreq_scale(loops_per_jiffy_ref, ref_freq, freq->new);
499
500 tsc_khz = cpufreq_scale(tsc_khz_ref, ref_freq, freq->new);
501 if (!(freq->flags & CPUFREQ_CONST_LOOPS))
502 mark_tsc_unstable("cpufreq changes");
503 }
504
52a8968c 505 set_cyc2ns_scale(tsc_khz, freq->cpu);
2dbe06fa
AK
506
507 return 0;
508}
509
510static struct notifier_block time_cpufreq_notifier_block = {
511 .notifier_call = time_cpufreq_notifier
512};
513
514static int __init cpufreq_tsc(void)
515{
060700b5
LT
516 if (!cpu_has_tsc)
517 return 0;
518 if (boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
519 return 0;
2dbe06fa
AK
520 cpufreq_register_notifier(&time_cpufreq_notifier_block,
521 CPUFREQ_TRANSITION_NOTIFIER);
522 return 0;
523}
524
525core_initcall(cpufreq_tsc);
526
527#endif /* CONFIG_CPU_FREQ */
8fbbc4b4
AK
528
529/* clocksource code */
530
531static struct clocksource clocksource_tsc;
532
533/*
534 * We compare the TSC to the cycle_last value in the clocksource
535 * structure to avoid a nasty time-warp. This can be observed in a
536 * very small window right after one CPU updated cycle_last under
537 * xtime/vsyscall_gtod lock and the other CPU reads a TSC value which
538 * is smaller than the cycle_last reference value due to a TSC which
539 * is slighty behind. This delta is nowhere else observable, but in
540 * that case it results in a forward time jump in the range of hours
541 * due to the unsigned delta calculation of the time keeping core
542 * code, which is necessary to support wrapping clocksources like pm
543 * timer.
544 */
545static cycle_t read_tsc(void)
546{
547 cycle_t ret = (cycle_t)get_cycles();
548
549 return ret >= clocksource_tsc.cycle_last ?
550 ret : clocksource_tsc.cycle_last;
551}
552
431ceb83 553#ifdef CONFIG_X86_64
8fbbc4b4
AK
554static cycle_t __vsyscall_fn vread_tsc(void)
555{
556 cycle_t ret = (cycle_t)vget_cycles();
557
558 return ret >= __vsyscall_gtod_data.clock.cycle_last ?
559 ret : __vsyscall_gtod_data.clock.cycle_last;
560}
431ceb83 561#endif
8fbbc4b4
AK
562
563static struct clocksource clocksource_tsc = {
564 .name = "tsc",
565 .rating = 300,
566 .read = read_tsc,
567 .mask = CLOCKSOURCE_MASK(64),
568 .shift = 22,
569 .flags = CLOCK_SOURCE_IS_CONTINUOUS |
570 CLOCK_SOURCE_MUST_VERIFY,
571#ifdef CONFIG_X86_64
572 .vread = vread_tsc,
573#endif
574};
575
576void mark_tsc_unstable(char *reason)
577{
578 if (!tsc_unstable) {
579 tsc_unstable = 1;
580 printk("Marking TSC unstable due to %s\n", reason);
581 /* Change only the rating, when not registered */
582 if (clocksource_tsc.mult)
583 clocksource_change_rating(&clocksource_tsc, 0);
584 else
585 clocksource_tsc.rating = 0;
586 }
587}
588
589EXPORT_SYMBOL_GPL(mark_tsc_unstable);
590
591static int __init dmi_mark_tsc_unstable(const struct dmi_system_id *d)
592{
593 printk(KERN_NOTICE "%s detected: marking TSC unstable.\n",
594 d->ident);
595 tsc_unstable = 1;
596 return 0;
597}
598
599/* List of systems that have known TSC problems */
600static struct dmi_system_id __initdata bad_tsc_dmi_table[] = {
601 {
602 .callback = dmi_mark_tsc_unstable,
603 .ident = "IBM Thinkpad 380XD",
604 .matches = {
605 DMI_MATCH(DMI_BOARD_VENDOR, "IBM"),
606 DMI_MATCH(DMI_BOARD_NAME, "2635FA0"),
607 },
608 },
609 {}
610};
611
612/*
613 * Geode_LX - the OLPC CPU has a possibly a very reliable TSC
614 */
615#ifdef CONFIG_MGEODE_LX
616/* RTSC counts during suspend */
617#define RTSC_SUSP 0x100
618
619static void __init check_geode_tsc_reliable(void)
620{
621 unsigned long res_low, res_high;
622
623 rdmsr_safe(MSR_GEODE_BUSCONT_CONF0, &res_low, &res_high);
624 if (res_low & RTSC_SUSP)
625 clocksource_tsc.flags &= ~CLOCK_SOURCE_MUST_VERIFY;
626}
627#else
628static inline void check_geode_tsc_reliable(void) { }
629#endif
630
631/*
632 * Make an educated guess if the TSC is trustworthy and synchronized
633 * over all CPUs.
634 */
635__cpuinit int unsynchronized_tsc(void)
636{
637 if (!cpu_has_tsc || tsc_unstable)
638 return 1;
639
640#ifdef CONFIG_SMP
641 if (apic_is_clustered_box())
642 return 1;
643#endif
644
645 if (boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
646 return 0;
647 /*
648 * Intel systems are normally all synchronized.
649 * Exceptions must mark TSC as unstable:
650 */
651 if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL) {
652 /* assume multi socket systems are not synchronized: */
653 if (num_possible_cpus() > 1)
654 tsc_unstable = 1;
655 }
656
657 return tsc_unstable;
658}
659
660static void __init init_tsc_clocksource(void)
661{
662 clocksource_tsc.mult = clocksource_khz2mult(tsc_khz,
663 clocksource_tsc.shift);
664 /* lower the rating if we already know its unstable: */
665 if (check_tsc_unstable()) {
666 clocksource_tsc.rating = 0;
667 clocksource_tsc.flags &= ~CLOCK_SOURCE_IS_CONTINUOUS;
668 }
669 clocksource_register(&clocksource_tsc);
670}
671
672void __init tsc_init(void)
673{
674 u64 lpj;
675 int cpu;
676
677 if (!cpu_has_tsc)
678 return;
679
e93ef949
AK
680 tsc_khz = calibrate_tsc();
681 cpu_khz = tsc_khz;
8fbbc4b4 682
e93ef949 683 if (!tsc_khz) {
8fbbc4b4
AK
684 mark_tsc_unstable("could not calculate TSC khz");
685 return;
686 }
687
688#ifdef CONFIG_X86_64
689 if (cpu_has(&boot_cpu_data, X86_FEATURE_CONSTANT_TSC) &&
690 (boot_cpu_data.x86_vendor == X86_VENDOR_AMD))
691 cpu_khz = calibrate_cpu();
692#endif
693
694 lpj = ((u64)tsc_khz * 1000);
695 do_div(lpj, HZ);
696 lpj_fine = lpj;
697
698 printk("Detected %lu.%03lu MHz processor.\n",
699 (unsigned long)cpu_khz / 1000,
700 (unsigned long)cpu_khz % 1000);
701
702 /*
703 * Secondary CPUs do not run through tsc_init(), so set up
704 * all the scale factors for all CPUs, assuming the same
705 * speed as the bootup CPU. (cpufreq notifiers will fix this
706 * up if their speed diverges)
707 */
708 for_each_possible_cpu(cpu)
709 set_cyc2ns_scale(cpu_khz, cpu);
710
711 if (tsc_disabled > 0)
712 return;
713
714 /* now allow native_sched_clock() to use rdtsc */
715 tsc_disabled = 0;
716
717 use_tsc_delay();
718 /* Check and install the TSC clocksource */
719 dmi_check_system(bad_tsc_dmi_table);
720
721 if (unsynchronized_tsc())
722 mark_tsc_unstable("TSCs unsynchronized");
723
724 check_geode_tsc_reliable();
725 init_tsc_clocksource();
726}
727