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/smp_lock.h>
23 #include <linux/interrupt.h>
24 #include <linux/mc146818rtc.h>
25 #include <linux/kernel_stat.h>
26 #include <linux/sysdev.h>
27 #include <linux/cpu.h>
28 #include <linux/module.h>
30 #include <asm/atomic.h>
33 #include <asm/mpspec.h>
35 #include <asm/arch_hooks.h>
37 #include <asm/i8253.h>
40 #include <mach_apic.h>
41 #include <mach_apicdef.h>
47 * cpu_mask that denotes the CPUs that needs timer interrupt coming in as
48 * IPIs in place of local APIC timers
50 static cpumask_t timer_bcast_ipi
;
53 * Knob to control our willingness to enable the local APIC.
55 static int enable_local_apic __initdata
= 0; /* -1=force-disable, +1=force-enable */
57 static inline void lapic_disable(void)
59 enable_local_apic
= -1;
60 clear_bit(X86_FEATURE_APIC
, boot_cpu_data
.x86_capability
);
63 static inline void lapic_enable(void)
65 enable_local_apic
= 1;
74 static void apic_pm_activate(void);
76 static int modern_apic(void)
78 unsigned int lvr
, version
;
79 /* AMD systems use old APIC versions, so check the CPU */
80 if (boot_cpu_data
.x86_vendor
== X86_VENDOR_AMD
&&
81 boot_cpu_data
.x86
>= 0xf)
83 lvr
= apic_read(APIC_LVR
);
84 version
= GET_APIC_VERSION(lvr
);
85 return version
>= 0x14;
89 * 'what should we do if we get a hw irq event on an illegal vector'.
90 * each architecture has to answer this themselves.
92 void ack_bad_irq(unsigned int irq
)
94 printk("unexpected IRQ trap at vector %02x\n", irq
);
96 * Currently unexpected vectors happen only on SMP and APIC.
97 * We _must_ ack these because every local APIC has only N
98 * irq slots per priority level, and a 'hanging, unacked' IRQ
99 * holds up an irq slot - in excessive cases (when multiple
100 * unexpected vectors occur) that might lock up the APIC
102 * But only ack when the APIC is enabled -AK
108 void __init
apic_intr_init(void)
113 /* self generated IPI for local APIC timer */
114 set_intr_gate(LOCAL_TIMER_VECTOR
, apic_timer_interrupt
);
116 /* IPI vectors for APIC spurious and error interrupts */
117 set_intr_gate(SPURIOUS_APIC_VECTOR
, spurious_interrupt
);
118 set_intr_gate(ERROR_APIC_VECTOR
, error_interrupt
);
120 /* thermal monitor LVT interrupt */
121 #ifdef CONFIG_X86_MCE_P4THERMAL
122 set_intr_gate(THERMAL_APIC_VECTOR
, thermal_interrupt
);
126 /* Using APIC to generate smp_local_timer_interrupt? */
127 int using_apic_timer __read_mostly
= 0;
129 static int enabled_via_apicbase
;
131 void enable_NMI_through_LVT0 (void * dummy
)
135 ver
= apic_read(APIC_LVR
);
136 ver
= GET_APIC_VERSION(ver
);
137 v
= APIC_DM_NMI
; /* unmask and set to NMI */
138 if (!APIC_INTEGRATED(ver
)) /* 82489DX */
139 v
|= APIC_LVT_LEVEL_TRIGGER
;
140 apic_write_around(APIC_LVT0
, v
);
143 int get_physical_broadcast(void)
153 unsigned int v
, ver
, maxlvt
;
155 v
= apic_read(APIC_LVR
);
156 ver
= GET_APIC_VERSION(v
);
157 /* 82489DXs do not report # of LVT entries. */
158 maxlvt
= APIC_INTEGRATED(ver
) ? GET_APIC_MAXLVT(v
) : 2;
162 void clear_local_APIC(void)
167 maxlvt
= get_maxlvt();
170 * Masking an LVT entry can trigger a local APIC error
171 * if the vector is zero. Mask LVTERR first to prevent this.
174 v
= ERROR_APIC_VECTOR
; /* any non-zero vector will do */
175 apic_write_around(APIC_LVTERR
, v
| APIC_LVT_MASKED
);
178 * Careful: we have to set masks only first to deassert
179 * any level-triggered sources.
181 v
= apic_read(APIC_LVTT
);
182 apic_write_around(APIC_LVTT
, v
| APIC_LVT_MASKED
);
183 v
= apic_read(APIC_LVT0
);
184 apic_write_around(APIC_LVT0
, v
| APIC_LVT_MASKED
);
185 v
= apic_read(APIC_LVT1
);
186 apic_write_around(APIC_LVT1
, v
| APIC_LVT_MASKED
);
188 v
= apic_read(APIC_LVTPC
);
189 apic_write_around(APIC_LVTPC
, v
| APIC_LVT_MASKED
);
192 /* lets not touch this if we didn't frob it */
193 #ifdef CONFIG_X86_MCE_P4THERMAL
195 v
= apic_read(APIC_LVTTHMR
);
196 apic_write_around(APIC_LVTTHMR
, v
| APIC_LVT_MASKED
);
200 * Clean APIC state for other OSs:
202 apic_write_around(APIC_LVTT
, APIC_LVT_MASKED
);
203 apic_write_around(APIC_LVT0
, APIC_LVT_MASKED
);
204 apic_write_around(APIC_LVT1
, APIC_LVT_MASKED
);
206 apic_write_around(APIC_LVTERR
, APIC_LVT_MASKED
);
208 apic_write_around(APIC_LVTPC
, APIC_LVT_MASKED
);
210 #ifdef CONFIG_X86_MCE_P4THERMAL
212 apic_write_around(APIC_LVTTHMR
, APIC_LVT_MASKED
);
214 v
= GET_APIC_VERSION(apic_read(APIC_LVR
));
215 if (APIC_INTEGRATED(v
)) { /* !82489DX */
216 if (maxlvt
> 3) /* Due to Pentium errata 3AP and 11AP. */
217 apic_write(APIC_ESR
, 0);
222 void __init
connect_bsp_APIC(void)
226 * Do not trust the local APIC being empty at bootup.
230 * PIC mode, enable APIC mode in the IMCR, i.e.
231 * connect BSP's local APIC to INT and NMI lines.
233 apic_printk(APIC_VERBOSE
, "leaving PIC mode, "
234 "enabling APIC mode.\n");
241 void disconnect_bsp_APIC(int virt_wire_setup
)
245 * Put the board back into PIC mode (has an effect
246 * only on certain older boards). Note that APIC
247 * interrupts, including IPIs, won't work beyond
248 * this point! The only exception are INIT IPIs.
250 apic_printk(APIC_VERBOSE
, "disabling APIC mode, "
251 "entering PIC mode.\n");
256 /* Go back to Virtual Wire compatibility mode */
259 /* For the spurious interrupt use vector F, and enable it */
260 value
= apic_read(APIC_SPIV
);
261 value
&= ~APIC_VECTOR_MASK
;
262 value
|= APIC_SPIV_APIC_ENABLED
;
264 apic_write_around(APIC_SPIV
, value
);
266 if (!virt_wire_setup
) {
267 /* For LVT0 make it edge triggered, active high, external and enabled */
268 value
= apic_read(APIC_LVT0
);
269 value
&= ~(APIC_MODE_MASK
| APIC_SEND_PENDING
|
270 APIC_INPUT_POLARITY
| APIC_LVT_REMOTE_IRR
|
271 APIC_LVT_LEVEL_TRIGGER
| APIC_LVT_MASKED
);
272 value
|= APIC_LVT_REMOTE_IRR
| APIC_SEND_PENDING
;
273 value
= SET_APIC_DELIVERY_MODE(value
, APIC_MODE_EXTINT
);
274 apic_write_around(APIC_LVT0
, value
);
278 apic_write_around(APIC_LVT0
, APIC_LVT_MASKED
);
281 /* For LVT1 make it edge triggered, active high, nmi and enabled */
282 value
= apic_read(APIC_LVT1
);
284 APIC_MODE_MASK
| APIC_SEND_PENDING
|
285 APIC_INPUT_POLARITY
| APIC_LVT_REMOTE_IRR
|
286 APIC_LVT_LEVEL_TRIGGER
| APIC_LVT_MASKED
);
287 value
|= APIC_LVT_REMOTE_IRR
| APIC_SEND_PENDING
;
288 value
= SET_APIC_DELIVERY_MODE(value
, APIC_MODE_NMI
);
289 apic_write_around(APIC_LVT1
, value
);
293 void disable_local_APIC(void)
300 * Disable APIC (implies clearing of registers
303 value
= apic_read(APIC_SPIV
);
304 value
&= ~APIC_SPIV_APIC_ENABLED
;
305 apic_write_around(APIC_SPIV
, value
);
307 if (enabled_via_apicbase
) {
309 rdmsr(MSR_IA32_APICBASE
, l
, h
);
310 l
&= ~MSR_IA32_APICBASE_ENABLE
;
311 wrmsr(MSR_IA32_APICBASE
, l
, h
);
316 * This is to verify that we're looking at a real local APIC.
317 * Check these against your board if the CPUs aren't getting
318 * started for no apparent reason.
320 int __init
verify_local_APIC(void)
322 unsigned int reg0
, reg1
;
325 * The version register is read-only in a real APIC.
327 reg0
= apic_read(APIC_LVR
);
328 apic_printk(APIC_DEBUG
, "Getting VERSION: %x\n", reg0
);
329 apic_write(APIC_LVR
, reg0
^ APIC_LVR_MASK
);
330 reg1
= apic_read(APIC_LVR
);
331 apic_printk(APIC_DEBUG
, "Getting VERSION: %x\n", reg1
);
334 * The two version reads above should print the same
335 * numbers. If the second one is different, then we
336 * poke at a non-APIC.
342 * Check if the version looks reasonably.
344 reg1
= GET_APIC_VERSION(reg0
);
345 if (reg1
== 0x00 || reg1
== 0xff)
348 if (reg1
< 0x02 || reg1
== 0xff)
352 * The ID register is read/write in a real APIC.
354 reg0
= apic_read(APIC_ID
);
355 apic_printk(APIC_DEBUG
, "Getting ID: %x\n", reg0
);
358 * The next two are just to see if we have sane values.
359 * They're only really relevant if we're in Virtual Wire
360 * compatibility mode, but most boxes are anymore.
362 reg0
= apic_read(APIC_LVT0
);
363 apic_printk(APIC_DEBUG
, "Getting LVT0: %x\n", reg0
);
364 reg1
= apic_read(APIC_LVT1
);
365 apic_printk(APIC_DEBUG
, "Getting LVT1: %x\n", reg1
);
370 void __init
sync_Arb_IDs(void)
372 /* Unsupported on P4 - see Intel Dev. Manual Vol. 3, Ch. 8.6.1
373 And not needed on AMD */
379 apic_wait_icr_idle();
381 apic_printk(APIC_DEBUG
, "Synchronizing Arb IDs.\n");
382 apic_write_around(APIC_ICR
, APIC_DEST_ALLINC
| APIC_INT_LEVELTRIG
386 extern void __error_in_apic_c (void);
389 * An initial setup of the virtual wire mode.
391 void __init
init_bsp_APIC(void)
393 unsigned long value
, ver
;
396 * Don't do the setup now if we have a SMP BIOS as the
397 * through-I/O-APIC virtual wire mode might be active.
399 if (smp_found_config
|| !cpu_has_apic
)
402 value
= apic_read(APIC_LVR
);
403 ver
= GET_APIC_VERSION(value
);
406 * Do not trust the local APIC being empty at bootup.
413 value
= apic_read(APIC_SPIV
);
414 value
&= ~APIC_VECTOR_MASK
;
415 value
|= APIC_SPIV_APIC_ENABLED
;
417 /* This bit is reserved on P4/Xeon and should be cleared */
418 if ((boot_cpu_data
.x86_vendor
== X86_VENDOR_INTEL
) && (boot_cpu_data
.x86
== 15))
419 value
&= ~APIC_SPIV_FOCUS_DISABLED
;
421 value
|= APIC_SPIV_FOCUS_DISABLED
;
422 value
|= SPURIOUS_APIC_VECTOR
;
423 apic_write_around(APIC_SPIV
, value
);
426 * Set up the virtual wire mode.
428 apic_write_around(APIC_LVT0
, APIC_DM_EXTINT
);
430 if (!APIC_INTEGRATED(ver
)) /* 82489DX */
431 value
|= APIC_LVT_LEVEL_TRIGGER
;
432 apic_write_around(APIC_LVT1
, value
);
435 void __devinit
setup_local_APIC(void)
437 unsigned long oldvalue
, value
, ver
, maxlvt
;
440 /* Pound the ESR really hard over the head with a big hammer - mbligh */
442 apic_write(APIC_ESR
, 0);
443 apic_write(APIC_ESR
, 0);
444 apic_write(APIC_ESR
, 0);
445 apic_write(APIC_ESR
, 0);
448 value
= apic_read(APIC_LVR
);
449 ver
= GET_APIC_VERSION(value
);
451 if ((SPURIOUS_APIC_VECTOR
& 0x0f) != 0x0f)
455 * Double-check whether this APIC is really registered.
457 if (!apic_id_registered())
461 * Intel recommends to set DFR, LDR and TPR before enabling
462 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
463 * document number 292116). So here it goes...
468 * Set Task Priority to 'accept all'. We never change this
471 value
= apic_read(APIC_TASKPRI
);
472 value
&= ~APIC_TPRI_MASK
;
473 apic_write_around(APIC_TASKPRI
, value
);
476 * After a crash, we no longer service the interrupts and a pending
477 * interrupt from previous kernel might still have ISR bit set.
479 * Most probably by now CPU has serviced that pending interrupt and
480 * it might not have done the ack_APIC_irq() because it thought,
481 * interrupt came from i8259 as ExtInt. LAPIC did not get EOI so it
482 * does not clear the ISR bit and cpu thinks it has already serivced
483 * the interrupt. Hence a vector might get locked. It was noticed
484 * for timer irq (vector 0x31). Issue an extra EOI to clear ISR.
486 for (i
= APIC_ISR_NR
- 1; i
>= 0; i
--) {
487 value
= apic_read(APIC_ISR
+ i
*0x10);
488 for (j
= 31; j
>= 0; j
--) {
495 * Now that we are all set up, enable the APIC
497 value
= apic_read(APIC_SPIV
);
498 value
&= ~APIC_VECTOR_MASK
;
502 value
|= APIC_SPIV_APIC_ENABLED
;
505 * Some unknown Intel IO/APIC (or APIC) errata is biting us with
506 * certain networking cards. If high frequency interrupts are
507 * happening on a particular IOAPIC pin, plus the IOAPIC routing
508 * entry is masked/unmasked at a high rate as well then sooner or
509 * later IOAPIC line gets 'stuck', no more interrupts are received
510 * from the device. If focus CPU is disabled then the hang goes
513 * [ This bug can be reproduced easily with a level-triggered
514 * PCI Ne2000 networking cards and PII/PIII processors, dual
518 * Actually disabling the focus CPU check just makes the hang less
519 * frequent as it makes the interrupt distributon model be more
520 * like LRU than MRU (the short-term load is more even across CPUs).
521 * See also the comment in end_level_ioapic_irq(). --macro
524 /* Enable focus processor (bit==0) */
525 value
&= ~APIC_SPIV_FOCUS_DISABLED
;
527 /* Disable focus processor (bit==1) */
528 value
|= APIC_SPIV_FOCUS_DISABLED
;
531 * Set spurious IRQ vector
533 value
|= SPURIOUS_APIC_VECTOR
;
534 apic_write_around(APIC_SPIV
, value
);
539 * set up through-local-APIC on the BP's LINT0. This is not
540 * strictly necessery in pure symmetric-IO mode, but sometimes
541 * we delegate interrupts to the 8259A.
544 * TODO: set up through-local-APIC from through-I/O-APIC? --macro
546 value
= apic_read(APIC_LVT0
) & APIC_LVT_MASKED
;
547 if (!smp_processor_id() && (pic_mode
|| !value
)) {
548 value
= APIC_DM_EXTINT
;
549 apic_printk(APIC_VERBOSE
, "enabled ExtINT on CPU#%d\n",
552 value
= APIC_DM_EXTINT
| APIC_LVT_MASKED
;
553 apic_printk(APIC_VERBOSE
, "masked ExtINT on CPU#%d\n",
556 apic_write_around(APIC_LVT0
, value
);
559 * only the BP should see the LINT1 NMI signal, obviously.
561 if (!smp_processor_id())
564 value
= APIC_DM_NMI
| APIC_LVT_MASKED
;
565 if (!APIC_INTEGRATED(ver
)) /* 82489DX */
566 value
|= APIC_LVT_LEVEL_TRIGGER
;
567 apic_write_around(APIC_LVT1
, value
);
569 if (APIC_INTEGRATED(ver
) && !esr_disable
) { /* !82489DX */
570 maxlvt
= get_maxlvt();
571 if (maxlvt
> 3) /* Due to the Pentium erratum 3AP. */
572 apic_write(APIC_ESR
, 0);
573 oldvalue
= apic_read(APIC_ESR
);
575 value
= ERROR_APIC_VECTOR
; // enables sending errors
576 apic_write_around(APIC_LVTERR
, value
);
578 * spec says clear errors after enabling vector.
581 apic_write(APIC_ESR
, 0);
582 value
= apic_read(APIC_ESR
);
583 if (value
!= oldvalue
)
584 apic_printk(APIC_VERBOSE
, "ESR value before enabling "
585 "vector: 0x%08lx after: 0x%08lx\n",
590 * Something untraceble is creating bad interrupts on
591 * secondary quads ... for the moment, just leave the
592 * ESR disabled - we can't do anything useful with the
593 * errors anyway - mbligh
595 printk("Leaving ESR disabled.\n");
597 printk("No ESR for 82489DX.\n");
600 setup_apic_nmi_watchdog(NULL
);
605 * If Linux enabled the LAPIC against the BIOS default
606 * disable it down before re-entering the BIOS on shutdown.
607 * Otherwise the BIOS may get confused and not power-off.
608 * Additionally clear all LVT entries before disable_local_APIC
609 * for the case where Linux didn't enable the LAPIC.
611 void lapic_shutdown(void)
618 local_irq_save(flags
);
621 if (enabled_via_apicbase
)
622 disable_local_APIC();
624 local_irq_restore(flags
);
631 /* r/w apic fields */
632 unsigned int apic_id
;
633 unsigned int apic_taskpri
;
634 unsigned int apic_ldr
;
635 unsigned int apic_dfr
;
636 unsigned int apic_spiv
;
637 unsigned int apic_lvtt
;
638 unsigned int apic_lvtpc
;
639 unsigned int apic_lvt0
;
640 unsigned int apic_lvt1
;
641 unsigned int apic_lvterr
;
642 unsigned int apic_tmict
;
643 unsigned int apic_tdcr
;
644 unsigned int apic_thmr
;
647 static int lapic_suspend(struct sys_device
*dev
, pm_message_t state
)
651 if (!apic_pm_state
.active
)
654 apic_pm_state
.apic_id
= apic_read(APIC_ID
);
655 apic_pm_state
.apic_taskpri
= apic_read(APIC_TASKPRI
);
656 apic_pm_state
.apic_ldr
= apic_read(APIC_LDR
);
657 apic_pm_state
.apic_dfr
= apic_read(APIC_DFR
);
658 apic_pm_state
.apic_spiv
= apic_read(APIC_SPIV
);
659 apic_pm_state
.apic_lvtt
= apic_read(APIC_LVTT
);
660 apic_pm_state
.apic_lvtpc
= apic_read(APIC_LVTPC
);
661 apic_pm_state
.apic_lvt0
= apic_read(APIC_LVT0
);
662 apic_pm_state
.apic_lvt1
= apic_read(APIC_LVT1
);
663 apic_pm_state
.apic_lvterr
= apic_read(APIC_LVTERR
);
664 apic_pm_state
.apic_tmict
= apic_read(APIC_TMICT
);
665 apic_pm_state
.apic_tdcr
= apic_read(APIC_TDCR
);
666 apic_pm_state
.apic_thmr
= apic_read(APIC_LVTTHMR
);
668 local_irq_save(flags
);
669 disable_local_APIC();
670 local_irq_restore(flags
);
674 static int lapic_resume(struct sys_device
*dev
)
679 if (!apic_pm_state
.active
)
682 local_irq_save(flags
);
685 * Make sure the APICBASE points to the right address
687 * FIXME! This will be wrong if we ever support suspend on
688 * SMP! We'll need to do this as part of the CPU restore!
690 rdmsr(MSR_IA32_APICBASE
, l
, h
);
691 l
&= ~MSR_IA32_APICBASE_BASE
;
692 l
|= MSR_IA32_APICBASE_ENABLE
| mp_lapic_addr
;
693 wrmsr(MSR_IA32_APICBASE
, l
, h
);
695 apic_write(APIC_LVTERR
, ERROR_APIC_VECTOR
| APIC_LVT_MASKED
);
696 apic_write(APIC_ID
, apic_pm_state
.apic_id
);
697 apic_write(APIC_DFR
, apic_pm_state
.apic_dfr
);
698 apic_write(APIC_LDR
, apic_pm_state
.apic_ldr
);
699 apic_write(APIC_TASKPRI
, apic_pm_state
.apic_taskpri
);
700 apic_write(APIC_SPIV
, apic_pm_state
.apic_spiv
);
701 apic_write(APIC_LVT0
, apic_pm_state
.apic_lvt0
);
702 apic_write(APIC_LVT1
, apic_pm_state
.apic_lvt1
);
703 apic_write(APIC_LVTTHMR
, apic_pm_state
.apic_thmr
);
704 apic_write(APIC_LVTPC
, apic_pm_state
.apic_lvtpc
);
705 apic_write(APIC_LVTT
, apic_pm_state
.apic_lvtt
);
706 apic_write(APIC_TDCR
, apic_pm_state
.apic_tdcr
);
707 apic_write(APIC_TMICT
, apic_pm_state
.apic_tmict
);
708 apic_write(APIC_ESR
, 0);
710 apic_write(APIC_LVTERR
, apic_pm_state
.apic_lvterr
);
711 apic_write(APIC_ESR
, 0);
713 local_irq_restore(flags
);
718 * This device has no shutdown method - fully functioning local APICs
719 * are needed on every CPU up until machine_halt/restart/poweroff.
722 static struct sysdev_class lapic_sysclass
= {
723 set_kset_name("lapic"),
724 .resume
= lapic_resume
,
725 .suspend
= lapic_suspend
,
728 static struct sys_device device_lapic
= {
730 .cls
= &lapic_sysclass
,
733 static void __devinit
apic_pm_activate(void)
735 apic_pm_state
.active
= 1;
738 static int __init
init_lapic_sysfs(void)
744 /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
746 error
= sysdev_class_register(&lapic_sysclass
);
748 error
= sysdev_register(&device_lapic
);
751 device_initcall(init_lapic_sysfs
);
753 #else /* CONFIG_PM */
755 static void apic_pm_activate(void) { }
757 #endif /* CONFIG_PM */
760 * Detect and enable local APICs on non-SMP boards.
761 * Original code written by Keir Fraser.
764 static int __init
apic_set_verbosity(char *str
)
766 if (strcmp("debug", str
) == 0)
767 apic_verbosity
= APIC_DEBUG
;
768 else if (strcmp("verbose", str
) == 0)
769 apic_verbosity
= APIC_VERBOSE
;
773 __setup("apic=", apic_set_verbosity
);
775 static int __init
detect_init_APIC (void)
779 /* Disabled by kernel option? */
780 if (enable_local_apic
< 0)
783 switch (boot_cpu_data
.x86_vendor
) {
785 if ((boot_cpu_data
.x86
== 6 && boot_cpu_data
.x86_model
> 1) ||
786 (boot_cpu_data
.x86
== 15))
789 case X86_VENDOR_INTEL
:
790 if (boot_cpu_data
.x86
== 6 || boot_cpu_data
.x86
== 15 ||
791 (boot_cpu_data
.x86
== 5 && cpu_has_apic
))
800 * Over-ride BIOS and try to enable the local
801 * APIC only if "lapic" specified.
803 if (enable_local_apic
<= 0) {
804 printk("Local APIC disabled by BIOS -- "
805 "you can enable it with \"lapic\"\n");
809 * Some BIOSes disable the local APIC in the
810 * APIC_BASE MSR. This can only be done in
811 * software for Intel P6 or later and AMD K7
812 * (Model > 1) or later.
814 rdmsr(MSR_IA32_APICBASE
, l
, h
);
815 if (!(l
& MSR_IA32_APICBASE_ENABLE
)) {
816 printk("Local APIC disabled by BIOS -- reenabling.\n");
817 l
&= ~MSR_IA32_APICBASE_BASE
;
818 l
|= MSR_IA32_APICBASE_ENABLE
| APIC_DEFAULT_PHYS_BASE
;
819 wrmsr(MSR_IA32_APICBASE
, l
, h
);
820 enabled_via_apicbase
= 1;
824 * The APIC feature bit should now be enabled
827 features
= cpuid_edx(1);
828 if (!(features
& (1 << X86_FEATURE_APIC
))) {
829 printk("Could not enable APIC!\n");
832 set_bit(X86_FEATURE_APIC
, boot_cpu_data
.x86_capability
);
833 mp_lapic_addr
= APIC_DEFAULT_PHYS_BASE
;
835 /* The BIOS may have set up the APIC at some other address */
836 rdmsr(MSR_IA32_APICBASE
, l
, h
);
837 if (l
& MSR_IA32_APICBASE_ENABLE
)
838 mp_lapic_addr
= l
& MSR_IA32_APICBASE_BASE
;
840 if (nmi_watchdog
!= NMI_NONE
)
841 nmi_watchdog
= NMI_LOCAL_APIC
;
843 printk("Found and enabled local APIC!\n");
850 printk("No local APIC present or hardware disabled\n");
854 void __init
init_apic_mappings(void)
856 unsigned long apic_phys
;
859 * If no local APIC can be found then set up a fake all
860 * zeroes page to simulate the local APIC and another
861 * one for the IO-APIC.
863 if (!smp_found_config
&& detect_init_APIC()) {
864 apic_phys
= (unsigned long) alloc_bootmem_pages(PAGE_SIZE
);
865 apic_phys
= __pa(apic_phys
);
867 apic_phys
= mp_lapic_addr
;
869 set_fixmap_nocache(FIX_APIC_BASE
, apic_phys
);
870 printk(KERN_DEBUG
"mapped APIC to %08lx (%08lx)\n", APIC_BASE
,
874 * Fetch the APIC ID of the BSP in case we have a
875 * default configuration (or the MP table is broken).
877 if (boot_cpu_physical_apicid
== -1U)
878 boot_cpu_physical_apicid
= GET_APIC_ID(apic_read(APIC_ID
));
880 #ifdef CONFIG_X86_IO_APIC
882 unsigned long ioapic_phys
, idx
= FIX_IO_APIC_BASE_0
;
885 for (i
= 0; i
< nr_ioapics
; i
++) {
886 if (smp_found_config
) {
887 ioapic_phys
= mp_ioapics
[i
].mpc_apicaddr
;
890 "WARNING: bogus zero IO-APIC "
891 "address found in MPTABLE, "
892 "disabling IO/APIC support!\n");
893 smp_found_config
= 0;
894 skip_ioapic_setup
= 1;
895 goto fake_ioapic_page
;
899 ioapic_phys
= (unsigned long)
900 alloc_bootmem_pages(PAGE_SIZE
);
901 ioapic_phys
= __pa(ioapic_phys
);
903 set_fixmap_nocache(idx
, ioapic_phys
);
904 printk(KERN_DEBUG
"mapped IOAPIC to %08lx (%08lx)\n",
905 __fix_to_virt(idx
), ioapic_phys
);
913 * This part sets up the APIC 32 bit clock in LVTT1, with HZ interrupts
914 * per second. We assume that the caller has already set up the local
917 * The APIC timer is not exactly sync with the external timer chip, it
918 * closely follows bus clocks.
922 * The timer chip is already set up at HZ interrupts per second here,
923 * but we do not accept timer interrupts yet. We only allow the BP
926 static unsigned int __devinit
get_8254_timer_count(void)
932 spin_lock_irqsave(&i8253_lock
, flags
);
934 outb_p(0x00, PIT_MODE
);
935 count
= inb_p(PIT_CH0
);
936 count
|= inb_p(PIT_CH0
) << 8;
938 spin_unlock_irqrestore(&i8253_lock
, flags
);
943 /* next tick in 8254 can be caught by catching timer wraparound */
944 static void __devinit
wait_8254_wraparound(void)
946 unsigned int curr_count
, prev_count
;
948 curr_count
= get_8254_timer_count();
950 prev_count
= curr_count
;
951 curr_count
= get_8254_timer_count();
953 /* workaround for broken Mercury/Neptune */
954 if (prev_count
>= curr_count
+ 0x100)
955 curr_count
= get_8254_timer_count();
957 } while (prev_count
>= curr_count
);
961 * Default initialization for 8254 timers. If we use other timers like HPET,
962 * we override this later
964 void (*wait_timer_tick
)(void) __devinitdata
= wait_8254_wraparound
;
967 * This function sets up the local APIC timer, with a timeout of
968 * 'clocks' APIC bus clock. During calibration we actually call
969 * this function twice on the boot CPU, once with a bogus timeout
970 * value, second time for real. The other (noncalibrating) CPUs
971 * call this function only once, with the real, calibrated value.
973 * We do reads before writes even if unnecessary, to get around the
974 * P5 APIC double write bug.
977 #define APIC_DIVISOR 16
979 static void __setup_APIC_LVTT(unsigned int clocks
)
981 unsigned int lvtt_value
, tmp_value
, ver
;
982 int cpu
= smp_processor_id();
984 ver
= GET_APIC_VERSION(apic_read(APIC_LVR
));
985 lvtt_value
= APIC_LVT_TIMER_PERIODIC
| LOCAL_TIMER_VECTOR
;
986 if (!APIC_INTEGRATED(ver
))
987 lvtt_value
|= SET_APIC_TIMER_BASE(APIC_TIMER_BASE_DIV
);
989 if (cpu_isset(cpu
, timer_bcast_ipi
))
990 lvtt_value
|= APIC_LVT_MASKED
;
992 apic_write_around(APIC_LVTT
, lvtt_value
);
997 tmp_value
= apic_read(APIC_TDCR
);
998 apic_write_around(APIC_TDCR
, (tmp_value
999 & ~(APIC_TDR_DIV_1
| APIC_TDR_DIV_TMBASE
))
1002 apic_write_around(APIC_TMICT
, clocks
/APIC_DIVISOR
);
1005 static void __devinit
setup_APIC_timer(unsigned int clocks
)
1007 unsigned long flags
;
1009 local_irq_save(flags
);
1012 * Wait for IRQ0's slice:
1016 __setup_APIC_LVTT(clocks
);
1018 local_irq_restore(flags
);
1022 * In this function we calibrate APIC bus clocks to the external
1023 * timer. Unfortunately we cannot use jiffies and the timer irq
1024 * to calibrate, since some later bootup code depends on getting
1025 * the first irq? Ugh.
1027 * We want to do the calibration only once since we
1028 * want to have local timer irqs syncron. CPUs connected
1029 * by the same APIC bus have the very same bus frequency.
1030 * And we want to have irqs off anyways, no accidental
1031 * APIC irq that way.
1034 static int __init
calibrate_APIC_clock(void)
1036 unsigned long long t1
= 0, t2
= 0;
1040 const int LOOPS
= HZ
/10;
1042 apic_printk(APIC_VERBOSE
, "calibrating APIC timer ...\n");
1045 * Put whatever arbitrary (but long enough) timeout
1046 * value into the APIC clock, we just want to get the
1047 * counter running for calibration.
1049 __setup_APIC_LVTT(1000000000);
1052 * The timer chip counts down to zero. Let's wait
1053 * for a wraparound to start exact measurement:
1054 * (the current tick might have been already half done)
1060 * We wrapped around just now. Let's start:
1064 tt1
= apic_read(APIC_TMCCT
);
1067 * Let's wait LOOPS wraprounds:
1069 for (i
= 0; i
< LOOPS
; i
++)
1072 tt2
= apic_read(APIC_TMCCT
);
1077 * The APIC bus clock counter is 32 bits only, it
1078 * might have overflown, but note that we use signed
1079 * longs, thus no extra care needed.
1081 * underflown to be exact, as the timer counts down ;)
1084 result
= (tt1
-tt2
)*APIC_DIVISOR
/LOOPS
;
1087 apic_printk(APIC_VERBOSE
, "..... CPU clock speed is "
1089 ((long)(t2
-t1
)/LOOPS
)/(1000000/HZ
),
1090 ((long)(t2
-t1
)/LOOPS
)%(1000000/HZ
));
1092 apic_printk(APIC_VERBOSE
, "..... host bus clock speed is "
1094 result
/(1000000/HZ
),
1095 result
%(1000000/HZ
));
1100 static unsigned int calibration_result
;
1102 void __init
setup_boot_APIC_clock(void)
1104 unsigned long flags
;
1105 apic_printk(APIC_VERBOSE
, "Using local APIC timer interrupts.\n");
1106 using_apic_timer
= 1;
1108 local_irq_save(flags
);
1110 calibration_result
= calibrate_APIC_clock();
1112 * Now set up the timer for real.
1114 setup_APIC_timer(calibration_result
);
1116 local_irq_restore(flags
);
1119 void __devinit
setup_secondary_APIC_clock(void)
1121 setup_APIC_timer(calibration_result
);
1124 void disable_APIC_timer(void)
1126 if (using_apic_timer
) {
1129 v
= apic_read(APIC_LVTT
);
1131 * When an illegal vector value (0-15) is written to an LVT
1132 * entry and delivery mode is Fixed, the APIC may signal an
1133 * illegal vector error, with out regard to whether the mask
1134 * bit is set or whether an interrupt is actually seen on input.
1136 * Boot sequence might call this function when the LVTT has
1137 * '0' vector value. So make sure vector field is set to
1140 v
|= (APIC_LVT_MASKED
| LOCAL_TIMER_VECTOR
);
1141 apic_write_around(APIC_LVTT
, v
);
1145 void enable_APIC_timer(void)
1147 int cpu
= smp_processor_id();
1149 if (using_apic_timer
&&
1150 !cpu_isset(cpu
, timer_bcast_ipi
)) {
1153 v
= apic_read(APIC_LVTT
);
1154 apic_write_around(APIC_LVTT
, v
& ~APIC_LVT_MASKED
);
1158 void switch_APIC_timer_to_ipi(void *cpumask
)
1160 cpumask_t mask
= *(cpumask_t
*)cpumask
;
1161 int cpu
= smp_processor_id();
1163 if (cpu_isset(cpu
, mask
) &&
1164 !cpu_isset(cpu
, timer_bcast_ipi
)) {
1165 disable_APIC_timer();
1166 cpu_set(cpu
, timer_bcast_ipi
);
1169 EXPORT_SYMBOL(switch_APIC_timer_to_ipi
);
1171 void switch_ipi_to_APIC_timer(void *cpumask
)
1173 cpumask_t mask
= *(cpumask_t
*)cpumask
;
1174 int cpu
= smp_processor_id();
1176 if (cpu_isset(cpu
, mask
) &&
1177 cpu_isset(cpu
, timer_bcast_ipi
)) {
1178 cpu_clear(cpu
, timer_bcast_ipi
);
1179 enable_APIC_timer();
1182 EXPORT_SYMBOL(switch_ipi_to_APIC_timer
);
1187 * Local timer interrupt handler. It does both profiling and
1188 * process statistics/rescheduling.
1190 * We do profiling in every local tick, statistics/rescheduling
1191 * happen only every 'profiling multiplier' ticks. The default
1192 * multiplier is 1 and it can be changed by writing the new multiplier
1193 * value into /proc/profile.
1196 inline void smp_local_timer_interrupt(void)
1198 profile_tick(CPU_PROFILING
);
1200 update_process_times(user_mode_vm(get_irq_regs()));
1204 * We take the 'long' return path, and there every subsystem
1205 * grabs the apropriate locks (kernel lock/ irq lock).
1207 * we might want to decouple profiling from the 'long path',
1208 * and do the profiling totally in assembly.
1210 * Currently this isn't too much of an issue (performance wise),
1211 * we can take more than 100K local irqs per second on a 100 MHz P5.
1216 * Local APIC timer interrupt. This is the most natural way for doing
1217 * local interrupts, but local timer interrupts can be emulated by
1218 * broadcast interrupts too. [in case the hw doesn't support APIC timers]
1220 * [ if a single-CPU system runs an SMP kernel then we call the local
1221 * interrupt as well. Thus we cannot inline the local irq ... ]
1224 fastcall
void smp_apic_timer_interrupt(struct pt_regs
*regs
)
1226 struct pt_regs
*old_regs
= set_irq_regs(regs
);
1227 int cpu
= smp_processor_id();
1230 * the NMI deadlock-detector uses this.
1232 per_cpu(irq_stat
, cpu
).apic_timer_irqs
++;
1235 * NOTE! We'd better ACK the irq immediately,
1236 * because timer handling can be slow.
1240 * update_process_times() expects us to have done irq_enter().
1241 * Besides, if we don't timer interrupts ignore the global
1242 * interrupt lock, which is the WrongThing (tm) to do.
1245 smp_local_timer_interrupt();
1247 set_irq_regs(old_regs
);
1251 static void up_apic_timer_interrupt_call(void)
1253 int cpu
= smp_processor_id();
1256 * the NMI deadlock-detector uses this.
1258 per_cpu(irq_stat
, cpu
).apic_timer_irqs
++;
1260 smp_local_timer_interrupt();
1264 void smp_send_timer_broadcast_ipi(void)
1268 cpus_and(mask
, cpu_online_map
, timer_bcast_ipi
);
1269 if (!cpus_empty(mask
)) {
1271 send_IPI_mask(mask
, LOCAL_TIMER_VECTOR
);
1274 * We can directly call the apic timer interrupt handler
1275 * in UP case. Minus all irq related functions
1277 up_apic_timer_interrupt_call();
1282 int setup_profiling_timer(unsigned int multiplier
)
1288 * This interrupt should _never_ happen with our APIC/SMP architecture
1290 fastcall
void smp_spurious_interrupt(struct pt_regs
*regs
)
1296 * Check if this really is a spurious interrupt and ACK it
1297 * if it is a vectored one. Just in case...
1298 * Spurious interrupts should not be ACKed.
1300 v
= apic_read(APIC_ISR
+ ((SPURIOUS_APIC_VECTOR
& ~0x1f) >> 1));
1301 if (v
& (1 << (SPURIOUS_APIC_VECTOR
& 0x1f)))
1304 /* see sw-dev-man vol 3, chapter 7.4.13.5 */
1305 printk(KERN_INFO
"spurious APIC interrupt on CPU#%d, should never happen.\n",
1306 smp_processor_id());
1311 * This interrupt should never happen with our APIC/SMP architecture
1314 fastcall
void smp_error_interrupt(struct pt_regs
*regs
)
1316 unsigned long v
, v1
;
1319 /* First tickle the hardware, only then report what went on. -- REW */
1320 v
= apic_read(APIC_ESR
);
1321 apic_write(APIC_ESR
, 0);
1322 v1
= apic_read(APIC_ESR
);
1324 atomic_inc(&irq_err_count
);
1326 /* Here is what the APIC error bits mean:
1329 2: Send accept error
1330 3: Receive accept error
1332 5: Send illegal vector
1333 6: Received illegal vector
1334 7: Illegal register address
1336 printk (KERN_DEBUG
"APIC error on CPU%d: %02lx(%02lx)\n",
1337 smp_processor_id(), v
, v1
);
1342 * This initializes the IO-APIC and APIC hardware if this is
1345 int __init
APIC_init_uniprocessor (void)
1347 if (enable_local_apic
< 0)
1348 clear_bit(X86_FEATURE_APIC
, boot_cpu_data
.x86_capability
);
1350 if (!smp_found_config
&& !cpu_has_apic
)
1354 * Complain if the BIOS pretends there is one.
1356 if (!cpu_has_apic
&& APIC_INTEGRATED(apic_version
[boot_cpu_physical_apicid
])) {
1357 printk(KERN_ERR
"BIOS bug, local APIC #%d not detected!...\n",
1358 boot_cpu_physical_apicid
);
1359 clear_bit(X86_FEATURE_APIC
, boot_cpu_data
.x86_capability
);
1363 verify_local_APIC();
1368 * Hack: In case of kdump, after a crash, kernel might be booting
1369 * on a cpu with non-zero lapic id. But boot_cpu_physical_apicid
1370 * might be zero if read from MP tables. Get it from LAPIC.
1372 #ifdef CONFIG_CRASH_DUMP
1373 boot_cpu_physical_apicid
= GET_APIC_ID(apic_read(APIC_ID
));
1375 phys_cpu_present_map
= physid_mask_of_physid(boot_cpu_physical_apicid
);
1379 #ifdef CONFIG_X86_IO_APIC
1380 if (smp_found_config
)
1381 if (!skip_ioapic_setup
&& nr_ioapics
)
1384 setup_boot_APIC_clock();
1389 static int __init
parse_lapic(char *arg
)
1394 early_param("lapic", parse_lapic
);
1396 static int __init
parse_nolapic(char *arg
)
1401 early_param("nolapic", parse_nolapic
);