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>
12 #include <asm/timex.h>
13 #include <asm/timer.h>
14 #include <asm/vgtod.h>
16 static int notsc __initdata = 0;
18 unsigned int cpu_khz; /* TSC clocks / usec, not used here */
19 EXPORT_SYMBOL(cpu_khz);
21 EXPORT_SYMBOL(tsc_khz);
23 /* Accelerators for sched_clock()
24 * convert from cycles(64bits) => nanoseconds (64bits)
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)
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
35 * And since SC is a constant power of two, we can convert the div
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)
42 * -johnstul@us.ibm.com "math is hard, lets go shopping!"
44 DEFINE_PER_CPU(unsigned long, cyc2ns);
46 static void set_cyc2ns_scale(unsigned long cpu_khz, int cpu)
48 unsigned long long tsc_now, ns_now;
49 unsigned long flags, *scale;
51 local_irq_save(flags);
52 sched_clock_idle_sleep_event();
54 scale = &per_cpu(cyc2ns, cpu);
57 ns_now = __cycles_2_ns(tsc_now);
60 *scale = (NSEC_PER_MSEC << CYC2NS_SCALE_FACTOR)/cpu_khz;
62 sched_clock_idle_wakeup_event(0);
63 local_irq_restore(flags);
66 unsigned long long native_sched_clock(void)
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.
77 return cycles_2_ns(a);
80 /* We need to define a real function for sched_clock, to override the
81 weak default version */
82 #ifdef CONFIG_PARAVIRT
83 unsigned long long sched_clock(void)
85 return paravirt_sched_clock();
89 sched_clock(void) __attribute__((alias("native_sched_clock")));
93 static int tsc_unstable;
95 int check_tsc_unstable(void)
99 EXPORT_SYMBOL_GPL(check_tsc_unstable);
101 #ifdef CONFIG_CPU_FREQ
103 /* Frequency scaling support. Adjust the TSC based timer when the cpu frequency
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.
110 * Should fix up last_tsc too. Currently gettimeofday in the
111 * first tick after the change will be slightly wrong.
114 static unsigned int ref_freq;
115 static unsigned long loops_per_jiffy_ref;
116 static unsigned long tsc_khz_ref;
118 static int time_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
121 struct cpufreq_freqs *freq = data;
122 unsigned long *lpj, dummy;
124 if (cpu_has(&cpu_data(freq->cpu), X86_FEATURE_CONSTANT_TSC))
128 if (!(freq->flags & CPUFREQ_CONST_LOOPS))
130 lpj = &cpu_data(freq->cpu).loops_per_jiffy;
132 lpj = &boot_cpu_data.loops_per_jiffy;
136 ref_freq = freq->old;
137 loops_per_jiffy_ref = *lpj;
138 tsc_khz_ref = tsc_khz;
140 if ((val == CPUFREQ_PRECHANGE && freq->old < freq->new) ||
141 (val == CPUFREQ_POSTCHANGE && freq->old > freq->new) ||
142 (val == CPUFREQ_RESUMECHANGE)) {
144 cpufreq_scale(loops_per_jiffy_ref, ref_freq, freq->new);
146 tsc_khz = cpufreq_scale(tsc_khz_ref, ref_freq, freq->new);
147 if (!(freq->flags & CPUFREQ_CONST_LOOPS))
148 mark_tsc_unstable("cpufreq changes");
151 set_cyc2ns_scale(tsc_khz_ref, freq->cpu);
156 static struct notifier_block time_cpufreq_notifier_block = {
157 .notifier_call = time_cpufreq_notifier
160 static int __init cpufreq_tsc(void)
162 cpufreq_register_notifier(&time_cpufreq_notifier_block,
163 CPUFREQ_TRANSITION_NOTIFIER);
167 core_initcall(cpufreq_tsc);
171 #define MAX_RETRIES 5
172 #define SMI_TRESHOLD 50000
175 * Read TSC and the reference counters. Take care of SMI disturbance
177 static unsigned long __init tsc_read_refs(unsigned long *pm,
180 unsigned long t1, t2;
183 for (i = 0; i < MAX_RETRIES; i++) {
186 *hpet = hpet_readl(HPET_COUNTER) & 0xFFFFFFFF;
188 *pm = acpi_pm_read_early();
190 if ((t2 - t1) < SMI_TRESHOLD)
197 * tsc_calibrate - calibrate the tsc on boot
199 void __init tsc_calibrate(void)
201 unsigned long flags, tsc1, tsc2, tr1, tr2, pm1, pm2, hpet1, hpet2;
202 int hpet = is_hpet_enabled(), cpu;
204 local_irq_save(flags);
206 tsc1 = tsc_read_refs(&pm1, hpet ? &hpet1 : NULL);
208 outb((inb(0x61) & ~0x02) | 0x01, 0x61);
211 outb((CLOCK_TICK_RATE / (1000 / 50)) & 0xff, 0x42);
212 outb((CLOCK_TICK_RATE / (1000 / 50)) >> 8, 0x42);
214 while ((inb(0x61) & 0x20) == 0);
217 tsc2 = tsc_read_refs(&pm2, hpet ? &hpet2 : NULL);
219 local_irq_restore(flags);
222 * Preset the result with the raw and inaccurate PIT
225 tsc_khz = (tr2 - tr1) / 50;
227 /* hpet or pmtimer available ? */
228 if (!hpet && !pm1 && !pm2) {
229 printk(KERN_INFO "TSC calibrated against PIT\n");
233 /* Check, whether the sampling was disturbed by an SMI */
234 if (tsc1 == ULONG_MAX || tsc2 == ULONG_MAX) {
235 printk(KERN_WARNING "TSC calibration disturbed by SMI, "
236 "using PIT calibration result\n");
240 tsc2 = (tsc2 - tsc1) * 1000000L;
243 printk(KERN_INFO "TSC calibrated against HPET\n");
245 hpet2 += 0x100000000UL;
247 tsc1 = (hpet2 * hpet_readl(HPET_PERIOD)) / 1000000;
249 printk(KERN_INFO "TSC calibrated against PM_TIMER\n");
251 pm2 += ACPI_PM_OVRRUN;
253 tsc1 = (pm2 * 1000000000) / PMTMR_TICKS_PER_SEC;
256 tsc_khz = tsc2 / tsc1;
259 for_each_possible_cpu(cpu)
260 set_cyc2ns_scale(tsc_khz, cpu);
264 * Make an educated guess if the TSC is trustworthy and synchronized
267 __cpuinit int unsynchronized_tsc(void)
273 if (apic_is_clustered_box())
277 if (boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
280 /* Assume multi socket systems are not synchronized */
281 return num_present_cpus() > 1;
284 int __init notsc_setup(char *s)
290 __setup("notsc", notsc_setup);
292 static struct clocksource clocksource_tsc;
295 * We compare the TSC to the cycle_last value in the clocksource
296 * structure to avoid a nasty time-warp. This can be observed in a
297 * very small window right after one CPU updated cycle_last under
298 * xtime/vsyscall_gtod lock and the other CPU reads a TSC value which
299 * is smaller than the cycle_last reference value due to a TSC which
300 * is slighty behind. This delta is nowhere else observable, but in
301 * that case it results in a forward time jump in the range of hours
302 * due to the unsigned delta calculation of the time keeping core
303 * code, which is necessary to support wrapping clocksources like pm
306 static cycle_t read_tsc(void)
308 cycle_t ret = (cycle_t)get_cycles();
310 return ret >= clocksource_tsc.cycle_last ?
311 ret : clocksource_tsc.cycle_last;
314 static cycle_t __vsyscall_fn vread_tsc(void)
316 cycle_t ret = (cycle_t)vget_cycles();
318 return ret >= __vsyscall_gtod_data.clock.cycle_last ?
319 ret : __vsyscall_gtod_data.clock.cycle_last;
322 static struct clocksource clocksource_tsc = {
326 .mask = CLOCKSOURCE_MASK(64),
328 .flags = CLOCK_SOURCE_IS_CONTINUOUS |
329 CLOCK_SOURCE_MUST_VERIFY,
333 void mark_tsc_unstable(char *reason)
337 printk("Marking TSC unstable due to %s\n", reason);
338 /* Change only the rating, when not registered */
339 if (clocksource_tsc.mult)
340 clocksource_change_rating(&clocksource_tsc, 0);
342 clocksource_tsc.rating = 0;
345 EXPORT_SYMBOL_GPL(mark_tsc_unstable);
347 void __init init_tsc_clocksource(void)
350 clocksource_tsc.mult = clocksource_khz2mult(tsc_khz,
351 clocksource_tsc.shift);
352 if (check_tsc_unstable())
353 clocksource_tsc.rating = 0;
355 clocksource_register(&clocksource_tsc);