manage.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  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/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. /**
  16. * synchronize_irq - wait for pending IRQ handlers (on other CPUs)
  17. * @irq: interrupt number to wait for
  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. if (irq >= NR_IRQS)
  29. return;
  30. while (desc->status & IRQ_INPROGRESS)
  31. cpu_relax();
  32. }
  33. EXPORT_SYMBOL(synchronize_irq);
  34. #endif
  35. /**
  36. * disable_irq_nosync - disable an irq without waiting
  37. * @irq: Interrupt to disable
  38. *
  39. * Disable the selected interrupt line. Disables and Enables are
  40. * nested.
  41. * Unlike disable_irq(), this function does not ensure existing
  42. * instances of the IRQ handler have completed before returning.
  43. *
  44. * This function may be called from IRQ context.
  45. */
  46. void disable_irq_nosync(unsigned int irq)
  47. {
  48. struct irq_desc *desc = irq_desc + irq;
  49. unsigned long flags;
  50. if (irq >= NR_IRQS)
  51. return;
  52. spin_lock_irqsave(&desc->lock, flags);
  53. if (!desc->depth++) {
  54. desc->status |= IRQ_DISABLED;
  55. desc->chip->disable(irq);
  56. }
  57. spin_unlock_irqrestore(&desc->lock, flags);
  58. }
  59. EXPORT_SYMBOL(disable_irq_nosync);
  60. /**
  61. * disable_irq - disable an irq and wait for completion
  62. * @irq: Interrupt to disable
  63. *
  64. * Disable the selected interrupt line. Enables and Disables are
  65. * nested.
  66. * This function waits for any pending IRQ handlers for this interrupt
  67. * to complete before returning. If you use this function while
  68. * holding a resource the IRQ handler may need you will deadlock.
  69. *
  70. * This function may be called - with care - from IRQ context.
  71. */
  72. void disable_irq(unsigned int irq)
  73. {
  74. struct irq_desc *desc = irq_desc + irq;
  75. if (irq >= NR_IRQS)
  76. return;
  77. disable_irq_nosync(irq);
  78. if (desc->action)
  79. synchronize_irq(irq);
  80. }
  81. EXPORT_SYMBOL(disable_irq);
  82. /**
  83. * enable_irq - enable handling of an irq
  84. * @irq: Interrupt to enable
  85. *
  86. * Undoes the effect of one call to disable_irq(). If this
  87. * matches the last disable, processing of interrupts on this
  88. * IRQ line is re-enabled.
  89. *
  90. * This function may be called from IRQ context.
  91. */
  92. void enable_irq(unsigned int irq)
  93. {
  94. struct irq_desc *desc = irq_desc + irq;
  95. unsigned long flags;
  96. if (irq >= NR_IRQS)
  97. return;
  98. spin_lock_irqsave(&desc->lock, flags);
  99. switch (desc->depth) {
  100. case 0:
  101. printk(KERN_WARNING "Unbalanced enable for IRQ %d\n", irq);
  102. WARN_ON(1);
  103. break;
  104. case 1: {
  105. unsigned int status = desc->status & ~IRQ_DISABLED;
  106. /* Prevent probing on this irq: */
  107. desc->status = status | IRQ_NOPROBE;
  108. check_irq_resend(desc, irq);
  109. /* fall-through */
  110. }
  111. default:
  112. desc->depth--;
  113. }
  114. spin_unlock_irqrestore(&desc->lock, flags);
  115. }
  116. EXPORT_SYMBOL(enable_irq);
  117. /**
  118. * set_irq_wake - control irq power management wakeup
  119. * @irq: interrupt to control
  120. * @on: enable/disable power management wakeup
  121. *
  122. * Enable/disable power management wakeup mode, which is
  123. * disabled by default. Enables and disables must match,
  124. * just as they match for non-wakeup mode support.
  125. *
  126. * Wakeup mode lets this IRQ wake the system from sleep
  127. * states like "suspend to RAM".
  128. */
  129. int set_irq_wake(unsigned int irq, unsigned int on)
  130. {
  131. struct irq_desc *desc = irq_desc + irq;
  132. unsigned long flags;
  133. int ret = -ENXIO;
  134. int (*set_wake)(unsigned, unsigned) = desc->chip->set_wake;
  135. /* wakeup-capable irqs can be shared between drivers that
  136. * don't need to have the same sleep mode behaviors.
  137. */
  138. spin_lock_irqsave(&desc->lock, flags);
  139. if (on) {
  140. if (desc->wake_depth++ == 0)
  141. desc->status |= IRQ_WAKEUP;
  142. else
  143. set_wake = NULL;
  144. } else {
  145. if (desc->wake_depth == 0) {
  146. printk(KERN_WARNING "Unbalanced IRQ %d "
  147. "wake disable\n", irq);
  148. WARN_ON(1);
  149. } else if (--desc->wake_depth == 0)
  150. desc->status &= ~IRQ_WAKEUP;
  151. else
  152. set_wake = NULL;
  153. }
  154. if (set_wake)
  155. ret = desc->chip->set_wake(irq, on);
  156. spin_unlock_irqrestore(&desc->lock, flags);
  157. return ret;
  158. }
  159. EXPORT_SYMBOL(set_irq_wake);
  160. /*
  161. * Internal function that tells the architecture code whether a
  162. * particular irq has been exclusively allocated or is available
  163. * for driver use.
  164. */
  165. int can_request_irq(unsigned int irq, unsigned long irqflags)
  166. {
  167. struct irqaction *action;
  168. if (irq >= NR_IRQS || irq_desc[irq].status & IRQ_NOREQUEST)
  169. return 0;
  170. action = irq_desc[irq].action;
  171. if (action)
  172. if (irqflags & action->flags & IRQF_SHARED)
  173. action = NULL;
  174. return !action;
  175. }
  176. void compat_irq_chip_set_default_handler(struct irq_desc *desc)
  177. {
  178. /*
  179. * If the architecture still has not overriden
  180. * the flow handler then zap the default. This
  181. * should catch incorrect flow-type setting.
  182. */
  183. if (desc->handle_irq == &handle_bad_irq)
  184. desc->handle_irq = NULL;
  185. }
  186. /*
  187. * Internal function to register an irqaction - typically used to
  188. * allocate special interrupts that are part of the architecture.
  189. */
  190. int setup_irq(unsigned int irq, struct irqaction *new)
  191. {
  192. struct irq_desc *desc = irq_desc + irq;
  193. struct irqaction *old, **p;
  194. const char *old_name = NULL;
  195. unsigned long flags;
  196. int shared = 0;
  197. if (irq >= NR_IRQS)
  198. return -EINVAL;
  199. if (desc->chip == &no_irq_chip)
  200. return -ENOSYS;
  201. /*
  202. * Some drivers like serial.c use request_irq() heavily,
  203. * so we have to be careful not to interfere with a
  204. * running system.
  205. */
  206. if (new->flags & IRQF_SAMPLE_RANDOM) {
  207. /*
  208. * This function might sleep, we want to call it first,
  209. * outside of the atomic block.
  210. * Yes, this might clear the entropy pool if the wrong
  211. * driver is attempted to be loaded, without actually
  212. * installing a new handler, but is this really a problem,
  213. * only the sysadmin is able to do this.
  214. */
  215. rand_initialize_irq(irq);
  216. }
  217. /*
  218. * The following block of code has to be executed atomically
  219. */
  220. spin_lock_irqsave(&desc->lock, flags);
  221. p = &desc->action;
  222. old = *p;
  223. if (old) {
  224. /*
  225. * Can't share interrupts unless both agree to and are
  226. * the same type (level, edge, polarity). So both flag
  227. * fields must have IRQF_SHARED set and the bits which
  228. * set the trigger type must match.
  229. */
  230. if (!((old->flags & new->flags) & IRQF_SHARED) ||
  231. ((old->flags ^ new->flags) & IRQF_TRIGGER_MASK)) {
  232. old_name = old->name;
  233. goto mismatch;
  234. }
  235. #if defined(CONFIG_IRQ_PER_CPU)
  236. /* All handlers must agree on per-cpuness */
  237. if ((old->flags & IRQF_PERCPU) !=
  238. (new->flags & IRQF_PERCPU))
  239. goto mismatch;
  240. #endif
  241. /* add new interrupt at end of irq queue */
  242. do {
  243. p = &old->next;
  244. old = *p;
  245. } while (old);
  246. shared = 1;
  247. }
  248. *p = new;
  249. #if defined(CONFIG_IRQ_PER_CPU)
  250. if (new->flags & IRQF_PERCPU)
  251. desc->status |= IRQ_PER_CPU;
  252. #endif
  253. if (!shared) {
  254. irq_chip_set_defaults(desc->chip);
  255. /* Setup the type (level, edge polarity) if configured: */
  256. if (new->flags & IRQF_TRIGGER_MASK) {
  257. if (desc->chip && desc->chip->set_type)
  258. desc->chip->set_type(irq,
  259. new->flags & IRQF_TRIGGER_MASK);
  260. else
  261. /*
  262. * IRQF_TRIGGER_* but the PIC does not support
  263. * multiple flow-types?
  264. */
  265. printk(KERN_WARNING "No IRQF_TRIGGER set_type "
  266. "function for IRQ %d (%s)\n", irq,
  267. desc->chip ? desc->chip->name :
  268. "unknown");
  269. } else
  270. compat_irq_chip_set_default_handler(desc);
  271. desc->status &= ~(IRQ_AUTODETECT | IRQ_WAITING |
  272. IRQ_INPROGRESS);
  273. if (!(desc->status & IRQ_NOAUTOEN)) {
  274. desc->depth = 0;
  275. desc->status &= ~IRQ_DISABLED;
  276. if (desc->chip->startup)
  277. desc->chip->startup(irq);
  278. else
  279. desc->chip->enable(irq);
  280. } else
  281. /* Undo nested disables: */
  282. desc->depth = 1;
  283. }
  284. /* Reset broken irq detection when installing new handler */
  285. desc->irq_count = 0;
  286. desc->irqs_unhandled = 0;
  287. spin_unlock_irqrestore(&desc->lock, flags);
  288. new->irq = irq;
  289. register_irq_proc(irq);
  290. new->dir = NULL;
  291. register_handler_proc(irq, new);
  292. return 0;
  293. mismatch:
  294. if (!(new->flags & IRQF_PROBE_SHARED)) {
  295. printk(KERN_ERR "IRQ handler type mismatch for IRQ %d\n", irq);
  296. if (old_name)
  297. printk(KERN_ERR "current handler: %s\n", old_name);
  298. dump_stack();
  299. }
  300. spin_unlock_irqrestore(&desc->lock, flags);
  301. return -EBUSY;
  302. }
  303. /**
  304. * free_irq - free an interrupt
  305. * @irq: Interrupt line to free
  306. * @dev_id: Device identity to free
  307. *
  308. * Remove an interrupt handler. The handler is removed and if the
  309. * interrupt line is no longer in use by any driver it is disabled.
  310. * On a shared IRQ the caller must ensure the interrupt is disabled
  311. * on the card it drives before calling this function. The function
  312. * does not return until any executing interrupts for this IRQ
  313. * have completed.
  314. *
  315. * This function must not be called from interrupt context.
  316. */
  317. void free_irq(unsigned int irq, void *dev_id)
  318. {
  319. struct irq_desc *desc;
  320. struct irqaction **p;
  321. unsigned long flags;
  322. WARN_ON(in_interrupt());
  323. if (irq >= NR_IRQS)
  324. return;
  325. desc = irq_desc + irq;
  326. spin_lock_irqsave(&desc->lock, flags);
  327. p = &desc->action;
  328. for (;;) {
  329. struct irqaction *action = *p;
  330. if (action) {
  331. struct irqaction **pp = p;
  332. p = &action->next;
  333. if (action->dev_id != dev_id)
  334. continue;
  335. /* Found it - now remove it from the list of entries */
  336. *pp = action->next;
  337. /* Currently used only by UML, might disappear one day.*/
  338. #ifdef CONFIG_IRQ_RELEASE_METHOD
  339. if (desc->chip->release)
  340. desc->chip->release(irq, dev_id);
  341. #endif
  342. if (!desc->action) {
  343. desc->status |= IRQ_DISABLED;
  344. if (desc->chip->shutdown)
  345. desc->chip->shutdown(irq);
  346. else
  347. desc->chip->disable(irq);
  348. }
  349. spin_unlock_irqrestore(&desc->lock, flags);
  350. unregister_handler_proc(irq, action);
  351. /* Make sure it's not being used on another CPU */
  352. synchronize_irq(irq);
  353. kfree(action);
  354. return;
  355. }
  356. printk(KERN_ERR "Trying to free already-free IRQ %d\n", irq);
  357. spin_unlock_irqrestore(&desc->lock, flags);
  358. return;
  359. }
  360. }
  361. EXPORT_SYMBOL(free_irq);
  362. /**
  363. * request_irq - allocate an interrupt line
  364. * @irq: Interrupt line to allocate
  365. * @handler: Function to be called when the IRQ occurs
  366. * @irqflags: Interrupt type flags
  367. * @devname: An ascii name for the claiming device
  368. * @dev_id: A cookie passed back to the handler function
  369. *
  370. * This call allocates interrupt resources and enables the
  371. * interrupt line and IRQ handling. From the point this
  372. * call is made your handler function may be invoked. Since
  373. * your handler function must clear any interrupt the board
  374. * raises, you must take care both to initialise your hardware
  375. * and to set up the interrupt handler in the right order.
  376. *
  377. * Dev_id must be globally unique. Normally the address of the
  378. * device data structure is used as the cookie. Since the handler
  379. * receives this value it makes sense to use it.
  380. *
  381. * If your interrupt is shared you must pass a non NULL dev_id
  382. * as this is required when freeing the interrupt.
  383. *
  384. * Flags:
  385. *
  386. * IRQF_SHARED Interrupt is shared
  387. * IRQF_DISABLED Disable local interrupts while processing
  388. * IRQF_SAMPLE_RANDOM The interrupt can be used for entropy
  389. *
  390. */
  391. int request_irq(unsigned int irq, irq_handler_t handler,
  392. unsigned long irqflags, const char *devname, void *dev_id)
  393. {
  394. struct irqaction *action;
  395. int retval;
  396. #ifdef CONFIG_LOCKDEP
  397. /*
  398. * Lockdep wants atomic interrupt handlers:
  399. */
  400. irqflags |= SA_INTERRUPT;
  401. #endif
  402. /*
  403. * Sanity-check: shared interrupts must pass in a real dev-ID,
  404. * otherwise we'll have trouble later trying to figure out
  405. * which interrupt is which (messes up the interrupt freeing
  406. * logic etc).
  407. */
  408. if ((irqflags & IRQF_SHARED) && !dev_id)
  409. return -EINVAL;
  410. if (irq >= NR_IRQS)
  411. return -EINVAL;
  412. if (irq_desc[irq].status & IRQ_NOREQUEST)
  413. return -EINVAL;
  414. if (!handler)
  415. return -EINVAL;
  416. action = kmalloc(sizeof(struct irqaction), GFP_ATOMIC);
  417. if (!action)
  418. return -ENOMEM;
  419. action->handler = handler;
  420. action->flags = irqflags;
  421. cpus_clear(action->mask);
  422. action->name = devname;
  423. action->next = NULL;
  424. action->dev_id = dev_id;
  425. select_smp_affinity(irq);
  426. retval = setup_irq(irq, action);
  427. if (retval)
  428. kfree(action);
  429. return retval;
  430. }
  431. EXPORT_SYMBOL(request_irq);
  432. /*
  433. * Device resource management aware IRQ request/free implementation.
  434. */
  435. struct irq_devres {
  436. unsigned int irq;
  437. void *dev_id;
  438. };
  439. static void devm_irq_release(struct device *dev, void *res)
  440. {
  441. struct irq_devres *this = res;
  442. free_irq(this->irq, this->dev_id);
  443. }
  444. static int devm_irq_match(struct device *dev, void *res, void *data)
  445. {
  446. struct irq_devres *this = res, *match = data;
  447. return this->irq == match->irq && this->dev_id == match->dev_id;
  448. }
  449. /**
  450. * devm_request_irq - allocate an interrupt line for a managed device
  451. * @dev: device to request interrupt for
  452. * @irq: Interrupt line to allocate
  453. * @handler: Function to be called when the IRQ occurs
  454. * @irqflags: Interrupt type flags
  455. * @devname: An ascii name for the claiming device
  456. * @dev_id: A cookie passed back to the handler function
  457. *
  458. * Except for the extra @dev argument, this function takes the
  459. * same arguments and performs the same function as
  460. * request_irq(). IRQs requested with this function will be
  461. * automatically freed on driver detach.
  462. *
  463. * If an IRQ allocated with this function needs to be freed
  464. * separately, dev_free_irq() must be used.
  465. */
  466. int devm_request_irq(struct device *dev, unsigned int irq,
  467. irq_handler_t handler, unsigned long irqflags,
  468. const char *devname, void *dev_id)
  469. {
  470. struct irq_devres *dr;
  471. int rc;
  472. dr = devres_alloc(devm_irq_release, sizeof(struct irq_devres),
  473. GFP_KERNEL);
  474. if (!dr)
  475. return -ENOMEM;
  476. rc = request_irq(irq, handler, irqflags, devname, dev_id);
  477. if (rc) {
  478. kfree(dr);
  479. return rc;
  480. }
  481. dr->irq = irq;
  482. dr->dev_id = dev_id;
  483. devres_add(dev, dr);
  484. return 0;
  485. }
  486. EXPORT_SYMBOL(devm_request_irq);
  487. /**
  488. * devm_free_irq - free an interrupt
  489. * @dev: device to free interrupt for
  490. * @irq: Interrupt line to free
  491. * @dev_id: Device identity to free
  492. *
  493. * Except for the extra @dev argument, this function takes the
  494. * same arguments and performs the same function as free_irq().
  495. * This function instead of free_irq() should be used to manually
  496. * free IRQs allocated with dev_request_irq().
  497. */
  498. void devm_free_irq(struct device *dev, unsigned int irq, void *dev_id)
  499. {
  500. struct irq_devres match_data = { irq, dev_id };
  501. free_irq(irq, dev_id);
  502. WARN_ON(devres_destroy(dev, devm_irq_release, devm_irq_match,
  503. &match_data));
  504. }
  505. EXPORT_SYMBOL(devm_free_irq);