manage.c 23 KB

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