handle.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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 <linux/rculist.h>
  18. #include <linux/hash.h>
  19. #include "internals.h"
  20. /*
  21. * lockdep: we want to handle all irq_desc locks as a single lock-class:
  22. */
  23. struct lock_class_key irq_desc_lock_class;
  24. /**
  25. * handle_bad_irq - handle spurious and unhandled irqs
  26. * @irq: the interrupt number
  27. * @desc: description of the interrupt
  28. *
  29. * Handles spurious and unhandled IRQ's. It also prints a debugmessage.
  30. */
  31. void handle_bad_irq(unsigned int irq, struct irq_desc *desc)
  32. {
  33. print_irq_desc(irq, desc);
  34. kstat_incr_irqs_this_cpu(irq, desc);
  35. ack_bad_irq(irq);
  36. }
  37. /*
  38. * Linux has a controller-independent interrupt architecture.
  39. * Every controller has a 'controller-template', that is used
  40. * by the main code to do the right thing. Each driver-visible
  41. * interrupt source is transparently wired to the appropriate
  42. * controller. Thus drivers need not be aware of the
  43. * interrupt-controller.
  44. *
  45. * The code is designed to be easily extended with new/different
  46. * interrupt controllers, without having to do assembly magic or
  47. * having to touch the generic code.
  48. *
  49. * Controller mappings for all interrupt sources:
  50. */
  51. int nr_irqs = NR_IRQS;
  52. EXPORT_SYMBOL_GPL(nr_irqs);
  53. void __init __attribute__((weak)) arch_early_irq_init(void)
  54. {
  55. }
  56. #ifdef CONFIG_SPARSE_IRQ
  57. static struct irq_desc irq_desc_init = {
  58. .irq = -1,
  59. .status = IRQ_DISABLED,
  60. .chip = &no_irq_chip,
  61. .handle_irq = handle_bad_irq,
  62. .depth = 1,
  63. .lock = __SPIN_LOCK_UNLOCKED(irq_desc_init.lock),
  64. #ifdef CONFIG_SMP
  65. .affinity = CPU_MASK_ALL
  66. #endif
  67. };
  68. void init_kstat_irqs(struct irq_desc *desc, int cpu, int nr)
  69. {
  70. unsigned long bytes;
  71. char *ptr;
  72. int node;
  73. /* Compute how many bytes we need per irq and allocate them */
  74. bytes = nr * sizeof(unsigned int);
  75. node = cpu_to_node(cpu);
  76. ptr = kzalloc_node(bytes, GFP_ATOMIC, node);
  77. printk(KERN_DEBUG " alloc kstat_irqs on cpu %d node %d\n", cpu, node);
  78. if (ptr)
  79. desc->kstat_irqs = (unsigned int *)ptr;
  80. }
  81. void __attribute__((weak)) arch_init_chip_data(struct irq_desc *desc, int cpu)
  82. {
  83. }
  84. static void init_one_irq_desc(int irq, struct irq_desc *desc, int cpu)
  85. {
  86. memcpy(desc, &irq_desc_init, sizeof(struct irq_desc));
  87. desc->irq = irq;
  88. #ifdef CONFIG_SMP
  89. desc->cpu = cpu;
  90. #endif
  91. lockdep_set_class(&desc->lock, &irq_desc_lock_class);
  92. init_kstat_irqs(desc, cpu, nr_cpu_ids);
  93. if (!desc->kstat_irqs) {
  94. printk(KERN_ERR "can not alloc kstat_irqs\n");
  95. BUG_ON(1);
  96. }
  97. arch_init_chip_data(desc, cpu);
  98. }
  99. /*
  100. * Protect the sparse_irqs:
  101. */
  102. DEFINE_SPINLOCK(sparse_irq_lock);
  103. struct irq_desc *irq_desc_ptrs[NR_IRQS] __read_mostly;
  104. static struct irq_desc irq_desc_legacy[NR_IRQS_LEGACY] __cacheline_aligned_in_smp = {
  105. [0 ... NR_IRQS_LEGACY-1] = {
  106. .irq = -1,
  107. .status = IRQ_DISABLED,
  108. .chip = &no_irq_chip,
  109. .handle_irq = handle_bad_irq,
  110. .depth = 1,
  111. .lock = __SPIN_LOCK_UNLOCKED(irq_desc_init.lock),
  112. #ifdef CONFIG_SMP
  113. .affinity = CPU_MASK_ALL
  114. #endif
  115. }
  116. };
  117. /* FIXME: use bootmem alloc ...*/
  118. static unsigned int kstat_irqs_legacy[NR_IRQS_LEGACY][NR_CPUS];
  119. void __init early_irq_init(void)
  120. {
  121. struct irq_desc *desc;
  122. int legacy_count;
  123. int i;
  124. desc = irq_desc_legacy;
  125. legacy_count = ARRAY_SIZE(irq_desc_legacy);
  126. for (i = 0; i < legacy_count; i++) {
  127. desc[i].irq = i;
  128. desc[i].kstat_irqs = kstat_irqs_legacy[i];
  129. irq_desc_ptrs[i] = desc + i;
  130. }
  131. for (i = legacy_count; i < NR_IRQS; i++)
  132. irq_desc_ptrs[i] = NULL;
  133. arch_early_irq_init();
  134. }
  135. struct irq_desc *irq_to_desc(unsigned int irq)
  136. {
  137. return (irq < NR_IRQS) ? irq_desc_ptrs[irq] : NULL;
  138. }
  139. struct irq_desc *irq_to_desc_alloc_cpu(unsigned int irq, int cpu)
  140. {
  141. struct irq_desc *desc;
  142. unsigned long flags;
  143. int node;
  144. if (irq >= NR_IRQS) {
  145. printk(KERN_WARNING "irq >= NR_IRQS in irq_to_desc_alloc: %d %d\n",
  146. irq, NR_IRQS);
  147. WARN_ON(1);
  148. return NULL;
  149. }
  150. desc = irq_desc_ptrs[irq];
  151. if (desc)
  152. return desc;
  153. spin_lock_irqsave(&sparse_irq_lock, flags);
  154. /* We have to check it to avoid races with another CPU */
  155. desc = irq_desc_ptrs[irq];
  156. if (desc)
  157. goto out_unlock;
  158. node = cpu_to_node(cpu);
  159. desc = kzalloc_node(sizeof(*desc), GFP_ATOMIC, node);
  160. printk(KERN_DEBUG " alloc irq_desc for %d on cpu %d node %d\n",
  161. irq, cpu, node);
  162. if (!desc) {
  163. printk(KERN_ERR "can not alloc irq_desc\n");
  164. BUG_ON(1);
  165. }
  166. init_one_irq_desc(irq, desc, cpu);
  167. irq_desc_ptrs[irq] = desc;
  168. out_unlock:
  169. spin_unlock_irqrestore(&sparse_irq_lock, flags);
  170. return desc;
  171. }
  172. #else /* !CONFIG_SPARSE_IRQ */
  173. struct irq_desc irq_desc[NR_IRQS] __cacheline_aligned_in_smp = {
  174. [0 ... NR_IRQS-1] = {
  175. .status = IRQ_DISABLED,
  176. .chip = &no_irq_chip,
  177. .handle_irq = handle_bad_irq,
  178. .depth = 1,
  179. .lock = __SPIN_LOCK_UNLOCKED(irq_desc->lock),
  180. #ifdef CONFIG_SMP
  181. .affinity = CPU_MASK_ALL
  182. #endif
  183. }
  184. };
  185. struct irq_desc *irq_to_desc(unsigned int irq)
  186. {
  187. return (irq < NR_IRQS) ? irq_desc + irq : NULL;
  188. }
  189. struct irq_desc *irq_to_desc_alloc_cpu(unsigned int irq, int cpu)
  190. {
  191. return irq_to_desc(irq);
  192. }
  193. #endif /* !CONFIG_SPARSE_IRQ */
  194. /*
  195. * What should we do if we get a hw irq event on an illegal vector?
  196. * Each architecture has to answer this themself.
  197. */
  198. static void ack_bad(unsigned int irq)
  199. {
  200. struct irq_desc *desc = irq_to_desc(irq);
  201. print_irq_desc(irq, desc);
  202. ack_bad_irq(irq);
  203. }
  204. /*
  205. * NOP functions
  206. */
  207. static void noop(unsigned int irq)
  208. {
  209. }
  210. static unsigned int noop_ret(unsigned int irq)
  211. {
  212. return 0;
  213. }
  214. /*
  215. * Generic no controller implementation
  216. */
  217. struct irq_chip no_irq_chip = {
  218. .name = "none",
  219. .startup = noop_ret,
  220. .shutdown = noop,
  221. .enable = noop,
  222. .disable = noop,
  223. .ack = ack_bad,
  224. .end = noop,
  225. };
  226. /*
  227. * Generic dummy implementation which can be used for
  228. * real dumb interrupt sources
  229. */
  230. struct irq_chip dummy_irq_chip = {
  231. .name = "dummy",
  232. .startup = noop_ret,
  233. .shutdown = noop,
  234. .enable = noop,
  235. .disable = noop,
  236. .ack = noop,
  237. .mask = noop,
  238. .unmask = noop,
  239. .end = noop,
  240. };
  241. /*
  242. * Special, empty irq handler:
  243. */
  244. irqreturn_t no_action(int cpl, void *dev_id)
  245. {
  246. return IRQ_NONE;
  247. }
  248. /**
  249. * handle_IRQ_event - irq action chain handler
  250. * @irq: the interrupt number
  251. * @action: the interrupt action chain for this irq
  252. *
  253. * Handles the action chain of an irq event
  254. */
  255. irqreturn_t handle_IRQ_event(unsigned int irq, struct irqaction *action)
  256. {
  257. irqreturn_t ret, retval = IRQ_NONE;
  258. unsigned int status = 0;
  259. if (!(action->flags & IRQF_DISABLED))
  260. local_irq_enable_in_hardirq();
  261. do {
  262. ret = action->handler(irq, action->dev_id);
  263. if (ret == IRQ_HANDLED)
  264. status |= action->flags;
  265. retval |= ret;
  266. action = action->next;
  267. } while (action);
  268. if (status & IRQF_SAMPLE_RANDOM)
  269. add_interrupt_randomness(irq);
  270. local_irq_disable();
  271. return retval;
  272. }
  273. #ifndef CONFIG_GENERIC_HARDIRQS_NO__DO_IRQ
  274. /**
  275. * __do_IRQ - original all in one highlevel IRQ handler
  276. * @irq: the interrupt number
  277. *
  278. * __do_IRQ handles all normal device IRQ's (the special
  279. * SMP cross-CPU interrupts have their own specific
  280. * handlers).
  281. *
  282. * This is the original x86 implementation which is used for every
  283. * interrupt type.
  284. */
  285. unsigned int __do_IRQ(unsigned int irq)
  286. {
  287. struct irq_desc *desc = irq_to_desc(irq);
  288. struct irqaction *action;
  289. unsigned int status;
  290. kstat_incr_irqs_this_cpu(irq, desc);
  291. if (CHECK_IRQ_PER_CPU(desc->status)) {
  292. irqreturn_t action_ret;
  293. /*
  294. * No locking required for CPU-local interrupts:
  295. */
  296. if (desc->chip->ack) {
  297. desc->chip->ack(irq);
  298. /* get new one */
  299. desc = irq_remap_to_desc(irq, desc);
  300. }
  301. if (likely(!(desc->status & IRQ_DISABLED))) {
  302. action_ret = handle_IRQ_event(irq, desc->action);
  303. if (!noirqdebug)
  304. note_interrupt(irq, desc, action_ret);
  305. }
  306. desc->chip->end(irq);
  307. return 1;
  308. }
  309. spin_lock(&desc->lock);
  310. if (desc->chip->ack) {
  311. desc->chip->ack(irq);
  312. desc = irq_remap_to_desc(irq, desc);
  313. }
  314. /*
  315. * REPLAY is when Linux resends an IRQ that was dropped earlier
  316. * WAITING is used by probe to mark irqs that are being tested
  317. */
  318. status = desc->status & ~(IRQ_REPLAY | IRQ_WAITING);
  319. status |= IRQ_PENDING; /* we _want_ to handle it */
  320. /*
  321. * If the IRQ is disabled for whatever reason, we cannot
  322. * use the action we have.
  323. */
  324. action = NULL;
  325. if (likely(!(status & (IRQ_DISABLED | IRQ_INPROGRESS)))) {
  326. action = desc->action;
  327. status &= ~IRQ_PENDING; /* we commit to handling */
  328. status |= IRQ_INPROGRESS; /* we are handling it */
  329. }
  330. desc->status = status;
  331. /*
  332. * If there is no IRQ handler or it was disabled, exit early.
  333. * Since we set PENDING, if another processor is handling
  334. * a different instance of this same irq, the other processor
  335. * will take care of it.
  336. */
  337. if (unlikely(!action))
  338. goto out;
  339. /*
  340. * Edge triggered interrupts need to remember
  341. * pending events.
  342. * This applies to any hw interrupts that allow a second
  343. * instance of the same irq to arrive while we are in do_IRQ
  344. * or in the handler. But the code here only handles the _second_
  345. * instance of the irq, not the third or fourth. So it is mostly
  346. * useful for irq hardware that does not mask cleanly in an
  347. * SMP environment.
  348. */
  349. for (;;) {
  350. irqreturn_t action_ret;
  351. spin_unlock(&desc->lock);
  352. action_ret = handle_IRQ_event(irq, action);
  353. if (!noirqdebug)
  354. note_interrupt(irq, desc, action_ret);
  355. spin_lock(&desc->lock);
  356. if (likely(!(desc->status & IRQ_PENDING)))
  357. break;
  358. desc->status &= ~IRQ_PENDING;
  359. }
  360. desc->status &= ~IRQ_INPROGRESS;
  361. out:
  362. /*
  363. * The ->end() handler has to deal with interrupts which got
  364. * disabled while the handler was running.
  365. */
  366. desc->chip->end(irq);
  367. spin_unlock(&desc->lock);
  368. return 1;
  369. }
  370. #endif
  371. void early_init_irq_lock_class(void)
  372. {
  373. struct irq_desc *desc;
  374. int i;
  375. for_each_irq_desc(i, desc) {
  376. lockdep_set_class(&desc->lock, &irq_desc_lock_class);
  377. }
  378. }
  379. #ifdef CONFIG_SPARSE_IRQ
  380. unsigned int kstat_irqs_cpu(unsigned int irq, int cpu)
  381. {
  382. struct irq_desc *desc = irq_to_desc(irq);
  383. return desc ? desc->kstat_irqs[cpu] : 0;
  384. }
  385. #endif
  386. EXPORT_SYMBOL(kstat_irqs_cpu);