x86_64: remove now unused code
[linux-2.6-block.git] / arch / x86 / kernel / apic_64.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>
1da177e4
LT
22#include <linux/interrupt.h>
23#include <linux/mc146818rtc.h>
24#include <linux/kernel_stat.h>
25#include <linux/sysdev.h>
d25bf7e5 26#include <linux/module.h>
39928722 27#include <linux/ioport.h>
ba7eda4c 28#include <linux/clockchips.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
LT
42
43int apic_verbosity;
0c3749c4 44int apic_calibrate_pmtmr __initdata;
1da177e4 45
fb79d22e 46int disable_apic_timer __cpuinitdata;
1da177e4 47
2e7c2838
LT
48/* Local APIC timer works in C2? */
49int local_apic_timer_c2_ok;
50EXPORT_SYMBOL_GPL(local_apic_timer_c2_ok);
51
39928722
AD
52static struct resource *ioapic_resources;
53static struct resource lapic_resource = {
54 .name = "Local APIC",
55 .flags = IORESOURCE_MEM | IORESOURCE_BUSY,
56};
57
d03030e9
TG
58static unsigned int calibration_result;
59
ba7eda4c
TG
60static int lapic_next_event(unsigned long delta,
61 struct clock_event_device *evt);
62static void lapic_timer_setup(enum clock_event_mode mode,
63 struct clock_event_device *evt);
64
65static void lapic_timer_broadcast(cpumask_t mask);
66
67static void __setup_APIC_LVTT(unsigned int clocks, int oneshot, int irqen);
68
69static struct clock_event_device lapic_clockevent = {
70 .name = "lapic",
71 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT
72 | CLOCK_EVT_FEAT_C3STOP | CLOCK_EVT_FEAT_DUMMY,
73 .shift = 32,
74 .set_mode = lapic_timer_setup,
75 .set_next_event = lapic_next_event,
76 .broadcast = lapic_timer_broadcast,
77 .rating = 100,
78 .irq = -1,
79};
80static DEFINE_PER_CPU(struct clock_event_device, lapic_events);
81
82static int lapic_next_event(unsigned long delta,
83 struct clock_event_device *evt)
84{
85 apic_write(APIC_TMICT, delta);
86 return 0;
87}
88
89static void lapic_timer_setup(enum clock_event_mode mode,
90 struct clock_event_device *evt)
91{
92 unsigned long flags;
93 unsigned int v;
94
95 /* Lapic used as dummy for broadcast ? */
96 if (evt->features & CLOCK_EVT_FEAT_DUMMY)
97 return;
98
99 local_irq_save(flags);
100
101 switch (mode) {
102 case CLOCK_EVT_MODE_PERIODIC:
103 case CLOCK_EVT_MODE_ONESHOT:
104 __setup_APIC_LVTT(calibration_result,
105 mode != CLOCK_EVT_MODE_PERIODIC, 1);
106 break;
107 case CLOCK_EVT_MODE_UNUSED:
108 case CLOCK_EVT_MODE_SHUTDOWN:
109 v = apic_read(APIC_LVTT);
110 v |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
111 apic_write(APIC_LVTT, v);
112 break;
113 case CLOCK_EVT_MODE_RESUME:
114 /* Nothing to do here */
115 break;
116 }
117
118 local_irq_restore(flags);
119}
120
121/*
122 * Local APIC timer broadcast function
123 */
124static void lapic_timer_broadcast(cpumask_t mask)
125{
126#ifdef CONFIG_SMP
127 send_IPI_mask(mask, LOCAL_TIMER_VECTOR);
128#endif
129}
130
1da177e4
LT
131static void apic_pm_activate(void);
132
8339e9fb
FLV
133void apic_wait_icr_idle(void)
134{
135 while (apic_read(APIC_ICR) & APIC_ICR_BUSY)
136 cpu_relax();
137}
138
139unsigned int safe_apic_wait_icr_idle(void)
140{
141 unsigned int send_status;
142 int timeout;
143
144 timeout = 0;
145 do {
146 send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
147 if (!send_status)
148 break;
149 udelay(100);
150 } while (timeout++ < 1000);
151
152 return send_status;
153}
154
1da177e4
LT
155void enable_NMI_through_LVT0 (void * dummy)
156{
11a8e778 157 unsigned int v;
6935d1f9
TG
158
159 /* unmask and set to NMI */
160 v = APIC_DM_NMI;
11a8e778 161 apic_write(APIC_LVT0, v);
1da177e4
LT
162}
163
164int get_maxlvt(void)
165{
11a8e778 166 unsigned int v, maxlvt;
1da177e4
LT
167
168 v = apic_read(APIC_LVR);
1da177e4
LT
169 maxlvt = GET_APIC_MAXLVT(v);
170 return maxlvt;
171}
172
3777a959
AK
173/*
174 * 'what should we do if we get a hw irq event on an illegal vector'.
175 * each architecture has to answer this themselves.
176 */
177void ack_bad_irq(unsigned int irq)
178{
179 printk("unexpected IRQ trap at vector %02x\n", irq);
180 /*
181 * Currently unexpected vectors happen only on SMP and APIC.
182 * We _must_ ack these because every local APIC has only N
183 * irq slots per priority level, and a 'hanging, unacked' IRQ
184 * holds up an irq slot - in excessive cases (when multiple
185 * unexpected vectors occur) that might lock up the APIC
186 * completely.
6935d1f9 187 * But don't ack when the APIC is disabled. -AK
3777a959
AK
188 */
189 if (!disable_apic)
190 ack_APIC_irq();
191}
192
1da177e4
LT
193void clear_local_APIC(void)
194{
195 int maxlvt;
196 unsigned int v;
197
198 maxlvt = get_maxlvt();
199
200 /*
704fc59e 201 * Masking an LVT entry can trigger a local APIC error
1da177e4
LT
202 * if the vector is zero. Mask LVTERR first to prevent this.
203 */
204 if (maxlvt >= 3) {
205 v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
11a8e778 206 apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
1da177e4
LT
207 }
208 /*
209 * Careful: we have to set masks only first to deassert
210 * any level-triggered sources.
211 */
212 v = apic_read(APIC_LVTT);
11a8e778 213 apic_write(APIC_LVTT, v | APIC_LVT_MASKED);
1da177e4 214 v = apic_read(APIC_LVT0);
11a8e778 215 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
1da177e4 216 v = apic_read(APIC_LVT1);
11a8e778 217 apic_write(APIC_LVT1, v | APIC_LVT_MASKED);
1da177e4
LT
218 if (maxlvt >= 4) {
219 v = apic_read(APIC_LVTPC);
11a8e778 220 apic_write(APIC_LVTPC, v | APIC_LVT_MASKED);
1da177e4
LT
221 }
222
223 /*
224 * Clean APIC state for other OSs:
225 */
11a8e778
AK
226 apic_write(APIC_LVTT, APIC_LVT_MASKED);
227 apic_write(APIC_LVT0, APIC_LVT_MASKED);
228 apic_write(APIC_LVT1, APIC_LVT_MASKED);
1da177e4 229 if (maxlvt >= 3)
11a8e778 230 apic_write(APIC_LVTERR, APIC_LVT_MASKED);
1da177e4 231 if (maxlvt >= 4)
11a8e778 232 apic_write(APIC_LVTPC, APIC_LVT_MASKED);
5a40b7c2
AK
233 apic_write(APIC_ESR, 0);
234 apic_read(APIC_ESR);
1da177e4
LT
235}
236
208fb931 237void disconnect_bsp_APIC(int virt_wire_setup)
1da177e4 238{
a8fcf1a2
AK
239 /* Go back to Virtual Wire compatibility mode */
240 unsigned long value;
208fb931 241
a8fcf1a2
AK
242 /* For the spurious interrupt use vector F, and enable it */
243 value = apic_read(APIC_SPIV);
244 value &= ~APIC_VECTOR_MASK;
245 value |= APIC_SPIV_APIC_ENABLED;
246 value |= 0xf;
247 apic_write(APIC_SPIV, value);
248
249 if (!virt_wire_setup) {
250 /* For LVT0 make it edge triggered, active high, external and enabled */
251 value = apic_read(APIC_LVT0);
252 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
208fb931 253 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
a8fcf1a2 254 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED );
208fb931 255 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
a8fcf1a2
AK
256 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
257 apic_write(APIC_LVT0, value);
258 } else {
259 /* Disable LVT0 */
260 apic_write(APIC_LVT0, APIC_LVT_MASKED);
208fb931 261 }
a8fcf1a2
AK
262
263 /* For LVT1 make it edge triggered, active high, nmi and enabled */
264 value = apic_read(APIC_LVT1);
265 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
266 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
267 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
268 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
269 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
270 apic_write(APIC_LVT1, value);
1da177e4
LT
271}
272
273void disable_local_APIC(void)
274{
275 unsigned int value;
276
277 clear_local_APIC();
278
279 /*
280 * Disable APIC (implies clearing of registers
281 * for 82489DX!).
282 */
283 value = apic_read(APIC_SPIV);
284 value &= ~APIC_SPIV_APIC_ENABLED;
11a8e778 285 apic_write(APIC_SPIV, value);
1da177e4
LT
286}
287
288/*
289 * This is to verify that we're looking at a real local APIC.
290 * Check these against your board if the CPUs aren't getting
291 * started for no apparent reason.
292 */
293int __init verify_local_APIC(void)
294{
295 unsigned int reg0, reg1;
296
297 /*
298 * The version register is read-only in a real APIC.
299 */
300 reg0 = apic_read(APIC_LVR);
301 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
302 apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
303 reg1 = apic_read(APIC_LVR);
304 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
305
306 /*
307 * The two version reads above should print the same
308 * numbers. If the second one is different, then we
309 * poke at a non-APIC.
310 */
311 if (reg1 != reg0)
312 return 0;
313
314 /*
315 * Check if the version looks reasonably.
316 */
317 reg1 = GET_APIC_VERSION(reg0);
318 if (reg1 == 0x00 || reg1 == 0xff)
319 return 0;
320 reg1 = get_maxlvt();
321 if (reg1 < 0x02 || reg1 == 0xff)
322 return 0;
323
324 /*
325 * The ID register is read/write in a real APIC.
326 */
327 reg0 = apic_read(APIC_ID);
328 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
329 apic_write(APIC_ID, reg0 ^ APIC_ID_MASK);
330 reg1 = apic_read(APIC_ID);
331 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg1);
332 apic_write(APIC_ID, reg0);
333 if (reg1 != (reg0 ^ APIC_ID_MASK))
334 return 0;
335
336 /*
337 * The next two are just to see if we have sane values.
338 * They're only really relevant if we're in Virtual Wire
339 * compatibility mode, but most boxes are anymore.
340 */
341 reg0 = apic_read(APIC_LVT0);
342 apic_printk(APIC_DEBUG,"Getting LVT0: %x\n", reg0);
343 reg1 = apic_read(APIC_LVT1);
344 apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
345
346 return 1;
347}
348
349void __init sync_Arb_IDs(void)
350{
351 /* Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 */
352 unsigned int ver = GET_APIC_VERSION(apic_read(APIC_LVR));
353 if (ver >= 0x14) /* P4 or higher */
354 return;
355
356 /*
357 * Wait for idle.
358 */
359 apic_wait_icr_idle();
360
361 apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
11a8e778 362 apic_write(APIC_ICR, APIC_DEST_ALLINC | APIC_INT_LEVELTRIG
1da177e4
LT
363 | APIC_DM_INIT);
364}
365
1da177e4
LT
366/*
367 * An initial setup of the virtual wire mode.
368 */
369void __init init_bsp_APIC(void)
370{
11a8e778 371 unsigned int value;
1da177e4
LT
372
373 /*
374 * Don't do the setup now if we have a SMP BIOS as the
375 * through-I/O-APIC virtual wire mode might be active.
376 */
377 if (smp_found_config || !cpu_has_apic)
378 return;
379
380 value = apic_read(APIC_LVR);
1da177e4
LT
381
382 /*
383 * Do not trust the local APIC being empty at bootup.
384 */
385 clear_local_APIC();
386
387 /*
388 * Enable APIC.
389 */
390 value = apic_read(APIC_SPIV);
391 value &= ~APIC_VECTOR_MASK;
392 value |= APIC_SPIV_APIC_ENABLED;
393 value |= APIC_SPIV_FOCUS_DISABLED;
394 value |= SPURIOUS_APIC_VECTOR;
11a8e778 395 apic_write(APIC_SPIV, value);
1da177e4
LT
396
397 /*
398 * Set up the virtual wire mode.
399 */
11a8e778 400 apic_write(APIC_LVT0, APIC_DM_EXTINT);
1da177e4 401 value = APIC_DM_NMI;
11a8e778 402 apic_write(APIC_LVT1, value);
1da177e4
LT
403}
404
e6982c67 405void __cpuinit setup_local_APIC (void)
1da177e4 406{
11a8e778 407 unsigned int value, maxlvt;
da7ed9f9 408 int i, j;
1da177e4 409
1da177e4 410 value = apic_read(APIC_LVR);
1da177e4 411
fe7414a2 412 BUILD_BUG_ON((SPURIOUS_APIC_VECTOR & 0x0f) != 0x0f);
1da177e4
LT
413
414 /*
415 * Double-check whether this APIC is really registered.
416 * This is meaningless in clustered apic mode, so we skip it.
417 */
418 if (!apic_id_registered())
419 BUG();
420
421 /*
422 * Intel recommends to set DFR, LDR and TPR before enabling
423 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
424 * document number 292116). So here it goes...
425 */
426 init_apic_ldr();
427
428 /*
429 * Set Task Priority to 'accept all'. We never change this
430 * later on.
431 */
432 value = apic_read(APIC_TASKPRI);
433 value &= ~APIC_TPRI_MASK;
11a8e778 434 apic_write(APIC_TASKPRI, value);
1da177e4 435
da7ed9f9
VG
436 /*
437 * After a crash, we no longer service the interrupts and a pending
438 * interrupt from previous kernel might still have ISR bit set.
439 *
440 * Most probably by now CPU has serviced that pending interrupt and
441 * it might not have done the ack_APIC_irq() because it thought,
442 * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
443 * does not clear the ISR bit and cpu thinks it has already serivced
444 * the interrupt. Hence a vector might get locked. It was noticed
445 * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
446 */
447 for (i = APIC_ISR_NR - 1; i >= 0; i--) {
448 value = apic_read(APIC_ISR + i*0x10);
449 for (j = 31; j >= 0; j--) {
450 if (value & (1<<j))
451 ack_APIC_irq();
452 }
453 }
454
1da177e4
LT
455 /*
456 * Now that we are all set up, enable the APIC
457 */
458 value = apic_read(APIC_SPIV);
459 value &= ~APIC_VECTOR_MASK;
460 /*
461 * Enable APIC
462 */
463 value |= APIC_SPIV_APIC_ENABLED;
464
3f14c746
AK
465 /* We always use processor focus */
466
1da177e4
LT
467 /*
468 * Set spurious IRQ vector
469 */
470 value |= SPURIOUS_APIC_VECTOR;
11a8e778 471 apic_write(APIC_SPIV, value);
1da177e4
LT
472
473 /*
474 * Set up LVT0, LVT1:
475 *
476 * set up through-local-APIC on the BP's LINT0. This is not
477 * strictly necessary in pure symmetric-IO mode, but sometimes
478 * we delegate interrupts to the 8259A.
479 */
480 /*
481 * TODO: set up through-local-APIC from through-I/O-APIC? --macro
482 */
483 value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
a8fcf1a2 484 if (!smp_processor_id() && !value) {
1da177e4
LT
485 value = APIC_DM_EXTINT;
486 apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n", smp_processor_id());
487 } else {
488 value = APIC_DM_EXTINT | APIC_LVT_MASKED;
489 apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n", smp_processor_id());
490 }
11a8e778 491 apic_write(APIC_LVT0, value);
1da177e4
LT
492
493 /*
494 * only the BP should see the LINT1 NMI signal, obviously.
495 */
496 if (!smp_processor_id())
497 value = APIC_DM_NMI;
498 else
499 value = APIC_DM_NMI | APIC_LVT_MASKED;
11a8e778 500 apic_write(APIC_LVT1, value);
1da177e4 501
61c11341 502 {
1da177e4
LT
503 unsigned oldvalue;
504 maxlvt = get_maxlvt();
1da177e4
LT
505 oldvalue = apic_read(APIC_ESR);
506 value = ERROR_APIC_VECTOR; // enables sending errors
11a8e778 507 apic_write(APIC_LVTERR, value);
1da177e4
LT
508 /*
509 * spec says clear errors after enabling vector.
510 */
511 if (maxlvt > 3)
512 apic_write(APIC_ESR, 0);
513 value = apic_read(APIC_ESR);
514 if (value != oldvalue)
515 apic_printk(APIC_VERBOSE,
516 "ESR value after enabling vector: %08x, after %08x\n",
517 oldvalue, value);
1da177e4
LT
518 }
519
520 nmi_watchdog_default();
f2802e7f 521 setup_apic_nmi_watchdog(NULL);
1da177e4
LT
522 apic_pm_activate();
523}
524
525#ifdef CONFIG_PM
526
527static struct {
528 /* 'active' is true if the local APIC was enabled by us and
529 not the BIOS; this signifies that we are also responsible
530 for disabling it before entering apm/acpi suspend */
531 int active;
532 /* r/w apic fields */
533 unsigned int apic_id;
534 unsigned int apic_taskpri;
535 unsigned int apic_ldr;
536 unsigned int apic_dfr;
537 unsigned int apic_spiv;
538 unsigned int apic_lvtt;
539 unsigned int apic_lvtpc;
540 unsigned int apic_lvt0;
541 unsigned int apic_lvt1;
542 unsigned int apic_lvterr;
543 unsigned int apic_tmict;
544 unsigned int apic_tdcr;
545 unsigned int apic_thmr;
546} apic_pm_state;
547
0b9c33a7 548static int lapic_suspend(struct sys_device *dev, pm_message_t state)
1da177e4
LT
549{
550 unsigned long flags;
f990fff4 551 int maxlvt;
1da177e4
LT
552
553 if (!apic_pm_state.active)
554 return 0;
555
f990fff4
KW
556 maxlvt = get_maxlvt();
557
1da177e4
LT
558 apic_pm_state.apic_id = apic_read(APIC_ID);
559 apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
560 apic_pm_state.apic_ldr = apic_read(APIC_LDR);
561 apic_pm_state.apic_dfr = apic_read(APIC_DFR);
562 apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
563 apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
f990fff4
KW
564 if (maxlvt >= 4)
565 apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
1da177e4
LT
566 apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
567 apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
568 apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
569 apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
570 apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
f990fff4
KW
571#ifdef CONFIG_X86_MCE_INTEL
572 if (maxlvt >= 5)
573 apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
574#endif