manage.c 17 KB

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