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>
50 #if (SPURIOUS_APIC_VECTOR & 0x0F) != 0x0F
51 # error SPURIOUS_APIC_VECTOR definition error
55 * cpu_mask that denotes the CPUs that needs timer interrupt coming in as
56 * IPIs in place of local APIC timers
58 static cpumask_t timer_bcast_ipi;
61 * Knob to control our willingness to enable the local APIC.
63 * -1=force-disable, +1=force-enable
65 static int enable_local_apic __initdata = 0;
68 * Debug level, exported for io_apic.c
72 static void apic_pm_activate(void);
75 /* Using APIC to generate smp_local_timer_interrupt? */
76 int using_apic_timer __read_mostly = 0;
78 /* Local APIC was disabled by the BIOS and enabled by the kernel */
79 static int enabled_via_apicbase;
82 * Get the LAPIC version
84 static inline int lapic_get_version(void)
86 return GET_APIC_VERSION(apic_read(APIC_LVR));
90 * Check, if the APIC is integrated or a seperate chip
92 static inline int lapic_is_integrated(void)
94 return APIC_INTEGRATED(lapic_get_version());
98 * Check, whether this is a modern or a first generation APIC
100 static int modern_apic(void)
102 /* AMD systems use old APIC versions, so check the CPU */
103 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
104 boot_cpu_data.x86 >= 0xf)
106 return lapic_get_version() >= 0x14;
110 * enable_NMI_through_LVT0 - enable NMI through local vector table 0
112 void enable_NMI_through_LVT0 (void * dummy)
114 unsigned int v = APIC_DM_NMI;
116 /* Level triggered for 82489DX */
117 if (!lapic_is_integrated())
118 v |= APIC_LVT_LEVEL_TRIGGER;
119 apic_write_around(APIC_LVT0, v);
123 * get_physical_broadcast - Get number of physical broadcast IDs
125 int get_physical_broadcast(void)
127 return modern_apic() ? 0xff : 0xf;
131 * lapic_get_maxlvt - get the maximum number of local vector table entries
133 int lapic_get_maxlvt(void)
135 unsigned int v = apic_read(APIC_LVR);
137 /* 82489DXs do not report # of LVT entries. */
138 return APIC_INTEGRATED(GET_APIC_VERSION(v)) ? GET_APIC_MAXLVT(v) : 2;
146 * This part sets up the APIC 32 bit clock in LVTT1, with HZ interrupts
147 * per second. We assume that the caller has already set up the local
150 * The APIC timer is not exactly sync with the external timer chip, it
151 * closely follows bus clocks.
155 * The timer chip is already set up at HZ interrupts per second here,
156 * but we do not accept timer interrupts yet. We only allow the BP
159 static unsigned int __devinit get_8254_timer_count(void)
165 spin_lock_irqsave(&i8253_lock, flags);
167 outb_p(0x00, PIT_MODE);
168 count = inb_p(PIT_CH0);
169 count |= inb_p(PIT_CH0) << 8;
171 spin_unlock_irqrestore(&i8253_lock, flags);
176 /* next tick in 8254 can be caught by catching timer wraparound */
177 static void __devinit wait_8254_wraparound(void)
179 unsigned int curr_count, prev_count;
181 curr_count = get_8254_timer_count();
183 prev_count = curr_count;
184 curr_count = get_8254_timer_count();
186 /* workaround for broken Mercury/Neptune */
187 if (prev_count >= curr_count + 0x100)
188 curr_count = get_8254_timer_count();
190 } while (prev_count >= curr_count);
194 * Default initialization for 8254 timers. If we use other timers like HPET,
195 * we override this later
197 void (*wait_timer_tick)(void) __devinitdata = wait_8254_wraparound;
200 * This function sets up the local APIC timer, with a timeout of
201 * 'clocks' APIC bus clock. During calibration we actually call
202 * this function twice on the boot CPU, once with a bogus timeout
203 * value, second time for real. The other (noncalibrating) CPUs
204 * call this function only once, with the real, calibrated value.
206 * We do reads before writes even if unnecessary, to get around the
207 * P5 APIC double write bug.
210 #define APIC_DIVISOR 16
212 static void __setup_APIC_LVTT(unsigned int clocks)
214 unsigned int lvtt_value, tmp_value;
215 int cpu = smp_processor_id();
217 lvtt_value = APIC_LVT_TIMER_PERIODIC | LOCAL_TIMER_VECTOR;
218 if (!lapic_is_integrated())
219 lvtt_value |= SET_APIC_TIMER_BASE(APIC_TIMER_BASE_DIV);
221 if (cpu_isset(cpu, timer_bcast_ipi))
222 lvtt_value |= APIC_LVT_MASKED;
224 apic_write_around(APIC_LVTT, lvtt_value);
229 tmp_value = apic_read(APIC_TDCR);
230 apic_write_around(APIC_TDCR, (tmp_value
231 & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE))
234 apic_write_around(APIC_TMICT, clocks/APIC_DIVISOR);
237 static void __devinit setup_APIC_timer(unsigned int clocks)
241 local_irq_save(flags);
244 * Wait for IRQ0's slice:
248 __setup_APIC_LVTT(clocks);
250 local_irq_restore(flags);
254 * In this function we calibrate APIC bus clocks to the external
255 * timer. Unfortunately we cannot use jiffies and the timer irq
256 * to calibrate, since some later bootup code depends on getting
257 * the first irq? Ugh.
259 * We want to do the calibration only once since we
260 * want to have local timer irqs syncron. CPUs connected
261 * by the same APIC bus have the very same bus frequency.
262 * And we want to have irqs off anyways, no accidental
266 static int __init calibrate_APIC_clock(void)
268 unsigned long long t1 = 0, t2 = 0;
272 const int LOOPS = HZ/10;
274 apic_printk(APIC_VERBOSE, "calibrating APIC timer ...\n");
277 * Put whatever arbitrary (but long enough) timeout
278 * value into the APIC clock, we just want to get the
279 * counter running for calibration.
281 __setup_APIC_LVTT(1000000000);
284 * The timer chip counts down to zero. Let's wait
285 * for a wraparound to start exact measurement:
286 * (the current tick might have been already half done)
292 * We wrapped around just now. Let's start:
296 tt1 = apic_read(APIC_TMCCT);
299 * Let's wait LOOPS wraprounds:
301 for (i = 0; i < LOOPS; i++)
304 tt2 = apic_read(APIC_TMCCT);
309 * The APIC bus clock counter is 32 bits only, it
310 * might have overflown, but note that we use signed
311 * longs, thus no extra care needed.
313 * underflown to be exact, as the timer counts down ;)
316 result = (tt1-tt2)*APIC_DIVISOR/LOOPS;
319 apic_printk(APIC_VERBOSE, "..... CPU clock speed is "
321 ((long)(t2-t1)/LOOPS)/(1000000/HZ),
322 ((long)(t2-t1)/LOOPS)%(1000000/HZ));
324 apic_printk(APIC_VERBOSE, "..... host bus clock speed is "
327 result%(1000000/HZ));
332 static unsigned int calibration_result;
334 void __init setup_boot_APIC_clock(void)
337 apic_printk(APIC_VERBOSE, "Using local APIC timer interrupts.\n");
338 using_apic_timer = 1;
340 local_irq_save(flags);
342 calibration_result = calibrate_APIC_clock();
344 * Now set up the timer for real.
346 setup_APIC_timer(calibration_result);
348 local_irq_restore(flags);
351 void __devinit setup_secondary_APIC_clock(void)
353 setup_APIC_timer(calibration_result);
356 void disable_APIC_timer(void)
358 if (using_apic_timer) {
361 v = apic_read(APIC_LVTT);
363 * When an illegal vector value (0-15) is written to an LVT
364 * entry and delivery mode is Fixed, the APIC may signal an
365 * illegal vector error, with out regard to whether the mask
366 * bit is set or whether an interrupt is actually seen on
369 * Boot sequence might call this function when the LVTT has
370 * '0' vector value. So make sure vector field is set to
373 v |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
374 apic_write_around(APIC_LVTT, v);
378 void enable_APIC_timer(void)
380 int cpu = smp_processor_id();
382 if (using_apic_timer && !cpu_isset(cpu, timer_bcast_ipi)) {
385 v = apic_read(APIC_LVTT);
386 apic_write_around(APIC_LVTT, v & ~APIC_LVT_MASKED);
390 void switch_APIC_timer_to_ipi(void *cpumask)
392 cpumask_t mask = *(cpumask_t *)cpumask;
393 int cpu = smp_processor_id();
395 if (cpu_isset(cpu, mask) &&
396 !cpu_isset(cpu, timer_bcast_ipi)) {
397 disable_APIC_timer();
398 cpu_set(cpu, timer_bcast_ipi);
401 EXPORT_SYMBOL(switch_APIC_timer_to_ipi);
403 void switch_ipi_to_APIC_timer(void *cpumask)
405 cpumask_t mask = *(cpumask_t *)cpumask;
406 int cpu = smp_processor_id();
408 if (cpu_isset(cpu, mask) &&
409 cpu_isset(cpu, timer_bcast_ipi)) {
410 cpu_clear(cpu, timer_bcast_ipi);
414 EXPORT_SYMBOL(switch_ipi_to_APIC_timer);
417 * Local timer interrupt handler. It does both profiling and
418 * process statistics/rescheduling.
420 inline void smp_local_timer_interrupt(void)
422 profile_tick(CPU_PROFILING);
424 update_process_times(user_mode_vm(get_irq_regs()));
428 * We take the 'long' return path, and there every subsystem
429 * grabs the apropriate locks (kernel lock/ irq lock).
431 * we might want to decouple profiling from the 'long path',
432 * and do the profiling totally in assembly.
434 * Currently this isn't too much of an issue (performance wise),
435 * we can take more than 100K local irqs per second on a 100 MHz P5.
440 * Local APIC timer interrupt. This is the most natural way for doing
441 * local interrupts, but local timer interrupts can be emulated by
442 * broadcast interrupts too. [in case the hw doesn't support APIC timers]
444 * [ if a single-CPU system runs an SMP kernel then we call the local
445 * interrupt as well. Thus we cannot inline the local irq ... ]
448 fastcall void smp_apic_timer_interrupt(struct pt_regs *regs)
450 struct pt_regs *old_regs = set_irq_regs(regs);
451 int cpu = smp_processor_id();
454 * the NMI deadlock-detector uses this.
456 per_cpu(irq_stat, cpu).apic_timer_irqs++;
459 * NOTE! We'd better ACK the irq immediately,
460 * because timer handling can be slow.
464 * update_process_times() expects us to have done irq_enter().
465 * Besides, if we don't timer interrupts ignore the global
466 * interrupt lock, which is the WrongThing (tm) to do.
470 smp_local_timer_interrupt();
472 set_irq_regs(old_regs);
476 static void up_apic_timer_interrupt_call(void)
478 int cpu = smp_processor_id();
481 * the NMI deadlock-detector uses this.
483 per_cpu(irq_stat, cpu).apic_timer_irqs++;
485 smp_local_timer_interrupt();
489 void smp_send_timer_broadcast_ipi(void)
493 cpus_and(mask, cpu_online_map, timer_bcast_ipi);
494 if (!cpus_empty(mask)) {
496 send_IPI_mask(mask, LOCAL_TIMER_VECTOR);
499 * We can directly call the apic timer interrupt handler
500 * in UP case. Minus all irq related functions
502 up_apic_timer_interrupt_call();
507 int setup_profiling_timer(unsigned int multiplier)
513 * Local APIC start and shutdown
517 * clear_local_APIC - shutdown the local APIC
519 * This is called, when a CPU is disabled and before rebooting, so the state of
520 * the local APIC has no dangling leftovers. Also used to cleanout any BIOS
521 * leftovers during boot.
523 void clear_local_APIC(void)
525 int maxlvt = lapic_get_maxlvt();
529 * Masking an LVT entry can trigger a local APIC error
530 * if the vector is zero. Mask LVTERR first to prevent this.
533 v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
534 apic_write_around(APIC_LVTERR, v | APIC_LVT_MASKED);
537 * Careful: we have to set masks only first to deassert
538 * any level-triggered sources.
540 v = apic_read(APIC_LVTT);
541 apic_write_around(APIC_LVTT, v | APIC_LVT_MASKED);
542 v = apic_read(APIC_LVT0);
543 apic_write_around(APIC_LVT0, v | APIC_LVT_MASKED);
544 v = apic_read(APIC_LVT1);
545 apic_write_around(APIC_LVT1, v | APIC_LVT_MASKED);
547 v = apic_read(APIC_LVTPC);
548 apic_write_around(APIC_LVTPC, v | APIC_LVT_MASKED);
551 /* lets not touch this if we didn't frob it */
552 #ifdef CONFIG_X86_MCE_P4THERMAL
554 v = apic_read(APIC_LVTTHMR);
555 apic_write_around(APIC_LVTTHMR, v | APIC_LVT_MASKED);
559 * Clean APIC state for other OSs:
561 apic_write_around(APIC_LVTT, APIC_LVT_MASKED);
562 apic_write_around(APIC_LVT0, APIC_LVT_MASKED);
563 apic_write_around(APIC_LVT1, APIC_LVT_MASKED);
565 apic_write_around(APIC_LVTERR, APIC_LVT_MASKED);
567 apic_write_around(APIC_LVTPC, APIC_LVT_MASKED);
569 #ifdef CONFIG_X86_MCE_P4THERMAL
571 apic_write_around(APIC_LVTTHMR, APIC_LVT_MASKED);
573 /* Integrated APIC (!82489DX) ? */
574 if (lapic_is_integrated()) {
576 /* Clear ESR due to Pentium errata 3AP and 11AP */
577 apic_write(APIC_ESR, 0);
583 * disable_local_APIC - clear and disable the local APIC
585 void disable_local_APIC(void)
592 * Disable APIC (implies clearing of registers
595 value = apic_read(APIC_SPIV);
596 value &= ~APIC_SPIV_APIC_ENABLED;
597 apic_write_around(APIC_SPIV, value);
600 * When LAPIC was disabled by the BIOS and enabled by the kernel,
601 * restore the disabled state.
603 if (enabled_via_apicbase) {
606 rdmsr(MSR_IA32_APICBASE, l, h);
607 l &= ~MSR_IA32_APICBASE_ENABLE;
608 wrmsr(MSR_IA32_APICBASE, l, h);
613 * If Linux enabled the LAPIC against the BIOS default disable it down before
614 * re-entering the BIOS on shutdown. Otherwise the BIOS may get confused and
615 * not power-off. Additionally clear all LVT entries before disable_local_APIC
616 * for the case where Linux didn't enable the LAPIC.
618 void lapic_shutdown(void)
625 local_irq_save(flags);
628 if (enabled_via_apicbase)
629 disable_local_APIC();
631 local_irq_restore(flags);
635 * This is to verify that we're looking at a real local APIC.
636 * Check these against your board if the CPUs aren't getting
637 * started for no apparent reason.
639 int __init verify_local_APIC(void)
641 unsigned int reg0, reg1;
644 * The version register is read-only in a real APIC.
646 reg0 = apic_read(APIC_LVR);
647 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
648 apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
649 reg1 = apic_read(APIC_LVR);
650 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
653 * The two version reads above should print the same
654 * numbers. If the second one is different, then we
655 * poke at a non-APIC.
661 * Check if the version looks reasonably.
663 reg1 = GET_APIC_VERSION(reg0);
664 if (reg1 == 0x00 || reg1 == 0xff)
666 reg1 = lapic_get_maxlvt();
667 if (reg1 < 0x02 || reg1 == 0xff)
671 * The ID register is read/write in a real APIC.
673 reg0 = apic_read(APIC_ID);
674 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
677 * The next two are just to see if we have sane values.
678 * They're only really relevant if we're in Virtual Wire
679 * compatibility mode, but most boxes are anymore.
681 reg0 = apic_read(APIC_LVT0);
682 apic_printk(APIC_DEBUG, "Getting LVT0: %x\n", reg0);
683 reg1 = apic_read(APIC_LVT1);
684 apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
690 * sync_Arb_IDs - synchronize APIC bus arbitration IDs
692 void __init sync_Arb_IDs(void)
695 * Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 And not
703 apic_wait_icr_idle();
705 apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
706 apic_write_around(APIC_ICR, APIC_DEST_ALLINC | APIC_INT_LEVELTRIG
711 * An initial setup of the virtual wire mode.
713 void __init init_bsp_APIC(void)
718 * Don't do the setup now if we have a SMP BIOS as the
719 * through-I/O-APIC virtual wire mode might be active.
721 if (smp_found_config || !cpu_has_apic)
725 * Do not trust the local APIC being empty at bootup.
732 value = apic_read(APIC_SPIV);
733 value &= ~APIC_VECTOR_MASK;
734 value |= APIC_SPIV_APIC_ENABLED;
736 /* This bit is reserved on P4/Xeon and should be cleared */
737 if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) &&
738 (boot_cpu_data.x86 == 15))
739 value &= ~APIC_SPIV_FOCUS_DISABLED;
741 value |= APIC_SPIV_FOCUS_DISABLED;
742 value |= SPURIOUS_APIC_VECTOR;
743 apic_write_around(APIC_SPIV, value);
746 * Set up the virtual wire mode.
748 apic_write_around(APIC_LVT0, APIC_DM_EXTINT);
750 if (!lapic_is_integrated()) /* 82489DX */
751 value |= APIC_LVT_LEVEL_TRIGGER;
752 apic_write_around(APIC_LVT1, value);
756 * setup_local_APIC - setup the local APIC
758 void __devinit setup_local_APIC(void)
760 unsigned long oldvalue, value, maxlvt, integrated;
763 /* Pound the ESR really hard over the head with a big hammer - mbligh */
765 apic_write(APIC_ESR, 0);
766 apic_write(APIC_ESR, 0);
767 apic_write(APIC_ESR, 0);
768 apic_write(APIC_ESR, 0);
771 integrated = lapic_is_integrated();
774 * Double-check whether this APIC is really registered.
776 if (!apic_id_registered())
780 * Intel recommends to set DFR, LDR and TPR before enabling
781 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
782 * document number 292116). So here it goes...
787 * Set Task Priority to 'accept all'. We never change this
790 value = apic_read(APIC_TASKPRI);
791 value &= ~APIC_TPRI_MASK;
792 apic_write_around(APIC_TASKPRI, value);
795 * After a crash, we no longer service the interrupts and a pending
796 * interrupt from previous kernel might still have ISR bit set.
798 * Most probably by now CPU has serviced that pending interrupt and
799 * it might not have done the ack_APIC_irq() because it thought,
800 * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
801 * does not clear the ISR bit and cpu thinks it has already serivced
802 * the interrupt. Hence a vector might get locked. It was noticed
803 * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
805 for (i = APIC_ISR_NR - 1; i >= 0; i--) {
806 value = apic_read(APIC_ISR + i*0x10);
807 for (j = 31; j >= 0; j--) {
814 * Now that we are all set up, enable the APIC
816 value = apic_read(APIC_SPIV);
817 value &= ~APIC_VECTOR_MASK;
821 value |= APIC_SPIV_APIC_ENABLED;
824 * Some unknown Intel IO/APIC (or APIC) errata is biting us with
825 * certain networking cards. If high frequency interrupts are
826 * happening on a particular IOAPIC pin, plus the IOAPIC routing
827 * entry is masked/unmasked at a high rate as well then sooner or
828 * later IOAPIC line gets 'stuck', no more interrupts are received
829 * from the device. If focus CPU is disabled then the hang goes
832 * [ This bug can be reproduced easily with a level-triggered
833 * PCI Ne2000 networking cards and PII/PIII processors, dual
837 * Actually disabling the focus CPU check just makes the hang less
838 * frequent as it makes the interrupt distributon model be more
839 * like LRU than MRU (the short-term load is more even across CPUs).
840 * See also the comment in end_level_ioapic_irq(). --macro
843 /* Enable focus processor (bit==0) */
844 value &= ~APIC_SPIV_FOCUS_DISABLED;
847 * Set spurious IRQ vector
849 value |= SPURIOUS_APIC_VECTOR;
850 apic_write_around(APIC_SPIV, value);
855 * set up through-local-APIC on the BP's LINT0. This is not
856 * strictly necessery in pure symmetric-IO mode, but sometimes
857 * we delegate interrupts to the 8259A.
860 * TODO: set up through-local-APIC from through-I/O-APIC? --macro
862 value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
863 if (!smp_processor_id() && (pic_mode || !value)) {
864 value = APIC_DM_EXTINT;
865 apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n",
868 value = APIC_DM_EXTINT | APIC_LVT_MASKED;
869 apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n",
872 apic_write_around(APIC_LVT0, value);
875 * only the BP should see the LINT1 NMI signal, obviously.
877 if (!smp_processor_id())
880 value = APIC_DM_NMI | APIC_LVT_MASKED;
881 if (!integrated) /* 82489DX */
882 value |= APIC_LVT_LEVEL_TRIGGER;
883 apic_write_around(APIC_LVT1, value);
885 if (integrated && !esr_disable) { /* !82489DX */
886 maxlvt = lapic_get_maxlvt();
887 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
888 apic_write(APIC_ESR, 0);
889 oldvalue = apic_read(APIC_ESR);
891 /* enables sending errors */
892 value = ERROR_APIC_VECTOR;
893 apic_write_around(APIC_LVTERR, value);
895 * spec says clear errors after enabling vector.
898 apic_write(APIC_ESR, 0);
899 value = apic_read(APIC_ESR);
900 if (value != oldvalue)
901 apic_printk(APIC_VERBOSE, "ESR value before enabling "
902 "vector: 0x%08lx after: 0x%08lx\n",
907 * Something untraceble is creating bad interrupts on
908 * secondary quads ... for the moment, just leave the
909 * ESR disabled - we can't do anything useful with the
910 * errors anyway - mbligh
912 printk(KERN_INFO "Leaving ESR disabled.\n");
914 printk(KERN_INFO "No ESR for 82489DX.\n");
917 setup_apic_nmi_watchdog(NULL);
922 * Detect and initialize APIC
924 static int __init detect_init_APIC (void)
928 /* Disabled by kernel option? */
929 if (enable_local_apic < 0)
932 switch (boot_cpu_data.x86_vendor) {
934 if ((boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model > 1) ||
935 (boot_cpu_data.x86 == 15))
938 case X86_VENDOR_INTEL:
939 if (boot_cpu_data.x86 == 6 || boot_cpu_data.x86 == 15 ||
940 (boot_cpu_data.x86 == 5 && cpu_has_apic))
949 * Over-ride BIOS and try to enable the local APIC only if
952 if (enable_local_apic <= 0) {
953 printk(KERN_INFO "Local APIC disabled by BIOS -- "
954 "you can enable it with \"lapic\"\n");
958 * Some BIOSes disable the local APIC in the APIC_BASE
959 * MSR. This can only be done in software for Intel P6 or later
960 * and AMD K7 (Model > 1) or later.
962 rdmsr(MSR_IA32_APICBASE, l, h);
963 if (!(l & MSR_IA32_APICBASE_ENABLE)) {
965 "Local APIC disabled by BIOS -- reenabling.\n");
966 l &= ~MSR_IA32_APICBASE_BASE;
967 l |= MSR_IA32_APICBASE_ENABLE | APIC_DEFAULT_PHYS_BASE;
968 wrmsr(MSR_IA32_APICBASE, l, h);
969 enabled_via_apicbase = 1;
973 * The APIC feature bit should now be enabled
976 features = cpuid_edx(1);
977 if (!(features & (1 << X86_FEATURE_APIC))) {
978 printk(KERN_WARNING "Could not enable APIC!\n");
981 set_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
982 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
984 /* The BIOS may have set up the APIC at some other address */
985 rdmsr(MSR_IA32_APICBASE, l, h);
986 if (l & MSR_IA32_APICBASE_ENABLE)
987 mp_lapic_addr = l & MSR_IA32_APICBASE_BASE;
989 if (nmi_watchdog != NMI_NONE)
990 nmi_watchdog = NMI_LOCAL_APIC;
992 printk(KERN_INFO "Found and enabled local APIC!\n");
999 printk(KERN_INFO "No local APIC present or hardware disabled\n");
1004 * init_apic_mappings - initialize APIC mappings
1006 void __init init_apic_mappings(void)
1008 unsigned long apic_phys;
1011 * If no local APIC can be found then set up a fake all
1012 * zeroes page to simulate the local APIC and another
1013 * one for the IO-APIC.
1015 if (!smp_found_config && detect_init_APIC()) {
1016 apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
1017 apic_phys = __pa(apic_phys);
1019 apic_phys = mp_lapic_addr;
1021 set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
1022 printk(KERN_DEBUG "mapped APIC to %08lx (%08lx)\n", APIC_BASE,
1026 * Fetch the APIC ID of the BSP in case we have a
1027 * default configuration (or the MP table is broken).
1029 if (boot_cpu_physical_apicid == -1U)
1030 boot_cpu_physical_apicid = GET_APIC_ID(apic_read(APIC_ID));
1032 #ifdef CONFIG_X86_IO_APIC
1034 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
1037 for (i = 0; i < nr_ioapics; i++) {
1038 if (smp_found_config) {
1039 ioapic_phys = mp_ioapics[i].mpc_apicaddr;
1042 "WARNING: bogus zero IO-APIC "
1043 "address found in MPTABLE, "
1044 "disabling IO/APIC support!\n");
1045 smp_found_config = 0;
1046 skip_ioapic_setup = 1;
1047 goto fake_ioapic_page;
1051 ioapic_phys = (unsigned long)
1052 alloc_bootmem_pages(PAGE_SIZE);
1053 ioapic_phys = __pa(ioapic_phys);
1055 set_fixmap_nocache(idx, ioapic_phys);
1056 printk(KERN_DEBUG "mapped IOAPIC to %08lx (%08lx)\n",
1057 __fix_to_virt(idx), ioapic_phys);
1065 * This initializes the IO-APIC and APIC hardware if this is
1068 int __init APIC_init_uniprocessor (void)
1070 if (enable_local_apic < 0)
1071 clear_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
1073 if (!smp_found_config && !cpu_has_apic)
1077 * Complain if the BIOS pretends there is one.
1079 if (!cpu_has_apic &&
1080 APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid])) {
1081 printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
1082 boot_cpu_physical_apicid);
1083 clear_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
1087 verify_local_APIC();
1092 * Hack: In case of kdump, after a crash, kernel might be booting
1093 * on a cpu with non-zero lapic id. But boot_cpu_physical_apicid
1094 * might be zero if read from MP tables. Get it from LAPIC.
1096 #ifdef CONFIG_CRASH_DUMP
1097 boot_cpu_physical_apicid = GET_APIC_ID(apic_read(APIC_ID));
1099 phys_cpu_present_map = physid_mask_of_physid(boot_cpu_physical_apicid);
1103 #ifdef CONFIG_X86_IO_APIC
1104 if (smp_found_config)
1105 if (!skip_ioapic_setup && nr_ioapics)
1114 * APIC command line parameters
1116 static int __init parse_lapic(char *arg)
1118 enable_local_apic = 1;
1121 early_param("lapic", parse_lapic);
1123 static int __init parse_nolapic(char *arg)
1125 enable_local_apic = -1;
1126 clear_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
1129 early_param("nolapic", parse_nolapic);
1131 static int __init apic_set_verbosity(char *str)
1133 if (strcmp("debug", str) == 0)
1134 apic_verbosity = APIC_DEBUG;
1135 else if (strcmp("verbose", str) == 0)
1136 apic_verbosity = APIC_VERBOSE;
1140 __setup("apic=", apic_set_verbosity);
1144 * Local APIC interrupts
1148 * This interrupt should _never_ happen with our APIC/SMP architecture
1150 fastcall void smp_spurious_interrupt(struct pt_regs *regs)
1157 * Check if this really is a spurious interrupt and ACK it
1158 * if it is a vectored one. Just in case...
1159 * Spurious interrupts should not be ACKed.
1161 v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
1162 if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
1165 /* see sw-dev-man vol 3, chapter 7.4.13.5 */
1166 printk(KERN_INFO "spurious APIC interrupt on CPU#%d, "
1167 "should never happen.\n", smp_processor_id());
1172 * This interrupt should never happen with our APIC/SMP architecture
1174 fastcall void smp_error_interrupt(struct pt_regs *regs)
1176 unsigned long v, v1;
1180 /* First tickle the hardware, only then report what went on. -- REW */
1181 v = apic_read(APIC_ESR);
1182 apic_write(APIC_ESR, 0);
1183 v1 = apic_read(APIC_ESR);
1185 atomic_inc(&irq_err_count);
1187 /* Here is what the APIC error bits mean:
1190 2: Send accept error
1191 3: Receive accept error
1193 5: Send illegal vector
1194 6: Received illegal vector
1195 7: Illegal register address
1197 printk (KERN_DEBUG "APIC error on CPU%d: %02lx(%02lx)\n",
1198 smp_processor_id(), v , v1);
1203 * Initialize APIC interrupts
1205 void __init apic_intr_init(void)
1210 /* self generated IPI for local APIC timer */
1211 set_intr_gate(LOCAL_TIMER_VECTOR, apic_timer_interrupt);
1213 /* IPI vectors for APIC spurious and error interrupts */
1214 set_intr_gate(SPURIOUS_APIC_VECTOR, spurious_interrupt);
1215 set_intr_gate(ERROR_APIC_VECTOR, error_interrupt);
1217 /* thermal monitor LVT interrupt */
1218 #ifdef CONFIG_X86_MCE_P4THERMAL
1219 set_intr_gate(THERMAL_APIC_VECTOR, thermal_interrupt);
1224 * connect_bsp_APIC - attach the APIC to the interrupt system
1226 void __init connect_bsp_APIC(void)
1230 * Do not trust the local APIC being empty at bootup.
1234 * PIC mode, enable APIC mode in the IMCR, i.e. connect BSP's
1235 * local APIC to INT and NMI lines.
1237 apic_printk(APIC_VERBOSE, "leaving PIC mode, "
1238 "enabling APIC mode.\n");
1246 * disconnect_bsp_APIC - detach the APIC from the interrupt system
1247 * @virt_wire_setup: indicates, whether virtual wire mode is selected
1249 * Virtual wire mode is necessary to deliver legacy interrupts even when the
1252 void disconnect_bsp_APIC(int virt_wire_setup)
1256 * Put the board back into PIC mode (has an effect only on
1257 * certain older boards). Note that APIC interrupts, including
1258 * IPIs, won't work beyond this point! The only exception are
1261 apic_printk(APIC_VERBOSE, "disabling APIC mode, "
1262 "entering PIC mode.\n");
1266 /* Go back to Virtual Wire compatibility mode */
1267 unsigned long value;
1269 /* For the spurious interrupt use vector F, and enable it */
1270 value = apic_read(APIC_SPIV);
1271 value &= ~APIC_VECTOR_MASK;
1272 value |= APIC_SPIV_APIC_ENABLED;
1274 apic_write_around(APIC_SPIV, value);
1276 if (!virt_wire_setup) {
1278 * For LVT0 make it edge triggered, active high,
1279 * external and enabled
1281 value = apic_read(APIC_LVT0);
1282 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
1283 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
1284 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED );
1285 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1286 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
1287 apic_write_around(APIC_LVT0, value);
1290 apic_write_around(APIC_LVT0, APIC_LVT_MASKED);
1294 * For LVT1 make it edge triggered, active high, nmi and
1297 value = apic_read(APIC_LVT1);
1299 APIC_MODE_MASK | APIC_SEND_PENDING |
1300 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
1301 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
1302 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1303 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
1304 apic_write_around(APIC_LVT1, value);
1315 /* r/w apic fields */
1316 unsigned int apic_id;
1317 unsigned int apic_taskpri;
1318 unsigned int apic_ldr;
1319 unsigned int apic_dfr;
1320 unsigned int apic_spiv;
1321 unsigned int apic_lvtt;
1322 unsigned int apic_lvtpc;
1323 unsigned int apic_lvt0;
1324 unsigned int apic_lvt1;
1325 unsigned int apic_lvterr;
1326 unsigned int apic_tmict;
1327 unsigned int apic_tdcr;
1328 unsigned int apic_thmr;
1331 static int lapic_suspend(struct sys_device *dev, pm_message_t state)
1333 unsigned long flags;
1336 if (!apic_pm_state.active)
1339 maxlvt = lapic_get_maxlvt();
1341 apic_pm_state.apic_id = apic_read(APIC_ID);
1342 apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
1343 apic_pm_state.apic_ldr = apic_read(APIC_LDR);
1344 apic_pm_state.apic_dfr = apic_read(APIC_DFR);
1345 apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
1346 apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
1348 apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
1349 apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
1350 apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
1351 apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
1352 apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
1353 apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
1354 #ifdef CONFIG_X86_MCE_P4THERMAL
1356 apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
1359 local_irq_save(flags);
1360 disable_local_APIC();
1361 local_irq_restore(flags);
1365 static int lapic_resume(struct sys_device *dev)
1368 unsigned long flags;
1371 if (!apic_pm_state.active)
1374 maxlvt = lapic_get_maxlvt();
1376 local_irq_save(flags);
1379 * Make sure the APICBASE points to the right address
1381 * FIXME! This will be wrong if we ever support suspend on
1382 * SMP! We'll need to do this as part of the CPU restore!
1384 rdmsr(MSR_IA32_APICBASE, l, h);
1385 l &= ~MSR_IA32_APICBASE_BASE;
1386 l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
1387 wrmsr(MSR_IA32_APICBASE, l, h);
1389 apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
1390 apic_write(APIC_ID, apic_pm_state.apic_id);
1391 apic_write(APIC_DFR, apic_pm_state.apic_dfr);
1392 apic_write(APIC_LDR, apic_pm_state.apic_ldr);
1393 apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
1394 apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
1395 apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
1396 apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
1397 #ifdef CONFIG_X86_MCE_P4THERMAL
1399 apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
1402 apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
1403 apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
1404 apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
1405 apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
1406 apic_write(APIC_ESR, 0);
1407 apic_read(APIC_ESR);
1408 apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
1409 apic_write(APIC_ESR, 0);
1410 apic_read(APIC_ESR);
1411 local_irq_restore(flags);
1416 * This device has no shutdown method - fully functioning local APICs
1417 * are needed on every CPU up until machine_halt/restart/poweroff.
1420 static struct sysdev_class lapic_sysclass = {
1421 set_kset_name("lapic"),
1422 .resume = lapic_resume,
1423 .suspend = lapic_suspend,
1426 static struct sys_device device_lapic = {
1428 .cls = &lapic_sysclass,
1431 static void __devinit apic_pm_activate(void)
1433 apic_pm_state.active = 1;
1436 static int __init init_lapic_sysfs(void)
1442 /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
1444 error = sysdev_class_register(&lapic_sysclass);
1446 error = sysdev_register(&device_lapic);
1449 device_initcall(init_lapic_sysfs);
1451 #else /* CONFIG_PM */
1453 static void apic_pm_activate(void) { }
1455 #endif /* CONFIG_PM */