chip.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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->handler_data = NULL;
  38. desc->chip_data = NULL;
  39. desc->action = NULL;
  40. desc->irq_count = 0;
  41. desc->irqs_unhandled = 0;
  42. #ifdef CONFIG_SMP
  43. desc->affinity = CPU_MASK_ALL;
  44. #endif
  45. spin_unlock_irqrestore(&desc->lock, flags);
  46. }
  47. /**
  48. * dynamic_irq_cleanup - cleanup a dynamically allocated irq
  49. * @irq: irq number to initialize
  50. */
  51. void dynamic_irq_cleanup(unsigned int irq)
  52. {
  53. struct irq_desc *desc;
  54. unsigned long flags;
  55. if (irq >= NR_IRQS) {
  56. printk(KERN_ERR "Trying to cleanup invalid IRQ%d\n", irq);
  57. WARN_ON(1);
  58. return;
  59. }
  60. desc = irq_desc + irq;
  61. spin_lock_irqsave(&desc->lock, flags);
  62. if (desc->action) {
  63. spin_unlock_irqrestore(&desc->lock, flags);
  64. printk(KERN_ERR "Destroying IRQ%d without calling free_irq\n",
  65. irq);
  66. WARN_ON(1);
  67. return;
  68. }
  69. desc->handle_irq = handle_bad_irq;
  70. desc->chip = &no_irq_chip;
  71. spin_unlock_irqrestore(&desc->lock, flags);
  72. }
  73. /**
  74. * set_irq_chip - set the irq chip for an irq
  75. * @irq: irq number
  76. * @chip: pointer to irq chip description structure
  77. */
  78. int set_irq_chip(unsigned int irq, struct irq_chip *chip)
  79. {
  80. struct irq_desc *desc;
  81. unsigned long flags;
  82. if (irq >= NR_IRQS) {
  83. printk(KERN_ERR "Trying to install chip for IRQ%d\n", irq);
  84. WARN_ON(1);
  85. return -EINVAL;
  86. }
  87. if (!chip)
  88. chip = &no_irq_chip;
  89. desc = irq_desc + irq;
  90. spin_lock_irqsave(&desc->lock, flags);
  91. irq_chip_set_defaults(chip);
  92. desc->chip = chip;
  93. spin_unlock_irqrestore(&desc->lock, flags);
  94. return 0;
  95. }
  96. EXPORT_SYMBOL(set_irq_chip);
  97. /**
  98. * set_irq_type - set the irq type for an irq
  99. * @irq: irq number
  100. * @type: interrupt type - see include/linux/interrupt.h
  101. */
  102. int set_irq_type(unsigned int irq, unsigned int type)
  103. {
  104. struct irq_desc *desc;
  105. unsigned long flags;
  106. int ret = -ENXIO;
  107. if (irq >= NR_IRQS) {
  108. printk(KERN_ERR "Trying to set irq type for IRQ%d\n", irq);
  109. return -ENODEV;
  110. }
  111. desc = irq_desc + irq;
  112. if (desc->chip->set_type) {
  113. spin_lock_irqsave(&desc->lock, flags);
  114. ret = desc->chip->set_type(irq, type);
  115. spin_unlock_irqrestore(&desc->lock, flags);
  116. }
  117. return ret;
  118. }
  119. EXPORT_SYMBOL(set_irq_type);
  120. /**
  121. * set_irq_data - set irq type data for an irq
  122. * @irq: Interrupt number
  123. * @data: Pointer to interrupt specific data
  124. *
  125. * Set the hardware irq controller data for an irq
  126. */
  127. int set_irq_data(unsigned int irq, void *data)
  128. {
  129. struct irq_desc *desc;
  130. unsigned long flags;
  131. if (irq >= NR_IRQS) {
  132. printk(KERN_ERR
  133. "Trying to install controller data for IRQ%d\n", irq);
  134. return -EINVAL;
  135. }
  136. desc = irq_desc + irq;
  137. spin_lock_irqsave(&desc->lock, flags);
  138. desc->handler_data = data;
  139. spin_unlock_irqrestore(&desc->lock, flags);
  140. return 0;
  141. }
  142. EXPORT_SYMBOL(set_irq_data);
  143. /**
  144. * set_irq_chip_data - set irq chip data for an irq
  145. * @irq: Interrupt number
  146. * @data: Pointer to chip specific data
  147. *
  148. * Set the hardware irq chip data for an irq
  149. */
  150. int set_irq_chip_data(unsigned int irq, void *data)
  151. {
  152. struct irq_desc *desc = irq_desc + irq;
  153. unsigned long flags;
  154. if (irq >= NR_IRQS || !desc->chip) {
  155. printk(KERN_ERR "BUG: bad set_irq_chip_data(IRQ#%d)\n", irq);
  156. return -EINVAL;
  157. }
  158. spin_lock_irqsave(&desc->lock, flags);
  159. desc->chip_data = data;
  160. spin_unlock_irqrestore(&desc->lock, flags);
  161. return 0;
  162. }
  163. EXPORT_SYMBOL(set_irq_chip_data);
  164. /*
  165. * default enable function
  166. */
  167. static void default_enable(unsigned int irq)
  168. {
  169. struct irq_desc *desc = irq_desc + irq;
  170. desc->chip->unmask(irq);
  171. desc->status &= ~IRQ_MASKED;
  172. }
  173. /*
  174. * default disable function
  175. */
  176. static void default_disable(unsigned int irq)
  177. {
  178. struct irq_desc *desc = irq_desc + irq;
  179. if (!(desc->status & IRQ_DELAYED_DISABLE))
  180. desc->chip->mask(irq);
  181. }
  182. /*
  183. * default startup function
  184. */
  185. static unsigned int default_startup(unsigned int irq)
  186. {
  187. irq_desc[irq].chip->enable(irq);
  188. return 0;
  189. }
  190. /*
  191. * Fixup enable/disable function pointers
  192. */
  193. void irq_chip_set_defaults(struct irq_chip *chip)
  194. {
  195. if (!chip->enable)
  196. chip->enable = default_enable;
  197. if (!chip->disable)
  198. chip->disable = default_disable;
  199. if (!chip->startup)
  200. chip->startup = default_startup;
  201. if (!chip->shutdown)
  202. chip->shutdown = chip->disable;
  203. if (!chip->name)
  204. chip->name = chip->typename;
  205. if (!chip->end)
  206. chip->end = dummy_irq_chip.end;
  207. }
  208. static inline void mask_ack_irq(struct irq_desc *desc, int irq)
  209. {
  210. if (desc->chip->mask_ack)
  211. desc->chip->mask_ack(irq);
  212. else {
  213. desc->chip->mask(irq);
  214. desc->chip->ack(irq);
  215. }
  216. }
  217. /**
  218. * handle_simple_irq - Simple and software-decoded IRQs.
  219. * @irq: the interrupt number
  220. * @desc: the interrupt description structure for this irq
  221. *
  222. * Simple interrupts are either sent from a demultiplexing interrupt
  223. * handler or come from hardware, where no interrupt hardware control
  224. * is necessary.
  225. *
  226. * Note: The caller is expected to handle the ack, clear, mask and
  227. * unmask issues if necessary.
  228. */
  229. void fastcall
  230. handle_simple_irq(unsigned int irq, struct irq_desc *desc)
  231. {
  232. struct irqaction *action;
  233. irqreturn_t action_ret;
  234. const unsigned int cpu = smp_processor_id();
  235. spin_lock(&desc->lock);
  236. if (unlikely(desc->status & IRQ_INPROGRESS))
  237. goto out_unlock;
  238. desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
  239. kstat_cpu(cpu).irqs[irq]++;
  240. action = desc->action;
  241. if (unlikely(!action || (desc->status & IRQ_DISABLED)))
  242. goto out_unlock;
  243. desc->status |= IRQ_INPROGRESS;
  244. spin_unlock(&desc->lock);
  245. action_ret = handle_IRQ_event(irq, action);
  246. if (!noirqdebug)
  247. note_interrupt(irq, desc, action_ret);
  248. spin_lock(&desc->lock);
  249. desc->status &= ~IRQ_INPROGRESS;
  250. out_unlock:
  251. spin_unlock(&desc->lock);
  252. }
  253. /**
  254. * handle_level_irq - Level type irq handler
  255. * @irq: the interrupt number
  256. * @desc: the interrupt description structure for this irq
  257. *
  258. * Level type interrupts are active as long as the hardware line has
  259. * the active level. This may require to mask the interrupt and unmask
  260. * it after the associated handler has acknowledged the device, so the
  261. * interrupt line is back to inactive.
  262. */
  263. void fastcall
  264. handle_level_irq(unsigned int irq, struct irq_desc *desc)
  265. {
  266. unsigned int cpu = smp_processor_id();
  267. struct irqaction *action;
  268. irqreturn_t action_ret;
  269. spin_lock(&desc->lock);
  270. mask_ack_irq(desc, irq);
  271. if (unlikely(desc->status & IRQ_INPROGRESS))
  272. goto out_unlock;
  273. desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
  274. kstat_cpu(cpu).irqs[irq]++;
  275. /*
  276. * If its disabled or no action available
  277. * keep it masked and get out of here
  278. */
  279. action = desc->action;
  280. if (unlikely(!action || (desc->status & IRQ_DISABLED))) {
  281. desc->status |= IRQ_PENDING;
  282. goto out_unlock;
  283. }
  284. desc->status |= IRQ_INPROGRESS;
  285. desc->status &= ~IRQ_PENDING;
  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. if (!(desc->status & IRQ_DISABLED) && desc->chip->unmask)
  293. desc->chip->unmask(irq);
  294. out_unlock:
  295. spin_unlock(&desc->lock);
  296. }
  297. /**
  298. * handle_fasteoi_irq - irq handler for transparent controllers
  299. * @irq: the interrupt number
  300. * @desc: the interrupt description structure for this irq
  301. *
  302. * Only a single callback will be issued to the chip: an ->eoi()
  303. * call when the interrupt has been serviced. This enables support
  304. * for modern forms of interrupt handlers, which handle the flow
  305. * details in hardware, transparently.
  306. */
  307. void fastcall
  308. handle_fasteoi_irq(unsigned int irq, struct irq_desc *desc)
  309. {
  310. unsigned int cpu = smp_processor_id();
  311. struct irqaction *action;
  312. irqreturn_t action_ret;
  313. spin_lock(&desc->lock);
  314. if (unlikely(desc->status & IRQ_INPROGRESS))
  315. goto out;
  316. desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
  317. kstat_cpu(cpu).irqs[irq]++;
  318. /*
  319. * If its disabled or no action available
  320. * keep it masked and get out of here
  321. */
  322. action = desc->action;
  323. if (unlikely(!action || (desc->status & IRQ_DISABLED))) {
  324. desc->status |= IRQ_PENDING;
  325. goto out;
  326. }
  327. desc->status |= IRQ_INPROGRESS;
  328. desc->status &= ~IRQ_PENDING;
  329. spin_unlock(&desc->lock);
  330. action_ret = handle_IRQ_event(irq, action);
  331. if (!noirqdebug)
  332. note_interrupt(irq, desc, action_ret);
  333. spin_lock(&desc->lock);
  334. desc->status &= ~IRQ_INPROGRESS;
  335. out:
  336. desc->chip->eoi(irq);
  337. spin_unlock(&desc->lock);
  338. }
  339. /**
  340. * handle_edge_irq - edge type IRQ handler
  341. * @irq: the interrupt number
  342. * @desc: the interrupt description structure for this irq
  343. *
  344. * Interrupt occures on the falling and/or rising edge of a hardware
  345. * signal. The occurence is latched into the irq controller hardware
  346. * and must be acked in order to be reenabled. After the ack another
  347. * interrupt can happen on the same source even before the first one
  348. * is handled by the assosiacted event handler. If this happens it
  349. * might be necessary to disable (mask) the interrupt depending on the
  350. * controller hardware. This requires to reenable the interrupt inside
  351. * of the loop which handles the interrupts which have arrived while
  352. * the handler was running. If all pending interrupts are handled, the
  353. * loop is left.
  354. */
  355. void fastcall
  356. handle_edge_irq(unsigned int irq, struct irq_desc *desc)
  357. {
  358. const unsigned int cpu = smp_processor_id();
  359. spin_lock(&desc->lock);
  360. desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
  361. /*
  362. * If we're currently running this IRQ, or its disabled,
  363. * we shouldn't process the IRQ. Mark it pending, handle
  364. * the necessary masking and go out
  365. */
  366. if (unlikely((desc->status & (IRQ_INPROGRESS | IRQ_DISABLED)) ||
  367. !desc->action)) {
  368. desc->status |= (IRQ_PENDING | IRQ_MASKED);
  369. mask_ack_irq(desc, irq);
  370. goto out_unlock;
  371. }
  372. kstat_cpu(cpu).irqs[irq]++;
  373. /* Start handling the irq */
  374. desc->chip->ack(irq);
  375. /* Mark the IRQ currently in progress.*/
  376. desc->status |= IRQ_INPROGRESS;
  377. do {
  378. struct irqaction *action = desc->action;
  379. irqreturn_t action_ret;
  380. if (unlikely(!action)) {
  381. desc->chip->mask(irq);
  382. goto out_unlock;
  383. }
  384. /*
  385. * When another irq arrived while we were handling
  386. * one, we could have masked the irq.
  387. * Renable it, if it was not disabled in meantime.
  388. */
  389. if (unlikely((desc->status &
  390. (IRQ_PENDING | IRQ_MASKED | IRQ_DISABLED)) ==
  391. (IRQ_PENDING | IRQ_MASKED))) {
  392. desc->chip->unmask(irq);
  393. desc->status &= ~IRQ_MASKED;
  394. }
  395. desc->status &= ~IRQ_PENDING;
  396. spin_unlock(&desc->lock);
  397. action_ret = handle_IRQ_event(irq, action);
  398. if (!noirqdebug)
  399. note_interrupt(irq, desc, action_ret);
  400. spin_lock(&desc->lock);
  401. } while ((desc->status & (IRQ_PENDING | IRQ_DISABLED)) == IRQ_PENDING);
  402. desc->status &= ~IRQ_INPROGRESS;
  403. out_unlock:
  404. spin_unlock(&desc->lock);
  405. }
  406. #ifdef CONFIG_SMP
  407. /**
  408. * handle_percpu_IRQ - Per CPU local irq handler
  409. * @irq: the interrupt number
  410. * @desc: the interrupt description structure for this irq
  411. *
  412. * Per CPU interrupts on SMP machines without locking requirements
  413. */
  414. void fastcall
  415. handle_percpu_irq(unsigned int irq, struct irq_desc *desc)
  416. {
  417. irqreturn_t action_ret;
  418. kstat_this_cpu.irqs[irq]++;
  419. if (desc->chip->ack)
  420. desc->chip->ack(irq);
  421. action_ret = handle_IRQ_event(irq, desc->action);
  422. if (!noirqdebug)
  423. note_interrupt(irq, desc, action_ret);
  424. if (desc->chip->eoi)
  425. desc->chip->eoi(irq);
  426. }
  427. #endif /* CONFIG_SMP */
  428. void
  429. __set_irq_handler(unsigned int irq, irq_flow_handler_t handle, int is_chained,
  430. const char *name)
  431. {
  432. struct irq_desc *desc;
  433. unsigned long flags;
  434. if (irq >= NR_IRQS) {
  435. printk(KERN_ERR
  436. "Trying to install type control for IRQ%d\n", irq);
  437. return;
  438. }
  439. desc = irq_desc + irq;
  440. if (!handle)
  441. handle = handle_bad_irq;
  442. if (desc->chip == &no_irq_chip) {
  443. printk(KERN_WARNING "Trying to install %sinterrupt handler "
  444. "for IRQ%d\n", is_chained ? "chained " : " ", irq);
  445. /*
  446. * Some ARM implementations install a handler for really dumb
  447. * interrupt hardware without setting an irq_chip. This worked
  448. * with the ARM no_irq_chip but the check in setup_irq would
  449. * prevent us to setup the interrupt at all. Switch it to
  450. * dummy_irq_chip for easy transition.
  451. */
  452. desc->chip = &dummy_irq_chip;
  453. }
  454. spin_lock_irqsave(&desc->lock, flags);
  455. /* Uninstall? */
  456. if (handle == handle_bad_irq) {
  457. if (desc->chip != &no_irq_chip) {
  458. desc->chip->mask(irq);
  459. desc->chip->ack(irq);
  460. }
  461. desc->status |= IRQ_DISABLED;
  462. desc->depth = 1;
  463. }
  464. desc->handle_irq = handle;
  465. desc->name = name;
  466. if (handle != handle_bad_irq && is_chained) {
  467. desc->status &= ~IRQ_DISABLED;
  468. desc->status |= IRQ_NOREQUEST | IRQ_NOPROBE;
  469. desc->depth = 0;
  470. desc->chip->unmask(irq);
  471. }
  472. spin_unlock_irqrestore(&desc->lock, flags);
  473. }
  474. void
  475. set_irq_chip_and_handler(unsigned int irq, struct irq_chip *chip,
  476. irq_flow_handler_t handle)
  477. {
  478. set_irq_chip(irq, chip);
  479. __set_irq_handler(irq, handle, 0, NULL);
  480. }
  481. void
  482. set_irq_chip_and_handler_name(unsigned int irq, struct irq_chip *chip,
  483. irq_flow_handler_t handle, const char *name)
  484. {
  485. set_irq_chip(irq, chip);
  486. __set_irq_handler(irq, handle, 0, name);
  487. }