manage.c 23 KB

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