Thomas Gleixner | 9c92ab6 | 2019-05-29 07:17:56 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Colin Cross | ab10023 | 2011-02-10 02:04:45 -0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2011 Google, Inc. |
| 4 | * |
| 5 | * Author: |
| 6 | * Colin Cross <ccross@android.com> |
Colin Cross | ab10023 | 2011-02-10 02:04:45 -0800 | [diff] [blame] | 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> |
Colin Cross | 6f3eaec | 2011-07-22 14:57:09 -0700 | [diff] [blame] | 14 | #include <linux/syscore_ops.h> |
Colin Cross | ab10023 | 2011-02-10 02:04:45 -0800 | [diff] [blame] | 15 | |
Valentin Schneider | b2f6662a | 2021-08-11 21:14:31 +0100 | [diff] [blame] | 16 | /* |
| 17 | * atomic_notifiers use a spinlock_t, which can block under PREEMPT_RT. |
| 18 | * Notifications for cpu_pm will be issued by the idle task itself, which can |
| 19 | * never block, IOW it requires using a raw_spinlock_t. |
| 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_notifier.chain), |
| 26 | .lock = __RAW_SPIN_LOCK_UNLOCKED(cpu_pm_notifier.lock), |
| 27 | }; |
Colin Cross | ab10023 | 2011-02-10 02:04:45 -0800 | [diff] [blame] | 28 | |
Peter Zijlstra | 70d9329 | 2020-08-18 15:57:36 +0200 | [diff] [blame] | 29 | static int cpu_pm_notify(enum cpu_pm_event event) |
Colin Cross | ab10023 | 2011-02-10 02:04:45 -0800 | [diff] [blame] | 30 | { |
| 31 | int ret; |
| 32 | |
Alex Shi | 313c8c1 | 2017-07-28 15:09:25 +0800 | [diff] [blame] | 33 | /* |
Valentin Schneider | b2f6662a | 2021-08-11 21:14:31 +0100 | [diff] [blame] | 34 | * This introduces a RCU read critical section, which could be |
| 35 | * disfunctional in cpu idle. Copy RCU_NONIDLE code to let RCU know |
| 36 | * this. |
Alex Shi | 313c8c1 | 2017-07-28 15:09:25 +0800 | [diff] [blame] | 37 | */ |
| 38 | rcu_irq_enter_irqson(); |
Valentin Schneider | b2f6662a | 2021-08-11 21:14:31 +0100 | [diff] [blame] | 39 | rcu_read_lock(); |
| 40 | ret = raw_notifier_call_chain(&cpu_pm_notifier.chain, event, NULL); |
| 41 | rcu_read_unlock(); |
Peter Zijlstra | 70d9329 | 2020-08-18 15:57:36 +0200 | [diff] [blame] | 42 | rcu_irq_exit_irqson(); |
| 43 | |
| 44 | return notifier_to_errno(ret); |
| 45 | } |
| 46 | |
| 47 | static int cpu_pm_notify_robust(enum cpu_pm_event event_up, enum cpu_pm_event event_down) |
| 48 | { |
Valentin Schneider | b2f6662a | 2021-08-11 21:14:31 +0100 | [diff] [blame] | 49 | unsigned long flags; |
Peter Zijlstra | 70d9329 | 2020-08-18 15:57:36 +0200 | [diff] [blame] | 50 | int ret; |
| 51 | |
| 52 | rcu_irq_enter_irqson(); |
Valentin Schneider | b2f6662a | 2021-08-11 21:14:31 +0100 | [diff] [blame] | 53 | raw_spin_lock_irqsave(&cpu_pm_notifier.lock, flags); |
| 54 | ret = raw_notifier_call_chain_robust(&cpu_pm_notifier.chain, event_up, event_down, NULL); |
| 55 | raw_spin_unlock_irqrestore(&cpu_pm_notifier.lock, flags); |
Alex Shi | 313c8c1 | 2017-07-28 15:09:25 +0800 | [diff] [blame] | 56 | rcu_irq_exit_irqson(); |
Colin Cross | ab10023 | 2011-02-10 02:04:45 -0800 | [diff] [blame] | 57 | |
| 58 | return notifier_to_errno(ret); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * cpu_pm_register_notifier - register a driver with cpu_pm |
| 63 | * @nb: notifier block to register |
| 64 | * |
| 65 | * Add a driver to a list of drivers that are notified about |
| 66 | * CPU and CPU cluster low power entry and exit. |
| 67 | * |
Valentin Schneider | b2f6662a | 2021-08-11 21:14:31 +0100 | [diff] [blame] | 68 | * This function has the same return conditions as raw_notifier_chain_register. |
Colin Cross | ab10023 | 2011-02-10 02:04:45 -0800 | [diff] [blame] | 69 | */ |
| 70 | int cpu_pm_register_notifier(struct notifier_block *nb) |
| 71 | { |
Valentin Schneider | b2f6662a | 2021-08-11 21:14:31 +0100 | [diff] [blame] | 72 | unsigned long flags; |
| 73 | int ret; |
| 74 | |
| 75 | raw_spin_lock_irqsave(&cpu_pm_notifier.lock, flags); |
| 76 | ret = raw_notifier_chain_register(&cpu_pm_notifier.chain, nb); |
| 77 | raw_spin_unlock_irqrestore(&cpu_pm_notifier.lock, flags); |
| 78 | return ret; |
Colin Cross | ab10023 | 2011-02-10 02:04:45 -0800 | [diff] [blame] | 79 | } |
| 80 | EXPORT_SYMBOL_GPL(cpu_pm_register_notifier); |
| 81 | |
| 82 | /** |
| 83 | * cpu_pm_unregister_notifier - unregister a driver with cpu_pm |
| 84 | * @nb: notifier block to be unregistered |
| 85 | * |
| 86 | * Remove a driver from the CPU PM notifier list. |
| 87 | * |
Valentin Schneider | b2f6662a | 2021-08-11 21:14:31 +0100 | [diff] [blame] | 88 | * This function has the same return conditions as raw_notifier_chain_unregister. |
Colin Cross | ab10023 | 2011-02-10 02:04:45 -0800 | [diff] [blame] | 89 | */ |
| 90 | int cpu_pm_unregister_notifier(struct notifier_block *nb) |
| 91 | { |
Valentin Schneider | b2f6662a | 2021-08-11 21:14:31 +0100 | [diff] [blame] | 92 | unsigned long flags; |
| 93 | int ret; |
| 94 | |
| 95 | raw_spin_lock_irqsave(&cpu_pm_notifier.lock, flags); |
| 96 | ret = raw_notifier_chain_unregister(&cpu_pm_notifier.chain, nb); |
| 97 | raw_spin_unlock_irqrestore(&cpu_pm_notifier.lock, flags); |
| 98 | return ret; |
Colin Cross | ab10023 | 2011-02-10 02:04:45 -0800 | [diff] [blame] | 99 | } |
| 100 | EXPORT_SYMBOL_GPL(cpu_pm_unregister_notifier); |
| 101 | |
| 102 | /** |
Nicolas Pitre | d84970b | 2012-05-31 16:26:07 -0700 | [diff] [blame] | 103 | * cpu_pm_enter - CPU low power entry notifier |
Colin Cross | ab10023 | 2011-02-10 02:04:45 -0800 | [diff] [blame] | 104 | * |
| 105 | * Notifies listeners that a single CPU is entering a low power state that may |
| 106 | * cause some blocks in the same power domain as the cpu to reset. |
| 107 | * |
| 108 | * Must be called on the affected CPU with interrupts disabled. Platform is |
| 109 | * responsible for ensuring that cpu_pm_enter is not called twice on the same |
| 110 | * CPU before cpu_pm_exit is called. Notified drivers can include VFP |
Nicolas Pitre | d84970b | 2012-05-31 16:26:07 -0700 | [diff] [blame] | 111 | * co-processor, interrupt controller and its PM extensions, local CPU |
Colin Cross | ab10023 | 2011-02-10 02:04:45 -0800 | [diff] [blame] | 112 | * timers context save/restore which shouldn't be interrupted. Hence it |
| 113 | * must be called with interrupts disabled. |
| 114 | * |
| 115 | * Return conditions are same as __raw_notifier_call_chain. |
| 116 | */ |
| 117 | int cpu_pm_enter(void) |
| 118 | { |
Peter Zijlstra | 70d9329 | 2020-08-18 15:57:36 +0200 | [diff] [blame] | 119 | return cpu_pm_notify_robust(CPU_PM_ENTER, CPU_PM_ENTER_FAILED); |
Colin Cross | ab10023 | 2011-02-10 02:04:45 -0800 | [diff] [blame] | 120 | } |
| 121 | EXPORT_SYMBOL_GPL(cpu_pm_enter); |
| 122 | |
| 123 | /** |
Nicolas Pitre | d84970b | 2012-05-31 16:26:07 -0700 | [diff] [blame] | 124 | * cpu_pm_exit - CPU low power exit notifier |
Colin Cross | ab10023 | 2011-02-10 02:04:45 -0800 | [diff] [blame] | 125 | * |
| 126 | * Notifies listeners that a single CPU is exiting a low power state that may |
| 127 | * have caused some blocks in the same power domain as the cpu to reset. |
| 128 | * |
| 129 | * Notified drivers can include VFP co-processor, interrupt controller |
Nicolas Pitre | d84970b | 2012-05-31 16:26:07 -0700 | [diff] [blame] | 130 | * and its PM extensions, local CPU timers context save/restore which |
Colin Cross | ab10023 | 2011-02-10 02:04:45 -0800 | [diff] [blame] | 131 | * shouldn't be interrupted. Hence it must be called with interrupts disabled. |
| 132 | * |
| 133 | * Return conditions are same as __raw_notifier_call_chain. |
| 134 | */ |
| 135 | int cpu_pm_exit(void) |
| 136 | { |
Peter Zijlstra | 70d9329 | 2020-08-18 15:57:36 +0200 | [diff] [blame] | 137 | return cpu_pm_notify(CPU_PM_EXIT); |
Colin Cross | ab10023 | 2011-02-10 02:04:45 -0800 | [diff] [blame] | 138 | } |
| 139 | EXPORT_SYMBOL_GPL(cpu_pm_exit); |
| 140 | |
| 141 | /** |
Nicolas Pitre | d84970b | 2012-05-31 16:26:07 -0700 | [diff] [blame] | 142 | * cpu_cluster_pm_enter - CPU cluster low power entry notifier |
Colin Cross | ab10023 | 2011-02-10 02:04:45 -0800 | [diff] [blame] | 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 |
Nicolas Pitre | d84970b | 2012-05-31 16:26:07 -0700 | [diff] [blame] | 150 | * and its PM extensions, local CPU timers context save/restore which |
Colin Cross | ab10023 | 2011-02-10 02:04:45 -0800 | [diff] [blame] | 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 | */ |
| 157 | int cpu_cluster_pm_enter(void) |
| 158 | { |
Peter Zijlstra | 70d9329 | 2020-08-18 15:57:36 +0200 | [diff] [blame] | 159 | return cpu_pm_notify_robust(CPU_CLUSTER_PM_ENTER, CPU_CLUSTER_PM_ENTER_FAILED); |
Colin Cross | ab10023 | 2011-02-10 02:04:45 -0800 | [diff] [blame] | 160 | } |
| 161 | EXPORT_SYMBOL_GPL(cpu_cluster_pm_enter); |
| 162 | |
| 163 | /** |
Nicolas Pitre | d84970b | 2012-05-31 16:26:07 -0700 | [diff] [blame] | 164 | * cpu_cluster_pm_exit - CPU cluster low power exit notifier |
Colin Cross | ab10023 | 2011-02-10 02:04:45 -0800 | [diff] [blame] | 165 | * |
| 166 | * Notifies listeners that all cpus in a power domain are exiting form a |
| 167 | * low power state that may have caused some blocks in the same power domain |
| 168 | * to reset. |
| 169 | * |
Lina Iyer | 21dd33b | 2015-09-02 16:18:57 -0600 | [diff] [blame] | 170 | * Must be called after cpu_cluster_pm_enter has been called for the power |
Colin Cross | ab10023 | 2011-02-10 02:04:45 -0800 | [diff] [blame] | 171 | * domain, and before cpu_pm_exit has been called on any cpu in the power |
| 172 | * domain. Notified drivers can include VFP co-processor, interrupt controller |
Nicolas Pitre | d84970b | 2012-05-31 16:26:07 -0700 | [diff] [blame] | 173 | * and its PM extensions, local CPU timers context save/restore which |
Colin Cross | ab10023 | 2011-02-10 02:04:45 -0800 | [diff] [blame] | 174 | * shouldn't be interrupted. Hence it must be called with interrupts disabled. |
| 175 | * |
| 176 | * Return conditions are same as __raw_notifier_call_chain. |
| 177 | */ |
| 178 | int cpu_cluster_pm_exit(void) |
| 179 | { |
Peter Zijlstra | 70d9329 | 2020-08-18 15:57:36 +0200 | [diff] [blame] | 180 | return cpu_pm_notify(CPU_CLUSTER_PM_EXIT); |
Colin Cross | ab10023 | 2011-02-10 02:04:45 -0800 | [diff] [blame] | 181 | } |
| 182 | EXPORT_SYMBOL_GPL(cpu_cluster_pm_exit); |
Colin Cross | 6f3eaec | 2011-07-22 14:57:09 -0700 | [diff] [blame] | 183 | |
| 184 | #ifdef CONFIG_PM |
| 185 | static int cpu_pm_suspend(void) |
| 186 | { |
| 187 | int ret; |
| 188 | |
| 189 | ret = cpu_pm_enter(); |
| 190 | if (ret) |
| 191 | return ret; |
| 192 | |
| 193 | ret = cpu_cluster_pm_enter(); |
| 194 | return ret; |
| 195 | } |
| 196 | |
| 197 | static void cpu_pm_resume(void) |
| 198 | { |
| 199 | cpu_cluster_pm_exit(); |
| 200 | cpu_pm_exit(); |
| 201 | } |
| 202 | |
| 203 | static struct syscore_ops cpu_pm_syscore_ops = { |
| 204 | .suspend = cpu_pm_suspend, |
| 205 | .resume = cpu_pm_resume, |
| 206 | }; |
| 207 | |
| 208 | static int cpu_pm_init(void) |
| 209 | { |
| 210 | register_syscore_ops(&cpu_pm_syscore_ops); |
| 211 | return 0; |
| 212 | } |
| 213 | core_initcall(cpu_pm_init); |
| 214 | #endif |