manage.c 14 KB

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