handle.c 11 KB

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