manage.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*
  2. * linux/kernel/irq/manage.c
  3. *
  4. * Copyright (C) 1992, 1998-2004 Linus Torvalds, Ingo Molnar
  5. *
  6. * This file contains driver APIs to the irq subsystem.
  7. */
  8. #include <linux/config.h>
  9. #include <linux/irq.h>
  10. #include <linux/module.h>
  11. #include <linux/random.h>
  12. #include <linux/interrupt.h>
  13. #include "internals.h"
  14. #ifdef CONFIG_SMP
  15. /**
  16. * synchronize_irq - wait for pending IRQ handlers (on other CPUs)
  17. * @irq: interrupt number to wait for
  18. *
  19. * This function waits for any pending IRQ handlers for this interrupt
  20. * to complete before returning. If you use this function while
  21. * holding a resource the IRQ handler may need you will deadlock.
  22. *
  23. * This function may be called - with care - from IRQ context.
  24. */
  25. void synchronize_irq(unsigned int irq)
  26. {
  27. struct irq_desc *desc = irq_desc + irq;
  28. if (irq >= NR_IRQS)
  29. return;
  30. while (desc->status & IRQ_INPROGRESS)
  31. cpu_relax();
  32. }
  33. EXPORT_SYMBOL(synchronize_irq);
  34. #endif
  35. /**
  36. * disable_irq_nosync - disable an irq without waiting
  37. * @irq: Interrupt to disable
  38. *
  39. * Disable the selected interrupt line. Disables and Enables are
  40. * nested.
  41. * Unlike disable_irq(), this function does not ensure existing
  42. * instances of the IRQ handler have completed before returning.
  43. *
  44. * This function may be called from IRQ context.
  45. */
  46. void disable_irq_nosync(unsigned int irq)
  47. {
  48. struct irq_desc *desc = irq_desc + irq;
  49. unsigned long flags;
  50. if (irq >= NR_IRQS)
  51. return;
  52. spin_lock_irqsave(&desc->lock, flags);
  53. if (!desc->depth++) {
  54. desc->status |= IRQ_DISABLED;
  55. desc->chip->disable(irq);
  56. }
  57. spin_unlock_irqrestore(&desc->lock, flags);
  58. }
  59. EXPORT_SYMBOL(disable_irq_nosync);
  60. /**
  61. * disable_irq - disable an irq and wait for completion
  62. * @irq: Interrupt to disable
  63. *
  64. * Disable the selected interrupt line. Enables and Disables are
  65. * nested.
  66. * This function waits for any pending IRQ handlers for this interrupt
  67. * to complete before returning. If you use this function while
  68. * holding a resource the IRQ handler may need you will deadlock.
  69. *
  70. * This function may be called - with care - from IRQ context.
  71. */
  72. void disable_irq(unsigned int irq)
  73. {
  74. struct irq_desc *desc = irq_desc + irq;
  75. if (irq >= NR_IRQS)
  76. return;
  77. disable_irq_nosync(irq);
  78. if (desc->action)
  79. synchronize_irq(irq);
  80. }
  81. EXPORT_SYMBOL(disable_irq);
  82. /**
  83. * enable_irq - enable handling of an irq
  84. * @irq: Interrupt to enable
  85. *
  86. * Undoes the effect of one call to disable_irq(). If this
  87. * matches the last disable, processing of interrupts on this
  88. * IRQ line is re-enabled.
  89. *
  90. * This function may be called from IRQ context.
  91. */
  92. void enable_irq(unsigned int irq)
  93. {
  94. struct irq_desc *desc = irq_desc + irq;
  95. unsigned long flags;
  96. if (irq >= NR_IRQS)
  97. return;
  98. spin_lock_irqsave(&desc->lock, flags);
  99. switch (desc->depth) {
  100. case 0:
  101. printk(KERN_WARNING "Unablanced enable_irq(%d)\n", irq);
  102. WARN_ON(1);
  103. break;
  104. case 1: {
  105. check_irq_resend(desc, irq);
  106. /* fall-through */
  107. }
  108. default:
  109. desc->depth--;
  110. }
  111. spin_unlock_irqrestore(&desc->lock, flags);
  112. }
  113. EXPORT_SYMBOL(enable_irq);
  114. /*
  115. * Internal function that tells the architecture code whether a
  116. * particular irq has been exclusively allocated or is available
  117. * for driver use.
  118. */
  119. int can_request_irq(unsigned int irq, unsigned long irqflags)
  120. {
  121. struct irqaction *action;
  122. if (irq >= NR_IRQS)
  123. return 0;
  124. action = irq_desc[irq].action;
  125. if (action)
  126. if (irqflags & action->flags & SA_SHIRQ)
  127. action = NULL;
  128. return !action;
  129. }
  130. /*
  131. * Internal function to register an irqaction - typically used to
  132. * allocate special interrupts that are part of the architecture.
  133. */
  134. int setup_irq(unsigned int irq, struct irqaction *new)
  135. {
  136. struct irq_desc *desc = irq_desc + irq;
  137. struct irqaction *old, **p;
  138. unsigned long flags;
  139. int shared = 0;
  140. if (irq >= NR_IRQS)
  141. return -EINVAL;
  142. if (desc->chip == &no_irq_type)
  143. return -ENOSYS;
  144. /*
  145. * Some drivers like serial.c use request_irq() heavily,
  146. * so we have to be careful not to interfere with a
  147. * running system.
  148. */
  149. if (new->flags & SA_SAMPLE_RANDOM) {
  150. /*
  151. * This function might sleep, we want to call it first,
  152. * outside of the atomic block.
  153. * Yes, this might clear the entropy pool if the wrong
  154. * driver is attempted to be loaded, without actually
  155. * installing a new handler, but is this really a problem,
  156. * only the sysadmin is able to do this.
  157. */
  158. rand_initialize_irq(irq);
  159. }
  160. /*
  161. * The following block of code has to be executed atomically
  162. */
  163. spin_lock_irqsave(&desc->lock, flags);
  164. p = &desc->action;
  165. old = *p;
  166. if (old) {
  167. /* Can't share interrupts unless both agree to */
  168. if (!(old->flags & new->flags & SA_SHIRQ))
  169. goto mismatch;
  170. #if defined(CONFIG_IRQ_PER_CPU) && defined(SA_PERCPU_IRQ)
  171. /* All handlers must agree on per-cpuness */
  172. if ((old->flags & IRQ_PER_CPU) != (new->flags & IRQ_PER_CPU))
  173. goto mismatch;
  174. #endif
  175. /* add new interrupt at end of irq queue */
  176. do {
  177. p = &old->next;
  178. old = *p;
  179. } while (old);
  180. shared = 1;
  181. }
  182. *p = new;
  183. #if defined(CONFIG_IRQ_PER_CPU) && defined(SA_PERCPU_IRQ)
  184. if (new->flags & SA_PERCPU_IRQ)
  185. desc->status |= IRQ_PER_CPU;
  186. #endif
  187. if (!shared) {
  188. desc->depth = 0;
  189. desc->status &= ~(IRQ_DISABLED | IRQ_AUTODETECT |
  190. IRQ_WAITING | IRQ_INPROGRESS);
  191. if (desc->chip->startup)
  192. desc->chip->startup(irq);
  193. else
  194. desc->chip->enable(irq);
  195. }
  196. spin_unlock_irqrestore(&desc->lock, flags);
  197. new->irq = irq;
  198. register_irq_proc(irq);
  199. new->dir = NULL;
  200. register_handler_proc(irq, new);
  201. return 0;
  202. mismatch:
  203. spin_unlock_irqrestore(&desc->lock, flags);
  204. if (!(new->flags & SA_PROBEIRQ)) {
  205. printk(KERN_ERR "%s: irq handler mismatch\n", __FUNCTION__);
  206. dump_stack();
  207. }
  208. return -EBUSY;
  209. }
  210. /**
  211. * free_irq - free an interrupt
  212. * @irq: Interrupt line to free
  213. * @dev_id: Device identity to free
  214. *
  215. * Remove an interrupt handler. The handler is removed and if the
  216. * interrupt line is no longer in use by any driver it is disabled.
  217. * On a shared IRQ the caller must ensure the interrupt is disabled
  218. * on the card it drives before calling this function. The function
  219. * does not return until any executing interrupts for this IRQ
  220. * have completed.
  221. *
  222. * This function must not be called from interrupt context.
  223. */
  224. void free_irq(unsigned int irq, void *dev_id)
  225. {
  226. struct irq_desc *desc;
  227. struct irqaction **p;
  228. unsigned long flags;
  229. WARN_ON(in_interrupt());
  230. if (irq >= NR_IRQS)
  231. return;
  232. desc = irq_desc + irq;
  233. spin_lock_irqsave(&desc->lock, flags);
  234. p = &desc->action;
  235. for (;;) {
  236. struct irqaction *action = *p;
  237. if (action) {
  238. struct irqaction **pp = p;
  239. p = &action->next;
  240. if (action->dev_id != dev_id)
  241. continue;
  242. /* Found it - now remove it from the list of entries */
  243. *pp = action->next;
  244. /* Currently used only by UML, might disappear one day.*/
  245. #ifdef CONFIG_IRQ_RELEASE_METHOD
  246. if (desc->chip->release)
  247. desc->chip->release(irq, dev_id);
  248. #endif
  249. if (!desc->action) {
  250. desc->status |= IRQ_DISABLED;
  251. if (desc->chip->shutdown)
  252. desc->chip->shutdown(irq);
  253. else
  254. desc->chip->disable(irq);
  255. }
  256. spin_unlock_irqrestore(&desc->lock, flags);
  257. unregister_handler_proc(irq, action);
  258. /* Make sure it's not being used on another CPU */
  259. synchronize_irq(irq);
  260. kfree(action);
  261. return;
  262. }
  263. printk(KERN_ERR "Trying to free free IRQ%d\n", irq);
  264. spin_unlock_irqrestore(&desc->lock, flags);
  265. return;
  266. }
  267. }
  268. EXPORT_SYMBOL(free_irq);
  269. /**
  270. * request_irq - allocate an interrupt line
  271. * @irq: Interrupt line to allocate
  272. * @handler: Function to be called when the IRQ occurs
  273. * @irqflags: Interrupt type flags
  274. * @devname: An ascii name for the claiming device
  275. * @dev_id: A cookie passed back to the handler function
  276. *
  277. * This call allocates interrupt resources and enables the
  278. * interrupt line and IRQ handling. From the point this
  279. * call is made your handler function may be invoked. Since
  280. * your handler function must clear any interrupt the board
  281. * raises, you must take care both to initialise your hardware
  282. * and to set up the interrupt handler in the right order.
  283. *
  284. * Dev_id must be globally unique. Normally the address of the
  285. * device data structure is used as the cookie. Since the handler
  286. * receives this value it makes sense to use it.
  287. *
  288. * If your interrupt is shared you must pass a non NULL dev_id
  289. * as this is required when freeing the interrupt.
  290. *
  291. * Flags:
  292. *
  293. * SA_SHIRQ Interrupt is shared
  294. * SA_INTERRUPT Disable local interrupts while processing
  295. * SA_SAMPLE_RANDOM The interrupt can be used for entropy
  296. *
  297. */
  298. int request_irq(unsigned int irq,
  299. irqreturn_t (*handler)(int, void *, struct pt_regs *),
  300. unsigned long irqflags, const char *devname, void *dev_id)
  301. {
  302. struct irqaction *action;
  303. int retval;
  304. /*
  305. * Sanity-check: shared interrupts must pass in a real dev-ID,
  306. * otherwise we'll have trouble later trying to figure out
  307. * which interrupt is which (messes up the interrupt freeing
  308. * logic etc).
  309. */
  310. if ((irqflags & SA_SHIRQ) && !dev_id)
  311. return -EINVAL;
  312. if (irq >= NR_IRQS)
  313. return -EINVAL;
  314. if (!handler)
  315. return -EINVAL;
  316. action = kmalloc(sizeof(struct irqaction), GFP_ATOMIC);
  317. if (!action)
  318. return -ENOMEM;
  319. action->handler = handler;
  320. action->flags = irqflags;
  321. cpus_clear(action->mask);
  322. action->name = devname;
  323. action->next = NULL;
  324. action->dev_id = dev_id;
  325. select_smp_affinity(irq);
  326. retval = setup_irq(irq, action);
  327. if (retval)
  328. kfree(action);
  329. return retval;
  330. }
  331. EXPORT_SYMBOL(request_irq);