chip.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  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. }
  206. static inline void mask_ack_irq(struct irq_desc *desc, int irq)
  207. {
  208. if (desc->chip->mask_ack)
  209. desc->chip->mask_ack(irq);
  210. else {
  211. desc->chip->mask(irq);
  212. desc->chip->ack(irq);
  213. }
  214. }
  215. /**
  216. * handle_simple_irq - Simple and software-decoded IRQs.
  217. * @irq: the interrupt number
  218. * @desc: the interrupt description structure for this irq
  219. *
  220. * Simple interrupts are either sent from a demultiplexing interrupt
  221. * handler or come from hardware, where no interrupt hardware control
  222. * is necessary.
  223. *
  224. * Note: The caller is expected to handle the ack, clear, mask and
  225. * unmask issues if necessary.
  226. */
  227. void fastcall
  228. handle_simple_irq(unsigned int irq, struct irq_desc *desc)
  229. {
  230. struct irqaction *action;
  231. irqreturn_t action_ret;
  232. const unsigned int cpu = smp_processor_id();
  233. spin_lock(&desc->lock);
  234. if (unlikely(desc->status & IRQ_INPROGRESS))
  235. goto out_unlock;
  236. desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
  237. kstat_cpu(cpu).irqs[irq]++;
  238. action = desc->action;
  239. if (unlikely(!action || (desc->status & IRQ_DISABLED)))
  240. goto out_unlock;
  241. desc->status |= IRQ_INPROGRESS;
  242. spin_unlock(&desc->lock);
  243. action_ret = handle_IRQ_event(irq, action);
  244. if (!noirqdebug)
  245. note_interrupt(irq, desc, action_ret);
  246. spin_lock(&desc->lock);
  247. desc->status &= ~IRQ_INPROGRESS;
  248. out_unlock:
  249. spin_unlock(&desc->lock);
  250. }
  251. /**
  252. * handle_level_irq - Level type irq handler
  253. * @irq: the interrupt number
  254. * @desc: the interrupt description structure for this irq
  255. *
  256. * Level type interrupts are active as long as the hardware line has
  257. * the active level. This may require to mask the interrupt and unmask
  258. * it after the associated handler has acknowledged the device, so the
  259. * interrupt line is back to inactive.
  260. */
  261. void fastcall
  262. handle_level_irq(unsigned int irq, struct irq_desc *desc)
  263. {
  264. unsigned int cpu = smp_processor_id();
  265. struct irqaction *action;
  266. irqreturn_t action_ret;
  267. spin_lock(&desc->lock);
  268. mask_ack_irq(desc, irq);
  269. if (unlikely(desc->status & IRQ_INPROGRESS))
  270. goto out_unlock;
  271. desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
  272. kstat_cpu(cpu).irqs[irq]++;
  273. /*
  274. * If its disabled or no action available
  275. * keep it masked and get out of here
  276. */
  277. action = desc->action;
  278. if (unlikely(!action || (desc->status & IRQ_DISABLED))) {
  279. desc->status |= IRQ_PENDING;
  280. goto out_unlock;
  281. }
  282. desc->status |= IRQ_INPROGRESS;
  283. desc->status &= ~IRQ_PENDING;
  284. spin_unlock(&desc->lock);
  285. action_ret = handle_IRQ_event(irq, action);
  286. if (!noirqdebug)
  287. note_interrupt(irq, desc, action_ret);
  288. spin_lock(&desc->lock);
  289. desc->status &= ~IRQ_INPROGRESS;
  290. if (!(desc->status & IRQ_DISABLED) && desc->chip->unmask)
  291. desc->chip->unmask(irq);
  292. out_unlock:
  293. spin_unlock(&desc->lock);
  294. }
  295. /**
  296. * handle_fasteoi_irq - irq handler for transparent controllers
  297. * @irq: the interrupt number
  298. * @desc: the interrupt description structure for this irq
  299. *
  300. * Only a single callback will be issued to the chip: an ->eoi()
  301. * call when the interrupt has been serviced. This enables support
  302. * for modern forms of interrupt handlers, which handle the flow
  303. * details in hardware, transparently.
  304. */
  305. void fastcall
  306. handle_fasteoi_irq(unsigned int irq, struct irq_desc *desc)
  307. {
  308. unsigned int cpu = smp_processor_id();
  309. struct irqaction *action;
  310. irqreturn_t action_ret;
  311. spin_lock(&desc->lock);
  312. if (unlikely(desc->status & IRQ_INPROGRESS))
  313. goto out;
  314. desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
  315. kstat_cpu(cpu).irqs[irq]++;
  316. /*
  317. * If its disabled or no action available
  318. * keep it masked and get out of here
  319. */
  320. action = desc->action;
  321. if (unlikely(!action || (desc->status & IRQ_DISABLED))) {
  322. desc->status |= IRQ_PENDING;
  323. goto out;
  324. }
  325. desc->status |= IRQ_INPROGRESS;
  326. desc->status &= ~IRQ_PENDING;
  327. spin_unlock(&desc->lock);
  328. action_ret = handle_IRQ_event(irq, action);
  329. if (!noirqdebug)
  330. note_interrupt(irq, desc, action_ret);
  331. spin_lock(&desc->lock);
  332. desc->status &= ~IRQ_INPROGRESS;
  333. out:
  334. desc->chip->eoi(irq);
  335. spin_unlock(&desc->lock);
  336. }
  337. /**
  338. * handle_edge_irq - edge type IRQ handler
  339. * @irq: the interrupt number
  340. * @desc: the interrupt description structure for this irq
  341. *
  342. * Interrupt occures on the falling and/or rising edge of a hardware
  343. * signal. The occurence is latched into the irq controller hardware
  344. * and must be acked in order to be reenabled. After the ack another
  345. * interrupt can happen on the same source even before the first one
  346. * is handled by the assosiacted event handler. If this happens it
  347. * might be necessary to disable (mask) the interrupt depending on the
  348. * controller hardware. This requires to reenable the interrupt inside
  349. * of the loop which handles the interrupts which have arrived while
  350. * the handler was running. If all pending interrupts are handled, the
  351. * loop is left.
  352. */
  353. void fastcall
  354. handle_edge_irq(unsigned int irq, struct irq_desc *desc)
  355. {
  356. const unsigned int cpu = smp_processor_id();
  357. spin_lock(&desc->lock);
  358. desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);
  359. /*
  360. * If we're currently running this IRQ, or its disabled,
  361. * we shouldn't process the IRQ. Mark it pending, handle
  362. * the necessary masking and go out
  363. */
  364. if (unlikely((desc->status & (IRQ_INPROGRESS | IRQ_DISABLED)) ||
  365. !desc->action)) {
  366. desc->status |= (IRQ_PENDING | IRQ_MASKED);
  367. mask_ack_irq(desc, irq);
  368. goto out_unlock;
  369. }
  370. kstat_cpu(cpu).irqs[irq]++;
  371. /* Start handling the irq */
  372. desc->chip->ack(irq);
  373. /* Mark the IRQ currently in progress.*/
  374. desc->status |= IRQ_INPROGRESS;
  375. do {
  376. struct irqaction *action = desc->action;
  377. irqreturn_t action_ret;
  378. if (unlikely(!action)) {
  379. desc->chip->mask(irq);
  380. goto out_unlock;
  381. }
  382. /*
  383. * When another irq arrived while we were handling
  384. * one, we could have masked the irq.
  385. * Renable it, if it was not disabled in meantime.
  386. */
  387. if (unlikely((desc->status &
  388. (IRQ_PENDING | IRQ_MASKED | IRQ_DISABLED)) ==
  389. (IRQ_PENDING | IRQ_MASKED))) {
  390. desc->chip->unmask(irq);
  391. desc->status &= ~IRQ_MASKED;
  392. }
  393. desc->status &= ~IRQ_PENDING;
  394. spin_unlock(&desc->lock);
  395. action_ret = handle_IRQ_event(irq, action);
  396. if (!noirqdebug)
  397. note_interrupt(irq, desc, action_ret);
  398. spin_lock(&desc->lock);
  399. } while ((desc->status & (IRQ_PENDING | IRQ_DISABLED)) == IRQ_PENDING);
  400. desc->status &= ~IRQ_INPROGRESS;
  401. out_unlock:
  402. spin_unlock(&desc->lock);
  403. }
  404. #ifdef CONFIG_SMP
  405. /**
  406. * handle_percpu_IRQ - Per CPU local irq handler
  407. * @irq: the interrupt number
  408. * @desc: the interrupt description structure for this irq
  409. *
  410. * Per CPU interrupts on SMP machines without locking requirements
  411. */
  412. void fastcall
  413. handle_percpu_irq(unsigned int irq, struct irq_desc *desc)
  414. {
  415. irqreturn_t action_ret;
  416. kstat_this_cpu.irqs[irq]++;
  417. if (desc->chip->ack)
  418. desc->chip->ack(irq);
  419. action_ret = handle_IRQ_event(irq, desc->action);
  420. if (!noirqdebug)
  421. note_interrupt(irq, desc, action_ret);
  422. if (desc->chip->eoi)
  423. desc->chip->eoi(irq);
  424. }
  425. #endif /* CONFIG_SMP */
  426. void
  427. __set_irq_handler(unsigned int irq, irq_flow_handler_t handle, int is_chained,
  428. const char *name)
  429. {
  430. struct irq_desc *desc;
  431. unsigned long flags;
  432. if (irq >= NR_IRQS) {
  433. printk(KERN_ERR
  434. "Trying to install type control for IRQ%d\n", irq);
  435. return;
  436. }
  437. desc = irq_desc + irq;
  438. if (!handle)
  439. handle = handle_bad_irq;
  440. if (desc->chip == &no_irq_chip) {
  441. printk(KERN_WARNING "Trying to install %sinterrupt handler "
  442. "for IRQ%d\n", is_chained ? "chained " : " ", irq);
  443. /*
  444. * Some ARM implementations install a handler for really dumb
  445. * interrupt hardware without setting an irq_chip. This worked
  446. * with the ARM no_irq_chip but the check in setup_irq would
  447. * prevent us to setup the interrupt at all. Switch it to
  448. * dummy_irq_chip for easy transition.
  449. */
  450. desc->chip = &dummy_irq_chip;
  451. }
  452. spin_lock_irqsave(&desc->lock, flags);
  453. /* Uninstall? */
  454. if (handle == handle_bad_irq) {
  455. if (desc->chip != &no_irq_chip) {
  456. desc->chip->mask(irq);
  457. desc->chip->ack(irq);
  458. }
  459. desc->status |= IRQ_DISABLED;
  460. desc->depth = 1;
  461. }
  462. desc->handle_irq = handle;
  463. desc->name = name;
  464. if (handle != handle_bad_irq && is_chained) {
  465. desc->status &= ~IRQ_DISABLED;
  466. desc->status |= IRQ_NOREQUEST | IRQ_NOPROBE;
  467. desc->depth = 0;
  468. desc->chip->unmask(irq);
  469. }
  470. spin_unlock_irqrestore(&desc->lock, flags);
  471. }
  472. void
  473. set_irq_chip_and_handler(unsigned int irq, struct irq_chip *chip,
  474. irq_flow_handler_t handle)
  475. {
  476. set_irq_chip(irq, chip);
  477. __set_irq_handler(irq, handle, 0, NULL);
  478. }
  479. void
  480. set_irq_chip_and_handler_name(unsigned int irq, struct irq_chip *chip,
  481. irq_flow_handler_t handle, const char *name)
  482. {
  483. set_irq_chip(irq, chip);
  484. __set_irq_handler(irq, handle, 0, name);
  485. }