xref: /openbmc/linux/kernel/cpu_pm.c (revision 9a87ffc99ec8eb8d35eed7c4f816d75f5cc9662e)
19c92ab61SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2ab10023eSColin Cross /*
3ab10023eSColin Cross  * Copyright (C) 2011 Google, Inc.
4ab10023eSColin Cross  *
5ab10023eSColin Cross  * Author:
6ab10023eSColin Cross  *	Colin Cross <ccross@android.com>
7ab10023eSColin Cross  */
8ab10023eSColin Cross 
9ab10023eSColin Cross #include <linux/kernel.h>
10ab10023eSColin Cross #include <linux/cpu_pm.h>
11ab10023eSColin Cross #include <linux/module.h>
12ab10023eSColin Cross #include <linux/notifier.h>
13ab10023eSColin Cross #include <linux/spinlock.h>
146f3eaec8SColin Cross #include <linux/syscore_ops.h>
15ab10023eSColin Cross 
16*b2f6662aSValentin Schneider /*
17*b2f6662aSValentin Schneider  * atomic_notifiers use a spinlock_t, which can block under PREEMPT_RT.
18*b2f6662aSValentin Schneider  * Notifications for cpu_pm will be issued by the idle task itself, which can
19*b2f6662aSValentin Schneider  * never block, IOW it requires using a raw_spinlock_t.
20*b2f6662aSValentin Schneider  */
21*b2f6662aSValentin Schneider static struct {
22*b2f6662aSValentin Schneider 	struct raw_notifier_head chain;
23*b2f6662aSValentin Schneider 	raw_spinlock_t lock;
24*b2f6662aSValentin Schneider } cpu_pm_notifier = {
25*b2f6662aSValentin Schneider 	.chain = RAW_NOTIFIER_INIT(cpu_pm_notifier.chain),
26*b2f6662aSValentin Schneider 	.lock  = __RAW_SPIN_LOCK_UNLOCKED(cpu_pm_notifier.lock),
27*b2f6662aSValentin Schneider };
28ab10023eSColin Cross 
cpu_pm_notify(enum cpu_pm_event event)2970d93298SPeter Zijlstra static int cpu_pm_notify(enum cpu_pm_event event)
30ab10023eSColin Cross {
31ab10023eSColin Cross 	int ret;
32ab10023eSColin Cross 
33*b2f6662aSValentin Schneider 	rcu_read_lock();
34*b2f6662aSValentin Schneider 	ret = raw_notifier_call_chain(&cpu_pm_notifier.chain, event, NULL);
35*b2f6662aSValentin Schneider 	rcu_read_unlock();
3670d93298SPeter Zijlstra 
3770d93298SPeter Zijlstra 	return notifier_to_errno(ret);
3870d93298SPeter Zijlstra }
3970d93298SPeter Zijlstra 
cpu_pm_notify_robust(enum cpu_pm_event event_up,enum cpu_pm_event event_down)4070d93298SPeter Zijlstra static int cpu_pm_notify_robust(enum cpu_pm_event event_up, enum cpu_pm_event event_down)
4170d93298SPeter Zijlstra {
42*b2f6662aSValentin Schneider 	unsigned long flags;
4370d93298SPeter Zijlstra 	int ret;
4470d93298SPeter Zijlstra 
45*b2f6662aSValentin Schneider 	raw_spin_lock_irqsave(&cpu_pm_notifier.lock, flags);
46*b2f6662aSValentin Schneider 	ret = raw_notifier_call_chain_robust(&cpu_pm_notifier.chain, event_up, event_down, NULL);
47*b2f6662aSValentin Schneider 	raw_spin_unlock_irqrestore(&cpu_pm_notifier.lock, flags);
48ab10023eSColin Cross 
49ab10023eSColin Cross 	return notifier_to_errno(ret);
50ab10023eSColin Cross }
51ab10023eSColin Cross 
52ab10023eSColin Cross /**
53ab10023eSColin Cross  * cpu_pm_register_notifier - register a driver with cpu_pm
54ab10023eSColin Cross  * @nb: notifier block to register
55ab10023eSColin Cross  *
56ab10023eSColin Cross  * Add a driver to a list of drivers that are notified about
57ab10023eSColin Cross  * CPU and CPU cluster low power entry and exit.
58ab10023eSColin Cross  *
59*b2f6662aSValentin Schneider  * This function has the same return conditions as raw_notifier_chain_register.
60ab10023eSColin Cross  */
cpu_pm_register_notifier(struct notifier_block * nb)61ab10023eSColin Cross int cpu_pm_register_notifier(struct notifier_block *nb)
62ab10023eSColin Cross {
63*b2f6662aSValentin Schneider 	unsigned long flags;
64*b2f6662aSValentin Schneider 	int ret;
65*b2f6662aSValentin Schneider 
66*b2f6662aSValentin Schneider 	raw_spin_lock_irqsave(&cpu_pm_notifier.lock, flags);
67*b2f6662aSValentin Schneider 	ret = raw_notifier_chain_register(&cpu_pm_notifier.chain, nb);
68*b2f6662aSValentin Schneider 	raw_spin_unlock_irqrestore(&cpu_pm_notifier.lock, flags);
69*b2f6662aSValentin Schneider 	return ret;
70ab10023eSColin Cross }
71ab10023eSColin Cross EXPORT_SYMBOL_GPL(cpu_pm_register_notifier);
72ab10023eSColin Cross 
73ab10023eSColin Cross /**
74ab10023eSColin Cross  * cpu_pm_unregister_notifier - unregister a driver with cpu_pm
75ab10023eSColin Cross  * @nb: notifier block to be unregistered
76ab10023eSColin Cross  *
77ab10023eSColin Cross  * Remove a driver from the CPU PM notifier list.
78ab10023eSColin Cross  *
79*b2f6662aSValentin Schneider  * This function has the same return conditions as raw_notifier_chain_unregister.
80ab10023eSColin Cross  */
cpu_pm_unregister_notifier(struct notifier_block * nb)81ab10023eSColin Cross int cpu_pm_unregister_notifier(struct notifier_block *nb)
82ab10023eSColin Cross {
83*b2f6662aSValentin Schneider 	unsigned long flags;
84*b2f6662aSValentin Schneider 	int ret;
85*b2f6662aSValentin Schneider 
86*b2f6662aSValentin Schneider 	raw_spin_lock_irqsave(&cpu_pm_notifier.lock, flags);
87*b2f6662aSValentin Schneider 	ret = raw_notifier_chain_unregister(&cpu_pm_notifier.chain, nb);
88*b2f6662aSValentin Schneider 	raw_spin_unlock_irqrestore(&cpu_pm_notifier.lock, flags);
89*b2f6662aSValentin Schneider 	return ret;
90ab10023eSColin Cross }
91ab10023eSColin Cross EXPORT_SYMBOL_GPL(cpu_pm_unregister_notifier);
92ab10023eSColin Cross 
93ab10023eSColin Cross /**
94d84970bbSNicolas Pitre  * cpu_pm_enter - CPU low power entry notifier
95ab10023eSColin Cross  *
96ab10023eSColin Cross  * Notifies listeners that a single CPU is entering a low power state that may
97ab10023eSColin Cross  * cause some blocks in the same power domain as the cpu to reset.
98ab10023eSColin Cross  *
99ab10023eSColin Cross  * Must be called on the affected CPU with interrupts disabled.  Platform is
100ab10023eSColin Cross  * responsible for ensuring that cpu_pm_enter is not called twice on the same
101ab10023eSColin Cross  * CPU before cpu_pm_exit is called. Notified drivers can include VFP
102d84970bbSNicolas Pitre  * co-processor, interrupt controller and its PM extensions, local CPU
103ab10023eSColin Cross  * timers context save/restore which shouldn't be interrupted. Hence it
104ab10023eSColin Cross  * must be called with interrupts disabled.
105ab10023eSColin Cross  *
106ab10023eSColin Cross  * Return conditions are same as __raw_notifier_call_chain.
107ab10023eSColin Cross  */
cpu_pm_enter(void)108ab10023eSColin Cross int cpu_pm_enter(void)
109ab10023eSColin Cross {
11070d93298SPeter Zijlstra 	return cpu_pm_notify_robust(CPU_PM_ENTER, CPU_PM_ENTER_FAILED);
111ab10023eSColin Cross }
112ab10023eSColin Cross EXPORT_SYMBOL_GPL(cpu_pm_enter);
113ab10023eSColin Cross 
114ab10023eSColin Cross /**
115d84970bbSNicolas Pitre  * cpu_pm_exit - CPU low power exit notifier
116ab10023eSColin Cross  *
117ab10023eSColin Cross  * Notifies listeners that a single CPU is exiting a low power state that may
118ab10023eSColin Cross  * have caused some blocks in the same power domain as the cpu to reset.
119ab10023eSColin Cross  *
120ab10023eSColin Cross  * Notified drivers can include VFP co-processor, interrupt controller
121d84970bbSNicolas Pitre  * and its PM extensions, local CPU timers context save/restore which
122ab10023eSColin Cross  * shouldn't be interrupted. Hence it must be called with interrupts disabled.
123ab10023eSColin Cross  *
124ab10023eSColin Cross  * Return conditions are same as __raw_notifier_call_chain.
125ab10023eSColin Cross  */
cpu_pm_exit(void)126ab10023eSColin Cross int cpu_pm_exit(void)
127ab10023eSColin Cross {
12870d93298SPeter Zijlstra 	return cpu_pm_notify(CPU_PM_EXIT);
129ab10023eSColin Cross }
130ab10023eSColin Cross EXPORT_SYMBOL_GPL(cpu_pm_exit);
131ab10023eSColin Cross 
132ab10023eSColin Cross /**
133d84970bbSNicolas Pitre  * cpu_cluster_pm_enter - CPU cluster low power entry notifier
134ab10023eSColin Cross  *
135ab10023eSColin Cross  * Notifies listeners that all cpus in a power domain are entering a low power
136ab10023eSColin Cross  * state that may cause some blocks in the same power domain to reset.
137ab10023eSColin Cross  *
138ab10023eSColin Cross  * Must be called after cpu_pm_enter has been called on all cpus in the power
139ab10023eSColin Cross  * domain, and before cpu_pm_exit has been called on any cpu in the power
140ab10023eSColin Cross  * domain. Notified drivers can include VFP co-processor, interrupt controller
141d84970bbSNicolas Pitre  * and its PM extensions, local CPU timers context save/restore which
142ab10023eSColin Cross  * shouldn't be interrupted. Hence it must be called with interrupts disabled.
143ab10023eSColin Cross  *
144ab10023eSColin Cross  * Must be called with interrupts disabled.
145ab10023eSColin Cross  *
146ab10023eSColin Cross  * Return conditions are same as __raw_notifier_call_chain.
147ab10023eSColin Cross  */
cpu_cluster_pm_enter(void)148ab10023eSColin Cross int cpu_cluster_pm_enter(void)
149ab10023eSColin Cross {
15070d93298SPeter Zijlstra 	return cpu_pm_notify_robust(CPU_CLUSTER_PM_ENTER, CPU_CLUSTER_PM_ENTER_FAILED);
151ab10023eSColin Cross }
152ab10023eSColin Cross EXPORT_SYMBOL_GPL(cpu_cluster_pm_enter);
153ab10023eSColin Cross 
154ab10023eSColin Cross /**
155d84970bbSNicolas Pitre  * cpu_cluster_pm_exit - CPU cluster low power exit notifier
156ab10023eSColin Cross  *
157ab10023eSColin Cross  * Notifies listeners that all cpus in a power domain are exiting form a
158ab10023eSColin Cross  * low power state that may have caused some blocks in the same power domain
159ab10023eSColin Cross  * to reset.
160ab10023eSColin Cross  *
16121dd33b0SLina Iyer  * Must be called after cpu_cluster_pm_enter has been called for the power
162ab10023eSColin Cross  * domain, and before cpu_pm_exit has been called on any cpu in the power
163ab10023eSColin Cross  * domain. Notified drivers can include VFP co-processor, interrupt controller
164d84970bbSNicolas Pitre  * and its PM extensions, local CPU timers context save/restore which
165ab10023eSColin Cross  * shouldn't be interrupted. Hence it must be called with interrupts disabled.
166ab10023eSColin Cross  *
167ab10023eSColin Cross  * Return conditions are same as __raw_notifier_call_chain.
168ab10023eSColin Cross  */
cpu_cluster_pm_exit(void)169ab10023eSColin Cross int cpu_cluster_pm_exit(void)
170ab10023eSColin Cross {
17170d93298SPeter Zijlstra 	return cpu_pm_notify(CPU_CLUSTER_PM_EXIT);
172ab10023eSColin Cross }
173ab10023eSColin Cross EXPORT_SYMBOL_GPL(cpu_cluster_pm_exit);
1746f3eaec8SColin Cross 
1756f3eaec8SColin Cross #ifdef CONFIG_PM
cpu_pm_suspend(void)1766f3eaec8SColin Cross static int cpu_pm_suspend(void)
1776f3eaec8SColin Cross {
1786f3eaec8SColin Cross 	int ret;
1796f3eaec8SColin Cross 
1806f3eaec8SColin Cross 	ret = cpu_pm_enter();
1816f3eaec8SColin Cross 	if (ret)
1826f3eaec8SColin Cross 		return ret;
1836f3eaec8SColin Cross 
1846f3eaec8SColin Cross 	ret = cpu_cluster_pm_enter();
1856f3eaec8SColin Cross 	return ret;
1866f3eaec8SColin Cross }
1876f3eaec8SColin Cross 
cpu_pm_resume(void)1886f3eaec8SColin Cross static void cpu_pm_resume(void)
1896f3eaec8SColin Cross {
1906f3eaec8SColin Cross 	cpu_cluster_pm_exit();
1916f3eaec8SColin Cross 	cpu_pm_exit();
1926f3eaec8SColin Cross }
1936f3eaec8SColin Cross 
1946f3eaec8SColin Cross static struct syscore_ops cpu_pm_syscore_ops = {
1956f3eaec8SColin Cross 	.suspend = cpu_pm_suspend,
1966f3eaec8SColin Cross 	.resume = cpu_pm_resume,
1976f3eaec8SColin Cross };
1986f3eaec8SColin Cross 
cpu_pm_init(void)1996f3eaec8SColin Cross static int cpu_pm_init(void)
2006f3eaec8SColin Cross {
2016f3eaec8SColin Cross 	register_syscore_ops(&cpu_pm_syscore_ops);
2026f3eaec8SColin Cross 	return 0;
2036f3eaec8SColin Cross }
2046f3eaec8SColin Cross core_initcall(cpu_pm_init);
2056f3eaec8SColin Cross #endif
206