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