1 #include <linux/sched.h>
2 #include <linux/clocksource.h>
3 #include <linux/workqueue.h>
4 #include <linux/delay.h>
5 #include <linux/cpufreq.h>
6 #include <linux/jiffies.h>
7 #include <linux/init.h>
9 #include <linux/percpu.h>
11 #include <asm/delay.h>
14 #include <asm/timer.h>
16 #include "mach_timer.h"
18 extern int tsc_unstable;
19 extern int tsc_disabled;
21 /* clock source code */
23 static struct clocksource clocksource_tsc;
26 * We compare the TSC to the cycle_last value in the clocksource
27 * structure to avoid a nasty time-warp issue. This can be observed in
28 * a very small window right after one CPU updated cycle_last under
29 * xtime lock and the other CPU reads a TSC value which is smaller
30 * than the cycle_last reference value due to a TSC which is slighty
31 * behind. This delta is nowhere else observable, but in that case it
32 * results in a forward time jump in the range of hours due to the
33 * unsigned delta calculation of the time keeping core code, which is
34 * necessary to support wrapping clocksources like pm timer.
36 static cycle_t read_tsc(void)
42 return ret >= clocksource_tsc.cycle_last ?
43 ret : clocksource_tsc.cycle_last;
46 static struct clocksource clocksource_tsc = {
50 .mask = CLOCKSOURCE_MASK(64),
51 .mult = 0, /* to be set */
53 .flags = CLOCK_SOURCE_IS_CONTINUOUS |
54 CLOCK_SOURCE_MUST_VERIFY,
57 void mark_tsc_unstable(char *reason)
61 printk("Marking TSC unstable due to: %s.\n", reason);
62 /* Can be called before registration */
63 if (clocksource_tsc.mult)
64 clocksource_change_rating(&clocksource_tsc, 0);
66 clocksource_tsc.rating = 0;
69 EXPORT_SYMBOL_GPL(mark_tsc_unstable);
71 static int __init dmi_mark_tsc_unstable(const struct dmi_system_id *d)
73 printk(KERN_NOTICE "%s detected: marking TSC unstable.\n",
79 /* List of systems that have known TSC problems */
80 static struct dmi_system_id __initdata bad_tsc_dmi_table[] = {
82 .callback = dmi_mark_tsc_unstable,
83 .ident = "IBM Thinkpad 380XD",
85 DMI_MATCH(DMI_BOARD_VENDOR, "IBM"),
86 DMI_MATCH(DMI_BOARD_NAME, "2635FA0"),
93 * Make an educated guess if the TSC is trustworthy and synchronized
96 __cpuinit int unsynchronized_tsc(void)
98 if (!cpu_has_tsc || tsc_unstable)
101 /* Anything with constant TSC should be synchronized */
102 if (boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
106 * Intel systems are normally all synchronized.
107 * Exceptions must mark TSC as unstable:
109 if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL) {
110 /* assume multi socket systems are not synchronized: */
111 if (num_possible_cpus() > 1)
118 * Geode_LX - the OLPC CPU has a possibly a very reliable TSC
120 #ifdef CONFIG_MGEODE_LX
121 /* RTSC counts during suspend */
122 #define RTSC_SUSP 0x100
124 static void __init check_geode_tsc_reliable(void)
126 unsigned long res_low, res_high;
128 rdmsr_safe(MSR_GEODE_BUSCONT_CONF0, &res_low, &res_high);
129 if (res_low & RTSC_SUSP)
130 clocksource_tsc.flags &= ~CLOCK_SOURCE_MUST_VERIFY;
133 static inline void check_geode_tsc_reliable(void) { }
137 void __init tsc_init(void)
142 if (!cpu_has_tsc || tsc_disabled > 0)
145 cpu_khz = calculate_cpu_khz();
149 mark_tsc_unstable("could not calculate TSC khz");
153 lpj = ((u64)tsc_khz * 1000);
157 /* now allow native_sched_clock() to use rdtsc */
160 printk("Detected %lu.%03lu MHz processor.\n",
161 (unsigned long)cpu_khz / 1000,
162 (unsigned long)cpu_khz % 1000);
165 * Secondary CPUs do not run through tsc_init(), so set up
166 * all the scale factors for all CPUs, assuming the same
167 * speed as the bootup CPU. (cpufreq notifiers will fix this
168 * up if their speed diverges)
170 for_each_possible_cpu(cpu)
171 set_cyc2ns_scale(cpu_khz, cpu);
175 /* Check and install the TSC clocksource */
176 dmi_check_system(bad_tsc_dmi_table);
178 unsynchronized_tsc();
179 check_geode_tsc_reliable();
180 clocksource_tsc.mult = clocksource_khz2mult(tsc_khz,
181 clocksource_tsc.shift);
182 /* lower the rating if we already know its unstable: */
183 if (check_tsc_unstable()) {
184 clocksource_tsc.rating = 0;
185 clocksource_tsc.flags &= ~CLOCK_SOURCE_IS_CONTINUOUS;
187 clocksource_register(&clocksource_tsc);