2 * This code largely moved from arch/i386/kernel/timer/timer_tsc.c
3 * which was originally moved from arch/i386/kernel/time.c.
4 * See comments there for proper credits.
7 #include <linux/clocksource.h>
8 #include <linux/workqueue.h>
9 #include <linux/cpufreq.h>
10 #include <linux/jiffies.h>
11 #include <linux/init.h>
12 #include <linux/dmi.h>
14 #include <asm/delay.h>
17 #include <asm/timer.h>
19 #include "mach_timer.h"
21 static int tsc_enabled
;
24 * On some systems the TSC frequency does not
25 * change with the cpu frequency. So we need
26 * an extra value to store the TSC freq
33 static int __init
tsc_setup(char *str
)
35 printk(KERN_WARNING
"notsc: Kernel compiled with CONFIG_X86_TSC, "
36 "cannot disable TSC.\n");
41 * disable flag for tsc. Takes effect by clearing the TSC cpu flag
44 static int __init
tsc_setup(char *str
)
52 __setup("notsc", tsc_setup
);
55 * code to mark and check if the TSC is unstable
56 * due to cpufreq or due to unsynced TSCs
58 static int tsc_unstable
;
60 static inline int check_tsc_unstable(void)
65 /* Accellerators for sched_clock()
66 * convert from cycles(64bits) => nanoseconds (64bits)
68 * ns = cycles / (freq / ns_per_sec)
69 * ns = cycles * (ns_per_sec / freq)
70 * ns = cycles * (10^9 / (cpu_khz * 10^3))
71 * ns = cycles * (10^6 / cpu_khz)
73 * Then we use scaling math (suggested by george@mvista.com) to get:
74 * ns = cycles * (10^6 * SC / cpu_khz) / SC
75 * ns = cycles * cyc2ns_scale / SC
77 * And since SC is a constant power of two, we can convert the div
80 * We can use khz divisor instead of mhz to keep a better percision, since
81 * cyc2ns_scale is limited to 10^6 * 2^10, which fits in 32 bits.
82 * (mathieu.desnoyers@polymtl.ca)
84 * -johnstul@us.ibm.com "math is hard, lets go shopping!"
86 static unsigned long cyc2ns_scale __read_mostly
;
88 #define CYC2NS_SCALE_FACTOR 10 /* 2^10, carefully chosen */
90 static inline void set_cyc2ns_scale(unsigned long cpu_khz
)
92 cyc2ns_scale
= (1000000 << CYC2NS_SCALE_FACTOR
)/cpu_khz
;
95 static inline unsigned long long cycles_2_ns(unsigned long long cyc
)
97 return (cyc
* cyc2ns_scale
) >> CYC2NS_SCALE_FACTOR
;
101 * Scheduler clock - returns current time in nanosec units.
103 unsigned long long sched_clock(void)
105 unsigned long long this_offset
;
108 * Fall back to jiffies if there's no TSC available:
110 if (unlikely(!tsc_enabled
))
111 /* No locking but a rare wrong value is not a big deal: */
112 return (jiffies_64
- INITIAL_JIFFIES
) * (1000000000 / HZ
);
114 /* read the Time Stamp Counter: */
115 get_scheduled_cycles(this_offset
);
117 /* return the value in ns */
118 return cycles_2_ns(this_offset
);
121 unsigned long native_calculate_cpu_khz(void)
123 unsigned long long start
, end
;
125 u64 delta64
= (u64
)ULLONG_MAX
;
129 local_irq_save(flags
);
131 /* run 3 times to ensure the cache is warm */
132 for (i
= 0; i
< 3; i
++) {
133 mach_prepare_counter();
135 mach_countup(&count
);
137 delta64
= min(delta64
, (end
- start
));
140 * Error: ECTCNEVERSET
141 * The CTC wasn't reliable: we got a hit on the very first read,
142 * or the CPU was so fast/slow that the quotient wouldn't fit in
148 /* cpu freq too fast: */
149 if (delta64
> (1ULL<<32))
152 /* cpu freq too slow: */
153 if (delta64
<= CALIBRATE_TIME_MSEC
)
156 delta64
+= CALIBRATE_TIME_MSEC
/2; /* round for do_div */
157 do_div(delta64
,CALIBRATE_TIME_MSEC
);
159 local_irq_restore(flags
);
160 return (unsigned long)delta64
;
162 local_irq_restore(flags
);
166 int recalibrate_cpu_khz(void)
169 unsigned long cpu_khz_old
= cpu_khz
;
172 cpu_khz
= calculate_cpu_khz();
174 cpu_data
[0].loops_per_jiffy
=
175 cpufreq_scale(cpu_data
[0].loops_per_jiffy
,
176 cpu_khz_old
, cpu_khz
);
185 EXPORT_SYMBOL(recalibrate_cpu_khz
);
187 #ifdef CONFIG_CPU_FREQ
190 * if the CPU frequency is scaled, TSC-based delays will need a different
191 * loops_per_jiffy value to function properly.
193 static unsigned int ref_freq
= 0;
194 static unsigned long loops_per_jiffy_ref
= 0;
195 static unsigned long cpu_khz_ref
= 0;
198 time_cpufreq_notifier(struct notifier_block
*nb
, unsigned long val
, void *data
)
200 struct cpufreq_freqs
*freq
= data
;
204 ref_freq
= freq
->new;
207 ref_freq
= freq
->old
;
208 loops_per_jiffy_ref
= cpu_data
[freq
->cpu
].loops_per_jiffy
;
209 cpu_khz_ref
= cpu_khz
;
212 if ((val
== CPUFREQ_PRECHANGE
&& freq
->old
< freq
->new) ||
213 (val
== CPUFREQ_POSTCHANGE
&& freq
->old
> freq
->new) ||
214 (val
== CPUFREQ_RESUMECHANGE
)) {
215 if (!(freq
->flags
& CPUFREQ_CONST_LOOPS
))
216 cpu_data
[freq
->cpu
].loops_per_jiffy
=
217 cpufreq_scale(loops_per_jiffy_ref
,
218 ref_freq
, freq
->new);
222 if (num_online_cpus() == 1)
223 cpu_khz
= cpufreq_scale(cpu_khz_ref
,
224 ref_freq
, freq
->new);
225 if (!(freq
->flags
& CPUFREQ_CONST_LOOPS
)) {
227 set_cyc2ns_scale(cpu_khz
);
229 * TSC based sched_clock turns
232 mark_tsc_unstable("cpufreq changes");
240 static struct notifier_block time_cpufreq_notifier_block
= {
241 .notifier_call
= time_cpufreq_notifier
244 static int __init
cpufreq_tsc(void)
246 return cpufreq_register_notifier(&time_cpufreq_notifier_block
,
247 CPUFREQ_TRANSITION_NOTIFIER
);
249 core_initcall(cpufreq_tsc
);
253 /* clock source code */
255 static unsigned long current_tsc_khz
= 0;
257 static cycle_t
read_tsc(void)
266 static struct clocksource clocksource_tsc
= {
270 .mask
= CLOCKSOURCE_MASK(64),
271 .mult
= 0, /* to be set */
273 .flags
= CLOCK_SOURCE_IS_CONTINUOUS
|
274 CLOCK_SOURCE_MUST_VERIFY
,
277 void mark_tsc_unstable(char *reason
)
282 printk("Marking TSC unstable due to: %s.\n", reason
);
283 /* Can be called before registration */
284 if (clocksource_tsc
.mult
)
285 clocksource_change_rating(&clocksource_tsc
, 0);
287 clocksource_tsc
.rating
= 0;
290 EXPORT_SYMBOL_GPL(mark_tsc_unstable
);
292 static int __init
dmi_mark_tsc_unstable(struct dmi_system_id
*d
)
294 printk(KERN_NOTICE
"%s detected: marking TSC unstable.\n",
300 /* List of systems that have known TSC problems */
301 static struct dmi_system_id __initdata bad_tsc_dmi_table
[] = {
303 .callback
= dmi_mark_tsc_unstable
,
304 .ident
= "IBM Thinkpad 380XD",
306 DMI_MATCH(DMI_BOARD_VENDOR
, "IBM"),
307 DMI_MATCH(DMI_BOARD_NAME
, "2635FA0"),
314 * Make an educated guess if the TSC is trustworthy and synchronized
317 __cpuinit
int unsynchronized_tsc(void)
319 if (!cpu_has_tsc
|| tsc_unstable
)
322 * Intel systems are normally all synchronized.
323 * Exceptions must mark TSC as unstable:
325 if (boot_cpu_data
.x86_vendor
!= X86_VENDOR_INTEL
) {
326 /* assume multi socket systems are not synchronized: */
327 if (num_possible_cpus() > 1)
334 * Geode_LX - the OLPC CPU has a possibly a very reliable TSC
336 #ifdef CONFIG_MGEODE_LX
337 /* RTSC counts during suspend */
338 #define RTSC_SUSP 0x100
340 static void __init
check_geode_tsc_reliable(void)
344 rdmsrl(MSR_GEODE_BUSCONT_CONF0
, val
);
345 if ((val
& RTSC_SUSP
))
346 clocksource_tsc
.flags
&= ~CLOCK_SOURCE_MUST_VERIFY
;
349 static inline void check_geode_tsc_reliable(void) { }
353 void __init
tsc_init(void)
355 if (!cpu_has_tsc
|| tsc_disable
)
358 cpu_khz
= calculate_cpu_khz();
364 printk("Detected %lu.%03lu MHz processor.\n",
365 (unsigned long)cpu_khz
/ 1000,
366 (unsigned long)cpu_khz
% 1000);
368 set_cyc2ns_scale(cpu_khz
);
371 /* Check and install the TSC clocksource */
372 dmi_check_system(bad_tsc_dmi_table
);
374 unsynchronized_tsc();
375 check_geode_tsc_reliable();
376 current_tsc_khz
= tsc_khz
;
377 clocksource_tsc
.mult
= clocksource_khz2mult(current_tsc_khz
,
378 clocksource_tsc
.shift
);
379 /* lower the rating if we already know its unstable: */
380 if (check_tsc_unstable()) {
381 clocksource_tsc
.rating
= 0;
382 clocksource_tsc
.flags
&= ~CLOCK_SOURCE_IS_CONTINUOUS
;
386 clocksource_register(&clocksource_tsc
);
392 * Set the tsc_disable flag if there's no TSC support, this
393 * makes it a fast flag for the kernel to see whether it
394 * should be using the TSC.