manage.c 15 KB

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