The acpica-unix-20040527 download from intel seems to like to use upper
[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 $
37d44089 27 * $DragonFly: src/sys/kern/kern_intr.c,v 1.15 2004/06/27 19:37:22 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];
37d44089
MD
58static int ill_count[NHWI+NSWI]; /* interrupt livelock counter */
59static int ill_ticks[NHWI+NSWI]; /* track ticks to calculate freq */
60static int ill_delta[NHWI+NSWI]; /* track ticks to calculate freq */
61static int ill_state[NHWI+NSWI]; /* current state */
62static struct systimer ill_timer[NHWI+NSWI];
63
64#define LIVELOCK_NONE 0
65#define LIVELOCK_LIMITED 1
66
67static int livelock_limit = 100000;
68static int livelock_fallback = 50000;
69SYSCTL_INT(_kern, OID_AUTO, livelock_limit,
70 CTLFLAG_RW, &livelock_limit, 0, "Livelock interrupt rate limit");
71SYSCTL_INT(_kern, OID_AUTO, livelock_fallback,
72 CTLFLAG_RW, &livelock_fallback, 0, "Livelock interrupt fallback rate");
984263bc 73
ef0fdad1 74static void ithread_handler(void *arg);
984263bc 75
96728c05 76thread_t
ef0fdad1 77register_swi(int intr, inthand2_t *handler, void *arg, const char *name)
984263bc 78{
ef0fdad1
MD
79 if (intr < NHWI || intr >= NHWI + NSWI)
80 panic("register_swi: bad intr %d", intr);
96728c05 81 return(register_int(intr, handler, arg, name));
984263bc
MD
82}
83
96728c05 84thread_t
ef0fdad1 85register_int(int intr, inthand2_t *handler, void *arg, const char *name)
984263bc 86{
ef0fdad1
MD
87 intrec_t **list;
88 intrec_t *rec;
89 thread_t td;
90
91 if (intr < 0 || intr > NHWI + NSWI)
92 panic("register_int: bad intr %d", intr);
93
94 rec = malloc(sizeof(intrec_t), M_DEVBUF, M_NOWAIT);
95 if (rec == NULL)
96 panic("register_swi: malloc failed");
97 rec->handler = handler;
98 rec->argument = arg;
99 rec->name = name;
100 rec->intr = intr;
101 rec->next = NULL;
102
103 list = &intlists[intr];
104
105 /*
106 * Create an interrupt thread if necessary, leave it in an unscheduled
26a0694b
MD
107 * state. The kthread restore function exits a critical section before
108 * starting the function so we need *TWO* critical sections in order
109 * for the handler to begin running in one.
ef0fdad1
MD
110 */
111 if ((td = ithreads[intr]) == NULL) {
112 lwkt_create((void *)ithread_handler, (void *)intr, &ithreads[intr],
75cdbe6c
MD
113 &ithread_ary[intr], TDF_STOPREQ|TDF_INTTHREAD, -1,
114 "ithread %d", intr);
ef0fdad1 115 td = ithreads[intr];
4b5f931b 116 if (intr >= NHWI && intr < NHWI + NSWI)
26a0694b 117 lwkt_setpri(td, TDPRI_SOFT_NORM + TDPRI_CRIT * 2);
4b5f931b 118 else
26a0694b 119 lwkt_setpri(td, TDPRI_INT_MED + TDPRI_CRIT * 2);
ef0fdad1
MD
120 }
121
122 /*
123 * Add the record to the interrupt list
124 */
125 crit_enter(); /* token */
126 while (*list != NULL)
127 list = &(*list)->next;
128 *list = rec;
129 crit_exit();
96728c05 130 return(td);
ef0fdad1 131}
984263bc 132
ef0fdad1
MD
133void
134unregister_swi(int intr, inthand2_t *handler)
135{
136 if (intr < NHWI || intr >= NHWI + NSWI)
137 panic("register_swi: bad intr %d", intr);
138 unregister_int(intr, handler);
984263bc
MD
139}
140
141void
ef0fdad1 142unregister_int(int intr, inthand2_t handler)
984263bc 143{
ef0fdad1
MD
144 intrec_t **list;
145 intrec_t *rec;
146
147 if (intr < 0 || intr > NHWI + NSWI)
148 panic("register_int: bad intr %d", intr);
149 list = &intlists[intr];
150 crit_enter();
151 while ((rec = *list) != NULL) {
152 if (rec->handler == (void *)handler) {
153 *list = rec->next;
154 break;
984263bc 155 }
ef0fdad1
MD
156 list = &rec->next;
157 }
158 crit_exit();
159 if (rec != NULL) {
160 free(rec, M_DEVBUF);
161 } else {
162 printf("warning: unregister_int: int %d handler %p not found\n",
163 intr, handler);
164 }
984263bc
MD
165}
166
4b5f931b
MD
167void
168swi_setpriority(int intr, int pri)
169{
170 struct thread *td;
171
172 if (intr < NHWI || intr >= NHWI + NSWI)
173 panic("register_swi: bad intr %d", intr);
174 if ((td = ithreads[intr]) != NULL)
175 lwkt_setpri(td, pri);
176}
177
7e071e7a
MD
178void
179register_randintr(int intr)
180{
181 struct random_softc *sc = &irandom_ary[intr];
182 sc->sc_intr = intr;
183 sc->sc_enabled = 1;
184}
185
186void
187unregister_randintr(int intr)
188{
189 struct random_softc *sc = &irandom_ary[intr];
190 sc->sc_enabled = 0;
191}
192
ef0fdad1 193/*
b68b7282
MD
194 * Dispatch an interrupt. If there's nothing to do we have a stray
195 * interrupt and can just return, leaving the interrupt masked.
96728c05
MD
196 *
197 * We need to schedule the interrupt and set its irunning[] bit. If
198 * we are not on the interrupt thread's cpu we have to send a message
199 * to the correct cpu that will issue the desired action (interlocking
200 * with the interrupt thread's critical section).
201 *
202 * We are NOT in a critical section, which will allow the scheduled
71ef2f5c 203 * interrupt to preempt us. The MP lock might *NOT* be held here.
ef0fdad1 204 */
96728c05
MD
205static void
206sched_ithd_remote(void *arg)
207{
208 sched_ithd((int)arg);
209}
210
ef0fdad1
MD
211void
212sched_ithd(int intr)
213{
214 thread_t td;
215
216 if ((td = ithreads[intr]) != NULL) {
b68b7282 217 if (intlists[intr] == NULL) {
ef0fdad1 218 printf("sched_ithd: stray interrupt %d\n", intr);
b68b7282 219 } else {
a72187e9 220 if (td->td_gd == mycpu) {
96728c05
MD
221 irunning[intr] = 1;
222 lwkt_schedule(td); /* preemption handled internally */
223 } else {
2db3b277 224 lwkt_send_ipiq(td->td_gd, sched_ithd_remote, (void *)intr);
96728c05 225 }
b68b7282 226 }
ef0fdad1
MD
227 } else {
228 printf("sched_ithd: stray interrupt %d\n", intr);
229 }
230}
231
37d44089
MD
232/*
233 * This is run from a periodic SYSTIMER (and thus must be MP safe, the BGL
234 * might not be held).
235 */
236static void
237ithread_livelock_wakeup(void *data)
238{
239 int intr = (int)data;
240 thread_t td;
241
242 if ((td = ithreads[intr]) != NULL)
243 lwkt_schedule(td);
244}
245
246
b68b7282 247/*
26a0694b 248 * Interrupt threads run this as their main loop. The handler should be
37d44089
MD
249 * in a critical section on entry and the BGL is usually left held (for now).
250 *
251 * The irunning state starts at 0. When an interrupt occurs, the hardware
252 * interrupt is disabled and sched_ithd() The HW interrupt remains disabled
253 * until all routines have run. We then call ithread_done() to reenable
254 * the HW interrupt and deschedule us until the next interrupt.
b68b7282 255 */
ef0fdad1
MD
256static void
257ithread_handler(void *arg)
258{
259 int intr = (int)arg;
37d44089
MD
260 int freq;
261 int bticks;
ef0fdad1
MD
262 intrec_t **list = &intlists[intr];
263 intrec_t *rec;
264 intrec_t *nrec;
7e071e7a 265 struct random_softc *sc = &irandom_ary[intr];
ef0fdad1 266
26a0694b 267 KKASSERT(curthread->td_pri >= TDPRI_CRIT);
ef0fdad1 268 for (;;) {
96728c05 269 irunning[intr] = 0;
ef0fdad1
MD
270 for (rec = *list; rec; rec = nrec) {
271 nrec = rec->next;
272 rec->handler(rec->argument);
273 }
37d44089
MD
274
275 /*
276 * This is our interrupt hook to add rate randomness to the random
277 * number generator.
278 */
7e071e7a 279 if (sc->sc_enabled)
96728c05 280 add_interrupt_randomness(intr);
37d44089
MD
281
282 /*
283 * This is our livelock test. If we hit the rate limit we
284 * limit ourselves to 10000 interrupts/sec until the rate
285 * falls below 50% of that value, then we unlimit again.
286 */
287 ++ill_count[intr];
288 bticks = ticks - ill_ticks[intr];
289 ill_ticks[intr] = ticks;
290 if (bticks < 0 || bticks > hz)
291 bticks = hz;
292
293 switch(ill_state[intr]) {
294 case LIVELOCK_NONE:
295 ill_delta[intr] += bticks;
296 if (ill_delta[intr] < hz)
297 break;
298 freq = ill_count[intr] * hz / ill_delta[intr];
299 ill_delta[intr] = 0;
300 ill_count[intr] = 0;
301 if (freq < livelock_limit)
302 break;
303 printf("intr %d at %d hz, livelocked! limiting at %d hz\n",
304 intr, freq, livelock_fallback);
305 ill_state[intr] = LIVELOCK_LIMITED;
306 bticks = 0;
307 /* fall through */
308 case LIVELOCK_LIMITED:
309 /*
310 * Delay (us) before rearming the interrupt
311 */
312 systimer_init_oneshot(&ill_timer[intr], ithread_livelock_wakeup,
313 (void *)intr, 1 + 1000000 / livelock_fallback);
314 lwkt_deschedule_self(curthread);
315 lwkt_switch();
316 systimer_del(&ill_timer[intr]);
317
318 /*
319 * Calculate interrupt rate (note that due to our delay it
320 * will not exceed livelock_fallback).
321 */
322 ill_delta[intr] += bticks;
323 if (ill_delta[intr] < hz)
324 break;
325 freq = ill_count[intr] * hz / ill_delta[intr];
326 ill_delta[intr] = 0;
327 ill_count[intr] = 0;
328 if (freq < (livelock_fallback >> 1)) {
329 printf("intr %d at %d hz, removing livelock limit\n",
330 intr, freq);
331 ill_state[intr] = LIVELOCK_NONE;
332 }
333 break;
334 }
335
336 /*
337 * If another interrupt has not been queued we can reenable the
338 * hardware interrupt and go to sleep.
339 */
96728c05
MD
340 if (irunning[intr] == 0)
341 ithread_done(intr);
ef0fdad1 342 }
ef0fdad1
MD
343}
344
984263bc
MD
345/*
346 * Sysctls used by systat and others: hw.intrnames and hw.intrcnt.
347 * The data for this machine dependent, and the declarations are in machine
348 * dependent code. The layout of intrnames and intrcnt however is machine
349 * independent.
350 *
351 * We do not know the length of intrcnt and intrnames at compile time, so
352 * calculate things at run time.
353 */
354static int
355sysctl_intrnames(SYSCTL_HANDLER_ARGS)
356{
357 return (sysctl_handle_opaque(oidp, intrnames, eintrnames - intrnames,
358 req));
359}
360
361SYSCTL_PROC(_hw, OID_AUTO, intrnames, CTLTYPE_OPAQUE | CTLFLAG_RD,
362 NULL, 0, sysctl_intrnames, "", "Interrupt Names");
363
364static int
365sysctl_intrcnt(SYSCTL_HANDLER_ARGS)
366{
367 return (sysctl_handle_opaque(oidp, intrcnt,
368 (char *)eintrcnt - (char *)intrcnt, req));
369}
370
371SYSCTL_PROC(_hw, OID_AUTO, intrcnt, CTLTYPE_OPAQUE | CTLFLAG_RD,
372 NULL, 0, sysctl_intrcnt, "", "Interrupt Counts");