manage.c 50 KB

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