chip.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064
  1. /*
  2. * linux/kernel/irq/chip.c
  3. *
  4. * Copyright (C) 1992, 1998-2006 Linus Torvalds, Ingo Molnar
  5. * Copyright (C) 2005-2006, Thomas Gleixner, Russell King
  6. *
  7. * This file contains the core interrupt handling code, for irq-chip
  8. * based architectures.
  9. *
  10. * Detailed information is available in Documentation/DocBook/genericirq
  11. */
  12. #include <linux/irq.h>
  13. #include <linux/msi.h>
  14. #include <linux/module.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/kernel_stat.h>
  17. #include <linux/irqdomain.h>
  18. #include <trace/events/irq.h>
  19. #include "internals.h"
  20. /**
  21. * irq_set_chip - set the irq chip for an irq
  22. * @irq: irq number
  23. * @chip: pointer to irq chip description structure
  24. */
  25. int irq_set_chip(unsigned int irq, struct irq_chip *chip)
  26. {
  27. unsigned long flags;
  28. struct irq_desc *desc = irq_get_desc_lock(irq, &flags, 0);
  29. if (!desc)
  30. return -EINVAL;
  31. if (!chip)
  32. chip = &no_irq_chip;
  33. desc->irq_data.chip = chip;
  34. irq_put_desc_unlock(desc, flags);
  35. /*
  36. * For !CONFIG_SPARSE_IRQ make the irq show up in
  37. * allocated_irqs.
  38. */
  39. irq_mark_irq(irq);
  40. return 0;
  41. }
  42. EXPORT_SYMBOL(irq_set_chip);
  43. /**
  44. * irq_set_type - set the irq trigger type for an irq
  45. * @irq: irq number
  46. * @type: IRQ_TYPE_{LEVEL,EDGE}_* value - see include/linux/irq.h
  47. */
  48. int irq_set_irq_type(unsigned int irq, unsigned int type)
  49. {
  50. unsigned long flags;
  51. struct irq_desc *desc = irq_get_desc_buslock(irq, &flags, IRQ_GET_DESC_CHECK_GLOBAL);
  52. int ret = 0;
  53. if (!desc)
  54. return -EINVAL;
  55. type &= IRQ_TYPE_SENSE_MASK;
  56. ret = __irq_set_trigger(desc, type);
  57. irq_put_desc_busunlock(desc, flags);
  58. return ret;
  59. }
  60. EXPORT_SYMBOL(irq_set_irq_type);
  61. /**
  62. * irq_set_handler_data - set irq handler data for an irq
  63. * @irq: Interrupt number
  64. * @data: Pointer to interrupt specific data
  65. *
  66. * Set the hardware irq controller data for an irq
  67. */
  68. int irq_set_handler_data(unsigned int irq, void *data)
  69. {
  70. unsigned long flags;
  71. struct irq_desc *desc = irq_get_desc_lock(irq, &flags, 0);
  72. if (!desc)
  73. return -EINVAL;
  74. desc->irq_common_data.handler_data = data;
  75. irq_put_desc_unlock(desc, flags);
  76. return 0;
  77. }
  78. EXPORT_SYMBOL(irq_set_handler_data);
  79. /**
  80. * irq_set_msi_desc_off - set MSI descriptor data for an irq at offset
  81. * @irq_base: Interrupt number base
  82. * @irq_offset: Interrupt number offset
  83. * @entry: Pointer to MSI descriptor data
  84. *
  85. * Set the MSI descriptor entry for an irq at offset
  86. */
  87. int irq_set_msi_desc_off(unsigned int irq_base, unsigned int irq_offset,
  88. struct msi_desc *entry)
  89. {
  90. unsigned long flags;
  91. struct irq_desc *desc = irq_get_desc_lock(irq_base + irq_offset, &flags, IRQ_GET_DESC_CHECK_GLOBAL);
  92. if (!desc)
  93. return -EINVAL;
  94. desc->irq_common_data.msi_desc = entry;
  95. if (entry && !irq_offset)
  96. entry->irq = irq_base;
  97. irq_put_desc_unlock(desc, flags);
  98. return 0;
  99. }
  100. /**
  101. * irq_set_msi_desc - set MSI descriptor data for an irq
  102. * @irq: Interrupt number
  103. * @entry: Pointer to MSI descriptor data
  104. *
  105. * Set the MSI descriptor entry for an irq
  106. */
  107. int irq_set_msi_desc(unsigned int irq, struct msi_desc *entry)
  108. {
  109. return irq_set_msi_desc_off(irq, 0, entry);
  110. }
  111. /**
  112. * irq_set_chip_data - set irq chip data for an irq
  113. * @irq: Interrupt number
  114. * @data: Pointer to chip specific data
  115. *
  116. * Set the hardware irq chip data for an irq
  117. */
  118. int irq_set_chip_data(unsigned int irq, void *data)
  119. {
  120. unsigned long flags;
  121. struct irq_desc *desc = irq_get_desc_lock(irq, &flags, 0);
  122. if (!desc)
  123. return -EINVAL;
  124. desc->irq_data.chip_data = data;
  125. irq_put_desc_unlock(desc, flags);
  126. return 0;
  127. }
  128. EXPORT_SYMBOL(irq_set_chip_data);
  129. struct irq_data *irq_get_irq_data(unsigned int irq)
  130. {
  131. struct irq_desc *desc = irq_to_desc(irq);
  132. return desc ? &desc->irq_data : NULL;
  133. }
  134. EXPORT_SYMBOL_GPL(irq_get_irq_data);
  135. static void irq_state_clr_disabled(struct irq_desc *desc)
  136. {
  137. irqd_clear(&desc->irq_data, IRQD_IRQ_DISABLED);
  138. }
  139. static void irq_state_set_disabled(struct irq_desc *desc)
  140. {
  141. irqd_set(&desc->irq_data, IRQD_IRQ_DISABLED);
  142. }
  143. static void irq_state_clr_masked(struct irq_desc *desc)
  144. {
  145. irqd_clear(&desc->irq_data, IRQD_IRQ_MASKED);
  146. }
  147. static void irq_state_set_masked(struct irq_desc *desc)
  148. {
  149. irqd_set(&desc->irq_data, IRQD_IRQ_MASKED);
  150. }
  151. int irq_startup(struct irq_desc *desc, bool resend)
  152. {
  153. int ret = 0;
  154. irq_state_clr_disabled(desc);
  155. desc->depth = 0;
  156. irq_domain_activate_irq(&desc->irq_data);
  157. if (desc->irq_data.chip->irq_startup) {
  158. ret = desc->irq_data.chip->irq_startup(&desc->irq_data);
  159. irq_state_clr_masked(desc);
  160. } else {
  161. irq_enable(desc);
  162. }
  163. if (resend)
  164. check_irq_resend(desc);
  165. return ret;
  166. }
  167. void irq_shutdown(struct irq_desc *desc)
  168. {
  169. irq_state_set_disabled(desc);
  170. desc->depth = 1;
  171. if (desc->irq_data.chip->irq_shutdown)
  172. desc->irq_data.chip->irq_shutdown(&desc->irq_data);
  173. else if (desc->irq_data.chip->irq_disable)
  174. desc->irq_data.chip->irq_disable(&desc->irq_data);
  175. else
  176. desc->irq_data.chip->irq_mask(&desc->irq_data);
  177. irq_domain_deactivate_irq(&desc->irq_data);
  178. irq_state_set_masked(desc);
  179. }
  180. void irq_enable(struct irq_desc *desc)
  181. {
  182. irq_state_clr_disabled(desc);
  183. if (desc->irq_data.chip->irq_enable)
  184. desc->irq_data.chip->irq_enable(&desc->irq_data);
  185. else
  186. desc->irq_data.chip->irq_unmask(&desc->irq_data);
  187. irq_state_clr_masked(desc);
  188. }
  189. /**
  190. * irq_disable - Mark interrupt disabled
  191. * @desc: irq descriptor which should be disabled
  192. *
  193. * If the chip does not implement the irq_disable callback, we
  194. * use a lazy disable approach. That means we mark the interrupt
  195. * disabled, but leave the hardware unmasked. That's an
  196. * optimization because we avoid the hardware access for the
  197. * common case where no interrupt happens after we marked it
  198. * disabled. If an interrupt happens, then the interrupt flow
  199. * handler masks the line at the hardware level and marks it
  200. * pending.
  201. */
  202. void irq_disable(struct irq_desc *desc)
  203. {
  204. irq_state_set_disabled(desc);
  205. if (desc->irq_data.chip->irq_disable) {
  206. desc->irq_data.chip->irq_disable(&desc->irq_data);
  207. irq_state_set_masked(desc);
  208. }
  209. }
  210. void irq_percpu_enable(struct irq_desc *desc, unsigned int cpu)
  211. {
  212. if (desc->irq_data.chip->irq_enable)
  213. desc->irq_data.chip->irq_enable(&desc->irq_data);
  214. else
  215. desc->irq_data.chip->irq_unmask(&desc->irq_data);
  216. cpumask_set_cpu(cpu, desc->percpu_enabled);
  217. }
  218. void irq_percpu_disable(struct irq_desc *desc, unsigned int cpu)
  219. {
  220. if (desc->irq_data.chip->irq_disable)
  221. desc->irq_data.chip->irq_disable(&desc->irq_data);
  222. else
  223. desc->irq_data.chip->irq_mask(&desc->irq_data);
  224. cpumask_clear_cpu(cpu, desc->percpu_enabled);
  225. }
  226. static inline void mask_ack_irq(struct irq_desc *desc)
  227. {
  228. if (desc->irq_data.chip->irq_mask_ack)
  229. desc->irq_data.chip->irq_mask_ack(&desc->irq_data);
  230. else {
  231. desc->irq_data.chip->irq_mask(&desc->irq_data);
  232. if (desc->irq_data.chip->irq_ack)
  233. desc->irq_data.chip->irq_ack(&desc->irq_data);
  234. }
  235. irq_state_set_masked(desc);
  236. }
  237. void mask_irq(struct irq_desc *desc)
  238. {
  239. if (desc->irq_data.chip->irq_mask) {
  240. desc->irq_data.chip->irq_mask(&desc->irq_data);
  241. irq_state_set_masked(desc);
  242. }
  243. }
  244. void unmask_irq(struct irq_desc *desc)
  245. {
  246. if (desc->irq_data.chip->irq_unmask) {
  247. desc->irq_data.chip->irq_unmask(&desc->irq_data);
  248. irq_state_clr_masked(desc);
  249. }
  250. }
  251. void unmask_threaded_irq(struct irq_desc *desc)
  252. {
  253. struct irq_chip *chip = desc->irq_data.chip;
  254. if (chip->flags & IRQCHIP_EOI_THREADED)
  255. chip->irq_eoi(&desc->irq_data);
  256. if (chip->irq_unmask) {
  257. chip->irq_unmask(&desc->irq_data);
  258. irq_state_clr_masked(desc);
  259. }
  260. }
  261. /*
  262. * handle_nested_irq - Handle a nested irq from a irq thread
  263. * @irq: the interrupt number
  264. *
  265. * Handle interrupts which are nested into a threaded interrupt
  266. * handler. The handler function is called inside the calling
  267. * threads context.
  268. */
  269. void handle_nested_irq(unsigned int irq)
  270. {
  271. struct irq_desc *desc = irq_to_desc(irq);
  272. struct irqaction *action;
  273. irqreturn_t action_ret;
  274. might_sleep();
  275. raw_spin_lock_irq(&desc->lock);
  276. desc->istate &= ~(IRQS_REPLAY | IRQS_WAITING);
  277. kstat_incr_irqs_this_cpu(desc);
  278. action = desc->action;
  279. if (unlikely(!action || irqd_irq_disabled(&desc->irq_data))) {
  280. desc->istate |= IRQS_PENDING;
  281. goto out_unlock;
  282. }
  283. irqd_set(&desc->irq_data, IRQD_IRQ_INPROGRESS);
  284. raw_spin_unlock_irq(&desc->lock);
  285. action_ret = action->thread_fn(action->irq, action->dev_id);
  286. if (!noirqdebug)
  287. note_interrupt(desc, action_ret);
  288. raw_spin_lock_irq(&desc->lock);
  289. irqd_clear(&desc->irq_data, IRQD_IRQ_INPROGRESS);
  290. out_unlock:
  291. raw_spin_unlock_irq(&desc->lock);
  292. }
  293. EXPORT_SYMBOL_GPL(handle_nested_irq);
  294. static bool irq_check_poll(struct irq_desc *desc)
  295. {
  296. if (!(desc->istate & IRQS_POLL_INPROGRESS))
  297. return false;
  298. return irq_wait_for_poll(desc);
  299. }
  300. static bool irq_may_run(struct irq_desc *desc)
  301. {
  302. unsigned int mask = IRQD_IRQ_INPROGRESS | IRQD_WAKEUP_ARMED;
  303. /*
  304. * If the interrupt is not in progress and is not an armed
  305. * wakeup interrupt, proceed.
  306. */
  307. if (!irqd_has_set(&desc->irq_data, mask))
  308. return true;
  309. /*
  310. * If the interrupt is an armed wakeup source, mark it pending
  311. * and suspended, disable it and notify the pm core about the
  312. * event.
  313. */
  314. if (irq_pm_check_wakeup(desc))
  315. return false;
  316. /*
  317. * Handle a potential concurrent poll on a different core.
  318. */
  319. return irq_check_poll(desc);
  320. }
  321. /**
  322. * handle_simple_irq - Simple and software-decoded IRQs.
  323. * @desc: the interrupt description structure for this irq
  324. *
  325. * Simple interrupts are either sent from a demultiplexing interrupt
  326. * handler or come from hardware, where no interrupt hardware control
  327. * is necessary.
  328. *
  329. * Note: The caller is expected to handle the ack, clear, mask and
  330. * unmask issues if necessary.
  331. */
  332. void handle_simple_irq(struct irq_desc *desc)
  333. {
  334. raw_spin_lock(&desc->lock);
  335. if (!irq_may_run(desc))
  336. goto out_unlock;
  337. desc->istate &= ~(IRQS_REPLAY | IRQS_WAITING);
  338. kstat_incr_irqs_this_cpu(desc);
  339. if (unlikely(!desc->action || irqd_irq_disabled(&desc->irq_data))) {
  340. desc->istate |= IRQS_PENDING;
  341. goto out_unlock;
  342. }
  343. handle_irq_event(desc);
  344. out_unlock:
  345. raw_spin_unlock(&desc->lock);
  346. }
  347. EXPORT_SYMBOL_GPL(handle_simple_irq);
  348. /*
  349. * Called unconditionally from handle_level_irq() and only for oneshot
  350. * interrupts from handle_fasteoi_irq()
  351. */
  352. static void cond_unmask_irq(struct irq_desc *desc)
  353. {
  354. /*
  355. * We need to unmask in the following cases:
  356. * - Standard level irq (IRQF_ONESHOT is not set)
  357. * - Oneshot irq which did not wake the thread (caused by a
  358. * spurious interrupt or a primary handler handling it
  359. * completely).
  360. */
  361. if (!irqd_irq_disabled(&desc->irq_data) &&
  362. irqd_irq_masked(&desc->irq_data) && !desc->threads_oneshot)
  363. unmask_irq(desc);
  364. }
  365. /**
  366. * handle_level_irq - Level type irq handler
  367. * @desc: the interrupt description structure for this irq
  368. *
  369. * Level type interrupts are active as long as the hardware line has
  370. * the active level. This may require to mask the interrupt and unmask
  371. * it after the associated handler has acknowledged the device, so the
  372. * interrupt line is back to inactive.
  373. */
  374. void handle_level_irq(struct irq_desc *desc)
  375. {
  376. raw_spin_lock(&desc->lock);
  377. mask_ack_irq(desc);
  378. if (!irq_may_run(desc))
  379. goto out_unlock;
  380. desc->istate &= ~(IRQS_REPLAY | IRQS_WAITING);
  381. kstat_incr_irqs_this_cpu(desc);
  382. /*
  383. * If its disabled or no action available
  384. * keep it masked and get out of here
  385. */
  386. if (unlikely(!desc->action || irqd_irq_disabled(&desc->irq_data))) {
  387. desc->istate |= IRQS_PENDING;
  388. goto out_unlock;
  389. }
  390. handle_irq_event(desc);
  391. cond_unmask_irq(desc);
  392. out_unlock:
  393. raw_spin_unlock(&desc->lock);
  394. }
  395. EXPORT_SYMBOL_GPL(handle_level_irq);
  396. #ifdef CONFIG_IRQ_PREFLOW_FASTEOI
  397. static inline void preflow_handler(struct irq_desc *desc)
  398. {
  399. if (desc->preflow_handler)
  400. desc->preflow_handler(&desc->irq_data);
  401. }
  402. #else
  403. static inline void preflow_handler(struct irq_desc *desc) { }
  404. #endif
  405. static void cond_unmask_eoi_irq(struct irq_desc *desc, struct irq_chip *chip)
  406. {
  407. if (!(desc->istate & IRQS_ONESHOT)) {
  408. chip->irq_eoi(&desc->irq_data);
  409. return;
  410. }
  411. /*
  412. * We need to unmask in the following cases:
  413. * - Oneshot irq which did not wake the thread (caused by a
  414. * spurious interrupt or a primary handler handling it
  415. * completely).
  416. */
  417. if (!irqd_irq_disabled(&desc->irq_data) &&
  418. irqd_irq_masked(&desc->irq_data) && !desc->threads_oneshot) {
  419. chip->irq_eoi(&desc->irq_data);
  420. unmask_irq(desc);
  421. } else if (!(chip->flags & IRQCHIP_EOI_THREADED)) {
  422. chip->irq_eoi(&desc->irq_data);
  423. }
  424. }
  425. /**
  426. * handle_fasteoi_irq - irq handler for transparent controllers
  427. * @desc: the interrupt description structure for this irq
  428. *
  429. * Only a single callback will be issued to the chip: an ->eoi()
  430. * call when the interrupt has been serviced. This enables support
  431. * for modern forms of interrupt handlers, which handle the flow
  432. * details in hardware, transparently.
  433. */
  434. void handle_fasteoi_irq(struct irq_desc *desc)
  435. {
  436. struct irq_chip *chip = desc->irq_data.chip;
  437. raw_spin_lock(&desc->lock);
  438. if (!irq_may_run(desc))
  439. goto out;
  440. desc->istate &= ~(IRQS_REPLAY | IRQS_WAITING);
  441. kstat_incr_irqs_this_cpu(desc);
  442. /*
  443. * If its disabled or no action available
  444. * then mask it and get out of here:
  445. */
  446. if (unlikely(!desc->action || irqd_irq_disabled(&desc->irq_data))) {
  447. desc->istate |= IRQS_PENDING;
  448. mask_irq(desc);
  449. goto out;
  450. }
  451. if (desc->istate & IRQS_ONESHOT)
  452. mask_irq(desc);
  453. preflow_handler(desc);
  454. handle_irq_event(desc);
  455. cond_unmask_eoi_irq(desc, chip);
  456. raw_spin_unlock(&desc->lock);
  457. return;
  458. out:
  459. if (!(chip->flags & IRQCHIP_EOI_IF_HANDLED))
  460. chip->irq_eoi(&desc->irq_data);
  461. raw_spin_unlock(&desc->lock);
  462. }
  463. EXPORT_SYMBOL_GPL(handle_fasteoi_irq);
  464. /**
  465. * handle_edge_irq - edge type IRQ handler
  466. * @desc: the interrupt description structure for this irq
  467. *
  468. * Interrupt occures on the falling and/or rising edge of a hardware
  469. * signal. The occurrence is latched into the irq controller hardware
  470. * and must be acked in order to be reenabled. After the ack another
  471. * interrupt can happen on the same source even before the first one
  472. * is handled by the associated event handler. If this happens it
  473. * might be necessary to disable (mask) the interrupt depending on the
  474. * controller hardware. This requires to reenable the interrupt inside
  475. * of the loop which handles the interrupts which have arrived while
  476. * the handler was running. If all pending interrupts are handled, the
  477. * loop is left.
  478. */
  479. void handle_edge_irq(struct irq_desc *desc)
  480. {
  481. raw_spin_lock(&desc->lock);
  482. desc->istate &= ~(IRQS_REPLAY | IRQS_WAITING);
  483. if (!irq_may_run(desc)) {
  484. desc->istate |= IRQS_PENDING;
  485. mask_ack_irq(desc);
  486. goto out_unlock;
  487. }
  488. /*
  489. * If its disabled or no action available then mask it and get
  490. * out of here.
  491. */
  492. if (irqd_irq_disabled(&desc->irq_data) || !desc->action) {
  493. desc->istate |= IRQS_PENDING;
  494. mask_ack_irq(desc);
  495. goto out_unlock;
  496. }
  497. kstat_incr_irqs_this_cpu(desc);
  498. /* Start handling the irq */
  499. desc->irq_data.chip->irq_ack(&desc->irq_data);
  500. do {
  501. if (unlikely(!desc->action)) {
  502. mask_irq(desc);
  503. goto out_unlock;
  504. }
  505. /*
  506. * When another irq arrived while we were handling
  507. * one, we could have masked the irq.
  508. * Renable it, if it was not disabled in meantime.
  509. */
  510. if (unlikely(desc->istate & IRQS_PENDING)) {
  511. if (!irqd_irq_disabled(&desc->irq_data) &&
  512. irqd_irq_masked(&desc->irq_data))
  513. unmask_irq(desc);
  514. }
  515. handle_irq_event(desc);
  516. } while ((desc->istate & IRQS_PENDING) &&
  517. !irqd_irq_disabled(&desc->irq_data));
  518. out_unlock:
  519. raw_spin_unlock(&desc->lock);
  520. }
  521. EXPORT_SYMBOL(handle_edge_irq);
  522. #ifdef CONFIG_IRQ_EDGE_EOI_HANDLER
  523. /**
  524. * handle_edge_eoi_irq - edge eoi type IRQ handler
  525. * @desc: the interrupt description structure for this irq
  526. *
  527. * Similar as the above handle_edge_irq, but using eoi and w/o the
  528. * mask/unmask logic.
  529. */
  530. void handle_edge_eoi_irq(struct irq_desc *desc)
  531. {
  532. struct irq_chip *chip = irq_desc_get_chip(desc);
  533. raw_spin_lock(&desc->lock);
  534. desc->istate &= ~(IRQS_REPLAY | IRQS_WAITING);
  535. if (!irq_may_run(desc)) {
  536. desc->istate |= IRQS_PENDING;
  537. goto out_eoi;
  538. }
  539. /*
  540. * If its disabled or no action available then mask it and get
  541. * out of here.
  542. */
  543. if (irqd_irq_disabled(&desc->irq_data) || !desc->action) {
  544. desc->istate |= IRQS_PENDING;
  545. goto out_eoi;
  546. }
  547. kstat_incr_irqs_this_cpu(desc);
  548. do {
  549. if (unlikely(!desc->action))
  550. goto out_eoi;
  551. handle_irq_event(desc);
  552. } while ((desc->istate & IRQS_PENDING) &&
  553. !irqd_irq_disabled(&desc->irq_data));
  554. out_eoi:
  555. chip->irq_eoi(&desc->irq_data);
  556. raw_spin_unlock(&desc->lock);
  557. }
  558. #endif
  559. /**
  560. * handle_percpu_irq - Per CPU local irq handler
  561. * @desc: the interrupt description structure for this irq
  562. *
  563. * Per CPU interrupts on SMP machines without locking requirements
  564. */
  565. void handle_percpu_irq(struct irq_desc *desc)
  566. {
  567. struct irq_chip *chip = irq_desc_get_chip(desc);
  568. kstat_incr_irqs_this_cpu(desc);
  569. if (chip->irq_ack)
  570. chip->irq_ack(&desc->irq_data);
  571. handle_irq_event_percpu(desc, desc->action);
  572. if (chip->irq_eoi)
  573. chip->irq_eoi(&desc->irq_data);
  574. }
  575. /**
  576. * handle_percpu_devid_irq - Per CPU local irq handler with per cpu dev ids
  577. * @desc: the interrupt description structure for this irq
  578. *
  579. * Per CPU interrupts on SMP machines without locking requirements. Same as
  580. * handle_percpu_irq() above but with the following extras:
  581. *
  582. * action->percpu_dev_id is a pointer to percpu variables which
  583. * contain the real device id for the cpu on which this handler is
  584. * called
  585. */
  586. void handle_percpu_devid_irq(struct irq_desc *desc)
  587. {
  588. struct irq_chip *chip = irq_desc_get_chip(desc);
  589. struct irqaction *action = desc->action;
  590. void *dev_id = raw_cpu_ptr(action->percpu_dev_id);
  591. unsigned int irq = irq_desc_get_irq(desc);
  592. irqreturn_t res;
  593. kstat_incr_irqs_this_cpu(desc);
  594. if (chip->irq_ack)
  595. chip->irq_ack(&desc->irq_data);
  596. trace_irq_handler_entry(irq, action);
  597. res = action->handler(irq, dev_id);
  598. trace_irq_handler_exit(irq, action, res);
  599. if (chip->irq_eoi)
  600. chip->irq_eoi(&desc->irq_data);
  601. }
  602. void
  603. __irq_do_set_handler(struct irq_desc *desc, irq_flow_handler_t handle,
  604. int is_chained, const char *name)
  605. {
  606. if (!handle) {
  607. handle = handle_bad_irq;
  608. } else {
  609. struct irq_data *irq_data = &desc->irq_data;
  610. #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
  611. /*
  612. * With hierarchical domains we might run into a
  613. * situation where the outermost chip is not yet set
  614. * up, but the inner chips are there. Instead of
  615. * bailing we install the handler, but obviously we
  616. * cannot enable/startup the interrupt at this point.
  617. */
  618. while (irq_data) {
  619. if (irq_data->chip != &no_irq_chip)
  620. break;
  621. /*
  622. * Bail out if the outer chip is not set up
  623. * and the interrrupt supposed to be started
  624. * right away.
  625. */
  626. if (WARN_ON(is_chained))
  627. return;
  628. /* Try the parent */
  629. irq_data = irq_data->parent_data;
  630. }
  631. #endif
  632. if (WARN_ON(!irq_data || irq_data->chip == &no_irq_chip))
  633. return;
  634. }
  635. /* Uninstall? */
  636. if (handle == handle_bad_irq) {
  637. if (desc->irq_data.chip != &no_irq_chip)
  638. mask_ack_irq(desc);
  639. irq_state_set_disabled(desc);
  640. desc->depth = 1;
  641. }
  642. desc->handle_irq = handle;
  643. desc->name = name;
  644. if (handle != handle_bad_irq && is_chained) {
  645. irq_settings_set_noprobe(desc);
  646. irq_settings_set_norequest(desc);
  647. irq_settings_set_nothread(desc);
  648. irq_startup(desc, true);
  649. }
  650. }
  651. void
  652. __irq_set_handler(unsigned int irq, irq_flow_handler_t handle, int is_chained,
  653. const char *name)
  654. {
  655. unsigned long flags;
  656. struct irq_desc *desc = irq_get_desc_buslock(irq, &flags, 0);
  657. if (!desc)
  658. return;
  659. __irq_do_set_handler(desc, handle, is_chained, name);
  660. irq_put_desc_busunlock(desc, flags);
  661. }
  662. EXPORT_SYMBOL_GPL(__irq_set_handler);
  663. void
  664. irq_set_chained_handler_and_data(unsigned int irq, irq_flow_handler_t handle,
  665. void *data)
  666. {
  667. unsigned long flags;
  668. struct irq_desc *desc = irq_get_desc_buslock(irq, &flags, 0);
  669. if (!desc)
  670. return;
  671. __irq_do_set_handler(desc, handle, 1, NULL);
  672. desc->irq_common_data.handler_data = data;
  673. irq_put_desc_busunlock(desc, flags);
  674. }
  675. EXPORT_SYMBOL_GPL(irq_set_chained_handler_and_data);
  676. void
  677. irq_set_chip_and_handler_name(unsigned int irq, struct irq_chip *chip,
  678. irq_flow_handler_t handle, const char *name)
  679. {
  680. irq_set_chip(irq, chip);
  681. __irq_set_handler(irq, handle, 0, name);
  682. }
  683. EXPORT_SYMBOL_GPL(irq_set_chip_and_handler_name);
  684. void irq_modify_status(unsigned int irq, unsigned long clr, unsigned long set)
  685. {
  686. unsigned long flags;
  687. struct irq_desc *desc = irq_get_desc_lock(irq, &flags, 0);
  688. if (!desc)
  689. return;
  690. irq_settings_clr_and_set(desc, clr, set);
  691. irqd_clear(&desc->irq_data, IRQD_NO_BALANCING | IRQD_PER_CPU |
  692. IRQD_TRIGGER_MASK | IRQD_LEVEL | IRQD_MOVE_PCNTXT);
  693. if (irq_settings_has_no_balance_set(desc))
  694. irqd_set(&desc->irq_data, IRQD_NO_BALANCING);
  695. if (irq_settings_is_per_cpu(desc))
  696. irqd_set(&desc->irq_data, IRQD_PER_CPU);
  697. if (irq_settings_can_move_pcntxt(desc))
  698. irqd_set(&desc->irq_data, IRQD_MOVE_PCNTXT);
  699. if (irq_settings_is_level(desc))
  700. irqd_set(&desc->irq_data, IRQD_LEVEL);
  701. irqd_set(&desc->irq_data, irq_settings_get_trigger_mask(desc));
  702. irq_put_desc_unlock(desc, flags);
  703. }
  704. EXPORT_SYMBOL_GPL(irq_modify_status);
  705. /**
  706. * irq_cpu_online - Invoke all irq_cpu_online functions.
  707. *
  708. * Iterate through all irqs and invoke the chip.irq_cpu_online()
  709. * for each.
  710. */
  711. void irq_cpu_online(void)
  712. {
  713. struct irq_desc *desc;
  714. struct irq_chip *chip;
  715. unsigned long flags;
  716. unsigned int irq;
  717. for_each_active_irq(irq) {
  718. desc = irq_to_desc(irq);
  719. if (!desc)
  720. continue;
  721. raw_spin_lock_irqsave(&desc->lock, flags);
  722. chip = irq_data_get_irq_chip(&desc->irq_data);
  723. if (chip && chip->irq_cpu_online &&
  724. (!(chip->flags & IRQCHIP_ONOFFLINE_ENABLED) ||
  725. !irqd_irq_disabled(&desc->irq_data)))
  726. chip->irq_cpu_online(&desc->irq_data);
  727. raw_spin_unlock_irqrestore(&desc->lock, flags);
  728. }
  729. }
  730. /**
  731. * irq_cpu_offline - Invoke all irq_cpu_offline functions.
  732. *
  733. * Iterate through all irqs and invoke the chip.irq_cpu_offline()
  734. * for each.
  735. */
  736. void irq_cpu_offline(void)
  737. {
  738. struct irq_desc *desc;
  739. struct irq_chip *chip;
  740. unsigned long flags;
  741. unsigned int irq;
  742. for_each_active_irq(irq) {
  743. desc = irq_to_desc(irq);
  744. if (!desc)
  745. continue;
  746. raw_spin_lock_irqsave(&desc->lock, flags);
  747. chip = irq_data_get_irq_chip(&desc->irq_data);
  748. if (chip && chip->irq_cpu_offline &&
  749. (!(chip->flags & IRQCHIP_ONOFFLINE_ENABLED) ||
  750. !irqd_irq_disabled(&desc->irq_data)))
  751. chip->irq_cpu_offline(&desc->irq_data);
  752. raw_spin_unlock_irqrestore(&desc->lock, flags);
  753. }
  754. }
  755. #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
  756. /**
  757. * irq_chip_enable_parent - Enable the parent interrupt (defaults to unmask if
  758. * NULL)
  759. * @data: Pointer to interrupt specific data
  760. */
  761. void irq_chip_enable_parent(struct irq_data *data)
  762. {
  763. data = data->parent_data;
  764. if (data->chip->irq_enable)
  765. data->chip->irq_enable(data);
  766. else
  767. data->chip->irq_unmask(data);
  768. }
  769. /**
  770. * irq_chip_disable_parent - Disable the parent interrupt (defaults to mask if
  771. * NULL)
  772. * @data: Pointer to interrupt specific data
  773. */
  774. void irq_chip_disable_parent(struct irq_data *data)
  775. {
  776. data = data->parent_data;
  777. if (data->chip->irq_disable)
  778. data->chip->irq_disable(data);
  779. else
  780. data->chip->irq_mask(data);
  781. }
  782. /**
  783. * irq_chip_ack_parent - Acknowledge the parent interrupt
  784. * @data: Pointer to interrupt specific data
  785. */
  786. void irq_chip_ack_parent(struct irq_data *data)
  787. {
  788. data = data->parent_data;
  789. data->chip->irq_ack(data);
  790. }
  791. /**
  792. * irq_chip_mask_parent - Mask the parent interrupt
  793. * @data: Pointer to interrupt specific data
  794. */
  795. void irq_chip_mask_parent(struct irq_data *data)
  796. {
  797. data = data->parent_data;
  798. data->chip->irq_mask(data);
  799. }
  800. /**
  801. * irq_chip_unmask_parent - Unmask the parent interrupt
  802. * @data: Pointer to interrupt specific data
  803. */
  804. void irq_chip_unmask_parent(struct irq_data *data)
  805. {
  806. data = data->parent_data;
  807. data->chip->irq_unmask(data);
  808. }
  809. /**
  810. * irq_chip_eoi_parent - Invoke EOI on the parent interrupt
  811. * @data: Pointer to interrupt specific data
  812. */
  813. void irq_chip_eoi_parent(struct irq_data *data)
  814. {
  815. data = data->parent_data;
  816. data->chip->irq_eoi(data);
  817. }
  818. /**
  819. * irq_chip_set_affinity_parent - Set affinity on the parent interrupt
  820. * @data: Pointer to interrupt specific data
  821. * @dest: The affinity mask to set
  822. * @force: Flag to enforce setting (disable online checks)
  823. *
  824. * Conditinal, as the underlying parent chip might not implement it.
  825. */
  826. int irq_chip_set_affinity_parent(struct irq_data *data,
  827. const struct cpumask *dest, bool force)
  828. {
  829. data = data->parent_data;
  830. if (data->chip->irq_set_affinity)
  831. return data->chip->irq_set_affinity(data, dest, force);
  832. return -ENOSYS;
  833. }
  834. /**
  835. * irq_chip_set_type_parent - Set IRQ type on the parent interrupt
  836. * @data: Pointer to interrupt specific data
  837. * @type: IRQ_TYPE_{LEVEL,EDGE}_* value - see include/linux/irq.h
  838. *
  839. * Conditional, as the underlying parent chip might not implement it.
  840. */
  841. int irq_chip_set_type_parent(struct irq_data *data, unsigned int type)
  842. {
  843. data = data->parent_data;
  844. if (data->chip->irq_set_type)
  845. return data->chip->irq_set_type(data, type);
  846. return -ENOSYS;
  847. }
  848. /**
  849. * irq_chip_retrigger_hierarchy - Retrigger an interrupt in hardware
  850. * @data: Pointer to interrupt specific data
  851. *
  852. * Iterate through the domain hierarchy of the interrupt and check
  853. * whether a hw retrigger function exists. If yes, invoke it.
  854. */
  855. int irq_chip_retrigger_hierarchy(struct irq_data *data)
  856. {
  857. for (data = data->parent_data; data; data = data->parent_data)
  858. if (data->chip && data->chip->irq_retrigger)
  859. return data->chip->irq_retrigger(data);
  860. return 0;
  861. }
  862. /**
  863. * irq_chip_set_vcpu_affinity_parent - Set vcpu affinity on the parent interrupt
  864. * @data: Pointer to interrupt specific data
  865. * @vcpu_info: The vcpu affinity information
  866. */
  867. int irq_chip_set_vcpu_affinity_parent(struct irq_data *data, void *vcpu_info)
  868. {
  869. data = data->parent_data;
  870. if (data->chip->irq_set_vcpu_affinity)
  871. return data->chip->irq_set_vcpu_affinity(data, vcpu_info);
  872. return -ENOSYS;
  873. }
  874. /**
  875. * irq_chip_set_wake_parent - Set/reset wake-up on the parent interrupt
  876. * @data: Pointer to interrupt specific data
  877. * @on: Whether to set or reset the wake-up capability of this irq
  878. *
  879. * Conditional, as the underlying parent chip might not implement it.
  880. */
  881. int irq_chip_set_wake_parent(struct irq_data *data, unsigned int on)
  882. {
  883. data = data->parent_data;
  884. if (data->chip->irq_set_wake)
  885. return data->chip->irq_set_wake(data, on);
  886. return -ENOSYS;
  887. }
  888. #endif
  889. /**
  890. * irq_chip_compose_msi_msg - Componse msi message for a irq chip
  891. * @data: Pointer to interrupt specific data
  892. * @msg: Pointer to the MSI message
  893. *
  894. * For hierarchical domains we find the first chip in the hierarchy
  895. * which implements the irq_compose_msi_msg callback. For non
  896. * hierarchical we use the top level chip.
  897. */
  898. int irq_chip_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
  899. {
  900. struct irq_data *pos = NULL;
  901. #ifdef CONFIG_IRQ_DOMAIN_HIERARCHY
  902. for (; data; data = data->parent_data)
  903. #endif
  904. if (data->chip && data->chip->irq_compose_msi_msg)
  905. pos = data;
  906. if (!pos)
  907. return -ENOSYS;
  908. pos->chip->irq_compose_msi_msg(pos, msg);
  909. return 0;
  910. }