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.
13 * 3. Neither the name of the author nor the names of any co-contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include "opt_atpic.h"
36 #include <sys/param.h>
37 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/malloc.h>
42 #include <sys/mutex.h>
43 #include <sys/sysctl.h>
48 #include <machine/apicreg.h>
49 #include <machine/frame.h>
50 #include <machine/intr_machdep.h>
51 #include <machine/apicvar.h>
52 #include <machine/segments.h>
54 #define IOAPIC_ISA_INTS 16
55 #define IOAPIC_MEM_REGION 32
56 #define IOAPIC_REDTBL_LO(i) (IOAPIC_REDTBL + (i) * 2)
57 #define IOAPIC_REDTBL_HI(i) (IOAPIC_REDTBL_LO(i) + 1)
59 #define IRQ_EXTINT (NUM_IO_INTS + 1)
60 #define IRQ_NMI (NUM_IO_INTS + 2)
61 #define IRQ_SMI (NUM_IO_INTS + 3)
62 #define IRQ_DISABLED (NUM_IO_INTS + 4)
64 #define TODO printf("%s: not implemented!\n", __func__)
66 static MALLOC_DEFINE(M_IOAPIC, "io_apic", "I/O APIC structures");
69 * I/O APIC interrupt source driver. Each pin is assigned an IRQ cookie
70 * as laid out in the ACPI System Interrupt number model where each I/O
71 * APIC has a contiguous chunk of the System Interrupt address space.
72 * We assume that IRQs 1 - 15 behave like ISA IRQs and that all other
73 * IRQs behave as PCI IRQs by default. We also assume that the pin for
74 * IRQ 0 is actually an ExtINT pin. The apic enumerators override the
75 * configuration of individual pins as indicated by their tables.
78 struct ioapic_intsrc {
79 struct intsrc io_intsrc;
85 u_int io_edgetrigger:1;
92 u_int io_id:8; /* logical ID */
94 u_int io_intbase:8; /* System Interrupt base */
96 volatile ioapic_t *io_addr; /* XXX: should use bus_space */
97 STAILQ_ENTRY(ioapic) io_next;
98 struct ioapic_intsrc io_pins[0];
101 static u_int ioapic_read(volatile ioapic_t *apic, int reg);
102 static void ioapic_write(volatile ioapic_t *apic, int reg, u_int val);
103 static const char *ioapic_bus_string(int bus_type);
104 static void ioapic_print_irq(struct ioapic_intsrc *intpin);
105 static void ioapic_enable_source(struct intsrc *isrc);
106 static void ioapic_disable_source(struct intsrc *isrc, int eoi);
107 static void ioapic_eoi_source(struct intsrc *isrc);
108 static void ioapic_enable_intr(struct intsrc *isrc);
109 static int ioapic_vector(struct intsrc *isrc);
110 static int ioapic_source_pending(struct intsrc *isrc);
111 static int ioapic_config_intr(struct intsrc *isrc, enum intr_trigger trig,
112 enum intr_polarity pol);
113 static void ioapic_suspend(struct intsrc *isrc);
114 static void ioapic_resume(struct intsrc *isrc);
115 static void ioapic_assign_cpu(struct intsrc *isrc, u_int apic_id);
116 static void ioapic_program_intpin(struct ioapic_intsrc *intpin);
118 static STAILQ_HEAD(,ioapic) ioapic_list = STAILQ_HEAD_INITIALIZER(ioapic_list);
119 struct pic ioapic_template = { ioapic_enable_source, ioapic_disable_source,
120 ioapic_eoi_source, ioapic_enable_intr,
121 ioapic_vector, ioapic_source_pending,
122 ioapic_suspend, ioapic_resume,
123 ioapic_config_intr, ioapic_assign_cpu };
125 static int next_ioapic_base;
126 static u_int next_id;
128 SYSCTL_NODE(_hw, OID_AUTO, apic, CTLFLAG_RD, 0, "APIC options");
129 static int enable_extint;
130 SYSCTL_INT(_hw_apic, OID_AUTO, enable_extint, CTLFLAG_RDTUN, &enable_extint, 0,
131 "Enable the ExtINT pin in the first I/O APIC");
132 TUNABLE_INT("hw.apic.enable_extint", &enable_extint);
135 _ioapic_eoi_source(struct intsrc *isrc)
141 ioapic_read(volatile ioapic_t *apic, int reg)
144 mtx_assert(&icu_lock, MA_OWNED);
145 apic->ioregsel = reg;
146 return (apic->iowin);
150 ioapic_write(volatile ioapic_t *apic, int reg, u_int val)
153 mtx_assert(&icu_lock, MA_OWNED);
154 apic->ioregsel = reg;
159 ioapic_bus_string(int bus_type)
175 ioapic_print_irq(struct ioapic_intsrc *intpin)
178 switch (intpin->io_irq) {
192 printf("%s IRQ %u", ioapic_bus_string(intpin->io_bus),
198 ioapic_enable_source(struct intsrc *isrc)
200 struct ioapic_intsrc *intpin = (struct ioapic_intsrc *)isrc;
201 struct ioapic *io = (struct ioapic *)isrc->is_pic;
204 mtx_lock_spin(&icu_lock);
205 if (intpin->io_masked) {
206 flags = ioapic_read(io->io_addr,
207 IOAPIC_REDTBL_LO(intpin->io_intpin));
208 flags &= ~(IOART_INTMASK);
209 ioapic_write(io->io_addr, IOAPIC_REDTBL_LO(intpin->io_intpin),
211 intpin->io_masked = 0;
213 mtx_unlock_spin(&icu_lock);
217 ioapic_disable_source(struct intsrc *isrc, int eoi)
219 struct ioapic_intsrc *intpin = (struct ioapic_intsrc *)isrc;
220 struct ioapic *io = (struct ioapic *)isrc->is_pic;
223 mtx_lock_spin(&icu_lock);
224 if (!intpin->io_masked && !intpin->io_edgetrigger) {
225 flags = ioapic_read(io->io_addr,
226 IOAPIC_REDTBL_LO(intpin->io_intpin));
227 flags |= IOART_INTMSET;
228 ioapic_write(io->io_addr, IOAPIC_REDTBL_LO(intpin->io_intpin),
230 intpin->io_masked = 1;
234 _ioapic_eoi_source(isrc);
236 mtx_unlock_spin(&icu_lock);
240 ioapic_eoi_source(struct intsrc *isrc)
243 _ioapic_eoi_source(isrc);
247 * Completely program an intpin based on the data in its interrupt source
251 ioapic_program_intpin(struct ioapic_intsrc *intpin)
253 struct ioapic *io = (struct ioapic *)intpin->io_intsrc.is_pic;
254 uint32_t low, high, value;
257 * If a pin is completely invalid or if it is valid but hasn't
258 * been enabled yet, just ensure that the pin is masked.
260 if (intpin->io_irq == IRQ_DISABLED || (intpin->io_irq < NUM_IO_INTS &&
261 intpin->io_vector == 0)) {
262 mtx_lock_spin(&icu_lock);
263 low = ioapic_read(io->io_addr,
264 IOAPIC_REDTBL_LO(intpin->io_intpin));
265 if ((low & IOART_INTMASK) == IOART_INTMCLR)
266 ioapic_write(io->io_addr,
267 IOAPIC_REDTBL_LO(intpin->io_intpin),
268 low | IOART_INTMSET);
269 mtx_unlock_spin(&icu_lock);
273 /* Set the destination. */
275 high = intpin->io_cpu << APIC_ID_SHIFT;
277 /* Program the rest of the low word. */
278 if (intpin->io_edgetrigger)
279 low |= IOART_TRGREDG;
281 low |= IOART_TRGRLVL;
282 if (intpin->io_activehi)
286 if (intpin->io_masked)
287 low |= IOART_INTMSET;
288 switch (intpin->io_irq) {
290 KASSERT(intpin->io_edgetrigger,
291 ("ExtINT not edge triggered"));
292 low |= IOART_DELEXINT;
295 KASSERT(intpin->io_edgetrigger,
296 ("NMI not edge triggered"));
300 KASSERT(intpin->io_edgetrigger,
301 ("SMI not edge triggered"));
305 KASSERT(intpin->io_vector != 0, ("No vector for IRQ %u",
307 low |= IOART_DELFIXED | intpin->io_vector;
310 /* Write the values to the APIC. */
311 mtx_lock_spin(&icu_lock);
312 ioapic_write(io->io_addr, IOAPIC_REDTBL_LO(intpin->io_intpin), low);
313 value = ioapic_read(io->io_addr, IOAPIC_REDTBL_HI(intpin->io_intpin));
314 value &= ~IOART_DEST;
316 ioapic_write(io->io_addr, IOAPIC_REDTBL_HI(intpin->io_intpin), value);
317 mtx_unlock_spin(&icu_lock);
321 ioapic_assign_cpu(struct intsrc *isrc, u_int apic_id)
323 struct ioapic_intsrc *intpin = (struct ioapic_intsrc *)isrc;
324 struct ioapic *io = (struct ioapic *)isrc->is_pic;
326 intpin->io_cpu = apic_id;
328 printf("ioapic%u: Assigning ", io->io_id);
329 ioapic_print_irq(intpin);
330 printf(" to local APIC %u\n", intpin->io_cpu);
332 ioapic_program_intpin(intpin);
336 ioapic_enable_intr(struct intsrc *isrc)
338 struct ioapic_intsrc *intpin = (struct ioapic_intsrc *)isrc;
339 struct ioapic *io = (struct ioapic *)isrc->is_pic;
341 if (intpin->io_vector == 0) {
343 * Allocate an APIC vector for this interrupt pin. Once
344 * we have a vector we program the interrupt pin.
346 intpin->io_vector = apic_alloc_vector(intpin->io_irq);
348 printf("ioapic%u: routing intpin %u (", io->io_id,
350 ioapic_print_irq(intpin);
351 printf(") to vector %u\n", intpin->io_vector);
353 ioapic_program_intpin(intpin);
354 apic_enable_vector(intpin->io_vector);
359 ioapic_vector(struct intsrc *isrc)
361 struct ioapic_intsrc *pin;
363 pin = (struct ioapic_intsrc *)isrc;
364 return (pin->io_irq);
368 ioapic_source_pending(struct intsrc *isrc)
370 struct ioapic_intsrc *intpin = (struct ioapic_intsrc *)isrc;
372 if (intpin->io_vector == 0)
374 return (lapic_intr_pending(intpin->io_vector));
378 ioapic_config_intr(struct intsrc *isrc, enum intr_trigger trig,
379 enum intr_polarity pol)
381 struct ioapic_intsrc *intpin = (struct ioapic_intsrc *)isrc;
382 struct ioapic *io = (struct ioapic *)isrc->is_pic;
385 KASSERT(!(trig == INTR_TRIGGER_CONFORM || pol == INTR_POLARITY_CONFORM),
386 ("%s: Conforming trigger or polarity\n", __func__));
389 * EISA interrupts always use active high polarity, so don't allow
390 * them to be set to active low.
392 * XXX: Should we write to the ELCR if the trigger mode changes for
393 * an EISA IRQ or an ISA IRQ with the ELCR present?
395 if (intpin->io_bus == APIC_BUS_EISA)
396 pol = INTR_POLARITY_HIGH;
398 if (intpin->io_edgetrigger != (trig == INTR_TRIGGER_EDGE)) {
400 printf("ioapic%u: Changing trigger for pin %u to %s\n",
401 io->io_id, intpin->io_intpin,
402 trig == INTR_TRIGGER_EDGE ? "edge" : "level");
403 intpin->io_edgetrigger = (trig == INTR_TRIGGER_EDGE);
406 if (intpin->io_activehi != (pol == INTR_POLARITY_HIGH)) {
408 printf("ioapic%u: Changing polarity for pin %u to %s\n",
409 io->io_id, intpin->io_intpin,
410 pol == INTR_POLARITY_HIGH ? "high" : "low");
411 intpin->io_activehi = (pol == INTR_POLARITY_HIGH);
415 ioapic_program_intpin(intpin);
420 ioapic_suspend(struct intsrc *isrc)
427 ioapic_resume(struct intsrc *isrc)
430 ioapic_program_intpin((struct ioapic_intsrc *)isrc);
434 * Create a plain I/O APIC object.
437 ioapic_create(uintptr_t addr, int32_t apic_id, int intbase)
440 struct ioapic_intsrc *intpin;
441 volatile ioapic_t *apic;
445 /* Map the register window so we can access the device. */
446 apic = (ioapic_t *)pmap_mapdev(addr, IOAPIC_MEM_REGION);
447 mtx_lock_spin(&icu_lock);
448 value = ioapic_read(apic, IOAPIC_VER);
449 mtx_unlock_spin(&icu_lock);
451 /* If it's version register doesn't seem to work, punt. */
452 if (value == 0xffffffff) {
453 pmap_unmapdev((vm_offset_t)apic, IOAPIC_MEM_REGION);
457 /* Determine the number of vectors and set the APIC ID. */
458 numintr = ((value & IOART_VER_MAXREDIR) >> MAXREDIRSHIFT) + 1;
459 io = malloc(sizeof(struct ioapic) +
460 numintr * sizeof(struct ioapic_intsrc), M_IOAPIC, M_WAITOK);
461 io->io_pic = ioapic_template;
462 mtx_lock_spin(&icu_lock);
463 io->io_id = next_id++;
464 io->io_apic_id = ioapic_read(apic, IOAPIC_ID) >> APIC_ID_SHIFT;
465 if (apic_id != -1 && io->io_apic_id != apic_id) {
466 ioapic_write(apic, IOAPIC_ID, apic_id << APIC_ID_SHIFT);
467 mtx_unlock_spin(&icu_lock);
468 io->io_apic_id = apic_id;
469 printf("ioapic%u: Changing APIC ID to %d\n", io->io_id,
472 mtx_unlock_spin(&icu_lock);
474 intbase = next_ioapic_base;
475 printf("ioapic%u: Assuming intbase of %d\n", io->io_id,
477 } else if (intbase != next_ioapic_base)
478 printf("ioapic%u: WARNING: intbase %d != expected base %d\n",
479 io->io_id, intbase, next_ioapic_base);
480 io->io_intbase = intbase;
481 next_ioapic_base = intbase + numintr;
482 io->io_numintr = numintr;
486 * Initialize pins. Start off with interrupts disabled. Default
487 * to active-hi and edge-triggered for ISA interrupts and active-lo
488 * and level-triggered for all others.
490 bzero(io->io_pins, sizeof(struct ioapic_intsrc) * numintr);
491 mtx_lock_spin(&icu_lock);
492 for (i = 0, intpin = io->io_pins; i < numintr; i++, intpin++) {
493 intpin->io_intsrc.is_pic = (struct pic *)io;
494 intpin->io_intpin = i;
495 intpin->io_irq = intbase + i;
498 * Assume that pin 0 on the first I/O APIC is an ExtINT pin.
499 * Assume that pins 1-15 are ISA interrupts and that all
500 * other pins are PCI interrupts.
502 if (intpin->io_irq == 0)
503 ioapic_set_extint(io, i);
504 else if (intpin->io_irq < IOAPIC_ISA_INTS) {
505 intpin->io_bus = APIC_BUS_ISA;
506 intpin->io_activehi = 1;
507 intpin->io_edgetrigger = 1;
508 intpin->io_masked = 1;
510 intpin->io_bus = APIC_BUS_PCI;
511 intpin->io_activehi = 0;
512 intpin->io_edgetrigger = 0;
513 intpin->io_masked = 1;
517 * Route interrupts to the BSP by default. Interrupts may
518 * be routed to other CPUs later after they are enabled.
520 intpin->io_cpu = PCPU_GET(apic_id);
521 if (bootverbose && intpin->io_irq != IRQ_DISABLED) {
522 printf("ioapic%u: intpin %d -> ", io->io_id, i);
523 ioapic_print_irq(intpin);
524 printf(" (%s, %s)\n", intpin->io_edgetrigger ?
525 "edge" : "level", intpin->io_activehi ? "high" :
528 value = ioapic_read(apic, IOAPIC_REDTBL_LO(i));
529 ioapic_write(apic, IOAPIC_REDTBL_LO(i), value | IOART_INTMSET);
531 mtx_unlock_spin(&icu_lock);
537 ioapic_get_vector(void *cookie, u_int pin)
541 io = (struct ioapic *)cookie;
542 if (pin >= io->io_numintr)
544 return (io->io_pins[pin].io_irq);
548 ioapic_disable_pin(void *cookie, u_int pin)
552 io = (struct ioapic *)cookie;
553 if (pin >= io->io_numintr)
555 if (io->io_pins[pin].io_irq == IRQ_DISABLED)
557 io->io_pins[pin].io_irq = IRQ_DISABLED;
559 printf("ioapic%u: intpin %d disabled\n", io->io_id, pin);
564 ioapic_remap_vector(void *cookie, u_int pin, int vector)
568 io = (struct ioapic *)cookie;
569 if (pin >= io->io_numintr || vector < 0)
571 if (io->io_pins[pin].io_irq >= NUM_IO_INTS)
573 io->io_pins[pin].io_irq = vector;
575 printf("ioapic%u: Routing IRQ %d -> intpin %d\n", io->io_id,
581 ioapic_set_bus(void *cookie, u_int pin, int bus_type)
585 if (bus_type < 0 || bus_type > APIC_BUS_MAX)
587 io = (struct ioapic *)cookie;
588 if (pin >= io->io_numintr)
590 if (io->io_pins[pin].io_irq >= NUM_IO_INTS)
592 io->io_pins[pin].io_bus = bus_type;
594 printf("ioapic%u: intpin %d bus %s\n", io->io_id, pin,
595 ioapic_bus_string(bus_type));
600 ioapic_set_nmi(void *cookie, u_int pin)
604 io = (struct ioapic *)cookie;
605 if (pin >= io->io_numintr)
607 if (io->io_pins[pin].io_irq == IRQ_NMI)
609 if (io->io_pins[pin].io_irq >= NUM_IO_INTS)
611 io->io_pins[pin].io_bus = APIC_BUS_UNKNOWN;
612 io->io_pins[pin].io_irq = IRQ_NMI;
613 io->io_pins[pin].io_masked = 0;
614 io->io_pins[pin].io_edgetrigger = 1;
615 io->io_pins[pin].io_activehi = 1;
617 printf("ioapic%u: Routing NMI -> intpin %d\n",
623 ioapic_set_smi(void *cookie, u_int pin)
627 io = (struct ioapic *)cookie;
628 if (pin >= io->io_numintr)
630 if (io->io_pins[pin].io_irq == IRQ_SMI)
632 if (io->io_pins[pin].io_irq >= NUM_IO_INTS)
634 io->io_pins[pin].io_bus = APIC_BUS_UNKNOWN;
635 io->io_pins[pin].io_irq = IRQ_SMI;
636 io->io_pins[pin].io_masked = 0;
637 io->io_pins[pin].io_edgetrigger = 1;
638 io->io_pins[pin].io_activehi = 1;
640 printf("ioapic%u: Routing SMI -> intpin %d\n",
646 ioapic_set_extint(void *cookie, u_int pin)
650 io = (struct ioapic *)cookie;
651 if (pin >= io->io_numintr)
653 if (io->io_pins[pin].io_irq == IRQ_EXTINT)
655 if (io->io_pins[pin].io_irq >= NUM_IO_INTS)
657 io->io_pins[pin].io_bus = APIC_BUS_UNKNOWN;
658 io->io_pins[pin].io_irq = IRQ_EXTINT;
660 io->io_pins[pin].io_masked = 0;
662 io->io_pins[pin].io_masked = 1;
663 io->io_pins[pin].io_edgetrigger = 1;
664 io->io_pins[pin].io_activehi = 1;
666 printf("ioapic%u: Routing external 8259A's -> intpin %d\n",
672 ioapic_set_polarity(void *cookie, u_int pin, enum intr_polarity pol)
676 io = (struct ioapic *)cookie;
677 if (pin >= io->io_numintr || pol == INTR_POLARITY_CONFORM)
679 if (io->io_pins[pin].io_irq >= NUM_IO_INTS)
681 io->io_pins[pin].io_activehi = (pol == INTR_POLARITY_HIGH);
683 printf("ioapic%u: intpin %d polarity: %s\n", io->io_id, pin,
684 pol == INTR_POLARITY_HIGH ? "high" : "low");
689 ioapic_set_triggermode(void *cookie, u_int pin, enum intr_trigger trigger)
693 io = (struct ioapic *)cookie;
694 if (pin >= io->io_numintr || trigger == INTR_TRIGGER_CONFORM)
696 if (io->io_pins[pin].io_irq >= NUM_IO_INTS)
698 io->io_pins[pin].io_edgetrigger = (trigger == INTR_TRIGGER_EDGE);
700 printf("ioapic%u: intpin %d trigger: %s\n", io->io_id, pin,
701 trigger == INTR_TRIGGER_EDGE ? "edge" : "level");
706 * Register a complete I/O APIC object with the interrupt subsystem.
709 ioapic_register(void *cookie)
711 struct ioapic_intsrc *pin;
713 volatile ioapic_t *apic;
717 io = (struct ioapic *)cookie;
719 mtx_lock_spin(&icu_lock);
720 flags = ioapic_read(apic, IOAPIC_VER) & IOART_VER_VERSION;
721 STAILQ_INSERT_TAIL(&ioapic_list, io, io_next);
722 mtx_unlock_spin(&icu_lock);
723 printf("ioapic%u <Version %u.%u> irqs %u-%u on motherboard\n",
724 io->io_id, flags >> 4, flags & 0xf, io->io_intbase,
725 io->io_intbase + io->io_numintr - 1);
727 /* Register valid pins as interrupt sources. */
728 for (i = 0, pin = io->io_pins; i < io->io_numintr; i++, pin++)
729 if (pin->io_irq < NUM_IO_INTS)
730 intr_register_source(&pin->io_intsrc);