chip.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  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. EXPORT_SYMBOL_GPL(handle_level_irq);
  335. /**
  336. * handle_fasteoi_irq - irq handler for transparent controllers
  337. * @irq: the interrupt number
  338. * @desc: the interrupt description structure for this irq
  339. *
  340. * Only a single callback will be issued to the chip: an ->eoi()
  341. * call when the interrupt has been serviced. This enables support
  342. * for modern forms of interrupt handlers, which handle the flow
  343. * details in hardware, transparently.
  344. */
  345. void
  346. handle_fasteoi_irq(unsigned int irq, struct irq_desc *desc)
  347. {
  348. struct irqaction *action;
  349. irqreturn_t action_ret;
  350. spin_lock(&desc->lock);
  351. if (unlikely(desc->status & IRQ_INPROGRESS))
  352. goto out;
  353. desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
  354. kstat_incr_irqs_this_cpu(irq, desc);
  355. /*
  356. * If its disabled or no action available
  357. * then mask it and get out of here:
  358. */
  359. action = desc->action;
  360. if (unlikely(!action || (desc->status & IRQ_DISABLED))) {
  361. desc->status |= IRQ_PENDING;
  362. if (desc->chip->mask)
  363. desc->chip->mask(irq);
  364. goto out;
  365. }
  366. desc->status |= IRQ_INPROGRESS;
  367. desc->status &= ~IRQ_PENDING;
  368. spin_unlock(&desc->lock);
  369. action_ret = handle_IRQ_event(irq, action);
  370. if (!noirqdebug)
  371. note_interrupt(irq, desc, action_ret);
  372. spin_lock(&desc->lock);
  373. desc->status &= ~IRQ_INPROGRESS;
  374. out:
  375. desc->chip->eoi(irq);
  376. desc = irq_remap_to_desc(irq, desc);
  377. spin_unlock(&desc->lock);
  378. }
  379. /**
  380. * handle_edge_irq - edge type IRQ handler
  381. * @irq: the interrupt number
  382. * @desc: the interrupt description structure for this irq
  383. *
  384. * Interrupt occures on the falling and/or rising edge of a hardware
  385. * signal. The occurence is latched into the irq controller hardware
  386. * and must be acked in order to be reenabled. After the ack another
  387. * interrupt can happen on the same source even before the first one
  388. * is handled by the assosiacted event handler. If this happens it
  389. * might be necessary to disable (mask) the interrupt depending on the
  390. * controller hardware. This requires to reenable the interrupt inside
  391. * of the loop which handles the interrupts which have arrived while
  392. * the handler was running. If all pending interrupts are handled, the
  393. * loop is left.
  394. */
  395. void
  396. handle_edge_irq(unsigned int irq, struct irq_desc *desc)
  397. {
  398. spin_lock(&desc->lock);
  399. desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
  400. /*
  401. * If we're currently running this IRQ, or its disabled,
  402. * we shouldn't process the IRQ. Mark it pending, handle
  403. * the necessary masking and go out
  404. */
  405. if (unlikely((desc->status & (IRQ_INPROGRESS | IRQ_DISABLED)) ||
  406. !desc->action)) {
  407. desc->status |= (IRQ_PENDING | IRQ_MASKED);
  408. mask_ack_irq(desc, irq);
  409. desc = irq_remap_to_desc(irq, desc);
  410. goto out_unlock;
  411. }
  412. kstat_incr_irqs_this_cpu(irq, desc);
  413. /* Start handling the irq */
  414. desc->chip->ack(irq);
  415. desc = irq_remap_to_desc(irq, desc);
  416. /* Mark the IRQ currently in progress.*/
  417. desc->status |= IRQ_INPROGRESS;
  418. do {
  419. struct irqaction *action = desc->action;
  420. irqreturn_t action_ret;
  421. if (unlikely(!action)) {
  422. desc->chip->mask(irq);
  423. goto out_unlock;
  424. }
  425. /*
  426. * When another irq arrived while we were handling
  427. * one, we could have masked the irq.
  428. * Renable it, if it was not disabled in meantime.
  429. */
  430. if (unlikely((desc->status &
  431. (IRQ_PENDING | IRQ_MASKED | IRQ_DISABLED)) ==
  432. (IRQ_PENDING | IRQ_MASKED))) {
  433. desc->chip->unmask(irq);
  434. desc->status &= ~IRQ_MASKED;
  435. }
  436. desc->status &= ~IRQ_PENDING;
  437. spin_unlock(&desc->lock);
  438. action_ret = handle_IRQ_event(irq, action);
  439. if (!noirqdebug)
  440. note_interrupt(irq, desc, action_ret);
  441. spin_lock(&desc->lock);
  442. } while ((desc->status & (IRQ_PENDING | IRQ_DISABLED)) == IRQ_PENDING);
  443. desc->status &= ~IRQ_INPROGRESS;
  444. out_unlock:
  445. spin_unlock(&desc->lock);
  446. }
  447. /**
  448. * handle_percpu_IRQ - Per CPU local irq handler
  449. * @irq: the interrupt number
  450. * @desc: the interrupt description structure for this irq
  451. *
  452. * Per CPU interrupts on SMP machines without locking requirements
  453. */
  454. void
  455. handle_percpu_irq(unsigned int irq, struct irq_desc *desc)
  456. {
  457. irqreturn_t action_ret;
  458. kstat_incr_irqs_this_cpu(irq, desc);
  459. if (desc->chip->ack)
  460. desc->chip->ack(irq);
  461. action_ret = handle_IRQ_event(irq, desc->action);
  462. if (!noirqdebug)
  463. note_interrupt(irq, desc, action_ret);
  464. if (desc->chip->eoi) {
  465. desc->chip->eoi(irq);
  466. desc = irq_remap_to_desc(irq, desc);
  467. }
  468. }
  469. void
  470. __set_irq_handler(unsigned int irq, irq_flow_handler_t handle, int is_chained,
  471. const char *name)
  472. {
  473. struct irq_desc *desc = irq_to_desc(irq);
  474. unsigned long flags;
  475. if (!desc) {
  476. printk(KERN_ERR
  477. "Trying to install type control for IRQ%d\n", irq);
  478. return;
  479. }
  480. if (!handle)
  481. handle = handle_bad_irq;
  482. else if (desc->chip == &no_irq_chip) {
  483. printk(KERN_WARNING "Trying to install %sinterrupt handler "
  484. "for IRQ%d\n", is_chained ? "chained " : "", irq);
  485. /*
  486. * Some ARM implementations install a handler for really dumb
  487. * interrupt hardware without setting an irq_chip. This worked
  488. * with the ARM no_irq_chip but the check in setup_irq would
  489. * prevent us to setup the interrupt at all. Switch it to
  490. * dummy_irq_chip for easy transition.
  491. */
  492. desc->chip = &dummy_irq_chip;
  493. }
  494. spin_lock_irqsave(&desc->lock, flags);
  495. /* Uninstall? */
  496. if (handle == handle_bad_irq) {
  497. if (desc->chip != &no_irq_chip) {
  498. mask_ack_irq(desc, irq);
  499. desc = irq_remap_to_desc(irq, desc);
  500. }
  501. desc->status |= IRQ_DISABLED;
  502. desc->depth = 1;
  503. }
  504. desc->handle_irq = handle;
  505. desc->name = name;
  506. if (handle != handle_bad_irq && is_chained) {
  507. desc->status &= ~IRQ_DISABLED;
  508. desc->status |= IRQ_NOREQUEST | IRQ_NOPROBE;
  509. desc->depth = 0;
  510. desc->chip->startup(irq);
  511. }
  512. spin_unlock_irqrestore(&desc->lock, flags);
  513. }
  514. EXPORT_SYMBOL_GPL(__set_irq_handler);
  515. void
  516. set_irq_chip_and_handler(unsigned int irq, struct irq_chip *chip,
  517. irq_flow_handler_t handle)
  518. {
  519. set_irq_chip(irq, chip);
  520. __set_irq_handler(irq, handle, 0, NULL);
  521. }
  522. void
  523. set_irq_chip_and_handler_name(unsigned int irq, struct irq_chip *chip,
  524. irq_flow_handler_t handle, const char *name)
  525. {
  526. set_irq_chip(irq, chip);
  527. __set_irq_handler(irq, handle, 0, name);
  528. }
  529. void __init set_irq_noprobe(unsigned int irq)
  530. {
  531. struct irq_desc *desc = irq_to_desc(irq);
  532. unsigned long flags;
  533. if (!desc) {
  534. printk(KERN_ERR "Trying to mark IRQ%d non-probeable\n", irq);
  535. return;
  536. }
  537. spin_lock_irqsave(&desc->lock, flags);
  538. desc->status |= IRQ_NOPROBE;
  539. spin_unlock_irqrestore(&desc->lock, flags);
  540. }
  541. void __init set_irq_probe(unsigned int irq)
  542. {
  543. struct irq_desc *desc = irq_to_desc(irq);
  544. unsigned long flags;
  545. if (!desc) {
  546. printk(KERN_ERR "Trying to mark IRQ%d probeable\n", irq);
  547. return;
  548. }
  549. spin_lock_irqsave(&desc->lock, flags);
  550. desc->status &= ~IRQ_NOPROBE;
  551. spin_unlock_irqrestore(&desc->lock, flags);
  552. }