manage.c 23 KB

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