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>
18 #include "mach_timer.h"
21 * On some systems the TSC frequency does not
22 * change with the cpu frequency. So we need
23 * an extra value to store the TSC freq
26 unsigned long long (*custom_sched_clock
)(void);
31 static int __init
tsc_setup(char *str
)
33 printk(KERN_WARNING
"notsc: Kernel compiled with CONFIG_X86_TSC, "
34 "cannot disable TSC.\n");
39 * disable flag for tsc. Takes effect by clearing the TSC cpu flag
42 static int __init
tsc_setup(char *str
)
50 __setup("notsc", tsc_setup
);
53 * code to mark and check if the TSC is unstable
54 * due to cpufreq or due to unsynced TSCs
56 static int tsc_unstable
;
58 static inline int check_tsc_unstable(void)
63 /* Accellerators for sched_clock()
64 * convert from cycles(64bits) => nanoseconds (64bits)
66 * ns = cycles / (freq / ns_per_sec)
67 * ns = cycles * (ns_per_sec / freq)
68 * ns = cycles * (10^9 / (cpu_khz * 10^3))
69 * ns = cycles * (10^6 / cpu_khz)
71 * Then we use scaling math (suggested by george@mvista.com) to get:
72 * ns = cycles * (10^6 * SC / cpu_khz) / SC
73 * ns = cycles * cyc2ns_scale / SC
75 * And since SC is a constant power of two, we can convert the div
78 * We can use khz divisor instead of mhz to keep a better percision, since
79 * cyc2ns_scale is limited to 10^6 * 2^10, which fits in 32 bits.
80 * (mathieu.desnoyers@polymtl.ca)
82 * -johnstul@us.ibm.com "math is hard, lets go shopping!"
84 static unsigned long cyc2ns_scale __read_mostly
;
86 #define CYC2NS_SCALE_FACTOR 10 /* 2^10, carefully chosen */
88 static inline void set_cyc2ns_scale(unsigned long cpu_khz
)
90 cyc2ns_scale
= (1000000 << CYC2NS_SCALE_FACTOR
)/cpu_khz
;
93 static inline unsigned long long cycles_2_ns(unsigned long long cyc
)
95 return (cyc
* cyc2ns_scale
) >> CYC2NS_SCALE_FACTOR
;
99 * Scheduler clock - returns current time in nanosec units.
101 unsigned long long sched_clock(void)
103 unsigned long long this_offset
;
105 if (unlikely(custom_sched_clock
))
106 return (*custom_sched_clock
)();
109 * Fall back to jiffies if there's no TSC available:
111 if (unlikely(tsc_disable
))
112 /* No locking but a rare wrong value is not a big deal: */
113 return (jiffies_64
- INITIAL_JIFFIES
) * (1000000000 / HZ
);
115 /* read the Time Stamp Counter: */
116 rdtscll(this_offset
);
118 /* return the value in ns */
119 return cycles_2_ns(this_offset
);
122 static unsigned long calculate_cpu_khz(void)
124 unsigned long long start
, end
;
130 local_irq_save(flags
);
132 /* run 3 times to ensure the cache is warm */
133 for (i
= 0; i
< 3; i
++) {
134 mach_prepare_counter();
136 mach_countup(&count
);
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 delta64
= end
- start
;
150 /* cpu freq too fast: */
151 if (delta64
> (1ULL<<32))
154 /* cpu freq too slow: */
155 if (delta64
<= CALIBRATE_TIME_MSEC
)
158 delta64
+= CALIBRATE_TIME_MSEC
/2; /* round for do_div */
159 do_div(delta64
,CALIBRATE_TIME_MSEC
);
161 local_irq_restore(flags
);
162 return (unsigned long)delta64
;
164 local_irq_restore(flags
);
168 int recalibrate_cpu_khz(void)
171 unsigned long cpu_khz_old
= cpu_khz
;
174 cpu_khz
= calculate_cpu_khz();
176 cpu_data
[0].loops_per_jiffy
=
177 cpufreq_scale(cpu_data
[0].loops_per_jiffy
,
178 cpu_khz_old
, cpu_khz
);
187 EXPORT_SYMBOL(recalibrate_cpu_khz
);
189 void __init
tsc_init(void)
191 if (!cpu_has_tsc
|| tsc_disable
)
194 cpu_khz
= calculate_cpu_khz();
200 printk("Detected %lu.%03lu MHz processor.\n",
201 (unsigned long)cpu_khz
/ 1000,
202 (unsigned long)cpu_khz
% 1000);
204 set_cyc2ns_scale(cpu_khz
);
210 * Set the tsc_disable flag if there's no TSC support, this
211 * makes it a fast flag for the kernel to see whether it
212 * should be using the TSC.
217 #ifdef CONFIG_CPU_FREQ
220 * if the CPU frequency is scaled, TSC-based delays will need a different
221 * loops_per_jiffy value to function properly.
223 static unsigned int ref_freq
= 0;
224 static unsigned long loops_per_jiffy_ref
= 0;
225 static unsigned long cpu_khz_ref
= 0;
228 time_cpufreq_notifier(struct notifier_block
*nb
, unsigned long val
, void *data
)
230 struct cpufreq_freqs
*freq
= data
;
232 if (val
!= CPUFREQ_RESUMECHANGE
&& val
!= CPUFREQ_SUSPENDCHANGE
)
233 write_seqlock_irq(&xtime_lock
);
237 ref_freq
= freq
->new;
240 ref_freq
= freq
->old
;
241 loops_per_jiffy_ref
= cpu_data
[freq
->cpu
].loops_per_jiffy
;
242 cpu_khz_ref
= cpu_khz
;
245 if ((val
== CPUFREQ_PRECHANGE
&& freq
->old
< freq
->new) ||
246 (val
== CPUFREQ_POSTCHANGE
&& freq
->old
> freq
->new) ||
247 (val
== CPUFREQ_RESUMECHANGE
)) {
248 if (!(freq
->flags
& CPUFREQ_CONST_LOOPS
))
249 cpu_data
[freq
->cpu
].loops_per_jiffy
=
250 cpufreq_scale(loops_per_jiffy_ref
,
251 ref_freq
, freq
->new);
255 if (num_online_cpus() == 1)
256 cpu_khz
= cpufreq_scale(cpu_khz_ref
,
257 ref_freq
, freq
->new);
258 if (!(freq
->flags
& CPUFREQ_CONST_LOOPS
)) {
260 set_cyc2ns_scale(cpu_khz
);
262 * TSC based sched_clock turns
270 if (val
!= CPUFREQ_RESUMECHANGE
&& val
!= CPUFREQ_SUSPENDCHANGE
)
271 write_sequnlock_irq(&xtime_lock
);
276 static struct notifier_block time_cpufreq_notifier_block
= {
277 .notifier_call
= time_cpufreq_notifier
280 static int __init
cpufreq_tsc(void)
282 return cpufreq_register_notifier(&time_cpufreq_notifier_block
,
283 CPUFREQ_TRANSITION_NOTIFIER
);
285 core_initcall(cpufreq_tsc
);
289 /* clock source code */
291 static unsigned long current_tsc_khz
= 0;
293 static cycle_t
read_tsc(void)
302 static struct clocksource clocksource_tsc
= {
306 .mask
= CLOCKSOURCE_MASK(64),
307 .mult
= 0, /* to be set */
309 .flags
= CLOCK_SOURCE_IS_CONTINUOUS
|
310 CLOCK_SOURCE_MUST_VERIFY
,
313 void mark_tsc_unstable(void)
317 /* Can be called before registration */
318 if (clocksource_tsc
.mult
)
319 clocksource_change_rating(&clocksource_tsc
, 0);
321 clocksource_tsc
.rating
= 0;
324 EXPORT_SYMBOL_GPL(mark_tsc_unstable
);
326 static int __init
dmi_mark_tsc_unstable(struct dmi_system_id
*d
)
328 printk(KERN_NOTICE
"%s detected: marking TSC unstable.\n",
334 /* List of systems that have known TSC problems */
335 static struct dmi_system_id __initdata bad_tsc_dmi_table
[] = {
337 .callback
= dmi_mark_tsc_unstable
,
338 .ident
= "IBM Thinkpad 380XD",
340 DMI_MATCH(DMI_BOARD_VENDOR
, "IBM"),
341 DMI_MATCH(DMI_BOARD_NAME
, "2635FA0"),
348 * Make an educated guess if the TSC is trustworthy and synchronized
351 __cpuinit
int unsynchronized_tsc(void)
353 if (!cpu_has_tsc
|| tsc_unstable
)
356 * Intel systems are normally all synchronized.
357 * Exceptions must mark TSC as unstable:
359 if (boot_cpu_data
.x86_vendor
!= X86_VENDOR_INTEL
) {
360 /* assume multi socket systems are not synchronized: */
361 if (num_possible_cpus() > 1)
368 * Geode_LX - the OLPC CPU has a possibly a very reliable TSC
370 #ifdef CONFIG_MGEODE_LX
371 /* RTSC counts during suspend */
372 #define RTSC_SUSP 0x100
374 static void __init
check_geode_tsc_reliable(void)
378 rdmsrl(MSR_GEODE_BUSCONT_CONF0
, val
);
379 if ((val
& RTSC_SUSP
))
380 clocksource_tsc
.flags
&= ~CLOCK_SOURCE_MUST_VERIFY
;
383 static inline void check_geode_tsc_reliable(void) { }
386 static int __init
init_tsc_clocksource(void)
389 if (cpu_has_tsc
&& tsc_khz
&& !tsc_disable
) {
390 /* check blacklist */
391 dmi_check_system(bad_tsc_dmi_table
);
393 unsynchronized_tsc();
394 check_geode_tsc_reliable();
395 current_tsc_khz
= tsc_khz
;
396 clocksource_tsc
.mult
= clocksource_khz2mult(current_tsc_khz
,
397 clocksource_tsc
.shift
);
398 /* lower the rating if we already know its unstable: */
399 if (check_tsc_unstable()) {
400 clocksource_tsc
.rating
= 0;
401 clocksource_tsc
.flags
&= ~CLOCK_SOURCE_IS_CONTINUOUS
;
404 return clocksource_register(&clocksource_tsc
);
410 module_init(init_tsc_clocksource
);