chip.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  1. /*
  2. * linux/kernel/irq/chip.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, for irq-chip
  8. * based architectures.
  9. *
  10. * Detailed information is available in Documentation/DocBook/genericirq
  11. */
  12. #include <linux/irq.h>
  13. #include <linux/msi.h>
  14. #include <linux/module.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/kernel_stat.h>
  17. #include "internals.h"
  18. /**
  19. * dynamic_irq_init - initialize a dynamically allocated irq
  20. * @irq: irq number to initialize
  21. */
  22. void dynamic_irq_init(unsigned int irq)
  23. {
  24. struct irq_desc *desc;
  25. unsigned long flags;
  26. if (irq >= NR_IRQS) {
  27. WARN(1, KERN_ERR "Trying to initialize invalid IRQ%d\n", irq);
  28. return;
  29. }
  30. /* Ensure we don't have left over values from a previous use of this irq */
  31. desc = irq_desc + irq;
  32. spin_lock_irqsave(&desc->lock, flags);
  33. desc->status = IRQ_DISABLED;
  34. desc->chip = &no_irq_chip;
  35. desc->handle_irq = handle_bad_irq;
  36. desc->depth = 1;
  37. desc->msi_desc = NULL;
  38. desc->handler_data = NULL;
  39. desc->chip_data = NULL;
  40. desc->action = NULL;
  41. desc->irq_count = 0;
  42. desc->irqs_unhandled = 0;
  43. #ifdef CONFIG_SMP
  44. cpus_setall(desc->affinity);
  45. #endif
  46. spin_unlock_irqrestore(&desc->lock, flags);
  47. }
  48. /**
  49. * dynamic_irq_cleanup - cleanup a dynamically allocated irq
  50. * @irq: irq number to initialize
  51. */
  52. void dynamic_irq_cleanup(unsigned int irq)
  53. {
  54. struct irq_desc *desc;
  55. unsigned long flags;
  56. if (irq >= NR_IRQS) {
  57. WARN(1, KERN_ERR "Trying to cleanup invalid IRQ%d\n", irq);
  58. return;
  59. }
  60. desc = irq_desc + irq;
  61. spin_lock_irqsave(&desc->lock, flags);
  62. if (desc->action) {
  63. spin_unlock_irqrestore(&desc->lock, flags);
  64. WARN(1, KERN_ERR "Destroying IRQ%d without calling free_irq\n",
  65. irq);
  66. return;
  67. }
  68. desc->msi_desc = NULL;
  69. desc->handler_data = NULL;
  70. desc->chip_data = NULL;
  71. desc->handle_irq = handle_bad_irq;
  72. desc->chip = &no_irq_chip;
  73. spin_unlock_irqrestore(&desc->lock, flags);
  74. }
  75. /**
  76. * set_irq_chip - set the irq chip for an irq
  77. * @irq: irq number
  78. * @chip: pointer to irq chip description structure
  79. */
  80. int set_irq_chip(unsigned int irq, struct irq_chip *chip)
  81. {
  82. struct irq_desc *desc;
  83. unsigned long flags;
  84. if (irq >= NR_IRQS) {
  85. WARN(1, KERN_ERR "Trying to install chip for IRQ%d\n", irq);
  86. return -EINVAL;
  87. }
  88. if (!chip)
  89. chip = &no_irq_chip;
  90. desc = irq_desc + irq;
  91. spin_lock_irqsave(&desc->lock, flags);
  92. irq_chip_set_defaults(chip);
  93. desc->chip = chip;
  94. spin_unlock_irqrestore(&desc->lock, flags);
  95. return 0;
  96. }
  97. EXPORT_SYMBOL(set_irq_chip);
  98. /**
  99. * set_irq_type - set the irq type for an irq
  100. * @irq: irq number
  101. * @type: interrupt type - see include/linux/interrupt.h
  102. */
  103. int set_irq_type(unsigned int irq, unsigned int type)
  104. {
  105. struct irq_desc *desc;
  106. unsigned long flags;
  107. int ret = -ENXIO;
  108. if (irq >= NR_IRQS) {
  109. printk(KERN_ERR "Trying to set irq type for IRQ%d\n", irq);
  110. return -ENODEV;
  111. }
  112. desc = irq_desc + irq;
  113. if (desc->chip->set_type) {
  114. spin_lock_irqsave(&desc->lock, flags);
  115. ret = desc->chip->set_type(irq, type);
  116. spin_unlock_irqrestore(&desc->lock, flags);
  117. }
  118. return ret;
  119. }
  120. EXPORT_SYMBOL(set_irq_type);
  121. /**
  122. * set_irq_data - set irq type data for an irq
  123. * @irq: Interrupt number
  124. * @data: Pointer to interrupt specific data
  125. *
  126. * Set the hardware irq controller data for an irq
  127. */
  128. int set_irq_data(unsigned int irq, void *data)
  129. {
  130. struct irq_desc *desc;
  131. unsigned long flags;
  132. if (irq >= NR_IRQS) {
  133. printk(KERN_ERR
  134. "Trying to install controller data for IRQ%d\n", irq);
  135. return -EINVAL;
  136. }
  137. desc = irq_desc + irq;
  138. spin_lock_irqsave(&desc->lock, flags);
  139. desc->handler_data = data;
  140. spin_unlock_irqrestore(&desc->lock, flags);
  141. return 0;
  142. }
  143. EXPORT_SYMBOL(set_irq_data);
  144. /**
  145. * set_irq_data - set irq type data for an irq
  146. * @irq: Interrupt number
  147. * @entry: Pointer to MSI descriptor data
  148. *
  149. * Set the hardware irq controller data for an irq
  150. */
  151. int set_irq_msi(unsigned int irq, struct msi_desc *entry)
  152. {
  153. struct irq_desc *desc;
  154. unsigned long flags;
  155. if (irq >= NR_IRQS) {
  156. printk(KERN_ERR
  157. "Trying to install msi data for IRQ%d\n", irq);
  158. return -EINVAL;
  159. }
  160. desc = irq_desc + irq;
  161. spin_lock_irqsave(&desc->lock, flags);
  162. desc->msi_desc = entry;
  163. if (entry)
  164. entry->irq = irq;
  165. spin_unlock_irqrestore(&desc->lock, flags);
  166. return 0;
  167. }
  168. /**
  169. * set_irq_chip_data - set irq chip data for an irq
  170. * @irq: Interrupt number
  171. * @data: Pointer to chip specific data
  172. *
  173. * Set the hardware irq chip data for an irq
  174. */
  175. int set_irq_chip_data(unsigned int irq, void *data)
  176. {
  177. struct irq_desc *desc = irq_desc + irq;
  178. unsigned long flags;
  179. if (irq >= NR_IRQS || !desc->chip) {
  180. printk(KERN_ERR "BUG: bad set_irq_chip_data(IRQ#%d)\n", irq);
  181. return -EINVAL;
  182. }
  183. spin_lock_irqsave(&desc->lock, flags);
  184. desc->chip_data = data;
  185. spin_unlock_irqrestore(&desc->lock, flags);
  186. return 0;
  187. }
  188. EXPORT_SYMBOL(set_irq_chip_data);
  189. /*
  190. * default enable function
  191. */
  192. static void default_enable(unsigned int irq)
  193. {
  194. struct irq_desc *desc = irq_desc + irq;
  195. desc->chip->unmask(irq);
  196. desc->status &= ~IRQ_MASKED;
  197. }
  198. /*
  199. * default disable function
  200. */
  201. static void default_disable(unsigned int irq)
  202. {
  203. }
  204. /*
  205. * default startup function
  206. */
  207. static unsigned int default_startup(unsigned int irq)
  208. {
  209. irq_desc[irq].chip->enable(irq);
  210. return 0;
  211. }
  212. /*
  213. * default shutdown function
  214. */
  215. static void default_shutdown(unsigned int irq)
  216. {
  217. struct irq_desc *desc = irq_desc + irq;
  218. desc->chip->mask(irq);
  219. desc->status |= IRQ_MASKED;
  220. }
  221. /*
  222. * Fixup enable/disable function pointers
  223. */
  224. void irq_chip_set_defaults(struct irq_chip *chip)
  225. {
  226. if (!chip->enable)
  227. chip->enable = default_enable;
  228. if (!chip->disable)
  229. chip->disable = default_disable;
  230. if (!chip->startup)
  231. chip->startup = default_startup;
  232. /*
  233. * We use chip->disable, when the user provided its own. When
  234. * we have default_disable set for chip->disable, then we need
  235. * to use default_shutdown, otherwise the irq line is not
  236. * disabled on free_irq():
  237. */
  238. if (!chip->shutdown)
  239. chip->shutdown = chip->disable != default_disable ?
  240. chip->disable : default_shutdown;
  241. if (!chip->name)
  242. chip->name = chip->typename;
  243. if (!chip->end)
  244. chip->end = dummy_irq_chip.end;
  245. }
  246. static inline void mask_ack_irq(struct irq_desc *desc, int irq)
  247. {
  248. if (desc->chip->mask_ack)
  249. desc->chip->mask_ack(irq);
  250. else {
  251. desc->chip->mask(irq);
  252. desc->chip->ack(irq);
  253. }
  254. }
  255. /**
  256. * handle_simple_irq - Simple and software-decoded IRQs.
  257. * @irq: the interrupt number
  258. * @desc: the interrupt description structure for this irq
  259. *
  260. * Simple interrupts are either sent from a demultiplexing interrupt
  261. * handler or come from hardware, where no interrupt hardware control
  262. * is necessary.
  263. *
  264. * Note: The caller is expected to handle the ack, clear, mask and
  265. * unmask issues if necessary.
  266. */
  267. void
  268. handle_simple_irq(unsigned int irq, struct irq_desc *desc)
  269. {
  270. struct irqaction *action;
  271. irqreturn_t action_ret;
  272. const unsigned int cpu = smp_processor_id();
  273. spin_lock(&desc->lock);
  274. if (unlikely(desc->status & IRQ_INPROGRESS))
  275. goto out_unlock;
  276. desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
  277. kstat_cpu(cpu).irqs[irq]++;
  278. action = desc->action;
  279. if (unlikely(!action || (desc->status & IRQ_DISABLED)))
  280. goto out_unlock;
  281. desc->status |= IRQ_INPROGRESS;
  282. spin_unlock(&desc->lock);
  283. action_ret = handle_IRQ_event(irq, action);
  284. if (!noirqdebug)
  285. note_interrupt(irq, desc, action_ret);
  286. spin_lock(&desc->lock);
  287. desc->status &= ~IRQ_INPROGRESS;
  288. out_unlock:
  289. spin_unlock(&desc->lock);
  290. }
  291. /**
  292. * handle_level_irq - Level type irq handler
  293. * @irq: the interrupt number
  294. * @desc: the interrupt description structure for this irq
  295. *
  296. * Level type interrupts are active as long as the hardware line has
  297. * the active level. This may require to mask the interrupt and unmask
  298. * it after the associated handler has acknowledged the device, so the
  299. * interrupt line is back to inactive.
  300. */
  301. void
  302. handle_level_irq(unsigned int irq, struct irq_desc *desc)
  303. {
  304. unsigned int cpu = smp_processor_id();
  305. struct irqaction *action;
  306. irqreturn_t action_ret;
  307. spin_lock(&desc->lock);
  308. mask_ack_irq(desc, irq);
  309. if (unlikely(desc->status & IRQ_INPROGRESS))
  310. goto out_unlock;
  311. desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
  312. kstat_cpu(cpu).irqs[irq]++;
  313. /*
  314. * If its disabled or no action available
  315. * keep it masked and get out of here
  316. */
  317. action = desc->action;
  318. if (unlikely(!action || (desc->status & IRQ_DISABLED)))
  319. goto out_unlock;
  320. desc->status |= IRQ_INPROGRESS;
  321. spin_unlock(&desc->lock);
  322. action_ret = handle_IRQ_event(irq, action);
  323. if (!noirqdebug)
  324. note_interrupt(irq, desc, action_ret);
  325. spin_lock(&desc->lock);
  326. desc->status &= ~IRQ_INPROGRESS;
  327. if (!(desc->status & IRQ_DISABLED) && desc->chip->unmask)
  328. desc->chip->unmask(irq);
  329. out_unlock:
  330. spin_unlock(&desc->lock);
  331. }
  332. /**
  333. * handle_fasteoi_irq - irq handler for transparent controllers
  334. * @irq: the interrupt number
  335. * @desc: the interrupt description structure for this irq
  336. *
  337. * Only a single callback will be issued to the chip: an ->eoi()
  338. * call when the interrupt has been serviced. This enables support
  339. * for modern forms of interrupt handlers, which handle the flow
  340. * details in hardware, transparently.
  341. */
  342. void
  343. handle_fasteoi_irq(unsigned int irq, struct irq_desc *desc)
  344. {
  345. unsigned int cpu = smp_processor_id();
  346. struct irqaction *action;
  347. irqreturn_t action_ret;
  348. spin_lock(&desc->lock);
  349. if (unlikely(desc->status & IRQ_INPROGRESS))
  350. goto out;
  351. desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
  352. kstat_cpu(cpu).irqs[irq]++;
  353. /*
  354. * If its disabled or no action available
  355. * then mask it and get out of here:
  356. */
  357. action = desc->action;
  358. if (unlikely(!action || (desc->status & IRQ_DISABLED))) {
  359. desc->status |= IRQ_PENDING;
  360. if (desc->chip->mask)
  361. desc->chip->mask(irq);
  362. goto out;
  363. }
  364. desc->status |= IRQ_INPROGRESS;
  365. desc->status &= ~IRQ_PENDING;
  366. spin_unlock(&desc->lock);
  367. action_ret = handle_IRQ_event(irq, action);
  368. if (!noirqdebug)
  369. note_interrupt(irq, desc, action_ret);
  370. spin_lock(&desc->lock);
  371. desc->status &= ~IRQ_INPROGRESS;
  372. out:
  373. desc->chip->eoi(irq);
  374. spin_unlock(&desc->lock);
  375. }
  376. /**
  377. * handle_edge_irq - edge type IRQ handler
  378. * @irq: the interrupt number
  379. * @desc: the interrupt description structure for this irq
  380. *
  381. * Interrupt occures on the falling and/or rising edge of a hardware
  382. * signal. The occurence is latched into the irq controller hardware
  383. * and must be acked in order to be reenabled. After the ack another
  384. * interrupt can happen on the same source even before the first one
  385. * is handled by the assosiacted event handler. If this happens it
  386. * might be necessary to disable (mask) the interrupt depending on the
  387. * controller hardware. This requires to reenable the interrupt inside
  388. * of the loop which handles the interrupts which have arrived while
  389. * the handler was running. If all pending interrupts are handled, the
  390. * loop is left.
  391. */
  392. void
  393. handle_edge_irq(unsigned int irq, struct irq_desc *desc)
  394. {
  395. const unsigned int cpu = smp_processor_id();
  396. spin_lock(&desc->lock);
  397. desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
  398. /*
  399. * If we're currently running this IRQ, or its disabled,
  400. * we shouldn't process the IRQ. Mark it pending, handle
  401. * the necessary masking and go out
  402. */
  403. if (unlikely((desc->status & (IRQ_INPROGRESS | IRQ_DISABLED)) ||
  404. !desc->action)) {
  405. desc->status |= (IRQ_PENDING | IRQ_MASKED);
  406. mask_ack_irq(desc, irq);
  407. goto out_unlock;
  408. }
  409. kstat_cpu(cpu).irqs[irq]++;
  410. /* Start handling the irq */
  411. desc->chip->ack(irq);
  412. /* Mark the IRQ currently in progress.*/
  413. desc->status |= IRQ_INPROGRESS;
  414. do {
  415. struct irqaction *action = desc->action;
  416. irqreturn_t action_ret;
  417. if (unlikely(!action)) {
  418. desc->chip->mask(irq);
  419. goto out_unlock;
  420. }
  421. /*
  422. * When another irq arrived while we were handling
  423. * one, we could have masked the irq.
  424. * Renable it, if it was not disabled in meantime.
  425. */
  426. if (unlikely((desc->status &
  427. (IRQ_PENDING | IRQ_MASKED | IRQ_DISABLED)) ==
  428. (IRQ_PENDING | IRQ_MASKED))) {
  429. desc->chip->unmask(irq);
  430. desc->status &= ~IRQ_MASKED;
  431. }
  432. desc->status &= ~IRQ_PENDING;
  433. spin_unlock(&desc->lock);
  434. action_ret = handle_IRQ_event(irq, action);
  435. if (!noirqdebug)
  436. note_interrupt(irq, desc, action_ret);
  437. spin_lock(&desc->lock);
  438. } while ((desc->status & (IRQ_PENDING | IRQ_DISABLED)) == IRQ_PENDING);
  439. desc->status &= ~IRQ_INPROGRESS;
  440. out_unlock:
  441. spin_unlock(&desc->lock);
  442. }
  443. /**
  444. * handle_percpu_IRQ - Per CPU local irq handler
  445. * @irq: the interrupt number
  446. * @desc: the interrupt description structure for this irq
  447. *
  448. * Per CPU interrupts on SMP machines without locking requirements
  449. */
  450. void
  451. handle_percpu_irq(unsigned int irq, struct irq_desc *desc)
  452. {
  453. irqreturn_t action_ret;
  454. kstat_this_cpu.irqs[irq]++;
  455. if (desc->chip->ack)
  456. desc->chip->ack(irq);
  457. action_ret = handle_IRQ_event(irq, desc->action);
  458. if (!noirqdebug)
  459. note_interrupt(irq, desc, action_ret);
  460. if (desc->chip->eoi)
  461. desc->chip->eoi(irq);
  462. }
  463. void
  464. __set_irq_handler(unsigned int irq, irq_flow_handler_t handle, int is_chained,
  465. const char *name)
  466. {
  467. struct irq_desc *desc;
  468. unsigned long flags;
  469. if (irq >= NR_IRQS) {
  470. printk(KERN_ERR
  471. "Trying to install type control for IRQ%d\n", irq);
  472. return;
  473. }
  474. desc = irq_desc + irq;
  475. if (!handle)
  476. handle = handle_bad_irq;
  477. else if (desc->chip == &no_irq_chip) {
  478. printk(KERN_WARNING "Trying to install %sinterrupt handler "
  479. "for IRQ%d\n", is_chained ? "chained " : "", irq);
  480. /*
  481. * Some ARM implementations install a handler for really dumb
  482. * interrupt hardware without setting an irq_chip. This worked
  483. * with the ARM no_irq_chip but the check in setup_irq would
  484. * prevent us to setup the interrupt at all. Switch it to
  485. * dummy_irq_chip for easy transition.
  486. */
  487. desc->chip = &dummy_irq_chip;
  488. }
  489. spin_lock_irqsave(&desc->lock, flags);
  490. /* Uninstall? */
  491. if (handle == handle_bad_irq) {
  492. if (desc->chip != &no_irq_chip)
  493. mask_ack_irq(desc, irq);
  494. desc->status |= IRQ_DISABLED;
  495. desc->depth = 1;
  496. }
  497. desc->handle_irq = handle;
  498. desc->name = name;
  499. if (handle != handle_bad_irq && is_chained) {
  500. desc->status &= ~IRQ_DISABLED;
  501. desc->status |= IRQ_NOREQUEST | IRQ_NOPROBE;
  502. desc->depth = 0;
  503. desc->chip->unmask(irq);
  504. }
  505. spin_unlock_irqrestore(&desc->lock, flags);
  506. }
  507. void
  508. set_irq_chip_and_handler(unsigned int irq, struct irq_chip *chip,
  509. irq_flow_handler_t handle)
  510. {
  511. set_irq_chip(irq, chip);
  512. __set_irq_handler(irq, handle, 0, NULL);
  513. }
  514. void
  515. set_irq_chip_and_handler_name(unsigned int irq, struct irq_chip *chip,
  516. irq_flow_handler_t handle, const char *name)
  517. {
  518. set_irq_chip(irq, chip);
  519. __set_irq_handler(irq, handle, 0, name);
  520. }
  521. void __init set_irq_noprobe(unsigned int irq)
  522. {
  523. struct irq_desc *desc;
  524. unsigned long flags;
  525. if (irq >= NR_IRQS) {
  526. printk(KERN_ERR "Trying to mark IRQ%d non-probeable\n", irq);
  527. return;
  528. }
  529. desc = irq_desc + irq;
  530. spin_lock_irqsave(&desc->lock, flags);
  531. desc->status |= IRQ_NOPROBE;
  532. spin_unlock_irqrestore(&desc->lock, flags);
  533. }
  534. void __init set_irq_probe(unsigned int irq)
  535. {
  536. struct irq_desc *desc;
  537. unsigned long flags;
  538. if (irq >= NR_IRQS) {
  539. printk(KERN_ERR "Trying to mark IRQ%d probeable\n", irq);
  540. return;
  541. }
  542. desc = irq_desc + irq;
  543. spin_lock_irqsave(&desc->lock, flags);
  544. desc->status &= ~IRQ_NOPROBE;
  545. spin_unlock_irqrestore(&desc->lock, flags);
  546. }