manage.c 16 KB

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