chip.c 12 KB

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