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/kernel_stat.h>
18 #include <linux/mc146818rtc.h>
19 #include <linux/acpi_pmtmr.h>
20 #include <linux/clockchips.h>
21 #include <linux/interrupt.h>
22 #include <linux/bootmem.h>
23 #include <linux/ftrace.h>
24 #include <linux/ioport.h>
25 #include <linux/module.h>
26 #include <linux/sysdev.h>
27 #include <linux/delay.h>
28 #include <linux/timex.h>
29 #include <linux/dmar.h>
30 #include <linux/init.h>
31 #include <linux/cpu.h>
32 #include <linux/dmi.h>
33 #include <linux/nmi.h>
34 #include <linux/smp.h>
37 #include <asm/arch_hooks.h>
38 #include <asm/pgalloc.h>
39 #include <asm/genapic.h>
40 #include <asm/atomic.h>
41 #include <asm/mpspec.h>
42 #include <asm/i8253.h>
43 #include <asm/i8259.h>
44 #include <asm/proto.h>
52 unsigned int num_processors
;
54 unsigned disabled_cpus __cpuinitdata
;
56 /* Processor that is doing the boot up */
57 unsigned int boot_cpu_physical_apicid
= -1U;
60 * The highest APIC ID seen during enumeration.
62 * This determines the messaging protocol we can use: if all APIC IDs
63 * are in the 0 ... 7 range, then we can use logical addressing which
64 * has some performance advantages (better broadcasting).
66 * If there's an APIC ID above 8, we use physical addressing.
68 unsigned int max_physical_apicid
;
71 * Bitmask of physically existing CPUs:
73 physid_mask_t phys_cpu_present_map
;
76 * Map cpu index to physical APIC ID
78 DEFINE_EARLY_PER_CPU(u16
, x86_cpu_to_apicid
, BAD_APICID
);
79 DEFINE_EARLY_PER_CPU(u16
, x86_bios_cpu_apicid
, BAD_APICID
);
80 EXPORT_EARLY_PER_CPU_SYMBOL(x86_cpu_to_apicid
);
81 EXPORT_EARLY_PER_CPU_SYMBOL(x86_bios_cpu_apicid
);
85 * Knob to control our willingness to enable the local APIC.
89 static int force_enable_local_apic
;
91 * APIC command line parameters
93 static int __init
parse_lapic(char *arg
)
95 force_enable_local_apic
= 1;
98 early_param("lapic", parse_lapic
);
99 /* Local APIC was disabled by the BIOS and enabled by the kernel */
100 static int enabled_via_apicbase
;
105 static int apic_calibrate_pmtmr __initdata
;
106 static __init
int setup_apicpmtimer(char *s
)
108 apic_calibrate_pmtmr
= 1;
112 __setup("apicpmtimer", setup_apicpmtimer
);
115 #ifdef CONFIG_X86_X2APIC
117 /* x2apic enabled before OS handover */
118 static int x2apic_preenabled
;
119 static int disable_x2apic
;
120 static __init
int setup_nox2apic(char *str
)
123 setup_clear_cpu_cap(X86_FEATURE_X2APIC
);
126 early_param("nox2apic", setup_nox2apic
);
129 unsigned long mp_lapic_addr
;
131 /* Disable local APIC timer from the kernel commandline or via dmi quirk */
132 static int disable_apic_timer __cpuinitdata
;
133 /* Local APIC timer works in C2 */
134 int local_apic_timer_c2_ok
;
135 EXPORT_SYMBOL_GPL(local_apic_timer_c2_ok
);
137 int first_system_vector
= 0xfe;
140 * Debug level, exported for io_apic.c
142 unsigned int apic_verbosity
;
146 /* Have we found an MP table */
147 int smp_found_config
;
149 static struct resource lapic_resource
= {
150 .name
= "Local APIC",
151 .flags
= IORESOURCE_MEM
| IORESOURCE_BUSY
,
154 static unsigned int calibration_result
;
156 static int lapic_next_event(unsigned long delta
,
157 struct clock_event_device
*evt
);
158 static void lapic_timer_setup(enum clock_event_mode mode
,
159 struct clock_event_device
*evt
);
160 static void lapic_timer_broadcast(const struct cpumask
*mask
);
161 static void apic_pm_activate(void);
164 * The local apic timer can be used for any function which is CPU local.
166 static struct clock_event_device lapic_clockevent
= {
168 .features
= CLOCK_EVT_FEAT_PERIODIC
| CLOCK_EVT_FEAT_ONESHOT
169 | CLOCK_EVT_FEAT_C3STOP
| CLOCK_EVT_FEAT_DUMMY
,
171 .set_mode
= lapic_timer_setup
,
172 .set_next_event
= lapic_next_event
,
173 .broadcast
= lapic_timer_broadcast
,
177 static DEFINE_PER_CPU(struct clock_event_device
, lapic_events
);
179 static unsigned long apic_phys
;
182 * Get the LAPIC version
184 static inline int lapic_get_version(void)
186 return GET_APIC_VERSION(apic_read(APIC_LVR
));
190 * Check, if the APIC is integrated or a separate chip
192 static inline int lapic_is_integrated(void)
197 return APIC_INTEGRATED(lapic_get_version());
202 * Check, whether this is a modern or a first generation APIC
204 static int modern_apic(void)
206 /* AMD systems use old APIC versions, so check the CPU */
207 if (boot_cpu_data
.x86_vendor
== X86_VENDOR_AMD
&&
208 boot_cpu_data
.x86
>= 0xf)
210 return lapic_get_version() >= 0x14;
214 * Paravirt kernels also might be using these below ops. So we still
215 * use generic apic_read()/apic_write(), which might be pointing to different
216 * ops in PARAVIRT case.
218 void xapic_wait_icr_idle(void)
220 while (apic_read(APIC_ICR
) & APIC_ICR_BUSY
)
224 u32
safe_xapic_wait_icr_idle(void)
231 send_status
= apic_read(APIC_ICR
) & APIC_ICR_BUSY
;
235 } while (timeout
++ < 1000);
240 void xapic_icr_write(u32 low
, u32 id
)
242 apic_write(APIC_ICR2
, SET_APIC_DEST_FIELD(id
));
243 apic_write(APIC_ICR
, low
);
246 static u64
xapic_icr_read(void)
250 icr2
= apic_read(APIC_ICR2
);
251 icr1
= apic_read(APIC_ICR
);
253 return icr1
| ((u64
)icr2
<< 32);
256 static struct apic_ops xapic_ops
= {
257 .read
= native_apic_mem_read
,
258 .write
= native_apic_mem_write
,
259 .icr_read
= xapic_icr_read
,
260 .icr_write
= xapic_icr_write
,
261 .wait_icr_idle
= xapic_wait_icr_idle
,
262 .safe_wait_icr_idle
= safe_xapic_wait_icr_idle
,
265 struct apic_ops __read_mostly
*apic_ops
= &xapic_ops
;
266 EXPORT_SYMBOL_GPL(apic_ops
);
268 #ifdef CONFIG_X86_X2APIC
269 static void x2apic_wait_icr_idle(void)
271 /* no need to wait for icr idle in x2apic */
275 static u32
safe_x2apic_wait_icr_idle(void)
277 /* no need to wait for icr idle in x2apic */
281 void x2apic_icr_write(u32 low
, u32 id
)
283 wrmsrl(APIC_BASE_MSR
+ (APIC_ICR
>> 4), ((__u64
) id
) << 32 | low
);
286 static u64
x2apic_icr_read(void)
290 rdmsrl(APIC_BASE_MSR
+ (APIC_ICR
>> 4), val
);
294 static struct apic_ops x2apic_ops
= {
295 .read
= native_apic_msr_read
,
296 .write
= native_apic_msr_write
,
297 .icr_read
= x2apic_icr_read
,
298 .icr_write
= x2apic_icr_write
,
299 .wait_icr_idle
= x2apic_wait_icr_idle
,
300 .safe_wait_icr_idle
= safe_x2apic_wait_icr_idle
,
305 * enable_NMI_through_LVT0 - enable NMI through local vector table 0
307 void __cpuinit
enable_NMI_through_LVT0(void)
311 /* unmask and set to NMI */
314 /* Level triggered for 82489DX (32bit mode) */
315 if (!lapic_is_integrated())
316 v
|= APIC_LVT_LEVEL_TRIGGER
;
318 apic_write(APIC_LVT0
, v
);
323 * get_physical_broadcast - Get number of physical broadcast IDs
325 int get_physical_broadcast(void)
327 return modern_apic() ? 0xff : 0xf;
332 * lapic_get_maxlvt - get the maximum number of local vector table entries
334 int lapic_get_maxlvt(void)
338 v
= apic_read(APIC_LVR
);
340 * - we always have APIC integrated on 64bit mode
341 * - 82489DXs do not report # of LVT entries
343 return APIC_INTEGRATED(GET_APIC_VERSION(v
)) ? GET_APIC_MAXLVT(v
) : 2;
351 #define APIC_DIVISOR 16
354 * This function sets up the local APIC timer, with a timeout of
355 * 'clocks' APIC bus clock. During calibration we actually call
356 * this function twice on the boot CPU, once with a bogus timeout
357 * value, second time for real. The other (noncalibrating) CPUs
358 * call this function only once, with the real, calibrated value.
360 * We do reads before writes even if unnecessary, to get around the
361 * P5 APIC double write bug.
363 static void __setup_APIC_LVTT(unsigned int clocks
, int oneshot
, int irqen
)
365 unsigned int lvtt_value
, tmp_value
;
367 lvtt_value
= LOCAL_TIMER_VECTOR
;
369 lvtt_value
|= APIC_LVT_TIMER_PERIODIC
;
370 if (!lapic_is_integrated())
371 lvtt_value
|= SET_APIC_TIMER_BASE(APIC_TIMER_BASE_DIV
);
374 lvtt_value
|= APIC_LVT_MASKED
;
376 apic_write(APIC_LVTT
, lvtt_value
);
381 tmp_value
= apic_read(APIC_TDCR
);
382 apic_write(APIC_TDCR
,
383 (tmp_value
& ~(APIC_TDR_DIV_1
| APIC_TDR_DIV_TMBASE
)) |
387 apic_write(APIC_TMICT
, clocks
/ APIC_DIVISOR
);
391 * Setup extended LVT, AMD specific (K8, family 10h)
393 * Vector mappings are hard coded. On K8 only offset 0 (APIC500) and
394 * MCE interrupts are supported. Thus MCE offset must be set to 0.
396 * If mask=1, the LVT entry does not generate interrupts while mask=0
397 * enables the vector. See also the BKDGs.
400 #define APIC_EILVT_LVTOFF_MCE 0
401 #define APIC_EILVT_LVTOFF_IBS 1
403 static void setup_APIC_eilvt(u8 lvt_off
, u8 vector
, u8 msg_type
, u8 mask
)
405 unsigned long reg
= (lvt_off
<< 4) + APIC_EILVT0
;
406 unsigned int v
= (mask
<< 16) | (msg_type
<< 8) | vector
;
411 u8
setup_APIC_eilvt_mce(u8 vector
, u8 msg_type
, u8 mask
)
413 setup_APIC_eilvt(APIC_EILVT_LVTOFF_MCE
, vector
, msg_type
, mask
);
414 return APIC_EILVT_LVTOFF_MCE
;
417 u8
setup_APIC_eilvt_ibs(u8 vector
, u8 msg_type
, u8 mask
)
419 setup_APIC_eilvt(APIC_EILVT_LVTOFF_IBS
, vector
, msg_type
, mask
);
420 return APIC_EILVT_LVTOFF_IBS
;
422 EXPORT_SYMBOL_GPL(setup_APIC_eilvt_ibs
);
425 * Program the next event, relative to now
427 static int lapic_next_event(unsigned long delta
,
428 struct clock_event_device
*evt
)
430 apic_write(APIC_TMICT
, delta
);
435 * Setup the lapic timer in periodic or oneshot mode
437 static void lapic_timer_setup(enum clock_event_mode mode
,
438 struct clock_event_device
*evt
)
443 /* Lapic used as dummy for broadcast ? */
444 if (evt
->features
& CLOCK_EVT_FEAT_DUMMY
)
447 local_irq_save(flags
);
450 case CLOCK_EVT_MODE_PERIODIC
:
451 case CLOCK_EVT_MODE_ONESHOT
:
452 __setup_APIC_LVTT(calibration_result
,
453 mode
!= CLOCK_EVT_MODE_PERIODIC
, 1);
455 case CLOCK_EVT_MODE_UNUSED
:
456 case CLOCK_EVT_MODE_SHUTDOWN
:
457 v
= apic_read(APIC_LVTT
);
458 v
|= (APIC_LVT_MASKED
| LOCAL_TIMER_VECTOR
);
459 apic_write(APIC_LVTT
, v
);
460 apic_write(APIC_TMICT
, 0xffffffff);
462 case CLOCK_EVT_MODE_RESUME
:
463 /* Nothing to do here */
467 local_irq_restore(flags
);
471 * Local APIC timer broadcast function
473 static void lapic_timer_broadcast(const struct cpumask
*mask
)
476 apic
->send_IPI_mask(mask
, LOCAL_TIMER_VECTOR
);
481 * Setup the local APIC timer for this CPU. Copy the initilized values
482 * of the boot CPU and register the clock event in the framework.
484 static void __cpuinit
setup_APIC_timer(void)
486 struct clock_event_device
*levt
= &__get_cpu_var(lapic_events
);
488 memcpy(levt
, &lapic_clockevent
, sizeof(*levt
));
489 levt
->cpumask
= cpumask_of(smp_processor_id());
491 clockevents_register_device(levt
);
495 * In this functions we calibrate APIC bus clocks to the external timer.
497 * We want to do the calibration only once since we want to have local timer
498 * irqs syncron. CPUs connected by the same APIC bus have the very same bus
501 * This was previously done by reading the PIT/HPET and waiting for a wrap
502 * around to find out, that a tick has elapsed. I have a box, where the PIT
503 * readout is broken, so it never gets out of the wait loop again. This was
504 * also reported by others.
506 * Monitoring the jiffies value is inaccurate and the clockevents
507 * infrastructure allows us to do a simple substitution of the interrupt
510 * The calibration routine also uses the pm_timer when possible, as the PIT
511 * happens to run way too slow (factor 2.3 on my VAIO CoreDuo, which goes
512 * back to normal later in the boot process).
515 #define LAPIC_CAL_LOOPS (HZ/10)
517 static __initdata
int lapic_cal_loops
= -1;
518 static __initdata
long lapic_cal_t1
, lapic_cal_t2
;
519 static __initdata
unsigned long long lapic_cal_tsc1
, lapic_cal_tsc2
;
520 static __initdata
unsigned long lapic_cal_pm1
, lapic_cal_pm2
;
521 static __initdata
unsigned long lapic_cal_j1
, lapic_cal_j2
;
524 * Temporary interrupt handler.
526 static void __init
lapic_cal_handler(struct clock_event_device
*dev
)
528 unsigned long long tsc
= 0;
529 long tapic
= apic_read(APIC_TMCCT
);
530 unsigned long pm
= acpi_pm_read_early();
535 switch (lapic_cal_loops
++) {
537 lapic_cal_t1
= tapic
;
538 lapic_cal_tsc1
= tsc
;
540 lapic_cal_j1
= jiffies
;
543 case LAPIC_CAL_LOOPS
:
544 lapic_cal_t2
= tapic
;
545 lapic_cal_tsc2
= tsc
;
546 if (pm
< lapic_cal_pm1
)
547 pm
+= ACPI_PM_OVRRUN
;
549 lapic_cal_j2
= jiffies
;
555 calibrate_by_pmtimer(long deltapm
, long *delta
, long *deltatsc
)
557 const long pm_100ms
= PMTMR_TICKS_PER_SEC
/ 10;
558 const long pm_thresh
= pm_100ms
/ 100;
562 #ifndef CONFIG_X86_PM_TIMER
566 apic_printk(APIC_VERBOSE
, "... PM-Timer delta = %ld\n", deltapm
);
568 /* Check, if the PM timer is available */
572 mult
= clocksource_hz2mult(PMTMR_TICKS_PER_SEC
, 22);
574 if (deltapm
> (pm_100ms
- pm_thresh
) &&
575 deltapm
< (pm_100ms
+ pm_thresh
)) {
576 apic_printk(APIC_VERBOSE
, "... PM-Timer result ok\n");
580 res
= (((u64
)deltapm
) * mult
) >> 22;
581 do_div(res
, 1000000);
582 pr_warning("APIC calibration not consistent "
583 "with PM-Timer: %ldms instead of 100ms\n",(long)res
);
585 /* Correct the lapic counter value */
586 res
= (((u64
)(*delta
)) * pm_100ms
);
587 do_div(res
, deltapm
);
588 pr_info("APIC delta adjusted to PM-Timer: "
589 "%lu (%ld)\n", (unsigned long)res
, *delta
);
592 /* Correct the tsc counter value */
594 res
= (((u64
)(*deltatsc
)) * pm_100ms
);
595 do_div(res
, deltapm
);
596 apic_printk(APIC_VERBOSE
, "TSC delta adjusted to "
597 "PM-Timer: %lu (%ld) \n",
598 (unsigned long)res
, *deltatsc
);
599 *deltatsc
= (long)res
;
605 static int __init
calibrate_APIC_clock(void)
607 struct clock_event_device
*levt
= &__get_cpu_var(lapic_events
);
608 void (*real_handler
)(struct clock_event_device
*dev
);
609 unsigned long deltaj
;
610 long delta
, deltatsc
;
611 int pm_referenced
= 0;
615 /* Replace the global interrupt handler */
616 real_handler
= global_clock_event
->event_handler
;
617 global_clock_event
->event_handler
= lapic_cal_handler
;
620 * Setup the APIC counter to maximum. There is no way the lapic
621 * can underflow in the 100ms detection time frame
623 __setup_APIC_LVTT(0xffffffff, 0, 0);
625 /* Let the interrupts run */
628 while (lapic_cal_loops
<= LAPIC_CAL_LOOPS
)
633 /* Restore the real event handler */
634 global_clock_event
->event_handler
= real_handler
;
636 /* Build delta t1-t2 as apic timer counts down */
637 delta
= lapic_cal_t1
- lapic_cal_t2
;
638 apic_printk(APIC_VERBOSE
, "... lapic delta = %ld\n", delta
);
640 deltatsc
= (long)(lapic_cal_tsc2
- lapic_cal_tsc1
);
642 /* we trust the PM based calibration if possible */
643 pm_referenced
= !calibrate_by_pmtimer(lapic_cal_pm2
- lapic_cal_pm1
,
646 /* Calculate the scaled math multiplication factor */
647 lapic_clockevent
.mult
= div_sc(delta
, TICK_NSEC
* LAPIC_CAL_LOOPS
,
648 lapic_clockevent
.shift
);
649 lapic_clockevent
.max_delta_ns
=
650 clockevent_delta2ns(0x7FFFFF, &lapic_clockevent
);
651 lapic_clockevent
.min_delta_ns
=
652 clockevent_delta2ns(0xF, &lapic_clockevent
);
654 calibration_result
= (delta
* APIC_DIVISOR
) / LAPIC_CAL_LOOPS
;
656 apic_printk(APIC_VERBOSE
, "..... delta %ld\n", delta
);
657 apic_printk(APIC_VERBOSE
, "..... mult: %ld\n", lapic_clockevent
.mult
);
658 apic_printk(APIC_VERBOSE
, "..... calibration result: %u\n",
662 apic_printk(APIC_VERBOSE
, "..... CPU clock speed is "
664 (deltatsc
/ LAPIC_CAL_LOOPS
) / (1000000 / HZ
),
665 (deltatsc
/ LAPIC_CAL_LOOPS
) % (1000000 / HZ
));
668 apic_printk(APIC_VERBOSE
, "..... host bus clock speed is "
670 calibration_result
/ (1000000 / HZ
),
671 calibration_result
% (1000000 / HZ
));
674 * Do a sanity check on the APIC calibration result
676 if (calibration_result
< (1000000 / HZ
)) {
678 pr_warning("APIC frequency too slow, disabling apic timer\n");
682 levt
->features
&= ~CLOCK_EVT_FEAT_DUMMY
;
685 * PM timer calibration failed or not turned on
686 * so lets try APIC timer based calibration
688 if (!pm_referenced
) {
689 apic_printk(APIC_VERBOSE
, "... verify APIC timer\n");
692 * Setup the apic timer manually
694 levt
->event_handler
= lapic_cal_handler
;
695 lapic_timer_setup(CLOCK_EVT_MODE_PERIODIC
, levt
);
696 lapic_cal_loops
= -1;
698 /* Let the interrupts run */
701 while (lapic_cal_loops
<= LAPIC_CAL_LOOPS
)
704 /* Stop the lapic timer */
705 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN
, levt
);
708 deltaj
= lapic_cal_j2
- lapic_cal_j1
;
709 apic_printk(APIC_VERBOSE
, "... jiffies delta = %lu\n", deltaj
);
711 /* Check, if the jiffies result is consistent */
712 if (deltaj
>= LAPIC_CAL_LOOPS
-2 && deltaj
<= LAPIC_CAL_LOOPS
+2)
713 apic_printk(APIC_VERBOSE
, "... jiffies result ok\n");
715 levt
->features
|= CLOCK_EVT_FEAT_DUMMY
;
719 if (levt
->features
& CLOCK_EVT_FEAT_DUMMY
) {
720 pr_warning("APIC timer disabled due to verification failure\n");
728 * Setup the boot APIC
730 * Calibrate and verify the result.
732 void __init
setup_boot_APIC_clock(void)
735 * The local apic timer can be disabled via the kernel
736 * commandline or from the CPU detection code. Register the lapic
737 * timer as a dummy clock event source on SMP systems, so the
738 * broadcast mechanism is used. On UP systems simply ignore it.
740 if (disable_apic_timer
) {
741 pr_info("Disabling APIC timer\n");
742 /* No broadcast on UP ! */
743 if (num_possible_cpus() > 1) {
744 lapic_clockevent
.mult
= 1;
750 apic_printk(APIC_VERBOSE
, "Using local APIC timer interrupts.\n"
751 "calibrating APIC timer ...\n");
753 if (calibrate_APIC_clock()) {
754 /* No broadcast on UP ! */
755 if (num_possible_cpus() > 1)
761 * If nmi_watchdog is set to IO_APIC, we need the
762 * PIT/HPET going. Otherwise register lapic as a dummy
765 if (nmi_watchdog
!= NMI_IO_APIC
)
766 lapic_clockevent
.features
&= ~CLOCK_EVT_FEAT_DUMMY
;
768 pr_warning("APIC timer registered as dummy,"
769 " due to nmi_watchdog=%d!\n", nmi_watchdog
);
771 /* Setup the lapic or request the broadcast */
775 void __cpuinit
setup_secondary_APIC_clock(void)
781 * The guts of the apic timer interrupt
783 static void local_apic_timer_interrupt(void)
785 int cpu
= smp_processor_id();
786 struct clock_event_device
*evt
= &per_cpu(lapic_events
, cpu
);
789 * Normally we should not be here till LAPIC has been initialized but
790 * in some cases like kdump, its possible that there is a pending LAPIC
791 * timer interrupt from previous kernel's context and is delivered in
792 * new kernel the moment interrupts are enabled.
794 * Interrupts are enabled early and LAPIC is setup much later, hence
795 * its possible that when we get here evt->event_handler is NULL.
796 * Check for event_handler being NULL and discard the interrupt as
799 if (!evt
->event_handler
) {
800 pr_warning("Spurious LAPIC timer interrupt on cpu %d\n", cpu
);
802 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN
, evt
);
807 * the NMI deadlock-detector uses this.
809 inc_irq_stat(apic_timer_irqs
);
811 evt
->event_handler(evt
);
815 * Local APIC timer interrupt. This is the most natural way for doing
816 * local interrupts, but local timer interrupts can be emulated by
817 * broadcast interrupts too. [in case the hw doesn't support APIC timers]
819 * [ if a single-CPU system runs an SMP kernel then we call the local
820 * interrupt as well. Thus we cannot inline the local irq ... ]
822 void __irq_entry
smp_apic_timer_interrupt(struct pt_regs
*regs
)
824 struct pt_regs
*old_regs
= set_irq_regs(regs
);
827 * NOTE! We'd better ACK the irq immediately,
828 * because timer handling can be slow.
832 * update_process_times() expects us to have done irq_enter().
833 * Besides, if we don't timer interrupts ignore the global
834 * interrupt lock, which is the WrongThing (tm) to do.
838 local_apic_timer_interrupt();
841 set_irq_regs(old_regs
);
844 int setup_profiling_timer(unsigned int multiplier
)
850 * Local APIC start and shutdown
854 * clear_local_APIC - shutdown the local APIC
856 * This is called, when a CPU is disabled and before rebooting, so the state of
857 * the local APIC has no dangling leftovers. Also used to cleanout any BIOS
858 * leftovers during boot.
860 void clear_local_APIC(void)
865 /* APIC hasn't been mapped yet */
869 maxlvt
= lapic_get_maxlvt();
871 * Masking an LVT entry can trigger a local APIC error
872 * if the vector is zero. Mask LVTERR first to prevent this.
875 v
= ERROR_APIC_VECTOR
; /* any non-zero vector will do */
876 apic_write(APIC_LVTERR
, v
| APIC_LVT_MASKED
);
879 * Careful: we have to set masks only first to deassert
880 * any level-triggered sources.
882 v
= apic_read(APIC_LVTT
);
883 apic_write(APIC_LVTT
, v
| APIC_LVT_MASKED
);
884 v
= apic_read(APIC_LVT0
);
885 apic_write(APIC_LVT0
, v
| APIC_LVT_MASKED
);
886 v
= apic_read(APIC_LVT1
);
887 apic_write(APIC_LVT1
, v
| APIC_LVT_MASKED
);
889 v
= apic_read(APIC_LVTPC
);
890 apic_write(APIC_LVTPC
, v
| APIC_LVT_MASKED
);
893 /* lets not touch this if we didn't frob it */
894 #if defined(CONFIG_X86_MCE_P4THERMAL) || defined(X86_MCE_INTEL)
896 v
= apic_read(APIC_LVTTHMR
);
897 apic_write(APIC_LVTTHMR
, v
| APIC_LVT_MASKED
);
901 * Clean APIC state for other OSs:
903 apic_write(APIC_LVTT
, APIC_LVT_MASKED
);
904 apic_write(APIC_LVT0
, APIC_LVT_MASKED
);
905 apic_write(APIC_LVT1
, APIC_LVT_MASKED
);
907 apic_write(APIC_LVTERR
, APIC_LVT_MASKED
);
909 apic_write(APIC_LVTPC
, APIC_LVT_MASKED
);
911 /* Integrated APIC (!82489DX) ? */
912 if (lapic_is_integrated()) {
914 /* Clear ESR due to Pentium errata 3AP and 11AP */
915 apic_write(APIC_ESR
, 0);
921 * disable_local_APIC - clear and disable the local APIC
923 void disable_local_APIC(void)
927 /* APIC hasn't been mapped yet */
934 * Disable APIC (implies clearing of registers
937 value
= apic_read(APIC_SPIV
);
938 value
&= ~APIC_SPIV_APIC_ENABLED
;
939 apic_write(APIC_SPIV
, value
);
943 * When LAPIC was disabled by the BIOS and enabled by the kernel,
944 * restore the disabled state.
946 if (enabled_via_apicbase
) {
949 rdmsr(MSR_IA32_APICBASE
, l
, h
);
950 l
&= ~MSR_IA32_APICBASE_ENABLE
;
951 wrmsr(MSR_IA32_APICBASE
, l
, h
);
957 * If Linux enabled the LAPIC against the BIOS default disable it down before
958 * re-entering the BIOS on shutdown. Otherwise the BIOS may get confused and
959 * not power-off. Additionally clear all LVT entries before disable_local_APIC
960 * for the case where Linux didn't enable the LAPIC.
962 void lapic_shutdown(void)
969 local_irq_save(flags
);
972 if (!enabled_via_apicbase
)
976 disable_local_APIC();
979 local_irq_restore(flags
);
983 * This is to verify that we're looking at a real local APIC.
984 * Check these against your board if the CPUs aren't getting
985 * started for no apparent reason.
987 int __init
verify_local_APIC(void)
989 unsigned int reg0
, reg1
;
992 * The version register is read-only in a real APIC.
994 reg0
= apic_read(APIC_LVR
);
995 apic_printk(APIC_DEBUG
, "Getting VERSION: %x\n", reg0
);
996 apic_write(APIC_LVR
, reg0
^ APIC_LVR_MASK
);
997 reg1
= apic_read(APIC_LVR
);
998 apic_printk(APIC_DEBUG
, "Getting VERSION: %x\n", reg1
);
1001 * The two version reads above should print the same
1002 * numbers. If the second one is different, then we
1003 * poke at a non-APIC.
1009 * Check if the version looks reasonably.
1011 reg1
= GET_APIC_VERSION(reg0
);
1012 if (reg1
== 0x00 || reg1
== 0xff)
1014 reg1
= lapic_get_maxlvt();
1015 if (reg1
< 0x02 || reg1
== 0xff)
1019 * The ID register is read/write in a real APIC.
1021 reg0
= apic_read(APIC_ID
);
1022 apic_printk(APIC_DEBUG
, "Getting ID: %x\n", reg0
);
1023 apic_write(APIC_ID
, reg0
^ apic
->apic_id_mask
);
1024 reg1
= apic_read(APIC_ID
);
1025 apic_printk(APIC_DEBUG
, "Getting ID: %x\n", reg1
);
1026 apic_write(APIC_ID
, reg0
);
1027 if (reg1
!= (reg0
^ apic
->apic_id_mask
))
1031 * The next two are just to see if we have sane values.
1032 * They're only really relevant if we're in Virtual Wire
1033 * compatibility mode, but most boxes are anymore.
1035 reg0
= apic_read(APIC_LVT0
);
1036 apic_printk(APIC_DEBUG
, "Getting LVT0: %x\n", reg0
);
1037 reg1
= apic_read(APIC_LVT1
);
1038 apic_printk(APIC_DEBUG
, "Getting LVT1: %x\n", reg1
);
1044 * sync_Arb_IDs - synchronize APIC bus arbitration IDs
1046 void __init
sync_Arb_IDs(void)
1049 * Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 And not
1052 if (modern_apic() || boot_cpu_data
.x86_vendor
== X86_VENDOR_AMD
)
1058 apic_wait_icr_idle();
1060 apic_printk(APIC_DEBUG
, "Synchronizing Arb IDs.\n");
1061 apic_write(APIC_ICR
, APIC_DEST_ALLINC
|
1062 APIC_INT_LEVELTRIG
| APIC_DM_INIT
);
1066 * An initial setup of the virtual wire mode.
1068 void __init
init_bsp_APIC(void)
1073 * Don't do the setup now if we have a SMP BIOS as the
1074 * through-I/O-APIC virtual wire mode might be active.
1076 if (smp_found_config
|| !cpu_has_apic
)
1080 * Do not trust the local APIC being empty at bootup.
1087 value
= apic_read(APIC_SPIV
);
1088 value
&= ~APIC_VECTOR_MASK
;
1089 value
|= APIC_SPIV_APIC_ENABLED
;
1091 #ifdef CONFIG_X86_32
1092 /* This bit is reserved on P4/Xeon and should be cleared */
1093 if ((boot_cpu_data
.x86_vendor
== X86_VENDOR_INTEL
) &&
1094 (boot_cpu_data
.x86
== 15))
1095 value
&= ~APIC_SPIV_FOCUS_DISABLED
;
1098 value
|= APIC_SPIV_FOCUS_DISABLED
;
1099 value
|= SPURIOUS_APIC_VECTOR
;
1100 apic_write(APIC_SPIV
, value
);
1103 * Set up the virtual wire mode.
1105 apic_write(APIC_LVT0
, APIC_DM_EXTINT
);
1106 value
= APIC_DM_NMI
;
1107 if (!lapic_is_integrated()) /* 82489DX */
1108 value
|= APIC_LVT_LEVEL_TRIGGER
;
1109 apic_write(APIC_LVT1
, value
);
1112 static void __cpuinit
lapic_setup_esr(void)
1114 unsigned int oldvalue
, value
, maxlvt
;
1116 if (!lapic_is_integrated()) {
1117 pr_info("No ESR for 82489DX.\n");
1121 if (apic
->disable_esr
) {
1123 * Something untraceable is creating bad interrupts on
1124 * secondary quads ... for the moment, just leave the
1125 * ESR disabled - we can't do anything useful with the
1126 * errors anyway - mbligh
1128 pr_info("Leaving ESR disabled.\n");
1132 maxlvt
= lapic_get_maxlvt();
1133 if (maxlvt
> 3) /* Due to the Pentium erratum 3AP. */
1134 apic_write(APIC_ESR
, 0);
1135 oldvalue
= apic_read(APIC_ESR
);
1137 /* enables sending errors */
1138 value
= ERROR_APIC_VECTOR
;
1139 apic_write(APIC_LVTERR
, value
);
1142 * spec says clear errors after enabling vector.
1145 apic_write(APIC_ESR
, 0);
1146 value
= apic_read(APIC_ESR
);
1147 if (value
!= oldvalue
)
1148 apic_printk(APIC_VERBOSE
, "ESR value before enabling "
1149 "vector: 0x%08x after: 0x%08x\n",
1155 * setup_local_APIC - setup the local APIC
1157 void __cpuinit
setup_local_APIC(void)
1163 arch_disable_smp_support();
1167 #ifdef CONFIG_X86_32
1168 /* Pound the ESR really hard over the head with a big hammer - mbligh */
1169 if (lapic_is_integrated() && apic
->disable_esr
) {
1170 apic_write(APIC_ESR
, 0);
1171 apic_write(APIC_ESR
, 0);
1172 apic_write(APIC_ESR
, 0);
1173 apic_write(APIC_ESR
, 0);
1180 * Double-check whether this APIC is really registered.
1181 * This is meaningless in clustered apic mode, so we skip it.
1183 if (!apic
->apic_id_registered())
1187 * Intel recommends to set DFR, LDR and TPR before enabling
1188 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
1189 * document number 292116). So here it goes...
1191 apic
->init_apic_ldr();
1194 * Set Task Priority to 'accept all'. We never change this
1197 value
= apic_read(APIC_TASKPRI
);
1198 value
&= ~APIC_TPRI_MASK
;
1199 apic_write(APIC_TASKPRI
, value
);
1202 * After a crash, we no longer service the interrupts and a pending
1203 * interrupt from previous kernel might still have ISR bit set.
1205 * Most probably by now CPU has serviced that pending interrupt and
1206 * it might not have done the ack_APIC_irq() because it thought,
1207 * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
1208 * does not clear the ISR bit and cpu thinks it has already serivced
1209 * the interrupt. Hence a vector might get locked. It was noticed
1210 * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
1212 for (i
= APIC_ISR_NR
- 1; i
>= 0; i
--) {
1213 value
= apic_read(APIC_ISR
+ i
*0x10);
1214 for (j
= 31; j
>= 0; j
--) {
1221 * Now that we are all set up, enable the APIC
1223 value
= apic_read(APIC_SPIV
);
1224 value
&= ~APIC_VECTOR_MASK
;
1228 value
|= APIC_SPIV_APIC_ENABLED
;
1230 #ifdef CONFIG_X86_32
1232 * Some unknown Intel IO/APIC (or APIC) errata is biting us with
1233 * certain networking cards. If high frequency interrupts are
1234 * happening on a particular IOAPIC pin, plus the IOAPIC routing
1235 * entry is masked/unmasked at a high rate as well then sooner or
1236 * later IOAPIC line gets 'stuck', no more interrupts are received
1237 * from the device. If focus CPU is disabled then the hang goes
1240 * [ This bug can be reproduced easily with a level-triggered
1241 * PCI Ne2000 networking cards and PII/PIII processors, dual
1245 * Actually disabling the focus CPU check just makes the hang less
1246 * frequent as it makes the interrupt distributon model be more
1247 * like LRU than MRU (the short-term load is more even across CPUs).
1248 * See also the comment in end_level_ioapic_irq(). --macro
1252 * - enable focus processor (bit==0)
1253 * - 64bit mode always use processor focus
1254 * so no need to set it
1256 value
&= ~APIC_SPIV_FOCUS_DISABLED
;
1260 * Set spurious IRQ vector
1262 value
|= SPURIOUS_APIC_VECTOR
;
1263 apic_write(APIC_SPIV
, value
);
1266 * Set up LVT0, LVT1:
1268 * set up through-local-APIC on the BP's LINT0. This is not
1269 * strictly necessary in pure symmetric-IO mode, but sometimes
1270 * we delegate interrupts to the 8259A.
1273 * TODO: set up through-local-APIC from through-I/O-APIC? --macro
1275 value
= apic_read(APIC_LVT0
) & APIC_LVT_MASKED
;
1276 if (!smp_processor_id() && (pic_mode
|| !value
)) {
1277 value
= APIC_DM_EXTINT
;
1278 apic_printk(APIC_VERBOSE
, "enabled ExtINT on CPU#%d\n",
1279 smp_processor_id());
1281 value
= APIC_DM_EXTINT
| APIC_LVT_MASKED
;
1282 apic_printk(APIC_VERBOSE
, "masked ExtINT on CPU#%d\n",
1283 smp_processor_id());
1285 apic_write(APIC_LVT0
, value
);
1288 * only the BP should see the LINT1 NMI signal, obviously.
1290 if (!smp_processor_id())
1291 value
= APIC_DM_NMI
;
1293 value
= APIC_DM_NMI
| APIC_LVT_MASKED
;
1294 if (!lapic_is_integrated()) /* 82489DX */
1295 value
|= APIC_LVT_LEVEL_TRIGGER
;
1296 apic_write(APIC_LVT1
, value
);
1301 void __cpuinit
end_local_APIC_setup(void)
1305 #ifdef CONFIG_X86_32
1308 /* Disable the local apic timer */
1309 value
= apic_read(APIC_LVTT
);
1310 value
|= (APIC_LVT_MASKED
| LOCAL_TIMER_VECTOR
);
1311 apic_write(APIC_LVTT
, value
);
1315 setup_apic_nmi_watchdog(NULL
);
1319 #ifdef CONFIG_X86_X2APIC
1320 void check_x2apic(void)
1324 if (!cpu_has_x2apic
)
1327 rdmsr(MSR_IA32_APICBASE
, msr
, msr2
);
1329 if (msr
& X2APIC_ENABLE
) {
1330 pr_info("x2apic enabled by BIOS, switching to x2apic ops\n");
1331 x2apic_preenabled
= x2apic
= 1;
1332 apic_ops
= &x2apic_ops
;
1336 void enable_x2apic(void)
1343 rdmsr(MSR_IA32_APICBASE
, msr
, msr2
);
1344 if (!(msr
& X2APIC_ENABLE
)) {
1345 pr_info("Enabling x2apic\n");
1346 wrmsr(MSR_IA32_APICBASE
, msr
| X2APIC_ENABLE
, 0);
1350 void __init
enable_IR_x2apic(void)
1352 #ifdef CONFIG_INTR_REMAP
1354 unsigned long flags
;
1356 if (!cpu_has_x2apic
)
1359 if (!x2apic_preenabled
&& disable_x2apic
) {
1360 pr_info("Skipped enabling x2apic and Interrupt-remapping "
1361 "because of nox2apic\n");
1365 if (x2apic_preenabled
&& disable_x2apic
)
1366 panic("Bios already enabled x2apic, can't enforce nox2apic");
1368 if (!x2apic_preenabled
&& skip_ioapic_setup
) {
1369 pr_info("Skipped enabling x2apic and Interrupt-remapping "
1370 "because of skipping io-apic setup\n");
1374 ret
= dmar_table_init();
1376 pr_info("dmar_table_init() failed with %d:\n", ret
);
1378 if (x2apic_preenabled
)
1379 panic("x2apic enabled by bios. But IR enabling failed");
1381 pr_info("Not enabling x2apic,Intr-remapping\n");
1385 local_irq_save(flags
);
1388 ret
= save_mask_IO_APIC_setup();
1390 pr_info("Saving IO-APIC state failed: %d\n", ret
);
1394 ret
= enable_intr_remapping(1);
1396 if (ret
&& x2apic_preenabled
) {
1397 local_irq_restore(flags
);
1398 panic("x2apic enabled by bios. But IR enabling failed");
1406 apic_ops
= &x2apic_ops
;
1413 * IR enabling failed
1415 restore_IO_APIC_setup();
1417 reinit_intr_remapped_IO_APIC(x2apic_preenabled
);
1421 local_irq_restore(flags
);
1424 if (!x2apic_preenabled
)
1425 pr_info("Enabled x2apic and interrupt-remapping\n");
1427 pr_info("Enabled Interrupt-remapping\n");
1429 pr_err("Failed to enable Interrupt-remapping and x2apic\n");
1431 if (!cpu_has_x2apic
)
1434 if (x2apic_preenabled
)
1435 panic("x2apic enabled prior OS handover,"
1436 " enable CONFIG_INTR_REMAP");
1438 pr_info("Enable CONFIG_INTR_REMAP for enabling intr-remapping "
1444 #endif /* CONFIG_X86_X2APIC */
1446 #ifdef CONFIG_X86_64
1448 * Detect and enable local APICs on non-SMP boards.
1449 * Original code written by Keir Fraser.
1450 * On AMD64 we trust the BIOS - if it says no APIC it is likely
1451 * not correctly set up (usually the APIC timer won't work etc.)
1453 static int __init
detect_init_APIC(void)
1455 if (!cpu_has_apic
) {
1456 pr_info("No local APIC present\n");
1460 mp_lapic_addr
= APIC_DEFAULT_PHYS_BASE
;
1461 boot_cpu_physical_apicid
= 0;
1466 * Detect and initialize APIC
1468 static int __init
detect_init_APIC(void)
1472 /* Disabled by kernel option? */
1476 switch (boot_cpu_data
.x86_vendor
) {
1477 case X86_VENDOR_AMD
:
1478 if ((boot_cpu_data
.x86
== 6 && boot_cpu_data
.x86_model
> 1) ||
1479 (boot_cpu_data
.x86
>= 15))
1482 case X86_VENDOR_INTEL
:
1483 if (boot_cpu_data
.x86
== 6 || boot_cpu_data
.x86
== 15 ||
1484 (boot_cpu_data
.x86
== 5 && cpu_has_apic
))
1491 if (!cpu_has_apic
) {
1493 * Over-ride BIOS and try to enable the local APIC only if
1494 * "lapic" specified.
1496 if (!force_enable_local_apic
) {
1497 pr_info("Local APIC disabled by BIOS -- "
1498 "you can enable it with \"lapic\"\n");
1502 * Some BIOSes disable the local APIC in the APIC_BASE
1503 * MSR. This can only be done in software for Intel P6 or later
1504 * and AMD K7 (Model > 1) or later.
1506 rdmsr(MSR_IA32_APICBASE
, l
, h
);
1507 if (!(l
& MSR_IA32_APICBASE_ENABLE
)) {
1508 pr_info("Local APIC disabled by BIOS -- reenabling.\n");
1509 l
&= ~MSR_IA32_APICBASE_BASE
;
1510 l
|= MSR_IA32_APICBASE_ENABLE
| APIC_DEFAULT_PHYS_BASE
;
1511 wrmsr(MSR_IA32_APICBASE
, l
, h
);
1512 enabled_via_apicbase
= 1;
1516 * The APIC feature bit should now be enabled
1519 features
= cpuid_edx(1);
1520 if (!(features
& (1 << X86_FEATURE_APIC
))) {
1521 pr_warning("Could not enable APIC!\n");
1524 set_cpu_cap(&boot_cpu_data
, X86_FEATURE_APIC
);
1525 mp_lapic_addr
= APIC_DEFAULT_PHYS_BASE
;
1527 /* The BIOS may have set up the APIC at some other address */
1528 rdmsr(MSR_IA32_APICBASE
, l
, h
);
1529 if (l
& MSR_IA32_APICBASE_ENABLE
)
1530 mp_lapic_addr
= l
& MSR_IA32_APICBASE_BASE
;
1532 pr_info("Found and enabled local APIC!\n");
1539 pr_info("No local APIC present or hardware disabled\n");
1544 #ifdef CONFIG_X86_64
1545 void __init
early_init_lapic_mapping(void)
1547 unsigned long phys_addr
;
1550 * If no local APIC can be found then go out
1551 * : it means there is no mpatable and MADT
1553 if (!smp_found_config
)
1556 phys_addr
= mp_lapic_addr
;
1558 set_fixmap_nocache(FIX_APIC_BASE
, phys_addr
);
1559 apic_printk(APIC_VERBOSE
, "mapped APIC to %16lx (%16lx)\n",
1560 APIC_BASE
, phys_addr
);
1563 * Fetch the APIC ID of the BSP in case we have a
1564 * default configuration (or the MP table is broken).
1566 boot_cpu_physical_apicid
= read_apic_id();
1571 * init_apic_mappings - initialize APIC mappings
1573 void __init
init_apic_mappings(void)
1575 #ifdef CONFIG_X86_X2APIC
1577 boot_cpu_physical_apicid
= read_apic_id();
1583 * If no local APIC can be found then set up a fake all
1584 * zeroes page to simulate the local APIC and another
1585 * one for the IO-APIC.
1587 if (!smp_found_config
&& detect_init_APIC()) {
1588 apic_phys
= (unsigned long) alloc_bootmem_pages(PAGE_SIZE
);
1589 apic_phys
= __pa(apic_phys
);
1591 apic_phys
= mp_lapic_addr
;
1593 set_fixmap_nocache(FIX_APIC_BASE
, apic_phys
);
1594 apic_printk(APIC_VERBOSE
, "mapped APIC to %08lx (%08lx)\n",
1595 APIC_BASE
, apic_phys
);
1598 * Fetch the APIC ID of the BSP in case we have a
1599 * default configuration (or the MP table is broken).
1601 if (boot_cpu_physical_apicid
== -1U)
1602 boot_cpu_physical_apicid
= read_apic_id();
1606 * This initializes the IO-APIC and APIC hardware if this is
1609 int apic_version
[MAX_APICS
];
1611 int __init
APIC_init_uniprocessor(void)
1614 pr_info("Apic disabled\n");
1617 #ifdef CONFIG_X86_64
1618 if (!cpu_has_apic
) {
1620 pr_info("Apic disabled by BIOS\n");
1624 if (!smp_found_config
&& !cpu_has_apic
)
1628 * Complain if the BIOS pretends there is one.
1630 if (!cpu_has_apic
&&
1631 APIC_INTEGRATED(apic_version
[boot_cpu_physical_apicid
])) {
1632 pr_err("BIOS bug, local APIC 0x%x not detected!...\n",
1633 boot_cpu_physical_apicid
);
1634 clear_cpu_cap(&boot_cpu_data
, X86_FEATURE_APIC
);
1640 #ifdef CONFIG_X86_64
1641 default_setup_apic_routing();
1644 verify_local_APIC();
1647 #ifdef CONFIG_X86_64
1648 apic_write(APIC_ID
, SET_APIC_ID(boot_cpu_physical_apicid
));
1651 * Hack: In case of kdump, after a crash, kernel might be booting
1652 * on a cpu with non-zero lapic id. But boot_cpu_physical_apicid
1653 * might be zero if read from MP tables. Get it from LAPIC.
1655 # ifdef CONFIG_CRASH_DUMP
1656 boot_cpu_physical_apicid
= read_apic_id();
1659 physid_set_mask_of_physid(boot_cpu_physical_apicid
, &phys_cpu_present_map
);
1662 #ifdef CONFIG_X86_IO_APIC
1664 * Now enable IO-APICs, actually call clear_IO_APIC
1665 * We need clear_IO_APIC before enabling error vector
1667 if (!skip_ioapic_setup
&& nr_ioapics
)
1671 end_local_APIC_setup();
1673 #ifdef CONFIG_X86_IO_APIC
1674 if (smp_found_config
&& !skip_ioapic_setup
&& nr_ioapics
)
1678 localise_nmi_watchdog();
1681 localise_nmi_watchdog();
1685 #ifdef CONFIG_X86_64
1686 check_nmi_watchdog();
1693 * Local APIC interrupts
1697 * This interrupt should _never_ happen with our APIC/SMP architecture
1699 void smp_spurious_interrupt(struct pt_regs
*regs
)
1706 * Check if this really is a spurious interrupt and ACK it
1707 * if it is a vectored one. Just in case...
1708 * Spurious interrupts should not be ACKed.
1710 v
= apic_read(APIC_ISR
+ ((SPURIOUS_APIC_VECTOR
& ~0x1f) >> 1));
1711 if (v
& (1 << (SPURIOUS_APIC_VECTOR
& 0x1f)))
1714 inc_irq_stat(irq_spurious_count
);
1716 /* see sw-dev-man vol 3, chapter 7.4.13.5 */
1717 pr_info("spurious APIC interrupt on CPU#%d, "
1718 "should never happen.\n", smp_processor_id());
1723 * This interrupt should never happen with our APIC/SMP architecture
1725 void smp_error_interrupt(struct pt_regs
*regs
)
1731 /* First tickle the hardware, only then report what went on. -- REW */
1732 v
= apic_read(APIC_ESR
);
1733 apic_write(APIC_ESR
, 0);
1734 v1
= apic_read(APIC_ESR
);
1736 atomic_inc(&irq_err_count
);
1739 * Here is what the APIC error bits mean:
1741 * 1: Receive CS error
1742 * 2: Send accept error
1743 * 3: Receive accept error
1745 * 5: Send illegal vector
1746 * 6: Received illegal vector
1747 * 7: Illegal register address
1749 pr_debug("APIC error on CPU%d: %02x(%02x)\n",
1750 smp_processor_id(), v
, v1
);
1755 * connect_bsp_APIC - attach the APIC to the interrupt system
1757 void __init
connect_bsp_APIC(void)
1759 #ifdef CONFIG_X86_32
1762 * Do not trust the local APIC being empty at bootup.
1766 * PIC mode, enable APIC mode in the IMCR, i.e. connect BSP's
1767 * local APIC to INT and NMI lines.
1769 apic_printk(APIC_VERBOSE
, "leaving PIC mode, "
1770 "enabling APIC mode.\n");
1775 if (apic
->enable_apic_mode
)
1776 apic
->enable_apic_mode();
1780 * disconnect_bsp_APIC - detach the APIC from the interrupt system
1781 * @virt_wire_setup: indicates, whether virtual wire mode is selected
1783 * Virtual wire mode is necessary to deliver legacy interrupts even when the
1786 void disconnect_bsp_APIC(int virt_wire_setup
)
1790 #ifdef CONFIG_X86_32
1793 * Put the board back into PIC mode (has an effect only on
1794 * certain older boards). Note that APIC interrupts, including
1795 * IPIs, won't work beyond this point! The only exception are
1798 apic_printk(APIC_VERBOSE
, "disabling APIC mode, "
1799 "entering PIC mode.\n");
1806 /* Go back to Virtual Wire compatibility mode */
1808 /* For the spurious interrupt use vector F, and enable it */
1809 value
= apic_read(APIC_SPIV
);
1810 value
&= ~APIC_VECTOR_MASK
;
1811 value
|= APIC_SPIV_APIC_ENABLED
;
1813 apic_write(APIC_SPIV
, value
);
1815 if (!virt_wire_setup
) {
1817 * For LVT0 make it edge triggered, active high,
1818 * external and enabled
1820 value
= apic_read(APIC_LVT0
);
1821 value
&= ~(APIC_MODE_MASK
| APIC_SEND_PENDING
|
1822 APIC_INPUT_POLARITY
| APIC_LVT_REMOTE_IRR
|
1823 APIC_LVT_LEVEL_TRIGGER
| APIC_LVT_MASKED
);
1824 value
|= APIC_LVT_REMOTE_IRR
| APIC_SEND_PENDING
;
1825 value
= SET_APIC_DELIVERY_MODE(value
, APIC_MODE_EXTINT
);
1826 apic_write(APIC_LVT0
, value
);
1829 apic_write(APIC_LVT0
, APIC_LVT_MASKED
);
1833 * For LVT1 make it edge triggered, active high,
1836 value
= apic_read(APIC_LVT1
);
1837 value
&= ~(APIC_MODE_MASK
| APIC_SEND_PENDING
|
1838 APIC_INPUT_POLARITY
| APIC_LVT_REMOTE_IRR
|
1839 APIC_LVT_LEVEL_TRIGGER
| APIC_LVT_MASKED
);
1840 value
|= APIC_LVT_REMOTE_IRR
| APIC_SEND_PENDING
;
1841 value
= SET_APIC_DELIVERY_MODE(value
, APIC_MODE_NMI
);
1842 apic_write(APIC_LVT1
, value
);
1845 void __cpuinit
generic_processor_info(int apicid
, int version
)
1852 if (version
== 0x0) {
1853 pr_warning("BIOS bug, APIC version is 0 for CPU#%d! "
1854 "fixing up to 0x10. (tell your hw vendor)\n",
1858 apic_version
[apicid
] = version
;
1860 if (num_processors
>= nr_cpu_ids
) {
1861 int max
= nr_cpu_ids
;
1862 int thiscpu
= max
+ disabled_cpus
;
1865 "ACPI: NR_CPUS/possible_cpus limit of %i reached."
1866 " Processor %d/0x%x ignored.\n", max
, thiscpu
, apicid
);
1873 cpu
= cpumask_next_zero(-1, cpu_present_mask
);
1875 if (version
!= apic_version
[boot_cpu_physical_apicid
])
1877 "ACPI: apic version mismatch, bootcpu: %x cpu %d: %x\n",
1878 apic_version
[boot_cpu_physical_apicid
], cpu
, version
);
1880 physid_set(apicid
, phys_cpu_present_map
);
1881 if (apicid
== boot_cpu_physical_apicid
) {
1883 * x86_bios_cpu_apicid is required to have processors listed
1884 * in same order as logical cpu numbers. Hence the first
1885 * entry is BSP, and so on.
1889 if (apicid
> max_physical_apicid
)
1890 max_physical_apicid
= apicid
;
1892 #ifdef CONFIG_X86_32
1894 * Would be preferable to switch to bigsmp when CONFIG_HOTPLUG_CPU=y
1895 * but we need to work other dependencies like SMP_SUSPEND etc
1896 * before this can be done without some confusion.
1897 * if (CPU_HOTPLUG_ENABLED || num_processors > 8)
1898 * - Ashok Raj <ashok.raj@intel.com>
1900 if (max_physical_apicid
>= 8) {
1901 switch (boot_cpu_data
.x86_vendor
) {
1902 case X86_VENDOR_INTEL
:
1903 if (!APIC_XAPIC(version
)) {
1907 /* If P4 and above fall through */
1908 case X86_VENDOR_AMD
:
1914 #if defined(CONFIG_SMP) || defined(CONFIG_X86_64)
1915 early_per_cpu(x86_cpu_to_apicid
, cpu
) = apicid
;
1916 early_per_cpu(x86_bios_cpu_apicid
, cpu
) = apicid
;
1919 set_cpu_possible(cpu
, true);
1920 set_cpu_present(cpu
, true);
1923 int hard_smp_processor_id(void)
1925 return read_apic_id();
1928 void default_init_apic_ldr(void)
1932 apic_write(APIC_DFR
, APIC_DFR_VALUE
);
1933 val
= apic_read(APIC_LDR
) & ~APIC_LDR_MASK
;
1934 val
|= SET_APIC_LOGICAL_ID(1UL << smp_processor_id());
1935 apic_write(APIC_LDR
, val
);
1938 #ifdef CONFIG_X86_32
1939 int default_apicid_to_node(int logical_apicid
)
1942 return apicid_2_node
[hard_smp_processor_id()];
1956 * 'active' is true if the local APIC was enabled by us and
1957 * not the BIOS; this signifies that we are also responsible
1958 * for disabling it before entering apm/acpi suspend
1961 /* r/w apic fields */
1962 unsigned int apic_id
;
1963 unsigned int apic_taskpri
;
1964 unsigned int apic_ldr
;
1965 unsigned int apic_dfr
;
1966 unsigned int apic_spiv
;
1967 unsigned int apic_lvtt
;
1968 unsigned int apic_lvtpc
;
1969 unsigned int apic_lvt0
;
1970 unsigned int apic_lvt1
;
1971 unsigned int apic_lvterr
;
1972 unsigned int apic_tmict
;
1973 unsigned int apic_tdcr
;
1974 unsigned int apic_thmr
;
1977 static int lapic_suspend(struct sys_device
*dev
, pm_message_t state
)
1979 unsigned long flags
;
1982 if (!apic_pm_state
.active
)
1985 maxlvt
= lapic_get_maxlvt();
1987 apic_pm_state
.apic_id
= apic_read(APIC_ID
);
1988 apic_pm_state
.apic_taskpri
= apic_read(APIC_TASKPRI
);
1989 apic_pm_state
.apic_ldr
= apic_read(APIC_LDR
);
1990 apic_pm_state
.apic_dfr
= apic_read(APIC_DFR
);
1991 apic_pm_state
.apic_spiv
= apic_read(APIC_SPIV
);
1992 apic_pm_state
.apic_lvtt
= apic_read(APIC_LVTT
);
1994 apic_pm_state
.apic_lvtpc
= apic_read(APIC_LVTPC
);
1995 apic_pm_state
.apic_lvt0
= apic_read(APIC_LVT0
);
1996 apic_pm_state
.apic_lvt1
= apic_read(APIC_LVT1
);
1997 apic_pm_state
.apic_lvterr
= apic_read(APIC_LVTERR
);
1998 apic_pm_state
.apic_tmict
= apic_read(APIC_TMICT
);
1999 apic_pm_state
.apic_tdcr
= apic_read(APIC_TDCR
);
2000 #if defined(CONFIG_X86_MCE_P4THERMAL) || defined(CONFIG_X86_MCE_INTEL)
2002 apic_pm_state
.apic_thmr
= apic_read(APIC_LVTTHMR
);
2005 local_irq_save(flags
);
2006 disable_local_APIC();
2007 local_irq_restore(flags
);
2011 static int lapic_resume(struct sys_device
*dev
)
2014 unsigned long flags
;
2017 if (!apic_pm_state
.active
)
2020 maxlvt
= lapic_get_maxlvt();
2022 local_irq_save(flags
);
2024 #ifdef CONFIG_X86_X2APIC
2031 * Make sure the APICBASE points to the right address
2033 * FIXME! This will be wrong if we ever support suspend on
2034 * SMP! We'll need to do this as part of the CPU restore!
2036 rdmsr(MSR_IA32_APICBASE
, l
, h
);
2037 l
&= ~MSR_IA32_APICBASE_BASE
;
2038 l
|= MSR_IA32_APICBASE_ENABLE
| mp_lapic_addr
;
2039 wrmsr(MSR_IA32_APICBASE
, l
, h
);
2042 apic_write(APIC_LVTERR
, ERROR_APIC_VECTOR
| APIC_LVT_MASKED
);
2043 apic_write(APIC_ID
, apic_pm_state
.apic_id
);
2044 apic_write(APIC_DFR
, apic_pm_state
.apic_dfr
);
2045 apic_write(APIC_LDR
, apic_pm_state
.apic_ldr
);
2046 apic_write(APIC_TASKPRI
, apic_pm_state
.apic_taskpri
);
2047 apic_write(APIC_SPIV
, apic_pm_state
.apic_spiv
);
2048 apic_write(APIC_LVT0
, apic_pm_state
.apic_lvt0
);
2049 apic_write(APIC_LVT1
, apic_pm_state
.apic_lvt1
);
2050 #if defined(CONFIG_X86_MCE_P4THERMAL) || defined(CONFIG_X86_MCE_INTEL)
2052 apic_write(APIC_LVTTHMR
, apic_pm_state
.apic_thmr
);
2055 apic_write(APIC_LVTPC
, apic_pm_state
.apic_lvtpc
);
2056 apic_write(APIC_LVTT
, apic_pm_state
.apic_lvtt
);
2057 apic_write(APIC_TDCR
, apic_pm_state
.apic_tdcr
);
2058 apic_write(APIC_TMICT
, apic_pm_state
.apic_tmict
);
2059 apic_write(APIC_ESR
, 0);
2060 apic_read(APIC_ESR
);
2061 apic_write(APIC_LVTERR
, apic_pm_state
.apic_lvterr
);
2062 apic_write(APIC_ESR
, 0);
2063 apic_read(APIC_ESR
);
2065 local_irq_restore(flags
);
2071 * This device has no shutdown method - fully functioning local APICs
2072 * are needed on every CPU up until machine_halt/restart/poweroff.
2075 static struct sysdev_class lapic_sysclass
= {
2077 .resume
= lapic_resume
,
2078 .suspend
= lapic_suspend
,
2081 static struct sys_device device_lapic
= {
2083 .cls
= &lapic_sysclass
,
2086 static void __cpuinit
apic_pm_activate(void)
2088 apic_pm_state
.active
= 1;
2091 static int __init
init_lapic_sysfs(void)
2097 /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
2099 error
= sysdev_class_register(&lapic_sysclass
);
2101 error
= sysdev_register(&device_lapic
);
2104 device_initcall(init_lapic_sysfs
);
2106 #else /* CONFIG_PM */
2108 static void apic_pm_activate(void) { }
2110 #endif /* CONFIG_PM */
2112 #ifdef CONFIG_X86_64
2114 * apic_is_clustered_box() -- Check if we can expect good TSC
2116 * Thus far, the major user of this is IBM's Summit2 series:
2118 * Clustered boxes may have unsynced TSC problems if they are
2119 * multi-chassis. Use available data to take a good guess.
2120 * If in doubt, go HPET.
2122 __cpuinit
int apic_is_clustered_box(void)
2124 int i
, clusters
, zeros
;
2126 u16
*bios_cpu_apicid
;
2127 DECLARE_BITMAP(clustermap
, NUM_APIC_CLUSTERS
);
2130 * there is not this kind of box with AMD CPU yet.
2131 * Some AMD box with quadcore cpu and 8 sockets apicid
2132 * will be [4, 0x23] or [8, 0x27] could be thought to
2133 * vsmp box still need checking...
2135 if ((boot_cpu_data
.x86_vendor
== X86_VENDOR_AMD
) && !is_vsmp_box())
2138 bios_cpu_apicid
= early_per_cpu_ptr(x86_bios_cpu_apicid
);
2139 bitmap_zero(clustermap
, NUM_APIC_CLUSTERS
);
2141 for (i
= 0; i
< nr_cpu_ids
; i
++) {
2142 /* are we being called early in kernel startup? */
2143 if (bios_cpu_apicid
) {
2144 id
= bios_cpu_apicid
[i
];
2145 } else if (i
< nr_cpu_ids
) {
2147 id
= per_cpu(x86_bios_cpu_apicid
, i
);
2153 if (id
!= BAD_APICID
)
2154 __set_bit(APIC_CLUSTERID(id
), clustermap
);
2157 /* Problem: Partially populated chassis may not have CPUs in some of
2158 * the APIC clusters they have been allocated. Only present CPUs have
2159 * x86_bios_cpu_apicid entries, thus causing zeroes in the bitmap.
2160 * Since clusters are allocated sequentially, count zeros only if
2161 * they are bounded by ones.
2165 for (i
= 0; i
< NUM_APIC_CLUSTERS
; i
++) {
2166 if (test_bit(i
, clustermap
)) {
2167 clusters
+= 1 + zeros
;
2173 /* ScaleMP vSMPowered boxes have one cluster per board and TSCs are
2174 * not guaranteed to be synced between boards
2176 if (is_vsmp_box() && clusters
> 1)
2180 * If clusters > 2, then should be multi-chassis.
2181 * May have to revisit this when multi-core + hyperthreaded CPUs come
2182 * out, but AFAIK this will work even for them.
2184 return (clusters
> 2);
2189 * APIC command line parameters
2191 static int __init
setup_disableapic(char *arg
)
2194 setup_clear_cpu_cap(X86_FEATURE_APIC
);
2197 early_param("disableapic", setup_disableapic
);
2199 /* same as disableapic, for compatibility */
2200 static int __init
setup_nolapic(char *arg
)
2202 return setup_disableapic(arg
);
2204 early_param("nolapic", setup_nolapic
);
2206 static int __init
parse_lapic_timer_c2_ok(char *arg
)
2208 local_apic_timer_c2_ok
= 1;
2211 early_param("lapic_timer_c2_ok", parse_lapic_timer_c2_ok
);
2213 static int __init
parse_disable_apic_timer(char *arg
)
2215 disable_apic_timer
= 1;
2218 early_param("noapictimer", parse_disable_apic_timer
);
2220 static int __init
parse_nolapic_timer(char *arg
)
2222 disable_apic_timer
= 1;
2225 early_param("nolapic_timer", parse_nolapic_timer
);
2227 static int __init
apic_set_verbosity(char *arg
)
2230 #ifdef CONFIG_X86_64
2231 skip_ioapic_setup
= 0;
2237 if (strcmp("debug", arg
) == 0)
2238 apic_verbosity
= APIC_DEBUG
;
2239 else if (strcmp("verbose", arg
) == 0)
2240 apic_verbosity
= APIC_VERBOSE
;
2242 pr_warning("APIC Verbosity level %s not recognised"
2243 " use apic=verbose or apic=debug\n", arg
);
2249 early_param("apic", apic_set_verbosity
);
2251 static int __init
lapic_insert_resource(void)
2256 /* Put local APIC into the resource map. */
2257 lapic_resource
.start
= apic_phys
;
2258 lapic_resource
.end
= lapic_resource
.start
+ PAGE_SIZE
- 1;
2259 insert_resource(&iomem_resource
, &lapic_resource
);
2265 * need call insert after e820_reserve_resources()
2266 * that is using request_resource
2268 late_initcall(lapic_insert_resource
);