]>
Commit | Line | Data |
---|---|---|
4f86d3a8 LB |
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> | |
e8db0be1 | 15 | #include <linux/pm_qos.h> |
4f86d3a8 LB |
16 | #include <linux/cpu.h> |
17 | #include <linux/cpuidle.h> | |
9a0b8415 | 18 | #include <linux/ktime.h> |
2e94d1f7 | 19 | #include <linux/hrtimer.h> |
884b17e1 | 20 | #include <linux/module.h> |
288f023e | 21 | #include <trace/events/power.h> |
4f86d3a8 LB |
22 | |
23 | #include "cpuidle.h" | |
24 | ||
25 | DEFINE_PER_CPU(struct cpuidle_device *, cpuidle_devices); | |
4f86d3a8 LB |
26 | |
27 | DEFINE_MUTEX(cpuidle_lock); | |
28 | LIST_HEAD(cpuidle_detected_devices); | |
4f86d3a8 LB |
29 | |
30 | static int enabled_devices; | |
62027aea | 31 | static int off __read_mostly; |
a0bfa137 | 32 | static int initialized __read_mostly; |
62027aea LB |
33 | |
34 | int cpuidle_disabled(void) | |
35 | { | |
36 | return off; | |
37 | } | |
d91ee586 LB |
38 | void disable_cpuidle(void) |
39 | { | |
40 | off = 1; | |
41 | } | |
4f86d3a8 | 42 | |
a6869cc4 VP |
43 | #if defined(CONFIG_ARCH_HAS_CPU_IDLE_WAIT) |
44 | static void cpuidle_kick_cpus(void) | |
45 | { | |
46 | cpu_idle_wait(); | |
47 | } | |
48 | #elif defined(CONFIG_SMP) | |
49 | # error "Arch needs cpu_idle_wait() equivalent here" | |
50 | #else /* !CONFIG_ARCH_HAS_CPU_IDLE_WAIT && !CONFIG_SMP */ | |
51 | static void cpuidle_kick_cpus(void) {} | |
52 | #endif | |
53 | ||
dcb84f33 VP |
54 | static int __cpuidle_register_device(struct cpuidle_device *dev); |
55 | ||
4f86d3a8 LB |
56 | /** |
57 | * cpuidle_idle_call - the main idle loop | |
58 | * | |
59 | * NOTE: no locks or semaphores should be used here | |
a0bfa137 | 60 | * return non-zero on failure |
4f86d3a8 | 61 | */ |
a0bfa137 | 62 | int cpuidle_idle_call(void) |
4f86d3a8 | 63 | { |
4a6f4fe8 | 64 | struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices); |
46bcfad7 | 65 | struct cpuidle_driver *drv = cpuidle_get_driver(); |
4f86d3a8 | 66 | struct cpuidle_state *target_state; |
e978aa7d | 67 | int next_state, entered_state; |
4f86d3a8 | 68 | |
a0bfa137 LB |
69 | if (off) |
70 | return -ENODEV; | |
71 | ||
72 | if (!initialized) | |
73 | return -ENODEV; | |
74 | ||
4f86d3a8 | 75 | /* check if the device is ready */ |
a0bfa137 LB |
76 | if (!dev || !dev->enabled) |
77 | return -EBUSY; | |
4f86d3a8 | 78 | |
9a655837 AV |
79 | #if 0 |
80 | /* shows regressions, re-enable for 2.6.29 */ | |
2e94d1f7 AV |
81 | /* |
82 | * run any timers that can be run now, at this point | |
83 | * before calculating the idle duration etc. | |
84 | */ | |
85 | hrtimer_peek_ahead_timers(); | |
9a655837 | 86 | #endif |
71abbbf8 | 87 | |
4f86d3a8 | 88 | /* ask the governor for the next state */ |
46bcfad7 | 89 | next_state = cpuidle_curr_governor->select(drv, dev); |
246eb7f0 KH |
90 | if (need_resched()) { |
91 | local_irq_enable(); | |
a0bfa137 | 92 | return 0; |
246eb7f0 KH |
93 | } |
94 | ||
46bcfad7 | 95 | target_state = &drv->states[next_state]; |
f77cfe4e TR |
96 | |
97 | trace_power_start(POWER_CSTATE, next_state, dev->cpu); | |
98 | trace_cpu_idle(next_state, dev->cpu); | |
99 | ||
46bcfad7 | 100 | entered_state = target_state->enter(dev, drv, next_state); |
f77cfe4e TR |
101 | |
102 | trace_power_end(dev->cpu); | |
103 | trace_cpu_idle(PWR_EVENT_EXIT, dev->cpu); | |
104 | ||
e978aa7d DD |
105 | if (entered_state >= 0) { |
106 | /* Update cpuidle counters */ | |
107 | /* This can be moved to within driver enter routine | |
108 | * but that results in multiple copies of same code. | |
109 | */ | |
4202735e | 110 | dev->states_usage[entered_state].time += |
e978aa7d | 111 | (unsigned long long)dev->last_residency; |
4202735e | 112 | dev->states_usage[entered_state].usage++; |
e978aa7d | 113 | } |
4f86d3a8 LB |
114 | |
115 | /* give the governor an opportunity to reflect on the outcome */ | |
116 | if (cpuidle_curr_governor->reflect) | |
e978aa7d | 117 | cpuidle_curr_governor->reflect(dev, entered_state); |
a0bfa137 LB |
118 | |
119 | return 0; | |
4f86d3a8 LB |
120 | } |
121 | ||
122 | /** | |
123 | * cpuidle_install_idle_handler - installs the cpuidle idle loop handler | |
124 | */ | |
125 | void cpuidle_install_idle_handler(void) | |
126 | { | |
a0bfa137 | 127 | if (enabled_devices) { |
4f86d3a8 LB |
128 | /* Make sure all changes finished before we switch to new idle */ |
129 | smp_wmb(); | |
a0bfa137 | 130 | initialized = 1; |
4f86d3a8 LB |
131 | } |
132 | } | |
133 | ||
134 | /** | |
135 | * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler | |
136 | */ | |
137 | void cpuidle_uninstall_idle_handler(void) | |
138 | { | |
a0bfa137 LB |
139 | if (enabled_devices) { |
140 | initialized = 0; | |
a6869cc4 | 141 | cpuidle_kick_cpus(); |
4f86d3a8 LB |
142 | } |
143 | } | |
144 | ||
145 | /** | |
146 | * cpuidle_pause_and_lock - temporarily disables CPUIDLE | |
147 | */ | |
148 | void cpuidle_pause_and_lock(void) | |
149 | { | |
150 | mutex_lock(&cpuidle_lock); | |
151 | cpuidle_uninstall_idle_handler(); | |
152 | } | |
153 | ||
154 | EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock); | |
155 | ||
156 | /** | |
157 | * cpuidle_resume_and_unlock - resumes CPUIDLE operation | |
158 | */ | |
159 | void cpuidle_resume_and_unlock(void) | |
160 | { | |
161 | cpuidle_install_idle_handler(); | |
162 | mutex_unlock(&cpuidle_lock); | |
163 | } | |
164 | ||
165 | EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock); | |
166 | ||
d8c216cf | 167 | #ifdef CONFIG_ARCH_HAS_CPU_RELAX |
46bcfad7 DD |
168 | static int poll_idle(struct cpuidle_device *dev, |
169 | struct cpuidle_driver *drv, int index) | |
d8c216cf RW |
170 | { |
171 | ktime_t t1, t2; | |
172 | s64 diff; | |
d8c216cf RW |
173 | |
174 | t1 = ktime_get(); | |
175 | local_irq_enable(); | |
176 | while (!need_resched()) | |
177 | cpu_relax(); | |
178 | ||
179 | t2 = ktime_get(); | |
180 | diff = ktime_to_us(ktime_sub(t2, t1)); | |
181 | if (diff > INT_MAX) | |
182 | diff = INT_MAX; | |
183 | ||
e978aa7d DD |
184 | dev->last_residency = (int) diff; |
185 | ||
186 | return index; | |
d8c216cf RW |
187 | } |
188 | ||
46bcfad7 | 189 | static void poll_idle_init(struct cpuidle_driver *drv) |
d8c216cf | 190 | { |
46bcfad7 | 191 | struct cpuidle_state *state = &drv->states[0]; |
d8c216cf | 192 | |
720f1c30 | 193 | snprintf(state->name, CPUIDLE_NAME_LEN, "POLL"); |
d8c216cf RW |
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; | |
d247632c | 198 | state->flags = 0; |
d8c216cf RW |
199 | state->enter = poll_idle; |
200 | } | |
201 | #else | |
46bcfad7 | 202 | static void poll_idle_init(struct cpuidle_driver *drv) {} |
d8c216cf RW |
203 | #endif /* CONFIG_ARCH_HAS_CPU_RELAX */ |
204 | ||
4f86d3a8 LB |
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; | |
752138df | 218 | if (!cpuidle_get_driver() || !cpuidle_curr_governor) |
4f86d3a8 LB |
219 | return -EIO; |
220 | if (!dev->state_count) | |
221 | return -EINVAL; | |
222 | ||
dcb84f33 VP |
223 | if (dev->registered == 0) { |
224 | ret = __cpuidle_register_device(dev); | |
225 | if (ret) | |
226 | return ret; | |
227 | } | |
228 | ||
46bcfad7 | 229 | poll_idle_init(cpuidle_get_driver()); |
d8c216cf | 230 | |
4f86d3a8 LB |
231 | if ((ret = cpuidle_add_state_sysfs(dev))) |
232 | return ret; | |
233 | ||
234 | if (cpuidle_curr_governor->enable && | |
46bcfad7 | 235 | (ret = cpuidle_curr_governor->enable(cpuidle_get_driver(), dev))) |
4f86d3a8 LB |
236 | goto fail_sysfs; |
237 | ||
238 | for (i = 0; i < dev->state_count; i++) { | |
4202735e DD |
239 | dev->states_usage[i].usage = 0; |
240 | dev->states_usage[i].time = 0; | |
4f86d3a8 LB |
241 | } |
242 | dev->last_residency = 0; | |
4f86d3a8 LB |
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; | |
752138df | 270 | if (!cpuidle_get_driver() || !cpuidle_curr_governor) |
4f86d3a8 LB |
271 | return; |
272 | ||
273 | dev->enabled = 0; | |
274 | ||
275 | if (cpuidle_curr_governor->disable) | |
46bcfad7 | 276 | cpuidle_curr_governor->disable(cpuidle_get_driver(), dev); |
4f86d3a8 LB |
277 | |
278 | cpuidle_remove_state_sysfs(dev); | |
279 | enabled_devices--; | |
280 | } | |
281 | ||
282 | EXPORT_SYMBOL_GPL(cpuidle_disable_device); | |
283 | ||
284 | /** | |
dcb84f33 VP |
285 | * __cpuidle_register_device - internal register function called before register |
286 | * and enable routines | |
4f86d3a8 | 287 | * @dev: the cpu |
dcb84f33 VP |
288 | * |
289 | * cpuidle_lock mutex must be held before this is called | |
4f86d3a8 | 290 | */ |
dcb84f33 | 291 | static int __cpuidle_register_device(struct cpuidle_device *dev) |
4f86d3a8 LB |
292 | { |
293 | int ret; | |
294 | struct sys_device *sys_dev = get_cpu_sysdev((unsigned long)dev->cpu); | |
752138df | 295 | struct cpuidle_driver *cpuidle_driver = cpuidle_get_driver(); |
4f86d3a8 LB |
296 | |
297 | if (!sys_dev) | |
298 | return -EINVAL; | |
752138df | 299 | if (!try_module_get(cpuidle_driver->owner)) |
4f86d3a8 LB |
300 | return -EINVAL; |
301 | ||
302 | init_completion(&dev->kobj_unregister); | |
303 | ||
4f86d3a8 LB |
304 | per_cpu(cpuidle_devices, dev->cpu) = dev; |
305 | list_add(&dev->device_list, &cpuidle_detected_devices); | |
306 | if ((ret = cpuidle_add_sysfs(sys_dev))) { | |
752138df | 307 | module_put(cpuidle_driver->owner); |
4f86d3a8 LB |
308 | return ret; |
309 | } | |
310 | ||
dcb84f33 VP |
311 | dev->registered = 1; |
312 | return 0; | |
313 | } | |
314 | ||
315 | /** | |
316 | * cpuidle_register_device - registers a CPU's idle PM feature | |
317 | * @dev: the cpu | |
318 | */ | |
319 | int cpuidle_register_device(struct cpuidle_device *dev) | |
320 | { | |
321 | int ret; | |
322 | ||
323 | mutex_lock(&cpuidle_lock); | |
324 | ||
325 | if ((ret = __cpuidle_register_device(dev))) { | |
326 | mutex_unlock(&cpuidle_lock); | |
327 | return ret; | |
328 | } | |
329 | ||
4f86d3a8 LB |
330 | cpuidle_enable_device(dev); |
331 | cpuidle_install_idle_handler(); | |
332 | ||
333 | mutex_unlock(&cpuidle_lock); | |
334 | ||
335 | return 0; | |
336 | ||
337 | } | |
338 | ||
339 | EXPORT_SYMBOL_GPL(cpuidle_register_device); | |
340 | ||
341 | /** | |
342 | * cpuidle_unregister_device - unregisters a CPU's idle PM feature | |
343 | * @dev: the cpu | |
344 | */ | |
345 | void cpuidle_unregister_device(struct cpuidle_device *dev) | |
346 | { | |
347 | struct sys_device *sys_dev = get_cpu_sysdev((unsigned long)dev->cpu); | |
752138df | 348 | struct cpuidle_driver *cpuidle_driver = cpuidle_get_driver(); |
4f86d3a8 | 349 | |
dcb84f33 VP |
350 | if (dev->registered == 0) |
351 | return; | |
352 | ||
4f86d3a8 LB |
353 | cpuidle_pause_and_lock(); |
354 | ||
355 | cpuidle_disable_device(dev); | |
356 | ||
357 | cpuidle_remove_sysfs(sys_dev); | |
358 | list_del(&dev->device_list); | |
359 | wait_for_completion(&dev->kobj_unregister); | |
360 | per_cpu(cpuidle_devices, dev->cpu) = NULL; | |
361 | ||
362 | cpuidle_resume_and_unlock(); | |
363 | ||
752138df | 364 | module_put(cpuidle_driver->owner); |
4f86d3a8 LB |
365 | } |
366 | ||
367 | EXPORT_SYMBOL_GPL(cpuidle_unregister_device); | |
368 | ||
369 | #ifdef CONFIG_SMP | |
370 | ||
371 | static void smp_callback(void *v) | |
372 | { | |
373 | /* we already woke the CPU up, nothing more to do */ | |
374 | } | |
375 | ||
376 | /* | |
377 | * This function gets called when a part of the kernel has a new latency | |
378 | * requirement. This means we need to get all processors out of their C-state, | |
379 | * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that | |
380 | * wakes them all right up. | |
381 | */ | |
382 | static int cpuidle_latency_notify(struct notifier_block *b, | |
383 | unsigned long l, void *v) | |
384 | { | |
8691e5a8 | 385 | smp_call_function(smp_callback, NULL, 1); |
4f86d3a8 LB |
386 | return NOTIFY_OK; |
387 | } | |
388 | ||
389 | static struct notifier_block cpuidle_latency_notifier = { | |
390 | .notifier_call = cpuidle_latency_notify, | |
391 | }; | |
392 | ||
d82b3518 MG |
393 | static inline void latency_notifier_init(struct notifier_block *n) |
394 | { | |
395 | pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY, n); | |
396 | } | |
4f86d3a8 LB |
397 | |
398 | #else /* CONFIG_SMP */ | |
399 | ||
400 | #define latency_notifier_init(x) do { } while (0) | |
401 | ||
402 | #endif /* CONFIG_SMP */ | |
403 | ||
404 | /** | |
405 | * cpuidle_init - core initializer | |
406 | */ | |
407 | static int __init cpuidle_init(void) | |
408 | { | |
409 | int ret; | |
410 | ||
62027aea LB |
411 | if (cpuidle_disabled()) |
412 | return -ENODEV; | |
413 | ||
4f86d3a8 LB |
414 | ret = cpuidle_add_class_sysfs(&cpu_sysdev_class); |
415 | if (ret) | |
416 | return ret; | |
417 | ||
418 | latency_notifier_init(&cpuidle_latency_notifier); | |
419 | ||
420 | return 0; | |
421 | } | |
422 | ||
62027aea | 423 | module_param(off, int, 0444); |
4f86d3a8 | 424 | core_initcall(cpuidle_init); |