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 <machine/globaldata.h>
33 #include <machine/smp.h>
34 #include <machine/md_var.h>
35 #include <machine/pmap.h>
36 #include <machine_base/apic/mpapic.h>
37 #include <machine_base/apic/ioapic_abi.h>
38 #include <machine/segments.h>
39 #include <sys/thread2.h>
41 #include <machine/intr_machdep.h>
45 /* EISA Edge/Level trigger control registers */
46 #define ELCR0 0x4d0 /* eisa irq 0-7 */
47 #define ELCR1 0x4d1 /* eisa irq 8-15 */
56 TAILQ_ENTRY(ioapic_info) io_link;
58 TAILQ_HEAD(ioapic_info_list, ioapic_info);
61 struct ioapic_info_list ioc_list;
62 int ioc_intsrc[16]; /* XXX magic number */
65 volatile lapic_t *lapic;
67 static void lapic_timer_calibrate(void);
68 static void lapic_timer_set_divisor(int);
69 static void lapic_timer_fixup_handler(void *);
70 static void lapic_timer_restart_handler(void *);
72 void lapic_timer_process(void);
73 void lapic_timer_process_frame(struct intrframe *);
74 void lapic_timer_always(struct intrframe *);
76 static int lapic_timer_enable = 1;
77 TUNABLE_INT("hw.lapic_timer_enable", &lapic_timer_enable);
79 static void lapic_timer_intr_reload(struct cputimer_intr *, sysclock_t);
80 static void lapic_timer_intr_enable(struct cputimer_intr *);
81 static void lapic_timer_intr_restart(struct cputimer_intr *);
82 static void lapic_timer_intr_pmfixup(struct cputimer_intr *);
84 static void ioapic_setup(const struct ioapic_info *);
85 static void ioapic_set_apic_id(const struct ioapic_info *);
86 static void ioapic_gsi_setup(int);
87 static const struct ioapic_info *
88 ioapic_gsi_search(int);
89 static void ioapic_pin_prog(void *, int, int,
90 enum intr_trigger, enum intr_polarity, uint32_t);
92 static struct cputimer_intr lapic_cputimer_intr = {
94 .reload = lapic_timer_intr_reload,
95 .enable = lapic_timer_intr_enable,
96 .config = cputimer_intr_default_config,
97 .restart = lapic_timer_intr_restart,
98 .pmfixup = lapic_timer_intr_pmfixup,
99 .initclock = cputimer_intr_default_initclock,
100 .next = SLIST_ENTRY_INITIALIZER,
102 .type = CPUTIMER_INTR_LAPIC,
103 .prio = CPUTIMER_INTR_PRIO_LAPIC,
104 .caps = CPUTIMER_INTR_CAP_NONE
108 * pointers to pmapped apic hardware.
111 volatile ioapic_t **ioapic;
113 static int lapic_timer_divisor_idx = -1;
114 static const uint32_t lapic_timer_divisors[] = {
115 APIC_TDCR_2, APIC_TDCR_4, APIC_TDCR_8, APIC_TDCR_16,
116 APIC_TDCR_32, APIC_TDCR_64, APIC_TDCR_128, APIC_TDCR_1
118 #define APIC_TIMER_NDIVISORS (int)(NELEM(lapic_timer_divisors))
121 static struct ioapic_conf ioapic_conf;
131 * Enable LAPIC, configure interrupts.
134 lapic_init(boolean_t bsp)
142 * Since IDT is shared between BSP and APs, these vectors
143 * only need to be installed once; we do it on BSP.
146 /* Install a 'Spurious INTerrupt' vector */
147 setidt(XSPURIOUSINT_OFFSET, Xspuriousint,
148 SDT_SYSIGT, SEL_KPL, 0);
150 /* Install an inter-CPU IPI for TLB invalidation */
151 setidt(XINVLTLB_OFFSET, Xinvltlb,
152 SDT_SYSIGT, SEL_KPL, 0);
154 /* Install an inter-CPU IPI for IPIQ messaging */
155 setidt(XIPIQ_OFFSET, Xipiq,
156 SDT_SYSIGT, SEL_KPL, 0);
158 /* Install a timer vector */
159 setidt(XTIMER_OFFSET, Xtimer,
160 SDT_SYSIGT, SEL_KPL, 0);
162 /* Install an inter-CPU IPI for CPU stop/restart */
163 setidt(XCPUSTOP_OFFSET, Xcpustop,
164 SDT_SYSIGT, SEL_KPL, 0);
168 * Setup LINT0 as ExtINT on the BSP. This is theoretically an
169 * aggregate interrupt input from the 8259. The INTA cycle
170 * will be routed to the external controller (the 8259) which
171 * is expected to supply the vector.
173 * Must be setup edge triggered, active high.
175 * Disable LINT0 on the APs. It doesn't matter what delivery
176 * mode we use because we leave it masked.
178 temp = lapic->lvt_lint0;
179 temp &= ~(APIC_LVT_MASKED | APIC_LVT_TRIG_MASK |
180 APIC_LVT_POLARITY_MASK | APIC_LVT_DM_MASK);
181 if (mycpu->gd_cpuid == 0)
182 temp |= APIC_LVT_DM_EXTINT;
184 temp |= APIC_LVT_DM_FIXED | APIC_LVT_MASKED;
185 lapic->lvt_lint0 = temp;
188 * Setup LINT1 as NMI, masked till later.
189 * Edge trigger, active high.
191 temp = lapic->lvt_lint1;
192 temp &= ~(APIC_LVT_MASKED | APIC_LVT_TRIG_MASK |
193 APIC_LVT_POLARITY_MASK | APIC_LVT_DM_MASK);
194 temp |= APIC_LVT_MASKED | APIC_LVT_DM_NMI;
195 lapic->lvt_lint1 = temp;
198 * Mask the LAPIC error interrupt, LAPIC performance counter
201 lapic->lvt_error = lapic->lvt_error | APIC_LVT_MASKED;
202 lapic->lvt_pcint = lapic->lvt_pcint | APIC_LVT_MASKED;
205 * Set LAPIC timer vector and mask the LAPIC timer interrupt.
207 timer = lapic->lvt_timer;
208 timer &= ~APIC_LVTT_VECTOR;
209 timer |= XTIMER_OFFSET;
210 timer |= APIC_LVTT_MASKED;
211 lapic->lvt_timer = timer;
214 * Set the Task Priority Register as needed. At the moment allow
215 * interrupts on all cpus (the APs will remain CLId until they are
216 * ready to deal). We could disable all but IPIs by setting
217 * temp |= TPR_IPI for cpu != 0.
220 temp &= ~APIC_TPR_PRIO; /* clear priority field */
221 #ifdef SMP /* APIC-IO */
222 if (!apic_io_enable) {
225 * If we are NOT running the IO APICs, the LAPIC will only be used
226 * for IPIs. Set the TPR to prevent any unintentional interrupts.
229 #ifdef SMP /* APIC-IO */
238 temp |= APIC_SVR_ENABLE; /* enable the LAPIC */
239 temp &= ~APIC_SVR_FOCUS_DISABLE; /* enable lopri focus processor */
242 * Set the spurious interrupt vector. The low 4 bits of the vector
245 if ((XSPURIOUSINT_OFFSET & 0x0F) != 0x0F)
246 panic("bad XSPURIOUSINT_OFFSET: 0x%08x", XSPURIOUSINT_OFFSET);
247 temp &= ~APIC_SVR_VECTOR;
248 temp |= XSPURIOUSINT_OFFSET;
253 * Pump out a few EOIs to clean out interrupts that got through
254 * before we were able to set the TPR.
261 lapic_timer_calibrate();
262 if (lapic_timer_enable) {
263 cputimer_intr_register(&lapic_cputimer_intr);
264 cputimer_intr_select(&lapic_cputimer_intr, 0);
267 lapic_timer_set_divisor(lapic_timer_divisor_idx);
271 apic_dump("apic_initialize()");
275 lapic_timer_set_divisor(int divisor_idx)
277 KKASSERT(divisor_idx >= 0 && divisor_idx < APIC_TIMER_NDIVISORS);
278 lapic->dcr_timer = lapic_timer_divisors[divisor_idx];
282 lapic_timer_oneshot(u_int count)
286 value = lapic->lvt_timer;
287 value &= ~APIC_LVTT_PERIODIC;
288 lapic->lvt_timer = value;
289 lapic->icr_timer = count;
293 lapic_timer_oneshot_quick(u_int count)
295 lapic->icr_timer = count;
299 lapic_timer_calibrate(void)
303 /* Try to calibrate the local APIC timer. */
304 for (lapic_timer_divisor_idx = 0;
305 lapic_timer_divisor_idx < APIC_TIMER_NDIVISORS;
306 lapic_timer_divisor_idx++) {
307 lapic_timer_set_divisor(lapic_timer_divisor_idx);
308 lapic_timer_oneshot(APIC_TIMER_MAX_COUNT);
310 value = APIC_TIMER_MAX_COUNT - lapic->ccr_timer;
311 if (value != APIC_TIMER_MAX_COUNT)
314 if (lapic_timer_divisor_idx >= APIC_TIMER_NDIVISORS)
315 panic("lapic: no proper timer divisor?!\n");
316 lapic_cputimer_intr.freq = value / 2;
318 kprintf("lapic: divisor index %d, frequency %u Hz\n",
319 lapic_timer_divisor_idx, lapic_cputimer_intr.freq);
323 lapic_timer_process_oncpu(struct globaldata *gd, struct intrframe *frame)
327 gd->gd_timer_running = 0;
329 count = sys_cputimer->count();
330 if (TAILQ_FIRST(&gd->gd_systimerq) != NULL)
331 systimer_intr(&count, 0, frame);
335 lapic_timer_process(void)
337 lapic_timer_process_oncpu(mycpu, NULL);
341 lapic_timer_process_frame(struct intrframe *frame)
343 lapic_timer_process_oncpu(mycpu, frame);
347 * This manual debugging code is called unconditionally from Xtimer
348 * (the lapic timer interrupt) whether the current thread is in a
349 * critical section or not) and can be useful in tracking down lockups.
351 * NOTE: MANUAL DEBUG CODE
354 static int saveticks[SMP_MAXCPU];
355 static int savecounts[SMP_MAXCPU];
359 lapic_timer_always(struct intrframe *frame)
362 globaldata_t gd = mycpu;
363 int cpu = gd->gd_cpuid;
369 gptr = (short *)0xFFFFFFFF800b8000 + 80 * cpu;
370 *gptr = ((*gptr + 1) & 0x00FF) | 0x0700;
373 ksnprintf(buf, sizeof(buf), " %p %16s %d %16s ",
374 (void *)frame->if_rip, gd->gd_curthread->td_comm, ticks,
376 for (i = 0; buf[i]; ++i) {
377 gptr[i] = 0x0700 | (unsigned char)buf[i];
381 if (saveticks[gd->gd_cpuid] != ticks) {
382 saveticks[gd->gd_cpuid] = ticks;
383 savecounts[gd->gd_cpuid] = 0;
385 ++savecounts[gd->gd_cpuid];
386 if (savecounts[gd->gd_cpuid] > 2000 && panicstr == NULL) {
387 panic("cpud %d panicing on ticks failure",
390 for (i = 0; i < ncpus; ++i) {
392 if (saveticks[i] && panicstr == NULL) {
393 delta = saveticks[i] - ticks;
394 if (delta < -10 || delta > 10) {
395 panic("cpu %d panicing on cpu %d watchdog",
405 lapic_timer_intr_reload(struct cputimer_intr *cti, sysclock_t reload)
407 struct globaldata *gd = mycpu;
409 reload = (int64_t)reload * cti->freq / sys_cputimer->freq;
413 if (gd->gd_timer_running) {
414 if (reload < lapic->ccr_timer)
415 lapic_timer_oneshot_quick(reload);
417 gd->gd_timer_running = 1;
418 lapic_timer_oneshot_quick(reload);
423 lapic_timer_intr_enable(struct cputimer_intr *cti __unused)
427 timer = lapic->lvt_timer;
428 timer &= ~(APIC_LVTT_MASKED | APIC_LVTT_PERIODIC);
429 lapic->lvt_timer = timer;
431 lapic_timer_fixup_handler(NULL);
435 lapic_timer_fixup_handler(void *arg)
442 if (strcmp(cpu_vendor, "AuthenticAMD") == 0) {
444 * Detect the presence of C1E capability mostly on latest
445 * dual-cores (or future) k8 family. This feature renders
446 * the local APIC timer dead, so we disable it by reading
447 * the Interrupt Pending Message register and clearing both
448 * C1eOnCmpHalt (bit 28) and SmiOnCmpHalt (bit 27).
451 * "BIOS and Kernel Developer's Guide for AMD NPT
452 * Family 0Fh Processors"
453 * #32559 revision 3.00
455 if ((cpu_id & 0x00000f00) == 0x00000f00 &&
456 (cpu_id & 0x0fff0000) >= 0x00040000) {
459 msr = rdmsr(0xc0010055);
460 if (msr & 0x18000000) {
461 struct globaldata *gd = mycpu;
463 kprintf("cpu%d: AMD C1E detected\n",
465 wrmsr(0xc0010055, msr & ~0x18000000ULL);
468 * We are kinda stalled;
471 gd->gd_timer_running = 1;
472 lapic_timer_oneshot_quick(2);
482 lapic_timer_restart_handler(void *dummy __unused)
486 lapic_timer_fixup_handler(&started);
488 struct globaldata *gd = mycpu;
490 gd->gd_timer_running = 1;
491 lapic_timer_oneshot_quick(2);
496 * This function is called only by ACPI-CA code currently:
497 * - AMD C1E fixup. AMD C1E only seems to happen after ACPI
498 * module controls PM. So once ACPI-CA is attached, we try
499 * to apply the fixup to prevent LAPIC timer from hanging.
502 lapic_timer_intr_pmfixup(struct cputimer_intr *cti __unused)
504 lwkt_send_ipiq_mask(smp_active_mask,
505 lapic_timer_fixup_handler, NULL);
509 lapic_timer_intr_restart(struct cputimer_intr *cti __unused)
511 lwkt_send_ipiq_mask(smp_active_mask, lapic_timer_restart_handler, NULL);
516 * dump contents of local APIC registers
521 kprintf("SMP: CPU%d %s:\n", mycpu->gd_cpuid, str);
522 kprintf(" lint0: 0x%08x lint1: 0x%08x TPR: 0x%08x SVR: 0x%08x\n",
523 lapic->lvt_lint0, lapic->lvt_lint1, lapic->tpr, lapic->svr);
527 #ifdef SMP /* APIC-IO */
533 #define IOAPIC_ISA_INTS 16
534 #define REDIRCNT_IOAPIC(A) \
535 ((int)((io_apic_versions[(A)] & IOART_VER_MAXREDIR) >> MAXREDIRSHIFT) + 1)
537 static int trigger (int apic, int pin, u_int32_t * flags);
538 static void polarity (int apic, int pin, u_int32_t * flags, int level);
540 #define DEFAULT_FLAGS \
546 #define DEFAULT_ISA_FLAGS \
555 io_apic_set_id(int apic, int id)
559 ux = ioapic_read(ioapic[apic], IOAPIC_ID); /* get current contents */
560 if (((ux & APIC_ID_MASK) >> 24) != id) {
561 kprintf("Changing APIC ID for IO APIC #%d"
562 " from %d to %d on chip\n",
563 apic, ((ux & APIC_ID_MASK) >> 24), id);
564 ux &= ~APIC_ID_MASK; /* clear the ID field */
566 ioapic_write(ioapic[apic], IOAPIC_ID, ux); /* write new value */
567 ux = ioapic_read(ioapic[apic], IOAPIC_ID); /* re-read && test */
568 if (((ux & APIC_ID_MASK) >> 24) != id)
569 panic("can't control IO APIC #%d ID, reg: 0x%08x",
576 io_apic_get_id(int apic)
578 return (ioapic_read(ioapic[apic], IOAPIC_ID) & APIC_ID_MASK) >> 24;
587 io_apic_setup_intpin(int apic, int pin)
589 int bus, bustype, irq;
590 u_char select; /* the select register is 8 bits */
591 u_int32_t flags; /* the window register is 32 bits */
592 u_int32_t target; /* the window register is 32 bits */
593 u_int32_t vector; /* the window register is 32 bits */
598 select = pin * 2 + IOAPIC_REDTBL0; /* register */
601 * Always clear an IO APIC pin before [re]programming it. This is
602 * particularly important if the pin is set up for a level interrupt
603 * as the IOART_REM_IRR bit might be set. When we reprogram the
604 * vector any EOI from pending ints on this pin could be lost and
605 * IRR might never get reset.
607 * To fix this problem, clear the vector and make sure it is
608 * programmed as an edge interrupt. This should theoretically
609 * clear IRR so we can later, safely program it as a level
614 flags = ioapic_read(ioapic[apic], select) & IOART_RESV;
615 flags |= IOART_INTMSET | IOART_TRGREDG | IOART_INTAHI;
616 flags |= IOART_DESTPHY | IOART_DELFIXED;
618 target = ioapic_read(ioapic[apic], select + 1) & IOART_HI_DEST_RESV;
619 target |= 0; /* fixed mode cpu mask of 0 - don't deliver anywhere */
623 ioapic_write(ioapic[apic], select, flags | vector);
624 ioapic_write(ioapic[apic], select + 1, target);
629 * We only deal with vectored interrupts here. ? documentation is
630 * lacking, I'm guessing an interrupt type of 0 is the 'INT' type,
633 * This test also catches unconfigured pins.
635 if (apic_int_type(apic, pin) != 0)
639 * Leave the pin unprogrammed if it does not correspond to
642 irq = apic_irq(apic, pin);
646 /* determine the bus type for this pin */
647 bus = apic_src_bus_id(apic, pin);
650 bustype = apic_bus_type(bus);
652 if ((bustype == ISA) &&
653 (pin < IOAPIC_ISA_INTS) &&
655 (apic_polarity(apic, pin) == 0x1) &&
656 (apic_trigger(apic, pin) == 0x3)) {
658 * A broken BIOS might describe some ISA
659 * interrupts as active-high level-triggered.
660 * Use default ISA flags for those interrupts.
662 flags = DEFAULT_ISA_FLAGS;
665 * Program polarity and trigger mode according to
668 flags = DEFAULT_FLAGS;
669 level = trigger(apic, pin, &flags);
671 int_to_apicintpin[irq].flags |= IOAPIC_IM_FLAG_LEVEL;
672 polarity(apic, pin, &flags, level);
676 ksnprintf(envpath, sizeof(envpath), "hw.irq.%d.dest", irq);
677 kgetenv_int(envpath, &cpuid);
679 /* ncpus may not be available yet */
684 kprintf("IOAPIC #%d intpin %d -> irq %d (CPU%d)\n",
685 apic, pin, irq, cpuid);
689 * Program the appropriate registers. This routing may be
690 * overridden when an interrupt handler for a device is
691 * actually added (see register_int(), which calls through
692 * the MACHINTR ABI to set up an interrupt handler/vector).
694 * The order in which we must program the two registers for
695 * safety is unclear! XXX
699 vector = IDT_OFFSET + irq; /* IDT vec */
700 target = ioapic_read(ioapic[apic], select + 1) & IOART_HI_DEST_RESV;
701 /* Deliver all interrupts to CPU0 (BSP) */
702 target |= (CPU_TO_ID(cpuid) << IOART_HI_DEST_SHIFT) &
704 flags |= ioapic_read(ioapic[apic], select) & IOART_RESV;
705 ioapic_write(ioapic[apic], select, flags | vector);
706 ioapic_write(ioapic[apic], select + 1, target);
712 io_apic_setup(int apic)
717 maxpin = REDIRCNT_IOAPIC(apic); /* pins in APIC */
718 kprintf("Programming %d pins in IOAPIC #%d\n", maxpin, apic);
720 for (pin = 0; pin < maxpin; ++pin) {
721 io_apic_setup_intpin(apic, pin);
724 if (apic_int_type(apic, pin) >= 0) {
725 kprintf("Warning: IOAPIC #%d pin %d does not exist,"
726 " cannot program!\n", apic, pin);
731 /* return GOOD status */
734 #undef DEFAULT_ISA_FLAGS
738 #define DEFAULT_EXTINT_FLAGS \
747 * XXX this function is only used by 8254 setup
748 * Setup the source of External INTerrupts.
751 ext_int_setup(int apic, int intr)
753 u_char select; /* the select register is 8 bits */
754 u_int32_t flags; /* the window register is 32 bits */
755 u_int32_t target; /* the window register is 32 bits */
756 u_int32_t vector; /* the window register is 32 bits */
760 if (apic_int_type(apic, intr) != 3)
764 ksnprintf(envpath, sizeof(envpath), "hw.irq.%d.dest", intr);
765 kgetenv_int(envpath, &cpuid);
767 /* ncpus may not be available yet */
771 /* Deliver interrupts to CPU0 (BSP) */
772 target = (CPU_TO_ID(cpuid) << IOART_HI_DEST_SHIFT) &
774 select = IOAPIC_REDTBL0 + (2 * intr);
775 vector = IDT_OFFSET + intr;
776 flags = DEFAULT_EXTINT_FLAGS;
778 ioapic_write(ioapic[apic], select, flags | vector);
779 ioapic_write(ioapic[apic], select + 1, target);
783 #undef DEFAULT_EXTINT_FLAGS
787 * Set the trigger level for an IO APIC pin.
790 trigger(int apic, int pin, u_int32_t * flags)
795 static int intcontrol = -1;
797 switch (apic_trigger(apic, pin)) {
803 *flags &= ~IOART_TRGRLVL; /* *flags |= IOART_TRGREDG */
807 *flags |= IOART_TRGRLVL;
815 if ((id = apic_src_bus_id(apic, pin)) == -1)
818 switch (apic_bus_type(id)) {
820 *flags &= ~IOART_TRGRLVL; /* *flags |= IOART_TRGREDG; */
824 eirq = apic_src_bus_irq(apic, pin);
826 if (eirq < 0 || eirq > 15) {
827 kprintf("EISA IRQ %d?!?!\n", eirq);
831 if (intcontrol == -1) {
832 intcontrol = inb(ELCR1) << 8;
833 intcontrol |= inb(ELCR0);
834 kprintf("EISA INTCONTROL = %08x\n", intcontrol);
837 /* Use ELCR settings to determine level or edge mode */
838 level = (intcontrol >> eirq) & 1;
841 * Note that on older Neptune chipset based systems, any
842 * pci interrupts often show up here and in the ELCR as well
843 * as level sensitive interrupts attributed to the EISA bus.
847 *flags |= IOART_TRGRLVL;
849 *flags &= ~IOART_TRGRLVL;
854 *flags |= IOART_TRGRLVL;
863 panic("bad APIC IO INT flags");
868 * Set the polarity value for an IO APIC pin.
871 polarity(int apic, int pin, u_int32_t * flags, int level)
875 switch (apic_polarity(apic, pin)) {
881 *flags &= ~IOART_INTALO; /* *flags |= IOART_INTAHI */
885 *flags |= IOART_INTALO;
893 if ((id = apic_src_bus_id(apic, pin)) == -1)
896 switch (apic_bus_type(id)) {
898 *flags &= ~IOART_INTALO; /* *flags |= IOART_INTAHI */
902 /* polarity converter always gives active high */
903 *flags &= ~IOART_INTALO;
907 *flags |= IOART_INTALO;
916 panic("bad APIC IO INT flags");
921 * Print contents of unmasked IRQs.
928 kprintf("SMP: enabled INTs: ");
929 for (x = 0; x < APIC_INTMAPSIZE; ++x) {
930 if ((int_to_apicintpin[x].flags & IOAPIC_IM_FLAG_MASKED) == 0)
938 * Inter Processor Interrupt functions.
941 #endif /* SMP APIC-IO */
944 * Send APIC IPI 'vector' to 'destType' via 'deliveryMode'.
946 * destType is 1 of: APIC_DEST_SELF, APIC_DEST_ALLISELF, APIC_DEST_ALLESELF
947 * vector is any valid SYSTEM INT vector
948 * delivery_mode is 1 of: APIC_DELMODE_FIXED, APIC_DELMODE_LOWPRIO
950 * A backlog of requests can create a deadlock between cpus. To avoid this
951 * we have to be able to accept IPIs at the same time we are trying to send
952 * them. The critical section prevents us from attempting to send additional
953 * IPIs reentrantly, but also prevents IPIQ processing so we have to call
954 * lwkt_process_ipiq() manually. It's rather messy and expensive for this
955 * to occur but fortunately it does not happen too often.
958 apic_ipi(int dest_type, int vector, int delivery_mode)
963 if ((lapic->icr_lo & APIC_DELSTAT_MASK) != 0) {
964 unsigned long rflags = read_rflags();
966 DEBUG_PUSH_INFO("apic_ipi");
967 while ((lapic->icr_lo & APIC_DELSTAT_MASK) != 0) {
971 write_rflags(rflags);
974 icr_lo = (lapic->icr_lo & APIC_ICRLO_RESV_MASK) | dest_type |
975 delivery_mode | vector;
976 lapic->icr_lo = icr_lo;
982 single_apic_ipi(int cpu, int vector, int delivery_mode)
988 if ((lapic->icr_lo & APIC_DELSTAT_MASK) != 0) {
989 unsigned long rflags = read_rflags();
991 DEBUG_PUSH_INFO("single_apic_ipi");
992 while ((lapic->icr_lo & APIC_DELSTAT_MASK) != 0) {
996 write_rflags(rflags);
998 icr_hi = lapic->icr_hi & ~APIC_ID_MASK;
999 icr_hi |= (CPU_TO_ID(cpu) << 24);
1000 lapic->icr_hi = icr_hi;
1003 icr_lo = (lapic->icr_lo & APIC_ICRLO_RESV_MASK)
1004 | APIC_DEST_DESTFLD | delivery_mode | vector;
1006 /* write APIC ICR */
1007 lapic->icr_lo = icr_lo;
1014 * Returns 0 if the apic is busy, 1 if we were able to queue the request.
1016 * NOT WORKING YET! The code as-is may end up not queueing an IPI at all
1017 * to the target, and the scheduler does not 'poll' for IPI messages.
1020 single_apic_ipi_passive(int cpu, int vector, int delivery_mode)
1026 if ((lapic->icr_lo & APIC_DELSTAT_MASK) != 0) {
1030 icr_hi = lapic->icr_hi & ~APIC_ID_MASK;
1031 icr_hi |= (CPU_TO_ID(cpu) << 24);
1032 lapic->icr_hi = icr_hi;
1035 icr_lo = (lapic->icr_lo & APIC_RESV2_MASK)
1036 | APIC_DEST_DESTFLD | delivery_mode | vector;
1038 /* write APIC ICR */
1039 lapic->icr_lo = icr_lo;
1047 * Send APIC IPI 'vector' to 'target's via 'delivery_mode'.
1049 * target is a bitmask of destination cpus. Vector is any
1050 * valid system INT vector. Delivery mode may be either
1051 * APIC_DELMODE_FIXED or APIC_DELMODE_LOWPRIO.
1054 selected_apic_ipi(cpumask_t target, int vector, int delivery_mode)
1058 int n = BSFCPUMASK(target);
1059 target &= ~CPUMASK(n);
1060 single_apic_ipi(n, vector, delivery_mode);
1066 * Timer code, in development...
1067 * - suggested by rgrimes@gndrsh.aac.dev.com
1070 get_apic_timer_frequency(void)
1072 return(lapic_cputimer_intr.freq);
1076 * Load a 'downcount time' in uSeconds.
1079 set_apic_timer(int us)
1084 * When we reach here, lapic timer's frequency
1085 * must have been calculated as well as the
1086 * divisor (lapic->dcr_timer is setup during the
1087 * divisor calculation).
1089 KKASSERT(lapic_cputimer_intr.freq != 0 &&
1090 lapic_timer_divisor_idx >= 0);
1092 count = ((us * (int64_t)lapic_cputimer_intr.freq) + 999999) / 1000000;
1093 lapic_timer_oneshot(count);
1098 * Read remaining time in timer.
1101 read_apic_timer(void)
1104 /** XXX FIXME: we need to return the actual remaining time,
1105 * for now we just return the remaining count.
1108 return lapic->ccr_timer;
1114 * Spin-style delay, set delay time in uS, spin till it drains.
1119 set_apic_timer(count);
1120 while (read_apic_timer())
1125 lapic_map(vm_offset_t lapic_addr)
1127 lapic = pmap_mapdev_uncacheable(lapic_addr, sizeof(struct LAPIC));
1129 kprintf("lapic: at 0x%08lx\n", lapic_addr);
1132 static TAILQ_HEAD(, lapic_enumerator) lapic_enumerators =
1133 TAILQ_HEAD_INITIALIZER(lapic_enumerators);
1138 struct lapic_enumerator *e;
1141 TAILQ_FOREACH(e, &lapic_enumerators, lapic_link) {
1142 error = e->lapic_probe(e);
1147 panic("can't config lapic\n");
1149 e->lapic_enumerate(e);
1153 lapic_enumerator_register(struct lapic_enumerator *ne)
1155 struct lapic_enumerator *e;
1157 TAILQ_FOREACH(e, &lapic_enumerators, lapic_link) {
1158 if (e->lapic_prio < ne->lapic_prio) {
1159 TAILQ_INSERT_BEFORE(e, ne, lapic_link);
1163 TAILQ_INSERT_TAIL(&lapic_enumerators, ne, lapic_link);
1166 static TAILQ_HEAD(, ioapic_enumerator) ioapic_enumerators =
1167 TAILQ_HEAD_INITIALIZER(ioapic_enumerators);
1172 struct ioapic_enumerator *e;
1175 TAILQ_INIT(&ioapic_conf.ioc_list);
1176 /* XXX magic number */
1177 for (i = 0; i < 16; ++i)
1178 ioapic_conf.ioc_intsrc[i] = -1;
1180 TAILQ_FOREACH(e, &ioapic_enumerators, ioapic_link) {
1181 error = e->ioapic_probe(e);
1187 panic("can't config I/O APIC\n");
1189 kprintf("no I/O APIC\n");
1194 e->ioapic_enumerate(e);
1196 if (!ioapic_use_old) {
1197 struct ioapic_info *info;
1200 * Fixup the rest of the fields of ioapic_info
1203 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link) {
1204 const struct ioapic_info *prev_info;
1207 info->io_apic_id = info->io_idx + lapic_id_max + 1;
1210 kprintf("IOAPIC: idx %d, apic id %d, "
1211 "gsi base %d, npin %d\n",
1218 /* Warning about possible GSI hole */
1219 prev_info = TAILQ_PREV(info, ioapic_info_list, io_link);
1220 if (prev_info != NULL) {
1221 if (info->io_gsi_base !=
1222 prev_info->io_gsi_base + prev_info->io_npin) {
1223 kprintf("IOAPIC: warning gsi hole "
1225 prev_info->io_gsi_base +
1227 info->io_gsi_base - 1);
1233 * Setup all I/O APIC
1235 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link)
1238 panic("ioapic_config: new ioapic not working yet\n");
1243 ioapic_enumerator_register(struct ioapic_enumerator *ne)
1245 struct ioapic_enumerator *e;
1247 TAILQ_FOREACH(e, &ioapic_enumerators, ioapic_link) {
1248 if (e->ioapic_prio < ne->ioapic_prio) {
1249 TAILQ_INSERT_BEFORE(e, ne, ioapic_link);
1253 TAILQ_INSERT_TAIL(&ioapic_enumerators, ne, ioapic_link);
1257 ioapic_add(void *addr, int gsi_base, int npin)
1259 struct ioapic_info *info, *ninfo;
1262 gsi_end = gsi_base + npin - 1;
1263 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link) {
1264 if ((gsi_base >= info->io_gsi_base &&
1265 gsi_base < info->io_gsi_base + info->io_npin) ||
1266 (gsi_end >= info->io_gsi_base &&
1267 gsi_end < info->io_gsi_base + info->io_npin)) {
1268 panic("ioapic_add: overlapped gsi, base %d npin %d, "
1269 "hit base %d, npin %d\n", gsi_base, npin,
1270 info->io_gsi_base, info->io_npin);
1272 if (info->io_addr == addr)
1273 panic("ioapic_add: duplicated addr %p\n", addr);
1276 ninfo = kmalloc(sizeof(*ninfo), M_DEVBUF, M_WAITOK | M_ZERO);
1277 ninfo->io_addr = addr;
1278 ninfo->io_npin = npin;
1279 ninfo->io_gsi_base = gsi_base;
1282 * Create IOAPIC list in ascending order of GSI base
1284 TAILQ_FOREACH_REVERSE(info, &ioapic_conf.ioc_list,
1285 ioapic_info_list, io_link) {
1286 if (ninfo->io_gsi_base > info->io_gsi_base) {
1287 TAILQ_INSERT_AFTER(&ioapic_conf.ioc_list,
1288 info, ninfo, io_link);
1293 TAILQ_INSERT_HEAD(&ioapic_conf.ioc_list, ninfo, io_link);
1297 ioapic_intsrc(int irq, int gsi)
1300 if (ioapic_conf.ioc_intsrc[irq] != -1 &&
1301 ioapic_conf.ioc_intsrc[irq] != gsi) {
1302 kprintf("IOAPIC: warning intsrc irq %d, gsi %d -> gsi %d\n",
1303 irq, ioapic_conf.ioc_intsrc[irq], gsi);
1305 ioapic_conf.ioc_intsrc[irq] = gsi;
1309 ioapic_set_apic_id(const struct ioapic_info *info)
1313 id = ioapic_read(info->io_addr, IOAPIC_ID);
1315 id &= ~APIC_ID_MASK;
1316 id |= (info->io_apic_id << 24);
1318 ioapic_write(info->io_addr, IOAPIC_ID, id);
1323 id = ioapic_read(info->io_addr, IOAPIC_ID);
1324 if (((id & APIC_ID_MASK) >> 24) != info->io_apic_id) {
1325 panic("ioapic_set_apic_id: can't set apic id to %d\n",
1331 ioapic_gsi_setup(int gsi)
1333 enum intr_trigger trig;
1334 enum intr_polarity pola;
1339 ioapic_extpin_setup(ioapic_gsi_ioaddr(gsi),
1340 ioapic_gsi_pin(gsi), 0);
1344 for (irq = 0; irq < 16; ++irq) {
1345 if (gsi == ioapic_conf.ioc_intsrc[irq]) {
1346 trig = INTR_TRIGGER_EDGE;
1347 pola = INTR_POLARITY_HIGH;
1354 trig = INTR_TRIGGER_EDGE;
1355 pola = INTR_POLARITY_HIGH;
1357 trig = INTR_TRIGGER_LEVEL;
1358 pola = INTR_POLARITY_LOW;
1363 ioapic_abi_set_irqmap(irq, gsi, trig, pola);
1367 ioapic_gsi_ioaddr(int gsi)
1369 const struct ioapic_info *info;
1371 info = ioapic_gsi_search(gsi);
1372 return info->io_addr;
1376 ioapic_gsi_pin(int gsi)
1378 const struct ioapic_info *info;
1380 info = ioapic_gsi_search(gsi);
1381 return gsi - info->io_gsi_base;
1384 static const struct ioapic_info *
1385 ioapic_gsi_search(int gsi)
1387 const struct ioapic_info *info;
1389 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link) {
1390 if (gsi >= info->io_gsi_base &&
1391 gsi < info->io_gsi_base + info->io_npin)
1394 panic("ioapic_gsi_search: no I/O APIC\n");
1398 ioapic_extpin_setup(void *addr, int pin, int vec)
1401 ioapic_pin_prog(addr, pin, vec,
1402 INTR_TRIGGER_CONFORM, INTR_POLARITY_CONFORM, IOART_DELEXINT);
1407 ioapic_pin_setup(void *addr, int pin, int vec,
1408 enum intr_trigger trig, enum intr_polarity pola)
1411 * Always clear an I/O APIC pin before [re]programming it. This is
1412 * particularly important if the pin is set up for a level interrupt
1413 * as the IOART_REM_IRR bit might be set. When we reprogram the
1414 * vector any EOI from pending ints on this pin could be lost and
1415 * IRR might never get reset.
1417 * To fix this problem, clear the vector and make sure it is
1418 * programmed as an edge interrupt. This should theoretically
1419 * clear IRR so we can later, safely program it as a level
1424 ioapic_pin_prog(addr, pin, vec, INTR_TRIGGER_EDGE, INTR_POLARITY_HIGH,
1426 ioapic_pin_prog(addr, pin, vec, trig, pola, IOART_DELFIXED);
1432 ioapic_pin_prog(void *addr, int pin, int vec,
1433 enum intr_trigger trig, enum intr_polarity pola, uint32_t del_mode)
1435 uint32_t flags, target;
1438 KKASSERT(del_mode == IOART_DELEXINT || del_mode == IOART_DELFIXED);
1440 select = IOAPIC_REDTBL0 + (2 * pin);
1442 flags = ioapic_read(addr, select) & IOART_RESV;
1443 flags |= IOART_INTMSET | IOART_DESTPHY | del_mode;
1445 if (del_mode == IOART_DELEXINT) {
1446 KKASSERT(trig == INTR_TRIGGER_CONFORM &&
1447 pola == INTR_POLARITY_CONFORM);
1448 flags |= IOART_TRGREDG | IOART_INTAHI;
1451 case INTR_TRIGGER_EDGE:
1452 flags |= IOART_TRGREDG;
1455 case INTR_TRIGGER_LEVEL:
1456 flags |= IOART_TRGRLVL;
1459 case INTR_TRIGGER_CONFORM:
1460 panic("ioapic_pin_prog: trig conform is not "
1464 case INTR_POLARITY_HIGH:
1465 flags |= IOART_INTAHI;
1468 case INTR_POLARITY_LOW:
1469 flags |= IOART_INTALO;
1472 case INTR_POLARITY_CONFORM:
1473 panic("ioapic_pin_prog: pola conform is not "
1478 target = ioapic_read(addr, select + 1) & IOART_HI_DEST_RESV;
1481 ioapic_write(addr, select, flags | vec);
1482 ioapic_write(addr, select + 1, target);
1486 ioapic_setup(const struct ioapic_info *info)
1490 ioapic_set_apic_id(info);
1492 for (i = 0; i < info->io_npin; ++i)
1493 ioapic_gsi_setup(info->io_gsi_base + i);