| 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.17 2004/06/28 02:57:11 drhodus 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 | static u_int ill_count[NHWI+NSWI]; /* interrupt livelock counter */ |
| 59 | static u_int ill_ticks[NHWI+NSWI]; /* track elapsed to calculate freq */ |
| 60 | static u_int ill_delta[NHWI+NSWI]; /* track elapsed to calculate freq */ |
| 61 | static int ill_state[NHWI+NSWI]; /* current state */ |
| 62 | static struct systimer ill_timer[NHWI+NSWI]; /* enforced freq. timer */ |
| 63 | static struct systimer ill_rtimer[NHWI+NSWI]; /* recovery timer */ |
| 64 | |
| 65 | #define LIVELOCK_NONE 0 |
| 66 | #define LIVELOCK_LIMITED 1 |
| 67 | |
| 68 | static int livelock_limit = 50000; |
| 69 | static int livelock_fallback = 20000; |
| 70 | SYSCTL_INT(_kern, OID_AUTO, livelock_limit, |
| 71 | CTLFLAG_RW, &livelock_limit, 0, "Livelock interrupt rate limit"); |
| 72 | SYSCTL_INT(_kern, OID_AUTO, livelock_fallback, |
| 73 | CTLFLAG_RW, &livelock_fallback, 0, "Livelock interrupt fallback rate"); |
| 74 | |
| 75 | static void ithread_handler(void *arg); |
| 76 | |
| 77 | thread_t |
| 78 | register_swi(int intr, inthand2_t *handler, void *arg, const char *name) |
| 79 | { |
| 80 | if (intr < NHWI || intr >= NHWI + NSWI) |
| 81 | panic("register_swi: bad intr %d", intr); |
| 82 | return(register_int(intr, handler, arg, name)); |
| 83 | } |
| 84 | |
| 85 | thread_t |
| 86 | register_int(int intr, inthand2_t *handler, void *arg, const char *name) |
| 87 | { |
| 88 | intrec_t **list; |
| 89 | intrec_t *rec; |
| 90 | thread_t td; |
| 91 | |
| 92 | if (intr < 0 || intr >= NHWI + NSWI) |
| 93 | panic("register_int: bad intr %d", intr); |
| 94 | |
| 95 | rec = malloc(sizeof(intrec_t), M_DEVBUF, M_NOWAIT); |
| 96 | if (rec == NULL) |
| 97 | panic("register_swi: malloc failed"); |
| 98 | rec->handler = handler; |
| 99 | rec->argument = arg; |
| 100 | rec->name = name; |
| 101 | rec->intr = intr; |
| 102 | rec->next = NULL; |
| 103 | |
| 104 | list = &intlists[intr]; |
| 105 | |
| 106 | /* |
| 107 | * Create an interrupt thread if necessary, leave it in an unscheduled |
| 108 | * state. The kthread restore function exits a critical section before |
| 109 | * starting the function so we need *TWO* critical sections in order |
| 110 | * for the handler to begin running in one. |
| 111 | */ |
| 112 | if ((td = ithreads[intr]) == NULL) { |
| 113 | lwkt_create((void *)ithread_handler, (void *)intr, &ithreads[intr], |
| 114 | &ithread_ary[intr], TDF_STOPREQ|TDF_INTTHREAD, -1, |
| 115 | "ithread %d", intr); |
| 116 | td = ithreads[intr]; |
| 117 | if (intr >= NHWI && intr < NHWI + NSWI) |
| 118 | lwkt_setpri(td, TDPRI_SOFT_NORM + TDPRI_CRIT * 2); |
| 119 | else |
| 120 | lwkt_setpri(td, TDPRI_INT_MED + TDPRI_CRIT * 2); |
| 121 | } |
| 122 | |
| 123 | /* |
| 124 | * Add the record to the interrupt list |
| 125 | */ |
| 126 | crit_enter(); /* token */ |
| 127 | while (*list != NULL) |
| 128 | list = &(*list)->next; |
| 129 | *list = rec; |
| 130 | crit_exit(); |
| 131 | return(td); |
| 132 | } |
| 133 | |
| 134 | void |
| 135 | unregister_swi(int intr, inthand2_t *handler) |
| 136 | { |
| 137 | if (intr < NHWI || intr >= NHWI + NSWI) |
| 138 | panic("register_swi: bad intr %d", intr); |
| 139 | unregister_int(intr, handler); |
| 140 | } |
| 141 | |
| 142 | void |
| 143 | unregister_int(int intr, inthand2_t handler) |
| 144 | { |
| 145 | intrec_t **list; |
| 146 | intrec_t *rec; |
| 147 | |
| 148 | if (intr < 0 || intr > NHWI + NSWI) |
| 149 | panic("register_int: bad intr %d", intr); |
| 150 | list = &intlists[intr]; |
| 151 | crit_enter(); |
| 152 | while ((rec = *list) != NULL) { |
| 153 | if (rec->handler == (void *)handler) { |
| 154 | *list = rec->next; |
| 155 | break; |
| 156 | } |
| 157 | list = &rec->next; |
| 158 | } |
| 159 | crit_exit(); |
| 160 | if (rec != NULL) { |
| 161 | free(rec, M_DEVBUF); |
| 162 | } else { |
| 163 | printf("warning: unregister_int: int %d handler %p not found\n", |
| 164 | intr, handler); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | void |
| 169 | swi_setpriority(int intr, int pri) |
| 170 | { |
| 171 | struct thread *td; |
| 172 | |
| 173 | if (intr < NHWI || intr >= NHWI + NSWI) |
| 174 | panic("register_swi: bad intr %d", intr); |
| 175 | if ((td = ithreads[intr]) != NULL) |
| 176 | lwkt_setpri(td, pri); |
| 177 | } |
| 178 | |
| 179 | void |
| 180 | register_randintr(int intr) |
| 181 | { |
| 182 | struct random_softc *sc = &irandom_ary[intr]; |
| 183 | sc->sc_intr = intr; |
| 184 | sc->sc_enabled = 1; |
| 185 | } |
| 186 | |
| 187 | void |
| 188 | unregister_randintr(int intr) |
| 189 | { |
| 190 | struct random_softc *sc = &irandom_ary[intr]; |
| 191 | sc->sc_enabled = 0; |
| 192 | } |
| 193 | |
| 194 | /* |
| 195 | * Dispatch an interrupt. If there's nothing to do we have a stray |
| 196 | * interrupt and can just return, leaving the interrupt masked. |
| 197 | * |
| 198 | * We need to schedule the interrupt and set its irunning[] bit. If |
| 199 | * we are not on the interrupt thread's cpu we have to send a message |
| 200 | * to the correct cpu that will issue the desired action (interlocking |
| 201 | * with the interrupt thread's critical section). |
| 202 | * |
| 203 | * We are NOT in a critical section, which will allow the scheduled |
| 204 | * interrupt to preempt us. The MP lock might *NOT* be held here. |
| 205 | */ |
| 206 | static void |
| 207 | sched_ithd_remote(void *arg) |
| 208 | { |
| 209 | sched_ithd((int)arg); |
| 210 | } |
| 211 | |
| 212 | void |
| 213 | sched_ithd(int intr) |
| 214 | { |
| 215 | thread_t td; |
| 216 | |
| 217 | if ((td = ithreads[intr]) != NULL) { |
| 218 | if (intlists[intr] == NULL) { |
| 219 | printf("sched_ithd: stray interrupt %d\n", intr); |
| 220 | } else { |
| 221 | if (td->td_gd == mycpu) { |
| 222 | irunning[intr] = 1; |
| 223 | lwkt_schedule(td); /* preemption handled internally */ |
| 224 | } else { |
| 225 | lwkt_send_ipiq(td->td_gd, sched_ithd_remote, (void *)intr); |
| 226 | } |
| 227 | } |
| 228 | } else { |
| 229 | printf("sched_ithd: stray interrupt %d\n", intr); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | /* |
| 234 | * This is run from a periodic SYSTIMER (and thus must be MP safe, the BGL |
| 235 | * might not be held). |
| 236 | */ |
| 237 | static void |
| 238 | ithread_livelock_wakeup(systimer_t info) |
| 239 | { |
| 240 | int intr = (int)info->data; |
| 241 | thread_t td; |
| 242 | |
| 243 | if ((td = ithreads[intr]) != NULL) |
| 244 | lwkt_schedule(td); |
| 245 | } |
| 246 | |
| 247 | |
| 248 | /* |
| 249 | * Interrupt threads run this as their main loop. The handler should be |
| 250 | * in a critical section on entry and the BGL is usually left held (for now). |
| 251 | * |
| 252 | * The irunning state starts at 0. When an interrupt occurs, the hardware |
| 253 | * interrupt is disabled and sched_ithd() The HW interrupt remains disabled |
| 254 | * until all routines have run. We then call ithread_done() to reenable |
| 255 | * the HW interrupt and deschedule us until the next interrupt. |
| 256 | */ |
| 257 | |
| 258 | #define LIVELOCK_TIMEFRAME(freq) ((freq) >> 2) /* 1/4 second */ |
| 259 | |
| 260 | static void |
| 261 | ithread_handler(void *arg) |
| 262 | { |
| 263 | int intr = (int)arg; |
| 264 | int freq; |
| 265 | u_int bticks; |
| 266 | u_int cputicks; |
| 267 | intrec_t **list = &intlists[intr]; |
| 268 | intrec_t *rec; |
| 269 | intrec_t *nrec; |
| 270 | struct random_softc *sc = &irandom_ary[intr]; |
| 271 | |
| 272 | KKASSERT(curthread->td_pri >= TDPRI_CRIT); |
| 273 | for (;;) { |
| 274 | /* |
| 275 | * We can get woken up by the livelock periodic code too, run the |
| 276 | * handlers only if there is a real interrupt pending. Clear |
| 277 | * irunning[] prior to running the handlers to interlock new |
| 278 | * events. |
| 279 | */ |
| 280 | if (irunning[intr]) { |
| 281 | irunning[intr] = 0; |
| 282 | for (rec = *list; rec; rec = nrec) { |
| 283 | nrec = rec->next; |
| 284 | rec->handler(rec->argument); |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | /* |
| 289 | * This is our interrupt hook to add rate randomness to the random |
| 290 | * number generator. |
| 291 | */ |
| 292 | if (sc->sc_enabled) |
| 293 | add_interrupt_randomness(intr); |
| 294 | |
| 295 | /* |
| 296 | * This is our livelock test. If we hit the rate limit we |
| 297 | * limit ourselves to 10000 interrupts/sec until the rate |
| 298 | * falls below 50% of that value, then we unlimit again. |
| 299 | */ |
| 300 | cputicks = cputimer_count(); |
| 301 | ++ill_count[intr]; |
| 302 | bticks = cputicks - ill_ticks[intr]; |
| 303 | ill_ticks[intr] = cputicks; |
| 304 | if (bticks > cputimer_freq) |
| 305 | bticks = cputimer_freq; |
| 306 | |
| 307 | switch(ill_state[intr]) { |
| 308 | case LIVELOCK_NONE: |
| 309 | ill_delta[intr] += bticks; |
| 310 | if (ill_delta[intr] < LIVELOCK_TIMEFRAME(cputimer_freq)) |
| 311 | break; |
| 312 | freq = (int64_t)ill_count[intr] * cputimer_freq / ill_delta[intr]; |
| 313 | ill_delta[intr] = 0; |
| 314 | ill_count[intr] = 0; |
| 315 | if (freq < livelock_limit) |
| 316 | break; |
| 317 | printf("intr %d at %d hz, livelocked! limiting at %d hz\n", |
| 318 | intr, freq, livelock_fallback); |
| 319 | ill_state[intr] = LIVELOCK_LIMITED; |
| 320 | bticks = 0; |
| 321 | /* force periodic check to avoid stale removal (if ints stop) */ |
| 322 | systimer_init_periodic(&ill_rtimer[intr], ithread_livelock_wakeup, |
| 323 | (void *)intr, 1); |
| 324 | /* fall through */ |
| 325 | case LIVELOCK_LIMITED: |
| 326 | /* |
| 327 | * Delay (us) before rearming the interrupt |
| 328 | */ |
| 329 | systimer_init_oneshot(&ill_timer[intr], ithread_livelock_wakeup, |
| 330 | (void *)intr, 1 + 1000000 / livelock_fallback); |
| 331 | lwkt_deschedule_self(curthread); |
| 332 | lwkt_switch(); |
| 333 | |
| 334 | /* in case we were woken up by something else */ |
| 335 | systimer_del(&ill_timer[intr]); |
| 336 | |
| 337 | /* |
| 338 | * Calculate interrupt rate (note that due to our delay it |
| 339 | * will not exceed livelock_fallback). |
| 340 | */ |
| 341 | ill_delta[intr] += bticks; |
| 342 | if (ill_delta[intr] < LIVELOCK_TIMEFRAME(cputimer_freq)) |
| 343 | break; |
| 344 | freq = (int64_t)ill_count[intr] * cputimer_freq / ill_delta[intr]; |
| 345 | ill_delta[intr] = 0; |
| 346 | ill_count[intr] = 0; |
| 347 | if (freq < (livelock_fallback >> 1)) { |
| 348 | printf("intr %d at %d hz, removing livelock limit\n", |
| 349 | intr, freq); |
| 350 | ill_state[intr] = LIVELOCK_NONE; |
| 351 | systimer_del(&ill_rtimer[intr]); |
| 352 | } |
| 353 | break; |
| 354 | } |
| 355 | |
| 356 | /* |
| 357 | * If another interrupt has not been queued we can reenable the |
| 358 | * hardware interrupt and go to sleep. |
| 359 | */ |
| 360 | if (irunning[intr] == 0) |
| 361 | ithread_done(intr); |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | /* |
| 366 | * Sysctls used by systat and others: hw.intrnames and hw.intrcnt. |
| 367 | * The data for this machine dependent, and the declarations are in machine |
| 368 | * dependent code. The layout of intrnames and intrcnt however is machine |
| 369 | * independent. |
| 370 | * |
| 371 | * We do not know the length of intrcnt and intrnames at compile time, so |
| 372 | * calculate things at run time. |
| 373 | */ |
| 374 | static int |
| 375 | sysctl_intrnames(SYSCTL_HANDLER_ARGS) |
| 376 | { |
| 377 | return (sysctl_handle_opaque(oidp, intrnames, eintrnames - intrnames, |
| 378 | req)); |
| 379 | } |
| 380 | |
| 381 | SYSCTL_PROC(_hw, OID_AUTO, intrnames, CTLTYPE_OPAQUE | CTLFLAG_RD, |
| 382 | NULL, 0, sysctl_intrnames, "", "Interrupt Names"); |
| 383 | |
| 384 | static int |
| 385 | sysctl_intrcnt(SYSCTL_HANDLER_ARGS) |
| 386 | { |
| 387 | return (sysctl_handle_opaque(oidp, intrcnt, |
| 388 | (char *)eintrcnt - (char *)intrcnt, req)); |
| 389 | } |
| 390 | |
| 391 | SYSCTL_PROC(_hw, OID_AUTO, intrcnt, CTLTYPE_OPAQUE | CTLFLAG_RD, |
| 392 | NULL, 0, sysctl_intrcnt, "", "Interrupt Counts"); |