pm.c 5.1 KB

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