handle.c 10 KB

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