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;
170 mtx_lock(&intr_table_lock);
171 for (vector = 0; vector < INTR_VECTORS && vector <= nvectors;
173 iscan = powerpc_intrs[vector];
174 if (iscan != NULL && iscan->irq == irq)
176 if (iscan == NULL && i->vector == -1)
181 if (iscan == NULL && i->vector != -1) {
182 powerpc_intrs[i->vector] = i;
183 sprintf(intrname, "irq%u:", i->irq);
184 intrcnt_setname(intrname, i->vector);
187 mtx_unlock(&intr_table_lock);
189 if (iscan != NULL || i->vector == -1) {
198 powerpc_map_irq(struct powerpc_intr *i)
201 #if ISA_IRQ_COUNT > 0
202 if (i->irq < ISA_IRQ_COUNT) {
203 if (pic8259 == NULL) {
215 i->intline = i->irq - ISA_IRQ_COUNT;
220 powerpc_intr_eoi(void *arg)
222 struct powerpc_intr *i = arg;
224 PIC_EOI(i->pic, i->intline);
228 powerpc_intr_mask(void *arg)
230 struct powerpc_intr *i = arg;
232 PIC_MASK(i->pic, i->intline);
236 powerpc_intr_unmask(void *arg)
238 struct powerpc_intr *i = arg;
240 PIC_UNMASK(i->pic, i->intline);
244 powerpc_register_pic(device_t dev, u_int ipi)
248 ipi_irq = ipi + ISA_IRQ_COUNT;
252 powerpc_register_8259(device_t dev)
259 powerpc_enable_intr(void)
261 struct powerpc_intr *i;
265 panic("no PIC detected\n");
268 /* Install an IPI handler. */
269 error = powerpc_setup_intr("IPI", ipi_irq, powerpc_ipi_handler,
270 NULL, NULL, INTR_TYPE_MISC | INTR_EXCL | INTR_FAST, &ipi_cookie);
272 printf("unable to setup IPI handler\n");
277 for (vector = 0; vector < nvectors; vector++) {
278 i = powerpc_intrs[vector];
282 error = powerpc_map_irq(i);
286 if (i->trig != INTR_TRIGGER_CONFORM ||
287 i->pol != INTR_POLARITY_CONFORM)
288 PIC_CONFIG(i->pic, i->intline, i->trig, i->pol);
290 if (i->event != NULL)
291 PIC_ENABLE(i->pic, i->intline, vector);
298 powerpc_setup_intr(const char *name, u_int irq, driver_filter_t filter,
299 driver_intr_t handler, void *arg, enum intr_type flags, void **cookiep)
301 struct powerpc_intr *i;
302 int error, enable = 0;
304 i = intr_lookup(irq);
308 if (i->event == NULL) {
309 error = intr_event_create(&i->event, (void *)i, 0, irq,
310 powerpc_intr_mask, powerpc_intr_unmask, powerpc_intr_eoi,
311 NULL, "irq%u:", irq);
315 i->cntp = &intrcnt[i->vector];
320 error = intr_event_add_handler(i->event, name, filter, handler, arg,
321 intr_priority(flags), flags, cookiep);
323 mtx_lock(&intr_table_lock);
324 intrcnt_setname(i->event->ie_fullname, i->vector);
325 mtx_unlock(&intr_table_lock);
328 error = powerpc_map_irq(i);
330 if (!error && (i->trig != INTR_TRIGGER_CONFORM ||
331 i->pol != INTR_POLARITY_CONFORM))
332 PIC_CONFIG(i->pic, i->intline, i->trig, i->pol);
334 if (!error && enable)
335 PIC_ENABLE(i->pic, i->intline, i->vector);
341 powerpc_teardown_intr(void *cookie)
344 return (intr_event_remove_handler(cookie));
348 powerpc_config_intr(int irq, enum intr_trigger trig, enum intr_polarity pol)
350 struct powerpc_intr *i;
352 i = intr_lookup(irq);
359 if (!cold && i->pic != NULL)
360 PIC_CONFIG(i->pic, i->intline, trig, pol);
366 powerpc_dispatch_intr(u_int vector, struct trapframe *tf)
368 struct powerpc_intr *i;
369 struct intr_event *ie;
371 i = powerpc_intrs[vector];
378 KASSERT(ie != NULL, ("%s: interrupt without an event", __func__));
380 if (intr_event_handle(ie, tf) != 0) {
387 if (stray_count <= MAX_STRAY_LOG) {
388 printf("stray irq %d\n", i ? i->irq : -1);
389 if (stray_count >= MAX_STRAY_LOG) {
390 printf("got %d stray interrupts, not logging anymore\n",
395 PIC_MASK(i->pic, i->intline);