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 <sys/param.h>
64 #include <sys/systm.h>
65 #include <sys/kernel.h>
66 #include <sys/queue.h>
68 #include <sys/interrupt.h>
70 #include <sys/malloc.h>
71 #include <sys/mutex.h>
73 #include <sys/vmmeter.h>
76 #include <machine/frame.h>
77 #include <machine/interruptvar.h>
78 #include <machine/intr_machdep.h>
79 #include <machine/trap.h>
81 #define MAX_STRAY_LOG 5
83 MALLOC_DEFINE(M_INTR, "intr", "interrupt handler data");
85 static int intr_initialized = 0;
87 static u_int intr_nirq;
88 static struct ppc_intr_handler *intr_handlers;
90 static struct mtx intr_table_lock;
92 extern int extint, extsize;
93 extern u_long extint_call;
95 static int intrcnt_index;
96 static ih_func_t intr_stray_handler;
97 static ih_func_t sched_ithd;
99 static void (*irq_enable)(uintptr_t);
100 static void (*irq_disable)(uintptr_t);
102 static void intrcnt_setname(const char *name, int index);
103 static void intrcnt_updatename(struct ppc_intr_handler *ih);
106 intrcnt_setname(const char *name, int index)
108 snprintf(intrnames + (MAXCOMLEN + 1) * index, MAXCOMLEN + 1, "%-*s",
113 intrcnt_updatename(struct ppc_intr_handler *ih)
115 intrcnt_setname(ih->ih_event->ie_fullname, ih->ih_index);
119 intrcnt_register(struct ppc_intr_handler *ih)
121 char straystr[MAXCOMLEN + 1];
123 KASSERT(ih->ih_event != NULL,
124 ("%s: ppc_intr_handler with no event", __func__));
126 ih->ih_index = intrcnt_index;
128 snprintf(straystr, MAXCOMLEN + 1, "stray irq%d", ih->ih_irq);
129 intrcnt_updatename(ih);
130 ih->ih_count = &intrcnt[ih->ih_index];
131 intrcnt_setname(straystr, ih->ih_index + 1);
132 ih->ih_straycount = &intrcnt[ih->ih_index + 1];
136 intr_init(void (*handler)(void), int nirq, void (*irq_e)(uintptr_t),
137 void (*irq_d)(uintptr_t))
142 if (intr_initialized != 0)
143 panic("intr_init: interrupts intialized twice\n");
148 intr_handlers = malloc(nirq * sizeof(struct ppc_intr_handler), M_INTR,
150 if (intr_handlers == NULL)
151 panic("intr_init: unable to allocate interrupt handler array");
153 for (i = 0; i < nirq; i++) {
154 intr_handlers[i].ih_func = intr_stray_handler;
155 intr_handlers[i].ih_arg = &intr_handlers[i];
156 intr_handlers[i].ih_irq = i;
157 intr_handlers[i].ih_flags = 0;
158 /* mux all initial stray irqs onto same count... */
159 intr_handlers[i].ih_straycount = &intrcnt[0];
162 intrcnt_setname("???", 0);
166 mtmsr(msr & ~PSL_EE);
168 ext_intr_install(handler);
175 mtx_init(&intr_table_lock, "intr table", NULL, MTX_SPIN);
179 intr_setup(u_int irq, ih_func_t *ihf, void *iha, u_int flags)
184 mtmsr(msr & ~PSL_EE);
186 intr_handlers[irq].ih_func = ihf;
187 intr_handlers[irq].ih_arg = iha;
188 intr_handlers[irq].ih_irq = irq;
189 intr_handlers[irq].ih_flags = flags;
195 inthand_add(const char *name, u_int irq, void (*handler)(void *), void *arg,
196 int flags, void **cookiep)
198 struct ppc_intr_handler *ih;
199 struct intr_event *event, *orphan;
201 int created_event = 0;
204 * Work around a race where more than one CPU may be registering
205 * handlers on the same IRQ at the same time.
207 ih = &intr_handlers[irq];
208 mtx_lock_spin(&intr_table_lock);
209 event = ih->ih_event;
210 mtx_unlock_spin(&intr_table_lock);
212 error = intr_event_create(&event, (void *)irq, 0,
213 (void (*)(void *))irq_enable, "irq%d:", irq);
217 mtx_lock_spin(&intr_table_lock);
219 if (ih->ih_event == NULL) {
220 ih->ih_event = event;
222 mtx_unlock_spin(&intr_table_lock);
225 event = ih->ih_event;
226 mtx_unlock_spin(&intr_table_lock);
227 intr_event_destroy(orphan);
231 /* XXX: Should probably fix support for multiple FAST. */
232 if (flags & INTR_FAST)
234 error = intr_event_add_handler(event, name, handler, arg,
235 intr_priority(flags), flags, cookiep);
237 if ((flags & INTR_FAST) == 0 || error) {
238 intr_setup(irq, sched_ithd, ih, flags);
245 if (flags & INTR_FAST)
246 intr_setup(irq, handler, arg, flags);
248 intrcnt_register(ih);
254 inthand_remove(u_int irq, void *cookie)
256 struct ppc_intr_handler *ih;
259 error = intr_event_remove_handler(cookie);
262 ih = &intr_handlers[irq];
264 mtx_lock_spin(&intr_table_lock);
266 if (ih->ih_event == NULL) {
267 intr_setup(irq, intr_stray_handler, ih, 0);
269 intr_setup(irq, sched_ithd, ih, 0);
272 mtx_unlock_spin(&intr_table_lock);
279 intr_handle(u_int irq)
281 atomic_add_long(intr_handlers[irq].ih_count, 1);
282 intr_handlers[irq].ih_func(intr_handlers[irq].ih_arg);
284 /* XXX wrong thing when using pre-emption ? */
285 if ((intr_handlers[irq].ih_flags & INTR_FAST) != 0)
290 intr_stray_handler(void *cookie)
292 struct ppc_intr_handler *ih;
294 ih = (struct ppc_intr_handler *)cookie;
296 if (*intr_handlers[ih->ih_irq].ih_straycount < MAX_STRAY_LOG) {
297 printf("stray irq %d\n", ih->ih_irq);
299 atomic_add_long(intr_handlers[ih->ih_irq].ih_straycount, 1);
300 if (*intr_handlers[ih->ih_irq].ih_straycount >= MAX_STRAY_LOG)
301 printf("got %d stray irq %d's: not logging anymore\n",
302 MAX_STRAY_LOG, ih->ih_irq);
307 sched_ithd(void *cookie)
309 struct ppc_intr_handler *ih;
312 ih = (struct ppc_intr_handler *)cookie;
314 error = intr_event_schedule_thread(ih->ih_event);
317 intr_stray_handler(ih);