handle.c 11 KB

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