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