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_base/apic/ioapic_abi.h>
38 #include <machine/segments.h>
39 #include <sys/thread2.h>
41 #include <machine/intr_machdep.h>
45 /* EISA Edge/Level trigger control registers */
46 #define ELCR0 0x4d0 /* eisa irq 0-7 */
47 #define ELCR1 0x4d1 /* eisa irq 8-15 */
56 TAILQ_ENTRY(ioapic_info) io_link;
58 TAILQ_HEAD(ioapic_info_list, ioapic_info);
61 struct ioapic_info_list ioc_list;
62 int ioc_intsrc[16]; /* XXX magic number */
65 volatile lapic_t *lapic;
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 *);
74 void lapic_timer_always(struct intrframe *);
76 static int lapic_timer_enable = 1;
77 TUNABLE_INT("hw.lapic_timer_enable", &lapic_timer_enable);
79 static void lapic_timer_intr_reload(struct cputimer_intr *, sysclock_t);
80 static void lapic_timer_intr_enable(struct cputimer_intr *);
81 static void lapic_timer_intr_restart(struct cputimer_intr *);
82 static void lapic_timer_intr_pmfixup(struct cputimer_intr *);
84 static void ioapic_setup(const struct ioapic_info *);
85 static void ioapic_set_apic_id(const struct ioapic_info *);
86 static void ioapic_gsi_setup(int);
87 static const struct ioapic_info *
88 ioapic_gsi_search(int);
90 static struct cputimer_intr lapic_cputimer_intr = {
92 .reload = lapic_timer_intr_reload,
93 .enable = lapic_timer_intr_enable,
94 .config = cputimer_intr_default_config,
95 .restart = lapic_timer_intr_restart,
96 .pmfixup = lapic_timer_intr_pmfixup,
97 .initclock = cputimer_intr_default_initclock,
98 .next = SLIST_ENTRY_INITIALIZER,
100 .type = CPUTIMER_INTR_LAPIC,
101 .prio = CPUTIMER_INTR_PRIO_LAPIC,
102 .caps = CPUTIMER_INTR_CAP_NONE
106 * pointers to pmapped apic hardware.
109 volatile ioapic_t **ioapic;
111 static int lapic_timer_divisor_idx = -1;
112 static const uint32_t lapic_timer_divisors[] = {
113 APIC_TDCR_2, APIC_TDCR_4, APIC_TDCR_8, APIC_TDCR_16,
114 APIC_TDCR_32, APIC_TDCR_64, APIC_TDCR_128, APIC_TDCR_1
116 #define APIC_TIMER_NDIVISORS (int)(NELEM(lapic_timer_divisors))
119 static struct ioapic_conf ioapic_conf;
129 * Enable LAPIC, configure interrupts.
132 apic_initialize(boolean_t bsp)
138 * Setup LINT0 as ExtINT on the BSP. This is theoretically an
139 * aggregate interrupt input from the 8259. The INTA cycle
140 * will be routed to the external controller (the 8259) which
141 * is expected to supply the vector.
143 * Must be setup edge triggered, active high.
145 * Disable LINT0 on the APs. It doesn't matter what delivery
146 * mode we use because we leave it masked.
148 temp = lapic->lvt_lint0;
149 temp &= ~(APIC_LVT_MASKED | APIC_LVT_TRIG_MASK |
150 APIC_LVT_POLARITY_MASK | APIC_LVT_DM_MASK);
151 if (mycpu->gd_cpuid == 0)
152 temp |= APIC_LVT_DM_EXTINT;
154 temp |= APIC_LVT_DM_FIXED | APIC_LVT_MASKED;
155 lapic->lvt_lint0 = temp;
158 * Setup LINT1 as NMI, masked till later.
159 * Edge trigger, active high.
161 temp = lapic->lvt_lint1;
162 temp &= ~(APIC_LVT_MASKED | APIC_LVT_TRIG_MASK |
163 APIC_LVT_POLARITY_MASK | APIC_LVT_DM_MASK);
164 temp |= APIC_LVT_MASKED | APIC_LVT_DM_NMI;
165 lapic->lvt_lint1 = temp;
168 * Mask the LAPIC error interrupt, LAPIC performance counter
171 lapic->lvt_error = lapic->lvt_error | APIC_LVT_MASKED;
172 lapic->lvt_pcint = lapic->lvt_pcint | APIC_LVT_MASKED;
175 * Set LAPIC timer vector and mask the LAPIC timer interrupt.
177 timer = lapic->lvt_timer;
178 timer &= ~APIC_LVTT_VECTOR;
179 timer |= XTIMER_OFFSET;
180 timer |= APIC_LVTT_MASKED;
181 lapic->lvt_timer = timer;
184 * Set the Task Priority Register as needed. At the moment allow
185 * interrupts on all cpus (the APs will remain CLId until they are
186 * ready to deal). We could disable all but IPIs by setting
187 * temp |= TPR_IPI for cpu != 0.
190 temp &= ~APIC_TPR_PRIO; /* clear priority field */
191 #ifdef SMP /* APIC-IO */
192 if (!apic_io_enable) {
195 * If we are NOT running the IO APICs, the LAPIC will only be used
196 * for IPIs. Set the TPR to prevent any unintentional interrupts.
199 #ifdef SMP /* APIC-IO */
208 temp |= APIC_SVR_ENABLE; /* enable the LAPIC */
209 temp &= ~APIC_SVR_FOCUS_DISABLE; /* enable lopri focus processor */
212 * Set the spurious interrupt vector. The low 4 bits of the vector
215 if ((XSPURIOUSINT_OFFSET & 0x0F) != 0x0F)
216 panic("bad XSPURIOUSINT_OFFSET: 0x%08x", XSPURIOUSINT_OFFSET);
217 temp &= ~APIC_SVR_VECTOR;
218 temp |= XSPURIOUSINT_OFFSET;
223 * Pump out a few EOIs to clean out interrupts that got through
224 * before we were able to set the TPR.
231 lapic_timer_calibrate();
232 if (lapic_timer_enable) {
233 cputimer_intr_register(&lapic_cputimer_intr);
234 cputimer_intr_select(&lapic_cputimer_intr, 0);
237 lapic_timer_set_divisor(lapic_timer_divisor_idx);
241 apic_dump("apic_initialize()");
245 lapic_timer_set_divisor(int divisor_idx)
247 KKASSERT(divisor_idx >= 0 && divisor_idx < APIC_TIMER_NDIVISORS);
248 lapic->dcr_timer = lapic_timer_divisors[divisor_idx];
252 lapic_timer_oneshot(u_int count)
256 value = lapic->lvt_timer;
257 value &= ~APIC_LVTT_PERIODIC;
258 lapic->lvt_timer = value;
259 lapic->icr_timer = count;
263 lapic_timer_oneshot_quick(u_int count)
265 lapic->icr_timer = count;
269 lapic_timer_calibrate(void)
273 /* Try to calibrate the local APIC timer. */
274 for (lapic_timer_divisor_idx = 0;
275 lapic_timer_divisor_idx < APIC_TIMER_NDIVISORS;
276 lapic_timer_divisor_idx++) {
277 lapic_timer_set_divisor(lapic_timer_divisor_idx);
278 lapic_timer_oneshot(APIC_TIMER_MAX_COUNT);
280 value = APIC_TIMER_MAX_COUNT - lapic->ccr_timer;
281 if (value != APIC_TIMER_MAX_COUNT)
284 if (lapic_timer_divisor_idx >= APIC_TIMER_NDIVISORS)
285 panic("lapic: no proper timer divisor?!\n");
286 lapic_cputimer_intr.freq = value / 2;
288 kprintf("lapic: divisor index %d, frequency %u Hz\n",
289 lapic_timer_divisor_idx, lapic_cputimer_intr.freq);
293 lapic_timer_process_oncpu(struct globaldata *gd, struct intrframe *frame)
297 gd->gd_timer_running = 0;
299 count = sys_cputimer->count();
300 if (TAILQ_FIRST(&gd->gd_systimerq) != NULL)
301 systimer_intr(&count, 0, frame);
305 lapic_timer_process(void)
307 lapic_timer_process_oncpu(mycpu, NULL);
311 lapic_timer_process_frame(struct intrframe *frame)
313 lapic_timer_process_oncpu(mycpu, frame);
317 * This manual debugging code is called unconditionally from Xtimer
318 * (the lapic timer interrupt) whether the current thread is in a
319 * critical section or not) and can be useful in tracking down lockups.
321 * NOTE: MANUAL DEBUG CODE
324 static int saveticks[SMP_MAXCPU];
325 static int savecounts[SMP_MAXCPU];
329 lapic_timer_always(struct intrframe *frame)
332 globaldata_t gd = mycpu;
333 int cpu = gd->gd_cpuid;
339 gptr = (short *)0xFFFFFFFF800b8000 + 80 * cpu;
340 *gptr = ((*gptr + 1) & 0x00FF) | 0x0700;
343 ksnprintf(buf, sizeof(buf), " %p %16s %d %16s ",
344 (void *)frame->if_rip, gd->gd_curthread->td_comm, ticks,
346 for (i = 0; buf[i]; ++i) {
347 gptr[i] = 0x0700 | (unsigned char)buf[i];
351 if (saveticks[gd->gd_cpuid] != ticks) {
352 saveticks[gd->gd_cpuid] = ticks;
353 savecounts[gd->gd_cpuid] = 0;
355 ++savecounts[gd->gd_cpuid];
356 if (savecounts[gd->gd_cpuid] > 2000 && panicstr == NULL) {
357 panic("cpud %d panicing on ticks failure",
360 for (i = 0; i < ncpus; ++i) {
362 if (saveticks[i] && panicstr == NULL) {
363 delta = saveticks[i] - ticks;
364 if (delta < -10 || delta > 10) {
365 panic("cpu %d panicing on cpu %d watchdog",
375 lapic_timer_intr_reload(struct cputimer_intr *cti, sysclock_t reload)
377 struct globaldata *gd = mycpu;
379 reload = (int64_t)reload * cti->freq / sys_cputimer->freq;
383 if (gd->gd_timer_running) {
384 if (reload < lapic->ccr_timer)
385 lapic_timer_oneshot_quick(reload);
387 gd->gd_timer_running = 1;
388 lapic_timer_oneshot_quick(reload);
393 lapic_timer_intr_enable(struct cputimer_intr *cti __unused)
397 timer = lapic->lvt_timer;
398 timer &= ~(APIC_LVTT_MASKED | APIC_LVTT_PERIODIC);
399 lapic->lvt_timer = timer;
401 lapic_timer_fixup_handler(NULL);
405 lapic_timer_fixup_handler(void *arg)
412 if (strcmp(cpu_vendor, "AuthenticAMD") == 0) {
414 * Detect the presence of C1E capability mostly on latest
415 * dual-cores (or future) k8 family. This feature renders
416 * the local APIC timer dead, so we disable it by reading
417 * the Interrupt Pending Message register and clearing both
418 * C1eOnCmpHalt (bit 28) and SmiOnCmpHalt (bit 27).
421 * "BIOS and Kernel Developer's Guide for AMD NPT
422 * Family 0Fh Processors"
423 * #32559 revision 3.00
425 if ((cpu_id & 0x00000f00) == 0x00000f00 &&
426 (cpu_id & 0x0fff0000) >= 0x00040000) {
429 msr = rdmsr(0xc0010055);
430 if (msr & 0x18000000) {
431 struct globaldata *gd = mycpu;
433 kprintf("cpu%d: AMD C1E detected\n",
435 wrmsr(0xc0010055, msr & ~0x18000000ULL);
438 * We are kinda stalled;
441 gd->gd_timer_running = 1;
442 lapic_timer_oneshot_quick(2);
452 lapic_timer_restart_handler(void *dummy __unused)
456 lapic_timer_fixup_handler(&started);
458 struct globaldata *gd = mycpu;
460 gd->gd_timer_running = 1;
461 lapic_timer_oneshot_quick(2);
466 * This function is called only by ACPI-CA code currently:
467 * - AMD C1E fixup. AMD C1E only seems to happen after ACPI
468 * module controls PM. So once ACPI-CA is attached, we try
469 * to apply the fixup to prevent LAPIC timer from hanging.
472 lapic_timer_intr_pmfixup(struct cputimer_intr *cti __unused)
474 lwkt_send_ipiq_mask(smp_active_mask,
475 lapic_timer_fixup_handler, NULL);
479 lapic_timer_intr_restart(struct cputimer_intr *cti __unused)
481 lwkt_send_ipiq_mask(smp_active_mask, lapic_timer_restart_handler, NULL);
486 * dump contents of local APIC registers
491 kprintf("SMP: CPU%d %s:\n", mycpu->gd_cpuid, str);
492 kprintf(" lint0: 0x%08x lint1: 0x%08x TPR: 0x%08x SVR: 0x%08x\n",
493 lapic->lvt_lint0, lapic->lvt_lint1, lapic->tpr, lapic->svr);
497 #ifdef SMP /* APIC-IO */
503 #define IOAPIC_ISA_INTS 16
504 #define REDIRCNT_IOAPIC(A) \
505 ((int)((io_apic_versions[(A)] & IOART_VER_MAXREDIR) >> MAXREDIRSHIFT) + 1)
507 static int trigger (int apic, int pin, u_int32_t * flags);
508 static void polarity (int apic, int pin, u_int32_t * flags, int level);
510 #define DEFAULT_FLAGS \
516 #define DEFAULT_ISA_FLAGS \
525 io_apic_set_id(int apic, int id)
529 ux = ioapic_read(ioapic[apic], IOAPIC_ID); /* get current contents */
530 if (((ux & APIC_ID_MASK) >> 24) != id) {
531 kprintf("Changing APIC ID for IO APIC #%d"
532 " from %d to %d on chip\n",
533 apic, ((ux & APIC_ID_MASK) >> 24), id);
534 ux &= ~APIC_ID_MASK; /* clear the ID field */
536 ioapic_write(ioapic[apic], IOAPIC_ID, ux); /* write new value */
537 ux = ioapic_read(ioapic[apic], IOAPIC_ID); /* re-read && test */
538 if (((ux & APIC_ID_MASK) >> 24) != id)
539 panic("can't control IO APIC #%d ID, reg: 0x%08x",
546 io_apic_get_id(int apic)
548 return (ioapic_read(ioapic[apic], IOAPIC_ID) & APIC_ID_MASK) >> 24;
557 io_apic_setup_intpin(int apic, int pin)
559 int bus, bustype, irq;
560 u_char select; /* the select register is 8 bits */
561 u_int32_t flags; /* the window register is 32 bits */
562 u_int32_t target; /* the window register is 32 bits */
563 u_int32_t vector; /* the window register is 32 bits */
568 select = pin * 2 + IOAPIC_REDTBL0; /* register */
571 * Always clear an IO APIC pin before [re]programming it. This is
572 * particularly important if the pin is set up for a level interrupt
573 * as the IOART_REM_IRR bit might be set. When we reprogram the
574 * vector any EOI from pending ints on this pin could be lost and
575 * IRR might never get reset.
577 * To fix this problem, clear the vector and make sure it is
578 * programmed as an edge interrupt. This should theoretically
579 * clear IRR so we can later, safely program it as a level
584 flags = ioapic_read(ioapic[apic], select) & IOART_RESV;
585 flags |= IOART_INTMSET | IOART_TRGREDG | IOART_INTAHI;
586 flags |= IOART_DESTPHY | IOART_DELFIXED;
588 target = ioapic_read(ioapic[apic], select + 1) & IOART_HI_DEST_RESV;
589 target |= 0; /* fixed mode cpu mask of 0 - don't deliver anywhere */
593 ioapic_write(ioapic[apic], select, flags | vector);
594 ioapic_write(ioapic[apic], select + 1, target);
599 * We only deal with vectored interrupts here. ? documentation is
600 * lacking, I'm guessing an interrupt type of 0 is the 'INT' type,
603 * This test also catches unconfigured pins.
605 if (apic_int_type(apic, pin) != 0)
609 * Leave the pin unprogrammed if it does not correspond to
612 irq = apic_irq(apic, pin);
616 /* determine the bus type for this pin */
617 bus = apic_src_bus_id(apic, pin);
620 bustype = apic_bus_type(bus);
622 if ((bustype == ISA) &&
623 (pin < IOAPIC_ISA_INTS) &&
625 (apic_polarity(apic, pin) == 0x1) &&
626 (apic_trigger(apic, pin) == 0x3)) {
628 * A broken BIOS might describe some ISA
629 * interrupts as active-high level-triggered.
630 * Use default ISA flags for those interrupts.
632 flags = DEFAULT_ISA_FLAGS;
635 * Program polarity and trigger mode according to
638 flags = DEFAULT_FLAGS;
639 level = trigger(apic, pin, &flags);
641 int_to_apicintpin[irq].flags |= IOAPIC_IM_FLAG_LEVEL;
642 polarity(apic, pin, &flags, level);
646 ksnprintf(envpath, sizeof(envpath), "hw.irq.%d.dest", irq);
647 kgetenv_int(envpath, &cpuid);
649 /* ncpus may not be available yet */
654 kprintf("IOAPIC #%d intpin %d -> irq %d (CPU%d)\n",
655 apic, pin, irq, cpuid);
659 * Program the appropriate registers. This routing may be
660 * overridden when an interrupt handler for a device is
661 * actually added (see register_int(), which calls through
662 * the MACHINTR ABI to set up an interrupt handler/vector).
664 * The order in which we must program the two registers for
665 * safety is unclear! XXX
669 vector = IDT_OFFSET + irq; /* IDT vec */
670 target = ioapic_read(ioapic[apic], select + 1) & IOART_HI_DEST_RESV;
671 /* Deliver all interrupts to CPU0 (BSP) */
672 target |= (CPU_TO_ID(cpuid) << IOART_HI_DEST_SHIFT) &
674 flags |= ioapic_read(ioapic[apic], select) & IOART_RESV;
675 ioapic_write(ioapic[apic], select, flags | vector);
676 ioapic_write(ioapic[apic], select + 1, target);
682 io_apic_setup(int apic)
687 maxpin = REDIRCNT_IOAPIC(apic); /* pins in APIC */
688 kprintf("Programming %d pins in IOAPIC #%d\n", maxpin, apic);
690 for (pin = 0; pin < maxpin; ++pin) {
691 io_apic_setup_intpin(apic, pin);
694 if (apic_int_type(apic, pin) >= 0) {
695 kprintf("Warning: IOAPIC #%d pin %d does not exist,"
696 " cannot program!\n", apic, pin);
701 /* return GOOD status */
704 #undef DEFAULT_ISA_FLAGS
708 #define DEFAULT_EXTINT_FLAGS \
717 * XXX this function is only used by 8254 setup
718 * Setup the source of External INTerrupts.
721 ext_int_setup(int apic, int intr)
723 u_char select; /* the select register is 8 bits */
724 u_int32_t flags; /* the window register is 32 bits */
725 u_int32_t target; /* the window register is 32 bits */
726 u_int32_t vector; /* the window register is 32 bits */
730 if (apic_int_type(apic, intr) != 3)
734 ksnprintf(envpath, sizeof(envpath), "hw.irq.%d.dest", intr);
735 kgetenv_int(envpath, &cpuid);
737 /* ncpus may not be available yet */
741 /* Deliver interrupts to CPU0 (BSP) */
742 target = (CPU_TO_ID(cpuid) << IOART_HI_DEST_SHIFT) &
744 select = IOAPIC_REDTBL0 + (2 * intr);
745 vector = IDT_OFFSET + intr;
746 flags = DEFAULT_EXTINT_FLAGS;
748 ioapic_write(ioapic[apic], select, flags | vector);
749 ioapic_write(ioapic[apic], select + 1, target);
753 #undef DEFAULT_EXTINT_FLAGS
757 * Set the trigger level for an IO APIC pin.
760 trigger(int apic, int pin, u_int32_t * flags)
765 static int intcontrol = -1;
767 switch (apic_trigger(apic, pin)) {
773 *flags &= ~IOART_TRGRLVL; /* *flags |= IOART_TRGREDG */
777 *flags |= IOART_TRGRLVL;
785 if ((id = apic_src_bus_id(apic, pin)) == -1)
788 switch (apic_bus_type(id)) {
790 *flags &= ~IOART_TRGRLVL; /* *flags |= IOART_TRGREDG; */
794 eirq = apic_src_bus_irq(apic, pin);
796 if (eirq < 0 || eirq > 15) {
797 kprintf("EISA IRQ %d?!?!\n", eirq);
801 if (intcontrol == -1) {
802 intcontrol = inb(ELCR1) << 8;
803 intcontrol |= inb(ELCR0);
804 kprintf("EISA INTCONTROL = %08x\n", intcontrol);
807 /* Use ELCR settings to determine level or edge mode */
808 level = (intcontrol >> eirq) & 1;
811 * Note that on older Neptune chipset based systems, any
812 * pci interrupts often show up here and in the ELCR as well
813 * as level sensitive interrupts attributed to the EISA bus.
817 *flags |= IOART_TRGRLVL;
819 *flags &= ~IOART_TRGRLVL;
824 *flags |= IOART_TRGRLVL;
833 panic("bad APIC IO INT flags");
838 * Set the polarity value for an IO APIC pin.
841 polarity(int apic, int pin, u_int32_t * flags, int level)
845 switch (apic_polarity(apic, pin)) {
851 *flags &= ~IOART_INTALO; /* *flags |= IOART_INTAHI */
855 *flags |= IOART_INTALO;
863 if ((id = apic_src_bus_id(apic, pin)) == -1)
866 switch (apic_bus_type(id)) {
868 *flags &= ~IOART_INTALO; /* *flags |= IOART_INTAHI */
872 /* polarity converter always gives active high */
873 *flags &= ~IOART_INTALO;
877 *flags |= IOART_INTALO;
886 panic("bad APIC IO INT flags");
891 * Print contents of unmasked IRQs.
898 kprintf("SMP: enabled INTs: ");
899 for (x = 0; x < APIC_INTMAPSIZE; ++x) {
900 if ((int_to_apicintpin[x].flags & IOAPIC_IM_FLAG_MASKED) == 0)
908 * Inter Processor Interrupt functions.
911 #endif /* SMP APIC-IO */
914 * Send APIC IPI 'vector' to 'destType' via 'deliveryMode'.
916 * destType is 1 of: APIC_DEST_SELF, APIC_DEST_ALLISELF, APIC_DEST_ALLESELF
917 * vector is any valid SYSTEM INT vector
918 * delivery_mode is 1 of: APIC_DELMODE_FIXED, APIC_DELMODE_LOWPRIO
920 * A backlog of requests can create a deadlock between cpus. To avoid this
921 * we have to be able to accept IPIs at the same time we are trying to send
922 * them. The critical section prevents us from attempting to send additional
923 * IPIs reentrantly, but also prevents IPIQ processing so we have to call
924 * lwkt_process_ipiq() manually. It's rather messy and expensive for this
925 * to occur but fortunately it does not happen too often.
928 apic_ipi(int dest_type, int vector, int delivery_mode)
933 if ((lapic->icr_lo & APIC_DELSTAT_MASK) != 0) {
934 unsigned long rflags = read_rflags();
936 DEBUG_PUSH_INFO("apic_ipi");
937 while ((lapic->icr_lo & APIC_DELSTAT_MASK) != 0) {
941 write_rflags(rflags);
944 icr_lo = (lapic->icr_lo & APIC_ICRLO_RESV_MASK) | dest_type |
945 delivery_mode | vector;
946 lapic->icr_lo = icr_lo;
952 single_apic_ipi(int cpu, int vector, int delivery_mode)
958 if ((lapic->icr_lo & APIC_DELSTAT_MASK) != 0) {
959 unsigned long rflags = read_rflags();
961 DEBUG_PUSH_INFO("single_apic_ipi");
962 while ((lapic->icr_lo & APIC_DELSTAT_MASK) != 0) {
966 write_rflags(rflags);
968 icr_hi = lapic->icr_hi & ~APIC_ID_MASK;
969 icr_hi |= (CPU_TO_ID(cpu) << 24);
970 lapic->icr_hi = icr_hi;
973 icr_lo = (lapic->icr_lo & APIC_ICRLO_RESV_MASK)
974 | APIC_DEST_DESTFLD | delivery_mode | vector;
977 lapic->icr_lo = icr_lo;
984 * Returns 0 if the apic is busy, 1 if we were able to queue the request.
986 * NOT WORKING YET! The code as-is may end up not queueing an IPI at all
987 * to the target, and the scheduler does not 'poll' for IPI messages.
990 single_apic_ipi_passive(int cpu, int vector, int delivery_mode)
996 if ((lapic->icr_lo & APIC_DELSTAT_MASK) != 0) {
1000 icr_hi = lapic->icr_hi & ~APIC_ID_MASK;
1001 icr_hi |= (CPU_TO_ID(cpu) << 24);
1002 lapic->icr_hi = icr_hi;
1005 icr_lo = (lapic->icr_lo & APIC_RESV2_MASK)
1006 | APIC_DEST_DESTFLD | delivery_mode | vector;
1008 /* write APIC ICR */
1009 lapic->icr_lo = icr_lo;
1017 * Send APIC IPI 'vector' to 'target's via 'delivery_mode'.
1019 * target is a bitmask of destination cpus. Vector is any
1020 * valid system INT vector. Delivery mode may be either
1021 * APIC_DELMODE_FIXED or APIC_DELMODE_LOWPRIO.
1024 selected_apic_ipi(cpumask_t target, int vector, int delivery_mode)
1028 int n = BSFCPUMASK(target);
1029 target &= ~CPUMASK(n);
1030 single_apic_ipi(n, vector, delivery_mode);
1036 * Timer code, in development...
1037 * - suggested by rgrimes@gndrsh.aac.dev.com
1040 get_apic_timer_frequency(void)
1042 return(lapic_cputimer_intr.freq);
1046 * Load a 'downcount time' in uSeconds.
1049 set_apic_timer(int us)
1054 * When we reach here, lapic timer's frequency
1055 * must have been calculated as well as the
1056 * divisor (lapic->dcr_timer is setup during the
1057 * divisor calculation).
1059 KKASSERT(lapic_cputimer_intr.freq != 0 &&
1060 lapic_timer_divisor_idx >= 0);
1062 count = ((us * (int64_t)lapic_cputimer_intr.freq) + 999999) / 1000000;
1063 lapic_timer_oneshot(count);
1068 * Read remaining time in timer.
1071 read_apic_timer(void)
1074 /** XXX FIXME: we need to return the actual remaining time,
1075 * for now we just return the remaining count.
1078 return lapic->ccr_timer;
1084 * Spin-style delay, set delay time in uS, spin till it drains.
1089 set_apic_timer(count);
1090 while (read_apic_timer())
1095 lapic_map(vm_offset_t lapic_addr)
1097 lapic = pmap_mapdev_uncacheable(lapic_addr, sizeof(struct LAPIC));
1099 kprintf("lapic: at 0x%08lx\n", lapic_addr);
1102 static TAILQ_HEAD(, lapic_enumerator) lapic_enumerators =
1103 TAILQ_HEAD_INITIALIZER(lapic_enumerators);
1108 struct lapic_enumerator *e;
1111 TAILQ_FOREACH(e, &lapic_enumerators, lapic_link) {
1112 error = e->lapic_probe(e);
1117 panic("can't config lapic\n");
1119 e->lapic_enumerate(e);
1123 lapic_enumerator_register(struct lapic_enumerator *ne)
1125 struct lapic_enumerator *e;
1127 TAILQ_FOREACH(e, &lapic_enumerators, lapic_link) {
1128 if (e->lapic_prio < ne->lapic_prio) {
1129 TAILQ_INSERT_BEFORE(e, ne, lapic_link);
1133 TAILQ_INSERT_TAIL(&lapic_enumerators, ne, lapic_link);
1136 static TAILQ_HEAD(, ioapic_enumerator) ioapic_enumerators =
1137 TAILQ_HEAD_INITIALIZER(ioapic_enumerators);
1142 struct ioapic_enumerator *e;
1145 TAILQ_INIT(&ioapic_conf.ioc_list);
1146 /* XXX magic number */
1147 for (i = 0; i < 16; ++i)
1148 ioapic_conf.ioc_intsrc[i] = -1;
1150 TAILQ_FOREACH(e, &ioapic_enumerators, ioapic_link) {
1151 error = e->ioapic_probe(e);
1157 panic("can't config I/O APIC\n");
1159 kprintf("no I/O APIC\n");
1164 e->ioapic_enumerate(e);
1166 if (!ioapic_use_old) {
1167 struct ioapic_info *info;
1170 * Fixup the rest of the fields of ioapic_info
1173 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link) {
1174 const struct ioapic_info *prev_info;
1177 info->io_apic_id = info->io_idx + lapic_id_max + 1;
1180 kprintf("IOAPIC: idx %d, apic id %d, "
1181 "gsi base %d, npin %d\n",
1188 /* Warning about possible GSI hole */
1189 prev_info = TAILQ_PREV(info, ioapic_info_list, io_link);
1190 if (prev_info != NULL) {
1191 if (info->io_gsi_base !=
1192 prev_info->io_gsi_base + prev_info->io_npin) {
1193 kprintf("IOAPIC: warning gsi hole "
1195 prev_info->io_gsi_base +
1197 info->io_gsi_base - 1);
1203 * Setup all I/O APIC
1205 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link)
1208 panic("ioapic_config: new ioapic not working yet\n");
1213 ioapic_enumerator_register(struct ioapic_enumerator *ne)
1215 struct ioapic_enumerator *e;
1217 TAILQ_FOREACH(e, &ioapic_enumerators, ioapic_link) {
1218 if (e->ioapic_prio < ne->ioapic_prio) {
1219 TAILQ_INSERT_BEFORE(e, ne, ioapic_link);
1223 TAILQ_INSERT_TAIL(&ioapic_enumerators, ne, ioapic_link);
1227 ioapic_add(void *addr, int gsi_base, int npin)
1229 struct ioapic_info *info, *ninfo;
1232 gsi_end = gsi_base + npin - 1;
1233 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link) {
1234 if ((gsi_base >= info->io_gsi_base &&
1235 gsi_base < info->io_gsi_base + info->io_npin) ||
1236 (gsi_end >= info->io_gsi_base &&
1237 gsi_end < info->io_gsi_base + info->io_npin)) {
1238 panic("ioapic_add: overlapped gsi, base %d npin %d, "
1239 "hit base %d, npin %d\n", gsi_base, npin,
1240 info->io_gsi_base, info->io_npin);
1242 if (info->io_addr == addr)
1243 panic("ioapic_add: duplicated addr %p\n", addr);
1246 ninfo = kmalloc(sizeof(*ninfo), M_DEVBUF, M_WAITOK | M_ZERO);
1247 ninfo->io_addr = addr;
1248 ninfo->io_npin = npin;
1249 ninfo->io_gsi_base = gsi_base;
1252 * Create IOAPIC list in ascending order of GSI base
1254 TAILQ_FOREACH_REVERSE(info, &ioapic_conf.ioc_list,
1255 ioapic_info_list, io_link) {
1256 if (ninfo->io_gsi_base > info->io_gsi_base) {
1257 TAILQ_INSERT_AFTER(&ioapic_conf.ioc_list,
1258 info, ninfo, io_link);
1263 TAILQ_INSERT_HEAD(&ioapic_conf.ioc_list, ninfo, io_link);
1267 ioapic_intsrc(int irq, int gsi)
1270 if (ioapic_conf.ioc_intsrc[irq] != -1 &&
1271 ioapic_conf.ioc_intsrc[irq] != gsi) {
1272 kprintf("IOAPIC: warning intsrc irq %d, gsi %d -> gsi %d\n",
1273 irq, ioapic_conf.ioc_intsrc[irq], gsi);
1275 ioapic_conf.ioc_intsrc[irq] = gsi;
1279 ioapic_set_apic_id(const struct ioapic_info *info)
1283 id = ioapic_read(info->io_addr, IOAPIC_ID);
1285 id &= ~APIC_ID_MASK;
1286 id |= (info->io_apic_id << 24);
1288 ioapic_write(info->io_addr, IOAPIC_ID, id);
1293 id = ioapic_read(info->io_addr, IOAPIC_ID);
1294 if (((id & APIC_ID_MASK) >> 24) != info->io_apic_id) {
1295 panic("ioapic_set_apic_id: can't set apic id to %d\n",
1301 ioapic_gsi_setup(int gsi)
1303 enum intr_trigger trig;
1304 enum intr_polarity pola;
1307 for (irq = 0; irq < 16; ++irq) {
1308 if (gsi == ioapic_conf.ioc_intsrc[irq]) {
1309 trig = INTR_TRIGGER_EDGE;
1310 pola = INTR_POLARITY_HIGH;
1317 /* TODO Program EXTINT */
1319 } else if (gsi < 16) {
1320 trig = INTR_TRIGGER_EDGE;
1321 pola = INTR_POLARITY_HIGH;
1323 trig = INTR_TRIGGER_LEVEL;
1324 pola = INTR_POLARITY_LOW;
1329 ioapic_abi_set_irqmap(irq, gsi, trig, pola);
1333 ioapic_gsi_ioaddr(int gsi)
1335 const struct ioapic_info *info;
1337 info = ioapic_gsi_search(gsi);
1338 return info->io_addr;
1342 ioapic_gsi_pin(int gsi)
1344 const struct ioapic_info *info;
1346 info = ioapic_gsi_search(gsi);
1347 return gsi - info->io_gsi_base;
1350 static const struct ioapic_info *
1351 ioapic_gsi_search(int gsi)
1353 const struct ioapic_info *info;
1355 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link) {
1356 if (gsi >= info->io_gsi_base &&
1357 gsi < info->io_gsi_base + info->io_npin)
1360 panic("ioapic_gsi_search: no I/O APIC\n");
1364 ioapic_setup(const struct ioapic_info *info)
1368 ioapic_set_apic_id(info);
1370 for (i = 0; i < info->io_npin; ++i)
1371 ioapic_gsi_setup(info->io_gsi_base + i);