manage.c 8.7 KB

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