manage.c 8.5 KB

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