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