2 * cpuidle.c - core cpuidle infrastructure
4 * (C) 2006-2007 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>
5 * Shaohua Li <shaohua.li@intel.com>
6 * Adam Belay <abelay@novell.com>
8 * This code is licenced under the GPL.
11 #include <linux/clockchips.h>
12 #include <linux/kernel.h>
13 #include <linux/mutex.h>
14 #include <linux/sched.h>
15 #include <linux/notifier.h>
16 #include <linux/pm_qos.h>
17 #include <linux/cpu.h>
18 #include <linux/cpuidle.h>
19 #include <linux/ktime.h>
20 #include <linux/hrtimer.h>
21 #include <linux/module.h>
22 #include <trace/events/power.h>
26 DEFINE_PER_CPU(struct cpuidle_device
*, cpuidle_devices
);
28 DEFINE_MUTEX(cpuidle_lock
);
29 LIST_HEAD(cpuidle_detected_devices
);
31 static int enabled_devices
;
32 static int off __read_mostly
;
33 static int initialized __read_mostly
;
35 int cpuidle_disabled(void)
39 void disable_cpuidle(void)
44 static int __cpuidle_register_device(struct cpuidle_device
*dev
);
46 static inline int cpuidle_enter(struct cpuidle_device
*dev
,
47 struct cpuidle_driver
*drv
, int index
)
49 struct cpuidle_state
*target_state
= &drv
->states
[index
];
50 return target_state
->enter(dev
, drv
, index
);
53 static inline int cpuidle_enter_tk(struct cpuidle_device
*dev
,
54 struct cpuidle_driver
*drv
, int index
)
56 return cpuidle_wrap_enter(dev
, drv
, index
, cpuidle_enter
);
59 typedef int (*cpuidle_enter_t
)(struct cpuidle_device
*dev
,
60 struct cpuidle_driver
*drv
, int index
);
62 static cpuidle_enter_t cpuidle_enter_ops
;
65 * cpuidle_play_dead - cpu off-lining
67 * Returns in case of an error or no driver
69 int cpuidle_play_dead(void)
71 struct cpuidle_device
*dev
= __this_cpu_read(cpuidle_devices
);
72 struct cpuidle_driver
*drv
= cpuidle_get_cpu_driver(dev
);
78 /* Find lowest-power state that supports long-term idle */
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
);
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
92 int cpuidle_enter_state(struct cpuidle_device
*dev
, struct cpuidle_driver
*drv
,
97 entered_state
= cpuidle_enter_ops(dev
, drv
, next_state
);
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.
104 dev
->states_usage
[entered_state
].time
+= dev
->last_residency
;
105 dev
->states_usage
[entered_state
].usage
++;
107 dev
->last_residency
= 0;
110 return entered_state
;
114 * cpuidle_idle_call - the main idle loop
116 * NOTE: no locks or semaphores should be used here
117 * return non-zero on failure
119 int cpuidle_idle_call(void)
121 struct cpuidle_device
*dev
= __this_cpu_read(cpuidle_devices
);
122 struct cpuidle_driver
*drv
;
123 int next_state
, entered_state
;
131 /* check if the device is ready */
132 if (!dev
|| !dev
->enabled
)
135 drv
= cpuidle_get_cpu_driver(dev
);
137 /* ask the governor for the next state */
138 next_state
= cpuidle_curr_governor
->select(drv
, dev
);
139 if (need_resched()) {
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
);
148 trace_cpu_idle_rcuidle(next_state
, dev
->cpu
);
150 if (drv
->states
[next_state
].flags
& CPUIDLE_FLAG_TIMER_STOP
)
151 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER
,
154 if (cpuidle_state_is_coupled(dev
, drv
, next_state
))
155 entered_state
= cpuidle_enter_state_coupled(dev
, drv
,
158 entered_state
= cpuidle_enter_state(dev
, drv
, next_state
);
160 if (drv
->states
[next_state
].flags
& CPUIDLE_FLAG_TIMER_STOP
)
161 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT
,
164 trace_cpu_idle_rcuidle(PWR_EVENT_EXIT
, dev
->cpu
);
166 /* give the governor an opportunity to reflect on the outcome */
167 if (cpuidle_curr_governor
->reflect
)
168 cpuidle_curr_governor
->reflect(dev
, entered_state
);
174 * cpuidle_install_idle_handler - installs the cpuidle idle loop handler
176 void cpuidle_install_idle_handler(void)
178 if (enabled_devices
) {
179 /* Make sure all changes finished before we switch to new idle */
186 * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler
188 void cpuidle_uninstall_idle_handler(void)
190 if (enabled_devices
) {
192 kick_all_cpus_sync();
197 * cpuidle_pause_and_lock - temporarily disables CPUIDLE
199 void cpuidle_pause_and_lock(void)
201 mutex_lock(&cpuidle_lock
);
202 cpuidle_uninstall_idle_handler();
205 EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock
);
208 * cpuidle_resume_and_unlock - resumes CPUIDLE operation
210 void cpuidle_resume_and_unlock(void)
212 cpuidle_install_idle_handler();
213 mutex_unlock(&cpuidle_lock
);
216 EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock
);
218 /* Currently used in suspend/resume path to suspend cpuidle */
219 void cpuidle_pause(void)
221 mutex_lock(&cpuidle_lock
);
222 cpuidle_uninstall_idle_handler();
223 mutex_unlock(&cpuidle_lock
);
226 /* Currently used in suspend/resume path to resume cpuidle */
227 void cpuidle_resume(void)
229 mutex_lock(&cpuidle_lock
);
230 cpuidle_install_idle_handler();
231 mutex_unlock(&cpuidle_lock
);
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.
240 int 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
))
245 ktime_t time_start
, time_end
;
248 time_start
= ktime_get();
250 index
= enter(dev
, drv
, index
);
252 time_end
= ktime_get();
256 diff
= ktime_to_us(ktime_sub(time_end
, time_start
));
260 dev
->last_residency
= (int) diff
;
265 #ifdef CONFIG_ARCH_HAS_CPU_RELAX
266 static int poll_idle(struct cpuidle_device
*dev
,
267 struct cpuidle_driver
*drv
, int index
)
274 while (!need_resched())
278 diff
= ktime_to_us(ktime_sub(t2
, t1
));
282 dev
->last_residency
= (int) diff
;
287 static void poll_idle_init(struct cpuidle_driver
*drv
)
289 struct cpuidle_state
*state
= &drv
->states
[0];
291 snprintf(state
->name
, CPUIDLE_NAME_LEN
, "POLL");
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;
297 state
->enter
= poll_idle
;
298 state
->disabled
= false;
301 static void poll_idle_init(struct cpuidle_driver
*drv
) {}
302 #endif /* CONFIG_ARCH_HAS_CPU_RELAX */
305 * cpuidle_enable_device - enables idle PM for a CPU
308 * This function must be called between cpuidle_pause_and_lock and
309 * cpuidle_resume_and_unlock when used externally.
311 int cpuidle_enable_device(struct cpuidle_device
*dev
)
314 struct cpuidle_driver
*drv
;
322 drv
= cpuidle_get_cpu_driver(dev
);
324 if (!drv
|| !cpuidle_curr_governor
)
327 if (!dev
->state_count
)
328 dev
->state_count
= drv
->state_count
;
330 if (dev
->registered
== 0) {
331 ret
= __cpuidle_register_device(dev
);
336 cpuidle_enter_ops
= drv
->en_core_tk_irqen
?
337 cpuidle_enter_tk
: cpuidle_enter
;
341 ret
= cpuidle_add_device_sysfs(dev
);
345 if (cpuidle_curr_governor
->enable
&&
346 (ret
= cpuidle_curr_governor
->enable(drv
, dev
)))
349 for (i
= 0; i
< dev
->state_count
; i
++) {
350 dev
->states_usage
[i
].usage
= 0;
351 dev
->states_usage
[i
].time
= 0;
353 dev
->last_residency
= 0;
363 cpuidle_remove_device_sysfs(dev
);
368 EXPORT_SYMBOL_GPL(cpuidle_enable_device
);
371 * cpuidle_disable_device - disables idle PM for a CPU
374 * This function must be called between cpuidle_pause_and_lock and
375 * cpuidle_resume_and_unlock when used externally.
377 void cpuidle_disable_device(struct cpuidle_device
*dev
)
379 struct cpuidle_driver
*drv
= cpuidle_get_cpu_driver(dev
);
381 if (!dev
|| !dev
->enabled
)
384 if (!drv
|| !cpuidle_curr_governor
)
389 if (cpuidle_curr_governor
->disable
)
390 cpuidle_curr_governor
->disable(drv
, dev
);
392 cpuidle_remove_device_sysfs(dev
);
396 EXPORT_SYMBOL_GPL(cpuidle_disable_device
);
399 * __cpuidle_register_device - internal register function called before register
400 * and enable routines
403 * cpuidle_lock mutex must be held before this is called
405 static int __cpuidle_register_device(struct cpuidle_device
*dev
)
408 struct cpuidle_driver
*drv
= cpuidle_get_cpu_driver(dev
);
410 if (!try_module_get(drv
->owner
))
413 per_cpu(cpuidle_devices
, dev
->cpu
) = dev
;
414 list_add(&dev
->device_list
, &cpuidle_detected_devices
);
415 ret
= cpuidle_add_sysfs(dev
);
419 ret
= cpuidle_coupled_register_device(dev
);
427 cpuidle_remove_sysfs(dev
);
429 list_del(&dev
->device_list
);
430 per_cpu(cpuidle_devices
, dev
->cpu
) = NULL
;
431 module_put(drv
->owner
);
436 * cpuidle_register_device - registers a CPU's idle PM feature
439 int cpuidle_register_device(struct cpuidle_device
*dev
)
446 mutex_lock(&cpuidle_lock
);
448 if ((ret
= __cpuidle_register_device(dev
))) {
449 mutex_unlock(&cpuidle_lock
);
453 cpuidle_enable_device(dev
);
454 cpuidle_install_idle_handler();
456 mutex_unlock(&cpuidle_lock
);
462 EXPORT_SYMBOL_GPL(cpuidle_register_device
);
465 * cpuidle_unregister_device - unregisters a CPU's idle PM feature
468 void cpuidle_unregister_device(struct cpuidle_device
*dev
)
470 struct cpuidle_driver
*drv
= cpuidle_get_cpu_driver(dev
);
472 if (dev
->registered
== 0)
475 cpuidle_pause_and_lock();
477 cpuidle_disable_device(dev
);
479 cpuidle_remove_sysfs(dev
);
480 list_del(&dev
->device_list
);
481 per_cpu(cpuidle_devices
, dev
->cpu
) = NULL
;
483 cpuidle_coupled_unregister_device(dev
);
485 cpuidle_resume_and_unlock();
487 module_put(drv
->owner
);
490 EXPORT_SYMBOL_GPL(cpuidle_unregister_device
);
494 static void smp_callback(void *v
)
496 /* we already woke the CPU up, nothing more to do */
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.
505 static int cpuidle_latency_notify(struct notifier_block
*b
,
506 unsigned long l
, void *v
)
508 smp_call_function(smp_callback
, NULL
, 1);
512 static struct notifier_block cpuidle_latency_notifier
= {
513 .notifier_call
= cpuidle_latency_notify
,
516 static inline void latency_notifier_init(struct notifier_block
*n
)
518 pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY
, n
);
521 #else /* CONFIG_SMP */
523 #define latency_notifier_init(x) do { } while (0)
525 #endif /* CONFIG_SMP */
528 * cpuidle_init - core initializer
530 static int __init
cpuidle_init(void)
534 if (cpuidle_disabled())
537 ret
= cpuidle_add_interface(cpu_subsys
.dev_root
);
541 latency_notifier_init(&cpuidle_latency_notifier
);
546 module_param(off
, int, 0444);
547 core_initcall(cpuidle_init
);