Add a sysctl and tunable kern.syscall_mpsafe which allows system calls
[dragonfly.git] / sys / kern / kern_intr.c
CommitLineData
984263bc 1/*
033a4603 2 * Copyright (c) 2003 Matthew Dillon <dillon@backplane.com> All rights reserved.
ef0fdad1 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 $
862f2618 27 * $DragonFly: src/sys/kern/kern_intr.c,v 1.35 2005/11/21 21:50:26 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>
477d3c1c
MD
40#include <sys/serialize.h>
41#include <sys/bus.h>
37e7efec 42#include <sys/machintr.h>
984263bc
MD
43
44#include <machine/ipl.h>
477d3c1c 45#include <machine/frame.h>
984263bc
MD
46
47#include <sys/interrupt.h>
48
9d522d14
MD
49struct info_info;
50
ef0fdad1
MD
51typedef struct intrec {
52 struct intrec *next;
9d522d14 53 struct intr_info *info;
ef0fdad1
MD
54 inthand2_t *handler;
55 void *argument;
477d3c1c 56 char *name;
ef0fdad1 57 int intr;
477d3c1c
MD
58 int intr_flags;
59 struct lwkt_serialize *serializer;
60} *intrec_t;
61
62struct intr_info {
63 intrec_t i_reclist;
64 struct thread i_thread;
65 struct random_softc i_random;
66 int i_running;
862f2618
MD
67 long i_count; /* interrupts dispatched */
68 int i_mplock_required;
477d3c1c
MD
69 int i_fast;
70 int i_slow;
f33e9c1c 71 int i_state;
5f456c40
MD
72} intr_info_ary[MAX_INTS];
73
74int max_installed_hard_intr;
75int max_installed_soft_intr;
477d3c1c 76
a9d00ec1
MD
77#define EMERGENCY_INTR_POLLING_FREQ_MAX 20000
78
79static int sysctl_emergency_freq(SYSCTL_HANDLER_ARGS);
80static int sysctl_emergency_enable(SYSCTL_HANDLER_ARGS);
81static void emergency_intr_timer_callback(systimer_t, struct intrframe *);
82static void ithread_handler(void *arg);
83static void ithread_emergency(void *arg);
84
477d3c1c 85int intr_info_size = sizeof(intr_info_ary) / sizeof(intr_info_ary[0]);
37d44089 86
a9d00ec1
MD
87static struct systimer emergency_intr_timer;
88static struct thread emergency_intr_thread;
89
f33e9c1c
MD
90#define ISTATE_NOTHREAD 0
91#define ISTATE_NORMAL 1
92#define ISTATE_LIVELOCKED 2
37d44089 93
862f2618 94static int intr_mpsafe = 0;
93781523 95static int livelock_limit = 50000;
f33e9c1c 96static int livelock_lowater = 20000;
862f2618
MD
97TUNABLE_INT("kern.intr_mpsafe", &intr_mpsafe);
98SYSCTL_INT(_kern, OID_AUTO, intr_mpsafe,
99 CTLFLAG_RW, &intr_mpsafe, 0, "Run INTR_MPSAFE handlers without the BGL");
37d44089
MD
100SYSCTL_INT(_kern, OID_AUTO, livelock_limit,
101 CTLFLAG_RW, &livelock_limit, 0, "Livelock interrupt rate limit");
f33e9c1c
MD
102SYSCTL_INT(_kern, OID_AUTO, livelock_lowater,
103 CTLFLAG_RW, &livelock_lowater, 0, "Livelock low-water mark restore");
984263bc 104
a9d00ec1
MD
105static int emergency_intr_enable = 0; /* emergency interrupt polling */
106TUNABLE_INT("kern.emergency_intr_enable", &emergency_intr_enable);
107SYSCTL_PROC(_kern, OID_AUTO, emergency_intr_enable, CTLTYPE_INT | CTLFLAG_RW,
108 0, 0, sysctl_emergency_enable, "I", "Emergency Interrupt Poll Enable");
109
110static int emergency_intr_freq = 10; /* emergency polling frequency */
111TUNABLE_INT("kern.emergency_intr_freq", &emergency_intr_freq);
112SYSCTL_PROC(_kern, OID_AUTO, emergency_intr_freq, CTLTYPE_INT | CTLFLAG_RW,
113 0, 0, sysctl_emergency_freq, "I", "Emergency Interrupt Poll Frequency");
114
115/*
116 * Sysctl support routines
117 */
118static int
119sysctl_emergency_enable(SYSCTL_HANDLER_ARGS)
120{
121 int error, enabled;
122
123 enabled = emergency_intr_enable;
124 error = sysctl_handle_int(oidp, &enabled, 0, req);
125 if (error || req->newptr == NULL)
126 return error;
127 emergency_intr_enable = enabled;
128 if (emergency_intr_enable) {
129 emergency_intr_timer.periodic =
130 sys_cputimer->fromhz(emergency_intr_freq);
131 } else {
132 emergency_intr_timer.periodic = sys_cputimer->fromhz(1);
133 }
134 return 0;
135}
136
137static int
138sysctl_emergency_freq(SYSCTL_HANDLER_ARGS)
139{
140 int error, phz;
141
142 phz = emergency_intr_freq;
143 error = sysctl_handle_int(oidp, &phz, 0, req);
144 if (error || req->newptr == NULL)
145 return error;
146 if (phz <= 0)
147 return EINVAL;
148 else if (phz > EMERGENCY_INTR_POLLING_FREQ_MAX)
149 phz = EMERGENCY_INTR_POLLING_FREQ_MAX;
150
151 emergency_intr_freq = phz;
152 if (emergency_intr_enable) {
153 emergency_intr_timer.periodic =
154 sys_cputimer->fromhz(emergency_intr_freq);
155 } else {
156 emergency_intr_timer.periodic = sys_cputimer->fromhz(1);
157 }
158 return 0;
159}
984263bc 160
45d76888
MD
161/*
162 * Register an SWI or INTerrupt handler.
45d76888 163 */
477d3c1c
MD
164void *
165register_swi(int intr, inthand2_t *handler, void *arg, const char *name,
166 struct lwkt_serialize *serializer)
984263bc 167{
5f456c40 168 if (intr < FIRST_SOFTINT || intr >= MAX_INTS)
ef0fdad1 169 panic("register_swi: bad intr %d", intr);
477d3c1c 170 return(register_int(intr, handler, arg, name, serializer, 0));
984263bc
MD
171}
172
477d3c1c
MD
173void *
174register_int(int intr, inthand2_t *handler, void *arg, const char *name,
175 struct lwkt_serialize *serializer, int intr_flags)
984263bc 176{
477d3c1c
MD
177 struct intr_info *info;
178 struct intrec **list;
179 intrec_t rec;
ef0fdad1 180
5f456c40 181 if (intr < 0 || intr >= MAX_INTS)
ef0fdad1 182 panic("register_int: bad intr %d", intr);
477d3c1c
MD
183 if (name == NULL)
184 name = "???";
185 info = &intr_info_ary[intr];
186
9d522d14
MD
187 /*
188 * Construct an interrupt handler record
189 */
477d3c1c
MD
190 rec = malloc(sizeof(struct intrec), M_DEVBUF, M_INTWAIT);
191 rec->name = malloc(strlen(name) + 1, M_DEVBUF, M_INTWAIT);
192 strcpy(rec->name, name);
ef0fdad1 193
9d522d14 194 rec->info = info;
ef0fdad1
MD
195 rec->handler = handler;
196 rec->argument = arg;
ef0fdad1 197 rec->intr = intr;
477d3c1c 198 rec->intr_flags = intr_flags;
ef0fdad1 199 rec->next = NULL;
477d3c1c 200 rec->serializer = serializer;
ef0fdad1 201
a9d00ec1
MD
202 /*
203 * Create an emergency polling thread and set up a systimer to wake
204 * it up.
205 */
206 if (emergency_intr_thread.td_kstack == NULL) {
207 lwkt_create(ithread_emergency, NULL, NULL,
208 &emergency_intr_thread, TDF_STOPREQ|TDF_INTTHREAD, -1,
209 "ithread emerg");
210 systimer_init_periodic_nq(&emergency_intr_timer,
211 emergency_intr_timer_callback, &emergency_intr_thread,
212 (emergency_intr_enable ? emergency_intr_freq : 1));
213 }
214
ef0fdad1
MD
215 /*
216 * Create an interrupt thread if necessary, leave it in an unscheduled
45d76888 217 * state.
ef0fdad1 218 */
f33e9c1c
MD
219 if (info->i_state == ISTATE_NOTHREAD) {
220 info->i_state = ISTATE_NORMAL;
477d3c1c 221 lwkt_create((void *)ithread_handler, (void *)intr, NULL,
862f2618 222 &info->i_thread, TDF_STOPREQ|TDF_INTTHREAD|TDF_MPSAFE, -1,
75cdbe6c 223 "ithread %d", intr);
5f456c40 224 if (intr >= FIRST_SOFTINT)
477d3c1c 225 lwkt_setpri(&info->i_thread, TDPRI_SOFT_NORM);
4b5f931b 226 else
477d3c1c
MD
227 lwkt_setpri(&info->i_thread, TDPRI_INT_MED);
228 info->i_thread.td_preemptable = lwkt_preempt;
ef0fdad1
MD
229 }
230
9d522d14
MD
231 list = &info->i_reclist;
232
ef0fdad1 233 /*
9d522d14 234 * Keep track of how many fast and slow interrupts we have.
862f2618
MD
235 * Set i_mplock_required if any handler in the chain requires
236 * the MP lock to operate.
ef0fdad1 237 */
862f2618
MD
238 if ((intr_flags & INTR_MPSAFE) == 0)
239 info->i_mplock_required = 1;
9d522d14
MD
240 if (intr_flags & INTR_FAST)
241 ++info->i_fast;
242 else
243 ++info->i_slow;
244
245 /*
246 * Add the record to the interrupt list.
247 */
248 crit_enter();
ef0fdad1
MD
249 while (*list != NULL)
250 list = &(*list)->next;
251 *list = rec;
252 crit_exit();
5f456c40
MD
253
254 /*
255 * Update max_installed_hard_intr to make the emergency intr poll
256 * a bit more efficient.
257 */
258 if (intr < FIRST_SOFTINT) {
259 if (max_installed_hard_intr <= intr)
260 max_installed_hard_intr = intr + 1;
261 } else {
262 if (max_installed_soft_intr <= intr)
263 max_installed_soft_intr = intr + 1;
264 }
9d522d14
MD
265
266 /*
267 * Setup the machine level interrupt vector
268 */
269 if (info->i_slow + info->i_fast == 1) {
270 if (machintr_vector_setup(intr, intr_flags))
271 printf("machintr_vector_setup: failed on irq %d\n", intr);
272 }
273
477d3c1c 274 return(rec);
ef0fdad1 275}
984263bc 276
9d522d14 277void
477d3c1c 278unregister_swi(void *id)
ef0fdad1 279{
9d522d14 280 unregister_int(id);
984263bc
MD
281}
282
9d522d14 283void
477d3c1c 284unregister_int(void *id)
984263bc 285{
477d3c1c
MD
286 struct intr_info *info;
287 struct intrec **list;
288 intrec_t rec;
289 int intr;
290
291 intr = ((intrec_t)id)->intr;
ef0fdad1 292
5f456c40 293 if (intr < 0 || intr >= MAX_INTS)
ef0fdad1 294 panic("register_int: bad intr %d", intr);
477d3c1c
MD
295
296 info = &intr_info_ary[intr];
297
298 /*
9d522d14
MD
299 * Remove the interrupt descriptor, adjust the descriptor count,
300 * and teardown the machine level vector if this was the last interrupt.
477d3c1c 301 */
ef0fdad1 302 crit_enter();
477d3c1c 303 list = &info->i_reclist;
ef0fdad1 304 while ((rec = *list) != NULL) {
9d522d14 305 if (rec == id)
ef0fdad1 306 break;
ef0fdad1
MD
307 list = &rec->next;
308 }
9d522d14
MD
309 if (rec) {
310 *list = rec->next;
311 if (rec->intr_flags & INTR_FAST)
312 --info->i_fast;
313 else
314 --info->i_slow;
315 if (info->i_fast + info->i_slow == 0)
316 machintr_vector_teardown(intr);
317 }
862f2618
MD
318
319 /*
320 * Clear i_mplock_required if no handlers in the chain require the
321 * MP lock.
322 */
323 for (rec = info->i_reclist; rec; rec = rec->next) {
324 if ((rec->intr_flags & INTR_MPSAFE) == 0)
325 break;
326 }
327 if (rec == NULL)
328 info->i_mplock_required = 0;
329
ef0fdad1 330 crit_exit();
477d3c1c
MD
331
332 /*
9d522d14 333 * Free the record.
477d3c1c 334 */
ef0fdad1 335 if (rec != NULL) {
477d3c1c 336 free(rec->name, M_DEVBUF);
ef0fdad1
MD
337 free(rec, M_DEVBUF);
338 } else {
477d3c1c
MD
339 printf("warning: unregister_int: int %d handler for %s not found\n",
340 intr, ((intrec_t)id)->name);
ef0fdad1 341 }
477d3c1c
MD
342}
343
344const char *
345get_registered_name(int intr)
346{
347 intrec_t rec;
348
5f456c40 349 if (intr < 0 || intr >= MAX_INTS)
477d3c1c
MD
350 panic("register_int: bad intr %d", intr);
351
352 if ((rec = intr_info_ary[intr].i_reclist) == NULL)
353 return(NULL);
354 else if (rec->next)
355 return("mux");
356 else
357 return(rec->name);
984263bc
MD
358}
359
477d3c1c
MD
360int
361count_registered_ints(int intr)
362{
363 struct intr_info *info;
364
5f456c40 365 if (intr < 0 || intr >= MAX_INTS)
477d3c1c
MD
366 panic("register_int: bad intr %d", intr);
367 info = &intr_info_ary[intr];
368 return(info->i_fast + info->i_slow);
369}
370
371long
372get_interrupt_counter(int intr)
373{
374 struct intr_info *info;
375
5f456c40 376 if (intr < 0 || intr >= MAX_INTS)
477d3c1c
MD
377 panic("register_int: bad intr %d", intr);
378 info = &intr_info_ary[intr];
379 return(info->i_count);
380}
381
382
4b5f931b
MD
383void
384swi_setpriority(int intr, int pri)
385{
477d3c1c 386 struct intr_info *info;
4b5f931b 387
5f456c40 388 if (intr < FIRST_SOFTINT || intr >= MAX_INTS)
4b5f931b 389 panic("register_swi: bad intr %d", intr);
477d3c1c 390 info = &intr_info_ary[intr];
f33e9c1c 391 if (info->i_state != ISTATE_NOTHREAD)
477d3c1c 392 lwkt_setpri(&info->i_thread, pri);
4b5f931b
MD
393}
394
7e071e7a
MD
395void
396register_randintr(int intr)
397{
477d3c1c
MD
398 struct intr_info *info;
399
5f456c40 400 if (intr < 0 || intr >= MAX_INTS)
417c990a 401 panic("register_randintr: bad intr %d", intr);
477d3c1c
MD
402 info = &intr_info_ary[intr];
403 info->i_random.sc_intr = intr;
404 info->i_random.sc_enabled = 1;
7e071e7a
MD
405}
406
407void
408unregister_randintr(int intr)
409{
477d3c1c
MD
410 struct intr_info *info;
411
5f456c40 412 if (intr < 0 || intr >= MAX_INTS)
477d3c1c
MD
413 panic("register_swi: bad intr %d", intr);
414 info = &intr_info_ary[intr];
415 info->i_random.sc_enabled = 0;
7e071e7a
MD
416}
417
5f456c40
MD
418int
419next_registered_randintr(int intr)
420{
421 struct intr_info *info;
422
423 if (intr < 0 || intr >= MAX_INTS)
424 panic("register_swi: bad intr %d", intr);
425 while (intr < MAX_INTS) {
426 info = &intr_info_ary[intr];
427 if (info->i_random.sc_enabled)
428 break;
429 ++intr;
430 }
431 return(intr);
432}
433
ef0fdad1 434/*
b68b7282
MD
435 * Dispatch an interrupt. If there's nothing to do we have a stray
436 * interrupt and can just return, leaving the interrupt masked.
96728c05 437 *
477d3c1c 438 * We need to schedule the interrupt and set its i_running bit. If
96728c05
MD
439 * we are not on the interrupt thread's cpu we have to send a message
440 * to the correct cpu that will issue the desired action (interlocking
f33e9c1c
MD
441 * with the interrupt thread's critical section). We do NOT attempt to
442 * reschedule interrupts whos i_running bit is already set because
443 * this would prematurely wakeup a livelock-limited interrupt thread.
444 *
445 * i_running is only tested/set on the same cpu as the interrupt thread.
96728c05
MD
446 *
447 * We are NOT in a critical section, which will allow the scheduled
71ef2f5c 448 * interrupt to preempt us. The MP lock might *NOT* be held here.
ef0fdad1 449 */
b8a98473
MD
450#ifdef SMP
451
96728c05
MD
452static void
453sched_ithd_remote(void *arg)
454{
455 sched_ithd((int)arg);
456}
457
b8a98473
MD
458#endif
459
ef0fdad1
MD
460void
461sched_ithd(int intr)
462{
477d3c1c 463 struct intr_info *info;
ef0fdad1 464
477d3c1c
MD
465 info = &intr_info_ary[intr];
466
467 ++info->i_count;
f33e9c1c 468 if (info->i_state != ISTATE_NOTHREAD) {
477d3c1c 469 if (info->i_reclist == NULL) {
ef0fdad1 470 printf("sched_ithd: stray interrupt %d\n", intr);
b68b7282 471 } else {
b8a98473 472#ifdef SMP
477d3c1c 473 if (info->i_thread.td_gd == mycpu) {
f33e9c1c
MD
474 if (info->i_running == 0) {
475 info->i_running = 1;
476 if (info->i_state != ISTATE_LIVELOCKED)
477 lwkt_schedule(&info->i_thread); /* MIGHT PREEMPT */
478 }
96728c05 479 } else {
477d3c1c
MD
480 lwkt_send_ipiq(info->i_thread.td_gd,
481 sched_ithd_remote, (void *)intr);
96728c05 482 }
b8a98473 483#else
f33e9c1c
MD
484 if (info->i_running == 0) {
485 info->i_running = 1;
486 if (info->i_state != ISTATE_LIVELOCKED)
487 lwkt_schedule(&info->i_thread); /* MIGHT PREEMPT */
488 }
b8a98473 489#endif
b68b7282 490 }
ef0fdad1
MD
491 } else {
492 printf("sched_ithd: stray interrupt %d\n", intr);
493 }
494}
495
37d44089
MD
496/*
497 * This is run from a periodic SYSTIMER (and thus must be MP safe, the BGL
498 * might not be held).
499 */
500static void
477d3c1c 501ithread_livelock_wakeup(systimer_t st)
37d44089 502{
477d3c1c 503 struct intr_info *info;
37d44089 504
477d3c1c 505 info = &intr_info_ary[(int)st->data];
f33e9c1c 506 if (info->i_state != ISTATE_NOTHREAD)
477d3c1c 507 lwkt_schedule(&info->i_thread);
37d44089
MD
508}
509
67b9bb39 510/*
477d3c1c
MD
511 * This function is called drectly from the ICU or APIC vector code assembly
512 * to process an interrupt. The critical section and interrupt deferral
513 * checks have already been done but the function is entered WITHOUT
514 * a critical section held. The BGL may or may not be held.
515 *
516 * Must return non-zero if we do not want the vector code to re-enable
517 * the interrupt (which we don't if we have to schedule the interrupt)
67b9bb39 518 */
477d3c1c
MD
519int ithread_fast_handler(struct intrframe frame);
520
521int
522ithread_fast_handler(struct intrframe frame)
523{
524 int intr;
525 struct intr_info *info;
526 struct intrec **list;
527 int must_schedule;
528#ifdef SMP
529 int got_mplock;
530#endif
531 intrec_t rec, next_rec;
532 globaldata_t gd;
533
534 intr = frame.if_vec;
535 gd = mycpu;
536
537 info = &intr_info_ary[intr];
538
539 /*
540 * If we are not processing any FAST interrupts, just schedule the thing.
541 * (since we aren't in a critical section, this can result in a
542 * preemption)
543 */
544 if (info->i_fast == 0) {
545 sched_ithd(intr);
546 return(1);
547 }
548
549 /*
550 * This should not normally occur since interrupts ought to be
551 * masked if the ithread has been scheduled or is running.
552 */
553 if (info->i_running)
554 return(1);
555
556 /*
557 * Bump the interrupt nesting level to process any FAST interrupts.
558 * Obtain the MP lock as necessary. If the MP lock cannot be obtained,
559 * schedule the interrupt thread to deal with the issue instead.
560 *
561 * To reduce overhead, just leave the MP lock held once it has been
562 * obtained.
563 */
564 crit_enter_gd(gd);
565 ++gd->gd_intr_nesting_level;
566 ++gd->gd_cnt.v_intr;
567 must_schedule = info->i_slow;
568#ifdef SMP
569 got_mplock = 0;
570#endif
571
572 list = &info->i_reclist;
573 for (rec = *list; rec; rec = next_rec) {
574 next_rec = rec->next; /* rec may be invalid after call */
575
576 if (rec->intr_flags & INTR_FAST) {
577#ifdef SMP
578 if ((rec->intr_flags & INTR_MPSAFE) == 0 && got_mplock == 0) {
579 if (try_mplock() == 0) {
afd7b1c0
MD
580 int owner;
581
477d3c1c 582 /*
afd7b1c0
MD
583 * If we couldn't get the MP lock try to forward it
584 * to the cpu holding the MP lock, setting must_schedule
585 * to -1 so we do not schedule and also do not unmask
586 * the interrupt. Otherwise just schedule it.
477d3c1c 587 */
afd7b1c0
MD
588 owner = owner_mplock();
589 if (owner >= 0 && owner != gd->gd_cpuid) {
590 lwkt_send_ipiq_bycpu(owner, forward_fastint_remote,
7e2d9bde 591 (void *)intr);
afd7b1c0
MD
592 must_schedule = -1;
593 ++gd->gd_cnt.v_forwarded_ints;
594 } else {
595 must_schedule = 1;
596 }
477d3c1c
MD
597 break;
598 }
599 got_mplock = 1;
600 }
601#endif
602 if (rec->serializer) {
603 must_schedule += lwkt_serialize_handler_try(
604 rec->serializer, rec->handler,
605 rec->argument, &frame);
606 } else {
607 rec->handler(rec->argument, &frame);
608 }
609 }
610 }
611
612 /*
613 * Cleanup
614 */
615 --gd->gd_intr_nesting_level;
616#ifdef SMP
617 if (got_mplock)
618 rel_mplock();
619#endif
620 crit_exit_gd(gd);
621
622 /*
623 * If we had a problem, schedule the thread to catch the missed
624 * records (it will just re-run all of them). A return value of 0
625 * indicates that all handlers have been run and the interrupt can
626 * be re-enabled, and a non-zero return indicates that the interrupt
627 * thread controls re-enablement.
628 */
afd7b1c0 629 if (must_schedule > 0)
477d3c1c 630 sched_ithd(intr);
afd7b1c0 631 else if (must_schedule == 0)
477d3c1c
MD
632 ++info->i_count;
633 return(must_schedule);
634}
635
636#if 0
637
6386: ; \
639 /* could not get the MP lock, forward the interrupt */ \
640 movl mp_lock, %eax ; /* check race */ \
641 cmpl $MP_FREE_LOCK,%eax ; \
642 je 2b ; \
643 incl PCPU(cnt)+V_FORWARDED_INTS ; \
644 subl $12,%esp ; \
645 movl $irq_num,8(%esp) ; \
646 movl $forward_fastint_remote,4(%esp) ; \
647 movl %eax,(%esp) ; \
648 call lwkt_send_ipiq_bycpu ; \
649 addl $12,%esp ; \
650 jmp 5f ;
651
652#endif
67b9bb39 653
37d44089 654
b68b7282 655/*
45d76888
MD
656 * Interrupt threads run this as their main loop.
657 *
658 * The handler begins execution outside a critical section and with the BGL
659 * held.
37d44089 660 *
477d3c1c 661 * The i_running state starts at 0. When an interrupt occurs, the hardware
37d44089
MD
662 * interrupt is disabled and sched_ithd() The HW interrupt remains disabled
663 * until all routines have run. We then call ithread_done() to reenable
45d76888
MD
664 * the HW interrupt and deschedule us until the next interrupt.
665 *
477d3c1c 666 * We are responsible for atomically checking i_running and ithread_done()
45d76888 667 * is responsible for atomically checking for platform-specific delayed
477d3c1c 668 * interrupts. i_running for our irq is only set in the context of our cpu,
45d76888 669 * so a critical section is a sufficient interlock.
b68b7282 670 */
93781523
MD
671#define LIVELOCK_TIMEFRAME(freq) ((freq) >> 2) /* 1/4 second */
672
ef0fdad1
MD
673static void
674ithread_handler(void *arg)
675{
477d3c1c 676 struct intr_info *info;
f33e9c1c
MD
677 int use_limit;
678 int lticks;
679 int lcount;
477d3c1c 680 int intr;
9d522d14 681 int mpheld;
477d3c1c
MD
682 struct intrec **list;
683 intrec_t rec, nrec;
f33e9c1c 684 globaldata_t gd;
67b9bb39 685 struct systimer ill_timer; /* enforced freq. timer */
f33e9c1c 686 u_int ill_count; /* interrupt livelock counter */
45d76888 687
f33e9c1c
MD
688 ill_count = 0;
689 lticks = ticks;
690 lcount = 0;
477d3c1c
MD
691 intr = (int)arg;
692 info = &intr_info_ary[intr];
693 list = &info->i_reclist;
694 gd = mycpu;
695
45d76888 696 /*
862f2618
MD
697 * The loop must be entered with one critical section held. The thread
698 * is created with TDF_MPSAFE so the MP lock is not held on start.
45d76888
MD
699 */
700 crit_enter_gd(gd);
862f2618 701 mpheld = 0;
ef0fdad1 702
ef0fdad1 703 for (;;) {
862f2618
MD
704 /*
705 * The chain is only considered MPSAFE if all its interrupt handlers
706 * are MPSAFE. However, if intr_mpsafe has been turned off we
707 * always operate with the BGL.
708 */
709 if (intr_mpsafe == 0) {
710 if (mpheld == 0) {
711 get_mplock();
712 mpheld = 1;
713 }
714 } else if (info->i_mplock_required != mpheld) {
715 if (info->i_mplock_required) {
716 KKASSERT(mpheld == 0);
717 get_mplock();
718 mpheld = 1;
719 } else {
720 KKASSERT(mpheld != 0);
721 rel_mplock();
722 mpheld = 0;
723 }
724 }
725
93781523 726 /*
f33e9c1c
MD
727 * If an interrupt is pending, clear i_running and execute the
728 * handlers. Note that certain types of interrupts can re-trigger
729 * and set i_running again.
45d76888 730 *
f33e9c1c 731 * Each handler is run in a critical section. Note that we run both
862f2618 732 * FAST and SLOW designated service routines.
93781523 733 */
f33e9c1c
MD
734 if (info->i_running) {
735 ++ill_count;
736 info->i_running = 0;
9d522d14 737
f33e9c1c
MD
738 for (rec = *list; rec; rec = nrec) {
739 nrec = rec->next;
740 if (rec->serializer) {
741 lwkt_serialize_handler_call(rec->serializer, rec->handler,
742 rec->argument, NULL);
743 } else {
744 rec->handler(rec->argument, NULL);
745 }
477d3c1c 746 }
ef0fdad1 747 }
37d44089
MD
748
749 /*
750 * This is our interrupt hook to add rate randomness to the random
751 * number generator.
752 */
477d3c1c 753 if (info->i_random.sc_enabled)
96728c05 754 add_interrupt_randomness(intr);
37d44089
MD
755
756 /*
f33e9c1c
MD
757 * Unmask the interrupt to allow it to trigger again. This only
758 * applies to certain types of interrupts (typ level interrupts).
759 * This can result in the interrupt retriggering, but the retrigger
760 * will not be processed until we cycle our critical section.
363d922a
MD
761 *
762 * Only unmask interrupts while handlers are installed. It is
763 * possible to hit a situation where no handlers are installed
764 * due to a device driver livelocking and then tearing down its
765 * interrupt on close (the parallel bus being a good example).
37d44089 766 */
363d922a 767 if (*list)
37e7efec 768 machintr_intren(intr);
f33e9c1c
MD
769
770 /*
771 * Do a quick exit/enter to catch any higher-priority interrupt
772 * sources, such as the statclock, so thread time accounting
773 * will still work. This may also cause an interrupt to re-trigger.
774 */
775 crit_exit_gd(gd);
776 crit_enter_gd(gd);
777
778 /*
779 * LIVELOCK STATE MACHINE
780 */
781 switch(info->i_state) {
782 case ISTATE_NORMAL:
783 /*
784 * Calculate a running average every tick.
785 */
786 if (lticks != ticks) {
787 lticks = ticks;
788 ill_count -= ill_count / hz;
789 }
790
791 /*
792 * If we did not exceed the frequency limit, we are done.
793 * If the interrupt has not retriggered we deschedule ourselves.
794 */
795 if (ill_count <= livelock_limit) {
796 if (info->i_running == 0) {
797 lwkt_deschedule_self(gd->gd_curthread);
798 lwkt_switch();
799 }
37d44089 800 break;
f33e9c1c
MD
801 }
802
803 /*
804 * Otherwise we are livelocked. Set up a periodic systimer
805 * to wake the thread up at the limit frequency.
806 */
807 printf("intr %d at %d > %d hz, livelocked limit engaged!\n",
808 intr, livelock_limit, ill_count);
809 info->i_state = ISTATE_LIVELOCKED;
810 if ((use_limit = livelock_limit) < 100)
811 use_limit = 100;
812 else if (use_limit > 500000)
813 use_limit = 500000;
814 systimer_init_periodic(&ill_timer, ithread_livelock_wakeup,
815 (void *)intr, use_limit);
816 lcount = 0;
37d44089 817 /* fall through */
f33e9c1c 818 case ISTATE_LIVELOCKED:
37d44089 819 /*
f33e9c1c
MD
820 * Wait for our periodic timer to go off. Since the interrupt
821 * has re-armed it can still set i_running, but it will not
822 * reschedule us while we are in a livelocked state.
37d44089 823 */
f33e9c1c 824 lwkt_deschedule_self(gd->gd_curthread);
37d44089 825 lwkt_switch();
93781523 826
37d44089 827 /*
f33e9c1c
MD
828 * Check to see if the livelock condition no longer applies.
829 * The interrupt must be able to operate normally for one
830 * full second before we restore normal operation.
37d44089 831 */
f33e9c1c
MD
832 if (lticks != ticks) {
833 lticks = ticks;
834 if (ill_count < livelock_lowater) {
835 if (++lcount >= hz) {
836 info->i_state = ISTATE_NORMAL;
837 systimer_del(&ill_timer);
838 printf("intr %d at %d < %d hz, livelock removed\n",
839 intr, ill_count, livelock_lowater);
840 }
841 } else {
842 lcount = 0;
843 }
844 ill_count -= ill_count / hz;
37d44089
MD
845 }
846 break;
847 }
ef0fdad1 848 }
e43a034f 849 /* not reached */
ef0fdad1
MD
850}
851
a9d00ec1
MD
852/*
853 * Emergency interrupt polling thread. The thread begins execution
854 * outside a critical section with the BGL held.
855 *
856 * If emergency interrupt polling is enabled, this thread will
857 * execute all system interrupts not marked INTR_NOPOLL at the
858 * specified polling frequency.
859 *
860 * WARNING! This thread runs *ALL* interrupt service routines that
861 * are not marked INTR_NOPOLL, which basically means everything except
862 * the 8254 clock interrupt and the ATA interrupt. It has very high
863 * overhead and should only be used in situations where the machine
864 * cannot otherwise be made to work. Due to the severe performance
865 * degredation, it should not be enabled on production machines.
866 */
867static void
868ithread_emergency(void *arg __unused)
869{
870 struct intr_info *info;
871 intrec_t rec, nrec;
872 int intr;
873
874 for (;;) {
5f456c40 875 for (intr = 0; intr < max_installed_hard_intr; ++intr) {
a9d00ec1
MD
876 info = &intr_info_ary[intr];
877 for (rec = info->i_reclist; rec; rec = nrec) {
878 if ((rec->intr_flags & INTR_NOPOLL) == 0) {
879 if (rec->serializer) {
880 lwkt_serialize_handler_call(rec->serializer,
881 rec->handler, rec->argument, NULL);
882 } else {
883 rec->handler(rec->argument, NULL);
884 }
885 }
886 nrec = rec->next;
887 }
888 }
889 lwkt_deschedule_self(curthread);
890 lwkt_switch();
891 }
892}
893
894/*
895 * Systimer callback - schedule the emergency interrupt poll thread
896 * if emergency polling is enabled.
897 */
898static
899void
900emergency_intr_timer_callback(systimer_t info, struct intrframe *frame __unused)
901{
902 if (emergency_intr_enable)
903 lwkt_schedule(info->data);
904}
905
984263bc
MD
906/*
907 * Sysctls used by systat and others: hw.intrnames and hw.intrcnt.
908 * The data for this machine dependent, and the declarations are in machine
909 * dependent code. The layout of intrnames and intrcnt however is machine
910 * independent.
911 *
912 * We do not know the length of intrcnt and intrnames at compile time, so
913 * calculate things at run time.
914 */
477d3c1c 915
984263bc
MD
916static int
917sysctl_intrnames(SYSCTL_HANDLER_ARGS)
918{
477d3c1c
MD
919 struct intr_info *info;
920 intrec_t rec;
921 int error = 0;
922 int len;
923 int intr;
924 char buf[64];
925
5f456c40 926 for (intr = 0; error == 0 && intr < MAX_INTS; ++intr) {
477d3c1c
MD
927 info = &intr_info_ary[intr];
928
929 len = 0;
930 buf[0] = 0;
931 for (rec = info->i_reclist; rec; rec = rec->next) {
932 snprintf(buf + len, sizeof(buf) - len, "%s%s",
933 (len ? "/" : ""), rec->name);
934 len += strlen(buf + len);
935 }
936 if (len == 0) {
937 snprintf(buf, sizeof(buf), "irq%d", intr);
938 len = strlen(buf);
939 }
940 error = SYSCTL_OUT(req, buf, len + 1);
941 }
942 return (error);
984263bc
MD
943}
944
477d3c1c 945
984263bc
MD
946SYSCTL_PROC(_hw, OID_AUTO, intrnames, CTLTYPE_OPAQUE | CTLFLAG_RD,
947 NULL, 0, sysctl_intrnames, "", "Interrupt Names");
948
949static int
950sysctl_intrcnt(SYSCTL_HANDLER_ARGS)
951{
477d3c1c
MD
952 struct intr_info *info;
953 int error = 0;
954 int intr;
955
5f456c40 956 for (intr = 0; intr < max_installed_hard_intr; ++intr) {
477d3c1c
MD
957 info = &intr_info_ary[intr];
958
959 error = SYSCTL_OUT(req, &info->i_count, sizeof(info->i_count));
960 if (error)
5f456c40
MD
961 goto failed;
962 }
963 for (intr = FIRST_SOFTINT; intr < max_installed_soft_intr; ++intr) {
964 info = &intr_info_ary[intr];
965
966 error = SYSCTL_OUT(req, &info->i_count, sizeof(info->i_count));
967 if (error)
968 goto failed;
477d3c1c 969 }
5f456c40 970failed:
477d3c1c 971 return(error);
984263bc
MD
972}
973
974SYSCTL_PROC(_hw, OID_AUTO, intrcnt, CTLTYPE_OPAQUE | CTLFLAG_RD,
975 NULL, 0, sysctl_intrcnt, "", "Interrupt Counts");
477d3c1c 976