chip.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  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. goto out_unlock;
  313. desc->status |= IRQ_INPROGRESS;
  314. spin_unlock(&desc->lock);
  315. action_ret = handle_IRQ_event(irq, action);
  316. if (!noirqdebug)
  317. note_interrupt(irq, desc, action_ret);
  318. spin_lock(&desc->lock);
  319. desc->status &= ~IRQ_INPROGRESS;
  320. if (!(desc->status & IRQ_DISABLED) && desc->chip->unmask)
  321. desc->chip->unmask(irq);
  322. out_unlock:
  323. spin_unlock(&desc->lock);
  324. }
  325. /**
  326. * handle_fasteoi_irq - irq handler for transparent controllers
  327. * @irq: the interrupt number
  328. * @desc: the interrupt description structure for this irq
  329. *
  330. * Only a single callback will be issued to the chip: an ->eoi()
  331. * call when the interrupt has been serviced. This enables support
  332. * for modern forms of interrupt handlers, which handle the flow
  333. * details in hardware, transparently.
  334. */
  335. void fastcall
  336. handle_fasteoi_irq(unsigned int irq, struct irq_desc *desc)
  337. {
  338. unsigned int cpu = smp_processor_id();
  339. struct irqaction *action;
  340. irqreturn_t action_ret;
  341. spin_lock(&desc->lock);
  342. if (unlikely(desc->status & IRQ_INPROGRESS))
  343. goto out;
  344. desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
  345. kstat_cpu(cpu).irqs[irq]++;
  346. /*
  347. * If its disabled or no action available
  348. * then mask it and get out of here:
  349. */
  350. action = desc->action;
  351. if (unlikely(!action || (desc->status & IRQ_DISABLED))) {
  352. desc->status |= IRQ_PENDING;
  353. if (desc->chip->mask)
  354. desc->chip->mask(irq);
  355. goto out;
  356. }
  357. desc->status |= IRQ_INPROGRESS;
  358. desc->status &= ~IRQ_PENDING;
  359. spin_unlock(&desc->lock);
  360. action_ret = handle_IRQ_event(irq, action);
  361. if (!noirqdebug)
  362. note_interrupt(irq, desc, action_ret);
  363. spin_lock(&desc->lock);
  364. desc->status &= ~IRQ_INPROGRESS;
  365. out:
  366. desc->chip->eoi(irq);
  367. spin_unlock(&desc->lock);
  368. }
  369. /**
  370. * handle_edge_irq - edge type IRQ handler
  371. * @irq: the interrupt number
  372. * @desc: the interrupt description structure for this irq
  373. *
  374. * Interrupt occures on the falling and/or rising edge of a hardware
  375. * signal. The occurence is latched into the irq controller hardware
  376. * and must be acked in order to be reenabled. After the ack another
  377. * interrupt can happen on the same source even before the first one
  378. * is handled by the assosiacted event handler. If this happens it
  379. * might be necessary to disable (mask) the interrupt depending on the
  380. * controller hardware. This requires to reenable the interrupt inside
  381. * of the loop which handles the interrupts which have arrived while
  382. * the handler was running. If all pending interrupts are handled, the
  383. * loop is left.
  384. */
  385. void fastcall
  386. handle_edge_irq(unsigned int irq, struct irq_desc *desc)
  387. {
  388. const unsigned int cpu = smp_processor_id();
  389. spin_lock(&desc->lock);
  390. desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
  391. /*
  392. * If we're currently running this IRQ, or its disabled,
  393. * we shouldn't process the IRQ. Mark it pending, handle
  394. * the necessary masking and go out
  395. */
  396. if (unlikely((desc->status & (IRQ_INPROGRESS | IRQ_DISABLED)) ||
  397. !desc->action)) {
  398. desc->status |= (IRQ_PENDING | IRQ_MASKED);
  399. mask_ack_irq(desc, irq);
  400. goto out_unlock;
  401. }
  402. kstat_cpu(cpu).irqs[irq]++;
  403. /* Start handling the irq */
  404. desc->chip->ack(irq);
  405. /* Mark the IRQ currently in progress.*/
  406. desc->status |= IRQ_INPROGRESS;
  407. do {
  408. struct irqaction *action = desc->action;
  409. irqreturn_t action_ret;
  410. if (unlikely(!action)) {
  411. desc->chip->mask(irq);
  412. goto out_unlock;
  413. }
  414. /*
  415. * When another irq arrived while we were handling
  416. * one, we could have masked the irq.
  417. * Renable it, if it was not disabled in meantime.
  418. */
  419. if (unlikely((desc->status &
  420. (IRQ_PENDING | IRQ_MASKED | IRQ_DISABLED)) ==
  421. (IRQ_PENDING | IRQ_MASKED))) {
  422. desc->chip->unmask(irq);
  423. desc->status &= ~IRQ_MASKED;
  424. }
  425. desc->status &= ~IRQ_PENDING;
  426. spin_unlock(&desc->lock);
  427. action_ret = handle_IRQ_event(irq, action);
  428. if (!noirqdebug)
  429. note_interrupt(irq, desc, action_ret);
  430. spin_lock(&desc->lock);
  431. } while ((desc->status & (IRQ_PENDING | IRQ_DISABLED)) == IRQ_PENDING);
  432. desc->status &= ~IRQ_INPROGRESS;
  433. out_unlock:
  434. spin_unlock(&desc->lock);
  435. }
  436. #ifdef CONFIG_SMP
  437. /**
  438. * handle_percpu_IRQ - Per CPU local irq handler
  439. * @irq: the interrupt number
  440. * @desc: the interrupt description structure for this irq
  441. *
  442. * Per CPU interrupts on SMP machines without locking requirements
  443. */
  444. void fastcall
  445. handle_percpu_irq(unsigned int irq, struct irq_desc *desc)
  446. {
  447. irqreturn_t action_ret;
  448. kstat_this_cpu.irqs[irq]++;
  449. if (desc->chip->ack)
  450. desc->chip->ack(irq);
  451. action_ret = handle_IRQ_event(irq, desc->action);
  452. if (!noirqdebug)
  453. note_interrupt(irq, desc, action_ret);
  454. if (desc->chip->eoi)
  455. desc->chip->eoi(irq);
  456. }
  457. #endif /* CONFIG_SMP */
  458. void
  459. __set_irq_handler(unsigned int irq, irq_flow_handler_t handle, int is_chained,
  460. const char *name)
  461. {
  462. struct irq_desc *desc;
  463. unsigned long flags;
  464. if (irq >= NR_IRQS) {
  465. printk(KERN_ERR
  466. "Trying to install type control for IRQ%d\n", irq);
  467. return;
  468. }
  469. desc = irq_desc + irq;
  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->unmask(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. }