handle.c 5.4 KB

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