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