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