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