handle.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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 <linux/slab.h>
  18. #include <trace/events/irq.h>
  19. #include "internals.h"
  20. #ifdef CONFIG_MT_RT_THROTTLE_MON
  21. #include "mt_sched_mon.h"
  22. #endif
  23. #ifdef CONFIG_MTPROF_CPUTIME
  24. #include "mt_cputime.h"
  25. /* cputime monitor en/disable value */
  26. #ifdef CONFIG_MT_ENG_BUILD
  27. /* max debug thread count, if reach the level, stop store new thread informaiton. */
  28. #define MAX_THREAD_COUNT (6000)
  29. #else
  30. #define MAX_THREAD_COUNT (3000)
  31. #endif
  32. #endif
  33. /**
  34. * handle_bad_irq - handle spurious and unhandled irqs
  35. * @irq: the interrupt number
  36. * @desc: description of the interrupt
  37. *
  38. * Handles spurious and unhandled IRQ's. It also prints a debugmessage.
  39. */
  40. void handle_bad_irq(unsigned int irq, struct irq_desc *desc)
  41. {
  42. print_irq_desc(irq, desc);
  43. kstat_incr_irqs_this_cpu(irq, desc);
  44. ack_bad_irq(irq);
  45. }
  46. /*
  47. * Special, empty irq handler:
  48. */
  49. irqreturn_t no_action(int cpl, void *dev_id)
  50. {
  51. return IRQ_NONE;
  52. }
  53. EXPORT_SYMBOL_GPL(no_action);
  54. static void warn_no_thread(unsigned int irq, struct irqaction *action)
  55. {
  56. if (test_and_set_bit(IRQTF_WARNED, &action->thread_flags))
  57. return;
  58. printk(KERN_WARNING "IRQ %d device %s returned IRQ_WAKE_THREAD "
  59. "but no thread function available.", irq, action->name);
  60. }
  61. void __irq_wake_thread(struct irq_desc *desc, struct irqaction *action)
  62. {
  63. /*
  64. * In case the thread crashed and was killed we just pretend that
  65. * we handled the interrupt. The hardirq handler has disabled the
  66. * device interrupt, so no irq storm is lurking.
  67. */
  68. if (action->thread->flags & PF_EXITING)
  69. return;
  70. /*
  71. * Wake up the handler thread for this action. If the
  72. * RUNTHREAD bit is already set, nothing to do.
  73. */
  74. if (test_and_set_bit(IRQTF_RUNTHREAD, &action->thread_flags))
  75. return;
  76. /*
  77. * It's safe to OR the mask lockless here. We have only two
  78. * places which write to threads_oneshot: This code and the
  79. * irq thread.
  80. *
  81. * This code is the hard irq context and can never run on two
  82. * cpus in parallel. If it ever does we have more serious
  83. * problems than this bitmask.
  84. *
  85. * The irq threads of this irq which clear their "running" bit
  86. * in threads_oneshot are serialized via desc->lock against
  87. * each other and they are serialized against this code by
  88. * IRQS_INPROGRESS.
  89. *
  90. * Hard irq handler:
  91. *
  92. * spin_lock(desc->lock);
  93. * desc->state |= IRQS_INPROGRESS;
  94. * spin_unlock(desc->lock);
  95. * set_bit(IRQTF_RUNTHREAD, &action->thread_flags);
  96. * desc->threads_oneshot |= mask;
  97. * spin_lock(desc->lock);
  98. * desc->state &= ~IRQS_INPROGRESS;
  99. * spin_unlock(desc->lock);
  100. *
  101. * irq thread:
  102. *
  103. * again:
  104. * spin_lock(desc->lock);
  105. * if (desc->state & IRQS_INPROGRESS) {
  106. * spin_unlock(desc->lock);
  107. * while(desc->state & IRQS_INPROGRESS)
  108. * cpu_relax();
  109. * goto again;
  110. * }
  111. * if (!test_bit(IRQTF_RUNTHREAD, &action->thread_flags))
  112. * desc->threads_oneshot &= ~mask;
  113. * spin_unlock(desc->lock);
  114. *
  115. * So either the thread waits for us to clear IRQS_INPROGRESS
  116. * or we are waiting in the flow handler for desc->lock to be
  117. * released before we reach this point. The thread also checks
  118. * IRQTF_RUNTHREAD under desc->lock. If set it leaves
  119. * threads_oneshot untouched and runs the thread another time.
  120. */
  121. desc->threads_oneshot |= action->thread_mask;
  122. /*
  123. * We increment the threads_active counter in case we wake up
  124. * the irq thread. The irq thread decrements the counter when
  125. * it returns from the handler or in the exit path and wakes
  126. * up waiters which are stuck in synchronize_irq() when the
  127. * active count becomes zero. synchronize_irq() is serialized
  128. * against this code (hard irq handler) via IRQS_INPROGRESS
  129. * like the finalize_oneshot() code. See comment above.
  130. */
  131. atomic_inc(&desc->threads_active);
  132. wake_up_process(action->thread);
  133. }
  134. #if defined(CONFIG_MTPROF_CPUTIME) || defined(CONFIG_MT_RT_THROTTLE_MON)
  135. static void save_isr_info(unsigned int irq, struct irqaction *action,
  136. unsigned long long start, unsigned long long end)
  137. {
  138. unsigned long long dur = end - start;
  139. #ifdef CONFIG_MTPROF_CPUTIME
  140. int isr_find = 0;
  141. struct mtk_isr_info *mtk_isr_point = current->se.mtk_isr;
  142. struct mtk_isr_info *mtk_isr_current = mtk_isr_point;
  143. char *isr_name = NULL;
  144. if (unlikely(mtsched_is_enabled())) {
  145. current->se.mtk_isr_time += dur;
  146. while ((current->se.mtk_isr != NULL) && (mtk_isr_point != NULL)) {
  147. if (mtk_isr_point->isr_num == irq) {
  148. mtk_isr_point->isr_time += dur;
  149. mtk_isr_point->isr_count++;
  150. isr_find = 1;
  151. break;
  152. }
  153. mtk_isr_current = mtk_isr_point;
  154. mtk_isr_point = mtk_isr_point->next;
  155. }
  156. if ((isr_find == 0) && (mtproc_counts() < MAX_THREAD_COUNT)) {
  157. mtk_isr_point = kmalloc(sizeof(struct mtk_isr_info), GFP_ATOMIC);
  158. if (mtk_isr_point == NULL) {
  159. pr_debug("cant' alloc mtk_isr_info mem!\n");
  160. } else {
  161. mtk_isr_point->isr_num = irq;
  162. mtk_isr_point->isr_time = dur;
  163. mtk_isr_point->isr_count = 1;
  164. mtk_isr_point->next = NULL;
  165. if (mtk_isr_current == NULL)
  166. current->se.mtk_isr = mtk_isr_point;
  167. else
  168. mtk_isr_current->next = mtk_isr_point;
  169. isr_name = kmalloc(sizeof(action->name), GFP_ATOMIC);
  170. if (isr_name != NULL) {
  171. strcpy(isr_name, action->name);
  172. mtk_isr_point->isr_name = isr_name;
  173. } else {
  174. pr_debug("cant' alloc isr_name mem!\n");
  175. }
  176. current->se.mtk_isr_count++;
  177. }
  178. }
  179. return;
  180. }
  181. #endif
  182. /* only record dur in mtk_isr_time if:
  183. * CONFIG_MTPROF_CPUTIME defined but not enabled, or
  184. * CONFIG_MTPROF_CPUTIME not defined
  185. */
  186. if ((current->policy == SCHED_FIFO || current->policy == SCHED_RR)
  187. && mt_rt_mon_enable())
  188. current->se.mtk_isr_time += dur;
  189. }
  190. #endif
  191. irqreturn_t
  192. handle_irq_event_percpu(struct irq_desc *desc, struct irqaction *action)
  193. {
  194. irqreturn_t retval = IRQ_NONE;
  195. unsigned int flags = 0, irq = desc->irq_data.irq;
  196. #if defined(CONFIG_MTPROF_CPUTIME) || defined(CONFIG_MT_RT_THROTTLE_MON)
  197. unsigned long long t1, t2;
  198. #endif
  199. do {
  200. irqreturn_t res;
  201. trace_irq_handler_entry(irq, action);
  202. #if defined(CONFIG_MTPROF_CPUTIME) || defined(CONFIG_MT_RT_THROTTLE_MON)
  203. t1 = sched_clock();
  204. #endif
  205. res = action->handler(irq, action->dev_id);
  206. #if defined(CONFIG_MTPROF_CPUTIME) || defined(CONFIG_MT_RT_THROTTLE_MON)
  207. t2 = sched_clock();
  208. save_isr_info(irq, action, t1, t2);
  209. #endif
  210. trace_irq_handler_exit(irq, action, res);
  211. if (WARN_ONCE(!irqs_disabled(), "irq %u handler %pF enabled interrupts\n",
  212. irq, action->handler))
  213. local_irq_disable();
  214. switch (res) {
  215. case IRQ_WAKE_THREAD:
  216. /*
  217. * Catch drivers which return WAKE_THREAD but
  218. * did not set up a thread function
  219. */
  220. if (unlikely(!action->thread_fn)) {
  221. warn_no_thread(irq, action);
  222. break;
  223. }
  224. __irq_wake_thread(desc, action);
  225. /* Fall through to add to randomness */
  226. case IRQ_HANDLED:
  227. flags |= action->flags;
  228. break;
  229. default:
  230. break;
  231. }
  232. retval |= res;
  233. action = action->next;
  234. } while (action);
  235. add_interrupt_randomness(irq, flags);
  236. if (!noirqdebug)
  237. note_interrupt(irq, desc, retval);
  238. return retval;
  239. }
  240. irqreturn_t handle_irq_event(struct irq_desc *desc)
  241. {
  242. struct irqaction *action = desc->action;
  243. irqreturn_t ret;
  244. desc->istate &= ~IRQS_PENDING;
  245. irqd_set(&desc->irq_data, IRQD_IRQ_INPROGRESS);
  246. raw_spin_unlock(&desc->lock);
  247. ret = handle_irq_event_percpu(desc, action);
  248. raw_spin_lock(&desc->lock);
  249. irqd_clear(&desc->irq_data, IRQD_IRQ_INPROGRESS);
  250. return ret;
  251. }