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); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 28 | |
| 29 | static int enabled_devices; |
Len Brown | 62027ae | 2011-04-01 18:13:10 -0400 | [diff] [blame] | 30 | static int off __read_mostly; |
Len Brown | a0bfa13 | 2011-04-01 19:34:59 -0400 | [diff] [blame] | 31 | static int initialized __read_mostly; |
Len Brown | 62027ae | 2011-04-01 18:13:10 -0400 | [diff] [blame] | 32 | |
| 33 | int cpuidle_disabled(void) |
| 34 | { |
| 35 | return off; |
| 36 | } |
Len Brown | d91ee58 | 2011-04-01 18:28:35 -0400 | [diff] [blame] | 37 | void disable_cpuidle(void) |
| 38 | { |
| 39 | off = 1; |
| 40 | } |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 41 | |
Venki Pallipadi | a6869cc | 2008-02-08 17:05:44 -0800 | [diff] [blame] | 42 | #if defined(CONFIG_ARCH_HAS_CPU_IDLE_WAIT) |
| 43 | static void cpuidle_kick_cpus(void) |
| 44 | { |
| 45 | cpu_idle_wait(); |
| 46 | } |
| 47 | #elif defined(CONFIG_SMP) |
| 48 | # error "Arch needs cpu_idle_wait() equivalent here" |
| 49 | #else /* !CONFIG_ARCH_HAS_CPU_IDLE_WAIT && !CONFIG_SMP */ |
| 50 | static void cpuidle_kick_cpus(void) {} |
| 51 | #endif |
| 52 | |
Venkatesh Pallipadi | dcb84f3 | 2008-05-19 19:09:27 -0400 | [diff] [blame] | 53 | static int __cpuidle_register_device(struct cpuidle_device *dev); |
| 54 | |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 55 | /** |
| 56 | * cpuidle_idle_call - the main idle loop |
| 57 | * |
| 58 | * NOTE: no locks or semaphores should be used here |
Len Brown | a0bfa13 | 2011-04-01 19:34:59 -0400 | [diff] [blame] | 59 | * return non-zero on failure |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 60 | */ |
Len Brown | a0bfa13 | 2011-04-01 19:34:59 -0400 | [diff] [blame] | 61 | int cpuidle_idle_call(void) |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 62 | { |
Christoph Lameter | 4a6f4fe | 2010-12-06 11:16:24 -0600 | [diff] [blame] | 63 | struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 64 | struct cpuidle_state *target_state; |
Deepthi Dharwar | e978aa7 | 2011-10-28 16:20:09 +0530 | [diff] [blame] | 65 | int next_state, entered_state; |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 66 | |
Len Brown | a0bfa13 | 2011-04-01 19:34:59 -0400 | [diff] [blame] | 67 | if (off) |
| 68 | return -ENODEV; |
| 69 | |
| 70 | if (!initialized) |
| 71 | return -ENODEV; |
| 72 | |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 73 | /* check if the device is ready */ |
Len Brown | a0bfa13 | 2011-04-01 19:34:59 -0400 | [diff] [blame] | 74 | if (!dev || !dev->enabled) |
| 75 | return -EBUSY; |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 76 | |
Arjan van de Ven | 9a65583 | 2008-11-09 12:45:10 -0800 | [diff] [blame] | 77 | #if 0 |
| 78 | /* shows regressions, re-enable for 2.6.29 */ |
Arjan van de Ven | 2e94d1f | 2008-09-10 16:06:00 -0700 | [diff] [blame] | 79 | /* |
| 80 | * run any timers that can be run now, at this point |
| 81 | * before calculating the idle duration etc. |
| 82 | */ |
| 83 | hrtimer_peek_ahead_timers(); |
Arjan van de Ven | 9a65583 | 2008-11-09 12:45:10 -0800 | [diff] [blame] | 84 | #endif |
Ai Li | 71abbbf | 2010-08-09 17:20:13 -0700 | [diff] [blame] | 85 | |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 86 | /* ask the governor for the next state */ |
| 87 | next_state = cpuidle_curr_governor->select(dev); |
Kevin Hilman | 246eb7f | 2009-10-26 16:50:18 -0700 | [diff] [blame] | 88 | if (need_resched()) { |
| 89 | local_irq_enable(); |
Len Brown | a0bfa13 | 2011-04-01 19:34:59 -0400 | [diff] [blame] | 90 | return 0; |
Kevin Hilman | 246eb7f | 2009-10-26 16:50:18 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 93 | target_state = &dev->states[next_state]; |
| 94 | |
Thomas Renninger | f77cfe4 | 2011-01-07 11:29:44 +0100 | [diff] [blame] | 95 | trace_power_start(POWER_CSTATE, next_state, dev->cpu); |
| 96 | trace_cpu_idle(next_state, dev->cpu); |
| 97 | |
Deepthi Dharwar | e978aa7 | 2011-10-28 16:20:09 +0530 | [diff] [blame] | 98 | entered_state = target_state->enter(dev, next_state); |
Thomas Renninger | f77cfe4 | 2011-01-07 11:29:44 +0100 | [diff] [blame] | 99 | |
| 100 | trace_power_end(dev->cpu); |
| 101 | trace_cpu_idle(PWR_EVENT_EXIT, dev->cpu); |
| 102 | |
Deepthi Dharwar | e978aa7 | 2011-10-28 16:20:09 +0530 | [diff] [blame] | 103 | if (entered_state >= 0) { |
| 104 | /* Update cpuidle counters */ |
| 105 | /* This can be moved to within driver enter routine |
| 106 | * but that results in multiple copies of same code. |
| 107 | */ |
Deepthi Dharwar | 4202735 | 2011-10-28 16:20:33 +0530 | [diff] [blame^] | 108 | dev->states_usage[entered_state].time += |
Deepthi Dharwar | e978aa7 | 2011-10-28 16:20:09 +0530 | [diff] [blame] | 109 | (unsigned long long)dev->last_residency; |
Deepthi Dharwar | 4202735 | 2011-10-28 16:20:33 +0530 | [diff] [blame^] | 110 | dev->states_usage[entered_state].usage++; |
Deepthi Dharwar | e978aa7 | 2011-10-28 16:20:09 +0530 | [diff] [blame] | 111 | } |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 112 | |
| 113 | /* give the governor an opportunity to reflect on the outcome */ |
| 114 | if (cpuidle_curr_governor->reflect) |
Deepthi Dharwar | e978aa7 | 2011-10-28 16:20:09 +0530 | [diff] [blame] | 115 | cpuidle_curr_governor->reflect(dev, entered_state); |
Len Brown | a0bfa13 | 2011-04-01 19:34:59 -0400 | [diff] [blame] | 116 | |
| 117 | return 0; |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | /** |
| 121 | * cpuidle_install_idle_handler - installs the cpuidle idle loop handler |
| 122 | */ |
| 123 | void cpuidle_install_idle_handler(void) |
| 124 | { |
Len Brown | a0bfa13 | 2011-04-01 19:34:59 -0400 | [diff] [blame] | 125 | if (enabled_devices) { |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 126 | /* Make sure all changes finished before we switch to new idle */ |
| 127 | smp_wmb(); |
Len Brown | a0bfa13 | 2011-04-01 19:34:59 -0400 | [diff] [blame] | 128 | initialized = 1; |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler |
| 134 | */ |
| 135 | void cpuidle_uninstall_idle_handler(void) |
| 136 | { |
Len Brown | a0bfa13 | 2011-04-01 19:34:59 -0400 | [diff] [blame] | 137 | if (enabled_devices) { |
| 138 | initialized = 0; |
Venki Pallipadi | a6869cc | 2008-02-08 17:05:44 -0800 | [diff] [blame] | 139 | cpuidle_kick_cpus(); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * cpuidle_pause_and_lock - temporarily disables CPUIDLE |
| 145 | */ |
| 146 | void cpuidle_pause_and_lock(void) |
| 147 | { |
| 148 | mutex_lock(&cpuidle_lock); |
| 149 | cpuidle_uninstall_idle_handler(); |
| 150 | } |
| 151 | |
| 152 | EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock); |
| 153 | |
| 154 | /** |
| 155 | * cpuidle_resume_and_unlock - resumes CPUIDLE operation |
| 156 | */ |
| 157 | void cpuidle_resume_and_unlock(void) |
| 158 | { |
| 159 | cpuidle_install_idle_handler(); |
| 160 | mutex_unlock(&cpuidle_lock); |
| 161 | } |
| 162 | |
| 163 | EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock); |
| 164 | |
Rafael J. Wysocki | d8c216c | 2011-01-08 00:29:20 +0100 | [diff] [blame] | 165 | #ifdef CONFIG_ARCH_HAS_CPU_RELAX |
Deepthi Dharwar | e978aa7 | 2011-10-28 16:20:09 +0530 | [diff] [blame] | 166 | static int poll_idle(struct cpuidle_device *dev, int index) |
Rafael J. Wysocki | d8c216c | 2011-01-08 00:29:20 +0100 | [diff] [blame] | 167 | { |
| 168 | ktime_t t1, t2; |
| 169 | s64 diff; |
Rafael J. Wysocki | d8c216c | 2011-01-08 00:29:20 +0100 | [diff] [blame] | 170 | |
| 171 | t1 = ktime_get(); |
| 172 | local_irq_enable(); |
| 173 | while (!need_resched()) |
| 174 | cpu_relax(); |
| 175 | |
| 176 | t2 = ktime_get(); |
| 177 | diff = ktime_to_us(ktime_sub(t2, t1)); |
| 178 | if (diff > INT_MAX) |
| 179 | diff = INT_MAX; |
| 180 | |
Deepthi Dharwar | e978aa7 | 2011-10-28 16:20:09 +0530 | [diff] [blame] | 181 | dev->last_residency = (int) diff; |
| 182 | |
| 183 | return index; |
Rafael J. Wysocki | d8c216c | 2011-01-08 00:29:20 +0100 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | static void poll_idle_init(struct cpuidle_device *dev) |
| 187 | { |
| 188 | struct cpuidle_state *state = &dev->states[0]; |
Deepthi Dharwar | 4202735 | 2011-10-28 16:20:33 +0530 | [diff] [blame^] | 189 | struct cpuidle_state_usage *state_usage = &dev->states_usage[0]; |
Rafael J. Wysocki | d8c216c | 2011-01-08 00:29:20 +0100 | [diff] [blame] | 190 | |
Deepthi Dharwar | 4202735 | 2011-10-28 16:20:33 +0530 | [diff] [blame^] | 191 | cpuidle_set_statedata(state_usage, NULL); |
Rafael J. Wysocki | d8c216c | 2011-01-08 00:29:20 +0100 | [diff] [blame] | 192 | |
Thomas Renninger | 720f1c3 | 2011-01-07 11:29:43 +0100 | [diff] [blame] | 193 | snprintf(state->name, CPUIDLE_NAME_LEN, "POLL"); |
Rafael J. Wysocki | d8c216c | 2011-01-08 00:29:20 +0100 | [diff] [blame] | 194 | snprintf(state->desc, CPUIDLE_DESC_LEN, "CPUIDLE CORE POLL IDLE"); |
| 195 | state->exit_latency = 0; |
| 196 | state->target_residency = 0; |
| 197 | state->power_usage = -1; |
Len Brown | d247632 | 2011-01-12 02:34:59 -0500 | [diff] [blame] | 198 | state->flags = 0; |
Rafael J. Wysocki | d8c216c | 2011-01-08 00:29:20 +0100 | [diff] [blame] | 199 | state->enter = poll_idle; |
| 200 | } |
| 201 | #else |
| 202 | static void poll_idle_init(struct cpuidle_device *dev) {} |
| 203 | #endif /* CONFIG_ARCH_HAS_CPU_RELAX */ |
| 204 | |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 205 | /** |
| 206 | * cpuidle_enable_device - enables idle PM for a CPU |
| 207 | * @dev: the CPU |
| 208 | * |
| 209 | * This function must be called between cpuidle_pause_and_lock and |
| 210 | * cpuidle_resume_and_unlock when used externally. |
| 211 | */ |
| 212 | int cpuidle_enable_device(struct cpuidle_device *dev) |
| 213 | { |
| 214 | int ret, i; |
| 215 | |
| 216 | if (dev->enabled) |
| 217 | return 0; |
Len Brown | 752138d | 2010-05-22 16:57:26 -0400 | [diff] [blame] | 218 | if (!cpuidle_get_driver() || !cpuidle_curr_governor) |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 219 | return -EIO; |
| 220 | if (!dev->state_count) |
| 221 | return -EINVAL; |
| 222 | |
Venkatesh Pallipadi | dcb84f3 | 2008-05-19 19:09:27 -0400 | [diff] [blame] | 223 | if (dev->registered == 0) { |
| 224 | ret = __cpuidle_register_device(dev); |
| 225 | if (ret) |
| 226 | return ret; |
| 227 | } |
| 228 | |
Rafael J. Wysocki | d8c216c | 2011-01-08 00:29:20 +0100 | [diff] [blame] | 229 | poll_idle_init(dev); |
| 230 | |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 231 | if ((ret = cpuidle_add_state_sysfs(dev))) |
| 232 | return ret; |
| 233 | |
| 234 | if (cpuidle_curr_governor->enable && |
| 235 | (ret = cpuidle_curr_governor->enable(dev))) |
| 236 | goto fail_sysfs; |
| 237 | |
| 238 | for (i = 0; i < dev->state_count; i++) { |
Deepthi Dharwar | 4202735 | 2011-10-28 16:20:33 +0530 | [diff] [blame^] | 239 | dev->states_usage[i].usage = 0; |
| 240 | dev->states_usage[i].time = 0; |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 241 | } |
| 242 | dev->last_residency = 0; |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 243 | |
| 244 | smp_wmb(); |
| 245 | |
| 246 | dev->enabled = 1; |
| 247 | |
| 248 | enabled_devices++; |
| 249 | return 0; |
| 250 | |
| 251 | fail_sysfs: |
| 252 | cpuidle_remove_state_sysfs(dev); |
| 253 | |
| 254 | return ret; |
| 255 | } |
| 256 | |
| 257 | EXPORT_SYMBOL_GPL(cpuidle_enable_device); |
| 258 | |
| 259 | /** |
| 260 | * cpuidle_disable_device - disables idle PM for a CPU |
| 261 | * @dev: the CPU |
| 262 | * |
| 263 | * This function must be called between cpuidle_pause_and_lock and |
| 264 | * cpuidle_resume_and_unlock when used externally. |
| 265 | */ |
| 266 | void cpuidle_disable_device(struct cpuidle_device *dev) |
| 267 | { |
| 268 | if (!dev->enabled) |
| 269 | return; |
Len Brown | 752138d | 2010-05-22 16:57:26 -0400 | [diff] [blame] | 270 | if (!cpuidle_get_driver() || !cpuidle_curr_governor) |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 271 | return; |
| 272 | |
| 273 | dev->enabled = 0; |
| 274 | |
| 275 | if (cpuidle_curr_governor->disable) |
| 276 | cpuidle_curr_governor->disable(dev); |
| 277 | |
| 278 | cpuidle_remove_state_sysfs(dev); |
| 279 | enabled_devices--; |
| 280 | } |
| 281 | |
| 282 | EXPORT_SYMBOL_GPL(cpuidle_disable_device); |
| 283 | |
| 284 | /** |
Venkatesh Pallipadi | dcb84f3 | 2008-05-19 19:09:27 -0400 | [diff] [blame] | 285 | * __cpuidle_register_device - internal register function called before register |
| 286 | * and enable routines |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 287 | * @dev: the cpu |
Venkatesh Pallipadi | dcb84f3 | 2008-05-19 19:09:27 -0400 | [diff] [blame] | 288 | * |
| 289 | * cpuidle_lock mutex must be held before this is called |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 290 | */ |
Venkatesh Pallipadi | dcb84f3 | 2008-05-19 19:09:27 -0400 | [diff] [blame] | 291 | static int __cpuidle_register_device(struct cpuidle_device *dev) |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 292 | { |
| 293 | int ret; |
| 294 | struct sys_device *sys_dev = get_cpu_sysdev((unsigned long)dev->cpu); |
Len Brown | 752138d | 2010-05-22 16:57:26 -0400 | [diff] [blame] | 295 | struct cpuidle_driver *cpuidle_driver = cpuidle_get_driver(); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 296 | |
| 297 | if (!sys_dev) |
| 298 | return -EINVAL; |
Len Brown | 752138d | 2010-05-22 16:57:26 -0400 | [diff] [blame] | 299 | if (!try_module_get(cpuidle_driver->owner)) |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 300 | return -EINVAL; |
| 301 | |
| 302 | init_completion(&dev->kobj_unregister); |
| 303 | |
Ai Li | 71abbbf | 2010-08-09 17:20:13 -0700 | [diff] [blame] | 304 | /* |
| 305 | * cpuidle driver should set the dev->power_specified bit |
| 306 | * before registering the device if the driver provides |
| 307 | * power_usage numbers. |
| 308 | * |
| 309 | * For those devices whose ->power_specified is not set, |
| 310 | * we fill in power_usage with decreasing values as the |
| 311 | * cpuidle code has an implicit assumption that state Cn |
| 312 | * uses less power than C(n-1). |
| 313 | * |
| 314 | * With CONFIG_ARCH_HAS_CPU_RELAX, C0 is already assigned |
| 315 | * an power value of -1. So we use -2, -3, etc, for other |
| 316 | * c-states. |
| 317 | */ |
| 318 | if (!dev->power_specified) { |
| 319 | int i; |
| 320 | for (i = CPUIDLE_DRIVER_STATE_START; i < dev->state_count; i++) |
| 321 | dev->states[i].power_usage = -1 - i; |
| 322 | } |
| 323 | |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 324 | per_cpu(cpuidle_devices, dev->cpu) = dev; |
| 325 | list_add(&dev->device_list, &cpuidle_detected_devices); |
| 326 | if ((ret = cpuidle_add_sysfs(sys_dev))) { |
Len Brown | 752138d | 2010-05-22 16:57:26 -0400 | [diff] [blame] | 327 | module_put(cpuidle_driver->owner); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 328 | return ret; |
| 329 | } |
| 330 | |
Venkatesh Pallipadi | dcb84f3 | 2008-05-19 19:09:27 -0400 | [diff] [blame] | 331 | dev->registered = 1; |
| 332 | return 0; |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * cpuidle_register_device - registers a CPU's idle PM feature |
| 337 | * @dev: the cpu |
| 338 | */ |
| 339 | int cpuidle_register_device(struct cpuidle_device *dev) |
| 340 | { |
| 341 | int ret; |
| 342 | |
| 343 | mutex_lock(&cpuidle_lock); |
| 344 | |
| 345 | if ((ret = __cpuidle_register_device(dev))) { |
| 346 | mutex_unlock(&cpuidle_lock); |
| 347 | return ret; |
| 348 | } |
| 349 | |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 350 | cpuidle_enable_device(dev); |
| 351 | cpuidle_install_idle_handler(); |
| 352 | |
| 353 | mutex_unlock(&cpuidle_lock); |
| 354 | |
| 355 | return 0; |
| 356 | |
| 357 | } |
| 358 | |
| 359 | EXPORT_SYMBOL_GPL(cpuidle_register_device); |
| 360 | |
| 361 | /** |
| 362 | * cpuidle_unregister_device - unregisters a CPU's idle PM feature |
| 363 | * @dev: the cpu |
| 364 | */ |
| 365 | void cpuidle_unregister_device(struct cpuidle_device *dev) |
| 366 | { |
| 367 | struct sys_device *sys_dev = get_cpu_sysdev((unsigned long)dev->cpu); |
Len Brown | 752138d | 2010-05-22 16:57:26 -0400 | [diff] [blame] | 368 | struct cpuidle_driver *cpuidle_driver = cpuidle_get_driver(); |
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 | if (dev->registered == 0) |
| 371 | return; |
| 372 | |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 373 | cpuidle_pause_and_lock(); |
| 374 | |
| 375 | cpuidle_disable_device(dev); |
| 376 | |
| 377 | cpuidle_remove_sysfs(sys_dev); |
| 378 | list_del(&dev->device_list); |
| 379 | wait_for_completion(&dev->kobj_unregister); |
| 380 | per_cpu(cpuidle_devices, dev->cpu) = NULL; |
| 381 | |
| 382 | cpuidle_resume_and_unlock(); |
| 383 | |
Len Brown | 752138d | 2010-05-22 16:57:26 -0400 | [diff] [blame] | 384 | module_put(cpuidle_driver->owner); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | EXPORT_SYMBOL_GPL(cpuidle_unregister_device); |
| 388 | |
| 389 | #ifdef CONFIG_SMP |
| 390 | |
| 391 | static void smp_callback(void *v) |
| 392 | { |
| 393 | /* we already woke the CPU up, nothing more to do */ |
| 394 | } |
| 395 | |
| 396 | /* |
| 397 | * This function gets called when a part of the kernel has a new latency |
| 398 | * requirement. This means we need to get all processors out of their C-state, |
| 399 | * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that |
| 400 | * wakes them all right up. |
| 401 | */ |
| 402 | static int cpuidle_latency_notify(struct notifier_block *b, |
| 403 | unsigned long l, void *v) |
| 404 | { |
Jens Axboe | 8691e5a | 2008-06-06 11:18:06 +0200 | [diff] [blame] | 405 | smp_call_function(smp_callback, NULL, 1); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 406 | return NOTIFY_OK; |
| 407 | } |
| 408 | |
| 409 | static struct notifier_block cpuidle_latency_notifier = { |
| 410 | .notifier_call = cpuidle_latency_notify, |
| 411 | }; |
| 412 | |
Mark Gross | d82b351 | 2008-02-04 22:30:08 -0800 | [diff] [blame] | 413 | static inline void latency_notifier_init(struct notifier_block *n) |
| 414 | { |
| 415 | pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY, n); |
| 416 | } |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 417 | |
| 418 | #else /* CONFIG_SMP */ |
| 419 | |
| 420 | #define latency_notifier_init(x) do { } while (0) |
| 421 | |
| 422 | #endif /* CONFIG_SMP */ |
| 423 | |
| 424 | /** |
| 425 | * cpuidle_init - core initializer |
| 426 | */ |
| 427 | static int __init cpuidle_init(void) |
| 428 | { |
| 429 | int ret; |
| 430 | |
Len Brown | 62027ae | 2011-04-01 18:13:10 -0400 | [diff] [blame] | 431 | if (cpuidle_disabled()) |
| 432 | return -ENODEV; |
| 433 | |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 434 | ret = cpuidle_add_class_sysfs(&cpu_sysdev_class); |
| 435 | if (ret) |
| 436 | return ret; |
| 437 | |
| 438 | latency_notifier_init(&cpuidle_latency_notifier); |
| 439 | |
| 440 | return 0; |
| 441 | } |
| 442 | |
Len Brown | 62027ae | 2011-04-01 18:13:10 -0400 | [diff] [blame] | 443 | module_param(off, int, 0444); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 444 | core_initcall(cpuidle_init); |