handle.c 6.2 KB

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