manage.c 9.9 KB

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