chip.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  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/module.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/kernel_stat.h>
  16. #include "internals.h"
  17. /**
  18. * dynamic_irq_init - initialize a dynamically allocated irq
  19. * @irq: irq number to initialize
  20. */
  21. void dynamic_irq_init(unsigned int irq)
  22. {
  23. struct irq_desc *desc;
  24. unsigned long flags;
  25. if (irq >= NR_IRQS) {
  26. printk(KERN_ERR "Trying to initialize invalid IRQ%d\n", irq);
  27. WARN_ON(1);
  28. return;
  29. }
  30. /* Ensure we don't have left over values from a previous use of this irq */
  31. desc = irq_desc + 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. desc->affinity = CPU_MASK_ALL;
  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;
  55. unsigned long flags;
  56. if (irq >= NR_IRQS) {
  57. printk(KERN_ERR "Trying to cleanup invalid IRQ%d\n", irq);
  58. WARN_ON(1);
  59. return;
  60. }
  61. desc = irq_desc + irq;
  62. spin_lock_irqsave(&desc->lock, flags);
  63. if (desc->action) {
  64. spin_unlock_irqrestore(&desc->lock, flags);
  65. printk(KERN_ERR "Destroying IRQ%d without calling free_irq\n",
  66. irq);
  67. WARN_ON(1);
  68. return;
  69. }
  70. desc->msi_desc = NULL;
  71. desc->handler_data = NULL;
  72. desc->chip_data = NULL;
  73. desc->handle_irq = handle_bad_irq;
  74. desc->chip = &no_irq_chip;
  75. spin_unlock_irqrestore(&desc->lock, flags);
  76. }
  77. /**
  78. * set_irq_chip - set the irq chip for an irq
  79. * @irq: irq number
  80. * @chip: pointer to irq chip description structure
  81. */
  82. int set_irq_chip(unsigned int irq, struct irq_chip *chip)
  83. {
  84. struct irq_desc *desc;
  85. unsigned long flags;
  86. if (irq >= NR_IRQS) {
  87. printk(KERN_ERR "Trying to install chip for IRQ%d\n", irq);
  88. WARN_ON(1);
  89. return -EINVAL;
  90. }
  91. if (!chip)
  92. chip = &no_irq_chip;
  93. desc = irq_desc + irq;
  94. spin_lock_irqsave(&desc->lock, flags);
  95. irq_chip_set_defaults(chip);
  96. desc->chip = chip;
  97. spin_unlock_irqrestore(&desc->lock, flags);
  98. return 0;
  99. }
  100. EXPORT_SYMBOL(set_irq_chip);
  101. /**
  102. * set_irq_type - set the irq type for an irq
  103. * @irq: irq number
  104. * @type: interrupt type - see include/linux/interrupt.h
  105. */
  106. int set_irq_type(unsigned int irq, unsigned int type)
  107. {
  108. struct irq_desc *desc;
  109. unsigned long flags;
  110. int ret = -ENXIO;
  111. if (irq >= NR_IRQS) {
  112. printk(KERN_ERR "Trying to set irq type for IRQ%d\n", irq);
  113. return -ENODEV;
  114. }
  115. desc = irq_desc + irq;
  116. if (desc->chip->set_type) {
  117. spin_lock_irqsave(&desc->lock, flags);
  118. ret = desc->chip->set_type(irq, type);
  119. spin_unlock_irqrestore(&desc->lock, flags);
  120. }
  121. return ret;
  122. }
  123. EXPORT_SYMBOL(set_irq_type);
  124. /**
  125. * set_irq_data - set irq type data for an irq
  126. * @irq: Interrupt number
  127. * @data: Pointer to interrupt specific data
  128. *
  129. * Set the hardware irq controller data for an irq
  130. */
  131. int set_irq_data(unsigned int irq, void *data)
  132. {
  133. struct irq_desc *desc;
  134. unsigned long flags;
  135. if (irq >= NR_IRQS) {
  136. printk(KERN_ERR
  137. "Trying to install controller data for IRQ%d\n", irq);
  138. return -EINVAL;
  139. }
  140. desc = irq_desc + irq;
  141. spin_lock_irqsave(&desc->lock, flags);
  142. desc->handler_data = data;
  143. spin_unlock_irqrestore(&desc->lock, flags);
  144. return 0;
  145. }
  146. EXPORT_SYMBOL(set_irq_data);
  147. /**
  148. * set_irq_data - set irq type data for an irq
  149. * @irq: Interrupt number
  150. * @data: Pointer to interrupt specific data
  151. *
  152. * Set the hardware irq controller data for an irq
  153. */
  154. int set_irq_msi(unsigned int irq, struct msi_desc *entry)
  155. {
  156. struct irq_desc *desc;
  157. unsigned long flags;
  158. if (irq >= NR_IRQS) {
  159. printk(KERN_ERR
  160. "Trying to install msi data for IRQ%d\n", irq);
  161. return -EINVAL;
  162. }
  163. desc = irq_desc + irq;
  164. spin_lock_irqsave(&desc->lock, flags);
  165. desc->msi_desc = entry;
  166. spin_unlock_irqrestore(&desc->lock, flags);
  167. return 0;
  168. }
  169. /**
  170. * set_irq_chip_data - set irq chip data for an irq
  171. * @irq: Interrupt number
  172. * @data: Pointer to chip specific data
  173. *
  174. * Set the hardware irq chip data for an irq
  175. */
  176. int set_irq_chip_data(unsigned int irq, void *data)
  177. {
  178. struct irq_desc *desc = irq_desc + irq;
  179. unsigned long flags;
  180. if (irq >= NR_IRQS || !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_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. struct irq_desc *desc = irq_desc + irq;
  205. if (!(desc->status & IRQ_DELAYED_DISABLE))
  206. desc->chip->mask(irq);
  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. desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
  265. kstat_cpu(cpu).irqs[irq]++;
  266. action = desc->action;
  267. if (unlikely(!action || (desc->status & IRQ_DISABLED)))
  268. goto out_unlock;
  269. desc->status |= IRQ_INPROGRESS;
  270. spin_unlock(&desc->lock);
  271. action_ret = handle_IRQ_event(irq, action);
  272. if (!noirqdebug)
  273. note_interrupt(irq, desc, action_ret);
  274. spin_lock(&desc->lock);
  275. desc->status &= ~IRQ_INPROGRESS;
  276. out_unlock:
  277. spin_unlock(&desc->lock);
  278. }
  279. /**
  280. * handle_level_irq - Level type irq handler
  281. * @irq: the interrupt number
  282. * @desc: the interrupt description structure for this irq
  283. *
  284. * Level type interrupts are active as long as the hardware line has
  285. * the active level. This may require to mask the interrupt and unmask
  286. * it after the associated handler has acknowledged the device, so the
  287. * interrupt line is back to inactive.
  288. */
  289. void fastcall
  290. handle_level_irq(unsigned int irq, struct irq_desc *desc)
  291. {
  292. unsigned int cpu = smp_processor_id();
  293. struct irqaction *action;
  294. irqreturn_t action_ret;
  295. spin_lock(&desc->lock);
  296. mask_ack_irq(desc, irq);
  297. if (unlikely(desc->status & IRQ_INPROGRESS))
  298. goto out_unlock;
  299. desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
  300. kstat_cpu(cpu).irqs[irq]++;
  301. /*
  302. * If its disabled or no action available
  303. * keep it masked and get out of here
  304. */
  305. action = desc->action;
  306. if (unlikely(!action || (desc->status & IRQ_DISABLED))) {
  307. desc->status |= IRQ_PENDING;
  308. goto out_unlock;
  309. }
  310. desc->status |= IRQ_INPROGRESS;
  311. desc->status &= ~IRQ_PENDING;
  312. spin_unlock(&desc->lock);
  313. action_ret = handle_IRQ_event(irq, action);
  314. if (!noirqdebug)
  315. note_interrupt(irq, desc, action_ret);
  316. spin_lock(&desc->lock);
  317. desc->status &= ~IRQ_INPROGRESS;
  318. if (!(desc->status & IRQ_DISABLED) && desc->chip->unmask)
  319. desc->chip->unmask(irq);
  320. out_unlock:
  321. spin_unlock(&desc->lock);
  322. }
  323. /**
  324. * handle_fasteoi_irq - irq handler for transparent controllers
  325. * @irq: the interrupt number
  326. * @desc: the interrupt description structure for this irq
  327. *
  328. * Only a single callback will be issued to the chip: an ->eoi()
  329. * call when the interrupt has been serviced. This enables support
  330. * for modern forms of interrupt handlers, which handle the flow
  331. * details in hardware, transparently.
  332. */
  333. void fastcall
  334. handle_fasteoi_irq(unsigned int irq, struct irq_desc *desc)
  335. {
  336. unsigned int cpu = smp_processor_id();
  337. struct irqaction *action;
  338. irqreturn_t action_ret;
  339. spin_lock(&desc->lock);
  340. if (unlikely(desc->status & IRQ_INPROGRESS))
  341. goto out;
  342. desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
  343. kstat_cpu(cpu).irqs[irq]++;
  344. /*
  345. * If its disabled or no action available
  346. * keep it masked and get out of here
  347. */
  348. action = desc->action;
  349. if (unlikely(!action || (desc->status & IRQ_DISABLED))) {
  350. desc->status |= IRQ_PENDING;
  351. goto out;
  352. }
  353. desc->status |= IRQ_INPROGRESS;
  354. desc->status &= ~IRQ_PENDING;
  355. spin_unlock(&desc->lock);
  356. action_ret = handle_IRQ_event(irq, action);
  357. if (!noirqdebug)
  358. note_interrupt(irq, desc, action_ret);
  359. spin_lock(&desc->lock);
  360. desc->status &= ~IRQ_INPROGRESS;
  361. out:
  362. desc->chip->eoi(irq);
  363. spin_unlock(&desc->lock);
  364. }
  365. /**
  366. * handle_edge_irq - edge type IRQ handler
  367. * @irq: the interrupt number
  368. * @desc: the interrupt description structure for this irq
  369. *
  370. * Interrupt occures on the falling and/or rising edge of a hardware
  371. * signal. The occurence is latched into the irq controller hardware
  372. * and must be acked in order to be reenabled. After the ack another
  373. * interrupt can happen on the same source even before the first one
  374. * is handled by the assosiacted event handler. If this happens it
  375. * might be necessary to disable (mask) the interrupt depending on the
  376. * controller hardware. This requires to reenable the interrupt inside
  377. * of the loop which handles the interrupts which have arrived while
  378. * the handler was running. If all pending interrupts are handled, the
  379. * loop is left.
  380. */
  381. void fastcall
  382. handle_edge_irq(unsigned int irq, struct irq_desc *desc)
  383. {
  384. const unsigned int cpu = smp_processor_id();
  385. spin_lock(&desc->lock);
  386. desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
  387. /*
  388. * If we're currently running this IRQ, or its disabled,
  389. * we shouldn't process the IRQ. Mark it pending, handle
  390. * the necessary masking and go out
  391. */
  392. if (unlikely((desc->status & (IRQ_INPROGRESS | IRQ_DISABLED)) ||
  393. !desc->action)) {
  394. desc->status |= (IRQ_PENDING | IRQ_MASKED);
  395. mask_ack_irq(desc, irq);
  396. goto out_unlock;
  397. }
  398. kstat_cpu(cpu).irqs[irq]++;
  399. /* Start handling the irq */
  400. desc->chip->ack(irq);
  401. /* Mark the IRQ currently in progress.*/
  402. desc->status |= IRQ_INPROGRESS;
  403. do {
  404. struct irqaction *action = desc->action;
  405. irqreturn_t action_ret;
  406. if (unlikely(!action)) {
  407. desc->chip->mask(irq);
  408. goto out_unlock;
  409. }
  410. /*
  411. * When another irq arrived while we were handling
  412. * one, we could have masked the irq.
  413. * Renable it, if it was not disabled in meantime.
  414. */
  415. if (unlikely((desc->status &
  416. (IRQ_PENDING | IRQ_MASKED | IRQ_DISABLED)) ==
  417. (IRQ_PENDING | IRQ_MASKED))) {
  418. desc->chip->unmask(irq);
  419. desc->status &= ~IRQ_MASKED;
  420. }
  421. desc->status &= ~IRQ_PENDING;
  422. spin_unlock(&desc->lock);
  423. action_ret = handle_IRQ_event(irq, action);
  424. if (!noirqdebug)
  425. note_interrupt(irq, desc, action_ret);
  426. spin_lock(&desc->lock);
  427. } while ((desc->status & (IRQ_PENDING | IRQ_DISABLED)) == IRQ_PENDING);
  428. desc->status &= ~IRQ_INPROGRESS;
  429. out_unlock:
  430. spin_unlock(&desc->lock);
  431. }
  432. #ifdef CONFIG_SMP
  433. /**
  434. * handle_percpu_IRQ - Per CPU local irq handler
  435. * @irq: the interrupt number
  436. * @desc: the interrupt description structure for this irq
  437. *
  438. * Per CPU interrupts on SMP machines without locking requirements
  439. */
  440. void fastcall
  441. handle_percpu_irq(unsigned int irq, struct irq_desc *desc)
  442. {
  443. irqreturn_t action_ret;
  444. kstat_this_cpu.irqs[irq]++;
  445. if (desc->chip->ack)
  446. desc->chip->ack(irq);
  447. action_ret = handle_IRQ_event(irq, desc->action);
  448. if (!noirqdebug)
  449. note_interrupt(irq, desc, action_ret);
  450. if (desc->chip->eoi)
  451. desc->chip->eoi(irq);
  452. }
  453. #endif /* CONFIG_SMP */
  454. void
  455. __set_irq_handler(unsigned int irq, irq_flow_handler_t handle, int is_chained,
  456. const char *name)
  457. {
  458. struct irq_desc *desc;
  459. unsigned long flags;
  460. if (irq >= NR_IRQS) {
  461. printk(KERN_ERR
  462. "Trying to install type control for IRQ%d\n", irq);
  463. return;
  464. }
  465. desc = irq_desc + irq;
  466. if (!handle)
  467. handle = handle_bad_irq;
  468. else if (desc->chip == &no_irq_chip) {
  469. printk(KERN_WARNING "Trying to install %sinterrupt handler "
  470. "for IRQ%d\n", is_chained ? "chained " : "", irq);
  471. /*
  472. * Some ARM implementations install a handler for really dumb
  473. * interrupt hardware without setting an irq_chip. This worked
  474. * with the ARM no_irq_chip but the check in setup_irq would
  475. * prevent us to setup the interrupt at all. Switch it to
  476. * dummy_irq_chip for easy transition.
  477. */
  478. desc->chip = &dummy_irq_chip;
  479. }
  480. spin_lock_irqsave(&desc->lock, flags);
  481. /* Uninstall? */
  482. if (handle == handle_bad_irq) {
  483. if (desc->chip != &no_irq_chip) {
  484. desc->chip->mask(irq);
  485. desc->chip->ack(irq);
  486. }
  487. desc->status |= IRQ_DISABLED;
  488. desc->depth = 1;
  489. }
  490. desc->handle_irq = handle;
  491. desc->name = name;
  492. if (handle != handle_bad_irq && is_chained) {
  493. desc->status &= ~IRQ_DISABLED;
  494. desc->status |= IRQ_NOREQUEST | IRQ_NOPROBE;
  495. desc->depth = 0;
  496. desc->chip->unmask(irq);
  497. }
  498. spin_unlock_irqrestore(&desc->lock, flags);
  499. }
  500. void
  501. set_irq_chip_and_handler(unsigned int irq, struct irq_chip *chip,
  502. irq_flow_handler_t handle)
  503. {
  504. set_irq_chip(irq, chip);
  505. __set_irq_handler(irq, handle, 0, NULL);
  506. }
  507. void
  508. set_irq_chip_and_handler_name(unsigned int irq, struct irq_chip *chip,
  509. irq_flow_handler_t handle, const char *name)
  510. {
  511. set_irq_chip(irq, chip);
  512. __set_irq_handler(irq, handle, 0, name);
  513. }