2 * This code largely moved from arch/i386/kernel/timer/timer_tsc.c
3 * which was originally moved from arch/i386/kernel/time.c.
4 * See comments there for proper credits.
7 #include <linux/sched.h>
8 #include <linux/clocksource.h>
9 #include <linux/workqueue.h>
10 #include <linux/cpufreq.h>
11 #include <linux/jiffies.h>
12 #include <linux/init.h>
13 #include <linux/dmi.h>
15 #include <asm/delay.h>
18 #include <asm/timer.h>
20 #include "mach_timer.h"
22 static int tsc_enabled;
25 * On some systems the TSC frequency does not
26 * change with the cpu frequency. So we need
27 * an extra value to store the TSC freq
34 static int __init tsc_setup(char *str)
36 printk(KERN_WARNING "notsc: Kernel compiled with CONFIG_X86_TSC, "
37 "cannot disable TSC.\n");
42 * disable flag for tsc. Takes effect by clearing the TSC cpu flag
45 static int __init tsc_setup(char *str)
53 __setup("notsc", tsc_setup);
56 * code to mark and check if the TSC is unstable
57 * due to cpufreq or due to unsynced TSCs
59 static int tsc_unstable;
61 static inline int check_tsc_unstable(void)
66 /* Accellerators for sched_clock()
67 * convert from cycles(64bits) => nanoseconds (64bits)
69 * ns = cycles / (freq / ns_per_sec)
70 * ns = cycles * (ns_per_sec / freq)
71 * ns = cycles * (10^9 / (cpu_khz * 10^3))
72 * ns = cycles * (10^6 / cpu_khz)
74 * Then we use scaling math (suggested by george@mvista.com) to get:
75 * ns = cycles * (10^6 * SC / cpu_khz) / SC
76 * ns = cycles * cyc2ns_scale / SC
78 * And since SC is a constant power of two, we can convert the div
81 * We can use khz divisor instead of mhz to keep a better percision, since
82 * cyc2ns_scale is limited to 10^6 * 2^10, which fits in 32 bits.
83 * (mathieu.desnoyers@polymtl.ca)
85 * -johnstul@us.ibm.com "math is hard, lets go shopping!"
87 static unsigned long cyc2ns_scale __read_mostly;
89 #define CYC2NS_SCALE_FACTOR 10 /* 2^10, carefully chosen */
91 static inline void set_cyc2ns_scale(unsigned long cpu_khz)
93 cyc2ns_scale = (1000000 << CYC2NS_SCALE_FACTOR)/cpu_khz;
96 static inline unsigned long long cycles_2_ns(unsigned long long cyc)
98 return (cyc * cyc2ns_scale) >> CYC2NS_SCALE_FACTOR;
102 * Scheduler clock - returns current time in nanosec units.
104 unsigned long long sched_clock(void)
106 unsigned long long this_offset;
109 * Fall back to jiffies if there's no TSC available:
110 * ( But note that we still use it if the TSC is marked
111 * unstable. We do this because unlike Time Of Day,
112 * the scheduler clock tolerates small errors and it's
113 * very important for it to be as fast as the platform
116 if (unlikely(!tsc_enabled && !tsc_unstable))
117 /* No locking but a rare wrong value is not a big deal: */
118 return (jiffies_64 - INITIAL_JIFFIES) * (1000000000 / HZ);
120 /* read the Time Stamp Counter: */
121 get_scheduled_cycles(this_offset);
123 /* return the value in ns */
124 return cycles_2_ns(this_offset);
127 unsigned long native_calculate_cpu_khz(void)
129 unsigned long long start, end;
135 local_irq_save(flags);
137 /* run 3 times to ensure the cache is warm */
138 for (i = 0; i < 3; i++) {
139 mach_prepare_counter();
141 mach_countup(&count);
145 * Error: ECTCNEVERSET
146 * The CTC wasn't reliable: we got a hit on the very first read,
147 * or the CPU was so fast/slow that the quotient wouldn't fit in
153 delta64 = end - start;
155 /* cpu freq too fast: */
156 if (delta64 > (1ULL<<32))
159 /* cpu freq too slow: */
160 if (delta64 <= CALIBRATE_TIME_MSEC)
163 delta64 += CALIBRATE_TIME_MSEC/2; /* round for do_div */
164 do_div(delta64,CALIBRATE_TIME_MSEC);
166 local_irq_restore(flags);
167 return (unsigned long)delta64;
169 local_irq_restore(flags);
173 int recalibrate_cpu_khz(void)
176 unsigned long cpu_khz_old = cpu_khz;
179 cpu_khz = calculate_cpu_khz();
181 cpu_data[0].loops_per_jiffy =
182 cpufreq_scale(cpu_data[0].loops_per_jiffy,
183 cpu_khz_old, cpu_khz);
192 EXPORT_SYMBOL(recalibrate_cpu_khz);
194 #ifdef CONFIG_CPU_FREQ
197 * if the CPU frequency is scaled, TSC-based delays will need a different
198 * loops_per_jiffy value to function properly.
200 static unsigned int ref_freq = 0;
201 static unsigned long loops_per_jiffy_ref = 0;
202 static unsigned long cpu_khz_ref = 0;
205 time_cpufreq_notifier(struct notifier_block *nb, unsigned long val, void *data)
207 struct cpufreq_freqs *freq = data;
211 ref_freq = freq->new;
214 ref_freq = freq->old;
215 loops_per_jiffy_ref = cpu_data[freq->cpu].loops_per_jiffy;
216 cpu_khz_ref = cpu_khz;
219 if ((val == CPUFREQ_PRECHANGE && freq->old < freq->new) ||
220 (val == CPUFREQ_POSTCHANGE && freq->old > freq->new) ||
221 (val == CPUFREQ_RESUMECHANGE)) {
222 if (!(freq->flags & CPUFREQ_CONST_LOOPS))
223 cpu_data[freq->cpu].loops_per_jiffy =
224 cpufreq_scale(loops_per_jiffy_ref,
225 ref_freq, freq->new);
229 if (num_online_cpus() == 1)
230 cpu_khz = cpufreq_scale(cpu_khz_ref,
231 ref_freq, freq->new);
232 if (!(freq->flags & CPUFREQ_CONST_LOOPS)) {
234 set_cyc2ns_scale(cpu_khz);
236 * TSC based sched_clock turns
239 mark_tsc_unstable("cpufreq changes");
247 static struct notifier_block time_cpufreq_notifier_block = {
248 .notifier_call = time_cpufreq_notifier
251 static int __init cpufreq_tsc(void)
253 return cpufreq_register_notifier(&time_cpufreq_notifier_block,
254 CPUFREQ_TRANSITION_NOTIFIER);
256 core_initcall(cpufreq_tsc);
260 /* clock source code */
262 static unsigned long current_tsc_khz = 0;
264 static cycle_t read_tsc(void)
273 static struct clocksource clocksource_tsc = {
277 .mask = CLOCKSOURCE_MASK(64),
278 .mult = 0, /* to be set */
280 .flags = CLOCK_SOURCE_IS_CONTINUOUS |
281 CLOCK_SOURCE_MUST_VERIFY,
284 void mark_tsc_unstable(char *reason)
286 sched_clock_unstable_event();
290 printk("Marking TSC unstable due to: %s.\n", reason);
291 /* Can be called before registration */
292 if (clocksource_tsc.mult)
293 clocksource_change_rating(&clocksource_tsc, 0);
295 clocksource_tsc.rating = 0;
298 EXPORT_SYMBOL_GPL(mark_tsc_unstable);
300 static int __init dmi_mark_tsc_unstable(struct dmi_system_id *d)
302 printk(KERN_NOTICE "%s detected: marking TSC unstable.\n",
308 /* List of systems that have known TSC problems */
309 static struct dmi_system_id __initdata bad_tsc_dmi_table[] = {
311 .callback = dmi_mark_tsc_unstable,
312 .ident = "IBM Thinkpad 380XD",
314 DMI_MATCH(DMI_BOARD_VENDOR, "IBM"),
315 DMI_MATCH(DMI_BOARD_NAME, "2635FA0"),
322 * Make an educated guess if the TSC is trustworthy and synchronized
325 __cpuinit int unsynchronized_tsc(void)
327 if (!cpu_has_tsc || tsc_unstable)
330 * Intel systems are normally all synchronized.
331 * Exceptions must mark TSC as unstable:
333 if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL) {
334 /* assume multi socket systems are not synchronized: */
335 if (num_possible_cpus() > 1)
342 * Geode_LX - the OLPC CPU has a possibly a very reliable TSC
344 #ifdef CONFIG_MGEODE_LX
345 /* RTSC counts during suspend */
346 #define RTSC_SUSP 0x100
348 static void __init check_geode_tsc_reliable(void)
352 rdmsrl(MSR_GEODE_BUSCONT_CONF0, val);
353 if ((val & RTSC_SUSP))
354 clocksource_tsc.flags &= ~CLOCK_SOURCE_MUST_VERIFY;
357 static inline void check_geode_tsc_reliable(void) { }
361 void __init tsc_init(void)
363 if (!cpu_has_tsc || tsc_disable)
366 cpu_khz = calculate_cpu_khz();
372 printk("Detected %lu.%03lu MHz processor.\n",
373 (unsigned long)cpu_khz / 1000,
374 (unsigned long)cpu_khz % 1000);
376 set_cyc2ns_scale(cpu_khz);
379 /* Check and install the TSC clocksource */
380 dmi_check_system(bad_tsc_dmi_table);
382 unsynchronized_tsc();
383 check_geode_tsc_reliable();
384 current_tsc_khz = tsc_khz;
385 clocksource_tsc.mult = clocksource_khz2mult(current_tsc_khz,
386 clocksource_tsc.shift);
387 /* lower the rating if we already know its unstable: */
388 if (check_tsc_unstable()) {
389 clocksource_tsc.rating = 0;
390 clocksource_tsc.flags &= ~CLOCK_SOURCE_IS_CONTINUOUS;
394 clocksource_register(&clocksource_tsc);
400 * Set the tsc_disable flag if there's no TSC support, this
401 * makes it a fast flag for the kernel to see whether it
402 * should be using the TSC.