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/clockchips.h>
28 #include <linux/acpi_pmtmr.h>
29 #include <linux/module.h>
30 #include <linux/dmar.h>
32 #include <asm/atomic.h>
35 #include <asm/mpspec.h>
37 #include <asm/pgalloc.h>
40 #include <asm/proto.h>
41 #include <asm/timex.h>
43 #include <asm/i8259.h>
46 #include <mach_apic.h>
48 /* Disable local APIC timer from the kernel commandline or via dmi quirk */
49 static int disable_apic_timer __cpuinitdata;
50 static int apic_calibrate_pmtmr __initdata;
55 /* x2apic enabled before OS handover */
56 int x2apic_preenabled;
58 /* Local APIC timer works in C2 */
59 int local_apic_timer_c2_ok;
60 EXPORT_SYMBOL_GPL(local_apic_timer_c2_ok);
63 * Debug level, exported for io_apic.c
65 unsigned int apic_verbosity;
67 /* Have we found an MP table */
70 static struct resource lapic_resource = {
72 .flags = IORESOURCE_MEM | IORESOURCE_BUSY,
75 static unsigned int calibration_result;
77 static int lapic_next_event(unsigned long delta,
78 struct clock_event_device *evt);
79 static void lapic_timer_setup(enum clock_event_mode mode,
80 struct clock_event_device *evt);
81 static void lapic_timer_broadcast(cpumask_t mask);
82 static void apic_pm_activate(void);
84 static struct clock_event_device lapic_clockevent = {
86 .features = CLOCK_EVT_FEAT_PERIODIC | CLOCK_EVT_FEAT_ONESHOT
87 | CLOCK_EVT_FEAT_C3STOP | CLOCK_EVT_FEAT_DUMMY,
89 .set_mode = lapic_timer_setup,
90 .set_next_event = lapic_next_event,
91 .broadcast = lapic_timer_broadcast,
95 static DEFINE_PER_CPU(struct clock_event_device, lapic_events);
97 static unsigned long apic_phys;
99 unsigned long mp_lapic_addr;
101 unsigned int __cpuinitdata maxcpus = NR_CPUS;
103 * Get the LAPIC version
105 static inline int lapic_get_version(void)
107 return GET_APIC_VERSION(apic_read(APIC_LVR));
111 * Check, if the APIC is integrated or a seperate chip
113 static inline int lapic_is_integrated(void)
119 * Check, whether this is a modern or a first generation APIC
121 static int modern_apic(void)
123 /* AMD systems use old APIC versions, so check the CPU */
124 if (boot_cpu_data.x86_vendor == X86_VENDOR_AMD &&
125 boot_cpu_data.x86 >= 0xf)
127 return lapic_get_version() >= 0x14;
130 void xapic_wait_icr_idle(void)
132 while (apic_read(APIC_ICR) & APIC_ICR_BUSY)
136 u32 safe_xapic_wait_icr_idle(void)
143 send_status = apic_read(APIC_ICR) & APIC_ICR_BUSY;
147 } while (timeout++ < 1000);
152 void xapic_icr_write(u32 low, u32 id)
154 apic_write(APIC_ICR2, SET_APIC_DEST_FIELD(id));
155 apic_write(APIC_ICR, low);
158 u64 xapic_icr_read(void)
162 icr2 = apic_read(APIC_ICR2);
163 icr1 = apic_read(APIC_ICR);
165 return (icr1 | ((u64)icr2 << 32));
168 static struct apic_ops xapic_ops = {
169 .read = native_apic_mem_read,
170 .write = native_apic_mem_write,
171 .icr_read = xapic_icr_read,
172 .icr_write = xapic_icr_write,
173 .wait_icr_idle = xapic_wait_icr_idle,
174 .safe_wait_icr_idle = safe_xapic_wait_icr_idle,
177 struct apic_ops __read_mostly *apic_ops = &xapic_ops;
179 EXPORT_SYMBOL_GPL(apic_ops);
181 static void x2apic_wait_icr_idle(void)
183 /* no need to wait for icr idle in x2apic */
187 static u32 safe_x2apic_wait_icr_idle(void)
189 /* no need to wait for icr idle in x2apic */
193 void x2apic_icr_write(u32 low, u32 id)
195 wrmsrl(APIC_BASE_MSR + (APIC_ICR >> 4), ((__u64) id) << 32 | low);
198 u64 x2apic_icr_read(void)
202 rdmsrl(APIC_BASE_MSR + (APIC_ICR >> 4), val);
206 static struct apic_ops x2apic_ops = {
207 .read = native_apic_msr_read,
208 .write = native_apic_msr_write,
209 .icr_read = x2apic_icr_read,
210 .icr_write = x2apic_icr_write,
211 .wait_icr_idle = x2apic_wait_icr_idle,
212 .safe_wait_icr_idle = safe_x2apic_wait_icr_idle,
216 * enable_NMI_through_LVT0 - enable NMI through local vector table 0
218 void __cpuinit enable_NMI_through_LVT0(void)
222 /* unmask and set to NMI */
225 /* Level triggered for 82489DX (32bit mode) */
226 if (!lapic_is_integrated())
227 v |= APIC_LVT_LEVEL_TRIGGER;
229 apic_write(APIC_LVT0, v);
233 * lapic_get_maxlvt - get the maximum number of local vector table entries
235 int lapic_get_maxlvt(void)
239 v = apic_read(APIC_LVR);
241 * - we always have APIC integrated on 64bit mode
242 * - 82489DXs do not report # of LVT entries
244 return APIC_INTEGRATED(GET_APIC_VERSION(v)) ? GET_APIC_MAXLVT(v) : 2;
248 * This function sets up the local APIC timer, with a timeout of
249 * 'clocks' APIC bus clock. During calibration we actually call
250 * this function twice on the boot CPU, once with a bogus timeout
251 * value, second time for real. The other (noncalibrating) CPUs
252 * call this function only once, with the real, calibrated value.
254 * We do reads before writes even if unnecessary, to get around the
255 * P5 APIC double write bug.
258 static void __setup_APIC_LVTT(unsigned int clocks, int oneshot, int irqen)
260 unsigned int lvtt_value, tmp_value;
262 lvtt_value = LOCAL_TIMER_VECTOR;
264 lvtt_value |= APIC_LVT_TIMER_PERIODIC;
266 lvtt_value |= APIC_LVT_MASKED;
268 apic_write(APIC_LVTT, lvtt_value);
273 tmp_value = apic_read(APIC_TDCR);
274 apic_write(APIC_TDCR, (tmp_value
275 & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE))
279 apic_write(APIC_TMICT, clocks);
283 * Setup extended LVT, AMD specific (K8, family 10h)
285 * Vector mappings are hard coded. On K8 only offset 0 (APIC500) and
286 * MCE interrupts are supported. Thus MCE offset must be set to 0.
289 #define APIC_EILVT_LVTOFF_MCE 0
290 #define APIC_EILVT_LVTOFF_IBS 1
292 static void setup_APIC_eilvt(u8 lvt_off, u8 vector, u8 msg_type, u8 mask)
294 unsigned long reg = (lvt_off << 4) + APIC_EILVT0;
295 unsigned int v = (mask << 16) | (msg_type << 8) | vector;
300 u8 setup_APIC_eilvt_mce(u8 vector, u8 msg_type, u8 mask)
302 setup_APIC_eilvt(APIC_EILVT_LVTOFF_MCE, vector, msg_type, mask);
303 return APIC_EILVT_LVTOFF_MCE;
306 u8 setup_APIC_eilvt_ibs(u8 vector, u8 msg_type, u8 mask)
308 setup_APIC_eilvt(APIC_EILVT_LVTOFF_IBS, vector, msg_type, mask);
309 return APIC_EILVT_LVTOFF_IBS;
313 * Program the next event, relative to now
315 static int lapic_next_event(unsigned long delta,
316 struct clock_event_device *evt)
318 apic_write(APIC_TMICT, delta);
323 * Setup the lapic timer in periodic or oneshot mode
325 static void lapic_timer_setup(enum clock_event_mode mode,
326 struct clock_event_device *evt)
331 /* Lapic used as dummy for broadcast ? */
332 if (evt->features & CLOCK_EVT_FEAT_DUMMY)
335 local_irq_save(flags);
338 case CLOCK_EVT_MODE_PERIODIC:
339 case CLOCK_EVT_MODE_ONESHOT:
340 __setup_APIC_LVTT(calibration_result,
341 mode != CLOCK_EVT_MODE_PERIODIC, 1);
343 case CLOCK_EVT_MODE_UNUSED:
344 case CLOCK_EVT_MODE_SHUTDOWN:
345 v = apic_read(APIC_LVTT);
346 v |= (APIC_LVT_MASKED | LOCAL_TIMER_VECTOR);
347 apic_write(APIC_LVTT, v);
349 case CLOCK_EVT_MODE_RESUME:
350 /* Nothing to do here */
354 local_irq_restore(flags);
358 * Local APIC timer broadcast function
360 static void lapic_timer_broadcast(cpumask_t mask)
363 send_IPI_mask(mask, LOCAL_TIMER_VECTOR);
368 * Setup the local APIC timer for this CPU. Copy the initilized values
369 * of the boot CPU and register the clock event in the framework.
371 static void setup_APIC_timer(void)
373 struct clock_event_device *levt = &__get_cpu_var(lapic_events);
375 memcpy(levt, &lapic_clockevent, sizeof(*levt));
376 levt->cpumask = cpumask_of_cpu(smp_processor_id());
378 clockevents_register_device(levt);
382 * In this function we calibrate APIC bus clocks to the external
383 * timer. Unfortunately we cannot use jiffies and the timer irq
384 * to calibrate, since some later bootup code depends on getting
385 * the first irq? Ugh.
387 * We want to do the calibration only once since we
388 * want to have local timer irqs syncron. CPUs connected
389 * by the same APIC bus have the very same bus frequency.
390 * And we want to have irqs off anyways, no accidental
394 #define TICK_COUNT 100000000
396 static int __init calibrate_APIC_clock(void)
398 unsigned apic, apic_start;
399 unsigned long tsc, tsc_start;
405 * Put whatever arbitrary (but long enough) timeout
406 * value into the APIC clock, we just want to get the
407 * counter running for calibration.
409 * No interrupt enable !
411 __setup_APIC_LVTT(250000000, 0, 0);
413 apic_start = apic_read(APIC_TMCCT);
414 #ifdef CONFIG_X86_PM_TIMER
415 if (apic_calibrate_pmtmr && pmtmr_ioport) {
416 pmtimer_wait(5000); /* 5ms wait */
417 apic = apic_read(APIC_TMCCT);
418 result = (apic_start - apic) * 1000L / 5;
425 apic = apic_read(APIC_TMCCT);
427 } while ((tsc - tsc_start) < TICK_COUNT &&
428 (apic_start - apic) < TICK_COUNT);
430 result = (apic_start - apic) * 1000L * tsc_khz /
436 printk(KERN_DEBUG "APIC timer calibration result %d\n", result);
438 printk(KERN_INFO "Detected %d.%03d MHz APIC timer.\n",
439 result / 1000 / 1000, result / 1000 % 1000);
441 /* Calculate the scaled math multiplication factor */
442 lapic_clockevent.mult = div_sc(result, NSEC_PER_SEC,
443 lapic_clockevent.shift);
444 lapic_clockevent.max_delta_ns =
445 clockevent_delta2ns(0x7FFFFF, &lapic_clockevent);
446 lapic_clockevent.min_delta_ns =
447 clockevent_delta2ns(0xF, &lapic_clockevent);
449 calibration_result = result / HZ;
452 * Do a sanity check on the APIC calibration result
454 if (calibration_result < (1000000 / HZ)) {
456 "APIC frequency too slow, disabling apic timer\n");
464 * Setup the boot APIC
466 * Calibrate and verify the result.
468 void __init setup_boot_APIC_clock(void)
471 * The local apic timer can be disabled via the kernel commandline.
472 * Register the lapic timer as a dummy clock event source on SMP
473 * systems, so the broadcast mechanism is used. On UP systems simply
476 if (disable_apic_timer) {
477 printk(KERN_INFO "Disabling APIC timer\n");
478 /* No broadcast on UP ! */
479 if (num_possible_cpus() > 1) {
480 lapic_clockevent.mult = 1;
486 printk(KERN_INFO "Using local APIC timer interrupts.\n");
487 if (calibrate_APIC_clock()) {
488 /* No broadcast on UP ! */
489 if (num_possible_cpus() > 1)
495 * If nmi_watchdog is set to IO_APIC, we need the
496 * PIT/HPET going. Otherwise register lapic as a dummy
499 if (nmi_watchdog != NMI_IO_APIC)
500 lapic_clockevent.features &= ~CLOCK_EVT_FEAT_DUMMY;
502 printk(KERN_WARNING "APIC timer registered as dummy,"
503 " due to nmi_watchdog=%d!\n", nmi_watchdog);
508 void __cpuinit setup_secondary_APIC_clock(void)
514 * The guts of the apic timer interrupt
516 static void local_apic_timer_interrupt(void)
518 int cpu = smp_processor_id();
519 struct clock_event_device *evt = &per_cpu(lapic_events, cpu);
522 * Normally we should not be here till LAPIC has been initialized but
523 * in some cases like kdump, its possible that there is a pending LAPIC
524 * timer interrupt from previous kernel's context and is delivered in
525 * new kernel the moment interrupts are enabled.
527 * Interrupts are enabled early and LAPIC is setup much later, hence
528 * its possible that when we get here evt->event_handler is NULL.
529 * Check for event_handler being NULL and discard the interrupt as
532 if (!evt->event_handler) {
534 "Spurious LAPIC timer interrupt on cpu %d\n", cpu);
536 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN, evt);
541 * the NMI deadlock-detector uses this.
543 add_pda(apic_timer_irqs, 1);
545 evt->event_handler(evt);
549 * Local APIC timer interrupt. This is the most natural way for doing
550 * local interrupts, but local timer interrupts can be emulated by
551 * broadcast interrupts too. [in case the hw doesn't support APIC timers]
553 * [ if a single-CPU system runs an SMP kernel then we call the local
554 * interrupt as well. Thus we cannot inline the local irq ... ]
556 void smp_apic_timer_interrupt(struct pt_regs *regs)
558 struct pt_regs *old_regs = set_irq_regs(regs);
561 * NOTE! We'd better ACK the irq immediately,
562 * because timer handling can be slow.
566 * update_process_times() expects us to have done irq_enter().
567 * Besides, if we don't timer interrupts ignore the global
568 * interrupt lock, which is the WrongThing (tm) to do.
572 local_apic_timer_interrupt();
574 set_irq_regs(old_regs);
577 int setup_profiling_timer(unsigned int multiplier)
584 * Local APIC start and shutdown
588 * clear_local_APIC - shutdown the local APIC
590 * This is called, when a CPU is disabled and before rebooting, so the state of
591 * the local APIC has no dangling leftovers. Also used to cleanout any BIOS
592 * leftovers during boot.
594 void clear_local_APIC(void)
599 /* APIC hasn't been mapped yet */
603 maxlvt = lapic_get_maxlvt();
605 * Masking an LVT entry can trigger a local APIC error
606 * if the vector is zero. Mask LVTERR first to prevent this.
609 v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
610 apic_write(APIC_LVTERR, v | APIC_LVT_MASKED);
613 * Careful: we have to set masks only first to deassert
614 * any level-triggered sources.
616 v = apic_read(APIC_LVTT);
617 apic_write(APIC_LVTT, v | APIC_LVT_MASKED);
618 v = apic_read(APIC_LVT0);
619 apic_write(APIC_LVT0, v | APIC_LVT_MASKED);
620 v = apic_read(APIC_LVT1);
621 apic_write(APIC_LVT1, v | APIC_LVT_MASKED);
623 v = apic_read(APIC_LVTPC);
624 apic_write(APIC_LVTPC, v | APIC_LVT_MASKED);
628 * Clean APIC state for other OSs:
630 apic_write(APIC_LVTT, APIC_LVT_MASKED);
631 apic_write(APIC_LVT0, APIC_LVT_MASKED);
632 apic_write(APIC_LVT1, APIC_LVT_MASKED);
634 apic_write(APIC_LVTERR, APIC_LVT_MASKED);
636 apic_write(APIC_LVTPC, APIC_LVT_MASKED);
637 apic_write(APIC_ESR, 0);
642 * disable_local_APIC - clear and disable the local APIC
644 void disable_local_APIC(void)
651 * Disable APIC (implies clearing of registers
654 value = apic_read(APIC_SPIV);
655 value &= ~APIC_SPIV_APIC_ENABLED;
656 apic_write(APIC_SPIV, value);
659 void lapic_shutdown(void)
666 local_irq_save(flags);
668 disable_local_APIC();
670 local_irq_restore(flags);
674 * This is to verify that we're looking at a real local APIC.
675 * Check these against your board if the CPUs aren't getting
676 * started for no apparent reason.
678 int __init verify_local_APIC(void)
680 unsigned int reg0, reg1;
683 * The version register is read-only in a real APIC.
685 reg0 = apic_read(APIC_LVR);
686 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
687 apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
688 reg1 = apic_read(APIC_LVR);
689 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
692 * The two version reads above should print the same
693 * numbers. If the second one is different, then we
694 * poke at a non-APIC.
700 * Check if the version looks reasonably.
702 reg1 = GET_APIC_VERSION(reg0);
703 if (reg1 == 0x00 || reg1 == 0xff)
705 reg1 = lapic_get_maxlvt();
706 if (reg1 < 0x02 || reg1 == 0xff)
710 * The ID register is read/write in a real APIC.
712 reg0 = apic_read(APIC_ID);
713 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
714 apic_write(APIC_ID, reg0 ^ APIC_ID_MASK);
715 reg1 = apic_read(APIC_ID);
716 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg1);
717 apic_write(APIC_ID, reg0);
718 if (reg1 != (reg0 ^ APIC_ID_MASK))
722 * The next two are just to see if we have sane values.
723 * They're only really relevant if we're in Virtual Wire
724 * compatibility mode, but most boxes are anymore.
726 reg0 = apic_read(APIC_LVT0);
727 apic_printk(APIC_DEBUG, "Getting LVT0: %x\n", reg0);
728 reg1 = apic_read(APIC_LVT1);
729 apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
735 * sync_Arb_IDs - synchronize APIC bus arbitration IDs
737 void __init sync_Arb_IDs(void)
739 /* Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 */
746 apic_wait_icr_idle();
748 apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
749 apic_write(APIC_ICR, APIC_DEST_ALLINC | APIC_INT_LEVELTRIG
754 * An initial setup of the virtual wire mode.
756 void __init init_bsp_APIC(void)
761 * Don't do the setup now if we have a SMP BIOS as the
762 * through-I/O-APIC virtual wire mode might be active.
764 if (smp_found_config || !cpu_has_apic)
767 value = apic_read(APIC_LVR);
770 * Do not trust the local APIC being empty at bootup.
777 value = apic_read(APIC_SPIV);
778 value &= ~APIC_VECTOR_MASK;
779 value |= APIC_SPIV_APIC_ENABLED;
780 value |= APIC_SPIV_FOCUS_DISABLED;
781 value |= SPURIOUS_APIC_VECTOR;
782 apic_write(APIC_SPIV, value);
785 * Set up the virtual wire mode.
787 apic_write(APIC_LVT0, APIC_DM_EXTINT);
789 apic_write(APIC_LVT1, value);
793 * setup_local_APIC - setup the local APIC
795 void __cpuinit setup_local_APIC(void)
801 value = apic_read(APIC_LVR);
803 BUILD_BUG_ON((SPURIOUS_APIC_VECTOR & 0x0f) != 0x0f);
806 * Double-check whether this APIC is really registered.
807 * This is meaningless in clustered apic mode, so we skip it.
809 if (!apic_id_registered())
813 * Intel recommends to set DFR, LDR and TPR before enabling
814 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
815 * document number 292116). So here it goes...
820 * Set Task Priority to 'accept all'. We never change this
823 value = apic_read(APIC_TASKPRI);
824 value &= ~APIC_TPRI_MASK;
825 apic_write(APIC_TASKPRI, value);
828 * After a crash, we no longer service the interrupts and a pending
829 * interrupt from previous kernel might still have ISR bit set.
831 * Most probably by now CPU has serviced that pending interrupt and
832 * it might not have done the ack_APIC_irq() because it thought,
833 * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
834 * does not clear the ISR bit and cpu thinks it has already serivced
835 * the interrupt. Hence a vector might get locked. It was noticed
836 * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
838 for (i = APIC_ISR_NR - 1; i >= 0; i--) {
839 value = apic_read(APIC_ISR + i*0x10);
840 for (j = 31; j >= 0; j--) {
847 * Now that we are all set up, enable the APIC
849 value = apic_read(APIC_SPIV);
850 value &= ~APIC_VECTOR_MASK;
854 value |= APIC_SPIV_APIC_ENABLED;
856 /* We always use processor focus */
859 * Set spurious IRQ vector
861 value |= SPURIOUS_APIC_VECTOR;
862 apic_write(APIC_SPIV, value);
867 * set up through-local-APIC on the BP's LINT0. This is not
868 * strictly necessary in pure symmetric-IO mode, but sometimes
869 * we delegate interrupts to the 8259A.
872 * TODO: set up through-local-APIC from through-I/O-APIC? --macro
874 value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
875 if (!smp_processor_id() && !value) {
876 value = APIC_DM_EXTINT;
877 apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n",
880 value = APIC_DM_EXTINT | APIC_LVT_MASKED;
881 apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n",
884 apic_write(APIC_LVT0, value);
887 * only the BP should see the LINT1 NMI signal, obviously.
889 if (!smp_processor_id())
892 value = APIC_DM_NMI | APIC_LVT_MASKED;
893 apic_write(APIC_LVT1, value);
897 static void __cpuinit lapic_setup_esr(void)
899 unsigned maxlvt = lapic_get_maxlvt();
901 apic_write(APIC_LVTERR, ERROR_APIC_VECTOR);
903 * spec says clear errors after enabling vector.
906 apic_write(APIC_ESR, 0);
909 void __cpuinit end_local_APIC_setup(void)
912 setup_apic_nmi_watchdog(NULL);
916 void check_x2apic(void)
920 rdmsr(MSR_IA32_APICBASE, msr, msr2);
922 if (msr & X2APIC_ENABLE) {
923 printk("x2apic enabled by BIOS, switching to x2apic ops\n");
924 x2apic_preenabled = x2apic = 1;
925 apic_ops = &x2apic_ops;
929 void enable_x2apic(void)
933 rdmsr(MSR_IA32_APICBASE, msr, msr2);
934 if (!(msr & X2APIC_ENABLE)) {
935 printk("Enabling x2apic\n");
936 wrmsr(MSR_IA32_APICBASE, msr | X2APIC_ENABLE, 0);
940 void enable_IR_x2apic(void)
942 #ifdef CONFIG_INTR_REMAP
949 if (!x2apic_preenabled && disable_x2apic) {
951 "Skipped enabling x2apic and Interrupt-remapping "
952 "because of nox2apic\n");
956 if (x2apic_preenabled && disable_x2apic)
957 panic("Bios already enabled x2apic, can't enforce nox2apic");
959 if (!x2apic_preenabled && skip_ioapic_setup) {
961 "Skipped enabling x2apic and Interrupt-remapping "
962 "because of skipping io-apic setup\n");
966 ret = dmar_table_init();
969 "dmar_table_init() failed with %d:\n", ret);
971 if (x2apic_preenabled)
972 panic("x2apic enabled by bios. But IR enabling failed");
975 "Not enabling x2apic,Intr-remapping\n");
979 local_irq_save(flags);
981 save_mask_IO_APIC_setup();
983 ret = enable_intr_remapping(1);
985 if (ret && x2apic_preenabled) {
986 local_irq_restore(flags);
987 panic("x2apic enabled by bios. But IR enabling failed");
995 apic_ops = &x2apic_ops;
1001 * IR enabling failed
1003 restore_IO_APIC_setup();
1005 reinit_intr_remapped_IO_APIC(x2apic_preenabled);
1008 local_irq_restore(flags);
1011 if (!x2apic_preenabled)
1013 "Enabled x2apic and interrupt-remapping\n");
1016 "Enabled Interrupt-remapping\n");
1019 "Failed to enable Interrupt-remapping and x2apic\n");
1021 if (!cpu_has_x2apic)
1024 if (x2apic_preenabled)
1025 panic("x2apic enabled prior OS handover,"
1026 " enable CONFIG_INTR_REMAP");
1028 printk(KERN_INFO "Enable CONFIG_INTR_REMAP for enabling intr-remapping "
1036 * Detect and enable local APICs on non-SMP boards.
1037 * Original code written by Keir Fraser.
1038 * On AMD64 we trust the BIOS - if it says no APIC it is likely
1039 * not correctly set up (usually the APIC timer won't work etc.)
1041 static int __init detect_init_APIC(void)
1043 if (!cpu_has_apic) {
1044 printk(KERN_INFO "No local APIC present\n");
1048 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
1049 boot_cpu_physical_apicid = 0;
1053 void __init early_init_lapic_mapping(void)
1055 unsigned long phys_addr;
1058 * If no local APIC can be found then go out
1059 * : it means there is no mpatable and MADT
1061 if (!smp_found_config)
1064 phys_addr = mp_lapic_addr;
1066 set_fixmap_nocache(FIX_APIC_BASE, phys_addr);
1067 apic_printk(APIC_VERBOSE, "mapped APIC to %16lx (%16lx)\n",
1068 APIC_BASE, phys_addr);
1071 * Fetch the APIC ID of the BSP in case we have a
1072 * default configuration (or the MP table is broken).
1074 boot_cpu_physical_apicid = read_apic_id();
1078 * init_apic_mappings - initialize APIC mappings
1080 void __init init_apic_mappings(void)
1083 boot_cpu_physical_apicid = read_apic_id();
1088 * If no local APIC can be found then set up a fake all
1089 * zeroes page to simulate the local APIC and another
1090 * one for the IO-APIC.
1092 if (!smp_found_config && detect_init_APIC()) {
1093 apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
1094 apic_phys = __pa(apic_phys);
1096 apic_phys = mp_lapic_addr;
1098 set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
1099 apic_printk(APIC_VERBOSE, "mapped APIC to %16lx (%16lx)\n",
1100 APIC_BASE, apic_phys);
1103 * Fetch the APIC ID of the BSP in case we have a
1104 * default configuration (or the MP table is broken).
1106 boot_cpu_physical_apicid = read_apic_id();
1110 * This initializes the IO-APIC and APIC hardware if this is
1113 int __init APIC_init_uniprocessor(void)
1116 printk(KERN_INFO "Apic disabled\n");
1119 if (!cpu_has_apic) {
1121 printk(KERN_INFO "Apic disabled by BIOS\n");
1126 setup_apic_routing();
1128 verify_local_APIC();
1132 physid_set_mask_of_physid(boot_cpu_physical_apicid, &phys_cpu_present_map);
1133 apic_write(APIC_ID, SET_APIC_ID(boot_cpu_physical_apicid));
1138 * Now enable IO-APICs, actually call clear_IO_APIC
1139 * We need clear_IO_APIC before enabling vector on BP
1141 if (!skip_ioapic_setup && nr_ioapics)
1144 if (!smp_found_config || skip_ioapic_setup || !nr_ioapics)
1145 localise_nmi_watchdog();
1146 end_local_APIC_setup();
1148 if (smp_found_config && !skip_ioapic_setup && nr_ioapics)
1152 setup_boot_APIC_clock();
1153 check_nmi_watchdog();
1158 * Local APIC interrupts
1162 * This interrupt should _never_ happen with our APIC/SMP architecture
1164 asmlinkage void smp_spurious_interrupt(void)
1170 * Check if this really is a spurious interrupt and ACK it
1171 * if it is a vectored one. Just in case...
1172 * Spurious interrupts should not be ACKed.
1174 v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
1175 if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
1178 add_pda(irq_spurious_count, 1);
1183 * This interrupt should never happen with our APIC/SMP architecture
1185 asmlinkage void smp_error_interrupt(void)
1191 /* First tickle the hardware, only then report what went on. -- REW */
1192 v = apic_read(APIC_ESR);
1193 apic_write(APIC_ESR, 0);
1194 v1 = apic_read(APIC_ESR);
1196 atomic_inc(&irq_err_count);
1198 /* Here is what the APIC error bits mean:
1201 2: Send accept error
1202 3: Receive accept error
1204 5: Send illegal vector
1205 6: Received illegal vector
1206 7: Illegal register address
1208 printk(KERN_DEBUG "APIC error on CPU%d: %02x(%02x)\n",
1209 smp_processor_id(), v , v1);
1214 * * connect_bsp_APIC - attach the APIC to the interrupt system
1216 void __init connect_bsp_APIC(void)
1221 void disconnect_bsp_APIC(int virt_wire_setup)
1223 /* Go back to Virtual Wire compatibility mode */
1224 unsigned long value;
1226 /* For the spurious interrupt use vector F, and enable it */
1227 value = apic_read(APIC_SPIV);
1228 value &= ~APIC_VECTOR_MASK;
1229 value |= APIC_SPIV_APIC_ENABLED;
1231 apic_write(APIC_SPIV, value);
1233 if (!virt_wire_setup) {
1235 * For LVT0 make it edge triggered, active high,
1236 * external and enabled
1238 value = apic_read(APIC_LVT0);
1239 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
1240 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
1241 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
1242 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1243 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXTINT);
1244 apic_write(APIC_LVT0, value);
1247 apic_write(APIC_LVT0, APIC_LVT_MASKED);
1250 /* For LVT1 make it edge triggered, active high, nmi and enabled */
1251 value = apic_read(APIC_LVT1);
1252 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
1253 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
1254 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
1255 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
1256 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
1257 apic_write(APIC_LVT1, value);
1260 void __cpuinit generic_processor_info(int apicid, int version)
1265 if (num_processors >= NR_CPUS) {
1266 printk(KERN_WARNING "WARNING: NR_CPUS limit of %i reached."
1267 " Processor ignored.\n", NR_CPUS);
1271 if (num_processors >= maxcpus) {
1272 printk(KERN_WARNING "WARNING: maxcpus limit of %i reached."
1273 " Processor ignored.\n", maxcpus);
1278 cpus_complement(tmp_map, cpu_present_map);
1279 cpu = first_cpu(tmp_map);
1281 physid_set(apicid, phys_cpu_present_map);
1282 if (apicid == boot_cpu_physical_apicid) {
1284 * x86_bios_cpu_apicid is required to have processors listed
1285 * in same order as logical cpu numbers. Hence the first
1286 * entry is BSP, and so on.
1290 if (apicid > max_physical_apicid)
1291 max_physical_apicid = apicid;
1293 /* are we being called early in kernel startup? */
1294 if (early_per_cpu_ptr(x86_cpu_to_apicid)) {
1295 u16 *cpu_to_apicid = early_per_cpu_ptr(x86_cpu_to_apicid);
1296 u16 *bios_cpu_apicid = early_per_cpu_ptr(x86_bios_cpu_apicid);
1298 cpu_to_apicid[cpu] = apicid;
1299 bios_cpu_apicid[cpu] = apicid;
1301 per_cpu(x86_cpu_to_apicid, cpu) = apicid;
1302 per_cpu(x86_bios_cpu_apicid, cpu) = apicid;
1305 cpu_set(cpu, cpu_possible_map);
1306 cpu_set(cpu, cpu_present_map);
1309 int hard_smp_processor_id(void)
1311 return read_apic_id();
1320 /* 'active' is true if the local APIC was enabled by us and
1321 not the BIOS; this signifies that we are also responsible
1322 for disabling it before entering apm/acpi suspend */
1324 /* r/w apic fields */
1325 unsigned int apic_id;
1326 unsigned int apic_taskpri;
1327 unsigned int apic_ldr;
1328 unsigned int apic_dfr;
1329 unsigned int apic_spiv;
1330 unsigned int apic_lvtt;
1331 unsigned int apic_lvtpc;
1332 unsigned int apic_lvt0;
1333 unsigned int apic_lvt1;
1334 unsigned int apic_lvterr;
1335 unsigned int apic_tmict;
1336 unsigned int apic_tdcr;
1337 unsigned int apic_thmr;
1340 static int lapic_suspend(struct sys_device *dev, pm_message_t state)
1342 unsigned long flags;
1345 if (!apic_pm_state.active)
1348 maxlvt = lapic_get_maxlvt();
1350 apic_pm_state.apic_id = apic_read(APIC_ID);
1351 apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
1352 apic_pm_state.apic_ldr = apic_read(APIC_LDR);
1353 apic_pm_state.apic_dfr = apic_read(APIC_DFR);
1354 apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
1355 apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
1357 apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
1358 apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
1359 apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
1360 apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
1361 apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
1362 apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
1363 #ifdef CONFIG_X86_MCE_INTEL
1365 apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
1367 local_irq_save(flags);
1368 disable_local_APIC();
1369 local_irq_restore(flags);
1373 static int lapic_resume(struct sys_device *dev)
1376 unsigned long flags;
1379 if (!apic_pm_state.active)
1382 maxlvt = lapic_get_maxlvt();
1384 local_irq_save(flags);
1386 rdmsr(MSR_IA32_APICBASE, l, h);
1387 l &= ~MSR_IA32_APICBASE_BASE;
1388 l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
1389 wrmsr(MSR_IA32_APICBASE, l, h);
1393 apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
1394 apic_write(APIC_ID, apic_pm_state.apic_id);
1395 apic_write(APIC_DFR, apic_pm_state.apic_dfr);
1396 apic_write(APIC_LDR, apic_pm_state.apic_ldr);
1397 apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
1398 apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
1399 apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
1400 apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
1401 #ifdef CONFIG_X86_MCE_INTEL
1403 apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
1406 apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
1407 apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
1408 apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
1409 apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
1410 apic_write(APIC_ESR, 0);
1411 apic_read(APIC_ESR);
1412 apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
1413 apic_write(APIC_ESR, 0);
1414 apic_read(APIC_ESR);
1415 local_irq_restore(flags);
1419 static struct sysdev_class lapic_sysclass = {
1421 .resume = lapic_resume,
1422 .suspend = lapic_suspend,
1425 static struct sys_device device_lapic = {
1427 .cls = &lapic_sysclass,
1430 static void __cpuinit apic_pm_activate(void)
1432 apic_pm_state.active = 1;
1435 static int __init init_lapic_sysfs(void)
1441 /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
1443 error = sysdev_class_register(&lapic_sysclass);
1445 error = sysdev_register(&device_lapic);
1448 device_initcall(init_lapic_sysfs);
1450 #else /* CONFIG_PM */
1452 static void apic_pm_activate(void) { }
1454 #endif /* CONFIG_PM */
1457 * apic_is_clustered_box() -- Check if we can expect good TSC
1459 * Thus far, the major user of this is IBM's Summit2 series:
1461 * Clustered boxes may have unsynced TSC problems if they are
1462 * multi-chassis. Use available data to take a good guess.
1463 * If in doubt, go HPET.
1465 __cpuinit int apic_is_clustered_box(void)
1467 int i, clusters, zeros;
1469 u16 *bios_cpu_apicid;
1470 DECLARE_BITMAP(clustermap, NUM_APIC_CLUSTERS);
1473 * there is not this kind of box with AMD CPU yet.
1474 * Some AMD box with quadcore cpu and 8 sockets apicid
1475 * will be [4, 0x23] or [8, 0x27] could be thought to
1476 * vsmp box still need checking...
1478 if ((boot_cpu_data.x86_vendor == X86_VENDOR_AMD) && !is_vsmp_box())
1481 bios_cpu_apicid = early_per_cpu_ptr(x86_bios_cpu_apicid);
1482 bitmap_zero(clustermap, NUM_APIC_CLUSTERS);
1484 for (i = 0; i < NR_CPUS; i++) {
1485 /* are we being called early in kernel startup? */
1486 if (bios_cpu_apicid) {
1487 id = bios_cpu_apicid[i];
1489 else if (i < nr_cpu_ids) {
1491 id = per_cpu(x86_bios_cpu_apicid, i);
1498 if (id != BAD_APICID)
1499 __set_bit(APIC_CLUSTERID(id), clustermap);
1502 /* Problem: Partially populated chassis may not have CPUs in some of
1503 * the APIC clusters they have been allocated. Only present CPUs have
1504 * x86_bios_cpu_apicid entries, thus causing zeroes in the bitmap.
1505 * Since clusters are allocated sequentially, count zeros only if
1506 * they are bounded by ones.
1510 for (i = 0; i < NUM_APIC_CLUSTERS; i++) {
1511 if (test_bit(i, clustermap)) {
1512 clusters += 1 + zeros;
1518 /* ScaleMP vSMPowered boxes have one cluster per board and TSCs are
1519 * not guaranteed to be synced between boards
1521 if (is_vsmp_box() && clusters > 1)
1525 * If clusters > 2, then should be multi-chassis.
1526 * May have to revisit this when multi-core + hyperthreaded CPUs come
1527 * out, but AFAIK this will work even for them.
1529 return (clusters > 2);
1532 static __init int setup_nox2apic(char *str)
1535 clear_cpu_cap(&boot_cpu_data, X86_FEATURE_X2APIC);
1538 early_param("nox2apic", setup_nox2apic);
1542 * APIC command line parameters
1544 static int __init apic_set_verbosity(char *str)
1547 skip_ioapic_setup = 0;
1551 if (strcmp("debug", str) == 0)
1552 apic_verbosity = APIC_DEBUG;
1553 else if (strcmp("verbose", str) == 0)
1554 apic_verbosity = APIC_VERBOSE;
1556 printk(KERN_WARNING "APIC Verbosity level %s not recognised"
1557 " use apic=verbose or apic=debug\n", str);
1563 early_param("apic", apic_set_verbosity);
1565 static __init int setup_disableapic(char *str)
1568 setup_clear_cpu_cap(X86_FEATURE_APIC);
1571 early_param("disableapic", setup_disableapic);
1573 /* same as disableapic, for compatibility */
1574 static __init int setup_nolapic(char *str)
1576 return setup_disableapic(str);
1578 early_param("nolapic", setup_nolapic);
1580 static int __init parse_lapic_timer_c2_ok(char *arg)
1582 local_apic_timer_c2_ok = 1;
1585 early_param("lapic_timer_c2_ok", parse_lapic_timer_c2_ok);
1587 static int __init parse_disable_apic_timer(char *arg)
1589 disable_apic_timer = 1;
1592 early_param("noapictimer", parse_disable_apic_timer);
1594 static int __init parse_nolapic_timer(char *arg)
1596 disable_apic_timer = 1;
1599 early_param("nolapic_timer", parse_nolapic_timer);
1601 static __init int setup_apicpmtimer(char *s)
1603 apic_calibrate_pmtmr = 1;
1607 __setup("apicpmtimer", setup_apicpmtimer);
1609 static int __init lapic_insert_resource(void)
1614 /* Put local APIC into the resource map. */
1615 lapic_resource.start = apic_phys;
1616 lapic_resource.end = lapic_resource.start + PAGE_SIZE - 1;
1617 insert_resource(&iomem_resource, &lapic_resource);
1623 * need call insert after e820_reserve_resources()
1624 * that is using request_resource
1626 late_initcall(lapic_insert_resource);