manage.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693
  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. #ifdef CONFIG_IRQ_FORCED_THREADING
  18. __read_mostly bool force_irqthreads;
  19. static int __init setup_forced_irqthreads(char *arg)
  20. {
  21. force_irqthreads = true;
  22. return 0;
  23. }
  24. early_param("threadirqs", setup_forced_irqthreads);
  25. #endif
  26. /**
  27. * synchronize_irq - wait for pending IRQ handlers (on other CPUs)
  28. * @irq: interrupt number to wait for
  29. *
  30. * This function waits for any pending IRQ handlers for this interrupt
  31. * to complete before returning. If you use this function while
  32. * holding a resource the IRQ handler may need you will deadlock.
  33. *
  34. * This function may be called - with care - from IRQ context.
  35. */
  36. void synchronize_irq(unsigned int irq)
  37. {
  38. struct irq_desc *desc = irq_to_desc(irq);
  39. bool inprogress;
  40. if (!desc)
  41. return;
  42. do {
  43. unsigned long flags;
  44. /*
  45. * Wait until we're out of the critical section. This might
  46. * give the wrong answer due to the lack of memory barriers.
  47. */
  48. while (irqd_irq_inprogress(&desc->irq_data))
  49. cpu_relax();
  50. /* Ok, that indicated we're done: double-check carefully. */
  51. raw_spin_lock_irqsave(&desc->lock, flags);
  52. inprogress = irqd_irq_inprogress(&desc->irq_data);
  53. raw_spin_unlock_irqrestore(&desc->lock, flags);
  54. /* Oops, that failed? */
  55. } while (inprogress);
  56. /*
  57. * We made sure that no hardirq handler is running. Now verify
  58. * that no threaded handlers are active.
  59. */
  60. wait_event(desc->wait_for_threads, !atomic_read(&desc->threads_active));
  61. }
  62. EXPORT_SYMBOL(synchronize_irq);
  63. #ifdef CONFIG_SMP
  64. cpumask_var_t irq_default_affinity;
  65. /**
  66. * irq_can_set_affinity - Check if the affinity of a given irq can be set
  67. * @irq: Interrupt to check
  68. *
  69. */
  70. int irq_can_set_affinity(unsigned int irq)
  71. {
  72. struct irq_desc *desc = irq_to_desc(irq);
  73. if (!desc || !irqd_can_balance(&desc->irq_data) ||
  74. !desc->irq_data.chip || !desc->irq_data.chip->irq_set_affinity)
  75. return 0;
  76. return 1;
  77. }
  78. /**
  79. * irq_set_thread_affinity - Notify irq threads to adjust affinity
  80. * @desc: irq descriptor which has affitnity changed
  81. *
  82. * We just set IRQTF_AFFINITY and delegate the affinity setting
  83. * to the interrupt thread itself. We can not call
  84. * set_cpus_allowed_ptr() here as we hold desc->lock and this
  85. * code can be called from hard interrupt context.
  86. */
  87. void irq_set_thread_affinity(struct irq_desc *desc)
  88. {
  89. struct irqaction *action = desc->action;
  90. while (action) {
  91. if (action->thread)
  92. set_bit(IRQTF_AFFINITY, &action->thread_flags);
  93. action = action->next;
  94. }
  95. }
  96. #ifdef CONFIG_GENERIC_PENDING_IRQ
  97. static inline bool irq_can_move_pcntxt(struct irq_data *data)
  98. {
  99. return irqd_can_move_in_process_context(data);
  100. }
  101. static inline bool irq_move_pending(struct irq_data *data)
  102. {
  103. return irqd_is_setaffinity_pending(data);
  104. }
  105. static inline void
  106. irq_copy_pending(struct irq_desc *desc, const struct cpumask *mask)
  107. {
  108. cpumask_copy(desc->pending_mask, mask);
  109. }
  110. static inline void
  111. irq_get_pending(struct cpumask *mask, struct irq_desc *desc)
  112. {
  113. cpumask_copy(mask, desc->pending_mask);
  114. }
  115. #else
  116. static inline bool irq_can_move_pcntxt(struct irq_data *data) { return true; }
  117. static inline bool irq_move_pending(struct irq_data *data) { return false; }
  118. static inline void
  119. irq_copy_pending(struct irq_desc *desc, const struct cpumask *mask) { }
  120. static inline void
  121. irq_get_pending(struct cpumask *mask, struct irq_desc *desc) { }
  122. #endif
  123. int __irq_set_affinity_locked(struct irq_data *data, const struct cpumask *mask)
  124. {
  125. struct irq_chip *chip = irq_data_get_irq_chip(data);
  126. struct irq_desc *desc = irq_data_to_desc(data);
  127. int ret = 0;
  128. if (!chip || !chip->irq_set_affinity)
  129. return -EINVAL;
  130. if (irq_can_move_pcntxt(data)) {
  131. ret = chip->irq_set_affinity(data, mask, false);
  132. switch (ret) {
  133. case IRQ_SET_MASK_OK:
  134. cpumask_copy(data->affinity, mask);
  135. case IRQ_SET_MASK_OK_NOCOPY:
  136. irq_set_thread_affinity(desc);
  137. ret = 0;
  138. }
  139. } else {
  140. irqd_set_move_pending(data);
  141. irq_copy_pending(desc, mask);
  142. }
  143. if (desc->affinity_notify) {
  144. kref_get(&desc->affinity_notify->kref);
  145. schedule_work(&desc->affinity_notify->work);
  146. }
  147. irqd_set(data, IRQD_AFFINITY_SET);
  148. return ret;
  149. }
  150. /**
  151. * irq_set_affinity - Set the irq affinity of a given irq
  152. * @irq: Interrupt to set affinity
  153. * @mask: cpumask
  154. *
  155. */
  156. int irq_set_affinity(unsigned int irq, const struct cpumask *mask)
  157. {
  158. struct irq_desc *desc = irq_to_desc(irq);
  159. unsigned long flags;
  160. int ret;
  161. if (!desc)
  162. return -EINVAL;
  163. raw_spin_lock_irqsave(&desc->lock, flags);
  164. ret = __irq_set_affinity_locked(irq_desc_get_irq_data(desc), mask);
  165. raw_spin_unlock_irqrestore(&desc->lock, flags);
  166. return ret;
  167. }
  168. int irq_set_affinity_hint(unsigned int irq, const struct cpumask *m)
  169. {
  170. unsigned long flags;
  171. struct irq_desc *desc = irq_get_desc_lock(irq, &flags, IRQ_GET_DESC_CHECK_GLOBAL);
  172. if (!desc)
  173. return -EINVAL;
  174. desc->affinity_hint = m;
  175. irq_put_desc_unlock(desc, flags);
  176. return 0;
  177. }
  178. EXPORT_SYMBOL_GPL(irq_set_affinity_hint);
  179. static void irq_affinity_notify(struct work_struct *work)
  180. {
  181. struct irq_affinity_notify *notify =
  182. container_of(work, struct irq_affinity_notify, work);
  183. struct irq_desc *desc = irq_to_desc(notify->irq);
  184. cpumask_var_t cpumask;
  185. unsigned long flags;
  186. if (!desc || !alloc_cpumask_var(&cpumask, GFP_KERNEL))
  187. goto out;
  188. raw_spin_lock_irqsave(&desc->lock, flags);
  189. if (irq_move_pending(&desc->irq_data))
  190. irq_get_pending(cpumask, desc);
  191. else
  192. cpumask_copy(cpumask, desc->irq_data.affinity);
  193. raw_spin_unlock_irqrestore(&desc->lock, flags);
  194. notify->notify(notify, cpumask);
  195. free_cpumask_var(cpumask);
  196. out:
  197. kref_put(&notify->kref, notify->release);
  198. }
  199. /**
  200. * irq_set_affinity_notifier - control notification of IRQ affinity changes
  201. * @irq: Interrupt for which to enable/disable notification
  202. * @notify: Context for notification, or %NULL to disable
  203. * notification. Function pointers must be initialised;
  204. * the other fields will be initialised by this function.
  205. *
  206. * Must be called in process context. Notification may only be enabled
  207. * after the IRQ is allocated and must be disabled before the IRQ is
  208. * freed using free_irq().
  209. */
  210. int
  211. irq_set_affinity_notifier(unsigned int irq, struct irq_affinity_notify *notify)
  212. {
  213. struct irq_desc *desc = irq_to_desc(irq);
  214. struct irq_affinity_notify *old_notify;
  215. unsigned long flags;
  216. /* The release function is promised process context */
  217. might_sleep();
  218. if (!desc)
  219. return -EINVAL;
  220. /* Complete initialisation of *notify */
  221. if (notify) {
  222. notify->irq = irq;
  223. kref_init(&notify->kref);
  224. INIT_WORK(&notify->work, irq_affinity_notify);
  225. }
  226. raw_spin_lock_irqsave(&desc->lock, flags);
  227. old_notify = desc->affinity_notify;
  228. desc->affinity_notify = notify;
  229. raw_spin_unlock_irqrestore(&desc->lock, flags);
  230. if (old_notify) {
  231. cancel_work_sync(&old_notify->work);
  232. kref_put(&old_notify->kref, old_notify->release);
  233. }
  234. return 0;
  235. }
  236. EXPORT_SYMBOL_GPL(irq_set_affinity_notifier);
  237. #ifndef CONFIG_AUTO_IRQ_AFFINITY
  238. /*
  239. * Generic version of the affinity autoselector.
  240. */
  241. static int
  242. setup_affinity(unsigned int irq, struct irq_desc *desc, struct cpumask *mask)
  243. {
  244. struct irq_chip *chip = irq_desc_get_chip(desc);
  245. struct cpumask *set = irq_default_affinity;
  246. int ret, node = desc->irq_data.node;
  247. /* Excludes PER_CPU and NO_BALANCE interrupts */
  248. if (!irq_can_set_affinity(irq))
  249. return 0;
  250. /*
  251. * Preserve an userspace affinity setup, but make sure that
  252. * one of the targets is online.
  253. */
  254. if (irqd_has_set(&desc->irq_data, IRQD_AFFINITY_SET)) {
  255. if (cpumask_intersects(desc->irq_data.affinity,
  256. cpu_online_mask))
  257. set = desc->irq_data.affinity;
  258. else
  259. irqd_clear(&desc->irq_data, IRQD_AFFINITY_SET);
  260. }
  261. cpumask_and(mask, cpu_online_mask, set);
  262. if (node != NUMA_NO_NODE) {
  263. const struct cpumask *nodemask = cpumask_of_node(node);
  264. /* make sure at least one of the cpus in nodemask is online */
  265. if (cpumask_intersects(mask, nodemask))
  266. cpumask_and(mask, mask, nodemask);
  267. }
  268. ret = chip->irq_set_affinity(&desc->irq_data, mask, false);
  269. switch (ret) {
  270. case IRQ_SET_MASK_OK:
  271. cpumask_copy(desc->irq_data.affinity, mask);
  272. case IRQ_SET_MASK_OK_NOCOPY:
  273. irq_set_thread_affinity(desc);
  274. }
  275. return 0;
  276. }
  277. #else
  278. static inline int
  279. setup_affinity(unsigned int irq, struct irq_desc *d, struct cpumask *mask)
  280. {
  281. return irq_select_affinity(irq);
  282. }
  283. #endif
  284. /*
  285. * Called when affinity is set via /proc/irq
  286. */
  287. int irq_select_affinity_usr(unsigned int irq, struct cpumask *mask)
  288. {
  289. struct irq_desc *desc = irq_to_desc(irq);
  290. unsigned long flags;
  291. int ret;
  292. raw_spin_lock_irqsave(&desc->lock, flags);
  293. ret = setup_affinity(irq, desc, mask);
  294. raw_spin_unlock_irqrestore(&desc->lock, flags);
  295. return ret;
  296. }
  297. #else
  298. static inline int
  299. setup_affinity(unsigned int irq, struct irq_desc *desc, struct cpumask *mask)
  300. {
  301. return 0;
  302. }
  303. #endif
  304. void __disable_irq(struct irq_desc *desc, unsigned int irq, bool suspend)
  305. {
  306. if (suspend) {
  307. if (!desc->action || (desc->action->flags & IRQF_NO_SUSPEND))
  308. return;
  309. desc->istate |= IRQS_SUSPENDED;
  310. }
  311. if (!desc->depth++)
  312. irq_disable(desc);
  313. }
  314. static int __disable_irq_nosync(unsigned int irq)
  315. {
  316. unsigned long flags;
  317. struct irq_desc *desc = irq_get_desc_buslock(irq, &flags, IRQ_GET_DESC_CHECK_GLOBAL);
  318. if (!desc)
  319. return -EINVAL;
  320. __disable_irq(desc, irq, false);
  321. irq_put_desc_busunlock(desc, flags);
  322. return 0;
  323. }
  324. /**
  325. * disable_irq_nosync - disable an irq without waiting
  326. * @irq: Interrupt to disable
  327. *
  328. * Disable the selected interrupt line. Disables and Enables are
  329. * nested.
  330. * Unlike disable_irq(), this function does not ensure existing
  331. * instances of the IRQ handler have completed before returning.
  332. *
  333. * This function may be called from IRQ context.
  334. */
  335. void disable_irq_nosync(unsigned int irq)
  336. {
  337. __disable_irq_nosync(irq);
  338. }
  339. EXPORT_SYMBOL(disable_irq_nosync);
  340. /**
  341. * disable_irq - disable an irq and wait for completion
  342. * @irq: Interrupt to disable
  343. *
  344. * Disable the selected interrupt line. Enables and Disables are
  345. * nested.
  346. * This function waits for any pending IRQ handlers for this interrupt
  347. * to complete before returning. If you use this function while
  348. * holding a resource the IRQ handler may need you will deadlock.
  349. *
  350. * This function may be called - with care - from IRQ context.
  351. */
  352. void disable_irq(unsigned int irq)
  353. {
  354. if (!__disable_irq_nosync(irq))
  355. synchronize_irq(irq);
  356. }
  357. EXPORT_SYMBOL(disable_irq);
  358. void __enable_irq(struct irq_desc *desc, unsigned int irq, bool resume)
  359. {
  360. if (resume) {
  361. if (!(desc->istate & IRQS_SUSPENDED)) {
  362. if (!desc->action)
  363. return;
  364. if (!(desc->action->flags & IRQF_FORCE_RESUME))
  365. return;
  366. /* Pretend that it got disabled ! */
  367. desc->depth++;
  368. }
  369. desc->istate &= ~IRQS_SUSPENDED;
  370. }
  371. switch (desc->depth) {
  372. case 0:
  373. err_out:
  374. WARN(1, KERN_WARNING "Unbalanced enable for IRQ %d\n", irq);
  375. break;
  376. case 1: {
  377. if (desc->istate & IRQS_SUSPENDED)
  378. goto err_out;
  379. /* Prevent probing on this irq: */
  380. irq_settings_set_noprobe(desc);
  381. irq_enable(desc);
  382. check_irq_resend(desc, irq);
  383. /* fall-through */
  384. }
  385. default:
  386. desc->depth--;
  387. }
  388. }
  389. /**
  390. * enable_irq - enable handling of an irq
  391. * @irq: Interrupt to enable
  392. *
  393. * Undoes the effect of one call to disable_irq(). If this
  394. * matches the last disable, processing of interrupts on this
  395. * IRQ line is re-enabled.
  396. *
  397. * This function may be called from IRQ context only when
  398. * desc->irq_data.chip->bus_lock and desc->chip->bus_sync_unlock are NULL !
  399. */
  400. void enable_irq(unsigned int irq)
  401. {
  402. unsigned long flags;
  403. struct irq_desc *desc = irq_get_desc_buslock(irq, &flags, IRQ_GET_DESC_CHECK_GLOBAL);
  404. if (!desc)
  405. return;
  406. if (WARN(!desc->irq_data.chip,
  407. KERN_ERR "enable_irq before setup/request_irq: irq %u\n", irq))
  408. goto out;
  409. __enable_irq(desc, irq, false);
  410. out:
  411. irq_put_desc_busunlock(desc, flags);
  412. }
  413. EXPORT_SYMBOL(enable_irq);
  414. static int set_irq_wake_real(unsigned int irq, unsigned int on)
  415. {
  416. struct irq_desc *desc = irq_to_desc(irq);
  417. int ret = -ENXIO;
  418. if (irq_desc_get_chip(desc)->flags & IRQCHIP_SKIP_SET_WAKE)
  419. return 0;
  420. if (desc->irq_data.chip->irq_set_wake)
  421. ret = desc->irq_data.chip->irq_set_wake(&desc->irq_data, on);
  422. return ret;
  423. }
  424. /**
  425. * irq_set_irq_wake - control irq power management wakeup
  426. * @irq: interrupt to control
  427. * @on: enable/disable power management wakeup
  428. *
  429. * Enable/disable power management wakeup mode, which is
  430. * disabled by default. Enables and disables must match,
  431. * just as they match for non-wakeup mode support.
  432. *
  433. * Wakeup mode lets this IRQ wake the system from sleep
  434. * states like "suspend to RAM".
  435. */
  436. int irq_set_irq_wake(unsigned int irq, unsigned int on)
  437. {
  438. unsigned long flags;
  439. struct irq_desc *desc = irq_get_desc_buslock(irq, &flags, IRQ_GET_DESC_CHECK_GLOBAL);
  440. int ret = 0;
  441. if (!desc)
  442. return -EINVAL;
  443. /* wakeup-capable irqs can be shared between drivers that
  444. * don't need to have the same sleep mode behaviors.
  445. */
  446. if (on) {
  447. if (desc->wake_depth++ == 0) {
  448. ret = set_irq_wake_real(irq, on);
  449. if (ret)
  450. desc->wake_depth = 0;
  451. else
  452. irqd_set(&desc->irq_data, IRQD_WAKEUP_STATE);
  453. }
  454. } else {
  455. if (desc->wake_depth == 0) {
  456. WARN(1, "Unbalanced IRQ %d wake disable\n", irq);
  457. } else if (--desc->wake_depth == 0) {
  458. ret = set_irq_wake_real(irq, on);
  459. if (ret)
  460. desc->wake_depth = 1;
  461. else
  462. irqd_clear(&desc->irq_data, IRQD_WAKEUP_STATE);
  463. }
  464. }
  465. irq_put_desc_busunlock(desc, flags);
  466. return ret;
  467. }
  468. EXPORT_SYMBOL(irq_set_irq_wake);
  469. /**
  470. * irq_read_line - read the value on an irq line
  471. * @irq: Interrupt number representing a hardware line
  472. *
  473. * This function is meant to be called from within the irq handler.
  474. * Slowbus irq controllers might sleep, but it is assumed that the irq
  475. * handler for slowbus interrupts will execute in thread context, so
  476. * sleeping is okay.
  477. */
  478. int irq_read_line(unsigned int irq)
  479. {
  480. struct irq_desc *desc = irq_to_desc(irq);
  481. int val;
  482. if (!desc || !desc->irq_data.chip->irq_read_line)
  483. return -EINVAL;
  484. chip_bus_lock(desc);
  485. raw_spin_lock(&desc->lock);
  486. val = desc->irq_data.chip->irq_read_line(&desc->irq_data);
  487. raw_spin_unlock(&desc->lock);
  488. chip_bus_sync_unlock(desc);
  489. return val;
  490. }
  491. EXPORT_SYMBOL_GPL(irq_read_line);
  492. /*
  493. * Internal function that tells the architecture code whether a
  494. * particular irq has been exclusively allocated or is available
  495. * for driver use.
  496. */
  497. int can_request_irq(unsigned int irq, unsigned long irqflags)
  498. {
  499. unsigned long flags;
  500. struct irq_desc *desc = irq_get_desc_lock(irq, &flags, 0);
  501. int canrequest = 0;
  502. if (!desc)
  503. return 0;
  504. if (irq_settings_can_request(desc)) {
  505. if (!desc->action ||
  506. irqflags & desc->action->flags & IRQF_SHARED)
  507. canrequest = 1;
  508. }
  509. irq_put_desc_unlock(desc, flags);
  510. return canrequest;
  511. }
  512. int __irq_set_trigger(struct irq_desc *desc, unsigned int irq,
  513. unsigned long flags)
  514. {
  515. struct irq_chip *chip = desc->irq_data.chip;
  516. int ret, unmask = 0;
  517. if (!chip || !chip->irq_set_type) {
  518. /*
  519. * IRQF_TRIGGER_* but the PIC does not support multiple
  520. * flow-types?
  521. */
  522. pr_debug("No set_type function for IRQ %d (%s)\n", irq,
  523. chip ? (chip->name ? : "unknown") : "unknown");
  524. return 0;
  525. }
  526. flags &= IRQ_TYPE_SENSE_MASK;
  527. if (chip->flags & IRQCHIP_SET_TYPE_MASKED) {
  528. if (!irqd_irq_masked(&desc->irq_data))
  529. mask_irq(desc);
  530. if (!irqd_irq_disabled(&desc->irq_data))
  531. unmask = 1;
  532. }
  533. /* caller masked out all except trigger mode flags */
  534. ret = chip->irq_set_type(&desc->irq_data, flags);
  535. switch (ret) {
  536. case IRQ_SET_MASK_OK:
  537. irqd_clear(&desc->irq_data, IRQD_TRIGGER_MASK);
  538. irqd_set(&desc->irq_data, flags);
  539. case IRQ_SET_MASK_OK_NOCOPY:
  540. flags = irqd_get_trigger_type(&desc->irq_data);
  541. irq_settings_set_trigger_mask(desc, flags);
  542. irqd_clear(&desc->irq_data, IRQD_LEVEL);
  543. irq_settings_clr_level(desc);
  544. if (flags & IRQ_TYPE_LEVEL_MASK) {
  545. irq_settings_set_level(desc);
  546. irqd_set(&desc->irq_data, IRQD_LEVEL);
  547. }
  548. ret = 0;
  549. break;
  550. default:
  551. pr_err("setting trigger mode %lu for irq %u failed (%pF)\n",
  552. flags, irq, chip->irq_set_type);
  553. }
  554. if (unmask)
  555. unmask_irq(desc);
  556. return ret;
  557. }
  558. /*
  559. * Default primary interrupt handler for threaded interrupts. Is
  560. * assigned as primary handler when request_threaded_irq is called
  561. * with handler == NULL. Useful for oneshot interrupts.
  562. */
  563. static irqreturn_t irq_default_primary_handler(int irq, void *dev_id)
  564. {
  565. return IRQ_WAKE_THREAD;
  566. }
  567. /*
  568. * Primary handler for nested threaded interrupts. Should never be
  569. * called.
  570. */
  571. static irqreturn_t irq_nested_primary_handler(int irq, void *dev_id)
  572. {
  573. WARN(1, "Primary handler called for nested irq %d\n", irq);
  574. return IRQ_NONE;
  575. }
  576. static int irq_wait_for_interrupt(struct irqaction *action)
  577. {
  578. set_current_state(TASK_INTERRUPTIBLE);
  579. while (!kthread_should_stop()) {
  580. if (test_and_clear_bit(IRQTF_RUNTHREAD,
  581. &action->thread_flags)) {
  582. __set_current_state(TASK_RUNNING);
  583. return 0;
  584. }
  585. schedule();
  586. set_current_state(TASK_INTERRUPTIBLE);
  587. }
  588. __set_current_state(TASK_RUNNING);
  589. return -1;
  590. }
  591. /*
  592. * Oneshot interrupts keep the irq line masked until the threaded
  593. * handler finished. unmask if the interrupt has not been disabled and
  594. * is marked MASKED.
  595. */
  596. static void irq_finalize_oneshot(struct irq_desc *desc,
  597. struct irqaction *action)
  598. {
  599. if (!(desc->istate & IRQS_ONESHOT))
  600. return;
  601. again:
  602. chip_bus_lock(desc);
  603. raw_spin_lock_irq(&desc->lock);
  604. /*
  605. * Implausible though it may be we need to protect us against
  606. * the following scenario:
  607. *
  608. * The thread is faster done than the hard interrupt handler
  609. * on the other CPU. If we unmask the irq line then the
  610. * interrupt can come in again and masks the line, leaves due
  611. * to IRQS_INPROGRESS and the irq line is masked forever.
  612. *
  613. * This also serializes the state of shared oneshot handlers
  614. * versus "desc->threads_onehsot |= action->thread_mask;" in
  615. * irq_wake_thread(). See the comment there which explains the
  616. * serialization.
  617. */
  618. if (unlikely(irqd_irq_inprogress(&desc->irq_data))) {
  619. raw_spin_unlock_irq(&desc->lock);
  620. chip_bus_sync_unlock(desc);
  621. cpu_relax();
  622. goto again;
  623. }
  624. /*
  625. * Now check again, whether the thread should run. Otherwise
  626. * we would clear the threads_oneshot bit of this thread which
  627. * was just set.
  628. */
  629. if (test_bit(IRQTF_RUNTHREAD, &action->thread_flags))
  630. goto out_unlock;
  631. desc->threads_oneshot &= ~action->thread_mask;
  632. if (!desc->threads_oneshot && !irqd_irq_disabled(&desc->irq_data) &&
  633. irqd_irq_masked(&desc->irq_data))
  634. unmask_irq(desc);
  635. out_unlock:
  636. raw_spin_unlock_irq(&desc->lock);
  637. chip_bus_sync_unlock(desc);
  638. }
  639. #ifdef CONFIG_SMP
  640. /*
  641. * Check whether we need to chasnge the affinity of the interrupt thread.
  642. */
  643. static void
  644. irq_thread_check_affinity(struct irq_desc *desc, struct irqaction *action)
  645. {
  646. cpumask_var_t mask;
  647. bool valid = true;
  648. if (!test_and_clear_bit(IRQTF_AFFINITY, &action->thread_flags))
  649. return;
  650. /*
  651. * In case we are out of memory we set IRQTF_AFFINITY again and
  652. * try again next time
  653. */
  654. if (!alloc_cpumask_var(&mask, GFP_KERNEL)) {
  655. set_bit(IRQTF_AFFINITY, &action->thread_flags);
  656. return;
  657. }
  658. raw_spin_lock_irq(&desc->lock);
  659. /*
  660. * This code is triggered unconditionally. Check the affinity
  661. * mask pointer. For CPU_MASK_OFFSTACK=n this is optimized out.
  662. */
  663. if (desc->irq_data.affinity)
  664. cpumask_copy(mask, desc->irq_data.affinity);
  665. else
  666. valid = false;
  667. raw_spin_unlock_irq(&desc->lock);
  668. if (valid)
  669. set_cpus_allowed_ptr(current, mask);
  670. free_cpumask_var(mask);
  671. }
  672. #else
  673. static inline void
  674. irq_thread_check_affinity(struct irq_desc *desc, struct irqaction *action) { }
  675. #endif
  676. /*
  677. * Interrupts which are not explicitely requested as threaded
  678. * interrupts rely on the implicit bh/preempt disable of the hard irq
  679. * context. So we need to disable bh here to avoid deadlocks and other
  680. * side effects.
  681. */
  682. static irqreturn_t
  683. irq_forced_thread_fn(struct irq_desc *desc, struct irqaction *action)
  684. {
  685. irqreturn_t ret;
  686. local_bh_disable();
  687. ret = action->thread_fn(action->irq, action->dev_id);
  688. irq_finalize_oneshot(desc, action);
  689. local_bh_enable();
  690. return ret;
  691. }
  692. /*
  693. * Interrupts explicitely requested as threaded interupts want to be
  694. * preemtible - many of them need to sleep and wait for slow busses to
  695. * complete.
  696. */
  697. static irqreturn_t irq_thread_fn(struct irq_desc *desc,
  698. struct irqaction *action)
  699. {
  700. irqreturn_t ret;
  701. ret = action->thread_fn(action->irq, action->dev_id);
  702. irq_finalize_oneshot(desc, action);
  703. return ret;
  704. }
  705. static void wake_threads_waitq(struct irq_desc *desc)
  706. {
  707. if (atomic_dec_and_test(&desc->threads_active))
  708. wake_up(&desc->wait_for_threads);
  709. }
  710. /*
  711. * Interrupt handler thread
  712. */
  713. static int irq_thread(void *data)
  714. {
  715. static const struct sched_param param = {
  716. .sched_priority = MAX_USER_RT_PRIO/2,
  717. };
  718. struct irqaction *action = data;
  719. struct irq_desc *desc = irq_to_desc(action->irq);
  720. irqreturn_t (*handler_fn)(struct irq_desc *desc,
  721. struct irqaction *action);
  722. if (force_irqthreads && test_bit(IRQTF_FORCED_THREAD,
  723. &action->thread_flags))
  724. handler_fn = irq_forced_thread_fn;
  725. else
  726. handler_fn = irq_thread_fn;
  727. sched_setscheduler(current, SCHED_FIFO, &param);
  728. current->irq_thread = 1;
  729. while (!irq_wait_for_interrupt(action)) {
  730. irqreturn_t action_ret;
  731. irq_thread_check_affinity(desc, action);
  732. action_ret = handler_fn(desc, action);
  733. if (action_ret == IRQ_HANDLED)
  734. atomic_inc(&desc->threads_handled);
  735. wake_threads_waitq(desc);
  736. }
  737. /*
  738. * This is the regular exit path. __free_irq() is stopping the
  739. * thread via kthread_stop() after calling
  740. * synchronize_irq(). So neither IRQTF_RUNTHREAD nor the
  741. * oneshot mask bit can be set. We cannot verify that as we
  742. * cannot touch the oneshot mask at this point anymore as
  743. * __setup_irq() might have given out currents thread_mask
  744. * again.
  745. *
  746. * Clear irq_thread. Otherwise exit_irq_thread() would make
  747. * fuzz about an active irq thread going into nirvana.
  748. */
  749. current->irq_thread = 0;
  750. return 0;
  751. }
  752. /*
  753. * Called from do_exit()
  754. */
  755. void exit_irq_thread(void)
  756. {
  757. struct task_struct *tsk = current;
  758. struct irq_desc *desc;
  759. struct irqaction *action;
  760. if (!tsk->irq_thread)
  761. return;
  762. action = kthread_data(tsk);
  763. printk(KERN_ERR
  764. "exiting task \"%s\" (%d) is an active IRQ thread (irq %d)\n",
  765. tsk->comm ? tsk->comm : "", tsk->pid, action->irq);
  766. desc = irq_to_desc(action->irq);
  767. /*
  768. * If IRQTF_RUNTHREAD is set, we need to decrement
  769. * desc->threads_active and wake possible waiters.
  770. */
  771. if (test_and_clear_bit(IRQTF_RUNTHREAD, &action->thread_flags))
  772. wake_threads_waitq(desc);
  773. /* Prevent a stale desc->threads_oneshot */
  774. irq_finalize_oneshot(desc, action);
  775. }
  776. static void irq_setup_forced_threading(struct irqaction *new)
  777. {
  778. if (!force_irqthreads)
  779. return;
  780. if (new->flags & (IRQF_NO_THREAD | IRQF_PERCPU | IRQF_ONESHOT))
  781. return;
  782. new->flags |= IRQF_ONESHOT;
  783. if (!new->thread_fn) {
  784. set_bit(IRQTF_FORCED_THREAD, &new->thread_flags);
  785. new->thread_fn = new->handler;
  786. new->handler = irq_default_primary_handler;
  787. }
  788. }
  789. /*
  790. * Internal function to register an irqaction - typically used to
  791. * allocate special interrupts that are part of the architecture.
  792. */
  793. static int
  794. __setup_irq(unsigned int irq, struct irq_desc *desc, struct irqaction *new)
  795. {
  796. struct irqaction *old, **old_ptr;
  797. const char *old_name = NULL;
  798. unsigned long flags, thread_mask = 0;
  799. int ret, nested, shared = 0;
  800. cpumask_var_t mask;
  801. if (!desc)
  802. return -EINVAL;
  803. if (desc->irq_data.chip == &no_irq_chip)
  804. return -ENOSYS;
  805. if (!try_module_get(desc->owner))
  806. return -ENODEV;
  807. /*
  808. * Check whether the interrupt nests into another interrupt
  809. * thread.
  810. */
  811. nested = irq_settings_is_nested_thread(desc);
  812. if (nested) {
  813. if (!new->thread_fn) {
  814. ret = -EINVAL;
  815. goto out_mput;
  816. }
  817. /*
  818. * Replace the primary handler which was provided from
  819. * the driver for non nested interrupt handling by the
  820. * dummy function which warns when called.
  821. */
  822. new->handler = irq_nested_primary_handler;
  823. } else {
  824. if (irq_settings_can_thread(desc))
  825. irq_setup_forced_threading(new);
  826. }
  827. /*
  828. * Create a handler thread when a thread function is supplied
  829. * and the interrupt does not nest into another interrupt
  830. * thread.
  831. */
  832. if (new->thread_fn && !nested) {
  833. struct task_struct *t;
  834. t = kthread_create(irq_thread, new, "irq/%d-%s", irq,
  835. new->name);
  836. if (IS_ERR(t)) {
  837. ret = PTR_ERR(t);
  838. goto out_mput;
  839. }
  840. /*
  841. * We keep the reference to the task struct even if
  842. * the thread dies to avoid that the interrupt code
  843. * references an already freed task_struct.
  844. */
  845. get_task_struct(t);
  846. new->thread = t;
  847. /*
  848. * Tell the thread to set its affinity. This is
  849. * important for shared interrupt handlers as we do
  850. * not invoke setup_affinity() for the secondary
  851. * handlers as everything is already set up. Even for
  852. * interrupts marked with IRQF_NO_BALANCE this is
  853. * correct as we want the thread to move to the cpu(s)
  854. * on which the requesting code placed the interrupt.
  855. */
  856. set_bit(IRQTF_AFFINITY, &new->thread_flags);
  857. }
  858. if (!alloc_cpumask_var(&mask, GFP_KERNEL)) {
  859. ret = -ENOMEM;
  860. goto out_thread;
  861. }
  862. /*
  863. * The following block of code has to be executed atomically
  864. */
  865. raw_spin_lock_irqsave(&desc->lock, flags);
  866. old_ptr = &desc->action;
  867. old = *old_ptr;
  868. if (old) {
  869. /*
  870. * Can't share interrupts unless both agree to and are
  871. * the same type (level, edge, polarity). So both flag
  872. * fields must have IRQF_SHARED set and the bits which
  873. * set the trigger type must match. Also all must
  874. * agree on ONESHOT.
  875. */
  876. if (!((old->flags & new->flags) & IRQF_SHARED) ||
  877. ((old->flags ^ new->flags) & IRQF_TRIGGER_MASK) ||
  878. ((old->flags ^ new->flags) & IRQF_ONESHOT)) {
  879. old_name = old->name;
  880. goto mismatch;
  881. }
  882. /* All handlers must agree on per-cpuness */
  883. if ((old->flags & IRQF_PERCPU) !=
  884. (new->flags & IRQF_PERCPU))
  885. goto mismatch;
  886. /* add new interrupt at end of irq queue */
  887. do {
  888. /*
  889. * Or all existing action->thread_mask bits,
  890. * so we can find the next zero bit for this
  891. * new action.
  892. */
  893. thread_mask |= old->thread_mask;
  894. old_ptr = &old->next;
  895. old = *old_ptr;
  896. } while (old);
  897. shared = 1;
  898. }
  899. /*
  900. * Setup the thread mask for this irqaction for ONESHOT. For
  901. * !ONESHOT irqs the thread mask is 0 so we can avoid a
  902. * conditional in irq_wake_thread().
  903. */
  904. if (new->flags & IRQF_ONESHOT) {
  905. /*
  906. * Unlikely to have 32 resp 64 irqs sharing one line,
  907. * but who knows.
  908. */
  909. if (thread_mask == ~0UL) {
  910. ret = -EBUSY;
  911. goto out_mask;
  912. }
  913. /*
  914. * The thread_mask for the action is or'ed to
  915. * desc->thread_active to indicate that the
  916. * IRQF_ONESHOT thread handler has been woken, but not
  917. * yet finished. The bit is cleared when a thread
  918. * completes. When all threads of a shared interrupt
  919. * line have completed desc->threads_active becomes
  920. * zero and the interrupt line is unmasked. See
  921. * handle.c:irq_wake_thread() for further information.
  922. *
  923. * If no thread is woken by primary (hard irq context)
  924. * interrupt handlers, then desc->threads_active is
  925. * also checked for zero to unmask the irq line in the
  926. * affected hard irq flow handlers
  927. * (handle_[fasteoi|level]_irq).
  928. *
  929. * The new action gets the first zero bit of
  930. * thread_mask assigned. See the loop above which or's
  931. * all existing action->thread_mask bits.
  932. */
  933. new->thread_mask = 1 << ffz(thread_mask);
  934. }
  935. if (!shared) {
  936. init_waitqueue_head(&desc->wait_for_threads);
  937. /* Setup the type (level, edge polarity) if configured: */
  938. if (new->flags & IRQF_TRIGGER_MASK) {
  939. ret = __irq_set_trigger(desc, irq,
  940. new->flags & IRQF_TRIGGER_MASK);
  941. if (ret)
  942. goto out_mask;
  943. }
  944. desc->istate &= ~(IRQS_AUTODETECT | IRQS_SPURIOUS_DISABLED | \
  945. IRQS_ONESHOT | IRQS_WAITING);
  946. irqd_clear(&desc->irq_data, IRQD_IRQ_INPROGRESS);
  947. if (new->flags & IRQF_PERCPU) {
  948. irqd_set(&desc->irq_data, IRQD_PER_CPU);
  949. irq_settings_set_per_cpu(desc);
  950. }
  951. if (new->flags & IRQF_ONESHOT)
  952. desc->istate |= IRQS_ONESHOT;
  953. if (irq_settings_can_autoenable(desc))
  954. irq_startup(desc, true);
  955. else
  956. /* Undo nested disables: */
  957. desc->depth = 1;
  958. /* Exclude IRQ from balancing if requested */
  959. if (new->flags & IRQF_NOBALANCING) {
  960. irq_settings_set_no_balancing(desc);
  961. irqd_set(&desc->irq_data, IRQD_NO_BALANCING);
  962. }
  963. /* Set default affinity mask once everything is setup */
  964. setup_affinity(irq, desc, mask);
  965. } else if (new->flags & IRQF_TRIGGER_MASK) {
  966. unsigned int nmsk = new->flags & IRQF_TRIGGER_MASK;
  967. unsigned int omsk = irq_settings_get_trigger_mask(desc);
  968. if (nmsk != omsk)
  969. /* hope the handler works with current trigger mode */
  970. pr_warning("IRQ %d uses trigger mode %u; requested %u\n",
  971. irq, nmsk, omsk);
  972. }
  973. new->irq = irq;
  974. *old_ptr = new;
  975. /* Reset broken irq detection when installing new handler */
  976. desc->irq_count = 0;
  977. desc->irqs_unhandled = 0;
  978. /*
  979. * Check whether we disabled the irq via the spurious handler
  980. * before. Reenable it and give it another chance.
  981. */
  982. if (shared && (desc->istate & IRQS_SPURIOUS_DISABLED)) {
  983. desc->istate &= ~IRQS_SPURIOUS_DISABLED;
  984. __enable_irq(desc, irq, false);
  985. }
  986. raw_spin_unlock_irqrestore(&desc->lock, flags);
  987. /*
  988. * Strictly no need to wake it up, but hung_task complains
  989. * when no hard interrupt wakes the thread up.
  990. */
  991. if (new->thread)
  992. wake_up_process(new->thread);
  993. register_irq_proc(irq, desc);
  994. new->dir = NULL;
  995. register_handler_proc(irq, new);
  996. free_cpumask_var(mask);
  997. return 0;
  998. mismatch:
  999. #ifdef CONFIG_DEBUG_SHIRQ
  1000. if (!(new->flags & IRQF_PROBE_SHARED)) {
  1001. printk(KERN_ERR "IRQ handler type mismatch for IRQ %d\n", irq);
  1002. if (old_name)
  1003. printk(KERN_ERR "current handler: %s\n", old_name);
  1004. dump_stack();
  1005. }
  1006. #endif
  1007. ret = -EBUSY;
  1008. out_mask:
  1009. raw_spin_unlock_irqrestore(&desc->lock, flags);
  1010. free_cpumask_var(mask);
  1011. out_thread:
  1012. if (new->thread) {
  1013. struct task_struct *t = new->thread;
  1014. new->thread = NULL;
  1015. kthread_stop(t);
  1016. put_task_struct(t);
  1017. }
  1018. out_mput:
  1019. module_put(desc->owner);
  1020. return ret;
  1021. }
  1022. /**
  1023. * setup_irq - setup an interrupt
  1024. * @irq: Interrupt line to setup
  1025. * @act: irqaction for the interrupt
  1026. *
  1027. * Used to statically setup interrupts in the early boot process.
  1028. */
  1029. int setup_irq(unsigned int irq, struct irqaction *act)
  1030. {
  1031. int retval;
  1032. struct irq_desc *desc = irq_to_desc(irq);
  1033. if (WARN_ON(irq_settings_is_per_cpu_devid(desc)))
  1034. return -EINVAL;
  1035. chip_bus_lock(desc);
  1036. retval = __setup_irq(irq, desc, act);
  1037. chip_bus_sync_unlock(desc);
  1038. return retval;
  1039. }
  1040. EXPORT_SYMBOL_GPL(setup_irq);
  1041. /*
  1042. * Internal function to unregister an irqaction - used to free
  1043. * regular and special interrupts that are part of the architecture.
  1044. */
  1045. static struct irqaction *__free_irq(unsigned int irq, void *dev_id)
  1046. {
  1047. struct irq_desc *desc = irq_to_desc(irq);
  1048. struct irqaction *action, **action_ptr;
  1049. unsigned long flags;
  1050. WARN(in_interrupt(), "Trying to free IRQ %d from IRQ context!\n", irq);
  1051. if (!desc)
  1052. return NULL;
  1053. chip_bus_lock(desc);
  1054. raw_spin_lock_irqsave(&desc->lock, flags);
  1055. /*
  1056. * There can be multiple actions per IRQ descriptor, find the right
  1057. * one based on the dev_id:
  1058. */
  1059. action_ptr = &desc->action;
  1060. for (;;) {
  1061. action = *action_ptr;
  1062. if (!action) {
  1063. WARN(1, "Trying to free already-free IRQ %d\n", irq);
  1064. raw_spin_unlock_irqrestore(&desc->lock, flags);
  1065. chip_bus_sync_unlock(desc);
  1066. return NULL;
  1067. }
  1068. if (action->dev_id == dev_id)
  1069. break;
  1070. action_ptr = &action->next;
  1071. }
  1072. /* Found it - now remove it from the list of entries: */
  1073. *action_ptr = action->next;
  1074. /* Currently used only by UML, might disappear one day: */
  1075. #ifdef CONFIG_IRQ_RELEASE_METHOD
  1076. if (desc->irq_data.chip->release)
  1077. desc->irq_data.chip->release(irq, dev_id);
  1078. #endif
  1079. /* If this was the last handler, shut down the IRQ line: */
  1080. if (!desc->action) {
  1081. irq_shutdown(desc);
  1082. /* Explicitly mask the interrupt */
  1083. if (desc->irq_data.chip->irq_mask)
  1084. desc->irq_data.chip->irq_mask(&desc->irq_data);
  1085. else if (desc->irq_data.chip->irq_mask_ack)
  1086. desc->irq_data.chip->irq_mask_ack(&desc->irq_data);
  1087. }
  1088. #ifdef CONFIG_SMP
  1089. /* make sure affinity_hint is cleaned up */
  1090. if (WARN_ON_ONCE(desc->affinity_hint))
  1091. desc->affinity_hint = NULL;
  1092. #endif
  1093. raw_spin_unlock_irqrestore(&desc->lock, flags);
  1094. chip_bus_sync_unlock(desc);
  1095. unregister_handler_proc(irq, action);
  1096. /* Make sure it's not being used on another CPU: */
  1097. synchronize_irq(irq);
  1098. #ifdef CONFIG_DEBUG_SHIRQ
  1099. /*
  1100. * It's a shared IRQ -- the driver ought to be prepared for an IRQ
  1101. * event to happen even now it's being freed, so let's make sure that
  1102. * is so by doing an extra call to the handler ....
  1103. *
  1104. * ( We do this after actually deregistering it, to make sure that a
  1105. * 'real' IRQ doesn't run in * parallel with our fake. )
  1106. */
  1107. if (action->flags & IRQF_SHARED) {
  1108. local_irq_save(flags);
  1109. action->handler(irq, dev_id);
  1110. local_irq_restore(flags);
  1111. }
  1112. #endif
  1113. if (action->thread) {
  1114. kthread_stop(action->thread);
  1115. put_task_struct(action->thread);
  1116. }
  1117. module_put(desc->owner);
  1118. return action;
  1119. }
  1120. /**
  1121. * remove_irq - free an interrupt
  1122. * @irq: Interrupt line to free
  1123. * @act: irqaction for the interrupt
  1124. *
  1125. * Used to remove interrupts statically setup by the early boot process.
  1126. */
  1127. void remove_irq(unsigned int irq, struct irqaction *act)
  1128. {
  1129. struct irq_desc *desc = irq_to_desc(irq);
  1130. if (desc && !WARN_ON(irq_settings_is_per_cpu_devid(desc)))
  1131. __free_irq(irq, act->dev_id);
  1132. }
  1133. EXPORT_SYMBOL_GPL(remove_irq);
  1134. /**
  1135. * free_irq - free an interrupt allocated with request_irq
  1136. * @irq: Interrupt line to free
  1137. * @dev_id: Device identity to free
  1138. *
  1139. * Remove an interrupt handler. The handler is removed and if the
  1140. * interrupt line is no longer in use by any driver it is disabled.
  1141. * On a shared IRQ the caller must ensure the interrupt is disabled
  1142. * on the card it drives before calling this function. The function
  1143. * does not return until any executing interrupts for this IRQ
  1144. * have completed.
  1145. *
  1146. * This function must not be called from interrupt context.
  1147. */
  1148. void free_irq(unsigned int irq, void *dev_id)
  1149. {
  1150. struct irq_desc *desc = irq_to_desc(irq);
  1151. if (!desc || WARN_ON(irq_settings_is_per_cpu_devid(desc)))
  1152. return;
  1153. #ifdef CONFIG_SMP
  1154. if (WARN_ON(desc->affinity_notify))
  1155. desc->affinity_notify = NULL;
  1156. #endif
  1157. kfree(__free_irq(irq, dev_id));
  1158. }
  1159. EXPORT_SYMBOL(free_irq);
  1160. /**
  1161. * request_threaded_irq - allocate an interrupt line
  1162. * @irq: Interrupt line to allocate
  1163. * @handler: Function to be called when the IRQ occurs.
  1164. * Primary handler for threaded interrupts
  1165. * If NULL and thread_fn != NULL the default
  1166. * primary handler is installed
  1167. * @thread_fn: Function called from the irq handler thread
  1168. * If NULL, no irq thread is created
  1169. * @irqflags: Interrupt type flags
  1170. * @devname: An ascii name for the claiming device
  1171. * @dev_id: A cookie passed back to the handler function
  1172. *
  1173. * This call allocates interrupt resources and enables the
  1174. * interrupt line and IRQ handling. From the point this
  1175. * call is made your handler function may be invoked. Since
  1176. * your handler function must clear any interrupt the board
  1177. * raises, you must take care both to initialise your hardware
  1178. * and to set up the interrupt handler in the right order.
  1179. *
  1180. * If you want to set up a threaded irq handler for your device
  1181. * then you need to supply @handler and @thread_fn. @handler is
  1182. * still called in hard interrupt context and has to check
  1183. * whether the interrupt originates from the device. If yes it
  1184. * needs to disable the interrupt on the device and return
  1185. * IRQ_WAKE_THREAD which will wake up the handler thread and run
  1186. * @thread_fn. This split handler design is necessary to support
  1187. * shared interrupts.
  1188. *
  1189. * Dev_id must be globally unique. Normally the address of the
  1190. * device data structure is used as the cookie. Since the handler
  1191. * receives this value it makes sense to use it.
  1192. *
  1193. * If your interrupt is shared you must pass a non NULL dev_id
  1194. * as this is required when freeing the interrupt.
  1195. *
  1196. * Flags:
  1197. *
  1198. * IRQF_SHARED Interrupt is shared
  1199. * IRQF_TRIGGER_* Specify active edge(s) or level
  1200. *
  1201. */
  1202. int request_threaded_irq(unsigned int irq, irq_handler_t handler,
  1203. irq_handler_t thread_fn, unsigned long irqflags,
  1204. const char *devname, void *dev_id)
  1205. {
  1206. struct irqaction *action;
  1207. struct irq_desc *desc;
  1208. int retval;
  1209. /*
  1210. * Sanity-check: shared interrupts must pass in a real dev-ID,
  1211. * otherwise we'll have trouble later trying to figure out
  1212. * which interrupt is which (messes up the interrupt freeing
  1213. * logic etc).
  1214. */
  1215. if ((irqflags & IRQF_SHARED) && !dev_id)
  1216. return -EINVAL;
  1217. desc = irq_to_desc(irq);
  1218. if (!desc)
  1219. return -EINVAL;
  1220. if (!irq_settings_can_request(desc) ||
  1221. WARN_ON(irq_settings_is_per_cpu_devid(desc)))
  1222. return -EINVAL;
  1223. if (!handler) {
  1224. if (!thread_fn)
  1225. return -EINVAL;
  1226. handler = irq_default_primary_handler;
  1227. }
  1228. action = kzalloc(sizeof(struct irqaction), GFP_KERNEL);
  1229. if (!action)
  1230. return -ENOMEM;
  1231. action->handler = handler;
  1232. action->thread_fn = thread_fn;
  1233. action->flags = irqflags;
  1234. action->name = devname;
  1235. action->dev_id = dev_id;
  1236. chip_bus_lock(desc);
  1237. retval = __setup_irq(irq, desc, action);
  1238. chip_bus_sync_unlock(desc);
  1239. if (retval)
  1240. kfree(action);
  1241. #ifdef CONFIG_DEBUG_SHIRQ_FIXME
  1242. if (!retval && (irqflags & IRQF_SHARED)) {
  1243. /*
  1244. * It's a shared IRQ -- the driver ought to be prepared for it
  1245. * to happen immediately, so let's make sure....
  1246. * We disable the irq to make sure that a 'real' IRQ doesn't
  1247. * run in parallel with our fake.
  1248. */
  1249. unsigned long flags;
  1250. disable_irq(irq);
  1251. local_irq_save(flags);
  1252. handler(irq, dev_id);
  1253. local_irq_restore(flags);
  1254. enable_irq(irq);
  1255. }
  1256. #endif
  1257. return retval;
  1258. }
  1259. EXPORT_SYMBOL(request_threaded_irq);
  1260. /**
  1261. * request_any_context_irq - allocate an interrupt line
  1262. * @irq: Interrupt line to allocate
  1263. * @handler: Function to be called when the IRQ occurs.
  1264. * Threaded handler for threaded interrupts.
  1265. * @flags: Interrupt type flags
  1266. * @name: An ascii name for the claiming device
  1267. * @dev_id: A cookie passed back to the handler function
  1268. *
  1269. * This call allocates interrupt resources and enables the
  1270. * interrupt line and IRQ handling. It selects either a
  1271. * hardirq or threaded handling method depending on the
  1272. * context.
  1273. *
  1274. * On failure, it returns a negative value. On success,
  1275. * it returns either IRQC_IS_HARDIRQ or IRQC_IS_NESTED.
  1276. */
  1277. int request_any_context_irq(unsigned int irq, irq_handler_t handler,
  1278. unsigned long flags, const char *name, void *dev_id)
  1279. {
  1280. struct irq_desc *desc = irq_to_desc(irq);
  1281. int ret;
  1282. if (!desc)
  1283. return -EINVAL;
  1284. if (irq_settings_is_nested_thread(desc)) {
  1285. ret = request_threaded_irq(irq, NULL, handler,
  1286. flags, name, dev_id);
  1287. return !ret ? IRQC_IS_NESTED : ret;
  1288. }
  1289. ret = request_irq(irq, handler, flags, name, dev_id);
  1290. return !ret ? IRQC_IS_HARDIRQ : ret;
  1291. }
  1292. EXPORT_SYMBOL_GPL(request_any_context_irq);
  1293. void irq_set_pending(unsigned int irq)
  1294. {
  1295. struct irq_desc *desc = irq_to_desc(irq);
  1296. unsigned long flags;
  1297. if (desc) {
  1298. raw_spin_lock_irqsave(&desc->lock, flags);
  1299. desc->istate |= IRQS_PENDING;
  1300. raw_spin_unlock_irqrestore(&desc->lock, flags);
  1301. }
  1302. }
  1303. EXPORT_SYMBOL_GPL(irq_set_pending);
  1304. void enable_percpu_irq(unsigned int irq, unsigned int type)
  1305. {
  1306. unsigned int cpu = smp_processor_id();
  1307. unsigned long flags;
  1308. struct irq_desc *desc = irq_get_desc_lock(irq, &flags, IRQ_GET_DESC_CHECK_PERCPU);
  1309. if (!desc)
  1310. return;
  1311. type &= IRQ_TYPE_SENSE_MASK;
  1312. if (type != IRQ_TYPE_NONE) {
  1313. int ret;
  1314. ret = __irq_set_trigger(desc, irq, type);
  1315. if (ret) {
  1316. WARN(1, "failed to set type for IRQ%d\n", irq);
  1317. goto out;
  1318. }
  1319. }
  1320. irq_percpu_enable(desc, cpu);
  1321. out:
  1322. irq_put_desc_unlock(desc, flags);
  1323. }
  1324. void disable_percpu_irq(unsigned int irq)
  1325. {
  1326. unsigned int cpu = smp_processor_id();
  1327. unsigned long flags;
  1328. struct irq_desc *desc = irq_get_desc_lock(irq, &flags, IRQ_GET_DESC_CHECK_PERCPU);
  1329. if (!desc)
  1330. return;
  1331. irq_percpu_disable(desc, cpu);
  1332. irq_put_desc_unlock(desc, flags);
  1333. }
  1334. /*
  1335. * Internal function to unregister a percpu irqaction.
  1336. */
  1337. static struct irqaction *__free_percpu_irq(unsigned int irq, void __percpu *dev_id)
  1338. {
  1339. struct irq_desc *desc = irq_to_desc(irq);
  1340. struct irqaction *action;
  1341. unsigned long flags;
  1342. WARN(in_interrupt(), "Trying to free IRQ %d from IRQ context!\n", irq);
  1343. if (!desc)
  1344. return NULL;
  1345. raw_spin_lock_irqsave(&desc->lock, flags);
  1346. action = desc->action;
  1347. if (!action || action->percpu_dev_id != dev_id) {
  1348. WARN(1, "Trying to free already-free IRQ %d\n", irq);
  1349. goto bad;
  1350. }
  1351. if (!cpumask_empty(desc->percpu_enabled)) {
  1352. WARN(1, "percpu IRQ %d still enabled on CPU%d!\n",
  1353. irq, cpumask_first(desc->percpu_enabled));
  1354. goto bad;
  1355. }
  1356. /* Found it - now remove it from the list of entries: */
  1357. desc->action = NULL;
  1358. raw_spin_unlock_irqrestore(&desc->lock, flags);
  1359. unregister_handler_proc(irq, action);
  1360. module_put(desc->owner);
  1361. return action;
  1362. bad:
  1363. raw_spin_unlock_irqrestore(&desc->lock, flags);
  1364. return NULL;
  1365. }
  1366. /**
  1367. * remove_percpu_irq - free a per-cpu interrupt
  1368. * @irq: Interrupt line to free
  1369. * @act: irqaction for the interrupt
  1370. *
  1371. * Used to remove interrupts statically setup by the early boot process.
  1372. */
  1373. void remove_percpu_irq(unsigned int irq, struct irqaction *act)
  1374. {
  1375. struct irq_desc *desc = irq_to_desc(irq);
  1376. if (desc && irq_settings_is_per_cpu_devid(desc))
  1377. __free_percpu_irq(irq, act->percpu_dev_id);
  1378. }
  1379. /**
  1380. * free_percpu_irq - free an interrupt allocated with request_percpu_irq
  1381. * @irq: Interrupt line to free
  1382. * @dev_id: Device identity to free
  1383. *
  1384. * Remove a percpu interrupt handler. The handler is removed, but
  1385. * the interrupt line is not disabled. This must be done on each
  1386. * CPU before calling this function. The function does not return
  1387. * until any executing interrupts for this IRQ have completed.
  1388. *
  1389. * This function must not be called from interrupt context.
  1390. */
  1391. void free_percpu_irq(unsigned int irq, void __percpu *dev_id)
  1392. {
  1393. struct irq_desc *desc = irq_to_desc(irq);
  1394. if (!desc || !irq_settings_is_per_cpu_devid(desc))
  1395. return;
  1396. chip_bus_lock(desc);
  1397. kfree(__free_percpu_irq(irq, dev_id));
  1398. chip_bus_sync_unlock(desc);
  1399. }
  1400. /**
  1401. * setup_percpu_irq - setup a per-cpu interrupt
  1402. * @irq: Interrupt line to setup
  1403. * @act: irqaction for the interrupt
  1404. *
  1405. * Used to statically setup per-cpu interrupts in the early boot process.
  1406. */
  1407. int setup_percpu_irq(unsigned int irq, struct irqaction *act)
  1408. {
  1409. struct irq_desc *desc = irq_to_desc(irq);
  1410. int retval;
  1411. if (!desc || !irq_settings_is_per_cpu_devid(desc))
  1412. return -EINVAL;
  1413. chip_bus_lock(desc);
  1414. retval = __setup_irq(irq, desc, act);
  1415. chip_bus_sync_unlock(desc);
  1416. return retval;
  1417. }
  1418. /**
  1419. * request_percpu_irq - allocate a percpu interrupt line
  1420. * @irq: Interrupt line to allocate
  1421. * @handler: Function to be called when the IRQ occurs.
  1422. * @devname: An ascii name for the claiming device
  1423. * @dev_id: A percpu cookie passed back to the handler function
  1424. *
  1425. * This call allocates interrupt resources, but doesn't
  1426. * automatically enable the interrupt. It has to be done on each
  1427. * CPU using enable_percpu_irq().
  1428. *
  1429. * Dev_id must be globally unique. It is a per-cpu variable, and
  1430. * the handler gets called with the interrupted CPU's instance of
  1431. * that variable.
  1432. */
  1433. int request_percpu_irq(unsigned int irq, irq_handler_t handler,
  1434. const char *devname, void __percpu *dev_id)
  1435. {
  1436. struct irqaction *action;
  1437. struct irq_desc *desc;
  1438. int retval;
  1439. if (!dev_id)
  1440. return -EINVAL;
  1441. desc = irq_to_desc(irq);
  1442. if (!desc || !irq_settings_can_request(desc) ||
  1443. !irq_settings_is_per_cpu_devid(desc))
  1444. return -EINVAL;
  1445. action = kzalloc(sizeof(struct irqaction), GFP_KERNEL);
  1446. if (!action)
  1447. return -ENOMEM;
  1448. action->handler = handler;
  1449. action->flags = IRQF_PERCPU | IRQF_NO_SUSPEND;
  1450. action->name = devname;
  1451. action->percpu_dev_id = dev_id;
  1452. chip_bus_lock(desc);
  1453. retval = __setup_irq(irq, desc, action);
  1454. chip_bus_sync_unlock(desc);
  1455. if (retval)
  1456. kfree(action);
  1457. return retval;
  1458. }