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 extern int tsc_unstable;
17 extern int tsc_disabled;
19 /* Accelerators for sched_clock()
20 * convert from cycles(64bits) => nanoseconds (64bits)
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)
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
31 * And since SC is a constant power of two, we can convert the div
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)
38 * -johnstul@us.ibm.com "math is hard, lets go shopping!"
41 DEFINE_PER_CPU(unsigned long, cyc2ns);
43 void set_cyc2ns_scale(unsigned long cpu_khz, int cpu)
45 unsigned long long tsc_now, ns_now;
46 unsigned long flags, *scale;
48 local_irq_save(flags);
49 sched_clock_idle_sleep_event();
51 scale = &per_cpu(cyc2ns, cpu);
54 ns_now = __cycles_2_ns(tsc_now);
57 *scale = (NSEC_PER_MSEC << CYC2NS_SCALE_FACTOR)/cpu_khz;
59 sched_clock_idle_wakeup_event(0);
60 local_irq_restore(flags);
63 #ifdef CONFIG_CPU_FREQ
65 /* Frequency scaling support. Adjust the TSC based timer when the cpu frequency
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.
72 * Should fix up last_tsc too. Currently gettimeofday in the
73 * first tick after the change will be slightly wrong.
76 static unsigned int ref_freq;
77 static unsigned long loops_per_jiffy_ref;
78 static unsigned long tsc_khz_ref;
80 static int time_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
83 struct cpufreq_freqs *freq = data;
84 unsigned long *lpj, dummy;
86 if (cpu_has(&cpu_data(freq->cpu), X86_FEATURE_CONSTANT_TSC))
90 if (!(freq->flags & CPUFREQ_CONST_LOOPS))
92 lpj = &cpu_data(freq->cpu).loops_per_jiffy;
94 lpj = &boot_cpu_data.loops_per_jiffy;
99 loops_per_jiffy_ref = *lpj;
100 tsc_khz_ref = tsc_khz;
102 if ((val == CPUFREQ_PRECHANGE && freq->old < freq->new) ||
103 (val == CPUFREQ_POSTCHANGE && freq->old > freq->new) ||
104 (val == CPUFREQ_RESUMECHANGE)) {
106 cpufreq_scale(loops_per_jiffy_ref, ref_freq, freq->new);
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");
113 set_cyc2ns_scale(tsc_khz_ref, freq->cpu);
118 static struct notifier_block time_cpufreq_notifier_block = {
119 .notifier_call = time_cpufreq_notifier
122 static int __init cpufreq_tsc(void)
124 cpufreq_register_notifier(&time_cpufreq_notifier_block,
125 CPUFREQ_TRANSITION_NOTIFIER);
129 core_initcall(cpufreq_tsc);
134 * Make an educated guess if the TSC is trustworthy and synchronized
137 __cpuinit int unsynchronized_tsc(void)
143 if (apic_is_clustered_box())
147 if (boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
150 /* Assume multi socket systems are not synchronized */
151 return num_present_cpus() > 1;
154 static struct clocksource clocksource_tsc;
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
168 static cycle_t read_tsc(void)
170 cycle_t ret = (cycle_t)get_cycles();
172 return ret >= clocksource_tsc.cycle_last ?
173 ret : clocksource_tsc.cycle_last;
176 static cycle_t __vsyscall_fn vread_tsc(void)
178 cycle_t ret = (cycle_t)vget_cycles();
180 return ret >= __vsyscall_gtod_data.clock.cycle_last ?
181 ret : __vsyscall_gtod_data.clock.cycle_last;
184 static struct clocksource clocksource_tsc = {
188 .mask = CLOCKSOURCE_MASK(64),
190 .flags = CLOCK_SOURCE_IS_CONTINUOUS |
191 CLOCK_SOURCE_MUST_VERIFY,
195 void mark_tsc_unstable(char *reason)
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);
204 clocksource_tsc.rating = 0;
207 EXPORT_SYMBOL_GPL(mark_tsc_unstable);
209 void __init init_tsc_clocksource(void)
211 if (tsc_disabled > 0)
214 clocksource_tsc.mult = clocksource_khz2mult(tsc_khz,
215 clocksource_tsc.shift);
216 if (check_tsc_unstable())
217 clocksource_tsc.rating = 0;
219 clocksource_register(&clocksource_tsc);