handle.c 5.1 KB

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