handle.c 5.1 KB

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