2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2003 John Baldwin <jhb@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/systm.h>
37 #include <sys/kernel.h>
39 #include <sys/malloc.h>
40 #include <sys/module.h>
41 #include <sys/mutex.h>
43 #include <sys/sysctl.h>
45 #include <dev/pci/pcireg.h>
46 #include <dev/pci/pcivar.h>
51 #include <x86/apicreg.h>
52 #include <machine/frame.h>
53 #include <machine/intr_machdep.h>
54 #include <x86/apicvar.h>
55 #include <machine/resource.h>
56 #include <machine/segments.h>
57 #include <x86/iommu/iommu_intrmap.h>
59 #define IOAPIC_ISA_INTS 16
60 #define IOAPIC_MEM_REGION 32
61 #define IOAPIC_REDTBL_LO(i) (IOAPIC_REDTBL + (i) * 2)
62 #define IOAPIC_REDTBL_HI(i) (IOAPIC_REDTBL_LO(i) + 1)
64 static MALLOC_DEFINE(M_IOAPIC, "io_apic", "I/O APIC structures");
67 * I/O APIC interrupt source driver. Each pin is assigned an IRQ cookie
68 * as laid out in the ACPI System Interrupt number model where each I/O
69 * APIC has a contiguous chunk of the System Interrupt address space.
70 * We assume that IRQs 1 - 15 behave like ISA IRQs and that all other
71 * IRQs behave as PCI IRQs by default. We also assume that the pin for
72 * IRQ 0 is actually an ExtINT pin. The apic enumerators override the
73 * configuration of individual pins as indicated by their tables.
75 * Documentation for the I/O APIC: "82093AA I/O Advanced Programmable
76 * Interrupt Controller (IOAPIC)", May 1996, Intel Corp.
77 * ftp://download.intel.com/design/chipsets/datashts/29056601.pdf
80 struct ioapic_intsrc {
81 struct intsrc io_intsrc;
87 u_int io_edgetrigger:1;
91 u_int io_remap_cookie;
96 u_int io_id:8; /* logical ID */
97 u_int io_apic_id:8; /* Id as enumerated by MADT */
98 u_int io_hw_apic_id:8; /* Content of APIC ID register */
99 u_int io_intbase:8; /* System Interrupt base */
102 volatile ioapic_t *io_addr; /* XXX: should use bus_space */
104 STAILQ_ENTRY(ioapic) io_next;
105 device_t pci_dev; /* matched pci device, if found */
106 struct resource *pci_wnd; /* BAR 0, should be same or alias to
108 struct ioapic_intsrc io_pins[0];
111 static u_int ioapic_read(volatile ioapic_t *apic, int reg);
112 static void ioapic_write(volatile ioapic_t *apic, int reg, u_int val);
113 static const char *ioapic_bus_string(int bus_type);
114 static void ioapic_print_irq(struct ioapic_intsrc *intpin);
115 static void ioapic_register_sources(struct pic *pic);
116 static void ioapic_enable_source(struct intsrc *isrc);
117 static void ioapic_disable_source(struct intsrc *isrc, int eoi);
118 static void ioapic_eoi_source(struct intsrc *isrc);
119 static void ioapic_enable_intr(struct intsrc *isrc);
120 static void ioapic_disable_intr(struct intsrc *isrc);
121 static int ioapic_vector(struct intsrc *isrc);
122 static int ioapic_source_pending(struct intsrc *isrc);
123 static int ioapic_config_intr(struct intsrc *isrc, enum intr_trigger trig,
124 enum intr_polarity pol);
125 static void ioapic_resume(struct pic *pic, bool suspend_cancelled);
126 static int ioapic_assign_cpu(struct intsrc *isrc, u_int apic_id);
127 static void ioapic_program_intpin(struct ioapic_intsrc *intpin);
128 static void ioapic_reprogram_intpin(struct intsrc *isrc);
130 static STAILQ_HEAD(,ioapic) ioapic_list = STAILQ_HEAD_INITIALIZER(ioapic_list);
131 struct pic ioapic_template = {
132 .pic_register_sources = ioapic_register_sources,
133 .pic_enable_source = ioapic_enable_source,
134 .pic_disable_source = ioapic_disable_source,
135 .pic_eoi_source = ioapic_eoi_source,
136 .pic_enable_intr = ioapic_enable_intr,
137 .pic_disable_intr = ioapic_disable_intr,
138 .pic_vector = ioapic_vector,
139 .pic_source_pending = ioapic_source_pending,
141 .pic_resume = ioapic_resume,
142 .pic_config_intr = ioapic_config_intr,
143 .pic_assign_cpu = ioapic_assign_cpu,
144 .pic_reprogram_pin = ioapic_reprogram_intpin,
147 static u_int next_ioapic_base;
148 static u_int next_id;
150 static int enable_extint;
151 SYSCTL_INT(_hw_apic, OID_AUTO, enable_extint, CTLFLAG_RDTUN, &enable_extint, 0,
152 "Enable the ExtINT pin in the first I/O APIC");
155 _ioapic_eoi_source(struct intsrc *isrc, int locked)
157 struct ioapic_intsrc *src;
159 volatile uint32_t *apic_eoi;
163 if (!lapic_eoi_suppression)
165 src = (struct ioapic_intsrc *)isrc;
166 if (src->io_edgetrigger)
168 io = (struct ioapic *)isrc->is_pic;
171 * Handle targeted EOI for level-triggered pins, if broadcast
172 * EOI suppression is supported by LAPICs.
176 * If IOAPIC has EOI Register, simply write vector
177 * number into the reg.
179 apic_eoi = (volatile uint32_t *)((volatile char *)
180 io->io_addr + IOAPIC_EOIR);
181 *apic_eoi = src->io_vector;
184 * Otherwise, if IO-APIC is too old to provide EOIR,
185 * do what Intel did for the Linux kernel. Temporary
186 * switch the pin to edge-trigger and back, masking
187 * the pin during the trick.
190 mtx_lock_spin(&icu_lock);
191 low1 = src->io_lowreg;
192 low1 &= ~IOART_TRGRLVL;
193 low1 |= IOART_TRGREDG | IOART_INTMSET;
194 ioapic_write(io->io_addr, IOAPIC_REDTBL_LO(src->io_intpin),
196 low1 = src->io_lowreg;
197 if (src->io_masked != 0)
198 low1 |= IOART_INTMSET;
199 ioapic_write(io->io_addr, IOAPIC_REDTBL_LO(src->io_intpin),
202 mtx_unlock_spin(&icu_lock);
207 ioapic_read(volatile ioapic_t *apic, int reg)
210 mtx_assert(&icu_lock, MA_OWNED);
211 apic->ioregsel = reg;
212 return (apic->iowin);
216 ioapic_write(volatile ioapic_t *apic, int reg, u_int val)
219 mtx_assert(&icu_lock, MA_OWNED);
220 apic->ioregsel = reg;
225 ioapic_bus_string(int bus_type)
241 ioapic_print_irq(struct ioapic_intsrc *intpin)
244 switch (intpin->io_irq) {
258 printf("%s IRQ %d", ioapic_bus_string(intpin->io_bus),
264 ioapic_enable_source(struct intsrc *isrc)
266 struct ioapic_intsrc *intpin = (struct ioapic_intsrc *)isrc;
267 struct ioapic *io = (struct ioapic *)isrc->is_pic;
270 mtx_lock_spin(&icu_lock);
271 if (intpin->io_masked) {
272 flags = intpin->io_lowreg & ~IOART_INTMASK;
273 ioapic_write(io->io_addr, IOAPIC_REDTBL_LO(intpin->io_intpin),
275 intpin->io_masked = 0;
277 mtx_unlock_spin(&icu_lock);
281 ioapic_disable_source(struct intsrc *isrc, int eoi)
283 struct ioapic_intsrc *intpin = (struct ioapic_intsrc *)isrc;
284 struct ioapic *io = (struct ioapic *)isrc->is_pic;
287 mtx_lock_spin(&icu_lock);
288 if (!intpin->io_masked && !intpin->io_edgetrigger) {
289 flags = intpin->io_lowreg | IOART_INTMSET;
290 ioapic_write(io->io_addr, IOAPIC_REDTBL_LO(intpin->io_intpin),
292 intpin->io_masked = 1;
296 _ioapic_eoi_source(isrc, 1);
298 mtx_unlock_spin(&icu_lock);
302 ioapic_eoi_source(struct intsrc *isrc)
305 _ioapic_eoi_source(isrc, 0);
309 * Completely program an intpin based on the data in its interrupt source
313 ioapic_program_intpin(struct ioapic_intsrc *intpin)
315 struct ioapic *io = (struct ioapic *)intpin->io_intsrc.is_pic;
322 * If a pin is completely invalid or if it is valid but hasn't
323 * been enabled yet, just ensure that the pin is masked.
325 mtx_assert(&icu_lock, MA_OWNED);
326 if (intpin->io_irq == IRQ_DISABLED || (intpin->io_irq >= 0 &&
327 intpin->io_vector == 0)) {
328 low = ioapic_read(io->io_addr,
329 IOAPIC_REDTBL_LO(intpin->io_intpin));
330 if ((low & IOART_INTMASK) == IOART_INTMCLR)
331 ioapic_write(io->io_addr,
332 IOAPIC_REDTBL_LO(intpin->io_intpin),
333 low | IOART_INTMSET);
335 mtx_unlock_spin(&icu_lock);
336 iommu_unmap_ioapic_intr(io->io_apic_id,
337 &intpin->io_remap_cookie);
338 mtx_lock_spin(&icu_lock);
344 mtx_unlock_spin(&icu_lock);
345 error = iommu_map_ioapic_intr(io->io_apic_id,
346 intpin->io_cpu, intpin->io_vector, intpin->io_edgetrigger,
347 intpin->io_activehi, intpin->io_irq, &intpin->io_remap_cookie,
349 mtx_lock_spin(&icu_lock);
351 ioapic_write(io->io_addr, IOAPIC_REDTBL_HI(intpin->io_intpin),
353 intpin->io_lowreg = low;
354 ioapic_write(io->io_addr, IOAPIC_REDTBL_LO(intpin->io_intpin),
357 } else if (error != EOPNOTSUPP) {
363 * Set the destination. Note that with Intel interrupt remapping,
364 * the previously reserved bits 55:48 now have a purpose so ensure
368 high = intpin->io_cpu << APIC_ID_SHIFT;
370 /* Program the rest of the low word. */
371 if (intpin->io_edgetrigger)
372 low |= IOART_TRGREDG;
374 low |= IOART_TRGRLVL;
375 if (intpin->io_activehi)
379 if (intpin->io_masked)
380 low |= IOART_INTMSET;
381 switch (intpin->io_irq) {
383 KASSERT(intpin->io_edgetrigger,
384 ("ExtINT not edge triggered"));
385 low |= IOART_DELEXINT;
388 KASSERT(intpin->io_edgetrigger,
389 ("NMI not edge triggered"));
393 KASSERT(intpin->io_edgetrigger,
394 ("SMI not edge triggered"));
398 KASSERT(intpin->io_vector != 0, ("No vector for IRQ %u",
400 low |= IOART_DELFIXED | intpin->io_vector;
403 /* Write the values to the APIC. */
404 ioapic_write(io->io_addr, IOAPIC_REDTBL_HI(intpin->io_intpin), high);
405 intpin->io_lowreg = low;
406 ioapic_write(io->io_addr, IOAPIC_REDTBL_LO(intpin->io_intpin), low);
410 ioapic_reprogram_intpin(struct intsrc *isrc)
413 mtx_lock_spin(&icu_lock);
414 ioapic_program_intpin((struct ioapic_intsrc *)isrc);
415 mtx_unlock_spin(&icu_lock);
419 ioapic_assign_cpu(struct intsrc *isrc, u_int apic_id)
421 struct ioapic_intsrc *intpin = (struct ioapic_intsrc *)isrc;
422 struct ioapic *io = (struct ioapic *)isrc->is_pic;
423 u_int old_vector, new_vector;
428 * - Stick to the first cpu for all I/O APIC pins.
429 * - And don't allow destination cpu changes.
431 if (vm_guest == VM_GUEST_HV) {
432 if (intpin->io_vector)
439 * keep 1st core as the destination for NMI
441 if (intpin->io_irq == IRQ_NMI)
445 * Set us up to free the old irq.
447 old_vector = intpin->io_vector;
448 old_id = intpin->io_cpu;
449 if (old_vector && apic_id == old_id)
453 * Allocate an APIC vector for this interrupt pin. Once
454 * we have a vector we program the interrupt pin.
456 new_vector = apic_alloc_vector(apic_id, intpin->io_irq);
461 * Mask the old intpin if it is enabled while it is migrated.
463 * At least some level-triggered interrupts seem to need the
464 * extra DELAY() to avoid being stuck in a non-EOI'd state.
466 mtx_lock_spin(&icu_lock);
467 if (!intpin->io_masked && !intpin->io_edgetrigger) {
468 ioapic_write(io->io_addr, IOAPIC_REDTBL_LO(intpin->io_intpin),
469 intpin->io_lowreg | IOART_INTMSET);
470 mtx_unlock_spin(&icu_lock);
472 mtx_lock_spin(&icu_lock);
475 intpin->io_cpu = apic_id;
476 intpin->io_vector = new_vector;
477 if (isrc->is_handlers > 0)
478 apic_enable_vector(intpin->io_cpu, intpin->io_vector);
480 printf("ioapic%u: routing intpin %u (", io->io_id,
482 ioapic_print_irq(intpin);
483 printf(") to lapic %u vector %u\n", intpin->io_cpu,
486 ioapic_program_intpin(intpin);
487 mtx_unlock_spin(&icu_lock);
490 * Free the old vector after the new one is established. This is done
491 * to prevent races where we could miss an interrupt.
494 if (isrc->is_handlers > 0)
495 apic_disable_vector(old_id, old_vector);
496 apic_free_vector(old_id, old_vector, intpin->io_irq);
502 ioapic_enable_intr(struct intsrc *isrc)
504 struct ioapic_intsrc *intpin = (struct ioapic_intsrc *)isrc;
506 if (intpin->io_vector == 0)
507 if (ioapic_assign_cpu(isrc, intr_next_cpu(isrc->is_domain)) != 0)
508 panic("Couldn't find an APIC vector for IRQ %d",
510 apic_enable_vector(intpin->io_cpu, intpin->io_vector);
514 ioapic_disable_intr(struct intsrc *isrc)
516 struct ioapic_intsrc *intpin = (struct ioapic_intsrc *)isrc;
519 if (intpin->io_vector != 0) {
520 /* Mask this interrupt pin and free its APIC vector. */
521 vector = intpin->io_vector;
522 apic_disable_vector(intpin->io_cpu, vector);
523 mtx_lock_spin(&icu_lock);
524 intpin->io_masked = 1;
525 intpin->io_vector = 0;
526 ioapic_program_intpin(intpin);
527 mtx_unlock_spin(&icu_lock);
528 apic_free_vector(intpin->io_cpu, vector, intpin->io_irq);
533 ioapic_vector(struct intsrc *isrc)
535 struct ioapic_intsrc *pin;
537 pin = (struct ioapic_intsrc *)isrc;
538 return (pin->io_irq);
542 ioapic_source_pending(struct intsrc *isrc)
544 struct ioapic_intsrc *intpin = (struct ioapic_intsrc *)isrc;
546 if (intpin->io_vector == 0)
548 return (lapic_intr_pending(intpin->io_vector));
552 ioapic_config_intr(struct intsrc *isrc, enum intr_trigger trig,
553 enum intr_polarity pol)
555 struct ioapic_intsrc *intpin = (struct ioapic_intsrc *)isrc;
556 struct ioapic *io = (struct ioapic *)isrc->is_pic;
559 KASSERT(!(trig == INTR_TRIGGER_CONFORM || pol == INTR_POLARITY_CONFORM),
560 ("%s: Conforming trigger or polarity\n", __func__));
563 * EISA interrupts always use active high polarity, so don't allow
564 * them to be set to active low.
566 * XXX: Should we write to the ELCR if the trigger mode changes for
567 * an EISA IRQ or an ISA IRQ with the ELCR present?
569 mtx_lock_spin(&icu_lock);
570 if (intpin->io_bus == APIC_BUS_EISA)
571 pol = INTR_POLARITY_HIGH;
573 if (intpin->io_edgetrigger != (trig == INTR_TRIGGER_EDGE)) {
575 printf("ioapic%u: Changing trigger for pin %u to %s\n",
576 io->io_id, intpin->io_intpin,
577 trig == INTR_TRIGGER_EDGE ? "edge" : "level");
578 intpin->io_edgetrigger = (trig == INTR_TRIGGER_EDGE);
581 if (intpin->io_activehi != (pol == INTR_POLARITY_HIGH)) {
583 printf("ioapic%u: Changing polarity for pin %u to %s\n",
584 io->io_id, intpin->io_intpin,
585 pol == INTR_POLARITY_HIGH ? "high" : "low");
586 intpin->io_activehi = (pol == INTR_POLARITY_HIGH);
590 ioapic_program_intpin(intpin);
591 mtx_unlock_spin(&icu_lock);
596 ioapic_resume(struct pic *pic, bool suspend_cancelled)
598 struct ioapic *io = (struct ioapic *)pic;
601 mtx_lock_spin(&icu_lock);
602 for (i = 0; i < io->io_numintr; i++)
603 ioapic_program_intpin(&io->io_pins[i]);
604 mtx_unlock_spin(&icu_lock);
608 * Create a plain I/O APIC object.
611 ioapic_create(vm_paddr_t addr, int32_t apic_id, int intbase)
614 struct ioapic_intsrc *intpin;
615 volatile ioapic_t *apic;
619 /* Map the register window so we can access the device. */
620 apic = pmap_mapdev(addr, IOAPIC_MEM_REGION);
621 mtx_lock_spin(&icu_lock);
622 value = ioapic_read(apic, IOAPIC_VER);
623 mtx_unlock_spin(&icu_lock);
625 /* If it's version register doesn't seem to work, punt. */
626 if (value == 0xffffffff) {
627 pmap_unmapdev((vm_offset_t)apic, IOAPIC_MEM_REGION);
631 /* Determine the number of vectors and set the APIC ID. */
632 numintr = ((value & IOART_VER_MAXREDIR) >> MAXREDIRSHIFT) + 1;
633 io = malloc(sizeof(struct ioapic) +
634 numintr * sizeof(struct ioapic_intsrc), M_IOAPIC, M_WAITOK);
635 io->io_pic = ioapic_template;
638 mtx_lock_spin(&icu_lock);
639 io->io_id = next_id++;
640 io->io_hw_apic_id = ioapic_read(apic, IOAPIC_ID) >> APIC_ID_SHIFT;
641 io->io_apic_id = apic_id == -1 ? io->io_hw_apic_id : apic_id;
642 mtx_unlock_spin(&icu_lock);
643 if (io->io_hw_apic_id != apic_id)
644 printf("ioapic%u: MADT APIC ID %d != hw id %d\n", io->io_id,
645 apic_id, io->io_hw_apic_id);
647 intbase = next_ioapic_base;
648 printf("ioapic%u: Assuming intbase of %d\n", io->io_id,
650 } else if (intbase != next_ioapic_base && bootverbose)
651 printf("ioapic%u: WARNING: intbase %d != expected base %d\n",
652 io->io_id, intbase, next_ioapic_base);
653 io->io_intbase = intbase;
654 next_ioapic_base = intbase + numintr;
655 if (next_ioapic_base > num_io_irqs)
656 num_io_irqs = next_ioapic_base;
657 io->io_numintr = numintr;
662 printf("ioapic%u: ver 0x%02x maxredir 0x%02x\n", io->io_id,
663 (value & IOART_VER_VERSION), (value & IOART_VER_MAXREDIR)
667 * The summary information about IO-APIC versions is taken from
668 * the Linux kernel source:
670 * 1Xh I/OAPIC or I/O(x)APIC which are not PCI 2.2 Compliant
671 * 2Xh I/O(x)APIC which is PCI 2.2 Compliant
673 * IO-APICs with version >= 0x20 have working EOIR register.
675 io->io_haseoi = (value & IOART_VER_VERSION) >= 0x20;
678 * Initialize pins. Start off with interrupts disabled. Default
679 * to active-hi and edge-triggered for ISA interrupts and active-lo
680 * and level-triggered for all others.
682 bzero(io->io_pins, sizeof(struct ioapic_intsrc) * numintr);
683 mtx_lock_spin(&icu_lock);
684 for (i = 0, intpin = io->io_pins; i < numintr; i++, intpin++) {
685 intpin->io_intsrc.is_pic = (struct pic *)io;
686 intpin->io_intpin = i;
687 intpin->io_irq = intbase + i;
690 * Assume that pin 0 on the first I/O APIC is an ExtINT pin.
691 * Assume that pins 1-15 are ISA interrupts and that all
692 * other pins are PCI interrupts.
694 if (intpin->io_irq == 0)
695 ioapic_set_extint(io, i);
696 else if (intpin->io_irq < IOAPIC_ISA_INTS) {
697 intpin->io_bus = APIC_BUS_ISA;
698 intpin->io_activehi = 1;
699 intpin->io_edgetrigger = 1;
700 intpin->io_masked = 1;
702 intpin->io_bus = APIC_BUS_PCI;
703 intpin->io_activehi = 0;
704 intpin->io_edgetrigger = 0;
705 intpin->io_masked = 1;
709 * Route interrupts to the BSP by default. Interrupts may
710 * be routed to other CPUs later after they are enabled.
712 intpin->io_cpu = PCPU_GET(apic_id);
713 value = ioapic_read(apic, IOAPIC_REDTBL_LO(i));
714 ioapic_write(apic, IOAPIC_REDTBL_LO(i), value | IOART_INTMSET);
716 /* dummy, but sets cookie */
717 mtx_unlock_spin(&icu_lock);
718 iommu_map_ioapic_intr(io->io_apic_id,
719 intpin->io_cpu, intpin->io_vector, intpin->io_edgetrigger,
720 intpin->io_activehi, intpin->io_irq,
721 &intpin->io_remap_cookie, NULL, NULL);
722 mtx_lock_spin(&icu_lock);
725 mtx_unlock_spin(&icu_lock);
731 ioapic_get_vector(void *cookie, u_int pin)
735 io = (struct ioapic *)cookie;
736 if (pin >= io->io_numintr)
738 return (io->io_pins[pin].io_irq);
742 ioapic_disable_pin(void *cookie, u_int pin)
746 io = (struct ioapic *)cookie;
747 if (pin >= io->io_numintr)
749 if (io->io_pins[pin].io_irq == IRQ_DISABLED)
751 io->io_pins[pin].io_irq = IRQ_DISABLED;
753 printf("ioapic%u: intpin %d disabled\n", io->io_id, pin);
758 ioapic_remap_vector(void *cookie, u_int pin, int vector)
762 io = (struct ioapic *)cookie;
763 if (pin >= io->io_numintr || vector < 0)
765 if (io->io_pins[pin].io_irq < 0)
767 io->io_pins[pin].io_irq = vector;
769 printf("ioapic%u: Routing IRQ %d -> intpin %d\n", io->io_id,
775 ioapic_set_bus(void *cookie, u_int pin, int bus_type)
779 if (bus_type < 0 || bus_type > APIC_BUS_MAX)
781 io = (struct ioapic *)cookie;
782 if (pin >= io->io_numintr)
784 if (io->io_pins[pin].io_irq < 0)
786 if (io->io_pins[pin].io_bus == bus_type)
788 io->io_pins[pin].io_bus = bus_type;
790 printf("ioapic%u: intpin %d bus %s\n", io->io_id, pin,
791 ioapic_bus_string(bus_type));
796 ioapic_set_nmi(void *cookie, u_int pin)
800 io = (struct ioapic *)cookie;
801 if (pin >= io->io_numintr)
803 if (io->io_pins[pin].io_irq == IRQ_NMI)
805 if (io->io_pins[pin].io_irq < 0)
807 io->io_pins[pin].io_bus = APIC_BUS_UNKNOWN;
808 io->io_pins[pin].io_irq = IRQ_NMI;
809 io->io_pins[pin].io_masked = 0;
810 io->io_pins[pin].io_edgetrigger = 1;
811 io->io_pins[pin].io_activehi = 1;
813 printf("ioapic%u: Routing NMI -> intpin %d\n",
819 ioapic_set_smi(void *cookie, u_int pin)
823 io = (struct ioapic *)cookie;
824 if (pin >= io->io_numintr)
826 if (io->io_pins[pin].io_irq == IRQ_SMI)
828 if (io->io_pins[pin].io_irq < 0)
830 io->io_pins[pin].io_bus = APIC_BUS_UNKNOWN;
831 io->io_pins[pin].io_irq = IRQ_SMI;
832 io->io_pins[pin].io_masked = 0;
833 io->io_pins[pin].io_edgetrigger = 1;
834 io->io_pins[pin].io_activehi = 1;
836 printf("ioapic%u: Routing SMI -> intpin %d\n",
842 ioapic_set_extint(void *cookie, u_int pin)
846 io = (struct ioapic *)cookie;
847 if (pin >= io->io_numintr)
849 if (io->io_pins[pin].io_irq == IRQ_EXTINT)
851 if (io->io_pins[pin].io_irq < 0)
853 io->io_pins[pin].io_bus = APIC_BUS_UNKNOWN;
854 io->io_pins[pin].io_irq = IRQ_EXTINT;
856 io->io_pins[pin].io_masked = 0;
858 io->io_pins[pin].io_masked = 1;
859 io->io_pins[pin].io_edgetrigger = 1;
860 io->io_pins[pin].io_activehi = 1;
862 printf("ioapic%u: Routing external 8259A's -> intpin %d\n",
868 ioapic_set_polarity(void *cookie, u_int pin, enum intr_polarity pol)
873 io = (struct ioapic *)cookie;
874 if (pin >= io->io_numintr || pol == INTR_POLARITY_CONFORM)
876 if (io->io_pins[pin].io_irq < 0)
878 activehi = (pol == INTR_POLARITY_HIGH);
879 if (io->io_pins[pin].io_activehi == activehi)
881 io->io_pins[pin].io_activehi = activehi;
883 printf("ioapic%u: intpin %d polarity: %s\n", io->io_id, pin,
884 pol == INTR_POLARITY_HIGH ? "high" : "low");
889 ioapic_set_triggermode(void *cookie, u_int pin, enum intr_trigger trigger)
894 io = (struct ioapic *)cookie;
895 if (pin >= io->io_numintr || trigger == INTR_TRIGGER_CONFORM)
897 if (io->io_pins[pin].io_irq < 0)
899 edgetrigger = (trigger == INTR_TRIGGER_EDGE);
900 if (io->io_pins[pin].io_edgetrigger == edgetrigger)
902 io->io_pins[pin].io_edgetrigger = edgetrigger;
904 printf("ioapic%u: intpin %d trigger: %s\n", io->io_id, pin,
905 trigger == INTR_TRIGGER_EDGE ? "edge" : "level");
910 * Register a complete I/O APIC object with the interrupt subsystem.
913 ioapic_register(void *cookie)
915 struct ioapic_intsrc *pin;
917 volatile ioapic_t *apic;
921 io = (struct ioapic *)cookie;
923 mtx_lock_spin(&icu_lock);
924 flags = ioapic_read(apic, IOAPIC_VER) & IOART_VER_VERSION;
925 STAILQ_INSERT_TAIL(&ioapic_list, io, io_next);
926 mtx_unlock_spin(&icu_lock);
927 printf("ioapic%u <Version %u.%u> irqs %u-%u\n",
928 io->io_id, flags >> 4, flags & 0xf, io->io_intbase,
929 io->io_intbase + io->io_numintr - 1);
932 * Reprogram pins to handle special case pins (such as NMI and
933 * SMI) and disable normal pins until a handler is registered.
935 intr_register_pic(&io->io_pic);
936 for (i = 0, pin = io->io_pins; i < io->io_numintr; i++, pin++)
937 ioapic_reprogram_intpin(&pin->io_intsrc);
941 * Add interrupt sources for I/O APIC interrupt pins.
944 ioapic_register_sources(struct pic *pic)
946 struct ioapic_intsrc *pin;
950 io = (struct ioapic *)pic;
951 for (i = 0, pin = io->io_pins; i < io->io_numintr; i++, pin++) {
952 if (pin->io_irq >= 0)
953 intr_register_source(&pin->io_intsrc);
957 /* A simple new-bus driver to consume PCI I/O APIC devices. */
959 ioapic_pci_probe(device_t dev)
962 if (pci_get_class(dev) == PCIC_BASEPERIPH &&
963 pci_get_subclass(dev) == PCIS_BASEPERIPH_PIC) {
964 switch (pci_get_progif(dev)) {
965 case PCIP_BASEPERIPH_PIC_IO_APIC:
966 device_set_desc(dev, "IO APIC");
968 case PCIP_BASEPERIPH_PIC_IOX_APIC:
969 device_set_desc(dev, "IO(x) APIC");
981 ioapic_pci_attach(device_t dev)
983 struct resource *res;
984 volatile ioapic_t *apic;
990 * Try to match the enumerated ioapic. Match BAR start
991 * against io_paddr. Due to a fear that PCI window is not the
992 * same as the MADT reported io window, but an alias, read the
993 * APIC ID from the mapped BAR and match against it.
996 res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
997 RF_ACTIVE | RF_SHAREABLE);
1000 device_printf(dev, "cannot activate BAR0\n");
1003 apic = (volatile ioapic_t *)rman_get_virtual(res);
1004 if (rman_get_size(res) < IOAPIC_WND_SIZE) {
1007 "BAR0 too small (%jd) for IOAPIC window\n",
1008 (uintmax_t)rman_get_size(res));
1011 mtx_lock_spin(&icu_lock);
1012 apic_id = ioapic_read(apic, IOAPIC_ID) >> APIC_ID_SHIFT;
1013 /* First match by io window address */
1014 STAILQ_FOREACH(io, &ioapic_list, io_next) {
1015 if (io->io_paddr == (vm_paddr_t)rman_get_start(res))
1018 /* Then by apic id */
1019 STAILQ_FOREACH(io, &ioapic_list, io_next) {
1020 if (io->io_hw_apic_id == apic_id)
1023 mtx_unlock_spin(&icu_lock);
1026 "cannot match pci bar apic id %d against MADT, BAR0 %#jx\n",
1027 apic_id, (uintmax_t)rman_get_start(res));
1029 bus_release_resource(dev, SYS_RES_MEMORY, rid, res);
1032 KASSERT(io->pci_dev == NULL,
1033 ("ioapic %d pci_dev not NULL", io->io_id));
1034 KASSERT(io->pci_wnd == NULL,
1035 ("ioapic %d pci_wnd not NULL", io->io_id));
1039 if (bootverbose && (io->io_paddr != (vm_paddr_t)rman_get_start(res) ||
1040 io->io_hw_apic_id != apic_id)) {
1041 device_printf(dev, "pci%d:%d:%d:%d pci BAR0@%jx id %d "
1042 "MADT id %d hw id %d paddr@%jx\n",
1043 pci_get_domain(dev), pci_get_bus(dev),
1044 pci_get_slot(dev), pci_get_function(dev),
1045 (uintmax_t)rman_get_start(res), apic_id,
1046 io->io_apic_id, io->io_hw_apic_id, (uintmax_t)io->io_paddr);
1048 mtx_unlock_spin(&icu_lock);
1052 static device_method_t ioapic_pci_methods[] = {
1053 /* Device interface */
1054 DEVMETHOD(device_probe, ioapic_pci_probe),
1055 DEVMETHOD(device_attach, ioapic_pci_attach),
1059 DEFINE_CLASS_0(ioapic, ioapic_pci_driver, ioapic_pci_methods, 0);
1061 static devclass_t ioapic_devclass;
1062 DRIVER_MODULE(ioapic, pci, ioapic_pci_driver, ioapic_devclass, 0, 0);
1065 ioapic_get_rid(u_int apic_id, uint16_t *ridp)
1071 mtx_lock_spin(&icu_lock);
1072 STAILQ_FOREACH(io, &ioapic_list, io_next) {
1073 if (io->io_apic_id == apic_id)
1076 mtx_unlock_spin(&icu_lock);
1077 if (io == NULL || io->pci_dev == NULL)
1079 error = pci_get_id(io->pci_dev, PCI_ID_RID, &rid);
1087 * A new-bus driver to consume the memory resources associated with
1088 * the APICs in the system. On some systems ACPI or PnPBIOS system
1089 * resource devices may already claim these resources. To keep from
1090 * breaking those devices, we attach ourself to the nexus device after
1091 * legacy0 and acpi0 and ignore any allocation failures.
1094 apic_identify(driver_t *driver, device_t parent)
1098 * Add at order 12. acpi0 is probed at order 10 and legacy0
1099 * is probed at order 11.
1101 if (lapic_paddr != 0)
1102 BUS_ADD_CHILD(parent, 12, "apic", 0);
1106 apic_probe(device_t dev)
1109 device_set_desc(dev, "APIC resources");
1115 apic_add_resource(device_t dev, int rid, vm_paddr_t base, size_t length)
1119 error = bus_set_resource(dev, SYS_RES_MEMORY, rid, base, length);
1121 panic("apic_add_resource: resource %d failed set with %d", rid,
1123 bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_SHAREABLE);
1127 apic_attach(device_t dev)
1132 /* Reserve the local APIC. */
1133 apic_add_resource(dev, 0, lapic_paddr, LAPIC_MEM_REGION);
1135 STAILQ_FOREACH(io, &ioapic_list, io_next) {
1136 apic_add_resource(dev, i, io->io_paddr, IOAPIC_MEM_REGION);
1142 static device_method_t apic_methods[] = {
1143 /* Device interface */
1144 DEVMETHOD(device_identify, apic_identify),
1145 DEVMETHOD(device_probe, apic_probe),
1146 DEVMETHOD(device_attach, apic_attach),
1150 DEFINE_CLASS_0(apic, apic_driver, apic_methods, 0);
1152 static devclass_t apic_devclass;
1153 DRIVER_MODULE(apic, nexus, apic_driver, apic_devclass, 0, 0);
1155 #include "opt_ddb.h"
1158 #include <ddb/ddb.h>
1161 ioapic_delivery_mode(uint32_t mode)
1165 case IOART_DELFIXED:
1167 case IOART_DELLOPRI:
1168 return ("lowestpri");
1179 case IOART_DELEXINT:
1187 db_ioapic_read(volatile ioapic_t *apic, int reg)
1190 apic->ioregsel = reg;
1191 return (apic->iowin);
1195 db_show_ioapic_one(volatile ioapic_t *io_addr)
1200 r = db_ioapic_read(io_addr, IOAPIC_VER);
1201 mre = (r & IOART_VER_MAXREDIR) >> MAXREDIRSHIFT;
1202 db_printf("Id 0x%08x Ver 0x%02x MRE %d\n",
1203 db_ioapic_read(io_addr, IOAPIC_ID), r & IOART_VER_VERSION, mre);
1204 for (i = 0; i < mre; i++) {
1205 lo = db_ioapic_read(io_addr, IOAPIC_REDTBL_LO(i));
1206 hi = db_ioapic_read(io_addr, IOAPIC_REDTBL_HI(i));
1207 db_printf(" pin %d Dest %s/%x %smasked Trig %s RemoteIRR %d "
1208 "Polarity %s Status %s DeliveryMode %s Vec %d\n", i,
1209 (lo & IOART_DESTMOD) == IOART_DESTLOG ? "log" : "phy",
1210 (hi & IOART_DEST) >> 24,
1211 (lo & IOART_INTMASK) == IOART_INTMSET ? "" : "not",
1212 (lo & IOART_TRGRMOD) == IOART_TRGRLVL ? "lvl" : "edge",
1213 (lo & IOART_REM_IRR) == IOART_REM_IRR ? 1 : 0,
1214 (lo & IOART_INTPOL) == IOART_INTALO ? "low" : "high",
1215 (lo & IOART_DELIVS) == IOART_DELIVS ? "pend" : "idle",
1216 ioapic_delivery_mode(lo & IOART_DELMOD),
1217 (lo & IOART_INTVEC));
1221 DB_SHOW_COMMAND(ioapic, db_show_ioapic)
1223 struct ioapic *ioapic;
1227 db_printf("usage: show ioapic index\n");
1233 STAILQ_FOREACH(ioapic, &ioapic_list, io_next) {
1235 db_show_ioapic_one(ioapic->io_addr);
1242 DB_SHOW_ALL_COMMAND(ioapics, db_show_all_ioapics)
1244 struct ioapic *ioapic;
1246 STAILQ_FOREACH(ioapic, &ioapic_list, io_next)
1247 db_show_ioapic_one(ioapic->io_addr);