Merge with 2.5.75.
[linux-2.6/linux-mips.git] / arch / i386 / kernel / timers / timer_tsc.c
blob9988b67dd83a64e55a2fdc5b20d2aa45fb30e203
1 /*
2 * This code largely moved from arch/i386/kernel/time.c.
3 * See comments there for proper credits.
4 */
6 #include <linux/spinlock.h>
7 #include <linux/init.h>
8 #include <linux/timex.h>
9 #include <linux/errno.h>
10 #include <linux/cpufreq.h>
11 #include <linux/string.h>
12 #include <linux/jiffies.h>
14 #include <asm/timer.h>
15 #include <asm/io.h>
16 /* processor.h for distable_tsc flag */
17 #include <asm/processor.h>
19 #include "io_ports.h"
20 #include "mach_timer.h"
22 int tsc_disable __initdata = 0;
24 extern spinlock_t i8253_lock;
26 static int use_tsc;
27 /* Number of usecs that the last interrupt was delayed */
28 static int delay_at_last_interrupt;
30 static unsigned long last_tsc_low; /* lsb 32 bits of Time Stamp Counter */
31 static unsigned long last_tsc_high; /* msb 32 bits of Time Stamp Counter */
32 static unsigned long long monotonic_base;
33 static rwlock_t monotonic_lock = RW_LOCK_UNLOCKED;
35 /* convert from cycles(64bits) => nanoseconds (64bits)
36 * basic equation:
37 * ns = cycles / (freq / ns_per_sec)
38 * ns = cycles * (ns_per_sec / freq)
39 * ns = cycles * (10^9 / (cpu_mhz * 10^6))
40 * ns = cycles * (10^3 / cpu_mhz)
42 * Then we use scaling math (suggested by george@mvista.com) to get:
43 * ns = cycles * (10^3 * SC / cpu_mhz) / SC
44 * ns = cycles * cyc2ns_scale / SC
46 * And since SC is a constant power of two, we can convert the div
47 * into a shift.
48 * -johnstul@us.ibm.com "math is hard, lets go shopping!"
50 static unsigned long cyc2ns_scale;
51 #define CYC2NS_SCALE_FACTOR 10 /* 2^10, carefully chosen */
53 static inline void set_cyc2ns_scale(unsigned long cpu_mhz)
55 cyc2ns_scale = (1000 << CYC2NS_SCALE_FACTOR)/cpu_mhz;
58 static inline unsigned long long cycles_2_ns(unsigned long long cyc)
60 return (cyc * cyc2ns_scale) >> CYC2NS_SCALE_FACTOR;
64 static int count2; /* counter for mark_offset_tsc() */
66 /* Cached *multiplier* to convert TSC counts to microseconds.
67 * (see the equation below).
68 * Equal to 2^32 * (1 / (clocks per usec) ).
69 * Initialized in time_init.
71 static unsigned long fast_gettimeoffset_quotient;
73 static unsigned long get_offset_tsc(void)
75 register unsigned long eax, edx;
77 /* Read the Time Stamp Counter */
79 rdtsc(eax,edx);
81 /* .. relative to previous jiffy (32 bits is enough) */
82 eax -= last_tsc_low; /* tsc_low delta */
85 * Time offset = (tsc_low delta) * fast_gettimeoffset_quotient
86 * = (tsc_low delta) * (usecs_per_clock)
87 * = (tsc_low delta) * (usecs_per_jiffy / clocks_per_jiffy)
89 * Using a mull instead of a divl saves up to 31 clock cycles
90 * in the critical path.
93 __asm__("mull %2"
94 :"=a" (eax), "=d" (edx)
95 :"rm" (fast_gettimeoffset_quotient),
96 "0" (eax));
98 /* our adjusted time offset in microseconds */
99 return delay_at_last_interrupt + edx;
102 static unsigned long long monotonic_clock_tsc(void)
104 unsigned long long last_offset, this_offset, base;
106 /* atomically read monotonic base & last_offset */
107 read_lock_irq(&monotonic_lock);
108 last_offset = ((unsigned long long)last_tsc_high<<32)|last_tsc_low;
109 base = monotonic_base;
110 read_unlock_irq(&monotonic_lock);
112 /* Read the Time Stamp Counter */
113 rdtscll(this_offset);
115 /* return the value in ns */
116 return base + cycles_2_ns(this_offset - last_offset);
119 static void mark_offset_tsc(void)
121 unsigned long lost,delay;
122 unsigned long delta = last_tsc_low;
123 int count;
124 int countmp;
125 static int count1 = 0;
126 unsigned long long this_offset, last_offset;
127 static int lost_count = 0;
129 write_lock(&monotonic_lock);
130 last_offset = ((unsigned long long)last_tsc_high<<32)|last_tsc_low;
132 * It is important that these two operations happen almost at
133 * the same time. We do the RDTSC stuff first, since it's
134 * faster. To avoid any inconsistencies, we need interrupts
135 * disabled locally.
139 * Interrupts are just disabled locally since the timer irq
140 * has the SA_INTERRUPT flag set. -arca
143 /* read Pentium cycle counter */
145 rdtsc(last_tsc_low, last_tsc_high);
147 spin_lock(&i8253_lock);
148 outb_p(0x00, PIT_MODE); /* latch the count ASAP */
150 count = inb_p(PIT_CH0); /* read the latched count */
151 count |= inb(PIT_CH0) << 8;
152 spin_unlock(&i8253_lock);
154 if (pit_latch_buggy) {
155 /* get center value of last 3 time lutch */
156 if ((count2 >= count && count >= count1)
157 || (count1 >= count && count >= count2)) {
158 count2 = count1; count1 = count;
159 } else if ((count1 >= count2 && count2 >= count)
160 || (count >= count2 && count2 >= count1)) {
161 countmp = count;count = count2;
162 count2 = count1;count1 = countmp;
163 } else {
164 count2 = count1; count1 = count; count = count1;
168 /* lost tick compensation */
169 delta = last_tsc_low - delta;
171 register unsigned long eax, edx;
172 eax = delta;
173 __asm__("mull %2"
174 :"=a" (eax), "=d" (edx)
175 :"rm" (fast_gettimeoffset_quotient),
176 "0" (eax));
177 delta = edx;
179 delta += delay_at_last_interrupt;
180 lost = delta/(1000000/HZ);
181 delay = delta%(1000000/HZ);
182 if (lost >= 2) {
183 jiffies += lost-1;
185 /* sanity check to ensure we're not always loosing ticks */
186 if (lost_count++ > 100) {
187 printk(KERN_WARNING "Loosing too many ticks!\n");
188 printk(KERN_WARNING "TSC cannot be used as a timesource."
189 " (Are you running with SpeedStep?)\n");
190 printk(KERN_WARNING "Falling back to a sane timesource.\n");
191 clock_fallback();
193 } else
194 lost_count = 0;
195 /* update the monotonic base value */
196 this_offset = ((unsigned long long)last_tsc_high<<32)|last_tsc_low;
197 monotonic_base += cycles_2_ns(this_offset - last_offset);
198 write_unlock(&monotonic_lock);
200 /* calculate delay_at_last_interrupt */
201 count = ((LATCH-1) - count) * TICK_SIZE;
202 delay_at_last_interrupt = (count + LATCH/2) / LATCH;
204 /* catch corner case where tick rollover occured
205 * between tsc and pit reads (as noted when
206 * usec delta is > 90% # of usecs/tick)
208 if (lost && abs(delay - delay_at_last_interrupt) > (900000/HZ))
209 jiffies++;
212 static void delay_tsc(unsigned long loops)
214 unsigned long bclock, now;
216 rdtscl(bclock);
219 rep_nop();
220 rdtscl(now);
221 } while ((now-bclock) < loops);
224 /* ------ Calibrate the TSC -------
225 * Return 2^32 * (1 / (TSC clocks per usec)) for do_fast_gettimeoffset().
226 * Too much 64-bit arithmetic here to do this cleanly in C, and for
227 * accuracy's sake we want to keep the overhead on the CTC speaker (channel 2)
228 * output busy loop as low as possible. We avoid reading the CTC registers
229 * directly because of the awkward 8-bit access mechanism of the 82C54
230 * device.
233 #define CALIBRATE_TIME (5 * 1000020/HZ)
235 unsigned long __init calibrate_tsc(void)
237 mach_prepare_counter();
240 unsigned long startlow, starthigh;
241 unsigned long endlow, endhigh;
242 unsigned long count;
244 rdtsc(startlow,starthigh);
245 mach_countup(&count);
246 rdtsc(endlow,endhigh);
248 last_tsc_low = endlow;
250 /* Error: ECTCNEVERSET */
251 if (count <= 1)
252 goto bad_ctc;
254 /* 64-bit subtract - gcc just messes up with long longs */
255 __asm__("subl %2,%0\n\t"
256 "sbbl %3,%1"
257 :"=a" (endlow), "=d" (endhigh)
258 :"g" (startlow), "g" (starthigh),
259 "0" (endlow), "1" (endhigh));
261 /* Error: ECPUTOOFAST */
262 if (endhigh)
263 goto bad_ctc;
265 /* Error: ECPUTOOSLOW */
266 if (endlow <= CALIBRATE_TIME)
267 goto bad_ctc;
269 __asm__("divl %2"
270 :"=a" (endlow), "=d" (endhigh)
271 :"r" (endlow), "0" (0), "1" (CALIBRATE_TIME));
273 return endlow;
277 * The CTC wasn't reliable: we got a hit on the very first read,
278 * or the CPU was so fast/slow that the quotient wouldn't fit in
279 * 32 bits..
281 bad_ctc:
282 return 0;
286 #ifdef CONFIG_CPU_FREQ
287 static unsigned int ref_freq = 0;
288 static unsigned long loops_per_jiffy_ref = 0;
290 #ifndef CONFIG_SMP
291 static unsigned long fast_gettimeoffset_ref = 0;
292 static unsigned long cpu_khz_ref = 0;
293 #endif
295 static int
296 time_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
297 void *data)
299 struct cpufreq_freqs *freq = data;
301 write_seqlock(&xtime_lock);
302 if (!ref_freq) {
303 ref_freq = freq->old;
304 loops_per_jiffy_ref = cpu_data[freq->cpu].loops_per_jiffy;
305 #ifndef CONFIG_SMP
306 fast_gettimeoffset_ref = fast_gettimeoffset_quotient;
307 cpu_khz_ref = cpu_khz;
308 #endif
311 if ((val == CPUFREQ_PRECHANGE && freq->old < freq->new) ||
312 (val == CPUFREQ_POSTCHANGE && freq->old > freq->new)) {
313 cpu_data[freq->cpu].loops_per_jiffy = cpufreq_scale(loops_per_jiffy_ref, ref_freq, freq->new);
314 #ifndef CONFIG_SMP
315 if (use_tsc) {
316 fast_gettimeoffset_quotient = cpufreq_scale(fast_gettimeoffset_ref, freq->new, ref_freq);
317 cpu_khz = cpufreq_scale(cpu_khz_ref, ref_freq, freq->new);
319 #endif
321 write_sequnlock(&xtime_lock);
323 return 0;
326 static struct notifier_block time_cpufreq_notifier_block = {
327 .notifier_call = time_cpufreq_notifier
329 #endif
332 static int __init init_tsc(char* override)
335 /* check clock override */
336 if (override[0] && strncmp(override,"tsc",3))
337 return -ENODEV;
340 * If we have APM enabled or the CPU clock speed is variable
341 * (CPU stops clock on HLT or slows clock to save power)
342 * then the TSC timestamps may diverge by up to 1 jiffy from
343 * 'real time' but nothing will break.
344 * The most frequent case is that the CPU is "woken" from a halt
345 * state by the timer interrupt itself, so we get 0 error. In the
346 * rare cases where a driver would "wake" the CPU and request a
347 * timestamp, the maximum error is < 1 jiffy. But timestamps are
348 * still perfectly ordered.
349 * Note that the TSC counter will be reset if APM suspends
350 * to disk; this won't break the kernel, though, 'cuz we're
351 * smart. See arch/i386/kernel/apm.c.
354 * Firstly we have to do a CPU check for chips with
355 * a potentially buggy TSC. At this point we haven't run
356 * the ident/bugs checks so we must run this hook as it
357 * may turn off the TSC flag.
359 * NOTE: this doesn't yet handle SMP 486 machines where only
360 * some CPU's have a TSC. Thats never worked and nobody has
361 * moaned if you have the only one in the world - you fix it!
364 #ifdef CONFIG_CPU_FREQ
365 cpufreq_register_notifier(&time_cpufreq_notifier_block, CPUFREQ_TRANSITION_NOTIFIER);
366 #endif
368 count2 = LATCH; /* initialize counter for mark_offset_tsc() */
370 if (cpu_has_tsc) {
371 unsigned long tsc_quotient = calibrate_tsc();
372 if (tsc_quotient) {
373 fast_gettimeoffset_quotient = tsc_quotient;
374 use_tsc = 1;
376 * We could be more selective here I suspect
377 * and just enable this for the next intel chips ?
379 /* report CPU clock rate in Hz.
380 * The formula is (10^6 * 2^32) / (2^32 * 1 / (clocks/us)) =
381 * clock/second. Our precision is about 100 ppm.
383 { unsigned long eax=0, edx=1000;
384 __asm__("divl %2"
385 :"=a" (cpu_khz), "=d" (edx)
386 :"r" (tsc_quotient),
387 "0" (eax), "1" (edx));
388 printk("Detected %lu.%03lu MHz processor.\n", cpu_khz / 1000, cpu_khz % 1000);
390 set_cyc2ns_scale(cpu_khz/1000);
391 return 0;
394 return -ENODEV;
397 #ifndef CONFIG_X86_TSC
398 /* disable flag for tsc. Takes effect by clearing the TSC cpu flag
399 * in cpu/common.c */
400 static int __init tsc_setup(char *str)
402 tsc_disable = 1;
403 return 1;
405 #else
406 static int __init tsc_setup(char *str)
408 printk(KERN_WARNING "notsc: Kernel compiled with CONFIG_X86_TSC, "
409 "cannot disable TSC.\n");
410 return 1;
412 #endif
413 __setup("notsc", tsc_setup);
417 /************************************************************/
419 /* tsc timer_opts struct */
420 struct timer_opts timer_tsc = {
421 .init = init_tsc,
422 .mark_offset = mark_offset_tsc,
423 .get_offset = get_offset_tsc,
424 .monotonic_clock = monotonic_clock_tsc,
425 .delay = delay_tsc,