chip.c 15 KB

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