2 * Copyright (c) 2003 Matthew Dillon <dillon@backplane.com> All rights reserved.
3 * Copyright (c) 1997, Stefan Esser <se@freebsd.org> All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
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.
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.
26 * $FreeBSD: src/sys/kern/kern_intr.c,v 1.24.2.1 2001/10/14 20:05:50 luigi Exp $
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/malloc.h>
33 #include <sys/kernel.h>
34 #include <sys/sysctl.h>
35 #include <sys/thread.h>
37 #include <sys/random.h>
38 #include <sys/serialize.h>
39 #include <sys/interrupt.h>
41 #include <sys/machintr.h>
43 #include <machine/frame.h>
45 #include <sys/thread2.h>
46 #include <sys/mplock2.h>
50 typedef struct intrec {
52 struct intr_info *info;
58 struct lwkt_serialize *serializer;
63 struct thread *i_thread; /* don't embed struct thread */
64 struct random_softc i_random;
65 long i_count; /* interrupts dispatched */
67 int i_mplock_required;
72 unsigned long i_straycount;
77 struct intr_info_block {
78 struct intr_info ary[MAXCPU][MAX_INTS];
81 static struct intr_info_block *intr_block;
82 static struct intr_info *swi_info_ary[MAX_SOFTINTS];
84 static int max_installed_hard_intr[MAXCPU];
86 MALLOC_DEFINE(M_INTRMNG, "intrmng", "interrupt management");
89 #define EMERGENCY_INTR_POLLING_FREQ_MAX 20000
92 * Assert that callers into interrupt handlers don't return with
93 * dangling tokens, spinlocks, or mp locks.
97 #define TD_INVARIANTS_DECLARE \
101 #define TD_INVARIANTS_GET(td) \
103 spincount = (td)->td_gd->gd_spinlocks; \
104 curstop = (td)->td_toks_stop; \
107 #define TD_INVARIANTS_TEST(td, name) \
109 KASSERT(spincount == (td)->td_gd->gd_spinlocks, \
110 ("spincount mismatch after interrupt handler %s", \
112 KASSERT(curstop == (td)->td_toks_stop, \
113 ("token count mismatch after interrupt handler %s", \
121 #define TD_INVARIANTS_DECLARE
122 #define TD_INVARIANTS_GET(td)
123 #define TD_INVARIANTS_TEST(td, name)
125 #endif /* ndef INVARIANTS */
127 static int sysctl_emergency_freq(SYSCTL_HANDLER_ARGS);
128 static int sysctl_emergency_enable(SYSCTL_HANDLER_ARGS);
129 static void emergency_intr_timer_callback(systimer_t, int, struct intrframe *);
130 static void ithread_handler(void *arg);
131 static void ithread_emergency(void *arg);
132 static void report_stray_interrupt(struct intr_info *info, const char *func);
133 static void int_moveto_destcpu(int *, int);
134 static void int_moveto_origcpu(int, int);
135 static void sched_ithd_intern(struct intr_info *info);
137 static struct systimer emergency_intr_timer[MAXCPU];
138 static struct thread *emergency_intr_thread[MAXCPU];
140 #define ISTATE_NOTHREAD 0
141 #define ISTATE_NORMAL 1
142 #define ISTATE_LIVELOCKED 2
144 static int livelock_limit = 40000;
145 static int livelock_lowater = 20000;
146 static int livelock_debug = -1;
147 SYSCTL_INT(_kern, OID_AUTO, livelock_limit,
148 CTLFLAG_RW, &livelock_limit, 0, "Livelock interrupt rate limit");
149 SYSCTL_INT(_kern, OID_AUTO, livelock_lowater,
150 CTLFLAG_RW, &livelock_lowater, 0, "Livelock low-water mark restore");
151 SYSCTL_INT(_kern, OID_AUTO, livelock_debug,
152 CTLFLAG_RW, &livelock_debug, 0, "Livelock debug intr#");
154 static int emergency_intr_enable = 0; /* emergency interrupt polling */
155 TUNABLE_INT("kern.emergency_intr_enable", &emergency_intr_enable);
156 SYSCTL_PROC(_kern, OID_AUTO, emergency_intr_enable, CTLTYPE_INT | CTLFLAG_RW,
157 0, 0, sysctl_emergency_enable, "I", "Emergency Interrupt Poll Enable");
159 static int emergency_intr_freq = 10; /* emergency polling frequency */
160 TUNABLE_INT("kern.emergency_intr_freq", &emergency_intr_freq);
161 SYSCTL_PROC(_kern, OID_AUTO, emergency_intr_freq, CTLTYPE_INT | CTLFLAG_RW,
162 0, 0, sysctl_emergency_freq, "I", "Emergency Interrupt Poll Frequency");
165 * Sysctl support routines
168 sysctl_emergency_enable(SYSCTL_HANDLER_ARGS)
170 int error, enabled, cpuid, freq;
172 enabled = emergency_intr_enable;
173 error = sysctl_handle_int(oidp, &enabled, 0, req);
174 if (error || req->newptr == NULL)
176 emergency_intr_enable = enabled;
177 if (emergency_intr_enable)
178 freq = emergency_intr_freq;
182 for (cpuid = 0; cpuid < ncpus; ++cpuid)
183 systimer_adjust_periodic(&emergency_intr_timer[cpuid], freq);
188 sysctl_emergency_freq(SYSCTL_HANDLER_ARGS)
190 int error, phz, cpuid, freq;
192 phz = emergency_intr_freq;
193 error = sysctl_handle_int(oidp, &phz, 0, req);
194 if (error || req->newptr == NULL)
198 else if (phz > EMERGENCY_INTR_POLLING_FREQ_MAX)
199 phz = EMERGENCY_INTR_POLLING_FREQ_MAX;
201 emergency_intr_freq = phz;
202 if (emergency_intr_enable)
203 freq = emergency_intr_freq;
207 for (cpuid = 0; cpuid < ncpus; ++cpuid)
208 systimer_adjust_periodic(&emergency_intr_timer[cpuid], freq);
213 * Register an SWI or INTerrupt handler.
216 register_swi(int intr, inthand2_t *handler, void *arg, const char *name,
217 struct lwkt_serialize *serializer, int cpuid)
219 if (intr < FIRST_SOFTINT || intr >= MAX_INTS)
220 panic("register_swi: bad intr %d", intr);
223 cpuid = intr % ncpus;
224 return(register_int(intr, handler, arg, name, serializer, 0, cpuid));
228 register_swi_mp(int intr, inthand2_t *handler, void *arg, const char *name,
229 struct lwkt_serialize *serializer, int cpuid)
231 if (intr < FIRST_SOFTINT || intr >= MAX_INTS)
232 panic("register_swi: bad intr %d", intr);
235 cpuid = intr % ncpus;
236 return(register_int(intr, handler, arg, name, serializer,
237 INTR_MPSAFE, cpuid));
241 register_int(int intr, inthand2_t *handler, void *arg, const char *name,
242 struct lwkt_serialize *serializer, int intr_flags, int cpuid)
244 struct intr_info *info;
245 struct intrec **list;
249 KKASSERT(cpuid >= 0 && cpuid < ncpus);
251 if (intr < 0 || intr >= MAX_INTS)
252 panic("register_int: bad intr %d", intr);
255 info = &intr_block->ary[cpuid][intr];
258 * Construct an interrupt handler record
260 rec = kmalloc(sizeof(struct intrec), M_DEVBUF, M_INTWAIT);
261 rec->name = kmalloc(strlen(name) + 1, M_DEVBUF, M_INTWAIT);
262 strcpy(rec->name, name);
265 rec->handler = handler;
268 rec->intr_flags = intr_flags;
270 rec->serializer = serializer;
272 int_moveto_destcpu(&orig_cpuid, cpuid);
275 * Create an emergency polling thread and set up a systimer to wake
276 * it up. objcache isn't operational yet so use kmalloc.
278 * objcache may not be operational yet, use kmalloc().
280 if (emergency_intr_thread[cpuid] == NULL) {
281 emergency_intr_thread[cpuid] = kmalloc(sizeof(struct thread), M_DEVBUF,
283 lwkt_create(ithread_emergency, NULL, NULL,
284 emergency_intr_thread[cpuid],
285 TDF_NOSTART | TDF_INTTHREAD, cpuid, "ithreadE %d",
287 systimer_init_periodic_nq(&emergency_intr_timer[cpuid],
288 emergency_intr_timer_callback,
289 emergency_intr_thread[cpuid],
290 (emergency_intr_enable ? emergency_intr_freq : 1));
294 * Create an interrupt thread if necessary, leave it in an unscheduled
297 if (info->i_state == ISTATE_NOTHREAD) {
298 info->i_state = ISTATE_NORMAL;
299 info->i_thread = kmalloc(sizeof(struct thread), M_DEVBUF,
301 lwkt_create(ithread_handler, (void *)(intptr_t)intr, NULL,
302 info->i_thread, TDF_NOSTART | TDF_INTTHREAD, cpuid,
303 "ithread%d %d", intr, cpuid);
304 if (intr >= FIRST_SOFTINT)
305 lwkt_setpri(info->i_thread, TDPRI_SOFT_NORM);
307 lwkt_setpri(info->i_thread, TDPRI_INT_MED);
308 info->i_thread->td_preemptable = lwkt_preempt;
311 list = &info->i_reclist;
314 * Keep track of how many fast and slow interrupts we have.
315 * Set i_mplock_required if any handler in the chain requires
316 * the MP lock to operate.
318 if ((intr_flags & INTR_MPSAFE) == 0)
319 info->i_mplock_required = 1;
320 if (intr_flags & INTR_CLOCK)
326 * Enable random number generation keying off of this interrupt.
328 if ((intr_flags & INTR_NOENTROPY) == 0 && info->i_random.sc_enabled == 0) {
329 info->i_random.sc_enabled = 1;
330 info->i_random.sc_intr = intr;
334 * Add the record to the interrupt list.
337 while (*list != NULL)
338 list = &(*list)->next;
343 * Update max_installed_hard_intr to make the emergency intr poll
344 * a bit more efficient.
346 if (intr < FIRST_SOFTINT) {
347 if (max_installed_hard_intr[cpuid] <= intr)
348 max_installed_hard_intr[cpuid] = intr + 1;
351 if (intr >= FIRST_SOFTINT)
352 swi_info_ary[intr - FIRST_SOFTINT] = info;
355 * Setup the machine level interrupt vector
357 if (intr < FIRST_SOFTINT && info->i_slow + info->i_fast == 1)
358 machintr_intr_setup(intr, intr_flags);
360 int_moveto_origcpu(orig_cpuid, cpuid);
366 unregister_swi(void *id, int intr, int cpuid)
369 cpuid = intr % ncpus;
371 unregister_int(id, cpuid);
375 unregister_int(void *id, int cpuid)
377 struct intr_info *info;
378 struct intrec **list;
380 int intr, orig_cpuid;
382 KKASSERT(cpuid >= 0 && cpuid < ncpus);
384 intr = ((intrec_t)id)->intr;
386 if (intr < 0 || intr >= MAX_INTS)
387 panic("register_int: bad intr %d", intr);
389 info = &intr_block->ary[cpuid][intr];
391 int_moveto_destcpu(&orig_cpuid, cpuid);
394 * Remove the interrupt descriptor, adjust the descriptor count,
395 * and teardown the machine level vector if this was the last interrupt.
398 list = &info->i_reclist;
399 while ((rec = *list) != NULL) {
408 if (rec->intr_flags & INTR_CLOCK)
412 if (intr < FIRST_SOFTINT && info->i_fast + info->i_slow == 0)
413 machintr_intr_teardown(intr);
416 * Clear i_mplock_required if no handlers in the chain require the
419 for (rec0 = info->i_reclist; rec0; rec0 = rec0->next) {
420 if ((rec0->intr_flags & INTR_MPSAFE) == 0)
424 info->i_mplock_required = 0;
427 if (intr >= FIRST_SOFTINT && info->i_reclist == NULL)
428 swi_info_ary[intr - FIRST_SOFTINT] = NULL;
432 int_moveto_origcpu(orig_cpuid, cpuid);
438 kfree(rec->name, M_DEVBUF);
439 kfree(rec, M_DEVBUF);
441 kprintf("warning: unregister_int: int %d handler for %s not found\n",
442 intr, ((intrec_t)id)->name);
447 get_interrupt_counter(int intr, int cpuid)
449 struct intr_info *info;
451 KKASSERT(cpuid >= 0 && cpuid < ncpus);
453 if (intr < 0 || intr >= MAX_INTS)
454 panic("register_int: bad intr %d", intr);
455 info = &intr_block->ary[cpuid][intr];
456 return(info->i_count);
460 register_randintr(int intr)
462 struct intr_info *info;
465 if (intr < 0 || intr >= MAX_INTS)
466 panic("register_randintr: bad intr %d", intr);
468 for (cpuid = 0; cpuid < ncpus; ++cpuid) {
469 info = &intr_block->ary[cpuid][intr];
470 info->i_random.sc_intr = intr;
471 info->i_random.sc_enabled = 1;
476 unregister_randintr(int intr)
478 struct intr_info *info;
481 if (intr < 0 || intr >= MAX_INTS)
482 panic("register_swi: bad intr %d", intr);
484 for (cpuid = 0; cpuid < ncpus; ++cpuid) {
485 info = &intr_block->ary[cpuid][intr];
486 info->i_random.sc_enabled = -1;
491 next_registered_randintr(int intr)
493 struct intr_info *info;
495 if (intr < 0 || intr >= MAX_INTS)
496 panic("register_swi: bad intr %d", intr);
498 while (intr < MAX_INTS) {
501 for (cpuid = 0; cpuid < ncpus; ++cpuid) {
502 info = &intr_block->ary[cpuid][intr];
503 if (info->i_random.sc_enabled > 0)
512 * Dispatch an interrupt. If there's nothing to do we have a stray
513 * interrupt and can just return, leaving the interrupt masked.
515 * We need to schedule the interrupt and set its i_running bit. If
516 * we are not on the interrupt thread's cpu we have to send a message
517 * to the correct cpu that will issue the desired action (interlocking
518 * with the interrupt thread's critical section). We do NOT attempt to
519 * reschedule interrupts whos i_running bit is already set because
520 * this would prematurely wakeup a livelock-limited interrupt thread.
522 * i_running is only tested/set on the same cpu as the interrupt thread.
524 * We are NOT in a critical section, which will allow the scheduled
525 * interrupt to preempt us. The MP lock might *NOT* be held here.
528 sched_ithd_remote(void *arg)
530 sched_ithd_intern(arg);
534 sched_ithd_intern(struct intr_info *info)
537 if (info->i_state != ISTATE_NOTHREAD) {
538 if (info->i_reclist == NULL) {
539 report_stray_interrupt(info, "sched_ithd");
541 if (info->i_thread->td_gd == mycpu) {
542 if (info->i_running == 0) {
544 if (info->i_state != ISTATE_LIVELOCKED)
545 lwkt_schedule(info->i_thread); /* MIGHT PREEMPT */
548 lwkt_send_ipiq(info->i_thread->td_gd, sched_ithd_remote, info);
552 report_stray_interrupt(info, "sched_ithd");
557 sched_ithd_soft(int intr)
559 struct intr_info *info;
561 KKASSERT(intr >= FIRST_SOFTINT && intr < MAX_INTS);
563 info = swi_info_ary[intr - FIRST_SOFTINT];
565 sched_ithd_intern(info);
567 kprintf("unregistered softint %d got scheduled on cpu%d\n",
573 sched_ithd_hard(int intr)
575 KKASSERT(intr >= 0 && intr < MAX_HARDINTS);
576 sched_ithd_intern(&intr_block->ary[mycpuid][intr]);
579 #ifdef _KERNEL_VIRTUAL
582 sched_ithd_hard_virtual(int intr)
584 KKASSERT(intr >= 0 && intr < MAX_HARDINTS);
585 sched_ithd_intern(&intr_block->ary[0][intr]);
589 register_int_virtual(int intr, inthand2_t *handler, void *arg, const char *name,
590 struct lwkt_serialize *serializer, int intr_flags)
592 return register_int(intr, handler, arg, name, serializer, intr_flags, 0);
596 unregister_int_virtual(void *id)
598 unregister_int(id, 0);
601 #endif /* _KERN_VIRTUAL */
604 report_stray_interrupt(struct intr_info *info, const char *func)
606 ++info->i_straycount;
607 if (info->i_straycount < 10) {
608 if (info->i_errorticks == ticks)
610 info->i_errorticks = ticks;
611 kprintf("%s: stray interrupt %d on cpu%d\n",
612 func, info->i_intr, mycpuid);
613 } else if (info->i_straycount == 10) {
614 kprintf("%s: %ld stray interrupts %d on cpu%d - "
615 "there will be no further reports\n", func,
616 info->i_straycount, info->i_intr, mycpuid);
621 * This is run from a periodic SYSTIMER (and thus must be MP safe, the BGL
622 * might not be held).
625 ithread_livelock_wakeup(systimer_t st, int in_ipi __unused,
626 struct intrframe *frame __unused)
628 struct intr_info *info;
630 info = &intr_block->ary[mycpuid][(int)(intptr_t)st->data];
631 if (info->i_state != ISTATE_NOTHREAD)
632 lwkt_schedule(info->i_thread);
636 * Schedule ithread within fast intr handler
638 * XXX Protect sched_ithd_hard() call with gd_intr_nesting_level?
639 * Interrupts aren't enabled, but still...
642 ithread_fast_sched(int intr, thread_t td)
647 * We are already in critical section, exit it now to
651 sched_ithd_hard(intr);
652 crit_enter_quick(td);
658 * This function is called directly from the ICU or APIC vector code assembly
659 * to process an interrupt. The critical section and interrupt deferral
660 * checks have already been done but the function is entered WITHOUT
661 * a critical section held. The BGL may or may not be held.
663 * Must return non-zero if we do not want the vector code to re-enable
664 * the interrupt (which we don't if we have to schedule the interrupt)
666 int ithread_fast_handler(struct intrframe *frame);
669 ithread_fast_handler(struct intrframe *frame)
672 struct intr_info *info;
673 struct intrec **list;
676 TD_INVARIANTS_DECLARE;
681 intr = frame->if_vec;
685 /* We must be in critical section. */
686 KKASSERT(td->td_critcount);
688 info = &intr_block->ary[mycpuid][intr];
691 * If we are not processing any FAST interrupts, just schedule the thing.
693 if (info->i_fast == 0) {
695 ithread_fast_sched(intr, td);
700 * This should not normally occur since interrupts ought to be
701 * masked if the ithread has been scheduled or is running.
707 * Bump the interrupt nesting level to process any FAST interrupts.
708 * Obtain the MP lock as necessary. If the MP lock cannot be obtained,
709 * schedule the interrupt thread to deal with the issue instead.
711 * To reduce overhead, just leave the MP lock held once it has been
714 ++gd->gd_intr_nesting_level;
716 must_schedule = info->i_slow;
719 TD_INVARIANTS_GET(td);
720 list = &info->i_reclist;
722 for (rec = *list; rec; rec = nrec) {
723 /* rec may be invalid after call */
726 if (rec->intr_flags & INTR_CLOCK) {
727 if ((rec->intr_flags & INTR_MPSAFE) == 0 && got_mplock == 0) {
728 if (try_mplock() == 0) {
729 /* Couldn't get the MP lock; just schedule it. */
735 if (rec->serializer) {
736 must_schedule += lwkt_serialize_handler_try(
737 rec->serializer, rec->handler,
738 rec->argument, frame);
740 rec->handler(rec->argument, frame);
742 TD_INVARIANTS_TEST(td, rec->name);
749 --gd->gd_intr_nesting_level;
754 * If we had a problem, or mixed fast and slow interrupt handlers are
755 * registered, schedule the ithread to catch the missed records (it
756 * will just re-run all of them). A return value of 0 indicates that
757 * all handlers have been run and the interrupt can be re-enabled, and
758 * a non-zero return indicates that the interrupt thread controls
761 if (must_schedule > 0)
762 ithread_fast_sched(intr, td);
763 else if (must_schedule == 0)
765 return(must_schedule);
769 * Interrupt threads run this as their main loop.
771 * The handler begins execution outside a critical section and no MP lock.
773 * The i_running state starts at 0. When an interrupt occurs, the hardware
774 * interrupt is disabled and sched_ithd_hard(). The HW interrupt remains
775 * disabled until all routines have run. We then call machintr_intr_enable()
776 * to reenable the HW interrupt and deschedule us until the next interrupt.
778 * We are responsible for atomically checking i_running. i_running for our
779 * irq is only set in the context of our cpu, so a critical section is a
780 * sufficient interlock.
782 #define LIVELOCK_TIMEFRAME(freq) ((freq) >> 2) /* 1/4 second */
785 ithread_handler(void *arg)
787 struct intr_info *info;
790 int intr, cpuid = mycpuid;
792 struct intrec **list;
795 struct systimer ill_timer; /* enforced freq. timer */
796 u_int ill_count; /* interrupt livelock counter */
797 TD_INVARIANTS_DECLARE;
800 intr = (int)(intptr_t)arg;
801 info = &intr_block->ary[cpuid][intr];
802 list = &info->i_reclist;
805 * The loop must be entered with one critical section held. The thread
806 * does not hold the mplock on startup.
809 lseconds = gd->gd_time_seconds;
815 * The chain is only considered MPSAFE if all its interrupt handlers
816 * are MPSAFE. However, if intr_mpsafe has been turned off we
817 * always operate with the BGL.
819 if (info->i_mplock_required != mpheld) {
820 if (info->i_mplock_required) {
821 KKASSERT(mpheld == 0);
825 KKASSERT(mpheld != 0);
831 TD_INVARIANTS_GET(gd->gd_curthread);
834 * If an interrupt is pending, clear i_running and execute the
835 * handlers. Note that certain types of interrupts can re-trigger
836 * and set i_running again.
838 * Each handler is run in a critical section. Note that we run both
839 * FAST and SLOW designated service routines.
841 if (info->i_running) {
846 report_stray_interrupt(info, "ithread_handler");
848 for (rec = *list; rec; rec = nrec) {
849 /* rec may be invalid after call */
851 if (rec->serializer) {
852 lwkt_serialize_handler_call(rec->serializer, rec->handler,
853 rec->argument, NULL);
855 rec->handler(rec->argument, NULL);
857 TD_INVARIANTS_TEST(gd->gd_curthread, rec->name);
862 * This is our interrupt hook to add rate randomness to the random
865 if (info->i_random.sc_enabled > 0)
866 add_interrupt_randomness(intr);
869 * Unmask the interrupt to allow it to trigger again. This only
870 * applies to certain types of interrupts (typ level interrupts).
871 * This can result in the interrupt retriggering, but the retrigger
872 * will not be processed until we cycle our critical section.
874 * Only unmask interrupts while handlers are installed. It is
875 * possible to hit a situation where no handlers are installed
876 * due to a device driver livelocking and then tearing down its
877 * interrupt on close (the parallel bus being a good example).
879 if (intr < FIRST_SOFTINT && *list)
880 machintr_intr_enable(intr);
883 * Do a quick exit/enter to catch any higher-priority interrupt
884 * sources, such as the statclock, so thread time accounting
885 * will still work. This may also cause an interrupt to re-trigger.
891 * LIVELOCK STATE MACHINE
893 switch(info->i_state) {
896 * Reset the count each second.
898 if (lseconds != gd->gd_time_seconds) {
899 lseconds = gd->gd_time_seconds;
904 * If we did not exceed the frequency limit, we are done.
905 * If the interrupt has not retriggered we deschedule ourselves.
907 if (ill_count <= livelock_limit) {
908 if (info->i_running == 0) {
909 lwkt_deschedule_self(gd->gd_curthread);
916 * Otherwise we are livelocked. Set up a periodic systimer
917 * to wake the thread up at the limit frequency.
919 kprintf("intr %d on cpu%d at %d/%d hz, livelocked limit engaged!\n",
920 intr, cpuid, ill_count, livelock_limit);
921 info->i_state = ISTATE_LIVELOCKED;
922 if ((use_limit = livelock_limit) < 100)
924 else if (use_limit > 500000)
926 systimer_init_periodic_nq(&ill_timer, ithread_livelock_wakeup,
927 (void *)(intptr_t)intr, use_limit);
929 case ISTATE_LIVELOCKED:
931 * Wait for our periodic timer to go off. Since the interrupt
932 * has re-armed it can still set i_running, but it will not
933 * reschedule us while we are in a livelocked state.
935 lwkt_deschedule_self(gd->gd_curthread);
939 * Check once a second to see if the livelock condition no
942 if (lseconds != gd->gd_time_seconds) {
943 lseconds = gd->gd_time_seconds;
944 if (ill_count < livelock_lowater) {
945 info->i_state = ISTATE_NORMAL;
946 systimer_del(&ill_timer);
947 kprintf("intr %d on cpu%d at %d/%d hz, livelock removed\n",
948 intr, cpuid, ill_count, livelock_lowater);
949 } else if (livelock_debug == intr ||
950 (bootverbose && cold)) {
951 kprintf("intr %d on cpu%d at %d/%d hz, in livelock\n",
952 intr, cpuid, ill_count, livelock_lowater);
963 * Emergency interrupt polling thread. The thread begins execution
964 * outside a critical section with the BGL held.
966 * If emergency interrupt polling is enabled, this thread will
967 * execute all system interrupts not marked INTR_NOPOLL at the
968 * specified polling frequency.
970 * WARNING! This thread runs *ALL* interrupt service routines that
971 * are not marked INTR_NOPOLL, which basically means everything except
972 * the 8254 clock interrupt and the ATA interrupt. It has very high
973 * overhead and should only be used in situations where the machine
974 * cannot otherwise be made to work. Due to the severe performance
975 * degredation, it should not be enabled on production machines.
978 ithread_emergency(void *arg __unused)
980 globaldata_t gd = mycpu;
981 struct intr_info *info;
983 int intr, cpuid = mycpuid;
984 TD_INVARIANTS_DECLARE;
988 TD_INVARIANTS_GET(gd->gd_curthread);
991 for (intr = 0; intr < max_installed_hard_intr[cpuid]; ++intr) {
992 info = &intr_block->ary[cpuid][intr];
993 for (rec = info->i_reclist; rec; rec = nrec) {
994 /* rec may be invalid after call */
996 if ((rec->intr_flags & INTR_NOPOLL) == 0) {
997 if (rec->serializer) {
998 lwkt_serialize_handler_try(rec->serializer,
999 rec->handler, rec->argument, NULL);
1001 rec->handler(rec->argument, NULL);
1003 TD_INVARIANTS_TEST(gd->gd_curthread, rec->name);
1007 lwkt_deschedule_self(gd->gd_curthread);
1014 * Systimer callback - schedule the emergency interrupt poll thread
1015 * if emergency polling is enabled.
1019 emergency_intr_timer_callback(systimer_t info, int in_ipi __unused,
1020 struct intrframe *frame __unused)
1022 if (emergency_intr_enable)
1023 lwkt_schedule(info->data);
1027 * Sysctls used by systat and others: hw.intrnames and hw.intrcnt.
1028 * The data for this machine dependent, and the declarations are in machine
1029 * dependent code. The layout of intrnames and intrcnt however is machine
1032 * We do not know the length of intrcnt and intrnames at compile time, so
1033 * calculate things at run time.
1037 sysctl_intrnames(SYSCTL_HANDLER_ARGS)
1039 struct intr_info *info;
1046 for (cpuid = 0; cpuid < ncpus; ++cpuid) {
1047 for (intr = 0; error == 0 && intr < MAX_INTS; ++intr) {
1048 info = &intr_block->ary[cpuid][intr];
1052 for (rec = info->i_reclist; rec; rec = rec->next) {
1053 ksnprintf(buf + len, sizeof(buf) - len, "%s%s",
1054 (len ? "/" : ""), rec->name);
1055 len += strlen(buf + len);
1058 ksnprintf(buf, sizeof(buf), "irq%d", intr);
1061 error = SYSCTL_OUT(req, buf, len + 1);
1067 SYSCTL_PROC(_hw, OID_AUTO, intrnames, CTLTYPE_OPAQUE | CTLFLAG_RD,
1068 NULL, 0, sysctl_intrnames, "", "Interrupt Names");
1071 sysctl_intrcnt_all(SYSCTL_HANDLER_ARGS)
1073 struct intr_info *info;
1077 for (cpuid = 0; cpuid < ncpus; ++cpuid) {
1078 for (intr = 0; intr < MAX_INTS; ++intr) {
1079 info = &intr_block->ary[cpuid][intr];
1081 error = SYSCTL_OUT(req, &info->i_count, sizeof(info->i_count));
1090 SYSCTL_PROC(_hw, OID_AUTO, intrcnt_all, CTLTYPE_OPAQUE | CTLFLAG_RD,
1091 NULL, 0, sysctl_intrcnt_all, "", "Interrupt Counts");
1093 SYSCTL_PROC(_hw, OID_AUTO, intrcnt, CTLTYPE_OPAQUE | CTLFLAG_RD,
1094 NULL, 0, sysctl_intrcnt_all, "", "Interrupt Counts");
1097 int_moveto_destcpu(int *orig_cpuid0, int cpuid)
1099 int orig_cpuid = mycpuid;
1101 if (cpuid != orig_cpuid)
1102 lwkt_migratecpu(cpuid);
1104 *orig_cpuid0 = orig_cpuid;
1108 int_moveto_origcpu(int orig_cpuid, int cpuid)
1110 if (cpuid != orig_cpuid)
1111 lwkt_migratecpu(orig_cpuid);
1115 intr_init(void *dummy __unused)
1119 kprintf("Initialize MI interrupts\n");
1121 intr_block = kmalloc(sizeof(*intr_block), M_INTRMNG,
1122 M_INTWAIT | M_ZERO);
1124 for (cpuid = 0; cpuid < ncpus; ++cpuid) {
1127 for (intr = 0; intr < MAX_INTS; ++intr) {
1128 struct intr_info *info = &intr_block->ary[cpuid][intr];
1130 info->i_cpuid = cpuid;
1131 info->i_intr = intr;
1135 SYSINIT(intr_init, SI_BOOT2_FINISH_PIC, SI_ORDER_ANY, intr_init, NULL);