2 * Local APIC handling, local APIC timers
4 * (c) 1999, 2000 Ingo Molnar <mingo@redhat.com>
7 * Maciej W. Rozycki : Bits for genuine 82489DX APICs;
8 * thanks to Eric Gilmore
10 * for testing these extensively.
11 * Maciej W. Rozycki : Various updates and fixes.
12 * Mikael Pettersson : Power Management for UP-APIC.
14 * Mikael Pettersson : PM converted to driver model.
17 #include <linux/init.h>
20 #include <linux/delay.h>
21 #include <linux/bootmem.h>
22 #include <linux/interrupt.h>
23 #include <linux/mc146818rtc.h>
24 #include <linux/kernel_stat.h>
25 #include <linux/sysdev.h>
26 #include <linux/cpu.h>
27 #include <linux/clockchips.h>
28 #include <linux/acpi_pmtmr.h>
29 #include <linux/module.h>
30 #include <linux/dmi.h>
32 #include <asm/atomic.h>
35 #include <asm/mpspec.h>
37 #include <asm/arch_hooks.h>
39 #include <asm/i8253.h>
42 #include <mach_apic.h>
43 #include <mach_apicdef.h>
51 #if (SPURIOUS_APIC_VECTOR & 0x0F) != 0x0F
52 # error SPURIOUS_APIC_VECTOR definition error
56 * Knob to control our willingness to enable the local APIC.
58 * -1=force-disable, +1=force-enable
60 static int enable_local_apic __initdata
= 0;
62 /* Local APIC timer verification ok */
63 static int local_apic_timer_verify_ok
;
64 /* Disable local APIC timer from the kernel commandline or via dmi quirk
65 or using CPU MSR check */
66 int local_apic_timer_disabled
;
67 /* Local APIC timer works in C2 */
68 int local_apic_timer_c2_ok
;
69 EXPORT_SYMBOL_GPL(local_apic_timer_c2_ok
);
72 * Debug level, exported for io_apic.c
76 static unsigned int calibration_result
;
78 static int lapic_next_event(unsigned long delta
,
79 struct clock_event_device
*evt
);
80 static void lapic_timer_setup(enum clock_event_mode mode
,
81 struct clock_event_device
*evt
);
82 static void lapic_timer_broadcast(cpumask_t mask
);
83 static void apic_pm_activate(void);
86 * The local apic timer can be used for any function which is CPU local.
88 static struct clock_event_device lapic_clockevent
= {
90 .features
= CLOCK_EVT_FEAT_PERIODIC
| CLOCK_EVT_FEAT_ONESHOT
91 | CLOCK_EVT_FEAT_C3STOP
| CLOCK_EVT_FEAT_DUMMY
,
93 .set_mode
= lapic_timer_setup
,
94 .set_next_event
= lapic_next_event
,
95 .broadcast
= lapic_timer_broadcast
,
99 static DEFINE_PER_CPU(struct clock_event_device
, lapic_events
);
101 /* Local APIC was disabled by the BIOS and enabled by the kernel */
102 static int enabled_via_apicbase
;
105 * Get the LAPIC version
107 static inline int lapic_get_version(void)
109 return GET_APIC_VERSION(apic_read(APIC_LVR
));
113 * Check, if the APIC is integrated or a seperate chip
115 static inline int lapic_is_integrated(void)
117 return APIC_INTEGRATED(lapic_get_version());
121 * Check, whether this is a modern or a first generation APIC
123 static int modern_apic(void)
125 /* AMD systems use old APIC versions, so check the CPU */
126 if (boot_cpu_data
.x86_vendor
== X86_VENDOR_AMD
&&
127 boot_cpu_data
.x86
>= 0xf)
129 return lapic_get_version() >= 0x14;
132 void apic_wait_icr_idle(void)
134 while (apic_read(APIC_ICR
) & APIC_ICR_BUSY
)
138 unsigned long safe_apic_wait_icr_idle(void)
140 unsigned long send_status
;
145 send_status
= apic_read(APIC_ICR
) & APIC_ICR_BUSY
;
149 } while (timeout
++ < 1000);
155 * enable_NMI_through_LVT0 - enable NMI through local vector table 0
157 void enable_NMI_through_LVT0 (void * dummy
)
159 unsigned int v
= APIC_DM_NMI
;
161 /* Level triggered for 82489DX */
162 if (!lapic_is_integrated())
163 v
|= APIC_LVT_LEVEL_TRIGGER
;
164 apic_write_around(APIC_LVT0
, v
);
168 * get_physical_broadcast - Get number of physical broadcast IDs
170 int get_physical_broadcast(void)
172 return modern_apic() ? 0xff : 0xf;
176 * lapic_get_maxlvt - get the maximum number of local vector table entries
178 int lapic_get_maxlvt(void)
180 unsigned int v
= apic_read(APIC_LVR
);
182 /* 82489DXs do not report # of LVT entries. */
183 return APIC_INTEGRATED(GET_APIC_VERSION(v
)) ? GET_APIC_MAXLVT(v
) : 2;
190 /* Clock divisor is set to 16 */
191 #define APIC_DIVISOR 16
194 * This function sets up the local APIC timer, with a timeout of
195 * 'clocks' APIC bus clock. During calibration we actually call
196 * this function twice on the boot CPU, once with a bogus timeout
197 * value, second time for real. The other (noncalibrating) CPUs
198 * call this function only once, with the real, calibrated value.
200 * We do reads before writes even if unnecessary, to get around the
201 * P5 APIC double write bug.
203 static void __setup_APIC_LVTT(unsigned int clocks
, int oneshot
, int irqen
)
205 unsigned int lvtt_value
, tmp_value
;
207 lvtt_value
= LOCAL_TIMER_VECTOR
;
209 lvtt_value
|= APIC_LVT_TIMER_PERIODIC
;
210 if (!lapic_is_integrated())
211 lvtt_value
|= SET_APIC_TIMER_BASE(APIC_TIMER_BASE_DIV
);
214 lvtt_value
|= APIC_LVT_MASKED
;
216 apic_write_around(APIC_LVTT
, lvtt_value
);
221 tmp_value
= apic_read(APIC_TDCR
);
222 apic_write_around(APIC_TDCR
, (tmp_value
223 & ~(APIC_TDR_DIV_1
| APIC_TDR_DIV_TMBASE
))
227 apic_write_around(APIC_TMICT
, clocks
/APIC_DIVISOR
);
231 * Program the next event, relative to now
233 static int lapic_next_event(unsigned long delta
,
234 struct clock_event_device
*evt
)
236 apic_write_around(APIC_TMICT
, delta
);
241 * Setup the lapic timer in periodic or oneshot mode
243 static void lapic_timer_setup(enum clock_event_mode mode
,
244 struct clock_event_device
*evt
)
249 /* Lapic used for broadcast ? */
250 if (!local_apic_timer_verify_ok
)
253 local_irq_save(flags
);
256 case CLOCK_EVT_MODE_PERIODIC
:
257 case CLOCK_EVT_MODE_ONESHOT
:
258 __setup_APIC_LVTT(calibration_result
,
259 mode
!= CLOCK_EVT_MODE_PERIODIC
, 1);
261 case CLOCK_EVT_MODE_UNUSED
:
262 case CLOCK_EVT_MODE_SHUTDOWN
:
263 v
= apic_read(APIC_LVTT
);
264 v
|= (APIC_LVT_MASKED
| LOCAL_TIMER_VECTOR
);
265 apic_write_around(APIC_LVTT
, v
);
269 local_irq_restore(flags
);
273 * Local APIC timer broadcast function
275 static void lapic_timer_broadcast(cpumask_t mask
)
278 send_IPI_mask(mask
, LOCAL_TIMER_VECTOR
);
283 * Setup the local APIC timer for this CPU. Copy the initilized values
284 * of the boot CPU and register the clock event in the framework.
286 static void __devinit
setup_APIC_timer(void)
288 struct clock_event_device
*levt
= &__get_cpu_var(lapic_events
);
290 memcpy(levt
, &lapic_clockevent
, sizeof(*levt
));
291 levt
->cpumask
= cpumask_of_cpu(smp_processor_id());
293 clockevents_register_device(levt
);
297 * In this functions we calibrate APIC bus clocks to the external timer.
299 * We want to do the calibration only once since we want to have local timer
300 * irqs syncron. CPUs connected by the same APIC bus have the very same bus
303 * This was previously done by reading the PIT/HPET and waiting for a wrap
304 * around to find out, that a tick has elapsed. I have a box, where the PIT
305 * readout is broken, so it never gets out of the wait loop again. This was
306 * also reported by others.
308 * Monitoring the jiffies value is inaccurate and the clockevents
309 * infrastructure allows us to do a simple substitution of the interrupt
312 * The calibration routine also uses the pm_timer when possible, as the PIT
313 * happens to run way too slow (factor 2.3 on my VAIO CoreDuo, which goes
314 * back to normal later in the boot process).
317 #define LAPIC_CAL_LOOPS (HZ/10)
319 static __initdata
volatile int lapic_cal_loops
= -1;
320 static __initdata
long lapic_cal_t1
, lapic_cal_t2
;
321 static __initdata
unsigned long long lapic_cal_tsc1
, lapic_cal_tsc2
;
322 static __initdata
unsigned long lapic_cal_pm1
, lapic_cal_pm2
;
323 static __initdata
unsigned long lapic_cal_j1
, lapic_cal_j2
;
326 * Temporary interrupt handler.
328 static void __init
lapic_cal_handler(struct clock_event_device
*dev
)
330 unsigned long long tsc
= 0;
331 long tapic
= apic_read(APIC_TMCCT
);
332 unsigned long pm
= acpi_pm_read_early();
337 switch (lapic_cal_loops
++) {
339 lapic_cal_t1
= tapic
;
340 lapic_cal_tsc1
= tsc
;
342 lapic_cal_j1
= jiffies
;
345 case LAPIC_CAL_LOOPS
:
346 lapic_cal_t2
= tapic
;
347 lapic_cal_tsc2
= tsc
;
348 if (pm
< lapic_cal_pm1
)
349 pm
+= ACPI_PM_OVRRUN
;
351 lapic_cal_j2
= jiffies
;
357 * Setup the boot APIC
359 * Calibrate and verify the result.
361 void __init
setup_boot_APIC_clock(void)
363 struct clock_event_device
*levt
= &__get_cpu_var(lapic_events
);
364 const long pm_100ms
= PMTMR_TICKS_PER_SEC
/10;
365 const long pm_thresh
= pm_100ms
/100;
366 void (*real_handler
)(struct clock_event_device
*dev
);
367 unsigned long deltaj
;
369 int pm_referenced
= 0;
372 * The local apic timer can be disabled via the kernel
373 * commandline or from the CPU detection code. Register the lapic
374 * timer as a dummy clock event source on SMP systems, so the
375 * broadcast mechanism is used. On UP systems simply ignore it.
377 if (local_apic_timer_disabled
) {
378 /* No broadcast on UP ! */
379 if (num_possible_cpus() > 1)
384 apic_printk(APIC_VERBOSE
, "Using local APIC timer interrupts.\n"
385 "calibrating APIC timer ...\n");
389 /* Replace the global interrupt handler */
390 real_handler
= global_clock_event
->event_handler
;
391 global_clock_event
->event_handler
= lapic_cal_handler
;
394 * Setup the APIC counter to 1e9. There is no way the lapic
395 * can underflow in the 100ms detection time frame
397 __setup_APIC_LVTT(1000000000, 0, 0);
399 /* Let the interrupts run */
402 while (lapic_cal_loops
<= LAPIC_CAL_LOOPS
)
407 /* Restore the real event handler */
408 global_clock_event
->event_handler
= real_handler
;
410 /* Build delta t1-t2 as apic timer counts down */
411 delta
= lapic_cal_t1
- lapic_cal_t2
;
412 apic_printk(APIC_VERBOSE
, "... lapic delta = %ld\n", delta
);
414 /* Check, if the PM timer is available */
415 deltapm
= lapic_cal_pm2
- lapic_cal_pm1
;
416 apic_printk(APIC_VERBOSE
, "... PM timer delta = %ld\n", deltapm
);
422 mult
= clocksource_hz2mult(PMTMR_TICKS_PER_SEC
, 22);
424 if (deltapm
> (pm_100ms
- pm_thresh
) &&
425 deltapm
< (pm_100ms
+ pm_thresh
)) {
426 apic_printk(APIC_VERBOSE
, "... PM timer result ok\n");
428 res
= (((u64
) deltapm
) * mult
) >> 22;
429 do_div(res
, 1000000);
430 printk(KERN_WARNING
"APIC calibration not consistent "
431 "with PM Timer: %ldms instead of 100ms\n",
433 /* Correct the lapic counter value */
434 res
= (((u64
) delta
) * pm_100ms
);
435 do_div(res
, deltapm
);
436 printk(KERN_INFO
"APIC delta adjusted to PM-Timer: "
437 "%lu (%ld)\n", (unsigned long) res
, delta
);
443 /* Calculate the scaled math multiplication factor */
444 lapic_clockevent
.mult
= div_sc(delta
, TICK_NSEC
* LAPIC_CAL_LOOPS
, 32);
445 lapic_clockevent
.max_delta_ns
=
446 clockevent_delta2ns(0x7FFFFF, &lapic_clockevent
);
447 lapic_clockevent
.min_delta_ns
=
448 clockevent_delta2ns(0xF, &lapic_clockevent
);
450 calibration_result
= (delta
* APIC_DIVISOR
) / LAPIC_CAL_LOOPS
;
452 apic_printk(APIC_VERBOSE
, "..... delta %ld\n", delta
);
453 apic_printk(APIC_VERBOSE
, "..... mult: %ld\n", lapic_clockevent
.mult
);
454 apic_printk(APIC_VERBOSE
, "..... calibration result: %u\n",
458 delta
= (long)(lapic_cal_tsc2
- lapic_cal_tsc1
);
459 apic_printk(APIC_VERBOSE
, "..... CPU clock speed is "
461 (delta
/ LAPIC_CAL_LOOPS
) / (1000000 / HZ
),
462 (delta
/ LAPIC_CAL_LOOPS
) % (1000000 / HZ
));
465 apic_printk(APIC_VERBOSE
, "..... host bus clock speed is "
467 calibration_result
/ (1000000 / HZ
),
468 calibration_result
% (1000000 / HZ
));
470 local_apic_timer_verify_ok
= 1;
472 /* We trust the pm timer based calibration */
473 if (!pm_referenced
) {
474 apic_printk(APIC_VERBOSE
, "... verify APIC timer\n");
477 * Setup the apic timer manually
479 levt
->event_handler
= lapic_cal_handler
;
480 lapic_timer_setup(CLOCK_EVT_MODE_PERIODIC
, levt
);
481 lapic_cal_loops
= -1;
483 /* Let the interrupts run */
486 while(lapic_cal_loops
<= LAPIC_CAL_LOOPS
)
491 /* Stop the lapic timer */
492 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN
, levt
);
497 deltaj
= lapic_cal_j2
- lapic_cal_j1
;
498 apic_printk(APIC_VERBOSE
, "... jiffies delta = %lu\n", deltaj
);
500 /* Check, if the jiffies result is consistent */
501 if (deltaj
>= LAPIC_CAL_LOOPS
-2 && deltaj
<= LAPIC_CAL_LOOPS
+2)
502 apic_printk(APIC_VERBOSE
, "... jiffies result ok\n");
504 local_apic_timer_verify_ok
= 0;
508 if (!local_apic_timer_verify_ok
) {
510 "APIC timer disabled due to verification failure.\n");
511 /* No broadcast on UP ! */
512 if (num_possible_cpus() == 1)
516 * If nmi_watchdog is set to IO_APIC, we need the
517 * PIT/HPET going. Otherwise register lapic as a dummy
520 if (nmi_watchdog
!= NMI_IO_APIC
)
521 lapic_clockevent
.features
&= ~CLOCK_EVT_FEAT_DUMMY
;
524 /* Setup the lapic or request the broadcast */
528 void __devinit
setup_secondary_APIC_clock(void)
534 * The guts of the apic timer interrupt
536 static void local_apic_timer_interrupt(void)
538 int cpu
= smp_processor_id();
539 struct clock_event_device
*evt
= &per_cpu(lapic_events
, cpu
);
542 * Normally we should not be here till LAPIC has been initialized but
543 * in some cases like kdump, its possible that there is a pending LAPIC
544 * timer interrupt from previous kernel's context and is delivered in
545 * new kernel the moment interrupts are enabled.
547 * Interrupts are enabled early and LAPIC is setup much later, hence
548 * its possible that when we get here evt->event_handler is NULL.
549 * Check for event_handler being NULL and discard the interrupt as
552 if (!evt
->event_handler
) {
554 "Spurious LAPIC timer interrupt on cpu %d\n", cpu
);
556 lapic_timer_setup(CLOCK_EVT_MODE_SHUTDOWN
, evt
);
560 per_cpu(irq_stat
, cpu
).apic_timer_irqs
++;
562 evt
->event_handler(evt
);
566 * Local APIC timer interrupt. This is the most natural way for doing
567 * local interrupts, but local timer interrupts can be emulated by
568 * broadcast interrupts too. [in case the hw doesn't support APIC timers]
570 * [ if a single-CPU system runs an SMP kernel then we call the local
571 * interrupt as well. Thus we cannot inline the local irq ... ]
574 void fastcall
smp_apic_timer_interrupt(struct pt_regs
*regs
)
576 struct pt_regs
*old_regs
= set_irq_regs(regs
);
579 * NOTE! We'd better ACK the irq immediately,
580 * because timer handling can be slow.
584 * update_process_times() expects us to have done irq_enter().
585 * Besides, if we don't timer interrupts ignore the global
586 * interrupt lock, which is the WrongThing (tm) to do.
589 local_apic_timer_interrupt();
592 set_irq_regs(old_regs
);
595 int setup_profiling_timer(unsigned int multiplier
)
601 * Local APIC start and shutdown
605 * clear_local_APIC - shutdown the local APIC
607 * This is called, when a CPU is disabled and before rebooting, so the state of
608 * the local APIC has no dangling leftovers. Also used to cleanout any BIOS
609 * leftovers during boot.
611 void clear_local_APIC(void)
613 int maxlvt
= lapic_get_maxlvt();
617 * Masking an LVT entry can trigger a local APIC error
618 * if the vector is zero. Mask LVTERR first to prevent this.
621 v
= ERROR_APIC_VECTOR
; /* any non-zero vector will do */
622 apic_write_around(APIC_LVTERR
, v
| APIC_LVT_MASKED
);
625 * Careful: we have to set masks only first to deassert
626 * any level-triggered sources.
628 v
= apic_read(APIC_LVTT
);
629 apic_write_around(APIC_LVTT
, v
| APIC_LVT_MASKED
);
630 v
= apic_read(APIC_LVT0
);
631 apic_write_around(APIC_LVT0
, v
| APIC_LVT_MASKED
);
632 v
= apic_read(APIC_LVT1
);
633 apic_write_around(APIC_LVT1
, v
| APIC_LVT_MASKED
);
635 v
= apic_read(APIC_LVTPC
);
636 apic_write_around(APIC_LVTPC
, v
| APIC_LVT_MASKED
);
639 /* lets not touch this if we didn't frob it */
640 #ifdef CONFIG_X86_MCE_P4THERMAL
642 v
= apic_read(APIC_LVTTHMR
);
643 apic_write_around(APIC_LVTTHMR
, v
| APIC_LVT_MASKED
);
647 * Clean APIC state for other OSs:
649 apic_write_around(APIC_LVTT
, APIC_LVT_MASKED
);
650 apic_write_around(APIC_LVT0
, APIC_LVT_MASKED
);
651 apic_write_around(APIC_LVT1
, APIC_LVT_MASKED
);
653 apic_write_around(APIC_LVTERR
, APIC_LVT_MASKED
);
655 apic_write_around(APIC_LVTPC
, APIC_LVT_MASKED
);
657 #ifdef CONFIG_X86_MCE_P4THERMAL
659 apic_write_around(APIC_LVTTHMR
, APIC_LVT_MASKED
);
661 /* Integrated APIC (!82489DX) ? */
662 if (lapic_is_integrated()) {
664 /* Clear ESR due to Pentium errata 3AP and 11AP */
665 apic_write(APIC_ESR
, 0);
671 * disable_local_APIC - clear and disable the local APIC
673 void disable_local_APIC(void)
680 * Disable APIC (implies clearing of registers
683 value
= apic_read(APIC_SPIV
);
684 value
&= ~APIC_SPIV_APIC_ENABLED
;
685 apic_write_around(APIC_SPIV
, value
);
688 * When LAPIC was disabled by the BIOS and enabled by the kernel,
689 * restore the disabled state.
691 if (enabled_via_apicbase
) {
694 rdmsr(MSR_IA32_APICBASE
, l
, h
);
695 l
&= ~MSR_IA32_APICBASE_ENABLE
;
696 wrmsr(MSR_IA32_APICBASE
, l
, h
);
701 * If Linux enabled the LAPIC against the BIOS default disable it down before
702 * re-entering the BIOS on shutdown. Otherwise the BIOS may get confused and
703 * not power-off. Additionally clear all LVT entries before disable_local_APIC
704 * for the case where Linux didn't enable the LAPIC.
706 void lapic_shutdown(void)
713 local_irq_save(flags
);
716 if (enabled_via_apicbase
)
717 disable_local_APIC();
719 local_irq_restore(flags
);
723 * This is to verify that we're looking at a real local APIC.
724 * Check these against your board if the CPUs aren't getting
725 * started for no apparent reason.
727 int __init
verify_local_APIC(void)
729 unsigned int reg0
, reg1
;
732 * The version register is read-only in a real APIC.
734 reg0
= apic_read(APIC_LVR
);
735 apic_printk(APIC_DEBUG
, "Getting VERSION: %x\n", reg0
);
736 apic_write(APIC_LVR
, reg0
^ APIC_LVR_MASK
);
737 reg1
= apic_read(APIC_LVR
);
738 apic_printk(APIC_DEBUG
, "Getting VERSION: %x\n", reg1
);
741 * The two version reads above should print the same
742 * numbers. If the second one is different, then we
743 * poke at a non-APIC.
749 * Check if the version looks reasonably.
751 reg1
= GET_APIC_VERSION(reg0
);
752 if (reg1
== 0x00 || reg1
== 0xff)
754 reg1
= lapic_get_maxlvt();
755 if (reg1
< 0x02 || reg1
== 0xff)
759 * The ID register is read/write in a real APIC.
761 reg0
= apic_read(APIC_ID
);
762 apic_printk(APIC_DEBUG
, "Getting ID: %x\n", reg0
);
765 * The next two are just to see if we have sane values.
766 * They're only really relevant if we're in Virtual Wire
767 * compatibility mode, but most boxes are anymore.
769 reg0
= apic_read(APIC_LVT0
);
770 apic_printk(APIC_DEBUG
, "Getting LVT0: %x\n", reg0
);
771 reg1
= apic_read(APIC_LVT1
);
772 apic_printk(APIC_DEBUG
, "Getting LVT1: %x\n", reg1
);
778 * sync_Arb_IDs - synchronize APIC bus arbitration IDs
780 void __init
sync_Arb_IDs(void)
783 * Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1 And not
791 apic_wait_icr_idle();
793 apic_printk(APIC_DEBUG
, "Synchronizing Arb IDs.\n");
794 apic_write_around(APIC_ICR
, APIC_DEST_ALLINC
| APIC_INT_LEVELTRIG
799 * An initial setup of the virtual wire mode.
801 void __init
init_bsp_APIC(void)
806 * Don't do the setup now if we have a SMP BIOS as the
807 * through-I/O-APIC virtual wire mode might be active.
809 if (smp_found_config
|| !cpu_has_apic
)
813 * Do not trust the local APIC being empty at bootup.
820 value
= apic_read(APIC_SPIV
);
821 value
&= ~APIC_VECTOR_MASK
;
822 value
|= APIC_SPIV_APIC_ENABLED
;
824 /* This bit is reserved on P4/Xeon and should be cleared */
825 if ((boot_cpu_data
.x86_vendor
== X86_VENDOR_INTEL
) &&
826 (boot_cpu_data
.x86
== 15))
827 value
&= ~APIC_SPIV_FOCUS_DISABLED
;
829 value
|= APIC_SPIV_FOCUS_DISABLED
;
830 value
|= SPURIOUS_APIC_VECTOR
;
831 apic_write_around(APIC_SPIV
, value
);
834 * Set up the virtual wire mode.
836 apic_write_around(APIC_LVT0
, APIC_DM_EXTINT
);
838 if (!lapic_is_integrated()) /* 82489DX */
839 value
|= APIC_LVT_LEVEL_TRIGGER
;
840 apic_write_around(APIC_LVT1
, value
);
844 * setup_local_APIC - setup the local APIC
846 void __devinit
setup_local_APIC(void)
848 unsigned long oldvalue
, value
, maxlvt
, integrated
;
851 /* Pound the ESR really hard over the head with a big hammer - mbligh */
853 apic_write(APIC_ESR
, 0);
854 apic_write(APIC_ESR
, 0);
855 apic_write(APIC_ESR
, 0);
856 apic_write(APIC_ESR
, 0);
859 integrated
= lapic_is_integrated();
862 * Double-check whether this APIC is really registered.
864 if (!apic_id_registered())
868 * Intel recommends to set DFR, LDR and TPR before enabling
869 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
870 * document number 292116). So here it goes...
875 * Set Task Priority to 'accept all'. We never change this
878 value
= apic_read(APIC_TASKPRI
);
879 value
&= ~APIC_TPRI_MASK
;
880 apic_write_around(APIC_TASKPRI
, value
);
883 * After a crash, we no longer service the interrupts and a pending
884 * interrupt from previous kernel might still have ISR bit set.
886 * Most probably by now CPU has serviced that pending interrupt and
887 * it might not have done the ack_APIC_irq() because it thought,
888 * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
889 * does not clear the ISR bit and cpu thinks it has already serivced
890 * the interrupt. Hence a vector might get locked. It was noticed
891 * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
893 for (i
= APIC_ISR_NR
- 1; i
>= 0; i
--) {
894 value
= apic_read(APIC_ISR
+ i
*0x10);
895 for (j
= 31; j
>= 0; j
--) {
902 * Now that we are all set up, enable the APIC
904 value
= apic_read(APIC_SPIV
);
905 value
&= ~APIC_VECTOR_MASK
;
909 value
|= APIC_SPIV_APIC_ENABLED
;
912 * Some unknown Intel IO/APIC (or APIC) errata is biting us with
913 * certain networking cards. If high frequency interrupts are
914 * happening on a particular IOAPIC pin, plus the IOAPIC routing
915 * entry is masked/unmasked at a high rate as well then sooner or
916 * later IOAPIC line gets 'stuck', no more interrupts are received
917 * from the device. If focus CPU is disabled then the hang goes
920 * [ This bug can be reproduced easily with a level-triggered
921 * PCI Ne2000 networking cards and PII/PIII processors, dual
925 * Actually disabling the focus CPU check just makes the hang less
926 * frequent as it makes the interrupt distributon model be more
927 * like LRU than MRU (the short-term load is more even across CPUs).
928 * See also the comment in end_level_ioapic_irq(). --macro
931 /* Enable focus processor (bit==0) */
932 value
&= ~APIC_SPIV_FOCUS_DISABLED
;
935 * Set spurious IRQ vector
937 value
|= SPURIOUS_APIC_VECTOR
;
938 apic_write_around(APIC_SPIV
, value
);
943 * set up through-local-APIC on the BP's LINT0. This is not
944 * strictly necessery in pure symmetric-IO mode, but sometimes
945 * we delegate interrupts to the 8259A.
948 * TODO: set up through-local-APIC from through-I/O-APIC? --macro
950 value
= apic_read(APIC_LVT0
) & APIC_LVT_MASKED
;
951 if (!smp_processor_id() && (pic_mode
|| !value
)) {
952 value
= APIC_DM_EXTINT
;
953 apic_printk(APIC_VERBOSE
, "enabled ExtINT on CPU#%d\n",
956 value
= APIC_DM_EXTINT
| APIC_LVT_MASKED
;
957 apic_printk(APIC_VERBOSE
, "masked ExtINT on CPU#%d\n",
960 apic_write_around(APIC_LVT0
, value
);
963 * only the BP should see the LINT1 NMI signal, obviously.
965 if (!smp_processor_id())
968 value
= APIC_DM_NMI
| APIC_LVT_MASKED
;
969 if (!integrated
) /* 82489DX */
970 value
|= APIC_LVT_LEVEL_TRIGGER
;
971 apic_write_around(APIC_LVT1
, value
);
973 if (integrated
&& !esr_disable
) { /* !82489DX */
974 maxlvt
= lapic_get_maxlvt();
975 if (maxlvt
> 3) /* Due to the Pentium erratum 3AP. */
976 apic_write(APIC_ESR
, 0);
977 oldvalue
= apic_read(APIC_ESR
);
979 /* enables sending errors */
980 value
= ERROR_APIC_VECTOR
;
981 apic_write_around(APIC_LVTERR
, value
);
983 * spec says clear errors after enabling vector.
986 apic_write(APIC_ESR
, 0);
987 value
= apic_read(APIC_ESR
);
988 if (value
!= oldvalue
)
989 apic_printk(APIC_VERBOSE
, "ESR value before enabling "
990 "vector: 0x%08lx after: 0x%08lx\n",
995 * Something untraceble is creating bad interrupts on
996 * secondary quads ... for the moment, just leave the
997 * ESR disabled - we can't do anything useful with the
998 * errors anyway - mbligh
1000 printk(KERN_INFO
"Leaving ESR disabled.\n");
1002 printk(KERN_INFO
"No ESR for 82489DX.\n");
1005 /* Disable the local apic timer */
1006 value
= apic_read(APIC_LVTT
);
1007 value
|= (APIC_LVT_MASKED
| LOCAL_TIMER_VECTOR
);
1008 apic_write_around(APIC_LVTT
, value
);
1010 setup_apic_nmi_watchdog(NULL
);
1015 * Detect and initialize APIC
1017 static int __init
detect_init_APIC (void)
1021 /* Disabled by kernel option? */
1022 if (enable_local_apic
< 0)
1025 switch (boot_cpu_data
.x86_vendor
) {
1026 case X86_VENDOR_AMD
:
1027 if ((boot_cpu_data
.x86
== 6 && boot_cpu_data
.x86_model
> 1) ||
1028 (boot_cpu_data
.x86
== 15))
1031 case X86_VENDOR_INTEL
:
1032 if (boot_cpu_data
.x86
== 6 || boot_cpu_data
.x86
== 15 ||
1033 (boot_cpu_data
.x86
== 5 && cpu_has_apic
))
1040 if (!cpu_has_apic
) {
1042 * Over-ride BIOS and try to enable the local APIC only if
1043 * "lapic" specified.
1045 if (enable_local_apic
<= 0) {
1046 printk(KERN_INFO
"Local APIC disabled by BIOS -- "
1047 "you can enable it with \"lapic\"\n");
1051 * Some BIOSes disable the local APIC in the APIC_BASE
1052 * MSR. This can only be done in software for Intel P6 or later
1053 * and AMD K7 (Model > 1) or later.
1055 rdmsr(MSR_IA32_APICBASE
, l
, h
);
1056 if (!(l
& MSR_IA32_APICBASE_ENABLE
)) {
1058 "Local APIC disabled by BIOS -- reenabling.\n");
1059 l
&= ~MSR_IA32_APICBASE_BASE
;
1060 l
|= MSR_IA32_APICBASE_ENABLE
| APIC_DEFAULT_PHYS_BASE
;
1061 wrmsr(MSR_IA32_APICBASE
, l
, h
);
1062 enabled_via_apicbase
= 1;
1066 * The APIC feature bit should now be enabled
1069 features
= cpuid_edx(1);
1070 if (!(features
& (1 << X86_FEATURE_APIC
))) {
1071 printk(KERN_WARNING
"Could not enable APIC!\n");
1074 set_bit(X86_FEATURE_APIC
, boot_cpu_data
.x86_capability
);
1075 mp_lapic_addr
= APIC_DEFAULT_PHYS_BASE
;
1077 /* The BIOS may have set up the APIC at some other address */
1078 rdmsr(MSR_IA32_APICBASE
, l
, h
);
1079 if (l
& MSR_IA32_APICBASE_ENABLE
)
1080 mp_lapic_addr
= l
& MSR_IA32_APICBASE_BASE
;
1082 if (nmi_watchdog
!= NMI_NONE
)
1083 nmi_watchdog
= NMI_LOCAL_APIC
;
1085 printk(KERN_INFO
"Found and enabled local APIC!\n");
1092 printk(KERN_INFO
"No local APIC present or hardware disabled\n");
1097 * init_apic_mappings - initialize APIC mappings
1099 void __init
init_apic_mappings(void)
1101 unsigned long apic_phys
;
1104 * If no local APIC can be found then set up a fake all
1105 * zeroes page to simulate the local APIC and another
1106 * one for the IO-APIC.
1108 if (!smp_found_config
&& detect_init_APIC()) {
1109 apic_phys
= (unsigned long) alloc_bootmem_pages(PAGE_SIZE
);
1110 apic_phys
= __pa(apic_phys
);
1112 apic_phys
= mp_lapic_addr
;
1114 set_fixmap_nocache(FIX_APIC_BASE
, apic_phys
);
1115 printk(KERN_DEBUG
"mapped APIC to %08lx (%08lx)\n", APIC_BASE
,
1119 * Fetch the APIC ID of the BSP in case we have a
1120 * default configuration (or the MP table is broken).
1122 if (boot_cpu_physical_apicid
== -1U)
1123 boot_cpu_physical_apicid
= GET_APIC_ID(apic_read(APIC_ID
));
1125 #ifdef CONFIG_X86_IO_APIC
1127 unsigned long ioapic_phys
, idx
= FIX_IO_APIC_BASE_0
;
1130 for (i
= 0; i
< nr_ioapics
; i
++) {
1131 if (smp_found_config
) {
1132 ioapic_phys
= mp_ioapics
[i
].mpc_apicaddr
;
1135 "WARNING: bogus zero IO-APIC "
1136 "address found in MPTABLE, "
1137 "disabling IO/APIC support!\n");
1138 smp_found_config
= 0;
1139 skip_ioapic_setup
= 1;
1140 goto fake_ioapic_page
;
1144 ioapic_phys
= (unsigned long)
1145 alloc_bootmem_pages(PAGE_SIZE
);
1146 ioapic_phys
= __pa(ioapic_phys
);
1148 set_fixmap_nocache(idx
, ioapic_phys
);
1149 printk(KERN_DEBUG
"mapped IOAPIC to %08lx (%08lx)\n",
1150 __fix_to_virt(idx
), ioapic_phys
);
1158 * This initializes the IO-APIC and APIC hardware if this is
1161 int __init
APIC_init_uniprocessor (void)
1163 if (enable_local_apic
< 0)
1164 clear_bit(X86_FEATURE_APIC
, boot_cpu_data
.x86_capability
);
1166 if (!smp_found_config
&& !cpu_has_apic
)
1170 * Complain if the BIOS pretends there is one.
1172 if (!cpu_has_apic
&&
1173 APIC_INTEGRATED(apic_version
[boot_cpu_physical_apicid
])) {
1174 printk(KERN_ERR
"BIOS bug, local APIC #%d not detected!...\n",
1175 boot_cpu_physical_apicid
);
1176 clear_bit(X86_FEATURE_APIC
, boot_cpu_data
.x86_capability
);
1180 verify_local_APIC();
1185 * Hack: In case of kdump, after a crash, kernel might be booting
1186 * on a cpu with non-zero lapic id. But boot_cpu_physical_apicid
1187 * might be zero if read from MP tables. Get it from LAPIC.
1189 #ifdef CONFIG_CRASH_DUMP
1190 boot_cpu_physical_apicid
= GET_APIC_ID(apic_read(APIC_ID
));
1192 phys_cpu_present_map
= physid_mask_of_physid(boot_cpu_physical_apicid
);
1196 #ifdef CONFIG_X86_IO_APIC
1197 if (smp_found_config
)
1198 if (!skip_ioapic_setup
&& nr_ioapics
)
1207 * APIC command line parameters
1209 static int __init
parse_lapic(char *arg
)
1211 enable_local_apic
= 1;
1214 early_param("lapic", parse_lapic
);
1216 static int __init
parse_nolapic(char *arg
)
1218 enable_local_apic
= -1;
1219 clear_bit(X86_FEATURE_APIC
, boot_cpu_data
.x86_capability
);
1222 early_param("nolapic", parse_nolapic
);
1224 static int __init
parse_disable_lapic_timer(char *arg
)
1226 local_apic_timer_disabled
= 1;
1229 early_param("nolapic_timer", parse_disable_lapic_timer
);
1231 static int __init
parse_lapic_timer_c2_ok(char *arg
)
1233 local_apic_timer_c2_ok
= 1;
1236 early_param("lapic_timer_c2_ok", parse_lapic_timer_c2_ok
);
1238 static int __init
apic_set_verbosity(char *str
)
1240 if (strcmp("debug", str
) == 0)
1241 apic_verbosity
= APIC_DEBUG
;
1242 else if (strcmp("verbose", str
) == 0)
1243 apic_verbosity
= APIC_VERBOSE
;
1247 __setup("apic=", apic_set_verbosity
);
1251 * Local APIC interrupts
1255 * This interrupt should _never_ happen with our APIC/SMP architecture
1257 void smp_spurious_interrupt(struct pt_regs
*regs
)
1263 * Check if this really is a spurious interrupt and ACK it
1264 * if it is a vectored one. Just in case...
1265 * Spurious interrupts should not be ACKed.
1267 v
= apic_read(APIC_ISR
+ ((SPURIOUS_APIC_VECTOR
& ~0x1f) >> 1));
1268 if (v
& (1 << (SPURIOUS_APIC_VECTOR
& 0x1f)))
1271 /* see sw-dev-man vol 3, chapter 7.4.13.5 */
1272 printk(KERN_INFO
"spurious APIC interrupt on CPU#%d, "
1273 "should never happen.\n", smp_processor_id());
1278 * This interrupt should never happen with our APIC/SMP architecture
1280 void smp_error_interrupt(struct pt_regs
*regs
)
1282 unsigned long v
, v1
;
1285 /* First tickle the hardware, only then report what went on. -- REW */
1286 v
= apic_read(APIC_ESR
);
1287 apic_write(APIC_ESR
, 0);
1288 v1
= apic_read(APIC_ESR
);
1290 atomic_inc(&irq_err_count
);
1292 /* Here is what the APIC error bits mean:
1295 2: Send accept error
1296 3: Receive accept error
1298 5: Send illegal vector
1299 6: Received illegal vector
1300 7: Illegal register address
1302 printk (KERN_DEBUG
"APIC error on CPU%d: %02lx(%02lx)\n",
1303 smp_processor_id(), v
, v1
);
1308 * Initialize APIC interrupts
1310 void __init
apic_intr_init(void)
1315 /* self generated IPI for local APIC timer */
1316 set_intr_gate(LOCAL_TIMER_VECTOR
, apic_timer_interrupt
);
1318 /* IPI vectors for APIC spurious and error interrupts */
1319 set_intr_gate(SPURIOUS_APIC_VECTOR
, spurious_interrupt
);
1320 set_intr_gate(ERROR_APIC_VECTOR
, error_interrupt
);
1322 /* thermal monitor LVT interrupt */
1323 #ifdef CONFIG_X86_MCE_P4THERMAL
1324 set_intr_gate(THERMAL_APIC_VECTOR
, thermal_interrupt
);
1329 * connect_bsp_APIC - attach the APIC to the interrupt system
1331 void __init
connect_bsp_APIC(void)
1335 * Do not trust the local APIC being empty at bootup.
1339 * PIC mode, enable APIC mode in the IMCR, i.e. connect BSP's
1340 * local APIC to INT and NMI lines.
1342 apic_printk(APIC_VERBOSE
, "leaving PIC mode, "
1343 "enabling APIC mode.\n");
1351 * disconnect_bsp_APIC - detach the APIC from the interrupt system
1352 * @virt_wire_setup: indicates, whether virtual wire mode is selected
1354 * Virtual wire mode is necessary to deliver legacy interrupts even when the
1357 void disconnect_bsp_APIC(int virt_wire_setup
)
1361 * Put the board back into PIC mode (has an effect only on
1362 * certain older boards). Note that APIC interrupts, including
1363 * IPIs, won't work beyond this point! The only exception are
1366 apic_printk(APIC_VERBOSE
, "disabling APIC mode, "
1367 "entering PIC mode.\n");
1371 /* Go back to Virtual Wire compatibility mode */
1372 unsigned long value
;
1374 /* For the spurious interrupt use vector F, and enable it */
1375 value
= apic_read(APIC_SPIV
);
1376 value
&= ~APIC_VECTOR_MASK
;
1377 value
|= APIC_SPIV_APIC_ENABLED
;
1379 apic_write_around(APIC_SPIV
, value
);
1381 if (!virt_wire_setup
) {
1383 * For LVT0 make it edge triggered, active high,
1384 * external and enabled
1386 value
= apic_read(APIC_LVT0
);
1387 value
&= ~(APIC_MODE_MASK
| APIC_SEND_PENDING
|
1388 APIC_INPUT_POLARITY
| APIC_LVT_REMOTE_IRR
|
1389 APIC_LVT_LEVEL_TRIGGER
| APIC_LVT_MASKED
);
1390 value
|= APIC_LVT_REMOTE_IRR
| APIC_SEND_PENDING
;
1391 value
= SET_APIC_DELIVERY_MODE(value
, APIC_MODE_EXTINT
);
1392 apic_write_around(APIC_LVT0
, value
);
1395 apic_write_around(APIC_LVT0
, APIC_LVT_MASKED
);
1399 * For LVT1 make it edge triggered, active high, nmi and
1402 value
= apic_read(APIC_LVT1
);
1404 APIC_MODE_MASK
| APIC_SEND_PENDING
|
1405 APIC_INPUT_POLARITY
| APIC_LVT_REMOTE_IRR
|
1406 APIC_LVT_LEVEL_TRIGGER
| APIC_LVT_MASKED
);
1407 value
|= APIC_LVT_REMOTE_IRR
| APIC_SEND_PENDING
;
1408 value
= SET_APIC_DELIVERY_MODE(value
, APIC_MODE_NMI
);
1409 apic_write_around(APIC_LVT1
, value
);
1420 /* r/w apic fields */
1421 unsigned int apic_id
;
1422 unsigned int apic_taskpri
;
1423 unsigned int apic_ldr
;
1424 unsigned int apic_dfr
;
1425 unsigned int apic_spiv
;
1426 unsigned int apic_lvtt
;
1427 unsigned int apic_lvtpc
;
1428 unsigned int apic_lvt0
;
1429 unsigned int apic_lvt1
;
1430 unsigned int apic_lvterr
;
1431 unsigned int apic_tmict
;
1432 unsigned int apic_tdcr
;
1433 unsigned int apic_thmr
;
1436 static int lapic_suspend(struct sys_device
*dev
, pm_message_t state
)
1438 unsigned long flags
;
1441 if (!apic_pm_state
.active
)
1444 maxlvt
= lapic_get_maxlvt();
1446 apic_pm_state
.apic_id
= apic_read(APIC_ID
);
1447 apic_pm_state
.apic_taskpri
= apic_read(APIC_TASKPRI
);
1448 apic_pm_state
.apic_ldr
= apic_read(APIC_LDR
);
1449 apic_pm_state
.apic_dfr
= apic_read(APIC_DFR
);
1450 apic_pm_state
.apic_spiv
= apic_read(APIC_SPIV
);
1451 apic_pm_state
.apic_lvtt
= apic_read(APIC_LVTT
);
1453 apic_pm_state
.apic_lvtpc
= apic_read(APIC_LVTPC
);
1454 apic_pm_state
.apic_lvt0
= apic_read(APIC_LVT0
);
1455 apic_pm_state
.apic_lvt1
= apic_read(APIC_LVT1
);
1456 apic_pm_state
.apic_lvterr
= apic_read(APIC_LVTERR
);
1457 apic_pm_state
.apic_tmict
= apic_read(APIC_TMICT
);
1458 apic_pm_state
.apic_tdcr
= apic_read(APIC_TDCR
);
1459 #ifdef CONFIG_X86_MCE_P4THERMAL
1461 apic_pm_state
.apic_thmr
= apic_read(APIC_LVTTHMR
);
1464 local_irq_save(flags
);
1465 disable_local_APIC();
1466 local_irq_restore(flags
);
1470 static int lapic_resume(struct sys_device
*dev
)
1473 unsigned long flags
;
1476 if (!apic_pm_state
.active
)
1479 maxlvt
= lapic_get_maxlvt();
1481 local_irq_save(flags
);
1484 * Make sure the APICBASE points to the right address
1486 * FIXME! This will be wrong if we ever support suspend on
1487 * SMP! We'll need to do this as part of the CPU restore!
1489 rdmsr(MSR_IA32_APICBASE
, l
, h
);
1490 l
&= ~MSR_IA32_APICBASE_BASE
;
1491 l
|= MSR_IA32_APICBASE_ENABLE
| mp_lapic_addr
;
1492 wrmsr(MSR_IA32_APICBASE
, l
, h
);
1494 apic_write(APIC_LVTERR
, ERROR_APIC_VECTOR
| APIC_LVT_MASKED
);
1495 apic_write(APIC_ID
, apic_pm_state
.apic_id
);
1496 apic_write(APIC_DFR
, apic_pm_state
.apic_dfr
);
1497 apic_write(APIC_LDR
, apic_pm_state
.apic_ldr
);
1498 apic_write(APIC_TASKPRI
, apic_pm_state
.apic_taskpri
);
1499 apic_write(APIC_SPIV
, apic_pm_state
.apic_spiv
);
1500 apic_write(APIC_LVT0
, apic_pm_state
.apic_lvt0
);
1501 apic_write(APIC_LVT1
, apic_pm_state
.apic_lvt1
);
1502 #ifdef CONFIG_X86_MCE_P4THERMAL
1504 apic_write(APIC_LVTTHMR
, apic_pm_state
.apic_thmr
);
1507 apic_write(APIC_LVTPC
, apic_pm_state
.apic_lvtpc
);
1508 apic_write(APIC_LVTT
, apic_pm_state
.apic_lvtt
);
1509 apic_write(APIC_TDCR
, apic_pm_state
.apic_tdcr
);
1510 apic_write(APIC_TMICT
, apic_pm_state
.apic_tmict
);
1511 apic_write(APIC_ESR
, 0);
1512 apic_read(APIC_ESR
);
1513 apic_write(APIC_LVTERR
, apic_pm_state
.apic_lvterr
);
1514 apic_write(APIC_ESR
, 0);
1515 apic_read(APIC_ESR
);
1516 local_irq_restore(flags
);
1521 * This device has no shutdown method - fully functioning local APICs
1522 * are needed on every CPU up until machine_halt/restart/poweroff.
1525 static struct sysdev_class lapic_sysclass
= {
1526 set_kset_name("lapic"),
1527 .resume
= lapic_resume
,
1528 .suspend
= lapic_suspend
,
1531 static struct sys_device device_lapic
= {
1533 .cls
= &lapic_sysclass
,
1536 static void __devinit
apic_pm_activate(void)
1538 apic_pm_state
.active
= 1;
1541 static int __init
init_lapic_sysfs(void)
1547 /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
1549 error
= sysdev_class_register(&lapic_sysclass
);
1551 error
= sysdev_register(&device_lapic
);
1554 device_initcall(init_lapic_sysfs
);
1556 #else /* CONFIG_PM */
1558 static void apic_pm_activate(void) { }
1560 #endif /* CONFIG_PM */