cpu_pm.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 DEFINE_RWLOCK(cpu_pm_notifier_lock);
  24. static RAW_NOTIFIER_HEAD(cpu_pm_notifier_chain);
  25. static int cpu_pm_notify(enum cpu_pm_event event, int nr_to_call, int *nr_calls)
  26. {
  27. int ret;
  28. ret = __raw_notifier_call_chain(&cpu_pm_notifier_chain, event, NULL,
  29. nr_to_call, nr_calls);
  30. return notifier_to_errno(ret);
  31. }
  32. /**
  33. * cpu_pm_register_notifier - register a driver with cpu_pm
  34. * @nb: notifier block to register
  35. *
  36. * Add a driver to a list of drivers that are notified about
  37. * CPU and CPU cluster low power entry and exit.
  38. *
  39. * This function may sleep, and has the same return conditions as
  40. * raw_notifier_chain_register.
  41. */
  42. int cpu_pm_register_notifier(struct notifier_block *nb)
  43. {
  44. unsigned long flags;
  45. int ret;
  46. write_lock_irqsave(&cpu_pm_notifier_lock, flags);
  47. ret = raw_notifier_chain_register(&cpu_pm_notifier_chain, nb);
  48. write_unlock_irqrestore(&cpu_pm_notifier_lock, flags);
  49. return ret;
  50. }
  51. EXPORT_SYMBOL_GPL(cpu_pm_register_notifier);
  52. /**
  53. * cpu_pm_unregister_notifier - unregister a driver with cpu_pm
  54. * @nb: notifier block to be unregistered
  55. *
  56. * Remove a driver from the CPU PM notifier list.
  57. *
  58. * This function may sleep, and has the same return conditions as
  59. * raw_notifier_chain_unregister.
  60. */
  61. int cpu_pm_unregister_notifier(struct notifier_block *nb)
  62. {
  63. unsigned long flags;
  64. int ret;
  65. write_lock_irqsave(&cpu_pm_notifier_lock, flags);
  66. ret = raw_notifier_chain_unregister(&cpu_pm_notifier_chain, nb);
  67. write_unlock_irqrestore(&cpu_pm_notifier_lock, flags);
  68. return ret;
  69. }
  70. EXPORT_SYMBOL_GPL(cpu_pm_unregister_notifier);
  71. /**
  72. * cpu_pm_enter - CPU low power entry notifier
  73. *
  74. * Notifies listeners that a single CPU is entering a low power state that may
  75. * cause some blocks in the same power domain as the cpu to reset.
  76. *
  77. * Must be called on the affected CPU with interrupts disabled. Platform is
  78. * responsible for ensuring that cpu_pm_enter is not called twice on the same
  79. * CPU before cpu_pm_exit is called. Notified drivers can include VFP
  80. * co-processor, interrupt controller and its PM extensions, local CPU
  81. * timers context save/restore which shouldn't be interrupted. Hence it
  82. * must be called with interrupts disabled.
  83. *
  84. * Return conditions are same as __raw_notifier_call_chain.
  85. */
  86. int cpu_pm_enter(void)
  87. {
  88. int nr_calls;
  89. int ret = 0;
  90. read_lock(&cpu_pm_notifier_lock);
  91. ret = cpu_pm_notify(CPU_PM_ENTER, -1, &nr_calls);
  92. if (ret)
  93. /*
  94. * Inform listeners (nr_calls - 1) about failure of CPU PM
  95. * PM entry who are notified earlier to prepare for it.
  96. */
  97. cpu_pm_notify(CPU_PM_ENTER_FAILED, nr_calls - 1, NULL);
  98. read_unlock(&cpu_pm_notifier_lock);
  99. return ret;
  100. }
  101. EXPORT_SYMBOL_GPL(cpu_pm_enter);
  102. /**
  103. * cpu_pm_exit - CPU low power exit notifier
  104. *
  105. * Notifies listeners that a single CPU is exiting a low power state that may
  106. * have caused some blocks in the same power domain as the cpu to reset.
  107. *
  108. * Notified drivers can include VFP co-processor, interrupt controller
  109. * and its PM extensions, local CPU timers context save/restore which
  110. * shouldn't be interrupted. Hence it must be called with interrupts disabled.
  111. *
  112. * Return conditions are same as __raw_notifier_call_chain.
  113. */
  114. int cpu_pm_exit(void)
  115. {
  116. int ret;
  117. read_lock(&cpu_pm_notifier_lock);
  118. ret = cpu_pm_notify(CPU_PM_EXIT, -1, NULL);
  119. read_unlock(&cpu_pm_notifier_lock);
  120. return ret;
  121. }
  122. EXPORT_SYMBOL_GPL(cpu_pm_exit);
  123. /**
  124. * cpu_cluster_pm_enter - CPU cluster low power entry notifier
  125. *
  126. * Notifies listeners that all cpus in a power domain are entering a low power
  127. * state that may cause some blocks in the same power domain to reset.
  128. *
  129. * Must be called after cpu_pm_enter has been called on all cpus in the power
  130. * domain, and before cpu_pm_exit has been called on any cpu in the power
  131. * domain. Notified drivers can include VFP co-processor, interrupt controller
  132. * and its PM extensions, local CPU timers context save/restore which
  133. * shouldn't be interrupted. Hence it must be called with interrupts disabled.
  134. *
  135. * Must be called with interrupts disabled.
  136. *
  137. * Return conditions are same as __raw_notifier_call_chain.
  138. */
  139. int cpu_cluster_pm_enter(void)
  140. {
  141. int nr_calls;
  142. int ret = 0;
  143. read_lock(&cpu_pm_notifier_lock);
  144. ret = cpu_pm_notify(CPU_CLUSTER_PM_ENTER, -1, &nr_calls);
  145. if (ret)
  146. /*
  147. * Inform listeners (nr_calls - 1) about failure of CPU cluster
  148. * PM entry who are notified earlier to prepare for it.
  149. */
  150. cpu_pm_notify(CPU_CLUSTER_PM_ENTER_FAILED, nr_calls - 1, NULL);
  151. read_unlock(&cpu_pm_notifier_lock);
  152. return ret;
  153. }
  154. EXPORT_SYMBOL_GPL(cpu_cluster_pm_enter);
  155. /**
  156. * cpu_cluster_pm_exit - CPU cluster low power exit notifier
  157. *
  158. * Notifies listeners that all cpus in a power domain are exiting form a
  159. * low power state that may have caused some blocks in the same power domain
  160. * to reset.
  161. *
  162. * Must be called after cpu_cluster_pm_enter has been called for the power
  163. * domain, and before cpu_pm_exit has been called on any cpu in the power
  164. * domain. Notified drivers can include VFP co-processor, interrupt controller
  165. * and its PM extensions, local CPU timers context save/restore which
  166. * shouldn't be interrupted. Hence it must be called with interrupts disabled.
  167. *
  168. * Return conditions are same as __raw_notifier_call_chain.
  169. */
  170. int cpu_cluster_pm_exit(void)
  171. {
  172. int ret;
  173. read_lock(&cpu_pm_notifier_lock);
  174. ret = cpu_pm_notify(CPU_CLUSTER_PM_EXIT, -1, NULL);
  175. read_unlock(&cpu_pm_notifier_lock);
  176. return ret;
  177. }
  178. EXPORT_SYMBOL_GPL(cpu_cluster_pm_exit);
  179. #ifdef CONFIG_PM
  180. static int cpu_pm_suspend(void)
  181. {
  182. int ret;
  183. ret = cpu_pm_enter();
  184. if (ret)
  185. return ret;
  186. ret = cpu_cluster_pm_enter();
  187. return ret;
  188. }
  189. static void cpu_pm_resume(void)
  190. {
  191. cpu_cluster_pm_exit();
  192. cpu_pm_exit();
  193. }
  194. static struct syscore_ops cpu_pm_syscore_ops = {
  195. .suspend = cpu_pm_suspend,
  196. .resume = cpu_pm_resume,
  197. };
  198. static int cpu_pm_init(void)
  199. {
  200. register_syscore_ops(&cpu_pm_syscore_ops);
  201. return 0;
  202. }
  203. core_initcall(cpu_pm_init);
  204. #endif