]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kern/kern_intr.c
Use config_intrhook API to create the dev.cpu.N.temperature sysctl node.
[FreeBSD/FreeBSD.git] / sys / kern / kern_intr.c
1 /*-
2  * Copyright (c) 1997, Stefan Esser <se@freebsd.org>
3  * 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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include "opt_ddb.h"
31
32 #include <sys/param.h>
33 #include <sys/bus.h>
34 #include <sys/conf.h>
35 #include <sys/cpuset.h>
36 #include <sys/rtprio.h>
37 #include <sys/systm.h>
38 #include <sys/interrupt.h>
39 #include <sys/kernel.h>
40 #include <sys/kthread.h>
41 #include <sys/ktr.h>
42 #include <sys/limits.h>
43 #include <sys/lock.h>
44 #include <sys/malloc.h>
45 #include <sys/mutex.h>
46 #include <sys/proc.h>
47 #include <sys/random.h>
48 #include <sys/resourcevar.h>
49 #include <sys/sched.h>
50 #include <sys/smp.h>
51 #include <sys/sysctl.h>
52 #include <sys/unistd.h>
53 #include <sys/vmmeter.h>
54 #include <machine/atomic.h>
55 #include <machine/cpu.h>
56 #include <machine/md_var.h>
57 #include <machine/stdarg.h>
58 #ifdef DDB
59 #include <ddb/ddb.h>
60 #include <ddb/db_sym.h>
61 #endif
62
63 /*
64  * Describe an interrupt thread.  There is one of these per interrupt event.
65  */
66 struct intr_thread {
67         struct intr_event *it_event;
68         struct thread *it_thread;       /* Kernel thread. */
69         int     it_flags;               /* (j) IT_* flags. */
70         int     it_need;                /* Needs service. */
71 };
72
73 /* Interrupt thread flags kept in it_flags */
74 #define IT_DEAD         0x000001        /* Thread is waiting to exit. */
75
76 struct  intr_entropy {
77         struct  thread *td;
78         uintptr_t event;
79 };
80
81 struct  intr_event *clk_intr_event;
82 struct  intr_event *tty_intr_event;
83 void    *vm_ih;
84 struct proc *intrproc;
85
86 static MALLOC_DEFINE(M_ITHREAD, "ithread", "Interrupt Threads");
87
88 static int intr_storm_threshold = 1000;
89 TUNABLE_INT("hw.intr_storm_threshold", &intr_storm_threshold);
90 SYSCTL_INT(_hw, OID_AUTO, intr_storm_threshold, CTLFLAG_RW,
91     &intr_storm_threshold, 0,
92     "Number of consecutive interrupts before storm protection is enabled");
93 static TAILQ_HEAD(, intr_event) event_list =
94     TAILQ_HEAD_INITIALIZER(event_list);
95 static struct mtx event_lock;
96 MTX_SYSINIT(intr_event_list, &event_lock, "intr event list", MTX_DEF);
97
98 static void     intr_event_update(struct intr_event *ie);
99 #ifdef INTR_FILTER
100 static int      intr_event_schedule_thread(struct intr_event *ie,
101                     struct intr_thread *ithd);
102 static int      intr_filter_loop(struct intr_event *ie,
103                     struct trapframe *frame, struct intr_thread **ithd);
104 static struct intr_thread *ithread_create(const char *name,
105                               struct intr_handler *ih);
106 #else
107 static int      intr_event_schedule_thread(struct intr_event *ie);
108 static struct intr_thread *ithread_create(const char *name);
109 #endif
110 static void     ithread_destroy(struct intr_thread *ithread);
111 static void     ithread_execute_handlers(struct proc *p, 
112                     struct intr_event *ie);
113 #ifdef INTR_FILTER
114 static void     priv_ithread_execute_handler(struct proc *p, 
115                     struct intr_handler *ih);
116 #endif
117 static void     ithread_loop(void *);
118 static void     ithread_update(struct intr_thread *ithd);
119 static void     start_softintr(void *);
120
121 /* Map an interrupt type to an ithread priority. */
122 u_char
123 intr_priority(enum intr_type flags)
124 {
125         u_char pri;
126
127         flags &= (INTR_TYPE_TTY | INTR_TYPE_BIO | INTR_TYPE_NET |
128             INTR_TYPE_CAM | INTR_TYPE_MISC | INTR_TYPE_CLK | INTR_TYPE_AV);
129         switch (flags) {
130         case INTR_TYPE_TTY:
131                 pri = PI_TTYLOW;
132                 break;
133         case INTR_TYPE_BIO:
134                 /*
135                  * XXX We need to refine this.  BSD/OS distinguishes
136                  * between tape and disk priorities.
137                  */
138                 pri = PI_DISK;
139                 break;
140         case INTR_TYPE_NET:
141                 pri = PI_NET;
142                 break;
143         case INTR_TYPE_CAM:
144                 pri = PI_DISK;          /* XXX or PI_CAM? */
145                 break;
146         case INTR_TYPE_AV:              /* Audio/video */
147                 pri = PI_AV;
148                 break;
149         case INTR_TYPE_CLK:
150                 pri = PI_REALTIME;
151                 break;
152         case INTR_TYPE_MISC:
153                 pri = PI_DULL;          /* don't care */
154                 break;
155         default:
156                 /* We didn't specify an interrupt level. */
157                 panic("intr_priority: no interrupt type in flags");
158         }
159
160         return pri;
161 }
162
163 /*
164  * Update an ithread based on the associated intr_event.
165  */
166 static void
167 ithread_update(struct intr_thread *ithd)
168 {
169         struct intr_event *ie;
170         struct thread *td;
171         u_char pri;
172
173         ie = ithd->it_event;
174         td = ithd->it_thread;
175
176         /* Determine the overall priority of this event. */
177         if (TAILQ_EMPTY(&ie->ie_handlers))
178                 pri = PRI_MAX_ITHD;
179         else
180                 pri = TAILQ_FIRST(&ie->ie_handlers)->ih_pri;
181
182         /* Update name and priority. */
183         strlcpy(td->td_name, ie->ie_fullname, sizeof(td->td_name));
184         thread_lock(td);
185         sched_prio(td, pri);
186         thread_unlock(td);
187 }
188
189 /*
190  * Regenerate the full name of an interrupt event and update its priority.
191  */
192 static void
193 intr_event_update(struct intr_event *ie)
194 {
195         struct intr_handler *ih;
196         char *last;
197         int missed, space;
198
199         /* Start off with no entropy and just the name of the event. */
200         mtx_assert(&ie->ie_lock, MA_OWNED);
201         strlcpy(ie->ie_fullname, ie->ie_name, sizeof(ie->ie_fullname));
202         ie->ie_flags &= ~IE_ENTROPY;
203         missed = 0;
204         space = 1;
205
206         /* Run through all the handlers updating values. */
207         TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) {
208                 if (strlen(ie->ie_fullname) + strlen(ih->ih_name) + 1 <
209                     sizeof(ie->ie_fullname)) {
210                         strcat(ie->ie_fullname, " ");
211                         strcat(ie->ie_fullname, ih->ih_name);
212                         space = 0;
213                 } else
214                         missed++;
215                 if (ih->ih_flags & IH_ENTROPY)
216                         ie->ie_flags |= IE_ENTROPY;
217         }
218
219         /*
220          * If the handler names were too long, add +'s to indicate missing
221          * names. If we run out of room and still have +'s to add, change
222          * the last character from a + to a *.
223          */
224         last = &ie->ie_fullname[sizeof(ie->ie_fullname) - 2];
225         while (missed-- > 0) {
226                 if (strlen(ie->ie_fullname) + 1 == sizeof(ie->ie_fullname)) {
227                         if (*last == '+') {
228                                 *last = '*';
229                                 break;
230                         } else
231                                 *last = '+';
232                 } else if (space) {
233                         strcat(ie->ie_fullname, " +");
234                         space = 0;
235                 } else
236                         strcat(ie->ie_fullname, "+");
237         }
238
239         /*
240          * If this event has an ithread, update it's priority and
241          * name.
242          */
243         if (ie->ie_thread != NULL)
244                 ithread_update(ie->ie_thread);
245         CTR2(KTR_INTR, "%s: updated %s", __func__, ie->ie_fullname);
246 }
247
248 int
249 intr_event_create(struct intr_event **event, void *source,int flags, int irq,
250     void (*pre_ithread)(void *), void (*post_ithread)(void *),
251     void (*post_filter)(void *), int (*assign_cpu)(void *, u_char),
252     const char *fmt, ...)
253 {
254         struct intr_event *ie;
255         va_list ap;
256
257         /* The only valid flag during creation is IE_SOFT. */
258         if ((flags & ~IE_SOFT) != 0)
259                 return (EINVAL);
260         ie = malloc(sizeof(struct intr_event), M_ITHREAD, M_WAITOK | M_ZERO);
261         ie->ie_source = source;
262         ie->ie_pre_ithread = pre_ithread;
263         ie->ie_post_ithread = post_ithread;
264         ie->ie_post_filter = post_filter;
265         ie->ie_assign_cpu = assign_cpu;
266         ie->ie_flags = flags;
267         ie->ie_irq = irq;
268         ie->ie_cpu = NOCPU;
269         TAILQ_INIT(&ie->ie_handlers);
270         mtx_init(&ie->ie_lock, "intr event", NULL, MTX_DEF);
271
272         va_start(ap, fmt);
273         vsnprintf(ie->ie_name, sizeof(ie->ie_name), fmt, ap);
274         va_end(ap);
275         strlcpy(ie->ie_fullname, ie->ie_name, sizeof(ie->ie_fullname));
276         mtx_lock(&event_lock);
277         TAILQ_INSERT_TAIL(&event_list, ie, ie_list);
278         mtx_unlock(&event_lock);
279         if (event != NULL)
280                 *event = ie;
281         CTR2(KTR_INTR, "%s: created %s", __func__, ie->ie_name);
282         return (0);
283 }
284
285 /*
286  * Bind an interrupt event to the specified CPU.  Note that not all
287  * platforms support binding an interrupt to a CPU.  For those
288  * platforms this request will fail.  For supported platforms, any
289  * associated ithreads as well as the primary interrupt context will
290  * be bound to the specificed CPU.  Using a cpu id of NOCPU unbinds
291  * the interrupt event.
292  */
293 int
294 intr_event_bind(struct intr_event *ie, u_char cpu)
295 {
296         cpuset_t mask;
297         lwpid_t id;
298         int error;
299
300         /* Need a CPU to bind to. */
301         if (cpu != NOCPU && CPU_ABSENT(cpu))
302                 return (EINVAL);
303
304         if (ie->ie_assign_cpu == NULL)
305                 return (EOPNOTSUPP);
306         /*
307          * If we have any ithreads try to set their mask first since this
308          * can fail.
309          */
310         mtx_lock(&ie->ie_lock);
311         if (ie->ie_thread != NULL) {
312                 CPU_ZERO(&mask);
313                 if (cpu == NOCPU)
314                         CPU_COPY(cpuset_root, &mask);
315                 else
316                         CPU_SET(cpu, &mask);
317                 id = ie->ie_thread->it_thread->td_tid;
318                 mtx_unlock(&ie->ie_lock);
319                 error = cpuset_setthread(id, &mask);
320                 if (error)
321                         return (error);
322         } else
323                 mtx_unlock(&ie->ie_lock);
324         error = ie->ie_assign_cpu(ie->ie_source, cpu);
325         if (error)
326                 return (error);
327         mtx_lock(&ie->ie_lock);
328         ie->ie_cpu = cpu;
329         mtx_unlock(&ie->ie_lock);
330
331         return (error);
332 }
333
334 static struct intr_event *
335 intr_lookup(int irq)
336 {
337         struct intr_event *ie;
338
339         mtx_lock(&event_lock);
340         TAILQ_FOREACH(ie, &event_list, ie_list)
341                 if (ie->ie_irq == irq &&
342                     (ie->ie_flags & IE_SOFT) == 0 &&
343                     TAILQ_FIRST(&ie->ie_handlers) != NULL)
344                         break;
345         mtx_unlock(&event_lock);
346         return (ie);
347 }
348
349 int
350 intr_setaffinity(int irq, void *m)
351 {
352         struct intr_event *ie;
353         cpuset_t *mask;
354         u_char cpu;
355         int n;
356
357         mask = m;
358         cpu = NOCPU;
359         /*
360          * If we're setting all cpus we can unbind.  Otherwise make sure
361          * only one cpu is in the set.
362          */
363         if (CPU_CMP(cpuset_root, mask)) {
364                 for (n = 0; n < CPU_SETSIZE; n++) {
365                         if (!CPU_ISSET(n, mask))
366                                 continue;
367                         if (cpu != NOCPU)
368                                 return (EINVAL);
369                         cpu = (u_char)n;
370                 }
371         }
372         ie = intr_lookup(irq);
373         if (ie == NULL)
374                 return (ESRCH);
375         intr_event_bind(ie, cpu);
376         return (0);
377 }
378
379 int
380 intr_getaffinity(int irq, void *m)
381 {
382         struct intr_event *ie;
383         cpuset_t *mask;
384
385         mask = m;
386         ie = intr_lookup(irq);
387         if (ie == NULL)
388                 return (ESRCH);
389         CPU_ZERO(mask);
390         mtx_lock(&ie->ie_lock);
391         if (ie->ie_cpu == NOCPU)
392                 CPU_COPY(cpuset_root, mask);
393         else
394                 CPU_SET(ie->ie_cpu, mask);
395         mtx_unlock(&ie->ie_lock);
396         return (0);
397 }
398
399 int
400 intr_event_destroy(struct intr_event *ie)
401 {
402
403         mtx_lock(&event_lock);
404         mtx_lock(&ie->ie_lock);
405         if (!TAILQ_EMPTY(&ie->ie_handlers)) {
406                 mtx_unlock(&ie->ie_lock);
407                 mtx_unlock(&event_lock);
408                 return (EBUSY);
409         }
410         TAILQ_REMOVE(&event_list, ie, ie_list);
411 #ifndef notyet
412         if (ie->ie_thread != NULL) {
413                 ithread_destroy(ie->ie_thread);
414                 ie->ie_thread = NULL;
415         }
416 #endif
417         mtx_unlock(&ie->ie_lock);
418         mtx_unlock(&event_lock);
419         mtx_destroy(&ie->ie_lock);
420         free(ie, M_ITHREAD);
421         return (0);
422 }
423
424 #ifndef INTR_FILTER
425 static struct intr_thread *
426 ithread_create(const char *name)
427 {
428         struct intr_thread *ithd;
429         struct thread *td;
430         int error;
431
432         ithd = malloc(sizeof(struct intr_thread), M_ITHREAD, M_WAITOK | M_ZERO);
433
434         error = kproc_kthread_add(ithread_loop, ithd, &intrproc,
435                     &td, RFSTOPPED | RFHIGHPID,
436                     0, "intr", "%s", name);
437         if (error)
438                 panic("kproc_create() failed with %d", error);
439         thread_lock(td);
440         sched_class(td, PRI_ITHD);
441         TD_SET_IWAIT(td);
442         thread_unlock(td);
443         td->td_pflags |= TDP_ITHREAD;
444         ithd->it_thread = td;
445         CTR2(KTR_INTR, "%s: created %s", __func__, name);
446         return (ithd);
447 }
448 #else
449 static struct intr_thread *
450 ithread_create(const char *name, struct intr_handler *ih)
451 {
452         struct intr_thread *ithd;
453         struct thread *td;
454         int error;
455
456         ithd = malloc(sizeof(struct intr_thread), M_ITHREAD, M_WAITOK | M_ZERO);
457
458         error = kproc_kthread_add(ithread_loop, ih, &intrproc,
459                     &td, RFSTOPPED | RFHIGHPID,
460                     0, "intr", "%s", name);
461         if (error)
462                 panic("kproc_create() failed with %d", error);
463         thread_lock(td);
464         sched_class(td, PRI_ITHD);
465         TD_SET_IWAIT(td);
466         thread_unlock(td);
467         td->td_pflags |= TDP_ITHREAD;
468         ithd->it_thread = td;
469         CTR2(KTR_INTR, "%s: created %s", __func__, name);
470         return (ithd);
471 }
472 #endif
473
474 static void
475 ithread_destroy(struct intr_thread *ithread)
476 {
477         struct thread *td;
478
479         CTR2(KTR_INTR, "%s: killing %s", __func__, ithread->it_event->ie_name);
480         td = ithread->it_thread;
481         thread_lock(td);
482         ithread->it_flags |= IT_DEAD;
483         if (TD_AWAITING_INTR(td)) {
484                 TD_CLR_IWAIT(td);
485                 sched_add(td, SRQ_INTR);
486         }
487         thread_unlock(td);
488 }
489
490 #ifndef INTR_FILTER
491 int
492 intr_event_add_handler(struct intr_event *ie, const char *name,
493     driver_filter_t filter, driver_intr_t handler, void *arg, u_char pri,
494     enum intr_type flags, void **cookiep)
495 {
496         struct intr_handler *ih, *temp_ih;
497         struct intr_thread *it;
498
499         if (ie == NULL || name == NULL || (handler == NULL && filter == NULL))
500                 return (EINVAL);
501
502         /* Allocate and populate an interrupt handler structure. */
503         ih = malloc(sizeof(struct intr_handler), M_ITHREAD, M_WAITOK | M_ZERO);
504         ih->ih_filter = filter;
505         ih->ih_handler = handler;
506         ih->ih_argument = arg;
507         ih->ih_name = name;
508         ih->ih_event = ie;
509         ih->ih_pri = pri;
510         if (flags & INTR_EXCL)
511                 ih->ih_flags = IH_EXCLUSIVE;
512         if (flags & INTR_MPSAFE)
513                 ih->ih_flags |= IH_MPSAFE;
514         if (flags & INTR_ENTROPY)
515                 ih->ih_flags |= IH_ENTROPY;
516
517         /* We can only have one exclusive handler in a event. */
518         mtx_lock(&ie->ie_lock);
519         if (!TAILQ_EMPTY(&ie->ie_handlers)) {
520                 if ((flags & INTR_EXCL) ||
521                     (TAILQ_FIRST(&ie->ie_handlers)->ih_flags & IH_EXCLUSIVE)) {
522                         mtx_unlock(&ie->ie_lock);
523                         free(ih, M_ITHREAD);
524                         return (EINVAL);
525                 }
526         }
527
528         /* Add the new handler to the event in priority order. */
529         TAILQ_FOREACH(temp_ih, &ie->ie_handlers, ih_next) {
530                 if (temp_ih->ih_pri > ih->ih_pri)
531                         break;
532         }
533         if (temp_ih == NULL)
534                 TAILQ_INSERT_TAIL(&ie->ie_handlers, ih, ih_next);
535         else
536                 TAILQ_INSERT_BEFORE(temp_ih, ih, ih_next);
537         intr_event_update(ie);
538
539         /* Create a thread if we need one. */
540         while (ie->ie_thread == NULL && handler != NULL) {
541                 if (ie->ie_flags & IE_ADDING_THREAD)
542                         msleep(ie, &ie->ie_lock, 0, "ithread", 0);
543                 else {
544                         ie->ie_flags |= IE_ADDING_THREAD;
545                         mtx_unlock(&ie->ie_lock);
546                         it = ithread_create("intr: newborn");
547                         mtx_lock(&ie->ie_lock);
548                         ie->ie_flags &= ~IE_ADDING_THREAD;
549                         ie->ie_thread = it;
550                         it->it_event = ie;
551                         ithread_update(it);
552                         wakeup(ie);
553                 }
554         }
555         CTR3(KTR_INTR, "%s: added %s to %s", __func__, ih->ih_name,
556             ie->ie_name);
557         mtx_unlock(&ie->ie_lock);
558
559         if (cookiep != NULL)
560                 *cookiep = ih;
561         return (0);
562 }
563 #else
564 int
565 intr_event_add_handler(struct intr_event *ie, const char *name,
566     driver_filter_t filter, driver_intr_t handler, void *arg, u_char pri,
567     enum intr_type flags, void **cookiep)
568 {
569         struct intr_handler *ih, *temp_ih;
570         struct intr_thread *it;
571
572         if (ie == NULL || name == NULL || (handler == NULL && filter == NULL))
573                 return (EINVAL);
574
575         /* Allocate and populate an interrupt handler structure. */
576         ih = malloc(sizeof(struct intr_handler), M_ITHREAD, M_WAITOK | M_ZERO);
577         ih->ih_filter = filter;
578         ih->ih_handler = handler;
579         ih->ih_argument = arg;
580         ih->ih_name = name;
581         ih->ih_event = ie;
582         ih->ih_pri = pri;
583         if (flags & INTR_EXCL)
584                 ih->ih_flags = IH_EXCLUSIVE;
585         if (flags & INTR_MPSAFE)
586                 ih->ih_flags |= IH_MPSAFE;
587         if (flags & INTR_ENTROPY)
588                 ih->ih_flags |= IH_ENTROPY;
589
590         /* We can only have one exclusive handler in a event. */
591         mtx_lock(&ie->ie_lock);
592         if (!TAILQ_EMPTY(&ie->ie_handlers)) {
593                 if ((flags & INTR_EXCL) ||
594                     (TAILQ_FIRST(&ie->ie_handlers)->ih_flags & IH_EXCLUSIVE)) {
595                         mtx_unlock(&ie->ie_lock);
596                         free(ih, M_ITHREAD);
597                         return (EINVAL);
598                 }
599         }
600
601         /* Add the new handler to the event in priority order. */
602         TAILQ_FOREACH(temp_ih, &ie->ie_handlers, ih_next) {
603                 if (temp_ih->ih_pri > ih->ih_pri)
604                         break;
605         }
606         if (temp_ih == NULL)
607                 TAILQ_INSERT_TAIL(&ie->ie_handlers, ih, ih_next);
608         else
609                 TAILQ_INSERT_BEFORE(temp_ih, ih, ih_next);
610         intr_event_update(ie);
611
612         /* For filtered handlers, create a private ithread to run on. */
613         if (filter != NULL && handler != NULL) { 
614                 mtx_unlock(&ie->ie_lock);
615                 it = ithread_create("intr: newborn", ih);               
616                 mtx_lock(&ie->ie_lock);
617                 it->it_event = ie; 
618                 ih->ih_thread = it;
619                 ithread_update(it); // XXX - do we really need this?!?!?
620         } else { /* Create the global per-event thread if we need one. */
621                 while (ie->ie_thread == NULL && handler != NULL) {
622                         if (ie->ie_flags & IE_ADDING_THREAD)
623                                 msleep(ie, &ie->ie_lock, 0, "ithread", 0);
624                         else {
625                                 ie->ie_flags |= IE_ADDING_THREAD;
626                                 mtx_unlock(&ie->ie_lock);
627                                 it = ithread_create("intr: newborn", ih);
628                                 mtx_lock(&ie->ie_lock);
629                                 ie->ie_flags &= ~IE_ADDING_THREAD;
630                                 ie->ie_thread = it;
631                                 it->it_event = ie;
632                                 ithread_update(it);
633                                 wakeup(ie);
634                         }
635                 }
636         }
637         CTR3(KTR_INTR, "%s: added %s to %s", __func__, ih->ih_name,
638             ie->ie_name);
639         mtx_unlock(&ie->ie_lock);
640
641         if (cookiep != NULL)
642                 *cookiep = ih;
643         return (0);
644 }
645 #endif
646
647 /*
648  * Return the ie_source field from the intr_event an intr_handler is
649  * associated with.
650  */
651 void *
652 intr_handler_source(void *cookie)
653 {
654         struct intr_handler *ih;
655         struct intr_event *ie;
656
657         ih = (struct intr_handler *)cookie;
658         if (ih == NULL)
659                 return (NULL);
660         ie = ih->ih_event;
661         KASSERT(ie != NULL,
662             ("interrupt handler \"%s\" has a NULL interrupt event",
663             ih->ih_name));
664         return (ie->ie_source);
665 }
666
667 #ifndef INTR_FILTER
668 int
669 intr_event_remove_handler(void *cookie)
670 {
671         struct intr_handler *handler = (struct intr_handler *)cookie;
672         struct intr_event *ie;
673 #ifdef INVARIANTS
674         struct intr_handler *ih;
675 #endif
676 #ifdef notyet
677         int dead;
678 #endif
679
680         if (handler == NULL)
681                 return (EINVAL);
682         ie = handler->ih_event;
683         KASSERT(ie != NULL,
684             ("interrupt handler \"%s\" has a NULL interrupt event",
685             handler->ih_name));
686         mtx_lock(&ie->ie_lock);
687         CTR3(KTR_INTR, "%s: removing %s from %s", __func__, handler->ih_name,
688             ie->ie_name);
689 #ifdef INVARIANTS
690         TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next)
691                 if (ih == handler)
692                         goto ok;
693         mtx_unlock(&ie->ie_lock);
694         panic("interrupt handler \"%s\" not found in interrupt event \"%s\"",
695             ih->ih_name, ie->ie_name);
696 ok:
697 #endif
698         /*
699          * If there is no ithread, then just remove the handler and return.
700          * XXX: Note that an INTR_FAST handler might be running on another
701          * CPU!
702          */
703         if (ie->ie_thread == NULL) {
704                 TAILQ_REMOVE(&ie->ie_handlers, handler, ih_next);
705                 mtx_unlock(&ie->ie_lock);
706                 free(handler, M_ITHREAD);
707                 return (0);
708         }
709
710         /*
711          * If the interrupt thread is already running, then just mark this
712          * handler as being dead and let the ithread do the actual removal.
713          *
714          * During a cold boot while cold is set, msleep() does not sleep,
715          * so we have to remove the handler here rather than letting the
716          * thread do it.
717          */
718         thread_lock(ie->ie_thread->it_thread);
719         if (!TD_AWAITING_INTR(ie->ie_thread->it_thread) && !cold) {
720                 handler->ih_flags |= IH_DEAD;
721
722                 /*
723                  * Ensure that the thread will process the handler list
724                  * again and remove this handler if it has already passed
725                  * it on the list.
726                  */
727                 ie->ie_thread->it_need = 1;
728         } else
729                 TAILQ_REMOVE(&ie->ie_handlers, handler, ih_next);
730         thread_unlock(ie->ie_thread->it_thread);
731         while (handler->ih_flags & IH_DEAD)
732                 msleep(handler, &ie->ie_lock, 0, "iev_rmh", 0);
733         intr_event_update(ie);
734 #ifdef notyet
735         /*
736          * XXX: This could be bad in the case of ppbus(8).  Also, I think
737          * this could lead to races of stale data when servicing an
738          * interrupt.
739          */
740         dead = 1;
741         TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) {
742                 if (!(ih->ih_flags & IH_FAST)) {
743                         dead = 0;
744                         break;
745                 }
746         }
747         if (dead) {
748                 ithread_destroy(ie->ie_thread);
749                 ie->ie_thread = NULL;
750         }
751 #endif
752         mtx_unlock(&ie->ie_lock);
753         free(handler, M_ITHREAD);
754         return (0);
755 }
756
757 static int
758 intr_event_schedule_thread(struct intr_event *ie)
759 {
760         struct intr_entropy entropy;
761         struct intr_thread *it;
762         struct thread *td;
763         struct thread *ctd;
764         struct proc *p;
765
766         /*
767          * If no ithread or no handlers, then we have a stray interrupt.
768          */
769         if (ie == NULL || TAILQ_EMPTY(&ie->ie_handlers) ||
770             ie->ie_thread == NULL)
771                 return (EINVAL);
772
773         ctd = curthread;
774         it = ie->ie_thread;
775         td = it->it_thread;
776         p = td->td_proc;
777
778         /*
779          * If any of the handlers for this ithread claim to be good
780          * sources of entropy, then gather some.
781          */
782         if (harvest.interrupt && ie->ie_flags & IE_ENTROPY) {
783                 CTR3(KTR_INTR, "%s: pid %d (%s) gathering entropy", __func__,
784                     p->p_pid, td->td_name);
785                 entropy.event = (uintptr_t)ie;
786                 entropy.td = ctd;
787                 random_harvest(&entropy, sizeof(entropy), 2, 0,
788                     RANDOM_INTERRUPT);
789         }
790
791         KASSERT(p != NULL, ("ithread %s has no process", ie->ie_name));
792
793         /*
794          * Set it_need to tell the thread to keep running if it is already
795          * running.  Then, lock the thread and see if we actually need to
796          * put it on the runqueue.
797          */
798         it->it_need = 1;
799         thread_lock(td);
800         if (TD_AWAITING_INTR(td)) {
801                 CTR3(KTR_INTR, "%s: schedule pid %d (%s)", __func__, p->p_pid,
802                     td->td_name);
803                 TD_CLR_IWAIT(td);
804                 sched_add(td, SRQ_INTR);
805         } else {
806                 CTR5(KTR_INTR, "%s: pid %d (%s): it_need %d, state %d",
807                     __func__, p->p_pid, td->td_name, it->it_need, td->td_state);
808         }
809         thread_unlock(td);
810
811         return (0);
812 }
813 #else
814 int
815 intr_event_remove_handler(void *cookie)
816 {
817         struct intr_handler *handler = (struct intr_handler *)cookie;
818         struct intr_event *ie;
819         struct intr_thread *it;
820 #ifdef INVARIANTS
821         struct intr_handler *ih;
822 #endif
823 #ifdef notyet
824         int dead;
825 #endif
826
827         if (handler == NULL)
828                 return (EINVAL);
829         ie = handler->ih_event;
830         KASSERT(ie != NULL,
831             ("interrupt handler \"%s\" has a NULL interrupt event",
832             handler->ih_name));
833         mtx_lock(&ie->ie_lock);
834         CTR3(KTR_INTR, "%s: removing %s from %s", __func__, handler->ih_name,
835             ie->ie_name);
836 #ifdef INVARIANTS
837         TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next)
838                 if (ih == handler)
839                         goto ok;
840         mtx_unlock(&ie->ie_lock);
841         panic("interrupt handler \"%s\" not found in interrupt event \"%s\"",
842             ih->ih_name, ie->ie_name);
843 ok:
844 #endif
845         /*
846          * If there are no ithreads (per event and per handler), then
847          * just remove the handler and return.  
848          * XXX: Note that an INTR_FAST handler might be running on another CPU!
849          */
850         if (ie->ie_thread == NULL && handler->ih_thread == NULL) {
851                 TAILQ_REMOVE(&ie->ie_handlers, handler, ih_next);
852                 mtx_unlock(&ie->ie_lock);
853                 free(handler, M_ITHREAD);
854                 return (0);
855         }
856
857         /* Private or global ithread? */
858         it = (handler->ih_thread) ? handler->ih_thread : ie->ie_thread;
859         /*
860          * If the interrupt thread is already running, then just mark this
861          * handler as being dead and let the ithread do the actual removal.
862          *
863          * During a cold boot while cold is set, msleep() does not sleep,
864          * so we have to remove the handler here rather than letting the
865          * thread do it.
866          */
867         thread_lock(it->it_thread);
868         if (!TD_AWAITING_INTR(it->it_thread) && !cold) {
869                 handler->ih_flags |= IH_DEAD;
870
871                 /*
872                  * Ensure that the thread will process the handler list
873                  * again and remove this handler if it has already passed
874                  * it on the list.
875                  */
876                 it->it_need = 1;
877         } else
878                 TAILQ_REMOVE(&ie->ie_handlers, handler, ih_next);
879         thread_unlock(it->it_thread);
880         while (handler->ih_flags & IH_DEAD)
881                 msleep(handler, &ie->ie_lock, 0, "iev_rmh", 0);
882         /* 
883          * At this point, the handler has been disconnected from the event,
884          * so we can kill the private ithread if any.
885          */
886         if (handler->ih_thread) {
887                 ithread_destroy(handler->ih_thread);
888                 handler->ih_thread = NULL;
889         }
890         intr_event_update(ie);
891 #ifdef notyet
892         /*
893          * XXX: This could be bad in the case of ppbus(8).  Also, I think
894          * this could lead to races of stale data when servicing an
895          * interrupt.
896          */
897         dead = 1;
898         TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) {
899                 if (handler != NULL) {
900                         dead = 0;
901                         break;
902                 }
903         }
904         if (dead) {
905                 ithread_destroy(ie->ie_thread);
906                 ie->ie_thread = NULL;
907         }
908 #endif
909         mtx_unlock(&ie->ie_lock);
910         free(handler, M_ITHREAD);
911         return (0);
912 }
913
914 static int
915 intr_event_schedule_thread(struct intr_event *ie, struct intr_thread *it)
916 {
917         struct intr_entropy entropy;
918         struct thread *td;
919         struct thread *ctd;
920         struct proc *p;
921
922         /*
923          * If no ithread or no handlers, then we have a stray interrupt.
924          */
925         if (ie == NULL || TAILQ_EMPTY(&ie->ie_handlers) || it == NULL)
926                 return (EINVAL);
927
928         ctd = curthread;
929         td = it->it_thread;
930         p = td->td_proc;
931
932         /*
933          * If any of the handlers for this ithread claim to be good
934          * sources of entropy, then gather some.
935          */
936         if (harvest.interrupt && ie->ie_flags & IE_ENTROPY) {
937                 CTR3(KTR_INTR, "%s: pid %d (%s) gathering entropy", __func__,
938                     p->p_pid, td->td_name);
939                 entropy.event = (uintptr_t)ie;
940                 entropy.td = ctd;
941                 random_harvest(&entropy, sizeof(entropy), 2, 0,
942                     RANDOM_INTERRUPT);
943         }
944
945         KASSERT(p != NULL, ("ithread %s has no process", ie->ie_name));
946
947         /*
948          * Set it_need to tell the thread to keep running if it is already
949          * running.  Then, lock the thread and see if we actually need to
950          * put it on the runqueue.
951          */
952         it->it_need = 1;
953         thread_lock(td);
954         if (TD_AWAITING_INTR(td)) {
955                 CTR3(KTR_INTR, "%s: schedule pid %d (%s)", __func__, p->p_pid,
956                     td->td_name);
957                 TD_CLR_IWAIT(td);
958                 sched_add(td, SRQ_INTR);
959         } else {
960                 CTR5(KTR_INTR, "%s: pid %d (%s): it_need %d, state %d",
961                     __func__, p->p_pid, td->td_name, it->it_need, td->td_state);
962         }
963         thread_unlock(td);
964
965         return (0);
966 }
967 #endif
968
969 /*
970  * Add a software interrupt handler to a specified event.  If a given event
971  * is not specified, then a new event is created.
972  */
973 int
974 swi_add(struct intr_event **eventp, const char *name, driver_intr_t handler,
975             void *arg, int pri, enum intr_type flags, void **cookiep)
976 {
977         struct intr_event *ie;
978         int error;
979
980         if (flags & INTR_ENTROPY)
981                 return (EINVAL);
982
983         ie = (eventp != NULL) ? *eventp : NULL;
984
985         if (ie != NULL) {
986                 if (!(ie->ie_flags & IE_SOFT))
987                         return (EINVAL);
988         } else {
989                 error = intr_event_create(&ie, NULL, IE_SOFT, 0,
990                     NULL, NULL, NULL, NULL, "swi%d:", pri);
991                 if (error)
992                         return (error);
993                 if (eventp != NULL)
994                         *eventp = ie;
995         }
996         error = intr_event_add_handler(ie, name, NULL, handler, arg,
997             (pri * RQ_PPQ) + PI_SOFT, flags, cookiep);
998         if (error)
999                 return (error);
1000         if (pri == SWI_CLOCK) {
1001                 struct proc *p;
1002                 p = ie->ie_thread->it_thread->td_proc;
1003                 PROC_LOCK(p);
1004                 p->p_flag |= P_NOLOAD;
1005                 PROC_UNLOCK(p);
1006         }
1007         return (0);
1008 }
1009
1010 /*
1011  * Schedule a software interrupt thread.
1012  */
1013 void
1014 swi_sched(void *cookie, int flags)
1015 {
1016         struct intr_handler *ih = (struct intr_handler *)cookie;
1017         struct intr_event *ie = ih->ih_event;
1018         int error;
1019
1020         CTR3(KTR_INTR, "swi_sched: %s %s need=%d", ie->ie_name, ih->ih_name,
1021             ih->ih_need);
1022
1023         /*
1024          * Set ih_need for this handler so that if the ithread is already
1025          * running it will execute this handler on the next pass.  Otherwise,
1026          * it will execute it the next time it runs.
1027          */
1028         atomic_store_rel_int(&ih->ih_need, 1);
1029
1030         if (!(flags & SWI_DELAY)) {
1031                 PCPU_INC(cnt.v_soft);
1032 #ifdef INTR_FILTER
1033                 error = intr_event_schedule_thread(ie, ie->ie_thread);
1034 #else
1035                 error = intr_event_schedule_thread(ie);
1036 #endif
1037                 KASSERT(error == 0, ("stray software interrupt"));
1038         }
1039 }
1040
1041 /*
1042  * Remove a software interrupt handler.  Currently this code does not
1043  * remove the associated interrupt event if it becomes empty.  Calling code
1044  * may do so manually via intr_event_destroy(), but that's not really
1045  * an optimal interface.
1046  */
1047 int
1048 swi_remove(void *cookie)
1049 {
1050
1051         return (intr_event_remove_handler(cookie));
1052 }
1053
1054 #ifdef INTR_FILTER
1055 static void
1056 priv_ithread_execute_handler(struct proc *p, struct intr_handler *ih)
1057 {
1058         struct intr_event *ie;
1059
1060         ie = ih->ih_event;
1061         /*
1062          * If this handler is marked for death, remove it from
1063          * the list of handlers and wake up the sleeper.
1064          */
1065         if (ih->ih_flags & IH_DEAD) {
1066                 mtx_lock(&ie->ie_lock);
1067                 TAILQ_REMOVE(&ie->ie_handlers, ih, ih_next);
1068                 ih->ih_flags &= ~IH_DEAD;
1069                 wakeup(ih);
1070                 mtx_unlock(&ie->ie_lock);
1071                 return;
1072         }
1073         
1074         /* Execute this handler. */
1075         CTR6(KTR_INTR, "%s: pid %d exec %p(%p) for %s flg=%x",
1076              __func__, p->p_pid, (void *)ih->ih_handler, ih->ih_argument,
1077              ih->ih_name, ih->ih_flags);
1078         
1079         if (!(ih->ih_flags & IH_MPSAFE))
1080                 mtx_lock(&Giant);
1081         ih->ih_handler(ih->ih_argument);
1082         if (!(ih->ih_flags & IH_MPSAFE))
1083                 mtx_unlock(&Giant);
1084 }
1085 #endif
1086
1087 static void
1088 ithread_execute_handlers(struct proc *p, struct intr_event *ie)
1089 {
1090         struct intr_handler *ih, *ihn;
1091
1092         /* Interrupt handlers should not sleep. */
1093         if (!(ie->ie_flags & IE_SOFT))
1094                 THREAD_NO_SLEEPING();
1095         TAILQ_FOREACH_SAFE(ih, &ie->ie_handlers, ih_next, ihn) {
1096
1097                 /*
1098                  * If this handler is marked for death, remove it from
1099                  * the list of handlers and wake up the sleeper.
1100                  */
1101                 if (ih->ih_flags & IH_DEAD) {
1102                         mtx_lock(&ie->ie_lock);
1103                         TAILQ_REMOVE(&ie->ie_handlers, ih, ih_next);
1104                         ih->ih_flags &= ~IH_DEAD;
1105                         wakeup(ih);
1106                         mtx_unlock(&ie->ie_lock);
1107                         continue;
1108                 }
1109
1110                 /* Skip filter only handlers */
1111                 if (ih->ih_handler == NULL)
1112                         continue;
1113
1114                 /*
1115                  * For software interrupt threads, we only execute
1116                  * handlers that have their need flag set.  Hardware
1117                  * interrupt threads always invoke all of their handlers.
1118                  */
1119                 if (ie->ie_flags & IE_SOFT) {
1120                         if (!ih->ih_need)
1121                                 continue;
1122                         else
1123                                 atomic_store_rel_int(&ih->ih_need, 0);
1124                 }
1125
1126                 /* Execute this handler. */
1127                 CTR6(KTR_INTR, "%s: pid %d exec %p(%p) for %s flg=%x",
1128                     __func__, p->p_pid, (void *)ih->ih_handler, 
1129                     ih->ih_argument, ih->ih_name, ih->ih_flags);
1130
1131                 if (!(ih->ih_flags & IH_MPSAFE))
1132                         mtx_lock(&Giant);
1133                 ih->ih_handler(ih->ih_argument);
1134                 if (!(ih->ih_flags & IH_MPSAFE))
1135                         mtx_unlock(&Giant);
1136         }
1137         if (!(ie->ie_flags & IE_SOFT))
1138                 THREAD_SLEEPING_OK();
1139
1140         /*
1141          * Interrupt storm handling:
1142          *
1143          * If this interrupt source is currently storming, then throttle
1144          * it to only fire the handler once  per clock tick.
1145          *
1146          * If this interrupt source is not currently storming, but the
1147          * number of back to back interrupts exceeds the storm threshold,
1148          * then enter storming mode.
1149          */
1150         if (intr_storm_threshold != 0 && ie->ie_count >= intr_storm_threshold &&
1151             !(ie->ie_flags & IE_SOFT)) {
1152                 /* Report the message only once every second. */
1153                 if (ppsratecheck(&ie->ie_warntm, &ie->ie_warncnt, 1)) {
1154                         printf(
1155         "interrupt storm detected on \"%s\"; throttling interrupt source\n",
1156                             ie->ie_name);
1157                 }
1158                 pause("istorm", 1);
1159         } else
1160                 ie->ie_count++;
1161
1162         /*
1163          * Now that all the handlers have had a chance to run, reenable
1164          * the interrupt source.
1165          */
1166         if (ie->ie_post_ithread != NULL)
1167                 ie->ie_post_ithread(ie->ie_source);
1168 }
1169
1170 #ifndef INTR_FILTER
1171 /*
1172  * This is the main code for interrupt threads.
1173  */
1174 static void
1175 ithread_loop(void *arg)
1176 {
1177         struct intr_thread *ithd;
1178         struct intr_event *ie;
1179         struct thread *td;
1180         struct proc *p;
1181
1182         td = curthread;
1183         p = td->td_proc;
1184         ithd = (struct intr_thread *)arg;
1185         KASSERT(ithd->it_thread == td,
1186             ("%s: ithread and proc linkage out of sync", __func__));
1187         ie = ithd->it_event;
1188         ie->ie_count = 0;
1189
1190         /*
1191          * As long as we have interrupts outstanding, go through the
1192          * list of handlers, giving each one a go at it.
1193          */
1194         for (;;) {
1195                 /*
1196                  * If we are an orphaned thread, then just die.
1197                  */
1198                 if (ithd->it_flags & IT_DEAD) {
1199                         CTR3(KTR_INTR, "%s: pid %d (%s) exiting", __func__,
1200                             p->p_pid, td->td_name);
1201                         free(ithd, M_ITHREAD);
1202                         kthread_exit();
1203                 }
1204
1205                 /*
1206                  * Service interrupts.  If another interrupt arrives while
1207                  * we are running, it will set it_need to note that we
1208                  * should make another pass.
1209                  */
1210                 while (ithd->it_need) {
1211                         /*
1212                          * This might need a full read and write barrier
1213                          * to make sure that this write posts before any
1214                          * of the memory or device accesses in the
1215                          * handlers.
1216                          */
1217                         atomic_store_rel_int(&ithd->it_need, 0);
1218                         ithread_execute_handlers(p, ie);
1219                 }
1220                 WITNESS_WARN(WARN_PANIC, NULL, "suspending ithread");
1221                 mtx_assert(&Giant, MA_NOTOWNED);
1222
1223                 /*
1224                  * Processed all our interrupts.  Now get the sched
1225                  * lock.  This may take a while and it_need may get
1226                  * set again, so we have to check it again.
1227                  */
1228                 thread_lock(td);
1229                 if (!ithd->it_need && !(ithd->it_flags & IT_DEAD)) {
1230                         TD_SET_IWAIT(td);
1231                         ie->ie_count = 0;
1232                         mi_switch(SW_VOL | SWT_IWAIT, NULL);
1233                 }
1234                 thread_unlock(td);
1235         }
1236 }
1237
1238 /*
1239  * Main interrupt handling body.
1240  *
1241  * Input:
1242  * o ie:                        the event connected to this interrupt.
1243  * o frame:                     some archs (i.e. i386) pass a frame to some.
1244  *                              handlers as their main argument.
1245  * Return value:
1246  * o 0:                         everything ok.
1247  * o EINVAL:                    stray interrupt.
1248  */
1249 int
1250 intr_event_handle(struct intr_event *ie, struct trapframe *frame)
1251 {
1252         struct intr_handler *ih;
1253         struct thread *td;
1254         int error, ret, thread;
1255
1256         td = curthread;
1257
1258         /* An interrupt with no event or handlers is a stray interrupt. */
1259         if (ie == NULL || TAILQ_EMPTY(&ie->ie_handlers))
1260                 return (EINVAL);
1261
1262         /*
1263          * Execute fast interrupt handlers directly.
1264          * To support clock handlers, if a handler registers
1265          * with a NULL argument, then we pass it a pointer to
1266          * a trapframe as its argument.
1267          */
1268         td->td_intr_nesting_level++;
1269         thread = 0;
1270         ret = 0;
1271         critical_enter();
1272         TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) {
1273                 if (ih->ih_filter == NULL) {
1274                         thread = 1;
1275                         continue;
1276                 }
1277                 CTR4(KTR_INTR, "%s: exec %p(%p) for %s", __func__,
1278                     ih->ih_filter, ih->ih_argument == NULL ? frame :
1279                     ih->ih_argument, ih->ih_name);
1280                 if (ih->ih_argument == NULL)
1281                         ret = ih->ih_filter(frame);
1282                 else
1283                         ret = ih->ih_filter(ih->ih_argument);
1284                 /* 
1285                  * Wrapper handler special handling:
1286                  *
1287                  * in some particular cases (like pccard and pccbb), 
1288                  * the _real_ device handler is wrapped in a couple of
1289                  * functions - a filter wrapper and an ithread wrapper.
1290                  * In this case (and just in this case), the filter wrapper 
1291                  * could ask the system to schedule the ithread and mask
1292                  * the interrupt source if the wrapped handler is composed
1293                  * of just an ithread handler.
1294                  *
1295                  * TODO: write a generic wrapper to avoid people rolling 
1296                  * their own
1297                  */
1298                 if (!thread) {
1299                         if (ret == FILTER_SCHEDULE_THREAD)
1300                                 thread = 1;
1301                 }
1302         }
1303
1304         if (thread) {
1305                 if (ie->ie_pre_ithread != NULL)
1306                         ie->ie_pre_ithread(ie->ie_source);
1307         } else {
1308                 if (ie->ie_post_filter != NULL)
1309                         ie->ie_post_filter(ie->ie_source);
1310         }
1311         
1312         /* Schedule the ithread if needed. */
1313         if (thread) {
1314                 error = intr_event_schedule_thread(ie);
1315                 KASSERT(error == 0, ("bad stray interrupt"));
1316         }
1317         critical_exit();
1318         td->td_intr_nesting_level--;
1319         return (0);
1320 }
1321 #else
1322 /*
1323  * This is the main code for interrupt threads.
1324  */
1325 static void
1326 ithread_loop(void *arg)
1327 {
1328         struct intr_thread *ithd;
1329         struct intr_handler *ih;
1330         struct intr_event *ie;
1331         struct thread *td;
1332         struct proc *p;
1333         int priv;
1334
1335         td = curthread;
1336         p = td->td_proc;
1337         ih = (struct intr_handler *)arg;
1338         priv = (ih->ih_thread != NULL) ? 1 : 0;
1339         ithd = (priv) ? ih->ih_thread : ih->ih_event->ie_thread;
1340         KASSERT(ithd->it_thread == td,
1341             ("%s: ithread and proc linkage out of sync", __func__));
1342         ie = ithd->it_event;
1343         ie->ie_count = 0;
1344
1345         /*
1346          * As long as we have interrupts outstanding, go through the
1347          * list of handlers, giving each one a go at it.
1348          */
1349         for (;;) {
1350                 /*
1351                  * If we are an orphaned thread, then just die.
1352                  */
1353                 if (ithd->it_flags & IT_DEAD) {
1354                         CTR3(KTR_INTR, "%s: pid %d (%s) exiting", __func__,
1355                             p->p_pid, td->td_name);
1356                         free(ithd, M_ITHREAD);
1357                         kthread_exit();
1358                 }
1359
1360                 /*
1361                  * Service interrupts.  If another interrupt arrives while
1362                  * we are running, it will set it_need to note that we
1363                  * should make another pass.
1364                  */
1365                 while (ithd->it_need) {
1366                         /*
1367                          * This might need a full read and write barrier
1368                          * to make sure that this write posts before any
1369                          * of the memory or device accesses in the
1370                          * handlers.
1371                          */
1372                         atomic_store_rel_int(&ithd->it_need, 0);
1373                         if (priv)
1374                                 priv_ithread_execute_handler(p, ih);
1375                         else 
1376                                 ithread_execute_handlers(p, ie);
1377                 }
1378                 WITNESS_WARN(WARN_PANIC, NULL, "suspending ithread");
1379                 mtx_assert(&Giant, MA_NOTOWNED);
1380
1381                 /*
1382                  * Processed all our interrupts.  Now get the sched
1383                  * lock.  This may take a while and it_need may get
1384                  * set again, so we have to check it again.
1385                  */
1386                 thread_lock(td);
1387                 if (!ithd->it_need && !(ithd->it_flags & IT_DEAD)) {
1388                         TD_SET_IWAIT(td);
1389                         ie->ie_count = 0;
1390                         mi_switch(SW_VOL | SWT_IWAIT, NULL);
1391                 }
1392                 thread_unlock(td);
1393         }
1394 }
1395
1396 /* 
1397  * Main loop for interrupt filter.
1398  *
1399  * Some architectures (i386, amd64 and arm) require the optional frame 
1400  * parameter, and use it as the main argument for fast handler execution
1401  * when ih_argument == NULL.
1402  *
1403  * Return value:
1404  * o FILTER_STRAY:              No filter recognized the event, and no
1405  *                              filter-less handler is registered on this 
1406  *                              line.
1407  * o FILTER_HANDLED:            A filter claimed the event and served it.
1408  * o FILTER_SCHEDULE_THREAD:    No filter claimed the event, but there's at
1409  *                              least one filter-less handler on this line.
1410  * o FILTER_HANDLED | 
1411  *   FILTER_SCHEDULE_THREAD:    A filter claimed the event, and asked for
1412  *                              scheduling the per-handler ithread.
1413  *
1414  * In case an ithread has to be scheduled, in *ithd there will be a 
1415  * pointer to a struct intr_thread containing the thread to be
1416  * scheduled.
1417  */
1418
1419 static int
1420 intr_filter_loop(struct intr_event *ie, struct trapframe *frame, 
1421                  struct intr_thread **ithd) 
1422 {
1423         struct intr_handler *ih;
1424         void *arg;
1425         int ret, thread_only;
1426
1427         ret = 0;
1428         thread_only = 0;
1429         TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) {
1430                 /*
1431                  * Execute fast interrupt handlers directly.
1432                  * To support clock handlers, if a handler registers
1433                  * with a NULL argument, then we pass it a pointer to
1434                  * a trapframe as its argument.
1435                  */
1436                 arg = ((ih->ih_argument == NULL) ? frame : ih->ih_argument);
1437                 
1438                 CTR5(KTR_INTR, "%s: exec %p/%p(%p) for %s", __func__,
1439                      ih->ih_filter, ih->ih_handler, arg, ih->ih_name);
1440
1441                 if (ih->ih_filter != NULL)
1442                         ret = ih->ih_filter(arg);
1443                 else {
1444                         thread_only = 1;
1445                         continue;
1446                 }
1447
1448                 if (ret & FILTER_STRAY)
1449                         continue;
1450                 else { 
1451                         *ithd = ih->ih_thread;
1452                         return (ret);
1453                 }
1454         }
1455
1456         /*
1457          * No filters handled the interrupt and we have at least
1458          * one handler without a filter.  In this case, we schedule
1459          * all of the filter-less handlers to run in the ithread.
1460          */     
1461         if (thread_only) {
1462                 *ithd = ie->ie_thread;
1463                 return (FILTER_SCHEDULE_THREAD);
1464         }
1465         return (FILTER_STRAY);
1466 }
1467
1468 /*
1469  * Main interrupt handling body.
1470  *
1471  * Input:
1472  * o ie:                        the event connected to this interrupt.
1473  * o frame:                     some archs (i.e. i386) pass a frame to some.
1474  *                              handlers as their main argument.
1475  * Return value:
1476  * o 0:                         everything ok.
1477  * o EINVAL:                    stray interrupt.
1478  */
1479 int
1480 intr_event_handle(struct intr_event *ie, struct trapframe *frame)
1481 {
1482         struct intr_thread *ithd;
1483         struct thread *td;
1484         int thread;
1485
1486         ithd = NULL;
1487         td = curthread;
1488
1489         if (ie == NULL || TAILQ_EMPTY(&ie->ie_handlers))
1490                 return (EINVAL);
1491
1492         td->td_intr_nesting_level++;
1493         thread = 0;
1494         critical_enter();
1495         thread = intr_filter_loop(ie, frame, &ithd);    
1496         if (thread & FILTER_HANDLED) {
1497                 if (ie->ie_post_filter != NULL)
1498                         ie->ie_post_filter(ie->ie_source);
1499         } else {
1500                 if (ie->ie_pre_ithread != NULL)
1501                         ie->ie_pre_ithread(ie->ie_source);
1502         }
1503         critical_exit();
1504         
1505         /* Interrupt storm logic */
1506         if (thread & FILTER_STRAY) {
1507                 ie->ie_count++;
1508                 if (ie->ie_count < intr_storm_threshold)
1509                         printf("Interrupt stray detection not present\n");
1510         }
1511
1512         /* Schedule an ithread if needed. */
1513         if (thread & FILTER_SCHEDULE_THREAD) {
1514                 if (intr_event_schedule_thread(ie, ithd) != 0)
1515                         panic("%s: impossible stray interrupt", __func__);
1516         }
1517         td->td_intr_nesting_level--;
1518         return (0);
1519 }
1520 #endif
1521
1522 #ifdef DDB
1523 /*
1524  * Dump details about an interrupt handler
1525  */
1526 static void
1527 db_dump_intrhand(struct intr_handler *ih)
1528 {
1529         int comma;
1530
1531         db_printf("\t%-10s ", ih->ih_name);
1532         switch (ih->ih_pri) {
1533         case PI_REALTIME:
1534                 db_printf("CLK ");
1535                 break;
1536         case PI_AV:
1537                 db_printf("AV  ");
1538                 break;
1539         case PI_TTYHIGH:
1540         case PI_TTYLOW:
1541                 db_printf("TTY ");
1542                 break;
1543         case PI_TAPE:
1544                 db_printf("TAPE");
1545                 break;
1546         case PI_NET:
1547                 db_printf("NET ");
1548                 break;
1549         case PI_DISK:
1550         case PI_DISKLOW:
1551                 db_printf("DISK");
1552                 break;
1553         case PI_DULL:
1554                 db_printf("DULL");
1555                 break;
1556         default:
1557                 if (ih->ih_pri >= PI_SOFT)
1558                         db_printf("SWI ");
1559                 else
1560                         db_printf("%4u", ih->ih_pri);
1561                 break;
1562         }
1563         db_printf(" ");
1564         db_printsym((uintptr_t)ih->ih_handler, DB_STGY_PROC);
1565         db_printf("(%p)", ih->ih_argument);
1566         if (ih->ih_need ||
1567             (ih->ih_flags & (IH_EXCLUSIVE | IH_ENTROPY | IH_DEAD |
1568             IH_MPSAFE)) != 0) {
1569                 db_printf(" {");
1570                 comma = 0;
1571                 if (ih->ih_flags & IH_EXCLUSIVE) {
1572                         if (comma)
1573                                 db_printf(", ");
1574                         db_printf("EXCL");
1575                         comma = 1;
1576                 }
1577                 if (ih->ih_flags & IH_ENTROPY) {
1578                         if (comma)
1579                                 db_printf(", ");
1580                         db_printf("ENTROPY");
1581                         comma = 1;
1582                 }
1583                 if (ih->ih_flags & IH_DEAD) {
1584                         if (comma)
1585                                 db_printf(", ");
1586                         db_printf("DEAD");
1587                         comma = 1;
1588                 }
1589                 if (ih->ih_flags & IH_MPSAFE) {
1590                         if (comma)
1591                                 db_printf(", ");
1592                         db_printf("MPSAFE");
1593                         comma = 1;
1594                 }
1595                 if (ih->ih_need) {
1596                         if (comma)
1597                                 db_printf(", ");
1598                         db_printf("NEED");
1599                 }
1600                 db_printf("}");
1601         }
1602         db_printf("\n");
1603 }
1604
1605 /*
1606  * Dump details about a event.
1607  */
1608 void
1609 db_dump_intr_event(struct intr_event *ie, int handlers)
1610 {
1611         struct intr_handler *ih;
1612         struct intr_thread *it;
1613         int comma;
1614
1615         db_printf("%s ", ie->ie_fullname);
1616         it = ie->ie_thread;
1617         if (it != NULL)
1618                 db_printf("(pid %d)", it->it_thread->td_proc->p_pid);
1619         else
1620                 db_printf("(no thread)");
1621         if ((ie->ie_flags & (IE_SOFT | IE_ENTROPY | IE_ADDING_THREAD)) != 0 ||
1622             (it != NULL && it->it_need)) {
1623                 db_printf(" {");
1624                 comma = 0;
1625                 if (ie->ie_flags & IE_SOFT) {
1626                         db_printf("SOFT");
1627                         comma = 1;
1628                 }
1629                 if (ie->ie_flags & IE_ENTROPY) {
1630                         if (comma)
1631                                 db_printf(", ");
1632                         db_printf("ENTROPY");
1633                         comma = 1;
1634                 }
1635                 if (ie->ie_flags & IE_ADDING_THREAD) {
1636                         if (comma)
1637                                 db_printf(", ");
1638                         db_printf("ADDING_THREAD");
1639                         comma = 1;
1640                 }
1641                 if (it != NULL && it->it_need) {
1642                         if (comma)
1643                                 db_printf(", ");
1644                         db_printf("NEED");
1645                 }
1646                 db_printf("}");
1647         }
1648         db_printf("\n");
1649
1650         if (handlers)
1651                 TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next)
1652                     db_dump_intrhand(ih);
1653 }
1654
1655 /*
1656  * Dump data about interrupt handlers
1657  */
1658 DB_SHOW_COMMAND(intr, db_show_intr)
1659 {
1660         struct intr_event *ie;
1661         int all, verbose;
1662
1663         verbose = index(modif, 'v') != NULL;
1664         all = index(modif, 'a') != NULL;
1665         TAILQ_FOREACH(ie, &event_list, ie_list) {
1666                 if (!all && TAILQ_EMPTY(&ie->ie_handlers))
1667                         continue;
1668                 db_dump_intr_event(ie, verbose);
1669                 if (db_pager_quit)
1670                         break;
1671         }
1672 }
1673 #endif /* DDB */
1674
1675 /*
1676  * Start standard software interrupt threads
1677  */
1678 static void
1679 start_softintr(void *dummy)
1680 {
1681
1682         if (swi_add(NULL, "vm", swi_vm, NULL, SWI_VM, INTR_MPSAFE, &vm_ih))
1683                 panic("died while creating vm swi ithread");
1684 }
1685 SYSINIT(start_softintr, SI_SUB_SOFTINTR, SI_ORDER_FIRST, start_softintr,
1686     NULL);
1687
1688 /*
1689  * Sysctls used by systat and others: hw.intrnames and hw.intrcnt.
1690  * The data for this machine dependent, and the declarations are in machine
1691  * dependent code.  The layout of intrnames and intrcnt however is machine
1692  * independent.
1693  *
1694  * We do not know the length of intrcnt and intrnames at compile time, so
1695  * calculate things at run time.
1696  */
1697 static int
1698 sysctl_intrnames(SYSCTL_HANDLER_ARGS)
1699 {
1700         return (sysctl_handle_opaque(oidp, intrnames, eintrnames - intrnames,
1701            req));
1702 }
1703
1704 SYSCTL_PROC(_hw, OID_AUTO, intrnames, CTLTYPE_OPAQUE | CTLFLAG_RD,
1705     NULL, 0, sysctl_intrnames, "", "Interrupt Names");
1706
1707 static int
1708 sysctl_intrcnt(SYSCTL_HANDLER_ARGS)
1709 {
1710         return (sysctl_handle_opaque(oidp, intrcnt,
1711             (char *)eintrcnt - (char *)intrcnt, req));
1712 }
1713
1714 SYSCTL_PROC(_hw, OID_AUTO, intrcnt, CTLTYPE_OPAQUE | CTLFLAG_RD,
1715     NULL, 0, sysctl_intrcnt, "", "Interrupt Counts");
1716
1717 #ifdef DDB
1718 /*
1719  * DDB command to dump the interrupt statistics.
1720  */
1721 DB_SHOW_COMMAND(intrcnt, db_show_intrcnt)
1722 {
1723         u_long *i;
1724         char *cp;
1725
1726         cp = intrnames;
1727         for (i = intrcnt; i != eintrcnt && !db_pager_quit; i++) {
1728                 if (*cp == '\0')
1729                         break;
1730                 if (*i != 0)
1731                         db_printf("%s\t%lu\n", cp, *i);
1732                 cp += strlen(cp) + 1;
1733         }
1734 }
1735 #endif