2 * Local APIC handling, local APIC timers
4 * (c) 1999, 2000 Ingo Molnar <mingo@redhat.com>
7 * Maciej W. Rozycki : Bits for genuine 82489DX APICs;
8 * thanks to Eric Gilmore
10 * for testing these extensively.
11 * Maciej W. Rozycki : Various updates and fixes.
12 * Mikael Pettersson : Power Management for UP-APIC.
14 * Mikael Pettersson : PM converted to driver model.
17 #include <linux/init.h>
20 #include <linux/delay.h>
21 #include <linux/bootmem.h>
22 #include <linux/interrupt.h>
23 #include <linux/mc146818rtc.h>
24 #include <linux/kernel_stat.h>
25 #include <linux/sysdev.h>
26 #include <linux/ioport.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>
32 #include <linux/dmar.h>
33 #include <linux/ftrace.h>
34 #include <linux/smp.h>
35 #include <linux/nmi.h>
36 #include <linux/timex.h>
38 #include <asm/atomic.h>
40 #include <asm/mpspec.h>
42 #include <asm/arch_hooks.h>
44 #include <asm/pgalloc.h>
45 #include <asm/i8253.h>
47 #include <asm/proto.h>
49 #include <asm/i8259.h>
52 #include <mach_apic.h>
58 #if ((SPURIOUS_APIC_VECTOR & 0x0F) != 0x0F)
59 # error SPURIOUS_APIC_VECTOR definition error
62 unsigned int num_processors;
63 unsigned disabled_cpus __cpuinitdata;
64 /* Processor that is doing the boot up */
65 unsigned int boot_cpu_physical_apicid = -1U;
66 EXPORT_SYMBOL(boot_cpu_physical_apicid);
67 unsigned int max_physical_apicid;
69 /* Bitmask of physically existing CPUs */
70 physid_mask_t phys_cpu_present_map;
73 * Map cpu index to physical APIC ID
75 DEFINE_EARLY_PER_CPU(u16, x86_cpu_to_apicid, BAD_APICID);
76 DEFINE_EARLY_PER_CPU(u16, x86_bios_cpu_apicid, BAD_APICID);
77 EXPORT_EARLY_PER_CPU_SYMBOL(x86_cpu_to_apicid);
78 EXPORT_EARLY_PER_CPU_SYMBOL(x86_bios_cpu_apicid);
82 * Knob to control our willingness to enable the local APIC.
86 static int force_enable_local_apic;
88 * APIC command line parameters
90 static int __init parse_lapic(char *arg)
92 force_enable_local_apic = 1;
95 early_param("lapic", parse_lapic);
96 /* Local APIC was disabled by the BIOS and enabled by the kernel */
97 static int enabled_via_apicbase;
102 static int apic_calibrate_pmtmr __initdata;
103 static __init int setup_apicpmtimer(char *s)
105 apic_calibrate_pmtmr = 1;
109 __setup("apicpmtimer", setup_apicpmtimer);
118 /* x2apic enabled before OS handover */
119 static int x2apic_preenabled;
120 static int disable_x2apic;
121 static __init int setup_nox2apic(char *str)
124 setup_clear_cpu_cap(X86_FEATURE_X2APIC);
127 early_param("nox2apic", setup_nox2apic);
130 unsigned long mp_lapic_addr;
132 /* Disable local APIC timer from the kernel commandline or via dmi quirk */
133 static int disable_apic_timer __cpuinitdata;
134 /* Local APIC timer works in C2 */
135 int local_apic_timer_c2_ok;
136 EXPORT_SYMBOL_GPL(local_apic_timer_c2_ok);
138 int first_system_vector = 0xfe;
141 * Debug level, exported for io_apic.c
143 unsigned int apic_verbosity;
147 /* Have we found an MP table */
148 int smp_found_config;
150 static struct resource lapic_resource = {
151 .name = "Local APIC",
152 .flags = IORESOURCE_MEM | IORESOURCE_BUSY,
155 static unsigned int calibration_result;
157 static int lapic_next_event(unsigned long delta,
158 struct clock_event_device *evt);
159 static void lapic_timer_setup(enum clock_event_mode mode,
160 struct clock_event_device *evt);
161 static void lapic_timer_broadcast(const struct cpumask *mask);
162 static void apic_pm_activate(void);
165 * The local apic timer can be used for any function which is CPU local.
167 static struct clock_event_device lapic_clockevent = {
169 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT
170 | CLOCK_EVT_FEAT_C3STOP | CLOCK_EVT_FEAT_DUMMY,
172 .set_mode = lapic_timer_setup,
173 .set_next_event = lapic_next_event,
174 .broadcast = lapic_timer_broadcast,
178 static DEFINE_PER_CPU(struct clock_event_device, lapic_events);
180 static unsigned long apic_phys;
183 * Get the LAPIC version
185 static inline int lapic_get_version(void)
187 return GET_APIC_VERSION(apic_read(APIC_LVR));
191 * Check, if the APIC is integrated or a separate chip
193 static inline int lapic_is_integrated(void)
198 return APIC_INTEGRATED(lapic_get_version());
203 * Check, whether this is a modern or a first generation APIC
205 static int modern_apic(void)
207 /* AMD systems use old APIC versions, so check the CPU */
208 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
209 boot_cpu_data.x86 >= 0xf)
211 return lapic_get_version() >= 0x14;
215 * Paravirt kernels also might be using these below ops. So we still
216 * use generic apic_read()/apic_write(), which might be pointing to different
217 * ops in PARAVIRT case.
219 void xapic_wait_icr_idle(void)
221 while (apic_read(APIC_ICR) & APIC_ICR_BUSY)
225 u32 safe_xapic_wait_icr_idle(void)
232 send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
236 } while (timeout++ < 1000);
241 void xapic_icr_write(u32 low, u32 id)
243 apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(id));
244 apic_write(APIC_ICR, low);
247 static u64 xapic_icr_read(void)
251 icr2 = apic_read(APIC_ICR2);
252 icr1 = apic_read(APIC_ICR);
254 return icr1 | ((u64)icr2 << 32);
257 static struct apic_ops xapic_ops = {
258 .read = native_apic_mem_read,
259 .write = native_apic_mem_write,
260 .icr_read = xapic_icr_read,
261 .icr_write = xapic_icr_write,
262 .wait_icr_idle = xapic_wait_icr_idle,
263 .safe_wait_icr_idle = safe_xapic_wait_icr_idle,
266 struct apic_ops __read_mostly *apic_ops = &xapic_ops;
267 EXPORT_SYMBOL_GPL(apic_ops);
270 static void x2apic_wait_icr_idle(void)
272 /* no need to wait for icr idle in x2apic */
276 static u32 safe_x2apic_wait_icr_idle(void)
278 /* no need to wait for icr idle in x2apic */
282 void x2apic_icr_write(u32 low, u32 id)
284 wrmsrl(APIC_BASE_MSR + (APIC_ICR >> 4), ((__u64) id) << 32 | low);
287 static u64 x2apic_icr_read(void)
291 rdmsrl(APIC_BASE_MSR + (APIC_ICR >> 4), val);
295 static struct apic_ops x2apic_ops = {
296 .read = native_apic_msr_read,
297 .write = native_apic_msr_write,
298 .icr_read = x2apic_icr_read,
299 .icr_write = x2apic_icr_write,
300 .wait_icr_idle = x2apic_wait_icr_idle,
301 .safe_wait_icr_idle = safe_x2apic_wait_icr_idle,
306 * enable_NMI_through_LVT0 - enable NMI through local vector table 0
308 void __cpuinit enable_NMI_through_LVT0(void)
312 /* unmask and set to NMI */
315 /* Level triggered for 82489DX (32bit mode) */
316 if (!lapic_is_integrated())
317 v |= APIC_LVT_LEVEL_TRIGGER;
319 apic_write(APIC_LVT0, v);
324 * get_physical_broadcast - Get number of physical broadcast IDs
326 int get_physical_broadcast(void)
328 return modern_apic() ? 0xff : 0xf;
333 * lapic_get_maxlvt - get the maximum number of local vector table entries
335 int lapic_get_maxlvt(void)
339 v = apic_read(APIC_LVR);
341 * - we always have APIC integrated on 64bit mode
342 * - 82489DXs do not report # of LVT entries
344 return APIC_INTEGRATED(GET_APIC_VERSION(v)) ? GET_APIC_MAXLVT(v) : 2;
352 #define APIC_DIVISOR 16
355 * This function sets up the local APIC timer, with a timeout of
356 * 'clocks' APIC bus clock. During calibration we actually call
357 * this function twice on the boot CPU, once with a bogus timeout
358 * value, second time for real. The other (noncalibrating) CPUs
359 * call this function only once, with the real, calibrated value.
361 * We do reads before writes even if unnecessary, to get around the
362 * P5 APIC double write bug.
364 static void __setup_APIC_LVTT(unsigned int clocks, int oneshot, int irqen)
366 unsigned int lvtt_value, tmp_value;
368 lvtt_value = LOCAL_TIMER_VECTOR;
370 lvtt_value |= APIC_LVT_TIMER_PERIODIC;
371 if (!lapic_is_integrated())
372 lvtt_value |= SET_APIC_TIMER_BASE(APIC_TIMER_BASE_DIV);
375 lvtt_value |= APIC_LVT_MASKED;
377 apic_write(APIC_LVTT, lvtt_value);
382 tmp_value = apic_read(APIC_TDCR);
383 apic_write(APIC_TDCR,
384 (tmp_value & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE)) |
388 apic_write(APIC_TMICT, clocks / APIC_DIVISOR);
392 * Setup extended LVT, AMD specific (K8, family 10h)
394 * Vector mappings are hard coded. On K8 only offset 0 (APIC500) and
395 * MCE interrupts are supported. Thus MCE offset must be set to 0.
397 * If mask=1, the LVT entry does not generate interrupts while mask=0
398 * enables the vector. See also the BKDGs.
401 #define APIC_EILVT_LVTOFF_MCE 0
402 #define APIC_EILVT_LVTOFF_IBS 1
404 static void setup_APIC_eilvt(u8 lvt_off, u8 vector, u8 msg_type, u8 mask)
406 unsigned long reg = (lvt_off << 4) + APIC_EILVT0;
407 unsigned int v = (mask << 16) | (msg_type << 8) | vector;
412 u8 setup_APIC_eilvt_mce(u8 vector, u8 msg_type, u8 mask)
414 setup_APIC_eilvt(APIC_EILVT_LVTOFF_MCE, vector, msg_type, mask);
415 return APIC_EILVT_LVTOFF_MCE;
418 u8 setup_APIC_eilvt_ibs(u8 vector, u8 msg_type, u8 mask)
420 setup_APIC_eilvt(APIC_EILVT_LVTOFF_IBS, vector, msg_type, mask);
421 return APIC_EILVT_LVTOFF_IBS;
423 EXPORT_SYMBOL_GPL(setup_APIC_eilvt_ibs);
426 * Program the next event, relative to now
428 static int lapic_next_event(unsigned long delta,
429 struct clock_event_device *evt)
431 apic_write(APIC_TMICT, delta);
436 * Setup the lapic timer in periodic or oneshot mode
438 static void lapic_timer_setup(enum clock_event_mode mode,
439 struct clock_event_device *evt)
444 /* Lapic used as dummy for broadcast ? */
445 if (evt->features & CLOCK_EVT_FEAT_DUMMY)
448 local_irq_save(flags);
451 case CLOCK_EVT_MODE_PERIODIC:
452 case CLOCK_EVT_MODE_ONESHOT:
453 __setup_APIC_LVTT(calibration_result,
454 mode != CLOCK_EVT_MODE_PERIODIC, 1);
456 case CLOCK_EVT_MODE_UNUSED:
457 case CLOCK_EVT_MODE_SHUTDOWN:
458 v = apic_read(APIC_LVTT);
459 v |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
460 apic_write(APIC_LVTT, v);
461 apic_write(APIC_TMICT, 0xffffffff);
463 case CLOCK_EVT_MODE_RESUME:
464 /* Nothing to do here */
468 local_irq_restore(flags);
472 * Local APIC timer broadcast function
474 static void lapic_timer_broadcast(const struct cpumask *mask)
477 apic->send_IPI_mask(mask, LOCAL_TIMER_VECTOR);
482 * Setup the local APIC timer for this CPU. Copy the initilized values
483 * of the boot CPU and register the clock event in the framework.
485 static void __cpuinit setup_APIC_timer(void)
487 struct clock_event_device *levt = &__get_cpu_var(lapic_events);
489 memcpy(levt, &lapic_clockevent, sizeof(*levt));
490 levt->cpumask = cpumask_of(smp_processor_id());
492 clockevents_register_device(levt);
496 * In this functions we calibrate APIC bus clocks to the external timer.
498 * We want to do the calibration only once since we want to have local timer
499 * irqs syncron. CPUs connected by the same APIC bus have the very same bus
502 * This was previously done by reading the PIT/HPET and waiting for a wrap
503 * around to find out, that a tick has elapsed. I have a box, where the PIT
504 * readout is broken, so it never gets out of the wait loop again. This was
505 * also reported by others.
507 * Monitoring the jiffies value is inaccurate and the clockevents
508 * infrastructure allows us to do a simple substitution of the interrupt
511 * The calibration routine also uses the pm_timer when possible, as the PIT
512 * happens to run way too slow (factor 2.3 on my VAIO CoreDuo, which goes
513 * back to normal later in the boot process).
516 #define LAPIC_CAL_LOOPS (HZ/10)
518 static __initdata int lapic_cal_loops = -1;
519 static __initdata long lapic_cal_t1, lapic_cal_t2;
520 static __initdata unsigned long long lapic_cal_tsc1, lapic_cal_tsc2;
521 static __initdata unsigned long lapic_cal_pm1, lapic_cal_pm2;
522 static __initdata unsigned long lapic_cal_j1, lapic_cal_j2;
525 * Temporary interrupt handler.
527 static void __init lapic_cal_handler(struct clock_event_device *dev)
529 unsigned long long tsc = 0;
530 long tapic = apic_read(APIC_TMCCT);
531 unsigned long pm = acpi_pm_read_early();
536 switch (lapic_cal_loops++) {
538 lapic_cal_t1 = tapic;
539 lapic_cal_tsc1 = tsc;
541 lapic_cal_j1 = jiffies;
544 case LAPIC_CAL_LOOPS:
545 lapic_cal_t2 = tapic;
546 lapic_cal_tsc2 = tsc;
547 if (pm < lapic_cal_pm1)
548 pm += ACPI_PM_OVRRUN;
550 lapic_cal_j2 = jiffies;
555 static int __init calibrate_by_pmtimer(long deltapm, long *delta)
557 const long pm_100ms = PMTMR_TICKS_PER_SEC / 10;
558 const long pm_thresh = pm_100ms / 100;
562 #ifndef CONFIG_X86_PM_TIMER
566 apic_printk(APIC_VERBOSE, "... PM timer delta = %ld\n", deltapm);
568 /* Check, if the PM timer is available */
572 mult = clocksource_hz2mult(PMTMR_TICKS_PER_SEC, 22);
574 if (deltapm > (pm_100ms - pm_thresh) &&
575 deltapm < (pm_100ms + pm_thresh)) {
576 apic_printk(APIC_VERBOSE, "... PM timer result ok\n");
578 res = (((u64)deltapm) * mult) >> 22;
579 do_div(res, 1000000);
580 pr_warning("APIC calibration not consistent "
581 "with PM Timer: %ldms instead of 100ms\n",
583 /* Correct the lapic counter value */
584 res = (((u64)(*delta)) * pm_100ms);
585 do_div(res, deltapm);
586 pr_info("APIC delta adjusted to PM-Timer: "
587 "%lu (%ld)\n", (unsigned long)res, *delta);
594 static int __init calibrate_APIC_clock(void)
596 struct clock_event_device *levt = &__get_cpu_var(lapic_events);
597 void (*real_handler)(struct clock_event_device *dev);
598 unsigned long deltaj;
600 int pm_referenced = 0;
604 /* Replace the global interrupt handler */
605 real_handler = global_clock_event->event_handler;
606 global_clock_event->event_handler = lapic_cal_handler;
609 * Setup the APIC counter to maximum. There is no way the lapic
610 * can underflow in the 100ms detection time frame
612 __setup_APIC_LVTT(0xffffffff, 0, 0);
614 /* Let the interrupts run */
617 while (lapic_cal_loops <= LAPIC_CAL_LOOPS)
622 /* Restore the real event handler */
623 global_clock_event->event_handler = real_handler;
625 /* Build delta t1-t2 as apic timer counts down */
626 delta = lapic_cal_t1 - lapic_cal_t2;
627 apic_printk(APIC_VERBOSE, "... lapic delta = %ld\n", delta);
629 /* we trust the PM based calibration if possible */
630 pm_referenced = !calibrate_by_pmtimer(lapic_cal_pm2 - lapic_cal_pm1,
633 /* Calculate the scaled math multiplication factor */
634 lapic_clockevent.mult = div_sc(delta, TICK_NSEC * LAPIC_CAL_LOOPS,
635 lapic_clockevent.shift);
636 lapic_clockevent.max_delta_ns =
637 clockevent_delta2ns(0x7FFFFF, &lapic_clockevent);
638 lapic_clockevent.min_delta_ns =
639 clockevent_delta2ns(0xF, &lapic_clockevent);
641 calibration_result = (delta * APIC_DIVISOR) / LAPIC_CAL_LOOPS;
643 apic_printk(APIC_VERBOSE, "..... delta %ld\n", delta);
644 apic_printk(APIC_VERBOSE, "..... mult: %ld\n", lapic_clockevent.mult);
645 apic_printk(APIC_VERBOSE, "..... calibration result: %u\n",
649 delta = (long)(lapic_cal_tsc2 - lapic_cal_tsc1);
650 apic_printk(APIC_VERBOSE, "..... CPU clock speed is "
652 (delta / LAPIC_CAL_LOOPS) / (1000000 / HZ),
653 (delta / LAPIC_CAL_LOOPS) % (1000000 / HZ));
656 apic_printk(APIC_VERBOSE, "..... host bus clock speed is "
658 calibration_result / (1000000 / HZ),
659 calibration_result % (1000000 / HZ));
662 * Do a sanity check on the APIC calibration result
664 if (calibration_result < (1000000 / HZ)) {
666 pr_warning("APIC frequency too slow, disabling apic timer\n");
670 levt->features &= ~CLOCK_EVT_FEAT_DUMMY;
673 * PM timer calibration failed or not turned on
674 * so lets try APIC timer based calibration
676 if (!pm_referenced) {
677 apic_printk(APIC_VERBOSE, "... verify APIC timer\n");
680 * Setup the apic timer manually
682 levt->event_handler = lapic_cal_handler;
683 lapic_timer_setup(CLOCK_EVT_MODE_PERIODIC, levt);
684 lapic_cal_loops = -1;
686 /* Let the interrupts run */
689 while (lapic_cal_loops <= LAPIC_CAL_LOOPS)
692 /* Stop the lapic timer */
693 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, levt);
696 deltaj = lapic_cal_j2 - lapic_cal_j1;
697 apic_printk(APIC_VERBOSE, "... jiffies delta = %lu\n", deltaj);
699 /* Check, if the jiffies result is consistent */
700 if (deltaj >= LAPIC_CAL_LOOPS-2 && deltaj <= LAPIC_CAL_LOOPS+2)
701 apic_printk(APIC_VERBOSE, "... jiffies result ok\n");
703 levt->features |= CLOCK_EVT_FEAT_DUMMY;
707 if (levt->features & CLOCK_EVT_FEAT_DUMMY) {
708 pr_warning("APIC timer disabled due to verification failure\n");
716 * Setup the boot APIC
718 * Calibrate and verify the result.
720 void __init setup_boot_APIC_clock(void)
723 * The local apic timer can be disabled via the kernel
724 * commandline or from the CPU detection code. Register the lapic
725 * timer as a dummy clock event source on SMP systems, so the
726 * broadcast mechanism is used. On UP systems simply ignore it.
728 if (disable_apic_timer) {
729 pr_info("Disabling APIC timer\n");
730 /* No broadcast on UP ! */
731 if (num_possible_cpus() > 1) {
732 lapic_clockevent.mult = 1;
738 apic_printk(APIC_VERBOSE, "Using local APIC timer interrupts.\n"
739 "calibrating APIC timer ...\n");
741 if (calibrate_APIC_clock()) {
742 /* No broadcast on UP ! */
743 if (num_possible_cpus() > 1)
749 * If nmi_watchdog is set to IO_APIC, we need the
750 * PIT/HPET going. Otherwise register lapic as a dummy
753 if (nmi_watchdog != NMI_IO_APIC)
754 lapic_clockevent.features &= ~CLOCK_EVT_FEAT_DUMMY;
756 pr_warning("APIC timer registered as dummy,"
757 " due to nmi_watchdog=%d!\n", nmi_watchdog);
759 /* Setup the lapic or request the broadcast */
763 void __cpuinit setup_secondary_APIC_clock(void)
769 * The guts of the apic timer interrupt
771 static void local_apic_timer_interrupt(void)
773 int cpu = smp_processor_id();
774 struct clock_event_device *evt = &per_cpu(lapic_events, cpu);
777 * Normally we should not be here till LAPIC has been initialized but
778 * in some cases like kdump, its possible that there is a pending LAPIC
779 * timer interrupt from previous kernel's context and is delivered in
780 * new kernel the moment interrupts are enabled.
782 * Interrupts are enabled early and LAPIC is setup much later, hence
783 * its possible that when we get here evt->event_handler is NULL.
784 * Check for event_handler being NULL and discard the interrupt as
787 if (!evt->event_handler) {
788 pr_warning("Spurious LAPIC timer interrupt on cpu %d\n", cpu);
790 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, evt);
795 * the NMI deadlock-detector uses this.
797 inc_irq_stat(apic_timer_irqs);
799 evt->event_handler(evt);
803 * Local APIC timer interrupt. This is the most natural way for doing
804 * local interrupts, but local timer interrupts can be emulated by
805 * broadcast interrupts too. [in case the hw doesn't support APIC timers]
807 * [ if a single-CPU system runs an SMP kernel then we call the local
808 * interrupt as well. Thus we cannot inline the local irq ... ]
810 void __irq_entry smp_apic_timer_interrupt(struct pt_regs *regs)
812 struct pt_regs *old_regs = set_irq_regs(regs);
815 * NOTE! We'd better ACK the irq immediately,
816 * because timer handling can be slow.
820 * update_process_times() expects us to have done irq_enter().
821 * Besides, if we don't timer interrupts ignore the global
822 * interrupt lock, which is the WrongThing (tm) to do.
826 local_apic_timer_interrupt();
829 set_irq_regs(old_regs);
832 int setup_profiling_timer(unsigned int multiplier)
838 * Local APIC start and shutdown
842 * clear_local_APIC - shutdown the local APIC
844 * This is called, when a CPU is disabled and before rebooting, so the state of
845 * the local APIC has no dangling leftovers. Also used to cleanout any BIOS
846 * leftovers during boot.
848 void clear_local_APIC(void)
853 /* APIC hasn't been mapped yet */
857 maxlvt = lapic_get_maxlvt();
859 * Masking an LVT entry can trigger a local APIC error
860 * if the vector is zero. Mask LVTERR first to prevent this.
863 v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
864 apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
867 * Careful: we have to set masks only first to deassert
868 * any level-triggered sources.
870 v = apic_read(APIC_LVTT);
871 apic_write(APIC_LVTT, v | APIC_LVT_MASKED);
872 v = apic_read(APIC_LVT0);
873 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
874 v = apic_read(APIC_LVT1);
875 apic_write(APIC_LVT1, v | APIC_LVT_MASKED);
877 v = apic_read(APIC_LVTPC);
878 apic_write(APIC_LVTPC, v | APIC_LVT_MASKED);
881 /* lets not touch this if we didn't frob it */
882 #if defined(CONFIG_X86_MCE_P4THERMAL) || defined(X86_MCE_INTEL)
884 v = apic_read(APIC_LVTTHMR);
885 apic_write(APIC_LVTTHMR, v | APIC_LVT_MASKED);
889 * Clean APIC state for other OSs:
891 apic_write(APIC_LVTT, APIC_LVT_MASKED);
892 apic_write(APIC_LVT0, APIC_LVT_MASKED);
893 apic_write(APIC_LVT1, APIC_LVT_MASKED);
895 apic_write(APIC_LVTERR, APIC_LVT_MASKED);
897 apic_write(APIC_LVTPC, APIC_LVT_MASKED);
899 /* Integrated APIC (!82489DX) ? */
900 if (lapic_is_integrated()) {
902 /* Clear ESR due to Pentium errata 3AP and 11AP */
903 apic_write(APIC_ESR, 0);
909 * disable_local_APIC - clear and disable the local APIC
911 void disable_local_APIC(void)
915 /* APIC hasn't been mapped yet */
922 * Disable APIC (implies clearing of registers
925 value = apic_read(APIC_SPIV);
926 value &= ~APIC_SPIV_APIC_ENABLED;
927 apic_write(APIC_SPIV, value);
931 * When LAPIC was disabled by the BIOS and enabled by the kernel,
932 * restore the disabled state.
934 if (enabled_via_apicbase) {
937 rdmsr(MSR_IA32_APICBASE, l, h);
938 l &= ~MSR_IA32_APICBASE_ENABLE;
939 wrmsr(MSR_IA32_APICBASE, l, h);
945 * If Linux enabled the LAPIC against the BIOS default disable it down before
946 * re-entering the BIOS on shutdown. Otherwise the BIOS may get confused and
947 * not power-off. Additionally clear all LVT entries before disable_local_APIC
948 * for the case where Linux didn't enable the LAPIC.
950 void lapic_shutdown(void)
957 local_irq_save(flags);
960 if (!enabled_via_apicbase)
964 disable_local_APIC();
967 local_irq_restore(flags);
971 * This is to verify that we're looking at a real local APIC.
972 * Check these against your board if the CPUs aren't getting
973 * started for no apparent reason.
975 int __init verify_local_APIC(void)
977 unsigned int reg0, reg1;
980 * The version register is read-only in a real APIC.
982 reg0 = apic_read(APIC_LVR);
983 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
984 apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
985 reg1 = apic_read(APIC_LVR);
986 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
989 * The two version reads above should print the same
990 * numbers. If the second one is different, then we
991 * poke at a non-APIC.
997 * Check if the version looks reasonably.
999 reg1 = GET_APIC_VERSION(reg0);
1000 if (reg1 == 0x00 || reg1 == 0xff)
1002 reg1 = lapic_get_maxlvt();
1003 if (reg1 < 0x02 || reg1 == 0xff)
1007 * The ID register is read/write in a real APIC.
1009 reg0 = apic_read(APIC_ID);
1010 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
1011 apic_write(APIC_ID, reg0 ^ apic->apic_id_mask);
1012 reg1 = apic_read(APIC_ID);
1013 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg1);
1014 apic_write(APIC_ID, reg0);
1015 if (reg1 != (reg0 ^ apic->apic_id_mask))
1019 * The next two are just to see if we have sane values.
1020 * They're only really relevant if we're in Virtual Wire
1021 * compatibility mode, but most boxes are anymore.
1023 reg0 = apic_read(APIC_LVT0);
1024 apic_printk(APIC_DEBUG, "Getting LVT0: %x\n", reg0);
1025 reg1 = apic_read(APIC_LVT1);
1026 apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
1032 * sync_Arb_IDs - synchronize APIC bus arbitration IDs
1034 void __init sync_Arb_IDs(void)
1037 * Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 And not
1040 if (modern_apic() || boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
1046 apic_wait_icr_idle();
1048 apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
1049 apic_write(APIC_ICR, APIC_DEST_ALLINC |
1050 APIC_INT_LEVELTRIG | APIC_DM_INIT);
1054 * An initial setup of the virtual wire mode.
1056 void __init init_bsp_APIC(void)
1061 * Don't do the setup now if we have a SMP BIOS as the
1062 * through-I/O-APIC virtual wire mode might be active.
1064 if (smp_found_config || !cpu_has_apic)
1068 * Do not trust the local APIC being empty at bootup.
1075 value = apic_read(APIC_SPIV);
1076 value &= ~APIC_VECTOR_MASK;
1077 value |= APIC_SPIV_APIC_ENABLED;
1079 #ifdef CONFIG_X86_32
1080 /* This bit is reserved on P4/Xeon and should be cleared */
1081 if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) &&
1082 (boot_cpu_data.x86 == 15))
1083 value &= ~APIC_SPIV_FOCUS_DISABLED;
1086 value |= APIC_SPIV_FOCUS_DISABLED;
1087 value |= SPURIOUS_APIC_VECTOR;
1088 apic_write(APIC_SPIV, value);
1091 * Set up the virtual wire mode.
1093 apic_write(APIC_LVT0, APIC_DM_EXTINT);
1094 value = APIC_DM_NMI;
1095 if (!lapic_is_integrated()) /* 82489DX */
1096 value |= APIC_LVT_LEVEL_TRIGGER;
1097 apic_write(APIC_LVT1, value);
1100 static void __cpuinit lapic_setup_esr(void)
1102 unsigned int oldvalue, value, maxlvt;
1104 if (!lapic_is_integrated()) {
1105 pr_info("No ESR for 82489DX.\n");
1109 if (apic->disable_esr) {
1111 * Something untraceable is creating bad interrupts on
1112 * secondary quads ... for the moment, just leave the
1113 * ESR disabled - we can't do anything useful with the
1114 * errors anyway - mbligh
1116 pr_info("Leaving ESR disabled.\n");
1120 maxlvt = lapic_get_maxlvt();
1121 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
1122 apic_write(APIC_ESR, 0);
1123 oldvalue = apic_read(APIC_ESR);
1125 /* enables sending errors */
1126 value = ERROR_APIC_VECTOR;
1127 apic_write(APIC_LVTERR, value);
1130 * spec says clear errors after enabling vector.
1133 apic_write(APIC_ESR, 0);
1134 value = apic_read(APIC_ESR);
1135 if (value != oldvalue)
1136 apic_printk(APIC_VERBOSE, "ESR value before enabling "
1137 "vector: 0x%08x after: 0x%08x\n",
1143 * setup_local_APIC - setup the local APIC
1145 void __cpuinit setup_local_APIC(void)
1151 #ifdef CONFIG_X86_IO_APIC
1152 disable_ioapic_setup();
1157 #ifdef CONFIG_X86_32
1158 /* Pound the ESR really hard over the head with a big hammer - mbligh */
1159 if (lapic_is_integrated() && apic->disable_esr) {
1160 apic_write(APIC_ESR, 0);
1161 apic_write(APIC_ESR, 0);
1162 apic_write(APIC_ESR, 0);
1163 apic_write(APIC_ESR, 0);
1170 * Double-check whether this APIC is really registered.
1171 * This is meaningless in clustered apic mode, so we skip it.
1173 if (!apic->apic_id_registered())
1177 * Intel recommends to set DFR, LDR and TPR before enabling
1178 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
1179 * document number 292116). So here it goes...
1181 apic->init_apic_ldr();
1184 * Set Task Priority to 'accept all'. We never change this
1187 value = apic_read(APIC_TASKPRI);
1188 value &= ~APIC_TPRI_MASK;
1189 apic_write(APIC_TASKPRI, value);
1192 * After a crash, we no longer service the interrupts and a pending
1193 * interrupt from previous kernel might still have ISR bit set.
1195 * Most probably by now CPU has serviced that pending interrupt and
1196 * it might not have done the ack_APIC_irq() because it thought,
1197 * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
1198 * does not clear the ISR bit and cpu thinks it has already serivced
1199 * the interrupt. Hence a vector might get locked. It was noticed
1200 * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
1202 for (i = APIC_ISR_NR - 1; i >= 0; i--) {
1203 value = apic_read(APIC_ISR + i*0x10);
1204 for (j = 31; j >= 0; j--) {
1211 * Now that we are all set up, enable the APIC
1213 value = apic_read(APIC_SPIV);
1214 value &= ~APIC_VECTOR_MASK;
1218 value |= APIC_SPIV_APIC_ENABLED;
1220 #ifdef CONFIG_X86_32
1222 * Some unknown Intel IO/APIC (or APIC) errata is biting us with
1223 * certain networking cards. If high frequency interrupts are
1224 * happening on a particular IOAPIC pin, plus the IOAPIC routing
1225 * entry is masked/unmasked at a high rate as well then sooner or
1226 * later IOAPIC line gets 'stuck', no more interrupts are received
1227 * from the device. If focus CPU is disabled then the hang goes
1230 * [ This bug can be reproduced easily with a level-triggered
1231 * PCI Ne2000 networking cards and PII/PIII processors, dual
1235 * Actually disabling the focus CPU check just makes the hang less
1236 * frequent as it makes the interrupt distributon model be more
1237 * like LRU than MRU (the short-term load is more even across CPUs).
1238 * See also the comment in end_level_ioapic_irq(). --macro
1242 * - enable focus processor (bit==0)
1243 * - 64bit mode always use processor focus
1244 * so no need to set it
1246 value &= ~APIC_SPIV_FOCUS_DISABLED;
1250 * Set spurious IRQ vector
1252 value |= SPURIOUS_APIC_VECTOR;
1253 apic_write(APIC_SPIV, value);
1256 * Set up LVT0, LVT1:
1258 * set up through-local-APIC on the BP's LINT0. This is not
1259 * strictly necessary in pure symmetric-IO mode, but sometimes
1260 * we delegate interrupts to the 8259A.
1263 * TODO: set up through-local-APIC from through-I/O-APIC? --macro
1265 value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
1266 if (!smp_processor_id() && (pic_mode || !value)) {
1267 value = APIC_DM_EXTINT;
1268 apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n",
1269 smp_processor_id());
1271 value = APIC_DM_EXTINT | APIC_LVT_MASKED;
1272 apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n",
1273 smp_processor_id());
1275 apic_write(APIC_LVT0, value);
1278 * only the BP should see the LINT1 NMI signal, obviously.
1280 if (!smp_processor_id())
1281 value = APIC_DM_NMI;
1283 value = APIC_DM_NMI | APIC_LVT_MASKED;
1284 if (!lapic_is_integrated()) /* 82489DX */
1285 value |= APIC_LVT_LEVEL_TRIGGER;
1286 apic_write(APIC_LVT1, value);
1291 void __cpuinit end_local_APIC_setup(void)
1295 #ifdef CONFIG_X86_32
1298 /* Disable the local apic timer */
1299 value = apic_read(APIC_LVTT);
1300 value |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
1301 apic_write(APIC_LVTT, value);
1305 setup_apic_nmi_watchdog(NULL);
1310 void check_x2apic(void)
1314 rdmsr(MSR_IA32_APICBASE, msr, msr2);
1316 if (msr & X2APIC_ENABLE) {
1317 pr_info("x2apic enabled by BIOS, switching to x2apic ops\n");
1318 x2apic_preenabled = x2apic = 1;
1319 apic_ops = &x2apic_ops;
1323 void enable_x2apic(void)
1327 rdmsr(MSR_IA32_APICBASE, msr, msr2);
1328 if (!(msr & X2APIC_ENABLE)) {
1329 pr_info("Enabling x2apic\n");
1330 wrmsr(MSR_IA32_APICBASE, msr | X2APIC_ENABLE, 0);
1334 void __init enable_IR_x2apic(void)
1336 #ifdef CONFIG_INTR_REMAP
1338 unsigned long flags;
1340 if (!cpu_has_x2apic)
1343 if (!x2apic_preenabled && disable_x2apic) {
1344 pr_info("Skipped enabling x2apic and Interrupt-remapping "
1345 "because of nox2apic\n");
1349 if (x2apic_preenabled && disable_x2apic)
1350 panic("Bios already enabled x2apic, can't enforce nox2apic");
1352 if (!x2apic_preenabled && skip_ioapic_setup) {
1353 pr_info("Skipped enabling x2apic and Interrupt-remapping "
1354 "because of skipping io-apic setup\n");
1358 ret = dmar_table_init();
1360 pr_info("dmar_table_init() failed with %d:\n", ret);
1362 if (x2apic_preenabled)
1363 panic("x2apic enabled by bios. But IR enabling failed");
1365 pr_info("Not enabling x2apic,Intr-remapping\n");
1369 local_irq_save(flags);
1372 ret = save_mask_IO_APIC_setup();
1374 pr_info("Saving IO-APIC state failed: %d\n", ret);
1378 ret = enable_intr_remapping(1);
1380 if (ret && x2apic_preenabled) {
1381 local_irq_restore(flags);
1382 panic("x2apic enabled by bios. But IR enabling failed");
1390 apic_ops = &x2apic_ops;
1397 * IR enabling failed
1399 restore_IO_APIC_setup();
1401 reinit_intr_remapped_IO_APIC(x2apic_preenabled);
1405 local_irq_restore(flags);
1408 if (!x2apic_preenabled)
1409 pr_info("Enabled x2apic and interrupt-remapping\n");
1411 pr_info("Enabled Interrupt-remapping\n");
1413 pr_err("Failed to enable Interrupt-remapping and x2apic\n");
1415 if (!cpu_has_x2apic)
1418 if (x2apic_preenabled)
1419 panic("x2apic enabled prior OS handover,"
1420 " enable CONFIG_INTR_REMAP");
1422 pr_info("Enable CONFIG_INTR_REMAP for enabling intr-remapping "
1428 #endif /* HAVE_X2APIC */
1430 #ifdef CONFIG_X86_64
1432 * Detect and enable local APICs on non-SMP boards.
1433 * Original code written by Keir Fraser.
1434 * On AMD64 we trust the BIOS - if it says no APIC it is likely
1435 * not correctly set up (usually the APIC timer won't work etc.)
1437 static int __init detect_init_APIC(void)
1439 if (!cpu_has_apic) {
1440 pr_info("No local APIC present\n");
1444 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
1445 boot_cpu_physical_apicid = 0;
1450 * Detect and initialize APIC
1452 static int __init detect_init_APIC(void)
1456 /* Disabled by kernel option? */
1460 switch (boot_cpu_data.x86_vendor) {
1461 case X86_VENDOR_AMD:
1462 if ((boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model > 1) ||
1463 (boot_cpu_data.x86 == 15))
1466 case X86_VENDOR_INTEL:
1467 if (boot_cpu_data.x86 == 6 || boot_cpu_data.x86 == 15 ||
1468 (boot_cpu_data.x86 == 5 && cpu_has_apic))
1475 if (!cpu_has_apic) {
1477 * Over-ride BIOS and try to enable the local APIC only if
1478 * "lapic" specified.
1480 if (!force_enable_local_apic) {
1481 pr_info("Local APIC disabled by BIOS -- "
1482 "you can enable it with \"lapic\"\n");
1486 * Some BIOSes disable the local APIC in the APIC_BASE
1487 * MSR. This can only be done in software for Intel P6 or later
1488 * and AMD K7 (Model > 1) or later.
1490 rdmsr(MSR_IA32_APICBASE, l, h);
1491 if (!(l & MSR_IA32_APICBASE_ENABLE)) {
1492 pr_info("Local APIC disabled by BIOS -- reenabling.\n");
1493 l &= ~MSR_IA32_APICBASE_BASE;
1494 l |= MSR_IA32_APICBASE_ENABLE | APIC_DEFAULT_PHYS_BASE;
1495 wrmsr(MSR_IA32_APICBASE, l, h);
1496 enabled_via_apicbase = 1;
1500 * The APIC feature bit should now be enabled
1503 features = cpuid_edx(1);
1504 if (!(features & (1 << X86_FEATURE_APIC))) {
1505 pr_warning("Could not enable APIC!\n");
1508 set_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
1509 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
1511 /* The BIOS may have set up the APIC at some other address */
1512 rdmsr(MSR_IA32_APICBASE, l, h);
1513 if (l & MSR_IA32_APICBASE_ENABLE)
1514 mp_lapic_addr = l & MSR_IA32_APICBASE_BASE;
1516 pr_info("Found and enabled local APIC!\n");
1523 pr_info("No local APIC present or hardware disabled\n");
1528 #ifdef CONFIG_X86_64
1529 void __init early_init_lapic_mapping(void)
1531 unsigned long phys_addr;
1534 * If no local APIC can be found then go out
1535 * : it means there is no mpatable and MADT
1537 if (!smp_found_config)
1540 phys_addr = mp_lapic_addr;
1542 set_fixmap_nocache(FIX_APIC_BASE, phys_addr);
1543 apic_printk(APIC_VERBOSE, "mapped APIC to %16lx (%16lx)\n",
1544 APIC_BASE, phys_addr);
1547 * Fetch the APIC ID of the BSP in case we have a
1548 * default configuration (or the MP table is broken).
1550 boot_cpu_physical_apicid = read_apic_id();
1555 * init_apic_mappings - initialize APIC mappings
1557 void __init init_apic_mappings(void)
1561 boot_cpu_physical_apicid = read_apic_id();
1567 * If no local APIC can be found then set up a fake all
1568 * zeroes page to simulate the local APIC and another
1569 * one for the IO-APIC.
1571 if (!smp_found_config && detect_init_APIC()) {
1572 apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
1573 apic_phys = __pa(apic_phys);
1575 apic_phys = mp_lapic_addr;
1577 set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
1578 apic_printk(APIC_VERBOSE, "mapped APIC to %08lx (%08lx)\n",
1579 APIC_BASE, apic_phys);
1582 * Fetch the APIC ID of the BSP in case we have a
1583 * default configuration (or the MP table is broken).
1585 if (boot_cpu_physical_apicid == -1U)
1586 boot_cpu_physical_apicid = read_apic_id();
1590 * This initializes the IO-APIC and APIC hardware if this is
1593 int apic_version[MAX_APICS];
1595 int __init APIC_init_uniprocessor(void)
1598 pr_info("Apic disabled\n");
1601 #ifdef CONFIG_X86_64
1602 if (!cpu_has_apic) {
1604 pr_info("Apic disabled by BIOS\n");
1608 if (!smp_found_config && !cpu_has_apic)
1612 * Complain if the BIOS pretends there is one.
1614 if (!cpu_has_apic &&
1615 APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid])) {
1616 pr_err("BIOS bug, local APIC 0x%x not detected!...\n",
1617 boot_cpu_physical_apicid);
1618 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_APIC);
1626 #ifdef CONFIG_X86_64
1627 default_setup_apic_routing();
1630 verify_local_APIC();
1633 #ifdef CONFIG_X86_64
1634 apic_write(APIC_ID, SET_APIC_ID(boot_cpu_physical_apicid));
1637 * Hack: In case of kdump, after a crash, kernel might be booting
1638 * on a cpu with non-zero lapic id. But boot_cpu_physical_apicid
1639 * might be zero if read from MP tables. Get it from LAPIC.
1641 # ifdef CONFIG_CRASH_DUMP
1642 boot_cpu_physical_apicid = read_apic_id();
1645 physid_set_mask_of_physid(boot_cpu_physical_apicid, &phys_cpu_present_map);
1648 #ifdef CONFIG_X86_64
1650 * Now enable IO-APICs, actually call clear_IO_APIC
1651 * We need clear_IO_APIC before enabling vector on BP
1653 if (!skip_ioapic_setup && nr_ioapics)
1657 #ifdef CONFIG_X86_IO_APIC
1658 if (!smp_found_config || skip_ioapic_setup || !nr_ioapics)
1660 localise_nmi_watchdog();
1661 end_local_APIC_setup();
1663 #ifdef CONFIG_X86_IO_APIC
1664 if (smp_found_config && !skip_ioapic_setup && nr_ioapics)
1666 # ifdef CONFIG_X86_64
1672 #ifdef CONFIG_X86_64
1673 setup_boot_APIC_clock();
1674 check_nmi_watchdog();
1683 * Local APIC interrupts
1687 * This interrupt should _never_ happen with our APIC/SMP architecture
1689 void smp_spurious_interrupt(struct pt_regs *regs)
1696 * Check if this really is a spurious interrupt and ACK it
1697 * if it is a vectored one. Just in case...
1698 * Spurious interrupts should not be ACKed.
1700 v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
1701 if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
1704 inc_irq_stat(irq_spurious_count);
1706 /* see sw-dev-man vol 3, chapter 7.4.13.5 */
1707 pr_info("spurious APIC interrupt on CPU#%d, "
1708 "should never happen.\n", smp_processor_id());
1713 * This interrupt should never happen with our APIC/SMP architecture
1715 void smp_error_interrupt(struct pt_regs *regs)
1721 /* First tickle the hardware, only then report what went on. -- REW */
1722 v = apic_read(APIC_ESR);
1723 apic_write(APIC_ESR, 0);
1724 v1 = apic_read(APIC_ESR);
1726 atomic_inc(&irq_err_count);
1729 * Here is what the APIC error bits mean:
1731 * 1: Receive CS error
1732 * 2: Send accept error
1733 * 3: Receive accept error
1735 * 5: Send illegal vector
1736 * 6: Received illegal vector
1737 * 7: Illegal register address
1739 pr_debug("APIC error on CPU%d: %02x(%02x)\n",
1740 smp_processor_id(), v , v1);
1745 * connect_bsp_APIC - attach the APIC to the interrupt system
1747 void __init connect_bsp_APIC(void)
1749 #ifdef CONFIG_X86_32
1752 * Do not trust the local APIC being empty at bootup.
1756 * PIC mode, enable APIC mode in the IMCR, i.e. connect BSP's
1757 * local APIC to INT and NMI lines.
1759 apic_printk(APIC_VERBOSE, "leaving PIC mode, "
1760 "enabling APIC mode.\n");
1765 if (apic->enable_apic_mode)
1766 apic->enable_apic_mode();
1770 * disconnect_bsp_APIC - detach the APIC from the interrupt system
1771 * @virt_wire_setup: indicates, whether virtual wire mode is selected
1773 * Virtual wire mode is necessary to deliver legacy interrupts even when the
1776 void disconnect_bsp_APIC(int virt_wire_setup)
1780 #ifdef CONFIG_X86_32
1783 * Put the board back into PIC mode (has an effect only on
1784 * certain older boards). Note that APIC interrupts, including
1785 * IPIs, won't work beyond this point! The only exception are
1788 apic_printk(APIC_VERBOSE, "disabling APIC mode, "
1789 "entering PIC mode.\n");
1796 /* Go back to Virtual Wire compatibility mode */
1798 /* For the spurious interrupt use vector F, and enable it */
1799 value = apic_read(APIC_SPIV);
1800 value &= ~APIC_VECTOR_MASK;
1801 value |= APIC_SPIV_APIC_ENABLED;
1803 apic_write(APIC_SPIV, value);
1805 if (!virt_wire_setup) {
1807 * For LVT0 make it edge triggered, active high,
1808 * external and enabled
1810 value = apic_read(APIC_LVT0);
1811 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
1812 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
1813 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
1814 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1815 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
1816 apic_write(APIC_LVT0, value);
1819 apic_write(APIC_LVT0, APIC_LVT_MASKED);
1823 * For LVT1 make it edge triggered, active high,
1826 value = apic_read(APIC_LVT1);
1827 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
1828 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
1829 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
1830 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1831 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
1832 apic_write(APIC_LVT1, value);
1835 void __cpuinit generic_processor_info(int apicid, int version)
1842 if (version == 0x0) {
1843 pr_warning("BIOS bug, APIC version is 0 for CPU#%d! "
1844 "fixing up to 0x10. (tell your hw vendor)\n",
1848 apic_version[apicid] = version;
1850 if (num_processors >= nr_cpu_ids) {
1851 int max = nr_cpu_ids;
1852 int thiscpu = max + disabled_cpus;
1855 "ACPI: NR_CPUS/possible_cpus limit of %i reached."
1856 " Processor %d/0x%x ignored.\n", max, thiscpu, apicid);
1863 cpu = cpumask_next_zero(-1, cpu_present_mask);
1865 if (version != apic_version[boot_cpu_physical_apicid])
1867 "ACPI: apic version mismatch, bootcpu: %x cpu %d: %x\n",
1868 apic_version[boot_cpu_physical_apicid], cpu, version);
1870 physid_set(apicid, phys_cpu_present_map);
1871 if (apicid == boot_cpu_physical_apicid) {
1873 * x86_bios_cpu_apicid is required to have processors listed
1874 * in same order as logical cpu numbers. Hence the first
1875 * entry is BSP, and so on.
1879 if (apicid > max_physical_apicid)
1880 max_physical_apicid = apicid;
1882 #ifdef CONFIG_X86_32
1884 * Would be preferable to switch to bigsmp when CONFIG_HOTPLUG_CPU=y
1885 * but we need to work other dependencies like SMP_SUSPEND etc
1886 * before this can be done without some confusion.
1887 * if (CPU_HOTPLUG_ENABLED || num_processors > 8)
1888 * - Ashok Raj <ashok.raj@intel.com>
1890 if (max_physical_apicid >= 8) {
1891 switch (boot_cpu_data.x86_vendor) {
1892 case X86_VENDOR_INTEL:
1893 if (!APIC_XAPIC(version)) {
1897 /* If P4 and above fall through */
1898 case X86_VENDOR_AMD:
1904 #if defined(CONFIG_X86_SMP) || defined(CONFIG_X86_64)
1905 early_per_cpu(x86_cpu_to_apicid, cpu) = apicid;
1906 early_per_cpu(x86_bios_cpu_apicid, cpu) = apicid;
1909 set_cpu_possible(cpu, true);
1910 set_cpu_present(cpu, true);
1913 #ifdef CONFIG_X86_64
1914 int hard_smp_processor_id(void)
1916 return read_apic_id();
1927 * 'active' is true if the local APIC was enabled by us and
1928 * not the BIOS; this signifies that we are also responsible
1929 * for disabling it before entering apm/acpi suspend
1932 /* r/w apic fields */
1933 unsigned int apic_id;
1934 unsigned int apic_taskpri;
1935 unsigned int apic_ldr;
1936 unsigned int apic_dfr;
1937 unsigned int apic_spiv;
1938 unsigned int apic_lvtt;
1939 unsigned int apic_lvtpc;
1940 unsigned int apic_lvt0;
1941 unsigned int apic_lvt1;
1942 unsigned int apic_lvterr;
1943 unsigned int apic_tmict;
1944 unsigned int apic_tdcr;
1945 unsigned int apic_thmr;
1948 static int lapic_suspend(struct sys_device *dev, pm_message_t state)
1950 unsigned long flags;
1953 if (!apic_pm_state.active)
1956 maxlvt = lapic_get_maxlvt();
1958 apic_pm_state.apic_id = apic_read(APIC_ID);
1959 apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
1960 apic_pm_state.apic_ldr = apic_read(APIC_LDR);
1961 apic_pm_state.apic_dfr = apic_read(APIC_DFR);
1962 apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
1963 apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
1965 apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
1966 apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
1967 apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
1968 apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
1969 apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
1970 apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
1971 #if defined(CONFIG_X86_MCE_P4THERMAL) || defined(CONFIG_X86_MCE_INTEL)
1973 apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
1976 local_irq_save(flags);
1977 disable_local_APIC();
1978 local_irq_restore(flags);
1982 static int lapic_resume(struct sys_device *dev)
1985 unsigned long flags;
1988 if (!apic_pm_state.active)
1991 maxlvt = lapic_get_maxlvt();
1993 local_irq_save(flags);
2002 * Make sure the APICBASE points to the right address
2004 * FIXME! This will be wrong if we ever support suspend on
2005 * SMP! We'll need to do this as part of the CPU restore!
2007 rdmsr(MSR_IA32_APICBASE, l, h);
2008 l &= ~MSR_IA32_APICBASE_BASE;
2009 l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
2010 wrmsr(MSR_IA32_APICBASE, l, h);
2013 apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
2014 apic_write(APIC_ID, apic_pm_state.apic_id);
2015 apic_write(APIC_DFR, apic_pm_state.apic_dfr);
2016 apic_write(APIC_LDR, apic_pm_state.apic_ldr);
2017 apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
2018 apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
2019 apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
2020 apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
2021 #if defined(CONFIG_X86_MCE_P4THERMAL) || defined(CONFIG_X86_MCE_INTEL)
2023 apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
2026 apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
2027 apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
2028 apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
2029 apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
2030 apic_write(APIC_ESR, 0);
2031 apic_read(APIC_ESR);
2032 apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
2033 apic_write(APIC_ESR, 0);
2034 apic_read(APIC_ESR);
2036 local_irq_restore(flags);
2042 * This device has no shutdown method - fully functioning local APICs
2043 * are needed on every CPU up until machine_halt/restart/poweroff.
2046 static struct sysdev_class lapic_sysclass = {
2048 .resume = lapic_resume,
2049 .suspend = lapic_suspend,
2052 static struct sys_device device_lapic = {
2054 .cls = &lapic_sysclass,
2057 static void __cpuinit apic_pm_activate(void)
2059 apic_pm_state.active = 1;
2062 static int __init init_lapic_sysfs(void)
2068 /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
2070 error = sysdev_class_register(&lapic_sysclass);
2072 error = sysdev_register(&device_lapic);
2075 device_initcall(init_lapic_sysfs);
2077 #else /* CONFIG_PM */
2079 static void apic_pm_activate(void) { }
2081 #endif /* CONFIG_PM */
2083 #ifdef CONFIG_X86_64
2085 * apic_is_clustered_box() -- Check if we can expect good TSC
2087 * Thus far, the major user of this is IBM's Summit2 series:
2089 * Clustered boxes may have unsynced TSC problems if they are
2090 * multi-chassis. Use available data to take a good guess.
2091 * If in doubt, go HPET.
2093 __cpuinit int apic_is_clustered_box(void)
2095 int i, clusters, zeros;
2097 u16 *bios_cpu_apicid;
2098 DECLARE_BITMAP(clustermap, NUM_APIC_CLUSTERS);
2101 * there is not this kind of box with AMD CPU yet.
2102 * Some AMD box with quadcore cpu and 8 sockets apicid
2103 * will be [4, 0x23] or [8, 0x27] could be thought to
2104 * vsmp box still need checking...
2106 if ((boot_cpu_data.x86_vendor == X86_VENDOR_AMD) && !is_vsmp_box())
2109 bios_cpu_apicid = early_per_cpu_ptr(x86_bios_cpu_apicid);
2110 bitmap_zero(clustermap, NUM_APIC_CLUSTERS);
2112 for (i = 0; i < nr_cpu_ids; i++) {
2113 /* are we being called early in kernel startup? */
2114 if (bios_cpu_apicid) {
2115 id = bios_cpu_apicid[i];
2116 } else if (i < nr_cpu_ids) {
2118 id = per_cpu(x86_bios_cpu_apicid, i);
2124 if (id != BAD_APICID)
2125 __set_bit(APIC_CLUSTERID(id), clustermap);
2128 /* Problem: Partially populated chassis may not have CPUs in some of
2129 * the APIC clusters they have been allocated. Only present CPUs have
2130 * x86_bios_cpu_apicid entries, thus causing zeroes in the bitmap.
2131 * Since clusters are allocated sequentially, count zeros only if
2132 * they are bounded by ones.
2136 for (i = 0; i < NUM_APIC_CLUSTERS; i++) {
2137 if (test_bit(i, clustermap)) {
2138 clusters += 1 + zeros;
2144 /* ScaleMP vSMPowered boxes have one cluster per board and TSCs are
2145 * not guaranteed to be synced between boards
2147 if (is_vsmp_box() && clusters > 1)
2151 * If clusters > 2, then should be multi-chassis.
2152 * May have to revisit this when multi-core + hyperthreaded CPUs come
2153 * out, but AFAIK this will work even for them.
2155 return (clusters > 2);
2160 * APIC command line parameters
2162 static int __init setup_disableapic(char *arg)
2165 setup_clear_cpu_cap(X86_FEATURE_APIC);
2168 early_param("disableapic", setup_disableapic);
2170 /* same as disableapic, for compatibility */
2171 static int __init setup_nolapic(char *arg)
2173 return setup_disableapic(arg);
2175 early_param("nolapic", setup_nolapic);
2177 static int __init parse_lapic_timer_c2_ok(char *arg)
2179 local_apic_timer_c2_ok = 1;
2182 early_param("lapic_timer_c2_ok", parse_lapic_timer_c2_ok);
2184 static int __init parse_disable_apic_timer(char *arg)
2186 disable_apic_timer = 1;
2189 early_param("noapictimer", parse_disable_apic_timer);
2191 static int __init parse_nolapic_timer(char *arg)
2193 disable_apic_timer = 1;
2196 early_param("nolapic_timer", parse_nolapic_timer);
2198 static int __init apic_set_verbosity(char *arg)
2201 #ifdef CONFIG_X86_64
2202 skip_ioapic_setup = 0;
2208 if (strcmp("debug", arg) == 0)
2209 apic_verbosity = APIC_DEBUG;
2210 else if (strcmp("verbose", arg) == 0)
2211 apic_verbosity = APIC_VERBOSE;
2213 pr_warning("APIC Verbosity level %s not recognised"
2214 " use apic=verbose or apic=debug\n", arg);
2220 early_param("apic", apic_set_verbosity);
2222 static int __init lapic_insert_resource(void)
2227 /* Put local APIC into the resource map. */
2228 lapic_resource.start = apic_phys;
2229 lapic_resource.end = lapic_resource.start + PAGE_SIZE - 1;
2230 insert_resource(&iomem_resource, &lapic_resource);
2236 * need call insert after e820_reserve_resources()
2237 * that is using request_resource
2239 late_initcall(lapic_insert_resource);