Merge branch 'upstream' of git://git.kernel.org/pub/scm/linux/kernel/git/ralf/upstrea...
[sfrench/cifs-2.6.git] / arch / x86 / kernel / tsc_64.c
1 #include <linux/kernel.h>
2 #include <linux/sched.h>
3 #include <linux/interrupt.h>
4 #include <linux/init.h>
5 #include <linux/clocksource.h>
6 #include <linux/time.h>
7 #include <linux/acpi.h>
8 #include <linux/cpufreq.h>
9 #include <linux/acpi_pmtmr.h>
10
11 #include <asm/hpet.h>
12 #include <asm/timex.h>
13 #include <asm/timer.h>
14 #include <asm/vgtod.h>
15
16 static int notsc __initdata = 0;
17
18 unsigned int cpu_khz;           /* TSC clocks / usec, not used here */
19 EXPORT_SYMBOL(cpu_khz);
20 unsigned int tsc_khz;
21 EXPORT_SYMBOL(tsc_khz);
22
23 /* Accelerators for sched_clock()
24  * convert from cycles(64bits) => nanoseconds (64bits)
25  *  basic equation:
26  *              ns = cycles / (freq / ns_per_sec)
27  *              ns = cycles * (ns_per_sec / freq)
28  *              ns = cycles * (10^9 / (cpu_khz * 10^3))
29  *              ns = cycles * (10^6 / cpu_khz)
30  *
31  *      Then we use scaling math (suggested by george@mvista.com) to get:
32  *              ns = cycles * (10^6 * SC / cpu_khz) / SC
33  *              ns = cycles * cyc2ns_scale / SC
34  *
35  *      And since SC is a constant power of two, we can convert the div
36  *  into a shift.
37  *
38  *  We can use khz divisor instead of mhz to keep a better precision, since
39  *  cyc2ns_scale is limited to 10^6 * 2^10, which fits in 32 bits.
40  *  (mathieu.desnoyers@polymtl.ca)
41  *
42  *                      -johnstul@us.ibm.com "math is hard, lets go shopping!"
43  */
44 DEFINE_PER_CPU(unsigned long, cyc2ns);
45
46 static void set_cyc2ns_scale(unsigned long cpu_khz, int cpu)
47 {
48         unsigned long flags, prev_scale, *scale;
49         unsigned long long tsc_now, ns_now;
50
51         local_irq_save(flags);
52         sched_clock_idle_sleep_event();
53
54         scale = &per_cpu(cyc2ns, cpu);
55
56         rdtscll(tsc_now);
57         ns_now = __cycles_2_ns(tsc_now);
58
59         prev_scale = *scale;
60         if (cpu_khz)
61                 *scale = (NSEC_PER_MSEC << CYC2NS_SCALE_FACTOR)/cpu_khz;
62
63         sched_clock_idle_wakeup_event(0);
64         local_irq_restore(flags);
65 }
66
67 unsigned long long native_sched_clock(void)
68 {
69         unsigned long a = 0;
70
71         /* Could do CPU core sync here. Opteron can execute rdtsc speculatively,
72          * which means it is not completely exact and may not be monotonous
73          * between CPUs. But the errors should be too small to matter for
74          * scheduling purposes.
75          */
76
77         rdtscll(a);
78         return cycles_2_ns(a);
79 }
80
81 /* We need to define a real function for sched_clock, to override the
82    weak default version */
83 #ifdef CONFIG_PARAVIRT
84 unsigned long long sched_clock(void)
85 {
86         return paravirt_sched_clock();
87 }
88 #else
89 unsigned long long
90 sched_clock(void) __attribute__((alias("native_sched_clock")));
91 #endif
92
93
94 static int tsc_unstable;
95
96 int check_tsc_unstable(void)
97 {
98         return tsc_unstable;
99 }
100 EXPORT_SYMBOL_GPL(check_tsc_unstable);
101
102 #ifdef CONFIG_CPU_FREQ
103
104 /* Frequency scaling support. Adjust the TSC based timer when the cpu frequency
105  * changes.
106  *
107  * RED-PEN: On SMP we assume all CPUs run with the same frequency.  It's
108  * not that important because current Opteron setups do not support
109  * scaling on SMP anyroads.
110  *
111  * Should fix up last_tsc too. Currently gettimeofday in the
112  * first tick after the change will be slightly wrong.
113  */
114
115 static unsigned int  ref_freq;
116 static unsigned long loops_per_jiffy_ref;
117 static unsigned long tsc_khz_ref;
118
119 static int time_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
120                                  void *data)
121 {
122         struct cpufreq_freqs *freq = data;
123         unsigned long *lpj, dummy;
124
125         if (cpu_has(&cpu_data(freq->cpu), X86_FEATURE_CONSTANT_TSC))
126                 return 0;
127
128         lpj = &dummy;
129         if (!(freq->flags & CPUFREQ_CONST_LOOPS))
130 #ifdef CONFIG_SMP
131                 lpj = &cpu_data(freq->cpu).loops_per_jiffy;
132 #else
133                 lpj = &boot_cpu_data.loops_per_jiffy;
134 #endif
135
136         if (!ref_freq) {
137                 ref_freq = freq->old;
138                 loops_per_jiffy_ref = *lpj;
139                 tsc_khz_ref = tsc_khz;
140         }
141         if ((val == CPUFREQ_PRECHANGE  && freq->old < freq->new) ||
142                 (val == CPUFREQ_POSTCHANGE && freq->old > freq->new) ||
143                 (val == CPUFREQ_RESUMECHANGE)) {
144                 *lpj =
145                 cpufreq_scale(loops_per_jiffy_ref, ref_freq, freq->new);
146
147                 tsc_khz = cpufreq_scale(tsc_khz_ref, ref_freq, freq->new);
148                 if (!(freq->flags & CPUFREQ_CONST_LOOPS))
149                         mark_tsc_unstable("cpufreq changes");
150         }
151
152         preempt_disable();
153         set_cyc2ns_scale(tsc_khz_ref, smp_processor_id());
154         preempt_enable();
155
156         return 0;
157 }
158
159 static struct notifier_block time_cpufreq_notifier_block = {
160         .notifier_call  = time_cpufreq_notifier
161 };
162
163 static int __init cpufreq_tsc(void)
164 {
165         cpufreq_register_notifier(&time_cpufreq_notifier_block,
166                                   CPUFREQ_TRANSITION_NOTIFIER);
167         return 0;
168 }
169
170 core_initcall(cpufreq_tsc);
171
172 #endif
173
174 #define MAX_RETRIES     5
175 #define SMI_TRESHOLD    50000
176
177 /*
178  * Read TSC and the reference counters. Take care of SMI disturbance
179  */
180 static unsigned long __init tsc_read_refs(unsigned long *pm,
181                                           unsigned long *hpet)
182 {
183         unsigned long t1, t2;
184         int i;
185
186         for (i = 0; i < MAX_RETRIES; i++) {
187                 t1 = get_cycles();
188                 if (hpet)
189                         *hpet = hpet_readl(HPET_COUNTER) & 0xFFFFFFFF;
190                 else
191                         *pm = acpi_pm_read_early();
192                 t2 = get_cycles();
193                 if ((t2 - t1) < SMI_TRESHOLD)
194                         return t2;
195         }
196         return ULONG_MAX;
197 }
198
199 /**
200  * tsc_calibrate - calibrate the tsc on boot
201  */
202 void __init tsc_calibrate(void)
203 {
204         unsigned long flags, tsc1, tsc2, tr1, tr2, pm1, pm2, hpet1, hpet2;
205         int hpet = is_hpet_enabled(), cpu;
206
207         local_irq_save(flags);
208
209         tsc1 = tsc_read_refs(&pm1, hpet ? &hpet1 : NULL);
210
211         outb((inb(0x61) & ~0x02) | 0x01, 0x61);
212
213         outb(0xb0, 0x43);
214         outb((CLOCK_TICK_RATE / (1000 / 50)) & 0xff, 0x42);
215         outb((CLOCK_TICK_RATE / (1000 / 50)) >> 8, 0x42);
216         tr1 = get_cycles();
217         while ((inb(0x61) & 0x20) == 0);
218         tr2 = get_cycles();
219
220         tsc2 = tsc_read_refs(&pm2, hpet ? &hpet2 : NULL);
221
222         local_irq_restore(flags);
223
224         /*
225          * Preset the result with the raw and inaccurate PIT
226          * calibration value
227          */
228         tsc_khz = (tr2 - tr1) / 50;
229
230         /* hpet or pmtimer available ? */
231         if (!hpet && !pm1 && !pm2) {
232                 printk(KERN_INFO "TSC calibrated against PIT\n");
233                 return;
234         }
235
236         /* Check, whether the sampling was disturbed by an SMI */
237         if (tsc1 == ULONG_MAX || tsc2 == ULONG_MAX) {
238                 printk(KERN_WARNING "TSC calibration disturbed by SMI, "
239                        "using PIT calibration result\n");
240                 return;
241         }
242
243         tsc2 = (tsc2 - tsc1) * 1000000L;
244
245         if (hpet) {
246                 printk(KERN_INFO "TSC calibrated against HPET\n");
247                 if (hpet2 < hpet1)
248                         hpet2 += 0x100000000;
249                 hpet2 -= hpet1;
250                 tsc1 = (hpet2 * hpet_readl(HPET_PERIOD)) / 1000000;
251         } else {
252                 printk(KERN_INFO "TSC calibrated against PM_TIMER\n");
253                 if (pm2 < pm1)
254                         pm2 += ACPI_PM_OVRRUN;
255                 pm2 -= pm1;
256                 tsc1 = (pm2 * 1000000000) / PMTMR_TICKS_PER_SEC;
257         }
258
259         tsc_khz = tsc2 / tsc1;
260
261         for_each_possible_cpu(cpu)
262                 set_cyc2ns_scale(tsc_khz, cpu);
263 }
264
265 /*
266  * Make an educated guess if the TSC is trustworthy and synchronized
267  * over all CPUs.
268  */
269 __cpuinit int unsynchronized_tsc(void)
270 {
271         if (tsc_unstable)
272                 return 1;
273
274 #ifdef CONFIG_SMP
275         if (apic_is_clustered_box())
276                 return 1;
277 #endif
278
279         if (boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
280                 return 0;
281
282         /* Assume multi socket systems are not synchronized */
283         return num_present_cpus() > 1;
284 }
285
286 int __init notsc_setup(char *s)
287 {
288         notsc = 1;
289         return 1;
290 }
291
292 __setup("notsc", notsc_setup);
293
294 static struct clocksource clocksource_tsc;
295
296 /*
297  * We compare the TSC to the cycle_last value in the clocksource
298  * structure to avoid a nasty time-warp. This can be observed in a
299  * very small window right after one CPU updated cycle_last under
300  * xtime/vsyscall_gtod lock and the other CPU reads a TSC value which
301  * is smaller than the cycle_last reference value due to a TSC which
302  * is slighty behind. This delta is nowhere else observable, but in
303  * that case it results in a forward time jump in the range of hours
304  * due to the unsigned delta calculation of the time keeping core
305  * code, which is necessary to support wrapping clocksources like pm
306  * timer.
307  */
308 static cycle_t read_tsc(void)
309 {
310         cycle_t ret = (cycle_t)get_cycles();
311
312         return ret >= clocksource_tsc.cycle_last ?
313                 ret : clocksource_tsc.cycle_last;
314 }
315
316 static cycle_t __vsyscall_fn vread_tsc(void)
317 {
318         cycle_t ret = (cycle_t)vget_cycles();
319
320         return ret >= __vsyscall_gtod_data.clock.cycle_last ?
321                 ret : __vsyscall_gtod_data.clock.cycle_last;
322 }
323
324 static struct clocksource clocksource_tsc = {
325         .name                   = "tsc",
326         .rating                 = 300,
327         .read                   = read_tsc,
328         .mask                   = CLOCKSOURCE_MASK(64),
329         .shift                  = 22,
330         .flags                  = CLOCK_SOURCE_IS_CONTINUOUS |
331                                   CLOCK_SOURCE_MUST_VERIFY,
332         .vread                  = vread_tsc,
333 };
334
335 void mark_tsc_unstable(char *reason)
336 {
337         if (!tsc_unstable) {
338                 tsc_unstable = 1;
339                 printk("Marking TSC unstable due to %s\n", reason);
340                 /* Change only the rating, when not registered */
341                 if (clocksource_tsc.mult)
342                         clocksource_change_rating(&clocksource_tsc, 0);
343                 else
344                         clocksource_tsc.rating = 0;
345         }
346 }
347 EXPORT_SYMBOL_GPL(mark_tsc_unstable);
348
349 void __init init_tsc_clocksource(void)
350 {
351         if (!notsc) {
352                 clocksource_tsc.mult = clocksource_khz2mult(tsc_khz,
353                                                         clocksource_tsc.shift);
354                 if (check_tsc_unstable())
355                         clocksource_tsc.rating = 0;
356
357                 clocksource_register(&clocksource_tsc);
358         }
359 }