pm.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2009 Rafael J. Wysocki <rjw@sisk.pl>, Novell Inc.
  4. *
  5. * This file contains power management functions related to interrupts.
  6. */
  7. #include <linux/irq.h>
  8. #include <linux/module.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/suspend.h>
  11. #include <linux/syscore_ops.h>
  12. #include "internals.h"
  13. bool irq_pm_check_wakeup(struct irq_desc *desc)
  14. {
  15. if (irqd_is_wakeup_armed(&desc->irq_data)) {
  16. irqd_clear(&desc->irq_data, IRQD_WAKEUP_ARMED);
  17. desc->istate |= IRQS_SUSPENDED | IRQS_PENDING;
  18. desc->depth++;
  19. irq_disable(desc);
  20. pm_system_irq_wakeup(irq_desc_get_irq(desc));
  21. return true;
  22. }
  23. return false;
  24. }
  25. /*
  26. * Called from __setup_irq() with desc->lock held after @action has
  27. * been installed in the action chain.
  28. */
  29. void irq_pm_install_action(struct irq_desc *desc, struct irqaction *action)
  30. {
  31. desc->nr_actions++;
  32. if (action->flags & IRQF_FORCE_RESUME)
  33. desc->force_resume_depth++;
  34. WARN_ON_ONCE(desc->force_resume_depth &&
  35. desc->force_resume_depth != desc->nr_actions);
  36. if (action->flags & IRQF_NO_SUSPEND)
  37. desc->no_suspend_depth++;
  38. else if (action->flags & IRQF_COND_SUSPEND)
  39. desc->cond_suspend_depth++;
  40. WARN_ON_ONCE(desc->no_suspend_depth &&
  41. (desc->no_suspend_depth +
  42. desc->cond_suspend_depth) != desc->nr_actions);
  43. }
  44. /*
  45. * Called from __free_irq() with desc->lock held after @action has
  46. * been removed from the action chain.
  47. */
  48. void irq_pm_remove_action(struct irq_desc *desc, struct irqaction *action)
  49. {
  50. desc->nr_actions--;
  51. if (action->flags & IRQF_FORCE_RESUME)
  52. desc->force_resume_depth--;
  53. if (action->flags & IRQF_NO_SUSPEND)
  54. desc->no_suspend_depth--;
  55. else if (action->flags & IRQF_COND_SUSPEND)
  56. desc->cond_suspend_depth--;
  57. }
  58. static bool suspend_device_irq(struct irq_desc *desc)
  59. {
  60. if (!desc->action || irq_desc_is_chained(desc) ||
  61. desc->no_suspend_depth)
  62. return false;
  63. if (irqd_is_wakeup_set(&desc->irq_data)) {
  64. irqd_set(&desc->irq_data, IRQD_WAKEUP_ARMED);
  65. /*
  66. * We return true here to force the caller to issue
  67. * synchronize_irq(). We need to make sure that the
  68. * IRQD_WAKEUP_ARMED is visible before we return from
  69. * suspend_device_irqs().
  70. */
  71. return true;
  72. }
  73. desc->istate |= IRQS_SUSPENDED;
  74. __disable_irq(desc);
  75. /*
  76. * Hardware which has no wakeup source configuration facility
  77. * requires that the non wakeup interrupts are masked at the
  78. * chip level. The chip implementation indicates that with
  79. * IRQCHIP_MASK_ON_SUSPEND.
  80. */
  81. if (irq_desc_get_chip(desc)->flags & IRQCHIP_MASK_ON_SUSPEND)
  82. mask_irq(desc);
  83. return true;
  84. }
  85. /**
  86. * suspend_device_irqs - disable all currently enabled interrupt lines
  87. *
  88. * During system-wide suspend or hibernation device drivers need to be
  89. * prevented from receiving interrupts and this function is provided
  90. * for this purpose.
  91. *
  92. * So we disable all interrupts and mark them IRQS_SUSPENDED except
  93. * for those which are unused, those which are marked as not
  94. * suspendable via an interrupt request with the flag IRQF_NO_SUSPEND
  95. * set and those which are marked as active wakeup sources.
  96. *
  97. * The active wakeup sources are handled by the flow handler entry
  98. * code which checks for the IRQD_WAKEUP_ARMED flag, suspends the
  99. * interrupt and notifies the pm core about the wakeup.
  100. */
  101. void suspend_device_irqs(void)
  102. {
  103. struct irq_desc *desc;
  104. int irq;
  105. for_each_irq_desc(irq, desc) {
  106. unsigned long flags;
  107. bool sync;
  108. if (irq_settings_is_nested_thread(desc))
  109. continue;
  110. raw_spin_lock_irqsave(&desc->lock, flags);
  111. sync = suspend_device_irq(desc);
  112. raw_spin_unlock_irqrestore(&desc->lock, flags);
  113. if (sync)
  114. synchronize_irq(irq);
  115. }
  116. }
  117. EXPORT_SYMBOL_GPL(suspend_device_irqs);
  118. static void resume_irq(struct irq_desc *desc)
  119. {
  120. irqd_clear(&desc->irq_data, IRQD_WAKEUP_ARMED);
  121. if (desc->istate & IRQS_SUSPENDED)
  122. goto resume;
  123. /* Force resume the interrupt? */
  124. if (!desc->force_resume_depth)
  125. return;
  126. /* Pretend that it got disabled ! */
  127. desc->depth++;
  128. irq_state_set_disabled(desc);
  129. irq_state_set_masked(desc);
  130. resume:
  131. desc->istate &= ~IRQS_SUSPENDED;
  132. __enable_irq(desc);
  133. }
  134. static void resume_irqs(bool want_early)
  135. {
  136. struct irq_desc *desc;
  137. int irq;
  138. for_each_irq_desc(irq, desc) {
  139. unsigned long flags;
  140. bool is_early = desc->action &&
  141. desc->action->flags & IRQF_EARLY_RESUME;
  142. if (!is_early && want_early)
  143. continue;
  144. if (irq_settings_is_nested_thread(desc))
  145. continue;
  146. raw_spin_lock_irqsave(&desc->lock, flags);
  147. resume_irq(desc);
  148. raw_spin_unlock_irqrestore(&desc->lock, flags);
  149. }
  150. }
  151. /**
  152. * irq_pm_syscore_ops - enable interrupt lines early
  153. *
  154. * Enable all interrupt lines with %IRQF_EARLY_RESUME set.
  155. */
  156. static void irq_pm_syscore_resume(void)
  157. {
  158. resume_irqs(true);
  159. }
  160. static struct syscore_ops irq_pm_syscore_ops = {
  161. .resume = irq_pm_syscore_resume,
  162. };
  163. static int __init irq_pm_init_ops(void)
  164. {
  165. register_syscore_ops(&irq_pm_syscore_ops);
  166. return 0;
  167. }
  168. device_initcall(irq_pm_init_ops);
  169. /**
  170. * resume_device_irqs - enable interrupt lines disabled by suspend_device_irqs()
  171. *
  172. * Enable all non-%IRQF_EARLY_RESUME interrupt lines previously
  173. * disabled by suspend_device_irqs() that have the IRQS_SUSPENDED flag
  174. * set as well as those with %IRQF_FORCE_RESUME.
  175. */
  176. void resume_device_irqs(void)
  177. {
  178. resume_irqs(false);
  179. }
  180. EXPORT_SYMBOL_GPL(resume_device_irqs);