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