chip.c 15 KB

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