cpuidle : handle clockevent notify from the cpuidle framework
[deliverable/linux.git] / drivers / cpuidle / cpuidle.c
CommitLineData
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
b60e6a0e 11#include <linux/clockchips.h>
4f86d3a8
LB
12#include <linux/kernel.h>
13#include <linux/mutex.h>
14#include <linux/sched.h>
15#include <linux/notifier.h>
e8db0be1 16#include <linux/pm_qos.h>
4f86d3a8
LB
17#include <linux/cpu.h>
18#include <linux/cpuidle.h>
9a0b8415 19#include <linux/ktime.h>
2e94d1f7 20#include <linux/hrtimer.h>
884b17e1 21#include <linux/module.h>
288f023e 22#include <trace/events/power.h>
4f86d3a8
LB
23
24#include "cpuidle.h"
25
26DEFINE_PER_CPU(struct cpuidle_device *, cpuidle_devices);
4f86d3a8
LB
27
28DEFINE_MUTEX(cpuidle_lock);
29LIST_HEAD(cpuidle_detected_devices);
4f86d3a8
LB
30
31static int enabled_devices;
62027aea 32static int off __read_mostly;
a0bfa137 33static int initialized __read_mostly;
62027aea
LB
34
35int cpuidle_disabled(void)
36{
37 return off;
38}
d91ee586
LB
39void disable_cpuidle(void)
40{
41 off = 1;
42}
4f86d3a8 43
dcb84f33
VP
44static int __cpuidle_register_device(struct cpuidle_device *dev);
45
e1689795
RL
46static inline int cpuidle_enter(struct cpuidle_device *dev,
47 struct cpuidle_driver *drv, int index)
48{
49 struct cpuidle_state *target_state = &drv->states[index];
50 return target_state->enter(dev, drv, index);
51}
52
53static inline int cpuidle_enter_tk(struct cpuidle_device *dev,
54 struct cpuidle_driver *drv, int index)
55{
56 return cpuidle_wrap_enter(dev, drv, index, cpuidle_enter);
57}
58
59typedef int (*cpuidle_enter_t)(struct cpuidle_device *dev,
60 struct cpuidle_driver *drv, int index);
61
62static cpuidle_enter_t cpuidle_enter_ops;
63
1a022e3f
BO
64/**
65 * cpuidle_play_dead - cpu off-lining
66 *
ee01e663 67 * Returns in case of an error or no driver
1a022e3f
BO
68 */
69int cpuidle_play_dead(void)
70{
71 struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices);
bf4d1b5d 72 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
8aef33a7 73 int i;
1a022e3f 74
ee01e663
TK
75 if (!drv)
76 return -ENODEV;
77
1a022e3f 78 /* Find lowest-power state that supports long-term idle */
8aef33a7
DL
79 for (i = drv->state_count - 1; i >= CPUIDLE_DRIVER_STATE_START; i--)
80 if (drv->states[i].enter_dead)
81 return drv->states[i].enter_dead(dev, i);
1a022e3f
BO
82
83 return -ENODEV;
84}
85
56cfbf74
CC
86/**
87 * cpuidle_enter_state - enter the state and update stats
88 * @dev: cpuidle device for this cpu
89 * @drv: cpuidle driver for this cpu
90 * @next_state: index into drv->states of the state to enter
91 */
92int cpuidle_enter_state(struct cpuidle_device *dev, struct cpuidle_driver *drv,
93 int next_state)
94{
95 int entered_state;
96
97 entered_state = cpuidle_enter_ops(dev, drv, next_state);
98
99 if (entered_state >= 0) {
100 /* Update cpuidle counters */
101 /* This can be moved to within driver enter routine
102 * but that results in multiple copies of same code.
103 */
a474a515 104 dev->states_usage[entered_state].time += dev->last_residency;
56cfbf74
CC
105 dev->states_usage[entered_state].usage++;
106 } else {
107 dev->last_residency = 0;
108 }
109
110 return entered_state;
111}
112
4f86d3a8
LB
113/**
114 * cpuidle_idle_call - the main idle loop
115 *
116 * NOTE: no locks or semaphores should be used here
a0bfa137 117 * return non-zero on failure
4f86d3a8 118 */
a0bfa137 119int cpuidle_idle_call(void)
4f86d3a8 120{
4a6f4fe8 121 struct cpuidle_device *dev = __this_cpu_read(cpuidle_devices);
bf4d1b5d 122 struct cpuidle_driver *drv;
e978aa7d 123 int next_state, entered_state;
4f86d3a8 124
a0bfa137
LB
125 if (off)
126 return -ENODEV;
127
128 if (!initialized)
129 return -ENODEV;
130
4f86d3a8 131 /* check if the device is ready */
a0bfa137
LB
132 if (!dev || !dev->enabled)
133 return -EBUSY;
4f86d3a8 134
bf4d1b5d
DL
135 drv = cpuidle_get_cpu_driver(dev);
136
4f86d3a8 137 /* ask the governor for the next state */
46bcfad7 138 next_state = cpuidle_curr_governor->select(drv, dev);
246eb7f0 139 if (need_resched()) {
d73d68dc
YS
140 dev->last_residency = 0;
141 /* give the governor an opportunity to reflect on the outcome */
142 if (cpuidle_curr_governor->reflect)
143 cpuidle_curr_governor->reflect(dev, next_state);
246eb7f0 144 local_irq_enable();
a0bfa137 145 return 0;
246eb7f0
KH
146 }
147
76027ea8 148 trace_cpu_idle_rcuidle(next_state, dev->cpu);
f77cfe4e 149
b60e6a0e
DL
150 if (drv->states[next_state].flags & CPUIDLE_FLAG_TIMER_STOP)
151 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER,
152 &dev->cpu);
153
4126c019
CC
154 if (cpuidle_state_is_coupled(dev, drv, next_state))
155 entered_state = cpuidle_enter_state_coupled(dev, drv,
156 next_state);
157 else
158 entered_state = cpuidle_enter_state(dev, drv, next_state);
f77cfe4e 159
b60e6a0e
DL
160 if (drv->states[next_state].flags & CPUIDLE_FLAG_TIMER_STOP)
161 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT,
162 &dev->cpu);
163
76027ea8 164 trace_cpu_idle_rcuidle(PWR_EVENT_EXIT, dev->cpu);
f77cfe4e 165
4f86d3a8
LB
166 /* give the governor an opportunity to reflect on the outcome */
167 if (cpuidle_curr_governor->reflect)
e978aa7d 168 cpuidle_curr_governor->reflect(dev, entered_state);
a0bfa137
LB
169
170 return 0;
4f86d3a8
LB
171}
172
173/**
174 * cpuidle_install_idle_handler - installs the cpuidle idle loop handler
175 */
176void cpuidle_install_idle_handler(void)
177{
a0bfa137 178 if (enabled_devices) {
4f86d3a8
LB
179 /* Make sure all changes finished before we switch to new idle */
180 smp_wmb();
a0bfa137 181 initialized = 1;
4f86d3a8
LB
182 }
183}
184
185/**
186 * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler
187 */
188void cpuidle_uninstall_idle_handler(void)
189{
a0bfa137
LB
190 if (enabled_devices) {
191 initialized = 0;
4a162513 192 kick_all_cpus_sync();
4f86d3a8
LB
193 }
194}
195
196/**
197 * cpuidle_pause_and_lock - temporarily disables CPUIDLE
198 */
199void cpuidle_pause_and_lock(void)
200{
201 mutex_lock(&cpuidle_lock);
202 cpuidle_uninstall_idle_handler();
203}
204
205EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock);
206
207/**
208 * cpuidle_resume_and_unlock - resumes CPUIDLE operation
209 */
210void cpuidle_resume_and_unlock(void)
211{
212 cpuidle_install_idle_handler();
213 mutex_unlock(&cpuidle_lock);
214}
215
216EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock);
217
8651f97b
PM
218/* Currently used in suspend/resume path to suspend cpuidle */
219void cpuidle_pause(void)
220{
221 mutex_lock(&cpuidle_lock);
222 cpuidle_uninstall_idle_handler();
223 mutex_unlock(&cpuidle_lock);
224}
225
226/* Currently used in suspend/resume path to resume cpuidle */
227void cpuidle_resume(void)
228{
229 mutex_lock(&cpuidle_lock);
230 cpuidle_install_idle_handler();
231 mutex_unlock(&cpuidle_lock);
232}
233
e1689795
RL
234/**
235 * cpuidle_wrap_enter - performs timekeeping and irqen around enter function
236 * @dev: pointer to a valid cpuidle_device object
237 * @drv: pointer to a valid cpuidle_driver object
238 * @index: index of the target cpuidle state.
239 */
240int cpuidle_wrap_enter(struct cpuidle_device *dev,
241 struct cpuidle_driver *drv, int index,
242 int (*enter)(struct cpuidle_device *dev,
243 struct cpuidle_driver *drv, int index))
244{
245 ktime_t time_start, time_end;
246 s64 diff;
247
248 time_start = ktime_get();
249
250 index = enter(dev, drv, index);
251
252 time_end = ktime_get();
253
254 local_irq_enable();
255
256 diff = ktime_to_us(ktime_sub(time_end, time_start));
257 if (diff > INT_MAX)
258 diff = INT_MAX;
259
260 dev->last_residency = (int) diff;
261
262 return index;
263}
264
d8c216cf 265#ifdef CONFIG_ARCH_HAS_CPU_RELAX
46bcfad7
DD
266static int poll_idle(struct cpuidle_device *dev,
267 struct cpuidle_driver *drv, int index)
d8c216cf
RW
268{
269 ktime_t t1, t2;
270 s64 diff;
d8c216cf
RW
271
272 t1 = ktime_get();
273 local_irq_enable();
274 while (!need_resched())
275 cpu_relax();
276
277 t2 = ktime_get();
278 diff = ktime_to_us(ktime_sub(t2, t1));
279 if (diff > INT_MAX)
280 diff = INT_MAX;
281
e978aa7d
DD
282 dev->last_residency = (int) diff;
283
284 return index;
d8c216cf
RW
285}
286
46bcfad7 287static void poll_idle_init(struct cpuidle_driver *drv)
d8c216cf 288{
46bcfad7 289 struct cpuidle_state *state = &drv->states[0];
d8c216cf 290
720f1c30 291 snprintf(state->name, CPUIDLE_NAME_LEN, "POLL");
d8c216cf
RW
292 snprintf(state->desc, CPUIDLE_DESC_LEN, "CPUIDLE CORE POLL IDLE");
293 state->exit_latency = 0;
294 state->target_residency = 0;
295 state->power_usage = -1;
d247632c 296 state->flags = 0;
d8c216cf 297 state->enter = poll_idle;
cbc9ef02 298 state->disabled = false;
d8c216cf
RW
299}
300#else
46bcfad7 301static void poll_idle_init(struct cpuidle_driver *drv) {}
d8c216cf
RW
302#endif /* CONFIG_ARCH_HAS_CPU_RELAX */
303
4f86d3a8
LB
304/**
305 * cpuidle_enable_device - enables idle PM for a CPU
306 * @dev: the CPU
307 *
308 * This function must be called between cpuidle_pause_and_lock and
309 * cpuidle_resume_and_unlock when used externally.
310 */
311int cpuidle_enable_device(struct cpuidle_device *dev)
312{
313 int ret, i;
bf4d1b5d 314 struct cpuidle_driver *drv;
4f86d3a8 315
1b0a0e9a
SB
316 if (!dev)
317 return -EINVAL;
318
4f86d3a8
LB
319 if (dev->enabled)
320 return 0;
bf4d1b5d
DL
321
322 drv = cpuidle_get_cpu_driver(dev);
323
e1689795 324 if (!drv || !cpuidle_curr_governor)
4f86d3a8 325 return -EIO;
bf4d1b5d 326
4f86d3a8 327 if (!dev->state_count)
fc850f39 328 dev->state_count = drv->state_count;
4f86d3a8 329
dcb84f33
VP
330 if (dev->registered == 0) {
331 ret = __cpuidle_register_device(dev);
332 if (ret)
333 return ret;
334 }
335
e1689795
RL
336 cpuidle_enter_ops = drv->en_core_tk_irqen ?
337 cpuidle_enter_tk : cpuidle_enter;
338
339 poll_idle_init(drv);
d8c216cf 340
bf4d1b5d
DL
341 ret = cpuidle_add_device_sysfs(dev);
342 if (ret)
4f86d3a8
LB
343 return ret;
344
345 if (cpuidle_curr_governor->enable &&
e1689795 346 (ret = cpuidle_curr_governor->enable(drv, dev)))
4f86d3a8
LB
347 goto fail_sysfs;
348
349 for (i = 0; i < dev->state_count; i++) {
4202735e
DD
350 dev->states_usage[i].usage = 0;
351 dev->states_usage[i].time = 0;
4f86d3a8
LB
352 }
353 dev->last_residency = 0;
4f86d3a8
LB
354
355 smp_wmb();
356
357 dev->enabled = 1;
358
359 enabled_devices++;
360 return 0;
361
362fail_sysfs:
bf4d1b5d 363 cpuidle_remove_device_sysfs(dev);
4f86d3a8
LB
364
365 return ret;
366}
367
368EXPORT_SYMBOL_GPL(cpuidle_enable_device);
369
370/**
371 * cpuidle_disable_device - disables idle PM for a CPU
372 * @dev: the CPU
373 *
374 * This function must be called between cpuidle_pause_and_lock and
375 * cpuidle_resume_and_unlock when used externally.
376 */
377void cpuidle_disable_device(struct cpuidle_device *dev)
378{
bf4d1b5d
DL
379 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
380
cf31cd1a 381 if (!dev || !dev->enabled)
4f86d3a8 382 return;
bf4d1b5d
DL
383
384 if (!drv || !cpuidle_curr_governor)
4f86d3a8
LB
385 return;
386
387 dev->enabled = 0;
388
389 if (cpuidle_curr_governor->disable)
bf4d1b5d 390 cpuidle_curr_governor->disable(drv, dev);
4f86d3a8 391
bf4d1b5d 392 cpuidle_remove_device_sysfs(dev);
4f86d3a8
LB
393 enabled_devices--;
394}
395
396EXPORT_SYMBOL_GPL(cpuidle_disable_device);
397
398/**
dcb84f33
VP
399 * __cpuidle_register_device - internal register function called before register
400 * and enable routines
4f86d3a8 401 * @dev: the cpu
dcb84f33
VP
402 *
403 * cpuidle_lock mutex must be held before this is called
4f86d3a8 404 */
dcb84f33 405static int __cpuidle_register_device(struct cpuidle_device *dev)
4f86d3a8
LB
406{
407 int ret;
bf4d1b5d 408 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
4f86d3a8 409
bf4d1b5d 410 if (!try_module_get(drv->owner))
4f86d3a8
LB
411 return -EINVAL;
412
4f86d3a8
LB
413 per_cpu(cpuidle_devices, dev->cpu) = dev;
414 list_add(&dev->device_list, &cpuidle_detected_devices);
1aef40e2 415 ret = cpuidle_add_sysfs(dev);
3af272ab
CC
416 if (ret)
417 goto err_sysfs;
4f86d3a8 418
4126c019
CC
419 ret = cpuidle_coupled_register_device(dev);
420 if (ret)
421 goto err_coupled;
4f86d3a8 422
dcb84f33
VP
423 dev->registered = 1;
424 return 0;
3af272ab 425
4126c019 426err_coupled:
1aef40e2 427 cpuidle_remove_sysfs(dev);
3af272ab
CC
428err_sysfs:
429 list_del(&dev->device_list);
430 per_cpu(cpuidle_devices, dev->cpu) = NULL;
bf4d1b5d 431 module_put(drv->owner);
3af272ab 432 return ret;
dcb84f33
VP
433}
434
435/**
436 * cpuidle_register_device - registers a CPU's idle PM feature
437 * @dev: the cpu
438 */
439int cpuidle_register_device(struct cpuidle_device *dev)
440{
441 int ret;
442
1b0a0e9a
SB
443 if (!dev)
444 return -EINVAL;
445
dcb84f33
VP
446 mutex_lock(&cpuidle_lock);
447
448 if ((ret = __cpuidle_register_device(dev))) {
449 mutex_unlock(&cpuidle_lock);
450 return ret;
451 }
452
4f86d3a8
LB
453 cpuidle_enable_device(dev);
454 cpuidle_install_idle_handler();
455
456 mutex_unlock(&cpuidle_lock);
457
458 return 0;
459
460}
461
462EXPORT_SYMBOL_GPL(cpuidle_register_device);
463
464/**
465 * cpuidle_unregister_device - unregisters a CPU's idle PM feature
466 * @dev: the cpu
467 */
468void cpuidle_unregister_device(struct cpuidle_device *dev)
469{
bf4d1b5d 470 struct cpuidle_driver *drv = cpuidle_get_cpu_driver(dev);
4f86d3a8 471
dcb84f33
VP
472 if (dev->registered == 0)
473 return;
474
4f86d3a8
LB
475 cpuidle_pause_and_lock();
476
477 cpuidle_disable_device(dev);
478
1aef40e2 479 cpuidle_remove_sysfs(dev);
4f86d3a8 480 list_del(&dev->device_list);
4f86d3a8
LB
481 per_cpu(cpuidle_devices, dev->cpu) = NULL;
482
4126c019
CC
483 cpuidle_coupled_unregister_device(dev);
484
4f86d3a8
LB
485 cpuidle_resume_and_unlock();
486
bf4d1b5d 487 module_put(drv->owner);
4f86d3a8
LB
488}
489
490EXPORT_SYMBOL_GPL(cpuidle_unregister_device);
491
492#ifdef CONFIG_SMP
493
494static void smp_callback(void *v)
495{
496 /* we already woke the CPU up, nothing more to do */
497}
498
499/*
500 * This function gets called when a part of the kernel has a new latency
501 * requirement. This means we need to get all processors out of their C-state,
502 * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that
503 * wakes them all right up.
504 */
505static int cpuidle_latency_notify(struct notifier_block *b,
506 unsigned long l, void *v)
507{
8691e5a8 508 smp_call_function(smp_callback, NULL, 1);
4f86d3a8
LB
509 return NOTIFY_OK;
510}
511
512static struct notifier_block cpuidle_latency_notifier = {
513 .notifier_call = cpuidle_latency_notify,
514};
515
d82b3518
MG
516static inline void latency_notifier_init(struct notifier_block *n)
517{
518 pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY, n);
519}
4f86d3a8
LB
520
521#else /* CONFIG_SMP */
522
523#define latency_notifier_init(x) do { } while (0)
524
525#endif /* CONFIG_SMP */
526
527/**
528 * cpuidle_init - core initializer
529 */
530static int __init cpuidle_init(void)
531{
532 int ret;
533
62027aea
LB
534 if (cpuidle_disabled())
535 return -ENODEV;
536
8a25a2fd 537 ret = cpuidle_add_interface(cpu_subsys.dev_root);
4f86d3a8
LB
538 if (ret)
539 return ret;
540
541 latency_notifier_init(&cpuidle_latency_notifier);
542
543 return 0;
544}
545
62027aea 546module_param(off, int, 0444);
4f86d3a8 547core_initcall(cpuidle_init);
This page took 0.477878 seconds and 5 git commands to generate.