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/cputypes.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>
45 extern pt_entry_t *SMPpt;
47 /* EISA Edge/Level trigger control registers */
48 #define ELCR0 0x4d0 /* eisa irq 0-7 */
49 #define ELCR1 0x4d1 /* eisa irq 8-15 */
58 TAILQ_ENTRY(ioapic_info) io_link;
60 TAILQ_HEAD(ioapic_info_list, ioapic_info);
63 struct ioapic_info_list ioc_list;
64 int ioc_intsrc[16]; /* XXX magic number */
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 *);
75 static int lapic_timer_enable = 1;
76 TUNABLE_INT("hw.lapic_timer_enable", &lapic_timer_enable);
78 static void lapic_timer_intr_reload(struct cputimer_intr *, sysclock_t);
79 static void lapic_timer_intr_enable(struct cputimer_intr *);
80 static void lapic_timer_intr_restart(struct cputimer_intr *);
81 static void lapic_timer_intr_pmfixup(struct cputimer_intr *);
83 static void ioapic_setup(const struct ioapic_info *);
84 static void ioapic_set_apic_id(const struct ioapic_info *);
85 static void ioapic_gsi_setup(int);
86 static const struct ioapic_info *
87 ioapic_gsi_search(int);
88 static void ioapic_pin_prog(void *, int, int,
89 enum intr_trigger, enum intr_polarity, uint32_t);
91 static struct cputimer_intr lapic_cputimer_intr = {
93 .reload = lapic_timer_intr_reload,
94 .enable = lapic_timer_intr_enable,
95 .config = cputimer_intr_default_config,
96 .restart = lapic_timer_intr_restart,
97 .pmfixup = lapic_timer_intr_pmfixup,
98 .initclock = cputimer_intr_default_initclock,
99 .next = SLIST_ENTRY_INITIALIZER,
101 .type = CPUTIMER_INTR_LAPIC,
102 .prio = CPUTIMER_INTR_PRIO_LAPIC,
103 .caps = CPUTIMER_INTR_CAP_NONE
107 * pointers to pmapped apic hardware.
110 volatile ioapic_t **ioapic;
112 static int lapic_timer_divisor_idx = -1;
113 static const uint32_t lapic_timer_divisors[] = {
114 APIC_TDCR_2, APIC_TDCR_4, APIC_TDCR_8, APIC_TDCR_16,
115 APIC_TDCR_32, APIC_TDCR_64, APIC_TDCR_128, APIC_TDCR_1
117 #define APIC_TIMER_NDIVISORS (int)(NELEM(lapic_timer_divisors))
121 static struct ioapic_conf ioapic_conf;
124 * Enable LAPIC, configure interrupts.
127 lapic_init(boolean_t bsp)
135 * Since IDT is shared between BSP and APs, these vectors
136 * only need to be installed once; we do it on BSP.
139 /* Install a 'Spurious INTerrupt' vector */
140 setidt(XSPURIOUSINT_OFFSET, Xspuriousint,
141 SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
143 /* Install an inter-CPU IPI for TLB invalidation */
144 setidt(XINVLTLB_OFFSET, Xinvltlb,
145 SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
147 /* Install an inter-CPU IPI for IPIQ messaging */
148 setidt(XIPIQ_OFFSET, Xipiq,
149 SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
151 /* Install a timer vector */
152 setidt(XTIMER_OFFSET, Xtimer,
153 SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
155 /* Install an inter-CPU IPI for CPU stop/restart */
156 setidt(XCPUSTOP_OFFSET, Xcpustop,
157 SDT_SYS386IGT, SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
161 * Setup LINT0 as ExtINT on the BSP. This is theoretically an
162 * aggregate interrupt input from the 8259. The INTA cycle
163 * will be routed to the external controller (the 8259) which
164 * is expected to supply the vector.
166 * Must be setup edge triggered, active high.
168 * Disable LINT0 on the APs. It doesn't matter what delivery
169 * mode we use because we leave it masked.
171 temp = lapic.lvt_lint0;
172 temp &= ~(APIC_LVT_MASKED | APIC_LVT_TRIG_MASK |
173 APIC_LVT_POLARITY_MASK | APIC_LVT_DM_MASK);
174 if (mycpu->gd_cpuid == 0)
175 temp |= APIC_LVT_DM_EXTINT;
177 temp |= APIC_LVT_DM_FIXED | APIC_LVT_MASKED;
178 lapic.lvt_lint0 = temp;
181 * Setup LINT1 as NMI, masked till later.
182 * Edge trigger, active high.
184 temp = lapic.lvt_lint1;
185 temp &= ~(APIC_LVT_MASKED | APIC_LVT_TRIG_MASK |
186 APIC_LVT_POLARITY_MASK | APIC_LVT_DM_MASK);
187 temp |= APIC_LVT_MASKED | APIC_LVT_DM_NMI;
188 lapic.lvt_lint1 = temp;
191 * Mask the LAPIC error interrupt, LAPIC performance counter
194 lapic.lvt_error = lapic.lvt_error | APIC_LVT_MASKED;
195 lapic.lvt_pcint = lapic.lvt_pcint | APIC_LVT_MASKED;
198 * Set LAPIC timer vector and mask the LAPIC timer interrupt.
200 timer = lapic.lvt_timer;
201 timer &= ~APIC_LVTT_VECTOR;
202 timer |= XTIMER_OFFSET;
203 timer |= APIC_LVTT_MASKED;
204 lapic.lvt_timer = timer;
207 * Set the Task Priority Register as needed. At the moment allow
208 * interrupts on all cpus (the APs will remain CLId until they are
209 * ready to deal). We could disable all but IPIs by setting
210 * temp |= TPR_IPI for cpu != 0.
213 temp &= ~APIC_TPR_PRIO; /* clear priority field */
214 #ifdef SMP /* APIC-IO */
215 if (!apic_io_enable) {
218 * If we are NOT running the IO APICs, the LAPIC will only be used
219 * for IPIs. Set the TPR to prevent any unintentional interrupts.
222 #ifdef SMP /* APIC-IO */
232 temp |= APIC_SVR_ENABLE; /* enable the LAPIC */
233 temp &= ~APIC_SVR_FOCUS_DISABLE; /* enable lopri focus processor */
236 * Set the spurious interrupt vector. The low 4 bits of the vector
239 if ((XSPURIOUSINT_OFFSET & 0x0F) != 0x0F)
240 panic("bad XSPURIOUSINT_OFFSET: 0x%08x", XSPURIOUSINT_OFFSET);
241 temp &= ~APIC_SVR_VECTOR;
242 temp |= XSPURIOUSINT_OFFSET;
247 * Pump out a few EOIs to clean out interrupts that got through
248 * before we were able to set the TPR.
255 lapic_timer_calibrate();
256 if (lapic_timer_enable) {
257 cputimer_intr_register(&lapic_cputimer_intr);
258 cputimer_intr_select(&lapic_cputimer_intr, 0);
261 lapic_timer_set_divisor(lapic_timer_divisor_idx);
265 apic_dump("apic_initialize()");
269 lapic_timer_set_divisor(int divisor_idx)
271 KKASSERT(divisor_idx >= 0 && divisor_idx < APIC_TIMER_NDIVISORS);
272 lapic.dcr_timer = lapic_timer_divisors[divisor_idx];
276 lapic_timer_oneshot(u_int count)
280 value = lapic.lvt_timer;
281 value &= ~APIC_LVTT_PERIODIC;
282 lapic.lvt_timer = value;
283 lapic.icr_timer = count;
287 lapic_timer_oneshot_quick(u_int count)
289 lapic.icr_timer = count;
293 lapic_timer_calibrate(void)
297 /* Try to calibrate the local APIC timer. */
298 for (lapic_timer_divisor_idx = 0;
299 lapic_timer_divisor_idx < APIC_TIMER_NDIVISORS;
300 lapic_timer_divisor_idx++) {
301 lapic_timer_set_divisor(lapic_timer_divisor_idx);
302 lapic_timer_oneshot(APIC_TIMER_MAX_COUNT);
304 value = APIC_TIMER_MAX_COUNT - lapic.ccr_timer;
305 if (value != APIC_TIMER_MAX_COUNT)
308 if (lapic_timer_divisor_idx >= APIC_TIMER_NDIVISORS)
309 panic("lapic: no proper timer divisor?!\n");
310 lapic_cputimer_intr.freq = value / 2;
312 kprintf("lapic: divisor index %d, frequency %u Hz\n",
313 lapic_timer_divisor_idx, lapic_cputimer_intr.freq);
317 lapic_timer_process_oncpu(struct globaldata *gd, struct intrframe *frame)
321 gd->gd_timer_running = 0;
323 count = sys_cputimer->count();
324 if (TAILQ_FIRST(&gd->gd_systimerq) != NULL)
325 systimer_intr(&count, 0, frame);
329 lapic_timer_process(void)
331 lapic_timer_process_oncpu(mycpu, NULL);
335 lapic_timer_process_frame(struct intrframe *frame)
337 lapic_timer_process_oncpu(mycpu, frame);
341 lapic_timer_intr_reload(struct cputimer_intr *cti, sysclock_t reload)
343 struct globaldata *gd = mycpu;
345 reload = (int64_t)reload * cti->freq / sys_cputimer->freq;
349 if (gd->gd_timer_running) {
350 if (reload < lapic.ccr_timer)
351 lapic_timer_oneshot_quick(reload);
353 gd->gd_timer_running = 1;
354 lapic_timer_oneshot_quick(reload);
359 lapic_timer_intr_enable(struct cputimer_intr *cti __unused)
363 timer = lapic.lvt_timer;
364 timer &= ~(APIC_LVTT_MASKED | APIC_LVTT_PERIODIC);
365 lapic.lvt_timer = timer;
367 lapic_timer_fixup_handler(NULL);
371 lapic_timer_fixup_handler(void *arg)
378 if (cpu_vendor_id == CPU_VENDOR_AMD) {
380 * Detect the presence of C1E capability mostly on latest
381 * dual-cores (or future) k8 family. This feature renders
382 * the local APIC timer dead, so we disable it by reading
383 * the Interrupt Pending Message register and clearing both
384 * C1eOnCmpHalt (bit 28) and SmiOnCmpHalt (bit 27).
387 * "BIOS and Kernel Developer's Guide for AMD NPT
388 * Family 0Fh Processors"
389 * #32559 revision 3.00
391 if ((cpu_id & 0x00000f00) == 0x00000f00 &&
392 (cpu_id & 0x0fff0000) >= 0x00040000) {
395 msr = rdmsr(0xc0010055);
396 if (msr & 0x18000000) {
397 struct globaldata *gd = mycpu;
399 kprintf("cpu%d: AMD C1E detected\n",
401 wrmsr(0xc0010055, msr & ~0x18000000ULL);
404 * We are kinda stalled;
407 gd->gd_timer_running = 1;
408 lapic_timer_oneshot_quick(2);
418 lapic_timer_restart_handler(void *dummy __unused)
422 lapic_timer_fixup_handler(&started);
424 struct globaldata *gd = mycpu;
426 gd->gd_timer_running = 1;
427 lapic_timer_oneshot_quick(2);
432 * This function is called only by ACPI-CA code currently:
433 * - AMD C1E fixup. AMD C1E only seems to happen after ACPI
434 * module controls PM. So once ACPI-CA is attached, we try
435 * to apply the fixup to prevent LAPIC timer from hanging.
438 lapic_timer_intr_pmfixup(struct cputimer_intr *cti __unused)
440 lwkt_send_ipiq_mask(smp_active_mask,
441 lapic_timer_fixup_handler, NULL);
445 lapic_timer_intr_restart(struct cputimer_intr *cti __unused)
447 lwkt_send_ipiq_mask(smp_active_mask, lapic_timer_restart_handler, NULL);
452 * dump contents of local APIC registers
457 kprintf("SMP: CPU%d %s:\n", mycpu->gd_cpuid, str);
458 kprintf(" lint0: 0x%08x lint1: 0x%08x TPR: 0x%08x SVR: 0x%08x\n",
459 lapic.lvt_lint0, lapic.lvt_lint1, lapic.tpr, lapic.svr);
463 #ifdef SMP /* APIC-IO */
469 #define IOAPIC_ISA_INTS 16
470 #define REDIRCNT_IOAPIC(A) \
471 ((int)((io_apic_versions[(A)] & IOART_VER_MAXREDIR) >> MAXREDIRSHIFT) + 1)
473 static int trigger (int apic, int pin, u_int32_t * flags);
474 static void polarity (int apic, int pin, u_int32_t * flags, int level);
476 #define DEFAULT_FLAGS \
482 #define DEFAULT_ISA_FLAGS \
491 io_apic_set_id(int apic, int id)
495 ux = ioapic_read(ioapic[apic], IOAPIC_ID); /* get current contents */
496 if (((ux & APIC_ID_MASK) >> 24) != id) {
497 kprintf("Changing APIC ID for IO APIC #%d"
498 " from %d to %d on chip\n",
499 apic, ((ux & APIC_ID_MASK) >> 24), id);
500 ux &= ~APIC_ID_MASK; /* clear the ID field */
502 ioapic_write(ioapic[apic], IOAPIC_ID, ux); /* write new value */
503 ux = ioapic_read(ioapic[apic], IOAPIC_ID); /* re-read && test */
504 if (((ux & APIC_ID_MASK) >> 24) != id)
505 panic("can't control IO APIC #%d ID, reg: 0x%08x",
512 io_apic_get_id(int apic)
514 return (ioapic_read(ioapic[apic], IOAPIC_ID) & APIC_ID_MASK) >> 24;
523 io_apic_setup_intpin(int apic, int pin)
525 int bus, bustype, irq;
526 u_char select; /* the select register is 8 bits */
527 u_int32_t flags; /* the window register is 32 bits */
528 u_int32_t target; /* the window register is 32 bits */
529 u_int32_t vector; /* the window register is 32 bits */
534 select = pin * 2 + IOAPIC_REDTBL0; /* register */
537 * Always clear an IO APIC pin before [re]programming it. This is
538 * particularly important if the pin is set up for a level interrupt
539 * as the IOART_REM_IRR bit might be set. When we reprogram the
540 * vector any EOI from pending ints on this pin could be lost and
541 * IRR might never get reset.
543 * To fix this problem, clear the vector and make sure it is
544 * programmed as an edge interrupt. This should theoretically
545 * clear IRR so we can later, safely program it as a level
550 flags = ioapic_read(ioapic[apic], select) & IOART_RESV;
551 flags |= IOART_INTMSET | IOART_TRGREDG | IOART_INTAHI;
552 flags |= IOART_DESTPHY | IOART_DELFIXED;
554 target = ioapic_read(ioapic[apic], select + 1) & IOART_HI_DEST_RESV;
555 target |= 0; /* fixed mode cpu mask of 0 - don't deliver anywhere */
559 ioapic_write(ioapic[apic], select, flags | vector);
560 ioapic_write(ioapic[apic], select + 1, target);
565 * We only deal with vectored interrupts here. ? documentation is
566 * lacking, I'm guessing an interrupt type of 0 is the 'INT' type,
569 * This test also catches unconfigured pins.
571 if (apic_int_type(apic, pin) != 0)
575 * Leave the pin unprogrammed if it does not correspond to
578 irq = apic_irq(apic, pin);
582 /* determine the bus type for this pin */
583 bus = apic_src_bus_id(apic, pin);
586 bustype = apic_bus_type(bus);
588 if ((bustype == ISA) &&
589 (pin < IOAPIC_ISA_INTS) &&
591 (apic_polarity(apic, pin) == 0x1) &&
592 (apic_trigger(apic, pin) == 0x3)) {
594 * A broken BIOS might describe some ISA
595 * interrupts as active-high level-triggered.
596 * Use default ISA flags for those interrupts.
598 flags = DEFAULT_ISA_FLAGS;
601 * Program polarity and trigger mode according to
604 flags = DEFAULT_FLAGS;
605 level = trigger(apic, pin, &flags);
607 int_to_apicintpin[irq].flags |= IOAPIC_IM_FLAG_LEVEL;
608 polarity(apic, pin, &flags, level);
612 ksnprintf(envpath, sizeof(envpath), "hw.irq.%d.dest", irq);
613 kgetenv_int(envpath, &cpuid);
615 /* ncpus may not be available yet */
620 kprintf("IOAPIC #%d intpin %d -> irq %d (CPU%d)\n",
621 apic, pin, irq, cpuid);
625 * Program the appropriate registers. This routing may be
626 * overridden when an interrupt handler for a device is
627 * actually added (see register_int(), which calls through
628 * the MACHINTR ABI to set up an interrupt handler/vector).
630 * The order in which we must program the two registers for
631 * safety is unclear! XXX
635 vector = IDT_OFFSET + irq; /* IDT vec */
636 target = ioapic_read(ioapic[apic], select + 1) & IOART_HI_DEST_RESV;
637 /* Deliver all interrupts to CPU0 (BSP) */
638 target |= (CPU_TO_ID(cpuid) << IOART_HI_DEST_SHIFT) &
640 flags |= ioapic_read(ioapic[apic], select) & IOART_RESV;
641 ioapic_write(ioapic[apic], select, flags | vector);
642 ioapic_write(ioapic[apic], select + 1, target);
648 io_apic_setup(int apic)
653 maxpin = REDIRCNT_IOAPIC(apic); /* pins in APIC */
654 kprintf("Programming %d pins in IOAPIC #%d\n", maxpin, apic);
656 for (pin = 0; pin < maxpin; ++pin) {
657 io_apic_setup_intpin(apic, pin);
660 if (apic_int_type(apic, pin) >= 0) {
661 kprintf("Warning: IOAPIC #%d pin %d does not exist,"
662 " cannot program!\n", apic, pin);
667 /* return GOOD status */
670 #undef DEFAULT_ISA_FLAGS
674 #define DEFAULT_EXTINT_FLAGS \
683 * XXX this function is only used by 8254 setup
684 * Setup the source of External INTerrupts.
687 ext_int_setup(int apic, int intr)
689 u_char select; /* the select register is 8 bits */
690 u_int32_t flags; /* the window register is 32 bits */
691 u_int32_t target; /* the window register is 32 bits */
692 u_int32_t vector; /* the window register is 32 bits */
696 if (apic_int_type(apic, intr) != 3)
700 ksnprintf(envpath, sizeof(envpath), "hw.irq.%d.dest", intr);
701 kgetenv_int(envpath, &cpuid);
703 /* ncpus may not be available yet */
707 /* Deliver interrupts to CPU0 (BSP) */
708 target = (CPU_TO_ID(cpuid) << IOART_HI_DEST_SHIFT) &
710 select = IOAPIC_REDTBL0 + (2 * intr);
711 vector = IDT_OFFSET + intr;
712 flags = DEFAULT_EXTINT_FLAGS;
714 ioapic_write(ioapic[apic], select, flags | vector);
715 ioapic_write(ioapic[apic], select + 1, target);
719 #undef DEFAULT_EXTINT_FLAGS
723 * Set the trigger level for an IO APIC pin.
726 trigger(int apic, int pin, u_int32_t * flags)
731 static int intcontrol = -1;
733 switch (apic_trigger(apic, pin)) {
739 *flags &= ~IOART_TRGRLVL; /* *flags |= IOART_TRGREDG */
743 *flags |= IOART_TRGRLVL;
751 if ((id = apic_src_bus_id(apic, pin)) == -1)
754 switch (apic_bus_type(id)) {
756 *flags &= ~IOART_TRGRLVL; /* *flags |= IOART_TRGREDG; */
760 eirq = apic_src_bus_irq(apic, pin);
762 if (eirq < 0 || eirq > 15) {
763 kprintf("EISA IRQ %d?!?!\n", eirq);
767 if (intcontrol == -1) {
768 intcontrol = inb(ELCR1) << 8;
769 intcontrol |= inb(ELCR0);
770 kprintf("EISA INTCONTROL = %08x\n", intcontrol);
773 /* Use ELCR settings to determine level or edge mode */
774 level = (intcontrol >> eirq) & 1;
777 * Note that on older Neptune chipset based systems, any
778 * pci interrupts often show up here and in the ELCR as well
779 * as level sensitive interrupts attributed to the EISA bus.
783 *flags |= IOART_TRGRLVL;
785 *flags &= ~IOART_TRGRLVL;
790 *flags |= IOART_TRGRLVL;
799 panic("bad APIC IO INT flags");
804 * Set the polarity value for an IO APIC pin.
807 polarity(int apic, int pin, u_int32_t * flags, int level)
811 switch (apic_polarity(apic, pin)) {
817 *flags &= ~IOART_INTALO; /* *flags |= IOART_INTAHI */
821 *flags |= IOART_INTALO;
829 if ((id = apic_src_bus_id(apic, pin)) == -1)
832 switch (apic_bus_type(id)) {
834 *flags &= ~IOART_INTALO; /* *flags |= IOART_INTAHI */
838 /* polarity converter always gives active high */
839 *flags &= ~IOART_INTALO;
843 *flags |= IOART_INTALO;
852 panic("bad APIC IO INT flags");
857 * Print contents of unmasked IRQs.
864 kprintf("SMP: enabled INTs: ");
865 for (x = 0; x < APIC_INTMAPSIZE; ++x) {
866 if ((int_to_apicintpin[x].flags & IOAPIC_IM_FLAG_MASKED) == 0)
874 * Inter Processor Interrupt functions.
877 #endif /* SMP APIC-IO */
880 * Send APIC IPI 'vector' to 'destType' via 'deliveryMode'.
882 * destType is 1 of: APIC_DEST_SELF, APIC_DEST_ALLISELF, APIC_DEST_ALLESELF
883 * vector is any valid SYSTEM INT vector
884 * delivery_mode is 1 of: APIC_DELMODE_FIXED, APIC_DELMODE_LOWPRIO
886 * A backlog of requests can create a deadlock between cpus. To avoid this
887 * we have to be able to accept IPIs at the same time we are trying to send
888 * them. The critical section prevents us from attempting to send additional
889 * IPIs reentrantly, but also prevents IPIQ processing so we have to call
890 * lwkt_process_ipiq() manually. It's rather messy and expensive for this
891 * to occur but fortunately it does not happen too often.
894 apic_ipi(int dest_type, int vector, int delivery_mode)
899 if ((lapic.icr_lo & APIC_DELSTAT_MASK) != 0) {
900 unsigned int eflags = read_eflags();
902 DEBUG_PUSH_INFO("apic_ipi");
903 while ((lapic.icr_lo & APIC_DELSTAT_MASK) != 0) {
907 write_eflags(eflags);
910 icr_lo = (lapic.icr_lo & APIC_ICRLO_RESV_MASK) | dest_type |
911 delivery_mode | vector;
912 lapic.icr_lo = icr_lo;
918 single_apic_ipi(int cpu, int vector, int delivery_mode)
924 if ((lapic.icr_lo & APIC_DELSTAT_MASK) != 0) {
925 unsigned int eflags = read_eflags();
927 DEBUG_PUSH_INFO("single_apic_ipi");
928 while ((lapic.icr_lo & APIC_DELSTAT_MASK) != 0) {
932 write_eflags(eflags);
934 icr_hi = lapic.icr_hi & ~APIC_ID_MASK;
935 icr_hi |= (CPU_TO_ID(cpu) << 24);
936 lapic.icr_hi = icr_hi;
939 icr_lo = (lapic.icr_lo & APIC_ICRLO_RESV_MASK)
940 | APIC_DEST_DESTFLD | delivery_mode | vector;
943 lapic.icr_lo = icr_lo;
950 * Returns 0 if the apic is busy, 1 if we were able to queue the request.
952 * NOT WORKING YET! The code as-is may end up not queueing an IPI at all
953 * to the target, and the scheduler does not 'poll' for IPI messages.
956 single_apic_ipi_passive(int cpu, int vector, int delivery_mode)
962 if ((lapic.icr_lo & APIC_DELSTAT_MASK) != 0) {
966 icr_hi = lapic.icr_hi & ~APIC_ID_MASK;
967 icr_hi |= (CPU_TO_ID(cpu) << 24);
968 lapic.icr_hi = icr_hi;
971 icr_lo = (lapic.icr_lo & APIC_RESV2_MASK)
972 | APIC_DEST_DESTFLD | delivery_mode | vector;
975 lapic.icr_lo = icr_lo;
983 * Send APIC IPI 'vector' to 'target's via 'delivery_mode'.
985 * target is a bitmask of destination cpus. Vector is any
986 * valid system INT vector. Delivery mode may be either
987 * APIC_DELMODE_FIXED or APIC_DELMODE_LOWPRIO.
990 selected_apic_ipi(cpumask_t target, int vector, int delivery_mode)
994 int n = BSFCPUMASK(target);
995 target &= ~CPUMASK(n);
996 single_apic_ipi(n, vector, delivery_mode);
1002 * Timer code, in development...
1003 * - suggested by rgrimes@gndrsh.aac.dev.com
1006 get_apic_timer_frequency(void)
1008 return(lapic_cputimer_intr.freq);
1012 * Load a 'downcount time' in uSeconds.
1015 set_apic_timer(int us)
1020 * When we reach here, lapic timer's frequency
1021 * must have been calculated as well as the
1022 * divisor (lapic.dcr_timer is setup during the
1023 * divisor calculation).
1025 KKASSERT(lapic_cputimer_intr.freq != 0 &&
1026 lapic_timer_divisor_idx >= 0);
1028 count = ((us * (int64_t)lapic_cputimer_intr.freq) + 999999) / 1000000;
1029 lapic_timer_oneshot(count);
1034 * Read remaining time in timer.
1037 read_apic_timer(void)
1040 /** XXX FIXME: we need to return the actual remaining time,
1041 * for now we just return the remaining count.
1044 return lapic.ccr_timer;
1050 * Spin-style delay, set delay time in uS, spin till it drains.
1055 set_apic_timer(count);
1056 while (read_apic_timer())
1061 lapic_map(vm_offset_t lapic_addr)
1063 /* Local apic is mapped on last page */
1064 SMPpt[NPTEPG - 1] = (pt_entry_t)(PG_V | PG_RW | PG_N |
1065 pmap_get_pgeflag() | (lapic_addr & PG_FRAME));
1067 kprintf("lapic: at %p\n", (void *)lapic_addr);
1070 static TAILQ_HEAD(, lapic_enumerator) lapic_enumerators =
1071 TAILQ_HEAD_INITIALIZER(lapic_enumerators);
1076 struct lapic_enumerator *e;
1079 TAILQ_FOREACH(e, &lapic_enumerators, lapic_link) {
1080 error = e->lapic_probe(e);
1085 panic("can't config lapic\n");
1087 e->lapic_enumerate(e);
1091 lapic_enumerator_register(struct lapic_enumerator *ne)
1093 struct lapic_enumerator *e;
1095 TAILQ_FOREACH(e, &lapic_enumerators, lapic_link) {
1096 if (e->lapic_prio < ne->lapic_prio) {
1097 TAILQ_INSERT_BEFORE(e, ne, lapic_link);
1101 TAILQ_INSERT_TAIL(&lapic_enumerators, ne, lapic_link);
1104 static TAILQ_HEAD(, ioapic_enumerator) ioapic_enumerators =
1105 TAILQ_HEAD_INITIALIZER(ioapic_enumerators);
1110 struct ioapic_enumerator *e;
1113 TAILQ_INIT(&ioapic_conf.ioc_list);
1114 /* XXX magic number */
1115 for (i = 0; i < 16; ++i)
1116 ioapic_conf.ioc_intsrc[i] = -1;
1118 TAILQ_FOREACH(e, &ioapic_enumerators, ioapic_link) {
1119 error = e->ioapic_probe(e);
1125 panic("can't config I/O APIC\n");
1127 kprintf("no I/O APIC\n");
1132 e->ioapic_enumerate(e);
1134 if (!ioapic_use_old) {
1135 struct ioapic_info *info;
1138 * Fixup the rest of the fields of ioapic_info
1141 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link) {
1142 const struct ioapic_info *prev_info;
1145 info->io_apic_id = info->io_idx + lapic_id_max + 1;
1148 kprintf("IOAPIC: idx %d, apic id %d, "
1149 "gsi base %d, npin %d\n",
1156 /* Warning about possible GSI hole */
1157 prev_info = TAILQ_PREV(info, ioapic_info_list, io_link);
1158 if (prev_info != NULL) {
1159 if (info->io_gsi_base !=
1160 prev_info->io_gsi_base + prev_info->io_npin) {
1161 kprintf("IOAPIC: warning gsi hole "
1163 prev_info->io_gsi_base +
1165 info->io_gsi_base - 1);
1171 * Setup all I/O APIC
1173 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link)
1176 panic("ioapic_config: new ioapic not working yet\n");
1181 ioapic_enumerator_register(struct ioapic_enumerator *ne)
1183 struct ioapic_enumerator *e;
1185 TAILQ_FOREACH(e, &ioapic_enumerators, ioapic_link) {
1186 if (e->ioapic_prio < ne->ioapic_prio) {
1187 TAILQ_INSERT_BEFORE(e, ne, ioapic_link);
1191 TAILQ_INSERT_TAIL(&ioapic_enumerators, ne, ioapic_link);
1195 ioapic_add(void *addr, int gsi_base, int npin)
1197 struct ioapic_info *info, *ninfo;
1200 gsi_end = gsi_base + npin - 1;
1201 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link) {
1202 if ((gsi_base >= info->io_gsi_base &&
1203 gsi_base < info->io_gsi_base + info->io_npin) ||
1204 (gsi_end >= info->io_gsi_base &&
1205 gsi_end < info->io_gsi_base + info->io_npin)) {
1206 panic("ioapic_add: overlapped gsi, base %d npin %d, "
1207 "hit base %d, npin %d\n", gsi_base, npin,
1208 info->io_gsi_base, info->io_npin);
1210 if (info->io_addr == addr)
1211 panic("ioapic_add: duplicated addr %p\n", addr);
1214 ninfo = kmalloc(sizeof(*ninfo), M_DEVBUF, M_WAITOK | M_ZERO);
1215 ninfo->io_addr = addr;
1216 ninfo->io_npin = npin;
1217 ninfo->io_gsi_base = gsi_base;
1220 * Create IOAPIC list in ascending order of GSI base
1222 TAILQ_FOREACH_REVERSE(info, &ioapic_conf.ioc_list,
1223 ioapic_info_list, io_link) {
1224 if (ninfo->io_gsi_base > info->io_gsi_base) {
1225 TAILQ_INSERT_AFTER(&ioapic_conf.ioc_list,
1226 info, ninfo, io_link);
1231 TAILQ_INSERT_HEAD(&ioapic_conf.ioc_list, ninfo, io_link);
1235 ioapic_intsrc(int irq, int gsi)
1238 if (ioapic_conf.ioc_intsrc[irq] != -1 &&
1239 ioapic_conf.ioc_intsrc[irq] != gsi) {
1240 kprintf("IOAPIC: warning intsrc irq %d, gsi %d -> gsi %d\n",
1241 irq, ioapic_conf.ioc_intsrc[irq], gsi);
1243 ioapic_conf.ioc_intsrc[irq] = gsi;
1247 ioapic_set_apic_id(const struct ioapic_info *info)
1251 id = ioapic_read(info->io_addr, IOAPIC_ID);
1253 id &= ~APIC_ID_MASK;
1254 id |= (info->io_apic_id << 24);
1256 ioapic_write(info->io_addr, IOAPIC_ID, id);
1261 id = ioapic_read(info->io_addr, IOAPIC_ID);
1262 if (((id & APIC_ID_MASK) >> 24) != info->io_apic_id) {
1263 panic("ioapic_set_apic_id: can't set apic id to %d\n",
1269 ioapic_gsi_setup(int gsi)
1271 enum intr_trigger trig;
1272 enum intr_polarity pola;
1277 ioapic_extpin_setup(ioapic_gsi_ioaddr(gsi),
1278 ioapic_gsi_pin(gsi), 0);
1282 for (irq = 0; irq < 16; ++irq) {
1283 if (gsi == ioapic_conf.ioc_intsrc[irq]) {
1284 trig = INTR_TRIGGER_EDGE;
1285 pola = INTR_POLARITY_HIGH;
1292 trig = INTR_TRIGGER_EDGE;
1293 pola = INTR_POLARITY_HIGH;
1295 trig = INTR_TRIGGER_LEVEL;
1296 pola = INTR_POLARITY_LOW;
1301 ioapic_abi_set_irqmap(irq, gsi, trig, pola);
1305 ioapic_gsi_ioaddr(int gsi)
1307 const struct ioapic_info *info;
1309 info = ioapic_gsi_search(gsi);
1310 return info->io_addr;
1314 ioapic_gsi_pin(int gsi)
1316 const struct ioapic_info *info;
1318 info = ioapic_gsi_search(gsi);
1319 return gsi - info->io_gsi_base;
1322 static const struct ioapic_info *
1323 ioapic_gsi_search(int gsi)
1325 const struct ioapic_info *info;
1327 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link) {
1328 if (gsi >= info->io_gsi_base &&
1329 gsi < info->io_gsi_base + info->io_npin)
1332 panic("ioapic_gsi_search: no I/O APIC\n");
1336 ioapic_extpin_setup(void *addr, int pin, int vec)
1339 ioapic_pin_prog(addr, pin, vec,
1340 INTR_TRIGGER_CONFORM, INTR_POLARITY_CONFORM, IOART_DELEXINT);
1345 ioapic_pin_setup(void *addr, int pin, int vec,
1346 enum intr_trigger trig, enum intr_polarity pola)
1349 * Always clear an I/O APIC pin before [re]programming it. This is
1350 * particularly important if the pin is set up for a level interrupt
1351 * as the IOART_REM_IRR bit might be set. When we reprogram the
1352 * vector any EOI from pending ints on this pin could be lost and
1353 * IRR might never get reset.
1355 * To fix this problem, clear the vector and make sure it is
1356 * programmed as an edge interrupt. This should theoretically
1357 * clear IRR so we can later, safely program it as a level
1362 ioapic_pin_prog(addr, pin, vec, INTR_TRIGGER_EDGE, INTR_POLARITY_HIGH,
1364 ioapic_pin_prog(addr, pin, vec, trig, pola, IOART_DELFIXED);
1370 ioapic_pin_prog(void *addr, int pin, int vec,
1371 enum intr_trigger trig, enum intr_polarity pola, uint32_t del_mode)
1373 uint32_t flags, target;
1376 KKASSERT(del_mode == IOART_DELEXINT || del_mode == IOART_DELFIXED);
1378 select = IOAPIC_REDTBL0 + (2 * pin);
1380 flags = ioapic_read(addr, select) & IOART_RESV;
1381 flags |= IOART_INTMSET | IOART_DESTPHY | del_mode;
1383 if (del_mode == IOART_DELEXINT) {
1384 KKASSERT(trig == INTR_TRIGGER_CONFORM &&
1385 pola == INTR_POLARITY_CONFORM);
1386 flags |= IOART_TRGREDG | IOART_INTAHI;
1389 case INTR_TRIGGER_EDGE:
1390 flags |= IOART_TRGREDG;
1393 case INTR_TRIGGER_LEVEL:
1394 flags |= IOART_TRGRLVL;
1397 case INTR_TRIGGER_CONFORM:
1398 panic("ioapic_pin_prog: trig conform is not "
1402 case INTR_POLARITY_HIGH:
1403 flags |= IOART_INTAHI;
1406 case INTR_POLARITY_LOW:
1407 flags |= IOART_INTALO;
1410 case INTR_POLARITY_CONFORM:
1411 panic("ioapic_pin_prog: pola conform is not "
1416 target = ioapic_read(addr, select + 1) & IOART_HI_DEST_RESV;
1419 ioapic_write(addr, select, flags | vec);
1420 ioapic_write(addr, select + 1, target);
1424 ioapic_setup(const struct ioapic_info *info)
1428 ioapic_set_apic_id(info);
1430 for (i = 0; i < info->io_npin; ++i)
1431 ioapic_gsi_setup(info->io_gsi_base + i);