handle.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 int handle_IRQ_event(unsigned int irq, struct pt_regs *regs,
  72. struct irqaction *action)
  73. {
  74. int ret, retval = 0, status = 0;
  75. if (!(action->flags & SA_INTERRUPT))
  76. local_irq_enable();
  77. do {
  78. ret = action->handler(irq, action->dev_id, regs);
  79. if (ret == IRQ_HANDLED)
  80. status |= action->flags;
  81. retval |= ret;
  82. action = action->next;
  83. } while (action);
  84. if (status & SA_SAMPLE_RANDOM)
  85. add_interrupt_randomness(irq);
  86. local_irq_disable();
  87. return retval;
  88. }
  89. /*
  90. * do_IRQ handles all normal device IRQ's (the special
  91. * SMP cross-CPU interrupts have their own specific
  92. * handlers).
  93. */
  94. fastcall unsigned int __do_IRQ(unsigned int irq, struct pt_regs *regs)
  95. {
  96. irq_desc_t *desc = irq_desc + irq;
  97. struct irqaction * action;
  98. unsigned int status;
  99. kstat_this_cpu.irqs[irq]++;
  100. if (CHECK_IRQ_PER_CPU(desc->status)) {
  101. irqreturn_t action_ret;
  102. /*
  103. * No locking required for CPU-local interrupts:
  104. */
  105. desc->handler->ack(irq);
  106. action_ret = handle_IRQ_event(irq, regs, desc->action);
  107. desc->handler->end(irq);
  108. return 1;
  109. }
  110. spin_lock(&desc->lock);
  111. desc->handler->ack(irq);
  112. /*
  113. * REPLAY is when Linux resends an IRQ that was dropped earlier
  114. * WAITING is used by probe to mark irqs that are being tested
  115. */
  116. status = desc->status & ~(IRQ_REPLAY | IRQ_WAITING);
  117. status |= IRQ_PENDING; /* we _want_ to handle it */
  118. /*
  119. * If the IRQ is disabled for whatever reason, we cannot
  120. * use the action we have.
  121. */
  122. action = NULL;
  123. if (likely(!(status & (IRQ_DISABLED | IRQ_INPROGRESS)))) {
  124. action = desc->action;
  125. status &= ~IRQ_PENDING; /* we commit to handling */
  126. status |= IRQ_INPROGRESS; /* we are handling it */
  127. }
  128. desc->status = status;
  129. /*
  130. * If there is no IRQ handler or it was disabled, exit early.
  131. * Since we set PENDING, if another processor is handling
  132. * a different instance of this same irq, the other processor
  133. * will take care of it.
  134. */
  135. if (unlikely(!action))
  136. goto out;
  137. /*
  138. * Edge triggered interrupts need to remember
  139. * pending events.
  140. * This applies to any hw interrupts that allow a second
  141. * instance of the same irq to arrive while we are in do_IRQ
  142. * or in the handler. But the code here only handles the _second_
  143. * instance of the irq, not the third or fourth. So it is mostly
  144. * useful for irq hardware that does not mask cleanly in an
  145. * SMP environment.
  146. */
  147. for (;;) {
  148. irqreturn_t action_ret;
  149. spin_unlock(&desc->lock);
  150. action_ret = handle_IRQ_event(irq, regs, action);
  151. spin_lock(&desc->lock);
  152. if (!noirqdebug)
  153. note_interrupt(irq, desc, action_ret, regs);
  154. if (likely(!(desc->status & IRQ_PENDING)))
  155. break;
  156. desc->status &= ~IRQ_PENDING;
  157. }
  158. desc->status &= ~IRQ_INPROGRESS;
  159. out:
  160. /*
  161. * The ->end() handler has to deal with interrupts which got
  162. * disabled while the handler was running.
  163. */
  164. desc->handler->end(irq);
  165. spin_unlock(&desc->lock);
  166. return 1;
  167. }