Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 1 | /* |
| 2 | * cpuidle.c - core cpuidle infrastructure |
| 3 | * |
| 4 | * (C) 2006-2007 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com> |
| 5 | * Shaohua Li <shaohua.li@intel.com> |
| 6 | * Adam Belay <abelay@novell.com> |
| 7 | * |
| 8 | * This code is licenced under the GPL. |
| 9 | */ |
| 10 | |
Daniel Lezcano | b60e6a0 | 2013-03-21 12:21:31 +0000 | [diff] [blame] | 11 | #include <linux/clockchips.h> |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 12 | #include <linux/kernel.h> |
| 13 | #include <linux/mutex.h> |
| 14 | #include <linux/sched.h> |
| 15 | #include <linux/notifier.h> |
Jean Pihet | e8db0be | 2011-08-25 15:35:03 +0200 | [diff] [blame] | 16 | #include <linux/pm_qos.h> |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 17 | #include <linux/cpu.h> |
| 18 | #include <linux/cpuidle.h> |
venkatesh.pallipadi@intel.com | 9a0b841 | 2008-01-31 17:35:06 -0800 | [diff] [blame] | 19 | #include <linux/ktime.h> |
Arjan van de Ven | 2e94d1f | 2008-09-10 16:06:00 -0700 | [diff] [blame] | 20 | #include <linux/hrtimer.h> |
Paul Gortmaker | 884b17e | 2011-08-29 17:52:39 -0400 | [diff] [blame] | 21 | #include <linux/module.h> |
Rafael J. Wysocki | 3810631 | 2015-02-12 23:33:15 +0100 | [diff] [blame] | 22 | #include <linux/suspend.h> |
Rafael J. Wysocki | 124cf911 | 2015-02-13 23:50:43 +0100 | [diff] [blame] | 23 | #include <linux/tick.h> |
Arjan van de Ven | 288f023 | 2009-09-19 13:35:33 +0200 | [diff] [blame] | 24 | #include <trace/events/power.h> |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 25 | |
| 26 | #include "cpuidle.h" |
| 27 | |
| 28 | DEFINE_PER_CPU(struct cpuidle_device *, cpuidle_devices); |
Daniel Lezcano | 4c637b2 | 2013-04-23 08:54:33 +0000 | [diff] [blame] | 29 | DEFINE_PER_CPU(struct cpuidle_device, cpuidle_dev); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 30 | |
| 31 | DEFINE_MUTEX(cpuidle_lock); |
| 32 | LIST_HEAD(cpuidle_detected_devices); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 33 | |
| 34 | static int enabled_devices; |
Len Brown | 62027ae | 2011-04-01 18:13:10 -0400 | [diff] [blame] | 35 | static int off __read_mostly; |
Len Brown | a0bfa13 | 2011-04-01 19:34:59 -0400 | [diff] [blame] | 36 | static int initialized __read_mostly; |
Len Brown | 62027ae | 2011-04-01 18:13:10 -0400 | [diff] [blame] | 37 | |
| 38 | int cpuidle_disabled(void) |
| 39 | { |
| 40 | return off; |
| 41 | } |
Len Brown | d91ee58 | 2011-04-01 18:28:35 -0400 | [diff] [blame] | 42 | void disable_cpuidle(void) |
| 43 | { |
| 44 | off = 1; |
| 45 | } |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 46 | |
| 47 | /** |
Boris Ostrovsky | 1a022e3 | 2012-03-13 19:55:09 +0100 | [diff] [blame] | 48 | * cpuidle_play_dead - cpu off-lining |
| 49 | * |
Toshi Kani | ee01e66 | 2012-03-31 21:37:02 -0600 | [diff] [blame] | 50 | * Returns in case of an error or no driver |
Boris Ostrovsky | 1a022e3 | 2012-03-13 19:55:09 +0100 | [diff] [blame] | 51 | */ |
| 52 | int cpuidle_play_dead(void) |
| 53 | { |
| 54 | struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices); |
Daniel Lezcano | bf4d1b5 | 2012-10-31 16:44:48 +0000 | [diff] [blame] | 55 | struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev); |
Daniel Lezcano | 8aef33a | 2013-01-15 14:18:04 +0100 | [diff] [blame] | 56 | int i; |
Boris Ostrovsky | 1a022e3 | 2012-03-13 19:55:09 +0100 | [diff] [blame] | 57 | |
Toshi Kani | ee01e66 | 2012-03-31 21:37:02 -0600 | [diff] [blame] | 58 | if (!drv) |
| 59 | return -ENODEV; |
| 60 | |
Boris Ostrovsky | 1a022e3 | 2012-03-13 19:55:09 +0100 | [diff] [blame] | 61 | /* Find lowest-power state that supports long-term idle */ |
Daniel Lezcano | 8aef33a | 2013-01-15 14:18:04 +0100 | [diff] [blame] | 62 | for (i = drv->state_count - 1; i >= CPUIDLE_DRIVER_STATE_START; i--) |
| 63 | if (drv->states[i].enter_dead) |
| 64 | return drv->states[i].enter_dead(dev, i); |
Boris Ostrovsky | 1a022e3 | 2012-03-13 19:55:09 +0100 | [diff] [blame] | 65 | |
| 66 | return -ENODEV; |
| 67 | } |
| 68 | |
| 69 | /** |
Rafael J. Wysocki | 3810631 | 2015-02-12 23:33:15 +0100 | [diff] [blame] | 70 | * cpuidle_find_deepest_state - Find deepest state meeting specific conditions. |
| 71 | * @drv: cpuidle driver for the given CPU. |
| 72 | * @dev: cpuidle device for the given CPU. |
Rafael J. Wysocki | 124cf911 | 2015-02-13 23:50:43 +0100 | [diff] [blame] | 73 | * @freeze: Whether or not the state should be suitable for suspend-to-idle. |
Rafael J. Wysocki | a6220fc | 2014-05-05 00:51:54 +0200 | [diff] [blame] | 74 | */ |
| 75 | static int cpuidle_find_deepest_state(struct cpuidle_driver *drv, |
Rafael J. Wysocki | 124cf911 | 2015-02-13 23:50:43 +0100 | [diff] [blame] | 76 | struct cpuidle_device *dev, bool freeze) |
Rafael J. Wysocki | a6220fc | 2014-05-05 00:51:54 +0200 | [diff] [blame] | 77 | { |
| 78 | unsigned int latency_req = 0; |
Rafael J. Wysocki | 124cf911 | 2015-02-13 23:50:43 +0100 | [diff] [blame] | 79 | int i, ret = freeze ? -1 : CPUIDLE_DRIVER_STATE_START - 1; |
Rafael J. Wysocki | a6220fc | 2014-05-05 00:51:54 +0200 | [diff] [blame] | 80 | |
| 81 | for (i = CPUIDLE_DRIVER_STATE_START; i < drv->state_count; i++) { |
| 82 | struct cpuidle_state *s = &drv->states[i]; |
| 83 | struct cpuidle_state_usage *su = &dev->states_usage[i]; |
| 84 | |
Rafael J. Wysocki | 124cf911 | 2015-02-13 23:50:43 +0100 | [diff] [blame] | 85 | if (s->disabled || su->disable || s->exit_latency <= latency_req |
| 86 | || (freeze && !s->enter_freeze)) |
Rafael J. Wysocki | a6220fc | 2014-05-05 00:51:54 +0200 | [diff] [blame] | 87 | continue; |
| 88 | |
| 89 | latency_req = s->exit_latency; |
| 90 | ret = i; |
| 91 | } |
| 92 | return ret; |
| 93 | } |
| 94 | |
Rafael J. Wysocki | 124cf911 | 2015-02-13 23:50:43 +0100 | [diff] [blame] | 95 | static void enter_freeze_proper(struct cpuidle_driver *drv, |
| 96 | struct cpuidle_device *dev, int index) |
| 97 | { |
| 98 | tick_freeze(); |
| 99 | /* |
| 100 | * The state used here cannot be a "coupled" one, because the "coupled" |
| 101 | * cpuidle mechanism enables interrupts and doing that with timekeeping |
| 102 | * suspended is generally unsafe. |
| 103 | */ |
| 104 | drv->states[index].enter_freeze(dev, drv, index); |
| 105 | WARN_ON(!irqs_disabled()); |
| 106 | /* |
| 107 | * timekeeping_resume() that will be called by tick_unfreeze() for the |
| 108 | * last CPU executing it calls functions containing RCU read-side |
| 109 | * critical sections, so tell RCU about that. |
| 110 | */ |
| 111 | RCU_NONIDLE(tick_unfreeze()); |
| 112 | } |
| 113 | |
Rafael J. Wysocki | a6220fc | 2014-05-05 00:51:54 +0200 | [diff] [blame] | 114 | /** |
Rafael J. Wysocki | 3810631 | 2015-02-12 23:33:15 +0100 | [diff] [blame] | 115 | * cpuidle_enter_freeze - Enter an idle state suitable for suspend-to-idle. |
| 116 | * |
Rafael J. Wysocki | 124cf911 | 2015-02-13 23:50:43 +0100 | [diff] [blame] | 117 | * If there are states with the ->enter_freeze callback, find the deepest of |
| 118 | * them and enter it with frozen tick. Otherwise, find the deepest state |
| 119 | * available and enter it normally. |
Rafael J. Wysocki | 3810631 | 2015-02-12 23:33:15 +0100 | [diff] [blame] | 120 | */ |
| 121 | void cpuidle_enter_freeze(void) |
| 122 | { |
| 123 | struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices); |
| 124 | struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev); |
| 125 | int index; |
| 126 | |
Rafael J. Wysocki | 124cf911 | 2015-02-13 23:50:43 +0100 | [diff] [blame] | 127 | /* |
| 128 | * Find the deepest state with ->enter_freeze present, which guarantees |
| 129 | * that interrupts won't be enabled when it exits and allows the tick to |
| 130 | * be frozen safely. |
| 131 | */ |
| 132 | index = cpuidle_find_deepest_state(drv, dev, true); |
| 133 | if (index >= 0) { |
| 134 | enter_freeze_proper(drv, dev, index); |
| 135 | return; |
| 136 | } |
| 137 | |
| 138 | /* |
| 139 | * It is not safe to freeze the tick, find the deepest state available |
| 140 | * at all and try to enter it normally. |
| 141 | */ |
| 142 | index = cpuidle_find_deepest_state(drv, dev, false); |
Rafael J. Wysocki | 3810631 | 2015-02-12 23:33:15 +0100 | [diff] [blame] | 143 | if (index >= 0) |
| 144 | cpuidle_enter(drv, dev, index); |
| 145 | else |
| 146 | arch_cpu_idle(); |
| 147 | |
| 148 | /* Interrupts are enabled again here. */ |
| 149 | local_irq_disable(); |
| 150 | } |
| 151 | |
| 152 | /** |
Colin Cross | 56cfbf7 | 2012-05-07 17:57:39 -0700 | [diff] [blame] | 153 | * cpuidle_enter_state - enter the state and update stats |
| 154 | * @dev: cpuidle device for this cpu |
| 155 | * @drv: cpuidle driver for this cpu |
| 156 | * @next_state: index into drv->states of the state to enter |
| 157 | */ |
| 158 | int cpuidle_enter_state(struct cpuidle_device *dev, struct cpuidle_driver *drv, |
Daniel Lezcano | 554c06b | 2013-04-23 08:54:31 +0000 | [diff] [blame] | 159 | int index) |
Colin Cross | 56cfbf7 | 2012-05-07 17:57:39 -0700 | [diff] [blame] | 160 | { |
| 161 | int entered_state; |
| 162 | |
Daniel Lezcano | 554c06b | 2013-04-23 08:54:31 +0000 | [diff] [blame] | 163 | struct cpuidle_state *target_state = &drv->states[index]; |
| 164 | ktime_t time_start, time_end; |
| 165 | s64 diff; |
| 166 | |
Sandeep Tripathy | 30fe688 | 2014-07-02 15:00:58 +0530 | [diff] [blame] | 167 | trace_cpu_idle_rcuidle(index, dev->cpu); |
Daniel Lezcano | 554c06b | 2013-04-23 08:54:31 +0000 | [diff] [blame] | 168 | time_start = ktime_get(); |
| 169 | |
| 170 | entered_state = target_state->enter(dev, drv, index); |
| 171 | |
| 172 | time_end = ktime_get(); |
Sandeep Tripathy | 30fe688 | 2014-07-02 15:00:58 +0530 | [diff] [blame] | 173 | trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, dev->cpu); |
Daniel Lezcano | 554c06b | 2013-04-23 08:54:31 +0000 | [diff] [blame] | 174 | |
Paul Burton | 0b89e9a | 2014-03-06 11:02:01 +0000 | [diff] [blame] | 175 | if (!cpuidle_state_is_coupled(dev, drv, entered_state)) |
| 176 | local_irq_enable(); |
Daniel Lezcano | 554c06b | 2013-04-23 08:54:31 +0000 | [diff] [blame] | 177 | |
| 178 | diff = ktime_to_us(ktime_sub(time_end, time_start)); |
| 179 | if (diff > INT_MAX) |
| 180 | diff = INT_MAX; |
| 181 | |
| 182 | dev->last_residency = (int) diff; |
Colin Cross | 56cfbf7 | 2012-05-07 17:57:39 -0700 | [diff] [blame] | 183 | |
| 184 | if (entered_state >= 0) { |
| 185 | /* Update cpuidle counters */ |
| 186 | /* This can be moved to within driver enter routine |
| 187 | * but that results in multiple copies of same code. |
| 188 | */ |
Julius Werner | a474a51 | 2012-11-27 14:17:58 +0100 | [diff] [blame] | 189 | dev->states_usage[entered_state].time += dev->last_residency; |
Colin Cross | 56cfbf7 | 2012-05-07 17:57:39 -0700 | [diff] [blame] | 190 | dev->states_usage[entered_state].usage++; |
| 191 | } else { |
| 192 | dev->last_residency = 0; |
| 193 | } |
| 194 | |
| 195 | return entered_state; |
| 196 | } |
| 197 | |
| 198 | /** |
Daniel Lezcano | 907e30f | 2014-03-03 08:48:50 +0100 | [diff] [blame] | 199 | * cpuidle_select - ask the cpuidle framework to choose an idle state |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 200 | * |
Daniel Lezcano | 907e30f | 2014-03-03 08:48:50 +0100 | [diff] [blame] | 201 | * @drv: the cpuidle driver |
| 202 | * @dev: the cpuidle device |
| 203 | * |
| 204 | * Returns the index of the idle state. |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 205 | */ |
Daniel Lezcano | 907e30f | 2014-03-03 08:48:50 +0100 | [diff] [blame] | 206 | int cpuidle_select(struct cpuidle_driver *drv, struct cpuidle_device *dev) |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 207 | { |
Rafael J. Wysocki | 52c324f | 2014-05-01 00:13:47 +0200 | [diff] [blame] | 208 | if (off || !initialized) |
| 209 | return -ENODEV; |
| 210 | |
| 211 | if (!drv || !dev || !dev->enabled) |
| 212 | return -EBUSY; |
| 213 | |
Daniel Lezcano | 907e30f | 2014-03-03 08:48:50 +0100 | [diff] [blame] | 214 | return cpuidle_curr_governor->select(drv, dev); |
| 215 | } |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 216 | |
Daniel Lezcano | 907e30f | 2014-03-03 08:48:50 +0100 | [diff] [blame] | 217 | /** |
| 218 | * cpuidle_enter - enter into the specified idle state |
| 219 | * |
| 220 | * @drv: the cpuidle driver tied with the cpu |
| 221 | * @dev: the cpuidle device |
| 222 | * @index: the index in the idle state table |
| 223 | * |
| 224 | * Returns the index in the idle state, < 0 in case of error. |
| 225 | * The error code depends on the backend driver |
| 226 | */ |
| 227 | int cpuidle_enter(struct cpuidle_driver *drv, struct cpuidle_device *dev, |
| 228 | int index) |
| 229 | { |
| 230 | if (cpuidle_state_is_coupled(dev, drv, index)) |
| 231 | return cpuidle_enter_state_coupled(dev, drv, index); |
| 232 | return cpuidle_enter_state(dev, drv, index); |
| 233 | } |
Len Brown | a0bfa13 | 2011-04-01 19:34:59 -0400 | [diff] [blame] | 234 | |
Daniel Lezcano | 907e30f | 2014-03-03 08:48:50 +0100 | [diff] [blame] | 235 | /** |
| 236 | * cpuidle_reflect - tell the underlying governor what was the state |
| 237 | * we were in |
| 238 | * |
| 239 | * @dev : the cpuidle device |
| 240 | * @index: the index in the idle state table |
| 241 | * |
| 242 | */ |
| 243 | void cpuidle_reflect(struct cpuidle_device *dev, int index) |
| 244 | { |
Rafael J. Wysocki | 3810631 | 2015-02-12 23:33:15 +0100 | [diff] [blame] | 245 | if (cpuidle_curr_governor->reflect) |
Daniel Lezcano | 907e30f | 2014-03-03 08:48:50 +0100 | [diff] [blame] | 246 | cpuidle_curr_governor->reflect(dev, index); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | /** |
| 250 | * cpuidle_install_idle_handler - installs the cpuidle idle loop handler |
| 251 | */ |
| 252 | void cpuidle_install_idle_handler(void) |
| 253 | { |
Len Brown | a0bfa13 | 2011-04-01 19:34:59 -0400 | [diff] [blame] | 254 | if (enabled_devices) { |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 255 | /* Make sure all changes finished before we switch to new idle */ |
| 256 | smp_wmb(); |
Len Brown | a0bfa13 | 2011-04-01 19:34:59 -0400 | [diff] [blame] | 257 | initialized = 1; |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 258 | } |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler |
| 263 | */ |
| 264 | void cpuidle_uninstall_idle_handler(void) |
| 265 | { |
Len Brown | a0bfa13 | 2011-04-01 19:34:59 -0400 | [diff] [blame] | 266 | if (enabled_devices) { |
| 267 | initialized = 0; |
Chuansheng Liu | 2ed903c | 2014-09-04 15:17:55 +0800 | [diff] [blame] | 268 | wake_up_all_idle_cpus(); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 269 | } |
Daniel Lezcano | 442bf3a | 2014-09-04 11:32:09 -0400 | [diff] [blame] | 270 | |
| 271 | /* |
| 272 | * Make sure external observers (such as the scheduler) |
| 273 | * are done looking at pointed idle states. |
| 274 | */ |
| 275 | synchronize_rcu(); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | /** |
| 279 | * cpuidle_pause_and_lock - temporarily disables CPUIDLE |
| 280 | */ |
| 281 | void cpuidle_pause_and_lock(void) |
| 282 | { |
| 283 | mutex_lock(&cpuidle_lock); |
| 284 | cpuidle_uninstall_idle_handler(); |
| 285 | } |
| 286 | |
| 287 | EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock); |
| 288 | |
| 289 | /** |
| 290 | * cpuidle_resume_and_unlock - resumes CPUIDLE operation |
| 291 | */ |
| 292 | void cpuidle_resume_and_unlock(void) |
| 293 | { |
| 294 | cpuidle_install_idle_handler(); |
| 295 | mutex_unlock(&cpuidle_lock); |
| 296 | } |
| 297 | |
| 298 | EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock); |
| 299 | |
Preeti U Murthy | 8651f97 | 2012-07-09 10:12:56 +0200 | [diff] [blame] | 300 | /* Currently used in suspend/resume path to suspend cpuidle */ |
| 301 | void cpuidle_pause(void) |
| 302 | { |
| 303 | mutex_lock(&cpuidle_lock); |
| 304 | cpuidle_uninstall_idle_handler(); |
| 305 | mutex_unlock(&cpuidle_lock); |
| 306 | } |
| 307 | |
| 308 | /* Currently used in suspend/resume path to resume cpuidle */ |
| 309 | void cpuidle_resume(void) |
| 310 | { |
| 311 | mutex_lock(&cpuidle_lock); |
| 312 | cpuidle_install_idle_handler(); |
| 313 | mutex_unlock(&cpuidle_lock); |
| 314 | } |
| 315 | |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 316 | /** |
| 317 | * cpuidle_enable_device - enables idle PM for a CPU |
| 318 | * @dev: the CPU |
| 319 | * |
| 320 | * This function must be called between cpuidle_pause_and_lock and |
| 321 | * cpuidle_resume_and_unlock when used externally. |
| 322 | */ |
| 323 | int cpuidle_enable_device(struct cpuidle_device *dev) |
| 324 | { |
Daniel Lezcano | 5df0aa7 | 2013-06-12 15:08:54 +0200 | [diff] [blame] | 325 | int ret; |
Daniel Lezcano | bf4d1b5 | 2012-10-31 16:44:48 +0000 | [diff] [blame] | 326 | struct cpuidle_driver *drv; |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 327 | |
Srivatsa S. Bhat | 1b0a0e9 | 2012-05-04 14:06:02 -0700 | [diff] [blame] | 328 | if (!dev) |
| 329 | return -EINVAL; |
| 330 | |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 331 | if (dev->enabled) |
| 332 | return 0; |
Daniel Lezcano | bf4d1b5 | 2012-10-31 16:44:48 +0000 | [diff] [blame] | 333 | |
| 334 | drv = cpuidle_get_cpu_driver(dev); |
| 335 | |
Robert Lee | e168979 | 2012-03-20 15:22:42 -0500 | [diff] [blame] | 336 | if (!drv || !cpuidle_curr_governor) |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 337 | return -EIO; |
Daniel Lezcano | bf4d1b5 | 2012-10-31 16:44:48 +0000 | [diff] [blame] | 338 | |
Daniel Lezcano | 10b9d3f | 2013-06-12 15:08:49 +0200 | [diff] [blame] | 339 | if (!dev->registered) |
| 340 | return -EINVAL; |
| 341 | |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 342 | if (!dev->state_count) |
Daniel Lezcano | fc850f3 | 2012-03-26 14:51:26 +0200 | [diff] [blame] | 343 | dev->state_count = drv->state_count; |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 344 | |
Daniel Lezcano | bf4d1b5 | 2012-10-31 16:44:48 +0000 | [diff] [blame] | 345 | ret = cpuidle_add_device_sysfs(dev); |
| 346 | if (ret) |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 347 | return ret; |
| 348 | |
| 349 | if (cpuidle_curr_governor->enable && |
Robert Lee | e168979 | 2012-03-20 15:22:42 -0500 | [diff] [blame] | 350 | (ret = cpuidle_curr_governor->enable(drv, dev))) |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 351 | goto fail_sysfs; |
| 352 | |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 353 | smp_wmb(); |
| 354 | |
| 355 | dev->enabled = 1; |
| 356 | |
| 357 | enabled_devices++; |
| 358 | return 0; |
| 359 | |
| 360 | fail_sysfs: |
Daniel Lezcano | bf4d1b5 | 2012-10-31 16:44:48 +0000 | [diff] [blame] | 361 | cpuidle_remove_device_sysfs(dev); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 362 | |
| 363 | return ret; |
| 364 | } |
| 365 | |
| 366 | EXPORT_SYMBOL_GPL(cpuidle_enable_device); |
| 367 | |
| 368 | /** |
| 369 | * cpuidle_disable_device - disables idle PM for a CPU |
| 370 | * @dev: the CPU |
| 371 | * |
| 372 | * This function must be called between cpuidle_pause_and_lock and |
| 373 | * cpuidle_resume_and_unlock when used externally. |
| 374 | */ |
| 375 | void cpuidle_disable_device(struct cpuidle_device *dev) |
| 376 | { |
Daniel Lezcano | bf4d1b5 | 2012-10-31 16:44:48 +0000 | [diff] [blame] | 377 | struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev); |
| 378 | |
Srivatsa S. Bhat | cf31cd1 | 2012-10-08 13:43:08 +0530 | [diff] [blame] | 379 | if (!dev || !dev->enabled) |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 380 | return; |
Daniel Lezcano | bf4d1b5 | 2012-10-31 16:44:48 +0000 | [diff] [blame] | 381 | |
| 382 | if (!drv || !cpuidle_curr_governor) |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 383 | return; |
| 384 | |
| 385 | dev->enabled = 0; |
| 386 | |
| 387 | if (cpuidle_curr_governor->disable) |
Daniel Lezcano | bf4d1b5 | 2012-10-31 16:44:48 +0000 | [diff] [blame] | 388 | cpuidle_curr_governor->disable(drv, dev); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 389 | |
Daniel Lezcano | bf4d1b5 | 2012-10-31 16:44:48 +0000 | [diff] [blame] | 390 | cpuidle_remove_device_sysfs(dev); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 391 | enabled_devices--; |
| 392 | } |
| 393 | |
| 394 | EXPORT_SYMBOL_GPL(cpuidle_disable_device); |
| 395 | |
Daniel Lezcano | f6bb51a | 2013-06-12 15:08:53 +0200 | [diff] [blame] | 396 | static void __cpuidle_unregister_device(struct cpuidle_device *dev) |
| 397 | { |
| 398 | struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev); |
| 399 | |
| 400 | list_del(&dev->device_list); |
| 401 | per_cpu(cpuidle_devices, dev->cpu) = NULL; |
| 402 | module_put(drv->owner); |
| 403 | } |
| 404 | |
Viresh Kumar | 267d4bf | 2013-10-03 21:26:43 +0530 | [diff] [blame] | 405 | static void __cpuidle_device_init(struct cpuidle_device *dev) |
Daniel Lezcano | 5df0aa7 | 2013-06-12 15:08:54 +0200 | [diff] [blame] | 406 | { |
| 407 | memset(dev->states_usage, 0, sizeof(dev->states_usage)); |
| 408 | dev->last_residency = 0; |
Daniel Lezcano | 5df0aa7 | 2013-06-12 15:08:54 +0200 | [diff] [blame] | 409 | } |
| 410 | |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 411 | /** |
Venkatesh Pallipadi | dcb84f3 | 2008-05-19 19:09:27 -0400 | [diff] [blame] | 412 | * __cpuidle_register_device - internal register function called before register |
| 413 | * and enable routines |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 414 | * @dev: the cpu |
Venkatesh Pallipadi | dcb84f3 | 2008-05-19 19:09:27 -0400 | [diff] [blame] | 415 | * |
| 416 | * cpuidle_lock mutex must be held before this is called |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 417 | */ |
Venkatesh Pallipadi | dcb84f3 | 2008-05-19 19:09:27 -0400 | [diff] [blame] | 418 | static int __cpuidle_register_device(struct cpuidle_device *dev) |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 419 | { |
| 420 | int ret; |
Daniel Lezcano | bf4d1b5 | 2012-10-31 16:44:48 +0000 | [diff] [blame] | 421 | struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 422 | |
Daniel Lezcano | bf4d1b5 | 2012-10-31 16:44:48 +0000 | [diff] [blame] | 423 | if (!try_module_get(drv->owner)) |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 424 | return -EINVAL; |
| 425 | |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 426 | per_cpu(cpuidle_devices, dev->cpu) = dev; |
| 427 | list_add(&dev->device_list, &cpuidle_detected_devices); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 428 | |
Colin Cross | 4126c01 | 2012-05-07 17:57:41 -0700 | [diff] [blame] | 429 | ret = cpuidle_coupled_register_device(dev); |
Viresh Kumar | 4718266 | 2013-10-03 21:26:46 +0530 | [diff] [blame] | 430 | if (ret) |
Daniel Lezcano | f6bb51a | 2013-06-12 15:08:53 +0200 | [diff] [blame] | 431 | __cpuidle_unregister_device(dev); |
Viresh Kumar | 4718266 | 2013-10-03 21:26:46 +0530 | [diff] [blame] | 432 | else |
| 433 | dev->registered = 1; |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 434 | |
Viresh Kumar | 4718266 | 2013-10-03 21:26:46 +0530 | [diff] [blame] | 435 | return ret; |
Venkatesh Pallipadi | dcb84f3 | 2008-05-19 19:09:27 -0400 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | /** |
| 439 | * cpuidle_register_device - registers a CPU's idle PM feature |
| 440 | * @dev: the cpu |
| 441 | */ |
| 442 | int cpuidle_register_device(struct cpuidle_device *dev) |
| 443 | { |
Daniel Lezcano | c878a52 | 2013-06-12 15:08:55 +0200 | [diff] [blame] | 444 | int ret = -EBUSY; |
Venkatesh Pallipadi | dcb84f3 | 2008-05-19 19:09:27 -0400 | [diff] [blame] | 445 | |
Srivatsa S. Bhat | 1b0a0e9 | 2012-05-04 14:06:02 -0700 | [diff] [blame] | 446 | if (!dev) |
| 447 | return -EINVAL; |
| 448 | |
Venkatesh Pallipadi | dcb84f3 | 2008-05-19 19:09:27 -0400 | [diff] [blame] | 449 | mutex_lock(&cpuidle_lock); |
| 450 | |
Daniel Lezcano | c878a52 | 2013-06-12 15:08:55 +0200 | [diff] [blame] | 451 | if (dev->registered) |
| 452 | goto out_unlock; |
| 453 | |
Viresh Kumar | 267d4bf | 2013-10-03 21:26:43 +0530 | [diff] [blame] | 454 | __cpuidle_device_init(dev); |
Daniel Lezcano | 5df0aa7 | 2013-06-12 15:08:54 +0200 | [diff] [blame] | 455 | |
Daniel Lezcano | f6bb51a | 2013-06-12 15:08:53 +0200 | [diff] [blame] | 456 | ret = __cpuidle_register_device(dev); |
| 457 | if (ret) |
| 458 | goto out_unlock; |
| 459 | |
| 460 | ret = cpuidle_add_sysfs(dev); |
| 461 | if (ret) |
| 462 | goto out_unregister; |
Venkatesh Pallipadi | dcb84f3 | 2008-05-19 19:09:27 -0400 | [diff] [blame] | 463 | |
Daniel Lezcano | 10b9d3f | 2013-06-12 15:08:49 +0200 | [diff] [blame] | 464 | ret = cpuidle_enable_device(dev); |
Daniel Lezcano | f6bb51a | 2013-06-12 15:08:53 +0200 | [diff] [blame] | 465 | if (ret) |
| 466 | goto out_sysfs; |
Daniel Lezcano | 10b9d3f | 2013-06-12 15:08:49 +0200 | [diff] [blame] | 467 | |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 468 | cpuidle_install_idle_handler(); |
| 469 | |
Daniel Lezcano | f6bb51a | 2013-06-12 15:08:53 +0200 | [diff] [blame] | 470 | out_unlock: |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 471 | mutex_unlock(&cpuidle_lock); |
| 472 | |
Daniel Lezcano | f6bb51a | 2013-06-12 15:08:53 +0200 | [diff] [blame] | 473 | return ret; |
| 474 | |
| 475 | out_sysfs: |
| 476 | cpuidle_remove_sysfs(dev); |
| 477 | out_unregister: |
| 478 | __cpuidle_unregister_device(dev); |
| 479 | goto out_unlock; |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | EXPORT_SYMBOL_GPL(cpuidle_register_device); |
| 483 | |
| 484 | /** |
| 485 | * cpuidle_unregister_device - unregisters a CPU's idle PM feature |
| 486 | * @dev: the cpu |
| 487 | */ |
| 488 | void cpuidle_unregister_device(struct cpuidle_device *dev) |
| 489 | { |
Konrad Rzeszutek Wilk | 813e8e3 | 2013-12-03 10:59:58 -0500 | [diff] [blame] | 490 | if (!dev || dev->registered == 0) |
Venkatesh Pallipadi | dcb84f3 | 2008-05-19 19:09:27 -0400 | [diff] [blame] | 491 | return; |
| 492 | |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 493 | cpuidle_pause_and_lock(); |
| 494 | |
| 495 | cpuidle_disable_device(dev); |
| 496 | |
Daniel Lezcano | 1aef40e | 2012-10-26 12:26:24 +0200 | [diff] [blame] | 497 | cpuidle_remove_sysfs(dev); |
Daniel Lezcano | f6bb51a | 2013-06-12 15:08:53 +0200 | [diff] [blame] | 498 | |
| 499 | __cpuidle_unregister_device(dev); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 500 | |
Colin Cross | 4126c01 | 2012-05-07 17:57:41 -0700 | [diff] [blame] | 501 | cpuidle_coupled_unregister_device(dev); |
| 502 | |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 503 | cpuidle_resume_and_unlock(); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | EXPORT_SYMBOL_GPL(cpuidle_unregister_device); |
| 507 | |
Daniel Lezcano | 1c192d0 | 2013-04-23 15:28:44 +0000 | [diff] [blame] | 508 | /** |
Daniel Lezcano | 4c637b2 | 2013-04-23 08:54:33 +0000 | [diff] [blame] | 509 | * cpuidle_unregister: unregister a driver and the devices. This function |
| 510 | * can be used only if the driver has been previously registered through |
| 511 | * the cpuidle_register function. |
| 512 | * |
| 513 | * @drv: a valid pointer to a struct cpuidle_driver |
| 514 | */ |
| 515 | void cpuidle_unregister(struct cpuidle_driver *drv) |
| 516 | { |
| 517 | int cpu; |
| 518 | struct cpuidle_device *device; |
| 519 | |
Daniel Lezcano | 82467a5 | 2013-06-07 21:53:09 +0000 | [diff] [blame] | 520 | for_each_cpu(cpu, drv->cpumask) { |
Daniel Lezcano | 4c637b2 | 2013-04-23 08:54:33 +0000 | [diff] [blame] | 521 | device = &per_cpu(cpuidle_dev, cpu); |
| 522 | cpuidle_unregister_device(device); |
| 523 | } |
| 524 | |
| 525 | cpuidle_unregister_driver(drv); |
| 526 | } |
| 527 | EXPORT_SYMBOL_GPL(cpuidle_unregister); |
| 528 | |
| 529 | /** |
| 530 | * cpuidle_register: registers the driver and the cpu devices with the |
| 531 | * coupled_cpus passed as parameter. This function is used for all common |
| 532 | * initialization pattern there are in the arch specific drivers. The |
| 533 | * devices is globally defined in this file. |
| 534 | * |
| 535 | * @drv : a valid pointer to a struct cpuidle_driver |
| 536 | * @coupled_cpus: a cpumask for the coupled states |
| 537 | * |
| 538 | * Returns 0 on success, < 0 otherwise |
| 539 | */ |
| 540 | int cpuidle_register(struct cpuidle_driver *drv, |
| 541 | const struct cpumask *const coupled_cpus) |
| 542 | { |
| 543 | int ret, cpu; |
| 544 | struct cpuidle_device *device; |
| 545 | |
| 546 | ret = cpuidle_register_driver(drv); |
| 547 | if (ret) { |
| 548 | pr_err("failed to register cpuidle driver\n"); |
| 549 | return ret; |
| 550 | } |
| 551 | |
Daniel Lezcano | 82467a5 | 2013-06-07 21:53:09 +0000 | [diff] [blame] | 552 | for_each_cpu(cpu, drv->cpumask) { |
Daniel Lezcano | 4c637b2 | 2013-04-23 08:54:33 +0000 | [diff] [blame] | 553 | device = &per_cpu(cpuidle_dev, cpu); |
| 554 | device->cpu = cpu; |
| 555 | |
| 556 | #ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED |
| 557 | /* |
Viresh Kumar | caf4a36 | 2013-10-03 21:26:41 +0530 | [diff] [blame] | 558 | * On multiplatform for ARM, the coupled idle states could be |
Daniel Lezcano | 4c637b2 | 2013-04-23 08:54:33 +0000 | [diff] [blame] | 559 | * enabled in the kernel even if the cpuidle driver does not |
| 560 | * use it. Note, coupled_cpus is a struct copy. |
| 561 | */ |
| 562 | if (coupled_cpus) |
| 563 | device->coupled_cpus = *coupled_cpus; |
| 564 | #endif |
| 565 | ret = cpuidle_register_device(device); |
| 566 | if (!ret) |
| 567 | continue; |
| 568 | |
| 569 | pr_err("Failed to register cpuidle device for cpu%d\n", cpu); |
| 570 | |
| 571 | cpuidle_unregister(drv); |
| 572 | break; |
| 573 | } |
| 574 | |
| 575 | return ret; |
| 576 | } |
| 577 | EXPORT_SYMBOL_GPL(cpuidle_register); |
| 578 | |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 579 | #ifdef CONFIG_SMP |
| 580 | |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 581 | /* |
| 582 | * This function gets called when a part of the kernel has a new latency |
| 583 | * requirement. This means we need to get all processors out of their C-state, |
| 584 | * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that |
| 585 | * wakes them all right up. |
| 586 | */ |
| 587 | static int cpuidle_latency_notify(struct notifier_block *b, |
| 588 | unsigned long l, void *v) |
| 589 | { |
Chuansheng Liu | 2ed903c | 2014-09-04 15:17:55 +0800 | [diff] [blame] | 590 | wake_up_all_idle_cpus(); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 591 | return NOTIFY_OK; |
| 592 | } |
| 593 | |
| 594 | static struct notifier_block cpuidle_latency_notifier = { |
| 595 | .notifier_call = cpuidle_latency_notify, |
| 596 | }; |
| 597 | |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 598 | static inline void latency_notifier_init(struct notifier_block *n) |
| 599 | { |
| 600 | pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY, n); |
| 601 | } |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 602 | |
| 603 | #else /* CONFIG_SMP */ |
| 604 | |
| 605 | #define latency_notifier_init(x) do { } while (0) |
| 606 | |
| 607 | #endif /* CONFIG_SMP */ |
| 608 | |
| 609 | /** |
| 610 | * cpuidle_init - core initializer |
| 611 | */ |
| 612 | static int __init cpuidle_init(void) |
| 613 | { |
| 614 | int ret; |
| 615 | |
Len Brown | 62027ae | 2011-04-01 18:13:10 -0400 | [diff] [blame] | 616 | if (cpuidle_disabled()) |
| 617 | return -ENODEV; |
| 618 | |
Kay Sievers | 8a25a2f | 2011-12-21 14:29:42 -0800 | [diff] [blame] | 619 | ret = cpuidle_add_interface(cpu_subsys.dev_root); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 620 | if (ret) |
| 621 | return ret; |
| 622 | |
| 623 | latency_notifier_init(&cpuidle_latency_notifier); |
| 624 | |
| 625 | return 0; |
| 626 | } |
| 627 | |
Len Brown | 62027ae | 2011-04-01 18:13:10 -0400 | [diff] [blame] | 628 | module_param(off, int, 0444); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 629 | core_initcall(cpuidle_init); |