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