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
;
35 static bool use_deepest_state __read_mostly
;
37 int cpuidle_disabled(void)
41 void disable_cpuidle(void)
47 * cpuidle_play_dead - cpu off-lining
49 * Returns in case of an error or no driver
51 int cpuidle_play_dead(void)
53 struct cpuidle_device
*dev
= __this_cpu_read(cpuidle_devices
);
54 struct cpuidle_driver
*drv
= cpuidle_get_cpu_driver(dev
);
60 /* Find lowest-power state that supports long-term idle */
61 for (i
= drv
->state_count
- 1; i
>= CPUIDLE_DRIVER_STATE_START
; i
--)
62 if (drv
->states
[i
].enter_dead
)
63 return drv
->states
[i
].enter_dead(dev
, i
);
69 * cpuidle_use_deepest_state - Enable/disable the "deepest idle" mode.
70 * @enable: Whether enable or disable the feature.
72 * If the "deepest idle" mode is enabled, cpuidle will ignore the governor and
73 * always use the state with the greatest exit latency (out of the states that
76 * This function can only be called after cpuidle_pause() to avoid races.
78 void cpuidle_use_deepest_state(bool enable
)
80 use_deepest_state
= enable
;
84 * cpuidle_find_deepest_state - Find the state of the greatest exit latency.
85 * @drv: cpuidle driver for a given CPU.
86 * @dev: cpuidle device for a given CPU.
88 static int cpuidle_find_deepest_state(struct cpuidle_driver
*drv
,
89 struct cpuidle_device
*dev
)
91 unsigned int latency_req
= 0;
92 int i
, ret
= CPUIDLE_DRIVER_STATE_START
- 1;
94 for (i
= CPUIDLE_DRIVER_STATE_START
; i
< drv
->state_count
; i
++) {
95 struct cpuidle_state
*s
= &drv
->states
[i
];
96 struct cpuidle_state_usage
*su
= &dev
->states_usage
[i
];
98 if (s
->disabled
|| su
->disable
|| s
->exit_latency
<= latency_req
)
101 latency_req
= s
->exit_latency
;
108 * cpuidle_enter_state - enter the state and update stats
109 * @dev: cpuidle device for this cpu
110 * @drv: cpuidle driver for this cpu
111 * @next_state: index into drv->states of the state to enter
113 int cpuidle_enter_state(struct cpuidle_device
*dev
, struct cpuidle_driver
*drv
,
118 struct cpuidle_state
*target_state
= &drv
->states
[index
];
119 ktime_t time_start
, time_end
;
122 trace_cpu_idle_rcuidle(index
, dev
->cpu
);
123 time_start
= ktime_get();
125 entered_state
= target_state
->enter(dev
, drv
, index
);
127 time_end
= ktime_get();
128 trace_cpu_idle_rcuidle(PWR_EVENT_EXIT
, dev
->cpu
);
130 if (!cpuidle_state_is_coupled(dev
, drv
, entered_state
))
133 diff
= ktime_to_us(ktime_sub(time_end
, time_start
));
137 dev
->last_residency
= (int) diff
;
139 if (entered_state
>= 0) {
140 /* Update cpuidle counters */
141 /* This can be moved to within driver enter routine
142 * but that results in multiple copies of same code.
144 dev
->states_usage
[entered_state
].time
+= dev
->last_residency
;
145 dev
->states_usage
[entered_state
].usage
++;
147 dev
->last_residency
= 0;
150 return entered_state
;
154 * cpuidle_select - ask the cpuidle framework to choose an idle state
156 * @drv: the cpuidle driver
157 * @dev: the cpuidle device
159 * Returns the index of the idle state.
161 int cpuidle_select(struct cpuidle_driver
*drv
, struct cpuidle_device
*dev
)
163 if (off
|| !initialized
)
166 if (!drv
|| !dev
|| !dev
->enabled
)
169 if (unlikely(use_deepest_state
))
170 return cpuidle_find_deepest_state(drv
, dev
);
172 return cpuidle_curr_governor
->select(drv
, dev
);
176 * cpuidle_enter - enter into the specified idle state
178 * @drv: the cpuidle driver tied with the cpu
179 * @dev: the cpuidle device
180 * @index: the index in the idle state table
182 * Returns the index in the idle state, < 0 in case of error.
183 * The error code depends on the backend driver
185 int cpuidle_enter(struct cpuidle_driver
*drv
, struct cpuidle_device
*dev
,
188 if (cpuidle_state_is_coupled(dev
, drv
, index
))
189 return cpuidle_enter_state_coupled(dev
, drv
, index
);
190 return cpuidle_enter_state(dev
, drv
, index
);
194 * cpuidle_reflect - tell the underlying governor what was the state
197 * @dev : the cpuidle device
198 * @index: the index in the idle state table
201 void cpuidle_reflect(struct cpuidle_device
*dev
, int index
)
203 if (cpuidle_curr_governor
->reflect
&& !unlikely(use_deepest_state
))
204 cpuidle_curr_governor
->reflect(dev
, index
);
208 * cpuidle_install_idle_handler - installs the cpuidle idle loop handler
210 void cpuidle_install_idle_handler(void)
212 if (enabled_devices
) {
213 /* Make sure all changes finished before we switch to new idle */
220 * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler
222 void cpuidle_uninstall_idle_handler(void)
224 if (enabled_devices
) {
226 wake_up_all_idle_cpus();
230 * Make sure external observers (such as the scheduler)
231 * are done looking at pointed idle states.
237 * cpuidle_pause_and_lock - temporarily disables CPUIDLE
239 void cpuidle_pause_and_lock(void)
241 mutex_lock(&cpuidle_lock
);
242 cpuidle_uninstall_idle_handler();
245 EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock
);
248 * cpuidle_resume_and_unlock - resumes CPUIDLE operation
250 void cpuidle_resume_and_unlock(void)
252 cpuidle_install_idle_handler();
253 mutex_unlock(&cpuidle_lock
);
256 EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock
);
258 /* Currently used in suspend/resume path to suspend cpuidle */
259 void cpuidle_pause(void)
261 mutex_lock(&cpuidle_lock
);
262 cpuidle_uninstall_idle_handler();
263 mutex_unlock(&cpuidle_lock
);
266 /* Currently used in suspend/resume path to resume cpuidle */
267 void cpuidle_resume(void)
269 mutex_lock(&cpuidle_lock
);
270 cpuidle_install_idle_handler();
271 mutex_unlock(&cpuidle_lock
);
275 * cpuidle_enable_device - enables idle PM for a CPU
278 * This function must be called between cpuidle_pause_and_lock and
279 * cpuidle_resume_and_unlock when used externally.
281 int cpuidle_enable_device(struct cpuidle_device
*dev
)
284 struct cpuidle_driver
*drv
;
292 drv
= cpuidle_get_cpu_driver(dev
);
294 if (!drv
|| !cpuidle_curr_governor
)
297 if (!dev
->registered
)
300 if (!dev
->state_count
)
301 dev
->state_count
= drv
->state_count
;
303 ret
= cpuidle_add_device_sysfs(dev
);
307 if (cpuidle_curr_governor
->enable
&&
308 (ret
= cpuidle_curr_governor
->enable(drv
, dev
)))
319 cpuidle_remove_device_sysfs(dev
);
324 EXPORT_SYMBOL_GPL(cpuidle_enable_device
);
327 * cpuidle_disable_device - disables idle PM for a CPU
330 * This function must be called between cpuidle_pause_and_lock and
331 * cpuidle_resume_and_unlock when used externally.
333 void cpuidle_disable_device(struct cpuidle_device
*dev
)
335 struct cpuidle_driver
*drv
= cpuidle_get_cpu_driver(dev
);
337 if (!dev
|| !dev
->enabled
)
340 if (!drv
|| !cpuidle_curr_governor
)
345 if (cpuidle_curr_governor
->disable
)
346 cpuidle_curr_governor
->disable(drv
, dev
);
348 cpuidle_remove_device_sysfs(dev
);
352 EXPORT_SYMBOL_GPL(cpuidle_disable_device
);
354 static void __cpuidle_unregister_device(struct cpuidle_device
*dev
)
356 struct cpuidle_driver
*drv
= cpuidle_get_cpu_driver(dev
);
358 list_del(&dev
->device_list
);
359 per_cpu(cpuidle_devices
, dev
->cpu
) = NULL
;
360 module_put(drv
->owner
);
363 static void __cpuidle_device_init(struct cpuidle_device
*dev
)
365 memset(dev
->states_usage
, 0, sizeof(dev
->states_usage
));
366 dev
->last_residency
= 0;
370 * __cpuidle_register_device - internal register function called before register
371 * and enable routines
374 * cpuidle_lock mutex must be held before this is called
376 static int __cpuidle_register_device(struct cpuidle_device
*dev
)
379 struct cpuidle_driver
*drv
= cpuidle_get_cpu_driver(dev
);
381 if (!try_module_get(drv
->owner
))
384 per_cpu(cpuidle_devices
, dev
->cpu
) = dev
;
385 list_add(&dev
->device_list
, &cpuidle_detected_devices
);
387 ret
= cpuidle_coupled_register_device(dev
);
389 __cpuidle_unregister_device(dev
);
397 * cpuidle_register_device - registers a CPU's idle PM feature
400 int cpuidle_register_device(struct cpuidle_device
*dev
)
407 mutex_lock(&cpuidle_lock
);
412 __cpuidle_device_init(dev
);
414 ret
= __cpuidle_register_device(dev
);
418 ret
= cpuidle_add_sysfs(dev
);
422 ret
= cpuidle_enable_device(dev
);
426 cpuidle_install_idle_handler();
429 mutex_unlock(&cpuidle_lock
);
434 cpuidle_remove_sysfs(dev
);
436 __cpuidle_unregister_device(dev
);
440 EXPORT_SYMBOL_GPL(cpuidle_register_device
);
443 * cpuidle_unregister_device - unregisters a CPU's idle PM feature
446 void cpuidle_unregister_device(struct cpuidle_device
*dev
)
448 if (!dev
|| dev
->registered
== 0)
451 cpuidle_pause_and_lock();
453 cpuidle_disable_device(dev
);
455 cpuidle_remove_sysfs(dev
);
457 __cpuidle_unregister_device(dev
);
459 cpuidle_coupled_unregister_device(dev
);
461 cpuidle_resume_and_unlock();
464 EXPORT_SYMBOL_GPL(cpuidle_unregister_device
);
467 * cpuidle_unregister: unregister a driver and the devices. This function
468 * can be used only if the driver has been previously registered through
469 * the cpuidle_register function.
471 * @drv: a valid pointer to a struct cpuidle_driver
473 void cpuidle_unregister(struct cpuidle_driver
*drv
)
476 struct cpuidle_device
*device
;
478 for_each_cpu(cpu
, drv
->cpumask
) {
479 device
= &per_cpu(cpuidle_dev
, cpu
);
480 cpuidle_unregister_device(device
);
483 cpuidle_unregister_driver(drv
);
485 EXPORT_SYMBOL_GPL(cpuidle_unregister
);
488 * cpuidle_register: registers the driver and the cpu devices with the
489 * coupled_cpus passed as parameter. This function is used for all common
490 * initialization pattern there are in the arch specific drivers. The
491 * devices is globally defined in this file.
493 * @drv : a valid pointer to a struct cpuidle_driver
494 * @coupled_cpus: a cpumask for the coupled states
496 * Returns 0 on success, < 0 otherwise
498 int cpuidle_register(struct cpuidle_driver
*drv
,
499 const struct cpumask
*const coupled_cpus
)
502 struct cpuidle_device
*device
;
504 ret
= cpuidle_register_driver(drv
);
506 pr_err("failed to register cpuidle driver\n");
510 for_each_cpu(cpu
, drv
->cpumask
) {
511 device
= &per_cpu(cpuidle_dev
, cpu
);
514 #ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED
516 * On multiplatform for ARM, the coupled idle states could be
517 * enabled in the kernel even if the cpuidle driver does not
518 * use it. Note, coupled_cpus is a struct copy.
521 device
->coupled_cpus
= *coupled_cpus
;
523 ret
= cpuidle_register_device(device
);
527 pr_err("Failed to register cpuidle device for cpu%d\n", cpu
);
529 cpuidle_unregister(drv
);
535 EXPORT_SYMBOL_GPL(cpuidle_register
);
540 * This function gets called when a part of the kernel has a new latency
541 * requirement. This means we need to get all processors out of their C-state,
542 * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that
543 * wakes them all right up.
545 static int cpuidle_latency_notify(struct notifier_block
*b
,
546 unsigned long l
, void *v
)
548 wake_up_all_idle_cpus();
552 static struct notifier_block cpuidle_latency_notifier
= {
553 .notifier_call
= cpuidle_latency_notify
,
556 static inline void latency_notifier_init(struct notifier_block
*n
)
558 pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY
, n
);
561 #else /* CONFIG_SMP */
563 #define latency_notifier_init(x) do { } while (0)
565 #endif /* CONFIG_SMP */
568 * cpuidle_init - core initializer
570 static int __init
cpuidle_init(void)
574 if (cpuidle_disabled())
577 ret
= cpuidle_add_interface(cpu_subsys
.dev_root
);
581 latency_notifier_init(&cpuidle_latency_notifier
);
586 module_param(off
, int, 0444);
587 core_initcall(cpuidle_init
);