| 1 | /* |
| 2 | * Copyright (c) 2003, Matthew Dillon <dillon@backplane.com> All rights reserved. |
| 3 | * Copyright (c) 1997, Stefan Esser <se@freebsd.org> All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice unmodified, this list of conditions, and the following |
| 10 | * disclaimer. |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 16 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 17 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 18 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 19 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 20 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 21 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 22 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 24 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | * |
| 26 | * $FreeBSD: src/sys/kern/kern_intr.c,v 1.24.2.1 2001/10/14 20:05:50 luigi Exp $ |
| 27 | * $DragonFly: src/sys/kern/kern_intr.c,v 1.9 2003/07/11 01:23:24 dillon Exp $ |
| 28 | * |
| 29 | */ |
| 30 | |
| 31 | #include <sys/param.h> |
| 32 | #include <sys/systm.h> |
| 33 | #include <sys/malloc.h> |
| 34 | #include <sys/kernel.h> |
| 35 | #include <sys/sysctl.h> |
| 36 | #include <sys/thread.h> |
| 37 | #include <sys/proc.h> |
| 38 | #include <sys/thread2.h> |
| 39 | #include <sys/random.h> |
| 40 | |
| 41 | #include <machine/ipl.h> |
| 42 | |
| 43 | #include <sys/interrupt.h> |
| 44 | |
| 45 | typedef struct intrec { |
| 46 | struct intrec *next; |
| 47 | inthand2_t *handler; |
| 48 | void *argument; |
| 49 | const char *name; |
| 50 | int intr; |
| 51 | } intrec_t; |
| 52 | |
| 53 | static intrec_t *intlists[NHWI+NSWI]; |
| 54 | static thread_t ithreads[NHWI+NSWI]; |
| 55 | static struct thread ithread_ary[NHWI+NSWI]; |
| 56 | static struct random_softc irandom_ary[NHWI+NSWI]; |
| 57 | static int irunning[NHWI+NSWI]; |
| 58 | |
| 59 | static void ithread_handler(void *arg); |
| 60 | |
| 61 | thread_t |
| 62 | register_swi(int intr, inthand2_t *handler, void *arg, const char *name) |
| 63 | { |
| 64 | if (intr < NHWI || intr >= NHWI + NSWI) |
| 65 | panic("register_swi: bad intr %d", intr); |
| 66 | return(register_int(intr, handler, arg, name)); |
| 67 | } |
| 68 | |
| 69 | thread_t |
| 70 | register_int(int intr, inthand2_t *handler, void *arg, const char *name) |
| 71 | { |
| 72 | intrec_t **list; |
| 73 | intrec_t *rec; |
| 74 | thread_t td; |
| 75 | |
| 76 | if (intr < 0 || intr > NHWI + NSWI) |
| 77 | panic("register_int: bad intr %d", intr); |
| 78 | |
| 79 | rec = malloc(sizeof(intrec_t), M_DEVBUF, M_NOWAIT); |
| 80 | if (rec == NULL) |
| 81 | panic("register_swi: malloc failed"); |
| 82 | rec->handler = handler; |
| 83 | rec->argument = arg; |
| 84 | rec->name = name; |
| 85 | rec->intr = intr; |
| 86 | rec->next = NULL; |
| 87 | |
| 88 | list = &intlists[intr]; |
| 89 | |
| 90 | /* |
| 91 | * Create an interrupt thread if necessary, leave it in an unscheduled |
| 92 | * state. The kthread restore function exits a critical section before |
| 93 | * starting the function so we need *TWO* critical sections in order |
| 94 | * for the handler to begin running in one. |
| 95 | */ |
| 96 | if ((td = ithreads[intr]) == NULL) { |
| 97 | lwkt_create((void *)ithread_handler, (void *)intr, &ithreads[intr], |
| 98 | &ithread_ary[intr], TDF_STOPREQ, "ithread %d", intr); |
| 99 | td = ithreads[intr]; |
| 100 | if (intr >= NHWI && intr < NHWI + NSWI) |
| 101 | lwkt_setpri(td, TDPRI_SOFT_NORM + TDPRI_CRIT * 2); |
| 102 | else |
| 103 | lwkt_setpri(td, TDPRI_INT_MED + TDPRI_CRIT * 2); |
| 104 | } |
| 105 | |
| 106 | /* |
| 107 | * Add the record to the interrupt list |
| 108 | */ |
| 109 | crit_enter(); /* token */ |
| 110 | while (*list != NULL) |
| 111 | list = &(*list)->next; |
| 112 | *list = rec; |
| 113 | crit_exit(); |
| 114 | return(td); |
| 115 | } |
| 116 | |
| 117 | void |
| 118 | unregister_swi(int intr, inthand2_t *handler) |
| 119 | { |
| 120 | if (intr < NHWI || intr >= NHWI + NSWI) |
| 121 | panic("register_swi: bad intr %d", intr); |
| 122 | unregister_int(intr, handler); |
| 123 | } |
| 124 | |
| 125 | void |
| 126 | unregister_int(int intr, inthand2_t handler) |
| 127 | { |
| 128 | intrec_t **list; |
| 129 | intrec_t *rec; |
| 130 | |
| 131 | if (intr < 0 || intr > NHWI + NSWI) |
| 132 | panic("register_int: bad intr %d", intr); |
| 133 | list = &intlists[intr]; |
| 134 | crit_enter(); |
| 135 | while ((rec = *list) != NULL) { |
| 136 | if (rec->handler == (void *)handler) { |
| 137 | *list = rec->next; |
| 138 | break; |
| 139 | } |
| 140 | list = &rec->next; |
| 141 | } |
| 142 | crit_exit(); |
| 143 | if (rec != NULL) { |
| 144 | free(rec, M_DEVBUF); |
| 145 | } else { |
| 146 | printf("warning: unregister_int: int %d handler %p not found\n", |
| 147 | intr, handler); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | void |
| 152 | swi_setpriority(int intr, int pri) |
| 153 | { |
| 154 | struct thread *td; |
| 155 | |
| 156 | if (intr < NHWI || intr >= NHWI + NSWI) |
| 157 | panic("register_swi: bad intr %d", intr); |
| 158 | if ((td = ithreads[intr]) != NULL) |
| 159 | lwkt_setpri(td, pri); |
| 160 | } |
| 161 | |
| 162 | void |
| 163 | register_randintr(int intr) |
| 164 | { |
| 165 | struct random_softc *sc = &irandom_ary[intr]; |
| 166 | sc->sc_intr = intr; |
| 167 | sc->sc_enabled = 1; |
| 168 | } |
| 169 | |
| 170 | void |
| 171 | unregister_randintr(int intr) |
| 172 | { |
| 173 | struct random_softc *sc = &irandom_ary[intr]; |
| 174 | sc->sc_enabled = 0; |
| 175 | } |
| 176 | |
| 177 | /* |
| 178 | * Dispatch an interrupt. If there's nothing to do we have a stray |
| 179 | * interrupt and can just return, leaving the interrupt masked. |
| 180 | * |
| 181 | * We need to schedule the interrupt and set its irunning[] bit. If |
| 182 | * we are not on the interrupt thread's cpu we have to send a message |
| 183 | * to the correct cpu that will issue the desired action (interlocking |
| 184 | * with the interrupt thread's critical section). |
| 185 | * |
| 186 | * We are NOT in a critical section, which will allow the scheduled |
| 187 | * interrupt to preempt us. |
| 188 | */ |
| 189 | static void |
| 190 | sched_ithd_remote(void *arg) |
| 191 | { |
| 192 | sched_ithd((int)arg); |
| 193 | } |
| 194 | |
| 195 | void |
| 196 | sched_ithd(int intr) |
| 197 | { |
| 198 | thread_t td; |
| 199 | |
| 200 | if ((td = ithreads[intr]) != NULL) { |
| 201 | if (intlists[intr] == NULL) { |
| 202 | printf("sched_ithd: stray interrupt %d\n", intr); |
| 203 | } else { |
| 204 | if (td->td_cpu == mycpu->gd_cpuid) { |
| 205 | irunning[intr] = 1; |
| 206 | lwkt_schedule(td); /* preemption handled internally */ |
| 207 | } else { |
| 208 | lwkt_send_ipiq(td->td_cpu, sched_ithd_remote, (void *)intr); |
| 209 | } |
| 210 | } |
| 211 | } else { |
| 212 | printf("sched_ithd: stray interrupt %d\n", intr); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | /* |
| 217 | * Interrupt threads run this as their main loop. The handler should be |
| 218 | * in a critical section on entry. |
| 219 | */ |
| 220 | static void |
| 221 | ithread_handler(void *arg) |
| 222 | { |
| 223 | int intr = (int)arg; |
| 224 | intrec_t **list = &intlists[intr]; |
| 225 | intrec_t *rec; |
| 226 | intrec_t *nrec; |
| 227 | struct random_softc *sc = &irandom_ary[intr]; |
| 228 | |
| 229 | KKASSERT(curthread->td_pri >= TDPRI_CRIT); |
| 230 | for (;;) { |
| 231 | irunning[intr] = 0; |
| 232 | for (rec = *list; rec; rec = nrec) { |
| 233 | nrec = rec->next; |
| 234 | rec->handler(rec->argument); |
| 235 | } |
| 236 | if (sc->sc_enabled) |
| 237 | add_interrupt_randomness(intr); |
| 238 | if (irunning[intr] == 0) |
| 239 | ithread_done(intr); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | /* |
| 244 | * Sysctls used by systat and others: hw.intrnames and hw.intrcnt. |
| 245 | * The data for this machine dependent, and the declarations are in machine |
| 246 | * dependent code. The layout of intrnames and intrcnt however is machine |
| 247 | * independent. |
| 248 | * |
| 249 | * We do not know the length of intrcnt and intrnames at compile time, so |
| 250 | * calculate things at run time. |
| 251 | */ |
| 252 | static int |
| 253 | sysctl_intrnames(SYSCTL_HANDLER_ARGS) |
| 254 | { |
| 255 | return (sysctl_handle_opaque(oidp, intrnames, eintrnames - intrnames, |
| 256 | req)); |
| 257 | } |
| 258 | |
| 259 | SYSCTL_PROC(_hw, OID_AUTO, intrnames, CTLTYPE_OPAQUE | CTLFLAG_RD, |
| 260 | NULL, 0, sysctl_intrnames, "", "Interrupt Names"); |
| 261 | |
| 262 | static int |
| 263 | sysctl_intrcnt(SYSCTL_HANDLER_ARGS) |
| 264 | { |
| 265 | return (sysctl_handle_opaque(oidp, intrcnt, |
| 266 | (char *)eintrcnt - (char *)intrcnt, req)); |
| 267 | } |
| 268 | |
| 269 | SYSCTL_PROC(_hw, OID_AUTO, intrcnt, CTLTYPE_OPAQUE | CTLFLAG_RD, |
| 270 | NULL, 0, sysctl_intrcnt, "", "Interrupt Counts"); |