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