Make the VR device backdown to emergency polling if the interrupt appears
[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 $
2db3b277 27 * $DragonFly: src/sys/kern/kern_intr.c,v 1.14 2004/02/12 06:57:48 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];
96728c05 57static int irunning[NHWI+NSWI];
984263bc 58
ef0fdad1 59static void ithread_handler(void *arg);
984263bc 60
96728c05 61thread_t
ef0fdad1 62register_swi(int intr, inthand2_t *handler, void *arg, const char *name)
984263bc 63{
ef0fdad1
MD
64 if (intr < NHWI || intr >= NHWI + NSWI)
65 panic("register_swi: bad intr %d", intr);
96728c05 66 return(register_int(intr, handler, arg, name));
984263bc
MD
67}
68
96728c05 69thread_t
ef0fdad1 70register_int(int intr, inthand2_t *handler, void *arg, const char *name)
984263bc 71{
ef0fdad1
MD
72 intrec_t **list;
73 intrec_t *rec;
74 thread_t td;
75
76 if (intr < 0 || intr > NHWI + NSWI)
77 panic("register_int: bad intr %d", intr);
78
79 rec = malloc(sizeof(intrec_t), M_DEVBUF, M_NOWAIT);
80 if (rec == NULL)
81 panic("register_swi: malloc failed");
82 rec->handler = handler;
83 rec->argument = arg;
84 rec->name = name;
85 rec->intr = intr;
86 rec->next = NULL;
87
88 list = &intlists[intr];
89
90 /*
91 * Create an interrupt thread if necessary, leave it in an unscheduled
26a0694b
MD
92 * state. The kthread restore function exits a critical section before
93 * starting the function so we need *TWO* critical sections in order
94 * for the handler to begin running in one.
ef0fdad1
MD
95 */
96 if ((td = ithreads[intr]) == NULL) {
97 lwkt_create((void *)ithread_handler, (void *)intr, &ithreads[intr],
75cdbe6c
MD
98 &ithread_ary[intr], TDF_STOPREQ|TDF_INTTHREAD, -1,
99 "ithread %d", intr);
ef0fdad1 100 td = ithreads[intr];
4b5f931b 101 if (intr >= NHWI && intr < NHWI + NSWI)
26a0694b 102 lwkt_setpri(td, TDPRI_SOFT_NORM + TDPRI_CRIT * 2);
4b5f931b 103 else
26a0694b 104 lwkt_setpri(td, TDPRI_INT_MED + TDPRI_CRIT * 2);
ef0fdad1
MD
105 }
106
107 /*
108 * Add the record to the interrupt list
109 */
110 crit_enter(); /* token */
111 while (*list != NULL)
112 list = &(*list)->next;
113 *list = rec;
114 crit_exit();
96728c05 115 return(td);
ef0fdad1 116}
984263bc 117
ef0fdad1
MD
118void
119unregister_swi(int intr, inthand2_t *handler)
120{
121 if (intr < NHWI || intr >= NHWI + NSWI)
122 panic("register_swi: bad intr %d", intr);
123 unregister_int(intr, handler);
984263bc
MD
124}
125
126void
ef0fdad1 127unregister_int(int intr, inthand2_t handler)
984263bc 128{
ef0fdad1
MD
129 intrec_t **list;
130 intrec_t *rec;
131
132 if (intr < 0 || intr > NHWI + NSWI)
133 panic("register_int: bad intr %d", intr);
134 list = &intlists[intr];
135 crit_enter();
136 while ((rec = *list) != NULL) {
137 if (rec->handler == (void *)handler) {
138 *list = rec->next;
139 break;
984263bc 140 }
ef0fdad1
MD
141 list = &rec->next;
142 }
143 crit_exit();
144 if (rec != NULL) {
145 free(rec, M_DEVBUF);
146 } else {
147 printf("warning: unregister_int: int %d handler %p not found\n",
148 intr, handler);
149 }
984263bc
MD
150}
151
4b5f931b
MD
152void
153swi_setpriority(int intr, int pri)
154{
155 struct thread *td;
156
157 if (intr < NHWI || intr >= NHWI + NSWI)
158 panic("register_swi: bad intr %d", intr);
159 if ((td = ithreads[intr]) != NULL)
160 lwkt_setpri(td, pri);
161}
162
7e071e7a
MD
163void
164register_randintr(int intr)
165{
166 struct random_softc *sc = &irandom_ary[intr];
167 sc->sc_intr = intr;
168 sc->sc_enabled = 1;
169}
170
171void
172unregister_randintr(int intr)
173{
174 struct random_softc *sc = &irandom_ary[intr];
175 sc->sc_enabled = 0;
176}
177
ef0fdad1 178/*
b68b7282
MD
179 * Dispatch an interrupt. If there's nothing to do we have a stray
180 * interrupt and can just return, leaving the interrupt masked.
96728c05
MD
181 *
182 * We need to schedule the interrupt and set its irunning[] bit. If
183 * we are not on the interrupt thread's cpu we have to send a message
184 * to the correct cpu that will issue the desired action (interlocking
185 * with the interrupt thread's critical section).
186 *
187 * We are NOT in a critical section, which will allow the scheduled
71ef2f5c 188 * interrupt to preempt us. The MP lock might *NOT* be held here.
ef0fdad1 189 */
96728c05
MD
190static void
191sched_ithd_remote(void *arg)
192{
193 sched_ithd((int)arg);
194}
195
ef0fdad1
MD
196void
197sched_ithd(int intr)
198{
199 thread_t td;
200
201 if ((td = ithreads[intr]) != NULL) {
b68b7282 202 if (intlists[intr] == NULL) {
ef0fdad1 203 printf("sched_ithd: stray interrupt %d\n", intr);
b68b7282 204 } else {
a72187e9 205 if (td->td_gd == mycpu) {
96728c05
MD
206 irunning[intr] = 1;
207 lwkt_schedule(td); /* preemption handled internally */
208 } else {
2db3b277 209 lwkt_send_ipiq(td->td_gd, sched_ithd_remote, (void *)intr);
96728c05 210 }
b68b7282 211 }
ef0fdad1
MD
212 } else {
213 printf("sched_ithd: stray interrupt %d\n", intr);
214 }
215}
216
b68b7282 217/*
26a0694b
MD
218 * Interrupt threads run this as their main loop. The handler should be
219 * in a critical section on entry.
b68b7282 220 */
ef0fdad1
MD
221static void
222ithread_handler(void *arg)
223{
224 int intr = (int)arg;
225 intrec_t **list = &intlists[intr];
226 intrec_t *rec;
227 intrec_t *nrec;
7e071e7a 228 struct random_softc *sc = &irandom_ary[intr];
ef0fdad1 229
26a0694b 230 KKASSERT(curthread->td_pri >= TDPRI_CRIT);
ef0fdad1 231 for (;;) {
96728c05 232 irunning[intr] = 0;
ef0fdad1
MD
233 for (rec = *list; rec; rec = nrec) {
234 nrec = rec->next;
235 rec->handler(rec->argument);
236 }
7e071e7a 237 if (sc->sc_enabled)
96728c05
MD
238 add_interrupt_randomness(intr);
239 if (irunning[intr] == 0)
240 ithread_done(intr);
ef0fdad1 241 }
ef0fdad1
MD
242}
243
984263bc
MD
244/*
245 * Sysctls used by systat and others: hw.intrnames and hw.intrcnt.
246 * The data for this machine dependent, and the declarations are in machine
247 * dependent code. The layout of intrnames and intrcnt however is machine
248 * independent.
249 *
250 * We do not know the length of intrcnt and intrnames at compile time, so
251 * calculate things at run time.
252 */
253static int
254sysctl_intrnames(SYSCTL_HANDLER_ARGS)
255{
256 return (sysctl_handle_opaque(oidp, intrnames, eintrnames - intrnames,
257 req));
258}
259
260SYSCTL_PROC(_hw, OID_AUTO, intrnames, CTLTYPE_OPAQUE | CTLFLAG_RD,
261 NULL, 0, sysctl_intrnames, "", "Interrupt Names");
262
263static int
264sysctl_intrcnt(SYSCTL_HANDLER_ARGS)
265{
266 return (sysctl_handle_opaque(oidp, intrcnt,
267 (char *)eintrcnt - (char *)intrcnt, req));
268}
269
270SYSCTL_PROC(_hw, OID_AUTO, intrcnt, CTLTYPE_OPAQUE | CTLFLAG_RD,
271 NULL, 0, sysctl_intrcnt, "", "Interrupt Counts");