manage.c 15 KB

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