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