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;
64 struct random_softc i_random;
66 long i_count; /* interrupts dispatched */
67 int i_mplock_required;
72 unsigned long i_straycount;
73 } intr_info_ary[MAX_INTS];
75 int max_installed_hard_intr;
76 int max_installed_soft_intr;
78 #define EMERGENCY_INTR_POLLING_FREQ_MAX 20000
81 * Assert that callers into interrupt handlers don't return with
82 * dangling tokens, spinlocks, or mp locks.
86 #define TD_INVARIANTS_DECLARE \
90 #define TD_INVARIANTS_GET(td) \
92 spincount = (td)->td_gd->gd_spinlocks_wr; \
93 curstop = (td)->td_toks_stop; \
96 #define TD_INVARIANTS_TEST(td, name) \
98 KASSERT(spincount == (td)->td_gd->gd_spinlocks_wr, \
99 ("spincount mismatch after interrupt handler %s", \
101 KASSERT(curstop == (td)->td_toks_stop, \
102 ("token count mismatch after interrupt handler %s", \
110 #define TD_INVARIANTS_DECLARE
111 #define TD_INVARIANTS_GET(td)
112 #define TD_INVARIANTS_TEST(td, name)
114 #endif /* ndef INVARIANTS */
116 static int sysctl_emergency_freq(SYSCTL_HANDLER_ARGS);
117 static int sysctl_emergency_enable(SYSCTL_HANDLER_ARGS);
118 static void emergency_intr_timer_callback(systimer_t, int, struct intrframe *);
119 static void ithread_handler(void *arg);
120 static void ithread_emergency(void *arg);
121 static void report_stray_interrupt(int intr, struct intr_info *info);
122 static void int_moveto_destcpu(int *, int);
123 static void int_moveto_origcpu(int, int);
125 int intr_info_size = NELEM(intr_info_ary);
127 static struct systimer emergency_intr_timer;
128 static struct thread emergency_intr_thread;
130 #define ISTATE_NOTHREAD 0
131 #define ISTATE_NORMAL 1
132 #define ISTATE_LIVELOCKED 2
134 static int livelock_limit = 40000;
135 static int livelock_lowater = 20000;
136 static int livelock_debug = -1;
137 SYSCTL_INT(_kern, OID_AUTO, livelock_limit,
138 CTLFLAG_RW, &livelock_limit, 0, "Livelock interrupt rate limit");
139 SYSCTL_INT(_kern, OID_AUTO, livelock_lowater,
140 CTLFLAG_RW, &livelock_lowater, 0, "Livelock low-water mark restore");
141 SYSCTL_INT(_kern, OID_AUTO, livelock_debug,
142 CTLFLAG_RW, &livelock_debug, 0, "Livelock debug intr#");
144 static int emergency_intr_enable = 0; /* emergency interrupt polling */
145 TUNABLE_INT("kern.emergency_intr_enable", &emergency_intr_enable);
146 SYSCTL_PROC(_kern, OID_AUTO, emergency_intr_enable, CTLTYPE_INT | CTLFLAG_RW,
147 0, 0, sysctl_emergency_enable, "I", "Emergency Interrupt Poll Enable");
149 static int emergency_intr_freq = 10; /* emergency polling frequency */
150 TUNABLE_INT("kern.emergency_intr_freq", &emergency_intr_freq);
151 SYSCTL_PROC(_kern, OID_AUTO, emergency_intr_freq, CTLTYPE_INT | CTLFLAG_RW,
152 0, 0, sysctl_emergency_freq, "I", "Emergency Interrupt Poll Frequency");
155 * Sysctl support routines
158 sysctl_emergency_enable(SYSCTL_HANDLER_ARGS)
162 enabled = emergency_intr_enable;
163 error = sysctl_handle_int(oidp, &enabled, 0, req);
164 if (error || req->newptr == NULL)
166 emergency_intr_enable = enabled;
167 if (emergency_intr_enable) {
168 systimer_adjust_periodic(&emergency_intr_timer,
169 emergency_intr_freq);
171 systimer_adjust_periodic(&emergency_intr_timer, 1);
177 sysctl_emergency_freq(SYSCTL_HANDLER_ARGS)
181 phz = emergency_intr_freq;
182 error = sysctl_handle_int(oidp, &phz, 0, req);
183 if (error || req->newptr == NULL)
187 else if (phz > EMERGENCY_INTR_POLLING_FREQ_MAX)
188 phz = EMERGENCY_INTR_POLLING_FREQ_MAX;
190 emergency_intr_freq = phz;
191 if (emergency_intr_enable) {
192 systimer_adjust_periodic(&emergency_intr_timer,
193 emergency_intr_freq);
195 systimer_adjust_periodic(&emergency_intr_timer, 1);
201 * Register an SWI or INTerrupt handler.
204 register_swi(int intr, inthand2_t *handler, void *arg, const char *name,
205 struct lwkt_serialize *serializer, int cpuid)
207 if (intr < FIRST_SOFTINT || intr >= MAX_INTS)
208 panic("register_swi: bad intr %d", intr);
211 cpuid = intr % ncpus;
212 return(register_int(intr, handler, arg, name, serializer, 0, cpuid));
216 register_swi_mp(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,
225 INTR_MPSAFE, cpuid));
229 register_int(int intr, inthand2_t *handler, void *arg, const char *name,
230 struct lwkt_serialize *serializer, int intr_flags, int cpuid)
232 struct intr_info *info;
233 struct intrec **list;
237 KKASSERT(cpuid >= 0 && cpuid < ncpus);
239 if (intr < 0 || intr >= MAX_INTS)
240 panic("register_int: bad intr %d", intr);
243 info = &intr_info_ary[intr];
246 * Construct an interrupt handler record
248 rec = kmalloc(sizeof(struct intrec), M_DEVBUF, M_INTWAIT);
249 rec->name = kmalloc(strlen(name) + 1, M_DEVBUF, M_INTWAIT);
250 strcpy(rec->name, name);
253 rec->handler = handler;
256 rec->intr_flags = intr_flags;
258 rec->serializer = serializer;
261 * Create an emergency polling thread and set up a systimer to wake
264 if (emergency_intr_thread.td_kstack == NULL) {
265 lwkt_create(ithread_emergency, NULL, NULL, &emergency_intr_thread,
266 TDF_NOSTART | TDF_INTTHREAD, ncpus - 1, "ithread emerg");
267 systimer_init_periodic_nq(&emergency_intr_timer,
268 emergency_intr_timer_callback, &emergency_intr_thread,
269 (emergency_intr_enable ? emergency_intr_freq : 1));
272 int_moveto_destcpu(&orig_cpuid, cpuid);
275 * Create an interrupt thread if necessary, leave it in an unscheduled
278 if (info->i_state == ISTATE_NOTHREAD) {
279 info->i_state = ISTATE_NORMAL;
280 lwkt_create(ithread_handler, (void *)(intptr_t)intr, NULL,
281 &info->i_thread, TDF_NOSTART | TDF_INTTHREAD, cpuid,
283 if (intr >= FIRST_SOFTINT)
284 lwkt_setpri(&info->i_thread, TDPRI_SOFT_NORM);
286 lwkt_setpri(&info->i_thread, TDPRI_INT_MED);
287 info->i_thread.td_preemptable = lwkt_preempt;
290 list = &info->i_reclist;
293 * Keep track of how many fast and slow interrupts we have.
294 * Set i_mplock_required if any handler in the chain requires
295 * the MP lock to operate.
297 if ((intr_flags & INTR_MPSAFE) == 0)
298 info->i_mplock_required = 1;
299 if (intr_flags & INTR_CLOCK)
305 * Enable random number generation keying off of this interrupt.
307 if ((intr_flags & INTR_NOENTROPY) == 0 && info->i_random.sc_enabled == 0) {
308 info->i_random.sc_enabled = 1;
309 info->i_random.sc_intr = intr;
313 * Add the record to the interrupt list.
316 while (*list != NULL)
317 list = &(*list)->next;
322 * Update max_installed_hard_intr to make the emergency intr poll
323 * a bit more efficient.
325 if (intr < FIRST_SOFTINT) {
326 if (max_installed_hard_intr <= intr)
327 max_installed_hard_intr = intr + 1;
329 if (max_installed_soft_intr <= intr)
330 max_installed_soft_intr = intr + 1;
334 * Setup the machine level interrupt vector
336 if (intr < FIRST_SOFTINT && info->i_slow + info->i_fast == 1)
337 machintr_intr_setup(intr, intr_flags);
339 int_moveto_origcpu(orig_cpuid, cpuid);
345 unregister_swi(void *id, int intr, int cpuid)
348 cpuid = intr % ncpus;
350 unregister_int(id, cpuid);
354 unregister_int(void *id, int cpuid)
356 struct intr_info *info;
357 struct intrec **list;
359 int intr, orig_cpuid;
361 KKASSERT(cpuid >= 0 && cpuid < ncpus);
363 intr = ((intrec_t)id)->intr;
365 if (intr < 0 || intr >= MAX_INTS)
366 panic("register_int: bad intr %d", intr);
368 info = &intr_info_ary[intr];
370 int_moveto_destcpu(&orig_cpuid, cpuid);
373 * Remove the interrupt descriptor, adjust the descriptor count,
374 * and teardown the machine level vector if this was the last interrupt.
377 list = &info->i_reclist;
378 while ((rec = *list) != NULL) {
387 if (rec->intr_flags & INTR_CLOCK)
391 if (intr < FIRST_SOFTINT && info->i_fast + info->i_slow == 0)
392 machintr_intr_teardown(intr);
395 * Clear i_mplock_required if no handlers in the chain require the
398 for (rec0 = info->i_reclist; rec0; rec0 = rec0->next) {
399 if ((rec0->intr_flags & INTR_MPSAFE) == 0)
403 info->i_mplock_required = 0;
408 int_moveto_origcpu(orig_cpuid, cpuid);
414 kfree(rec->name, M_DEVBUF);
415 kfree(rec, M_DEVBUF);
417 kprintf("warning: unregister_int: int %d handler for %s not found\n",
418 intr, ((intrec_t)id)->name);
423 get_registered_name(int intr)
427 if (intr < 0 || intr >= MAX_INTS)
428 panic("register_int: bad intr %d", intr);
430 if ((rec = intr_info_ary[intr].i_reclist) == NULL)
439 count_registered_ints(int intr)
441 struct intr_info *info;
443 if (intr < 0 || intr >= MAX_INTS)
444 panic("register_int: bad intr %d", intr);
445 info = &intr_info_ary[intr];
446 return(info->i_fast + info->i_slow);
450 get_interrupt_counter(int intr)
452 struct intr_info *info;
454 if (intr < 0 || intr >= MAX_INTS)
455 panic("register_int: bad intr %d", intr);
456 info = &intr_info_ary[intr];
457 return(info->i_count);
462 swi_setpriority(int intr, int pri)
464 struct intr_info *info;
466 if (intr < FIRST_SOFTINT || intr >= MAX_INTS)
467 panic("register_swi: bad intr %d", intr);
468 info = &intr_info_ary[intr];
469 if (info->i_state != ISTATE_NOTHREAD)
470 lwkt_setpri(&info->i_thread, pri);
474 register_randintr(int intr)
476 struct intr_info *info;
478 if (intr < 0 || intr >= MAX_INTS)
479 panic("register_randintr: bad intr %d", intr);
480 info = &intr_info_ary[intr];
481 info->i_random.sc_intr = intr;
482 info->i_random.sc_enabled = 1;
486 unregister_randintr(int intr)
488 struct intr_info *info;
490 if (intr < 0 || intr >= MAX_INTS)
491 panic("register_swi: bad intr %d", intr);
492 info = &intr_info_ary[intr];
493 info->i_random.sc_enabled = -1;
497 next_registered_randintr(int intr)
499 struct intr_info *info;
501 if (intr < 0 || intr >= MAX_INTS)
502 panic("register_swi: bad intr %d", intr);
503 while (intr < MAX_INTS) {
504 info = &intr_info_ary[intr];
505 if (info->i_random.sc_enabled > 0)
513 * Dispatch an interrupt. If there's nothing to do we have a stray
514 * interrupt and can just return, leaving the interrupt masked.
516 * We need to schedule the interrupt and set its i_running bit. If
517 * we are not on the interrupt thread's cpu we have to send a message
518 * to the correct cpu that will issue the desired action (interlocking
519 * with the interrupt thread's critical section). We do NOT attempt to
520 * reschedule interrupts whos i_running bit is already set because
521 * this would prematurely wakeup a livelock-limited interrupt thread.
523 * i_running is only tested/set on the same cpu as the interrupt thread.
525 * We are NOT in a critical section, which will allow the scheduled
526 * interrupt to preempt us. The MP lock might *NOT* be held here.
531 sched_ithd_remote(void *arg)
533 sched_ithd((int)(intptr_t)arg);
541 struct intr_info *info;
543 info = &intr_info_ary[intr];
546 if (info->i_state != ISTATE_NOTHREAD) {
547 if (info->i_reclist == NULL) {
548 report_stray_interrupt(intr, info);
551 if (info->i_thread.td_gd == mycpu) {
552 if (info->i_running == 0) {
554 if (info->i_state != ISTATE_LIVELOCKED)
555 lwkt_schedule(&info->i_thread); /* MIGHT PREEMPT */
558 lwkt_send_ipiq(info->i_thread.td_gd,
559 sched_ithd_remote, (void *)(intptr_t)intr);
562 if (info->i_running == 0) {
564 if (info->i_state != ISTATE_LIVELOCKED)
565 lwkt_schedule(&info->i_thread); /* MIGHT PREEMPT */
570 report_stray_interrupt(intr, info);
575 report_stray_interrupt(int intr, struct intr_info *info)
577 ++info->i_straycount;
578 if (info->i_straycount < 10) {
579 if (info->i_errorticks == ticks)
581 info->i_errorticks = ticks;
582 kprintf("sched_ithd: stray interrupt %d on cpu %d\n",
584 } else if (info->i_straycount == 10) {
585 kprintf("sched_ithd: %ld stray interrupts %d on cpu %d - "
586 "there will be no further reports\n",
587 info->i_straycount, intr, mycpuid);
592 * This is run from a periodic SYSTIMER (and thus must be MP safe, the BGL
593 * might not be held).
596 ithread_livelock_wakeup(systimer_t st, int in_ipi __unused,
597 struct intrframe *frame __unused)
599 struct intr_info *info;
601 info = &intr_info_ary[(int)(intptr_t)st->data];
602 if (info->i_state != ISTATE_NOTHREAD)
603 lwkt_schedule(&info->i_thread);
607 * Schedule ithread within fast intr handler
609 * XXX Protect sched_ithd() call with gd_intr_nesting_level?
610 * Interrupts aren't enabled, but still...
613 ithread_fast_sched(int intr, thread_t td)
618 * We are already in critical section, exit it now to
623 crit_enter_quick(td);
629 * This function is called directly from the ICU or APIC vector code assembly
630 * to process an interrupt. The critical section and interrupt deferral
631 * checks have already been done but the function is entered WITHOUT
632 * a critical section held. The BGL may or may not be held.
634 * Must return non-zero if we do not want the vector code to re-enable
635 * the interrupt (which we don't if we have to schedule the interrupt)
637 int ithread_fast_handler(struct intrframe *frame);
640 ithread_fast_handler(struct intrframe *frame)
643 struct intr_info *info;
644 struct intrec **list;
649 TD_INVARIANTS_DECLARE;
654 intr = frame->if_vec;
658 /* We must be in critical section. */
659 KKASSERT(td->td_critcount);
661 info = &intr_info_ary[intr];
664 * If we are not processing any FAST interrupts, just schedule the thing.
666 if (info->i_fast == 0) {
668 ithread_fast_sched(intr, td);
673 * This should not normally occur since interrupts ought to be
674 * masked if the ithread has been scheduled or is running.
680 * Bump the interrupt nesting level to process any FAST interrupts.
681 * Obtain the MP lock as necessary. If the MP lock cannot be obtained,
682 * schedule the interrupt thread to deal with the issue instead.
684 * To reduce overhead, just leave the MP lock held once it has been
687 ++gd->gd_intr_nesting_level;
689 must_schedule = info->i_slow;
694 TD_INVARIANTS_GET(td);
695 list = &info->i_reclist;
697 for (rec = *list; rec; rec = nrec) {
698 /* rec may be invalid after call */
701 if (rec->intr_flags & INTR_CLOCK) {
703 if ((rec->intr_flags & INTR_MPSAFE) == 0 && got_mplock == 0) {
704 if (try_mplock() == 0) {
705 /* Couldn't get the MP lock; just schedule it. */
712 if (rec->serializer) {
713 must_schedule += lwkt_serialize_handler_try(
714 rec->serializer, rec->handler,
715 rec->argument, frame);
717 rec->handler(rec->argument, frame);
719 TD_INVARIANTS_TEST(td, rec->name);
726 --gd->gd_intr_nesting_level;
733 * If we had a problem, or mixed fast and slow interrupt handlers are
734 * registered, schedule the ithread to catch the missed records (it
735 * will just re-run all of them). A return value of 0 indicates that
736 * all handlers have been run and the interrupt can be re-enabled, and
737 * a non-zero return indicates that the interrupt thread controls
740 if (must_schedule > 0)
741 ithread_fast_sched(intr, td);
742 else if (must_schedule == 0)
744 return(must_schedule);
748 * Interrupt threads run this as their main loop.
750 * The handler begins execution outside a critical section and no MP lock.
752 * The i_running state starts at 0. When an interrupt occurs, the hardware
753 * interrupt is disabled and sched_ithd() The HW interrupt remains disabled
754 * until all routines have run. We then call ithread_done() to reenable
755 * the HW interrupt and deschedule us until the next interrupt.
757 * We are responsible for atomically checking i_running and ithread_done()
758 * is responsible for atomically checking for platform-specific delayed
759 * interrupts. i_running for our irq is only set in the context of our cpu,
760 * so a critical section is a sufficient interlock.
762 #define LIVELOCK_TIMEFRAME(freq) ((freq) >> 2) /* 1/4 second */
765 ithread_handler(void *arg)
767 struct intr_info *info;
772 struct intrec **list;
775 struct systimer ill_timer; /* enforced freq. timer */
776 u_int ill_count; /* interrupt livelock counter */
777 TD_INVARIANTS_DECLARE;
780 intr = (int)(intptr_t)arg;
781 info = &intr_info_ary[intr];
782 list = &info->i_reclist;
785 * The loop must be entered with one critical section held. The thread
786 * does not hold the mplock on startup.
789 lseconds = gd->gd_time_seconds;
795 * The chain is only considered MPSAFE if all its interrupt handlers
796 * are MPSAFE. However, if intr_mpsafe has been turned off we
797 * always operate with the BGL.
800 if (info->i_mplock_required != mpheld) {
801 if (info->i_mplock_required) {
802 KKASSERT(mpheld == 0);
806 KKASSERT(mpheld != 0);
813 TD_INVARIANTS_GET(gd->gd_curthread);
816 * If an interrupt is pending, clear i_running and execute the
817 * handlers. Note that certain types of interrupts can re-trigger
818 * and set i_running again.
820 * Each handler is run in a critical section. Note that we run both
821 * FAST and SLOW designated service routines.
823 if (info->i_running) {
828 report_stray_interrupt(intr, info);
830 for (rec = *list; rec; rec = nrec) {
831 /* rec may be invalid after call */
833 if (rec->serializer) {
834 lwkt_serialize_handler_call(rec->serializer, rec->handler,
835 rec->argument, NULL);
837 rec->handler(rec->argument, NULL);
839 TD_INVARIANTS_TEST(gd->gd_curthread, rec->name);
844 * This is our interrupt hook to add rate randomness to the random
847 if (info->i_random.sc_enabled > 0)
848 add_interrupt_randomness(intr);
851 * Unmask the interrupt to allow it to trigger again. This only
852 * applies to certain types of interrupts (typ level interrupts).
853 * This can result in the interrupt retriggering, but the retrigger
854 * will not be processed until we cycle our critical section.
856 * Only unmask interrupts while handlers are installed. It is
857 * possible to hit a situation where no handlers are installed
858 * due to a device driver livelocking and then tearing down its
859 * interrupt on close (the parallel bus being a good example).
861 if (intr < FIRST_SOFTINT && *list)
862 machintr_intr_enable(intr);
865 * Do a quick exit/enter to catch any higher-priority interrupt
866 * sources, such as the statclock, so thread time accounting
867 * will still work. This may also cause an interrupt to re-trigger.
873 * LIVELOCK STATE MACHINE
875 switch(info->i_state) {
878 * Reset the count each second.
880 if (lseconds != gd->gd_time_seconds) {
881 lseconds = gd->gd_time_seconds;
886 * If we did not exceed the frequency limit, we are done.
887 * If the interrupt has not retriggered we deschedule ourselves.
889 if (ill_count <= livelock_limit) {
890 if (info->i_running == 0) {
891 lwkt_deschedule_self(gd->gd_curthread);
898 * Otherwise we are livelocked. Set up a periodic systimer
899 * to wake the thread up at the limit frequency.
901 kprintf("intr %d at %d/%d hz, livelocked limit engaged!\n",
902 intr, ill_count, livelock_limit);
903 info->i_state = ISTATE_LIVELOCKED;
904 if ((use_limit = livelock_limit) < 100)
906 else if (use_limit > 500000)
908 systimer_init_periodic_nq(&ill_timer, ithread_livelock_wakeup,
909 (void *)(intptr_t)intr, use_limit);
911 case ISTATE_LIVELOCKED:
913 * Wait for our periodic timer to go off. Since the interrupt
914 * has re-armed it can still set i_running, but it will not
915 * reschedule us while we are in a livelocked state.
917 lwkt_deschedule_self(gd->gd_curthread);
921 * Check once a second to see if the livelock condition no
924 if (lseconds != gd->gd_time_seconds) {
925 lseconds = gd->gd_time_seconds;
926 if (ill_count < livelock_lowater) {
927 info->i_state = ISTATE_NORMAL;
928 systimer_del(&ill_timer);
929 kprintf("intr %d at %d/%d hz, livelock removed\n",
930 intr, ill_count, livelock_lowater);
931 } else if (livelock_debug == intr ||
932 (bootverbose && cold)) {
933 kprintf("intr %d at %d/%d hz, in livelock\n",
934 intr, ill_count, livelock_lowater);
945 * Emergency interrupt polling thread. The thread begins execution
946 * outside a critical section with the BGL held.
948 * If emergency interrupt polling is enabled, this thread will
949 * execute all system interrupts not marked INTR_NOPOLL at the
950 * specified polling frequency.
952 * WARNING! This thread runs *ALL* interrupt service routines that
953 * are not marked INTR_NOPOLL, which basically means everything except
954 * the 8254 clock interrupt and the ATA interrupt. It has very high
955 * overhead and should only be used in situations where the machine
956 * cannot otherwise be made to work. Due to the severe performance
957 * degredation, it should not be enabled on production machines.
960 ithread_emergency(void *arg __unused)
962 globaldata_t gd = mycpu;
963 struct intr_info *info;
966 TD_INVARIANTS_DECLARE;
970 TD_INVARIANTS_GET(gd->gd_curthread);
973 for (intr = 0; intr < max_installed_hard_intr; ++intr) {
974 info = &intr_info_ary[intr];
975 for (rec = info->i_reclist; rec; rec = nrec) {
976 /* rec may be invalid after call */
978 if ((rec->intr_flags & INTR_NOPOLL) == 0) {
979 if (rec->serializer) {
980 lwkt_serialize_handler_try(rec->serializer,
981 rec->handler, rec->argument, NULL);
983 rec->handler(rec->argument, NULL);
985 TD_INVARIANTS_TEST(gd->gd_curthread, rec->name);
989 lwkt_deschedule_self(gd->gd_curthread);
996 * Systimer callback - schedule the emergency interrupt poll thread
997 * if emergency polling is enabled.
1001 emergency_intr_timer_callback(systimer_t info, int in_ipi __unused,
1002 struct intrframe *frame __unused)
1004 if (emergency_intr_enable)
1005 lwkt_schedule(info->data);
1009 ithread_cpuid(int intr)
1011 const struct intr_info *info;
1013 KKASSERT(intr >= 0 && intr < MAX_INTS);
1014 info = &intr_info_ary[intr];
1016 if (info->i_state == ISTATE_NOTHREAD)
1018 return info->i_thread.td_gd->gd_cpuid;
1022 * Sysctls used by systat and others: hw.intrnames and hw.intrcnt.
1023 * The data for this machine dependent, and the declarations are in machine
1024 * dependent code. The layout of intrnames and intrcnt however is machine
1027 * We do not know the length of intrcnt and intrnames at compile time, so
1028 * calculate things at run time.
1032 sysctl_intrnames(SYSCTL_HANDLER_ARGS)
1034 struct intr_info *info;
1041 for (intr = 0; error == 0 && intr < MAX_INTS; ++intr) {
1042 info = &intr_info_ary[intr];
1046 for (rec = info->i_reclist; rec; rec = rec->next) {
1047 ksnprintf(buf + len, sizeof(buf) - len, "%s%s",
1048 (len ? "/" : ""), rec->name);
1049 len += strlen(buf + len);
1052 ksnprintf(buf, sizeof(buf), "irq%d", intr);
1055 error = SYSCTL_OUT(req, buf, len + 1);
1061 SYSCTL_PROC(_hw, OID_AUTO, intrnames, CTLTYPE_OPAQUE | CTLFLAG_RD,
1062 NULL, 0, sysctl_intrnames, "", "Interrupt Names");
1065 sysctl_intrcnt(SYSCTL_HANDLER_ARGS)
1067 struct intr_info *info;
1071 for (intr = 0; intr < max_installed_hard_intr; ++intr) {
1072 info = &intr_info_ary[intr];
1074 error = SYSCTL_OUT(req, &info->i_count, sizeof(info->i_count));
1078 for (intr = FIRST_SOFTINT; intr < max_installed_soft_intr; ++intr) {
1079 info = &intr_info_ary[intr];
1081 error = SYSCTL_OUT(req, &info->i_count, sizeof(info->i_count));
1089 SYSCTL_PROC(_hw, OID_AUTO, intrcnt, CTLTYPE_OPAQUE | CTLFLAG_RD,
1090 NULL, 0, sysctl_intrcnt, "", "Interrupt Counts");
1093 sysctl_intrcnt_all(SYSCTL_HANDLER_ARGS)
1095 struct intr_info *info;
1099 for (intr = 0; intr < MAX_INTS; ++intr) {
1100 info = &intr_info_ary[intr];
1102 error = SYSCTL_OUT(req, &info->i_count, sizeof(info->i_count));
1110 SYSCTL_PROC(_hw, OID_AUTO, intrcnt_all, CTLTYPE_OPAQUE | CTLFLAG_RD,
1111 NULL, 0, sysctl_intrcnt_all, "", "Interrupt Counts");
1114 int_moveto_destcpu(int *orig_cpuid0, int cpuid)
1116 int orig_cpuid = mycpuid;
1118 if (cpuid != orig_cpuid)
1119 lwkt_migratecpu(cpuid);
1121 *orig_cpuid0 = orig_cpuid;
1125 int_moveto_origcpu(int orig_cpuid, int cpuid)
1127 if (cpuid != orig_cpuid)
1128 lwkt_migratecpu(orig_cpuid);