2 * drivers/base/power/main.c - Where the driver meets power management.
4 * Copyright (c) 2003 Patrick Mochel
5 * Copyright (c) 2003 Open Source Development Lab
7 * This file is released under the GPLv2
10 * The driver model core calls device_pm_add() when a device is registered.
11 * This will initialize the embedded device_pm_info object in the device
12 * and add it to the list of power-controlled devices. sysfs entries for
13 * controlling device power management will also be added.
15 * A separate list is used for keeping track of power info, because the power
16 * domain dependencies may differ from the ancestral dependencies that the
17 * subsystem list maintains.
20 #include <linux/device.h>
21 #include <linux/kallsyms.h>
22 #include <linux/export.h>
23 #include <linux/mutex.h>
25 #include <linux/pm_runtime.h>
26 #include <linux/resume-trace.h>
27 #include <linux/interrupt.h>
28 #include <linux/sched.h>
29 #include <linux/async.h>
30 #include <linux/suspend.h>
31 #include <trace/events/power.h>
32 #include <linux/cpufreq.h>
33 #include <linux/cpuidle.h>
34 #include <linux/timer.h>
39 typedef int (*pm_callback_t
)(struct device
*);
42 * The entries in the dpm_list list are in a depth first order, simply
43 * because children are guaranteed to be discovered after parents, and
44 * are inserted at the back of the list on discovery.
46 * Since device_pm_add() may be called with a device lock held,
47 * we must never try to acquire a device lock while holding
52 static LIST_HEAD(dpm_prepared_list
);
53 static LIST_HEAD(dpm_suspended_list
);
54 static LIST_HEAD(dpm_late_early_list
);
55 static LIST_HEAD(dpm_noirq_list
);
57 struct suspend_stats suspend_stats
;
58 static DEFINE_MUTEX(dpm_list_mtx
);
59 static pm_message_t pm_transition
;
61 static int async_error
;
63 static char *pm_verb(int event
)
66 case PM_EVENT_SUSPEND
:
72 case PM_EVENT_QUIESCE
:
74 case PM_EVENT_HIBERNATE
:
78 case PM_EVENT_RESTORE
:
80 case PM_EVENT_RECOVER
:
83 return "(unknown PM event)";
88 * device_pm_sleep_init - Initialize system suspend-related device fields.
89 * @dev: Device object being initialized.
91 void device_pm_sleep_init(struct device
*dev
)
93 dev
->power
.is_prepared
= false;
94 dev
->power
.is_suspended
= false;
95 dev
->power
.is_noirq_suspended
= false;
96 dev
->power
.is_late_suspended
= false;
97 init_completion(&dev
->power
.completion
);
98 complete_all(&dev
->power
.completion
);
99 dev
->power
.wakeup
= NULL
;
100 INIT_LIST_HEAD(&dev
->power
.entry
);
104 * device_pm_lock - Lock the list of active devices used by the PM core.
106 void device_pm_lock(void)
108 mutex_lock(&dpm_list_mtx
);
112 * device_pm_unlock - Unlock the list of active devices used by the PM core.
114 void device_pm_unlock(void)
116 mutex_unlock(&dpm_list_mtx
);
120 * device_pm_add - Add a device to the PM core's list of active devices.
121 * @dev: Device to add to the list.
123 void device_pm_add(struct device
*dev
)
125 pr_debug("PM: Adding info for %s:%s\n",
126 dev
->bus
? dev
->bus
->name
: "No Bus", dev_name(dev
));
127 mutex_lock(&dpm_list_mtx
);
128 if (dev
->parent
&& dev
->parent
->power
.is_prepared
)
129 dev_warn(dev
, "parent %s should not be sleeping\n",
130 dev_name(dev
->parent
));
131 list_add_tail(&dev
->power
.entry
, &dpm_list
);
132 mutex_unlock(&dpm_list_mtx
);
136 * device_pm_remove - Remove a device from the PM core's list of active devices.
137 * @dev: Device to be removed from the list.
139 void device_pm_remove(struct device
*dev
)
141 pr_debug("PM: Removing info for %s:%s\n",
142 dev
->bus
? dev
->bus
->name
: "No Bus", dev_name(dev
));
143 complete_all(&dev
->power
.completion
);
144 mutex_lock(&dpm_list_mtx
);
145 list_del_init(&dev
->power
.entry
);
146 mutex_unlock(&dpm_list_mtx
);
147 device_wakeup_disable(dev
);
148 pm_runtime_remove(dev
);
152 * device_pm_move_before - Move device in the PM core's list of active devices.
153 * @deva: Device to move in dpm_list.
154 * @devb: Device @deva should come before.
156 void device_pm_move_before(struct device
*deva
, struct device
*devb
)
158 pr_debug("PM: Moving %s:%s before %s:%s\n",
159 deva
->bus
? deva
->bus
->name
: "No Bus", dev_name(deva
),
160 devb
->bus
? devb
->bus
->name
: "No Bus", dev_name(devb
));
161 /* Delete deva from dpm_list and reinsert before devb. */
162 list_move_tail(&deva
->power
.entry
, &devb
->power
.entry
);
166 * device_pm_move_after - Move device in the PM core's list of active devices.
167 * @deva: Device to move in dpm_list.
168 * @devb: Device @deva should come after.
170 void device_pm_move_after(struct device
*deva
, struct device
*devb
)
172 pr_debug("PM: Moving %s:%s after %s:%s\n",
173 deva
->bus
? deva
->bus
->name
: "No Bus", dev_name(deva
),
174 devb
->bus
? devb
->bus
->name
: "No Bus", dev_name(devb
));
175 /* Delete deva from dpm_list and reinsert after devb. */
176 list_move(&deva
->power
.entry
, &devb
->power
.entry
);
180 * device_pm_move_last - Move device to end of the PM core's list of devices.
181 * @dev: Device to move in dpm_list.
183 void device_pm_move_last(struct device
*dev
)
185 pr_debug("PM: Moving %s:%s to end of list\n",
186 dev
->bus
? dev
->bus
->name
: "No Bus", dev_name(dev
));
187 list_move_tail(&dev
->power
.entry
, &dpm_list
);
190 static ktime_t
initcall_debug_start(struct device
*dev
)
192 ktime_t calltime
= ktime_set(0, 0);
194 if (pm_print_times_enabled
) {
195 pr_info("calling %s+ @ %i, parent: %s\n",
196 dev_name(dev
), task_pid_nr(current
),
197 dev
->parent
? dev_name(dev
->parent
) : "none");
198 calltime
= ktime_get();
204 static void initcall_debug_report(struct device
*dev
, ktime_t calltime
,
205 int error
, pm_message_t state
, char *info
)
210 rettime
= ktime_get();
211 nsecs
= (s64
) ktime_to_ns(ktime_sub(rettime
, calltime
));
213 if (pm_print_times_enabled
) {
214 pr_info("call %s+ returned %d after %Ld usecs\n", dev_name(dev
),
215 error
, (unsigned long long)nsecs
>> 10);
218 trace_device_pm_report_time(dev
, info
, nsecs
, pm_verb(state
.event
),
223 * dpm_wait - Wait for a PM operation to complete.
224 * @dev: Device to wait for.
225 * @async: If unset, wait only if the device's power.async_suspend flag is set.
227 static void dpm_wait(struct device
*dev
, bool async
)
232 if (async
|| (pm_async_enabled
&& dev
->power
.async_suspend
))
233 wait_for_completion(&dev
->power
.completion
);
236 static int dpm_wait_fn(struct device
*dev
, void *async_ptr
)
238 dpm_wait(dev
, *((bool *)async_ptr
));
242 static void dpm_wait_for_children(struct device
*dev
, bool async
)
244 device_for_each_child(dev
, &async
, dpm_wait_fn
);
248 * pm_op - Return the PM operation appropriate for given PM event.
249 * @ops: PM operations to choose from.
250 * @state: PM transition of the system being carried out.
252 static pm_callback_t
pm_op(const struct dev_pm_ops
*ops
, pm_message_t state
)
254 switch (state
.event
) {
255 #ifdef CONFIG_SUSPEND
256 case PM_EVENT_SUSPEND
:
258 case PM_EVENT_RESUME
:
260 #endif /* CONFIG_SUSPEND */
261 #ifdef CONFIG_HIBERNATE_CALLBACKS
262 case PM_EVENT_FREEZE
:
263 case PM_EVENT_QUIESCE
:
265 case PM_EVENT_HIBERNATE
:
266 return ops
->poweroff
;
268 case PM_EVENT_RECOVER
:
271 case PM_EVENT_RESTORE
:
273 #endif /* CONFIG_HIBERNATE_CALLBACKS */
280 * pm_late_early_op - Return the PM operation appropriate for given PM event.
281 * @ops: PM operations to choose from.
282 * @state: PM transition of the system being carried out.
284 * Runtime PM is disabled for @dev while this function is being executed.
286 static pm_callback_t
pm_late_early_op(const struct dev_pm_ops
*ops
,
289 switch (state
.event
) {
290 #ifdef CONFIG_SUSPEND
291 case PM_EVENT_SUSPEND
:
292 return ops
->suspend_late
;
293 case PM_EVENT_RESUME
:
294 return ops
->resume_early
;
295 #endif /* CONFIG_SUSPEND */
296 #ifdef CONFIG_HIBERNATE_CALLBACKS
297 case PM_EVENT_FREEZE
:
298 case PM_EVENT_QUIESCE
:
299 return ops
->freeze_late
;
300 case PM_EVENT_HIBERNATE
:
301 return ops
->poweroff_late
;
303 case PM_EVENT_RECOVER
:
304 return ops
->thaw_early
;
305 case PM_EVENT_RESTORE
:
306 return ops
->restore_early
;
307 #endif /* CONFIG_HIBERNATE_CALLBACKS */
314 * pm_noirq_op - Return the PM operation appropriate for given PM event.
315 * @ops: PM operations to choose from.
316 * @state: PM transition of the system being carried out.
318 * The driver of @dev will not receive interrupts while this function is being
321 static pm_callback_t
pm_noirq_op(const struct dev_pm_ops
*ops
, pm_message_t state
)
323 switch (state
.event
) {
324 #ifdef CONFIG_SUSPEND
325 case PM_EVENT_SUSPEND
:
326 return ops
->suspend_noirq
;
327 case PM_EVENT_RESUME
:
328 return ops
->resume_noirq
;
329 #endif /* CONFIG_SUSPEND */
330 #ifdef CONFIG_HIBERNATE_CALLBACKS
331 case PM_EVENT_FREEZE
:
332 case PM_EVENT_QUIESCE
:
333 return ops
->freeze_noirq
;
334 case PM_EVENT_HIBERNATE
:
335 return ops
->poweroff_noirq
;
337 case PM_EVENT_RECOVER
:
338 return ops
->thaw_noirq
;
339 case PM_EVENT_RESTORE
:
340 return ops
->restore_noirq
;
341 #endif /* CONFIG_HIBERNATE_CALLBACKS */
347 static void pm_dev_dbg(struct device
*dev
, pm_message_t state
, char *info
)
349 dev_dbg(dev
, "%s%s%s\n", info
, pm_verb(state
.event
),
350 ((state
.event
& PM_EVENT_SLEEP
) && device_may_wakeup(dev
)) ?
351 ", may wakeup" : "");
354 static void pm_dev_err(struct device
*dev
, pm_message_t state
, char *info
,
357 printk(KERN_ERR
"PM: Device %s failed to %s%s: error %d\n",
358 dev_name(dev
), pm_verb(state
.event
), info
, error
);
361 static void dpm_show_time(ktime_t starttime
, pm_message_t state
, char *info
)
367 calltime
= ktime_get();
368 usecs64
= ktime_to_ns(ktime_sub(calltime
, starttime
));
369 do_div(usecs64
, NSEC_PER_USEC
);
373 pr_info("PM: %s%s%s of devices complete after %ld.%03ld msecs\n",
374 info
?: "", info
? " " : "", pm_verb(state
.event
),
375 usecs
/ USEC_PER_MSEC
, usecs
% USEC_PER_MSEC
);
378 static int dpm_run_callback(pm_callback_t cb
, struct device
*dev
,
379 pm_message_t state
, char *info
)
387 calltime
= initcall_debug_start(dev
);
389 pm_dev_dbg(dev
, state
, info
);
391 suspend_report_result(cb
, error
);
393 initcall_debug_report(dev
, calltime
, error
, state
, info
);
398 #ifdef CONFIG_DPM_WATCHDOG
399 struct dpm_watchdog
{
401 struct task_struct
*tsk
;
402 struct timer_list timer
;
405 #define DECLARE_DPM_WATCHDOG_ON_STACK(wd) \
406 struct dpm_watchdog wd
409 * dpm_watchdog_handler - Driver suspend / resume watchdog handler.
410 * @data: Watchdog object address.
412 * Called when a driver has timed out suspending or resuming.
413 * There's not much we can do here to recover so panic() to
414 * capture a crash-dump in pstore.
416 static void dpm_watchdog_handler(unsigned long data
)
418 struct dpm_watchdog
*wd
= (void *)data
;
420 dev_emerg(wd
->dev
, "**** DPM device timeout ****\n");
421 show_stack(wd
->tsk
, NULL
);
422 panic("%s %s: unrecoverable failure\n",
423 dev_driver_string(wd
->dev
), dev_name(wd
->dev
));
427 * dpm_watchdog_set - Enable pm watchdog for given device.
428 * @wd: Watchdog. Must be allocated on the stack.
429 * @dev: Device to handle.
431 static void dpm_watchdog_set(struct dpm_watchdog
*wd
, struct device
*dev
)
433 struct timer_list
*timer
= &wd
->timer
;
438 init_timer_on_stack(timer
);
439 /* use same timeout value for both suspend and resume */
440 timer
->expires
= jiffies
+ HZ
* CONFIG_DPM_WATCHDOG_TIMEOUT
;
441 timer
->function
= dpm_watchdog_handler
;
442 timer
->data
= (unsigned long)wd
;
447 * dpm_watchdog_clear - Disable suspend/resume watchdog.
448 * @wd: Watchdog to disable.
450 static void dpm_watchdog_clear(struct dpm_watchdog
*wd
)
452 struct timer_list
*timer
= &wd
->timer
;
454 del_timer_sync(timer
);
455 destroy_timer_on_stack(timer
);
458 #define DECLARE_DPM_WATCHDOG_ON_STACK(wd)
459 #define dpm_watchdog_set(x, y)
460 #define dpm_watchdog_clear(x)
463 /*------------------------- Resume routines -------------------------*/
466 * device_resume_noirq - Execute an "early resume" callback for given device.
467 * @dev: Device to handle.
468 * @state: PM transition of the system being carried out.
470 * The driver of @dev will not receive interrupts while this function is being
473 static int device_resume_noirq(struct device
*dev
, pm_message_t state
, bool async
)
475 pm_callback_t callback
= NULL
;
482 if (dev
->power
.syscore
)
485 if (!dev
->power
.is_noirq_suspended
)
488 dpm_wait(dev
->parent
, async
);
490 if (dev
->pm_domain
) {
491 info
= "noirq power domain ";
492 callback
= pm_noirq_op(&dev
->pm_domain
->ops
, state
);
493 } else if (dev
->type
&& dev
->type
->pm
) {
494 info
= "noirq type ";
495 callback
= pm_noirq_op(dev
->type
->pm
, state
);
496 } else if (dev
->class && dev
->class->pm
) {
497 info
= "noirq class ";
498 callback
= pm_noirq_op(dev
->class->pm
, state
);
499 } else if (dev
->bus
&& dev
->bus
->pm
) {
501 callback
= pm_noirq_op(dev
->bus
->pm
, state
);
504 if (!callback
&& dev
->driver
&& dev
->driver
->pm
) {
505 info
= "noirq driver ";
506 callback
= pm_noirq_op(dev
->driver
->pm
, state
);
509 error
= dpm_run_callback(callback
, dev
, state
, info
);
510 dev
->power
.is_noirq_suspended
= false;
513 complete_all(&dev
->power
.completion
);
518 static bool is_async(struct device
*dev
)
520 return dev
->power
.async_suspend
&& pm_async_enabled
521 && !pm_trace_is_enabled();
524 static void async_resume_noirq(void *data
, async_cookie_t cookie
)
526 struct device
*dev
= (struct device
*)data
;
529 error
= device_resume_noirq(dev
, pm_transition
, true);
531 pm_dev_err(dev
, pm_transition
, " async", error
);
537 * dpm_resume_noirq - Execute "noirq resume" callbacks for all devices.
538 * @state: PM transition of the system being carried out.
540 * Call the "noirq" resume handlers for all devices in dpm_noirq_list and
541 * enable device drivers to receive interrupts.
543 static void dpm_resume_noirq(pm_message_t state
)
546 ktime_t starttime
= ktime_get();
548 mutex_lock(&dpm_list_mtx
);
549 pm_transition
= state
;
552 * Advanced the async threads upfront,
553 * in case the starting of async threads is
554 * delayed by non-async resuming devices.
556 list_for_each_entry(dev
, &dpm_noirq_list
, power
.entry
) {
557 reinit_completion(&dev
->power
.completion
);
560 async_schedule(async_resume_noirq
, dev
);
564 while (!list_empty(&dpm_noirq_list
)) {
565 dev
= to_device(dpm_noirq_list
.next
);
567 list_move_tail(&dev
->power
.entry
, &dpm_late_early_list
);
568 mutex_unlock(&dpm_list_mtx
);
570 if (!is_async(dev
)) {
573 error
= device_resume_noirq(dev
, state
, false);
575 suspend_stats
.failed_resume_noirq
++;
576 dpm_save_failed_step(SUSPEND_RESUME_NOIRQ
);
577 dpm_save_failed_dev(dev_name(dev
));
578 pm_dev_err(dev
, state
, " noirq", error
);
582 mutex_lock(&dpm_list_mtx
);
585 mutex_unlock(&dpm_list_mtx
);
586 async_synchronize_full();
587 dpm_show_time(starttime
, state
, "noirq");
588 resume_device_irqs();
593 * device_resume_early - Execute an "early resume" callback for given device.
594 * @dev: Device to handle.
595 * @state: PM transition of the system being carried out.
597 * Runtime PM is disabled for @dev while this function is being executed.
599 static int device_resume_early(struct device
*dev
, pm_message_t state
, bool async
)
601 pm_callback_t callback
= NULL
;
608 if (dev
->power
.syscore
)
611 if (!dev
->power
.is_late_suspended
)
614 dpm_wait(dev
->parent
, async
);
616 if (dev
->pm_domain
) {
617 info
= "early power domain ";
618 callback
= pm_late_early_op(&dev
->pm_domain
->ops
, state
);
619 } else if (dev
->type
&& dev
->type
->pm
) {
620 info
= "early type ";
621 callback
= pm_late_early_op(dev
->type
->pm
, state
);
622 } else if (dev
->class && dev
->class->pm
) {
623 info
= "early class ";
624 callback
= pm_late_early_op(dev
->class->pm
, state
);
625 } else if (dev
->bus
&& dev
->bus
->pm
) {
627 callback
= pm_late_early_op(dev
->bus
->pm
, state
);
630 if (!callback
&& dev
->driver
&& dev
->driver
->pm
) {
631 info
= "early driver ";
632 callback
= pm_late_early_op(dev
->driver
->pm
, state
);
635 error
= dpm_run_callback(callback
, dev
, state
, info
);
636 dev
->power
.is_late_suspended
= false;
641 pm_runtime_enable(dev
);
642 complete_all(&dev
->power
.completion
);
646 static void async_resume_early(void *data
, async_cookie_t cookie
)
648 struct device
*dev
= (struct device
*)data
;
651 error
= device_resume_early(dev
, pm_transition
, true);
653 pm_dev_err(dev
, pm_transition
, " async", error
);
659 * dpm_resume_early - Execute "early resume" callbacks for all devices.
660 * @state: PM transition of the system being carried out.
662 static void dpm_resume_early(pm_message_t state
)
665 ktime_t starttime
= ktime_get();
667 mutex_lock(&dpm_list_mtx
);
668 pm_transition
= state
;
671 * Advanced the async threads upfront,
672 * in case the starting of async threads is
673 * delayed by non-async resuming devices.
675 list_for_each_entry(dev
, &dpm_late_early_list
, power
.entry
) {
676 reinit_completion(&dev
->power
.completion
);
679 async_schedule(async_resume_early
, dev
);
683 while (!list_empty(&dpm_late_early_list
)) {
684 dev
= to_device(dpm_late_early_list
.next
);
686 list_move_tail(&dev
->power
.entry
, &dpm_suspended_list
);
687 mutex_unlock(&dpm_list_mtx
);
689 if (!is_async(dev
)) {
692 error
= device_resume_early(dev
, state
, false);
694 suspend_stats
.failed_resume_early
++;
695 dpm_save_failed_step(SUSPEND_RESUME_EARLY
);
696 dpm_save_failed_dev(dev_name(dev
));
697 pm_dev_err(dev
, state
, " early", error
);
700 mutex_lock(&dpm_list_mtx
);
703 mutex_unlock(&dpm_list_mtx
);
704 async_synchronize_full();
705 dpm_show_time(starttime
, state
, "early");
709 * dpm_resume_start - Execute "noirq" and "early" device callbacks.
710 * @state: PM transition of the system being carried out.
712 void dpm_resume_start(pm_message_t state
)
714 dpm_resume_noirq(state
);
715 dpm_resume_early(state
);
717 EXPORT_SYMBOL_GPL(dpm_resume_start
);
720 * device_resume - Execute "resume" callbacks for given device.
721 * @dev: Device to handle.
722 * @state: PM transition of the system being carried out.
723 * @async: If true, the device is being resumed asynchronously.
725 static int device_resume(struct device
*dev
, pm_message_t state
, bool async
)
727 pm_callback_t callback
= NULL
;
730 DECLARE_DPM_WATCHDOG_ON_STACK(wd
);
735 if (dev
->power
.syscore
)
738 dpm_wait(dev
->parent
, async
);
739 dpm_watchdog_set(&wd
, dev
);
743 * This is a fib. But we'll allow new children to be added below
744 * a resumed device, even if the device hasn't been completed yet.
746 dev
->power
.is_prepared
= false;
748 if (!dev
->power
.is_suspended
)
751 if (dev
->pm_domain
) {
752 info
= "power domain ";
753 callback
= pm_op(&dev
->pm_domain
->ops
, state
);
757 if (dev
->type
&& dev
->type
->pm
) {
759 callback
= pm_op(dev
->type
->pm
, state
);
764 if (dev
->class->pm
) {
766 callback
= pm_op(dev
->class->pm
, state
);
768 } else if (dev
->class->resume
) {
769 info
= "legacy class ";
770 callback
= dev
->class->resume
;
778 callback
= pm_op(dev
->bus
->pm
, state
);
779 } else if (dev
->bus
->resume
) {
780 info
= "legacy bus ";
781 callback
= dev
->bus
->resume
;
787 if (!callback
&& dev
->driver
&& dev
->driver
->pm
) {
789 callback
= pm_op(dev
->driver
->pm
, state
);
793 error
= dpm_run_callback(callback
, dev
, state
, info
);
794 dev
->power
.is_suspended
= false;
798 dpm_watchdog_clear(&wd
);
801 complete_all(&dev
->power
.completion
);
808 static void async_resume(void *data
, async_cookie_t cookie
)
810 struct device
*dev
= (struct device
*)data
;
813 error
= device_resume(dev
, pm_transition
, true);
815 pm_dev_err(dev
, pm_transition
, " async", error
);
820 * dpm_resume - Execute "resume" callbacks for non-sysdev devices.
821 * @state: PM transition of the system being carried out.
823 * Execute the appropriate "resume" callback for all devices whose status
824 * indicates that they are suspended.
826 void dpm_resume(pm_message_t state
)
829 ktime_t starttime
= ktime_get();
833 mutex_lock(&dpm_list_mtx
);
834 pm_transition
= state
;
837 list_for_each_entry(dev
, &dpm_suspended_list
, power
.entry
) {
838 reinit_completion(&dev
->power
.completion
);
841 async_schedule(async_resume
, dev
);
845 while (!list_empty(&dpm_suspended_list
)) {
846 dev
= to_device(dpm_suspended_list
.next
);
848 if (!is_async(dev
)) {
851 mutex_unlock(&dpm_list_mtx
);
853 error
= device_resume(dev
, state
, false);
855 suspend_stats
.failed_resume
++;
856 dpm_save_failed_step(SUSPEND_RESUME
);
857 dpm_save_failed_dev(dev_name(dev
));
858 pm_dev_err(dev
, state
, "", error
);
861 mutex_lock(&dpm_list_mtx
);
863 if (!list_empty(&dev
->power
.entry
))
864 list_move_tail(&dev
->power
.entry
, &dpm_prepared_list
);
867 mutex_unlock(&dpm_list_mtx
);
868 async_synchronize_full();
869 dpm_show_time(starttime
, state
, NULL
);
875 * device_complete - Complete a PM transition for given device.
876 * @dev: Device to handle.
877 * @state: PM transition of the system being carried out.
879 static void device_complete(struct device
*dev
, pm_message_t state
)
881 void (*callback
)(struct device
*) = NULL
;
884 if (dev
->power
.syscore
)
889 if (dev
->pm_domain
) {
890 info
= "completing power domain ";
891 callback
= dev
->pm_domain
->ops
.complete
;
892 } else if (dev
->type
&& dev
->type
->pm
) {
893 info
= "completing type ";
894 callback
= dev
->type
->pm
->complete
;
895 } else if (dev
->class && dev
->class->pm
) {
896 info
= "completing class ";
897 callback
= dev
->class->pm
->complete
;
898 } else if (dev
->bus
&& dev
->bus
->pm
) {
899 info
= "completing bus ";
900 callback
= dev
->bus
->pm
->complete
;
903 if (!callback
&& dev
->driver
&& dev
->driver
->pm
) {
904 info
= "completing driver ";
905 callback
= dev
->driver
->pm
->complete
;
909 pm_dev_dbg(dev
, state
, info
);
919 * dpm_complete - Complete a PM transition for all non-sysdev devices.
920 * @state: PM transition of the system being carried out.
922 * Execute the ->complete() callbacks for all devices whose PM status is not
923 * DPM_ON (this allows new devices to be registered).
925 void dpm_complete(pm_message_t state
)
927 struct list_head list
;
931 INIT_LIST_HEAD(&list
);
932 mutex_lock(&dpm_list_mtx
);
933 while (!list_empty(&dpm_prepared_list
)) {
934 struct device
*dev
= to_device(dpm_prepared_list
.prev
);
937 dev
->power
.is_prepared
= false;
938 list_move(&dev
->power
.entry
, &list
);
939 mutex_unlock(&dpm_list_mtx
);
941 device_complete(dev
, state
);
943 mutex_lock(&dpm_list_mtx
);
946 list_splice(&list
, &dpm_list
);
947 mutex_unlock(&dpm_list_mtx
);
951 * dpm_resume_end - Execute "resume" callbacks and complete system transition.
952 * @state: PM transition of the system being carried out.
954 * Execute "resume" callbacks for all devices and complete the PM transition of
957 void dpm_resume_end(pm_message_t state
)
962 EXPORT_SYMBOL_GPL(dpm_resume_end
);
965 /*------------------------- Suspend routines -------------------------*/
968 * resume_event - Return a "resume" message for given "suspend" sleep state.
969 * @sleep_state: PM message representing a sleep state.
971 * Return a PM message representing the resume event corresponding to given
974 static pm_message_t
resume_event(pm_message_t sleep_state
)
976 switch (sleep_state
.event
) {
977 case PM_EVENT_SUSPEND
:
979 case PM_EVENT_FREEZE
:
980 case PM_EVENT_QUIESCE
:
982 case PM_EVENT_HIBERNATE
:
989 * device_suspend_noirq - Execute a "late suspend" callback for given device.
990 * @dev: Device to handle.
991 * @state: PM transition of the system being carried out.
993 * The driver of @dev will not receive interrupts while this function is being
996 static int __device_suspend_noirq(struct device
*dev
, pm_message_t state
, bool async
)
998 pm_callback_t callback
= NULL
;
1005 if (pm_wakeup_pending()) {
1006 async_error
= -EBUSY
;
1010 if (dev
->power
.syscore
)
1013 dpm_wait_for_children(dev
, async
);
1015 if (dev
->pm_domain
) {
1016 info
= "noirq power domain ";
1017 callback
= pm_noirq_op(&dev
->pm_domain
->ops
, state
);
1018 } else if (dev
->type
&& dev
->type
->pm
) {
1019 info
= "noirq type ";
1020 callback
= pm_noirq_op(dev
->type
->pm
, state
);
1021 } else if (dev
->class && dev
->class->pm
) {
1022 info
= "noirq class ";
1023 callback
= pm_noirq_op(dev
->class->pm
, state
);
1024 } else if (dev
->bus
&& dev
->bus
->pm
) {
1025 info
= "noirq bus ";
1026 callback
= pm_noirq_op(dev
->bus
->pm
, state
);
1029 if (!callback
&& dev
->driver
&& dev
->driver
->pm
) {
1030 info
= "noirq driver ";
1031 callback
= pm_noirq_op(dev
->driver
->pm
, state
);
1034 error
= dpm_run_callback(callback
, dev
, state
, info
);
1036 dev
->power
.is_noirq_suspended
= true;
1038 async_error
= error
;
1041 complete_all(&dev
->power
.completion
);
1045 static void async_suspend_noirq(void *data
, async_cookie_t cookie
)
1047 struct device
*dev
= (struct device
*)data
;
1050 error
= __device_suspend_noirq(dev
, pm_transition
, true);
1052 dpm_save_failed_dev(dev_name(dev
));
1053 pm_dev_err(dev
, pm_transition
, " async", error
);
1059 static int device_suspend_noirq(struct device
*dev
)
1061 reinit_completion(&dev
->power
.completion
);
1063 if (pm_async_enabled
&& dev
->power
.async_suspend
) {
1065 async_schedule(async_suspend_noirq
, dev
);
1068 return __device_suspend_noirq(dev
, pm_transition
, false);
1072 * dpm_suspend_noirq - Execute "noirq suspend" callbacks for all devices.
1073 * @state: PM transition of the system being carried out.
1075 * Prevent device drivers from receiving interrupts and call the "noirq" suspend
1076 * handlers for all non-sysdev devices.
1078 static int dpm_suspend_noirq(pm_message_t state
)
1080 ktime_t starttime
= ktime_get();
1084 suspend_device_irqs();
1085 mutex_lock(&dpm_list_mtx
);
1086 pm_transition
= state
;
1089 while (!list_empty(&dpm_late_early_list
)) {
1090 struct device
*dev
= to_device(dpm_late_early_list
.prev
);
1093 mutex_unlock(&dpm_list_mtx
);
1095 error
= device_suspend_noirq(dev
);
1097 mutex_lock(&dpm_list_mtx
);
1099 pm_dev_err(dev
, state
, " noirq", error
);
1100 dpm_save_failed_dev(dev_name(dev
));
1104 if (!list_empty(&dev
->power
.entry
))
1105 list_move(&dev
->power
.entry
, &dpm_noirq_list
);
1111 mutex_unlock(&dpm_list_mtx
);
1112 async_synchronize_full();
1114 error
= async_error
;
1117 suspend_stats
.failed_suspend_noirq
++;
1118 dpm_save_failed_step(SUSPEND_SUSPEND_NOIRQ
);
1119 dpm_resume_noirq(resume_event(state
));
1121 dpm_show_time(starttime
, state
, "noirq");
1127 * device_suspend_late - Execute a "late suspend" callback for given device.
1128 * @dev: Device to handle.
1129 * @state: PM transition of the system being carried out.
1131 * Runtime PM is disabled for @dev while this function is being executed.
1133 static int __device_suspend_late(struct device
*dev
, pm_message_t state
, bool async
)
1135 pm_callback_t callback
= NULL
;
1139 __pm_runtime_disable(dev
, false);
1144 if (pm_wakeup_pending()) {
1145 async_error
= -EBUSY
;
1149 if (dev
->power
.syscore
)
1152 dpm_wait_for_children(dev
, async
);
1154 if (dev
->pm_domain
) {
1155 info
= "late power domain ";
1156 callback
= pm_late_early_op(&dev
->pm_domain
->ops
, state
);
1157 } else if (dev
->type
&& dev
->type
->pm
) {
1158 info
= "late type ";
1159 callback
= pm_late_early_op(dev
->type
->pm
, state
);
1160 } else if (dev
->class && dev
->class->pm
) {
1161 info
= "late class ";
1162 callback
= pm_late_early_op(dev
->class->pm
, state
);
1163 } else if (dev
->bus
&& dev
->bus
->pm
) {
1165 callback
= pm_late_early_op(dev
->bus
->pm
, state
);
1168 if (!callback
&& dev
->driver
&& dev
->driver
->pm
) {
1169 info
= "late driver ";
1170 callback
= pm_late_early_op(dev
->driver
->pm
, state
);
1173 error
= dpm_run_callback(callback
, dev
, state
, info
);
1175 dev
->power
.is_late_suspended
= true;
1177 async_error
= error
;
1180 complete_all(&dev
->power
.completion
);
1184 static void async_suspend_late(void *data
, async_cookie_t cookie
)
1186 struct device
*dev
= (struct device
*)data
;
1189 error
= __device_suspend_late(dev
, pm_transition
, true);
1191 dpm_save_failed_dev(dev_name(dev
));
1192 pm_dev_err(dev
, pm_transition
, " async", error
);
1197 static int device_suspend_late(struct device
*dev
)
1199 reinit_completion(&dev
->power
.completion
);
1201 if (pm_async_enabled
&& dev
->power
.async_suspend
) {
1203 async_schedule(async_suspend_late
, dev
);
1207 return __device_suspend_late(dev
, pm_transition
, false);
1211 * dpm_suspend_late - Execute "late suspend" callbacks for all devices.
1212 * @state: PM transition of the system being carried out.
1214 static int dpm_suspend_late(pm_message_t state
)
1216 ktime_t starttime
= ktime_get();
1219 mutex_lock(&dpm_list_mtx
);
1220 pm_transition
= state
;
1223 while (!list_empty(&dpm_suspended_list
)) {
1224 struct device
*dev
= to_device(dpm_suspended_list
.prev
);
1227 mutex_unlock(&dpm_list_mtx
);
1229 error
= device_suspend_late(dev
);
1231 mutex_lock(&dpm_list_mtx
);
1233 pm_dev_err(dev
, state
, " late", error
);
1234 dpm_save_failed_dev(dev_name(dev
));
1238 if (!list_empty(&dev
->power
.entry
))
1239 list_move(&dev
->power
.entry
, &dpm_late_early_list
);
1245 mutex_unlock(&dpm_list_mtx
);
1246 async_synchronize_full();
1248 suspend_stats
.failed_suspend_late
++;
1249 dpm_save_failed_step(SUSPEND_SUSPEND_LATE
);
1250 dpm_resume_early(resume_event(state
));
1252 dpm_show_time(starttime
, state
, "late");
1258 * dpm_suspend_end - Execute "late" and "noirq" device suspend callbacks.
1259 * @state: PM transition of the system being carried out.
1261 int dpm_suspend_end(pm_message_t state
)
1263 int error
= dpm_suspend_late(state
);
1267 error
= dpm_suspend_noirq(state
);
1269 dpm_resume_early(resume_event(state
));
1275 EXPORT_SYMBOL_GPL(dpm_suspend_end
);
1278 * legacy_suspend - Execute a legacy (bus or class) suspend callback for device.
1279 * @dev: Device to suspend.
1280 * @state: PM transition of the system being carried out.
1281 * @cb: Suspend callback to execute.
1283 static int legacy_suspend(struct device
*dev
, pm_message_t state
,
1284 int (*cb
)(struct device
*dev
, pm_message_t state
),
1290 calltime
= initcall_debug_start(dev
);
1292 error
= cb(dev
, state
);
1293 suspend_report_result(cb
, error
);
1295 initcall_debug_report(dev
, calltime
, error
, state
, info
);
1301 * device_suspend - Execute "suspend" callbacks for given device.
1302 * @dev: Device to handle.
1303 * @state: PM transition of the system being carried out.
1304 * @async: If true, the device is being suspended asynchronously.
1306 static int __device_suspend(struct device
*dev
, pm_message_t state
, bool async
)
1308 pm_callback_t callback
= NULL
;
1311 DECLARE_DPM_WATCHDOG_ON_STACK(wd
);
1313 dpm_wait_for_children(dev
, async
);
1319 * If a device configured to wake up the system from sleep states
1320 * has been suspended at run time and there's a resume request pending
1321 * for it, this is equivalent to the device signaling wakeup, so the
1322 * system suspend operation should be aborted.
1324 if (pm_runtime_barrier(dev
) && device_may_wakeup(dev
))
1325 pm_wakeup_event(dev
, 0);
1327 if (pm_wakeup_pending()) {
1328 async_error
= -EBUSY
;
1332 if (dev
->power
.syscore
)
1335 dpm_watchdog_set(&wd
, dev
);
1338 if (dev
->pm_domain
) {
1339 info
= "power domain ";
1340 callback
= pm_op(&dev
->pm_domain
->ops
, state
);
1344 if (dev
->type
&& dev
->type
->pm
) {
1346 callback
= pm_op(dev
->type
->pm
, state
);
1351 if (dev
->class->pm
) {
1353 callback
= pm_op(dev
->class->pm
, state
);
1355 } else if (dev
->class->suspend
) {
1356 pm_dev_dbg(dev
, state
, "legacy class ");
1357 error
= legacy_suspend(dev
, state
, dev
->class->suspend
,
1366 callback
= pm_op(dev
->bus
->pm
, state
);
1367 } else if (dev
->bus
->suspend
) {
1368 pm_dev_dbg(dev
, state
, "legacy bus ");
1369 error
= legacy_suspend(dev
, state
, dev
->bus
->suspend
,
1376 if (!callback
&& dev
->driver
&& dev
->driver
->pm
) {
1378 callback
= pm_op(dev
->driver
->pm
, state
);
1381 error
= dpm_run_callback(callback
, dev
, state
, info
);
1385 dev
->power
.is_suspended
= true;
1386 if (dev
->power
.wakeup_path
1387 && dev
->parent
&& !dev
->parent
->power
.ignore_children
)
1388 dev
->parent
->power
.wakeup_path
= true;
1392 dpm_watchdog_clear(&wd
);
1395 complete_all(&dev
->power
.completion
);
1397 async_error
= error
;
1402 static void async_suspend(void *data
, async_cookie_t cookie
)
1404 struct device
*dev
= (struct device
*)data
;
1407 error
= __device_suspend(dev
, pm_transition
, true);
1409 dpm_save_failed_dev(dev_name(dev
));
1410 pm_dev_err(dev
, pm_transition
, " async", error
);
1416 static int device_suspend(struct device
*dev
)
1418 reinit_completion(&dev
->power
.completion
);
1420 if (pm_async_enabled
&& dev
->power
.async_suspend
) {
1422 async_schedule(async_suspend
, dev
);
1426 return __device_suspend(dev
, pm_transition
, false);
1430 * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices.
1431 * @state: PM transition of the system being carried out.
1433 int dpm_suspend(pm_message_t state
)
1435 ktime_t starttime
= ktime_get();
1442 mutex_lock(&dpm_list_mtx
);
1443 pm_transition
= state
;
1445 while (!list_empty(&dpm_prepared_list
)) {
1446 struct device
*dev
= to_device(dpm_prepared_list
.prev
);
1449 mutex_unlock(&dpm_list_mtx
);
1451 error
= device_suspend(dev
);
1453 mutex_lock(&dpm_list_mtx
);
1455 pm_dev_err(dev
, state
, "", error
);
1456 dpm_save_failed_dev(dev_name(dev
));
1460 if (!list_empty(&dev
->power
.entry
))
1461 list_move(&dev
->power
.entry
, &dpm_suspended_list
);
1466 mutex_unlock(&dpm_list_mtx
);
1467 async_synchronize_full();
1469 error
= async_error
;
1471 suspend_stats
.failed_suspend
++;
1472 dpm_save_failed_step(SUSPEND_SUSPEND
);
1474 dpm_show_time(starttime
, state
, NULL
);
1479 * device_prepare - Prepare a device for system power transition.
1480 * @dev: Device to handle.
1481 * @state: PM transition of the system being carried out.
1483 * Execute the ->prepare() callback(s) for given device. No new children of the
1484 * device may be registered after this function has returned.
1486 static int device_prepare(struct device
*dev
, pm_message_t state
)
1488 int (*callback
)(struct device
*) = NULL
;
1492 if (dev
->power
.syscore
)
1496 * If a device's parent goes into runtime suspend at the wrong time,
1497 * it won't be possible to resume the device. To prevent this we
1498 * block runtime suspend here, during the prepare phase, and allow
1499 * it again during the complete phase.
1501 pm_runtime_get_noresume(dev
);
1505 dev
->power
.wakeup_path
= device_may_wakeup(dev
);
1507 if (dev
->pm_domain
) {
1508 info
= "preparing power domain ";
1509 callback
= dev
->pm_domain
->ops
.prepare
;
1510 } else if (dev
->type
&& dev
->type
->pm
) {
1511 info
= "preparing type ";
1512 callback
= dev
->type
->pm
->prepare
;
1513 } else if (dev
->class && dev
->class->pm
) {
1514 info
= "preparing class ";
1515 callback
= dev
->class->pm
->prepare
;
1516 } else if (dev
->bus
&& dev
->bus
->pm
) {
1517 info
= "preparing bus ";
1518 callback
= dev
->bus
->pm
->prepare
;
1521 if (!callback
&& dev
->driver
&& dev
->driver
->pm
) {
1522 info
= "preparing driver ";
1523 callback
= dev
->driver
->pm
->prepare
;
1527 error
= callback(dev
);
1528 suspend_report_result(callback
, error
);
1534 pm_runtime_put(dev
);
1540 * dpm_prepare - Prepare all non-sysdev devices for a system PM transition.
1541 * @state: PM transition of the system being carried out.
1543 * Execute the ->prepare() callback(s) for all devices.
1545 int dpm_prepare(pm_message_t state
)
1551 mutex_lock(&dpm_list_mtx
);
1552 while (!list_empty(&dpm_list
)) {
1553 struct device
*dev
= to_device(dpm_list
.next
);
1556 mutex_unlock(&dpm_list_mtx
);
1558 error
= device_prepare(dev
, state
);
1560 mutex_lock(&dpm_list_mtx
);
1562 if (error
== -EAGAIN
) {
1567 printk(KERN_INFO
"PM: Device %s not prepared "
1568 "for power transition: code %d\n",
1569 dev_name(dev
), error
);
1573 dev
->power
.is_prepared
= true;
1574 if (!list_empty(&dev
->power
.entry
))
1575 list_move_tail(&dev
->power
.entry
, &dpm_prepared_list
);
1578 mutex_unlock(&dpm_list_mtx
);
1583 * dpm_suspend_start - Prepare devices for PM transition and suspend them.
1584 * @state: PM transition of the system being carried out.
1586 * Prepare all non-sysdev devices for system PM transition and execute "suspend"
1587 * callbacks for them.
1589 int dpm_suspend_start(pm_message_t state
)
1593 error
= dpm_prepare(state
);
1595 suspend_stats
.failed_prepare
++;
1596 dpm_save_failed_step(SUSPEND_PREPARE
);
1598 error
= dpm_suspend(state
);
1601 EXPORT_SYMBOL_GPL(dpm_suspend_start
);
1603 void __suspend_report_result(const char *function
, void *fn
, int ret
)
1606 printk(KERN_ERR
"%s(): %pF returns %d\n", function
, fn
, ret
);
1608 EXPORT_SYMBOL_GPL(__suspend_report_result
);
1611 * device_pm_wait_for_dev - Wait for suspend/resume of a device to complete.
1612 * @dev: Device to wait for.
1613 * @subordinate: Device that needs to wait for @dev.
1615 int device_pm_wait_for_dev(struct device
*subordinate
, struct device
*dev
)
1617 dpm_wait(dev
, subordinate
->power
.async_suspend
);
1620 EXPORT_SYMBOL_GPL(device_pm_wait_for_dev
);
1623 * dpm_for_each_dev - device iterator.
1624 * @data: data for the callback.
1625 * @fn: function to be called for each device.
1627 * Iterate over devices in dpm_list, and call @fn for each device,
1630 void dpm_for_each_dev(void *data
, void (*fn
)(struct device
*, void *))
1638 list_for_each_entry(dev
, &dpm_list
, power
.entry
)
1642 EXPORT_SYMBOL_GPL(dpm_for_each_dev
);