manage.c 14 KB

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