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.29 2005/10/26 01:16:04 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 #include <sys/serialize.h> 41 #include <sys/bus.h> 42 43 #include <machine/ipl.h> 44 #include <machine/frame.h> 45 46 #include <sys/interrupt.h> 47 48 typedef struct intrec { 49 struct intrec *next; 50 inthand2_t *handler; 51 void *argument; 52 char *name; 53 int intr; 54 int intr_flags; 55 struct lwkt_serialize *serializer; 56 } *intrec_t; 57 58 struct intr_info { 59 intrec_t i_reclist; 60 struct thread i_thread; 61 struct random_softc i_random; 62 int i_running; 63 long i_count; 64 int i_fast; 65 int i_slow; 66 int i_state; 67 } intr_info_ary[NHWI + NSWI]; 68 69 #define EMERGENCY_INTR_POLLING_FREQ_MAX 20000 70 71 static int sysctl_emergency_freq(SYSCTL_HANDLER_ARGS); 72 static int sysctl_emergency_enable(SYSCTL_HANDLER_ARGS); 73 static void emergency_intr_timer_callback(systimer_t, struct intrframe *); 74 static void ithread_handler(void *arg); 75 static void ithread_emergency(void *arg); 76 77 int intr_info_size = sizeof(intr_info_ary) / sizeof(intr_info_ary[0]); 78 79 static struct systimer emergency_intr_timer; 80 static struct thread emergency_intr_thread; 81 82 #define ISTATE_NOTHREAD 0 83 #define ISTATE_NORMAL 1 84 #define ISTATE_LIVELOCKED 2 85 86 static int livelock_limit = 50000; 87 static int livelock_lowater = 20000; 88 SYSCTL_INT(_kern, OID_AUTO, livelock_limit, 89 CTLFLAG_RW, &livelock_limit, 0, "Livelock interrupt rate limit"); 90 SYSCTL_INT(_kern, OID_AUTO, livelock_lowater, 91 CTLFLAG_RW, &livelock_lowater, 0, "Livelock low-water mark restore"); 92 93 static int emergency_intr_enable = 0; /* emergency interrupt polling */ 94 TUNABLE_INT("kern.emergency_intr_enable", &emergency_intr_enable); 95 SYSCTL_PROC(_kern, OID_AUTO, emergency_intr_enable, CTLTYPE_INT | CTLFLAG_RW, 96 0, 0, sysctl_emergency_enable, "I", "Emergency Interrupt Poll Enable"); 97 98 static int emergency_intr_freq = 10; /* emergency polling frequency */ 99 TUNABLE_INT("kern.emergency_intr_freq", &emergency_intr_freq); 100 SYSCTL_PROC(_kern, OID_AUTO, emergency_intr_freq, CTLTYPE_INT | CTLFLAG_RW, 101 0, 0, sysctl_emergency_freq, "I", "Emergency Interrupt Poll Frequency"); 102 103 /* 104 * Sysctl support routines 105 */ 106 static int 107 sysctl_emergency_enable(SYSCTL_HANDLER_ARGS) 108 { 109 int error, enabled; 110 111 enabled = emergency_intr_enable; 112 error = sysctl_handle_int(oidp, &enabled, 0, req); 113 if (error || req->newptr == NULL) 114 return error; 115 emergency_intr_enable = enabled; 116 if (emergency_intr_enable) { 117 emergency_intr_timer.periodic = 118 sys_cputimer->fromhz(emergency_intr_freq); 119 } else { 120 emergency_intr_timer.periodic = sys_cputimer->fromhz(1); 121 } 122 return 0; 123 } 124 125 static int 126 sysctl_emergency_freq(SYSCTL_HANDLER_ARGS) 127 { 128 int error, phz; 129 130 phz = emergency_intr_freq; 131 error = sysctl_handle_int(oidp, &phz, 0, req); 132 if (error || req->newptr == NULL) 133 return error; 134 if (phz <= 0) 135 return EINVAL; 136 else if (phz > EMERGENCY_INTR_POLLING_FREQ_MAX) 137 phz = EMERGENCY_INTR_POLLING_FREQ_MAX; 138 139 emergency_intr_freq = phz; 140 if (emergency_intr_enable) { 141 emergency_intr_timer.periodic = 142 sys_cputimer->fromhz(emergency_intr_freq); 143 } else { 144 emergency_intr_timer.periodic = sys_cputimer->fromhz(1); 145 } 146 return 0; 147 } 148 149 /* 150 * Register an SWI or INTerrupt handler. 151 */ 152 void * 153 register_swi(int intr, inthand2_t *handler, void *arg, const char *name, 154 struct lwkt_serialize *serializer) 155 { 156 if (intr < NHWI || intr >= NHWI + NSWI) 157 panic("register_swi: bad intr %d", intr); 158 return(register_int(intr, handler, arg, name, serializer, 0)); 159 } 160 161 void * 162 register_int(int intr, inthand2_t *handler, void *arg, const char *name, 163 struct lwkt_serialize *serializer, int intr_flags) 164 { 165 struct intr_info *info; 166 struct intrec **list; 167 intrec_t rec; 168 169 if (intr < 0 || intr >= NHWI + NSWI) 170 panic("register_int: bad intr %d", intr); 171 if (name == NULL) 172 name = "???"; 173 info = &intr_info_ary[intr]; 174 175 rec = malloc(sizeof(struct intrec), M_DEVBUF, M_INTWAIT); 176 rec->name = malloc(strlen(name) + 1, M_DEVBUF, M_INTWAIT); 177 strcpy(rec->name, name); 178 179 rec->handler = handler; 180 rec->argument = arg; 181 rec->intr = intr; 182 rec->intr_flags = intr_flags; 183 rec->next = NULL; 184 rec->serializer = serializer; 185 186 list = &info->i_reclist; 187 188 /* 189 * Keep track of how many fast and slow interrupts we have. 190 */ 191 if (intr_flags & INTR_FAST) 192 ++info->i_fast; 193 else 194 ++info->i_slow; 195 196 /* 197 * Create an emergency polling thread and set up a systimer to wake 198 * it up. 199 */ 200 if (emergency_intr_thread.td_kstack == NULL) { 201 lwkt_create(ithread_emergency, NULL, NULL, 202 &emergency_intr_thread, TDF_STOPREQ|TDF_INTTHREAD, -1, 203 "ithread emerg"); 204 systimer_init_periodic_nq(&emergency_intr_timer, 205 emergency_intr_timer_callback, &emergency_intr_thread, 206 (emergency_intr_enable ? emergency_intr_freq : 1)); 207 } 208 209 /* 210 * Create an interrupt thread if necessary, leave it in an unscheduled 211 * state. 212 */ 213 if (info->i_state == ISTATE_NOTHREAD) { 214 info->i_state = ISTATE_NORMAL; 215 lwkt_create((void *)ithread_handler, (void *)intr, NULL, 216 &info->i_thread, TDF_STOPREQ|TDF_INTTHREAD, -1, 217 "ithread %d", intr); 218 if (intr >= NHWI && intr < NHWI + NSWI) 219 lwkt_setpri(&info->i_thread, TDPRI_SOFT_NORM); 220 else 221 lwkt_setpri(&info->i_thread, TDPRI_INT_MED); 222 info->i_thread.td_preemptable = lwkt_preempt; 223 } 224 225 /* 226 * Add the record to the interrupt list 227 */ 228 crit_enter(); /* token */ 229 while (*list != NULL) 230 list = &(*list)->next; 231 *list = rec; 232 crit_exit(); 233 return(rec); 234 } 235 236 int 237 unregister_swi(void *id) 238 { 239 return(unregister_int(id)); 240 } 241 242 int 243 unregister_int(void *id) 244 { 245 struct intr_info *info; 246 struct intrec **list; 247 intrec_t rec; 248 int intr; 249 250 intr = ((intrec_t)id)->intr; 251 252 if (intr < 0 || intr > NHWI + NSWI) 253 panic("register_int: bad intr %d", intr); 254 255 info = &intr_info_ary[intr]; 256 257 /* 258 * Remove the interrupt descriptor 259 */ 260 crit_enter(); 261 list = &info->i_reclist; 262 while ((rec = *list) != NULL) { 263 if (rec == id) { 264 *list = rec->next; 265 break; 266 } 267 list = &rec->next; 268 } 269 crit_exit(); 270 271 /* 272 * Free it, adjust interrupt type counts 273 */ 274 if (rec != NULL) { 275 if (rec->intr_flags & INTR_FAST) 276 --info->i_fast; 277 else 278 --info->i_slow; 279 free(rec->name, M_DEVBUF); 280 free(rec, M_DEVBUF); 281 } else { 282 printf("warning: unregister_int: int %d handler for %s not found\n", 283 intr, ((intrec_t)id)->name); 284 } 285 286 /* 287 * Return the number of interrupt vectors still registered on this intr 288 */ 289 return(info->i_fast + info->i_slow); 290 } 291 292 int 293 get_registered_intr(void *id) 294 { 295 return(((intrec_t)id)->intr); 296 } 297 298 const char * 299 get_registered_name(int intr) 300 { 301 intrec_t rec; 302 303 if (intr < 0 || intr > NHWI + NSWI) 304 panic("register_int: bad intr %d", intr); 305 306 if ((rec = intr_info_ary[intr].i_reclist) == NULL) 307 return(NULL); 308 else if (rec->next) 309 return("mux"); 310 else 311 return(rec->name); 312 } 313 314 int 315 count_registered_ints(int intr) 316 { 317 struct intr_info *info; 318 319 if (intr < 0 || intr > NHWI + NSWI) 320 panic("register_int: bad intr %d", intr); 321 info = &intr_info_ary[intr]; 322 return(info->i_fast + info->i_slow); 323 } 324 325 long 326 get_interrupt_counter(int intr) 327 { 328 struct intr_info *info; 329 330 if (intr < 0 || intr > NHWI + NSWI) 331 panic("register_int: bad intr %d", intr); 332 info = &intr_info_ary[intr]; 333 return(info->i_count); 334 } 335 336 337 void 338 swi_setpriority(int intr, int pri) 339 { 340 struct intr_info *info; 341 342 if (intr < NHWI || intr >= NHWI + NSWI) 343 panic("register_swi: bad intr %d", intr); 344 info = &intr_info_ary[intr]; 345 if (info->i_state != ISTATE_NOTHREAD) 346 lwkt_setpri(&info->i_thread, pri); 347 } 348 349 void 350 register_randintr(int intr) 351 { 352 struct intr_info *info; 353 354 if ((unsigned int)intr >= NHWI + NSWI) 355 panic("register_randintr: bad intr %d", intr); 356 info = &intr_info_ary[intr]; 357 info->i_random.sc_intr = intr; 358 info->i_random.sc_enabled = 1; 359 } 360 361 void 362 unregister_randintr(int intr) 363 { 364 struct intr_info *info; 365 366 if (intr < NHWI || intr >= NHWI + NSWI) 367 panic("register_swi: bad intr %d", intr); 368 info = &intr_info_ary[intr]; 369 info->i_random.sc_enabled = 0; 370 } 371 372 /* 373 * Dispatch an interrupt. If there's nothing to do we have a stray 374 * interrupt and can just return, leaving the interrupt masked. 375 * 376 * We need to schedule the interrupt and set its i_running bit. If 377 * we are not on the interrupt thread's cpu we have to send a message 378 * to the correct cpu that will issue the desired action (interlocking 379 * with the interrupt thread's critical section). We do NOT attempt to 380 * reschedule interrupts whos i_running bit is already set because 381 * this would prematurely wakeup a livelock-limited interrupt thread. 382 * 383 * i_running is only tested/set on the same cpu as the interrupt thread. 384 * 385 * We are NOT in a critical section, which will allow the scheduled 386 * interrupt to preempt us. The MP lock might *NOT* be held here. 387 */ 388 #ifdef SMP 389 390 static void 391 sched_ithd_remote(void *arg) 392 { 393 sched_ithd((int)arg); 394 } 395 396 #endif 397 398 void 399 sched_ithd(int intr) 400 { 401 struct intr_info *info; 402 403 info = &intr_info_ary[intr]; 404 405 ++info->i_count; 406 if (info->i_state != ISTATE_NOTHREAD) { 407 if (info->i_reclist == NULL) { 408 printf("sched_ithd: stray interrupt %d\n", intr); 409 } else { 410 #ifdef SMP 411 if (info->i_thread.td_gd == mycpu) { 412 if (info->i_running == 0) { 413 info->i_running = 1; 414 if (info->i_state != ISTATE_LIVELOCKED) 415 lwkt_schedule(&info->i_thread); /* MIGHT PREEMPT */ 416 } 417 } else { 418 lwkt_send_ipiq(info->i_thread.td_gd, 419 sched_ithd_remote, (void *)intr); 420 } 421 #else 422 if (info->i_running == 0) { 423 info->i_running = 1; 424 if (info->i_state != ISTATE_LIVELOCKED) 425 lwkt_schedule(&info->i_thread); /* MIGHT PREEMPT */ 426 } 427 #endif 428 } 429 } else { 430 printf("sched_ithd: stray interrupt %d\n", intr); 431 } 432 } 433 434 /* 435 * This is run from a periodic SYSTIMER (and thus must be MP safe, the BGL 436 * might not be held). 437 */ 438 static void 439 ithread_livelock_wakeup(systimer_t st) 440 { 441 struct intr_info *info; 442 443 info = &intr_info_ary[(int)st->data]; 444 if (info->i_state != ISTATE_NOTHREAD) 445 lwkt_schedule(&info->i_thread); 446 } 447 448 /* 449 * This function is called drectly from the ICU or APIC vector code assembly 450 * to process an interrupt. The critical section and interrupt deferral 451 * checks have already been done but the function is entered WITHOUT 452 * a critical section held. The BGL may or may not be held. 453 * 454 * Must return non-zero if we do not want the vector code to re-enable 455 * the interrupt (which we don't if we have to schedule the interrupt) 456 */ 457 int ithread_fast_handler(struct intrframe frame); 458 459 int 460 ithread_fast_handler(struct intrframe frame) 461 { 462 int intr; 463 struct intr_info *info; 464 struct intrec **list; 465 int must_schedule; 466 #ifdef SMP 467 int got_mplock; 468 #endif 469 intrec_t rec, next_rec; 470 globaldata_t gd; 471 472 intr = frame.if_vec; 473 gd = mycpu; 474 475 info = &intr_info_ary[intr]; 476 477 /* 478 * If we are not processing any FAST interrupts, just schedule the thing. 479 * (since we aren't in a critical section, this can result in a 480 * preemption) 481 */ 482 if (info->i_fast == 0) { 483 sched_ithd(intr); 484 return(1); 485 } 486 487 /* 488 * This should not normally occur since interrupts ought to be 489 * masked if the ithread has been scheduled or is running. 490 */ 491 if (info->i_running) 492 return(1); 493 494 /* 495 * Bump the interrupt nesting level to process any FAST interrupts. 496 * Obtain the MP lock as necessary. If the MP lock cannot be obtained, 497 * schedule the interrupt thread to deal with the issue instead. 498 * 499 * To reduce overhead, just leave the MP lock held once it has been 500 * obtained. 501 */ 502 crit_enter_gd(gd); 503 ++gd->gd_intr_nesting_level; 504 ++gd->gd_cnt.v_intr; 505 must_schedule = info->i_slow; 506 #ifdef SMP 507 got_mplock = 0; 508 #endif 509 510 list = &info->i_reclist; 511 for (rec = *list; rec; rec = next_rec) { 512 next_rec = rec->next; /* rec may be invalid after call */ 513 514 if (rec->intr_flags & INTR_FAST) { 515 #ifdef SMP 516 if ((rec->intr_flags & INTR_MPSAFE) == 0 && got_mplock == 0) { 517 if (try_mplock() == 0) { 518 /* 519 * XXX forward to the cpu holding the MP lock 520 */ 521 must_schedule = 1; 522 break; 523 } 524 got_mplock = 1; 525 } 526 #endif 527 if (rec->serializer) { 528 must_schedule += lwkt_serialize_handler_try( 529 rec->serializer, rec->handler, 530 rec->argument, &frame); 531 } else { 532 rec->handler(rec->argument, &frame); 533 } 534 } 535 } 536 537 /* 538 * Cleanup 539 */ 540 --gd->gd_intr_nesting_level; 541 #ifdef SMP 542 if (got_mplock) 543 rel_mplock(); 544 #endif 545 crit_exit_gd(gd); 546 547 /* 548 * If we had a problem, schedule the thread to catch the missed 549 * records (it will just re-run all of them). A return value of 0 550 * indicates that all handlers have been run and the interrupt can 551 * be re-enabled, and a non-zero return indicates that the interrupt 552 * thread controls re-enablement. 553 */ 554 if (must_schedule) 555 sched_ithd(intr); 556 else 557 ++info->i_count; 558 return(must_schedule); 559 } 560 561 #if 0 562 563 6: ; \ 564 /* could not get the MP lock, forward the interrupt */ \ 565 movl mp_lock, %eax ; /* check race */ \ 566 cmpl $MP_FREE_LOCK,%eax ; \ 567 je 2b ; \ 568 incl PCPU(cnt)+V_FORWARDED_INTS ; \ 569 subl $12,%esp ; \ 570 movl $irq_num,8(%esp) ; \ 571 movl $forward_fastint_remote,4(%esp) ; \ 572 movl %eax,(%esp) ; \ 573 call lwkt_send_ipiq_bycpu ; \ 574 addl $12,%esp ; \ 575 jmp 5f ; 576 577 #endif 578 579 580 /* 581 * Interrupt threads run this as their main loop. 582 * 583 * The handler begins execution outside a critical section and with the BGL 584 * held. 585 * 586 * The i_running state starts at 0. When an interrupt occurs, the hardware 587 * interrupt is disabled and sched_ithd() The HW interrupt remains disabled 588 * until all routines have run. We then call ithread_done() to reenable 589 * the HW interrupt and deschedule us until the next interrupt. 590 * 591 * We are responsible for atomically checking i_running and ithread_done() 592 * is responsible for atomically checking for platform-specific delayed 593 * interrupts. i_running for our irq is only set in the context of our cpu, 594 * so a critical section is a sufficient interlock. 595 */ 596 #define LIVELOCK_TIMEFRAME(freq) ((freq) >> 2) /* 1/4 second */ 597 598 static void 599 ithread_handler(void *arg) 600 { 601 struct intr_info *info; 602 int use_limit; 603 int lticks; 604 int lcount; 605 int intr; 606 struct intrec **list; 607 intrec_t rec, nrec; 608 globaldata_t gd; 609 struct systimer ill_timer; /* enforced freq. timer */ 610 u_int ill_count; /* interrupt livelock counter */ 611 612 ill_count = 0; 613 lticks = ticks; 614 lcount = 0; 615 intr = (int)arg; 616 info = &intr_info_ary[intr]; 617 list = &info->i_reclist; 618 gd = mycpu; 619 620 /* 621 * The loop must be entered with one critical section held. 622 */ 623 crit_enter_gd(gd); 624 625 for (;;) { 626 /* 627 * If an interrupt is pending, clear i_running and execute the 628 * handlers. Note that certain types of interrupts can re-trigger 629 * and set i_running again. 630 * 631 * Each handler is run in a critical section. Note that we run both 632 * FAST and SLOW designated service routines. 633 */ 634 if (info->i_running) { 635 ++ill_count; 636 info->i_running = 0; 637 for (rec = *list; rec; rec = nrec) { 638 nrec = rec->next; 639 if (rec->serializer) { 640 lwkt_serialize_handler_call(rec->serializer, rec->handler, 641 rec->argument, NULL); 642 } else { 643 rec->handler(rec->argument, NULL); 644 } 645 } 646 } 647 648 /* 649 * This is our interrupt hook to add rate randomness to the random 650 * number generator. 651 */ 652 if (info->i_random.sc_enabled) 653 add_interrupt_randomness(intr); 654 655 /* 656 * Unmask the interrupt to allow it to trigger again. This only 657 * applies to certain types of interrupts (typ level interrupts). 658 * This can result in the interrupt retriggering, but the retrigger 659 * will not be processed until we cycle our critical section. 660 * 661 * Only unmask interrupts while handlers are installed. It is 662 * possible to hit a situation where no handlers are installed 663 * due to a device driver livelocking and then tearing down its 664 * interrupt on close (the parallel bus being a good example). 665 */ 666 if (*list) 667 ithread_unmask(intr); 668 669 /* 670 * Do a quick exit/enter to catch any higher-priority interrupt 671 * sources, such as the statclock, so thread time accounting 672 * will still work. This may also cause an interrupt to re-trigger. 673 */ 674 crit_exit_gd(gd); 675 crit_enter_gd(gd); 676 677 /* 678 * LIVELOCK STATE MACHINE 679 */ 680 switch(info->i_state) { 681 case ISTATE_NORMAL: 682 /* 683 * Calculate a running average every tick. 684 */ 685 if (lticks != ticks) { 686 lticks = ticks; 687 ill_count -= ill_count / hz; 688 } 689 690 /* 691 * If we did not exceed the frequency limit, we are done. 692 * If the interrupt has not retriggered we deschedule ourselves. 693 */ 694 if (ill_count <= livelock_limit) { 695 if (info->i_running == 0) { 696 lwkt_deschedule_self(gd->gd_curthread); 697 lwkt_switch(); 698 } 699 break; 700 } 701 702 /* 703 * Otherwise we are livelocked. Set up a periodic systimer 704 * to wake the thread up at the limit frequency. 705 */ 706 printf("intr %d at %d > %d hz, livelocked limit engaged!\n", 707 intr, livelock_limit, ill_count); 708 info->i_state = ISTATE_LIVELOCKED; 709 if ((use_limit = livelock_limit) < 100) 710 use_limit = 100; 711 else if (use_limit > 500000) 712 use_limit = 500000; 713 systimer_init_periodic(&ill_timer, ithread_livelock_wakeup, 714 (void *)intr, use_limit); 715 lcount = 0; 716 /* fall through */ 717 case ISTATE_LIVELOCKED: 718 /* 719 * Wait for our periodic timer to go off. Since the interrupt 720 * has re-armed it can still set i_running, but it will not 721 * reschedule us while we are in a livelocked state. 722 */ 723 lwkt_deschedule_self(gd->gd_curthread); 724 lwkt_switch(); 725 726 /* 727 * Check to see if the livelock condition no longer applies. 728 * The interrupt must be able to operate normally for one 729 * full second before we restore normal operation. 730 */ 731 if (lticks != ticks) { 732 lticks = ticks; 733 if (ill_count < livelock_lowater) { 734 if (++lcount >= hz) { 735 info->i_state = ISTATE_NORMAL; 736 systimer_del(&ill_timer); 737 printf("intr %d at %d < %d hz, livelock removed\n", 738 intr, ill_count, livelock_lowater); 739 } 740 } else { 741 lcount = 0; 742 } 743 ill_count -= ill_count / hz; 744 } 745 break; 746 } 747 } 748 /* not reached */ 749 } 750 751 /* 752 * Emergency interrupt polling thread. The thread begins execution 753 * outside a critical section with the BGL held. 754 * 755 * If emergency interrupt polling is enabled, this thread will 756 * execute all system interrupts not marked INTR_NOPOLL at the 757 * specified polling frequency. 758 * 759 * WARNING! This thread runs *ALL* interrupt service routines that 760 * are not marked INTR_NOPOLL, which basically means everything except 761 * the 8254 clock interrupt and the ATA interrupt. It has very high 762 * overhead and should only be used in situations where the machine 763 * cannot otherwise be made to work. Due to the severe performance 764 * degredation, it should not be enabled on production machines. 765 */ 766 static void 767 ithread_emergency(void *arg __unused) 768 { 769 struct intr_info *info; 770 intrec_t rec, nrec; 771 int intr; 772 773 for (;;) { 774 for (intr = 0; intr < NHWI + NSWI; ++intr) { 775 info = &intr_info_ary[intr]; 776 for (rec = info->i_reclist; rec; rec = nrec) { 777 if ((rec->intr_flags & INTR_NOPOLL) == 0) { 778 if (rec->serializer) { 779 lwkt_serialize_handler_call(rec->serializer, 780 rec->handler, rec->argument, NULL); 781 } else { 782 rec->handler(rec->argument, NULL); 783 } 784 } 785 nrec = rec->next; 786 } 787 } 788 lwkt_deschedule_self(curthread); 789 lwkt_switch(); 790 } 791 } 792 793 /* 794 * Systimer callback - schedule the emergency interrupt poll thread 795 * if emergency polling is enabled. 796 */ 797 static 798 void 799 emergency_intr_timer_callback(systimer_t info, struct intrframe *frame __unused) 800 { 801 if (emergency_intr_enable) 802 lwkt_schedule(info->data); 803 } 804 805 /* 806 * Sysctls used by systat and others: hw.intrnames and hw.intrcnt. 807 * The data for this machine dependent, and the declarations are in machine 808 * dependent code. The layout of intrnames and intrcnt however is machine 809 * independent. 810 * 811 * We do not know the length of intrcnt and intrnames at compile time, so 812 * calculate things at run time. 813 */ 814 815 static int 816 sysctl_intrnames(SYSCTL_HANDLER_ARGS) 817 { 818 struct intr_info *info; 819 intrec_t rec; 820 int error = 0; 821 int len; 822 int intr; 823 char buf[64]; 824 825 for (intr = 0; error == 0 && intr < NHWI + NSWI; ++intr) { 826 info = &intr_info_ary[intr]; 827 828 len = 0; 829 buf[0] = 0; 830 for (rec = info->i_reclist; rec; rec = rec->next) { 831 snprintf(buf + len, sizeof(buf) - len, "%s%s", 832 (len ? "/" : ""), rec->name); 833 len += strlen(buf + len); 834 } 835 if (len == 0) { 836 snprintf(buf, sizeof(buf), "irq%d", intr); 837 len = strlen(buf); 838 } 839 error = SYSCTL_OUT(req, buf, len + 1); 840 } 841 return (error); 842 } 843 844 845 SYSCTL_PROC(_hw, OID_AUTO, intrnames, CTLTYPE_OPAQUE | CTLFLAG_RD, 846 NULL, 0, sysctl_intrnames, "", "Interrupt Names"); 847 848 static int 849 sysctl_intrcnt(SYSCTL_HANDLER_ARGS) 850 { 851 struct intr_info *info; 852 int error = 0; 853 int intr; 854 855 for (intr = 0; intr < NHWI + NSWI; ++intr) { 856 info = &intr_info_ary[intr]; 857 858 error = SYSCTL_OUT(req, &info->i_count, sizeof(info->i_count)); 859 if (error) 860 break; 861 } 862 return(error); 863 } 864 865 SYSCTL_PROC(_hw, OID_AUTO, intrcnt, CTLTYPE_OPAQUE | CTLFLAG_RD, 866 NULL, 0, sysctl_intrcnt, "", "Interrupt Counts"); 867 868