chip.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571
  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, Russell King
  5. *
  6. * This file contains the core interrupt handling code, for irq-chip based
  7. * architectures. Detailed information is available in
  8. * Documentation/core-api/genericirq.rst
  9. */
  10. #include <linux/irq.h>
  11. #include <linux/msi.h>
  12. #include <linux/module.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/kernel_stat.h>
  15. #include <linux/irqdomain.h>
  16. #include <trace/events/irq.h>
  17. #include "internals.h"
  18. static irqreturn_t bad_chained_irq(int irq, void *dev_id)
  19. {
  20. WARN_ONCE(1, "Chained irq %d should not call an action\n", irq);
  21. return IRQ_NONE;
  22. }
  23. /*
  24. * Chained handlers should never call action on their IRQ. This default
  25. * action will emit warning if such thing happens.
  26. */
  27. struct irqaction chained_action = {
  28. .handler = bad_chained_irq,
  29. };
  30. /**
  31. * irq_set_chip - set the irq chip for an irq
  32. * @irq: irq number
  33. * @chip: pointer to irq chip description structure
  34. */
  35. int irq_set_chip(unsigned int irq, const struct irq_chip *chip)
  36. {
  37. int ret = -EINVAL;
  38. scoped_irqdesc_get_and_lock(irq, 0) {
  39. scoped_irqdesc->irq_data.chip = (struct irq_chip *)(chip ?: &no_irq_chip);
  40. ret = 0;
  41. }
  42. /* For !CONFIG_SPARSE_IRQ make the irq show up in allocated_irqs. */
  43. if (!ret)
  44. irq_mark_irq(irq);
  45. return ret;
  46. }
  47. EXPORT_SYMBOL(irq_set_chip);
  48. /**
  49. * irq_set_irq_type - set the irq trigger type for an irq
  50. * @irq: irq number
  51. * @type: IRQ_TYPE_{LEVEL,EDGE}_* value - see include/linux/irq.h
  52. */
  53. int irq_set_irq_type(unsigned int irq, unsigned int type)
  54. {
  55. scoped_irqdesc_get_and_buslock(irq, IRQ_GET_DESC_CHECK_GLOBAL)
  56. return __irq_set_trigger(scoped_irqdesc, type);
  57. return -EINVAL;
  58. }
  59. EXPORT_SYMBOL(irq_set_irq_type);
  60. /**
  61. * irq_set_handler_data - set irq handler data for an irq
  62. * @irq: Interrupt number
  63. * @data: Pointer to interrupt specific data
  64. *
  65. * Set the hardware irq controller data for an irq
  66. */
  67. int irq_set_handler_data(unsigned int irq, void *data)
  68. {
  69. scoped_irqdesc_get_and_lock(irq, 0) {
  70. scoped_irqdesc->irq_common_data.handler_data = data;
  71. return 0;
  72. }
  73. return -EINVAL;
  74. }
  75. EXPORT_SYMBOL(irq_set_handler_data);
  76. /**
  77. * irq_set_msi_desc_off - set MSI descriptor data for an irq at offset
  78. * @irq_base: Interrupt number base
  79. * @irq_offset: Interrupt number offset
  80. * @entry: Pointer to MSI descriptor data
  81. *
  82. * Set the MSI descriptor entry for an irq at offset
  83. */
  84. int irq_set_msi_desc_off(unsigned int irq_base, unsigned int irq_offset, struct msi_desc *entry)
  85. {
  86. scoped_irqdesc_get_and_lock(irq_base + irq_offset, IRQ_GET_DESC_CHECK_GLOBAL) {
  87. scoped_irqdesc->irq_common_data.msi_desc = entry;
  88. if (entry && !irq_offset)
  89. entry->irq = irq_base;
  90. return 0;
  91. }
  92. return -EINVAL;
  93. }
  94. /**
  95. * irq_set_msi_desc - set MSI descriptor data for an irq
  96. * @irq: Interrupt number
  97. * @entry: Pointer to MSI descriptor data
  98. *
  99. * Set the MSI descriptor entry for an irq
  100. */
  101. int irq_set_msi_desc(unsigned int irq, struct msi_desc *entry)
  102. {
  103. return irq_set_msi_desc_off(irq, 0, entry);
  104. }
  105. /**
  106. * irq_set_chip_data - set irq chip data for an irq
  107. * @irq: Interrupt number
  108. * @data: Pointer to chip specific data
  109. *
  110. * Set the hardware irq chip data for an irq
  111. */
  112. int irq_set_chip_data(unsigned int irq, void *data)
  113. {
  114. scoped_irqdesc_get_and_lock(irq, 0) {
  115. scoped_irqdesc->irq_data.chip_data = data;
  116. return 0;
  117. }
  118. return -EINVAL;
  119. }
  120. EXPORT_SYMBOL(irq_set_chip_data);
  121. struct irq_data *irq_get_irq_data(unsigned int irq)
  122. {
  123. struct irq_desc *desc = irq_to_desc(irq);
  124. return desc ? &desc->irq_data : NULL;
  125. }
  126. EXPORT_SYMBOL_GPL(irq_get_irq_data);
  127. static void irq_state_clr_disabled(struct irq_desc *desc)
  128. {
  129. irqd_clear(&desc->irq_data, IRQD_IRQ_DISABLED);
  130. }
  131. static void irq_state_clr_masked(struct irq_desc *desc)
  132. {
  133. irqd_clear(&desc->irq_data, IRQD_IRQ_MASKED);
  134. }
  135. static void irq_state_clr_started(struct irq_desc *desc)
  136. {
  137. irqd_clear(&desc->irq_data, IRQD_IRQ_STARTED);
  138. }
  139. static void irq_state_set_started(struct irq_desc *desc)
  140. {
  141. irqd_set(&desc->irq_data, IRQD_IRQ_STARTED);
  142. }
  143. enum {
  144. IRQ_STARTUP_NORMAL,
  145. IRQ_STARTUP_MANAGED,
  146. IRQ_STARTUP_ABORT,
  147. };
  148. #ifdef CONFIG_SMP
  149. static int
  150. __irq_startup_managed(struct irq_desc *desc, const struct cpumask *aff,
  151. bool force)
  152. {
  153. struct irq_data *d = irq_desc_get_irq_data(desc);
  154. if (!irqd_affinity_is_managed(d))
  155. return IRQ_STARTUP_NORMAL;
  156. irqd_clr_managed_shutdown(d);
  157. if (!cpumask_intersects(aff, cpu_online_mask)) {
  158. /*
  159. * Catch code which fiddles with enable_irq() on a managed
  160. * and potentially shutdown IRQ. Chained interrupt
  161. * installment or irq auto probing should not happen on
  162. * managed irqs either.
  163. */
  164. if (WARN_ON_ONCE(force))
  165. return IRQ_STARTUP_ABORT;
  166. /*
  167. * The interrupt was requested, but there is no online CPU
  168. * in it's affinity mask. Put it into managed shutdown
  169. * state and let the cpu hotplug mechanism start it up once
  170. * a CPU in the mask becomes available.
  171. */
  172. return IRQ_STARTUP_ABORT;
  173. }
  174. /*
  175. * Managed interrupts have reserved resources, so this should not
  176. * happen.
  177. */
  178. if (WARN_ON(irq_domain_activate_irq(d, false)))
  179. return IRQ_STARTUP_ABORT;
  180. return IRQ_STARTUP_MANAGED;
  181. }
  182. void irq_startup_managed(struct irq_desc *desc)
  183. {
  184. struct irq_data *d = irq_desc_get_irq_data(desc);
  185. /*
  186. * Clear managed-shutdown flag, so we don't repeat managed-startup for
  187. * multiple hotplugs, and cause imbalanced disable depth.
  188. */
  189. irqd_clr_managed_shutdown(d);
  190. /*
  191. * Only start it up when the disable depth is 1, so that a disable,
  192. * hotunplug, hotplug sequence does not end up enabling it during
  193. * hotplug unconditionally.
  194. */
  195. desc->depth--;
  196. if (!desc->depth)
  197. irq_startup(desc, IRQ_RESEND, IRQ_START_COND);
  198. }
  199. #else
  200. static __always_inline int
  201. __irq_startup_managed(struct irq_desc *desc, const struct cpumask *aff,
  202. bool force)
  203. {
  204. return IRQ_STARTUP_NORMAL;
  205. }
  206. #endif
  207. static void irq_enable(struct irq_desc *desc)
  208. {
  209. if (!irqd_irq_disabled(&desc->irq_data)) {
  210. unmask_irq(desc);
  211. } else {
  212. irq_state_clr_disabled(desc);
  213. if (desc->irq_data.chip->irq_enable) {
  214. desc->irq_data.chip->irq_enable(&desc->irq_data);
  215. irq_state_clr_masked(desc);
  216. } else {
  217. unmask_irq(desc);
  218. }
  219. }
  220. }
  221. static int __irq_startup(struct irq_desc *desc)
  222. {
  223. struct irq_data *d = irq_desc_get_irq_data(desc);
  224. int ret = 0;
  225. /* Warn if this interrupt is not activated but try nevertheless */
  226. WARN_ON_ONCE(!irqd_is_activated(d));
  227. if (d->chip->irq_startup) {
  228. ret = d->chip->irq_startup(d);
  229. irq_state_clr_disabled(desc);
  230. irq_state_clr_masked(desc);
  231. } else {
  232. irq_enable(desc);
  233. }
  234. irq_state_set_started(desc);
  235. return ret;
  236. }
  237. int irq_startup(struct irq_desc *desc, bool resend, bool force)
  238. {
  239. struct irq_data *d = irq_desc_get_irq_data(desc);
  240. const struct cpumask *aff = irq_data_get_affinity_mask(d);
  241. int ret = 0;
  242. desc->depth = 0;
  243. if (irqd_is_started(d)) {
  244. irq_enable(desc);
  245. } else {
  246. switch (__irq_startup_managed(desc, aff, force)) {
  247. case IRQ_STARTUP_NORMAL:
  248. if (d->chip->flags & IRQCHIP_AFFINITY_PRE_STARTUP)
  249. irq_setup_affinity(desc);
  250. ret = __irq_startup(desc);
  251. if (!(d->chip->flags & IRQCHIP_AFFINITY_PRE_STARTUP))
  252. irq_setup_affinity(desc);
  253. break;
  254. case IRQ_STARTUP_MANAGED:
  255. irq_do_set_affinity(d, aff, false);
  256. ret = __irq_startup(desc);
  257. break;
  258. case IRQ_STARTUP_ABORT:
  259. desc->depth = 1;
  260. irqd_set_managed_shutdown(d);
  261. return 0;
  262. }
  263. }
  264. if (resend)
  265. check_irq_resend(desc, false);
  266. return ret;
  267. }
  268. int irq_activate(struct irq_desc *desc)
  269. {
  270. struct irq_data *d = irq_desc_get_irq_data(desc);
  271. if (!irqd_affinity_is_managed(d))
  272. return irq_domain_activate_irq(d, false);
  273. return 0;
  274. }
  275. int irq_activate_and_startup(struct irq_desc *desc, bool resend)
  276. {
  277. if (WARN_ON(irq_activate(desc)))
  278. return 0;
  279. return irq_startup(desc, resend, IRQ_START_FORCE);
  280. }
  281. static void __irq_disable(struct irq_desc *desc, bool mask);
  282. void irq_shutdown(struct irq_desc *desc)
  283. {
  284. if (irqd_is_started(&desc->irq_data)) {
  285. clear_irq_resend(desc);
  286. /*
  287. * Increment disable depth, so that a managed shutdown on
  288. * CPU hotunplug preserves the actual disabled state when the
  289. * CPU comes back online. See irq_startup_managed().
  290. */
  291. desc->depth++;
  292. if (desc->irq_data.chip->irq_shutdown) {
  293. desc->irq_data.chip->irq_shutdown(&desc->irq_data);
  294. irq_state_set_disabled(desc);
  295. irq_state_set_masked(desc);
  296. } else {
  297. __irq_disable(desc, true);
  298. }
  299. irq_state_clr_started(desc);
  300. }
  301. }
  302. void irq_shutdown_and_deactivate(struct irq_desc *desc)
  303. {
  304. irq_shutdown(desc);
  305. /*
  306. * This must be called even if the interrupt was never started up,
  307. * because the activation can happen before the interrupt is
  308. * available for request/startup. It has it's own state tracking so
  309. * it's safe to call it unconditionally.
  310. */
  311. irq_domain_deactivate_irq(&desc->irq_data);
  312. }
  313. static void __irq_disable(struct irq_desc *desc, bool mask)
  314. {
  315. if (irqd_irq_disabled(&desc->irq_data)) {
  316. if (mask)
  317. mask_irq(desc);
  318. } else {
  319. irq_state_set_disabled(desc);
  320. if (desc->irq_data.chip->irq_disable) {
  321. desc->irq_data.chip->irq_disable(&desc->irq_data);
  322. irq_state_set_masked(desc);
  323. } else if (mask) {
  324. mask_irq(desc);
  325. }
  326. }
  327. }
  328. /**
  329. * irq_disable - Mark interrupt disabled
  330. * @desc: irq descriptor which should be disabled
  331. *
  332. * If the chip does not implement the irq_disable callback, we
  333. * use a lazy disable approach. That means we mark the interrupt
  334. * disabled, but leave the hardware unmasked. That's an
  335. * optimization because we avoid the hardware access for the
  336. * common case where no interrupt happens after we marked it
  337. * disabled. If an interrupt happens, then the interrupt flow
  338. * handler masks the line at the hardware level and marks it
  339. * pending.
  340. *
  341. * If the interrupt chip does not implement the irq_disable callback,
  342. * a driver can disable the lazy approach for a particular irq line by
  343. * calling 'irq_set_status_flags(irq, IRQ_DISABLE_UNLAZY)'. This can
  344. * be used for devices which cannot disable the interrupt at the
  345. * device level under certain circumstances and have to use
  346. * disable_irq[_nosync] instead.
  347. */
  348. void irq_disable(struct irq_desc *desc)
  349. {
  350. __irq_disable(desc, irq_settings_disable_unlazy(desc));
  351. }
  352. void irq_percpu_enable(struct irq_desc *desc, unsigned int cpu)
  353. {
  354. if (desc->irq_data.chip->irq_enable)
  355. desc->irq_data.chip->irq_enable(&desc->irq_data);
  356. else
  357. desc->irq_data.chip->irq_unmask(&desc->irq_data);
  358. cpumask_set_cpu(cpu, desc->percpu_enabled);
  359. }
  360. void irq_percpu_disable(struct irq_desc *desc, unsigned int cpu)
  361. {
  362. if (desc->irq_data.chip->irq_disable)
  363. desc->irq_data.chip->irq_disable(&desc->irq_data);
  364. else
  365. desc->irq_data.chip->irq_mask(&desc->irq_data);
  366. cpumask_clear_cpu(cpu, desc->percpu_enabled);
  367. }
  368. static inline void mask_ack_irq(struct irq_desc *desc)
  369. {
  370. if (desc->irq_data.chip->irq_mask_ack) {
  371. desc->irq_data.chip->irq_mask_ack(&desc->irq_data);
  372. irq_state_set_masked(desc);
  373. } else {
  374. mask_irq(desc);
  375. if (desc->irq_data.chip->irq_ack)
  376. desc->irq_data.chip->irq_ack(&desc->irq_data);
  377. }
  378. }
  379. void mask_irq(struct irq_desc *desc)
  380. {
  381. if (irqd_irq_masked(&desc->irq_data))
  382. return;
  383. if (desc->irq_data.chip->irq_mask) {
  384. desc->irq_data.chip->irq_mask(&desc->irq_data);
  385. irq_state_set_masked(desc);
  386. }
  387. }
  388. void unmask_irq(struct irq_desc *desc)
  389. {
  390. if (!irqd_irq_masked(&desc->irq_data))
  391. return;
  392. if (desc->irq_data.chip->irq_unmask) {
  393. desc->irq_data.chip->irq_unmask(&desc->irq_data);
  394. irq_state_clr_masked(desc);
  395. }
  396. }
  397. void unmask_threaded_irq(struct irq_desc *desc)
  398. {
  399. struct irq_chip *chip = desc->irq_data.chip;
  400. if (chip->flags & IRQCHIP_EOI_THREADED)
  401. chip->irq_eoi(&desc->irq_data);
  402. unmask_irq(desc);
  403. }
  404. /* Busy wait until INPROGRESS is cleared */
  405. static bool irq_wait_on_inprogress(struct irq_desc *desc)
  406. {
  407. if (IS_ENABLED(CONFIG_SMP)) {
  408. do {
  409. raw_spin_unlock(&desc->lock);
  410. while (irqd_irq_inprogress(&desc->irq_data))
  411. cpu_relax();
  412. raw_spin_lock(&desc->lock);
  413. } while (irqd_irq_inprogress(&desc->irq_data));
  414. /* Might have been disabled in meantime */
  415. return !irqd_irq_disabled(&desc->irq_data) && desc->action;
  416. }
  417. return false;
  418. }
  419. static bool irq_can_handle_pm(struct irq_desc *desc)
  420. {
  421. struct irq_data *irqd = &desc->irq_data;
  422. const struct cpumask *aff;
  423. /*
  424. * If the interrupt is not in progress and is not an armed
  425. * wakeup interrupt, proceed.
  426. */
  427. if (!irqd_has_set(irqd, IRQD_IRQ_INPROGRESS | IRQD_WAKEUP_ARMED))
  428. return true;
  429. /*
  430. * If the interrupt is an armed wakeup source, mark it pending
  431. * and suspended, disable it and notify the pm core about the
  432. * event.
  433. */
  434. if (unlikely(irqd_has_set(irqd, IRQD_WAKEUP_ARMED))) {
  435. irq_pm_handle_wakeup(desc);
  436. return false;
  437. }
  438. /* Check whether the interrupt is polled on another CPU */
  439. if (unlikely(desc->istate & IRQS_POLL_INPROGRESS)) {
  440. if (WARN_ONCE(irq_poll_cpu == smp_processor_id(),
  441. "irq poll in progress on cpu %d for irq %d\n",
  442. smp_processor_id(), desc->irq_data.irq))
  443. return false;
  444. return irq_wait_on_inprogress(desc);
  445. }
  446. /* The below works only for single target interrupts */
  447. if (!IS_ENABLED(CONFIG_GENERIC_IRQ_EFFECTIVE_AFF_MASK) ||
  448. !irqd_is_single_target(irqd) || desc->handle_irq != handle_edge_irq)
  449. return false;
  450. /*
  451. * If the interrupt affinity was moved to this CPU and the
  452. * interrupt is currently handled on the previous target CPU, then
  453. * busy wait for INPROGRESS to be cleared. Otherwise for edge type
  454. * interrupts the handler might get stuck on the previous target:
  455. *
  456. * CPU 0 CPU 1 (new target)
  457. * handle_edge_irq()
  458. * repeat:
  459. * handle_event() handle_edge_irq()
  460. * if (INPROGESS) {
  461. * set(PENDING);
  462. * mask();
  463. * return;
  464. * }
  465. * if (PENDING) {
  466. * clear(PENDING);
  467. * unmask();
  468. * goto repeat;
  469. * }
  470. *
  471. * This happens when the device raises interrupts with a high rate
  472. * and always before handle_event() completes and the CPU0 handler
  473. * can clear INPROGRESS. This has been observed in virtual machines.
  474. */
  475. aff = irq_data_get_effective_affinity_mask(irqd);
  476. if (cpumask_first(aff) != smp_processor_id())
  477. return false;
  478. return irq_wait_on_inprogress(desc);
  479. }
  480. static inline bool irq_can_handle_actions(struct irq_desc *desc)
  481. {
  482. desc->istate &= ~(IRQS_REPLAY | IRQS_WAITING);
  483. if (unlikely(!desc->action || irqd_irq_disabled(&desc->irq_data))) {
  484. desc->istate |= IRQS_PENDING;
  485. return false;
  486. }
  487. return true;
  488. }
  489. static inline bool irq_can_handle(struct irq_desc *desc)
  490. {
  491. if (!irq_can_handle_pm(desc))
  492. return false;
  493. return irq_can_handle_actions(desc);
  494. }
  495. /**
  496. * handle_nested_irq - Handle a nested irq from a irq thread
  497. * @irq: the interrupt number
  498. *
  499. * Handle interrupts which are nested into a threaded interrupt
  500. * handler. The handler function is called inside the calling threads
  501. * context.
  502. */
  503. void handle_nested_irq(unsigned int irq)
  504. {
  505. struct irq_desc *desc = irq_to_desc(irq);
  506. struct irqaction *action;
  507. irqreturn_t action_ret;
  508. might_sleep();
  509. scoped_guard(raw_spinlock_irq, &desc->lock) {
  510. if (!irq_can_handle_actions(desc))
  511. return;
  512. action = desc->action;
  513. kstat_incr_irqs_this_cpu(desc);
  514. atomic_inc(&desc->threads_active);
  515. }
  516. action_ret = IRQ_NONE;
  517. for_each_action_of_desc(desc, action)
  518. action_ret |= action->thread_fn(action->irq, action->dev_id);
  519. if (!irq_settings_no_debug(desc))
  520. note_interrupt(desc, action_ret);
  521. wake_threads_waitq(desc);
  522. }
  523. EXPORT_SYMBOL_GPL(handle_nested_irq);
  524. /**
  525. * handle_simple_irq - Simple and software-decoded IRQs.
  526. * @desc: the interrupt description structure for this irq
  527. *
  528. * Simple interrupts are either sent from a demultiplexing interrupt
  529. * handler or come from hardware, where no interrupt hardware control is
  530. * necessary.
  531. *
  532. * Note: The caller is expected to handle the ack, clear, mask and unmask
  533. * issues if necessary.
  534. */
  535. void handle_simple_irq(struct irq_desc *desc)
  536. {
  537. guard(raw_spinlock)(&desc->lock);
  538. if (!irq_can_handle_pm(desc)) {
  539. if (irqd_needs_resend_when_in_progress(&desc->irq_data))
  540. desc->istate |= IRQS_PENDING;
  541. return;
  542. }
  543. if (!irq_can_handle_actions(desc))
  544. return;
  545. kstat_incr_irqs_this_cpu(desc);
  546. handle_irq_event(desc);
  547. }
  548. EXPORT_SYMBOL_GPL(handle_simple_irq);
  549. /**
  550. * handle_untracked_irq - Simple and software-decoded IRQs.
  551. * @desc: the interrupt description structure for this irq
  552. *
  553. * Untracked interrupts are sent from a demultiplexing interrupt handler
  554. * when the demultiplexer does not know which device it its multiplexed irq
  555. * domain generated the interrupt. IRQ's handled through here are not
  556. * subjected to stats tracking, randomness, or spurious interrupt
  557. * detection.
  558. *
  559. * Note: Like handle_simple_irq, the caller is expected to handle the ack,
  560. * clear, mask and unmask issues if necessary.
  561. */
  562. void handle_untracked_irq(struct irq_desc *desc)
  563. {
  564. scoped_guard(raw_spinlock, &desc->lock) {
  565. if (!irq_can_handle(desc))
  566. return;
  567. desc->istate &= ~IRQS_PENDING;
  568. irqd_set(&desc->irq_data, IRQD_IRQ_INPROGRESS);
  569. }
  570. __handle_irq_event_percpu(desc);
  571. scoped_guard(raw_spinlock, &desc->lock)
  572. irqd_clear(&desc->irq_data, IRQD_IRQ_INPROGRESS);
  573. }
  574. EXPORT_SYMBOL_GPL(handle_untracked_irq);
  575. /*
  576. * Called unconditionally from handle_level_irq() and only for oneshot
  577. * interrupts from handle_fasteoi_irq()
  578. */
  579. static void cond_unmask_irq(struct irq_desc *desc)
  580. {
  581. /*
  582. * We need to unmask in the following cases:
  583. * - Standard level irq (IRQF_ONESHOT is not set)
  584. * - Oneshot irq which did not wake the thread (caused by a
  585. * spurious interrupt or a primary handler handling it
  586. * completely).
  587. */
  588. if (!irqd_irq_disabled(&desc->irq_data) &&
  589. irqd_irq_masked(&desc->irq_data) && !desc->threads_oneshot)
  590. unmask_irq(desc);
  591. }
  592. /**
  593. * handle_level_irq - Level type irq handler
  594. * @desc: the interrupt description structure for this irq
  595. *
  596. * Level type interrupts are active as long as the hardware line has the
  597. * active level. This may require to mask the interrupt and unmask it after
  598. * the associated handler has acknowledged the device, so the interrupt
  599. * line is back to inactive.
  600. */
  601. void handle_level_irq(struct irq_desc *desc)
  602. {
  603. guard(raw_spinlock)(&desc->lock);
  604. mask_ack_irq(desc);
  605. if (!irq_can_handle(desc))
  606. return;
  607. kstat_incr_irqs_this_cpu(desc);
  608. handle_irq_event(desc);
  609. cond_unmask_irq(desc);
  610. }
  611. EXPORT_SYMBOL_GPL(handle_level_irq);
  612. static void cond_unmask_eoi_irq(struct irq_desc *desc, struct irq_chip *chip)
  613. {
  614. if (!(desc->istate & IRQS_ONESHOT)) {
  615. chip->irq_eoi(&desc->irq_data);
  616. return;
  617. }
  618. /*
  619. * We need to unmask in the following cases:
  620. * - Oneshot irq which did not wake the thread (caused by a
  621. * spurious interrupt or a primary handler handling it
  622. * completely).
  623. */
  624. if (!irqd_irq_disabled(&desc->irq_data) &&
  625. irqd_irq_masked(&desc->irq_data) && !desc->threads_oneshot) {
  626. chip->irq_eoi(&desc->irq_data);
  627. unmask_irq(desc);
  628. } else if (!(chip->flags & IRQCHIP_EOI_THREADED)) {
  629. chip->irq_eoi(&desc->irq_data);
  630. }
  631. }
  632. static inline void cond_eoi_irq(struct irq_chip *chip, struct irq_data *data)
  633. {
  634. if (!(chip->flags & IRQCHIP_EOI_IF_HANDLED))
  635. chip->irq_eoi(data);
  636. }
  637. /**
  638. * handle_fasteoi_irq - irq handler for transparent controllers
  639. * @desc: the interrupt description structure for this irq
  640. *
  641. * Only a single callback will be issued to the chip: an ->eoi() call when
  642. * the interrupt has been serviced. This enables support for modern forms
  643. * of interrupt handlers, which handle the flow details in hardware,
  644. * transparently.
  645. */
  646. void handle_fasteoi_irq(struct irq_desc *desc)
  647. {
  648. struct irq_chip *chip = desc->irq_data.chip;
  649. guard(raw_spinlock)(&desc->lock);
  650. /*
  651. * When an affinity change races with IRQ handling, the next interrupt
  652. * can arrive on the new CPU before the original CPU has completed
  653. * handling the previous one - it may need to be resent.
  654. */
  655. if (!irq_can_handle_pm(desc)) {
  656. if (irqd_needs_resend_when_in_progress(&desc->irq_data))
  657. desc->istate |= IRQS_PENDING;
  658. cond_eoi_irq(chip, &desc->irq_data);
  659. return;
  660. }
  661. if (!irq_can_handle_actions(desc)) {
  662. mask_irq(desc);
  663. cond_eoi_irq(chip, &desc->irq_data);
  664. return;
  665. }
  666. kstat_incr_irqs_this_cpu(desc);
  667. if (desc->istate & IRQS_ONESHOT)
  668. mask_irq(desc);
  669. handle_irq_event(desc);
  670. cond_unmask_eoi_irq(desc, chip);
  671. /*
  672. * When the race described above happens this will resend the interrupt.
  673. */
  674. if (unlikely(desc->istate & IRQS_PENDING))
  675. check_irq_resend(desc, false);
  676. }
  677. EXPORT_SYMBOL_GPL(handle_fasteoi_irq);
  678. /**
  679. * handle_fasteoi_nmi - irq handler for NMI interrupt lines
  680. * @desc: the interrupt description structure for this irq
  681. *
  682. * A simple NMI-safe handler, considering the restrictions
  683. * from request_nmi.
  684. *
  685. * Only a single callback will be issued to the chip: an ->eoi()
  686. * call when the interrupt has been serviced. This enables support
  687. * for modern forms of interrupt handlers, which handle the flow
  688. * details in hardware, transparently.
  689. */
  690. void handle_fasteoi_nmi(struct irq_desc *desc)
  691. {
  692. struct irq_chip *chip = irq_desc_get_chip(desc);
  693. struct irqaction *action = desc->action;
  694. unsigned int irq = irq_desc_get_irq(desc);
  695. irqreturn_t res;
  696. __kstat_incr_irqs_this_cpu(desc);
  697. trace_irq_handler_entry(irq, action);
  698. /*
  699. * NMIs cannot be shared, there is only one action.
  700. */
  701. res = action->handler(irq, action->dev_id);
  702. trace_irq_handler_exit(irq, action, res);
  703. if (chip->irq_eoi)
  704. chip->irq_eoi(&desc->irq_data);
  705. }
  706. EXPORT_SYMBOL_GPL(handle_fasteoi_nmi);
  707. /**
  708. * handle_edge_irq - edge type IRQ handler
  709. * @desc: the interrupt description structure for this irq
  710. *
  711. * Interrupt occurs on the falling and/or rising edge of a hardware
  712. * signal. The occurrence is latched into the irq controller hardware and
  713. * must be acked in order to be reenabled. After the ack another interrupt
  714. * can happen on the same source even before the first one is handled by
  715. * the associated event handler. If this happens it might be necessary to
  716. * disable (mask) the interrupt depending on the controller hardware. This
  717. * requires to reenable the interrupt inside of the loop which handles the
  718. * interrupts which have arrived while the handler was running. If all
  719. * pending interrupts are handled, the loop is left.
  720. */
  721. void handle_edge_irq(struct irq_desc *desc)
  722. {
  723. guard(raw_spinlock)(&desc->lock);
  724. if (!irq_can_handle(desc)) {
  725. desc->istate |= IRQS_PENDING;
  726. mask_ack_irq(desc);
  727. return;
  728. }
  729. kstat_incr_irqs_this_cpu(desc);
  730. /* Start handling the irq */
  731. desc->irq_data.chip->irq_ack(&desc->irq_data);
  732. do {
  733. if (unlikely(!desc->action)) {
  734. mask_irq(desc);
  735. return;
  736. }
  737. /*
  738. * When another irq arrived while we were handling
  739. * one, we could have masked the irq.
  740. * Reenable it, if it was not disabled in meantime.
  741. */
  742. if (unlikely(desc->istate & IRQS_PENDING)) {
  743. if (!irqd_irq_disabled(&desc->irq_data) &&
  744. irqd_irq_masked(&desc->irq_data))
  745. unmask_irq(desc);
  746. }
  747. handle_irq_event(desc);
  748. } while ((desc->istate & IRQS_PENDING) && !irqd_irq_disabled(&desc->irq_data));
  749. }
  750. EXPORT_SYMBOL(handle_edge_irq);
  751. /**
  752. * handle_percpu_irq - Per CPU local irq handler
  753. * @desc: the interrupt description structure for this irq
  754. *
  755. * Per CPU interrupts on SMP machines without locking requirements
  756. */
  757. void handle_percpu_irq(struct irq_desc *desc)
  758. {
  759. struct irq_chip *chip = irq_desc_get_chip(desc);
  760. /*
  761. * PER CPU interrupts are not serialized. Do not touch
  762. * desc->tot_count.
  763. */
  764. __kstat_incr_irqs_this_cpu(desc);
  765. if (chip->irq_ack)
  766. chip->irq_ack(&desc->irq_data);
  767. handle_irq_event_percpu(desc);
  768. if (chip->irq_eoi)
  769. chip->irq_eoi(&desc->irq_data);
  770. }
  771. /**
  772. * handle_percpu_devid_irq - Per CPU local irq handler with per cpu dev ids
  773. * @desc: the interrupt description structure for this irq
  774. *
  775. * Per CPU interrupts on SMP machines without locking requirements. Same as
  776. * handle_percpu_irq() above but with the following extras:
  777. *
  778. * action->percpu_dev_id is a pointer to percpu variables which
  779. * contain the real device id for the cpu on which this handler is
  780. * called
  781. */
  782. void handle_percpu_devid_irq(struct irq_desc *desc)
  783. {
  784. struct irq_chip *chip = irq_desc_get_chip(desc);
  785. unsigned int irq = irq_desc_get_irq(desc);
  786. unsigned int cpu = smp_processor_id();
  787. struct irqaction *action;
  788. irqreturn_t res;
  789. /*
  790. * PER CPU interrupts are not serialized. Do not touch
  791. * desc->tot_count.
  792. */
  793. __kstat_incr_irqs_this_cpu(desc);
  794. if (chip->irq_ack)
  795. chip->irq_ack(&desc->irq_data);
  796. for (action = desc->action; action; action = action->next)
  797. if (cpumask_test_cpu(cpu, action->affinity))
  798. break;
  799. if (likely(action)) {
  800. trace_irq_handler_entry(irq, action);
  801. res = action->handler(irq, raw_cpu_ptr(action->percpu_dev_id));
  802. trace_irq_handler_exit(irq, action, res);
  803. } else {
  804. bool enabled = cpumask_test_cpu(cpu, desc->percpu_enabled);
  805. if (enabled)
  806. irq_percpu_disable(desc, cpu);
  807. pr_err_once("Spurious%s percpu IRQ%u on CPU%u\n",
  808. enabled ? " and unmasked" : "", irq, cpu);
  809. }
  810. if (chip->irq_eoi)
  811. chip->irq_eoi(&desc->irq_data);
  812. }
  813. static void
  814. __irq_do_set_handler(struct irq_desc *desc, irq_flow_handler_t handle,
  815. int is_chained, const char *name)
  816. {
  817. if (!handle) {
  818. handle = handle_bad_irq;
  819. } else {
  820. struct irq_data *irq_data = &desc->irq_data;
  821. #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
  822. /*
  823. * With hierarchical domains we might run into a
  824. * situation where the outermost chip is not yet set
  825. * up, but the inner chips are there. Instead of
  826. * bailing we install the handler, but obviously we
  827. * cannot enable/startup the interrupt at this point.
  828. */
  829. while (irq_data) {
  830. if (irq_data->chip != &no_irq_chip)
  831. break;
  832. /*
  833. * Bail out if the outer chip is not set up
  834. * and the interrupt supposed to be started
  835. * right away.
  836. */
  837. if (WARN_ON(is_chained))
  838. return;
  839. /* Try the parent */
  840. irq_data = irq_data->parent_data;
  841. }
  842. #endif
  843. if (WARN_ON(!irq_data || irq_data->chip == &no_irq_chip))
  844. return;
  845. }
  846. /* Uninstall? */
  847. if (handle == handle_bad_irq) {
  848. if (desc->irq_data.chip != &no_irq_chip)
  849. mask_ack_irq(desc);
  850. irq_state_set_disabled(desc);
  851. if (is_chained) {
  852. desc->action = NULL;
  853. irq_chip_pm_put(irq_desc_get_irq_data(desc));
  854. }
  855. desc->depth = 1;
  856. }
  857. desc->handle_irq = handle;
  858. desc->name = name;
  859. if (handle != handle_bad_irq && is_chained) {
  860. unsigned int type = irqd_get_trigger_type(&desc->irq_data);
  861. /*
  862. * We're about to start this interrupt immediately,
  863. * hence the need to set the trigger configuration.
  864. * But the .set_type callback may have overridden the
  865. * flow handler, ignoring that we're dealing with a
  866. * chained interrupt. Reset it immediately because we
  867. * do know better.
  868. */
  869. if (type != IRQ_TYPE_NONE) {
  870. __irq_set_trigger(desc, type);
  871. desc->handle_irq = handle;
  872. }
  873. irq_settings_set_noprobe(desc);
  874. irq_settings_set_norequest(desc);
  875. irq_settings_set_nothread(desc);
  876. desc->action = &chained_action;
  877. WARN_ON(irq_chip_pm_get(irq_desc_get_irq_data(desc)));
  878. irq_activate_and_startup(desc, IRQ_RESEND);
  879. }
  880. }
  881. void __irq_set_handler(unsigned int irq, irq_flow_handler_t handle, int is_chained,
  882. const char *name)
  883. {
  884. scoped_irqdesc_get_and_buslock(irq, 0)
  885. __irq_do_set_handler(scoped_irqdesc, handle, is_chained, name);
  886. }
  887. EXPORT_SYMBOL_GPL(__irq_set_handler);
  888. void irq_set_chained_handler_and_data(unsigned int irq, irq_flow_handler_t handle,
  889. void *data)
  890. {
  891. scoped_irqdesc_get_and_buslock(irq, 0) {
  892. struct irq_desc *desc = scoped_irqdesc;
  893. desc->irq_common_data.handler_data = data;
  894. __irq_do_set_handler(desc, handle, 1, NULL);
  895. }
  896. }
  897. EXPORT_SYMBOL_GPL(irq_set_chained_handler_and_data);
  898. void
  899. irq_set_chip_and_handler_name(unsigned int irq, const struct irq_chip *chip,
  900. irq_flow_handler_t handle, const char *name)
  901. {
  902. irq_set_chip(irq, chip);
  903. __irq_set_handler(irq, handle, 0, name);
  904. }
  905. EXPORT_SYMBOL_GPL(irq_set_chip_and_handler_name);
  906. void irq_modify_status(unsigned int irq, unsigned long clr, unsigned long set)
  907. {
  908. scoped_irqdesc_get_and_lock(irq, 0) {
  909. struct irq_desc *desc = scoped_irqdesc;
  910. unsigned long trigger, tmp;
  911. /*
  912. * Warn when a driver sets the no autoenable flag on an already
  913. * active interrupt.
  914. */
  915. WARN_ON_ONCE(!desc->depth && (set & _IRQ_NOAUTOEN));
  916. irq_settings_clr_and_set(desc, clr, set);
  917. trigger = irqd_get_trigger_type(&desc->irq_data);
  918. irqd_clear(&desc->irq_data, IRQD_NO_BALANCING | IRQD_PER_CPU |
  919. IRQD_TRIGGER_MASK | IRQD_LEVEL);
  920. if (irq_settings_has_no_balance_set(desc))
  921. irqd_set(&desc->irq_data, IRQD_NO_BALANCING);
  922. if (irq_settings_is_per_cpu(desc))
  923. irqd_set(&desc->irq_data, IRQD_PER_CPU);
  924. if (irq_settings_is_level(desc))
  925. irqd_set(&desc->irq_data, IRQD_LEVEL);
  926. tmp = irq_settings_get_trigger_mask(desc);
  927. if (tmp != IRQ_TYPE_NONE)
  928. trigger = tmp;
  929. irqd_set(&desc->irq_data, trigger);
  930. }
  931. }
  932. EXPORT_SYMBOL_GPL(irq_modify_status);
  933. #ifdef CONFIG_DEPRECATED_IRQ_CPU_ONOFFLINE
  934. /**
  935. * irq_cpu_online - Invoke all irq_cpu_online functions.
  936. *
  937. * Iterate through all irqs and invoke the chip.irq_cpu_online()
  938. * for each.
  939. */
  940. void irq_cpu_online(void)
  941. {
  942. unsigned int irq;
  943. for_each_active_irq(irq) {
  944. struct irq_desc *desc = irq_to_desc(irq);
  945. struct irq_chip *chip;
  946. if (!desc)
  947. continue;
  948. guard(raw_spinlock_irqsave)(&desc->lock);
  949. chip = irq_data_get_irq_chip(&desc->irq_data);
  950. if (chip && chip->irq_cpu_online &&
  951. (!(chip->flags & IRQCHIP_ONOFFLINE_ENABLED) ||
  952. !irqd_irq_disabled(&desc->irq_data)))
  953. chip->irq_cpu_online(&desc->irq_data);
  954. }
  955. }
  956. /**
  957. * irq_cpu_offline - Invoke all irq_cpu_offline functions.
  958. *
  959. * Iterate through all irqs and invoke the chip.irq_cpu_offline()
  960. * for each.
  961. */
  962. void irq_cpu_offline(void)
  963. {
  964. unsigned int irq;
  965. for_each_active_irq(irq) {
  966. struct irq_desc *desc = irq_to_desc(irq);
  967. struct irq_chip *chip;
  968. if (!desc)
  969. continue;
  970. guard(raw_spinlock_irqsave)(&desc->lock);
  971. chip = irq_data_get_irq_chip(&desc->irq_data);
  972. if (chip && chip->irq_cpu_offline &&
  973. (!(chip->flags & IRQCHIP_ONOFFLINE_ENABLED) ||
  974. !irqd_irq_disabled(&desc->irq_data)))
  975. chip->irq_cpu_offline(&desc->irq_data);
  976. }
  977. }
  978. #endif
  979. #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
  980. #ifdef CONFIG_IRQ_FASTEOI_HIERARCHY_HANDLERS
  981. /**
  982. * handle_fasteoi_ack_irq - irq handler for edge hierarchy stacked on
  983. * transparent controllers
  984. *
  985. * @desc: the interrupt description structure for this irq
  986. *
  987. * Like handle_fasteoi_irq(), but for use with hierarchy where the irq_chip
  988. * also needs to have its ->irq_ack() function called.
  989. */
  990. void handle_fasteoi_ack_irq(struct irq_desc *desc)
  991. {
  992. struct irq_chip *chip = desc->irq_data.chip;
  993. guard(raw_spinlock)(&desc->lock);
  994. if (!irq_can_handle_pm(desc)) {
  995. cond_eoi_irq(chip, &desc->irq_data);
  996. return;
  997. }
  998. if (unlikely(!irq_can_handle_actions(desc))) {
  999. mask_irq(desc);
  1000. cond_eoi_irq(chip, &desc->irq_data);
  1001. return;
  1002. }
  1003. kstat_incr_irqs_this_cpu(desc);
  1004. if (desc->istate & IRQS_ONESHOT)
  1005. mask_irq(desc);
  1006. desc->irq_data.chip->irq_ack(&desc->irq_data);
  1007. handle_irq_event(desc);
  1008. cond_unmask_eoi_irq(desc, chip);
  1009. }
  1010. EXPORT_SYMBOL_GPL(handle_fasteoi_ack_irq);
  1011. /**
  1012. * handle_fasteoi_mask_irq - irq handler for level hierarchy stacked on
  1013. * transparent controllers
  1014. *
  1015. * @desc: the interrupt description structure for this irq
  1016. *
  1017. * Like handle_fasteoi_irq(), but for use with hierarchy where the irq_chip
  1018. * also needs to have its ->irq_mask_ack() function called.
  1019. */
  1020. void handle_fasteoi_mask_irq(struct irq_desc *desc)
  1021. {
  1022. struct irq_chip *chip = desc->irq_data.chip;
  1023. guard(raw_spinlock)(&desc->lock);
  1024. mask_ack_irq(desc);
  1025. if (!irq_can_handle(desc)) {
  1026. cond_eoi_irq(chip, &desc->irq_data);
  1027. return;
  1028. }
  1029. kstat_incr_irqs_this_cpu(desc);
  1030. handle_irq_event(desc);
  1031. cond_unmask_eoi_irq(desc, chip);
  1032. }
  1033. EXPORT_SYMBOL_GPL(handle_fasteoi_mask_irq);
  1034. #endif /* CONFIG_IRQ_FASTEOI_HIERARCHY_HANDLERS */
  1035. #ifdef CONFIG_SMP
  1036. void irq_chip_pre_redirect_parent(struct irq_data *data)
  1037. {
  1038. data = data->parent_data;
  1039. data->chip->irq_pre_redirect(data);
  1040. }
  1041. EXPORT_SYMBOL_GPL(irq_chip_pre_redirect_parent);
  1042. #endif
  1043. /**
  1044. * irq_chip_set_parent_state - set the state of a parent interrupt.
  1045. *
  1046. * @data: Pointer to interrupt specific data
  1047. * @which: State to be restored (one of IRQCHIP_STATE_*)
  1048. * @val: Value corresponding to @which
  1049. *
  1050. * Conditional success, if the underlying irqchip does not implement it.
  1051. */
  1052. int irq_chip_set_parent_state(struct irq_data *data,
  1053. enum irqchip_irq_state which,
  1054. bool val)
  1055. {
  1056. data = data->parent_data;
  1057. if (!data || !data->chip->irq_set_irqchip_state)
  1058. return 0;
  1059. return data->chip->irq_set_irqchip_state(data, which, val);
  1060. }
  1061. EXPORT_SYMBOL_GPL(irq_chip_set_parent_state);
  1062. /**
  1063. * irq_chip_get_parent_state - get the state of a parent interrupt.
  1064. *
  1065. * @data: Pointer to interrupt specific data
  1066. * @which: one of IRQCHIP_STATE_* the caller wants to know
  1067. * @state: a pointer to a boolean where the state is to be stored
  1068. *
  1069. * Conditional success, if the underlying irqchip does not implement it.
  1070. */
  1071. int irq_chip_get_parent_state(struct irq_data *data,
  1072. enum irqchip_irq_state which,
  1073. bool *state)
  1074. {
  1075. data = data->parent_data;
  1076. if (!data || !data->chip->irq_get_irqchip_state)
  1077. return 0;
  1078. return data->chip->irq_get_irqchip_state(data, which, state);
  1079. }
  1080. EXPORT_SYMBOL_GPL(irq_chip_get_parent_state);
  1081. /**
  1082. * irq_chip_shutdown_parent - Shutdown the parent interrupt
  1083. * @data: Pointer to interrupt specific data
  1084. *
  1085. * Invokes the irq_shutdown() callback of the parent if available or falls
  1086. * back to irq_chip_disable_parent().
  1087. */
  1088. void irq_chip_shutdown_parent(struct irq_data *data)
  1089. {
  1090. struct irq_data *parent = data->parent_data;
  1091. if (parent->chip->irq_shutdown)
  1092. parent->chip->irq_shutdown(parent);
  1093. else
  1094. irq_chip_disable_parent(data);
  1095. }
  1096. EXPORT_SYMBOL_GPL(irq_chip_shutdown_parent);
  1097. /**
  1098. * irq_chip_startup_parent - Startup the parent interrupt
  1099. * @data: Pointer to interrupt specific data
  1100. *
  1101. * Invokes the irq_startup() callback of the parent if available or falls
  1102. * back to irq_chip_enable_parent().
  1103. */
  1104. unsigned int irq_chip_startup_parent(struct irq_data *data)
  1105. {
  1106. struct irq_data *parent = data->parent_data;
  1107. if (parent->chip->irq_startup)
  1108. return parent->chip->irq_startup(parent);
  1109. irq_chip_enable_parent(data);
  1110. return 0;
  1111. }
  1112. EXPORT_SYMBOL_GPL(irq_chip_startup_parent);
  1113. /**
  1114. * irq_chip_enable_parent - Enable the parent interrupt (defaults to unmask if
  1115. * NULL)
  1116. * @data: Pointer to interrupt specific data
  1117. */
  1118. void irq_chip_enable_parent(struct irq_data *data)
  1119. {
  1120. data = data->parent_data;
  1121. if (data->chip->irq_enable)
  1122. data->chip->irq_enable(data);
  1123. else
  1124. data->chip->irq_unmask(data);
  1125. }
  1126. EXPORT_SYMBOL_GPL(irq_chip_enable_parent);
  1127. /**
  1128. * irq_chip_disable_parent - Disable the parent interrupt (defaults to mask if
  1129. * NULL)
  1130. * @data: Pointer to interrupt specific data
  1131. */
  1132. void irq_chip_disable_parent(struct irq_data *data)
  1133. {
  1134. data = data->parent_data;
  1135. if (data->chip->irq_disable)
  1136. data->chip->irq_disable(data);
  1137. else
  1138. data->chip->irq_mask(data);
  1139. }
  1140. EXPORT_SYMBOL_GPL(irq_chip_disable_parent);
  1141. /**
  1142. * irq_chip_ack_parent - Acknowledge the parent interrupt
  1143. * @data: Pointer to interrupt specific data
  1144. */
  1145. void irq_chip_ack_parent(struct irq_data *data)
  1146. {
  1147. data = data->parent_data;
  1148. data->chip->irq_ack(data);
  1149. }
  1150. EXPORT_SYMBOL_GPL(irq_chip_ack_parent);
  1151. /**
  1152. * irq_chip_mask_parent - Mask the parent interrupt
  1153. * @data: Pointer to interrupt specific data
  1154. */
  1155. void irq_chip_mask_parent(struct irq_data *data)
  1156. {
  1157. data = data->parent_data;
  1158. data->chip->irq_mask(data);
  1159. }
  1160. EXPORT_SYMBOL_GPL(irq_chip_mask_parent);
  1161. /**
  1162. * irq_chip_mask_ack_parent - Mask and acknowledge the parent interrupt
  1163. * @data: Pointer to interrupt specific data
  1164. */
  1165. void irq_chip_mask_ack_parent(struct irq_data *data)
  1166. {
  1167. data = data->parent_data;
  1168. data->chip->irq_mask_ack(data);
  1169. }
  1170. EXPORT_SYMBOL_GPL(irq_chip_mask_ack_parent);
  1171. /**
  1172. * irq_chip_unmask_parent - Unmask the parent interrupt
  1173. * @data: Pointer to interrupt specific data
  1174. */
  1175. void irq_chip_unmask_parent(struct irq_data *data)
  1176. {
  1177. data = data->parent_data;
  1178. data->chip->irq_unmask(data);
  1179. }
  1180. EXPORT_SYMBOL_GPL(irq_chip_unmask_parent);
  1181. /**
  1182. * irq_chip_eoi_parent - Invoke EOI on the parent interrupt
  1183. * @data: Pointer to interrupt specific data
  1184. */
  1185. void irq_chip_eoi_parent(struct irq_data *data)
  1186. {
  1187. data = data->parent_data;
  1188. data->chip->irq_eoi(data);
  1189. }
  1190. EXPORT_SYMBOL_GPL(irq_chip_eoi_parent);
  1191. /**
  1192. * irq_chip_set_affinity_parent - Set affinity on the parent interrupt
  1193. * @data: Pointer to interrupt specific data
  1194. * @dest: The affinity mask to set
  1195. * @force: Flag to enforce setting (disable online checks)
  1196. *
  1197. * Conditional, as the underlying parent chip might not implement it.
  1198. */
  1199. int irq_chip_set_affinity_parent(struct irq_data *data,
  1200. const struct cpumask *dest, bool force)
  1201. {
  1202. data = data->parent_data;
  1203. if (data->chip->irq_set_affinity)
  1204. return data->chip->irq_set_affinity(data, dest, force);
  1205. return -ENOSYS;
  1206. }
  1207. EXPORT_SYMBOL_GPL(irq_chip_set_affinity_parent);
  1208. /**
  1209. * irq_chip_set_type_parent - Set IRQ type on the parent interrupt
  1210. * @data: Pointer to interrupt specific data
  1211. * @type: IRQ_TYPE_{LEVEL,EDGE}_* value - see include/linux/irq.h
  1212. *
  1213. * Conditional, as the underlying parent chip might not implement it.
  1214. */
  1215. int irq_chip_set_type_parent(struct irq_data *data, unsigned int type)
  1216. {
  1217. data = data->parent_data;
  1218. if (data->chip->irq_set_type)
  1219. return data->chip->irq_set_type(data, type);
  1220. return -ENOSYS;
  1221. }
  1222. EXPORT_SYMBOL_GPL(irq_chip_set_type_parent);
  1223. /**
  1224. * irq_chip_retrigger_hierarchy - Retrigger an interrupt in hardware
  1225. * @data: Pointer to interrupt specific data
  1226. *
  1227. * Iterate through the domain hierarchy of the interrupt and check
  1228. * whether a hw retrigger function exists. If yes, invoke it.
  1229. */
  1230. int irq_chip_retrigger_hierarchy(struct irq_data *data)
  1231. {
  1232. for (data = data->parent_data; data; data = data->parent_data)
  1233. if (data->chip && data->chip->irq_retrigger)
  1234. return data->chip->irq_retrigger(data);
  1235. return 0;
  1236. }
  1237. EXPORT_SYMBOL_GPL(irq_chip_retrigger_hierarchy);
  1238. /**
  1239. * irq_chip_set_vcpu_affinity_parent - Set vcpu affinity on the parent interrupt
  1240. * @data: Pointer to interrupt specific data
  1241. * @vcpu_info: The vcpu affinity information
  1242. */
  1243. int irq_chip_set_vcpu_affinity_parent(struct irq_data *data, void *vcpu_info)
  1244. {
  1245. data = data->parent_data;
  1246. if (data->chip->irq_set_vcpu_affinity)
  1247. return data->chip->irq_set_vcpu_affinity(data, vcpu_info);
  1248. return -ENOSYS;
  1249. }
  1250. EXPORT_SYMBOL_GPL(irq_chip_set_vcpu_affinity_parent);
  1251. /**
  1252. * irq_chip_set_wake_parent - Set/reset wake-up on the parent interrupt
  1253. * @data: Pointer to interrupt specific data
  1254. * @on: Whether to set or reset the wake-up capability of this irq
  1255. *
  1256. * Conditional, as the underlying parent chip might not implement it.
  1257. */
  1258. int irq_chip_set_wake_parent(struct irq_data *data, unsigned int on)
  1259. {
  1260. data = data->parent_data;
  1261. if (data->chip->flags & IRQCHIP_SKIP_SET_WAKE)
  1262. return 0;
  1263. if (data->chip->irq_set_wake)
  1264. return data->chip->irq_set_wake(data, on);
  1265. return -ENOSYS;
  1266. }
  1267. EXPORT_SYMBOL_GPL(irq_chip_set_wake_parent);
  1268. /**
  1269. * irq_chip_request_resources_parent - Request resources on the parent interrupt
  1270. * @data: Pointer to interrupt specific data
  1271. */
  1272. int irq_chip_request_resources_parent(struct irq_data *data)
  1273. {
  1274. data = data->parent_data;
  1275. if (data->chip->irq_request_resources)
  1276. return data->chip->irq_request_resources(data);
  1277. /* no error on missing optional irq_chip::irq_request_resources */
  1278. return 0;
  1279. }
  1280. EXPORT_SYMBOL_GPL(irq_chip_request_resources_parent);
  1281. /**
  1282. * irq_chip_release_resources_parent - Release resources on the parent interrupt
  1283. * @data: Pointer to interrupt specific data
  1284. */
  1285. void irq_chip_release_resources_parent(struct irq_data *data)
  1286. {
  1287. data = data->parent_data;
  1288. if (data->chip->irq_release_resources)
  1289. data->chip->irq_release_resources(data);
  1290. }
  1291. EXPORT_SYMBOL_GPL(irq_chip_release_resources_parent);
  1292. #endif /* CONFIG_IRQ_DOMAIN_HIERARCHY */
  1293. #ifdef CONFIG_SMP
  1294. int irq_chip_redirect_set_affinity(struct irq_data *data, const struct cpumask *dest, bool force)
  1295. {
  1296. struct irq_redirect *redir = &irq_data_to_desc(data)->redirect;
  1297. WRITE_ONCE(redir->target_cpu, cpumask_first(dest));
  1298. irq_data_update_effective_affinity(data, dest);
  1299. return IRQ_SET_MASK_OK_DONE;
  1300. }
  1301. EXPORT_SYMBOL_GPL(irq_chip_redirect_set_affinity);
  1302. #endif
  1303. /**
  1304. * irq_chip_compose_msi_msg - Compose msi message for a irq chip
  1305. * @data: Pointer to interrupt specific data
  1306. * @msg: Pointer to the MSI message
  1307. *
  1308. * For hierarchical domains we find the first chip in the hierarchy
  1309. * which implements the irq_compose_msi_msg callback. For non
  1310. * hierarchical we use the top level chip.
  1311. */
  1312. int irq_chip_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
  1313. {
  1314. struct irq_data *pos;
  1315. for (pos = NULL; !pos && data; data = irqd_get_parent_data(data)) {
  1316. if (data->chip && data->chip->irq_compose_msi_msg)
  1317. pos = data;
  1318. }
  1319. if (!pos)
  1320. return -ENOSYS;
  1321. pos->chip->irq_compose_msi_msg(pos, msg);
  1322. return 0;
  1323. }
  1324. static struct device *irq_get_pm_device(struct irq_data *data)
  1325. {
  1326. if (data->domain)
  1327. return data->domain->pm_dev;
  1328. return NULL;
  1329. }
  1330. /**
  1331. * irq_chip_pm_get - Enable power for an IRQ chip
  1332. * @data: Pointer to interrupt specific data
  1333. *
  1334. * Enable the power to the IRQ chip referenced by the interrupt data
  1335. * structure.
  1336. */
  1337. int irq_chip_pm_get(struct irq_data *data)
  1338. {
  1339. struct device *dev = irq_get_pm_device(data);
  1340. int retval = 0;
  1341. if (IS_ENABLED(CONFIG_PM) && dev)
  1342. retval = pm_runtime_resume_and_get(dev);
  1343. return retval;
  1344. }
  1345. /**
  1346. * irq_chip_pm_put - Drop a PM reference on an IRQ chip
  1347. * @data: Pointer to interrupt specific data
  1348. *
  1349. * Drop a power management reference, acquired via irq_chip_pm_get(), on the IRQ
  1350. * chip represented by the interrupt data structure.
  1351. *
  1352. * Note that this will not disable power to the IRQ chip until this function
  1353. * has been called for all IRQs that have called irq_chip_pm_get() and it may
  1354. * not disable power at all (if user space prevents that, for example).
  1355. */
  1356. void irq_chip_pm_put(struct irq_data *data)
  1357. {
  1358. struct device *dev = irq_get_pm_device(data);
  1359. if (dev)
  1360. pm_runtime_put(dev);
  1361. }