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