intr: Force unshareble interrupt setting
[dragonfly.git] / sys / kern / kern_intr.c
blob1664f819c6a16c8a43c3f06f28f02c7d0f61fc27
1 /*
2 * Copyright (c) 2003 Matthew Dillon <dillon@backplane.com> All rights reserved.
3 * Copyright (c) 1997, Stefan Esser <se@freebsd.org> All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
10 * disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
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>
36 #include <sys/proc.h>
37 #include <sys/random.h>
38 #include <sys/serialize.h>
39 #include <sys/interrupt.h>
40 #include <sys/bus.h>
41 #include <sys/machintr.h>
43 #include <machine/frame.h>
45 #include <sys/thread2.h>
46 #include <sys/mplock2.h>
48 struct intr_info;
50 typedef struct intrec {
51 struct intrec *next;
52 struct intr_info *info;
53 inthand2_t *handler;
54 void *argument;
55 char *name;
56 int intr;
57 int intr_flags;
58 struct lwkt_serialize *serializer;
59 } *intrec_t;
61 struct intr_info {
62 intrec_t i_reclist;
63 struct thread *i_thread; /* don't embed struct thread */
64 struct random_softc i_random;
65 long i_count; /* interrupts dispatched */
66 int i_running;
67 short i_mplock_required;
68 short i_flags;
69 int i_fast;
70 int i_slow;
71 int i_state;
72 int i_errorticks;
73 unsigned long i_straycount;
74 int i_cpuid;
75 int i_intr;
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.
96 #ifdef INVARIANTS
98 #define TD_INVARIANTS_DECLARE \
99 int spincount; \
100 lwkt_tokref_t curstop
102 #define TD_INVARIANTS_GET(td) \
103 do { \
104 spincount = (td)->td_gd->gd_spinlocks; \
105 curstop = (td)->td_toks_stop; \
106 } while(0)
108 #define TD_INVARIANTS_TEST(td, name) \
109 do { \
110 KASSERT(spincount == (td)->td_gd->gd_spinlocks, \
111 ("spincount mismatch after interrupt handler %s", \
112 name)); \
113 KASSERT(curstop == (td)->td_toks_stop, \
114 ("token count mismatch after interrupt handler %s", \
115 name)); \
116 } while(0)
118 #else
120 /* !INVARIANTS */
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_lowater = 20000;
147 static int livelock_debug = -1;
148 SYSCTL_INT(_kern, OID_AUTO, livelock_limit,
149 CTLFLAG_RW, &livelock_limit, 0, "Livelock interrupt rate limit");
150 SYSCTL_INT(_kern, OID_AUTO, livelock_lowater,
151 CTLFLAG_RW, &livelock_lowater, 0, "Livelock low-water mark restore");
152 SYSCTL_INT(_kern, OID_AUTO, livelock_debug,
153 CTLFLAG_RW, &livelock_debug, 0, "Livelock debug intr#");
155 static int emergency_intr_enable = 0; /* emergency interrupt polling */
156 TUNABLE_INT("kern.emergency_intr_enable", &emergency_intr_enable);
157 SYSCTL_PROC(_kern, OID_AUTO, emergency_intr_enable, CTLTYPE_INT | CTLFLAG_RW,
158 0, 0, sysctl_emergency_enable, "I", "Emergency Interrupt Poll Enable");
160 static int emergency_intr_freq = 10; /* emergency polling frequency */
161 TUNABLE_INT("kern.emergency_intr_freq", &emergency_intr_freq);
162 SYSCTL_PROC(_kern, OID_AUTO, emergency_intr_freq, CTLTYPE_INT | CTLFLAG_RW,
163 0, 0, sysctl_emergency_freq, "I", "Emergency Interrupt Poll Frequency");
166 * Sysctl support routines
168 static int
169 sysctl_emergency_enable(SYSCTL_HANDLER_ARGS)
171 int error, enabled, cpuid, freq;
173 enabled = emergency_intr_enable;
174 error = sysctl_handle_int(oidp, &enabled, 0, req);
175 if (error || req->newptr == NULL)
176 return error;
177 emergency_intr_enable = enabled;
178 if (emergency_intr_enable)
179 freq = emergency_intr_freq;
180 else
181 freq = 1;
183 for (cpuid = 0; cpuid < ncpus; ++cpuid)
184 systimer_adjust_periodic(&emergency_intr_timer[cpuid], freq);
185 return 0;
188 static int
189 sysctl_emergency_freq(SYSCTL_HANDLER_ARGS)
191 int error, phz, cpuid, freq;
193 phz = emergency_intr_freq;
194 error = sysctl_handle_int(oidp, &phz, 0, req);
195 if (error || req->newptr == NULL)
196 return error;
197 if (phz <= 0)
198 return EINVAL;
199 else if (phz > EMERGENCY_INTR_POLLING_FREQ_MAX)
200 phz = EMERGENCY_INTR_POLLING_FREQ_MAX;
202 emergency_intr_freq = phz;
203 if (emergency_intr_enable)
204 freq = emergency_intr_freq;
205 else
206 freq = 1;
208 for (cpuid = 0; cpuid < ncpus; ++cpuid)
209 systimer_adjust_periodic(&emergency_intr_timer[cpuid], freq);
210 return 0;
214 * Register an SWI or INTerrupt handler.
216 void *
217 register_swi(int intr, inthand2_t *handler, void *arg, const char *name,
218 struct lwkt_serialize *serializer, int cpuid)
220 if (intr < FIRST_SOFTINT || intr >= MAX_INTS)
221 panic("register_swi: bad intr %d", intr);
223 if (cpuid < 0)
224 cpuid = intr % ncpus;
225 return(register_int(intr, handler, arg, name, serializer, 0, cpuid));
228 void *
229 register_swi_mp(int intr, inthand2_t *handler, void *arg, const char *name,
230 struct lwkt_serialize *serializer, int cpuid)
232 if (intr < FIRST_SOFTINT || intr >= MAX_INTS)
233 panic("register_swi: bad intr %d", intr);
235 if (cpuid < 0)
236 cpuid = intr % ncpus;
237 return(register_int(intr, handler, arg, name, serializer,
238 INTR_MPSAFE, cpuid));
241 void *
242 register_int(int intr, inthand2_t *handler, void *arg, const char *name,
243 struct lwkt_serialize *serializer, int intr_flags, int cpuid)
245 struct intr_info *info;
246 struct intrec **list;
247 intrec_t rec = NULL;
248 int orig_cpuid;
250 KKASSERT(cpuid >= 0 && cpuid < ncpus);
252 if (intr < 0 || intr >= MAX_INTS)
253 panic("register_int: bad intr %d", intr);
254 if (name == NULL)
255 name = "???";
256 info = &intr_block->ary[cpuid][intr];
258 int_moveto_destcpu(&orig_cpuid, cpuid);
261 * This intr has been registered as exclusive one, so
262 * it can't shared.
264 if (info->i_flags & INTR_EXCL)
265 goto done;
268 * This intr has been registered as shared one, so it
269 * can't be used for exclusive handler.
271 list = &info->i_reclist;
272 if ((intr_flags & INTR_EXCL) && *list != NULL)
273 goto done;
276 * Construct an interrupt handler record
278 rec = kmalloc(sizeof(struct intrec), M_DEVBUF, M_INTWAIT);
279 rec->name = kmalloc(strlen(name) + 1, M_DEVBUF, M_INTWAIT);
280 strcpy(rec->name, name);
282 rec->info = info;
283 rec->handler = handler;
284 rec->argument = arg;
285 rec->intr = intr;
286 rec->intr_flags = intr_flags;
287 rec->next = NULL;
288 rec->serializer = serializer;
291 * Create an emergency polling thread and set up a systimer to wake
292 * it up. objcache isn't operational yet so use kmalloc.
294 * objcache may not be operational yet, use kmalloc().
296 if (emergency_intr_thread[cpuid] == NULL) {
297 emergency_intr_thread[cpuid] = kmalloc(sizeof(struct thread), M_DEVBUF,
298 M_INTWAIT | M_ZERO);
299 lwkt_create(ithread_emergency, NULL, NULL,
300 emergency_intr_thread[cpuid],
301 TDF_NOSTART | TDF_INTTHREAD, cpuid, "ithreadE %d",
302 cpuid);
303 systimer_init_periodic_nq(&emergency_intr_timer[cpuid],
304 emergency_intr_timer_callback,
305 emergency_intr_thread[cpuid],
306 (emergency_intr_enable ? emergency_intr_freq : 1));
310 * Create an interrupt thread if necessary, leave it in an unscheduled
311 * state.
313 if (info->i_state == ISTATE_NOTHREAD) {
314 info->i_state = ISTATE_NORMAL;
315 info->i_thread = kmalloc(sizeof(struct thread), M_DEVBUF,
316 M_INTWAIT | M_ZERO);
317 lwkt_create(ithread_handler, (void *)(intptr_t)intr, NULL,
318 info->i_thread, TDF_NOSTART | TDF_INTTHREAD, cpuid,
319 "ithread%d %d", intr, cpuid);
320 if (intr >= FIRST_SOFTINT)
321 lwkt_setpri(info->i_thread, TDPRI_SOFT_NORM);
322 else
323 lwkt_setpri(info->i_thread, TDPRI_INT_MED);
324 info->i_thread->td_preemptable = lwkt_preempt;
328 * Keep track of how many fast and slow interrupts we have.
329 * Set i_mplock_required if any handler in the chain requires
330 * the MP lock to operate.
332 if ((intr_flags & INTR_MPSAFE) == 0)
333 info->i_mplock_required = 1;
334 if (intr_flags & INTR_CLOCK)
335 ++info->i_fast;
336 else
337 ++info->i_slow;
338 info->i_flags |= (intr_flags & INTR_EXCL);
341 * Enable random number generation keying off of this interrupt.
343 if ((intr_flags & INTR_NOENTROPY) == 0 && info->i_random.sc_enabled == 0) {
344 info->i_random.sc_enabled = 1;
345 info->i_random.sc_intr = intr;
349 * Add the record to the interrupt list.
351 crit_enter();
352 while (*list != NULL)
353 list = &(*list)->next;
354 *list = rec;
355 crit_exit();
358 * Update max_installed_hard_intr to make the emergency intr poll
359 * a bit more efficient.
361 if (intr < FIRST_SOFTINT) {
362 if (max_installed_hard_intr[cpuid] <= intr)
363 max_installed_hard_intr[cpuid] = intr + 1;
366 if (intr >= FIRST_SOFTINT)
367 swi_info_ary[intr - FIRST_SOFTINT] = info;
370 * Setup the machine level interrupt vector
372 if (intr < FIRST_SOFTINT && info->i_slow + info->i_fast == 1)
373 machintr_intr_setup(intr, intr_flags);
375 done:
376 int_moveto_origcpu(orig_cpuid, cpuid);
377 return(rec);
380 void
381 unregister_swi(void *id, int intr, int cpuid)
383 if (cpuid < 0)
384 cpuid = intr % ncpus;
386 unregister_int(id, cpuid);
389 void
390 unregister_int(void *id, int cpuid)
392 struct intr_info *info;
393 struct intrec **list;
394 intrec_t rec;
395 int intr, orig_cpuid;
397 KKASSERT(cpuid >= 0 && cpuid < ncpus);
399 intr = ((intrec_t)id)->intr;
401 if (intr < 0 || intr >= MAX_INTS)
402 panic("register_int: bad intr %d", intr);
404 info = &intr_block->ary[cpuid][intr];
406 int_moveto_destcpu(&orig_cpuid, cpuid);
409 * Remove the interrupt descriptor, adjust the descriptor count,
410 * and teardown the machine level vector if this was the last interrupt.
412 crit_enter();
413 list = &info->i_reclist;
414 while ((rec = *list) != NULL) {
415 if (rec == id)
416 break;
417 list = &rec->next;
419 if (rec) {
420 intrec_t rec0;
422 *list = rec->next;
423 if (rec->intr_flags & INTR_CLOCK)
424 --info->i_fast;
425 else
426 --info->i_slow;
427 if (intr < FIRST_SOFTINT && info->i_fast + info->i_slow == 0)
428 machintr_intr_teardown(intr);
431 * Clear i_mplock_required if no handlers in the chain require the
432 * MP lock.
434 for (rec0 = info->i_reclist; rec0; rec0 = rec0->next) {
435 if ((rec0->intr_flags & INTR_MPSAFE) == 0)
436 break;
438 if (rec0 == NULL)
439 info->i_mplock_required = 0;
442 if (info->i_reclist == NULL) {
443 info->i_flags = 0;
444 if (intr >= FIRST_SOFTINT)
445 swi_info_ary[intr - FIRST_SOFTINT] = NULL;
448 crit_exit();
450 int_moveto_origcpu(orig_cpuid, cpuid);
453 * Free the record.
455 if (rec != NULL) {
456 kfree(rec->name, M_DEVBUF);
457 kfree(rec, M_DEVBUF);
458 } else {
459 kprintf("warning: unregister_int: int %d handler for %s not found\n",
460 intr, ((intrec_t)id)->name);
464 long
465 get_interrupt_counter(int intr, int cpuid)
467 struct intr_info *info;
469 KKASSERT(cpuid >= 0 && cpuid < ncpus);
471 if (intr < 0 || intr >= MAX_INTS)
472 panic("register_int: bad intr %d", intr);
473 info = &intr_block->ary[cpuid][intr];
474 return(info->i_count);
477 void
478 register_randintr(int intr)
480 struct intr_info *info;
481 int cpuid;
483 if (intr < 0 || intr >= MAX_INTS)
484 panic("register_randintr: bad intr %d", intr);
486 for (cpuid = 0; cpuid < ncpus; ++cpuid) {
487 info = &intr_block->ary[cpuid][intr];
488 info->i_random.sc_intr = intr;
489 info->i_random.sc_enabled = 1;
493 void
494 unregister_randintr(int intr)
496 struct intr_info *info;
497 int cpuid;
499 if (intr < 0 || intr >= MAX_INTS)
500 panic("register_swi: bad intr %d", intr);
502 for (cpuid = 0; cpuid < ncpus; ++cpuid) {
503 info = &intr_block->ary[cpuid][intr];
504 info->i_random.sc_enabled = -1;
509 next_registered_randintr(int intr)
511 struct intr_info *info;
513 if (intr < 0 || intr >= MAX_INTS)
514 panic("register_swi: bad intr %d", intr);
516 while (intr < MAX_INTS) {
517 int cpuid;
519 for (cpuid = 0; cpuid < ncpus; ++cpuid) {
520 info = &intr_block->ary[cpuid][intr];
521 if (info->i_random.sc_enabled > 0)
522 return intr;
524 ++intr;
526 return intr;
530 * Dispatch an interrupt. If there's nothing to do we have a stray
531 * interrupt and can just return, leaving the interrupt masked.
533 * We need to schedule the interrupt and set its i_running bit. If
534 * we are not on the interrupt thread's cpu we have to send a message
535 * to the correct cpu that will issue the desired action (interlocking
536 * with the interrupt thread's critical section). We do NOT attempt to
537 * reschedule interrupts whos i_running bit is already set because
538 * this would prematurely wakeup a livelock-limited interrupt thread.
540 * i_running is only tested/set on the same cpu as the interrupt thread.
542 * We are NOT in a critical section, which will allow the scheduled
543 * interrupt to preempt us. The MP lock might *NOT* be held here.
545 static void
546 sched_ithd_remote(void *arg)
548 sched_ithd_intern(arg);
551 static void
552 sched_ithd_intern(struct intr_info *info)
554 ++info->i_count;
555 if (info->i_state != ISTATE_NOTHREAD) {
556 if (info->i_reclist == NULL) {
557 report_stray_interrupt(info, "sched_ithd");
558 } else {
559 if (info->i_thread->td_gd == mycpu) {
560 if (info->i_running == 0) {
561 info->i_running = 1;
562 if (info->i_state != ISTATE_LIVELOCKED)
563 lwkt_schedule(info->i_thread); /* MIGHT PREEMPT */
565 } else {
566 lwkt_send_ipiq(info->i_thread->td_gd, sched_ithd_remote, info);
569 } else {
570 report_stray_interrupt(info, "sched_ithd");
574 void
575 sched_ithd_soft(int intr)
577 struct intr_info *info;
579 KKASSERT(intr >= FIRST_SOFTINT && intr < MAX_INTS);
581 info = swi_info_ary[intr - FIRST_SOFTINT];
582 if (info != NULL) {
583 sched_ithd_intern(info);
584 } else {
585 kprintf("unregistered softint %d got scheduled on cpu%d\n",
586 intr, mycpuid);
590 void
591 sched_ithd_hard(int intr)
593 KKASSERT(intr >= 0 && intr < MAX_HARDINTS);
594 sched_ithd_intern(&intr_block->ary[mycpuid][intr]);
597 #ifdef _KERNEL_VIRTUAL
599 void
600 sched_ithd_hard_virtual(int intr)
602 KKASSERT(intr >= 0 && intr < MAX_HARDINTS);
603 sched_ithd_intern(&intr_block->ary[0][intr]);
606 void *
607 register_int_virtual(int intr, inthand2_t *handler, void *arg, const char *name,
608 struct lwkt_serialize *serializer, int intr_flags)
610 return register_int(intr, handler, arg, name, serializer, intr_flags, 0);
613 void
614 unregister_int_virtual(void *id)
616 unregister_int(id, 0);
619 #endif /* _KERN_VIRTUAL */
621 static void
622 report_stray_interrupt(struct intr_info *info, const char *func)
624 ++info->i_straycount;
625 if (info->i_straycount < 10) {
626 if (info->i_errorticks == ticks)
627 return;
628 info->i_errorticks = ticks;
629 kprintf("%s: stray interrupt %d on cpu%d\n",
630 func, info->i_intr, mycpuid);
631 } else if (info->i_straycount == 10) {
632 kprintf("%s: %ld stray interrupts %d on cpu%d - "
633 "there will be no further reports\n", func,
634 info->i_straycount, info->i_intr, mycpuid);
639 * This is run from a periodic SYSTIMER (and thus must be MP safe, the BGL
640 * might not be held).
642 static void
643 ithread_livelock_wakeup(systimer_t st, int in_ipi __unused,
644 struct intrframe *frame __unused)
646 struct intr_info *info;
648 info = &intr_block->ary[mycpuid][(int)(intptr_t)st->data];
649 if (info->i_state != ISTATE_NOTHREAD)
650 lwkt_schedule(info->i_thread);
654 * Schedule ithread within fast intr handler
656 * XXX Protect sched_ithd_hard() call with gd_intr_nesting_level?
657 * Interrupts aren't enabled, but still...
659 static __inline void
660 ithread_fast_sched(int intr, thread_t td)
662 ++td->td_nest_count;
665 * We are already in critical section, exit it now to
666 * allow preemption.
668 crit_exit_quick(td);
669 sched_ithd_hard(intr);
670 crit_enter_quick(td);
672 --td->td_nest_count;
676 * This function is called directly from the ICU or APIC vector code assembly
677 * to process an interrupt. The critical section and interrupt deferral
678 * checks have already been done but the function is entered WITHOUT
679 * a critical section held. The BGL may or may not be held.
681 * Must return non-zero if we do not want the vector code to re-enable
682 * the interrupt (which we don't if we have to schedule the interrupt)
684 int ithread_fast_handler(struct intrframe *frame);
687 ithread_fast_handler(struct intrframe *frame)
689 int intr;
690 struct intr_info *info;
691 struct intrec **list;
692 int must_schedule;
693 int got_mplock;
694 TD_INVARIANTS_DECLARE;
695 intrec_t rec, nrec;
696 globaldata_t gd;
697 thread_t td;
699 intr = frame->if_vec;
700 gd = mycpu;
701 td = curthread;
703 /* We must be in critical section. */
704 KKASSERT(td->td_critcount);
706 info = &intr_block->ary[mycpuid][intr];
709 * If we are not processing any FAST interrupts, just schedule the thing.
711 if (info->i_fast == 0) {
712 ++gd->gd_cnt.v_intr;
713 ithread_fast_sched(intr, td);
714 return(1);
718 * This should not normally occur since interrupts ought to be
719 * masked if the ithread has been scheduled or is running.
721 if (info->i_running)
722 return(1);
725 * Bump the interrupt nesting level to process any FAST interrupts.
726 * Obtain the MP lock as necessary. If the MP lock cannot be obtained,
727 * schedule the interrupt thread to deal with the issue instead.
729 * To reduce overhead, just leave the MP lock held once it has been
730 * obtained.
732 ++gd->gd_intr_nesting_level;
733 ++gd->gd_cnt.v_intr;
734 must_schedule = info->i_slow;
735 got_mplock = 0;
737 TD_INVARIANTS_GET(td);
738 list = &info->i_reclist;
740 for (rec = *list; rec; rec = nrec) {
741 /* rec may be invalid after call */
742 nrec = rec->next;
744 if (rec->intr_flags & INTR_CLOCK) {
745 if ((rec->intr_flags & INTR_MPSAFE) == 0 && got_mplock == 0) {
746 if (try_mplock() == 0) {
747 /* Couldn't get the MP lock; just schedule it. */
748 must_schedule = 1;
749 break;
751 got_mplock = 1;
753 if (rec->serializer) {
754 must_schedule += lwkt_serialize_handler_try(
755 rec->serializer, rec->handler,
756 rec->argument, frame);
757 } else {
758 rec->handler(rec->argument, frame);
760 TD_INVARIANTS_TEST(td, rec->name);
765 * Cleanup
767 --gd->gd_intr_nesting_level;
768 if (got_mplock)
769 rel_mplock();
772 * If we had a problem, or mixed fast and slow interrupt handlers are
773 * registered, schedule the ithread to catch the missed records (it
774 * will just re-run all of them). A return value of 0 indicates that
775 * all handlers have been run and the interrupt can be re-enabled, and
776 * a non-zero return indicates that the interrupt thread controls
777 * re-enablement.
779 if (must_schedule > 0)
780 ithread_fast_sched(intr, td);
781 else if (must_schedule == 0)
782 ++info->i_count;
783 return(must_schedule);
787 * Interrupt threads run this as their main loop.
789 * The handler begins execution outside a critical section and no MP lock.
791 * The i_running state starts at 0. When an interrupt occurs, the hardware
792 * interrupt is disabled and sched_ithd_hard(). The HW interrupt remains
793 * disabled until all routines have run. We then call machintr_intr_enable()
794 * to reenable the HW interrupt and deschedule us until the next interrupt.
796 * We are responsible for atomically checking i_running. i_running for our
797 * irq is only set in the context of our cpu, so a critical section is a
798 * sufficient interlock.
800 #define LIVELOCK_TIMEFRAME(freq) ((freq) >> 2) /* 1/4 second */
802 static void
803 ithread_handler(void *arg)
805 struct intr_info *info;
806 int use_limit;
807 uint32_t lseconds;
808 int intr, cpuid = mycpuid;
809 int mpheld;
810 struct intrec **list;
811 intrec_t rec, nrec;
812 globaldata_t gd;
813 struct systimer ill_timer; /* enforced freq. timer */
814 u_int ill_count; /* interrupt livelock counter */
815 TD_INVARIANTS_DECLARE;
817 ill_count = 0;
818 intr = (int)(intptr_t)arg;
819 info = &intr_block->ary[cpuid][intr];
820 list = &info->i_reclist;
823 * The loop must be entered with one critical section held. The thread
824 * does not hold the mplock on startup.
826 gd = mycpu;
827 lseconds = gd->gd_time_seconds;
828 crit_enter_gd(gd);
829 mpheld = 0;
831 for (;;) {
833 * The chain is only considered MPSAFE if all its interrupt handlers
834 * are MPSAFE. However, if intr_mpsafe has been turned off we
835 * always operate with the BGL.
837 if (info->i_mplock_required != mpheld) {
838 if (info->i_mplock_required) {
839 KKASSERT(mpheld == 0);
840 get_mplock();
841 mpheld = 1;
842 } else {
843 KKASSERT(mpheld != 0);
844 rel_mplock();
845 mpheld = 0;
849 TD_INVARIANTS_GET(gd->gd_curthread);
852 * If an interrupt is pending, clear i_running and execute the
853 * handlers. Note that certain types of interrupts can re-trigger
854 * and set i_running again.
856 * Each handler is run in a critical section. Note that we run both
857 * FAST and SLOW designated service routines.
859 if (info->i_running) {
860 ++ill_count;
861 info->i_running = 0;
863 if (*list == NULL)
864 report_stray_interrupt(info, "ithread_handler");
866 for (rec = *list; rec; rec = nrec) {
867 /* rec may be invalid after call */
868 nrec = rec->next;
869 if (rec->serializer) {
870 lwkt_serialize_handler_call(rec->serializer, rec->handler,
871 rec->argument, NULL);
872 } else {
873 rec->handler(rec->argument, NULL);
875 TD_INVARIANTS_TEST(gd->gd_curthread, rec->name);
880 * This is our interrupt hook to add rate randomness to the random
881 * number generator.
883 if (info->i_random.sc_enabled > 0)
884 add_interrupt_randomness(intr);
887 * Unmask the interrupt to allow it to trigger again. This only
888 * applies to certain types of interrupts (typ level interrupts).
889 * This can result in the interrupt retriggering, but the retrigger
890 * will not be processed until we cycle our critical section.
892 * Only unmask interrupts while handlers are installed. It is
893 * possible to hit a situation where no handlers are installed
894 * due to a device driver livelocking and then tearing down its
895 * interrupt on close (the parallel bus being a good example).
897 if (intr < FIRST_SOFTINT && *list)
898 machintr_intr_enable(intr);
901 * Do a quick exit/enter to catch any higher-priority interrupt
902 * sources, such as the statclock, so thread time accounting
903 * will still work. This may also cause an interrupt to re-trigger.
905 crit_exit_gd(gd);
906 crit_enter_gd(gd);
909 * LIVELOCK STATE MACHINE
911 switch(info->i_state) {
912 case ISTATE_NORMAL:
914 * Reset the count each second.
916 if (lseconds != gd->gd_time_seconds) {
917 lseconds = gd->gd_time_seconds;
918 ill_count = 0;
922 * If we did not exceed the frequency limit, we are done.
923 * If the interrupt has not retriggered we deschedule ourselves.
925 if (ill_count <= livelock_limit) {
926 if (info->i_running == 0) {
927 lwkt_deschedule_self(gd->gd_curthread);
928 lwkt_switch();
930 break;
934 * Otherwise we are livelocked. Set up a periodic systimer
935 * to wake the thread up at the limit frequency.
937 kprintf("intr %d on cpu%d at %d/%d hz, livelocked limit engaged!\n",
938 intr, cpuid, ill_count, livelock_limit);
939 info->i_state = ISTATE_LIVELOCKED;
940 if ((use_limit = livelock_limit) < 100)
941 use_limit = 100;
942 else if (use_limit > 500000)
943 use_limit = 500000;
944 systimer_init_periodic_nq(&ill_timer, ithread_livelock_wakeup,
945 (void *)(intptr_t)intr, use_limit);
946 /* fall through */
947 case ISTATE_LIVELOCKED:
949 * Wait for our periodic timer to go off. Since the interrupt
950 * has re-armed it can still set i_running, but it will not
951 * reschedule us while we are in a livelocked state.
953 lwkt_deschedule_self(gd->gd_curthread);
954 lwkt_switch();
957 * Check once a second to see if the livelock condition no
958 * longer applies.
960 if (lseconds != gd->gd_time_seconds) {
961 lseconds = gd->gd_time_seconds;
962 if (ill_count < livelock_lowater) {
963 info->i_state = ISTATE_NORMAL;
964 systimer_del(&ill_timer);
965 kprintf("intr %d on cpu%d at %d/%d hz, livelock removed\n",
966 intr, cpuid, ill_count, livelock_lowater);
967 } else if (livelock_debug == intr ||
968 (bootverbose && cold)) {
969 kprintf("intr %d on cpu%d at %d/%d hz, in livelock\n",
970 intr, cpuid, ill_count, livelock_lowater);
972 ill_count = 0;
974 break;
977 /* NOT REACHED */
981 * Emergency interrupt polling thread. The thread begins execution
982 * outside a critical section with the BGL held.
984 * If emergency interrupt polling is enabled, this thread will
985 * execute all system interrupts not marked INTR_NOPOLL at the
986 * specified polling frequency.
988 * WARNING! This thread runs *ALL* interrupt service routines that
989 * are not marked INTR_NOPOLL, which basically means everything except
990 * the 8254 clock interrupt and the ATA interrupt. It has very high
991 * overhead and should only be used in situations where the machine
992 * cannot otherwise be made to work. Due to the severe performance
993 * degredation, it should not be enabled on production machines.
995 static void
996 ithread_emergency(void *arg __unused)
998 globaldata_t gd = mycpu;
999 struct intr_info *info;
1000 intrec_t rec, nrec;
1001 int intr, cpuid = mycpuid;
1002 TD_INVARIANTS_DECLARE;
1004 get_mplock();
1005 crit_enter_gd(gd);
1006 TD_INVARIANTS_GET(gd->gd_curthread);
1008 for (;;) {
1009 for (intr = 0; intr < max_installed_hard_intr[cpuid]; ++intr) {
1010 info = &intr_block->ary[cpuid][intr];
1011 for (rec = info->i_reclist; rec; rec = nrec) {
1012 /* rec may be invalid after call */
1013 nrec = rec->next;
1014 if ((rec->intr_flags & INTR_NOPOLL) == 0) {
1015 if (rec->serializer) {
1016 lwkt_serialize_handler_try(rec->serializer,
1017 rec->handler, rec->argument, NULL);
1018 } else {
1019 rec->handler(rec->argument, NULL);
1021 TD_INVARIANTS_TEST(gd->gd_curthread, rec->name);
1025 lwkt_deschedule_self(gd->gd_curthread);
1026 lwkt_switch();
1028 /* NOT REACHED */
1032 * Systimer callback - schedule the emergency interrupt poll thread
1033 * if emergency polling is enabled.
1035 static
1036 void
1037 emergency_intr_timer_callback(systimer_t info, int in_ipi __unused,
1038 struct intrframe *frame __unused)
1040 if (emergency_intr_enable)
1041 lwkt_schedule(info->data);
1045 * Sysctls used by systat and others: hw.intrnames and hw.intrcnt.
1046 * The data for this machine dependent, and the declarations are in machine
1047 * dependent code. The layout of intrnames and intrcnt however is machine
1048 * independent.
1050 * We do not know the length of intrcnt and intrnames at compile time, so
1051 * calculate things at run time.
1054 static int
1055 sysctl_intrnames(SYSCTL_HANDLER_ARGS)
1057 struct intr_info *info;
1058 intrec_t rec;
1059 int error = 0;
1060 int len;
1061 int intr, cpuid;
1062 char buf[64];
1064 for (cpuid = 0; cpuid < ncpus; ++cpuid) {
1065 for (intr = 0; error == 0 && intr < MAX_INTS; ++intr) {
1066 info = &intr_block->ary[cpuid][intr];
1068 len = 0;
1069 buf[0] = 0;
1070 for (rec = info->i_reclist; rec; rec = rec->next) {
1071 ksnprintf(buf + len, sizeof(buf) - len, "%s%s",
1072 (len ? "/" : ""), rec->name);
1073 len += strlen(buf + len);
1075 if (len == 0) {
1076 ksnprintf(buf, sizeof(buf), "irq%d", intr);
1077 len = strlen(buf);
1079 error = SYSCTL_OUT(req, buf, len + 1);
1082 return (error);
1085 SYSCTL_PROC(_hw, OID_AUTO, intrnames, CTLTYPE_OPAQUE | CTLFLAG_RD,
1086 NULL, 0, sysctl_intrnames, "", "Interrupt Names");
1088 static int
1089 sysctl_intrcnt_all(SYSCTL_HANDLER_ARGS)
1091 struct intr_info *info;
1092 int error = 0;
1093 int intr, cpuid;
1095 for (cpuid = 0; cpuid < ncpus; ++cpuid) {
1096 for (intr = 0; intr < MAX_INTS; ++intr) {
1097 info = &intr_block->ary[cpuid][intr];
1099 error = SYSCTL_OUT(req, &info->i_count, sizeof(info->i_count));
1100 if (error)
1101 goto failed;
1104 failed:
1105 return(error);
1108 SYSCTL_PROC(_hw, OID_AUTO, intrcnt_all, CTLTYPE_OPAQUE | CTLFLAG_RD,
1109 NULL, 0, sysctl_intrcnt_all, "", "Interrupt Counts");
1111 SYSCTL_PROC(_hw, OID_AUTO, intrcnt, CTLTYPE_OPAQUE | CTLFLAG_RD,
1112 NULL, 0, sysctl_intrcnt_all, "", "Interrupt Counts");
1114 static void
1115 int_moveto_destcpu(int *orig_cpuid0, int cpuid)
1117 int orig_cpuid = mycpuid;
1119 if (cpuid != orig_cpuid)
1120 lwkt_migratecpu(cpuid);
1122 *orig_cpuid0 = orig_cpuid;
1125 static void
1126 int_moveto_origcpu(int orig_cpuid, int cpuid)
1128 if (cpuid != orig_cpuid)
1129 lwkt_migratecpu(orig_cpuid);
1132 static void
1133 intr_init(void *dummy __unused)
1135 int cpuid;
1137 kprintf("Initialize MI interrupts\n");
1139 intr_block = kmalloc(sizeof(*intr_block), M_INTRMNG,
1140 M_INTWAIT | M_ZERO);
1142 for (cpuid = 0; cpuid < ncpus; ++cpuid) {
1143 int intr;
1145 for (intr = 0; intr < MAX_INTS; ++intr) {
1146 struct intr_info *info = &intr_block->ary[cpuid][intr];
1148 info->i_cpuid = cpuid;
1149 info->i_intr = intr;
1153 SYSINIT(intr_init, SI_BOOT2_FINISH_PIC, SI_ORDER_ANY, intr_init, NULL);