manage.c 17 KB

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