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);
65 struct ioapic_info_list ioc_list;
66 int ioc_intsrc[16]; /* XXX magic number */
69 volatile lapic_t *lapic;
71 static void lapic_timer_calibrate(void);
72 static void lapic_timer_set_divisor(int);
73 static void lapic_timer_fixup_handler(void *);
74 static void lapic_timer_restart_handler(void *);
76 void lapic_timer_process(void);
77 void lapic_timer_process_frame(struct intrframe *);
78 void lapic_timer_always(struct intrframe *);
80 static int lapic_timer_enable = 1;
81 TUNABLE_INT("hw.lapic_timer_enable", &lapic_timer_enable);
83 static void lapic_timer_intr_reload(struct cputimer_intr *, sysclock_t);
84 static void lapic_timer_intr_enable(struct cputimer_intr *);
85 static void lapic_timer_intr_restart(struct cputimer_intr *);
86 static void lapic_timer_intr_pmfixup(struct cputimer_intr *);
88 static int lapic_unused_apic_id(int);
90 static void ioapic_setup(const struct ioapic_info *);
91 static int ioapic_alloc_apic_id(int);
92 static void ioapic_set_apic_id(const struct ioapic_info *);
93 static void ioapic_gsi_setup(int);
94 static const struct ioapic_info *
95 ioapic_gsi_search(int);
96 static void ioapic_pin_prog(void *, int, int,
97 enum intr_trigger, enum intr_polarity, uint32_t);
99 static struct cputimer_intr lapic_cputimer_intr = {
101 .reload = lapic_timer_intr_reload,
102 .enable = lapic_timer_intr_enable,
103 .config = cputimer_intr_default_config,
104 .restart = lapic_timer_intr_restart,
105 .pmfixup = lapic_timer_intr_pmfixup,
106 .initclock = cputimer_intr_default_initclock,
107 .next = SLIST_ENTRY_INITIALIZER,
109 .type = CPUTIMER_INTR_LAPIC,
110 .prio = CPUTIMER_INTR_PRIO_LAPIC,
111 .caps = CPUTIMER_INTR_CAP_NONE
115 * pointers to pmapped apic hardware.
118 volatile ioapic_t **ioapic;
120 static int lapic_timer_divisor_idx = -1;
121 static const uint32_t lapic_timer_divisors[] = {
122 APIC_TDCR_2, APIC_TDCR_4, APIC_TDCR_8, APIC_TDCR_16,
123 APIC_TDCR_32, APIC_TDCR_64, APIC_TDCR_128, APIC_TDCR_1
125 #define APIC_TIMER_NDIVISORS (int)(NELEM(lapic_timer_divisors))
128 static struct ioapic_conf ioapic_conf;
138 * Enable LAPIC, configure interrupts.
141 lapic_init(boolean_t bsp)
149 * Since IDT is shared between BSP and APs, these vectors
150 * only need to be installed once; we do it on BSP.
153 /* Install a 'Spurious INTerrupt' vector */
154 setidt(XSPURIOUSINT_OFFSET, Xspuriousint,
155 SDT_SYSIGT, SEL_KPL, 0);
157 /* Install an inter-CPU IPI for TLB invalidation */
158 setidt(XINVLTLB_OFFSET, Xinvltlb,
159 SDT_SYSIGT, SEL_KPL, 0);
161 /* Install an inter-CPU IPI for IPIQ messaging */
162 setidt(XIPIQ_OFFSET, Xipiq,
163 SDT_SYSIGT, SEL_KPL, 0);
165 /* Install a timer vector */
166 setidt(XTIMER_OFFSET, Xtimer,
167 SDT_SYSIGT, SEL_KPL, 0);
169 /* Install an inter-CPU IPI for CPU stop/restart */
170 setidt(XCPUSTOP_OFFSET, Xcpustop,
171 SDT_SYSIGT, SEL_KPL, 0);
175 * Setup LINT0 as ExtINT on the BSP. This is theoretically an
176 * aggregate interrupt input from the 8259. The INTA cycle
177 * will be routed to the external controller (the 8259) which
178 * is expected to supply the vector.
180 * Must be setup edge triggered, active high.
182 * Disable LINT0 on BSP, if I/O APIC is enabled.
184 * Disable LINT0 on the APs. It doesn't matter what delivery
185 * mode we use because we leave it masked.
187 temp = lapic->lvt_lint0;
188 temp &= ~(APIC_LVT_MASKED | APIC_LVT_TRIG_MASK |
189 APIC_LVT_POLARITY_MASK | APIC_LVT_DM_MASK);
191 temp |= APIC_LVT_DM_EXTINT;
193 temp |= APIC_LVT_MASKED;
195 temp |= APIC_LVT_DM_FIXED | APIC_LVT_MASKED;
197 lapic->lvt_lint0 = temp;
200 * Setup LINT1 as NMI.
202 * Must be setup edge trigger, active high.
204 * Enable LINT1 on BSP, if I/O APIC is enabled.
206 * Disable LINT1 on the APs.
208 temp = lapic->lvt_lint1;
209 temp &= ~(APIC_LVT_MASKED | APIC_LVT_TRIG_MASK |
210 APIC_LVT_POLARITY_MASK | APIC_LVT_DM_MASK);
211 temp |= APIC_LVT_MASKED | APIC_LVT_DM_NMI;
212 if (bsp && apic_io_enable)
213 temp &= ~APIC_LVT_MASKED;
214 lapic->lvt_lint1 = temp;
217 * Mask the LAPIC error interrupt, LAPIC performance counter
220 lapic->lvt_error = lapic->lvt_error | APIC_LVT_MASKED;
221 lapic->lvt_pcint = lapic->lvt_pcint | APIC_LVT_MASKED;
224 * Set LAPIC timer vector and mask the LAPIC timer interrupt.
226 timer = lapic->lvt_timer;
227 timer &= ~APIC_LVTT_VECTOR;
228 timer |= XTIMER_OFFSET;
229 timer |= APIC_LVTT_MASKED;
230 lapic->lvt_timer = timer;
233 * Set the Task Priority Register as needed. At the moment allow
234 * interrupts on all cpus (the APs will remain CLId until they are
235 * ready to deal). We could disable all but IPIs by setting
236 * temp |= TPR_IPI for cpu != 0.
239 temp &= ~APIC_TPR_PRIO; /* clear priority field */
240 #ifdef SMP /* APIC-IO */
241 if (!apic_io_enable) {
244 * If we are NOT running the IO APICs, the LAPIC will only be used
245 * for IPIs. Set the TPR to prevent any unintentional interrupts.
248 #ifdef SMP /* APIC-IO */
257 temp |= APIC_SVR_ENABLE; /* enable the LAPIC */
258 temp &= ~APIC_SVR_FOCUS_DISABLE; /* enable lopri focus processor */
261 * Set the spurious interrupt vector. The low 4 bits of the vector
264 if ((XSPURIOUSINT_OFFSET & 0x0F) != 0x0F)
265 panic("bad XSPURIOUSINT_OFFSET: 0x%08x", XSPURIOUSINT_OFFSET);
266 temp &= ~APIC_SVR_VECTOR;
267 temp |= XSPURIOUSINT_OFFSET;
272 * Pump out a few EOIs to clean out interrupts that got through
273 * before we were able to set the TPR.
280 lapic_timer_calibrate();
281 if (lapic_timer_enable) {
282 cputimer_intr_register(&lapic_cputimer_intr);
283 cputimer_intr_select(&lapic_cputimer_intr, 0);
286 lapic_timer_set_divisor(lapic_timer_divisor_idx);
290 apic_dump("apic_initialize()");
294 lapic_timer_set_divisor(int divisor_idx)
296 KKASSERT(divisor_idx >= 0 && divisor_idx < APIC_TIMER_NDIVISORS);
297 lapic->dcr_timer = lapic_timer_divisors[divisor_idx];
301 lapic_timer_oneshot(u_int count)
305 value = lapic->lvt_timer;
306 value &= ~APIC_LVTT_PERIODIC;
307 lapic->lvt_timer = value;
308 lapic->icr_timer = count;
312 lapic_timer_oneshot_quick(u_int count)
314 lapic->icr_timer = count;
318 lapic_timer_calibrate(void)
322 /* Try to calibrate the local APIC timer. */
323 for (lapic_timer_divisor_idx = 0;
324 lapic_timer_divisor_idx < APIC_TIMER_NDIVISORS;
325 lapic_timer_divisor_idx++) {
326 lapic_timer_set_divisor(lapic_timer_divisor_idx);
327 lapic_timer_oneshot(APIC_TIMER_MAX_COUNT);
329 value = APIC_TIMER_MAX_COUNT - lapic->ccr_timer;
330 if (value != APIC_TIMER_MAX_COUNT)
333 if (lapic_timer_divisor_idx >= APIC_TIMER_NDIVISORS)
334 panic("lapic: no proper timer divisor?!\n");
335 lapic_cputimer_intr.freq = value / 2;
337 kprintf("lapic: divisor index %d, frequency %u Hz\n",
338 lapic_timer_divisor_idx, lapic_cputimer_intr.freq);
342 lapic_timer_process_oncpu(struct globaldata *gd, struct intrframe *frame)
346 gd->gd_timer_running = 0;
348 count = sys_cputimer->count();
349 if (TAILQ_FIRST(&gd->gd_systimerq) != NULL)
350 systimer_intr(&count, 0, frame);
354 lapic_timer_process(void)
356 lapic_timer_process_oncpu(mycpu, NULL);
360 lapic_timer_process_frame(struct intrframe *frame)
362 lapic_timer_process_oncpu(mycpu, frame);
366 * This manual debugging code is called unconditionally from Xtimer
367 * (the lapic timer interrupt) whether the current thread is in a
368 * critical section or not) and can be useful in tracking down lockups.
370 * NOTE: MANUAL DEBUG CODE
373 static int saveticks[SMP_MAXCPU];
374 static int savecounts[SMP_MAXCPU];
378 lapic_timer_always(struct intrframe *frame)
381 globaldata_t gd = mycpu;
382 int cpu = gd->gd_cpuid;
388 gptr = (short *)0xFFFFFFFF800b8000 + 80 * cpu;
389 *gptr = ((*gptr + 1) & 0x00FF) | 0x0700;
392 ksnprintf(buf, sizeof(buf), " %p %16s %d %16s ",
393 (void *)frame->if_rip, gd->gd_curthread->td_comm, ticks,
395 for (i = 0; buf[i]; ++i) {
396 gptr[i] = 0x0700 | (unsigned char)buf[i];
400 if (saveticks[gd->gd_cpuid] != ticks) {
401 saveticks[gd->gd_cpuid] = ticks;
402 savecounts[gd->gd_cpuid] = 0;
404 ++savecounts[gd->gd_cpuid];
405 if (savecounts[gd->gd_cpuid] > 2000 && panicstr == NULL) {
406 panic("cpud %d panicing on ticks failure",
409 for (i = 0; i < ncpus; ++i) {
411 if (saveticks[i] && panicstr == NULL) {
412 delta = saveticks[i] - ticks;
413 if (delta < -10 || delta > 10) {
414 panic("cpu %d panicing on cpu %d watchdog",
424 lapic_timer_intr_reload(struct cputimer_intr *cti, sysclock_t reload)
426 struct globaldata *gd = mycpu;
428 reload = (int64_t)reload * cti->freq / sys_cputimer->freq;
432 if (gd->gd_timer_running) {
433 if (reload < lapic->ccr_timer)
434 lapic_timer_oneshot_quick(reload);
436 gd->gd_timer_running = 1;
437 lapic_timer_oneshot_quick(reload);
442 lapic_timer_intr_enable(struct cputimer_intr *cti __unused)
446 timer = lapic->lvt_timer;
447 timer &= ~(APIC_LVTT_MASKED | APIC_LVTT_PERIODIC);
448 lapic->lvt_timer = timer;
450 lapic_timer_fixup_handler(NULL);
454 lapic_timer_fixup_handler(void *arg)
461 if (strcmp(cpu_vendor, "AuthenticAMD") == 0) {
463 * Detect the presence of C1E capability mostly on latest
464 * dual-cores (or future) k8 family. This feature renders
465 * the local APIC timer dead, so we disable it by reading
466 * the Interrupt Pending Message register and clearing both
467 * C1eOnCmpHalt (bit 28) and SmiOnCmpHalt (bit 27).
470 * "BIOS and Kernel Developer's Guide for AMD NPT
471 * Family 0Fh Processors"
472 * #32559 revision 3.00
474 if ((cpu_id & 0x00000f00) == 0x00000f00 &&
475 (cpu_id & 0x0fff0000) >= 0x00040000) {
478 msr = rdmsr(0xc0010055);
479 if (msr & 0x18000000) {
480 struct globaldata *gd = mycpu;
482 kprintf("cpu%d: AMD C1E detected\n",
484 wrmsr(0xc0010055, msr & ~0x18000000ULL);
487 * We are kinda stalled;
490 gd->gd_timer_running = 1;
491 lapic_timer_oneshot_quick(2);
501 lapic_timer_restart_handler(void *dummy __unused)
505 lapic_timer_fixup_handler(&started);
507 struct globaldata *gd = mycpu;
509 gd->gd_timer_running = 1;
510 lapic_timer_oneshot_quick(2);
515 * This function is called only by ACPI-CA code currently:
516 * - AMD C1E fixup. AMD C1E only seems to happen after ACPI
517 * module controls PM. So once ACPI-CA is attached, we try
518 * to apply the fixup to prevent LAPIC timer from hanging.
521 lapic_timer_intr_pmfixup(struct cputimer_intr *cti __unused)
523 lwkt_send_ipiq_mask(smp_active_mask,
524 lapic_timer_fixup_handler, NULL);
528 lapic_timer_intr_restart(struct cputimer_intr *cti __unused)
530 lwkt_send_ipiq_mask(smp_active_mask, lapic_timer_restart_handler, NULL);
535 * dump contents of local APIC registers
540 kprintf("SMP: CPU%d %s:\n", mycpu->gd_cpuid, str);
541 kprintf(" lint0: 0x%08x lint1: 0x%08x TPR: 0x%08x SVR: 0x%08x\n",
542 lapic->lvt_lint0, lapic->lvt_lint1, lapic->tpr, lapic->svr);
546 #ifdef SMP /* APIC-IO */
552 #define IOAPIC_ISA_INTS 16
553 #define REDIRCNT_IOAPIC(A) \
554 ((int)((io_apic_versions[(A)] & IOART_VER_MAXREDIR) >> MAXREDIRSHIFT) + 1)
556 static int trigger (int apic, int pin, u_int32_t * flags);
557 static void polarity (int apic, int pin, u_int32_t * flags, int level);
559 #define DEFAULT_FLAGS \
565 #define DEFAULT_ISA_FLAGS \
574 io_apic_set_id(int apic, int id)
578 ux = ioapic_read(ioapic[apic], IOAPIC_ID); /* get current contents */
579 if (((ux & APIC_ID_MASK) >> 24) != id) {
580 kprintf("Changing APIC ID for IO APIC #%d"
581 " from %d to %d on chip\n",
582 apic, ((ux & APIC_ID_MASK) >> 24), id);
583 ux &= ~APIC_ID_MASK; /* clear the ID field */
585 ioapic_write(ioapic[apic], IOAPIC_ID, ux); /* write new value */
586 ux = ioapic_read(ioapic[apic], IOAPIC_ID); /* re-read && test */
587 if (((ux & APIC_ID_MASK) >> 24) != id)
588 panic("can't control IO APIC #%d ID, reg: 0x%08x",
595 io_apic_get_id(int apic)
597 return (ioapic_read(ioapic[apic], IOAPIC_ID) & APIC_ID_MASK) >> 24;
606 io_apic_setup_intpin(int apic, int pin)
608 int bus, bustype, irq;
609 u_char select; /* the select register is 8 bits */
610 u_int32_t flags; /* the window register is 32 bits */
611 u_int32_t target; /* the window register is 32 bits */
612 u_int32_t vector; /* the window register is 32 bits */
617 select = pin * 2 + IOAPIC_REDTBL0; /* register */
620 * Always clear an IO APIC pin before [re]programming it. This is
621 * particularly important if the pin is set up for a level interrupt
622 * as the IOART_REM_IRR bit might be set. When we reprogram the
623 * vector any EOI from pending ints on this pin could be lost and
624 * IRR might never get reset.
626 * To fix this problem, clear the vector and make sure it is
627 * programmed as an edge interrupt. This should theoretically
628 * clear IRR so we can later, safely program it as a level
633 flags = ioapic_read(ioapic[apic], select) & IOART_RESV;
634 flags |= IOART_INTMSET | IOART_TRGREDG | IOART_INTAHI;
635 flags |= IOART_DESTPHY | IOART_DELFIXED;
637 target = ioapic_read(ioapic[apic], select + 1) & IOART_HI_DEST_RESV;
638 target |= 0; /* fixed mode cpu mask of 0 - don't deliver anywhere */
642 ioapic_write(ioapic[apic], select, flags | vector);
643 ioapic_write(ioapic[apic], select + 1, target);
648 * We only deal with vectored interrupts here. ? documentation is
649 * lacking, I'm guessing an interrupt type of 0 is the 'INT' type,
652 * This test also catches unconfigured pins.
654 if (apic_int_type(apic, pin) != 0)
658 * Leave the pin unprogrammed if it does not correspond to
661 irq = apic_irq(apic, pin);
665 /* determine the bus type for this pin */
666 bus = apic_src_bus_id(apic, pin);
669 bustype = apic_bus_type(bus);
671 if ((bustype == ISA) &&
672 (pin < IOAPIC_ISA_INTS) &&
674 (apic_polarity(apic, pin) == 0x1) &&
675 (apic_trigger(apic, pin) == 0x3)) {
677 * A broken BIOS might describe some ISA
678 * interrupts as active-high level-triggered.
679 * Use default ISA flags for those interrupts.
681 flags = DEFAULT_ISA_FLAGS;
684 * Program polarity and trigger mode according to
687 flags = DEFAULT_FLAGS;
688 level = trigger(apic, pin, &flags);
690 int_to_apicintpin[irq].flags |= IOAPIC_IM_FLAG_LEVEL;
691 polarity(apic, pin, &flags, level);
695 ksnprintf(envpath, sizeof(envpath), "hw.irq.%d.dest", irq);
696 kgetenv_int(envpath, &cpuid);
698 /* ncpus may not be available yet */
703 kprintf("IOAPIC #%d intpin %d -> irq %d (CPU%d)\n",
704 apic, pin, irq, cpuid);
708 * Program the appropriate registers. This routing may be
709 * overridden when an interrupt handler for a device is
710 * actually added (see register_int(), which calls through
711 * the MACHINTR ABI to set up an interrupt handler/vector).
713 * The order in which we must program the two registers for
714 * safety is unclear! XXX
718 vector = IDT_OFFSET + irq; /* IDT vec */
719 target = ioapic_read(ioapic[apic], select + 1) & IOART_HI_DEST_RESV;
720 /* Deliver all interrupts to CPU0 (BSP) */
721 target |= (CPU_TO_ID(cpuid) << IOART_HI_DEST_SHIFT) &
723 flags |= ioapic_read(ioapic[apic], select) & IOART_RESV;
724 ioapic_write(ioapic[apic], select, flags | vector);
725 ioapic_write(ioapic[apic], select + 1, target);
731 io_apic_setup(int apic)
736 maxpin = REDIRCNT_IOAPIC(apic); /* pins in APIC */
737 kprintf("Programming %d pins in IOAPIC #%d\n", maxpin, apic);
739 for (pin = 0; pin < maxpin; ++pin) {
740 io_apic_setup_intpin(apic, pin);
743 if (apic_int_type(apic, pin) >= 0) {
744 kprintf("Warning: IOAPIC #%d pin %d does not exist,"
745 " cannot program!\n", apic, pin);
750 /* return GOOD status */
753 #undef DEFAULT_ISA_FLAGS
757 #define DEFAULT_EXTINT_FLAGS \
766 * XXX this function is only used by 8254 setup
767 * Setup the source of External INTerrupts.
770 ext_int_setup(int apic, int intr)
772 u_char select; /* the select register is 8 bits */
773 u_int32_t flags; /* the window register is 32 bits */
774 u_int32_t target; /* the window register is 32 bits */
775 u_int32_t vector; /* the window register is 32 bits */
779 if (apic_int_type(apic, intr) != 3)
783 ksnprintf(envpath, sizeof(envpath), "hw.irq.%d.dest", intr);
784 kgetenv_int(envpath, &cpuid);
786 /* ncpus may not be available yet */
790 /* Deliver interrupts to CPU0 (BSP) */
791 target = (CPU_TO_ID(cpuid) << IOART_HI_DEST_SHIFT) &
793 select = IOAPIC_REDTBL0 + (2 * intr);
794 vector = IDT_OFFSET + intr;
795 flags = DEFAULT_EXTINT_FLAGS;
797 ioapic_write(ioapic[apic], select, flags | vector);
798 ioapic_write(ioapic[apic], select + 1, target);
802 #undef DEFAULT_EXTINT_FLAGS
806 * Set the trigger level for an IO APIC pin.
809 trigger(int apic, int pin, u_int32_t * flags)
814 static int intcontrol = -1;
816 switch (apic_trigger(apic, pin)) {
822 *flags &= ~IOART_TRGRLVL; /* *flags |= IOART_TRGREDG */
826 *flags |= IOART_TRGRLVL;
834 if ((id = apic_src_bus_id(apic, pin)) == -1)
837 switch (apic_bus_type(id)) {
839 *flags &= ~IOART_TRGRLVL; /* *flags |= IOART_TRGREDG; */
843 eirq = apic_src_bus_irq(apic, pin);
845 if (eirq < 0 || eirq > 15) {
846 kprintf("EISA IRQ %d?!?!\n", eirq);
850 if (intcontrol == -1) {
851 intcontrol = inb(ELCR1) << 8;
852 intcontrol |= inb(ELCR0);
853 kprintf("EISA INTCONTROL = %08x\n", intcontrol);
856 /* Use ELCR settings to determine level or edge mode */
857 level = (intcontrol >> eirq) & 1;
860 * Note that on older Neptune chipset based systems, any
861 * pci interrupts often show up here and in the ELCR as well
862 * as level sensitive interrupts attributed to the EISA bus.
866 *flags |= IOART_TRGRLVL;
868 *flags &= ~IOART_TRGRLVL;
873 *flags |= IOART_TRGRLVL;
882 panic("bad APIC IO INT flags");
887 * Set the polarity value for an IO APIC pin.
890 polarity(int apic, int pin, u_int32_t * flags, int level)
894 switch (apic_polarity(apic, pin)) {
900 *flags &= ~IOART_INTALO; /* *flags |= IOART_INTAHI */
904 *flags |= IOART_INTALO;
912 if ((id = apic_src_bus_id(apic, pin)) == -1)
915 switch (apic_bus_type(id)) {
917 *flags &= ~IOART_INTALO; /* *flags |= IOART_INTAHI */
921 /* polarity converter always gives active high */
922 *flags &= ~IOART_INTALO;
926 *flags |= IOART_INTALO;
935 panic("bad APIC IO INT flags");
940 * Print contents of unmasked IRQs.
947 kprintf("SMP: enabled INTs: ");
948 for (x = 0; x < APIC_INTMAPSIZE; ++x) {
949 if ((int_to_apicintpin[x].flags & IOAPIC_IM_FLAG_MASKED) == 0)
957 * Inter Processor Interrupt functions.
960 #endif /* SMP APIC-IO */
963 * Send APIC IPI 'vector' to 'destType' via 'deliveryMode'.
965 * destType is 1 of: APIC_DEST_SELF, APIC_DEST_ALLISELF, APIC_DEST_ALLESELF
966 * vector is any valid SYSTEM INT vector
967 * delivery_mode is 1 of: APIC_DELMODE_FIXED, APIC_DELMODE_LOWPRIO
969 * A backlog of requests can create a deadlock between cpus. To avoid this
970 * we have to be able to accept IPIs at the same time we are trying to send
971 * them. The critical section prevents us from attempting to send additional
972 * IPIs reentrantly, but also prevents IPIQ processing so we have to call
973 * lwkt_process_ipiq() manually. It's rather messy and expensive for this
974 * to occur but fortunately it does not happen too often.
977 apic_ipi(int dest_type, int vector, int delivery_mode)
982 if ((lapic->icr_lo & APIC_DELSTAT_MASK) != 0) {
983 unsigned long rflags = read_rflags();
985 DEBUG_PUSH_INFO("apic_ipi");
986 while ((lapic->icr_lo & APIC_DELSTAT_MASK) != 0) {
990 write_rflags(rflags);
993 icr_lo = (lapic->icr_lo & APIC_ICRLO_RESV_MASK) | dest_type |
994 delivery_mode | vector;
995 lapic->icr_lo = icr_lo;
1001 single_apic_ipi(int cpu, int vector, int delivery_mode)
1007 if ((lapic->icr_lo & APIC_DELSTAT_MASK) != 0) {
1008 unsigned long rflags = read_rflags();
1010 DEBUG_PUSH_INFO("single_apic_ipi");
1011 while ((lapic->icr_lo & APIC_DELSTAT_MASK) != 0) {
1012 lwkt_process_ipiq();
1015 write_rflags(rflags);
1017 icr_hi = lapic->icr_hi & ~APIC_ID_MASK;
1018 icr_hi |= (CPU_TO_ID(cpu) << 24);
1019 lapic->icr_hi = icr_hi;
1022 icr_lo = (lapic->icr_lo & APIC_ICRLO_RESV_MASK)
1023 | APIC_DEST_DESTFLD | delivery_mode | vector;
1025 /* write APIC ICR */
1026 lapic->icr_lo = icr_lo;
1033 * Returns 0 if the apic is busy, 1 if we were able to queue the request.
1035 * NOT WORKING YET! The code as-is may end up not queueing an IPI at all
1036 * to the target, and the scheduler does not 'poll' for IPI messages.
1039 single_apic_ipi_passive(int cpu, int vector, int delivery_mode)
1045 if ((lapic->icr_lo & APIC_DELSTAT_MASK) != 0) {
1049 icr_hi = lapic->icr_hi & ~APIC_ID_MASK;
1050 icr_hi |= (CPU_TO_ID(cpu) << 24);
1051 lapic->icr_hi = icr_hi;
1054 icr_lo = (lapic->icr_lo & APIC_RESV2_MASK)
1055 | APIC_DEST_DESTFLD | delivery_mode | vector;
1057 /* write APIC ICR */
1058 lapic->icr_lo = icr_lo;
1066 * Send APIC IPI 'vector' to 'target's via 'delivery_mode'.
1068 * target is a bitmask of destination cpus. Vector is any
1069 * valid system INT vector. Delivery mode may be either
1070 * APIC_DELMODE_FIXED or APIC_DELMODE_LOWPRIO.
1073 selected_apic_ipi(cpumask_t target, int vector, int delivery_mode)
1077 int n = BSFCPUMASK(target);
1078 target &= ~CPUMASK(n);
1079 single_apic_ipi(n, vector, delivery_mode);
1085 * Timer code, in development...
1086 * - suggested by rgrimes@gndrsh.aac.dev.com
1089 get_apic_timer_frequency(void)
1091 return(lapic_cputimer_intr.freq);
1095 * Load a 'downcount time' in uSeconds.
1098 set_apic_timer(int us)
1103 * When we reach here, lapic timer's frequency
1104 * must have been calculated as well as the
1105 * divisor (lapic->dcr_timer is setup during the
1106 * divisor calculation).
1108 KKASSERT(lapic_cputimer_intr.freq != 0 &&
1109 lapic_timer_divisor_idx >= 0);
1111 count = ((us * (int64_t)lapic_cputimer_intr.freq) + 999999) / 1000000;
1112 lapic_timer_oneshot(count);
1117 * Read remaining time in timer.
1120 read_apic_timer(void)
1123 /** XXX FIXME: we need to return the actual remaining time,
1124 * for now we just return the remaining count.
1127 return lapic->ccr_timer;
1133 * Spin-style delay, set delay time in uS, spin till it drains.
1138 set_apic_timer(count);
1139 while (read_apic_timer())
1144 lapic_unused_apic_id(int start)
1148 for (i = start; i < NAPICID; ++i) {
1149 if (ID_TO_CPU(i) == -1)
1156 lapic_map(vm_offset_t lapic_addr)
1158 lapic = pmap_mapdev_uncacheable(lapic_addr, sizeof(struct LAPIC));
1160 kprintf("lapic: at 0x%08lx\n", lapic_addr);
1163 static TAILQ_HEAD(, lapic_enumerator) lapic_enumerators =
1164 TAILQ_HEAD_INITIALIZER(lapic_enumerators);
1169 struct lapic_enumerator *e;
1172 for (i = 0; i < NAPICID; ++i)
1175 TAILQ_FOREACH(e, &lapic_enumerators, lapic_link) {
1176 error = e->lapic_probe(e);
1181 panic("can't config lapic\n");
1183 e->lapic_enumerate(e);
1187 lapic_enumerator_register(struct lapic_enumerator *ne)
1189 struct lapic_enumerator *e;
1191 TAILQ_FOREACH(e, &lapic_enumerators, lapic_link) {
1192 if (e->lapic_prio < ne->lapic_prio) {
1193 TAILQ_INSERT_BEFORE(e, ne, lapic_link);
1197 TAILQ_INSERT_TAIL(&lapic_enumerators, ne, lapic_link);
1200 static TAILQ_HEAD(, ioapic_enumerator) ioapic_enumerators =
1201 TAILQ_HEAD_INITIALIZER(ioapic_enumerators);
1206 struct ioapic_enumerator *e;
1210 TAILQ_INIT(&ioapic_conf.ioc_list);
1211 /* XXX magic number */
1212 for (i = 0; i < 16; ++i)
1213 ioapic_conf.ioc_intsrc[i] = -1;
1215 TAILQ_FOREACH(e, &ioapic_enumerators, ioapic_link) {
1216 error = e->ioapic_probe(e);
1222 panic("can't config I/O APIC\n");
1224 kprintf("no I/O APIC\n");
1229 if (!ioapic_use_old) {
1236 * Switch to I/O APIC MachIntrABI and reconfigure
1237 * the default IDT entries.
1239 MachIntrABI = MachIntrABI_IOAPIC;
1240 MachIntrABI.setdefault();
1243 e->ioapic_enumerate(e);
1245 if (!ioapic_use_old) {
1246 struct ioapic_info *info;
1247 int start_apic_id = 0;
1253 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link)
1256 if (i > IOAPIC_COUNT_MAX) /* XXX magic number */
1257 panic("ioapic_config: more than 16 I/O APIC\n");
1262 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link) {
1265 apic_id = ioapic_alloc_apic_id(start_apic_id);
1266 if (apic_id == NAPICID) {
1267 kprintf("IOAPIC: can't alloc APIC ID for "
1268 "%dth I/O APIC\n", info->io_idx);
1271 info->io_apic_id = apic_id;
1273 start_apic_id = apic_id + 1;
1277 * xAPIC allows I/O APIC's APIC ID to be same
1278 * as the LAPIC's APIC ID
1280 kprintf("IOAPIC: use xAPIC model to alloc APIC ID "
1283 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link)
1284 info->io_apic_id = info->io_idx;
1288 * Warning about any GSI holes
1290 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link) {
1291 const struct ioapic_info *prev_info;
1293 prev_info = TAILQ_PREV(info, ioapic_info_list, io_link);
1294 if (prev_info != NULL) {
1295 if (info->io_gsi_base !=
1296 prev_info->io_gsi_base + prev_info->io_npin) {
1297 kprintf("IOAPIC: warning gsi hole "
1299 prev_info->io_gsi_base +
1301 info->io_gsi_base - 1);
1307 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link) {
1308 kprintf("IOAPIC: idx %d, apic id %d, "
1309 "gsi base %d, npin %d\n",
1318 * Setup all I/O APIC
1320 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link)
1322 ioapic_abi_fixup_irqmap();
1326 MachIntrABI.cleanup();
1333 ioapic_enumerator_register(struct ioapic_enumerator *ne)
1335 struct ioapic_enumerator *e;
1337 TAILQ_FOREACH(e, &ioapic_enumerators, ioapic_link) {
1338 if (e->ioapic_prio < ne->ioapic_prio) {
1339 TAILQ_INSERT_BEFORE(e, ne, ioapic_link);
1343 TAILQ_INSERT_TAIL(&ioapic_enumerators, ne, ioapic_link);
1347 ioapic_add(void *addr, int gsi_base, int npin)
1349 struct ioapic_info *info, *ninfo;
1352 gsi_end = gsi_base + npin - 1;
1353 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link) {
1354 if ((gsi_base >= info->io_gsi_base &&
1355 gsi_base < info->io_gsi_base + info->io_npin) ||
1356 (gsi_end >= info->io_gsi_base &&
1357 gsi_end < info->io_gsi_base + info->io_npin)) {
1358 panic("ioapic_add: overlapped gsi, base %d npin %d, "
1359 "hit base %d, npin %d\n", gsi_base, npin,
1360 info->io_gsi_base, info->io_npin);
1362 if (info->io_addr == addr)
1363 panic("ioapic_add: duplicated addr %p\n", addr);
1366 ninfo = kmalloc(sizeof(*ninfo), M_DEVBUF, M_WAITOK | M_ZERO);
1367 ninfo->io_addr = addr;
1368 ninfo->io_npin = npin;
1369 ninfo->io_gsi_base = gsi_base;
1370 ninfo->io_apic_id = -1;
1373 * Create IOAPIC list in ascending order of GSI base
1375 TAILQ_FOREACH_REVERSE(info, &ioapic_conf.ioc_list,
1376 ioapic_info_list, io_link) {
1377 if (ninfo->io_gsi_base > info->io_gsi_base) {
1378 TAILQ_INSERT_AFTER(&ioapic_conf.ioc_list,
1379 info, ninfo, io_link);
1384 TAILQ_INSERT_HEAD(&ioapic_conf.ioc_list, ninfo, io_link);
1388 ioapic_intsrc(int irq, int gsi)
1390 KKASSERT(irq != gsi);
1394 /* Don't allow mixed mode */
1395 kprintf("IOAPIC: warning intsrc irq %d -> gsi 0\n", irq);
1399 if (ioapic_conf.ioc_intsrc[irq] != -1 &&
1400 ioapic_conf.ioc_intsrc[irq] != gsi) {
1401 kprintf("IOAPIC: warning intsrc irq %d, gsi %d -> gsi %d\n",
1402 irq, ioapic_conf.ioc_intsrc[irq], gsi);
1404 ioapic_conf.ioc_intsrc[irq] = gsi;
1408 ioapic_set_apic_id(const struct ioapic_info *info)
1413 id = ioapic_read(info->io_addr, IOAPIC_ID);
1415 id &= ~APIC_ID_MASK;
1416 id |= (info->io_apic_id << 24);
1418 ioapic_write(info->io_addr, IOAPIC_ID, id);
1423 id = ioapic_read(info->io_addr, IOAPIC_ID);
1424 apic_id = (id & APIC_ID_MASK) >> 24;
1427 * I/O APIC ID is a 4bits field
1429 if ((apic_id & IOAPIC_ID_MASK) !=
1430 (info->io_apic_id & IOAPIC_ID_MASK)) {
1431 panic("ioapic_set_apic_id: can't set apic id to %d, "
1432 "currently set to %d\n", info->io_apic_id, apic_id);
1437 ioapic_gsi_setup(int gsi)
1439 enum intr_trigger trig;
1440 enum intr_polarity pola;
1446 ioapic_extpin_setup(ioapic_gsi_ioaddr(gsi),
1447 ioapic_gsi_pin(gsi), 0);
1452 for (irq = 0; irq < 16; ++irq) {
1453 if (gsi == ioapic_conf.ioc_intsrc[irq]) {
1454 trig = INTR_TRIGGER_EDGE;
1455 pola = INTR_POLARITY_HIGH;
1462 trig = INTR_TRIGGER_EDGE;
1463 pola = INTR_POLARITY_HIGH;
1465 trig = INTR_TRIGGER_LEVEL;
1466 pola = INTR_POLARITY_LOW;
1471 ioapic_abi_set_irqmap(irq, gsi, trig, pola);
1475 ioapic_gsi_ioaddr(int gsi)
1477 const struct ioapic_info *info;
1479 info = ioapic_gsi_search(gsi);
1480 return info->io_addr;
1484 ioapic_gsi_pin(int gsi)
1486 const struct ioapic_info *info;
1488 info = ioapic_gsi_search(gsi);
1489 return gsi - info->io_gsi_base;
1492 static const struct ioapic_info *
1493 ioapic_gsi_search(int gsi)
1495 const struct ioapic_info *info;
1497 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link) {
1498 if (gsi >= info->io_gsi_base &&
1499 gsi < info->io_gsi_base + info->io_npin)
1502 panic("ioapic_gsi_search: no I/O APIC\n");
1506 ioapic_gsi(int idx, int pin)
1508 const struct ioapic_info *info;
1510 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link) {
1511 if (info->io_idx == idx)
1516 if (pin >= info->io_npin)
1518 return info->io_gsi_base + pin;
1522 ioapic_extpin_setup(void *addr, int pin, int vec)
1524 ioapic_pin_prog(addr, pin, vec,
1525 INTR_TRIGGER_CONFORM, INTR_POLARITY_CONFORM, IOART_DELEXINT);
1529 ioapic_extpin_gsi(void)
1535 ioapic_pin_setup(void *addr, int pin, int vec,
1536 enum intr_trigger trig, enum intr_polarity pola)
1539 * Always clear an I/O APIC pin before [re]programming it. This is
1540 * particularly important if the pin is set up for a level interrupt
1541 * as the IOART_REM_IRR bit might be set. When we reprogram the
1542 * vector any EOI from pending ints on this pin could be lost and
1543 * IRR might never get reset.
1545 * To fix this problem, clear the vector and make sure it is
1546 * programmed as an edge interrupt. This should theoretically
1547 * clear IRR so we can later, safely program it as a level
1550 ioapic_pin_prog(addr, pin, vec, INTR_TRIGGER_EDGE, INTR_POLARITY_HIGH,
1552 ioapic_pin_prog(addr, pin, vec, trig, pola, IOART_DELFIXED);
1556 ioapic_pin_prog(void *addr, int pin, int vec,
1557 enum intr_trigger trig, enum intr_polarity pola, uint32_t del_mode)
1559 uint32_t flags, target;
1562 KKASSERT(del_mode == IOART_DELEXINT || del_mode == IOART_DELFIXED);
1564 select = IOAPIC_REDTBL0 + (2 * pin);
1566 flags = ioapic_read(addr, select) & IOART_RESV;
1567 flags |= IOART_INTMSET | IOART_DESTPHY;
1572 * We only support limited I/O APIC mixed mode,
1573 * so even for ExtINT, we still use "fixed"
1576 flags |= IOART_DELFIXED;
1579 if (del_mode == IOART_DELEXINT) {
1580 KKASSERT(trig == INTR_TRIGGER_CONFORM &&
1581 pola == INTR_POLARITY_CONFORM);
1582 flags |= IOART_TRGREDG | IOART_INTAHI;
1585 case INTR_TRIGGER_EDGE:
1586 flags |= IOART_TRGREDG;
1589 case INTR_TRIGGER_LEVEL:
1590 flags |= IOART_TRGRLVL;
1593 case INTR_TRIGGER_CONFORM:
1594 panic("ioapic_pin_prog: trig conform is not "
1598 case INTR_POLARITY_HIGH:
1599 flags |= IOART_INTAHI;
1602 case INTR_POLARITY_LOW:
1603 flags |= IOART_INTALO;
1606 case INTR_POLARITY_CONFORM:
1607 panic("ioapic_pin_prog: pola conform is not "
1612 target = ioapic_read(addr, select + 1) & IOART_HI_DEST_RESV;
1613 target |= (CPU_TO_ID(0) << IOART_HI_DEST_SHIFT) &
1616 ioapic_write(addr, select, flags | vec);
1617 ioapic_write(addr, select + 1, target);
1621 ioapic_setup(const struct ioapic_info *info)
1625 ioapic_set_apic_id(info);
1627 for (i = 0; i < info->io_npin; ++i)
1628 ioapic_gsi_setup(info->io_gsi_base + i);
1632 ioapic_alloc_apic_id(int start)
1635 const struct ioapic_info *info;
1636 int apic_id, apic_id16;
1638 apic_id = lapic_unused_apic_id(start);
1639 if (apic_id == NAPICID) {
1640 kprintf("IOAPIC: can't find unused APIC ID\n");
1643 apic_id16 = apic_id & IOAPIC_ID_MASK;
1646 * Check against other I/O APIC's APIC ID's lower 4bits.
1648 * The new APIC ID will have to be different from others
1649 * in the lower 4bits, no matter whether xAPIC is used
1652 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link) {
1653 if (info->io_apic_id == -1) {
1657 if ((info->io_apic_id & IOAPIC_ID_MASK) == apic_id16)
1663 kprintf("IOAPIC: APIC ID %d has same lower 4bits as "
1664 "%dth I/O APIC, keep searching...\n",
1665 apic_id, info->io_idx);
1667 start = apic_id + 1;
1669 panic("ioapic_unused_apic_id: never reached\n");