blob: 068759db63ddebca0ed5561a1306a302dc38f474 [file] [log] [blame]
Ingo Molnarbb29ab22007-07-09 18:51:59 +02001#include <linux/sched.h>
john stultz5d0cf412006-06-26 00:25:12 -07002#include <linux/clocksource.h>
john stultz539eb112006-06-26 00:25:10 -07003#include <linux/workqueue.h>
4#include <linux/cpufreq.h>
5#include <linux/jiffies.h>
6#include <linux/init.h>
john stultz5d0cf412006-06-26 00:25:12 -07007#include <linux/dmi.h>
Guillaume Chazarain53d517c2008-01-30 13:30:06 +01008#include <linux/percpu.h>
john stultz539eb112006-06-26 00:25:10 -07009
john stultz5d0cf412006-06-26 00:25:12 -070010#include <asm/delay.h>
john stultz539eb112006-06-26 00:25:10 -070011#include <asm/tsc.h>
12#include <asm/io.h>
Zachary Amsden6cb9a832007-03-05 00:30:35 -080013#include <asm/timer.h>
john stultz539eb112006-06-26 00:25:10 -070014
15#include "mach_timer.h"
16
Thomas Gleixner9ccc9062008-05-13 12:31:00 +020017static int tsc_disabled;
Thomas Gleixnerd9a5c0a2007-03-24 23:02:49 +010018
john stultz539eb112006-06-26 00:25:10 -070019/*
20 * On some systems the TSC frequency does not
21 * change with the cpu frequency. So we need
22 * an extra value to store the TSC freq
23 */
24unsigned int tsc_khz;
Rusty Russelld7e28ff2007-07-19 01:49:23 -070025EXPORT_SYMBOL_GPL(tsc_khz);
john stultz539eb112006-06-26 00:25:10 -070026
john stultz539eb112006-06-26 00:25:10 -070027#ifdef CONFIG_X86_TSC
28static int __init tsc_setup(char *str)
29{
30 printk(KERN_WARNING "notsc: Kernel compiled with CONFIG_X86_TSC, "
Thomas Gleixner9ccc9062008-05-13 12:31:00 +020031 "cannot disable TSC completely.\n");
32 tsc_disabled = 1;
john stultz539eb112006-06-26 00:25:10 -070033 return 1;
34}
35#else
36/*
37 * disable flag for tsc. Takes effect by clearing the TSC cpu flag
38 * in cpu/common.c
39 */
40static int __init tsc_setup(char *str)
41{
Andi Kleen404ee5b2008-01-30 13:33:20 +010042 setup_clear_cpu_cap(X86_FEATURE_TSC);
john stultz539eb112006-06-26 00:25:10 -070043 return 1;
44}
45#endif
46
47__setup("notsc", tsc_setup);
48
john stultz539eb112006-06-26 00:25:10 -070049/*
50 * code to mark and check if the TSC is unstable
51 * due to cpufreq or due to unsynced TSCs
52 */
53static int tsc_unstable;
54
Rusty Russelld7e28ff2007-07-19 01:49:23 -070055int check_tsc_unstable(void)
john stultz539eb112006-06-26 00:25:10 -070056{
57 return tsc_unstable;
58}
Rusty Russelld7e28ff2007-07-19 01:49:23 -070059EXPORT_SYMBOL_GPL(check_tsc_unstable);
john stultz539eb112006-06-26 00:25:10 -070060
Simon Arlott27b46d72007-10-20 01:13:56 +020061/* Accelerators for sched_clock()
john stultz539eb112006-06-26 00:25:10 -070062 * convert from cycles(64bits) => nanoseconds (64bits)
63 * basic equation:
64 * ns = cycles / (freq / ns_per_sec)
65 * ns = cycles * (ns_per_sec / freq)
66 * ns = cycles * (10^9 / (cpu_khz * 10^3))
67 * ns = cycles * (10^6 / cpu_khz)
68 *
69 * Then we use scaling math (suggested by george@mvista.com) to get:
70 * ns = cycles * (10^6 * SC / cpu_khz) / SC
71 * ns = cycles * cyc2ns_scale / SC
72 *
73 * And since SC is a constant power of two, we can convert the div
74 * into a shift.
75 *
Josh Triplett96315122007-10-20 02:23:49 +020076 * We can use khz divisor instead of mhz to keep a better precision, since
john stultz539eb112006-06-26 00:25:10 -070077 * cyc2ns_scale is limited to 10^6 * 2^10, which fits in 32 bits.
78 * (mathieu.desnoyers@polymtl.ca)
79 *
80 * -johnstul@us.ibm.com "math is hard, lets go shopping!"
81 */
john stultz539eb112006-06-26 00:25:10 -070082
Guillaume Chazarain53d517c2008-01-30 13:30:06 +010083DEFINE_PER_CPU(unsigned long, cyc2ns);
john stultz539eb112006-06-26 00:25:10 -070084
Guillaume Chazarain53d517c2008-01-30 13:30:06 +010085static void set_cyc2ns_scale(unsigned long cpu_khz, int cpu)
john stultz539eb112006-06-26 00:25:10 -070086{
Guillaume Chazarain53d517c2008-01-30 13:30:06 +010087 unsigned long long tsc_now, ns_now;
Ingo Molnar1725037f2008-03-31 14:52:15 +020088 unsigned long flags, *scale;
Guillaume Chazarain53d517c2008-01-30 13:30:06 +010089
90 local_irq_save(flags);
91 sched_clock_idle_sleep_event();
92
93 scale = &per_cpu(cyc2ns, cpu);
94
95 rdtscll(tsc_now);
96 ns_now = __cycles_2_ns(tsc_now);
97
Guillaume Chazarain53d517c2008-01-30 13:30:06 +010098 if (cpu_khz)
99 *scale = (NSEC_PER_MSEC << CYC2NS_SCALE_FACTOR)/cpu_khz;
100
101 /*
102 * Start smoothly with the new frequency:
103 */
104 sched_clock_idle_wakeup_event(0);
105 local_irq_restore(flags);
john stultz539eb112006-06-26 00:25:10 -0700106}
107
john stultz539eb112006-06-26 00:25:10 -0700108/*
109 * Scheduler clock - returns current time in nanosec units.
110 */
Jeremy Fitzhardinge688340e2007-07-17 18:37:04 -0700111unsigned long long native_sched_clock(void)
john stultz539eb112006-06-26 00:25:10 -0700112{
113 unsigned long long this_offset;
114
115 /*
Ingo Molnarf9690982007-02-13 13:26:22 +0100116 * Fall back to jiffies if there's no TSC available:
Ingo Molnarbb29ab22007-07-09 18:51:59 +0200117 * ( But note that we still use it if the TSC is marked
118 * unstable. We do this because unlike Time Of Day,
119 * the scheduler clock tolerates small errors and it's
120 * very important for it to be as fast as the platform
121 * can achive it. )
john stultz539eb112006-06-26 00:25:10 -0700122 */
Thomas Gleixner9ccc9062008-05-13 12:31:00 +0200123 if (unlikely(tsc_disabled))
Ingo Molnarf9690982007-02-13 13:26:22 +0100124 /* No locking but a rare wrong value is not a big deal: */
john stultz539eb112006-06-26 00:25:10 -0700125 return (jiffies_64 - INITIAL_JIFFIES) * (1000000000 / HZ);
126
127 /* read the Time Stamp Counter: */
Jeremy Fitzhardinge688340e2007-07-17 18:37:04 -0700128 rdtscll(this_offset);
john stultz539eb112006-06-26 00:25:10 -0700129
130 /* return the value in ns */
131 return cycles_2_ns(this_offset);
132}
133
Jeremy Fitzhardinge688340e2007-07-17 18:37:04 -0700134/* We need to define a real function for sched_clock, to override the
135 weak default version */
136#ifdef CONFIG_PARAVIRT
137unsigned long long sched_clock(void)
138{
139 return paravirt_sched_clock();
140}
141#else
142unsigned long long sched_clock(void)
143 __attribute__((alias("native_sched_clock")));
144#endif
145
Zachary Amsden1182d852007-03-05 00:30:36 -0800146unsigned long native_calculate_cpu_khz(void)
john stultz539eb112006-06-26 00:25:10 -0700147{
148 unsigned long long start, end;
149 unsigned long count;
Dave Johnsonedaf4202007-10-23 22:37:22 +0200150 u64 delta64 = (u64)ULLONG_MAX;
john stultz539eb112006-06-26 00:25:10 -0700151 int i;
152 unsigned long flags;
153
154 local_irq_save(flags);
155
Dave Johnson8c660062007-10-23 22:37:22 +0200156 /* run 3 times to ensure the cache is warm and to get an accurate reading */
john stultz539eb112006-06-26 00:25:10 -0700157 for (i = 0; i < 3; i++) {
158 mach_prepare_counter();
159 rdtscll(start);
160 mach_countup(&count);
161 rdtscll(end);
Dave Johnson8c660062007-10-23 22:37:22 +0200162
163 /*
164 * Error: ECTCNEVERSET
165 * The CTC wasn't reliable: we got a hit on the very first read,
166 * or the CPU was so fast/slow that the quotient wouldn't fit in
167 * 32 bits..
168 */
169 if (count <= 1)
170 continue;
171
172 /* cpu freq too slow: */
173 if ((end - start) <= CALIBRATE_TIME_MSEC)
174 continue;
175
176 /*
177 * We want the minimum time of all runs in case one of them
178 * is inaccurate due to SMI or other delay
179 */
Dave Johnsonedaf4202007-10-23 22:37:22 +0200180 delta64 = min(delta64, (end - start));
john stultz539eb112006-06-26 00:25:10 -0700181 }
john stultz539eb112006-06-26 00:25:10 -0700182
Dave Johnson8c660062007-10-23 22:37:22 +0200183 /* cpu freq too fast (or every run was bad): */
john stultz539eb112006-06-26 00:25:10 -0700184 if (delta64 > (1ULL<<32))
185 goto err;
186
john stultz539eb112006-06-26 00:25:10 -0700187 delta64 += CALIBRATE_TIME_MSEC/2; /* round for do_div */
188 do_div(delta64,CALIBRATE_TIME_MSEC);
189
190 local_irq_restore(flags);
191 return (unsigned long)delta64;
192err:
193 local_irq_restore(flags);
194 return 0;
195}
196
197int recalibrate_cpu_khz(void)
198{
199#ifndef CONFIG_SMP
200 unsigned long cpu_khz_old = cpu_khz;
201
202 if (cpu_has_tsc) {
203 cpu_khz = calculate_cpu_khz();
204 tsc_khz = cpu_khz;
Mike Travis92cb7612007-10-19 20:35:04 +0200205 cpu_data(0).loops_per_jiffy =
206 cpufreq_scale(cpu_data(0).loops_per_jiffy,
john stultz539eb112006-06-26 00:25:10 -0700207 cpu_khz_old, cpu_khz);
208 return 0;
209 } else
210 return -ENODEV;
211#else
212 return -ENODEV;
213#endif
214}
215
216EXPORT_SYMBOL(recalibrate_cpu_khz);
217
john stultz539eb112006-06-26 00:25:10 -0700218#ifdef CONFIG_CPU_FREQ
219
john stultz539eb112006-06-26 00:25:10 -0700220/*
221 * if the CPU frequency is scaled, TSC-based delays will need a different
222 * loops_per_jiffy value to function properly.
223 */
Pavel Machek4bd01602008-02-19 11:02:30 +0100224static unsigned int ref_freq;
225static unsigned long loops_per_jiffy_ref;
226static unsigned long cpu_khz_ref;
john stultz539eb112006-06-26 00:25:10 -0700227
228static int
229time_cpufreq_notifier(struct notifier_block *nb, unsigned long val, void *data)
230{
231 struct cpufreq_freqs *freq = data;
232
john stultz539eb112006-06-26 00:25:10 -0700233 if (!ref_freq) {
234 if (!freq->old){
235 ref_freq = freq->new;
Daniel Walkerdf3624a2007-05-02 19:27:18 +0200236 return 0;
john stultz539eb112006-06-26 00:25:10 -0700237 }
238 ref_freq = freq->old;
Mike Travis92cb7612007-10-19 20:35:04 +0200239 loops_per_jiffy_ref = cpu_data(freq->cpu).loops_per_jiffy;
john stultz539eb112006-06-26 00:25:10 -0700240 cpu_khz_ref = cpu_khz;
241 }
242
243 if ((val == CPUFREQ_PRECHANGE && freq->old < freq->new) ||
244 (val == CPUFREQ_POSTCHANGE && freq->old > freq->new) ||
245 (val == CPUFREQ_RESUMECHANGE)) {
246 if (!(freq->flags & CPUFREQ_CONST_LOOPS))
Mike Travis92cb7612007-10-19 20:35:04 +0200247 cpu_data(freq->cpu).loops_per_jiffy =
john stultz539eb112006-06-26 00:25:10 -0700248 cpufreq_scale(loops_per_jiffy_ref,
249 ref_freq, freq->new);
250
251 if (cpu_khz) {
252
253 if (num_online_cpus() == 1)
254 cpu_khz = cpufreq_scale(cpu_khz_ref,
255 ref_freq, freq->new);
256 if (!(freq->flags & CPUFREQ_CONST_LOOPS)) {
257 tsc_khz = cpu_khz;
Karsten Wiese4f41c942008-04-07 12:14:45 +0200258 set_cyc2ns_scale(cpu_khz, freq->cpu);
john stultz539eb112006-06-26 00:25:10 -0700259 /*
260 * TSC based sched_clock turns
261 * to junk w/ cpufreq
262 */
john stultz5a90cf22007-05-02 19:27:08 +0200263 mark_tsc_unstable("cpufreq changes");
john stultz539eb112006-06-26 00:25:10 -0700264 }
265 }
266 }
john stultz539eb112006-06-26 00:25:10 -0700267
268 return 0;
269}
270
271static struct notifier_block time_cpufreq_notifier_block = {
272 .notifier_call = time_cpufreq_notifier
273};
274
275static int __init cpufreq_tsc(void)
276{
Thomas Gleixner26a08eb2007-02-16 01:27:32 -0800277 return cpufreq_register_notifier(&time_cpufreq_notifier_block,
278 CPUFREQ_TRANSITION_NOTIFIER);
john stultz539eb112006-06-26 00:25:10 -0700279}
john stultz539eb112006-06-26 00:25:10 -0700280core_initcall(cpufreq_tsc);
281
282#endif
john stultz5d0cf412006-06-26 00:25:12 -0700283
284/* clock source code */
285
Pavel Machek4bd01602008-02-19 11:02:30 +0100286static unsigned long current_tsc_khz;
Thomas Gleixnerd8bb6f42008-04-01 19:45:18 +0200287static struct clocksource clocksource_tsc;
john stultz5d0cf412006-06-26 00:25:12 -0700288
Thomas Gleixnerd8bb6f42008-04-01 19:45:18 +0200289/*
290 * We compare the TSC to the cycle_last value in the clocksource
291 * structure to avoid a nasty time-warp issue. This can be observed in
292 * a very small window right after one CPU updated cycle_last under
293 * xtime lock and the other CPU reads a TSC value which is smaller
294 * than the cycle_last reference value due to a TSC which is slighty
295 * behind. This delta is nowhere else observable, but in that case it
296 * results in a forward time jump in the range of hours due to the
297 * unsigned delta calculation of the time keeping core code, which is
298 * necessary to support wrapping clocksources like pm timer.
299 */
john stultz5d0cf412006-06-26 00:25:12 -0700300static cycle_t read_tsc(void)
301{
302 cycle_t ret;
303
304 rdtscll(ret);
305
Thomas Gleixnerd8bb6f42008-04-01 19:45:18 +0200306 return ret >= clocksource_tsc.cycle_last ?
307 ret : clocksource_tsc.cycle_last;
john stultz5d0cf412006-06-26 00:25:12 -0700308}
309
310static struct clocksource clocksource_tsc = {
311 .name = "tsc",
312 .rating = 300,
313 .read = read_tsc,
Jim Cromie7f9f3032006-06-26 00:25:15 -0700314 .mask = CLOCKSOURCE_MASK(64),
john stultz5d0cf412006-06-26 00:25:12 -0700315 .mult = 0, /* to be set */
316 .shift = 22,
Thomas Gleixner73b08d22007-02-16 01:27:36 -0800317 .flags = CLOCK_SOURCE_IS_CONTINUOUS |
318 CLOCK_SOURCE_MUST_VERIFY,
john stultz5d0cf412006-06-26 00:25:12 -0700319};
320
john stultz5a90cf22007-05-02 19:27:08 +0200321void mark_tsc_unstable(char *reason)
john stultz5d0cf412006-06-26 00:25:12 -0700322{
Thomas Gleixner7e69f2b2007-02-16 01:27:42 -0800323 if (!tsc_unstable) {
324 tsc_unstable = 1;
john stultz5a90cf22007-05-02 19:27:08 +0200325 printk("Marking TSC unstable due to: %s.\n", reason);
Thomas Gleixner7e69f2b2007-02-16 01:27:42 -0800326 /* Can be called before registration */
327 if (clocksource_tsc.mult)
328 clocksource_change_rating(&clocksource_tsc, 0);
329 else
330 clocksource_tsc.rating = 0;
john stultz5d0cf412006-06-26 00:25:12 -0700331 }
john stultz5d0cf412006-06-26 00:25:12 -0700332}
Thomas Gleixner7e69f2b2007-02-16 01:27:42 -0800333EXPORT_SYMBOL_GPL(mark_tsc_unstable);
john stultz5d0cf412006-06-26 00:25:12 -0700334
Jeff Garzik18552562007-10-03 15:15:40 -0400335static int __init dmi_mark_tsc_unstable(const struct dmi_system_id *d)
john stultz5d0cf412006-06-26 00:25:12 -0700336{
337 printk(KERN_NOTICE "%s detected: marking TSC unstable.\n",
Thomas Gleixner9ccc9062008-05-13 12:31:00 +0200338 d->ident);
Thomas Gleixner7e69f2b2007-02-16 01:27:42 -0800339 tsc_unstable = 1;
john stultz5d0cf412006-06-26 00:25:12 -0700340 return 0;
341}
342
343/* List of systems that have known TSC problems */
344static struct dmi_system_id __initdata bad_tsc_dmi_table[] = {
345 {
346 .callback = dmi_mark_tsc_unstable,
347 .ident = "IBM Thinkpad 380XD",
348 .matches = {
349 DMI_MATCH(DMI_BOARD_VENDOR, "IBM"),
350 DMI_MATCH(DMI_BOARD_NAME, "2635FA0"),
351 },
352 },
353 {}
354};
355
john stultz5d0cf412006-06-26 00:25:12 -0700356/*
357 * Make an educated guess if the TSC is trustworthy and synchronized
358 * over all CPUs.
359 */
Ingo Molnar95492e42007-02-16 01:27:34 -0800360__cpuinit int unsynchronized_tsc(void)
john stultz5d0cf412006-06-26 00:25:12 -0700361{
Ingo Molnar95492e42007-02-16 01:27:34 -0800362 if (!cpu_has_tsc || tsc_unstable)
363 return 1;
Andi Kleen51fc97b2008-01-30 13:32:40 +0100364
365 /* Anything with constant TSC should be synchronized */
366 if (boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
367 return 0;
368
john stultz5d0cf412006-06-26 00:25:12 -0700369 /*
370 * Intel systems are normally all synchronized.
371 * Exceptions must mark TSC as unstable:
372 */
Thomas Gleixner7e69f2b2007-02-16 01:27:42 -0800373 if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL) {
374 /* assume multi socket systems are not synchronized: */
375 if (num_possible_cpus() > 1)
376 tsc_unstable = 1;
377 }
378 return tsc_unstable;
john stultz5d0cf412006-06-26 00:25:12 -0700379}
380
Marcelo Tosatti07190a02007-02-16 01:27:44 -0800381/*
382 * Geode_LX - the OLPC CPU has a possibly a very reliable TSC
383 */
384#ifdef CONFIG_MGEODE_LX
385/* RTSC counts during suspend */
386#define RTSC_SUSP 0x100
387
388static void __init check_geode_tsc_reliable(void)
389{
Ingo Molnarf97586b2007-10-17 18:04:34 +0200390 unsigned long res_low, res_high;
Marcelo Tosatti07190a02007-02-16 01:27:44 -0800391
Ingo Molnarf97586b2007-10-17 18:04:34 +0200392 rdmsr_safe(MSR_GEODE_BUSCONT_CONF0, &res_low, &res_high);
393 if (res_low & RTSC_SUSP)
Marcelo Tosatti07190a02007-02-16 01:27:44 -0800394 clocksource_tsc.flags &= ~CLOCK_SOURCE_MUST_VERIFY;
395}
396#else
397static inline void check_geode_tsc_reliable(void) { }
398#endif
399
john stultz6bb74df2007-03-05 00:30:50 -0800400
401void __init tsc_init(void)
john stultz5d0cf412006-06-26 00:25:12 -0700402{
Guillaume Chazarain53d517c2008-01-30 13:30:06 +0100403 int cpu;
404
Thomas Gleixner9ccc9062008-05-13 12:31:00 +0200405 if (!cpu_has_tsc || tsc_disabled) {
406 /* Disable the TSC in case of !cpu_has_tsc */
407 tsc_disabled = 1;
Rusty Russell3c2047c2008-03-04 23:07:50 +1100408 return;
Thomas Gleixner9ccc9062008-05-13 12:31:00 +0200409 }
john stultz5d0cf412006-06-26 00:25:12 -0700410
john stultz6bb74df2007-03-05 00:30:50 -0800411 cpu_khz = calculate_cpu_khz();
412 tsc_khz = cpu_khz;
john stultz5d0cf412006-06-26 00:25:12 -0700413
Rusty Russell3c2047c2008-03-04 23:07:50 +1100414 if (!cpu_khz) {
415 mark_tsc_unstable("could not calculate TSC khz");
Thomas Gleixner74dc51a2008-05-18 22:17:59 +0200416 /*
417 * We need to disable the TSC completely in this case
418 * to prevent sched_clock() from using it.
419 */
420 tsc_disabled = 1;
Rusty Russell3c2047c2008-03-04 23:07:50 +1100421 return;
422 }
john stultz6bb74df2007-03-05 00:30:50 -0800423
424 printk("Detected %lu.%03lu MHz processor.\n",
425 (unsigned long)cpu_khz / 1000,
426 (unsigned long)cpu_khz % 1000);
427
Guillaume Chazarain53d517c2008-01-30 13:30:06 +0100428 /*
429 * Secondary CPUs do not run through tsc_init(), so set up
430 * all the scale factors for all CPUs, assuming the same
431 * speed as the bootup CPU. (cpufreq notifiers will fix this
432 * up if their speed diverges)
433 */
434 for_each_possible_cpu(cpu)
435 set_cyc2ns_scale(cpu_khz, cpu);
436
john stultz6bb74df2007-03-05 00:30:50 -0800437 use_tsc_delay();
438
439 /* Check and install the TSC clocksource */
440 dmi_check_system(bad_tsc_dmi_table);
441
442 unsynchronized_tsc();
443 check_geode_tsc_reliable();
444 current_tsc_khz = tsc_khz;
445 clocksource_tsc.mult = clocksource_khz2mult(current_tsc_khz,
john stultz5d0cf412006-06-26 00:25:12 -0700446 clocksource_tsc.shift);
john stultz6bb74df2007-03-05 00:30:50 -0800447 /* lower the rating if we already know its unstable: */
448 if (check_tsc_unstable()) {
449 clocksource_tsc.rating = 0;
450 clocksource_tsc.flags &= ~CLOCK_SOURCE_IS_CONTINUOUS;
Thomas Gleixner9ccc9062008-05-13 12:31:00 +0200451 }
john stultz6bb74df2007-03-05 00:30:50 -0800452 clocksource_register(&clocksource_tsc);
john stultz5d0cf412006-06-26 00:25:12 -0700453}