handle.c 11 KB

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