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
;
152 static __init
int setup_nox2apic(char *str
)
154 if (x2apic_enabled()) {
155 int apicid
= native_apic_msr_read(APIC_ID
);
158 pr_warning("Apicid: %08x, cannot enforce nox2apic\n",
163 pr_warning("x2apic already enabled. will disable it\n");
165 setup_clear_cpu_cap(X86_FEATURE_X2APIC
);
171 early_param("nox2apic", setup_nox2apic
);
174 unsigned long mp_lapic_addr
;
176 /* Disable local APIC timer from the kernel commandline or via dmi quirk */
177 static int disable_apic_timer __initdata
;
178 /* Local APIC timer works in C2 */
179 int local_apic_timer_c2_ok
;
180 EXPORT_SYMBOL_GPL(local_apic_timer_c2_ok
);
182 int first_system_vector
= 0xfe;
185 * Debug level, exported for io_apic.c
187 unsigned int apic_verbosity
;
191 /* Have we found an MP table */
192 int smp_found_config
;
194 static struct resource lapic_resource
= {
195 .name
= "Local APIC",
196 .flags
= IORESOURCE_MEM
| IORESOURCE_BUSY
,
199 unsigned int lapic_timer_frequency
= 0;
201 static void apic_pm_activate(void);
203 static unsigned long apic_phys
;
206 * Get the LAPIC version
208 static inline int lapic_get_version(void)
210 return GET_APIC_VERSION(apic_read(APIC_LVR
));
214 * Check, if the APIC is integrated or a separate chip
216 static inline int lapic_is_integrated(void)
221 return APIC_INTEGRATED(lapic_get_version());
226 * Check, whether this is a modern or a first generation APIC
228 static int modern_apic(void)
230 /* AMD systems use old APIC versions, so check the CPU */
231 if (boot_cpu_data
.x86_vendor
== X86_VENDOR_AMD
&&
232 boot_cpu_data
.x86
>= 0xf)
234 return lapic_get_version() >= 0x14;
238 * right after this call apic become NOOP driven
239 * so apic->write/read doesn't do anything
241 static void __init
apic_disable(void)
243 pr_info("APIC: switched to apic NOOP\n");
247 void native_apic_wait_icr_idle(void)
249 while (apic_read(APIC_ICR
) & APIC_ICR_BUSY
)
253 u32
native_safe_apic_wait_icr_idle(void)
260 send_status
= apic_read(APIC_ICR
) & APIC_ICR_BUSY
;
263 inc_irq_stat(icr_read_retry_count
);
265 } while (timeout
++ < 1000);
270 void native_apic_icr_write(u32 low
, u32 id
)
272 apic_write(APIC_ICR2
, SET_APIC_DEST_FIELD(id
));
273 apic_write(APIC_ICR
, low
);
276 u64
native_apic_icr_read(void)
280 icr2
= apic_read(APIC_ICR2
);
281 icr1
= apic_read(APIC_ICR
);
283 return icr1
| ((u64
)icr2
<< 32);
288 * get_physical_broadcast - Get number of physical broadcast IDs
290 int get_physical_broadcast(void)
292 return modern_apic() ? 0xff : 0xf;
297 * lapic_get_maxlvt - get the maximum number of local vector table entries
299 int lapic_get_maxlvt(void)
303 v
= apic_read(APIC_LVR
);
305 * - we always have APIC integrated on 64bit mode
306 * - 82489DXs do not report # of LVT entries
308 return APIC_INTEGRATED(GET_APIC_VERSION(v
)) ? GET_APIC_MAXLVT(v
) : 2;
316 #define APIC_DIVISOR 16
319 * This function sets up the local APIC timer, with a timeout of
320 * 'clocks' APIC bus clock. During calibration we actually call
321 * this function twice on the boot CPU, once with a bogus timeout
322 * value, second time for real. The other (noncalibrating) CPUs
323 * call this function only once, with the real, calibrated value.
325 * We do reads before writes even if unnecessary, to get around the
326 * P5 APIC double write bug.
328 static void __setup_APIC_LVTT(unsigned int clocks
, int oneshot
, int irqen
)
330 unsigned int lvtt_value
, tmp_value
;
332 lvtt_value
= LOCAL_TIMER_VECTOR
;
334 lvtt_value
|= APIC_LVT_TIMER_PERIODIC
;
335 if (!lapic_is_integrated())
336 lvtt_value
|= SET_APIC_TIMER_BASE(APIC_TIMER_BASE_DIV
);
339 lvtt_value
|= APIC_LVT_MASKED
;
341 apic_write(APIC_LVTT
, lvtt_value
);
346 tmp_value
= apic_read(APIC_TDCR
);
347 apic_write(APIC_TDCR
,
348 (tmp_value
& ~(APIC_TDR_DIV_1
| APIC_TDR_DIV_TMBASE
)) |
352 apic_write(APIC_TMICT
, clocks
/ APIC_DIVISOR
);
356 * Setup extended LVT, AMD specific
358 * Software should use the LVT offsets the BIOS provides. The offsets
359 * are determined by the subsystems using it like those for MCE
360 * threshold or IBS. On K8 only offset 0 (APIC500) and MCE interrupts
361 * are supported. Beginning with family 10h at least 4 offsets are
364 * Since the offsets must be consistent for all cores, we keep track
365 * of the LVT offsets in software and reserve the offset for the same
366 * vector also to be used on other cores. An offset is freed by
367 * setting the entry to APIC_EILVT_MASKED.
369 * If the BIOS is right, there should be no conflicts. Otherwise a
370 * "[Firmware Bug]: ..." error message is generated. However, if
371 * software does not properly determines the offsets, it is not
372 * necessarily a BIOS bug.
375 static atomic_t eilvt_offsets
[APIC_EILVT_NR_MAX
];
377 static inline int eilvt_entry_is_changeable(unsigned int old
, unsigned int new)
379 return (old
& APIC_EILVT_MASKED
)
380 || (new == APIC_EILVT_MASKED
)
381 || ((new & ~APIC_EILVT_MASKED
) == old
);
384 static unsigned int reserve_eilvt_offset(int offset
, unsigned int new)
386 unsigned int rsvd
; /* 0: uninitialized */
388 if (offset
>= APIC_EILVT_NR_MAX
)
391 rsvd
= atomic_read(&eilvt_offsets
[offset
]) & ~APIC_EILVT_MASKED
;
394 !eilvt_entry_is_changeable(rsvd
, new))
395 /* may not change if vectors are different */
397 rsvd
= atomic_cmpxchg(&eilvt_offsets
[offset
], rsvd
, new);
398 } while (rsvd
!= new);
404 * If mask=1, the LVT entry does not generate interrupts while mask=0
405 * enables the vector. See also the BKDGs. Must be called with
406 * preemption disabled.
409 int setup_APIC_eilvt(u8 offset
, u8 vector
, u8 msg_type
, u8 mask
)
411 unsigned long reg
= APIC_EILVTn(offset
);
412 unsigned int new, old
, reserved
;
414 new = (mask
<< 16) | (msg_type
<< 8) | vector
;
415 old
= apic_read(reg
);
416 reserved
= reserve_eilvt_offset(offset
, new);
418 if (reserved
!= new) {
419 pr_err(FW_BUG
"cpu %d, try to use APIC%lX (LVT offset %d) for "
420 "vector 0x%x, but the register is already in use for "
421 "vector 0x%x on another cpu\n",
422 smp_processor_id(), reg
, offset
, new, reserved
);
426 if (!eilvt_entry_is_changeable(old
, new)) {
427 pr_err(FW_BUG
"cpu %d, try to use APIC%lX (LVT offset %d) for "
428 "vector 0x%x, but the register is already in use for "
429 "vector 0x%x on this cpu\n",
430 smp_processor_id(), reg
, offset
, new, old
);
434 apic_write(reg
, new);
438 EXPORT_SYMBOL_GPL(setup_APIC_eilvt
);
441 * Program the next event, relative to now
443 static int lapic_next_event(unsigned long delta
,
444 struct clock_event_device
*evt
)
446 apic_write(APIC_TMICT
, delta
);
451 * Setup the lapic timer in periodic or oneshot mode
453 static void lapic_timer_setup(enum clock_event_mode mode
,
454 struct clock_event_device
*evt
)
459 /* Lapic used as dummy for broadcast ? */
460 if (evt
->features
& CLOCK_EVT_FEAT_DUMMY
)
463 local_irq_save(flags
);
466 case CLOCK_EVT_MODE_PERIODIC
:
467 case CLOCK_EVT_MODE_ONESHOT
:
468 __setup_APIC_LVTT(lapic_timer_frequency
,
469 mode
!= CLOCK_EVT_MODE_PERIODIC
, 1);
471 case CLOCK_EVT_MODE_UNUSED
:
472 case CLOCK_EVT_MODE_SHUTDOWN
:
473 v
= apic_read(APIC_LVTT
);
474 v
|= (APIC_LVT_MASKED
| LOCAL_TIMER_VECTOR
);
475 apic_write(APIC_LVTT
, v
);
476 apic_write(APIC_TMICT
, 0);
478 case CLOCK_EVT_MODE_RESUME
:
479 /* Nothing to do here */
483 local_irq_restore(flags
);
487 * Local APIC timer broadcast function
489 static void lapic_timer_broadcast(const struct cpumask
*mask
)
492 apic
->send_IPI_mask(mask
, LOCAL_TIMER_VECTOR
);
498 * The local apic timer can be used for any function which is CPU local.
500 static struct clock_event_device lapic_clockevent
= {
502 .features
= CLOCK_EVT_FEAT_PERIODIC
| CLOCK_EVT_FEAT_ONESHOT
503 | CLOCK_EVT_FEAT_C3STOP
| CLOCK_EVT_FEAT_DUMMY
,
505 .set_mode
= lapic_timer_setup
,
506 .set_next_event
= lapic_next_event
,
507 .broadcast
= lapic_timer_broadcast
,
511 static DEFINE_PER_CPU(struct clock_event_device
, lapic_events
);
514 * Setup the local APIC timer for this CPU. Copy the initialized values
515 * of the boot CPU and register the clock event in the framework.
517 static void __cpuinit
setup_APIC_timer(void)
519 struct clock_event_device
*levt
= &__get_cpu_var(lapic_events
);
521 if (this_cpu_has(X86_FEATURE_ARAT
)) {
522 lapic_clockevent
.features
&= ~CLOCK_EVT_FEAT_C3STOP
;
523 /* Make LAPIC timer preferrable over percpu HPET */
524 lapic_clockevent
.rating
= 150;
527 memcpy(levt
, &lapic_clockevent
, sizeof(*levt
));
528 levt
->cpumask
= cpumask_of(smp_processor_id());
530 clockevents_register_device(levt
);
534 * In this functions we calibrate APIC bus clocks to the external timer.
536 * We want to do the calibration only once since we want to have local timer
537 * irqs syncron. CPUs connected by the same APIC bus have the very same bus
540 * This was previously done by reading the PIT/HPET and waiting for a wrap
541 * around to find out, that a tick has elapsed. I have a box, where the PIT
542 * readout is broken, so it never gets out of the wait loop again. This was
543 * also reported by others.
545 * Monitoring the jiffies value is inaccurate and the clockevents
546 * infrastructure allows us to do a simple substitution of the interrupt
549 * The calibration routine also uses the pm_timer when possible, as the PIT
550 * happens to run way too slow (factor 2.3 on my VAIO CoreDuo, which goes
551 * back to normal later in the boot process).
554 #define LAPIC_CAL_LOOPS (HZ/10)
556 static __initdata
int lapic_cal_loops
= -1;
557 static __initdata
long lapic_cal_t1
, lapic_cal_t2
;
558 static __initdata
unsigned long long lapic_cal_tsc1
, lapic_cal_tsc2
;
559 static __initdata
unsigned long lapic_cal_pm1
, lapic_cal_pm2
;
560 static __initdata
unsigned long lapic_cal_j1
, lapic_cal_j2
;
563 * Temporary interrupt handler.
565 static void __init
lapic_cal_handler(struct clock_event_device
*dev
)
567 unsigned long long tsc
= 0;
568 long tapic
= apic_read(APIC_TMCCT
);
569 unsigned long pm
= acpi_pm_read_early();
574 switch (lapic_cal_loops
++) {
576 lapic_cal_t1
= tapic
;
577 lapic_cal_tsc1
= tsc
;
579 lapic_cal_j1
= jiffies
;
582 case LAPIC_CAL_LOOPS
:
583 lapic_cal_t2
= tapic
;
584 lapic_cal_tsc2
= tsc
;
585 if (pm
< lapic_cal_pm1
)
586 pm
+= ACPI_PM_OVRRUN
;
588 lapic_cal_j2
= jiffies
;
594 calibrate_by_pmtimer(long deltapm
, long *delta
, long *deltatsc
)
596 const long pm_100ms
= PMTMR_TICKS_PER_SEC
/ 10;
597 const long pm_thresh
= pm_100ms
/ 100;
601 #ifndef CONFIG_X86_PM_TIMER
605 apic_printk(APIC_VERBOSE
, "... PM-Timer delta = %ld\n", deltapm
);
607 /* Check, if the PM timer is available */
611 mult
= clocksource_hz2mult(PMTMR_TICKS_PER_SEC
, 22);
613 if (deltapm
> (pm_100ms
- pm_thresh
) &&
614 deltapm
< (pm_100ms
+ pm_thresh
)) {
615 apic_printk(APIC_VERBOSE
, "... PM-Timer result ok\n");
619 res
= (((u64
)deltapm
) * mult
) >> 22;
620 do_div(res
, 1000000);
621 pr_warning("APIC calibration not consistent "
622 "with PM-Timer: %ldms instead of 100ms\n",(long)res
);
624 /* Correct the lapic counter value */
625 res
= (((u64
)(*delta
)) * pm_100ms
);
626 do_div(res
, deltapm
);
627 pr_info("APIC delta adjusted to PM-Timer: "
628 "%lu (%ld)\n", (unsigned long)res
, *delta
);
631 /* Correct the tsc counter value */
633 res
= (((u64
)(*deltatsc
)) * pm_100ms
);
634 do_div(res
, deltapm
);
635 apic_printk(APIC_VERBOSE
, "TSC delta adjusted to "
636 "PM-Timer: %lu (%ld)\n",
637 (unsigned long)res
, *deltatsc
);
638 *deltatsc
= (long)res
;
644 static int __init
calibrate_APIC_clock(void)
646 struct clock_event_device
*levt
= &__get_cpu_var(lapic_events
);
647 void (*real_handler
)(struct clock_event_device
*dev
);
648 unsigned long deltaj
;
649 long delta
, deltatsc
;
650 int pm_referenced
= 0;
653 * check if lapic timer has already been calibrated by platform
654 * specific routine, such as tsc calibration code. if so, we just fill
655 * in the clockevent structure and return.
658 if (lapic_timer_frequency
) {
659 apic_printk(APIC_VERBOSE
, "lapic timer already calibrated %d\n",
660 lapic_timer_frequency
);
661 lapic_clockevent
.mult
= div_sc(lapic_timer_frequency
/APIC_DIVISOR
,
662 TICK_NSEC
, lapic_clockevent
.shift
);
663 lapic_clockevent
.max_delta_ns
=
664 clockevent_delta2ns(0x7FFFFF, &lapic_clockevent
);
665 lapic_clockevent
.min_delta_ns
=
666 clockevent_delta2ns(0xF, &lapic_clockevent
);
667 lapic_clockevent
.features
&= ~CLOCK_EVT_FEAT_DUMMY
;
673 /* Replace the global interrupt handler */
674 real_handler
= global_clock_event
->event_handler
;
675 global_clock_event
->event_handler
= lapic_cal_handler
;
678 * Setup the APIC counter to maximum. There is no way the lapic
679 * can underflow in the 100ms detection time frame
681 __setup_APIC_LVTT(0xffffffff, 0, 0);
683 /* Let the interrupts run */
686 while (lapic_cal_loops
<= LAPIC_CAL_LOOPS
)
691 /* Restore the real event handler */
692 global_clock_event
->event_handler
= real_handler
;
694 /* Build delta t1-t2 as apic timer counts down */
695 delta
= lapic_cal_t1
- lapic_cal_t2
;
696 apic_printk(APIC_VERBOSE
, "... lapic delta = %ld\n", delta
);
698 deltatsc
= (long)(lapic_cal_tsc2
- lapic_cal_tsc1
);
700 /* we trust the PM based calibration if possible */
701 pm_referenced
= !calibrate_by_pmtimer(lapic_cal_pm2
- lapic_cal_pm1
,
704 /* Calculate the scaled math multiplication factor */
705 lapic_clockevent
.mult
= div_sc(delta
, TICK_NSEC
* LAPIC_CAL_LOOPS
,
706 lapic_clockevent
.shift
);
707 lapic_clockevent
.max_delta_ns
=
708 clockevent_delta2ns(0x7FFFFFFF, &lapic_clockevent
);
709 lapic_clockevent
.min_delta_ns
=
710 clockevent_delta2ns(0xF, &lapic_clockevent
);
712 lapic_timer_frequency
= (delta
* APIC_DIVISOR
) / LAPIC_CAL_LOOPS
;
714 apic_printk(APIC_VERBOSE
, "..... delta %ld\n", delta
);
715 apic_printk(APIC_VERBOSE
, "..... mult: %u\n", lapic_clockevent
.mult
);
716 apic_printk(APIC_VERBOSE
, "..... calibration result: %u\n",
717 lapic_timer_frequency
);
720 apic_printk(APIC_VERBOSE
, "..... CPU clock speed is "
722 (deltatsc
/ LAPIC_CAL_LOOPS
) / (1000000 / HZ
),
723 (deltatsc
/ LAPIC_CAL_LOOPS
) % (1000000 / HZ
));
726 apic_printk(APIC_VERBOSE
, "..... host bus clock speed is "
728 lapic_timer_frequency
/ (1000000 / HZ
),
729 lapic_timer_frequency
% (1000000 / HZ
));
732 * Do a sanity check on the APIC calibration result
734 if (lapic_timer_frequency
< (1000000 / HZ
)) {
736 pr_warning("APIC frequency too slow, disabling apic timer\n");
740 levt
->features
&= ~CLOCK_EVT_FEAT_DUMMY
;
743 * PM timer calibration failed or not turned on
744 * so lets try APIC timer based calibration
746 if (!pm_referenced
) {
747 apic_printk(APIC_VERBOSE
, "... verify APIC timer\n");
750 * Setup the apic timer manually
752 levt
->event_handler
= lapic_cal_handler
;
753 lapic_timer_setup(CLOCK_EVT_MODE_PERIODIC
, levt
);
754 lapic_cal_loops
= -1;
756 /* Let the interrupts run */
759 while (lapic_cal_loops
<= LAPIC_CAL_LOOPS
)
762 /* Stop the lapic timer */
763 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN
, levt
);
766 deltaj
= lapic_cal_j2
- lapic_cal_j1
;
767 apic_printk(APIC_VERBOSE
, "... jiffies delta = %lu\n", deltaj
);
769 /* Check, if the jiffies result is consistent */
770 if (deltaj
>= LAPIC_CAL_LOOPS
-2 && deltaj
<= LAPIC_CAL_LOOPS
+2)
771 apic_printk(APIC_VERBOSE
, "... jiffies result ok\n");
773 levt
->features
|= CLOCK_EVT_FEAT_DUMMY
;
777 if (levt
->features
& CLOCK_EVT_FEAT_DUMMY
) {
778 pr_warning("APIC timer disabled due to verification failure\n");
786 * Setup the boot APIC
788 * Calibrate and verify the result.
790 void __init
setup_boot_APIC_clock(void)
793 * The local apic timer can be disabled via the kernel
794 * commandline or from the CPU detection code. Register the lapic
795 * timer as a dummy clock event source on SMP systems, so the
796 * broadcast mechanism is used. On UP systems simply ignore it.
798 if (disable_apic_timer
) {
799 pr_info("Disabling APIC timer\n");
800 /* No broadcast on UP ! */
801 if (num_possible_cpus() > 1) {
802 lapic_clockevent
.mult
= 1;
808 apic_printk(APIC_VERBOSE
, "Using local APIC timer interrupts.\n"
809 "calibrating APIC timer ...\n");
811 if (calibrate_APIC_clock()) {
812 /* No broadcast on UP ! */
813 if (num_possible_cpus() > 1)
819 * If nmi_watchdog is set to IO_APIC, we need the
820 * PIT/HPET going. Otherwise register lapic as a dummy
823 lapic_clockevent
.features
&= ~CLOCK_EVT_FEAT_DUMMY
;
825 /* Setup the lapic or request the broadcast */
829 void __cpuinit
setup_secondary_APIC_clock(void)
835 * The guts of the apic timer interrupt
837 static void local_apic_timer_interrupt(void)
839 int cpu
= smp_processor_id();
840 struct clock_event_device
*evt
= &per_cpu(lapic_events
, cpu
);
843 * Normally we should not be here till LAPIC has been initialized but
844 * in some cases like kdump, its possible that there is a pending LAPIC
845 * timer interrupt from previous kernel's context and is delivered in
846 * new kernel the moment interrupts are enabled.
848 * Interrupts are enabled early and LAPIC is setup much later, hence
849 * its possible that when we get here evt->event_handler is NULL.
850 * Check for event_handler being NULL and discard the interrupt as
853 if (!evt
->event_handler
) {
854 pr_warning("Spurious LAPIC timer interrupt on cpu %d\n", cpu
);
856 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN
, evt
);
861 * the NMI deadlock-detector uses this.
863 inc_irq_stat(apic_timer_irqs
);
865 evt
->event_handler(evt
);
869 * Local APIC timer interrupt. This is the most natural way for doing
870 * local interrupts, but local timer interrupts can be emulated by
871 * broadcast interrupts too. [in case the hw doesn't support APIC timers]
873 * [ if a single-CPU system runs an SMP kernel then we call the local
874 * interrupt as well. Thus we cannot inline the local irq ... ]
876 void __irq_entry
smp_apic_timer_interrupt(struct pt_regs
*regs
)
878 struct pt_regs
*old_regs
= set_irq_regs(regs
);
881 * NOTE! We'd better ACK the irq immediately,
882 * because timer handling can be slow.
886 * update_process_times() expects us to have done irq_enter().
887 * Besides, if we don't timer interrupts ignore the global
888 * interrupt lock, which is the WrongThing (tm) to do.
892 local_apic_timer_interrupt();
895 set_irq_regs(old_regs
);
898 int setup_profiling_timer(unsigned int multiplier
)
904 * Local APIC start and shutdown
908 * clear_local_APIC - shutdown the local APIC
910 * This is called, when a CPU is disabled and before rebooting, so the state of
911 * the local APIC has no dangling leftovers. Also used to cleanout any BIOS
912 * leftovers during boot.
914 void clear_local_APIC(void)
919 /* APIC hasn't been mapped yet */
920 if (!x2apic_mode
&& !apic_phys
)
923 maxlvt
= lapic_get_maxlvt();
925 * Masking an LVT entry can trigger a local APIC error
926 * if the vector is zero. Mask LVTERR first to prevent this.
929 v
= ERROR_APIC_VECTOR
; /* any non-zero vector will do */
930 apic_write(APIC_LVTERR
, v
| APIC_LVT_MASKED
);
933 * Careful: we have to set masks only first to deassert
934 * any level-triggered sources.
936 v
= apic_read(APIC_LVTT
);
937 apic_write(APIC_LVTT
, v
| APIC_LVT_MASKED
);
938 v
= apic_read(APIC_LVT0
);
939 apic_write(APIC_LVT0
, v
| APIC_LVT_MASKED
);
940 v
= apic_read(APIC_LVT1
);
941 apic_write(APIC_LVT1
, v
| APIC_LVT_MASKED
);
943 v
= apic_read(APIC_LVTPC
);
944 apic_write(APIC_LVTPC
, v
| APIC_LVT_MASKED
);
947 /* lets not touch this if we didn't frob it */
948 #ifdef CONFIG_X86_THERMAL_VECTOR
950 v
= apic_read(APIC_LVTTHMR
);
951 apic_write(APIC_LVTTHMR
, v
| APIC_LVT_MASKED
);
954 #ifdef CONFIG_X86_MCE_INTEL
956 v
= apic_read(APIC_LVTCMCI
);
957 if (!(v
& APIC_LVT_MASKED
))
958 apic_write(APIC_LVTCMCI
, v
| APIC_LVT_MASKED
);
963 * Clean APIC state for other OSs:
965 apic_write(APIC_LVTT
, APIC_LVT_MASKED
);
966 apic_write(APIC_LVT0
, APIC_LVT_MASKED
);
967 apic_write(APIC_LVT1
, APIC_LVT_MASKED
);
969 apic_write(APIC_LVTERR
, APIC_LVT_MASKED
);
971 apic_write(APIC_LVTPC
, APIC_LVT_MASKED
);
973 /* Integrated APIC (!82489DX) ? */
974 if (lapic_is_integrated()) {
976 /* Clear ESR due to Pentium errata 3AP and 11AP */
977 apic_write(APIC_ESR
, 0);
983 * disable_local_APIC - clear and disable the local APIC
985 void disable_local_APIC(void)
989 /* APIC hasn't been mapped yet */
990 if (!x2apic_mode
&& !apic_phys
)
996 * Disable APIC (implies clearing of registers
999 value
= apic_read(APIC_SPIV
);
1000 value
&= ~APIC_SPIV_APIC_ENABLED
;
1001 apic_write(APIC_SPIV
, value
);
1003 #ifdef CONFIG_X86_32
1005 * When LAPIC was disabled by the BIOS and enabled by the kernel,
1006 * restore the disabled state.
1008 if (enabled_via_apicbase
) {
1011 rdmsr(MSR_IA32_APICBASE
, l
, h
);
1012 l
&= ~MSR_IA32_APICBASE_ENABLE
;
1013 wrmsr(MSR_IA32_APICBASE
, l
, h
);
1019 * If Linux enabled the LAPIC against the BIOS default disable it down before
1020 * re-entering the BIOS on shutdown. Otherwise the BIOS may get confused and
1021 * not power-off. Additionally clear all LVT entries before disable_local_APIC
1022 * for the case where Linux didn't enable the LAPIC.
1024 void lapic_shutdown(void)
1026 unsigned long flags
;
1028 if (!cpu_has_apic
&& !apic_from_smp_config())
1031 local_irq_save(flags
);
1033 #ifdef CONFIG_X86_32
1034 if (!enabled_via_apicbase
)
1038 disable_local_APIC();
1041 local_irq_restore(flags
);
1045 * This is to verify that we're looking at a real local APIC.
1046 * Check these against your board if the CPUs aren't getting
1047 * started for no apparent reason.
1049 int __init
verify_local_APIC(void)
1051 unsigned int reg0
, reg1
;
1054 * The version register is read-only in a real APIC.
1056 reg0
= apic_read(APIC_LVR
);
1057 apic_printk(APIC_DEBUG
, "Getting VERSION: %x\n", reg0
);
1058 apic_write(APIC_LVR
, reg0
^ APIC_LVR_MASK
);
1059 reg1
= apic_read(APIC_LVR
);
1060 apic_printk(APIC_DEBUG
, "Getting VERSION: %x\n", reg1
);
1063 * The two version reads above should print the same
1064 * numbers. If the second one is different, then we
1065 * poke at a non-APIC.
1071 * Check if the version looks reasonably.
1073 reg1
= GET_APIC_VERSION(reg0
);
1074 if (reg1
== 0x00 || reg1
== 0xff)
1076 reg1
= lapic_get_maxlvt();
1077 if (reg1
< 0x02 || reg1
== 0xff)
1081 * The ID register is read/write in a real APIC.
1083 reg0
= apic_read(APIC_ID
);
1084 apic_printk(APIC_DEBUG
, "Getting ID: %x\n", reg0
);
1085 apic_write(APIC_ID
, reg0
^ apic
->apic_id_mask
);
1086 reg1
= apic_read(APIC_ID
);
1087 apic_printk(APIC_DEBUG
, "Getting ID: %x\n", reg1
);
1088 apic_write(APIC_ID
, reg0
);
1089 if (reg1
!= (reg0
^ apic
->apic_id_mask
))
1093 * The next two are just to see if we have sane values.
1094 * They're only really relevant if we're in Virtual Wire
1095 * compatibility mode, but most boxes are anymore.
1097 reg0
= apic_read(APIC_LVT0
);
1098 apic_printk(APIC_DEBUG
, "Getting LVT0: %x\n", reg0
);
1099 reg1
= apic_read(APIC_LVT1
);
1100 apic_printk(APIC_DEBUG
, "Getting LVT1: %x\n", reg1
);
1106 * sync_Arb_IDs - synchronize APIC bus arbitration IDs
1108 void __init
sync_Arb_IDs(void)
1111 * Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 And not
1114 if (modern_apic() || boot_cpu_data
.x86_vendor
== X86_VENDOR_AMD
)
1120 apic_wait_icr_idle();
1122 apic_printk(APIC_DEBUG
, "Synchronizing Arb IDs.\n");
1123 apic_write(APIC_ICR
, APIC_DEST_ALLINC
|
1124 APIC_INT_LEVELTRIG
| APIC_DM_INIT
);
1128 * An initial setup of the virtual wire mode.
1130 void __init
init_bsp_APIC(void)
1135 * Don't do the setup now if we have a SMP BIOS as the
1136 * through-I/O-APIC virtual wire mode might be active.
1138 if (smp_found_config
|| !cpu_has_apic
)
1142 * Do not trust the local APIC being empty at bootup.
1149 value
= apic_read(APIC_SPIV
);
1150 value
&= ~APIC_VECTOR_MASK
;
1151 value
|= APIC_SPIV_APIC_ENABLED
;
1153 #ifdef CONFIG_X86_32
1154 /* This bit is reserved on P4/Xeon and should be cleared */
1155 if ((boot_cpu_data
.x86_vendor
== X86_VENDOR_INTEL
) &&
1156 (boot_cpu_data
.x86
== 15))
1157 value
&= ~APIC_SPIV_FOCUS_DISABLED
;
1160 value
|= APIC_SPIV_FOCUS_DISABLED
;
1161 value
|= SPURIOUS_APIC_VECTOR
;
1162 apic_write(APIC_SPIV
, value
);
1165 * Set up the virtual wire mode.
1167 apic_write(APIC_LVT0
, APIC_DM_EXTINT
);
1168 value
= APIC_DM_NMI
;
1169 if (!lapic_is_integrated()) /* 82489DX */
1170 value
|= APIC_LVT_LEVEL_TRIGGER
;
1171 apic_write(APIC_LVT1
, value
);
1174 static void __cpuinit
lapic_setup_esr(void)
1176 unsigned int oldvalue
, value
, maxlvt
;
1178 if (!lapic_is_integrated()) {
1179 pr_info("No ESR for 82489DX.\n");
1183 if (apic
->disable_esr
) {
1185 * Something untraceable is creating bad interrupts on
1186 * secondary quads ... for the moment, just leave the
1187 * ESR disabled - we can't do anything useful with the
1188 * errors anyway - mbligh
1190 pr_info("Leaving ESR disabled.\n");
1194 maxlvt
= lapic_get_maxlvt();
1195 if (maxlvt
> 3) /* Due to the Pentium erratum 3AP. */
1196 apic_write(APIC_ESR
, 0);
1197 oldvalue
= apic_read(APIC_ESR
);
1199 /* enables sending errors */
1200 value
= ERROR_APIC_VECTOR
;
1201 apic_write(APIC_LVTERR
, value
);
1204 * spec says clear errors after enabling vector.
1207 apic_write(APIC_ESR
, 0);
1208 value
= apic_read(APIC_ESR
);
1209 if (value
!= oldvalue
)
1210 apic_printk(APIC_VERBOSE
, "ESR value before enabling "
1211 "vector: 0x%08x after: 0x%08x\n",
1216 * setup_local_APIC - setup the local APIC
1218 * Used to setup local APIC while initializing BSP or bringin up APs.
1219 * Always called with preemption disabled.
1221 void __cpuinit
setup_local_APIC(void)
1223 int cpu
= smp_processor_id();
1224 unsigned int value
, queued
;
1225 int i
, j
, acked
= 0;
1226 unsigned long long tsc
= 0, ntsc
;
1227 long long max_loops
= cpu_khz
;
1233 disable_ioapic_support();
1237 #ifdef CONFIG_X86_32
1238 /* Pound the ESR really hard over the head with a big hammer - mbligh */
1239 if (lapic_is_integrated() && apic
->disable_esr
) {
1240 apic_write(APIC_ESR
, 0);
1241 apic_write(APIC_ESR
, 0);
1242 apic_write(APIC_ESR
, 0);
1243 apic_write(APIC_ESR
, 0);
1246 perf_events_lapic_init();
1249 * Double-check whether this APIC is really registered.
1250 * This is meaningless in clustered apic mode, so we skip it.
1252 BUG_ON(!apic
->apic_id_registered());
1255 * Intel recommends to set DFR, LDR and TPR before enabling
1256 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
1257 * document number 292116). So here it goes...
1259 apic
->init_apic_ldr();
1261 #ifdef CONFIG_X86_32
1263 * APIC LDR is initialized. If logical_apicid mapping was
1264 * initialized during get_smp_config(), make sure it matches the
1267 i
= early_per_cpu(x86_cpu_to_logical_apicid
, cpu
);
1268 WARN_ON(i
!= BAD_APICID
&& i
!= logical_smp_processor_id());
1269 /* always use the value from LDR */
1270 early_per_cpu(x86_cpu_to_logical_apicid
, cpu
) =
1271 logical_smp_processor_id();
1274 * Some NUMA implementations (NUMAQ) don't initialize apicid to
1275 * node mapping during NUMA init. Now that logical apicid is
1276 * guaranteed to be known, give it another chance. This is already
1277 * a bit too late - percpu allocation has already happened without
1278 * proper NUMA affinity.
1280 if (apic
->x86_32_numa_cpu_node
)
1281 set_apicid_to_node(early_per_cpu(x86_cpu_to_apicid
, cpu
),
1282 apic
->x86_32_numa_cpu_node(cpu
));
1286 * Set Task Priority to 'accept all'. We never change this
1289 value
= apic_read(APIC_TASKPRI
);
1290 value
&= ~APIC_TPRI_MASK
;
1291 apic_write(APIC_TASKPRI
, value
);
1294 * After a crash, we no longer service the interrupts and a pending
1295 * interrupt from previous kernel might still have ISR bit set.
1297 * Most probably by now CPU has serviced that pending interrupt and
1298 * it might not have done the ack_APIC_irq() because it thought,
1299 * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
1300 * does not clear the ISR bit and cpu thinks it has already serivced
1301 * the interrupt. Hence a vector might get locked. It was noticed
1302 * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
1306 for (i
= APIC_ISR_NR
- 1; i
>= 0; i
--)
1307 queued
|= apic_read(APIC_IRR
+ i
*0x10);
1309 for (i
= APIC_ISR_NR
- 1; i
>= 0; i
--) {
1310 value
= apic_read(APIC_ISR
+ i
*0x10);
1311 for (j
= 31; j
>= 0; j
--) {
1312 if (value
& (1<<j
)) {
1319 printk(KERN_ERR
"LAPIC pending interrupts after %d EOI\n",
1325 max_loops
= (cpu_khz
<< 10) - (ntsc
- tsc
);
1328 } while (queued
&& max_loops
> 0);
1329 WARN_ON(max_loops
<= 0);
1332 * Now that we are all set up, enable the APIC
1334 value
= apic_read(APIC_SPIV
);
1335 value
&= ~APIC_VECTOR_MASK
;
1339 value
|= APIC_SPIV_APIC_ENABLED
;
1341 #ifdef CONFIG_X86_32
1343 * Some unknown Intel IO/APIC (or APIC) errata is biting us with
1344 * certain networking cards. If high frequency interrupts are
1345 * happening on a particular IOAPIC pin, plus the IOAPIC routing
1346 * entry is masked/unmasked at a high rate as well then sooner or
1347 * later IOAPIC line gets 'stuck', no more interrupts are received
1348 * from the device. If focus CPU is disabled then the hang goes
1351 * [ This bug can be reproduced easily with a level-triggered
1352 * PCI Ne2000 networking cards and PII/PIII processors, dual
1356 * Actually disabling the focus CPU check just makes the hang less
1357 * frequent as it makes the interrupt distributon model be more
1358 * like LRU than MRU (the short-term load is more even across CPUs).
1359 * See also the comment in end_level_ioapic_irq(). --macro
1363 * - enable focus processor (bit==0)
1364 * - 64bit mode always use processor focus
1365 * so no need to set it
1367 value
&= ~APIC_SPIV_FOCUS_DISABLED
;
1371 * Set spurious IRQ vector
1373 value
|= SPURIOUS_APIC_VECTOR
;
1374 apic_write(APIC_SPIV
, value
);
1377 * Set up LVT0, LVT1:
1379 * set up through-local-APIC on the BP's LINT0. This is not
1380 * strictly necessary in pure symmetric-IO mode, but sometimes
1381 * we delegate interrupts to the 8259A.
1384 * TODO: set up through-local-APIC from through-I/O-APIC? --macro
1386 value
= apic_read(APIC_LVT0
) & APIC_LVT_MASKED
;
1387 if (!cpu
&& (pic_mode
|| !value
)) {
1388 value
= APIC_DM_EXTINT
;
1389 apic_printk(APIC_VERBOSE
, "enabled ExtINT on CPU#%d\n", cpu
);
1391 value
= APIC_DM_EXTINT
| APIC_LVT_MASKED
;
1392 apic_printk(APIC_VERBOSE
, "masked ExtINT on CPU#%d\n", cpu
);
1394 apic_write(APIC_LVT0
, value
);
1397 * only the BP should see the LINT1 NMI signal, obviously.
1400 value
= APIC_DM_NMI
;
1402 value
= APIC_DM_NMI
| APIC_LVT_MASKED
;
1403 if (!lapic_is_integrated()) /* 82489DX */
1404 value
|= APIC_LVT_LEVEL_TRIGGER
;
1405 apic_write(APIC_LVT1
, value
);
1407 #ifdef CONFIG_X86_MCE_INTEL
1408 /* Recheck CMCI information after local APIC is up on CPU #0 */
1414 void __cpuinit
end_local_APIC_setup(void)
1418 #ifdef CONFIG_X86_32
1421 /* Disable the local apic timer */
1422 value
= apic_read(APIC_LVTT
);
1423 value
|= (APIC_LVT_MASKED
| LOCAL_TIMER_VECTOR
);
1424 apic_write(APIC_LVTT
, value
);
1431 void __init
bsp_end_local_APIC_setup(void)
1433 end_local_APIC_setup();
1436 * Now that local APIC setup is completed for BP, configure the fault
1437 * handling for interrupt remapping.
1439 if (intr_remapping_enabled
)
1440 enable_drhd_fault_handling();
1444 #ifdef CONFIG_X86_X2APIC
1446 * Need to disable xapic and x2apic at the same time and then enable xapic mode
1448 static inline void __disable_x2apic(u64 msr
)
1450 wrmsrl(MSR_IA32_APICBASE
,
1451 msr
& ~(X2APIC_ENABLE
| XAPIC_ENABLE
));
1452 wrmsrl(MSR_IA32_APICBASE
, msr
& ~X2APIC_ENABLE
);
1455 static __init
void disable_x2apic(void)
1459 if (!cpu_has_x2apic
)
1462 rdmsrl(MSR_IA32_APICBASE
, msr
);
1463 if (msr
& X2APIC_ENABLE
) {
1464 u32 x2apic_id
= read_apic_id();
1466 if (x2apic_id
>= 255)
1467 panic("Cannot disable x2apic, id: %08x\n", x2apic_id
);
1469 pr_info("Disabling x2apic\n");
1470 __disable_x2apic(msr
);
1473 clear_cpu_cap(&cpu_data(0), X86_FEATURE_X2APIC
);
1474 setup_clear_cpu_cap(X86_FEATURE_X2APIC
);
1477 x2apic_disabled
= 1;
1480 register_lapic_address(mp_lapic_addr
);
1484 void check_x2apic(void)
1486 if (x2apic_enabled()) {
1487 pr_info("x2apic enabled by BIOS, switching to x2apic ops\n");
1488 x2apic_preenabled
= x2apic_mode
= 1;
1492 void enable_x2apic(void)
1496 rdmsrl(MSR_IA32_APICBASE
, msr
);
1497 if (x2apic_disabled
) {
1498 __disable_x2apic(msr
);
1505 if (!(msr
& X2APIC_ENABLE
)) {
1506 printk_once(KERN_INFO
"Enabling x2apic\n");
1507 wrmsrl(MSR_IA32_APICBASE
, msr
| X2APIC_ENABLE
);
1510 #endif /* CONFIG_X86_X2APIC */
1512 int __init
enable_IR(void)
1514 #ifdef CONFIG_IRQ_REMAP
1515 if (!intr_remapping_supported()) {
1516 pr_debug("intr-remapping not supported\n");
1520 if (!x2apic_preenabled
&& skip_ioapic_setup
) {
1521 pr_info("Skipped enabling intr-remap because of skipping "
1526 return enable_intr_remapping();
1531 void __init
enable_IR_x2apic(void)
1533 unsigned long flags
;
1534 int ret
, x2apic_enabled
= 0;
1535 int dmar_table_init_ret
;
1537 dmar_table_init_ret
= dmar_table_init();
1538 if (dmar_table_init_ret
&& !x2apic_supported())
1541 ret
= save_ioapic_entries();
1543 pr_info("Saving IO-APIC state failed: %d\n", ret
);
1547 local_irq_save(flags
);
1548 legacy_pic
->mask_all();
1549 mask_ioapic_entries();
1551 if (x2apic_preenabled
&& nox2apic
)
1554 if (dmar_table_init_ret
)
1559 if (!x2apic_supported())
1563 /* IR is required if there is APIC ID > 255 even when running
1566 if (max_physical_apicid
> 255 ||
1567 !hypervisor_x2apic_available()) {
1568 if (x2apic_preenabled
)
1573 * without IR all CPUs can be addressed by IOAPIC/MSI
1574 * only in physical mode
1576 x2apic_force_phys();
1579 if (ret
== IRQ_REMAP_XAPIC_MODE
) {
1580 pr_info("x2apic not enabled, IRQ remapping is in xapic mode\n");
1586 if (x2apic_supported() && !x2apic_mode
) {
1589 pr_info("Enabled x2apic\n");
1593 if (ret
< 0) /* IR enabling failed */
1594 restore_ioapic_entries();
1595 legacy_pic
->restore_mask();
1596 local_irq_restore(flags
);
1599 #ifdef CONFIG_X86_64
1601 * Detect and enable local APICs on non-SMP boards.
1602 * Original code written by Keir Fraser.
1603 * On AMD64 we trust the BIOS - if it says no APIC it is likely
1604 * not correctly set up (usually the APIC timer won't work etc.)
1606 static int __init
detect_init_APIC(void)
1608 if (!cpu_has_apic
) {
1609 pr_info("No local APIC present\n");
1613 mp_lapic_addr
= APIC_DEFAULT_PHYS_BASE
;
1618 static int __init
apic_verify(void)
1623 * The APIC feature bit should now be enabled
1626 features
= cpuid_edx(1);
1627 if (!(features
& (1 << X86_FEATURE_APIC
))) {
1628 pr_warning("Could not enable APIC!\n");
1631 set_cpu_cap(&boot_cpu_data
, X86_FEATURE_APIC
);
1632 mp_lapic_addr
= APIC_DEFAULT_PHYS_BASE
;
1634 /* The BIOS may have set up the APIC at some other address */
1635 if (boot_cpu_data
.x86
>= 6) {
1636 rdmsr(MSR_IA32_APICBASE
, l
, h
);
1637 if (l
& MSR_IA32_APICBASE_ENABLE
)
1638 mp_lapic_addr
= l
& MSR_IA32_APICBASE_BASE
;
1641 pr_info("Found and enabled local APIC!\n");
1645 int __init
apic_force_enable(unsigned long addr
)
1653 * Some BIOSes disable the local APIC in the APIC_BASE
1654 * MSR. This can only be done in software for Intel P6 or later
1655 * and AMD K7 (Model > 1) or later.
1657 if (boot_cpu_data
.x86
>= 6) {
1658 rdmsr(MSR_IA32_APICBASE
, l
, h
);
1659 if (!(l
& MSR_IA32_APICBASE_ENABLE
)) {
1660 pr_info("Local APIC disabled by BIOS -- reenabling.\n");
1661 l
&= ~MSR_IA32_APICBASE_BASE
;
1662 l
|= MSR_IA32_APICBASE_ENABLE
| addr
;
1663 wrmsr(MSR_IA32_APICBASE
, l
, h
);
1664 enabled_via_apicbase
= 1;
1667 return apic_verify();
1671 * Detect and initialize APIC
1673 static int __init
detect_init_APIC(void)
1675 /* Disabled by kernel option? */
1679 switch (boot_cpu_data
.x86_vendor
) {
1680 case X86_VENDOR_AMD
:
1681 if ((boot_cpu_data
.x86
== 6 && boot_cpu_data
.x86_model
> 1) ||
1682 (boot_cpu_data
.x86
>= 15))
1685 case X86_VENDOR_INTEL
:
1686 if (boot_cpu_data
.x86
== 6 || boot_cpu_data
.x86
== 15 ||
1687 (boot_cpu_data
.x86
== 5 && cpu_has_apic
))
1694 if (!cpu_has_apic
) {
1696 * Over-ride BIOS and try to enable the local APIC only if
1697 * "lapic" specified.
1699 if (!force_enable_local_apic
) {
1700 pr_info("Local APIC disabled by BIOS -- "
1701 "you can enable it with \"lapic\"\n");
1704 if (apic_force_enable(APIC_DEFAULT_PHYS_BASE
))
1716 pr_info("No local APIC present or hardware disabled\n");
1722 * init_apic_mappings - initialize APIC mappings
1724 void __init
init_apic_mappings(void)
1726 unsigned int new_apicid
;
1729 boot_cpu_physical_apicid
= read_apic_id();
1733 /* If no local APIC can be found return early */
1734 if (!smp_found_config
&& detect_init_APIC()) {
1735 /* lets NOP'ify apic operations */
1736 pr_info("APIC: disable apic facility\n");
1739 apic_phys
= mp_lapic_addr
;
1742 * acpi lapic path already maps that address in
1743 * acpi_register_lapic_address()
1745 if (!acpi_lapic
&& !smp_found_config
)
1746 register_lapic_address(apic_phys
);
1750 * Fetch the APIC ID of the BSP in case we have a
1751 * default configuration (or the MP table is broken).
1753 new_apicid
= read_apic_id();
1754 if (boot_cpu_physical_apicid
!= new_apicid
) {
1755 boot_cpu_physical_apicid
= new_apicid
;
1757 * yeah -- we lie about apic_version
1758 * in case if apic was disabled via boot option
1759 * but it's not a problem for SMP compiled kernel
1760 * since smp_sanity_check is prepared for such a case
1761 * and disable smp mode
1763 apic_version
[new_apicid
] =
1764 GET_APIC_VERSION(apic_read(APIC_LVR
));
1768 void __init
register_lapic_address(unsigned long address
)
1770 mp_lapic_addr
= address
;
1773 set_fixmap_nocache(FIX_APIC_BASE
, address
);
1774 apic_printk(APIC_VERBOSE
, "mapped APIC to %16lx (%16lx)\n",
1775 APIC_BASE
, mp_lapic_addr
);
1777 if (boot_cpu_physical_apicid
== -1U) {
1778 boot_cpu_physical_apicid
= read_apic_id();
1779 apic_version
[boot_cpu_physical_apicid
] =
1780 GET_APIC_VERSION(apic_read(APIC_LVR
));
1785 * This initializes the IO-APIC and APIC hardware if this is
1788 int apic_version
[MAX_LOCAL_APIC
];
1790 int __init
APIC_init_uniprocessor(void)
1793 pr_info("Apic disabled\n");
1796 #ifdef CONFIG_X86_64
1797 if (!cpu_has_apic
) {
1799 pr_info("Apic disabled by BIOS\n");
1803 if (!smp_found_config
&& !cpu_has_apic
)
1807 * Complain if the BIOS pretends there is one.
1809 if (!cpu_has_apic
&&
1810 APIC_INTEGRATED(apic_version
[boot_cpu_physical_apicid
])) {
1811 pr_err("BIOS bug, local APIC 0x%x not detected!...\n",
1812 boot_cpu_physical_apicid
);
1817 default_setup_apic_routing();
1819 verify_local_APIC();
1822 #ifdef CONFIG_X86_64
1823 apic_write(APIC_ID
, SET_APIC_ID(boot_cpu_physical_apicid
));
1826 * Hack: In case of kdump, after a crash, kernel might be booting
1827 * on a cpu with non-zero lapic id. But boot_cpu_physical_apicid
1828 * might be zero if read from MP tables. Get it from LAPIC.
1830 # ifdef CONFIG_CRASH_DUMP
1831 boot_cpu_physical_apicid
= read_apic_id();
1834 physid_set_mask_of_physid(boot_cpu_physical_apicid
, &phys_cpu_present_map
);
1837 #ifdef CONFIG_X86_IO_APIC
1839 * Now enable IO-APICs, actually call clear_IO_APIC
1840 * We need clear_IO_APIC before enabling error vector
1842 if (!skip_ioapic_setup
&& nr_ioapics
)
1846 bsp_end_local_APIC_setup();
1848 #ifdef CONFIG_X86_IO_APIC
1849 if (smp_found_config
&& !skip_ioapic_setup
&& nr_ioapics
)
1856 x86_init
.timers
.setup_percpu_clockev();
1861 * Local APIC interrupts
1865 * This interrupt should _never_ happen with our APIC/SMP architecture
1867 void smp_spurious_interrupt(struct pt_regs
*regs
)
1874 * Check if this really is a spurious interrupt and ACK it
1875 * if it is a vectored one. Just in case...
1876 * Spurious interrupts should not be ACKed.
1878 v
= apic_read(APIC_ISR
+ ((SPURIOUS_APIC_VECTOR
& ~0x1f) >> 1));
1879 if (v
& (1 << (SPURIOUS_APIC_VECTOR
& 0x1f)))
1882 inc_irq_stat(irq_spurious_count
);
1884 /* see sw-dev-man vol 3, chapter 7.4.13.5 */
1885 pr_info("spurious APIC interrupt on CPU#%d, "
1886 "should never happen.\n", smp_processor_id());
1891 * This interrupt should never happen with our APIC/SMP architecture
1893 void smp_error_interrupt(struct pt_regs
*regs
)
1897 static const char * const error_interrupt_reason
[] = {
1898 "Send CS error", /* APIC Error Bit 0 */
1899 "Receive CS error", /* APIC Error Bit 1 */
1900 "Send accept error", /* APIC Error Bit 2 */
1901 "Receive accept error", /* APIC Error Bit 3 */
1902 "Redirectable IPI", /* APIC Error Bit 4 */
1903 "Send illegal vector", /* APIC Error Bit 5 */
1904 "Received illegal vector", /* APIC Error Bit 6 */
1905 "Illegal register address", /* APIC Error Bit 7 */
1910 /* First tickle the hardware, only then report what went on. -- REW */
1911 v0
= apic_read(APIC_ESR
);
1912 apic_write(APIC_ESR
, 0);
1913 v1
= apic_read(APIC_ESR
);
1915 atomic_inc(&irq_err_count
);
1917 apic_printk(APIC_DEBUG
, KERN_DEBUG
"APIC error on CPU%d: %02x(%02x)",
1918 smp_processor_id(), v0
, v1
);
1923 apic_printk(APIC_DEBUG
, KERN_CONT
" : %s", error_interrupt_reason
[i
]);
1928 apic_printk(APIC_DEBUG
, KERN_CONT
"\n");
1934 * connect_bsp_APIC - attach the APIC to the interrupt system
1936 void __init
connect_bsp_APIC(void)
1938 #ifdef CONFIG_X86_32
1941 * Do not trust the local APIC being empty at bootup.
1945 * PIC mode, enable APIC mode in the IMCR, i.e. connect BSP's
1946 * local APIC to INT and NMI lines.
1948 apic_printk(APIC_VERBOSE
, "leaving PIC mode, "
1949 "enabling APIC mode.\n");
1953 if (apic
->enable_apic_mode
)
1954 apic
->enable_apic_mode();
1958 * disconnect_bsp_APIC - detach the APIC from the interrupt system
1959 * @virt_wire_setup: indicates, whether virtual wire mode is selected
1961 * Virtual wire mode is necessary to deliver legacy interrupts even when the
1964 void disconnect_bsp_APIC(int virt_wire_setup
)
1968 #ifdef CONFIG_X86_32
1971 * Put the board back into PIC mode (has an effect only on
1972 * certain older boards). Note that APIC interrupts, including
1973 * IPIs, won't work beyond this point! The only exception are
1976 apic_printk(APIC_VERBOSE
, "disabling APIC mode, "
1977 "entering PIC mode.\n");
1983 /* Go back to Virtual Wire compatibility mode */
1985 /* For the spurious interrupt use vector F, and enable it */
1986 value
= apic_read(APIC_SPIV
);
1987 value
&= ~APIC_VECTOR_MASK
;
1988 value
|= APIC_SPIV_APIC_ENABLED
;
1990 apic_write(APIC_SPIV
, value
);
1992 if (!virt_wire_setup
) {
1994 * For LVT0 make it edge triggered, active high,
1995 * external and enabled
1997 value
= apic_read(APIC_LVT0
);
1998 value
&= ~(APIC_MODE_MASK
| APIC_SEND_PENDING
|
1999 APIC_INPUT_POLARITY
| APIC_LVT_REMOTE_IRR
|
2000 APIC_LVT_LEVEL_TRIGGER
| APIC_LVT_MASKED
);
2001 value
|= APIC_LVT_REMOTE_IRR
| APIC_SEND_PENDING
;
2002 value
= SET_APIC_DELIVERY_MODE(value
, APIC_MODE_EXTINT
);
2003 apic_write(APIC_LVT0
, value
);
2006 apic_write(APIC_LVT0
, APIC_LVT_MASKED
);
2010 * For LVT1 make it edge triggered, active high,
2013 value
= apic_read(APIC_LVT1
);
2014 value
&= ~(APIC_MODE_MASK
| APIC_SEND_PENDING
|
2015 APIC_INPUT_POLARITY
| APIC_LVT_REMOTE_IRR
|
2016 APIC_LVT_LEVEL_TRIGGER
| APIC_LVT_MASKED
);
2017 value
|= APIC_LVT_REMOTE_IRR
| APIC_SEND_PENDING
;
2018 value
= SET_APIC_DELIVERY_MODE(value
, APIC_MODE_NMI
);
2019 apic_write(APIC_LVT1
, value
);
2022 void __cpuinit
generic_processor_info(int apicid
, int version
)
2024 int cpu
, max
= nr_cpu_ids
;
2025 bool boot_cpu_detected
= physid_isset(boot_cpu_physical_apicid
,
2026 phys_cpu_present_map
);
2029 * If boot cpu has not been detected yet, then only allow upto
2030 * nr_cpu_ids - 1 processors and keep one slot free for boot cpu
2032 if (!boot_cpu_detected
&& num_processors
>= nr_cpu_ids
- 1 &&
2033 apicid
!= boot_cpu_physical_apicid
) {
2034 int thiscpu
= max
+ disabled_cpus
- 1;
2037 "ACPI: NR_CPUS/possible_cpus limit of %i almost"
2038 " reached. Keeping one slot for boot cpu."
2039 " Processor %d/0x%x ignored.\n", max
, thiscpu
, apicid
);
2045 if (num_processors
>= nr_cpu_ids
) {
2046 int thiscpu
= max
+ disabled_cpus
;
2049 "ACPI: NR_CPUS/possible_cpus limit of %i reached."
2050 " Processor %d/0x%x ignored.\n", max
, thiscpu
, apicid
);
2057 if (apicid
== boot_cpu_physical_apicid
) {
2059 * x86_bios_cpu_apicid is required to have processors listed
2060 * in same order as logical cpu numbers. Hence the first
2061 * entry is BSP, and so on.
2062 * boot_cpu_init() already hold bit 0 in cpu_present_mask
2067 cpu
= cpumask_next_zero(-1, cpu_present_mask
);
2072 if (version
== 0x0) {
2073 pr_warning("BIOS bug: APIC version is 0 for CPU %d/0x%x, fixing up to 0x10\n",
2077 apic_version
[apicid
] = version
;
2079 if (version
!= apic_version
[boot_cpu_physical_apicid
]) {
2080 pr_warning("BIOS bug: APIC version mismatch, boot CPU: %x, CPU %d: version %x\n",
2081 apic_version
[boot_cpu_physical_apicid
], cpu
, version
);
2084 physid_set(apicid
, phys_cpu_present_map
);
2085 if (apicid
> max_physical_apicid
)
2086 max_physical_apicid
= apicid
;
2088 #if defined(CONFIG_SMP) || defined(CONFIG_X86_64)
2089 early_per_cpu(x86_cpu_to_apicid
, cpu
) = apicid
;
2090 early_per_cpu(x86_bios_cpu_apicid
, cpu
) = apicid
;
2092 #ifdef CONFIG_X86_32
2093 early_per_cpu(x86_cpu_to_logical_apicid
, cpu
) =
2094 apic
->x86_32_early_logical_apicid(cpu
);
2096 set_cpu_possible(cpu
, true);
2097 set_cpu_present(cpu
, true);
2100 int hard_smp_processor_id(void)
2102 return read_apic_id();
2105 void default_init_apic_ldr(void)
2109 apic_write(APIC_DFR
, APIC_DFR_VALUE
);
2110 val
= apic_read(APIC_LDR
) & ~APIC_LDR_MASK
;
2111 val
|= SET_APIC_LOGICAL_ID(1UL << smp_processor_id());
2112 apic_write(APIC_LDR
, val
);
2122 * 'active' is true if the local APIC was enabled by us and
2123 * not the BIOS; this signifies that we are also responsible
2124 * for disabling it before entering apm/acpi suspend
2127 /* r/w apic fields */
2128 unsigned int apic_id
;
2129 unsigned int apic_taskpri
;
2130 unsigned int apic_ldr
;
2131 unsigned int apic_dfr
;
2132 unsigned int apic_spiv
;
2133 unsigned int apic_lvtt
;
2134 unsigned int apic_lvtpc
;
2135 unsigned int apic_lvt0
;
2136 unsigned int apic_lvt1
;
2137 unsigned int apic_lvterr
;
2138 unsigned int apic_tmict
;
2139 unsigned int apic_tdcr
;
2140 unsigned int apic_thmr
;
2143 static int lapic_suspend(void)
2145 unsigned long flags
;
2148 if (!apic_pm_state
.active
)
2151 maxlvt
= lapic_get_maxlvt();
2153 apic_pm_state
.apic_id
= apic_read(APIC_ID
);
2154 apic_pm_state
.apic_taskpri
= apic_read(APIC_TASKPRI
);
2155 apic_pm_state
.apic_ldr
= apic_read(APIC_LDR
);
2156 apic_pm_state
.apic_dfr
= apic_read(APIC_DFR
);
2157 apic_pm_state
.apic_spiv
= apic_read(APIC_SPIV
);
2158 apic_pm_state
.apic_lvtt
= apic_read(APIC_LVTT
);
2160 apic_pm_state
.apic_lvtpc
= apic_read(APIC_LVTPC
);
2161 apic_pm_state
.apic_lvt0
= apic_read(APIC_LVT0
);
2162 apic_pm_state
.apic_lvt1
= apic_read(APIC_LVT1
);
2163 apic_pm_state
.apic_lvterr
= apic_read(APIC_LVTERR
);
2164 apic_pm_state
.apic_tmict
= apic_read(APIC_TMICT
);
2165 apic_pm_state
.apic_tdcr
= apic_read(APIC_TDCR
);
2166 #ifdef CONFIG_X86_THERMAL_VECTOR
2168 apic_pm_state
.apic_thmr
= apic_read(APIC_LVTTHMR
);
2171 local_irq_save(flags
);
2172 disable_local_APIC();
2174 if (intr_remapping_enabled
)
2175 disable_intr_remapping();
2177 local_irq_restore(flags
);
2181 static void lapic_resume(void)
2184 unsigned long flags
;
2187 if (!apic_pm_state
.active
)
2190 local_irq_save(flags
);
2191 if (intr_remapping_enabled
) {
2193 * IO-APIC and PIC have their own resume routines.
2194 * We just mask them here to make sure the interrupt
2195 * subsystem is completely quiet while we enable x2apic
2196 * and interrupt-remapping.
2198 mask_ioapic_entries();
2199 legacy_pic
->mask_all();
2206 * Make sure the APICBASE points to the right address
2208 * FIXME! This will be wrong if we ever support suspend on
2209 * SMP! We'll need to do this as part of the CPU restore!
2211 if (boot_cpu_data
.x86
>= 6) {
2212 rdmsr(MSR_IA32_APICBASE
, l
, h
);
2213 l
&= ~MSR_IA32_APICBASE_BASE
;
2214 l
|= MSR_IA32_APICBASE_ENABLE
| mp_lapic_addr
;
2215 wrmsr(MSR_IA32_APICBASE
, l
, h
);
2219 maxlvt
= lapic_get_maxlvt();
2220 apic_write(APIC_LVTERR
, ERROR_APIC_VECTOR
| APIC_LVT_MASKED
);
2221 apic_write(APIC_ID
, apic_pm_state
.apic_id
);
2222 apic_write(APIC_DFR
, apic_pm_state
.apic_dfr
);
2223 apic_write(APIC_LDR
, apic_pm_state
.apic_ldr
);
2224 apic_write(APIC_TASKPRI
, apic_pm_state
.apic_taskpri
);
2225 apic_write(APIC_SPIV
, apic_pm_state
.apic_spiv
);
2226 apic_write(APIC_LVT0
, apic_pm_state
.apic_lvt0
);
2227 apic_write(APIC_LVT1
, apic_pm_state
.apic_lvt1
);
2228 #if defined(CONFIG_X86_MCE_P4THERMAL) || defined(CONFIG_X86_MCE_INTEL)
2230 apic_write(APIC_LVTTHMR
, apic_pm_state
.apic_thmr
);
2233 apic_write(APIC_LVTPC
, apic_pm_state
.apic_lvtpc
);
2234 apic_write(APIC_LVTT
, apic_pm_state
.apic_lvtt
);
2235 apic_write(APIC_TDCR
, apic_pm_state
.apic_tdcr
);
2236 apic_write(APIC_TMICT
, apic_pm_state
.apic_tmict
);
2237 apic_write(APIC_ESR
, 0);
2238 apic_read(APIC_ESR
);
2239 apic_write(APIC_LVTERR
, apic_pm_state
.apic_lvterr
);
2240 apic_write(APIC_ESR
, 0);
2241 apic_read(APIC_ESR
);
2243 if (intr_remapping_enabled
)
2244 reenable_intr_remapping(x2apic_mode
);
2246 local_irq_restore(flags
);
2250 * This device has no shutdown method - fully functioning local APICs
2251 * are needed on every CPU up until machine_halt/restart/poweroff.
2254 static struct syscore_ops lapic_syscore_ops
= {
2255 .resume
= lapic_resume
,
2256 .suspend
= lapic_suspend
,
2259 static void __cpuinit
apic_pm_activate(void)
2261 apic_pm_state
.active
= 1;
2264 static int __init
init_lapic_sysfs(void)
2266 /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
2268 register_syscore_ops(&lapic_syscore_ops
);
2273 /* local apic needs to resume before other devices access its registers. */
2274 core_initcall(init_lapic_sysfs
);
2276 #else /* CONFIG_PM */
2278 static void apic_pm_activate(void) { }
2280 #endif /* CONFIG_PM */
2282 #ifdef CONFIG_X86_64
2284 static int __cpuinit
apic_cluster_num(void)
2286 int i
, clusters
, zeros
;
2288 u16
*bios_cpu_apicid
;
2289 DECLARE_BITMAP(clustermap
, NUM_APIC_CLUSTERS
);
2291 bios_cpu_apicid
= early_per_cpu_ptr(x86_bios_cpu_apicid
);
2292 bitmap_zero(clustermap
, NUM_APIC_CLUSTERS
);
2294 for (i
= 0; i
< nr_cpu_ids
; i
++) {
2295 /* are we being called early in kernel startup? */
2296 if (bios_cpu_apicid
) {
2297 id
= bios_cpu_apicid
[i
];
2298 } else if (i
< nr_cpu_ids
) {
2300 id
= per_cpu(x86_bios_cpu_apicid
, i
);
2306 if (id
!= BAD_APICID
)
2307 __set_bit(APIC_CLUSTERID(id
), clustermap
);
2310 /* Problem: Partially populated chassis may not have CPUs in some of
2311 * the APIC clusters they have been allocated. Only present CPUs have
2312 * x86_bios_cpu_apicid entries, thus causing zeroes in the bitmap.
2313 * Since clusters are allocated sequentially, count zeros only if
2314 * they are bounded by ones.
2318 for (i
= 0; i
< NUM_APIC_CLUSTERS
; i
++) {
2319 if (test_bit(i
, clustermap
)) {
2320 clusters
+= 1 + zeros
;
2329 static int __cpuinitdata multi_checked
;
2330 static int __cpuinitdata multi
;
2332 static int __cpuinit
set_multi(const struct dmi_system_id
*d
)
2336 pr_info("APIC: %s detected, Multi Chassis\n", d
->ident
);
2341 static const __cpuinitconst
struct dmi_system_id multi_dmi_table
[] = {
2343 .callback
= set_multi
,
2344 .ident
= "IBM System Summit2",
2346 DMI_MATCH(DMI_SYS_VENDOR
, "IBM"),
2347 DMI_MATCH(DMI_PRODUCT_NAME
, "Summit2"),
2353 static void __cpuinit
dmi_check_multi(void)
2358 dmi_check_system(multi_dmi_table
);
2363 * apic_is_clustered_box() -- Check if we can expect good TSC
2365 * Thus far, the major user of this is IBM's Summit2 series:
2366 * Clustered boxes may have unsynced TSC problems if they are
2368 * Use DMI to check them
2370 __cpuinit
int apic_is_clustered_box(void)
2380 * ScaleMP vSMPowered boxes have one cluster per board and TSCs are
2381 * not guaranteed to be synced between boards
2383 if (apic_cluster_num() > 1)
2391 * APIC command line parameters
2393 static int __init
setup_disableapic(char *arg
)
2396 setup_clear_cpu_cap(X86_FEATURE_APIC
);
2399 early_param("disableapic", setup_disableapic
);
2401 /* same as disableapic, for compatibility */
2402 static int __init
setup_nolapic(char *arg
)
2404 return setup_disableapic(arg
);
2406 early_param("nolapic", setup_nolapic
);
2408 static int __init
parse_lapic_timer_c2_ok(char *arg
)
2410 local_apic_timer_c2_ok
= 1;
2413 early_param("lapic_timer_c2_ok", parse_lapic_timer_c2_ok
);
2415 static int __init
parse_disable_apic_timer(char *arg
)
2417 disable_apic_timer
= 1;
2420 early_param("noapictimer", parse_disable_apic_timer
);
2422 static int __init
parse_nolapic_timer(char *arg
)
2424 disable_apic_timer
= 1;
2427 early_param("nolapic_timer", parse_nolapic_timer
);
2429 static int __init
apic_set_verbosity(char *arg
)
2432 #ifdef CONFIG_X86_64
2433 skip_ioapic_setup
= 0;
2439 if (strcmp("debug", arg
) == 0)
2440 apic_verbosity
= APIC_DEBUG
;
2441 else if (strcmp("verbose", arg
) == 0)
2442 apic_verbosity
= APIC_VERBOSE
;
2444 pr_warning("APIC Verbosity level %s not recognised"
2445 " use apic=verbose or apic=debug\n", arg
);
2451 early_param("apic", apic_set_verbosity
);
2453 static int __init
lapic_insert_resource(void)
2458 /* Put local APIC into the resource map. */
2459 lapic_resource
.start
= apic_phys
;
2460 lapic_resource
.end
= lapic_resource
.start
+ PAGE_SIZE
- 1;
2461 insert_resource(&iomem_resource
, &lapic_resource
);
2467 * need call insert after e820_reserve_resources()
2468 * that is using request_resource
2470 late_initcall(lapic_insert_resource
);