cpu_pm.c 6.1 KB

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