x86-64: add "local_apic_timer_c2_ok" here too
[linux-2.6-block.git] / arch / x86_64 / kernel / apic.c
CommitLineData
1da177e4
LT
1/*
2 * Local APIC handling, local APIC timers
3 *
4 * (c) 1999, 2000 Ingo Molnar <mingo@redhat.com>
5 *
6 * Fixes
7 * Maciej W. Rozycki : Bits for genuine 82489DX APICs;
8 * thanks to Eric Gilmore
9 * and Rolf G. Tews
10 * for testing these extensively.
11 * Maciej W. Rozycki : Various updates and fixes.
12 * Mikael Pettersson : Power Management for UP-APIC.
13 * Pavel Machek and
14 * Mikael Pettersson : PM converted to driver model.
15 */
16
1da177e4
LT
17#include <linux/init.h>
18
19#include <linux/mm.h>
1da177e4
LT
20#include <linux/delay.h>
21#include <linux/bootmem.h>
22#include <linux/smp_lock.h>
23#include <linux/interrupt.h>
24#include <linux/mc146818rtc.h>
25#include <linux/kernel_stat.h>
26#include <linux/sysdev.h>
d25bf7e5 27#include <linux/module.h>
39928722 28#include <linux/ioport.h>
1da177e4
LT
29
30#include <asm/atomic.h>
31#include <asm/smp.h>
32#include <asm/mtrr.h>
33#include <asm/mpspec.h>
34#include <asm/pgalloc.h>
35#include <asm/mach_apic.h>
75152114 36#include <asm/nmi.h>
95833c83 37#include <asm/idle.h>
73dea47f
AK
38#include <asm/proto.h>
39#include <asm/timex.h>
2d0c87c3 40#include <asm/hpet.h>
2c8c0e6b 41#include <asm/apic.h>
1da177e4 42
b7f5e3c7 43int apic_mapped;
1da177e4 44int apic_verbosity;
73dea47f 45int apic_runs_main_timer;
0c3749c4 46int apic_calibrate_pmtmr __initdata;
1da177e4
LT
47
48int disable_apic_timer __initdata;
49
2e7c2838
LT
50/* Local APIC timer works in C2? */
51int local_apic_timer_c2_ok;
52EXPORT_SYMBOL_GPL(local_apic_timer_c2_ok);
53
39928722
AD
54static struct resource *ioapic_resources;
55static struct resource lapic_resource = {
56 .name = "Local APIC",
57 .flags = IORESOURCE_MEM | IORESOURCE_BUSY,
58};
59
d25bf7e5
VP
60/*
61 * cpu_mask that denotes the CPUs that needs timer interrupt coming in as
62 * IPIs in place of local APIC timers
63 */
64static cpumask_t timer_interrupt_broadcast_ipi_mask;
65
1da177e4 66/* Using APIC to generate smp_local_timer_interrupt? */
acae9d32 67int using_apic_timer __read_mostly = 0;
1da177e4 68
1da177e4
LT
69static void apic_pm_activate(void);
70
71void enable_NMI_through_LVT0 (void * dummy)
72{
11a8e778 73 unsigned int v;
1da177e4 74
1da177e4 75 v = APIC_DM_NMI; /* unmask and set to NMI */
11a8e778 76 apic_write(APIC_LVT0, v);
1da177e4
LT
77}
78
79int get_maxlvt(void)
80{
11a8e778 81 unsigned int v, maxlvt;
1da177e4
LT
82
83 v = apic_read(APIC_LVR);
1da177e4
LT
84 maxlvt = GET_APIC_MAXLVT(v);
85 return maxlvt;
86}
87
3777a959
AK
88/*
89 * 'what should we do if we get a hw irq event on an illegal vector'.
90 * each architecture has to answer this themselves.
91 */
92void ack_bad_irq(unsigned int irq)
93{
94 printk("unexpected IRQ trap at vector %02x\n", irq);
95 /*
96 * Currently unexpected vectors happen only on SMP and APIC.
97 * We _must_ ack these because every local APIC has only N
98 * irq slots per priority level, and a 'hanging, unacked' IRQ
99 * holds up an irq slot - in excessive cases (when multiple
100 * unexpected vectors occur) that might lock up the APIC
101 * completely.
102 * But don't ack when the APIC is disabled. -AK
103 */
104 if (!disable_apic)
105 ack_APIC_irq();
106}
107
1da177e4
LT
108void clear_local_APIC(void)
109{
110 int maxlvt;
111 unsigned int v;
112
113 maxlvt = get_maxlvt();
114
115 /*
704fc59e 116 * Masking an LVT entry can trigger a local APIC error
1da177e4
LT
117 * if the vector is zero. Mask LVTERR first to prevent this.
118 */
119 if (maxlvt >= 3) {
120 v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
11a8e778 121 apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
1da177e4
LT
122 }
123 /*
124 * Careful: we have to set masks only first to deassert
125 * any level-triggered sources.
126 */
127 v = apic_read(APIC_LVTT);
11a8e778 128 apic_write(APIC_LVTT, v | APIC_LVT_MASKED);
1da177e4 129 v = apic_read(APIC_LVT0);
11a8e778 130 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
1da177e4 131 v = apic_read(APIC_LVT1);
11a8e778 132 apic_write(APIC_LVT1, v | APIC_LVT_MASKED);
1da177e4
LT
133 if (maxlvt >= 4) {
134 v = apic_read(APIC_LVTPC);
11a8e778 135 apic_write(APIC_LVTPC, v | APIC_LVT_MASKED);
1da177e4
LT
136 }
137
138 /*
139 * Clean APIC state for other OSs:
140 */
11a8e778
AK
141 apic_write(APIC_LVTT, APIC_LVT_MASKED);
142 apic_write(APIC_LVT0, APIC_LVT_MASKED);
143 apic_write(APIC_LVT1, APIC_LVT_MASKED);
1da177e4 144 if (maxlvt >= 3)
11a8e778 145 apic_write(APIC_LVTERR, APIC_LVT_MASKED);
1da177e4 146 if (maxlvt >= 4)
11a8e778 147 apic_write(APIC_LVTPC, APIC_LVT_MASKED);
5a40b7c2
AK
148 apic_write(APIC_ESR, 0);
149 apic_read(APIC_ESR);
1da177e4
LT
150}
151
208fb931 152void disconnect_bsp_APIC(int virt_wire_setup)
1da177e4 153{
a8fcf1a2
AK
154 /* Go back to Virtual Wire compatibility mode */
155 unsigned long value;
208fb931 156
a8fcf1a2
AK
157 /* For the spurious interrupt use vector F, and enable it */
158 value = apic_read(APIC_SPIV);
159 value &= ~APIC_VECTOR_MASK;
160 value |= APIC_SPIV_APIC_ENABLED;
161 value |= 0xf;
162 apic_write(APIC_SPIV, value);
163
164 if (!virt_wire_setup) {
165 /* For LVT0 make it edge triggered, active high, external and enabled */
166 value = apic_read(APIC_LVT0);
167 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
208fb931 168 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
a8fcf1a2 169 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED );
208fb931 170 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
a8fcf1a2
AK
171 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
172 apic_write(APIC_LVT0, value);
173 } else {
174 /* Disable LVT0 */
175 apic_write(APIC_LVT0, APIC_LVT_MASKED);
208fb931 176 }
a8fcf1a2
AK
177
178 /* For LVT1 make it edge triggered, active high, nmi and enabled */
179 value = apic_read(APIC_LVT1);
180 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
181 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
182 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
183 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
184 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
185 apic_write(APIC_LVT1, value);
1da177e4
LT
186}
187
188void disable_local_APIC(void)
189{
190 unsigned int value;
191
192 clear_local_APIC();
193
194 /*
195 * Disable APIC (implies clearing of registers
196 * for 82489DX!).
197 */
198 value = apic_read(APIC_SPIV);
199 value &= ~APIC_SPIV_APIC_ENABLED;
11a8e778 200 apic_write(APIC_SPIV, value);
1da177e4
LT
201}
202
203/*
204 * This is to verify that we're looking at a real local APIC.
205 * Check these against your board if the CPUs aren't getting
206 * started for no apparent reason.
207 */
208int __init verify_local_APIC(void)
209{
210 unsigned int reg0, reg1;
211
212 /*
213 * The version register is read-only in a real APIC.
214 */
215 reg0 = apic_read(APIC_LVR);
216 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
217 apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
218 reg1 = apic_read(APIC_LVR);
219 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
220
221 /*
222 * The two version reads above should print the same
223 * numbers. If the second one is different, then we
224 * poke at a non-APIC.
225 */
226 if (reg1 != reg0)
227 return 0;
228
229 /*
230 * Check if the version looks reasonably.
231 */
232 reg1 = GET_APIC_VERSION(reg0);
233 if (reg1 == 0x00 || reg1 == 0xff)
234 return 0;
235 reg1 = get_maxlvt();
236 if (reg1 < 0x02 || reg1 == 0xff)
237 return 0;
238
239 /*
240 * The ID register is read/write in a real APIC.
241 */
242 reg0 = apic_read(APIC_ID);
243 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
244 apic_write(APIC_ID, reg0 ^ APIC_ID_MASK);
245 reg1 = apic_read(APIC_ID);
246 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg1);
247 apic_write(APIC_ID, reg0);
248 if (reg1 != (reg0 ^ APIC_ID_MASK))
249 return 0;
250
251 /*
252 * The next two are just to see if we have sane values.
253 * They're only really relevant if we're in Virtual Wire
254 * compatibility mode, but most boxes are anymore.
255 */
256 reg0 = apic_read(APIC_LVT0);
257 apic_printk(APIC_DEBUG,"Getting LVT0: %x\n", reg0);
258 reg1 = apic_read(APIC_LVT1);
259 apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
260
261 return 1;
262}
263
264void __init sync_Arb_IDs(void)
265{
266 /* Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 */
267 unsigned int ver = GET_APIC_VERSION(apic_read(APIC_LVR));
268 if (ver >= 0x14) /* P4 or higher */
269 return;
270
271 /*
272 * Wait for idle.
273 */
274 apic_wait_icr_idle();
275
276 apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
11a8e778 277 apic_write(APIC_ICR, APIC_DEST_ALLINC | APIC_INT_LEVELTRIG
1da177e4
LT
278 | APIC_DM_INIT);
279}
280
1da177e4
LT
281/*
282 * An initial setup of the virtual wire mode.
283 */
284void __init init_bsp_APIC(void)
285{
11a8e778 286 unsigned int value;
1da177e4
LT
287
288 /*
289 * Don't do the setup now if we have a SMP BIOS as the
290 * through-I/O-APIC virtual wire mode might be active.
291 */
292 if (smp_found_config || !cpu_has_apic)
293 return;
294
295 value = apic_read(APIC_LVR);
1da177e4
LT
296
297 /*
298 * Do not trust the local APIC being empty at bootup.
299 */
300 clear_local_APIC();
301
302 /*
303 * Enable APIC.
304 */
305 value = apic_read(APIC_SPIV);
306 value &= ~APIC_VECTOR_MASK;
307 value |= APIC_SPIV_APIC_ENABLED;
308 value |= APIC_SPIV_FOCUS_DISABLED;
309 value |= SPURIOUS_APIC_VECTOR;
11a8e778 310 apic_write(APIC_SPIV, value);
1da177e4
LT
311
312 /*
313 * Set up the virtual wire mode.
314 */
11a8e778 315 apic_write(APIC_LVT0, APIC_DM_EXTINT);
1da177e4 316 value = APIC_DM_NMI;
11a8e778 317 apic_write(APIC_LVT1, value);
1da177e4
LT
318}
319
e6982c67 320void __cpuinit setup_local_APIC (void)
1da177e4 321{
11a8e778 322 unsigned int value, maxlvt;
da7ed9f9 323 int i, j;
1da177e4 324
1da177e4 325 value = apic_read(APIC_LVR);
1da177e4 326
fe7414a2 327 BUILD_BUG_ON((SPURIOUS_APIC_VECTOR & 0x0f) != 0x0f);
1da177e4
LT
328
329 /*
330 * Double-check whether this APIC is really registered.
331 * This is meaningless in clustered apic mode, so we skip it.
332 */
333 if (!apic_id_registered())
334 BUG();
335
336 /*
337 * Intel recommends to set DFR, LDR and TPR before enabling
338 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
339 * document number 292116). So here it goes...
340 */
341 init_apic_ldr();
342
343 /*
344 * Set Task Priority to 'accept all'. We never change this
345 * later on.
346 */
347 value = apic_read(APIC_TASKPRI);
348 value &= ~APIC_TPRI_MASK;
11a8e778 349 apic_write(APIC_TASKPRI, value);
1da177e4 350
da7ed9f9
VG
351 /*
352 * After a crash, we no longer service the interrupts and a pending
353 * interrupt from previous kernel might still have ISR bit set.
354 *
355 * Most probably by now CPU has serviced that pending interrupt and
356 * it might not have done the ack_APIC_irq() because it thought,
357 * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
358 * does not clear the ISR bit and cpu thinks it has already serivced
359 * the interrupt. Hence a vector might get locked. It was noticed
360 * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
361 */
362 for (i = APIC_ISR_NR - 1; i >= 0; i--) {
363 value = apic_read(APIC_ISR + i*0x10);
364 for (j = 31; j >= 0; j--) {
365 if (value & (1<<j))
366 ack_APIC_irq();
367 }
368 }
369
1da177e4
LT
370 /*
371 * Now that we are all set up, enable the APIC
372 */
373 value = apic_read(APIC_SPIV);
374 value &= ~APIC_VECTOR_MASK;
375 /*
376 * Enable APIC
377 */
378 value |= APIC_SPIV_APIC_ENABLED;
379
3f14c746
AK
380 /* We always use processor focus */
381
1da177e4
LT
382 /*
383 * Set spurious IRQ vector
384 */
385 value |= SPURIOUS_APIC_VECTOR;
11a8e778 386 apic_write(APIC_SPIV, value);
1da177e4
LT
387
388 /*
389 * Set up LVT0, LVT1:
390 *
391 * set up through-local-APIC on the BP's LINT0. This is not
392 * strictly necessary in pure symmetric-IO mode, but sometimes
393 * we delegate interrupts to the 8259A.
394 */
395 /*
396 * TODO: set up through-local-APIC from through-I/O-APIC? --macro
397 */
398 value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
a8fcf1a2 399 if (!smp_processor_id() && !value) {
1da177e4
LT
400 value = APIC_DM_EXTINT;
401 apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n", smp_processor_id());
402 } else {
403 value = APIC_DM_EXTINT | APIC_LVT_MASKED;
404 apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n", smp_processor_id());
405 }
11a8e778 406 apic_write(APIC_LVT0, value);
1da177e4
LT
407
408 /*
409 * only the BP should see the LINT1 NMI signal, obviously.
410 */
411 if (!smp_processor_id())
412 value = APIC_DM_NMI;
413 else
414 value = APIC_DM_NMI | APIC_LVT_MASKED;
11a8e778 415 apic_write(APIC_LVT1, value);
1da177e4 416
61c11341 417 {
1da177e4
LT
418 unsigned oldvalue;
419 maxlvt = get_maxlvt();
1da177e4
LT
420 oldvalue = apic_read(APIC_ESR);
421 value = ERROR_APIC_VECTOR; // enables sending errors
11a8e778 422 apic_write(APIC_LVTERR, value);
1da177e4
LT
423 /*
424 * spec says clear errors after enabling vector.
425 */
426 if (maxlvt > 3)
427 apic_write(APIC_ESR, 0);
428 value = apic_read(APIC_ESR);
429 if (value != oldvalue)
430 apic_printk(APIC_VERBOSE,
431 "ESR value after enabling vector: %08x, after %08x\n",
432 oldvalue, value);
1da177e4
LT
433 }
434
435 nmi_watchdog_default();
f2802e7f 436 setup_apic_nmi_watchdog(NULL);
1da177e4
LT
437 apic_pm_activate();
438}
439
440#ifdef CONFIG_PM
441
442static struct {
443 /* 'active' is true if the local APIC was enabled by us and
444 not the BIOS; this signifies that we are also responsible
445 for disabling it before entering apm/acpi suspend */
446 int active;
447 /* r/w apic fields */
448 unsigned int apic_id;
449 unsigned int apic_taskpri;
450 unsigned int apic_ldr;
451 unsigned int apic_dfr;
452 unsigned int apic_spiv;
453 unsigned int apic_lvtt;
454 unsigned int apic_lvtpc;
455 unsigned int apic_lvt0;
456 unsigned int apic_lvt1;
457 unsigned int apic_lvterr;
458 unsigned int apic_tmict;
459 unsigned int apic_tdcr;
460 unsigned int apic_thmr;
461} apic_pm_state;
462
0b9c33a7 463static int lapic_suspend(struct sys_device *dev, pm_message_t state)
1da177e4
LT
464{
465 unsigned long flags;
f990fff4 466 int maxlvt;
1da177e4
LT
467
468 if (!apic_pm_state.active)
469 return 0;
470
f990fff4
KW
471 maxlvt = get_maxlvt();
472
1da177e4
LT
473 apic_pm_state.apic_id = apic_read(APIC_ID);
474 apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
475 apic_pm_state.apic_ldr = apic_read(APIC_LDR);
476 apic_pm_state.apic_dfr = apic_read(APIC_DFR);
477 apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
478 apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
f990fff4
KW
479 if (maxlvt >= 4)
480 apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
1da177e4
LT
481 apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
482 apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
483 apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
484 apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
485 apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
f990fff4
KW
486#ifdef CONFIG_X86_MCE_INTEL
487 if (maxlvt >= 5)
488 apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
489#endif