xref: /dragonfly/sys/kern/kern_intr.c (revision d0d42ea0)
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.
4  *
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.
14  *
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.
25  *
26  * $FreeBSD: src/sys/kern/kern_intr.c,v 1.24.2.1 2001/10/14 20:05:50 luigi Exp $
27  *
28  */
29 
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>
42 
43 #include <machine/frame.h>
44 
45 #include <sys/thread2.h>
46 #include <sys/mplock2.h>
47 
48 struct intr_info;
49 
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;
60 
61 struct intr_info {
62 	intrec_t	i_reclist;
63 	struct thread	i_thread;
64 	struct random_softc i_random;
65 	int		i_running;
66 	long		i_count;	/* interrupts dispatched */
67 	int		i_mplock_required;
68 	int		i_fast;
69 	int		i_slow;
70 	int		i_state;
71 	int		i_errorticks;
72 	unsigned long	i_straycount;
73 	int		i_cpuid;
74 	int		i_intr;
75 };
76 
77 static struct intr_info intr_info_ary[MAXCPU][MAX_INTS];
78 static struct intr_info *swi_info_ary[MAX_SOFTINTS];
79 
80 static int max_installed_hard_intr[MAXCPU];
81 
82 #define EMERGENCY_INTR_POLLING_FREQ_MAX 20000
83 
84 /*
85  * Assert that callers into interrupt handlers don't return with
86  * dangling tokens, spinlocks, or mp locks.
87  */
88 #ifdef INVARIANTS
89 
90 #define TD_INVARIANTS_DECLARE   \
91         int spincount;          \
92         lwkt_tokref_t curstop
93 
94 #define TD_INVARIANTS_GET(td)                                   \
95         do {                                                    \
96                 spincount = (td)->td_gd->gd_spinlocks_wr;       \
97                 curstop = (td)->td_toks_stop;                   \
98         } while(0)
99 
100 #define TD_INVARIANTS_TEST(td, name)                                    \
101         do {                                                            \
102                 KASSERT(spincount == (td)->td_gd->gd_spinlocks_wr,      \
103                         ("spincount mismatch after interrupt handler %s", \
104                         name));                                         \
105                 KASSERT(curstop == (td)->td_toks_stop,                  \
106                         ("token count mismatch after interrupt handler %s", \
107                         name));                                         \
108         } while(0)
109 
110 #else
111 
112 /* !INVARIANTS */
113 
114 #define TD_INVARIANTS_DECLARE
115 #define TD_INVARIANTS_GET(td)
116 #define TD_INVARIANTS_TEST(td, name)
117 
118 #endif /* ndef INVARIANTS */
119 
120 static int sysctl_emergency_freq(SYSCTL_HANDLER_ARGS);
121 static int sysctl_emergency_enable(SYSCTL_HANDLER_ARGS);
122 static void emergency_intr_timer_callback(systimer_t, int, struct intrframe *);
123 static void ithread_handler(void *arg);
124 static void ithread_emergency(void *arg);
125 static void report_stray_interrupt(struct intr_info *info, const char *func);
126 static void int_moveto_destcpu(int *, int);
127 static void int_moveto_origcpu(int, int);
128 static void sched_ithd_intern(struct intr_info *info);
129 
130 static struct systimer emergency_intr_timer[MAXCPU];
131 static struct thread emergency_intr_thread[MAXCPU];
132 
133 #define ISTATE_NOTHREAD		0
134 #define ISTATE_NORMAL		1
135 #define ISTATE_LIVELOCKED	2
136 
137 static int livelock_limit = 40000;
138 static int livelock_lowater = 20000;
139 static int livelock_debug = -1;
140 SYSCTL_INT(_kern, OID_AUTO, livelock_limit,
141         CTLFLAG_RW, &livelock_limit, 0, "Livelock interrupt rate limit");
142 SYSCTL_INT(_kern, OID_AUTO, livelock_lowater,
143         CTLFLAG_RW, &livelock_lowater, 0, "Livelock low-water mark restore");
144 SYSCTL_INT(_kern, OID_AUTO, livelock_debug,
145         CTLFLAG_RW, &livelock_debug, 0, "Livelock debug intr#");
146 
147 static int emergency_intr_enable = 0;	/* emergency interrupt polling */
148 TUNABLE_INT("kern.emergency_intr_enable", &emergency_intr_enable);
149 SYSCTL_PROC(_kern, OID_AUTO, emergency_intr_enable, CTLTYPE_INT | CTLFLAG_RW,
150         0, 0, sysctl_emergency_enable, "I", "Emergency Interrupt Poll Enable");
151 
152 static int emergency_intr_freq = 10;	/* emergency polling frequency */
153 TUNABLE_INT("kern.emergency_intr_freq", &emergency_intr_freq);
154 SYSCTL_PROC(_kern, OID_AUTO, emergency_intr_freq, CTLTYPE_INT | CTLFLAG_RW,
155         0, 0, sysctl_emergency_freq, "I", "Emergency Interrupt Poll Frequency");
156 
157 /*
158  * Sysctl support routines
159  */
160 static int
161 sysctl_emergency_enable(SYSCTL_HANDLER_ARGS)
162 {
163 	int error, enabled, cpuid, freq;
164 
165 	enabled = emergency_intr_enable;
166 	error = sysctl_handle_int(oidp, &enabled, 0, req);
167 	if (error || req->newptr == NULL)
168 		return error;
169 	emergency_intr_enable = enabled;
170 	if (emergency_intr_enable)
171 		freq = emergency_intr_freq;
172 	else
173 		freq = 1;
174 
175 	for (cpuid = 0; cpuid < ncpus; ++cpuid)
176 		systimer_adjust_periodic(&emergency_intr_timer[cpuid], freq);
177 	return 0;
178 }
179 
180 static int
181 sysctl_emergency_freq(SYSCTL_HANDLER_ARGS)
182 {
183         int error, phz, cpuid, freq;
184 
185         phz = emergency_intr_freq;
186         error = sysctl_handle_int(oidp, &phz, 0, req);
187         if (error || req->newptr == NULL)
188                 return error;
189         if (phz <= 0)
190                 return EINVAL;
191         else if (phz > EMERGENCY_INTR_POLLING_FREQ_MAX)
192                 phz = EMERGENCY_INTR_POLLING_FREQ_MAX;
193 
194         emergency_intr_freq = phz;
195 	if (emergency_intr_enable)
196 		freq = emergency_intr_freq;
197 	else
198 		freq = 1;
199 
200 	for (cpuid = 0; cpuid < ncpus; ++cpuid)
201 		systimer_adjust_periodic(&emergency_intr_timer[cpuid], freq);
202         return 0;
203 }
204 
205 /*
206  * Register an SWI or INTerrupt handler.
207  */
208 void *
209 register_swi(int intr, inthand2_t *handler, void *arg, const char *name,
210 		struct lwkt_serialize *serializer, int cpuid)
211 {
212     if (intr < FIRST_SOFTINT || intr >= MAX_INTS)
213 	panic("register_swi: bad intr %d", intr);
214 
215     if (cpuid < 0)
216 	cpuid = intr % ncpus;
217     return(register_int(intr, handler, arg, name, serializer, 0, cpuid));
218 }
219 
220 void *
221 register_swi_mp(int intr, inthand2_t *handler, void *arg, const char *name,
222 		struct lwkt_serialize *serializer, int cpuid)
223 {
224     if (intr < FIRST_SOFTINT || intr >= MAX_INTS)
225 	panic("register_swi: bad intr %d", intr);
226 
227     if (cpuid < 0)
228 	cpuid = intr % ncpus;
229     return(register_int(intr, handler, arg, name, serializer,
230         INTR_MPSAFE, cpuid));
231 }
232 
233 void *
234 register_int(int intr, inthand2_t *handler, void *arg, const char *name,
235 		struct lwkt_serialize *serializer, int intr_flags, int cpuid)
236 {
237     struct intr_info *info;
238     struct intrec **list;
239     intrec_t rec;
240     int orig_cpuid;
241 
242     KKASSERT(cpuid >= 0 && cpuid < ncpus);
243 
244     if (intr < 0 || intr >= MAX_INTS)
245 	panic("register_int: bad intr %d", intr);
246     if (name == NULL)
247 	name = "???";
248     info = &intr_info_ary[cpuid][intr];
249 
250     /*
251      * Construct an interrupt handler record
252      */
253     rec = kmalloc(sizeof(struct intrec), M_DEVBUF, M_INTWAIT);
254     rec->name = kmalloc(strlen(name) + 1, M_DEVBUF, M_INTWAIT);
255     strcpy(rec->name, name);
256 
257     rec->info = info;
258     rec->handler = handler;
259     rec->argument = arg;
260     rec->intr = intr;
261     rec->intr_flags = intr_flags;
262     rec->next = NULL;
263     rec->serializer = serializer;
264 
265     int_moveto_destcpu(&orig_cpuid, cpuid);
266 
267     /*
268      * Create an emergency polling thread and set up a systimer to wake
269      * it up.
270      */
271     if (emergency_intr_thread[cpuid].td_kstack == NULL) {
272 	lwkt_create(ithread_emergency, NULL, NULL,
273 		    &emergency_intr_thread[cpuid],
274 		    TDF_NOSTART | TDF_INTTHREAD, cpuid, "ithreadE %d",
275 		    cpuid);
276 	systimer_init_periodic_nq(&emergency_intr_timer[cpuid],
277 		    emergency_intr_timer_callback,
278 		    &emergency_intr_thread[cpuid],
279 		    (emergency_intr_enable ? emergency_intr_freq : 1));
280     }
281 
282     /*
283      * Create an interrupt thread if necessary, leave it in an unscheduled
284      * state.
285      */
286     if (info->i_state == ISTATE_NOTHREAD) {
287 	info->i_state = ISTATE_NORMAL;
288 	lwkt_create(ithread_handler, (void *)(intptr_t)intr, NULL,
289 		    &info->i_thread, TDF_NOSTART | TDF_INTTHREAD, cpuid,
290 		    "ithread%d %d", intr, cpuid);
291 	if (intr >= FIRST_SOFTINT)
292 	    lwkt_setpri(&info->i_thread, TDPRI_SOFT_NORM);
293 	else
294 	    lwkt_setpri(&info->i_thread, TDPRI_INT_MED);
295 	info->i_thread.td_preemptable = lwkt_preempt;
296     }
297 
298     list = &info->i_reclist;
299 
300     /*
301      * Keep track of how many fast and slow interrupts we have.
302      * Set i_mplock_required if any handler in the chain requires
303      * the MP lock to operate.
304      */
305     if ((intr_flags & INTR_MPSAFE) == 0)
306 	info->i_mplock_required = 1;
307     if (intr_flags & INTR_CLOCK)
308 	++info->i_fast;
309     else
310 	++info->i_slow;
311 
312     /*
313      * Enable random number generation keying off of this interrupt.
314      */
315     if ((intr_flags & INTR_NOENTROPY) == 0 && info->i_random.sc_enabled == 0) {
316 	info->i_random.sc_enabled = 1;
317 	info->i_random.sc_intr = intr;
318     }
319 
320     /*
321      * Add the record to the interrupt list.
322      */
323     crit_enter();
324     while (*list != NULL)
325 	list = &(*list)->next;
326     *list = rec;
327     crit_exit();
328 
329     /*
330      * Update max_installed_hard_intr to make the emergency intr poll
331      * a bit more efficient.
332      */
333     if (intr < FIRST_SOFTINT) {
334 	if (max_installed_hard_intr[cpuid] <= intr)
335 	    max_installed_hard_intr[cpuid] = intr + 1;
336     }
337 
338     if (intr >= FIRST_SOFTINT)
339 	swi_info_ary[intr - FIRST_SOFTINT] = info;
340 
341     /*
342      * Setup the machine level interrupt vector
343      */
344     if (intr < FIRST_SOFTINT && info->i_slow + info->i_fast == 1)
345 	machintr_intr_setup(intr, intr_flags);
346 
347     int_moveto_origcpu(orig_cpuid, cpuid);
348 
349     return(rec);
350 }
351 
352 void
353 unregister_swi(void *id, int intr, int cpuid)
354 {
355     if (cpuid < 0)
356 	cpuid = intr % ncpus;
357 
358     unregister_int(id, cpuid);
359 }
360 
361 void
362 unregister_int(void *id, int cpuid)
363 {
364     struct intr_info *info;
365     struct intrec **list;
366     intrec_t rec;
367     int intr, orig_cpuid;
368 
369     KKASSERT(cpuid >= 0 && cpuid < ncpus);
370 
371     intr = ((intrec_t)id)->intr;
372 
373     if (intr < 0 || intr >= MAX_INTS)
374 	panic("register_int: bad intr %d", intr);
375 
376     info = &intr_info_ary[cpuid][intr];
377 
378     int_moveto_destcpu(&orig_cpuid, cpuid);
379 
380     /*
381      * Remove the interrupt descriptor, adjust the descriptor count,
382      * and teardown the machine level vector if this was the last interrupt.
383      */
384     crit_enter();
385     list = &info->i_reclist;
386     while ((rec = *list) != NULL) {
387 	if (rec == id)
388 	    break;
389 	list = &rec->next;
390     }
391     if (rec) {
392 	intrec_t rec0;
393 
394 	*list = rec->next;
395 	if (rec->intr_flags & INTR_CLOCK)
396 	    --info->i_fast;
397 	else
398 	    --info->i_slow;
399 	if (intr < FIRST_SOFTINT && info->i_fast + info->i_slow == 0)
400 	    machintr_intr_teardown(intr);
401 
402 	/*
403 	 * Clear i_mplock_required if no handlers in the chain require the
404 	 * MP lock.
405 	 */
406 	for (rec0 = info->i_reclist; rec0; rec0 = rec0->next) {
407 	    if ((rec0->intr_flags & INTR_MPSAFE) == 0)
408 		break;
409 	}
410 	if (rec0 == NULL)
411 	    info->i_mplock_required = 0;
412     }
413 
414     if (intr >= FIRST_SOFTINT && info->i_reclist == NULL)
415 	swi_info_ary[intr - FIRST_SOFTINT] = NULL;
416 
417     crit_exit();
418 
419     int_moveto_origcpu(orig_cpuid, cpuid);
420 
421     /*
422      * Free the record.
423      */
424     if (rec != NULL) {
425 	kfree(rec->name, M_DEVBUF);
426 	kfree(rec, M_DEVBUF);
427     } else {
428 	kprintf("warning: unregister_int: int %d handler for %s not found\n",
429 		intr, ((intrec_t)id)->name);
430     }
431 }
432 
433 long
434 get_interrupt_counter(int intr, int cpuid)
435 {
436     struct intr_info *info;
437 
438     KKASSERT(cpuid >= 0 && cpuid < ncpus);
439 
440     if (intr < 0 || intr >= MAX_INTS)
441 	panic("register_int: bad intr %d", intr);
442     info = &intr_info_ary[cpuid][intr];
443     return(info->i_count);
444 }
445 
446 void
447 register_randintr(int intr)
448 {
449     struct intr_info *info;
450     int cpuid;
451 
452     if (intr < 0 || intr >= MAX_INTS)
453 	panic("register_randintr: bad intr %d", intr);
454 
455     for (cpuid = 0; cpuid < ncpus; ++cpuid) {
456 	info = &intr_info_ary[cpuid][intr];
457 	info->i_random.sc_intr = intr;
458 	info->i_random.sc_enabled = 1;
459     }
460 }
461 
462 void
463 unregister_randintr(int intr)
464 {
465     struct intr_info *info;
466     int cpuid;
467 
468     if (intr < 0 || intr >= MAX_INTS)
469 	panic("register_swi: bad intr %d", intr);
470 
471     for (cpuid = 0; cpuid < ncpus; ++cpuid) {
472 	info = &intr_info_ary[cpuid][intr];
473 	info->i_random.sc_enabled = -1;
474     }
475 }
476 
477 int
478 next_registered_randintr(int intr)
479 {
480     struct intr_info *info;
481 
482     if (intr < 0 || intr >= MAX_INTS)
483 	panic("register_swi: bad intr %d", intr);
484 
485     while (intr < MAX_INTS) {
486 	int cpuid;
487 
488 	for (cpuid = 0; cpuid < ncpus; ++cpuid) {
489 	    info = &intr_info_ary[cpuid][intr];
490 	    if (info->i_random.sc_enabled > 0)
491 		return intr;
492 	}
493 	++intr;
494     }
495     return intr;
496 }
497 
498 /*
499  * Dispatch an interrupt.  If there's nothing to do we have a stray
500  * interrupt and can just return, leaving the interrupt masked.
501  *
502  * We need to schedule the interrupt and set its i_running bit.  If
503  * we are not on the interrupt thread's cpu we have to send a message
504  * to the correct cpu that will issue the desired action (interlocking
505  * with the interrupt thread's critical section).  We do NOT attempt to
506  * reschedule interrupts whos i_running bit is already set because
507  * this would prematurely wakeup a livelock-limited interrupt thread.
508  *
509  * i_running is only tested/set on the same cpu as the interrupt thread.
510  *
511  * We are NOT in a critical section, which will allow the scheduled
512  * interrupt to preempt us.  The MP lock might *NOT* be held here.
513  */
514 #ifdef SMP
515 
516 static void
517 sched_ithd_remote(void *arg)
518 {
519     sched_ithd_intern(arg);
520 }
521 
522 #endif
523 
524 static void
525 sched_ithd_intern(struct intr_info *info)
526 {
527     ++info->i_count;
528     if (info->i_state != ISTATE_NOTHREAD) {
529 	if (info->i_reclist == NULL) {
530 	    report_stray_interrupt(info, "sched_ithd");
531 	} else {
532 #ifdef SMP
533 	    if (info->i_thread.td_gd == mycpu) {
534 		if (info->i_running == 0) {
535 		    info->i_running = 1;
536 		    if (info->i_state != ISTATE_LIVELOCKED)
537 			lwkt_schedule(&info->i_thread); /* MIGHT PREEMPT */
538 		}
539 	    } else {
540 		lwkt_send_ipiq(info->i_thread.td_gd, sched_ithd_remote, info);
541 	    }
542 #else
543 	    if (info->i_running == 0) {
544 		info->i_running = 1;
545 		if (info->i_state != ISTATE_LIVELOCKED)
546 		    lwkt_schedule(&info->i_thread); /* MIGHT PREEMPT */
547 	    }
548 #endif
549 	}
550     } else {
551 	report_stray_interrupt(info, "sched_ithd");
552     }
553 }
554 
555 void
556 sched_ithd_soft(int intr)
557 {
558 	struct intr_info *info;
559 
560 	KKASSERT(intr >= FIRST_SOFTINT && intr < MAX_INTS);
561 
562 	info = swi_info_ary[intr - FIRST_SOFTINT];
563 	if (info != NULL) {
564 		sched_ithd_intern(info);
565 	} else {
566 		kprintf("unregistered softint %d got scheduled on cpu%d\n",
567 		    intr, mycpuid);
568 	}
569 }
570 
571 void
572 sched_ithd_hard(int intr)
573 {
574 	KKASSERT(intr >= 0 && intr < MAX_HARDINTS);
575 	sched_ithd_intern(&intr_info_ary[mycpuid][intr]);
576 }
577 
578 static void
579 report_stray_interrupt(struct intr_info *info, const char *func)
580 {
581 	++info->i_straycount;
582 	if (info->i_straycount < 10) {
583 		if (info->i_errorticks == ticks)
584 			return;
585 		info->i_errorticks = ticks;
586 		kprintf("%s: stray interrupt %d on cpu%d\n",
587 		    func, info->i_intr, mycpuid);
588 	} else if (info->i_straycount == 10) {
589 		kprintf("%s: %ld stray interrupts %d on cpu%d - "
590 			"there will be no further reports\n", func,
591 			info->i_straycount, info->i_intr, mycpuid);
592 	}
593 }
594 
595 /*
596  * This is run from a periodic SYSTIMER (and thus must be MP safe, the BGL
597  * might not be held).
598  */
599 static void
600 ithread_livelock_wakeup(systimer_t st, int in_ipi __unused,
601     struct intrframe *frame __unused)
602 {
603     struct intr_info *info;
604 
605     info = &intr_info_ary[mycpuid][(int)(intptr_t)st->data];
606     if (info->i_state != ISTATE_NOTHREAD)
607 	lwkt_schedule(&info->i_thread);
608 }
609 
610 /*
611  * Schedule ithread within fast intr handler
612  *
613  * XXX Protect sched_ithd_hard() call with gd_intr_nesting_level?
614  * Interrupts aren't enabled, but still...
615  */
616 static __inline void
617 ithread_fast_sched(int intr, thread_t td)
618 {
619     ++td->td_nest_count;
620 
621     /*
622      * We are already in critical section, exit it now to
623      * allow preemption.
624      */
625     crit_exit_quick(td);
626     sched_ithd_hard(intr);
627     crit_enter_quick(td);
628 
629     --td->td_nest_count;
630 }
631 
632 /*
633  * This function is called directly from the ICU or APIC vector code assembly
634  * to process an interrupt.  The critical section and interrupt deferral
635  * checks have already been done but the function is entered WITHOUT
636  * a critical section held.  The BGL may or may not be held.
637  *
638  * Must return non-zero if we do not want the vector code to re-enable
639  * the interrupt (which we don't if we have to schedule the interrupt)
640  */
641 int ithread_fast_handler(struct intrframe *frame);
642 
643 int
644 ithread_fast_handler(struct intrframe *frame)
645 {
646     int intr;
647     struct intr_info *info;
648     struct intrec **list;
649     int must_schedule;
650 #ifdef SMP
651     int got_mplock;
652 #endif
653     TD_INVARIANTS_DECLARE;
654     intrec_t rec, nrec;
655     globaldata_t gd;
656     thread_t td;
657 
658     intr = frame->if_vec;
659     gd = mycpu;
660     td = curthread;
661 
662     /* We must be in critical section. */
663     KKASSERT(td->td_critcount);
664 
665     info = &intr_info_ary[mycpuid][intr];
666 
667     /*
668      * If we are not processing any FAST interrupts, just schedule the thing.
669      */
670     if (info->i_fast == 0) {
671     	++gd->gd_cnt.v_intr;
672 	ithread_fast_sched(intr, td);
673 	return(1);
674     }
675 
676     /*
677      * This should not normally occur since interrupts ought to be
678      * masked if the ithread has been scheduled or is running.
679      */
680     if (info->i_running)
681 	return(1);
682 
683     /*
684      * Bump the interrupt nesting level to process any FAST interrupts.
685      * Obtain the MP lock as necessary.  If the MP lock cannot be obtained,
686      * schedule the interrupt thread to deal with the issue instead.
687      *
688      * To reduce overhead, just leave the MP lock held once it has been
689      * obtained.
690      */
691     ++gd->gd_intr_nesting_level;
692     ++gd->gd_cnt.v_intr;
693     must_schedule = info->i_slow;
694 #ifdef SMP
695     got_mplock = 0;
696 #endif
697 
698     TD_INVARIANTS_GET(td);
699     list = &info->i_reclist;
700 
701     for (rec = *list; rec; rec = nrec) {
702 	/* rec may be invalid after call */
703 	nrec = rec->next;
704 
705 	if (rec->intr_flags & INTR_CLOCK) {
706 #ifdef SMP
707 	    if ((rec->intr_flags & INTR_MPSAFE) == 0 && got_mplock == 0) {
708 		if (try_mplock() == 0) {
709 		    /* Couldn't get the MP lock; just schedule it. */
710 		    must_schedule = 1;
711 		    break;
712 		}
713 		got_mplock = 1;
714 	    }
715 #endif
716 	    if (rec->serializer) {
717 		must_schedule += lwkt_serialize_handler_try(
718 					rec->serializer, rec->handler,
719 					rec->argument, frame);
720 	    } else {
721 		rec->handler(rec->argument, frame);
722 	    }
723 	    TD_INVARIANTS_TEST(td, rec->name);
724 	}
725     }
726 
727     /*
728      * Cleanup
729      */
730     --gd->gd_intr_nesting_level;
731 #ifdef SMP
732     if (got_mplock)
733 	rel_mplock();
734 #endif
735 
736     /*
737      * If we had a problem, or mixed fast and slow interrupt handlers are
738      * registered, schedule the ithread to catch the missed records (it
739      * will just re-run all of them).  A return value of 0 indicates that
740      * all handlers have been run and the interrupt can be re-enabled, and
741      * a non-zero return indicates that the interrupt thread controls
742      * re-enablement.
743      */
744     if (must_schedule > 0)
745 	ithread_fast_sched(intr, td);
746     else if (must_schedule == 0)
747 	++info->i_count;
748     return(must_schedule);
749 }
750 
751 /*
752  * Interrupt threads run this as their main loop.
753  *
754  * The handler begins execution outside a critical section and no MP lock.
755  *
756  * The i_running state starts at 0.  When an interrupt occurs, the hardware
757  * interrupt is disabled and sched_ithd_hard() The HW interrupt remains
758  * disabled until all routines have run.  We then call ithread_done() to
759  * reenable the HW interrupt and deschedule us until the next interrupt.
760  *
761  * We are responsible for atomically checking i_running and ithread_done()
762  * is responsible for atomically checking for platform-specific delayed
763  * interrupts.  i_running for our irq is only set in the context of our cpu,
764  * so a critical section is a sufficient interlock.
765  */
766 #define LIVELOCK_TIMEFRAME(freq)	((freq) >> 2)	/* 1/4 second */
767 
768 static void
769 ithread_handler(void *arg)
770 {
771     struct intr_info *info;
772     int use_limit;
773     __uint32_t lseconds;
774     int intr, cpuid = mycpuid;
775     int mpheld;
776     struct intrec **list;
777     intrec_t rec, nrec;
778     globaldata_t gd;
779     struct systimer ill_timer;	/* enforced freq. timer */
780     u_int ill_count;		/* interrupt livelock counter */
781     TD_INVARIANTS_DECLARE;
782 
783     ill_count = 0;
784     intr = (int)(intptr_t)arg;
785     info = &intr_info_ary[cpuid][intr];
786     list = &info->i_reclist;
787 
788     /*
789      * The loop must be entered with one critical section held.  The thread
790      * does not hold the mplock on startup.
791      */
792     gd = mycpu;
793     lseconds = gd->gd_time_seconds;
794     crit_enter_gd(gd);
795     mpheld = 0;
796 
797     for (;;) {
798 	/*
799 	 * The chain is only considered MPSAFE if all its interrupt handlers
800 	 * are MPSAFE.  However, if intr_mpsafe has been turned off we
801 	 * always operate with the BGL.
802 	 */
803 #ifdef SMP
804 	if (info->i_mplock_required != mpheld) {
805 	    if (info->i_mplock_required) {
806 		KKASSERT(mpheld == 0);
807 		get_mplock();
808 		mpheld = 1;
809 	    } else {
810 		KKASSERT(mpheld != 0);
811 		rel_mplock();
812 		mpheld = 0;
813 	    }
814 	}
815 #endif
816 
817 	TD_INVARIANTS_GET(gd->gd_curthread);
818 
819 	/*
820 	 * If an interrupt is pending, clear i_running and execute the
821 	 * handlers.  Note that certain types of interrupts can re-trigger
822 	 * and set i_running again.
823 	 *
824 	 * Each handler is run in a critical section.  Note that we run both
825 	 * FAST and SLOW designated service routines.
826 	 */
827 	if (info->i_running) {
828 	    ++ill_count;
829 	    info->i_running = 0;
830 
831 	    if (*list == NULL)
832 		report_stray_interrupt(info, "ithread_handler");
833 
834 	    for (rec = *list; rec; rec = nrec) {
835 		/* rec may be invalid after call */
836 		nrec = rec->next;
837 		if (rec->serializer) {
838 		    lwkt_serialize_handler_call(rec->serializer, rec->handler,
839 						rec->argument, NULL);
840 		} else {
841 		    rec->handler(rec->argument, NULL);
842 		}
843 		TD_INVARIANTS_TEST(gd->gd_curthread, rec->name);
844 	    }
845 	}
846 
847 	/*
848 	 * This is our interrupt hook to add rate randomness to the random
849 	 * number generator.
850 	 */
851 	if (info->i_random.sc_enabled > 0)
852 	    add_interrupt_randomness(intr);
853 
854 	/*
855 	 * Unmask the interrupt to allow it to trigger again.  This only
856 	 * applies to certain types of interrupts (typ level interrupts).
857 	 * This can result in the interrupt retriggering, but the retrigger
858 	 * will not be processed until we cycle our critical section.
859 	 *
860 	 * Only unmask interrupts while handlers are installed.  It is
861 	 * possible to hit a situation where no handlers are installed
862 	 * due to a device driver livelocking and then tearing down its
863 	 * interrupt on close (the parallel bus being a good example).
864 	 */
865 	if (intr < FIRST_SOFTINT && *list)
866 	    machintr_intr_enable(intr);
867 
868 	/*
869 	 * Do a quick exit/enter to catch any higher-priority interrupt
870 	 * sources, such as the statclock, so thread time accounting
871 	 * will still work.  This may also cause an interrupt to re-trigger.
872 	 */
873 	crit_exit_gd(gd);
874 	crit_enter_gd(gd);
875 
876 	/*
877 	 * LIVELOCK STATE MACHINE
878 	 */
879 	switch(info->i_state) {
880 	case ISTATE_NORMAL:
881 	    /*
882 	     * Reset the count each second.
883 	     */
884 	    if (lseconds != gd->gd_time_seconds) {
885 		lseconds = gd->gd_time_seconds;
886 		ill_count = 0;
887 	    }
888 
889 	    /*
890 	     * If we did not exceed the frequency limit, we are done.
891 	     * If the interrupt has not retriggered we deschedule ourselves.
892 	     */
893 	    if (ill_count <= livelock_limit) {
894 		if (info->i_running == 0) {
895 		    lwkt_deschedule_self(gd->gd_curthread);
896 		    lwkt_switch();
897 		}
898 		break;
899 	    }
900 
901 	    /*
902 	     * Otherwise we are livelocked.  Set up a periodic systimer
903 	     * to wake the thread up at the limit frequency.
904 	     */
905 	    kprintf("intr %d on cpu%d at %d/%d hz, livelocked limit engaged!\n",
906 		    intr, cpuid, ill_count, livelock_limit);
907 	    info->i_state = ISTATE_LIVELOCKED;
908 	    if ((use_limit = livelock_limit) < 100)
909 		use_limit = 100;
910 	    else if (use_limit > 500000)
911 		use_limit = 500000;
912 	    systimer_init_periodic_nq(&ill_timer, ithread_livelock_wakeup,
913 				      (void *)(intptr_t)intr, use_limit);
914 	    /* fall through */
915 	case ISTATE_LIVELOCKED:
916 	    /*
917 	     * Wait for our periodic timer to go off.  Since the interrupt
918 	     * has re-armed it can still set i_running, but it will not
919 	     * reschedule us while we are in a livelocked state.
920 	     */
921 	    lwkt_deschedule_self(gd->gd_curthread);
922 	    lwkt_switch();
923 
924 	    /*
925 	     * Check once a second to see if the livelock condition no
926 	     * longer applies.
927 	     */
928 	    if (lseconds != gd->gd_time_seconds) {
929 		lseconds = gd->gd_time_seconds;
930 		if (ill_count < livelock_lowater) {
931 		    info->i_state = ISTATE_NORMAL;
932 		    systimer_del(&ill_timer);
933 		    kprintf("intr %d on cpu%d at %d/%d hz, livelock removed\n",
934 			    intr, cpuid, ill_count, livelock_lowater);
935 		} else if (livelock_debug == intr ||
936 			   (bootverbose && cold)) {
937 		    kprintf("intr %d on cpu%d at %d/%d hz, in livelock\n",
938 			    intr, cpuid, ill_count, livelock_lowater);
939 		}
940 		ill_count = 0;
941 	    }
942 	    break;
943 	}
944     }
945     /* NOT REACHED */
946 }
947 
948 /*
949  * Emergency interrupt polling thread.  The thread begins execution
950  * outside a critical section with the BGL held.
951  *
952  * If emergency interrupt polling is enabled, this thread will
953  * execute all system interrupts not marked INTR_NOPOLL at the
954  * specified polling frequency.
955  *
956  * WARNING!  This thread runs *ALL* interrupt service routines that
957  * are not marked INTR_NOPOLL, which basically means everything except
958  * the 8254 clock interrupt and the ATA interrupt.  It has very high
959  * overhead and should only be used in situations where the machine
960  * cannot otherwise be made to work.  Due to the severe performance
961  * degredation, it should not be enabled on production machines.
962  */
963 static void
964 ithread_emergency(void *arg __unused)
965 {
966     globaldata_t gd = mycpu;
967     struct intr_info *info;
968     intrec_t rec, nrec;
969     int intr, cpuid = mycpuid;
970     TD_INVARIANTS_DECLARE;
971 
972     get_mplock();
973     crit_enter_gd(gd);
974     TD_INVARIANTS_GET(gd->gd_curthread);
975 
976     for (;;) {
977 	for (intr = 0; intr < max_installed_hard_intr[cpuid]; ++intr) {
978 	    info = &intr_info_ary[cpuid][intr];
979 	    for (rec = info->i_reclist; rec; rec = nrec) {
980 		/* rec may be invalid after call */
981 		nrec = rec->next;
982 		if ((rec->intr_flags & INTR_NOPOLL) == 0) {
983 		    if (rec->serializer) {
984 			lwkt_serialize_handler_try(rec->serializer,
985 						rec->handler, rec->argument, NULL);
986 		    } else {
987 			rec->handler(rec->argument, NULL);
988 		    }
989 		    TD_INVARIANTS_TEST(gd->gd_curthread, rec->name);
990 		}
991 	    }
992 	}
993 	lwkt_deschedule_self(gd->gd_curthread);
994 	lwkt_switch();
995     }
996     /* NOT REACHED */
997 }
998 
999 /*
1000  * Systimer callback - schedule the emergency interrupt poll thread
1001  * 		       if emergency polling is enabled.
1002  */
1003 static
1004 void
1005 emergency_intr_timer_callback(systimer_t info, int in_ipi __unused,
1006     struct intrframe *frame __unused)
1007 {
1008     if (emergency_intr_enable)
1009 	lwkt_schedule(info->data);
1010 }
1011 
1012 int
1013 ithread_cpuid(int intr)
1014 {
1015 	return machintr_intr_cpuid(intr);
1016 }
1017 
1018 /*
1019  * Sysctls used by systat and others: hw.intrnames and hw.intrcnt.
1020  * The data for this machine dependent, and the declarations are in machine
1021  * dependent code.  The layout of intrnames and intrcnt however is machine
1022  * independent.
1023  *
1024  * We do not know the length of intrcnt and intrnames at compile time, so
1025  * calculate things at run time.
1026  */
1027 
1028 static int
1029 sysctl_intrnames(SYSCTL_HANDLER_ARGS)
1030 {
1031     struct intr_info *info;
1032     intrec_t rec;
1033     int error = 0;
1034     int len;
1035     int intr, cpuid;
1036     char buf[64];
1037 
1038     for (cpuid = 0; cpuid < ncpus; ++cpuid) {
1039 	for (intr = 0; error == 0 && intr < MAX_INTS; ++intr) {
1040 	    info = &intr_info_ary[cpuid][intr];
1041 
1042 	    len = 0;
1043 	    buf[0] = 0;
1044 	    for (rec = info->i_reclist; rec; rec = rec->next) {
1045 		ksnprintf(buf + len, sizeof(buf) - len, "%s%s",
1046 		    (len ? "/" : ""), rec->name);
1047 		len += strlen(buf + len);
1048 	    }
1049 	    if (len == 0) {
1050 		ksnprintf(buf, sizeof(buf), "irq%d", intr);
1051 		len = strlen(buf);
1052 	    }
1053 	    error = SYSCTL_OUT(req, buf, len + 1);
1054 	}
1055     }
1056     return (error);
1057 }
1058 
1059 SYSCTL_PROC(_hw, OID_AUTO, intrnames, CTLTYPE_OPAQUE | CTLFLAG_RD,
1060 	NULL, 0, sysctl_intrnames, "", "Interrupt Names");
1061 
1062 static int
1063 sysctl_intrcnt_all(SYSCTL_HANDLER_ARGS)
1064 {
1065     struct intr_info *info;
1066     int error = 0;
1067     int intr, cpuid;
1068 
1069     for (cpuid = 0; cpuid < ncpus; ++cpuid) {
1070 	for (intr = 0; intr < MAX_INTS; ++intr) {
1071 	    info = &intr_info_ary[cpuid][intr];
1072 
1073 	    error = SYSCTL_OUT(req, &info->i_count, sizeof(info->i_count));
1074 	    if (error)
1075 		goto failed;
1076 	}
1077     }
1078 failed:
1079     return(error);
1080 }
1081 
1082 SYSCTL_PROC(_hw, OID_AUTO, intrcnt_all, CTLTYPE_OPAQUE | CTLFLAG_RD,
1083 	NULL, 0, sysctl_intrcnt_all, "", "Interrupt Counts");
1084 
1085 SYSCTL_PROC(_hw, OID_AUTO, intrcnt, CTLTYPE_OPAQUE | CTLFLAG_RD,
1086 	NULL, 0, sysctl_intrcnt_all, "", "Interrupt Counts");
1087 
1088 static void
1089 int_moveto_destcpu(int *orig_cpuid0, int cpuid)
1090 {
1091     int orig_cpuid = mycpuid;
1092 
1093     if (cpuid != orig_cpuid)
1094 	lwkt_migratecpu(cpuid);
1095 
1096     *orig_cpuid0 = orig_cpuid;
1097 }
1098 
1099 static void
1100 int_moveto_origcpu(int orig_cpuid, int cpuid)
1101 {
1102     if (cpuid != orig_cpuid)
1103 	lwkt_migratecpu(orig_cpuid);
1104 }
1105 
1106 static void
1107 intr_init(void *dummy __unused)
1108 {
1109 	int cpuid;
1110 
1111 	kprintf("Initialize MI interrupts\n");
1112 
1113 	for (cpuid = 0; cpuid < ncpus; ++cpuid) {
1114 		int intr;
1115 
1116 		for (intr = 0; intr < MAX_INTS; ++intr) {
1117 			struct intr_info *info = &intr_info_ary[cpuid][intr];
1118 
1119 			info->i_cpuid = cpuid;
1120 			info->i_intr = intr;
1121 		}
1122 	}
1123 }
1124 SYSINIT(intr_init, SI_BOOT2_FINISH_PIC, SI_ORDER_ANY, intr_init, NULL);
1125