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>
29 #include <linux/dmi.h>
31 #include <asm/atomic.h>
34 #include <asm/mpspec.h>
36 #include <asm/arch_hooks.h>
39 #include <mach_apic.h>
49 extern int enable_local_apic;
51 static void apic_pm_activate(void);
53 void __init apic_intr_init(void)
58 /* self generated IPI for local APIC timer */
59 set_intr_gate(LOCAL_TIMER_VECTOR, apic_timer_interrupt);
61 /* IPI vectors for APIC spurious and error interrupts */
62 set_intr_gate(SPURIOUS_APIC_VECTOR, spurious_interrupt);
63 set_intr_gate(ERROR_APIC_VECTOR, error_interrupt);
65 /* thermal monitor LVT interrupt */
66 #ifdef CONFIG_X86_MCE_P4THERMAL
67 set_intr_gate(THERMAL_APIC_VECTOR, thermal_interrupt);
71 /* Using APIC to generate smp_local_timer_interrupt? */
72 int using_apic_timer = 0;
74 static DEFINE_PER_CPU(int, prof_multiplier) = 1;
75 static DEFINE_PER_CPU(int, prof_old_multiplier) = 1;
76 static DEFINE_PER_CPU(int, prof_counter) = 1;
78 static int enabled_via_apicbase;
80 void enable_NMI_through_LVT0 (void * dummy)
84 ver = apic_read(APIC_LVR);
85 ver = GET_APIC_VERSION(ver);
86 v = APIC_DM_NMI; /* unmask and set to NMI */
87 if (!APIC_INTEGRATED(ver)) /* 82489DX */
88 v |= APIC_LVT_LEVEL_TRIGGER;
89 apic_write_around(APIC_LVT0, v);
92 int get_physical_broadcast(void)
94 unsigned int lvr, version;
95 lvr = apic_read(APIC_LVR);
96 version = GET_APIC_VERSION(lvr);
105 unsigned int v, ver, maxlvt;
107 v = apic_read(APIC_LVR);
108 ver = GET_APIC_VERSION(v);
109 /* 82489DXs do not report # of LVT entries. */
110 maxlvt = APIC_INTEGRATED(ver) ? GET_APIC_MAXLVT(v) : 2;
114 void clear_local_APIC(void)
119 maxlvt = get_maxlvt();
122 * Masking an LVT entry on a P6 can trigger a local APIC error
123 * if the vector is zero. Mask LVTERR first to prevent this.
126 v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
127 apic_write_around(APIC_LVTERR, v | APIC_LVT_MASKED);
130 * Careful: we have to set masks only first to deassert
131 * any level-triggered sources.
133 v = apic_read(APIC_LVTT);
134 apic_write_around(APIC_LVTT, v | APIC_LVT_MASKED);
135 v = apic_read(APIC_LVT0);
136 apic_write_around(APIC_LVT0, v | APIC_LVT_MASKED);
137 v = apic_read(APIC_LVT1);
138 apic_write_around(APIC_LVT1, v | APIC_LVT_MASKED);
140 v = apic_read(APIC_LVTPC);
141 apic_write_around(APIC_LVTPC, v | APIC_LVT_MASKED);
144 /* lets not touch this if we didn't frob it */
145 #ifdef CONFIG_X86_MCE_P4THERMAL
147 v = apic_read(APIC_LVTTHMR);
148 apic_write_around(APIC_LVTTHMR, v | APIC_LVT_MASKED);
152 * Clean APIC state for other OSs:
154 apic_write_around(APIC_LVTT, APIC_LVT_MASKED);
155 apic_write_around(APIC_LVT0, APIC_LVT_MASKED);
156 apic_write_around(APIC_LVT1, APIC_LVT_MASKED);
158 apic_write_around(APIC_LVTERR, APIC_LVT_MASKED);
160 apic_write_around(APIC_LVTPC, APIC_LVT_MASKED);
162 #ifdef CONFIG_X86_MCE_P4THERMAL
164 apic_write_around(APIC_LVTTHMR, APIC_LVT_MASKED);
166 v = GET_APIC_VERSION(apic_read(APIC_LVR));
167 if (APIC_INTEGRATED(v)) { /* !82489DX */
168 if (maxlvt > 3) /* Due to Pentium errata 3AP and 11AP. */
169 apic_write(APIC_ESR, 0);
174 void __init connect_bsp_APIC(void)
178 * Do not trust the local APIC being empty at bootup.
182 * PIC mode, enable APIC mode in the IMCR, i.e.
183 * connect BSP's local APIC to INT and NMI lines.
185 apic_printk(APIC_VERBOSE, "leaving PIC mode, "
186 "enabling APIC mode.\n");
193 void disconnect_bsp_APIC(void)
197 * Put the board back into PIC mode (has an effect
198 * only on certain older boards). Note that APIC
199 * interrupts, including IPIs, won't work beyond
200 * this point! The only exception are INIT IPIs.
202 apic_printk(APIC_VERBOSE, "disabling APIC mode, "
203 "entering PIC mode.\n");
209 void disable_local_APIC(void)
213 if (enable_local_apic < 0)
219 * Disable APIC (implies clearing of registers
222 value = apic_read(APIC_SPIV);
223 value &= ~APIC_SPIV_APIC_ENABLED;
224 apic_write_around(APIC_SPIV, value);
226 if (enabled_via_apicbase) {
228 rdmsr(MSR_IA32_APICBASE, l, h);
229 l &= ~MSR_IA32_APICBASE_ENABLE;
230 wrmsr(MSR_IA32_APICBASE, l, h);
235 * This is to verify that we're looking at a real local APIC.
236 * Check these against your board if the CPUs aren't getting
237 * started for no apparent reason.
239 int __init verify_local_APIC(void)
241 unsigned int reg0, reg1;
244 * The version register is read-only in a real APIC.
246 reg0 = apic_read(APIC_LVR);
247 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg0);
248 apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
249 reg1 = apic_read(APIC_LVR);
250 apic_printk(APIC_DEBUG, "Getting VERSION: %x\n", reg1);
253 * The two version reads above should print the same
254 * numbers. If the second one is different, then we
255 * poke at a non-APIC.
261 * Check if the version looks reasonably.
263 reg1 = GET_APIC_VERSION(reg0);
264 if (reg1 == 0x00 || reg1 == 0xff)
267 if (reg1 < 0x02 || reg1 == 0xff)
271 * The ID register is read/write in a real APIC.
273 reg0 = apic_read(APIC_ID);
274 apic_printk(APIC_DEBUG, "Getting ID: %x\n", reg0);
277 * The next two are just to see if we have sane values.
278 * They're only really relevant if we're in Virtual Wire
279 * compatibility mode, but most boxes are anymore.
281 reg0 = apic_read(APIC_LVT0);
282 apic_printk(APIC_DEBUG, "Getting LVT0: %x\n", reg0);
283 reg1 = apic_read(APIC_LVT1);
284 apic_printk(APIC_DEBUG, "Getting LVT1: %x\n", reg1);
289 void __init sync_Arb_IDs(void)
294 apic_wait_icr_idle();
296 apic_printk(APIC_DEBUG, "Synchronizing Arb IDs.\n");
297 apic_write_around(APIC_ICR, APIC_DEST_ALLINC | APIC_INT_LEVELTRIG
301 extern void __error_in_apic_c (void);
304 * An initial setup of the virtual wire mode.
306 void __init init_bsp_APIC(void)
308 unsigned long value, ver;
311 * Don't do the setup now if we have a SMP BIOS as the
312 * through-I/O-APIC virtual wire mode might be active.
314 if (smp_found_config || !cpu_has_apic)
317 value = apic_read(APIC_LVR);
318 ver = GET_APIC_VERSION(value);
321 * Do not trust the local APIC being empty at bootup.
328 value = apic_read(APIC_SPIV);
329 value &= ~APIC_VECTOR_MASK;
330 value |= APIC_SPIV_APIC_ENABLED;
332 /* This bit is reserved on P4/Xeon and should be cleared */
333 if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) && (boot_cpu_data.x86 == 15))
334 value &= ~APIC_SPIV_FOCUS_DISABLED;
336 value |= APIC_SPIV_FOCUS_DISABLED;
337 value |= SPURIOUS_APIC_VECTOR;
338 apic_write_around(APIC_SPIV, value);
341 * Set up the virtual wire mode.
343 apic_write_around(APIC_LVT0, APIC_DM_EXTINT);
345 if (!APIC_INTEGRATED(ver)) /* 82489DX */
346 value |= APIC_LVT_LEVEL_TRIGGER;
347 apic_write_around(APIC_LVT1, value);
350 void __init setup_local_APIC (void)
352 unsigned long value, ver, maxlvt;
354 /* Pound the ESR really hard over the head with a big hammer - mbligh */
356 apic_write(APIC_ESR, 0);
357 apic_write(APIC_ESR, 0);
358 apic_write(APIC_ESR, 0);
359 apic_write(APIC_ESR, 0);
362 value = apic_read(APIC_LVR);
363 ver = GET_APIC_VERSION(value);
365 if ((SPURIOUS_APIC_VECTOR & 0x0f) != 0x0f)
369 * Double-check whether this APIC is really registered.
371 if (!apic_id_registered())
375 * Intel recommends to set DFR, LDR and TPR before enabling
376 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
377 * document number 292116). So here it goes...
382 * Set Task Priority to 'accept all'. We never change this
385 value = apic_read(APIC_TASKPRI);
386 value &= ~APIC_TPRI_MASK;
387 apic_write_around(APIC_TASKPRI, value);
390 * Now that we are all set up, enable the APIC
392 value = apic_read(APIC_SPIV);
393 value &= ~APIC_VECTOR_MASK;
397 value |= APIC_SPIV_APIC_ENABLED;
400 * Some unknown Intel IO/APIC (or APIC) errata is biting us with
401 * certain networking cards. If high frequency interrupts are
402 * happening on a particular IOAPIC pin, plus the IOAPIC routing
403 * entry is masked/unmasked at a high rate as well then sooner or
404 * later IOAPIC line gets 'stuck', no more interrupts are received
405 * from the device. If focus CPU is disabled then the hang goes
408 * [ This bug can be reproduced easily with a level-triggered
409 * PCI Ne2000 networking cards and PII/PIII processors, dual
413 * Actually disabling the focus CPU check just makes the hang less
414 * frequent as it makes the interrupt distributon model be more
415 * like LRU than MRU (the short-term load is more even across CPUs).
416 * See also the comment in end_level_ioapic_irq(). --macro
419 /* Enable focus processor (bit==0) */
420 value &= ~APIC_SPIV_FOCUS_DISABLED;
422 /* Disable focus processor (bit==1) */
423 value |= APIC_SPIV_FOCUS_DISABLED;
426 * Set spurious IRQ vector
428 value |= SPURIOUS_APIC_VECTOR;
429 apic_write_around(APIC_SPIV, value);
434 * set up through-local-APIC on the BP's LINT0. This is not
435 * strictly necessery in pure symmetric-IO mode, but sometimes
436 * we delegate interrupts to the 8259A.
439 * TODO: set up through-local-APIC from through-I/O-APIC? --macro
441 value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
442 if (!smp_processor_id() && (pic_mode || !value)) {
443 value = APIC_DM_EXTINT;
444 apic_printk(APIC_VERBOSE, "enabled ExtINT on CPU#%d\n",
447 value = APIC_DM_EXTINT | APIC_LVT_MASKED;
448 apic_printk(APIC_VERBOSE, "masked ExtINT on CPU#%d\n",
451 apic_write_around(APIC_LVT0, value);
454 * only the BP should see the LINT1 NMI signal, obviously.
456 if (!smp_processor_id())
459 value = APIC_DM_NMI | APIC_LVT_MASKED;
460 if (!APIC_INTEGRATED(ver)) /* 82489DX */
461 value |= APIC_LVT_LEVEL_TRIGGER;
462 apic_write_around(APIC_LVT1, value);
464 if (APIC_INTEGRATED(ver) && !esr_disable) { /* !82489DX */
465 maxlvt = get_maxlvt();
466 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
467 apic_write(APIC_ESR, 0);
468 value = apic_read(APIC_ESR);
469 apic_printk(APIC_VERBOSE, "ESR value before enabling vector:"
472 value = ERROR_APIC_VECTOR; // enables sending errors
473 apic_write_around(APIC_LVTERR, value);
475 * spec says clear errors after enabling vector.
478 apic_write(APIC_ESR, 0);
479 value = apic_read(APIC_ESR);
480 apic_printk(APIC_VERBOSE, "ESR value after enabling vector:"
485 * Something untraceble is creating bad interrupts on
486 * secondary quads ... for the moment, just leave the
487 * ESR disabled - we can't do anything useful with the
488 * errors anyway - mbligh
490 printk("Leaving ESR disabled.\n");
492 printk("No ESR for 82489DX.\n");
495 if (nmi_watchdog == NMI_LOCAL_APIC)
496 setup_apic_nmi_watchdog();
503 /* 'active' is true if the local APIC was enabled by us and
504 not the BIOS; this signifies that we are also responsible
505 for disabling it before entering apm/acpi suspend */
507 /* r/w apic fields */
508 unsigned int apic_id;
509 unsigned int apic_taskpri;
510 unsigned int apic_ldr;
511 unsigned int apic_dfr;
512 unsigned int apic_spiv;
513 unsigned int apic_lvtt;
514 unsigned int apic_lvtpc;
515 unsigned int apic_lvt0;
516 unsigned int apic_lvt1;
517 unsigned int apic_lvterr;
518 unsigned int apic_tmict;
519 unsigned int apic_tdcr;
520 unsigned int apic_thmr;
523 static int lapic_suspend(struct sys_device *dev, u32 state)
527 if (!apic_pm_state.active)
530 apic_pm_state.apic_id = apic_read(APIC_ID);
531 apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
532 apic_pm_state.apic_ldr = apic_read(APIC_LDR);
533 apic_pm_state.apic_dfr = apic_read(APIC_DFR);
534 apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
535 apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
536 apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
537 apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
538 apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
539 apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
540 apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
541 apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
542 apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
544 local_irq_save(flags);
545 disable_local_APIC();
546 local_irq_restore(flags);
550 static int lapic_resume(struct sys_device *dev)
555 if (!apic_pm_state.active)
558 local_irq_save(flags);
561 * Make sure the APICBASE points to the right address
563 * FIXME! This will be wrong if we ever support suspend on
564 * SMP! We'll need to do this as part of the CPU restore!
566 rdmsr(MSR_IA32_APICBASE, l, h);
567 l &= ~MSR_IA32_APICBASE_BASE;
568 l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
569 wrmsr(MSR_IA32_APICBASE, l, h);
571 apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
572 apic_write(APIC_ID, apic_pm_state.apic_id);
573 apic_write(APIC_DFR, apic_pm_state.apic_dfr);
574 apic_write(APIC_LDR, apic_pm_state.apic_ldr);
575 apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
576 apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
577 apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
578 apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
579 apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
580 apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
581 apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
582 apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
583 apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
584 apic_write(APIC_ESR, 0);
586 apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
587 apic_write(APIC_ESR, 0);
589 local_irq_restore(flags);
594 static struct sysdev_class lapic_sysclass = {
595 set_kset_name("lapic"),
596 .resume = lapic_resume,
597 .suspend = lapic_suspend,
600 static struct sys_device device_lapic = {
602 .cls = &lapic_sysclass,
605 static void __init apic_pm_activate(void)
607 apic_pm_state.active = 1;
610 static int __init init_lapic_sysfs(void)
616 /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
618 error = sysdev_class_register(&lapic_sysclass);
620 error = sysdev_register(&device_lapic);
623 device_initcall(init_lapic_sysfs);
625 #else /* CONFIG_PM */
627 static void apic_pm_activate(void) { }
629 #endif /* CONFIG_PM */
632 * Detect and enable local APICs on non-SMP boards.
633 * Original code written by Keir Fraser.
637 * Knob to control our willingness to enable the local APIC.
639 /* For SuSE don't enable APIC by default on UP kernels */
641 int enable_local_apic = -1; /* -1=force-disable, +1=force-enable */
643 int enable_local_apic = 0;
646 int __init lapic_disable(char *str)
648 enable_local_apic = -1;
649 clear_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
652 __setup("nolapic", lapic_disable);
654 int __init lapic_enable(char *str)
656 enable_local_apic = 1;
659 __setup("lapic", lapic_enable);
661 static int __init apic_set_verbosity(char *str)
663 if (strcmp("debug", str) == 0)
664 apic_verbosity = APIC_DEBUG;
665 else if (strcmp("verbose", str) == 0)
666 apic_verbosity = APIC_VERBOSE;
668 printk(KERN_WARNING "APIC Verbosity level %s not recognised"
669 " use apic=verbose or apic=debug", str);
674 __setup("apic=", apic_set_verbosity);
676 int __init apic_enable(char *str)
678 printk("apic_enable\n");
680 #ifdef CONFIG_X86_IO_APIC
681 extern int skip_ioapic_setup;
682 skip_ioapic_setup = 0;
684 enable_local_apic = 1;
687 __setup("apic", apic_enable);
689 static int __init detect_init_APIC (void)
692 extern void get_cpu_vendor(struct cpuinfo_x86*);
694 /* Disabled by DMI scan or kernel option? */
695 if (enable_local_apic < 0)
698 /* Workaround for us being called before identify_cpu(). */
699 get_cpu_vendor(&boot_cpu_data);
701 switch (boot_cpu_data.x86_vendor) {
703 if ((boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model > 1) ||
704 (boot_cpu_data.x86 == 15))
707 case X86_VENDOR_INTEL:
708 if (boot_cpu_data.x86 == 6 ||
709 (boot_cpu_data.x86 == 15 && (cpu_has_apic || enable_local_apic > 0)) ||
710 (boot_cpu_data.x86 == 5 && cpu_has_apic))
719 * Some BIOSes disable the local APIC in the
720 * APIC_BASE MSR. This can only be done in
721 * software for Intel P6 and AMD K7 (Model > 1).
723 rdmsr(MSR_IA32_APICBASE, l, h);
724 if (!(l & MSR_IA32_APICBASE_ENABLE)) {
725 apic_printk(APIC_VERBOSE, "Local APIC disabled "
726 "by BIOS -- reenabling.\n");
727 l &= ~MSR_IA32_APICBASE_BASE;
728 l |= MSR_IA32_APICBASE_ENABLE | APIC_DEFAULT_PHYS_BASE;
729 wrmsr(MSR_IA32_APICBASE, l, h);
730 enabled_via_apicbase = 1;
734 * The APIC feature bit should now be enabled
737 features = cpuid_edx(1);
738 if (!(features & (1 << X86_FEATURE_APIC))) {
739 printk("Could not enable APIC!\n");
742 set_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
743 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
745 /* The BIOS may have set up the APIC at some other address */
746 rdmsr(MSR_IA32_APICBASE, l, h);
747 if (l & MSR_IA32_APICBASE_ENABLE)
748 mp_lapic_addr = l & MSR_IA32_APICBASE_BASE;
750 if (nmi_watchdog != NMI_NONE)
751 nmi_watchdog = NMI_LOCAL_APIC;
753 apic_printk(APIC_VERBOSE, "Found and enabled local APIC!\n");
760 printk("No local APIC present or hardware disabled\n");
764 void __init init_apic_mappings(void)
766 unsigned long apic_phys;
769 * If no local APIC can be found then set up a fake all
770 * zeroes page to simulate the local APIC and another
771 * one for the IO-APIC.
773 if (!smp_found_config && detect_init_APIC()) {
774 apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
775 apic_phys = __pa(apic_phys);
777 apic_phys = mp_lapic_addr;
779 set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
780 apic_printk(APIC_DEBUG, "mapped APIC to %08lx (%08lx)\n", APIC_BASE,
784 * Fetch the APIC ID of the BSP in case we have a
785 * default configuration (or the MP table is broken).
787 if (boot_cpu_physical_apicid == -1U)
788 boot_cpu_physical_apicid = GET_APIC_ID(apic_read(APIC_ID));
790 #ifdef CONFIG_X86_IO_APIC
792 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
795 for (i = 0; i < nr_ioapics; i++) {
796 if (smp_found_config) {
797 ioapic_phys = mp_ioapics[i].mpc_apicaddr;
799 printk(KERN_ERR "WARNING: bogus zero IO-APIC address found in MPTABLE, disabling IO/APIC support!\n");
801 smp_found_config = 0;
802 skip_ioapic_setup = 1;
803 goto fake_ioapic_page;
807 ioapic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
808 ioapic_phys = __pa(ioapic_phys);
810 set_fixmap_nocache(idx, ioapic_phys);
811 apic_printk(APIC_DEBUG, "mapped IOAPIC to "
813 __fix_to_virt(idx), ioapic_phys);
821 * This part sets up the APIC 32 bit clock in LVTT1, with HZ interrupts
822 * per second. We assume that the caller has already set up the local
825 * The APIC timer is not exactly sync with the external timer chip, it
826 * closely follows bus clocks.
830 * The timer chip is already set up at HZ interrupts per second here,
831 * but we do not accept timer interrupts yet. We only allow the BP
834 static unsigned int __init get_8254_timer_count(void)
836 extern spinlock_t i8253_lock;
841 spin_lock_irqsave(&i8253_lock, flags);
843 outb_p(0x00, PIT_MODE);
844 count = inb_p(PIT_CH0);
845 count |= inb_p(PIT_CH0) << 8;
847 spin_unlock_irqrestore(&i8253_lock, flags);
852 /* next tick in 8254 can be caught by catching timer wraparound */
853 static void __init wait_8254_wraparound(void)
855 unsigned int curr_count, prev_count=~0;
858 curr_count = get_8254_timer_count();
861 prev_count = curr_count;
862 curr_count = get_8254_timer_count();
863 delta = curr_count-prev_count;
866 * This limit for delta seems arbitrary, but it isn't, it's
867 * slightly above the level of error a buggy Mercury/Neptune
868 * chipset timer can cause.
871 } while (delta < 300);
875 * Default initialization for 8254 timers. If we use other timers like HPET,
876 * we override this later
878 void (*wait_timer_tick)(void) = wait_8254_wraparound;
881 * This function sets up the local APIC timer, with a timeout of
882 * 'clocks' APIC bus clock. During calibration we actually call
883 * this function twice on the boot CPU, once with a bogus timeout
884 * value, second time for real. The other (noncalibrating) CPUs
885 * call this function only once, with the real, calibrated value.
887 * We do reads before writes even if unnecessary, to get around the
888 * P5 APIC double write bug.
891 #define APIC_DIVISOR 16
893 void __setup_APIC_LVTT(unsigned int clocks)
895 unsigned int lvtt_value, tmp_value, ver;
897 ver = GET_APIC_VERSION(apic_read(APIC_LVR));
898 lvtt_value = APIC_LVT_TIMER_PERIODIC | LOCAL_TIMER_VECTOR;
899 if (!APIC_INTEGRATED(ver))
900 lvtt_value |= SET_APIC_TIMER_BASE(APIC_TIMER_BASE_DIV);
901 apic_write_around(APIC_LVTT, lvtt_value);
906 tmp_value = apic_read(APIC_TDCR);
907 apic_write_around(APIC_TDCR, (tmp_value
908 & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE))
911 apic_write_around(APIC_TMICT, clocks/APIC_DIVISOR);
914 static void setup_APIC_timer(unsigned int clocks)
918 local_irq_save(flags);
921 * Wait for IRQ0's slice:
925 __setup_APIC_LVTT(clocks);
927 local_irq_restore(flags);
931 * In this function we calibrate APIC bus clocks to the external
932 * timer. Unfortunately we cannot use jiffies and the timer irq
933 * to calibrate, since some later bootup code depends on getting
934 * the first irq? Ugh.
936 * We want to do the calibration only once since we
937 * want to have local timer irqs syncron. CPUs connected
938 * by the same APIC bus have the very same bus frequency.
939 * And we want to have irqs off anyways, no accidental
943 int __init calibrate_APIC_clock(void)
945 unsigned long long t1 = 0, t2 = 0;
949 const int LOOPS = HZ/10;
951 apic_printk(APIC_VERBOSE, "calibrating APIC timer ...\n");
954 * Put whatever arbitrary (but long enough) timeout
955 * value into the APIC clock, we just want to get the
956 * counter running for calibration.
958 __setup_APIC_LVTT(1000000000);
961 * The timer chip counts down to zero. Let's wait
962 * for a wraparound to start exact measurement:
963 * (the current tick might have been already half done)
969 * We wrapped around just now. Let's start:
973 tt1 = apic_read(APIC_TMCCT);
976 * Let's wait LOOPS wraprounds:
978 for (i = 0; i < LOOPS; i++)
981 tt2 = apic_read(APIC_TMCCT);
986 * The APIC bus clock counter is 32 bits only, it
987 * might have overflown, but note that we use signed
988 * longs, thus no extra care needed.
990 * underflown to be exact, as the timer counts down ;)
993 result = (tt1-tt2)*APIC_DIVISOR/LOOPS;
996 apic_printk(APIC_VERBOSE, "..... CPU clock speed is "
998 ((long)(t2-t1)/LOOPS)/(1000000/HZ),
999 ((long)(t2-t1)/LOOPS)%(1000000/HZ));
1001 apic_printk(APIC_VERBOSE, "..... host bus clock speed is "
1003 result/(1000000/HZ),
1004 result%(1000000/HZ));
1009 static unsigned int calibration_result;
1011 void __init setup_boot_APIC_clock(void)
1013 apic_printk(APIC_VERBOSE, "Using local APIC timer interrupts.\n");
1014 using_apic_timer = 1;
1016 local_irq_disable();
1018 calibration_result = calibrate_APIC_clock();
1020 * Now set up the timer for real.
1022 setup_APIC_timer(calibration_result);
1027 void __init setup_secondary_APIC_clock(void)
1029 local_irq_disable(); /* FIXME: Do we need this? --RR */
1030 setup_APIC_timer(calibration_result);
1034 void __init disable_APIC_timer(void)
1036 if (using_apic_timer) {
1039 v = apic_read(APIC_LVTT);
1040 apic_write_around(APIC_LVTT, v | APIC_LVT_MASKED);
1044 void enable_APIC_timer(void)
1046 if (using_apic_timer) {
1049 v = apic_read(APIC_LVTT);
1050 apic_write_around(APIC_LVTT, v & ~APIC_LVT_MASKED);
1055 * the frequency of the profiling timer can be changed
1056 * by writing a multiplier value into /proc/profile.
1058 int setup_profiling_timer(unsigned int multiplier)
1063 * Sanity check. [at least 500 APIC cycles should be
1064 * between APIC interrupts as a rule of thumb, to avoid
1067 if ( (!multiplier) || (calibration_result/multiplier < 500))
1071 * Set the new multiplier for each CPU. CPUs don't start using the
1072 * new values until the next timer interrupt in which they do process
1073 * accounting. At that time they also adjust their APIC timers
1076 for (i = 0; i < NR_CPUS; ++i)
1077 per_cpu(prof_multiplier, i) = multiplier;
1085 * Local timer interrupt handler. It does both profiling and
1086 * process statistics/rescheduling.
1088 * We do profiling in every local tick, statistics/rescheduling
1089 * happen only every 'profiling multiplier' ticks. The default
1090 * multiplier is 1 and it can be changed by writing the new multiplier
1091 * value into /proc/profile.
1094 inline void smp_local_timer_interrupt(struct pt_regs * regs)
1096 int cpu = smp_processor_id();
1098 x86_do_profile(regs);
1100 if (--per_cpu(prof_counter, cpu) <= 0) {
1102 * The multiplier may have changed since the last time we got
1103 * to this point as a result of the user writing to
1104 * /proc/profile. In this case we need to adjust the APIC
1105 * timer accordingly.
1107 * Interrupts are already masked off at this point.
1109 per_cpu(prof_counter, cpu) = per_cpu(prof_multiplier, cpu);
1110 if (per_cpu(prof_counter, cpu) !=
1111 per_cpu(prof_old_multiplier, cpu)) {
1114 per_cpu(prof_counter, cpu));
1115 per_cpu(prof_old_multiplier, cpu) =
1116 per_cpu(prof_counter, cpu);
1120 update_process_times(user_mode(regs));
1125 * We take the 'long' return path, and there every subsystem
1126 * grabs the apropriate locks (kernel lock/ irq lock).
1128 * we might want to decouple profiling from the 'long path',
1129 * and do the profiling totally in assembly.
1131 * Currently this isn't too much of an issue (performance wise),
1132 * we can take more than 100K local irqs per second on a 100 MHz P5.
1137 * Local APIC timer interrupt. This is the most natural way for doing
1138 * local interrupts, but local timer interrupts can be emulated by
1139 * broadcast interrupts too. [in case the hw doesn't support APIC timers]
1141 * [ if a single-CPU system runs an SMP kernel then we call the local
1142 * interrupt as well. Thus we cannot inline the local irq ... ]
1145 void smp_apic_timer_interrupt(struct pt_regs regs)
1147 int cpu = smp_processor_id();
1150 * the NMI deadlock-detector uses this.
1152 irq_stat[cpu].apic_timer_irqs++;
1155 * NOTE! We'd better ACK the irq immediately,
1156 * because timer handling can be slow.
1160 * update_process_times() expects us to have done irq_enter().
1161 * Besides, if we don't timer interrupts ignore the global
1162 * interrupt lock, which is the WrongThing (tm) to do.
1165 smp_local_timer_interrupt(®s);
1170 * This interrupt should _never_ happen with our APIC/SMP architecture
1172 asmlinkage void smp_spurious_interrupt(void)
1178 * Check if this really is a spurious interrupt and ACK it
1179 * if it is a vectored one. Just in case...
1180 * Spurious interrupts should not be ACKed.
1182 v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
1183 if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
1186 /* see sw-dev-man vol 3, chapter 7.4.13.5 */
1187 printk(KERN_INFO "spurious APIC interrupt on CPU#%d, should never happen.\n",
1188 smp_processor_id());
1193 * This interrupt should never happen with our APIC/SMP architecture
1196 asmlinkage void smp_error_interrupt(void)
1198 unsigned long v, v1;
1201 /* First tickle the hardware, only then report what went on. -- REW */
1202 v = apic_read(APIC_ESR);
1203 apic_write(APIC_ESR, 0);
1204 v1 = apic_read(APIC_ESR);
1206 atomic_inc(&irq_err_count);
1208 /* Here is what the APIC error bits mean:
1211 2: Send accept error
1212 3: Receive accept error
1214 5: Send illegal vector
1215 6: Received illegal vector
1216 7: Illegal register address
1218 printk (KERN_INFO "APIC error on CPU%d: %02lx(%02lx)\n",
1219 smp_processor_id(), v , v1);
1223 static int __init need_local_apic(struct dmi_system_id *d)
1225 #ifdef CONFIG_X86_LOCAL_APIC
1226 extern int enable_local_apic;
1227 enable_local_apic = 0;
1228 printk(KERN_WARNING "%s machine detected. Enablig LAPIC\n",
1234 static struct dmi_system_id __initdata apic_dmi_table[] = {
1235 { need_local_apic, "Intel C440GX+", {
1236 DMI_MATCH(DMI_BOARD_VENDOR,"Intel"),
1237 DMI_MATCH(DMI_BOARD_NAME,"C440GX+") } }
1241 * This initializes the IO-APIC and APIC hardware if this is
1244 int __init APIC_init_uniprocessor (void)
1246 dmi_check_system(apic_dmi_table);
1248 if (enable_local_apic < 0)
1249 clear_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
1251 if (!smp_found_config && !cpu_has_apic)
1255 * Complain if the BIOS pretends there is one.
1257 if (!cpu_has_apic && APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid])) {
1261 verify_local_APIC();
1265 phys_cpu_present_map = physid_mask_of_physid(boot_cpu_physical_apicid);
1269 if (nmi_watchdog == NMI_LOCAL_APIC)
1270 check_nmi_watchdog();
1271 #ifdef CONFIG_X86_IO_APIC
1272 if (smp_found_config)
1273 if (!skip_ioapic_setup && nr_ioapics)
1276 setup_boot_APIC_clock();