handle.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. static void irq_wake_thread(struct irq_desc *desc, struct irqaction *action)
  47. {
  48. /*
  49. * Wake up the handler thread for this action. In case the
  50. * thread crashed and was killed we just pretend that we
  51. * handled the interrupt. The hardirq handler has disabled the
  52. * device interrupt, so no irq storm is lurking. If the
  53. * RUNTHREAD bit is already set, nothing to do.
  54. */
  55. if (test_bit(IRQTF_DIED, &action->thread_flags) ||
  56. test_and_set_bit(IRQTF_RUNTHREAD, &action->thread_flags))
  57. return;
  58. /*
  59. * It's safe to OR the mask lockless here. We have only two
  60. * places which write to threads_oneshot: This code and the
  61. * irq thread.
  62. *
  63. * This code is the hard irq context and can never run on two
  64. * cpus in parallel. If it ever does we have more serious
  65. * problems than this bitmask.
  66. *
  67. * The irq threads of this irq which clear their "running" bit
  68. * in threads_oneshot are serialized via desc->lock against
  69. * each other and they are serialized against this code by
  70. * IRQS_INPROGRESS.
  71. *
  72. * Hard irq handler:
  73. *
  74. * spin_lock(desc->lock);
  75. * desc->state |= IRQS_INPROGRESS;
  76. * spin_unlock(desc->lock);
  77. * set_bit(IRQTF_RUNTHREAD, &action->thread_flags);
  78. * desc->threads_oneshot |= mask;
  79. * spin_lock(desc->lock);
  80. * desc->state &= ~IRQS_INPROGRESS;
  81. * spin_unlock(desc->lock);
  82. *
  83. * irq thread:
  84. *
  85. * again:
  86. * spin_lock(desc->lock);
  87. * if (desc->state & IRQS_INPROGRESS) {
  88. * spin_unlock(desc->lock);
  89. * while(desc->state & IRQS_INPROGRESS)
  90. * cpu_relax();
  91. * goto again;
  92. * }
  93. * if (!test_bit(IRQTF_RUNTHREAD, &action->thread_flags))
  94. * desc->threads_oneshot &= ~mask;
  95. * spin_unlock(desc->lock);
  96. *
  97. * So either the thread waits for us to clear IRQS_INPROGRESS
  98. * or we are waiting in the flow handler for desc->lock to be
  99. * released before we reach this point. The thread also checks
  100. * IRQTF_RUNTHREAD under desc->lock. If set it leaves
  101. * threads_oneshot untouched and runs the thread another time.
  102. */
  103. desc->threads_oneshot |= action->thread_mask;
  104. wake_up_process(action->thread);
  105. }
  106. irqreturn_t
  107. handle_irq_event_percpu(struct irq_desc *desc, struct irqaction *action)
  108. {
  109. irqreturn_t retval = IRQ_NONE;
  110. unsigned int random = 0, irq = desc->irq_data.irq;
  111. do {
  112. irqreturn_t res;
  113. trace_irq_handler_entry(irq, action);
  114. res = action->handler(irq, action->dev_id);
  115. trace_irq_handler_exit(irq, action, res);
  116. if (WARN_ONCE(!irqs_disabled(),"irq %u handler %pF enabled interrupts\n",
  117. irq, action->handler))
  118. local_irq_disable();
  119. switch (res) {
  120. case IRQ_WAKE_THREAD:
  121. /*
  122. * Set result to handled so the spurious check
  123. * does not trigger.
  124. */
  125. res = IRQ_HANDLED;
  126. /*
  127. * Catch drivers which return WAKE_THREAD but
  128. * did not set up a thread function
  129. */
  130. if (unlikely(!action->thread_fn)) {
  131. warn_no_thread(irq, action);
  132. break;
  133. }
  134. irq_wake_thread(desc, action);
  135. /* Fall through to add to randomness */
  136. case IRQ_HANDLED:
  137. random |= action->flags;
  138. break;
  139. default:
  140. break;
  141. }
  142. retval |= res;
  143. action = action->next;
  144. } while (action);
  145. if (random & IRQF_SAMPLE_RANDOM)
  146. add_interrupt_randomness(irq);
  147. if (!noirqdebug)
  148. note_interrupt(irq, desc, retval);
  149. return retval;
  150. }
  151. irqreturn_t handle_irq_event(struct irq_desc *desc)
  152. {
  153. struct irqaction *action = desc->action;
  154. irqreturn_t ret;
  155. irq_compat_clr_pending(desc);
  156. desc->istate &= ~IRQS_PENDING;
  157. irq_compat_set_progress(desc);
  158. desc->istate |= IRQS_INPROGRESS;
  159. raw_spin_unlock(&desc->lock);
  160. ret = handle_irq_event_percpu(desc, action);
  161. raw_spin_lock(&desc->lock);
  162. desc->istate &= ~IRQS_INPROGRESS;
  163. irq_compat_clr_progress(desc);
  164. return ret;
  165. }
  166. /**
  167. * handle_IRQ_event - irq action chain handler
  168. * @irq: the interrupt number
  169. * @action: the interrupt action chain for this irq
  170. *
  171. * Handles the action chain of an irq event
  172. */
  173. irqreturn_t handle_IRQ_event(unsigned int irq, struct irqaction *action)
  174. {
  175. return handle_irq_event_percpu(irq_to_desc(irq), action);
  176. }