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>
34 #include <asm/pgalloc.h>
36 #include <asm/arch_hooks.h>
39 #include <mach_apic.h>
43 static void apic_pm_activate(void);
45 void __init apic_intr_init(void)
50 /* self generated IPI for local APIC timer */
51 set_intr_gate(LOCAL_TIMER_VECTOR, apic_timer_interrupt);
53 /* IPI vectors for APIC spurious and error interrupts */
54 set_intr_gate(SPURIOUS_APIC_VECTOR, spurious_interrupt);
55 set_intr_gate(ERROR_APIC_VECTOR, error_interrupt);
57 /* thermal monitor LVT interrupt */
58 #ifdef CONFIG_X86_MCE_P4THERMAL
59 set_intr_gate(THERMAL_APIC_VECTOR, thermal_interrupt);
63 /* Using APIC to generate smp_local_timer_interrupt? */
64 int using_apic_timer = 0;
66 static DEFINE_PER_CPU(int, prof_multiplier) = 1;
67 static DEFINE_PER_CPU(int, prof_old_multiplier) = 1;
68 static DEFINE_PER_CPU(int, prof_counter) = 1;
70 static int enabled_via_apicbase;
72 void enable_NMI_through_LVT0 (void * dummy)
76 ver = apic_read(APIC_LVR);
77 ver = GET_APIC_VERSION(ver);
78 v = APIC_DM_NMI; /* unmask and set to NMI */
79 if (!APIC_INTEGRATED(ver)) /* 82489DX */
80 v |= APIC_LVT_LEVEL_TRIGGER;
81 apic_write_around(APIC_LVT0, v);
86 unsigned int v, ver, maxlvt;
88 v = apic_read(APIC_LVR);
89 ver = GET_APIC_VERSION(v);
90 /* 82489DXs do not report # of LVT entries. */
91 maxlvt = APIC_INTEGRATED(ver) ? GET_APIC_MAXLVT(v) : 2;
95 void clear_local_APIC(void)
100 maxlvt = get_maxlvt();
103 * Masking an LVT entry on a P6 can trigger a local APIC error
104 * if the vector is zero. Mask LVTERR first to prevent this.
107 v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
108 apic_write_around(APIC_LVTERR, v | APIC_LVT_MASKED);
111 * Careful: we have to set masks only first to deassert
112 * any level-triggered sources.
114 v = apic_read(APIC_LVTT);
115 apic_write_around(APIC_LVTT, v | APIC_LVT_MASKED);
116 v = apic_read(APIC_LVT0);
117 apic_write_around(APIC_LVT0, v | APIC_LVT_MASKED);
118 v = apic_read(APIC_LVT1);
119 apic_write_around(APIC_LVT1, v | APIC_LVT_MASKED);
121 v = apic_read(APIC_LVTPC);
122 apic_write_around(APIC_LVTPC, v | APIC_LVT_MASKED);
125 /* lets not touch this if we didn't frob it */
126 #ifdef CONFIG_X86_MCE_P4THERMAL
128 v = apic_read(APIC_LVTTHMR);
129 apic_write_around(APIC_LVTTHMR, v | APIC_LVT_MASKED);
133 * Clean APIC state for other OSs:
135 apic_write_around(APIC_LVTT, APIC_LVT_MASKED);
136 apic_write_around(APIC_LVT0, APIC_LVT_MASKED);
137 apic_write_around(APIC_LVT1, APIC_LVT_MASKED);
139 apic_write_around(APIC_LVTERR, APIC_LVT_MASKED);
141 apic_write_around(APIC_LVTPC, APIC_LVT_MASKED);
143 #ifdef CONFIG_X86_MCE_P4THERMAL
145 apic_write_around(APIC_LVTTHMR, APIC_LVT_MASKED);
147 v = GET_APIC_VERSION(apic_read(APIC_LVR));
148 if (APIC_INTEGRATED(v)) { /* !82489DX */
149 if (maxlvt > 3) /* Due to Pentium errata 3AP and 11AP. */
150 apic_write(APIC_ESR, 0);
155 void __init connect_bsp_APIC(void)
159 * Do not trust the local APIC being empty at bootup.
163 * PIC mode, enable APIC mode in the IMCR, i.e.
164 * connect BSP's local APIC to INT and NMI lines.
166 printk("leaving PIC mode, enabling APIC mode.\n");
173 void disconnect_bsp_APIC(void)
177 * Put the board back into PIC mode (has an effect
178 * only on certain older boards). Note that APIC
179 * interrupts, including IPIs, won't work beyond
180 * this point! The only exception are INIT IPIs.
182 printk("disabling APIC mode, entering PIC mode.\n");
188 void disable_local_APIC(void)
195 * Disable APIC (implies clearing of registers
198 value = apic_read(APIC_SPIV);
199 value &= ~APIC_SPIV_APIC_ENABLED;
200 apic_write_around(APIC_SPIV, value);
202 if (enabled_via_apicbase) {
204 rdmsr(MSR_IA32_APICBASE, l, h);
205 l &= ~MSR_IA32_APICBASE_ENABLE;
206 wrmsr(MSR_IA32_APICBASE, l, h);
211 * This is to verify that we're looking at a real local APIC.
212 * Check these against your board if the CPUs aren't getting
213 * started for no apparent reason.
215 int __init verify_local_APIC(void)
217 unsigned int reg0, reg1;
220 * The version register is read-only in a real APIC.
222 reg0 = apic_read(APIC_LVR);
223 Dprintk("Getting VERSION: %x\n", reg0);
224 apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
225 reg1 = apic_read(APIC_LVR);
226 Dprintk("Getting VERSION: %x\n", reg1);
229 * The two version reads above should print the same
230 * numbers. If the second one is different, then we
231 * poke at a non-APIC.
237 * Check if the version looks reasonably.
239 reg1 = GET_APIC_VERSION(reg0);
240 if (reg1 == 0x00 || reg1 == 0xff)
243 if (reg1 < 0x02 || reg1 == 0xff)
247 * The ID register is read/write in a real APIC.
249 reg0 = apic_read(APIC_ID);
250 Dprintk("Getting ID: %x\n", reg0);
253 * The next two are just to see if we have sane values.
254 * They're only really relevant if we're in Virtual Wire
255 * compatibility mode, but most boxes are anymore.
257 reg0 = apic_read(APIC_LVT0);
258 Dprintk("Getting LVT0: %x\n", reg0);
259 reg1 = apic_read(APIC_LVT1);
260 Dprintk("Getting LVT1: %x\n", reg1);
265 void __init sync_Arb_IDs(void)
270 apic_wait_icr_idle();
272 Dprintk("Synchronizing Arb IDs.\n");
273 apic_write_around(APIC_ICR, APIC_DEST_ALLINC | APIC_INT_LEVELTRIG
277 extern void __error_in_apic_c (void);
280 * An initial setup of the virtual wire mode.
282 void __init init_bsp_APIC(void)
284 unsigned long value, ver;
287 * Don't do the setup now if we have a SMP BIOS as the
288 * through-I/O-APIC virtual wire mode might be active.
290 if (smp_found_config || !cpu_has_apic)
293 value = apic_read(APIC_LVR);
294 ver = GET_APIC_VERSION(value);
297 * Do not trust the local APIC being empty at bootup.
304 value = apic_read(APIC_SPIV);
305 value &= ~APIC_VECTOR_MASK;
306 value |= APIC_SPIV_APIC_ENABLED;
308 /* This bit is reserved on P4/Xeon and should be cleared */
309 if ((boot_cpu_data.x86_vendor == X86_VENDOR_INTEL) && (boot_cpu_data.x86 == 15))
310 value &= ~APIC_SPIV_FOCUS_DISABLED;
312 value |= APIC_SPIV_FOCUS_DISABLED;
313 value |= SPURIOUS_APIC_VECTOR;
314 apic_write_around(APIC_SPIV, value);
317 * Set up the virtual wire mode.
319 apic_write_around(APIC_LVT0, APIC_DM_EXTINT);
321 if (!APIC_INTEGRATED(ver)) /* 82489DX */
322 value |= APIC_LVT_LEVEL_TRIGGER;
323 apic_write_around(APIC_LVT1, value);
326 void __init setup_local_APIC (void)
328 unsigned long value, ver, maxlvt;
330 /* Pound the ESR really hard over the head with a big hammer - mbligh */
332 apic_write(APIC_ESR, 0);
333 apic_write(APIC_ESR, 0);
334 apic_write(APIC_ESR, 0);
335 apic_write(APIC_ESR, 0);
338 value = apic_read(APIC_LVR);
339 ver = GET_APIC_VERSION(value);
341 if ((SPURIOUS_APIC_VECTOR & 0x0f) != 0x0f)
345 * Double-check whether this APIC is really registered.
347 if (!apic_id_registered())
351 * Intel recommends to set DFR, LDR and TPR before enabling
352 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
353 * document number 292116). So here it goes...
358 * Set Task Priority to 'accept all'. We never change this
361 value = apic_read(APIC_TASKPRI);
362 value &= ~APIC_TPRI_MASK;
363 apic_write_around(APIC_TASKPRI, value);
366 * Now that we are all set up, enable the APIC
368 value = apic_read(APIC_SPIV);
369 value &= ~APIC_VECTOR_MASK;
373 value |= APIC_SPIV_APIC_ENABLED;
376 * Some unknown Intel IO/APIC (or APIC) errata is biting us with
377 * certain networking cards. If high frequency interrupts are
378 * happening on a particular IOAPIC pin, plus the IOAPIC routing
379 * entry is masked/unmasked at a high rate as well then sooner or
380 * later IOAPIC line gets 'stuck', no more interrupts are received
381 * from the device. If focus CPU is disabled then the hang goes
384 * [ This bug can be reproduced easily with a level-triggered
385 * PCI Ne2000 networking cards and PII/PIII processors, dual
389 * Actually disabling the focus CPU check just makes the hang less
390 * frequent as it makes the interrupt distributon model be more
391 * like LRU than MRU (the short-term load is more even across CPUs).
392 * See also the comment in end_level_ioapic_irq(). --macro
395 /* Enable focus processor (bit==0) */
396 value &= ~APIC_SPIV_FOCUS_DISABLED;
398 /* Disable focus processor (bit==1) */
399 value |= APIC_SPIV_FOCUS_DISABLED;
402 * Set spurious IRQ vector
404 value |= SPURIOUS_APIC_VECTOR;
405 apic_write_around(APIC_SPIV, value);
410 * set up through-local-APIC on the BP's LINT0. This is not
411 * strictly necessery in pure symmetric-IO mode, but sometimes
412 * we delegate interrupts to the 8259A.
415 * TODO: set up through-local-APIC from through-I/O-APIC? --macro
417 value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
418 if (!smp_processor_id() && (pic_mode || !value)) {
419 value = APIC_DM_EXTINT;
420 printk("enabled ExtINT on CPU#%d\n", smp_processor_id());
422 value = APIC_DM_EXTINT | APIC_LVT_MASKED;
423 printk("masked ExtINT on CPU#%d\n", smp_processor_id());
425 apic_write_around(APIC_LVT0, value);
428 * only the BP should see the LINT1 NMI signal, obviously.
430 if (!smp_processor_id())
433 value = APIC_DM_NMI | APIC_LVT_MASKED;
434 if (!APIC_INTEGRATED(ver)) /* 82489DX */
435 value |= APIC_LVT_LEVEL_TRIGGER;
436 apic_write_around(APIC_LVT1, value);
438 if (APIC_INTEGRATED(ver) && !esr_disable) { /* !82489DX */
439 maxlvt = get_maxlvt();
440 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
441 apic_write(APIC_ESR, 0);
442 value = apic_read(APIC_ESR);
443 printk("ESR value before enabling vector: %08lx\n", value);
445 value = ERROR_APIC_VECTOR; // enables sending errors
446 apic_write_around(APIC_LVTERR, value);
448 * spec says clear errors after enabling vector.
451 apic_write(APIC_ESR, 0);
452 value = apic_read(APIC_ESR);
453 printk("ESR value after enabling vector: %08lx\n", value);
457 * Something untraceble is creating bad interrupts on
458 * secondary quads ... for the moment, just leave the
459 * ESR disabled - we can't do anything useful with the
460 * errors anyway - mbligh
462 printk("Leaving ESR disabled.\n");
464 printk("No ESR for 82489DX.\n");
467 if (nmi_watchdog == NMI_LOCAL_APIC)
468 setup_apic_nmi_watchdog();
475 /* 'active' is true if the local APIC was enabled by us and
476 not the BIOS; this signifies that we are also responsible
477 for disabling it before entering apm/acpi suspend */
479 /* r/w apic fields */
480 unsigned int apic_id;
481 unsigned int apic_taskpri;
482 unsigned int apic_ldr;
483 unsigned int apic_dfr;
484 unsigned int apic_spiv;
485 unsigned int apic_lvtt;
486 unsigned int apic_lvtpc;
487 unsigned int apic_lvt0;
488 unsigned int apic_lvt1;
489 unsigned int apic_lvterr;
490 unsigned int apic_tmict;
491 unsigned int apic_tdcr;
492 unsigned int apic_thmr;
495 static int lapic_suspend(struct sys_device *dev, u32 state)
499 if (!apic_pm_state.active)
502 apic_pm_state.apic_id = apic_read(APIC_ID);
503 apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
504 apic_pm_state.apic_ldr = apic_read(APIC_LDR);
505 apic_pm_state.apic_dfr = apic_read(APIC_DFR);
506 apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
507 apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
508 apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
509 apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
510 apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
511 apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
512 apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
513 apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
514 apic_pm_state.apic_thmr = apic_read(APIC_LVTTHMR);
516 local_irq_save(flags);
517 disable_local_APIC();
518 local_irq_restore(flags);
522 static int lapic_resume(struct sys_device *dev)
527 if (!apic_pm_state.active)
530 local_irq_save(flags);
533 * Make sure the APICBASE points to the right address
535 * FIXME! This will be wrong if we ever support suspend on
536 * SMP! We'll need to do this as part of the CPU restore!
538 rdmsr(MSR_IA32_APICBASE, l, h);
539 l &= ~MSR_IA32_APICBASE_BASE;
540 l |= MSR_IA32_APICBASE_ENABLE | mp_lapic_addr;
541 wrmsr(MSR_IA32_APICBASE, l, h);
543 apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
544 apic_write(APIC_ID, apic_pm_state.apic_id);
545 apic_write(APIC_DFR, apic_pm_state.apic_dfr);
546 apic_write(APIC_LDR, apic_pm_state.apic_ldr);
547 apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
548 apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
549 apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
550 apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
551 apic_write(APIC_LVTTHMR, apic_pm_state.apic_thmr);
552 apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
553 apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
554 apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
555 apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
556 apic_write(APIC_ESR, 0);
558 apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
559 apic_write(APIC_ESR, 0);
561 local_irq_restore(flags);
566 static struct sysdev_class lapic_sysclass = {
567 set_kset_name("lapic"),
568 .resume = lapic_resume,
569 .suspend = lapic_suspend,
572 static struct sys_device device_lapic = {
574 .cls = &lapic_sysclass,
577 static void __init apic_pm_activate(void)
579 apic_pm_state.active = 1;
582 static int __init init_lapic_sysfs(void)
588 /* XXX: remove suspend/resume procs if !apic_pm_state.active? */
590 error = sysdev_class_register(&lapic_sysclass);
592 error = sysdev_register(&device_lapic);
595 device_initcall(init_lapic_sysfs);
597 #else /* CONFIG_PM */
599 static void apic_pm_activate(void) { }
601 #endif /* CONFIG_PM */
604 * Detect and enable local APICs on non-SMP boards.
605 * Original code written by Keir Fraser.
609 * Knob to control our willingness to enable the local APIC.
611 int enable_local_apic __initdata = 0; /* -1=force-disable, +1=force-enable */
613 static int __init lapic_disable(char *str)
615 enable_local_apic = -1;
616 clear_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
619 __setup("nolapic", lapic_disable);
621 static int __init lapic_enable(char *str)
623 enable_local_apic = 1;
626 __setup("lapic", lapic_enable);
628 static int __init detect_init_APIC (void)
631 extern void get_cpu_vendor(struct cpuinfo_x86*);
633 /* Disabled by DMI scan or kernel option? */
634 if (enable_local_apic < 0)
637 /* Workaround for us being called before identify_cpu(). */
638 get_cpu_vendor(&boot_cpu_data);
640 switch (boot_cpu_data.x86_vendor) {
642 if ((boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model > 1) ||
643 (boot_cpu_data.x86 == 15))
646 case X86_VENDOR_INTEL:
647 if (boot_cpu_data.x86 == 6 ||
648 (boot_cpu_data.x86 == 15 && (cpu_has_apic || enable_local_apic > 0)) ||
649 (boot_cpu_data.x86 == 5 && cpu_has_apic))
658 * Some BIOSes disable the local APIC in the
659 * APIC_BASE MSR. This can only be done in
660 * software for Intel P6 and AMD K7 (Model > 1).
662 rdmsr(MSR_IA32_APICBASE, l, h);
663 if (!(l & MSR_IA32_APICBASE_ENABLE)) {
664 printk("Local APIC disabled by BIOS -- reenabling.\n");
665 l &= ~MSR_IA32_APICBASE_BASE;
666 l |= MSR_IA32_APICBASE_ENABLE | APIC_DEFAULT_PHYS_BASE;
667 wrmsr(MSR_IA32_APICBASE, l, h);
668 enabled_via_apicbase = 1;
672 * The APIC feature bit should now be enabled
675 features = cpuid_edx(1);
676 if (!(features & (1 << X86_FEATURE_APIC))) {
677 printk("Could not enable APIC!\n");
680 set_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability);
681 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
683 /* The BIOS may have set up the APIC at some other address */
684 rdmsr(MSR_IA32_APICBASE, l, h);
685 if (l & MSR_IA32_APICBASE_ENABLE)
686 mp_lapic_addr = l & MSR_IA32_APICBASE_BASE;
688 if (nmi_watchdog != NMI_NONE)
689 nmi_watchdog = NMI_LOCAL_APIC;
691 printk("Found and enabled local APIC!\n");
698 printk("No local APIC present or hardware disabled\n");
702 void __init init_apic_mappings(void)
704 unsigned long apic_phys;
707 * If no local APIC can be found then set up a fake all
708 * zeroes page to simulate the local APIC and another
709 * one for the IO-APIC.
711 if (!smp_found_config && detect_init_APIC()) {
712 apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
713 apic_phys = __pa(apic_phys);
715 apic_phys = mp_lapic_addr;
717 set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
718 Dprintk("mapped APIC to %08lx (%08lx)\n", APIC_BASE, apic_phys);
721 * Fetch the APIC ID of the BSP in case we have a
722 * default configuration (or the MP table is broken).
724 if (boot_cpu_physical_apicid == -1U)
725 boot_cpu_physical_apicid = GET_APIC_ID(apic_read(APIC_ID));
727 #ifdef CONFIG_X86_IO_APIC
729 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
732 for (i = 0; i < nr_ioapics; i++) {
733 if (smp_found_config) {
734 ioapic_phys = mp_ioapics[i].mpc_apicaddr;
736 printk(KERN_ERR "WARNING: bogus zero IO-APIC address found in MPTABLE, disabling IO/APIC support!\n");
738 smp_found_config = 0;
739 skip_ioapic_setup = 1;
740 goto fake_ioapic_page;
744 ioapic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
745 ioapic_phys = __pa(ioapic_phys);
747 set_fixmap_nocache(idx, ioapic_phys);
748 Dprintk("mapped IOAPIC to %08lx (%08lx)\n",
749 __fix_to_virt(idx), ioapic_phys);
757 * This part sets up the APIC 32 bit clock in LVTT1, with HZ interrupts
758 * per second. We assume that the caller has already set up the local
761 * The APIC timer is not exactly sync with the external timer chip, it
762 * closely follows bus clocks.
766 * The timer chip is already set up at HZ interrupts per second here,
767 * but we do not accept timer interrupts yet. We only allow the BP
770 static unsigned int __init get_8254_timer_count(void)
772 extern spinlock_t i8253_lock;
777 spin_lock_irqsave(&i8253_lock, flags);
779 outb_p(0x00, PIT_MODE);
780 count = inb_p(PIT_CH0);
781 count |= inb_p(PIT_CH0) << 8;
783 spin_unlock_irqrestore(&i8253_lock, flags);
788 /* next tick in 8254 can be caught by catching timer wraparound */
789 static void __init wait_8254_wraparound(void)
791 unsigned int curr_count, prev_count=~0;
794 curr_count = get_8254_timer_count();
797 prev_count = curr_count;
798 curr_count = get_8254_timer_count();
799 delta = curr_count-prev_count;
802 * This limit for delta seems arbitrary, but it isn't, it's
803 * slightly above the level of error a buggy Mercury/Neptune
804 * chipset timer can cause.
807 } while (delta < 300);
811 * Default initialization for 8254 timers. If we use other timers like HPET,
812 * we override this later
814 void (*wait_timer_tick)(void) = wait_8254_wraparound;
817 * This function sets up the local APIC timer, with a timeout of
818 * 'clocks' APIC bus clock. During calibration we actually call
819 * this function twice on the boot CPU, once with a bogus timeout
820 * value, second time for real. The other (noncalibrating) CPUs
821 * call this function only once, with the real, calibrated value.
823 * We do reads before writes even if unnecessary, to get around the
824 * P5 APIC double write bug.
827 #define APIC_DIVISOR 16
829 void __setup_APIC_LVTT(unsigned int clocks)
831 unsigned int lvtt_value, tmp_value, ver;
833 ver = GET_APIC_VERSION(apic_read(APIC_LVR));
834 lvtt_value = APIC_LVT_TIMER_PERIODIC | LOCAL_TIMER_VECTOR;
835 if (!APIC_INTEGRATED(ver))
836 lvtt_value |= SET_APIC_TIMER_BASE(APIC_TIMER_BASE_DIV);
837 apic_write_around(APIC_LVTT, lvtt_value);
842 tmp_value = apic_read(APIC_TDCR);
843 apic_write_around(APIC_TDCR, (tmp_value
844 & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE))
847 apic_write_around(APIC_TMICT, clocks/APIC_DIVISOR);
850 static void setup_APIC_timer(unsigned int clocks)
854 local_irq_save(flags);
857 * Wait for IRQ0's slice:
861 __setup_APIC_LVTT(clocks);
863 local_irq_restore(flags);
867 * In this function we calibrate APIC bus clocks to the external
868 * timer. Unfortunately we cannot use jiffies and the timer irq
869 * to calibrate, since some later bootup code depends on getting
870 * the first irq? Ugh.
872 * We want to do the calibration only once since we
873 * want to have local timer irqs syncron. CPUs connected
874 * by the same APIC bus have the very same bus frequency.
875 * And we want to have irqs off anyways, no accidental
879 int __init calibrate_APIC_clock(void)
881 unsigned long long t1 = 0, t2 = 0;
885 const int LOOPS = HZ/10;
887 printk("calibrating APIC timer ...\n");
890 * Put whatever arbitrary (but long enough) timeout
891 * value into the APIC clock, we just want to get the
892 * counter running for calibration.
894 __setup_APIC_LVTT(1000000000);
897 * The timer chip counts down to zero. Let's wait
898 * for a wraparound to start exact measurement:
899 * (the current tick might have been already half done)
905 * We wrapped around just now. Let's start:
909 tt1 = apic_read(APIC_TMCCT);
912 * Let's wait LOOPS wraprounds:
914 for (i = 0; i < LOOPS; i++)
917 tt2 = apic_read(APIC_TMCCT);
922 * The APIC bus clock counter is 32 bits only, it
923 * might have overflown, but note that we use signed
924 * longs, thus no extra care needed.
926 * underflown to be exact, as the timer counts down ;)
929 result = (tt1-tt2)*APIC_DIVISOR/LOOPS;
932 printk("..... CPU clock speed is %ld.%04ld MHz.\n",
933 ((long)(t2-t1)/LOOPS)/(1000000/HZ),
934 ((long)(t2-t1)/LOOPS)%(1000000/HZ));
936 printk("..... host bus clock speed is %ld.%04ld MHz.\n",
938 result%(1000000/HZ));
943 static unsigned int calibration_result;
945 void __init setup_boot_APIC_clock(void)
947 printk("Using local APIC timer interrupts.\n");
948 using_apic_timer = 1;
952 calibration_result = calibrate_APIC_clock();
954 * Now set up the timer for real.
956 setup_APIC_timer(calibration_result);
961 void __init setup_secondary_APIC_clock(void)
963 local_irq_disable(); /* FIXME: Do we need this? --RR */
964 setup_APIC_timer(calibration_result);
968 void __init disable_APIC_timer(void)
970 if (using_apic_timer) {
973 v = apic_read(APIC_LVTT);
974 apic_write_around(APIC_LVTT, v | APIC_LVT_MASKED);
978 void enable_APIC_timer(void)
980 if (using_apic_timer) {
983 v = apic_read(APIC_LVTT);
984 apic_write_around(APIC_LVTT, v & ~APIC_LVT_MASKED);
989 * the frequency of the profiling timer can be changed
990 * by writing a multiplier value into /proc/profile.
992 int setup_profiling_timer(unsigned int multiplier)
997 * Sanity check. [at least 500 APIC cycles should be
998 * between APIC interrupts as a rule of thumb, to avoid
1001 if ( (!multiplier) || (calibration_result/multiplier < 500))
1005 * Set the new multiplier for each CPU. CPUs don't start using the
1006 * new values until the next timer interrupt in which they do process
1007 * accounting. At that time they also adjust their APIC timers
1010 for (i = 0; i < NR_CPUS; ++i)
1011 per_cpu(prof_multiplier, i) = multiplier;
1019 * Local timer interrupt handler. It does both profiling and
1020 * process statistics/rescheduling.
1022 * We do profiling in every local tick, statistics/rescheduling
1023 * happen only every 'profiling multiplier' ticks. The default
1024 * multiplier is 1 and it can be changed by writing the new multiplier
1025 * value into /proc/profile.
1028 inline void smp_local_timer_interrupt(struct pt_regs * regs)
1030 int cpu = smp_processor_id();
1032 x86_do_profile(regs);
1034 if (--per_cpu(prof_counter, cpu) <= 0) {
1036 * The multiplier may have changed since the last time we got
1037 * to this point as a result of the user writing to
1038 * /proc/profile. In this case we need to adjust the APIC
1039 * timer accordingly.
1041 * Interrupts are already masked off at this point.
1043 per_cpu(prof_counter, cpu) = per_cpu(prof_multiplier, cpu);
1044 if (per_cpu(prof_counter, cpu) !=
1045 per_cpu(prof_old_multiplier, cpu)) {
1048 per_cpu(prof_counter, cpu));
1049 per_cpu(prof_old_multiplier, cpu) =
1050 per_cpu(prof_counter, cpu);
1054 update_process_times(user_mode(regs));
1059 * We take the 'long' return path, and there every subsystem
1060 * grabs the apropriate locks (kernel lock/ irq lock).
1062 * we might want to decouple profiling from the 'long path',
1063 * and do the profiling totally in assembly.
1065 * Currently this isn't too much of an issue (performance wise),
1066 * we can take more than 100K local irqs per second on a 100 MHz P5.
1071 * Local APIC timer interrupt. This is the most natural way for doing
1072 * local interrupts, but local timer interrupts can be emulated by
1073 * broadcast interrupts too. [in case the hw doesn't support APIC timers]
1075 * [ if a single-CPU system runs an SMP kernel then we call the local
1076 * interrupt as well. Thus we cannot inline the local irq ... ]
1079 void smp_apic_timer_interrupt(struct pt_regs regs)
1081 int cpu = smp_processor_id();
1084 * the NMI deadlock-detector uses this.
1086 irq_stat[cpu].apic_timer_irqs++;
1089 * NOTE! We'd better ACK the irq immediately,
1090 * because timer handling can be slow.
1094 * update_process_times() expects us to have done irq_enter().
1095 * Besides, if we don't timer interrupts ignore the global
1096 * interrupt lock, which is the WrongThing (tm) to do.
1099 smp_local_timer_interrupt(®s);
1104 * This interrupt should _never_ happen with our APIC/SMP architecture
1106 asmlinkage void smp_spurious_interrupt(void)
1112 * Check if this really is a spurious interrupt and ACK it
1113 * if it is a vectored one. Just in case...
1114 * Spurious interrupts should not be ACKed.
1116 v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
1117 if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
1120 /* see sw-dev-man vol 3, chapter 7.4.13.5 */
1121 printk(KERN_INFO "spurious APIC interrupt on CPU#%d, should never happen.\n",
1122 smp_processor_id());
1127 * This interrupt should never happen with our APIC/SMP architecture
1130 asmlinkage void smp_error_interrupt(void)
1132 unsigned long v, v1;
1135 /* First tickle the hardware, only then report what went on. -- REW */
1136 v = apic_read(APIC_ESR);
1137 apic_write(APIC_ESR, 0);
1138 v1 = apic_read(APIC_ESR);
1140 atomic_inc(&irq_err_count);
1142 /* Here is what the APIC error bits mean:
1145 2: Send accept error
1146 3: Receive accept error
1148 5: Send illegal vector
1149 6: Received illegal vector
1150 7: Illegal register address
1152 printk (KERN_INFO "APIC error on CPU%d: %02lx(%02lx)\n",
1153 smp_processor_id(), v , v1);
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 && APIC_INTEGRATED(apic_version[boot_cpu_physical_apicid])) {
1173 printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
1174 boot_cpu_physical_apicid);
1178 verify_local_APIC();
1182 phys_cpu_present_map = physid_mask_of_physid(boot_cpu_physical_apicid);
1186 if (nmi_watchdog == NMI_LOCAL_APIC)
1187 check_nmi_watchdog();
1188 #ifdef CONFIG_X86_IO_APIC
1189 if (smp_found_config)
1190 if (!skip_ioapic_setup && nr_ioapics)
1193 setup_boot_APIC_clock();