cpu_pm.c 6.0 KB

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