2 * Copyright (c) 1996, by Steve Passe
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. The name of the developer may NOT be used to endorse or promote products
11 * derived from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * $FreeBSD: src/sys/i386/i386/mpapic.c,v 1.37.2.7 2003/01/25 02:31:47 peter Exp $
28 #include <sys/param.h>
29 #include <sys/systm.h>
30 #include <sys/kernel.h>
32 #include <sys/machintr.h>
33 #include <machine/globaldata.h>
34 #include <machine/smp.h>
35 #include <machine/md_var.h>
36 #include <machine/pmap.h>
37 #include <machine_base/apic/mpapic.h>
38 #include <machine_base/apic/ioapic_abi.h>
39 #include <machine/segments.h>
40 #include <sys/thread2.h>
42 #include <machine/intr_machdep.h>
46 #define IOAPIC_COUNT_MAX 16
47 #define IOAPIC_ID_MASK (IOAPIC_COUNT_MAX - 1)
49 /* EISA Edge/Level trigger control registers */
50 #define ELCR0 0x4d0 /* eisa irq 0-7 */
51 #define ELCR1 0x4d1 /* eisa irq 8-15 */
60 TAILQ_ENTRY(ioapic_info) io_link;
62 TAILQ_HEAD(ioapic_info_list, ioapic_info);
64 struct ioapic_intsrc {
66 enum intr_trigger int_trig;
67 enum intr_polarity int_pola;
71 struct ioapic_info_list ioc_list;
72 struct ioapic_intsrc ioc_intsrc[16]; /* XXX magic number */
75 volatile lapic_t *lapic;
77 static void lapic_timer_calibrate(void);
78 static void lapic_timer_set_divisor(int);
79 static void lapic_timer_fixup_handler(void *);
80 static void lapic_timer_restart_handler(void *);
82 void lapic_timer_process(void);
83 void lapic_timer_process_frame(struct intrframe *);
84 void lapic_timer_always(struct intrframe *);
86 static int lapic_timer_enable = 1;
87 TUNABLE_INT("hw.lapic_timer_enable", &lapic_timer_enable);
89 static void lapic_timer_intr_reload(struct cputimer_intr *, sysclock_t);
90 static void lapic_timer_intr_enable(struct cputimer_intr *);
91 static void lapic_timer_intr_restart(struct cputimer_intr *);
92 static void lapic_timer_intr_pmfixup(struct cputimer_intr *);
94 static int lapic_unused_apic_id(int);
96 static void ioapic_setup(const struct ioapic_info *);
97 static int ioapic_alloc_apic_id(int);
98 static void ioapic_set_apic_id(const struct ioapic_info *);
99 static void ioapic_gsi_setup(int);
100 static const struct ioapic_info *
101 ioapic_gsi_search(int);
102 static void ioapic_pin_prog(void *, int, int,
103 enum intr_trigger, enum intr_polarity, uint32_t);
105 static struct cputimer_intr lapic_cputimer_intr = {
107 .reload = lapic_timer_intr_reload,
108 .enable = lapic_timer_intr_enable,
109 .config = cputimer_intr_default_config,
110 .restart = lapic_timer_intr_restart,
111 .pmfixup = lapic_timer_intr_pmfixup,
112 .initclock = cputimer_intr_default_initclock,
113 .next = SLIST_ENTRY_INITIALIZER,
115 .type = CPUTIMER_INTR_LAPIC,
116 .prio = CPUTIMER_INTR_PRIO_LAPIC,
117 .caps = CPUTIMER_INTR_CAP_NONE
121 * pointers to pmapped apic hardware.
124 volatile ioapic_t **ioapic;
126 static int lapic_timer_divisor_idx = -1;
127 static const uint32_t lapic_timer_divisors[] = {
128 APIC_TDCR_2, APIC_TDCR_4, APIC_TDCR_8, APIC_TDCR_16,
129 APIC_TDCR_32, APIC_TDCR_64, APIC_TDCR_128, APIC_TDCR_1
131 #define APIC_TIMER_NDIVISORS (int)(NELEM(lapic_timer_divisors))
134 static struct ioapic_conf ioapic_conf;
144 * Enable LAPIC, configure interrupts.
147 lapic_init(boolean_t bsp)
155 * Since IDT is shared between BSP and APs, these vectors
156 * only need to be installed once; we do it on BSP.
159 /* Install a 'Spurious INTerrupt' vector */
160 setidt(XSPURIOUSINT_OFFSET, Xspuriousint,
161 SDT_SYSIGT, SEL_KPL, 0);
163 /* Install an inter-CPU IPI for TLB invalidation */
164 setidt(XINVLTLB_OFFSET, Xinvltlb,
165 SDT_SYSIGT, SEL_KPL, 0);
167 /* Install an inter-CPU IPI for IPIQ messaging */
168 setidt(XIPIQ_OFFSET, Xipiq,
169 SDT_SYSIGT, SEL_KPL, 0);
171 /* Install a timer vector */
172 setidt(XTIMER_OFFSET, Xtimer,
173 SDT_SYSIGT, SEL_KPL, 0);
175 /* Install an inter-CPU IPI for CPU stop/restart */
176 setidt(XCPUSTOP_OFFSET, Xcpustop,
177 SDT_SYSIGT, SEL_KPL, 0);
181 * Setup LINT0 as ExtINT on the BSP. This is theoretically an
182 * aggregate interrupt input from the 8259. The INTA cycle
183 * will be routed to the external controller (the 8259) which
184 * is expected to supply the vector.
186 * Must be setup edge triggered, active high.
188 * Disable LINT0 on BSP, if I/O APIC is enabled.
190 * Disable LINT0 on the APs. It doesn't matter what delivery
191 * mode we use because we leave it masked.
193 temp = lapic->lvt_lint0;
194 temp &= ~(APIC_LVT_MASKED | APIC_LVT_TRIG_MASK |
195 APIC_LVT_POLARITY_MASK | APIC_LVT_DM_MASK);
197 temp |= APIC_LVT_DM_EXTINT;
199 temp |= APIC_LVT_MASKED;
201 temp |= APIC_LVT_DM_FIXED | APIC_LVT_MASKED;
203 lapic->lvt_lint0 = temp;
206 * Setup LINT1 as NMI.
208 * Must be setup edge trigger, active high.
210 * Enable LINT1 on BSP, if I/O APIC is enabled.
212 * Disable LINT1 on the APs.
214 temp = lapic->lvt_lint1;
215 temp &= ~(APIC_LVT_MASKED | APIC_LVT_TRIG_MASK |
216 APIC_LVT_POLARITY_MASK | APIC_LVT_DM_MASK);
217 temp |= APIC_LVT_MASKED | APIC_LVT_DM_NMI;
218 if (bsp && apic_io_enable)
219 temp &= ~APIC_LVT_MASKED;
220 lapic->lvt_lint1 = temp;
223 * Mask the LAPIC error interrupt, LAPIC performance counter
226 lapic->lvt_error = lapic->lvt_error | APIC_LVT_MASKED;
227 lapic->lvt_pcint = lapic->lvt_pcint | APIC_LVT_MASKED;
230 * Set LAPIC timer vector and mask the LAPIC timer interrupt.
232 timer = lapic->lvt_timer;
233 timer &= ~APIC_LVTT_VECTOR;
234 timer |= XTIMER_OFFSET;
235 timer |= APIC_LVTT_MASKED;
236 lapic->lvt_timer = timer;
239 * Set the Task Priority Register as needed. At the moment allow
240 * interrupts on all cpus (the APs will remain CLId until they are
241 * ready to deal). We could disable all but IPIs by setting
242 * temp |= TPR_IPI for cpu != 0.
245 temp &= ~APIC_TPR_PRIO; /* clear priority field */
246 #ifdef SMP /* APIC-IO */
247 if (!apic_io_enable) {
250 * If we are NOT running the IO APICs, the LAPIC will only be used
251 * for IPIs. Set the TPR to prevent any unintentional interrupts.
254 #ifdef SMP /* APIC-IO */
263 temp |= APIC_SVR_ENABLE; /* enable the LAPIC */
264 temp &= ~APIC_SVR_FOCUS_DISABLE; /* enable lopri focus processor */
267 * Set the spurious interrupt vector. The low 4 bits of the vector
270 if ((XSPURIOUSINT_OFFSET & 0x0F) != 0x0F)
271 panic("bad XSPURIOUSINT_OFFSET: 0x%08x", XSPURIOUSINT_OFFSET);
272 temp &= ~APIC_SVR_VECTOR;
273 temp |= XSPURIOUSINT_OFFSET;
278 * Pump out a few EOIs to clean out interrupts that got through
279 * before we were able to set the TPR.
286 lapic_timer_calibrate();
287 if (lapic_timer_enable) {
288 cputimer_intr_register(&lapic_cputimer_intr);
289 cputimer_intr_select(&lapic_cputimer_intr, 0);
292 lapic_timer_set_divisor(lapic_timer_divisor_idx);
296 apic_dump("apic_initialize()");
300 lapic_timer_set_divisor(int divisor_idx)
302 KKASSERT(divisor_idx >= 0 && divisor_idx < APIC_TIMER_NDIVISORS);
303 lapic->dcr_timer = lapic_timer_divisors[divisor_idx];
307 lapic_timer_oneshot(u_int count)
311 value = lapic->lvt_timer;
312 value &= ~APIC_LVTT_PERIODIC;
313 lapic->lvt_timer = value;
314 lapic->icr_timer = count;
318 lapic_timer_oneshot_quick(u_int count)
320 lapic->icr_timer = count;
324 lapic_timer_calibrate(void)
328 /* Try to calibrate the local APIC timer. */
329 for (lapic_timer_divisor_idx = 0;
330 lapic_timer_divisor_idx < APIC_TIMER_NDIVISORS;
331 lapic_timer_divisor_idx++) {
332 lapic_timer_set_divisor(lapic_timer_divisor_idx);
333 lapic_timer_oneshot(APIC_TIMER_MAX_COUNT);
335 value = APIC_TIMER_MAX_COUNT - lapic->ccr_timer;
336 if (value != APIC_TIMER_MAX_COUNT)
339 if (lapic_timer_divisor_idx >= APIC_TIMER_NDIVISORS)
340 panic("lapic: no proper timer divisor?!\n");
341 lapic_cputimer_intr.freq = value / 2;
343 kprintf("lapic: divisor index %d, frequency %u Hz\n",
344 lapic_timer_divisor_idx, lapic_cputimer_intr.freq);
348 lapic_timer_process_oncpu(struct globaldata *gd, struct intrframe *frame)
352 gd->gd_timer_running = 0;
354 count = sys_cputimer->count();
355 if (TAILQ_FIRST(&gd->gd_systimerq) != NULL)
356 systimer_intr(&count, 0, frame);
360 lapic_timer_process(void)
362 lapic_timer_process_oncpu(mycpu, NULL);
366 lapic_timer_process_frame(struct intrframe *frame)
368 lapic_timer_process_oncpu(mycpu, frame);
372 * This manual debugging code is called unconditionally from Xtimer
373 * (the lapic timer interrupt) whether the current thread is in a
374 * critical section or not) and can be useful in tracking down lockups.
376 * NOTE: MANUAL DEBUG CODE
379 static int saveticks[SMP_MAXCPU];
380 static int savecounts[SMP_MAXCPU];
384 lapic_timer_always(struct intrframe *frame)
387 globaldata_t gd = mycpu;
388 int cpu = gd->gd_cpuid;
394 gptr = (short *)0xFFFFFFFF800b8000 + 80 * cpu;
395 *gptr = ((*gptr + 1) & 0x00FF) | 0x0700;
398 ksnprintf(buf, sizeof(buf), " %p %16s %d %16s ",
399 (void *)frame->if_rip, gd->gd_curthread->td_comm, ticks,
401 for (i = 0; buf[i]; ++i) {
402 gptr[i] = 0x0700 | (unsigned char)buf[i];
406 if (saveticks[gd->gd_cpuid] != ticks) {
407 saveticks[gd->gd_cpuid] = ticks;
408 savecounts[gd->gd_cpuid] = 0;
410 ++savecounts[gd->gd_cpuid];
411 if (savecounts[gd->gd_cpuid] > 2000 && panicstr == NULL) {
412 panic("cpud %d panicing on ticks failure",
415 for (i = 0; i < ncpus; ++i) {
417 if (saveticks[i] && panicstr == NULL) {
418 delta = saveticks[i] - ticks;
419 if (delta < -10 || delta > 10) {
420 panic("cpu %d panicing on cpu %d watchdog",
430 lapic_timer_intr_reload(struct cputimer_intr *cti, sysclock_t reload)
432 struct globaldata *gd = mycpu;
434 reload = (int64_t)reload * cti->freq / sys_cputimer->freq;
438 if (gd->gd_timer_running) {
439 if (reload < lapic->ccr_timer)
440 lapic_timer_oneshot_quick(reload);
442 gd->gd_timer_running = 1;
443 lapic_timer_oneshot_quick(reload);
448 lapic_timer_intr_enable(struct cputimer_intr *cti __unused)
452 timer = lapic->lvt_timer;
453 timer &= ~(APIC_LVTT_MASKED | APIC_LVTT_PERIODIC);
454 lapic->lvt_timer = timer;
456 lapic_timer_fixup_handler(NULL);
460 lapic_timer_fixup_handler(void *arg)
467 if (strcmp(cpu_vendor, "AuthenticAMD") == 0) {
469 * Detect the presence of C1E capability mostly on latest
470 * dual-cores (or future) k8 family. This feature renders
471 * the local APIC timer dead, so we disable it by reading
472 * the Interrupt Pending Message register and clearing both
473 * C1eOnCmpHalt (bit 28) and SmiOnCmpHalt (bit 27).
476 * "BIOS and Kernel Developer's Guide for AMD NPT
477 * Family 0Fh Processors"
478 * #32559 revision 3.00
480 if ((cpu_id & 0x00000f00) == 0x00000f00 &&
481 (cpu_id & 0x0fff0000) >= 0x00040000) {
484 msr = rdmsr(0xc0010055);
485 if (msr & 0x18000000) {
486 struct globaldata *gd = mycpu;
488 kprintf("cpu%d: AMD C1E detected\n",
490 wrmsr(0xc0010055, msr & ~0x18000000ULL);
493 * We are kinda stalled;
496 gd->gd_timer_running = 1;
497 lapic_timer_oneshot_quick(2);
507 lapic_timer_restart_handler(void *dummy __unused)
511 lapic_timer_fixup_handler(&started);
513 struct globaldata *gd = mycpu;
515 gd->gd_timer_running = 1;
516 lapic_timer_oneshot_quick(2);
521 * This function is called only by ACPI-CA code currently:
522 * - AMD C1E fixup. AMD C1E only seems to happen after ACPI
523 * module controls PM. So once ACPI-CA is attached, we try
524 * to apply the fixup to prevent LAPIC timer from hanging.
527 lapic_timer_intr_pmfixup(struct cputimer_intr *cti __unused)
529 lwkt_send_ipiq_mask(smp_active_mask,
530 lapic_timer_fixup_handler, NULL);
534 lapic_timer_intr_restart(struct cputimer_intr *cti __unused)
536 lwkt_send_ipiq_mask(smp_active_mask, lapic_timer_restart_handler, NULL);
541 * dump contents of local APIC registers
546 kprintf("SMP: CPU%d %s:\n", mycpu->gd_cpuid, str);
547 kprintf(" lint0: 0x%08x lint1: 0x%08x TPR: 0x%08x SVR: 0x%08x\n",
548 lapic->lvt_lint0, lapic->lvt_lint1, lapic->tpr, lapic->svr);
552 #ifdef SMP /* APIC-IO */
558 #define IOAPIC_ISA_INTS 16
559 #define REDIRCNT_IOAPIC(A) \
560 ((int)((io_apic_versions[(A)] & IOART_VER_MAXREDIR) >> MAXREDIRSHIFT) + 1)
562 static int trigger (int apic, int pin, u_int32_t * flags);
563 static void polarity (int apic, int pin, u_int32_t * flags, int level);
565 #define DEFAULT_FLAGS \
571 #define DEFAULT_ISA_FLAGS \
580 io_apic_set_id(int apic, int id)
584 ux = ioapic_read(ioapic[apic], IOAPIC_ID); /* get current contents */
585 if (((ux & APIC_ID_MASK) >> 24) != id) {
586 kprintf("Changing APIC ID for IO APIC #%d"
587 " from %d to %d on chip\n",
588 apic, ((ux & APIC_ID_MASK) >> 24), id);
589 ux &= ~APIC_ID_MASK; /* clear the ID field */
591 ioapic_write(ioapic[apic], IOAPIC_ID, ux); /* write new value */
592 ux = ioapic_read(ioapic[apic], IOAPIC_ID); /* re-read && test */
593 if (((ux & APIC_ID_MASK) >> 24) != id)
594 panic("can't control IO APIC #%d ID, reg: 0x%08x",
601 io_apic_get_id(int apic)
603 return (ioapic_read(ioapic[apic], IOAPIC_ID) & APIC_ID_MASK) >> 24;
612 io_apic_setup_intpin(int apic, int pin)
614 int bus, bustype, irq;
615 u_char select; /* the select register is 8 bits */
616 u_int32_t flags; /* the window register is 32 bits */
617 u_int32_t target; /* the window register is 32 bits */
618 u_int32_t vector; /* the window register is 32 bits */
623 select = pin * 2 + IOAPIC_REDTBL0; /* register */
626 * Always clear an IO APIC pin before [re]programming it. This is
627 * particularly important if the pin is set up for a level interrupt
628 * as the IOART_REM_IRR bit might be set. When we reprogram the
629 * vector any EOI from pending ints on this pin could be lost and
630 * IRR might never get reset.
632 * To fix this problem, clear the vector and make sure it is
633 * programmed as an edge interrupt. This should theoretically
634 * clear IRR so we can later, safely program it as a level
639 flags = ioapic_read(ioapic[apic], select) & IOART_RESV;
640 flags |= IOART_INTMSET | IOART_TRGREDG | IOART_INTAHI;
641 flags |= IOART_DESTPHY | IOART_DELFIXED;
643 target = ioapic_read(ioapic[apic], select + 1) & IOART_HI_DEST_RESV;
644 target |= 0; /* fixed mode cpu mask of 0 - don't deliver anywhere */
648 ioapic_write(ioapic[apic], select, flags | vector);
649 ioapic_write(ioapic[apic], select + 1, target);
654 * We only deal with vectored interrupts here. ? documentation is
655 * lacking, I'm guessing an interrupt type of 0 is the 'INT' type,
658 * This test also catches unconfigured pins.
660 if (apic_int_type(apic, pin) != 0)
664 * Leave the pin unprogrammed if it does not correspond to
667 irq = apic_irq(apic, pin);
671 /* determine the bus type for this pin */
672 bus = apic_src_bus_id(apic, pin);
675 bustype = apic_bus_type(bus);
677 if ((bustype == ISA) &&
678 (pin < IOAPIC_ISA_INTS) &&
680 (apic_polarity(apic, pin) == 0x1) &&
681 (apic_trigger(apic, pin) == 0x3)) {
683 * A broken BIOS might describe some ISA
684 * interrupts as active-high level-triggered.
685 * Use default ISA flags for those interrupts.
687 flags = DEFAULT_ISA_FLAGS;
690 * Program polarity and trigger mode according to
693 flags = DEFAULT_FLAGS;
694 level = trigger(apic, pin, &flags);
696 int_to_apicintpin[irq].flags |= IOAPIC_IM_FLAG_LEVEL;
697 polarity(apic, pin, &flags, level);
701 ksnprintf(envpath, sizeof(envpath), "hw.irq.%d.dest", irq);
702 kgetenv_int(envpath, &cpuid);
704 /* ncpus may not be available yet */
709 kprintf("IOAPIC #%d intpin %d -> irq %d (CPU%d)\n",
710 apic, pin, irq, cpuid);
714 * Program the appropriate registers. This routing may be
715 * overridden when an interrupt handler for a device is
716 * actually added (see register_int(), which calls through
717 * the MACHINTR ABI to set up an interrupt handler/vector).
719 * The order in which we must program the two registers for
720 * safety is unclear! XXX
724 vector = IDT_OFFSET + irq; /* IDT vec */
725 target = ioapic_read(ioapic[apic], select + 1) & IOART_HI_DEST_RESV;
726 /* Deliver all interrupts to CPU0 (BSP) */
727 target |= (CPU_TO_ID(cpuid) << IOART_HI_DEST_SHIFT) &
729 flags |= ioapic_read(ioapic[apic], select) & IOART_RESV;
730 ioapic_write(ioapic[apic], select, flags | vector);
731 ioapic_write(ioapic[apic], select + 1, target);
737 io_apic_setup(int apic)
742 maxpin = REDIRCNT_IOAPIC(apic); /* pins in APIC */
743 kprintf("Programming %d pins in IOAPIC #%d\n", maxpin, apic);
745 for (pin = 0; pin < maxpin; ++pin) {
746 io_apic_setup_intpin(apic, pin);
749 if (apic_int_type(apic, pin) >= 0) {
750 kprintf("Warning: IOAPIC #%d pin %d does not exist,"
751 " cannot program!\n", apic, pin);
756 /* return GOOD status */
759 #undef DEFAULT_ISA_FLAGS
763 #define DEFAULT_EXTINT_FLAGS \
772 * XXX this function is only used by 8254 setup
773 * Setup the source of External INTerrupts.
776 ext_int_setup(int apic, int intr)
778 u_char select; /* the select register is 8 bits */
779 u_int32_t flags; /* the window register is 32 bits */
780 u_int32_t target; /* the window register is 32 bits */
781 u_int32_t vector; /* the window register is 32 bits */
785 if (apic_int_type(apic, intr) != 3)
789 ksnprintf(envpath, sizeof(envpath), "hw.irq.%d.dest", intr);
790 kgetenv_int(envpath, &cpuid);
792 /* ncpus may not be available yet */
796 /* Deliver interrupts to CPU0 (BSP) */
797 target = (CPU_TO_ID(cpuid) << IOART_HI_DEST_SHIFT) &
799 select = IOAPIC_REDTBL0 + (2 * intr);
800 vector = IDT_OFFSET + intr;
801 flags = DEFAULT_EXTINT_FLAGS;
803 ioapic_write(ioapic[apic], select, flags | vector);
804 ioapic_write(ioapic[apic], select + 1, target);
808 #undef DEFAULT_EXTINT_FLAGS
812 * Set the trigger level for an IO APIC pin.
815 trigger(int apic, int pin, u_int32_t * flags)
820 static int intcontrol = -1;
822 switch (apic_trigger(apic, pin)) {
828 *flags &= ~IOART_TRGRLVL; /* *flags |= IOART_TRGREDG */
832 *flags |= IOART_TRGRLVL;
840 if ((id = apic_src_bus_id(apic, pin)) == -1)
843 switch (apic_bus_type(id)) {
845 *flags &= ~IOART_TRGRLVL; /* *flags |= IOART_TRGREDG; */
849 eirq = apic_src_bus_irq(apic, pin);
851 if (eirq < 0 || eirq > 15) {
852 kprintf("EISA IRQ %d?!?!\n", eirq);
856 if (intcontrol == -1) {
857 intcontrol = inb(ELCR1) << 8;
858 intcontrol |= inb(ELCR0);
859 kprintf("EISA INTCONTROL = %08x\n", intcontrol);
862 /* Use ELCR settings to determine level or edge mode */
863 level = (intcontrol >> eirq) & 1;
866 * Note that on older Neptune chipset based systems, any
867 * pci interrupts often show up here and in the ELCR as well
868 * as level sensitive interrupts attributed to the EISA bus.
872 *flags |= IOART_TRGRLVL;
874 *flags &= ~IOART_TRGRLVL;
879 *flags |= IOART_TRGRLVL;
888 panic("bad APIC IO INT flags");
893 * Set the polarity value for an IO APIC pin.
896 polarity(int apic, int pin, u_int32_t * flags, int level)
900 switch (apic_polarity(apic, pin)) {
906 *flags &= ~IOART_INTALO; /* *flags |= IOART_INTAHI */
910 *flags |= IOART_INTALO;
918 if ((id = apic_src_bus_id(apic, pin)) == -1)
921 switch (apic_bus_type(id)) {
923 *flags &= ~IOART_INTALO; /* *flags |= IOART_INTAHI */
927 /* polarity converter always gives active high */
928 *flags &= ~IOART_INTALO;
932 *flags |= IOART_INTALO;
941 panic("bad APIC IO INT flags");
946 * Print contents of unmasked IRQs.
953 kprintf("SMP: enabled INTs: ");
954 for (x = 0; x < APIC_INTMAPSIZE; ++x) {
955 if ((int_to_apicintpin[x].flags & IOAPIC_IM_FLAG_MASKED) == 0)
963 * Inter Processor Interrupt functions.
966 #endif /* SMP APIC-IO */
969 * Send APIC IPI 'vector' to 'destType' via 'deliveryMode'.
971 * destType is 1 of: APIC_DEST_SELF, APIC_DEST_ALLISELF, APIC_DEST_ALLESELF
972 * vector is any valid SYSTEM INT vector
973 * delivery_mode is 1 of: APIC_DELMODE_FIXED, APIC_DELMODE_LOWPRIO
975 * A backlog of requests can create a deadlock between cpus. To avoid this
976 * we have to be able to accept IPIs at the same time we are trying to send
977 * them. The critical section prevents us from attempting to send additional
978 * IPIs reentrantly, but also prevents IPIQ processing so we have to call
979 * lwkt_process_ipiq() manually. It's rather messy and expensive for this
980 * to occur but fortunately it does not happen too often.
983 apic_ipi(int dest_type, int vector, int delivery_mode)
988 if ((lapic->icr_lo & APIC_DELSTAT_MASK) != 0) {
989 unsigned long rflags = read_rflags();
991 DEBUG_PUSH_INFO("apic_ipi");
992 while ((lapic->icr_lo & APIC_DELSTAT_MASK) != 0) {
996 write_rflags(rflags);
999 icr_lo = (lapic->icr_lo & APIC_ICRLO_RESV_MASK) | dest_type |
1000 delivery_mode | vector;
1001 lapic->icr_lo = icr_lo;
1007 single_apic_ipi(int cpu, int vector, int delivery_mode)
1013 if ((lapic->icr_lo & APIC_DELSTAT_MASK) != 0) {
1014 unsigned long rflags = read_rflags();
1016 DEBUG_PUSH_INFO("single_apic_ipi");
1017 while ((lapic->icr_lo & APIC_DELSTAT_MASK) != 0) {
1018 lwkt_process_ipiq();
1021 write_rflags(rflags);
1023 icr_hi = lapic->icr_hi & ~APIC_ID_MASK;
1024 icr_hi |= (CPU_TO_ID(cpu) << 24);
1025 lapic->icr_hi = icr_hi;
1028 icr_lo = (lapic->icr_lo & APIC_ICRLO_RESV_MASK)
1029 | APIC_DEST_DESTFLD | delivery_mode | vector;
1031 /* write APIC ICR */
1032 lapic->icr_lo = icr_lo;
1039 * Returns 0 if the apic is busy, 1 if we were able to queue the request.
1041 * NOT WORKING YET! The code as-is may end up not queueing an IPI at all
1042 * to the target, and the scheduler does not 'poll' for IPI messages.
1045 single_apic_ipi_passive(int cpu, int vector, int delivery_mode)
1051 if ((lapic->icr_lo & APIC_DELSTAT_MASK) != 0) {
1055 icr_hi = lapic->icr_hi & ~APIC_ID_MASK;
1056 icr_hi |= (CPU_TO_ID(cpu) << 24);
1057 lapic->icr_hi = icr_hi;
1060 icr_lo = (lapic->icr_lo & APIC_RESV2_MASK)
1061 | APIC_DEST_DESTFLD | delivery_mode | vector;
1063 /* write APIC ICR */
1064 lapic->icr_lo = icr_lo;
1072 * Send APIC IPI 'vector' to 'target's via 'delivery_mode'.
1074 * target is a bitmask of destination cpus. Vector is any
1075 * valid system INT vector. Delivery mode may be either
1076 * APIC_DELMODE_FIXED or APIC_DELMODE_LOWPRIO.
1079 selected_apic_ipi(cpumask_t target, int vector, int delivery_mode)
1083 int n = BSFCPUMASK(target);
1084 target &= ~CPUMASK(n);
1085 single_apic_ipi(n, vector, delivery_mode);
1091 * Timer code, in development...
1092 * - suggested by rgrimes@gndrsh.aac.dev.com
1095 get_apic_timer_frequency(void)
1097 return(lapic_cputimer_intr.freq);
1101 * Load a 'downcount time' in uSeconds.
1104 set_apic_timer(int us)
1109 * When we reach here, lapic timer's frequency
1110 * must have been calculated as well as the
1111 * divisor (lapic->dcr_timer is setup during the
1112 * divisor calculation).
1114 KKASSERT(lapic_cputimer_intr.freq != 0 &&
1115 lapic_timer_divisor_idx >= 0);
1117 count = ((us * (int64_t)lapic_cputimer_intr.freq) + 999999) / 1000000;
1118 lapic_timer_oneshot(count);
1123 * Read remaining time in timer.
1126 read_apic_timer(void)
1129 /** XXX FIXME: we need to return the actual remaining time,
1130 * for now we just return the remaining count.
1133 return lapic->ccr_timer;
1139 * Spin-style delay, set delay time in uS, spin till it drains.
1144 set_apic_timer(count);
1145 while (read_apic_timer())
1150 lapic_unused_apic_id(int start)
1154 for (i = start; i < NAPICID; ++i) {
1155 if (ID_TO_CPU(i) == -1)
1162 lapic_map(vm_offset_t lapic_addr)
1164 lapic = pmap_mapdev_uncacheable(lapic_addr, sizeof(struct LAPIC));
1166 kprintf("lapic: at 0x%08lx\n", lapic_addr);
1169 static TAILQ_HEAD(, lapic_enumerator) lapic_enumerators =
1170 TAILQ_HEAD_INITIALIZER(lapic_enumerators);
1175 struct lapic_enumerator *e;
1178 for (i = 0; i < NAPICID; ++i)
1181 TAILQ_FOREACH(e, &lapic_enumerators, lapic_link) {
1182 error = e->lapic_probe(e);
1187 panic("can't config lapic\n");
1189 e->lapic_enumerate(e);
1193 lapic_enumerator_register(struct lapic_enumerator *ne)
1195 struct lapic_enumerator *e;
1197 TAILQ_FOREACH(e, &lapic_enumerators, lapic_link) {
1198 if (e->lapic_prio < ne->lapic_prio) {
1199 TAILQ_INSERT_BEFORE(e, ne, lapic_link);
1203 TAILQ_INSERT_TAIL(&lapic_enumerators, ne, lapic_link);
1206 static TAILQ_HEAD(, ioapic_enumerator) ioapic_enumerators =
1207 TAILQ_HEAD_INITIALIZER(ioapic_enumerators);
1212 struct ioapic_enumerator *e;
1216 TAILQ_INIT(&ioapic_conf.ioc_list);
1217 /* XXX magic number */
1218 for (i = 0; i < 16; ++i)
1219 ioapic_conf.ioc_intsrc[i].int_gsi = -1;
1221 TAILQ_FOREACH(e, &ioapic_enumerators, ioapic_link) {
1222 error = e->ioapic_probe(e);
1228 panic("can't config I/O APIC\n");
1230 kprintf("no I/O APIC\n");
1235 if (!ioapic_use_old) {
1242 * Switch to I/O APIC MachIntrABI and reconfigure
1243 * the default IDT entries.
1245 MachIntrABI = MachIntrABI_IOAPIC;
1246 MachIntrABI.setdefault();
1249 e->ioapic_enumerate(e);
1251 if (!ioapic_use_old) {
1252 struct ioapic_info *info;
1253 int start_apic_id = 0;
1259 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link)
1262 if (i > IOAPIC_COUNT_MAX) /* XXX magic number */
1263 panic("ioapic_config: more than 16 I/O APIC\n");
1268 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link) {
1271 apic_id = ioapic_alloc_apic_id(start_apic_id);
1272 if (apic_id == NAPICID) {
1273 kprintf("IOAPIC: can't alloc APIC ID for "
1274 "%dth I/O APIC\n", info->io_idx);
1277 info->io_apic_id = apic_id;
1279 start_apic_id = apic_id + 1;
1283 * xAPIC allows I/O APIC's APIC ID to be same
1284 * as the LAPIC's APIC ID
1286 kprintf("IOAPIC: use xAPIC model to alloc APIC ID "
1289 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link)
1290 info->io_apic_id = info->io_idx;
1294 * Warning about any GSI holes
1296 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link) {
1297 const struct ioapic_info *prev_info;
1299 prev_info = TAILQ_PREV(info, ioapic_info_list, io_link);
1300 if (prev_info != NULL) {
1301 if (info->io_gsi_base !=
1302 prev_info->io_gsi_base + prev_info->io_npin) {
1303 kprintf("IOAPIC: warning gsi hole "
1305 prev_info->io_gsi_base +
1307 info->io_gsi_base - 1);
1313 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link) {
1314 kprintf("IOAPIC: idx %d, apic id %d, "
1315 "gsi base %d, npin %d\n",
1324 * Setup all I/O APIC
1326 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link)
1328 ioapic_abi_fixup_irqmap();
1332 MachIntrABI.cleanup();
1339 ioapic_enumerator_register(struct ioapic_enumerator *ne)
1341 struct ioapic_enumerator *e;
1343 TAILQ_FOREACH(e, &ioapic_enumerators, ioapic_link) {
1344 if (e->ioapic_prio < ne->ioapic_prio) {
1345 TAILQ_INSERT_BEFORE(e, ne, ioapic_link);
1349 TAILQ_INSERT_TAIL(&ioapic_enumerators, ne, ioapic_link);
1353 ioapic_add(void *addr, int gsi_base, int npin)
1355 struct ioapic_info *info, *ninfo;
1358 gsi_end = gsi_base + npin - 1;
1359 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link) {
1360 if ((gsi_base >= info->io_gsi_base &&
1361 gsi_base < info->io_gsi_base + info->io_npin) ||
1362 (gsi_end >= info->io_gsi_base &&
1363 gsi_end < info->io_gsi_base + info->io_npin)) {
1364 panic("ioapic_add: overlapped gsi, base %d npin %d, "
1365 "hit base %d, npin %d\n", gsi_base, npin,
1366 info->io_gsi_base, info->io_npin);
1368 if (info->io_addr == addr)
1369 panic("ioapic_add: duplicated addr %p\n", addr);
1372 ninfo = kmalloc(sizeof(*ninfo), M_DEVBUF, M_WAITOK | M_ZERO);
1373 ninfo->io_addr = addr;
1374 ninfo->io_npin = npin;
1375 ninfo->io_gsi_base = gsi_base;
1376 ninfo->io_apic_id = -1;
1379 * Create IOAPIC list in ascending order of GSI base
1381 TAILQ_FOREACH_REVERSE(info, &ioapic_conf.ioc_list,
1382 ioapic_info_list, io_link) {
1383 if (ninfo->io_gsi_base > info->io_gsi_base) {
1384 TAILQ_INSERT_AFTER(&ioapic_conf.ioc_list,
1385 info, ninfo, io_link);
1390 TAILQ_INSERT_HEAD(&ioapic_conf.ioc_list, ninfo, io_link);
1394 ioapic_intsrc(int irq, int gsi, enum intr_trigger trig, enum intr_polarity pola)
1396 struct ioapic_intsrc *int_src;
1399 int_src = &ioapic_conf.ioc_intsrc[irq];
1402 /* Don't allow mixed mode */
1403 kprintf("IOAPIC: warning intsrc irq %d -> gsi 0\n", irq);
1407 if (int_src->int_gsi != -1) {
1408 if (int_src->int_gsi != gsi) {
1409 kprintf("IOAPIC: warning intsrc irq %d, gsi "
1410 "%d -> %d\n", irq, int_src->int_gsi, gsi);
1412 if (int_src->int_trig != trig) {
1413 kprintf("IOAPIC: warning intsrc irq %d, trig "
1415 intr_str_trigger(int_src->int_trig),
1416 intr_str_trigger(trig));
1418 if (int_src->int_pola != pola) {
1419 kprintf("IOAPIC: warning intsrc irq %d, pola "
1421 intr_str_polarity(int_src->int_pola),
1422 intr_str_polarity(pola));
1425 int_src->int_gsi = gsi;
1426 int_src->int_trig = trig;
1427 int_src->int_pola = pola;
1431 ioapic_set_apic_id(const struct ioapic_info *info)
1436 id = ioapic_read(info->io_addr, IOAPIC_ID);
1438 id &= ~APIC_ID_MASK;
1439 id |= (info->io_apic_id << 24);
1441 ioapic_write(info->io_addr, IOAPIC_ID, id);
1446 id = ioapic_read(info->io_addr, IOAPIC_ID);
1447 apic_id = (id & APIC_ID_MASK) >> 24;
1450 * I/O APIC ID is a 4bits field
1452 if ((apic_id & IOAPIC_ID_MASK) !=
1453 (info->io_apic_id & IOAPIC_ID_MASK)) {
1454 panic("ioapic_set_apic_id: can't set apic id to %d, "
1455 "currently set to %d\n", info->io_apic_id, apic_id);
1460 ioapic_gsi_setup(int gsi)
1462 enum intr_trigger trig;
1463 enum intr_polarity pola;
1469 ioapic_extpin_setup(ioapic_gsi_ioaddr(gsi),
1470 ioapic_gsi_pin(gsi), 0);
1475 for (irq = 0; irq < 16; ++irq) {
1476 const struct ioapic_intsrc *int_src =
1477 &ioapic_conf.ioc_intsrc[irq];
1479 if (gsi == int_src->int_gsi) {
1480 trig = int_src->int_trig;
1481 pola = int_src->int_pola;
1488 trig = INTR_TRIGGER_EDGE;
1489 pola = INTR_POLARITY_HIGH;
1491 trig = INTR_TRIGGER_LEVEL;
1492 pola = INTR_POLARITY_LOW;
1497 ioapic_abi_set_irqmap(irq, gsi, trig, pola);
1501 ioapic_gsi_ioaddr(int gsi)
1503 const struct ioapic_info *info;
1505 info = ioapic_gsi_search(gsi);
1506 return info->io_addr;
1510 ioapic_gsi_pin(int gsi)
1512 const struct ioapic_info *info;
1514 info = ioapic_gsi_search(gsi);
1515 return gsi - info->io_gsi_base;
1518 static const struct ioapic_info *
1519 ioapic_gsi_search(int gsi)
1521 const struct ioapic_info *info;
1523 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link) {
1524 if (gsi >= info->io_gsi_base &&
1525 gsi < info->io_gsi_base + info->io_npin)
1528 panic("ioapic_gsi_search: no I/O APIC\n");
1532 ioapic_gsi(int idx, int pin)
1534 const struct ioapic_info *info;
1536 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link) {
1537 if (info->io_idx == idx)
1542 if (pin >= info->io_npin)
1544 return info->io_gsi_base + pin;
1548 ioapic_extpin_setup(void *addr, int pin, int vec)
1550 ioapic_pin_prog(addr, pin, vec,
1551 INTR_TRIGGER_CONFORM, INTR_POLARITY_CONFORM, IOART_DELEXINT);
1555 ioapic_extpin_gsi(void)
1561 ioapic_pin_setup(void *addr, int pin, int vec,
1562 enum intr_trigger trig, enum intr_polarity pola)
1565 * Always clear an I/O APIC pin before [re]programming it. This is
1566 * particularly important if the pin is set up for a level interrupt
1567 * as the IOART_REM_IRR bit might be set. When we reprogram the
1568 * vector any EOI from pending ints on this pin could be lost and
1569 * IRR might never get reset.
1571 * To fix this problem, clear the vector and make sure it is
1572 * programmed as an edge interrupt. This should theoretically
1573 * clear IRR so we can later, safely program it as a level
1576 ioapic_pin_prog(addr, pin, vec, INTR_TRIGGER_EDGE, INTR_POLARITY_HIGH,
1578 ioapic_pin_prog(addr, pin, vec, trig, pola, IOART_DELFIXED);
1582 ioapic_pin_prog(void *addr, int pin, int vec,
1583 enum intr_trigger trig, enum intr_polarity pola, uint32_t del_mode)
1585 uint32_t flags, target;
1588 KKASSERT(del_mode == IOART_DELEXINT || del_mode == IOART_DELFIXED);
1590 select = IOAPIC_REDTBL0 + (2 * pin);
1592 flags = ioapic_read(addr, select) & IOART_RESV;
1593 flags |= IOART_INTMSET | IOART_DESTPHY;
1598 * We only support limited I/O APIC mixed mode,
1599 * so even for ExtINT, we still use "fixed"
1602 flags |= IOART_DELFIXED;
1605 if (del_mode == IOART_DELEXINT) {
1606 KKASSERT(trig == INTR_TRIGGER_CONFORM &&
1607 pola == INTR_POLARITY_CONFORM);
1608 flags |= IOART_TRGREDG | IOART_INTAHI;
1611 case INTR_TRIGGER_EDGE:
1612 flags |= IOART_TRGREDG;
1615 case INTR_TRIGGER_LEVEL:
1616 flags |= IOART_TRGRLVL;
1619 case INTR_TRIGGER_CONFORM:
1620 panic("ioapic_pin_prog: trig conform is not "
1624 case INTR_POLARITY_HIGH:
1625 flags |= IOART_INTAHI;
1628 case INTR_POLARITY_LOW:
1629 flags |= IOART_INTALO;
1632 case INTR_POLARITY_CONFORM:
1633 panic("ioapic_pin_prog: pola conform is not "
1638 target = ioapic_read(addr, select + 1) & IOART_HI_DEST_RESV;
1639 target |= (CPU_TO_ID(0) << IOART_HI_DEST_SHIFT) &
1642 ioapic_write(addr, select, flags | vec);
1643 ioapic_write(addr, select + 1, target);
1647 ioapic_setup(const struct ioapic_info *info)
1651 ioapic_set_apic_id(info);
1653 for (i = 0; i < info->io_npin; ++i)
1654 ioapic_gsi_setup(info->io_gsi_base + i);
1658 ioapic_alloc_apic_id(int start)
1661 const struct ioapic_info *info;
1662 int apic_id, apic_id16;
1664 apic_id = lapic_unused_apic_id(start);
1665 if (apic_id == NAPICID) {
1666 kprintf("IOAPIC: can't find unused APIC ID\n");
1669 apic_id16 = apic_id & IOAPIC_ID_MASK;
1672 * Check against other I/O APIC's APIC ID's lower 4bits.
1674 * The new APIC ID will have to be different from others
1675 * in the lower 4bits, no matter whether xAPIC is used
1678 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link) {
1679 if (info->io_apic_id == -1) {
1683 if ((info->io_apic_id & IOAPIC_ID_MASK) == apic_id16)
1689 kprintf("IOAPIC: APIC ID %d has same lower 4bits as "
1690 "%dth I/O APIC, keep searching...\n",
1691 apic_id, info->io_idx);
1693 start = apic_id + 1;
1695 panic("ioapic_unused_apic_id: never reached\n");