1 #include <linux/kernel.h>
2 #include <linux/sched.h>
3 #include <linux/init.h>
4 #include <linux/module.h>
5 #include <linux/timer.h>
6 #include <linux/acpi_pmtmr.h>
7 #include <linux/cpufreq.h>
11 unsigned int cpu_khz
; /* TSC clocks / usec, not used here */
12 EXPORT_SYMBOL(cpu_khz
);
14 EXPORT_SYMBOL(tsc_khz
);
17 * TSC can be unstable due to cpufreq or due to unsynced TSCs
21 /* native_sched_clock() is called before tsc_init(), so
22 we must start with the TSC soft disabled to prevent
23 erroneous rdtsc usage on !cpu_has_tsc processors */
24 int tsc_disabled
= -1;
27 * Scheduler clock - returns current time in nanosec units.
29 u64
native_sched_clock(void)
34 * Fall back to jiffies if there's no TSC available:
35 * ( But note that we still use it if the TSC is marked
36 * unstable. We do this because unlike Time Of Day,
37 * the scheduler clock tolerates small errors and it's
38 * very important for it to be as fast as the platform
41 if (unlikely(tsc_disabled
)) {
42 /* No locking but a rare wrong value is not a big deal: */
43 return (jiffies_64
- INITIAL_JIFFIES
) * (1000000000 / HZ
);
46 /* read the Time Stamp Counter: */
49 /* return the value in ns */
50 return cycles_2_ns(this_offset
);
53 /* We need to define a real function for sched_clock, to override the
54 weak default version */
55 #ifdef CONFIG_PARAVIRT
56 unsigned long long sched_clock(void)
58 return paravirt_sched_clock();
62 sched_clock(void) __attribute__((alias("native_sched_clock")));
65 int check_tsc_unstable(void)
69 EXPORT_SYMBOL_GPL(check_tsc_unstable
);
72 int __init
notsc_setup(char *str
)
74 printk(KERN_WARNING
"notsc: Kernel compiled with CONFIG_X86_TSC, "
75 "cannot disable TSC completely.\n");
81 * disable flag for tsc. Takes effect by clearing the TSC cpu flag
84 int __init
notsc_setup(char *str
)
86 setup_clear_cpu_cap(X86_FEATURE_TSC
);
91 __setup("notsc", notsc_setup
);
94 #define SMI_TRESHOLD 50000
97 * Read TSC and the reference counters. Take care of SMI disturbance
99 static u64 __init
tsc_read_refs(u64
*pm
, u64
*hpet
)
104 for (i
= 0; i
< MAX_RETRIES
; i
++) {
107 *hpet
= hpet_readl(HPET_COUNTER
) & 0xFFFFFFFF;
109 *pm
= acpi_pm_read_early();
111 if ((t2
- t1
) < SMI_TRESHOLD
)
118 * tsc_calibrate - calibrate the tsc on boot
120 static unsigned int __init
tsc_calibrate(void)
123 u64 tsc1
, tsc2
, tr1
, tr2
, delta
, pm1
, pm2
, hpet1
, hpet2
;
124 int hpet
= is_hpet_enabled();
125 unsigned int tsc_khz_val
= 0;
127 local_irq_save(flags
);
129 tsc1
= tsc_read_refs(&pm1
, hpet
? &hpet1
: NULL
);
131 outb((inb(0x61) & ~0x02) | 0x01, 0x61);
134 outb((CLOCK_TICK_RATE
/ (1000 / 50)) & 0xff, 0x42);
135 outb((CLOCK_TICK_RATE
/ (1000 / 50)) >> 8, 0x42);
137 while ((inb(0x61) & 0x20) == 0);
140 tsc2
= tsc_read_refs(&pm2
, hpet
? &hpet2
: NULL
);
142 local_irq_restore(flags
);
145 * Preset the result with the raw and inaccurate PIT
152 /* hpet or pmtimer available ? */
153 if (!hpet
&& !pm1
&& !pm2
) {
154 printk(KERN_INFO
"TSC calibrated against PIT\n");
158 /* Check, whether the sampling was disturbed by an SMI */
159 if (tsc1
== ULLONG_MAX
|| tsc2
== ULLONG_MAX
) {
160 printk(KERN_WARNING
"TSC calibration disturbed by SMI, "
161 "using PIT calibration result\n");
165 tsc2
= (tsc2
- tsc1
) * 1000000LL;
168 printk(KERN_INFO
"TSC calibrated against HPET\n");
170 hpet2
+= 0x100000000ULL
;
172 tsc1
= ((u64
)hpet2
* hpet_readl(HPET_PERIOD
));
173 do_div(tsc1
, 1000000);
175 printk(KERN_INFO
"TSC calibrated against PM_TIMER\n");
177 pm2
+= (u64
)ACPI_PM_OVRRUN
;
179 tsc1
= pm2
* 1000000000LL;
180 do_div(tsc1
, PMTMR_TICKS_PER_SEC
);
190 unsigned long native_calculate_cpu_khz(void)
192 return tsc_calibrate();
196 /* Only called from the Powernow K7 cpu freq driver */
197 int recalibrate_cpu_khz(void)
200 unsigned long cpu_khz_old
= cpu_khz
;
203 cpu_khz
= calculate_cpu_khz();
205 cpu_data(0).loops_per_jiffy
=
206 cpufreq_scale(cpu_data(0).loops_per_jiffy
,
207 cpu_khz_old
, cpu_khz
);
216 EXPORT_SYMBOL(recalibrate_cpu_khz
);
218 #endif /* CONFIG_X86_32 */
220 /* Accelerators for sched_clock()
221 * convert from cycles(64bits) => nanoseconds (64bits)
223 * ns = cycles / (freq / ns_per_sec)
224 * ns = cycles * (ns_per_sec / freq)
225 * ns = cycles * (10^9 / (cpu_khz * 10^3))
226 * ns = cycles * (10^6 / cpu_khz)
228 * Then we use scaling math (suggested by george@mvista.com) to get:
229 * ns = cycles * (10^6 * SC / cpu_khz) / SC
230 * ns = cycles * cyc2ns_scale / SC
232 * And since SC is a constant power of two, we can convert the div
235 * We can use khz divisor instead of mhz to keep a better precision, since
236 * cyc2ns_scale is limited to 10^6 * 2^10, which fits in 32 bits.
237 * (mathieu.desnoyers@polymtl.ca)
239 * -johnstul@us.ibm.com "math is hard, lets go shopping!"
242 DEFINE_PER_CPU(unsigned long, cyc2ns
);
244 void set_cyc2ns_scale(unsigned long cpu_khz
, int cpu
)
246 unsigned long long tsc_now
, ns_now
;
247 unsigned long flags
, *scale
;
249 local_irq_save(flags
);
250 sched_clock_idle_sleep_event();
252 scale
= &per_cpu(cyc2ns
, cpu
);
255 ns_now
= __cycles_2_ns(tsc_now
);
258 *scale
= (NSEC_PER_MSEC
<< CYC2NS_SCALE_FACTOR
)/cpu_khz
;
260 sched_clock_idle_wakeup_event(0);
261 local_irq_restore(flags
);
264 #ifdef CONFIG_CPU_FREQ
266 /* Frequency scaling support. Adjust the TSC based timer when the cpu frequency
269 * RED-PEN: On SMP we assume all CPUs run with the same frequency. It's
270 * not that important because current Opteron setups do not support
271 * scaling on SMP anyroads.
273 * Should fix up last_tsc too. Currently gettimeofday in the
274 * first tick after the change will be slightly wrong.
277 static unsigned int ref_freq
;
278 static unsigned long loops_per_jiffy_ref
;
279 static unsigned long tsc_khz_ref
;
281 static int time_cpufreq_notifier(struct notifier_block
*nb
, unsigned long val
,
284 struct cpufreq_freqs
*freq
= data
;
285 unsigned long *lpj
, dummy
;
287 if (cpu_has(&cpu_data(freq
->cpu
), X86_FEATURE_CONSTANT_TSC
))
291 if (!(freq
->flags
& CPUFREQ_CONST_LOOPS
))
293 lpj
= &cpu_data(freq
->cpu
).loops_per_jiffy
;
295 lpj
= &boot_cpu_data
.loops_per_jiffy
;
299 ref_freq
= freq
->old
;
300 loops_per_jiffy_ref
= *lpj
;
301 tsc_khz_ref
= tsc_khz
;
303 if ((val
== CPUFREQ_PRECHANGE
&& freq
->old
< freq
->new) ||
304 (val
== CPUFREQ_POSTCHANGE
&& freq
->old
> freq
->new) ||
305 (val
== CPUFREQ_RESUMECHANGE
)) {
306 *lpj
= cpufreq_scale(loops_per_jiffy_ref
, ref_freq
, freq
->new);
308 tsc_khz
= cpufreq_scale(tsc_khz_ref
, ref_freq
, freq
->new);
309 if (!(freq
->flags
& CPUFREQ_CONST_LOOPS
))
310 mark_tsc_unstable("cpufreq changes");
313 set_cyc2ns_scale(tsc_khz_ref
, freq
->cpu
);
318 static struct notifier_block time_cpufreq_notifier_block
= {
319 .notifier_call
= time_cpufreq_notifier
322 static int __init
cpufreq_tsc(void)
324 cpufreq_register_notifier(&time_cpufreq_notifier_block
,
325 CPUFREQ_TRANSITION_NOTIFIER
);
329 core_initcall(cpufreq_tsc
);
331 #endif /* CONFIG_CPU_FREQ */