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