manage.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  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/kthread.h>
  11. #include <linux/module.h>
  12. #include <linux/random.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/slab.h>
  15. #include <linux/sched.h>
  16. #include "internals.h"
  17. /**
  18. * synchronize_irq - wait for pending IRQ handlers (on other CPUs)
  19. * @irq: interrupt number to wait for
  20. *
  21. * This function waits for any pending IRQ handlers for this interrupt
  22. * to complete before returning. If you use this function while
  23. * holding a resource the IRQ handler may need you will deadlock.
  24. *
  25. * This function may be called - with care - from IRQ context.
  26. */
  27. void synchronize_irq(unsigned int irq)
  28. {
  29. struct irq_desc *desc = irq_to_desc(irq);
  30. unsigned int status;
  31. if (!desc)
  32. return;
  33. do {
  34. unsigned long flags;
  35. /*
  36. * Wait until we're out of the critical section. This might
  37. * give the wrong answer due to the lack of memory barriers.
  38. */
  39. while (desc->status & IRQ_INPROGRESS)
  40. cpu_relax();
  41. /* Ok, that indicated we're done: double-check carefully. */
  42. spin_lock_irqsave(&desc->lock, flags);
  43. status = desc->status;
  44. spin_unlock_irqrestore(&desc->lock, flags);
  45. /* Oops, that failed? */
  46. } while (status & IRQ_INPROGRESS);
  47. /*
  48. * We made sure that no hardirq handler is running. Now verify
  49. * that no threaded handlers are active.
  50. */
  51. wait_event(desc->wait_for_threads, !atomic_read(&desc->threads_active));
  52. }
  53. EXPORT_SYMBOL(synchronize_irq);
  54. #ifdef CONFIG_SMP
  55. cpumask_var_t irq_default_affinity;
  56. /**
  57. * irq_can_set_affinity - Check if the affinity of a given irq can be set
  58. * @irq: Interrupt to check
  59. *
  60. */
  61. int irq_can_set_affinity(unsigned int irq)
  62. {
  63. struct irq_desc *desc = irq_to_desc(irq);
  64. if (CHECK_IRQ_PER_CPU(desc->status) || !desc->chip ||
  65. !desc->chip->set_affinity)
  66. return 0;
  67. return 1;
  68. }
  69. static void
  70. irq_set_thread_affinity(struct irq_desc *desc, const struct cpumask *cpumask)
  71. {
  72. struct irqaction *action = desc->action;
  73. while (action) {
  74. if (action->thread)
  75. set_cpus_allowed_ptr(action->thread, cpumask);
  76. action = action->next;
  77. }
  78. }
  79. /**
  80. * irq_set_affinity - Set the irq affinity of a given irq
  81. * @irq: Interrupt to set affinity
  82. * @cpumask: cpumask
  83. *
  84. */
  85. int irq_set_affinity(unsigned int irq, const struct cpumask *cpumask)
  86. {
  87. struct irq_desc *desc = irq_to_desc(irq);
  88. unsigned long flags;
  89. if (!desc->chip->set_affinity)
  90. return -EINVAL;
  91. spin_lock_irqsave(&desc->lock, flags);
  92. #ifdef CONFIG_GENERIC_PENDING_IRQ
  93. if (desc->status & IRQ_MOVE_PCNTXT || desc->status & IRQ_DISABLED) {
  94. cpumask_copy(desc->affinity, cpumask);
  95. desc->chip->set_affinity(irq, cpumask);
  96. } else {
  97. desc->status |= IRQ_MOVE_PENDING;
  98. cpumask_copy(desc->pending_mask, cpumask);
  99. }
  100. #else
  101. cpumask_copy(desc->affinity, cpumask);
  102. desc->chip->set_affinity(irq, cpumask);
  103. #endif
  104. irq_set_thread_affinity(desc, cpumask);
  105. desc->status |= IRQ_AFFINITY_SET;
  106. spin_unlock_irqrestore(&desc->lock, flags);
  107. return 0;
  108. }
  109. #ifndef CONFIG_AUTO_IRQ_AFFINITY
  110. /*
  111. * Generic version of the affinity autoselector.
  112. */
  113. static int setup_affinity(unsigned int irq, struct irq_desc *desc)
  114. {
  115. if (!irq_can_set_affinity(irq))
  116. return 0;
  117. /*
  118. * Preserve an userspace affinity setup, but make sure that
  119. * one of the targets is online.
  120. */
  121. if (desc->status & (IRQ_AFFINITY_SET | IRQ_NO_BALANCING)) {
  122. if (cpumask_any_and(desc->affinity, cpu_online_mask)
  123. < nr_cpu_ids)
  124. goto set_affinity;
  125. else
  126. desc->status &= ~IRQ_AFFINITY_SET;
  127. }
  128. cpumask_and(desc->affinity, cpu_online_mask, irq_default_affinity);
  129. set_affinity:
  130. desc->chip->set_affinity(irq, desc->affinity);
  131. return 0;
  132. }
  133. #else
  134. static inline int setup_affinity(unsigned int irq, struct irq_desc *d)
  135. {
  136. return irq_select_affinity(irq);
  137. }
  138. #endif
  139. /*
  140. * Called when affinity is set via /proc/irq
  141. */
  142. int irq_select_affinity_usr(unsigned int irq)
  143. {
  144. struct irq_desc *desc = irq_to_desc(irq);
  145. unsigned long flags;
  146. int ret;
  147. spin_lock_irqsave(&desc->lock, flags);
  148. ret = setup_affinity(irq, desc);
  149. if (!ret)
  150. irq_set_thread_affinity(desc, desc->affinity);
  151. spin_unlock_irqrestore(&desc->lock, flags);
  152. return ret;
  153. }
  154. #else
  155. static inline int setup_affinity(unsigned int irq, struct irq_desc *desc)
  156. {
  157. return 0;
  158. }
  159. #endif
  160. void __disable_irq(struct irq_desc *desc, unsigned int irq, bool suspend)
  161. {
  162. if (suspend) {
  163. if (!desc->action || (desc->action->flags & IRQF_TIMER))
  164. return;
  165. desc->status |= IRQ_SUSPENDED;
  166. }
  167. if (!desc->depth++) {
  168. desc->status |= IRQ_DISABLED;
  169. desc->chip->disable(irq);
  170. }
  171. }
  172. /**
  173. * disable_irq_nosync - disable an irq without waiting
  174. * @irq: Interrupt to disable
  175. *
  176. * Disable the selected interrupt line. Disables and Enables are
  177. * nested.
  178. * Unlike disable_irq(), this function does not ensure existing
  179. * instances of the IRQ handler have completed before returning.
  180. *
  181. * This function may be called from IRQ context.
  182. */
  183. void disable_irq_nosync(unsigned int irq)
  184. {
  185. struct irq_desc *desc = irq_to_desc(irq);
  186. unsigned long flags;
  187. if (!desc)
  188. return;
  189. spin_lock_irqsave(&desc->lock, flags);
  190. __disable_irq(desc, irq, false);
  191. spin_unlock_irqrestore(&desc->lock, flags);
  192. }
  193. EXPORT_SYMBOL(disable_irq_nosync);
  194. /**
  195. * disable_irq - disable an irq and wait for completion
  196. * @irq: Interrupt to disable
  197. *
  198. * Disable the selected interrupt line. Enables and Disables are
  199. * nested.
  200. * This function waits for any pending IRQ handlers for this interrupt
  201. * to complete before returning. If you use this function while
  202. * holding a resource the IRQ handler may need you will deadlock.
  203. *
  204. * This function may be called - with care - from IRQ context.
  205. */
  206. void disable_irq(unsigned int irq)
  207. {
  208. struct irq_desc *desc = irq_to_desc(irq);
  209. if (!desc)
  210. return;
  211. disable_irq_nosync(irq);
  212. if (desc->action)
  213. synchronize_irq(irq);
  214. }
  215. EXPORT_SYMBOL(disable_irq);
  216. void __enable_irq(struct irq_desc *desc, unsigned int irq, bool resume)
  217. {
  218. if (resume)
  219. desc->status &= ~IRQ_SUSPENDED;
  220. switch (desc->depth) {
  221. case 0:
  222. err_out:
  223. WARN(1, KERN_WARNING "Unbalanced enable for IRQ %d\n", irq);
  224. break;
  225. case 1: {
  226. unsigned int status = desc->status & ~IRQ_DISABLED;
  227. if (desc->status & IRQ_SUSPENDED)
  228. goto err_out;
  229. /* Prevent probing on this irq: */
  230. desc->status = status | IRQ_NOPROBE;
  231. check_irq_resend(desc, irq);
  232. /* fall-through */
  233. }
  234. default:
  235. desc->depth--;
  236. }
  237. }
  238. /**
  239. * enable_irq - enable handling of an irq
  240. * @irq: Interrupt to enable
  241. *
  242. * Undoes the effect of one call to disable_irq(). If this
  243. * matches the last disable, processing of interrupts on this
  244. * IRQ line is re-enabled.
  245. *
  246. * This function may be called from IRQ context.
  247. */
  248. void enable_irq(unsigned int irq)
  249. {
  250. struct irq_desc *desc = irq_to_desc(irq);
  251. unsigned long flags;
  252. if (!desc)
  253. return;
  254. spin_lock_irqsave(&desc->lock, flags);
  255. __enable_irq(desc, irq, false);
  256. spin_unlock_irqrestore(&desc->lock, flags);
  257. }
  258. EXPORT_SYMBOL(enable_irq);
  259. static int set_irq_wake_real(unsigned int irq, unsigned int on)
  260. {
  261. struct irq_desc *desc = irq_to_desc(irq);
  262. int ret = -ENXIO;
  263. if (desc->chip->set_wake)
  264. ret = desc->chip->set_wake(irq, on);
  265. return ret;
  266. }
  267. /**
  268. * set_irq_wake - control irq power management wakeup
  269. * @irq: interrupt to control
  270. * @on: enable/disable power management wakeup
  271. *
  272. * Enable/disable power management wakeup mode, which is
  273. * disabled by default. Enables and disables must match,
  274. * just as they match for non-wakeup mode support.
  275. *
  276. * Wakeup mode lets this IRQ wake the system from sleep
  277. * states like "suspend to RAM".
  278. */
  279. int set_irq_wake(unsigned int irq, unsigned int on)
  280. {
  281. struct irq_desc *desc = irq_to_desc(irq);
  282. unsigned long flags;
  283. int ret = 0;
  284. /* wakeup-capable irqs can be shared between drivers that
  285. * don't need to have the same sleep mode behaviors.
  286. */
  287. spin_lock_irqsave(&desc->lock, flags);
  288. if (on) {
  289. if (desc->wake_depth++ == 0) {
  290. ret = set_irq_wake_real(irq, on);
  291. if (ret)
  292. desc->wake_depth = 0;
  293. else
  294. desc->status |= IRQ_WAKEUP;
  295. }
  296. } else {
  297. if (desc->wake_depth == 0) {
  298. WARN(1, "Unbalanced IRQ %d wake disable\n", irq);
  299. } else if (--desc->wake_depth == 0) {
  300. ret = set_irq_wake_real(irq, on);
  301. if (ret)
  302. desc->wake_depth = 1;
  303. else
  304. desc->status &= ~IRQ_WAKEUP;
  305. }
  306. }
  307. spin_unlock_irqrestore(&desc->lock, flags);
  308. return ret;
  309. }
  310. EXPORT_SYMBOL(set_irq_wake);
  311. /*
  312. * Internal function that tells the architecture code whether a
  313. * particular irq has been exclusively allocated or is available
  314. * for driver use.
  315. */
  316. int can_request_irq(unsigned int irq, unsigned long irqflags)
  317. {
  318. struct irq_desc *desc = irq_to_desc(irq);
  319. struct irqaction *action;
  320. if (!desc)
  321. return 0;
  322. if (desc->status & IRQ_NOREQUEST)
  323. return 0;
  324. action = desc->action;
  325. if (action)
  326. if (irqflags & action->flags & IRQF_SHARED)
  327. action = NULL;
  328. return !action;
  329. }
  330. void compat_irq_chip_set_default_handler(struct irq_desc *desc)
  331. {
  332. /*
  333. * If the architecture still has not overriden
  334. * the flow handler then zap the default. This
  335. * should catch incorrect flow-type setting.
  336. */
  337. if (desc->handle_irq == &handle_bad_irq)
  338. desc->handle_irq = NULL;
  339. }
  340. int __irq_set_trigger(struct irq_desc *desc, unsigned int irq,
  341. unsigned long flags)
  342. {
  343. int ret;
  344. struct irq_chip *chip = desc->chip;
  345. if (!chip || !chip->set_type) {
  346. /*
  347. * IRQF_TRIGGER_* but the PIC does not support multiple
  348. * flow-types?
  349. */
  350. pr_debug("No set_type function for IRQ %d (%s)\n", irq,
  351. chip ? (chip->name ? : "unknown") : "unknown");
  352. return 0;
  353. }
  354. /* caller masked out all except trigger mode flags */
  355. ret = chip->set_type(irq, flags);
  356. if (ret)
  357. pr_err("setting trigger mode %d for irq %u failed (%pF)\n",
  358. (int)flags, irq, chip->set_type);
  359. else {
  360. if (flags & (IRQ_TYPE_LEVEL_LOW | IRQ_TYPE_LEVEL_HIGH))
  361. flags |= IRQ_LEVEL;
  362. /* note that IRQF_TRIGGER_MASK == IRQ_TYPE_SENSE_MASK */
  363. desc->status &= ~(IRQ_LEVEL | IRQ_TYPE_SENSE_MASK);
  364. desc->status |= flags;
  365. }
  366. return ret;
  367. }
  368. static int irq_wait_for_interrupt(struct irqaction *action)
  369. {
  370. while (!kthread_should_stop()) {
  371. set_current_state(TASK_INTERRUPTIBLE);
  372. if (test_and_clear_bit(IRQTF_RUNTHREAD,
  373. &action->thread_flags)) {
  374. __set_current_state(TASK_RUNNING);
  375. return 0;
  376. }
  377. schedule();
  378. }
  379. return -1;
  380. }
  381. /*
  382. * Interrupt handler thread
  383. */
  384. static int irq_thread(void *data)
  385. {
  386. struct sched_param param = { .sched_priority = MAX_USER_RT_PRIO/2, };
  387. struct irqaction *action = data;
  388. struct irq_desc *desc = irq_to_desc(action->irq);
  389. int wake;
  390. sched_setscheduler(current, SCHED_FIFO, &param);
  391. current->irqaction = action;
  392. while (!irq_wait_for_interrupt(action)) {
  393. atomic_inc(&desc->threads_active);
  394. spin_lock_irq(&desc->lock);
  395. if (unlikely(desc->status & IRQ_DISABLED)) {
  396. /*
  397. * CHECKME: We might need a dedicated
  398. * IRQ_THREAD_PENDING flag here, which
  399. * retriggers the thread in check_irq_resend()
  400. * but AFAICT IRQ_PENDING should be fine as it
  401. * retriggers the interrupt itself --- tglx
  402. */
  403. desc->status |= IRQ_PENDING;
  404. spin_unlock_irq(&desc->lock);
  405. } else {
  406. spin_unlock_irq(&desc->lock);
  407. action->thread_fn(action->irq, action->dev_id);
  408. }
  409. wake = atomic_dec_and_test(&desc->threads_active);
  410. if (wake && waitqueue_active(&desc->wait_for_threads))
  411. wake_up(&desc->wait_for_threads);
  412. }
  413. /*
  414. * Clear irqaction. Otherwise exit_irq_thread() would make
  415. * fuzz about an active irq thread going into nirvana.
  416. */
  417. current->irqaction = NULL;
  418. return 0;
  419. }
  420. /*
  421. * Called from do_exit()
  422. */
  423. void exit_irq_thread(void)
  424. {
  425. struct task_struct *tsk = current;
  426. if (!tsk->irqaction)
  427. return;
  428. printk(KERN_ERR
  429. "exiting task \"%s\" (%d) is an active IRQ thread (irq %d)\n",
  430. tsk->comm ? tsk->comm : "", tsk->pid, tsk->irqaction->irq);
  431. /*
  432. * Set the THREAD DIED flag to prevent further wakeups of the
  433. * soon to be gone threaded handler.
  434. */
  435. set_bit(IRQTF_DIED, &tsk->irqaction->flags);
  436. }
  437. /*
  438. * Internal function to register an irqaction - typically used to
  439. * allocate special interrupts that are part of the architecture.
  440. */
  441. static int
  442. __setup_irq(unsigned int irq, struct irq_desc *desc, struct irqaction *new)
  443. {
  444. struct irqaction *old, **old_ptr;
  445. const char *old_name = NULL;
  446. unsigned long flags;
  447. int shared = 0;
  448. int ret;
  449. if (!desc)
  450. return -EINVAL;
  451. if (desc->chip == &no_irq_chip)
  452. return -ENOSYS;
  453. /*
  454. * Some drivers like serial.c use request_irq() heavily,
  455. * so we have to be careful not to interfere with a
  456. * running system.
  457. */
  458. if (new->flags & IRQF_SAMPLE_RANDOM) {
  459. /*
  460. * This function might sleep, we want to call it first,
  461. * outside of the atomic block.
  462. * Yes, this might clear the entropy pool if the wrong
  463. * driver is attempted to be loaded, without actually
  464. * installing a new handler, but is this really a problem,
  465. * only the sysadmin is able to do this.
  466. */
  467. rand_initialize_irq(irq);
  468. }
  469. /*
  470. * Threaded handler ?
  471. */
  472. if (new->thread_fn) {
  473. struct task_struct *t;
  474. t = kthread_create(irq_thread, new, "irq/%d-%s", irq,
  475. new->name);
  476. if (IS_ERR(t))
  477. return PTR_ERR(t);
  478. /*
  479. * We keep the reference to the task struct even if
  480. * the thread dies to avoid that the interrupt code
  481. * references an already freed task_struct.
  482. */
  483. get_task_struct(t);
  484. new->thread = t;
  485. wake_up_process(t);
  486. }
  487. /*
  488. * The following block of code has to be executed atomically
  489. */
  490. spin_lock_irqsave(&desc->lock, flags);
  491. old_ptr = &desc->action;
  492. old = *old_ptr;
  493. if (old) {
  494. /*
  495. * Can't share interrupts unless both agree to and are
  496. * the same type (level, edge, polarity). So both flag
  497. * fields must have IRQF_SHARED set and the bits which
  498. * set the trigger type must match.
  499. */
  500. if (!((old->flags & new->flags) & IRQF_SHARED) ||
  501. ((old->flags ^ new->flags) & IRQF_TRIGGER_MASK)) {
  502. old_name = old->name;
  503. goto mismatch;
  504. }
  505. #if defined(CONFIG_IRQ_PER_CPU)
  506. /* All handlers must agree on per-cpuness */
  507. if ((old->flags & IRQF_PERCPU) !=
  508. (new->flags & IRQF_PERCPU))
  509. goto mismatch;
  510. #endif
  511. /* add new interrupt at end of irq queue */
  512. do {
  513. old_ptr = &old->next;
  514. old = *old_ptr;
  515. } while (old);
  516. shared = 1;
  517. }
  518. if (!shared) {
  519. irq_chip_set_defaults(desc->chip);
  520. init_waitqueue_head(&desc->wait_for_threads);
  521. /* Setup the type (level, edge polarity) if configured: */
  522. if (new->flags & IRQF_TRIGGER_MASK) {
  523. ret = __irq_set_trigger(desc, irq,
  524. new->flags & IRQF_TRIGGER_MASK);
  525. if (ret)
  526. goto out_thread;
  527. } else
  528. compat_irq_chip_set_default_handler(desc);
  529. #if defined(CONFIG_IRQ_PER_CPU)
  530. if (new->flags & IRQF_PERCPU)
  531. desc->status |= IRQ_PER_CPU;
  532. #endif
  533. desc->status &= ~(IRQ_AUTODETECT | IRQ_WAITING |
  534. IRQ_INPROGRESS | IRQ_SPURIOUS_DISABLED);
  535. if (!(desc->status & IRQ_NOAUTOEN)) {
  536. desc->depth = 0;
  537. desc->status &= ~IRQ_DISABLED;
  538. desc->chip->startup(irq);
  539. } else
  540. /* Undo nested disables: */
  541. desc->depth = 1;
  542. /* Exclude IRQ from balancing if requested */
  543. if (new->flags & IRQF_NOBALANCING)
  544. desc->status |= IRQ_NO_BALANCING;
  545. /* Set default affinity mask once everything is setup */
  546. setup_affinity(irq, desc);
  547. } else if ((new->flags & IRQF_TRIGGER_MASK)
  548. && (new->flags & IRQF_TRIGGER_MASK)
  549. != (desc->status & IRQ_TYPE_SENSE_MASK)) {
  550. /* hope the handler works with the actual trigger mode... */
  551. pr_warning("IRQ %d uses trigger mode %d; requested %d\n",
  552. irq, (int)(desc->status & IRQ_TYPE_SENSE_MASK),
  553. (int)(new->flags & IRQF_TRIGGER_MASK));
  554. }
  555. *old_ptr = new;
  556. /* Reset broken irq detection when installing new handler */
  557. desc->irq_count = 0;
  558. desc->irqs_unhandled = 0;
  559. /*
  560. * Check whether we disabled the irq via the spurious handler
  561. * before. Reenable it and give it another chance.
  562. */
  563. if (shared && (desc->status & IRQ_SPURIOUS_DISABLED)) {
  564. desc->status &= ~IRQ_SPURIOUS_DISABLED;
  565. __enable_irq(desc, irq, false);
  566. }
  567. spin_unlock_irqrestore(&desc->lock, flags);
  568. new->irq = irq;
  569. register_irq_proc(irq, desc);
  570. new->dir = NULL;
  571. register_handler_proc(irq, new);
  572. return 0;
  573. mismatch:
  574. #ifdef CONFIG_DEBUG_SHIRQ
  575. if (!(new->flags & IRQF_PROBE_SHARED)) {
  576. printk(KERN_ERR "IRQ handler type mismatch for IRQ %d\n", irq);
  577. if (old_name)
  578. printk(KERN_ERR "current handler: %s\n", old_name);
  579. dump_stack();
  580. }
  581. #endif
  582. ret = -EBUSY;
  583. out_thread:
  584. spin_unlock_irqrestore(&desc->lock, flags);
  585. if (new->thread) {
  586. struct task_struct *t = new->thread;
  587. new->thread = NULL;
  588. if (likely(!test_bit(IRQTF_DIED, &new->thread_flags)))
  589. kthread_stop(t);
  590. put_task_struct(t);
  591. }
  592. return ret;
  593. }
  594. /**
  595. * setup_irq - setup an interrupt
  596. * @irq: Interrupt line to setup
  597. * @act: irqaction for the interrupt
  598. *
  599. * Used to statically setup interrupts in the early boot process.
  600. */
  601. int setup_irq(unsigned int irq, struct irqaction *act)
  602. {
  603. struct irq_desc *desc = irq_to_desc(irq);
  604. return __setup_irq(irq, desc, act);
  605. }
  606. EXPORT_SYMBOL_GPL(setup_irq);
  607. /*
  608. * Internal function to unregister an irqaction - used to free
  609. * regular and special interrupts that are part of the architecture.
  610. */
  611. static struct irqaction *__free_irq(unsigned int irq, void *dev_id)
  612. {
  613. struct irq_desc *desc = irq_to_desc(irq);
  614. struct irqaction *action, **action_ptr;
  615. struct task_struct *irqthread;
  616. unsigned long flags;
  617. WARN(in_interrupt(), "Trying to free IRQ %d from IRQ context!\n", irq);
  618. if (!desc)
  619. return NULL;
  620. spin_lock_irqsave(&desc->lock, flags);
  621. /*
  622. * There can be multiple actions per IRQ descriptor, find the right
  623. * one based on the dev_id:
  624. */
  625. action_ptr = &desc->action;
  626. for (;;) {
  627. action = *action_ptr;
  628. if (!action) {
  629. WARN(1, "Trying to free already-free IRQ %d\n", irq);
  630. spin_unlock_irqrestore(&desc->lock, flags);
  631. return NULL;
  632. }
  633. if (action->dev_id == dev_id)
  634. break;
  635. action_ptr = &action->next;
  636. }
  637. /* Found it - now remove it from the list of entries: */
  638. *action_ptr = action->next;
  639. /* Currently used only by UML, might disappear one day: */
  640. #ifdef CONFIG_IRQ_RELEASE_METHOD
  641. if (desc->chip->release)
  642. desc->chip->release(irq, dev_id);
  643. #endif
  644. /* If this was the last handler, shut down the IRQ line: */
  645. if (!desc->action) {
  646. desc->status |= IRQ_DISABLED;
  647. if (desc->chip->shutdown)
  648. desc->chip->shutdown(irq);
  649. else
  650. desc->chip->disable(irq);
  651. }
  652. irqthread = action->thread;
  653. action->thread = NULL;
  654. spin_unlock_irqrestore(&desc->lock, flags);
  655. unregister_handler_proc(irq, action);
  656. /* Make sure it's not being used on another CPU: */
  657. synchronize_irq(irq);
  658. if (irqthread) {
  659. if (!test_bit(IRQTF_DIED, &action->thread_flags))
  660. kthread_stop(irqthread);
  661. put_task_struct(irqthread);
  662. }
  663. #ifdef CONFIG_DEBUG_SHIRQ
  664. /*
  665. * It's a shared IRQ -- the driver ought to be prepared for an IRQ
  666. * event to happen even now it's being freed, so let's make sure that
  667. * is so by doing an extra call to the handler ....
  668. *
  669. * ( We do this after actually deregistering it, to make sure that a
  670. * 'real' IRQ doesn't run in * parallel with our fake. )
  671. */
  672. if (action->flags & IRQF_SHARED) {
  673. local_irq_save(flags);
  674. action->handler(irq, dev_id);
  675. local_irq_restore(flags);
  676. }
  677. #endif
  678. return action;
  679. }
  680. /**
  681. * remove_irq - free an interrupt
  682. * @irq: Interrupt line to free
  683. * @act: irqaction for the interrupt
  684. *
  685. * Used to remove interrupts statically setup by the early boot process.
  686. */
  687. void remove_irq(unsigned int irq, struct irqaction *act)
  688. {
  689. __free_irq(irq, act->dev_id);
  690. }
  691. EXPORT_SYMBOL_GPL(remove_irq);
  692. /**
  693. * free_irq - free an interrupt allocated with request_irq
  694. * @irq: Interrupt line to free
  695. * @dev_id: Device identity to free
  696. *
  697. * Remove an interrupt handler. The handler is removed and if the
  698. * interrupt line is no longer in use by any driver it is disabled.
  699. * On a shared IRQ the caller must ensure the interrupt is disabled
  700. * on the card it drives before calling this function. The function
  701. * does not return until any executing interrupts for this IRQ
  702. * have completed.
  703. *
  704. * This function must not be called from interrupt context.
  705. */
  706. void free_irq(unsigned int irq, void *dev_id)
  707. {
  708. kfree(__free_irq(irq, dev_id));
  709. }
  710. EXPORT_SYMBOL(free_irq);
  711. /**
  712. * request_threaded_irq - allocate an interrupt line
  713. * @irq: Interrupt line to allocate
  714. * @handler: Function to be called when the IRQ occurs.
  715. * Primary handler for threaded interrupts
  716. * @thread_fn: Function called from the irq handler thread
  717. * If NULL, no irq thread is created
  718. * @irqflags: Interrupt type flags
  719. * @devname: An ascii name for the claiming device
  720. * @dev_id: A cookie passed back to the handler function
  721. *
  722. * This call allocates interrupt resources and enables the
  723. * interrupt line and IRQ handling. From the point this
  724. * call is made your handler function may be invoked. Since
  725. * your handler function must clear any interrupt the board
  726. * raises, you must take care both to initialise your hardware
  727. * and to set up the interrupt handler in the right order.
  728. *
  729. * If you want to set up a threaded irq handler for your device
  730. * then you need to supply @handler and @thread_fn. @handler ist
  731. * still called in hard interrupt context and has to check
  732. * whether the interrupt originates from the device. If yes it
  733. * needs to disable the interrupt on the device and return
  734. * IRQ_THREAD_WAKE which will wake up the handler thread and run
  735. * @thread_fn. This split handler design is necessary to support
  736. * shared interrupts.
  737. *
  738. * Dev_id must be globally unique. Normally the address of the
  739. * device data structure is used as the cookie. Since the handler
  740. * receives this value it makes sense to use it.
  741. *
  742. * If your interrupt is shared you must pass a non NULL dev_id
  743. * as this is required when freeing the interrupt.
  744. *
  745. * Flags:
  746. *
  747. * IRQF_SHARED Interrupt is shared
  748. * IRQF_DISABLED Disable local interrupts while processing
  749. * IRQF_SAMPLE_RANDOM The interrupt can be used for entropy
  750. * IRQF_TRIGGER_* Specify active edge(s) or level
  751. *
  752. */
  753. int request_threaded_irq(unsigned int irq, irq_handler_t handler,
  754. irq_handler_t thread_fn, unsigned long irqflags,
  755. const char *devname, void *dev_id)
  756. {
  757. struct irqaction *action;
  758. struct irq_desc *desc;
  759. int retval;
  760. /*
  761. * handle_IRQ_event() always ignores IRQF_DISABLED except for
  762. * the _first_ irqaction (sigh). That can cause oopsing, but
  763. * the behavior is classified as "will not fix" so we need to
  764. * start nudging drivers away from using that idiom.
  765. */
  766. if ((irqflags & (IRQF_SHARED|IRQF_DISABLED)) ==
  767. (IRQF_SHARED|IRQF_DISABLED)) {
  768. pr_warning(
  769. "IRQ %d/%s: IRQF_DISABLED is not guaranteed on shared IRQs\n",
  770. irq, devname);
  771. }
  772. #ifdef CONFIG_LOCKDEP
  773. /*
  774. * Lockdep wants atomic interrupt handlers:
  775. */
  776. irqflags |= IRQF_DISABLED;
  777. #endif
  778. /*
  779. * Sanity-check: shared interrupts must pass in a real dev-ID,
  780. * otherwise we'll have trouble later trying to figure out
  781. * which interrupt is which (messes up the interrupt freeing
  782. * logic etc).
  783. */
  784. if ((irqflags & IRQF_SHARED) && !dev_id)
  785. return -EINVAL;
  786. desc = irq_to_desc(irq);
  787. if (!desc)
  788. return -EINVAL;
  789. if (desc->status & IRQ_NOREQUEST)
  790. return -EINVAL;
  791. if (!handler)
  792. return -EINVAL;
  793. action = kzalloc(sizeof(struct irqaction), GFP_KERNEL);
  794. if (!action)
  795. return -ENOMEM;
  796. action->handler = handler;
  797. action->thread_fn = thread_fn;
  798. action->flags = irqflags;
  799. action->name = devname;
  800. action->dev_id = dev_id;
  801. retval = __setup_irq(irq, desc, action);
  802. if (retval)
  803. kfree(action);
  804. #ifdef CONFIG_DEBUG_SHIRQ
  805. if (irqflags & IRQF_SHARED) {
  806. /*
  807. * It's a shared IRQ -- the driver ought to be prepared for it
  808. * to happen immediately, so let's make sure....
  809. * We disable the irq to make sure that a 'real' IRQ doesn't
  810. * run in parallel with our fake.
  811. */
  812. unsigned long flags;
  813. disable_irq(irq);
  814. local_irq_save(flags);
  815. handler(irq, dev_id);
  816. local_irq_restore(flags);
  817. enable_irq(irq);
  818. }
  819. #endif
  820. return retval;
  821. }
  822. EXPORT_SYMBOL(request_threaded_irq);