blob: 46f752a8bbf3270a8d34bc3564fdf641e07bf8ea [file] [log] [blame]
john stultz539eb112006-06-26 00:25:10 -07001/*
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.
5 */
6
john stultz5d0cf412006-06-26 00:25:12 -07007#include <linux/clocksource.h>
john stultz539eb112006-06-26 00:25:10 -07008#include <linux/workqueue.h>
9#include <linux/cpufreq.h>
10#include <linux/jiffies.h>
11#include <linux/init.h>
john stultz5d0cf412006-06-26 00:25:12 -070012#include <linux/dmi.h>
john stultz539eb112006-06-26 00:25:10 -070013
john stultz5d0cf412006-06-26 00:25:12 -070014#include <asm/delay.h>
john stultz539eb112006-06-26 00:25:10 -070015#include <asm/tsc.h>
16#include <asm/io.h>
17
18#include "mach_timer.h"
19
20/*
21 * On some systems the TSC frequency does not
22 * change with the cpu frequency. So we need
23 * an extra value to store the TSC freq
24 */
25unsigned int tsc_khz;
Zachary Amsdenbbab4f32007-02-13 13:26:21 +010026unsigned long long (*custom_sched_clock)(void);
john stultz539eb112006-06-26 00:25:10 -070027
Vivek Goyal664c0d32007-01-10 23:15:36 -080028int tsc_disable;
john stultz539eb112006-06-26 00:25:10 -070029
30#ifdef CONFIG_X86_TSC
31static int __init tsc_setup(char *str)
32{
33 printk(KERN_WARNING "notsc: Kernel compiled with CONFIG_X86_TSC, "
34 "cannot disable TSC.\n");
35 return 1;
36}
37#else
38/*
39 * disable flag for tsc. Takes effect by clearing the TSC cpu flag
40 * in cpu/common.c
41 */
42static int __init tsc_setup(char *str)
43{
44 tsc_disable = 1;
45
46 return 1;
47}
48#endif
49
50__setup("notsc", tsc_setup);
51
john stultz539eb112006-06-26 00:25:10 -070052/*
53 * code to mark and check if the TSC is unstable
54 * due to cpufreq or due to unsynced TSCs
55 */
56static int tsc_unstable;
57
58static inline int check_tsc_unstable(void)
59{
60 return tsc_unstable;
61}
62
63void mark_tsc_unstable(void)
64{
65 tsc_unstable = 1;
66}
67EXPORT_SYMBOL_GPL(mark_tsc_unstable);
68
69/* Accellerators for sched_clock()
70 * convert from cycles(64bits) => nanoseconds (64bits)
71 * basic equation:
72 * ns = cycles / (freq / ns_per_sec)
73 * ns = cycles * (ns_per_sec / freq)
74 * ns = cycles * (10^9 / (cpu_khz * 10^3))
75 * ns = cycles * (10^6 / cpu_khz)
76 *
77 * Then we use scaling math (suggested by george@mvista.com) to get:
78 * ns = cycles * (10^6 * SC / cpu_khz) / SC
79 * ns = cycles * cyc2ns_scale / SC
80 *
81 * And since SC is a constant power of two, we can convert the div
82 * into a shift.
83 *
84 * We can use khz divisor instead of mhz to keep a better percision, since
85 * cyc2ns_scale is limited to 10^6 * 2^10, which fits in 32 bits.
86 * (mathieu.desnoyers@polymtl.ca)
87 *
88 * -johnstul@us.ibm.com "math is hard, lets go shopping!"
89 */
90static unsigned long cyc2ns_scale __read_mostly;
91
92#define CYC2NS_SCALE_FACTOR 10 /* 2^10, carefully chosen */
93
94static inline void set_cyc2ns_scale(unsigned long cpu_khz)
95{
96 cyc2ns_scale = (1000000 << CYC2NS_SCALE_FACTOR)/cpu_khz;
97}
98
99static inline unsigned long long cycles_2_ns(unsigned long long cyc)
100{
101 return (cyc * cyc2ns_scale) >> CYC2NS_SCALE_FACTOR;
102}
103
104/*
105 * Scheduler clock - returns current time in nanosec units.
106 */
107unsigned long long sched_clock(void)
108{
109 unsigned long long this_offset;
110
Zachary Amsdenbbab4f32007-02-13 13:26:21 +0100111 if (unlikely(custom_sched_clock))
112 return (*custom_sched_clock)();
113
john stultz539eb112006-06-26 00:25:10 -0700114 /*
Ingo Molnarf9690982007-02-13 13:26:22 +0100115 * Fall back to jiffies if there's no TSC available:
john stultz539eb112006-06-26 00:25:10 -0700116 */
Ingo Molnarf9690982007-02-13 13:26:22 +0100117 if (unlikely(tsc_disable))
118 /* No locking but a rare wrong value is not a big deal: */
john stultz539eb112006-06-26 00:25:10 -0700119 return (jiffies_64 - INITIAL_JIFFIES) * (1000000000 / HZ);
120
121 /* read the Time Stamp Counter: */
122 rdtscll(this_offset);
123
124 /* return the value in ns */
125 return cycles_2_ns(this_offset);
126}
127
128static unsigned long calculate_cpu_khz(void)
129{
130 unsigned long long start, end;
131 unsigned long count;
132 u64 delta64;
133 int i;
134 unsigned long flags;
135
136 local_irq_save(flags);
137
138 /* run 3 times to ensure the cache is warm */
139 for (i = 0; i < 3; i++) {
140 mach_prepare_counter();
141 rdtscll(start);
142 mach_countup(&count);
143 rdtscll(end);
144 }
145 /*
146 * Error: ECTCNEVERSET
147 * The CTC wasn't reliable: we got a hit on the very first read,
148 * or the CPU was so fast/slow that the quotient wouldn't fit in
149 * 32 bits..
150 */
151 if (count <= 1)
152 goto err;
153
154 delta64 = end - start;
155
156 /* cpu freq too fast: */
157 if (delta64 > (1ULL<<32))
158 goto err;
159
160 /* cpu freq too slow: */
161 if (delta64 <= CALIBRATE_TIME_MSEC)
162 goto err;
163
164 delta64 += CALIBRATE_TIME_MSEC/2; /* round for do_div */
165 do_div(delta64,CALIBRATE_TIME_MSEC);
166
167 local_irq_restore(flags);
168 return (unsigned long)delta64;
169err:
170 local_irq_restore(flags);
171 return 0;
172}
173
174int recalibrate_cpu_khz(void)
175{
176#ifndef CONFIG_SMP
177 unsigned long cpu_khz_old = cpu_khz;
178
179 if (cpu_has_tsc) {
180 cpu_khz = calculate_cpu_khz();
181 tsc_khz = cpu_khz;
182 cpu_data[0].loops_per_jiffy =
183 cpufreq_scale(cpu_data[0].loops_per_jiffy,
184 cpu_khz_old, cpu_khz);
185 return 0;
186 } else
187 return -ENODEV;
188#else
189 return -ENODEV;
190#endif
191}
192
193EXPORT_SYMBOL(recalibrate_cpu_khz);
194
Magnus Dammc0d83742006-09-26 10:52:35 +0200195void __init tsc_init(void)
john stultz539eb112006-06-26 00:25:10 -0700196{
197 if (!cpu_has_tsc || tsc_disable)
Ingo Molnarf9690982007-02-13 13:26:22 +0100198 goto out_no_tsc;
john stultz539eb112006-06-26 00:25:10 -0700199
200 cpu_khz = calculate_cpu_khz();
201 tsc_khz = cpu_khz;
202
203 if (!cpu_khz)
Ingo Molnarf9690982007-02-13 13:26:22 +0100204 goto out_no_tsc;
john stultz539eb112006-06-26 00:25:10 -0700205
206 printk("Detected %lu.%03lu MHz processor.\n",
207 (unsigned long)cpu_khz / 1000,
208 (unsigned long)cpu_khz % 1000);
209
210 set_cyc2ns_scale(cpu_khz);
john stultz6f84fa22006-06-26 00:25:11 -0700211 use_tsc_delay();
Ingo Molnarf9690982007-02-13 13:26:22 +0100212 return;
213
214out_no_tsc:
215 /*
216 * Set the tsc_disable flag if there's no TSC support, this
217 * makes it a fast flag for the kernel to see whether it
218 * should be using the TSC.
219 */
220 tsc_disable = 1;
john stultz539eb112006-06-26 00:25:10 -0700221}
222
223#ifdef CONFIG_CPU_FREQ
224
225static unsigned int cpufreq_delayed_issched = 0;
226static unsigned int cpufreq_init = 0;
227static struct work_struct cpufreq_delayed_get_work;
228
David Howellsc4028952006-11-22 14:57:56 +0000229static void handle_cpufreq_delayed_get(struct work_struct *work)
john stultz539eb112006-06-26 00:25:10 -0700230{
231 unsigned int cpu;
232
233 for_each_online_cpu(cpu)
234 cpufreq_get(cpu);
235
236 cpufreq_delayed_issched = 0;
237}
238
239/*
240 * if we notice cpufreq oddness, schedule a call to cpufreq_get() as it tries
241 * to verify the CPU frequency the timing core thinks the CPU is running
242 * at is still correct.
243 */
244static inline void cpufreq_delayed_get(void)
245{
246 if (cpufreq_init && !cpufreq_delayed_issched) {
247 cpufreq_delayed_issched = 1;
248 printk(KERN_DEBUG "Checking if CPU frequency changed.\n");
249 schedule_work(&cpufreq_delayed_get_work);
250 }
251}
252
253/*
254 * if the CPU frequency is scaled, TSC-based delays will need a different
255 * loops_per_jiffy value to function properly.
256 */
257static unsigned int ref_freq = 0;
258static unsigned long loops_per_jiffy_ref = 0;
259static unsigned long cpu_khz_ref = 0;
260
261static int
262time_cpufreq_notifier(struct notifier_block *nb, unsigned long val, void *data)
263{
264 struct cpufreq_freqs *freq = data;
265
266 if (val != CPUFREQ_RESUMECHANGE && val != CPUFREQ_SUSPENDCHANGE)
267 write_seqlock_irq(&xtime_lock);
268
269 if (!ref_freq) {
270 if (!freq->old){
271 ref_freq = freq->new;
272 goto end;
273 }
274 ref_freq = freq->old;
275 loops_per_jiffy_ref = cpu_data[freq->cpu].loops_per_jiffy;
276 cpu_khz_ref = cpu_khz;
277 }
278
279 if ((val == CPUFREQ_PRECHANGE && freq->old < freq->new) ||
280 (val == CPUFREQ_POSTCHANGE && freq->old > freq->new) ||
281 (val == CPUFREQ_RESUMECHANGE)) {
282 if (!(freq->flags & CPUFREQ_CONST_LOOPS))
283 cpu_data[freq->cpu].loops_per_jiffy =
284 cpufreq_scale(loops_per_jiffy_ref,
285 ref_freq, freq->new);
286
287 if (cpu_khz) {
288
289 if (num_online_cpus() == 1)
290 cpu_khz = cpufreq_scale(cpu_khz_ref,
291 ref_freq, freq->new);
292 if (!(freq->flags & CPUFREQ_CONST_LOOPS)) {
293 tsc_khz = cpu_khz;
294 set_cyc2ns_scale(cpu_khz);
295 /*
296 * TSC based sched_clock turns
297 * to junk w/ cpufreq
298 */
299 mark_tsc_unstable();
300 }
301 }
302 }
303end:
304 if (val != CPUFREQ_RESUMECHANGE && val != CPUFREQ_SUSPENDCHANGE)
305 write_sequnlock_irq(&xtime_lock);
306
307 return 0;
308}
309
310static struct notifier_block time_cpufreq_notifier_block = {
311 .notifier_call = time_cpufreq_notifier
312};
313
314static int __init cpufreq_tsc(void)
315{
316 int ret;
317
David Howellsc4028952006-11-22 14:57:56 +0000318 INIT_WORK(&cpufreq_delayed_get_work, handle_cpufreq_delayed_get);
john stultz539eb112006-06-26 00:25:10 -0700319 ret = cpufreq_register_notifier(&time_cpufreq_notifier_block,
320 CPUFREQ_TRANSITION_NOTIFIER);
321 if (!ret)
322 cpufreq_init = 1;
323
324 return ret;
325}
326
327core_initcall(cpufreq_tsc);
328
329#endif
john stultz5d0cf412006-06-26 00:25:12 -0700330
331/* clock source code */
332
333static unsigned long current_tsc_khz = 0;
334static int tsc_update_callback(void);
335
336static cycle_t read_tsc(void)
337{
338 cycle_t ret;
339
340 rdtscll(ret);
341
342 return ret;
343}
344
345static struct clocksource clocksource_tsc = {
346 .name = "tsc",
347 .rating = 300,
348 .read = read_tsc,
Jim Cromie7f9f3032006-06-26 00:25:15 -0700349 .mask = CLOCKSOURCE_MASK(64),
john stultz5d0cf412006-06-26 00:25:12 -0700350 .mult = 0, /* to be set */
351 .shift = 22,
352 .update_callback = tsc_update_callback,
353 .is_continuous = 1,
354};
355
356static int tsc_update_callback(void)
357{
358 int change = 0;
359
360 /* check to see if we should switch to the safe clocksource: */
john stultz3f4a0b92006-10-17 00:09:32 -0700361 if (clocksource_tsc.rating != 0 && check_tsc_unstable()) {
362 clocksource_tsc.rating = 0;
john stultza2752542006-06-26 00:25:14 -0700363 clocksource_reselect();
john stultz5d0cf412006-06-26 00:25:12 -0700364 change = 1;
365 }
366
367 /* only update if tsc_khz has changed: */
368 if (current_tsc_khz != tsc_khz) {
369 current_tsc_khz = tsc_khz;
370 clocksource_tsc.mult = clocksource_khz2mult(current_tsc_khz,
371 clocksource_tsc.shift);
372 change = 1;
373 }
374
375 return change;
376}
377
378static int __init dmi_mark_tsc_unstable(struct dmi_system_id *d)
379{
380 printk(KERN_NOTICE "%s detected: marking TSC unstable.\n",
381 d->ident);
382 mark_tsc_unstable();
383 return 0;
384}
385
386/* List of systems that have known TSC problems */
387static struct dmi_system_id __initdata bad_tsc_dmi_table[] = {
388 {
389 .callback = dmi_mark_tsc_unstable,
390 .ident = "IBM Thinkpad 380XD",
391 .matches = {
392 DMI_MATCH(DMI_BOARD_VENDOR, "IBM"),
393 DMI_MATCH(DMI_BOARD_NAME, "2635FA0"),
394 },
395 },
396 {}
397};
398
399#define TSC_FREQ_CHECK_INTERVAL (10*MSEC_PER_SEC) /* 10sec in MS */
400static struct timer_list verify_tsc_freq_timer;
401
402/* XXX - Probably should add locking */
403static void verify_tsc_freq(unsigned long unused)
404{
405 static u64 last_tsc;
406 static unsigned long last_jiffies;
407
408 u64 now_tsc, interval_tsc;
409 unsigned long now_jiffies, interval_jiffies;
410
411
412 if (check_tsc_unstable())
413 return;
414
415 rdtscll(now_tsc);
416 now_jiffies = jiffies;
417
418 if (!last_jiffies) {
419 goto out;
420 }
421
422 interval_jiffies = now_jiffies - last_jiffies;
423 interval_tsc = now_tsc - last_tsc;
424 interval_tsc *= HZ;
425 do_div(interval_tsc, cpu_khz*1000);
426
427 if (interval_tsc < (interval_jiffies * 3 / 4)) {
428 printk("TSC appears to be running slowly. "
429 "Marking it as unstable\n");
430 mark_tsc_unstable();
431 return;
432 }
433
434out:
435 last_tsc = now_tsc;
436 last_jiffies = now_jiffies;
437 /* set us up to go off on the next interval: */
438 mod_timer(&verify_tsc_freq_timer,
439 jiffies + msecs_to_jiffies(TSC_FREQ_CHECK_INTERVAL));
440}
441
442/*
443 * Make an educated guess if the TSC is trustworthy and synchronized
444 * over all CPUs.
445 */
446static __init int unsynchronized_tsc(void)
447{
448 /*
449 * Intel systems are normally all synchronized.
450 * Exceptions must mark TSC as unstable:
451 */
452 if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
453 return 0;
454
455 /* assume multi socket systems are not synchronized: */
456 return num_possible_cpus() > 1;
457}
458
459static int __init init_tsc_clocksource(void)
460{
461
462 if (cpu_has_tsc && tsc_khz && !tsc_disable) {
463 /* check blacklist */
464 dmi_check_system(bad_tsc_dmi_table);
465
466 if (unsynchronized_tsc()) /* mark unstable if unsynced */
467 mark_tsc_unstable();
468 current_tsc_khz = tsc_khz;
469 clocksource_tsc.mult = clocksource_khz2mult(current_tsc_khz,
470 clocksource_tsc.shift);
471 /* lower the rating if we already know its unstable: */
472 if (check_tsc_unstable())
john stultz3f4a0b92006-10-17 00:09:32 -0700473 clocksource_tsc.rating = 0;
john stultz5d0cf412006-06-26 00:25:12 -0700474
475 init_timer(&verify_tsc_freq_timer);
476 verify_tsc_freq_timer.function = verify_tsc_freq;
477 verify_tsc_freq_timer.expires =
478 jiffies + msecs_to_jiffies(TSC_FREQ_CHECK_INTERVAL);
479 add_timer(&verify_tsc_freq_timer);
480
john stultza2752542006-06-26 00:25:14 -0700481 return clocksource_register(&clocksource_tsc);
john stultz5d0cf412006-06-26 00:25:12 -0700482 }
483
484 return 0;
485}
486
487module_init(init_tsc_clocksource);