blob: f3c19d0bed60ec0051dd87cf5b6021de3e7e3bea [file] [log] [blame]
Colin Crossab100232011-02-10 02:04:45 -08001/*
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>
Colin Cross6f3eaec2011-07-22 14:57:09 -070023#include <linux/syscore_ops.h>
Colin Crossab100232011-02-10 02:04:45 -080024
Murali Nalajala00aaa932015-05-19 10:26:11 -070025bool from_suspend;
26
Colin Crossab100232011-02-10 02:04:45 -080027static DEFINE_RWLOCK(cpu_pm_notifier_lock);
28static RAW_NOTIFIER_HEAD(cpu_pm_notifier_chain);
29
Murali Nalajalade7c75e2015-01-07 19:36:57 -080030static int cpu_pm_notify(enum cpu_pm_event event, int nr_to_call, int *nr_calls,
31 void *data)
Colin Crossab100232011-02-10 02:04:45 -080032{
33 int ret;
34
Murali Nalajalade7c75e2015-01-07 19:36:57 -080035 ret = __raw_notifier_call_chain(&cpu_pm_notifier_chain, event, data,
Colin Crossab100232011-02-10 02:04:45 -080036 nr_to_call, nr_calls);
37
38 return notifier_to_errno(ret);
39}
40
41/**
42 * cpu_pm_register_notifier - register a driver with cpu_pm
43 * @nb: notifier block to register
44 *
45 * Add a driver to a list of drivers that are notified about
46 * CPU and CPU cluster low power entry and exit.
47 *
48 * This function may sleep, and has the same return conditions as
49 * raw_notifier_chain_register.
50 */
51int cpu_pm_register_notifier(struct notifier_block *nb)
52{
53 unsigned long flags;
54 int ret;
55
56 write_lock_irqsave(&cpu_pm_notifier_lock, flags);
57 ret = raw_notifier_chain_register(&cpu_pm_notifier_chain, nb);
58 write_unlock_irqrestore(&cpu_pm_notifier_lock, flags);
59
60 return ret;
61}
62EXPORT_SYMBOL_GPL(cpu_pm_register_notifier);
63
64/**
65 * cpu_pm_unregister_notifier - unregister a driver with cpu_pm
66 * @nb: notifier block to be unregistered
67 *
68 * Remove a driver from the CPU PM notifier list.
69 *
70 * This function may sleep, and has the same return conditions as
71 * raw_notifier_chain_unregister.
72 */
73int cpu_pm_unregister_notifier(struct notifier_block *nb)
74{
75 unsigned long flags;
76 int ret;
77
78 write_lock_irqsave(&cpu_pm_notifier_lock, flags);
79 ret = raw_notifier_chain_unregister(&cpu_pm_notifier_chain, nb);
80 write_unlock_irqrestore(&cpu_pm_notifier_lock, flags);
81
82 return ret;
83}
84EXPORT_SYMBOL_GPL(cpu_pm_unregister_notifier);
85
86/**
Nicolas Pitred84970b2012-05-31 16:26:07 -070087 * cpu_pm_enter - CPU low power entry notifier
Colin Crossab100232011-02-10 02:04:45 -080088 *
89 * Notifies listeners that a single CPU is entering a low power state that may
90 * cause some blocks in the same power domain as the cpu to reset.
91 *
92 * Must be called on the affected CPU with interrupts disabled. Platform is
93 * responsible for ensuring that cpu_pm_enter is not called twice on the same
94 * CPU before cpu_pm_exit is called. Notified drivers can include VFP
Nicolas Pitred84970b2012-05-31 16:26:07 -070095 * co-processor, interrupt controller and its PM extensions, local CPU
Colin Crossab100232011-02-10 02:04:45 -080096 * timers context save/restore which shouldn't be interrupted. Hence it
97 * must be called with interrupts disabled.
98 *
99 * Return conditions are same as __raw_notifier_call_chain.
100 */
101int cpu_pm_enter(void)
102{
103 int nr_calls;
104 int ret = 0;
105
106 read_lock(&cpu_pm_notifier_lock);
Murali Nalajalade7c75e2015-01-07 19:36:57 -0800107 ret = cpu_pm_notify(CPU_PM_ENTER, -1, &nr_calls, NULL);
Colin Crossab100232011-02-10 02:04:45 -0800108 if (ret)
109 /*
110 * Inform listeners (nr_calls - 1) about failure of CPU PM
111 * PM entry who are notified earlier to prepare for it.
112 */
Murali Nalajalade7c75e2015-01-07 19:36:57 -0800113 cpu_pm_notify(CPU_PM_ENTER_FAILED, nr_calls - 1, NULL, NULL);
Colin Crossab100232011-02-10 02:04:45 -0800114 read_unlock(&cpu_pm_notifier_lock);
115
116 return ret;
117}
118EXPORT_SYMBOL_GPL(cpu_pm_enter);
119
120/**
Nicolas Pitred84970b2012-05-31 16:26:07 -0700121 * cpu_pm_exit - CPU low power exit notifier
Colin Crossab100232011-02-10 02:04:45 -0800122 *
123 * Notifies listeners that a single CPU is exiting a low power state that may
124 * have caused some blocks in the same power domain as the cpu to reset.
125 *
126 * Notified drivers can include VFP co-processor, interrupt controller
Nicolas Pitred84970b2012-05-31 16:26:07 -0700127 * and its PM extensions, local CPU timers context save/restore which
Colin Crossab100232011-02-10 02:04:45 -0800128 * shouldn't be interrupted. Hence it must be called with interrupts disabled.
129 *
130 * Return conditions are same as __raw_notifier_call_chain.
131 */
132int cpu_pm_exit(void)
133{
134 int ret;
135
136 read_lock(&cpu_pm_notifier_lock);
Murali Nalajalade7c75e2015-01-07 19:36:57 -0800137 ret = cpu_pm_notify(CPU_PM_EXIT, -1, NULL, NULL);
Colin Crossab100232011-02-10 02:04:45 -0800138 read_unlock(&cpu_pm_notifier_lock);
139
140 return ret;
141}
142EXPORT_SYMBOL_GPL(cpu_pm_exit);
143
144/**
Nicolas Pitred84970b2012-05-31 16:26:07 -0700145 * cpu_cluster_pm_enter - CPU cluster low power entry notifier
Colin Crossab100232011-02-10 02:04:45 -0800146 *
147 * Notifies listeners that all cpus in a power domain are entering a low power
148 * state that may cause some blocks in the same power domain to reset.
149 *
150 * Must be called after cpu_pm_enter has been called on all cpus in the power
151 * domain, and before cpu_pm_exit has been called on any cpu in the power
152 * domain. Notified drivers can include VFP co-processor, interrupt controller
Nicolas Pitred84970b2012-05-31 16:26:07 -0700153 * and its PM extensions, local CPU timers context save/restore which
Colin Crossab100232011-02-10 02:04:45 -0800154 * shouldn't be interrupted. Hence it must be called with interrupts disabled.
155 *
156 * Must be called with interrupts disabled.
157 *
158 * Return conditions are same as __raw_notifier_call_chain.
159 */
Murali Nalajalade7c75e2015-01-07 19:36:57 -0800160int cpu_cluster_pm_enter(unsigned long aff_level)
Colin Crossab100232011-02-10 02:04:45 -0800161{
162 int nr_calls;
163 int ret = 0;
164
165 read_lock(&cpu_pm_notifier_lock);
Murali Nalajalade7c75e2015-01-07 19:36:57 -0800166 ret = cpu_pm_notify(CPU_CLUSTER_PM_ENTER, -1, &nr_calls,
167 (void *) aff_level);
Colin Crossab100232011-02-10 02:04:45 -0800168 if (ret)
169 /*
170 * Inform listeners (nr_calls - 1) about failure of CPU cluster
171 * PM entry who are notified earlier to prepare for it.
172 */
Murali Nalajalade7c75e2015-01-07 19:36:57 -0800173 cpu_pm_notify(CPU_CLUSTER_PM_ENTER_FAILED, nr_calls - 1, NULL,
174 (void *) aff_level);
Colin Crossab100232011-02-10 02:04:45 -0800175 read_unlock(&cpu_pm_notifier_lock);
176
177 return ret;
178}
179EXPORT_SYMBOL_GPL(cpu_cluster_pm_enter);
180
181/**
Nicolas Pitred84970b2012-05-31 16:26:07 -0700182 * cpu_cluster_pm_exit - CPU cluster low power exit notifier
Colin Crossab100232011-02-10 02:04:45 -0800183 *
184 * Notifies listeners that all cpus in a power domain are exiting form a
185 * low power state that may have caused some blocks in the same power domain
186 * to reset.
187 *
Lina Iyer21dd33b2015-09-02 16:18:57 -0600188 * Must be called after cpu_cluster_pm_enter has been called for the power
Colin Crossab100232011-02-10 02:04:45 -0800189 * domain, and before cpu_pm_exit has been called on any cpu in the power
190 * domain. Notified drivers can include VFP co-processor, interrupt controller
Nicolas Pitred84970b2012-05-31 16:26:07 -0700191 * and its PM extensions, local CPU timers context save/restore which
Colin Crossab100232011-02-10 02:04:45 -0800192 * shouldn't be interrupted. Hence it must be called with interrupts disabled.
193 *
194 * Return conditions are same as __raw_notifier_call_chain.
195 */
Murali Nalajalade7c75e2015-01-07 19:36:57 -0800196int cpu_cluster_pm_exit(unsigned long aff_level)
Colin Crossab100232011-02-10 02:04:45 -0800197{
198 int ret;
199
200 read_lock(&cpu_pm_notifier_lock);
Murali Nalajalade7c75e2015-01-07 19:36:57 -0800201 ret = cpu_pm_notify(CPU_CLUSTER_PM_EXIT, -1, NULL, (void *) aff_level);
Colin Crossab100232011-02-10 02:04:45 -0800202 read_unlock(&cpu_pm_notifier_lock);
203
204 return ret;
205}
206EXPORT_SYMBOL_GPL(cpu_cluster_pm_exit);
Colin Cross6f3eaec2011-07-22 14:57:09 -0700207
208#ifdef CONFIG_PM
209static int cpu_pm_suspend(void)
210{
211 int ret;
212
Murali Nalajala00aaa932015-05-19 10:26:11 -0700213 from_suspend = true;
Colin Cross6f3eaec2011-07-22 14:57:09 -0700214 ret = cpu_pm_enter();
215 if (ret)
216 return ret;
217
Murali Nalajalade7c75e2015-01-07 19:36:57 -0800218 ret = cpu_cluster_pm_enter(0);
Colin Cross6f3eaec2011-07-22 14:57:09 -0700219 return ret;
220}
221
222static void cpu_pm_resume(void)
223{
Murali Nalajala00aaa932015-05-19 10:26:11 -0700224 from_suspend = false;
Murali Nalajalade7c75e2015-01-07 19:36:57 -0800225 cpu_cluster_pm_exit(0);
Colin Cross6f3eaec2011-07-22 14:57:09 -0700226 cpu_pm_exit();
227}
228
229static struct syscore_ops cpu_pm_syscore_ops = {
230 .suspend = cpu_pm_suspend,
231 .resume = cpu_pm_resume,
232};
233
234static int cpu_pm_init(void)
235{
236 register_syscore_ops(&cpu_pm_syscore_ops);
237 return 0;
238}
239core_initcall(cpu_pm_init);
240#endif