chip.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  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. desc->affinity = CPU_MASK_ALL;
  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. * Fixup enable/disable function pointers
  218. */
  219. void irq_chip_set_defaults(struct irq_chip *chip)
  220. {
  221. if (!chip->enable)
  222. chip->enable = default_enable;
  223. if (!chip->disable)
  224. chip->disable = default_disable;
  225. if (!chip->startup)
  226. chip->startup = default_startup;
  227. if (!chip->shutdown)
  228. chip->shutdown = chip->disable;
  229. if (!chip->name)
  230. chip->name = chip->typename;
  231. if (!chip->end)
  232. chip->end = dummy_irq_chip.end;
  233. }
  234. static inline void mask_ack_irq(struct irq_desc *desc, int irq)
  235. {
  236. if (desc->chip->mask_ack)
  237. desc->chip->mask_ack(irq);
  238. else {
  239. desc->chip->mask(irq);
  240. desc->chip->ack(irq);
  241. }
  242. }
  243. /**
  244. * handle_simple_irq - Simple and software-decoded IRQs.
  245. * @irq: the interrupt number
  246. * @desc: the interrupt description structure for this irq
  247. *
  248. * Simple interrupts are either sent from a demultiplexing interrupt
  249. * handler or come from hardware, where no interrupt hardware control
  250. * is necessary.
  251. *
  252. * Note: The caller is expected to handle the ack, clear, mask and
  253. * unmask issues if necessary.
  254. */
  255. void fastcall
  256. handle_simple_irq(unsigned int irq, struct irq_desc *desc)
  257. {
  258. struct irqaction *action;
  259. irqreturn_t action_ret;
  260. const unsigned int cpu = smp_processor_id();
  261. spin_lock(&desc->lock);
  262. if (unlikely(desc->status & IRQ_INPROGRESS))
  263. goto out_unlock;
  264. kstat_cpu(cpu).irqs[irq]++;
  265. action = desc->action;
  266. if (unlikely(!action || (desc->status & IRQ_DISABLED))) {
  267. if (desc->chip->mask)
  268. desc->chip->mask(irq);
  269. desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
  270. desc->status |= IRQ_PENDING;
  271. goto out_unlock;
  272. }
  273. desc->status &= ~(IRQ_REPLAY | IRQ_WAITING | IRQ_PENDING);
  274. desc->status |= IRQ_INPROGRESS;
  275. spin_unlock(&desc->lock);
  276. action_ret = handle_IRQ_event(irq, action);
  277. if (!noirqdebug)
  278. note_interrupt(irq, desc, action_ret);
  279. spin_lock(&desc->lock);
  280. desc->status &= ~IRQ_INPROGRESS;
  281. out_unlock:
  282. spin_unlock(&desc->lock);
  283. }
  284. /**
  285. * handle_level_irq - Level type irq handler
  286. * @irq: the interrupt number
  287. * @desc: the interrupt description structure for this irq
  288. *
  289. * Level type interrupts are active as long as the hardware line has
  290. * the active level. This may require to mask the interrupt and unmask
  291. * it after the associated handler has acknowledged the device, so the
  292. * interrupt line is back to inactive.
  293. */
  294. void fastcall
  295. handle_level_irq(unsigned int irq, struct irq_desc *desc)
  296. {
  297. unsigned int cpu = smp_processor_id();
  298. struct irqaction *action;
  299. irqreturn_t action_ret;
  300. spin_lock(&desc->lock);
  301. mask_ack_irq(desc, irq);
  302. if (unlikely(desc->status & IRQ_INPROGRESS))
  303. goto out_unlock;
  304. desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
  305. kstat_cpu(cpu).irqs[irq]++;
  306. /*
  307. * If its disabled or no action available
  308. * keep it masked and get out of here
  309. */
  310. action = desc->action;
  311. if (unlikely(!action || (desc->status & IRQ_DISABLED))) {
  312. desc->status |= IRQ_PENDING;
  313. goto out_unlock;
  314. }
  315. desc->status |= IRQ_INPROGRESS;
  316. desc->status &= ~IRQ_PENDING;
  317. spin_unlock(&desc->lock);
  318. action_ret = handle_IRQ_event(irq, action);
  319. if (!noirqdebug)
  320. note_interrupt(irq, desc, action_ret);
  321. spin_lock(&desc->lock);
  322. desc->status &= ~IRQ_INPROGRESS;
  323. if (!(desc->status & IRQ_DISABLED) && desc->chip->unmask)
  324. desc->chip->unmask(irq);
  325. out_unlock:
  326. spin_unlock(&desc->lock);
  327. }
  328. /**
  329. * handle_fasteoi_irq - irq handler for transparent controllers
  330. * @irq: the interrupt number
  331. * @desc: the interrupt description structure for this irq
  332. *
  333. * Only a single callback will be issued to the chip: an ->eoi()
  334. * call when the interrupt has been serviced. This enables support
  335. * for modern forms of interrupt handlers, which handle the flow
  336. * details in hardware, transparently.
  337. */
  338. void fastcall
  339. handle_fasteoi_irq(unsigned int irq, struct irq_desc *desc)
  340. {
  341. unsigned int cpu = smp_processor_id();
  342. struct irqaction *action;
  343. irqreturn_t action_ret;
  344. spin_lock(&desc->lock);
  345. if (unlikely(desc->status & IRQ_INPROGRESS))
  346. goto out;
  347. desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
  348. kstat_cpu(cpu).irqs[irq]++;
  349. /*
  350. * If its disabled or no action available
  351. * then mask it and get out of here:
  352. */
  353. action = desc->action;
  354. if (unlikely(!action || (desc->status & IRQ_DISABLED))) {
  355. desc->status |= IRQ_PENDING;
  356. if (desc->chip->mask)
  357. desc->chip->mask(irq);
  358. goto out;
  359. }
  360. desc->status |= IRQ_INPROGRESS;
  361. desc->status &= ~IRQ_PENDING;
  362. spin_unlock(&desc->lock);
  363. action_ret = handle_IRQ_event(irq, action);
  364. if (!noirqdebug)
  365. note_interrupt(irq, desc, action_ret);
  366. spin_lock(&desc->lock);
  367. desc->status &= ~IRQ_INPROGRESS;
  368. out:
  369. desc->chip->eoi(irq);
  370. spin_unlock(&desc->lock);
  371. }
  372. /**
  373. * handle_edge_irq - edge type IRQ handler
  374. * @irq: the interrupt number
  375. * @desc: the interrupt description structure for this irq
  376. *
  377. * Interrupt occures on the falling and/or rising edge of a hardware
  378. * signal. The occurence is latched into the irq controller hardware
  379. * and must be acked in order to be reenabled. After the ack another
  380. * interrupt can happen on the same source even before the first one
  381. * is handled by the assosiacted event handler. If this happens it
  382. * might be necessary to disable (mask) the interrupt depending on the
  383. * controller hardware. This requires to reenable the interrupt inside
  384. * of the loop which handles the interrupts which have arrived while
  385. * the handler was running. If all pending interrupts are handled, the
  386. * loop is left.
  387. */
  388. void fastcall
  389. handle_edge_irq(unsigned int irq, struct irq_desc *desc)
  390. {
  391. const unsigned int cpu = smp_processor_id();
  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_cpu(cpu).irqs[irq]++;
  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. #ifdef CONFIG_SMP
  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 fastcall
  448. handle_percpu_irq(unsigned int irq, struct irq_desc *desc)
  449. {
  450. irqreturn_t action_ret;
  451. kstat_this_cpu.irqs[irq]++;
  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. #endif /* CONFIG_SMP */
  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;
  466. unsigned long flags;
  467. if (irq >= NR_IRQS) {
  468. printk(KERN_ERR
  469. "Trying to install type control for IRQ%d\n", irq);
  470. return;
  471. }
  472. desc = irq_desc + irq;
  473. if (!handle)
  474. handle = handle_bad_irq;
  475. else if (desc->chip == &no_irq_chip) {
  476. printk(KERN_WARNING "Trying to install %sinterrupt handler "
  477. "for IRQ%d\n", is_chained ? "chained " : "", irq);
  478. /*
  479. * Some ARM implementations install a handler for really dumb
  480. * interrupt hardware without setting an irq_chip. This worked
  481. * with the ARM no_irq_chip but the check in setup_irq would
  482. * prevent us to setup the interrupt at all. Switch it to
  483. * dummy_irq_chip for easy transition.
  484. */
  485. desc->chip = &dummy_irq_chip;
  486. }
  487. spin_lock_irqsave(&desc->lock, flags);
  488. /* Uninstall? */
  489. if (handle == handle_bad_irq) {
  490. if (desc->chip != &no_irq_chip)
  491. mask_ack_irq(desc, irq);
  492. desc->status |= IRQ_DISABLED;
  493. desc->depth = 1;
  494. }
  495. desc->handle_irq = handle;
  496. desc->name = name;
  497. if (handle != handle_bad_irq && is_chained) {
  498. desc->status &= ~IRQ_DISABLED;
  499. desc->status |= IRQ_NOREQUEST | IRQ_NOPROBE;
  500. desc->depth = 0;
  501. desc->chip->unmask(irq);
  502. }
  503. spin_unlock_irqrestore(&desc->lock, flags);
  504. }
  505. void
  506. set_irq_chip_and_handler(unsigned int irq, struct irq_chip *chip,
  507. irq_flow_handler_t handle)
  508. {
  509. set_irq_chip(irq, chip);
  510. __set_irq_handler(irq, handle, 0, NULL);
  511. }
  512. void
  513. set_irq_chip_and_handler_name(unsigned int irq, struct irq_chip *chip,
  514. irq_flow_handler_t handle, const char *name)
  515. {
  516. set_irq_chip(irq, chip);
  517. __set_irq_handler(irq, handle, 0, name);
  518. }