chip.c 13 KB

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