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/interrupt.h>
47 #include <sys/thread2.h>
48 #include <sys/mplock2.h>
52 typedef struct intrec {
54 struct intr_info *info;
60 struct lwkt_serialize *serializer;
65 struct thread i_thread;
66 struct random_softc i_random;
68 long i_count; /* interrupts dispatched */
69 int i_mplock_required;
74 unsigned long i_straycount;
75 } intr_info_ary[MAX_INTS];
77 int max_installed_hard_intr;
78 int max_installed_soft_intr;
80 #define EMERGENCY_INTR_POLLING_FREQ_MAX 20000
83 * Assert that callers into interrupt handlers don't return with
84 * dangling tokens, spinlocks, or mp locks.
88 #define TD_INVARIANTS_DECLARE \
92 #define TD_INVARIANTS_GET(td) \
94 spincount = (td)->td_gd->gd_spinlocks_wr; \
95 curstop = (td)->td_toks_stop; \
98 #define TD_INVARIANTS_TEST(td, name) \
100 KASSERT(spincount == (td)->td_gd->gd_spinlocks_wr, \
101 ("spincount mismatch after interrupt handler %s", \
103 KASSERT(curstop == (td)->td_toks_stop, \
104 ("token count mismatch after interrupt handler %s", \
112 #define TD_INVARIANTS_DECLARE
113 #define TD_INVARIANTS_GET(td)
114 #define TD_INVARIANTS_TEST(td, name)
116 #endif /* ndef INVARIANTS */
118 static int sysctl_emergency_freq(SYSCTL_HANDLER_ARGS);
119 static int sysctl_emergency_enable(SYSCTL_HANDLER_ARGS);
120 static void emergency_intr_timer_callback(systimer_t, int, struct intrframe *);
121 static void ithread_handler(void *arg);
122 static void ithread_emergency(void *arg);
123 static void report_stray_interrupt(int intr, struct intr_info *info);
124 static void int_moveto_destcpu(int *, int *, int);
125 static void int_moveto_origcpu(int, int);
127 int intr_info_size = NELEM(intr_info_ary);
129 static struct systimer emergency_intr_timer;
130 static struct thread emergency_intr_thread;
132 #define ISTATE_NOTHREAD 0
133 #define ISTATE_NORMAL 1
134 #define ISTATE_LIVELOCKED 2
136 static int livelock_limit = 40000;
137 static int livelock_lowater = 20000;
138 static int livelock_debug = -1;
139 SYSCTL_INT(_kern, OID_AUTO, livelock_limit,
140 CTLFLAG_RW, &livelock_limit, 0, "Livelock interrupt rate limit");
141 SYSCTL_INT(_kern, OID_AUTO, livelock_lowater,
142 CTLFLAG_RW, &livelock_lowater, 0, "Livelock low-water mark restore");
143 SYSCTL_INT(_kern, OID_AUTO, livelock_debug,
144 CTLFLAG_RW, &livelock_debug, 0, "Livelock debug intr#");
146 static int emergency_intr_enable = 0; /* emergency interrupt polling */
147 TUNABLE_INT("kern.emergency_intr_enable", &emergency_intr_enable);
148 SYSCTL_PROC(_kern, OID_AUTO, emergency_intr_enable, CTLTYPE_INT | CTLFLAG_RW,
149 0, 0, sysctl_emergency_enable, "I", "Emergency Interrupt Poll Enable");
151 static int emergency_intr_freq = 10; /* emergency polling frequency */
152 TUNABLE_INT("kern.emergency_intr_freq", &emergency_intr_freq);
153 SYSCTL_PROC(_kern, OID_AUTO, emergency_intr_freq, CTLTYPE_INT | CTLFLAG_RW,
154 0, 0, sysctl_emergency_freq, "I", "Emergency Interrupt Poll Frequency");
157 * Sysctl support routines
160 sysctl_emergency_enable(SYSCTL_HANDLER_ARGS)
164 enabled = emergency_intr_enable;
165 error = sysctl_handle_int(oidp, &enabled, 0, req);
166 if (error || req->newptr == NULL)
168 emergency_intr_enable = enabled;
169 if (emergency_intr_enable) {
170 systimer_adjust_periodic(&emergency_intr_timer,
171 emergency_intr_freq);
173 systimer_adjust_periodic(&emergency_intr_timer, 1);
179 sysctl_emergency_freq(SYSCTL_HANDLER_ARGS)
183 phz = emergency_intr_freq;
184 error = sysctl_handle_int(oidp, &phz, 0, req);
185 if (error || req->newptr == NULL)
189 else if (phz > EMERGENCY_INTR_POLLING_FREQ_MAX)
190 phz = EMERGENCY_INTR_POLLING_FREQ_MAX;
192 emergency_intr_freq = phz;
193 if (emergency_intr_enable) {
194 systimer_adjust_periodic(&emergency_intr_timer,
195 emergency_intr_freq);
197 systimer_adjust_periodic(&emergency_intr_timer, 1);
203 * Register an SWI or INTerrupt handler.
206 register_swi(int intr, inthand2_t *handler, void *arg, const char *name,
207 struct lwkt_serialize *serializer)
209 if (intr < FIRST_SOFTINT || intr >= MAX_INTS)
210 panic("register_swi: bad intr %d", intr);
211 return(register_int(intr, handler, arg, name, serializer, 0));
215 register_swi_mp(int intr, inthand2_t *handler, void *arg, const char *name,
216 struct lwkt_serialize *serializer)
218 if (intr < FIRST_SOFTINT || intr >= MAX_INTS)
219 panic("register_swi: bad intr %d", intr);
220 return(register_int(intr, handler, arg, name, serializer, INTR_MPSAFE));
224 register_int(int intr, inthand2_t *handler, void *arg, const char *name,
225 struct lwkt_serialize *serializer, int intr_flags)
227 struct intr_info *info;
228 struct intrec **list;
230 int orig_cpuid, cpuid;
232 if (intr < 0 || intr >= MAX_INTS)
233 panic("register_int: bad intr %d", intr);
236 info = &intr_info_ary[intr];
239 * Construct an interrupt handler record
241 rec = kmalloc(sizeof(struct intrec), M_DEVBUF, M_INTWAIT);
242 rec->name = kmalloc(strlen(name) + 1, M_DEVBUF, M_INTWAIT);
243 strcpy(rec->name, name);
246 rec->handler = handler;
249 rec->intr_flags = intr_flags;
251 rec->serializer = serializer;
254 * Create an emergency polling thread and set up a systimer to wake
257 if (emergency_intr_thread.td_kstack == NULL) {
258 lwkt_create(ithread_emergency, NULL, NULL, &emergency_intr_thread,
259 TDF_STOPREQ | TDF_INTTHREAD, -1, "ithread emerg");
260 systimer_init_periodic_nq(&emergency_intr_timer,
261 emergency_intr_timer_callback, &emergency_intr_thread,
262 (emergency_intr_enable ? emergency_intr_freq : 1));
265 int_moveto_destcpu(&orig_cpuid, &cpuid, intr);
268 * Create an interrupt thread if necessary, leave it in an unscheduled
271 if (info->i_state == ISTATE_NOTHREAD) {
272 info->i_state = ISTATE_NORMAL;
273 lwkt_create(ithread_handler, (void *)(intptr_t)intr, NULL,
274 &info->i_thread, TDF_STOPREQ | TDF_INTTHREAD, -1,
276 if (intr >= FIRST_SOFTINT)
277 lwkt_setpri(&info->i_thread, TDPRI_SOFT_NORM);
279 lwkt_setpri(&info->i_thread, TDPRI_INT_MED);
280 info->i_thread.td_preemptable = lwkt_preempt;
283 list = &info->i_reclist;
286 * Keep track of how many fast and slow interrupts we have.
287 * Set i_mplock_required if any handler in the chain requires
288 * the MP lock to operate.
290 if ((intr_flags & INTR_MPSAFE) == 0)
291 info->i_mplock_required = 1;
292 if (intr_flags & INTR_CLOCK)
298 * Enable random number generation keying off of this interrupt.
300 if ((intr_flags & INTR_NOENTROPY) == 0 && info->i_random.sc_enabled == 0) {
301 info->i_random.sc_enabled = 1;
302 info->i_random.sc_intr = intr;
306 * Add the record to the interrupt list.
309 while (*list != NULL)
310 list = &(*list)->next;
315 * Update max_installed_hard_intr to make the emergency intr poll
316 * a bit more efficient.
318 if (intr < FIRST_SOFTINT) {
319 if (max_installed_hard_intr <= intr)
320 max_installed_hard_intr = intr + 1;
322 if (max_installed_soft_intr <= intr)
323 max_installed_soft_intr = intr + 1;
327 * Setup the machine level interrupt vector
329 if (intr < FIRST_SOFTINT && info->i_slow + info->i_fast == 1) {
330 if (machintr_vector_setup(intr, intr_flags))
331 kprintf("machintr_vector_setup: failed on irq %d\n", intr);
334 int_moveto_origcpu(orig_cpuid, cpuid);
340 unregister_swi(void *id)
346 unregister_int(void *id)
348 struct intr_info *info;
349 struct intrec **list;
351 int intr, orig_cpuid, cpuid;
353 intr = ((intrec_t)id)->intr;
355 if (intr < 0 || intr >= MAX_INTS)
356 panic("register_int: bad intr %d", intr);
358 info = &intr_info_ary[intr];
360 int_moveto_destcpu(&orig_cpuid, &cpuid, intr);
363 * Remove the interrupt descriptor, adjust the descriptor count,
364 * and teardown the machine level vector if this was the last interrupt.
367 list = &info->i_reclist;
368 while ((rec = *list) != NULL) {
377 if (rec->intr_flags & INTR_CLOCK)
381 if (intr < FIRST_SOFTINT && info->i_fast + info->i_slow == 0)
382 machintr_vector_teardown(intr);
385 * Clear i_mplock_required if no handlers in the chain require the
388 for (rec0 = info->i_reclist; rec0; rec0 = rec0->next) {
389 if ((rec0->intr_flags & INTR_MPSAFE) == 0)
393 info->i_mplock_required = 0;
398 int_moveto_origcpu(orig_cpuid, cpuid);
404 kfree(rec->name, M_DEVBUF);
405 kfree(rec, M_DEVBUF);
407 kprintf("warning: unregister_int: int %d handler for %s not found\n",
408 intr, ((intrec_t)id)->name);
413 get_registered_name(int intr)
417 if (intr < 0 || intr >= MAX_INTS)
418 panic("register_int: bad intr %d", intr);
420 if ((rec = intr_info_ary[intr].i_reclist) == NULL)
429 count_registered_ints(int intr)
431 struct intr_info *info;
433 if (intr < 0 || intr >= MAX_INTS)
434 panic("register_int: bad intr %d", intr);
435 info = &intr_info_ary[intr];
436 return(info->i_fast + info->i_slow);
440 get_interrupt_counter(int intr)
442 struct intr_info *info;
444 if (intr < 0 || intr >= MAX_INTS)
445 panic("register_int: bad intr %d", intr);
446 info = &intr_info_ary[intr];
447 return(info->i_count);
452 swi_setpriority(int intr, int pri)
454 struct intr_info *info;
456 if (intr < FIRST_SOFTINT || intr >= MAX_INTS)
457 panic("register_swi: bad intr %d", intr);
458 info = &intr_info_ary[intr];
459 if (info->i_state != ISTATE_NOTHREAD)
460 lwkt_setpri(&info->i_thread, pri);
464 register_randintr(int intr)
466 struct intr_info *info;
468 if (intr < 0 || intr >= MAX_INTS)
469 panic("register_randintr: bad intr %d", intr);
470 info = &intr_info_ary[intr];
471 info->i_random.sc_intr = intr;
472 info->i_random.sc_enabled = 1;
476 unregister_randintr(int intr)
478 struct intr_info *info;
480 if (intr < 0 || intr >= MAX_INTS)
481 panic("register_swi: bad intr %d", intr);
482 info = &intr_info_ary[intr];
483 info->i_random.sc_enabled = -1;
487 next_registered_randintr(int intr)
489 struct intr_info *info;
491 if (intr < 0 || intr >= MAX_INTS)
492 panic("register_swi: bad intr %d", intr);
493 while (intr < MAX_INTS) {
494 info = &intr_info_ary[intr];
495 if (info->i_random.sc_enabled > 0)
503 * Dispatch an interrupt. If there's nothing to do we have a stray
504 * interrupt and can just return, leaving the interrupt masked.
506 * We need to schedule the interrupt and set its i_running bit. If
507 * we are not on the interrupt thread's cpu we have to send a message
508 * to the correct cpu that will issue the desired action (interlocking
509 * with the interrupt thread's critical section). We do NOT attempt to
510 * reschedule interrupts whos i_running bit is already set because
511 * this would prematurely wakeup a livelock-limited interrupt thread.
513 * i_running is only tested/set on the same cpu as the interrupt thread.
515 * We are NOT in a critical section, which will allow the scheduled
516 * interrupt to preempt us. The MP lock might *NOT* be held here.
521 sched_ithd_remote(void *arg)
523 sched_ithd((int)(intptr_t)arg);
531 struct intr_info *info;
533 info = &intr_info_ary[intr];
536 if (info->i_state != ISTATE_NOTHREAD) {
537 if (info->i_reclist == NULL) {
538 report_stray_interrupt(intr, info);
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,
549 sched_ithd_remote, (void *)(intptr_t)intr);
552 if (info->i_running == 0) {
554 if (info->i_state != ISTATE_LIVELOCKED)
555 lwkt_schedule(&info->i_thread); /* MIGHT PREEMPT */
560 report_stray_interrupt(intr, info);
565 report_stray_interrupt(int intr, struct intr_info *info)
567 ++info->i_straycount;
568 if (info->i_straycount < 10) {
569 if (info->i_errorticks == ticks)
571 info->i_errorticks = ticks;
572 kprintf("sched_ithd: stray interrupt %d on cpu %d\n",
574 } else if (info->i_straycount == 10) {
575 kprintf("sched_ithd: %ld stray interrupts %d on cpu %d - "
576 "there will be no further reports\n",
577 info->i_straycount, intr, mycpuid);
582 * This is run from a periodic SYSTIMER (and thus must be MP safe, the BGL
583 * might not be held).
586 ithread_livelock_wakeup(systimer_t st, int in_ipi __unused,
587 struct intrframe *frame __unused)
589 struct intr_info *info;
591 info = &intr_info_ary[(int)(intptr_t)st->data];
592 if (info->i_state != ISTATE_NOTHREAD)
593 lwkt_schedule(&info->i_thread);
597 * Schedule ithread within fast intr handler
599 * XXX Protect sched_ithd() call with gd_intr_nesting_level?
600 * Interrupts aren't enabled, but still...
603 ithread_fast_sched(int intr, thread_t td)
608 * We are already in critical section, exit it now to
613 crit_enter_quick(td);
619 * This function is called directly from the ICU or APIC vector code assembly
620 * to process an interrupt. The critical section and interrupt deferral
621 * checks have already been done but the function is entered WITHOUT
622 * a critical section held. The BGL may or may not be held.
624 * Must return non-zero if we do not want the vector code to re-enable
625 * the interrupt (which we don't if we have to schedule the interrupt)
627 int ithread_fast_handler(struct intrframe *frame);
630 ithread_fast_handler(struct intrframe *frame)
633 struct intr_info *info;
634 struct intrec **list;
639 TD_INVARIANTS_DECLARE;
644 intr = frame->if_vec;
648 /* We must be in critical section. */
649 KKASSERT(td->td_critcount);
651 info = &intr_info_ary[intr];
654 * If we are not processing any FAST interrupts, just schedule the thing.
656 if (info->i_fast == 0) {
658 ithread_fast_sched(intr, td);
663 * This should not normally occur since interrupts ought to be
664 * masked if the ithread has been scheduled or is running.
670 * Bump the interrupt nesting level to process any FAST interrupts.
671 * Obtain the MP lock as necessary. If the MP lock cannot be obtained,
672 * schedule the interrupt thread to deal with the issue instead.
674 * To reduce overhead, just leave the MP lock held once it has been
677 ++gd->gd_intr_nesting_level;
679 must_schedule = info->i_slow;
684 TD_INVARIANTS_GET(td);
685 list = &info->i_reclist;
687 for (rec = *list; rec; rec = nrec) {
688 /* rec may be invalid after call */
691 if (rec->intr_flags & INTR_CLOCK) {
693 if ((rec->intr_flags & INTR_MPSAFE) == 0 && got_mplock == 0) {
694 if (try_mplock() == 0) {
695 /* Couldn't get the MP lock; just schedule it. */
702 if (rec->serializer) {
703 must_schedule += lwkt_serialize_handler_try(
704 rec->serializer, rec->handler,
705 rec->argument, frame);
707 rec->handler(rec->argument, frame);
709 TD_INVARIANTS_TEST(td, rec->name);
716 --gd->gd_intr_nesting_level;
723 * If we had a problem, or mixed fast and slow interrupt handlers are
724 * registered, schedule the ithread to catch the missed records (it
725 * will just re-run all of them). A return value of 0 indicates that
726 * all handlers have been run and the interrupt can be re-enabled, and
727 * a non-zero return indicates that the interrupt thread controls
730 if (must_schedule > 0)
731 ithread_fast_sched(intr, td);
732 else if (must_schedule == 0)
734 return(must_schedule);
738 * Interrupt threads run this as their main loop.
740 * The handler begins execution outside a critical section and no MP lock.
742 * The i_running state starts at 0. When an interrupt occurs, the hardware
743 * interrupt is disabled and sched_ithd() The HW interrupt remains disabled
744 * until all routines have run. We then call ithread_done() to reenable
745 * the HW interrupt and deschedule us until the next interrupt.
747 * We are responsible for atomically checking i_running and ithread_done()
748 * is responsible for atomically checking for platform-specific delayed
749 * interrupts. i_running for our irq is only set in the context of our cpu,
750 * so a critical section is a sufficient interlock.
752 #define LIVELOCK_TIMEFRAME(freq) ((freq) >> 2) /* 1/4 second */
755 ithread_handler(void *arg)
757 struct intr_info *info;
762 struct intrec **list;
765 struct systimer ill_timer; /* enforced freq. timer */
766 u_int ill_count; /* interrupt livelock counter */
767 TD_INVARIANTS_DECLARE;
770 intr = (int)(intptr_t)arg;
771 info = &intr_info_ary[intr];
772 list = &info->i_reclist;
775 * The loop must be entered with one critical section held. The thread
776 * does not hold the mplock on startup.
779 lseconds = gd->gd_time_seconds;
785 * The chain is only considered MPSAFE if all its interrupt handlers
786 * are MPSAFE. However, if intr_mpsafe has been turned off we
787 * always operate with the BGL.
790 if (info->i_mplock_required != mpheld) {
791 if (info->i_mplock_required) {
792 KKASSERT(mpheld == 0);
796 KKASSERT(mpheld != 0);
803 TD_INVARIANTS_GET(gd->gd_curthread);
806 * If an interrupt is pending, clear i_running and execute the
807 * handlers. Note that certain types of interrupts can re-trigger
808 * and set i_running again.
810 * Each handler is run in a critical section. Note that we run both
811 * FAST and SLOW designated service routines.
813 if (info->i_running) {
818 report_stray_interrupt(intr, info);
820 for (rec = *list; rec; rec = nrec) {
821 /* rec may be invalid after call */
823 if (rec->serializer) {
824 lwkt_serialize_handler_call(rec->serializer, rec->handler,
825 rec->argument, NULL);
827 rec->handler(rec->argument, NULL);
829 TD_INVARIANTS_TEST(gd->gd_curthread, rec->name);
834 * This is our interrupt hook to add rate randomness to the random
837 if (info->i_random.sc_enabled > 0)
838 add_interrupt_randomness(intr);
841 * Unmask the interrupt to allow it to trigger again. This only
842 * applies to certain types of interrupts (typ level interrupts).
843 * This can result in the interrupt retriggering, but the retrigger
844 * will not be processed until we cycle our critical section.
846 * Only unmask interrupts while handlers are installed. It is
847 * possible to hit a situation where no handlers are installed
848 * due to a device driver livelocking and then tearing down its
849 * interrupt on close (the parallel bus being a good example).
851 if (intr < FIRST_SOFTINT && *list)
852 machintr_intren(intr);
855 * Do a quick exit/enter to catch any higher-priority interrupt
856 * sources, such as the statclock, so thread time accounting
857 * will still work. This may also cause an interrupt to re-trigger.
863 * LIVELOCK STATE MACHINE
865 switch(info->i_state) {
868 * Reset the count each second.
870 if (lseconds != gd->gd_time_seconds) {
871 lseconds = gd->gd_time_seconds;
876 * If we did not exceed the frequency limit, we are done.
877 * If the interrupt has not retriggered we deschedule ourselves.
879 if (ill_count <= livelock_limit) {
880 if (info->i_running == 0) {
881 lwkt_deschedule_self(gd->gd_curthread);
888 * Otherwise we are livelocked. Set up a periodic systimer
889 * to wake the thread up at the limit frequency.
891 kprintf("intr %d at %d/%d hz, livelocked limit engaged!\n",
892 intr, ill_count, livelock_limit);
893 info->i_state = ISTATE_LIVELOCKED;
894 if ((use_limit = livelock_limit) < 100)
896 else if (use_limit > 500000)
898 systimer_init_periodic_nq(&ill_timer, ithread_livelock_wakeup,
899 (void *)(intptr_t)intr, use_limit);
901 case ISTATE_LIVELOCKED:
903 * Wait for our periodic timer to go off. Since the interrupt
904 * has re-armed it can still set i_running, but it will not
905 * reschedule us while we are in a livelocked state.
907 lwkt_deschedule_self(gd->gd_curthread);
911 * Check once a second to see if the livelock condition no
914 if (lseconds != gd->gd_time_seconds) {
915 lseconds = gd->gd_time_seconds;
916 if (ill_count < livelock_lowater) {
917 info->i_state = ISTATE_NORMAL;
918 systimer_del(&ill_timer);
919 kprintf("intr %d at %d/%d hz, livelock removed\n",
920 intr, ill_count, livelock_lowater);
921 } else if (livelock_debug == intr ||
922 (bootverbose && cold)) {
923 kprintf("intr %d at %d/%d hz, in livelock\n",
924 intr, ill_count, livelock_lowater);
935 * Emergency interrupt polling thread. The thread begins execution
936 * outside a critical section with the BGL held.
938 * If emergency interrupt polling is enabled, this thread will
939 * execute all system interrupts not marked INTR_NOPOLL at the
940 * specified polling frequency.
942 * WARNING! This thread runs *ALL* interrupt service routines that
943 * are not marked INTR_NOPOLL, which basically means everything except
944 * the 8254 clock interrupt and the ATA interrupt. It has very high
945 * overhead and should only be used in situations where the machine
946 * cannot otherwise be made to work. Due to the severe performance
947 * degredation, it should not be enabled on production machines.
950 ithread_emergency(void *arg __unused)
952 globaldata_t gd = mycpu;
953 struct intr_info *info;
956 TD_INVARIANTS_DECLARE;
960 TD_INVARIANTS_GET(gd->gd_curthread);
963 for (intr = 0; intr < max_installed_hard_intr; ++intr) {
964 info = &intr_info_ary[intr];
965 for (rec = info->i_reclist; rec; rec = nrec) {
966 /* rec may be invalid after call */
968 if ((rec->intr_flags & INTR_NOPOLL) == 0) {
969 if (rec->serializer) {
970 lwkt_serialize_handler_try(rec->serializer,
971 rec->handler, rec->argument, NULL);
973 rec->handler(rec->argument, NULL);
975 TD_INVARIANTS_TEST(gd->gd_curthread, rec->name);
979 lwkt_deschedule_self(gd->gd_curthread);
986 * Systimer callback - schedule the emergency interrupt poll thread
987 * if emergency polling is enabled.
991 emergency_intr_timer_callback(systimer_t info, int in_ipi __unused,
992 struct intrframe *frame __unused)
994 if (emergency_intr_enable)
995 lwkt_schedule(info->data);
999 ithread_cpuid(int intr)
1001 const struct intr_info *info;
1003 KKASSERT(intr >= 0 && intr < MAX_INTS);
1004 info = &intr_info_ary[intr];
1006 if (info->i_state == ISTATE_NOTHREAD)
1008 return info->i_thread.td_gd->gd_cpuid;
1012 * Sysctls used by systat and others: hw.intrnames and hw.intrcnt.
1013 * The data for this machine dependent, and the declarations are in machine
1014 * dependent code. The layout of intrnames and intrcnt however is machine
1017 * We do not know the length of intrcnt and intrnames at compile time, so
1018 * calculate things at run time.
1022 sysctl_intrnames(SYSCTL_HANDLER_ARGS)
1024 struct intr_info *info;
1031 for (intr = 0; error == 0 && intr < MAX_INTS; ++intr) {
1032 info = &intr_info_ary[intr];
1036 for (rec = info->i_reclist; rec; rec = rec->next) {
1037 ksnprintf(buf + len, sizeof(buf) - len, "%s%s",
1038 (len ? "/" : ""), rec->name);
1039 len += strlen(buf + len);
1042 ksnprintf(buf, sizeof(buf), "irq%d", intr);
1045 error = SYSCTL_OUT(req, buf, len + 1);
1051 SYSCTL_PROC(_hw, OID_AUTO, intrnames, CTLTYPE_OPAQUE | CTLFLAG_RD,
1052 NULL, 0, sysctl_intrnames, "", "Interrupt Names");
1055 sysctl_intrcnt(SYSCTL_HANDLER_ARGS)
1057 struct intr_info *info;
1061 for (intr = 0; intr < max_installed_hard_intr; ++intr) {
1062 info = &intr_info_ary[intr];
1064 error = SYSCTL_OUT(req, &info->i_count, sizeof(info->i_count));
1068 for (intr = FIRST_SOFTINT; intr < max_installed_soft_intr; ++intr) {
1069 info = &intr_info_ary[intr];
1071 error = SYSCTL_OUT(req, &info->i_count, sizeof(info->i_count));
1079 SYSCTL_PROC(_hw, OID_AUTO, intrcnt, CTLTYPE_OPAQUE | CTLFLAG_RD,
1080 NULL, 0, sysctl_intrcnt, "", "Interrupt Counts");
1083 sysctl_intrcnt_all(SYSCTL_HANDLER_ARGS)
1085 struct intr_info *info;
1089 for (intr = 0; intr < MAX_INTS; ++intr) {
1090 info = &intr_info_ary[intr];
1092 error = SYSCTL_OUT(req, &info->i_count, sizeof(info->i_count));
1100 SYSCTL_PROC(_hw, OID_AUTO, intrcnt_all, CTLTYPE_OPAQUE | CTLFLAG_RD,
1101 NULL, 0, sysctl_intrcnt_all, "", "Interrupt Counts");
1104 int_moveto_destcpu(int *orig_cpuid0, int *cpuid0, int intr)
1106 int orig_cpuid = mycpuid, cpuid;
1110 ksnprintf(envpath, sizeof(envpath), "hw.irq.%d.dest", intr);
1111 kgetenv_int(envpath, &cpuid);
1115 if (cpuid != orig_cpuid)
1116 lwkt_migratecpu(cpuid);
1118 *orig_cpuid0 = orig_cpuid;
1123 int_moveto_origcpu(int orig_cpuid, int cpuid)
1125 if (cpuid != orig_cpuid)
1126 lwkt_migratecpu(orig_cpuid);