78cbdf5c006b68d5461ec1b4ed8e0750c40369fb
[safe/jmp/linux-2.6] / arch / x86 / kernel / time_64.c
1 /*
2  *  Copyright (c) 1991,1992,1995  Linus Torvalds
3  *  Copyright (c) 1994  Alan Modra
4  *  Copyright (c) 1995  Markus Kuhn
5  *  Copyright (c) 1996  Ingo Molnar
6  *  Copyright (c) 1998  Andrea Arcangeli
7  *  Copyright (c) 2002,2006  Vojtech Pavlik
8  *  Copyright (c) 2003  Andi Kleen
9  *
10  */
11
12 #include <linux/clockchips.h>
13 #include <linux/interrupt.h>
14 #include <linux/time.h>
15 #include <linux/mca.h>
16
17 #include <asm/vsyscall.h>
18 #include <asm/x86_init.h>
19 #include <asm/i8259.h>
20 #include <asm/i8253.h>
21 #include <asm/timer.h>
22 #include <asm/hpet.h>
23 #include <asm/time.h>
24 #include <asm/nmi.h>
25
26 #if defined(CONFIG_X86_32) && defined(CONFIG_X86_IO_APIC)
27 int timer_ack;
28 #endif
29
30 #ifdef CONFIG_X86_64
31 volatile unsigned long __jiffies __section_jiffies = INITIAL_JIFFIES;
32 #endif
33
34 unsigned long profile_pc(struct pt_regs *regs)
35 {
36         unsigned long pc = instruction_pointer(regs);
37
38         /* Assume the lock function has either no stack frame or a copy
39            of flags from PUSHF
40            Eflags always has bits 22 and up cleared unlike kernel addresses. */
41         if (!user_mode_vm(regs) && in_lock_functions(pc)) {
42 #ifdef CONFIG_FRAME_POINTER
43                 return *(unsigned long *)(regs->bp + sizeof(long));
44 #else
45                 unsigned long *sp = (unsigned long *)regs->sp;
46                 if (sp[0] >> 22)
47                         return sp[0];
48                 if (sp[1] >> 22)
49                         return sp[1];
50 #endif
51         }
52         return pc;
53 }
54 EXPORT_SYMBOL(profile_pc);
55
56 /*
57  * Default timer interrupt handler for PIT/HPET
58  */
59 static irqreturn_t timer_interrupt(int irq, void *dev_id)
60 {
61         /* Keep nmi watchdog up to date */
62         inc_irq_stat(irq0_irqs);
63
64         /* Optimized out for !IO_APIC and x86_64 */
65         if (timer_ack) {
66                 /*
67                  * Subtle, when I/O APICs are used we have to ack timer IRQ
68                  * manually to deassert NMI lines for the watchdog if run
69                  * on an 82489DX-based system.
70                  */
71                 spin_lock(&i8259A_lock);
72                 outb(0x0c, PIC_MASTER_OCW3);
73                 /* Ack the IRQ; AEOI will end it automatically. */
74                 inb(PIC_MASTER_POLL);
75                 spin_unlock(&i8259A_lock);
76         }
77
78         global_clock_event->event_handler(global_clock_event);
79
80         /* MCA bus quirk: Acknowledge irq0 by setting bit 7 in port 0x61 */
81         if (MCA_bus)
82                 outb_p(inb_p(0x61)| 0x80, 0x61);
83
84         return IRQ_HANDLED;
85 }
86
87 /*
88  * calibrate_cpu is used on systems with fixed rate TSCs to determine
89  * processor frequency
90  */
91 #define TICK_COUNT 100000000
92 unsigned long __init calibrate_cpu(void)
93 {
94         int tsc_start, tsc_now;
95         int i, no_ctr_free;
96         unsigned long evntsel3 = 0, pmc3 = 0, pmc_now = 0;
97         unsigned long flags;
98
99         for (i = 0; i < 4; i++)
100                 if (avail_to_resrv_perfctr_nmi_bit(i))
101                         break;
102         no_ctr_free = (i == 4);
103         if (no_ctr_free) {
104                 WARN(1, KERN_WARNING "Warning: AMD perfctrs busy ... "
105                      "cpu_khz value may be incorrect.\n");
106                 i = 3;
107                 rdmsrl(MSR_K7_EVNTSEL3, evntsel3);
108                 wrmsrl(MSR_K7_EVNTSEL3, 0);
109                 rdmsrl(MSR_K7_PERFCTR3, pmc3);
110         } else {
111                 reserve_perfctr_nmi(MSR_K7_PERFCTR0 + i);
112                 reserve_evntsel_nmi(MSR_K7_EVNTSEL0 + i);
113         }
114         local_irq_save(flags);
115         /* start measuring cycles, incrementing from 0 */
116         wrmsrl(MSR_K7_PERFCTR0 + i, 0);
117         wrmsrl(MSR_K7_EVNTSEL0 + i, 1 << 22 | 3 << 16 | 0x76);
118         rdtscl(tsc_start);
119         do {
120                 rdmsrl(MSR_K7_PERFCTR0 + i, pmc_now);
121                 tsc_now = get_cycles();
122         } while ((tsc_now - tsc_start) < TICK_COUNT);
123
124         local_irq_restore(flags);
125         if (no_ctr_free) {
126                 wrmsrl(MSR_K7_EVNTSEL3, 0);
127                 wrmsrl(MSR_K7_PERFCTR3, pmc3);
128                 wrmsrl(MSR_K7_EVNTSEL3, evntsel3);
129         } else {
130                 release_perfctr_nmi(MSR_K7_PERFCTR0 + i);
131                 release_evntsel_nmi(MSR_K7_EVNTSEL0 + i);
132         }
133
134         return pmc_now * tsc_khz / (tsc_now - tsc_start);
135 }
136
137 static struct irqaction irq0  = {
138         .handler = timer_interrupt,
139         .flags = IRQF_DISABLED | IRQF_NOBALANCING | IRQF_IRQPOLL | IRQF_TIMER,
140         .name = "timer"
141 };
142
143 void __init setup_default_timer_irq(void)
144 {
145         irq0.mask = cpumask_of_cpu(0);
146         setup_irq(0, &irq0);
147 }
148
149 /* Default timer init function */
150 void __init hpet_time_init(void)
151 {
152         if (!hpet_enable())
153                 setup_pit_timer();
154         setup_default_timer_irq();
155 }
156
157 static void x86_late_time_init(void)
158 {
159         x86_init.timers.timer_init();
160 }
161
162 /*
163  * Initialize TSC and delay the periodic timer init to
164  * late x86_late_time_init() so ioremap works.
165  */
166 void __init time_init(void)
167 {
168         tsc_init();
169         late_time_init = x86_late_time_init;
170 }