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 short i_mplock_required
;
73 unsigned long i_straycount
;
78 struct intr_info_block
{
79 struct intr_info ary
[MAXCPU
][MAX_INTS
];
82 static struct intr_info_block
*intr_block
;
83 static struct intr_info
*swi_info_ary
[MAX_SOFTINTS
];
85 static int max_installed_hard_intr
[MAXCPU
];
87 MALLOC_DEFINE(M_INTRMNG
, "intrmng", "interrupt management");
90 #define EMERGENCY_INTR_POLLING_FREQ_MAX 20000
93 * Assert that callers into interrupt handlers don't return with
94 * dangling tokens, spinlocks, or mp locks.
98 #define TD_INVARIANTS_DECLARE \
100 lwkt_tokref_t curstop
102 #define TD_INVARIANTS_GET(td) \
104 spincount = (td)->td_gd->gd_spinlocks; \
105 curstop = (td)->td_toks_stop; \
108 #define TD_INVARIANTS_TEST(td, name) \
110 KASSERT(spincount == (td)->td_gd->gd_spinlocks, \
111 ("spincount mismatch after interrupt handler %s", \
113 KASSERT(curstop == (td)->td_toks_stop, \
114 ("token count mismatch after interrupt handler %s", \
122 #define TD_INVARIANTS_DECLARE
123 #define TD_INVARIANTS_GET(td)
124 #define TD_INVARIANTS_TEST(td, name)
126 #endif /* ndef INVARIANTS */
128 static int sysctl_emergency_freq(SYSCTL_HANDLER_ARGS
);
129 static int sysctl_emergency_enable(SYSCTL_HANDLER_ARGS
);
130 static void emergency_intr_timer_callback(systimer_t
, int, struct intrframe
*);
131 static void ithread_handler(void *arg
);
132 static void ithread_emergency(void *arg
);
133 static void report_stray_interrupt(struct intr_info
*info
, const char *func
);
134 static void int_moveto_destcpu(int *, int);
135 static void int_moveto_origcpu(int, int);
136 static void sched_ithd_intern(struct intr_info
*info
);
138 static struct systimer emergency_intr_timer
[MAXCPU
];
139 static struct thread
*emergency_intr_thread
[MAXCPU
];
141 #define ISTATE_NOTHREAD 0
142 #define ISTATE_NORMAL 1
143 #define ISTATE_LIVELOCKED 2
145 static int livelock_limit
= 40000;
146 static int livelock_limit_hi
= 120000;
147 static int livelock_lowater
= 20000;
148 static int livelock_debug
= -1;
149 SYSCTL_INT(_kern
, OID_AUTO
, livelock_limit
,
150 CTLFLAG_RW
, &livelock_limit
, 0, "Livelock interrupt rate limit");
151 SYSCTL_INT(_kern
, OID_AUTO
, livelock_limit_hi
,
152 CTLFLAG_RW
, &livelock_limit_hi
, 0,
153 "Livelock interrupt rate limit (high frequency)");
154 SYSCTL_INT(_kern
, OID_AUTO
, livelock_lowater
,
155 CTLFLAG_RW
, &livelock_lowater
, 0, "Livelock low-water mark restore");
156 SYSCTL_INT(_kern
, OID_AUTO
, livelock_debug
,
157 CTLFLAG_RW
, &livelock_debug
, 0, "Livelock debug intr#");
159 static int emergency_intr_enable
= 0; /* emergency interrupt polling */
160 TUNABLE_INT("kern.emergency_intr_enable", &emergency_intr_enable
);
161 SYSCTL_PROC(_kern
, OID_AUTO
, emergency_intr_enable
, CTLTYPE_INT
| CTLFLAG_RW
,
162 0, 0, sysctl_emergency_enable
, "I", "Emergency Interrupt Poll Enable");
164 static int emergency_intr_freq
= 10; /* emergency polling frequency */
165 TUNABLE_INT("kern.emergency_intr_freq", &emergency_intr_freq
);
166 SYSCTL_PROC(_kern
, OID_AUTO
, emergency_intr_freq
, CTLTYPE_INT
| CTLFLAG_RW
,
167 0, 0, sysctl_emergency_freq
, "I", "Emergency Interrupt Poll Frequency");
170 * Sysctl support routines
173 sysctl_emergency_enable(SYSCTL_HANDLER_ARGS
)
175 int error
, enabled
, cpuid
, freq
;
177 enabled
= emergency_intr_enable
;
178 error
= sysctl_handle_int(oidp
, &enabled
, 0, req
);
179 if (error
|| req
->newptr
== NULL
)
181 emergency_intr_enable
= enabled
;
182 if (emergency_intr_enable
)
183 freq
= emergency_intr_freq
;
187 for (cpuid
= 0; cpuid
< ncpus
; ++cpuid
)
188 systimer_adjust_periodic(&emergency_intr_timer
[cpuid
], freq
);
193 sysctl_emergency_freq(SYSCTL_HANDLER_ARGS
)
195 int error
, phz
, cpuid
, freq
;
197 phz
= emergency_intr_freq
;
198 error
= sysctl_handle_int(oidp
, &phz
, 0, req
);
199 if (error
|| req
->newptr
== NULL
)
203 else if (phz
> EMERGENCY_INTR_POLLING_FREQ_MAX
)
204 phz
= EMERGENCY_INTR_POLLING_FREQ_MAX
;
206 emergency_intr_freq
= phz
;
207 if (emergency_intr_enable
)
208 freq
= emergency_intr_freq
;
212 for (cpuid
= 0; cpuid
< ncpus
; ++cpuid
)
213 systimer_adjust_periodic(&emergency_intr_timer
[cpuid
], freq
);
218 * Register an SWI or INTerrupt handler.
221 register_swi(int intr
, inthand2_t
*handler
, void *arg
, const char *name
,
222 struct lwkt_serialize
*serializer
, int cpuid
)
224 if (intr
< FIRST_SOFTINT
|| intr
>= MAX_INTS
)
225 panic("register_swi: bad intr %d", intr
);
228 cpuid
= intr
% ncpus
;
229 return(register_int(intr
, handler
, arg
, name
, serializer
, 0, cpuid
));
233 register_swi_mp(int intr
, inthand2_t
*handler
, void *arg
, const char *name
,
234 struct lwkt_serialize
*serializer
, int cpuid
)
236 if (intr
< FIRST_SOFTINT
|| intr
>= MAX_INTS
)
237 panic("register_swi: bad intr %d", intr
);
240 cpuid
= intr
% ncpus
;
241 return(register_int(intr
, handler
, arg
, name
, serializer
,
242 INTR_MPSAFE
, cpuid
));
246 register_int(int intr
, inthand2_t
*handler
, void *arg
, const char *name
,
247 struct lwkt_serialize
*serializer
, int intr_flags
, int cpuid
)
249 struct intr_info
*info
;
250 struct intrec
**list
;
254 KKASSERT(cpuid
>= 0 && cpuid
< ncpus
);
256 if (intr
< 0 || intr
>= MAX_INTS
)
257 panic("register_int: bad intr %d", intr
);
260 info
= &intr_block
->ary
[cpuid
][intr
];
262 int_moveto_destcpu(&orig_cpuid
, cpuid
);
265 * This intr has been registered as exclusive one, so
268 if (info
->i_flags
& INTR_EXCL
)
272 * This intr has been registered as shared one, so it
273 * can't be used for exclusive handler.
275 list
= &info
->i_reclist
;
276 if ((intr_flags
& INTR_EXCL
) && *list
!= NULL
)
280 * Construct an interrupt handler record
282 rec
= kmalloc(sizeof(struct intrec
), M_DEVBUF
, M_INTWAIT
);
283 rec
->name
= kmalloc(strlen(name
) + 1, M_DEVBUF
, M_INTWAIT
);
284 strcpy(rec
->name
, name
);
287 rec
->handler
= handler
;
290 rec
->intr_flags
= intr_flags
;
292 rec
->serializer
= serializer
;
295 * Create an emergency polling thread and set up a systimer to wake
296 * it up. objcache isn't operational yet so use kmalloc.
298 * objcache may not be operational yet, use kmalloc().
300 if (emergency_intr_thread
[cpuid
] == NULL
) {
301 emergency_intr_thread
[cpuid
] = kmalloc(sizeof(struct thread
), M_DEVBUF
,
303 lwkt_create(ithread_emergency
, NULL
, NULL
,
304 emergency_intr_thread
[cpuid
],
305 TDF_NOSTART
| TDF_INTTHREAD
, cpuid
, "ithreadE %d",
307 systimer_init_periodic_nq(&emergency_intr_timer
[cpuid
],
308 emergency_intr_timer_callback
,
309 emergency_intr_thread
[cpuid
],
310 (emergency_intr_enable
? emergency_intr_freq
: 1));
314 * Create an interrupt thread if necessary, leave it in an unscheduled
317 if (info
->i_state
== ISTATE_NOTHREAD
) {
318 info
->i_state
= ISTATE_NORMAL
;
319 info
->i_thread
= kmalloc(sizeof(struct thread
), M_DEVBUF
,
321 lwkt_create(ithread_handler
, (void *)(intptr_t)intr
, NULL
,
322 info
->i_thread
, TDF_NOSTART
| TDF_INTTHREAD
, cpuid
,
323 "ithread%d %d", intr
, cpuid
);
324 if (intr
>= FIRST_SOFTINT
)
325 lwkt_setpri(info
->i_thread
, TDPRI_SOFT_NORM
);
327 lwkt_setpri(info
->i_thread
, TDPRI_INT_MED
);
328 info
->i_thread
->td_preemptable
= lwkt_preempt
;
332 * Keep track of how many fast and slow interrupts we have.
333 * Set i_mplock_required if any handler in the chain requires
334 * the MP lock to operate.
336 if ((intr_flags
& INTR_MPSAFE
) == 0) {
337 info
->i_mplock_required
= 1;
338 kprintf("interrupt uses mplock: %s\n", name
);
340 if (intr_flags
& INTR_CLOCK
)
345 info
->i_flags
|= (intr_flags
& INTR_EXCL
);
346 if (info
->i_slow
+ info
->i_fast
== 1 && (intr_flags
& INTR_HIFREQ
)) {
348 * Allow high frequency interrupt, if this intr is not
351 info
->i_flags
|= INTR_HIFREQ
;
353 info
->i_flags
&= ~INTR_HIFREQ
;
357 * Enable random number generation keying off of this interrupt.
359 if ((intr_flags
& INTR_NOENTROPY
) == 0 && info
->i_random
.sc_enabled
== 0) {
360 info
->i_random
.sc_enabled
= 1;
361 info
->i_random
.sc_intr
= intr
;
365 * Add the record to the interrupt list.
368 while (*list
!= NULL
)
369 list
= &(*list
)->next
;
374 * Update max_installed_hard_intr to make the emergency intr poll
375 * a bit more efficient.
377 if (intr
< FIRST_SOFTINT
) {
378 if (max_installed_hard_intr
[cpuid
] <= intr
)
379 max_installed_hard_intr
[cpuid
] = intr
+ 1;
382 if (intr
>= FIRST_SOFTINT
)
383 swi_info_ary
[intr
- FIRST_SOFTINT
] = info
;
386 * Setup the machine level interrupt vector
388 if (intr
< FIRST_SOFTINT
&& info
->i_slow
+ info
->i_fast
== 1)
389 machintr_intr_setup(intr
, intr_flags
);
392 int_moveto_origcpu(orig_cpuid
, cpuid
);
397 unregister_swi(void *id
, int intr
, int cpuid
)
400 cpuid
= intr
% ncpus
;
402 unregister_int(id
, cpuid
);
406 unregister_int(void *id
, int cpuid
)
408 struct intr_info
*info
;
409 struct intrec
**list
;
411 int intr
, orig_cpuid
;
413 KKASSERT(cpuid
>= 0 && cpuid
< ncpus
);
415 intr
= ((intrec_t
)id
)->intr
;
417 if (intr
< 0 || intr
>= MAX_INTS
)
418 panic("register_int: bad intr %d", intr
);
420 info
= &intr_block
->ary
[cpuid
][intr
];
422 int_moveto_destcpu(&orig_cpuid
, cpuid
);
425 * Remove the interrupt descriptor, adjust the descriptor count,
426 * and teardown the machine level vector if this was the last interrupt.
429 list
= &info
->i_reclist
;
430 while ((rec
= *list
) != NULL
) {
439 if (rec
->intr_flags
& INTR_CLOCK
)
443 if (intr
< FIRST_SOFTINT
&& info
->i_fast
+ info
->i_slow
== 0)
444 machintr_intr_teardown(intr
);
447 * Clear i_mplock_required if no handlers in the chain require the
450 for (rec0
= info
->i_reclist
; rec0
; rec0
= rec0
->next
) {
451 if ((rec0
->intr_flags
& INTR_MPSAFE
) == 0)
455 info
->i_mplock_required
= 0;
458 if (info
->i_reclist
== NULL
) {
460 if (intr
>= FIRST_SOFTINT
)
461 swi_info_ary
[intr
- FIRST_SOFTINT
] = NULL
;
462 } else if (info
->i_fast
+ info
->i_slow
== 1 &&
463 (info
->i_reclist
->intr_flags
& INTR_HIFREQ
)) {
464 /* Unshared high frequency interrupt. */
465 info
->i_flags
|= INTR_HIFREQ
;
470 int_moveto_origcpu(orig_cpuid
, cpuid
);
476 kfree(rec
->name
, M_DEVBUF
);
477 kfree(rec
, M_DEVBUF
);
479 kprintf("warning: unregister_int: int %d handler for %s not found\n",
480 intr
, ((intrec_t
)id
)->name
);
485 get_interrupt_counter(int intr
, int cpuid
)
487 struct intr_info
*info
;
489 KKASSERT(cpuid
>= 0 && cpuid
< ncpus
);
491 if (intr
< 0 || intr
>= MAX_INTS
)
492 panic("register_int: bad intr %d", intr
);
493 info
= &intr_block
->ary
[cpuid
][intr
];
494 return(info
->i_count
);
498 register_randintr(int intr
)
500 struct intr_info
*info
;
503 if (intr
< 0 || intr
>= MAX_INTS
)
504 panic("register_randintr: bad intr %d", intr
);
506 for (cpuid
= 0; cpuid
< ncpus
; ++cpuid
) {
507 info
= &intr_block
->ary
[cpuid
][intr
];
508 info
->i_random
.sc_intr
= intr
;
509 info
->i_random
.sc_enabled
= 1;
514 unregister_randintr(int intr
)
516 struct intr_info
*info
;
519 if (intr
< 0 || intr
>= MAX_INTS
)
520 panic("register_swi: bad intr %d", intr
);
522 for (cpuid
= 0; cpuid
< ncpus
; ++cpuid
) {
523 info
= &intr_block
->ary
[cpuid
][intr
];
524 info
->i_random
.sc_enabled
= -1;
529 next_registered_randintr(int intr
)
531 struct intr_info
*info
;
533 if (intr
< 0 || intr
>= MAX_INTS
)
534 panic("register_swi: bad intr %d", intr
);
536 while (intr
< MAX_INTS
) {
539 for (cpuid
= 0; cpuid
< ncpus
; ++cpuid
) {
540 info
= &intr_block
->ary
[cpuid
][intr
];
541 if (info
->i_random
.sc_enabled
> 0)
550 * Dispatch an interrupt. If there's nothing to do we have a stray
551 * interrupt and can just return, leaving the interrupt masked.
553 * We need to schedule the interrupt and set its i_running bit. If
554 * we are not on the interrupt thread's cpu we have to send a message
555 * to the correct cpu that will issue the desired action (interlocking
556 * with the interrupt thread's critical section). We do NOT attempt to
557 * reschedule interrupts whos i_running bit is already set because
558 * this would prematurely wakeup a livelock-limited interrupt thread.
560 * i_running is only tested/set on the same cpu as the interrupt thread.
562 * We are NOT in a critical section, which will allow the scheduled
563 * interrupt to preempt us. The MP lock might *NOT* be held here.
566 sched_ithd_remote(void *arg
)
568 sched_ithd_intern(arg
);
572 sched_ithd_intern(struct intr_info
*info
)
575 if (info
->i_state
!= ISTATE_NOTHREAD
) {
576 if (info
->i_reclist
== NULL
) {
577 report_stray_interrupt(info
, "sched_ithd");
579 if (info
->i_thread
->td_gd
== mycpu
) {
580 if (info
->i_running
== 0) {
582 if (info
->i_state
!= ISTATE_LIVELOCKED
)
583 lwkt_schedule(info
->i_thread
); /* MIGHT PREEMPT */
586 lwkt_send_ipiq(info
->i_thread
->td_gd
, sched_ithd_remote
, info
);
590 report_stray_interrupt(info
, "sched_ithd");
595 sched_ithd_soft(int intr
)
597 struct intr_info
*info
;
599 KKASSERT(intr
>= FIRST_SOFTINT
&& intr
< MAX_INTS
);
601 info
= swi_info_ary
[intr
- FIRST_SOFTINT
];
603 sched_ithd_intern(info
);
605 kprintf("unregistered softint %d got scheduled on cpu%d\n",
611 sched_ithd_hard(int intr
)
613 KKASSERT(intr
>= 0 && intr
< MAX_HARDINTS
);
614 sched_ithd_intern(&intr_block
->ary
[mycpuid
][intr
]);
617 #ifdef _KERNEL_VIRTUAL
620 sched_ithd_hard_virtual(int intr
)
622 KKASSERT(intr
>= 0 && intr
< MAX_HARDINTS
);
623 sched_ithd_intern(&intr_block
->ary
[0][intr
]);
627 register_int_virtual(int intr
, inthand2_t
*handler
, void *arg
, const char *name
,
628 struct lwkt_serialize
*serializer
, int intr_flags
)
630 return register_int(intr
, handler
, arg
, name
, serializer
, intr_flags
, 0);
634 unregister_int_virtual(void *id
)
636 unregister_int(id
, 0);
639 #endif /* _KERN_VIRTUAL */
642 report_stray_interrupt(struct intr_info
*info
, const char *func
)
644 ++info
->i_straycount
;
645 if (info
->i_straycount
< 10) {
646 if (info
->i_errorticks
== ticks
)
648 info
->i_errorticks
= ticks
;
649 kprintf("%s: stray interrupt %d on cpu%d\n",
650 func
, info
->i_intr
, mycpuid
);
651 } else if (info
->i_straycount
== 10) {
652 kprintf("%s: %ld stray interrupts %d on cpu%d - "
653 "there will be no further reports\n", func
,
654 info
->i_straycount
, info
->i_intr
, mycpuid
);
659 * This is run from a periodic SYSTIMER (and thus must be MP safe, the BGL
660 * might not be held).
663 ithread_livelock_wakeup(systimer_t st
, int in_ipi __unused
,
664 struct intrframe
*frame __unused
)
666 struct intr_info
*info
;
668 info
= &intr_block
->ary
[mycpuid
][(int)(intptr_t)st
->data
];
669 if (info
->i_state
!= ISTATE_NOTHREAD
)
670 lwkt_schedule(info
->i_thread
);
674 * Schedule ithread within fast intr handler
676 * XXX Protect sched_ithd_hard() call with gd_intr_nesting_level?
677 * Interrupts aren't enabled, but still...
680 ithread_fast_sched(int intr
, thread_t td
)
685 * We are already in critical section, exit it now to
689 sched_ithd_hard(intr
);
690 crit_enter_quick(td
);
696 * This function is called directly from the ICU or APIC vector code assembly
697 * to process an interrupt. The critical section and interrupt deferral
698 * checks have already been done but the function is entered WITHOUT
699 * a critical section held. The BGL may or may not be held.
701 * Must return non-zero if we do not want the vector code to re-enable
702 * the interrupt (which we don't if we have to schedule the interrupt)
704 int ithread_fast_handler(struct intrframe
*frame
);
707 ithread_fast_handler(struct intrframe
*frame
)
710 struct intr_info
*info
;
711 struct intrec
**list
;
714 TD_INVARIANTS_DECLARE
;
719 intr
= frame
->if_vec
;
723 /* We must be in critical section. */
724 KKASSERT(td
->td_critcount
);
726 info
= &intr_block
->ary
[mycpuid
][intr
];
729 * If we are not processing any FAST interrupts, just schedule the thing.
731 if (info
->i_fast
== 0) {
733 ithread_fast_sched(intr
, td
);
738 * This should not normally occur since interrupts ought to be
739 * masked if the ithread has been scheduled or is running.
745 * Bump the interrupt nesting level to process any FAST interrupts.
746 * Obtain the MP lock as necessary. If the MP lock cannot be obtained,
747 * schedule the interrupt thread to deal with the issue instead.
749 * To reduce overhead, just leave the MP lock held once it has been
752 ++gd
->gd_intr_nesting_level
;
754 must_schedule
= info
->i_slow
;
757 TD_INVARIANTS_GET(td
);
758 list
= &info
->i_reclist
;
760 for (rec
= *list
; rec
; rec
= nrec
) {
761 /* rec may be invalid after call */
764 if (rec
->intr_flags
& INTR_CLOCK
) {
765 if ((rec
->intr_flags
& INTR_MPSAFE
) == 0 && got_mplock
== 0) {
766 if (try_mplock() == 0) {
767 /* Couldn't get the MP lock; just schedule it. */
773 if (rec
->serializer
) {
774 must_schedule
+= lwkt_serialize_handler_try(
775 rec
->serializer
, rec
->handler
,
776 rec
->argument
, frame
);
778 rec
->handler(rec
->argument
, frame
);
780 TD_INVARIANTS_TEST(td
, rec
->name
);
787 --gd
->gd_intr_nesting_level
;
792 * If we had a problem, or mixed fast and slow interrupt handlers are
793 * registered, schedule the ithread to catch the missed records (it
794 * will just re-run all of them). A return value of 0 indicates that
795 * all handlers have been run and the interrupt can be re-enabled, and
796 * a non-zero return indicates that the interrupt thread controls
799 if (must_schedule
> 0)
800 ithread_fast_sched(intr
, td
);
801 else if (must_schedule
== 0)
803 return(must_schedule
);
807 * Interrupt threads run this as their main loop.
809 * The handler begins execution outside a critical section and no MP lock.
811 * The i_running state starts at 0. When an interrupt occurs, the hardware
812 * interrupt is disabled and sched_ithd_hard(). The HW interrupt remains
813 * disabled until all routines have run. We then call machintr_intr_enable()
814 * to reenable the HW interrupt and deschedule us until the next interrupt.
816 * We are responsible for atomically checking i_running. i_running for our
817 * irq is only set in the context of our cpu, so a critical section is a
818 * sufficient interlock.
820 #define LIVELOCK_TIMEFRAME(freq) ((freq) >> 2) /* 1/4 second */
823 ithread_handler(void *arg
)
825 struct intr_info
*info
;
828 int intr
, cpuid
= mycpuid
;
830 struct intrec
**list
;
833 struct systimer ill_timer
; /* enforced freq. timer */
834 u_int ill_count
; /* interrupt livelock counter */
835 int upper_limit
; /* interrupt livelock upper limit */
836 TD_INVARIANTS_DECLARE
;
839 intr
= (int)(intptr_t)arg
;
840 info
= &intr_block
->ary
[cpuid
][intr
];
841 list
= &info
->i_reclist
;
844 * The loop must be entered with one critical section held. The thread
845 * does not hold the mplock on startup.
848 lseconds
= gd
->gd_time_seconds
;
854 * The chain is only considered MPSAFE if all its interrupt handlers
855 * are MPSAFE. However, if intr_mpsafe has been turned off we
856 * always operate with the BGL.
858 if (info
->i_mplock_required
!= mpheld
) {
859 if (info
->i_mplock_required
) {
860 KKASSERT(mpheld
== 0);
864 KKASSERT(mpheld
!= 0);
870 TD_INVARIANTS_GET(gd
->gd_curthread
);
873 * If an interrupt is pending, clear i_running and execute the
874 * handlers. Note that certain types of interrupts can re-trigger
875 * and set i_running again.
877 * Each handler is run in a critical section. Note that we run both
878 * FAST and SLOW designated service routines.
880 if (info
->i_running
) {
885 report_stray_interrupt(info
, "ithread_handler");
887 for (rec
= *list
; rec
; rec
= nrec
) {
888 /* rec may be invalid after call */
890 if (rec
->handler
== NULL
) {
891 kprintf("NULL HANDLER %s\n", rec
->name
);
893 if (rec
->serializer
) {
894 lwkt_serialize_handler_call(rec
->serializer
, rec
->handler
,
895 rec
->argument
, NULL
);
897 rec
->handler(rec
->argument
, NULL
);
899 TD_INVARIANTS_TEST(gd
->gd_curthread
, rec
->name
);
904 * This is our interrupt hook to add rate randomness to the random
907 if (info
->i_random
.sc_enabled
> 0)
908 add_interrupt_randomness(intr
);
911 * Unmask the interrupt to allow it to trigger again. This only
912 * applies to certain types of interrupts (typ level interrupts).
913 * This can result in the interrupt retriggering, but the retrigger
914 * will not be processed until we cycle our critical section.
916 * Only unmask interrupts while handlers are installed. It is
917 * possible to hit a situation where no handlers are installed
918 * due to a device driver livelocking and then tearing down its
919 * interrupt on close (the parallel bus being a good example).
921 if (intr
< FIRST_SOFTINT
&& *list
)
922 machintr_intr_enable(intr
);
925 * Do a quick exit/enter to catch any higher-priority interrupt
926 * sources, such as the statclock, so thread time accounting
927 * will still work. This may also cause an interrupt to re-trigger.
933 * LIVELOCK STATE MACHINE
935 switch(info
->i_state
) {
938 * Reset the count each second.
940 if (lseconds
!= gd
->gd_time_seconds
) {
941 lseconds
= gd
->gd_time_seconds
;
946 * If we did not exceed the frequency limit, we are done.
947 * If the interrupt has not retriggered we deschedule ourselves.
949 if (info
->i_flags
& INTR_HIFREQ
)
950 upper_limit
= livelock_limit_hi
;
952 upper_limit
= livelock_limit
;
953 if (ill_count
<= upper_limit
) {
954 if (info
->i_running
== 0) {
955 lwkt_deschedule_self(gd
->gd_curthread
);
962 * Otherwise we are livelocked. Set up a periodic systimer
963 * to wake the thread up at the limit frequency.
965 kprintf("intr %d on cpu%d at %d/%d hz, livelocked limit engaged!\n",
966 intr
, cpuid
, ill_count
, upper_limit
);
967 info
->i_state
= ISTATE_LIVELOCKED
;
968 if ((use_limit
= upper_limit
) < 100)
970 else if (use_limit
> 500000)
972 systimer_init_periodic_nq(&ill_timer
, ithread_livelock_wakeup
,
973 (void *)(intptr_t)intr
, use_limit
);
975 case ISTATE_LIVELOCKED
:
977 * Wait for our periodic timer to go off. Since the interrupt
978 * has re-armed it can still set i_running, but it will not
979 * reschedule us while we are in a livelocked state.
981 lwkt_deschedule_self(gd
->gd_curthread
);
985 * Check once a second to see if the livelock condition no
988 if (lseconds
!= gd
->gd_time_seconds
) {
989 lseconds
= gd
->gd_time_seconds
;
990 if (ill_count
< livelock_lowater
) {
991 info
->i_state
= ISTATE_NORMAL
;
992 systimer_del(&ill_timer
);
993 kprintf("intr %d on cpu%d at %d/%d hz, livelock removed\n",
994 intr
, cpuid
, ill_count
, livelock_lowater
);
995 } else if (livelock_debug
== intr
||
996 (bootverbose
&& cold
)) {
997 kprintf("intr %d on cpu%d at %d/%d hz, in livelock\n",
998 intr
, cpuid
, ill_count
, livelock_lowater
);
1009 * Emergency interrupt polling thread. The thread begins execution
1010 * outside a critical section with the BGL held.
1012 * If emergency interrupt polling is enabled, this thread will
1013 * execute all system interrupts not marked INTR_NOPOLL at the
1014 * specified polling frequency.
1016 * WARNING! This thread runs *ALL* interrupt service routines that
1017 * are not marked INTR_NOPOLL, which basically means everything except
1018 * the 8254 clock interrupt and the ATA interrupt. It has very high
1019 * overhead and should only be used in situations where the machine
1020 * cannot otherwise be made to work. Due to the severe performance
1021 * degredation, it should not be enabled on production machines.
1024 ithread_emergency(void *arg __unused
)
1026 globaldata_t gd
= mycpu
;
1027 struct intr_info
*info
;
1029 int intr
, cpuid
= mycpuid
;
1030 TD_INVARIANTS_DECLARE
;
1034 TD_INVARIANTS_GET(gd
->gd_curthread
);
1037 for (intr
= 0; intr
< max_installed_hard_intr
[cpuid
]; ++intr
) {
1038 info
= &intr_block
->ary
[cpuid
][intr
];
1039 for (rec
= info
->i_reclist
; rec
; rec
= nrec
) {
1040 /* rec may be invalid after call */
1042 if ((rec
->intr_flags
& INTR_NOPOLL
) == 0) {
1043 if (rec
->serializer
) {
1044 lwkt_serialize_handler_try(rec
->serializer
,
1045 rec
->handler
, rec
->argument
, NULL
);
1047 rec
->handler(rec
->argument
, NULL
);
1049 TD_INVARIANTS_TEST(gd
->gd_curthread
, rec
->name
);
1053 lwkt_deschedule_self(gd
->gd_curthread
);
1060 * Systimer callback - schedule the emergency interrupt poll thread
1061 * if emergency polling is enabled.
1065 emergency_intr_timer_callback(systimer_t info
, int in_ipi __unused
,
1066 struct intrframe
*frame __unused
)
1068 if (emergency_intr_enable
)
1069 lwkt_schedule(info
->data
);
1073 * Sysctls used by systat and others: hw.intrnames and hw.intrcnt.
1074 * The data for this machine dependent, and the declarations are in machine
1075 * dependent code. The layout of intrnames and intrcnt however is machine
1078 * We do not know the length of intrcnt and intrnames at compile time, so
1079 * calculate things at run time.
1083 sysctl_intrnames(SYSCTL_HANDLER_ARGS
)
1085 struct intr_info
*info
;
1092 for (cpuid
= 0; cpuid
< ncpus
; ++cpuid
) {
1093 for (intr
= 0; error
== 0 && intr
< MAX_INTS
; ++intr
) {
1094 info
= &intr_block
->ary
[cpuid
][intr
];
1098 for (rec
= info
->i_reclist
; rec
; rec
= rec
->next
) {
1099 ksnprintf(buf
+ len
, sizeof(buf
) - len
, "%s%s",
1100 (len
? "/" : ""), rec
->name
);
1101 len
+= strlen(buf
+ len
);
1104 ksnprintf(buf
, sizeof(buf
), "irq%d", intr
);
1107 error
= SYSCTL_OUT(req
, buf
, len
+ 1);
1113 SYSCTL_PROC(_hw
, OID_AUTO
, intrnames
, CTLTYPE_OPAQUE
| CTLFLAG_RD
,
1114 NULL
, 0, sysctl_intrnames
, "", "Interrupt Names");
1117 sysctl_intrcnt_all(SYSCTL_HANDLER_ARGS
)
1119 struct intr_info
*info
;
1123 for (cpuid
= 0; cpuid
< ncpus
; ++cpuid
) {
1124 for (intr
= 0; intr
< MAX_INTS
; ++intr
) {
1125 info
= &intr_block
->ary
[cpuid
][intr
];
1127 error
= SYSCTL_OUT(req
, &info
->i_count
, sizeof(info
->i_count
));
1136 SYSCTL_PROC(_hw
, OID_AUTO
, intrcnt_all
, CTLTYPE_OPAQUE
| CTLFLAG_RD
,
1137 NULL
, 0, sysctl_intrcnt_all
, "", "Interrupt Counts");
1139 SYSCTL_PROC(_hw
, OID_AUTO
, intrcnt
, CTLTYPE_OPAQUE
| CTLFLAG_RD
,
1140 NULL
, 0, sysctl_intrcnt_all
, "", "Interrupt Counts");
1143 int_moveto_destcpu(int *orig_cpuid0
, int cpuid
)
1145 int orig_cpuid
= mycpuid
;
1147 if (cpuid
!= orig_cpuid
)
1148 lwkt_migratecpu(cpuid
);
1150 *orig_cpuid0
= orig_cpuid
;
1154 int_moveto_origcpu(int orig_cpuid
, int cpuid
)
1156 if (cpuid
!= orig_cpuid
)
1157 lwkt_migratecpu(orig_cpuid
);
1161 intr_init(void *dummy __unused
)
1165 kprintf("Initialize MI interrupts\n");
1167 intr_block
= kmalloc(sizeof(*intr_block
), M_INTRMNG
,
1168 M_INTWAIT
| M_ZERO
);
1170 for (cpuid
= 0; cpuid
< ncpus
; ++cpuid
) {
1173 for (intr
= 0; intr
< MAX_INTS
; ++intr
) {
1174 struct intr_info
*info
= &intr_block
->ary
[cpuid
][intr
];
1176 info
->i_cpuid
= cpuid
;
1177 info
->i_intr
= intr
;
1181 SYSINIT(intr_init
, SI_BOOT2_FINISH_PIC
, SI_ORDER_ANY
, intr_init
, NULL
);