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 <sys/machintr.h>
33 #include <sys/malloc.h>
34 #include <sys/thread2.h>
36 #include <machine/pmap.h>
37 #include <machine_base/isa/isa_intr.h>
38 #include <machine_base/icu/icu_var.h>
39 #include <machine_base/apic/lapic.h>
40 #include <machine_base/apic/ioapic.h>
41 #include <machine_base/apic/ioapic_abi.h>
42 #include <machine_base/apic/apicvar.h>
44 #define IOAPIC_COUNT_MAX 16
45 #define IOAPIC_ID_MASK (IOAPIC_COUNT_MAX - 1)
54 TAILQ_ENTRY(ioapic_info) io_link;
56 TAILQ_HEAD(ioapic_info_list, ioapic_info);
58 struct ioapic_intsrc {
60 enum intr_trigger int_trig;
61 enum intr_polarity int_pola;
65 struct ioapic_info_list ioc_list;
66 struct ioapic_intsrc ioc_intsrc[ISA_IRQ_CNT];
69 static int ioapic_config(void);
70 static void ioapic_setup(const struct ioapic_info *);
71 static int ioapic_alloc_apic_id(int);
72 static void ioapic_set_apic_id(const struct ioapic_info *);
73 static void ioapic_gsi_setup(int);
74 static const struct ioapic_info *
75 ioapic_gsi_search(int);
76 static void ioapic_pin_prog(void *, int, int,
77 enum intr_trigger, enum intr_polarity, uint32_t, int);
79 static struct ioapic_conf ioapic_conf;
81 static TAILQ_HEAD(, ioapic_enumerator) ioapic_enumerators =
82 TAILQ_HEAD_INITIALIZER(ioapic_enumerators);
84 int ioapic_enable = -1; /* I/O APIC auto-enable mode */
89 struct ioapic_enumerator *e;
90 struct ioapic_info *info;
91 int start_apic_id = 0;
95 TAILQ_INIT(&ioapic_conf.ioc_list);
96 for (i = 0; i < ISA_IRQ_CNT; ++i)
97 ioapic_conf.ioc_intsrc[i].int_gsi = -1;
100 TUNABLE_INT_FETCH("hw.ioapic_probe", &probe);
102 kprintf("IOAPIC: warning I/O APIC will not be probed\n");
106 TAILQ_FOREACH(e, &ioapic_enumerators, ioapic_link) {
107 error = e->ioapic_probe(e);
112 kprintf("IOAPIC: can't find I/O APIC\n");
122 * Switch to I/O APIC MachIntrABI and reconfigure
123 * the default IDT entries.
125 MachIntrABI = MachIntrABI_IOAPIC;
126 MachIntrABI.setdefault();
128 e->ioapic_enumerate(e);
134 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link)
137 if (i > IOAPIC_COUNT_MAX)
138 panic("ioapic_config: more than 16 I/O APIC");
143 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link) {
146 apic_id = ioapic_alloc_apic_id(start_apic_id);
147 if (apic_id == NAPICID) {
148 kprintf("IOAPIC: can't alloc APIC ID for "
149 "%dth I/O APIC\n", info->io_idx);
152 info->io_apic_id = apic_id;
154 start_apic_id = apic_id + 1;
158 * xAPIC allows I/O APIC's APIC ID to be same
159 * as the LAPIC's APIC ID
161 kprintf("IOAPIC: use xAPIC model to alloc APIC ID "
164 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link)
165 info->io_apic_id = info->io_idx;
169 * Warning about any GSI holes
171 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link) {
172 const struct ioapic_info *prev_info;
174 prev_info = TAILQ_PREV(info, ioapic_info_list, io_link);
175 if (prev_info != NULL) {
176 if (info->io_gsi_base !=
177 prev_info->io_gsi_base + prev_info->io_npin) {
178 kprintf("IOAPIC: warning gsi hole "
180 prev_info->io_gsi_base +
182 info->io_gsi_base - 1);
188 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link) {
189 kprintf("IOAPIC: idx %d, apic id %d, "
190 "gsi base %d, npin %d\n",
201 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link)
203 ioapic_fixup_legacy_irqmaps();
207 MachIntrABI.cleanup();
215 ioapic_enumerator_register(struct ioapic_enumerator *ne)
217 struct ioapic_enumerator *e;
219 TAILQ_FOREACH(e, &ioapic_enumerators, ioapic_link) {
220 if (e->ioapic_prio < ne->ioapic_prio) {
221 TAILQ_INSERT_BEFORE(e, ne, ioapic_link);
225 TAILQ_INSERT_TAIL(&ioapic_enumerators, ne, ioapic_link);
229 ioapic_add(void *addr, int gsi_base, int npin)
231 struct ioapic_info *info, *ninfo;
234 gsi_end = gsi_base + npin - 1;
235 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link) {
236 if ((gsi_base >= info->io_gsi_base &&
237 gsi_base < info->io_gsi_base + info->io_npin) ||
238 (gsi_end >= info->io_gsi_base &&
239 gsi_end < info->io_gsi_base + info->io_npin)) {
240 panic("ioapic_add: overlapped gsi, base %d npin %d, "
241 "hit base %d, npin %d\n", gsi_base, npin,
242 info->io_gsi_base, info->io_npin);
244 if (info->io_addr == addr)
245 panic("ioapic_add: duplicated addr %p", addr);
248 ninfo = kmalloc(sizeof(*ninfo), M_DEVBUF, M_WAITOK | M_ZERO);
249 ninfo->io_addr = addr;
250 ninfo->io_npin = npin;
251 ninfo->io_gsi_base = gsi_base;
252 ninfo->io_apic_id = -1;
255 * Create IOAPIC list in ascending order of GSI base
257 TAILQ_FOREACH_REVERSE(info, &ioapic_conf.ioc_list,
258 ioapic_info_list, io_link) {
259 if (ninfo->io_gsi_base > info->io_gsi_base) {
260 TAILQ_INSERT_AFTER(&ioapic_conf.ioc_list,
261 info, ninfo, io_link);
266 TAILQ_INSERT_HEAD(&ioapic_conf.ioc_list, ninfo, io_link);
270 ioapic_intsrc(int irq, int gsi, enum intr_trigger trig, enum intr_polarity pola)
272 struct ioapic_intsrc *int_src;
274 KKASSERT(irq < ISA_IRQ_CNT);
275 int_src = &ioapic_conf.ioc_intsrc[irq];
278 /* Don't allow mixed mode */
279 kprintf("IOAPIC: warning intsrc irq %d -> gsi 0\n", irq);
283 if (int_src->int_gsi != -1) {
284 if (int_src->int_gsi != gsi) {
285 kprintf("IOAPIC: warning intsrc irq %d, gsi "
286 "%d -> %d\n", irq, int_src->int_gsi, gsi);
288 if (int_src->int_trig != trig) {
289 kprintf("IOAPIC: warning intsrc irq %d, trig "
291 intr_str_trigger(int_src->int_trig),
292 intr_str_trigger(trig));
294 if (int_src->int_pola != pola) {
295 kprintf("IOAPIC: warning intsrc irq %d, pola "
297 intr_str_polarity(int_src->int_pola),
298 intr_str_polarity(pola));
301 int_src->int_gsi = gsi;
302 int_src->int_trig = trig;
303 int_src->int_pola = pola;
307 ioapic_set_apic_id(const struct ioapic_info *info)
312 id = ioapic_read(info->io_addr, IOAPIC_ID);
315 id |= (info->io_apic_id << 24);
317 ioapic_write(info->io_addr, IOAPIC_ID, id);
322 id = ioapic_read(info->io_addr, IOAPIC_ID);
323 apic_id = (id & APIC_ID_MASK) >> 24;
326 * I/O APIC ID is a 4bits field
328 if ((apic_id & IOAPIC_ID_MASK) !=
329 (info->io_apic_id & IOAPIC_ID_MASK)) {
330 panic("ioapic_set_apic_id: can't set apic id to %d, "
331 "currently set to %d\n", info->io_apic_id, apic_id);
336 ioapic_gsi_setup(int gsi)
338 enum intr_trigger trig;
339 enum intr_polarity pola;
345 ioapic_extpin_setup(ioapic_gsi_ioaddr(gsi),
346 ioapic_gsi_pin(gsi), 0);
351 trig = 0; /* silence older gcc's */
352 pola = 0; /* silence older gcc's */
354 for (irq = 0; irq < ISA_IRQ_CNT; ++irq) {
355 const struct ioapic_intsrc *int_src =
356 &ioapic_conf.ioc_intsrc[irq];
358 if (gsi == int_src->int_gsi) {
359 trig = int_src->int_trig;
360 pola = int_src->int_pola;
365 if (irq == ISA_IRQ_CNT) {
367 * No explicit IRQ to GSI mapping;
368 * use the default 1:1 mapping
371 if (irq < ISA_IRQ_CNT) {
372 if (ioapic_conf.ioc_intsrc[irq].int_gsi >= 0) {
374 * This IRQ is mapped to different GSI,
375 * don't do the default configuration.
376 * The configuration of the target GSI
377 * will finally setup this IRQ.
379 * This GSI is not used, disable it.
382 ioapic_pin_setup(ioapic_gsi_ioaddr(gsi),
383 ioapic_gsi_pin(gsi), 0,
384 INTR_TRIGGER_EDGE, INTR_POLARITY_HIGH, 0);
388 trig = INTR_TRIGGER_EDGE;
389 pola = INTR_POLARITY_HIGH;
391 trig = INTR_TRIGGER_LEVEL;
392 pola = INTR_POLARITY_LOW;
396 ioapic_set_legacy_irqmap(irq, gsi, trig, pola);
400 ioapic_gsi_ioaddr(int gsi)
402 const struct ioapic_info *info;
404 info = ioapic_gsi_search(gsi);
405 return info->io_addr;
409 ioapic_gsi_pin(int gsi)
411 const struct ioapic_info *info;
413 info = ioapic_gsi_search(gsi);
414 return gsi - info->io_gsi_base;
417 static const struct ioapic_info *
418 ioapic_gsi_search(int gsi)
420 const struct ioapic_info *info;
422 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link) {
423 if (gsi >= info->io_gsi_base &&
424 gsi < info->io_gsi_base + info->io_npin)
427 panic("ioapic_gsi_search: no I/O APIC");
431 ioapic_gsi(int idx, int pin)
433 const struct ioapic_info *info;
435 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link) {
436 if (info->io_idx == idx)
441 if (pin >= info->io_npin)
443 return info->io_gsi_base + pin;
447 ioapic_extpin_setup(void *addr, int pin, int vec)
449 ioapic_pin_prog(addr, pin, vec,
450 INTR_TRIGGER_CONFORM, INTR_POLARITY_CONFORM, IOART_DELEXINT, 0);
454 ioapic_extpin_gsi(void)
460 ioapic_pin_setup(void *addr, int pin, int vec,
461 enum intr_trigger trig, enum intr_polarity pola, int cpuid)
464 * Always clear an I/O APIC pin before [re]programming it. This is
465 * particularly important if the pin is set up for a level interrupt
466 * as the IOART_REM_IRR bit might be set. When we reprogram the
467 * vector any EOI from pending ints on this pin could be lost and
468 * IRR might never get reset.
470 * To fix this problem, clear the vector and make sure it is
471 * programmed as an edge interrupt. This should theoretically
472 * clear IRR so we can later, safely program it as a level
475 ioapic_pin_prog(addr, pin, vec, INTR_TRIGGER_EDGE, INTR_POLARITY_HIGH,
476 IOART_DELFIXED, cpuid);
477 ioapic_pin_prog(addr, pin, vec, trig, pola, IOART_DELFIXED, cpuid);
481 ioapic_pin_prog(void *addr, int pin, int vec,
482 enum intr_trigger trig, enum intr_polarity pola,
483 uint32_t del_mode, int cpuid)
485 uint32_t flags, target;
488 KKASSERT(del_mode == IOART_DELEXINT || del_mode == IOART_DELFIXED);
490 select = IOAPIC_REDTBL0 + (2 * pin);
492 flags = ioapic_read(addr, select) & IOART_RESV;
493 flags |= IOART_INTMSET | IOART_DESTPHY;
498 * We only support limited I/O APIC mixed mode,
499 * so even for ExtINT, we still use "fixed"
502 flags |= IOART_DELFIXED;
505 if (del_mode == IOART_DELEXINT) {
506 KKASSERT(trig == INTR_TRIGGER_CONFORM &&
507 pola == INTR_POLARITY_CONFORM);
508 flags |= IOART_TRGREDG | IOART_INTAHI;
511 case INTR_TRIGGER_EDGE:
512 flags |= IOART_TRGREDG;
515 case INTR_TRIGGER_LEVEL:
516 flags |= IOART_TRGRLVL;
519 case INTR_TRIGGER_CONFORM:
520 panic("ioapic_pin_prog: trig conform is not "
524 case INTR_POLARITY_HIGH:
525 flags |= IOART_INTAHI;
528 case INTR_POLARITY_LOW:
529 flags |= IOART_INTALO;
532 case INTR_POLARITY_CONFORM:
533 panic("ioapic_pin_prog: pola conform is not "
538 target = ioapic_read(addr, select + 1) & IOART_HI_DEST_RESV;
539 target |= (CPUID_TO_APICID(cpuid) << IOART_HI_DEST_SHIFT) &
542 ioapic_write(addr, select, flags | vec);
543 ioapic_write(addr, select + 1, target);
547 ioapic_setup(const struct ioapic_info *info)
551 ioapic_set_apic_id(info);
553 for (i = 0; i < info->io_npin; ++i)
554 ioapic_gsi_setup(info->io_gsi_base + i);
558 ioapic_alloc_apic_id(int start)
561 const struct ioapic_info *info;
562 int apic_id, apic_id16;
564 apic_id = lapic_unused_apic_id(start);
565 if (apic_id == NAPICID) {
566 kprintf("IOAPIC: can't find unused APIC ID\n");
569 apic_id16 = apic_id & IOAPIC_ID_MASK;
572 * Check against other I/O APIC's APIC ID's lower 4bits.
574 * The new APIC ID will have to be different from others
575 * in the lower 4bits, no matter whether xAPIC is used
578 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link) {
579 if (info->io_apic_id == -1) {
583 if ((info->io_apic_id & IOAPIC_ID_MASK) == apic_id16)
589 kprintf("IOAPIC: APIC ID %d has same lower 4bits as "
590 "%dth I/O APIC, keep searching...\n",
591 apic_id, info->io_idx);
595 panic("ioapic_unused_apic_id: never reached");
599 * Map a physical memory address representing I/O into KVA. The I/O
600 * block is assumed not to cross a page boundary.
603 ioapic_map(vm_paddr_t pa)
605 KKASSERT(pa < 0x100000000LL);
607 return pmap_mapdev_uncacheable(pa, PAGE_SIZE);
611 ioapic_sysinit(void *dummy __unused)
618 KASSERT(lapic_enable, ("I/O APIC is enabled, but LAPIC is disabled"));
619 error = ioapic_config();
622 icu_reinit_noioapic();
623 lapic_fixup_noioapic();
626 SYSINIT(ioapic, SI_BOOT2_IOAPIC, SI_ORDER_FIRST, ioapic_sysinit, NULL);