1 /* 2 * linux/arch/arm/kernel/irq.c 3 * 4 * Copyright (C) 1992 Linus Torvalds 5 * Modifications for ARM processor Copyright (C) 1995-2000 Russell King. 6 * 7 * Support for Dynamic Tick Timer Copyright (C) 2004-2005 Nokia Corporation. 8 * Dynamic Tick Timer written by Tony Lindgren <tony@atomide.com> and 9 * Tuukka Tikkanen <tuukka.tikkanen@elektrobit.com>. 10 * 11 * This program is free software; you can redistribute it and/or modify 12 * it under the terms of the GNU General Public License version 2 as 13 * published by the Free Software Foundation. 14 * 15 * This file contains the code used by various IRQ handling routines: 16 * asking for different IRQ's should be done through these routines 17 * instead of just grabbing them. Thus setups with different IRQ numbers 18 * shouldn't result in any weird surprises, and installing new handlers 19 * should be easier. 20 * 21 * IRQ's are in fact implemented a bit like signal handlers for the kernel. 22 * Naturally it's not a 1:1 relation, but there are similarities. 23 */ 24 #include <linux/config.h> 25 #include <linux/kernel_stat.h> 26 #include <linux/module.h> 27 #include <linux/signal.h> 28 #include <linux/ioport.h> 29 #include <linux/interrupt.h> 30 #include <linux/ptrace.h> 31 #include <linux/slab.h> 32 #include <linux/random.h> 33 #include <linux/smp.h> 34 #include <linux/init.h> 35 #include <linux/seq_file.h> 36 #include <linux/errno.h> 37 #include <linux/list.h> 38 #include <linux/kallsyms.h> 39 #include <linux/proc_fs.h> 40 41 #include <asm/irq.h> 42 #include <asm/system.h> 43 #include <asm/mach/irq.h> 44 #include <asm/mach/time.h> 45 46 /* 47 * Maximum IRQ count. Currently, this is arbitary. However, it should 48 * not be set too low to prevent false triggering. Conversely, if it 49 * is set too high, then you could miss a stuck IRQ. 50 * 51 * Maybe we ought to set a timer and re-enable the IRQ at a later time? 52 */ 53 #define MAX_IRQ_CNT 100000 54 55 static int noirqdebug; 56 static volatile unsigned long irq_err_count; 57 static DEFINE_SPINLOCK(irq_controller_lock); 58 static LIST_HEAD(irq_pending); 59 60 struct irqdesc irq_desc[NR_IRQS]; 61 void (*init_arch_irq)(void) __initdata = NULL; 62 63 /* 64 * No architecture-specific irq_finish function defined in arm/arch/irqs.h. 65 */ 66 #ifndef irq_finish 67 #define irq_finish(irq) do { } while (0) 68 #endif 69 70 /* 71 * Dummy mask/unmask handler 72 */ 73 void dummy_mask_unmask_irq(unsigned int irq) 74 { 75 } 76 77 irqreturn_t no_action(int irq, void *dev_id, struct pt_regs *regs) 78 { 79 return IRQ_NONE; 80 } 81 82 void do_bad_IRQ(unsigned int irq, struct irqdesc *desc, struct pt_regs *regs) 83 { 84 irq_err_count += 1; 85 printk(KERN_ERR "IRQ: spurious interrupt %d\n", irq); 86 } 87 88 static struct irqchip bad_chip = { 89 .ack = dummy_mask_unmask_irq, 90 .mask = dummy_mask_unmask_irq, 91 .unmask = dummy_mask_unmask_irq, 92 }; 93 94 static struct irqdesc bad_irq_desc = { 95 .chip = &bad_chip, 96 .handle = do_bad_IRQ, 97 .pend = LIST_HEAD_INIT(bad_irq_desc.pend), 98 .disable_depth = 1, 99 }; 100 101 #ifdef CONFIG_SMP 102 void synchronize_irq(unsigned int irq) 103 { 104 struct irqdesc *desc = irq_desc + irq; 105 106 while (desc->running) 107 barrier(); 108 } 109 EXPORT_SYMBOL(synchronize_irq); 110 111 #define smp_set_running(desc) do { desc->running = 1; } while (0) 112 #define smp_clear_running(desc) do { desc->running = 0; } while (0) 113 #else 114 #define smp_set_running(desc) do { } while (0) 115 #define smp_clear_running(desc) do { } while (0) 116 #endif 117 118 /** 119 * disable_irq_nosync - disable an irq without waiting 120 * @irq: Interrupt to disable 121 * 122 * Disable the selected interrupt line. Enables and disables 123 * are nested. We do this lazily. 124 * 125 * This function may be called from IRQ context. 126 */ 127 void disable_irq_nosync(unsigned int irq) 128 { 129 struct irqdesc *desc = irq_desc + irq; 130 unsigned long flags; 131 132 spin_lock_irqsave(&irq_controller_lock, flags); 133 desc->disable_depth++; 134 list_del_init(&desc->pend); 135 spin_unlock_irqrestore(&irq_controller_lock, flags); 136 } 137 EXPORT_SYMBOL(disable_irq_nosync); 138 139 /** 140 * disable_irq - disable an irq and wait for completion 141 * @irq: Interrupt to disable 142 * 143 * Disable the selected interrupt line. Enables and disables 144 * are nested. This functions waits for any pending IRQ 145 * handlers for this interrupt to complete before returning. 146 * If you use this function while holding a resource the IRQ 147 * handler may need you will deadlock. 148 * 149 * This function may be called - with care - from IRQ context. 150 */ 151 void disable_irq(unsigned int irq) 152 { 153 struct irqdesc *desc = irq_desc + irq; 154 155 disable_irq_nosync(irq); 156 if (desc->action) 157 synchronize_irq(irq); 158 } 159 EXPORT_SYMBOL(disable_irq); 160 161 /** 162 * enable_irq - enable interrupt handling on an irq 163 * @irq: Interrupt to enable 164 * 165 * Re-enables the processing of interrupts on this IRQ line. 166 * Note that this may call the interrupt handler, so you may 167 * get unexpected results if you hold IRQs disabled. 168 * 169 * This function may be called from IRQ context. 170 */ 171 void enable_irq(unsigned int irq) 172 { 173 struct irqdesc *desc = irq_desc + irq; 174 unsigned long flags; 175 176 spin_lock_irqsave(&irq_controller_lock, flags); 177 if (unlikely(!desc->disable_depth)) { 178 printk("enable_irq(%u) unbalanced from %p\n", irq, 179 __builtin_return_address(0)); 180 } else if (!--desc->disable_depth) { 181 desc->probing = 0; 182 desc->chip->unmask(irq); 183 184 /* 185 * If the interrupt is waiting to be processed, 186 * try to re-run it. We can't directly run it 187 * from here since the caller might be in an 188 * interrupt-protected region. 189 */ 190 if (desc->pending && list_empty(&desc->pend)) { 191 desc->pending = 0; 192 if (!desc->chip->retrigger || 193 desc->chip->retrigger(irq)) 194 list_add(&desc->pend, &irq_pending); 195 } 196 } 197 spin_unlock_irqrestore(&irq_controller_lock, flags); 198 } 199 EXPORT_SYMBOL(enable_irq); 200 201 /* 202 * Enable wake on selected irq 203 */ 204 void enable_irq_wake(unsigned int irq) 205 { 206 struct irqdesc *desc = irq_desc + irq; 207 unsigned long flags; 208 209 spin_lock_irqsave(&irq_controller_lock, flags); 210 if (desc->chip->set_wake) 211 desc->chip->set_wake(irq, 1); 212 spin_unlock_irqrestore(&irq_controller_lock, flags); 213 } 214 EXPORT_SYMBOL(enable_irq_wake); 215 216 void disable_irq_wake(unsigned int irq) 217 { 218 struct irqdesc *desc = irq_desc + irq; 219 unsigned long flags; 220 221 spin_lock_irqsave(&irq_controller_lock, flags); 222 if (desc->chip->set_wake) 223 desc->chip->set_wake(irq, 0); 224 spin_unlock_irqrestore(&irq_controller_lock, flags); 225 } 226 EXPORT_SYMBOL(disable_irq_wake); 227 228 int show_interrupts(struct seq_file *p, void *v) 229 { 230 int i = *(loff_t *) v, cpu; 231 struct irqaction * action; 232 unsigned long flags; 233 234 if (i == 0) { 235 char cpuname[12]; 236 237 seq_printf(p, " "); 238 for_each_present_cpu(cpu) { 239 sprintf(cpuname, "CPU%d", cpu); 240 seq_printf(p, " %10s", cpuname); 241 } 242 seq_putc(p, '\n'); 243 } 244 245 if (i < NR_IRQS) { 246 spin_lock_irqsave(&irq_controller_lock, flags); 247 action = irq_desc[i].action; 248 if (!action) 249 goto unlock; 250 251 seq_printf(p, "%3d: ", i); 252 for_each_present_cpu(cpu) 253 seq_printf(p, "%10u ", kstat_cpu(cpu).irqs[i]); 254 seq_printf(p, " %s", action->name); 255 for (action = action->next; action; action = action->next) 256 seq_printf(p, ", %s", action->name); 257 258 seq_putc(p, '\n'); 259 unlock: 260 spin_unlock_irqrestore(&irq_controller_lock, flags); 261 } else if (i == NR_IRQS) { 262 #ifdef CONFIG_ARCH_ACORN 263 show_fiq_list(p, v); 264 #endif 265 #ifdef CONFIG_SMP 266 show_ipi_list(p); 267 show_local_irqs(p); 268 #endif 269 seq_printf(p, "Err: %10lu\n", irq_err_count); 270 } 271 return 0; 272 } 273 274 /* 275 * IRQ lock detection. 276 * 277 * Hopefully, this should get us out of a few locked situations. 278 * However, it may take a while for this to happen, since we need 279 * a large number if IRQs to appear in the same jiffie with the 280 * same instruction pointer (or within 2 instructions). 281 */ 282 static int check_irq_lock(struct irqdesc *desc, int irq, struct pt_regs *regs) 283 { 284 unsigned long instr_ptr = instruction_pointer(regs); 285 286 if (desc->lck_jif == jiffies && 287 desc->lck_pc >= instr_ptr && desc->lck_pc < instr_ptr + 8) { 288 desc->lck_cnt += 1; 289 290 if (desc->lck_cnt > MAX_IRQ_CNT) { 291 printk(KERN_ERR "IRQ LOCK: IRQ%d is locking the system, disabled\n", irq); 292 return 1; 293 } 294 } else { 295 desc->lck_cnt = 0; 296 desc->lck_pc = instruction_pointer(regs); 297 desc->lck_jif = jiffies; 298 } 299 return 0; 300 } 301 302 static void 303 report_bad_irq(unsigned int irq, struct pt_regs *regs, struct irqdesc *desc, int ret) 304 { 305 static int count = 100; 306 struct irqaction *action; 307 308 if (!count || noirqdebug) 309 return; 310 311 count--; 312 313 if (ret != IRQ_HANDLED && ret != IRQ_NONE) { 314 printk("irq%u: bogus retval mask %x\n", irq, ret); 315 } else { 316 printk("irq%u: nobody cared\n", irq); 317 } 318 show_regs(regs); 319 dump_stack(); 320 printk(KERN_ERR "handlers:"); 321 action = desc->action; 322 do { 323 printk("\n" KERN_ERR "[<%p>]", action->handler); 324 print_symbol(" (%s)", (unsigned long)action->handler); 325 action = action->next; 326 } while (action); 327 printk("\n"); 328 } 329 330 static int 331 __do_irq(unsigned int irq, struct irqaction *action, struct pt_regs *regs) 332 { 333 unsigned int status; 334 int ret, retval = 0; 335 336 spin_unlock(&irq_controller_lock); 337 338 #ifdef CONFIG_NO_IDLE_HZ 339 if (!(action->flags & SA_TIMER) && system_timer->dyn_tick != NULL) { 340 write_seqlock(&xtime_lock); 341 if (system_timer->dyn_tick->state & DYN_TICK_ENABLED) 342 system_timer->dyn_tick->handler(irq, 0, regs); 343 write_sequnlock(&xtime_lock); 344 } 345 #endif 346 347 if (!(action->flags & SA_INTERRUPT)) 348 local_irq_enable(); 349 350 status = 0; 351 do { 352 ret = action->handler(irq, action->dev_id, regs); 353 if (ret == IRQ_HANDLED) 354 status |= action->flags; 355 retval |= ret; 356 action = action->next; 357 } while (action); 358 359 if (status & SA_SAMPLE_RANDOM) 360 add_interrupt_randomness(irq); 361 362 spin_lock_irq(&irq_controller_lock); 363 364 return retval; 365 } 366 367 /* 368 * This is for software-decoded IRQs. The caller is expected to 369 * handle the ack, clear, mask and unmask issues. 370 */ 371 void 372 do_simple_IRQ(unsigned int irq, struct irqdesc *desc, struct pt_regs *regs) 373 { 374 struct irqaction *action; 375 const unsigned int cpu = smp_processor_id(); 376 377 desc->triggered = 1; 378 379 kstat_cpu(cpu).irqs[irq]++; 380 381 smp_set_running(desc); 382 383 action = desc->action; 384 if (action) { 385 int ret = __do_irq(irq, action, regs); 386 if (ret != IRQ_HANDLED) 387 report_bad_irq(irq, regs, desc, ret); 388 } 389 390 smp_clear_running(desc); 391 } 392 393 /* 394 * Most edge-triggered IRQ implementations seem to take a broken 395 * approach to this. Hence the complexity. 396 */ 397 void 398 do_edge_IRQ(unsigned int irq, struct irqdesc *desc, struct pt_regs *regs) 399 { 400 const unsigned int cpu = smp_processor_id(); 401 402 desc->triggered = 1; 403 404 /* 405 * If we're currently running this IRQ, or its disabled, 406 * we shouldn't process the IRQ. Instead, turn on the 407 * hardware masks. 408 */ 409 if (unlikely(desc->running || desc->disable_depth)) 410 goto running; 411 412 /* 413 * Acknowledge and clear the IRQ, but don't mask it. 414 */ 415 desc->chip->ack(irq); 416 417 /* 418 * Mark the IRQ currently in progress. 419 */ 420 desc->running = 1; 421 422 kstat_cpu(cpu).irqs[irq]++; 423 424 do { 425 struct irqaction *action; 426 427 action = desc->action; 428 if (!action) 429 break; 430 431 if (desc->pending && !desc->disable_depth) { 432 desc->pending = 0; 433 desc->chip->unmask(irq); 434 } 435 436 __do_irq(irq, action, regs); 437 } while (desc->pending && !desc->disable_depth); 438 439 desc->running = 0; 440 441 /* 442 * If we were disabled or freed, shut down the handler. 443 */ 444 if (likely(desc->action && !check_irq_lock(desc, irq, regs))) 445 return; 446 447 running: 448 /* 449 * We got another IRQ while this one was masked or 450 * currently running. Delay it. 451 */ 452 desc->pending = 1; 453 desc->chip->mask(irq); 454 desc->chip->ack(irq); 455 } 456 457 /* 458 * Level-based IRQ handler. Nice and simple. 459 */ 460 void 461 do_level_IRQ(unsigned int irq, struct irqdesc *desc, struct pt_regs *regs) 462 { 463 struct irqaction *action; 464 const unsigned int cpu = smp_processor_id(); 465 466 desc->triggered = 1; 467 468 /* 469 * Acknowledge, clear _AND_ disable the interrupt. 470 */ 471 desc->chip->ack(irq); 472 473 if (likely(!desc->disable_depth)) { 474 kstat_cpu(cpu).irqs[irq]++; 475 476 smp_set_running(desc); 477 478 /* 479 * Return with this interrupt masked if no action 480 */ 481 action = desc->action; 482 if (action) { 483 int ret = __do_irq(irq, desc->action, regs); 484 485 if (ret != IRQ_HANDLED) 486 report_bad_irq(irq, regs, desc, ret); 487 488 if (likely(!desc->disable_depth && 489 !check_irq_lock(desc, irq, regs))) 490 desc->chip->unmask(irq); 491 } 492 493 smp_clear_running(desc); 494 } 495 } 496 497 static void do_pending_irqs(struct pt_regs *regs) 498 { 499 struct list_head head, *l, *n; 500 501 do { 502 struct irqdesc *desc; 503 504 /* 505 * First, take the pending interrupts off the list. 506 * The act of calling the handlers may add some IRQs 507 * back onto the list. 508 */ 509 head = irq_pending; 510 INIT_LIST_HEAD(&irq_pending); 511 head.next->prev = &head; 512 head.prev->next = &head; 513 514 /* 515 * Now run each entry. We must delete it from our 516 * list before calling the handler. 517 */ 518 list_for_each_safe(l, n, &head) { 519 desc = list_entry(l, struct irqdesc, pend); 520 list_del_init(&desc->pend); 521 desc_handle_irq(desc - irq_desc, desc, regs); 522 } 523 524 /* 525 * The list must be empty. 526 */ 527 BUG_ON(!list_empty(&head)); 528 } while (!list_empty(&irq_pending)); 529 } 530 531 /* 532 * do_IRQ handles all hardware IRQ's. Decoded IRQs should not 533 * come via this function. Instead, they should provide their 534 * own 'handler' 535 */ 536 asmlinkage void asm_do_IRQ(unsigned int irq, struct pt_regs *regs) 537 { 538 struct irqdesc *desc = irq_desc + irq; 539 540 /* 541 * Some hardware gives randomly wrong interrupts. Rather 542 * than crashing, do something sensible. 543 */ 544 if (irq >= NR_IRQS) 545 desc = &bad_irq_desc; 546 547 irq_enter(); 548 spin_lock(&irq_controller_lock); 549 desc_handle_irq(irq, desc, regs); 550 551 /* 552 * Now re-run any pending interrupts. 553 */ 554 if (!list_empty(&irq_pending)) 555 do_pending_irqs(regs); 556 557 irq_finish(irq); 558 559 spin_unlock(&irq_controller_lock); 560 irq_exit(); 561 } 562 563 void __set_irq_handler(unsigned int irq, irq_handler_t handle, int is_chained) 564 { 565 struct irqdesc *desc; 566 unsigned long flags; 567 568 if (irq >= NR_IRQS) { 569 printk(KERN_ERR "Trying to install handler for IRQ%d\n", irq); 570 return; 571 } 572 573 if (handle == NULL) 574 handle = do_bad_IRQ; 575 576 desc = irq_desc + irq; 577 578 if (is_chained && desc->chip == &bad_chip) 579 printk(KERN_WARNING "Trying to install chained handler for IRQ%d\n", irq); 580 581 spin_lock_irqsave(&irq_controller_lock, flags); 582 if (handle == do_bad_IRQ) { 583 desc->chip->mask(irq); 584 desc->chip->ack(irq); 585 desc->disable_depth = 1; 586 } 587 desc->handle = handle; 588 if (handle != do_bad_IRQ && is_chained) { 589 desc->valid = 0; 590 desc->probe_ok = 0; 591 desc->disable_depth = 0; 592 desc->chip->unmask(irq); 593 } 594 spin_unlock_irqrestore(&irq_controller_lock, flags); 595 } 596 597 void set_irq_chip(unsigned int irq, struct irqchip *chip) 598 { 599 struct irqdesc *desc; 600 unsigned long flags; 601 602 if (irq >= NR_IRQS) { 603 printk(KERN_ERR "Trying to install chip for IRQ%d\n", irq); 604 return; 605 } 606 607 if (chip == NULL) 608 chip = &bad_chip; 609 610 desc = irq_desc + irq; 611 spin_lock_irqsave(&irq_controller_lock, flags); 612 desc->chip = chip; 613 spin_unlock_irqrestore(&irq_controller_lock, flags); 614 } 615 616 int set_irq_type(unsigned int irq, unsigned int type) 617 { 618 struct irqdesc *desc; 619 unsigned long flags; 620 int ret = -ENXIO; 621 622 if (irq >= NR_IRQS) { 623 printk(KERN_ERR "Trying to set irq type for IRQ%d\n", irq); 624 return -ENODEV; 625 } 626 627 desc = irq_desc + irq; 628 if (desc->chip->set_type) { 629 spin_lock_irqsave(&irq_controller_lock, flags); 630 ret = desc->chip->set_type(irq, type); 631 spin_unlock_irqrestore(&irq_controller_lock, flags); 632 } 633 634 return ret; 635 } 636 EXPORT_SYMBOL(set_irq_type); 637 638 void set_irq_flags(unsigned int irq, unsigned int iflags) 639 { 640 struct irqdesc *desc; 641 unsigned long flags; 642 643 if (irq >= NR_IRQS) { 644 printk(KERN_ERR "Trying to set irq flags for IRQ%d\n", irq); 645 return; 646 } 647 648 desc = irq_desc + irq; 649 spin_lock_irqsave(&irq_controller_lock, flags); 650 desc->valid = (iflags & IRQF_VALID) != 0; 651 desc->probe_ok = (iflags & IRQF_PROBE) != 0; 652 desc->noautoenable = (iflags & IRQF_NOAUTOEN) != 0; 653 spin_unlock_irqrestore(&irq_controller_lock, flags); 654 } 655 656 int setup_irq(unsigned int irq, struct irqaction *new) 657 { 658 int shared = 0; 659 struct irqaction *old, **p; 660 unsigned long flags; 661 struct irqdesc *desc; 662 663 /* 664 * Some drivers like serial.c use request_irq() heavily, 665 * so we have to be careful not to interfere with a 666 * running system. 667 */ 668 if (new->flags & SA_SAMPLE_RANDOM) { 669 /* 670 * This function might sleep, we want to call it first, 671 * outside of the atomic block. 672 * Yes, this might clear the entropy pool if the wrong 673 * driver is attempted to be loaded, without actually 674 * installing a new handler, but is this really a problem, 675 * only the sysadmin is able to do this. 676 */ 677 rand_initialize_irq(irq); 678 } 679 680 /* 681 * The following block of code has to be executed atomically 682 */ 683 desc = irq_desc + irq; 684 spin_lock_irqsave(&irq_controller_lock, flags); 685 p = &desc->action; 686 if ((old = *p) != NULL) { 687 /* Can't share interrupts unless both agree to */ 688 if (!(old->flags & new->flags & SA_SHIRQ)) { 689 spin_unlock_irqrestore(&irq_controller_lock, flags); 690 return -EBUSY; 691 } 692 693 /* add new interrupt at end of irq queue */ 694 do { 695 p = &old->next; 696 old = *p; 697 } while (old); 698 shared = 1; 699 } 700 701 *p = new; 702 703 if (!shared) { 704 desc->probing = 0; 705 desc->running = 0; 706 desc->pending = 0; 707 desc->disable_depth = 1; 708 if (!desc->noautoenable) { 709 desc->disable_depth = 0; 710 desc->chip->unmask(irq); 711 } 712 } 713 714 spin_unlock_irqrestore(&irq_controller_lock, flags); 715 return 0; 716 } 717 718 /** 719 * request_irq - allocate an interrupt line 720 * @irq: Interrupt line to allocate 721 * @handler: Function to be called when the IRQ occurs 722 * @irqflags: Interrupt type flags 723 * @devname: An ascii name for the claiming device 724 * @dev_id: A cookie passed back to the handler function 725 * 726 * This call allocates interrupt resources and enables the 727 * interrupt line and IRQ handling. From the point this 728 * call is made your handler function may be invoked. Since 729 * your handler function must clear any interrupt the board 730 * raises, you must take care both to initialise your hardware 731 * and to set up the interrupt handler in the right order. 732 * 733 * Dev_id must be globally unique. Normally the address of the 734 * device data structure is used as the cookie. Since the handler 735 * receives this value it makes sense to use it. 736 * 737 * If your interrupt is shared you must pass a non NULL dev_id 738 * as this is required when freeing the interrupt. 739 * 740 * Flags: 741 * 742 * SA_SHIRQ Interrupt is shared 743 * 744 * SA_INTERRUPT Disable local interrupts while processing 745 * 746 * SA_SAMPLE_RANDOM The interrupt can be used for entropy 747 * 748 */ 749 int request_irq(unsigned int irq, irqreturn_t (*handler)(int, void *, struct pt_regs *), 750 unsigned long irq_flags, const char * devname, void *dev_id) 751 { 752 unsigned long retval; 753 struct irqaction *action; 754 755 if (irq >= NR_IRQS || !irq_desc[irq].valid || !handler || 756 (irq_flags & SA_SHIRQ && !dev_id)) 757 return -EINVAL; 758 759 action = (struct irqaction *)kmalloc(sizeof(struct irqaction), GFP_KERNEL); 760 if (!action) 761 return -ENOMEM; 762 763 action->handler = handler; 764 action->flags = irq_flags; 765 cpus_clear(action->mask); 766 action->name = devname; 767 action->next = NULL; 768 action->dev_id = dev_id; 769 770 retval = setup_irq(irq, action); 771 772 if (retval) 773 kfree(action); 774 return retval; 775 } 776 777 EXPORT_SYMBOL(request_irq); 778 779 /** 780 * free_irq - free an interrupt 781 * @irq: Interrupt line to free 782 * @dev_id: Device identity to free 783 * 784 * Remove an interrupt handler. The handler is removed and if the 785 * interrupt line is no longer in use by any driver it is disabled. 786 * On a shared IRQ the caller must ensure the interrupt is disabled 787 * on the card it drives before calling this function. 788 * 789 * This function must not be called from interrupt context. 790 */ 791 void free_irq(unsigned int irq, void *dev_id) 792 { 793 struct irqaction * action, **p; 794 unsigned long flags; 795 796 if (irq >= NR_IRQS || !irq_desc[irq].valid) { 797 printk(KERN_ERR "Trying to free IRQ%d\n",irq); 798 dump_stack(); 799 return; 800 } 801 802 spin_lock_irqsave(&irq_controller_lock, flags); 803 for (p = &irq_desc[irq].action; (action = *p) != NULL; p = &action->next) { 804 if (action->dev_id != dev_id) 805 continue; 806 807 /* Found it - now free it */ 808 *p = action->next; 809 break; 810 } 811 spin_unlock_irqrestore(&irq_controller_lock, flags); 812 813 if (!action) { 814 printk(KERN_ERR "Trying to free free IRQ%d\n",irq); 815 dump_stack(); 816 } else { 817 synchronize_irq(irq); 818 kfree(action); 819 } 820 } 821 822 EXPORT_SYMBOL(free_irq); 823 824 static DECLARE_MUTEX(probe_sem); 825 826 /* Start the interrupt probing. Unlike other architectures, 827 * we don't return a mask of interrupts from probe_irq_on, 828 * but return the number of interrupts enabled for the probe. 829 * The interrupts which have been enabled for probing is 830 * instead recorded in the irq_desc structure. 831 */ 832 unsigned long probe_irq_on(void) 833 { 834 unsigned int i, irqs = 0; 835 unsigned long delay; 836 837 down(&probe_sem); 838 839 /* 840 * first snaffle up any unassigned but 841 * probe-able interrupts 842 */ 843 spin_lock_irq(&irq_controller_lock); 844 for (i = 0; i < NR_IRQS; i++) { 845 if (!irq_desc[i].probe_ok || irq_desc[i].action) 846 continue; 847 848 irq_desc[i].probing = 1; 849 irq_desc[i].triggered = 0; 850 if (irq_desc[i].chip->set_type) 851 irq_desc[i].chip->set_type(i, IRQT_PROBE); 852 irq_desc[i].chip->unmask(i); 853 irqs += 1; 854 } 855 spin_unlock_irq(&irq_controller_lock); 856 857 /* 858 * wait for spurious interrupts to mask themselves out again 859 */ 860 for (delay = jiffies + HZ/10; time_before(jiffies, delay); ) 861 /* min 100ms delay */; 862 863 /* 864 * now filter out any obviously spurious interrupts 865 */ 866 spin_lock_irq(&irq_controller_lock); 867 for (i = 0; i < NR_IRQS; i++) { 868 if (irq_desc[i].probing && irq_desc[i].triggered) { 869 irq_desc[i].probing = 0; 870 irqs -= 1; 871 } 872 } 873 spin_unlock_irq(&irq_controller_lock); 874 875 return irqs; 876 } 877 878 EXPORT_SYMBOL(probe_irq_on); 879 880 unsigned int probe_irq_mask(unsigned long irqs) 881 { 882 unsigned int mask = 0, i; 883 884 spin_lock_irq(&irq_controller_lock); 885 for (i = 0; i < 16 && i < NR_IRQS; i++) 886 if (irq_desc[i].probing && irq_desc[i].triggered) 887 mask |= 1 << i; 888 spin_unlock_irq(&irq_controller_lock); 889 890 up(&probe_sem); 891 892 return mask; 893 } 894 EXPORT_SYMBOL(probe_irq_mask); 895 896 /* 897 * Possible return values: 898 * >= 0 - interrupt number 899 * -1 - no interrupt/many interrupts 900 */ 901 int probe_irq_off(unsigned long irqs) 902 { 903 unsigned int i; 904 int irq_found = NO_IRQ; 905 906 /* 907 * look at the interrupts, and find exactly one 908 * that we were probing has been triggered 909 */ 910 spin_lock_irq(&irq_controller_lock); 911 for (i = 0; i < NR_IRQS; i++) { 912 if (irq_desc[i].probing && 913 irq_desc[i].triggered) { 914 if (irq_found != NO_IRQ) { 915 irq_found = NO_IRQ; 916 goto out; 917 } 918 irq_found = i; 919 } 920 } 921 922 if (irq_found == -1) 923 irq_found = NO_IRQ; 924 out: 925 spin_unlock_irq(&irq_controller_lock); 926 927 up(&probe_sem); 928 929 return irq_found; 930 } 931 932 EXPORT_SYMBOL(probe_irq_off); 933 934 #ifdef CONFIG_SMP 935 static void route_irq(struct irqdesc *desc, unsigned int irq, unsigned int cpu) 936 { 937 pr_debug("IRQ%u: moving from cpu%u to cpu%u\n", irq, desc->cpu, cpu); 938 939 spin_lock_irq(&irq_controller_lock); 940 desc->cpu = cpu; 941 desc->chip->set_cpu(desc, irq, cpu); 942 spin_unlock_irq(&irq_controller_lock); 943 } 944 945 #ifdef CONFIG_PROC_FS 946 static int 947 irq_affinity_read_proc(char *page, char **start, off_t off, int count, 948 int *eof, void *data) 949 { 950 struct irqdesc *desc = irq_desc + ((int)data); 951 int len = cpumask_scnprintf(page, count, desc->affinity); 952 953 if (count - len < 2) 954 return -EINVAL; 955 page[len++] = '\n'; 956 page[len] = '\0'; 957 958 return len; 959 } 960 961 static int 962 irq_affinity_write_proc(struct file *file, const char __user *buffer, 963 unsigned long count, void *data) 964 { 965 unsigned int irq = (unsigned int)data; 966 struct irqdesc *desc = irq_desc + irq; 967 cpumask_t affinity, tmp; 968 int ret = -EIO; 969 970 if (!desc->chip->set_cpu) 971 goto out; 972 973 ret = cpumask_parse(buffer, count, affinity); 974 if (ret) 975 goto out; 976 977 cpus_and(tmp, affinity, cpu_online_map); 978 if (cpus_empty(tmp)) { 979 ret = -EINVAL; 980 goto out; 981 } 982 983 desc->affinity = affinity; 984 route_irq(desc, irq, first_cpu(tmp)); 985 ret = count; 986 987 out: 988 return ret; 989 } 990 #endif 991 #endif 992 993 void __init init_irq_proc(void) 994 { 995 #if defined(CONFIG_SMP) && defined(CONFIG_PROC_FS) 996 struct proc_dir_entry *dir; 997 int irq; 998 999 dir = proc_mkdir("irq", NULL); 1000 if (!dir) 1001 return; 1002 1003 for (irq = 0; irq < NR_IRQS; irq++) { 1004 struct proc_dir_entry *entry; 1005 struct irqdesc *desc; 1006 char name[16]; 1007 1008 desc = irq_desc + irq; 1009 memset(name, 0, sizeof(name)); 1010 snprintf(name, sizeof(name) - 1, "%u", irq); 1011 1012 desc->procdir = proc_mkdir(name, dir); 1013 if (!desc->procdir) 1014 continue; 1015 1016 entry = create_proc_entry("smp_affinity", 0600, desc->procdir); 1017 if (entry) { 1018 entry->nlink = 1; 1019 entry->data = (void *)irq; 1020 entry->read_proc = irq_affinity_read_proc; 1021 entry->write_proc = irq_affinity_write_proc; 1022 } 1023 } 1024 #endif 1025 } 1026 1027 void __init init_IRQ(void) 1028 { 1029 struct irqdesc *desc; 1030 extern void init_dma(void); 1031 int irq; 1032 1033 #ifdef CONFIG_SMP 1034 bad_irq_desc.affinity = CPU_MASK_ALL; 1035 bad_irq_desc.cpu = smp_processor_id(); 1036 #endif 1037 1038 for (irq = 0, desc = irq_desc; irq < NR_IRQS; irq++, desc++) { 1039 *desc = bad_irq_desc; 1040 INIT_LIST_HEAD(&desc->pend); 1041 } 1042 1043 init_arch_irq(); 1044 init_dma(); 1045 } 1046 1047 static int __init noirqdebug_setup(char *str) 1048 { 1049 noirqdebug = 1; 1050 return 1; 1051 } 1052 1053 __setup("noirqdebug", noirqdebug_setup); 1054 1055 #ifdef CONFIG_HOTPLUG_CPU 1056 /* 1057 * The CPU has been marked offline. Migrate IRQs off this CPU. If 1058 * the affinity settings do not allow other CPUs, force them onto any 1059 * available CPU. 1060 */ 1061 void migrate_irqs(void) 1062 { 1063 unsigned int i, cpu = smp_processor_id(); 1064 1065 for (i = 0; i < NR_IRQS; i++) { 1066 struct irqdesc *desc = irq_desc + i; 1067 1068 if (desc->cpu == cpu) { 1069 unsigned int newcpu = any_online_cpu(desc->affinity); 1070 1071 if (newcpu == NR_CPUS) { 1072 if (printk_ratelimit()) 1073 printk(KERN_INFO "IRQ%u no longer affine to CPU%u\n", 1074 i, cpu); 1075 1076 cpus_setall(desc->affinity); 1077 newcpu = any_online_cpu(desc->affinity); 1078 } 1079 1080 route_irq(desc, i, newcpu); 1081 } 1082 } 1083 } 1084 #endif /* CONFIG_HOTPLUG_CPU */ 1085