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>
10 #include <asm/timex.h>
12 static int notsc __initdata = 0;
14 unsigned int cpu_khz; /* TSC clocks / usec, not used here */
15 EXPORT_SYMBOL(cpu_khz);
17 static unsigned int cyc2ns_scale __read_mostly;
19 void set_cyc2ns_scale(unsigned long khz)
21 cyc2ns_scale = (NSEC_PER_MSEC << NS_SCALE) / khz;
24 static unsigned long long cycles_2_ns(unsigned long long cyc)
26 return (cyc * cyc2ns_scale) >> NS_SCALE;
29 unsigned long long sched_clock(void)
33 /* Could do CPU core sync here. Opteron can execute rdtsc speculatively,
34 * which means it is not completely exact and may not be monotonous
35 * between CPUs. But the errors should be too small to matter for
36 * scheduling purposes.
40 return cycles_2_ns(a);
43 static int tsc_unstable;
45 static inline int check_tsc_unstable(void)
49 #ifdef CONFIG_CPU_FREQ
51 /* Frequency scaling support. Adjust the TSC based timer when the cpu frequency
54 * RED-PEN: On SMP we assume all CPUs run with the same frequency. It's
55 * not that important because current Opteron setups do not support
56 * scaling on SMP anyroads.
58 * Should fix up last_tsc too. Currently gettimeofday in the
59 * first tick after the change will be slightly wrong.
62 #include <linux/workqueue.h>
64 static unsigned int cpufreq_delayed_issched = 0;
65 static unsigned int cpufreq_init = 0;
66 static struct work_struct cpufreq_delayed_get_work;
68 static void handle_cpufreq_delayed_get(struct work_struct *v)
71 for_each_online_cpu(cpu) {
74 cpufreq_delayed_issched = 0;
77 static unsigned int ref_freq = 0;
78 static unsigned long loops_per_jiffy_ref = 0;
80 static unsigned long cpu_khz_ref = 0;
82 static int time_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
85 struct cpufreq_freqs *freq = data;
86 unsigned long *lpj, dummy;
88 if (cpu_has(&cpu_data[freq->cpu], X86_FEATURE_CONSTANT_TSC))
92 if (!(freq->flags & CPUFREQ_CONST_LOOPS))
94 lpj = &cpu_data[freq->cpu].loops_per_jiffy;
96 lpj = &boot_cpu_data.loops_per_jiffy;
100 ref_freq = freq->old;
101 loops_per_jiffy_ref = *lpj;
102 cpu_khz_ref = cpu_khz;
104 if ((val == CPUFREQ_PRECHANGE && freq->old < freq->new) ||
105 (val == CPUFREQ_POSTCHANGE && freq->old > freq->new) ||
106 (val == CPUFREQ_RESUMECHANGE)) {
108 cpufreq_scale(loops_per_jiffy_ref, ref_freq, freq->new);
110 cpu_khz = cpufreq_scale(cpu_khz_ref, ref_freq, freq->new);
111 if (!(freq->flags & CPUFREQ_CONST_LOOPS))
115 set_cyc2ns_scale(cpu_khz_ref);
120 static struct notifier_block time_cpufreq_notifier_block = {
121 .notifier_call = time_cpufreq_notifier
124 static int __init cpufreq_tsc(void)
126 INIT_WORK(&cpufreq_delayed_get_work, handle_cpufreq_delayed_get);
127 if (!cpufreq_register_notifier(&time_cpufreq_notifier_block,
128 CPUFREQ_TRANSITION_NOTIFIER))
133 core_initcall(cpufreq_tsc);
137 static int tsc_unstable = 0;
140 * Make an educated guess if the TSC is trustworthy and synchronized
143 __cpuinit int unsynchronized_tsc(void)
149 if (apic_is_clustered_box())
152 /* Most intel systems have synchronized TSCs except for
153 multi node systems */
154 if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) {
156 /* But TSC doesn't tick in C3 so don't use it there */
157 if (acpi_gbl_FADT.header.length > 0 && acpi_gbl_FADT.C3latency < 1000)
163 /* Assume multi socket systems are not synchronized */
164 return num_present_cpus() > 1;
167 int __init notsc_setup(char *s)
173 __setup("notsc", notsc_setup);
176 /* clock source code: */
177 static cycle_t read_tsc(void)
179 cycle_t ret = (cycle_t)get_cycles_sync();
183 static cycle_t __vsyscall_fn vread_tsc(void)
185 cycle_t ret = (cycle_t)get_cycles_sync();
189 static struct clocksource clocksource_tsc = {
193 .mask = CLOCKSOURCE_MASK(64),
195 .flags = CLOCK_SOURCE_IS_CONTINUOUS |
196 CLOCK_SOURCE_MUST_VERIFY,
200 void mark_tsc_unstable(void)
204 /* Change only the rating, when not registered */
205 if (clocksource_tsc.mult)
206 clocksource_change_rating(&clocksource_tsc, 0);
208 clocksource_tsc.rating = 0;
211 EXPORT_SYMBOL_GPL(mark_tsc_unstable);
213 void __init init_tsc_clocksource(void)
216 clocksource_tsc.mult = clocksource_khz2mult(cpu_khz,
217 clocksource_tsc.shift);
218 if (check_tsc_unstable())
219 clocksource_tsc.rating = 0;
221 clocksource_register(&clocksource_tsc);