2 * Local APIC handling, local APIC timers
4 * (c) 1999, 2000 Ingo Molnar <mingo@redhat.com>
7 * Maciej W. Rozycki : Bits for genuine 82489DX APICs;
8 * thanks to Eric Gilmore
10 * for testing these extensively.
11 * Maciej W. Rozycki : Various updates and fixes.
12 * Mikael Pettersson : Power Management for UP-APIC.
14 * Mikael Pettersson : PM converted to driver model.
17 #include <linux/init.h>
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>
27 #include <linux/cpu.h>
28 #include <linux/module.h>
30 #include <asm/atomic.h>
33 #include <asm/mpspec.h>
35 #include <asm/arch_hooks.h>
37 #include <asm/i8253.h>
41 #include <mach_apic.h>
42 #include <mach_apicdef.h>
48 * cpu_mask that denotes the CPUs that needs timer interrupt coming in as
49 * IPIs in place of local APIC timers
51 static cpumask_t timer_bcast_ipi;
54 * Knob to control our willingness to enable the local APIC.
56 static int enable_local_apic __initdata = 0; /* -1=force-disable, +1=force-enable */
58 static inline void lapic_disable(void)
60 enable_local_apic = -1;
61 clear_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
64 static inline void lapic_enable(void)
66 enable_local_apic = 1;
75 static void apic_pm_activate(void);
77 static int modern_apic(void)
79 unsigned int lvr, version;
80 /* AMD systems use old APIC versions, so check the CPU */
81 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
82 boot_cpu_data.x86 >= 0xf)
84 lvr = apic_read(APIC_LVR);
85 version = GET_APIC_VERSION(lvr);
86 return version >= 0x14;
90 * 'what should we do if we get a hw irq event on an illegal vector'.
91 * each architecture has to answer this themselves.
93 void ack_bad_irq(unsigned int irq)
95 printk("unexpected IRQ trap at vector %02x\n", irq);
97 * Currently unexpected vectors happen only on SMP and APIC.
98 * We _must_ ack these because every local APIC has only N
99 * irq slots per priority level, and a 'hanging, unacked' IRQ
100 * holds up an irq slot - in excessive cases (when multiple
101 * unexpected vectors occur) that might lock up the APIC
103 * But only ack when the APIC is enabled -AK
109 void __init apic_intr_init(void)
114 /* self generated IPI for local APIC timer */
115 set_intr_gate(LOCAL_TIMER_VECTOR, apic_timer_interrupt);
117 /* IPI vectors for APIC spurious and error interrupts */
118 set_intr_gate(SPURIOUS_APIC_VECTOR, spurious_interrupt);
119 set_intr_gate(ERROR_APIC_VECTOR, error_interrupt);
121 /* thermal monitor LVT interrupt */
122 #ifdef CONFIG_X86_MCE_P4THERMAL
123 set_intr_gate(THERMAL_APIC_VECTOR, thermal_interrupt);
127 /* Using APIC to generate smp_local_timer_interrupt? */
128 int using_apic_timer __read_mostly = 0;
130 static int enabled_via_apicbase;
132 void enable_NMI_through_LVT0 (void * dummy)
136 ver = apic_read(APIC_LVR);
137 ver = GET_APIC_VERSION(ver);
138 v = APIC_DM_NMI; /* unmask and set to NMI */
139 if (!APIC_INTEGRATED(ver)) /* 82489DX */
140 v |= APIC_LVT_LEVEL_TRIGGER;
141 apic_write_around(APIC_LVT0, v);
144 int get_physical_broadcast(void)
154 unsigned int v, ver, maxlvt;
156 v = apic_read(APIC_LVR);
157 ver = GET_APIC_VERSION(v);
158 /* 82489DXs do not report # of LVT entries. */
159 maxlvt = APIC_INTEGRATED(ver) ? GET_APIC_MAXLVT(v) : 2;
163 void clear_local_APIC(void)
168 maxlvt = get_maxlvt();
171 * Masking an LVT entry can trigger a local APIC error
172 * if the vector is zero. Mask LVTERR first to prevent this.
175 v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
176 apic_write_around(APIC_LVTERR, v | APIC_LVT_MASKED);
179 * Careful: we have to set masks only first to deassert
180 * any level-triggered sources.
182 v = apic_read(APIC_LVTT);
183 apic_write_around(APIC_LVTT, v | APIC_LVT_MASKED);
184 v = apic_read(APIC_LVT0);
185 apic_write_around(APIC_LVT0, v | APIC_LVT_MASKED);
186 v = apic_read(APIC_LVT1);
187 apic_write_around(APIC_LVT1, v | APIC_LVT_MASKED);
189 v = apic_read(APIC_LVTPC);
190 apic_write_around(APIC_LVTPC, v | APIC_LVT_MASKED);
193 /* lets not touch this if we didn't frob it */
194 #ifdef CONFIG_X86_MCE_P4THERMAL
196 v = apic_read(APIC_LVTTHMR);
197 apic_write_around(APIC_LVTTHMR, v | APIC_LVT_MASKED);
201 * Clean APIC state for other OSs:
203 apic_write_around(APIC_LVTT, APIC_LVT_MASKED);
204 apic_write_around(APIC_LVT0, APIC_LVT_MASKED);
205 apic_write_around(APIC_LVT1, APIC_LVT_MASKED);
207 apic_write_around(APIC_LVTERR, APIC_LVT_MASKED);
209 apic_write_around(APIC_LVTPC, APIC_LVT_MASKED);
211 #ifdef CONFIG_X86_MCE_P4THERMAL
213 apic_write_around(APIC_LVTTHMR, APIC_LVT_MASKED);
215 v = GET_APIC_VERSION(apic_read(APIC_LVR));
216 if (APIC_INTEGRATED(v)) { /* !82489DX */
217 if (maxlvt > 3) /* Due to Pentium errata 3AP and 11AP. */
218 apic_write(APIC_ESR, 0);
223 void __init connect_bsp_APIC(void)
227 * Do not trust the local APIC being empty at bootup.
231 * PIC mode, enable APIC mode in the IMCR, i.e.
232 * connect BSP's local APIC to INT and NMI lines.
234 apic_printk(APIC_VERBOSE, "leaving PIC mode, "
235 "enabling APIC mode.\n");
242 void disconnect_bsp_APIC(int virt_wire_setup)
246 * Put the board back into PIC mode (has an effect
247 * only on certain older boards). Note that APIC
248 * interrupts, including IPIs, won't work beyond
249 * this point! The only exception are INIT IPIs.
251 apic_printk(APIC_VERBOSE, "disabling APIC mode, "
252 "entering PIC mode.\n");
257 /* Go back to Virtual Wire compatibility mode */
260 /* For the spurious interrupt use vector F, and enable it */
261 value = apic_read(APIC_SPIV);
262 value &= ~APIC_VECTOR_MASK;
263 value |= APIC_SPIV_APIC_ENABLED;
265 apic_write_around(APIC_SPIV, value);
267 if (!virt_wire_setup) {
268 /* For LVT0 make it edge triggered, active high, external and enabled */
269 value = apic_read(APIC_LVT0);
270 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
271 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
272 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED );
273 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
274 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
275 apic_write_around(APIC_LVT0, value);
279 apic_write_around(APIC_LVT0, APIC_LVT_MASKED);
282 /* For LVT1 make it edge triggered, active high, nmi and enabled */
283 value = apic_read(APIC_LVT1);
285 APIC_MODE_MASK | APIC_SEND_PENDING |
286 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
287 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
288 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
289 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
290 apic_write_around(APIC_LVT1, value);
294 void disable_local_APIC(void)
301 * Disable APIC (implies clearing of registers
304 value = apic_read(APIC_SPIV);
305 value &= ~APIC_SPIV_APIC_ENABLED;
306 apic_write_around(APIC_SPIV, value);
308 if (enabled_via_apicbase) {
310 rdmsr(MSR_IA32_APICBASE, l, h);
311 l &= ~MSR_IA32_APICBASE_ENABLE;
312 wrmsr(MSR_IA32_APICBASE, l, h);
317 * This is to verify that we're looking at a real local APIC.
318 * Check these against your board if the CPUs aren't getting
319 * started for no apparent reason.
321 int __init verify_local_APIC(void)
323 unsigned int reg0, reg1;
326 * The version register is read-only in a real APIC.
328 reg0 = apic_read(APIC_LVR);
329 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
330 apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
331 reg1 = apic_read(APIC_LVR);
332 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
335 * The two version reads above should print the same
336 * numbers. If the second one is different, then we
337 * poke at a non-APIC.
343 * Check if the version looks reasonably.
345 reg1 = GET_APIC_VERSION(reg0);
346 if (reg1 == 0x00 || reg1 == 0xff)
349 if (reg1 < 0x02 || reg1 == 0xff)
353 * The ID register is read/write in a real APIC.
355 reg0 = apic_read(APIC_ID);
356 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
359 * The next two are just to see if we have sane values.
360 * They're only really relevant if we're in Virtual Wire
361 * compatibility mode, but most boxes are anymore.
363 reg0 = apic_read(APIC_LVT0);
364 apic_printk(APIC_DEBUG, "Getting LVT0: %x\n", reg0);
365 reg1 = apic_read(APIC_LVT1);
366 apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
371 void __init sync_Arb_IDs(void)
373 /* Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1
374 And not needed on AMD */
380 apic_wait_icr_idle();
382 apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
383 apic_write_around(APIC_ICR, APIC_DEST_ALLINC | APIC_INT_LEVELTRIG
387 extern void __error_in_apic_c (void);
390 * An initial setup of the virtual wire mode.
392 void __init init_bsp_APIC(void)
394 unsigned long value, ver;
397 * Don't do the setup now if we have a SMP BIOS as the
398 * through-I/O-APIC virtual wire mode might be active.
400 if (smp_found_config || !cpu_has_apic)
403 value = apic_read(APIC_LVR);
404 ver = GET_APIC_VERSION(value);
407 * Do not trust the local APIC being empty at bootup.
414 value = apic_read(APIC_SPIV);
415 value &= ~APIC_VECTOR_MASK;
416 value |= APIC_SPIV_APIC_ENABLED;
418 /* This bit is reserved on P4/Xeon and should be cleared */
419 if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) && (boot_cpu_data.x86 == 15))
420 value &= ~APIC_SPIV_FOCUS_DISABLED;
422 value |= APIC_SPIV_FOCUS_DISABLED;
423 value |= SPURIOUS_APIC_VECTOR;
424 apic_write_around(APIC_SPIV, value);
427 * Set up the virtual wire mode.
429 apic_write_around(APIC_LVT0, APIC_DM_EXTINT);
431 if (!APIC_INTEGRATED(ver)) /* 82489DX */
432 value |= APIC_LVT_LEVEL_TRIGGER;
433 apic_write_around(APIC_LVT1, value);
436 void __devinit setup_local_APIC(void)
438 unsigned long oldvalue, value, ver, maxlvt;
441 /* Pound the ESR really hard over the head with a big hammer - mbligh */
443 apic_write(APIC_ESR, 0);
444 apic_write(APIC_ESR, 0);
445 apic_write(APIC_ESR, 0);
446 apic_write(APIC_ESR, 0);
449 value = apic_read(APIC_LVR);
450 ver = GET_APIC_VERSION(value);
452 if ((SPURIOUS_APIC_VECTOR & 0x0f) != 0x0f)
456 * Double-check whether this APIC is really registered.
458 if (!apic_id_registered())
462 * Intel recommends to set DFR, LDR and TPR before enabling
463 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
464 * document number 292116). So here it goes...
469 * Set Task Priority to 'accept all'. We never change this
472 value = apic_read(APIC_TASKPRI);
473 value &= ~APIC_TPRI_MASK;
474 apic_write_around(APIC_TASKPRI, value);
477 * After a crash, we no longer service the interrupts and a pending
478 * interrupt from previous kernel might still have ISR bit set.
480 * Most probably by now CPU has serviced that pending interrupt and
481 * it might not have done the ack_APIC_irq() because it thought,
482 * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
483 * does not clear the ISR bit and cpu thinks it has already serivced
484 * the interrupt. Hence a vector might get locked. It was noticed
485 * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
487 for (i = APIC_ISR_NR - 1; i >= 0; i--) {
488 value = apic_read(APIC_ISR + i*0x10);
489 for (j = 31; j >= 0; j--) {
496 * Now that we are all set up, enable the APIC
498 value = apic_read(APIC_SPIV);
499 value &= ~APIC_VECTOR_MASK;
503 value |= APIC_SPIV_APIC_ENABLED;
506 * Some unknown Intel IO/APIC (or APIC) errata is biting us with
507 * certain networking cards. If high frequency interrupts are
508 * happening on a particular IOAPIC pin, plus the IOAPIC routing
509 * entry is masked/unmasked at a high rate as well then sooner or
510 * later IOAPIC line gets 'stuck', no more interrupts are received
511 * from the device. If focus CPU is disabled then the hang goes
514 * [ This bug can be reproduced easily with a level-triggered
515 * PCI Ne2000 networking cards and PII/PIII processors, dual
519 * Actually disabling the focus CPU check just makes the hang less
520 * frequent as it makes the interrupt distributon model be more
521 * like LRU than MRU (the short-term load is more even across CPUs).
522 * See also the comment in end_level_ioapic_irq(). --macro
525 /* Enable focus processor (bit==0) */
526 value &= ~APIC_SPIV_FOCUS_DISABLED;
528 /* Disable focus processor (bit==1) */
529 value |= APIC_SPIV_FOCUS_DISABLED;
532 * Set spurious IRQ vector
534 value |= SPURIOUS_APIC_VECTOR;
535 apic_write_around(APIC_SPIV, value);
540 * set up through-local-APIC on the BP's LINT0. This is not
541 * strictly necessery in pure symmetric-IO mode, but sometimes
542 * we delegate interrupts to the 8259A.
545 * TODO: set up through-local-APIC from through-I/O-APIC? --macro
547 value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
548 if (!smp_processor_id() && (pic_mode || !value)) {
549 value = APIC_DM_EXTINT;
550 apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n",
553 value = APIC_DM_EXTINT | APIC_LVT_MASKED;
554 apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n",
557 apic_write_around(APIC_LVT0, value);
560 * only the BP should see the LINT1 NMI signal, obviously.
562 if (!smp_processor_id())
565 value = APIC_DM_NMI | APIC_LVT_MASKED;
566 if (!APIC_INTEGRATED(ver)) /* 82489DX */
567 value |= APIC_LVT_LEVEL_TRIGGER;
568 apic_write_around(APIC_LVT1, value);
570 if (APIC_INTEGRATED(ver) && !esr_disable) { /* !82489DX */
571 maxlvt = get_maxlvt();
572 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
573 apic_write(APIC_ESR, 0);
574 oldvalue = apic_read(APIC_ESR);
576 value = ERROR_APIC_VECTOR; // enables sending errors
577 apic_write_around(APIC_LVTERR, value);
579 * spec says clear errors after enabling vector.
582 apic_write(APIC_ESR, 0);
583 value = apic_read(APIC_ESR);
584 if (value != oldvalue)
585 apic_printk(APIC_VERBOSE, "ESR value before enabling "
586 "vector: 0x%08lx after: 0x%08lx\n",
591 * Something untraceble is creating bad interrupts on
592 * secondary quads ... for the moment, just leave the
593 * ESR disabled - we can't do anything useful with the
594 * errors anyway - mbligh
596 printk("Leaving ESR disabled.\n");
598 printk("No ESR for 82489DX.\n");
601 setup_apic_nmi_watchdog(NULL);
606 * If Linux enabled the LAPIC against the BIOS default
607 * disable it down before re-entering the BIOS on shutdown.
608 * Otherwise the BIOS may get confused and not power-off.
609 * Additionally clear all LVT entries before disable_local_APIC
610 * for the case where Linux didn't enable the LAPIC.
612 void lapic_shutdown(void)
619 local_irq_save(flags);
622 if (enabled_via_apicbase)
623 disable_local_APIC();
625 local_irq_restore(flags);
632 /* r/w apic fields */
633 unsigned int apic_id;
634 unsigned int apic_taskpri;
635 unsigned int apic_ldr;
636 unsigned int apic_dfr;
637 unsigned int apic_spiv;
638 unsigned int apic_lvtt;
639 unsigned int apic_lvtpc;
640 unsigned int apic_lvt0;
641 unsigned int apic_lvt1;
642 unsigned int apic_lvterr;
643 unsigned int apic_tmict;
644 unsigned int apic_tdcr;
645 unsigned int apic_thmr;
648 static int lapic_suspend(struct sys_device *dev, pm_message_t state)
653 if (!apic_pm_state.active)
656 maxlvt = get_maxlvt();
658 apic_pm_state.apic_id = apic_read(APIC_ID);
659 apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
660 apic_pm_state.apic_ldr = apic_read(APIC_LDR);
661 apic_pm_state.apic_dfr = apic_read(APIC_DFR);
662 apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
663 apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
665 apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
666 apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
667 apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
668 apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
669 apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
670 apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
671 #ifdef CONFIG_X86_MCE_P4THERMAL
673 apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
676 local_irq_save(flags);
677 disable_local_APIC();
678 local_irq_restore(flags);
682 static int lapic_resume(struct sys_device *dev)
688 if (!apic_pm_state.active)
691 maxlvt = get_maxlvt();
693 local_irq_save(flags);
696 * Make sure the APICBASE points to the right address
698 * FIXME! This will be wrong if we ever support suspend on
699 * SMP! We'll need to do this as part of the CPU restore!
701 rdmsr(MSR_IA32_APICBASE, l, h);
702 l &= ~MSR_IA32_APICBASE_BASE;
703 l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
704 wrmsr(MSR_IA32_APICBASE, l, h);
706 apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
707 apic_write(APIC_ID, apic_pm_state.apic_id);
708 apic_write(APIC_DFR, apic_pm_state.apic_dfr);
709 apic_write(APIC_LDR, apic_pm_state.apic_ldr);
710 apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
711 apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
712 apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
713 apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
714 #ifdef CONFIG_X86_MCE_P4THERMAL
716 apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
719 apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
720 apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
721 apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
722 apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
723 apic_write(APIC_ESR, 0);
725 apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
726 apic_write(APIC_ESR, 0);
728 local_irq_restore(flags);
733 * This device has no shutdown method - fully functioning local APICs
734 * are needed on every CPU up until machine_halt/restart/poweroff.
737 static struct sysdev_class lapic_sysclass = {
738 set_kset_name("lapic"),
739 .resume = lapic_resume,
740 .suspend = lapic_suspend,
743 static struct sys_device device_lapic = {
745 .cls = &lapic_sysclass,
748 static void __devinit apic_pm_activate(void)
750 apic_pm_state.active = 1;
753 static int __init init_lapic_sysfs(void)
759 /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
761 error = sysdev_class_register(&lapic_sysclass);
763 error = sysdev_register(&device_lapic);
766 device_initcall(init_lapic_sysfs);
768 #else /* CONFIG_PM */
770 static void apic_pm_activate(void) { }
772 #endif /* CONFIG_PM */
775 * Detect and enable local APICs on non-SMP boards.
776 * Original code written by Keir Fraser.
779 static int __init apic_set_verbosity(char *str)
781 if (strcmp("debug", str) == 0)
782 apic_verbosity = APIC_DEBUG;
783 else if (strcmp("verbose", str) == 0)
784 apic_verbosity = APIC_VERBOSE;
788 __setup("apic=", apic_set_verbosity);
790 static int __init detect_init_APIC (void)
794 /* Disabled by kernel option? */
795 if (enable_local_apic < 0)
798 switch (boot_cpu_data.x86_vendor) {
800 if ((boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model > 1) ||
801 (boot_cpu_data.x86 == 15))
804 case X86_VENDOR_INTEL:
805 if (boot_cpu_data.x86 == 6 || boot_cpu_data.x86 == 15 ||
806 (boot_cpu_data.x86 == 5 && cpu_has_apic))
815 * Over-ride BIOS and try to enable the local
816 * APIC only if "lapic" specified.
818 if (enable_local_apic <= 0) {
819 printk("Local APIC disabled by BIOS -- "
820 "you can enable it with \"lapic\"\n");
824 * Some BIOSes disable the local APIC in the
825 * APIC_BASE MSR. This can only be done in
826 * software for Intel P6 or later and AMD K7
827 * (Model > 1) or later.
829 rdmsr(MSR_IA32_APICBASE, l, h);
830 if (!(l & MSR_IA32_APICBASE_ENABLE)) {
831 printk("Local APIC disabled by BIOS -- reenabling.\n");
832 l &= ~MSR_IA32_APICBASE_BASE;
833 l |= MSR_IA32_APICBASE_ENABLE | APIC_DEFAULT_PHYS_BASE;
834 wrmsr(MSR_IA32_APICBASE, l, h);
835 enabled_via_apicbase = 1;
839 * The APIC feature bit should now be enabled
842 features = cpuid_edx(1);
843 if (!(features & (1 << X86_FEATURE_APIC))) {
844 printk("Could not enable APIC!\n");
847 set_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
848 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
850 /* The BIOS may have set up the APIC at some other address */
851 rdmsr(MSR_IA32_APICBASE, l, h);
852 if (l & MSR_IA32_APICBASE_ENABLE)
853 mp_lapic_addr = l & MSR_IA32_APICBASE_BASE;
855 if (nmi_watchdog != NMI_NONE)
856 nmi_watchdog = NMI_LOCAL_APIC;
858 printk("Found and enabled local APIC!\n");
865 printk("No local APIC present or hardware disabled\n");
869 void __init init_apic_mappings(void)
871 unsigned long apic_phys;
874 * If no local APIC can be found then set up a fake all
875 * zeroes page to simulate the local APIC and another
876 * one for the IO-APIC.
878 if (!smp_found_config && detect_init_APIC()) {
879 apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
880 apic_phys = __pa(apic_phys);
882 apic_phys = mp_lapic_addr;
884 set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
885 printk(KERN_DEBUG "mapped APIC to %08lx (%08lx)\n", APIC_BASE,
889 * Fetch the APIC ID of the BSP in case we have a
890 * default configuration (or the MP table is broken).
892 if (boot_cpu_physical_apicid == -1U)
893 boot_cpu_physical_apicid = GET_APIC_ID(apic_read(APIC_ID));
895 #ifdef CONFIG_X86_IO_APIC
897 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
900 for (i = 0; i < nr_ioapics; i++) {
901 if (smp_found_config) {
902 ioapic_phys = mp_ioapics[i].mpc_apicaddr;
905 "WARNING: bogus zero IO-APIC "
906 "address found in MPTABLE, "
907 "disabling IO/APIC support!\n");
908 smp_found_config = 0;
909 skip_ioapic_setup = 1;
910 goto fake_ioapic_page;
914 ioapic_phys = (unsigned long)
915 alloc_bootmem_pages(PAGE_SIZE);
916 ioapic_phys = __pa(ioapic_phys);
918 set_fixmap_nocache(idx, ioapic_phys);
919 printk(KERN_DEBUG "mapped IOAPIC to %08lx (%08lx)\n",
920 __fix_to_virt(idx), ioapic_phys);
928 * This part sets up the APIC 32 bit clock in LVTT1, with HZ interrupts
929 * per second. We assume that the caller has already set up the local
932 * The APIC timer is not exactly sync with the external timer chip, it
933 * closely follows bus clocks.
937 * The timer chip is already set up at HZ interrupts per second here,
938 * but we do not accept timer interrupts yet. We only allow the BP
941 static unsigned int __devinit get_8254_timer_count(void)
947 spin_lock_irqsave(&i8253_lock, flags);
949 outb_p(0x00, PIT_MODE);
950 count = inb_p(PIT_CH0);
951 count |= inb_p(PIT_CH0) << 8;
953 spin_unlock_irqrestore(&i8253_lock, flags);
958 /* next tick in 8254 can be caught by catching timer wraparound */
959 static void __devinit wait_8254_wraparound(void)
961 unsigned int curr_count, prev_count;
963 curr_count = get_8254_timer_count();
965 prev_count = curr_count;
966 curr_count = get_8254_timer_count();
968 /* workaround for broken Mercury/Neptune */
969 if (prev_count >= curr_count + 0x100)
970 curr_count = get_8254_timer_count();
972 } while (prev_count >= curr_count);
976 * Default initialization for 8254 timers. If we use other timers like HPET,
977 * we override this later
979 void (*wait_timer_tick)(void) __devinitdata = wait_8254_wraparound;
982 * This function sets up the local APIC timer, with a timeout of
983 * 'clocks' APIC bus clock. During calibration we actually call
984 * this function twice on the boot CPU, once with a bogus timeout
985 * value, second time for real. The other (noncalibrating) CPUs
986 * call this function only once, with the real, calibrated value.
988 * We do reads before writes even if unnecessary, to get around the
989 * P5 APIC double write bug.
992 #define APIC_DIVISOR 16
994 static void __setup_APIC_LVTT(unsigned int clocks)
996 unsigned int lvtt_value, tmp_value, ver;
997 int cpu = smp_processor_id();
999 ver = GET_APIC_VERSION(apic_read(APIC_LVR));
1000 lvtt_value = APIC_LVT_TIMER_PERIODIC | LOCAL_TIMER_VECTOR;
1001 if (!APIC_INTEGRATED(ver))
1002 lvtt_value |= SET_APIC_TIMER_BASE(APIC_TIMER_BASE_DIV);
1004 if (cpu_isset(cpu, timer_bcast_ipi))
1005 lvtt_value |= APIC_LVT_MASKED;
1007 apic_write_around(APIC_LVTT, lvtt_value);
1010 * Divide PICLK by 16
1012 tmp_value = apic_read(APIC_TDCR);
1013 apic_write_around(APIC_TDCR, (tmp_value
1014 & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE))
1017 apic_write_around(APIC_TMICT, clocks/APIC_DIVISOR);
1020 static void __devinit setup_APIC_timer(unsigned int clocks)
1022 unsigned long flags;
1024 local_irq_save(flags);
1027 * Wait for IRQ0's slice:
1031 __setup_APIC_LVTT(clocks);
1033 local_irq_restore(flags);
1037 * In this function we calibrate APIC bus clocks to the external
1038 * timer. Unfortunately we cannot use jiffies and the timer irq
1039 * to calibrate, since some later bootup code depends on getting
1040 * the first irq? Ugh.
1042 * We want to do the calibration only once since we
1043 * want to have local timer irqs syncron. CPUs connected
1044 * by the same APIC bus have the very same bus frequency.
1045 * And we want to have irqs off anyways, no accidental
1046 * APIC irq that way.
1049 static int __init calibrate_APIC_clock(void)
1051 unsigned long long t1 = 0, t2 = 0;
1055 const int LOOPS = HZ/10;
1057 apic_printk(APIC_VERBOSE, "calibrating APIC timer ...\n");
1060 * Put whatever arbitrary (but long enough) timeout
1061 * value into the APIC clock, we just want to get the
1062 * counter running for calibration.
1064 __setup_APIC_LVTT(1000000000);
1067 * The timer chip counts down to zero. Let's wait
1068 * for a wraparound to start exact measurement:
1069 * (the current tick might have been already half done)
1075 * We wrapped around just now. Let's start:
1079 tt1 = apic_read(APIC_TMCCT);
1082 * Let's wait LOOPS wraprounds:
1084 for (i = 0; i < LOOPS; i++)
1087 tt2 = apic_read(APIC_TMCCT);
1092 * The APIC bus clock counter is 32 bits only, it
1093 * might have overflown, but note that we use signed
1094 * longs, thus no extra care needed.
1096 * underflown to be exact, as the timer counts down ;)
1099 result = (tt1-tt2)*APIC_DIVISOR/LOOPS;
1102 apic_printk(APIC_VERBOSE, "..... CPU clock speed is "
1104 ((long)(t2-t1)/LOOPS)/(1000000/HZ),
1105 ((long)(t2-t1)/LOOPS)%(1000000/HZ));
1107 apic_printk(APIC_VERBOSE, "..... host bus clock speed is "
1109 result/(1000000/HZ),
1110 result%(1000000/HZ));
1115 static unsigned int calibration_result;
1117 void __init setup_boot_APIC_clock(void)
1119 unsigned long flags;
1120 apic_printk(APIC_VERBOSE, "Using local APIC timer interrupts.\n");
1121 using_apic_timer = 1;
1123 local_irq_save(flags);
1125 calibration_result = calibrate_APIC_clock();
1127 * Now set up the timer for real.
1129 setup_APIC_timer(calibration_result);
1131 local_irq_restore(flags);
1134 void __devinit setup_secondary_APIC_clock(void)
1136 setup_APIC_timer(calibration_result);
1139 void disable_APIC_timer(void)
1141 if (using_apic_timer) {
1144 v = apic_read(APIC_LVTT);
1146 * When an illegal vector value (0-15) is written to an LVT
1147 * entry and delivery mode is Fixed, the APIC may signal an
1148 * illegal vector error, with out regard to whether the mask
1149 * bit is set or whether an interrupt is actually seen on input.
1151 * Boot sequence might call this function when the LVTT has
1152 * '0' vector value. So make sure vector field is set to
1155 v |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
1156 apic_write_around(APIC_LVTT, v);
1160 void enable_APIC_timer(void)
1162 int cpu = smp_processor_id();
1164 if (using_apic_timer &&
1165 !cpu_isset(cpu, timer_bcast_ipi)) {
1168 v = apic_read(APIC_LVTT);
1169 apic_write_around(APIC_LVTT, v & ~APIC_LVT_MASKED);
1173 void switch_APIC_timer_to_ipi(void *cpumask)
1175 cpumask_t mask = *(cpumask_t *)cpumask;
1176 int cpu = smp_processor_id();
1178 if (cpu_isset(cpu, mask) &&
1179 !cpu_isset(cpu, timer_bcast_ipi)) {
1180 disable_APIC_timer();
1181 cpu_set(cpu, timer_bcast_ipi);
1184 EXPORT_SYMBOL(switch_APIC_timer_to_ipi);
1186 void switch_ipi_to_APIC_timer(void *cpumask)
1188 cpumask_t mask = *(cpumask_t *)cpumask;
1189 int cpu = smp_processor_id();
1191 if (cpu_isset(cpu, mask) &&
1192 cpu_isset(cpu, timer_bcast_ipi)) {
1193 cpu_clear(cpu, timer_bcast_ipi);
1194 enable_APIC_timer();
1197 EXPORT_SYMBOL(switch_ipi_to_APIC_timer);
1202 * Local timer interrupt handler. It does both profiling and
1203 * process statistics/rescheduling.
1205 * We do profiling in every local tick, statistics/rescheduling
1206 * happen only every 'profiling multiplier' ticks. The default
1207 * multiplier is 1 and it can be changed by writing the new multiplier
1208 * value into /proc/profile.
1211 inline void smp_local_timer_interrupt(void)
1213 profile_tick(CPU_PROFILING);
1215 update_process_times(user_mode_vm(get_irq_regs()));
1219 * We take the 'long' return path, and there every subsystem
1220 * grabs the apropriate locks (kernel lock/ irq lock).
1222 * we might want to decouple profiling from the 'long path',
1223 * and do the profiling totally in assembly.
1225 * Currently this isn't too much of an issue (performance wise),
1226 * we can take more than 100K local irqs per second on a 100 MHz P5.
1231 * Local APIC timer interrupt. This is the most natural way for doing
1232 * local interrupts, but local timer interrupts can be emulated by
1233 * broadcast interrupts too. [in case the hw doesn't support APIC timers]
1235 * [ if a single-CPU system runs an SMP kernel then we call the local
1236 * interrupt as well. Thus we cannot inline the local irq ... ]
1239 fastcall void smp_apic_timer_interrupt(struct pt_regs *regs)
1241 struct pt_regs *old_regs = set_irq_regs(regs);
1242 int cpu = smp_processor_id();
1245 * the NMI deadlock-detector uses this.
1247 per_cpu(irq_stat, cpu).apic_timer_irqs++;
1250 * NOTE! We'd better ACK the irq immediately,
1251 * because timer handling can be slow.
1255 * update_process_times() expects us to have done irq_enter().
1256 * Besides, if we don't timer interrupts ignore the global
1257 * interrupt lock, which is the WrongThing (tm) to do.
1261 smp_local_timer_interrupt();
1263 set_irq_regs(old_regs);
1267 static void up_apic_timer_interrupt_call(void)
1269 int cpu = smp_processor_id();
1272 * the NMI deadlock-detector uses this.
1274 per_cpu(irq_stat, cpu).apic_timer_irqs++;
1276 smp_local_timer_interrupt();
1280 void smp_send_timer_broadcast_ipi(void)
1284 cpus_and(mask, cpu_online_map, timer_bcast_ipi);
1285 if (!cpus_empty(mask)) {
1287 send_IPI_mask(mask, LOCAL_TIMER_VECTOR);
1290 * We can directly call the apic timer interrupt handler
1291 * in UP case. Minus all irq related functions
1293 up_apic_timer_interrupt_call();
1298 int setup_profiling_timer(unsigned int multiplier)
1304 * This interrupt should _never_ happen with our APIC/SMP architecture
1306 fastcall void smp_spurious_interrupt(struct pt_regs *regs)
1313 * Check if this really is a spurious interrupt and ACK it
1314 * if it is a vectored one. Just in case...
1315 * Spurious interrupts should not be ACKed.
1317 v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
1318 if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
1321 /* see sw-dev-man vol 3, chapter 7.4.13.5 */
1322 printk(KERN_INFO "spurious APIC interrupt on CPU#%d, should never happen.\n",
1323 smp_processor_id());
1328 * This interrupt should never happen with our APIC/SMP architecture
1331 fastcall void smp_error_interrupt(struct pt_regs *regs)
1333 unsigned long v, v1;
1337 /* First tickle the hardware, only then report what went on. -- REW */
1338 v = apic_read(APIC_ESR);
1339 apic_write(APIC_ESR, 0);
1340 v1 = apic_read(APIC_ESR);
1342 atomic_inc(&irq_err_count);
1344 /* Here is what the APIC error bits mean:
1347 2: Send accept error
1348 3: Receive accept error
1350 5: Send illegal vector
1351 6: Received illegal vector
1352 7: Illegal register address
1354 printk (KERN_DEBUG "APIC error on CPU%d: %02lx(%02lx)\n",
1355 smp_processor_id(), v , v1);
1360 * This initializes the IO-APIC and APIC hardware if this is
1363 int __init APIC_init_uniprocessor (void)
1365 if (enable_local_apic < 0)
1366 clear_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
1368 if (!smp_found_config && !cpu_has_apic)
1372 * Complain if the BIOS pretends there is one.
1374 if (!cpu_has_apic && APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid])) {
1375 printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
1376 boot_cpu_physical_apicid);
1377 clear_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
1381 verify_local_APIC();
1386 * Hack: In case of kdump, after a crash, kernel might be booting
1387 * on a cpu with non-zero lapic id. But boot_cpu_physical_apicid
1388 * might be zero if read from MP tables. Get it from LAPIC.
1390 #ifdef CONFIG_CRASH_DUMP
1391 boot_cpu_physical_apicid = GET_APIC_ID(apic_read(APIC_ID));
1393 phys_cpu_present_map = physid_mask_of_physid(boot_cpu_physical_apicid);
1397 #ifdef CONFIG_X86_IO_APIC
1398 if (smp_found_config)
1399 if (!skip_ioapic_setup && nr_ioapics)
1407 static int __init parse_lapic(char *arg)
1412 early_param("lapic", parse_lapic);
1414 static int __init parse_nolapic(char *arg)
1419 early_param("nolapic", parse_nolapic);