xref: /linux/kernel/cpu_pm.c (revision 486b257357c23743d9729ea715136d105fff461e)
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 
9 #include <linux/kernel.h>
10 #include <linux/cpu_pm.h>
11 #include <linux/module.h>
12 #include <linux/notifier.h>
13 #include <linux/rcupdate.h>
14 #include <linux/spinlock.h>
15 #include <linux/syscore_ops.h>
16 
17 /*
18  * atomic_notifiers use a spinlock_t, which can block under PREEMPT_RT.
19  * Notifications for cpu_pm will be issued by the idle task itself, which can
20  * never block, IOW it requires using a raw_spinlock_t.
21  */
22 static struct {
23 	struct raw_notifier_head chain;
24 	raw_spinlock_t lock;
25 } cpu_pm_notifier = {
26 	.chain = RAW_NOTIFIER_INIT(cpu_pm_notifier.chain),
27 	.lock  = __RAW_SPIN_LOCK_UNLOCKED(cpu_pm_notifier.lock),
28 };
29 
30 static int cpu_pm_notify(enum cpu_pm_event event)
31 {
32 	int ret;
33 
34 	rcu_read_lock();
35 	ret = raw_notifier_call_chain(&cpu_pm_notifier.chain, event, NULL);
36 	rcu_read_unlock();
37 
38 	return notifier_to_errno(ret);
39 }
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 
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 
50 	return notifier_to_errno(ret);
51 }
52 
53 /**
54  * cpu_pm_register_notifier - register a driver with cpu_pm
55  * @nb: notifier block to register
56  *
57  * Add a driver to a list of drivers that are notified about
58  * CPU and CPU cluster low power entry and exit.
59  *
60  * This function has the same return conditions as raw_notifier_chain_register.
61  */
62 int cpu_pm_register_notifier(struct notifier_block *nb)
63 {
64 	unsigned long flags;
65 	int ret;
66 
67 	raw_spin_lock_irqsave(&cpu_pm_notifier.lock, flags);
68 	ret = raw_notifier_chain_register(&cpu_pm_notifier.chain, nb);
69 	raw_spin_unlock_irqrestore(&cpu_pm_notifier.lock, flags);
70 	return ret;
71 }
72 EXPORT_SYMBOL_GPL(cpu_pm_register_notifier);
73 
74 /**
75  * cpu_pm_unregister_notifier - unregister a driver with cpu_pm
76  * @nb: notifier block to be unregistered
77  *
78  * Remove a driver from the CPU PM notifier list.
79  *
80  * This function may sleep, and has the same return conditions as
81  * raw_notifier_chain_unregister.
82  */
83 int cpu_pm_unregister_notifier(struct notifier_block *nb)
84 {
85 	unsigned long flags;
86 	int ret;
87 
88 	raw_spin_lock_irqsave(&cpu_pm_notifier.lock, flags);
89 	ret = raw_notifier_chain_unregister(&cpu_pm_notifier.chain, nb);
90 	raw_spin_unlock_irqrestore(&cpu_pm_notifier.lock, flags);
91 
92 	/* Wait for the rcu_read_lock() walkers in cpu_pm_notify(). */
93 	if (!ret)
94 		synchronize_rcu();
95 
96 	return ret;
97 }
98 EXPORT_SYMBOL_GPL(cpu_pm_unregister_notifier);
99 
100 /**
101  * cpu_pm_enter - CPU low power entry notifier
102  *
103  * Notifies listeners that a single CPU is entering a low power state that may
104  * cause some blocks in the same power domain as the cpu to reset.
105  *
106  * Must be called on the affected CPU with interrupts disabled.  Platform is
107  * responsible for ensuring that cpu_pm_enter is not called twice on the same
108  * CPU before cpu_pm_exit is called. Notified drivers can include VFP
109  * co-processor, interrupt controller and its PM extensions, local CPU
110  * timers context save/restore which shouldn't be interrupted. Hence it
111  * must be called with interrupts disabled.
112  *
113  * Return conditions are same as __raw_notifier_call_chain.
114  */
115 int cpu_pm_enter(void)
116 {
117 	return cpu_pm_notify_robust(CPU_PM_ENTER, CPU_PM_ENTER_FAILED);
118 }
119 EXPORT_SYMBOL_GPL(cpu_pm_enter);
120 
121 /**
122  * cpu_pm_exit - CPU low power exit notifier
123  *
124  * Notifies listeners that a single CPU is exiting a low power state that may
125  * have caused some blocks in the same power domain as the cpu to reset.
126  *
127  * Notified drivers can include VFP co-processor, interrupt controller
128  * and its PM extensions, local CPU timers context save/restore which
129  * shouldn't be interrupted. Hence it must be called with interrupts disabled.
130  *
131  * Return conditions are same as __raw_notifier_call_chain.
132  */
133 int cpu_pm_exit(void)
134 {
135 	return cpu_pm_notify(CPU_PM_EXIT);
136 }
137 EXPORT_SYMBOL_GPL(cpu_pm_exit);
138 
139 /**
140  * cpu_cluster_pm_enter - CPU cluster low power entry notifier
141  *
142  * Notifies listeners that all cpus in a power domain are entering a low power
143  * state that may cause some blocks in the same power domain to reset.
144  *
145  * Must be called after cpu_pm_enter has been called on all cpus in 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  * Must be called with interrupts disabled.
152  *
153  * Return conditions are same as __raw_notifier_call_chain.
154  */
155 int cpu_cluster_pm_enter(void)
156 {
157 	return cpu_pm_notify_robust(CPU_CLUSTER_PM_ENTER, CPU_CLUSTER_PM_ENTER_FAILED);
158 }
159 EXPORT_SYMBOL_GPL(cpu_cluster_pm_enter);
160 
161 /**
162  * cpu_cluster_pm_exit - CPU cluster low power exit notifier
163  *
164  * Notifies listeners that all cpus in a power domain are exiting form a
165  * low power state that may have caused some blocks in the same power domain
166  * to reset.
167  *
168  * Must be called after cpu_cluster_pm_enter has been called for the power
169  * domain, and before cpu_pm_exit has been called on any cpu in the power
170  * domain. Notified drivers can include VFP co-processor, interrupt controller
171  * and its PM extensions, local CPU timers context save/restore which
172  * shouldn't be interrupted. Hence it must be called with interrupts disabled.
173  *
174  * Return conditions are same as __raw_notifier_call_chain.
175  */
176 int cpu_cluster_pm_exit(void)
177 {
178 	return cpu_pm_notify(CPU_CLUSTER_PM_EXIT);
179 }
180 EXPORT_SYMBOL_GPL(cpu_cluster_pm_exit);
181 
182 #ifdef CONFIG_PM
183 static int cpu_pm_suspend(void *data)
184 {
185 	int ret;
186 
187 	ret = cpu_pm_enter();
188 	if (ret)
189 		return ret;
190 
191 	ret = cpu_cluster_pm_enter();
192 	return ret;
193 }
194 
195 static void cpu_pm_resume(void *data)
196 {
197 	cpu_cluster_pm_exit();
198 	cpu_pm_exit();
199 }
200 
201 static const struct syscore_ops cpu_pm_syscore_ops = {
202 	.suspend = cpu_pm_suspend,
203 	.resume = cpu_pm_resume,
204 };
205 
206 static struct syscore cpu_pm_syscore = {
207 	.ops = &cpu_pm_syscore_ops,
208 };
209 
210 static int cpu_pm_init(void)
211 {
212 	register_syscore(&cpu_pm_syscore);
213 	return 0;
214 }
215 core_initcall(cpu_pm_init);
216 #endif
217