cpu_pm.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2011 Google, Inc.
  4. *
  5. * Author:
  6. * Colin Cross <ccross@android.com>
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/cpu_pm.h>
  10. #include <linux/module.h>
  11. #include <linux/notifier.h>
  12. #include <linux/spinlock.h>
  13. #include <linux/syscore_ops.h>
  14. /*
  15. * atomic_notifiers use a spinlock_t, which can block under PREEMPT_RT.
  16. * Notifications for cpu_pm will be issued by the idle task itself, which can
  17. * never block, IOW it requires using a raw_spinlock_t.
  18. */
  19. static struct {
  20. struct raw_notifier_head chain;
  21. raw_spinlock_t lock;
  22. } cpu_pm_notifier = {
  23. .chain = RAW_NOTIFIER_INIT(cpu_pm_notifier.chain),
  24. .lock = __RAW_SPIN_LOCK_UNLOCKED(cpu_pm_notifier.lock),
  25. };
  26. static int cpu_pm_notify(enum cpu_pm_event event)
  27. {
  28. int ret;
  29. rcu_read_lock();
  30. ret = raw_notifier_call_chain(&cpu_pm_notifier.chain, event, NULL);
  31. rcu_read_unlock();
  32. return notifier_to_errno(ret);
  33. }
  34. static int cpu_pm_notify_robust(enum cpu_pm_event event_up, enum cpu_pm_event event_down)
  35. {
  36. unsigned long flags;
  37. int ret;
  38. raw_spin_lock_irqsave(&cpu_pm_notifier.lock, flags);
  39. ret = raw_notifier_call_chain_robust(&cpu_pm_notifier.chain, event_up, event_down, NULL);
  40. raw_spin_unlock_irqrestore(&cpu_pm_notifier.lock, flags);
  41. return notifier_to_errno(ret);
  42. }
  43. /**
  44. * cpu_pm_register_notifier - register a driver with cpu_pm
  45. * @nb: notifier block to register
  46. *
  47. * Add a driver to a list of drivers that are notified about
  48. * CPU and CPU cluster low power entry and exit.
  49. *
  50. * This function has the same return conditions as raw_notifier_chain_register.
  51. */
  52. int cpu_pm_register_notifier(struct notifier_block *nb)
  53. {
  54. unsigned long flags;
  55. int ret;
  56. raw_spin_lock_irqsave(&cpu_pm_notifier.lock, flags);
  57. ret = raw_notifier_chain_register(&cpu_pm_notifier.chain, nb);
  58. raw_spin_unlock_irqrestore(&cpu_pm_notifier.lock, flags);
  59. return ret;
  60. }
  61. EXPORT_SYMBOL_GPL(cpu_pm_register_notifier);
  62. /**
  63. * cpu_pm_unregister_notifier - unregister a driver with cpu_pm
  64. * @nb: notifier block to be unregistered
  65. *
  66. * Remove a driver from the CPU PM notifier list.
  67. *
  68. * This function has the same return conditions as raw_notifier_chain_unregister.
  69. */
  70. int cpu_pm_unregister_notifier(struct notifier_block *nb)
  71. {
  72. unsigned long flags;
  73. int ret;
  74. raw_spin_lock_irqsave(&cpu_pm_notifier.lock, flags);
  75. ret = raw_notifier_chain_unregister(&cpu_pm_notifier.chain, nb);
  76. raw_spin_unlock_irqrestore(&cpu_pm_notifier.lock, flags);
  77. return ret;
  78. }
  79. EXPORT_SYMBOL_GPL(cpu_pm_unregister_notifier);
  80. /**
  81. * cpu_pm_enter - CPU low power entry notifier
  82. *
  83. * Notifies listeners that a single CPU is entering a low power state that may
  84. * cause some blocks in the same power domain as the cpu to reset.
  85. *
  86. * Must be called on the affected CPU with interrupts disabled. Platform is
  87. * responsible for ensuring that cpu_pm_enter is not called twice on the same
  88. * CPU before cpu_pm_exit is called. Notified drivers can include VFP
  89. * co-processor, interrupt controller and its PM extensions, local CPU
  90. * timers context save/restore which shouldn't be interrupted. Hence it
  91. * must be called with interrupts disabled.
  92. *
  93. * Return conditions are same as __raw_notifier_call_chain.
  94. */
  95. int cpu_pm_enter(void)
  96. {
  97. return cpu_pm_notify_robust(CPU_PM_ENTER, CPU_PM_ENTER_FAILED);
  98. }
  99. EXPORT_SYMBOL_GPL(cpu_pm_enter);
  100. /**
  101. * cpu_pm_exit - CPU low power exit notifier
  102. *
  103. * Notifies listeners that a single CPU is exiting a low power state that may
  104. * have caused some blocks in the same power domain as the cpu to reset.
  105. *
  106. * Notified drivers can include VFP co-processor, interrupt controller
  107. * and its PM extensions, local CPU timers context save/restore which
  108. * shouldn't be interrupted. Hence it must be called with interrupts disabled.
  109. *
  110. * Return conditions are same as __raw_notifier_call_chain.
  111. */
  112. int cpu_pm_exit(void)
  113. {
  114. return cpu_pm_notify(CPU_PM_EXIT);
  115. }
  116. EXPORT_SYMBOL_GPL(cpu_pm_exit);
  117. /**
  118. * cpu_cluster_pm_enter - CPU cluster low power entry notifier
  119. *
  120. * Notifies listeners that all cpus in a power domain are entering a low power
  121. * state that may cause some blocks in the same power domain to reset.
  122. *
  123. * Must be called after cpu_pm_enter has been called on all cpus in the power
  124. * domain, and before cpu_pm_exit has been called on any cpu in the power
  125. * domain. Notified drivers can include VFP co-processor, interrupt controller
  126. * and its PM extensions, local CPU timers context save/restore which
  127. * shouldn't be interrupted. Hence it must be called with interrupts disabled.
  128. *
  129. * Must be called with interrupts disabled.
  130. *
  131. * Return conditions are same as __raw_notifier_call_chain.
  132. */
  133. int cpu_cluster_pm_enter(void)
  134. {
  135. return cpu_pm_notify_robust(CPU_CLUSTER_PM_ENTER, CPU_CLUSTER_PM_ENTER_FAILED);
  136. }
  137. EXPORT_SYMBOL_GPL(cpu_cluster_pm_enter);
  138. /**
  139. * cpu_cluster_pm_exit - CPU cluster low power exit notifier
  140. *
  141. * Notifies listeners that all cpus in a power domain are exiting form a
  142. * low power state that may have caused some blocks in the same power domain
  143. * to reset.
  144. *
  145. * Must be called after cpu_cluster_pm_enter has been called for the power
  146. * domain, and before cpu_pm_exit has been called on any cpu in the power
  147. * domain. Notified drivers can include VFP co-processor, interrupt controller
  148. * and its PM extensions, local CPU timers context save/restore which
  149. * shouldn't be interrupted. Hence it must be called with interrupts disabled.
  150. *
  151. * Return conditions are same as __raw_notifier_call_chain.
  152. */
  153. int cpu_cluster_pm_exit(void)
  154. {
  155. return cpu_pm_notify(CPU_CLUSTER_PM_EXIT);
  156. }
  157. EXPORT_SYMBOL_GPL(cpu_cluster_pm_exit);
  158. #ifdef CONFIG_PM
  159. static int cpu_pm_suspend(void)
  160. {
  161. int ret;
  162. ret = cpu_pm_enter();
  163. if (ret)
  164. return ret;
  165. ret = cpu_cluster_pm_enter();
  166. return ret;
  167. }
  168. static void cpu_pm_resume(void)
  169. {
  170. cpu_cluster_pm_exit();
  171. cpu_pm_exit();
  172. }
  173. static struct syscore_ops cpu_pm_syscore_ops = {
  174. .suspend = cpu_pm_suspend,
  175. .resume = cpu_pm_resume,
  176. };
  177. static int cpu_pm_init(void)
  178. {
  179. register_syscore_ops(&cpu_pm_syscore_ops);
  180. return 0;
  181. }
  182. core_initcall(cpu_pm_init);
  183. #endif