2 * Local APIC handling, local APIC timers
4 * (c) 1999, 2000, 2009 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/perf_event.h>
18 #include <linux/kernel_stat.h>
19 #include <linux/mc146818rtc.h>
20 #include <linux/acpi_pmtmr.h>
21 #include <linux/clockchips.h>
22 #include <linux/interrupt.h>
23 #include <linux/bootmem.h>
24 #include <linux/ftrace.h>
25 #include <linux/ioport.h>
26 #include <linux/module.h>
27 #include <linux/syscore_ops.h>
28 #include <linux/delay.h>
29 #include <linux/timex.h>
30 #include <linux/i8253.h>
31 #include <linux/dmar.h>
32 #include <linux/init.h>
33 #include <linux/cpu.h>
34 #include <linux/dmi.h>
35 #include <linux/smp.h>
38 #include <asm/perf_event.h>
39 #include <asm/x86_init.h>
40 #include <asm/pgalloc.h>
41 #include <linux/atomic.h>
42 #include <asm/mpspec.h>
43 #include <asm/i8259.h>
44 #include <asm/proto.h>
46 #include <asm/io_apic.h>
55 #include <asm/hypervisor.h>
57 unsigned int num_processors;
59 unsigned disabled_cpus __cpuinitdata;
61 /* Processor that is doing the boot up */
62 unsigned int boot_cpu_physical_apicid = -1U;
65 * The highest APIC ID seen during enumeration.
67 unsigned int max_physical_apicid;
70 * Bitmask of physically existing CPUs:
72 physid_mask_t phys_cpu_present_map;
75 * Map cpu index to physical APIC ID
77 DEFINE_EARLY_PER_CPU(u16, x86_cpu_to_apicid, BAD_APICID);
78 DEFINE_EARLY_PER_CPU(u16, x86_bios_cpu_apicid, BAD_APICID);
79 EXPORT_EARLY_PER_CPU_SYMBOL(x86_cpu_to_apicid);
80 EXPORT_EARLY_PER_CPU_SYMBOL(x86_bios_cpu_apicid);
85 * On x86_32, the mapping between cpu and logical apicid may vary
86 * depending on apic in use. The following early percpu variable is
87 * used for the mapping. This is where the behaviors of x86_64 and 32
88 * actually diverge. Let's keep it ugly for now.
90 DEFINE_EARLY_PER_CPU(int, x86_cpu_to_logical_apicid, BAD_APICID);
93 * Knob to control our willingness to enable the local APIC.
97 static int force_enable_local_apic __initdata;
99 * APIC command line parameters
101 static int __init parse_lapic(char *arg)
103 force_enable_local_apic = 1;
106 early_param("lapic", parse_lapic);
107 /* Local APIC was disabled by the BIOS and enabled by the kernel */
108 static int enabled_via_apicbase;
111 * Handle interrupt mode configuration register (IMCR).
112 * This register controls whether the interrupt signals
113 * that reach the BSP come from the master PIC or from the
114 * local APIC. Before entering Symmetric I/O Mode, either
115 * the BIOS or the operating system must switch out of
116 * PIC Mode by changing the IMCR.
118 static inline void imcr_pic_to_apic(void)
120 /* select IMCR register */
122 /* NMI and 8259 INTR go through APIC */
126 static inline void imcr_apic_to_pic(void)
128 /* select IMCR register */
130 /* NMI and 8259 INTR go directly to BSP */
136 static int apic_calibrate_pmtmr __initdata;
137 static __init int setup_apicpmtimer(char *s)
139 apic_calibrate_pmtmr = 1;
143 __setup("apicpmtimer", setup_apicpmtimer);
147 #ifdef CONFIG_X86_X2APIC
148 /* x2apic enabled before OS handover */
149 int x2apic_preenabled;
150 static int x2apic_disabled;
151 static __init int setup_nox2apic(char *str)
153 if (x2apic_enabled()) {
154 pr_warning("Bios already enabled x2apic, "
155 "can't enforce nox2apic");
159 setup_clear_cpu_cap(X86_FEATURE_X2APIC);
162 early_param("nox2apic", setup_nox2apic);
165 unsigned long mp_lapic_addr;
167 /* Disable local APIC timer from the kernel commandline or via dmi quirk */
168 static int disable_apic_timer __initdata;
169 /* Local APIC timer works in C2 */
170 int local_apic_timer_c2_ok;
171 EXPORT_SYMBOL_GPL(local_apic_timer_c2_ok);
173 int first_system_vector = 0xfe;
176 * Debug level, exported for io_apic.c
178 unsigned int apic_verbosity;
182 /* Have we found an MP table */
183 int smp_found_config;
185 static struct resource lapic_resource = {
186 .name = "Local APIC",
187 .flags = IORESOURCE_MEM | IORESOURCE_BUSY,
190 unsigned int lapic_timer_frequency = 0;
192 static void apic_pm_activate(void);
194 static unsigned long apic_phys;
197 * Get the LAPIC version
199 static inline int lapic_get_version(void)
201 return GET_APIC_VERSION(apic_read(APIC_LVR));
205 * Check, if the APIC is integrated or a separate chip
207 static inline int lapic_is_integrated(void)
212 return APIC_INTEGRATED(lapic_get_version());
217 * Check, whether this is a modern or a first generation APIC
219 static int modern_apic(void)
221 /* AMD systems use old APIC versions, so check the CPU */
222 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
223 boot_cpu_data.x86 >= 0xf)
225 return lapic_get_version() >= 0x14;
229 * right after this call apic become NOOP driven
230 * so apic->write/read doesn't do anything
232 static void __init apic_disable(void)
234 pr_info("APIC: switched to apic NOOP\n");
238 void native_apic_wait_icr_idle(void)
240 while (apic_read(APIC_ICR) & APIC_ICR_BUSY)
244 u32 native_safe_apic_wait_icr_idle(void)
251 send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
254 inc_irq_stat(icr_read_retry_count);
256 } while (timeout++ < 1000);
261 void native_apic_icr_write(u32 low, u32 id)
263 apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(id));
264 apic_write(APIC_ICR, low);
267 u64 native_apic_icr_read(void)
271 icr2 = apic_read(APIC_ICR2);
272 icr1 = apic_read(APIC_ICR);
274 return icr1 | ((u64)icr2 << 32);
279 * get_physical_broadcast - Get number of physical broadcast IDs
281 int get_physical_broadcast(void)
283 return modern_apic() ? 0xff : 0xf;
288 * lapic_get_maxlvt - get the maximum number of local vector table entries
290 int lapic_get_maxlvt(void)
294 v = apic_read(APIC_LVR);
296 * - we always have APIC integrated on 64bit mode
297 * - 82489DXs do not report # of LVT entries
299 return APIC_INTEGRATED(GET_APIC_VERSION(v)) ? GET_APIC_MAXLVT(v) : 2;
307 #define APIC_DIVISOR 16
310 * This function sets up the local APIC timer, with a timeout of
311 * 'clocks' APIC bus clock. During calibration we actually call
312 * this function twice on the boot CPU, once with a bogus timeout
313 * value, second time for real. The other (noncalibrating) CPUs
314 * call this function only once, with the real, calibrated value.
316 * We do reads before writes even if unnecessary, to get around the
317 * P5 APIC double write bug.
319 static void __setup_APIC_LVTT(unsigned int clocks, int oneshot, int irqen)
321 unsigned int lvtt_value, tmp_value;
323 lvtt_value = LOCAL_TIMER_VECTOR;
325 lvtt_value |= APIC_LVT_TIMER_PERIODIC;
326 if (!lapic_is_integrated())
327 lvtt_value |= SET_APIC_TIMER_BASE(APIC_TIMER_BASE_DIV);
330 lvtt_value |= APIC_LVT_MASKED;
332 apic_write(APIC_LVTT, lvtt_value);
337 tmp_value = apic_read(APIC_TDCR);
338 apic_write(APIC_TDCR,
339 (tmp_value & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE)) |
343 apic_write(APIC_TMICT, clocks / APIC_DIVISOR);
347 * Setup extended LVT, AMD specific
349 * Software should use the LVT offsets the BIOS provides. The offsets
350 * are determined by the subsystems using it like those for MCE
351 * threshold or IBS. On K8 only offset 0 (APIC500) and MCE interrupts
352 * are supported. Beginning with family 10h at least 4 offsets are
355 * Since the offsets must be consistent for all cores, we keep track
356 * of the LVT offsets in software and reserve the offset for the same
357 * vector also to be used on other cores. An offset is freed by
358 * setting the entry to APIC_EILVT_MASKED.
360 * If the BIOS is right, there should be no conflicts. Otherwise a
361 * "[Firmware Bug]: ..." error message is generated. However, if
362 * software does not properly determines the offsets, it is not
363 * necessarily a BIOS bug.
366 static atomic_t eilvt_offsets[APIC_EILVT_NR_MAX];
368 static inline int eilvt_entry_is_changeable(unsigned int old, unsigned int new)
370 return (old & APIC_EILVT_MASKED)
371 || (new == APIC_EILVT_MASKED)
372 || ((new & ~APIC_EILVT_MASKED) == old);
375 static unsigned int reserve_eilvt_offset(int offset, unsigned int new)
377 unsigned int rsvd; /* 0: uninitialized */
379 if (offset >= APIC_EILVT_NR_MAX)
382 rsvd = atomic_read(&eilvt_offsets[offset]) & ~APIC_EILVT_MASKED;
385 !eilvt_entry_is_changeable(rsvd, new))
386 /* may not change if vectors are different */
388 rsvd = atomic_cmpxchg(&eilvt_offsets[offset], rsvd, new);
389 } while (rsvd != new);
395 * If mask=1, the LVT entry does not generate interrupts while mask=0
396 * enables the vector. See also the BKDGs. Must be called with
397 * preemption disabled.
400 int setup_APIC_eilvt(u8 offset, u8 vector, u8 msg_type, u8 mask)
402 unsigned long reg = APIC_EILVTn(offset);
403 unsigned int new, old, reserved;
405 new = (mask << 16) | (msg_type << 8) | vector;
406 old = apic_read(reg);
407 reserved = reserve_eilvt_offset(offset, new);
409 if (reserved != new) {
410 pr_err(FW_BUG "cpu %d, try to use APIC%lX (LVT offset %d) for "
411 "vector 0x%x, but the register is already in use for "
412 "vector 0x%x on another cpu\n",
413 smp_processor_id(), reg, offset, new, reserved);
417 if (!eilvt_entry_is_changeable(old, new)) {
418 pr_err(FW_BUG "cpu %d, try to use APIC%lX (LVT offset %d) for "
419 "vector 0x%x, but the register is already in use for "
420 "vector 0x%x on this cpu\n",
421 smp_processor_id(), reg, offset, new, old);
425 apic_write(reg, new);
429 EXPORT_SYMBOL_GPL(setup_APIC_eilvt);
432 * Program the next event, relative to now
434 static int lapic_next_event(unsigned long delta,
435 struct clock_event_device *evt)
437 apic_write(APIC_TMICT, delta);
442 * Setup the lapic timer in periodic or oneshot mode
444 static void lapic_timer_setup(enum clock_event_mode mode,
445 struct clock_event_device *evt)
450 /* Lapic used as dummy for broadcast ? */
451 if (evt->features & CLOCK_EVT_FEAT_DUMMY)
454 local_irq_save(flags);
457 case CLOCK_EVT_MODE_PERIODIC:
458 case CLOCK_EVT_MODE_ONESHOT:
459 __setup_APIC_LVTT(lapic_timer_frequency,
460 mode != CLOCK_EVT_MODE_PERIODIC, 1);
462 case CLOCK_EVT_MODE_UNUSED:
463 case CLOCK_EVT_MODE_SHUTDOWN:
464 v = apic_read(APIC_LVTT);
465 v |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
466 apic_write(APIC_LVTT, v);
467 apic_write(APIC_TMICT, 0);
469 case CLOCK_EVT_MODE_RESUME:
470 /* Nothing to do here */
474 local_irq_restore(flags);
478 * Local APIC timer broadcast function
480 static void lapic_timer_broadcast(const struct cpumask *mask)
483 apic->send_IPI_mask(mask, LOCAL_TIMER_VECTOR);
489 * The local apic timer can be used for any function which is CPU local.
491 static struct clock_event_device lapic_clockevent = {
493 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT
494 | CLOCK_EVT_FEAT_C3STOP | CLOCK_EVT_FEAT_DUMMY,
496 .set_mode = lapic_timer_setup,
497 .set_next_event = lapic_next_event,
498 .broadcast = lapic_timer_broadcast,
502 static DEFINE_PER_CPU(struct clock_event_device, lapic_events);
505 * Setup the local APIC timer for this CPU. Copy the initialized values
506 * of the boot CPU and register the clock event in the framework.
508 static void __cpuinit setup_APIC_timer(void)
510 struct clock_event_device *levt = &__get_cpu_var(lapic_events);
512 if (this_cpu_has(X86_FEATURE_ARAT)) {
513 lapic_clockevent.features &= ~CLOCK_EVT_FEAT_C3STOP;
514 /* Make LAPIC timer preferrable over percpu HPET */
515 lapic_clockevent.rating = 150;
518 memcpy(levt, &lapic_clockevent, sizeof(*levt));
519 levt->cpumask = cpumask_of(smp_processor_id());
521 clockevents_register_device(levt);
525 * In this functions we calibrate APIC bus clocks to the external timer.
527 * We want to do the calibration only once since we want to have local timer
528 * irqs syncron. CPUs connected by the same APIC bus have the very same bus
531 * This was previously done by reading the PIT/HPET and waiting for a wrap
532 * around to find out, that a tick has elapsed. I have a box, where the PIT
533 * readout is broken, so it never gets out of the wait loop again. This was
534 * also reported by others.
536 * Monitoring the jiffies value is inaccurate and the clockevents
537 * infrastructure allows us to do a simple substitution of the interrupt
540 * The calibration routine also uses the pm_timer when possible, as the PIT
541 * happens to run way too slow (factor 2.3 on my VAIO CoreDuo, which goes
542 * back to normal later in the boot process).
545 #define LAPIC_CAL_LOOPS (HZ/10)
547 static __initdata int lapic_cal_loops = -1;
548 static __initdata long lapic_cal_t1, lapic_cal_t2;
549 static __initdata unsigned long long lapic_cal_tsc1, lapic_cal_tsc2;
550 static __initdata unsigned long lapic_cal_pm1, lapic_cal_pm2;
551 static __initdata unsigned long lapic_cal_j1, lapic_cal_j2;
554 * Temporary interrupt handler.
556 static void __init lapic_cal_handler(struct clock_event_device *dev)
558 unsigned long long tsc = 0;
559 long tapic = apic_read(APIC_TMCCT);
560 unsigned long pm = acpi_pm_read_early();
565 switch (lapic_cal_loops++) {
567 lapic_cal_t1 = tapic;
568 lapic_cal_tsc1 = tsc;
570 lapic_cal_j1 = jiffies;
573 case LAPIC_CAL_LOOPS:
574 lapic_cal_t2 = tapic;
575 lapic_cal_tsc2 = tsc;
576 if (pm < lapic_cal_pm1)
577 pm += ACPI_PM_OVRRUN;
579 lapic_cal_j2 = jiffies;
585 calibrate_by_pmtimer(long deltapm, long *delta, long *deltatsc)
587 const long pm_100ms = PMTMR_TICKS_PER_SEC / 10;
588 const long pm_thresh = pm_100ms / 100;
592 #ifndef CONFIG_X86_PM_TIMER
596 apic_printk(APIC_VERBOSE, "... PM-Timer delta = %ld\n", deltapm);
598 /* Check, if the PM timer is available */
602 mult = clocksource_hz2mult(PMTMR_TICKS_PER_SEC, 22);
604 if (deltapm > (pm_100ms - pm_thresh) &&
605 deltapm < (pm_100ms + pm_thresh)) {
606 apic_printk(APIC_VERBOSE, "... PM-Timer result ok\n");
610 res = (((u64)deltapm) * mult) >> 22;
611 do_div(res, 1000000);
612 pr_warning("APIC calibration not consistent "
613 "with PM-Timer: %ldms instead of 100ms\n",(long)res);
615 /* Correct the lapic counter value */
616 res = (((u64)(*delta)) * pm_100ms);
617 do_div(res, deltapm);
618 pr_info("APIC delta adjusted to PM-Timer: "
619 "%lu (%ld)\n", (unsigned long)res, *delta);
622 /* Correct the tsc counter value */
624 res = (((u64)(*deltatsc)) * pm_100ms);
625 do_div(res, deltapm);
626 apic_printk(APIC_VERBOSE, "TSC delta adjusted to "
627 "PM-Timer: %lu (%ld)\n",
628 (unsigned long)res, *deltatsc);
629 *deltatsc = (long)res;
635 static int __init calibrate_APIC_clock(void)
637 struct clock_event_device *levt = &__get_cpu_var(lapic_events);
638 void (*real_handler)(struct clock_event_device *dev);
639 unsigned long deltaj;
640 long delta, deltatsc;
641 int pm_referenced = 0;
644 * check if lapic timer has already been calibrated by platform
645 * specific routine, such as tsc calibration code. if so, we just fill
646 * in the clockevent structure and return.
649 if (lapic_timer_frequency) {
650 apic_printk(APIC_VERBOSE, "lapic timer already calibrated %d\n",
651 lapic_timer_frequency);
652 lapic_clockevent.mult = div_sc(lapic_timer_frequency/APIC_DIVISOR,
653 TICK_NSEC, lapic_clockevent.shift);
654 lapic_clockevent.max_delta_ns =
655 clockevent_delta2ns(0x7FFFFF, &lapic_clockevent);
656 lapic_clockevent.min_delta_ns =
657 clockevent_delta2ns(0xF, &lapic_clockevent);
658 lapic_clockevent.features &= ~CLOCK_EVT_FEAT_DUMMY;
664 /* Replace the global interrupt handler */
665 real_handler = global_clock_event->event_handler;
666 global_clock_event->event_handler = lapic_cal_handler;
669 * Setup the APIC counter to maximum. There is no way the lapic
670 * can underflow in the 100ms detection time frame
672 __setup_APIC_LVTT(0xffffffff, 0, 0);
674 /* Let the interrupts run */
677 while (lapic_cal_loops <= LAPIC_CAL_LOOPS)
682 /* Restore the real event handler */
683 global_clock_event->event_handler = real_handler;
685 /* Build delta t1-t2 as apic timer counts down */
686 delta = lapic_cal_t1 - lapic_cal_t2;
687 apic_printk(APIC_VERBOSE, "... lapic delta = %ld\n", delta);
689 deltatsc = (long)(lapic_cal_tsc2 - lapic_cal_tsc1);
691 /* we trust the PM based calibration if possible */
692 pm_referenced = !calibrate_by_pmtimer(lapic_cal_pm2 - lapic_cal_pm1,
695 /* Calculate the scaled math multiplication factor */
696 lapic_clockevent.mult = div_sc(delta, TICK_NSEC * LAPIC_CAL_LOOPS,
697 lapic_clockevent.shift);
698 lapic_clockevent.max_delta_ns =
699 clockevent_delta2ns(0x7FFFFFFF, &lapic_clockevent);
700 lapic_clockevent.min_delta_ns =
701 clockevent_delta2ns(0xF, &lapic_clockevent);
703 lapic_timer_frequency = (delta * APIC_DIVISOR) / LAPIC_CAL_LOOPS;
705 apic_printk(APIC_VERBOSE, "..... delta %ld\n", delta);
706 apic_printk(APIC_VERBOSE, "..... mult: %u\n", lapic_clockevent.mult);
707 apic_printk(APIC_VERBOSE, "..... calibration result: %u\n",
708 lapic_timer_frequency);
711 apic_printk(APIC_VERBOSE, "..... CPU clock speed is "
713 (deltatsc / LAPIC_CAL_LOOPS) / (1000000 / HZ),
714 (deltatsc / LAPIC_CAL_LOOPS) % (1000000 / HZ));
717 apic_printk(APIC_VERBOSE, "..... host bus clock speed is "
719 lapic_timer_frequency / (1000000 / HZ),
720 lapic_timer_frequency % (1000000 / HZ));
723 * Do a sanity check on the APIC calibration result
725 if (lapic_timer_frequency < (1000000 / HZ)) {
727 pr_warning("APIC frequency too slow, disabling apic timer\n");
731 levt->features &= ~CLOCK_EVT_FEAT_DUMMY;
734 * PM timer calibration failed or not turned on
735 * so lets try APIC timer based calibration
737 if (!pm_referenced) {
738 apic_printk(APIC_VERBOSE, "... verify APIC timer\n");
741 * Setup the apic timer manually
743 levt->event_handler = lapic_cal_handler;
744 lapic_timer_setup(CLOCK_EVT_MODE_PERIODIC, levt);
745 lapic_cal_loops = -1;
747 /* Let the interrupts run */
750 while (lapic_cal_loops <= LAPIC_CAL_LOOPS)
753 /* Stop the lapic timer */
754 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, levt);
757 deltaj = lapic_cal_j2 - lapic_cal_j1;
758 apic_printk(APIC_VERBOSE, "... jiffies delta = %lu\n", deltaj);
760 /* Check, if the jiffies result is consistent */
761 if (deltaj >= LAPIC_CAL_LOOPS-2 && deltaj <= LAPIC_CAL_LOOPS+2)
762 apic_printk(APIC_VERBOSE, "... jiffies result ok\n");
764 levt->features |= CLOCK_EVT_FEAT_DUMMY;
768 if (levt->features & CLOCK_EVT_FEAT_DUMMY) {
769 pr_warning("APIC timer disabled due to verification failure\n");
777 * Setup the boot APIC
779 * Calibrate and verify the result.
781 void __init setup_boot_APIC_clock(void)
784 * The local apic timer can be disabled via the kernel
785 * commandline or from the CPU detection code. Register the lapic
786 * timer as a dummy clock event source on SMP systems, so the
787 * broadcast mechanism is used. On UP systems simply ignore it.
789 if (disable_apic_timer) {
790 pr_info("Disabling APIC timer\n");
791 /* No broadcast on UP ! */
792 if (num_possible_cpus() > 1) {
793 lapic_clockevent.mult = 1;
799 apic_printk(APIC_VERBOSE, "Using local APIC timer interrupts.\n"
800 "calibrating APIC timer ...\n");
802 if (calibrate_APIC_clock()) {
803 /* No broadcast on UP ! */
804 if (num_possible_cpus() > 1)
810 * If nmi_watchdog is set to IO_APIC, we need the
811 * PIT/HPET going. Otherwise register lapic as a dummy
814 lapic_clockevent.features &= ~CLOCK_EVT_FEAT_DUMMY;
816 /* Setup the lapic or request the broadcast */
820 void __cpuinit setup_secondary_APIC_clock(void)
826 * The guts of the apic timer interrupt
828 static void local_apic_timer_interrupt(void)
830 int cpu = smp_processor_id();
831 struct clock_event_device *evt = &per_cpu(lapic_events, cpu);
834 * Normally we should not be here till LAPIC has been initialized but
835 * in some cases like kdump, its possible that there is a pending LAPIC
836 * timer interrupt from previous kernel's context and is delivered in
837 * new kernel the moment interrupts are enabled.
839 * Interrupts are enabled early and LAPIC is setup much later, hence
840 * its possible that when we get here evt->event_handler is NULL.
841 * Check for event_handler being NULL and discard the interrupt as
844 if (!evt->event_handler) {
845 pr_warning("Spurious LAPIC timer interrupt on cpu %d\n", cpu);
847 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, evt);
852 * the NMI deadlock-detector uses this.
854 inc_irq_stat(apic_timer_irqs);
856 evt->event_handler(evt);
860 * Local APIC timer interrupt. This is the most natural way for doing
861 * local interrupts, but local timer interrupts can be emulated by
862 * broadcast interrupts too. [in case the hw doesn't support APIC timers]
864 * [ if a single-CPU system runs an SMP kernel then we call the local
865 * interrupt as well. Thus we cannot inline the local irq ... ]
867 void __irq_entry smp_apic_timer_interrupt(struct pt_regs *regs)
869 struct pt_regs *old_regs = set_irq_regs(regs);
872 * NOTE! We'd better ACK the irq immediately,
873 * because timer handling can be slow.
877 * update_process_times() expects us to have done irq_enter().
878 * Besides, if we don't timer interrupts ignore the global
879 * interrupt lock, which is the WrongThing (tm) to do.
883 local_apic_timer_interrupt();
886 set_irq_regs(old_regs);
889 int setup_profiling_timer(unsigned int multiplier)
895 * Local APIC start and shutdown
899 * clear_local_APIC - shutdown the local APIC
901 * This is called, when a CPU is disabled and before rebooting, so the state of
902 * the local APIC has no dangling leftovers. Also used to cleanout any BIOS
903 * leftovers during boot.
905 void clear_local_APIC(void)
910 /* APIC hasn't been mapped yet */
911 if (!x2apic_mode && !apic_phys)
914 maxlvt = lapic_get_maxlvt();
916 * Masking an LVT entry can trigger a local APIC error
917 * if the vector is zero. Mask LVTERR first to prevent this.
920 v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
921 apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
924 * Careful: we have to set masks only first to deassert
925 * any level-triggered sources.
927 v = apic_read(APIC_LVTT);
928 apic_write(APIC_LVTT, v | APIC_LVT_MASKED);
929 v = apic_read(APIC_LVT0);
930 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
931 v = apic_read(APIC_LVT1);
932 apic_write(APIC_LVT1, v | APIC_LVT_MASKED);
934 v = apic_read(APIC_LVTPC);
935 apic_write(APIC_LVTPC, v | APIC_LVT_MASKED);
938 /* lets not touch this if we didn't frob it */
939 #ifdef CONFIG_X86_THERMAL_VECTOR
941 v = apic_read(APIC_LVTTHMR);
942 apic_write(APIC_LVTTHMR, v | APIC_LVT_MASKED);
945 #ifdef CONFIG_X86_MCE_INTEL
947 v = apic_read(APIC_LVTCMCI);
948 if (!(v & APIC_LVT_MASKED))
949 apic_write(APIC_LVTCMCI, v | APIC_LVT_MASKED);
954 * Clean APIC state for other OSs:
956 apic_write(APIC_LVTT, APIC_LVT_MASKED);
957 apic_write(APIC_LVT0, APIC_LVT_MASKED);
958 apic_write(APIC_LVT1, APIC_LVT_MASKED);
960 apic_write(APIC_LVTERR, APIC_LVT_MASKED);
962 apic_write(APIC_LVTPC, APIC_LVT_MASKED);
964 /* Integrated APIC (!82489DX) ? */
965 if (lapic_is_integrated()) {
967 /* Clear ESR due to Pentium errata 3AP and 11AP */
968 apic_write(APIC_ESR, 0);
974 * disable_local_APIC - clear and disable the local APIC
976 void disable_local_APIC(void)
980 /* APIC hasn't been mapped yet */
981 if (!x2apic_mode && !apic_phys)
987 * Disable APIC (implies clearing of registers
990 value = apic_read(APIC_SPIV);
991 value &= ~APIC_SPIV_APIC_ENABLED;
992 apic_write(APIC_SPIV, value);
996 * When LAPIC was disabled by the BIOS and enabled by the kernel,
997 * restore the disabled state.
999 if (enabled_via_apicbase) {
1002 rdmsr(MSR_IA32_APICBASE, l, h);
1003 l &= ~MSR_IA32_APICBASE_ENABLE;
1004 wrmsr(MSR_IA32_APICBASE, l, h);
1010 * If Linux enabled the LAPIC against the BIOS default disable it down before
1011 * re-entering the BIOS on shutdown. Otherwise the BIOS may get confused and
1012 * not power-off. Additionally clear all LVT entries before disable_local_APIC
1013 * for the case where Linux didn't enable the LAPIC.
1015 void lapic_shutdown(void)
1017 unsigned long flags;
1019 if (!cpu_has_apic && !apic_from_smp_config())
1022 local_irq_save(flags);
1024 #ifdef CONFIG_X86_32
1025 if (!enabled_via_apicbase)
1029 disable_local_APIC();
1032 local_irq_restore(flags);
1036 * This is to verify that we're looking at a real local APIC.
1037 * Check these against your board if the CPUs aren't getting
1038 * started for no apparent reason.
1040 int __init verify_local_APIC(void)
1042 unsigned int reg0, reg1;
1045 * The version register is read-only in a real APIC.
1047 reg0 = apic_read(APIC_LVR);
1048 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
1049 apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
1050 reg1 = apic_read(APIC_LVR);
1051 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
1054 * The two version reads above should print the same
1055 * numbers. If the second one is different, then we
1056 * poke at a non-APIC.
1062 * Check if the version looks reasonably.
1064 reg1 = GET_APIC_VERSION(reg0);
1065 if (reg1 == 0x00 || reg1 == 0xff)
1067 reg1 = lapic_get_maxlvt();
1068 if (reg1 < 0x02 || reg1 == 0xff)
1072 * The ID register is read/write in a real APIC.
1074 reg0 = apic_read(APIC_ID);
1075 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
1076 apic_write(APIC_ID, reg0 ^ apic->apic_id_mask);
1077 reg1 = apic_read(APIC_ID);
1078 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg1);
1079 apic_write(APIC_ID, reg0);
1080 if (reg1 != (reg0 ^ apic->apic_id_mask))
1084 * The next two are just to see if we have sane values.
1085 * They're only really relevant if we're in Virtual Wire
1086 * compatibility mode, but most boxes are anymore.
1088 reg0 = apic_read(APIC_LVT0);
1089 apic_printk(APIC_DEBUG, "Getting LVT0: %x\n", reg0);
1090 reg1 = apic_read(APIC_LVT1);
1091 apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
1097 * sync_Arb_IDs - synchronize APIC bus arbitration IDs
1099 void __init sync_Arb_IDs(void)
1102 * Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 And not
1105 if (modern_apic() || boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
1111 apic_wait_icr_idle();
1113 apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
1114 apic_write(APIC_ICR, APIC_DEST_ALLINC |
1115 APIC_INT_LEVELTRIG | APIC_DM_INIT);
1119 * An initial setup of the virtual wire mode.
1121 void __init init_bsp_APIC(void)
1126 * Don't do the setup now if we have a SMP BIOS as the
1127 * through-I/O-APIC virtual wire mode might be active.
1129 if (smp_found_config || !cpu_has_apic)
1133 * Do not trust the local APIC being empty at bootup.
1140 value = apic_read(APIC_SPIV);
1141 value &= ~APIC_VECTOR_MASK;
1142 value |= APIC_SPIV_APIC_ENABLED;
1144 #ifdef CONFIG_X86_32
1145 /* This bit is reserved on P4/Xeon and should be cleared */
1146 if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) &&
1147 (boot_cpu_data.x86 == 15))
1148 value &= ~APIC_SPIV_FOCUS_DISABLED;
1151 value |= APIC_SPIV_FOCUS_DISABLED;
1152 value |= SPURIOUS_APIC_VECTOR;
1153 apic_write(APIC_SPIV, value);
1156 * Set up the virtual wire mode.
1158 apic_write(APIC_LVT0, APIC_DM_EXTINT);
1159 value = APIC_DM_NMI;
1160 if (!lapic_is_integrated()) /* 82489DX */
1161 value |= APIC_LVT_LEVEL_TRIGGER;
1162 apic_write(APIC_LVT1, value);
1165 static void __cpuinit lapic_setup_esr(void)
1167 unsigned int oldvalue, value, maxlvt;
1169 if (!lapic_is_integrated()) {
1170 pr_info("No ESR for 82489DX.\n");
1174 if (apic->disable_esr) {
1176 * Something untraceable is creating bad interrupts on
1177 * secondary quads ... for the moment, just leave the
1178 * ESR disabled - we can't do anything useful with the
1179 * errors anyway - mbligh
1181 pr_info("Leaving ESR disabled.\n");
1185 maxlvt = lapic_get_maxlvt();
1186 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
1187 apic_write(APIC_ESR, 0);
1188 oldvalue = apic_read(APIC_ESR);
1190 /* enables sending errors */
1191 value = ERROR_APIC_VECTOR;
1192 apic_write(APIC_LVTERR, value);
1195 * spec says clear errors after enabling vector.
1198 apic_write(APIC_ESR, 0);
1199 value = apic_read(APIC_ESR);
1200 if (value != oldvalue)
1201 apic_printk(APIC_VERBOSE, "ESR value before enabling "
1202 "vector: 0x%08x after: 0x%08x\n",
1207 * setup_local_APIC - setup the local APIC
1209 * Used to setup local APIC while initializing BSP or bringin up APs.
1210 * Always called with preemption disabled.
1212 void __cpuinit setup_local_APIC(void)
1214 int cpu = smp_processor_id();
1215 unsigned int value, queued;
1216 int i, j, acked = 0;
1217 unsigned long long tsc = 0, ntsc;
1218 long long max_loops = cpu_khz;
1224 disable_ioapic_support();
1228 #ifdef CONFIG_X86_32
1229 /* Pound the ESR really hard over the head with a big hammer - mbligh */
1230 if (lapic_is_integrated() && apic->disable_esr) {
1231 apic_write(APIC_ESR, 0);
1232 apic_write(APIC_ESR, 0);
1233 apic_write(APIC_ESR, 0);
1234 apic_write(APIC_ESR, 0);
1237 perf_events_lapic_init();
1240 * Double-check whether this APIC is really registered.
1241 * This is meaningless in clustered apic mode, so we skip it.
1243 BUG_ON(!apic->apic_id_registered());
1246 * Intel recommends to set DFR, LDR and TPR before enabling
1247 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
1248 * document number 292116). So here it goes...
1250 apic->init_apic_ldr();
1252 #ifdef CONFIG_X86_32
1254 * APIC LDR is initialized. If logical_apicid mapping was
1255 * initialized during get_smp_config(), make sure it matches the
1258 i = early_per_cpu(x86_cpu_to_logical_apicid, cpu);
1259 WARN_ON(i != BAD_APICID && i != logical_smp_processor_id());
1260 /* always use the value from LDR */
1261 early_per_cpu(x86_cpu_to_logical_apicid, cpu) =
1262 logical_smp_processor_id();
1265 * Some NUMA implementations (NUMAQ) don't initialize apicid to
1266 * node mapping during NUMA init. Now that logical apicid is
1267 * guaranteed to be known, give it another chance. This is already
1268 * a bit too late - percpu allocation has already happened without
1269 * proper NUMA affinity.
1271 if (apic->x86_32_numa_cpu_node)
1272 set_apicid_to_node(early_per_cpu(x86_cpu_to_apicid, cpu),
1273 apic->x86_32_numa_cpu_node(cpu));
1277 * Set Task Priority to 'accept all'. We never change this
1280 value = apic_read(APIC_TASKPRI);
1281 value &= ~APIC_TPRI_MASK;
1282 apic_write(APIC_TASKPRI, value);
1285 * After a crash, we no longer service the interrupts and a pending
1286 * interrupt from previous kernel might still have ISR bit set.
1288 * Most probably by now CPU has serviced that pending interrupt and
1289 * it might not have done the ack_APIC_irq() because it thought,
1290 * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
1291 * does not clear the ISR bit and cpu thinks it has already serivced
1292 * the interrupt. Hence a vector might get locked. It was noticed
1293 * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
1297 for (i = APIC_ISR_NR - 1; i >= 0; i--)
1298 queued |= apic_read(APIC_IRR + i*0x10);
1300 for (i = APIC_ISR_NR - 1; i >= 0; i--) {
1301 value = apic_read(APIC_ISR + i*0x10);
1302 for (j = 31; j >= 0; j--) {
1303 if (value & (1<<j)) {
1310 printk(KERN_ERR "LAPIC pending interrupts after %d EOI\n",
1316 max_loops = (cpu_khz << 10) - (ntsc - tsc);
1319 } while (queued && max_loops > 0);
1320 WARN_ON(max_loops <= 0);
1323 * Now that we are all set up, enable the APIC
1325 value = apic_read(APIC_SPIV);
1326 value &= ~APIC_VECTOR_MASK;
1330 value |= APIC_SPIV_APIC_ENABLED;
1332 #ifdef CONFIG_X86_32
1334 * Some unknown Intel IO/APIC (or APIC) errata is biting us with
1335 * certain networking cards. If high frequency interrupts are
1336 * happening on a particular IOAPIC pin, plus the IOAPIC routing
1337 * entry is masked/unmasked at a high rate as well then sooner or
1338 * later IOAPIC line gets 'stuck', no more interrupts are received
1339 * from the device. If focus CPU is disabled then the hang goes
1342 * [ This bug can be reproduced easily with a level-triggered
1343 * PCI Ne2000 networking cards and PII/PIII processors, dual
1347 * Actually disabling the focus CPU check just makes the hang less
1348 * frequent as it makes the interrupt distributon model be more
1349 * like LRU than MRU (the short-term load is more even across CPUs).
1350 * See also the comment in end_level_ioapic_irq(). --macro
1354 * - enable focus processor (bit==0)
1355 * - 64bit mode always use processor focus
1356 * so no need to set it
1358 value &= ~APIC_SPIV_FOCUS_DISABLED;
1362 * Set spurious IRQ vector
1364 value |= SPURIOUS_APIC_VECTOR;
1365 apic_write(APIC_SPIV, value);
1368 * Set up LVT0, LVT1:
1370 * set up through-local-APIC on the BP's LINT0. This is not
1371 * strictly necessary in pure symmetric-IO mode, but sometimes
1372 * we delegate interrupts to the 8259A.
1375 * TODO: set up through-local-APIC from through-I/O-APIC? --macro
1377 value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
1378 if (!cpu && (pic_mode || !value)) {
1379 value = APIC_DM_EXTINT;
1380 apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n", cpu);
1382 value = APIC_DM_EXTINT | APIC_LVT_MASKED;
1383 apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n", cpu);
1385 apic_write(APIC_LVT0, value);
1388 * only the BP should see the LINT1 NMI signal, obviously.
1391 value = APIC_DM_NMI;
1393 value = APIC_DM_NMI | APIC_LVT_MASKED;
1394 if (!lapic_is_integrated()) /* 82489DX */
1395 value |= APIC_LVT_LEVEL_TRIGGER;
1396 apic_write(APIC_LVT1, value);
1398 #ifdef CONFIG_X86_MCE_INTEL
1399 /* Recheck CMCI information after local APIC is up on CPU #0 */
1405 void __cpuinit end_local_APIC_setup(void)
1409 #ifdef CONFIG_X86_32
1412 /* Disable the local apic timer */
1413 value = apic_read(APIC_LVTT);
1414 value |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
1415 apic_write(APIC_LVTT, value);
1422 void __init bsp_end_local_APIC_setup(void)
1424 end_local_APIC_setup();
1427 * Now that local APIC setup is completed for BP, configure the fault
1428 * handling for interrupt remapping.
1430 if (intr_remapping_enabled)
1431 enable_drhd_fault_handling();
1435 #ifdef CONFIG_X86_X2APIC
1437 * Need to disable xapic and x2apic at the same time and then enable xapic mode
1439 static inline void __disable_x2apic(u64 msr)
1441 wrmsrl(MSR_IA32_APICBASE,
1442 msr & ~(X2APIC_ENABLE | XAPIC_ENABLE));
1443 wrmsrl(MSR_IA32_APICBASE, msr & ~X2APIC_ENABLE);
1446 static void disable_x2apic(void)
1450 if (!cpu_has_x2apic)
1453 rdmsrl(MSR_IA32_APICBASE, msr);
1454 if (msr & X2APIC_ENABLE) {
1455 u32 x2apic_id = read_apic_id();
1457 if (x2apic_id >= 255)
1458 panic("Cannot disable x2apic, id: %08x\n", x2apic_id);
1460 pr_info("Disabling x2apic\n");
1461 __disable_x2apic(msr);
1463 x2apic_disabled = 1;
1466 register_lapic_address(mp_lapic_addr);
1470 void check_x2apic(void)
1472 if (x2apic_enabled()) {
1473 pr_info("x2apic enabled by BIOS, switching to x2apic ops\n");
1474 x2apic_preenabled = x2apic_mode = 1;
1478 void enable_x2apic(void)
1482 rdmsrl(MSR_IA32_APICBASE, msr);
1483 if (x2apic_disabled) {
1484 __disable_x2apic(msr);
1491 if (!(msr & X2APIC_ENABLE)) {
1492 printk_once(KERN_INFO "Enabling x2apic\n");
1493 wrmsrl(MSR_IA32_APICBASE, msr | X2APIC_ENABLE);
1496 #endif /* CONFIG_X86_X2APIC */
1498 int __init enable_IR(void)
1500 #ifdef CONFIG_IRQ_REMAP
1501 if (!intr_remapping_supported()) {
1502 pr_debug("intr-remapping not supported\n");
1506 if (!x2apic_preenabled && skip_ioapic_setup) {
1507 pr_info("Skipped enabling intr-remap because of skipping "
1512 return enable_intr_remapping();
1517 void __init enable_IR_x2apic(void)
1519 unsigned long flags;
1520 int ret, x2apic_enabled = 0;
1521 int dmar_table_init_ret;
1523 dmar_table_init_ret = dmar_table_init();
1524 if (dmar_table_init_ret && !x2apic_supported())
1527 ret = save_ioapic_entries();
1529 pr_info("Saving IO-APIC state failed: %d\n", ret);
1533 local_irq_save(flags);
1534 legacy_pic->mask_all();
1535 mask_ioapic_entries();
1537 if (dmar_table_init_ret)
1542 if (!x2apic_supported())
1546 /* IR is required if there is APIC ID > 255 even when running
1549 if (max_physical_apicid > 255 ||
1550 !hypervisor_x2apic_available()) {
1551 if (x2apic_preenabled)
1556 * without IR all CPUs can be addressed by IOAPIC/MSI
1557 * only in physical mode
1559 x2apic_force_phys();
1562 if (ret == IRQ_REMAP_XAPIC_MODE) {
1563 pr_info("x2apic not enabled, IRQ remapping is in xapic mode\n");
1569 if (x2apic_supported() && !x2apic_mode) {
1572 pr_info("Enabled x2apic\n");
1576 if (ret < 0) /* IR enabling failed */
1577 restore_ioapic_entries();
1578 legacy_pic->restore_mask();
1579 local_irq_restore(flags);
1582 #ifdef CONFIG_X86_64
1584 * Detect and enable local APICs on non-SMP boards.
1585 * Original code written by Keir Fraser.
1586 * On AMD64 we trust the BIOS - if it says no APIC it is likely
1587 * not correctly set up (usually the APIC timer won't work etc.)
1589 static int __init detect_init_APIC(void)
1591 if (!cpu_has_apic) {
1592 pr_info("No local APIC present\n");
1596 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
1601 static int __init apic_verify(void)
1606 * The APIC feature bit should now be enabled
1609 features = cpuid_edx(1);
1610 if (!(features & (1 << X86_FEATURE_APIC))) {
1611 pr_warning("Could not enable APIC!\n");
1614 set_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
1615 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
1617 /* The BIOS may have set up the APIC at some other address */
1618 rdmsr(MSR_IA32_APICBASE, l, h);
1619 if (l & MSR_IA32_APICBASE_ENABLE)
1620 mp_lapic_addr = l & MSR_IA32_APICBASE_BASE;
1622 pr_info("Found and enabled local APIC!\n");
1626 int __init apic_force_enable(unsigned long addr)
1634 * Some BIOSes disable the local APIC in the APIC_BASE
1635 * MSR. This can only be done in software for Intel P6 or later
1636 * and AMD K7 (Model > 1) or later.
1638 rdmsr(MSR_IA32_APICBASE, l, h);
1639 if (!(l & MSR_IA32_APICBASE_ENABLE)) {
1640 pr_info("Local APIC disabled by BIOS -- reenabling.\n");
1641 l &= ~MSR_IA32_APICBASE_BASE;
1642 l |= MSR_IA32_APICBASE_ENABLE | addr;
1643 wrmsr(MSR_IA32_APICBASE, l, h);
1644 enabled_via_apicbase = 1;
1646 return apic_verify();
1650 * Detect and initialize APIC
1652 static int __init detect_init_APIC(void)
1654 /* Disabled by kernel option? */
1658 switch (boot_cpu_data.x86_vendor) {
1659 case X86_VENDOR_AMD:
1660 if ((boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model > 1) ||
1661 (boot_cpu_data.x86 >= 15))
1664 case X86_VENDOR_INTEL:
1665 if (boot_cpu_data.x86 == 6 || boot_cpu_data.x86 == 15 ||
1666 (boot_cpu_data.x86 == 5 && cpu_has_apic))
1673 if (!cpu_has_apic) {
1675 * Over-ride BIOS and try to enable the local APIC only if
1676 * "lapic" specified.
1678 if (!force_enable_local_apic) {
1679 pr_info("Local APIC disabled by BIOS -- "
1680 "you can enable it with \"lapic\"\n");
1683 if (apic_force_enable(APIC_DEFAULT_PHYS_BASE))
1695 pr_info("No local APIC present or hardware disabled\n");
1701 * init_apic_mappings - initialize APIC mappings
1703 void __init init_apic_mappings(void)
1705 unsigned int new_apicid;
1708 boot_cpu_physical_apicid = read_apic_id();
1712 /* If no local APIC can be found return early */
1713 if (!smp_found_config && detect_init_APIC()) {
1714 /* lets NOP'ify apic operations */
1715 pr_info("APIC: disable apic facility\n");
1718 apic_phys = mp_lapic_addr;
1721 * acpi lapic path already maps that address in
1722 * acpi_register_lapic_address()
1724 if (!acpi_lapic && !smp_found_config)
1725 register_lapic_address(apic_phys);
1729 * Fetch the APIC ID of the BSP in case we have a
1730 * default configuration (or the MP table is broken).
1732 new_apicid = read_apic_id();
1733 if (boot_cpu_physical_apicid != new_apicid) {
1734 boot_cpu_physical_apicid = new_apicid;
1736 * yeah -- we lie about apic_version
1737 * in case if apic was disabled via boot option
1738 * but it's not a problem for SMP compiled kernel
1739 * since smp_sanity_check is prepared for such a case
1740 * and disable smp mode
1742 apic_version[new_apicid] =
1743 GET_APIC_VERSION(apic_read(APIC_LVR));
1747 void __init register_lapic_address(unsigned long address)
1749 mp_lapic_addr = address;
1752 set_fixmap_nocache(FIX_APIC_BASE, address);
1753 apic_printk(APIC_VERBOSE, "mapped APIC to %16lx (%16lx)\n",
1754 APIC_BASE, mp_lapic_addr);
1756 if (boot_cpu_physical_apicid == -1U) {
1757 boot_cpu_physical_apicid = read_apic_id();
1758 apic_version[boot_cpu_physical_apicid] =
1759 GET_APIC_VERSION(apic_read(APIC_LVR));
1764 * This initializes the IO-APIC and APIC hardware if this is
1767 int apic_version[MAX_LOCAL_APIC];
1769 int __init APIC_init_uniprocessor(void)
1772 pr_info("Apic disabled\n");
1775 #ifdef CONFIG_X86_64
1776 if (!cpu_has_apic) {
1778 pr_info("Apic disabled by BIOS\n");
1782 if (!smp_found_config && !cpu_has_apic)
1786 * Complain if the BIOS pretends there is one.
1788 if (!cpu_has_apic &&
1789 APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid])) {
1790 pr_err("BIOS bug, local APIC 0x%x not detected!...\n",
1791 boot_cpu_physical_apicid);
1796 default_setup_apic_routing();
1798 verify_local_APIC();
1801 #ifdef CONFIG_X86_64
1802 apic_write(APIC_ID, SET_APIC_ID(boot_cpu_physical_apicid));
1805 * Hack: In case of kdump, after a crash, kernel might be booting
1806 * on a cpu with non-zero lapic id. But boot_cpu_physical_apicid
1807 * might be zero if read from MP tables. Get it from LAPIC.
1809 # ifdef CONFIG_CRASH_DUMP
1810 boot_cpu_physical_apicid = read_apic_id();
1813 physid_set_mask_of_physid(boot_cpu_physical_apicid, &phys_cpu_present_map);
1816 #ifdef CONFIG_X86_IO_APIC
1818 * Now enable IO-APICs, actually call clear_IO_APIC
1819 * We need clear_IO_APIC before enabling error vector
1821 if (!skip_ioapic_setup && nr_ioapics)
1825 bsp_end_local_APIC_setup();
1827 #ifdef CONFIG_X86_IO_APIC
1828 if (smp_found_config && !skip_ioapic_setup && nr_ioapics)
1835 x86_init.timers.setup_percpu_clockev();
1840 * Local APIC interrupts
1844 * This interrupt should _never_ happen with our APIC/SMP architecture
1846 void smp_spurious_interrupt(struct pt_regs *regs)
1853 * Check if this really is a spurious interrupt and ACK it
1854 * if it is a vectored one. Just in case...
1855 * Spurious interrupts should not be ACKed.
1857 v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
1858 if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
1861 inc_irq_stat(irq_spurious_count);
1863 /* see sw-dev-man vol 3, chapter 7.4.13.5 */
1864 pr_info("spurious APIC interrupt on CPU#%d, "
1865 "should never happen.\n", smp_processor_id());
1870 * This interrupt should never happen with our APIC/SMP architecture
1872 void smp_error_interrupt(struct pt_regs *regs)
1876 static const char * const error_interrupt_reason[] = {
1877 "Send CS error", /* APIC Error Bit 0 */
1878 "Receive CS error", /* APIC Error Bit 1 */
1879 "Send accept error", /* APIC Error Bit 2 */
1880 "Receive accept error", /* APIC Error Bit 3 */
1881 "Redirectable IPI", /* APIC Error Bit 4 */
1882 "Send illegal vector", /* APIC Error Bit 5 */
1883 "Received illegal vector", /* APIC Error Bit 6 */
1884 "Illegal register address", /* APIC Error Bit 7 */
1889 /* First tickle the hardware, only then report what went on. -- REW */
1890 v0 = apic_read(APIC_ESR);
1891 apic_write(APIC_ESR, 0);
1892 v1 = apic_read(APIC_ESR);
1894 atomic_inc(&irq_err_count);
1896 apic_printk(APIC_DEBUG, KERN_DEBUG "APIC error on CPU%d: %02x(%02x)",
1897 smp_processor_id(), v0 , v1);
1902 apic_printk(APIC_DEBUG, KERN_CONT " : %s", error_interrupt_reason[i]);
1907 apic_printk(APIC_DEBUG, KERN_CONT "\n");
1913 * connect_bsp_APIC - attach the APIC to the interrupt system
1915 void __init connect_bsp_APIC(void)
1917 #ifdef CONFIG_X86_32
1920 * Do not trust the local APIC being empty at bootup.
1924 * PIC mode, enable APIC mode in the IMCR, i.e. connect BSP's
1925 * local APIC to INT and NMI lines.
1927 apic_printk(APIC_VERBOSE, "leaving PIC mode, "
1928 "enabling APIC mode.\n");
1932 if (apic->enable_apic_mode)
1933 apic->enable_apic_mode();
1937 * disconnect_bsp_APIC - detach the APIC from the interrupt system
1938 * @virt_wire_setup: indicates, whether virtual wire mode is selected
1940 * Virtual wire mode is necessary to deliver legacy interrupts even when the
1943 void disconnect_bsp_APIC(int virt_wire_setup)
1947 #ifdef CONFIG_X86_32
1950 * Put the board back into PIC mode (has an effect only on
1951 * certain older boards). Note that APIC interrupts, including
1952 * IPIs, won't work beyond this point! The only exception are
1955 apic_printk(APIC_VERBOSE, "disabling APIC mode, "
1956 "entering PIC mode.\n");
1962 /* Go back to Virtual Wire compatibility mode */
1964 /* For the spurious interrupt use vector F, and enable it */
1965 value = apic_read(APIC_SPIV);
1966 value &= ~APIC_VECTOR_MASK;
1967 value |= APIC_SPIV_APIC_ENABLED;
1969 apic_write(APIC_SPIV, value);
1971 if (!virt_wire_setup) {
1973 * For LVT0 make it edge triggered, active high,
1974 * external and enabled
1976 value = apic_read(APIC_LVT0);
1977 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
1978 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
1979 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
1980 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1981 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
1982 apic_write(APIC_LVT0, value);
1985 apic_write(APIC_LVT0, APIC_LVT_MASKED);
1989 * For LVT1 make it edge triggered, active high,
1992 value = apic_read(APIC_LVT1);
1993 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
1994 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
1995 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
1996 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1997 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
1998 apic_write(APIC_LVT1, value);
2001 void __cpuinit generic_processor_info(int apicid, int version)
2003 int cpu, max = nr_cpu_ids;
2004 bool boot_cpu_detected = physid_isset(boot_cpu_physical_apicid,
2005 phys_cpu_present_map);
2008 * If boot cpu has not been detected yet, then only allow upto
2009 * nr_cpu_ids - 1 processors and keep one slot free for boot cpu
2011 if (!boot_cpu_detected && num_processors >= nr_cpu_ids - 1 &&
2012 apicid != boot_cpu_physical_apicid) {
2013 int thiscpu = max + disabled_cpus - 1;
2016 "ACPI: NR_CPUS/possible_cpus limit of %i almost"
2017 " reached. Keeping one slot for boot cpu."
2018 " Processor %d/0x%x ignored.\n", max, thiscpu, apicid);
2024 if (num_processors >= nr_cpu_ids) {
2025 int thiscpu = max + disabled_cpus;
2028 "ACPI: NR_CPUS/possible_cpus limit of %i reached."
2029 " Processor %d/0x%x ignored.\n", max, thiscpu, apicid);
2036 if (apicid == boot_cpu_physical_apicid) {
2038 * x86_bios_cpu_apicid is required to have processors listed
2039 * in same order as logical cpu numbers. Hence the first
2040 * entry is BSP, and so on.
2041 * boot_cpu_init() already hold bit 0 in cpu_present_mask
2046 cpu = cpumask_next_zero(-1, cpu_present_mask);
2051 if (version == 0x0) {
2052 pr_warning("BIOS bug: APIC version is 0 for CPU %d/0x%x, fixing up to 0x10\n",
2056 apic_version[apicid] = version;
2058 if (version != apic_version[boot_cpu_physical_apicid]) {
2059 pr_warning("BIOS bug: APIC version mismatch, boot CPU: %x, CPU %d: version %x\n",
2060 apic_version[boot_cpu_physical_apicid], cpu, version);
2063 physid_set(apicid, phys_cpu_present_map);
2064 if (apicid > max_physical_apicid)
2065 max_physical_apicid = apicid;
2067 #if defined(CONFIG_SMP) || defined(CONFIG_X86_64)
2068 early_per_cpu(x86_cpu_to_apicid, cpu) = apicid;
2069 early_per_cpu(x86_bios_cpu_apicid, cpu) = apicid;
2071 #ifdef CONFIG_X86_32
2072 early_per_cpu(x86_cpu_to_logical_apicid, cpu) =
2073 apic->x86_32_early_logical_apicid(cpu);
2075 set_cpu_possible(cpu, true);
2076 set_cpu_present(cpu, true);
2079 int hard_smp_processor_id(void)
2081 return read_apic_id();
2084 void default_init_apic_ldr(void)
2088 apic_write(APIC_DFR, APIC_DFR_VALUE);
2089 val = apic_read(APIC_LDR) & ~APIC_LDR_MASK;
2090 val |= SET_APIC_LOGICAL_ID(1UL << smp_processor_id());
2091 apic_write(APIC_LDR, val);
2101 * 'active' is true if the local APIC was enabled by us and
2102 * not the BIOS; this signifies that we are also responsible
2103 * for disabling it before entering apm/acpi suspend
2106 /* r/w apic fields */
2107 unsigned int apic_id;
2108 unsigned int apic_taskpri;
2109 unsigned int apic_ldr;
2110 unsigned int apic_dfr;
2111 unsigned int apic_spiv;
2112 unsigned int apic_lvtt;
2113 unsigned int apic_lvtpc;
2114 unsigned int apic_lvt0;
2115 unsigned int apic_lvt1;
2116 unsigned int apic_lvterr;
2117 unsigned int apic_tmict;
2118 unsigned int apic_tdcr;
2119 unsigned int apic_thmr;
2122 static int lapic_suspend(void)
2124 unsigned long flags;
2127 if (!apic_pm_state.active)
2130 maxlvt = lapic_get_maxlvt();
2132 apic_pm_state.apic_id = apic_read(APIC_ID);
2133 apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
2134 apic_pm_state.apic_ldr = apic_read(APIC_LDR);
2135 apic_pm_state.apic_dfr = apic_read(APIC_DFR);
2136 apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
2137 apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
2139 apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
2140 apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
2141 apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
2142 apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
2143 apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
2144 apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
2145 #ifdef CONFIG_X86_THERMAL_VECTOR
2147 apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
2150 local_irq_save(flags);
2151 disable_local_APIC();
2153 if (intr_remapping_enabled)
2154 disable_intr_remapping();
2156 local_irq_restore(flags);
2160 static void lapic_resume(void)
2163 unsigned long flags;
2166 if (!apic_pm_state.active)
2169 local_irq_save(flags);
2170 if (intr_remapping_enabled) {
2172 * IO-APIC and PIC have their own resume routines.
2173 * We just mask them here to make sure the interrupt
2174 * subsystem is completely quiet while we enable x2apic
2175 * and interrupt-remapping.
2177 mask_ioapic_entries();
2178 legacy_pic->mask_all();
2185 * Make sure the APICBASE points to the right address
2187 * FIXME! This will be wrong if we ever support suspend on
2188 * SMP! We'll need to do this as part of the CPU restore!
2190 rdmsr(MSR_IA32_APICBASE, l, h);
2191 l &= ~MSR_IA32_APICBASE_BASE;
2192 l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
2193 wrmsr(MSR_IA32_APICBASE, l, h);
2196 maxlvt = lapic_get_maxlvt();
2197 apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
2198 apic_write(APIC_ID, apic_pm_state.apic_id);
2199 apic_write(APIC_DFR, apic_pm_state.apic_dfr);
2200 apic_write(APIC_LDR, apic_pm_state.apic_ldr);
2201 apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
2202 apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
2203 apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
2204 apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
2205 #if defined(CONFIG_X86_MCE_P4THERMAL) || defined(CONFIG_X86_MCE_INTEL)
2207 apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
2210 apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
2211 apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
2212 apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
2213 apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
2214 apic_write(APIC_ESR, 0);
2215 apic_read(APIC_ESR);
2216 apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
2217 apic_write(APIC_ESR, 0);
2218 apic_read(APIC_ESR);
2220 if (intr_remapping_enabled)
2221 reenable_intr_remapping(x2apic_mode);
2223 local_irq_restore(flags);
2227 * This device has no shutdown method - fully functioning local APICs
2228 * are needed on every CPU up until machine_halt/restart/poweroff.
2231 static struct syscore_ops lapic_syscore_ops = {
2232 .resume = lapic_resume,
2233 .suspend = lapic_suspend,
2236 static void __cpuinit apic_pm_activate(void)
2238 apic_pm_state.active = 1;
2241 static int __init init_lapic_sysfs(void)
2243 /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
2245 register_syscore_ops(&lapic_syscore_ops);
2250 /* local apic needs to resume before other devices access its registers. */
2251 core_initcall(init_lapic_sysfs);
2253 #else /* CONFIG_PM */
2255 static void apic_pm_activate(void) { }
2257 #endif /* CONFIG_PM */
2259 #ifdef CONFIG_X86_64
2261 static int __cpuinit apic_cluster_num(void)
2263 int i, clusters, zeros;
2265 u16 *bios_cpu_apicid;
2266 DECLARE_BITMAP(clustermap, NUM_APIC_CLUSTERS);
2268 bios_cpu_apicid = early_per_cpu_ptr(x86_bios_cpu_apicid);
2269 bitmap_zero(clustermap, NUM_APIC_CLUSTERS);
2271 for (i = 0; i < nr_cpu_ids; i++) {
2272 /* are we being called early in kernel startup? */
2273 if (bios_cpu_apicid) {
2274 id = bios_cpu_apicid[i];
2275 } else if (i < nr_cpu_ids) {
2277 id = per_cpu(x86_bios_cpu_apicid, i);
2283 if (id != BAD_APICID)
2284 __set_bit(APIC_CLUSTERID(id), clustermap);
2287 /* Problem: Partially populated chassis may not have CPUs in some of
2288 * the APIC clusters they have been allocated. Only present CPUs have
2289 * x86_bios_cpu_apicid entries, thus causing zeroes in the bitmap.
2290 * Since clusters are allocated sequentially, count zeros only if
2291 * they are bounded by ones.
2295 for (i = 0; i < NUM_APIC_CLUSTERS; i++) {
2296 if (test_bit(i, clustermap)) {
2297 clusters += 1 + zeros;
2306 static int __cpuinitdata multi_checked;
2307 static int __cpuinitdata multi;
2309 static int __cpuinit set_multi(const struct dmi_system_id *d)
2313 pr_info("APIC: %s detected, Multi Chassis\n", d->ident);
2318 static const __cpuinitconst struct dmi_system_id multi_dmi_table[] = {
2320 .callback = set_multi,
2321 .ident = "IBM System Summit2",
2323 DMI_MATCH(DMI_SYS_VENDOR, "IBM"),
2324 DMI_MATCH(DMI_PRODUCT_NAME, "Summit2"),
2330 static void __cpuinit dmi_check_multi(void)
2335 dmi_check_system(multi_dmi_table);
2340 * apic_is_clustered_box() -- Check if we can expect good TSC
2342 * Thus far, the major user of this is IBM's Summit2 series:
2343 * Clustered boxes may have unsynced TSC problems if they are
2345 * Use DMI to check them
2347 __cpuinit int apic_is_clustered_box(void)
2357 * ScaleMP vSMPowered boxes have one cluster per board and TSCs are
2358 * not guaranteed to be synced between boards
2360 if (apic_cluster_num() > 1)
2368 * APIC command line parameters
2370 static int __init setup_disableapic(char *arg)
2373 setup_clear_cpu_cap(X86_FEATURE_APIC);
2376 early_param("disableapic", setup_disableapic);
2378 /* same as disableapic, for compatibility */
2379 static int __init setup_nolapic(char *arg)
2381 return setup_disableapic(arg);
2383 early_param("nolapic", setup_nolapic);
2385 static int __init parse_lapic_timer_c2_ok(char *arg)
2387 local_apic_timer_c2_ok = 1;
2390 early_param("lapic_timer_c2_ok", parse_lapic_timer_c2_ok);
2392 static int __init parse_disable_apic_timer(char *arg)
2394 disable_apic_timer = 1;
2397 early_param("noapictimer", parse_disable_apic_timer);
2399 static int __init parse_nolapic_timer(char *arg)
2401 disable_apic_timer = 1;
2404 early_param("nolapic_timer", parse_nolapic_timer);
2406 static int __init apic_set_verbosity(char *arg)
2409 #ifdef CONFIG_X86_64
2410 skip_ioapic_setup = 0;
2416 if (strcmp("debug", arg) == 0)
2417 apic_verbosity = APIC_DEBUG;
2418 else if (strcmp("verbose", arg) == 0)
2419 apic_verbosity = APIC_VERBOSE;
2421 pr_warning("APIC Verbosity level %s not recognised"
2422 " use apic=verbose or apic=debug\n", arg);
2428 early_param("apic", apic_set_verbosity);
2430 static int __init lapic_insert_resource(void)
2435 /* Put local APIC into the resource map. */
2436 lapic_resource.start = apic_phys;
2437 lapic_resource.end = lapic_resource.start + PAGE_SIZE - 1;
2438 insert_resource(&iomem_resource, &lapic_resource);
2444 * need call insert after e820_reserve_resources()
2445 * that is using request_resource
2447 late_initcall(lapic_insert_resource);