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/segments.h>
39 #include <sys/thread2.h>
41 #include <machine/intr_machdep.h>
44 extern pt_entry_t *SMPpt;
46 /* EISA Edge/Level trigger control registers */
47 #define ELCR0 0x4d0 /* eisa irq 0-7 */
48 #define ELCR1 0x4d1 /* eisa irq 8-15 */
57 TAILQ_ENTRY(ioapic_info) io_link;
59 TAILQ_HEAD(ioapic_info_list, ioapic_info);
62 struct ioapic_info_list ioc_list;
63 int ioc_intsrc[16]; /* XXX magic number */
66 static void lapic_timer_calibrate(void);
67 static void lapic_timer_set_divisor(int);
68 static void lapic_timer_fixup_handler(void *);
69 static void lapic_timer_restart_handler(void *);
71 void lapic_timer_process(void);
72 void lapic_timer_process_frame(struct intrframe *);
74 static int lapic_timer_enable = 1;
75 TUNABLE_INT("hw.lapic_timer_enable", &lapic_timer_enable);
77 static void lapic_timer_intr_reload(struct cputimer_intr *, sysclock_t);
78 static void lapic_timer_intr_enable(struct cputimer_intr *);
79 static void lapic_timer_intr_restart(struct cputimer_intr *);
80 static void lapic_timer_intr_pmfixup(struct cputimer_intr *);
82 static void ioapic_setup(const struct ioapic_info *);
83 static void ioapic_set_apic_id(const struct ioapic_info *);
84 static void ioapic_gsi_setup(int);
85 static const struct ioapic_info *
86 ioapic_gsi_search(int);
88 static struct cputimer_intr lapic_cputimer_intr = {
90 .reload = lapic_timer_intr_reload,
91 .enable = lapic_timer_intr_enable,
92 .config = cputimer_intr_default_config,
93 .restart = lapic_timer_intr_restart,
94 .pmfixup = lapic_timer_intr_pmfixup,
95 .initclock = cputimer_intr_default_initclock,
96 .next = SLIST_ENTRY_INITIALIZER,
98 .type = CPUTIMER_INTR_LAPIC,
99 .prio = CPUTIMER_INTR_PRIO_LAPIC,
100 .caps = CPUTIMER_INTR_CAP_NONE
104 * pointers to pmapped apic hardware.
107 volatile ioapic_t **ioapic;
109 static int lapic_timer_divisor_idx = -1;
110 static const uint32_t lapic_timer_divisors[] = {
111 APIC_TDCR_2, APIC_TDCR_4, APIC_TDCR_8, APIC_TDCR_16,
112 APIC_TDCR_32, APIC_TDCR_64, APIC_TDCR_128, APIC_TDCR_1
114 #define APIC_TIMER_NDIVISORS (int)(NELEM(lapic_timer_divisors))
118 static struct ioapic_conf ioapic_conf;
121 * Enable LAPIC, configure interrupts.
124 apic_initialize(boolean_t bsp)
130 * Setup LINT0 as ExtINT on the BSP. This is theoretically an
131 * aggregate interrupt input from the 8259. The INTA cycle
132 * will be routed to the external controller (the 8259) which
133 * is expected to supply the vector.
135 * Must be setup edge triggered, active high.
137 * Disable LINT0 on the APs. It doesn't matter what delivery
138 * mode we use because we leave it masked.
140 temp = lapic.lvt_lint0;
141 temp &= ~(APIC_LVT_MASKED | APIC_LVT_TRIG_MASK |
142 APIC_LVT_POLARITY_MASK | APIC_LVT_DM_MASK);
143 if (mycpu->gd_cpuid == 0)
144 temp |= APIC_LVT_DM_EXTINT;
146 temp |= APIC_LVT_DM_FIXED | APIC_LVT_MASKED;
147 lapic.lvt_lint0 = temp;
150 * Setup LINT1 as NMI, masked till later.
151 * Edge trigger, active high.
153 temp = lapic.lvt_lint1;
154 temp &= ~(APIC_LVT_MASKED | APIC_LVT_TRIG_MASK |
155 APIC_LVT_POLARITY_MASK | APIC_LVT_DM_MASK);
156 temp |= APIC_LVT_MASKED | APIC_LVT_DM_NMI;
157 lapic.lvt_lint1 = temp;
160 * Mask the LAPIC error interrupt, LAPIC performance counter
163 lapic.lvt_error = lapic.lvt_error | APIC_LVT_MASKED;
164 lapic.lvt_pcint = lapic.lvt_pcint | APIC_LVT_MASKED;
167 * Set LAPIC timer vector and mask the LAPIC timer interrupt.
169 timer = lapic.lvt_timer;
170 timer &= ~APIC_LVTT_VECTOR;
171 timer |= XTIMER_OFFSET;
172 timer |= APIC_LVTT_MASKED;
173 lapic.lvt_timer = timer;
176 * Set the Task Priority Register as needed. At the moment allow
177 * interrupts on all cpus (the APs will remain CLId until they are
178 * ready to deal). We could disable all but IPIs by setting
179 * temp |= TPR_IPI for cpu != 0.
182 temp &= ~APIC_TPR_PRIO; /* clear priority field */
183 #ifdef SMP /* APIC-IO */
184 if (!apic_io_enable) {
187 * If we are NOT running the IO APICs, the LAPIC will only be used
188 * for IPIs. Set the TPR to prevent any unintentional interrupts.
191 #ifdef SMP /* APIC-IO */
201 temp |= APIC_SVR_ENABLE; /* enable the LAPIC */
202 temp &= ~APIC_SVR_FOCUS_DISABLE; /* enable lopri focus processor */
205 * Set the spurious interrupt vector. The low 4 bits of the vector
208 if ((XSPURIOUSINT_OFFSET & 0x0F) != 0x0F)
209 panic("bad XSPURIOUSINT_OFFSET: 0x%08x", XSPURIOUSINT_OFFSET);
210 temp &= ~APIC_SVR_VECTOR;
211 temp |= XSPURIOUSINT_OFFSET;
216 * Pump out a few EOIs to clean out interrupts that got through
217 * before we were able to set the TPR.
224 lapic_timer_calibrate();
225 if (lapic_timer_enable) {
226 cputimer_intr_register(&lapic_cputimer_intr);
227 cputimer_intr_select(&lapic_cputimer_intr, 0);
230 lapic_timer_set_divisor(lapic_timer_divisor_idx);
234 apic_dump("apic_initialize()");
238 lapic_timer_set_divisor(int divisor_idx)
240 KKASSERT(divisor_idx >= 0 && divisor_idx < APIC_TIMER_NDIVISORS);
241 lapic.dcr_timer = lapic_timer_divisors[divisor_idx];
245 lapic_timer_oneshot(u_int count)
249 value = lapic.lvt_timer;
250 value &= ~APIC_LVTT_PERIODIC;
251 lapic.lvt_timer = value;
252 lapic.icr_timer = count;
256 lapic_timer_oneshot_quick(u_int count)
258 lapic.icr_timer = count;
262 lapic_timer_calibrate(void)
266 /* Try to calibrate the local APIC timer. */
267 for (lapic_timer_divisor_idx = 0;
268 lapic_timer_divisor_idx < APIC_TIMER_NDIVISORS;
269 lapic_timer_divisor_idx++) {
270 lapic_timer_set_divisor(lapic_timer_divisor_idx);
271 lapic_timer_oneshot(APIC_TIMER_MAX_COUNT);
273 value = APIC_TIMER_MAX_COUNT - lapic.ccr_timer;
274 if (value != APIC_TIMER_MAX_COUNT)
277 if (lapic_timer_divisor_idx >= APIC_TIMER_NDIVISORS)
278 panic("lapic: no proper timer divisor?!\n");
279 lapic_cputimer_intr.freq = value / 2;
281 kprintf("lapic: divisor index %d, frequency %u Hz\n",
282 lapic_timer_divisor_idx, lapic_cputimer_intr.freq);
286 lapic_timer_process_oncpu(struct globaldata *gd, struct intrframe *frame)
290 gd->gd_timer_running = 0;
292 count = sys_cputimer->count();
293 if (TAILQ_FIRST(&gd->gd_systimerq) != NULL)
294 systimer_intr(&count, 0, frame);
298 lapic_timer_process(void)
300 lapic_timer_process_oncpu(mycpu, NULL);
304 lapic_timer_process_frame(struct intrframe *frame)
306 lapic_timer_process_oncpu(mycpu, frame);
310 lapic_timer_intr_reload(struct cputimer_intr *cti, sysclock_t reload)
312 struct globaldata *gd = mycpu;
314 reload = (int64_t)reload * cti->freq / sys_cputimer->freq;
318 if (gd->gd_timer_running) {
319 if (reload < lapic.ccr_timer)
320 lapic_timer_oneshot_quick(reload);
322 gd->gd_timer_running = 1;
323 lapic_timer_oneshot_quick(reload);
328 lapic_timer_intr_enable(struct cputimer_intr *cti __unused)
332 timer = lapic.lvt_timer;
333 timer &= ~(APIC_LVTT_MASKED | APIC_LVTT_PERIODIC);
334 lapic.lvt_timer = timer;
336 lapic_timer_fixup_handler(NULL);
340 lapic_timer_fixup_handler(void *arg)
347 if (cpu_vendor_id == CPU_VENDOR_AMD) {
349 * Detect the presence of C1E capability mostly on latest
350 * dual-cores (or future) k8 family. This feature renders
351 * the local APIC timer dead, so we disable it by reading
352 * the Interrupt Pending Message register and clearing both
353 * C1eOnCmpHalt (bit 28) and SmiOnCmpHalt (bit 27).
356 * "BIOS and Kernel Developer's Guide for AMD NPT
357 * Family 0Fh Processors"
358 * #32559 revision 3.00
360 if ((cpu_id & 0x00000f00) == 0x00000f00 &&
361 (cpu_id & 0x0fff0000) >= 0x00040000) {
364 msr = rdmsr(0xc0010055);
365 if (msr & 0x18000000) {
366 struct globaldata *gd = mycpu;
368 kprintf("cpu%d: AMD C1E detected\n",
370 wrmsr(0xc0010055, msr & ~0x18000000ULL);
373 * We are kinda stalled;
376 gd->gd_timer_running = 1;
377 lapic_timer_oneshot_quick(2);
387 lapic_timer_restart_handler(void *dummy __unused)
391 lapic_timer_fixup_handler(&started);
393 struct globaldata *gd = mycpu;
395 gd->gd_timer_running = 1;
396 lapic_timer_oneshot_quick(2);
401 * This function is called only by ACPI-CA code currently:
402 * - AMD C1E fixup. AMD C1E only seems to happen after ACPI
403 * module controls PM. So once ACPI-CA is attached, we try
404 * to apply the fixup to prevent LAPIC timer from hanging.
407 lapic_timer_intr_pmfixup(struct cputimer_intr *cti __unused)
409 lwkt_send_ipiq_mask(smp_active_mask,
410 lapic_timer_fixup_handler, NULL);
414 lapic_timer_intr_restart(struct cputimer_intr *cti __unused)
416 lwkt_send_ipiq_mask(smp_active_mask, lapic_timer_restart_handler, NULL);
421 * dump contents of local APIC registers
426 kprintf("SMP: CPU%d %s:\n", mycpu->gd_cpuid, str);
427 kprintf(" lint0: 0x%08x lint1: 0x%08x TPR: 0x%08x SVR: 0x%08x\n",
428 lapic.lvt_lint0, lapic.lvt_lint1, lapic.tpr, lapic.svr);
432 #ifdef SMP /* APIC-IO */
438 #define IOAPIC_ISA_INTS 16
439 #define REDIRCNT_IOAPIC(A) \
440 ((int)((io_apic_versions[(A)] & IOART_VER_MAXREDIR) >> MAXREDIRSHIFT) + 1)
442 static int trigger (int apic, int pin, u_int32_t * flags);
443 static void polarity (int apic, int pin, u_int32_t * flags, int level);
445 #define DEFAULT_FLAGS \
451 #define DEFAULT_ISA_FLAGS \
460 io_apic_set_id(int apic, int id)
464 ux = ioapic_read(ioapic[apic], IOAPIC_ID); /* get current contents */
465 if (((ux & APIC_ID_MASK) >> 24) != id) {
466 kprintf("Changing APIC ID for IO APIC #%d"
467 " from %d to %d on chip\n",
468 apic, ((ux & APIC_ID_MASK) >> 24), id);
469 ux &= ~APIC_ID_MASK; /* clear the ID field */
471 ioapic_write(ioapic[apic], IOAPIC_ID, ux); /* write new value */
472 ux = ioapic_read(ioapic[apic], IOAPIC_ID); /* re-read && test */
473 if (((ux & APIC_ID_MASK) >> 24) != id)
474 panic("can't control IO APIC #%d ID, reg: 0x%08x",
481 io_apic_get_id(int apic)
483 return (ioapic_read(ioapic[apic], IOAPIC_ID) & APIC_ID_MASK) >> 24;
492 io_apic_setup_intpin(int apic, int pin)
494 int bus, bustype, irq;
495 u_char select; /* the select register is 8 bits */
496 u_int32_t flags; /* the window register is 32 bits */
497 u_int32_t target; /* the window register is 32 bits */
498 u_int32_t vector; /* the window register is 32 bits */
503 select = pin * 2 + IOAPIC_REDTBL0; /* register */
506 * Always clear an IO APIC pin before [re]programming it. This is
507 * particularly important if the pin is set up for a level interrupt
508 * as the IOART_REM_IRR bit might be set. When we reprogram the
509 * vector any EOI from pending ints on this pin could be lost and
510 * IRR might never get reset.
512 * To fix this problem, clear the vector and make sure it is
513 * programmed as an edge interrupt. This should theoretically
514 * clear IRR so we can later, safely program it as a level
519 flags = ioapic_read(ioapic[apic], select) & IOART_RESV;
520 flags |= IOART_INTMSET | IOART_TRGREDG | IOART_INTAHI;
521 flags |= IOART_DESTPHY | IOART_DELFIXED;
523 target = ioapic_read(ioapic[apic], select + 1) & IOART_HI_DEST_RESV;
524 target |= 0; /* fixed mode cpu mask of 0 - don't deliver anywhere */
528 ioapic_write(ioapic[apic], select, flags | vector);
529 ioapic_write(ioapic[apic], select + 1, target);
534 * We only deal with vectored interrupts here. ? documentation is
535 * lacking, I'm guessing an interrupt type of 0 is the 'INT' type,
538 * This test also catches unconfigured pins.
540 if (apic_int_type(apic, pin) != 0)
544 * Leave the pin unprogrammed if it does not correspond to
547 irq = apic_irq(apic, pin);
551 /* determine the bus type for this pin */
552 bus = apic_src_bus_id(apic, pin);
555 bustype = apic_bus_type(bus);
557 if ((bustype == ISA) &&
558 (pin < IOAPIC_ISA_INTS) &&
560 (apic_polarity(apic, pin) == 0x1) &&
561 (apic_trigger(apic, pin) == 0x3)) {
563 * A broken BIOS might describe some ISA
564 * interrupts as active-high level-triggered.
565 * Use default ISA flags for those interrupts.
567 flags = DEFAULT_ISA_FLAGS;
570 * Program polarity and trigger mode according to
573 flags = DEFAULT_FLAGS;
574 level = trigger(apic, pin, &flags);
576 int_to_apicintpin[irq].flags |= IOAPIC_IM_FLAG_LEVEL;
577 polarity(apic, pin, &flags, level);
581 ksnprintf(envpath, sizeof(envpath), "hw.irq.%d.dest", irq);
582 kgetenv_int(envpath, &cpuid);
584 /* ncpus may not be available yet */
589 kprintf("IOAPIC #%d intpin %d -> irq %d (CPU%d)\n",
590 apic, pin, irq, cpuid);
594 * Program the appropriate registers. This routing may be
595 * overridden when an interrupt handler for a device is
596 * actually added (see register_int(), which calls through
597 * the MACHINTR ABI to set up an interrupt handler/vector).
599 * The order in which we must program the two registers for
600 * safety is unclear! XXX
604 vector = IDT_OFFSET + irq; /* IDT vec */
605 target = ioapic_read(ioapic[apic], select + 1) & IOART_HI_DEST_RESV;
606 /* Deliver all interrupts to CPU0 (BSP) */
607 target |= (CPU_TO_ID(cpuid) << IOART_HI_DEST_SHIFT) &
609 flags |= ioapic_read(ioapic[apic], select) & IOART_RESV;
610 ioapic_write(ioapic[apic], select, flags | vector);
611 ioapic_write(ioapic[apic], select + 1, target);
617 io_apic_setup(int apic)
622 maxpin = REDIRCNT_IOAPIC(apic); /* pins in APIC */
623 kprintf("Programming %d pins in IOAPIC #%d\n", maxpin, apic);
625 for (pin = 0; pin < maxpin; ++pin) {
626 io_apic_setup_intpin(apic, pin);
629 if (apic_int_type(apic, pin) >= 0) {
630 kprintf("Warning: IOAPIC #%d pin %d does not exist,"
631 " cannot program!\n", apic, pin);
636 /* return GOOD status */
639 #undef DEFAULT_ISA_FLAGS
643 #define DEFAULT_EXTINT_FLAGS \
652 * XXX this function is only used by 8254 setup
653 * Setup the source of External INTerrupts.
656 ext_int_setup(int apic, int intr)
658 u_char select; /* the select register is 8 bits */
659 u_int32_t flags; /* the window register is 32 bits */
660 u_int32_t target; /* the window register is 32 bits */
661 u_int32_t vector; /* the window register is 32 bits */
665 if (apic_int_type(apic, intr) != 3)
669 ksnprintf(envpath, sizeof(envpath), "hw.irq.%d.dest", intr);
670 kgetenv_int(envpath, &cpuid);
672 /* ncpus may not be available yet */
676 /* Deliver interrupts to CPU0 (BSP) */
677 target = (CPU_TO_ID(cpuid) << IOART_HI_DEST_SHIFT) &
679 select = IOAPIC_REDTBL0 + (2 * intr);
680 vector = IDT_OFFSET + intr;
681 flags = DEFAULT_EXTINT_FLAGS;
683 ioapic_write(ioapic[apic], select, flags | vector);
684 ioapic_write(ioapic[apic], select + 1, target);
688 #undef DEFAULT_EXTINT_FLAGS
692 * Set the trigger level for an IO APIC pin.
695 trigger(int apic, int pin, u_int32_t * flags)
700 static int intcontrol = -1;
702 switch (apic_trigger(apic, pin)) {
708 *flags &= ~IOART_TRGRLVL; /* *flags |= IOART_TRGREDG */
712 *flags |= IOART_TRGRLVL;
720 if ((id = apic_src_bus_id(apic, pin)) == -1)
723 switch (apic_bus_type(id)) {
725 *flags &= ~IOART_TRGRLVL; /* *flags |= IOART_TRGREDG; */
729 eirq = apic_src_bus_irq(apic, pin);
731 if (eirq < 0 || eirq > 15) {
732 kprintf("EISA IRQ %d?!?!\n", eirq);
736 if (intcontrol == -1) {
737 intcontrol = inb(ELCR1) << 8;
738 intcontrol |= inb(ELCR0);
739 kprintf("EISA INTCONTROL = %08x\n", intcontrol);
742 /* Use ELCR settings to determine level or edge mode */
743 level = (intcontrol >> eirq) & 1;
746 * Note that on older Neptune chipset based systems, any
747 * pci interrupts often show up here and in the ELCR as well
748 * as level sensitive interrupts attributed to the EISA bus.
752 *flags |= IOART_TRGRLVL;
754 *flags &= ~IOART_TRGRLVL;
759 *flags |= IOART_TRGRLVL;
768 panic("bad APIC IO INT flags");
773 * Set the polarity value for an IO APIC pin.
776 polarity(int apic, int pin, u_int32_t * flags, int level)
780 switch (apic_polarity(apic, pin)) {
786 *flags &= ~IOART_INTALO; /* *flags |= IOART_INTAHI */
790 *flags |= IOART_INTALO;
798 if ((id = apic_src_bus_id(apic, pin)) == -1)
801 switch (apic_bus_type(id)) {
803 *flags &= ~IOART_INTALO; /* *flags |= IOART_INTAHI */
807 /* polarity converter always gives active high */
808 *flags &= ~IOART_INTALO;
812 *flags |= IOART_INTALO;
821 panic("bad APIC IO INT flags");
826 * Print contents of unmasked IRQs.
833 kprintf("SMP: enabled INTs: ");
834 for (x = 0; x < APIC_INTMAPSIZE; ++x) {
835 if ((int_to_apicintpin[x].flags & IOAPIC_IM_FLAG_MASKED) == 0)
843 * Inter Processor Interrupt functions.
846 #endif /* SMP APIC-IO */
849 * Send APIC IPI 'vector' to 'destType' via 'deliveryMode'.
851 * destType is 1 of: APIC_DEST_SELF, APIC_DEST_ALLISELF, APIC_DEST_ALLESELF
852 * vector is any valid SYSTEM INT vector
853 * delivery_mode is 1 of: APIC_DELMODE_FIXED, APIC_DELMODE_LOWPRIO
855 * A backlog of requests can create a deadlock between cpus. To avoid this
856 * we have to be able to accept IPIs at the same time we are trying to send
857 * them. The critical section prevents us from attempting to send additional
858 * IPIs reentrantly, but also prevents IPIQ processing so we have to call
859 * lwkt_process_ipiq() manually. It's rather messy and expensive for this
860 * to occur but fortunately it does not happen too often.
863 apic_ipi(int dest_type, int vector, int delivery_mode)
868 if ((lapic.icr_lo & APIC_DELSTAT_MASK) != 0) {
869 unsigned int eflags = read_eflags();
871 DEBUG_PUSH_INFO("apic_ipi");
872 while ((lapic.icr_lo & APIC_DELSTAT_MASK) != 0) {
876 write_eflags(eflags);
879 icr_lo = (lapic.icr_lo & APIC_ICRLO_RESV_MASK) | dest_type |
880 delivery_mode | vector;
881 lapic.icr_lo = icr_lo;
887 single_apic_ipi(int cpu, int vector, int delivery_mode)
893 if ((lapic.icr_lo & APIC_DELSTAT_MASK) != 0) {
894 unsigned int eflags = read_eflags();
896 DEBUG_PUSH_INFO("single_apic_ipi");
897 while ((lapic.icr_lo & APIC_DELSTAT_MASK) != 0) {
901 write_eflags(eflags);
903 icr_hi = lapic.icr_hi & ~APIC_ID_MASK;
904 icr_hi |= (CPU_TO_ID(cpu) << 24);
905 lapic.icr_hi = icr_hi;
908 icr_lo = (lapic.icr_lo & APIC_ICRLO_RESV_MASK)
909 | APIC_DEST_DESTFLD | delivery_mode | vector;
912 lapic.icr_lo = icr_lo;
919 * Returns 0 if the apic is busy, 1 if we were able to queue the request.
921 * NOT WORKING YET! The code as-is may end up not queueing an IPI at all
922 * to the target, and the scheduler does not 'poll' for IPI messages.
925 single_apic_ipi_passive(int cpu, int vector, int delivery_mode)
931 if ((lapic.icr_lo & APIC_DELSTAT_MASK) != 0) {
935 icr_hi = lapic.icr_hi & ~APIC_ID_MASK;
936 icr_hi |= (CPU_TO_ID(cpu) << 24);
937 lapic.icr_hi = icr_hi;
940 icr_lo = (lapic.icr_lo & APIC_RESV2_MASK)
941 | APIC_DEST_DESTFLD | delivery_mode | vector;
944 lapic.icr_lo = icr_lo;
952 * Send APIC IPI 'vector' to 'target's via 'delivery_mode'.
954 * target is a bitmask of destination cpus. Vector is any
955 * valid system INT vector. Delivery mode may be either
956 * APIC_DELMODE_FIXED or APIC_DELMODE_LOWPRIO.
959 selected_apic_ipi(cpumask_t target, int vector, int delivery_mode)
963 int n = BSFCPUMASK(target);
964 target &= ~CPUMASK(n);
965 single_apic_ipi(n, vector, delivery_mode);
971 * Timer code, in development...
972 * - suggested by rgrimes@gndrsh.aac.dev.com
975 get_apic_timer_frequency(void)
977 return(lapic_cputimer_intr.freq);
981 * Load a 'downcount time' in uSeconds.
984 set_apic_timer(int us)
989 * When we reach here, lapic timer's frequency
990 * must have been calculated as well as the
991 * divisor (lapic.dcr_timer is setup during the
992 * divisor calculation).
994 KKASSERT(lapic_cputimer_intr.freq != 0 &&
995 lapic_timer_divisor_idx >= 0);
997 count = ((us * (int64_t)lapic_cputimer_intr.freq) + 999999) / 1000000;
998 lapic_timer_oneshot(count);
1003 * Read remaining time in timer.
1006 read_apic_timer(void)
1009 /** XXX FIXME: we need to return the actual remaining time,
1010 * for now we just return the remaining count.
1013 return lapic.ccr_timer;
1019 * Spin-style delay, set delay time in uS, spin till it drains.
1024 set_apic_timer(count);
1025 while (read_apic_timer())
1030 lapic_map(vm_offset_t lapic_addr)
1032 /* Local apic is mapped on last page */
1033 SMPpt[NPTEPG - 1] = (pt_entry_t)(PG_V | PG_RW | PG_N |
1034 pmap_get_pgeflag() | (lapic_addr & PG_FRAME));
1036 kprintf("lapic: at %p\n", (void *)lapic_addr);
1039 static TAILQ_HEAD(, lapic_enumerator) lapic_enumerators =
1040 TAILQ_HEAD_INITIALIZER(lapic_enumerators);
1045 struct lapic_enumerator *e;
1048 TAILQ_FOREACH(e, &lapic_enumerators, lapic_link) {
1049 error = e->lapic_probe(e);
1054 panic("can't config lapic\n");
1056 e->lapic_enumerate(e);
1060 lapic_enumerator_register(struct lapic_enumerator *ne)
1062 struct lapic_enumerator *e;
1064 TAILQ_FOREACH(e, &lapic_enumerators, lapic_link) {
1065 if (e->lapic_prio < ne->lapic_prio) {
1066 TAILQ_INSERT_BEFORE(e, ne, lapic_link);
1070 TAILQ_INSERT_TAIL(&lapic_enumerators, ne, lapic_link);
1073 static TAILQ_HEAD(, ioapic_enumerator) ioapic_enumerators =
1074 TAILQ_HEAD_INITIALIZER(ioapic_enumerators);
1079 struct ioapic_enumerator *e;
1082 TAILQ_INIT(&ioapic_conf.ioc_list);
1083 /* XXX magic number */
1084 for (i = 0; i < 16; ++i)
1085 ioapic_conf.ioc_intsrc[i] = -1;
1087 TAILQ_FOREACH(e, &ioapic_enumerators, ioapic_link) {
1088 error = e->ioapic_probe(e);
1094 panic("can't config I/O APIC\n");
1096 kprintf("no I/O APIC\n");
1101 e->ioapic_enumerate(e);
1103 if (!ioapic_use_old) {
1104 struct ioapic_info *info;
1107 * Fixup the rest of the fields of ioapic_info
1110 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link) {
1111 const struct ioapic_info *prev_info;
1114 info->io_apic_id = info->io_idx + lapic_id_max + 1;
1117 kprintf("IOAPIC: idx %d, apic id %d, "
1118 "gsi base %d, npin %d\n",
1125 /* Warning about possible GSI hole */
1126 prev_info = TAILQ_PREV(info, ioapic_info_list, io_link);
1127 if (prev_info != NULL) {
1128 if (info->io_gsi_base !=
1129 prev_info->io_gsi_base + prev_info->io_npin) {
1130 kprintf("IOAPIC: warning gsi hole "
1132 prev_info->io_gsi_base +
1134 info->io_gsi_base - 1);
1140 * Setup all I/O APIC
1142 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link)
1145 panic("ioapic_config: new ioapic not working yet\n");
1150 ioapic_enumerator_register(struct ioapic_enumerator *ne)
1152 struct ioapic_enumerator *e;
1154 TAILQ_FOREACH(e, &ioapic_enumerators, ioapic_link) {
1155 if (e->ioapic_prio < ne->ioapic_prio) {
1156 TAILQ_INSERT_BEFORE(e, ne, ioapic_link);
1160 TAILQ_INSERT_TAIL(&ioapic_enumerators, ne, ioapic_link);
1164 ioapic_add(void *addr, int gsi_base, int npin)
1166 struct ioapic_info *info, *ninfo;
1169 gsi_end = gsi_base + npin - 1;
1170 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link) {
1171 if ((gsi_base >= info->io_gsi_base &&
1172 gsi_base < info->io_gsi_base + info->io_npin) ||
1173 (gsi_end >= info->io_gsi_base &&
1174 gsi_end < info->io_gsi_base + info->io_npin)) {
1175 panic("ioapic_add: overlapped gsi, base %d npin %d, "
1176 "hit base %d, npin %d\n", gsi_base, npin,
1177 info->io_gsi_base, info->io_npin);
1179 if (info->io_addr == addr)
1180 panic("ioapic_add: duplicated addr %p\n", addr);
1183 ninfo = kmalloc(sizeof(*ninfo), M_DEVBUF, M_WAITOK | M_ZERO);
1184 ninfo->io_addr = addr;
1185 ninfo->io_npin = npin;
1186 ninfo->io_gsi_base = gsi_base;
1189 * Create IOAPIC list in ascending order of GSI base
1191 TAILQ_FOREACH_REVERSE(info, &ioapic_conf.ioc_list,
1192 ioapic_info_list, io_link) {
1193 if (ninfo->io_gsi_base > info->io_gsi_base) {
1194 TAILQ_INSERT_AFTER(&ioapic_conf.ioc_list,
1195 info, ninfo, io_link);
1200 TAILQ_INSERT_HEAD(&ioapic_conf.ioc_list, ninfo, io_link);
1204 ioapic_intsrc(int irq, int gsi)
1207 if (ioapic_conf.ioc_intsrc[irq] != -1 &&
1208 ioapic_conf.ioc_intsrc[irq] != gsi) {
1209 kprintf("IOAPIC: warning intsrc irq %d, gsi %d -> gsi %d\n",
1210 irq, ioapic_conf.ioc_intsrc[irq], gsi);
1212 ioapic_conf.ioc_intsrc[irq] = gsi;
1216 ioapic_set_apic_id(const struct ioapic_info *info)
1220 id = ioapic_read(info->io_addr, IOAPIC_ID);
1222 id &= ~APIC_ID_MASK;
1223 id |= (info->io_apic_id << 24);
1225 ioapic_write(info->io_addr, IOAPIC_ID, id);
1230 id = ioapic_read(info->io_addr, IOAPIC_ID);
1231 if (((id & APIC_ID_MASK) >> 24) != info->io_apic_id) {
1232 panic("ioapic_set_apic_id: can't set apic id to %d\n",
1238 ioapic_gsi_setup(int gsi)
1240 enum intr_trigger trig;
1241 enum intr_polarity pola;
1244 for (irq = 0; irq < 16; ++irq) {
1245 if (gsi == ioapic_conf.ioc_intsrc[irq]) {
1246 trig = INTR_TRIGGER_EDGE;
1247 pola = INTR_POLARITY_HIGH;
1254 /* TODO Program EXTINT */
1256 } else if (gsi < 16) {
1257 trig = INTR_TRIGGER_EDGE;
1258 pola = INTR_POLARITY_HIGH;
1260 trig = INTR_TRIGGER_LEVEL;
1261 pola = INTR_POLARITY_LOW;
1267 ioapic_abi_set_irqmap(irq, gsi, trig, pola);
1272 ioapic_gsi_ioaddr(int gsi)
1274 const struct ioapic_info *info;
1276 info = ioapic_gsi_search(gsi);
1277 return info->io_addr;
1281 ioapic_gsi_pin(int gsi)
1283 const struct ioapic_info *info;
1285 info = ioapic_gsi_search(gsi);
1286 return gsi - info->io_gsi_base;
1289 static const struct ioapic_info *
1290 ioapic_gsi_search(int gsi)
1292 const struct ioapic_info *info;
1294 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link) {
1295 if (gsi >= info->io_gsi_base &&
1296 gsi < info->io_gsi_base + info->io_npin)
1299 panic("ioapic_gsi_search: no I/O APIC\n");
1303 ioapic_setup(const struct ioapic_info *info)
1307 ioapic_set_apic_id(info);
1309 for (i = 0; i < info->io_npin; ++i)
1310 ioapic_gsi_setup(info->io_gsi_base + i);