]> bbs.cooldavid.org Git - net-next-2.6.git/blob - arch/i386/kernel/tsc.c
[PATCH] i386: vMI timer patches
[net-next-2.6.git] / arch / i386 / kernel / tsc.c
1 /*
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
7 #include <linux/clocksource.h>
8 #include <linux/workqueue.h>
9 #include <linux/cpufreq.h>
10 #include <linux/jiffies.h>
11 #include <linux/init.h>
12 #include <linux/dmi.h>
13
14 #include <asm/delay.h>
15 #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  */
25 unsigned int tsc_khz;
26 unsigned long long (*custom_sched_clock)(void);
27
28 int tsc_disable;
29
30 #ifdef CONFIG_X86_TSC
31 static 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  */
42 static 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
52 /*
53  * code to mark and check if the TSC is unstable
54  * due to cpufreq or due to unsynced TSCs
55  */
56 static int tsc_unstable;
57
58 static inline int check_tsc_unstable(void)
59 {
60         return tsc_unstable;
61 }
62
63 void mark_tsc_unstable(void)
64 {
65         tsc_unstable = 1;
66 }
67 EXPORT_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  */
90 static unsigned long cyc2ns_scale __read_mostly;
91
92 #define CYC2NS_SCALE_FACTOR 10 /* 2^10, carefully chosen */
93
94 static inline void set_cyc2ns_scale(unsigned long cpu_khz)
95 {
96         cyc2ns_scale = (1000000 << CYC2NS_SCALE_FACTOR)/cpu_khz;
97 }
98
99 static 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  */
107 unsigned long long sched_clock(void)
108 {
109         unsigned long long this_offset;
110
111         if (unlikely(custom_sched_clock))
112                 return (*custom_sched_clock)();
113
114         /*
115          * in the NUMA case we dont use the TSC as they are not
116          * synchronized across all CPUs.
117          */
118 #ifndef CONFIG_NUMA
119         if (!cpu_khz || check_tsc_unstable())
120 #endif
121                 /* no locking but a rare wrong value is not a big deal */
122                 return (jiffies_64 - INITIAL_JIFFIES) * (1000000000 / HZ);
123
124         /* read the Time Stamp Counter: */
125         rdtscll(this_offset);
126
127         /* return the value in ns */
128         return cycles_2_ns(this_offset);
129 }
130
131 static unsigned long calculate_cpu_khz(void)
132 {
133         unsigned long long start, end;
134         unsigned long count;
135         u64 delta64;
136         int i;
137         unsigned long flags;
138
139         local_irq_save(flags);
140
141         /* run 3 times to ensure the cache is warm */
142         for (i = 0; i < 3; i++) {
143                 mach_prepare_counter();
144                 rdtscll(start);
145                 mach_countup(&count);
146                 rdtscll(end);
147         }
148         /*
149          * Error: ECTCNEVERSET
150          * The CTC wasn't reliable: we got a hit on the very first read,
151          * or the CPU was so fast/slow that the quotient wouldn't fit in
152          * 32 bits..
153          */
154         if (count <= 1)
155                 goto err;
156
157         delta64 = end - start;
158
159         /* cpu freq too fast: */
160         if (delta64 > (1ULL<<32))
161                 goto err;
162
163         /* cpu freq too slow: */
164         if (delta64 <= CALIBRATE_TIME_MSEC)
165                 goto err;
166
167         delta64 += CALIBRATE_TIME_MSEC/2; /* round for do_div */
168         do_div(delta64,CALIBRATE_TIME_MSEC);
169
170         local_irq_restore(flags);
171         return (unsigned long)delta64;
172 err:
173         local_irq_restore(flags);
174         return 0;
175 }
176
177 int recalibrate_cpu_khz(void)
178 {
179 #ifndef CONFIG_SMP
180         unsigned long cpu_khz_old = cpu_khz;
181
182         if (cpu_has_tsc) {
183                 cpu_khz = calculate_cpu_khz();
184                 tsc_khz = cpu_khz;
185                 cpu_data[0].loops_per_jiffy =
186                         cpufreq_scale(cpu_data[0].loops_per_jiffy,
187                                         cpu_khz_old, cpu_khz);
188                 return 0;
189         } else
190                 return -ENODEV;
191 #else
192         return -ENODEV;
193 #endif
194 }
195
196 EXPORT_SYMBOL(recalibrate_cpu_khz);
197
198 void __init tsc_init(void)
199 {
200         if (!cpu_has_tsc || tsc_disable)
201                 return;
202
203         cpu_khz = calculate_cpu_khz();
204         tsc_khz = cpu_khz;
205
206         if (!cpu_khz)
207                 return;
208
209         printk("Detected %lu.%03lu MHz processor.\n",
210                                 (unsigned long)cpu_khz / 1000,
211                                 (unsigned long)cpu_khz % 1000);
212
213         set_cyc2ns_scale(cpu_khz);
214         use_tsc_delay();
215 }
216
217 #ifdef CONFIG_CPU_FREQ
218
219 static unsigned int cpufreq_delayed_issched = 0;
220 static unsigned int cpufreq_init = 0;
221 static struct work_struct cpufreq_delayed_get_work;
222
223 static void handle_cpufreq_delayed_get(struct work_struct *work)
224 {
225         unsigned int cpu;
226
227         for_each_online_cpu(cpu)
228                 cpufreq_get(cpu);
229
230         cpufreq_delayed_issched = 0;
231 }
232
233 /*
234  * if we notice cpufreq oddness, schedule a call to cpufreq_get() as it tries
235  * to verify the CPU frequency the timing core thinks the CPU is running
236  * at is still correct.
237  */
238 static inline void cpufreq_delayed_get(void)
239 {
240         if (cpufreq_init && !cpufreq_delayed_issched) {
241                 cpufreq_delayed_issched = 1;
242                 printk(KERN_DEBUG "Checking if CPU frequency changed.\n");
243                 schedule_work(&cpufreq_delayed_get_work);
244         }
245 }
246
247 /*
248  * if the CPU frequency is scaled, TSC-based delays will need a different
249  * loops_per_jiffy value to function properly.
250  */
251 static unsigned int ref_freq = 0;
252 static unsigned long loops_per_jiffy_ref = 0;
253 static unsigned long cpu_khz_ref = 0;
254
255 static int
256 time_cpufreq_notifier(struct notifier_block *nb, unsigned long val, void *data)
257 {
258         struct cpufreq_freqs *freq = data;
259
260         if (val != CPUFREQ_RESUMECHANGE && val != CPUFREQ_SUSPENDCHANGE)
261                 write_seqlock_irq(&xtime_lock);
262
263         if (!ref_freq) {
264                 if (!freq->old){
265                         ref_freq = freq->new;
266                         goto end;
267                 }
268                 ref_freq = freq->old;
269                 loops_per_jiffy_ref = cpu_data[freq->cpu].loops_per_jiffy;
270                 cpu_khz_ref = cpu_khz;
271         }
272
273         if ((val == CPUFREQ_PRECHANGE  && freq->old < freq->new) ||
274             (val == CPUFREQ_POSTCHANGE && freq->old > freq->new) ||
275             (val == CPUFREQ_RESUMECHANGE)) {
276                 if (!(freq->flags & CPUFREQ_CONST_LOOPS))
277                         cpu_data[freq->cpu].loops_per_jiffy =
278                                 cpufreq_scale(loops_per_jiffy_ref,
279                                                 ref_freq, freq->new);
280
281                 if (cpu_khz) {
282
283                         if (num_online_cpus() == 1)
284                                 cpu_khz = cpufreq_scale(cpu_khz_ref,
285                                                 ref_freq, freq->new);
286                         if (!(freq->flags & CPUFREQ_CONST_LOOPS)) {
287                                 tsc_khz = cpu_khz;
288                                 set_cyc2ns_scale(cpu_khz);
289                                 /*
290                                  * TSC based sched_clock turns
291                                  * to junk w/ cpufreq
292                                  */
293                                 mark_tsc_unstable();
294                         }
295                 }
296         }
297 end:
298         if (val != CPUFREQ_RESUMECHANGE && val != CPUFREQ_SUSPENDCHANGE)
299                 write_sequnlock_irq(&xtime_lock);
300
301         return 0;
302 }
303
304 static struct notifier_block time_cpufreq_notifier_block = {
305         .notifier_call  = time_cpufreq_notifier
306 };
307
308 static int __init cpufreq_tsc(void)
309 {
310         int ret;
311
312         INIT_WORK(&cpufreq_delayed_get_work, handle_cpufreq_delayed_get);
313         ret = cpufreq_register_notifier(&time_cpufreq_notifier_block,
314                                         CPUFREQ_TRANSITION_NOTIFIER);
315         if (!ret)
316                 cpufreq_init = 1;
317
318         return ret;
319 }
320
321 core_initcall(cpufreq_tsc);
322
323 #endif
324
325 /* clock source code */
326
327 static unsigned long current_tsc_khz = 0;
328 static int tsc_update_callback(void);
329
330 static cycle_t read_tsc(void)
331 {
332         cycle_t ret;
333
334         rdtscll(ret);
335
336         return ret;
337 }
338
339 static struct clocksource clocksource_tsc = {
340         .name                   = "tsc",
341         .rating                 = 300,
342         .read                   = read_tsc,
343         .mask                   = CLOCKSOURCE_MASK(64),
344         .mult                   = 0, /* to be set */
345         .shift                  = 22,
346         .update_callback        = tsc_update_callback,
347         .is_continuous          = 1,
348 };
349
350 static int tsc_update_callback(void)
351 {
352         int change = 0;
353
354         /* check to see if we should switch to the safe clocksource: */
355         if (clocksource_tsc.rating != 0 && check_tsc_unstable()) {
356                 clocksource_tsc.rating = 0;
357                 clocksource_reselect();
358                 change = 1;
359         }
360
361         /* only update if tsc_khz has changed: */
362         if (current_tsc_khz != tsc_khz) {
363                 current_tsc_khz = tsc_khz;
364                 clocksource_tsc.mult = clocksource_khz2mult(current_tsc_khz,
365                                                         clocksource_tsc.shift);
366                 change = 1;
367         }
368
369         return change;
370 }
371
372 static int __init dmi_mark_tsc_unstable(struct dmi_system_id *d)
373 {
374         printk(KERN_NOTICE "%s detected: marking TSC unstable.\n",
375                        d->ident);
376         mark_tsc_unstable();
377         return 0;
378 }
379
380 /* List of systems that have known TSC problems */
381 static struct dmi_system_id __initdata bad_tsc_dmi_table[] = {
382         {
383          .callback = dmi_mark_tsc_unstable,
384          .ident = "IBM Thinkpad 380XD",
385          .matches = {
386                      DMI_MATCH(DMI_BOARD_VENDOR, "IBM"),
387                      DMI_MATCH(DMI_BOARD_NAME, "2635FA0"),
388                      },
389          },
390          {}
391 };
392
393 #define TSC_FREQ_CHECK_INTERVAL (10*MSEC_PER_SEC) /* 10sec in MS */
394 static struct timer_list verify_tsc_freq_timer;
395
396 /* XXX - Probably should add locking */
397 static void verify_tsc_freq(unsigned long unused)
398 {
399         static u64 last_tsc;
400         static unsigned long last_jiffies;
401
402         u64 now_tsc, interval_tsc;
403         unsigned long now_jiffies, interval_jiffies;
404
405
406         if (check_tsc_unstable())
407                 return;
408
409         rdtscll(now_tsc);
410         now_jiffies = jiffies;
411
412         if (!last_jiffies) {
413                 goto out;
414         }
415
416         interval_jiffies = now_jiffies - last_jiffies;
417         interval_tsc = now_tsc - last_tsc;
418         interval_tsc *= HZ;
419         do_div(interval_tsc, cpu_khz*1000);
420
421         if (interval_tsc < (interval_jiffies * 3 / 4)) {
422                 printk("TSC appears to be running slowly. "
423                         "Marking it as unstable\n");
424                 mark_tsc_unstable();
425                 return;
426         }
427
428 out:
429         last_tsc = now_tsc;
430         last_jiffies = now_jiffies;
431         /* set us up to go off on the next interval: */
432         mod_timer(&verify_tsc_freq_timer,
433                 jiffies + msecs_to_jiffies(TSC_FREQ_CHECK_INTERVAL));
434 }
435
436 /*
437  * Make an educated guess if the TSC is trustworthy and synchronized
438  * over all CPUs.
439  */
440 static __init int unsynchronized_tsc(void)
441 {
442         /*
443          * Intel systems are normally all synchronized.
444          * Exceptions must mark TSC as unstable:
445          */
446         if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
447                 return 0;
448
449         /* assume multi socket systems are not synchronized: */
450         return num_possible_cpus() > 1;
451 }
452
453 static int __init init_tsc_clocksource(void)
454 {
455
456         if (cpu_has_tsc && tsc_khz && !tsc_disable) {
457                 /* check blacklist */
458                 dmi_check_system(bad_tsc_dmi_table);
459
460                 if (unsynchronized_tsc()) /* mark unstable if unsynced */
461                         mark_tsc_unstable();
462                 current_tsc_khz = tsc_khz;
463                 clocksource_tsc.mult = clocksource_khz2mult(current_tsc_khz,
464                                                         clocksource_tsc.shift);
465                 /* lower the rating if we already know its unstable: */
466                 if (check_tsc_unstable())
467                         clocksource_tsc.rating = 0;
468
469                 init_timer(&verify_tsc_freq_timer);
470                 verify_tsc_freq_timer.function = verify_tsc_freq;
471                 verify_tsc_freq_timer.expires =
472                         jiffies + msecs_to_jiffies(TSC_FREQ_CHECK_INTERVAL);
473                 add_timer(&verify_tsc_freq_timer);
474
475                 return clocksource_register(&clocksource_tsc);
476         }
477
478         return 0;
479 }
480
481 module_init(init_tsc_clocksource);