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/sysdev.h>
28 #include <linux/delay.h>
29 #include <linux/timex.h>
30 #include <linux/dmar.h>
31 #include <linux/init.h>
32 #include <linux/cpu.h>
33 #include <linux/dmi.h>
34 #include <linux/nmi.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 <asm/atomic.h>
42 #include <asm/mpspec.h>
43 #include <asm/i8253.h>
44 #include <asm/i8259.h>
45 #include <asm/proto.h>
53 #include <asm/kvm_para.h>
56 unsigned int num_processors
;
58 unsigned disabled_cpus __cpuinitdata
;
60 /* Processor that is doing the boot up */
61 unsigned int boot_cpu_physical_apicid
= -1U;
64 * The highest APIC ID seen during enumeration.
66 * On AMD, this determines the messaging protocol we can use: if all APIC IDs
67 * are in the 0 ... 7 range, then we can use logical addressing which
68 * has some performance advantages (better broadcasting).
70 * If there's an APIC ID above 8, we use physical addressing.
72 unsigned int max_physical_apicid
;
75 * Bitmask of physically existing CPUs:
77 physid_mask_t phys_cpu_present_map
;
80 * Map cpu index to physical APIC ID
82 DEFINE_EARLY_PER_CPU(u16
, x86_cpu_to_apicid
, BAD_APICID
);
83 DEFINE_EARLY_PER_CPU(u16
, x86_bios_cpu_apicid
, BAD_APICID
);
84 EXPORT_EARLY_PER_CPU_SYMBOL(x86_cpu_to_apicid
);
85 EXPORT_EARLY_PER_CPU_SYMBOL(x86_bios_cpu_apicid
);
89 * Knob to control our willingness to enable the local APIC.
93 static int force_enable_local_apic
;
95 * APIC command line parameters
97 static int __init
parse_lapic(char *arg
)
99 force_enable_local_apic
= 1;
102 early_param("lapic", parse_lapic
);
103 /* Local APIC was disabled by the BIOS and enabled by the kernel */
104 static int enabled_via_apicbase
;
107 * Handle interrupt mode configuration register (IMCR).
108 * This register controls whether the interrupt signals
109 * that reach the BSP come from the master PIC or from the
110 * local APIC. Before entering Symmetric I/O Mode, either
111 * the BIOS or the operating system must switch out of
112 * PIC Mode by changing the IMCR.
114 static inline void imcr_pic_to_apic(void)
116 /* select IMCR register */
118 /* NMI and 8259 INTR go through APIC */
122 static inline void imcr_apic_to_pic(void)
124 /* select IMCR register */
126 /* NMI and 8259 INTR go directly to BSP */
132 static int apic_calibrate_pmtmr __initdata
;
133 static __init
int setup_apicpmtimer(char *s
)
135 apic_calibrate_pmtmr
= 1;
139 __setup("apicpmtimer", setup_apicpmtimer
);
143 #ifdef CONFIG_X86_X2APIC
144 /* x2apic enabled before OS handover */
145 static int x2apic_preenabled
;
146 static __init
int setup_nox2apic(char *str
)
148 if (x2apic_enabled()) {
149 pr_warning("Bios already enabled x2apic, "
150 "can't enforce nox2apic");
154 setup_clear_cpu_cap(X86_FEATURE_X2APIC
);
157 early_param("nox2apic", setup_nox2apic
);
160 unsigned long mp_lapic_addr
;
162 /* Disable local APIC timer from the kernel commandline or via dmi quirk */
163 static int disable_apic_timer __cpuinitdata
;
164 /* Local APIC timer works in C2 */
165 int local_apic_timer_c2_ok
;
166 EXPORT_SYMBOL_GPL(local_apic_timer_c2_ok
);
168 int first_system_vector
= 0xfe;
171 * Debug level, exported for io_apic.c
173 unsigned int apic_verbosity
;
177 /* Have we found an MP table */
178 int smp_found_config
;
180 static struct resource lapic_resource
= {
181 .name
= "Local APIC",
182 .flags
= IORESOURCE_MEM
| IORESOURCE_BUSY
,
185 static unsigned int calibration_result
;
187 static int lapic_next_event(unsigned long delta
,
188 struct clock_event_device
*evt
);
189 static void lapic_timer_setup(enum clock_event_mode mode
,
190 struct clock_event_device
*evt
);
191 static void lapic_timer_broadcast(const struct cpumask
*mask
);
192 static void apic_pm_activate(void);
195 * The local apic timer can be used for any function which is CPU local.
197 static struct clock_event_device lapic_clockevent
= {
199 .features
= CLOCK_EVT_FEAT_PERIODIC
| CLOCK_EVT_FEAT_ONESHOT
200 | CLOCK_EVT_FEAT_C3STOP
| CLOCK_EVT_FEAT_DUMMY
,
202 .set_mode
= lapic_timer_setup
,
203 .set_next_event
= lapic_next_event
,
204 .broadcast
= lapic_timer_broadcast
,
208 static DEFINE_PER_CPU(struct clock_event_device
, lapic_events
);
210 static unsigned long apic_phys
;
213 * Get the LAPIC version
215 static inline int lapic_get_version(void)
217 return GET_APIC_VERSION(apic_read(APIC_LVR
));
221 * Check, if the APIC is integrated or a separate chip
223 static inline int lapic_is_integrated(void)
228 return APIC_INTEGRATED(lapic_get_version());
233 * Check, whether this is a modern or a first generation APIC
235 static int modern_apic(void)
237 /* AMD systems use old APIC versions, so check the CPU */
238 if (boot_cpu_data
.x86_vendor
== X86_VENDOR_AMD
&&
239 boot_cpu_data
.x86
>= 0xf)
241 return lapic_get_version() >= 0x14;
245 * bare function to substitute write operation
246 * and it's _that_ fast :)
248 static void native_apic_write_dummy(u32 reg
, u32 v
)
250 WARN_ON_ONCE(cpu_has_apic
&& !disable_apic
);
253 static u32
native_apic_read_dummy(u32 reg
)
255 WARN_ON_ONCE((cpu_has_apic
&& !disable_apic
));
260 * right after this call apic->write/read doesn't do anything
261 * note that there is no restore operation it works one way
263 void apic_disable(void)
265 apic
->read
= native_apic_read_dummy
;
266 apic
->write
= native_apic_write_dummy
;
269 void native_apic_wait_icr_idle(void)
271 while (apic_read(APIC_ICR
) & APIC_ICR_BUSY
)
275 u32
native_safe_apic_wait_icr_idle(void)
282 send_status
= apic_read(APIC_ICR
) & APIC_ICR_BUSY
;
286 } while (timeout
++ < 1000);
291 void native_apic_icr_write(u32 low
, u32 id
)
293 apic_write(APIC_ICR2
, SET_APIC_DEST_FIELD(id
));
294 apic_write(APIC_ICR
, low
);
297 u64
native_apic_icr_read(void)
301 icr2
= apic_read(APIC_ICR2
);
302 icr1
= apic_read(APIC_ICR
);
304 return icr1
| ((u64
)icr2
<< 32);
308 * enable_NMI_through_LVT0 - enable NMI through local vector table 0
310 void __cpuinit
enable_NMI_through_LVT0(void)
314 /* unmask and set to NMI */
317 /* Level triggered for 82489DX (32bit mode) */
318 if (!lapic_is_integrated())
319 v
|= APIC_LVT_LEVEL_TRIGGER
;
321 apic_write(APIC_LVT0
, v
);
326 * get_physical_broadcast - Get number of physical broadcast IDs
328 int get_physical_broadcast(void)
330 return modern_apic() ? 0xff : 0xf;
335 * lapic_get_maxlvt - get the maximum number of local vector table entries
337 int lapic_get_maxlvt(void)
341 v
= apic_read(APIC_LVR
);
343 * - we always have APIC integrated on 64bit mode
344 * - 82489DXs do not report # of LVT entries
346 return APIC_INTEGRATED(GET_APIC_VERSION(v
)) ? GET_APIC_MAXLVT(v
) : 2;
354 #define APIC_DIVISOR 16
357 * This function sets up the local APIC timer, with a timeout of
358 * 'clocks' APIC bus clock. During calibration we actually call
359 * this function twice on the boot CPU, once with a bogus timeout
360 * value, second time for real. The other (noncalibrating) CPUs
361 * call this function only once, with the real, calibrated value.
363 * We do reads before writes even if unnecessary, to get around the
364 * P5 APIC double write bug.
366 static void __setup_APIC_LVTT(unsigned int clocks
, int oneshot
, int irqen
)
368 unsigned int lvtt_value
, tmp_value
;
370 lvtt_value
= LOCAL_TIMER_VECTOR
;
372 lvtt_value
|= APIC_LVT_TIMER_PERIODIC
;
373 if (!lapic_is_integrated())
374 lvtt_value
|= SET_APIC_TIMER_BASE(APIC_TIMER_BASE_DIV
);
377 lvtt_value
|= APIC_LVT_MASKED
;
379 apic_write(APIC_LVTT
, lvtt_value
);
384 tmp_value
= apic_read(APIC_TDCR
);
385 apic_write(APIC_TDCR
,
386 (tmp_value
& ~(APIC_TDR_DIV_1
| APIC_TDR_DIV_TMBASE
)) |
390 apic_write(APIC_TMICT
, clocks
/ APIC_DIVISOR
);
394 * Setup extended LVT, AMD specific (K8, family 10h)
396 * Vector mappings are hard coded. On K8 only offset 0 (APIC500) and
397 * MCE interrupts are supported. Thus MCE offset must be set to 0.
399 * If mask=1, the LVT entry does not generate interrupts while mask=0
400 * enables the vector. See also the BKDGs.
403 #define APIC_EILVT_LVTOFF_MCE 0
404 #define APIC_EILVT_LVTOFF_IBS 1
406 static void setup_APIC_eilvt(u8 lvt_off
, u8 vector
, u8 msg_type
, u8 mask
)
408 unsigned long reg
= (lvt_off
<< 4) + APIC_EILVTn(0);
409 unsigned int v
= (mask
<< 16) | (msg_type
<< 8) | vector
;
414 u8
setup_APIC_eilvt_mce(u8 vector
, u8 msg_type
, u8 mask
)
416 setup_APIC_eilvt(APIC_EILVT_LVTOFF_MCE
, vector
, msg_type
, mask
);
417 return APIC_EILVT_LVTOFF_MCE
;
420 u8
setup_APIC_eilvt_ibs(u8 vector
, u8 msg_type
, u8 mask
)
422 setup_APIC_eilvt(APIC_EILVT_LVTOFF_IBS
, vector
, msg_type
, mask
);
423 return APIC_EILVT_LVTOFF_IBS
;
425 EXPORT_SYMBOL_GPL(setup_APIC_eilvt_ibs
);
428 * Program the next event, relative to now
430 static int lapic_next_event(unsigned long delta
,
431 struct clock_event_device
*evt
)
433 apic_write(APIC_TMICT
, delta
);
438 * Setup the lapic timer in periodic or oneshot mode
440 static void lapic_timer_setup(enum clock_event_mode mode
,
441 struct clock_event_device
*evt
)
446 /* Lapic used as dummy for broadcast ? */
447 if (evt
->features
& CLOCK_EVT_FEAT_DUMMY
)
450 local_irq_save(flags
);
453 case CLOCK_EVT_MODE_PERIODIC
:
454 case CLOCK_EVT_MODE_ONESHOT
:
455 __setup_APIC_LVTT(calibration_result
,
456 mode
!= CLOCK_EVT_MODE_PERIODIC
, 1);
458 case CLOCK_EVT_MODE_UNUSED
:
459 case CLOCK_EVT_MODE_SHUTDOWN
:
460 v
= apic_read(APIC_LVTT
);
461 v
|= (APIC_LVT_MASKED
| LOCAL_TIMER_VECTOR
);
462 apic_write(APIC_LVTT
, v
);
463 apic_write(APIC_TMICT
, 0xffffffff);
465 case CLOCK_EVT_MODE_RESUME
:
466 /* Nothing to do here */
470 local_irq_restore(flags
);
474 * Local APIC timer broadcast function
476 static void lapic_timer_broadcast(const struct cpumask
*mask
)
479 apic
->send_IPI_mask(mask
, LOCAL_TIMER_VECTOR
);
484 * Setup the local APIC timer for this CPU. Copy the initilized values
485 * of the boot CPU and register the clock event in the framework.
487 static void __cpuinit
setup_APIC_timer(void)
489 struct clock_event_device
*levt
= &__get_cpu_var(lapic_events
);
491 if (cpu_has(¤t_cpu_data
, X86_FEATURE_ARAT
)) {
492 lapic_clockevent
.features
&= ~CLOCK_EVT_FEAT_C3STOP
;
493 /* Make LAPIC timer preferrable over percpu HPET */
494 lapic_clockevent
.rating
= 150;
497 memcpy(levt
, &lapic_clockevent
, sizeof(*levt
));
498 levt
->cpumask
= cpumask_of(smp_processor_id());
500 clockevents_register_device(levt
);
504 * In this functions we calibrate APIC bus clocks to the external timer.
506 * We want to do the calibration only once since we want to have local timer
507 * irqs syncron. CPUs connected by the same APIC bus have the very same bus
510 * This was previously done by reading the PIT/HPET and waiting for a wrap
511 * around to find out, that a tick has elapsed. I have a box, where the PIT
512 * readout is broken, so it never gets out of the wait loop again. This was
513 * also reported by others.
515 * Monitoring the jiffies value is inaccurate and the clockevents
516 * infrastructure allows us to do a simple substitution of the interrupt
519 * The calibration routine also uses the pm_timer when possible, as the PIT
520 * happens to run way too slow (factor 2.3 on my VAIO CoreDuo, which goes
521 * back to normal later in the boot process).
524 #define LAPIC_CAL_LOOPS (HZ/10)
526 static __initdata
int lapic_cal_loops
= -1;
527 static __initdata
long lapic_cal_t1
, lapic_cal_t2
;
528 static __initdata
unsigned long long lapic_cal_tsc1
, lapic_cal_tsc2
;
529 static __initdata
unsigned long lapic_cal_pm1
, lapic_cal_pm2
;
530 static __initdata
unsigned long lapic_cal_j1
, lapic_cal_j2
;
533 * Temporary interrupt handler.
535 static void __init
lapic_cal_handler(struct clock_event_device
*dev
)
537 unsigned long long tsc
= 0;
538 long tapic
= apic_read(APIC_TMCCT
);
539 unsigned long pm
= acpi_pm_read_early();
544 switch (lapic_cal_loops
++) {
546 lapic_cal_t1
= tapic
;
547 lapic_cal_tsc1
= tsc
;
549 lapic_cal_j1
= jiffies
;
552 case LAPIC_CAL_LOOPS
:
553 lapic_cal_t2
= tapic
;
554 lapic_cal_tsc2
= tsc
;
555 if (pm
< lapic_cal_pm1
)
556 pm
+= ACPI_PM_OVRRUN
;
558 lapic_cal_j2
= jiffies
;
564 calibrate_by_pmtimer(long deltapm
, long *delta
, long *deltatsc
)
566 const long pm_100ms
= PMTMR_TICKS_PER_SEC
/ 10;
567 const long pm_thresh
= pm_100ms
/ 100;
571 #ifndef CONFIG_X86_PM_TIMER
575 apic_printk(APIC_VERBOSE
, "... PM-Timer delta = %ld\n", deltapm
);
577 /* Check, if the PM timer is available */
581 mult
= clocksource_hz2mult(PMTMR_TICKS_PER_SEC
, 22);
583 if (deltapm
> (pm_100ms
- pm_thresh
) &&
584 deltapm
< (pm_100ms
+ pm_thresh
)) {
585 apic_printk(APIC_VERBOSE
, "... PM-Timer result ok\n");
589 res
= (((u64
)deltapm
) * mult
) >> 22;
590 do_div(res
, 1000000);
591 pr_warning("APIC calibration not consistent "
592 "with PM-Timer: %ldms instead of 100ms\n",(long)res
);
594 /* Correct the lapic counter value */
595 res
= (((u64
)(*delta
)) * pm_100ms
);
596 do_div(res
, deltapm
);
597 pr_info("APIC delta adjusted to PM-Timer: "
598 "%lu (%ld)\n", (unsigned long)res
, *delta
);
601 /* Correct the tsc counter value */
603 res
= (((u64
)(*deltatsc
)) * pm_100ms
);
604 do_div(res
, deltapm
);
605 apic_printk(APIC_VERBOSE
, "TSC delta adjusted to "
606 "PM-Timer: %lu (%ld) \n",
607 (unsigned long)res
, *deltatsc
);
608 *deltatsc
= (long)res
;
614 static int __init
calibrate_APIC_clock(void)
616 struct clock_event_device
*levt
= &__get_cpu_var(lapic_events
);
617 void (*real_handler
)(struct clock_event_device
*dev
);
618 unsigned long deltaj
;
619 long delta
, deltatsc
;
620 int pm_referenced
= 0;
624 /* Replace the global interrupt handler */
625 real_handler
= global_clock_event
->event_handler
;
626 global_clock_event
->event_handler
= lapic_cal_handler
;
629 * Setup the APIC counter to maximum. There is no way the lapic
630 * can underflow in the 100ms detection time frame
632 __setup_APIC_LVTT(0xffffffff, 0, 0);
634 /* Let the interrupts run */
637 while (lapic_cal_loops
<= LAPIC_CAL_LOOPS
)
642 /* Restore the real event handler */
643 global_clock_event
->event_handler
= real_handler
;
645 /* Build delta t1-t2 as apic timer counts down */
646 delta
= lapic_cal_t1
- lapic_cal_t2
;
647 apic_printk(APIC_VERBOSE
, "... lapic delta = %ld\n", delta
);
649 deltatsc
= (long)(lapic_cal_tsc2
- lapic_cal_tsc1
);
651 /* we trust the PM based calibration if possible */
652 pm_referenced
= !calibrate_by_pmtimer(lapic_cal_pm2
- lapic_cal_pm1
,
655 /* Calculate the scaled math multiplication factor */
656 lapic_clockevent
.mult
= div_sc(delta
, TICK_NSEC
* LAPIC_CAL_LOOPS
,
657 lapic_clockevent
.shift
);
658 lapic_clockevent
.max_delta_ns
=
659 clockevent_delta2ns(0x7FFFFF, &lapic_clockevent
);
660 lapic_clockevent
.min_delta_ns
=
661 clockevent_delta2ns(0xF, &lapic_clockevent
);
663 calibration_result
= (delta
* APIC_DIVISOR
) / LAPIC_CAL_LOOPS
;
665 apic_printk(APIC_VERBOSE
, "..... delta %ld\n", delta
);
666 apic_printk(APIC_VERBOSE
, "..... mult: %ld\n", lapic_clockevent
.mult
);
667 apic_printk(APIC_VERBOSE
, "..... calibration result: %u\n",
671 apic_printk(APIC_VERBOSE
, "..... CPU clock speed is "
673 (deltatsc
/ LAPIC_CAL_LOOPS
) / (1000000 / HZ
),
674 (deltatsc
/ LAPIC_CAL_LOOPS
) % (1000000 / HZ
));
677 apic_printk(APIC_VERBOSE
, "..... host bus clock speed is "
679 calibration_result
/ (1000000 / HZ
),
680 calibration_result
% (1000000 / HZ
));
683 * Do a sanity check on the APIC calibration result
685 if (calibration_result
< (1000000 / HZ
)) {
687 pr_warning("APIC frequency too slow, disabling apic timer\n");
691 levt
->features
&= ~CLOCK_EVT_FEAT_DUMMY
;
694 * PM timer calibration failed or not turned on
695 * so lets try APIC timer based calibration
697 if (!pm_referenced
) {
698 apic_printk(APIC_VERBOSE
, "... verify APIC timer\n");
701 * Setup the apic timer manually
703 levt
->event_handler
= lapic_cal_handler
;
704 lapic_timer_setup(CLOCK_EVT_MODE_PERIODIC
, levt
);
705 lapic_cal_loops
= -1;
707 /* Let the interrupts run */
710 while (lapic_cal_loops
<= LAPIC_CAL_LOOPS
)
713 /* Stop the lapic timer */
714 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN
, levt
);
717 deltaj
= lapic_cal_j2
- lapic_cal_j1
;
718 apic_printk(APIC_VERBOSE
, "... jiffies delta = %lu\n", deltaj
);
720 /* Check, if the jiffies result is consistent */
721 if (deltaj
>= LAPIC_CAL_LOOPS
-2 && deltaj
<= LAPIC_CAL_LOOPS
+2)
722 apic_printk(APIC_VERBOSE
, "... jiffies result ok\n");
724 levt
->features
|= CLOCK_EVT_FEAT_DUMMY
;
728 if (levt
->features
& CLOCK_EVT_FEAT_DUMMY
) {
729 pr_warning("APIC timer disabled due to verification failure\n");
737 * Setup the boot APIC
739 * Calibrate and verify the result.
741 void __init
setup_boot_APIC_clock(void)
744 * The local apic timer can be disabled via the kernel
745 * commandline or from the CPU detection code. Register the lapic
746 * timer as a dummy clock event source on SMP systems, so the
747 * broadcast mechanism is used. On UP systems simply ignore it.
749 if (disable_apic_timer
) {
750 pr_info("Disabling APIC timer\n");
751 /* No broadcast on UP ! */
752 if (num_possible_cpus() > 1) {
753 lapic_clockevent
.mult
= 1;
759 apic_printk(APIC_VERBOSE
, "Using local APIC timer interrupts.\n"
760 "calibrating APIC timer ...\n");
762 if (calibrate_APIC_clock()) {
763 /* No broadcast on UP ! */
764 if (num_possible_cpus() > 1)
770 * If nmi_watchdog is set to IO_APIC, we need the
771 * PIT/HPET going. Otherwise register lapic as a dummy
774 if (nmi_watchdog
!= NMI_IO_APIC
)
775 lapic_clockevent
.features
&= ~CLOCK_EVT_FEAT_DUMMY
;
777 pr_warning("APIC timer registered as dummy,"
778 " due to nmi_watchdog=%d!\n", nmi_watchdog
);
780 /* Setup the lapic or request the broadcast */
784 void __cpuinit
setup_secondary_APIC_clock(void)
790 * The guts of the apic timer interrupt
792 static void local_apic_timer_interrupt(void)
794 int cpu
= smp_processor_id();
795 struct clock_event_device
*evt
= &per_cpu(lapic_events
, cpu
);
798 * Normally we should not be here till LAPIC has been initialized but
799 * in some cases like kdump, its possible that there is a pending LAPIC
800 * timer interrupt from previous kernel's context and is delivered in
801 * new kernel the moment interrupts are enabled.
803 * Interrupts are enabled early and LAPIC is setup much later, hence
804 * its possible that when we get here evt->event_handler is NULL.
805 * Check for event_handler being NULL and discard the interrupt as
808 if (!evt
->event_handler
) {
809 pr_warning("Spurious LAPIC timer interrupt on cpu %d\n", cpu
);
811 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN
, evt
);
816 * the NMI deadlock-detector uses this.
818 inc_irq_stat(apic_timer_irqs
);
820 evt
->event_handler(evt
);
824 * Local APIC timer interrupt. This is the most natural way for doing
825 * local interrupts, but local timer interrupts can be emulated by
826 * broadcast interrupts too. [in case the hw doesn't support APIC timers]
828 * [ if a single-CPU system runs an SMP kernel then we call the local
829 * interrupt as well. Thus we cannot inline the local irq ... ]
831 void __irq_entry
smp_apic_timer_interrupt(struct pt_regs
*regs
)
833 struct pt_regs
*old_regs
= set_irq_regs(regs
);
836 * NOTE! We'd better ACK the irq immediately,
837 * because timer handling can be slow.
841 * update_process_times() expects us to have done irq_enter().
842 * Besides, if we don't timer interrupts ignore the global
843 * interrupt lock, which is the WrongThing (tm) to do.
847 local_apic_timer_interrupt();
850 set_irq_regs(old_regs
);
853 int setup_profiling_timer(unsigned int multiplier
)
859 * Local APIC start and shutdown
863 * clear_local_APIC - shutdown the local APIC
865 * This is called, when a CPU is disabled and before rebooting, so the state of
866 * the local APIC has no dangling leftovers. Also used to cleanout any BIOS
867 * leftovers during boot.
869 void clear_local_APIC(void)
874 /* APIC hasn't been mapped yet */
875 if (!x2apic_mode
&& !apic_phys
)
878 maxlvt
= lapic_get_maxlvt();
880 * Masking an LVT entry can trigger a local APIC error
881 * if the vector is zero. Mask LVTERR first to prevent this.
884 v
= ERROR_APIC_VECTOR
; /* any non-zero vector will do */
885 apic_write(APIC_LVTERR
, v
| APIC_LVT_MASKED
);
888 * Careful: we have to set masks only first to deassert
889 * any level-triggered sources.
891 v
= apic_read(APIC_LVTT
);
892 apic_write(APIC_LVTT
, v
| APIC_LVT_MASKED
);
893 v
= apic_read(APIC_LVT0
);
894 apic_write(APIC_LVT0
, v
| APIC_LVT_MASKED
);
895 v
= apic_read(APIC_LVT1
);
896 apic_write(APIC_LVT1
, v
| APIC_LVT_MASKED
);
898 v
= apic_read(APIC_LVTPC
);
899 apic_write(APIC_LVTPC
, v
| APIC_LVT_MASKED
);
902 /* lets not touch this if we didn't frob it */
903 #ifdef CONFIG_X86_THERMAL_VECTOR
905 v
= apic_read(APIC_LVTTHMR
);
906 apic_write(APIC_LVTTHMR
, v
| APIC_LVT_MASKED
);
909 #ifdef CONFIG_X86_MCE_INTEL
911 v
= apic_read(APIC_LVTCMCI
);
912 if (!(v
& APIC_LVT_MASKED
))
913 apic_write(APIC_LVTCMCI
, v
| APIC_LVT_MASKED
);
918 * Clean APIC state for other OSs:
920 apic_write(APIC_LVTT
, APIC_LVT_MASKED
);
921 apic_write(APIC_LVT0
, APIC_LVT_MASKED
);
922 apic_write(APIC_LVT1
, APIC_LVT_MASKED
);
924 apic_write(APIC_LVTERR
, APIC_LVT_MASKED
);
926 apic_write(APIC_LVTPC
, APIC_LVT_MASKED
);
928 /* Integrated APIC (!82489DX) ? */
929 if (lapic_is_integrated()) {
931 /* Clear ESR due to Pentium errata 3AP and 11AP */
932 apic_write(APIC_ESR
, 0);
938 * disable_local_APIC - clear and disable the local APIC
940 void disable_local_APIC(void)
944 /* APIC hasn't been mapped yet */
945 if (!x2apic_mode
&& !apic_phys
)
951 * Disable APIC (implies clearing of registers
954 value
= apic_read(APIC_SPIV
);
955 value
&= ~APIC_SPIV_APIC_ENABLED
;
956 apic_write(APIC_SPIV
, value
);
960 * When LAPIC was disabled by the BIOS and enabled by the kernel,
961 * restore the disabled state.
963 if (enabled_via_apicbase
) {
966 rdmsr(MSR_IA32_APICBASE
, l
, h
);
967 l
&= ~MSR_IA32_APICBASE_ENABLE
;
968 wrmsr(MSR_IA32_APICBASE
, l
, h
);
974 * If Linux enabled the LAPIC against the BIOS default disable it down before
975 * re-entering the BIOS on shutdown. Otherwise the BIOS may get confused and
976 * not power-off. Additionally clear all LVT entries before disable_local_APIC
977 * for the case where Linux didn't enable the LAPIC.
979 void lapic_shutdown(void)
983 if (!cpu_has_apic
&& !apic_from_smp_config())
986 local_irq_save(flags
);
989 if (!enabled_via_apicbase
)
993 disable_local_APIC();
996 local_irq_restore(flags
);
1000 * This is to verify that we're looking at a real local APIC.
1001 * Check these against your board if the CPUs aren't getting
1002 * started for no apparent reason.
1004 int __init
verify_local_APIC(void)
1006 unsigned int reg0
, reg1
;
1009 * The version register is read-only in a real APIC.
1011 reg0
= apic_read(APIC_LVR
);
1012 apic_printk(APIC_DEBUG
, "Getting VERSION: %x\n", reg0
);
1013 apic_write(APIC_LVR
, reg0
^ APIC_LVR_MASK
);
1014 reg1
= apic_read(APIC_LVR
);
1015 apic_printk(APIC_DEBUG
, "Getting VERSION: %x\n", reg1
);
1018 * The two version reads above should print the same
1019 * numbers. If the second one is different, then we
1020 * poke at a non-APIC.
1026 * Check if the version looks reasonably.
1028 reg1
= GET_APIC_VERSION(reg0
);
1029 if (reg1
== 0x00 || reg1
== 0xff)
1031 reg1
= lapic_get_maxlvt();
1032 if (reg1
< 0x02 || reg1
== 0xff)
1036 * The ID register is read/write in a real APIC.
1038 reg0
= apic_read(APIC_ID
);
1039 apic_printk(APIC_DEBUG
, "Getting ID: %x\n", reg0
);
1040 apic_write(APIC_ID
, reg0
^ apic
->apic_id_mask
);
1041 reg1
= apic_read(APIC_ID
);
1042 apic_printk(APIC_DEBUG
, "Getting ID: %x\n", reg1
);
1043 apic_write(APIC_ID
, reg0
);
1044 if (reg1
!= (reg0
^ apic
->apic_id_mask
))
1048 * The next two are just to see if we have sane values.
1049 * They're only really relevant if we're in Virtual Wire
1050 * compatibility mode, but most boxes are anymore.
1052 reg0
= apic_read(APIC_LVT0
);
1053 apic_printk(APIC_DEBUG
, "Getting LVT0: %x\n", reg0
);
1054 reg1
= apic_read(APIC_LVT1
);
1055 apic_printk(APIC_DEBUG
, "Getting LVT1: %x\n", reg1
);
1061 * sync_Arb_IDs - synchronize APIC bus arbitration IDs
1063 void __init
sync_Arb_IDs(void)
1066 * Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 And not
1069 if (modern_apic() || boot_cpu_data
.x86_vendor
== X86_VENDOR_AMD
)
1075 apic_wait_icr_idle();
1077 apic_printk(APIC_DEBUG
, "Synchronizing Arb IDs.\n");
1078 apic_write(APIC_ICR
, APIC_DEST_ALLINC
|
1079 APIC_INT_LEVELTRIG
| APIC_DM_INIT
);
1083 * An initial setup of the virtual wire mode.
1085 void __init
init_bsp_APIC(void)
1090 * Don't do the setup now if we have a SMP BIOS as the
1091 * through-I/O-APIC virtual wire mode might be active.
1093 if (smp_found_config
|| !cpu_has_apic
)
1097 * Do not trust the local APIC being empty at bootup.
1104 value
= apic_read(APIC_SPIV
);
1105 value
&= ~APIC_VECTOR_MASK
;
1106 value
|= APIC_SPIV_APIC_ENABLED
;
1108 #ifdef CONFIG_X86_32
1109 /* This bit is reserved on P4/Xeon and should be cleared */
1110 if ((boot_cpu_data
.x86_vendor
== X86_VENDOR_INTEL
) &&
1111 (boot_cpu_data
.x86
== 15))
1112 value
&= ~APIC_SPIV_FOCUS_DISABLED
;
1115 value
|= APIC_SPIV_FOCUS_DISABLED
;
1116 value
|= SPURIOUS_APIC_VECTOR
;
1117 apic_write(APIC_SPIV
, value
);
1120 * Set up the virtual wire mode.
1122 apic_write(APIC_LVT0
, APIC_DM_EXTINT
);
1123 value
= APIC_DM_NMI
;
1124 if (!lapic_is_integrated()) /* 82489DX */
1125 value
|= APIC_LVT_LEVEL_TRIGGER
;
1126 apic_write(APIC_LVT1
, value
);
1129 static void __cpuinit
lapic_setup_esr(void)
1131 unsigned int oldvalue
, value
, maxlvt
;
1133 if (!lapic_is_integrated()) {
1134 pr_info("No ESR for 82489DX.\n");
1138 if (apic
->disable_esr
) {
1140 * Something untraceable is creating bad interrupts on
1141 * secondary quads ... for the moment, just leave the
1142 * ESR disabled - we can't do anything useful with the
1143 * errors anyway - mbligh
1145 pr_info("Leaving ESR disabled.\n");
1149 maxlvt
= lapic_get_maxlvt();
1150 if (maxlvt
> 3) /* Due to the Pentium erratum 3AP. */
1151 apic_write(APIC_ESR
, 0);
1152 oldvalue
= apic_read(APIC_ESR
);
1154 /* enables sending errors */
1155 value
= ERROR_APIC_VECTOR
;
1156 apic_write(APIC_LVTERR
, value
);
1159 * spec says clear errors after enabling vector.
1162 apic_write(APIC_ESR
, 0);
1163 value
= apic_read(APIC_ESR
);
1164 if (value
!= oldvalue
)
1165 apic_printk(APIC_VERBOSE
, "ESR value before enabling "
1166 "vector: 0x%08x after: 0x%08x\n",
1172 * setup_local_APIC - setup the local APIC
1174 void __cpuinit
setup_local_APIC(void)
1176 unsigned int value
, queued
;
1177 int i
, j
, acked
= 0;
1178 unsigned long long tsc
= 0, ntsc
;
1179 long long max_loops
= cpu_khz
;
1185 arch_disable_smp_support();
1189 #ifdef CONFIG_X86_32
1190 /* Pound the ESR really hard over the head with a big hammer - mbligh */
1191 if (lapic_is_integrated() && apic
->disable_esr
) {
1192 apic_write(APIC_ESR
, 0);
1193 apic_write(APIC_ESR
, 0);
1194 apic_write(APIC_ESR
, 0);
1195 apic_write(APIC_ESR
, 0);
1198 perf_events_lapic_init();
1203 * Double-check whether this APIC is really registered.
1204 * This is meaningless in clustered apic mode, so we skip it.
1206 BUG_ON(!apic
->apic_id_registered());
1209 * Intel recommends to set DFR, LDR and TPR before enabling
1210 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
1211 * document number 292116). So here it goes...
1213 apic
->init_apic_ldr();
1216 * Set Task Priority to 'accept all'. We never change this
1219 value
= apic_read(APIC_TASKPRI
);
1220 value
&= ~APIC_TPRI_MASK
;
1221 apic_write(APIC_TASKPRI
, value
);
1224 * After a crash, we no longer service the interrupts and a pending
1225 * interrupt from previous kernel might still have ISR bit set.
1227 * Most probably by now CPU has serviced that pending interrupt and
1228 * it might not have done the ack_APIC_irq() because it thought,
1229 * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
1230 * does not clear the ISR bit and cpu thinks it has already serivced
1231 * the interrupt. Hence a vector might get locked. It was noticed
1232 * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
1236 for (i
= APIC_ISR_NR
- 1; i
>= 0; i
--)
1237 queued
|= apic_read(APIC_IRR
+ i
*0x10);
1239 for (i
= APIC_ISR_NR
- 1; i
>= 0; i
--) {
1240 value
= apic_read(APIC_ISR
+ i
*0x10);
1241 for (j
= 31; j
>= 0; j
--) {
1242 if (value
& (1<<j
)) {
1249 printk(KERN_ERR
"LAPIC pending interrupts after %d EOI\n",
1255 max_loops
= (cpu_khz
<< 10) - (ntsc
- tsc
);
1258 } while (queued
&& max_loops
> 0);
1259 WARN_ON(max_loops
<= 0);
1262 * Now that we are all set up, enable the APIC
1264 value
= apic_read(APIC_SPIV
);
1265 value
&= ~APIC_VECTOR_MASK
;
1269 value
|= APIC_SPIV_APIC_ENABLED
;
1271 #ifdef CONFIG_X86_32
1273 * Some unknown Intel IO/APIC (or APIC) errata is biting us with
1274 * certain networking cards. If high frequency interrupts are
1275 * happening on a particular IOAPIC pin, plus the IOAPIC routing
1276 * entry is masked/unmasked at a high rate as well then sooner or
1277 * later IOAPIC line gets 'stuck', no more interrupts are received
1278 * from the device. If focus CPU is disabled then the hang goes
1281 * [ This bug can be reproduced easily with a level-triggered
1282 * PCI Ne2000 networking cards and PII/PIII processors, dual
1286 * Actually disabling the focus CPU check just makes the hang less
1287 * frequent as it makes the interrupt distributon model be more
1288 * like LRU than MRU (the short-term load is more even across CPUs).
1289 * See also the comment in end_level_ioapic_irq(). --macro
1293 * - enable focus processor (bit==0)
1294 * - 64bit mode always use processor focus
1295 * so no need to set it
1297 value
&= ~APIC_SPIV_FOCUS_DISABLED
;
1301 * Set spurious IRQ vector
1303 value
|= SPURIOUS_APIC_VECTOR
;
1304 apic_write(APIC_SPIV
, value
);
1307 * Set up LVT0, LVT1:
1309 * set up through-local-APIC on the BP's LINT0. This is not
1310 * strictly necessary in pure symmetric-IO mode, but sometimes
1311 * we delegate interrupts to the 8259A.
1314 * TODO: set up through-local-APIC from through-I/O-APIC? --macro
1316 value
= apic_read(APIC_LVT0
) & APIC_LVT_MASKED
;
1317 if (!smp_processor_id() && (pic_mode
|| !value
)) {
1318 value
= APIC_DM_EXTINT
;
1319 apic_printk(APIC_VERBOSE
, "enabled ExtINT on CPU#%d\n",
1320 smp_processor_id());
1322 value
= APIC_DM_EXTINT
| APIC_LVT_MASKED
;
1323 apic_printk(APIC_VERBOSE
, "masked ExtINT on CPU#%d\n",
1324 smp_processor_id());
1326 apic_write(APIC_LVT0
, value
);
1329 * only the BP should see the LINT1 NMI signal, obviously.
1331 if (!smp_processor_id())
1332 value
= APIC_DM_NMI
;
1334 value
= APIC_DM_NMI
| APIC_LVT_MASKED
;
1335 if (!lapic_is_integrated()) /* 82489DX */
1336 value
|= APIC_LVT_LEVEL_TRIGGER
;
1337 apic_write(APIC_LVT1
, value
);
1341 #ifdef CONFIG_X86_MCE_INTEL
1342 /* Recheck CMCI information after local APIC is up on CPU #0 */
1343 if (smp_processor_id() == 0)
1348 void __cpuinit
end_local_APIC_setup(void)
1352 #ifdef CONFIG_X86_32
1355 /* Disable the local apic timer */
1356 value
= apic_read(APIC_LVTT
);
1357 value
|= (APIC_LVT_MASKED
| LOCAL_TIMER_VECTOR
);
1358 apic_write(APIC_LVTT
, value
);
1362 setup_apic_nmi_watchdog(NULL
);
1366 * Now that local APIC setup is completed for BP, configure the fault
1367 * handling for interrupt remapping.
1369 if (!smp_processor_id() && intr_remapping_enabled
)
1370 enable_drhd_fault_handling();
1374 #ifdef CONFIG_X86_X2APIC
1375 void check_x2apic(void)
1377 if (x2apic_enabled()) {
1378 pr_info("x2apic enabled by BIOS, switching to x2apic ops\n");
1379 x2apic_preenabled
= x2apic_mode
= 1;
1383 void enable_x2apic(void)
1390 rdmsr(MSR_IA32_APICBASE
, msr
, msr2
);
1391 if (!(msr
& X2APIC_ENABLE
)) {
1392 pr_info("Enabling x2apic\n");
1393 wrmsr(MSR_IA32_APICBASE
, msr
| X2APIC_ENABLE
, 0);
1396 #endif /* CONFIG_X86_X2APIC */
1398 int __init
enable_IR(void)
1400 #ifdef CONFIG_INTR_REMAP
1401 if (!intr_remapping_supported()) {
1402 pr_debug("intr-remapping not supported\n");
1406 if (!x2apic_preenabled
&& skip_ioapic_setup
) {
1407 pr_info("Skipped enabling intr-remap because of skipping "
1412 if (enable_intr_remapping(x2apic_supported()))
1415 pr_info("Enabled Interrupt-remapping\n");
1423 void __init
enable_IR_x2apic(void)
1425 unsigned long flags
;
1426 struct IO_APIC_route_entry
**ioapic_entries
= NULL
;
1427 int ret
, x2apic_enabled
= 0;
1428 int dmar_table_init_ret
= 0;
1430 #ifdef CONFIG_INTR_REMAP
1431 dmar_table_init_ret
= dmar_table_init();
1432 if (dmar_table_init_ret
)
1433 pr_debug("dmar_table_init() failed with %d:\n",
1434 dmar_table_init_ret
);
1437 ioapic_entries
= alloc_ioapic_entries();
1438 if (!ioapic_entries
) {
1439 pr_err("Allocate ioapic_entries failed\n");
1443 ret
= save_IO_APIC_setup(ioapic_entries
);
1445 pr_info("Saving IO-APIC state failed: %d\n", ret
);
1449 local_irq_save(flags
);
1451 mask_IO_APIC_setup(ioapic_entries
);
1453 if (dmar_table_init_ret
)
1459 /* IR is required if there is APIC ID > 255 even when running
1462 if (max_physical_apicid
> 255 || !kvm_para_available())
1465 * without IR all CPUs can be addressed by IOAPIC/MSI
1466 * only in physical mode
1468 x2apic_force_phys();
1473 if (x2apic_supported() && !x2apic_mode
) {
1476 pr_info("Enabled x2apic\n");
1480 if (!ret
) /* IR enabling failed */
1481 restore_IO_APIC_setup(ioapic_entries
);
1483 local_irq_restore(flags
);
1487 free_ioapic_entries(ioapic_entries
);
1492 if (x2apic_preenabled
)
1493 panic("x2apic: enabled by BIOS but kernel init failed.");
1494 else if (cpu_has_x2apic
)
1495 pr_info("Not enabling x2apic, Intr-remapping init failed.\n");
1498 #ifdef CONFIG_X86_64
1500 * Detect and enable local APICs on non-SMP boards.
1501 * Original code written by Keir Fraser.
1502 * On AMD64 we trust the BIOS - if it says no APIC it is likely
1503 * not correctly set up (usually the APIC timer won't work etc.)
1505 static int __init
detect_init_APIC(void)
1507 if (!cpu_has_apic
) {
1508 pr_info("No local APIC present\n");
1512 mp_lapic_addr
= APIC_DEFAULT_PHYS_BASE
;
1517 * Detect and initialize APIC
1519 static int __init
detect_init_APIC(void)
1523 /* Disabled by kernel option? */
1527 switch (boot_cpu_data
.x86_vendor
) {
1528 case X86_VENDOR_AMD
:
1529 if ((boot_cpu_data
.x86
== 6 && boot_cpu_data
.x86_model
> 1) ||
1530 (boot_cpu_data
.x86
>= 15))
1533 case X86_VENDOR_INTEL
:
1534 if (boot_cpu_data
.x86
== 6 || boot_cpu_data
.x86
== 15 ||
1535 (boot_cpu_data
.x86
== 5 && cpu_has_apic
))
1542 if (!cpu_has_apic
) {
1544 * Over-ride BIOS and try to enable the local APIC only if
1545 * "lapic" specified.
1547 if (!force_enable_local_apic
) {
1548 pr_info("Local APIC disabled by BIOS -- "
1549 "you can enable it with \"lapic\"\n");
1553 * Some BIOSes disable the local APIC in the APIC_BASE
1554 * MSR. This can only be done in software for Intel P6 or later
1555 * and AMD K7 (Model > 1) or later.
1557 rdmsr(MSR_IA32_APICBASE
, l
, h
);
1558 if (!(l
& MSR_IA32_APICBASE_ENABLE
)) {
1559 pr_info("Local APIC disabled by BIOS -- reenabling.\n");
1560 l
&= ~MSR_IA32_APICBASE_BASE
;
1561 l
|= MSR_IA32_APICBASE_ENABLE
| APIC_DEFAULT_PHYS_BASE
;
1562 wrmsr(MSR_IA32_APICBASE
, l
, h
);
1563 enabled_via_apicbase
= 1;
1567 * The APIC feature bit should now be enabled
1570 features
= cpuid_edx(1);
1571 if (!(features
& (1 << X86_FEATURE_APIC
))) {
1572 pr_warning("Could not enable APIC!\n");
1575 set_cpu_cap(&boot_cpu_data
, X86_FEATURE_APIC
);
1576 mp_lapic_addr
= APIC_DEFAULT_PHYS_BASE
;
1578 /* The BIOS may have set up the APIC at some other address */
1579 rdmsr(MSR_IA32_APICBASE
, l
, h
);
1580 if (l
& MSR_IA32_APICBASE_ENABLE
)
1581 mp_lapic_addr
= l
& MSR_IA32_APICBASE_BASE
;
1583 pr_info("Found and enabled local APIC!\n");
1590 pr_info("No local APIC present or hardware disabled\n");
1595 #ifdef CONFIG_X86_64
1596 void __init
early_init_lapic_mapping(void)
1599 * If no local APIC can be found then go out
1600 * : it means there is no mpatable and MADT
1602 if (!smp_found_config
)
1605 set_fixmap_nocache(FIX_APIC_BASE
, mp_lapic_addr
);
1606 apic_printk(APIC_VERBOSE
, "mapped APIC to %16lx (%16lx)\n",
1607 APIC_BASE
, mp_lapic_addr
);
1610 * Fetch the APIC ID of the BSP in case we have a
1611 * default configuration (or the MP table is broken).
1613 boot_cpu_physical_apicid
= read_apic_id();
1618 * init_apic_mappings - initialize APIC mappings
1620 void __init
init_apic_mappings(void)
1622 unsigned int new_apicid
;
1625 boot_cpu_physical_apicid
= read_apic_id();
1629 /* If no local APIC can be found return early */
1630 if (!smp_found_config
&& detect_init_APIC()) {
1631 /* lets NOP'ify apic operations */
1632 pr_info("APIC: disable apic facility\n");
1635 apic_phys
= mp_lapic_addr
;
1638 * acpi lapic path already maps that address in
1639 * acpi_register_lapic_address()
1642 set_fixmap_nocache(FIX_APIC_BASE
, apic_phys
);
1644 apic_printk(APIC_VERBOSE
, "mapped APIC to %08lx (%08lx)\n",
1645 APIC_BASE
, apic_phys
);
1649 * Fetch the APIC ID of the BSP in case we have a
1650 * default configuration (or the MP table is broken).
1652 new_apicid
= read_apic_id();
1653 if (boot_cpu_physical_apicid
!= new_apicid
) {
1654 boot_cpu_physical_apicid
= new_apicid
;
1656 * yeah -- we lie about apic_version
1657 * in case if apic was disabled via boot option
1658 * but it's not a problem for SMP compiled kernel
1659 * since smp_sanity_check is prepared for such a case
1660 * and disable smp mode
1662 apic_version
[new_apicid
] =
1663 GET_APIC_VERSION(apic_read(APIC_LVR
));
1668 * This initializes the IO-APIC and APIC hardware if this is
1671 int apic_version
[MAX_APICS
];
1673 int __init
APIC_init_uniprocessor(void)
1676 pr_info("Apic disabled\n");
1679 #ifdef CONFIG_X86_64
1680 if (!cpu_has_apic
) {
1682 pr_info("Apic disabled by BIOS\n");
1686 if (!smp_found_config
&& !cpu_has_apic
)
1690 * Complain if the BIOS pretends there is one.
1692 if (!cpu_has_apic
&&
1693 APIC_INTEGRATED(apic_version
[boot_cpu_physical_apicid
])) {
1694 pr_err("BIOS bug, local APIC 0x%x not detected!...\n",
1695 boot_cpu_physical_apicid
);
1702 default_setup_apic_routing();
1705 verify_local_APIC();
1708 #ifdef CONFIG_X86_64
1709 apic_write(APIC_ID
, SET_APIC_ID(boot_cpu_physical_apicid
));
1712 * Hack: In case of kdump, after a crash, kernel might be booting
1713 * on a cpu with non-zero lapic id. But boot_cpu_physical_apicid
1714 * might be zero if read from MP tables. Get it from LAPIC.
1716 # ifdef CONFIG_CRASH_DUMP
1717 boot_cpu_physical_apicid
= read_apic_id();
1720 physid_set_mask_of_physid(boot_cpu_physical_apicid
, &phys_cpu_present_map
);
1723 #ifdef CONFIG_X86_IO_APIC
1725 * Now enable IO-APICs, actually call clear_IO_APIC
1726 * We need clear_IO_APIC before enabling error vector
1728 if (!skip_ioapic_setup
&& nr_ioapics
)
1732 end_local_APIC_setup();
1734 #ifdef CONFIG_X86_IO_APIC
1735 if (smp_found_config
&& !skip_ioapic_setup
&& nr_ioapics
)
1739 localise_nmi_watchdog();
1742 localise_nmi_watchdog();
1745 x86_init
.timers
.setup_percpu_clockev();
1746 #ifdef CONFIG_X86_64
1747 check_nmi_watchdog();
1754 * Local APIC interrupts
1758 * This interrupt should _never_ happen with our APIC/SMP architecture
1760 void smp_spurious_interrupt(struct pt_regs
*regs
)
1767 * Check if this really is a spurious interrupt and ACK it
1768 * if it is a vectored one. Just in case...
1769 * Spurious interrupts should not be ACKed.
1771 v
= apic_read(APIC_ISR
+ ((SPURIOUS_APIC_VECTOR
& ~0x1f) >> 1));
1772 if (v
& (1 << (SPURIOUS_APIC_VECTOR
& 0x1f)))
1775 inc_irq_stat(irq_spurious_count
);
1777 /* see sw-dev-man vol 3, chapter 7.4.13.5 */
1778 pr_info("spurious APIC interrupt on CPU#%d, "
1779 "should never happen.\n", smp_processor_id());
1784 * This interrupt should never happen with our APIC/SMP architecture
1786 void smp_error_interrupt(struct pt_regs
*regs
)
1792 /* First tickle the hardware, only then report what went on. -- REW */
1793 v
= apic_read(APIC_ESR
);
1794 apic_write(APIC_ESR
, 0);
1795 v1
= apic_read(APIC_ESR
);
1797 atomic_inc(&irq_err_count
);
1800 * Here is what the APIC error bits mean:
1802 * 1: Receive CS error
1803 * 2: Send accept error
1804 * 3: Receive accept error
1806 * 5: Send illegal vector
1807 * 6: Received illegal vector
1808 * 7: Illegal register address
1810 pr_debug("APIC error on CPU%d: %02x(%02x)\n",
1811 smp_processor_id(), v
, v1
);
1816 * connect_bsp_APIC - attach the APIC to the interrupt system
1818 void __init
connect_bsp_APIC(void)
1820 #ifdef CONFIG_X86_32
1823 * Do not trust the local APIC being empty at bootup.
1827 * PIC mode, enable APIC mode in the IMCR, i.e. connect BSP's
1828 * local APIC to INT and NMI lines.
1830 apic_printk(APIC_VERBOSE
, "leaving PIC mode, "
1831 "enabling APIC mode.\n");
1835 if (apic
->enable_apic_mode
)
1836 apic
->enable_apic_mode();
1840 * disconnect_bsp_APIC - detach the APIC from the interrupt system
1841 * @virt_wire_setup: indicates, whether virtual wire mode is selected
1843 * Virtual wire mode is necessary to deliver legacy interrupts even when the
1846 void disconnect_bsp_APIC(int virt_wire_setup
)
1850 #ifdef CONFIG_X86_32
1853 * Put the board back into PIC mode (has an effect only on
1854 * certain older boards). Note that APIC interrupts, including
1855 * IPIs, won't work beyond this point! The only exception are
1858 apic_printk(APIC_VERBOSE
, "disabling APIC mode, "
1859 "entering PIC mode.\n");
1865 /* Go back to Virtual Wire compatibility mode */
1867 /* For the spurious interrupt use vector F, and enable it */
1868 value
= apic_read(APIC_SPIV
);
1869 value
&= ~APIC_VECTOR_MASK
;
1870 value
|= APIC_SPIV_APIC_ENABLED
;
1872 apic_write(APIC_SPIV
, value
);
1874 if (!virt_wire_setup
) {
1876 * For LVT0 make it edge triggered, active high,
1877 * external and enabled
1879 value
= apic_read(APIC_LVT0
);
1880 value
&= ~(APIC_MODE_MASK
| APIC_SEND_PENDING
|
1881 APIC_INPUT_POLARITY
| APIC_LVT_REMOTE_IRR
|
1882 APIC_LVT_LEVEL_TRIGGER
| APIC_LVT_MASKED
);
1883 value
|= APIC_LVT_REMOTE_IRR
| APIC_SEND_PENDING
;
1884 value
= SET_APIC_DELIVERY_MODE(value
, APIC_MODE_EXTINT
);
1885 apic_write(APIC_LVT0
, value
);
1888 apic_write(APIC_LVT0
, APIC_LVT_MASKED
);
1892 * For LVT1 make it edge triggered, active high,
1895 value
= apic_read(APIC_LVT1
);
1896 value
&= ~(APIC_MODE_MASK
| APIC_SEND_PENDING
|
1897 APIC_INPUT_POLARITY
| APIC_LVT_REMOTE_IRR
|
1898 APIC_LVT_LEVEL_TRIGGER
| APIC_LVT_MASKED
);
1899 value
|= APIC_LVT_REMOTE_IRR
| APIC_SEND_PENDING
;
1900 value
= SET_APIC_DELIVERY_MODE(value
, APIC_MODE_NMI
);
1901 apic_write(APIC_LVT1
, value
);
1904 void __cpuinit
generic_processor_info(int apicid
, int version
)
1911 if (version
== 0x0) {
1912 pr_warning("BIOS bug, APIC version is 0 for CPU#%d! "
1913 "fixing up to 0x10. (tell your hw vendor)\n",
1917 apic_version
[apicid
] = version
;
1919 if (num_processors
>= nr_cpu_ids
) {
1920 int max
= nr_cpu_ids
;
1921 int thiscpu
= max
+ disabled_cpus
;
1924 "ACPI: NR_CPUS/possible_cpus limit of %i reached."
1925 " Processor %d/0x%x ignored.\n", max
, thiscpu
, apicid
);
1932 cpu
= cpumask_next_zero(-1, cpu_present_mask
);
1934 if (version
!= apic_version
[boot_cpu_physical_apicid
])
1936 "ACPI: apic version mismatch, bootcpu: %x cpu %d: %x\n",
1937 apic_version
[boot_cpu_physical_apicid
], cpu
, version
);
1939 physid_set(apicid
, phys_cpu_present_map
);
1940 if (apicid
== boot_cpu_physical_apicid
) {
1942 * x86_bios_cpu_apicid is required to have processors listed
1943 * in same order as logical cpu numbers. Hence the first
1944 * entry is BSP, and so on.
1948 if (apicid
> max_physical_apicid
)
1949 max_physical_apicid
= apicid
;
1951 #if defined(CONFIG_SMP) || defined(CONFIG_X86_64)
1952 early_per_cpu(x86_cpu_to_apicid
, cpu
) = apicid
;
1953 early_per_cpu(x86_bios_cpu_apicid
, cpu
) = apicid
;
1956 set_cpu_possible(cpu
, true);
1957 set_cpu_present(cpu
, true);
1960 int hard_smp_processor_id(void)
1962 return read_apic_id();
1965 void default_init_apic_ldr(void)
1969 apic_write(APIC_DFR
, APIC_DFR_VALUE
);
1970 val
= apic_read(APIC_LDR
) & ~APIC_LDR_MASK
;
1971 val
|= SET_APIC_LOGICAL_ID(1UL << smp_processor_id());
1972 apic_write(APIC_LDR
, val
);
1975 #ifdef CONFIG_X86_32
1976 int default_apicid_to_node(int logical_apicid
)
1979 return apicid_2_node
[hard_smp_processor_id()];
1993 * 'active' is true if the local APIC was enabled by us and
1994 * not the BIOS; this signifies that we are also responsible
1995 * for disabling it before entering apm/acpi suspend
1998 /* r/w apic fields */
1999 unsigned int apic_id
;
2000 unsigned int apic_taskpri
;
2001 unsigned int apic_ldr
;
2002 unsigned int apic_dfr
;
2003 unsigned int apic_spiv
;
2004 unsigned int apic_lvtt
;
2005 unsigned int apic_lvtpc
;
2006 unsigned int apic_lvt0
;
2007 unsigned int apic_lvt1
;
2008 unsigned int apic_lvterr
;
2009 unsigned int apic_tmict
;
2010 unsigned int apic_tdcr
;
2011 unsigned int apic_thmr
;
2014 static int lapic_suspend(struct sys_device
*dev
, pm_message_t state
)
2016 unsigned long flags
;
2019 if (!apic_pm_state
.active
)
2022 maxlvt
= lapic_get_maxlvt();
2024 apic_pm_state
.apic_id
= apic_read(APIC_ID
);
2025 apic_pm_state
.apic_taskpri
= apic_read(APIC_TASKPRI
);
2026 apic_pm_state
.apic_ldr
= apic_read(APIC_LDR
);
2027 apic_pm_state
.apic_dfr
= apic_read(APIC_DFR
);
2028 apic_pm_state
.apic_spiv
= apic_read(APIC_SPIV
);
2029 apic_pm_state
.apic_lvtt
= apic_read(APIC_LVTT
);
2031 apic_pm_state
.apic_lvtpc
= apic_read(APIC_LVTPC
);
2032 apic_pm_state
.apic_lvt0
= apic_read(APIC_LVT0
);
2033 apic_pm_state
.apic_lvt1
= apic_read(APIC_LVT1
);
2034 apic_pm_state
.apic_lvterr
= apic_read(APIC_LVTERR
);
2035 apic_pm_state
.apic_tmict
= apic_read(APIC_TMICT
);
2036 apic_pm_state
.apic_tdcr
= apic_read(APIC_TDCR
);
2037 #ifdef CONFIG_X86_THERMAL_VECTOR
2039 apic_pm_state
.apic_thmr
= apic_read(APIC_LVTTHMR
);
2042 local_irq_save(flags
);
2043 disable_local_APIC();
2045 if (intr_remapping_enabled
)
2046 disable_intr_remapping();
2048 local_irq_restore(flags
);
2052 static int lapic_resume(struct sys_device
*dev
)
2055 unsigned long flags
;
2058 struct IO_APIC_route_entry
**ioapic_entries
= NULL
;
2060 if (!apic_pm_state
.active
)
2063 local_irq_save(flags
);
2064 if (intr_remapping_enabled
) {
2065 ioapic_entries
= alloc_ioapic_entries();
2066 if (!ioapic_entries
) {
2067 WARN(1, "Alloc ioapic_entries in lapic resume failed.");
2072 ret
= save_IO_APIC_setup(ioapic_entries
);
2074 WARN(1, "Saving IO-APIC state failed: %d\n", ret
);
2075 free_ioapic_entries(ioapic_entries
);
2079 mask_IO_APIC_setup(ioapic_entries
);
2087 * Make sure the APICBASE points to the right address
2089 * FIXME! This will be wrong if we ever support suspend on
2090 * SMP! We'll need to do this as part of the CPU restore!
2092 rdmsr(MSR_IA32_APICBASE
, l
, h
);
2093 l
&= ~MSR_IA32_APICBASE_BASE
;
2094 l
|= MSR_IA32_APICBASE_ENABLE
| mp_lapic_addr
;
2095 wrmsr(MSR_IA32_APICBASE
, l
, h
);
2098 maxlvt
= lapic_get_maxlvt();
2099 apic_write(APIC_LVTERR
, ERROR_APIC_VECTOR
| APIC_LVT_MASKED
);
2100 apic_write(APIC_ID
, apic_pm_state
.apic_id
);
2101 apic_write(APIC_DFR
, apic_pm_state
.apic_dfr
);
2102 apic_write(APIC_LDR
, apic_pm_state
.apic_ldr
);
2103 apic_write(APIC_TASKPRI
, apic_pm_state
.apic_taskpri
);
2104 apic_write(APIC_SPIV
, apic_pm_state
.apic_spiv
);
2105 apic_write(APIC_LVT0
, apic_pm_state
.apic_lvt0
);
2106 apic_write(APIC_LVT1
, apic_pm_state
.apic_lvt1
);
2107 #if defined(CONFIG_X86_MCE_P4THERMAL) || defined(CONFIG_X86_MCE_INTEL)
2109 apic_write(APIC_LVTTHMR
, apic_pm_state
.apic_thmr
);
2112 apic_write(APIC_LVTPC
, apic_pm_state
.apic_lvtpc
);
2113 apic_write(APIC_LVTT
, apic_pm_state
.apic_lvtt
);
2114 apic_write(APIC_TDCR
, apic_pm_state
.apic_tdcr
);
2115 apic_write(APIC_TMICT
, apic_pm_state
.apic_tmict
);
2116 apic_write(APIC_ESR
, 0);
2117 apic_read(APIC_ESR
);
2118 apic_write(APIC_LVTERR
, apic_pm_state
.apic_lvterr
);
2119 apic_write(APIC_ESR
, 0);
2120 apic_read(APIC_ESR
);
2122 if (intr_remapping_enabled
) {
2123 reenable_intr_remapping(x2apic_mode
);
2125 restore_IO_APIC_setup(ioapic_entries
);
2126 free_ioapic_entries(ioapic_entries
);
2129 local_irq_restore(flags
);
2135 * This device has no shutdown method - fully functioning local APICs
2136 * are needed on every CPU up until machine_halt/restart/poweroff.
2139 static struct sysdev_class lapic_sysclass
= {
2141 .resume
= lapic_resume
,
2142 .suspend
= lapic_suspend
,
2145 static struct sys_device device_lapic
= {
2147 .cls
= &lapic_sysclass
,
2150 static void __cpuinit
apic_pm_activate(void)
2152 apic_pm_state
.active
= 1;
2155 static int __init
init_lapic_sysfs(void)
2161 /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
2163 error
= sysdev_class_register(&lapic_sysclass
);
2165 error
= sysdev_register(&device_lapic
);
2169 /* local apic needs to resume before other devices access its registers. */
2170 core_initcall(init_lapic_sysfs
);
2172 #else /* CONFIG_PM */
2174 static void apic_pm_activate(void) { }
2176 #endif /* CONFIG_PM */
2178 #ifdef CONFIG_X86_64
2180 static int __cpuinit
apic_cluster_num(void)
2182 int i
, clusters
, zeros
;
2184 u16
*bios_cpu_apicid
;
2185 DECLARE_BITMAP(clustermap
, NUM_APIC_CLUSTERS
);
2187 bios_cpu_apicid
= early_per_cpu_ptr(x86_bios_cpu_apicid
);
2188 bitmap_zero(clustermap
, NUM_APIC_CLUSTERS
);
2190 for (i
= 0; i
< nr_cpu_ids
; i
++) {
2191 /* are we being called early in kernel startup? */
2192 if (bios_cpu_apicid
) {
2193 id
= bios_cpu_apicid
[i
];
2194 } else if (i
< nr_cpu_ids
) {
2196 id
= per_cpu(x86_bios_cpu_apicid
, i
);
2202 if (id
!= BAD_APICID
)
2203 __set_bit(APIC_CLUSTERID(id
), clustermap
);
2206 /* Problem: Partially populated chassis may not have CPUs in some of
2207 * the APIC clusters they have been allocated. Only present CPUs have
2208 * x86_bios_cpu_apicid entries, thus causing zeroes in the bitmap.
2209 * Since clusters are allocated sequentially, count zeros only if
2210 * they are bounded by ones.
2214 for (i
= 0; i
< NUM_APIC_CLUSTERS
; i
++) {
2215 if (test_bit(i
, clustermap
)) {
2216 clusters
+= 1 + zeros
;
2225 static int __cpuinitdata multi_checked
;
2226 static int __cpuinitdata multi
;
2228 static int __cpuinit
set_multi(const struct dmi_system_id
*d
)
2232 pr_info("APIC: %s detected, Multi Chassis\n", d
->ident
);
2237 static const __cpuinitconst
struct dmi_system_id multi_dmi_table
[] = {
2239 .callback
= set_multi
,
2240 .ident
= "IBM System Summit2",
2242 DMI_MATCH(DMI_SYS_VENDOR
, "IBM"),
2243 DMI_MATCH(DMI_PRODUCT_NAME
, "Summit2"),
2249 static void __cpuinit
dmi_check_multi(void)
2254 dmi_check_system(multi_dmi_table
);
2259 * apic_is_clustered_box() -- Check if we can expect good TSC
2261 * Thus far, the major user of this is IBM's Summit2 series:
2262 * Clustered boxes may have unsynced TSC problems if they are
2264 * Use DMI to check them
2266 __cpuinit
int apic_is_clustered_box(void)
2276 * ScaleMP vSMPowered boxes have one cluster per board and TSCs are
2277 * not guaranteed to be synced between boards
2279 if (apic_cluster_num() > 1)
2287 * APIC command line parameters
2289 static int __init
setup_disableapic(char *arg
)
2292 setup_clear_cpu_cap(X86_FEATURE_APIC
);
2295 early_param("disableapic", setup_disableapic
);
2297 /* same as disableapic, for compatibility */
2298 static int __init
setup_nolapic(char *arg
)
2300 return setup_disableapic(arg
);
2302 early_param("nolapic", setup_nolapic
);
2304 static int __init
parse_lapic_timer_c2_ok(char *arg
)
2306 local_apic_timer_c2_ok
= 1;
2309 early_param("lapic_timer_c2_ok", parse_lapic_timer_c2_ok
);
2311 static int __init
parse_disable_apic_timer(char *arg
)
2313 disable_apic_timer
= 1;
2316 early_param("noapictimer", parse_disable_apic_timer
);
2318 static int __init
parse_nolapic_timer(char *arg
)
2320 disable_apic_timer
= 1;
2323 early_param("nolapic_timer", parse_nolapic_timer
);
2325 static int __init
apic_set_verbosity(char *arg
)
2328 #ifdef CONFIG_X86_64
2329 skip_ioapic_setup
= 0;
2335 if (strcmp("debug", arg
) == 0)
2336 apic_verbosity
= APIC_DEBUG
;
2337 else if (strcmp("verbose", arg
) == 0)
2338 apic_verbosity
= APIC_VERBOSE
;
2340 pr_warning("APIC Verbosity level %s not recognised"
2341 " use apic=verbose or apic=debug\n", arg
);
2347 early_param("apic", apic_set_verbosity
);
2349 static int __init
lapic_insert_resource(void)
2354 /* Put local APIC into the resource map. */
2355 lapic_resource
.start
= apic_phys
;
2356 lapic_resource
.end
= lapic_resource
.start
+ PAGE_SIZE
- 1;
2357 insert_resource(&iomem_resource
, &lapic_resource
);
2363 * need call insert after e820_reserve_resources()
2364 * that is using request_resource
2366 late_initcall(lapic_insert_resource
);