1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2011 Google, Inc.
6 * Colin Cross <ccross@android.com>
9 #include <linux/kernel.h>
10 #include <linux/cpu_pm.h>
11 #include <linux/module.h>
12 #include <linux/notifier.h>
13 #include <linux/spinlock.h>
14 #include <linux/syscore_ops.h>
16 static ATOMIC_NOTIFIER_HEAD(cpu_pm_notifier_chain);
18 static int cpu_pm_notify(enum cpu_pm_event event)
23 * atomic_notifier_call_chain has a RCU read critical section, which
24 * could be disfunctional in cpu idle. Copy RCU_NONIDLE code to let
27 rcu_irq_enter_irqson();
28 ret = atomic_notifier_call_chain(&cpu_pm_notifier_chain, event, NULL);
29 rcu_irq_exit_irqson();
31 return notifier_to_errno(ret);
34 static int cpu_pm_notify_robust(enum cpu_pm_event event_up, enum cpu_pm_event event_down)
38 rcu_irq_enter_irqson();
39 ret = atomic_notifier_call_chain_robust(&cpu_pm_notifier_chain, event_up, event_down, NULL);
40 rcu_irq_exit_irqson();
42 return notifier_to_errno(ret);
46 * cpu_pm_register_notifier - register a driver with cpu_pm
47 * @nb: notifier block to register
49 * Add a driver to a list of drivers that are notified about
50 * CPU and CPU cluster low power entry and exit.
52 * This function may sleep, and has the same return conditions as
53 * raw_notifier_chain_register.
55 int cpu_pm_register_notifier(struct notifier_block *nb)
57 return atomic_notifier_chain_register(&cpu_pm_notifier_chain, nb);
59 EXPORT_SYMBOL_GPL(cpu_pm_register_notifier);
62 * cpu_pm_unregister_notifier - unregister a driver with cpu_pm
63 * @nb: notifier block to be unregistered
65 * Remove a driver from the CPU PM notifier list.
67 * This function may sleep, and has the same return conditions as
68 * raw_notifier_chain_unregister.
70 int cpu_pm_unregister_notifier(struct notifier_block *nb)
72 return atomic_notifier_chain_unregister(&cpu_pm_notifier_chain, nb);
74 EXPORT_SYMBOL_GPL(cpu_pm_unregister_notifier);
77 * cpu_pm_enter - CPU low power entry notifier
79 * Notifies listeners that a single CPU is entering a low power state that may
80 * cause some blocks in the same power domain as the cpu to reset.
82 * Must be called on the affected CPU with interrupts disabled. Platform is
83 * responsible for ensuring that cpu_pm_enter is not called twice on the same
84 * CPU before cpu_pm_exit is called. Notified drivers can include VFP
85 * co-processor, interrupt controller and its PM extensions, local CPU
86 * timers context save/restore which shouldn't be interrupted. Hence it
87 * must be called with interrupts disabled.
89 * Return conditions are same as __raw_notifier_call_chain.
91 int cpu_pm_enter(void)
93 return cpu_pm_notify_robust(CPU_PM_ENTER, CPU_PM_ENTER_FAILED);
95 EXPORT_SYMBOL_GPL(cpu_pm_enter);
98 * cpu_pm_exit - CPU low power exit notifier
100 * Notifies listeners that a single CPU is exiting a low power state that may
101 * have caused some blocks in the same power domain as the cpu to reset.
103 * Notified drivers can include VFP co-processor, interrupt controller
104 * and its PM extensions, local CPU timers context save/restore which
105 * shouldn't be interrupted. Hence it must be called with interrupts disabled.
107 * Return conditions are same as __raw_notifier_call_chain.
109 int cpu_pm_exit(void)
111 return cpu_pm_notify(CPU_PM_EXIT);
113 EXPORT_SYMBOL_GPL(cpu_pm_exit);
116 * cpu_cluster_pm_enter - CPU cluster low power entry notifier
118 * Notifies listeners that all cpus in a power domain are entering a low power
119 * state that may cause some blocks in the same power domain to reset.
121 * Must be called after cpu_pm_enter has been called on all cpus in the power
122 * domain, and before cpu_pm_exit has been called on any cpu in the power
123 * domain. Notified drivers can include VFP co-processor, interrupt controller
124 * and its PM extensions, local CPU timers context save/restore which
125 * shouldn't be interrupted. Hence it must be called with interrupts disabled.
127 * Must be called with interrupts disabled.
129 * Return conditions are same as __raw_notifier_call_chain.
131 int cpu_cluster_pm_enter(void)
133 return cpu_pm_notify_robust(CPU_CLUSTER_PM_ENTER, CPU_CLUSTER_PM_ENTER_FAILED);
135 EXPORT_SYMBOL_GPL(cpu_cluster_pm_enter);
138 * cpu_cluster_pm_exit - CPU cluster low power exit notifier
140 * Notifies listeners that all cpus in a power domain are exiting form a
141 * low power state that may have caused some blocks in the same power domain
144 * Must be called after cpu_cluster_pm_enter has been called for the power
145 * domain, and before cpu_pm_exit has been called on any cpu in the power
146 * domain. Notified drivers can include VFP co-processor, interrupt controller
147 * and its PM extensions, local CPU timers context save/restore which
148 * shouldn't be interrupted. Hence it must be called with interrupts disabled.
150 * Return conditions are same as __raw_notifier_call_chain.
152 int cpu_cluster_pm_exit(void)
154 return cpu_pm_notify(CPU_CLUSTER_PM_EXIT);
156 EXPORT_SYMBOL_GPL(cpu_cluster_pm_exit);
159 static int cpu_pm_suspend(void)
163 ret = cpu_pm_enter();
167 ret = cpu_cluster_pm_enter();
171 static void cpu_pm_resume(void)
173 cpu_cluster_pm_exit();
177 static struct syscore_ops cpu_pm_syscore_ops = {
178 .suspend = cpu_pm_suspend,
179 .resume = cpu_pm_resume,
182 static int cpu_pm_init(void)
184 register_syscore_ops(&cpu_pm_syscore_ops);
187 core_initcall(cpu_pm_init);