manage.c 8.9 KB

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