Make usleep as a cancellation point.
[dragonfly.git] / sys / kern / kern_intr.c
1 /*
2  * Copyright (c) 2003 Matthew Dillon <dillon@backplane.com> All rights reserved.
3  * Copyright (c) 1997, Stefan Esser <se@freebsd.org> All rights reserved.
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 $
27  * $DragonFly: src/sys/kern/kern_intr.c,v 1.26 2005/10/15 03:23:01 dillon Exp $
28  *
29  */
30
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>
36 #include <sys/thread.h>
37 #include <sys/proc.h>
38 #include <sys/thread2.h>
39 #include <sys/random.h>
40 #include <sys/serialize.h>
41 #include <sys/bus.h>
42
43 #include <machine/ipl.h>
44 #include <machine/frame.h>
45
46 #include <sys/interrupt.h>
47
48 typedef struct intrec {
49     struct intrec *next;
50     inthand2_t  *handler;
51     void        *argument;
52     char        *name;
53     int         intr;
54     int         intr_flags;
55     struct lwkt_serialize *serializer;
56 } *intrec_t;
57
58 struct 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
69 #define EMERGENCY_INTR_POLLING_FREQ_MAX 20000
70
71 static int sysctl_emergency_freq(SYSCTL_HANDLER_ARGS);
72 static int sysctl_emergency_enable(SYSCTL_HANDLER_ARGS);
73 static void emergency_intr_timer_callback(systimer_t, struct intrframe *);
74 static void ithread_handler(void *arg);
75 static void ithread_emergency(void *arg);
76
77 int intr_info_size = sizeof(intr_info_ary) / sizeof(intr_info_ary[0]);
78
79 static struct systimer emergency_intr_timer;
80 static struct thread emergency_intr_thread;
81
82 #define LIVELOCK_NONE           0
83 #define LIVELOCK_LIMITED        1
84
85 static int livelock_limit = 50000;
86 static int livelock_fallback = 20000;
87 SYSCTL_INT(_kern, OID_AUTO, livelock_limit,
88         CTLFLAG_RW, &livelock_limit, 0, "Livelock interrupt rate limit");
89 SYSCTL_INT(_kern, OID_AUTO, livelock_fallback,
90         CTLFLAG_RW, &livelock_fallback, 0, "Livelock interrupt fallback rate");
91
92 static int emergency_intr_enable = 0;   /* emergency interrupt polling */
93 TUNABLE_INT("kern.emergency_intr_enable", &emergency_intr_enable);
94 SYSCTL_PROC(_kern, OID_AUTO, emergency_intr_enable, CTLTYPE_INT | CTLFLAG_RW,
95         0, 0, sysctl_emergency_enable, "I", "Emergency Interrupt Poll Enable");
96
97 static int emergency_intr_freq = 10;    /* emergency polling frequency */
98 TUNABLE_INT("kern.emergency_intr_freq", &emergency_intr_freq);
99 SYSCTL_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  */
105 static int
106 sysctl_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
124 static int
125 sysctl_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 }
147
148 /*
149  * Register an SWI or INTerrupt handler.
150  */
151 void *
152 register_swi(int intr, inthand2_t *handler, void *arg, const char *name,
153                 struct lwkt_serialize *serializer)
154 {
155     if (intr < NHWI || intr >= NHWI + NSWI)
156         panic("register_swi: bad intr %d", intr);
157     return(register_int(intr, handler, arg, name, serializer, 0));
158 }
159
160 void *
161 register_int(int intr, inthand2_t *handler, void *arg, const char *name,
162                 struct lwkt_serialize *serializer, int intr_flags)
163 {
164     struct intr_info *info;
165     struct intrec **list;
166     intrec_t rec;
167
168     if (intr < 0 || intr >= NHWI + NSWI)
169         panic("register_int: bad intr %d", intr);
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);
177
178     rec->handler = handler;
179     rec->argument = arg;
180     rec->intr = intr;
181     rec->intr_flags = intr_flags;
182     rec->next = NULL;
183     rec->serializer = serializer;
184
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;
194
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
208     /*
209      * Create an interrupt thread if necessary, leave it in an unscheduled
210      * state.
211      */
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, 
216             "ithread %d", intr);
217         if (intr >= NHWI && intr < NHWI + NSWI)
218             lwkt_setpri(&info->i_thread, TDPRI_SOFT_NORM);
219         else
220             lwkt_setpri(&info->i_thread, TDPRI_INT_MED);
221         info->i_thread.td_preemptable = lwkt_preempt;
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();
232     return(rec);
233 }
234
235 int
236 unregister_swi(void *id)
237 {
238     return(unregister_int(id));
239 }
240
241 int
242 unregister_int(void *id)
243 {
244     struct intr_info *info;
245     struct intrec **list;
246     intrec_t rec;
247     int intr;
248
249     intr = ((intrec_t)id)->intr;
250
251     if (intr < 0 || intr > NHWI + NSWI)
252         panic("register_int: bad intr %d", intr);
253
254     info = &intr_info_ary[intr];
255
256     /*
257      * Remove the interrupt descriptor
258      */
259     crit_enter();
260     list = &info->i_reclist;
261     while ((rec = *list) != NULL) {
262         if (rec == id) {
263             *list = rec->next;
264             break;
265         }
266         list = &rec->next;
267     }
268     crit_exit();
269
270     /*
271      * Free it, adjust interrupt type counts
272      */
273     if (rec != NULL) {
274         if (rec->intr_flags & INTR_FAST)
275             --info->i_fast;
276         else
277             --info->i_slow;
278         free(rec->name, M_DEVBUF);
279         free(rec, M_DEVBUF);
280     } else {
281         printf("warning: unregister_int: int %d handler for %s not found\n",
282                 intr, ((intrec_t)id)->name);
283     }
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
291 int
292 get_registered_intr(void *id)
293 {
294     return(((intrec_t)id)->intr);
295 }
296
297 const char *
298 get_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);
311 }
312
313 int
314 count_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
324 long
325 get_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
336 void
337 swi_setpriority(int intr, int pri)
338 {
339     struct intr_info *info;
340
341     if (intr < NHWI || intr >= NHWI + NSWI)
342         panic("register_swi: bad intr %d", intr);
343     info = &intr_info_ary[intr];
344     if (info->i_valid_thread)
345         lwkt_setpri(&info->i_thread, pri);
346 }
347
348 void
349 register_randintr(int intr)
350 {
351     struct intr_info *info;
352
353     if ((unsigned int)intr >= NHWI + NSWI)
354         panic("register_randintr: bad intr %d", intr);
355     info = &intr_info_ary[intr];
356     info->i_random.sc_intr = intr;
357     info->i_random.sc_enabled = 1;
358 }
359
360 void
361 unregister_randintr(int intr)
362 {
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;
369 }
370
371 /*
372  * Dispatch an interrupt.  If there's nothing to do we have a stray
373  * interrupt and can just return, leaving the interrupt masked.
374  *
375  * We need to schedule the interrupt and set its i_running bit.  If
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
381  * interrupt to preempt us.  The MP lock might *NOT* be held here.
382  */
383 static void
384 sched_ithd_remote(void *arg)
385 {
386     sched_ithd((int)arg);
387 }
388
389 void
390 sched_ithd(int intr)
391 {
392     struct intr_info *info;
393
394     info = &intr_info_ary[intr];
395
396     ++info->i_count;
397     if (info->i_valid_thread) {
398         if (info->i_reclist == NULL) {
399             printf("sched_ithd: stray interrupt %d\n", intr);
400         } else {
401             if (info->i_thread.td_gd == mycpu) {
402                 info->i_running = 1;
403                 /* preemption handled internally */
404                 lwkt_schedule(&info->i_thread);
405             } else {
406                 lwkt_send_ipiq(info->i_thread.td_gd, 
407                                 sched_ithd_remote, (void *)intr);
408             }
409         }
410     } else {
411         printf("sched_ithd: stray interrupt %d\n", intr);
412     }
413 }
414
415 /*
416  * This is run from a periodic SYSTIMER (and thus must be MP safe, the BGL
417  * might not be held).
418  */
419 static void
420 ithread_livelock_wakeup(systimer_t st)
421 {
422     struct intr_info *info;
423
424     info = &intr_info_ary[(int)st->data];
425     if (info->i_valid_thread)
426         lwkt_schedule(&info->i_thread);
427 }
428
429 /*
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)
437  */
438 int ithread_fast_handler(struct intrframe frame);
439
440 int
441 ithread_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
544 6: ;                                                                    \
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
559
560
561 /*
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.
566  *
567  * The i_running state starts at 0.  When an interrupt occurs, the hardware
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 
570  * the HW interrupt and deschedule us until the next interrupt. 
571  *
572  * We are responsible for atomically checking i_running and ithread_done()
573  * is responsible for atomically checking for platform-specific delayed
574  * interrupts.  i_running for our irq is only set in the context of our cpu,
575  * so a critical section is a sufficient interlock.
576  */
577 #define LIVELOCK_TIMEFRAME(freq)        ((freq) >> 2)   /* 1/4 second */
578
579 static void
580 ithread_handler(void *arg)
581 {
582     struct intr_info *info;
583     u_int cputicks;
584     u_int bticks;
585     int intr;
586     int freq;
587     struct intrec **list;
588     intrec_t rec, nrec;
589     globaldata_t gd = mycpu;
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 */
596
597     intr = (int)arg;
598     info = &intr_info_ary[intr];
599     list = &info->i_reclist;
600     gd = mycpu;
601
602     /*
603      * The loop must be entered with one critical section held.
604      */
605     crit_enter_gd(gd);
606
607     for (;;) {
608         /*
609          * We can get woken up by the livelock periodic code too, run the 
610          * handlers only if there is a real interrupt pending.  XXX
611          *
612          * Clear i_running prior to running the handlers to interlock
613          * again new events occuring during processing of existing events.
614          *
615          * Run each handler in a critical section.  Note that we run both
616          * FAST and SLOW designated service routines.
617          */
618         info->i_running = 0;
619         for (rec = *list; rec; rec = nrec) {
620             nrec = rec->next;
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             }
627         }
628
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
637         /*
638          * This is our interrupt hook to add rate randomness to the random
639          * number generator.
640          */
641         if (info->i_random.sc_enabled)
642             add_interrupt_randomness(intr);
643
644         /*
645          * This is our livelock test.  If we hit the rate limit we
646          * limit ourselves to X interrupts/sec until the rate
647          * falls below 50% of that value, then we unlimit again.
648          *
649          * XXX calling cputimer_count() is expensive but a livelock may
650          * prevent other interrupts from occuring so we cannot use ticks.
651          */
652         cputicks = sys_cputimer->count();
653         ++ill_count;
654         bticks = cputicks - ill_ticks;
655         ill_ticks = cputicks;
656         if (bticks > sys_cputimer->freq)
657             bticks = sys_cputimer->freq;
658
659         switch(ill_state) {
660         case LIVELOCK_NONE:
661             ill_delta += bticks;
662             if (ill_delta < LIVELOCK_TIMEFRAME(sys_cputimer->freq))
663                 break;
664             freq = (int64_t)ill_count * sys_cputimer->freq / 
665                    ill_delta;
666             ill_delta = 0;
667             ill_count = 0;
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);
672             ill_state = LIVELOCK_LIMITED;
673             bticks = 0;
674             /* force periodic check to avoid stale removal (if ints stop) */
675             systimer_init_periodic(&ill_rtimer, ithread_livelock_wakeup,
676                                     (void *)intr, 1);
677             /* fall through */
678         case LIVELOCK_LIMITED:
679             /*
680              * Delay (us) before rearming the interrupt
681              */
682             systimer_init_oneshot(&ill_timer, ithread_livelock_wakeup,
683                                 (void *)intr, 1 + 1000000 / livelock_fallback);
684             lwkt_deschedule_self(curthread);
685             lwkt_switch();
686
687             /* in case we were woken up by something else */
688             systimer_del(&ill_timer);
689
690             /*
691              * Calculate interrupt rate (note that due to our delay it
692              * will not exceed livelock_fallback).
693              */
694             ill_delta += bticks;
695             if (ill_delta < LIVELOCK_TIMEFRAME(sys_cputimer->freq))
696                 break;
697             freq = (int64_t)ill_count * sys_cputimer->freq / ill_delta;
698             ill_delta = 0;
699             ill_count = 0;
700             if (freq < (livelock_fallback >> 1)) {
701                 printf("intr %d at %d hz, removing livelock limit\n",
702                         intr, freq);
703                 ill_state = LIVELOCK_NONE;
704                 systimer_del(&ill_rtimer);
705             }
706             break;
707         }
708
709         /*
710          * There are two races here.  i_running is set by sched_ithd()
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.
715          */
716         if (info->i_running == 0)
717             ithread_done(intr);
718         /* must be in critical section on loop */
719     }
720     /* not reached */
721 }
722
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  */
738 static void
739 ithread_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  */
769 static
770 void
771 emergency_intr_timer_callback(systimer_t info, struct intrframe *frame __unused)
772 {
773     if (emergency_intr_enable)
774         lwkt_schedule(info->data);
775 }
776
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  */
786
787 static int
788 sysctl_intrnames(SYSCTL_HANDLER_ARGS)
789 {
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);
814 }
815
816
817 SYSCTL_PROC(_hw, OID_AUTO, intrnames, CTLTYPE_OPAQUE | CTLFLAG_RD,
818         NULL, 0, sysctl_intrnames, "", "Interrupt Names");
819
820 static int
821 sysctl_intrcnt(SYSCTL_HANDLER_ARGS)
822 {
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);
835 }
836
837 SYSCTL_PROC(_hw, OID_AUTO, intrcnt, CTLTYPE_OPAQUE | CTLFLAG_RD,
838         NULL, 0, sysctl_intrcnt, "", "Interrupt Counts");
839