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>
36 * The entries in the dpm_list list are in a depth first order, simply
37 * because children are guaranteed to be discovered after parents, and
38 * are inserted at the back of the list on discovery.
40 * Since device_pm_add() may be called with a device lock held,
41 * we must never try to acquire a device lock while holding
46 LIST_HEAD(dpm_prepared_list
);
47 LIST_HEAD(dpm_suspended_list
);
48 LIST_HEAD(dpm_noirq_list
);
50 struct suspend_stats suspend_stats
;
51 static DEFINE_MUTEX(dpm_list_mtx
);
52 static pm_message_t pm_transition
;
54 static int async_error
;
57 * device_pm_init - Initialize the PM-related part of a device object.
58 * @dev: Device object being initialized.
60 void device_pm_init(struct device
*dev
)
62 dev
->power
.is_prepared
= false;
63 dev
->power
.is_suspended
= false;
64 init_completion(&dev
->power
.completion
);
65 complete_all(&dev
->power
.completion
);
66 dev
->power
.wakeup
= NULL
;
67 spin_lock_init(&dev
->power
.lock
);
69 INIT_LIST_HEAD(&dev
->power
.entry
);
73 * device_pm_lock - Lock the list of active devices used by the PM core.
75 void device_pm_lock(void)
77 mutex_lock(&dpm_list_mtx
);
81 * device_pm_unlock - Unlock the list of active devices used by the PM core.
83 void device_pm_unlock(void)
85 mutex_unlock(&dpm_list_mtx
);
89 * device_pm_add - Add a device to the PM core's list of active devices.
90 * @dev: Device to add to the list.
92 void device_pm_add(struct device
*dev
)
94 pr_debug("PM: Adding info for %s:%s\n",
95 dev
->bus
? dev
->bus
->name
: "No Bus", dev_name(dev
));
96 mutex_lock(&dpm_list_mtx
);
97 if (dev
->parent
&& dev
->parent
->power
.is_prepared
)
98 dev_warn(dev
, "parent %s should not be sleeping\n",
99 dev_name(dev
->parent
));
100 list_add_tail(&dev
->power
.entry
, &dpm_list
);
101 mutex_unlock(&dpm_list_mtx
);
105 * device_pm_remove - Remove a device from the PM core's list of active devices.
106 * @dev: Device to be removed from the list.
108 void device_pm_remove(struct device
*dev
)
110 pr_debug("PM: Removing info for %s:%s\n",
111 dev
->bus
? dev
->bus
->name
: "No Bus", dev_name(dev
));
112 complete_all(&dev
->power
.completion
);
113 mutex_lock(&dpm_list_mtx
);
114 list_del_init(&dev
->power
.entry
);
115 mutex_unlock(&dpm_list_mtx
);
116 device_wakeup_disable(dev
);
117 pm_runtime_remove(dev
);
121 * device_pm_move_before - Move device in the PM core's list of active devices.
122 * @deva: Device to move in dpm_list.
123 * @devb: Device @deva should come before.
125 void device_pm_move_before(struct device
*deva
, struct device
*devb
)
127 pr_debug("PM: Moving %s:%s before %s:%s\n",
128 deva
->bus
? deva
->bus
->name
: "No Bus", dev_name(deva
),
129 devb
->bus
? devb
->bus
->name
: "No Bus", dev_name(devb
));
130 /* Delete deva from dpm_list and reinsert before devb. */
131 list_move_tail(&deva
->power
.entry
, &devb
->power
.entry
);
135 * device_pm_move_after - Move device in the PM core's list of active devices.
136 * @deva: Device to move in dpm_list.
137 * @devb: Device @deva should come after.
139 void device_pm_move_after(struct device
*deva
, struct device
*devb
)
141 pr_debug("PM: Moving %s:%s after %s:%s\n",
142 deva
->bus
? deva
->bus
->name
: "No Bus", dev_name(deva
),
143 devb
->bus
? devb
->bus
->name
: "No Bus", dev_name(devb
));
144 /* Delete deva from dpm_list and reinsert after devb. */
145 list_move(&deva
->power
.entry
, &devb
->power
.entry
);
149 * device_pm_move_last - Move device to end of the PM core's list of devices.
150 * @dev: Device to move in dpm_list.
152 void device_pm_move_last(struct device
*dev
)
154 pr_debug("PM: Moving %s:%s to end of list\n",
155 dev
->bus
? dev
->bus
->name
: "No Bus", dev_name(dev
));
156 list_move_tail(&dev
->power
.entry
, &dpm_list
);
159 static ktime_t
initcall_debug_start(struct device
*dev
)
161 ktime_t calltime
= ktime_set(0, 0);
163 if (initcall_debug
) {
164 pr_info("calling %s+ @ %i\n",
165 dev_name(dev
), task_pid_nr(current
));
166 calltime
= ktime_get();
172 static void initcall_debug_report(struct device
*dev
, ktime_t calltime
,
175 ktime_t delta
, rettime
;
177 if (initcall_debug
) {
178 rettime
= ktime_get();
179 delta
= ktime_sub(rettime
, calltime
);
180 pr_info("call %s+ returned %d after %Ld usecs\n", dev_name(dev
),
181 error
, (unsigned long long)ktime_to_ns(delta
) >> 10);
186 * dpm_wait - Wait for a PM operation to complete.
187 * @dev: Device to wait for.
188 * @async: If unset, wait only if the device's power.async_suspend flag is set.
190 static void dpm_wait(struct device
*dev
, bool async
)
195 if (async
|| (pm_async_enabled
&& dev
->power
.async_suspend
))
196 wait_for_completion(&dev
->power
.completion
);
199 static int dpm_wait_fn(struct device
*dev
, void *async_ptr
)
201 dpm_wait(dev
, *((bool *)async_ptr
));
205 static void dpm_wait_for_children(struct device
*dev
, bool async
)
207 device_for_each_child(dev
, &async
, dpm_wait_fn
);
211 * pm_op - Execute the PM operation appropriate for given PM event.
212 * @dev: Device to handle.
213 * @ops: PM operations to choose from.
214 * @state: PM transition of the system being carried out.
216 static int pm_op(struct device
*dev
,
217 const struct dev_pm_ops
*ops
,
223 calltime
= initcall_debug_start(dev
);
225 switch (state
.event
) {
226 #ifdef CONFIG_SUSPEND
227 case PM_EVENT_SUSPEND
:
229 error
= ops
->suspend(dev
);
230 suspend_report_result(ops
->suspend
, error
);
233 case PM_EVENT_RESUME
:
235 error
= ops
->resume(dev
);
236 suspend_report_result(ops
->resume
, error
);
239 #endif /* CONFIG_SUSPEND */
240 #ifdef CONFIG_HIBERNATE_CALLBACKS
241 case PM_EVENT_FREEZE
:
242 case PM_EVENT_QUIESCE
:
244 error
= ops
->freeze(dev
);
245 suspend_report_result(ops
->freeze
, error
);
248 case PM_EVENT_HIBERNATE
:
250 error
= ops
->poweroff(dev
);
251 suspend_report_result(ops
->poweroff
, error
);
255 case PM_EVENT_RECOVER
:
257 error
= ops
->thaw(dev
);
258 suspend_report_result(ops
->thaw
, error
);
261 case PM_EVENT_RESTORE
:
263 error
= ops
->restore(dev
);
264 suspend_report_result(ops
->restore
, error
);
267 #endif /* CONFIG_HIBERNATE_CALLBACKS */
272 initcall_debug_report(dev
, calltime
, error
);
278 * pm_noirq_op - Execute the PM operation appropriate for given PM event.
279 * @dev: Device to handle.
280 * @ops: PM operations to choose from.
281 * @state: PM transition of the system being carried out.
283 * The driver of @dev will not receive interrupts while this function is being
286 static int pm_noirq_op(struct device
*dev
,
287 const struct dev_pm_ops
*ops
,
291 ktime_t calltime
= ktime_set(0, 0), delta
, rettime
;
293 if (initcall_debug
) {
294 pr_info("calling %s+ @ %i, parent: %s\n",
295 dev_name(dev
), task_pid_nr(current
),
296 dev
->parent
? dev_name(dev
->parent
) : "none");
297 calltime
= ktime_get();
300 switch (state
.event
) {
301 #ifdef CONFIG_SUSPEND
302 case PM_EVENT_SUSPEND
:
303 if (ops
->suspend_noirq
) {
304 error
= ops
->suspend_noirq(dev
);
305 suspend_report_result(ops
->suspend_noirq
, error
);
308 case PM_EVENT_RESUME
:
309 if (ops
->resume_noirq
) {
310 error
= ops
->resume_noirq(dev
);
311 suspend_report_result(ops
->resume_noirq
, error
);
314 #endif /* CONFIG_SUSPEND */
315 #ifdef CONFIG_HIBERNATE_CALLBACKS
316 case PM_EVENT_FREEZE
:
317 case PM_EVENT_QUIESCE
:
318 if (ops
->freeze_noirq
) {
319 error
= ops
->freeze_noirq(dev
);
320 suspend_report_result(ops
->freeze_noirq
, error
);
323 case PM_EVENT_HIBERNATE
:
324 if (ops
->poweroff_noirq
) {
325 error
= ops
->poweroff_noirq(dev
);
326 suspend_report_result(ops
->poweroff_noirq
, error
);
330 case PM_EVENT_RECOVER
:
331 if (ops
->thaw_noirq
) {
332 error
= ops
->thaw_noirq(dev
);
333 suspend_report_result(ops
->thaw_noirq
, error
);
336 case PM_EVENT_RESTORE
:
337 if (ops
->restore_noirq
) {
338 error
= ops
->restore_noirq(dev
);
339 suspend_report_result(ops
->restore_noirq
, error
);
342 #endif /* CONFIG_HIBERNATE_CALLBACKS */
347 if (initcall_debug
) {
348 rettime
= ktime_get();
349 delta
= ktime_sub(rettime
, calltime
);
350 printk("initcall %s_i+ returned %d after %Ld usecs\n",
351 dev_name(dev
), error
,
352 (unsigned long long)ktime_to_ns(delta
) >> 10);
358 static char *pm_verb(int event
)
361 case PM_EVENT_SUSPEND
:
363 case PM_EVENT_RESUME
:
365 case PM_EVENT_FREEZE
:
367 case PM_EVENT_QUIESCE
:
369 case PM_EVENT_HIBERNATE
:
373 case PM_EVENT_RESTORE
:
375 case PM_EVENT_RECOVER
:
378 return "(unknown PM event)";
382 static void pm_dev_dbg(struct device
*dev
, pm_message_t state
, char *info
)
384 dev_dbg(dev
, "%s%s%s\n", info
, pm_verb(state
.event
),
385 ((state
.event
& PM_EVENT_SLEEP
) && device_may_wakeup(dev
)) ?
386 ", may wakeup" : "");
389 static void pm_dev_err(struct device
*dev
, pm_message_t state
, char *info
,
392 printk(KERN_ERR
"PM: Device %s failed to %s%s: error %d\n",
393 dev_name(dev
), pm_verb(state
.event
), info
, error
);
396 static void dpm_show_time(ktime_t starttime
, pm_message_t state
, char *info
)
402 calltime
= ktime_get();
403 usecs64
= ktime_to_ns(ktime_sub(calltime
, starttime
));
404 do_div(usecs64
, NSEC_PER_USEC
);
408 pr_info("PM: %s%s%s of devices complete after %ld.%03ld msecs\n",
409 info
?: "", info
? " " : "", pm_verb(state
.event
),
410 usecs
/ USEC_PER_MSEC
, usecs
% USEC_PER_MSEC
);
413 /*------------------------- Resume routines -------------------------*/
416 * device_resume_noirq - Execute an "early resume" callback for given device.
417 * @dev: Device to handle.
418 * @state: PM transition of the system being carried out.
420 * The driver of @dev will not receive interrupts while this function is being
423 static int device_resume_noirq(struct device
*dev
, pm_message_t state
)
430 if (dev
->pm_domain
) {
431 pm_dev_dbg(dev
, state
, "EARLY power domain ");
432 error
= pm_noirq_op(dev
, &dev
->pm_domain
->ops
, state
);
433 } else if (dev
->type
&& dev
->type
->pm
) {
434 pm_dev_dbg(dev
, state
, "EARLY type ");
435 error
= pm_noirq_op(dev
, dev
->type
->pm
, state
);
436 } else if (dev
->class && dev
->class->pm
) {
437 pm_dev_dbg(dev
, state
, "EARLY class ");
438 error
= pm_noirq_op(dev
, dev
->class->pm
, state
);
439 } else if (dev
->bus
&& dev
->bus
->pm
) {
440 pm_dev_dbg(dev
, state
, "EARLY ");
441 error
= pm_noirq_op(dev
, dev
->bus
->pm
, state
);
449 * dpm_resume_noirq - Execute "early resume" callbacks for non-sysdev devices.
450 * @state: PM transition of the system being carried out.
452 * Call the "noirq" resume handlers for all devices marked as DPM_OFF_IRQ and
453 * enable device drivers to receive interrupts.
455 void dpm_resume_noirq(pm_message_t state
)
457 ktime_t starttime
= ktime_get();
459 mutex_lock(&dpm_list_mtx
);
460 while (!list_empty(&dpm_noirq_list
)) {
461 struct device
*dev
= to_device(dpm_noirq_list
.next
);
465 list_move_tail(&dev
->power
.entry
, &dpm_suspended_list
);
466 mutex_unlock(&dpm_list_mtx
);
468 error
= device_resume_noirq(dev
, state
);
470 suspend_stats
.failed_resume_noirq
++;
471 dpm_save_failed_step(SUSPEND_RESUME_NOIRQ
);
472 dpm_save_failed_dev(dev_name(dev
));
473 pm_dev_err(dev
, state
, " early", error
);
476 mutex_lock(&dpm_list_mtx
);
479 mutex_unlock(&dpm_list_mtx
);
480 dpm_show_time(starttime
, state
, "early");
481 resume_device_irqs();
483 EXPORT_SYMBOL_GPL(dpm_resume_noirq
);
486 * legacy_resume - Execute a legacy (bus or class) resume callback for device.
487 * @dev: Device to resume.
488 * @cb: Resume callback to execute.
490 static int legacy_resume(struct device
*dev
, int (*cb
)(struct device
*dev
))
495 calltime
= initcall_debug_start(dev
);
498 suspend_report_result(cb
, error
);
500 initcall_debug_report(dev
, calltime
, error
);
506 * device_resume - Execute "resume" callbacks for given device.
507 * @dev: Device to handle.
508 * @state: PM transition of the system being carried out.
509 * @async: If true, the device is being resumed asynchronously.
511 static int device_resume(struct device
*dev
, pm_message_t state
, bool async
)
519 dpm_wait(dev
->parent
, async
);
523 * This is a fib. But we'll allow new children to be added below
524 * a resumed device, even if the device hasn't been completed yet.
526 dev
->power
.is_prepared
= false;
528 if (!dev
->power
.is_suspended
)
531 pm_runtime_enable(dev
);
534 if (dev
->pm_domain
) {
535 pm_dev_dbg(dev
, state
, "power domain ");
536 error
= pm_op(dev
, &dev
->pm_domain
->ops
, state
);
540 if (dev
->type
&& dev
->type
->pm
) {
541 pm_dev_dbg(dev
, state
, "type ");
542 error
= pm_op(dev
, dev
->type
->pm
, state
);
547 if (dev
->class->pm
) {
548 pm_dev_dbg(dev
, state
, "class ");
549 error
= pm_op(dev
, dev
->class->pm
, state
);
551 } else if (dev
->class->resume
) {
552 pm_dev_dbg(dev
, state
, "legacy class ");
553 error
= legacy_resume(dev
, dev
->class->resume
);
560 pm_dev_dbg(dev
, state
, "");
561 error
= pm_op(dev
, dev
->bus
->pm
, state
);
562 } else if (dev
->bus
->resume
) {
563 pm_dev_dbg(dev
, state
, "legacy ");
564 error
= legacy_resume(dev
, dev
->bus
->resume
);
569 dev
->power
.is_suspended
= false;
573 complete_all(&dev
->power
.completion
);
578 pm_runtime_put_sync(dev
);
583 static void async_resume(void *data
, async_cookie_t cookie
)
585 struct device
*dev
= (struct device
*)data
;
588 error
= device_resume(dev
, pm_transition
, true);
590 pm_dev_err(dev
, pm_transition
, " async", error
);
594 static bool is_async(struct device
*dev
)
596 return dev
->power
.async_suspend
&& pm_async_enabled
597 && !pm_trace_is_enabled();
601 * dpm_resume - Execute "resume" callbacks for non-sysdev devices.
602 * @state: PM transition of the system being carried out.
604 * Execute the appropriate "resume" callback for all devices whose status
605 * indicates that they are suspended.
607 void dpm_resume(pm_message_t state
)
610 ktime_t starttime
= ktime_get();
614 mutex_lock(&dpm_list_mtx
);
615 pm_transition
= state
;
618 list_for_each_entry(dev
, &dpm_suspended_list
, power
.entry
) {
619 INIT_COMPLETION(dev
->power
.completion
);
622 async_schedule(async_resume
, dev
);
626 while (!list_empty(&dpm_suspended_list
)) {
627 dev
= to_device(dpm_suspended_list
.next
);
629 if (!is_async(dev
)) {
632 mutex_unlock(&dpm_list_mtx
);
634 error
= device_resume(dev
, state
, false);
636 suspend_stats
.failed_resume
++;
637 dpm_save_failed_step(SUSPEND_RESUME
);
638 dpm_save_failed_dev(dev_name(dev
));
639 pm_dev_err(dev
, state
, "", error
);
642 mutex_lock(&dpm_list_mtx
);
644 if (!list_empty(&dev
->power
.entry
))
645 list_move_tail(&dev
->power
.entry
, &dpm_prepared_list
);
648 mutex_unlock(&dpm_list_mtx
);
649 async_synchronize_full();
650 dpm_show_time(starttime
, state
, NULL
);
654 * device_complete - Complete a PM transition for given device.
655 * @dev: Device to handle.
656 * @state: PM transition of the system being carried out.
658 static void device_complete(struct device
*dev
, pm_message_t state
)
662 if (dev
->pm_domain
) {
663 pm_dev_dbg(dev
, state
, "completing power domain ");
664 if (dev
->pm_domain
->ops
.complete
)
665 dev
->pm_domain
->ops
.complete(dev
);
666 } else if (dev
->type
&& dev
->type
->pm
) {
667 pm_dev_dbg(dev
, state
, "completing type ");
668 if (dev
->type
->pm
->complete
)
669 dev
->type
->pm
->complete(dev
);
670 } else if (dev
->class && dev
->class->pm
) {
671 pm_dev_dbg(dev
, state
, "completing class ");
672 if (dev
->class->pm
->complete
)
673 dev
->class->pm
->complete(dev
);
674 } else if (dev
->bus
&& dev
->bus
->pm
) {
675 pm_dev_dbg(dev
, state
, "completing ");
676 if (dev
->bus
->pm
->complete
)
677 dev
->bus
->pm
->complete(dev
);
684 * dpm_complete - Complete a PM transition for all non-sysdev devices.
685 * @state: PM transition of the system being carried out.
687 * Execute the ->complete() callbacks for all devices whose PM status is not
688 * DPM_ON (this allows new devices to be registered).
690 void dpm_complete(pm_message_t state
)
692 struct list_head list
;
696 INIT_LIST_HEAD(&list
);
697 mutex_lock(&dpm_list_mtx
);
698 while (!list_empty(&dpm_prepared_list
)) {
699 struct device
*dev
= to_device(dpm_prepared_list
.prev
);
702 dev
->power
.is_prepared
= false;
703 list_move(&dev
->power
.entry
, &list
);
704 mutex_unlock(&dpm_list_mtx
);
706 device_complete(dev
, state
);
708 mutex_lock(&dpm_list_mtx
);
711 list_splice(&list
, &dpm_list
);
712 mutex_unlock(&dpm_list_mtx
);
716 * dpm_resume_end - Execute "resume" callbacks and complete system transition.
717 * @state: PM transition of the system being carried out.
719 * Execute "resume" callbacks for all devices and complete the PM transition of
722 void dpm_resume_end(pm_message_t state
)
727 EXPORT_SYMBOL_GPL(dpm_resume_end
);
730 /*------------------------- Suspend routines -------------------------*/
733 * resume_event - Return a "resume" message for given "suspend" sleep state.
734 * @sleep_state: PM message representing a sleep state.
736 * Return a PM message representing the resume event corresponding to given
739 static pm_message_t
resume_event(pm_message_t sleep_state
)
741 switch (sleep_state
.event
) {
742 case PM_EVENT_SUSPEND
:
744 case PM_EVENT_FREEZE
:
745 case PM_EVENT_QUIESCE
:
747 case PM_EVENT_HIBERNATE
:
754 * device_suspend_noirq - Execute a "late suspend" callback for given device.
755 * @dev: Device to handle.
756 * @state: PM transition of the system being carried out.
758 * The driver of @dev will not receive interrupts while this function is being
761 static int device_suspend_noirq(struct device
*dev
, pm_message_t state
)
765 if (dev
->pm_domain
) {
766 pm_dev_dbg(dev
, state
, "LATE power domain ");
767 error
= pm_noirq_op(dev
, &dev
->pm_domain
->ops
, state
);
770 } else if (dev
->type
&& dev
->type
->pm
) {
771 pm_dev_dbg(dev
, state
, "LATE type ");
772 error
= pm_noirq_op(dev
, dev
->type
->pm
, state
);
775 } else if (dev
->class && dev
->class->pm
) {
776 pm_dev_dbg(dev
, state
, "LATE class ");
777 error
= pm_noirq_op(dev
, dev
->class->pm
, state
);
780 } else if (dev
->bus
&& dev
->bus
->pm
) {
781 pm_dev_dbg(dev
, state
, "LATE ");
782 error
= pm_noirq_op(dev
, dev
->bus
->pm
, state
);
791 * dpm_suspend_noirq - Execute "late suspend" callbacks for non-sysdev devices.
792 * @state: PM transition of the system being carried out.
794 * Prevent device drivers from receiving interrupts and call the "noirq" suspend
795 * handlers for all non-sysdev devices.
797 int dpm_suspend_noirq(pm_message_t state
)
799 ktime_t starttime
= ktime_get();
802 suspend_device_irqs();
803 mutex_lock(&dpm_list_mtx
);
804 while (!list_empty(&dpm_suspended_list
)) {
805 struct device
*dev
= to_device(dpm_suspended_list
.prev
);
808 mutex_unlock(&dpm_list_mtx
);
810 error
= device_suspend_noirq(dev
, state
);
812 mutex_lock(&dpm_list_mtx
);
814 pm_dev_err(dev
, state
, " late", error
);
815 suspend_stats
.failed_suspend_noirq
++;
816 dpm_save_failed_step(SUSPEND_SUSPEND_NOIRQ
);
817 dpm_save_failed_dev(dev_name(dev
));
821 if (!list_empty(&dev
->power
.entry
))
822 list_move(&dev
->power
.entry
, &dpm_noirq_list
);
825 mutex_unlock(&dpm_list_mtx
);
827 dpm_resume_noirq(resume_event(state
));
829 dpm_show_time(starttime
, state
, "late");
832 EXPORT_SYMBOL_GPL(dpm_suspend_noirq
);
835 * legacy_suspend - Execute a legacy (bus or class) suspend callback for device.
836 * @dev: Device to suspend.
837 * @state: PM transition of the system being carried out.
838 * @cb: Suspend callback to execute.
840 static int legacy_suspend(struct device
*dev
, pm_message_t state
,
841 int (*cb
)(struct device
*dev
, pm_message_t state
))
846 calltime
= initcall_debug_start(dev
);
848 error
= cb(dev
, state
);
849 suspend_report_result(cb
, error
);
851 initcall_debug_report(dev
, calltime
, error
);
857 * device_suspend - Execute "suspend" callbacks for given device.
858 * @dev: Device to handle.
859 * @state: PM transition of the system being carried out.
860 * @async: If true, the device is being suspended asynchronously.
862 static int __device_suspend(struct device
*dev
, pm_message_t state
, bool async
)
866 dpm_wait_for_children(dev
, async
);
871 pm_runtime_get_noresume(dev
);
872 if (pm_runtime_barrier(dev
) && device_may_wakeup(dev
))
873 pm_wakeup_event(dev
, 0);
875 if (pm_wakeup_pending()) {
876 pm_runtime_put_sync(dev
);
877 async_error
= -EBUSY
;
883 if (dev
->pm_domain
) {
884 pm_dev_dbg(dev
, state
, "power domain ");
885 error
= pm_op(dev
, &dev
->pm_domain
->ops
, state
);
889 if (dev
->type
&& dev
->type
->pm
) {
890 pm_dev_dbg(dev
, state
, "type ");
891 error
= pm_op(dev
, dev
->type
->pm
, state
);
896 if (dev
->class->pm
) {
897 pm_dev_dbg(dev
, state
, "class ");
898 error
= pm_op(dev
, dev
->class->pm
, state
);
900 } else if (dev
->class->suspend
) {
901 pm_dev_dbg(dev
, state
, "legacy class ");
902 error
= legacy_suspend(dev
, state
, dev
->class->suspend
);
909 pm_dev_dbg(dev
, state
, "");
910 error
= pm_op(dev
, dev
->bus
->pm
, state
);
911 } else if (dev
->bus
->suspend
) {
912 pm_dev_dbg(dev
, state
, "legacy ");
913 error
= legacy_suspend(dev
, state
, dev
->bus
->suspend
);
918 dev
->power
.is_suspended
= !error
;
921 complete_all(&dev
->power
.completion
);
924 pm_runtime_put_sync(dev
);
926 } else if (dev
->power
.is_suspended
) {
927 __pm_runtime_disable(dev
, false);
933 static void async_suspend(void *data
, async_cookie_t cookie
)
935 struct device
*dev
= (struct device
*)data
;
938 error
= __device_suspend(dev
, pm_transition
, true);
940 dpm_save_failed_dev(dev_name(dev
));
941 pm_dev_err(dev
, pm_transition
, " async", error
);
947 static int device_suspend(struct device
*dev
)
949 INIT_COMPLETION(dev
->power
.completion
);
951 if (pm_async_enabled
&& dev
->power
.async_suspend
) {
953 async_schedule(async_suspend
, dev
);
957 return __device_suspend(dev
, pm_transition
, false);
961 * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices.
962 * @state: PM transition of the system being carried out.
964 int dpm_suspend(pm_message_t state
)
966 ktime_t starttime
= ktime_get();
971 mutex_lock(&dpm_list_mtx
);
972 pm_transition
= state
;
974 while (!list_empty(&dpm_prepared_list
)) {
975 struct device
*dev
= to_device(dpm_prepared_list
.prev
);
978 mutex_unlock(&dpm_list_mtx
);
980 error
= device_suspend(dev
);
982 mutex_lock(&dpm_list_mtx
);
984 pm_dev_err(dev
, state
, "", error
);
985 dpm_save_failed_dev(dev_name(dev
));
989 if (!list_empty(&dev
->power
.entry
))
990 list_move(&dev
->power
.entry
, &dpm_suspended_list
);
995 mutex_unlock(&dpm_list_mtx
);
996 async_synchronize_full();
1000 suspend_stats
.failed_suspend
++;
1001 dpm_save_failed_step(SUSPEND_SUSPEND
);
1003 dpm_show_time(starttime
, state
, NULL
);
1008 * device_prepare - Prepare a device for system power transition.
1009 * @dev: Device to handle.
1010 * @state: PM transition of the system being carried out.
1012 * Execute the ->prepare() callback(s) for given device. No new children of the
1013 * device may be registered after this function has returned.
1015 static int device_prepare(struct device
*dev
, pm_message_t state
)
1021 if (dev
->pm_domain
) {
1022 pm_dev_dbg(dev
, state
, "preparing power domain ");
1023 if (dev
->pm_domain
->ops
.prepare
)
1024 error
= dev
->pm_domain
->ops
.prepare(dev
);
1025 suspend_report_result(dev
->pm_domain
->ops
.prepare
, error
);
1028 } else if (dev
->type
&& dev
->type
->pm
) {
1029 pm_dev_dbg(dev
, state
, "preparing type ");
1030 if (dev
->type
->pm
->prepare
)
1031 error
= dev
->type
->pm
->prepare(dev
);
1032 suspend_report_result(dev
->type
->pm
->prepare
, error
);
1035 } else if (dev
->class && dev
->class->pm
) {
1036 pm_dev_dbg(dev
, state
, "preparing class ");
1037 if (dev
->class->pm
->prepare
)
1038 error
= dev
->class->pm
->prepare(dev
);
1039 suspend_report_result(dev
->class->pm
->prepare
, error
);
1042 } else if (dev
->bus
&& dev
->bus
->pm
) {
1043 pm_dev_dbg(dev
, state
, "preparing ");
1044 if (dev
->bus
->pm
->prepare
)
1045 error
= dev
->bus
->pm
->prepare(dev
);
1046 suspend_report_result(dev
->bus
->pm
->prepare
, error
);
1056 * dpm_prepare - Prepare all non-sysdev devices for a system PM transition.
1057 * @state: PM transition of the system being carried out.
1059 * Execute the ->prepare() callback(s) for all devices.
1061 int dpm_prepare(pm_message_t state
)
1067 mutex_lock(&dpm_list_mtx
);
1068 while (!list_empty(&dpm_list
)) {
1069 struct device
*dev
= to_device(dpm_list
.next
);
1072 mutex_unlock(&dpm_list_mtx
);
1074 error
= device_prepare(dev
, state
);
1076 mutex_lock(&dpm_list_mtx
);
1078 if (error
== -EAGAIN
) {
1083 printk(KERN_INFO
"PM: Device %s not prepared "
1084 "for power transition: code %d\n",
1085 dev_name(dev
), error
);
1089 dev
->power
.is_prepared
= true;
1090 if (!list_empty(&dev
->power
.entry
))
1091 list_move_tail(&dev
->power
.entry
, &dpm_prepared_list
);
1094 mutex_unlock(&dpm_list_mtx
);
1099 * dpm_suspend_start - Prepare devices for PM transition and suspend them.
1100 * @state: PM transition of the system being carried out.
1102 * Prepare all non-sysdev devices for system PM transition and execute "suspend"
1103 * callbacks for them.
1105 int dpm_suspend_start(pm_message_t state
)
1109 error
= dpm_prepare(state
);
1111 suspend_stats
.failed_prepare
++;
1112 dpm_save_failed_step(SUSPEND_PREPARE
);
1114 error
= dpm_suspend(state
);
1117 EXPORT_SYMBOL_GPL(dpm_suspend_start
);
1119 void __suspend_report_result(const char *function
, void *fn
, int ret
)
1122 printk(KERN_ERR
"%s(): %pF returns %d\n", function
, fn
, ret
);
1124 EXPORT_SYMBOL_GPL(__suspend_report_result
);
1127 * device_pm_wait_for_dev - Wait for suspend/resume of a device to complete.
1128 * @dev: Device to wait for.
1129 * @subordinate: Device that needs to wait for @dev.
1131 int device_pm_wait_for_dev(struct device
*subordinate
, struct device
*dev
)
1133 dpm_wait(dev
, subordinate
->power
.async_suspend
);
1136 EXPORT_SYMBOL_GPL(device_pm_wait_for_dev
);