chip.c 15 KB

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