2 * Copyright (c) 2003 John Baldwin <jhb@FreeBSD.org>
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. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
36 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/module.h>
40 #include <sys/mutex.h>
41 #include <sys/sysctl.h>
43 #include <dev/pci/pcireg.h>
44 #include <dev/pci/pcivar.h>
49 #include <x86/apicreg.h>
50 #include <machine/frame.h>
51 #include <machine/intr_machdep.h>
52 #include <x86/apicvar.h>
53 #include <machine/resource.h>
54 #include <machine/segments.h>
55 #include <x86/iommu/iommu_intrmap.h>
57 #define IOAPIC_ISA_INTS 16
58 #define IOAPIC_MEM_REGION 32
59 #define IOAPIC_REDTBL_LO(i) (IOAPIC_REDTBL + (i) * 2)
60 #define IOAPIC_REDTBL_HI(i) (IOAPIC_REDTBL_LO(i) + 1)
62 static MALLOC_DEFINE(M_IOAPIC, "io_apic", "I/O APIC structures");
65 * I/O APIC interrupt source driver. Each pin is assigned an IRQ cookie
66 * as laid out in the ACPI System Interrupt number model where each I/O
67 * APIC has a contiguous chunk of the System Interrupt address space.
68 * We assume that IRQs 1 - 15 behave like ISA IRQs and that all other
69 * IRQs behave as PCI IRQs by default. We also assume that the pin for
70 * IRQ 0 is actually an ExtINT pin. The apic enumerators override the
71 * configuration of individual pins as indicated by their tables.
73 * Documentation for the I/O APIC: "82093AA I/O Advanced Programmable
74 * Interrupt Controller (IOAPIC)", May 1996, Intel Corp.
75 * ftp://download.intel.com/design/chipsets/datashts/29056601.pdf
78 struct ioapic_intsrc {
79 struct intsrc io_intsrc;
85 u_int io_edgetrigger:1;
89 u_int io_remap_cookie;
94 u_int io_id:8; /* logical ID */
96 u_int io_intbase:8; /* System Interrupt base */
99 volatile ioapic_t *io_addr; /* XXX: should use bus_space */
101 STAILQ_ENTRY(ioapic) io_next;
102 struct ioapic_intsrc io_pins[0];
105 static u_int ioapic_read(volatile ioapic_t *apic, int reg);
106 static void ioapic_write(volatile ioapic_t *apic, int reg, u_int val);
107 static const char *ioapic_bus_string(int bus_type);
108 static void ioapic_print_irq(struct ioapic_intsrc *intpin);
109 static void ioapic_enable_source(struct intsrc *isrc);
110 static void ioapic_disable_source(struct intsrc *isrc, int eoi);
111 static void ioapic_eoi_source(struct intsrc *isrc);
112 static void ioapic_enable_intr(struct intsrc *isrc);
113 static void ioapic_disable_intr(struct intsrc *isrc);
114 static int ioapic_vector(struct intsrc *isrc);
115 static int ioapic_source_pending(struct intsrc *isrc);
116 static int ioapic_config_intr(struct intsrc *isrc, enum intr_trigger trig,
117 enum intr_polarity pol);
118 static void ioapic_resume(struct pic *pic, bool suspend_cancelled);
119 static int ioapic_assign_cpu(struct intsrc *isrc, u_int apic_id);
120 static void ioapic_program_intpin(struct ioapic_intsrc *intpin);
121 static void ioapic_reprogram_intpin(struct intsrc *isrc);
123 static STAILQ_HEAD(,ioapic) ioapic_list = STAILQ_HEAD_INITIALIZER(ioapic_list);
124 struct pic ioapic_template = {
125 .pic_enable_source = ioapic_enable_source,
126 .pic_disable_source = ioapic_disable_source,
127 .pic_eoi_source = ioapic_eoi_source,
128 .pic_enable_intr = ioapic_enable_intr,
129 .pic_disable_intr = ioapic_disable_intr,
130 .pic_vector = ioapic_vector,
131 .pic_source_pending = ioapic_source_pending,
133 .pic_resume = ioapic_resume,
134 .pic_config_intr = ioapic_config_intr,
135 .pic_assign_cpu = ioapic_assign_cpu,
136 .pic_reprogram_pin = ioapic_reprogram_intpin,
139 static int next_ioapic_base;
140 static u_int next_id;
142 static int enable_extint;
143 SYSCTL_INT(_hw_apic, OID_AUTO, enable_extint, CTLFLAG_RDTUN, &enable_extint, 0,
144 "Enable the ExtINT pin in the first I/O APIC");
147 _ioapic_eoi_source(struct intsrc *isrc, int locked)
149 struct ioapic_intsrc *src;
151 volatile uint32_t *apic_eoi;
155 if (!lapic_eoi_suppression)
157 src = (struct ioapic_intsrc *)isrc;
158 if (src->io_edgetrigger)
160 io = (struct ioapic *)isrc->is_pic;
163 * Handle targeted EOI for level-triggered pins, if broadcast
164 * EOI suppression is supported by LAPICs.
168 * If IOAPIC has EOI Register, simply write vector
169 * number into the reg.
171 apic_eoi = (volatile uint32_t *)((volatile char *)
172 io->io_addr + IOAPIC_EOIR);
173 *apic_eoi = src->io_vector;
176 * Otherwise, if IO-APIC is too old to provide EOIR,
177 * do what Intel did for the Linux kernel. Temporary
178 * switch the pin to edge-trigger and back, masking
179 * the pin during the trick.
182 mtx_lock_spin(&icu_lock);
183 low1 = src->io_lowreg;
184 low1 &= ~IOART_TRGRLVL;
185 low1 |= IOART_TRGREDG | IOART_INTMSET;
186 ioapic_write(io->io_addr, IOAPIC_REDTBL_LO(src->io_intpin),
188 ioapic_write(io->io_addr, IOAPIC_REDTBL_LO(src->io_intpin),
191 mtx_unlock_spin(&icu_lock);
196 ioapic_read(volatile ioapic_t *apic, int reg)
199 mtx_assert(&icu_lock, MA_OWNED);
200 apic->ioregsel = reg;
201 return (apic->iowin);
205 ioapic_write(volatile ioapic_t *apic, int reg, u_int val)
208 mtx_assert(&icu_lock, MA_OWNED);
209 apic->ioregsel = reg;
214 ioapic_bus_string(int bus_type)
230 ioapic_print_irq(struct ioapic_intsrc *intpin)
233 switch (intpin->io_irq) {
247 printf("%s IRQ %u", ioapic_bus_string(intpin->io_bus),
253 ioapic_enable_source(struct intsrc *isrc)
255 struct ioapic_intsrc *intpin = (struct ioapic_intsrc *)isrc;
256 struct ioapic *io = (struct ioapic *)isrc->is_pic;
259 mtx_lock_spin(&icu_lock);
260 if (intpin->io_masked) {
261 flags = intpin->io_lowreg & ~IOART_INTMASK;
262 ioapic_write(io->io_addr, IOAPIC_REDTBL_LO(intpin->io_intpin),
264 intpin->io_masked = 0;
266 mtx_unlock_spin(&icu_lock);
270 ioapic_disable_source(struct intsrc *isrc, int eoi)
272 struct ioapic_intsrc *intpin = (struct ioapic_intsrc *)isrc;
273 struct ioapic *io = (struct ioapic *)isrc->is_pic;
276 mtx_lock_spin(&icu_lock);
277 if (!intpin->io_masked && !intpin->io_edgetrigger) {
278 flags = intpin->io_lowreg | IOART_INTMSET;
279 ioapic_write(io->io_addr, IOAPIC_REDTBL_LO(intpin->io_intpin),
281 intpin->io_masked = 1;
285 _ioapic_eoi_source(isrc, 1);
287 mtx_unlock_spin(&icu_lock);
291 ioapic_eoi_source(struct intsrc *isrc)
294 _ioapic_eoi_source(isrc, 0);
298 * Completely program an intpin based on the data in its interrupt source
302 ioapic_program_intpin(struct ioapic_intsrc *intpin)
304 struct ioapic *io = (struct ioapic *)intpin->io_intsrc.is_pic;
305 uint32_t low, high, value;
311 * If a pin is completely invalid or if it is valid but hasn't
312 * been enabled yet, just ensure that the pin is masked.
314 mtx_assert(&icu_lock, MA_OWNED);
315 if (intpin->io_irq == IRQ_DISABLED || (intpin->io_irq < NUM_IO_INTS &&
316 intpin->io_vector == 0)) {
317 low = ioapic_read(io->io_addr,
318 IOAPIC_REDTBL_LO(intpin->io_intpin));
319 if ((low & IOART_INTMASK) == IOART_INTMCLR)
320 ioapic_write(io->io_addr,
321 IOAPIC_REDTBL_LO(intpin->io_intpin),
322 low | IOART_INTMSET);
324 mtx_unlock_spin(&icu_lock);
325 iommu_unmap_ioapic_intr(io->io_apic_id,
326 &intpin->io_remap_cookie);
327 mtx_lock_spin(&icu_lock);
333 mtx_unlock_spin(&icu_lock);
334 error = iommu_map_ioapic_intr(io->io_apic_id,
335 intpin->io_cpu, intpin->io_vector, intpin->io_edgetrigger,
336 intpin->io_activehi, intpin->io_irq, &intpin->io_remap_cookie,
338 mtx_lock_spin(&icu_lock);
340 ioapic_write(io->io_addr, IOAPIC_REDTBL_HI(intpin->io_intpin),
342 intpin->io_lowreg = low;
343 ioapic_write(io->io_addr, IOAPIC_REDTBL_LO(intpin->io_intpin),
346 } else if (error != EOPNOTSUPP) {
351 /* Set the destination. */
353 high = intpin->io_cpu << APIC_ID_SHIFT;
355 /* Program the rest of the low word. */
356 if (intpin->io_edgetrigger)
357 low |= IOART_TRGREDG;
359 low |= IOART_TRGRLVL;
360 if (intpin->io_activehi)
364 if (intpin->io_masked)
365 low |= IOART_INTMSET;
366 switch (intpin->io_irq) {
368 KASSERT(intpin->io_edgetrigger,
369 ("ExtINT not edge triggered"));
370 low |= IOART_DELEXINT;
373 KASSERT(intpin->io_edgetrigger,
374 ("NMI not edge triggered"));
378 KASSERT(intpin->io_edgetrigger,
379 ("SMI not edge triggered"));
383 KASSERT(intpin->io_vector != 0, ("No vector for IRQ %u",
385 low |= IOART_DELFIXED | intpin->io_vector;
388 /* Write the values to the APIC. */
389 value = ioapic_read(io->io_addr, IOAPIC_REDTBL_HI(intpin->io_intpin));
390 value &= ~IOART_DEST;
392 ioapic_write(io->io_addr, IOAPIC_REDTBL_HI(intpin->io_intpin), value);
393 intpin->io_lowreg = low;
394 ioapic_write(io->io_addr, IOAPIC_REDTBL_LO(intpin->io_intpin), low);
398 ioapic_reprogram_intpin(struct intsrc *isrc)
401 mtx_lock_spin(&icu_lock);
402 ioapic_program_intpin((struct ioapic_intsrc *)isrc);
403 mtx_unlock_spin(&icu_lock);
407 ioapic_assign_cpu(struct intsrc *isrc, u_int apic_id)
409 struct ioapic_intsrc *intpin = (struct ioapic_intsrc *)isrc;
410 struct ioapic *io = (struct ioapic *)isrc->is_pic;
411 u_int old_vector, new_vector;
415 * keep 1st core as the destination for NMI
417 if (intpin->io_irq == IRQ_NMI)
421 * Set us up to free the old irq.
423 old_vector = intpin->io_vector;
424 old_id = intpin->io_cpu;
425 if (old_vector && apic_id == old_id)
429 * Allocate an APIC vector for this interrupt pin. Once
430 * we have a vector we program the interrupt pin.
432 new_vector = apic_alloc_vector(apic_id, intpin->io_irq);
437 * Mask the old intpin if it is enabled while it is migrated.
439 * At least some level-triggered interrupts seem to need the
440 * extra DELAY() to avoid being stuck in a non-EOI'd state.
442 mtx_lock_spin(&icu_lock);
443 if (!intpin->io_masked && !intpin->io_edgetrigger) {
444 ioapic_write(io->io_addr, IOAPIC_REDTBL_LO(intpin->io_intpin),
445 intpin->io_lowreg | IOART_INTMSET);
446 mtx_unlock_spin(&icu_lock);
448 mtx_lock_spin(&icu_lock);
451 intpin->io_cpu = apic_id;
452 intpin->io_vector = new_vector;
453 if (isrc->is_handlers > 0)
454 apic_enable_vector(intpin->io_cpu, intpin->io_vector);
456 printf("ioapic%u: routing intpin %u (", io->io_id,
458 ioapic_print_irq(intpin);
459 printf(") to lapic %u vector %u\n", intpin->io_cpu,
462 ioapic_program_intpin(intpin);
463 mtx_unlock_spin(&icu_lock);
466 * Free the old vector after the new one is established. This is done
467 * to prevent races where we could miss an interrupt.
470 if (isrc->is_handlers > 0)
471 apic_disable_vector(old_id, old_vector);
472 apic_free_vector(old_id, old_vector, intpin->io_irq);
478 ioapic_enable_intr(struct intsrc *isrc)
480 struct ioapic_intsrc *intpin = (struct ioapic_intsrc *)isrc;
482 if (intpin->io_vector == 0)
483 if (ioapic_assign_cpu(isrc, intr_next_cpu()) != 0)
484 panic("Couldn't find an APIC vector for IRQ %d",
486 apic_enable_vector(intpin->io_cpu, intpin->io_vector);
491 ioapic_disable_intr(struct intsrc *isrc)
493 struct ioapic_intsrc *intpin = (struct ioapic_intsrc *)isrc;
496 if (intpin->io_vector != 0) {
497 /* Mask this interrupt pin and free its APIC vector. */
498 vector = intpin->io_vector;
499 apic_disable_vector(intpin->io_cpu, vector);
500 mtx_lock_spin(&icu_lock);
501 intpin->io_masked = 1;
502 intpin->io_vector = 0;
503 ioapic_program_intpin(intpin);
504 mtx_unlock_spin(&icu_lock);
505 apic_free_vector(intpin->io_cpu, vector, intpin->io_irq);
510 ioapic_vector(struct intsrc *isrc)
512 struct ioapic_intsrc *pin;
514 pin = (struct ioapic_intsrc *)isrc;
515 return (pin->io_irq);
519 ioapic_source_pending(struct intsrc *isrc)
521 struct ioapic_intsrc *intpin = (struct ioapic_intsrc *)isrc;
523 if (intpin->io_vector == 0)
525 return (lapic_intr_pending(intpin->io_vector));
529 ioapic_config_intr(struct intsrc *isrc, enum intr_trigger trig,
530 enum intr_polarity pol)
532 struct ioapic_intsrc *intpin = (struct ioapic_intsrc *)isrc;
533 struct ioapic *io = (struct ioapic *)isrc->is_pic;
536 KASSERT(!(trig == INTR_TRIGGER_CONFORM || pol == INTR_POLARITY_CONFORM),
537 ("%s: Conforming trigger or polarity\n", __func__));
540 * EISA interrupts always use active high polarity, so don't allow
541 * them to be set to active low.
543 * XXX: Should we write to the ELCR if the trigger mode changes for
544 * an EISA IRQ or an ISA IRQ with the ELCR present?
546 mtx_lock_spin(&icu_lock);
547 if (intpin->io_bus == APIC_BUS_EISA)
548 pol = INTR_POLARITY_HIGH;
550 if (intpin->io_edgetrigger != (trig == INTR_TRIGGER_EDGE)) {
552 printf("ioapic%u: Changing trigger for pin %u to %s\n",
553 io->io_id, intpin->io_intpin,
554 trig == INTR_TRIGGER_EDGE ? "edge" : "level");
555 intpin->io_edgetrigger = (trig == INTR_TRIGGER_EDGE);
558 if (intpin->io_activehi != (pol == INTR_POLARITY_HIGH)) {
560 printf("ioapic%u: Changing polarity for pin %u to %s\n",
561 io->io_id, intpin->io_intpin,
562 pol == INTR_POLARITY_HIGH ? "high" : "low");
563 intpin->io_activehi = (pol == INTR_POLARITY_HIGH);
567 ioapic_program_intpin(intpin);
568 mtx_unlock_spin(&icu_lock);
573 ioapic_resume(struct pic *pic, bool suspend_cancelled)
575 struct ioapic *io = (struct ioapic *)pic;
578 mtx_lock_spin(&icu_lock);
579 for (i = 0; i < io->io_numintr; i++)
580 ioapic_program_intpin(&io->io_pins[i]);
581 mtx_unlock_spin(&icu_lock);
585 * Create a plain I/O APIC object.
588 ioapic_create(vm_paddr_t addr, int32_t apic_id, int intbase)
591 struct ioapic_intsrc *intpin;
592 volatile ioapic_t *apic;
596 /* Map the register window so we can access the device. */
597 apic = pmap_mapdev(addr, IOAPIC_MEM_REGION);
598 mtx_lock_spin(&icu_lock);
599 value = ioapic_read(apic, IOAPIC_VER);
600 mtx_unlock_spin(&icu_lock);
602 /* If it's version register doesn't seem to work, punt. */
603 if (value == 0xffffffff) {
604 pmap_unmapdev((vm_offset_t)apic, IOAPIC_MEM_REGION);
608 /* Determine the number of vectors and set the APIC ID. */
609 numintr = ((value & IOART_VER_MAXREDIR) >> MAXREDIRSHIFT) + 1;
610 io = malloc(sizeof(struct ioapic) +
611 numintr * sizeof(struct ioapic_intsrc), M_IOAPIC, M_WAITOK);
612 io->io_pic = ioapic_template;
613 mtx_lock_spin(&icu_lock);
614 io->io_id = next_id++;
615 io->io_apic_id = ioapic_read(apic, IOAPIC_ID) >> APIC_ID_SHIFT;
616 if (apic_id != -1 && io->io_apic_id != apic_id) {
617 ioapic_write(apic, IOAPIC_ID, apic_id << APIC_ID_SHIFT);
618 mtx_unlock_spin(&icu_lock);
619 io->io_apic_id = apic_id;
620 printf("ioapic%u: Changing APIC ID to %d\n", io->io_id,
623 mtx_unlock_spin(&icu_lock);
625 intbase = next_ioapic_base;
626 printf("ioapic%u: Assuming intbase of %d\n", io->io_id,
628 } else if (intbase != next_ioapic_base && bootverbose)
629 printf("ioapic%u: WARNING: intbase %d != expected base %d\n",
630 io->io_id, intbase, next_ioapic_base);
631 io->io_intbase = intbase;
632 next_ioapic_base = intbase + numintr;
633 io->io_numintr = numintr;
638 printf("ioapic%u: ver 0x%02x maxredir 0x%02x\n", io->io_id,
639 (value & IOART_VER_VERSION), (value & IOART_VER_MAXREDIR)
643 * The summary information about IO-APIC versions is taken from
644 * the Linux kernel source:
646 * 1Xh I/OAPIC or I/O(x)APIC which are not PCI 2.2 Compliant
647 * 2Xh I/O(x)APIC which is PCI 2.2 Compliant
649 * IO-APICs with version >= 0x20 have working EOIR register.
651 io->io_haseoi = (value & IOART_VER_VERSION) >= 0x20;
654 * Initialize pins. Start off with interrupts disabled. Default
655 * to active-hi and edge-triggered for ISA interrupts and active-lo
656 * and level-triggered for all others.
658 bzero(io->io_pins, sizeof(struct ioapic_intsrc) * numintr);
659 mtx_lock_spin(&icu_lock);
660 for (i = 0, intpin = io->io_pins; i < numintr; i++, intpin++) {
661 intpin->io_intsrc.is_pic = (struct pic *)io;
662 intpin->io_intpin = i;
663 intpin->io_irq = intbase + i;
666 * Assume that pin 0 on the first I/O APIC is an ExtINT pin.
667 * Assume that pins 1-15 are ISA interrupts and that all
668 * other pins are PCI interrupts.
670 if (intpin->io_irq == 0)
671 ioapic_set_extint(io, i);
672 else if (intpin->io_irq < IOAPIC_ISA_INTS) {
673 intpin->io_bus = APIC_BUS_ISA;
674 intpin->io_activehi = 1;
675 intpin->io_edgetrigger = 1;
676 intpin->io_masked = 1;
678 intpin->io_bus = APIC_BUS_PCI;
679 intpin->io_activehi = 0;
680 intpin->io_edgetrigger = 0;
681 intpin->io_masked = 1;
685 * Route interrupts to the BSP by default. Interrupts may
686 * be routed to other CPUs later after they are enabled.
688 intpin->io_cpu = PCPU_GET(apic_id);
689 value = ioapic_read(apic, IOAPIC_REDTBL_LO(i));
690 ioapic_write(apic, IOAPIC_REDTBL_LO(i), value | IOART_INTMSET);
692 /* dummy, but sets cookie */
693 mtx_unlock_spin(&icu_lock);
694 iommu_map_ioapic_intr(io->io_apic_id,
695 intpin->io_cpu, intpin->io_vector, intpin->io_edgetrigger,
696 intpin->io_activehi, intpin->io_irq,
697 &intpin->io_remap_cookie, NULL, NULL);
698 mtx_lock_spin(&icu_lock);
701 mtx_unlock_spin(&icu_lock);
707 ioapic_get_vector(void *cookie, u_int pin)
711 io = (struct ioapic *)cookie;
712 if (pin >= io->io_numintr)
714 return (io->io_pins[pin].io_irq);
718 ioapic_disable_pin(void *cookie, u_int pin)
722 io = (struct ioapic *)cookie;
723 if (pin >= io->io_numintr)
725 if (io->io_pins[pin].io_irq == IRQ_DISABLED)
727 io->io_pins[pin].io_irq = IRQ_DISABLED;
729 printf("ioapic%u: intpin %d disabled\n", io->io_id, pin);
734 ioapic_remap_vector(void *cookie, u_int pin, int vector)
738 io = (struct ioapic *)cookie;
739 if (pin >= io->io_numintr || vector < 0)
741 if (io->io_pins[pin].io_irq >= NUM_IO_INTS)
743 io->io_pins[pin].io_irq = vector;
745 printf("ioapic%u: Routing IRQ %d -> intpin %d\n", io->io_id,
751 ioapic_set_bus(void *cookie, u_int pin, int bus_type)
755 if (bus_type < 0 || bus_type > APIC_BUS_MAX)
757 io = (struct ioapic *)cookie;
758 if (pin >= io->io_numintr)
760 if (io->io_pins[pin].io_irq >= NUM_IO_INTS)
762 if (io->io_pins[pin].io_bus == bus_type)
764 io->io_pins[pin].io_bus = bus_type;
766 printf("ioapic%u: intpin %d bus %s\n", io->io_id, pin,
767 ioapic_bus_string(bus_type));
772 ioapic_set_nmi(void *cookie, u_int pin)
776 io = (struct ioapic *)cookie;
777 if (pin >= io->io_numintr)
779 if (io->io_pins[pin].io_irq == IRQ_NMI)
781 if (io->io_pins[pin].io_irq >= NUM_IO_INTS)
783 io->io_pins[pin].io_bus = APIC_BUS_UNKNOWN;
784 io->io_pins[pin].io_irq = IRQ_NMI;
785 io->io_pins[pin].io_masked = 0;
786 io->io_pins[pin].io_edgetrigger = 1;
787 io->io_pins[pin].io_activehi = 1;
789 printf("ioapic%u: Routing NMI -> intpin %d\n",
795 ioapic_set_smi(void *cookie, u_int pin)
799 io = (struct ioapic *)cookie;
800 if (pin >= io->io_numintr)
802 if (io->io_pins[pin].io_irq == IRQ_SMI)
804 if (io->io_pins[pin].io_irq >= NUM_IO_INTS)
806 io->io_pins[pin].io_bus = APIC_BUS_UNKNOWN;
807 io->io_pins[pin].io_irq = IRQ_SMI;
808 io->io_pins[pin].io_masked = 0;
809 io->io_pins[pin].io_edgetrigger = 1;
810 io->io_pins[pin].io_activehi = 1;
812 printf("ioapic%u: Routing SMI -> intpin %d\n",
818 ioapic_set_extint(void *cookie, u_int pin)
822 io = (struct ioapic *)cookie;
823 if (pin >= io->io_numintr)
825 if (io->io_pins[pin].io_irq == IRQ_EXTINT)
827 if (io->io_pins[pin].io_irq >= NUM_IO_INTS)
829 io->io_pins[pin].io_bus = APIC_BUS_UNKNOWN;
830 io->io_pins[pin].io_irq = IRQ_EXTINT;
832 io->io_pins[pin].io_masked = 0;
834 io->io_pins[pin].io_masked = 1;
835 io->io_pins[pin].io_edgetrigger = 1;
836 io->io_pins[pin].io_activehi = 1;
838 printf("ioapic%u: Routing external 8259A's -> intpin %d\n",
844 ioapic_set_polarity(void *cookie, u_int pin, enum intr_polarity pol)
849 io = (struct ioapic *)cookie;
850 if (pin >= io->io_numintr || pol == INTR_POLARITY_CONFORM)
852 if (io->io_pins[pin].io_irq >= NUM_IO_INTS)
854 activehi = (pol == INTR_POLARITY_HIGH);
855 if (io->io_pins[pin].io_activehi == activehi)
857 io->io_pins[pin].io_activehi = activehi;
859 printf("ioapic%u: intpin %d polarity: %s\n", io->io_id, pin,
860 pol == INTR_POLARITY_HIGH ? "high" : "low");
865 ioapic_set_triggermode(void *cookie, u_int pin, enum intr_trigger trigger)
870 io = (struct ioapic *)cookie;
871 if (pin >= io->io_numintr || trigger == INTR_TRIGGER_CONFORM)
873 if (io->io_pins[pin].io_irq >= NUM_IO_INTS)
875 edgetrigger = (trigger == INTR_TRIGGER_EDGE);
876 if (io->io_pins[pin].io_edgetrigger == edgetrigger)
878 io->io_pins[pin].io_edgetrigger = edgetrigger;
880 printf("ioapic%u: intpin %d trigger: %s\n", io->io_id, pin,
881 trigger == INTR_TRIGGER_EDGE ? "edge" : "level");
886 * Register a complete I/O APIC object with the interrupt subsystem.
889 ioapic_register(void *cookie)
891 struct ioapic_intsrc *pin;
893 volatile ioapic_t *apic;
897 io = (struct ioapic *)cookie;
899 mtx_lock_spin(&icu_lock);
900 flags = ioapic_read(apic, IOAPIC_VER) & IOART_VER_VERSION;
901 STAILQ_INSERT_TAIL(&ioapic_list, io, io_next);
902 mtx_unlock_spin(&icu_lock);
903 printf("ioapic%u <Version %u.%u> irqs %u-%u on motherboard\n",
904 io->io_id, flags >> 4, flags & 0xf, io->io_intbase,
905 io->io_intbase + io->io_numintr - 1);
907 /* Register valid pins as interrupt sources. */
908 intr_register_pic(&io->io_pic);
909 for (i = 0, pin = io->io_pins; i < io->io_numintr; i++, pin++)
910 if (pin->io_irq < NUM_IO_INTS)
911 intr_register_source(&pin->io_intsrc);
914 /* A simple new-bus driver to consume PCI I/O APIC devices. */
916 ioapic_pci_probe(device_t dev)
919 if (pci_get_class(dev) == PCIC_BASEPERIPH &&
920 pci_get_subclass(dev) == PCIS_BASEPERIPH_PIC) {
921 switch (pci_get_progif(dev)) {
922 case PCIP_BASEPERIPH_PIC_IO_APIC:
923 device_set_desc(dev, "IO APIC");
925 case PCIP_BASEPERIPH_PIC_IOX_APIC:
926 device_set_desc(dev, "IO(x) APIC");
938 ioapic_pci_attach(device_t dev)
944 static device_method_t ioapic_pci_methods[] = {
945 /* Device interface */
946 DEVMETHOD(device_probe, ioapic_pci_probe),
947 DEVMETHOD(device_attach, ioapic_pci_attach),
952 DEFINE_CLASS_0(ioapic, ioapic_pci_driver, ioapic_pci_methods, 0);
954 static devclass_t ioapic_devclass;
955 DRIVER_MODULE(ioapic, pci, ioapic_pci_driver, ioapic_devclass, 0, 0);
958 * A new-bus driver to consume the memory resources associated with
959 * the APICs in the system. On some systems ACPI or PnPBIOS system
960 * resource devices may already claim these resources. To keep from
961 * breaking those devices, we attach ourself to the nexus device after
962 * legacy0 and acpi0 and ignore any allocation failures.
965 apic_identify(driver_t *driver, device_t parent)
969 * Add at order 12. acpi0 is probed at order 10 and legacy0
970 * is probed at order 11.
972 if (lapic_paddr != 0)
973 BUS_ADD_CHILD(parent, 12, "apic", 0);
977 apic_probe(device_t dev)
980 device_set_desc(dev, "APIC resources");
986 apic_add_resource(device_t dev, int rid, vm_paddr_t base, size_t length)
990 error = bus_set_resource(dev, SYS_RES_MEMORY, rid, base, length);
992 panic("apic_add_resource: resource %d failed set with %d", rid,
994 bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, 0);
998 apic_attach(device_t dev)
1003 /* Reserve the local APIC. */
1004 apic_add_resource(dev, 0, lapic_paddr, LAPIC_MEM_REGION);
1006 STAILQ_FOREACH(io, &ioapic_list, io_next) {
1007 apic_add_resource(dev, i, io->io_paddr, IOAPIC_MEM_REGION);
1013 static device_method_t apic_methods[] = {
1014 /* Device interface */
1015 DEVMETHOD(device_identify, apic_identify),
1016 DEVMETHOD(device_probe, apic_probe),
1017 DEVMETHOD(device_attach, apic_attach),
1022 DEFINE_CLASS_0(apic, apic_driver, apic_methods, 0);
1024 static devclass_t apic_devclass;
1025 DRIVER_MODULE(apic, nexus, apic_driver, apic_devclass, 0, 0);
1027 #include "opt_ddb.h"
1030 #include <ddb/ddb.h>
1033 ioapic_delivery_mode(uint32_t mode)
1037 case IOART_DELFIXED:
1039 case IOART_DELLOPRI:
1040 return ("lowestpri");
1051 case IOART_DELEXINT:
1059 db_ioapic_read(volatile ioapic_t *apic, int reg)
1062 apic->ioregsel = reg;
1063 return (apic->iowin);
1067 db_show_ioapic_one(volatile ioapic_t *io_addr)
1072 r = db_ioapic_read(io_addr, IOAPIC_VER);
1073 mre = (r & IOART_VER_MAXREDIR) >> MAXREDIRSHIFT;
1074 db_printf("Id 0x%08x Ver 0x%02x MRE %d\n",
1075 db_ioapic_read(io_addr, IOAPIC_ID), r & IOART_VER_VERSION, mre);
1076 for (i = 0; i < mre; i++) {
1077 lo = db_ioapic_read(io_addr, IOAPIC_REDTBL_LO(i));
1078 hi = db_ioapic_read(io_addr, IOAPIC_REDTBL_HI(i));
1079 db_printf(" pin %d Dest %s/%x %smasked Trig %s RemoteIRR %d "
1080 "Polarity %s Status %s DeliveryMode %s Vec %d\n", i,
1081 (lo & IOART_DESTMOD) == IOART_DESTLOG ? "log" : "phy",
1082 (hi & IOART_DEST) >> 24,
1083 (lo & IOART_INTMASK) == IOART_INTMSET ? "" : "not",
1084 (lo & IOART_TRGRMOD) == IOART_TRGRLVL ? "lvl" : "edge",
1085 (lo & IOART_REM_IRR) == IOART_REM_IRR ? 1 : 0,
1086 (lo & IOART_INTPOL) == IOART_INTALO ? "low" : "high",
1087 (lo & IOART_DELIVS) == IOART_DELIVS ? "pend" : "idle",
1088 ioapic_delivery_mode(lo & IOART_DELMOD),
1089 (lo & IOART_INTVEC));
1093 DB_SHOW_COMMAND(ioapic, db_show_ioapic)
1095 struct ioapic *ioapic;
1099 db_printf("usage: show ioapic index\n");
1105 STAILQ_FOREACH(ioapic, &ioapic_list, io_next) {
1107 db_show_ioapic_one(ioapic->io_addr);
1114 DB_SHOW_ALL_COMMAND(ioapics, db_show_all_ioapics)
1116 struct ioapic *ioapic;
1118 STAILQ_FOREACH(ioapic, &ioapic_list, io_next)
1119 db_show_ioapic_one(ioapic->io_addr);