chip.c 15 KB

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