e261dee9a313e053dff9fc567e2989bb0153066e
[dragonfly.git] / sys / kern / kern_intr.c
1 /*
2  * Copyright (c) 2003, Matthew Dillon <dillon@backplane.com> All rights reserved.
3  * Copyright (c) 1997, Stefan Esser <se@freebsd.org> All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice unmodified, this list of conditions, and the following
10  *    disclaimer.
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.
14  *
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.
25  *
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.6 2003/06/30 19:50:31 dillon Exp $
28  *
29  */
30
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>
37 #include <sys/proc.h>
38 #include <sys/thread2.h>
39
40 #include <machine/ipl.h>
41
42 #include <sys/interrupt.h>
43
44 typedef struct intrec {
45     struct intrec *next;
46     inthand2_t  *handler;
47     void        *argument;
48     const char  *name;
49     int         intr;
50 } intrec_t;
51
52 static intrec_t *intlists[NHWI+NSWI];
53 static thread_t ithreads[NHWI+NSWI];
54 static struct thread ithread_ary[NHWI+NSWI];
55
56 static void ithread_handler(void *arg);
57
58 void
59 register_swi(int intr, inthand2_t *handler, void *arg, const char *name)
60 {
61     if (intr < NHWI || intr >= NHWI + NSWI)
62         panic("register_swi: bad intr %d", intr);
63     register_int(intr, handler, arg, name);
64 }
65
66 void
67 register_int(int intr, inthand2_t *handler, void *arg, const char *name)
68 {
69     intrec_t **list;
70     intrec_t *rec;
71     thread_t td;
72
73     if (intr < 0 || intr > NHWI + NSWI)
74         panic("register_int: bad intr %d", intr);
75
76     rec = malloc(sizeof(intrec_t), M_DEVBUF, M_NOWAIT);
77     if (rec == NULL)
78         panic("register_swi: malloc failed");
79     rec->handler = handler;
80     rec->argument = arg;
81     rec->name = name;
82     rec->intr = intr;
83     rec->next = NULL;
84
85     list = &intlists[intr];
86
87     /*
88      * Create an interrupt thread if necessary, leave it in an unscheduled
89      * state.  The kthread restore function exits a critical section before
90      * starting the function so we need *TWO* critical sections in order
91      * for the handler to begin running in one.
92      */
93     if ((td = ithreads[intr]) == NULL) {
94         lwkt_create((void *)ithread_handler, (void *)intr, &ithreads[intr],
95             &ithread_ary[intr], TDF_STOPREQ, "ithread %d", intr);
96         td = ithreads[intr];
97         if (intr >= NHWI && intr < NHWI + NSWI)
98             lwkt_setpri(td, TDPRI_SOFT_NORM + TDPRI_CRIT * 2);
99         else
100             lwkt_setpri(td, TDPRI_INT_MED + TDPRI_CRIT * 2);
101     }
102
103     /*
104      * Add the record to the interrupt list
105      */
106     crit_enter();       /* token */
107     while (*list != NULL)
108         list = &(*list)->next;
109     *list = rec;
110     crit_exit();
111 }
112
113 void
114 unregister_swi(int intr, inthand2_t *handler)
115 {
116     if (intr < NHWI || intr >= NHWI + NSWI)
117         panic("register_swi: bad intr %d", intr);
118     unregister_int(intr, handler);
119 }
120
121 void
122 unregister_int(int intr, inthand2_t handler)
123 {
124     intrec_t **list;
125     intrec_t *rec;
126
127     if (intr < 0 || intr > NHWI + NSWI)
128         panic("register_int: bad intr %d", intr);
129     list = &intlists[intr];
130     crit_enter();
131     while ((rec = *list) != NULL) {
132         if (rec->handler == (void *)handler) {
133             *list = rec->next;
134             break;
135         }
136         list = &rec->next;
137     }
138     crit_exit();
139     if (rec != NULL) {
140         free(rec, M_DEVBUF);
141     } else {
142         printf("warning: unregister_int: int %d handler %p not found\n",
143             intr, handler);
144     }
145 }
146
147 void
148 swi_setpriority(int intr, int pri)
149 {
150     struct thread *td;
151
152     if (intr < NHWI || intr >= NHWI + NSWI)
153         panic("register_swi: bad intr %d", intr);
154     if ((td = ithreads[intr]) != NULL)
155         lwkt_setpri(td, pri);
156 }
157
158 /*
159  * Dispatch an interrupt.  If there's nothing to do we have a stray
160  * interrupt and can just return, leaving the interrupt masked.
161  */
162 void
163 sched_ithd(int intr)
164 {
165     thread_t td;
166
167     if ((td = ithreads[intr]) != NULL) {
168         if (intlists[intr] == NULL) {
169             printf("sched_ithd: stray interrupt %d\n", intr);
170         } else {
171             lwkt_schedule(td);
172             lwkt_preempt(td, intr);
173         }
174     } else {
175         printf("sched_ithd: stray interrupt %d\n", intr);
176     }
177 }
178
179 /*
180  * Interrupt threads run this as their main loop.  The handler should be
181  * in a critical section on entry.
182  */
183 static void
184 ithread_handler(void *arg)
185 {
186     int intr = (int)arg;
187     intrec_t **list = &intlists[intr];
188     intrec_t *rec;
189     intrec_t *nrec;
190
191     KKASSERT(curthread->td_pri >= TDPRI_CRIT);
192     for (;;) {
193         for (rec = *list; rec; rec = nrec) {
194             nrec = rec->next;
195             rec->handler(rec->argument);
196         }
197         ithread_done(intr);
198     }
199 }
200
201
202 /* 
203  * Sysctls used by systat and others: hw.intrnames and hw.intrcnt.
204  * The data for this machine dependent, and the declarations are in machine
205  * dependent code.  The layout of intrnames and intrcnt however is machine
206  * independent.
207  *
208  * We do not know the length of intrcnt and intrnames at compile time, so
209  * calculate things at run time.
210  */
211 static int
212 sysctl_intrnames(SYSCTL_HANDLER_ARGS)
213 {
214         return (sysctl_handle_opaque(oidp, intrnames, eintrnames - intrnames, 
215             req));
216 }
217
218 SYSCTL_PROC(_hw, OID_AUTO, intrnames, CTLTYPE_OPAQUE | CTLFLAG_RD,
219         NULL, 0, sysctl_intrnames, "", "Interrupt Names");
220
221 static int
222 sysctl_intrcnt(SYSCTL_HANDLER_ARGS)
223 {
224         return (sysctl_handle_opaque(oidp, intrcnt, 
225             (char *)eintrcnt - (char *)intrcnt, req));
226 }
227
228 SYSCTL_PROC(_hw, OID_AUTO, intrcnt, CTLTYPE_OPAQUE | CTLFLAG_RD,
229         NULL, 0, sysctl_intrcnt, "", "Interrupt Counts");