Commit | Line | Data |
---|---|---|
ab10023e CC |
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 | ||
18 | #include <linux/kernel.h> | |
19 | #include <linux/cpu_pm.h> | |
20 | #include <linux/module.h> | |
21 | #include <linux/notifier.h> | |
22 | #include <linux/spinlock.h> | |
6f3eaec8 | 23 | #include <linux/syscore_ops.h> |
ab10023e | 24 | |
313c8c16 | 25 | static ATOMIC_NOTIFIER_HEAD(cpu_pm_notifier_chain); |
ab10023e CC |
26 | |
27 | static int cpu_pm_notify(enum cpu_pm_event event, int nr_to_call, int *nr_calls) | |
28 | { | |
29 | int ret; | |
30 | ||
313c8c16 AS |
31 | /* |
32 | * __atomic_notifier_call_chain has a RCU read critical section, which | |
33 | * could be disfunctional in cpu idle. Copy RCU_NONIDLE code to let | |
34 | * RCU know this. | |
35 | */ | |
36 | rcu_irq_enter_irqson(); | |
37 | ret = __atomic_notifier_call_chain(&cpu_pm_notifier_chain, event, NULL, | |
ab10023e | 38 | nr_to_call, nr_calls); |
313c8c16 | 39 | rcu_irq_exit_irqson(); |
ab10023e CC |
40 | |
41 | return notifier_to_errno(ret); | |
42 | } | |
43 | ||
44 | /** | |
45 | * cpu_pm_register_notifier - register a driver with cpu_pm | |
46 | * @nb: notifier block to register | |
47 | * | |
48 | * Add a driver to a list of drivers that are notified about | |
49 | * CPU and CPU cluster low power entry and exit. | |
50 | * | |
51 | * This function may sleep, and has the same return conditions as | |
52 | * raw_notifier_chain_register. | |
53 | */ | |
54 | int cpu_pm_register_notifier(struct notifier_block *nb) | |
55 | { | |
313c8c16 | 56 | return atomic_notifier_chain_register(&cpu_pm_notifier_chain, nb); |
ab10023e CC |
57 | } |
58 | EXPORT_SYMBOL_GPL(cpu_pm_register_notifier); | |
59 | ||
60 | /** | |
61 | * cpu_pm_unregister_notifier - unregister a driver with cpu_pm | |
62 | * @nb: notifier block to be unregistered | |
63 | * | |
64 | * Remove a driver from the CPU PM notifier list. | |
65 | * | |
66 | * This function may sleep, and has the same return conditions as | |
67 | * raw_notifier_chain_unregister. | |
68 | */ | |
69 | int cpu_pm_unregister_notifier(struct notifier_block *nb) | |
70 | { | |
313c8c16 | 71 | return atomic_notifier_chain_unregister(&cpu_pm_notifier_chain, nb); |
ab10023e CC |
72 | } |
73 | EXPORT_SYMBOL_GPL(cpu_pm_unregister_notifier); | |
74 | ||
75 | /** | |
d84970bb | 76 | * cpu_pm_enter - CPU low power entry notifier |
ab10023e CC |
77 | * |
78 | * Notifies listeners that a single CPU is entering a low power state that may | |
79 | * cause some blocks in the same power domain as the cpu to reset. | |
80 | * | |
81 | * Must be called on the affected CPU with interrupts disabled. Platform is | |
82 | * responsible for ensuring that cpu_pm_enter is not called twice on the same | |
83 | * CPU before cpu_pm_exit is called. Notified drivers can include VFP | |
d84970bb | 84 | * co-processor, interrupt controller and its PM extensions, local CPU |
ab10023e CC |
85 | * timers context save/restore which shouldn't be interrupted. Hence it |
86 | * must be called with interrupts disabled. | |
87 | * | |
88 | * Return conditions are same as __raw_notifier_call_chain. | |
89 | */ | |
90 | int cpu_pm_enter(void) | |
91 | { | |
92 | int nr_calls; | |
93 | int ret = 0; | |
94 | ||
ab10023e CC |
95 | ret = cpu_pm_notify(CPU_PM_ENTER, -1, &nr_calls); |
96 | if (ret) | |
97 | /* | |
98 | * Inform listeners (nr_calls - 1) about failure of CPU PM | |
99 | * PM entry who are notified earlier to prepare for it. | |
100 | */ | |
101 | cpu_pm_notify(CPU_PM_ENTER_FAILED, nr_calls - 1, NULL); | |
ab10023e CC |
102 | |
103 | return ret; | |
104 | } | |
105 | EXPORT_SYMBOL_GPL(cpu_pm_enter); | |
106 | ||
15ee6980 YL |
107 | int cpu_pm_enter_pre(void) |
108 | { | |
109 | return cpu_pm_notify(CPU_PM_ENTER_PREPARE, -1, NULL); | |
110 | } | |
111 | EXPORT_SYMBOL_GPL(cpu_pm_enter_pre); | |
112 | ||
ab10023e | 113 | /** |
d84970bb | 114 | * cpu_pm_exit - CPU low power exit notifier |
ab10023e CC |
115 | * |
116 | * Notifies listeners that a single CPU is exiting a low power state that may | |
117 | * have caused some blocks in the same power domain as the cpu to reset. | |
118 | * | |
119 | * Notified drivers can include VFP co-processor, interrupt controller | |
d84970bb | 120 | * and its PM extensions, local CPU timers context save/restore which |
ab10023e CC |
121 | * shouldn't be interrupted. Hence it must be called with interrupts disabled. |
122 | * | |
123 | * Return conditions are same as __raw_notifier_call_chain. | |
124 | */ | |
125 | int cpu_pm_exit(void) | |
126 | { | |
313c8c16 | 127 | return cpu_pm_notify(CPU_PM_EXIT, -1, NULL); |
ab10023e CC |
128 | } |
129 | EXPORT_SYMBOL_GPL(cpu_pm_exit); | |
130 | ||
15ee6980 YL |
131 | int cpu_pm_exit_post(void) |
132 | { | |
133 | return cpu_pm_notify(CPU_PM_EXIT_POST, -1, NULL); | |
134 | } | |
135 | EXPORT_SYMBOL_GPL(cpu_pm_exit_post); | |
136 | ||
ab10023e | 137 | /** |
d84970bb | 138 | * cpu_cluster_pm_enter - CPU cluster low power entry notifier |
ab10023e CC |
139 | * |
140 | * Notifies listeners that all cpus in a power domain are entering a low power | |
141 | * state that may cause some blocks in the same power domain to reset. | |
142 | * | |
143 | * Must be called after cpu_pm_enter has been called on all cpus in the power | |
144 | * domain, and before cpu_pm_exit has been called on any cpu in the power | |
145 | * domain. Notified drivers can include VFP co-processor, interrupt controller | |
d84970bb | 146 | * and its PM extensions, local CPU timers context save/restore which |
ab10023e CC |
147 | * shouldn't be interrupted. Hence it must be called with interrupts disabled. |
148 | * | |
149 | * Must be called with interrupts disabled. | |
150 | * | |
151 | * Return conditions are same as __raw_notifier_call_chain. | |
152 | */ | |
153 | int cpu_cluster_pm_enter(void) | |
154 | { | |
155 | int nr_calls; | |
156 | int ret = 0; | |
157 | ||
ab10023e CC |
158 | ret = cpu_pm_notify(CPU_CLUSTER_PM_ENTER, -1, &nr_calls); |
159 | if (ret) | |
160 | /* | |
161 | * Inform listeners (nr_calls - 1) about failure of CPU cluster | |
162 | * PM entry who are notified earlier to prepare for it. | |
163 | */ | |
164 | cpu_pm_notify(CPU_CLUSTER_PM_ENTER_FAILED, nr_calls - 1, NULL); | |
ab10023e CC |
165 | |
166 | return ret; | |
167 | } | |
168 | EXPORT_SYMBOL_GPL(cpu_cluster_pm_enter); | |
169 | ||
170 | /** | |
d84970bb | 171 | * cpu_cluster_pm_exit - CPU cluster low power exit notifier |
ab10023e CC |
172 | * |
173 | * Notifies listeners that all cpus in a power domain are exiting form a | |
174 | * low power state that may have caused some blocks in the same power domain | |
175 | * to reset. | |
176 | * | |
21dd33b0 | 177 | * Must be called after cpu_cluster_pm_enter has been called for the power |
ab10023e CC |
178 | * domain, and before cpu_pm_exit has been called on any cpu in the power |
179 | * domain. Notified drivers can include VFP co-processor, interrupt controller | |
d84970bb | 180 | * and its PM extensions, local CPU timers context save/restore which |
ab10023e CC |
181 | * shouldn't be interrupted. Hence it must be called with interrupts disabled. |
182 | * | |
183 | * Return conditions are same as __raw_notifier_call_chain. | |
184 | */ | |
185 | int cpu_cluster_pm_exit(void) | |
186 | { | |
313c8c16 | 187 | return cpu_pm_notify(CPU_CLUSTER_PM_EXIT, -1, NULL); |
ab10023e CC |
188 | } |
189 | EXPORT_SYMBOL_GPL(cpu_cluster_pm_exit); | |
6f3eaec8 CC |
190 | |
191 | #ifdef CONFIG_PM | |
192 | static int cpu_pm_suspend(void) | |
193 | { | |
194 | int ret; | |
195 | ||
196 | ret = cpu_pm_enter(); | |
197 | if (ret) | |
198 | return ret; | |
199 | ||
200 | ret = cpu_cluster_pm_enter(); | |
201 | return ret; | |
202 | } | |
203 | ||
204 | static void cpu_pm_resume(void) | |
205 | { | |
206 | cpu_cluster_pm_exit(); | |
207 | cpu_pm_exit(); | |
208 | } | |
209 | ||
210 | static struct syscore_ops cpu_pm_syscore_ops = { | |
211 | .suspend = cpu_pm_suspend, | |
212 | .resume = cpu_pm_resume, | |
213 | }; | |
214 | ||
215 | static int cpu_pm_init(void) | |
216 | { | |
217 | register_syscore_ops(&cpu_pm_syscore_ops); | |
218 | return 0; | |
219 | } | |
220 | core_initcall(cpu_pm_init); | |
221 | #endif |