2 * Copyright (c) 1991 The Regents of the University of California.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 4. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * Copyright (c) 2002 Benno Rice.
34 * All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
45 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
57 * from: @(#)isa.c 7.2 (Berkeley) 5/13/91
58 * form: src/sys/i386/isa/intr_machdep.c,v 1.57 2001/07/20
63 #include "opt_platform.h"
65 #include <sys/param.h>
66 #include <sys/systm.h>
67 #include <sys/kernel.h>
68 #include <sys/queue.h>
70 #include <sys/interrupt.h>
73 #include <sys/malloc.h>
74 #include <sys/mutex.h>
76 #include <sys/syslog.h>
77 #include <sys/vmmeter.h>
80 #include <machine/frame.h>
81 #include <machine/intr_machdep.h>
82 #include <machine/md_var.h>
83 #include <machine/smp.h>
84 #include <machine/trap.h>
89 #define ISA_IRQ_COUNT 16
93 #define ISA_IRQ_COUNT 0
96 #define MAX_STRAY_LOG 5
98 MALLOC_DEFINE(M_INTR, "intr", "interrupt handler data");
100 struct powerpc_intr {
101 struct intr_event *event;
107 enum intr_trigger trig;
108 enum intr_polarity pol;
111 static struct mtx intr_table_lock;
112 static struct powerpc_intr *powerpc_intrs[INTR_VECTORS];
113 static u_int nvectors; /* Allocated vectors */
114 static u_int stray_count;
117 static void *ipi_cookie;
120 static u_int ipi_irq;
126 intr_init(void *dummy __unused)
129 mtx_init(&intr_table_lock, "intr sources lock", NULL, MTX_DEF);
131 SYSINIT(intr_init, SI_SUB_INTR, SI_ORDER_FIRST, intr_init, NULL);
134 intrcnt_setname(const char *name, int index)
137 snprintf(intrnames + (MAXCOMLEN + 1) * index, MAXCOMLEN + 1, "%-*s",
141 static struct powerpc_intr *
142 intr_lookup(u_int irq)
145 struct powerpc_intr *i, *iscan;
148 mtx_lock(&intr_table_lock);
149 for (vector = 0; vector < nvectors; vector++) {
150 i = powerpc_intrs[vector];
151 if (i != NULL && i->irq == irq) {
152 mtx_unlock(&intr_table_lock);
156 mtx_unlock(&intr_table_lock);
158 i = malloc(sizeof(*i), M_INTR, M_NOWAIT);
164 i->trig = INTR_TRIGGER_CONFORM;
165 i->pol = INTR_POLARITY_CONFORM;
169 mtx_lock(&intr_table_lock);
170 for (vector = 0; vector < INTR_VECTORS && vector <= nvectors;
172 iscan = powerpc_intrs[vector];
173 if (iscan != NULL && iscan->irq == irq)
175 if (iscan == NULL && i->vector == -1)
180 if (iscan == NULL && i->vector != -1) {
181 powerpc_intrs[i->vector] = i;
182 sprintf(intrname, "irq%u:", i->irq);
183 intrcnt_setname(intrname, i->vector);
186 mtx_unlock(&intr_table_lock);
188 if (iscan != NULL || i->vector == -1) {
197 powerpc_map_irq(struct powerpc_intr *i)
200 #if ISA_IRQ_COUNT > 0
201 if (i->irq < ISA_IRQ_COUNT) {
202 if (pic8259 == NULL) {
214 i->intline = i->irq - ISA_IRQ_COUNT;
219 powerpc_intr_eoi(void *arg)
221 struct powerpc_intr *i = arg;
223 PIC_EOI(i->pic, i->intline);
227 powerpc_intr_mask(void *arg)
229 struct powerpc_intr *i = arg;
231 PIC_MASK(i->pic, i->intline);
235 powerpc_intr_unmask(void *arg)
237 struct powerpc_intr *i = arg;
239 PIC_UNMASK(i->pic, i->intline);
243 powerpc_register_pic(device_t dev, u_int ipi)
247 ipi_irq = ipi + ISA_IRQ_COUNT;
251 powerpc_register_8259(device_t dev)
258 powerpc_enable_intr(void)
260 struct powerpc_intr *i;
264 panic("no PIC detected\n");
267 /* Install an IPI handler. */
268 error = powerpc_setup_intr("IPI", ipi_irq, powerpc_ipi_handler,
269 NULL, NULL, INTR_TYPE_MISC | INTR_EXCL | INTR_FAST, &ipi_cookie);
271 printf("unable to setup IPI handler\n");
276 for (vector = 0; vector < nvectors; vector++) {
277 i = powerpc_intrs[vector];
281 error = powerpc_map_irq(i);
285 if (i->trig != INTR_TRIGGER_CONFORM ||
286 i->pol != INTR_POLARITY_CONFORM)
287 PIC_CONFIG(i->pic, i->intline, i->trig, i->pol);
289 if (i->event != NULL)
290 PIC_ENABLE(i->pic, i->intline, vector);
297 powerpc_setup_intr(const char *name, u_int irq, driver_filter_t filter,
298 driver_intr_t handler, void *arg, enum intr_type flags, void **cookiep)
300 struct powerpc_intr *i;
301 int error, enable = 0;
303 i = intr_lookup(irq);
307 if (i->event == NULL) {
308 error = intr_event_create(&i->event, (void *)i, 0, irq,
309 powerpc_intr_mask, powerpc_intr_unmask, powerpc_intr_eoi,
310 NULL, "irq%u:", irq);
314 i->cntp = &intrcnt[i->vector];
319 error = intr_event_add_handler(i->event, name, filter, handler, arg,
320 intr_priority(flags), flags, cookiep);
322 mtx_lock(&intr_table_lock);
323 intrcnt_setname(i->event->ie_fullname, i->vector);
324 mtx_unlock(&intr_table_lock);
327 error = powerpc_map_irq(i);
328 if (!error && enable)
329 PIC_ENABLE(i->pic, i->intline, i->vector);
335 powerpc_teardown_intr(void *cookie)
338 return (intr_event_remove_handler(cookie));
342 powerpc_config_intr(int irq, enum intr_trigger trig, enum intr_polarity pol)
344 struct powerpc_intr *i;
346 i = intr_lookup(irq);
354 PIC_CONFIG(i->pic, i->intline, trig, pol);
360 powerpc_dispatch_intr(u_int vector, struct trapframe *tf)
362 struct powerpc_intr *i;
363 struct intr_event *ie;
365 i = powerpc_intrs[vector];
372 KASSERT(ie != NULL, ("%s: interrupt without an event", __func__));
374 if (intr_event_handle(ie, tf) != 0) {
381 if (stray_count <= MAX_STRAY_LOG) {
382 printf("stray irq %d\n", i ? i->irq : -1);
383 if (stray_count >= MAX_STRAY_LOG) {
384 printf("got %d stray interrupts, not logging anymore\n",
389 PIC_MASK(i->pic, i->intline);