chip.c 14 KB

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