blob: 2d8c6ce1ecda6439a1ff8eaf057f26e7e4b78101 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
17#include <linux/config.h>
18#include <linux/init.h>
19
20#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/delay.h>
22#include <linux/bootmem.h>
23#include <linux/smp_lock.h>
24#include <linux/interrupt.h>
25#include <linux/mc146818rtc.h>
26#include <linux/kernel_stat.h>
27#include <linux/sysdev.h>
Zwane Mwaikambof3705132005-06-25 14:54:50 -070028#include <linux/cpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30#include <asm/atomic.h>
31#include <asm/smp.h>
32#include <asm/mtrr.h>
33#include <asm/mpspec.h>
34#include <asm/desc.h>
35#include <asm/arch_hooks.h>
36#include <asm/hpet.h>
Ingo Molnar306e4402005-06-30 02:58:55 -070037#include <asm/i8253.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39#include <mach_apic.h>
40
41#include "io_ports.h"
42
43/*
Eric W. Biederman9635b472005-06-25 14:57:41 -070044 * Knob to control our willingness to enable the local APIC.
45 */
46int enable_local_apic __initdata = 0; /* -1=force-disable, +1=force-enable */
47
48/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 * Debug level
50 */
51int apic_verbosity;
52
53
54static void apic_pm_activate(void);
55
56/*
57 * 'what should we do if we get a hw irq event on an illegal vector'.
58 * each architecture has to answer this themselves.
59 */
60void ack_bad_irq(unsigned int irq)
61{
62 printk("unexpected IRQ trap at vector %02x\n", irq);
63 /*
64 * Currently unexpected vectors happen only on SMP and APIC.
65 * We _must_ ack these because every local APIC has only N
66 * irq slots per priority level, and a 'hanging, unacked' IRQ
67 * holds up an irq slot - in excessive cases (when multiple
68 * unexpected vectors occur) that might lock up the APIC
69 * completely.
70 */
71 ack_APIC_irq();
72}
73
74void __init apic_intr_init(void)
75{
76#ifdef CONFIG_SMP
77 smp_intr_init();
78#endif
79 /* self generated IPI for local APIC timer */
80 set_intr_gate(LOCAL_TIMER_VECTOR, apic_timer_interrupt);
81
82 /* IPI vectors for APIC spurious and error interrupts */
83 set_intr_gate(SPURIOUS_APIC_VECTOR, spurious_interrupt);
84 set_intr_gate(ERROR_APIC_VECTOR, error_interrupt);
85
86 /* thermal monitor LVT interrupt */
87#ifdef CONFIG_X86_MCE_P4THERMAL
88 set_intr_gate(THERMAL_APIC_VECTOR, thermal_interrupt);
89#endif
90}
91
92/* Using APIC to generate smp_local_timer_interrupt? */
93int using_apic_timer = 0;
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095static int enabled_via_apicbase;
96
97void enable_NMI_through_LVT0 (void * dummy)
98{
99 unsigned int v, ver;
100
101 ver = apic_read(APIC_LVR);
102 ver = GET_APIC_VERSION(ver);
103 v = APIC_DM_NMI; /* unmask and set to NMI */
104 if (!APIC_INTEGRATED(ver)) /* 82489DX */
105 v |= APIC_LVT_LEVEL_TRIGGER;
106 apic_write_around(APIC_LVT0, v);
107}
108
109int get_physical_broadcast(void)
110{
111 unsigned int lvr, version;
112 lvr = apic_read(APIC_LVR);
113 version = GET_APIC_VERSION(lvr);
114 if (!APIC_INTEGRATED(version) || version >= 0x14)
115 return 0xff;
116 else
117 return 0xf;
118}
119
120int get_maxlvt(void)
121{
122 unsigned int v, ver, maxlvt;
123
124 v = apic_read(APIC_LVR);
125 ver = GET_APIC_VERSION(v);
126 /* 82489DXs do not report # of LVT entries. */
127 maxlvt = APIC_INTEGRATED(ver) ? GET_APIC_MAXLVT(v) : 2;
128 return maxlvt;
129}
130
131void clear_local_APIC(void)
132{
133 int maxlvt;
134 unsigned long v;
135
136 maxlvt = get_maxlvt();
137
138 /*
139 * Masking an LVT entry on a P6 can trigger a local APIC error
140 * if the vector is zero. Mask LVTERR first to prevent this.
141 */
142 if (maxlvt >= 3) {
143 v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
144 apic_write_around(APIC_LVTERR, v | APIC_LVT_MASKED);
145 }
146 /*
147 * Careful: we have to set masks only first to deassert
148 * any level-triggered sources.
149 */
150 v = apic_read(APIC_LVTT);
151 apic_write_around(APIC_LVTT, v | APIC_LVT_MASKED);
152 v = apic_read(APIC_LVT0);
153 apic_write_around(APIC_LVT0, v | APIC_LVT_MASKED);
154 v = apic_read(APIC_LVT1);
155 apic_write_around(APIC_LVT1, v | APIC_LVT_MASKED);
156 if (maxlvt >= 4) {
157 v = apic_read(APIC_LVTPC);
158 apic_write_around(APIC_LVTPC, v | APIC_LVT_MASKED);
159 }
160
161/* lets not touch this if we didn't frob it */
162#ifdef CONFIG_X86_MCE_P4THERMAL
163 if (maxlvt >= 5) {
164 v = apic_read(APIC_LVTTHMR);
165 apic_write_around(APIC_LVTTHMR, v | APIC_LVT_MASKED);
166 }
167#endif
168 /*
169 * Clean APIC state for other OSs:
170 */
171 apic_write_around(APIC_LVTT, APIC_LVT_MASKED);
172 apic_write_around(APIC_LVT0, APIC_LVT_MASKED);
173 apic_write_around(APIC_LVT1, APIC_LVT_MASKED);
174 if (maxlvt >= 3)
175 apic_write_around(APIC_LVTERR, APIC_LVT_MASKED);
176 if (maxlvt >= 4)
177 apic_write_around(APIC_LVTPC, APIC_LVT_MASKED);
178
179#ifdef CONFIG_X86_MCE_P4THERMAL
180 if (maxlvt >= 5)
181 apic_write_around(APIC_LVTTHMR, APIC_LVT_MASKED);
182#endif
183 v = GET_APIC_VERSION(apic_read(APIC_LVR));
184 if (APIC_INTEGRATED(v)) { /* !82489DX */
185 if (maxlvt > 3) /* Due to Pentium errata 3AP and 11AP. */
186 apic_write(APIC_ESR, 0);
187 apic_read(APIC_ESR);
188 }
189}
190
191void __init connect_bsp_APIC(void)
192{
193 if (pic_mode) {
194 /*
195 * Do not trust the local APIC being empty at bootup.
196 */
197 clear_local_APIC();
198 /*
199 * PIC mode, enable APIC mode in the IMCR, i.e.
200 * connect BSP's local APIC to INT and NMI lines.
201 */
202 apic_printk(APIC_VERBOSE, "leaving PIC mode, "
203 "enabling APIC mode.\n");
204 outb(0x70, 0x22);
205 outb(0x01, 0x23);
206 }
207 enable_apic_mode();
208}
209
Eric W. Biederman650927e2005-06-25 14:57:44 -0700210void disconnect_bsp_APIC(int virt_wire_setup)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
212 if (pic_mode) {
213 /*
214 * Put the board back into PIC mode (has an effect
215 * only on certain older boards). Note that APIC
216 * interrupts, including IPIs, won't work beyond
217 * this point! The only exception are INIT IPIs.
218 */
219 apic_printk(APIC_VERBOSE, "disabling APIC mode, "
220 "entering PIC mode.\n");
221 outb(0x70, 0x22);
222 outb(0x00, 0x23);
223 }
Eric W. Biederman650927e2005-06-25 14:57:44 -0700224 else {
225 /* Go back to Virtual Wire compatibility mode */
226 unsigned long value;
227
228 /* For the spurious interrupt use vector F, and enable it */
229 value = apic_read(APIC_SPIV);
230 value &= ~APIC_VECTOR_MASK;
231 value |= APIC_SPIV_APIC_ENABLED;
232 value |= 0xf;
233 apic_write_around(APIC_SPIV, value);
234
235 if (!virt_wire_setup) {
236 /* For LVT0 make it edge triggered, active high, external and enabled */
237 value = apic_read(APIC_LVT0);
238 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
239 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
240 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED );
241 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
242 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
243 apic_write_around(APIC_LVT0, value);
244 }
245 else {
246 /* Disable LVT0 */
247 apic_write_around(APIC_LVT0, APIC_LVT_MASKED);
248 }
249
250 /* For LVT1 make it edge triggered, active high, nmi and enabled */
251 value = apic_read(APIC_LVT1);
252 value &= ~(
253 APIC_MODE_MASK | APIC_SEND_PENDING |
254 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
255 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
256 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
257 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
258 apic_write_around(APIC_LVT1, value);
259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260}
261
262void disable_local_APIC(void)
263{
264 unsigned long value;
265
266 clear_local_APIC();
267
268 /*
269 * Disable APIC (implies clearing of registers
270 * for 82489DX!).
271 */
272 value = apic_read(APIC_SPIV);
273 value &= ~APIC_SPIV_APIC_ENABLED;
274 apic_write_around(APIC_SPIV, value);
275
276 if (enabled_via_apicbase) {
277 unsigned int l, h;
278 rdmsr(MSR_IA32_APICBASE, l, h);
279 l &= ~MSR_IA32_APICBASE_ENABLE;
280 wrmsr(MSR_IA32_APICBASE, l, h);
281 }
282}
283
284/*
285 * This is to verify that we're looking at a real local APIC.
286 * Check these against your board if the CPUs aren't getting
287 * started for no apparent reason.
288 */
289int __init verify_local_APIC(void)
290{
291 unsigned int reg0, reg1;
292
293 /*
294 * The version register is read-only in a real APIC.
295 */
296 reg0 = apic_read(APIC_LVR);
297 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
298 apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
299 reg1 = apic_read(APIC_LVR);
300 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
301
302 /*
303 * The two version reads above should print the same
304 * numbers. If the second one is different, then we
305 * poke at a non-APIC.
306 */
307 if (reg1 != reg0)
308 return 0;
309
310 /*
311 * Check if the version looks reasonably.
312 */
313 reg1 = GET_APIC_VERSION(reg0);
314 if (reg1 == 0x00 || reg1 == 0xff)
315 return 0;
316 reg1 = get_maxlvt();
317 if (reg1 < 0x02 || reg1 == 0xff)
318 return 0;
319
320 /*
321 * The ID register is read/write in a real APIC.
322 */
323 reg0 = apic_read(APIC_ID);
324 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
325
326 /*
327 * The next two are just to see if we have sane values.
328 * They're only really relevant if we're in Virtual Wire
329 * compatibility mode, but most boxes are anymore.
330 */
331 reg0 = apic_read(APIC_LVT0);
332 apic_printk(APIC_DEBUG, "Getting LVT0: %x\n", reg0);
333 reg1 = apic_read(APIC_LVT1);
334 apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
335
336 return 1;
337}
338
339void __init sync_Arb_IDs(void)
340{
341 /* Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 */
342 unsigned int ver = GET_APIC_VERSION(apic_read(APIC_LVR));
343 if (ver >= 0x14) /* P4 or higher */
344 return;
345 /*
346 * Wait for idle.
347 */
348 apic_wait_icr_idle();
349
350 apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
351 apic_write_around(APIC_ICR, APIC_DEST_ALLINC | APIC_INT_LEVELTRIG
352 | APIC_DM_INIT);
353}
354
355extern void __error_in_apic_c (void);
356
357/*
358 * An initial setup of the virtual wire mode.
359 */
360void __init init_bsp_APIC(void)
361{
362 unsigned long value, ver;
363
364 /*
365 * Don't do the setup now if we have a SMP BIOS as the
366 * through-I/O-APIC virtual wire mode might be active.
367 */
368 if (smp_found_config || !cpu_has_apic)
369 return;
370
371 value = apic_read(APIC_LVR);
372 ver = GET_APIC_VERSION(value);
373
374 /*
375 * Do not trust the local APIC being empty at bootup.
376 */
377 clear_local_APIC();
378
379 /*
380 * Enable APIC.
381 */
382 value = apic_read(APIC_SPIV);
383 value &= ~APIC_VECTOR_MASK;
384 value |= APIC_SPIV_APIC_ENABLED;
385
386 /* This bit is reserved on P4/Xeon and should be cleared */
387 if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) && (boot_cpu_data.x86 == 15))
388 value &= ~APIC_SPIV_FOCUS_DISABLED;
389 else
390 value |= APIC_SPIV_FOCUS_DISABLED;
391 value |= SPURIOUS_APIC_VECTOR;
392 apic_write_around(APIC_SPIV, value);
393
394 /*
395 * Set up the virtual wire mode.
396 */
397 apic_write_around(APIC_LVT0, APIC_DM_EXTINT);
398 value = APIC_DM_NMI;
399 if (!APIC_INTEGRATED(ver)) /* 82489DX */
400 value |= APIC_LVT_LEVEL_TRIGGER;
401 apic_write_around(APIC_LVT1, value);
402}
403
Li Shaohua0bb31842005-06-25 14:54:55 -0700404void __devinit setup_local_APIC(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
406 unsigned long oldvalue, value, ver, maxlvt;
407
408 /* Pound the ESR really hard over the head with a big hammer - mbligh */
409 if (esr_disable) {
410 apic_write(APIC_ESR, 0);
411 apic_write(APIC_ESR, 0);
412 apic_write(APIC_ESR, 0);
413 apic_write(APIC_ESR, 0);
414 }
415
416 value = apic_read(APIC_LVR);
417 ver = GET_APIC_VERSION(value);
418
419 if ((SPURIOUS_APIC_VECTOR & 0x0f) != 0x0f)
420 __error_in_apic_c();
421
422 /*
423 * Double-check whether this APIC is really registered.
424 */
425 if (!apic_id_registered())
426 BUG();
427
428 /*
429 * Intel recommends to set DFR, LDR and TPR before enabling
430 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
431 * document number 292116). So here it goes...
432 */
433 init_apic_ldr();
434
435 /*
436 * Set Task Priority to 'accept all'. We never change this
437 * later on.
438 */
439 value = apic_read(APIC_TASKPRI);
440 value &= ~APIC_TPRI_MASK;
441 apic_write_around(APIC_TASKPRI, value);
442
443 /*
444 * Now that we are all set up, enable the APIC
445 */
446 value = apic_read(APIC_SPIV);
447 value &= ~APIC_VECTOR_MASK;
448 /*
449 * Enable APIC
450 */
451 value |= APIC_SPIV_APIC_ENABLED;
452
453 /*
454 * Some unknown Intel IO/APIC (or APIC) errata is biting us with
455 * certain networking cards. If high frequency interrupts are
456 * happening on a particular IOAPIC pin, plus the IOAPIC routing
457 * entry is masked/unmasked at a high rate as well then sooner or
458 * later IOAPIC line gets 'stuck', no more interrupts are received
459 * from the device. If focus CPU is disabled then the hang goes
460 * away, oh well :-(
461 *
462 * [ This bug can be reproduced easily with a level-triggered
463 * PCI Ne2000 networking cards and PII/PIII processors, dual
464 * BX chipset. ]
465 */
466 /*
467 * Actually disabling the focus CPU check just makes the hang less
468 * frequent as it makes the interrupt distributon model be more
469 * like LRU than MRU (the short-term load is more even across CPUs).
470 * See also the comment in end_level_ioapic_irq(). --macro
471 */
472#if 1
473 /* Enable focus processor (bit==0) */
474 value &= ~APIC_SPIV_FOCUS_DISABLED;
475#else
476 /* Disable focus processor (bit==1) */
477 value |= APIC_SPIV_FOCUS_DISABLED;
478#endif
479 /*
480 * Set spurious IRQ vector
481 */
482 value |= SPURIOUS_APIC_VECTOR;
483 apic_write_around(APIC_SPIV, value);
484
485 /*
486 * Set up LVT0, LVT1:
487 *
488 * set up through-local-APIC on the BP's LINT0. This is not
489 * strictly necessery in pure symmetric-IO mode, but sometimes
490 * we delegate interrupts to the 8259A.
491 */
492 /*
493 * TODO: set up through-local-APIC from through-I/O-APIC? --macro
494 */
495 value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
496 if (!smp_processor_id() && (pic_mode || !value)) {
497 value = APIC_DM_EXTINT;
498 apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n",
499 smp_processor_id());
500 } else {
501 value = APIC_DM_EXTINT | APIC_LVT_MASKED;
502 apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n",
503 smp_processor_id());
504 }
505 apic_write_around(APIC_LVT0, value);
506
507 /*
508 * only the BP should see the LINT1 NMI signal, obviously.
509 */
510 if (!smp_processor_id())
511 value = APIC_DM_NMI;
512 else
513 value = APIC_DM_NMI | APIC_LVT_MASKED;
514 if (!APIC_INTEGRATED(ver)) /* 82489DX */
515 value |= APIC_LVT_LEVEL_TRIGGER;
516 apic_write_around(APIC_LVT1, value);
517
518 if (APIC_INTEGRATED(ver) && !esr_disable) { /* !82489DX */
519 maxlvt = get_maxlvt();
520 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
521 apic_write(APIC_ESR, 0);
522 oldvalue = apic_read(APIC_ESR);
523
524 value = ERROR_APIC_VECTOR; // enables sending errors
525 apic_write_around(APIC_LVTERR, value);
526 /*
527 * spec says clear errors after enabling vector.
528 */
529 if (maxlvt > 3)
530 apic_write(APIC_ESR, 0);
531 value = apic_read(APIC_ESR);
532 if (value != oldvalue)
533 apic_printk(APIC_VERBOSE, "ESR value before enabling "
534 "vector: 0x%08lx after: 0x%08lx\n",
535 oldvalue, value);
536 } else {
537 if (esr_disable)
538 /*
539 * Something untraceble is creating bad interrupts on
540 * secondary quads ... for the moment, just leave the
541 * ESR disabled - we can't do anything useful with the
542 * errors anyway - mbligh
543 */
544 printk("Leaving ESR disabled.\n");
545 else
546 printk("No ESR for 82489DX.\n");
547 }
548
549 if (nmi_watchdog == NMI_LOCAL_APIC)
550 setup_apic_nmi_watchdog();
551 apic_pm_activate();
552}
553
554/*
555 * If Linux enabled the LAPIC against the BIOS default
556 * disable it down before re-entering the BIOS on shutdown.
557 * Otherwise the BIOS may get confused and not power-off.
Zwane Mwaikambo77f72b12005-11-07 00:58:33 -0800558 * Additionally clear all LVT entries before disable_local_APIC
559 * for the case where Linux didn't enable the LAPIC.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 */
561void lapic_shutdown(void)
562{
Zwane Mwaikambo77f72b12005-11-07 00:58:33 -0800563 if (!cpu_has_apic)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 return;
565
566 local_irq_disable();
Zwane Mwaikambo77f72b12005-11-07 00:58:33 -0800567 clear_local_APIC();
568
569 if (enabled_via_apicbase)
570 disable_local_APIC();
571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 local_irq_enable();
573}
574
575#ifdef CONFIG_PM
576
577static struct {
578 int active;
579 /* r/w apic fields */
580 unsigned int apic_id;
581 unsigned int apic_taskpri;
582 unsigned int apic_ldr;
583 unsigned int apic_dfr;
584 unsigned int apic_spiv;
585 unsigned int apic_lvtt;
586 unsigned int apic_lvtpc;
587 unsigned int apic_lvt0;
588 unsigned int apic_lvt1;
589 unsigned int apic_lvterr;
590 unsigned int apic_tmict;
591 unsigned int apic_tdcr;
592 unsigned int apic_thmr;
593} apic_pm_state;
594
Pavel Machek438510f2005-04-16 15:25:24 -0700595static int lapic_suspend(struct sys_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
597 unsigned long flags;
598
599 if (!apic_pm_state.active)
600 return 0;
601
602 apic_pm_state.apic_id = apic_read(APIC_ID);
603 apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
604 apic_pm_state.apic_ldr = apic_read(APIC_LDR);
605 apic_pm_state.apic_dfr = apic_read(APIC_DFR);
606 apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
607 apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
608 apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
609 apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
610 apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
611 apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
612 apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
613 apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
614 apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
615
616 local_irq_save(flags);
617 disable_local_APIC();
618 local_irq_restore(flags);
619 return 0;
620}
621
622static int lapic_resume(struct sys_device *dev)
623{
624 unsigned int l, h;
625 unsigned long flags;
626
627 if (!apic_pm_state.active)
628 return 0;
629
630 local_irq_save(flags);
631
632 /*
633 * Make sure the APICBASE points to the right address
634 *
635 * FIXME! This will be wrong if we ever support suspend on
636 * SMP! We'll need to do this as part of the CPU restore!
637 */
638 rdmsr(MSR_IA32_APICBASE, l, h);
639 l &= ~MSR_IA32_APICBASE_BASE;
640 l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
641 wrmsr(MSR_IA32_APICBASE, l, h);
642
643 apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
644 apic_write(APIC_ID, apic_pm_state.apic_id);
645 apic_write(APIC_DFR, apic_pm_state.apic_dfr);
646 apic_write(APIC_LDR, apic_pm_state.apic_ldr);
647 apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
648 apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
649 apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
650 apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
651 apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
652 apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
653 apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
654 apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
655 apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
656 apic_write(APIC_ESR, 0);
657 apic_read(APIC_ESR);
658 apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
659 apic_write(APIC_ESR, 0);
660 apic_read(APIC_ESR);
661 local_irq_restore(flags);
662 return 0;
663}
664
665/*
666 * This device has no shutdown method - fully functioning local APICs
667 * are needed on every CPU up until machine_halt/restart/poweroff.
668 */
669
670static struct sysdev_class lapic_sysclass = {
671 set_kset_name("lapic"),
672 .resume = lapic_resume,
673 .suspend = lapic_suspend,
674};
675
676static struct sys_device device_lapic = {
677 .id = 0,
678 .cls = &lapic_sysclass,
679};
680
Li Shaohua0bb31842005-06-25 14:54:55 -0700681static void __devinit apic_pm_activate(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682{
683 apic_pm_state.active = 1;
684}
685
686static int __init init_lapic_sysfs(void)
687{
688 int error;
689
690 if (!cpu_has_apic)
691 return 0;
692 /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
693
694 error = sysdev_class_register(&lapic_sysclass);
695 if (!error)
696 error = sysdev_register(&device_lapic);
697 return error;
698}
699device_initcall(init_lapic_sysfs);
700
701#else /* CONFIG_PM */
702
703static void apic_pm_activate(void) { }
704
705#endif /* CONFIG_PM */
706
707/*
708 * Detect and enable local APICs on non-SMP boards.
709 * Original code written by Keir Fraser.
710 */
711
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712static int __init apic_set_verbosity(char *str)
713{
714 if (strcmp("debug", str) == 0)
715 apic_verbosity = APIC_DEBUG;
716 else if (strcmp("verbose", str) == 0)
717 apic_verbosity = APIC_VERBOSE;
718 else
719 printk(KERN_WARNING "APIC Verbosity level %s not recognised"
Dave Jonese31b88b2006-01-06 00:12:11 -0800720 " use apic=verbose or apic=debug\n", str);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
722 return 0;
723}
724
725__setup("apic=", apic_set_verbosity);
726
727static int __init detect_init_APIC (void)
728{
729 u32 h, l, features;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
731 /* Disabled by kernel option? */
732 if (enable_local_apic < 0)
733 return -1;
734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 switch (boot_cpu_data.x86_vendor) {
736 case X86_VENDOR_AMD:
737 if ((boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model > 1) ||
738 (boot_cpu_data.x86 == 15))
739 break;
740 goto no_apic;
741 case X86_VENDOR_INTEL:
742 if (boot_cpu_data.x86 == 6 || boot_cpu_data.x86 == 15 ||
743 (boot_cpu_data.x86 == 5 && cpu_has_apic))
744 break;
745 goto no_apic;
746 default:
747 goto no_apic;
748 }
749
750 if (!cpu_has_apic) {
751 /*
752 * Over-ride BIOS and try to enable the local
753 * APIC only if "lapic" specified.
754 */
755 if (enable_local_apic <= 0) {
756 printk("Local APIC disabled by BIOS -- "
757 "you can enable it with \"lapic\"\n");
758 return -1;
759 }
760 /*
761 * Some BIOSes disable the local APIC in the
762 * APIC_BASE MSR. This can only be done in
763 * software for Intel P6 or later and AMD K7
764 * (Model > 1) or later.
765 */
766 rdmsr(MSR_IA32_APICBASE, l, h);
767 if (!(l & MSR_IA32_APICBASE_ENABLE)) {
768 printk("Local APIC disabled by BIOS -- reenabling.\n");
769 l &= ~MSR_IA32_APICBASE_BASE;
770 l |= MSR_IA32_APICBASE_ENABLE | APIC_DEFAULT_PHYS_BASE;
771 wrmsr(MSR_IA32_APICBASE, l, h);
772 enabled_via_apicbase = 1;
773 }
774 }
775 /*
776 * The APIC feature bit should now be enabled
777 * in `cpuid'
778 */
779 features = cpuid_edx(1);
780 if (!(features & (1 << X86_FEATURE_APIC))) {
781 printk("Could not enable APIC!\n");
782 return -1;
783 }
784 set_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
785 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
786
787 /* The BIOS may have set up the APIC at some other address */
788 rdmsr(MSR_IA32_APICBASE, l, h);
789 if (l & MSR_IA32_APICBASE_ENABLE)
790 mp_lapic_addr = l & MSR_IA32_APICBASE_BASE;
791
792 if (nmi_watchdog != NMI_NONE)
793 nmi_watchdog = NMI_LOCAL_APIC;
794
795 printk("Found and enabled local APIC!\n");
796
797 apic_pm_activate();
798
799 return 0;
800
801no_apic:
802 printk("No local APIC present or hardware disabled\n");
803 return -1;
804}
805
806void __init init_apic_mappings(void)
807{
808 unsigned long apic_phys;
809
810 /*
811 * If no local APIC can be found then set up a fake all
812 * zeroes page to simulate the local APIC and another
813 * one for the IO-APIC.
814 */
815 if (!smp_found_config && detect_init_APIC()) {
816 apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
817 apic_phys = __pa(apic_phys);
818 } else
819 apic_phys = mp_lapic_addr;
820
821 set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
822 printk(KERN_DEBUG "mapped APIC to %08lx (%08lx)\n", APIC_BASE,
823 apic_phys);
824
825 /*
826 * Fetch the APIC ID of the BSP in case we have a
827 * default configuration (or the MP table is broken).
828 */
Linus Torvalds1e4c85f2005-10-31 19:16:17 -0800829 if (boot_cpu_physical_apicid == -1U)
830 boot_cpu_physical_apicid = GET_APIC_ID(apic_read(APIC_ID));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
832#ifdef CONFIG_X86_IO_APIC
833 {
834 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
835 int i;
836
837 for (i = 0; i < nr_ioapics; i++) {
838 if (smp_found_config) {
839 ioapic_phys = mp_ioapics[i].mpc_apicaddr;
840 if (!ioapic_phys) {
841 printk(KERN_ERR
842 "WARNING: bogus zero IO-APIC "
843 "address found in MPTABLE, "
844 "disabling IO/APIC support!\n");
845 smp_found_config = 0;
846 skip_ioapic_setup = 1;
847 goto fake_ioapic_page;
848 }
849 } else {
850fake_ioapic_page:
851 ioapic_phys = (unsigned long)
852 alloc_bootmem_pages(PAGE_SIZE);
853 ioapic_phys = __pa(ioapic_phys);
854 }
855 set_fixmap_nocache(idx, ioapic_phys);
856 printk(KERN_DEBUG "mapped IOAPIC to %08lx (%08lx)\n",
857 __fix_to_virt(idx), ioapic_phys);
858 idx++;
859 }
860 }
861#endif
862}
863
864/*
865 * This part sets up the APIC 32 bit clock in LVTT1, with HZ interrupts
866 * per second. We assume that the caller has already set up the local
867 * APIC.
868 *
869 * The APIC timer is not exactly sync with the external timer chip, it
870 * closely follows bus clocks.
871 */
872
873/*
874 * The timer chip is already set up at HZ interrupts per second here,
875 * but we do not accept timer interrupts yet. We only allow the BP
876 * to calibrate.
877 */
Li Shaohua0bb31842005-06-25 14:54:55 -0700878static unsigned int __devinit get_8254_timer_count(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 unsigned long flags;
881
882 unsigned int count;
883
884 spin_lock_irqsave(&i8253_lock, flags);
885
886 outb_p(0x00, PIT_MODE);
887 count = inb_p(PIT_CH0);
888 count |= inb_p(PIT_CH0) << 8;
889
890 spin_unlock_irqrestore(&i8253_lock, flags);
891
892 return count;
893}
894
895/* next tick in 8254 can be caught by catching timer wraparound */
Li Shaohua0bb31842005-06-25 14:54:55 -0700896static void __devinit wait_8254_wraparound(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897{
898 unsigned int curr_count, prev_count;
899
900 curr_count = get_8254_timer_count();
901 do {
902 prev_count = curr_count;
903 curr_count = get_8254_timer_count();
904
905 /* workaround for broken Mercury/Neptune */
906 if (prev_count >= curr_count + 0x100)
907 curr_count = get_8254_timer_count();
908
909 } while (prev_count >= curr_count);
910}
911
912/*
913 * Default initialization for 8254 timers. If we use other timers like HPET,
914 * we override this later
915 */
Li Shaohua0bb31842005-06-25 14:54:55 -0700916void (*wait_timer_tick)(void) __devinitdata = wait_8254_wraparound;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
918/*
919 * This function sets up the local APIC timer, with a timeout of
920 * 'clocks' APIC bus clock. During calibration we actually call
921 * this function twice on the boot CPU, once with a bogus timeout
922 * value, second time for real. The other (noncalibrating) CPUs
923 * call this function only once, with the real, calibrated value.
924 *
925 * We do reads before writes even if unnecessary, to get around the
926 * P5 APIC double write bug.
927 */
928
929#define APIC_DIVISOR 16
930
931static void __setup_APIC_LVTT(unsigned int clocks)
932{
933 unsigned int lvtt_value, tmp_value, ver;
934
935 ver = GET_APIC_VERSION(apic_read(APIC_LVR));
936 lvtt_value = APIC_LVT_TIMER_PERIODIC | LOCAL_TIMER_VECTOR;
937 if (!APIC_INTEGRATED(ver))
938 lvtt_value |= SET_APIC_TIMER_BASE(APIC_TIMER_BASE_DIV);
939 apic_write_around(APIC_LVTT, lvtt_value);
940
941 /*
942 * Divide PICLK by 16
943 */
944 tmp_value = apic_read(APIC_TDCR);
945 apic_write_around(APIC_TDCR, (tmp_value
946 & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE))
947 | APIC_TDR_DIV_16);
948
949 apic_write_around(APIC_TMICT, clocks/APIC_DIVISOR);
950}
951
Li Shaohua0bb31842005-06-25 14:54:55 -0700952static void __devinit setup_APIC_timer(unsigned int clocks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953{
954 unsigned long flags;
955
956 local_irq_save(flags);
957
958 /*
959 * Wait for IRQ0's slice:
960 */
961 wait_timer_tick();
962
963 __setup_APIC_LVTT(clocks);
964
965 local_irq_restore(flags);
966}
967
968/*
969 * In this function we calibrate APIC bus clocks to the external
970 * timer. Unfortunately we cannot use jiffies and the timer irq
971 * to calibrate, since some later bootup code depends on getting
972 * the first irq? Ugh.
973 *
974 * We want to do the calibration only once since we
975 * want to have local timer irqs syncron. CPUs connected
976 * by the same APIC bus have the very same bus frequency.
977 * And we want to have irqs off anyways, no accidental
978 * APIC irq that way.
979 */
980
981static int __init calibrate_APIC_clock(void)
982{
983 unsigned long long t1 = 0, t2 = 0;
984 long tt1, tt2;
985 long result;
986 int i;
987 const int LOOPS = HZ/10;
988
989 apic_printk(APIC_VERBOSE, "calibrating APIC timer ...\n");
990
991 /*
992 * Put whatever arbitrary (but long enough) timeout
993 * value into the APIC clock, we just want to get the
994 * counter running for calibration.
995 */
996 __setup_APIC_LVTT(1000000000);
997
998 /*
999 * The timer chip counts down to zero. Let's wait
1000 * for a wraparound to start exact measurement:
1001 * (the current tick might have been already half done)
1002 */
1003
1004 wait_timer_tick();
1005
1006 /*
1007 * We wrapped around just now. Let's start:
1008 */
1009 if (cpu_has_tsc)
1010 rdtscll(t1);
1011 tt1 = apic_read(APIC_TMCCT);
1012
1013 /*
1014 * Let's wait LOOPS wraprounds:
1015 */
1016 for (i = 0; i < LOOPS; i++)
1017 wait_timer_tick();
1018
1019 tt2 = apic_read(APIC_TMCCT);
1020 if (cpu_has_tsc)
1021 rdtscll(t2);
1022
1023 /*
1024 * The APIC bus clock counter is 32 bits only, it
1025 * might have overflown, but note that we use signed
1026 * longs, thus no extra care needed.
1027 *
1028 * underflown to be exact, as the timer counts down ;)
1029 */
1030
1031 result = (tt1-tt2)*APIC_DIVISOR/LOOPS;
1032
1033 if (cpu_has_tsc)
1034 apic_printk(APIC_VERBOSE, "..... CPU clock speed is "
1035 "%ld.%04ld MHz.\n",
1036 ((long)(t2-t1)/LOOPS)/(1000000/HZ),
1037 ((long)(t2-t1)/LOOPS)%(1000000/HZ));
1038
1039 apic_printk(APIC_VERBOSE, "..... host bus clock speed is "
1040 "%ld.%04ld MHz.\n",
1041 result/(1000000/HZ),
1042 result%(1000000/HZ));
1043
1044 return result;
1045}
1046
1047static unsigned int calibration_result;
1048
1049void __init setup_boot_APIC_clock(void)
1050{
Eric W. Biedermanf2b36db2005-10-30 14:59:41 -08001051 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 apic_printk(APIC_VERBOSE, "Using local APIC timer interrupts.\n");
1053 using_apic_timer = 1;
1054
Eric W. Biedermanf2b36db2005-10-30 14:59:41 -08001055 local_irq_save(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
1057 calibration_result = calibrate_APIC_clock();
1058 /*
1059 * Now set up the timer for real.
1060 */
1061 setup_APIC_timer(calibration_result);
1062
Eric W. Biedermanf2b36db2005-10-30 14:59:41 -08001063 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064}
1065
Li Shaohua0bb31842005-06-25 14:54:55 -07001066void __devinit setup_secondary_APIC_clock(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067{
1068 setup_APIC_timer(calibration_result);
1069}
1070
Zwane Mwaikambof3705132005-06-25 14:54:50 -07001071void __devinit disable_APIC_timer(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072{
1073 if (using_apic_timer) {
1074 unsigned long v;
1075
1076 v = apic_read(APIC_LVTT);
1077 apic_write_around(APIC_LVTT, v | APIC_LVT_MASKED);
1078 }
1079}
1080
1081void enable_APIC_timer(void)
1082{
1083 if (using_apic_timer) {
1084 unsigned long v;
1085
1086 v = apic_read(APIC_LVTT);
1087 apic_write_around(APIC_LVTT, v & ~APIC_LVT_MASKED);
1088 }
1089}
1090
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091#undef APIC_DIVISOR
1092
1093/*
1094 * Local timer interrupt handler. It does both profiling and
1095 * process statistics/rescheduling.
1096 *
1097 * We do profiling in every local tick, statistics/rescheduling
1098 * happen only every 'profiling multiplier' ticks. The default
1099 * multiplier is 1 and it can be changed by writing the new multiplier
1100 * value into /proc/profile.
1101 */
1102
1103inline void smp_local_timer_interrupt(struct pt_regs * regs)
1104{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 profile_tick(CPU_PROFILING, regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106#ifdef CONFIG_SMP
Venkatesh Pallipadi5a07a302006-01-11 22:44:18 +01001107 update_process_times(user_mode_vm(regs));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
1110 /*
1111 * We take the 'long' return path, and there every subsystem
1112 * grabs the apropriate locks (kernel lock/ irq lock).
1113 *
1114 * we might want to decouple profiling from the 'long path',
1115 * and do the profiling totally in assembly.
1116 *
1117 * Currently this isn't too much of an issue (performance wise),
1118 * we can take more than 100K local irqs per second on a 100 MHz P5.
1119 */
1120}
1121
1122/*
1123 * Local APIC timer interrupt. This is the most natural way for doing
1124 * local interrupts, but local timer interrupts can be emulated by
1125 * broadcast interrupts too. [in case the hw doesn't support APIC timers]
1126 *
1127 * [ if a single-CPU system runs an SMP kernel then we call the local
1128 * interrupt as well. Thus we cannot inline the local irq ... ]
1129 */
1130
1131fastcall void smp_apic_timer_interrupt(struct pt_regs *regs)
1132{
1133 int cpu = smp_processor_id();
1134
1135 /*
1136 * the NMI deadlock-detector uses this.
1137 */
1138 per_cpu(irq_stat, cpu).apic_timer_irqs++;
1139
1140 /*
1141 * NOTE! We'd better ACK the irq immediately,
1142 * because timer handling can be slow.
1143 */
1144 ack_APIC_irq();
1145 /*
1146 * update_process_times() expects us to have done irq_enter().
1147 * Besides, if we don't timer interrupts ignore the global
1148 * interrupt lock, which is the WrongThing (tm) to do.
1149 */
1150 irq_enter();
1151 smp_local_timer_interrupt(regs);
1152 irq_exit();
1153}
1154
Venkatesh Pallipadi5a07a302006-01-11 22:44:18 +01001155int setup_profiling_timer(unsigned int multiplier)
1156{
1157 return -EINVAL;
1158}
1159
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160/*
1161 * This interrupt should _never_ happen with our APIC/SMP architecture
1162 */
1163fastcall void smp_spurious_interrupt(struct pt_regs *regs)
1164{
1165 unsigned long v;
1166
1167 irq_enter();
1168 /*
1169 * Check if this really is a spurious interrupt and ACK it
1170 * if it is a vectored one. Just in case...
1171 * Spurious interrupts should not be ACKed.
1172 */
1173 v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
1174 if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
1175 ack_APIC_irq();
1176
1177 /* see sw-dev-man vol 3, chapter 7.4.13.5 */
1178 printk(KERN_INFO "spurious APIC interrupt on CPU#%d, should never happen.\n",
1179 smp_processor_id());
1180 irq_exit();
1181}
1182
1183/*
1184 * This interrupt should never happen with our APIC/SMP architecture
1185 */
1186
1187fastcall void smp_error_interrupt(struct pt_regs *regs)
1188{
1189 unsigned long v, v1;
1190
1191 irq_enter();
1192 /* First tickle the hardware, only then report what went on. -- REW */
1193 v = apic_read(APIC_ESR);
1194 apic_write(APIC_ESR, 0);
1195 v1 = apic_read(APIC_ESR);
1196 ack_APIC_irq();
1197 atomic_inc(&irq_err_count);
1198
1199 /* Here is what the APIC error bits mean:
1200 0: Send CS error
1201 1: Receive CS error
1202 2: Send accept error
1203 3: Receive accept error
1204 4: Reserved
1205 5: Send illegal vector
1206 6: Received illegal vector
1207 7: Illegal register address
1208 */
1209 printk (KERN_DEBUG "APIC error on CPU%d: %02lx(%02lx)\n",
1210 smp_processor_id(), v , v1);
1211 irq_exit();
1212}
1213
1214/*
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001215 * This initializes the IO-APIC and APIC hardware if this is
1216 * a UP kernel.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 */
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001218int __init APIC_init_uniprocessor (void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219{
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001220 if (enable_local_apic < 0)
1221 clear_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
Eric W. Biedermanf2b36db2005-10-30 14:59:41 -08001222
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001223 if (!smp_found_config && !cpu_has_apic)
Eric W. Biedermanf2b36db2005-10-30 14:59:41 -08001224 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
1226 /*
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001227 * Complain if the BIOS pretends there is one.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 */
1229 if (!cpu_has_apic && APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid])) {
1230 printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
1231 boot_cpu_physical_apicid);
1232 return -1;
1233 }
1234
1235 verify_local_APIC();
1236
1237 connect_bsp_APIC();
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001238
1239 phys_cpu_present_map = physid_mask_of_physid(boot_cpu_physical_apicid);
1240
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 setup_local_APIC();
1242
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243#ifdef CONFIG_X86_IO_APIC
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001244 if (smp_found_config)
1245 if (!skip_ioapic_setup && nr_ioapics)
1246 setup_IO_APIC();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247#endif
1248 setup_boot_APIC_clock();
Linus Torvalds1e4c85f2005-10-31 19:16:17 -08001249
1250 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251}