handle.c 10 KB

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