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/sched/clock.h>
16 #include <linux/notifier.h>
17 #include <linux/pm_qos.h>
18 #include <linux/cpu.h>
19 #include <linux/cpuidle.h>
20 #include <linux/ktime.h>
21 #include <linux/hrtimer.h>
22 #include <linux/module.h>
23 #include <linux/suspend.h>
24 #include <linux/tick.h>
25 #include <trace/events/power.h>
29 DEFINE_PER_CPU(struct cpuidle_device
*, cpuidle_devices
);
30 DEFINE_PER_CPU(struct cpuidle_device
, cpuidle_dev
);
32 DEFINE_MUTEX(cpuidle_lock
);
33 LIST_HEAD(cpuidle_detected_devices
);
35 static int enabled_devices
;
36 static int off __read_mostly
;
37 static int initialized __read_mostly
;
39 int cpuidle_disabled(void)
43 void disable_cpuidle(void)
48 bool cpuidle_not_available(struct cpuidle_driver
*drv
,
49 struct cpuidle_device
*dev
)
51 return off
|| !initialized
|| !drv
|| !dev
|| !dev
->enabled
;
55 * cpuidle_play_dead - cpu off-lining
57 * Returns in case of an error or no driver
59 int cpuidle_play_dead(void)
61 struct cpuidle_device
*dev
= __this_cpu_read(cpuidle_devices
);
62 struct cpuidle_driver
*drv
= cpuidle_get_cpu_driver(dev
);
68 /* Find lowest-power state that supports long-term idle */
69 for (i
= drv
->state_count
- 1; i
>= 0; i
--)
70 if (drv
->states
[i
].enter_dead
)
71 return drv
->states
[i
].enter_dead(dev
, i
);
76 static int find_deepest_state(struct cpuidle_driver
*drv
,
77 struct cpuidle_device
*dev
,
79 unsigned int forbidden_flags
,
85 for (i
= 1; i
< drv
->state_count
; i
++) {
86 struct cpuidle_state
*s
= &drv
->states
[i
];
88 if (dev
->states_usage
[i
].disable
||
89 s
->exit_latency_ns
<= latency_req
||
90 s
->exit_latency_ns
> max_latency_ns
||
91 (s
->flags
& forbidden_flags
) ||
92 (s2idle
&& !s
->enter_s2idle
))
95 latency_req
= s
->exit_latency_ns
;
102 * cpuidle_use_deepest_state - Set/unset governor override mode.
103 * @latency_limit_ns: Idle state exit latency limit (or no override if 0).
105 * If @latency_limit_ns is nonzero, set the current CPU to use the deepest idle
106 * state with exit latency within @latency_limit_ns (override governors going
107 * forward), or do not override governors if it is zero.
109 void cpuidle_use_deepest_state(u64 latency_limit_ns
)
111 struct cpuidle_device
*dev
;
114 dev
= cpuidle_get_device();
116 dev
->forced_idle_latency_limit_ns
= latency_limit_ns
;
121 * cpuidle_find_deepest_state - Find the deepest available idle state.
122 * @drv: cpuidle driver for the given CPU.
123 * @dev: cpuidle device for the given CPU.
124 * @latency_limit_ns: Idle state exit latency limit
126 * Return: the index of the deepest available idle state.
128 int cpuidle_find_deepest_state(struct cpuidle_driver
*drv
,
129 struct cpuidle_device
*dev
,
130 u64 latency_limit_ns
)
132 return find_deepest_state(drv
, dev
, latency_limit_ns
, 0, false);
135 #ifdef CONFIG_SUSPEND
136 static void enter_s2idle_proper(struct cpuidle_driver
*drv
,
137 struct cpuidle_device
*dev
, int index
)
139 ktime_t time_start
, time_end
;
141 time_start
= ns_to_ktime(local_clock());
144 * trace_suspend_resume() called by tick_freeze() for the last CPU
145 * executing it contains RCU usage regarded as invalid in the idle
146 * context, so tell RCU about that.
148 RCU_NONIDLE(tick_freeze());
150 * The state used here cannot be a "coupled" one, because the "coupled"
151 * cpuidle mechanism enables interrupts and doing that with timekeeping
152 * suspended is generally unsafe.
154 stop_critical_timings();
155 drv
->states
[index
].enter_s2idle(dev
, drv
, index
);
156 WARN_ON(!irqs_disabled());
158 * timekeeping_resume() that will be called by tick_unfreeze() for the
159 * first CPU executing it calls functions containing RCU read-side
160 * critical sections, so tell RCU about that.
162 RCU_NONIDLE(tick_unfreeze());
163 start_critical_timings();
165 time_end
= ns_to_ktime(local_clock());
167 dev
->states_usage
[index
].s2idle_time
+= ktime_us_delta(time_end
, time_start
);
168 dev
->states_usage
[index
].s2idle_usage
++;
172 * cpuidle_enter_s2idle - Enter an idle state suitable for suspend-to-idle.
173 * @drv: cpuidle driver for the given CPU.
174 * @dev: cpuidle device for the given CPU.
176 * If there are states with the ->enter_s2idle callback, find the deepest of
177 * them and enter it with frozen tick.
179 int cpuidle_enter_s2idle(struct cpuidle_driver
*drv
, struct cpuidle_device
*dev
)
184 * Find the deepest state with ->enter_s2idle present, which guarantees
185 * that interrupts won't be enabled when it exits and allows the tick to
188 index
= find_deepest_state(drv
, dev
, U64_MAX
, 0, true);
190 enter_s2idle_proper(drv
, dev
, index
);
195 #endif /* CONFIG_SUSPEND */
198 * cpuidle_enter_state - enter the state and update stats
199 * @dev: cpuidle device for this cpu
200 * @drv: cpuidle driver for this cpu
201 * @index: index into the states table in @drv of the state to enter
203 int cpuidle_enter_state(struct cpuidle_device
*dev
, struct cpuidle_driver
*drv
,
208 struct cpuidle_state
*target_state
= &drv
->states
[index
];
209 bool broadcast
= !!(target_state
->flags
& CPUIDLE_FLAG_TIMER_STOP
);
210 ktime_t time_start
, time_end
;
213 * Tell the time framework to switch to a broadcast timer because our
214 * local timer will be shut down. If a local timer is used from another
215 * CPU as a broadcast timer, this call may fail if it is not available.
217 if (broadcast
&& tick_broadcast_enter()) {
218 index
= find_deepest_state(drv
, dev
, target_state
->exit_latency_ns
,
219 CPUIDLE_FLAG_TIMER_STOP
, false);
224 target_state
= &drv
->states
[index
];
228 /* Take note of the planned idle state. */
229 sched_idle_set_state(target_state
);
231 trace_cpu_idle_rcuidle(index
, dev
->cpu
);
232 time_start
= ns_to_ktime(local_clock());
234 stop_critical_timings();
235 entered_state
= target_state
->enter(dev
, drv
, index
);
236 start_critical_timings();
238 sched_clock_idle_wakeup_event();
239 time_end
= ns_to_ktime(local_clock());
240 trace_cpu_idle_rcuidle(PWR_EVENT_EXIT
, dev
->cpu
);
242 /* The cpu is no longer idle or about to enter idle. */
243 sched_idle_set_state(NULL
);
246 if (WARN_ON_ONCE(!irqs_disabled()))
249 tick_broadcast_exit();
252 if (!cpuidle_state_is_coupled(drv
, index
))
255 if (entered_state
>= 0) {
256 s64 diff
, delay
= drv
->states
[entered_state
].exit_latency_ns
;
260 * Update cpuidle counters
261 * This can be moved to within driver enter routine,
262 * but that results in multiple copies of same code.
264 diff
= ktime_sub(time_end
, time_start
);
266 dev
->last_residency_ns
= diff
;
267 dev
->states_usage
[entered_state
].time_ns
+= diff
;
268 dev
->states_usage
[entered_state
].usage
++;
270 if (diff
< drv
->states
[entered_state
].target_residency_ns
) {
271 for (i
= entered_state
- 1; i
>= 0; i
--) {
272 if (dev
->states_usage
[i
].disable
)
275 /* Shallower states are enabled, so update. */
276 dev
->states_usage
[entered_state
].above
++;
279 } else if (diff
> delay
) {
280 for (i
= entered_state
+ 1; i
< drv
->state_count
; i
++) {
281 if (dev
->states_usage
[i
].disable
)
285 * Update if a deeper state would have been a
286 * better match for the observed idle duration.
288 if (diff
- delay
>= drv
->states
[i
].target_residency_ns
)
289 dev
->states_usage
[entered_state
].below
++;
295 dev
->last_residency_ns
= 0;
298 return entered_state
;
302 * cpuidle_select - ask the cpuidle framework to choose an idle state
304 * @drv: the cpuidle driver
305 * @dev: the cpuidle device
306 * @stop_tick: indication on whether or not to stop the tick
308 * Returns the index of the idle state. The return value must not be negative.
310 * The memory location pointed to by @stop_tick is expected to be written the
311 * 'false' boolean value if the scheduler tick should not be stopped before
312 * entering the returned state.
314 int cpuidle_select(struct cpuidle_driver
*drv
, struct cpuidle_device
*dev
,
317 return cpuidle_curr_governor
->select(drv
, dev
, stop_tick
);
321 * cpuidle_enter - enter into the specified idle state
323 * @drv: the cpuidle driver tied with the cpu
324 * @dev: the cpuidle device
325 * @index: the index in the idle state table
327 * Returns the index in the idle state, < 0 in case of error.
328 * The error code depends on the backend driver
330 int cpuidle_enter(struct cpuidle_driver
*drv
, struct cpuidle_device
*dev
,
336 * Store the next hrtimer, which becomes either next tick or the next
337 * timer event, whatever expires first. Additionally, to make this data
338 * useful for consumers outside cpuidle, we rely on that the governor's
339 * ->select() callback have decided, whether to stop the tick or not.
341 WRITE_ONCE(dev
->next_hrtimer
, tick_nohz_get_next_hrtimer());
343 if (cpuidle_state_is_coupled(drv
, index
))
344 ret
= cpuidle_enter_state_coupled(dev
, drv
, index
);
346 ret
= cpuidle_enter_state(dev
, drv
, index
);
348 WRITE_ONCE(dev
->next_hrtimer
, 0);
353 * cpuidle_reflect - tell the underlying governor what was the state
356 * @dev : the cpuidle device
357 * @index: the index in the idle state table
360 void cpuidle_reflect(struct cpuidle_device
*dev
, int index
)
362 if (cpuidle_curr_governor
->reflect
&& index
>= 0)
363 cpuidle_curr_governor
->reflect(dev
, index
);
367 * cpuidle_poll_time - return amount of time to poll for,
368 * governors can override dev->poll_limit_ns if necessary
370 * @drv: the cpuidle driver tied with the cpu
371 * @dev: the cpuidle device
374 u64
cpuidle_poll_time(struct cpuidle_driver
*drv
,
375 struct cpuidle_device
*dev
)
380 if (dev
->poll_limit_ns
)
381 return dev
->poll_limit_ns
;
383 limit_ns
= TICK_NSEC
;
384 for (i
= 1; i
< drv
->state_count
; i
++) {
385 if (dev
->states_usage
[i
].disable
)
388 limit_ns
= drv
->states
[i
].target_residency_ns
;
392 dev
->poll_limit_ns
= limit_ns
;
394 return dev
->poll_limit_ns
;
398 * cpuidle_install_idle_handler - installs the cpuidle idle loop handler
400 void cpuidle_install_idle_handler(void)
402 if (enabled_devices
) {
403 /* Make sure all changes finished before we switch to new idle */
410 * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler
412 void cpuidle_uninstall_idle_handler(void)
414 if (enabled_devices
) {
416 wake_up_all_idle_cpus();
420 * Make sure external observers (such as the scheduler)
421 * are done looking at pointed idle states.
427 * cpuidle_pause_and_lock - temporarily disables CPUIDLE
429 void cpuidle_pause_and_lock(void)
431 mutex_lock(&cpuidle_lock
);
432 cpuidle_uninstall_idle_handler();
435 EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock
);
438 * cpuidle_resume_and_unlock - resumes CPUIDLE operation
440 void cpuidle_resume_and_unlock(void)
442 cpuidle_install_idle_handler();
443 mutex_unlock(&cpuidle_lock
);
446 EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock
);
448 /* Currently used in suspend/resume path to suspend cpuidle */
449 void cpuidle_pause(void)
451 mutex_lock(&cpuidle_lock
);
452 cpuidle_uninstall_idle_handler();
453 mutex_unlock(&cpuidle_lock
);
456 /* Currently used in suspend/resume path to resume cpuidle */
457 void cpuidle_resume(void)
459 mutex_lock(&cpuidle_lock
);
460 cpuidle_install_idle_handler();
461 mutex_unlock(&cpuidle_lock
);
465 * cpuidle_enable_device - enables idle PM for a CPU
468 * This function must be called between cpuidle_pause_and_lock and
469 * cpuidle_resume_and_unlock when used externally.
471 int cpuidle_enable_device(struct cpuidle_device
*dev
)
474 struct cpuidle_driver
*drv
;
482 if (!cpuidle_curr_governor
)
485 drv
= cpuidle_get_cpu_driver(dev
);
490 if (!dev
->registered
)
493 ret
= cpuidle_add_device_sysfs(dev
);
497 if (cpuidle_curr_governor
->enable
) {
498 ret
= cpuidle_curr_governor
->enable(drv
, dev
);
511 cpuidle_remove_device_sysfs(dev
);
516 EXPORT_SYMBOL_GPL(cpuidle_enable_device
);
519 * cpuidle_disable_device - disables idle PM for a CPU
522 * This function must be called between cpuidle_pause_and_lock and
523 * cpuidle_resume_and_unlock when used externally.
525 void cpuidle_disable_device(struct cpuidle_device
*dev
)
527 struct cpuidle_driver
*drv
= cpuidle_get_cpu_driver(dev
);
529 if (!dev
|| !dev
->enabled
)
532 if (!drv
|| !cpuidle_curr_governor
)
537 if (cpuidle_curr_governor
->disable
)
538 cpuidle_curr_governor
->disable(drv
, dev
);
540 cpuidle_remove_device_sysfs(dev
);
544 EXPORT_SYMBOL_GPL(cpuidle_disable_device
);
546 static void __cpuidle_unregister_device(struct cpuidle_device
*dev
)
548 struct cpuidle_driver
*drv
= cpuidle_get_cpu_driver(dev
);
550 list_del(&dev
->device_list
);
551 per_cpu(cpuidle_devices
, dev
->cpu
) = NULL
;
552 module_put(drv
->owner
);
557 static void __cpuidle_device_init(struct cpuidle_device
*dev
)
559 memset(dev
->states_usage
, 0, sizeof(dev
->states_usage
));
560 dev
->last_residency_ns
= 0;
561 dev
->next_hrtimer
= 0;
565 * __cpuidle_register_device - internal register function called before register
566 * and enable routines
569 * cpuidle_lock mutex must be held before this is called
571 static int __cpuidle_register_device(struct cpuidle_device
*dev
)
573 struct cpuidle_driver
*drv
= cpuidle_get_cpu_driver(dev
);
576 if (!try_module_get(drv
->owner
))
579 for (i
= 0; i
< drv
->state_count
; i
++) {
580 if (drv
->states
[i
].flags
& CPUIDLE_FLAG_UNUSABLE
)
581 dev
->states_usage
[i
].disable
|= CPUIDLE_STATE_DISABLED_BY_DRIVER
;
583 if (drv
->states
[i
].flags
& CPUIDLE_FLAG_OFF
)
584 dev
->states_usage
[i
].disable
|= CPUIDLE_STATE_DISABLED_BY_USER
;
587 per_cpu(cpuidle_devices
, dev
->cpu
) = dev
;
588 list_add(&dev
->device_list
, &cpuidle_detected_devices
);
590 ret
= cpuidle_coupled_register_device(dev
);
592 __cpuidle_unregister_device(dev
);
600 * cpuidle_register_device - registers a CPU's idle PM feature
603 int cpuidle_register_device(struct cpuidle_device
*dev
)
610 mutex_lock(&cpuidle_lock
);
615 __cpuidle_device_init(dev
);
617 ret
= __cpuidle_register_device(dev
);
621 ret
= cpuidle_add_sysfs(dev
);
625 ret
= cpuidle_enable_device(dev
);
629 cpuidle_install_idle_handler();
632 mutex_unlock(&cpuidle_lock
);
637 cpuidle_remove_sysfs(dev
);
639 __cpuidle_unregister_device(dev
);
643 EXPORT_SYMBOL_GPL(cpuidle_register_device
);
646 * cpuidle_unregister_device - unregisters a CPU's idle PM feature
649 void cpuidle_unregister_device(struct cpuidle_device
*dev
)
651 if (!dev
|| dev
->registered
== 0)
654 cpuidle_pause_and_lock();
656 cpuidle_disable_device(dev
);
658 cpuidle_remove_sysfs(dev
);
660 __cpuidle_unregister_device(dev
);
662 cpuidle_coupled_unregister_device(dev
);
664 cpuidle_resume_and_unlock();
667 EXPORT_SYMBOL_GPL(cpuidle_unregister_device
);
670 * cpuidle_unregister: unregister a driver and the devices. This function
671 * can be used only if the driver has been previously registered through
672 * the cpuidle_register function.
674 * @drv: a valid pointer to a struct cpuidle_driver
676 void cpuidle_unregister(struct cpuidle_driver
*drv
)
679 struct cpuidle_device
*device
;
681 for_each_cpu(cpu
, drv
->cpumask
) {
682 device
= &per_cpu(cpuidle_dev
, cpu
);
683 cpuidle_unregister_device(device
);
686 cpuidle_unregister_driver(drv
);
688 EXPORT_SYMBOL_GPL(cpuidle_unregister
);
691 * cpuidle_register: registers the driver and the cpu devices with the
692 * coupled_cpus passed as parameter. This function is used for all common
693 * initialization pattern there are in the arch specific drivers. The
694 * devices is globally defined in this file.
696 * @drv : a valid pointer to a struct cpuidle_driver
697 * @coupled_cpus: a cpumask for the coupled states
699 * Returns 0 on success, < 0 otherwise
701 int cpuidle_register(struct cpuidle_driver
*drv
,
702 const struct cpumask
*const coupled_cpus
)
705 struct cpuidle_device
*device
;
707 ret
= cpuidle_register_driver(drv
);
709 pr_err("failed to register cpuidle driver\n");
713 for_each_cpu(cpu
, drv
->cpumask
) {
714 device
= &per_cpu(cpuidle_dev
, cpu
);
717 #ifdef CONFIG_ARCH_NEEDS_CPU_IDLE_COUPLED
719 * On multiplatform for ARM, the coupled idle states could be
720 * enabled in the kernel even if the cpuidle driver does not
721 * use it. Note, coupled_cpus is a struct copy.
724 device
->coupled_cpus
= *coupled_cpus
;
726 ret
= cpuidle_register_device(device
);
730 pr_err("Failed to register cpuidle device for cpu%d\n", cpu
);
732 cpuidle_unregister(drv
);
738 EXPORT_SYMBOL_GPL(cpuidle_register
);
741 * cpuidle_init - core initializer
743 static int __init
cpuidle_init(void)
745 if (cpuidle_disabled())
748 return cpuidle_add_interface(cpu_subsys
.dev_root
);
751 module_param(off
, int, 0444);
752 module_param_string(governor
, param_governor
, CPUIDLE_NAME_LEN
, 0444);
753 core_initcall(cpuidle_init
);