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