manage.c 55 KB

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