manage.c 14 KB

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