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/cputypes.h>
36 #include <machine/md_var.h>
37 #include <machine/pmap.h>
38 #include <machine_base/apic/mpapic.h>
39 #include <machine_base/apic/ioapic_abi.h>
40 #include <machine/segments.h>
41 #include <sys/thread2.h>
43 #include <machine/intr_machdep.h>
45 #define IOAPIC_COUNT_MAX 16
46 #define IOAPIC_ID_MASK (IOAPIC_COUNT_MAX - 1)
49 extern pt_entry_t *SMPpt;
51 /* EISA Edge/Level trigger control registers */
52 #define ELCR0 0x4d0 /* eisa irq 0-7 */
53 #define ELCR1 0x4d1 /* eisa irq 8-15 */
62 TAILQ_ENTRY(ioapic_info) io_link;
64 TAILQ_HEAD(ioapic_info_list, ioapic_info);
66 struct ioapic_intsrc {
68 enum intr_trigger int_trig;
69 enum intr_polarity int_pola;
73 struct ioapic_info_list ioc_list;
74 struct ioapic_intsrc ioc_intsrc[16]; /* XXX magic number */
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 *);
85 static int lapic_timer_enable = 1;
86 TUNABLE_INT("hw.lapic_timer_enable", &lapic_timer_enable);
88 static void lapic_timer_intr_reload(struct cputimer_intr *, sysclock_t);
89 static void lapic_timer_intr_enable(struct cputimer_intr *);
90 static void lapic_timer_intr_restart(struct cputimer_intr *);
91 static void lapic_timer_intr_pmfixup(struct cputimer_intr *);
93 static int lapic_unused_apic_id(int);
95 static void ioapic_setup(const struct ioapic_info *);
96 static int ioapic_alloc_apic_id(int);
97 static void ioapic_set_apic_id(const struct ioapic_info *);
98 static void ioapic_gsi_setup(int);
99 static const struct ioapic_info *
100 ioapic_gsi_search(int);
101 static void ioapic_pin_prog(void *, int, int,
102 enum intr_trigger, enum intr_polarity, uint32_t);
104 static struct cputimer_intr lapic_cputimer_intr = {
106 .reload = lapic_timer_intr_reload,
107 .enable = lapic_timer_intr_enable,
108 .config = cputimer_intr_default_config,
109 .restart = lapic_timer_intr_restart,
110 .pmfixup = lapic_timer_intr_pmfixup,
111 .initclock = cputimer_intr_default_initclock,
112 .next = SLIST_ENTRY_INITIALIZER,
114 .type = CPUTIMER_INTR_LAPIC,
115 .prio = CPUTIMER_INTR_PRIO_LAPIC,
116 .caps = CPUTIMER_INTR_CAP_NONE
119 static int lapic_timer_divisor_idx = -1;
120 static const uint32_t lapic_timer_divisors[] = {
121 APIC_TDCR_2, APIC_TDCR_4, APIC_TDCR_8, APIC_TDCR_16,
122 APIC_TDCR_32, APIC_TDCR_64, APIC_TDCR_128, APIC_TDCR_1
124 #define APIC_TIMER_NDIVISORS (int)(NELEM(lapic_timer_divisors))
128 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_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
150 /* Install an inter-CPU IPI for TLB invalidation */
151 setidt(XINVLTLB_OFFSET, Xinvltlb,
152 SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
154 /* Install an inter-CPU IPI for IPIQ messaging */
155 setidt(XIPIQ_OFFSET, Xipiq,
156 SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
158 /* Install a timer vector */
159 setidt(XTIMER_OFFSET, Xtimer,
160 SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
162 /* Install an inter-CPU IPI for CPU stop/restart */
163 setidt(XCPUSTOP_OFFSET, Xcpustop,
164 SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
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 BSP, if I/O APIC is enabled.
177 * Disable LINT0 on the APs. It doesn't matter what delivery
178 * mode we use because we leave it masked.
180 temp = lapic.lvt_lint0;
181 temp &= ~(APIC_LVT_MASKED | APIC_LVT_TRIG_MASK |
182 APIC_LVT_POLARITY_MASK | APIC_LVT_DM_MASK);
184 temp |= APIC_LVT_DM_EXTINT;
186 temp |= APIC_LVT_MASKED;
188 temp |= APIC_LVT_DM_FIXED | APIC_LVT_MASKED;
190 lapic.lvt_lint0 = temp;
193 * Setup LINT1 as NMI.
195 * Must be setup edge trigger, active high.
197 * Enable LINT1 on BSP, if I/O APIC is enabled.
199 * Disable LINT1 on the APs.
201 temp = lapic.lvt_lint1;
202 temp &= ~(APIC_LVT_MASKED | APIC_LVT_TRIG_MASK |
203 APIC_LVT_POLARITY_MASK | APIC_LVT_DM_MASK);
204 temp |= APIC_LVT_MASKED | APIC_LVT_DM_NMI;
205 if (bsp && apic_io_enable)
206 temp &= ~APIC_LVT_MASKED;
207 lapic.lvt_lint1 = temp;
210 * Mask the LAPIC error interrupt, LAPIC performance counter
213 lapic.lvt_error = lapic.lvt_error | APIC_LVT_MASKED;
214 lapic.lvt_pcint = lapic.lvt_pcint | APIC_LVT_MASKED;
217 * Set LAPIC timer vector and mask the LAPIC timer interrupt.
219 timer = lapic.lvt_timer;
220 timer &= ~APIC_LVTT_VECTOR;
221 timer |= XTIMER_OFFSET;
222 timer |= APIC_LVTT_MASKED;
223 lapic.lvt_timer = timer;
226 * Set the Task Priority Register as needed. At the moment allow
227 * interrupts on all cpus (the APs will remain CLId until they are
228 * ready to deal). We could disable all but IPIs by setting
229 * temp |= TPR_IPI for cpu != 0.
232 temp &= ~APIC_TPR_PRIO; /* clear priority field */
233 #ifdef SMP /* APIC-IO */
234 if (!apic_io_enable) {
237 * If we are NOT running the IO APICs, the LAPIC will only be used
238 * for IPIs. Set the TPR to prevent any unintentional interrupts.
241 #ifdef SMP /* APIC-IO */
251 temp |= APIC_SVR_ENABLE; /* enable the LAPIC */
252 temp &= ~APIC_SVR_FOCUS_DISABLE; /* enable lopri focus processor */
255 * Set the spurious interrupt vector. The low 4 bits of the vector
258 if ((XSPURIOUSINT_OFFSET & 0x0F) != 0x0F)
259 panic("bad XSPURIOUSINT_OFFSET: 0x%08x", XSPURIOUSINT_OFFSET);
260 temp &= ~APIC_SVR_VECTOR;
261 temp |= XSPURIOUSINT_OFFSET;
266 * Pump out a few EOIs to clean out interrupts that got through
267 * before we were able to set the TPR.
274 lapic_timer_calibrate();
275 if (lapic_timer_enable) {
276 cputimer_intr_register(&lapic_cputimer_intr);
277 cputimer_intr_select(&lapic_cputimer_intr, 0);
280 lapic_timer_set_divisor(lapic_timer_divisor_idx);
284 apic_dump("apic_initialize()");
288 lapic_timer_set_divisor(int divisor_idx)
290 KKASSERT(divisor_idx >= 0 && divisor_idx < APIC_TIMER_NDIVISORS);
291 lapic.dcr_timer = lapic_timer_divisors[divisor_idx];
295 lapic_timer_oneshot(u_int count)
299 value = lapic.lvt_timer;
300 value &= ~APIC_LVTT_PERIODIC;
301 lapic.lvt_timer = value;
302 lapic.icr_timer = count;
306 lapic_timer_oneshot_quick(u_int count)
308 lapic.icr_timer = count;
312 lapic_timer_calibrate(void)
316 /* Try to calibrate the local APIC timer. */
317 for (lapic_timer_divisor_idx = 0;
318 lapic_timer_divisor_idx < APIC_TIMER_NDIVISORS;
319 lapic_timer_divisor_idx++) {
320 lapic_timer_set_divisor(lapic_timer_divisor_idx);
321 lapic_timer_oneshot(APIC_TIMER_MAX_COUNT);
323 value = APIC_TIMER_MAX_COUNT - lapic.ccr_timer;
324 if (value != APIC_TIMER_MAX_COUNT)
327 if (lapic_timer_divisor_idx >= APIC_TIMER_NDIVISORS)
328 panic("lapic: no proper timer divisor?!\n");
329 lapic_cputimer_intr.freq = value / 2;
331 kprintf("lapic: divisor index %d, frequency %u Hz\n",
332 lapic_timer_divisor_idx, lapic_cputimer_intr.freq);
336 lapic_timer_process_oncpu(struct globaldata *gd, struct intrframe *frame)
340 gd->gd_timer_running = 0;
342 count = sys_cputimer->count();
343 if (TAILQ_FIRST(&gd->gd_systimerq) != NULL)
344 systimer_intr(&count, 0, frame);
348 lapic_timer_process(void)
350 lapic_timer_process_oncpu(mycpu, NULL);
354 lapic_timer_process_frame(struct intrframe *frame)
356 lapic_timer_process_oncpu(mycpu, frame);
360 lapic_timer_intr_reload(struct cputimer_intr *cti, sysclock_t reload)
362 struct globaldata *gd = mycpu;
364 reload = (int64_t)reload * cti->freq / sys_cputimer->freq;
368 if (gd->gd_timer_running) {
369 if (reload < lapic.ccr_timer)
370 lapic_timer_oneshot_quick(reload);
372 gd->gd_timer_running = 1;
373 lapic_timer_oneshot_quick(reload);
378 lapic_timer_intr_enable(struct cputimer_intr *cti __unused)
382 timer = lapic.lvt_timer;
383 timer &= ~(APIC_LVTT_MASKED | APIC_LVTT_PERIODIC);
384 lapic.lvt_timer = timer;
386 lapic_timer_fixup_handler(NULL);
390 lapic_timer_fixup_handler(void *arg)
397 if (cpu_vendor_id == CPU_VENDOR_AMD) {
399 * Detect the presence of C1E capability mostly on latest
400 * dual-cores (or future) k8 family. This feature renders
401 * the local APIC timer dead, so we disable it by reading
402 * the Interrupt Pending Message register and clearing both
403 * C1eOnCmpHalt (bit 28) and SmiOnCmpHalt (bit 27).
406 * "BIOS and Kernel Developer's Guide for AMD NPT
407 * Family 0Fh Processors"
408 * #32559 revision 3.00
410 if ((cpu_id & 0x00000f00) == 0x00000f00 &&
411 (cpu_id & 0x0fff0000) >= 0x00040000) {
414 msr = rdmsr(0xc0010055);
415 if (msr & 0x18000000) {
416 struct globaldata *gd = mycpu;
418 kprintf("cpu%d: AMD C1E detected\n",
420 wrmsr(0xc0010055, msr & ~0x18000000ULL);
423 * We are kinda stalled;
426 gd->gd_timer_running = 1;
427 lapic_timer_oneshot_quick(2);
437 lapic_timer_restart_handler(void *dummy __unused)
441 lapic_timer_fixup_handler(&started);
443 struct globaldata *gd = mycpu;
445 gd->gd_timer_running = 1;
446 lapic_timer_oneshot_quick(2);
451 * This function is called only by ACPI-CA code currently:
452 * - AMD C1E fixup. AMD C1E only seems to happen after ACPI
453 * module controls PM. So once ACPI-CA is attached, we try
454 * to apply the fixup to prevent LAPIC timer from hanging.
457 lapic_timer_intr_pmfixup(struct cputimer_intr *cti __unused)
459 lwkt_send_ipiq_mask(smp_active_mask,
460 lapic_timer_fixup_handler, NULL);
464 lapic_timer_intr_restart(struct cputimer_intr *cti __unused)
466 lwkt_send_ipiq_mask(smp_active_mask, lapic_timer_restart_handler, NULL);
471 * dump contents of local APIC registers
476 kprintf("SMP: CPU%d %s:\n", mycpu->gd_cpuid, str);
477 kprintf(" lint0: 0x%08x lint1: 0x%08x TPR: 0x%08x SVR: 0x%08x\n",
478 lapic.lvt_lint0, lapic.lvt_lint1, lapic.tpr, lapic.svr);
482 * Inter Processor Interrupt functions.
486 * Send APIC IPI 'vector' to 'destType' via 'deliveryMode'.
488 * destType is 1 of: APIC_DEST_SELF, APIC_DEST_ALLISELF, APIC_DEST_ALLESELF
489 * vector is any valid SYSTEM INT vector
490 * delivery_mode is 1 of: APIC_DELMODE_FIXED, APIC_DELMODE_LOWPRIO
492 * A backlog of requests can create a deadlock between cpus. To avoid this
493 * we have to be able to accept IPIs at the same time we are trying to send
494 * them. The critical section prevents us from attempting to send additional
495 * IPIs reentrantly, but also prevents IPIQ processing so we have to call
496 * lwkt_process_ipiq() manually. It's rather messy and expensive for this
497 * to occur but fortunately it does not happen too often.
500 apic_ipi(int dest_type, int vector, int delivery_mode)
505 if ((lapic.icr_lo & APIC_DELSTAT_MASK) != 0) {
506 unsigned int eflags = read_eflags();
508 DEBUG_PUSH_INFO("apic_ipi");
509 while ((lapic.icr_lo & APIC_DELSTAT_MASK) != 0) {
513 write_eflags(eflags);
516 icr_lo = (lapic.icr_lo & APIC_ICRLO_RESV_MASK) | dest_type |
517 delivery_mode | vector;
518 lapic.icr_lo = icr_lo;
524 single_apic_ipi(int cpu, int vector, int delivery_mode)
530 if ((lapic.icr_lo & APIC_DELSTAT_MASK) != 0) {
531 unsigned int eflags = read_eflags();
533 DEBUG_PUSH_INFO("single_apic_ipi");
534 while ((lapic.icr_lo & APIC_DELSTAT_MASK) != 0) {
538 write_eflags(eflags);
540 icr_hi = lapic.icr_hi & ~APIC_ID_MASK;
541 icr_hi |= (CPU_TO_ID(cpu) << 24);
542 lapic.icr_hi = icr_hi;
545 icr_lo = (lapic.icr_lo & APIC_ICRLO_RESV_MASK)
546 | APIC_DEST_DESTFLD | delivery_mode | vector;
549 lapic.icr_lo = icr_lo;
556 * Returns 0 if the apic is busy, 1 if we were able to queue the request.
558 * NOT WORKING YET! The code as-is may end up not queueing an IPI at all
559 * to the target, and the scheduler does not 'poll' for IPI messages.
562 single_apic_ipi_passive(int cpu, int vector, int delivery_mode)
568 if ((lapic.icr_lo & APIC_DELSTAT_MASK) != 0) {
572 icr_hi = lapic.icr_hi & ~APIC_ID_MASK;
573 icr_hi |= (CPU_TO_ID(cpu) << 24);
574 lapic.icr_hi = icr_hi;
577 icr_lo = (lapic.icr_lo & APIC_RESV2_MASK)
578 | APIC_DEST_DESTFLD | delivery_mode | vector;
581 lapic.icr_lo = icr_lo;
589 * Send APIC IPI 'vector' to 'target's via 'delivery_mode'.
591 * target is a bitmask of destination cpus. Vector is any
592 * valid system INT vector. Delivery mode may be either
593 * APIC_DELMODE_FIXED or APIC_DELMODE_LOWPRIO.
596 selected_apic_ipi(cpumask_t target, int vector, int delivery_mode)
600 int n = BSFCPUMASK(target);
601 target &= ~CPUMASK(n);
602 single_apic_ipi(n, vector, delivery_mode);
608 * Timer code, in development...
609 * - suggested by rgrimes@gndrsh.aac.dev.com
612 get_apic_timer_frequency(void)
614 return(lapic_cputimer_intr.freq);
618 * Load a 'downcount time' in uSeconds.
621 set_apic_timer(int us)
626 * When we reach here, lapic timer's frequency
627 * must have been calculated as well as the
628 * divisor (lapic.dcr_timer is setup during the
629 * divisor calculation).
631 KKASSERT(lapic_cputimer_intr.freq != 0 &&
632 lapic_timer_divisor_idx >= 0);
634 count = ((us * (int64_t)lapic_cputimer_intr.freq) + 999999) / 1000000;
635 lapic_timer_oneshot(count);
640 * Read remaining time in timer.
643 read_apic_timer(void)
646 /** XXX FIXME: we need to return the actual remaining time,
647 * for now we just return the remaining count.
650 return lapic.ccr_timer;
656 * Spin-style delay, set delay time in uS, spin till it drains.
661 set_apic_timer(count);
662 while (read_apic_timer())
667 lapic_unused_apic_id(int start)
671 for (i = start; i < NAPICID; ++i) {
672 if (ID_TO_CPU(i) == -1)
679 lapic_map(vm_offset_t lapic_addr)
681 /* Local apic is mapped on last page */
682 SMPpt[NPTEPG - 1] = (pt_entry_t)(PG_V | PG_RW | PG_N |
683 pmap_get_pgeflag() | (lapic_addr & PG_FRAME));
685 kprintf("lapic: at %p\n", (void *)lapic_addr);
688 static TAILQ_HEAD(, lapic_enumerator) lapic_enumerators =
689 TAILQ_HEAD_INITIALIZER(lapic_enumerators);
694 struct lapic_enumerator *e;
697 for (i = 0; i < NAPICID; ++i)
700 TAILQ_FOREACH(e, &lapic_enumerators, lapic_link) {
701 error = e->lapic_probe(e);
706 panic("can't config lapic\n");
708 e->lapic_enumerate(e);
712 lapic_enumerator_register(struct lapic_enumerator *ne)
714 struct lapic_enumerator *e;
716 TAILQ_FOREACH(e, &lapic_enumerators, lapic_link) {
717 if (e->lapic_prio < ne->lapic_prio) {
718 TAILQ_INSERT_BEFORE(e, ne, lapic_link);
722 TAILQ_INSERT_TAIL(&lapic_enumerators, ne, lapic_link);
725 static TAILQ_HEAD(, ioapic_enumerator) ioapic_enumerators =
726 TAILQ_HEAD_INITIALIZER(ioapic_enumerators);
731 struct ioapic_info *info;
732 int start_apic_id = 0;
733 struct ioapic_enumerator *e;
737 TAILQ_INIT(&ioapic_conf.ioc_list);
738 /* XXX magic number */
739 for (i = 0; i < 16; ++i)
740 ioapic_conf.ioc_intsrc[i].int_gsi = -1;
742 TAILQ_FOREACH(e, &ioapic_enumerators, ioapic_link) {
743 error = e->ioapic_probe(e);
749 panic("can't config I/O APIC\n");
751 kprintf("no I/O APIC\n");
762 * Switch to I/O APIC MachIntrABI and reconfigure
763 * the default IDT entries.
765 MachIntrABI = MachIntrABI_IOAPIC;
766 MachIntrABI.setdefault();
768 e->ioapic_enumerate(e);
774 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link)
777 if (i > IOAPIC_COUNT_MAX) /* XXX magic number */
778 panic("ioapic_config: more than 16 I/O APIC\n");
783 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link) {
786 apic_id = ioapic_alloc_apic_id(start_apic_id);
787 if (apic_id == NAPICID) {
788 kprintf("IOAPIC: can't alloc APIC ID for "
789 "%dth I/O APIC\n", info->io_idx);
792 info->io_apic_id = apic_id;
794 start_apic_id = apic_id + 1;
798 * xAPIC allows I/O APIC's APIC ID to be same
799 * as the LAPIC's APIC ID
801 kprintf("IOAPIC: use xAPIC model to alloc APIC ID "
804 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link)
805 info->io_apic_id = info->io_idx;
809 * Warning about any GSI holes
811 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link) {
812 const struct ioapic_info *prev_info;
814 prev_info = TAILQ_PREV(info, ioapic_info_list, io_link);
815 if (prev_info != NULL) {
816 if (info->io_gsi_base !=
817 prev_info->io_gsi_base + prev_info->io_npin) {
818 kprintf("IOAPIC: warning gsi hole "
820 prev_info->io_gsi_base +
822 info->io_gsi_base - 1);
828 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link) {
829 kprintf("IOAPIC: idx %d, apic id %d, "
830 "gsi base %d, npin %d\n",
841 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link)
843 ioapic_abi_fixup_irqmap();
847 MachIntrABI.cleanup();
853 ioapic_enumerator_register(struct ioapic_enumerator *ne)
855 struct ioapic_enumerator *e;
857 TAILQ_FOREACH(e, &ioapic_enumerators, ioapic_link) {
858 if (e->ioapic_prio < ne->ioapic_prio) {
859 TAILQ_INSERT_BEFORE(e, ne, ioapic_link);
863 TAILQ_INSERT_TAIL(&ioapic_enumerators, ne, ioapic_link);
867 ioapic_add(void *addr, int gsi_base, int npin)
869 struct ioapic_info *info, *ninfo;
872 gsi_end = gsi_base + npin - 1;
873 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link) {
874 if ((gsi_base >= info->io_gsi_base &&
875 gsi_base < info->io_gsi_base + info->io_npin) ||
876 (gsi_end >= info->io_gsi_base &&
877 gsi_end < info->io_gsi_base + info->io_npin)) {
878 panic("ioapic_add: overlapped gsi, base %d npin %d, "
879 "hit base %d, npin %d\n", gsi_base, npin,
880 info->io_gsi_base, info->io_npin);
882 if (info->io_addr == addr)
883 panic("ioapic_add: duplicated addr %p\n", addr);
886 ninfo = kmalloc(sizeof(*ninfo), M_DEVBUF, M_WAITOK | M_ZERO);
887 ninfo->io_addr = addr;
888 ninfo->io_npin = npin;
889 ninfo->io_gsi_base = gsi_base;
890 ninfo->io_apic_id = -1;
893 * Create IOAPIC list in ascending order of GSI base
895 TAILQ_FOREACH_REVERSE(info, &ioapic_conf.ioc_list,
896 ioapic_info_list, io_link) {
897 if (ninfo->io_gsi_base > info->io_gsi_base) {
898 TAILQ_INSERT_AFTER(&ioapic_conf.ioc_list,
899 info, ninfo, io_link);
904 TAILQ_INSERT_HEAD(&ioapic_conf.ioc_list, ninfo, io_link);
908 ioapic_intsrc(int irq, int gsi, enum intr_trigger trig, enum intr_polarity pola)
910 struct ioapic_intsrc *int_src;
913 int_src = &ioapic_conf.ioc_intsrc[irq];
916 /* Don't allow mixed mode */
917 kprintf("IOAPIC: warning intsrc irq %d -> gsi 0\n", irq);
921 if (int_src->int_gsi != -1) {
922 if (int_src->int_gsi != gsi) {
923 kprintf("IOAPIC: warning intsrc irq %d, gsi "
924 "%d -> %d\n", irq, int_src->int_gsi, gsi);
926 if (int_src->int_trig != trig) {
927 kprintf("IOAPIC: warning intsrc irq %d, trig "
929 intr_str_trigger(int_src->int_trig),
930 intr_str_trigger(trig));
932 if (int_src->int_pola != pola) {
933 kprintf("IOAPIC: warning intsrc irq %d, pola "
935 intr_str_polarity(int_src->int_pola),
936 intr_str_polarity(pola));
939 int_src->int_gsi = gsi;
940 int_src->int_trig = trig;
941 int_src->int_pola = pola;
945 ioapic_set_apic_id(const struct ioapic_info *info)
950 id = ioapic_read(info->io_addr, IOAPIC_ID);
953 id |= (info->io_apic_id << 24);
955 ioapic_write(info->io_addr, IOAPIC_ID, id);
960 id = ioapic_read(info->io_addr, IOAPIC_ID);
961 apic_id = (id & APIC_ID_MASK) >> 24;
964 * I/O APIC ID is a 4bits field
966 if ((apic_id & IOAPIC_ID_MASK) !=
967 (info->io_apic_id & IOAPIC_ID_MASK)) {
968 panic("ioapic_set_apic_id: can't set apic id to %d, "
969 "currently set to %d\n", info->io_apic_id, apic_id);
974 ioapic_gsi_setup(int gsi)
976 enum intr_trigger trig;
977 enum intr_polarity pola;
983 ioapic_extpin_setup(ioapic_gsi_ioaddr(gsi),
984 ioapic_gsi_pin(gsi), 0);
989 trig = 0; /* silence older gcc's */
990 pola = 0; /* silence older gcc's */
992 for (irq = 0; irq < 16; ++irq) {
993 const struct ioapic_intsrc *int_src =
994 &ioapic_conf.ioc_intsrc[irq];
996 if (gsi == int_src->int_gsi) {
997 trig = int_src->int_trig;
998 pola = int_src->int_pola;
1005 trig = INTR_TRIGGER_EDGE;
1006 pola = INTR_POLARITY_HIGH;
1008 trig = INTR_TRIGGER_LEVEL;
1009 pola = INTR_POLARITY_LOW;
1014 ioapic_abi_set_irqmap(irq, gsi, trig, pola);
1018 ioapic_gsi_ioaddr(int gsi)
1020 const struct ioapic_info *info;
1022 info = ioapic_gsi_search(gsi);
1023 return info->io_addr;
1027 ioapic_gsi_pin(int gsi)
1029 const struct ioapic_info *info;
1031 info = ioapic_gsi_search(gsi);
1032 return gsi - info->io_gsi_base;
1035 static const struct ioapic_info *
1036 ioapic_gsi_search(int gsi)
1038 const struct ioapic_info *info;
1040 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link) {
1041 if (gsi >= info->io_gsi_base &&
1042 gsi < info->io_gsi_base + info->io_npin)
1045 panic("ioapic_gsi_search: no I/O APIC\n");
1049 ioapic_gsi(int idx, int pin)
1051 const struct ioapic_info *info;
1053 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link) {
1054 if (info->io_idx == idx)
1059 if (pin >= info->io_npin)
1061 return info->io_gsi_base + pin;
1065 ioapic_extpin_setup(void *addr, int pin, int vec)
1067 ioapic_pin_prog(addr, pin, vec,
1068 INTR_TRIGGER_CONFORM, INTR_POLARITY_CONFORM, IOART_DELEXINT);
1072 ioapic_extpin_gsi(void)
1078 ioapic_pin_setup(void *addr, int pin, int vec,
1079 enum intr_trigger trig, enum intr_polarity pola)
1082 * Always clear an I/O APIC pin before [re]programming it. This is
1083 * particularly important if the pin is set up for a level interrupt
1084 * as the IOART_REM_IRR bit might be set. When we reprogram the
1085 * vector any EOI from pending ints on this pin could be lost and
1086 * IRR might never get reset.
1088 * To fix this problem, clear the vector and make sure it is
1089 * programmed as an edge interrupt. This should theoretically
1090 * clear IRR so we can later, safely program it as a level
1093 ioapic_pin_prog(addr, pin, vec, INTR_TRIGGER_EDGE, INTR_POLARITY_HIGH,
1095 ioapic_pin_prog(addr, pin, vec, trig, pola, IOART_DELFIXED);
1099 ioapic_pin_prog(void *addr, int pin, int vec,
1100 enum intr_trigger trig, enum intr_polarity pola, uint32_t del_mode)
1102 uint32_t flags, target;
1105 KKASSERT(del_mode == IOART_DELEXINT || del_mode == IOART_DELFIXED);
1107 select = IOAPIC_REDTBL0 + (2 * pin);
1109 flags = ioapic_read(addr, select) & IOART_RESV;
1110 flags |= IOART_INTMSET | IOART_DESTPHY;
1115 * We only support limited I/O APIC mixed mode,
1116 * so even for ExtINT, we still use "fixed"
1119 flags |= IOART_DELFIXED;
1122 if (del_mode == IOART_DELEXINT) {
1123 KKASSERT(trig == INTR_TRIGGER_CONFORM &&
1124 pola == INTR_POLARITY_CONFORM);
1125 flags |= IOART_TRGREDG | IOART_INTAHI;
1128 case INTR_TRIGGER_EDGE:
1129 flags |= IOART_TRGREDG;
1132 case INTR_TRIGGER_LEVEL:
1133 flags |= IOART_TRGRLVL;
1136 case INTR_TRIGGER_CONFORM:
1137 panic("ioapic_pin_prog: trig conform is not "
1141 case INTR_POLARITY_HIGH:
1142 flags |= IOART_INTAHI;
1145 case INTR_POLARITY_LOW:
1146 flags |= IOART_INTALO;
1149 case INTR_POLARITY_CONFORM:
1150 panic("ioapic_pin_prog: pola conform is not "
1155 target = ioapic_read(addr, select + 1) & IOART_HI_DEST_RESV;
1156 target |= (CPU_TO_ID(0) << IOART_HI_DEST_SHIFT) &
1159 ioapic_write(addr, select, flags | vec);
1160 ioapic_write(addr, select + 1, target);
1164 ioapic_setup(const struct ioapic_info *info)
1168 ioapic_set_apic_id(info);
1170 for (i = 0; i < info->io_npin; ++i)
1171 ioapic_gsi_setup(info->io_gsi_base + i);
1175 ioapic_alloc_apic_id(int start)
1178 const struct ioapic_info *info;
1179 int apic_id, apic_id16;
1181 apic_id = lapic_unused_apic_id(start);
1182 if (apic_id == NAPICID) {
1183 kprintf("IOAPIC: can't find unused APIC ID\n");
1186 apic_id16 = apic_id & IOAPIC_ID_MASK;
1189 * Check against other I/O APIC's APIC ID's lower 4bits.
1191 * The new APIC ID will have to be different from others
1192 * in the lower 4bits, no matter whether xAPIC is used
1195 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link) {
1196 if (info->io_apic_id == -1) {
1200 if ((info->io_apic_id & IOAPIC_ID_MASK) == apic_id16)
1206 kprintf("IOAPIC: APIC ID %d has same lower 4bits as "
1207 "%dth I/O APIC, keep searching...\n",
1208 apic_id, info->io_idx);
1210 start = apic_id + 1;
1212 panic("ioapic_unused_apic_id: never reached\n");