chip.c 15 KB

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