handle.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * linux/kernel/irq/handle.c
  3. *
  4. * Copyright (C) 1992, 1998-2004 Linus Torvalds, Ingo Molnar
  5. *
  6. * This file contains the core interrupt handling code.
  7. */
  8. #include <linux/irq.h>
  9. #include <linux/module.h>
  10. #include <linux/random.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/kernel_stat.h>
  13. #include "internals.h"
  14. /*
  15. * Linux has a controller-independent interrupt architecture.
  16. * Every controller has a 'controller-template', that is used
  17. * by the main code to do the right thing. Each driver-visible
  18. * interrupt source is transparently wired to the apropriate
  19. * controller. Thus drivers need not be aware of the
  20. * interrupt-controller.
  21. *
  22. * The code is designed to be easily extended with new/different
  23. * interrupt controllers, without having to do assembly magic or
  24. * having to touch the generic code.
  25. *
  26. * Controller mappings for all interrupt sources:
  27. */
  28. irq_desc_t irq_desc[NR_IRQS] __cacheline_aligned = {
  29. [0 ... NR_IRQS-1] = {
  30. .status = IRQ_DISABLED,
  31. .handler = &no_irq_type,
  32. .lock = SPIN_LOCK_UNLOCKED
  33. }
  34. };
  35. /*
  36. * Generic 'no controller' code
  37. */
  38. static void end_none(unsigned int irq) { }
  39. static void enable_none(unsigned int irq) { }
  40. static void disable_none(unsigned int irq) { }
  41. static void shutdown_none(unsigned int irq) { }
  42. static unsigned int startup_none(unsigned int irq) { return 0; }
  43. static void ack_none(unsigned int irq)
  44. {
  45. /*
  46. * 'what should we do if we get a hw irq event on an illegal vector'.
  47. * each architecture has to answer this themself.
  48. */
  49. ack_bad_irq(irq);
  50. }
  51. struct hw_interrupt_type no_irq_type = {
  52. .typename = "none",
  53. .startup = startup_none,
  54. .shutdown = shutdown_none,
  55. .enable = enable_none,
  56. .disable = disable_none,
  57. .ack = ack_none,
  58. .end = end_none,
  59. .set_affinity = NULL
  60. };
  61. /*
  62. * Special, empty irq handler:
  63. */
  64. irqreturn_t no_action(int cpl, void *dev_id, struct pt_regs *regs)
  65. {
  66. return IRQ_NONE;
  67. }
  68. /*
  69. * Have got an event to handle:
  70. */
  71. fastcall irqreturn_t handle_IRQ_event(unsigned int irq, struct pt_regs *regs,
  72. struct irqaction *action)
  73. {
  74. irqreturn_t ret, retval = IRQ_NONE;
  75. unsigned int status = 0;
  76. if (!(action->flags & SA_INTERRUPT))
  77. local_irq_enable();
  78. do {
  79. ret = action->handler(irq, action->dev_id, regs);
  80. if (ret == IRQ_HANDLED)
  81. status |= action->flags;
  82. retval |= ret;
  83. action = action->next;
  84. } while (action);
  85. if (status & SA_SAMPLE_RANDOM)
  86. add_interrupt_randomness(irq);
  87. local_irq_disable();
  88. return retval;
  89. }
  90. /*
  91. * do_IRQ handles all normal device IRQ's (the special
  92. * SMP cross-CPU interrupts have their own specific
  93. * handlers).
  94. */
  95. fastcall unsigned int __do_IRQ(unsigned int irq, struct pt_regs *regs)
  96. {
  97. irq_desc_t *desc = irq_desc + irq;
  98. struct irqaction * action;
  99. unsigned int status;
  100. kstat_this_cpu.irqs[irq]++;
  101. if (CHECK_IRQ_PER_CPU(desc->status)) {
  102. irqreturn_t action_ret;
  103. /*
  104. * No locking required for CPU-local interrupts:
  105. */
  106. if (desc->handler->ack)
  107. desc->handler->ack(irq);
  108. action_ret = handle_IRQ_event(irq, regs, desc->action);
  109. desc->handler->end(irq);
  110. return 1;
  111. }
  112. spin_lock(&desc->lock);
  113. if (desc->handler->ack)
  114. desc->handler->ack(irq);
  115. /*
  116. * REPLAY is when Linux resends an IRQ that was dropped earlier
  117. * WAITING is used by probe to mark irqs that are being tested
  118. */
  119. status = desc->status & ~(IRQ_REPLAY | IRQ_WAITING);
  120. status |= IRQ_PENDING; /* we _want_ to handle it */
  121. /*
  122. * If the IRQ is disabled for whatever reason, we cannot
  123. * use the action we have.
  124. */
  125. action = NULL;
  126. if (likely(!(status & (IRQ_DISABLED | IRQ_INPROGRESS)))) {
  127. action = desc->action;
  128. status &= ~IRQ_PENDING; /* we commit to handling */
  129. status |= IRQ_INPROGRESS; /* we are handling it */
  130. }
  131. desc->status = status;
  132. /*
  133. * If there is no IRQ handler or it was disabled, exit early.
  134. * Since we set PENDING, if another processor is handling
  135. * a different instance of this same irq, the other processor
  136. * will take care of it.
  137. */
  138. if (unlikely(!action))
  139. goto out;
  140. /*
  141. * Edge triggered interrupts need to remember
  142. * pending events.
  143. * This applies to any hw interrupts that allow a second
  144. * instance of the same irq to arrive while we are in do_IRQ
  145. * or in the handler. But the code here only handles the _second_
  146. * instance of the irq, not the third or fourth. So it is mostly
  147. * useful for irq hardware that does not mask cleanly in an
  148. * SMP environment.
  149. */
  150. for (;;) {
  151. irqreturn_t action_ret;
  152. spin_unlock(&desc->lock);
  153. action_ret = handle_IRQ_event(irq, regs, action);
  154. spin_lock(&desc->lock);
  155. if (!noirqdebug)
  156. note_interrupt(irq, desc, action_ret, regs);
  157. if (likely(!(desc->status & IRQ_PENDING)))
  158. break;
  159. desc->status &= ~IRQ_PENDING;
  160. }
  161. desc->status &= ~IRQ_INPROGRESS;
  162. out:
  163. /*
  164. * The ->end() handler has to deal with interrupts which got
  165. * disabled while the handler was running.
  166. */
  167. desc->handler->end(irq);
  168. spin_unlock(&desc->lock);
  169. return 1;
  170. }