handle.c 5.4 KB

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