2 * Copyright (c) 1997, Stefan Esser <se@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/param.h>
33 #include <sys/rtprio.h>
34 #include <sys/systm.h>
35 #include <sys/interrupt.h>
36 #include <sys/kernel.h>
37 #include <sys/kthread.h>
40 #include <sys/malloc.h>
41 #include <sys/mutex.h>
43 #include <sys/random.h>
44 #include <sys/resourcevar.h>
45 #include <sys/sysctl.h>
46 #include <sys/unistd.h>
47 #include <sys/vmmeter.h>
48 #include <machine/atomic.h>
49 #include <machine/cpu.h>
50 #include <machine/md_var.h>
51 #include <machine/stdarg.h>
53 #include <net/netisr.h> /* prototype for legacy_setsoftnet */
63 struct ithd *clk_ithd;
64 struct ithd *tty_ithd;
66 static MALLOC_DEFINE(M_ITHREAD, "ithread", "Interrupt Threads");
68 static void ithread_update(struct ithd *);
69 static void ithread_loop(void *);
70 static void start_softintr(void *);
71 static void swi_net(void *);
74 ithread_priority(enum intr_type flags)
78 flags &= (INTR_TYPE_TTY | INTR_TYPE_BIO | INTR_TYPE_NET |
79 INTR_TYPE_CAM | INTR_TYPE_MISC | INTR_TYPE_CLK | INTR_TYPE_AV);
86 * XXX We need to refine this. BSD/OS distinguishes
87 * between tape and disk priorities.
95 pri = PI_DISK; /* XXX or PI_CAM? */
97 case INTR_TYPE_AV: /* Audio/video */
104 pri = PI_DULL; /* don't care */
107 /* We didn't specify an interrupt level. */
108 panic("ithread_priority: no interrupt type in flags");
115 * Regenerate the name (p_comm) and priority for a threaded interrupt thread.
118 ithread_update(struct ithd *ithd)
125 mtx_assert(&ithd->it_lock, MA_OWNED);
131 strncpy(p->p_comm, ithd->it_name, sizeof(ithd->it_name));
132 ih = TAILQ_FIRST(&ithd->it_handlers);
134 mtx_lock_spin(&sched_lock);
135 td->td_priority = PRI_MAX_ITHD;
136 td->td_base_pri = PRI_MAX_ITHD;
137 mtx_unlock_spin(&sched_lock);
138 ithd->it_flags &= ~IT_ENTROPY;
142 mtx_lock_spin(&sched_lock);
143 td->td_priority = ih->ih_pri;
144 td->td_base_pri = ih->ih_pri;
145 mtx_unlock_spin(&sched_lock);
146 TAILQ_FOREACH(ih, &ithd->it_handlers, ih_next) {
147 if (strlen(p->p_comm) + strlen(ih->ih_name) + 1 <
149 strcat(p->p_comm, " ");
150 strcat(p->p_comm, ih->ih_name);
151 } else if (strlen(p->p_comm) + 1 == sizeof(p->p_comm)) {
152 if (p->p_comm[sizeof(p->p_comm) - 2] == '+')
153 p->p_comm[sizeof(p->p_comm) - 2] = '*';
155 p->p_comm[sizeof(p->p_comm) - 2] = '+';
157 strcat(p->p_comm, "+");
158 if (ih->ih_flags & IH_ENTROPY)
162 ithd->it_flags |= IT_ENTROPY;
164 ithd->it_flags &= ~IT_ENTROPY;
165 CTR2(KTR_INTR, "%s: updated %s\n", __func__, p->p_comm);
169 ithread_create(struct ithd **ithread, int vector, int flags,
170 void (*disable)(int), void (*enable)(int), const char *fmt, ...)
178 /* The only valid flag during creation is IT_SOFT. */
179 if ((flags & ~IT_SOFT) != 0)
182 ithd = malloc(sizeof(struct ithd), M_ITHREAD, M_WAITOK | M_ZERO);
183 ithd->it_vector = vector;
184 ithd->it_disable = disable;
185 ithd->it_enable = enable;
186 ithd->it_flags = flags;
187 TAILQ_INIT(&ithd->it_handlers);
188 mtx_init(&ithd->it_lock, "ithread", NULL, MTX_DEF);
191 vsnprintf(ithd->it_name, sizeof(ithd->it_name), fmt, ap);
194 error = kthread_create(ithread_loop, ithd, &p, RFSTOPPED | RFHIGHPID,
195 "%s", ithd->it_name);
197 mtx_destroy(&ithd->it_lock);
198 free(ithd, M_ITHREAD);
201 td = FIRST_THREAD_IN_PROC(p); /* XXXKSE */
202 td->td_ksegrp->kg_pri_class = PRI_ITHD;
203 td->td_priority = PRI_MAX_ITHD;
210 CTR2(KTR_INTR, "%s: created %s", __func__, ithd->it_name);
215 ithread_destroy(struct ithd *ithread)
225 mtx_lock(&ithread->it_lock);
226 if (!TAILQ_EMPTY(&ithread->it_handlers)) {
227 mtx_unlock(&ithread->it_lock);
230 ithread->it_flags |= IT_DEAD;
231 mtx_lock_spin(&sched_lock);
232 if (p->p_stat == SWAIT) {
233 p->p_stat = SRUN; /* XXXKSE */
236 mtx_unlock_spin(&sched_lock);
237 mtx_unlock(&ithread->it_lock);
238 CTR2(KTR_INTR, "%s: killing %s", __func__, ithread->it_name);
243 ithread_add_handler(struct ithd* ithread, const char *name,
244 driver_intr_t handler, void *arg, u_char pri, enum intr_type flags,
247 struct intrhand *ih, *temp_ih;
249 if (ithread == NULL || name == NULL || handler == NULL)
251 if ((flags & INTR_FAST) !=0)
254 ih = malloc(sizeof(struct intrhand), M_ITHREAD, M_WAITOK | M_ZERO);
255 ih->ih_handler = handler;
256 ih->ih_argument = arg;
258 ih->ih_ithread = ithread;
260 if (flags & INTR_FAST)
261 ih->ih_flags = IH_FAST | IH_EXCLUSIVE;
262 else if (flags & INTR_EXCL)
263 ih->ih_flags = IH_EXCLUSIVE;
264 if (flags & INTR_MPSAFE)
265 ih->ih_flags |= IH_MPSAFE;
266 if (flags & INTR_ENTROPY)
267 ih->ih_flags |= IH_ENTROPY;
269 mtx_lock(&ithread->it_lock);
270 if ((flags & INTR_EXCL) !=0 && !TAILQ_EMPTY(&ithread->it_handlers))
272 if (!TAILQ_EMPTY(&ithread->it_handlers) &&
273 (TAILQ_FIRST(&ithread->it_handlers)->ih_flags & IH_EXCLUSIVE) != 0)
276 TAILQ_FOREACH(temp_ih, &ithread->it_handlers, ih_next)
277 if (temp_ih->ih_pri > ih->ih_pri)
280 TAILQ_INSERT_TAIL(&ithread->it_handlers, ih, ih_next);
282 TAILQ_INSERT_BEFORE(temp_ih, ih, ih_next);
283 ithread_update(ithread);
284 mtx_unlock(&ithread->it_lock);
288 CTR3(KTR_INTR, "%s: added %s to %s", __func__, ih->ih_name,
293 mtx_unlock(&ithread->it_lock);
299 ithread_remove_handler(void *cookie)
301 struct intrhand *handler = (struct intrhand *)cookie;
302 struct ithd *ithread;
309 ithread = handler->ih_ithread;
310 KASSERT(ithread != NULL,
311 ("interrupt handler \"%s\" has a NULL interrupt thread",
313 CTR3(KTR_INTR, "%s: removing %s from %s", __func__, handler->ih_name,
315 mtx_lock(&ithread->it_lock);
317 TAILQ_FOREACH(ih, &ithread->it_handlers, ih_next)
320 mtx_unlock(&ithread->it_lock);
321 panic("interrupt handler \"%s\" not found in interrupt thread \"%s\"",
322 ih->ih_name, ithread->it_name);
326 * If the interrupt thread is already running, then just mark this
327 * handler as being dead and let the ithread do the actual removal.
329 mtx_lock_spin(&sched_lock);
330 if (ithread->it_td->td_proc->p_stat != SWAIT) {
331 handler->ih_flags |= IH_DEAD;
334 * Ensure that the thread will process the handler list
335 * again and remove this handler if it has already passed
338 ithread->it_need = 1;
340 TAILQ_REMOVE(&ithread->it_handlers, handler, ih_next);
341 mtx_unlock_spin(&sched_lock);
342 if ((handler->ih_flags & IH_DEAD) != 0)
343 msleep(handler, &ithread->it_lock, PUSER, "itrmh", 0);
344 ithread_update(ithread);
345 mtx_unlock(&ithread->it_lock);
346 free(handler, M_ITHREAD);
351 ithread_schedule(struct ithd *ithread, int do_switch)
353 struct int_entropy entropy;
358 * If no ithread or no handlers, then we have a stray interrupt.
360 if ((ithread == NULL) || TAILQ_EMPTY(&ithread->it_handlers))
364 * If any of the handlers for this ithread claim to be good
365 * sources of entropy, then gather some.
367 if (harvest.interrupt && ithread->it_flags & IT_ENTROPY) {
368 entropy.vector = ithread->it_vector;
369 entropy.proc = curthread->td_proc;;
370 random_harvest(&entropy, sizeof(entropy), 2, 0,
376 KASSERT(p != NULL, ("ithread %s has no process", ithread->it_name));
377 CTR4(KTR_INTR, "%s: pid %d: (%s) need = %d", __func__, p->p_pid, p->p_comm,
381 * Set it_need to tell the thread to keep running if it is already
382 * running. Then, grab sched_lock and see if we actually need to
383 * put this thread on the runqueue. If so and the do_switch flag is
384 * true and it is safe to switch, then switch to the ithread
385 * immediately. Otherwise, set the needresched flag to guarantee
386 * that this ithread will run before any userland processes.
388 ithread->it_need = 1;
389 mtx_lock_spin(&sched_lock);
390 if (p->p_stat == SWAIT) {
391 CTR2(KTR_INTR, "%s: setrunqueue %d", __func__, p->p_pid);
393 setrunqueue(td); /* XXXKSE */
394 if (do_switch && curthread->td_critnest == 1 &&
395 curthread->td_proc->p_stat == SRUN) {
396 if (curthread != PCPU_GET(idlethread))
397 setrunqueue(curthread);
398 curthread->td_proc->p_stats->p_ru.ru_nivcsw++;
401 curthread->td_kse->ke_flags |= KEF_NEEDRESCHED;
403 CTR4(KTR_INTR, "%s: pid %d: it_need %d, state %d",
404 __func__, p->p_pid, ithread->it_need, p->p_stat);
406 mtx_unlock_spin(&sched_lock);
412 swi_add(struct ithd **ithdp, const char *name, driver_intr_t handler,
413 void *arg, int pri, enum intr_type flags, void **cookiep)
418 if (flags & (INTR_FAST | INTR_ENTROPY))
421 ithd = (ithdp != NULL) ? *ithdp : NULL;
424 if ((ithd->it_flags & IT_SOFT) == 0)
427 error = ithread_create(&ithd, pri, IT_SOFT, NULL, NULL,
435 return (ithread_add_handler(ithd, name, handler, arg,
436 (pri * RQ_PPQ) + PI_SOFT, flags, cookiep));
441 * Schedule a heavyweight software interrupt process.
444 swi_sched(void *cookie, int flags)
446 struct intrhand *ih = (struct intrhand *)cookie;
447 struct ithd *it = ih->ih_ithread;
450 atomic_add_int(&cnt.v_intr, 1); /* one more global interrupt */
452 CTR3(KTR_INTR, "swi_sched pid %d(%s) need=%d",
453 it->it_td->td_proc->p_pid, it->it_td->td_proc->p_comm, it->it_need);
456 * Set ih_need for this handler so that if the ithread is already
457 * running it will execute this handler on the next pass. Otherwise,
458 * it will execute it the next time it runs.
460 atomic_store_rel_int(&ih->ih_need, 1);
461 if (!(flags & SWI_DELAY)) {
462 error = ithread_schedule(it, !cold);
463 KASSERT(error == 0, ("stray software interrupt"));
468 * This is the main code for interrupt threads.
471 ithread_loop(void *arg)
473 struct ithd *ithd; /* our thread context */
474 struct intrhand *ih; /* and our interrupt handler chain */
480 ithd = (struct ithd *)arg; /* point to myself */
481 KASSERT(ithd->it_td == td && td->td_ithd == ithd,
482 ("%s: ithread and proc linkage out of sync", __func__));
485 * As long as we have interrupts outstanding, go through the
486 * list of handlers, giving each one a go at it.
490 * If we are an orphaned thread, then just die.
492 if (ithd->it_flags & IT_DEAD) {
493 CTR3(KTR_INTR, "%s: pid %d: (%s) exiting", __func__,
494 p->p_pid, p->p_comm);
496 mtx_destroy(&ithd->it_lock);
498 free(ithd, M_ITHREAD);
502 CTR4(KTR_INTR, "%s: pid %d: (%s) need=%d", __func__,
503 p->p_pid, p->p_comm, ithd->it_need);
504 while (ithd->it_need) {
506 * Service interrupts. If another interrupt
507 * arrives while we are running, they will set
508 * it_need to denote that we should make
511 atomic_store_rel_int(&ithd->it_need, 0);
513 TAILQ_FOREACH(ih, &ithd->it_handlers, ih_next) {
514 if (ithd->it_flags & IT_SOFT && !ih->ih_need)
516 atomic_store_rel_int(&ih->ih_need, 0);
518 "%s: pid %d ih=%p: %p(%p) flg=%x", __func__,
519 p->p_pid, (void *)ih,
520 (void *)ih->ih_handler, ih->ih_argument,
523 if ((ih->ih_flags & IH_DEAD) != 0) {
524 mtx_lock(&ithd->it_lock);
525 TAILQ_REMOVE(&ithd->it_handlers, ih,
528 mtx_unlock(&ithd->it_lock);
531 if ((ih->ih_flags & IH_MPSAFE) == 0)
533 ih->ih_handler(ih->ih_argument);
534 if ((ih->ih_flags & IH_MPSAFE) == 0)
540 * Processed all our interrupts. Now get the sched
541 * lock. This may take a while and it_need may get
542 * set again, so we have to check it again.
544 mtx_assert(&Giant, MA_NOTOWNED);
545 mtx_lock_spin(&sched_lock);
546 if (!ithd->it_need) {
548 * Should we call this earlier in the loop above?
550 if (ithd->it_enable != NULL)
551 ithd->it_enable(ithd->it_vector);
552 p->p_stat = SWAIT; /* we're idle */
553 p->p_stats->p_ru.ru_nvcsw++;
554 CTR2(KTR_INTR, "%s: pid %d: done", __func__, p->p_pid);
556 CTR2(KTR_INTR, "%s: pid %d: resumed", __func__, p->p_pid);
558 mtx_unlock_spin(&sched_lock);
563 * Start standard software interrupt threads
566 start_softintr(void *dummy)
569 if (swi_add(NULL, "net", swi_net, NULL, SWI_NET, 0, &net_ih) ||
570 swi_add(&clk_ithd, "clock", softclock, NULL, SWI_CLOCK,
571 INTR_MPSAFE, &softclock_ih) ||
572 swi_add(NULL, "vm", swi_vm, NULL, SWI_VM, 0, &vm_ih))
573 panic("died while creating standard software ithreads");
575 PROC_LOCK(clk_ithd->it_td->td_proc);
576 clk_ithd->it_td->td_proc->p_flag |= P_NOLOAD;
577 PROC_UNLOCK(clk_ithd->it_td->td_proc);
579 SYSINIT(start_softintr, SI_SUB_SOFTINTR, SI_ORDER_FIRST, start_softintr, NULL)
582 legacy_setsoftnet(void)
584 swi_sched(net_ih, 0);
588 * XXX: This should really be in the network code somewhere and installed
589 * via a SI_SUB_SOFINTR, SI_ORDER_MIDDLE sysinit.
591 void (*netisrs[32])(void);
592 volatile unsigned int netisr; /* scheduling bits for network */
595 register_netisr(num, handler)
600 if (num < 0 || num >= (sizeof(netisrs)/sizeof(*netisrs)) ) {
601 printf("register_netisr: bad isr number: %d\n", num);
604 netisrs[num] = handler;
609 unregister_netisr(num)
613 if (num < 0 || num >= (sizeof(netisrs)/sizeof(*netisrs)) ) {
614 printf("unregister_netisr: bad isr number: %d\n", num);
621 #ifdef DEVICE_POLLING
622 void netisr_pollmore(void);
631 #ifdef DEVICE_POLLING
635 bits = atomic_readandclear_int(&netisr);
636 #ifdef DEVICE_POLLING
639 pollmore = bits & (1 << NETISR_POLL);
641 while ((i = ffs(bits)) != 0) {
643 if (netisrs[i] != NULL)
646 printf("swi_net: unregistered isr number: %d.\n", i);
649 #ifdef DEVICE_POLLING
657 * Sysctls used by systat and others: hw.intrnames and hw.intrcnt.
658 * The data for this machine dependent, and the declarations are in machine
659 * dependent code. The layout of intrnames and intrcnt however is machine
662 * We do not know the length of intrcnt and intrnames at compile time, so
663 * calculate things at run time.
666 sysctl_intrnames(SYSCTL_HANDLER_ARGS)
668 return (sysctl_handle_opaque(oidp, intrnames, eintrnames - intrnames,
672 SYSCTL_PROC(_hw, OID_AUTO, intrnames, CTLTYPE_OPAQUE | CTLFLAG_RD,
673 NULL, 0, sysctl_intrnames, "", "Interrupt Names");
676 sysctl_intrcnt(SYSCTL_HANDLER_ARGS)
678 return (sysctl_handle_opaque(oidp, intrcnt,
679 (char *)eintrcnt - (char *)intrcnt, req));
682 SYSCTL_PROC(_hw, OID_AUTO, intrcnt, CTLTYPE_OPAQUE | CTLFLAG_RD,
683 NULL, 0, sysctl_intrcnt, "", "Interrupt Counts");