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 | |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/mutex.h> |
| 13 | #include <linux/sched.h> |
| 14 | #include <linux/notifier.h> |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 15 | #include <linux/pm_qos_params.h> |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 16 | #include <linux/cpu.h> |
| 17 | #include <linux/cpuidle.h> |
venkatesh.pallipadi@intel.com | 9a0b841 | 2008-01-31 17:35:06 -0800 | [diff] [blame] | 18 | #include <linux/ktime.h> |
Arjan van de Ven | 2e94d1f | 2008-09-10 16:06:00 -0700 | [diff] [blame] | 19 | #include <linux/hrtimer.h> |
Arjan van de Ven | 288f023 | 2009-09-19 13:35:33 +0200 | [diff] [blame] | 20 | #include <trace/events/power.h> |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 21 | |
| 22 | #include "cpuidle.h" |
| 23 | |
| 24 | DEFINE_PER_CPU(struct cpuidle_device *, cpuidle_devices); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 25 | |
| 26 | DEFINE_MUTEX(cpuidle_lock); |
| 27 | LIST_HEAD(cpuidle_detected_devices); |
| 28 | static void (*pm_idle_old)(void); |
| 29 | |
| 30 | static int enabled_devices; |
| 31 | |
Venki Pallipadi | a6869cc | 2008-02-08 17:05:44 -0800 | [diff] [blame] | 32 | #if defined(CONFIG_ARCH_HAS_CPU_IDLE_WAIT) |
| 33 | static void cpuidle_kick_cpus(void) |
| 34 | { |
| 35 | cpu_idle_wait(); |
| 36 | } |
| 37 | #elif defined(CONFIG_SMP) |
| 38 | # error "Arch needs cpu_idle_wait() equivalent here" |
| 39 | #else /* !CONFIG_ARCH_HAS_CPU_IDLE_WAIT && !CONFIG_SMP */ |
| 40 | static void cpuidle_kick_cpus(void) {} |
| 41 | #endif |
| 42 | |
Venkatesh Pallipadi | dcb84f3 | 2008-05-19 19:09:27 -0400 | [diff] [blame] | 43 | static int __cpuidle_register_device(struct cpuidle_device *dev); |
| 44 | |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 45 | /** |
| 46 | * cpuidle_idle_call - the main idle loop |
| 47 | * |
| 48 | * NOTE: no locks or semaphores should be used here |
| 49 | */ |
| 50 | static void cpuidle_idle_call(void) |
| 51 | { |
| 52 | struct cpuidle_device *dev = __get_cpu_var(cpuidle_devices); |
| 53 | struct cpuidle_state *target_state; |
| 54 | int next_state; |
| 55 | |
| 56 | /* check if the device is ready */ |
| 57 | if (!dev || !dev->enabled) { |
| 58 | if (pm_idle_old) |
| 59 | pm_idle_old(); |
| 60 | else |
Venkatesh Pallipadi | 89cedfe | 2008-10-16 19:00:08 -0400 | [diff] [blame] | 61 | #if defined(CONFIG_ARCH_HAS_DEFAULT_IDLE) |
| 62 | default_idle(); |
| 63 | #else |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 64 | local_irq_enable(); |
Venkatesh Pallipadi | 89cedfe | 2008-10-16 19:00:08 -0400 | [diff] [blame] | 65 | #endif |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 66 | return; |
| 67 | } |
| 68 | |
Arjan van de Ven | 9a65583 | 2008-11-09 12:45:10 -0800 | [diff] [blame] | 69 | #if 0 |
| 70 | /* shows regressions, re-enable for 2.6.29 */ |
Arjan van de Ven | 2e94d1f | 2008-09-10 16:06:00 -0700 | [diff] [blame] | 71 | /* |
| 72 | * run any timers that can be run now, at this point |
| 73 | * before calculating the idle duration etc. |
| 74 | */ |
| 75 | hrtimer_peek_ahead_timers(); |
Arjan van de Ven | 9a65583 | 2008-11-09 12:45:10 -0800 | [diff] [blame] | 76 | #endif |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 77 | /* ask the governor for the next state */ |
| 78 | next_state = cpuidle_curr_governor->select(dev); |
| 79 | if (need_resched()) |
| 80 | return; |
| 81 | target_state = &dev->states[next_state]; |
| 82 | |
| 83 | /* enter the state and update stats */ |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 84 | dev->last_state = target_state; |
Venkatesh Pallipadi | 887e301 | 2008-09-29 15:24:27 -0700 | [diff] [blame] | 85 | dev->last_residency = target_state->enter(dev, target_state); |
| 86 | if (dev->last_state) |
| 87 | target_state = dev->last_state; |
| 88 | |
Yi Yang | 8b78cf6 | 2008-02-25 08:46:12 +0800 | [diff] [blame] | 89 | target_state->time += (unsigned long long)dev->last_residency; |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 90 | target_state->usage++; |
| 91 | |
| 92 | /* give the governor an opportunity to reflect on the outcome */ |
| 93 | if (cpuidle_curr_governor->reflect) |
| 94 | cpuidle_curr_governor->reflect(dev); |
Arjan van de Ven | 288f023 | 2009-09-19 13:35:33 +0200 | [diff] [blame] | 95 | trace_power_end(0); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | /** |
| 99 | * cpuidle_install_idle_handler - installs the cpuidle idle loop handler |
| 100 | */ |
| 101 | void cpuidle_install_idle_handler(void) |
| 102 | { |
| 103 | if (enabled_devices && (pm_idle != cpuidle_idle_call)) { |
| 104 | /* Make sure all changes finished before we switch to new idle */ |
| 105 | smp_wmb(); |
| 106 | pm_idle = cpuidle_idle_call; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler |
| 112 | */ |
| 113 | void cpuidle_uninstall_idle_handler(void) |
| 114 | { |
Thomas Gleixner | b032bf70d | 2008-07-27 23:47:12 +0200 | [diff] [blame] | 115 | if (enabled_devices && pm_idle_old && (pm_idle != pm_idle_old)) { |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 116 | pm_idle = pm_idle_old; |
Venki Pallipadi | a6869cc | 2008-02-08 17:05:44 -0800 | [diff] [blame] | 117 | cpuidle_kick_cpus(); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * cpuidle_pause_and_lock - temporarily disables CPUIDLE |
| 123 | */ |
| 124 | void cpuidle_pause_and_lock(void) |
| 125 | { |
| 126 | mutex_lock(&cpuidle_lock); |
| 127 | cpuidle_uninstall_idle_handler(); |
| 128 | } |
| 129 | |
| 130 | EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock); |
| 131 | |
| 132 | /** |
| 133 | * cpuidle_resume_and_unlock - resumes CPUIDLE operation |
| 134 | */ |
| 135 | void cpuidle_resume_and_unlock(void) |
| 136 | { |
| 137 | cpuidle_install_idle_handler(); |
| 138 | mutex_unlock(&cpuidle_lock); |
| 139 | } |
| 140 | |
| 141 | EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock); |
| 142 | |
| 143 | /** |
| 144 | * cpuidle_enable_device - enables idle PM for a CPU |
| 145 | * @dev: the CPU |
| 146 | * |
| 147 | * This function must be called between cpuidle_pause_and_lock and |
| 148 | * cpuidle_resume_and_unlock when used externally. |
| 149 | */ |
| 150 | int cpuidle_enable_device(struct cpuidle_device *dev) |
| 151 | { |
| 152 | int ret, i; |
| 153 | |
| 154 | if (dev->enabled) |
| 155 | return 0; |
| 156 | if (!cpuidle_curr_driver || !cpuidle_curr_governor) |
| 157 | return -EIO; |
| 158 | if (!dev->state_count) |
| 159 | return -EINVAL; |
| 160 | |
Venkatesh Pallipadi | dcb84f3 | 2008-05-19 19:09:27 -0400 | [diff] [blame] | 161 | if (dev->registered == 0) { |
| 162 | ret = __cpuidle_register_device(dev); |
| 163 | if (ret) |
| 164 | return ret; |
| 165 | } |
| 166 | |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 167 | if ((ret = cpuidle_add_state_sysfs(dev))) |
| 168 | return ret; |
| 169 | |
| 170 | if (cpuidle_curr_governor->enable && |
| 171 | (ret = cpuidle_curr_governor->enable(dev))) |
| 172 | goto fail_sysfs; |
| 173 | |
| 174 | for (i = 0; i < dev->state_count; i++) { |
| 175 | dev->states[i].usage = 0; |
| 176 | dev->states[i].time = 0; |
| 177 | } |
| 178 | dev->last_residency = 0; |
| 179 | dev->last_state = NULL; |
| 180 | |
| 181 | smp_wmb(); |
| 182 | |
| 183 | dev->enabled = 1; |
| 184 | |
| 185 | enabled_devices++; |
| 186 | return 0; |
| 187 | |
| 188 | fail_sysfs: |
| 189 | cpuidle_remove_state_sysfs(dev); |
| 190 | |
| 191 | return ret; |
| 192 | } |
| 193 | |
| 194 | EXPORT_SYMBOL_GPL(cpuidle_enable_device); |
| 195 | |
| 196 | /** |
| 197 | * cpuidle_disable_device - disables idle PM for a CPU |
| 198 | * @dev: the CPU |
| 199 | * |
| 200 | * This function must be called between cpuidle_pause_and_lock and |
| 201 | * cpuidle_resume_and_unlock when used externally. |
| 202 | */ |
| 203 | void cpuidle_disable_device(struct cpuidle_device *dev) |
| 204 | { |
| 205 | if (!dev->enabled) |
| 206 | return; |
| 207 | if (!cpuidle_curr_driver || !cpuidle_curr_governor) |
| 208 | return; |
| 209 | |
| 210 | dev->enabled = 0; |
| 211 | |
| 212 | if (cpuidle_curr_governor->disable) |
| 213 | cpuidle_curr_governor->disable(dev); |
| 214 | |
| 215 | cpuidle_remove_state_sysfs(dev); |
| 216 | enabled_devices--; |
| 217 | } |
| 218 | |
| 219 | EXPORT_SYMBOL_GPL(cpuidle_disable_device); |
| 220 | |
venkatesh.pallipadi@intel.com | 9a0b841 | 2008-01-31 17:35:06 -0800 | [diff] [blame] | 221 | #ifdef CONFIG_ARCH_HAS_CPU_RELAX |
| 222 | static int poll_idle(struct cpuidle_device *dev, struct cpuidle_state *st) |
| 223 | { |
| 224 | ktime_t t1, t2; |
| 225 | s64 diff; |
| 226 | int ret; |
| 227 | |
| 228 | t1 = ktime_get(); |
| 229 | local_irq_enable(); |
| 230 | while (!need_resched()) |
| 231 | cpu_relax(); |
| 232 | |
| 233 | t2 = ktime_get(); |
| 234 | diff = ktime_to_us(ktime_sub(t2, t1)); |
| 235 | if (diff > INT_MAX) |
| 236 | diff = INT_MAX; |
| 237 | |
| 238 | ret = (int) diff; |
| 239 | return ret; |
| 240 | } |
| 241 | |
| 242 | static void poll_idle_init(struct cpuidle_device *dev) |
| 243 | { |
| 244 | struct cpuidle_state *state = &dev->states[0]; |
| 245 | |
| 246 | cpuidle_set_statedata(state, NULL); |
| 247 | |
Venkatesh Pallipadi | 4fcb2fc | 2008-02-11 17:46:31 -0800 | [diff] [blame] | 248 | snprintf(state->name, CPUIDLE_NAME_LEN, "C0"); |
| 249 | snprintf(state->desc, CPUIDLE_DESC_LEN, "CPUIDLE CORE POLL IDLE"); |
venkatesh.pallipadi@intel.com | 9a0b841 | 2008-01-31 17:35:06 -0800 | [diff] [blame] | 250 | state->exit_latency = 0; |
| 251 | state->target_residency = 0; |
| 252 | state->power_usage = -1; |
Venki Pallipadi | 8e92b66 | 2008-02-29 10:24:32 -0800 | [diff] [blame] | 253 | state->flags = CPUIDLE_FLAG_POLL; |
venkatesh.pallipadi@intel.com | 9a0b841 | 2008-01-31 17:35:06 -0800 | [diff] [blame] | 254 | state->enter = poll_idle; |
| 255 | } |
| 256 | #else |
| 257 | static void poll_idle_init(struct cpuidle_device *dev) {} |
| 258 | #endif /* CONFIG_ARCH_HAS_CPU_RELAX */ |
| 259 | |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 260 | /** |
Venkatesh Pallipadi | dcb84f3 | 2008-05-19 19:09:27 -0400 | [diff] [blame] | 261 | * __cpuidle_register_device - internal register function called before register |
| 262 | * and enable routines |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 263 | * @dev: the cpu |
Venkatesh Pallipadi | dcb84f3 | 2008-05-19 19:09:27 -0400 | [diff] [blame] | 264 | * |
| 265 | * cpuidle_lock mutex must be held before this is called |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 266 | */ |
Venkatesh Pallipadi | dcb84f3 | 2008-05-19 19:09:27 -0400 | [diff] [blame] | 267 | static int __cpuidle_register_device(struct cpuidle_device *dev) |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 268 | { |
| 269 | int ret; |
| 270 | struct sys_device *sys_dev = get_cpu_sysdev((unsigned long)dev->cpu); |
| 271 | |
| 272 | if (!sys_dev) |
| 273 | return -EINVAL; |
| 274 | if (!try_module_get(cpuidle_curr_driver->owner)) |
| 275 | return -EINVAL; |
| 276 | |
| 277 | init_completion(&dev->kobj_unregister); |
| 278 | |
venkatesh.pallipadi@intel.com | 9a0b841 | 2008-01-31 17:35:06 -0800 | [diff] [blame] | 279 | poll_idle_init(dev); |
| 280 | |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 281 | per_cpu(cpuidle_devices, dev->cpu) = dev; |
| 282 | list_add(&dev->device_list, &cpuidle_detected_devices); |
| 283 | if ((ret = cpuidle_add_sysfs(sys_dev))) { |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 284 | module_put(cpuidle_curr_driver->owner); |
| 285 | return ret; |
| 286 | } |
| 287 | |
Venkatesh Pallipadi | dcb84f3 | 2008-05-19 19:09:27 -0400 | [diff] [blame] | 288 | dev->registered = 1; |
| 289 | return 0; |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * cpuidle_register_device - registers a CPU's idle PM feature |
| 294 | * @dev: the cpu |
| 295 | */ |
| 296 | int cpuidle_register_device(struct cpuidle_device *dev) |
| 297 | { |
| 298 | int ret; |
| 299 | |
| 300 | mutex_lock(&cpuidle_lock); |
| 301 | |
| 302 | if ((ret = __cpuidle_register_device(dev))) { |
| 303 | mutex_unlock(&cpuidle_lock); |
| 304 | return ret; |
| 305 | } |
| 306 | |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 307 | cpuidle_enable_device(dev); |
| 308 | cpuidle_install_idle_handler(); |
| 309 | |
| 310 | mutex_unlock(&cpuidle_lock); |
| 311 | |
| 312 | return 0; |
| 313 | |
| 314 | } |
| 315 | |
| 316 | EXPORT_SYMBOL_GPL(cpuidle_register_device); |
| 317 | |
| 318 | /** |
| 319 | * cpuidle_unregister_device - unregisters a CPU's idle PM feature |
| 320 | * @dev: the cpu |
| 321 | */ |
| 322 | void cpuidle_unregister_device(struct cpuidle_device *dev) |
| 323 | { |
| 324 | struct sys_device *sys_dev = get_cpu_sysdev((unsigned long)dev->cpu); |
| 325 | |
Venkatesh Pallipadi | dcb84f3 | 2008-05-19 19:09:27 -0400 | [diff] [blame] | 326 | if (dev->registered == 0) |
| 327 | return; |
| 328 | |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 329 | cpuidle_pause_and_lock(); |
| 330 | |
| 331 | cpuidle_disable_device(dev); |
| 332 | |
| 333 | cpuidle_remove_sysfs(sys_dev); |
| 334 | list_del(&dev->device_list); |
| 335 | wait_for_completion(&dev->kobj_unregister); |
| 336 | per_cpu(cpuidle_devices, dev->cpu) = NULL; |
| 337 | |
| 338 | cpuidle_resume_and_unlock(); |
| 339 | |
| 340 | module_put(cpuidle_curr_driver->owner); |
| 341 | } |
| 342 | |
| 343 | EXPORT_SYMBOL_GPL(cpuidle_unregister_device); |
| 344 | |
| 345 | #ifdef CONFIG_SMP |
| 346 | |
| 347 | static void smp_callback(void *v) |
| 348 | { |
| 349 | /* we already woke the CPU up, nothing more to do */ |
| 350 | } |
| 351 | |
| 352 | /* |
| 353 | * This function gets called when a part of the kernel has a new latency |
| 354 | * requirement. This means we need to get all processors out of their C-state, |
| 355 | * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that |
| 356 | * wakes them all right up. |
| 357 | */ |
| 358 | static int cpuidle_latency_notify(struct notifier_block *b, |
| 359 | unsigned long l, void *v) |
| 360 | { |
Jens Axboe | 8691e5a | 2008-06-06 11:18:06 +0200 | [diff] [blame] | 361 | smp_call_function(smp_callback, NULL, 1); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 362 | return NOTIFY_OK; |
| 363 | } |
| 364 | |
| 365 | static struct notifier_block cpuidle_latency_notifier = { |
| 366 | .notifier_call = cpuidle_latency_notify, |
| 367 | }; |
| 368 | |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 369 | static inline void latency_notifier_init(struct notifier_block *n) |
| 370 | { |
| 371 | pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY, n); |
| 372 | } |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 373 | |
| 374 | #else /* CONFIG_SMP */ |
| 375 | |
| 376 | #define latency_notifier_init(x) do { } while (0) |
| 377 | |
| 378 | #endif /* CONFIG_SMP */ |
| 379 | |
| 380 | /** |
| 381 | * cpuidle_init - core initializer |
| 382 | */ |
| 383 | static int __init cpuidle_init(void) |
| 384 | { |
| 385 | int ret; |
| 386 | |
| 387 | pm_idle_old = pm_idle; |
| 388 | |
| 389 | ret = cpuidle_add_class_sysfs(&cpu_sysdev_class); |
| 390 | if (ret) |
| 391 | return ret; |
| 392 | |
| 393 | latency_notifier_init(&cpuidle_latency_notifier); |
| 394 | |
| 395 | return 0; |
| 396 | } |
| 397 | |
| 398 | core_initcall(cpuidle_init); |