handle.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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/module.h>
  14. #include <linux/random.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/kernel_stat.h>
  17. #include "internals.h"
  18. /**
  19. * handle_bad_irq - handle spurious and unhandled irqs
  20. * @irq: the interrupt number
  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(unsigned int irq, struct irq_desc *desc)
  26. {
  27. print_irq_desc(irq, desc);
  28. kstat_incr_irqs_this_cpu(irq, desc);
  29. ack_bad_irq(irq);
  30. }
  31. /*
  32. * Linux has a controller-independent interrupt architecture.
  33. * Every controller has a 'controller-template', that is used
  34. * by the main code to do the right thing. Each driver-visible
  35. * interrupt source is transparently wired to the appropriate
  36. * controller. Thus drivers need not be aware of the
  37. * interrupt-controller.
  38. *
  39. * The code is designed to be easily extended with new/different
  40. * interrupt controllers, without having to do assembly magic or
  41. * having to touch the generic code.
  42. *
  43. * Controller mappings for all interrupt sources:
  44. */
  45. int nr_irqs = NR_IRQS;
  46. EXPORT_SYMBOL_GPL(nr_irqs);
  47. struct irq_desc irq_desc[NR_IRQS] __cacheline_aligned_in_smp = {
  48. [0 ... NR_IRQS-1] = {
  49. .status = IRQ_DISABLED,
  50. .chip = &no_irq_chip,
  51. .handle_irq = handle_bad_irq,
  52. .depth = 1,
  53. .lock = __SPIN_LOCK_UNLOCKED(irq_desc->lock),
  54. #ifdef CONFIG_SMP
  55. .affinity = CPU_MASK_ALL
  56. #endif
  57. }
  58. };
  59. /*
  60. * What should we do if we get a hw irq event on an illegal vector?
  61. * Each architecture has to answer this themself.
  62. */
  63. static void ack_bad(unsigned int irq)
  64. {
  65. struct irq_desc *desc = irq_to_desc(irq);
  66. print_irq_desc(irq, desc);
  67. ack_bad_irq(irq);
  68. }
  69. /*
  70. * NOP functions
  71. */
  72. static void noop(unsigned int irq)
  73. {
  74. }
  75. static unsigned int noop_ret(unsigned int irq)
  76. {
  77. return 0;
  78. }
  79. /*
  80. * Generic no controller implementation
  81. */
  82. struct irq_chip no_irq_chip = {
  83. .name = "none",
  84. .startup = noop_ret,
  85. .shutdown = noop,
  86. .enable = noop,
  87. .disable = noop,
  88. .ack = ack_bad,
  89. .end = noop,
  90. };
  91. /*
  92. * Generic dummy implementation which can be used for
  93. * real dumb interrupt sources
  94. */
  95. struct irq_chip dummy_irq_chip = {
  96. .name = "dummy",
  97. .startup = noop_ret,
  98. .shutdown = noop,
  99. .enable = noop,
  100. .disable = noop,
  101. .ack = noop,
  102. .mask = noop,
  103. .unmask = noop,
  104. .end = noop,
  105. };
  106. /*
  107. * Special, empty irq handler:
  108. */
  109. irqreturn_t no_action(int cpl, void *dev_id)
  110. {
  111. return IRQ_NONE;
  112. }
  113. /**
  114. * handle_IRQ_event - irq action chain handler
  115. * @irq: the interrupt number
  116. * @action: the interrupt action chain for this irq
  117. *
  118. * Handles the action chain of an irq event
  119. */
  120. irqreturn_t handle_IRQ_event(unsigned int irq, struct irqaction *action)
  121. {
  122. irqreturn_t ret, retval = IRQ_NONE;
  123. unsigned int status = 0;
  124. if (!(action->flags & IRQF_DISABLED))
  125. local_irq_enable_in_hardirq();
  126. do {
  127. ret = action->handler(irq, action->dev_id);
  128. if (ret == IRQ_HANDLED)
  129. status |= action->flags;
  130. retval |= ret;
  131. action = action->next;
  132. } while (action);
  133. if (status & IRQF_SAMPLE_RANDOM)
  134. add_interrupt_randomness(irq);
  135. local_irq_disable();
  136. return retval;
  137. }
  138. #ifndef CONFIG_GENERIC_HARDIRQS_NO__DO_IRQ
  139. /**
  140. * __do_IRQ - original all in one highlevel IRQ handler
  141. * @irq: the interrupt number
  142. *
  143. * __do_IRQ handles all normal device IRQ's (the special
  144. * SMP cross-CPU interrupts have their own specific
  145. * handlers).
  146. *
  147. * This is the original x86 implementation which is used for every
  148. * interrupt type.
  149. */
  150. unsigned int __do_IRQ(unsigned int irq)
  151. {
  152. struct irq_desc *desc = irq_to_desc(irq);
  153. struct irqaction *action;
  154. unsigned int status;
  155. kstat_incr_irqs_this_cpu(irq, desc);
  156. if (CHECK_IRQ_PER_CPU(desc->status)) {
  157. irqreturn_t action_ret;
  158. /*
  159. * No locking required for CPU-local interrupts:
  160. */
  161. if (desc->chip->ack)
  162. desc->chip->ack(irq);
  163. if (likely(!(desc->status & IRQ_DISABLED))) {
  164. action_ret = handle_IRQ_event(irq, desc->action);
  165. if (!noirqdebug)
  166. note_interrupt(irq, desc, action_ret);
  167. }
  168. desc->chip->end(irq);
  169. return 1;
  170. }
  171. spin_lock(&desc->lock);
  172. if (desc->chip->ack)
  173. desc->chip->ack(irq);
  174. /*
  175. * REPLAY is when Linux resends an IRQ that was dropped earlier
  176. * WAITING is used by probe to mark irqs that are being tested
  177. */
  178. status = desc->status & ~(IRQ_REPLAY | IRQ_WAITING);
  179. status |= IRQ_PENDING; /* we _want_ to handle it */
  180. /*
  181. * If the IRQ is disabled for whatever reason, we cannot
  182. * use the action we have.
  183. */
  184. action = NULL;
  185. if (likely(!(status & (IRQ_DISABLED | IRQ_INPROGRESS)))) {
  186. action = desc->action;
  187. status &= ~IRQ_PENDING; /* we commit to handling */
  188. status |= IRQ_INPROGRESS; /* we are handling it */
  189. }
  190. desc->status = status;
  191. /*
  192. * If there is no IRQ handler or it was disabled, exit early.
  193. * Since we set PENDING, if another processor is handling
  194. * a different instance of this same irq, the other processor
  195. * will take care of it.
  196. */
  197. if (unlikely(!action))
  198. goto out;
  199. /*
  200. * Edge triggered interrupts need to remember
  201. * pending events.
  202. * This applies to any hw interrupts that allow a second
  203. * instance of the same irq to arrive while we are in do_IRQ
  204. * or in the handler. But the code here only handles the _second_
  205. * instance of the irq, not the third or fourth. So it is mostly
  206. * useful for irq hardware that does not mask cleanly in an
  207. * SMP environment.
  208. */
  209. for (;;) {
  210. irqreturn_t action_ret;
  211. spin_unlock(&desc->lock);
  212. action_ret = handle_IRQ_event(irq, action);
  213. if (!noirqdebug)
  214. note_interrupt(irq, desc, action_ret);
  215. spin_lock(&desc->lock);
  216. if (likely(!(desc->status & IRQ_PENDING)))
  217. break;
  218. desc->status &= ~IRQ_PENDING;
  219. }
  220. desc->status &= ~IRQ_INPROGRESS;
  221. out:
  222. /*
  223. * The ->end() handler has to deal with interrupts which got
  224. * disabled while the handler was running.
  225. */
  226. desc->chip->end(irq);
  227. spin_unlock(&desc->lock);
  228. return 1;
  229. }
  230. #endif
  231. #ifdef CONFIG_TRACE_IRQFLAGS
  232. /*
  233. * lockdep: we want to handle all irq_desc locks as a single lock-class:
  234. */
  235. static struct lock_class_key irq_desc_lock_class;
  236. void early_init_irq_lock_class(void)
  237. {
  238. struct irq_desc *desc;
  239. int i;
  240. for_each_irq_desc(i, desc)
  241. lockdep_set_class(&desc->lock, &irq_desc_lock_class);
  242. }
  243. #endif