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.
15 #include <linux/config.h>
16 #include <linux/init.h>
19 #include <linux/irq.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>
27 #include <asm/atomic.h>
30 #include <asm/mpspec.h>
31 #include <asm/pgalloc.h>
32 #include <asm/timex.h>
34 extern spinlock_t i8253_lock;
36 /* Using APIC to generate smp_local_timer_interrupt? */
37 int using_apic_timer = 0;
39 int prof_multiplier[NR_CPUS] = { 1, };
40 int prof_old_multiplier[NR_CPUS] = { 1, };
41 int prof_counter[NR_CPUS] = { 1, };
45 unsigned int v, ver, maxlvt;
47 v = apic_read(APIC_LVR);
48 ver = GET_APIC_VERSION(v);
49 /* 82489DXs do not report # of LVT entries. */
50 maxlvt = APIC_INTEGRATED(ver) ? GET_APIC_MAXLVT(v) : 2;
54 void clear_local_APIC(void)
59 maxlvt = get_maxlvt();
62 * Masking an LVT entry on a P6 can trigger a local APIC error
63 * if the vector is zero. Mask LVTERR first to prevent this.
66 v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
67 apic_write_around(APIC_LVTERR, v | APIC_LVT_MASKED);
70 * Careful: we have to set masks only first to deassert
71 * any level-triggered sources.
73 v = apic_read(APIC_LVTT);
74 apic_write_around(APIC_LVTT, v | APIC_LVT_MASKED);
75 v = apic_read(APIC_LVT0);
76 apic_write_around(APIC_LVT0, v | APIC_LVT_MASKED);
77 v = apic_read(APIC_LVT1);
78 apic_write_around(APIC_LVT1, v | APIC_LVT_MASKED);
80 v = apic_read(APIC_LVTPC);
81 apic_write_around(APIC_LVTPC, v | APIC_LVT_MASKED);
85 * Clean APIC state for other OSs:
87 apic_write_around(APIC_LVTT, APIC_LVT_MASKED);
88 apic_write_around(APIC_LVT0, APIC_LVT_MASKED);
89 apic_write_around(APIC_LVT1, APIC_LVT_MASKED);
91 apic_write_around(APIC_LVTERR, APIC_LVT_MASKED);
93 apic_write_around(APIC_LVTPC, APIC_LVT_MASKED);
94 v = GET_APIC_VERSION(apic_read(APIC_LVR));
95 if (APIC_INTEGRATED(v)) { /* !82489DX */
97 apic_write(APIC_ESR, 0);
102 void __init connect_bsp_APIC(void)
106 * Do not trust the local APIC being empty at bootup.
110 * PIC mode, enable APIC mode in the IMCR, i.e.
111 * connect BSP's local APIC to INT and NMI lines.
113 printk("leaving PIC mode, enabling APIC mode.\n");
119 void disconnect_bsp_APIC(void)
123 * Put the board back into PIC mode (has an effect
124 * only on certain older boards). Note that APIC
125 * interrupts, including IPIs, won't work beyond
126 * this point! The only exception are INIT IPIs.
128 printk("disabling APIC mode, entering PIC mode.\n");
134 void disable_local_APIC(void)
141 * Disable APIC (implies clearing of registers
144 value = apic_read(APIC_SPIV);
145 value &= ~APIC_SPIV_APIC_ENABLED;
146 apic_write_around(APIC_SPIV, value);
150 * This is to verify that we're looking at a real local APIC.
151 * Check these against your board if the CPUs aren't getting
152 * started for no apparent reason.
154 int __init verify_local_APIC(void)
156 unsigned int reg0, reg1;
159 * The version register is read-only in a real APIC.
161 reg0 = apic_read(APIC_LVR);
162 Dprintk("Getting VERSION: %x\n", reg0);
163 apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
164 reg1 = apic_read(APIC_LVR);
165 Dprintk("Getting VERSION: %x\n", reg1);
168 * The two version reads above should print the same
169 * numbers. If the second one is different, then we
170 * poke at a non-APIC.
176 * Check if the version looks reasonably.
178 reg1 = GET_APIC_VERSION(reg0);
179 if (reg1 == 0x00 || reg1 == 0xff)
182 if (reg1 < 0x02 || reg1 == 0xff)
186 * The ID register is read/write in a real APIC.
188 reg0 = apic_read(APIC_ID);
189 Dprintk("Getting ID: %x\n", reg0);
190 apic_write(APIC_ID, reg0 ^ APIC_ID_MASK);
191 reg1 = apic_read(APIC_ID);
192 Dprintk("Getting ID: %x\n", reg1);
193 apic_write(APIC_ID, reg0);
194 if (reg1 != (reg0 ^ APIC_ID_MASK))
198 * The next two are just to see if we have sane values.
199 * They're only really relevant if we're in Virtual Wire
200 * compatibility mode, but most boxes are anymore.
202 reg0 = apic_read(APIC_LVT0);
203 Dprintk("Getting LVT0: %x\n", reg0);
204 reg1 = apic_read(APIC_LVT1);
205 Dprintk("Getting LVT1: %x\n", reg1);
210 void __init sync_Arb_IDs(void)
215 apic_wait_icr_idle();
217 Dprintk("Synchronizing Arb IDs.\n");
218 apic_write_around(APIC_ICR, APIC_DEST_ALLINC | APIC_INT_LEVELTRIG
222 extern void __error_in_apic_c (void);
225 * An initial setup of the virtual wire mode.
227 void __init init_bsp_APIC(void)
229 unsigned int value, ver;
232 * Don't do the setup now if we have a SMP BIOS as the
233 * through-I/O-APIC virtual wire mode might be active.
235 if (smp_found_config || !cpu_has_apic)
238 value = apic_read(APIC_LVR);
239 ver = GET_APIC_VERSION(value);
242 * Do not trust the local APIC being empty at bootup.
249 value = apic_read(APIC_SPIV);
250 value &= ~APIC_VECTOR_MASK;
251 value |= APIC_SPIV_APIC_ENABLED;
252 value |= APIC_SPIV_FOCUS_DISABLED;
253 value |= SPURIOUS_APIC_VECTOR;
254 apic_write_around(APIC_SPIV, value);
257 * Set up the virtual wire mode.
259 apic_write_around(APIC_LVT0, APIC_DM_EXTINT);
261 if (!APIC_INTEGRATED(ver)) /* 82489DX */
262 value |= APIC_LVT_LEVEL_TRIGGER;
263 apic_write_around(APIC_LVT1, value);
266 void __init setup_local_APIC (void)
268 unsigned int value, ver, maxlvt;
270 /* Pound the ESR really hard over the head with a big hammer - mbligh */
272 apic_write(APIC_ESR, 0);
273 apic_write(APIC_ESR, 0);
274 apic_write(APIC_ESR, 0);
275 apic_write(APIC_ESR, 0);
278 value = apic_read(APIC_LVR);
279 ver = GET_APIC_VERSION(value);
281 if ((SPURIOUS_APIC_VECTOR & 0x0f) != 0x0f)
285 * Double-check wether this APIC is really registered.
286 * This is meaningless in clustered apic mode, so we skip it.
288 if (!clustered_apic_mode &&
289 !test_bit(GET_APIC_ID(apic_read(APIC_ID)), &phys_cpu_present_map))
293 * Intel recommends to set DFR, LDR and TPR before enabling
294 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
295 * document number 292116). So here it goes...
298 if (!clustered_apic_mode) {
300 * In clustered apic mode, the firmware does this for us
301 * Put the APIC into flat delivery mode.
302 * Must be "all ones" explicitly for 82489DX.
304 apic_write_around(APIC_DFR, 0xffffffff);
307 * Set up the logical destination ID.
309 value = apic_read(APIC_LDR);
310 value &= ~APIC_LDR_MASK;
311 value |= (1<<(smp_processor_id()+24));
312 apic_write_around(APIC_LDR, value);
316 * Set Task Priority to 'accept all'. We never change this
319 value = apic_read(APIC_TASKPRI);
320 value &= ~APIC_TPRI_MASK;
321 apic_write_around(APIC_TASKPRI, value);
324 * Now that we are all set up, enable the APIC
326 value = apic_read(APIC_SPIV);
327 value &= ~APIC_VECTOR_MASK;
331 value |= APIC_SPIV_APIC_ENABLED;
334 * Some unknown Intel IO/APIC (or APIC) errata is biting us with
335 * certain networking cards. If high frequency interrupts are
336 * happening on a particular IOAPIC pin, plus the IOAPIC routing
337 * entry is masked/unmasked at a high rate as well then sooner or
338 * later IOAPIC line gets 'stuck', no more interrupts are received
339 * from the device. If focus CPU is disabled then the hang goes
342 * [ This bug can be reproduced easily with a level-triggered
343 * PCI Ne2000 networking cards and PII/PIII processors, dual
347 * Actually disabling the focus CPU check just makes the hang less
348 * frequent as it makes the interrupt distributon model be more
349 * like LRU than MRU (the short-term load is more even across CPUs).
350 * See also the comment in end_level_ioapic_irq(). --macro
353 /* Enable focus processor (bit==0) */
354 value &= ~APIC_SPIV_FOCUS_DISABLED;
356 /* Disable focus processor (bit==1) */
357 value |= APIC_SPIV_FOCUS_DISABLED;
360 * Set spurious IRQ vector
362 value |= SPURIOUS_APIC_VECTOR;
363 apic_write_around(APIC_SPIV, value);
368 * set up through-local-APIC on the BP's LINT0. This is not
369 * strictly necessery in pure symmetric-IO mode, but sometimes
370 * we delegate interrupts to the 8259A.
373 * TODO: set up through-local-APIC from through-I/O-APIC? --macro
375 value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
376 if (!smp_processor_id() && (pic_mode || !value)) {
377 value = APIC_DM_EXTINT;
378 printk("enabled ExtINT on CPU#%d\n", smp_processor_id());
380 value = APIC_DM_EXTINT | APIC_LVT_MASKED;
381 printk("masked ExtINT on CPU#%d\n", smp_processor_id());
383 apic_write_around(APIC_LVT0, value);
386 * only the BP should see the LINT1 NMI signal, obviously.
388 if (!smp_processor_id())
391 value = APIC_DM_NMI | APIC_LVT_MASKED;
392 if (!APIC_INTEGRATED(ver)) /* 82489DX */
393 value |= APIC_LVT_LEVEL_TRIGGER;
394 apic_write_around(APIC_LVT1, value);
396 if (APIC_INTEGRATED(ver) && !esr_disable) { /* !82489DX */
397 maxlvt = get_maxlvt();
398 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
399 apic_write(APIC_ESR, 0);
400 value = apic_read(APIC_ESR);
401 printk("ESR value before enabling vector: %08x\n", value);
403 value = ERROR_APIC_VECTOR; /* enables sending errors */
404 apic_write_around(APIC_LVTERR, value);
406 * spec says clear errors after enabling vector.
409 apic_write(APIC_ESR, 0);
410 value = apic_read(APIC_ESR);
411 printk("ESR value after enabling vector: %08x\n", value);
415 * Something untraceble is creating bad interrupts on
416 * secondary quads ... for the moment, just leave the
417 * ESR disabled - we can't do anything useful with the
418 * errors anyway - mbligh
420 printk("Leaving ESR disabled.\n");
422 printk("No ESR for 82489DX.\n");
425 if (nmi_watchdog == NMI_LOCAL_APIC)
426 setup_apic_nmi_watchdog();
431 #include <linux/slab.h>
432 #include <linux/pm.h>
435 /* 'active' is true if the local APIC was enabled by us and
436 not the BIOS; this signifies that we are also responsible
437 for disabling it before entering apm/acpi suspend */
439 /* 'perfctr_pmdev' is here because the current (2.4.1) PM
440 callback system doesn't handle hierarchical dependencies */
441 struct pm_dev *perfctr_pmdev;
442 /* r/w apic fields */
443 unsigned int apic_id;
444 unsigned int apic_taskpri;
445 unsigned int apic_ldr;
446 unsigned int apic_dfr;
447 unsigned int apic_spiv;
448 unsigned int apic_lvtt;
449 unsigned int apic_lvtpc;
450 unsigned int apic_lvt0;
451 unsigned int apic_lvt1;
452 unsigned int apic_lvterr;
453 unsigned int apic_tmict;
454 unsigned int apic_tdcr;
457 static void apic_pm_suspend(void *data)
462 if (apic_pm_state.perfctr_pmdev)
463 pm_send(apic_pm_state.perfctr_pmdev, PM_SUSPEND, data);
464 apic_pm_state.apic_id = apic_read(APIC_ID);
465 apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
466 apic_pm_state.apic_ldr = apic_read(APIC_LDR);
467 apic_pm_state.apic_dfr = apic_read(APIC_DFR);
468 apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
469 apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
470 apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
471 apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
472 apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
473 apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
474 apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
475 apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
478 disable_local_APIC();
479 rdmsr(MSR_IA32_APICBASE, l, h);
480 l &= ~MSR_IA32_APICBASE_ENABLE;
481 wrmsr(MSR_IA32_APICBASE, l, h);
482 __restore_flags(flags);
485 static void apic_pm_resume(void *data)
492 rdmsr(MSR_IA32_APICBASE, l, h);
493 l &= ~MSR_IA32_APICBASE_BASE;
494 l |= MSR_IA32_APICBASE_ENABLE | APIC_DEFAULT_PHYS_BASE;
495 wrmsr(MSR_IA32_APICBASE, l, h);
496 apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
497 apic_write(APIC_ID, apic_pm_state.apic_id);
498 apic_write(APIC_DFR, apic_pm_state.apic_dfr);
499 apic_write(APIC_LDR, apic_pm_state.apic_ldr);
500 apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
501 apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
502 apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
503 apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
504 apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
505 apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
506 apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
507 apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
508 apic_write(APIC_ESR, 0);
510 apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
511 apic_write(APIC_ESR, 0);
513 __restore_flags(flags);
514 if (apic_pm_state.perfctr_pmdev)
515 pm_send(apic_pm_state.perfctr_pmdev, PM_RESUME, data);
518 static int apic_pm_callback(struct pm_dev *dev, pm_request_t rqst, void *data)
522 apic_pm_suspend(data);
525 apic_pm_resume(data);
531 /* perfctr driver should call this instead of pm_register() */
532 struct pm_dev *apic_pm_register(pm_dev_t type,
534 pm_callback callback)
538 if (!apic_pm_state.active)
539 return pm_register(type, id, callback);
540 if (apic_pm_state.perfctr_pmdev)
541 return NULL; /* we're busy */
542 dev = kmalloc(sizeof(struct pm_dev), GFP_KERNEL);
544 memset(dev, 0, sizeof(*dev));
547 dev->callback = callback;
548 apic_pm_state.perfctr_pmdev = dev;
553 /* perfctr driver should call this instead of pm_unregister() */
554 void apic_pm_unregister(struct pm_dev *dev)
556 if (!apic_pm_state.active) {
558 } else if (dev == apic_pm_state.perfctr_pmdev) {
559 apic_pm_state.perfctr_pmdev = NULL;
564 static void __init apic_pm_init1(void)
566 /* can't pm_register() at this early stage in the boot process
567 (causes an immediate reboot), so just set the flag */
568 apic_pm_state.active = 1;
571 static void __init apic_pm_init2(void)
573 if (apic_pm_state.active)
574 pm_register(PM_SYS_DEV, 0, apic_pm_callback);
577 #else /* CONFIG_PM */
579 static inline void apic_pm_init1(void) { }
580 static inline void apic_pm_init2(void) { }
582 #endif /* CONFIG_PM */
585 * Detect and enable local APICs on non-SMP boards.
586 * Original code written by Keir Fraser.
588 int dont_enable_local_apic __initdata = 0;
590 static int __init detect_init_APIC (void)
593 extern void get_cpu_vendor(struct cpuinfo_x86*);
595 /* Disabled by DMI scan or kernel option? */
596 if (dont_enable_local_apic)
599 /* Workaround for us being called before identify_cpu(). */
600 get_cpu_vendor(&boot_cpu_data);
602 switch (boot_cpu_data.x86_vendor) {
604 if (boot_cpu_data.x86 == 6 && boot_cpu_data.x86_model > 1)
607 case X86_VENDOR_INTEL:
608 if (boot_cpu_data.x86 == 6 ||
609 (boot_cpu_data.x86 == 15 && cpu_has_apic) ||
610 (boot_cpu_data.x86 == 5 && cpu_has_apic))
619 * Some BIOSes disable the local APIC in the
620 * APIC_BASE MSR. This can only be done in
621 * software for Intel P6 and AMD K7 (Model > 1).
623 rdmsr(MSR_IA32_APICBASE, l, h);
624 if (!(l & MSR_IA32_APICBASE_ENABLE)) {
625 printk("Local APIC disabled by BIOS -- reenabling.\n");
626 l &= ~MSR_IA32_APICBASE_BASE;
627 l |= MSR_IA32_APICBASE_ENABLE | APIC_DEFAULT_PHYS_BASE;
628 wrmsr(MSR_IA32_APICBASE, l, h);
632 * The APIC feature bit should now be enabled
635 features = cpuid_edx(1);
636 if (!(features & (1 << X86_FEATURE_APIC))) {
637 printk("Could not enable APIC!\n");
640 set_bit(X86_FEATURE_APIC, &boot_cpu_data.x86_capability);
641 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
643 if (nmi_watchdog != NMI_NONE)
644 nmi_watchdog = NMI_LOCAL_APIC;
646 printk("Found and enabled local APIC!\n");
653 printk("No local APIC present or hardware disabled\n");
657 void __init init_apic_mappings(void)
659 unsigned long apic_phys;
662 * If no local APIC can be found then set up a fake all
663 * zeroes page to simulate the local APIC and another
664 * one for the IO-APIC.
666 if (!smp_found_config && detect_init_APIC()) {
667 apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
668 apic_phys = __pa(apic_phys);
670 apic_phys = mp_lapic_addr;
672 set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
673 Dprintk("mapped APIC to %08lx (%08lx)\n", APIC_BASE, apic_phys);
676 * Fetch the APIC ID of the BSP in case we have a
677 * default configuration (or the MP table is broken).
679 if (boot_cpu_id == -1U)
680 boot_cpu_id = GET_APIC_ID(apic_read(APIC_ID));
682 #ifdef CONFIG_X86_IO_APIC
684 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
687 for (i = 0; i < nr_ioapics; i++) {
688 if (smp_found_config) {
689 ioapic_phys = mp_ioapics[i].mpc_apicaddr;
691 printk(KERN_ERR "WARNING: bogus zero IO-APIC address found in MPTABLE, disabling IO-APIC support\n");
692 smp_found_config = 0;
693 skip_ioapic_setup = 1;
694 goto fake_ioapic_page;
698 ioapic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
699 ioapic_phys = __pa(ioapic_phys);
701 set_fixmap_nocache(idx, ioapic_phys);
702 Dprintk("mapped IOAPIC to %08lx (%08lx)\n",
703 __fix_to_virt(idx), ioapic_phys);
711 * This part sets up the APIC 32 bit clock in LVTT1, with HZ interrupts
712 * per second. We assume that the caller has already set up the local
717 * This function sets up the local APIC timer, with a timeout of
718 * 'clocks' APIC bus clock. During calibration we actually call
719 * this function twice on the boot CPU, once with a bogus timeout
720 * value, second time for real. The other (noncalibrating) CPUs
721 * call this function only once, with the real, calibrated value.
723 * We do reads before writes even if unnecessary, to get around the
724 * P5 APIC double write bug.
727 #define APIC_DIVISOR 16
729 void __setup_APIC_LVTT(unsigned int clocks)
731 unsigned int lvtt1_value, tmp_value;
733 lvtt1_value = SET_APIC_TIMER_BASE(APIC_TIMER_BASE_DIV) |
734 APIC_LVT_TIMER_PERIODIC | LOCAL_TIMER_VECTOR;
735 apic_write_around(APIC_LVTT, lvtt1_value);
740 tmp_value = apic_read(APIC_TDCR);
741 apic_write_around(APIC_TDCR, (tmp_value
742 & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE))
745 apic_write_around(APIC_TMICT, clocks/APIC_DIVISOR);
748 void setup_APIC_timer(void * data)
750 unsigned int clocks = (unsigned long) data, slice, t0, t1;
754 * ok, Intel has some smart code in their APIC that knows
755 * if a CPU was in 'hlt' lowpower mode, and this increases
756 * its APIC arbitration priority. To avoid the external timer
757 * IRQ APIC event being in synchron with the APIC clock we
758 * introduce an interrupt skew to spread out timer events.
760 * The number of slices within a 'big' timeslice is smp_num_cpus+1
763 slice = clocks / (smp_num_cpus+1);
764 printk(KERN_INFO "cpu: %d, clocks: %d, slice: %d\n",
765 smp_processor_id(), clocks, slice);
768 * Wait for timer IRQ slice:
772 int trigger = hpet_readl(HPET_T0_CMP);
773 while (hpet_readl(HPET_COUNTER) >= trigger);
774 while (hpet_readl(HPET_COUNTER) < trigger);
777 spin_lock(&i8253_lock);
780 c2 |= inb_p(0x40) << 8;
785 c2 |= inb_p(0x40) << 8;
786 } while (c2 - c1 < 300);
787 spin_unlock(&i8253_lock);
790 __setup_APIC_LVTT(clocks);
792 t0 = apic_read(APIC_TMICT)*APIC_DIVISOR;
794 /* Wait till TMCCT gets reloaded from TMICT... */
796 t1 = apic_read(APIC_TMCCT)*APIC_DIVISOR;
797 delta = (int)(t0 - t1 - slice*(smp_processor_id()+1));
798 } while (delta >= 0);
800 /* Now wait for our slice for real. */
802 t1 = apic_read(APIC_TMCCT)*APIC_DIVISOR;
803 delta = (int)(t0 - t1 - slice*(smp_processor_id()+1));
806 __setup_APIC_LVTT(clocks);
808 printk(KERN_INFO "CPU%d<T0:%u,T1:%u,D:%d,S:%u,C:%u>\n",
809 smp_processor_id(), t0, t1, delta, slice, clocks);
813 * In this function we calibrate APIC bus clocks to the external timer.
814 * Unlike on i386 we don't use the PIT to do that, but rely on a
815 * pre-calibrated TSC counter instead.
817 * We want to do the calibration only once since we want to have local timer
818 * irqs syncron. CPUs connected by the same APIC bus have the very same bus
819 * frequency. And we want to have irqs off anyways, no accidental APIC irq
823 #define TICK_COUNT 100000000
825 int __init calibrate_APIC_clock(void)
827 int apic, apic_start, tsc, tsc_start;
830 * Put whatever arbitrary (but long enough) timeout
831 * value into the APIC clock, we just want to get the
832 * counter running for calibration.
834 __setup_APIC_LVTT(1000000000);
836 apic_start = apic_read(APIC_TMCCT);
840 apic = apic_read(APIC_TMCCT);
842 } while ((tsc - tsc_start) < TICK_COUNT && (apic - apic_start) < TICK_COUNT);
844 result = (apic_start - apic) * 1000L * cpu_khz / (tsc - tsc_start);
846 printk("Detected %d.%03d MHz APIC timer.\n",
847 result / 1000 / 1000, result / 1000 % 1000);
849 return result * APIC_DIVISOR / HZ;
852 static unsigned int calibration_result;
854 int dont_use_local_apic_timer __initdata = 0;
856 void __init setup_APIC_clocks (void)
858 /* Disabled by DMI scan or kernel option? */
859 if (dont_use_local_apic_timer)
862 printk("Using local APIC timer interrupts.\n");
863 using_apic_timer = 1;
867 calibration_result = calibrate_APIC_clock();
869 * Now set up the timer for real.
871 setup_APIC_timer((void *)(u64)calibration_result);
875 /* and update all other cpus */
876 smp_call_function(setup_APIC_timer, (void *)(u64)calibration_result, 1, 1);
880 * the frequency of the profiling timer can be changed
881 * by writing a multiplier value into /proc/profile.
883 int setup_profiling_timer(unsigned int multiplier)
888 * Sanity check. [at least 500 APIC cycles should be
889 * between APIC interrupts as a rule of thumb, to avoid
892 if ( (!multiplier) || (calibration_result/multiplier < 500))
896 * Set the new multiplier for each CPU. CPUs don't start using the
897 * new values until the next timer interrupt in which they do process
898 * accounting. At that time they also adjust their APIC timers
901 for (i = 0; i < NR_CPUS; ++i)
902 prof_multiplier[i] = multiplier;
910 * Local timer interrupt handler. It does both profiling and
911 * process statistics/rescheduling.
913 * We do profiling in every local tick, statistics/rescheduling
914 * happen only every 'profiling multiplier' ticks. The default
915 * multiplier is 1 and it can be changed by writing the new multiplier
916 * value into /proc/profile.
919 inline void smp_local_timer_interrupt(struct pt_regs *regs)
921 int user = user_mode(regs);
922 int cpu = smp_processor_id();
925 * The profiling function is SMP safe. (nothing can mess
926 * around with "current", and the profiling counters are
927 * updated with atomic operations). This is especially
928 * useful with a profiling multiplier != 1
931 x86_do_profile(regs->rip);
933 if (--prof_counter[cpu] <= 0) {
935 * The multiplier may have changed since the last time we got
936 * to this point as a result of the user writing to
937 * /proc/profile. In this case we need to adjust the APIC
940 * Interrupts are already masked off at this point.
942 prof_counter[cpu] = prof_multiplier[cpu];
943 if (prof_counter[cpu] != prof_old_multiplier[cpu]) {
944 __setup_APIC_LVTT(calibration_result/prof_counter[cpu]);
945 prof_old_multiplier[cpu] = prof_counter[cpu];
949 update_process_times(user);
954 * We take the 'long' return path, and there every subsystem
955 * grabs the apropriate locks (kernel lock/ irq lock).
957 * we might want to decouple profiling from the 'long path',
958 * and do the profiling totally in assembly.
960 * Currently this isn't too much of an issue (performance wise),
961 * we can take more than 100K local irqs per second on a 100 MHz P5.
966 * Local APIC timer interrupt. This is the most natural way for doing
967 * local interrupts, but local timer interrupts can be emulated by
968 * broadcast interrupts too. [in case the hw doesnt support APIC timers]
970 * [ if a single-CPU system runs an SMP kernel then we call the local
971 * interrupt as well. Thus we cannot inline the local irq ... ]
973 unsigned int apic_timer_irqs [NR_CPUS];
975 void smp_apic_timer_interrupt(struct pt_regs *regs)
977 int cpu = smp_processor_id();
980 * the NMI deadlock-detector uses this.
982 apic_timer_irqs[cpu]++;
985 * NOTE! We'd better ACK the irq immediately,
986 * because timer handling can be slow.
990 * update_process_times() expects us to have done irq_enter().
991 * Besides, if we don't timer interrupts ignore the global
992 * interrupt lock, which is the WrongThing (tm) to do.
995 smp_local_timer_interrupt(regs);
998 if (softirq_pending(cpu))
1003 * This interrupt should _never_ happen with our APIC/SMP architecture
1005 asmlinkage void smp_spurious_interrupt(void)
1008 static unsigned long last_warning;
1009 static unsigned long skipped;
1012 * Check if this really is a spurious interrupt and ACK it
1013 * if it is a vectored one. Just in case...
1014 * Spurious interrupts should not be ACKed.
1016 v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
1017 if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
1020 /* see sw-dev-man vol 3, chapter 7.4.13.5 */
1021 if (last_warning+30*HZ < jiffies) {
1022 printk(KERN_INFO "spurious APIC interrupt on CPU#%d, %ld skipped.\n",
1023 smp_processor_id(), skipped);
1024 last_warning = jiffies;
1032 * This interrupt should never happen with our APIC/SMP architecture
1035 asmlinkage void smp_error_interrupt(void)
1039 /* First tickle the hardware, only then report what went on. -- REW */
1040 v = apic_read(APIC_ESR);
1041 apic_write(APIC_ESR, 0);
1042 v1 = apic_read(APIC_ESR);
1044 atomic_inc(&irq_err_count);
1046 /* Here is what the APIC error bits mean:
1049 2: Send accept error
1050 3: Receive accept error
1052 5: Send illegal vector
1053 6: Received illegal vector
1054 7: Illegal register address
1056 printk (KERN_ERR "APIC error on CPU%d: %02x(%02x)\n",
1057 smp_processor_id(), v , v1);
1061 * This initializes the IO-APIC and APIC hardware if this is
1064 int __init APIC_init_uniprocessor (void)
1066 if (!smp_found_config && !cpu_has_apic)
1070 * Complain if the BIOS pretends there is one.
1072 if (!cpu_has_apic && APIC_INTEGRATED(apic_version[boot_cpu_id])) {
1073 printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
1078 verify_local_APIC();
1082 phys_cpu_present_map = 1;
1083 apic_write_around(APIC_ID, boot_cpu_id);
1089 if (nmi_watchdog == NMI_LOCAL_APIC)
1090 check_nmi_watchdog();
1091 #ifdef CONFIG_X86_IO_APIC
1092 if (smp_found_config && !skip_ioapic_setup && nr_ioapics)
1097 setup_APIC_clocks();