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;
58 TAILQ_ENTRY(ioapic_info) io_link;
60 TAILQ_HEAD(ioapic_info_list, ioapic_info);
62 struct ioapic_intsrc {
64 enum intr_trigger int_trig;
65 enum intr_polarity int_pola;
69 struct ioapic_info_list ioc_list;
70 struct ioapic_intsrc ioc_intsrc[16]; /* XXX magic number */
73 static void lapic_timer_calibrate(void);
74 static void lapic_timer_set_divisor(int);
75 static void lapic_timer_fixup_handler(void *);
76 static void lapic_timer_restart_handler(void *);
78 void lapic_timer_process(void);
79 void lapic_timer_process_frame(struct intrframe *);
81 static int lapic_timer_enable = 1;
82 TUNABLE_INT("hw.lapic_timer_enable", &lapic_timer_enable);
84 static void lapic_timer_intr_reload(struct cputimer_intr *, sysclock_t);
85 static void lapic_timer_intr_enable(struct cputimer_intr *);
86 static void lapic_timer_intr_restart(struct cputimer_intr *);
87 static void lapic_timer_intr_pmfixup(struct cputimer_intr *);
89 static int lapic_unused_apic_id(int);
91 static void ioapic_setup(const struct ioapic_info *);
92 static int ioapic_alloc_apic_id(int);
93 static void ioapic_set_apic_id(const struct ioapic_info *);
94 static void ioapic_gsi_setup(int);
95 static const struct ioapic_info *
96 ioapic_gsi_search(int);
97 static void ioapic_pin_prog(void *, int, int,
98 enum intr_trigger, enum intr_polarity, uint32_t);
100 static struct cputimer_intr lapic_cputimer_intr = {
102 .reload = lapic_timer_intr_reload,
103 .enable = lapic_timer_intr_enable,
104 .config = cputimer_intr_default_config,
105 .restart = lapic_timer_intr_restart,
106 .pmfixup = lapic_timer_intr_pmfixup,
107 .initclock = cputimer_intr_default_initclock,
108 .next = SLIST_ENTRY_INITIALIZER,
110 .type = CPUTIMER_INTR_LAPIC,
111 .prio = CPUTIMER_INTR_PRIO_LAPIC,
112 .caps = CPUTIMER_INTR_CAP_NONE
115 static int lapic_timer_divisor_idx = -1;
116 static const uint32_t lapic_timer_divisors[] = {
117 APIC_TDCR_2, APIC_TDCR_4, APIC_TDCR_8, APIC_TDCR_16,
118 APIC_TDCR_32, APIC_TDCR_64, APIC_TDCR_128, APIC_TDCR_1
120 #define APIC_TIMER_NDIVISORS (int)(NELEM(lapic_timer_divisors))
122 static struct ioapic_conf ioapic_conf;
125 * Enable LAPIC, configure interrupts.
128 lapic_init(boolean_t bsp)
136 * Since IDT is shared between BSP and APs, these vectors
137 * only need to be installed once; we do it on BSP.
140 /* Install a 'Spurious INTerrupt' vector */
141 setidt(XSPURIOUSINT_OFFSET, Xspuriousint,
142 SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
144 /* Install an inter-CPU IPI for TLB invalidation */
145 setidt(XINVLTLB_OFFSET, Xinvltlb,
146 SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
148 /* Install an inter-CPU IPI for IPIQ messaging */
149 setidt(XIPIQ_OFFSET, Xipiq,
150 SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
152 /* Install a timer vector */
153 setidt(XTIMER_OFFSET, Xtimer,
154 SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
156 /* Install an inter-CPU IPI for CPU stop/restart */
157 setidt(XCPUSTOP_OFFSET, Xcpustop,
158 SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
162 * Setup LINT0 as ExtINT on the BSP. This is theoretically an
163 * aggregate interrupt input from the 8259. The INTA cycle
164 * will be routed to the external controller (the 8259) which
165 * is expected to supply the vector.
167 * Must be setup edge triggered, active high.
169 * Disable LINT0 on BSP, if I/O APIC is enabled.
171 * Disable LINT0 on the APs. It doesn't matter what delivery
172 * mode we use because we leave it masked.
174 temp = lapic.lvt_lint0;
175 temp &= ~(APIC_LVT_MASKED | APIC_LVT_TRIG_MASK |
176 APIC_LVT_POLARITY_MASK | APIC_LVT_DM_MASK);
178 temp |= APIC_LVT_DM_EXTINT;
180 temp |= APIC_LVT_MASKED;
182 temp |= APIC_LVT_DM_FIXED | APIC_LVT_MASKED;
184 lapic.lvt_lint0 = temp;
187 * Setup LINT1 as NMI.
189 * Must be setup edge trigger, active high.
191 * Enable LINT1 on BSP, if I/O APIC is enabled.
193 * Disable LINT1 on the APs.
195 temp = lapic.lvt_lint1;
196 temp &= ~(APIC_LVT_MASKED | APIC_LVT_TRIG_MASK |
197 APIC_LVT_POLARITY_MASK | APIC_LVT_DM_MASK);
198 temp |= APIC_LVT_MASKED | APIC_LVT_DM_NMI;
199 if (bsp && apic_io_enable)
200 temp &= ~APIC_LVT_MASKED;
201 lapic.lvt_lint1 = temp;
204 * Mask the LAPIC error interrupt, LAPIC performance counter
207 lapic.lvt_error = lapic.lvt_error | APIC_LVT_MASKED;
208 lapic.lvt_pcint = lapic.lvt_pcint | APIC_LVT_MASKED;
211 * Set LAPIC timer vector and mask the LAPIC timer interrupt.
213 timer = lapic.lvt_timer;
214 timer &= ~APIC_LVTT_VECTOR;
215 timer |= XTIMER_OFFSET;
216 timer |= APIC_LVTT_MASKED;
217 lapic.lvt_timer = timer;
220 * Set the Task Priority Register as needed. At the moment allow
221 * interrupts on all cpus (the APs will remain CLId until they are
222 * ready to deal). We could disable all but IPIs by setting
223 * temp |= TPR_IPI for cpu != 0.
226 temp &= ~APIC_TPR_PRIO; /* clear priority field */
227 #ifdef SMP /* APIC-IO */
228 if (!apic_io_enable) {
231 * If we are NOT running the IO APICs, the LAPIC will only be used
232 * for IPIs. Set the TPR to prevent any unintentional interrupts.
235 #ifdef SMP /* APIC-IO */
245 temp |= APIC_SVR_ENABLE; /* enable the LAPIC */
246 temp &= ~APIC_SVR_FOCUS_DISABLE; /* enable lopri focus processor */
249 * Set the spurious interrupt vector. The low 4 bits of the vector
252 if ((XSPURIOUSINT_OFFSET & 0x0F) != 0x0F)
253 panic("bad XSPURIOUSINT_OFFSET: 0x%08x", XSPURIOUSINT_OFFSET);
254 temp &= ~APIC_SVR_VECTOR;
255 temp |= XSPURIOUSINT_OFFSET;
260 * Pump out a few EOIs to clean out interrupts that got through
261 * before we were able to set the TPR.
268 lapic_timer_calibrate();
269 if (lapic_timer_enable) {
270 cputimer_intr_register(&lapic_cputimer_intr);
271 cputimer_intr_select(&lapic_cputimer_intr, 0);
274 lapic_timer_set_divisor(lapic_timer_divisor_idx);
278 apic_dump("apic_initialize()");
282 lapic_timer_set_divisor(int divisor_idx)
284 KKASSERT(divisor_idx >= 0 && divisor_idx < APIC_TIMER_NDIVISORS);
285 lapic.dcr_timer = lapic_timer_divisors[divisor_idx];
289 lapic_timer_oneshot(u_int count)
293 value = lapic.lvt_timer;
294 value &= ~APIC_LVTT_PERIODIC;
295 lapic.lvt_timer = value;
296 lapic.icr_timer = count;
300 lapic_timer_oneshot_quick(u_int count)
302 lapic.icr_timer = count;
306 lapic_timer_calibrate(void)
310 /* Try to calibrate the local APIC timer. */
311 for (lapic_timer_divisor_idx = 0;
312 lapic_timer_divisor_idx < APIC_TIMER_NDIVISORS;
313 lapic_timer_divisor_idx++) {
314 lapic_timer_set_divisor(lapic_timer_divisor_idx);
315 lapic_timer_oneshot(APIC_TIMER_MAX_COUNT);
317 value = APIC_TIMER_MAX_COUNT - lapic.ccr_timer;
318 if (value != APIC_TIMER_MAX_COUNT)
321 if (lapic_timer_divisor_idx >= APIC_TIMER_NDIVISORS)
322 panic("lapic: no proper timer divisor?!\n");
323 lapic_cputimer_intr.freq = value / 2;
325 kprintf("lapic: divisor index %d, frequency %u Hz\n",
326 lapic_timer_divisor_idx, lapic_cputimer_intr.freq);
330 lapic_timer_process_oncpu(struct globaldata *gd, struct intrframe *frame)
334 gd->gd_timer_running = 0;
336 count = sys_cputimer->count();
337 if (TAILQ_FIRST(&gd->gd_systimerq) != NULL)
338 systimer_intr(&count, 0, frame);
342 lapic_timer_process(void)
344 lapic_timer_process_oncpu(mycpu, NULL);
348 lapic_timer_process_frame(struct intrframe *frame)
350 lapic_timer_process_oncpu(mycpu, frame);
354 lapic_timer_intr_reload(struct cputimer_intr *cti, sysclock_t reload)
356 struct globaldata *gd = mycpu;
358 reload = (int64_t)reload * cti->freq / sys_cputimer->freq;
362 if (gd->gd_timer_running) {
363 if (reload < lapic.ccr_timer)
364 lapic_timer_oneshot_quick(reload);
366 gd->gd_timer_running = 1;
367 lapic_timer_oneshot_quick(reload);
372 lapic_timer_intr_enable(struct cputimer_intr *cti __unused)
376 timer = lapic.lvt_timer;
377 timer &= ~(APIC_LVTT_MASKED | APIC_LVTT_PERIODIC);
378 lapic.lvt_timer = timer;
380 lapic_timer_fixup_handler(NULL);
384 lapic_timer_fixup_handler(void *arg)
391 if (cpu_vendor_id == CPU_VENDOR_AMD) {
393 * Detect the presence of C1E capability mostly on latest
394 * dual-cores (or future) k8 family. This feature renders
395 * the local APIC timer dead, so we disable it by reading
396 * the Interrupt Pending Message register and clearing both
397 * C1eOnCmpHalt (bit 28) and SmiOnCmpHalt (bit 27).
400 * "BIOS and Kernel Developer's Guide for AMD NPT
401 * Family 0Fh Processors"
402 * #32559 revision 3.00
404 if ((cpu_id & 0x00000f00) == 0x00000f00 &&
405 (cpu_id & 0x0fff0000) >= 0x00040000) {
408 msr = rdmsr(0xc0010055);
409 if (msr & 0x18000000) {
410 struct globaldata *gd = mycpu;
412 kprintf("cpu%d: AMD C1E detected\n",
414 wrmsr(0xc0010055, msr & ~0x18000000ULL);
417 * We are kinda stalled;
420 gd->gd_timer_running = 1;
421 lapic_timer_oneshot_quick(2);
431 lapic_timer_restart_handler(void *dummy __unused)
435 lapic_timer_fixup_handler(&started);
437 struct globaldata *gd = mycpu;
439 gd->gd_timer_running = 1;
440 lapic_timer_oneshot_quick(2);
445 * This function is called only by ACPI-CA code currently:
446 * - AMD C1E fixup. AMD C1E only seems to happen after ACPI
447 * module controls PM. So once ACPI-CA is attached, we try
448 * to apply the fixup to prevent LAPIC timer from hanging.
451 lapic_timer_intr_pmfixup(struct cputimer_intr *cti __unused)
453 lwkt_send_ipiq_mask(smp_active_mask,
454 lapic_timer_fixup_handler, NULL);
458 lapic_timer_intr_restart(struct cputimer_intr *cti __unused)
460 lwkt_send_ipiq_mask(smp_active_mask, lapic_timer_restart_handler, NULL);
465 * dump contents of local APIC registers
470 kprintf("SMP: CPU%d %s:\n", mycpu->gd_cpuid, str);
471 kprintf(" lint0: 0x%08x lint1: 0x%08x TPR: 0x%08x SVR: 0x%08x\n",
472 lapic.lvt_lint0, lapic.lvt_lint1, lapic.tpr, lapic.svr);
476 * Inter Processor Interrupt functions.
480 * Send APIC IPI 'vector' to 'destType' via 'deliveryMode'.
482 * destType is 1 of: APIC_DEST_SELF, APIC_DEST_ALLISELF, APIC_DEST_ALLESELF
483 * vector is any valid SYSTEM INT vector
484 * delivery_mode is 1 of: APIC_DELMODE_FIXED, APIC_DELMODE_LOWPRIO
486 * A backlog of requests can create a deadlock between cpus. To avoid this
487 * we have to be able to accept IPIs at the same time we are trying to send
488 * them. The critical section prevents us from attempting to send additional
489 * IPIs reentrantly, but also prevents IPIQ processing so we have to call
490 * lwkt_process_ipiq() manually. It's rather messy and expensive for this
491 * to occur but fortunately it does not happen too often.
494 apic_ipi(int dest_type, int vector, int delivery_mode)
499 if ((lapic.icr_lo & APIC_DELSTAT_MASK) != 0) {
500 unsigned int eflags = read_eflags();
502 DEBUG_PUSH_INFO("apic_ipi");
503 while ((lapic.icr_lo & APIC_DELSTAT_MASK) != 0) {
507 write_eflags(eflags);
510 icr_lo = (lapic.icr_lo & APIC_ICRLO_RESV_MASK) | dest_type |
511 delivery_mode | vector;
512 lapic.icr_lo = icr_lo;
518 single_apic_ipi(int cpu, int vector, int delivery_mode)
524 if ((lapic.icr_lo & APIC_DELSTAT_MASK) != 0) {
525 unsigned int eflags = read_eflags();
527 DEBUG_PUSH_INFO("single_apic_ipi");
528 while ((lapic.icr_lo & APIC_DELSTAT_MASK) != 0) {
532 write_eflags(eflags);
534 icr_hi = lapic.icr_hi & ~APIC_ID_MASK;
535 icr_hi |= (CPU_TO_ID(cpu) << 24);
536 lapic.icr_hi = icr_hi;
539 icr_lo = (lapic.icr_lo & APIC_ICRLO_RESV_MASK)
540 | APIC_DEST_DESTFLD | delivery_mode | vector;
543 lapic.icr_lo = icr_lo;
550 * Returns 0 if the apic is busy, 1 if we were able to queue the request.
552 * NOT WORKING YET! The code as-is may end up not queueing an IPI at all
553 * to the target, and the scheduler does not 'poll' for IPI messages.
556 single_apic_ipi_passive(int cpu, int vector, int delivery_mode)
562 if ((lapic.icr_lo & APIC_DELSTAT_MASK) != 0) {
566 icr_hi = lapic.icr_hi & ~APIC_ID_MASK;
567 icr_hi |= (CPU_TO_ID(cpu) << 24);
568 lapic.icr_hi = icr_hi;
571 icr_lo = (lapic.icr_lo & APIC_RESV2_MASK)
572 | APIC_DEST_DESTFLD | delivery_mode | vector;
575 lapic.icr_lo = icr_lo;
583 * Send APIC IPI 'vector' to 'target's via 'delivery_mode'.
585 * target is a bitmask of destination cpus. Vector is any
586 * valid system INT vector. Delivery mode may be either
587 * APIC_DELMODE_FIXED or APIC_DELMODE_LOWPRIO.
590 selected_apic_ipi(cpumask_t target, int vector, int delivery_mode)
594 int n = BSFCPUMASK(target);
595 target &= ~CPUMASK(n);
596 single_apic_ipi(n, vector, delivery_mode);
602 * Timer code, in development...
603 * - suggested by rgrimes@gndrsh.aac.dev.com
606 get_apic_timer_frequency(void)
608 return(lapic_cputimer_intr.freq);
612 * Load a 'downcount time' in uSeconds.
615 set_apic_timer(int us)
620 * When we reach here, lapic timer's frequency
621 * must have been calculated as well as the
622 * divisor (lapic.dcr_timer is setup during the
623 * divisor calculation).
625 KKASSERT(lapic_cputimer_intr.freq != 0 &&
626 lapic_timer_divisor_idx >= 0);
628 count = ((us * (int64_t)lapic_cputimer_intr.freq) + 999999) / 1000000;
629 lapic_timer_oneshot(count);
634 * Read remaining time in timer.
637 read_apic_timer(void)
640 /** XXX FIXME: we need to return the actual remaining time,
641 * for now we just return the remaining count.
644 return lapic.ccr_timer;
650 * Spin-style delay, set delay time in uS, spin till it drains.
655 set_apic_timer(count);
656 while (read_apic_timer())
661 lapic_unused_apic_id(int start)
665 for (i = start; i < NAPICID; ++i) {
666 if (ID_TO_CPU(i) == -1)
673 lapic_map(vm_offset_t lapic_addr)
675 /* Local apic is mapped on last page */
676 SMPpt[NPTEPG - 1] = (pt_entry_t)(PG_V | PG_RW | PG_N |
677 pmap_get_pgeflag() | (lapic_addr & PG_FRAME));
679 kprintf("lapic: at %p\n", (void *)lapic_addr);
682 static TAILQ_HEAD(, lapic_enumerator) lapic_enumerators =
683 TAILQ_HEAD_INITIALIZER(lapic_enumerators);
688 struct lapic_enumerator *e;
691 for (i = 0; i < NAPICID; ++i)
694 TAILQ_FOREACH(e, &lapic_enumerators, lapic_link) {
695 error = e->lapic_probe(e);
700 panic("can't config lapic\n");
702 e->lapic_enumerate(e);
706 lapic_enumerator_register(struct lapic_enumerator *ne)
708 struct lapic_enumerator *e;
710 TAILQ_FOREACH(e, &lapic_enumerators, lapic_link) {
711 if (e->lapic_prio < ne->lapic_prio) {
712 TAILQ_INSERT_BEFORE(e, ne, lapic_link);
716 TAILQ_INSERT_TAIL(&lapic_enumerators, ne, lapic_link);
719 static TAILQ_HEAD(, ioapic_enumerator) ioapic_enumerators =
720 TAILQ_HEAD_INITIALIZER(ioapic_enumerators);
725 struct ioapic_info *info;
726 int start_apic_id = 0;
727 struct ioapic_enumerator *e;
731 TAILQ_INIT(&ioapic_conf.ioc_list);
732 /* XXX magic number */
733 for (i = 0; i < 16; ++i)
734 ioapic_conf.ioc_intsrc[i].int_gsi = -1;
736 TAILQ_FOREACH(e, &ioapic_enumerators, ioapic_link) {
737 error = e->ioapic_probe(e);
743 panic("can't config I/O APIC\n");
745 kprintf("no I/O APIC\n");
756 * Switch to I/O APIC MachIntrABI and reconfigure
757 * the default IDT entries.
759 MachIntrABI = MachIntrABI_IOAPIC;
760 MachIntrABI.setdefault();
762 e->ioapic_enumerate(e);
768 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link)
771 if (i > IOAPIC_COUNT_MAX) /* XXX magic number */
772 panic("ioapic_config: more than 16 I/O APIC\n");
777 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link) {
780 apic_id = ioapic_alloc_apic_id(start_apic_id);
781 if (apic_id == NAPICID) {
782 kprintf("IOAPIC: can't alloc APIC ID for "
783 "%dth I/O APIC\n", info->io_idx);
786 info->io_apic_id = apic_id;
788 start_apic_id = apic_id + 1;
792 * xAPIC allows I/O APIC's APIC ID to be same
793 * as the LAPIC's APIC ID
795 kprintf("IOAPIC: use xAPIC model to alloc APIC ID "
798 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link)
799 info->io_apic_id = info->io_idx;
803 * Warning about any GSI holes
805 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link) {
806 const struct ioapic_info *prev_info;
808 prev_info = TAILQ_PREV(info, ioapic_info_list, io_link);
809 if (prev_info != NULL) {
810 if (info->io_gsi_base !=
811 prev_info->io_gsi_base + prev_info->io_npin) {
812 kprintf("IOAPIC: warning gsi hole "
814 prev_info->io_gsi_base +
816 info->io_gsi_base - 1);
822 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link) {
823 kprintf("IOAPIC: idx %d, apic id %d, "
824 "gsi base %d, npin %d\n",
835 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link)
837 ioapic_abi_fixup_irqmap();
841 MachIntrABI.cleanup();
847 ioapic_enumerator_register(struct ioapic_enumerator *ne)
849 struct ioapic_enumerator *e;
851 TAILQ_FOREACH(e, &ioapic_enumerators, ioapic_link) {
852 if (e->ioapic_prio < ne->ioapic_prio) {
853 TAILQ_INSERT_BEFORE(e, ne, ioapic_link);
857 TAILQ_INSERT_TAIL(&ioapic_enumerators, ne, ioapic_link);
861 ioapic_add(void *addr, int gsi_base, int npin)
863 struct ioapic_info *info, *ninfo;
866 gsi_end = gsi_base + npin - 1;
867 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link) {
868 if ((gsi_base >= info->io_gsi_base &&
869 gsi_base < info->io_gsi_base + info->io_npin) ||
870 (gsi_end >= info->io_gsi_base &&
871 gsi_end < info->io_gsi_base + info->io_npin)) {
872 panic("ioapic_add: overlapped gsi, base %d npin %d, "
873 "hit base %d, npin %d\n", gsi_base, npin,
874 info->io_gsi_base, info->io_npin);
876 if (info->io_addr == addr)
877 panic("ioapic_add: duplicated addr %p\n", addr);
880 ninfo = kmalloc(sizeof(*ninfo), M_DEVBUF, M_WAITOK | M_ZERO);
881 ninfo->io_addr = addr;
882 ninfo->io_npin = npin;
883 ninfo->io_gsi_base = gsi_base;
884 ninfo->io_apic_id = -1;
887 * Create IOAPIC list in ascending order of GSI base
889 TAILQ_FOREACH_REVERSE(info, &ioapic_conf.ioc_list,
890 ioapic_info_list, io_link) {
891 if (ninfo->io_gsi_base > info->io_gsi_base) {
892 TAILQ_INSERT_AFTER(&ioapic_conf.ioc_list,
893 info, ninfo, io_link);
898 TAILQ_INSERT_HEAD(&ioapic_conf.ioc_list, ninfo, io_link);
902 ioapic_intsrc(int irq, int gsi, enum intr_trigger trig, enum intr_polarity pola)
904 struct ioapic_intsrc *int_src;
907 int_src = &ioapic_conf.ioc_intsrc[irq];
910 /* Don't allow mixed mode */
911 kprintf("IOAPIC: warning intsrc irq %d -> gsi 0\n", irq);
915 if (int_src->int_gsi != -1) {
916 if (int_src->int_gsi != gsi) {
917 kprintf("IOAPIC: warning intsrc irq %d, gsi "
918 "%d -> %d\n", irq, int_src->int_gsi, gsi);
920 if (int_src->int_trig != trig) {
921 kprintf("IOAPIC: warning intsrc irq %d, trig "
923 intr_str_trigger(int_src->int_trig),
924 intr_str_trigger(trig));
926 if (int_src->int_pola != pola) {
927 kprintf("IOAPIC: warning intsrc irq %d, pola "
929 intr_str_polarity(int_src->int_pola),
930 intr_str_polarity(pola));
933 int_src->int_gsi = gsi;
934 int_src->int_trig = trig;
935 int_src->int_pola = pola;
939 ioapic_set_apic_id(const struct ioapic_info *info)
944 id = ioapic_read(info->io_addr, IOAPIC_ID);
947 id |= (info->io_apic_id << 24);
949 ioapic_write(info->io_addr, IOAPIC_ID, id);
954 id = ioapic_read(info->io_addr, IOAPIC_ID);
955 apic_id = (id & APIC_ID_MASK) >> 24;
958 * I/O APIC ID is a 4bits field
960 if ((apic_id & IOAPIC_ID_MASK) !=
961 (info->io_apic_id & IOAPIC_ID_MASK)) {
962 panic("ioapic_set_apic_id: can't set apic id to %d, "
963 "currently set to %d\n", info->io_apic_id, apic_id);
968 ioapic_gsi_setup(int gsi)
970 enum intr_trigger trig;
971 enum intr_polarity pola;
977 ioapic_extpin_setup(ioapic_gsi_ioaddr(gsi),
978 ioapic_gsi_pin(gsi), 0);
983 trig = 0; /* silence older gcc's */
984 pola = 0; /* silence older gcc's */
986 for (irq = 0; irq < 16; ++irq) {
987 const struct ioapic_intsrc *int_src =
988 &ioapic_conf.ioc_intsrc[irq];
990 if (gsi == int_src->int_gsi) {
991 trig = int_src->int_trig;
992 pola = int_src->int_pola;
999 trig = INTR_TRIGGER_EDGE;
1000 pola = INTR_POLARITY_HIGH;
1002 trig = INTR_TRIGGER_LEVEL;
1003 pola = INTR_POLARITY_LOW;
1008 ioapic_abi_set_irqmap(irq, gsi, trig, pola);
1012 ioapic_gsi_ioaddr(int gsi)
1014 const struct ioapic_info *info;
1016 info = ioapic_gsi_search(gsi);
1017 return info->io_addr;
1021 ioapic_gsi_pin(int gsi)
1023 const struct ioapic_info *info;
1025 info = ioapic_gsi_search(gsi);
1026 return gsi - info->io_gsi_base;
1029 static const struct ioapic_info *
1030 ioapic_gsi_search(int gsi)
1032 const struct ioapic_info *info;
1034 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link) {
1035 if (gsi >= info->io_gsi_base &&
1036 gsi < info->io_gsi_base + info->io_npin)
1039 panic("ioapic_gsi_search: no I/O APIC\n");
1043 ioapic_gsi(int idx, int pin)
1045 const struct ioapic_info *info;
1047 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link) {
1048 if (info->io_idx == idx)
1053 if (pin >= info->io_npin)
1055 return info->io_gsi_base + pin;
1059 ioapic_extpin_setup(void *addr, int pin, int vec)
1061 ioapic_pin_prog(addr, pin, vec,
1062 INTR_TRIGGER_CONFORM, INTR_POLARITY_CONFORM, IOART_DELEXINT);
1066 ioapic_extpin_gsi(void)
1072 ioapic_pin_setup(void *addr, int pin, int vec,
1073 enum intr_trigger trig, enum intr_polarity pola)
1076 * Always clear an I/O APIC pin before [re]programming it. This is
1077 * particularly important if the pin is set up for a level interrupt
1078 * as the IOART_REM_IRR bit might be set. When we reprogram the
1079 * vector any EOI from pending ints on this pin could be lost and
1080 * IRR might never get reset.
1082 * To fix this problem, clear the vector and make sure it is
1083 * programmed as an edge interrupt. This should theoretically
1084 * clear IRR so we can later, safely program it as a level
1087 ioapic_pin_prog(addr, pin, vec, INTR_TRIGGER_EDGE, INTR_POLARITY_HIGH,
1089 ioapic_pin_prog(addr, pin, vec, trig, pola, IOART_DELFIXED);
1093 ioapic_pin_prog(void *addr, int pin, int vec,
1094 enum intr_trigger trig, enum intr_polarity pola, uint32_t del_mode)
1096 uint32_t flags, target;
1099 KKASSERT(del_mode == IOART_DELEXINT || del_mode == IOART_DELFIXED);
1101 select = IOAPIC_REDTBL0 + (2 * pin);
1103 flags = ioapic_read(addr, select) & IOART_RESV;
1104 flags |= IOART_INTMSET | IOART_DESTPHY;
1109 * We only support limited I/O APIC mixed mode,
1110 * so even for ExtINT, we still use "fixed"
1113 flags |= IOART_DELFIXED;
1116 if (del_mode == IOART_DELEXINT) {
1117 KKASSERT(trig == INTR_TRIGGER_CONFORM &&
1118 pola == INTR_POLARITY_CONFORM);
1119 flags |= IOART_TRGREDG | IOART_INTAHI;
1122 case INTR_TRIGGER_EDGE:
1123 flags |= IOART_TRGREDG;
1126 case INTR_TRIGGER_LEVEL:
1127 flags |= IOART_TRGRLVL;
1130 case INTR_TRIGGER_CONFORM:
1131 panic("ioapic_pin_prog: trig conform is not "
1135 case INTR_POLARITY_HIGH:
1136 flags |= IOART_INTAHI;
1139 case INTR_POLARITY_LOW:
1140 flags |= IOART_INTALO;
1143 case INTR_POLARITY_CONFORM:
1144 panic("ioapic_pin_prog: pola conform is not "
1149 target = ioapic_read(addr, select + 1) & IOART_HI_DEST_RESV;
1150 target |= (CPU_TO_ID(0) << IOART_HI_DEST_SHIFT) &
1153 ioapic_write(addr, select, flags | vec);
1154 ioapic_write(addr, select + 1, target);
1158 ioapic_setup(const struct ioapic_info *info)
1162 ioapic_set_apic_id(info);
1164 for (i = 0; i < info->io_npin; ++i)
1165 ioapic_gsi_setup(info->io_gsi_base + i);
1169 ioapic_alloc_apic_id(int start)
1172 const struct ioapic_info *info;
1173 int apic_id, apic_id16;
1175 apic_id = lapic_unused_apic_id(start);
1176 if (apic_id == NAPICID) {
1177 kprintf("IOAPIC: can't find unused APIC ID\n");
1180 apic_id16 = apic_id & IOAPIC_ID_MASK;
1183 * Check against other I/O APIC's APIC ID's lower 4bits.
1185 * The new APIC ID will have to be different from others
1186 * in the lower 4bits, no matter whether xAPIC is used
1189 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link) {
1190 if (info->io_apic_id == -1) {
1194 if ((info->io_apic_id & IOAPIC_ID_MASK) == apic_id16)
1200 kprintf("IOAPIC: APIC ID %d has same lower 4bits as "
1201 "%dth I/O APIC, keep searching...\n",
1202 apic_id, info->io_idx);
1204 start = apic_id + 1;
1206 panic("ioapic_unused_apic_id: never reached\n");