blob: b14df912c0a7b8ef1fb08bea7c293e287b18fd65 [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
dmitry pervushin26b69ae2016-11-23 20:00:29 +010025static DEFINE_RAW_SPINLOCK(cpu_pm_notifier_lock);
Colin Crossab100232011-02-10 02:04:45 -080026static RAW_NOTIFIER_HEAD(cpu_pm_notifier_chain);
27
28static int cpu_pm_notify(enum cpu_pm_event event, int nr_to_call, int *nr_calls)
29{
30 int ret;
31
32 ret = __raw_notifier_call_chain(&cpu_pm_notifier_chain, event, NULL,
33 nr_to_call, nr_calls);
34
35 return notifier_to_errno(ret);
36}
37
38/**
39 * cpu_pm_register_notifier - register a driver with cpu_pm
40 * @nb: notifier block to register
41 *
42 * Add a driver to a list of drivers that are notified about
43 * CPU and CPU cluster low power entry and exit.
44 *
45 * This function may sleep, and has the same return conditions as
46 * raw_notifier_chain_register.
47 */
48int cpu_pm_register_notifier(struct notifier_block *nb)
49{
50 unsigned long flags;
51 int ret;
52
dmitry pervushin452ca902016-09-02 22:54:26 +020053 raw_spin_lock_irqsave(&cpu_pm_notifier_lock, flags);
Colin Crossab100232011-02-10 02:04:45 -080054 ret = raw_notifier_chain_register(&cpu_pm_notifier_chain, nb);
dmitry pervushin452ca902016-09-02 22:54:26 +020055 raw_spin_unlock_irqrestore(&cpu_pm_notifier_lock, flags);
Colin Crossab100232011-02-10 02:04:45 -080056
57 return ret;
58}
59EXPORT_SYMBOL_GPL(cpu_pm_register_notifier);
60
61/**
62 * cpu_pm_unregister_notifier - unregister a driver with cpu_pm
63 * @nb: notifier block to be unregistered
64 *
65 * Remove a driver from the CPU PM notifier list.
66 *
67 * This function may sleep, and has the same return conditions as
68 * raw_notifier_chain_unregister.
69 */
70int cpu_pm_unregister_notifier(struct notifier_block *nb)
71{
72 unsigned long flags;
73 int ret;
74
dmitry pervushin452ca902016-09-02 22:54:26 +020075 raw_spin_lock_irqsave(&cpu_pm_notifier_lock, flags);
Colin Crossab100232011-02-10 02:04:45 -080076 ret = raw_notifier_chain_unregister(&cpu_pm_notifier_chain, nb);
dmitry pervushin452ca902016-09-02 22:54:26 +020077 raw_spin_unlock_irqrestore(&cpu_pm_notifier_lock, flags);
Colin Crossab100232011-02-10 02:04:45 -080078
79 return ret;
80}
81EXPORT_SYMBOL_GPL(cpu_pm_unregister_notifier);
82
83/**
Nicolas Pitred84970b2012-05-31 16:26:07 -070084 * cpu_pm_enter - CPU low power entry notifier
Colin Crossab100232011-02-10 02:04:45 -080085 *
86 * Notifies listeners that a single CPU is entering a low power state that may
87 * cause some blocks in the same power domain as the cpu to reset.
88 *
89 * Must be called on the affected CPU with interrupts disabled. Platform is
90 * responsible for ensuring that cpu_pm_enter is not called twice on the same
91 * CPU before cpu_pm_exit is called. Notified drivers can include VFP
Nicolas Pitred84970b2012-05-31 16:26:07 -070092 * co-processor, interrupt controller and its PM extensions, local CPU
Colin Crossab100232011-02-10 02:04:45 -080093 * timers context save/restore which shouldn't be interrupted. Hence it
94 * must be called with interrupts disabled.
95 *
96 * Return conditions are same as __raw_notifier_call_chain.
97 */
98int cpu_pm_enter(void)
99{
100 int nr_calls;
101 int ret = 0;
dmitry pervushin452ca902016-09-02 22:54:26 +0200102 unsigned long flags;
Colin Crossab100232011-02-10 02:04:45 -0800103
dmitry pervushin452ca902016-09-02 22:54:26 +0200104 raw_spin_lock_irqsave(&cpu_pm_notifier_lock, flags);
Colin Crossab100232011-02-10 02:04:45 -0800105 ret = cpu_pm_notify(CPU_PM_ENTER, -1, &nr_calls);
106 if (ret)
107 /*
108 * Inform listeners (nr_calls - 1) about failure of CPU PM
109 * PM entry who are notified earlier to prepare for it.
110 */
111 cpu_pm_notify(CPU_PM_ENTER_FAILED, nr_calls - 1, NULL);
dmitry pervushin452ca902016-09-02 22:54:26 +0200112 raw_spin_unlock_irqrestore(&cpu_pm_notifier_lock, flags);
Colin Crossab100232011-02-10 02:04:45 -0800113
114 return ret;
115}
116EXPORT_SYMBOL_GPL(cpu_pm_enter);
117
118/**
Nicolas Pitred84970b2012-05-31 16:26:07 -0700119 * cpu_pm_exit - CPU low power exit notifier
Colin Crossab100232011-02-10 02:04:45 -0800120 *
121 * Notifies listeners that a single CPU is exiting a low power state that may
122 * have caused some blocks in the same power domain as the cpu to reset.
123 *
124 * Notified drivers can include VFP co-processor, interrupt controller
Nicolas Pitred84970b2012-05-31 16:26:07 -0700125 * and its PM extensions, local CPU timers context save/restore which
Colin Crossab100232011-02-10 02:04:45 -0800126 * shouldn't be interrupted. Hence it must be called with interrupts disabled.
127 *
128 * Return conditions are same as __raw_notifier_call_chain.
129 */
130int cpu_pm_exit(void)
131{
132 int ret;
dmitry pervushin452ca902016-09-02 22:54:26 +0200133 unsigned long flags;
Colin Crossab100232011-02-10 02:04:45 -0800134
dmitry pervushin452ca902016-09-02 22:54:26 +0200135 raw_spin_lock_irqsave(&cpu_pm_notifier_lock, flags);
Colin Crossab100232011-02-10 02:04:45 -0800136 ret = cpu_pm_notify(CPU_PM_EXIT, -1, NULL);
dmitry pervushin452ca902016-09-02 22:54:26 +0200137 raw_spin_unlock_irqrestore(&cpu_pm_notifier_lock, flags);
Colin Crossab100232011-02-10 02:04:45 -0800138
139 return ret;
140}
141EXPORT_SYMBOL_GPL(cpu_pm_exit);
142
143/**
Nicolas Pitred84970b2012-05-31 16:26:07 -0700144 * cpu_cluster_pm_enter - CPU cluster low power entry notifier
Colin Crossab100232011-02-10 02:04:45 -0800145 *
146 * Notifies listeners that all cpus in a power domain are entering a low power
147 * state that may cause some blocks in the same power domain to reset.
148 *
149 * Must be called after cpu_pm_enter has been called on all cpus in the power
150 * domain, and before cpu_pm_exit has been called on any cpu in the power
151 * domain. Notified drivers can include VFP co-processor, interrupt controller
Nicolas Pitred84970b2012-05-31 16:26:07 -0700152 * and its PM extensions, local CPU timers context save/restore which
Colin Crossab100232011-02-10 02:04:45 -0800153 * shouldn't be interrupted. Hence it must be called with interrupts disabled.
154 *
155 * Must be called with interrupts disabled.
156 *
157 * Return conditions are same as __raw_notifier_call_chain.
158 */
159int cpu_cluster_pm_enter(void)
160{
161 int nr_calls;
162 int ret = 0;
dmitry pervushin452ca902016-09-02 22:54:26 +0200163 unsigned long flags;
Colin Crossab100232011-02-10 02:04:45 -0800164
dmitry pervushin452ca902016-09-02 22:54:26 +0200165 raw_spin_lock_irqsave(&cpu_pm_notifier_lock, flags);
Colin Crossab100232011-02-10 02:04:45 -0800166 ret = cpu_pm_notify(CPU_CLUSTER_PM_ENTER, -1, &nr_calls);
167 if (ret)
168 /*
169 * Inform listeners (nr_calls - 1) about failure of CPU cluster
170 * PM entry who are notified earlier to prepare for it.
171 */
172 cpu_pm_notify(CPU_CLUSTER_PM_ENTER_FAILED, nr_calls - 1, NULL);
dmitry pervushin452ca902016-09-02 22:54:26 +0200173 raw_spin_unlock_irqrestore(&cpu_pm_notifier_lock, flags);
Colin Crossab100232011-02-10 02:04:45 -0800174
175 return ret;
176}
177EXPORT_SYMBOL_GPL(cpu_cluster_pm_enter);
178
179/**
Nicolas Pitred84970b2012-05-31 16:26:07 -0700180 * cpu_cluster_pm_exit - CPU cluster low power exit notifier
Colin Crossab100232011-02-10 02:04:45 -0800181 *
182 * Notifies listeners that all cpus in a power domain are exiting form a
183 * low power state that may have caused some blocks in the same power domain
184 * to reset.
185 *
Lina Iyer21dd33b2015-09-02 16:18:57 -0600186 * Must be called after cpu_cluster_pm_enter has been called for the power
Colin Crossab100232011-02-10 02:04:45 -0800187 * domain, and before cpu_pm_exit has been called on any cpu in the power
188 * domain. Notified drivers can include VFP co-processor, interrupt controller
Nicolas Pitred84970b2012-05-31 16:26:07 -0700189 * and its PM extensions, local CPU timers context save/restore which
Colin Crossab100232011-02-10 02:04:45 -0800190 * shouldn't be interrupted. Hence it must be called with interrupts disabled.
191 *
192 * Return conditions are same as __raw_notifier_call_chain.
193 */
194int cpu_cluster_pm_exit(void)
195{
196 int ret;
dmitry pervushin452ca902016-09-02 22:54:26 +0200197 unsigned long flags;
Colin Crossab100232011-02-10 02:04:45 -0800198
dmitry pervushin452ca902016-09-02 22:54:26 +0200199 raw_spin_lock_irqsave(&cpu_pm_notifier_lock, flags);
Colin Crossab100232011-02-10 02:04:45 -0800200 ret = cpu_pm_notify(CPU_CLUSTER_PM_EXIT, -1, NULL);
dmitry pervushin452ca902016-09-02 22:54:26 +0200201 raw_spin_unlock_irqrestore(&cpu_pm_notifier_lock, flags);
Colin Crossab100232011-02-10 02:04:45 -0800202
203 return ret;
204}
205EXPORT_SYMBOL_GPL(cpu_cluster_pm_exit);
Colin Cross6f3eaec2011-07-22 14:57:09 -0700206
207#ifdef CONFIG_PM
208static int cpu_pm_suspend(void)
209{
210 int ret;
211
212 ret = cpu_pm_enter();
213 if (ret)
214 return ret;
215
216 ret = cpu_cluster_pm_enter();
217 return ret;
218}
219
220static void cpu_pm_resume(void)
221{
222 cpu_cluster_pm_exit();
223 cpu_pm_exit();
224}
225
226static struct syscore_ops cpu_pm_syscore_ops = {
227 .suspend = cpu_pm_suspend,
228 .resume = cpu_pm_resume,
229};
230
231static int cpu_pm_init(void)
232{
233 register_syscore_ops(&cpu_pm_syscore_ops);
234 return 0;
235}
236core_initcall(cpu_pm_init);
237#endif