handle.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * linux/kernel/irq/handle.c
  3. *
  4. * Copyright (C) 1992, 1998-2004 Linus Torvalds, Ingo Molnar
  5. *
  6. * This file contains the core interrupt handling code.
  7. */
  8. #include <linux/irq.h>
  9. #include <linux/module.h>
  10. #include <linux/random.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/kernel_stat.h>
  13. #include "internals.h"
  14. /*
  15. * Linux has a controller-independent interrupt architecture.
  16. * Every controller has a 'controller-template', that is used
  17. * by the main code to do the right thing. Each driver-visible
  18. * interrupt source is transparently wired to the appropriate
  19. * controller. Thus drivers need not be aware of the
  20. * interrupt-controller.
  21. *
  22. * The code is designed to be easily extended with new/different
  23. * interrupt controllers, without having to do assembly magic or
  24. * having to touch the generic code.
  25. *
  26. * Controller mappings for all interrupt sources:
  27. */
  28. struct irq_desc irq_desc[NR_IRQS] __cacheline_aligned = {
  29. [0 ... NR_IRQS-1] = {
  30. .status = IRQ_DISABLED,
  31. .chip = &no_irq_type,
  32. .lock = SPIN_LOCK_UNLOCKED,
  33. #ifdef CONFIG_SMP
  34. .affinity = CPU_MASK_ALL
  35. #endif
  36. }
  37. };
  38. /*
  39. * Generic 'no controller' code
  40. */
  41. static void end_none(unsigned int irq) { }
  42. static void enable_none(unsigned int irq) { }
  43. static void disable_none(unsigned int irq) { }
  44. static void shutdown_none(unsigned int irq) { }
  45. static unsigned int startup_none(unsigned int irq) { return 0; }
  46. static void ack_none(unsigned int irq)
  47. {
  48. /*
  49. * 'what should we do if we get a hw irq event on an illegal vector'.
  50. * each architecture has to answer this themself.
  51. */
  52. ack_bad_irq(irq);
  53. }
  54. struct hw_interrupt_type no_irq_type = {
  55. .typename = "none",
  56. .startup = startup_none,
  57. .shutdown = shutdown_none,
  58. .enable = enable_none,
  59. .disable = disable_none,
  60. .ack = ack_none,
  61. .end = end_none,
  62. .set_affinity = NULL
  63. };
  64. /*
  65. * Special, empty irq handler:
  66. */
  67. irqreturn_t no_action(int cpl, void *dev_id, struct pt_regs *regs)
  68. {
  69. return IRQ_NONE;
  70. }
  71. /*
  72. * Have got an event to handle:
  73. */
  74. irqreturn_t handle_IRQ_event(unsigned int irq, struct pt_regs *regs,
  75. struct irqaction *action)
  76. {
  77. irqreturn_t ret, retval = IRQ_NONE;
  78. unsigned int status = 0;
  79. if (!(action->flags & SA_INTERRUPT))
  80. local_irq_enable();
  81. do {
  82. ret = action->handler(irq, action->dev_id, regs);
  83. if (ret == IRQ_HANDLED)
  84. status |= action->flags;
  85. retval |= ret;
  86. action = action->next;
  87. } while (action);
  88. if (status & SA_SAMPLE_RANDOM)
  89. add_interrupt_randomness(irq);
  90. local_irq_disable();
  91. return retval;
  92. }
  93. /*
  94. * do_IRQ handles all normal device IRQ's (the special
  95. * SMP cross-CPU interrupts have their own specific
  96. * handlers).
  97. */
  98. fastcall unsigned int __do_IRQ(unsigned int irq, struct pt_regs *regs)
  99. {
  100. struct irq_desc *desc = irq_desc + irq;
  101. struct irqaction *action;
  102. unsigned int status;
  103. kstat_this_cpu.irqs[irq]++;
  104. if (CHECK_IRQ_PER_CPU(desc->status)) {
  105. irqreturn_t action_ret;
  106. /*
  107. * No locking required for CPU-local interrupts:
  108. */
  109. if (desc->chip->ack)
  110. desc->chip->ack(irq);
  111. action_ret = handle_IRQ_event(irq, regs, desc->action);
  112. desc->chip->end(irq);
  113. return 1;
  114. }
  115. spin_lock(&desc->lock);
  116. if (desc->chip->ack)
  117. desc->chip->ack(irq);
  118. /*
  119. * REPLAY is when Linux resends an IRQ that was dropped earlier
  120. * WAITING is used by probe to mark irqs that are being tested
  121. */
  122. status = desc->status & ~(IRQ_REPLAY | IRQ_WAITING);
  123. status |= IRQ_PENDING; /* we _want_ to handle it */
  124. /*
  125. * If the IRQ is disabled for whatever reason, we cannot
  126. * use the action we have.
  127. */
  128. action = NULL;
  129. if (likely(!(status & (IRQ_DISABLED | IRQ_INPROGRESS)))) {
  130. action = desc->action;
  131. status &= ~IRQ_PENDING; /* we commit to handling */
  132. status |= IRQ_INPROGRESS; /* we are handling it */
  133. }
  134. desc->status = status;
  135. /*
  136. * If there is no IRQ handler or it was disabled, exit early.
  137. * Since we set PENDING, if another processor is handling
  138. * a different instance of this same irq, the other processor
  139. * will take care of it.
  140. */
  141. if (unlikely(!action))
  142. goto out;
  143. /*
  144. * Edge triggered interrupts need to remember
  145. * pending events.
  146. * This applies to any hw interrupts that allow a second
  147. * instance of the same irq to arrive while we are in do_IRQ
  148. * or in the handler. But the code here only handles the _second_
  149. * instance of the irq, not the third or fourth. So it is mostly
  150. * useful for irq hardware that does not mask cleanly in an
  151. * SMP environment.
  152. */
  153. for (;;) {
  154. irqreturn_t action_ret;
  155. spin_unlock(&desc->lock);
  156. action_ret = handle_IRQ_event(irq, regs, action);
  157. spin_lock(&desc->lock);
  158. if (!noirqdebug)
  159. note_interrupt(irq, desc, action_ret, regs);
  160. if (likely(!(desc->status & IRQ_PENDING)))
  161. break;
  162. desc->status &= ~IRQ_PENDING;
  163. }
  164. desc->status &= ~IRQ_INPROGRESS;
  165. out:
  166. /*
  167. * The ->end() handler has to deal with interrupts which got
  168. * disabled while the handler was running.
  169. */
  170. desc->chip->end(irq);
  171. spin_unlock(&desc->lock);
  172. return 1;
  173. }