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,2006 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>
30 #include <acpi/achware.h> /* for PM timer frequency */
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>
38 #include <asm/sections.h>
39 #include <linux/cpufreq.h>
40 #include <linux/hpet.h>
41 #ifdef CONFIG_X86_LOCAL_APIC
45 #ifdef CONFIG_CPU_FREQ
46 static void cpufreq_delayed_get(void);
48 extern void i8254_timer_resume(void);
49 extern int using_apic_timer;
51 static char *time_init_gtod(void);
53 DEFINE_SPINLOCK(rtc_lock);
54 EXPORT_SYMBOL(rtc_lock);
55 DEFINE_SPINLOCK(i8253_lock);
57 int nohpet __initdata = 0;
58 static int notsc __initdata = 0;
60 #define USEC_PER_TICK (USEC_PER_SEC / HZ)
61 #define NSEC_PER_TICK (NSEC_PER_SEC / HZ)
62 #define FSEC_PER_TICK (FSEC_PER_SEC / HZ)
64 #define NS_SCALE 10 /* 2^10, carefully chosen */
65 #define US_SCALE 32 /* 2^32, arbitralrily chosen */
67 unsigned int cpu_khz; /* TSC clocks / usec, not used here */
68 EXPORT_SYMBOL(cpu_khz);
69 static unsigned long hpet_period; /* fsecs / HPET clock */
70 unsigned long hpet_tick; /* HPET clocks / interrupt */
71 int hpet_use_timer; /* Use counter of hpet for time keeping, otherwise PIT */
72 unsigned long vxtime_hz = PIT_TICK_RATE;
73 int report_lost_ticks; /* command line option */
74 unsigned long long monotonic_base;
76 struct vxtime_data __vxtime __section_vxtime; /* for vsyscalls */
78 volatile unsigned long __jiffies __section_jiffies = INITIAL_JIFFIES;
79 unsigned long __wall_jiffies __section_wall_jiffies = INITIAL_JIFFIES;
80 struct timespec __xtime __section_xtime;
81 struct timezone __sys_tz __section_sys_tz;
84 * do_gettimeoffset() returns microseconds since last timer interrupt was
85 * triggered by hardware. A memory read of HPET is slower than a register read
86 * of TSC, but much more reliable. It's also synchronized to the timer
87 * interrupt. Note that do_gettimeoffset() may return more than hpet_tick, if a
88 * timer interrupt has happened already, but vxtime.trigger wasn't updated yet.
89 * This is not a problem, because jiffies hasn't updated either. They are bound
90 * together by xtime_lock.
93 static inline unsigned int do_gettimeoffset_tsc(void)
97 t = get_cycles_sync();
98 if (t < vxtime.last_tsc)
99 t = vxtime.last_tsc; /* hack */
100 x = ((t - vxtime.last_tsc) * vxtime.tsc_quot) >> US_SCALE;
104 static inline unsigned int do_gettimeoffset_hpet(void)
106 /* cap counter read to one tick to avoid inconsistencies */
107 unsigned long counter = hpet_readl(HPET_COUNTER) - vxtime.last;
108 return (min(counter,hpet_tick) * vxtime.quot) >> US_SCALE;
111 unsigned int (*do_gettimeoffset)(void) = do_gettimeoffset_tsc;
114 * This version of gettimeofday() has microsecond resolution and better than
115 * microsecond precision, as we're using at least a 10 MHz (usually 14.31818
119 void do_gettimeofday(struct timeval *tv)
121 unsigned long seq, t;
122 unsigned int sec, usec;
125 seq = read_seqbegin(&xtime_lock);
128 usec = xtime.tv_nsec / NSEC_PER_USEC;
130 /* i386 does some correction here to keep the clock
131 monotonous even when ntpd is fixing drift.
132 But they didn't work for me, there is a non monotonic
133 clock anyways with ntp.
134 I dropped all corrections now until a real solution can
135 be found. Note when you fix it here you need to do the same
136 in arch/x86_64/kernel/vsyscall.c and export all needed
137 variables in vmlinux.lds. -AK */
139 t = (jiffies - wall_jiffies) * USEC_PER_TICK +
143 } while (read_seqretry(&xtime_lock, seq));
145 tv->tv_sec = sec + usec / USEC_PER_SEC;
146 tv->tv_usec = usec % USEC_PER_SEC;
149 EXPORT_SYMBOL(do_gettimeofday);
152 * settimeofday() first undoes the correction that gettimeofday would do
153 * on the time, and then saves it. This is ugly, but has been like this for
157 int do_settimeofday(struct timespec *tv)
159 time_t wtm_sec, sec = tv->tv_sec;
160 long wtm_nsec, nsec = tv->tv_nsec;
162 if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
165 write_seqlock_irq(&xtime_lock);
167 nsec -= do_gettimeoffset() * NSEC_PER_USEC +
168 (jiffies - wall_jiffies) * NSEC_PER_TICK;
170 wtm_sec = wall_to_monotonic.tv_sec + (xtime.tv_sec - sec);
171 wtm_nsec = wall_to_monotonic.tv_nsec + (xtime.tv_nsec - nsec);
173 set_normalized_timespec(&xtime, sec, nsec);
174 set_normalized_timespec(&wall_to_monotonic, wtm_sec, wtm_nsec);
178 write_sequnlock_irq(&xtime_lock);
183 EXPORT_SYMBOL(do_settimeofday);
185 unsigned long profile_pc(struct pt_regs *regs)
187 unsigned long pc = instruction_pointer(regs);
189 /* Assume the lock function has either no stack frame or only a single
190 word. This checks if the address on the stack looks like a kernel
192 There is a small window for false hits, but in that case the tick
193 is just accounted to the spinlock function.
194 Better would be to write these functions in assembler again
195 and check exactly. */
196 if (in_lock_functions(pc)) {
197 char *v = *(char **)regs->rsp;
198 if ((v >= _stext && v <= _etext) ||
199 (v >= _sinittext && v <= _einittext) ||
200 (v >= (char *)MODULES_VADDR && v <= (char *)MODULES_END))
201 return (unsigned long)v;
202 return ((unsigned long *)regs->rsp)[1];
206 EXPORT_SYMBOL(profile_pc);
209 * In order to set the CMOS clock precisely, set_rtc_mmss has to be called 500
210 * ms after the second nowtime has started, because when nowtime is written
211 * into the registers of the CMOS clock, it will jump to the next second
212 * precisely 500 ms later. Check the Motorola MC146818A or Dallas DS12887 data
216 static void set_rtc_mmss(unsigned long nowtime)
218 int real_seconds, real_minutes, cmos_minutes;
219 unsigned char control, freq_select;
222 * IRQs are disabled when we're called from the timer interrupt,
223 * no need for spin_lock_irqsave()
226 spin_lock(&rtc_lock);
229 * Tell the clock it's being set and stop it.
232 control = CMOS_READ(RTC_CONTROL);
233 CMOS_WRITE(control | RTC_SET, RTC_CONTROL);
235 freq_select = CMOS_READ(RTC_FREQ_SELECT);
236 CMOS_WRITE(freq_select | RTC_DIV_RESET2, RTC_FREQ_SELECT);
238 cmos_minutes = CMOS_READ(RTC_MINUTES);
239 BCD_TO_BIN(cmos_minutes);
242 * since we're only adjusting minutes and seconds, don't interfere with hour
243 * overflow. This avoids messing with unknown time zones but requires your RTC
244 * not to be off by more than 15 minutes. Since we're calling it only when
245 * our clock is externally synchronized using NTP, this shouldn't be a problem.
248 real_seconds = nowtime % 60;
249 real_minutes = nowtime / 60;
250 if (((abs(real_minutes - cmos_minutes) + 15) / 30) & 1)
251 real_minutes += 30; /* correct for half hour time zone */
254 if (abs(real_minutes - cmos_minutes) >= 30) {
255 printk(KERN_WARNING "time.c: can't update CMOS clock "
256 "from %d to %d\n", cmos_minutes, real_minutes);
258 BIN_TO_BCD(real_seconds);
259 BIN_TO_BCD(real_minutes);
260 CMOS_WRITE(real_seconds, RTC_SECONDS);
261 CMOS_WRITE(real_minutes, RTC_MINUTES);
265 * The following flags have to be released exactly in this order, otherwise the
266 * DS12887 (popular MC146818A clone with integrated battery and quartz) will
267 * not reset the oscillator and will not update precisely 500 ms later. You
268 * won't find this mentioned in the Dallas Semiconductor data sheets, but who
269 * believes data sheets anyway ... -- Markus Kuhn
272 CMOS_WRITE(control, RTC_CONTROL);
273 CMOS_WRITE(freq_select, RTC_FREQ_SELECT);
275 spin_unlock(&rtc_lock);
279 /* monotonic_clock(): returns # of nanoseconds passed since time_init()
280 * Note: This function is required to return accurate
281 * time even in the absence of multiple timer ticks.
283 unsigned long long monotonic_clock(void)
286 u32 last_offset, this_offset, offset;
287 unsigned long long base;
289 if (vxtime.mode == VXTIME_HPET) {
291 seq = read_seqbegin(&xtime_lock);
293 last_offset = vxtime.last;
294 base = monotonic_base;
295 this_offset = hpet_readl(HPET_COUNTER);
296 } while (read_seqretry(&xtime_lock, seq));
297 offset = (this_offset - last_offset);
298 offset *= NSEC_PER_TICK / hpet_tick;
301 seq = read_seqbegin(&xtime_lock);
303 last_offset = vxtime.last_tsc;
304 base = monotonic_base;
305 } while (read_seqretry(&xtime_lock, seq));
306 this_offset = get_cycles_sync();
307 /* FIXME: 1000 or 1000000? */
308 offset = (this_offset - last_offset)*1000 / cpu_khz;
310 return base + offset;
312 EXPORT_SYMBOL(monotonic_clock);
314 static noinline void handle_lost_ticks(int lost, struct pt_regs *regs)
316 static long lost_count;
318 if (report_lost_ticks) {
319 printk(KERN_WARNING "time.c: Lost %d timer tick(s)! ", lost);
320 print_symbol("rip %s)\n", regs->rip);
323 if (lost_count == 1000 && !warned) {
324 printk(KERN_WARNING "warning: many lost ticks.\n"
325 KERN_WARNING "Your time source seems to be instable or "
326 "some driver is hogging interupts\n");
327 print_symbol("rip %s\n", regs->rip);
328 if (vxtime.mode == VXTIME_TSC && vxtime.hpet_address) {
329 printk(KERN_WARNING "Falling back to HPET\n");
331 vxtime.last = hpet_readl(HPET_T0_CMP) -
334 vxtime.last = hpet_readl(HPET_COUNTER);
335 vxtime.mode = VXTIME_HPET;
336 do_gettimeoffset = do_gettimeoffset_hpet;
338 /* else should fall back to PIT, but code missing. */
343 #ifdef CONFIG_CPU_FREQ
344 /* In some cases the CPU can change frequency without us noticing
345 Give cpufreq a change to catch up. */
346 if ((lost_count+1) % 25 == 0)
347 cpufreq_delayed_get();
351 void main_timer_handler(struct pt_regs *regs)
353 static unsigned long rtc_update = 0;
355 int delay = 0, offset = 0, lost = 0;
358 * Here we are in the timer irq handler. We have irqs locally disabled (so we
359 * don't need spin_lock_irqsave()) but we don't know if the timer_bh is running
360 * on the other CPU, so we need a lock. We also need to lock the vsyscall
361 * variables, because both do_timer() and us change them -arca+vojtech
364 write_seqlock(&xtime_lock);
366 if (vxtime.hpet_address)
367 offset = hpet_readl(HPET_COUNTER);
369 if (hpet_use_timer) {
370 /* if we're using the hpet timer functionality,
371 * we can more accurately know the counter value
372 * when the timer interrupt occured.
374 offset = hpet_readl(HPET_T0_CMP) - hpet_tick;
375 delay = hpet_readl(HPET_COUNTER) - offset;
376 } else if (!pmtmr_ioport) {
377 spin_lock(&i8253_lock);
380 delay |= inb(0x40) << 8;
381 spin_unlock(&i8253_lock);
382 delay = LATCH - 1 - delay;
385 tsc = get_cycles_sync();
387 if (vxtime.mode == VXTIME_HPET) {
388 if (offset - vxtime.last > hpet_tick) {
389 lost = (offset - vxtime.last) / hpet_tick - 1;
393 (offset - vxtime.last) * NSEC_PER_TICK / hpet_tick;
395 vxtime.last = offset;
396 #ifdef CONFIG_X86_PM_TIMER
397 } else if (vxtime.mode == VXTIME_PMTMR) {
398 lost = pmtimer_mark_offset();
401 offset = (((tsc - vxtime.last_tsc) *
402 vxtime.tsc_quot) >> US_SCALE) - USEC_PER_TICK;
407 if (offset > USEC_PER_TICK) {
408 lost = offset / USEC_PER_TICK;
409 offset %= USEC_PER_TICK;
412 /* FIXME: 1000 or 1000000? */
413 monotonic_base += (tsc - vxtime.last_tsc) * 1000000 / cpu_khz;
415 vxtime.last_tsc = tsc - vxtime.quot * delay / vxtime.tsc_quot;
417 if ((((tsc - vxtime.last_tsc) *
418 vxtime.tsc_quot) >> US_SCALE) < offset)
419 vxtime.last_tsc = tsc -
420 (((long) offset << US_SCALE) / vxtime.tsc_quot) - 1;
424 handle_lost_ticks(lost, regs);
429 * Do the timer stuff.
434 update_process_times(user_mode(regs));
438 * In the SMP case we use the local APIC timer interrupt to do the profiling,
439 * except when we simulate SMP mode on a uniprocessor system, in that case we
440 * have to call the local interrupt handler.
443 #ifndef CONFIG_X86_LOCAL_APIC
444 profile_tick(CPU_PROFILING, regs);
446 if (!using_apic_timer)
447 smp_local_timer_interrupt(regs);
451 * If we have an externally synchronized Linux clock, then update CMOS clock
452 * accordingly every ~11 minutes. set_rtc_mmss() will be called in the jiffy
453 * closest to exactly 500 ms before the next second. If the update fails, we
454 * don't care, as it'll be updated on the next turn, and the problem (time way
455 * off) isn't likely to go away much sooner anyway.
458 if (ntp_synced() && xtime.tv_sec > rtc_update &&
459 abs(xtime.tv_nsec - 500000000) <= tick_nsec / 2) {
460 set_rtc_mmss(xtime.tv_sec);
461 rtc_update = xtime.tv_sec + 660;
464 write_sequnlock(&xtime_lock);
467 static irqreturn_t timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
469 if (apic_runs_main_timer > 1)
471 main_timer_handler(regs);
472 #ifdef CONFIG_X86_LOCAL_APIC
473 if (using_apic_timer)
474 smp_send_timer_broadcast_ipi();
479 static unsigned int cyc2ns_scale __read_mostly;
481 static inline void set_cyc2ns_scale(unsigned long cpu_khz)
483 cyc2ns_scale = (NSEC_PER_MSEC << NS_SCALE) / cpu_khz;
486 static inline unsigned long long cycles_2_ns(unsigned long long cyc)
488 return (cyc * cyc2ns_scale) >> NS_SCALE;
491 unsigned long long sched_clock(void)
496 /* Don't do a HPET read here. Using TSC always is much faster
497 and HPET may not be mapped yet when the scheduler first runs.
498 Disadvantage is a small drift between CPUs in some configurations,
499 but that should be tolerable. */
500 if (__vxtime.mode == VXTIME_HPET)
501 return (hpet_readl(HPET_COUNTER) * vxtime.quot) >> US_SCALE;
504 /* Could do CPU core sync here. Opteron can execute rdtsc speculatively,
505 which means it is not completely exact and may not be monotonous between
506 CPUs. But the errors should be too small to matter for scheduling
510 return cycles_2_ns(a);
513 static unsigned long get_cmos_time(void)
515 unsigned int year, mon, day, hour, min, sec;
517 unsigned extyear = 0;
519 spin_lock_irqsave(&rtc_lock, flags);
522 sec = CMOS_READ(RTC_SECONDS);
523 min = CMOS_READ(RTC_MINUTES);
524 hour = CMOS_READ(RTC_HOURS);
525 day = CMOS_READ(RTC_DAY_OF_MONTH);
526 mon = CMOS_READ(RTC_MONTH);
527 year = CMOS_READ(RTC_YEAR);
529 if (acpi_fadt.revision >= FADT2_REVISION_ID &&
531 extyear = CMOS_READ(acpi_fadt.century);
533 } while (sec != CMOS_READ(RTC_SECONDS));
535 spin_unlock_irqrestore(&rtc_lock, flags);
538 * We know that x86-64 always uses BCD format, no need to check the
552 printk(KERN_INFO "Extended CMOS year: %d\n", extyear);
555 * x86-64 systems only exists since 2002.
556 * This will work up to Dec 31, 2100
561 return mktime(year, mon, day, hour, min, sec);
564 #ifdef CONFIG_CPU_FREQ
566 /* Frequency scaling support. Adjust the TSC based timer when the cpu frequency
569 RED-PEN: On SMP we assume all CPUs run with the same frequency. It's
570 not that important because current Opteron setups do not support
571 scaling on SMP anyroads.
573 Should fix up last_tsc too. Currently gettimeofday in the
574 first tick after the change will be slightly wrong. */
576 #include <linux/workqueue.h>
578 static unsigned int cpufreq_delayed_issched = 0;
579 static unsigned int cpufreq_init = 0;
580 static struct work_struct cpufreq_delayed_get_work;
582 static void handle_cpufreq_delayed_get(void *v)
585 for_each_online_cpu(cpu) {
588 cpufreq_delayed_issched = 0;
591 /* if we notice lost ticks, schedule a call to cpufreq_get() as it tries
592 * to verify the CPU frequency the timing core thinks the CPU is running
593 * at is still correct.
595 static void cpufreq_delayed_get(void)
598 if (cpufreq_init && !cpufreq_delayed_issched) {
599 cpufreq_delayed_issched = 1;
603 "Losing some ticks... checking if CPU frequency changed.\n");
605 schedule_work(&cpufreq_delayed_get_work);
609 static unsigned int ref_freq = 0;
610 static unsigned long loops_per_jiffy_ref = 0;
612 static unsigned long cpu_khz_ref = 0;
614 static int time_cpufreq_notifier(struct notifier_block *nb, unsigned long val,
617 struct cpufreq_freqs *freq = data;
618 unsigned long *lpj, dummy;
620 if (cpu_has(&cpu_data[freq->cpu], X86_FEATURE_CONSTANT_TSC))
624 if (!(freq->flags & CPUFREQ_CONST_LOOPS))
626 lpj = &cpu_data[freq->cpu].loops_per_jiffy;
628 lpj = &boot_cpu_data.loops_per_jiffy;
632 ref_freq = freq->old;
633 loops_per_jiffy_ref = *lpj;
634 cpu_khz_ref = cpu_khz;
636 if ((val == CPUFREQ_PRECHANGE && freq->old < freq->new) ||
637 (val == CPUFREQ_POSTCHANGE && freq->old > freq->new) ||
638 (val == CPUFREQ_RESUMECHANGE)) {
640 cpufreq_scale(loops_per_jiffy_ref, ref_freq, freq->new);
642 cpu_khz = cpufreq_scale(cpu_khz_ref, ref_freq, freq->new);
643 if (!(freq->flags & CPUFREQ_CONST_LOOPS))
644 vxtime.tsc_quot = (USEC_PER_MSEC << US_SCALE) / cpu_khz;
647 set_cyc2ns_scale(cpu_khz_ref);
652 static struct notifier_block time_cpufreq_notifier_block = {
653 .notifier_call = time_cpufreq_notifier
656 static int __init cpufreq_tsc(void)
658 INIT_WORK(&cpufreq_delayed_get_work, handle_cpufreq_delayed_get, NULL);
659 if (!cpufreq_register_notifier(&time_cpufreq_notifier_block,
660 CPUFREQ_TRANSITION_NOTIFIER))
665 core_initcall(cpufreq_tsc);
670 * calibrate_tsc() calibrates the processor TSC in a very simple way, comparing
671 * it to the HPET timer of known frequency.
674 #define TICK_COUNT 100000000
676 static unsigned int __init hpet_calibrate_tsc(void)
678 int tsc_start, hpet_start;
679 int tsc_now, hpet_now;
682 local_irq_save(flags);
685 hpet_start = hpet_readl(HPET_COUNTER);
690 hpet_now = hpet_readl(HPET_COUNTER);
691 tsc_now = get_cycles_sync();
692 local_irq_restore(flags);
693 } while ((tsc_now - tsc_start) < TICK_COUNT &&
694 (hpet_now - hpet_start) < TICK_COUNT);
696 return (tsc_now - tsc_start) * 1000000000L
697 / ((hpet_now - hpet_start) * hpet_period / 1000);
702 * pit_calibrate_tsc() uses the speaker output (channel 2) of
703 * the PIT. This is better than using the timer interrupt output,
704 * because we can read the value of the speaker with just one inb(),
705 * where we need three i/o operations for the interrupt channel.
706 * We count how many ticks the TSC does in 50 ms.
709 static unsigned int __init pit_calibrate_tsc(void)
711 unsigned long start, end;
714 spin_lock_irqsave(&i8253_lock, flags);
716 outb((inb(0x61) & ~0x02) | 0x01, 0x61);
719 outb((PIT_TICK_RATE / (1000 / 50)) & 0xff, 0x42);
720 outb((PIT_TICK_RATE / (1000 / 50)) >> 8, 0x42);
721 start = get_cycles_sync();
722 while ((inb(0x61) & 0x20) == 0);
723 end = get_cycles_sync();
725 spin_unlock_irqrestore(&i8253_lock, flags);
727 return (end - start) / 50;
731 static __init int late_hpet_init(void)
736 if (!vxtime.hpet_address)
739 memset(&hd, 0, sizeof (hd));
741 ntimer = hpet_readl(HPET_ID);
742 ntimer = (ntimer & HPET_ID_NUMBER) >> HPET_ID_NUMBER_SHIFT;
746 * Register with driver.
747 * Timer0 and Timer1 is used by platform.
749 hd.hd_phys_address = vxtime.hpet_address;
750 hd.hd_address = (void __iomem *)fix_to_virt(FIX_HPET_BASE);
751 hd.hd_nirqs = ntimer;
752 hd.hd_flags = HPET_DATA_PLATFORM;
753 hpet_reserve_timer(&hd, 0);
754 #ifdef CONFIG_HPET_EMULATE_RTC
755 hpet_reserve_timer(&hd, 1);
757 hd.hd_irq[0] = HPET_LEGACY_8254;
758 hd.hd_irq[1] = HPET_LEGACY_RTC;
761 struct hpet_timer *timer;
764 hpet = (struct hpet *) fix_to_virt(FIX_HPET_BASE);
765 timer = &hpet->hpet_timers[2];
766 for (i = 2; i < ntimer; timer++, i++)
767 hd.hd_irq[i] = (timer->hpet_config &
768 Tn_INT_ROUTE_CNF_MASK) >>
769 Tn_INT_ROUTE_CNF_SHIFT;
776 fs_initcall(late_hpet_init);
779 static int hpet_timer_stop_set_go(unsigned long tick)
784 * Stop the timers and reset the main counter.
787 cfg = hpet_readl(HPET_CFG);
788 cfg &= ~(HPET_CFG_ENABLE | HPET_CFG_LEGACY);
789 hpet_writel(cfg, HPET_CFG);
790 hpet_writel(0, HPET_COUNTER);
791 hpet_writel(0, HPET_COUNTER + 4);
794 * Set up timer 0, as periodic with first interrupt to happen at hpet_tick,
795 * and period also hpet_tick.
797 if (hpet_use_timer) {
798 hpet_writel(HPET_TN_ENABLE | HPET_TN_PERIODIC | HPET_TN_SETVAL |
799 HPET_TN_32BIT, HPET_T0_CFG);
800 hpet_writel(hpet_tick, HPET_T0_CMP); /* next interrupt */
801 hpet_writel(hpet_tick, HPET_T0_CMP); /* period */
802 cfg |= HPET_CFG_LEGACY;
808 cfg |= HPET_CFG_ENABLE;
809 hpet_writel(cfg, HPET_CFG);
814 static int hpet_init(void)
818 if (!vxtime.hpet_address)
820 set_fixmap_nocache(FIX_HPET_BASE, vxtime.hpet_address);
821 __set_fixmap(VSYSCALL_HPET, vxtime.hpet_address, PAGE_KERNEL_VSYSCALL_NOCACHE);
824 * Read the period, compute tick and quotient.
827 id = hpet_readl(HPET_ID);
829 if (!(id & HPET_ID_VENDOR) || !(id & HPET_ID_NUMBER))
832 hpet_period = hpet_readl(HPET_PERIOD);
833 if (hpet_period < 100000 || hpet_period > 100000000)
836 hpet_tick = (FSEC_PER_TICK + hpet_period / 2) / hpet_period;
838 hpet_use_timer = (id & HPET_ID_LEGSUP);
840 return hpet_timer_stop_set_go(hpet_tick);
843 static int hpet_reenable(void)
845 return hpet_timer_stop_set_go(hpet_tick);
848 #define PIT_MODE 0x43
851 static void __init __pit_init(int val, u8 mode)
855 spin_lock_irqsave(&i8253_lock, flags);
856 outb_p(mode, PIT_MODE);
857 outb_p(val & 0xff, PIT_CH0); /* LSB */
858 outb_p(val >> 8, PIT_CH0); /* MSB */
859 spin_unlock_irqrestore(&i8253_lock, flags);
862 void __init pit_init(void)
864 __pit_init(LATCH, 0x34); /* binary, mode 2, LSB/MSB, ch 0 */
867 void __init pit_stop_interrupt(void)
869 __pit_init(0, 0x30); /* mode 0 */
872 void __init stop_timer_interrupt(void)
875 if (vxtime.hpet_address) {
877 hpet_timer_stop_set_go(0);
880 pit_stop_interrupt();
882 printk(KERN_INFO "timer: %s interrupt stopped.\n", name);
885 int __init time_setup(char *str)
887 report_lost_ticks = 1;
891 static struct irqaction irq0 = {
892 timer_interrupt, IRQF_DISABLED, CPU_MASK_NONE, "timer", NULL, NULL
895 void __init time_init(void)
901 vxtime.hpet_address = 0;
903 xtime.tv_sec = get_cmos_time();
906 set_normalized_timespec(&wall_to_monotonic,
907 -xtime.tv_sec, -xtime.tv_nsec);
910 vxtime_hz = (FSEC_PER_SEC + hpet_period / 2) / hpet_period;
912 vxtime.hpet_address = 0;
914 if (hpet_use_timer) {
915 /* set tick_nsec to use the proper rate for HPET */
916 tick_nsec = TICK_NSEC_HPET;
917 cpu_khz = hpet_calibrate_tsc();
919 #ifdef CONFIG_X86_PM_TIMER
920 } else if (pmtmr_ioport && !vxtime.hpet_address) {
921 vxtime_hz = PM_TIMER_FREQUENCY;
924 cpu_khz = pit_calibrate_tsc();
928 cpu_khz = pit_calibrate_tsc();
932 vxtime.mode = VXTIME_TSC;
933 gtod = time_init_gtod();
935 printk(KERN_INFO "time.c: Using %ld.%06ld MHz WALL %s GTOD %s timer.\n",
936 vxtime_hz / 1000000, vxtime_hz % 1000000, timename, gtod);
937 printk(KERN_INFO "time.c: Detected %d.%03d MHz processor.\n",
938 cpu_khz / 1000, cpu_khz % 1000);
939 vxtime.quot = (USEC_PER_SEC << US_SCALE) / vxtime_hz;
940 vxtime.tsc_quot = (USEC_PER_MSEC << US_SCALE) / cpu_khz;
941 vxtime.last_tsc = get_cycles_sync();
944 set_cyc2ns_scale(cpu_khz);
948 * Make an educated guess if the TSC is trustworthy and synchronized
951 __cpuinit int unsynchronized_tsc(void)
954 if (apic_is_clustered_box())
956 /* Intel systems are normally all synchronized. Exceptions
957 are handled in the check above. */
958 if (boot_cpu_data.x86_vendor == X86_VENDOR_INTEL)
961 /* Assume multi socket systems are not synchronized */
962 return num_present_cpus() > 1;
966 * Decide what mode gettimeofday should use.
968 __init static char *time_init_gtod(void)
972 if (unsynchronized_tsc())
974 if (vxtime.hpet_address && notsc) {
975 timetype = hpet_use_timer ? "HPET" : "PIT/HPET";
977 vxtime.last = hpet_readl(HPET_T0_CMP) - hpet_tick;
979 vxtime.last = hpet_readl(HPET_COUNTER);
980 vxtime.mode = VXTIME_HPET;
981 do_gettimeoffset = do_gettimeoffset_hpet;
982 #ifdef CONFIG_X86_PM_TIMER
983 /* Using PM for gettimeofday is quite slow, but we have no other
984 choice because the TSC is too unreliable on some systems. */
985 } else if (pmtmr_ioport && !vxtime.hpet_address && notsc) {
987 do_gettimeoffset = do_gettimeoffset_pm;
988 vxtime.mode = VXTIME_PMTMR;
990 printk(KERN_INFO "Disabling vsyscall due to use of PM timer\n");
993 timetype = hpet_use_timer ? "HPET/TSC" : "PIT/TSC";
994 vxtime.mode = VXTIME_TSC;
999 __setup("report_lost_ticks", time_setup);
1001 static long clock_cmos_diff;
1002 static unsigned long sleep_start;
1005 * sysfs support for the timer.
1008 static int timer_suspend(struct sys_device *dev, pm_message_t state)
1011 * Estimate time zone so that set_time can update the clock
1013 long cmos_time = get_cmos_time();
1015 clock_cmos_diff = -cmos_time;
1016 clock_cmos_diff += get_seconds();
1017 sleep_start = cmos_time;
1021 static int timer_resume(struct sys_device *dev)
1023 unsigned long flags;
1025 unsigned long ctime = get_cmos_time();
1026 unsigned long sleep_length = (ctime - sleep_start) * HZ;
1028 if (vxtime.hpet_address)
1031 i8254_timer_resume();
1033 sec = ctime + clock_cmos_diff;
1034 write_seqlock_irqsave(&xtime_lock,flags);
1037 if (vxtime.mode == VXTIME_HPET) {
1039 vxtime.last = hpet_readl(HPET_T0_CMP) - hpet_tick;
1041 vxtime.last = hpet_readl(HPET_COUNTER);
1042 #ifdef CONFIG_X86_PM_TIMER
1043 } else if (vxtime.mode == VXTIME_PMTMR) {
1047 vxtime.last_tsc = get_cycles_sync();
1048 write_sequnlock_irqrestore(&xtime_lock,flags);
1049 jiffies += sleep_length;
1050 wall_jiffies += sleep_length;
1051 monotonic_base += sleep_length * (NSEC_PER_SEC/HZ);
1052 touch_softlockup_watchdog();
1056 static struct sysdev_class timer_sysclass = {
1057 .resume = timer_resume,
1058 .suspend = timer_suspend,
1059 set_kset_name("timer"),
1062 /* XXX this driverfs stuff should probably go elsewhere later -john */
1063 static struct sys_device device_timer = {
1065 .cls = &timer_sysclass,
1068 static int time_init_device(void)
1070 int error = sysdev_class_register(&timer_sysclass);
1072 error = sysdev_register(&device_timer);
1076 device_initcall(time_init_device);
1078 #ifdef CONFIG_HPET_EMULATE_RTC
1079 /* HPET in LegacyReplacement Mode eats up RTC interrupt line. When, HPET
1080 * is enabled, we support RTC interrupt functionality in software.
1081 * RTC has 3 kinds of interrupts:
1082 * 1) Update Interrupt - generate an interrupt, every sec, when RTC clock
1084 * 2) Alarm Interrupt - generate an interrupt at a specific time of day
1085 * 3) Periodic Interrupt - generate periodic interrupt, with frequencies
1086 * 2Hz-8192Hz (2Hz-64Hz for non-root user) (all freqs in powers of 2)
1087 * (1) and (2) above are implemented using polling at a frequency of
1088 * 64 Hz. The exact frequency is a tradeoff between accuracy and interrupt
1089 * overhead. (DEFAULT_RTC_INT_FREQ)
1090 * For (3), we use interrupts at 64Hz or user specified periodic
1091 * frequency, whichever is higher.
1093 #include <linux/rtc.h>
1095 #define DEFAULT_RTC_INT_FREQ 64
1096 #define RTC_NUM_INTS 1
1098 static unsigned long UIE_on;
1099 static unsigned long prev_update_sec;
1101 static unsigned long AIE_on;
1102 static struct rtc_time alarm_time;
1104 static unsigned long PIE_on;
1105 static unsigned long PIE_freq = DEFAULT_RTC_INT_FREQ;
1106 static unsigned long PIE_count;
1108 static unsigned long hpet_rtc_int_freq; /* RTC interrupt frequency */
1109 static unsigned int hpet_t1_cmp; /* cached comparator register */
1111 int is_hpet_enabled(void)
1113 return vxtime.hpet_address != 0;
1117 * Timer 1 for RTC, we do not use periodic interrupt feature,
1118 * even if HPET supports periodic interrupts on Timer 1.
1119 * The reason being, to set up a periodic interrupt in HPET, we need to
1120 * stop the main counter. And if we do that everytime someone diables/enables
1121 * RTC, we will have adverse effect on main kernel timer running on Timer 0.
1122 * So, for the time being, simulate the periodic interrupt in software.
1124 * hpet_rtc_timer_init() is called for the first time and during subsequent
1125 * interuppts reinit happens through hpet_rtc_timer_reinit().
1127 int hpet_rtc_timer_init(void)
1129 unsigned int cfg, cnt;
1130 unsigned long flags;
1132 if (!is_hpet_enabled())
1135 * Set the counter 1 and enable the interrupts.
1137 if (PIE_on && (PIE_freq > DEFAULT_RTC_INT_FREQ))
1138 hpet_rtc_int_freq = PIE_freq;
1140 hpet_rtc_int_freq = DEFAULT_RTC_INT_FREQ;
1142 local_irq_save(flags);
1143 cnt = hpet_readl(HPET_COUNTER);
1144 cnt += ((hpet_tick*HZ)/hpet_rtc_int_freq);
1145 hpet_writel(cnt, HPET_T1_CMP);
1147 local_irq_restore(flags);
1149 cfg = hpet_readl(HPET_T1_CFG);
1150 cfg &= ~HPET_TN_PERIODIC;
1151 cfg |= HPET_TN_ENABLE | HPET_TN_32BIT;
1152 hpet_writel(cfg, HPET_T1_CFG);
1157 static void hpet_rtc_timer_reinit(void)
1159 unsigned int cfg, cnt;
1161 if (unlikely(!(PIE_on | AIE_on | UIE_on))) {
1162 cfg = hpet_readl(HPET_T1_CFG);
1163 cfg &= ~HPET_TN_ENABLE;
1164 hpet_writel(cfg, HPET_T1_CFG);
1168 if (PIE_on && (PIE_freq > DEFAULT_RTC_INT_FREQ))
1169 hpet_rtc_int_freq = PIE_freq;
1171 hpet_rtc_int_freq = DEFAULT_RTC_INT_FREQ;
1173 /* It is more accurate to use the comparator value than current count.*/
1175 cnt += hpet_tick*HZ/hpet_rtc_int_freq;
1176 hpet_writel(cnt, HPET_T1_CMP);
1181 * The functions below are called from rtc driver.
1182 * Return 0 if HPET is not being used.
1183 * Otherwise do the necessary changes and return 1.
1185 int hpet_mask_rtc_irq_bit(unsigned long bit_mask)
1187 if (!is_hpet_enabled())
1190 if (bit_mask & RTC_UIE)
1192 if (bit_mask & RTC_PIE)
1194 if (bit_mask & RTC_AIE)
1200 int hpet_set_rtc_irq_bit(unsigned long bit_mask)
1202 int timer_init_reqd = 0;
1204 if (!is_hpet_enabled())
1207 if (!(PIE_on | AIE_on | UIE_on))
1208 timer_init_reqd = 1;
1210 if (bit_mask & RTC_UIE) {
1213 if (bit_mask & RTC_PIE) {
1217 if (bit_mask & RTC_AIE) {
1221 if (timer_init_reqd)
1222 hpet_rtc_timer_init();
1227 int hpet_set_alarm_time(unsigned char hrs, unsigned char min, unsigned char sec)
1229 if (!is_hpet_enabled())
1232 alarm_time.tm_hour = hrs;
1233 alarm_time.tm_min = min;
1234 alarm_time.tm_sec = sec;
1239 int hpet_set_periodic_freq(unsigned long freq)
1241 if (!is_hpet_enabled())
1250 int hpet_rtc_dropped_irq(void)
1252 if (!is_hpet_enabled())
1258 irqreturn_t hpet_rtc_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1260 struct rtc_time curr_time;
1261 unsigned long rtc_int_flag = 0;
1262 int call_rtc_interrupt = 0;
1264 hpet_rtc_timer_reinit();
1266 if (UIE_on | AIE_on) {
1267 rtc_get_rtc_time(&curr_time);
1270 if (curr_time.tm_sec != prev_update_sec) {
1271 /* Set update int info, call real rtc int routine */
1272 call_rtc_interrupt = 1;
1273 rtc_int_flag = RTC_UF;
1274 prev_update_sec = curr_time.tm_sec;
1279 if (PIE_count >= hpet_rtc_int_freq/PIE_freq) {
1280 /* Set periodic int info, call real rtc int routine */
1281 call_rtc_interrupt = 1;
1282 rtc_int_flag |= RTC_PF;
1287 if ((curr_time.tm_sec == alarm_time.tm_sec) &&
1288 (curr_time.tm_min == alarm_time.tm_min) &&
1289 (curr_time.tm_hour == alarm_time.tm_hour)) {
1290 /* Set alarm int info, call real rtc int routine */
1291 call_rtc_interrupt = 1;
1292 rtc_int_flag |= RTC_AF;
1295 if (call_rtc_interrupt) {
1296 rtc_int_flag |= (RTC_IRQF | (RTC_NUM_INTS << 8));
1297 rtc_interrupt(rtc_int_flag, dev_id, regs);
1303 static int __init nohpet_setup(char *s)
1309 __setup("nohpet", nohpet_setup);
1311 int __init notsc_setup(char *s)
1317 __setup("notsc", notsc_setup);