handle.c 5.6 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/random.h>
  14. #include <linux/sched.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/kernel_stat.h>
  17. #include <trace/events/irq.h>
  18. #include "internals.h"
  19. /**
  20. * handle_bad_irq - handle spurious and unhandled irqs
  21. * @irq: the interrupt number
  22. * @desc: description of the interrupt
  23. *
  24. * Handles spurious and unhandled IRQ's. It also prints a debugmessage.
  25. */
  26. void handle_bad_irq(unsigned int irq, struct irq_desc *desc)
  27. {
  28. print_irq_desc(irq, desc);
  29. kstat_incr_irqs_this_cpu(irq, desc);
  30. ack_bad_irq(irq);
  31. }
  32. /*
  33. * Special, empty irq handler:
  34. */
  35. irqreturn_t no_action(int cpl, void *dev_id)
  36. {
  37. return IRQ_NONE;
  38. }
  39. static void warn_no_thread(unsigned int irq, struct irqaction *action)
  40. {
  41. if (test_and_set_bit(IRQTF_WARNED, &action->thread_flags))
  42. return;
  43. printk(KERN_WARNING "IRQ %d device %s returned IRQ_WAKE_THREAD "
  44. "but no thread function available.", irq, action->name);
  45. }
  46. /**
  47. * handle_IRQ_event - irq action chain handler
  48. * @irq: the interrupt number
  49. * @action: the interrupt action chain for this irq
  50. *
  51. * Handles the action chain of an irq event
  52. */
  53. irqreturn_t handle_IRQ_event(unsigned int irq, struct irqaction *action)
  54. {
  55. irqreturn_t ret, retval = IRQ_NONE;
  56. unsigned int status = 0;
  57. do {
  58. trace_irq_handler_entry(irq, action);
  59. ret = action->handler(irq, action->dev_id);
  60. trace_irq_handler_exit(irq, action, ret);
  61. switch (ret) {
  62. case IRQ_WAKE_THREAD:
  63. /*
  64. * Set result to handled so the spurious check
  65. * does not trigger.
  66. */
  67. ret = IRQ_HANDLED;
  68. /*
  69. * Catch drivers which return WAKE_THREAD but
  70. * did not set up a thread function
  71. */
  72. if (unlikely(!action->thread_fn)) {
  73. warn_no_thread(irq, action);
  74. break;
  75. }
  76. /*
  77. * Wake up the handler thread for this
  78. * action. In case the thread crashed and was
  79. * killed we just pretend that we handled the
  80. * interrupt. The hardirq handler above has
  81. * disabled the device interrupt, so no irq
  82. * storm is lurking.
  83. */
  84. if (likely(!test_bit(IRQTF_DIED,
  85. &action->thread_flags))) {
  86. set_bit(IRQTF_RUNTHREAD, &action->thread_flags);
  87. wake_up_process(action->thread);
  88. }
  89. /* Fall through to add to randomness */
  90. case IRQ_HANDLED:
  91. status |= action->flags;
  92. break;
  93. default:
  94. break;
  95. }
  96. retval |= ret;
  97. action = action->next;
  98. } while (action);
  99. if (status & IRQF_SAMPLE_RANDOM)
  100. add_interrupt_randomness(irq);
  101. local_irq_disable();
  102. return retval;
  103. }
  104. #ifndef CONFIG_GENERIC_HARDIRQS_NO__DO_IRQ
  105. #ifdef CONFIG_ENABLE_WARN_DEPRECATED
  106. # warning __do_IRQ is deprecated. Please convert to proper flow handlers
  107. #endif
  108. /**
  109. * __do_IRQ - original all in one highlevel IRQ handler
  110. * @irq: the interrupt number
  111. *
  112. * __do_IRQ handles all normal device IRQ's (the special
  113. * SMP cross-CPU interrupts have their own specific
  114. * handlers).
  115. *
  116. * This is the original x86 implementation which is used for every
  117. * interrupt type.
  118. */
  119. unsigned int __do_IRQ(unsigned int irq)
  120. {
  121. struct irq_desc *desc = irq_to_desc(irq);
  122. struct irqaction *action;
  123. unsigned int status;
  124. kstat_incr_irqs_this_cpu(irq, desc);
  125. if (CHECK_IRQ_PER_CPU(desc->status)) {
  126. irqreturn_t action_ret;
  127. /*
  128. * No locking required for CPU-local interrupts:
  129. */
  130. if (desc->irq_data.chip->ack)
  131. desc->irq_data.chip->ack(irq);
  132. if (likely(!(desc->status & IRQ_DISABLED))) {
  133. action_ret = handle_IRQ_event(irq, desc->action);
  134. if (!noirqdebug)
  135. note_interrupt(irq, desc, action_ret);
  136. }
  137. desc->irq_data.chip->end(irq);
  138. return 1;
  139. }
  140. raw_spin_lock(&desc->lock);
  141. if (desc->irq_data.chip->ack)
  142. desc->irq_data.chip->ack(irq);
  143. /*
  144. * REPLAY is when Linux resends an IRQ that was dropped earlier
  145. * WAITING is used by probe to mark irqs that are being tested
  146. */
  147. status = desc->status & ~(IRQ_REPLAY | IRQ_WAITING);
  148. status |= IRQ_PENDING; /* we _want_ to handle it */
  149. /*
  150. * If the IRQ is disabled for whatever reason, we cannot
  151. * use the action we have.
  152. */
  153. action = NULL;
  154. if (likely(!(status & (IRQ_DISABLED | IRQ_INPROGRESS)))) {
  155. action = desc->action;
  156. status &= ~IRQ_PENDING; /* we commit to handling */
  157. status |= IRQ_INPROGRESS; /* we are handling it */
  158. }
  159. desc->status = status;
  160. /*
  161. * If there is no IRQ handler or it was disabled, exit early.
  162. * Since we set PENDING, if another processor is handling
  163. * a different instance of this same irq, the other processor
  164. * will take care of it.
  165. */
  166. if (unlikely(!action))
  167. goto out;
  168. /*
  169. * Edge triggered interrupts need to remember
  170. * pending events.
  171. * This applies to any hw interrupts that allow a second
  172. * instance of the same irq to arrive while we are in do_IRQ
  173. * or in the handler. But the code here only handles the _second_
  174. * instance of the irq, not the third or fourth. So it is mostly
  175. * useful for irq hardware that does not mask cleanly in an
  176. * SMP environment.
  177. */
  178. for (;;) {
  179. irqreturn_t action_ret;
  180. raw_spin_unlock(&desc->lock);
  181. action_ret = handle_IRQ_event(irq, action);
  182. if (!noirqdebug)
  183. note_interrupt(irq, desc, action_ret);
  184. raw_spin_lock(&desc->lock);
  185. if (likely(!(desc->status & IRQ_PENDING)))
  186. break;
  187. desc->status &= ~IRQ_PENDING;
  188. }
  189. desc->status &= ~IRQ_INPROGRESS;
  190. out:
  191. /*
  192. * The ->end() handler has to deal with interrupts which got
  193. * disabled while the handler was running.
  194. */
  195. desc->irq_data.chip->end(irq);
  196. raw_spin_unlock(&desc->lock);
  197. return 1;
  198. }
  199. #endif