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>
34 #include <machine/globaldata.h>
35 #include <machine/smp.h>
36 #include <machine/cputypes.h>
37 #include <machine/md_var.h>
38 #include <machine/pmap.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/segments.h>
43 #include <sys/thread2.h>
45 #include <machine/intr_machdep.h>
47 #define IOAPIC_COUNT_MAX 16
48 #define IOAPIC_ID_MASK (IOAPIC_COUNT_MAX - 1)
51 extern pt_entry_t *SMPpt;
60 TAILQ_ENTRY(ioapic_info) io_link;
62 TAILQ_HEAD(ioapic_info_list, ioapic_info);
64 struct ioapic_intsrc {
66 enum intr_trigger int_trig;
67 enum intr_polarity int_pola;
71 struct ioapic_info_list ioc_list;
72 struct ioapic_intsrc ioc_intsrc[16]; /* XXX magic number */
75 static void ioapic_setup(const struct ioapic_info *);
76 static int ioapic_alloc_apic_id(int);
77 static void ioapic_set_apic_id(const struct ioapic_info *);
78 static void ioapic_gsi_setup(int);
79 static const struct ioapic_info *
80 ioapic_gsi_search(int);
81 static void ioapic_pin_prog(void *, int, int,
82 enum intr_trigger, enum intr_polarity, uint32_t);
84 static struct ioapic_conf ioapic_conf;
86 static TAILQ_HEAD(, ioapic_enumerator) ioapic_enumerators =
87 TAILQ_HEAD_INITIALIZER(ioapic_enumerators);
92 struct ioapic_info *info;
93 int start_apic_id = 0;
94 struct ioapic_enumerator *e;
98 TAILQ_INIT(&ioapic_conf.ioc_list);
99 /* XXX magic number */
100 for (i = 0; i < 16; ++i)
101 ioapic_conf.ioc_intsrc[i].int_gsi = -1;
104 TUNABLE_INT_FETCH("hw.ioapic_probe", &probe);
106 kprintf("IOAPIC: warning I/O APIC will not be probed\n");
110 TAILQ_FOREACH(e, &ioapic_enumerators, ioapic_link) {
111 error = e->ioapic_probe(e);
116 kprintf("IOAPIC: can't find I/O APIC\n");
126 * Switch to I/O APIC MachIntrABI and reconfigure
127 * the default IDT entries.
129 MachIntrABI = MachIntrABI_IOAPIC;
130 MachIntrABI.setdefault();
132 e->ioapic_enumerate(e);
138 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link)
141 if (i > IOAPIC_COUNT_MAX) /* XXX magic number */
142 panic("ioapic_config: more than 16 I/O APIC\n");
147 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link) {
150 apic_id = ioapic_alloc_apic_id(start_apic_id);
151 if (apic_id == NAPICID) {
152 kprintf("IOAPIC: can't alloc APIC ID for "
153 "%dth I/O APIC\n", info->io_idx);
156 info->io_apic_id = apic_id;
158 start_apic_id = apic_id + 1;
162 * xAPIC allows I/O APIC's APIC ID to be same
163 * as the LAPIC's APIC ID
165 kprintf("IOAPIC: use xAPIC model to alloc APIC ID "
168 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link)
169 info->io_apic_id = info->io_idx;
173 * Warning about any GSI holes
175 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link) {
176 const struct ioapic_info *prev_info;
178 prev_info = TAILQ_PREV(info, ioapic_info_list, io_link);
179 if (prev_info != NULL) {
180 if (info->io_gsi_base !=
181 prev_info->io_gsi_base + prev_info->io_npin) {
182 kprintf("IOAPIC: warning gsi hole "
184 prev_info->io_gsi_base +
186 info->io_gsi_base - 1);
192 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link) {
193 kprintf("IOAPIC: idx %d, apic id %d, "
194 "gsi base %d, npin %d\n",
205 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link)
207 ioapic_abi_fixup_irqmap();
211 MachIntrABI.cleanup();
219 ioapic_enumerator_register(struct ioapic_enumerator *ne)
221 struct ioapic_enumerator *e;
223 TAILQ_FOREACH(e, &ioapic_enumerators, ioapic_link) {
224 if (e->ioapic_prio < ne->ioapic_prio) {
225 TAILQ_INSERT_BEFORE(e, ne, ioapic_link);
229 TAILQ_INSERT_TAIL(&ioapic_enumerators, ne, ioapic_link);
233 ioapic_add(void *addr, int gsi_base, int npin)
235 struct ioapic_info *info, *ninfo;
238 gsi_end = gsi_base + npin - 1;
239 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link) {
240 if ((gsi_base >= info->io_gsi_base &&
241 gsi_base < info->io_gsi_base + info->io_npin) ||
242 (gsi_end >= info->io_gsi_base &&
243 gsi_end < info->io_gsi_base + info->io_npin)) {
244 panic("ioapic_add: overlapped gsi, base %d npin %d, "
245 "hit base %d, npin %d\n", gsi_base, npin,
246 info->io_gsi_base, info->io_npin);
248 if (info->io_addr == addr)
249 panic("ioapic_add: duplicated addr %p\n", addr);
252 ninfo = kmalloc(sizeof(*ninfo), M_DEVBUF, M_WAITOK | M_ZERO);
253 ninfo->io_addr = addr;
254 ninfo->io_npin = npin;
255 ninfo->io_gsi_base = gsi_base;
256 ninfo->io_apic_id = -1;
259 * Create IOAPIC list in ascending order of GSI base
261 TAILQ_FOREACH_REVERSE(info, &ioapic_conf.ioc_list,
262 ioapic_info_list, io_link) {
263 if (ninfo->io_gsi_base > info->io_gsi_base) {
264 TAILQ_INSERT_AFTER(&ioapic_conf.ioc_list,
265 info, ninfo, io_link);
270 TAILQ_INSERT_HEAD(&ioapic_conf.ioc_list, ninfo, io_link);
274 ioapic_intsrc(int irq, int gsi, enum intr_trigger trig, enum intr_polarity pola)
276 struct ioapic_intsrc *int_src;
279 int_src = &ioapic_conf.ioc_intsrc[irq];
282 /* Don't allow mixed mode */
283 kprintf("IOAPIC: warning intsrc irq %d -> gsi 0\n", irq);
287 if (int_src->int_gsi != -1) {
288 if (int_src->int_gsi != gsi) {
289 kprintf("IOAPIC: warning intsrc irq %d, gsi "
290 "%d -> %d\n", irq, int_src->int_gsi, gsi);
292 if (int_src->int_trig != trig) {
293 kprintf("IOAPIC: warning intsrc irq %d, trig "
295 intr_str_trigger(int_src->int_trig),
296 intr_str_trigger(trig));
298 if (int_src->int_pola != pola) {
299 kprintf("IOAPIC: warning intsrc irq %d, pola "
301 intr_str_polarity(int_src->int_pola),
302 intr_str_polarity(pola));
305 int_src->int_gsi = gsi;
306 int_src->int_trig = trig;
307 int_src->int_pola = pola;
311 ioapic_set_apic_id(const struct ioapic_info *info)
316 id = ioapic_read(info->io_addr, IOAPIC_ID);
319 id |= (info->io_apic_id << 24);
321 ioapic_write(info->io_addr, IOAPIC_ID, id);
326 id = ioapic_read(info->io_addr, IOAPIC_ID);
327 apic_id = (id & APIC_ID_MASK) >> 24;
330 * I/O APIC ID is a 4bits field
332 if ((apic_id & IOAPIC_ID_MASK) !=
333 (info->io_apic_id & IOAPIC_ID_MASK)) {
334 panic("ioapic_set_apic_id: can't set apic id to %d, "
335 "currently set to %d\n", info->io_apic_id, apic_id);
340 ioapic_gsi_setup(int gsi)
342 enum intr_trigger trig;
343 enum intr_polarity pola;
349 ioapic_extpin_setup(ioapic_gsi_ioaddr(gsi),
350 ioapic_gsi_pin(gsi), 0);
355 trig = 0; /* silence older gcc's */
356 pola = 0; /* silence older gcc's */
358 for (irq = 0; irq < 16; ++irq) {
359 const struct ioapic_intsrc *int_src =
360 &ioapic_conf.ioc_intsrc[irq];
362 if (gsi == int_src->int_gsi) {
363 trig = int_src->int_trig;
364 pola = int_src->int_pola;
371 trig = INTR_TRIGGER_EDGE;
372 pola = INTR_POLARITY_HIGH;
374 trig = INTR_TRIGGER_LEVEL;
375 pola = INTR_POLARITY_LOW;
380 ioapic_abi_set_irqmap(irq, gsi, trig, pola);
384 ioapic_gsi_ioaddr(int gsi)
386 const struct ioapic_info *info;
388 info = ioapic_gsi_search(gsi);
389 return info->io_addr;
393 ioapic_gsi_pin(int gsi)
395 const struct ioapic_info *info;
397 info = ioapic_gsi_search(gsi);
398 return gsi - info->io_gsi_base;
401 static const struct ioapic_info *
402 ioapic_gsi_search(int gsi)
404 const struct ioapic_info *info;
406 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link) {
407 if (gsi >= info->io_gsi_base &&
408 gsi < info->io_gsi_base + info->io_npin)
411 panic("ioapic_gsi_search: no I/O APIC\n");
415 ioapic_gsi(int idx, int pin)
417 const struct ioapic_info *info;
419 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link) {
420 if (info->io_idx == idx)
425 if (pin >= info->io_npin)
427 return info->io_gsi_base + pin;
431 ioapic_extpin_setup(void *addr, int pin, int vec)
433 ioapic_pin_prog(addr, pin, vec,
434 INTR_TRIGGER_CONFORM, INTR_POLARITY_CONFORM, IOART_DELEXINT);
438 ioapic_extpin_gsi(void)
444 ioapic_pin_setup(void *addr, int pin, int vec,
445 enum intr_trigger trig, enum intr_polarity pola)
448 * Always clear an I/O APIC pin before [re]programming it. This is
449 * particularly important if the pin is set up for a level interrupt
450 * as the IOART_REM_IRR bit might be set. When we reprogram the
451 * vector any EOI from pending ints on this pin could be lost and
452 * IRR might never get reset.
454 * To fix this problem, clear the vector and make sure it is
455 * programmed as an edge interrupt. This should theoretically
456 * clear IRR so we can later, safely program it as a level
459 ioapic_pin_prog(addr, pin, vec, INTR_TRIGGER_EDGE, INTR_POLARITY_HIGH,
461 ioapic_pin_prog(addr, pin, vec, trig, pola, IOART_DELFIXED);
465 ioapic_pin_prog(void *addr, int pin, int vec,
466 enum intr_trigger trig, enum intr_polarity pola, uint32_t del_mode)
468 uint32_t flags, target;
471 KKASSERT(del_mode == IOART_DELEXINT || del_mode == IOART_DELFIXED);
473 select = IOAPIC_REDTBL0 + (2 * pin);
475 flags = ioapic_read(addr, select) & IOART_RESV;
476 flags |= IOART_INTMSET | IOART_DESTPHY;
481 * We only support limited I/O APIC mixed mode,
482 * so even for ExtINT, we still use "fixed"
485 flags |= IOART_DELFIXED;
488 if (del_mode == IOART_DELEXINT) {
489 KKASSERT(trig == INTR_TRIGGER_CONFORM &&
490 pola == INTR_POLARITY_CONFORM);
491 flags |= IOART_TRGREDG | IOART_INTAHI;
494 case INTR_TRIGGER_EDGE:
495 flags |= IOART_TRGREDG;
498 case INTR_TRIGGER_LEVEL:
499 flags |= IOART_TRGRLVL;
502 case INTR_TRIGGER_CONFORM:
503 panic("ioapic_pin_prog: trig conform is not "
507 case INTR_POLARITY_HIGH:
508 flags |= IOART_INTAHI;
511 case INTR_POLARITY_LOW:
512 flags |= IOART_INTALO;
515 case INTR_POLARITY_CONFORM:
516 panic("ioapic_pin_prog: pola conform is not "
521 target = ioapic_read(addr, select + 1) & IOART_HI_DEST_RESV;
522 target |= (CPUID_TO_APICID(0) << IOART_HI_DEST_SHIFT) &
525 ioapic_write(addr, select, flags | vec);
526 ioapic_write(addr, select + 1, target);
530 ioapic_setup(const struct ioapic_info *info)
534 ioapic_set_apic_id(info);
536 for (i = 0; i < info->io_npin; ++i)
537 ioapic_gsi_setup(info->io_gsi_base + i);
541 ioapic_alloc_apic_id(int start)
544 const struct ioapic_info *info;
545 int apic_id, apic_id16;
547 apic_id = lapic_unused_apic_id(start);
548 if (apic_id == NAPICID) {
549 kprintf("IOAPIC: can't find unused APIC ID\n");
552 apic_id16 = apic_id & IOAPIC_ID_MASK;
555 * Check against other I/O APIC's APIC ID's lower 4bits.
557 * The new APIC ID will have to be different from others
558 * in the lower 4bits, no matter whether xAPIC is used
561 TAILQ_FOREACH(info, &ioapic_conf.ioc_list, io_link) {
562 if (info->io_apic_id == -1) {
566 if ((info->io_apic_id & IOAPIC_ID_MASK) == apic_id16)
572 kprintf("IOAPIC: APIC ID %d has same lower 4bits as "
573 "%dth I/O APIC, keep searching...\n",
574 apic_id, info->io_idx);
578 panic("ioapic_unused_apic_id: never reached\n");
582 ioapic_map(vm_paddr_t pa)
584 KKASSERT(pa < 0x100000000LL);
585 return pmap_mapdev_uncacheable(pa, PAGE_SIZE);