chip.c 13 KB

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