[PATCH] time: x86_64: convert x86_64 to use GENERIC_TIME
[linux-2.6/mini2440.git] / arch / x86_64 / kernel / tsc.c
blob8c92f2fe7e2eeec425dc9f0485e6818942f70cb2
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)
31 unsigned long a = 0;
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.
39 rdtscll(a);
40 return cycles_2_ns(a);
43 static int tsc_unstable;
45 static inline int check_tsc_unstable(void)
47 return tsc_unstable;
49 #ifdef CONFIG_CPU_FREQ
51 /* Frequency scaling support. Adjust the TSC based timer when the cpu frequency
52 * changes.
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)
70 unsigned int cpu;
71 for_each_online_cpu(cpu) {
72 cpufreq_get(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,
83 void *data)
85 struct cpufreq_freqs *freq = data;
86 unsigned long *lpj, dummy;
88 if (cpu_has(&cpu_data[freq->cpu], X86_FEATURE_CONSTANT_TSC))
89 return 0;
91 lpj = &dummy;
92 if (!(freq->flags & CPUFREQ_CONST_LOOPS))
93 #ifdef CONFIG_SMP
94 lpj = &cpu_data[freq->cpu].loops_per_jiffy;
95 #else
96 lpj = &boot_cpu_data.loops_per_jiffy;
97 #endif
99 if (!ref_freq) {
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)) {
107 *lpj =
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))
112 mark_tsc_unstable();
115 set_cyc2ns_scale(cpu_khz_ref);
117 return 0;
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))
129 cpufreq_init = 1;
130 return 0;
133 core_initcall(cpufreq_tsc);
135 #endif
137 static int tsc_unstable = 0;
140 * Make an educated guess if the TSC is trustworthy and synchronized
141 * over all CPUs.
143 __cpuinit int unsynchronized_tsc(void)
145 if (tsc_unstable)
146 return 1;
148 #ifdef CONFIG_SMP
149 if (apic_is_clustered_box())
150 return 1;
151 #endif
152 /* Most intel systems have synchronized TSCs except for
153 multi node systems */
154 if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) {
155 #ifdef CONFIG_ACPI
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)
158 return 1;
159 #endif
160 return 0;
163 /* Assume multi socket systems are not synchronized */
164 return num_present_cpus() > 1;
167 int __init notsc_setup(char *s)
169 notsc = 1;
170 return 1;
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();
180 return ret;
183 static struct clocksource clocksource_tsc = {
184 .name = "tsc",
185 .rating = 300,
186 .read = read_tsc,
187 .mask = CLOCKSOURCE_MASK(64),
188 .shift = 22,
189 .flags = CLOCK_SOURCE_IS_CONTINUOUS |
190 CLOCK_SOURCE_MUST_VERIFY,
193 void mark_tsc_unstable(void)
195 if (!tsc_unstable) {
196 tsc_unstable = 1;
197 /* Change only the rating, when not registered */
198 if (clocksource_tsc.mult)
199 clocksource_change_rating(&clocksource_tsc, 0);
200 else
201 clocksource_tsc.rating = 0;
204 EXPORT_SYMBOL_GPL(mark_tsc_unstable);
206 static int __init init_tsc_clocksource(void)
208 if (!notsc) {
209 clocksource_tsc.mult = clocksource_khz2mult(cpu_khz,
210 clocksource_tsc.shift);
211 if (check_tsc_unstable())
212 clocksource_tsc.rating = 0;
214 return clocksource_register(&clocksource_tsc);
216 return 0;
219 module_init(init_tsc_clocksource);