chip.c 15 KB

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