chip.c 15 KB

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