blob: 755209dc93e1beba498e176c0483eb04d4cc4628 [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>
Zachary Amsden6cb9a832007-03-05 00:30:35 -080017#include <asm/timer.h>
john stultz539eb112006-06-26 00:25:10 -070018
19#include "mach_timer.h"
20
Thomas Gleixnerd9a5c0a2007-03-24 23:02:49 +010021static int tsc_enabled;
22
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;
29
Vivek Goyal664c0d32007-01-10 23:15:36 -080030int tsc_disable;
john stultz539eb112006-06-26 00:25:10 -070031
32#ifdef CONFIG_X86_TSC
33static int __init tsc_setup(char *str)
34{
35 printk(KERN_WARNING "notsc: Kernel compiled with CONFIG_X86_TSC, "
36 "cannot disable TSC.\n");
37 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{
46 tsc_disable = 1;
47
48 return 1;
49}
50#endif
51
52__setup("notsc", tsc_setup);
53
john stultz539eb112006-06-26 00:25:10 -070054/*
55 * code to mark and check if the TSC is unstable
56 * due to cpufreq or due to unsynced TSCs
57 */
58static int tsc_unstable;
59
60static inline int check_tsc_unstable(void)
61{
62 return tsc_unstable;
63}
64
john stultz539eb112006-06-26 00:25:10 -070065/* Accellerators for sched_clock()
66 * 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 *
80 * We can use khz divisor instead of mhz to keep a better percision, since
81 * 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 */
86static unsigned long cyc2ns_scale __read_mostly;
87
88#define CYC2NS_SCALE_FACTOR 10 /* 2^10, carefully chosen */
89
90static inline void set_cyc2ns_scale(unsigned long cpu_khz)
91{
92 cyc2ns_scale = (1000000 << CYC2NS_SCALE_FACTOR)/cpu_khz;
93}
94
95static inline unsigned long long cycles_2_ns(unsigned long long cyc)
96{
97 return (cyc * cyc2ns_scale) >> CYC2NS_SCALE_FACTOR;
98}
99
100/*
101 * Scheduler clock - returns current time in nanosec units.
102 */
103unsigned long long sched_clock(void)
104{
105 unsigned long long this_offset;
106
107 /*
Ingo Molnarf9690982007-02-13 13:26:22 +0100108 * Fall back to jiffies if there's no TSC available:
john stultz539eb112006-06-26 00:25:10 -0700109 */
Thomas Gleixnerd9a5c0a2007-03-24 23:02:49 +0100110 if (unlikely(!tsc_enabled))
Ingo Molnarf9690982007-02-13 13:26:22 +0100111 /* No locking but a rare wrong value is not a big deal: */
john stultz539eb112006-06-26 00:25:10 -0700112 return (jiffies_64 - INITIAL_JIFFIES) * (1000000000 / HZ);
113
114 /* read the Time Stamp Counter: */
Zachary Amsden6cb9a832007-03-05 00:30:35 -0800115 get_scheduled_cycles(this_offset);
john stultz539eb112006-06-26 00:25:10 -0700116
117 /* return the value in ns */
118 return cycles_2_ns(this_offset);
119}
120
Zachary Amsden1182d852007-03-05 00:30:36 -0800121unsigned long native_calculate_cpu_khz(void)
john stultz539eb112006-06-26 00:25:10 -0700122{
123 unsigned long long start, end;
124 unsigned long count;
125 u64 delta64;
126 int i;
127 unsigned long flags;
128
129 local_irq_save(flags);
130
131 /* run 3 times to ensure the cache is warm */
132 for (i = 0; i < 3; i++) {
133 mach_prepare_counter();
134 rdtscll(start);
135 mach_countup(&count);
136 rdtscll(end);
137 }
138 /*
139 * Error: ECTCNEVERSET
140 * The CTC wasn't reliable: we got a hit on the very first read,
141 * or the CPU was so fast/slow that the quotient wouldn't fit in
142 * 32 bits..
143 */
144 if (count <= 1)
145 goto err;
146
147 delta64 = end - start;
148
149 /* cpu freq too fast: */
150 if (delta64 > (1ULL<<32))
151 goto err;
152
153 /* cpu freq too slow: */
154 if (delta64 <= CALIBRATE_TIME_MSEC)
155 goto err;
156
157 delta64 += CALIBRATE_TIME_MSEC/2; /* round for do_div */
158 do_div(delta64,CALIBRATE_TIME_MSEC);
159
160 local_irq_restore(flags);
161 return (unsigned long)delta64;
162err:
163 local_irq_restore(flags);
164 return 0;
165}
166
167int recalibrate_cpu_khz(void)
168{
169#ifndef CONFIG_SMP
170 unsigned long cpu_khz_old = cpu_khz;
171
172 if (cpu_has_tsc) {
173 cpu_khz = calculate_cpu_khz();
174 tsc_khz = cpu_khz;
175 cpu_data[0].loops_per_jiffy =
176 cpufreq_scale(cpu_data[0].loops_per_jiffy,
177 cpu_khz_old, cpu_khz);
178 return 0;
179 } else
180 return -ENODEV;
181#else
182 return -ENODEV;
183#endif
184}
185
186EXPORT_SYMBOL(recalibrate_cpu_khz);
187
john stultz539eb112006-06-26 00:25:10 -0700188#ifdef CONFIG_CPU_FREQ
189
john stultz539eb112006-06-26 00:25:10 -0700190/*
191 * if the CPU frequency is scaled, TSC-based delays will need a different
192 * loops_per_jiffy value to function properly.
193 */
194static unsigned int ref_freq = 0;
195static unsigned long loops_per_jiffy_ref = 0;
196static unsigned long cpu_khz_ref = 0;
197
198static int
199time_cpufreq_notifier(struct notifier_block *nb, unsigned long val, void *data)
200{
201 struct cpufreq_freqs *freq = data;
202
203 if (val != CPUFREQ_RESUMECHANGE && val != CPUFREQ_SUSPENDCHANGE)
204 write_seqlock_irq(&xtime_lock);
205
206 if (!ref_freq) {
207 if (!freq->old){
208 ref_freq = freq->new;
209 goto end;
210 }
211 ref_freq = freq->old;
212 loops_per_jiffy_ref = cpu_data[freq->cpu].loops_per_jiffy;
213 cpu_khz_ref = cpu_khz;
214 }
215
216 if ((val == CPUFREQ_PRECHANGE && freq->old < freq->new) ||
217 (val == CPUFREQ_POSTCHANGE && freq->old > freq->new) ||
218 (val == CPUFREQ_RESUMECHANGE)) {
219 if (!(freq->flags & CPUFREQ_CONST_LOOPS))
220 cpu_data[freq->cpu].loops_per_jiffy =
221 cpufreq_scale(loops_per_jiffy_ref,
222 ref_freq, freq->new);
223
224 if (cpu_khz) {
225
226 if (num_online_cpus() == 1)
227 cpu_khz = cpufreq_scale(cpu_khz_ref,
228 ref_freq, freq->new);
229 if (!(freq->flags & CPUFREQ_CONST_LOOPS)) {
230 tsc_khz = cpu_khz;
231 set_cyc2ns_scale(cpu_khz);
232 /*
233 * TSC based sched_clock turns
234 * to junk w/ cpufreq
235 */
john stultz5a90cf22007-05-02 19:27:08 +0200236 mark_tsc_unstable("cpufreq changes");
john stultz539eb112006-06-26 00:25:10 -0700237 }
238 }
239 }
240end:
241 if (val != CPUFREQ_RESUMECHANGE && val != CPUFREQ_SUSPENDCHANGE)
242 write_sequnlock_irq(&xtime_lock);
243
244 return 0;
245}
246
247static struct notifier_block time_cpufreq_notifier_block = {
248 .notifier_call = time_cpufreq_notifier
249};
250
251static int __init cpufreq_tsc(void)
252{
Thomas Gleixner26a08eb2007-02-16 01:27:32 -0800253 return cpufreq_register_notifier(&time_cpufreq_notifier_block,
254 CPUFREQ_TRANSITION_NOTIFIER);
john stultz539eb112006-06-26 00:25:10 -0700255}
john stultz539eb112006-06-26 00:25:10 -0700256core_initcall(cpufreq_tsc);
257
258#endif
john stultz5d0cf412006-06-26 00:25:12 -0700259
260/* clock source code */
261
262static unsigned long current_tsc_khz = 0;
john stultz5d0cf412006-06-26 00:25:12 -0700263
264static cycle_t read_tsc(void)
265{
266 cycle_t ret;
267
268 rdtscll(ret);
269
270 return ret;
271}
272
273static struct clocksource clocksource_tsc = {
274 .name = "tsc",
275 .rating = 300,
276 .read = read_tsc,
Jim Cromie7f9f3032006-06-26 00:25:15 -0700277 .mask = CLOCKSOURCE_MASK(64),
john stultz5d0cf412006-06-26 00:25:12 -0700278 .mult = 0, /* to be set */
279 .shift = 22,
Thomas Gleixner73b08d22007-02-16 01:27:36 -0800280 .flags = CLOCK_SOURCE_IS_CONTINUOUS |
281 CLOCK_SOURCE_MUST_VERIFY,
john stultz5d0cf412006-06-26 00:25:12 -0700282};
283
john stultz5a90cf22007-05-02 19:27:08 +0200284void mark_tsc_unstable(char *reason)
john stultz5d0cf412006-06-26 00:25:12 -0700285{
Thomas Gleixner7e69f2b2007-02-16 01:27:42 -0800286 if (!tsc_unstable) {
287 tsc_unstable = 1;
Thomas Gleixnerd9a5c0a2007-03-24 23:02:49 +0100288 tsc_enabled = 0;
john stultz5a90cf22007-05-02 19:27:08 +0200289 printk("Marking TSC unstable due to: %s.\n", reason);
Thomas Gleixner7e69f2b2007-02-16 01:27:42 -0800290 /* Can be called before registration */
291 if (clocksource_tsc.mult)
292 clocksource_change_rating(&clocksource_tsc, 0);
293 else
294 clocksource_tsc.rating = 0;
john stultz5d0cf412006-06-26 00:25:12 -0700295 }
john stultz5d0cf412006-06-26 00:25:12 -0700296}
Thomas Gleixner7e69f2b2007-02-16 01:27:42 -0800297EXPORT_SYMBOL_GPL(mark_tsc_unstable);
john stultz5d0cf412006-06-26 00:25:12 -0700298
299static int __init dmi_mark_tsc_unstable(struct dmi_system_id *d)
300{
301 printk(KERN_NOTICE "%s detected: marking TSC unstable.\n",
302 d->ident);
Thomas Gleixner7e69f2b2007-02-16 01:27:42 -0800303 tsc_unstable = 1;
john stultz5d0cf412006-06-26 00:25:12 -0700304 return 0;
305}
306
307/* List of systems that have known TSC problems */
308static struct dmi_system_id __initdata bad_tsc_dmi_table[] = {
309 {
310 .callback = dmi_mark_tsc_unstable,
311 .ident = "IBM Thinkpad 380XD",
312 .matches = {
313 DMI_MATCH(DMI_BOARD_VENDOR, "IBM"),
314 DMI_MATCH(DMI_BOARD_NAME, "2635FA0"),
315 },
316 },
317 {}
318};
319
john stultz5d0cf412006-06-26 00:25:12 -0700320/*
321 * Make an educated guess if the TSC is trustworthy and synchronized
322 * over all CPUs.
323 */
Ingo Molnar95492e42007-02-16 01:27:34 -0800324__cpuinit int unsynchronized_tsc(void)
john stultz5d0cf412006-06-26 00:25:12 -0700325{
Ingo Molnar95492e42007-02-16 01:27:34 -0800326 if (!cpu_has_tsc || tsc_unstable)
327 return 1;
john stultz5d0cf412006-06-26 00:25:12 -0700328 /*
329 * Intel systems are normally all synchronized.
330 * Exceptions must mark TSC as unstable:
331 */
Thomas Gleixner7e69f2b2007-02-16 01:27:42 -0800332 if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL) {
333 /* assume multi socket systems are not synchronized: */
334 if (num_possible_cpus() > 1)
335 tsc_unstable = 1;
336 }
337 return tsc_unstable;
john stultz5d0cf412006-06-26 00:25:12 -0700338}
339
Marcelo Tosatti07190a02007-02-16 01:27:44 -0800340/*
341 * Geode_LX - the OLPC CPU has a possibly a very reliable TSC
342 */
343#ifdef CONFIG_MGEODE_LX
344/* RTSC counts during suspend */
345#define RTSC_SUSP 0x100
346
347static void __init check_geode_tsc_reliable(void)
348{
349 unsigned long val;
350
351 rdmsrl(MSR_GEODE_BUSCONT_CONF0, val);
352 if ((val & RTSC_SUSP))
353 clocksource_tsc.flags &= ~CLOCK_SOURCE_MUST_VERIFY;
354}
355#else
356static inline void check_geode_tsc_reliable(void) { }
357#endif
358
john stultz6bb74df2007-03-05 00:30:50 -0800359
360void __init tsc_init(void)
john stultz5d0cf412006-06-26 00:25:12 -0700361{
john stultz6bb74df2007-03-05 00:30:50 -0800362 if (!cpu_has_tsc || tsc_disable)
363 goto out_no_tsc;
john stultz5d0cf412006-06-26 00:25:12 -0700364
john stultz6bb74df2007-03-05 00:30:50 -0800365 cpu_khz = calculate_cpu_khz();
366 tsc_khz = cpu_khz;
john stultz5d0cf412006-06-26 00:25:12 -0700367
john stultz6bb74df2007-03-05 00:30:50 -0800368 if (!cpu_khz)
369 goto out_no_tsc;
370
371 printk("Detected %lu.%03lu MHz processor.\n",
372 (unsigned long)cpu_khz / 1000,
373 (unsigned long)cpu_khz % 1000);
374
375 set_cyc2ns_scale(cpu_khz);
376 use_tsc_delay();
377
378 /* Check and install the TSC clocksource */
379 dmi_check_system(bad_tsc_dmi_table);
380
381 unsynchronized_tsc();
382 check_geode_tsc_reliable();
383 current_tsc_khz = tsc_khz;
384 clocksource_tsc.mult = clocksource_khz2mult(current_tsc_khz,
john stultz5d0cf412006-06-26 00:25:12 -0700385 clocksource_tsc.shift);
john stultz6bb74df2007-03-05 00:30:50 -0800386 /* lower the rating if we already know its unstable: */
387 if (check_tsc_unstable()) {
388 clocksource_tsc.rating = 0;
389 clocksource_tsc.flags &= ~CLOCK_SOURCE_IS_CONTINUOUS;
Thomas Gleixnerd9a5c0a2007-03-24 23:02:49 +0100390 } else
391 tsc_enabled = 1;
392
john stultz6bb74df2007-03-05 00:30:50 -0800393 clocksource_register(&clocksource_tsc);
john stultz5d0cf412006-06-26 00:25:12 -0700394
john stultz6bb74df2007-03-05 00:30:50 -0800395 return;
396
397out_no_tsc:
398 /*
399 * Set the tsc_disable flag if there's no TSC support, this
400 * makes it a fast flag for the kernel to see whether it
401 * should be using the TSC.
402 */
403 tsc_disable = 1;
john stultz5d0cf412006-06-26 00:25:12 -0700404}