1 // SPDX-License-Identifier: GPL-2.0-only << 2 /* 1 /* 3 * Copyright (C) 2011 Google, Inc. 2 * Copyright (C) 2011 Google, Inc. 4 * 3 * 5 * Author: 4 * Author: 6 * Colin Cross <ccross@android.com> 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 * 7 */ 16 */ 8 17 9 #include <linux/kernel.h> 18 #include <linux/kernel.h> 10 #include <linux/cpu_pm.h> 19 #include <linux/cpu_pm.h> 11 #include <linux/module.h> 20 #include <linux/module.h> 12 #include <linux/notifier.h> 21 #include <linux/notifier.h> 13 #include <linux/spinlock.h> 22 #include <linux/spinlock.h> 14 #include <linux/syscore_ops.h> 23 #include <linux/syscore_ops.h> 15 24 16 /* !! 25 static ATOMIC_NOTIFIER_HEAD(cpu_pm_notifier_chain); 17 * atomic_notifiers use a spinlock_t, which ca << 18 * Notifications for cpu_pm will be issued by << 19 * never block, IOW it requires using a raw_sp << 20 */ << 21 static struct { << 22 struct raw_notifier_head chain; << 23 raw_spinlock_t lock; << 24 } cpu_pm_notifier = { << 25 .chain = RAW_NOTIFIER_INIT(cpu_pm_noti << 26 .lock = __RAW_SPIN_LOCK_UNLOCKED(cpu_ << 27 }; << 28 26 29 static int cpu_pm_notify(enum cpu_pm_event eve !! 27 static int cpu_pm_notify(enum cpu_pm_event event, int nr_to_call, int *nr_calls) 30 { 28 { 31 int ret; 29 int ret; 32 30 33 rcu_read_lock(); !! 31 /* 34 ret = raw_notifier_call_chain(&cpu_pm_ !! 32 * __atomic_notifier_call_chain has a RCU read critical section, which 35 rcu_read_unlock(); !! 33 * could be disfunctional in cpu idle. Copy RCU_NONIDLE code to let 36 !! 34 * RCU know this. 37 return notifier_to_errno(ret); !! 35 */ 38 } !! 36 rcu_irq_enter_irqson(); 39 !! 37 ret = __atomic_notifier_call_chain(&cpu_pm_notifier_chain, event, NULL, 40 static int cpu_pm_notify_robust(enum cpu_pm_ev !! 38 nr_to_call, nr_calls); 41 { !! 39 rcu_irq_exit_irqson(); 42 unsigned long flags; << 43 int ret; << 44 << 45 raw_spin_lock_irqsave(&cpu_pm_notifier << 46 ret = raw_notifier_call_chain_robust(& << 47 raw_spin_unlock_irqrestore(&cpu_pm_not << 48 40 49 return notifier_to_errno(ret); 41 return notifier_to_errno(ret); 50 } 42 } 51 43 52 /** 44 /** 53 * cpu_pm_register_notifier - register a drive 45 * cpu_pm_register_notifier - register a driver with cpu_pm 54 * @nb: notifier block to register 46 * @nb: notifier block to register 55 * 47 * 56 * Add a driver to a list of drivers that are 48 * Add a driver to a list of drivers that are notified about 57 * CPU and CPU cluster low power entry and exi 49 * CPU and CPU cluster low power entry and exit. 58 * 50 * 59 * This function has the same return condition !! 51 * This function may sleep, and has the same return conditions as >> 52 * raw_notifier_chain_register. 60 */ 53 */ 61 int cpu_pm_register_notifier(struct notifier_b 54 int cpu_pm_register_notifier(struct notifier_block *nb) 62 { 55 { 63 unsigned long flags; !! 56 return atomic_notifier_chain_register(&cpu_pm_notifier_chain, nb); 64 int ret; << 65 << 66 raw_spin_lock_irqsave(&cpu_pm_notifier << 67 ret = raw_notifier_chain_register(&cpu << 68 raw_spin_unlock_irqrestore(&cpu_pm_not << 69 return ret; << 70 } 57 } 71 EXPORT_SYMBOL_GPL(cpu_pm_register_notifier); 58 EXPORT_SYMBOL_GPL(cpu_pm_register_notifier); 72 59 73 /** 60 /** 74 * cpu_pm_unregister_notifier - unregister a d 61 * cpu_pm_unregister_notifier - unregister a driver with cpu_pm 75 * @nb: notifier block to be unregistered 62 * @nb: notifier block to be unregistered 76 * 63 * 77 * Remove a driver from the CPU PM notifier li 64 * Remove a driver from the CPU PM notifier list. 78 * 65 * 79 * This function has the same return condition !! 66 * This function may sleep, and has the same return conditions as >> 67 * raw_notifier_chain_unregister. 80 */ 68 */ 81 int cpu_pm_unregister_notifier(struct notifier 69 int cpu_pm_unregister_notifier(struct notifier_block *nb) 82 { 70 { 83 unsigned long flags; !! 71 return atomic_notifier_chain_unregister(&cpu_pm_notifier_chain, nb); 84 int ret; << 85 << 86 raw_spin_lock_irqsave(&cpu_pm_notifier << 87 ret = raw_notifier_chain_unregister(&c << 88 raw_spin_unlock_irqrestore(&cpu_pm_not << 89 return ret; << 90 } 72 } 91 EXPORT_SYMBOL_GPL(cpu_pm_unregister_notifier); 73 EXPORT_SYMBOL_GPL(cpu_pm_unregister_notifier); 92 74 93 /** 75 /** 94 * cpu_pm_enter - CPU low power entry notifier 76 * cpu_pm_enter - CPU low power entry notifier 95 * 77 * 96 * Notifies listeners that a single CPU is ent 78 * Notifies listeners that a single CPU is entering a low power state that may 97 * cause some blocks in the same power domain 79 * cause some blocks in the same power domain as the cpu to reset. 98 * 80 * 99 * Must be called on the affected CPU with int 81 * Must be called on the affected CPU with interrupts disabled. Platform is 100 * responsible for ensuring that cpu_pm_enter 82 * responsible for ensuring that cpu_pm_enter is not called twice on the same 101 * CPU before cpu_pm_exit is called. Notified 83 * CPU before cpu_pm_exit is called. Notified drivers can include VFP 102 * co-processor, interrupt controller and its 84 * co-processor, interrupt controller and its PM extensions, local CPU 103 * timers context save/restore which shouldn't 85 * timers context save/restore which shouldn't be interrupted. Hence it 104 * must be called with interrupts disabled. 86 * must be called with interrupts disabled. 105 * 87 * 106 * Return conditions are same as __raw_notifie 88 * Return conditions are same as __raw_notifier_call_chain. 107 */ 89 */ 108 int cpu_pm_enter(void) 90 int cpu_pm_enter(void) 109 { 91 { 110 return cpu_pm_notify_robust(CPU_PM_ENT !! 92 int nr_calls; >> 93 int ret = 0; >> 94 >> 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); >> 102 >> 103 return ret; 111 } 104 } 112 EXPORT_SYMBOL_GPL(cpu_pm_enter); 105 EXPORT_SYMBOL_GPL(cpu_pm_enter); 113 106 114 /** 107 /** 115 * cpu_pm_exit - CPU low power exit notifier 108 * cpu_pm_exit - CPU low power exit notifier 116 * 109 * 117 * Notifies listeners that a single CPU is exi 110 * Notifies listeners that a single CPU is exiting a low power state that may 118 * have caused some blocks in the same power d 111 * have caused some blocks in the same power domain as the cpu to reset. 119 * 112 * 120 * Notified drivers can include VFP co-process 113 * Notified drivers can include VFP co-processor, interrupt controller 121 * and its PM extensions, local CPU timers con 114 * and its PM extensions, local CPU timers context save/restore which 122 * shouldn't be interrupted. Hence it must be 115 * shouldn't be interrupted. Hence it must be called with interrupts disabled. 123 * 116 * 124 * Return conditions are same as __raw_notifie 117 * Return conditions are same as __raw_notifier_call_chain. 125 */ 118 */ 126 int cpu_pm_exit(void) 119 int cpu_pm_exit(void) 127 { 120 { 128 return cpu_pm_notify(CPU_PM_EXIT); !! 121 return cpu_pm_notify(CPU_PM_EXIT, -1, NULL); 129 } 122 } 130 EXPORT_SYMBOL_GPL(cpu_pm_exit); 123 EXPORT_SYMBOL_GPL(cpu_pm_exit); 131 124 132 /** 125 /** 133 * cpu_cluster_pm_enter - CPU cluster low powe 126 * cpu_cluster_pm_enter - CPU cluster low power entry notifier 134 * 127 * 135 * Notifies listeners that all cpus in a power 128 * Notifies listeners that all cpus in a power domain are entering a low power 136 * state that may cause some blocks in the sam 129 * state that may cause some blocks in the same power domain to reset. 137 * 130 * 138 * Must be called after cpu_pm_enter has been 131 * Must be called after cpu_pm_enter has been called on all cpus in the power 139 * domain, and before cpu_pm_exit has been cal 132 * domain, and before cpu_pm_exit has been called on any cpu in the power 140 * domain. Notified drivers can include VFP co 133 * domain. Notified drivers can include VFP co-processor, interrupt controller 141 * and its PM extensions, local CPU timers con 134 * and its PM extensions, local CPU timers context save/restore which 142 * shouldn't be interrupted. Hence it must be 135 * shouldn't be interrupted. Hence it must be called with interrupts disabled. 143 * 136 * 144 * Must be called with interrupts disabled. 137 * Must be called with interrupts disabled. 145 * 138 * 146 * Return conditions are same as __raw_notifie 139 * Return conditions are same as __raw_notifier_call_chain. 147 */ 140 */ 148 int cpu_cluster_pm_enter(void) 141 int cpu_cluster_pm_enter(void) 149 { 142 { 150 return cpu_pm_notify_robust(CPU_CLUSTE !! 143 int nr_calls; >> 144 int ret = 0; >> 145 >> 146 ret = cpu_pm_notify(CPU_CLUSTER_PM_ENTER, -1, &nr_calls); >> 147 if (ret) >> 148 /* >> 149 * Inform listeners (nr_calls - 1) about failure of CPU cluster >> 150 * PM entry who are notified earlier to prepare for it. >> 151 */ >> 152 cpu_pm_notify(CPU_CLUSTER_PM_ENTER_FAILED, nr_calls - 1, NULL); >> 153 >> 154 return ret; 151 } 155 } 152 EXPORT_SYMBOL_GPL(cpu_cluster_pm_enter); 156 EXPORT_SYMBOL_GPL(cpu_cluster_pm_enter); 153 157 154 /** 158 /** 155 * cpu_cluster_pm_exit - CPU cluster low power 159 * cpu_cluster_pm_exit - CPU cluster low power exit notifier 156 * 160 * 157 * Notifies listeners that all cpus in a power 161 * Notifies listeners that all cpus in a power domain are exiting form a 158 * low power state that may have caused some b 162 * low power state that may have caused some blocks in the same power domain 159 * to reset. 163 * to reset. 160 * 164 * 161 * Must be called after cpu_cluster_pm_enter h 165 * Must be called after cpu_cluster_pm_enter has been called for the power 162 * domain, and before cpu_pm_exit has been cal 166 * domain, and before cpu_pm_exit has been called on any cpu in the power 163 * domain. Notified drivers can include VFP co 167 * domain. Notified drivers can include VFP co-processor, interrupt controller 164 * and its PM extensions, local CPU timers con 168 * and its PM extensions, local CPU timers context save/restore which 165 * shouldn't be interrupted. Hence it must be 169 * shouldn't be interrupted. Hence it must be called with interrupts disabled. 166 * 170 * 167 * Return conditions are same as __raw_notifie 171 * Return conditions are same as __raw_notifier_call_chain. 168 */ 172 */ 169 int cpu_cluster_pm_exit(void) 173 int cpu_cluster_pm_exit(void) 170 { 174 { 171 return cpu_pm_notify(CPU_CLUSTER_PM_EX !! 175 return cpu_pm_notify(CPU_CLUSTER_PM_EXIT, -1, NULL); 172 } 176 } 173 EXPORT_SYMBOL_GPL(cpu_cluster_pm_exit); 177 EXPORT_SYMBOL_GPL(cpu_cluster_pm_exit); 174 178 175 #ifdef CONFIG_PM 179 #ifdef CONFIG_PM 176 static int cpu_pm_suspend(void) 180 static int cpu_pm_suspend(void) 177 { 181 { 178 int ret; 182 int ret; 179 183 180 ret = cpu_pm_enter(); 184 ret = cpu_pm_enter(); 181 if (ret) 185 if (ret) 182 return ret; 186 return ret; 183 187 184 ret = cpu_cluster_pm_enter(); 188 ret = cpu_cluster_pm_enter(); 185 return ret; 189 return ret; 186 } 190 } 187 191 188 static void cpu_pm_resume(void) 192 static void cpu_pm_resume(void) 189 { 193 { 190 cpu_cluster_pm_exit(); 194 cpu_cluster_pm_exit(); 191 cpu_pm_exit(); 195 cpu_pm_exit(); 192 } 196 } 193 197 194 static struct syscore_ops cpu_pm_syscore_ops = 198 static struct syscore_ops cpu_pm_syscore_ops = { 195 .suspend = cpu_pm_suspend, 199 .suspend = cpu_pm_suspend, 196 .resume = cpu_pm_resume, 200 .resume = cpu_pm_resume, 197 }; 201 }; 198 202 199 static int cpu_pm_init(void) 203 static int cpu_pm_init(void) 200 { 204 { 201 register_syscore_ops(&cpu_pm_syscore_o 205 register_syscore_ops(&cpu_pm_syscore_ops); 202 return 0; 206 return 0; 203 } 207 } 204 core_initcall(cpu_pm_init); 208 core_initcall(cpu_pm_init); 205 #endif 209 #endif 206 210
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.