handle.c 11 KB

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