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>
36 extern spinlock_t i8253_lock;
38 /* Using APIC to generate smp_local_timer_interrupt? */
39 int using_apic_timer = 0;
41 int prof_multiplier[NR_CPUS] = { 1, };
42 int prof_old_multiplier[NR_CPUS] = { 1, };
43 int prof_counter[NR_CPUS] = { 1, };
47 unsigned int v, ver, maxlvt;
49 v = apic_read(APIC_LVR);
50 ver = GET_APIC_VERSION(v);
51 /* 82489DXs do not report # of LVT entries. */
52 maxlvt = APIC_INTEGRATED(ver) ? GET_APIC_MAXLVT(v) : 2;
56 void clear_local_APIC(void)
61 maxlvt = get_maxlvt();
64 * Masking an LVT entry on a P6 can trigger a local APIC error
65 * if the vector is zero. Mask LVTERR first to prevent this.
68 v = ERROR_APIC_VECTOR; /* any non-zero vector will do */
69 apic_write_around(APIC_LVTERR, v | APIC_LVT_MASKED);
72 * Careful: we have to set masks only first to deassert
73 * any level-triggered sources.
75 v = apic_read(APIC_LVTT);
76 apic_write_around(APIC_LVTT, v | APIC_LVT_MASKED);
77 v = apic_read(APIC_LVT0);
78 apic_write_around(APIC_LVT0, v | APIC_LVT_MASKED);
79 v = apic_read(APIC_LVT1);
80 apic_write_around(APIC_LVT1, v | APIC_LVT_MASKED);
82 v = apic_read(APIC_LVTPC);
83 apic_write_around(APIC_LVTPC, v | APIC_LVT_MASKED);
87 * Clean APIC state for other OSs:
89 apic_write_around(APIC_LVTT, APIC_LVT_MASKED);
90 apic_write_around(APIC_LVT0, APIC_LVT_MASKED);
91 apic_write_around(APIC_LVT1, APIC_LVT_MASKED);
93 apic_write_around(APIC_LVTERR, APIC_LVT_MASKED);
95 apic_write_around(APIC_LVTPC, APIC_LVT_MASKED);
96 v = GET_APIC_VERSION(apic_read(APIC_LVR));
97 if (APIC_INTEGRATED(v)) { /* !82489DX */
99 apic_write(APIC_ESR, 0);
104 void __init connect_bsp_APIC(void)
108 * Do not trust the local APIC being empty at bootup.
112 * PIC mode, enable APIC mode in the IMCR, i.e.
113 * connect BSP's local APIC to INT and NMI lines.
115 printk("leaving PIC mode, enabling APIC mode.\n");
121 void disconnect_bsp_APIC(void)
125 * Put the board back into PIC mode (has an effect
126 * only on certain older boards). Note that APIC
127 * interrupts, including IPIs, won't work beyond
128 * this point! The only exception are INIT IPIs.
130 printk("disabling APIC mode, entering PIC mode.\n");
136 void disable_local_APIC(void)
143 * Disable APIC (implies clearing of registers
146 value = apic_read(APIC_SPIV);
147 value &= ~APIC_SPIV_APIC_ENABLED;
148 apic_write_around(APIC_SPIV, value);
152 * This is to verify that we're looking at a real local APIC.
153 * Check these against your board if the CPUs aren't getting
154 * started for no apparent reason.
156 int __init verify_local_APIC(void)
158 unsigned int reg0, reg1;
161 * The version register is read-only in a real APIC.
163 reg0 = apic_read(APIC_LVR);
164 Dprintk("Getting VERSION: %x\n", reg0);
165 apic_write(APIC_LVR, reg0 ^ APIC_LVR_MASK);
166 reg1 = apic_read(APIC_LVR);
167 Dprintk("Getting VERSION: %x\n", reg1);
170 * The two version reads above should print the same
171 * numbers. If the second one is different, then we
172 * poke at a non-APIC.
178 * Check if the version looks reasonably.
180 reg1 = GET_APIC_VERSION(reg0);
181 if (reg1 == 0x00 || reg1 == 0xff)
184 if (reg1 < 0x02 || reg1 == 0xff)
188 * The ID register is read/write in a real APIC.
190 reg0 = apic_read(APIC_ID);
191 Dprintk("Getting ID: %x\n", reg0);
192 apic_write(APIC_ID, reg0 ^ APIC_ID_MASK);
193 reg1 = apic_read(APIC_ID);
194 Dprintk("Getting ID: %x\n", reg1);
195 apic_write(APIC_ID, reg0);
196 if (reg1 != (reg0 ^ APIC_ID_MASK))
200 * The next two are just to see if we have sane values.
201 * They're only really relevant if we're in Virtual Wire
202 * compatibility mode, but most boxes are anymore.
204 reg0 = apic_read(APIC_LVT0);
205 Dprintk("Getting LVT0: %x\n", reg0);
206 reg1 = apic_read(APIC_LVT1);
207 Dprintk("Getting LVT1: %x\n", reg1);
212 void __init sync_Arb_IDs(void)
217 apic_wait_icr_idle();
219 Dprintk("Synchronizing Arb IDs.\n");
220 apic_write_around(APIC_ICR, APIC_DEST_ALLINC | APIC_INT_LEVELTRIG
224 extern void __error_in_apic_c (void);
227 * An initial setup of the virtual wire mode.
229 void __init init_bsp_APIC(void)
231 unsigned int value, ver;
234 * Don't do the setup now if we have a SMP BIOS as the
235 * through-I/O-APIC virtual wire mode might be active.
237 if (smp_found_config || !cpu_has_apic)
240 value = apic_read(APIC_LVR);
241 ver = GET_APIC_VERSION(value);
244 * Do not trust the local APIC being empty at bootup.
251 value = apic_read(APIC_SPIV);
252 value &= ~APIC_VECTOR_MASK;
253 value |= APIC_SPIV_APIC_ENABLED;
254 value |= APIC_SPIV_FOCUS_DISABLED;
255 value |= SPURIOUS_APIC_VECTOR;
256 apic_write_around(APIC_SPIV, value);
259 * Set up the virtual wire mode.
261 apic_write_around(APIC_LVT0, APIC_DM_EXTINT);
263 if (!APIC_INTEGRATED(ver)) /* 82489DX */
264 value |= APIC_LVT_LEVEL_TRIGGER;
265 apic_write_around(APIC_LVT1, value);
268 void __init setup_local_APIC (void)
270 unsigned int value, ver, maxlvt;
272 /* Pound the ESR really hard over the head with a big hammer - mbligh */
274 apic_write(APIC_ESR, 0);
275 apic_write(APIC_ESR, 0);
276 apic_write(APIC_ESR, 0);
277 apic_write(APIC_ESR, 0);
280 value = apic_read(APIC_LVR);
281 ver = GET_APIC_VERSION(value);
283 if ((SPURIOUS_APIC_VECTOR & 0x0f) != 0x0f)
287 * Double-check wether this APIC is really registered.
288 * This is meaningless in clustered apic mode, so we skip it.
290 if (!clustered_apic_mode &&
291 !test_bit(GET_APIC_ID(apic_read(APIC_ID)), &phys_cpu_present_map))
295 * Intel recommends to set DFR, LDR and TPR before enabling
296 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
297 * document number 292116). So here it goes...
300 if (!clustered_apic_mode) {
302 * In clustered apic mode, the firmware does this for us
303 * Put the APIC into flat delivery mode.
304 * Must be "all ones" explicitly for 82489DX.
306 apic_write_around(APIC_DFR, 0xffffffff);
309 * Set up the logical destination ID.
311 value = apic_read(APIC_LDR);
312 value &= ~APIC_LDR_MASK;
313 value |= (1<<(smp_processor_id()+24));
314 apic_write_around(APIC_LDR, value);
318 * Set Task Priority to 'accept all'. We never change this
321 value = apic_read(APIC_TASKPRI);
322 value &= ~APIC_TPRI_MASK;
323 apic_write_around(APIC_TASKPRI, value);
326 * Now that we are all set up, enable the APIC
328 value = apic_read(APIC_SPIV);
329 value &= ~APIC_VECTOR_MASK;
333 value |= APIC_SPIV_APIC_ENABLED;
336 * Some unknown Intel IO/APIC (or APIC) errata is biting us with
337 * certain networking cards. If high frequency interrupts are
338 * happening on a particular IOAPIC pin, plus the IOAPIC routing
339 * entry is masked/unmasked at a high rate as well then sooner or
340 * later IOAPIC line gets 'stuck', no more interrupts are received
341 * from the device. If focus CPU is disabled then the hang goes
344 * [ This bug can be reproduced easily with a level-triggered
345 * PCI Ne2000 networking cards and PII/PIII processors, dual
349 * Actually disabling the focus CPU check just makes the hang less
350 * frequent as it makes the interrupt distributon model be more
351 * like LRU than MRU (the short-term load is more even across CPUs).
352 * See also the comment in end_level_ioapic_irq(). --macro
355 /* Enable focus processor (bit==0) */
356 value &= ~APIC_SPIV_FOCUS_DISABLED;
358 /* Disable focus processor (bit==1) */
359 value |= APIC_SPIV_FOCUS_DISABLED;
362 * Set spurious IRQ vector
364 value |= SPURIOUS_APIC_VECTOR;
365 apic_write_around(APIC_SPIV, value);
370 * set up through-local-APIC on the BP's LINT0. This is not
371 * strictly necessery in pure symmetric-IO mode, but sometimes
372 * we delegate interrupts to the 8259A.
375 * TODO: set up through-local-APIC from through-I/O-APIC? --macro
377 value = apic_read(APIC_LVT0) & APIC_LVT_MASKED;
378 if (!smp_processor_id() && (pic_mode || !value)) {
379 value = APIC_DM_EXTINT;
380 Dprintk("enabled ExtINT on CPU#%d\n", smp_processor_id());
382 value = APIC_DM_EXTINT | APIC_LVT_MASKED;
383 Dprintk("masked ExtINT on CPU#%d\n", smp_processor_id());
385 apic_write_around(APIC_LVT0, value);
388 * only the BP should see the LINT1 NMI signal, obviously.
390 if (!smp_processor_id())
393 value = APIC_DM_NMI | APIC_LVT_MASKED;
394 if (!APIC_INTEGRATED(ver)) /* 82489DX */
395 value |= APIC_LVT_LEVEL_TRIGGER;
396 apic_write_around(APIC_LVT1, value);
398 if (APIC_INTEGRATED(ver) && !esr_disable) { /* !82489DX */
399 maxlvt = get_maxlvt();
400 if (maxlvt > 3) /* Due to the Pentium erratum 3AP. */
401 apic_write(APIC_ESR, 0);
402 value = apic_read(APIC_ESR);
403 Dprintk("ESR value before enabling vector: %08x\n", value);
405 value = ERROR_APIC_VECTOR; /* enables sending errors */
406 apic_write_around(APIC_LVTERR, value);
408 * spec says clear errors after enabling vector.
411 apic_write(APIC_ESR, 0);
412 value = apic_read(APIC_ESR);
413 Dprintk("ESR value after enabling vector: %08x\n", value);
417 * Something untraceble is creating bad interrupts on
418 * secondary quads ... for the moment, just leave the
419 * ESR disabled - we can't do anything useful with the
420 * errors anyway - mbligh
422 Dprintk("Leaving ESR disabled.\n");
424 Dprintk("No ESR for 82489DX.\n");
427 if (nmi_watchdog == NMI_LOCAL_APIC)
428 setup_apic_nmi_watchdog();
433 #include <linux/slab.h>
434 #include <linux/pm.h>
437 /* 'active' is true if the local APIC was enabled by us and
438 not the BIOS; this signifies that we are also responsible
439 for disabling it before entering apm/acpi suspend */
441 /* 'perfctr_pmdev' is here because the current (2.4.1) PM
442 callback system doesn't handle hierarchical dependencies */
443 struct pm_dev *perfctr_pmdev;
444 /* r/w apic fields */
445 unsigned int apic_id;
446 unsigned int apic_taskpri;
447 unsigned int apic_ldr;
448 unsigned int apic_dfr;
449 unsigned int apic_spiv;
450 unsigned int apic_lvtt;
451 unsigned int apic_lvtpc;
452 unsigned int apic_lvt0;
453 unsigned int apic_lvt1;
454 unsigned int apic_lvterr;
455 unsigned int apic_tmict;
456 unsigned int apic_tdcr;
459 static void apic_pm_suspend(void *data)
464 if (apic_pm_state.perfctr_pmdev)
465 pm_send(apic_pm_state.perfctr_pmdev, PM_SUSPEND, data);
466 apic_pm_state.apic_id = apic_read(APIC_ID);
467 apic_pm_state.apic_taskpri = apic_read(APIC_TASKPRI);
468 apic_pm_state.apic_ldr = apic_read(APIC_LDR);
469 apic_pm_state.apic_dfr = apic_read(APIC_DFR);
470 apic_pm_state.apic_spiv = apic_read(APIC_SPIV);
471 apic_pm_state.apic_lvtt = apic_read(APIC_LVTT);
472 apic_pm_state.apic_lvtpc = apic_read(APIC_LVTPC);
473 apic_pm_state.apic_lvt0 = apic_read(APIC_LVT0);
474 apic_pm_state.apic_lvt1 = apic_read(APIC_LVT1);
475 apic_pm_state.apic_lvterr = apic_read(APIC_LVTERR);
476 apic_pm_state.apic_tmict = apic_read(APIC_TMICT);
477 apic_pm_state.apic_tdcr = apic_read(APIC_TDCR);
480 disable_local_APIC();
481 rdmsr(MSR_IA32_APICBASE, l, h);
482 l &= ~MSR_IA32_APICBASE_ENABLE;
483 wrmsr(MSR_IA32_APICBASE, l, h);
484 __restore_flags(flags);
487 static void apic_pm_resume(void *data)
494 rdmsr(MSR_IA32_APICBASE, l, h);
495 l &= ~MSR_IA32_APICBASE_BASE;
496 l |= MSR_IA32_APICBASE_ENABLE | APIC_DEFAULT_PHYS_BASE;
497 wrmsr(MSR_IA32_APICBASE, l, h);
498 apic_write(APIC_LVTERR, ERROR_APIC_VECTOR | APIC_LVT_MASKED);
499 apic_write(APIC_ID, apic_pm_state.apic_id);
500 apic_write(APIC_DFR, apic_pm_state.apic_dfr);
501 apic_write(APIC_LDR, apic_pm_state.apic_ldr);
502 apic_write(APIC_TASKPRI, apic_pm_state.apic_taskpri);
503 apic_write(APIC_SPIV, apic_pm_state.apic_spiv);
504 apic_write(APIC_LVT0, apic_pm_state.apic_lvt0);
505 apic_write(APIC_LVT1, apic_pm_state.apic_lvt1);
506 apic_write(APIC_LVTPC, apic_pm_state.apic_lvtpc);
507 apic_write(APIC_LVTT, apic_pm_state.apic_lvtt);
508 apic_write(APIC_TDCR, apic_pm_state.apic_tdcr);
509 apic_write(APIC_TMICT, apic_pm_state.apic_tmict);
510 apic_write(APIC_ESR, 0);
512 apic_write(APIC_LVTERR, apic_pm_state.apic_lvterr);
513 apic_write(APIC_ESR, 0);
515 __restore_flags(flags);
516 if (apic_pm_state.perfctr_pmdev)
517 pm_send(apic_pm_state.perfctr_pmdev, PM_RESUME, data);
520 static int apic_pm_callback(struct pm_dev *dev, pm_request_t rqst, void *data)
524 apic_pm_suspend(data);
527 apic_pm_resume(data);
533 /* perfctr driver should call this instead of pm_register() */
534 struct pm_dev *apic_pm_register(pm_dev_t type,
536 pm_callback callback)
540 if (!apic_pm_state.active)
541 return pm_register(type, id, callback);
542 if (apic_pm_state.perfctr_pmdev)
543 return NULL; /* we're busy */
544 dev = kmalloc(sizeof(struct pm_dev), GFP_KERNEL);
546 memset(dev, 0, sizeof(*dev));
549 dev->callback = callback;
550 apic_pm_state.perfctr_pmdev = dev;
555 /* perfctr driver should call this instead of pm_unregister() */
556 void apic_pm_unregister(struct pm_dev *dev)
558 if (!apic_pm_state.active) {
560 } else if (dev == apic_pm_state.perfctr_pmdev) {
561 apic_pm_state.perfctr_pmdev = NULL;
566 static void __init apic_pm_init1(void)
568 /* can't pm_register() at this early stage in the boot process
569 (causes an immediate reboot), so just set the flag */
570 apic_pm_state.active = 1;
573 static void __init apic_pm_init2(void)
575 if (apic_pm_state.active)
576 pm_register(PM_SYS_DEV, 0, apic_pm_callback);
579 #else /* CONFIG_PM */
581 static inline void apic_pm_init1(void) { }
582 static inline void apic_pm_init2(void) { }
584 #endif /* CONFIG_PM */
587 * Detect and enable local APICs on non-SMP boards.
588 * Original code written by Keir Fraser.
590 int dont_enable_local_apic __initdata = 0;
592 static int __init detect_init_APIC (void)
594 /* Disabled by DMI scan or kernel option? */
595 if (dont_enable_local_apic)
598 /* Don't force the APIC if the BIOS didn't enable it. Usually
599 it is broken in some way. */
601 printk(KERN_INFO "APIC disabled by BIOS.\n");
604 set_bit(X86_FEATURE_APIC, &boot_cpu_data.x86_capability);
605 mp_lapic_addr = APIC_DEFAULT_PHYS_BASE;
607 if (nmi_watchdog != NMI_NONE)
608 nmi_watchdog = NMI_LOCAL_APIC;
610 printk(KERN_INFO "Found and enabled local APIC!\n");
617 void __init init_apic_mappings(void)
619 unsigned long apic_phys;
622 * If no local APIC can be found then set up a fake all
623 * zeroes page to simulate the local APIC and another
624 * one for the IO-APIC.
626 if (!smp_found_config && detect_init_APIC()) {
627 apic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
628 apic_phys = __pa(apic_phys);
630 apic_phys = mp_lapic_addr;
632 set_fixmap_nocache(FIX_APIC_BASE, apic_phys);
633 Dprintk("mapped APIC to %08lx (%08lx)\n", APIC_BASE, apic_phys);
636 * Fetch the APIC ID of the BSP in case we have a
637 * default configuration (or the MP table is broken).
639 if (boot_cpu_id == -1U)
640 boot_cpu_id = GET_APIC_ID(apic_read(APIC_ID));
642 #ifdef CONFIG_X86_IO_APIC
644 unsigned long ioapic_phys, idx = FIX_IO_APIC_BASE_0;
647 for (i = 0; i < nr_ioapics; i++) {
648 if (smp_found_config) {
649 ioapic_phys = mp_ioapics[i].mpc_apicaddr;
651 printk(KERN_ERR "WARNING: bogus zero IO-APIC address found in MPTABLE, disabling IO-APIC support\n");
652 smp_found_config = 0;
653 skip_ioapic_setup = 1;
654 goto fake_ioapic_page;
658 ioapic_phys = (unsigned long) alloc_bootmem_pages(PAGE_SIZE);
659 ioapic_phys = __pa(ioapic_phys);
661 set_fixmap_nocache(idx, ioapic_phys);
662 Dprintk("mapped IOAPIC to %08lx (%08lx)\n",
663 __fix_to_virt(idx), ioapic_phys);
671 * This part sets up the APIC 32 bit clock in LVTT1, with HZ interrupts
672 * per second. We assume that the caller has already set up the local
677 * This function sets up the local APIC timer, with a timeout of
678 * 'clocks' APIC bus clock. During calibration we actually call
679 * this function twice on the boot CPU, once with a bogus timeout
680 * value, second time for real. The other (noncalibrating) CPUs
681 * call this function only once, with the real, calibrated value.
683 * We do reads before writes even if unnecessary, to get around the
684 * P5 APIC double write bug.
687 #define APIC_DIVISOR 16
689 void __setup_APIC_LVTT(unsigned int clocks)
691 unsigned int lvtt1_value, tmp_value;
693 lvtt1_value = SET_APIC_TIMER_BASE(APIC_TIMER_BASE_DIV) |
694 APIC_LVT_TIMER_PERIODIC | LOCAL_TIMER_VECTOR;
695 apic_write_around(APIC_LVTT, lvtt1_value);
700 tmp_value = apic_read(APIC_TDCR);
701 apic_write_around(APIC_TDCR, (tmp_value
702 & ~(APIC_TDR_DIV_1 | APIC_TDR_DIV_TMBASE))
705 apic_write_around(APIC_TMICT, clocks/APIC_DIVISOR);
708 void setup_APIC_timer(void * data)
710 unsigned int clocks = (unsigned long) data, slice, t0, t1;
714 * ok, Intel has some smart code in their APIC that knows
715 * if a CPU was in 'hlt' lowpower mode, and this increases
716 * its APIC arbitration priority. To avoid the external timer
717 * IRQ APIC event being in synchron with the APIC clock we
718 * introduce an interrupt skew to spread out timer events.
720 * The number of slices within a 'big' timeslice is smp_num_cpus+1
723 slice = clocks / (smp_num_cpus+1);
724 printk(KERN_INFO "cpu: %d, clocks: %d, slice: %d\n",
725 smp_processor_id(), clocks, slice);
728 * Wait for timer IRQ slice:
732 int trigger = hpet_readl(HPET_T0_CMP);
733 while (hpet_readl(HPET_COUNTER) >= trigger);
734 while (hpet_readl(HPET_COUNTER) < trigger);
737 spin_lock(&i8253_lock);
740 c2 |= inb_p(0x40) << 8;
745 c2 |= inb_p(0x40) << 8;
746 } while (c2 - c1 < 300);
747 spin_unlock(&i8253_lock);
750 __setup_APIC_LVTT(clocks);
752 t0 = apic_read(APIC_TMICT)*APIC_DIVISOR;
754 /* Wait till TMCCT gets reloaded from TMICT... */
756 t1 = apic_read(APIC_TMCCT)*APIC_DIVISOR;
757 delta = (int)(t0 - t1 - slice*(smp_processor_id()+1));
758 } while (delta >= 0);
760 /* Now wait for our slice for real. */
762 t1 = apic_read(APIC_TMCCT)*APIC_DIVISOR;
763 delta = (int)(t0 - t1 - slice*(smp_processor_id()+1));
766 __setup_APIC_LVTT(clocks);
768 printk(KERN_INFO "CPU%d<T0:%u,T1:%u,D:%d,S:%u,C:%u>\n",
769 smp_processor_id(), t0, t1, delta, slice, clocks);
773 * In this function we calibrate APIC bus clocks to the external timer.
774 * Unlike on i386 we don't use the PIT to do that, but rely on a
775 * pre-calibrated TSC counter instead.
777 * We want to do the calibration only once since we want to have local timer
778 * irqs syncron. CPUs connected by the same APIC bus have the very same bus
779 * frequency. And we want to have irqs off anyways, no accidental APIC irq
783 #define TICK_COUNT 100000000
785 int __init calibrate_APIC_clock(void)
787 int apic, apic_start, tsc, tsc_start;
790 * Put whatever arbitrary (but long enough) timeout
791 * value into the APIC clock, we just want to get the
792 * counter running for calibration.
794 __setup_APIC_LVTT(1000000000);
796 apic_start = apic_read(APIC_TMCCT);
800 apic = apic_read(APIC_TMCCT);
802 } while ((tsc - tsc_start) < TICK_COUNT && (apic - apic_start) < TICK_COUNT);
804 result = (apic_start - apic) * 1000L * cpu_khz / (tsc - tsc_start);
806 printk("Detected %d.%03d MHz APIC timer.\n",
807 result / 1000 / 1000, result / 1000 % 1000);
809 return result * APIC_DIVISOR / HZ;
812 static unsigned int calibration_result;
814 int dont_use_local_apic_timer __initdata = 0;
816 void __init setup_APIC_clocks (void)
818 /* Disabled by DMI scan or kernel option? */
819 if (dont_use_local_apic_timer)
822 printk("Using local APIC timer interrupts.\n");
823 using_apic_timer = 1;
827 calibration_result = calibrate_APIC_clock();
829 * Now set up the timer for real.
831 setup_APIC_timer((void *)(u64)calibration_result);
835 /* and update all other cpus */
836 smp_call_function(setup_APIC_timer, (void *)(u64)calibration_result, 1, 1);
840 * the frequency of the profiling timer can be changed
841 * by writing a multiplier value into /proc/profile.
843 int setup_profiling_timer(unsigned int multiplier)
848 * Sanity check. [at least 500 APIC cycles should be
849 * between APIC interrupts as a rule of thumb, to avoid
852 if ( (!multiplier) || (calibration_result/multiplier < 500))
856 * Set the new multiplier for each CPU. CPUs don't start using the
857 * new values until the next timer interrupt in which they do process
858 * accounting. At that time they also adjust their APIC timers
861 for (i = 0; i < NR_CPUS; ++i)
862 prof_multiplier[i] = multiplier;
870 * Local timer interrupt handler. It does both profiling and
871 * process statistics/rescheduling.
873 * We do profiling in every local tick, statistics/rescheduling
874 * happen only every 'profiling multiplier' ticks. The default
875 * multiplier is 1 and it can be changed by writing the new multiplier
876 * value into /proc/profile.
879 inline void smp_local_timer_interrupt(struct pt_regs *regs)
881 int user = user_mode(regs);
882 int cpu = smp_processor_id();
885 * The profiling function is SMP safe. (nothing can mess
886 * around with "current", and the profiling counters are
887 * updated with atomic operations). This is especially
888 * useful with a profiling multiplier != 1
891 x86_do_profile(regs->rip);
893 if (--prof_counter[cpu] <= 0) {
895 * The multiplier may have changed since the last time we got
896 * to this point as a result of the user writing to
897 * /proc/profile. In this case we need to adjust the APIC
900 * Interrupts are already masked off at this point.
902 prof_counter[cpu] = prof_multiplier[cpu];
903 if (prof_counter[cpu] != prof_old_multiplier[cpu]) {
904 __setup_APIC_LVTT(calibration_result/prof_counter[cpu]);
905 prof_old_multiplier[cpu] = prof_counter[cpu];
909 update_process_times(user);
914 * We take the 'long' return path, and there every subsystem
915 * grabs the apropriate locks (kernel lock/ irq lock).
917 * we might want to decouple profiling from the 'long path',
918 * and do the profiling totally in assembly.
920 * Currently this isn't too much of an issue (performance wise),
921 * we can take more than 100K local irqs per second on a 100 MHz P5.
926 * Local APIC timer interrupt. This is the most natural way for doing
927 * local interrupts, but local timer interrupts can be emulated by
928 * broadcast interrupts too. [in case the hw doesnt support APIC timers]
930 * [ if a single-CPU system runs an SMP kernel then we call the local
931 * interrupt as well. Thus we cannot inline the local irq ... ]
933 unsigned int apic_timer_irqs [NR_CPUS];
935 void smp_apic_timer_interrupt(struct pt_regs *regs)
937 int cpu = smp_processor_id();
940 * the NMI deadlock-detector uses this.
942 apic_timer_irqs[cpu]++;
945 * NOTE! We'd better ACK the irq immediately,
946 * because timer handling can be slow.
950 * update_process_times() expects us to have done irq_enter().
951 * Besides, if we don't timer interrupts ignore the global
952 * interrupt lock, which is the WrongThing (tm) to do.
955 smp_local_timer_interrupt(regs);
958 if (softirq_pending(cpu))
963 * This interrupt should _never_ happen with our APIC/SMP architecture
965 asmlinkage void smp_spurious_interrupt(void)
968 static unsigned long last_warning;
969 static unsigned long skipped;
972 * Check if this really is a spurious interrupt and ACK it
973 * if it is a vectored one. Just in case...
974 * Spurious interrupts should not be ACKed.
976 v = apic_read(APIC_ISR + ((SPURIOUS_APIC_VECTOR & ~0x1f) >> 1));
977 if (v & (1 << (SPURIOUS_APIC_VECTOR & 0x1f)))
980 /* see sw-dev-man vol 3, chapter 7.4.13.5 */
981 if (last_warning+30*HZ < jiffies) {
982 printk(KERN_INFO "spurious APIC interrupt on CPU#%d, %ld skipped.\n",
983 smp_processor_id(), skipped);
984 last_warning = jiffies;
992 * This interrupt should never happen with our APIC/SMP architecture
995 asmlinkage void smp_error_interrupt(void)
999 /* First tickle the hardware, only then report what went on. -- REW */
1000 v = apic_read(APIC_ESR);
1001 apic_write(APIC_ESR, 0);
1002 v1 = apic_read(APIC_ESR);
1004 atomic_inc(&irq_err_count);
1006 /* Here is what the APIC error bits mean:
1009 2: Send accept error
1010 3: Receive accept error
1012 5: Send illegal vector
1013 6: Received illegal vector
1014 7: Illegal register address
1016 printk (KERN_ERR "APIC error on CPU%d: %02x(%02x)\n",
1017 smp_processor_id(), v , v1);
1021 * This initializes the IO-APIC and APIC hardware if this is
1024 int __init APIC_init_uniprocessor (void)
1027 clear_bit(X86_FEATURE_APIC, &boot_cpu_data.x86_capability);
1029 if (!cpu_has_apic) {
1030 printk("No APIC available.\n");
1036 * Complain if the BIOS pretends there is one.
1038 if (!cpu_has_apic && APIC_INTEGRATED(apic_version[boot_cpu_id])) {
1039 printk(KERN_ERR "BIOS bug, local APIC #%d not detected!...\n",
1045 verify_local_APIC();
1049 phys_cpu_present_map = 1;
1050 apic_write_around(APIC_ID, boot_cpu_id);
1056 if (nmi_watchdog == NMI_LOCAL_APIC)
1057 check_nmi_watchdog();
1058 #ifdef CONFIG_X86_IO_APIC
1059 if (smp_found_config && !skip_ioapic_setup && nr_ioapics)
1064 setup_APIC_clocks();
1069 static int disable_local_apic(char *s)
1074 __setup("nolocalapic", disable_local_apic);