]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - arch/x86/kernel/tsc_32.c
Merge branch 'linus' into x86/delay
[linux-2.6-omap-h63xx.git] / arch / x86 / kernel / tsc_32.c
1 #include <linux/sched.h>
2 #include <linux/clocksource.h>
3 #include <linux/workqueue.h>
4 #include <linux/delay.h>
5 #include <linux/cpufreq.h>
6 #include <linux/jiffies.h>
7 #include <linux/init.h>
8 #include <linux/dmi.h>
9 #include <linux/percpu.h>
10
11 #include <asm/delay.h>
12 #include <asm/tsc.h>
13 #include <asm/io.h>
14 #include <asm/timer.h>
15
16 #include "mach_timer.h"
17
18 /* 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 */
21 static int tsc_disabled = -1;
22
23 /*
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  */
28 unsigned int tsc_khz;
29 EXPORT_SYMBOL_GPL(tsc_khz);
30
31 #ifdef CONFIG_X86_TSC
32 static int __init tsc_setup(char *str)
33 {
34         printk(KERN_WARNING "notsc: Kernel compiled with CONFIG_X86_TSC, "
35                "cannot disable TSC completely.\n");
36         tsc_disabled = 1;
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  */
44 static int __init tsc_setup(char *str)
45 {
46         setup_clear_cpu_cap(X86_FEATURE_TSC);
47         return 1;
48 }
49 #endif
50
51 __setup("notsc", tsc_setup);
52
53 /*
54  * code to mark and check if the TSC is unstable
55  * due to cpufreq or due to unsynced TSCs
56  */
57 static int tsc_unstable;
58
59 int check_tsc_unstable(void)
60 {
61         return tsc_unstable;
62 }
63 EXPORT_SYMBOL_GPL(check_tsc_unstable);
64
65 /* Accelerators 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 precision, 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  */
86
87 DEFINE_PER_CPU(unsigned long, cyc2ns);
88
89 static void set_cyc2ns_scale(unsigned long cpu_khz, int cpu)
90 {
91         unsigned long long tsc_now, ns_now;
92         unsigned long flags, *scale;
93
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
102         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);
110 }
111
112 /*
113  * Scheduler clock - returns current time in nanosec units.
114  */
115 unsigned long long native_sched_clock(void)
116 {
117         unsigned long long this_offset;
118
119         /*
120          * Fall back to jiffies if there's no TSC available:
121          * ( 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. )
126          */
127         if (unlikely(tsc_disabled))
128                 /* No locking but a rare wrong value is not a big deal: */
129                 return (jiffies_64 - INITIAL_JIFFIES) * (1000000000 / HZ);
130
131         /* read the Time Stamp Counter: */
132         rdtscll(this_offset);
133
134         /* return the value in ns */
135         return cycles_2_ns(this_offset);
136 }
137
138 /* We need to define a real function for sched_clock, to override the
139    weak default version */
140 #ifdef CONFIG_PARAVIRT
141 unsigned long long sched_clock(void)
142 {
143         return paravirt_sched_clock();
144 }
145 #else
146 unsigned long long sched_clock(void)
147         __attribute__((alias("native_sched_clock")));
148 #endif
149
150 unsigned long native_calculate_cpu_khz(void)
151 {
152         unsigned long long start, end;
153         unsigned long count;
154         u64 delta64 = (u64)ULLONG_MAX;
155         int i;
156         unsigned long flags;
157
158         local_irq_save(flags);
159
160         /* run 3 times to ensure the cache is warm and to get an accurate reading */
161         for (i = 0; i < 3; i++) {
162                 mach_prepare_counter();
163                 rdtscll(start);
164                 mach_countup(&count);
165                 rdtscll(end);
166
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                  */
184                 delta64 = min(delta64, (end - start));
185         }
186
187         /* cpu freq too fast (or every run was bad): */
188         if (delta64 > (1ULL<<32))
189                 goto err;
190
191         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;
196 err:
197         local_irq_restore(flags);
198         return 0;
199 }
200
201 int 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;
209                 cpu_data(0).loops_per_jiffy =
210                         cpufreq_scale(cpu_data(0).loops_per_jiffy,
211                                         cpu_khz_old, cpu_khz);
212                 return 0;
213         } else
214                 return -ENODEV;
215 #else
216         return -ENODEV;
217 #endif
218 }
219
220 EXPORT_SYMBOL(recalibrate_cpu_khz);
221
222 #ifdef CONFIG_CPU_FREQ
223
224 /*
225  * if the CPU frequency is scaled, TSC-based delays will need a different
226  * loops_per_jiffy value to function properly.
227  */
228 static unsigned int ref_freq;
229 static unsigned long loops_per_jiffy_ref;
230 static unsigned long cpu_khz_ref;
231
232 static int
233 time_cpufreq_notifier(struct notifier_block *nb, unsigned long val, void *data)
234 {
235         struct cpufreq_freqs *freq = data;
236
237         if (!ref_freq) {
238                 if (!freq->old){
239                         ref_freq = freq->new;
240                         return 0;
241                 }
242                 ref_freq = freq->old;
243                 loops_per_jiffy_ref = cpu_data(freq->cpu).loops_per_jiffy;
244                 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))
251                         cpu_data(freq->cpu).loops_per_jiffy =
252                                 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;
262                                 set_cyc2ns_scale(cpu_khz, freq->cpu);
263                                 /*
264                                  * TSC based sched_clock turns
265                                  * to junk w/ cpufreq
266                                  */
267                                 mark_tsc_unstable("cpufreq changes");
268                         }
269                 }
270         }
271
272         return 0;
273 }
274
275 static struct notifier_block time_cpufreq_notifier_block = {
276         .notifier_call  = time_cpufreq_notifier
277 };
278
279 static int __init cpufreq_tsc(void)
280 {
281         return cpufreq_register_notifier(&time_cpufreq_notifier_block,
282                                          CPUFREQ_TRANSITION_NOTIFIER);
283 }
284 core_initcall(cpufreq_tsc);
285
286 #endif
287
288 /* clock source code */
289
290 static unsigned long current_tsc_khz;
291 static struct clocksource clocksource_tsc;
292
293 /*
294  * We compare the TSC to the cycle_last value in the clocksource
295  * structure to avoid a nasty time-warp issue. This can be observed in
296  * a very small window right after one CPU updated cycle_last under
297  * xtime lock and the other CPU reads a TSC value which is smaller
298  * than the cycle_last reference value due to a TSC which is slighty
299  * behind. This delta is nowhere else observable, but in that case it
300  * results in a forward time jump in the range of hours due to the
301  * unsigned delta calculation of the time keeping core code, which is
302  * necessary to support wrapping clocksources like pm timer.
303  */
304 static cycle_t read_tsc(void)
305 {
306         cycle_t ret;
307
308         rdtscll(ret);
309
310         return ret >= clocksource_tsc.cycle_last ?
311                 ret : clocksource_tsc.cycle_last;
312 }
313
314 static struct clocksource clocksource_tsc = {
315         .name                   = "tsc",
316         .rating                 = 300,
317         .read                   = read_tsc,
318         .mask                   = CLOCKSOURCE_MASK(64),
319         .mult                   = 0, /* to be set */
320         .shift                  = 22,
321         .flags                  = CLOCK_SOURCE_IS_CONTINUOUS |
322                                   CLOCK_SOURCE_MUST_VERIFY,
323 };
324
325 void mark_tsc_unstable(char *reason)
326 {
327         if (!tsc_unstable) {
328                 tsc_unstable = 1;
329                 printk("Marking TSC unstable due to: %s.\n", reason);
330                 /* Can be called before registration */
331                 if (clocksource_tsc.mult)
332                         clocksource_change_rating(&clocksource_tsc, 0);
333                 else
334                         clocksource_tsc.rating = 0;
335         }
336 }
337 EXPORT_SYMBOL_GPL(mark_tsc_unstable);
338
339 static int __init dmi_mark_tsc_unstable(const struct dmi_system_id *d)
340 {
341         printk(KERN_NOTICE "%s detected: marking TSC unstable.\n",
342                d->ident);
343         tsc_unstable = 1;
344         return 0;
345 }
346
347 /* List of systems that have known TSC problems */
348 static struct dmi_system_id __initdata bad_tsc_dmi_table[] = {
349         {
350          .callback = dmi_mark_tsc_unstable,
351          .ident = "IBM Thinkpad 380XD",
352          .matches = {
353                      DMI_MATCH(DMI_BOARD_VENDOR, "IBM"),
354                      DMI_MATCH(DMI_BOARD_NAME, "2635FA0"),
355                      },
356          },
357          {}
358 };
359
360 /*
361  * Make an educated guess if the TSC is trustworthy and synchronized
362  * over all CPUs.
363  */
364 __cpuinit int unsynchronized_tsc(void)
365 {
366         if (!cpu_has_tsc || tsc_unstable)
367                 return 1;
368
369         /* Anything with constant TSC should be synchronized */
370         if (boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
371                 return 0;
372
373         /*
374          * Intel systems are normally all synchronized.
375          * Exceptions must mark TSC as unstable:
376          */
377         if (boot_cpu_data.x86_vendor != X86_VENDOR_INTEL) {
378                 /* assume multi socket systems are not synchronized: */
379                 if (num_possible_cpus() > 1)
380                         tsc_unstable = 1;
381         }
382         return tsc_unstable;
383 }
384
385 /*
386  * Geode_LX - the OLPC CPU has a possibly a very reliable TSC
387  */
388 #ifdef CONFIG_MGEODE_LX
389 /* RTSC counts during suspend */
390 #define RTSC_SUSP 0x100
391
392 static void __init check_geode_tsc_reliable(void)
393 {
394         unsigned long res_low, res_high;
395
396         rdmsr_safe(MSR_GEODE_BUSCONT_CONF0, &res_low, &res_high);
397         if (res_low & RTSC_SUSP)
398                 clocksource_tsc.flags &= ~CLOCK_SOURCE_MUST_VERIFY;
399 }
400 #else
401 static inline void check_geode_tsc_reliable(void) { }
402 #endif
403
404
405 void __init tsc_init(void)
406 {
407         int cpu;
408         u64 lpj;
409
410         if (!cpu_has_tsc || tsc_disabled > 0)
411                 return;
412
413         cpu_khz = calculate_cpu_khz();
414         tsc_khz = cpu_khz;
415
416         if (!cpu_khz) {
417                 mark_tsc_unstable("could not calculate TSC khz");
418                 return;
419         }
420
421         lpj = ((u64)tsc_khz * 1000);
422         do_div(lpj, HZ);
423         lpj_fine = lpj;
424
425         /* now allow native_sched_clock() to use rdtsc */
426         tsc_disabled = 0;
427
428         printk("Detected %lu.%03lu MHz processor.\n",
429                                 (unsigned long)cpu_khz / 1000,
430                                 (unsigned long)cpu_khz % 1000);
431
432         /*
433          * Secondary CPUs do not run through tsc_init(), so set up
434          * all the scale factors for all CPUs, assuming the same
435          * speed as the bootup CPU. (cpufreq notifiers will fix this
436          * up if their speed diverges)
437          */
438         for_each_possible_cpu(cpu)
439                 set_cyc2ns_scale(cpu_khz, cpu);
440
441         use_tsc_delay();
442
443         /* Check and install the TSC clocksource */
444         dmi_check_system(bad_tsc_dmi_table);
445
446         unsynchronized_tsc();
447         check_geode_tsc_reliable();
448         current_tsc_khz = tsc_khz;
449         clocksource_tsc.mult = clocksource_khz2mult(current_tsc_khz,
450                                                         clocksource_tsc.shift);
451         /* lower the rating if we already know its unstable: */
452         if (check_tsc_unstable()) {
453                 clocksource_tsc.rating = 0;
454                 clocksource_tsc.flags &= ~CLOCK_SOURCE_IS_CONTINUOUS;
455         }
456         clocksource_register(&clocksource_tsc);
457 }