[PATCH] x86-64: Fix bad assumption that dualcore cpus have synced TSCs
[linux-2.6.git] / arch / x86_64 / kernel / time.c
blob703acde2a1a58647978e48cc3b4a5cf216eabc0c
1 /*
2 * linux/arch/x86-64/kernel/time.c
4 * "High Precision Event Timer" based timekeeping.
6 * Copyright (c) 1991,1992,1995 Linus Torvalds
7 * Copyright (c) 1994 Alan Modra
8 * Copyright (c) 1995 Markus Kuhn
9 * Copyright (c) 1996 Ingo Molnar
10 * Copyright (c) 1998 Andrea Arcangeli
11 * Copyright (c) 2002 Vojtech Pavlik
12 * Copyright (c) 2003 Andi Kleen
13 * RTC support code taken from arch/i386/kernel/timers/time_hpet.c
16 #include <linux/kernel.h>
17 #include <linux/sched.h>
18 #include <linux/interrupt.h>
19 #include <linux/init.h>
20 #include <linux/mc146818rtc.h>
21 #include <linux/time.h>
22 #include <linux/ioport.h>
23 #include <linux/module.h>
24 #include <linux/device.h>
25 #include <linux/sysdev.h>
26 #include <linux/bcd.h>
27 #include <linux/kallsyms.h>
28 #include <linux/acpi.h>
29 #ifdef CONFIG_ACPI
30 #include <acpi/achware.h> /* for PM timer frequency */
31 #endif
32 #include <asm/8253pit.h>
33 #include <asm/pgtable.h>
34 #include <asm/vsyscall.h>
35 #include <asm/timex.h>
36 #include <asm/proto.h>
37 #include <asm/hpet.h>
38 #include <asm/sections.h>
39 #include <linux/cpufreq.h>
40 #include <linux/hpet.h>
41 #ifdef CONFIG_X86_LOCAL_APIC
42 #include <asm/apic.h>
43 #endif
45 u64 jiffies_64 = INITIAL_JIFFIES;
47 EXPORT_SYMBOL(jiffies_64);
49 #ifdef CONFIG_CPU_FREQ
50 static void cpufreq_delayed_get(void);
51 #endif
52 extern void i8254_timer_resume(void);
53 extern int using_apic_timer;
55 DEFINE_SPINLOCK(rtc_lock);
56 DEFINE_SPINLOCK(i8253_lock);
58 static int nohpet __initdata = 0;
59 static int notsc __initdata = 0;
61 #undef HPET_HACK_ENABLE_DANGEROUS
63 unsigned int cpu_khz; /* TSC clocks / usec, not used here */
64 static unsigned long hpet_period; /* fsecs / HPET clock */
65 unsigned long hpet_tick; /* HPET clocks / interrupt */
66 static int hpet_use_timer;
67 unsigned long vxtime_hz = PIT_TICK_RATE;
68 int report_lost_ticks; /* command line option */
69 unsigned long long monotonic_base;
71 struct vxtime_data __vxtime __section_vxtime; /* for vsyscalls */
73 volatile unsigned long __jiffies __section_jiffies = INITIAL_JIFFIES;
74 unsigned long __wall_jiffies __section_wall_jiffies = INITIAL_JIFFIES;
75 struct timespec __xtime __section_xtime;
76 struct timezone __sys_tz __section_sys_tz;
78 static inline void rdtscll_sync(unsigned long *tsc)
80 #ifdef CONFIG_SMP
81 sync_core();
82 #endif
83 rdtscll(*tsc);
87 * do_gettimeoffset() returns microseconds since last timer interrupt was
88 * triggered by hardware. A memory read of HPET is slower than a register read
89 * of TSC, but much more reliable. It's also synchronized to the timer
90 * interrupt. Note that do_gettimeoffset() may return more than hpet_tick, if a
91 * timer interrupt has happened already, but vxtime.trigger wasn't updated yet.
92 * This is not a problem, because jiffies hasn't updated either. They are bound
93 * together by xtime_lock.
96 static inline unsigned int do_gettimeoffset_tsc(void)
98 unsigned long t;
99 unsigned long x;
100 rdtscll_sync(&t);
101 if (t < vxtime.last_tsc) t = vxtime.last_tsc; /* hack */
102 x = ((t - vxtime.last_tsc) * vxtime.tsc_quot) >> 32;
103 return x;
106 static inline unsigned int do_gettimeoffset_hpet(void)
108 /* cap counter read to one tick to avoid inconsistencies */
109 unsigned long counter = hpet_readl(HPET_COUNTER) - vxtime.last;
110 return (min(counter,hpet_tick) * vxtime.quot) >> 32;
113 unsigned int (*do_gettimeoffset)(void) = do_gettimeoffset_tsc;
116 * This version of gettimeofday() has microsecond resolution and better than
117 * microsecond precision, as we're using at least a 10 MHz (usually 14.31818
118 * MHz) HPET timer.
121 void do_gettimeofday(struct timeval *tv)
123 unsigned long seq, t;
124 unsigned int sec, usec;
126 do {
127 seq = read_seqbegin(&xtime_lock);
129 sec = xtime.tv_sec;
130 usec = xtime.tv_nsec / 1000;
132 /* i386 does some correction here to keep the clock
133 monotonous even when ntpd is fixing drift.
134 But they didn't work for me, there is a non monotonic
135 clock anyways with ntp.
136 I dropped all corrections now until a real solution can
137 be found. Note when you fix it here you need to do the same
138 in arch/x86_64/kernel/vsyscall.c and export all needed
139 variables in vmlinux.lds. -AK */
141 t = (jiffies - wall_jiffies) * (1000000L / HZ) +
142 do_gettimeoffset();
143 usec += t;
145 } while (read_seqretry(&xtime_lock, seq));
147 tv->tv_sec = sec + usec / 1000000;
148 tv->tv_usec = usec % 1000000;
151 EXPORT_SYMBOL(do_gettimeofday);
154 * settimeofday() first undoes the correction that gettimeofday would do
155 * on the time, and then saves it. This is ugly, but has been like this for
156 * ages already.
159 int do_settimeofday(struct timespec *tv)
161 time_t wtm_sec, sec = tv->tv_sec;
162 long wtm_nsec, nsec = tv->tv_nsec;
164 if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
165 return -EINVAL;
167 write_seqlock_irq(&xtime_lock);
169 nsec -= do_gettimeoffset() * 1000 +
170 (jiffies - wall_jiffies) * (NSEC_PER_SEC/HZ);
172 wtm_sec = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec);
173 wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec);
175 set_normalized_timespec(&xtime, sec, nsec);
176 set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
178 ntp_clear();
180 write_sequnlock_irq(&xtime_lock);
181 clock_was_set();
182 return 0;
185 EXPORT_SYMBOL(do_settimeofday);
187 unsigned long profile_pc(struct pt_regs *regs)
189 unsigned long pc = instruction_pointer(regs);
191 /* Assume the lock function has either no stack frame or only a single word.
192 This checks if the address on the stack looks like a kernel text address.
193 There is a small window for false hits, but in that case the tick
194 is just accounted to the spinlock function.
195 Better would be to write these functions in assembler again
196 and check exactly. */
197 if (in_lock_functions(pc)) {
198 char *v = *(char **)regs->rsp;
199 if ((v >= _stext && v <= _etext) ||
200 (v >= _sinittext && v <= _einittext) ||
201 (v >= (char *)MODULES_VADDR && v <= (char *)MODULES_END))
202 return (unsigned long)v;
203 return ((unsigned long *)regs->rsp)[1];
205 return pc;
207 EXPORT_SYMBOL(profile_pc);
210 * In order to set the CMOS clock precisely, set_rtc_mmss has to be called 500
211 * ms after the second nowtime has started, because when nowtime is written
212 * into the registers of the CMOS clock, it will jump to the next second
213 * precisely 500 ms later. Check the Motorola MC146818A or Dallas DS12887 data
214 * sheet for details.
217 static void set_rtc_mmss(unsigned long nowtime)
219 int real_seconds, real_minutes, cmos_minutes;
220 unsigned char control, freq_select;
223 * IRQs are disabled when we're called from the timer interrupt,
224 * no need for spin_lock_irqsave()
227 spin_lock(&rtc_lock);
230 * Tell the clock it's being set and stop it.
233 control = CMOS_READ(RTC_CONTROL);
234 CMOS_WRITE(control | RTC_SET, RTC_CONTROL);
236 freq_select = CMOS_READ(RTC_FREQ_SELECT);
237 CMOS_WRITE(freq_select | RTC_DIV_RESET2, RTC_FREQ_SELECT);
239 cmos_minutes = CMOS_READ(RTC_MINUTES);
240 BCD_TO_BIN(cmos_minutes);
243 * since we're only adjusting minutes and seconds, don't interfere with hour
244 * overflow. This avoids messing with unknown time zones but requires your RTC
245 * not to be off by more than 15 minutes. Since we're calling it only when
246 * our clock is externally synchronized using NTP, this shouldn't be a problem.
249 real_seconds = nowtime % 60;
250 real_minutes = nowtime / 60;
251 if (((abs(real_minutes - cmos_minutes) + 15) / 30) & 1)
252 real_minutes += 30; /* correct for half hour time zone */
253 real_minutes %= 60;
255 #if 0
256 /* AMD 8111 is a really bad time keeper and hits this regularly.
257 It probably was an attempt to avoid screwing up DST, but ignore
258 that for now. */
259 if (abs(real_minutes - cmos_minutes) >= 30) {
260 printk(KERN_WARNING "time.c: can't update CMOS clock "
261 "from %d to %d\n", cmos_minutes, real_minutes);
262 } else
263 #endif
266 BIN_TO_BCD(real_seconds);
267 BIN_TO_BCD(real_minutes);
268 CMOS_WRITE(real_seconds, RTC_SECONDS);
269 CMOS_WRITE(real_minutes, RTC_MINUTES);
273 * The following flags have to be released exactly in this order, otherwise the
274 * DS12887 (popular MC146818A clone with integrated battery and quartz) will
275 * not reset the oscillator and will not update precisely 500 ms later. You
276 * won't find this mentioned in the Dallas Semiconductor data sheets, but who
277 * believes data sheets anyway ... -- Markus Kuhn
280 CMOS_WRITE(control, RTC_CONTROL);
281 CMOS_WRITE(freq_select, RTC_FREQ_SELECT);
283 spin_unlock(&rtc_lock);
287 /* monotonic_clock(): returns # of nanoseconds passed since time_init()
288 * Note: This function is required to return accurate
289 * time even in the absence of multiple timer ticks.
291 unsigned long long monotonic_clock(void)
293 unsigned long seq;
294 u32 last_offset, this_offset, offset;
295 unsigned long long base;
297 if (vxtime.mode == VXTIME_HPET) {
298 do {
299 seq = read_seqbegin(&xtime_lock);
301 last_offset = vxtime.last;
302 base = monotonic_base;
303 this_offset = hpet_readl(HPET_COUNTER);
305 } while (read_seqretry(&xtime_lock, seq));
306 offset = (this_offset - last_offset);
307 offset *=(NSEC_PER_SEC/HZ)/hpet_tick;
308 return base + offset;
309 }else{
310 do {
311 seq = read_seqbegin(&xtime_lock);
313 last_offset = vxtime.last_tsc;
314 base = monotonic_base;
315 } while (read_seqretry(&xtime_lock, seq));
316 sync_core();
317 rdtscll(this_offset);
318 offset = (this_offset - last_offset)*1000/cpu_khz;
319 return base + offset;
324 EXPORT_SYMBOL(monotonic_clock);
326 static noinline void handle_lost_ticks(int lost, struct pt_regs *regs)
328 static long lost_count;
329 static int warned;
331 if (report_lost_ticks) {
332 printk(KERN_WARNING "time.c: Lost %d timer "
333 "tick(s)! ", lost);
334 print_symbol("rip %s)\n", regs->rip);
337 if (lost_count == 1000 && !warned) {
338 printk(KERN_WARNING
339 "warning: many lost ticks.\n"
340 KERN_WARNING "Your time source seems to be instable or "
341 "some driver is hogging interupts\n");
342 print_symbol("rip %s\n", regs->rip);
343 if (vxtime.mode == VXTIME_TSC && vxtime.hpet_address) {
344 printk(KERN_WARNING "Falling back to HPET\n");
345 vxtime.last = hpet_readl(HPET_T0_CMP) - hpet_tick;
346 vxtime.mode = VXTIME_HPET;
347 do_gettimeoffset = do_gettimeoffset_hpet;
349 /* else should fall back to PIT, but code missing. */
350 warned = 1;
351 } else
352 lost_count++;
354 #ifdef CONFIG_CPU_FREQ
355 /* In some cases the CPU can change frequency without us noticing
356 (like going into thermal throttle)
357 Give cpufreq a change to catch up. */
358 if ((lost_count+1) % 25 == 0) {
359 cpufreq_delayed_get();
361 #endif
364 static irqreturn_t timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
366 static unsigned long rtc_update = 0;
367 unsigned long tsc;
368 int delay, offset = 0, lost = 0;
371 * Here we are in the timer irq handler. We have irqs locally disabled (so we
372 * don't need spin_lock_irqsave()) but we don't know if the timer_bh is running
373 * on the other CPU, so we need a lock. We also need to lock the vsyscall
374 * variables, because both do_timer() and us change them -arca+vojtech
377 write_seqlock(&xtime_lock);
379 if (vxtime.hpet_address)
380 offset = hpet_readl(HPET_COUNTER);
382 if (hpet_use_timer) {
383 /* if we're using the hpet timer functionality,
384 * we can more accurately know the counter value
385 * when the timer interrupt occured.
387 offset = hpet_readl(HPET_T0_CMP) - hpet_tick;
388 delay = hpet_readl(HPET_COUNTER) - offset;
389 } else {
390 spin_lock(&i8253_lock);
391 outb_p(0x00, 0x43);
392 delay = inb_p(0x40);
393 delay |= inb(0x40) << 8;
394 spin_unlock(&i8253_lock);
395 delay = LATCH - 1 - delay;
398 rdtscll_sync(&tsc);
400 if (vxtime.mode == VXTIME_HPET) {
401 if (offset - vxtime.last > hpet_tick) {
402 lost = (offset - vxtime.last) / hpet_tick - 1;
405 monotonic_base +=
406 (offset - vxtime.last)*(NSEC_PER_SEC/HZ) / hpet_tick;
408 vxtime.last = offset;
409 #ifdef CONFIG_X86_PM_TIMER
410 } else if (vxtime.mode == VXTIME_PMTMR) {
411 lost = pmtimer_mark_offset();
412 #endif
413 } else {
414 offset = (((tsc - vxtime.last_tsc) *
415 vxtime.tsc_quot) >> 32) - (USEC_PER_SEC / HZ);
417 if (offset < 0)
418 offset = 0;
420 if (offset > (USEC_PER_SEC / HZ)) {
421 lost = offset / (USEC_PER_SEC / HZ);
422 offset %= (USEC_PER_SEC / HZ);
425 monotonic_base += (tsc - vxtime.last_tsc)*1000000/cpu_khz ;
427 vxtime.last_tsc = tsc - vxtime.quot * delay / vxtime.tsc_quot;
429 if ((((tsc - vxtime.last_tsc) *
430 vxtime.tsc_quot) >> 32) < offset)
431 vxtime.last_tsc = tsc -
432 (((long) offset << 32) / vxtime.tsc_quot) - 1;
435 if (lost > 0) {
436 handle_lost_ticks(lost, regs);
437 jiffies += lost;
441 * Do the timer stuff.
444 do_timer(regs);
445 #ifndef CONFIG_SMP
446 update_process_times(user_mode(regs));
447 #endif
450 * In the SMP case we use the local APIC timer interrupt to do the profiling,
451 * except when we simulate SMP mode on a uniprocessor system, in that case we
452 * have to call the local interrupt handler.
455 #ifndef CONFIG_X86_LOCAL_APIC
456 profile_tick(CPU_PROFILING, regs);
457 #else
458 if (!using_apic_timer)
459 smp_local_timer_interrupt(regs);
460 #endif
463 * If we have an externally synchronized Linux clock, then update CMOS clock
464 * accordingly every ~11 minutes. set_rtc_mmss() will be called in the jiffy
465 * closest to exactly 500 ms before the next second. If the update fails, we
466 * don't care, as it'll be updated on the next turn, and the problem (time way
467 * off) isn't likely to go away much sooner anyway.
470 if (ntp_synced() && xtime.tv_sec > rtc_update &&
471 abs(xtime.tv_nsec - 500000000) <= tick_nsec / 2) {
472 set_rtc_mmss(xtime.tv_sec);
473 rtc_update = xtime.tv_sec + 660;
476 write_sequnlock(&xtime_lock);
478 return IRQ_HANDLED;
481 static unsigned int cyc2ns_scale;
482 #define CYC2NS_SCALE_FACTOR 10 /* 2^10, carefully chosen */
484 static inline void set_cyc2ns_scale(unsigned long cpu_mhz)
486 cyc2ns_scale = (1000 << CYC2NS_SCALE_FACTOR)/cpu_mhz;
489 static inline unsigned long long cycles_2_ns(unsigned long long cyc)
491 return (cyc * cyc2ns_scale) >> CYC2NS_SCALE_FACTOR;
494 unsigned long long sched_clock(void)
496 unsigned long a = 0;
498 #if 0
499 /* Don't do a HPET read here. Using TSC always is much faster
500 and HPET may not be mapped yet when the scheduler first runs.
501 Disadvantage is a small drift between CPUs in some configurations,
502 but that should be tolerable. */
503 if (__vxtime.mode == VXTIME_HPET)
504 return (hpet_readl(HPET_COUNTER) * vxtime.quot) >> 32;
505 #endif
507 /* Could do CPU core sync here. Opteron can execute rdtsc speculatively,
508 which means it is not completely exact and may not be monotonous between
509 CPUs. But the errors should be too small to matter for scheduling
510 purposes. */
512 rdtscll(a);
513 return cycles_2_ns(a);
516 unsigned long get_cmos_time(void)
518 unsigned int timeout, year, mon, day, hour, min, sec;
519 unsigned char last, this;
520 unsigned long flags;
523 * The Linux interpretation of the CMOS clock register contents: When the
524 * Update-In-Progress (UIP) flag goes from 1 to 0, the RTC registers show the
525 * second which has precisely just started. Waiting for this can take up to 1
526 * second, we timeout approximately after 2.4 seconds on a machine with
527 * standard 8.3 MHz ISA bus.
530 spin_lock_irqsave(&rtc_lock, flags);
532 timeout = 1000000;
533 last = this = 0;
535 while (timeout && last && !this) {
536 last = this;
537 this = CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP;
538 timeout--;
542 * Here we are safe to assume the registers won't change for a whole second, so
543 * we just go ahead and read them.
546 sec = CMOS_READ(RTC_SECONDS);
547 min = CMOS_READ(RTC_MINUTES);
548 hour = CMOS_READ(RTC_HOURS);
549 day = CMOS_READ(RTC_DAY_OF_MONTH);
550 mon = CMOS_READ(RTC_MONTH);
551 year = CMOS_READ(RTC_YEAR);
553 spin_unlock_irqrestore(&rtc_lock, flags);
556 * We know that x86-64 always uses BCD format, no need to check the config
557 * register.
560 BCD_TO_BIN(sec);
561 BCD_TO_BIN(min);
562 BCD_TO_BIN(hour);
563 BCD_TO_BIN(day);
564 BCD_TO_BIN(mon);
565 BCD_TO_BIN(year);
568 * x86-64 systems only exists since 2002.
569 * This will work up to Dec 31, 2100
571 year += 2000;
573 return mktime(year, mon, day, hour, min, sec);
576 #ifdef CONFIG_CPU_FREQ
578 /* Frequency scaling support. Adjust the TSC based timer when the cpu frequency
579 changes.
581 RED-PEN: On SMP we assume all CPUs run with the same frequency. It's
582 not that important because current Opteron setups do not support
583 scaling on SMP anyroads.
585 Should fix up last_tsc too. Currently gettimeofday in the
586 first tick after the change will be slightly wrong. */
588 #include <linux/workqueue.h>
590 static unsigned int cpufreq_delayed_issched = 0;
591 static unsigned int cpufreq_init = 0;
592 static struct work_struct cpufreq_delayed_get_work;
594 static void handle_cpufreq_delayed_get(void *v)
596 unsigned int cpu;
597 for_each_online_cpu(cpu) {
598 cpufreq_get(cpu);
600 cpufreq_delayed_issched = 0;
603 /* if we notice lost ticks, schedule a call to cpufreq_get() as it tries
604 * to verify the CPU frequency the timing core thinks the CPU is running
605 * at is still correct.
607 static void cpufreq_delayed_get(void)
609 static int warned;
610 if (cpufreq_init && !cpufreq_delayed_issched) {
611 cpufreq_delayed_issched = 1;
612 if (!warned) {
613 warned = 1;
614 printk(KERN_DEBUG "Losing some ticks... checking if CPU frequency changed.\n");
616 schedule_work(&cpufreq_delayed_get_work);
620 static unsigned int ref_freq = 0;
621 static unsigned long loops_per_jiffy_ref = 0;
623 static unsigned long cpu_khz_ref = 0;
625 static int time_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
626 void *data)
628 struct cpufreq_freqs *freq = data;
629 unsigned long *lpj, dummy;
631 if (cpu_has(&cpu_data[freq->cpu], X86_FEATURE_CONSTANT_TSC))
632 return 0;
634 lpj = &dummy;
635 if (!(freq->flags & CPUFREQ_CONST_LOOPS))
636 #ifdef CONFIG_SMP
637 lpj = &cpu_data[freq->cpu].loops_per_jiffy;
638 #else
639 lpj = &boot_cpu_data.loops_per_jiffy;
640 #endif
642 if (!ref_freq) {
643 ref_freq = freq->old;
644 loops_per_jiffy_ref = *lpj;
645 cpu_khz_ref = cpu_khz;
647 if ((val == CPUFREQ_PRECHANGE && freq->old < freq->new) ||
648 (val == CPUFREQ_POSTCHANGE && freq->old > freq->new) ||
649 (val == CPUFREQ_RESUMECHANGE)) {
650 *lpj =
651 cpufreq_scale(loops_per_jiffy_ref, ref_freq, freq->new);
653 cpu_khz = cpufreq_scale(cpu_khz_ref, ref_freq, freq->new);
654 if (!(freq->flags & CPUFREQ_CONST_LOOPS))
655 vxtime.tsc_quot = (1000L << 32) / cpu_khz;
658 set_cyc2ns_scale(cpu_khz_ref / 1000);
660 return 0;
663 static struct notifier_block time_cpufreq_notifier_block = {
664 .notifier_call = time_cpufreq_notifier
667 static int __init cpufreq_tsc(void)
669 INIT_WORK(&cpufreq_delayed_get_work, handle_cpufreq_delayed_get, NULL);
670 if (!cpufreq_register_notifier(&time_cpufreq_notifier_block,
671 CPUFREQ_TRANSITION_NOTIFIER))
672 cpufreq_init = 1;
673 return 0;
676 core_initcall(cpufreq_tsc);
678 #endif
681 * calibrate_tsc() calibrates the processor TSC in a very simple way, comparing
682 * it to the HPET timer of known frequency.
685 #define TICK_COUNT 100000000
687 static unsigned int __init hpet_calibrate_tsc(void)
689 int tsc_start, hpet_start;
690 int tsc_now, hpet_now;
691 unsigned long flags;
693 local_irq_save(flags);
694 local_irq_disable();
696 hpet_start = hpet_readl(HPET_COUNTER);
697 rdtscl(tsc_start);
699 do {
700 local_irq_disable();
701 hpet_now = hpet_readl(HPET_COUNTER);
702 sync_core();
703 rdtscl(tsc_now);
704 local_irq_restore(flags);
705 } while ((tsc_now - tsc_start) < TICK_COUNT &&
706 (hpet_now - hpet_start) < TICK_COUNT);
708 return (tsc_now - tsc_start) * 1000000000L
709 / ((hpet_now - hpet_start) * hpet_period / 1000);
714 * pit_calibrate_tsc() uses the speaker output (channel 2) of
715 * the PIT. This is better than using the timer interrupt output,
716 * because we can read the value of the speaker with just one inb(),
717 * where we need three i/o operations for the interrupt channel.
718 * We count how many ticks the TSC does in 50 ms.
721 static unsigned int __init pit_calibrate_tsc(void)
723 unsigned long start, end;
724 unsigned long flags;
726 spin_lock_irqsave(&i8253_lock, flags);
728 outb((inb(0x61) & ~0x02) | 0x01, 0x61);
730 outb(0xb0, 0x43);
731 outb((PIT_TICK_RATE / (1000 / 50)) & 0xff, 0x42);
732 outb((PIT_TICK_RATE / (1000 / 50)) >> 8, 0x42);
733 rdtscll(start);
734 sync_core();
735 while ((inb(0x61) & 0x20) == 0);
736 sync_core();
737 rdtscll(end);
739 spin_unlock_irqrestore(&i8253_lock, flags);
741 return (end - start) / 50;
744 #ifdef CONFIG_HPET
745 static __init int late_hpet_init(void)
747 struct hpet_data hd;
748 unsigned int ntimer;
750 if (!vxtime.hpet_address)
751 return -1;
753 memset(&hd, 0, sizeof (hd));
755 ntimer = hpet_readl(HPET_ID);
756 ntimer = (ntimer & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT;
757 ntimer++;
760 * Register with driver.
761 * Timer0 and Timer1 is used by platform.
763 hd.hd_phys_address = vxtime.hpet_address;
764 hd.hd_address = (void *)fix_to_virt(FIX_HPET_BASE);
765 hd.hd_nirqs = ntimer;
766 hd.hd_flags = HPET_DATA_PLATFORM;
767 hpet_reserve_timer(&hd, 0);
768 #ifdef CONFIG_HPET_EMULATE_RTC
769 hpet_reserve_timer(&hd, 1);
770 #endif
771 hd.hd_irq[0] = HPET_LEGACY_8254;
772 hd.hd_irq[1] = HPET_LEGACY_RTC;
773 if (ntimer > 2) {
774 struct hpet *hpet;
775 struct hpet_timer *timer;
776 int i;
778 hpet = (struct hpet *) fix_to_virt(FIX_HPET_BASE);
780 for (i = 2, timer = &hpet->hpet_timers[2]; i < ntimer;
781 timer++, i++)
782 hd.hd_irq[i] = (timer->hpet_config &
783 Tn_INT_ROUTE_CNF_MASK) >>
784 Tn_INT_ROUTE_CNF_SHIFT;
788 hpet_alloc(&hd);
789 return 0;
791 fs_initcall(late_hpet_init);
792 #endif
794 static int hpet_timer_stop_set_go(unsigned long tick)
796 unsigned int cfg;
799 * Stop the timers and reset the main counter.
802 cfg = hpet_readl(HPET_CFG);
803 cfg &= ~(HPET_CFG_ENABLE | HPET_CFG_LEGACY);
804 hpet_writel(cfg, HPET_CFG);
805 hpet_writel(0, HPET_COUNTER);
806 hpet_writel(0, HPET_COUNTER + 4);
809 * Set up timer 0, as periodic with first interrupt to happen at hpet_tick,
810 * and period also hpet_tick.
812 if (hpet_use_timer) {
813 hpet_writel(HPET_TN_ENABLE | HPET_TN_PERIODIC | HPET_TN_SETVAL |
814 HPET_TN_32BIT, HPET_T0_CFG);
815 hpet_writel(hpet_tick, HPET_T0_CMP);
816 hpet_writel(hpet_tick, HPET_T0_CMP); /* AK: why twice? */
817 cfg |= HPET_CFG_LEGACY;
820 * Go!
823 cfg |= HPET_CFG_ENABLE;
824 hpet_writel(cfg, HPET_CFG);
826 return 0;
829 static int hpet_init(void)
831 unsigned int id;
833 if (!vxtime.hpet_address)
834 return -1;
835 set_fixmap_nocache(FIX_HPET_BASE, vxtime.hpet_address);
836 __set_fixmap(VSYSCALL_HPET, vxtime.hpet_address, PAGE_KERNEL_VSYSCALL_NOCACHE);
839 * Read the period, compute tick and quotient.
842 id = hpet_readl(HPET_ID);
844 if (!(id & HPET_ID_VENDOR) || !(id & HPET_ID_NUMBER))
845 return -1;
847 hpet_period = hpet_readl(HPET_PERIOD);
848 if (hpet_period < 100000 || hpet_period > 100000000)
849 return -1;
851 hpet_tick = (1000000000L * (USEC_PER_SEC / HZ) + hpet_period / 2) /
852 hpet_period;
854 hpet_use_timer = (id & HPET_ID_LEGSUP);
856 return hpet_timer_stop_set_go(hpet_tick);
859 static int hpet_reenable(void)
861 return hpet_timer_stop_set_go(hpet_tick);
864 void __init pit_init(void)
866 unsigned long flags;
868 spin_lock_irqsave(&i8253_lock, flags);
869 outb_p(0x34, 0x43); /* binary, mode 2, LSB/MSB, ch 0 */
870 outb_p(LATCH & 0xff, 0x40); /* LSB */
871 outb_p(LATCH >> 8, 0x40); /* MSB */
872 spin_unlock_irqrestore(&i8253_lock, flags);
875 int __init time_setup(char *str)
877 report_lost_ticks = 1;
878 return 1;
881 static struct irqaction irq0 = {
882 timer_interrupt, SA_INTERRUPT, CPU_MASK_NONE, "timer", NULL, NULL
885 extern void __init config_acpi_tables(void);
887 void __init time_init(void)
889 char *timename;
891 #ifdef HPET_HACK_ENABLE_DANGEROUS
892 if (!vxtime.hpet_address) {
893 printk(KERN_WARNING "time.c: WARNING: Enabling HPET base "
894 "manually!\n");
895 outl(0x800038a0, 0xcf8);
896 outl(0xff000001, 0xcfc);
897 outl(0x800038a0, 0xcf8);
898 vxtime.hpet_address = inl(0xcfc) & 0xfffffffe;
899 printk(KERN_WARNING "time.c: WARNING: Enabled HPET "
900 "at %#lx.\n", vxtime.hpet_address);
902 #endif
903 if (nohpet)
904 vxtime.hpet_address = 0;
906 xtime.tv_sec = get_cmos_time();
907 xtime.tv_nsec = 0;
909 set_normalized_timespec(&wall_to_monotonic,
910 -xtime.tv_sec, -xtime.tv_nsec);
912 if (!hpet_init())
913 vxtime_hz = (1000000000000000L + hpet_period / 2) /
914 hpet_period;
916 if (hpet_use_timer) {
917 cpu_khz = hpet_calibrate_tsc();
918 timename = "HPET";
919 #ifdef CONFIG_X86_PM_TIMER
920 } else if (pmtmr_ioport) {
921 vxtime_hz = PM_TIMER_FREQUENCY;
922 timename = "PM";
923 pit_init();
924 cpu_khz = pit_calibrate_tsc();
925 #endif
926 } else {
927 pit_init();
928 cpu_khz = pit_calibrate_tsc();
929 timename = "PIT";
932 printk(KERN_INFO "time.c: Using %ld.%06ld MHz %s timer.\n",
933 vxtime_hz / 1000000, vxtime_hz % 1000000, timename);
934 printk(KERN_INFO "time.c: Detected %d.%03d MHz processor.\n",
935 cpu_khz / 1000, cpu_khz % 1000);
936 vxtime.mode = VXTIME_TSC;
937 vxtime.quot = (1000000L << 32) / vxtime_hz;
938 vxtime.tsc_quot = (1000L << 32) / cpu_khz;
939 rdtscll_sync(&vxtime.last_tsc);
940 setup_irq(0, &irq0);
942 set_cyc2ns_scale(cpu_khz / 1000);
944 #ifndef CONFIG_SMP
945 time_init_gtod();
946 #endif
950 * Make an educated guess if the TSC is trustworthy and synchronized
951 * over all CPUs.
953 static __init int unsynchronized_tsc(void)
955 #ifdef CONFIG_SMP
956 if (oem_force_hpet_timer())
957 return 1;
958 /* Intel systems are normally all synchronized. Exceptions
959 are handled in the OEM check above. */
960 if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
961 return 0;
962 #endif
963 /* Assume multi socket systems are not synchronized */
964 return num_online_cpus() > 1;
968 * Decide after all CPUs are booted what mode gettimeofday should use.
970 void __init time_init_gtod(void)
972 char *timetype;
974 if (unsynchronized_tsc())
975 notsc = 1;
976 if (vxtime.hpet_address && notsc) {
977 timetype = hpet_use_timer ? "HPET" : "PIT/HPET";
978 vxtime.last = hpet_readl(HPET_T0_CMP) - hpet_tick;
979 vxtime.mode = VXTIME_HPET;
980 do_gettimeoffset = do_gettimeoffset_hpet;
981 #ifdef CONFIG_X86_PM_TIMER
982 /* Using PM for gettimeofday is quite slow, but we have no other
983 choice because the TSC is too unreliable on some systems. */
984 } else if (pmtmr_ioport && !vxtime.hpet_address && notsc) {
985 timetype = "PM";
986 do_gettimeoffset = do_gettimeoffset_pm;
987 vxtime.mode = VXTIME_PMTMR;
988 sysctl_vsyscall = 0;
989 printk(KERN_INFO "Disabling vsyscall due to use of PM timer\n");
990 #endif
991 } else {
992 timetype = hpet_use_timer ? "HPET/TSC" : "PIT/TSC";
993 vxtime.mode = VXTIME_TSC;
996 printk(KERN_INFO "time.c: Using %s based timekeeping.\n", timetype);
999 __setup("report_lost_ticks", time_setup);
1001 static long clock_cmos_diff;
1002 static unsigned long sleep_start;
1004 static int timer_suspend(struct sys_device *dev, pm_message_t state)
1007 * Estimate time zone so that set_time can update the clock
1009 long cmos_time = get_cmos_time();
1011 clock_cmos_diff = -cmos_time;
1012 clock_cmos_diff += get_seconds();
1013 sleep_start = cmos_time;
1014 return 0;
1017 static int timer_resume(struct sys_device *dev)
1019 unsigned long flags;
1020 unsigned long sec;
1021 unsigned long ctime = get_cmos_time();
1022 unsigned long sleep_length = (ctime - sleep_start) * HZ;
1024 if (vxtime.hpet_address)
1025 hpet_reenable();
1026 else
1027 i8254_timer_resume();
1029 sec = ctime + clock_cmos_diff;
1030 write_seqlock_irqsave(&xtime_lock,flags);
1031 xtime.tv_sec = sec;
1032 xtime.tv_nsec = 0;
1033 write_sequnlock_irqrestore(&xtime_lock,flags);
1034 jiffies += sleep_length;
1035 wall_jiffies += sleep_length;
1036 touch_softlockup_watchdog();
1037 return 0;
1040 static struct sysdev_class timer_sysclass = {
1041 .resume = timer_resume,
1042 .suspend = timer_suspend,
1043 set_kset_name("timer"),
1047 /* XXX this driverfs stuff should probably go elsewhere later -john */
1048 static struct sys_device device_timer = {
1049 .id = 0,
1050 .cls = &timer_sysclass,
1053 static int time_init_device(void)
1055 int error = sysdev_class_register(&timer_sysclass);
1056 if (!error)
1057 error = sysdev_register(&device_timer);
1058 return error;
1061 device_initcall(time_init_device);
1063 #ifdef CONFIG_HPET_EMULATE_RTC
1064 /* HPET in LegacyReplacement Mode eats up RTC interrupt line. When, HPET
1065 * is enabled, we support RTC interrupt functionality in software.
1066 * RTC has 3 kinds of interrupts:
1067 * 1) Update Interrupt - generate an interrupt, every sec, when RTC clock
1068 * is updated
1069 * 2) Alarm Interrupt - generate an interrupt at a specific time of day
1070 * 3) Periodic Interrupt - generate periodic interrupt, with frequencies
1071 * 2Hz-8192Hz (2Hz-64Hz for non-root user) (all freqs in powers of 2)
1072 * (1) and (2) above are implemented using polling at a frequency of
1073 * 64 Hz. The exact frequency is a tradeoff between accuracy and interrupt
1074 * overhead. (DEFAULT_RTC_INT_FREQ)
1075 * For (3), we use interrupts at 64Hz or user specified periodic
1076 * frequency, whichever is higher.
1078 #include <linux/rtc.h>
1080 extern irqreturn_t rtc_interrupt(int irq, void *dev_id, struct pt_regs *regs);
1082 #define DEFAULT_RTC_INT_FREQ 64
1083 #define RTC_NUM_INTS 1
1085 static unsigned long UIE_on;
1086 static unsigned long prev_update_sec;
1088 static unsigned long AIE_on;
1089 static struct rtc_time alarm_time;
1091 static unsigned long PIE_on;
1092 static unsigned long PIE_freq = DEFAULT_RTC_INT_FREQ;
1093 static unsigned long PIE_count;
1095 static unsigned long hpet_rtc_int_freq; /* RTC interrupt frequency */
1097 int is_hpet_enabled(void)
1099 return vxtime.hpet_address != 0;
1103 * Timer 1 for RTC, we do not use periodic interrupt feature,
1104 * even if HPET supports periodic interrupts on Timer 1.
1105 * The reason being, to set up a periodic interrupt in HPET, we need to
1106 * stop the main counter. And if we do that everytime someone diables/enables
1107 * RTC, we will have adverse effect on main kernel timer running on Timer 0.
1108 * So, for the time being, simulate the periodic interrupt in software.
1110 * hpet_rtc_timer_init() is called for the first time and during subsequent
1111 * interuppts reinit happens through hpet_rtc_timer_reinit().
1113 int hpet_rtc_timer_init(void)
1115 unsigned int cfg, cnt;
1116 unsigned long flags;
1118 if (!is_hpet_enabled())
1119 return 0;
1121 * Set the counter 1 and enable the interrupts.
1123 if (PIE_on && (PIE_freq > DEFAULT_RTC_INT_FREQ))
1124 hpet_rtc_int_freq = PIE_freq;
1125 else
1126 hpet_rtc_int_freq = DEFAULT_RTC_INT_FREQ;
1128 local_irq_save(flags);
1129 cnt = hpet_readl(HPET_COUNTER);
1130 cnt += ((hpet_tick*HZ)/hpet_rtc_int_freq);
1131 hpet_writel(cnt, HPET_T1_CMP);
1132 local_irq_restore(flags);
1134 cfg = hpet_readl(HPET_T1_CFG);
1135 cfg |= HPET_TN_ENABLE | HPET_TN_SETVAL | HPET_TN_32BIT;
1136 hpet_writel(cfg, HPET_T1_CFG);
1138 return 1;
1141 static void hpet_rtc_timer_reinit(void)
1143 unsigned int cfg, cnt;
1145 if (!(PIE_on | AIE_on | UIE_on))
1146 return;
1148 if (PIE_on && (PIE_freq > DEFAULT_RTC_INT_FREQ))
1149 hpet_rtc_int_freq = PIE_freq;
1150 else
1151 hpet_rtc_int_freq = DEFAULT_RTC_INT_FREQ;
1153 /* It is more accurate to use the comparator value than current count.*/
1154 cnt = hpet_readl(HPET_T1_CMP);
1155 cnt += hpet_tick*HZ/hpet_rtc_int_freq;
1156 hpet_writel(cnt, HPET_T1_CMP);
1158 cfg = hpet_readl(HPET_T1_CFG);
1159 cfg |= HPET_TN_ENABLE | HPET_TN_SETVAL | HPET_TN_32BIT;
1160 hpet_writel(cfg, HPET_T1_CFG);
1162 return;
1166 * The functions below are called from rtc driver.
1167 * Return 0 if HPET is not being used.
1168 * Otherwise do the necessary changes and return 1.
1170 int hpet_mask_rtc_irq_bit(unsigned long bit_mask)
1172 if (!is_hpet_enabled())
1173 return 0;
1175 if (bit_mask & RTC_UIE)
1176 UIE_on = 0;
1177 if (bit_mask & RTC_PIE)
1178 PIE_on = 0;
1179 if (bit_mask & RTC_AIE)
1180 AIE_on = 0;
1182 return 1;
1185 int hpet_set_rtc_irq_bit(unsigned long bit_mask)
1187 int timer_init_reqd = 0;
1189 if (!is_hpet_enabled())
1190 return 0;
1192 if (!(PIE_on | AIE_on | UIE_on))
1193 timer_init_reqd = 1;
1195 if (bit_mask & RTC_UIE) {
1196 UIE_on = 1;
1198 if (bit_mask & RTC_PIE) {
1199 PIE_on = 1;
1200 PIE_count = 0;
1202 if (bit_mask & RTC_AIE) {
1203 AIE_on = 1;
1206 if (timer_init_reqd)
1207 hpet_rtc_timer_init();
1209 return 1;
1212 int hpet_set_alarm_time(unsigned char hrs, unsigned char min, unsigned char sec)
1214 if (!is_hpet_enabled())
1215 return 0;
1217 alarm_time.tm_hour = hrs;
1218 alarm_time.tm_min = min;
1219 alarm_time.tm_sec = sec;
1221 return 1;
1224 int hpet_set_periodic_freq(unsigned long freq)
1226 if (!is_hpet_enabled())
1227 return 0;
1229 PIE_freq = freq;
1230 PIE_count = 0;
1232 return 1;
1235 int hpet_rtc_dropped_irq(void)
1237 if (!is_hpet_enabled())
1238 return 0;
1240 return 1;
1243 irqreturn_t hpet_rtc_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1245 struct rtc_time curr_time;
1246 unsigned long rtc_int_flag = 0;
1247 int call_rtc_interrupt = 0;
1249 hpet_rtc_timer_reinit();
1251 if (UIE_on | AIE_on) {
1252 rtc_get_rtc_time(&curr_time);
1254 if (UIE_on) {
1255 if (curr_time.tm_sec != prev_update_sec) {
1256 /* Set update int info, call real rtc int routine */
1257 call_rtc_interrupt = 1;
1258 rtc_int_flag = RTC_UF;
1259 prev_update_sec = curr_time.tm_sec;
1262 if (PIE_on) {
1263 PIE_count++;
1264 if (PIE_count >= hpet_rtc_int_freq/PIE_freq) {
1265 /* Set periodic int info, call real rtc int routine */
1266 call_rtc_interrupt = 1;
1267 rtc_int_flag |= RTC_PF;
1268 PIE_count = 0;
1271 if (AIE_on) {
1272 if ((curr_time.tm_sec == alarm_time.tm_sec) &&
1273 (curr_time.tm_min == alarm_time.tm_min) &&
1274 (curr_time.tm_hour == alarm_time.tm_hour)) {
1275 /* Set alarm int info, call real rtc int routine */
1276 call_rtc_interrupt = 1;
1277 rtc_int_flag |= RTC_AF;
1280 if (call_rtc_interrupt) {
1281 rtc_int_flag |= (RTC_IRQF | (RTC_NUM_INTS << 8));
1282 rtc_interrupt(rtc_int_flag, dev_id, regs);
1284 return IRQ_HANDLED;
1286 #endif
1290 static int __init nohpet_setup(char *s)
1292 nohpet = 1;
1293 return 0;
1296 __setup("nohpet", nohpet_setup);
1299 static int __init notsc_setup(char *s)
1301 notsc = 1;
1302 return 0;
1305 __setup("notsc", notsc_setup);