]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - arch/x86/kernel/tsc_64.c
c852ff9bd5d4f567292be0fbce0c2362edd1ddb4
[linux-2.6-omap-h63xx.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 extern int tsc_unstable;
17 extern int tsc_disabled;
18
19 /* Accelerators for sched_clock()
20  * convert from cycles(64bits) => nanoseconds (64bits)
21  *  basic equation:
22  *              ns = cycles / (freq / ns_per_sec)
23  *              ns = cycles * (ns_per_sec / freq)
24  *              ns = cycles * (10^9 / (cpu_khz * 10^3))
25  *              ns = cycles * (10^6 / cpu_khz)
26  *
27  *      Then we use scaling math (suggested by george@mvista.com) to get:
28  *              ns = cycles * (10^6 * SC / cpu_khz) / SC
29  *              ns = cycles * cyc2ns_scale / SC
30  *
31  *      And since SC is a constant power of two, we can convert the div
32  *  into a shift.
33  *
34  *  We can use khz divisor instead of mhz to keep a better precision, since
35  *  cyc2ns_scale is limited to 10^6 * 2^10, which fits in 32 bits.
36  *  (mathieu.desnoyers@polymtl.ca)
37  *
38  *                      -johnstul@us.ibm.com "math is hard, lets go shopping!"
39  */
40
41 DEFINE_PER_CPU(unsigned long, cyc2ns);
42
43 void set_cyc2ns_scale(unsigned long cpu_khz, int cpu)
44 {
45         unsigned long long tsc_now, ns_now;
46         unsigned long flags, *scale;
47
48         local_irq_save(flags);
49         sched_clock_idle_sleep_event();
50
51         scale = &per_cpu(cyc2ns, cpu);
52
53         rdtscll(tsc_now);
54         ns_now = __cycles_2_ns(tsc_now);
55
56         if (cpu_khz)
57                 *scale = (NSEC_PER_MSEC << CYC2NS_SCALE_FACTOR)/cpu_khz;
58
59         sched_clock_idle_wakeup_event(0);
60         local_irq_restore(flags);
61 }
62
63 #ifdef CONFIG_CPU_FREQ
64
65 /* Frequency scaling support. Adjust the TSC based timer when the cpu frequency
66  * changes.
67  *
68  * RED-PEN: On SMP we assume all CPUs run with the same frequency.  It's
69  * not that important because current Opteron setups do not support
70  * scaling on SMP anyroads.
71  *
72  * Should fix up last_tsc too. Currently gettimeofday in the
73  * first tick after the change will be slightly wrong.
74  */
75
76 static unsigned int  ref_freq;
77 static unsigned long loops_per_jiffy_ref;
78 static unsigned long tsc_khz_ref;
79
80 static int time_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
81                                  void *data)
82 {
83         struct cpufreq_freqs *freq = data;
84         unsigned long *lpj, dummy;
85
86         if (cpu_has(&cpu_data(freq->cpu), X86_FEATURE_CONSTANT_TSC))
87                 return 0;
88
89         lpj = &dummy;
90         if (!(freq->flags & CPUFREQ_CONST_LOOPS))
91 #ifdef CONFIG_SMP
92                 lpj = &cpu_data(freq->cpu).loops_per_jiffy;
93 #else
94                 lpj = &boot_cpu_data.loops_per_jiffy;
95 #endif
96
97         if (!ref_freq) {
98                 ref_freq = freq->old;
99                 loops_per_jiffy_ref = *lpj;
100                 tsc_khz_ref = tsc_khz;
101         }
102         if ((val == CPUFREQ_PRECHANGE  && freq->old < freq->new) ||
103                 (val == CPUFREQ_POSTCHANGE && freq->old > freq->new) ||
104                 (val == CPUFREQ_RESUMECHANGE)) {
105                 *lpj =
106                 cpufreq_scale(loops_per_jiffy_ref, ref_freq, freq->new);
107
108                 tsc_khz = cpufreq_scale(tsc_khz_ref, ref_freq, freq->new);
109                 if (!(freq->flags & CPUFREQ_CONST_LOOPS))
110                         mark_tsc_unstable("cpufreq changes");
111         }
112
113         set_cyc2ns_scale(tsc_khz_ref, freq->cpu);
114
115         return 0;
116 }
117
118 static struct notifier_block time_cpufreq_notifier_block = {
119         .notifier_call  = time_cpufreq_notifier
120 };
121
122 static int __init cpufreq_tsc(void)
123 {
124         cpufreq_register_notifier(&time_cpufreq_notifier_block,
125                                   CPUFREQ_TRANSITION_NOTIFIER);
126         return 0;
127 }
128
129 core_initcall(cpufreq_tsc);
130
131 #endif
132
133 /*
134  * Make an educated guess if the TSC is trustworthy and synchronized
135  * over all CPUs.
136  */
137 __cpuinit int unsynchronized_tsc(void)
138 {
139         if (tsc_unstable)
140                 return 1;
141
142 #ifdef CONFIG_SMP
143         if (apic_is_clustered_box())
144                 return 1;
145 #endif
146
147         if (boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
148                 return 0;
149
150         /* Assume multi socket systems are not synchronized */
151         return num_present_cpus() > 1;
152 }
153
154 static struct clocksource clocksource_tsc;
155
156 /*
157  * We compare the TSC to the cycle_last value in the clocksource
158  * structure to avoid a nasty time-warp. This can be observed in a
159  * very small window right after one CPU updated cycle_last under
160  * xtime/vsyscall_gtod lock and the other CPU reads a TSC value which
161  * is smaller than the cycle_last reference value due to a TSC which
162  * is slighty behind. This delta is nowhere else observable, but in
163  * that case it results in a forward time jump in the range of hours
164  * due to the unsigned delta calculation of the time keeping core
165  * code, which is necessary to support wrapping clocksources like pm
166  * timer.
167  */
168 static cycle_t read_tsc(void)
169 {
170         cycle_t ret = (cycle_t)get_cycles();
171
172         return ret >= clocksource_tsc.cycle_last ?
173                 ret : clocksource_tsc.cycle_last;
174 }
175
176 static cycle_t __vsyscall_fn vread_tsc(void)
177 {
178         cycle_t ret = (cycle_t)vget_cycles();
179
180         return ret >= __vsyscall_gtod_data.clock.cycle_last ?
181                 ret : __vsyscall_gtod_data.clock.cycle_last;
182 }
183
184 static struct clocksource clocksource_tsc = {
185         .name                   = "tsc",
186         .rating                 = 300,
187         .read                   = read_tsc,
188         .mask                   = CLOCKSOURCE_MASK(64),
189         .shift                  = 22,
190         .flags                  = CLOCK_SOURCE_IS_CONTINUOUS |
191                                   CLOCK_SOURCE_MUST_VERIFY,
192         .vread                  = vread_tsc,
193 };
194
195 void mark_tsc_unstable(char *reason)
196 {
197         if (!tsc_unstable) {
198                 tsc_unstable = 1;
199                 printk("Marking TSC unstable due to %s\n", reason);
200                 /* Change only the rating, when not registered */
201                 if (clocksource_tsc.mult)
202                         clocksource_change_rating(&clocksource_tsc, 0);
203                 else
204                         clocksource_tsc.rating = 0;
205         }
206 }
207 EXPORT_SYMBOL_GPL(mark_tsc_unstable);
208
209 void __init init_tsc_clocksource(void)
210 {
211         if (tsc_disabled > 0)
212                 return;
213
214         clocksource_tsc.mult = clocksource_khz2mult(tsc_khz,
215                         clocksource_tsc.shift);
216         if (check_tsc_unstable())
217                 clocksource_tsc.rating = 0;
218
219         clocksource_register(&clocksource_tsc);
220 }