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 | |
Alex Shi | 313c8c1 | 2017-07-28 15:09:25 +0800 | [diff] [blame] | 16 | static ATOMIC_NOTIFIER_HEAD(cpu_pm_notifier_chain); |
Colin Cross | ab10023 | 2011-02-10 02:04:45 -0800 | [diff] [blame] | 17 | |
Peter Zijlstra | 70d9329 | 2020-08-18 15:57:36 +0200 | [diff] [blame^] | 18 | static int cpu_pm_notify(enum cpu_pm_event event) |
Colin Cross | ab10023 | 2011-02-10 02:04:45 -0800 | [diff] [blame] | 19 | { |
| 20 | int ret; |
| 21 | |
Alex Shi | 313c8c1 | 2017-07-28 15:09:25 +0800 | [diff] [blame] | 22 | /* |
Peter Zijlstra | 70d9329 | 2020-08-18 15:57:36 +0200 | [diff] [blame^] | 23 | * atomic_notifier_call_chain has a RCU read critical section, which |
Alex Shi | 313c8c1 | 2017-07-28 15:09:25 +0800 | [diff] [blame] | 24 | * could be disfunctional in cpu idle. Copy RCU_NONIDLE code to let |
| 25 | * RCU know this. |
| 26 | */ |
| 27 | rcu_irq_enter_irqson(); |
Peter Zijlstra | 70d9329 | 2020-08-18 15:57:36 +0200 | [diff] [blame^] | 28 | ret = atomic_notifier_call_chain(&cpu_pm_notifier_chain, event, NULL); |
| 29 | rcu_irq_exit_irqson(); |
| 30 | |
| 31 | return notifier_to_errno(ret); |
| 32 | } |
| 33 | |
| 34 | static int cpu_pm_notify_robust(enum cpu_pm_event event_up, enum cpu_pm_event event_down) |
| 35 | { |
| 36 | int ret; |
| 37 | |
| 38 | rcu_irq_enter_irqson(); |
| 39 | ret = atomic_notifier_call_chain_robust(&cpu_pm_notifier_chain, event_up, event_down, NULL); |
Alex Shi | 313c8c1 | 2017-07-28 15:09:25 +0800 | [diff] [blame] | 40 | rcu_irq_exit_irqson(); |
Colin Cross | ab10023 | 2011-02-10 02:04:45 -0800 | [diff] [blame] | 41 | |
| 42 | return notifier_to_errno(ret); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * cpu_pm_register_notifier - register a driver with cpu_pm |
| 47 | * @nb: notifier block to register |
| 48 | * |
| 49 | * Add a driver to a list of drivers that are notified about |
| 50 | * CPU and CPU cluster low power entry and exit. |
| 51 | * |
| 52 | * This function may sleep, and has the same return conditions as |
| 53 | * raw_notifier_chain_register. |
| 54 | */ |
| 55 | int cpu_pm_register_notifier(struct notifier_block *nb) |
| 56 | { |
Alex Shi | 313c8c1 | 2017-07-28 15:09:25 +0800 | [diff] [blame] | 57 | return atomic_notifier_chain_register(&cpu_pm_notifier_chain, nb); |
Colin Cross | ab10023 | 2011-02-10 02:04:45 -0800 | [diff] [blame] | 58 | } |
| 59 | EXPORT_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 | */ |
| 70 | int cpu_pm_unregister_notifier(struct notifier_block *nb) |
| 71 | { |
Alex Shi | 313c8c1 | 2017-07-28 15:09:25 +0800 | [diff] [blame] | 72 | return atomic_notifier_chain_unregister(&cpu_pm_notifier_chain, nb); |
Colin Cross | ab10023 | 2011-02-10 02:04:45 -0800 | [diff] [blame] | 73 | } |
| 74 | EXPORT_SYMBOL_GPL(cpu_pm_unregister_notifier); |
| 75 | |
| 76 | /** |
Nicolas Pitre | d84970b | 2012-05-31 16:26:07 -0700 | [diff] [blame] | 77 | * cpu_pm_enter - CPU low power entry notifier |
Colin Cross | ab10023 | 2011-02-10 02:04:45 -0800 | [diff] [blame] | 78 | * |
| 79 | * Notifies listeners that a single CPU is entering a low power state that may |
| 80 | * cause some blocks in the same power domain as the cpu to reset. |
| 81 | * |
| 82 | * Must be called on the affected CPU with interrupts disabled. Platform is |
| 83 | * responsible for ensuring that cpu_pm_enter is not called twice on the same |
| 84 | * CPU before cpu_pm_exit is called. Notified drivers can include VFP |
Nicolas Pitre | d84970b | 2012-05-31 16:26:07 -0700 | [diff] [blame] | 85 | * co-processor, interrupt controller and its PM extensions, local CPU |
Colin Cross | ab10023 | 2011-02-10 02:04:45 -0800 | [diff] [blame] | 86 | * timers context save/restore which shouldn't be interrupted. Hence it |
| 87 | * must be called with interrupts disabled. |
| 88 | * |
| 89 | * Return conditions are same as __raw_notifier_call_chain. |
| 90 | */ |
| 91 | int cpu_pm_enter(void) |
| 92 | { |
Peter Zijlstra | 70d9329 | 2020-08-18 15:57:36 +0200 | [diff] [blame^] | 93 | return cpu_pm_notify_robust(CPU_PM_ENTER, CPU_PM_ENTER_FAILED); |
Colin Cross | ab10023 | 2011-02-10 02:04:45 -0800 | [diff] [blame] | 94 | } |
| 95 | EXPORT_SYMBOL_GPL(cpu_pm_enter); |
| 96 | |
| 97 | /** |
Nicolas Pitre | d84970b | 2012-05-31 16:26:07 -0700 | [diff] [blame] | 98 | * cpu_pm_exit - CPU low power exit notifier |
Colin Cross | ab10023 | 2011-02-10 02:04:45 -0800 | [diff] [blame] | 99 | * |
| 100 | * Notifies listeners that a single CPU is exiting a low power state that may |
| 101 | * have caused some blocks in the same power domain as the cpu to reset. |
| 102 | * |
| 103 | * Notified drivers can include VFP co-processor, interrupt controller |
Nicolas Pitre | d84970b | 2012-05-31 16:26:07 -0700 | [diff] [blame] | 104 | * and its PM extensions, local CPU timers context save/restore which |
Colin Cross | ab10023 | 2011-02-10 02:04:45 -0800 | [diff] [blame] | 105 | * shouldn't be interrupted. Hence it must be called with interrupts disabled. |
| 106 | * |
| 107 | * Return conditions are same as __raw_notifier_call_chain. |
| 108 | */ |
| 109 | int cpu_pm_exit(void) |
| 110 | { |
Peter Zijlstra | 70d9329 | 2020-08-18 15:57:36 +0200 | [diff] [blame^] | 111 | return cpu_pm_notify(CPU_PM_EXIT); |
Colin Cross | ab10023 | 2011-02-10 02:04:45 -0800 | [diff] [blame] | 112 | } |
| 113 | EXPORT_SYMBOL_GPL(cpu_pm_exit); |
| 114 | |
| 115 | /** |
Nicolas Pitre | d84970b | 2012-05-31 16:26:07 -0700 | [diff] [blame] | 116 | * cpu_cluster_pm_enter - CPU cluster low power entry notifier |
Colin Cross | ab10023 | 2011-02-10 02:04:45 -0800 | [diff] [blame] | 117 | * |
| 118 | * Notifies listeners that all cpus in a power domain are entering a low power |
| 119 | * state that may cause some blocks in the same power domain to reset. |
| 120 | * |
| 121 | * Must be called after cpu_pm_enter has been called on all cpus in the power |
| 122 | * domain, and before cpu_pm_exit has been called on any cpu in the power |
| 123 | * domain. Notified drivers can include VFP co-processor, interrupt controller |
Nicolas Pitre | d84970b | 2012-05-31 16:26:07 -0700 | [diff] [blame] | 124 | * and its PM extensions, local CPU timers context save/restore which |
Colin Cross | ab10023 | 2011-02-10 02:04:45 -0800 | [diff] [blame] | 125 | * shouldn't be interrupted. Hence it must be called with interrupts disabled. |
| 126 | * |
| 127 | * Must be called with interrupts disabled. |
| 128 | * |
| 129 | * Return conditions are same as __raw_notifier_call_chain. |
| 130 | */ |
| 131 | int cpu_cluster_pm_enter(void) |
| 132 | { |
Peter Zijlstra | 70d9329 | 2020-08-18 15:57:36 +0200 | [diff] [blame^] | 133 | 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] | 134 | } |
| 135 | EXPORT_SYMBOL_GPL(cpu_cluster_pm_enter); |
| 136 | |
| 137 | /** |
Nicolas Pitre | d84970b | 2012-05-31 16:26:07 -0700 | [diff] [blame] | 138 | * cpu_cluster_pm_exit - CPU cluster low power exit notifier |
Colin Cross | ab10023 | 2011-02-10 02:04:45 -0800 | [diff] [blame] | 139 | * |
| 140 | * Notifies listeners that all cpus in a power domain are exiting form a |
| 141 | * low power state that may have caused some blocks in the same power domain |
| 142 | * to reset. |
| 143 | * |
Lina Iyer | 21dd33b | 2015-09-02 16:18:57 -0600 | [diff] [blame] | 144 | * 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] | 145 | * domain, and before cpu_pm_exit has been called on any cpu in the power |
| 146 | * domain. Notified drivers can include VFP co-processor, interrupt controller |
Nicolas Pitre | d84970b | 2012-05-31 16:26:07 -0700 | [diff] [blame] | 147 | * and its PM extensions, local CPU timers context save/restore which |
Colin Cross | ab10023 | 2011-02-10 02:04:45 -0800 | [diff] [blame] | 148 | * shouldn't be interrupted. Hence it must be called with interrupts disabled. |
| 149 | * |
| 150 | * Return conditions are same as __raw_notifier_call_chain. |
| 151 | */ |
| 152 | int cpu_cluster_pm_exit(void) |
| 153 | { |
Peter Zijlstra | 70d9329 | 2020-08-18 15:57:36 +0200 | [diff] [blame^] | 154 | return cpu_pm_notify(CPU_CLUSTER_PM_EXIT); |
Colin Cross | ab10023 | 2011-02-10 02:04:45 -0800 | [diff] [blame] | 155 | } |
| 156 | EXPORT_SYMBOL_GPL(cpu_cluster_pm_exit); |
Colin Cross | 6f3eaec | 2011-07-22 14:57:09 -0700 | [diff] [blame] | 157 | |
| 158 | #ifdef CONFIG_PM |
| 159 | static int cpu_pm_suspend(void) |
| 160 | { |
| 161 | int ret; |
| 162 | |
| 163 | ret = cpu_pm_enter(); |
| 164 | if (ret) |
| 165 | return ret; |
| 166 | |
| 167 | ret = cpu_cluster_pm_enter(); |
| 168 | return ret; |
| 169 | } |
| 170 | |
| 171 | static void cpu_pm_resume(void) |
| 172 | { |
| 173 | cpu_cluster_pm_exit(); |
| 174 | cpu_pm_exit(); |
| 175 | } |
| 176 | |
| 177 | static struct syscore_ops cpu_pm_syscore_ops = { |
| 178 | .suspend = cpu_pm_suspend, |
| 179 | .resume = cpu_pm_resume, |
| 180 | }; |
| 181 | |
| 182 | static int cpu_pm_init(void) |
| 183 | { |
| 184 | register_syscore_ops(&cpu_pm_syscore_ops); |
| 185 | return 0; |
| 186 | } |
| 187 | core_initcall(cpu_pm_init); |
| 188 | #endif |