manage.c 46 KB

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