manage.c 9.1 KB

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