handle.c 11 KB

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