2 * Copyright (c) 2003, Matthew Dillon <dillon@backplane.com> All rights reserved.
3 * Copyright (c) 1997, Stefan Esser <se@freebsd.org> All rights reserved.
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 unmodified, this list of conditions, and the following
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 * $FreeBSD: src/sys/kern/kern_intr.c,v 1.24.2.1 2001/10/14 20:05:50 luigi Exp $
27 * $DragonFly: src/sys/kern/kern_intr.c,v 1.5 2003/06/29 07:37:06 dillon Exp $
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/malloc.h>
34 #include <sys/kernel.h>
35 #include <sys/sysctl.h>
36 #include <sys/thread.h>
38 #include <sys/thread2.h>
40 #include <machine/ipl.h>
42 #include <sys/interrupt.h>
44 typedef struct intrec {
52 static intrec_t *intlists[NHWI+NSWI];
53 static thread_t ithreads[NHWI+NSWI];
54 static struct thread ithread_ary[NHWI+NSWI];
56 static void ithread_handler(void *arg);
59 register_swi(int intr, inthand2_t *handler, void *arg, const char *name)
61 if (intr < NHWI || intr >= NHWI + NSWI)
62 panic("register_swi: bad intr %d", intr);
63 register_int(intr, handler, arg, name);
67 register_int(int intr, inthand2_t *handler, void *arg, const char *name)
73 if (intr < 0 || intr > NHWI + NSWI)
74 panic("register_int: bad intr %d", intr);
76 rec = malloc(sizeof(intrec_t), M_DEVBUF, M_NOWAIT);
78 panic("register_swi: malloc failed");
79 rec->handler = handler;
85 list = &intlists[intr];
88 * Create an interrupt thread if necessary, leave it in an unscheduled
91 if ((td = ithreads[intr]) == NULL) {
92 lwkt_create((void *)ithread_handler, (void *)intr, &ithreads[intr],
93 &ithread_ary[intr], TDF_STOPREQ, "ithread %d", intr);
95 if (intr >= NHWI && intr < NHWI + NSWI)
96 lwkt_setpri(td, TDPRI_SOFT_NORM);
98 lwkt_setpri(td, TDPRI_INT_MED);
102 * Add the record to the interrupt list
104 crit_enter(); /* token */
105 while (*list != NULL)
106 list = &(*list)->next;
112 unregister_swi(int intr, inthand2_t *handler)
114 if (intr < NHWI || intr >= NHWI + NSWI)
115 panic("register_swi: bad intr %d", intr);
116 unregister_int(intr, handler);
120 unregister_int(int intr, inthand2_t handler)
125 if (intr < 0 || intr > NHWI + NSWI)
126 panic("register_int: bad intr %d", intr);
127 list = &intlists[intr];
129 while ((rec = *list) != NULL) {
130 if (rec->handler == (void *)handler) {
140 printf("warning: unregister_int: int %d handler %p not found\n",
146 swi_setpriority(int intr, int pri)
150 if (intr < NHWI || intr >= NHWI + NSWI)
151 panic("register_swi: bad intr %d", intr);
152 if ((td = ithreads[intr]) != NULL)
153 lwkt_setpri(td, pri);
157 * Dispatch an interrupt. If there's nothing to do we have a stray
158 * interrupt and can just return, leaving the interrupt masked.
165 if ((td = ithreads[intr]) != NULL) {
166 if (intlists[intr] == NULL) {
167 printf("sched_ithd: stray interrupt %d\n", intr);
170 lwkt_preempt(td, intr);
173 printf("sched_ithd: stray interrupt %d\n", intr);
178 * Interrupt threads run this as their main loop.
181 ithread_handler(void *arg)
184 intrec_t **list = &intlists[intr];
187 int xpri = (curthread->td_pri & TDPRI_MASK) + TDPRI_CRIT; /* DEBUG YYY */
189 crit_enter(); /* replaces SPLs */
191 for (rec = *list; rec; rec = nrec) {
193 rec->handler(rec->argument);
198 * temporary sanity check. If we preempted another thread we
199 * are placed in another critical section by that thread which
200 * will be released when we block and resume the original thread.
202 KKASSERT(curthread->td_pri == xpri ||
203 (curthread->td_preempted && curthread->td_pri == xpri + TDPRI_CRIT));
205 crit_exit(); /* not reached */
210 * Sysctls used by systat and others: hw.intrnames and hw.intrcnt.
211 * The data for this machine dependent, and the declarations are in machine
212 * dependent code. The layout of intrnames and intrcnt however is machine
215 * We do not know the length of intrcnt and intrnames at compile time, so
216 * calculate things at run time.
219 sysctl_intrnames(SYSCTL_HANDLER_ARGS)
221 return (sysctl_handle_opaque(oidp, intrnames, eintrnames - intrnames,
225 SYSCTL_PROC(_hw, OID_AUTO, intrnames, CTLTYPE_OPAQUE | CTLFLAG_RD,
226 NULL, 0, sysctl_intrnames, "", "Interrupt Names");
229 sysctl_intrcnt(SYSCTL_HANDLER_ARGS)
231 return (sysctl_handle_opaque(oidp, intrcnt,
232 (char *)eintrcnt - (char *)intrcnt, req));
235 SYSCTL_PROC(_hw, OID_AUTO, intrcnt, CTLTYPE_OPAQUE | CTLFLAG_RD,
236 NULL, 0, sysctl_intrcnt, "", "Interrupt Counts");