handle.c 4.7 KB

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