blob: 00bb4c1c05935f60adf9a7f88b6c757428dc5d23 [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
Vivek Goyal664c0d32007-01-10 23:15:36 -080027int tsc_disable;
john stultz539eb112006-06-26 00:25:10 -070028
29#ifdef CONFIG_X86_TSC
30static int __init tsc_setup(char *str)
31{
32 printk(KERN_WARNING "notsc: Kernel compiled with CONFIG_X86_TSC, "
33 "cannot disable TSC.\n");
34 return 1;
35}
36#else
37/*
38 * disable flag for tsc. Takes effect by clearing the TSC cpu flag
39 * in cpu/common.c
40 */
41static int __init tsc_setup(char *str)
42{
43 tsc_disable = 1;
44
45 return 1;
46}
47#endif
48
49__setup("notsc", tsc_setup);
50
john stultz539eb112006-06-26 00:25:10 -070051/*
52 * code to mark and check if the TSC is unstable
53 * due to cpufreq or due to unsynced TSCs
54 */
55static int tsc_unstable;
56
Rusty Russelld7e28ff2007-07-19 01:49:23 -070057int check_tsc_unstable(void)
john stultz539eb112006-06-26 00:25:10 -070058{
59 return tsc_unstable;
60}
Rusty Russelld7e28ff2007-07-19 01:49:23 -070061EXPORT_SYMBOL_GPL(check_tsc_unstable);
john stultz539eb112006-06-26 00:25:10 -070062
Simon Arlott27b46d72007-10-20 01:13:56 +020063/* Accelerators for sched_clock()
john stultz539eb112006-06-26 00:25:10 -070064 * convert from cycles(64bits) => nanoseconds (64bits)
65 * basic equation:
66 * ns = cycles / (freq / ns_per_sec)
67 * ns = cycles * (ns_per_sec / freq)
68 * ns = cycles * (10^9 / (cpu_khz * 10^3))
69 * ns = cycles * (10^6 / cpu_khz)
70 *
71 * Then we use scaling math (suggested by george@mvista.com) to get:
72 * ns = cycles * (10^6 * SC / cpu_khz) / SC
73 * ns = cycles * cyc2ns_scale / SC
74 *
75 * And since SC is a constant power of two, we can convert the div
76 * into a shift.
77 *
Josh Triplett96315122007-10-20 02:23:49 +020078 * We can use khz divisor instead of mhz to keep a better precision, since
john stultz539eb112006-06-26 00:25:10 -070079 * cyc2ns_scale is limited to 10^6 * 2^10, which fits in 32 bits.
80 * (mathieu.desnoyers@polymtl.ca)
81 *
82 * -johnstul@us.ibm.com "math is hard, lets go shopping!"
83 */
john stultz539eb112006-06-26 00:25:10 -070084
Guillaume Chazarain53d517c2008-01-30 13:30:06 +010085DEFINE_PER_CPU(unsigned long, cyc2ns);
john stultz539eb112006-06-26 00:25:10 -070086
Guillaume Chazarain53d517c2008-01-30 13:30:06 +010087static void set_cyc2ns_scale(unsigned long cpu_khz, int cpu)
john stultz539eb112006-06-26 00:25:10 -070088{
Guillaume Chazarain53d517c2008-01-30 13:30:06 +010089 unsigned long flags, prev_scale, *scale;
90 unsigned long long tsc_now, ns_now;
91
92 local_irq_save(flags);
93 sched_clock_idle_sleep_event();
94
95 scale = &per_cpu(cyc2ns, cpu);
96
97 rdtscll(tsc_now);
98 ns_now = __cycles_2_ns(tsc_now);
99
100 prev_scale = *scale;
101 if (cpu_khz)
102 *scale = (NSEC_PER_MSEC << CYC2NS_SCALE_FACTOR)/cpu_khz;
103
104 /*
105 * Start smoothly with the new frequency:
106 */
107 sched_clock_idle_wakeup_event(0);
108 local_irq_restore(flags);
john stultz539eb112006-06-26 00:25:10 -0700109}
110
john stultz539eb112006-06-26 00:25:10 -0700111/*
112 * Scheduler clock - returns current time in nanosec units.
113 */
Jeremy Fitzhardinge688340e2007-07-17 18:37:04 -0700114unsigned long long native_sched_clock(void)
john stultz539eb112006-06-26 00:25:10 -0700115{
116 unsigned long long this_offset;
117
118 /*
Ingo Molnarf9690982007-02-13 13:26:22 +0100119 * Fall back to jiffies if there's no TSC available:
Ingo Molnarbb29ab22007-07-09 18:51:59 +0200120 * ( But note that we still use it if the TSC is marked
121 * unstable. We do this because unlike Time Of Day,
122 * the scheduler clock tolerates small errors and it's
123 * very important for it to be as fast as the platform
124 * can achive it. )
john stultz539eb112006-06-26 00:25:10 -0700125 */
Ingo Molnarbb29ab22007-07-09 18:51:59 +0200126 if (unlikely(!tsc_enabled && !tsc_unstable))
Ingo Molnarf9690982007-02-13 13:26:22 +0100127 /* No locking but a rare wrong value is not a big deal: */
john stultz539eb112006-06-26 00:25:10 -0700128 return (jiffies_64 - INITIAL_JIFFIES) * (1000000000 / HZ);
129
130 /* read the Time Stamp Counter: */
Jeremy Fitzhardinge688340e2007-07-17 18:37:04 -0700131 rdtscll(this_offset);
john stultz539eb112006-06-26 00:25:10 -0700132
133 /* return the value in ns */
134 return cycles_2_ns(this_offset);
135}
136
Jeremy Fitzhardinge688340e2007-07-17 18:37:04 -0700137/* We need to define a real function for sched_clock, to override the
138 weak default version */
139#ifdef CONFIG_PARAVIRT
140unsigned long long sched_clock(void)
141{
142 return paravirt_sched_clock();
143}
144#else
145unsigned long long sched_clock(void)
146 __attribute__((alias("native_sched_clock")));
147#endif
148
Zachary Amsden1182d852007-03-05 00:30:36 -0800149unsigned long native_calculate_cpu_khz(void)
john stultz539eb112006-06-26 00:25:10 -0700150{
151 unsigned long long start, end;
152 unsigned long count;
Dave Johnsonedaf4202007-10-23 22:37:22 +0200153 u64 delta64 = (u64)ULLONG_MAX;
john stultz539eb112006-06-26 00:25:10 -0700154 int i;
155 unsigned long flags;
156
157 local_irq_save(flags);
158
Dave Johnson8c660062007-10-23 22:37:22 +0200159 /* run 3 times to ensure the cache is warm and to get an accurate reading */
john stultz539eb112006-06-26 00:25:10 -0700160 for (i = 0; i < 3; i++) {
161 mach_prepare_counter();
162 rdtscll(start);
163 mach_countup(&count);
164 rdtscll(end);
Dave Johnson8c660062007-10-23 22:37:22 +0200165
166 /*
167 * Error: ECTCNEVERSET
168 * The CTC wasn't reliable: we got a hit on the very first read,
169 * or the CPU was so fast/slow that the quotient wouldn't fit in
170 * 32 bits..
171 */
172 if (count <= 1)
173 continue;
174
175 /* cpu freq too slow: */
176 if ((end - start) <= CALIBRATE_TIME_MSEC)
177 continue;
178
179 /*
180 * We want the minimum time of all runs in case one of them
181 * is inaccurate due to SMI or other delay
182 */
Dave Johnsonedaf4202007-10-23 22:37:22 +0200183 delta64 = min(delta64, (end - start));
john stultz539eb112006-06-26 00:25:10 -0700184 }
john stultz539eb112006-06-26 00:25:10 -0700185
Dave Johnson8c660062007-10-23 22:37:22 +0200186 /* cpu freq too fast (or every run was bad): */
john stultz539eb112006-06-26 00:25:10 -0700187 if (delta64 > (1ULL<<32))
188 goto err;
189
john stultz539eb112006-06-26 00:25:10 -0700190 delta64 += CALIBRATE_TIME_MSEC/2; /* round for do_div */
191 do_div(delta64,CALIBRATE_TIME_MSEC);
192
193 local_irq_restore(flags);
194 return (unsigned long)delta64;
195err:
196 local_irq_restore(flags);
197 return 0;
198}
199
200int recalibrate_cpu_khz(void)
201{
202#ifndef CONFIG_SMP
203 unsigned long cpu_khz_old = cpu_khz;
204
205 if (cpu_has_tsc) {
206 cpu_khz = calculate_cpu_khz();
207 tsc_khz = cpu_khz;
Mike Travis92cb7612007-10-19 20:35:04 +0200208 cpu_data(0).loops_per_jiffy =
209 cpufreq_scale(cpu_data(0).loops_per_jiffy,
john stultz539eb112006-06-26 00:25:10 -0700210 cpu_khz_old, cpu_khz);
211 return 0;
212 } else
213 return -ENODEV;
214#else
215 return -ENODEV;
216#endif
217}
218
219EXPORT_SYMBOL(recalibrate_cpu_khz);
220
john stultz539eb112006-06-26 00:25:10 -0700221#ifdef CONFIG_CPU_FREQ
222
john stultz539eb112006-06-26 00:25:10 -0700223/*
224 * if the CPU frequency is scaled, TSC-based delays will need a different
225 * loops_per_jiffy value to function properly.
226 */
227static unsigned int ref_freq = 0;
228static unsigned long loops_per_jiffy_ref = 0;
229static unsigned long cpu_khz_ref = 0;
230
231static int
232time_cpufreq_notifier(struct notifier_block *nb, unsigned long val, void *data)
233{
234 struct cpufreq_freqs *freq = data;
235
john stultz539eb112006-06-26 00:25:10 -0700236 if (!ref_freq) {
237 if (!freq->old){
238 ref_freq = freq->new;
Daniel Walkerdf3624a2007-05-02 19:27:18 +0200239 return 0;
john stultz539eb112006-06-26 00:25:10 -0700240 }
241 ref_freq = freq->old;
Mike Travis92cb7612007-10-19 20:35:04 +0200242 loops_per_jiffy_ref = cpu_data(freq->cpu).loops_per_jiffy;
john stultz539eb112006-06-26 00:25:10 -0700243 cpu_khz_ref = cpu_khz;
244 }
245
246 if ((val == CPUFREQ_PRECHANGE && freq->old < freq->new) ||
247 (val == CPUFREQ_POSTCHANGE && freq->old > freq->new) ||
248 (val == CPUFREQ_RESUMECHANGE)) {
249 if (!(freq->flags & CPUFREQ_CONST_LOOPS))
Mike Travis92cb7612007-10-19 20:35:04 +0200250 cpu_data(freq->cpu).loops_per_jiffy =
john stultz539eb112006-06-26 00:25:10 -0700251 cpufreq_scale(loops_per_jiffy_ref,
252 ref_freq, freq->new);
253
254 if (cpu_khz) {
255
256 if (num_online_cpus() == 1)
257 cpu_khz = cpufreq_scale(cpu_khz_ref,
258 ref_freq, freq->new);
259 if (!(freq->flags & CPUFREQ_CONST_LOOPS)) {
260 tsc_khz = cpu_khz;
Guillaume Chazarain53d517c2008-01-30 13:30:06 +0100261 preempt_disable();
262 set_cyc2ns_scale(cpu_khz, smp_processor_id());
263 preempt_enable();
john stultz539eb112006-06-26 00:25:10 -0700264 /*
265 * TSC based sched_clock turns
266 * to junk w/ cpufreq
267 */
john stultz5a90cf22007-05-02 19:27:08 +0200268 mark_tsc_unstable("cpufreq changes");
john stultz539eb112006-06-26 00:25:10 -0700269 }
270 }
271 }
john stultz539eb112006-06-26 00:25:10 -0700272
273 return 0;
274}
275
276static struct notifier_block time_cpufreq_notifier_block = {
277 .notifier_call = time_cpufreq_notifier
278};
279
280static int __init cpufreq_tsc(void)
281{
Thomas Gleixner26a08eb2007-02-16 01:27:32 -0800282 return cpufreq_register_notifier(&time_cpufreq_notifier_block,
283 CPUFREQ_TRANSITION_NOTIFIER);
john stultz539eb112006-06-26 00:25:10 -0700284}
john stultz539eb112006-06-26 00:25:10 -0700285core_initcall(cpufreq_tsc);
286
287#endif
john stultz5d0cf412006-06-26 00:25:12 -0700288
289/* clock source code */
290
291static unsigned long current_tsc_khz = 0;
john stultz5d0cf412006-06-26 00:25:12 -0700292
293static cycle_t read_tsc(void)
294{
295 cycle_t ret;
296
297 rdtscll(ret);
298
299 return ret;
300}
301
302static struct clocksource clocksource_tsc = {
303 .name = "tsc",
304 .rating = 300,
305 .read = read_tsc,
Jim Cromie7f9f3032006-06-26 00:25:15 -0700306 .mask = CLOCKSOURCE_MASK(64),
john stultz5d0cf412006-06-26 00:25:12 -0700307 .mult = 0, /* to be set */
308 .shift = 22,
Thomas Gleixner73b08d22007-02-16 01:27:36 -0800309 .flags = CLOCK_SOURCE_IS_CONTINUOUS |
310 CLOCK_SOURCE_MUST_VERIFY,
john stultz5d0cf412006-06-26 00:25:12 -0700311};
312
john stultz5a90cf22007-05-02 19:27:08 +0200313void mark_tsc_unstable(char *reason)
john stultz5d0cf412006-06-26 00:25:12 -0700314{
Thomas Gleixner7e69f2b2007-02-16 01:27:42 -0800315 if (!tsc_unstable) {
316 tsc_unstable = 1;
Thomas Gleixnerd9a5c0a2007-03-24 23:02:49 +0100317 tsc_enabled = 0;
john stultz5a90cf22007-05-02 19:27:08 +0200318 printk("Marking TSC unstable due to: %s.\n", reason);
Thomas Gleixner7e69f2b2007-02-16 01:27:42 -0800319 /* Can be called before registration */
320 if (clocksource_tsc.mult)
321 clocksource_change_rating(&clocksource_tsc, 0);
322 else
323 clocksource_tsc.rating = 0;
john stultz5d0cf412006-06-26 00:25:12 -0700324 }
john stultz5d0cf412006-06-26 00:25:12 -0700325}
Thomas Gleixner7e69f2b2007-02-16 01:27:42 -0800326EXPORT_SYMBOL_GPL(mark_tsc_unstable);
john stultz5d0cf412006-06-26 00:25:12 -0700327
Jeff Garzik18552562007-10-03 15:15:40 -0400328static int __init dmi_mark_tsc_unstable(const struct dmi_system_id *d)
john stultz5d0cf412006-06-26 00:25:12 -0700329{
330 printk(KERN_NOTICE "%s detected: marking TSC unstable.\n",
331 d->ident);
Thomas Gleixner7e69f2b2007-02-16 01:27:42 -0800332 tsc_unstable = 1;
john stultz5d0cf412006-06-26 00:25:12 -0700333 return 0;
334}
335
336/* List of systems that have known TSC problems */
337static struct dmi_system_id __initdata bad_tsc_dmi_table[] = {
338 {
339 .callback = dmi_mark_tsc_unstable,
340 .ident = "IBM Thinkpad 380XD",
341 .matches = {
342 DMI_MATCH(DMI_BOARD_VENDOR, "IBM"),
343 DMI_MATCH(DMI_BOARD_NAME, "2635FA0"),
344 },
345 },
346 {}
347};
348
john stultz5d0cf412006-06-26 00:25:12 -0700349/*
350 * Make an educated guess if the TSC is trustworthy and synchronized
351 * over all CPUs.
352 */
Ingo Molnar95492e42007-02-16 01:27:34 -0800353__cpuinit int unsynchronized_tsc(void)
john stultz5d0cf412006-06-26 00:25:12 -0700354{
Ingo Molnar95492e42007-02-16 01:27:34 -0800355 if (!cpu_has_tsc || tsc_unstable)
356 return 1;
john stultz5d0cf412006-06-26 00:25:12 -0700357 /*
358 * Intel systems are normally all synchronized.
359 * Exceptions must mark TSC as unstable:
360 */
Thomas Gleixner7e69f2b2007-02-16 01:27:42 -0800361 if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL) {
362 /* assume multi socket systems are not synchronized: */
363 if (num_possible_cpus() > 1)
364 tsc_unstable = 1;
365 }
366 return tsc_unstable;
john stultz5d0cf412006-06-26 00:25:12 -0700367}
368
Marcelo Tosatti07190a02007-02-16 01:27:44 -0800369/*
370 * Geode_LX - the OLPC CPU has a possibly a very reliable TSC
371 */
372#ifdef CONFIG_MGEODE_LX
373/* RTSC counts during suspend */
374#define RTSC_SUSP 0x100
375
376static void __init check_geode_tsc_reliable(void)
377{
Ingo Molnarf97586b2007-10-17 18:04:34 +0200378 unsigned long res_low, res_high;
Marcelo Tosatti07190a02007-02-16 01:27:44 -0800379
Ingo Molnarf97586b2007-10-17 18:04:34 +0200380 rdmsr_safe(MSR_GEODE_BUSCONT_CONF0, &res_low, &res_high);
381 if (res_low & RTSC_SUSP)
Marcelo Tosatti07190a02007-02-16 01:27:44 -0800382 clocksource_tsc.flags &= ~CLOCK_SOURCE_MUST_VERIFY;
383}
384#else
385static inline void check_geode_tsc_reliable(void) { }
386#endif
387
john stultz6bb74df2007-03-05 00:30:50 -0800388
389void __init tsc_init(void)
john stultz5d0cf412006-06-26 00:25:12 -0700390{
Guillaume Chazarain53d517c2008-01-30 13:30:06 +0100391 int cpu;
392
john stultz6bb74df2007-03-05 00:30:50 -0800393 if (!cpu_has_tsc || tsc_disable)
394 goto out_no_tsc;
john stultz5d0cf412006-06-26 00:25:12 -0700395
john stultz6bb74df2007-03-05 00:30:50 -0800396 cpu_khz = calculate_cpu_khz();
397 tsc_khz = cpu_khz;
john stultz5d0cf412006-06-26 00:25:12 -0700398
john stultz6bb74df2007-03-05 00:30:50 -0800399 if (!cpu_khz)
400 goto out_no_tsc;
401
402 printk("Detected %lu.%03lu MHz processor.\n",
403 (unsigned long)cpu_khz / 1000,
404 (unsigned long)cpu_khz % 1000);
405
Guillaume Chazarain53d517c2008-01-30 13:30:06 +0100406 /*
407 * Secondary CPUs do not run through tsc_init(), so set up
408 * all the scale factors for all CPUs, assuming the same
409 * speed as the bootup CPU. (cpufreq notifiers will fix this
410 * up if their speed diverges)
411 */
412 for_each_possible_cpu(cpu)
413 set_cyc2ns_scale(cpu_khz, cpu);
414
john stultz6bb74df2007-03-05 00:30:50 -0800415 use_tsc_delay();
416
417 /* Check and install the TSC clocksource */
418 dmi_check_system(bad_tsc_dmi_table);
419
420 unsynchronized_tsc();
421 check_geode_tsc_reliable();
422 current_tsc_khz = tsc_khz;
423 clocksource_tsc.mult = clocksource_khz2mult(current_tsc_khz,
john stultz5d0cf412006-06-26 00:25:12 -0700424 clocksource_tsc.shift);
john stultz6bb74df2007-03-05 00:30:50 -0800425 /* lower the rating if we already know its unstable: */
426 if (check_tsc_unstable()) {
427 clocksource_tsc.rating = 0;
428 clocksource_tsc.flags &= ~CLOCK_SOURCE_IS_CONTINUOUS;
Thomas Gleixnerd9a5c0a2007-03-24 23:02:49 +0100429 } else
430 tsc_enabled = 1;
431
john stultz6bb74df2007-03-05 00:30:50 -0800432 clocksource_register(&clocksource_tsc);
john stultz5d0cf412006-06-26 00:25:12 -0700433
john stultz6bb74df2007-03-05 00:30:50 -0800434 return;
435
436out_no_tsc:
437 /*
438 * Set the tsc_disable flag if there's no TSC support, this
439 * makes it a fast flag for the kernel to see whether it
440 * should be using the TSC.
441 */
442 tsc_disable = 1;
john stultz5d0cf412006-06-26 00:25:12 -0700443}