blob: 6240922e497c30aa9a96a1982fe73a2a698ed5eb [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>
Ingo Molnar6ff10de2008-06-24 01:19:49 +02004#include <linux/delay.h>
john stultz539eb112006-06-26 00:25:10 -07005#include <linux/cpufreq.h>
6#include <linux/jiffies.h>
7#include <linux/init.h>
john stultz5d0cf412006-06-26 00:25:12 -07008#include <linux/dmi.h>
Guillaume Chazarain53d517c2008-01-30 13:30:06 +01009#include <linux/percpu.h>
john stultz539eb112006-06-26 00:25:10 -070010
john stultz5d0cf412006-06-26 00:25:12 -070011#include <asm/delay.h>
john stultz539eb112006-06-26 00:25:10 -070012#include <asm/tsc.h>
13#include <asm/io.h>
Zachary Amsden6cb9a832007-03-05 00:30:35 -080014#include <asm/timer.h>
john stultz539eb112006-06-26 00:25:10 -070015
16#include "mach_timer.h"
17
Mikael Petterssondf17b1d2008-06-15 02:19:56 +020018/* native_sched_clock() is called before tsc_init(), so
19 we must start with the TSC soft disabled to prevent
20 erroneous rdtsc usage on !cpu_has_tsc processors */
21static int tsc_disabled = -1;
Thomas Gleixnerd9a5c0a2007-03-24 23:02:49 +010022
john stultz539eb112006-06-26 00:25:10 -070023/*
24 * On some systems the TSC frequency does not
25 * change with the cpu frequency. So we need
26 * an extra value to store the TSC freq
27 */
28unsigned int tsc_khz;
Rusty Russelld7e28ff2007-07-19 01:49:23 -070029EXPORT_SYMBOL_GPL(tsc_khz);
john stultz539eb112006-06-26 00:25:10 -070030
john stultz539eb112006-06-26 00:25:10 -070031#ifdef CONFIG_X86_TSC
32static int __init tsc_setup(char *str)
33{
34 printk(KERN_WARNING "notsc: Kernel compiled with CONFIG_X86_TSC, "
Thomas Gleixner9ccc9062008-05-13 12:31:00 +020035 "cannot disable TSC completely.\n");
36 tsc_disabled = 1;
john stultz539eb112006-06-26 00:25:10 -070037 return 1;
38}
39#else
40/*
41 * disable flag for tsc. Takes effect by clearing the TSC cpu flag
42 * in cpu/common.c
43 */
44static int __init tsc_setup(char *str)
45{
Andi Kleen404ee5b2008-01-30 13:33:20 +010046 setup_clear_cpu_cap(X86_FEATURE_TSC);
john stultz539eb112006-06-26 00:25:10 -070047 return 1;
48}
49#endif
50
51__setup("notsc", tsc_setup);
52
john stultz539eb112006-06-26 00:25:10 -070053/*
54 * code to mark and check if the TSC is unstable
55 * due to cpufreq or due to unsynced TSCs
56 */
57static int tsc_unstable;
58
Rusty Russelld7e28ff2007-07-19 01:49:23 -070059int check_tsc_unstable(void)
john stultz539eb112006-06-26 00:25:10 -070060{
61 return tsc_unstable;
62}
Rusty Russelld7e28ff2007-07-19 01:49:23 -070063EXPORT_SYMBOL_GPL(check_tsc_unstable);
john stultz539eb112006-06-26 00:25:10 -070064
Simon Arlott27b46d72007-10-20 01:13:56 +020065/* Accelerators for sched_clock()
john stultz539eb112006-06-26 00:25:10 -070066 * convert from cycles(64bits) => nanoseconds (64bits)
67 * basic equation:
68 * ns = cycles / (freq / ns_per_sec)
69 * ns = cycles * (ns_per_sec / freq)
70 * ns = cycles * (10^9 / (cpu_khz * 10^3))
71 * ns = cycles * (10^6 / cpu_khz)
72 *
73 * Then we use scaling math (suggested by george@mvista.com) to get:
74 * ns = cycles * (10^6 * SC / cpu_khz) / SC
75 * ns = cycles * cyc2ns_scale / SC
76 *
77 * And since SC is a constant power of two, we can convert the div
78 * into a shift.
79 *
Josh Triplett96315122007-10-20 02:23:49 +020080 * We can use khz divisor instead of mhz to keep a better precision, since
john stultz539eb112006-06-26 00:25:10 -070081 * cyc2ns_scale is limited to 10^6 * 2^10, which fits in 32 bits.
82 * (mathieu.desnoyers@polymtl.ca)
83 *
84 * -johnstul@us.ibm.com "math is hard, lets go shopping!"
85 */
john stultz539eb112006-06-26 00:25:10 -070086
Guillaume Chazarain53d517c2008-01-30 13:30:06 +010087DEFINE_PER_CPU(unsigned long, cyc2ns);
john stultz539eb112006-06-26 00:25:10 -070088
Guillaume Chazarain53d517c2008-01-30 13:30:06 +010089static void set_cyc2ns_scale(unsigned long cpu_khz, int cpu)
john stultz539eb112006-06-26 00:25:10 -070090{
Guillaume Chazarain53d517c2008-01-30 13:30:06 +010091 unsigned long long tsc_now, ns_now;
Ingo Molnar1725037f2008-03-31 14:52:15 +020092 unsigned long flags, *scale;
Guillaume Chazarain53d517c2008-01-30 13:30:06 +010093
94 local_irq_save(flags);
95 sched_clock_idle_sleep_event();
96
97 scale = &per_cpu(cyc2ns, cpu);
98
99 rdtscll(tsc_now);
100 ns_now = __cycles_2_ns(tsc_now);
101
Guillaume Chazarain53d517c2008-01-30 13:30:06 +0100102 if (cpu_khz)
103 *scale = (NSEC_PER_MSEC << CYC2NS_SCALE_FACTOR)/cpu_khz;
104
105 /*
106 * Start smoothly with the new frequency:
107 */
108 sched_clock_idle_wakeup_event(0);
109 local_irq_restore(flags);
john stultz539eb112006-06-26 00:25:10 -0700110}
111
john stultz539eb112006-06-26 00:25:10 -0700112/*
113 * Scheduler clock - returns current time in nanosec units.
114 */
Jeremy Fitzhardinge688340e2007-07-17 18:37:04 -0700115unsigned long long native_sched_clock(void)
john stultz539eb112006-06-26 00:25:10 -0700116{
117 unsigned long long this_offset;
118
119 /*
Ingo Molnarf9690982007-02-13 13:26:22 +0100120 * Fall back to jiffies if there's no TSC available:
Ingo Molnarbb29ab22007-07-09 18:51:59 +0200121 * ( But note that we still use it if the TSC is marked
122 * unstable. We do this because unlike Time Of Day,
123 * the scheduler clock tolerates small errors and it's
124 * very important for it to be as fast as the platform
125 * can achive it. )
john stultz539eb112006-06-26 00:25:10 -0700126 */
Thomas Gleixner9ccc9062008-05-13 12:31:00 +0200127 if (unlikely(tsc_disabled))
Ingo Molnarf9690982007-02-13 13:26:22 +0100128 /* No locking but a rare wrong value is not a big deal: */
john stultz539eb112006-06-26 00:25:10 -0700129 return (jiffies_64 - INITIAL_JIFFIES) * (1000000000 / HZ);
130
131 /* read the Time Stamp Counter: */
Jeremy Fitzhardinge688340e2007-07-17 18:37:04 -0700132 rdtscll(this_offset);
john stultz539eb112006-06-26 00:25:10 -0700133
134 /* return the value in ns */
135 return cycles_2_ns(this_offset);
136}
137
Jeremy Fitzhardinge688340e2007-07-17 18:37:04 -0700138/* We need to define a real function for sched_clock, to override the
139 weak default version */
140#ifdef CONFIG_PARAVIRT
141unsigned long long sched_clock(void)
142{
143 return paravirt_sched_clock();
144}
145#else
146unsigned long long sched_clock(void)
147 __attribute__((alias("native_sched_clock")));
148#endif
149
Zachary Amsden1182d852007-03-05 00:30:36 -0800150unsigned long native_calculate_cpu_khz(void)
john stultz539eb112006-06-26 00:25:10 -0700151{
152 unsigned long long start, end;
153 unsigned long count;
Dave Johnsonedaf4202007-10-23 22:37:22 +0200154 u64 delta64 = (u64)ULLONG_MAX;
john stultz539eb112006-06-26 00:25:10 -0700155 int i;
156 unsigned long flags;
157
158 local_irq_save(flags);
159
Dave Johnson8c660062007-10-23 22:37:22 +0200160 /* run 3 times to ensure the cache is warm and to get an accurate reading */
john stultz539eb112006-06-26 00:25:10 -0700161 for (i = 0; i < 3; i++) {
162 mach_prepare_counter();
163 rdtscll(start);
164 mach_countup(&count);
165 rdtscll(end);
Dave Johnson8c660062007-10-23 22:37:22 +0200166
167 /*
168 * Error: ECTCNEVERSET
169 * The CTC wasn't reliable: we got a hit on the very first read,
170 * or the CPU was so fast/slow that the quotient wouldn't fit in
171 * 32 bits..
172 */
173 if (count <= 1)
174 continue;
175
176 /* cpu freq too slow: */
177 if ((end - start) <= CALIBRATE_TIME_MSEC)
178 continue;
179
180 /*
181 * We want the minimum time of all runs in case one of them
182 * is inaccurate due to SMI or other delay
183 */
Dave Johnsonedaf4202007-10-23 22:37:22 +0200184 delta64 = min(delta64, (end - start));
john stultz539eb112006-06-26 00:25:10 -0700185 }
john stultz539eb112006-06-26 00:25:10 -0700186
Dave Johnson8c660062007-10-23 22:37:22 +0200187 /* cpu freq too fast (or every run was bad): */
john stultz539eb112006-06-26 00:25:10 -0700188 if (delta64 > (1ULL<<32))
189 goto err;
190
john stultz539eb112006-06-26 00:25:10 -0700191 delta64 += CALIBRATE_TIME_MSEC/2; /* round for do_div */
192 do_div(delta64,CALIBRATE_TIME_MSEC);
193
194 local_irq_restore(flags);
195 return (unsigned long)delta64;
196err:
197 local_irq_restore(flags);
198 return 0;
199}
200
201int recalibrate_cpu_khz(void)
202{
203#ifndef CONFIG_SMP
204 unsigned long cpu_khz_old = cpu_khz;
205
206 if (cpu_has_tsc) {
207 cpu_khz = calculate_cpu_khz();
208 tsc_khz = cpu_khz;
Mike Travis92cb7612007-10-19 20:35:04 +0200209 cpu_data(0).loops_per_jiffy =
210 cpufreq_scale(cpu_data(0).loops_per_jiffy,
john stultz539eb112006-06-26 00:25:10 -0700211 cpu_khz_old, cpu_khz);
212 return 0;
213 } else
214 return -ENODEV;
215#else
216 return -ENODEV;
217#endif
218}
219
220EXPORT_SYMBOL(recalibrate_cpu_khz);
221
john stultz539eb112006-06-26 00:25:10 -0700222#ifdef CONFIG_CPU_FREQ
223
john stultz539eb112006-06-26 00:25:10 -0700224/*
225 * if the CPU frequency is scaled, TSC-based delays will need a different
226 * loops_per_jiffy value to function properly.
227 */
Pavel Machek4bd01602008-02-19 11:02:30 +0100228static unsigned int ref_freq;
229static unsigned long loops_per_jiffy_ref;
230static unsigned long cpu_khz_ref;
john stultz539eb112006-06-26 00:25:10 -0700231
232static int
233time_cpufreq_notifier(struct notifier_block *nb, unsigned long val, void *data)
234{
235 struct cpufreq_freqs *freq = data;
236
john stultz539eb112006-06-26 00:25:10 -0700237 if (!ref_freq) {
238 if (!freq->old){
239 ref_freq = freq->new;
Daniel Walkerdf3624a2007-05-02 19:27:18 +0200240 return 0;
john stultz539eb112006-06-26 00:25:10 -0700241 }
242 ref_freq = freq->old;
Mike Travis92cb7612007-10-19 20:35:04 +0200243 loops_per_jiffy_ref = cpu_data(freq->cpu).loops_per_jiffy;
john stultz539eb112006-06-26 00:25:10 -0700244 cpu_khz_ref = cpu_khz;
245 }
246
247 if ((val == CPUFREQ_PRECHANGE && freq->old < freq->new) ||
248 (val == CPUFREQ_POSTCHANGE && freq->old > freq->new) ||
249 (val == CPUFREQ_RESUMECHANGE)) {
250 if (!(freq->flags & CPUFREQ_CONST_LOOPS))
Mike Travis92cb7612007-10-19 20:35:04 +0200251 cpu_data(freq->cpu).loops_per_jiffy =
john stultz539eb112006-06-26 00:25:10 -0700252 cpufreq_scale(loops_per_jiffy_ref,
253 ref_freq, freq->new);
254
255 if (cpu_khz) {
256
257 if (num_online_cpus() == 1)
258 cpu_khz = cpufreq_scale(cpu_khz_ref,
259 ref_freq, freq->new);
260 if (!(freq->flags & CPUFREQ_CONST_LOOPS)) {
261 tsc_khz = cpu_khz;
Karsten Wiese4f41c942008-04-07 12:14:45 +0200262 set_cyc2ns_scale(cpu_khz, freq->cpu);
john stultz539eb112006-06-26 00:25:10 -0700263 /*
264 * TSC based sched_clock turns
265 * to junk w/ cpufreq
266 */
john stultz5a90cf22007-05-02 19:27:08 +0200267 mark_tsc_unstable("cpufreq changes");
john stultz539eb112006-06-26 00:25:10 -0700268 }
269 }
270 }
john stultz539eb112006-06-26 00:25:10 -0700271
272 return 0;
273}
274
275static struct notifier_block time_cpufreq_notifier_block = {
276 .notifier_call = time_cpufreq_notifier
277};
278
279static int __init cpufreq_tsc(void)
280{
Thomas Gleixner26a08eb2007-02-16 01:27:32 -0800281 return cpufreq_register_notifier(&time_cpufreq_notifier_block,
282 CPUFREQ_TRANSITION_NOTIFIER);
john stultz539eb112006-06-26 00:25:10 -0700283}
john stultz539eb112006-06-26 00:25:10 -0700284core_initcall(cpufreq_tsc);
285
286#endif
john stultz5d0cf412006-06-26 00:25:12 -0700287
288/* clock source code */
289
Thomas Gleixnerd8bb6f42008-04-01 19:45:18 +0200290static struct clocksource clocksource_tsc;
john stultz5d0cf412006-06-26 00:25:12 -0700291
Thomas Gleixnerd8bb6f42008-04-01 19:45:18 +0200292/*
293 * We compare the TSC to the cycle_last value in the clocksource
294 * structure to avoid a nasty time-warp issue. This can be observed in
295 * a very small window right after one CPU updated cycle_last under
296 * xtime lock and the other CPU reads a TSC value which is smaller
297 * than the cycle_last reference value due to a TSC which is slighty
298 * behind. This delta is nowhere else observable, but in that case it
299 * results in a forward time jump in the range of hours due to the
300 * unsigned delta calculation of the time keeping core code, which is
301 * necessary to support wrapping clocksources like pm timer.
302 */
john stultz5d0cf412006-06-26 00:25:12 -0700303static cycle_t read_tsc(void)
304{
305 cycle_t ret;
306
307 rdtscll(ret);
308
Thomas Gleixnerd8bb6f42008-04-01 19:45:18 +0200309 return ret >= clocksource_tsc.cycle_last ?
310 ret : clocksource_tsc.cycle_last;
john stultz5d0cf412006-06-26 00:25:12 -0700311}
312
313static struct clocksource clocksource_tsc = {
314 .name = "tsc",
315 .rating = 300,
316 .read = read_tsc,
Jim Cromie7f9f3032006-06-26 00:25:15 -0700317 .mask = CLOCKSOURCE_MASK(64),
john stultz5d0cf412006-06-26 00:25:12 -0700318 .mult = 0, /* to be set */
319 .shift = 22,
Thomas Gleixner73b08d22007-02-16 01:27:36 -0800320 .flags = CLOCK_SOURCE_IS_CONTINUOUS |
321 CLOCK_SOURCE_MUST_VERIFY,
john stultz5d0cf412006-06-26 00:25:12 -0700322};
323
john stultz5a90cf22007-05-02 19:27:08 +0200324void mark_tsc_unstable(char *reason)
john stultz5d0cf412006-06-26 00:25:12 -0700325{
Thomas Gleixner7e69f2b2007-02-16 01:27:42 -0800326 if (!tsc_unstable) {
327 tsc_unstable = 1;
john stultz5a90cf22007-05-02 19:27:08 +0200328 printk("Marking TSC unstable due to: %s.\n", reason);
Thomas Gleixner7e69f2b2007-02-16 01:27:42 -0800329 /* Can be called before registration */
330 if (clocksource_tsc.mult)
331 clocksource_change_rating(&clocksource_tsc, 0);
332 else
333 clocksource_tsc.rating = 0;
john stultz5d0cf412006-06-26 00:25:12 -0700334 }
john stultz5d0cf412006-06-26 00:25:12 -0700335}
Thomas Gleixner7e69f2b2007-02-16 01:27:42 -0800336EXPORT_SYMBOL_GPL(mark_tsc_unstable);
john stultz5d0cf412006-06-26 00:25:12 -0700337
Jeff Garzik18552562007-10-03 15:15:40 -0400338static int __init dmi_mark_tsc_unstable(const struct dmi_system_id *d)
john stultz5d0cf412006-06-26 00:25:12 -0700339{
340 printk(KERN_NOTICE "%s detected: marking TSC unstable.\n",
Thomas Gleixner9ccc9062008-05-13 12:31:00 +0200341 d->ident);
Thomas Gleixner7e69f2b2007-02-16 01:27:42 -0800342 tsc_unstable = 1;
john stultz5d0cf412006-06-26 00:25:12 -0700343 return 0;
344}
345
346/* List of systems that have known TSC problems */
347static struct dmi_system_id __initdata bad_tsc_dmi_table[] = {
348 {
349 .callback = dmi_mark_tsc_unstable,
350 .ident = "IBM Thinkpad 380XD",
351 .matches = {
352 DMI_MATCH(DMI_BOARD_VENDOR, "IBM"),
353 DMI_MATCH(DMI_BOARD_NAME, "2635FA0"),
354 },
355 },
356 {}
357};
358
john stultz5d0cf412006-06-26 00:25:12 -0700359/*
360 * Make an educated guess if the TSC is trustworthy and synchronized
361 * over all CPUs.
362 */
Ingo Molnar95492e42007-02-16 01:27:34 -0800363__cpuinit int unsynchronized_tsc(void)
john stultz5d0cf412006-06-26 00:25:12 -0700364{
Ingo Molnar95492e42007-02-16 01:27:34 -0800365 if (!cpu_has_tsc || tsc_unstable)
366 return 1;
Andi Kleen51fc97b2008-01-30 13:32:40 +0100367
368 /* Anything with constant TSC should be synchronized */
369 if (boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
370 return 0;
371
john stultz5d0cf412006-06-26 00:25:12 -0700372 /*
373 * Intel systems are normally all synchronized.
374 * Exceptions must mark TSC as unstable:
375 */
Thomas Gleixner7e69f2b2007-02-16 01:27:42 -0800376 if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL) {
377 /* assume multi socket systems are not synchronized: */
378 if (num_possible_cpus() > 1)
379 tsc_unstable = 1;
380 }
381 return tsc_unstable;
john stultz5d0cf412006-06-26 00:25:12 -0700382}
383
Marcelo Tosatti07190a02007-02-16 01:27:44 -0800384/*
385 * Geode_LX - the OLPC CPU has a possibly a very reliable TSC
386 */
387#ifdef CONFIG_MGEODE_LX
388/* RTSC counts during suspend */
389#define RTSC_SUSP 0x100
390
391static void __init check_geode_tsc_reliable(void)
392{
Ingo Molnarf97586b2007-10-17 18:04:34 +0200393 unsigned long res_low, res_high;
Marcelo Tosatti07190a02007-02-16 01:27:44 -0800394
Ingo Molnarf97586b2007-10-17 18:04:34 +0200395 rdmsr_safe(MSR_GEODE_BUSCONT_CONF0, &res_low, &res_high);
396 if (res_low & RTSC_SUSP)
Marcelo Tosatti07190a02007-02-16 01:27:44 -0800397 clocksource_tsc.flags &= ~CLOCK_SOURCE_MUST_VERIFY;
398}
399#else
400static inline void check_geode_tsc_reliable(void) { }
401#endif
402
john stultz6bb74df2007-03-05 00:30:50 -0800403
404void __init tsc_init(void)
john stultz5d0cf412006-06-26 00:25:12 -0700405{
Guillaume Chazarain53d517c2008-01-30 13:30:06 +0100406 int cpu;
Alok Kataria3da757d2008-06-20 15:06:33 -0700407 u64 lpj;
Guillaume Chazarain53d517c2008-01-30 13:30:06 +0100408
Mikael Petterssondf17b1d2008-06-15 02:19:56 +0200409 if (!cpu_has_tsc || tsc_disabled > 0)
Rusty Russell3c2047c2008-03-04 23:07:50 +1100410 return;
john stultz5d0cf412006-06-26 00:25:12 -0700411
john stultz6bb74df2007-03-05 00:30:50 -0800412 cpu_khz = calculate_cpu_khz();
413 tsc_khz = cpu_khz;
john stultz5d0cf412006-06-26 00:25:12 -0700414
Rusty Russell3c2047c2008-03-04 23:07:50 +1100415 if (!cpu_khz) {
416 mark_tsc_unstable("could not calculate TSC khz");
417 return;
418 }
john stultz6bb74df2007-03-05 00:30:50 -0800419
Alok Kataria3da757d2008-06-20 15:06:33 -0700420 lpj = ((u64)tsc_khz * 1000);
421 do_div(lpj, HZ);
Alok Katariaf3f31492008-06-23 18:21:56 -0700422 lpj_fine = lpj;
Alok Kataria3da757d2008-06-20 15:06:33 -0700423
Mikael Petterssondf17b1d2008-06-15 02:19:56 +0200424 /* now allow native_sched_clock() to use rdtsc */
425 tsc_disabled = 0;
426
john stultz6bb74df2007-03-05 00:30:50 -0800427 printk("Detected %lu.%03lu MHz processor.\n",
428 (unsigned long)cpu_khz / 1000,
429 (unsigned long)cpu_khz % 1000);
430
Guillaume Chazarain53d517c2008-01-30 13:30:06 +0100431 /*
432 * Secondary CPUs do not run through tsc_init(), so set up
433 * all the scale factors for all CPUs, assuming the same
434 * speed as the bootup CPU. (cpufreq notifiers will fix this
435 * up if their speed diverges)
436 */
437 for_each_possible_cpu(cpu)
438 set_cyc2ns_scale(cpu_khz, cpu);
439
john stultz6bb74df2007-03-05 00:30:50 -0800440 use_tsc_delay();
441
442 /* Check and install the TSC clocksource */
443 dmi_check_system(bad_tsc_dmi_table);
444
445 unsynchronized_tsc();
446 check_geode_tsc_reliable();
Thomas Gleixner0748aca2008-05-13 10:34:20 +0200447 clocksource_tsc.mult = clocksource_khz2mult(tsc_khz,
448 clocksource_tsc.shift);
john stultz6bb74df2007-03-05 00:30:50 -0800449 /* lower the rating if we already know its unstable: */
450 if (check_tsc_unstable()) {
451 clocksource_tsc.rating = 0;
452 clocksource_tsc.flags &= ~CLOCK_SOURCE_IS_CONTINUOUS;
Thomas Gleixner9ccc9062008-05-13 12:31:00 +0200453 }
john stultz6bb74df2007-03-05 00:30:50 -0800454 clocksource_register(&clocksource_tsc);
john stultz5d0cf412006-06-26 00:25:12 -0700455}