cpu_pm.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Copyright (C) 2011 Google, Inc.
  3. *
  4. * Author:
  5. * Colin Cross <ccross@android.com>
  6. *
  7. * This software is licensed under the terms of the GNU General Public
  8. * License version 2, as published by the Free Software Foundation, and
  9. * may be copied, distributed, and modified under those terms.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/cpu_pm.h>
  19. #include <linux/module.h>
  20. #include <linux/notifier.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/syscore_ops.h>
  23. static ATOMIC_NOTIFIER_HEAD(cpu_pm_notifier_chain);
  24. static int cpu_pm_notify(enum cpu_pm_event event, int nr_to_call, int *nr_calls)
  25. {
  26. int ret;
  27. /*
  28. * __atomic_notifier_call_chain has a RCU read critical section, which
  29. * could be disfunctional in cpu idle. Copy RCU_NONIDLE code to let
  30. * RCU know this.
  31. */
  32. rcu_irq_enter_irqson();
  33. ret = __atomic_notifier_call_chain(&cpu_pm_notifier_chain, event, NULL,
  34. nr_to_call, nr_calls);
  35. rcu_irq_exit_irqson();
  36. return notifier_to_errno(ret);
  37. }
  38. /**
  39. * cpu_pm_register_notifier - register a driver with cpu_pm
  40. * @nb: notifier block to register
  41. *
  42. * Add a driver to a list of drivers that are notified about
  43. * CPU and CPU cluster low power entry and exit.
  44. *
  45. * This function may sleep, and has the same return conditions as
  46. * raw_notifier_chain_register.
  47. */
  48. int cpu_pm_register_notifier(struct notifier_block *nb)
  49. {
  50. return atomic_notifier_chain_register(&cpu_pm_notifier_chain, nb);
  51. }
  52. EXPORT_SYMBOL_GPL(cpu_pm_register_notifier);
  53. /**
  54. * cpu_pm_unregister_notifier - unregister a driver with cpu_pm
  55. * @nb: notifier block to be unregistered
  56. *
  57. * Remove a driver from the CPU PM notifier list.
  58. *
  59. * This function may sleep, and has the same return conditions as
  60. * raw_notifier_chain_unregister.
  61. */
  62. int cpu_pm_unregister_notifier(struct notifier_block *nb)
  63. {
  64. return atomic_notifier_chain_unregister(&cpu_pm_notifier_chain, nb);
  65. }
  66. EXPORT_SYMBOL_GPL(cpu_pm_unregister_notifier);
  67. /**
  68. * cpu_pm_enter - CPU low power entry notifier
  69. *
  70. * Notifies listeners that a single CPU is entering a low power state that may
  71. * cause some blocks in the same power domain as the cpu to reset.
  72. *
  73. * Must be called on the affected CPU with interrupts disabled. Platform is
  74. * responsible for ensuring that cpu_pm_enter is not called twice on the same
  75. * CPU before cpu_pm_exit is called. Notified drivers can include VFP
  76. * co-processor, interrupt controller and its PM extensions, local CPU
  77. * timers context save/restore which shouldn't be interrupted. Hence it
  78. * must be called with interrupts disabled.
  79. *
  80. * Return conditions are same as __raw_notifier_call_chain.
  81. */
  82. int cpu_pm_enter(void)
  83. {
  84. int nr_calls = 0;
  85. int ret = 0;
  86. ret = cpu_pm_notify(CPU_PM_ENTER, -1, &nr_calls);
  87. if (ret)
  88. /*
  89. * Inform listeners (nr_calls - 1) about failure of CPU PM
  90. * PM entry who are notified earlier to prepare for it.
  91. */
  92. cpu_pm_notify(CPU_PM_ENTER_FAILED, nr_calls - 1, NULL);
  93. return ret;
  94. }
  95. EXPORT_SYMBOL_GPL(cpu_pm_enter);
  96. /**
  97. * cpu_pm_exit - CPU low power exit notifier
  98. *
  99. * Notifies listeners that a single CPU is exiting a low power state that may
  100. * have caused some blocks in the same power domain as the cpu to reset.
  101. *
  102. * Notified drivers can include VFP co-processor, interrupt controller
  103. * and its PM extensions, local CPU timers context save/restore which
  104. * shouldn't be interrupted. Hence it must be called with interrupts disabled.
  105. *
  106. * Return conditions are same as __raw_notifier_call_chain.
  107. */
  108. int cpu_pm_exit(void)
  109. {
  110. return cpu_pm_notify(CPU_PM_EXIT, -1, NULL);
  111. }
  112. EXPORT_SYMBOL_GPL(cpu_pm_exit);
  113. /**
  114. * cpu_cluster_pm_enter - CPU cluster low power entry notifier
  115. *
  116. * Notifies listeners that all cpus in a power domain are entering a low power
  117. * state that may cause some blocks in the same power domain to reset.
  118. *
  119. * Must be called after cpu_pm_enter has been called on all cpus in the power
  120. * domain, and before cpu_pm_exit has been called on any cpu in the power
  121. * domain. Notified drivers can include VFP co-processor, interrupt controller
  122. * and its PM extensions, local CPU timers context save/restore which
  123. * shouldn't be interrupted. Hence it must be called with interrupts disabled.
  124. *
  125. * Must be called with interrupts disabled.
  126. *
  127. * Return conditions are same as __raw_notifier_call_chain.
  128. */
  129. int cpu_cluster_pm_enter(void)
  130. {
  131. int nr_calls = 0;
  132. int ret = 0;
  133. ret = cpu_pm_notify(CPU_CLUSTER_PM_ENTER, -1, &nr_calls);
  134. if (ret)
  135. /*
  136. * Inform listeners (nr_calls - 1) about failure of CPU cluster
  137. * PM entry who are notified earlier to prepare for it.
  138. */
  139. cpu_pm_notify(CPU_CLUSTER_PM_ENTER_FAILED, nr_calls - 1, NULL);
  140. return ret;
  141. }
  142. EXPORT_SYMBOL_GPL(cpu_cluster_pm_enter);
  143. /**
  144. * cpu_cluster_pm_exit - CPU cluster low power exit notifier
  145. *
  146. * Notifies listeners that all cpus in a power domain are exiting form a
  147. * low power state that may have caused some blocks in the same power domain
  148. * to reset.
  149. *
  150. * Must be called after cpu_cluster_pm_enter has been called for the power
  151. * domain, and before cpu_pm_exit has been called on any cpu in the power
  152. * domain. Notified drivers can include VFP co-processor, interrupt controller
  153. * and its PM extensions, local CPU timers context save/restore which
  154. * shouldn't be interrupted. Hence it must be called with interrupts disabled.
  155. *
  156. * Return conditions are same as __raw_notifier_call_chain.
  157. */
  158. int cpu_cluster_pm_exit(void)
  159. {
  160. return cpu_pm_notify(CPU_CLUSTER_PM_EXIT, -1, NULL);
  161. }
  162. EXPORT_SYMBOL_GPL(cpu_cluster_pm_exit);
  163. #ifdef CONFIG_PM
  164. static int cpu_pm_suspend(void)
  165. {
  166. int ret;
  167. ret = cpu_pm_enter();
  168. if (ret)
  169. return ret;
  170. ret = cpu_cluster_pm_enter();
  171. return ret;
  172. }
  173. static void cpu_pm_resume(void)
  174. {
  175. cpu_cluster_pm_exit();
  176. cpu_pm_exit();
  177. }
  178. static struct syscore_ops cpu_pm_syscore_ops = {
  179. .suspend = cpu_pm_suspend,
  180. .resume = cpu_pm_resume,
  181. };
  182. static int cpu_pm_init(void)
  183. {
  184. register_syscore_ops(&cpu_pm_syscore_ops);
  185. return 0;
  186. }
  187. core_initcall(cpu_pm_init);
  188. #endif