blob: 947554ddabb6c7779f9e9adedfc07d7891dba283 [file] [log] [blame]
john stultzc37e7bb2007-02-16 01:28:19 -08001#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>
Thomas Gleixnerd3716982007-10-12 23:04:06 +02009#include <linux/acpi_pmtmr.h>
john stultzc37e7bb2007-02-16 01:28:19 -080010
Thomas Gleixnerd3716982007-10-12 23:04:06 +020011#include <asm/hpet.h>
john stultzc37e7bb2007-02-16 01:28:19 -080012#include <asm/timex.h>
Guillaume Chazarain53d517c2008-01-30 13:30:06 +010013#include <asm/timer.h>
john stultzc37e7bb2007-02-16 01:28:19 -080014
john stultz14899392007-02-16 01:28:20 -080015static int notsc __initdata = 0;
john stultzc37e7bb2007-02-16 01:28:19 -080016
17unsigned int cpu_khz; /* TSC clocks / usec, not used here */
18EXPORT_SYMBOL(cpu_khz);
Joerg Roedel6b37f5a2007-05-02 19:27:06 +020019unsigned int tsc_khz;
20EXPORT_SYMBOL(tsc_khz);
john stultzc37e7bb2007-02-16 01:28:19 -080021
Guillaume Chazarain53d517c2008-01-30 13:30:06 +010022/* Accelerators for sched_clock()
23 * convert from cycles(64bits) => nanoseconds (64bits)
24 * basic equation:
25 * ns = cycles / (freq / ns_per_sec)
26 * ns = cycles * (ns_per_sec / freq)
27 * ns = cycles * (10^9 / (cpu_khz * 10^3))
28 * ns = cycles * (10^6 / cpu_khz)
29 *
30 * Then we use scaling math (suggested by george@mvista.com) to get:
31 * ns = cycles * (10^6 * SC / cpu_khz) / SC
32 * ns = cycles * cyc2ns_scale / SC
33 *
34 * And since SC is a constant power of two, we can convert the div
35 * into a shift.
36 *
37 * We can use khz divisor instead of mhz to keep a better precision, since
38 * cyc2ns_scale is limited to 10^6 * 2^10, which fits in 32 bits.
39 * (mathieu.desnoyers@polymtl.ca)
40 *
41 * -johnstul@us.ibm.com "math is hard, lets go shopping!"
42 */
43DEFINE_PER_CPU(unsigned long, cyc2ns);
john stultzc37e7bb2007-02-16 01:28:19 -080044
Guillaume Chazarain53d517c2008-01-30 13:30:06 +010045static void set_cyc2ns_scale(unsigned long cpu_khz, int cpu)
john stultzc37e7bb2007-02-16 01:28:19 -080046{
Guillaume Chazarain53d517c2008-01-30 13:30:06 +010047 unsigned long flags, prev_scale, *scale;
48 unsigned long long tsc_now, ns_now;
john stultzc37e7bb2007-02-16 01:28:19 -080049
Guillaume Chazarain53d517c2008-01-30 13:30:06 +010050 local_irq_save(flags);
51 sched_clock_idle_sleep_event();
52
53 scale = &per_cpu(cyc2ns, cpu);
54
55 rdtscll(tsc_now);
56 ns_now = __cycles_2_ns(tsc_now);
57
58 prev_scale = *scale;
59 if (cpu_khz)
60 *scale = (NSEC_PER_MSEC << CYC2NS_SCALE_FACTOR)/cpu_khz;
61
62 sched_clock_idle_wakeup_event(0);
63 local_irq_restore(flags);
john stultzc37e7bb2007-02-16 01:28:19 -080064}
65
Glauber de Oliveira Costa16e20112008-01-30 13:31:03 +010066unsigned long long native_sched_clock(void)
john stultzc37e7bb2007-02-16 01:28:19 -080067{
68 unsigned long a = 0;
69
70 /* Could do CPU core sync here. Opteron can execute rdtsc speculatively,
71 * which means it is not completely exact and may not be monotonous
72 * between CPUs. But the errors should be too small to matter for
73 * scheduling purposes.
74 */
75
76 rdtscll(a);
77 return cycles_2_ns(a);
78}
79
Glauber de Oliveira Costa16e20112008-01-30 13:31:03 +010080/* We need to define a real function for sched_clock, to override the
81 weak default version */
82#ifdef CONFIG_PARAVIRT
83unsigned long long sched_clock(void)
84{
85 return paravirt_sched_clock();
86}
87#else
88unsigned long long
89sched_clock(void) __attribute__((alias("native_sched_clock")));
90#endif
91
92
john stultz14899392007-02-16 01:28:20 -080093static int tsc_unstable;
94
Glauber de Oliveira Costadbae5952008-01-30 13:33:24 +010095int check_tsc_unstable(void)
john stultz14899392007-02-16 01:28:20 -080096{
97 return tsc_unstable;
98}
Glauber de Oliveira Costadbae5952008-01-30 13:33:24 +010099EXPORT_SYMBOL_GPL(check_tsc_unstable);
100
john stultzc37e7bb2007-02-16 01:28:19 -0800101#ifdef CONFIG_CPU_FREQ
102
103/* Frequency scaling support. Adjust the TSC based timer when the cpu frequency
104 * changes.
105 *
106 * RED-PEN: On SMP we assume all CPUs run with the same frequency. It's
107 * not that important because current Opteron setups do not support
108 * scaling on SMP anyroads.
109 *
110 * Should fix up last_tsc too. Currently gettimeofday in the
111 * first tick after the change will be slightly wrong.
112 */
113
Thomas Gleixner7ff98472007-07-21 17:10:13 +0200114static unsigned int ref_freq;
115static unsigned long loops_per_jiffy_ref;
116static unsigned long tsc_khz_ref;
john stultzc37e7bb2007-02-16 01:28:19 -0800117
118static int time_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
119 void *data)
120{
121 struct cpufreq_freqs *freq = data;
122 unsigned long *lpj, dummy;
123
Mike Travis92cb7612007-10-19 20:35:04 +0200124 if (cpu_has(&cpu_data(freq->cpu), X86_FEATURE_CONSTANT_TSC))
john stultzc37e7bb2007-02-16 01:28:19 -0800125 return 0;
126
127 lpj = &dummy;
128 if (!(freq->flags & CPUFREQ_CONST_LOOPS))
129#ifdef CONFIG_SMP
Mike Travis92cb7612007-10-19 20:35:04 +0200130 lpj = &cpu_data(freq->cpu).loops_per_jiffy;
john stultzc37e7bb2007-02-16 01:28:19 -0800131#else
132 lpj = &boot_cpu_data.loops_per_jiffy;
133#endif
134
135 if (!ref_freq) {
136 ref_freq = freq->old;
137 loops_per_jiffy_ref = *lpj;
Joerg Roedel6b37f5a2007-05-02 19:27:06 +0200138 tsc_khz_ref = tsc_khz;
john stultzc37e7bb2007-02-16 01:28:19 -0800139 }
140 if ((val == CPUFREQ_PRECHANGE && freq->old < freq->new) ||
141 (val == CPUFREQ_POSTCHANGE && freq->old > freq->new) ||
142 (val == CPUFREQ_RESUMECHANGE)) {
143 *lpj =
144 cpufreq_scale(loops_per_jiffy_ref, ref_freq, freq->new);
145
Joerg Roedel6b37f5a2007-05-02 19:27:06 +0200146 tsc_khz = cpufreq_scale(tsc_khz_ref, ref_freq, freq->new);
john stultzc37e7bb2007-02-16 01:28:19 -0800147 if (!(freq->flags & CPUFREQ_CONST_LOOPS))
john stultz5a90cf22007-05-02 19:27:08 +0200148 mark_tsc_unstable("cpufreq changes");
john stultzc37e7bb2007-02-16 01:28:19 -0800149 }
150
Guillaume Chazarain53d517c2008-01-30 13:30:06 +0100151 preempt_disable();
152 set_cyc2ns_scale(tsc_khz_ref, smp_processor_id());
153 preempt_enable();
john stultzc37e7bb2007-02-16 01:28:19 -0800154
155 return 0;
156}
157
158static struct notifier_block time_cpufreq_notifier_block = {
159 .notifier_call = time_cpufreq_notifier
160};
161
162static int __init cpufreq_tsc(void)
163{
Thomas Gleixner7ff98472007-07-21 17:10:13 +0200164 cpufreq_register_notifier(&time_cpufreq_notifier_block,
165 CPUFREQ_TRANSITION_NOTIFIER);
john stultzc37e7bb2007-02-16 01:28:19 -0800166 return 0;
167}
168
169core_initcall(cpufreq_tsc);
170
171#endif
172
Thomas Gleixnerd3716982007-10-12 23:04:06 +0200173#define MAX_RETRIES 5
174#define SMI_TRESHOLD 50000
175
176/*
177 * Read TSC and the reference counters. Take care of SMI disturbance
178 */
179static unsigned long __init tsc_read_refs(unsigned long *pm,
180 unsigned long *hpet)
181{
182 unsigned long t1, t2;
183 int i;
184
185 for (i = 0; i < MAX_RETRIES; i++) {
Andi Kleen6d63de82008-01-30 13:32:39 +0100186 t1 = get_cycles();
Thomas Gleixnerd3716982007-10-12 23:04:06 +0200187 if (hpet)
188 *hpet = hpet_readl(HPET_COUNTER) & 0xFFFFFFFF;
189 else
190 *pm = acpi_pm_read_early();
Andi Kleen6d63de82008-01-30 13:32:39 +0100191 t2 = get_cycles();
Thomas Gleixnerd3716982007-10-12 23:04:06 +0200192 if ((t2 - t1) < SMI_TRESHOLD)
193 return t2;
194 }
195 return ULONG_MAX;
196}
197
198/**
199 * tsc_calibrate - calibrate the tsc on boot
200 */
201void __init tsc_calibrate(void)
202{
203 unsigned long flags, tsc1, tsc2, tr1, tr2, pm1, pm2, hpet1, hpet2;
Guillaume Chazarain53d517c2008-01-30 13:30:06 +0100204 int hpet = is_hpet_enabled(), cpu;
Thomas Gleixnerd3716982007-10-12 23:04:06 +0200205
206 local_irq_save(flags);
207
208 tsc1 = tsc_read_refs(&pm1, hpet ? &hpet1 : NULL);
209
210 outb((inb(0x61) & ~0x02) | 0x01, 0x61);
211
212 outb(0xb0, 0x43);
213 outb((CLOCK_TICK_RATE / (1000 / 50)) & 0xff, 0x42);
214 outb((CLOCK_TICK_RATE / (1000 / 50)) >> 8, 0x42);
Andi Kleen6d63de82008-01-30 13:32:39 +0100215 tr1 = get_cycles();
Thomas Gleixnerd3716982007-10-12 23:04:06 +0200216 while ((inb(0x61) & 0x20) == 0);
Andi Kleen6d63de82008-01-30 13:32:39 +0100217 tr2 = get_cycles();
Thomas Gleixnerd3716982007-10-12 23:04:06 +0200218
219 tsc2 = tsc_read_refs(&pm2, hpet ? &hpet2 : NULL);
220
221 local_irq_restore(flags);
222
223 /*
224 * Preset the result with the raw and inaccurate PIT
225 * calibration value
226 */
227 tsc_khz = (tr2 - tr1) / 50;
228
229 /* hpet or pmtimer available ? */
230 if (!hpet && !pm1 && !pm2) {
231 printk(KERN_INFO "TSC calibrated against PIT\n");
232 return;
233 }
234
235 /* Check, whether the sampling was disturbed by an SMI */
236 if (tsc1 == ULONG_MAX || tsc2 == ULONG_MAX) {
237 printk(KERN_WARNING "TSC calibration disturbed by SMI, "
238 "using PIT calibration result\n");
239 return;
240 }
241
242 tsc2 = (tsc2 - tsc1) * 1000000L;
243
244 if (hpet) {
245 printk(KERN_INFO "TSC calibrated against HPET\n");
246 if (hpet2 < hpet1)
247 hpet2 += 0x100000000;
248 hpet2 -= hpet1;
249 tsc1 = (hpet2 * hpet_readl(HPET_PERIOD)) / 1000000;
250 } else {
251 printk(KERN_INFO "TSC calibrated against PM_TIMER\n");
252 if (pm2 < pm1)
253 pm2 += ACPI_PM_OVRRUN;
254 pm2 -= pm1;
255 tsc1 = (pm2 * 1000000000) / PMTMR_TICKS_PER_SEC;
256 }
257
258 tsc_khz = tsc2 / tsc1;
Guillaume Chazarain53d517c2008-01-30 13:30:06 +0100259
260 for_each_possible_cpu(cpu)
261 set_cyc2ns_scale(tsc_khz, cpu);
Thomas Gleixnerd3716982007-10-12 23:04:06 +0200262}
263
john stultzc37e7bb2007-02-16 01:28:19 -0800264/*
265 * Make an educated guess if the TSC is trustworthy and synchronized
266 * over all CPUs.
267 */
268__cpuinit int unsynchronized_tsc(void)
269{
270 if (tsc_unstable)
271 return 1;
272
273#ifdef CONFIG_SMP
274 if (apic_is_clustered_box())
275 return 1;
276#endif
Andi Kleen51fc97b2008-01-30 13:32:40 +0100277
Andi Kleen32c75532008-01-30 13:32:41 +0100278 if (boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
Thomas Gleixner7ff98472007-07-21 17:10:13 +0200279 return 0;
john stultzc37e7bb2007-02-16 01:28:19 -0800280
Thomas Gleixner7ff98472007-07-21 17:10:13 +0200281 /* Assume multi socket systems are not synchronized */
282 return num_present_cpus() > 1;
john stultzc37e7bb2007-02-16 01:28:19 -0800283}
284
285int __init notsc_setup(char *s)
286{
287 notsc = 1;
288 return 1;
289}
290
291__setup("notsc", notsc_setup);
john stultz14899392007-02-16 01:28:20 -0800292
293
294/* clock source code: */
295static cycle_t read_tsc(void)
296{
Andi Kleen6d63de82008-01-30 13:32:39 +0100297 cycle_t ret = (cycle_t)get_cycles();
john stultz14899392007-02-16 01:28:20 -0800298 return ret;
299}
300
john stultz7460ed22007-02-16 01:28:21 -0800301static cycle_t __vsyscall_fn vread_tsc(void)
302{
Andi Kleen6d63de82008-01-30 13:32:39 +0100303 cycle_t ret = (cycle_t)vget_cycles();
john stultz7460ed22007-02-16 01:28:21 -0800304 return ret;
305}
306
john stultz14899392007-02-16 01:28:20 -0800307static struct clocksource clocksource_tsc = {
308 .name = "tsc",
309 .rating = 300,
310 .read = read_tsc,
311 .mask = CLOCKSOURCE_MASK(64),
312 .shift = 22,
313 .flags = CLOCK_SOURCE_IS_CONTINUOUS |
314 CLOCK_SOURCE_MUST_VERIFY,
john stultz7460ed22007-02-16 01:28:21 -0800315 .vread = vread_tsc,
john stultz14899392007-02-16 01:28:20 -0800316};
317
john stultz5a90cf22007-05-02 19:27:08 +0200318void mark_tsc_unstable(char *reason)
john stultz14899392007-02-16 01:28:20 -0800319{
320 if (!tsc_unstable) {
321 tsc_unstable = 1;
john stultz5a90cf22007-05-02 19:27:08 +0200322 printk("Marking TSC unstable due to %s\n", reason);
john stultz14899392007-02-16 01:28:20 -0800323 /* Change only the rating, when not registered */
324 if (clocksource_tsc.mult)
325 clocksource_change_rating(&clocksource_tsc, 0);
326 else
327 clocksource_tsc.rating = 0;
328 }
329}
330EXPORT_SYMBOL_GPL(mark_tsc_unstable);
331
john stultz6bb74df2007-03-05 00:30:50 -0800332void __init init_tsc_clocksource(void)
john stultz14899392007-02-16 01:28:20 -0800333{
334 if (!notsc) {
Joerg Roedel6b37f5a2007-05-02 19:27:06 +0200335 clocksource_tsc.mult = clocksource_khz2mult(tsc_khz,
john stultz14899392007-02-16 01:28:20 -0800336 clocksource_tsc.shift);
337 if (check_tsc_unstable())
338 clocksource_tsc.rating = 0;
339
john stultz6bb74df2007-03-05 00:30:50 -0800340 clocksource_register(&clocksource_tsc);
john stultz14899392007-02-16 01:28:20 -0800341 }
john stultz14899392007-02-16 01:28:20 -0800342}