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/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. * @entry: Pointer to MSI descriptor 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. }
  205. /*
  206. * default startup function
  207. */
  208. static unsigned int default_startup(unsigned int irq)
  209. {
  210. irq_desc[irq].chip->enable(irq);
  211. return 0;
  212. }
  213. /*
  214. * Fixup enable/disable function pointers
  215. */
  216. void irq_chip_set_defaults(struct irq_chip *chip)
  217. {
  218. if (!chip->enable)
  219. chip->enable = default_enable;
  220. if (!chip->disable)
  221. chip->disable = default_disable;
  222. if (!chip->startup)
  223. chip->startup = default_startup;
  224. if (!chip->shutdown)
  225. chip->shutdown = chip->disable;
  226. if (!chip->name)
  227. chip->name = chip->typename;
  228. if (!chip->end)
  229. chip->end = dummy_irq_chip.end;
  230. }
  231. static inline void mask_ack_irq(struct irq_desc *desc, int irq)
  232. {
  233. if (desc->chip->mask_ack)
  234. desc->chip->mask_ack(irq);
  235. else {
  236. desc->chip->mask(irq);
  237. desc->chip->ack(irq);
  238. }
  239. }
  240. /**
  241. * handle_simple_irq - Simple and software-decoded IRQs.
  242. * @irq: the interrupt number
  243. * @desc: the interrupt description structure for this irq
  244. *
  245. * Simple interrupts are either sent from a demultiplexing interrupt
  246. * handler or come from hardware, where no interrupt hardware control
  247. * is necessary.
  248. *
  249. * Note: The caller is expected to handle the ack, clear, mask and
  250. * unmask issues if necessary.
  251. */
  252. void fastcall
  253. handle_simple_irq(unsigned int irq, struct irq_desc *desc)
  254. {
  255. struct irqaction *action;
  256. irqreturn_t action_ret;
  257. const unsigned int cpu = smp_processor_id();
  258. spin_lock(&desc->lock);
  259. if (unlikely(desc->status & IRQ_INPROGRESS))
  260. goto out_unlock;
  261. kstat_cpu(cpu).irqs[irq]++;
  262. action = desc->action;
  263. if (unlikely(!action || (desc->status & IRQ_DISABLED))) {
  264. if (desc->chip->mask)
  265. desc->chip->mask(irq);
  266. desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
  267. desc->status |= IRQ_PENDING;
  268. goto out_unlock;
  269. }
  270. desc->status &= ~(IRQ_REPLAY | IRQ_WAITING | IRQ_PENDING);
  271. desc->status |= IRQ_INPROGRESS;
  272. spin_unlock(&desc->lock);
  273. action_ret = handle_IRQ_event(irq, action);
  274. if (!noirqdebug)
  275. note_interrupt(irq, desc, action_ret);
  276. spin_lock(&desc->lock);
  277. desc->status &= ~IRQ_INPROGRESS;
  278. out_unlock:
  279. spin_unlock(&desc->lock);
  280. }
  281. /**
  282. * handle_level_irq - Level type irq handler
  283. * @irq: the interrupt number
  284. * @desc: the interrupt description structure for this irq
  285. *
  286. * Level type interrupts are active as long as the hardware line has
  287. * the active level. This may require to mask the interrupt and unmask
  288. * it after the associated handler has acknowledged the device, so the
  289. * interrupt line is back to inactive.
  290. */
  291. void fastcall
  292. handle_level_irq(unsigned int irq, struct irq_desc *desc)
  293. {
  294. unsigned int cpu = smp_processor_id();
  295. struct irqaction *action;
  296. irqreturn_t action_ret;
  297. spin_lock(&desc->lock);
  298. mask_ack_irq(desc, irq);
  299. if (unlikely(desc->status & IRQ_INPROGRESS))
  300. goto out_unlock;
  301. desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
  302. kstat_cpu(cpu).irqs[irq]++;
  303. /*
  304. * If its disabled or no action available
  305. * keep it masked and get out of here
  306. */
  307. action = desc->action;
  308. if (unlikely(!action || (desc->status & IRQ_DISABLED))) {
  309. desc->status |= IRQ_PENDING;
  310. goto out_unlock;
  311. }
  312. desc->status |= IRQ_INPROGRESS;
  313. desc->status &= ~IRQ_PENDING;
  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. }