ICU/APIC cleanup part 7/many.
[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.31 2005/11/02 22:59:48 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                     /*
551                      * XXX forward to the cpu holding the MP lock
552                      */
553                     must_schedule = 1;
554                     break;
555                 }
556                 got_mplock = 1;
557             }
558 #endif
559             if (rec->serializer) {
560                 must_schedule += lwkt_serialize_handler_try(
561                                         rec->serializer, rec->handler,
562                                         rec->argument, &frame);
563             } else {
564                 rec->handler(rec->argument, &frame);
565             }
566         }
567     }
568
569     /*
570      * Cleanup
571      */
572     --gd->gd_intr_nesting_level;
573 #ifdef SMP
574     if (got_mplock)
575         rel_mplock();
576 #endif
577     crit_exit_gd(gd);
578
579     /*
580      * If we had a problem, schedule the thread to catch the missed
581      * records (it will just re-run all of them).  A return value of 0
582      * indicates that all handlers have been run and the interrupt can
583      * be re-enabled, and a non-zero return indicates that the interrupt
584      * thread controls re-enablement.
585      */
586     if (must_schedule)
587         sched_ithd(intr);
588     else
589         ++info->i_count;
590     return(must_schedule);
591 }
592
593 #if 0
594
595 6: ;                                                                    \
596         /* could not get the MP lock, forward the interrupt */          \
597         movl    mp_lock, %eax ;          /* check race */               \
598         cmpl    $MP_FREE_LOCK,%eax ;                                    \
599         je      2b ;                                                    \
600         incl    PCPU(cnt)+V_FORWARDED_INTS ;                            \
601         subl    $12,%esp ;                                              \
602         movl    $irq_num,8(%esp) ;                                      \
603         movl    $forward_fastint_remote,4(%esp) ;                       \
604         movl    %eax,(%esp) ;                                           \
605         call    lwkt_send_ipiq_bycpu ;                                  \
606         addl    $12,%esp ;                                              \
607         jmp     5f ;                   
608
609 #endif
610
611
612 /*
613  * Interrupt threads run this as their main loop.
614  *
615  * The handler begins execution outside a critical section and with the BGL
616  * held.
617  *
618  * The i_running state starts at 0.  When an interrupt occurs, the hardware
619  * interrupt is disabled and sched_ithd() The HW interrupt remains disabled
620  * until all routines have run.  We then call ithread_done() to reenable 
621  * the HW interrupt and deschedule us until the next interrupt. 
622  *
623  * We are responsible for atomically checking i_running and ithread_done()
624  * is responsible for atomically checking for platform-specific delayed
625  * interrupts.  i_running for our irq is only set in the context of our cpu,
626  * so a critical section is a sufficient interlock.
627  */
628 #define LIVELOCK_TIMEFRAME(freq)        ((freq) >> 2)   /* 1/4 second */
629
630 static void
631 ithread_handler(void *arg)
632 {
633     struct intr_info *info;
634     int use_limit;
635     int lticks;
636     int lcount;
637     int intr;
638     struct intrec **list;
639     intrec_t rec, nrec;
640     globaldata_t gd;
641     struct systimer ill_timer;  /* enforced freq. timer */
642     u_int ill_count;            /* interrupt livelock counter */
643
644     ill_count = 0;
645     lticks = ticks;
646     lcount = 0;
647     intr = (int)arg;
648     info = &intr_info_ary[intr];
649     list = &info->i_reclist;
650     gd = mycpu;
651
652     /*
653      * The loop must be entered with one critical section held.
654      */
655     crit_enter_gd(gd);
656
657     for (;;) {
658         /*
659          * If an interrupt is pending, clear i_running and execute the
660          * handlers.  Note that certain types of interrupts can re-trigger
661          * and set i_running again.
662          *
663          * Each handler is run in a critical section.  Note that we run both
664          * FAST and SLOW designated service routines.
665          */
666         if (info->i_running) {
667             ++ill_count;
668             info->i_running = 0;
669             for (rec = *list; rec; rec = nrec) {
670                 nrec = rec->next;
671                 if (rec->serializer) {
672                     lwkt_serialize_handler_call(rec->serializer, rec->handler,
673                                                 rec->argument, NULL);
674                 } else {
675                     rec->handler(rec->argument, NULL);
676                 }
677             }
678         }
679
680         /*
681          * This is our interrupt hook to add rate randomness to the random
682          * number generator.
683          */
684         if (info->i_random.sc_enabled)
685             add_interrupt_randomness(intr);
686
687         /*
688          * Unmask the interrupt to allow it to trigger again.  This only
689          * applies to certain types of interrupts (typ level interrupts).
690          * This can result in the interrupt retriggering, but the retrigger
691          * will not be processed until we cycle our critical section.
692          *
693          * Only unmask interrupts while handlers are installed.  It is
694          * possible to hit a situation where no handlers are installed
695          * due to a device driver livelocking and then tearing down its
696          * interrupt on close (the parallel bus being a good example).
697          */
698         if (*list)
699             machintr_intren(intr);
700
701         /*
702          * Do a quick exit/enter to catch any higher-priority interrupt
703          * sources, such as the statclock, so thread time accounting
704          * will still work.  This may also cause an interrupt to re-trigger.
705          */
706         crit_exit_gd(gd);
707         crit_enter_gd(gd);
708
709         /*
710          * LIVELOCK STATE MACHINE
711          */
712         switch(info->i_state) {
713         case ISTATE_NORMAL:
714             /*
715              * Calculate a running average every tick.
716              */
717             if (lticks != ticks) {
718                 lticks = ticks;
719                 ill_count -= ill_count / hz;
720             }
721
722             /*
723              * If we did not exceed the frequency limit, we are done.  
724              * If the interrupt has not retriggered we deschedule ourselves.
725              */
726             if (ill_count <= livelock_limit) {
727                 if (info->i_running == 0) {
728                     lwkt_deschedule_self(gd->gd_curthread);
729                     lwkt_switch();
730                 }
731                 break;
732             }
733
734             /*
735              * Otherwise we are livelocked.  Set up a periodic systimer
736              * to wake the thread up at the limit frequency.
737              */
738             printf("intr %d at %d > %d hz, livelocked limit engaged!\n",
739                    intr, livelock_limit, ill_count);
740             info->i_state = ISTATE_LIVELOCKED;
741             if ((use_limit = livelock_limit) < 100)
742                 use_limit = 100;
743             else if (use_limit > 500000)
744                 use_limit = 500000;
745             systimer_init_periodic(&ill_timer, ithread_livelock_wakeup,
746                                    (void *)intr, use_limit);
747             lcount = 0;
748             /* fall through */
749         case ISTATE_LIVELOCKED:
750             /*
751              * Wait for our periodic timer to go off.  Since the interrupt
752              * has re-armed it can still set i_running, but it will not
753              * reschedule us while we are in a livelocked state.
754              */
755             lwkt_deschedule_self(gd->gd_curthread);
756             lwkt_switch();
757
758             /*
759              * Check to see if the livelock condition no longer applies.
760              * The interrupt must be able to operate normally for one
761              * full second before we restore normal operation.
762              */
763             if (lticks != ticks) {
764                 lticks = ticks;
765                 if (ill_count < livelock_lowater) {
766                     if (++lcount >= hz) {
767                         info->i_state = ISTATE_NORMAL;
768                         systimer_del(&ill_timer);
769                         printf("intr %d at %d < %d hz, livelock removed\n",
770                                intr, ill_count, livelock_lowater);
771                     }
772                 } else {
773                     lcount = 0;
774                 }
775                 ill_count -= ill_count / hz;
776             }
777             break;
778         }
779     }
780     /* not reached */
781 }
782
783 /*
784  * Emergency interrupt polling thread.  The thread begins execution
785  * outside a critical section with the BGL held.
786  *
787  * If emergency interrupt polling is enabled, this thread will 
788  * execute all system interrupts not marked INTR_NOPOLL at the
789  * specified polling frequency.
790  *
791  * WARNING!  This thread runs *ALL* interrupt service routines that
792  * are not marked INTR_NOPOLL, which basically means everything except
793  * the 8254 clock interrupt and the ATA interrupt.  It has very high
794  * overhead and should only be used in situations where the machine
795  * cannot otherwise be made to work.  Due to the severe performance
796  * degredation, it should not be enabled on production machines.
797  */
798 static void
799 ithread_emergency(void *arg __unused)
800 {
801     struct intr_info *info;
802     intrec_t rec, nrec;
803     int intr;
804
805     for (;;) {
806         for (intr = 0; intr < max_installed_hard_intr; ++intr) {
807             info = &intr_info_ary[intr];
808             for (rec = info->i_reclist; rec; rec = nrec) {
809                 if ((rec->intr_flags & INTR_NOPOLL) == 0) {
810                     if (rec->serializer) {
811                         lwkt_serialize_handler_call(rec->serializer,
812                                                 rec->handler, rec->argument, NULL);
813                     } else {
814                         rec->handler(rec->argument, NULL);
815                     }
816                 }
817                 nrec = rec->next;
818             }
819         }
820         lwkt_deschedule_self(curthread);
821         lwkt_switch();
822     }
823 }
824
825 /*
826  * Systimer callback - schedule the emergency interrupt poll thread
827  *                     if emergency polling is enabled.
828  */
829 static
830 void
831 emergency_intr_timer_callback(systimer_t info, struct intrframe *frame __unused)
832 {
833     if (emergency_intr_enable)
834         lwkt_schedule(info->data);
835 }
836
837 /* 
838  * Sysctls used by systat and others: hw.intrnames and hw.intrcnt.
839  * The data for this machine dependent, and the declarations are in machine
840  * dependent code.  The layout of intrnames and intrcnt however is machine
841  * independent.
842  *
843  * We do not know the length of intrcnt and intrnames at compile time, so
844  * calculate things at run time.
845  */
846
847 static int
848 sysctl_intrnames(SYSCTL_HANDLER_ARGS)
849 {
850     struct intr_info *info;
851     intrec_t rec;
852     int error = 0;
853     int len;
854     int intr;
855     char buf[64];
856
857     for (intr = 0; error == 0 && intr < MAX_INTS; ++intr) {
858         info = &intr_info_ary[intr];
859
860         len = 0;
861         buf[0] = 0;
862         for (rec = info->i_reclist; rec; rec = rec->next) {
863             snprintf(buf + len, sizeof(buf) - len, "%s%s", 
864                 (len ? "/" : ""), rec->name);
865             len += strlen(buf + len);
866         }
867         if (len == 0) {
868             snprintf(buf, sizeof(buf), "irq%d", intr);
869             len = strlen(buf);
870         }
871         error = SYSCTL_OUT(req, buf, len + 1);
872     }
873     return (error);
874 }
875
876
877 SYSCTL_PROC(_hw, OID_AUTO, intrnames, CTLTYPE_OPAQUE | CTLFLAG_RD,
878         NULL, 0, sysctl_intrnames, "", "Interrupt Names");
879
880 static int
881 sysctl_intrcnt(SYSCTL_HANDLER_ARGS)
882 {
883     struct intr_info *info;
884     int error = 0;
885     int intr;
886
887     for (intr = 0; intr < max_installed_hard_intr; ++intr) {
888         info = &intr_info_ary[intr];
889
890         error = SYSCTL_OUT(req, &info->i_count, sizeof(info->i_count));
891         if (error)
892                 goto failed;
893     }
894     for (intr = FIRST_SOFTINT; intr < max_installed_soft_intr; ++intr) {
895         info = &intr_info_ary[intr];
896
897         error = SYSCTL_OUT(req, &info->i_count, sizeof(info->i_count));
898         if (error)
899                 goto failed;
900     }
901 failed:
902     return(error);
903 }
904
905 SYSCTL_PROC(_hw, OID_AUTO, intrcnt, CTLTYPE_OPAQUE | CTLFLAG_RD,
906         NULL, 0, sysctl_intrcnt, "", "Interrupt Counts");
907