manage.c 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 1992, 1998-2006 Linus Torvalds, Ingo Molnar
  4. * Copyright (C) 2005-2006 Thomas Gleixner
  5. *
  6. * This file contains driver APIs to the irq subsystem.
  7. */
  8. #define pr_fmt(fmt) "genirq: " fmt
  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/irqdomain.h>
  15. #include <linux/slab.h>
  16. #include <linux/sched.h>
  17. #include <linux/sched/rt.h>
  18. #include <linux/sched/task.h>
  19. #include <linux/sched/isolation.h>
  20. #include <uapi/linux/sched/types.h>
  21. #include <linux/task_work.h>
  22. #include "internals.h"
  23. #if defined(CONFIG_IRQ_FORCED_THREADING) && !defined(CONFIG_PREEMPT_RT)
  24. DEFINE_STATIC_KEY_FALSE(force_irqthreads_key);
  25. static int __init setup_forced_irqthreads(char *arg)
  26. {
  27. static_branch_enable(&force_irqthreads_key);
  28. return 0;
  29. }
  30. early_param("threadirqs", setup_forced_irqthreads);
  31. #endif
  32. static void __synchronize_hardirq(struct irq_desc *desc, bool sync_chip)
  33. {
  34. struct irq_data *irqd = irq_desc_get_irq_data(desc);
  35. bool inprogress;
  36. do {
  37. unsigned long flags;
  38. /*
  39. * Wait until we're out of the critical section. This might
  40. * give the wrong answer due to the lack of memory barriers.
  41. */
  42. while (irqd_irq_inprogress(&desc->irq_data))
  43. cpu_relax();
  44. /* Ok, that indicated we're done: double-check carefully. */
  45. raw_spin_lock_irqsave(&desc->lock, flags);
  46. inprogress = irqd_irq_inprogress(&desc->irq_data);
  47. /*
  48. * If requested and supported, check at the chip whether it
  49. * is in flight at the hardware level, i.e. already pending
  50. * in a CPU and waiting for service and acknowledge.
  51. */
  52. if (!inprogress && sync_chip) {
  53. /*
  54. * Ignore the return code. inprogress is only updated
  55. * when the chip supports it.
  56. */
  57. __irq_get_irqchip_state(irqd, IRQCHIP_STATE_ACTIVE,
  58. &inprogress);
  59. }
  60. raw_spin_unlock_irqrestore(&desc->lock, flags);
  61. /* Oops, that failed? */
  62. } while (inprogress);
  63. }
  64. /**
  65. * synchronize_hardirq - wait for pending hard IRQ handlers (on other CPUs)
  66. * @irq: interrupt number to wait for
  67. *
  68. * This function waits for any pending hard IRQ handlers for this
  69. * interrupt to complete before returning. If you use this
  70. * function while holding a resource the IRQ handler may need you
  71. * will deadlock. It does not take associated threaded handlers
  72. * into account.
  73. *
  74. * Do not use this for shutdown scenarios where you must be sure
  75. * that all parts (hardirq and threaded handler) have completed.
  76. *
  77. * Returns: false if a threaded handler is active.
  78. *
  79. * This function may be called - with care - from IRQ context.
  80. *
  81. * It does not check whether there is an interrupt in flight at the
  82. * hardware level, but not serviced yet, as this might deadlock when
  83. * called with interrupts disabled and the target CPU of the interrupt
  84. * is the current CPU.
  85. */
  86. bool synchronize_hardirq(unsigned int irq)
  87. {
  88. struct irq_desc *desc = irq_to_desc(irq);
  89. if (desc) {
  90. __synchronize_hardirq(desc, false);
  91. return !atomic_read(&desc->threads_active);
  92. }
  93. return true;
  94. }
  95. EXPORT_SYMBOL(synchronize_hardirq);
  96. static void __synchronize_irq(struct irq_desc *desc)
  97. {
  98. __synchronize_hardirq(desc, true);
  99. /*
  100. * We made sure that no hardirq handler is running. Now verify that no
  101. * threaded handlers are active.
  102. */
  103. wait_event(desc->wait_for_threads, !atomic_read(&desc->threads_active));
  104. }
  105. /**
  106. * synchronize_irq - wait for pending IRQ handlers (on other CPUs)
  107. * @irq: interrupt number to wait for
  108. *
  109. * This function waits for any pending IRQ handlers for this interrupt
  110. * to complete before returning. If you use this function while
  111. * holding a resource the IRQ handler may need you will deadlock.
  112. *
  113. * Can only be called from preemptible code as it might sleep when
  114. * an interrupt thread is associated to @irq.
  115. *
  116. * It optionally makes sure (when the irq chip supports that method)
  117. * that the interrupt is not pending in any CPU and waiting for
  118. * service.
  119. */
  120. void synchronize_irq(unsigned int irq)
  121. {
  122. struct irq_desc *desc = irq_to_desc(irq);
  123. if (desc)
  124. __synchronize_irq(desc);
  125. }
  126. EXPORT_SYMBOL(synchronize_irq);
  127. #ifdef CONFIG_SMP
  128. cpumask_var_t irq_default_affinity;
  129. static bool __irq_can_set_affinity(struct irq_desc *desc)
  130. {
  131. if (!desc || !irqd_can_balance(&desc->irq_data) ||
  132. !desc->irq_data.chip || !desc->irq_data.chip->irq_set_affinity)
  133. return false;
  134. return true;
  135. }
  136. /**
  137. * irq_can_set_affinity - Check if the affinity of a given irq can be set
  138. * @irq: Interrupt to check
  139. *
  140. */
  141. int irq_can_set_affinity(unsigned int irq)
  142. {
  143. return __irq_can_set_affinity(irq_to_desc(irq));
  144. }
  145. /**
  146. * irq_can_set_affinity_usr - Check if affinity of a irq can be set from user space
  147. * @irq: Interrupt to check
  148. *
  149. * Like irq_can_set_affinity() above, but additionally checks for the
  150. * AFFINITY_MANAGED flag.
  151. */
  152. bool irq_can_set_affinity_usr(unsigned int irq)
  153. {
  154. struct irq_desc *desc = irq_to_desc(irq);
  155. return __irq_can_set_affinity(desc) &&
  156. !irqd_affinity_is_managed(&desc->irq_data);
  157. }
  158. /**
  159. * irq_set_thread_affinity - Notify irq threads to adjust affinity
  160. * @desc: irq descriptor which has affinity changed
  161. *
  162. * We just set IRQTF_AFFINITY and delegate the affinity setting
  163. * to the interrupt thread itself. We can not call
  164. * set_cpus_allowed_ptr() here as we hold desc->lock and this
  165. * code can be called from hard interrupt context.
  166. */
  167. void irq_set_thread_affinity(struct irq_desc *desc)
  168. {
  169. struct irqaction *action;
  170. for_each_action_of_desc(desc, action) {
  171. if (action->thread) {
  172. set_bit(IRQTF_AFFINITY, &action->thread_flags);
  173. wake_up_process(action->thread);
  174. }
  175. if (action->secondary && action->secondary->thread) {
  176. set_bit(IRQTF_AFFINITY, &action->secondary->thread_flags);
  177. wake_up_process(action->secondary->thread);
  178. }
  179. }
  180. }
  181. #ifdef CONFIG_GENERIC_IRQ_EFFECTIVE_AFF_MASK
  182. static void irq_validate_effective_affinity(struct irq_data *data)
  183. {
  184. const struct cpumask *m = irq_data_get_effective_affinity_mask(data);
  185. struct irq_chip *chip = irq_data_get_irq_chip(data);
  186. if (!cpumask_empty(m))
  187. return;
  188. pr_warn_once("irq_chip %s did not update eff. affinity mask of irq %u\n",
  189. chip->name, data->irq);
  190. }
  191. #else
  192. static inline void irq_validate_effective_affinity(struct irq_data *data) { }
  193. #endif
  194. static DEFINE_PER_CPU(struct cpumask, __tmp_mask);
  195. int irq_do_set_affinity(struct irq_data *data, const struct cpumask *mask,
  196. bool force)
  197. {
  198. struct cpumask *tmp_mask = this_cpu_ptr(&__tmp_mask);
  199. struct irq_desc *desc = irq_data_to_desc(data);
  200. struct irq_chip *chip = irq_data_get_irq_chip(data);
  201. const struct cpumask *prog_mask;
  202. int ret;
  203. if (!chip || !chip->irq_set_affinity)
  204. return -EINVAL;
  205. /*
  206. * If this is a managed interrupt and housekeeping is enabled on
  207. * it check whether the requested affinity mask intersects with
  208. * a housekeeping CPU. If so, then remove the isolated CPUs from
  209. * the mask and just keep the housekeeping CPU(s). This prevents
  210. * the affinity setter from routing the interrupt to an isolated
  211. * CPU to avoid that I/O submitted from a housekeeping CPU causes
  212. * interrupts on an isolated one.
  213. *
  214. * If the masks do not intersect or include online CPU(s) then
  215. * keep the requested mask. The isolated target CPUs are only
  216. * receiving interrupts when the I/O operation was submitted
  217. * directly from them.
  218. *
  219. * If all housekeeping CPUs in the affinity mask are offline, the
  220. * interrupt will be migrated by the CPU hotplug code once a
  221. * housekeeping CPU which belongs to the affinity mask comes
  222. * online.
  223. */
  224. if (irqd_affinity_is_managed(data) &&
  225. housekeeping_enabled(HK_TYPE_MANAGED_IRQ)) {
  226. const struct cpumask *hk_mask;
  227. hk_mask = housekeeping_cpumask(HK_TYPE_MANAGED_IRQ);
  228. cpumask_and(tmp_mask, mask, hk_mask);
  229. if (!cpumask_intersects(tmp_mask, cpu_online_mask))
  230. prog_mask = mask;
  231. else
  232. prog_mask = tmp_mask;
  233. } else {
  234. prog_mask = mask;
  235. }
  236. /*
  237. * Make sure we only provide online CPUs to the irqchip,
  238. * unless we are being asked to force the affinity (in which
  239. * case we do as we are told).
  240. */
  241. cpumask_and(tmp_mask, prog_mask, cpu_online_mask);
  242. if (!force && !cpumask_empty(tmp_mask))
  243. ret = chip->irq_set_affinity(data, tmp_mask, force);
  244. else if (force)
  245. ret = chip->irq_set_affinity(data, mask, force);
  246. else
  247. ret = -EINVAL;
  248. switch (ret) {
  249. case IRQ_SET_MASK_OK:
  250. case IRQ_SET_MASK_OK_DONE:
  251. cpumask_copy(desc->irq_common_data.affinity, mask);
  252. fallthrough;
  253. case IRQ_SET_MASK_OK_NOCOPY:
  254. irq_validate_effective_affinity(data);
  255. irq_set_thread_affinity(desc);
  256. ret = 0;
  257. }
  258. return ret;
  259. }
  260. #ifdef CONFIG_GENERIC_PENDING_IRQ
  261. static inline int irq_set_affinity_pending(struct irq_data *data,
  262. const struct cpumask *dest)
  263. {
  264. struct irq_desc *desc = irq_data_to_desc(data);
  265. irqd_set_move_pending(data);
  266. irq_copy_pending(desc, dest);
  267. return 0;
  268. }
  269. #else
  270. static inline int irq_set_affinity_pending(struct irq_data *data,
  271. const struct cpumask *dest)
  272. {
  273. return -EBUSY;
  274. }
  275. #endif
  276. static int irq_try_set_affinity(struct irq_data *data,
  277. const struct cpumask *dest, bool force)
  278. {
  279. int ret = irq_do_set_affinity(data, dest, force);
  280. /*
  281. * In case that the underlying vector management is busy and the
  282. * architecture supports the generic pending mechanism then utilize
  283. * this to avoid returning an error to user space.
  284. */
  285. if (ret == -EBUSY && !force)
  286. ret = irq_set_affinity_pending(data, dest);
  287. return ret;
  288. }
  289. static bool irq_set_affinity_deactivated(struct irq_data *data,
  290. const struct cpumask *mask)
  291. {
  292. struct irq_desc *desc = irq_data_to_desc(data);
  293. /*
  294. * Handle irq chips which can handle affinity only in activated
  295. * state correctly
  296. *
  297. * If the interrupt is not yet activated, just store the affinity
  298. * mask and do not call the chip driver at all. On activation the
  299. * driver has to make sure anyway that the interrupt is in a
  300. * usable state so startup works.
  301. */
  302. if (!IS_ENABLED(CONFIG_IRQ_DOMAIN_HIERARCHY) ||
  303. irqd_is_activated(data) || !irqd_affinity_on_activate(data))
  304. return false;
  305. cpumask_copy(desc->irq_common_data.affinity, mask);
  306. irq_data_update_effective_affinity(data, mask);
  307. irqd_set(data, IRQD_AFFINITY_SET);
  308. return true;
  309. }
  310. int irq_set_affinity_locked(struct irq_data *data, const struct cpumask *mask,
  311. bool force)
  312. {
  313. struct irq_chip *chip = irq_data_get_irq_chip(data);
  314. struct irq_desc *desc = irq_data_to_desc(data);
  315. int ret = 0;
  316. if (!chip || !chip->irq_set_affinity)
  317. return -EINVAL;
  318. if (irq_set_affinity_deactivated(data, mask))
  319. return 0;
  320. if (irq_can_move_pcntxt(data) && !irqd_is_setaffinity_pending(data)) {
  321. ret = irq_try_set_affinity(data, mask, force);
  322. } else {
  323. irqd_set_move_pending(data);
  324. irq_copy_pending(desc, mask);
  325. }
  326. if (desc->affinity_notify) {
  327. kref_get(&desc->affinity_notify->kref);
  328. if (!schedule_work(&desc->affinity_notify->work)) {
  329. /* Work was already scheduled, drop our extra ref */
  330. kref_put(&desc->affinity_notify->kref,
  331. desc->affinity_notify->release);
  332. }
  333. }
  334. irqd_set(data, IRQD_AFFINITY_SET);
  335. return ret;
  336. }
  337. /**
  338. * irq_update_affinity_desc - Update affinity management for an interrupt
  339. * @irq: The interrupt number to update
  340. * @affinity: Pointer to the affinity descriptor
  341. *
  342. * This interface can be used to configure the affinity management of
  343. * interrupts which have been allocated already.
  344. *
  345. * There are certain limitations on when it may be used - attempts to use it
  346. * for when the kernel is configured for generic IRQ reservation mode (in
  347. * config GENERIC_IRQ_RESERVATION_MODE) will fail, as it may conflict with
  348. * managed/non-managed interrupt accounting. In addition, attempts to use it on
  349. * an interrupt which is already started or which has already been configured
  350. * as managed will also fail, as these mean invalid init state or double init.
  351. */
  352. int irq_update_affinity_desc(unsigned int irq,
  353. struct irq_affinity_desc *affinity)
  354. {
  355. struct irq_desc *desc;
  356. unsigned long flags;
  357. bool activated;
  358. int ret = 0;
  359. /*
  360. * Supporting this with the reservation scheme used by x86 needs
  361. * some more thought. Fail it for now.
  362. */
  363. if (IS_ENABLED(CONFIG_GENERIC_IRQ_RESERVATION_MODE))
  364. return -EOPNOTSUPP;
  365. desc = irq_get_desc_buslock(irq, &flags, 0);
  366. if (!desc)
  367. return -EINVAL;
  368. /* Requires the interrupt to be shut down */
  369. if (irqd_is_started(&desc->irq_data)) {
  370. ret = -EBUSY;
  371. goto out_unlock;
  372. }
  373. /* Interrupts which are already managed cannot be modified */
  374. if (irqd_affinity_is_managed(&desc->irq_data)) {
  375. ret = -EBUSY;
  376. goto out_unlock;
  377. }
  378. /*
  379. * Deactivate the interrupt. That's required to undo
  380. * anything an earlier activation has established.
  381. */
  382. activated = irqd_is_activated(&desc->irq_data);
  383. if (activated)
  384. irq_domain_deactivate_irq(&desc->irq_data);
  385. if (affinity->is_managed) {
  386. irqd_set(&desc->irq_data, IRQD_AFFINITY_MANAGED);
  387. irqd_set(&desc->irq_data, IRQD_MANAGED_SHUTDOWN);
  388. }
  389. cpumask_copy(desc->irq_common_data.affinity, &affinity->mask);
  390. /* Restore the activation state */
  391. if (activated)
  392. irq_domain_activate_irq(&desc->irq_data, false);
  393. out_unlock:
  394. irq_put_desc_busunlock(desc, flags);
  395. return ret;
  396. }
  397. static int __irq_set_affinity(unsigned int irq, const struct cpumask *mask,
  398. bool force)
  399. {
  400. struct irq_desc *desc = irq_to_desc(irq);
  401. unsigned long flags;
  402. int ret;
  403. if (!desc)
  404. return -EINVAL;
  405. raw_spin_lock_irqsave(&desc->lock, flags);
  406. ret = irq_set_affinity_locked(irq_desc_get_irq_data(desc), mask, force);
  407. raw_spin_unlock_irqrestore(&desc->lock, flags);
  408. return ret;
  409. }
  410. /**
  411. * irq_set_affinity - Set the irq affinity of a given irq
  412. * @irq: Interrupt to set affinity
  413. * @cpumask: cpumask
  414. *
  415. * Fails if cpumask does not contain an online CPU
  416. */
  417. int irq_set_affinity(unsigned int irq, const struct cpumask *cpumask)
  418. {
  419. return __irq_set_affinity(irq, cpumask, false);
  420. }
  421. EXPORT_SYMBOL_GPL(irq_set_affinity);
  422. /**
  423. * irq_force_affinity - Force the irq affinity of a given irq
  424. * @irq: Interrupt to set affinity
  425. * @cpumask: cpumask
  426. *
  427. * Same as irq_set_affinity, but without checking the mask against
  428. * online cpus.
  429. *
  430. * Solely for low level cpu hotplug code, where we need to make per
  431. * cpu interrupts affine before the cpu becomes online.
  432. */
  433. int irq_force_affinity(unsigned int irq, const struct cpumask *cpumask)
  434. {
  435. return __irq_set_affinity(irq, cpumask, true);
  436. }
  437. EXPORT_SYMBOL_GPL(irq_force_affinity);
  438. int __irq_apply_affinity_hint(unsigned int irq, const struct cpumask *m,
  439. bool setaffinity)
  440. {
  441. unsigned long flags;
  442. struct irq_desc *desc = irq_get_desc_lock(irq, &flags, IRQ_GET_DESC_CHECK_GLOBAL);
  443. if (!desc)
  444. return -EINVAL;
  445. desc->affinity_hint = m;
  446. irq_put_desc_unlock(desc, flags);
  447. if (m && setaffinity)
  448. __irq_set_affinity(irq, m, false);
  449. return 0;
  450. }
  451. EXPORT_SYMBOL_GPL(__irq_apply_affinity_hint);
  452. static void irq_affinity_notify(struct work_struct *work)
  453. {
  454. struct irq_affinity_notify *notify =
  455. container_of(work, struct irq_affinity_notify, work);
  456. struct irq_desc *desc = irq_to_desc(notify->irq);
  457. cpumask_var_t cpumask;
  458. unsigned long flags;
  459. if (!desc || !alloc_cpumask_var(&cpumask, GFP_KERNEL))
  460. goto out;
  461. raw_spin_lock_irqsave(&desc->lock, flags);
  462. if (irq_move_pending(&desc->irq_data))
  463. irq_get_pending(cpumask, desc);
  464. else
  465. cpumask_copy(cpumask, desc->irq_common_data.affinity);
  466. raw_spin_unlock_irqrestore(&desc->lock, flags);
  467. notify->notify(notify, cpumask);
  468. free_cpumask_var(cpumask);
  469. out:
  470. kref_put(&notify->kref, notify->release);
  471. }
  472. /**
  473. * irq_set_affinity_notifier - control notification of IRQ affinity changes
  474. * @irq: Interrupt for which to enable/disable notification
  475. * @notify: Context for notification, or %NULL to disable
  476. * notification. Function pointers must be initialised;
  477. * the other fields will be initialised by this function.
  478. *
  479. * Must be called in process context. Notification may only be enabled
  480. * after the IRQ is allocated and must be disabled before the IRQ is
  481. * freed using free_irq().
  482. */
  483. int
  484. irq_set_affinity_notifier(unsigned int irq, struct irq_affinity_notify *notify)
  485. {
  486. struct irq_desc *desc = irq_to_desc(irq);
  487. struct irq_affinity_notify *old_notify;
  488. unsigned long flags;
  489. /* The release function is promised process context */
  490. might_sleep();
  491. if (!desc || irq_is_nmi(desc))
  492. return -EINVAL;
  493. /* Complete initialisation of *notify */
  494. if (notify) {
  495. notify->irq = irq;
  496. kref_init(&notify->kref);
  497. INIT_WORK(&notify->work, irq_affinity_notify);
  498. }
  499. raw_spin_lock_irqsave(&desc->lock, flags);
  500. old_notify = desc->affinity_notify;
  501. desc->affinity_notify = notify;
  502. raw_spin_unlock_irqrestore(&desc->lock, flags);
  503. if (old_notify) {
  504. if (cancel_work_sync(&old_notify->work)) {
  505. /* Pending work had a ref, put that one too */
  506. kref_put(&old_notify->kref, old_notify->release);
  507. }
  508. kref_put(&old_notify->kref, old_notify->release);
  509. }
  510. return 0;
  511. }
  512. EXPORT_SYMBOL_GPL(irq_set_affinity_notifier);
  513. #ifndef CONFIG_AUTO_IRQ_AFFINITY
  514. /*
  515. * Generic version of the affinity autoselector.
  516. */
  517. int irq_setup_affinity(struct irq_desc *desc)
  518. {
  519. struct cpumask *set = irq_default_affinity;
  520. int ret, node = irq_desc_get_node(desc);
  521. static DEFINE_RAW_SPINLOCK(mask_lock);
  522. static struct cpumask mask;
  523. /* Excludes PER_CPU and NO_BALANCE interrupts */
  524. if (!__irq_can_set_affinity(desc))
  525. return 0;
  526. raw_spin_lock(&mask_lock);
  527. /*
  528. * Preserve the managed affinity setting and a userspace affinity
  529. * setup, but make sure that one of the targets is online.
  530. */
  531. if (irqd_affinity_is_managed(&desc->irq_data) ||
  532. irqd_has_set(&desc->irq_data, IRQD_AFFINITY_SET)) {
  533. if (cpumask_intersects(desc->irq_common_data.affinity,
  534. cpu_online_mask))
  535. set = desc->irq_common_data.affinity;
  536. else
  537. irqd_clear(&desc->irq_data, IRQD_AFFINITY_SET);
  538. }
  539. cpumask_and(&mask, cpu_online_mask, set);
  540. if (cpumask_empty(&mask))
  541. cpumask_copy(&mask, cpu_online_mask);
  542. if (node != NUMA_NO_NODE) {
  543. const struct cpumask *nodemask = cpumask_of_node(node);
  544. /* make sure at least one of the cpus in nodemask is online */
  545. if (cpumask_intersects(&mask, nodemask))
  546. cpumask_and(&mask, &mask, nodemask);
  547. }
  548. ret = irq_do_set_affinity(&desc->irq_data, &mask, false);
  549. raw_spin_unlock(&mask_lock);
  550. return ret;
  551. }
  552. #else
  553. /* Wrapper for ALPHA specific affinity selector magic */
  554. int irq_setup_affinity(struct irq_desc *desc)
  555. {
  556. return irq_select_affinity(irq_desc_get_irq(desc));
  557. }
  558. #endif /* CONFIG_AUTO_IRQ_AFFINITY */
  559. #endif /* CONFIG_SMP */
  560. /**
  561. * irq_set_vcpu_affinity - Set vcpu affinity for the interrupt
  562. * @irq: interrupt number to set affinity
  563. * @vcpu_info: vCPU specific data or pointer to a percpu array of vCPU
  564. * specific data for percpu_devid interrupts
  565. *
  566. * This function uses the vCPU specific data to set the vCPU
  567. * affinity for an irq. The vCPU specific data is passed from
  568. * outside, such as KVM. One example code path is as below:
  569. * KVM -> IOMMU -> irq_set_vcpu_affinity().
  570. */
  571. int irq_set_vcpu_affinity(unsigned int irq, void *vcpu_info)
  572. {
  573. unsigned long flags;
  574. struct irq_desc *desc = irq_get_desc_lock(irq, &flags, 0);
  575. struct irq_data *data;
  576. struct irq_chip *chip;
  577. int ret = -ENOSYS;
  578. if (!desc)
  579. return -EINVAL;
  580. data = irq_desc_get_irq_data(desc);
  581. do {
  582. chip = irq_data_get_irq_chip(data);
  583. if (chip && chip->irq_set_vcpu_affinity)
  584. break;
  585. #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
  586. data = data->parent_data;
  587. #else
  588. data = NULL;
  589. #endif
  590. } while (data);
  591. if (data)
  592. ret = chip->irq_set_vcpu_affinity(data, vcpu_info);
  593. irq_put_desc_unlock(desc, flags);
  594. return ret;
  595. }
  596. EXPORT_SYMBOL_GPL(irq_set_vcpu_affinity);
  597. void __disable_irq(struct irq_desc *desc)
  598. {
  599. if (!desc->depth++)
  600. irq_disable(desc);
  601. }
  602. static int __disable_irq_nosync(unsigned int irq)
  603. {
  604. unsigned long flags;
  605. struct irq_desc *desc = irq_get_desc_buslock(irq, &flags, IRQ_GET_DESC_CHECK_GLOBAL);
  606. if (!desc)
  607. return -EINVAL;
  608. __disable_irq(desc);
  609. irq_put_desc_busunlock(desc, flags);
  610. return 0;
  611. }
  612. /**
  613. * disable_irq_nosync - disable an irq without waiting
  614. * @irq: Interrupt to disable
  615. *
  616. * Disable the selected interrupt line. Disables and Enables are
  617. * nested.
  618. * Unlike disable_irq(), this function does not ensure existing
  619. * instances of the IRQ handler have completed before returning.
  620. *
  621. * This function may be called from IRQ context.
  622. */
  623. void disable_irq_nosync(unsigned int irq)
  624. {
  625. __disable_irq_nosync(irq);
  626. }
  627. EXPORT_SYMBOL(disable_irq_nosync);
  628. /**
  629. * disable_irq - disable an irq and wait for completion
  630. * @irq: Interrupt to disable
  631. *
  632. * Disable the selected interrupt line. Enables and Disables are
  633. * nested.
  634. * This function waits for any pending IRQ handlers for this interrupt
  635. * to complete before returning. If you use this function while
  636. * holding a resource the IRQ handler may need you will deadlock.
  637. *
  638. * Can only be called from preemptible code as it might sleep when
  639. * an interrupt thread is associated to @irq.
  640. *
  641. */
  642. void disable_irq(unsigned int irq)
  643. {
  644. might_sleep();
  645. if (!__disable_irq_nosync(irq))
  646. synchronize_irq(irq);
  647. }
  648. EXPORT_SYMBOL(disable_irq);
  649. /**
  650. * disable_hardirq - disables an irq and waits for hardirq completion
  651. * @irq: Interrupt to disable
  652. *
  653. * Disable the selected interrupt line. Enables and Disables are
  654. * nested.
  655. * This function waits for any pending hard IRQ handlers for this
  656. * interrupt to complete before returning. If you use this function while
  657. * holding a resource the hard IRQ handler may need you will deadlock.
  658. *
  659. * When used to optimistically disable an interrupt from atomic context
  660. * the return value must be checked.
  661. *
  662. * Returns: false if a threaded handler is active.
  663. *
  664. * This function may be called - with care - from IRQ context.
  665. */
  666. bool disable_hardirq(unsigned int irq)
  667. {
  668. if (!__disable_irq_nosync(irq))
  669. return synchronize_hardirq(irq);
  670. return false;
  671. }
  672. EXPORT_SYMBOL_GPL(disable_hardirq);
  673. /**
  674. * disable_nmi_nosync - disable an nmi without waiting
  675. * @irq: Interrupt to disable
  676. *
  677. * Disable the selected interrupt line. Disables and enables are
  678. * nested.
  679. * The interrupt to disable must have been requested through request_nmi.
  680. * Unlike disable_nmi(), this function does not ensure existing
  681. * instances of the IRQ handler have completed before returning.
  682. */
  683. void disable_nmi_nosync(unsigned int irq)
  684. {
  685. disable_irq_nosync(irq);
  686. }
  687. void __enable_irq(struct irq_desc *desc)
  688. {
  689. switch (desc->depth) {
  690. case 0:
  691. err_out:
  692. WARN(1, KERN_WARNING "Unbalanced enable for IRQ %d\n",
  693. irq_desc_get_irq(desc));
  694. break;
  695. case 1: {
  696. if (desc->istate & IRQS_SUSPENDED)
  697. goto err_out;
  698. /* Prevent probing on this irq: */
  699. irq_settings_set_noprobe(desc);
  700. /*
  701. * Call irq_startup() not irq_enable() here because the
  702. * interrupt might be marked NOAUTOEN so irq_startup()
  703. * needs to be invoked when it gets enabled the first time.
  704. * This is also required when __enable_irq() is invoked for
  705. * a managed and shutdown interrupt from the S3 resume
  706. * path.
  707. *
  708. * If it was already started up, then irq_startup() will
  709. * invoke irq_enable() under the hood.
  710. */
  711. irq_startup(desc, IRQ_RESEND, IRQ_START_FORCE);
  712. break;
  713. }
  714. default:
  715. desc->depth--;
  716. }
  717. }
  718. /**
  719. * enable_irq - enable handling of an irq
  720. * @irq: Interrupt to enable
  721. *
  722. * Undoes the effect of one call to disable_irq(). If this
  723. * matches the last disable, processing of interrupts on this
  724. * IRQ line is re-enabled.
  725. *
  726. * This function may be called from IRQ context only when
  727. * desc->irq_data.chip->bus_lock and desc->chip->bus_sync_unlock are NULL !
  728. */
  729. void enable_irq(unsigned int irq)
  730. {
  731. unsigned long flags;
  732. struct irq_desc *desc = irq_get_desc_buslock(irq, &flags, IRQ_GET_DESC_CHECK_GLOBAL);
  733. if (!desc)
  734. return;
  735. if (WARN(!desc->irq_data.chip,
  736. KERN_ERR "enable_irq before setup/request_irq: irq %u\n", irq))
  737. goto out;
  738. __enable_irq(desc);
  739. out:
  740. irq_put_desc_busunlock(desc, flags);
  741. }
  742. EXPORT_SYMBOL(enable_irq);
  743. /**
  744. * enable_nmi - enable handling of an nmi
  745. * @irq: Interrupt to enable
  746. *
  747. * The interrupt to enable must have been requested through request_nmi.
  748. * Undoes the effect of one call to disable_nmi(). If this
  749. * matches the last disable, processing of interrupts on this
  750. * IRQ line is re-enabled.
  751. */
  752. void enable_nmi(unsigned int irq)
  753. {
  754. enable_irq(irq);
  755. }
  756. static int set_irq_wake_real(unsigned int irq, unsigned int on)
  757. {
  758. struct irq_desc *desc = irq_to_desc(irq);
  759. int ret = -ENXIO;
  760. if (irq_desc_get_chip(desc)->flags & IRQCHIP_SKIP_SET_WAKE)
  761. return 0;
  762. if (desc->irq_data.chip->irq_set_wake)
  763. ret = desc->irq_data.chip->irq_set_wake(&desc->irq_data, on);
  764. return ret;
  765. }
  766. /**
  767. * irq_set_irq_wake - control irq power management wakeup
  768. * @irq: interrupt to control
  769. * @on: enable/disable power management wakeup
  770. *
  771. * Enable/disable power management wakeup mode, which is
  772. * disabled by default. Enables and disables must match,
  773. * just as they match for non-wakeup mode support.
  774. *
  775. * Wakeup mode lets this IRQ wake the system from sleep
  776. * states like "suspend to RAM".
  777. *
  778. * Note: irq enable/disable state is completely orthogonal
  779. * to the enable/disable state of irq wake. An irq can be
  780. * disabled with disable_irq() and still wake the system as
  781. * long as the irq has wake enabled. If this does not hold,
  782. * then the underlying irq chip and the related driver need
  783. * to be investigated.
  784. */
  785. int irq_set_irq_wake(unsigned int irq, unsigned int on)
  786. {
  787. unsigned long flags;
  788. struct irq_desc *desc = irq_get_desc_buslock(irq, &flags, IRQ_GET_DESC_CHECK_GLOBAL);
  789. int ret = 0;
  790. if (!desc)
  791. return -EINVAL;
  792. /* Don't use NMIs as wake up interrupts please */
  793. if (irq_is_nmi(desc)) {
  794. ret = -EINVAL;
  795. goto out_unlock;
  796. }
  797. /* wakeup-capable irqs can be shared between drivers that
  798. * don't need to have the same sleep mode behaviors.
  799. */
  800. if (on) {
  801. if (desc->wake_depth++ == 0) {
  802. ret = set_irq_wake_real(irq, on);
  803. if (ret)
  804. desc->wake_depth = 0;
  805. else
  806. irqd_set(&desc->irq_data, IRQD_WAKEUP_STATE);
  807. }
  808. } else {
  809. if (desc->wake_depth == 0) {
  810. WARN(1, "Unbalanced IRQ %d wake disable\n", irq);
  811. } else if (--desc->wake_depth == 0) {
  812. ret = set_irq_wake_real(irq, on);
  813. if (ret)
  814. desc->wake_depth = 1;
  815. else
  816. irqd_clear(&desc->irq_data, IRQD_WAKEUP_STATE);
  817. }
  818. }
  819. out_unlock:
  820. irq_put_desc_busunlock(desc, flags);
  821. return ret;
  822. }
  823. EXPORT_SYMBOL(irq_set_irq_wake);
  824. /*
  825. * Internal function that tells the architecture code whether a
  826. * particular irq has been exclusively allocated or is available
  827. * for driver use.
  828. */
  829. int can_request_irq(unsigned int irq, unsigned long irqflags)
  830. {
  831. unsigned long flags;
  832. struct irq_desc *desc = irq_get_desc_lock(irq, &flags, 0);
  833. int canrequest = 0;
  834. if (!desc)
  835. return 0;
  836. if (irq_settings_can_request(desc)) {
  837. if (!desc->action ||
  838. irqflags & desc->action->flags & IRQF_SHARED)
  839. canrequest = 1;
  840. }
  841. irq_put_desc_unlock(desc, flags);
  842. return canrequest;
  843. }
  844. int __irq_set_trigger(struct irq_desc *desc, unsigned long flags)
  845. {
  846. struct irq_chip *chip = desc->irq_data.chip;
  847. int ret, unmask = 0;
  848. if (!chip || !chip->irq_set_type) {
  849. /*
  850. * IRQF_TRIGGER_* but the PIC does not support multiple
  851. * flow-types?
  852. */
  853. pr_debug("No set_type function for IRQ %d (%s)\n",
  854. irq_desc_get_irq(desc),
  855. chip ? (chip->name ? : "unknown") : "unknown");
  856. return 0;
  857. }
  858. if (chip->flags & IRQCHIP_SET_TYPE_MASKED) {
  859. if (!irqd_irq_masked(&desc->irq_data))
  860. mask_irq(desc);
  861. if (!irqd_irq_disabled(&desc->irq_data))
  862. unmask = 1;
  863. }
  864. /* Mask all flags except trigger mode */
  865. flags &= IRQ_TYPE_SENSE_MASK;
  866. ret = chip->irq_set_type(&desc->irq_data, flags);
  867. switch (ret) {
  868. case IRQ_SET_MASK_OK:
  869. case IRQ_SET_MASK_OK_DONE:
  870. irqd_clear(&desc->irq_data, IRQD_TRIGGER_MASK);
  871. irqd_set(&desc->irq_data, flags);
  872. fallthrough;
  873. case IRQ_SET_MASK_OK_NOCOPY:
  874. flags = irqd_get_trigger_type(&desc->irq_data);
  875. irq_settings_set_trigger_mask(desc, flags);
  876. irqd_clear(&desc->irq_data, IRQD_LEVEL);
  877. irq_settings_clr_level(desc);
  878. if (flags & IRQ_TYPE_LEVEL_MASK) {
  879. irq_settings_set_level(desc);
  880. irqd_set(&desc->irq_data, IRQD_LEVEL);
  881. }
  882. ret = 0;
  883. break;
  884. default:
  885. pr_err("Setting trigger mode %lu for irq %u failed (%pS)\n",
  886. flags, irq_desc_get_irq(desc), chip->irq_set_type);
  887. }
  888. if (unmask)
  889. unmask_irq(desc);
  890. return ret;
  891. }
  892. #ifdef CONFIG_HARDIRQS_SW_RESEND
  893. int irq_set_parent(int irq, int parent_irq)
  894. {
  895. unsigned long flags;
  896. struct irq_desc *desc = irq_get_desc_lock(irq, &flags, 0);
  897. if (!desc)
  898. return -EINVAL;
  899. desc->parent_irq = parent_irq;
  900. irq_put_desc_unlock(desc, flags);
  901. return 0;
  902. }
  903. EXPORT_SYMBOL_GPL(irq_set_parent);
  904. #endif
  905. /*
  906. * Default primary interrupt handler for threaded interrupts. Is
  907. * assigned as primary handler when request_threaded_irq is called
  908. * with handler == NULL. Useful for oneshot interrupts.
  909. */
  910. static irqreturn_t irq_default_primary_handler(int irq, void *dev_id)
  911. {
  912. return IRQ_WAKE_THREAD;
  913. }
  914. /*
  915. * Primary handler for nested threaded interrupts. Should never be
  916. * called.
  917. */
  918. static irqreturn_t irq_nested_primary_handler(int irq, void *dev_id)
  919. {
  920. WARN(1, "Primary handler called for nested irq %d\n", irq);
  921. return IRQ_NONE;
  922. }
  923. static irqreturn_t irq_forced_secondary_handler(int irq, void *dev_id)
  924. {
  925. WARN(1, "Secondary action handler called for irq %d\n", irq);
  926. return IRQ_NONE;
  927. }
  928. #ifdef CONFIG_SMP
  929. /*
  930. * Check whether we need to change the affinity of the interrupt thread.
  931. */
  932. static void irq_thread_check_affinity(struct irq_desc *desc, struct irqaction *action)
  933. {
  934. cpumask_var_t mask;
  935. bool valid = false;
  936. if (!test_and_clear_bit(IRQTF_AFFINITY, &action->thread_flags))
  937. return;
  938. __set_current_state(TASK_RUNNING);
  939. /*
  940. * In case we are out of memory we set IRQTF_AFFINITY again and
  941. * try again next time
  942. */
  943. if (!alloc_cpumask_var(&mask, GFP_KERNEL)) {
  944. set_bit(IRQTF_AFFINITY, &action->thread_flags);
  945. return;
  946. }
  947. raw_spin_lock_irq(&desc->lock);
  948. /*
  949. * This code is triggered unconditionally. Check the affinity
  950. * mask pointer. For CPU_MASK_OFFSTACK=n this is optimized out.
  951. */
  952. if (cpumask_available(desc->irq_common_data.affinity)) {
  953. const struct cpumask *m;
  954. m = irq_data_get_effective_affinity_mask(&desc->irq_data);
  955. cpumask_copy(mask, m);
  956. valid = true;
  957. }
  958. raw_spin_unlock_irq(&desc->lock);
  959. if (valid)
  960. set_cpus_allowed_ptr(current, mask);
  961. free_cpumask_var(mask);
  962. }
  963. #else
  964. static inline void irq_thread_check_affinity(struct irq_desc *desc, struct irqaction *action) { }
  965. #endif
  966. static int irq_wait_for_interrupt(struct irq_desc *desc,
  967. struct irqaction *action)
  968. {
  969. for (;;) {
  970. set_current_state(TASK_INTERRUPTIBLE);
  971. irq_thread_check_affinity(desc, action);
  972. if (kthread_should_stop()) {
  973. /* may need to run one last time */
  974. if (test_and_clear_bit(IRQTF_RUNTHREAD,
  975. &action->thread_flags)) {
  976. __set_current_state(TASK_RUNNING);
  977. return 0;
  978. }
  979. __set_current_state(TASK_RUNNING);
  980. return -1;
  981. }
  982. if (test_and_clear_bit(IRQTF_RUNTHREAD,
  983. &action->thread_flags)) {
  984. __set_current_state(TASK_RUNNING);
  985. return 0;
  986. }
  987. schedule();
  988. }
  989. }
  990. /*
  991. * Oneshot interrupts keep the irq line masked until the threaded
  992. * handler finished. unmask if the interrupt has not been disabled and
  993. * is marked MASKED.
  994. */
  995. static void irq_finalize_oneshot(struct irq_desc *desc,
  996. struct irqaction *action)
  997. {
  998. if (!(desc->istate & IRQS_ONESHOT) ||
  999. action->handler == irq_forced_secondary_handler)
  1000. return;
  1001. again:
  1002. chip_bus_lock(desc);
  1003. raw_spin_lock_irq(&desc->lock);
  1004. /*
  1005. * Implausible though it may be we need to protect us against
  1006. * the following scenario:
  1007. *
  1008. * The thread is faster done than the hard interrupt handler
  1009. * on the other CPU. If we unmask the irq line then the
  1010. * interrupt can come in again and masks the line, leaves due
  1011. * to IRQS_INPROGRESS and the irq line is masked forever.
  1012. *
  1013. * This also serializes the state of shared oneshot handlers
  1014. * versus "desc->threads_oneshot |= action->thread_mask;" in
  1015. * irq_wake_thread(). See the comment there which explains the
  1016. * serialization.
  1017. */
  1018. if (unlikely(irqd_irq_inprogress(&desc->irq_data))) {
  1019. raw_spin_unlock_irq(&desc->lock);
  1020. chip_bus_sync_unlock(desc);
  1021. cpu_relax();
  1022. goto again;
  1023. }
  1024. /*
  1025. * Now check again, whether the thread should run. Otherwise
  1026. * we would clear the threads_oneshot bit of this thread which
  1027. * was just set.
  1028. */
  1029. if (test_bit(IRQTF_RUNTHREAD, &action->thread_flags))
  1030. goto out_unlock;
  1031. desc->threads_oneshot &= ~action->thread_mask;
  1032. if (!desc->threads_oneshot && !irqd_irq_disabled(&desc->irq_data) &&
  1033. irqd_irq_masked(&desc->irq_data))
  1034. unmask_threaded_irq(desc);
  1035. out_unlock:
  1036. raw_spin_unlock_irq(&desc->lock);
  1037. chip_bus_sync_unlock(desc);
  1038. }
  1039. /*
  1040. * Interrupts which are not explicitly requested as threaded
  1041. * interrupts rely on the implicit bh/preempt disable of the hard irq
  1042. * context. So we need to disable bh here to avoid deadlocks and other
  1043. * side effects.
  1044. */
  1045. static irqreturn_t
  1046. irq_forced_thread_fn(struct irq_desc *desc, struct irqaction *action)
  1047. {
  1048. irqreturn_t ret;
  1049. local_bh_disable();
  1050. if (!IS_ENABLED(CONFIG_PREEMPT_RT))
  1051. local_irq_disable();
  1052. ret = action->thread_fn(action->irq, action->dev_id);
  1053. if (ret == IRQ_HANDLED)
  1054. atomic_inc(&desc->threads_handled);
  1055. irq_finalize_oneshot(desc, action);
  1056. if (!IS_ENABLED(CONFIG_PREEMPT_RT))
  1057. local_irq_enable();
  1058. local_bh_enable();
  1059. return ret;
  1060. }
  1061. /*
  1062. * Interrupts explicitly requested as threaded interrupts want to be
  1063. * preemptible - many of them need to sleep and wait for slow busses to
  1064. * complete.
  1065. */
  1066. static irqreturn_t irq_thread_fn(struct irq_desc *desc,
  1067. struct irqaction *action)
  1068. {
  1069. irqreturn_t ret;
  1070. ret = action->thread_fn(action->irq, action->dev_id);
  1071. if (ret == IRQ_HANDLED)
  1072. atomic_inc(&desc->threads_handled);
  1073. irq_finalize_oneshot(desc, action);
  1074. return ret;
  1075. }
  1076. void wake_threads_waitq(struct irq_desc *desc)
  1077. {
  1078. if (atomic_dec_and_test(&desc->threads_active))
  1079. wake_up(&desc->wait_for_threads);
  1080. }
  1081. static void irq_thread_dtor(struct callback_head *unused)
  1082. {
  1083. struct task_struct *tsk = current;
  1084. struct irq_desc *desc;
  1085. struct irqaction *action;
  1086. if (WARN_ON_ONCE(!(current->flags & PF_EXITING)))
  1087. return;
  1088. action = kthread_data(tsk);
  1089. pr_err("exiting task \"%s\" (%d) is an active IRQ thread (irq %d)\n",
  1090. tsk->comm, tsk->pid, action->irq);
  1091. desc = irq_to_desc(action->irq);
  1092. /*
  1093. * If IRQTF_RUNTHREAD is set, we need to decrement
  1094. * desc->threads_active and wake possible waiters.
  1095. */
  1096. if (test_and_clear_bit(IRQTF_RUNTHREAD, &action->thread_flags))
  1097. wake_threads_waitq(desc);
  1098. /* Prevent a stale desc->threads_oneshot */
  1099. irq_finalize_oneshot(desc, action);
  1100. }
  1101. static void irq_wake_secondary(struct irq_desc *desc, struct irqaction *action)
  1102. {
  1103. struct irqaction *secondary = action->secondary;
  1104. if (WARN_ON_ONCE(!secondary))
  1105. return;
  1106. raw_spin_lock_irq(&desc->lock);
  1107. __irq_wake_thread(desc, secondary);
  1108. raw_spin_unlock_irq(&desc->lock);
  1109. }
  1110. /*
  1111. * Internal function to notify that a interrupt thread is ready.
  1112. */
  1113. static void irq_thread_set_ready(struct irq_desc *desc,
  1114. struct irqaction *action)
  1115. {
  1116. set_bit(IRQTF_READY, &action->thread_flags);
  1117. wake_up(&desc->wait_for_threads);
  1118. }
  1119. /*
  1120. * Internal function to wake up a interrupt thread and wait until it is
  1121. * ready.
  1122. */
  1123. static void wake_up_and_wait_for_irq_thread_ready(struct irq_desc *desc,
  1124. struct irqaction *action)
  1125. {
  1126. if (!action || !action->thread)
  1127. return;
  1128. wake_up_process(action->thread);
  1129. wait_event(desc->wait_for_threads,
  1130. test_bit(IRQTF_READY, &action->thread_flags));
  1131. }
  1132. /*
  1133. * Interrupt handler thread
  1134. */
  1135. static int irq_thread(void *data)
  1136. {
  1137. struct callback_head on_exit_work;
  1138. struct irqaction *action = data;
  1139. struct irq_desc *desc = irq_to_desc(action->irq);
  1140. irqreturn_t (*handler_fn)(struct irq_desc *desc,
  1141. struct irqaction *action);
  1142. irq_thread_set_ready(desc, action);
  1143. sched_set_fifo(current);
  1144. if (force_irqthreads() && test_bit(IRQTF_FORCED_THREAD,
  1145. &action->thread_flags))
  1146. handler_fn = irq_forced_thread_fn;
  1147. else
  1148. handler_fn = irq_thread_fn;
  1149. init_task_work(&on_exit_work, irq_thread_dtor);
  1150. task_work_add(current, &on_exit_work, TWA_NONE);
  1151. while (!irq_wait_for_interrupt(desc, action)) {
  1152. irqreturn_t action_ret;
  1153. action_ret = handler_fn(desc, action);
  1154. if (action_ret == IRQ_WAKE_THREAD)
  1155. irq_wake_secondary(desc, action);
  1156. wake_threads_waitq(desc);
  1157. }
  1158. /*
  1159. * This is the regular exit path. __free_irq() is stopping the
  1160. * thread via kthread_stop() after calling
  1161. * synchronize_hardirq(). So neither IRQTF_RUNTHREAD nor the
  1162. * oneshot mask bit can be set.
  1163. */
  1164. task_work_cancel_func(current, irq_thread_dtor);
  1165. return 0;
  1166. }
  1167. /**
  1168. * irq_wake_thread - wake the irq thread for the action identified by dev_id
  1169. * @irq: Interrupt line
  1170. * @dev_id: Device identity for which the thread should be woken
  1171. *
  1172. */
  1173. void irq_wake_thread(unsigned int irq, void *dev_id)
  1174. {
  1175. struct irq_desc *desc = irq_to_desc(irq);
  1176. struct irqaction *action;
  1177. unsigned long flags;
  1178. if (!desc || WARN_ON(irq_settings_is_per_cpu_devid(desc)))
  1179. return;
  1180. raw_spin_lock_irqsave(&desc->lock, flags);
  1181. for_each_action_of_desc(desc, action) {
  1182. if (action->dev_id == dev_id) {
  1183. if (action->thread)
  1184. __irq_wake_thread(desc, action);
  1185. break;
  1186. }
  1187. }
  1188. raw_spin_unlock_irqrestore(&desc->lock, flags);
  1189. }
  1190. EXPORT_SYMBOL_GPL(irq_wake_thread);
  1191. static int irq_setup_forced_threading(struct irqaction *new)
  1192. {
  1193. if (!force_irqthreads())
  1194. return 0;
  1195. if (new->flags & (IRQF_NO_THREAD | IRQF_PERCPU | IRQF_ONESHOT))
  1196. return 0;
  1197. /*
  1198. * No further action required for interrupts which are requested as
  1199. * threaded interrupts already
  1200. */
  1201. if (new->handler == irq_default_primary_handler)
  1202. return 0;
  1203. new->flags |= IRQF_ONESHOT;
  1204. /*
  1205. * Handle the case where we have a real primary handler and a
  1206. * thread handler. We force thread them as well by creating a
  1207. * secondary action.
  1208. */
  1209. if (new->handler && new->thread_fn) {
  1210. /* Allocate the secondary action */
  1211. new->secondary = kzalloc(sizeof(struct irqaction), GFP_KERNEL);
  1212. if (!new->secondary)
  1213. return -ENOMEM;
  1214. new->secondary->handler = irq_forced_secondary_handler;
  1215. new->secondary->thread_fn = new->thread_fn;
  1216. new->secondary->dev_id = new->dev_id;
  1217. new->secondary->irq = new->irq;
  1218. new->secondary->name = new->name;
  1219. }
  1220. /* Deal with the primary handler */
  1221. set_bit(IRQTF_FORCED_THREAD, &new->thread_flags);
  1222. new->thread_fn = new->handler;
  1223. new->handler = irq_default_primary_handler;
  1224. return 0;
  1225. }
  1226. static int irq_request_resources(struct irq_desc *desc)
  1227. {
  1228. struct irq_data *d = &desc->irq_data;
  1229. struct irq_chip *c = d->chip;
  1230. return c->irq_request_resources ? c->irq_request_resources(d) : 0;
  1231. }
  1232. static void irq_release_resources(struct irq_desc *desc)
  1233. {
  1234. struct irq_data *d = &desc->irq_data;
  1235. struct irq_chip *c = d->chip;
  1236. if (c->irq_release_resources)
  1237. c->irq_release_resources(d);
  1238. }
  1239. static bool irq_supports_nmi(struct irq_desc *desc)
  1240. {
  1241. struct irq_data *d = irq_desc_get_irq_data(desc);
  1242. #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
  1243. /* Only IRQs directly managed by the root irqchip can be set as NMI */
  1244. if (d->parent_data)
  1245. return false;
  1246. #endif
  1247. /* Don't support NMIs for chips behind a slow bus */
  1248. if (d->chip->irq_bus_lock || d->chip->irq_bus_sync_unlock)
  1249. return false;
  1250. return d->chip->flags & IRQCHIP_SUPPORTS_NMI;
  1251. }
  1252. static int irq_nmi_setup(struct irq_desc *desc)
  1253. {
  1254. struct irq_data *d = irq_desc_get_irq_data(desc);
  1255. struct irq_chip *c = d->chip;
  1256. return c->irq_nmi_setup ? c->irq_nmi_setup(d) : -EINVAL;
  1257. }
  1258. static void irq_nmi_teardown(struct irq_desc *desc)
  1259. {
  1260. struct irq_data *d = irq_desc_get_irq_data(desc);
  1261. struct irq_chip *c = d->chip;
  1262. if (c->irq_nmi_teardown)
  1263. c->irq_nmi_teardown(d);
  1264. }
  1265. static int
  1266. setup_irq_thread(struct irqaction *new, unsigned int irq, bool secondary)
  1267. {
  1268. struct task_struct *t;
  1269. if (!secondary) {
  1270. t = kthread_create(irq_thread, new, "irq/%d-%s", irq,
  1271. new->name);
  1272. } else {
  1273. t = kthread_create(irq_thread, new, "irq/%d-s-%s", irq,
  1274. new->name);
  1275. }
  1276. if (IS_ERR(t))
  1277. return PTR_ERR(t);
  1278. /*
  1279. * We keep the reference to the task struct even if
  1280. * the thread dies to avoid that the interrupt code
  1281. * references an already freed task_struct.
  1282. */
  1283. new->thread = get_task_struct(t);
  1284. /*
  1285. * Tell the thread to set its affinity. This is
  1286. * important for shared interrupt handlers as we do
  1287. * not invoke setup_affinity() for the secondary
  1288. * handlers as everything is already set up. Even for
  1289. * interrupts marked with IRQF_NO_BALANCE this is
  1290. * correct as we want the thread to move to the cpu(s)
  1291. * on which the requesting code placed the interrupt.
  1292. */
  1293. set_bit(IRQTF_AFFINITY, &new->thread_flags);
  1294. return 0;
  1295. }
  1296. /*
  1297. * Internal function to register an irqaction - typically used to
  1298. * allocate special interrupts that are part of the architecture.
  1299. *
  1300. * Locking rules:
  1301. *
  1302. * desc->request_mutex Provides serialization against a concurrent free_irq()
  1303. * chip_bus_lock Provides serialization for slow bus operations
  1304. * desc->lock Provides serialization against hard interrupts
  1305. *
  1306. * chip_bus_lock and desc->lock are sufficient for all other management and
  1307. * interrupt related functions. desc->request_mutex solely serializes
  1308. * request/free_irq().
  1309. */
  1310. static int
  1311. __setup_irq(unsigned int irq, struct irq_desc *desc, struct irqaction *new)
  1312. {
  1313. struct irqaction *old, **old_ptr;
  1314. unsigned long flags, thread_mask = 0;
  1315. int ret, nested, shared = 0;
  1316. if (!desc)
  1317. return -EINVAL;
  1318. if (desc->irq_data.chip == &no_irq_chip)
  1319. return -ENOSYS;
  1320. if (!try_module_get(desc->owner))
  1321. return -ENODEV;
  1322. new->irq = irq;
  1323. /*
  1324. * If the trigger type is not specified by the caller,
  1325. * then use the default for this interrupt.
  1326. */
  1327. if (!(new->flags & IRQF_TRIGGER_MASK))
  1328. new->flags |= irqd_get_trigger_type(&desc->irq_data);
  1329. /*
  1330. * Check whether the interrupt nests into another interrupt
  1331. * thread.
  1332. */
  1333. nested = irq_settings_is_nested_thread(desc);
  1334. if (nested) {
  1335. if (!new->thread_fn) {
  1336. ret = -EINVAL;
  1337. goto out_mput;
  1338. }
  1339. /*
  1340. * Replace the primary handler which was provided from
  1341. * the driver for non nested interrupt handling by the
  1342. * dummy function which warns when called.
  1343. */
  1344. new->handler = irq_nested_primary_handler;
  1345. } else {
  1346. if (irq_settings_can_thread(desc)) {
  1347. ret = irq_setup_forced_threading(new);
  1348. if (ret)
  1349. goto out_mput;
  1350. }
  1351. }
  1352. /*
  1353. * Create a handler thread when a thread function is supplied
  1354. * and the interrupt does not nest into another interrupt
  1355. * thread.
  1356. */
  1357. if (new->thread_fn && !nested) {
  1358. ret = setup_irq_thread(new, irq, false);
  1359. if (ret)
  1360. goto out_mput;
  1361. if (new->secondary) {
  1362. ret = setup_irq_thread(new->secondary, irq, true);
  1363. if (ret)
  1364. goto out_thread;
  1365. }
  1366. }
  1367. /*
  1368. * Drivers are often written to work w/o knowledge about the
  1369. * underlying irq chip implementation, so a request for a
  1370. * threaded irq without a primary hard irq context handler
  1371. * requires the ONESHOT flag to be set. Some irq chips like
  1372. * MSI based interrupts are per se one shot safe. Check the
  1373. * chip flags, so we can avoid the unmask dance at the end of
  1374. * the threaded handler for those.
  1375. */
  1376. if (desc->irq_data.chip->flags & IRQCHIP_ONESHOT_SAFE)
  1377. new->flags &= ~IRQF_ONESHOT;
  1378. /*
  1379. * Protects against a concurrent __free_irq() call which might wait
  1380. * for synchronize_hardirq() to complete without holding the optional
  1381. * chip bus lock and desc->lock. Also protects against handing out
  1382. * a recycled oneshot thread_mask bit while it's still in use by
  1383. * its previous owner.
  1384. */
  1385. mutex_lock(&desc->request_mutex);
  1386. /*
  1387. * Acquire bus lock as the irq_request_resources() callback below
  1388. * might rely on the serialization or the magic power management
  1389. * functions which are abusing the irq_bus_lock() callback,
  1390. */
  1391. chip_bus_lock(desc);
  1392. /* First installed action requests resources. */
  1393. if (!desc->action) {
  1394. ret = irq_request_resources(desc);
  1395. if (ret) {
  1396. pr_err("Failed to request resources for %s (irq %d) on irqchip %s\n",
  1397. new->name, irq, desc->irq_data.chip->name);
  1398. goto out_bus_unlock;
  1399. }
  1400. }
  1401. /*
  1402. * The following block of code has to be executed atomically
  1403. * protected against a concurrent interrupt and any of the other
  1404. * management calls which are not serialized via
  1405. * desc->request_mutex or the optional bus lock.
  1406. */
  1407. raw_spin_lock_irqsave(&desc->lock, flags);
  1408. old_ptr = &desc->action;
  1409. old = *old_ptr;
  1410. if (old) {
  1411. /*
  1412. * Can't share interrupts unless both agree to and are
  1413. * the same type (level, edge, polarity). So both flag
  1414. * fields must have IRQF_SHARED set and the bits which
  1415. * set the trigger type must match. Also all must
  1416. * agree on ONESHOT.
  1417. * Interrupt lines used for NMIs cannot be shared.
  1418. */
  1419. unsigned int oldtype;
  1420. if (irq_is_nmi(desc)) {
  1421. pr_err("Invalid attempt to share NMI for %s (irq %d) on irqchip %s.\n",
  1422. new->name, irq, desc->irq_data.chip->name);
  1423. ret = -EINVAL;
  1424. goto out_unlock;
  1425. }
  1426. /*
  1427. * If nobody did set the configuration before, inherit
  1428. * the one provided by the requester.
  1429. */
  1430. if (irqd_trigger_type_was_set(&desc->irq_data)) {
  1431. oldtype = irqd_get_trigger_type(&desc->irq_data);
  1432. } else {
  1433. oldtype = new->flags & IRQF_TRIGGER_MASK;
  1434. irqd_set_trigger_type(&desc->irq_data, oldtype);
  1435. }
  1436. if (!((old->flags & new->flags) & IRQF_SHARED) ||
  1437. (oldtype != (new->flags & IRQF_TRIGGER_MASK)))
  1438. goto mismatch;
  1439. if ((old->flags & IRQF_ONESHOT) &&
  1440. (new->flags & IRQF_COND_ONESHOT))
  1441. new->flags |= IRQF_ONESHOT;
  1442. else if ((old->flags ^ new->flags) & IRQF_ONESHOT)
  1443. goto mismatch;
  1444. /* All handlers must agree on per-cpuness */
  1445. if ((old->flags & IRQF_PERCPU) !=
  1446. (new->flags & IRQF_PERCPU))
  1447. goto mismatch;
  1448. /* add new interrupt at end of irq queue */
  1449. do {
  1450. /*
  1451. * Or all existing action->thread_mask bits,
  1452. * so we can find the next zero bit for this
  1453. * new action.
  1454. */
  1455. thread_mask |= old->thread_mask;
  1456. old_ptr = &old->next;
  1457. old = *old_ptr;
  1458. } while (old);
  1459. shared = 1;
  1460. }
  1461. /*
  1462. * Setup the thread mask for this irqaction for ONESHOT. For
  1463. * !ONESHOT irqs the thread mask is 0 so we can avoid a
  1464. * conditional in irq_wake_thread().
  1465. */
  1466. if (new->flags & IRQF_ONESHOT) {
  1467. /*
  1468. * Unlikely to have 32 resp 64 irqs sharing one line,
  1469. * but who knows.
  1470. */
  1471. if (thread_mask == ~0UL) {
  1472. ret = -EBUSY;
  1473. goto out_unlock;
  1474. }
  1475. /*
  1476. * The thread_mask for the action is or'ed to
  1477. * desc->thread_active to indicate that the
  1478. * IRQF_ONESHOT thread handler has been woken, but not
  1479. * yet finished. The bit is cleared when a thread
  1480. * completes. When all threads of a shared interrupt
  1481. * line have completed desc->threads_active becomes
  1482. * zero and the interrupt line is unmasked. See
  1483. * handle.c:irq_wake_thread() for further information.
  1484. *
  1485. * If no thread is woken by primary (hard irq context)
  1486. * interrupt handlers, then desc->threads_active is
  1487. * also checked for zero to unmask the irq line in the
  1488. * affected hard irq flow handlers
  1489. * (handle_[fasteoi|level]_irq).
  1490. *
  1491. * The new action gets the first zero bit of
  1492. * thread_mask assigned. See the loop above which or's
  1493. * all existing action->thread_mask bits.
  1494. */
  1495. new->thread_mask = 1UL << ffz(thread_mask);
  1496. } else if (new->handler == irq_default_primary_handler &&
  1497. !(desc->irq_data.chip->flags & IRQCHIP_ONESHOT_SAFE)) {
  1498. /*
  1499. * The interrupt was requested with handler = NULL, so
  1500. * we use the default primary handler for it. But it
  1501. * does not have the oneshot flag set. In combination
  1502. * with level interrupts this is deadly, because the
  1503. * default primary handler just wakes the thread, then
  1504. * the irq lines is reenabled, but the device still
  1505. * has the level irq asserted. Rinse and repeat....
  1506. *
  1507. * While this works for edge type interrupts, we play
  1508. * it safe and reject unconditionally because we can't
  1509. * say for sure which type this interrupt really
  1510. * has. The type flags are unreliable as the
  1511. * underlying chip implementation can override them.
  1512. */
  1513. pr_err("Threaded irq requested with handler=NULL and !ONESHOT for %s (irq %d)\n",
  1514. new->name, irq);
  1515. ret = -EINVAL;
  1516. goto out_unlock;
  1517. }
  1518. if (!shared) {
  1519. /* Setup the type (level, edge polarity) if configured: */
  1520. if (new->flags & IRQF_TRIGGER_MASK) {
  1521. ret = __irq_set_trigger(desc,
  1522. new->flags & IRQF_TRIGGER_MASK);
  1523. if (ret)
  1524. goto out_unlock;
  1525. }
  1526. /*
  1527. * Activate the interrupt. That activation must happen
  1528. * independently of IRQ_NOAUTOEN. request_irq() can fail
  1529. * and the callers are supposed to handle
  1530. * that. enable_irq() of an interrupt requested with
  1531. * IRQ_NOAUTOEN is not supposed to fail. The activation
  1532. * keeps it in shutdown mode, it merily associates
  1533. * resources if necessary and if that's not possible it
  1534. * fails. Interrupts which are in managed shutdown mode
  1535. * will simply ignore that activation request.
  1536. */
  1537. ret = irq_activate(desc);
  1538. if (ret)
  1539. goto out_unlock;
  1540. desc->istate &= ~(IRQS_AUTODETECT | IRQS_SPURIOUS_DISABLED | \
  1541. IRQS_ONESHOT | IRQS_WAITING);
  1542. irqd_clear(&desc->irq_data, IRQD_IRQ_INPROGRESS);
  1543. if (new->flags & IRQF_PERCPU) {
  1544. irqd_set(&desc->irq_data, IRQD_PER_CPU);
  1545. irq_settings_set_per_cpu(desc);
  1546. if (new->flags & IRQF_NO_DEBUG)
  1547. irq_settings_set_no_debug(desc);
  1548. }
  1549. if (noirqdebug)
  1550. irq_settings_set_no_debug(desc);
  1551. if (new->flags & IRQF_ONESHOT)
  1552. desc->istate |= IRQS_ONESHOT;
  1553. /* Exclude IRQ from balancing if requested */
  1554. if (new->flags & IRQF_NOBALANCING) {
  1555. irq_settings_set_no_balancing(desc);
  1556. irqd_set(&desc->irq_data, IRQD_NO_BALANCING);
  1557. }
  1558. if (!(new->flags & IRQF_NO_AUTOEN) &&
  1559. irq_settings_can_autoenable(desc)) {
  1560. irq_startup(desc, IRQ_RESEND, IRQ_START_COND);
  1561. } else {
  1562. /*
  1563. * Shared interrupts do not go well with disabling
  1564. * auto enable. The sharing interrupt might request
  1565. * it while it's still disabled and then wait for
  1566. * interrupts forever.
  1567. */
  1568. WARN_ON_ONCE(new->flags & IRQF_SHARED);
  1569. /* Undo nested disables: */
  1570. desc->depth = 1;
  1571. }
  1572. } else if (new->flags & IRQF_TRIGGER_MASK) {
  1573. unsigned int nmsk = new->flags & IRQF_TRIGGER_MASK;
  1574. unsigned int omsk = irqd_get_trigger_type(&desc->irq_data);
  1575. if (nmsk != omsk)
  1576. /* hope the handler works with current trigger mode */
  1577. pr_warn("irq %d uses trigger mode %u; requested %u\n",
  1578. irq, omsk, nmsk);
  1579. }
  1580. *old_ptr = new;
  1581. irq_pm_install_action(desc, new);
  1582. /* Reset broken irq detection when installing new handler */
  1583. desc->irq_count = 0;
  1584. desc->irqs_unhandled = 0;
  1585. /*
  1586. * Check whether we disabled the irq via the spurious handler
  1587. * before. Reenable it and give it another chance.
  1588. */
  1589. if (shared && (desc->istate & IRQS_SPURIOUS_DISABLED)) {
  1590. desc->istate &= ~IRQS_SPURIOUS_DISABLED;
  1591. __enable_irq(desc);
  1592. }
  1593. raw_spin_unlock_irqrestore(&desc->lock, flags);
  1594. chip_bus_sync_unlock(desc);
  1595. mutex_unlock(&desc->request_mutex);
  1596. irq_setup_timings(desc, new);
  1597. wake_up_and_wait_for_irq_thread_ready(desc, new);
  1598. wake_up_and_wait_for_irq_thread_ready(desc, new->secondary);
  1599. register_irq_proc(irq, desc);
  1600. new->dir = NULL;
  1601. register_handler_proc(irq, new);
  1602. return 0;
  1603. mismatch:
  1604. if (!(new->flags & IRQF_PROBE_SHARED)) {
  1605. pr_err("Flags mismatch irq %d. %08x (%s) vs. %08x (%s)\n",
  1606. irq, new->flags, new->name, old->flags, old->name);
  1607. #ifdef CONFIG_DEBUG_SHIRQ
  1608. dump_stack();
  1609. #endif
  1610. }
  1611. ret = -EBUSY;
  1612. out_unlock:
  1613. raw_spin_unlock_irqrestore(&desc->lock, flags);
  1614. if (!desc->action)
  1615. irq_release_resources(desc);
  1616. out_bus_unlock:
  1617. chip_bus_sync_unlock(desc);
  1618. mutex_unlock(&desc->request_mutex);
  1619. out_thread:
  1620. if (new->thread) {
  1621. struct task_struct *t = new->thread;
  1622. new->thread = NULL;
  1623. kthread_stop_put(t);
  1624. }
  1625. if (new->secondary && new->secondary->thread) {
  1626. struct task_struct *t = new->secondary->thread;
  1627. new->secondary->thread = NULL;
  1628. kthread_stop_put(t);
  1629. }
  1630. out_mput:
  1631. module_put(desc->owner);
  1632. return ret;
  1633. }
  1634. /*
  1635. * Internal function to unregister an irqaction - used to free
  1636. * regular and special interrupts that are part of the architecture.
  1637. */
  1638. static struct irqaction *__free_irq(struct irq_desc *desc, void *dev_id)
  1639. {
  1640. unsigned irq = desc->irq_data.irq;
  1641. struct irqaction *action, **action_ptr;
  1642. unsigned long flags;
  1643. WARN(in_interrupt(), "Trying to free IRQ %d from IRQ context!\n", irq);
  1644. mutex_lock(&desc->request_mutex);
  1645. chip_bus_lock(desc);
  1646. raw_spin_lock_irqsave(&desc->lock, flags);
  1647. /*
  1648. * There can be multiple actions per IRQ descriptor, find the right
  1649. * one based on the dev_id:
  1650. */
  1651. action_ptr = &desc->action;
  1652. for (;;) {
  1653. action = *action_ptr;
  1654. if (!action) {
  1655. WARN(1, "Trying to free already-free IRQ %d\n", irq);
  1656. raw_spin_unlock_irqrestore(&desc->lock, flags);
  1657. chip_bus_sync_unlock(desc);
  1658. mutex_unlock(&desc->request_mutex);
  1659. return NULL;
  1660. }
  1661. if (action->dev_id == dev_id)
  1662. break;
  1663. action_ptr = &action->next;
  1664. }
  1665. /* Found it - now remove it from the list of entries: */
  1666. *action_ptr = action->next;
  1667. irq_pm_remove_action(desc, action);
  1668. /* If this was the last handler, shut down the IRQ line: */
  1669. if (!desc->action) {
  1670. irq_settings_clr_disable_unlazy(desc);
  1671. /* Only shutdown. Deactivate after synchronize_hardirq() */
  1672. irq_shutdown(desc);
  1673. }
  1674. #ifdef CONFIG_SMP
  1675. /* make sure affinity_hint is cleaned up */
  1676. if (WARN_ON_ONCE(desc->affinity_hint))
  1677. desc->affinity_hint = NULL;
  1678. #endif
  1679. raw_spin_unlock_irqrestore(&desc->lock, flags);
  1680. /*
  1681. * Drop bus_lock here so the changes which were done in the chip
  1682. * callbacks above are synced out to the irq chips which hang
  1683. * behind a slow bus (I2C, SPI) before calling synchronize_hardirq().
  1684. *
  1685. * Aside of that the bus_lock can also be taken from the threaded
  1686. * handler in irq_finalize_oneshot() which results in a deadlock
  1687. * because kthread_stop() would wait forever for the thread to
  1688. * complete, which is blocked on the bus lock.
  1689. *
  1690. * The still held desc->request_mutex() protects against a
  1691. * concurrent request_irq() of this irq so the release of resources
  1692. * and timing data is properly serialized.
  1693. */
  1694. chip_bus_sync_unlock(desc);
  1695. unregister_handler_proc(irq, action);
  1696. /*
  1697. * Make sure it's not being used on another CPU and if the chip
  1698. * supports it also make sure that there is no (not yet serviced)
  1699. * interrupt in flight at the hardware level.
  1700. */
  1701. __synchronize_irq(desc);
  1702. #ifdef CONFIG_DEBUG_SHIRQ
  1703. /*
  1704. * It's a shared IRQ -- the driver ought to be prepared for an IRQ
  1705. * event to happen even now it's being freed, so let's make sure that
  1706. * is so by doing an extra call to the handler ....
  1707. *
  1708. * ( We do this after actually deregistering it, to make sure that a
  1709. * 'real' IRQ doesn't run in parallel with our fake. )
  1710. */
  1711. if (action->flags & IRQF_SHARED) {
  1712. local_irq_save(flags);
  1713. action->handler(irq, dev_id);
  1714. local_irq_restore(flags);
  1715. }
  1716. #endif
  1717. /*
  1718. * The action has already been removed above, but the thread writes
  1719. * its oneshot mask bit when it completes. Though request_mutex is
  1720. * held across this which prevents __setup_irq() from handing out
  1721. * the same bit to a newly requested action.
  1722. */
  1723. if (action->thread) {
  1724. kthread_stop_put(action->thread);
  1725. if (action->secondary && action->secondary->thread)
  1726. kthread_stop_put(action->secondary->thread);
  1727. }
  1728. /* Last action releases resources */
  1729. if (!desc->action) {
  1730. /*
  1731. * Reacquire bus lock as irq_release_resources() might
  1732. * require it to deallocate resources over the slow bus.
  1733. */
  1734. chip_bus_lock(desc);
  1735. /*
  1736. * There is no interrupt on the fly anymore. Deactivate it
  1737. * completely.
  1738. */
  1739. raw_spin_lock_irqsave(&desc->lock, flags);
  1740. irq_domain_deactivate_irq(&desc->irq_data);
  1741. raw_spin_unlock_irqrestore(&desc->lock, flags);
  1742. irq_release_resources(desc);
  1743. chip_bus_sync_unlock(desc);
  1744. irq_remove_timings(desc);
  1745. }
  1746. mutex_unlock(&desc->request_mutex);
  1747. irq_chip_pm_put(&desc->irq_data);
  1748. module_put(desc->owner);
  1749. kfree(action->secondary);
  1750. return action;
  1751. }
  1752. /**
  1753. * free_irq - free an interrupt allocated with request_irq
  1754. * @irq: Interrupt line to free
  1755. * @dev_id: Device identity to free
  1756. *
  1757. * Remove an interrupt handler. The handler is removed and if the
  1758. * interrupt line is no longer in use by any driver it is disabled.
  1759. * On a shared IRQ the caller must ensure the interrupt is disabled
  1760. * on the card it drives before calling this function. The function
  1761. * does not return until any executing interrupts for this IRQ
  1762. * have completed.
  1763. *
  1764. * This function must not be called from interrupt context.
  1765. *
  1766. * Returns the devname argument passed to request_irq.
  1767. */
  1768. const void *free_irq(unsigned int irq, void *dev_id)
  1769. {
  1770. struct irq_desc *desc = irq_to_desc(irq);
  1771. struct irqaction *action;
  1772. const char *devname;
  1773. if (!desc || WARN_ON(irq_settings_is_per_cpu_devid(desc)))
  1774. return NULL;
  1775. #ifdef CONFIG_SMP
  1776. if (WARN_ON(desc->affinity_notify))
  1777. desc->affinity_notify = NULL;
  1778. #endif
  1779. action = __free_irq(desc, dev_id);
  1780. if (!action)
  1781. return NULL;
  1782. devname = action->name;
  1783. kfree(action);
  1784. return devname;
  1785. }
  1786. EXPORT_SYMBOL(free_irq);
  1787. /* This function must be called with desc->lock held */
  1788. static const void *__cleanup_nmi(unsigned int irq, struct irq_desc *desc)
  1789. {
  1790. const char *devname = NULL;
  1791. desc->istate &= ~IRQS_NMI;
  1792. if (!WARN_ON(desc->action == NULL)) {
  1793. irq_pm_remove_action(desc, desc->action);
  1794. devname = desc->action->name;
  1795. unregister_handler_proc(irq, desc->action);
  1796. kfree(desc->action);
  1797. desc->action = NULL;
  1798. }
  1799. irq_settings_clr_disable_unlazy(desc);
  1800. irq_shutdown_and_deactivate(desc);
  1801. irq_release_resources(desc);
  1802. irq_chip_pm_put(&desc->irq_data);
  1803. module_put(desc->owner);
  1804. return devname;
  1805. }
  1806. const void *free_nmi(unsigned int irq, void *dev_id)
  1807. {
  1808. struct irq_desc *desc = irq_to_desc(irq);
  1809. unsigned long flags;
  1810. const void *devname;
  1811. if (!desc || WARN_ON(!irq_is_nmi(desc)))
  1812. return NULL;
  1813. if (WARN_ON(irq_settings_is_per_cpu_devid(desc)))
  1814. return NULL;
  1815. /* NMI still enabled */
  1816. if (WARN_ON(desc->depth == 0))
  1817. disable_nmi_nosync(irq);
  1818. raw_spin_lock_irqsave(&desc->lock, flags);
  1819. irq_nmi_teardown(desc);
  1820. devname = __cleanup_nmi(irq, desc);
  1821. raw_spin_unlock_irqrestore(&desc->lock, flags);
  1822. return devname;
  1823. }
  1824. /**
  1825. * request_threaded_irq - allocate an interrupt line
  1826. * @irq: Interrupt line to allocate
  1827. * @handler: Function to be called when the IRQ occurs.
  1828. * Primary handler for threaded interrupts.
  1829. * If handler is NULL and thread_fn != NULL
  1830. * the default primary handler is installed.
  1831. * @thread_fn: Function called from the irq handler thread
  1832. * If NULL, no irq thread is created
  1833. * @irqflags: Interrupt type flags
  1834. * @devname: An ascii name for the claiming device
  1835. * @dev_id: A cookie passed back to the handler function
  1836. *
  1837. * This call allocates interrupt resources and enables the
  1838. * interrupt line and IRQ handling. From the point this
  1839. * call is made your handler function may be invoked. Since
  1840. * your handler function must clear any interrupt the board
  1841. * raises, you must take care both to initialise your hardware
  1842. * and to set up the interrupt handler in the right order.
  1843. *
  1844. * If you want to set up a threaded irq handler for your device
  1845. * then you need to supply @handler and @thread_fn. @handler is
  1846. * still called in hard interrupt context and has to check
  1847. * whether the interrupt originates from the device. If yes it
  1848. * needs to disable the interrupt on the device and return
  1849. * IRQ_WAKE_THREAD which will wake up the handler thread and run
  1850. * @thread_fn. This split handler design is necessary to support
  1851. * shared interrupts.
  1852. *
  1853. * Dev_id must be globally unique. Normally the address of the
  1854. * device data structure is used as the cookie. Since the handler
  1855. * receives this value it makes sense to use it.
  1856. *
  1857. * If your interrupt is shared you must pass a non NULL dev_id
  1858. * as this is required when freeing the interrupt.
  1859. *
  1860. * Flags:
  1861. *
  1862. * IRQF_SHARED Interrupt is shared
  1863. * IRQF_TRIGGER_* Specify active edge(s) or level
  1864. * IRQF_ONESHOT Run thread_fn with interrupt line masked
  1865. */
  1866. int request_threaded_irq(unsigned int irq, irq_handler_t handler,
  1867. irq_handler_t thread_fn, unsigned long irqflags,
  1868. const char *devname, void *dev_id)
  1869. {
  1870. struct irqaction *action;
  1871. struct irq_desc *desc;
  1872. int retval;
  1873. if (irq == IRQ_NOTCONNECTED)
  1874. return -ENOTCONN;
  1875. /*
  1876. * Sanity-check: shared interrupts must pass in a real dev-ID,
  1877. * otherwise we'll have trouble later trying to figure out
  1878. * which interrupt is which (messes up the interrupt freeing
  1879. * logic etc).
  1880. *
  1881. * Also shared interrupts do not go well with disabling auto enable.
  1882. * The sharing interrupt might request it while it's still disabled
  1883. * and then wait for interrupts forever.
  1884. *
  1885. * Also IRQF_COND_SUSPEND only makes sense for shared interrupts and
  1886. * it cannot be set along with IRQF_NO_SUSPEND.
  1887. */
  1888. if (((irqflags & IRQF_SHARED) && !dev_id) ||
  1889. ((irqflags & IRQF_SHARED) && (irqflags & IRQF_NO_AUTOEN)) ||
  1890. (!(irqflags & IRQF_SHARED) && (irqflags & IRQF_COND_SUSPEND)) ||
  1891. ((irqflags & IRQF_NO_SUSPEND) && (irqflags & IRQF_COND_SUSPEND)))
  1892. return -EINVAL;
  1893. desc = irq_to_desc(irq);
  1894. if (!desc)
  1895. return -EINVAL;
  1896. if (!irq_settings_can_request(desc) ||
  1897. WARN_ON(irq_settings_is_per_cpu_devid(desc)))
  1898. return -EINVAL;
  1899. if (!handler) {
  1900. if (!thread_fn)
  1901. return -EINVAL;
  1902. handler = irq_default_primary_handler;
  1903. }
  1904. action = kzalloc(sizeof(struct irqaction), GFP_KERNEL);
  1905. if (!action)
  1906. return -ENOMEM;
  1907. action->handler = handler;
  1908. action->thread_fn = thread_fn;
  1909. action->flags = irqflags;
  1910. action->name = devname;
  1911. action->dev_id = dev_id;
  1912. retval = irq_chip_pm_get(&desc->irq_data);
  1913. if (retval < 0) {
  1914. kfree(action);
  1915. return retval;
  1916. }
  1917. retval = __setup_irq(irq, desc, action);
  1918. if (retval) {
  1919. irq_chip_pm_put(&desc->irq_data);
  1920. kfree(action->secondary);
  1921. kfree(action);
  1922. }
  1923. #ifdef CONFIG_DEBUG_SHIRQ_FIXME
  1924. if (!retval && (irqflags & IRQF_SHARED)) {
  1925. /*
  1926. * It's a shared IRQ -- the driver ought to be prepared for it
  1927. * to happen immediately, so let's make sure....
  1928. * We disable the irq to make sure that a 'real' IRQ doesn't
  1929. * run in parallel with our fake.
  1930. */
  1931. unsigned long flags;
  1932. disable_irq(irq);
  1933. local_irq_save(flags);
  1934. handler(irq, dev_id);
  1935. local_irq_restore(flags);
  1936. enable_irq(irq);
  1937. }
  1938. #endif
  1939. return retval;
  1940. }
  1941. EXPORT_SYMBOL(request_threaded_irq);
  1942. /**
  1943. * request_any_context_irq - allocate an interrupt line
  1944. * @irq: Interrupt line to allocate
  1945. * @handler: Function to be called when the IRQ occurs.
  1946. * Threaded handler for threaded interrupts.
  1947. * @flags: Interrupt type flags
  1948. * @name: An ascii name for the claiming device
  1949. * @dev_id: A cookie passed back to the handler function
  1950. *
  1951. * This call allocates interrupt resources and enables the
  1952. * interrupt line and IRQ handling. It selects either a
  1953. * hardirq or threaded handling method depending on the
  1954. * context.
  1955. *
  1956. * On failure, it returns a negative value. On success,
  1957. * it returns either IRQC_IS_HARDIRQ or IRQC_IS_NESTED.
  1958. */
  1959. int request_any_context_irq(unsigned int irq, irq_handler_t handler,
  1960. unsigned long flags, const char *name, void *dev_id)
  1961. {
  1962. struct irq_desc *desc;
  1963. int ret;
  1964. if (irq == IRQ_NOTCONNECTED)
  1965. return -ENOTCONN;
  1966. desc = irq_to_desc(irq);
  1967. if (!desc)
  1968. return -EINVAL;
  1969. if (irq_settings_is_nested_thread(desc)) {
  1970. ret = request_threaded_irq(irq, NULL, handler,
  1971. flags, name, dev_id);
  1972. return !ret ? IRQC_IS_NESTED : ret;
  1973. }
  1974. ret = request_irq(irq, handler, flags, name, dev_id);
  1975. return !ret ? IRQC_IS_HARDIRQ : ret;
  1976. }
  1977. EXPORT_SYMBOL_GPL(request_any_context_irq);
  1978. /**
  1979. * request_nmi - allocate an interrupt line for NMI delivery
  1980. * @irq: Interrupt line to allocate
  1981. * @handler: Function to be called when the IRQ occurs.
  1982. * Threaded handler for threaded interrupts.
  1983. * @irqflags: Interrupt type flags
  1984. * @name: An ascii name for the claiming device
  1985. * @dev_id: A cookie passed back to the handler function
  1986. *
  1987. * This call allocates interrupt resources and enables the
  1988. * interrupt line and IRQ handling. It sets up the IRQ line
  1989. * to be handled as an NMI.
  1990. *
  1991. * An interrupt line delivering NMIs cannot be shared and IRQ handling
  1992. * cannot be threaded.
  1993. *
  1994. * Interrupt lines requested for NMI delivering must produce per cpu
  1995. * interrupts and have auto enabling setting disabled.
  1996. *
  1997. * Dev_id must be globally unique. Normally the address of the
  1998. * device data structure is used as the cookie. Since the handler
  1999. * receives this value it makes sense to use it.
  2000. *
  2001. * If the interrupt line cannot be used to deliver NMIs, function
  2002. * will fail and return a negative value.
  2003. */
  2004. int request_nmi(unsigned int irq, irq_handler_t handler,
  2005. unsigned long irqflags, const char *name, void *dev_id)
  2006. {
  2007. struct irqaction *action;
  2008. struct irq_desc *desc;
  2009. unsigned long flags;
  2010. int retval;
  2011. if (irq == IRQ_NOTCONNECTED)
  2012. return -ENOTCONN;
  2013. /* NMI cannot be shared, used for Polling */
  2014. if (irqflags & (IRQF_SHARED | IRQF_COND_SUSPEND | IRQF_IRQPOLL))
  2015. return -EINVAL;
  2016. if (!(irqflags & IRQF_PERCPU))
  2017. return -EINVAL;
  2018. if (!handler)
  2019. return -EINVAL;
  2020. desc = irq_to_desc(irq);
  2021. if (!desc || (irq_settings_can_autoenable(desc) &&
  2022. !(irqflags & IRQF_NO_AUTOEN)) ||
  2023. !irq_settings_can_request(desc) ||
  2024. WARN_ON(irq_settings_is_per_cpu_devid(desc)) ||
  2025. !irq_supports_nmi(desc))
  2026. return -EINVAL;
  2027. action = kzalloc(sizeof(struct irqaction), GFP_KERNEL);
  2028. if (!action)
  2029. return -ENOMEM;
  2030. action->handler = handler;
  2031. action->flags = irqflags | IRQF_NO_THREAD | IRQF_NOBALANCING;
  2032. action->name = name;
  2033. action->dev_id = dev_id;
  2034. retval = irq_chip_pm_get(&desc->irq_data);
  2035. if (retval < 0)
  2036. goto err_out;
  2037. retval = __setup_irq(irq, desc, action);
  2038. if (retval)
  2039. goto err_irq_setup;
  2040. raw_spin_lock_irqsave(&desc->lock, flags);
  2041. /* Setup NMI state */
  2042. desc->istate |= IRQS_NMI;
  2043. retval = irq_nmi_setup(desc);
  2044. if (retval) {
  2045. __cleanup_nmi(irq, desc);
  2046. raw_spin_unlock_irqrestore(&desc->lock, flags);
  2047. return -EINVAL;
  2048. }
  2049. raw_spin_unlock_irqrestore(&desc->lock, flags);
  2050. return 0;
  2051. err_irq_setup:
  2052. irq_chip_pm_put(&desc->irq_data);
  2053. err_out:
  2054. kfree(action);
  2055. return retval;
  2056. }
  2057. void enable_percpu_irq(unsigned int irq, unsigned int type)
  2058. {
  2059. unsigned int cpu = smp_processor_id();
  2060. unsigned long flags;
  2061. struct irq_desc *desc = irq_get_desc_lock(irq, &flags, IRQ_GET_DESC_CHECK_PERCPU);
  2062. if (!desc)
  2063. return;
  2064. /*
  2065. * If the trigger type is not specified by the caller, then
  2066. * use the default for this interrupt.
  2067. */
  2068. type &= IRQ_TYPE_SENSE_MASK;
  2069. if (type == IRQ_TYPE_NONE)
  2070. type = irqd_get_trigger_type(&desc->irq_data);
  2071. if (type != IRQ_TYPE_NONE) {
  2072. int ret;
  2073. ret = __irq_set_trigger(desc, type);
  2074. if (ret) {
  2075. WARN(1, "failed to set type for IRQ%d\n", irq);
  2076. goto out;
  2077. }
  2078. }
  2079. irq_percpu_enable(desc, cpu);
  2080. out:
  2081. irq_put_desc_unlock(desc, flags);
  2082. }
  2083. EXPORT_SYMBOL_GPL(enable_percpu_irq);
  2084. void enable_percpu_nmi(unsigned int irq, unsigned int type)
  2085. {
  2086. enable_percpu_irq(irq, type);
  2087. }
  2088. /**
  2089. * irq_percpu_is_enabled - Check whether the per cpu irq is enabled
  2090. * @irq: Linux irq number to check for
  2091. *
  2092. * Must be called from a non migratable context. Returns the enable
  2093. * state of a per cpu interrupt on the current cpu.
  2094. */
  2095. bool irq_percpu_is_enabled(unsigned int irq)
  2096. {
  2097. unsigned int cpu = smp_processor_id();
  2098. struct irq_desc *desc;
  2099. unsigned long flags;
  2100. bool is_enabled;
  2101. desc = irq_get_desc_lock(irq, &flags, IRQ_GET_DESC_CHECK_PERCPU);
  2102. if (!desc)
  2103. return false;
  2104. is_enabled = cpumask_test_cpu(cpu, desc->percpu_enabled);
  2105. irq_put_desc_unlock(desc, flags);
  2106. return is_enabled;
  2107. }
  2108. EXPORT_SYMBOL_GPL(irq_percpu_is_enabled);
  2109. void disable_percpu_irq(unsigned int irq)
  2110. {
  2111. unsigned int cpu = smp_processor_id();
  2112. unsigned long flags;
  2113. struct irq_desc *desc = irq_get_desc_lock(irq, &flags, IRQ_GET_DESC_CHECK_PERCPU);
  2114. if (!desc)
  2115. return;
  2116. irq_percpu_disable(desc, cpu);
  2117. irq_put_desc_unlock(desc, flags);
  2118. }
  2119. EXPORT_SYMBOL_GPL(disable_percpu_irq);
  2120. void disable_percpu_nmi(unsigned int irq)
  2121. {
  2122. disable_percpu_irq(irq);
  2123. }
  2124. /*
  2125. * Internal function to unregister a percpu irqaction.
  2126. */
  2127. static struct irqaction *__free_percpu_irq(unsigned int irq, void __percpu *dev_id)
  2128. {
  2129. struct irq_desc *desc = irq_to_desc(irq);
  2130. struct irqaction *action;
  2131. unsigned long flags;
  2132. WARN(in_interrupt(), "Trying to free IRQ %d from IRQ context!\n", irq);
  2133. if (!desc)
  2134. return NULL;
  2135. raw_spin_lock_irqsave(&desc->lock, flags);
  2136. action = desc->action;
  2137. if (!action || action->percpu_dev_id != dev_id) {
  2138. WARN(1, "Trying to free already-free IRQ %d\n", irq);
  2139. goto bad;
  2140. }
  2141. if (!cpumask_empty(desc->percpu_enabled)) {
  2142. WARN(1, "percpu IRQ %d still enabled on CPU%d!\n",
  2143. irq, cpumask_first(desc->percpu_enabled));
  2144. goto bad;
  2145. }
  2146. /* Found it - now remove it from the list of entries: */
  2147. desc->action = NULL;
  2148. desc->istate &= ~IRQS_NMI;
  2149. raw_spin_unlock_irqrestore(&desc->lock, flags);
  2150. unregister_handler_proc(irq, action);
  2151. irq_chip_pm_put(&desc->irq_data);
  2152. module_put(desc->owner);
  2153. return action;
  2154. bad:
  2155. raw_spin_unlock_irqrestore(&desc->lock, flags);
  2156. return NULL;
  2157. }
  2158. /**
  2159. * remove_percpu_irq - free a per-cpu interrupt
  2160. * @irq: Interrupt line to free
  2161. * @act: irqaction for the interrupt
  2162. *
  2163. * Used to remove interrupts statically setup by the early boot process.
  2164. */
  2165. void remove_percpu_irq(unsigned int irq, struct irqaction *act)
  2166. {
  2167. struct irq_desc *desc = irq_to_desc(irq);
  2168. if (desc && irq_settings_is_per_cpu_devid(desc))
  2169. __free_percpu_irq(irq, act->percpu_dev_id);
  2170. }
  2171. /**
  2172. * free_percpu_irq - free an interrupt allocated with request_percpu_irq
  2173. * @irq: Interrupt line to free
  2174. * @dev_id: Device identity to free
  2175. *
  2176. * Remove a percpu interrupt handler. The handler is removed, but
  2177. * the interrupt line is not disabled. This must be done on each
  2178. * CPU before calling this function. The function does not return
  2179. * until any executing interrupts for this IRQ have completed.
  2180. *
  2181. * This function must not be called from interrupt context.
  2182. */
  2183. void free_percpu_irq(unsigned int irq, void __percpu *dev_id)
  2184. {
  2185. struct irq_desc *desc = irq_to_desc(irq);
  2186. if (!desc || !irq_settings_is_per_cpu_devid(desc))
  2187. return;
  2188. chip_bus_lock(desc);
  2189. kfree(__free_percpu_irq(irq, dev_id));
  2190. chip_bus_sync_unlock(desc);
  2191. }
  2192. EXPORT_SYMBOL_GPL(free_percpu_irq);
  2193. void free_percpu_nmi(unsigned int irq, void __percpu *dev_id)
  2194. {
  2195. struct irq_desc *desc = irq_to_desc(irq);
  2196. if (!desc || !irq_settings_is_per_cpu_devid(desc))
  2197. return;
  2198. if (WARN_ON(!irq_is_nmi(desc)))
  2199. return;
  2200. kfree(__free_percpu_irq(irq, dev_id));
  2201. }
  2202. /**
  2203. * setup_percpu_irq - setup a per-cpu interrupt
  2204. * @irq: Interrupt line to setup
  2205. * @act: irqaction for the interrupt
  2206. *
  2207. * Used to statically setup per-cpu interrupts in the early boot process.
  2208. */
  2209. int setup_percpu_irq(unsigned int irq, struct irqaction *act)
  2210. {
  2211. struct irq_desc *desc = irq_to_desc(irq);
  2212. int retval;
  2213. if (!desc || !irq_settings_is_per_cpu_devid(desc))
  2214. return -EINVAL;
  2215. retval = irq_chip_pm_get(&desc->irq_data);
  2216. if (retval < 0)
  2217. return retval;
  2218. retval = __setup_irq(irq, desc, act);
  2219. if (retval)
  2220. irq_chip_pm_put(&desc->irq_data);
  2221. return retval;
  2222. }
  2223. /**
  2224. * __request_percpu_irq - allocate a percpu interrupt line
  2225. * @irq: Interrupt line to allocate
  2226. * @handler: Function to be called when the IRQ occurs.
  2227. * @flags: Interrupt type flags (IRQF_TIMER only)
  2228. * @devname: An ascii name for the claiming device
  2229. * @dev_id: A percpu cookie passed back to the handler function
  2230. *
  2231. * This call allocates interrupt resources and enables the
  2232. * interrupt on the local CPU. If the interrupt is supposed to be
  2233. * enabled on other CPUs, it has to be done on each CPU using
  2234. * enable_percpu_irq().
  2235. *
  2236. * Dev_id must be globally unique. It is a per-cpu variable, and
  2237. * the handler gets called with the interrupted CPU's instance of
  2238. * that variable.
  2239. */
  2240. int __request_percpu_irq(unsigned int irq, irq_handler_t handler,
  2241. unsigned long flags, const char *devname,
  2242. void __percpu *dev_id)
  2243. {
  2244. struct irqaction *action;
  2245. struct irq_desc *desc;
  2246. int retval;
  2247. if (!dev_id)
  2248. return -EINVAL;
  2249. desc = irq_to_desc(irq);
  2250. if (!desc || !irq_settings_can_request(desc) ||
  2251. !irq_settings_is_per_cpu_devid(desc))
  2252. return -EINVAL;
  2253. if (flags && flags != IRQF_TIMER)
  2254. return -EINVAL;
  2255. action = kzalloc(sizeof(struct irqaction), GFP_KERNEL);
  2256. if (!action)
  2257. return -ENOMEM;
  2258. action->handler = handler;
  2259. action->flags = flags | IRQF_PERCPU | IRQF_NO_SUSPEND;
  2260. action->name = devname;
  2261. action->percpu_dev_id = dev_id;
  2262. retval = irq_chip_pm_get(&desc->irq_data);
  2263. if (retval < 0) {
  2264. kfree(action);
  2265. return retval;
  2266. }
  2267. retval = __setup_irq(irq, desc, action);
  2268. if (retval) {
  2269. irq_chip_pm_put(&desc->irq_data);
  2270. kfree(action);
  2271. }
  2272. return retval;
  2273. }
  2274. EXPORT_SYMBOL_GPL(__request_percpu_irq);
  2275. /**
  2276. * request_percpu_nmi - allocate a percpu interrupt line for NMI delivery
  2277. * @irq: Interrupt line to allocate
  2278. * @handler: Function to be called when the IRQ occurs.
  2279. * @name: An ascii name for the claiming device
  2280. * @dev_id: A percpu cookie passed back to the handler function
  2281. *
  2282. * This call allocates interrupt resources for a per CPU NMI. Per CPU NMIs
  2283. * have to be setup on each CPU by calling prepare_percpu_nmi() before
  2284. * being enabled on the same CPU by using enable_percpu_nmi().
  2285. *
  2286. * Dev_id must be globally unique. It is a per-cpu variable, and
  2287. * the handler gets called with the interrupted CPU's instance of
  2288. * that variable.
  2289. *
  2290. * Interrupt lines requested for NMI delivering should have auto enabling
  2291. * setting disabled.
  2292. *
  2293. * If the interrupt line cannot be used to deliver NMIs, function
  2294. * will fail returning a negative value.
  2295. */
  2296. int request_percpu_nmi(unsigned int irq, irq_handler_t handler,
  2297. const char *name, void __percpu *dev_id)
  2298. {
  2299. struct irqaction *action;
  2300. struct irq_desc *desc;
  2301. unsigned long flags;
  2302. int retval;
  2303. if (!handler)
  2304. return -EINVAL;
  2305. desc = irq_to_desc(irq);
  2306. if (!desc || !irq_settings_can_request(desc) ||
  2307. !irq_settings_is_per_cpu_devid(desc) ||
  2308. irq_settings_can_autoenable(desc) ||
  2309. !irq_supports_nmi(desc))
  2310. return -EINVAL;
  2311. /* The line cannot already be NMI */
  2312. if (irq_is_nmi(desc))
  2313. return -EINVAL;
  2314. action = kzalloc(sizeof(struct irqaction), GFP_KERNEL);
  2315. if (!action)
  2316. return -ENOMEM;
  2317. action->handler = handler;
  2318. action->flags = IRQF_PERCPU | IRQF_NO_SUSPEND | IRQF_NO_THREAD
  2319. | IRQF_NOBALANCING;
  2320. action->name = name;
  2321. action->percpu_dev_id = dev_id;
  2322. retval = irq_chip_pm_get(&desc->irq_data);
  2323. if (retval < 0)
  2324. goto err_out;
  2325. retval = __setup_irq(irq, desc, action);
  2326. if (retval)
  2327. goto err_irq_setup;
  2328. raw_spin_lock_irqsave(&desc->lock, flags);
  2329. desc->istate |= IRQS_NMI;
  2330. raw_spin_unlock_irqrestore(&desc->lock, flags);
  2331. return 0;
  2332. err_irq_setup:
  2333. irq_chip_pm_put(&desc->irq_data);
  2334. err_out:
  2335. kfree(action);
  2336. return retval;
  2337. }
  2338. /**
  2339. * prepare_percpu_nmi - performs CPU local setup for NMI delivery
  2340. * @irq: Interrupt line to prepare for NMI delivery
  2341. *
  2342. * This call prepares an interrupt line to deliver NMI on the current CPU,
  2343. * before that interrupt line gets enabled with enable_percpu_nmi().
  2344. *
  2345. * As a CPU local operation, this should be called from non-preemptible
  2346. * context.
  2347. *
  2348. * If the interrupt line cannot be used to deliver NMIs, function
  2349. * will fail returning a negative value.
  2350. */
  2351. int prepare_percpu_nmi(unsigned int irq)
  2352. {
  2353. unsigned long flags;
  2354. struct irq_desc *desc;
  2355. int ret = 0;
  2356. WARN_ON(preemptible());
  2357. desc = irq_get_desc_lock(irq, &flags,
  2358. IRQ_GET_DESC_CHECK_PERCPU);
  2359. if (!desc)
  2360. return -EINVAL;
  2361. if (WARN(!irq_is_nmi(desc),
  2362. KERN_ERR "prepare_percpu_nmi called for a non-NMI interrupt: irq %u\n",
  2363. irq)) {
  2364. ret = -EINVAL;
  2365. goto out;
  2366. }
  2367. ret = irq_nmi_setup(desc);
  2368. if (ret) {
  2369. pr_err("Failed to setup NMI delivery: irq %u\n", irq);
  2370. goto out;
  2371. }
  2372. out:
  2373. irq_put_desc_unlock(desc, flags);
  2374. return ret;
  2375. }
  2376. /**
  2377. * teardown_percpu_nmi - undoes NMI setup of IRQ line
  2378. * @irq: Interrupt line from which CPU local NMI configuration should be
  2379. * removed
  2380. *
  2381. * This call undoes the setup done by prepare_percpu_nmi().
  2382. *
  2383. * IRQ line should not be enabled for the current CPU.
  2384. *
  2385. * As a CPU local operation, this should be called from non-preemptible
  2386. * context.
  2387. */
  2388. void teardown_percpu_nmi(unsigned int irq)
  2389. {
  2390. unsigned long flags;
  2391. struct irq_desc *desc;
  2392. WARN_ON(preemptible());
  2393. desc = irq_get_desc_lock(irq, &flags,
  2394. IRQ_GET_DESC_CHECK_PERCPU);
  2395. if (!desc)
  2396. return;
  2397. if (WARN_ON(!irq_is_nmi(desc)))
  2398. goto out;
  2399. irq_nmi_teardown(desc);
  2400. out:
  2401. irq_put_desc_unlock(desc, flags);
  2402. }
  2403. int __irq_get_irqchip_state(struct irq_data *data, enum irqchip_irq_state which,
  2404. bool *state)
  2405. {
  2406. struct irq_chip *chip;
  2407. int err = -EINVAL;
  2408. do {
  2409. chip = irq_data_get_irq_chip(data);
  2410. if (WARN_ON_ONCE(!chip))
  2411. return -ENODEV;
  2412. if (chip->irq_get_irqchip_state)
  2413. break;
  2414. #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
  2415. data = data->parent_data;
  2416. #else
  2417. data = NULL;
  2418. #endif
  2419. } while (data);
  2420. if (data)
  2421. err = chip->irq_get_irqchip_state(data, which, state);
  2422. return err;
  2423. }
  2424. /**
  2425. * irq_get_irqchip_state - returns the irqchip state of a interrupt.
  2426. * @irq: Interrupt line that is forwarded to a VM
  2427. * @which: One of IRQCHIP_STATE_* the caller wants to know about
  2428. * @state: a pointer to a boolean where the state is to be stored
  2429. *
  2430. * This call snapshots the internal irqchip state of an
  2431. * interrupt, returning into @state the bit corresponding to
  2432. * stage @which
  2433. *
  2434. * This function should be called with preemption disabled if the
  2435. * interrupt controller has per-cpu registers.
  2436. */
  2437. int irq_get_irqchip_state(unsigned int irq, enum irqchip_irq_state which,
  2438. bool *state)
  2439. {
  2440. struct irq_desc *desc;
  2441. struct irq_data *data;
  2442. unsigned long flags;
  2443. int err = -EINVAL;
  2444. desc = irq_get_desc_buslock(irq, &flags, 0);
  2445. if (!desc)
  2446. return err;
  2447. data = irq_desc_get_irq_data(desc);
  2448. err = __irq_get_irqchip_state(data, which, state);
  2449. irq_put_desc_busunlock(desc, flags);
  2450. return err;
  2451. }
  2452. EXPORT_SYMBOL_GPL(irq_get_irqchip_state);
  2453. /**
  2454. * irq_set_irqchip_state - set the state of a forwarded interrupt.
  2455. * @irq: Interrupt line that is forwarded to a VM
  2456. * @which: State to be restored (one of IRQCHIP_STATE_*)
  2457. * @val: Value corresponding to @which
  2458. *
  2459. * This call sets the internal irqchip state of an interrupt,
  2460. * depending on the value of @which.
  2461. *
  2462. * This function should be called with migration disabled if the
  2463. * interrupt controller has per-cpu registers.
  2464. */
  2465. int irq_set_irqchip_state(unsigned int irq, enum irqchip_irq_state which,
  2466. bool val)
  2467. {
  2468. struct irq_desc *desc;
  2469. struct irq_data *data;
  2470. struct irq_chip *chip;
  2471. unsigned long flags;
  2472. int err = -EINVAL;
  2473. desc = irq_get_desc_buslock(irq, &flags, 0);
  2474. if (!desc)
  2475. return err;
  2476. data = irq_desc_get_irq_data(desc);
  2477. do {
  2478. chip = irq_data_get_irq_chip(data);
  2479. if (WARN_ON_ONCE(!chip)) {
  2480. err = -ENODEV;
  2481. goto out_unlock;
  2482. }
  2483. if (chip->irq_set_irqchip_state)
  2484. break;
  2485. #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
  2486. data = data->parent_data;
  2487. #else
  2488. data = NULL;
  2489. #endif
  2490. } while (data);
  2491. if (data)
  2492. err = chip->irq_set_irqchip_state(data, which, val);
  2493. out_unlock:
  2494. irq_put_desc_busunlock(desc, flags);
  2495. return err;
  2496. }
  2497. EXPORT_SYMBOL_GPL(irq_set_irqchip_state);
  2498. /**
  2499. * irq_has_action - Check whether an interrupt is requested
  2500. * @irq: The linux irq number
  2501. *
  2502. * Returns: A snapshot of the current state
  2503. */
  2504. bool irq_has_action(unsigned int irq)
  2505. {
  2506. bool res;
  2507. rcu_read_lock();
  2508. res = irq_desc_has_action(irq_to_desc(irq));
  2509. rcu_read_unlock();
  2510. return res;
  2511. }
  2512. EXPORT_SYMBOL_GPL(irq_has_action);
  2513. /**
  2514. * irq_check_status_bit - Check whether bits in the irq descriptor status are set
  2515. * @irq: The linux irq number
  2516. * @bitmask: The bitmask to evaluate
  2517. *
  2518. * Returns: True if one of the bits in @bitmask is set
  2519. */
  2520. bool irq_check_status_bit(unsigned int irq, unsigned int bitmask)
  2521. {
  2522. struct irq_desc *desc;
  2523. bool res = false;
  2524. rcu_read_lock();
  2525. desc = irq_to_desc(irq);
  2526. if (desc)
  2527. res = !!(desc->status_use_accessors & bitmask);
  2528. rcu_read_unlock();
  2529. return res;
  2530. }
  2531. EXPORT_SYMBOL_GPL(irq_check_status_bit);