handle.c 5.4 KB

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