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.55 2008/09/01 12:49:00 sephe 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/random.h> 39 #include <sys/serialize.h> 40 #include <sys/interrupt.h> 41 #include <sys/bus.h> 42 #include <sys/machintr.h> 43 44 #include <machine/frame.h> 45 46 #include <sys/interrupt.h> 47 48 #include <sys/thread2.h> 49 #include <sys/mplock2.h> 50 51 struct info_info; 52 53 typedef struct intrec { 54 struct intrec *next; 55 struct intr_info *info; 56 inthand2_t *handler; 57 void *argument; 58 char *name; 59 int intr; 60 int intr_flags; 61 struct lwkt_serialize *serializer; 62 } *intrec_t; 63 64 struct intr_info { 65 intrec_t i_reclist; 66 struct thread i_thread; 67 struct random_softc i_random; 68 int i_running; 69 long i_count; /* interrupts dispatched */ 70 int i_mplock_required; 71 int i_fast; 72 int i_slow; 73 int i_state; 74 int i_errorticks; 75 unsigned long i_straycount; 76 } intr_info_ary[MAX_INTS]; 77 78 int max_installed_hard_intr; 79 int max_installed_soft_intr; 80 81 #define EMERGENCY_INTR_POLLING_FREQ_MAX 20000 82 83 /* 84 * Assert that callers into interrupt handlers don't return with 85 * dangling tokens, spinlocks, or mp locks. 86 */ 87 #ifdef INVARIANTS 88 89 #define TD_INVARIANTS_DECLARE \ 90 int spincount; \ 91 lwkt_tokref_t curstop 92 93 #define TD_INVARIANTS_GET(td) \ 94 do { \ 95 spincount = (td)->td_gd->gd_spinlocks_wr; \ 96 curstop = (td)->td_toks_stop; \ 97 } while(0) 98 99 #define TD_INVARIANTS_TEST(td, name) \ 100 do { \ 101 KASSERT(spincount == (td)->td_gd->gd_spinlocks_wr, \ 102 ("spincount mismatch after interrupt handler %s", \ 103 name)); \ 104 KASSERT(curstop == (td)->td_toks_stop, \ 105 ("token count mismatch after interrupt handler %s", \ 106 name)); \ 107 } while(0) 108 109 #else 110 111 /* !INVARIANTS */ 112 113 #define TD_INVARIANTS_DECLARE 114 #define TD_INVARIANTS_GET(td) 115 #define TD_INVARIANTS_TEST(td, name) 116 117 #endif /* ndef INVARIANTS */ 118 119 static int sysctl_emergency_freq(SYSCTL_HANDLER_ARGS); 120 static int sysctl_emergency_enable(SYSCTL_HANDLER_ARGS); 121 static void emergency_intr_timer_callback(systimer_t, struct intrframe *); 122 static void ithread_handler(void *arg); 123 static void ithread_emergency(void *arg); 124 static void report_stray_interrupt(int intr, struct intr_info *info); 125 static void int_moveto_destcpu(int *, int *, int); 126 static void int_moveto_origcpu(int, int); 127 128 int intr_info_size = sizeof(intr_info_ary) / sizeof(intr_info_ary[0]); 129 130 static struct systimer emergency_intr_timer; 131 static struct thread emergency_intr_thread; 132 133 #define ISTATE_NOTHREAD 0 134 #define ISTATE_NORMAL 1 135 #define ISTATE_LIVELOCKED 2 136 137 static int livelock_limit = 40000; 138 static int livelock_lowater = 20000; 139 static int livelock_debug = -1; 140 SYSCTL_INT(_kern, OID_AUTO, livelock_limit, 141 CTLFLAG_RW, &livelock_limit, 0, "Livelock interrupt rate limit"); 142 SYSCTL_INT(_kern, OID_AUTO, livelock_lowater, 143 CTLFLAG_RW, &livelock_lowater, 0, "Livelock low-water mark restore"); 144 SYSCTL_INT(_kern, OID_AUTO, livelock_debug, 145 CTLFLAG_RW, &livelock_debug, 0, "Livelock debug intr#"); 146 147 static int emergency_intr_enable = 0; /* emergency interrupt polling */ 148 TUNABLE_INT("kern.emergency_intr_enable", &emergency_intr_enable); 149 SYSCTL_PROC(_kern, OID_AUTO, emergency_intr_enable, CTLTYPE_INT | CTLFLAG_RW, 150 0, 0, sysctl_emergency_enable, "I", "Emergency Interrupt Poll Enable"); 151 152 static int emergency_intr_freq = 10; /* emergency polling frequency */ 153 TUNABLE_INT("kern.emergency_intr_freq", &emergency_intr_freq); 154 SYSCTL_PROC(_kern, OID_AUTO, emergency_intr_freq, CTLTYPE_INT | CTLFLAG_RW, 155 0, 0, sysctl_emergency_freq, "I", "Emergency Interrupt Poll Frequency"); 156 157 /* 158 * Sysctl support routines 159 */ 160 static int 161 sysctl_emergency_enable(SYSCTL_HANDLER_ARGS) 162 { 163 int error, enabled; 164 165 enabled = emergency_intr_enable; 166 error = sysctl_handle_int(oidp, &enabled, 0, req); 167 if (error || req->newptr == NULL) 168 return error; 169 emergency_intr_enable = enabled; 170 if (emergency_intr_enable) { 171 systimer_adjust_periodic(&emergency_intr_timer, 172 emergency_intr_freq); 173 } else { 174 systimer_adjust_periodic(&emergency_intr_timer, 1); 175 } 176 return 0; 177 } 178 179 static int 180 sysctl_emergency_freq(SYSCTL_HANDLER_ARGS) 181 { 182 int error, phz; 183 184 phz = emergency_intr_freq; 185 error = sysctl_handle_int(oidp, &phz, 0, req); 186 if (error || req->newptr == NULL) 187 return error; 188 if (phz <= 0) 189 return EINVAL; 190 else if (phz > EMERGENCY_INTR_POLLING_FREQ_MAX) 191 phz = EMERGENCY_INTR_POLLING_FREQ_MAX; 192 193 emergency_intr_freq = phz; 194 if (emergency_intr_enable) { 195 systimer_adjust_periodic(&emergency_intr_timer, 196 emergency_intr_freq); 197 } else { 198 systimer_adjust_periodic(&emergency_intr_timer, 1); 199 } 200 return 0; 201 } 202 203 /* 204 * Register an SWI or INTerrupt handler. 205 */ 206 void * 207 register_swi(int intr, inthand2_t *handler, void *arg, const char *name, 208 struct lwkt_serialize *serializer) 209 { 210 if (intr < FIRST_SOFTINT || intr >= MAX_INTS) 211 panic("register_swi: bad intr %d", intr); 212 return(register_int(intr, handler, arg, name, serializer, 0)); 213 } 214 215 void * 216 register_swi_mp(int intr, inthand2_t *handler, void *arg, const char *name, 217 struct lwkt_serialize *serializer) 218 { 219 if (intr < FIRST_SOFTINT || intr >= MAX_INTS) 220 panic("register_swi: bad intr %d", intr); 221 return(register_int(intr, handler, arg, name, serializer, INTR_MPSAFE)); 222 } 223 224 void * 225 register_int(int intr, inthand2_t *handler, void *arg, const char *name, 226 struct lwkt_serialize *serializer, int intr_flags) 227 { 228 struct intr_info *info; 229 struct intrec **list; 230 intrec_t rec; 231 int orig_cpuid, cpuid; 232 233 if (intr < 0 || intr >= MAX_INTS) 234 panic("register_int: bad intr %d", intr); 235 if (name == NULL) 236 name = "???"; 237 info = &intr_info_ary[intr]; 238 239 /* 240 * Construct an interrupt handler record 241 */ 242 rec = kmalloc(sizeof(struct intrec), M_DEVBUF, M_INTWAIT); 243 rec->name = kmalloc(strlen(name) + 1, M_DEVBUF, M_INTWAIT); 244 strcpy(rec->name, name); 245 246 rec->info = info; 247 rec->handler = handler; 248 rec->argument = arg; 249 rec->intr = intr; 250 rec->intr_flags = intr_flags; 251 rec->next = NULL; 252 rec->serializer = serializer; 253 254 /* 255 * Create an emergency polling thread and set up a systimer to wake 256 * it up. 257 */ 258 if (emergency_intr_thread.td_kstack == NULL) { 259 lwkt_create(ithread_emergency, NULL, NULL, &emergency_intr_thread, 260 TDF_STOPREQ | TDF_INTTHREAD, -1, "ithread emerg"); 261 systimer_init_periodic_nq(&emergency_intr_timer, 262 emergency_intr_timer_callback, &emergency_intr_thread, 263 (emergency_intr_enable ? emergency_intr_freq : 1)); 264 } 265 266 int_moveto_destcpu(&orig_cpuid, &cpuid, intr); 267 268 /* 269 * Create an interrupt thread if necessary, leave it in an unscheduled 270 * state. 271 */ 272 if (info->i_state == ISTATE_NOTHREAD) { 273 info->i_state = ISTATE_NORMAL; 274 lwkt_create(ithread_handler, (void *)(intptr_t)intr, NULL, 275 &info->i_thread, TDF_STOPREQ | TDF_INTTHREAD, -1, 276 "ithread %d", intr); 277 if (intr >= FIRST_SOFTINT) 278 lwkt_setpri(&info->i_thread, TDPRI_SOFT_NORM); 279 else 280 lwkt_setpri(&info->i_thread, TDPRI_INT_MED); 281 info->i_thread.td_preemptable = lwkt_preempt; 282 } 283 284 list = &info->i_reclist; 285 286 /* 287 * Keep track of how many fast and slow interrupts we have. 288 * Set i_mplock_required if any handler in the chain requires 289 * the MP lock to operate. 290 */ 291 if ((intr_flags & INTR_MPSAFE) == 0) 292 info->i_mplock_required = 1; 293 if (intr_flags & INTR_CLOCK) 294 ++info->i_fast; 295 else 296 ++info->i_slow; 297 298 /* 299 * Enable random number generation keying off of this interrupt. 300 */ 301 if ((intr_flags & INTR_NOENTROPY) == 0 && info->i_random.sc_enabled == 0) { 302 info->i_random.sc_enabled = 1; 303 info->i_random.sc_intr = intr; 304 } 305 306 /* 307 * Add the record to the interrupt list. 308 */ 309 crit_enter(); 310 while (*list != NULL) 311 list = &(*list)->next; 312 *list = rec; 313 crit_exit(); 314 315 /* 316 * Update max_installed_hard_intr to make the emergency intr poll 317 * a bit more efficient. 318 */ 319 if (intr < FIRST_SOFTINT) { 320 if (max_installed_hard_intr <= intr) 321 max_installed_hard_intr = intr + 1; 322 } else { 323 if (max_installed_soft_intr <= intr) 324 max_installed_soft_intr = intr + 1; 325 } 326 327 /* 328 * Setup the machine level interrupt vector 329 * 330 * XXX temporary workaround for some ACPI brokedness. ACPI installs 331 * its interrupt too early, before the IOAPICs have been configured, 332 * which means the IOAPIC is not enabled by the registration of the 333 * ACPI interrupt. Anything else sharing that IRQ will wind up not 334 * being enabled. Temporarily work around the problem by always 335 * installing and enabling on every new interrupt handler, even 336 * if one has already been setup on that irq. 337 */ 338 if (intr < FIRST_SOFTINT /* && info->i_slow + info->i_fast == 1*/) { 339 if (machintr_vector_setup(intr, intr_flags)) 340 kprintf("machintr_vector_setup: failed on irq %d\n", intr); 341 } 342 343 int_moveto_origcpu(orig_cpuid, cpuid); 344 345 return(rec); 346 } 347 348 void 349 unregister_swi(void *id) 350 { 351 unregister_int(id); 352 } 353 354 void 355 unregister_int(void *id) 356 { 357 struct intr_info *info; 358 struct intrec **list; 359 intrec_t rec; 360 int intr, orig_cpuid, cpuid; 361 362 intr = ((intrec_t)id)->intr; 363 364 if (intr < 0 || intr >= MAX_INTS) 365 panic("register_int: bad intr %d", intr); 366 367 info = &intr_info_ary[intr]; 368 369 int_moveto_destcpu(&orig_cpuid, &cpuid, intr); 370 371 /* 372 * Remove the interrupt descriptor, adjust the descriptor count, 373 * and teardown the machine level vector if this was the last interrupt. 374 */ 375 crit_enter(); 376 list = &info->i_reclist; 377 while ((rec = *list) != NULL) { 378 if (rec == id) 379 break; 380 list = &rec->next; 381 } 382 if (rec) { 383 intrec_t rec0; 384 385 *list = rec->next; 386 if (rec->intr_flags & INTR_CLOCK) 387 --info->i_fast; 388 else 389 --info->i_slow; 390 if (intr < FIRST_SOFTINT && info->i_fast + info->i_slow == 0) 391 machintr_vector_teardown(intr); 392 393 /* 394 * Clear i_mplock_required if no handlers in the chain require the 395 * MP lock. 396 */ 397 for (rec0 = info->i_reclist; rec0; rec0 = rec0->next) { 398 if ((rec0->intr_flags & INTR_MPSAFE) == 0) 399 break; 400 } 401 if (rec0 == NULL) 402 info->i_mplock_required = 0; 403 } 404 405 crit_exit(); 406 407 int_moveto_origcpu(orig_cpuid, cpuid); 408 409 /* 410 * Free the record. 411 */ 412 if (rec != NULL) { 413 kfree(rec->name, M_DEVBUF); 414 kfree(rec, M_DEVBUF); 415 } else { 416 kprintf("warning: unregister_int: int %d handler for %s not found\n", 417 intr, ((intrec_t)id)->name); 418 } 419 } 420 421 const char * 422 get_registered_name(int intr) 423 { 424 intrec_t rec; 425 426 if (intr < 0 || intr >= MAX_INTS) 427 panic("register_int: bad intr %d", intr); 428 429 if ((rec = intr_info_ary[intr].i_reclist) == NULL) 430 return(NULL); 431 else if (rec->next) 432 return("mux"); 433 else 434 return(rec->name); 435 } 436 437 int 438 count_registered_ints(int intr) 439 { 440 struct intr_info *info; 441 442 if (intr < 0 || intr >= MAX_INTS) 443 panic("register_int: bad intr %d", intr); 444 info = &intr_info_ary[intr]; 445 return(info->i_fast + info->i_slow); 446 } 447 448 long 449 get_interrupt_counter(int intr) 450 { 451 struct intr_info *info; 452 453 if (intr < 0 || intr >= MAX_INTS) 454 panic("register_int: bad intr %d", intr); 455 info = &intr_info_ary[intr]; 456 return(info->i_count); 457 } 458 459 460 void 461 swi_setpriority(int intr, int pri) 462 { 463 struct intr_info *info; 464 465 if (intr < FIRST_SOFTINT || intr >= MAX_INTS) 466 panic("register_swi: bad intr %d", intr); 467 info = &intr_info_ary[intr]; 468 if (info->i_state != ISTATE_NOTHREAD) 469 lwkt_setpri(&info->i_thread, pri); 470 } 471 472 void 473 register_randintr(int intr) 474 { 475 struct intr_info *info; 476 477 if (intr < 0 || intr >= MAX_INTS) 478 panic("register_randintr: bad intr %d", intr); 479 info = &intr_info_ary[intr]; 480 info->i_random.sc_intr = intr; 481 info->i_random.sc_enabled = 1; 482 } 483 484 void 485 unregister_randintr(int intr) 486 { 487 struct intr_info *info; 488 489 if (intr < 0 || intr >= MAX_INTS) 490 panic("register_swi: bad intr %d", intr); 491 info = &intr_info_ary[intr]; 492 info->i_random.sc_enabled = -1; 493 } 494 495 int 496 next_registered_randintr(int intr) 497 { 498 struct intr_info *info; 499 500 if (intr < 0 || intr >= MAX_INTS) 501 panic("register_swi: bad intr %d", intr); 502 while (intr < MAX_INTS) { 503 info = &intr_info_ary[intr]; 504 if (info->i_random.sc_enabled > 0) 505 break; 506 ++intr; 507 } 508 return(intr); 509 } 510 511 /* 512 * Dispatch an interrupt. If there's nothing to do we have a stray 513 * interrupt and can just return, leaving the interrupt masked. 514 * 515 * We need to schedule the interrupt and set its i_running bit. If 516 * we are not on the interrupt thread's cpu we have to send a message 517 * to the correct cpu that will issue the desired action (interlocking 518 * with the interrupt thread's critical section). We do NOT attempt to 519 * reschedule interrupts whos i_running bit is already set because 520 * this would prematurely wakeup a livelock-limited interrupt thread. 521 * 522 * i_running is only tested/set on the same cpu as the interrupt thread. 523 * 524 * We are NOT in a critical section, which will allow the scheduled 525 * interrupt to preempt us. The MP lock might *NOT* be held here. 526 */ 527 #ifdef SMP 528 529 static void 530 sched_ithd_remote(void *arg) 531 { 532 sched_ithd((int)(intptr_t)arg); 533 } 534 535 #endif 536 537 void 538 sched_ithd(int intr) 539 { 540 struct intr_info *info; 541 542 info = &intr_info_ary[intr]; 543 544 ++info->i_count; 545 if (info->i_state != ISTATE_NOTHREAD) { 546 if (info->i_reclist == NULL) { 547 report_stray_interrupt(intr, info); 548 } else { 549 #ifdef SMP 550 if (info->i_thread.td_gd == mycpu) { 551 if (info->i_running == 0) { 552 info->i_running = 1; 553 if (info->i_state != ISTATE_LIVELOCKED) 554 lwkt_schedule(&info->i_thread); /* MIGHT PREEMPT */ 555 } 556 } else { 557 lwkt_send_ipiq(info->i_thread.td_gd, 558 sched_ithd_remote, (void *)(intptr_t)intr); 559 } 560 #else 561 if (info->i_running == 0) { 562 info->i_running = 1; 563 if (info->i_state != ISTATE_LIVELOCKED) 564 lwkt_schedule(&info->i_thread); /* MIGHT PREEMPT */ 565 } 566 #endif 567 } 568 } else { 569 report_stray_interrupt(intr, info); 570 } 571 } 572 573 static void 574 report_stray_interrupt(int intr, struct intr_info *info) 575 { 576 ++info->i_straycount; 577 if (info->i_straycount < 10) { 578 if (info->i_errorticks == ticks) 579 return; 580 info->i_errorticks = ticks; 581 kprintf("sched_ithd: stray interrupt %d on cpu %d\n", 582 intr, mycpuid); 583 } else if (info->i_straycount == 10) { 584 kprintf("sched_ithd: %ld stray interrupts %d on cpu %d - " 585 "there will be no further reports\n", 586 info->i_straycount, intr, mycpuid); 587 } 588 } 589 590 /* 591 * This is run from a periodic SYSTIMER (and thus must be MP safe, the BGL 592 * might not be held). 593 */ 594 static void 595 ithread_livelock_wakeup(systimer_t st) 596 { 597 struct intr_info *info; 598 599 info = &intr_info_ary[(int)(intptr_t)st->data]; 600 if (info->i_state != ISTATE_NOTHREAD) 601 lwkt_schedule(&info->i_thread); 602 } 603 604 /* 605 * Schedule ithread within fast intr handler 606 * 607 * XXX Protect sched_ithd() call with gd_intr_nesting_level? 608 * Interrupts aren't enabled, but still... 609 */ 610 static __inline void 611 ithread_fast_sched(int intr, thread_t td) 612 { 613 ++td->td_nest_count; 614 615 /* 616 * We are already in critical section, exit it now to 617 * allow preemption. 618 */ 619 crit_exit_quick(td); 620 sched_ithd(intr); 621 crit_enter_quick(td); 622 623 --td->td_nest_count; 624 } 625 626 /* 627 * This function is called directly from the ICU or APIC vector code assembly 628 * to process an interrupt. The critical section and interrupt deferral 629 * checks have already been done but the function is entered WITHOUT 630 * a critical section held. The BGL may or may not be held. 631 * 632 * Must return non-zero if we do not want the vector code to re-enable 633 * the interrupt (which we don't if we have to schedule the interrupt) 634 */ 635 int ithread_fast_handler(struct intrframe *frame); 636 637 int 638 ithread_fast_handler(struct intrframe *frame) 639 { 640 int intr; 641 struct intr_info *info; 642 struct intrec **list; 643 int must_schedule; 644 #ifdef SMP 645 int got_mplock; 646 #endif 647 TD_INVARIANTS_DECLARE; 648 intrec_t rec, nrec; 649 globaldata_t gd; 650 thread_t td; 651 652 intr = frame->if_vec; 653 gd = mycpu; 654 td = curthread; 655 656 /* We must be in critical section. */ 657 KKASSERT(td->td_critcount); 658 659 info = &intr_info_ary[intr]; 660 661 /* 662 * If we are not processing any FAST interrupts, just schedule the thing. 663 */ 664 if (info->i_fast == 0) { 665 ++gd->gd_cnt.v_intr; 666 ithread_fast_sched(intr, td); 667 return(1); 668 } 669 670 /* 671 * This should not normally occur since interrupts ought to be 672 * masked if the ithread has been scheduled or is running. 673 */ 674 if (info->i_running) 675 return(1); 676 677 /* 678 * Bump the interrupt nesting level to process any FAST interrupts. 679 * Obtain the MP lock as necessary. If the MP lock cannot be obtained, 680 * schedule the interrupt thread to deal with the issue instead. 681 * 682 * To reduce overhead, just leave the MP lock held once it has been 683 * obtained. 684 */ 685 ++gd->gd_intr_nesting_level; 686 ++gd->gd_cnt.v_intr; 687 must_schedule = info->i_slow; 688 #ifdef SMP 689 got_mplock = 0; 690 #endif 691 692 TD_INVARIANTS_GET(td); 693 list = &info->i_reclist; 694 695 for (rec = *list; rec; rec = nrec) { 696 /* rec may be invalid after call */ 697 nrec = rec->next; 698 699 if (rec->intr_flags & INTR_CLOCK) { 700 #ifdef SMP 701 if ((rec->intr_flags & INTR_MPSAFE) == 0 && got_mplock == 0) { 702 if (try_mplock() == 0) { 703 /* Couldn't get the MP lock; just schedule it. */ 704 must_schedule = 1; 705 break; 706 } 707 got_mplock = 1; 708 } 709 #endif 710 if (rec->serializer) { 711 must_schedule += lwkt_serialize_handler_try( 712 rec->serializer, rec->handler, 713 rec->argument, frame); 714 } else { 715 rec->handler(rec->argument, frame); 716 } 717 TD_INVARIANTS_TEST(td, rec->name); 718 } 719 } 720 721 /* 722 * Cleanup 723 */ 724 --gd->gd_intr_nesting_level; 725 #ifdef SMP 726 if (got_mplock) 727 rel_mplock(); 728 #endif 729 730 /* 731 * If we had a problem, or mixed fast and slow interrupt handlers are 732 * registered, schedule the ithread to catch the missed records (it 733 * will just re-run all of them). A return value of 0 indicates that 734 * all handlers have been run and the interrupt can be re-enabled, and 735 * a non-zero return indicates that the interrupt thread controls 736 * re-enablement. 737 */ 738 if (must_schedule > 0) 739 ithread_fast_sched(intr, td); 740 else if (must_schedule == 0) 741 ++info->i_count; 742 return(must_schedule); 743 } 744 745 /* 746 * Interrupt threads run this as their main loop. 747 * 748 * The handler begins execution outside a critical section and no MP lock. 749 * 750 * The i_running state starts at 0. When an interrupt occurs, the hardware 751 * interrupt is disabled and sched_ithd() The HW interrupt remains disabled 752 * until all routines have run. We then call ithread_done() to reenable 753 * the HW interrupt and deschedule us until the next interrupt. 754 * 755 * We are responsible for atomically checking i_running and ithread_done() 756 * is responsible for atomically checking for platform-specific delayed 757 * interrupts. i_running for our irq is only set in the context of our cpu, 758 * so a critical section is a sufficient interlock. 759 */ 760 #define LIVELOCK_TIMEFRAME(freq) ((freq) >> 2) /* 1/4 second */ 761 762 static void 763 ithread_handler(void *arg) 764 { 765 struct intr_info *info; 766 int use_limit; 767 __uint32_t lseconds; 768 int intr; 769 int mpheld; 770 struct intrec **list; 771 intrec_t rec, nrec; 772 globaldata_t gd; 773 struct systimer ill_timer; /* enforced freq. timer */ 774 u_int ill_count; /* interrupt livelock counter */ 775 TD_INVARIANTS_DECLARE; 776 777 ill_count = 0; 778 intr = (int)(intptr_t)arg; 779 info = &intr_info_ary[intr]; 780 list = &info->i_reclist; 781 782 /* 783 * The loop must be entered with one critical section held. The thread 784 * does not hold the mplock on startup. 785 */ 786 gd = mycpu; 787 lseconds = gd->gd_time_seconds; 788 crit_enter_gd(gd); 789 mpheld = 0; 790 791 for (;;) { 792 /* 793 * The chain is only considered MPSAFE if all its interrupt handlers 794 * are MPSAFE. However, if intr_mpsafe has been turned off we 795 * always operate with the BGL. 796 */ 797 #ifdef SMP 798 if (info->i_mplock_required != mpheld) { 799 if (info->i_mplock_required) { 800 KKASSERT(mpheld == 0); 801 get_mplock(); 802 mpheld = 1; 803 } else { 804 KKASSERT(mpheld != 0); 805 rel_mplock(); 806 mpheld = 0; 807 } 808 } 809 #endif 810 811 TD_INVARIANTS_GET(gd->gd_curthread); 812 813 /* 814 * If an interrupt is pending, clear i_running and execute the 815 * handlers. Note that certain types of interrupts can re-trigger 816 * and set i_running again. 817 * 818 * Each handler is run in a critical section. Note that we run both 819 * FAST and SLOW designated service routines. 820 */ 821 if (info->i_running) { 822 ++ill_count; 823 info->i_running = 0; 824 825 if (*list == NULL) 826 report_stray_interrupt(intr, info); 827 828 for (rec = *list; rec; rec = nrec) { 829 /* rec may be invalid after call */ 830 nrec = rec->next; 831 if (rec->serializer) { 832 lwkt_serialize_handler_call(rec->serializer, rec->handler, 833 rec->argument, NULL); 834 } else { 835 rec->handler(rec->argument, NULL); 836 } 837 TD_INVARIANTS_TEST(gd->gd_curthread, rec->name); 838 } 839 } 840 841 /* 842 * This is our interrupt hook to add rate randomness to the random 843 * number generator. 844 */ 845 if (info->i_random.sc_enabled > 0) 846 add_interrupt_randomness(intr); 847 848 /* 849 * Unmask the interrupt to allow it to trigger again. This only 850 * applies to certain types of interrupts (typ level interrupts). 851 * This can result in the interrupt retriggering, but the retrigger 852 * will not be processed until we cycle our critical section. 853 * 854 * Only unmask interrupts while handlers are installed. It is 855 * possible to hit a situation where no handlers are installed 856 * due to a device driver livelocking and then tearing down its 857 * interrupt on close (the parallel bus being a good example). 858 */ 859 if (*list) 860 machintr_intren(intr); 861 862 /* 863 * Do a quick exit/enter to catch any higher-priority interrupt 864 * sources, such as the statclock, so thread time accounting 865 * will still work. This may also cause an interrupt to re-trigger. 866 */ 867 crit_exit_gd(gd); 868 crit_enter_gd(gd); 869 870 /* 871 * LIVELOCK STATE MACHINE 872 */ 873 switch(info->i_state) { 874 case ISTATE_NORMAL: 875 /* 876 * Reset the count each second. 877 */ 878 if (lseconds != gd->gd_time_seconds) { 879 lseconds = gd->gd_time_seconds; 880 ill_count = 0; 881 } 882 883 /* 884 * If we did not exceed the frequency limit, we are done. 885 * If the interrupt has not retriggered we deschedule ourselves. 886 */ 887 if (ill_count <= livelock_limit) { 888 if (info->i_running == 0) { 889 lwkt_deschedule_self(gd->gd_curthread); 890 lwkt_switch(); 891 } 892 break; 893 } 894 895 /* 896 * Otherwise we are livelocked. Set up a periodic systimer 897 * to wake the thread up at the limit frequency. 898 */ 899 kprintf("intr %d at %d/%d hz, livelocked limit engaged!\n", 900 intr, ill_count, livelock_limit); 901 info->i_state = ISTATE_LIVELOCKED; 902 if ((use_limit = livelock_limit) < 100) 903 use_limit = 100; 904 else if (use_limit > 500000) 905 use_limit = 500000; 906 systimer_init_periodic_nq(&ill_timer, ithread_livelock_wakeup, 907 (void *)(intptr_t)intr, use_limit); 908 /* fall through */ 909 case ISTATE_LIVELOCKED: 910 /* 911 * Wait for our periodic timer to go off. Since the interrupt 912 * has re-armed it can still set i_running, but it will not 913 * reschedule us while we are in a livelocked state. 914 */ 915 lwkt_deschedule_self(gd->gd_curthread); 916 lwkt_switch(); 917 918 /* 919 * Check once a second to see if the livelock condition no 920 * longer applies. 921 */ 922 if (lseconds != gd->gd_time_seconds) { 923 lseconds = gd->gd_time_seconds; 924 if (ill_count < livelock_lowater) { 925 info->i_state = ISTATE_NORMAL; 926 systimer_del(&ill_timer); 927 kprintf("intr %d at %d/%d hz, livelock removed\n", 928 intr, ill_count, livelock_lowater); 929 } else if (livelock_debug == intr || 930 (bootverbose && cold)) { 931 kprintf("intr %d at %d/%d hz, in livelock\n", 932 intr, ill_count, livelock_lowater); 933 } 934 ill_count = 0; 935 } 936 break; 937 } 938 } 939 /* not reached */ 940 } 941 942 /* 943 * Emergency interrupt polling thread. The thread begins execution 944 * outside a critical section with the BGL held. 945 * 946 * If emergency interrupt polling is enabled, this thread will 947 * execute all system interrupts not marked INTR_NOPOLL at the 948 * specified polling frequency. 949 * 950 * WARNING! This thread runs *ALL* interrupt service routines that 951 * are not marked INTR_NOPOLL, which basically means everything except 952 * the 8254 clock interrupt and the ATA interrupt. It has very high 953 * overhead and should only be used in situations where the machine 954 * cannot otherwise be made to work. Due to the severe performance 955 * degredation, it should not be enabled on production machines. 956 */ 957 static void 958 ithread_emergency(void *arg __unused) 959 { 960 struct intr_info *info; 961 intrec_t rec, nrec; 962 int intr; 963 thread_t td __debugvar = curthread; 964 TD_INVARIANTS_DECLARE; 965 966 get_mplock(); 967 TD_INVARIANTS_GET(td); 968 969 for (;;) { 970 for (intr = 0; intr < max_installed_hard_intr; ++intr) { 971 info = &intr_info_ary[intr]; 972 for (rec = info->i_reclist; rec; rec = nrec) { 973 /* rec may be invalid after call */ 974 nrec = rec->next; 975 if ((rec->intr_flags & INTR_NOPOLL) == 0) { 976 if (rec->serializer) { 977 lwkt_serialize_handler_try(rec->serializer, 978 rec->handler, rec->argument, NULL); 979 } else { 980 rec->handler(rec->argument, NULL); 981 } 982 TD_INVARIANTS_TEST(td, rec->name); 983 } 984 } 985 } 986 lwkt_deschedule_self(curthread); 987 lwkt_switch(); 988 } 989 } 990 991 /* 992 * Systimer callback - schedule the emergency interrupt poll thread 993 * if emergency polling is enabled. 994 */ 995 static 996 void 997 emergency_intr_timer_callback(systimer_t info, struct intrframe *frame __unused) 998 { 999 if (emergency_intr_enable) 1000 lwkt_schedule(info->data); 1001 } 1002 1003 int 1004 ithread_cpuid(int intr) 1005 { 1006 const struct intr_info *info; 1007 1008 KKASSERT(intr >= 0 && intr < MAX_INTS); 1009 info = &intr_info_ary[intr]; 1010 1011 if (info->i_state == ISTATE_NOTHREAD) 1012 return -1; 1013 return info->i_thread.td_gd->gd_cpuid; 1014 } 1015 1016 /* 1017 * Sysctls used by systat and others: hw.intrnames and hw.intrcnt. 1018 * The data for this machine dependent, and the declarations are in machine 1019 * dependent code. The layout of intrnames and intrcnt however is machine 1020 * independent. 1021 * 1022 * We do not know the length of intrcnt and intrnames at compile time, so 1023 * calculate things at run time. 1024 */ 1025 1026 static int 1027 sysctl_intrnames(SYSCTL_HANDLER_ARGS) 1028 { 1029 struct intr_info *info; 1030 intrec_t rec; 1031 int error = 0; 1032 int len; 1033 int intr; 1034 char buf[64]; 1035 1036 for (intr = 0; error == 0 && intr < MAX_INTS; ++intr) { 1037 info = &intr_info_ary[intr]; 1038 1039 len = 0; 1040 buf[0] = 0; 1041 for (rec = info->i_reclist; rec; rec = rec->next) { 1042 ksnprintf(buf + len, sizeof(buf) - len, "%s%s", 1043 (len ? "/" : ""), rec->name); 1044 len += strlen(buf + len); 1045 } 1046 if (len == 0) { 1047 ksnprintf(buf, sizeof(buf), "irq%d", intr); 1048 len = strlen(buf); 1049 } 1050 error = SYSCTL_OUT(req, buf, len + 1); 1051 } 1052 return (error); 1053 } 1054 1055 1056 SYSCTL_PROC(_hw, OID_AUTO, intrnames, CTLTYPE_OPAQUE | CTLFLAG_RD, 1057 NULL, 0, sysctl_intrnames, "", "Interrupt Names"); 1058 1059 static int 1060 sysctl_intrcnt(SYSCTL_HANDLER_ARGS) 1061 { 1062 struct intr_info *info; 1063 int error = 0; 1064 int intr; 1065 1066 for (intr = 0; intr < max_installed_hard_intr; ++intr) { 1067 info = &intr_info_ary[intr]; 1068 1069 error = SYSCTL_OUT(req, &info->i_count, sizeof(info->i_count)); 1070 if (error) 1071 goto failed; 1072 } 1073 for (intr = FIRST_SOFTINT; intr < max_installed_soft_intr; ++intr) { 1074 info = &intr_info_ary[intr]; 1075 1076 error = SYSCTL_OUT(req, &info->i_count, sizeof(info->i_count)); 1077 if (error) 1078 goto failed; 1079 } 1080 failed: 1081 return(error); 1082 } 1083 1084 SYSCTL_PROC(_hw, OID_AUTO, intrcnt, CTLTYPE_OPAQUE | CTLFLAG_RD, 1085 NULL, 0, sysctl_intrcnt, "", "Interrupt Counts"); 1086 1087 static void 1088 int_moveto_destcpu(int *orig_cpuid0, int *cpuid0, int intr) 1089 { 1090 int orig_cpuid = mycpuid, cpuid; 1091 char envpath[32]; 1092 1093 cpuid = orig_cpuid; 1094 ksnprintf(envpath, sizeof(envpath), "hw.irq.%d.dest", intr); 1095 kgetenv_int(envpath, &cpuid); 1096 if (cpuid >= ncpus) 1097 cpuid = orig_cpuid; 1098 1099 if (cpuid != orig_cpuid) 1100 lwkt_migratecpu(cpuid); 1101 1102 *orig_cpuid0 = orig_cpuid; 1103 *cpuid0 = cpuid; 1104 } 1105 1106 static void 1107 int_moveto_origcpu(int orig_cpuid, int cpuid) 1108 { 1109 if (cpuid != orig_cpuid) 1110 lwkt_migratecpu(orig_cpuid); 1111 } 1112