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/config.h>
18 #include <linux/init.h>
21 #include <linux/irq.h>
22 #include <linux/delay.h>
23 #include <linux/bootmem.h>
24 #include <linux/smp_lock.h>
25 #include <linux/interrupt.h>
26 #include <linux/mc146818rtc.h>
27 #include <linux/kernel_stat.h>
28 #include <linux/sysdev.h>
30 #include <asm/atomic.h>
33 #include <asm/mpspec.h>
35 #include <asm/arch_hooks.h>
38 #include <mach_apic.h>
48 static void apic_pm_activate(void);
50 void __init apic_intr_init(void)
55 /* self generated IPI for local APIC timer */
56 set_intr_gate(LOCAL_TIMER_VECTOR, apic_timer_interrupt);
58 /* IPI vectors for APIC spurious and error interrupts */
59 set_intr_gate(SPURIOUS_APIC_VECTOR, spurious_interrupt);
60 set_intr_gate(ERROR_APIC_VECTOR, error_interrupt);
62 /* thermal monitor LVT interrupt */
63 #ifdef CONFIG_X86_MCE_P4THERMAL
64 set_intr_gate(THERMAL_APIC_VECTOR, thermal_interrupt);
68 /* Using APIC to generate smp_local_timer_interrupt? */
69 int using_apic_timer = 0;
71 static DEFINE_PER_CPU(int, prof_multiplier) = 1;
72 static DEFINE_PER_CPU(int, prof_old_multiplier) = 1;
73 static DEFINE_PER_CPU(int, prof_counter) = 1;
75 static int enabled_via_apicbase;
77 void enable_NMI_through_LVT0 (void * dummy)
81 ver = apic_read(APIC_LVR);
82 ver = GET_APIC_VERSION(ver);
83 v = APIC_DM_NMI; /* unmask and set to NMI */
84 if (!APIC_INTEGRATED(ver)) /* 82489DX */
85 v |= APIC_LVT_LEVEL_TRIGGER;
86 apic_write_around(APIC_LVT0, v);
89 int get_physical_broadcast(void)
91 unsigned int lvr, version;
92 lvr = apic_read(APIC_LVR);
93 version = GET_APIC_VERSION(lvr);
94 if (!APIC_INTEGRATED(version) || version >= 0x14)
102 unsigned int v, ver, maxlvt;
104 v = apic_read(APIC_LVR);
105 ver = GET_APIC_VERSION(v);
106 /* 82489DXs do not report # of LVT entries. */
107 maxlvt = APIC_INTEGRATED(ver) ? GET_APIC_MAXLVT(v) : 2;
111 void clear_local_APIC(void)
116 maxlvt = get_maxlvt();
119 * Masking an LVT entry on a P6 can trigger a local APIC error
120 * if the vector is zero. Mask LVTERR first to prevent this.
123 v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
124 apic_write_around(APIC_LVTERR, v | APIC_LVT_MASKED);
127 * Careful: we have to set masks only first to deassert
128 * any level-triggered sources.
130 v = apic_read(APIC_LVTT);
131 apic_write_around(APIC_LVTT, v | APIC_LVT_MASKED);
132 v = apic_read(APIC_LVT0);
133 apic_write_around(APIC_LVT0, v | APIC_LVT_MASKED);
134 v = apic_read(APIC_LVT1);
135 apic_write_around(APIC_LVT1, v | APIC_LVT_MASKED);
137 v = apic_read(APIC_LVTPC);
138 apic_write_around(APIC_LVTPC, v | APIC_LVT_MASKED);
141 /* lets not touch this if we didn't frob it */
142 #ifdef CONFIG_X86_MCE_P4THERMAL
144 v = apic_read(APIC_LVTTHMR);
145 apic_write_around(APIC_LVTTHMR, v | APIC_LVT_MASKED);
149 * Clean APIC state for other OSs:
151 apic_write_around(APIC_LVTT, APIC_LVT_MASKED);
152 apic_write_around(APIC_LVT0, APIC_LVT_MASKED);
153 apic_write_around(APIC_LVT1, APIC_LVT_MASKED);
155 apic_write_around(APIC_LVTERR, APIC_LVT_MASKED);
157 apic_write_around(APIC_LVTPC, APIC_LVT_MASKED);
159 #ifdef CONFIG_X86_MCE_P4THERMAL
161 apic_write_around(APIC_LVTTHMR, APIC_LVT_MASKED);
163 v = GET_APIC_VERSION(apic_read(APIC_LVR));
164 if (APIC_INTEGRATED(v)) { /* !82489DX */
165 if (maxlvt > 3) /* Due to Pentium errata 3AP and 11AP. */
166 apic_write(APIC_ESR, 0);
171 void __init connect_bsp_APIC(void)
175 * Do not trust the local APIC being empty at bootup.
179 * PIC mode, enable APIC mode in the IMCR, i.e.
180 * connect BSP's local APIC to INT and NMI lines.
182 apic_printk(APIC_VERBOSE, "leaving PIC mode, "
183 "enabling APIC mode.\n");
190 void disconnect_bsp_APIC(void)
194 * Put the board back into PIC mode (has an effect
195 * only on certain older boards). Note that APIC
196 * interrupts, including IPIs, won't work beyond
197 * this point! The only exception are INIT IPIs.
199 apic_printk(APIC_VERBOSE, "disabling APIC mode, "
200 "entering PIC mode.\n");
205 /* Go back to Virtual Wire compatibility mode */
208 /* For the spurious interrupt use vector F, and enable it */
209 value = apic_read(APIC_SPIV);
210 value &= ~APIC_VECTOR_MASK;
211 value |= APIC_SPIV_APIC_ENABLED;
213 apic_write_around(APIC_SPIV, value);
215 /* For LVT0 make it edge triggered, active high, external and enabled */
216 value = apic_read(APIC_LVT0);
217 value &= ~(APIC_MODE_MASK | APIC_SEND_PENDING |
218 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
219 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED );
220 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
221 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_EXINT);
222 apic_write_around(APIC_LVT0, value);
224 /* For LVT1 make it edge triggered, active high, nmi and enabled */
225 value = apic_read(APIC_LVT1);
227 APIC_MODE_MASK | APIC_SEND_PENDING |
228 APIC_INPUT_POLARITY | APIC_LVT_REMOTE_IRR |
229 APIC_LVT_LEVEL_TRIGGER | APIC_LVT_MASKED);
230 value |= APIC_LVT_REMOTE_IRR | APIC_SEND_PENDING;
231 value = SET_APIC_DELIVERY_MODE(value, APIC_MODE_NMI);
232 apic_write_around(APIC_LVT1, value);
236 void disable_local_APIC(void)
243 * Disable APIC (implies clearing of registers
246 value = apic_read(APIC_SPIV);
247 value &= ~APIC_SPIV_APIC_ENABLED;
248 apic_write_around(APIC_SPIV, value);
250 if (enabled_via_apicbase) {
252 rdmsr(MSR_IA32_APICBASE, l, h);
253 l &= ~MSR_IA32_APICBASE_ENABLE;
254 wrmsr(MSR_IA32_APICBASE, l, h);
259 * This is to verify that we're looking at a real local APIC.
260 * Check these against your board if the CPUs aren't getting
261 * started for no apparent reason.
263 int __init verify_local_APIC(void)
265 unsigned int reg0, reg1;
268 * The version register is read-only in a real APIC.
270 reg0 = apic_read(APIC_LVR);
271 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
272 apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
273 reg1 = apic_read(APIC_LVR);
274 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
277 * The two version reads above should print the same
278 * numbers. If the second one is different, then we
279 * poke at a non-APIC.
285 * Check if the version looks reasonably.
287 reg1 = GET_APIC_VERSION(reg0);
288 if (reg1 == 0x00 || reg1 == 0xff)
291 if (reg1 < 0x02 || reg1 == 0xff)
295 * The ID register is read/write in a real APIC.
297 reg0 = apic_read(APIC_ID);
298 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
301 * The next two are just to see if we have sane values.
302 * They're only really relevant if we're in Virtual Wire
303 * compatibility mode, but most boxes are anymore.
305 reg0 = apic_read(APIC_LVT0);
306 apic_printk(APIC_DEBUG, "Getting LVT0: %x\n", reg0);
307 reg1 = apic_read(APIC_LVT1);
308 apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
313 void __init sync_Arb_IDs(void)
318 apic_wait_icr_idle();
320 apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
321 apic_write_around(APIC_ICR, APIC_DEST_ALLINC | APIC_INT_LEVELTRIG
325 extern void __error_in_apic_c (void);
328 * An initial setup of the virtual wire mode.
330 void __init init_bsp_APIC(void)
332 unsigned long value, ver;
335 * Don't do the setup now if we have a SMP BIOS as the
336 * through-I/O-APIC virtual wire mode might be active.
338 if (smp_found_config || !cpu_has_apic)
341 value = apic_read(APIC_LVR);
342 ver = GET_APIC_VERSION(value);
345 * Do not trust the local APIC being empty at bootup.
352 value = apic_read(APIC_SPIV);
353 value &= ~APIC_VECTOR_MASK;
354 value |= APIC_SPIV_APIC_ENABLED;
356 /* This bit is reserved on P4/Xeon and should be cleared */
357 if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) && (boot_cpu_data.x86 == 15))
358 value &= ~APIC_SPIV_FOCUS_DISABLED;
360 value |= APIC_SPIV_FOCUS_DISABLED;
361 value |= SPURIOUS_APIC_VECTOR;
362 apic_write_around(APIC_SPIV, value);
365 * Set up the virtual wire mode.
367 apic_write_around(APIC_LVT0, APIC_DM_EXTINT);
369 if (!APIC_INTEGRATED(ver)) /* 82489DX */
370 value |= APIC_LVT_LEVEL_TRIGGER;
371 apic_write_around(APIC_LVT1, value);
374 void __init setup_local_APIC (void)
376 unsigned long oldvalue, value, ver, maxlvt;
378 /* Pound the ESR really hard over the head with a big hammer - mbligh */
380 apic_write(APIC_ESR, 0);
381 apic_write(APIC_ESR, 0);
382 apic_write(APIC_ESR, 0);
383 apic_write(APIC_ESR, 0);
386 value = apic_read(APIC_LVR);
387 ver = GET_APIC_VERSION(value);
389 if ((SPURIOUS_APIC_VECTOR & 0x0f) != 0x0f)
393 * Double-check whether this APIC is really registered.
395 if (!apic_id_registered())
399 * Intel recommends to set DFR, LDR and TPR before enabling
400 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
401 * document number 292116). So here it goes...
406 * Set Task Priority to 'accept all'. We never change this
409 value = apic_read(APIC_TASKPRI);
410 value &= ~APIC_TPRI_MASK;
411 apic_write_around(APIC_TASKPRI, value);
414 * Now that we are all set up, enable the APIC
416 value = apic_read(APIC_SPIV);
417 value &= ~APIC_VECTOR_MASK;
421 value |= APIC_SPIV_APIC_ENABLED;
424 * Some unknown Intel IO/APIC (or APIC) errata is biting us with
425 * certain networking cards. If high frequency interrupts are
426 * happening on a particular IOAPIC pin, plus the IOAPIC routing
427 * entry is masked/unmasked at a high rate as well then sooner or
428 * later IOAPIC line gets 'stuck', no more interrupts are received
429 * from the device. If focus CPU is disabled then the hang goes
432 * [ This bug can be reproduced easily with a level-triggered
433 * PCI Ne2000 networking cards and PII/PIII processors, dual
437 * Actually disabling the focus CPU check just makes the hang less
438 * frequent as it makes the interrupt distributon model be more
439 * like LRU than MRU (the short-term load is more even across CPUs).
440 * See also the comment in end_level_ioapic_irq(). --macro
443 /* Enable focus processor (bit==0) */
444 value &= ~APIC_SPIV_FOCUS_DISABLED;
446 /* Disable focus processor (bit==1) */
447 value |= APIC_SPIV_FOCUS_DISABLED;
450 * Set spurious IRQ vector
452 value |= SPURIOUS_APIC_VECTOR;
453 apic_write_around(APIC_SPIV, value);
458 * set up through-local-APIC on the BP's LINT0. This is not
459 * strictly necessery in pure symmetric-IO mode, but sometimes
460 * we delegate interrupts to the 8259A.
463 * TODO: set up through-local-APIC from through-I/O-APIC? --macro
465 value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
466 if (!smp_processor_id() && (pic_mode || !value)) {
467 value = APIC_DM_EXTINT;
468 apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n",
471 value = APIC_DM_EXTINT | APIC_LVT_MASKED;
472 apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n",
475 apic_write_around(APIC_LVT0, value);
478 * only the BP should see the LINT1 NMI signal, obviously.
480 if (!smp_processor_id())
483 value = APIC_DM_NMI | APIC_LVT_MASKED;
484 if (!APIC_INTEGRATED(ver)) /* 82489DX */
485 value |= APIC_LVT_LEVEL_TRIGGER;
486 apic_write_around(APIC_LVT1, value);
488 if (APIC_INTEGRATED(ver) && !esr_disable) { /* !82489DX */
489 maxlvt = get_maxlvt();
490 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
491 apic_write(APIC_ESR, 0);
492 oldvalue = apic_read(APIC_ESR);
494 value = ERROR_APIC_VECTOR; // enables sending errors
495 apic_write_around(APIC_LVTERR, value);
497 * spec says clear errors after enabling vector.
500 apic_write(APIC_ESR, 0);
501 value = apic_read(APIC_ESR);
502 if (value != oldvalue)
503 apic_printk(APIC_VERBOSE, "ESR value before enabling "
504 "vector: 0x%08lx after: 0x%08lx\n",
509 * Something untraceble is creating bad interrupts on
510 * secondary quads ... for the moment, just leave the
511 * ESR disabled - we can't do anything useful with the
512 * errors anyway - mbligh
514 printk("Leaving ESR disabled.\n");
516 printk("No ESR for 82489DX.\n");
519 if (nmi_watchdog == NMI_LOCAL_APIC)
520 setup_apic_nmi_watchdog();
527 /* 'active' is true if the local APIC was enabled by us and
528 not the BIOS; this signifies that we are also responsible
529 for disabling it before entering apm/acpi suspend */
531 /* r/w apic fields */
532 unsigned int apic_id;
533 unsigned int apic_taskpri;
534 unsigned int apic_ldr;
535 unsigned int apic_dfr;
536 unsigned int apic_spiv;
537 unsigned int apic_lvtt;
538 unsigned int apic_lvtpc;
539 unsigned int apic_lvt0;
540 unsigned int apic_lvt1;
541 unsigned int apic_lvterr;
542 unsigned int apic_tmict;
543 unsigned int apic_tdcr;
544 unsigned int apic_thmr;
547 static int lapic_suspend(struct sys_device *dev, u32 state)
551 if (!apic_pm_state.active)
554 apic_pm_state.apic_id = apic_read(APIC_ID);
555 apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
556 apic_pm_state.apic_ldr = apic_read(APIC_LDR);
557 apic_pm_state.apic_dfr = apic_read(APIC_DFR);
558 apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
559 apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
560 apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
561 apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
562 apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
563 apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
564 apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
565 apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
566 apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
568 local_irq_save(flags);
569 disable_local_APIC();
570 local_irq_restore(flags);
574 static int lapic_resume(struct sys_device *dev)
579 if (!apic_pm_state.active)
582 local_irq_save(flags);
585 * Make sure the APICBASE points to the right address
587 * FIXME! This will be wrong if we ever support suspend on
588 * SMP! We'll need to do this as part of the CPU restore!
590 rdmsr(MSR_IA32_APICBASE, l, h);
591 l &= ~MSR_IA32_APICBASE_BASE;
592 l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
593 wrmsr(MSR_IA32_APICBASE, l, h);
595 apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
596 apic_write(APIC_ID, apic_pm_state.apic_id);
597 apic_write(APIC_DFR, apic_pm_state.apic_dfr);
598 apic_write(APIC_LDR, apic_pm_state.apic_ldr);
599 apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
600 apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
601 apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
602 apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
603 apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
604 apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
605 apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
606 apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
607 apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
608 apic_write(APIC_ESR, 0);
610 apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
611 apic_write(APIC_ESR, 0);
613 local_irq_restore(flags);
618 static struct sysdev_class lapic_sysclass = {
619 set_kset_name("lapic"),
620 .resume = lapic_resume,
621 .suspend = lapic_suspend,
624 static struct sys_device device_lapic = {
626 .cls = &lapic_sysclass,
629 static void __init apic_pm_activate(void)
631 apic_pm_state.active = 1;
634 static int __init init_lapic_sysfs(void)
640 /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
642 error = sysdev_class_register(&lapic_sysclass);
644 error = sysdev_register(&device_lapic);
647 device_initcall(init_lapic_sysfs);
649 #else /* CONFIG_PM */
651 static void apic_pm_activate(void) { }
653 #endif /* CONFIG_PM */
656 * Detect and enable local APICs on non-SMP boards.
657 * Original code written by Keir Fraser.
661 * Knob to control our willingness to enable the local APIC.
663 int enable_local_apic __initdata = 0; /* -1=force-disable, +1=force-enable */
665 static int __init lapic_disable(char *str)
667 enable_local_apic = -1;
668 clear_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
671 __setup("nolapic", lapic_disable);
673 static int __init lapic_enable(char *str)
675 enable_local_apic = 1;
678 __setup("lapic", lapic_enable);
680 static int __init apic_set_verbosity(char *str)
682 if (strcmp("debug", str) == 0)
683 apic_verbosity = APIC_DEBUG;
684 else if (strcmp("verbose", str) == 0)
685 apic_verbosity = APIC_VERBOSE;
687 printk(KERN_WARNING "APIC Verbosity level %s not recognised"
688 " use apic=verbose or apic=debug", str);
693 __setup("apic=", apic_set_verbosity);
695 static int __init detect_init_APIC (void)
698 extern void get_cpu_vendor(struct cpuinfo_x86*);
700 /* Disabled by DMI scan or kernel option? */
701 if (enable_local_apic < 0)
704 /* Workaround for us being called before identify_cpu(). */
705 get_cpu_vendor(&boot_cpu_data);
707 switch (boot_cpu_data.x86_vendor) {
709 if ((boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model > 1) ||
710 (boot_cpu_data.x86 == 15))
713 case X86_VENDOR_INTEL:
714 if (boot_cpu_data.x86 == 6 ||
715 (boot_cpu_data.x86 == 15 && (cpu_has_apic || enable_local_apic > 0)) ||
716 (boot_cpu_data.x86 == 5 && cpu_has_apic))
725 * Over-ride BIOS and try to enable LAPIC
726 * only if "lapic" specified
728 if (enable_local_apic != 1)
731 * Some BIOSes disable the local APIC in the
732 * APIC_BASE MSR. This can only be done in
733 * software for Intel P6 and AMD K7 (Model > 1).
735 rdmsr(MSR_IA32_APICBASE, l, h);
736 if (!(l & MSR_IA32_APICBASE_ENABLE)) {
737 apic_printk(APIC_VERBOSE, "Local APIC disabled "
738 "by BIOS -- reenabling.\n");
739 l &= ~MSR_IA32_APICBASE_BASE;
740 l |= MSR_IA32_APICBASE_ENABLE | APIC_DEFAULT_PHYS_BASE;
741 wrmsr(MSR_IA32_APICBASE, l, h);
742 enabled_via_apicbase = 1;
746 * The APIC feature bit should now be enabled
749 features = cpuid_edx(1);
750 if (!(features & (1 << X86_FEATURE_APIC))) {
751 printk("Could not enable APIC!\n");
754 set_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
755 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
757 /* The BIOS may have set up the APIC at some other address */
758 rdmsr(MSR_IA32_APICBASE, l, h);
759 if (l & MSR_IA32_APICBASE_ENABLE)
760 mp_lapic_addr = l & MSR_IA32_APICBASE_BASE;
762 if (nmi_watchdog != NMI_NONE)
763 nmi_watchdog = NMI_LOCAL_APIC;
765 apic_printk(APIC_VERBOSE, "Found and enabled local APIC!\n");
772 printk("No local APIC present or hardware disabled\n");
776 void __init init_apic_mappings(void)
778 unsigned long apic_phys;
781 * If no local APIC can be found then set up a fake all
782 * zeroes page to simulate the local APIC and another
783 * one for the IO-APIC.
785 if (!smp_found_config && detect_init_APIC()) {
786 apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
787 apic_phys = __pa(apic_phys);
789 apic_phys = mp_lapic_addr;
791 set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
792 apic_printk(APIC_DEBUG, "mapped APIC to %08lx (%08lx)\n", APIC_BASE,
796 * Fetch the APIC ID of the BSP in case we have a
797 * default configuration (or the MP table is broken).
799 if (boot_cpu_physical_apicid == -1U)
800 boot_cpu_physical_apicid = GET_APIC_ID(apic_read(APIC_ID));
802 #ifdef CONFIG_X86_IO_APIC
804 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
807 for (i = 0; i < nr_ioapics; i++) {
808 if (smp_found_config) {
809 ioapic_phys = mp_ioapics[i].mpc_apicaddr;
811 printk(KERN_ERR "WARNING: bogus zero IO-APIC address found in MPTABLE, disabling IO/APIC support!\n");
813 smp_found_config = 0;
814 skip_ioapic_setup = 1;
815 goto fake_ioapic_page;
819 ioapic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
820 ioapic_phys = __pa(ioapic_phys);
822 set_fixmap_nocache(idx, ioapic_phys);
823 apic_printk(APIC_DEBUG, "mapped IOAPIC to "
825 __fix_to_virt(idx), ioapic_phys);
833 * This part sets up the APIC 32 bit clock in LVTT1, with HZ interrupts
834 * per second. We assume that the caller has already set up the local
837 * The APIC timer is not exactly sync with the external timer chip, it
838 * closely follows bus clocks.
842 * The timer chip is already set up at HZ interrupts per second here,
843 * but we do not accept timer interrupts yet. We only allow the BP
846 static unsigned int __init get_8254_timer_count(void)
848 extern spinlock_t i8253_lock;
853 spin_lock_irqsave(&i8253_lock, flags);
855 outb_p(0x00, PIT_MODE);
856 count = inb_p(PIT_CH0);
857 count |= inb_p(PIT_CH0) << 8;
859 spin_unlock_irqrestore(&i8253_lock, flags);
864 /* next tick in 8254 can be caught by catching timer wraparound */
865 static void __init wait_8254_wraparound(void)
867 unsigned int curr_count, prev_count=~0;
870 curr_count = get_8254_timer_count();
873 prev_count = curr_count;
874 curr_count = get_8254_timer_count();
875 delta = curr_count-prev_count;
878 * This limit for delta seems arbitrary, but it isn't, it's
879 * slightly above the level of error a buggy Mercury/Neptune
880 * chipset timer can cause.
883 } while (delta < 300);
887 * Default initialization for 8254 timers. If we use other timers like HPET,
888 * we override this later
890 void (*wait_timer_tick)(void) = wait_8254_wraparound;
893 * This function sets up the local APIC timer, with a timeout of
894 * 'clocks' APIC bus clock. During calibration we actually call
895 * this function twice on the boot CPU, once with a bogus timeout
896 * value, second time for real. The other (noncalibrating) CPUs
897 * call this function only once, with the real, calibrated value.
899 * We do reads before writes even if unnecessary, to get around the
900 * P5 APIC double write bug.
903 #define APIC_DIVISOR 16
905 void __setup_APIC_LVTT(unsigned int clocks)
907 unsigned int lvtt_value, tmp_value, ver;
909 ver = GET_APIC_VERSION(apic_read(APIC_LVR));
910 lvtt_value = APIC_LVT_TIMER_PERIODIC | LOCAL_TIMER_VECTOR;
911 if (!APIC_INTEGRATED(ver))
912 lvtt_value |= SET_APIC_TIMER_BASE(APIC_TIMER_BASE_DIV);
913 apic_write_around(APIC_LVTT, lvtt_value);
918 tmp_value = apic_read(APIC_TDCR);
919 apic_write_around(APIC_TDCR, (tmp_value
920 & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE))
923 apic_write_around(APIC_TMICT, clocks/APIC_DIVISOR);
926 static void setup_APIC_timer(unsigned int clocks)
930 local_irq_save(flags);
933 * Wait for IRQ0's slice:
937 __setup_APIC_LVTT(clocks);
939 local_irq_restore(flags);
943 * In this function we calibrate APIC bus clocks to the external
944 * timer. Unfortunately we cannot use jiffies and the timer irq
945 * to calibrate, since some later bootup code depends on getting
946 * the first irq? Ugh.
948 * We want to do the calibration only once since we
949 * want to have local timer irqs syncron. CPUs connected
950 * by the same APIC bus have the very same bus frequency.
951 * And we want to have irqs off anyways, no accidental
955 int __init calibrate_APIC_clock(void)
957 unsigned long long t1 = 0, t2 = 0;
961 const int LOOPS = HZ/10;
963 apic_printk(APIC_VERBOSE, "calibrating APIC timer ...\n");
966 * Put whatever arbitrary (but long enough) timeout
967 * value into the APIC clock, we just want to get the
968 * counter running for calibration.
970 __setup_APIC_LVTT(1000000000);
973 * The timer chip counts down to zero. Let's wait
974 * for a wraparound to start exact measurement:
975 * (the current tick might have been already half done)
981 * We wrapped around just now. Let's start:
985 tt1 = apic_read(APIC_TMCCT);
988 * Let's wait LOOPS wraprounds:
990 for (i = 0; i < LOOPS; i++)
993 tt2 = apic_read(APIC_TMCCT);
998 * The APIC bus clock counter is 32 bits only, it
999 * might have overflown, but note that we use signed
1000 * longs, thus no extra care needed.
1002 * underflown to be exact, as the timer counts down ;)
1005 result = (tt1-tt2)*APIC_DIVISOR/LOOPS;
1008 apic_printk(APIC_VERBOSE, "..... CPU clock speed is "
1010 ((long)(t2-t1)/LOOPS)/(1000000/HZ),
1011 ((long)(t2-t1)/LOOPS)%(1000000/HZ));
1013 apic_printk(APIC_VERBOSE, "..... host bus clock speed is "
1015 result/(1000000/HZ),
1016 result%(1000000/HZ));
1021 static unsigned int calibration_result;
1023 void __init setup_boot_APIC_clock(void)
1025 apic_printk(APIC_VERBOSE, "Using local APIC timer interrupts.\n");
1026 using_apic_timer = 1;
1028 local_irq_disable();
1030 calibration_result = calibrate_APIC_clock();
1032 * Now set up the timer for real.
1034 setup_APIC_timer(calibration_result);
1039 void __init setup_secondary_APIC_clock(void)
1041 local_irq_disable(); /* FIXME: Do we need this? --RR */
1042 setup_APIC_timer(calibration_result);
1046 void __init disable_APIC_timer(void)
1048 if (using_apic_timer) {
1051 v = apic_read(APIC_LVTT);
1052 apic_write_around(APIC_LVTT, v | APIC_LVT_MASKED);
1056 void enable_APIC_timer(void)
1058 if (using_apic_timer) {
1061 v = apic_read(APIC_LVTT);
1062 apic_write_around(APIC_LVTT, v & ~APIC_LVT_MASKED);
1067 * the frequency of the profiling timer can be changed
1068 * by writing a multiplier value into /proc/profile.
1070 int setup_profiling_timer(unsigned int multiplier)
1075 * Sanity check. [at least 500 APIC cycles should be
1076 * between APIC interrupts as a rule of thumb, to avoid
1079 if ( (!multiplier) || (calibration_result/multiplier < 500))
1083 * Set the new multiplier for each CPU. CPUs don't start using the
1084 * new values until the next timer interrupt in which they do process
1085 * accounting. At that time they also adjust their APIC timers
1088 for (i = 0; i < NR_CPUS; ++i)
1089 per_cpu(prof_multiplier, i) = multiplier;
1097 * Local timer interrupt handler. It does both profiling and
1098 * process statistics/rescheduling.
1100 * We do profiling in every local tick, statistics/rescheduling
1101 * happen only every 'profiling multiplier' ticks. The default
1102 * multiplier is 1 and it can be changed by writing the new multiplier
1103 * value into /proc/profile.
1106 inline void smp_local_timer_interrupt(struct pt_regs * regs)
1108 int cpu = smp_processor_id();
1110 profile_tick(CPU_PROFILING, regs);
1111 if (--per_cpu(prof_counter, cpu) <= 0) {
1113 * The multiplier may have changed since the last time we got
1114 * to this point as a result of the user writing to
1115 * /proc/profile. In this case we need to adjust the APIC
1116 * timer accordingly.
1118 * Interrupts are already masked off at this point.
1120 per_cpu(prof_counter, cpu) = per_cpu(prof_multiplier, cpu);
1121 if (per_cpu(prof_counter, cpu) !=
1122 per_cpu(prof_old_multiplier, cpu)) {
1125 per_cpu(prof_counter, cpu));
1126 per_cpu(prof_old_multiplier, cpu) =
1127 per_cpu(prof_counter, cpu);
1131 update_process_times(user_mode(regs));
1136 * We take the 'long' return path, and there every subsystem
1137 * grabs the apropriate locks (kernel lock/ irq lock).
1139 * we might want to decouple profiling from the 'long path',
1140 * and do the profiling totally in assembly.
1142 * Currently this isn't too much of an issue (performance wise),
1143 * we can take more than 100K local irqs per second on a 100 MHz P5.
1148 * Local APIC timer interrupt. This is the most natural way for doing
1149 * local interrupts, but local timer interrupts can be emulated by
1150 * broadcast interrupts too. [in case the hw doesn't support APIC timers]
1152 * [ if a single-CPU system runs an SMP kernel then we call the local
1153 * interrupt as well. Thus we cannot inline the local irq ... ]
1156 void smp_apic_timer_interrupt(struct pt_regs regs)
1158 int cpu = smp_processor_id();
1161 * the NMI deadlock-detector uses this.
1163 irq_stat[cpu].apic_timer_irqs++;
1166 * NOTE! We'd better ACK the irq immediately,
1167 * because timer handling can be slow.
1171 * update_process_times() expects us to have done irq_enter().
1172 * Besides, if we don't timer interrupts ignore the global
1173 * interrupt lock, which is the WrongThing (tm) to do.
1176 smp_local_timer_interrupt(®s);
1181 * This interrupt should _never_ happen with our APIC/SMP architecture
1183 asmlinkage void smp_spurious_interrupt(void)
1189 * Check if this really is a spurious interrupt and ACK it
1190 * if it is a vectored one. Just in case...
1191 * Spurious interrupts should not be ACKed.
1193 v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
1194 if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
1197 /* see sw-dev-man vol 3, chapter 7.4.13.5 */
1198 printk(KERN_INFO "spurious APIC interrupt on CPU#%d, should never happen.\n",
1199 smp_processor_id());
1204 * This interrupt should never happen with our APIC/SMP architecture
1207 asmlinkage void smp_error_interrupt(void)
1209 unsigned long v, v1;
1212 /* First tickle the hardware, only then report what went on. -- REW */
1213 v = apic_read(APIC_ESR);
1214 apic_write(APIC_ESR, 0);
1215 v1 = apic_read(APIC_ESR);
1217 atomic_inc(&irq_err_count);
1219 /* Here is what the APIC error bits mean:
1222 2: Send accept error
1223 3: Receive accept error
1225 5: Send illegal vector
1226 6: Received illegal vector
1227 7: Illegal register address
1229 printk (KERN_DEBUG "APIC error on CPU%d: %02lx(%02lx)\n",
1230 smp_processor_id(), v , v1);
1235 * This initializes the IO-APIC and APIC hardware if this is
1238 int __init APIC_init_uniprocessor (void)
1240 if (enable_local_apic < 0)
1241 clear_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
1243 if (!smp_found_config && !cpu_has_apic)
1247 * Complain if the BIOS pretends there is one.
1249 if (!cpu_has_apic && APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid])) {
1250 printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
1251 boot_cpu_physical_apicid);
1255 verify_local_APIC();
1259 phys_cpu_present_map = physid_mask_of_physid(boot_cpu_physical_apicid);
1263 if (nmi_watchdog == NMI_LOCAL_APIC)
1264 check_nmi_watchdog();
1265 #ifdef CONFIG_X86_IO_APIC
1266 if (smp_found_config)
1267 if (!skip_ioapic_setup && nr_ioapics)
1270 setup_boot_APIC_clock();