FreeBSD/Linux Kernel Cross Reference
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: src/sys/kern/kern_intr.c,v 1.166 2008/07/18 07:07:57 kmacy Exp $");
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 |