blob: 68657d8526fb89b43d6e55eb3e316b6625549dc3 [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 Gleixnerd9a5c0a2007-03-24 23:02:49 +010017static int tsc_enabled;
18
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, "
Pavel Machek7265b6f2008-02-19 11:02:30 +010031 "cannot disable TSC completely.\n");
32 mark_tsc_unstable("user disabled TSC");
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 flags, prev_scale, *scale;
88 unsigned long long tsc_now, ns_now;
89
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
98 prev_scale = *scale;
99 if (cpu_khz)
100 *scale = (NSEC_PER_MSEC << CYC2NS_SCALE_FACTOR)/cpu_khz;
101
102 /*
103 * Start smoothly with the new frequency:
104 */
105 sched_clock_idle_wakeup_event(0);
106 local_irq_restore(flags);
john stultz539eb112006-06-26 00:25:10 -0700107}
108
john stultz539eb112006-06-26 00:25:10 -0700109/*
110 * Scheduler clock - returns current time in nanosec units.
111 */
Jeremy Fitzhardinge688340e2007-07-17 18:37:04 -0700112unsigned long long native_sched_clock(void)
john stultz539eb112006-06-26 00:25:10 -0700113{
114 unsigned long long this_offset;
115
116 /*
Ingo Molnarf9690982007-02-13 13:26:22 +0100117 * Fall back to jiffies if there's no TSC available:
Ingo Molnarbb29ab22007-07-09 18:51:59 +0200118 * ( But note that we still use it if the TSC is marked
119 * unstable. We do this because unlike Time Of Day,
120 * the scheduler clock tolerates small errors and it's
121 * very important for it to be as fast as the platform
122 * can achive it. )
john stultz539eb112006-06-26 00:25:10 -0700123 */
Ingo Molnarbb29ab22007-07-09 18:51:59 +0200124 if (unlikely(!tsc_enabled && !tsc_unstable))
Ingo Molnarf9690982007-02-13 13:26:22 +0100125 /* No locking but a rare wrong value is not a big deal: */
john stultz539eb112006-06-26 00:25:10 -0700126 return (jiffies_64 - INITIAL_JIFFIES) * (1000000000 / HZ);
127
128 /* read the Time Stamp Counter: */
Jeremy Fitzhardinge688340e2007-07-17 18:37:04 -0700129 rdtscll(this_offset);
john stultz539eb112006-06-26 00:25:10 -0700130
131 /* return the value in ns */
132 return cycles_2_ns(this_offset);
133}
134
Jeremy Fitzhardinge688340e2007-07-17 18:37:04 -0700135/* We need to define a real function for sched_clock, to override the
136 weak default version */
137#ifdef CONFIG_PARAVIRT
138unsigned long long sched_clock(void)
139{
140 return paravirt_sched_clock();
141}
142#else
143unsigned long long sched_clock(void)
144 __attribute__((alias("native_sched_clock")));
145#endif
146
Zachary Amsden1182d852007-03-05 00:30:36 -0800147unsigned long native_calculate_cpu_khz(void)
john stultz539eb112006-06-26 00:25:10 -0700148{
149 unsigned long long start, end;
150 unsigned long count;
Dave Johnsonedaf4202007-10-23 22:37:22 +0200151 u64 delta64 = (u64)ULLONG_MAX;
john stultz539eb112006-06-26 00:25:10 -0700152 int i;
153 unsigned long flags;
154
155 local_irq_save(flags);
156
Dave Johnson8c660062007-10-23 22:37:22 +0200157 /* run 3 times to ensure the cache is warm and to get an accurate reading */
john stultz539eb112006-06-26 00:25:10 -0700158 for (i = 0; i < 3; i++) {
159 mach_prepare_counter();
160 rdtscll(start);
161 mach_countup(&count);
162 rdtscll(end);
Dave Johnson8c660062007-10-23 22:37:22 +0200163
164 /*
165 * Error: ECTCNEVERSET
166 * The CTC wasn't reliable: we got a hit on the very first read,
167 * or the CPU was so fast/slow that the quotient wouldn't fit in
168 * 32 bits..
169 */
170 if (count <= 1)
171 continue;
172
173 /* cpu freq too slow: */
174 if ((end - start) <= CALIBRATE_TIME_MSEC)
175 continue;
176
177 /*
178 * We want the minimum time of all runs in case one of them
179 * is inaccurate due to SMI or other delay
180 */
Dave Johnsonedaf4202007-10-23 22:37:22 +0200181 delta64 = min(delta64, (end - start));
john stultz539eb112006-06-26 00:25:10 -0700182 }
john stultz539eb112006-06-26 00:25:10 -0700183
Dave Johnson8c660062007-10-23 22:37:22 +0200184 /* cpu freq too fast (or every run was bad): */
john stultz539eb112006-06-26 00:25:10 -0700185 if (delta64 > (1ULL<<32))
186 goto err;
187
john stultz539eb112006-06-26 00:25:10 -0700188 delta64 += CALIBRATE_TIME_MSEC/2; /* round for do_div */
189 do_div(delta64,CALIBRATE_TIME_MSEC);
190
191 local_irq_restore(flags);
192 return (unsigned long)delta64;
193err:
194 local_irq_restore(flags);
195 return 0;
196}
197
198int recalibrate_cpu_khz(void)
199{
200#ifndef CONFIG_SMP
201 unsigned long cpu_khz_old = cpu_khz;
202
203 if (cpu_has_tsc) {
204 cpu_khz = calculate_cpu_khz();
205 tsc_khz = cpu_khz;
Mike Travis92cb7612007-10-19 20:35:04 +0200206 cpu_data(0).loops_per_jiffy =
207 cpufreq_scale(cpu_data(0).loops_per_jiffy,
john stultz539eb112006-06-26 00:25:10 -0700208 cpu_khz_old, cpu_khz);
209 return 0;
210 } else
211 return -ENODEV;
212#else
213 return -ENODEV;
214#endif
215}
216
217EXPORT_SYMBOL(recalibrate_cpu_khz);
218
john stultz539eb112006-06-26 00:25:10 -0700219#ifdef CONFIG_CPU_FREQ
220
john stultz539eb112006-06-26 00:25:10 -0700221/*
222 * if the CPU frequency is scaled, TSC-based delays will need a different
223 * loops_per_jiffy value to function properly.
224 */
225static unsigned int ref_freq = 0;
226static unsigned long loops_per_jiffy_ref = 0;
227static unsigned long cpu_khz_ref = 0;
228
229static int
230time_cpufreq_notifier(struct notifier_block *nb, unsigned long val, void *data)
231{
232 struct cpufreq_freqs *freq = data;
233
john stultz539eb112006-06-26 00:25:10 -0700234 if (!ref_freq) {
235 if (!freq->old){
236 ref_freq = freq->new;
Daniel Walkerdf3624a2007-05-02 19:27:18 +0200237 return 0;
john stultz539eb112006-06-26 00:25:10 -0700238 }
239 ref_freq = freq->old;
Mike Travis92cb7612007-10-19 20:35:04 +0200240 loops_per_jiffy_ref = cpu_data(freq->cpu).loops_per_jiffy;
john stultz539eb112006-06-26 00:25:10 -0700241 cpu_khz_ref = cpu_khz;
242 }
243
244 if ((val == CPUFREQ_PRECHANGE && freq->old < freq->new) ||
245 (val == CPUFREQ_POSTCHANGE && freq->old > freq->new) ||
246 (val == CPUFREQ_RESUMECHANGE)) {
247 if (!(freq->flags & CPUFREQ_CONST_LOOPS))
Mike Travis92cb7612007-10-19 20:35:04 +0200248 cpu_data(freq->cpu).loops_per_jiffy =
john stultz539eb112006-06-26 00:25:10 -0700249 cpufreq_scale(loops_per_jiffy_ref,
250 ref_freq, freq->new);
251
252 if (cpu_khz) {
253
254 if (num_online_cpus() == 1)
255 cpu_khz = cpufreq_scale(cpu_khz_ref,
256 ref_freq, freq->new);
257 if (!(freq->flags & CPUFREQ_CONST_LOOPS)) {
258 tsc_khz = cpu_khz;
Karsten Wiese4f41c942008-04-07 12:14:45 +0200259 set_cyc2ns_scale(cpu_khz, freq->cpu);
john stultz539eb112006-06-26 00:25:10 -0700260 /*
261 * TSC based sched_clock turns
262 * to junk w/ cpufreq
263 */
john stultz5a90cf22007-05-02 19:27:08 +0200264 mark_tsc_unstable("cpufreq changes");
john stultz539eb112006-06-26 00:25:10 -0700265 }
266 }
267 }
john stultz539eb112006-06-26 00:25:10 -0700268
269 return 0;
270}
271
272static struct notifier_block time_cpufreq_notifier_block = {
273 .notifier_call = time_cpufreq_notifier
274};
275
276static int __init cpufreq_tsc(void)
277{
Thomas Gleixner26a08eb2007-02-16 01:27:32 -0800278 return cpufreq_register_notifier(&time_cpufreq_notifier_block,
279 CPUFREQ_TRANSITION_NOTIFIER);
john stultz539eb112006-06-26 00:25:10 -0700280}
john stultz539eb112006-06-26 00:25:10 -0700281core_initcall(cpufreq_tsc);
282
283#endif
john stultz5d0cf412006-06-26 00:25:12 -0700284
285/* clock source code */
286
287static unsigned long current_tsc_khz = 0;
john stultz5d0cf412006-06-26 00:25:12 -0700288
289static cycle_t read_tsc(void)
290{
291 cycle_t ret;
292
293 rdtscll(ret);
294
Ingo Molnar5b13d862008-04-07 20:58:08 +0200295 return ret;
john stultz5d0cf412006-06-26 00:25:12 -0700296}
297
298static struct clocksource clocksource_tsc = {
299 .name = "tsc",
300 .rating = 300,
301 .read = read_tsc,
Jim Cromie7f9f3032006-06-26 00:25:15 -0700302 .mask = CLOCKSOURCE_MASK(64),
john stultz5d0cf412006-06-26 00:25:12 -0700303 .mult = 0, /* to be set */
304 .shift = 22,
Thomas Gleixner73b08d22007-02-16 01:27:36 -0800305 .flags = CLOCK_SOURCE_IS_CONTINUOUS |
306 CLOCK_SOURCE_MUST_VERIFY,
john stultz5d0cf412006-06-26 00:25:12 -0700307};
308
john stultz5a90cf22007-05-02 19:27:08 +0200309void mark_tsc_unstable(char *reason)
john stultz5d0cf412006-06-26 00:25:12 -0700310{
Thomas Gleixner7e69f2b2007-02-16 01:27:42 -0800311 if (!tsc_unstable) {
312 tsc_unstable = 1;
Thomas Gleixnerd9a5c0a2007-03-24 23:02:49 +0100313 tsc_enabled = 0;
john stultz5a90cf22007-05-02 19:27:08 +0200314 printk("Marking TSC unstable due to: %s.\n", reason);
Thomas Gleixner7e69f2b2007-02-16 01:27:42 -0800315 /* Can be called before registration */
316 if (clocksource_tsc.mult)
317 clocksource_change_rating(&clocksource_tsc, 0);
318 else
319 clocksource_tsc.rating = 0;
john stultz5d0cf412006-06-26 00:25:12 -0700320 }
john stultz5d0cf412006-06-26 00:25:12 -0700321}
Thomas Gleixner7e69f2b2007-02-16 01:27:42 -0800322EXPORT_SYMBOL_GPL(mark_tsc_unstable);
john stultz5d0cf412006-06-26 00:25:12 -0700323
Jeff Garzik18552562007-10-03 15:15:40 -0400324static int __init dmi_mark_tsc_unstable(const struct dmi_system_id *d)
john stultz5d0cf412006-06-26 00:25:12 -0700325{
326 printk(KERN_NOTICE "%s detected: marking TSC unstable.\n",
327 d->ident);
Thomas Gleixner7e69f2b2007-02-16 01:27:42 -0800328 tsc_unstable = 1;
john stultz5d0cf412006-06-26 00:25:12 -0700329 return 0;
330}
331
332/* List of systems that have known TSC problems */
333static struct dmi_system_id __initdata bad_tsc_dmi_table[] = {
334 {
335 .callback = dmi_mark_tsc_unstable,
336 .ident = "IBM Thinkpad 380XD",
337 .matches = {
338 DMI_MATCH(DMI_BOARD_VENDOR, "IBM"),
339 DMI_MATCH(DMI_BOARD_NAME, "2635FA0"),
340 },
341 },
342 {}
343};
344
john stultz5d0cf412006-06-26 00:25:12 -0700345/*
346 * Make an educated guess if the TSC is trustworthy and synchronized
347 * over all CPUs.
348 */
Ingo Molnar95492e42007-02-16 01:27:34 -0800349__cpuinit int unsynchronized_tsc(void)
john stultz5d0cf412006-06-26 00:25:12 -0700350{
Ingo Molnar95492e42007-02-16 01:27:34 -0800351 if (!cpu_has_tsc || tsc_unstable)
352 return 1;
Andi Kleen51fc97b2008-01-30 13:32:40 +0100353
354 /* Anything with constant TSC should be synchronized */
355 if (boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
356 return 0;
357
john stultz5d0cf412006-06-26 00:25:12 -0700358 /*
359 * Intel systems are normally all synchronized.
360 * Exceptions must mark TSC as unstable:
361 */
Thomas Gleixner7e69f2b2007-02-16 01:27:42 -0800362 if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL) {
363 /* assume multi socket systems are not synchronized: */
364 if (num_possible_cpus() > 1)
365 tsc_unstable = 1;
366 }
367 return tsc_unstable;
john stultz5d0cf412006-06-26 00:25:12 -0700368}
369
Marcelo Tosatti07190a02007-02-16 01:27:44 -0800370/*
371 * Geode_LX - the OLPC CPU has a possibly a very reliable TSC
372 */
373#ifdef CONFIG_MGEODE_LX
374/* RTSC counts during suspend */
375#define RTSC_SUSP 0x100
376
377static void __init check_geode_tsc_reliable(void)
378{
Ingo Molnarf97586b2007-10-17 18:04:34 +0200379 unsigned long res_low, res_high;
Marcelo Tosatti07190a02007-02-16 01:27:44 -0800380
Ingo Molnarf97586b2007-10-17 18:04:34 +0200381 rdmsr_safe(MSR_GEODE_BUSCONT_CONF0, &res_low, &res_high);
382 if (res_low & RTSC_SUSP)
Marcelo Tosatti07190a02007-02-16 01:27:44 -0800383 clocksource_tsc.flags &= ~CLOCK_SOURCE_MUST_VERIFY;
384}
385#else
386static inline void check_geode_tsc_reliable(void) { }
387#endif
388
john stultz6bb74df2007-03-05 00:30:50 -0800389
390void __init tsc_init(void)
john stultz5d0cf412006-06-26 00:25:12 -0700391{
Guillaume Chazarain53d517c2008-01-30 13:30:06 +0100392 int cpu;
393
Andi Kleen404ee5b2008-01-30 13:33:20 +0100394 if (!cpu_has_tsc)
Rusty Russell3c2047c2008-03-04 23:07:50 +1100395 return;
john stultz5d0cf412006-06-26 00:25:12 -0700396
john stultz6bb74df2007-03-05 00:30:50 -0800397 cpu_khz = calculate_cpu_khz();
398 tsc_khz = cpu_khz;
john stultz5d0cf412006-06-26 00:25:12 -0700399
Rusty Russell3c2047c2008-03-04 23:07:50 +1100400 if (!cpu_khz) {
401 mark_tsc_unstable("could not calculate TSC khz");
402 return;
403 }
john stultz6bb74df2007-03-05 00:30:50 -0800404
405 printk("Detected %lu.%03lu MHz processor.\n",
406 (unsigned long)cpu_khz / 1000,
407 (unsigned long)cpu_khz % 1000);
408
Guillaume Chazarain53d517c2008-01-30 13:30:06 +0100409 /*
410 * Secondary CPUs do not run through tsc_init(), so set up
411 * all the scale factors for all CPUs, assuming the same
412 * speed as the bootup CPU. (cpufreq notifiers will fix this
413 * up if their speed diverges)
414 */
415 for_each_possible_cpu(cpu)
416 set_cyc2ns_scale(cpu_khz, cpu);
417
john stultz6bb74df2007-03-05 00:30:50 -0800418 use_tsc_delay();
419
420 /* Check and install the TSC clocksource */
421 dmi_check_system(bad_tsc_dmi_table);
422
423 unsynchronized_tsc();
424 check_geode_tsc_reliable();
425 current_tsc_khz = tsc_khz;
426 clocksource_tsc.mult = clocksource_khz2mult(current_tsc_khz,
john stultz5d0cf412006-06-26 00:25:12 -0700427 clocksource_tsc.shift);
john stultz6bb74df2007-03-05 00:30:50 -0800428 /* lower the rating if we already know its unstable: */
429 if (check_tsc_unstable()) {
430 clocksource_tsc.rating = 0;
431 clocksource_tsc.flags &= ~CLOCK_SOURCE_IS_CONTINUOUS;
Thomas Gleixnerd9a5c0a2007-03-24 23:02:49 +0100432 } else
433 tsc_enabled = 1;
434
john stultz6bb74df2007-03-05 00:30:50 -0800435 clocksource_register(&clocksource_tsc);
john stultz5d0cf412006-06-26 00:25:12 -0700436}