2 * Copyright (c) 1996, by Steve Passe
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. The name of the developer may NOT be used to endorse or promote products
11 * derived from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * $FreeBSD: src/sys/i386/i386/mpapic.c,v 1.37.2.7 2003/01/25 02:31:47 peter Exp $
28 #include <sys/param.h>
29 #include <sys/systm.h>
30 #include <sys/kernel.h>
32 #include <machine/globaldata.h>
33 #include <machine/smp.h>
34 #include <machine/md_var.h>
35 #include <machine/pmap.h>
36 #include <machine_base/apic/mpapic.h>
37 #include <machine/segments.h>
38 #include <sys/thread2.h>
40 #include <machine/intr_machdep.h>
44 /* EISA Edge/Level trigger control registers */
45 #define ELCR0 0x4d0 /* eisa irq 0-7 */
46 #define ELCR1 0x4d1 /* eisa irq 8-15 */
55 TAILQ_ENTRY(ioapic_info) io_link;
57 TAILQ_HEAD(ioapic_info_list, ioapic_info);
60 struct ioapic_info_list ioc_list;
61 int ioc_intsrc[16]; /* XXX magic number */
64 volatile lapic_t *lapic;
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 *);
73 void lapic_timer_always(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);
89 static struct cputimer_intr lapic_cputimer_intr = {
91 .reload = lapic_timer_intr_reload,
92 .enable = lapic_timer_intr_enable,
93 .config = cputimer_intr_default_config,
94 .restart = lapic_timer_intr_restart,
95 .pmfixup = lapic_timer_intr_pmfixup,
96 .initclock = cputimer_intr_default_initclock,
97 .next = SLIST_ENTRY_INITIALIZER,
99 .type = CPUTIMER_INTR_LAPIC,
100 .prio = CPUTIMER_INTR_PRIO_LAPIC,
101 .caps = CPUTIMER_INTR_CAP_NONE
105 * pointers to pmapped apic hardware.
108 volatile ioapic_t **ioapic;
110 static int lapic_timer_divisor_idx = -1;
111 static const uint32_t lapic_timer_divisors[] = {
112 APIC_TDCR_2, APIC_TDCR_4, APIC_TDCR_8, APIC_TDCR_16,
113 APIC_TDCR_32, APIC_TDCR_64, APIC_TDCR_128, APIC_TDCR_1
115 #define APIC_TIMER_NDIVISORS (int)(NELEM(lapic_timer_divisors))
118 static struct ioapic_conf ioapic_conf;
128 * Enable LAPIC, configure interrupts.
131 apic_initialize(boolean_t bsp)
137 * Setup LINT0 as ExtINT on the BSP. This is theoretically an
138 * aggregate interrupt input from the 8259. The INTA cycle
139 * will be routed to the external controller (the 8259) which
140 * is expected to supply the vector.
142 * Must be setup edge triggered, active high.
144 * Disable LINT0 on the APs. It doesn't matter what delivery
145 * mode we use because we leave it masked.
147 temp = lapic->lvt_lint0;
148 temp &= ~(APIC_LVT_MASKED | APIC_LVT_TRIG_MASK |
149 APIC_LVT_POLARITY_MASK | APIC_LVT_DM_MASK);
150 if (mycpu->gd_cpuid == 0)
151 temp |= APIC_LVT_DM_EXTINT;
153 temp |= APIC_LVT_DM_FIXED | APIC_LVT_MASKED;
154 lapic->lvt_lint0 = temp;
157 * Setup LINT1 as NMI, masked till later.
158 * Edge trigger, active high.
160 temp = lapic->lvt_lint1;
161 temp &= ~(APIC_LVT_MASKED | APIC_LVT_TRIG_MASK |
162 APIC_LVT_POLARITY_MASK | APIC_LVT_DM_MASK);
163 temp |= APIC_LVT_MASKED | APIC_LVT_DM_NMI;
164 lapic->lvt_lint1 = temp;
167 * Mask the LAPIC error interrupt, LAPIC performance counter
170 lapic->lvt_error = lapic->lvt_error | APIC_LVT_MASKED;
171 lapic->lvt_pcint = lapic->lvt_pcint | APIC_LVT_MASKED;
174 * Set LAPIC timer vector and mask the LAPIC timer interrupt.
176 timer = lapic->lvt_timer;
177 timer &= ~APIC_LVTT_VECTOR;
178 timer |= XTIMER_OFFSET;
179 timer |= APIC_LVTT_MASKED;
180 lapic->lvt_timer = timer;
183 * Set the Task Priority Register as needed. At the moment allow
184 * interrupts on all cpus (the APs will remain CLId until they are
185 * ready to deal). We could disable all but IPIs by setting
186 * temp |= TPR_IPI for cpu != 0.
189 temp &= ~APIC_TPR_PRIO; /* clear priority field */
190 #ifdef SMP /* APIC-IO */
191 if (!apic_io_enable) {
194 * If we are NOT running the IO APICs, the LAPIC will only be used
195 * for IPIs. Set the TPR to prevent any unintentional interrupts.
198 #ifdef SMP /* APIC-IO */
207 temp |= APIC_SVR_ENABLE; /* enable the LAPIC */
208 temp &= ~APIC_SVR_FOCUS_DISABLE; /* enable lopri focus processor */
211 * Set the spurious interrupt vector. The low 4 bits of the vector
214 if ((XSPURIOUSINT_OFFSET & 0x0F) != 0x0F)
215 panic("bad XSPURIOUSINT_OFFSET: 0x%08x", XSPURIOUSINT_OFFSET);
216 temp &= ~APIC_SVR_VECTOR;
217 temp |= XSPURIOUSINT_OFFSET;
222 * Pump out a few EOIs to clean out interrupts that got through
223 * before we were able to set the TPR.
230 lapic_timer_calibrate();
231 if (lapic_timer_enable) {
232 cputimer_intr_register(&lapic_cputimer_intr);
233 cputimer_intr_select(&lapic_cputimer_intr, 0);
236 lapic_timer_set_divisor(lapic_timer_divisor_idx);
240 apic_dump("apic_initialize()");
244 lapic_timer_set_divisor(int divisor_idx)
246 KKASSERT(divisor_idx >= 0 && divisor_idx < APIC_TIMER_NDIVISORS);
247 lapic->dcr_timer = lapic_timer_divisors[divisor_idx];
251 lapic_timer_oneshot(u_int count)
255 value = lapic->lvt_timer;
256 value &= ~APIC_LVTT_PERIODIC;
257 lapic->lvt_timer = value;
258 lapic->icr_timer = count;
262 lapic_timer_oneshot_quick(u_int count)
264 lapic->icr_timer = count;
268 lapic_timer_calibrate(void)
272 /* Try to calibrate the local APIC timer. */
273 for (lapic_timer_divisor_idx = 0;
274 lapic_timer_divisor_idx < APIC_TIMER_NDIVISORS;
275 lapic_timer_divisor_idx++) {
276 lapic_timer_set_divisor(lapic_timer_divisor_idx);
277 lapic_timer_oneshot(APIC_TIMER_MAX_COUNT);
279 value = APIC_TIMER_MAX_COUNT - lapic->ccr_timer;
280 if (value != APIC_TIMER_MAX_COUNT)
283 if (lapic_timer_divisor_idx >= APIC_TIMER_NDIVISORS)
284 panic("lapic: no proper timer divisor?!\n");
285 lapic_cputimer_intr.freq = value / 2;
287 kprintf("lapic: divisor index %d, frequency %u Hz\n",
288 lapic_timer_divisor_idx, lapic_cputimer_intr.freq);
292 lapic_timer_process_oncpu(struct globaldata *gd, struct intrframe *frame)
296 gd->gd_timer_running = 0;
298 count = sys_cputimer->count();
299 if (TAILQ_FIRST(&gd->gd_systimerq) != NULL)
300 systimer_intr(&count, 0, frame);
304 lapic_timer_process(void)
306 lapic_timer_process_oncpu(mycpu, NULL);
310 lapic_timer_process_frame(struct intrframe *frame)
312 lapic_timer_process_oncpu(mycpu, frame);
316 * This manual debugging code is called unconditionally from Xtimer
317 * (the lapic timer interrupt) whether the current thread is in a
318 * critical section or not) and can be useful in tracking down lockups.
320 * NOTE: MANUAL DEBUG CODE
323 static int saveticks[SMP_MAXCPU];
324 static int savecounts[SMP_MAXCPU];
328 lapic_timer_always(struct intrframe *frame)
331 globaldata_t gd = mycpu;
332 int cpu = gd->gd_cpuid;
338 gptr = (short *)0xFFFFFFFF800b8000 + 80 * cpu;
339 *gptr = ((*gptr + 1) & 0x00FF) | 0x0700;
342 ksnprintf(buf, sizeof(buf), " %p %16s %d %16s ",
343 (void *)frame->if_rip, gd->gd_curthread->td_comm, ticks,
345 for (i = 0; buf[i]; ++i) {
346 gptr[i] = 0x0700 | (unsigned char)buf[i];
350 if (saveticks[gd->gd_cpuid] != ticks) {
351 saveticks[gd->gd_cpuid] = ticks;
352 savecounts[gd->gd_cpuid] = 0;
354 ++savecounts[gd->gd_cpuid];
355 if (savecounts[gd->gd_cpuid] > 2000 && panicstr == NULL) {
356 panic("cpud %d panicing on ticks failure",
359 for (i = 0; i < ncpus; ++i) {
361 if (saveticks[i] && panicstr == NULL) {
362 delta = saveticks[i] - ticks;
363 if (delta < -10 || delta > 10) {
364 panic("cpu %d panicing on cpu %d watchdog",
374 lapic_timer_intr_reload(struct cputimer_intr *cti, sysclock_t reload)
376 struct globaldata *gd = mycpu;
378 reload = (int64_t)reload * cti->freq / sys_cputimer->freq;
382 if (gd->gd_timer_running) {
383 if (reload < lapic->ccr_timer)
384 lapic_timer_oneshot_quick(reload);
386 gd->gd_timer_running = 1;
387 lapic_timer_oneshot_quick(reload);
392 lapic_timer_intr_enable(struct cputimer_intr *cti __unused)
396 timer = lapic->lvt_timer;
397 timer &= ~(APIC_LVTT_MASKED | APIC_LVTT_PERIODIC);
398 lapic->lvt_timer = timer;
400 lapic_timer_fixup_handler(NULL);
404 lapic_timer_fixup_handler(void *arg)
411 if (strcmp(cpu_vendor, "AuthenticAMD") == 0) {
413 * Detect the presence of C1E capability mostly on latest
414 * dual-cores (or future) k8 family. This feature renders
415 * the local APIC timer dead, so we disable it by reading
416 * the Interrupt Pending Message register and clearing both
417 * C1eOnCmpHalt (bit 28) and SmiOnCmpHalt (bit 27).
420 * "BIOS and Kernel Developer's Guide for AMD NPT
421 * Family 0Fh Processors"
422 * #32559 revision 3.00
424 if ((cpu_id & 0x00000f00) == 0x00000f00 &&
425 (cpu_id & 0x0fff0000) >= 0x00040000) {
428 msr = rdmsr(0xc0010055);
429 if (msr & 0x18000000) {
430 struct globaldata *gd = mycpu;
432 kprintf("cpu%d: AMD C1E detected\n",
434 wrmsr(0xc0010055, msr & ~0x18000000ULL);
437 * We are kinda stalled;
440 gd->gd_timer_running = 1;
441 lapic_timer_oneshot_quick(2);
451 lapic_timer_restart_handler(void *dummy __unused)
455 lapic_timer_fixup_handler(&started);
457 struct globaldata *gd = mycpu;
459 gd->gd_timer_running = 1;
460 lapic_timer_oneshot_quick(2);
465 * This function is called only by ACPI-CA code currently:
466 * - AMD C1E fixup. AMD C1E only seems to happen after ACPI
467 * module controls PM. So once ACPI-CA is attached, we try
468 * to apply the fixup to prevent LAPIC timer from hanging.
471 lapic_timer_intr_pmfixup(struct cputimer_intr *cti __unused)
473 lwkt_send_ipiq_mask(smp_active_mask,
474 lapic_timer_fixup_handler, NULL);
478 lapic_timer_intr_restart(struct cputimer_intr *cti __unused)
480 lwkt_send_ipiq_mask(smp_active_mask, lapic_timer_restart_handler, NULL);
485 * dump contents of local APIC registers
490 kprintf("SMP: CPU%d %s:\n", mycpu->gd_cpuid, str);
491 kprintf(" lint0: 0x%08x lint1: 0x%08x TPR: 0x%08x SVR: 0x%08x\n",
492 lapic->lvt_lint0, lapic->lvt_lint1, lapic->tpr, lapic->svr);
496 #ifdef SMP /* APIC-IO */
502 #define IOAPIC_ISA_INTS 16
503 #define REDIRCNT_IOAPIC(A) \
504 ((int)((io_apic_versions[(A)] & IOART_VER_MAXREDIR) >> MAXREDIRSHIFT) + 1)
506 static int trigger (int apic, int pin, u_int32_t * flags);
507 static void polarity (int apic, int pin, u_int32_t * flags, int level);
509 #define DEFAULT_FLAGS \
515 #define DEFAULT_ISA_FLAGS \
524 io_apic_set_id(int apic, int id)
528 ux = ioapic_read(ioapic[apic], IOAPIC_ID); /* get current contents */
529 if (((ux & APIC_ID_MASK) >> 24) != id) {
530 kprintf("Changing APIC ID for IO APIC #%d"
531 " from %d to %d on chip\n",
532 apic, ((ux & APIC_ID_MASK) >> 24), id);
533 ux &= ~APIC_ID_MASK; /* clear the ID field */
535 ioapic_write(ioapic[apic], IOAPIC_ID, ux); /* write new value */
536 ux = ioapic_read(ioapic[apic], IOAPIC_ID); /* re-read && test */
537 if (((ux & APIC_ID_MASK) >> 24) != id)
538 panic("can't control IO APIC #%d ID, reg: 0x%08x",
545 io_apic_get_id(int apic)
547 return (ioapic_read(ioapic[apic], IOAPIC_ID) & APIC_ID_MASK) >> 24;
556 io_apic_setup_intpin(int apic, int pin)
558 int bus, bustype, irq;
559 u_char select; /* the select register is 8 bits */
560 u_int32_t flags; /* the window register is 32 bits */
561 u_int32_t target; /* the window register is 32 bits */
562 u_int32_t vector; /* the window register is 32 bits */
567 select = pin * 2 + IOAPIC_REDTBL0; /* register */
570 * Always clear an IO APIC pin before [re]programming it. This is
571 * particularly important if the pin is set up for a level interrupt
572 * as the IOART_REM_IRR bit might be set. When we reprogram the
573 * vector any EOI from pending ints on this pin could be lost and
574 * IRR might never get reset.
576 * To fix this problem, clear the vector and make sure it is
577 * programmed as an edge interrupt. This should theoretically
578 * clear IRR so we can later, safely program it as a level
583 flags = ioapic_read(ioapic[apic], select) & IOART_RESV;
584 flags |= IOART_INTMSET | IOART_TRGREDG | IOART_INTAHI;
585 flags |= IOART_DESTPHY | IOART_DELFIXED;
587 target = ioapic_read(ioapic[apic], select + 1) & IOART_HI_DEST_RESV;
588 target |= 0; /* fixed mode cpu mask of 0 - don't deliver anywhere */
592 ioapic_write(ioapic[apic], select, flags | vector);
593 ioapic_write(ioapic[apic], select + 1, target);
598 * We only deal with vectored interrupts here. ? documentation is
599 * lacking, I'm guessing an interrupt type of 0 is the 'INT' type,
602 * This test also catches unconfigured pins.
604 if (apic_int_type(apic, pin) != 0)
608 * Leave the pin unprogrammed if it does not correspond to
611 irq = apic_irq(apic, pin);
615 /* determine the bus type for this pin */
616 bus = apic_src_bus_id(apic, pin);
619 bustype = apic_bus_type(bus);
621 if ((bustype == ISA) &&
622 (pin < IOAPIC_ISA_INTS) &&
624 (apic_polarity(apic, pin) == 0x1) &&
625 (apic_trigger(apic, pin) == 0x3)) {
627 * A broken BIOS might describe some ISA
628 * interrupts as active-high level-triggered.
629 * Use default ISA flags for those interrupts.
631 flags = DEFAULT_ISA_FLAGS;
634 * Program polarity and trigger mode according to
637 flags = DEFAULT_FLAGS;
638 level = trigger(apic, pin, &flags);
640 int_to_apicintpin[irq].flags |= IOAPIC_IM_FLAG_LEVEL;
641 polarity(apic, pin, &flags, level);
645 ksnprintf(envpath, sizeof(envpath), "hw.irq.%d.dest", irq);
646 kgetenv_int(envpath, &cpuid);
648 /* ncpus may not be available yet */
653 kprintf("IOAPIC #%d intpin %d -> irq %d (CPU%d)\n",
654 apic, pin, irq, cpuid);
658 * Program the appropriate registers. This routing may be
659 * overridden when an interrupt handler for a device is
660 * actually added (see register_int(), which calls through
661 * the MACHINTR ABI to set up an interrupt handler/vector).
663 * The order in which we must program the two registers for
664 * safety is unclear! XXX
668 vector = IDT_OFFSET + irq; /* IDT vec */
669 target = ioapic_read(ioapic[apic], select + 1) & IOART_HI_DEST_RESV;
670 /* Deliver all interrupts to CPU0 (BSP) */
671 target |= (CPU_TO_ID(cpuid) << IOART_HI_DEST_SHIFT) &
673 flags |= ioapic_read(ioapic[apic], select) & IOART_RESV;
674 ioapic_write(ioapic[apic], select, flags | vector);
675 ioapic_write(ioapic[apic], select + 1, target);
681 io_apic_setup(int apic)
686 maxpin = REDIRCNT_IOAPIC(apic); /* pins in APIC */
687 kprintf("Programming %d pins in IOAPIC #%d\n", maxpin, apic);
689 for (pin = 0; pin < maxpin; ++pin) {
690 io_apic_setup_intpin(apic, pin);
693 if (apic_int_type(apic, pin) >= 0) {
694 kprintf("Warning: IOAPIC #%d pin %d does not exist,"
695 " cannot program!\n", apic, pin);
700 /* return GOOD status */
703 #undef DEFAULT_ISA_FLAGS
707 #define DEFAULT_EXTINT_FLAGS \
716 * XXX this function is only used by 8254 setup
717 * Setup the source of External INTerrupts.
720 ext_int_setup(int apic, int intr)
722 u_char select; /* the select register is 8 bits */
723 u_int32_t flags; /* the window register is 32 bits */
724 u_int32_t target; /* the window register is 32 bits */
725 u_int32_t vector; /* the window register is 32 bits */
729 if (apic_int_type(apic, intr) != 3)
733 ksnprintf(envpath, sizeof(envpath), "hw.irq.%d.dest", intr);
734 kgetenv_int(envpath, &cpuid);
736 /* ncpus may not be available yet */
740 /* Deliver interrupts to CPU0 (BSP) */
741 target = (CPU_TO_ID(cpuid) << IOART_HI_DEST_SHIFT) &
743 select = IOAPIC_REDTBL0 + (2 * intr);
744 vector = IDT_OFFSET + intr;
745 flags = DEFAULT_EXTINT_FLAGS;
747 ioapic_write(ioapic[apic], select, flags | vector);
748 ioapic_write(ioapic[apic], select + 1, target);
752 #undef DEFAULT_EXTINT_FLAGS
756 * Set the trigger level for an IO APIC pin.
759 trigger(int apic, int pin, u_int32_t * flags)
764 static int intcontrol = -1;
766 switch (apic_trigger(apic, pin)) {
772 *flags &= ~IOART_TRGRLVL; /* *flags |= IOART_TRGREDG */
776 *flags |= IOART_TRGRLVL;
784 if ((id = apic_src_bus_id(apic, pin)) == -1)
787 switch (apic_bus_type(id)) {
789 *flags &= ~IOART_TRGRLVL; /* *flags |= IOART_TRGREDG; */
793 eirq = apic_src_bus_irq(apic, pin);
795 if (eirq < 0 || eirq > 15) {
796 kprintf("EISA IRQ %d?!?!\n", eirq);
800 if (intcontrol == -1) {
801 intcontrol = inb(ELCR1) << 8;
802 intcontrol |= inb(ELCR0);
803 kprintf("EISA INTCONTROL = %08x\n", intcontrol);
806 /* Use ELCR settings to determine level or edge mode */
807 level = (intcontrol >> eirq) & 1;
810 * Note that on older Neptune chipset based systems, any
811 * pci interrupts often show up here and in the ELCR as well
812 * as level sensitive interrupts attributed to the EISA bus.
816 *flags |= IOART_TRGRLVL;
818 *flags &= ~IOART_TRGRLVL;
823 *flags |= IOART_TRGRLVL;
832 panic("bad APIC IO INT flags");
837 * Set the polarity value for an IO APIC pin.
840 polarity(int apic, int pin, u_int32_t * flags, int level)
844 switch (apic_polarity(apic, pin)) {
850 *flags &= ~IOART_INTALO; /* *flags |= IOART_INTAHI */
854 *flags |= IOART_INTALO;
862 if ((id = apic_src_bus_id(apic, pin)) == -1)
865 switch (apic_bus_type(id)) {
867 *flags &= ~IOART_INTALO; /* *flags |= IOART_INTAHI */
871 /* polarity converter always gives active high */
872 *flags &= ~IOART_INTALO;
876 *flags |= IOART_INTALO;
885 panic("bad APIC IO INT flags");
890 * Print contents of unmasked IRQs.
897 kprintf("SMP: enabled INTs: ");
898 for (x = 0; x < APIC_INTMAPSIZE; ++x) {
899 if ((int_to_apicintpin[x].flags & IOAPIC_IM_FLAG_MASKED) == 0)
907 * Inter Processor Interrupt functions.
910 #endif /* SMP APIC-IO */
913 * Send APIC IPI 'vector' to 'destType' via 'deliveryMode'.
915 * destType is 1 of: APIC_DEST_SELF, APIC_DEST_ALLISELF, APIC_DEST_ALLESELF
916 * vector is any valid SYSTEM INT vector
917 * delivery_mode is 1 of: APIC_DELMODE_FIXED, APIC_DELMODE_LOWPRIO
919 * A backlog of requests can create a deadlock between cpus. To avoid this
920 * we have to be able to accept IPIs at the same time we are trying to send
921 * them. The critical section prevents us from attempting to send additional
922 * IPIs reentrantly, but also prevents IPIQ processing so we have to call
923 * lwkt_process_ipiq() manually. It's rather messy and expensive for this
924 * to occur but fortunately it does not happen too often.
927 apic_ipi(int dest_type, int vector, int delivery_mode)
932 if ((lapic->icr_lo & APIC_DELSTAT_MASK) != 0) {
933 unsigned long rflags = read_rflags();
935 DEBUG_PUSH_INFO("apic_ipi");
936 while ((lapic->icr_lo & APIC_DELSTAT_MASK) != 0) {
940 write_rflags(rflags);
943 icr_lo = (lapic->icr_lo & APIC_ICRLO_RESV_MASK) | dest_type |
944 delivery_mode | vector;
945 lapic->icr_lo = icr_lo;
951 single_apic_ipi(int cpu, int vector, int delivery_mode)
957 if ((lapic->icr_lo & APIC_DELSTAT_MASK) != 0) {
958 unsigned long rflags = read_rflags();
960 DEBUG_PUSH_INFO("single_apic_ipi");
961 while ((lapic->icr_lo & APIC_DELSTAT_MASK) != 0) {
965 write_rflags(rflags);
967 icr_hi = lapic->icr_hi & ~APIC_ID_MASK;
968 icr_hi |= (CPU_TO_ID(cpu) << 24);
969 lapic->icr_hi = icr_hi;
972 icr_lo = (lapic->icr_lo & APIC_ICRLO_RESV_MASK)
973 | APIC_DEST_DESTFLD | delivery_mode | vector;
976 lapic->icr_lo = icr_lo;
983 * Returns 0 if the apic is busy, 1 if we were able to queue the request.
985 * NOT WORKING YET! The code as-is may end up not queueing an IPI at all
986 * to the target, and the scheduler does not 'poll' for IPI messages.
989 single_apic_ipi_passive(int cpu, int vector, int delivery_mode)
995 if ((lapic->icr_lo & APIC_DELSTAT_MASK) != 0) {
999 icr_hi = lapic->icr_hi & ~APIC_ID_MASK;
1000 icr_hi |= (CPU_TO_ID(cpu) << 24);
1001 lapic->icr_hi = icr_hi;
1004 icr_lo = (lapic->icr_lo & APIC_RESV2_MASK)
1005 | APIC_DEST_DESTFLD | delivery_mode | vector;
1007 /* write APIC ICR */
1008 lapic->icr_lo = icr_lo;
1016 * Send APIC IPI 'vector' to 'target's via 'delivery_mode'.
1018 * target is a bitmask of destination cpus. Vector is any
1019 * valid system INT vector. Delivery mode may be either
1020 * APIC_DELMODE_FIXED or APIC_DELMODE_LOWPRIO.
1023 selected_apic_ipi(cpumask_t target, int vector, int delivery_mode)
1027 int n = BSFCPUMASK(target);
1028 target &= ~CPUMASK(n);
1029 single_apic_ipi(n, vector, delivery_mode);
1035 * Timer code, in development...
1036 * - suggested by rgrimes@gndrsh.aac.dev.com
1039 get_apic_timer_frequency(void)
1041 return(lapic_cputimer_intr.freq);
1045 * Load a 'downcount time' in uSeconds.
1048 set_apic_timer(int us)
1053 * When we reach here, lapic timer's frequency
1054 * must have been calculated as well as the
1055 * divisor (lapic->dcr_timer is setup during the
1056 * divisor calculation).
1058 KKASSERT(lapic_cputimer_intr.freq != 0 &&
1059 lapic_timer_divisor_idx >= 0);
1061 count = ((us * (int64_t)lapic_cputimer_intr.freq) + 999999) / 1000000;
1062 lapic_timer_oneshot(count);
1067 * Read remaining time in timer.
1070 read_apic_timer(void)
1073 /** XXX FIXME: we need to return the actual remaining time,
1074 * for now we just return the remaining count.
1077 return lapic->ccr_timer;
1083 * Spin-style delay, set delay time in uS, spin till it drains.
1088 set_apic_timer(count);
1089 while (read_apic_timer())
1094 lapic_map(vm_offset_t lapic_addr)
1096 lapic = pmap_mapdev_uncacheable(lapic_addr, sizeof(struct LAPIC));
1098 kprintf("lapic: at 0x%08lx\n", lapic_addr);
1101 static TAILQ_HEAD(, lapic_enumerator) lapic_enumerators =
1102 TAILQ_HEAD_INITIALIZER(lapic_enumerators);
1107 struct lapic_enumerator *e;
1110 TAILQ_FOREACH(e, &lapic_enumerators, lapic_link) {
1111 error = e->lapic_probe(e);
1116 panic("can't config lapic\n");
1118 e->lapic_enumerate(e);
1122 lapic_enumerator_register(struct lapic_enumerator *ne)
1124 struct lapic_enumerator *e;
1126 TAILQ_FOREACH(e, &lapic_enumerators, lapic_link) {
1127 if (e->lapic_prio < ne->lapic_prio) {
1128 TAILQ_INSERT_BEFORE(e, ne, lapic_link);
1132 TAILQ_INSERT_TAIL(&lapic_enumerators, ne, lapic_link);
1135 static TAILQ_HEAD(, ioapic_enumerator) ioapic_enumerators =
1136 TAILQ_HEAD_INITIALIZER(ioapic_enumerators);
1141 struct ioapic_enumerator *e;
1144 TAILQ_INIT(&ioapic_conf.ioc_list);
1145 /* XXX magic number */
1146 for (i = 0; i < 16; ++i)
1147 ioapic_conf.ioc_intsrc[i] = -1;
1149 TAILQ_FOREACH(e, &ioapic_enumerators, ioapic_link) {
1150 error = e->ioapic_probe(e);
1156 panic("can't config I/O APIC\n");
1158 kprintf("no I/O APIC\n");
1163 e->ioapic_enumerate(e);
1165 if (!ioapic_use_old) {
1166 struct ioapic_info *info;
1169 * Fixup the rest of the fields of ioapic_info
1172 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link) {
1173 const struct ioapic_info *prev_info;
1176 info->io_apic_id = info->io_idx + lapic_id_max + 1;
1179 kprintf("IOAPIC: idx %d, apic id %d, "
1180 "gsi base %d, npin %d\n",
1187 /* Warning about possible GSI hole */
1188 prev_info = TAILQ_PREV(info, ioapic_info_list, io_link);
1189 if (prev_info != NULL) {
1190 if (info->io_gsi_base !=
1191 prev_info->io_gsi_base + prev_info->io_npin) {
1192 kprintf("IOAPIC: warning gsi hole "
1194 prev_info->io_gsi_base +
1196 info->io_gsi_base - 1);
1202 * Setup all I/O APIC
1204 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link)
1207 panic("ioapic_config: new ioapic not working yet\n");
1212 ioapic_enumerator_register(struct ioapic_enumerator *ne)
1214 struct ioapic_enumerator *e;
1216 TAILQ_FOREACH(e, &ioapic_enumerators, ioapic_link) {
1217 if (e->ioapic_prio < ne->ioapic_prio) {
1218 TAILQ_INSERT_BEFORE(e, ne, ioapic_link);
1222 TAILQ_INSERT_TAIL(&ioapic_enumerators, ne, ioapic_link);
1226 ioapic_add(void *addr, int gsi_base, int npin)
1228 struct ioapic_info *info, *ninfo;
1231 gsi_end = gsi_base + npin - 1;
1232 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link) {
1233 if ((gsi_base >= info->io_gsi_base &&
1234 gsi_base < info->io_gsi_base + info->io_npin) ||
1235 (gsi_end >= info->io_gsi_base &&
1236 gsi_end < info->io_gsi_base + info->io_npin)) {
1237 panic("ioapic_add: overlapped gsi, base %d npin %d, "
1238 "hit base %d, npin %d\n", gsi_base, npin,
1239 info->io_gsi_base, info->io_npin);
1241 if (info->io_addr == addr)
1242 panic("ioapic_add: duplicated addr %p\n", addr);
1245 ninfo = kmalloc(sizeof(*ninfo), M_DEVBUF, M_WAITOK | M_ZERO);
1246 ninfo->io_addr = addr;
1247 ninfo->io_npin = npin;
1248 ninfo->io_gsi_base = gsi_base;
1251 * Create IOAPIC list in ascending order of GSI base
1253 TAILQ_FOREACH_REVERSE(info, &ioapic_conf.ioc_list,
1254 ioapic_info_list, io_link) {
1255 if (ninfo->io_gsi_base > info->io_gsi_base) {
1256 TAILQ_INSERT_AFTER(&ioapic_conf.ioc_list,
1257 info, ninfo, io_link);
1262 TAILQ_INSERT_HEAD(&ioapic_conf.ioc_list, ninfo, io_link);
1266 ioapic_intsrc(int irq, int gsi)
1269 if (ioapic_conf.ioc_intsrc[irq] != -1 &&
1270 ioapic_conf.ioc_intsrc[irq] != gsi) {
1271 kprintf("IOAPIC: warning intsrc irq %d, gsi %d -> gsi %d\n",
1272 irq, ioapic_conf.ioc_intsrc[irq], gsi);
1274 ioapic_conf.ioc_intsrc[irq] = gsi;
1278 ioapic_set_apic_id(const struct ioapic_info *info)
1282 id = ioapic_read(info->io_addr, IOAPIC_ID);
1284 id &= ~APIC_ID_MASK;
1285 id |= (info->io_apic_id << 24);
1287 ioapic_write(info->io_addr, IOAPIC_ID, id);
1292 id = ioapic_read(info->io_addr, IOAPIC_ID);
1293 if (((id & APIC_ID_MASK) >> 24) != info->io_apic_id) {
1294 panic("ioapic_set_apic_id: can't set apic id to %d\n",
1300 ioapic_gsi_setup(int gsi)
1302 enum intr_trigger trig;
1303 enum intr_polarity pola;
1306 for (irq = 0; irq < 16; ++irq) {
1307 if (gsi == ioapic_conf.ioc_intsrc[irq]) {
1308 trig = INTR_TRIGGER_EDGE;
1309 pola = INTR_POLARITY_HIGH;
1316 /* TODO Program EXTINT */
1318 } else if (gsi < 16) {
1319 trig = INTR_TRIGGER_EDGE;
1320 pola = INTR_POLARITY_HIGH;
1322 trig = INTR_TRIGGER_LEVEL;
1323 pola = INTR_POLARITY_LOW;
1329 ioapic_abi_set_irqmap(irq, gsi, trig, pola);
1334 ioapic_gsi_ioaddr(int gsi)
1336 const struct ioapic_info *info;
1338 info = ioapic_gsi_search(gsi);
1339 return info->io_addr;
1343 ioapic_gsi_pin(int gsi)
1345 const struct ioapic_info *info;
1347 info = ioapic_gsi_search(gsi);
1348 return gsi - info->io_gsi_base;
1351 static const struct ioapic_info *
1352 ioapic_gsi_search(int gsi)
1354 const struct ioapic_info *info;
1356 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link) {
1357 if (gsi >= info->io_gsi_base &&
1358 gsi < info->io_gsi_base + info->io_npin)
1361 panic("ioapic_gsi_search: no I/O APIC\n");
1365 ioapic_setup(const struct ioapic_info *info)
1369 ioapic_set_apic_id(info);
1371 for (i = 0; i < info->io_npin; ++i)
1372 ioapic_gsi_setup(info->io_gsi_base + i);