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