manage.c 8.9 KB

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