handle.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. if (desc->handler->ack)
  106. desc->handler->ack(irq);
  107. action_ret = handle_IRQ_event(irq, regs, desc->action);
  108. desc->handler->end(irq);
  109. return 1;
  110. }
  111. spin_lock(&desc->lock);
  112. if (desc->handler->ack)
  113. desc->handler->ack(irq);
  114. /*
  115. * REPLAY is when Linux resends an IRQ that was dropped earlier
  116. * WAITING is used by probe to mark irqs that are being tested
  117. */
  118. status = desc->status & ~(IRQ_REPLAY | IRQ_WAITING);
  119. status |= IRQ_PENDING; /* we _want_ to handle it */
  120. /*
  121. * If the IRQ is disabled for whatever reason, we cannot
  122. * use the action we have.
  123. */
  124. action = NULL;
  125. if (likely(!(status & (IRQ_DISABLED | IRQ_INPROGRESS)))) {
  126. action = desc->action;
  127. status &= ~IRQ_PENDING; /* we commit to handling */
  128. status |= IRQ_INPROGRESS; /* we are handling it */
  129. }
  130. desc->status = status;
  131. /*
  132. * If there is no IRQ handler or it was disabled, exit early.
  133. * Since we set PENDING, if another processor is handling
  134. * a different instance of this same irq, the other processor
  135. * will take care of it.
  136. */
  137. if (unlikely(!action))
  138. goto out;
  139. /*
  140. * Edge triggered interrupts need to remember
  141. * pending events.
  142. * This applies to any hw interrupts that allow a second
  143. * instance of the same irq to arrive while we are in do_IRQ
  144. * or in the handler. But the code here only handles the _second_
  145. * instance of the irq, not the third or fourth. So it is mostly
  146. * useful for irq hardware that does not mask cleanly in an
  147. * SMP environment.
  148. */
  149. for (;;) {
  150. irqreturn_t action_ret;
  151. spin_unlock(&desc->lock);
  152. action_ret = handle_IRQ_event(irq, regs, action);
  153. spin_lock(&desc->lock);
  154. if (!noirqdebug)
  155. note_interrupt(irq, desc, action_ret, regs);
  156. if (likely(!(desc->status & IRQ_PENDING)))
  157. break;
  158. desc->status &= ~IRQ_PENDING;
  159. }
  160. desc->status &= ~IRQ_INPROGRESS;
  161. out:
  162. /*
  163. * The ->end() handler has to deal with interrupts which got
  164. * disabled while the handler was running.
  165. */
  166. desc->handler->end(irq);
  167. spin_unlock(&desc->lock);
  168. return 1;
  169. }