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/clockchips.h>
29 #include <linux/acpi_pmtmr.h>
30 #include <linux/module.h>
31 #include <linux/dmi.h>
33 #include <asm/atomic.h>
36 #include <asm/mpspec.h>
38 #include <asm/arch_hooks.h>
40 #include <asm/i8253.h>
43 #include <mach_apic.h>
44 #include <mach_apicdef.h>
52 #if (SPURIOUS_APIC_VECTOR & 0x0F) != 0x0F
53 # error SPURIOUS_APIC_VECTOR definition error
57 * Knob to control our willingness to enable the local APIC.
59 * -1=force-disable, +1=force-enable
61 static int enable_local_apic __initdata = 0;
63 /* Local APIC timer verification ok */
64 static int local_apic_timer_verify_ok;
65 /* Disable local APIC timer from the kernel commandline or via dmi quirk */
66 static int local_apic_timer_disabled;
67 /* Local APIC timer works in C2 */
68 int local_apic_timer_c2_ok;
69 EXPORT_SYMBOL_GPL(local_apic_timer_c2_ok);
72 * Debug level, exported for io_apic.c
76 static unsigned int calibration_result;
78 static int lapic_next_event(unsigned long delta,
79 struct clock_event_device *evt);
80 static void lapic_timer_setup(enum clock_event_mode mode,
81 struct clock_event_device *evt);
82 static void lapic_timer_broadcast(cpumask_t mask);
83 static void apic_pm_activate(void);
86 * The local apic timer can be used for any function which is CPU local.
88 static struct clock_event_device lapic_clockevent = {
90 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT
91 | CLOCK_EVT_FEAT_C3STOP | CLOCK_EVT_FEAT_DUMMY,
93 .set_mode = lapic_timer_setup,
94 .set_next_event = lapic_next_event,
95 .broadcast = lapic_timer_broadcast,
99 static DEFINE_PER_CPU(struct clock_event_device, lapic_events);
101 /* Local APIC was disabled by the BIOS and enabled by the kernel */
102 static int enabled_via_apicbase;
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 seperate 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 unsigned long safe_apic_wait_icr_idle(void)
140 unsigned long send_status;
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 enable_NMI_through_LVT0 (void * dummy)
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);
269 local_irq_restore(flags);
273 * Local APIC timer broadcast function
275 static void lapic_timer_broadcast(cpumask_t mask)
278 send_IPI_mask(mask, LOCAL_TIMER_VECTOR);
283 * Setup the local APIC timer for this CPU. Copy the initilized values
284 * of the boot CPU and register the clock event in the framework.
286 static void __devinit setup_APIC_timer(void)
288 struct clock_event_device *levt = &__get_cpu_var(lapic_events);
290 memcpy(levt, &lapic_clockevent, sizeof(*levt));
291 levt->cpumask = cpumask_of_cpu(smp_processor_id());
293 clockevents_register_device(levt);
297 * In this functions we calibrate APIC bus clocks to the external timer.
299 * We want to do the calibration only once since we want to have local timer
300 * irqs syncron. CPUs connected by the same APIC bus have the very same bus
303 * This was previously done by reading the PIT/HPET and waiting for a wrap
304 * around to find out, that a tick has elapsed. I have a box, where the PIT
305 * readout is broken, so it never gets out of the wait loop again. This was
306 * also reported by others.
308 * Monitoring the jiffies value is inaccurate and the clockevents
309 * infrastructure allows us to do a simple substitution of the interrupt
312 * The calibration routine also uses the pm_timer when possible, as the PIT
313 * happens to run way too slow (factor 2.3 on my VAIO CoreDuo, which goes
314 * back to normal later in the boot process).
317 #define LAPIC_CAL_LOOPS (HZ/10)
319 static __initdata volatile int lapic_cal_loops = -1;
320 static __initdata long lapic_cal_t1, lapic_cal_t2;
321 static __initdata unsigned long long lapic_cal_tsc1, lapic_cal_tsc2;
322 static __initdata unsigned long lapic_cal_pm1, lapic_cal_pm2;
323 static __initdata unsigned long lapic_cal_j1, lapic_cal_j2;
326 * Temporary interrupt handler.
328 static void __init lapic_cal_handler(struct clock_event_device *dev)
330 unsigned long long tsc = 0;
331 long tapic = apic_read(APIC_TMCCT);
332 unsigned long pm = acpi_pm_read_early();
337 switch (lapic_cal_loops++) {
339 lapic_cal_t1 = tapic;
340 lapic_cal_tsc1 = tsc;
342 lapic_cal_j1 = jiffies;
345 case LAPIC_CAL_LOOPS:
346 lapic_cal_t2 = tapic;
347 lapic_cal_tsc2 = tsc;
348 if (pm < lapic_cal_pm1)
349 pm += ACPI_PM_OVRRUN;
351 lapic_cal_j2 = jiffies;
357 * Setup the boot APIC
359 * Calibrate and verify the result.
361 void __init setup_boot_APIC_clock(void)
363 struct clock_event_device *levt = &__get_cpu_var(lapic_events);
364 const long pm_100ms = PMTMR_TICKS_PER_SEC/10;
365 const long pm_thresh = pm_100ms/100;
366 void (*real_handler)(struct clock_event_device *dev);
367 unsigned long deltaj;
369 int pm_referenced = 0;
371 if (boot_cpu_has(X86_FEATURE_LAPIC_TIMER_BROKEN))
372 local_apic_timer_disabled = 1;
375 * The local apic timer can be disabled via the kernel
376 * commandline or from the test above. 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)
387 apic_printk(APIC_VERBOSE, "Using local APIC timer interrupts.\n"
388 "calibrating APIC timer ...\n");
392 /* Replace the global interrupt handler */
393 real_handler = global_clock_event->event_handler;
394 global_clock_event->event_handler = lapic_cal_handler;
397 * Setup the APIC counter to 1e9. There is no way the lapic
398 * can underflow in the 100ms detection time frame
400 __setup_APIC_LVTT(1000000000, 0, 0);
402 /* Let the interrupts run */
405 while (lapic_cal_loops <= LAPIC_CAL_LOOPS)
410 /* Restore the real event handler */
411 global_clock_event->event_handler = real_handler;
413 /* Build delta t1-t2 as apic timer counts down */
414 delta = lapic_cal_t1 - lapic_cal_t2;
415 apic_printk(APIC_VERBOSE, "... lapic delta = %ld\n", delta);
417 /* Check, if the PM timer is available */
418 deltapm = lapic_cal_pm2 - lapic_cal_pm1;
419 apic_printk(APIC_VERBOSE, "... PM timer delta = %ld\n", deltapm);
425 mult = clocksource_hz2mult(PMTMR_TICKS_PER_SEC, 22);
427 if (deltapm > (pm_100ms - pm_thresh) &&
428 deltapm < (pm_100ms + pm_thresh)) {
429 apic_printk(APIC_VERBOSE, "... PM timer result ok\n");
431 res = (((u64) deltapm) * mult) >> 22;
432 do_div(res, 1000000);
433 printk(KERN_WARNING "APIC calibration not consistent "
434 "with PM Timer: %ldms instead of 100ms\n",
436 /* Correct the lapic counter value */
437 res = (((u64) delta ) * pm_100ms);
438 do_div(res, deltapm);
439 printk(KERN_INFO "APIC delta adjusted to PM-Timer: "
440 "%lu (%ld)\n", (unsigned long) res, delta);
446 /* Calculate the scaled math multiplication factor */
447 lapic_clockevent.mult = div_sc(delta, TICK_NSEC * LAPIC_CAL_LOOPS, 32);
448 lapic_clockevent.max_delta_ns =
449 clockevent_delta2ns(0x7FFFFF, &lapic_clockevent);
450 lapic_clockevent.min_delta_ns =
451 clockevent_delta2ns(0xF, &lapic_clockevent);
453 calibration_result = (delta * APIC_DIVISOR) / LAPIC_CAL_LOOPS;
455 apic_printk(APIC_VERBOSE, "..... delta %ld\n", delta);
456 apic_printk(APIC_VERBOSE, "..... mult: %ld\n", lapic_clockevent.mult);
457 apic_printk(APIC_VERBOSE, "..... calibration result: %u\n",
461 delta = (long)(lapic_cal_tsc2 - lapic_cal_tsc1);
462 apic_printk(APIC_VERBOSE, "..... CPU clock speed is "
464 (delta / LAPIC_CAL_LOOPS) / (1000000 / HZ),
465 (delta / LAPIC_CAL_LOOPS) % (1000000 / HZ));
468 apic_printk(APIC_VERBOSE, "..... host bus clock speed is "
470 calibration_result / (1000000 / HZ),
471 calibration_result % (1000000 / HZ));
473 local_apic_timer_verify_ok = 1;
475 /* We trust the pm timer based calibration */
476 if (!pm_referenced) {
477 apic_printk(APIC_VERBOSE, "... verify APIC timer\n");
480 * Setup the apic timer manually
482 levt->event_handler = lapic_cal_handler;
483 lapic_timer_setup(CLOCK_EVT_MODE_PERIODIC, levt);
484 lapic_cal_loops = -1;
486 /* Let the interrupts run */
489 while(lapic_cal_loops <= LAPIC_CAL_LOOPS)
494 /* Stop the lapic timer */
495 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, levt);
500 deltaj = lapic_cal_j2 - lapic_cal_j1;
501 apic_printk(APIC_VERBOSE, "... jiffies delta = %lu\n", deltaj);
503 /* Check, if the jiffies result is consistent */
504 if (deltaj >= LAPIC_CAL_LOOPS-2 && deltaj <= LAPIC_CAL_LOOPS+2)
505 apic_printk(APIC_VERBOSE, "... jiffies result ok\n");
507 local_apic_timer_verify_ok = 0;
511 if (!local_apic_timer_verify_ok) {
513 "APIC timer disabled due to verification failure.\n");
514 /* No broadcast on UP ! */
515 if (num_possible_cpus() == 1)
519 * If nmi_watchdog is set to IO_APIC, we need the
520 * PIT/HPET going. Otherwise register lapic as a dummy
523 if (nmi_watchdog != NMI_IO_APIC)
524 lapic_clockevent.features &= ~CLOCK_EVT_FEAT_DUMMY;
527 /* Setup the lapic or request the broadcast */
531 void __devinit setup_secondary_APIC_clock(void)
537 * The guts of the apic timer interrupt
539 static void local_apic_timer_interrupt(void)
541 int cpu = smp_processor_id();
542 struct clock_event_device *evt = &per_cpu(lapic_events, cpu);
545 * Normally we should not be here till LAPIC has been initialized but
546 * in some cases like kdump, its possible that there is a pending LAPIC
547 * timer interrupt from previous kernel's context and is delivered in
548 * new kernel the moment interrupts are enabled.
550 * Interrupts are enabled early and LAPIC is setup much later, hence
551 * its possible that when we get here evt->event_handler is NULL.
552 * Check for event_handler being NULL and discard the interrupt as
555 if (!evt->event_handler) {
557 "Spurious LAPIC timer interrupt on cpu %d\n", cpu);
559 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, evt);
563 per_cpu(irq_stat, cpu).apic_timer_irqs++;
565 evt->event_handler(evt);
569 * Local APIC timer interrupt. This is the most natural way for doing
570 * local interrupts, but local timer interrupts can be emulated by
571 * broadcast interrupts too. [in case the hw doesn't support APIC timers]
573 * [ if a single-CPU system runs an SMP kernel then we call the local
574 * interrupt as well. Thus we cannot inline the local irq ... ]
577 void fastcall smp_apic_timer_interrupt(struct pt_regs *regs)
579 struct pt_regs *old_regs = set_irq_regs(regs);
582 * NOTE! We'd better ACK the irq immediately,
583 * because timer handling can be slow.
587 * update_process_times() expects us to have done irq_enter().
588 * Besides, if we don't timer interrupts ignore the global
589 * interrupt lock, which is the WrongThing (tm) to do.
592 local_apic_timer_interrupt();
595 set_irq_regs(old_regs);
598 int setup_profiling_timer(unsigned int multiplier)
604 * Local APIC start and shutdown
608 * clear_local_APIC - shutdown the local APIC
610 * This is called, when a CPU is disabled and before rebooting, so the state of
611 * the local APIC has no dangling leftovers. Also used to cleanout any BIOS
612 * leftovers during boot.
614 void clear_local_APIC(void)
616 int maxlvt = lapic_get_maxlvt();
620 * Masking an LVT entry can trigger a local APIC error
621 * if the vector is zero. Mask LVTERR first to prevent this.
624 v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
625 apic_write_around(APIC_LVTERR, v | APIC_LVT_MASKED);
628 * Careful: we have to set masks only first to deassert
629 * any level-triggered sources.
631 v = apic_read(APIC_LVTT);
632 apic_write_around(APIC_LVTT, v | APIC_LVT_MASKED);
633 v = apic_read(APIC_LVT0);
634 apic_write_around(APIC_LVT0, v | APIC_LVT_MASKED);
635 v = apic_read(APIC_LVT1);
636 apic_write_around(APIC_LVT1, v | APIC_LVT_MASKED);
638 v = apic_read(APIC_LVTPC);
639 apic_write_around(APIC_LVTPC, v | APIC_LVT_MASKED);
642 /* lets not touch this if we didn't frob it */
643 #ifdef CONFIG_X86_MCE_P4THERMAL
645 v = apic_read(APIC_LVTTHMR);
646 apic_write_around(APIC_LVTTHMR, v | APIC_LVT_MASKED);
650 * Clean APIC state for other OSs:
652 apic_write_around(APIC_LVTT, APIC_LVT_MASKED);
653 apic_write_around(APIC_LVT0, APIC_LVT_MASKED);
654 apic_write_around(APIC_LVT1, APIC_LVT_MASKED);
656 apic_write_around(APIC_LVTERR, APIC_LVT_MASKED);
658 apic_write_around(APIC_LVTPC, APIC_LVT_MASKED);
660 #ifdef CONFIG_X86_MCE_P4THERMAL
662 apic_write_around(APIC_LVTTHMR, APIC_LVT_MASKED);
664 /* Integrated APIC (!82489DX) ? */
665 if (lapic_is_integrated()) {
667 /* Clear ESR due to Pentium errata 3AP and 11AP */
668 apic_write(APIC_ESR, 0);
674 * disable_local_APIC - clear and disable the local APIC
676 void disable_local_APIC(void)
683 * Disable APIC (implies clearing of registers
686 value = apic_read(APIC_SPIV);
687 value &= ~APIC_SPIV_APIC_ENABLED;
688 apic_write_around(APIC_SPIV, value);
691 * When LAPIC was disabled by the BIOS and enabled by the kernel,
692 * restore the disabled state.
694 if (enabled_via_apicbase) {
697 rdmsr(MSR_IA32_APICBASE, l, h);
698 l &= ~MSR_IA32_APICBASE_ENABLE;
699 wrmsr(MSR_IA32_APICBASE, l, h);
704 * If Linux enabled the LAPIC against the BIOS default disable it down before
705 * re-entering the BIOS on shutdown. Otherwise the BIOS may get confused and
706 * not power-off. Additionally clear all LVT entries before disable_local_APIC
707 * for the case where Linux didn't enable the LAPIC.
709 void lapic_shutdown(void)
716 local_irq_save(flags);
719 if (enabled_via_apicbase)
720 disable_local_APIC();
722 local_irq_restore(flags);
726 * This is to verify that we're looking at a real local APIC.
727 * Check these against your board if the CPUs aren't getting
728 * started for no apparent reason.
730 int __init verify_local_APIC(void)
732 unsigned int reg0, reg1;
735 * The version register is read-only in a real APIC.
737 reg0 = apic_read(APIC_LVR);
738 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
739 apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
740 reg1 = apic_read(APIC_LVR);
741 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
744 * The two version reads above should print the same
745 * numbers. If the second one is different, then we
746 * poke at a non-APIC.
752 * Check if the version looks reasonably.
754 reg1 = GET_APIC_VERSION(reg0);
755 if (reg1 == 0x00 || reg1 == 0xff)
757 reg1 = lapic_get_maxlvt();
758 if (reg1 < 0x02 || reg1 == 0xff)
762 * The ID register is read/write in a real APIC.
764 reg0 = apic_read(APIC_ID);
765 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
768 * The next two are just to see if we have sane values.
769 * They're only really relevant if we're in Virtual Wire
770 * compatibility mode, but most boxes are anymore.
772 reg0 = apic_read(APIC_LVT0);
773 apic_printk(APIC_DEBUG, "Getting LVT0: %x\n", reg0);
774 reg1 = apic_read(APIC_LVT1);
775 apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
781 * sync_Arb_IDs - synchronize APIC bus arbitration IDs
783 void __init sync_Arb_IDs(void)
786 * Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 And not
794 apic_wait_icr_idle();
796 apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
797 apic_write_around(APIC_ICR, APIC_DEST_ALLINC | APIC_INT_LEVELTRIG
802 * An initial setup of the virtual wire mode.
804 void __init init_bsp_APIC(void)
809 * Don't do the setup now if we have a SMP BIOS as the
810 * through-I/O-APIC virtual wire mode might be active.
812 if (smp_found_config || !cpu_has_apic)
816 * Do not trust the local APIC being empty at bootup.
823 value = apic_read(APIC_SPIV);
824 value &= ~APIC_VECTOR_MASK;
825 value |= APIC_SPIV_APIC_ENABLED;
827 /* This bit is reserved on P4/Xeon and should be cleared */
828 if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) &&
829 (boot_cpu_data.x86 == 15))
830 value &= ~APIC_SPIV_FOCUS_DISABLED;
832 value |= APIC_SPIV_FOCUS_DISABLED;
833 value |= SPURIOUS_APIC_VECTOR;
834 apic_write_around(APIC_SPIV, value);
837 * Set up the virtual wire mode.
839 apic_write_around(APIC_LVT0, APIC_DM_EXTINT);
841 if (!lapic_is_integrated()) /* 82489DX */
842 value |= APIC_LVT_LEVEL_TRIGGER;
843 apic_write_around(APIC_LVT1, value);
847 * setup_local_APIC - setup the local APIC
849 void __devinit setup_local_APIC(void)
851 unsigned long oldvalue, value, maxlvt, integrated;
854 /* Pound the ESR really hard over the head with a big hammer - mbligh */
856 apic_write(APIC_ESR, 0);
857 apic_write(APIC_ESR, 0);
858 apic_write(APIC_ESR, 0);
859 apic_write(APIC_ESR, 0);
862 integrated = lapic_is_integrated();
865 * Double-check whether this APIC is really registered.
867 if (!apic_id_registered())
871 * Intel recommends to set DFR, LDR and TPR before enabling
872 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
873 * document number 292116). So here it goes...
878 * Set Task Priority to 'accept all'. We never change this
881 value = apic_read(APIC_TASKPRI);
882 value &= ~APIC_TPRI_MASK;
883 apic_write_around(APIC_TASKPRI, value);
886 * After a crash, we no longer service the interrupts and a pending
887 * interrupt from previous kernel might still have ISR bit set.
889 * Most probably by now CPU has serviced that pending interrupt and
890 * it might not have done the ack_APIC_irq() because it thought,
891 * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
892 * does not clear the ISR bit and cpu thinks it has already serivced
893 * the interrupt. Hence a vector might get locked. It was noticed
894 * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
896 for (i = APIC_ISR_NR - 1; i >= 0; i--) {
897 value = apic_read(APIC_ISR + i*0x10);
898 for (j = 31; j >= 0; j--) {
905 * Now that we are all set up, enable the APIC
907 value = apic_read(APIC_SPIV);
908 value &= ~APIC_VECTOR_MASK;
912 value |= APIC_SPIV_APIC_ENABLED;
915 * Some unknown Intel IO/APIC (or APIC) errata is biting us with
916 * certain networking cards. If high frequency interrupts are
917 * happening on a particular IOAPIC pin, plus the IOAPIC routing
918 * entry is masked/unmasked at a high rate as well then sooner or
919 * later IOAPIC line gets 'stuck', no more interrupts are received
920 * from the device. If focus CPU is disabled then the hang goes
923 * [ This bug can be reproduced easily with a level-triggered
924 * PCI Ne2000 networking cards and PII/PIII processors, dual
928 * Actually disabling the focus CPU check just makes the hang less
929 * frequent as it makes the interrupt distributon model be more
930 * like LRU than MRU (the short-term load is more even across CPUs).
931 * See also the comment in end_level_ioapic_irq(). --macro
934 /* Enable focus processor (bit==0) */
935 value &= ~APIC_SPIV_FOCUS_DISABLED;
938 * Set spurious IRQ vector
940 value |= SPURIOUS_APIC_VECTOR;
941 apic_write_around(APIC_SPIV, value);
946 * set up through-local-APIC on the BP's LINT0. This is not
947 * strictly necessery in pure symmetric-IO mode, but sometimes
948 * we delegate interrupts to the 8259A.
951 * TODO: set up through-local-APIC from through-I/O-APIC? --macro
953 value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
954 if (!smp_processor_id() && (pic_mode || !value)) {
955 value = APIC_DM_EXTINT;
956 apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n",
959 value = APIC_DM_EXTINT | APIC_LVT_MASKED;
960 apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n",
963 apic_write_around(APIC_LVT0, value);
966 * only the BP should see the LINT1 NMI signal, obviously.
968 if (!smp_processor_id())
971 value = APIC_DM_NMI | APIC_LVT_MASKED;
972 if (!integrated) /* 82489DX */
973 value |= APIC_LVT_LEVEL_TRIGGER;
974 apic_write_around(APIC_LVT1, value);
976 if (integrated && !esr_disable) { /* !82489DX */
977 maxlvt = lapic_get_maxlvt();
978 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
979 apic_write(APIC_ESR, 0);
980 oldvalue = apic_read(APIC_ESR);
982 /* enables sending errors */
983 value = ERROR_APIC_VECTOR;
984 apic_write_around(APIC_LVTERR, value);
986 * spec says clear errors after enabling vector.
989 apic_write(APIC_ESR, 0);
990 value = apic_read(APIC_ESR);
991 if (value != oldvalue)
992 apic_printk(APIC_VERBOSE, "ESR value before enabling "
993 "vector: 0x%08lx after: 0x%08lx\n",
998 * Something untraceble is creating bad interrupts on
999 * secondary quads ... for the moment, just leave the
1000 * ESR disabled - we can't do anything useful with the
1001 * errors anyway - mbligh
1003 printk(KERN_INFO "Leaving ESR disabled.\n");
1005 printk(KERN_INFO "No ESR for 82489DX.\n");
1008 /* Disable the local apic timer */
1009 value = apic_read(APIC_LVTT);
1010 value |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
1011 apic_write_around(APIC_LVTT, value);
1013 setup_apic_nmi_watchdog(NULL);
1018 * Detect and initialize APIC
1020 static int __init detect_init_APIC (void)
1024 /* Disabled by kernel option? */
1025 if (enable_local_apic < 0)
1028 switch (boot_cpu_data.x86_vendor) {
1029 case X86_VENDOR_AMD:
1030 if ((boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model > 1) ||
1031 (boot_cpu_data.x86 == 15))
1034 case X86_VENDOR_INTEL:
1035 if (boot_cpu_data.x86 == 6 || boot_cpu_data.x86 == 15 ||
1036 (boot_cpu_data.x86 == 5 && cpu_has_apic))
1043 if (!cpu_has_apic) {
1045 * Over-ride BIOS and try to enable the local APIC only if
1046 * "lapic" specified.
1048 if (enable_local_apic <= 0) {
1049 printk(KERN_INFO "Local APIC disabled by BIOS -- "
1050 "you can enable it with \"lapic\"\n");
1054 * Some BIOSes disable the local APIC in the APIC_BASE
1055 * MSR. This can only be done in software for Intel P6 or later
1056 * and AMD K7 (Model > 1) or later.
1058 rdmsr(MSR_IA32_APICBASE, l, h);
1059 if (!(l & MSR_IA32_APICBASE_ENABLE)) {
1061 "Local APIC disabled by BIOS -- reenabling.\n");
1062 l &= ~MSR_IA32_APICBASE_BASE;
1063 l |= MSR_IA32_APICBASE_ENABLE | APIC_DEFAULT_PHYS_BASE;
1064 wrmsr(MSR_IA32_APICBASE, l, h);
1065 enabled_via_apicbase = 1;
1069 * The APIC feature bit should now be enabled
1072 features = cpuid_edx(1);
1073 if (!(features & (1 << X86_FEATURE_APIC))) {
1074 printk(KERN_WARNING "Could not enable APIC!\n");
1077 set_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
1078 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
1080 /* The BIOS may have set up the APIC at some other address */
1081 rdmsr(MSR_IA32_APICBASE, l, h);
1082 if (l & MSR_IA32_APICBASE_ENABLE)
1083 mp_lapic_addr = l & MSR_IA32_APICBASE_BASE;
1085 if (nmi_watchdog != NMI_NONE)
1086 nmi_watchdog = NMI_LOCAL_APIC;
1088 printk(KERN_INFO "Found and enabled local APIC!\n");
1095 printk(KERN_INFO "No local APIC present or hardware disabled\n");
1100 * init_apic_mappings - initialize APIC mappings
1102 void __init init_apic_mappings(void)
1104 unsigned long apic_phys;
1107 * If no local APIC can be found then set up a fake all
1108 * zeroes page to simulate the local APIC and another
1109 * one for the IO-APIC.
1111 if (!smp_found_config && detect_init_APIC()) {
1112 apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
1113 apic_phys = __pa(apic_phys);
1115 apic_phys = mp_lapic_addr;
1117 set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
1118 printk(KERN_DEBUG "mapped APIC to %08lx (%08lx)\n", APIC_BASE,
1122 * Fetch the APIC ID of the BSP in case we have a
1123 * default configuration (or the MP table is broken).
1125 if (boot_cpu_physical_apicid == -1U)
1126 boot_cpu_physical_apicid = GET_APIC_ID(apic_read(APIC_ID));
1128 #ifdef CONFIG_X86_IO_APIC
1130 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
1133 for (i = 0; i < nr_ioapics; i++) {
1134 if (smp_found_config) {
1135 ioapic_phys = mp_ioapics[i].mpc_apicaddr;
1138 "WARNING: bogus zero IO-APIC "
1139 "address found in MPTABLE, "
1140 "disabling IO/APIC support!\n");
1141 smp_found_config = 0;
1142 skip_ioapic_setup = 1;
1143 goto fake_ioapic_page;
1147 ioapic_phys = (unsigned long)
1148 alloc_bootmem_pages(PAGE_SIZE);
1149 ioapic_phys = __pa(ioapic_phys);
1151 set_fixmap_nocache(idx, ioapic_phys);
1152 printk(KERN_DEBUG "mapped IOAPIC to %08lx (%08lx)\n",
1153 __fix_to_virt(idx), ioapic_phys);
1161 * This initializes the IO-APIC and APIC hardware if this is
1164 int __init APIC_init_uniprocessor (void)
1166 if (enable_local_apic < 0)
1167 clear_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
1169 if (!smp_found_config && !cpu_has_apic)
1173 * Complain if the BIOS pretends there is one.
1175 if (!cpu_has_apic &&
1176 APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid])) {
1177 printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
1178 boot_cpu_physical_apicid);
1179 clear_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
1183 verify_local_APIC();
1188 * Hack: In case of kdump, after a crash, kernel might be booting
1189 * on a cpu with non-zero lapic id. But boot_cpu_physical_apicid
1190 * might be zero if read from MP tables. Get it from LAPIC.
1192 #ifdef CONFIG_CRASH_DUMP
1193 boot_cpu_physical_apicid = GET_APIC_ID(apic_read(APIC_ID));
1195 phys_cpu_present_map = physid_mask_of_physid(boot_cpu_physical_apicid);
1199 #ifdef CONFIG_X86_IO_APIC
1200 if (smp_found_config)
1201 if (!skip_ioapic_setup && nr_ioapics)
1210 * APIC command line parameters
1212 static int __init parse_lapic(char *arg)
1214 enable_local_apic = 1;
1217 early_param("lapic", parse_lapic);
1219 static int __init parse_nolapic(char *arg)
1221 enable_local_apic = -1;
1222 clear_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
1225 early_param("nolapic", parse_nolapic);
1227 static int __init parse_disable_lapic_timer(char *arg)
1229 local_apic_timer_disabled = 1;
1232 early_param("nolapic_timer", parse_disable_lapic_timer);
1234 static int __init parse_lapic_timer_c2_ok(char *arg)
1236 local_apic_timer_c2_ok = 1;
1239 early_param("lapic_timer_c2_ok", parse_lapic_timer_c2_ok);
1241 static int __init apic_set_verbosity(char *str)
1243 if (strcmp("debug", str) == 0)
1244 apic_verbosity = APIC_DEBUG;
1245 else if (strcmp("verbose", str) == 0)
1246 apic_verbosity = APIC_VERBOSE;
1250 __setup("apic=", apic_set_verbosity);
1254 * Local APIC interrupts
1258 * This interrupt should _never_ happen with our APIC/SMP architecture
1260 void smp_spurious_interrupt(struct pt_regs *regs)
1266 * Check if this really is a spurious interrupt and ACK it
1267 * if it is a vectored one. Just in case...
1268 * Spurious interrupts should not be ACKed.
1270 v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
1271 if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
1274 /* see sw-dev-man vol 3, chapter 7.4.13.5 */
1275 printk(KERN_INFO "spurious APIC interrupt on CPU#%d, "
1276 "should never happen.\n", smp_processor_id());
1281 * This interrupt should never happen with our APIC/SMP architecture
1283 void smp_error_interrupt(struct pt_regs *regs)
1285 unsigned long v, v1;
1288 /* First tickle the hardware, only then report what went on. -- REW */
1289 v = apic_read(APIC_ESR);
1290 apic_write(APIC_ESR, 0);
1291 v1 = apic_read(APIC_ESR);
1293 atomic_inc(&irq_err_count);
1295 /* Here is what the APIC error bits mean:
1298 2: Send accept error
1299 3: Receive accept error
1301 5: Send illegal vector
1302 6: Received illegal vector
1303 7: Illegal register address
1305 printk (KERN_DEBUG "APIC error on CPU%d: %02lx(%02lx)\n",
1306 smp_processor_id(), v , v1);
1311 * Initialize APIC interrupts
1313 void __init apic_intr_init(void)
1318 /* self generated IPI for local APIC timer */
1319 set_intr_gate(LOCAL_TIMER_VECTOR, apic_timer_interrupt);
1321 /* IPI vectors for APIC spurious and error interrupts */
1322 set_intr_gate(SPURIOUS_APIC_VECTOR, spurious_interrupt);
1323 set_intr_gate(ERROR_APIC_VECTOR, error_interrupt);
1325 /* thermal monitor LVT interrupt */
1326 #ifdef CONFIG_X86_MCE_P4THERMAL
1327 set_intr_gate(THERMAL_APIC_VECTOR, thermal_interrupt);
1332 * connect_bsp_APIC - attach the APIC to the interrupt system
1334 void __init connect_bsp_APIC(void)
1338 * Do not trust the local APIC being empty at bootup.
1342 * PIC mode, enable APIC mode in the IMCR, i.e. connect BSP's
1343 * local APIC to INT and NMI lines.
1345 apic_printk(APIC_VERBOSE, "leaving PIC mode, "
1346 "enabling APIC mode.\n");
1354 * disconnect_bsp_APIC - detach the APIC from the interrupt system
1355 * @virt_wire_setup: indicates, whether virtual wire mode is selected
1357 * Virtual wire mode is necessary to deliver legacy interrupts even when the
1360 void disconnect_bsp_APIC(int virt_wire_setup)
1364 * Put the board back into PIC mode (has an effect only on
1365 * certain older boards). Note that APIC interrupts, including
1366 * IPIs, won't work beyond this point! The only exception are
1369 apic_printk(APIC_VERBOSE, "disabling APIC mode, "
1370 "entering PIC mode.\n");
1374 /* Go back to Virtual Wire compatibility mode */
1375 unsigned long value;
1377 /* For the spurious interrupt use vector F, and enable it */
1378 value = apic_read(APIC_SPIV);
1379 value &= ~APIC_VECTOR_MASK;
1380 value |= APIC_SPIV_APIC_ENABLED;
1382 apic_write_around(APIC_SPIV, value);
1384 if (!virt_wire_setup) {
1386 * For LVT0 make it edge triggered, active high,
1387 * external and enabled
1389 value = apic_read(APIC_LVT0);
1390 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
1391 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
1392 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED );
1393 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1394 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
1395 apic_write_around(APIC_LVT0, value);
1398 apic_write_around(APIC_LVT0, APIC_LVT_MASKED);
1402 * For LVT1 make it edge triggered, active high, nmi and
1405 value = apic_read(APIC_LVT1);
1407 APIC_MODE_MASK | APIC_SEND_PENDING |
1408 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
1409 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
1410 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1411 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
1412 apic_write_around(APIC_LVT1, value);
1423 /* r/w apic fields */
1424 unsigned int apic_id;
1425 unsigned int apic_taskpri;
1426 unsigned int apic_ldr;
1427 unsigned int apic_dfr;
1428 unsigned int apic_spiv;
1429 unsigned int apic_lvtt;
1430 unsigned int apic_lvtpc;
1431 unsigned int apic_lvt0;
1432 unsigned int apic_lvt1;
1433 unsigned int apic_lvterr;
1434 unsigned int apic_tmict;
1435 unsigned int apic_tdcr;
1436 unsigned int apic_thmr;
1439 static int lapic_suspend(struct sys_device *dev, pm_message_t state)
1441 unsigned long flags;
1444 if (!apic_pm_state.active)
1447 maxlvt = lapic_get_maxlvt();
1449 apic_pm_state.apic_id = apic_read(APIC_ID);
1450 apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
1451 apic_pm_state.apic_ldr = apic_read(APIC_LDR);
1452 apic_pm_state.apic_dfr = apic_read(APIC_DFR);
1453 apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
1454 apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
1456 apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
1457 apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
1458 apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
1459 apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
1460 apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
1461 apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
1462 #ifdef CONFIG_X86_MCE_P4THERMAL
1464 apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
1467 local_irq_save(flags);
1468 disable_local_APIC();
1469 local_irq_restore(flags);
1473 static int lapic_resume(struct sys_device *dev)
1476 unsigned long flags;
1479 if (!apic_pm_state.active)
1482 maxlvt = lapic_get_maxlvt();
1484 local_irq_save(flags);
1487 * Make sure the APICBASE points to the right address
1489 * FIXME! This will be wrong if we ever support suspend on
1490 * SMP! We'll need to do this as part of the CPU restore!
1492 rdmsr(MSR_IA32_APICBASE, l, h);
1493 l &= ~MSR_IA32_APICBASE_BASE;
1494 l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
1495 wrmsr(MSR_IA32_APICBASE, l, h);
1497 apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
1498 apic_write(APIC_ID, apic_pm_state.apic_id);
1499 apic_write(APIC_DFR, apic_pm_state.apic_dfr);
1500 apic_write(APIC_LDR, apic_pm_state.apic_ldr);
1501 apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
1502 apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
1503 apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
1504 apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
1505 #ifdef CONFIG_X86_MCE_P4THERMAL
1507 apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
1510 apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
1511 apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
1512 apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
1513 apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
1514 apic_write(APIC_ESR, 0);
1515 apic_read(APIC_ESR);
1516 apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
1517 apic_write(APIC_ESR, 0);
1518 apic_read(APIC_ESR);
1519 local_irq_restore(flags);
1524 * This device has no shutdown method - fully functioning local APICs
1525 * are needed on every CPU up until machine_halt/restart/poweroff.
1528 static struct sysdev_class lapic_sysclass = {
1529 set_kset_name("lapic"),
1530 .resume = lapic_resume,
1531 .suspend = lapic_suspend,
1534 static struct sys_device device_lapic = {
1536 .cls = &lapic_sysclass,
1539 static void __devinit apic_pm_activate(void)
1541 apic_pm_state.active = 1;
1544 static int __init init_lapic_sysfs(void)
1550 /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
1552 error = sysdev_class_register(&lapic_sysclass);
1554 error = sysdev_register(&device_lapic);
1557 device_initcall(init_lapic_sysfs);
1559 #else /* CONFIG_PM */
1561 static void apic_pm_activate(void) { }
1563 #endif /* CONFIG_PM */