1 #include <linux/kernel.h>
2 #include <linux/sched.h>
3 #include <linux/interrupt.h>
4 #include <linux/init.h>
5 #include <linux/clocksource.h>
6 #include <linux/time.h>
7 #include <linux/acpi.h>
8 #include <linux/cpufreq.h>
9 #include <linux/acpi_pmtmr.h>
12 #include <asm/timex.h>
13 #include <asm/timer.h>
14 #include <asm/vgtod.h>
16 extern int tsc_unstable;
17 extern int tsc_disabled;
20 * Make an educated guess if the TSC is trustworthy and synchronized
23 __cpuinit int unsynchronized_tsc(void)
29 if (apic_is_clustered_box())
33 if (boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
36 /* Assume multi socket systems are not synchronized */
37 return num_present_cpus() > 1;
40 static struct clocksource clocksource_tsc;
43 * We compare the TSC to the cycle_last value in the clocksource
44 * structure to avoid a nasty time-warp. This can be observed in a
45 * very small window right after one CPU updated cycle_last under
46 * xtime/vsyscall_gtod lock and the other CPU reads a TSC value which
47 * is smaller than the cycle_last reference value due to a TSC which
48 * is slighty behind. This delta is nowhere else observable, but in
49 * that case it results in a forward time jump in the range of hours
50 * due to the unsigned delta calculation of the time keeping core
51 * code, which is necessary to support wrapping clocksources like pm
54 static cycle_t read_tsc(void)
56 cycle_t ret = (cycle_t)get_cycles();
58 return ret >= clocksource_tsc.cycle_last ?
59 ret : clocksource_tsc.cycle_last;
62 static cycle_t __vsyscall_fn vread_tsc(void)
64 cycle_t ret = (cycle_t)vget_cycles();
66 return ret >= __vsyscall_gtod_data.clock.cycle_last ?
67 ret : __vsyscall_gtod_data.clock.cycle_last;
70 static struct clocksource clocksource_tsc = {
74 .mask = CLOCKSOURCE_MASK(64),
76 .flags = CLOCK_SOURCE_IS_CONTINUOUS |
77 CLOCK_SOURCE_MUST_VERIFY,
81 void mark_tsc_unstable(char *reason)
85 printk("Marking TSC unstable due to %s\n", reason);
86 /* Change only the rating, when not registered */
87 if (clocksource_tsc.mult)
88 clocksource_change_rating(&clocksource_tsc, 0);
90 clocksource_tsc.rating = 0;
93 EXPORT_SYMBOL_GPL(mark_tsc_unstable);
95 void __init init_tsc_clocksource(void)
100 clocksource_tsc.mult = clocksource_khz2mult(tsc_khz,
101 clocksource_tsc.shift);
102 if (check_tsc_unstable())
103 clocksource_tsc.rating = 0;
105 clocksource_register(&clocksource_tsc);