chip.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  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. if (irq >= NR_IRQS) {
  27. printk(KERN_ERR "Trying to initialize invalid IRQ%d\n", irq);
  28. WARN_ON(1);
  29. return;
  30. }
  31. /* Ensure we don't have left over values from a previous use of this irq */
  32. desc = irq_desc + irq;
  33. spin_lock_irqsave(&desc->lock, flags);
  34. desc->status = IRQ_DISABLED;
  35. desc->chip = &no_irq_chip;
  36. desc->handle_irq = handle_bad_irq;
  37. desc->depth = 1;
  38. desc->msi_desc = NULL;
  39. desc->handler_data = NULL;
  40. desc->chip_data = NULL;
  41. desc->action = NULL;
  42. desc->irq_count = 0;
  43. desc->irqs_unhandled = 0;
  44. #ifdef CONFIG_SMP
  45. cpus_setall(desc->affinity);
  46. #endif
  47. spin_unlock_irqrestore(&desc->lock, flags);
  48. }
  49. /**
  50. * dynamic_irq_cleanup - cleanup a dynamically allocated irq
  51. * @irq: irq number to initialize
  52. */
  53. void dynamic_irq_cleanup(unsigned int irq)
  54. {
  55. struct irq_desc *desc;
  56. unsigned long flags;
  57. if (irq >= NR_IRQS) {
  58. printk(KERN_ERR "Trying to cleanup invalid IRQ%d\n", irq);
  59. WARN_ON(1);
  60. return;
  61. }
  62. desc = irq_desc + irq;
  63. spin_lock_irqsave(&desc->lock, flags);
  64. if (desc->action) {
  65. spin_unlock_irqrestore(&desc->lock, flags);
  66. printk(KERN_ERR "Destroying IRQ%d without calling free_irq\n",
  67. irq);
  68. WARN_ON(1);
  69. return;
  70. }
  71. desc->msi_desc = NULL;
  72. desc->handler_data = NULL;
  73. desc->chip_data = NULL;
  74. desc->handle_irq = handle_bad_irq;
  75. desc->chip = &no_irq_chip;
  76. spin_unlock_irqrestore(&desc->lock, flags);
  77. }
  78. /**
  79. * set_irq_chip - set the irq chip for an irq
  80. * @irq: irq number
  81. * @chip: pointer to irq chip description structure
  82. */
  83. int set_irq_chip(unsigned int irq, struct irq_chip *chip)
  84. {
  85. struct irq_desc *desc;
  86. unsigned long flags;
  87. if (irq >= NR_IRQS) {
  88. printk(KERN_ERR "Trying to install chip for IRQ%d\n", irq);
  89. WARN_ON(1);
  90. return -EINVAL;
  91. }
  92. if (!chip)
  93. chip = &no_irq_chip;
  94. desc = irq_desc + irq;
  95. spin_lock_irqsave(&desc->lock, flags);
  96. irq_chip_set_defaults(chip);
  97. desc->chip = chip;
  98. spin_unlock_irqrestore(&desc->lock, flags);
  99. return 0;
  100. }
  101. EXPORT_SYMBOL(set_irq_chip);
  102. /**
  103. * set_irq_type - set the irq type for an irq
  104. * @irq: irq number
  105. * @type: interrupt type - see include/linux/interrupt.h
  106. */
  107. int set_irq_type(unsigned int irq, unsigned int type)
  108. {
  109. struct irq_desc *desc;
  110. unsigned long flags;
  111. int ret = -ENXIO;
  112. if (irq >= NR_IRQS) {
  113. printk(KERN_ERR "Trying to set irq type for IRQ%d\n", irq);
  114. return -ENODEV;
  115. }
  116. desc = irq_desc + irq;
  117. if (desc->chip->set_type) {
  118. spin_lock_irqsave(&desc->lock, flags);
  119. ret = desc->chip->set_type(irq, type);
  120. spin_unlock_irqrestore(&desc->lock, flags);
  121. }
  122. return ret;
  123. }
  124. EXPORT_SYMBOL(set_irq_type);
  125. /**
  126. * set_irq_data - set irq type data for an irq
  127. * @irq: Interrupt number
  128. * @data: Pointer to interrupt specific data
  129. *
  130. * Set the hardware irq controller data for an irq
  131. */
  132. int set_irq_data(unsigned int irq, void *data)
  133. {
  134. struct irq_desc *desc;
  135. unsigned long flags;
  136. if (irq >= NR_IRQS) {
  137. printk(KERN_ERR
  138. "Trying to install controller data for IRQ%d\n", irq);
  139. return -EINVAL;
  140. }
  141. desc = irq_desc + irq;
  142. spin_lock_irqsave(&desc->lock, flags);
  143. desc->handler_data = data;
  144. spin_unlock_irqrestore(&desc->lock, flags);
  145. return 0;
  146. }
  147. EXPORT_SYMBOL(set_irq_data);
  148. /**
  149. * set_irq_data - set irq type data for an irq
  150. * @irq: Interrupt number
  151. * @entry: Pointer to MSI descriptor data
  152. *
  153. * Set the hardware irq controller data for an irq
  154. */
  155. int set_irq_msi(unsigned int irq, struct msi_desc *entry)
  156. {
  157. struct irq_desc *desc;
  158. unsigned long flags;
  159. if (irq >= NR_IRQS) {
  160. printk(KERN_ERR
  161. "Trying to install msi data for IRQ%d\n", irq);
  162. return -EINVAL;
  163. }
  164. desc = irq_desc + irq;
  165. spin_lock_irqsave(&desc->lock, flags);
  166. desc->msi_desc = entry;
  167. if (entry)
  168. entry->irq = irq;
  169. spin_unlock_irqrestore(&desc->lock, flags);
  170. return 0;
  171. }
  172. /**
  173. * set_irq_chip_data - set irq chip data for an irq
  174. * @irq: Interrupt number
  175. * @data: Pointer to chip specific data
  176. *
  177. * Set the hardware irq chip data for an irq
  178. */
  179. int set_irq_chip_data(unsigned int irq, void *data)
  180. {
  181. struct irq_desc *desc = irq_desc + irq;
  182. unsigned long flags;
  183. if (irq >= NR_IRQS || !desc->chip) {
  184. printk(KERN_ERR "BUG: bad set_irq_chip_data(IRQ#%d)\n", irq);
  185. return -EINVAL;
  186. }
  187. spin_lock_irqsave(&desc->lock, flags);
  188. desc->chip_data = data;
  189. spin_unlock_irqrestore(&desc->lock, flags);
  190. return 0;
  191. }
  192. EXPORT_SYMBOL(set_irq_chip_data);
  193. /*
  194. * default enable function
  195. */
  196. static void default_enable(unsigned int irq)
  197. {
  198. struct irq_desc *desc = irq_desc + irq;
  199. desc->chip->unmask(irq);
  200. desc->status &= ~IRQ_MASKED;
  201. }
  202. /*
  203. * default disable function
  204. */
  205. static void default_disable(unsigned int irq)
  206. {
  207. }
  208. /*
  209. * default startup function
  210. */
  211. static unsigned int default_startup(unsigned int irq)
  212. {
  213. irq_desc[irq].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_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. desc->chip->ack(irq);
  257. }
  258. }
  259. /**
  260. * handle_simple_irq - Simple and software-decoded IRQs.
  261. * @irq: the interrupt number
  262. * @desc: the interrupt description structure for this irq
  263. *
  264. * Simple interrupts are either sent from a demultiplexing interrupt
  265. * handler or come from hardware, where no interrupt hardware control
  266. * is necessary.
  267. *
  268. * Note: The caller is expected to handle the ack, clear, mask and
  269. * unmask issues if necessary.
  270. */
  271. void
  272. handle_simple_irq(unsigned int irq, struct irq_desc *desc)
  273. {
  274. struct irqaction *action;
  275. irqreturn_t action_ret;
  276. const unsigned int cpu = smp_processor_id();
  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_cpu(cpu).irqs[irq]++;
  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. unsigned int cpu = smp_processor_id();
  309. struct irqaction *action;
  310. irqreturn_t action_ret;
  311. spin_lock(&desc->lock);
  312. mask_ack_irq(desc, irq);
  313. if (unlikely(desc->status & IRQ_INPROGRESS))
  314. goto out_unlock;
  315. desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
  316. kstat_cpu(cpu).irqs[irq]++;
  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. /**
  337. * handle_fasteoi_irq - irq handler for transparent controllers
  338. * @irq: the interrupt number
  339. * @desc: the interrupt description structure for this irq
  340. *
  341. * Only a single callback will be issued to the chip: an ->eoi()
  342. * call when the interrupt has been serviced. This enables support
  343. * for modern forms of interrupt handlers, which handle the flow
  344. * details in hardware, transparently.
  345. */
  346. void
  347. handle_fasteoi_irq(unsigned int irq, struct irq_desc *desc)
  348. {
  349. unsigned int cpu = smp_processor_id();
  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_cpu(cpu).irqs[irq]++;
  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. spin_unlock(&desc->lock);
  379. }
  380. /**
  381. * handle_edge_irq - edge type IRQ handler
  382. * @irq: the interrupt number
  383. * @desc: the interrupt description structure for this irq
  384. *
  385. * Interrupt occures on the falling and/or rising edge of a hardware
  386. * signal. The occurence is latched into the irq controller hardware
  387. * and must be acked in order to be reenabled. After the ack another
  388. * interrupt can happen on the same source even before the first one
  389. * is handled by the assosiacted event handler. If this happens it
  390. * might be necessary to disable (mask) the interrupt depending on the
  391. * controller hardware. This requires to reenable the interrupt inside
  392. * of the loop which handles the interrupts which have arrived while
  393. * the handler was running. If all pending interrupts are handled, the
  394. * loop is left.
  395. */
  396. void
  397. handle_edge_irq(unsigned int irq, struct irq_desc *desc)
  398. {
  399. const unsigned int cpu = smp_processor_id();
  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. goto out_unlock;
  412. }
  413. kstat_cpu(cpu).irqs[irq]++;
  414. /* Start handling the irq */
  415. desc->chip->ack(irq);
  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_this_cpu.irqs[irq]++;
  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. }
  467. void
  468. __set_irq_handler(unsigned int irq, irq_flow_handler_t handle, int is_chained,
  469. const char *name)
  470. {
  471. struct irq_desc *desc;
  472. unsigned long flags;
  473. if (irq >= NR_IRQS) {
  474. printk(KERN_ERR
  475. "Trying to install type control for IRQ%d\n", irq);
  476. return;
  477. }
  478. desc = irq_desc + irq;
  479. if (!handle)
  480. handle = handle_bad_irq;
  481. else if (desc->chip == &no_irq_chip) {
  482. printk(KERN_WARNING "Trying to install %sinterrupt handler "
  483. "for IRQ%d\n", is_chained ? "chained " : "", irq);
  484. /*
  485. * Some ARM implementations install a handler for really dumb
  486. * interrupt hardware without setting an irq_chip. This worked
  487. * with the ARM no_irq_chip but the check in setup_irq would
  488. * prevent us to setup the interrupt at all. Switch it to
  489. * dummy_irq_chip for easy transition.
  490. */
  491. desc->chip = &dummy_irq_chip;
  492. }
  493. spin_lock_irqsave(&desc->lock, flags);
  494. /* Uninstall? */
  495. if (handle == handle_bad_irq) {
  496. if (desc->chip != &no_irq_chip)
  497. mask_ack_irq(desc, irq);
  498. desc->status |= IRQ_DISABLED;
  499. desc->depth = 1;
  500. }
  501. desc->handle_irq = handle;
  502. desc->name = name;
  503. if (handle != handle_bad_irq && is_chained) {
  504. desc->status &= ~IRQ_DISABLED;
  505. desc->status |= IRQ_NOREQUEST | IRQ_NOPROBE;
  506. desc->depth = 0;
  507. desc->chip->unmask(irq);
  508. }
  509. spin_unlock_irqrestore(&desc->lock, flags);
  510. }
  511. void
  512. set_irq_chip_and_handler(unsigned int irq, struct irq_chip *chip,
  513. irq_flow_handler_t handle)
  514. {
  515. set_irq_chip(irq, chip);
  516. __set_irq_handler(irq, handle, 0, NULL);
  517. }
  518. void
  519. set_irq_chip_and_handler_name(unsigned int irq, struct irq_chip *chip,
  520. irq_flow_handler_t handle, const char *name)
  521. {
  522. set_irq_chip(irq, chip);
  523. __set_irq_handler(irq, handle, 0, name);
  524. }
  525. void __init set_irq_noprobe(unsigned int irq)
  526. {
  527. struct irq_desc *desc;
  528. unsigned long flags;
  529. if (irq >= NR_IRQS) {
  530. printk(KERN_ERR "Trying to mark IRQ%d non-probeable\n", irq);
  531. return;
  532. }
  533. desc = irq_desc + irq;
  534. spin_lock_irqsave(&desc->lock, flags);
  535. desc->status |= IRQ_NOPROBE;
  536. spin_unlock_irqrestore(&desc->lock, flags);
  537. }
  538. void __init set_irq_probe(unsigned int irq)
  539. {
  540. struct irq_desc *desc;
  541. unsigned long flags;
  542. if (irq >= NR_IRQS) {
  543. printk(KERN_ERR "Trying to mark IRQ%d probeable\n", irq);
  544. return;
  545. }
  546. desc = irq_desc + irq;
  547. spin_lock_irqsave(&desc->lock, flags);
  548. desc->status &= ~IRQ_NOPROBE;
  549. spin_unlock_irqrestore(&desc->lock, flags);
  550. }