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_wakeup();
  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, int irq)
  60. {
  61. if (!desc->action || 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, irq);
  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, irq);
  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, int irq)
  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. resume:
  129. desc->istate &= ~IRQS_SUSPENDED;
  130. __enable_irq(desc, irq);
  131. }
  132. static void resume_irqs(bool want_early)
  133. {
  134. struct irq_desc *desc;
  135. int irq;
  136. for_each_irq_desc(irq, desc) {
  137. unsigned long flags;
  138. bool is_early = desc->action &&
  139. desc->action->flags & IRQF_EARLY_RESUME;
  140. if (!is_early && want_early)
  141. continue;
  142. if (irq_settings_is_nested_thread(desc))
  143. continue;
  144. raw_spin_lock_irqsave(&desc->lock, flags);
  145. resume_irq(desc, irq);
  146. raw_spin_unlock_irqrestore(&desc->lock, flags);
  147. }
  148. }
  149. /**
  150. * irq_pm_syscore_ops - enable interrupt lines early
  151. *
  152. * Enable all interrupt lines with %IRQF_EARLY_RESUME set.
  153. */
  154. static void irq_pm_syscore_resume(void)
  155. {
  156. resume_irqs(true);
  157. }
  158. static struct syscore_ops irq_pm_syscore_ops = {
  159. .resume = irq_pm_syscore_resume,
  160. };
  161. static int __init irq_pm_init_ops(void)
  162. {
  163. register_syscore_ops(&irq_pm_syscore_ops);
  164. return 0;
  165. }
  166. device_initcall(irq_pm_init_ops);
  167. /**
  168. * resume_device_irqs - enable interrupt lines disabled by suspend_device_irqs()
  169. *
  170. * Enable all non-%IRQF_EARLY_RESUME interrupt lines previously
  171. * disabled by suspend_device_irqs() that have the IRQS_SUSPENDED flag
  172. * set as well as those with %IRQF_FORCE_RESUME.
  173. */
  174. void resume_device_irqs(void)
  175. {
  176. resume_irqs(false);
  177. }
  178. EXPORT_SYMBOL_GPL(resume_device_irqs);