kern_intr.c revision 103216
126156Sse/* 226156Sse * Copyright (c) 1997, Stefan Esser <se@freebsd.org> 326156Sse * All rights reserved. 426156Sse * 526156Sse * Redistribution and use in source and binary forms, with or without 626156Sse * modification, are permitted provided that the following conditions 726156Sse * are met: 826156Sse * 1. Redistributions of source code must retain the above copyright 926156Sse * notice unmodified, this list of conditions, and the following 1026156Sse * disclaimer. 1126156Sse * 2. Redistributions in binary form must reproduce the above copyright 1226156Sse * notice, this list of conditions and the following disclaimer in the 1326156Sse * documentation and/or other materials provided with the distribution. 1426156Sse * 1526156Sse * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 1626156Sse * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 1726156Sse * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 1826156Sse * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 1926156Sse * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 2026156Sse * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2126156Sse * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2226156Sse * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2326156Sse * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 2426156Sse * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2526156Sse * 2650477Speter * $FreeBSD: head/sys/kern/kern_intr.c 103216 2002-09-11 08:13:56Z julian $ 2726156Sse * 2826156Sse */ 2926156Sse 3036887Sdfr 3141059Speter#include <sys/param.h> 3265822Sjhb#include <sys/bus.h> 3365822Sjhb#include <sys/rtprio.h> 3441059Speter#include <sys/systm.h> 3566698Sjhb#include <sys/interrupt.h> 3666698Sjhb#include <sys/kernel.h> 3766698Sjhb#include <sys/kthread.h> 3866698Sjhb#include <sys/ktr.h> 3974914Sjhb#include <sys/lock.h> 4026156Sse#include <sys/malloc.h> 4167365Sjhb#include <sys/mutex.h> 4266698Sjhb#include <sys/proc.h> 4372759Sjhb#include <sys/random.h> 4472237Sjhb#include <sys/resourcevar.h> 4577582Stmm#include <sys/sysctl.h> 4666698Sjhb#include <sys/unistd.h> 4766698Sjhb#include <sys/vmmeter.h> 4866698Sjhb#include <machine/atomic.h> 4966698Sjhb#include <machine/cpu.h> 5067551Sjhb#include <machine/md_var.h> 5172237Sjhb#include <machine/stdarg.h> 5226156Sse 5367551Sjhb#include <net/netisr.h> /* prototype for legacy_setsoftnet */ 5438244Sbde 5572759Sjhbstruct int_entropy { 5672759Sjhb struct proc *proc; 5772759Sjhb int vector; 5872759Sjhb}; 5972759Sjhb 6072237Sjhbvoid *net_ih; 6172237Sjhbvoid *vm_ih; 6272237Sjhbvoid *softclock_ih; 6372237Sjhbstruct ithd *clk_ithd; 6472237Sjhbstruct ithd *tty_ithd; 6538244Sbde 6672237Sjhbstatic MALLOC_DEFINE(M_ITHREAD, "ithread", "Interrupt Threads"); 6772237Sjhb 6872237Sjhbstatic void ithread_update(struct ithd *); 6972237Sjhbstatic void ithread_loop(void *); 7072237Sjhbstatic void start_softintr(void *); 7172237Sjhbstatic void swi_net(void *); 7272237Sjhb 7372237Sjhbu_char 7472237Sjhbithread_priority(enum intr_type flags) 7565822Sjhb{ 7672237Sjhb u_char pri; 7765822Sjhb 7872237Sjhb flags &= (INTR_TYPE_TTY | INTR_TYPE_BIO | INTR_TYPE_NET | 7978365Speter INTR_TYPE_CAM | INTR_TYPE_MISC | INTR_TYPE_CLK | INTR_TYPE_AV); 8065822Sjhb switch (flags) { 8172237Sjhb case INTR_TYPE_TTY: 8265822Sjhb pri = PI_TTYLOW; 8365822Sjhb break; 8465822Sjhb case INTR_TYPE_BIO: 8565822Sjhb /* 8665822Sjhb * XXX We need to refine this. BSD/OS distinguishes 8765822Sjhb * between tape and disk priorities. 8865822Sjhb */ 8965822Sjhb pri = PI_DISK; 9065822Sjhb break; 9165822Sjhb case INTR_TYPE_NET: 9265822Sjhb pri = PI_NET; 9365822Sjhb break; 9465822Sjhb case INTR_TYPE_CAM: 9565822Sjhb pri = PI_DISK; /* XXX or PI_CAM? */ 9665822Sjhb break; 9778365Speter case INTR_TYPE_AV: /* Audio/video */ 9878365Speter pri = PI_AV; 9978365Speter break; 10072237Sjhb case INTR_TYPE_CLK: 10172237Sjhb pri = PI_REALTIME; 10272237Sjhb break; 10365822Sjhb case INTR_TYPE_MISC: 10465822Sjhb pri = PI_DULL; /* don't care */ 10565822Sjhb break; 10665822Sjhb default: 10772237Sjhb /* We didn't specify an interrupt level. */ 10865822Sjhb panic("ithread_priority: no interrupt type in flags"); 10965822Sjhb } 11065822Sjhb 11165822Sjhb return pri; 11265822Sjhb} 11365822Sjhb 11472237Sjhb/* 11572237Sjhb * Regenerate the name (p_comm) and priority for a threaded interrupt thread. 11672237Sjhb */ 11772237Sjhbstatic void 11872237Sjhbithread_update(struct ithd *ithd) 11972237Sjhb{ 12072237Sjhb struct intrhand *ih; 12183366Sjulian struct thread *td; 12272237Sjhb struct proc *p; 12372237Sjhb int entropy; 12467551Sjhb 12576771Sjhb mtx_assert(&ithd->it_lock, MA_OWNED); 12683366Sjulian td = ithd->it_td; 12783366Sjulian if (td == NULL) 12872237Sjhb return; 12983366Sjulian p = td->td_proc; 13072237Sjhb 13172237Sjhb strncpy(p->p_comm, ithd->it_name, sizeof(ithd->it_name)); 13272237Sjhb ih = TAILQ_FIRST(&ithd->it_handlers); 13372237Sjhb if (ih == NULL) { 13494457Sjhb mtx_lock_spin(&sched_lock); 13590538Sjulian td->td_priority = PRI_MAX_ITHD; 13694457Sjhb td->td_base_pri = PRI_MAX_ITHD; 13794457Sjhb mtx_unlock_spin(&sched_lock); 13872237Sjhb ithd->it_flags &= ~IT_ENTROPY; 13972237Sjhb return; 14072237Sjhb } 14172237Sjhb entropy = 0; 14294457Sjhb mtx_lock_spin(&sched_lock); 14390538Sjulian td->td_priority = ih->ih_pri; 14490538Sjulian td->td_base_pri = ih->ih_pri; 14594457Sjhb mtx_unlock_spin(&sched_lock); 14672237Sjhb TAILQ_FOREACH(ih, &ithd->it_handlers, ih_next) { 14772237Sjhb if (strlen(p->p_comm) + strlen(ih->ih_name) + 1 < 14872237Sjhb sizeof(p->p_comm)) { 14972237Sjhb strcat(p->p_comm, " "); 15072237Sjhb strcat(p->p_comm, ih->ih_name); 15172237Sjhb } else if (strlen(p->p_comm) + 1 == sizeof(p->p_comm)) { 15272237Sjhb if (p->p_comm[sizeof(p->p_comm) - 2] == '+') 15372237Sjhb p->p_comm[sizeof(p->p_comm) - 2] = '*'; 15472237Sjhb else 15572237Sjhb p->p_comm[sizeof(p->p_comm) - 2] = '+'; 15672237Sjhb } else 15772237Sjhb strcat(p->p_comm, "+"); 15872237Sjhb if (ih->ih_flags & IH_ENTROPY) 15972237Sjhb entropy++; 16072237Sjhb } 16172759Sjhb if (entropy) 16272237Sjhb ithd->it_flags |= IT_ENTROPY; 16372237Sjhb else 16472237Sjhb ithd->it_flags &= ~IT_ENTROPY; 16587593Sobrien CTR2(KTR_INTR, "%s: updated %s\n", __func__, p->p_comm); 16672237Sjhb} 16772237Sjhb 16872237Sjhbint 16972237Sjhbithread_create(struct ithd **ithread, int vector, int flags, 17072237Sjhb void (*disable)(int), void (*enable)(int), const char *fmt, ...) 17166698Sjhb{ 17272237Sjhb struct ithd *ithd; 17383366Sjulian struct thread *td; 17467551Sjhb struct proc *p; 17572237Sjhb int error; 17672237Sjhb va_list ap; 17772237Sjhb 17872759Sjhb /* The only valid flag during creation is IT_SOFT. */ 17972759Sjhb if ((flags & ~IT_SOFT) != 0) 18072759Sjhb return (EINVAL); 18172759Sjhb 18272237Sjhb ithd = malloc(sizeof(struct ithd), M_ITHREAD, M_WAITOK | M_ZERO); 18372237Sjhb ithd->it_vector = vector; 18472237Sjhb ithd->it_disable = disable; 18572237Sjhb ithd->it_enable = enable; 18672237Sjhb ithd->it_flags = flags; 18772237Sjhb TAILQ_INIT(&ithd->it_handlers); 18893818Sjhb mtx_init(&ithd->it_lock, "ithread", NULL, MTX_DEF); 18972237Sjhb 19072237Sjhb va_start(ap, fmt); 19172237Sjhb vsnprintf(ithd->it_name, sizeof(ithd->it_name), fmt, ap); 19272237Sjhb va_end(ap); 19372237Sjhb 19472237Sjhb error = kthread_create(ithread_loop, ithd, &p, RFSTOPPED | RFHIGHPID, 19574734Sjhb "%s", ithd->it_name); 19672237Sjhb if (error) { 19776771Sjhb mtx_destroy(&ithd->it_lock); 19872237Sjhb free(ithd, M_ITHREAD); 19972237Sjhb return (error); 20072237Sjhb } 20190361Sjulian td = FIRST_THREAD_IN_PROC(p); /* XXXKSE */ 20290538Sjulian td->td_ksegrp->kg_pri_class = PRI_ITHD; 20390538Sjulian td->td_priority = PRI_MAX_ITHD; 204103216Sjulian TD_SET_IWAIT(td); 20583366Sjulian ithd->it_td = td; 20683366Sjulian td->td_ithd = ithd; 20772237Sjhb if (ithread != NULL) 20872237Sjhb *ithread = ithd; 20972237Sjhb 21087593Sobrien CTR2(KTR_INTR, "%s: created %s", __func__, ithd->it_name); 21172237Sjhb return (0); 21272237Sjhb} 21372237Sjhb 21472237Sjhbint 21572237Sjhbithread_destroy(struct ithd *ithread) 21672237Sjhb{ 21772237Sjhb 21883366Sjulian struct thread *td; 21983366Sjulian struct proc *p; 22076771Sjhb if (ithread == NULL) 22172237Sjhb return (EINVAL); 22272237Sjhb 22383366Sjulian td = ithread->it_td; 22483366Sjulian p = td->td_proc; 22576771Sjhb mtx_lock(&ithread->it_lock); 22676771Sjhb if (!TAILQ_EMPTY(&ithread->it_handlers)) { 22776771Sjhb mtx_unlock(&ithread->it_lock); 22876771Sjhb return (EINVAL); 22976771Sjhb } 23076771Sjhb ithread->it_flags |= IT_DEAD; 23172237Sjhb mtx_lock_spin(&sched_lock); 232103216Sjulian if (TD_AWAITING_INTR(td)) { 233103216Sjulian TD_CLR_IWAIT(td); 23483366Sjulian setrunqueue(td); 23572237Sjhb } 23672237Sjhb mtx_unlock_spin(&sched_lock); 23776771Sjhb mtx_unlock(&ithread->it_lock); 23887593Sobrien CTR2(KTR_INTR, "%s: killing %s", __func__, ithread->it_name); 23972237Sjhb return (0); 24072237Sjhb} 24172237Sjhb 24272237Sjhbint 24372237Sjhbithread_add_handler(struct ithd* ithread, const char *name, 24472237Sjhb driver_intr_t handler, void *arg, u_char pri, enum intr_type flags, 24572237Sjhb void **cookiep) 24672237Sjhb{ 24772237Sjhb struct intrhand *ih, *temp_ih; 24872237Sjhb 24972237Sjhb if (ithread == NULL || name == NULL || handler == NULL) 25072237Sjhb return (EINVAL); 25172237Sjhb if ((flags & INTR_FAST) !=0) 25272237Sjhb flags |= INTR_EXCL; 25372237Sjhb 25472237Sjhb ih = malloc(sizeof(struct intrhand), M_ITHREAD, M_WAITOK | M_ZERO); 25572237Sjhb ih->ih_handler = handler; 25672237Sjhb ih->ih_argument = arg; 25772237Sjhb ih->ih_name = name; 25872237Sjhb ih->ih_ithread = ithread; 25972237Sjhb ih->ih_pri = pri; 26072237Sjhb if (flags & INTR_FAST) 26172237Sjhb ih->ih_flags = IH_FAST | IH_EXCLUSIVE; 26272237Sjhb else if (flags & INTR_EXCL) 26372237Sjhb ih->ih_flags = IH_EXCLUSIVE; 26472237Sjhb if (flags & INTR_MPSAFE) 26572237Sjhb ih->ih_flags |= IH_MPSAFE; 26672237Sjhb if (flags & INTR_ENTROPY) 26772237Sjhb ih->ih_flags |= IH_ENTROPY; 26872237Sjhb 26976771Sjhb mtx_lock(&ithread->it_lock); 27072237Sjhb if ((flags & INTR_EXCL) !=0 && !TAILQ_EMPTY(&ithread->it_handlers)) 27172237Sjhb goto fail; 27272237Sjhb if (!TAILQ_EMPTY(&ithread->it_handlers) && 27372237Sjhb (TAILQ_FIRST(&ithread->it_handlers)->ih_flags & IH_EXCLUSIVE) != 0) 27472237Sjhb goto fail; 27572237Sjhb 27672237Sjhb TAILQ_FOREACH(temp_ih, &ithread->it_handlers, ih_next) 27772237Sjhb if (temp_ih->ih_pri > ih->ih_pri) 27872237Sjhb break; 27972237Sjhb if (temp_ih == NULL) 28072237Sjhb TAILQ_INSERT_TAIL(&ithread->it_handlers, ih, ih_next); 28172237Sjhb else 28272237Sjhb TAILQ_INSERT_BEFORE(temp_ih, ih, ih_next); 28372237Sjhb ithread_update(ithread); 28476771Sjhb mtx_unlock(&ithread->it_lock); 28572237Sjhb 28672237Sjhb if (cookiep != NULL) 28772237Sjhb *cookiep = ih; 28887593Sobrien CTR3(KTR_INTR, "%s: added %s to %s", __func__, ih->ih_name, 28972837Sjhb ithread->it_name); 29072237Sjhb return (0); 29172237Sjhb 29272237Sjhbfail: 29376771Sjhb mtx_unlock(&ithread->it_lock); 29472237Sjhb free(ih, M_ITHREAD); 29572237Sjhb return (EINVAL); 29672237Sjhb} 29772237Sjhb 29872237Sjhbint 29972237Sjhbithread_remove_handler(void *cookie) 30072237Sjhb{ 30172237Sjhb struct intrhand *handler = (struct intrhand *)cookie; 30272237Sjhb struct ithd *ithread; 30372237Sjhb#ifdef INVARIANTS 30472237Sjhb struct intrhand *ih; 30572237Sjhb#endif 30672237Sjhb 30772759Sjhb if (handler == NULL) 30872237Sjhb return (EINVAL); 30972827Sjhb ithread = handler->ih_ithread; 31072827Sjhb KASSERT(ithread != NULL, 31172759Sjhb ("interrupt handler \"%s\" has a NULL interrupt thread", 31272759Sjhb handler->ih_name)); 31387593Sobrien CTR3(KTR_INTR, "%s: removing %s from %s", __func__, handler->ih_name, 31472837Sjhb ithread->it_name); 31576771Sjhb mtx_lock(&ithread->it_lock); 31672237Sjhb#ifdef INVARIANTS 31772237Sjhb TAILQ_FOREACH(ih, &ithread->it_handlers, ih_next) 31872759Sjhb if (ih == handler) 31972759Sjhb goto ok; 32076771Sjhb mtx_unlock(&ithread->it_lock); 32172759Sjhb panic("interrupt handler \"%s\" not found in interrupt thread \"%s\"", 32272759Sjhb ih->ih_name, ithread->it_name); 32372759Sjhbok: 32472237Sjhb#endif 32572839Sjhb /* 32672839Sjhb * If the interrupt thread is already running, then just mark this 32772839Sjhb * handler as being dead and let the ithread do the actual removal. 32872839Sjhb */ 32972839Sjhb mtx_lock_spin(&sched_lock); 330103216Sjulian if (!TD_AWAITING_INTR(ithread->it_td)) { 33172839Sjhb handler->ih_flags |= IH_DEAD; 33272839Sjhb 33372839Sjhb /* 33472839Sjhb * Ensure that the thread will process the handler list 33572839Sjhb * again and remove this handler if it has already passed 33672839Sjhb * it on the list. 33772839Sjhb */ 33872839Sjhb ithread->it_need = 1; 33976771Sjhb } else 34072839Sjhb TAILQ_REMOVE(&ithread->it_handlers, handler, ih_next); 34172839Sjhb mtx_unlock_spin(&sched_lock); 34276771Sjhb if ((handler->ih_flags & IH_DEAD) != 0) 34376771Sjhb msleep(handler, &ithread->it_lock, PUSER, "itrmh", 0); 34476771Sjhb ithread_update(ithread); 34576771Sjhb mtx_unlock(&ithread->it_lock); 34676771Sjhb free(handler, M_ITHREAD); 34772237Sjhb return (0); 34872237Sjhb} 34972237Sjhb 35072237Sjhbint 35172759Sjhbithread_schedule(struct ithd *ithread, int do_switch) 35272759Sjhb{ 35372759Sjhb struct int_entropy entropy; 35483366Sjulian struct thread *td; 355101176Sjulian struct thread *ctd; 35672759Sjhb struct proc *p; 35772759Sjhb 35872759Sjhb /* 35972759Sjhb * If no ithread or no handlers, then we have a stray interrupt. 36072759Sjhb */ 36172759Sjhb if ((ithread == NULL) || TAILQ_EMPTY(&ithread->it_handlers)) 36272759Sjhb return (EINVAL); 36372759Sjhb 364101176Sjulian ctd = curthread; 36572759Sjhb /* 36672759Sjhb * If any of the handlers for this ithread claim to be good 36772759Sjhb * sources of entropy, then gather some. 36872759Sjhb */ 36972759Sjhb if (harvest.interrupt && ithread->it_flags & IT_ENTROPY) { 37072759Sjhb entropy.vector = ithread->it_vector; 371102996Sdavidxu entropy.proc = ctd->td_proc; 37272759Sjhb random_harvest(&entropy, sizeof(entropy), 2, 0, 37372759Sjhb RANDOM_INTERRUPT); 37472759Sjhb } 37572759Sjhb 37683366Sjulian td = ithread->it_td; 37783366Sjulian p = td->td_proc; 37873313Sjhb KASSERT(p != NULL, ("ithread %s has no process", ithread->it_name)); 37999072Sjulian CTR4(KTR_INTR, "%s: pid %d: (%s) need = %d", 38099072Sjulian __func__, p->p_pid, p->p_comm, ithread->it_need); 38172759Sjhb 38272759Sjhb /* 38372759Sjhb * Set it_need to tell the thread to keep running if it is already 38472759Sjhb * running. Then, grab sched_lock and see if we actually need to 38572759Sjhb * put this thread on the runqueue. If so and the do_switch flag is 38688900Sjhb * true and it is safe to switch, then switch to the ithread 38788900Sjhb * immediately. Otherwise, set the needresched flag to guarantee 38888900Sjhb * that this ithread will run before any userland processes. 38972759Sjhb */ 39072759Sjhb ithread->it_need = 1; 39172759Sjhb mtx_lock_spin(&sched_lock); 392103216Sjulian if (TD_AWAITING_INTR(td)) { 39387593Sobrien CTR2(KTR_INTR, "%s: setrunqueue %d", __func__, p->p_pid); 394103216Sjulian TD_CLR_IWAIT(td); 39599072Sjulian setrunqueue(td); 39699072Sjulian if (do_switch && 397101176Sjulian (ctd->td_critnest == 1) ) { 398103216Sjulian KASSERT((TD_IS_RUNNING(ctd)), 399101176Sjulian ("ithread_schedule: Bad state for curthread.")); 400101176Sjulian ctd->td_proc->p_stats->p_ru.ru_nivcsw++; 401101176Sjulian if (ctd->td_kse->ke_flags & KEF_IDLEKSE) 402103216Sjulian ctd->td_state = TDS_CAN_RUN; /* XXXKSE */ 40372759Sjhb mi_switch(); 40497515Sjulian } else { 40583366Sjulian curthread->td_kse->ke_flags |= KEF_NEEDRESCHED; 40697515Sjulian } 40772759Sjhb } else { 40887593Sobrien CTR4(KTR_INTR, "%s: pid %d: it_need %d, state %d", 40999072Sjulian __func__, p->p_pid, ithread->it_need, p->p_state); 41072759Sjhb } 41172759Sjhb mtx_unlock_spin(&sched_lock); 41272759Sjhb 41372759Sjhb return (0); 41472759Sjhb} 41572759Sjhb 41672759Sjhbint 41772237Sjhbswi_add(struct ithd **ithdp, const char *name, driver_intr_t handler, 41872237Sjhb void *arg, int pri, enum intr_type flags, void **cookiep) 41972237Sjhb{ 42067551Sjhb struct ithd *ithd; 42172237Sjhb int error; 42266698Sjhb 42372759Sjhb if (flags & (INTR_FAST | INTR_ENTROPY)) 42472759Sjhb return (EINVAL); 42572759Sjhb 42667551Sjhb ithd = (ithdp != NULL) ? *ithdp : NULL; 42766698Sjhb 42872759Sjhb if (ithd != NULL) { 42972759Sjhb if ((ithd->it_flags & IT_SOFT) == 0) 43072759Sjhb return(EINVAL); 43172759Sjhb } else { 43272237Sjhb error = ithread_create(&ithd, pri, IT_SOFT, NULL, NULL, 43372237Sjhb "swi%d:", pri); 43467551Sjhb if (error) 43572237Sjhb return (error); 43672237Sjhb 43767551Sjhb if (ithdp != NULL) 43867551Sjhb *ithdp = ithd; 43966698Sjhb } 44072376Sjake return (ithread_add_handler(ithd, name, handler, arg, 44172376Sjake (pri * RQ_PPQ) + PI_SOFT, flags, cookiep)); 44266698Sjhb} 44366698Sjhb 44466698Sjhb 44566698Sjhb/* 44667551Sjhb * Schedule a heavyweight software interrupt process. 44766698Sjhb */ 44867551Sjhbvoid 44972237Sjhbswi_sched(void *cookie, int flags) 45066698Sjhb{ 45172237Sjhb struct intrhand *ih = (struct intrhand *)cookie; 45272237Sjhb struct ithd *it = ih->ih_ithread; 45372759Sjhb int error; 45466698Sjhb 45567551Sjhb atomic_add_int(&cnt.v_intr, 1); /* one more global interrupt */ 45667551Sjhb 45772237Sjhb CTR3(KTR_INTR, "swi_sched pid %d(%s) need=%d", 45883366Sjulian it->it_td->td_proc->p_pid, it->it_td->td_proc->p_comm, it->it_need); 45967551Sjhb 46067551Sjhb /* 46172759Sjhb * Set ih_need for this handler so that if the ithread is already 46272759Sjhb * running it will execute this handler on the next pass. Otherwise, 46372759Sjhb * it will execute it the next time it runs. 46467551Sjhb */ 46572237Sjhb atomic_store_rel_int(&ih->ih_need, 1); 46672237Sjhb if (!(flags & SWI_DELAY)) { 46788900Sjhb error = ithread_schedule(it, !cold); 46872759Sjhb KASSERT(error == 0, ("stray software interrupt")); 46966698Sjhb } 47066698Sjhb} 47166698Sjhb 47266698Sjhb/* 47372237Sjhb * This is the main code for interrupt threads. 47466698Sjhb */ 47567551Sjhbvoid 47672237Sjhbithread_loop(void *arg) 47766698Sjhb{ 47872237Sjhb struct ithd *ithd; /* our thread context */ 47967551Sjhb struct intrhand *ih; /* and our interrupt handler chain */ 48083366Sjulian struct thread *td; 48172237Sjhb struct proc *p; 48267551Sjhb 48383366Sjulian td = curthread; 48483366Sjulian p = td->td_proc; 48572237Sjhb ithd = (struct ithd *)arg; /* point to myself */ 48683366Sjulian KASSERT(ithd->it_td == td && td->td_ithd == ithd, 48787593Sobrien ("%s: ithread and proc linkage out of sync", __func__)); 48866698Sjhb 48967551Sjhb /* 49067551Sjhb * As long as we have interrupts outstanding, go through the 49167551Sjhb * list of handlers, giving each one a go at it. 49267551Sjhb */ 49366698Sjhb for (;;) { 49472237Sjhb /* 49572237Sjhb * If we are an orphaned thread, then just die. 49672237Sjhb */ 49772237Sjhb if (ithd->it_flags & IT_DEAD) { 49887593Sobrien CTR3(KTR_INTR, "%s: pid %d: (%s) exiting", __func__, 49972237Sjhb p->p_pid, p->p_comm); 50083366Sjulian td->td_ithd = NULL; 50176771Sjhb mtx_destroy(&ithd->it_lock); 50272237Sjhb mtx_lock(&Giant); 50372237Sjhb free(ithd, M_ITHREAD); 50472237Sjhb kthread_exit(0); 50572237Sjhb } 50672237Sjhb 50787593Sobrien CTR4(KTR_INTR, "%s: pid %d: (%s) need=%d", __func__, 50872237Sjhb p->p_pid, p->p_comm, ithd->it_need); 50972237Sjhb while (ithd->it_need) { 51067551Sjhb /* 51167551Sjhb * Service interrupts. If another interrupt 51267551Sjhb * arrives while we are running, they will set 51367551Sjhb * it_need to denote that we should make 51467551Sjhb * another pass. 51567551Sjhb */ 51672237Sjhb atomic_store_rel_int(&ithd->it_need, 0); 51772839Sjhbrestart: 51872237Sjhb TAILQ_FOREACH(ih, &ithd->it_handlers, ih_next) { 51972237Sjhb if (ithd->it_flags & IT_SOFT && !ih->ih_need) 52067551Sjhb continue; 52172237Sjhb atomic_store_rel_int(&ih->ih_need, 0); 52287593Sobrien CTR6(KTR_INTR, 52387593Sobrien "%s: pid %d ih=%p: %p(%p) flg=%x", __func__, 52467551Sjhb p->p_pid, (void *)ih, 52567551Sjhb (void *)ih->ih_handler, ih->ih_argument, 52667551Sjhb ih->ih_flags); 52766698Sjhb 52872839Sjhb if ((ih->ih_flags & IH_DEAD) != 0) { 52976771Sjhb mtx_lock(&ithd->it_lock); 53072839Sjhb TAILQ_REMOVE(&ithd->it_handlers, ih, 53172839Sjhb ih_next); 53276771Sjhb wakeup(ih); 53376771Sjhb mtx_unlock(&ithd->it_lock); 53472839Sjhb goto restart; 53572839Sjhb } 53676771Sjhb if ((ih->ih_flags & IH_MPSAFE) == 0) 53776771Sjhb mtx_lock(&Giant); 53867551Sjhb ih->ih_handler(ih->ih_argument); 53972237Sjhb if ((ih->ih_flags & IH_MPSAFE) == 0) 54072200Sbmilekic mtx_unlock(&Giant); 54167551Sjhb } 54266698Sjhb } 54367551Sjhb 54466698Sjhb /* 54566698Sjhb * Processed all our interrupts. Now get the sched 54667551Sjhb * lock. This may take a while and it_need may get 54766698Sjhb * set again, so we have to check it again. 54866698Sjhb */ 54968789Sjhb mtx_assert(&Giant, MA_NOTOWNED); 55072200Sbmilekic mtx_lock_spin(&sched_lock); 55172237Sjhb if (!ithd->it_need) { 55272237Sjhb /* 55372237Sjhb * Should we call this earlier in the loop above? 55472237Sjhb */ 55572237Sjhb if (ithd->it_enable != NULL) 55672237Sjhb ithd->it_enable(ithd->it_vector); 557103216Sjulian TD_SET_IWAIT(td); /* we're idle */ 55878765Sjhb p->p_stats->p_ru.ru_nvcsw++; 55987593Sobrien CTR2(KTR_INTR, "%s: pid %d: done", __func__, p->p_pid); 56066698Sjhb mi_switch(); 56187593Sobrien CTR2(KTR_INTR, "%s: pid %d: resumed", __func__, p->p_pid); 56266698Sjhb } 56372200Sbmilekic mtx_unlock_spin(&sched_lock); 56466698Sjhb } 56566698Sjhb} 56666698Sjhb 56772237Sjhb/* 56867551Sjhb * Start standard software interrupt threads 56966698Sjhb */ 57067551Sjhbstatic void 57172237Sjhbstart_softintr(void *dummy) 57267551Sjhb{ 57372237Sjhb 57472237Sjhb if (swi_add(NULL, "net", swi_net, NULL, SWI_NET, 0, &net_ih) || 57572237Sjhb swi_add(&clk_ithd, "clock", softclock, NULL, SWI_CLOCK, 57672237Sjhb INTR_MPSAFE, &softclock_ih) || 57772237Sjhb swi_add(NULL, "vm", swi_vm, NULL, SWI_VM, 0, &vm_ih)) 57872237Sjhb panic("died while creating standard software ithreads"); 57972759Sjhb 58083366Sjulian PROC_LOCK(clk_ithd->it_td->td_proc); 58183366Sjulian clk_ithd->it_td->td_proc->p_flag |= P_NOLOAD; 58283366Sjulian PROC_UNLOCK(clk_ithd->it_td->td_proc); 58366698Sjhb} 58472237SjhbSYSINIT(start_softintr, SI_SUB_SOFTINTR, SI_ORDER_FIRST, start_softintr, NULL) 58566698Sjhb 58667551Sjhbvoid 58772237Sjhblegacy_setsoftnet(void) 58867551Sjhb{ 58988900Sjhb swi_sched(net_ih, 0); 59066698Sjhb} 59166698Sjhb 59267551Sjhb/* 59367551Sjhb * XXX: This should really be in the network code somewhere and installed 59467551Sjhb * via a SI_SUB_SOFINTR, SI_ORDER_MIDDLE sysinit. 59567551Sjhb */ 59692723Salfredvoid (*netisrs[32])(void); 59782844Sobrienvolatile unsigned int netisr; /* scheduling bits for network */ 59866698Sjhb 59969586Sjakeint 60069586Sjakeregister_netisr(num, handler) 60169586Sjake int num; 60269586Sjake netisr_t *handler; 60369586Sjake{ 60469586Sjake 60569586Sjake if (num < 0 || num >= (sizeof(netisrs)/sizeof(*netisrs)) ) { 60669586Sjake printf("register_netisr: bad isr number: %d\n", num); 60769586Sjake return (EINVAL); 60869586Sjake } 60969586Sjake netisrs[num] = handler; 61069586Sjake return (0); 61169586Sjake} 61269586Sjake 61369586Sjakeint 61469586Sjakeunregister_netisr(num) 61569586Sjake int num; 61669586Sjake{ 61769586Sjake 61869586Sjake if (num < 0 || num >= (sizeof(netisrs)/sizeof(*netisrs)) ) { 61969586Sjake printf("unregister_netisr: bad isr number: %d\n", num); 62069586Sjake return (EINVAL); 62169586Sjake } 62269586Sjake netisrs[num] = NULL; 62369586Sjake return (0); 62469586Sjake} 62569586Sjake 62691936Sluigi#ifdef DEVICE_POLLING 62791936Sluigi void netisr_pollmore(void); 62891936Sluigi#endif 62991936Sluigi 63067551Sjhbstatic void 63167551Sjhbswi_net(void *dummy) 63266698Sjhb{ 63367551Sjhb u_int bits; 63467551Sjhb int i; 63566698Sjhb 63687902Sluigi#ifdef DEVICE_POLLING 63787902Sluigi for (;;) { 63887902Sluigi int pollmore; 63987902Sluigi#endif 64067551Sjhb bits = atomic_readandclear_int(&netisr); 64187902Sluigi#ifdef DEVICE_POLLING 64287902Sluigi if (bits == 0) 64387902Sluigi return; 64487902Sluigi pollmore = bits & (1 << NETISR_POLL); 64587902Sluigi#endif 64667551Sjhb while ((i = ffs(bits)) != 0) { 64767551Sjhb i--; 64870531Stanimura if (netisrs[i] != NULL) 64970531Stanimura netisrs[i](); 65070531Stanimura else 65170531Stanimura printf("swi_net: unregistered isr number: %d.\n", i); 65267551Sjhb bits &= ~(1 << i); 65366698Sjhb } 65487902Sluigi#ifdef DEVICE_POLLING 65587902Sluigi if (pollmore) 65690550Sluigi netisr_pollmore(); 65787902Sluigi } 65887902Sluigi#endif 65966698Sjhb} 66077582Stmm 66177582Stmm/* 66277582Stmm * Sysctls used by systat and others: hw.intrnames and hw.intrcnt. 66377582Stmm * The data for this machine dependent, and the declarations are in machine 66477582Stmm * dependent code. The layout of intrnames and intrcnt however is machine 66577582Stmm * independent. 66677582Stmm * 66777582Stmm * We do not know the length of intrcnt and intrnames at compile time, so 66877582Stmm * calculate things at run time. 66977582Stmm */ 67077582Stmmstatic int 67177582Stmmsysctl_intrnames(SYSCTL_HANDLER_ARGS) 67277582Stmm{ 67377582Stmm return (sysctl_handle_opaque(oidp, intrnames, eintrnames - intrnames, 67477582Stmm req)); 67577582Stmm} 67677582Stmm 67777582StmmSYSCTL_PROC(_hw, OID_AUTO, intrnames, CTLTYPE_OPAQUE | CTLFLAG_RD, 67877582Stmm NULL, 0, sysctl_intrnames, "", "Interrupt Names"); 67977582Stmm 68077582Stmmstatic int 68177582Stmmsysctl_intrcnt(SYSCTL_HANDLER_ARGS) 68277582Stmm{ 68377582Stmm return (sysctl_handle_opaque(oidp, intrcnt, 68477582Stmm (char *)eintrcnt - (char *)intrcnt, req)); 68577582Stmm} 68677582Stmm 68777582StmmSYSCTL_PROC(_hw, OID_AUTO, intrcnt, CTLTYPE_OPAQUE | CTLFLAG_RD, 68877582Stmm NULL, 0, sysctl_intrcnt, "", "Interrupt Counts"); 689