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
);
27 DEFINE_PER_CPU(struct cpuidle_device
, cpuidle_dev
);
29 DEFINE_MUTEX(cpuidle_lock
);
30 LIST_HEAD(cpuidle_detected_devices
);
32 static int enabled_devices
;
33 static int off __read_mostly
;
34 static int initialized __read_mostly
;
36 int cpuidle_disabled(void)
40 void disable_cpuidle(void)
46 * cpuidle_play_dead - cpu off-lining
48 * Returns in case of an error or no driver
50 int cpuidle_play_dead(void)
52 struct cpuidle_device
*dev
= __this_cpu_read(cpuidle_devices
);
53 struct cpuidle_driver
*drv
= cpuidle_get_cpu_driver(dev
);
59 /* Find lowest-power state that supports long-term idle */
60 for (i
= drv
->state_count
- 1; i
>= CPUIDLE_DRIVER_STATE_START
; i
--)
61 if (drv
->states
[i
].enter_dead
)
62 return drv
->states
[i
].enter_dead(dev
, i
);
68 * cpuidle_enabled - check if the cpuidle framework is ready
69 * @dev: cpuidle device for this cpu
70 * @drv: cpuidle driver for this cpu
72 * Return 0 on success, otherwise:
73 * -NODEV : the cpuidle framework is not available
74 * -EBUSY : the cpuidle framework is not initialized
76 int cpuidle_enabled(struct cpuidle_driver
*drv
, struct cpuidle_device
*dev
)
78 if (off
|| !initialized
)
81 if (!drv
|| !dev
|| !dev
->enabled
)
88 * cpuidle_enter_state - enter the state and update stats
89 * @dev: cpuidle device for this cpu
90 * @drv: cpuidle driver for this cpu
91 * @next_state: index into drv->states of the state to enter
93 int cpuidle_enter_state(struct cpuidle_device
*dev
, struct cpuidle_driver
*drv
,
98 struct cpuidle_state
*target_state
= &drv
->states
[index
];
99 ktime_t time_start
, time_end
;
102 time_start
= ktime_get();
104 entered_state
= target_state
->enter(dev
, drv
, index
);
106 time_end
= ktime_get();
108 if (!cpuidle_state_is_coupled(dev
, drv
, entered_state
))
111 diff
= ktime_to_us(ktime_sub(time_end
, time_start
));
115 dev
->last_residency
= (int) diff
;
117 if (entered_state
>= 0) {
118 /* Update cpuidle counters */
119 /* This can be moved to within driver enter routine
120 * but that results in multiple copies of same code.
122 dev
->states_usage
[entered_state
].time
+= dev
->last_residency
;
123 dev
->states_usage
[entered_state
].usage
++;
125 dev
->last_residency
= 0;
128 return entered_state
;
132 * cpuidle_select - ask the cpuidle framework to choose an idle state
134 * @drv: the cpuidle driver
135 * @dev: the cpuidle device
137 * Returns the index of the idle state.
139 int cpuidle_select(struct cpuidle_driver
*drv
, struct cpuidle_device
*dev
)
141 return cpuidle_curr_governor
->select(drv
, dev
);
145 * cpuidle_enter - enter into the specified idle state
147 * @drv: the cpuidle driver tied with the cpu
148 * @dev: the cpuidle device
149 * @index: the index in the idle state table
151 * Returns the index in the idle state, < 0 in case of error.
152 * The error code depends on the backend driver
154 int cpuidle_enter(struct cpuidle_driver
*drv
, struct cpuidle_device
*dev
,
157 if (cpuidle_state_is_coupled(dev
, drv
, index
))
158 return cpuidle_enter_state_coupled(dev
, drv
, index
);
159 return cpuidle_enter_state(dev
, drv
, index
);
163 * cpuidle_reflect - tell the underlying governor what was the state
166 * @dev : the cpuidle device
167 * @index: the index in the idle state table
170 void cpuidle_reflect(struct cpuidle_device
*dev
, int index
)
172 if (cpuidle_curr_governor
->reflect
)
173 cpuidle_curr_governor
->reflect(dev
, index
);
177 * cpuidle_install_idle_handler - installs the cpuidle idle loop handler
179 void cpuidle_install_idle_handler(void)
181 if (enabled_devices
) {
182 /* Make sure all changes finished before we switch to new idle */
189 * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler
191 void cpuidle_uninstall_idle_handler(void)
193 if (enabled_devices
) {
195 kick_all_cpus_sync();
200 * cpuidle_pause_and_lock - temporarily disables CPUIDLE
202 void cpuidle_pause_and_lock(void)
204 mutex_lock(&cpuidle_lock
);
205 cpuidle_uninstall_idle_handler();
208 EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock
);
211 * cpuidle_resume_and_unlock - resumes CPUIDLE operation
213 void cpuidle_resume_and_unlock(void)
215 cpuidle_install_idle_handler();
216 mutex_unlock(&cpuidle_lock
);
219 EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock
);
221 /* Currently used in suspend/resume path to suspend cpuidle */
222 void cpuidle_pause(void)
224 mutex_lock(&cpuidle_lock
);
225 cpuidle_uninstall_idle_handler();
226 mutex_unlock(&cpuidle_lock
);
229 /* Currently used in suspend/resume path to resume cpuidle */
230 void cpuidle_resume(void)
232 mutex_lock(&cpuidle_lock
);
233 cpuidle_install_idle_handler();
234 mutex_unlock(&cpuidle_lock
);
238 * cpuidle_enable_device - enables idle PM for a CPU
241 * This function must be called between cpuidle_pause_and_lock and
242 * cpuidle_resume_and_unlock when used externally.
244 int cpuidle_enable_device(struct cpuidle_device
*dev
)
247 struct cpuidle_driver
*drv
;
255 drv
= cpuidle_get_cpu_driver(dev
);
257 if (!drv
|| !cpuidle_curr_governor
)
260 if (!dev
->registered
)
263 if (!dev
->state_count
)
264 dev
->state_count
= drv
->state_count
;
266 ret
= cpuidle_add_device_sysfs(dev
);
270 if (cpuidle_curr_governor
->enable
&&
271 (ret
= cpuidle_curr_governor
->enable(drv
, dev
)))
282 cpuidle_remove_device_sysfs(dev
);
287 EXPORT_SYMBOL_GPL(cpuidle_enable_device
);
290 * cpuidle_disable_device - disables idle PM for a CPU
293 * This function must be called between cpuidle_pause_and_lock and
294 * cpuidle_resume_and_unlock when used externally.
296 void cpuidle_disable_device(struct cpuidle_device
*dev
)
298 struct cpuidle_driver
*drv
= cpuidle_get_cpu_driver(dev
);
300 if (!dev
|| !dev
->enabled
)
303 if (!drv
|| !cpuidle_curr_governor
)
308 if (cpuidle_curr_governor
->disable
)
309 cpuidle_curr_governor
->disable(drv
, dev
);
311 cpuidle_remove_device_sysfs(dev
);
315 EXPORT_SYMBOL_GPL(cpuidle_disable_device
);
317 static void __cpuidle_unregister_device(struct cpuidle_device
*dev
)
319 struct cpuidle_driver
*drv
= cpuidle_get_cpu_driver(dev
);
321 list_del(&dev
->device_list
);
322 per_cpu(cpuidle_devices
, dev
->cpu
) = NULL
;
323 module_put(drv
->owner
);
326 static void __cpuidle_device_init(struct cpuidle_device
*dev
)
328 memset(dev
->states_usage
, 0, sizeof(dev
->states_usage
));
329 dev
->last_residency
= 0;
333 * __cpuidle_register_device - internal register function called before register
334 * and enable routines
337 * cpuidle_lock mutex must be held before this is called
339 static int __cpuidle_register_device(struct cpuidle_device
*dev
)
342 struct cpuidle_driver
*drv
= cpuidle_get_cpu_driver(dev
);
344 if (!try_module_get(drv
->owner
))
347 per_cpu(cpuidle_devices
, dev
->cpu
) = dev
;
348 list_add(&dev
->device_list
, &cpuidle_detected_devices
);
350 ret
= cpuidle_coupled_register_device(dev
);
352 __cpuidle_unregister_device(dev
);
360 * cpuidle_register_device - registers a CPU's idle PM feature
363 int cpuidle_register_device(struct cpuidle_device
*dev
)
370 mutex_lock(&cpuidle_lock
);
375 __cpuidle_device_init(dev
);
377 ret
= __cpuidle_register_device(dev
);
381 ret
= cpuidle_add_sysfs(dev
);
385 ret
= cpuidle_enable_device(dev
);
389 cpuidle_install_idle_handler();
392 mutex_unlock(&cpuidle_lock
);
397 cpuidle_remove_sysfs(dev
);
399 __cpuidle_unregister_device(dev
);
403 EXPORT_SYMBOL_GPL(cpuidle_register_device
);
406 * cpuidle_unregister_device - unregisters a CPU's idle PM feature
409 void cpuidle_unregister_device(struct cpuidle_device
*dev
)
411 if (!dev
|| dev
->registered
== 0)
414 cpuidle_pause_and_lock();
416 cpuidle_disable_device(dev
);
418 cpuidle_remove_sysfs(dev
);
420 __cpuidle_unregister_device(dev
);
422 cpuidle_coupled_unregister_device(dev
);
424 cpuidle_resume_and_unlock();
427 EXPORT_SYMBOL_GPL(cpuidle_unregister_device
);
430 * cpuidle_unregister: unregister a driver and the devices. This function
431 * can be used only if the driver has been previously registered through
432 * the cpuidle_register function.
434 * @drv: a valid pointer to a struct cpuidle_driver
436 void cpuidle_unregister(struct cpuidle_driver
*drv
)
439 struct cpuidle_device
*device
;
441 for_each_cpu(cpu
, drv
->cpumask
) {
442 device
= &per_cpu(cpuidle_dev
, cpu
);
443 cpuidle_unregister_device(device
);
446 cpuidle_unregister_driver(drv
);
448 EXPORT_SYMBOL_GPL(cpuidle_unregister
);
451 * cpuidle_register: registers the driver and the cpu devices with the
452 * coupled_cpus passed as parameter. This function is used for all common
453 * initialization pattern there are in the arch specific drivers. The
454 * devices is globally defined in this file.
456 * @drv : a valid pointer to a struct cpuidle_driver
457 * @coupled_cpus: a cpumask for the coupled states
459 * Returns 0 on success, < 0 otherwise
461 int cpuidle_register(struct cpuidle_driver
*drv
,
462 const struct cpumask
*const coupled_cpus
)
465 struct cpuidle_device
*device
;
467 ret
= cpuidle_register_driver(drv
);
469 pr_err("failed to register cpuidle driver\n");
473 for_each_cpu(cpu
, drv
->cpumask
) {
474 device
= &per_cpu(cpuidle_dev
, cpu
);
477 #ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED
479 * On multiplatform for ARM, the coupled idle states could be
480 * enabled in the kernel even if the cpuidle driver does not
481 * use it. Note, coupled_cpus is a struct copy.
484 device
->coupled_cpus
= *coupled_cpus
;
486 ret
= cpuidle_register_device(device
);
490 pr_err("Failed to register cpuidle device for cpu%d\n", cpu
);
492 cpuidle_unregister(drv
);
498 EXPORT_SYMBOL_GPL(cpuidle_register
);
502 static void smp_callback(void *v
)
504 /* we already woke the CPU up, nothing more to do */
508 * This function gets called when a part of the kernel has a new latency
509 * requirement. This means we need to get all processors out of their C-state,
510 * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that
511 * wakes them all right up.
513 static int cpuidle_latency_notify(struct notifier_block
*b
,
514 unsigned long l
, void *v
)
516 smp_call_function(smp_callback
, NULL
, 1);
520 static struct notifier_block cpuidle_latency_notifier
= {
521 .notifier_call
= cpuidle_latency_notify
,
524 static inline void latency_notifier_init(struct notifier_block
*n
)
526 pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY
, n
);
529 #else /* CONFIG_SMP */
531 #define latency_notifier_init(x) do { } while (0)
533 #endif /* CONFIG_SMP */
536 * cpuidle_init - core initializer
538 static int __init
cpuidle_init(void)
542 if (cpuidle_disabled())
545 ret
= cpuidle_add_interface(cpu_subsys
.dev_root
);
549 latency_notifier_init(&cpuidle_latency_notifier
);
554 module_param(off
, int, 0444);
555 core_initcall(cpuidle_init
);