manage.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314
  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 state;
  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->istate & IRQS_INPROGRESS)
  40. cpu_relax();
  41. /* Ok, that indicated we're done: double-check carefully. */
  42. raw_spin_lock_irqsave(&desc->lock, flags);
  43. state = desc->istate;
  44. raw_spin_unlock_irqrestore(&desc->lock, flags);
  45. /* Oops, that failed? */
  46. } while (state & IRQS_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 (!desc || !irqd_can_balance(&desc->irq_data) ||
  65. !desc->irq_data.chip || !desc->irq_data.chip->irq_set_affinity)
  66. return 0;
  67. return 1;
  68. }
  69. /**
  70. * irq_set_thread_affinity - Notify irq threads to adjust affinity
  71. * @desc: irq descriptor which has affitnity changed
  72. *
  73. * We just set IRQTF_AFFINITY and delegate the affinity setting
  74. * to the interrupt thread itself. We can not call
  75. * set_cpus_allowed_ptr() here as we hold desc->lock and this
  76. * code can be called from hard interrupt context.
  77. */
  78. void irq_set_thread_affinity(struct irq_desc *desc)
  79. {
  80. struct irqaction *action = desc->action;
  81. while (action) {
  82. if (action->thread)
  83. set_bit(IRQTF_AFFINITY, &action->thread_flags);
  84. action = action->next;
  85. }
  86. }
  87. #ifdef CONFIG_GENERIC_PENDING_IRQ
  88. static inline bool irq_can_move_pcntxt(struct irq_desc *desc)
  89. {
  90. return irq_settings_can_move_pcntxt(desc);
  91. }
  92. static inline bool irq_move_pending(struct irq_desc *desc)
  93. {
  94. return irqd_is_setaffinity_pending(&desc->irq_data);
  95. }
  96. static inline void
  97. irq_copy_pending(struct irq_desc *desc, const struct cpumask *mask)
  98. {
  99. cpumask_copy(desc->pending_mask, mask);
  100. }
  101. static inline void
  102. irq_get_pending(struct cpumask *mask, struct irq_desc *desc)
  103. {
  104. cpumask_copy(mask, desc->pending_mask);
  105. }
  106. #else
  107. static inline bool irq_can_move_pcntxt(struct irq_desc *desc) { return true; }
  108. static inline bool irq_move_pending(struct irq_desc *desc) { return false; }
  109. static inline void
  110. irq_copy_pending(struct irq_desc *desc, const struct cpumask *mask) { }
  111. static inline void
  112. irq_get_pending(struct cpumask *mask, struct irq_desc *desc) { }
  113. #endif
  114. /**
  115. * irq_set_affinity - Set the irq affinity of a given irq
  116. * @irq: Interrupt to set affinity
  117. * @cpumask: cpumask
  118. *
  119. */
  120. int irq_set_affinity(unsigned int irq, const struct cpumask *mask)
  121. {
  122. struct irq_desc *desc = irq_to_desc(irq);
  123. struct irq_chip *chip = desc->irq_data.chip;
  124. unsigned long flags;
  125. int ret = 0;
  126. if (!chip->irq_set_affinity)
  127. return -EINVAL;
  128. raw_spin_lock_irqsave(&desc->lock, flags);
  129. if (irq_can_move_pcntxt(desc)) {
  130. ret = chip->irq_set_affinity(&desc->irq_data, mask, false);
  131. switch (ret) {
  132. case IRQ_SET_MASK_OK:
  133. cpumask_copy(desc->irq_data.affinity, mask);
  134. case IRQ_SET_MASK_OK_NOCOPY:
  135. irq_set_thread_affinity(desc);
  136. ret = 0;
  137. }
  138. } else {
  139. irqd_set_move_pending(&desc->irq_data);
  140. irq_copy_pending(desc, mask);
  141. }
  142. if (desc->affinity_notify) {
  143. kref_get(&desc->affinity_notify->kref);
  144. schedule_work(&desc->affinity_notify->work);
  145. }
  146. irq_compat_set_affinity(desc);
  147. irqd_set(&desc->irq_data, IRQD_AFFINITY_SET);
  148. raw_spin_unlock_irqrestore(&desc->lock, flags);
  149. return ret;
  150. }
  151. int irq_set_affinity_hint(unsigned int irq, const struct cpumask *m)
  152. {
  153. struct irq_desc *desc = irq_to_desc(irq);
  154. unsigned long flags;
  155. if (!desc)
  156. return -EINVAL;
  157. raw_spin_lock_irqsave(&desc->lock, flags);
  158. desc->affinity_hint = m;
  159. raw_spin_unlock_irqrestore(&desc->lock, flags);
  160. return 0;
  161. }
  162. EXPORT_SYMBOL_GPL(irq_set_affinity_hint);
  163. static void irq_affinity_notify(struct work_struct *work)
  164. {
  165. struct irq_affinity_notify *notify =
  166. container_of(work, struct irq_affinity_notify, work);
  167. struct irq_desc *desc = irq_to_desc(notify->irq);
  168. cpumask_var_t cpumask;
  169. unsigned long flags;
  170. if (!desc || !alloc_cpumask_var(&cpumask, GFP_KERNEL))
  171. goto out;
  172. raw_spin_lock_irqsave(&desc->lock, flags);
  173. if (irq_move_pending(desc))
  174. irq_get_pending(cpumask, desc);
  175. else
  176. cpumask_copy(cpumask, desc->irq_data.affinity);
  177. raw_spin_unlock_irqrestore(&desc->lock, flags);
  178. notify->notify(notify, cpumask);
  179. free_cpumask_var(cpumask);
  180. out:
  181. kref_put(&notify->kref, notify->release);
  182. }
  183. /**
  184. * irq_set_affinity_notifier - control notification of IRQ affinity changes
  185. * @irq: Interrupt for which to enable/disable notification
  186. * @notify: Context for notification, or %NULL to disable
  187. * notification. Function pointers must be initialised;
  188. * the other fields will be initialised by this function.
  189. *
  190. * Must be called in process context. Notification may only be enabled
  191. * after the IRQ is allocated and must be disabled before the IRQ is
  192. * freed using free_irq().
  193. */
  194. int
  195. irq_set_affinity_notifier(unsigned int irq, struct irq_affinity_notify *notify)
  196. {
  197. struct irq_desc *desc = irq_to_desc(irq);
  198. struct irq_affinity_notify *old_notify;
  199. unsigned long flags;
  200. /* The release function is promised process context */
  201. might_sleep();
  202. if (!desc)
  203. return -EINVAL;
  204. /* Complete initialisation of *notify */
  205. if (notify) {
  206. notify->irq = irq;
  207. kref_init(&notify->kref);
  208. INIT_WORK(&notify->work, irq_affinity_notify);
  209. }
  210. raw_spin_lock_irqsave(&desc->lock, flags);
  211. old_notify = desc->affinity_notify;
  212. desc->affinity_notify = notify;
  213. raw_spin_unlock_irqrestore(&desc->lock, flags);
  214. if (old_notify)
  215. kref_put(&old_notify->kref, old_notify->release);
  216. return 0;
  217. }
  218. EXPORT_SYMBOL_GPL(irq_set_affinity_notifier);
  219. #ifndef CONFIG_AUTO_IRQ_AFFINITY
  220. /*
  221. * Generic version of the affinity autoselector.
  222. */
  223. static int
  224. setup_affinity(unsigned int irq, struct irq_desc *desc, struct cpumask *mask)
  225. {
  226. struct irq_chip *chip = irq_desc_get_chip(desc);
  227. struct cpumask *set = irq_default_affinity;
  228. int ret;
  229. /* Excludes PER_CPU and NO_BALANCE interrupts */
  230. if (!irq_can_set_affinity(irq))
  231. return 0;
  232. /*
  233. * Preserve an userspace affinity setup, but make sure that
  234. * one of the targets is online.
  235. */
  236. if (irqd_has_set(&desc->irq_data, IRQD_AFFINITY_SET)) {
  237. if (cpumask_intersects(desc->irq_data.affinity,
  238. cpu_online_mask))
  239. set = desc->irq_data.affinity;
  240. else {
  241. irq_compat_clr_affinity(desc);
  242. irqd_clear(&desc->irq_data, IRQD_AFFINITY_SET);
  243. }
  244. }
  245. cpumask_and(mask, cpu_online_mask, set);
  246. ret = chip->irq_set_affinity(&desc->irq_data, mask, false);
  247. switch (ret) {
  248. case IRQ_SET_MASK_OK:
  249. cpumask_copy(desc->irq_data.affinity, mask);
  250. case IRQ_SET_MASK_OK_NOCOPY:
  251. irq_set_thread_affinity(desc);
  252. }
  253. return 0;
  254. }
  255. #else
  256. static inline int
  257. setup_affinity(unsigned int irq, struct irq_desc *d, struct cpumask *mask)
  258. {
  259. return irq_select_affinity(irq);
  260. }
  261. #endif
  262. /*
  263. * Called when affinity is set via /proc/irq
  264. */
  265. int irq_select_affinity_usr(unsigned int irq, struct cpumask *mask)
  266. {
  267. struct irq_desc *desc = irq_to_desc(irq);
  268. unsigned long flags;
  269. int ret;
  270. raw_spin_lock_irqsave(&desc->lock, flags);
  271. ret = setup_affinity(irq, desc, mask);
  272. raw_spin_unlock_irqrestore(&desc->lock, flags);
  273. return ret;
  274. }
  275. #else
  276. static inline int
  277. setup_affinity(unsigned int irq, struct irq_desc *desc, struct cpumask *mask)
  278. {
  279. return 0;
  280. }
  281. #endif
  282. void __disable_irq(struct irq_desc *desc, unsigned int irq, bool suspend)
  283. {
  284. if (suspend) {
  285. if (!desc->action || (desc->action->flags & IRQF_NO_SUSPEND))
  286. return;
  287. desc->istate |= IRQS_SUSPENDED;
  288. }
  289. if (!desc->depth++)
  290. irq_disable(desc);
  291. }
  292. /**
  293. * disable_irq_nosync - disable an irq without waiting
  294. * @irq: Interrupt to disable
  295. *
  296. * Disable the selected interrupt line. Disables and Enables are
  297. * nested.
  298. * Unlike disable_irq(), this function does not ensure existing
  299. * instances of the IRQ handler have completed before returning.
  300. *
  301. * This function may be called from IRQ context.
  302. */
  303. void disable_irq_nosync(unsigned int irq)
  304. {
  305. struct irq_desc *desc = irq_to_desc(irq);
  306. unsigned long flags;
  307. if (!desc)
  308. return;
  309. chip_bus_lock(desc);
  310. raw_spin_lock_irqsave(&desc->lock, flags);
  311. __disable_irq(desc, irq, false);
  312. raw_spin_unlock_irqrestore(&desc->lock, flags);
  313. chip_bus_sync_unlock(desc);
  314. }
  315. EXPORT_SYMBOL(disable_irq_nosync);
  316. /**
  317. * disable_irq - disable an irq and wait for completion
  318. * @irq: Interrupt to disable
  319. *
  320. * Disable the selected interrupt line. Enables and Disables are
  321. * nested.
  322. * This function waits for any pending IRQ handlers for this interrupt
  323. * to complete before returning. If you use this function while
  324. * holding a resource the IRQ handler may need you will deadlock.
  325. *
  326. * This function may be called - with care - from IRQ context.
  327. */
  328. void disable_irq(unsigned int irq)
  329. {
  330. struct irq_desc *desc = irq_to_desc(irq);
  331. if (!desc)
  332. return;
  333. disable_irq_nosync(irq);
  334. if (desc->action)
  335. synchronize_irq(irq);
  336. }
  337. EXPORT_SYMBOL(disable_irq);
  338. void __enable_irq(struct irq_desc *desc, unsigned int irq, bool resume)
  339. {
  340. if (resume) {
  341. if (!(desc->istate & IRQS_SUSPENDED)) {
  342. if (!desc->action)
  343. return;
  344. if (!(desc->action->flags & IRQF_FORCE_RESUME))
  345. return;
  346. /* Pretend that it got disabled ! */
  347. desc->depth++;
  348. }
  349. desc->istate &= ~IRQS_SUSPENDED;
  350. }
  351. switch (desc->depth) {
  352. case 0:
  353. err_out:
  354. WARN(1, KERN_WARNING "Unbalanced enable for IRQ %d\n", irq);
  355. break;
  356. case 1: {
  357. if (desc->istate & IRQS_SUSPENDED)
  358. goto err_out;
  359. /* Prevent probing on this irq: */
  360. irq_settings_set_noprobe(desc);
  361. irq_enable(desc);
  362. check_irq_resend(desc, irq);
  363. /* fall-through */
  364. }
  365. default:
  366. desc->depth--;
  367. }
  368. }
  369. /**
  370. * enable_irq - enable handling of an irq
  371. * @irq: Interrupt to enable
  372. *
  373. * Undoes the effect of one call to disable_irq(). If this
  374. * matches the last disable, processing of interrupts on this
  375. * IRQ line is re-enabled.
  376. *
  377. * This function may be called from IRQ context only when
  378. * desc->irq_data.chip->bus_lock and desc->chip->bus_sync_unlock are NULL !
  379. */
  380. void enable_irq(unsigned int irq)
  381. {
  382. struct irq_desc *desc = irq_to_desc(irq);
  383. unsigned long flags;
  384. if (!desc)
  385. return;
  386. if (WARN(!desc->irq_data.chip,
  387. KERN_ERR "enable_irq before setup/request_irq: irq %u\n", irq))
  388. return;
  389. chip_bus_lock(desc);
  390. raw_spin_lock_irqsave(&desc->lock, flags);
  391. __enable_irq(desc, irq, false);
  392. raw_spin_unlock_irqrestore(&desc->lock, flags);
  393. chip_bus_sync_unlock(desc);
  394. }
  395. EXPORT_SYMBOL(enable_irq);
  396. static int set_irq_wake_real(unsigned int irq, unsigned int on)
  397. {
  398. struct irq_desc *desc = irq_to_desc(irq);
  399. int ret = -ENXIO;
  400. if (desc->irq_data.chip->irq_set_wake)
  401. ret = desc->irq_data.chip->irq_set_wake(&desc->irq_data, on);
  402. return ret;
  403. }
  404. /**
  405. * irq_set_irq_wake - control irq power management wakeup
  406. * @irq: interrupt to control
  407. * @on: enable/disable power management wakeup
  408. *
  409. * Enable/disable power management wakeup mode, which is
  410. * disabled by default. Enables and disables must match,
  411. * just as they match for non-wakeup mode support.
  412. *
  413. * Wakeup mode lets this IRQ wake the system from sleep
  414. * states like "suspend to RAM".
  415. */
  416. int irq_set_irq_wake(unsigned int irq, unsigned int on)
  417. {
  418. struct irq_desc *desc = irq_to_desc(irq);
  419. unsigned long flags;
  420. int ret = 0;
  421. /* wakeup-capable irqs can be shared between drivers that
  422. * don't need to have the same sleep mode behaviors.
  423. */
  424. chip_bus_lock(desc);
  425. raw_spin_lock_irqsave(&desc->lock, flags);
  426. if (on) {
  427. if (desc->wake_depth++ == 0) {
  428. ret = set_irq_wake_real(irq, on);
  429. if (ret)
  430. desc->wake_depth = 0;
  431. else
  432. desc->istate |= IRQS_WAKEUP;
  433. }
  434. } else {
  435. if (desc->wake_depth == 0) {
  436. WARN(1, "Unbalanced IRQ %d wake disable\n", irq);
  437. } else if (--desc->wake_depth == 0) {
  438. ret = set_irq_wake_real(irq, on);
  439. if (ret)
  440. desc->wake_depth = 1;
  441. else
  442. desc->istate &= ~IRQS_WAKEUP;
  443. }
  444. }
  445. raw_spin_unlock_irqrestore(&desc->lock, flags);
  446. chip_bus_sync_unlock(desc);
  447. return ret;
  448. }
  449. EXPORT_SYMBOL(irq_set_irq_wake);
  450. /*
  451. * Internal function that tells the architecture code whether a
  452. * particular irq has been exclusively allocated or is available
  453. * for driver use.
  454. */
  455. int can_request_irq(unsigned int irq, unsigned long irqflags)
  456. {
  457. struct irq_desc *desc = irq_to_desc(irq);
  458. struct irqaction *action;
  459. unsigned long flags;
  460. if (!desc)
  461. return 0;
  462. if (!irq_settings_can_request(desc))
  463. return 0;
  464. raw_spin_lock_irqsave(&desc->lock, flags);
  465. action = desc->action;
  466. if (action)
  467. if (irqflags & action->flags & IRQF_SHARED)
  468. action = NULL;
  469. raw_spin_unlock_irqrestore(&desc->lock, flags);
  470. return !action;
  471. }
  472. void compat_irq_chip_set_default_handler(struct irq_desc *desc)
  473. {
  474. /*
  475. * If the architecture still has not overriden
  476. * the flow handler then zap the default. This
  477. * should catch incorrect flow-type setting.
  478. */
  479. if (desc->handle_irq == &handle_bad_irq)
  480. desc->handle_irq = NULL;
  481. }
  482. int __irq_set_trigger(struct irq_desc *desc, unsigned int irq,
  483. unsigned long flags)
  484. {
  485. struct irq_chip *chip = desc->irq_data.chip;
  486. int ret, unmask = 0;
  487. if (!chip || !chip->irq_set_type) {
  488. /*
  489. * IRQF_TRIGGER_* but the PIC does not support multiple
  490. * flow-types?
  491. */
  492. pr_debug("No set_type function for IRQ %d (%s)\n", irq,
  493. chip ? (chip->name ? : "unknown") : "unknown");
  494. return 0;
  495. }
  496. flags &= IRQ_TYPE_SENSE_MASK;
  497. if (chip->flags & IRQCHIP_SET_TYPE_MASKED) {
  498. if (!(desc->istate & IRQS_MASKED))
  499. mask_irq(desc);
  500. if (!(desc->istate & IRQS_DISABLED))
  501. unmask = 1;
  502. }
  503. /* caller masked out all except trigger mode flags */
  504. ret = chip->irq_set_type(&desc->irq_data, flags);
  505. switch (ret) {
  506. case IRQ_SET_MASK_OK:
  507. irqd_clear(&desc->irq_data, IRQD_TRIGGER_MASK);
  508. irqd_set(&desc->irq_data, flags);
  509. case IRQ_SET_MASK_OK_NOCOPY:
  510. flags = irqd_get_trigger_type(&desc->irq_data);
  511. irq_settings_set_trigger_mask(desc, flags);
  512. irqd_clear(&desc->irq_data, IRQD_LEVEL);
  513. irq_settings_clr_level(desc);
  514. if (flags & IRQ_TYPE_LEVEL_MASK) {
  515. irq_settings_set_level(desc);
  516. irqd_set(&desc->irq_data, IRQD_LEVEL);
  517. }
  518. if (chip != desc->irq_data.chip)
  519. irq_chip_set_defaults(desc->irq_data.chip);
  520. ret = 0;
  521. default:
  522. pr_err("setting trigger mode %lu for irq %u failed (%pF)\n",
  523. flags, irq, chip->irq_set_type);
  524. }
  525. if (unmask)
  526. unmask_irq(desc);
  527. return ret;
  528. }
  529. /*
  530. * Default primary interrupt handler for threaded interrupts. Is
  531. * assigned as primary handler when request_threaded_irq is called
  532. * with handler == NULL. Useful for oneshot interrupts.
  533. */
  534. static irqreturn_t irq_default_primary_handler(int irq, void *dev_id)
  535. {
  536. return IRQ_WAKE_THREAD;
  537. }
  538. /*
  539. * Primary handler for nested threaded interrupts. Should never be
  540. * called.
  541. */
  542. static irqreturn_t irq_nested_primary_handler(int irq, void *dev_id)
  543. {
  544. WARN(1, "Primary handler called for nested irq %d\n", irq);
  545. return IRQ_NONE;
  546. }
  547. static int irq_wait_for_interrupt(struct irqaction *action)
  548. {
  549. while (!kthread_should_stop()) {
  550. set_current_state(TASK_INTERRUPTIBLE);
  551. if (test_and_clear_bit(IRQTF_RUNTHREAD,
  552. &action->thread_flags)) {
  553. __set_current_state(TASK_RUNNING);
  554. return 0;
  555. }
  556. schedule();
  557. }
  558. return -1;
  559. }
  560. /*
  561. * Oneshot interrupts keep the irq line masked until the threaded
  562. * handler finished. unmask if the interrupt has not been disabled and
  563. * is marked MASKED.
  564. */
  565. static void irq_finalize_oneshot(unsigned int irq, struct irq_desc *desc)
  566. {
  567. again:
  568. chip_bus_lock(desc);
  569. raw_spin_lock_irq(&desc->lock);
  570. /*
  571. * Implausible though it may be we need to protect us against
  572. * the following scenario:
  573. *
  574. * The thread is faster done than the hard interrupt handler
  575. * on the other CPU. If we unmask the irq line then the
  576. * interrupt can come in again and masks the line, leaves due
  577. * to IRQS_INPROGRESS and the irq line is masked forever.
  578. */
  579. if (unlikely(desc->istate & IRQS_INPROGRESS)) {
  580. raw_spin_unlock_irq(&desc->lock);
  581. chip_bus_sync_unlock(desc);
  582. cpu_relax();
  583. goto again;
  584. }
  585. if (!(desc->istate & IRQS_DISABLED) && (desc->istate & IRQS_MASKED)) {
  586. irq_compat_clr_masked(desc);
  587. desc->istate &= ~IRQS_MASKED;
  588. desc->irq_data.chip->irq_unmask(&desc->irq_data);
  589. }
  590. raw_spin_unlock_irq(&desc->lock);
  591. chip_bus_sync_unlock(desc);
  592. }
  593. #ifdef CONFIG_SMP
  594. /*
  595. * Check whether we need to chasnge the affinity of the interrupt thread.
  596. */
  597. static void
  598. irq_thread_check_affinity(struct irq_desc *desc, struct irqaction *action)
  599. {
  600. cpumask_var_t mask;
  601. if (!test_and_clear_bit(IRQTF_AFFINITY, &action->thread_flags))
  602. return;
  603. /*
  604. * In case we are out of memory we set IRQTF_AFFINITY again and
  605. * try again next time
  606. */
  607. if (!alloc_cpumask_var(&mask, GFP_KERNEL)) {
  608. set_bit(IRQTF_AFFINITY, &action->thread_flags);
  609. return;
  610. }
  611. raw_spin_lock_irq(&desc->lock);
  612. cpumask_copy(mask, desc->irq_data.affinity);
  613. raw_spin_unlock_irq(&desc->lock);
  614. set_cpus_allowed_ptr(current, mask);
  615. free_cpumask_var(mask);
  616. }
  617. #else
  618. static inline void
  619. irq_thread_check_affinity(struct irq_desc *desc, struct irqaction *action) { }
  620. #endif
  621. /*
  622. * Interrupt handler thread
  623. */
  624. static int irq_thread(void *data)
  625. {
  626. static const struct sched_param param = {
  627. .sched_priority = MAX_USER_RT_PRIO/2,
  628. };
  629. struct irqaction *action = data;
  630. struct irq_desc *desc = irq_to_desc(action->irq);
  631. int wake, oneshot = desc->istate & IRQS_ONESHOT;
  632. sched_setscheduler(current, SCHED_FIFO, &param);
  633. current->irqaction = action;
  634. while (!irq_wait_for_interrupt(action)) {
  635. irq_thread_check_affinity(desc, action);
  636. atomic_inc(&desc->threads_active);
  637. raw_spin_lock_irq(&desc->lock);
  638. if (unlikely(desc->istate & IRQS_DISABLED)) {
  639. /*
  640. * CHECKME: We might need a dedicated
  641. * IRQ_THREAD_PENDING flag here, which
  642. * retriggers the thread in check_irq_resend()
  643. * but AFAICT IRQS_PENDING should be fine as it
  644. * retriggers the interrupt itself --- tglx
  645. */
  646. irq_compat_set_pending(desc);
  647. desc->istate |= IRQS_PENDING;
  648. raw_spin_unlock_irq(&desc->lock);
  649. } else {
  650. raw_spin_unlock_irq(&desc->lock);
  651. action->thread_fn(action->irq, action->dev_id);
  652. if (oneshot)
  653. irq_finalize_oneshot(action->irq, desc);
  654. }
  655. wake = atomic_dec_and_test(&desc->threads_active);
  656. if (wake && waitqueue_active(&desc->wait_for_threads))
  657. wake_up(&desc->wait_for_threads);
  658. }
  659. /*
  660. * Clear irqaction. Otherwise exit_irq_thread() would make
  661. * fuzz about an active irq thread going into nirvana.
  662. */
  663. current->irqaction = NULL;
  664. return 0;
  665. }
  666. /*
  667. * Called from do_exit()
  668. */
  669. void exit_irq_thread(void)
  670. {
  671. struct task_struct *tsk = current;
  672. if (!tsk->irqaction)
  673. return;
  674. printk(KERN_ERR
  675. "exiting task \"%s\" (%d) is an active IRQ thread (irq %d)\n",
  676. tsk->comm ? tsk->comm : "", tsk->pid, tsk->irqaction->irq);
  677. /*
  678. * Set the THREAD DIED flag to prevent further wakeups of the
  679. * soon to be gone threaded handler.
  680. */
  681. set_bit(IRQTF_DIED, &tsk->irqaction->flags);
  682. }
  683. /*
  684. * Internal function to register an irqaction - typically used to
  685. * allocate special interrupts that are part of the architecture.
  686. */
  687. static int
  688. __setup_irq(unsigned int irq, struct irq_desc *desc, struct irqaction *new)
  689. {
  690. struct irqaction *old, **old_ptr;
  691. const char *old_name = NULL;
  692. unsigned long flags;
  693. int ret, nested, shared = 0;
  694. cpumask_var_t mask;
  695. if (!desc)
  696. return -EINVAL;
  697. if (desc->irq_data.chip == &no_irq_chip)
  698. return -ENOSYS;
  699. /*
  700. * Some drivers like serial.c use request_irq() heavily,
  701. * so we have to be careful not to interfere with a
  702. * running system.
  703. */
  704. if (new->flags & IRQF_SAMPLE_RANDOM) {
  705. /*
  706. * This function might sleep, we want to call it first,
  707. * outside of the atomic block.
  708. * Yes, this might clear the entropy pool if the wrong
  709. * driver is attempted to be loaded, without actually
  710. * installing a new handler, but is this really a problem,
  711. * only the sysadmin is able to do this.
  712. */
  713. rand_initialize_irq(irq);
  714. }
  715. /* Oneshot interrupts are not allowed with shared */
  716. if ((new->flags & IRQF_ONESHOT) && (new->flags & IRQF_SHARED))
  717. return -EINVAL;
  718. /*
  719. * Check whether the interrupt nests into another interrupt
  720. * thread.
  721. */
  722. nested = irq_settings_is_nested_thread(desc);
  723. if (nested) {
  724. if (!new->thread_fn)
  725. return -EINVAL;
  726. /*
  727. * Replace the primary handler which was provided from
  728. * the driver for non nested interrupt handling by the
  729. * dummy function which warns when called.
  730. */
  731. new->handler = irq_nested_primary_handler;
  732. }
  733. /*
  734. * Create a handler thread when a thread function is supplied
  735. * and the interrupt does not nest into another interrupt
  736. * thread.
  737. */
  738. if (new->thread_fn && !nested) {
  739. struct task_struct *t;
  740. t = kthread_create(irq_thread, new, "irq/%d-%s", irq,
  741. new->name);
  742. if (IS_ERR(t))
  743. return PTR_ERR(t);
  744. /*
  745. * We keep the reference to the task struct even if
  746. * the thread dies to avoid that the interrupt code
  747. * references an already freed task_struct.
  748. */
  749. get_task_struct(t);
  750. new->thread = t;
  751. }
  752. if (!alloc_cpumask_var(&mask, GFP_KERNEL)) {
  753. ret = -ENOMEM;
  754. goto out_thread;
  755. }
  756. /*
  757. * The following block of code has to be executed atomically
  758. */
  759. raw_spin_lock_irqsave(&desc->lock, flags);
  760. old_ptr = &desc->action;
  761. old = *old_ptr;
  762. if (old) {
  763. /*
  764. * Can't share interrupts unless both agree to and are
  765. * the same type (level, edge, polarity). So both flag
  766. * fields must have IRQF_SHARED set and the bits which
  767. * set the trigger type must match.
  768. */
  769. if (!((old->flags & new->flags) & IRQF_SHARED) ||
  770. ((old->flags ^ new->flags) & IRQF_TRIGGER_MASK)) {
  771. old_name = old->name;
  772. goto mismatch;
  773. }
  774. /* All handlers must agree on per-cpuness */
  775. if ((old->flags & IRQF_PERCPU) !=
  776. (new->flags & IRQF_PERCPU))
  777. goto mismatch;
  778. /* add new interrupt at end of irq queue */
  779. do {
  780. old_ptr = &old->next;
  781. old = *old_ptr;
  782. } while (old);
  783. shared = 1;
  784. }
  785. if (!shared) {
  786. irq_chip_set_defaults(desc->irq_data.chip);
  787. init_waitqueue_head(&desc->wait_for_threads);
  788. /* Setup the type (level, edge polarity) if configured: */
  789. if (new->flags & IRQF_TRIGGER_MASK) {
  790. ret = __irq_set_trigger(desc, irq,
  791. new->flags & IRQF_TRIGGER_MASK);
  792. if (ret)
  793. goto out_mask;
  794. } else
  795. compat_irq_chip_set_default_handler(desc);
  796. desc->istate &= ~(IRQS_AUTODETECT | IRQS_SPURIOUS_DISABLED | \
  797. IRQS_INPROGRESS | IRQS_ONESHOT | \
  798. IRQS_WAITING);
  799. if (new->flags & IRQF_PERCPU) {
  800. irqd_set(&desc->irq_data, IRQD_PER_CPU);
  801. irq_settings_set_per_cpu(desc);
  802. }
  803. if (new->flags & IRQF_ONESHOT)
  804. desc->istate |= IRQS_ONESHOT;
  805. if (irq_settings_can_autoenable(desc))
  806. irq_startup(desc);
  807. else
  808. /* Undo nested disables: */
  809. desc->depth = 1;
  810. /* Exclude IRQ from balancing if requested */
  811. if (new->flags & IRQF_NOBALANCING) {
  812. irq_settings_set_no_balancing(desc);
  813. irqd_set(&desc->irq_data, IRQD_NO_BALANCING);
  814. }
  815. /* Set default affinity mask once everything is setup */
  816. setup_affinity(irq, desc, mask);
  817. } else if (new->flags & IRQF_TRIGGER_MASK) {
  818. unsigned int nmsk = new->flags & IRQF_TRIGGER_MASK;
  819. unsigned int omsk = irq_settings_get_trigger_mask(desc);
  820. if (nmsk != omsk)
  821. /* hope the handler works with current trigger mode */
  822. pr_warning("IRQ %d uses trigger mode %u; requested %u\n",
  823. irq, nmsk, omsk);
  824. }
  825. new->irq = irq;
  826. *old_ptr = new;
  827. /* Reset broken irq detection when installing new handler */
  828. desc->irq_count = 0;
  829. desc->irqs_unhandled = 0;
  830. /*
  831. * Check whether we disabled the irq via the spurious handler
  832. * before. Reenable it and give it another chance.
  833. */
  834. if (shared && (desc->istate & IRQS_SPURIOUS_DISABLED)) {
  835. desc->istate &= ~IRQS_SPURIOUS_DISABLED;
  836. __enable_irq(desc, irq, false);
  837. }
  838. raw_spin_unlock_irqrestore(&desc->lock, flags);
  839. /*
  840. * Strictly no need to wake it up, but hung_task complains
  841. * when no hard interrupt wakes the thread up.
  842. */
  843. if (new->thread)
  844. wake_up_process(new->thread);
  845. register_irq_proc(irq, desc);
  846. new->dir = NULL;
  847. register_handler_proc(irq, new);
  848. return 0;
  849. mismatch:
  850. #ifdef CONFIG_DEBUG_SHIRQ
  851. if (!(new->flags & IRQF_PROBE_SHARED)) {
  852. printk(KERN_ERR "IRQ handler type mismatch for IRQ %d\n", irq);
  853. if (old_name)
  854. printk(KERN_ERR "current handler: %s\n", old_name);
  855. dump_stack();
  856. }
  857. #endif
  858. ret = -EBUSY;
  859. out_mask:
  860. free_cpumask_var(mask);
  861. out_thread:
  862. raw_spin_unlock_irqrestore(&desc->lock, flags);
  863. if (new->thread) {
  864. struct task_struct *t = new->thread;
  865. new->thread = NULL;
  866. if (likely(!test_bit(IRQTF_DIED, &new->thread_flags)))
  867. kthread_stop(t);
  868. put_task_struct(t);
  869. }
  870. return ret;
  871. }
  872. /**
  873. * setup_irq - setup an interrupt
  874. * @irq: Interrupt line to setup
  875. * @act: irqaction for the interrupt
  876. *
  877. * Used to statically setup interrupts in the early boot process.
  878. */
  879. int setup_irq(unsigned int irq, struct irqaction *act)
  880. {
  881. int retval;
  882. struct irq_desc *desc = irq_to_desc(irq);
  883. chip_bus_lock(desc);
  884. retval = __setup_irq(irq, desc, act);
  885. chip_bus_sync_unlock(desc);
  886. return retval;
  887. }
  888. EXPORT_SYMBOL_GPL(setup_irq);
  889. /*
  890. * Internal function to unregister an irqaction - used to free
  891. * regular and special interrupts that are part of the architecture.
  892. */
  893. static struct irqaction *__free_irq(unsigned int irq, void *dev_id)
  894. {
  895. struct irq_desc *desc = irq_to_desc(irq);
  896. struct irqaction *action, **action_ptr;
  897. unsigned long flags;
  898. WARN(in_interrupt(), "Trying to free IRQ %d from IRQ context!\n", irq);
  899. if (!desc)
  900. return NULL;
  901. raw_spin_lock_irqsave(&desc->lock, flags);
  902. /*
  903. * There can be multiple actions per IRQ descriptor, find the right
  904. * one based on the dev_id:
  905. */
  906. action_ptr = &desc->action;
  907. for (;;) {
  908. action = *action_ptr;
  909. if (!action) {
  910. WARN(1, "Trying to free already-free IRQ %d\n", irq);
  911. raw_spin_unlock_irqrestore(&desc->lock, flags);
  912. return NULL;
  913. }
  914. if (action->dev_id == dev_id)
  915. break;
  916. action_ptr = &action->next;
  917. }
  918. /* Found it - now remove it from the list of entries: */
  919. *action_ptr = action->next;
  920. /* Currently used only by UML, might disappear one day: */
  921. #ifdef CONFIG_IRQ_RELEASE_METHOD
  922. if (desc->irq_data.chip->release)
  923. desc->irq_data.chip->release(irq, dev_id);
  924. #endif
  925. /* If this was the last handler, shut down the IRQ line: */
  926. if (!desc->action)
  927. irq_shutdown(desc);
  928. #ifdef CONFIG_SMP
  929. /* make sure affinity_hint is cleaned up */
  930. if (WARN_ON_ONCE(desc->affinity_hint))
  931. desc->affinity_hint = NULL;
  932. #endif
  933. raw_spin_unlock_irqrestore(&desc->lock, flags);
  934. unregister_handler_proc(irq, action);
  935. /* Make sure it's not being used on another CPU: */
  936. synchronize_irq(irq);
  937. #ifdef CONFIG_DEBUG_SHIRQ
  938. /*
  939. * It's a shared IRQ -- the driver ought to be prepared for an IRQ
  940. * event to happen even now it's being freed, so let's make sure that
  941. * is so by doing an extra call to the handler ....
  942. *
  943. * ( We do this after actually deregistering it, to make sure that a
  944. * 'real' IRQ doesn't run in * parallel with our fake. )
  945. */
  946. if (action->flags & IRQF_SHARED) {
  947. local_irq_save(flags);
  948. action->handler(irq, dev_id);
  949. local_irq_restore(flags);
  950. }
  951. #endif
  952. if (action->thread) {
  953. if (!test_bit(IRQTF_DIED, &action->thread_flags))
  954. kthread_stop(action->thread);
  955. put_task_struct(action->thread);
  956. }
  957. return action;
  958. }
  959. /**
  960. * remove_irq - free an interrupt
  961. * @irq: Interrupt line to free
  962. * @act: irqaction for the interrupt
  963. *
  964. * Used to remove interrupts statically setup by the early boot process.
  965. */
  966. void remove_irq(unsigned int irq, struct irqaction *act)
  967. {
  968. __free_irq(irq, act->dev_id);
  969. }
  970. EXPORT_SYMBOL_GPL(remove_irq);
  971. /**
  972. * free_irq - free an interrupt allocated with request_irq
  973. * @irq: Interrupt line to free
  974. * @dev_id: Device identity to free
  975. *
  976. * Remove an interrupt handler. The handler is removed and if the
  977. * interrupt line is no longer in use by any driver it is disabled.
  978. * On a shared IRQ the caller must ensure the interrupt is disabled
  979. * on the card it drives before calling this function. The function
  980. * does not return until any executing interrupts for this IRQ
  981. * have completed.
  982. *
  983. * This function must not be called from interrupt context.
  984. */
  985. void free_irq(unsigned int irq, void *dev_id)
  986. {
  987. struct irq_desc *desc = irq_to_desc(irq);
  988. if (!desc)
  989. return;
  990. #ifdef CONFIG_SMP
  991. if (WARN_ON(desc->affinity_notify))
  992. desc->affinity_notify = NULL;
  993. #endif
  994. chip_bus_lock(desc);
  995. kfree(__free_irq(irq, dev_id));
  996. chip_bus_sync_unlock(desc);
  997. }
  998. EXPORT_SYMBOL(free_irq);
  999. /**
  1000. * request_threaded_irq - allocate an interrupt line
  1001. * @irq: Interrupt line to allocate
  1002. * @handler: Function to be called when the IRQ occurs.
  1003. * Primary handler for threaded interrupts
  1004. * If NULL and thread_fn != NULL the default
  1005. * primary handler is installed
  1006. * @thread_fn: Function called from the irq handler thread
  1007. * If NULL, no irq thread is created
  1008. * @irqflags: Interrupt type flags
  1009. * @devname: An ascii name for the claiming device
  1010. * @dev_id: A cookie passed back to the handler function
  1011. *
  1012. * This call allocates interrupt resources and enables the
  1013. * interrupt line and IRQ handling. From the point this
  1014. * call is made your handler function may be invoked. Since
  1015. * your handler function must clear any interrupt the board
  1016. * raises, you must take care both to initialise your hardware
  1017. * and to set up the interrupt handler in the right order.
  1018. *
  1019. * If you want to set up a threaded irq handler for your device
  1020. * then you need to supply @handler and @thread_fn. @handler ist
  1021. * still called in hard interrupt context and has to check
  1022. * whether the interrupt originates from the device. If yes it
  1023. * needs to disable the interrupt on the device and return
  1024. * IRQ_WAKE_THREAD which will wake up the handler thread and run
  1025. * @thread_fn. This split handler design is necessary to support
  1026. * shared interrupts.
  1027. *
  1028. * Dev_id must be globally unique. Normally the address of the
  1029. * device data structure is used as the cookie. Since the handler
  1030. * receives this value it makes sense to use it.
  1031. *
  1032. * If your interrupt is shared you must pass a non NULL dev_id
  1033. * as this is required when freeing the interrupt.
  1034. *
  1035. * Flags:
  1036. *
  1037. * IRQF_SHARED Interrupt is shared
  1038. * IRQF_SAMPLE_RANDOM The interrupt can be used for entropy
  1039. * IRQF_TRIGGER_* Specify active edge(s) or level
  1040. *
  1041. */
  1042. int request_threaded_irq(unsigned int irq, irq_handler_t handler,
  1043. irq_handler_t thread_fn, unsigned long irqflags,
  1044. const char *devname, void *dev_id)
  1045. {
  1046. struct irqaction *action;
  1047. struct irq_desc *desc;
  1048. int retval;
  1049. /*
  1050. * Sanity-check: shared interrupts must pass in a real dev-ID,
  1051. * otherwise we'll have trouble later trying to figure out
  1052. * which interrupt is which (messes up the interrupt freeing
  1053. * logic etc).
  1054. */
  1055. if ((irqflags & IRQF_SHARED) && !dev_id)
  1056. return -EINVAL;
  1057. desc = irq_to_desc(irq);
  1058. if (!desc)
  1059. return -EINVAL;
  1060. if (!irq_settings_can_request(desc))
  1061. return -EINVAL;
  1062. if (!handler) {
  1063. if (!thread_fn)
  1064. return -EINVAL;
  1065. handler = irq_default_primary_handler;
  1066. }
  1067. action = kzalloc(sizeof(struct irqaction), GFP_KERNEL);
  1068. if (!action)
  1069. return -ENOMEM;
  1070. action->handler = handler;
  1071. action->thread_fn = thread_fn;
  1072. action->flags = irqflags;
  1073. action->name = devname;
  1074. action->dev_id = dev_id;
  1075. chip_bus_lock(desc);
  1076. retval = __setup_irq(irq, desc, action);
  1077. chip_bus_sync_unlock(desc);
  1078. if (retval)
  1079. kfree(action);
  1080. #ifdef CONFIG_DEBUG_SHIRQ_FIXME
  1081. if (!retval && (irqflags & IRQF_SHARED)) {
  1082. /*
  1083. * It's a shared IRQ -- the driver ought to be prepared for it
  1084. * to happen immediately, so let's make sure....
  1085. * We disable the irq to make sure that a 'real' IRQ doesn't
  1086. * run in parallel with our fake.
  1087. */
  1088. unsigned long flags;
  1089. disable_irq(irq);
  1090. local_irq_save(flags);
  1091. handler(irq, dev_id);
  1092. local_irq_restore(flags);
  1093. enable_irq(irq);
  1094. }
  1095. #endif
  1096. return retval;
  1097. }
  1098. EXPORT_SYMBOL(request_threaded_irq);
  1099. /**
  1100. * request_any_context_irq - allocate an interrupt line
  1101. * @irq: Interrupt line to allocate
  1102. * @handler: Function to be called when the IRQ occurs.
  1103. * Threaded handler for threaded interrupts.
  1104. * @flags: Interrupt type flags
  1105. * @name: An ascii name for the claiming device
  1106. * @dev_id: A cookie passed back to the handler function
  1107. *
  1108. * This call allocates interrupt resources and enables the
  1109. * interrupt line and IRQ handling. It selects either a
  1110. * hardirq or threaded handling method depending on the
  1111. * context.
  1112. *
  1113. * On failure, it returns a negative value. On success,
  1114. * it returns either IRQC_IS_HARDIRQ or IRQC_IS_NESTED.
  1115. */
  1116. int request_any_context_irq(unsigned int irq, irq_handler_t handler,
  1117. unsigned long flags, const char *name, void *dev_id)
  1118. {
  1119. struct irq_desc *desc = irq_to_desc(irq);
  1120. int ret;
  1121. if (!desc)
  1122. return -EINVAL;
  1123. if (irq_settings_is_nested_thread(desc)) {
  1124. ret = request_threaded_irq(irq, NULL, handler,
  1125. flags, name, dev_id);
  1126. return !ret ? IRQC_IS_NESTED : ret;
  1127. }
  1128. ret = request_irq(irq, handler, flags, name, dev_id);
  1129. return !ret ? IRQC_IS_HARDIRQ : ret;
  1130. }
  1131. EXPORT_SYMBOL_GPL(request_any_context_irq);