Linux Audio

Check our new training course

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