Make usleep as a cancellation point.
[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 $
a9d00ec1 27 * $DragonFly: src/sys/kern/kern_intr.c,v 1.26 2005/10/15 03:23:01 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 */
96728c05
MD
383static void
384sched_ithd_remote(void *arg)
385{
386 sched_ithd((int)arg);
387}
388
ef0fdad1
MD
389void
390sched_ithd(int intr)
391{
477d3c1c 392 struct intr_info *info;
ef0fdad1 393
477d3c1c
MD
394 info = &intr_info_ary[intr];
395
396 ++info->i_count;
397 if (info->i_valid_thread) {
398 if (info->i_reclist == NULL) {
ef0fdad1 399 printf("sched_ithd: stray interrupt %d\n", intr);
b68b7282 400 } else {
477d3c1c
MD
401 if (info->i_thread.td_gd == mycpu) {
402 info->i_running = 1;
403 /* preemption handled internally */
404 lwkt_schedule(&info->i_thread);
96728c05 405 } else {
477d3c1c
MD
406 lwkt_send_ipiq(info->i_thread.td_gd,
407 sched_ithd_remote, (void *)intr);
96728c05 408 }
b68b7282 409 }
ef0fdad1
MD
410 } else {
411 printf("sched_ithd: stray interrupt %d\n", intr);
412 }
413}
414
37d44089
MD
415/*
416 * This is run from a periodic SYSTIMER (and thus must be MP safe, the BGL
417 * might not be held).
418 */
419static void
477d3c1c 420ithread_livelock_wakeup(systimer_t st)
37d44089 421{
477d3c1c 422 struct intr_info *info;
37d44089 423
477d3c1c
MD
424 info = &intr_info_ary[(int)st->data];
425 if (info->i_valid_thread)
426 lwkt_schedule(&info->i_thread);
37d44089
MD
427}
428
67b9bb39 429/*
477d3c1c
MD
430 * This function is called drectly from the ICU or APIC vector code assembly
431 * to process an interrupt. The critical section and interrupt deferral
432 * checks have already been done but the function is entered WITHOUT
433 * a critical section held. The BGL may or may not be held.
434 *
435 * Must return non-zero if we do not want the vector code to re-enable
436 * the interrupt (which we don't if we have to schedule the interrupt)
67b9bb39 437 */
477d3c1c
MD
438int ithread_fast_handler(struct intrframe frame);
439
440int
441ithread_fast_handler(struct intrframe frame)
442{
443 int intr;
444 struct intr_info *info;
445 struct intrec **list;
446 int must_schedule;
447#ifdef SMP
448 int got_mplock;
449#endif
450 intrec_t rec, next_rec;
451 globaldata_t gd;
452
453 intr = frame.if_vec;
454 gd = mycpu;
455
456 info = &intr_info_ary[intr];
457
458 /*
459 * If we are not processing any FAST interrupts, just schedule the thing.
460 * (since we aren't in a critical section, this can result in a
461 * preemption)
462 */
463 if (info->i_fast == 0) {
464 sched_ithd(intr);
465 return(1);
466 }
467
468 /*
469 * This should not normally occur since interrupts ought to be
470 * masked if the ithread has been scheduled or is running.
471 */
472 if (info->i_running)
473 return(1);
474
475 /*
476 * Bump the interrupt nesting level to process any FAST interrupts.
477 * Obtain the MP lock as necessary. If the MP lock cannot be obtained,
478 * schedule the interrupt thread to deal with the issue instead.
479 *
480 * To reduce overhead, just leave the MP lock held once it has been
481 * obtained.
482 */
483 crit_enter_gd(gd);
484 ++gd->gd_intr_nesting_level;
485 ++gd->gd_cnt.v_intr;
486 must_schedule = info->i_slow;
487#ifdef SMP
488 got_mplock = 0;
489#endif
490
491 list = &info->i_reclist;
492 for (rec = *list; rec; rec = next_rec) {
493 next_rec = rec->next; /* rec may be invalid after call */
494
495 if (rec->intr_flags & INTR_FAST) {
496#ifdef SMP
497 if ((rec->intr_flags & INTR_MPSAFE) == 0 && got_mplock == 0) {
498 if (try_mplock() == 0) {
499 /*
500 * XXX forward to the cpu holding the MP lock
501 */
502 must_schedule = 1;
503 break;
504 }
505 got_mplock = 1;
506 }
507#endif
508 if (rec->serializer) {
509 must_schedule += lwkt_serialize_handler_try(
510 rec->serializer, rec->handler,
511 rec->argument, &frame);
512 } else {
513 rec->handler(rec->argument, &frame);
514 }
515 }
516 }
517
518 /*
519 * Cleanup
520 */
521 --gd->gd_intr_nesting_level;
522#ifdef SMP
523 if (got_mplock)
524 rel_mplock();
525#endif
526 crit_exit_gd(gd);
527
528 /*
529 * If we had a problem, schedule the thread to catch the missed
530 * records (it will just re-run all of them). A return value of 0
531 * indicates that all handlers have been run and the interrupt can
532 * be re-enabled, and a non-zero return indicates that the interrupt
533 * thread controls re-enablement.
534 */
535 if (must_schedule)
536 sched_ithd(intr);
537 else
538 ++info->i_count;
539 return(must_schedule);
540}
541
542#if 0
543
5446: ; \
545 /* could not get the MP lock, forward the interrupt */ \
546 movl mp_lock, %eax ; /* check race */ \
547 cmpl $MP_FREE_LOCK,%eax ; \
548 je 2b ; \
549 incl PCPU(cnt)+V_FORWARDED_INTS ; \
550 subl $12,%esp ; \
551 movl $irq_num,8(%esp) ; \
552 movl $forward_fastint_remote,4(%esp) ; \
553 movl %eax,(%esp) ; \
554 call lwkt_send_ipiq_bycpu ; \
555 addl $12,%esp ; \
556 jmp 5f ;
557
558#endif
67b9bb39 559
37d44089 560
b68b7282 561/*
45d76888
MD
562 * Interrupt threads run this as their main loop.
563 *
564 * The handler begins execution outside a critical section and with the BGL
565 * held.
37d44089 566 *
477d3c1c 567 * The i_running state starts at 0. When an interrupt occurs, the hardware
37d44089
MD
568 * interrupt is disabled and sched_ithd() The HW interrupt remains disabled
569 * until all routines have run. We then call ithread_done() to reenable
45d76888
MD
570 * the HW interrupt and deschedule us until the next interrupt.
571 *
477d3c1c 572 * We are responsible for atomically checking i_running and ithread_done()
45d76888 573 * is responsible for atomically checking for platform-specific delayed
477d3c1c 574 * interrupts. i_running for our irq is only set in the context of our cpu,
45d76888 575 * so a critical section is a sufficient interlock.
b68b7282 576 */
93781523
MD
577#define LIVELOCK_TIMEFRAME(freq) ((freq) >> 2) /* 1/4 second */
578
ef0fdad1
MD
579static void
580ithread_handler(void *arg)
581{
477d3c1c 582 struct intr_info *info;
93781523 583 u_int cputicks;
477d3c1c
MD
584 u_int bticks;
585 int intr;
586 int freq;
587 struct intrec **list;
588 intrec_t rec, nrec;
45d76888 589 globaldata_t gd = mycpu;
67b9bb39
MD
590 struct systimer ill_timer; /* enforced freq. timer */
591 struct systimer ill_rtimer; /* recovery timer */
592 u_int ill_count = 0; /* interrupt livelock counter */
593 u_int ill_ticks = 0; /* track elapsed to calculate freq */
594 u_int ill_delta = 0; /* track elapsed to calculate freq */
595 int ill_state = 0; /* current state */
45d76888 596
477d3c1c
MD
597 intr = (int)arg;
598 info = &intr_info_ary[intr];
599 list = &info->i_reclist;
600 gd = mycpu;
601
45d76888
MD
602 /*
603 * The loop must be entered with one critical section held.
604 */
605 crit_enter_gd(gd);
ef0fdad1 606
ef0fdad1 607 for (;;) {
93781523
MD
608 /*
609 * We can get woken up by the livelock periodic code too, run the
45d76888
MD
610 * handlers only if there is a real interrupt pending. XXX
611 *
477d3c1c 612 * Clear i_running prior to running the handlers to interlock
45d76888 613 * again new events occuring during processing of existing events.
e43a034f 614 *
477d3c1c
MD
615 * Run each handler in a critical section. Note that we run both
616 * FAST and SLOW designated service routines.
93781523 617 */
477d3c1c 618 info->i_running = 0;
a474df86
MD
619 for (rec = *list; rec; rec = nrec) {
620 nrec = rec->next;
477d3c1c
MD
621 if (rec->serializer) {
622 lwkt_serialize_handler_call(rec->serializer,
623 rec->handler, rec->argument, NULL);
624 } else {
625 rec->handler(rec->argument, NULL);
626 }
ef0fdad1 627 }
37d44089 628
e43a034f
MD
629 /*
630 * Do a quick exit/enter to catch any higher-priority
631 * interrupt sources and so user/system/interrupt statistics
632 * work for interrupt threads.
633 */
634 crit_exit_gd(gd);
635 crit_enter_gd(gd);
636
37d44089
MD
637 /*
638 * This is our interrupt hook to add rate randomness to the random
639 * number generator.
640 */
477d3c1c 641 if (info->i_random.sc_enabled)
96728c05 642 add_interrupt_randomness(intr);
37d44089
MD
643
644 /*
645 * This is our livelock test. If we hit the rate limit we
45d76888 646 * limit ourselves to X interrupts/sec until the rate
37d44089 647 * falls below 50% of that value, then we unlimit again.
45d76888
MD
648 *
649 * XXX calling cputimer_count() is expensive but a livelock may
650 * prevent other interrupts from occuring so we cannot use ticks.
37d44089 651 */
044ee7c4 652 cputicks = sys_cputimer->count();
67b9bb39
MD
653 ++ill_count;
654 bticks = cputicks - ill_ticks;
655 ill_ticks = cputicks;
044ee7c4
MD
656 if (bticks > sys_cputimer->freq)
657 bticks = sys_cputimer->freq;
37d44089 658
67b9bb39 659 switch(ill_state) {
37d44089 660 case LIVELOCK_NONE:
67b9bb39
MD
661 ill_delta += bticks;
662 if (ill_delta < LIVELOCK_TIMEFRAME(sys_cputimer->freq))
37d44089 663 break;
67b9bb39
MD
664 freq = (int64_t)ill_count * sys_cputimer->freq /
665 ill_delta;
666 ill_delta = 0;
667 ill_count = 0;
37d44089
MD
668 if (freq < livelock_limit)
669 break;
670 printf("intr %d at %d hz, livelocked! limiting at %d hz\n",
671 intr, freq, livelock_fallback);
67b9bb39 672 ill_state = LIVELOCK_LIMITED;
37d44089 673 bticks = 0;
93781523 674 /* force periodic check to avoid stale removal (if ints stop) */
67b9bb39
MD
675 systimer_init_periodic(&ill_rtimer, ithread_livelock_wakeup,
676 (void *)intr, 1);
37d44089
MD
677 /* fall through */
678 case LIVELOCK_LIMITED:
679 /*
680 * Delay (us) before rearming the interrupt
681 */
67b9bb39 682 systimer_init_oneshot(&ill_timer, ithread_livelock_wakeup,
37d44089
MD
683 (void *)intr, 1 + 1000000 / livelock_fallback);
684 lwkt_deschedule_self(curthread);
685 lwkt_switch();
93781523
MD
686
687 /* in case we were woken up by something else */
67b9bb39 688 systimer_del(&ill_timer);
37d44089
MD
689
690 /*
691 * Calculate interrupt rate (note that due to our delay it
692 * will not exceed livelock_fallback).
693 */
67b9bb39
MD
694 ill_delta += bticks;
695 if (ill_delta < LIVELOCK_TIMEFRAME(sys_cputimer->freq))
37d44089 696 break;
67b9bb39
MD
697 freq = (int64_t)ill_count * sys_cputimer->freq / ill_delta;
698 ill_delta = 0;
699 ill_count = 0;
37d44089
MD
700 if (freq < (livelock_fallback >> 1)) {
701 printf("intr %d at %d hz, removing livelock limit\n",
702 intr, freq);
67b9bb39
MD
703 ill_state = LIVELOCK_NONE;
704 systimer_del(&ill_rtimer);
37d44089
MD
705 }
706 break;
707 }
708
709 /*
477d3c1c 710 * There are two races here. i_running is set by sched_ithd()
45d76888
MD
711 * in the context of our cpu and is critical-section safe. We
712 * are responsible for checking it. ipending is not critical
713 * section safe and must be handled by the platform specific
714 * ithread_done() routine.
37d44089 715 */
477d3c1c 716 if (info->i_running == 0)
e43a034f 717 ithread_done(intr);
45d76888 718 /* must be in critical section on loop */
ef0fdad1 719 }
e43a034f 720 /* not reached */
ef0fdad1
MD
721}
722
a9d00ec1
MD
723/*
724 * Emergency interrupt polling thread. The thread begins execution
725 * outside a critical section with the BGL held.
726 *
727 * If emergency interrupt polling is enabled, this thread will
728 * execute all system interrupts not marked INTR_NOPOLL at the
729 * specified polling frequency.
730 *
731 * WARNING! This thread runs *ALL* interrupt service routines that
732 * are not marked INTR_NOPOLL, which basically means everything except
733 * the 8254 clock interrupt and the ATA interrupt. It has very high
734 * overhead and should only be used in situations where the machine
735 * cannot otherwise be made to work. Due to the severe performance
736 * degredation, it should not be enabled on production machines.
737 */
738static void
739ithread_emergency(void *arg __unused)
740{
741 struct intr_info *info;
742 intrec_t rec, nrec;
743 int intr;
744
745 for (;;) {
746 for (intr = 0; intr < NHWI + NSWI; ++intr) {
747 info = &intr_info_ary[intr];
748 for (rec = info->i_reclist; rec; rec = nrec) {
749 if ((rec->intr_flags & INTR_NOPOLL) == 0) {
750 if (rec->serializer) {
751 lwkt_serialize_handler_call(rec->serializer,
752 rec->handler, rec->argument, NULL);
753 } else {
754 rec->handler(rec->argument, NULL);
755 }
756 }
757 nrec = rec->next;
758 }
759 }
760 lwkt_deschedule_self(curthread);
761 lwkt_switch();
762 }
763}
764
765/*
766 * Systimer callback - schedule the emergency interrupt poll thread
767 * if emergency polling is enabled.
768 */
769static
770void
771emergency_intr_timer_callback(systimer_t info, struct intrframe *frame __unused)
772{
773 if (emergency_intr_enable)
774 lwkt_schedule(info->data);
775}
776
984263bc
MD
777/*
778 * Sysctls used by systat and others: hw.intrnames and hw.intrcnt.
779 * The data for this machine dependent, and the declarations are in machine
780 * dependent code. The layout of intrnames and intrcnt however is machine
781 * independent.
782 *
783 * We do not know the length of intrcnt and intrnames at compile time, so
784 * calculate things at run time.
785 */
477d3c1c 786
984263bc
MD
787static int
788sysctl_intrnames(SYSCTL_HANDLER_ARGS)
789{
477d3c1c
MD
790 struct intr_info *info;
791 intrec_t rec;
792 int error = 0;
793 int len;
794 int intr;
795 char buf[64];
796
797 for (intr = 0; error == 0 && intr < NHWI + NSWI; ++intr) {
798 info = &intr_info_ary[intr];
799
800 len = 0;
801 buf[0] = 0;
802 for (rec = info->i_reclist; rec; rec = rec->next) {
803 snprintf(buf + len, sizeof(buf) - len, "%s%s",
804 (len ? "/" : ""), rec->name);
805 len += strlen(buf + len);
806 }
807 if (len == 0) {
808 snprintf(buf, sizeof(buf), "irq%d", intr);
809 len = strlen(buf);
810 }
811 error = SYSCTL_OUT(req, buf, len + 1);
812 }
813 return (error);
984263bc
MD
814}
815
477d3c1c 816
984263bc
MD
817SYSCTL_PROC(_hw, OID_AUTO, intrnames, CTLTYPE_OPAQUE | CTLFLAG_RD,
818 NULL, 0, sysctl_intrnames, "", "Interrupt Names");
819
820static int
821sysctl_intrcnt(SYSCTL_HANDLER_ARGS)
822{
477d3c1c
MD
823 struct intr_info *info;
824 int error = 0;
825 int intr;
826
827 for (intr = 0; intr < NHWI + NSWI; ++intr) {
828 info = &intr_info_ary[intr];
829
830 error = SYSCTL_OUT(req, &info->i_count, sizeof(info->i_count));
831 if (error)
832 break;
833 }
834 return(error);
984263bc
MD
835}
836
837SYSCTL_PROC(_hw, OID_AUTO, intrcnt, CTLTYPE_OPAQUE | CTLFLAG_RD,
838 NULL, 0, sysctl_intrcnt, "", "Interrupt Counts");
477d3c1c 839