pm.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. WARN_ON_ONCE(desc->no_suspend_depth &&
  40. desc->no_suspend_depth != desc->nr_actions);
  41. }
  42. /*
  43. * Called from __free_irq() with desc->lock held after @action has
  44. * been removed from the action chain.
  45. */
  46. void irq_pm_remove_action(struct irq_desc *desc, struct irqaction *action)
  47. {
  48. desc->nr_actions--;
  49. if (action->flags & IRQF_FORCE_RESUME)
  50. desc->force_resume_depth--;
  51. if (action->flags & IRQF_NO_SUSPEND)
  52. desc->no_suspend_depth--;
  53. }
  54. static bool suspend_device_irq(struct irq_desc *desc, int irq)
  55. {
  56. if (!desc->action || desc->no_suspend_depth)
  57. return false;
  58. if (irqd_is_wakeup_set(&desc->irq_data)) {
  59. irqd_set(&desc->irq_data, IRQD_WAKEUP_ARMED);
  60. /*
  61. * We return true here to force the caller to issue
  62. * synchronize_irq(). We need to make sure that the
  63. * IRQD_WAKEUP_ARMED is visible before we return from
  64. * suspend_device_irqs().
  65. */
  66. return true;
  67. }
  68. desc->istate |= IRQS_SUSPENDED;
  69. __disable_irq(desc, irq);
  70. /*
  71. * Hardware which has no wakeup source configuration facility
  72. * requires that the non wakeup interrupts are masked at the
  73. * chip level. The chip implementation indicates that with
  74. * IRQCHIP_MASK_ON_SUSPEND.
  75. */
  76. if (irq_desc_get_chip(desc)->flags & IRQCHIP_MASK_ON_SUSPEND)
  77. mask_irq(desc);
  78. return true;
  79. }
  80. /**
  81. * suspend_device_irqs - disable all currently enabled interrupt lines
  82. *
  83. * During system-wide suspend or hibernation device drivers need to be
  84. * prevented from receiving interrupts and this function is provided
  85. * for this purpose.
  86. *
  87. * So we disable all interrupts and mark them IRQS_SUSPENDED except
  88. * for those which are unused, those which are marked as not
  89. * suspendable via an interrupt request with the flag IRQF_NO_SUSPEND
  90. * set and those which are marked as active wakeup sources.
  91. *
  92. * The active wakeup sources are handled by the flow handler entry
  93. * code which checks for the IRQD_WAKEUP_ARMED flag, suspends the
  94. * interrupt and notifies the pm core about the wakeup.
  95. */
  96. void suspend_device_irqs(void)
  97. {
  98. struct irq_desc *desc;
  99. int irq;
  100. for_each_irq_desc(irq, desc) {
  101. unsigned long flags;
  102. bool sync;
  103. raw_spin_lock_irqsave(&desc->lock, flags);
  104. sync = suspend_device_irq(desc, irq);
  105. raw_spin_unlock_irqrestore(&desc->lock, flags);
  106. if (sync)
  107. synchronize_irq(irq);
  108. }
  109. }
  110. EXPORT_SYMBOL_GPL(suspend_device_irqs);
  111. static void resume_irq(struct irq_desc *desc, int irq)
  112. {
  113. irqd_clear(&desc->irq_data, IRQD_WAKEUP_ARMED);
  114. if (desc->istate & IRQS_SUSPENDED)
  115. goto resume;
  116. /* Force resume the interrupt? */
  117. if (!desc->force_resume_depth)
  118. return;
  119. /* Pretend that it got disabled ! */
  120. desc->depth++;
  121. resume:
  122. desc->istate &= ~IRQS_SUSPENDED;
  123. __enable_irq(desc, irq);
  124. }
  125. static void resume_irqs(bool want_early)
  126. {
  127. struct irq_desc *desc;
  128. int irq;
  129. for_each_irq_desc(irq, desc) {
  130. unsigned long flags;
  131. bool is_early = desc->action &&
  132. desc->action->flags & IRQF_EARLY_RESUME;
  133. if (!is_early && want_early)
  134. continue;
  135. raw_spin_lock_irqsave(&desc->lock, flags);
  136. resume_irq(desc, irq);
  137. raw_spin_unlock_irqrestore(&desc->lock, flags);
  138. }
  139. }
  140. /**
  141. * irq_pm_syscore_ops - enable interrupt lines early
  142. *
  143. * Enable all interrupt lines with %IRQF_EARLY_RESUME set.
  144. */
  145. static void irq_pm_syscore_resume(void)
  146. {
  147. resume_irqs(true);
  148. }
  149. static struct syscore_ops irq_pm_syscore_ops = {
  150. .resume = irq_pm_syscore_resume,
  151. };
  152. static int __init irq_pm_init_ops(void)
  153. {
  154. register_syscore_ops(&irq_pm_syscore_ops);
  155. return 0;
  156. }
  157. device_initcall(irq_pm_init_ops);
  158. /**
  159. * resume_device_irqs - enable interrupt lines disabled by suspend_device_irqs()
  160. *
  161. * Enable all non-%IRQF_EARLY_RESUME interrupt lines previously
  162. * disabled by suspend_device_irqs() that have the IRQS_SUSPENDED flag
  163. * set as well as those with %IRQF_FORCE_RESUME.
  164. */
  165. void resume_device_irqs(void)
  166. {
  167. resume_irqs(false);
  168. }
  169. EXPORT_SYMBOL_GPL(resume_device_irqs);