manage.c 61 KB

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