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/interrupt.h>
23 #include <linux/mc146818rtc.h>
24 #include <linux/kernel_stat.h>
25 #include <linux/sysdev.h>
26 #include <linux/cpu.h>
27 #include <linux/clockchips.h>
28 #include <linux/acpi_pmtmr.h>
29 #include <linux/module.h>
30 #include <linux/dmi.h>
32 #include <asm/atomic.h>
35 #include <asm/mpspec.h>
37 #include <asm/arch_hooks.h>
39 #include <asm/i8253.h>
42 #include <mach_apic.h>
43 #include <mach_apicdef.h>
49 #if ((SPURIOUS_APIC_VECTOR & 0x0F) != 0x0F)
50 # error SPURIOUS_APIC_VECTOR definition error
54 * Knob to control our willingness to enable the local APIC.
56 * -1=force-disable, +1=force-enable
58 static int enable_local_apic __initdata;
60 /* Local APIC timer verification ok */
61 static int local_apic_timer_verify_ok;
62 /* Disable local APIC timer from the kernel commandline or via dmi quirk
63 or using CPU MSR check */
64 int local_apic_timer_disabled;
65 /* Local APIC timer works in C2 */
66 int local_apic_timer_c2_ok;
67 EXPORT_SYMBOL_GPL(local_apic_timer_c2_ok);
70 * Debug level, exported for io_apic.c
74 static unsigned int calibration_result;
76 static int lapic_next_event(unsigned long delta,
77 struct clock_event_device *evt);
78 static void lapic_timer_setup(enum clock_event_mode mode,
79 struct clock_event_device *evt);
80 static void lapic_timer_broadcast(cpumask_t mask);
81 static void apic_pm_activate(void);
84 * The local apic timer can be used for any function which is CPU local.
86 static struct clock_event_device lapic_clockevent = {
88 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT
89 | CLOCK_EVT_FEAT_C3STOP | CLOCK_EVT_FEAT_DUMMY,
91 .set_mode = lapic_timer_setup,
92 .set_next_event = lapic_next_event,
93 .broadcast = lapic_timer_broadcast,
97 static DEFINE_PER_CPU(struct clock_event_device, lapic_events);
99 /* Local APIC was disabled by the BIOS and enabled by the kernel */
100 static int enabled_via_apicbase;
102 static unsigned long apic_phys;
105 * Get the LAPIC version
107 static inline int lapic_get_version(void)
109 return GET_APIC_VERSION(apic_read(APIC_LVR));
113 * Check, if the APIC is integrated or a separate chip
115 static inline int lapic_is_integrated(void)
117 return APIC_INTEGRATED(lapic_get_version());
121 * Check, whether this is a modern or a first generation APIC
123 static int modern_apic(void)
125 /* AMD systems use old APIC versions, so check the CPU */
126 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
127 boot_cpu_data.x86 >= 0xf)
129 return lapic_get_version() >= 0x14;
132 void apic_wait_icr_idle(void)
134 while (apic_read(APIC_ICR) & APIC_ICR_BUSY)
138 u32 safe_apic_wait_icr_idle(void)
145 send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
149 } while (timeout++ < 1000);
155 * enable_NMI_through_LVT0 - enable NMI through local vector table 0
157 void __cpuinit enable_NMI_through_LVT0(void)
159 unsigned int v = APIC_DM_NMI;
161 /* Level triggered for 82489DX */
162 if (!lapic_is_integrated())
163 v |= APIC_LVT_LEVEL_TRIGGER;
164 apic_write_around(APIC_LVT0, v);
168 * get_physical_broadcast - Get number of physical broadcast IDs
170 int get_physical_broadcast(void)
172 return modern_apic() ? 0xff : 0xf;
176 * lapic_get_maxlvt - get the maximum number of local vector table entries
178 int lapic_get_maxlvt(void)
180 unsigned int v = apic_read(APIC_LVR);
182 /* 82489DXs do not report # of LVT entries. */
183 return APIC_INTEGRATED(GET_APIC_VERSION(v)) ? GET_APIC_MAXLVT(v) : 2;
190 /* Clock divisor is set to 16 */
191 #define APIC_DIVISOR 16
194 * This function sets up the local APIC timer, with a timeout of
195 * 'clocks' APIC bus clock. During calibration we actually call
196 * this function twice on the boot CPU, once with a bogus timeout
197 * value, second time for real. The other (noncalibrating) CPUs
198 * call this function only once, with the real, calibrated value.
200 * We do reads before writes even if unnecessary, to get around the
201 * P5 APIC double write bug.
203 static void __setup_APIC_LVTT(unsigned int clocks, int oneshot, int irqen)
205 unsigned int lvtt_value, tmp_value;
207 lvtt_value = LOCAL_TIMER_VECTOR;
209 lvtt_value |= APIC_LVT_TIMER_PERIODIC;
210 if (!lapic_is_integrated())
211 lvtt_value |= SET_APIC_TIMER_BASE(APIC_TIMER_BASE_DIV);
214 lvtt_value |= APIC_LVT_MASKED;
216 apic_write_around(APIC_LVTT, lvtt_value);
221 tmp_value = apic_read(APIC_TDCR);
222 apic_write_around(APIC_TDCR, (tmp_value
223 & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE))
227 apic_write_around(APIC_TMICT, clocks/APIC_DIVISOR);
231 * Program the next event, relative to now
233 static int lapic_next_event(unsigned long delta,
234 struct clock_event_device *evt)
236 apic_write_around(APIC_TMICT, delta);
241 * Setup the lapic timer in periodic or oneshot mode
243 static void lapic_timer_setup(enum clock_event_mode mode,
244 struct clock_event_device *evt)
249 /* Lapic used for broadcast ? */
250 if (!local_apic_timer_verify_ok)
253 local_irq_save(flags);
256 case CLOCK_EVT_MODE_PERIODIC:
257 case CLOCK_EVT_MODE_ONESHOT:
258 __setup_APIC_LVTT(calibration_result,
259 mode != CLOCK_EVT_MODE_PERIODIC, 1);
261 case CLOCK_EVT_MODE_UNUSED:
262 case CLOCK_EVT_MODE_SHUTDOWN:
263 v = apic_read(APIC_LVTT);
264 v |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
265 apic_write_around(APIC_LVTT, v);
267 case CLOCK_EVT_MODE_RESUME:
268 /* Nothing to do here */
272 local_irq_restore(flags);
276 * Local APIC timer broadcast function
278 static void lapic_timer_broadcast(cpumask_t mask)
281 send_IPI_mask(mask, LOCAL_TIMER_VECTOR);
286 * Setup the local APIC timer for this CPU. Copy the initilized values
287 * of the boot CPU and register the clock event in the framework.
289 static void __devinit setup_APIC_timer(void)
291 struct clock_event_device *levt = &__get_cpu_var(lapic_events);
293 memcpy(levt, &lapic_clockevent, sizeof(*levt));
294 levt->cpumask = cpumask_of_cpu(smp_processor_id());
296 clockevents_register_device(levt);
300 * In this functions we calibrate APIC bus clocks to the external timer.
302 * We want to do the calibration only once since we want to have local timer
303 * irqs syncron. CPUs connected by the same APIC bus have the very same bus
306 * This was previously done by reading the PIT/HPET and waiting for a wrap
307 * around to find out, that a tick has elapsed. I have a box, where the PIT
308 * readout is broken, so it never gets out of the wait loop again. This was
309 * also reported by others.
311 * Monitoring the jiffies value is inaccurate and the clockevents
312 * infrastructure allows us to do a simple substitution of the interrupt
315 * The calibration routine also uses the pm_timer when possible, as the PIT
316 * happens to run way too slow (factor 2.3 on my VAIO CoreDuo, which goes
317 * back to normal later in the boot process).
320 #define LAPIC_CAL_LOOPS (HZ/10)
322 static __initdata int lapic_cal_loops = -1;
323 static __initdata long lapic_cal_t1, lapic_cal_t2;
324 static __initdata unsigned long long lapic_cal_tsc1, lapic_cal_tsc2;
325 static __initdata unsigned long lapic_cal_pm1, lapic_cal_pm2;
326 static __initdata unsigned long lapic_cal_j1, lapic_cal_j2;
329 * Temporary interrupt handler.
331 static void __init lapic_cal_handler(struct clock_event_device *dev)
333 unsigned long long tsc = 0;
334 long tapic = apic_read(APIC_TMCCT);
335 unsigned long pm = acpi_pm_read_early();
340 switch (lapic_cal_loops++) {
342 lapic_cal_t1 = tapic;
343 lapic_cal_tsc1 = tsc;
345 lapic_cal_j1 = jiffies;
348 case LAPIC_CAL_LOOPS:
349 lapic_cal_t2 = tapic;
350 lapic_cal_tsc2 = tsc;
351 if (pm < lapic_cal_pm1)
352 pm += ACPI_PM_OVRRUN;
354 lapic_cal_j2 = jiffies;
360 * Setup the boot APIC
362 * Calibrate and verify the result.
364 void __init setup_boot_APIC_clock(void)
366 struct clock_event_device *levt = &__get_cpu_var(lapic_events);
367 const long pm_100ms = PMTMR_TICKS_PER_SEC/10;
368 const long pm_thresh = pm_100ms/100;
369 void (*real_handler)(struct clock_event_device *dev);
370 unsigned long deltaj;
372 int pm_referenced = 0;
375 * The local apic timer can be disabled via the kernel
376 * commandline or from the CPU detection code. Register the lapic
377 * timer as a dummy clock event source on SMP systems, so the
378 * broadcast mechanism is used. On UP systems simply ignore it.
380 if (local_apic_timer_disabled) {
381 /* No broadcast on UP ! */
382 if (num_possible_cpus() > 1) {
383 lapic_clockevent.mult = 1;
389 apic_printk(APIC_VERBOSE, "Using local APIC timer interrupts.\n"
390 "calibrating APIC timer ...\n");
394 /* Replace the global interrupt handler */
395 real_handler = global_clock_event->event_handler;
396 global_clock_event->event_handler = lapic_cal_handler;
399 * Setup the APIC counter to 1e9. There is no way the lapic
400 * can underflow in the 100ms detection time frame
402 __setup_APIC_LVTT(1000000000, 0, 0);
404 /* Let the interrupts run */
407 while (lapic_cal_loops <= LAPIC_CAL_LOOPS)
412 /* Restore the real event handler */
413 global_clock_event->event_handler = real_handler;
415 /* Build delta t1-t2 as apic timer counts down */
416 delta = lapic_cal_t1 - lapic_cal_t2;
417 apic_printk(APIC_VERBOSE, "... lapic delta = %ld\n", delta);
419 /* Check, if the PM timer is available */
420 deltapm = lapic_cal_pm2 - lapic_cal_pm1;
421 apic_printk(APIC_VERBOSE, "... PM timer delta = %ld\n", deltapm);
427 mult = clocksource_hz2mult(PMTMR_TICKS_PER_SEC, 22);
429 if (deltapm > (pm_100ms - pm_thresh) &&
430 deltapm < (pm_100ms + pm_thresh)) {
431 apic_printk(APIC_VERBOSE, "... PM timer result ok\n");
433 res = (((u64) deltapm) * mult) >> 22;
434 do_div(res, 1000000);
435 printk(KERN_WARNING "APIC calibration not consistent "
436 "with PM Timer: %ldms instead of 100ms\n",
438 /* Correct the lapic counter value */
439 res = (((u64) delta) * pm_100ms);
440 do_div(res, deltapm);
441 printk(KERN_INFO "APIC delta adjusted to PM-Timer: "
442 "%lu (%ld)\n", (unsigned long) res, delta);
448 /* Calculate the scaled math multiplication factor */
449 lapic_clockevent.mult = div_sc(delta, TICK_NSEC * LAPIC_CAL_LOOPS, 32);
450 lapic_clockevent.max_delta_ns =
451 clockevent_delta2ns(0x7FFFFF, &lapic_clockevent);
452 lapic_clockevent.min_delta_ns =
453 clockevent_delta2ns(0xF, &lapic_clockevent);
455 calibration_result = (delta * APIC_DIVISOR) / LAPIC_CAL_LOOPS;
457 apic_printk(APIC_VERBOSE, "..... delta %ld\n", delta);
458 apic_printk(APIC_VERBOSE, "..... mult: %ld\n", lapic_clockevent.mult);
459 apic_printk(APIC_VERBOSE, "..... calibration result: %u\n",
463 delta = (long)(lapic_cal_tsc2 - lapic_cal_tsc1);
464 apic_printk(APIC_VERBOSE, "..... CPU clock speed is "
466 (delta / LAPIC_CAL_LOOPS) / (1000000 / HZ),
467 (delta / LAPIC_CAL_LOOPS) % (1000000 / HZ));
470 apic_printk(APIC_VERBOSE, "..... host bus clock speed is "
472 calibration_result / (1000000 / HZ),
473 calibration_result % (1000000 / HZ));
475 local_apic_timer_verify_ok = 1;
478 * Do a sanity check on the APIC calibration result
480 if (calibration_result < (1000000 / HZ)) {
483 "APIC frequency too slow, disabling apic timer\n");
484 /* No broadcast on UP ! */
485 if (num_possible_cpus() > 1)
490 /* We trust the pm timer based calibration */
491 if (!pm_referenced) {
492 apic_printk(APIC_VERBOSE, "... verify APIC timer\n");
495 * Setup the apic timer manually
497 levt->event_handler = lapic_cal_handler;
498 lapic_timer_setup(CLOCK_EVT_MODE_PERIODIC, levt);
499 lapic_cal_loops = -1;
501 /* Let the interrupts run */
504 while (lapic_cal_loops <= LAPIC_CAL_LOOPS)
509 /* Stop the lapic timer */
510 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, levt);
515 deltaj = lapic_cal_j2 - lapic_cal_j1;
516 apic_printk(APIC_VERBOSE, "... jiffies delta = %lu\n", deltaj);
518 /* Check, if the jiffies result is consistent */
519 if (deltaj >= LAPIC_CAL_LOOPS-2 && deltaj <= LAPIC_CAL_LOOPS+2)
520 apic_printk(APIC_VERBOSE, "... jiffies result ok\n");
522 local_apic_timer_verify_ok = 0;
526 if (!local_apic_timer_verify_ok) {
528 "APIC timer disabled due to verification failure.\n");
529 /* No broadcast on UP ! */
530 if (num_possible_cpus() == 1)
534 * If nmi_watchdog is set to IO_APIC, we need the
535 * PIT/HPET going. Otherwise register lapic as a dummy
538 if (nmi_watchdog != NMI_IO_APIC)
539 lapic_clockevent.features &= ~CLOCK_EVT_FEAT_DUMMY;
541 printk(KERN_WARNING "APIC timer registered as dummy,"
542 " due to nmi_watchdog=1!\n");
545 /* Setup the lapic or request the broadcast */
549 void __devinit setup_secondary_APIC_clock(void)
555 * The guts of the apic timer interrupt
557 static void local_apic_timer_interrupt(void)
559 int cpu = smp_processor_id();
560 struct clock_event_device *evt = &per_cpu(lapic_events, cpu);
563 * Normally we should not be here till LAPIC has been initialized but
564 * in some cases like kdump, its possible that there is a pending LAPIC
565 * timer interrupt from previous kernel's context and is delivered in
566 * new kernel the moment interrupts are enabled.
568 * Interrupts are enabled early and LAPIC is setup much later, hence
569 * its possible that when we get here evt->event_handler is NULL.
570 * Check for event_handler being NULL and discard the interrupt as
573 if (!evt->event_handler) {
575 "Spurious LAPIC timer interrupt on cpu %d\n", cpu);
577 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, evt);
582 * the NMI deadlock-detector uses this.
584 per_cpu(irq_stat, cpu).apic_timer_irqs++;
586 evt->event_handler(evt);
590 * Local APIC timer interrupt. This is the most natural way for doing
591 * local interrupts, but local timer interrupts can be emulated by
592 * broadcast interrupts too. [in case the hw doesn't support APIC timers]
594 * [ if a single-CPU system runs an SMP kernel then we call the local
595 * interrupt as well. Thus we cannot inline the local irq ... ]
597 void smp_apic_timer_interrupt(struct pt_regs *regs)
599 struct pt_regs *old_regs = set_irq_regs(regs);
602 * NOTE! We'd better ACK the irq immediately,
603 * because timer handling can be slow.
607 * update_process_times() expects us to have done irq_enter().
608 * Besides, if we don't timer interrupts ignore the global
609 * interrupt lock, which is the WrongThing (tm) to do.
612 local_apic_timer_interrupt();
615 set_irq_regs(old_regs);
618 int setup_profiling_timer(unsigned int multiplier)
624 * Local APIC start and shutdown
628 * clear_local_APIC - shutdown the local APIC
630 * This is called, when a CPU is disabled and before rebooting, so the state of
631 * the local APIC has no dangling leftovers. Also used to cleanout any BIOS
632 * leftovers during boot.
634 void clear_local_APIC(void)
639 /* APIC hasn't been mapped yet */
643 maxlvt = lapic_get_maxlvt();
645 * Masking an LVT entry can trigger a local APIC error
646 * if the vector is zero. Mask LVTERR first to prevent this.
649 v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
650 apic_write_around(APIC_LVTERR, v | APIC_LVT_MASKED);
653 * Careful: we have to set masks only first to deassert
654 * any level-triggered sources.
656 v = apic_read(APIC_LVTT);
657 apic_write_around(APIC_LVTT, v | APIC_LVT_MASKED);
658 v = apic_read(APIC_LVT0);
659 apic_write_around(APIC_LVT0, v | APIC_LVT_MASKED);
660 v = apic_read(APIC_LVT1);
661 apic_write_around(APIC_LVT1, v | APIC_LVT_MASKED);
663 v = apic_read(APIC_LVTPC);
664 apic_write_around(APIC_LVTPC, v | APIC_LVT_MASKED);
667 /* lets not touch this if we didn't frob it */
668 #ifdef CONFIG_X86_MCE_P4THERMAL
670 v = apic_read(APIC_LVTTHMR);
671 apic_write_around(APIC_LVTTHMR, v | APIC_LVT_MASKED);
675 * Clean APIC state for other OSs:
677 apic_write_around(APIC_LVTT, APIC_LVT_MASKED);
678 apic_write_around(APIC_LVT0, APIC_LVT_MASKED);
679 apic_write_around(APIC_LVT1, APIC_LVT_MASKED);
681 apic_write_around(APIC_LVTERR, APIC_LVT_MASKED);
683 apic_write_around(APIC_LVTPC, APIC_LVT_MASKED);
685 #ifdef CONFIG_X86_MCE_P4THERMAL
687 apic_write_around(APIC_LVTTHMR, APIC_LVT_MASKED);
689 /* Integrated APIC (!82489DX) ? */
690 if (lapic_is_integrated()) {
692 /* Clear ESR due to Pentium errata 3AP and 11AP */
693 apic_write(APIC_ESR, 0);
699 * disable_local_APIC - clear and disable the local APIC
701 void disable_local_APIC(void)
708 * Disable APIC (implies clearing of registers
711 value = apic_read(APIC_SPIV);
712 value &= ~APIC_SPIV_APIC_ENABLED;
713 apic_write_around(APIC_SPIV, value);
716 * When LAPIC was disabled by the BIOS and enabled by the kernel,
717 * restore the disabled state.
719 if (enabled_via_apicbase) {
722 rdmsr(MSR_IA32_APICBASE, l, h);
723 l &= ~MSR_IA32_APICBASE_ENABLE;
724 wrmsr(MSR_IA32_APICBASE, l, h);
729 * If Linux enabled the LAPIC against the BIOS default disable it down before
730 * re-entering the BIOS on shutdown. Otherwise the BIOS may get confused and
731 * not power-off. Additionally clear all LVT entries before disable_local_APIC
732 * for the case where Linux didn't enable the LAPIC.
734 void lapic_shutdown(void)
741 local_irq_save(flags);
744 if (enabled_via_apicbase)
745 disable_local_APIC();
747 local_irq_restore(flags);
751 * This is to verify that we're looking at a real local APIC.
752 * Check these against your board if the CPUs aren't getting
753 * started for no apparent reason.
755 int __init verify_local_APIC(void)
757 unsigned int reg0, reg1;
760 * The version register is read-only in a real APIC.
762 reg0 = apic_read(APIC_LVR);
763 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
764 apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
765 reg1 = apic_read(APIC_LVR);
766 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
769 * The two version reads above should print the same
770 * numbers. If the second one is different, then we
771 * poke at a non-APIC.
777 * Check if the version looks reasonably.
779 reg1 = GET_APIC_VERSION(reg0);
780 if (reg1 == 0x00 || reg1 == 0xff)
782 reg1 = lapic_get_maxlvt();
783 if (reg1 < 0x02 || reg1 == 0xff)
787 * The ID register is read/write in a real APIC.
789 reg0 = apic_read(APIC_ID);
790 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
793 * The next two are just to see if we have sane values.
794 * They're only really relevant if we're in Virtual Wire
795 * compatibility mode, but most boxes are anymore.
797 reg0 = apic_read(APIC_LVT0);
798 apic_printk(APIC_DEBUG, "Getting LVT0: %x\n", reg0);
799 reg1 = apic_read(APIC_LVT1);
800 apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
806 * sync_Arb_IDs - synchronize APIC bus arbitration IDs
808 void __init sync_Arb_IDs(void)
811 * Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 And not
814 if (modern_apic() || boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
819 apic_wait_icr_idle();
821 apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
822 apic_write_around(APIC_ICR, APIC_DEST_ALLINC | APIC_INT_LEVELTRIG
827 * An initial setup of the virtual wire mode.
829 void __init init_bsp_APIC(void)
834 * Don't do the setup now if we have a SMP BIOS as the
835 * through-I/O-APIC virtual wire mode might be active.
837 if (smp_found_config || !cpu_has_apic)
841 * Do not trust the local APIC being empty at bootup.
848 value = apic_read(APIC_SPIV);
849 value &= ~APIC_VECTOR_MASK;
850 value |= APIC_SPIV_APIC_ENABLED;
852 /* This bit is reserved on P4/Xeon and should be cleared */
853 if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) &&
854 (boot_cpu_data.x86 == 15))
855 value &= ~APIC_SPIV_FOCUS_DISABLED;
857 value |= APIC_SPIV_FOCUS_DISABLED;
858 value |= SPURIOUS_APIC_VECTOR;
859 apic_write_around(APIC_SPIV, value);
862 * Set up the virtual wire mode.
864 apic_write_around(APIC_LVT0, APIC_DM_EXTINT);
866 if (!lapic_is_integrated()) /* 82489DX */
867 value |= APIC_LVT_LEVEL_TRIGGER;
868 apic_write_around(APIC_LVT1, value);
872 * setup_local_APIC - setup the local APIC
874 void __cpuinit setup_local_APIC(void)
876 unsigned long oldvalue, value, maxlvt, integrated;
879 /* Pound the ESR really hard over the head with a big hammer - mbligh */
881 apic_write(APIC_ESR, 0);
882 apic_write(APIC_ESR, 0);
883 apic_write(APIC_ESR, 0);
884 apic_write(APIC_ESR, 0);
887 integrated = lapic_is_integrated();
890 * Double-check whether this APIC is really registered.
892 if (!apic_id_registered())
896 * Intel recommends to set DFR, LDR and TPR before enabling
897 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
898 * document number 292116). So here it goes...
903 * Set Task Priority to 'accept all'. We never change this
906 value = apic_read(APIC_TASKPRI);
907 value &= ~APIC_TPRI_MASK;
908 apic_write_around(APIC_TASKPRI, value);
911 * After a crash, we no longer service the interrupts and a pending
912 * interrupt from previous kernel might still have ISR bit set.
914 * Most probably by now CPU has serviced that pending interrupt and
915 * it might not have done the ack_APIC_irq() because it thought,
916 * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
917 * does not clear the ISR bit and cpu thinks it has already serivced
918 * the interrupt. Hence a vector might get locked. It was noticed
919 * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
921 for (i = APIC_ISR_NR - 1; i >= 0; i--) {
922 value = apic_read(APIC_ISR + i*0x10);
923 for (j = 31; j >= 0; j--) {
930 * Now that we are all set up, enable the APIC
932 value = apic_read(APIC_SPIV);
933 value &= ~APIC_VECTOR_MASK;
937 value |= APIC_SPIV_APIC_ENABLED;
940 * Some unknown Intel IO/APIC (or APIC) errata is biting us with
941 * certain networking cards. If high frequency interrupts are
942 * happening on a particular IOAPIC pin, plus the IOAPIC routing
943 * entry is masked/unmasked at a high rate as well then sooner or
944 * later IOAPIC line gets 'stuck', no more interrupts are received
945 * from the device. If focus CPU is disabled then the hang goes
948 * [ This bug can be reproduced easily with a level-triggered
949 * PCI Ne2000 networking cards and PII/PIII processors, dual
953 * Actually disabling the focus CPU check just makes the hang less
954 * frequent as it makes the interrupt distributon model be more
955 * like LRU than MRU (the short-term load is more even across CPUs).
956 * See also the comment in end_level_ioapic_irq(). --macro
959 /* Enable focus processor (bit==0) */
960 value &= ~APIC_SPIV_FOCUS_DISABLED;
963 * Set spurious IRQ vector
965 value |= SPURIOUS_APIC_VECTOR;
966 apic_write_around(APIC_SPIV, value);
971 * set up through-local-APIC on the BP's LINT0. This is not
972 * strictly necessary in pure symmetric-IO mode, but sometimes
973 * we delegate interrupts to the 8259A.
976 * TODO: set up through-local-APIC from through-I/O-APIC? --macro
978 value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
979 if (!smp_processor_id() && (pic_mode || !value)) {
980 value = APIC_DM_EXTINT;
981 apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n",
984 value = APIC_DM_EXTINT | APIC_LVT_MASKED;
985 apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n",
988 apic_write_around(APIC_LVT0, value);
991 * only the BP should see the LINT1 NMI signal, obviously.
993 if (!smp_processor_id())
996 value = APIC_DM_NMI | APIC_LVT_MASKED;
997 if (!integrated) /* 82489DX */
998 value |= APIC_LVT_LEVEL_TRIGGER;
999 apic_write_around(APIC_LVT1, value);
1001 if (integrated && !esr_disable) {
1003 maxlvt = lapic_get_maxlvt();
1004 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
1005 apic_write(APIC_ESR, 0);
1006 oldvalue = apic_read(APIC_ESR);
1008 /* enables sending errors */
1009 value = ERROR_APIC_VECTOR;
1010 apic_write_around(APIC_LVTERR, value);
1012 * spec says clear errors after enabling vector.
1015 apic_write(APIC_ESR, 0);
1016 value = apic_read(APIC_ESR);
1017 if (value != oldvalue)
1018 apic_printk(APIC_VERBOSE, "ESR value before enabling "
1019 "vector: 0x%08lx after: 0x%08lx\n",
1024 * Something untraceable is creating bad interrupts on
1025 * secondary quads ... for the moment, just leave the
1026 * ESR disabled - we can't do anything useful with the
1027 * errors anyway - mbligh
1029 printk(KERN_INFO "Leaving ESR disabled.\n");
1031 printk(KERN_INFO "No ESR for 82489DX.\n");
1034 /* Disable the local apic timer */
1035 value = apic_read(APIC_LVTT);
1036 value |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
1037 apic_write_around(APIC_LVTT, value);
1039 setup_apic_nmi_watchdog(NULL);
1044 * Detect and initialize APIC
1046 static int __init detect_init_APIC(void)
1050 /* Disabled by kernel option? */
1051 if (enable_local_apic < 0)
1054 switch (boot_cpu_data.x86_vendor) {
1055 case X86_VENDOR_AMD:
1056 if ((boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model > 1) ||
1057 (boot_cpu_data.x86 == 15))
1060 case X86_VENDOR_INTEL:
1061 if (boot_cpu_data.x86 == 6 || boot_cpu_data.x86 == 15 ||
1062 (boot_cpu_data.x86 == 5 && cpu_has_apic))
1069 if (!cpu_has_apic) {
1071 * Over-ride BIOS and try to enable the local APIC only if
1072 * "lapic" specified.
1074 if (enable_local_apic <= 0) {
1075 printk(KERN_INFO "Local APIC disabled by BIOS -- "
1076 "you can enable it with \"lapic\"\n");
1080 * Some BIOSes disable the local APIC in the APIC_BASE
1081 * MSR. This can only be done in software for Intel P6 or later
1082 * and AMD K7 (Model > 1) or later.
1084 rdmsr(MSR_IA32_APICBASE, l, h);
1085 if (!(l & MSR_IA32_APICBASE_ENABLE)) {
1087 "Local APIC disabled by BIOS -- reenabling.\n");
1088 l &= ~MSR_IA32_APICBASE_BASE;
1089 l |= MSR_IA32_APICBASE_ENABLE | APIC_DEFAULT_PHYS_BASE;
1090 wrmsr(MSR_IA32_APICBASE, l, h);
1091 enabled_via_apicbase = 1;
1095 * The APIC feature bit should now be enabled
1098 features = cpuid_edx(1);
1099 if (!(features & (1 << X86_FEATURE_APIC))) {
1100 printk(KERN_WARNING "Could not enable APIC!\n");
1103 set_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
1104 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
1106 /* The BIOS may have set up the APIC at some other address */
1107 rdmsr(MSR_IA32_APICBASE, l, h);
1108 if (l & MSR_IA32_APICBASE_ENABLE)
1109 mp_lapic_addr = l & MSR_IA32_APICBASE_BASE;
1111 if (nmi_watchdog != NMI_NONE && nmi_watchdog != NMI_DISABLED)
1112 nmi_watchdog = NMI_LOCAL_APIC;
1114 printk(KERN_INFO "Found and enabled local APIC!\n");
1121 printk(KERN_INFO "No local APIC present or hardware disabled\n");
1126 * init_apic_mappings - initialize APIC mappings
1128 void __init init_apic_mappings(void)
1131 * If no local APIC can be found then set up a fake all
1132 * zeroes page to simulate the local APIC and another
1133 * one for the IO-APIC.
1135 if (!smp_found_config && detect_init_APIC()) {
1136 apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
1137 apic_phys = __pa(apic_phys);
1139 apic_phys = mp_lapic_addr;
1141 set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
1142 printk(KERN_DEBUG "mapped APIC to %08lx (%08lx)\n", APIC_BASE,
1146 * Fetch the APIC ID of the BSP in case we have a
1147 * default configuration (or the MP table is broken).
1149 if (boot_cpu_physical_apicid == -1U)
1150 boot_cpu_physical_apicid = GET_APIC_ID(apic_read(APIC_ID));
1152 #ifdef CONFIG_X86_IO_APIC
1154 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
1157 for (i = 0; i < nr_ioapics; i++) {
1158 if (smp_found_config) {
1159 ioapic_phys = mp_ioapics[i].mpc_apicaddr;
1162 "WARNING: bogus zero IO-APIC "
1163 "address found in MPTABLE, "
1164 "disabling IO/APIC support!\n");
1165 smp_found_config = 0;
1166 skip_ioapic_setup = 1;
1167 goto fake_ioapic_page;
1171 ioapic_phys = (unsigned long)
1172 alloc_bootmem_pages(PAGE_SIZE);
1173 ioapic_phys = __pa(ioapic_phys);
1175 set_fixmap_nocache(idx, ioapic_phys);
1176 printk(KERN_DEBUG "mapped IOAPIC to %08lx (%08lx)\n",
1177 __fix_to_virt(idx), ioapic_phys);
1185 * This initializes the IO-APIC and APIC hardware if this is
1188 int __init APIC_init_uniprocessor(void)
1190 if (enable_local_apic < 0)
1191 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
1193 if (!smp_found_config && !cpu_has_apic)
1197 * Complain if the BIOS pretends there is one.
1199 if (!cpu_has_apic &&
1200 APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid])) {
1201 printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
1202 boot_cpu_physical_apicid);
1203 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
1207 verify_local_APIC();
1212 * Hack: In case of kdump, after a crash, kernel might be booting
1213 * on a cpu with non-zero lapic id. But boot_cpu_physical_apicid
1214 * might be zero if read from MP tables. Get it from LAPIC.
1216 #ifdef CONFIG_CRASH_DUMP
1217 boot_cpu_physical_apicid = GET_APIC_ID(apic_read(APIC_ID));
1219 phys_cpu_present_map = physid_mask_of_physid(boot_cpu_physical_apicid);
1223 #ifdef CONFIG_X86_IO_APIC
1224 if (smp_found_config)
1225 if (!skip_ioapic_setup && nr_ioapics)
1234 * Local APIC interrupts
1238 * This interrupt should _never_ happen with our APIC/SMP architecture
1240 void smp_spurious_interrupt(struct pt_regs *regs)
1246 * Check if this really is a spurious interrupt and ACK it
1247 * if it is a vectored one. Just in case...
1248 * Spurious interrupts should not be ACKed.
1250 v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
1251 if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
1254 /* see sw-dev-man vol 3, chapter 7.4.13.5 */
1255 printk(KERN_INFO "spurious APIC interrupt on CPU#%d, "
1256 "should never happen.\n", smp_processor_id());
1257 __get_cpu_var(irq_stat).irq_spurious_count++;
1262 * This interrupt should never happen with our APIC/SMP architecture
1264 void smp_error_interrupt(struct pt_regs *regs)
1266 unsigned long v, v1;
1269 /* First tickle the hardware, only then report what went on. -- REW */
1270 v = apic_read(APIC_ESR);
1271 apic_write(APIC_ESR, 0);
1272 v1 = apic_read(APIC_ESR);
1274 atomic_inc(&irq_err_count);
1276 /* Here is what the APIC error bits mean:
1279 2: Send accept error
1280 3: Receive accept error
1282 5: Send illegal vector
1283 6: Received illegal vector
1284 7: Illegal register address
1286 printk(KERN_DEBUG "APIC error on CPU%d: %02lx(%02lx)\n",
1287 smp_processor_id(), v , v1);
1292 * Initialize APIC interrupts
1294 void __init apic_intr_init(void)
1299 /* self generated IPI for local APIC timer */
1300 set_intr_gate(LOCAL_TIMER_VECTOR, apic_timer_interrupt);
1302 /* IPI vectors for APIC spurious and error interrupts */
1303 set_intr_gate(SPURIOUS_APIC_VECTOR, spurious_interrupt);
1304 set_intr_gate(ERROR_APIC_VECTOR, error_interrupt);
1306 /* thermal monitor LVT interrupt */
1307 #ifdef CONFIG_X86_MCE_P4THERMAL
1308 set_intr_gate(THERMAL_APIC_VECTOR, thermal_interrupt);
1313 * connect_bsp_APIC - attach the APIC to the interrupt system
1315 void __init connect_bsp_APIC(void)
1319 * Do not trust the local APIC being empty at bootup.
1323 * PIC mode, enable APIC mode in the IMCR, i.e. connect BSP's
1324 * local APIC to INT and NMI lines.
1326 apic_printk(APIC_VERBOSE, "leaving PIC mode, "
1327 "enabling APIC mode.\n");
1335 * disconnect_bsp_APIC - detach the APIC from the interrupt system
1336 * @virt_wire_setup: indicates, whether virtual wire mode is selected
1338 * Virtual wire mode is necessary to deliver legacy interrupts even when the
1341 void disconnect_bsp_APIC(int virt_wire_setup)
1345 * Put the board back into PIC mode (has an effect only on
1346 * certain older boards). Note that APIC interrupts, including
1347 * IPIs, won't work beyond this point! The only exception are
1350 apic_printk(APIC_VERBOSE, "disabling APIC mode, "
1351 "entering PIC mode.\n");
1355 /* Go back to Virtual Wire compatibility mode */
1356 unsigned long value;
1358 /* For the spurious interrupt use vector F, and enable it */
1359 value = apic_read(APIC_SPIV);
1360 value &= ~APIC_VECTOR_MASK;
1361 value |= APIC_SPIV_APIC_ENABLED;
1363 apic_write_around(APIC_SPIV, value);
1365 if (!virt_wire_setup) {
1367 * For LVT0 make it edge triggered, active high,
1368 * external and enabled
1370 value = apic_read(APIC_LVT0);
1371 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
1372 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
1373 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
1374 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1375 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
1376 apic_write_around(APIC_LVT0, value);
1379 apic_write_around(APIC_LVT0, APIC_LVT_MASKED);
1383 * For LVT1 make it edge triggered, active high, nmi and
1386 value = apic_read(APIC_LVT1);
1388 APIC_MODE_MASK | APIC_SEND_PENDING |
1389 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
1390 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
1391 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1392 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
1393 apic_write_around(APIC_LVT1, value);
1404 /* r/w apic fields */
1405 unsigned int apic_id;
1406 unsigned int apic_taskpri;
1407 unsigned int apic_ldr;
1408 unsigned int apic_dfr;
1409 unsigned int apic_spiv;
1410 unsigned int apic_lvtt;
1411 unsigned int apic_lvtpc;
1412 unsigned int apic_lvt0;
1413 unsigned int apic_lvt1;
1414 unsigned int apic_lvterr;
1415 unsigned int apic_tmict;
1416 unsigned int apic_tdcr;
1417 unsigned int apic_thmr;
1420 static int lapic_suspend(struct sys_device *dev, pm_message_t state)
1422 unsigned long flags;
1425 if (!apic_pm_state.active)
1428 maxlvt = lapic_get_maxlvt();
1430 apic_pm_state.apic_id = apic_read(APIC_ID);
1431 apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
1432 apic_pm_state.apic_ldr = apic_read(APIC_LDR);
1433 apic_pm_state.apic_dfr = apic_read(APIC_DFR);
1434 apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
1435 apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
1437 apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
1438 apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
1439 apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
1440 apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
1441 apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
1442 apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
1443 #ifdef CONFIG_X86_MCE_P4THERMAL
1445 apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
1448 local_irq_save(flags);
1449 disable_local_APIC();
1450 local_irq_restore(flags);
1454 static int lapic_resume(struct sys_device *dev)
1457 unsigned long flags;
1460 if (!apic_pm_state.active)
1463 maxlvt = lapic_get_maxlvt();
1465 local_irq_save(flags);
1468 * Make sure the APICBASE points to the right address
1470 * FIXME! This will be wrong if we ever support suspend on
1471 * SMP! We'll need to do this as part of the CPU restore!
1473 rdmsr(MSR_IA32_APICBASE, l, h);
1474 l &= ~MSR_IA32_APICBASE_BASE;
1475 l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
1476 wrmsr(MSR_IA32_APICBASE, l, h);
1478 apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
1479 apic_write(APIC_ID, apic_pm_state.apic_id);
1480 apic_write(APIC_DFR, apic_pm_state.apic_dfr);
1481 apic_write(APIC_LDR, apic_pm_state.apic_ldr);
1482 apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
1483 apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
1484 apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
1485 apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
1486 #ifdef CONFIG_X86_MCE_P4THERMAL
1488 apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
1491 apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
1492 apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
1493 apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
1494 apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
1495 apic_write(APIC_ESR, 0);
1496 apic_read(APIC_ESR);
1497 apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
1498 apic_write(APIC_ESR, 0);
1499 apic_read(APIC_ESR);
1500 local_irq_restore(flags);
1505 * This device has no shutdown method - fully functioning local APICs
1506 * are needed on every CPU up until machine_halt/restart/poweroff.
1509 static struct sysdev_class lapic_sysclass = {
1511 .resume = lapic_resume,
1512 .suspend = lapic_suspend,
1515 static struct sys_device device_lapic = {
1517 .cls = &lapic_sysclass,
1520 static void __devinit apic_pm_activate(void)
1522 apic_pm_state.active = 1;
1525 static int __init init_lapic_sysfs(void)
1531 /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
1533 error = sysdev_class_register(&lapic_sysclass);
1535 error = sysdev_register(&device_lapic);
1538 device_initcall(init_lapic_sysfs);
1540 #else /* CONFIG_PM */
1542 static void apic_pm_activate(void) { }
1544 #endif /* CONFIG_PM */
1547 * APIC command line parameters
1549 static int __init parse_lapic(char *arg)
1551 enable_local_apic = 1;
1554 early_param("lapic", parse_lapic);
1556 static int __init parse_nolapic(char *arg)
1558 enable_local_apic = -1;
1559 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
1562 early_param("nolapic", parse_nolapic);
1564 static int __init parse_disable_lapic_timer(char *arg)
1566 local_apic_timer_disabled = 1;
1569 early_param("nolapic_timer", parse_disable_lapic_timer);
1571 static int __init parse_lapic_timer_c2_ok(char *arg)
1573 local_apic_timer_c2_ok = 1;
1576 early_param("lapic_timer_c2_ok", parse_lapic_timer_c2_ok);
1578 static int __init apic_set_verbosity(char *str)
1580 if (strcmp("debug", str) == 0)
1581 apic_verbosity = APIC_DEBUG;
1582 else if (strcmp("verbose", str) == 0)
1583 apic_verbosity = APIC_VERBOSE;
1586 __setup("apic=", apic_set_verbosity);