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/export.h>
22 #include <linux/mutex.h>
24 #include <linux/pm_runtime.h>
25 #include <linux/pm-trace.h>
26 #include <linux/pm_wakeirq.h>
27 #include <linux/interrupt.h>
28 #include <linux/sched.h>
29 #include <linux/sched/debug.h>
30 #include <linux/async.h>
31 #include <linux/suspend.h>
32 #include <trace/events/power.h>
33 #include <linux/cpufreq.h>
34 #include <linux/cpuidle.h>
35 #include <linux/timer.h>
40 typedef int (*pm_callback_t
)(struct device
*);
43 * The entries in the dpm_list list are in a depth first order, simply
44 * because children are guaranteed to be discovered after parents, and
45 * are inserted at the back of the list on discovery.
47 * Since device_pm_add() may be called with a device lock held,
48 * we must never try to acquire a device lock while holding
53 static LIST_HEAD(dpm_prepared_list
);
54 static LIST_HEAD(dpm_suspended_list
);
55 static LIST_HEAD(dpm_late_early_list
);
56 static LIST_HEAD(dpm_noirq_list
);
58 struct suspend_stats suspend_stats
;
59 static DEFINE_MUTEX(dpm_list_mtx
);
60 static pm_message_t pm_transition
;
62 static int async_error
;
64 static const char *pm_verb(int event
)
67 case PM_EVENT_SUSPEND
:
73 case PM_EVENT_QUIESCE
:
75 case PM_EVENT_HIBERNATE
:
79 case PM_EVENT_RESTORE
:
81 case PM_EVENT_RECOVER
:
84 return "(unknown PM event)";
89 * device_pm_sleep_init - Initialize system suspend-related device fields.
90 * @dev: Device object being initialized.
92 void device_pm_sleep_init(struct device
*dev
)
94 dev
->power
.is_prepared
= false;
95 dev
->power
.is_suspended
= false;
96 dev
->power
.is_noirq_suspended
= false;
97 dev
->power
.is_late_suspended
= false;
98 init_completion(&dev
->power
.completion
);
99 complete_all(&dev
->power
.completion
);
100 dev
->power
.wakeup
= NULL
;
101 INIT_LIST_HEAD(&dev
->power
.entry
);
105 * device_pm_lock - Lock the list of active devices used by the PM core.
107 void device_pm_lock(void)
109 mutex_lock(&dpm_list_mtx
);
113 * device_pm_unlock - Unlock the list of active devices used by the PM core.
115 void device_pm_unlock(void)
117 mutex_unlock(&dpm_list_mtx
);
121 * device_pm_add - Add a device to the PM core's list of active devices.
122 * @dev: Device to add to the list.
124 void device_pm_add(struct device
*dev
)
126 pr_debug("PM: Adding info for %s:%s\n",
127 dev
->bus
? dev
->bus
->name
: "No Bus", dev_name(dev
));
128 device_pm_check_callbacks(dev
);
129 mutex_lock(&dpm_list_mtx
);
130 if (dev
->parent
&& dev
->parent
->power
.is_prepared
)
131 dev_warn(dev
, "parent %s should not be sleeping\n",
132 dev_name(dev
->parent
));
133 list_add_tail(&dev
->power
.entry
, &dpm_list
);
134 dev
->power
.in_dpm_list
= true;
135 mutex_unlock(&dpm_list_mtx
);
139 * device_pm_remove - Remove a device from the PM core's list of active devices.
140 * @dev: Device to be removed from the list.
142 void device_pm_remove(struct device
*dev
)
144 pr_debug("PM: Removing info for %s:%s\n",
145 dev
->bus
? dev
->bus
->name
: "No Bus", dev_name(dev
));
146 complete_all(&dev
->power
.completion
);
147 mutex_lock(&dpm_list_mtx
);
148 list_del_init(&dev
->power
.entry
);
149 dev
->power
.in_dpm_list
= false;
150 mutex_unlock(&dpm_list_mtx
);
151 device_wakeup_disable(dev
);
152 pm_runtime_remove(dev
);
153 device_pm_check_callbacks(dev
);
157 * device_pm_move_before - Move device in the PM core's list of active devices.
158 * @deva: Device to move in dpm_list.
159 * @devb: Device @deva should come before.
161 void device_pm_move_before(struct device
*deva
, struct device
*devb
)
163 pr_debug("PM: Moving %s:%s before %s:%s\n",
164 deva
->bus
? deva
->bus
->name
: "No Bus", dev_name(deva
),
165 devb
->bus
? devb
->bus
->name
: "No Bus", dev_name(devb
));
166 /* Delete deva from dpm_list and reinsert before devb. */
167 list_move_tail(&deva
->power
.entry
, &devb
->power
.entry
);
171 * device_pm_move_after - Move device in the PM core's list of active devices.
172 * @deva: Device to move in dpm_list.
173 * @devb: Device @deva should come after.
175 void device_pm_move_after(struct device
*deva
, struct device
*devb
)
177 pr_debug("PM: Moving %s:%s after %s:%s\n",
178 deva
->bus
? deva
->bus
->name
: "No Bus", dev_name(deva
),
179 devb
->bus
? devb
->bus
->name
: "No Bus", dev_name(devb
));
180 /* Delete deva from dpm_list and reinsert after devb. */
181 list_move(&deva
->power
.entry
, &devb
->power
.entry
);
185 * device_pm_move_last - Move device to end of the PM core's list of devices.
186 * @dev: Device to move in dpm_list.
188 void device_pm_move_last(struct device
*dev
)
190 pr_debug("PM: Moving %s:%s to end of list\n",
191 dev
->bus
? dev
->bus
->name
: "No Bus", dev_name(dev
));
192 list_move_tail(&dev
->power
.entry
, &dpm_list
);
195 static ktime_t
initcall_debug_start(struct device
*dev
, void *cb
)
197 if (!pm_print_times_enabled
)
200 dev_info(dev
, "calling %pF @ %i, parent: %s\n", cb
,
201 task_pid_nr(current
),
202 dev
->parent
? dev_name(dev
->parent
) : "none");
206 static void initcall_debug_report(struct device
*dev
, ktime_t calltime
,
212 if (!pm_print_times_enabled
)
215 rettime
= ktime_get();
216 nsecs
= (s64
) ktime_to_ns(ktime_sub(rettime
, calltime
));
218 dev_info(dev
, "%pF returned %d after %Ld usecs\n", cb
, error
,
219 (unsigned long long)nsecs
>> 10);
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
);
247 static void dpm_wait_for_suppliers(struct device
*dev
, bool async
)
249 struct device_link
*link
;
252 idx
= device_links_read_lock();
255 * If the supplier goes away right after we've checked the link to it,
256 * we'll wait for its completion to change the state, but that's fine,
257 * because the only things that will block as a result are the SRCU
258 * callbacks freeing the link objects for the links in the list we're
261 list_for_each_entry_rcu(link
, &dev
->links
.suppliers
, c_node
)
262 if (READ_ONCE(link
->status
) != DL_STATE_DORMANT
)
263 dpm_wait(link
->supplier
, async
);
265 device_links_read_unlock(idx
);
268 static bool dpm_wait_for_superior(struct device
*dev
, bool async
)
270 struct device
*parent
;
273 * If the device is resumed asynchronously and the parent's callback
274 * deletes both the device and the parent itself, the parent object may
275 * be freed while this function is running, so avoid that by reference
276 * counting the parent once more unless the device has been deleted
277 * already (in which case return right away).
279 mutex_lock(&dpm_list_mtx
);
281 if (!device_pm_initialized(dev
)) {
282 mutex_unlock(&dpm_list_mtx
);
286 parent
= get_device(dev
->parent
);
288 mutex_unlock(&dpm_list_mtx
);
290 dpm_wait(parent
, async
);
293 dpm_wait_for_suppliers(dev
, async
);
296 * If the parent's callback has deleted the device, attempting to resume
297 * it would be invalid, so avoid doing that then.
299 return device_pm_initialized(dev
);
302 static void dpm_wait_for_consumers(struct device
*dev
, bool async
)
304 struct device_link
*link
;
307 idx
= device_links_read_lock();
310 * The status of a device link can only be changed from "dormant" by a
311 * probe, but that cannot happen during system suspend/resume. In
312 * theory it can change to "dormant" at that time, but then it is
313 * reasonable to wait for the target device anyway (eg. if it goes
314 * away, it's better to wait for it to go away completely and then
315 * continue instead of trying to continue in parallel with its
318 list_for_each_entry_rcu(link
, &dev
->links
.consumers
, s_node
)
319 if (READ_ONCE(link
->status
) != DL_STATE_DORMANT
)
320 dpm_wait(link
->consumer
, async
);
322 device_links_read_unlock(idx
);
325 static void dpm_wait_for_subordinate(struct device
*dev
, bool async
)
327 dpm_wait_for_children(dev
, async
);
328 dpm_wait_for_consumers(dev
, async
);
332 * pm_op - Return the PM operation appropriate for given PM event.
333 * @ops: PM operations to choose from.
334 * @state: PM transition of the system being carried out.
336 static pm_callback_t
pm_op(const struct dev_pm_ops
*ops
, pm_message_t state
)
338 switch (state
.event
) {
339 #ifdef CONFIG_SUSPEND
340 case PM_EVENT_SUSPEND
:
342 case PM_EVENT_RESUME
:
344 #endif /* CONFIG_SUSPEND */
345 #ifdef CONFIG_HIBERNATE_CALLBACKS
346 case PM_EVENT_FREEZE
:
347 case PM_EVENT_QUIESCE
:
349 case PM_EVENT_HIBERNATE
:
350 return ops
->poweroff
;
352 case PM_EVENT_RECOVER
:
355 case PM_EVENT_RESTORE
:
357 #endif /* CONFIG_HIBERNATE_CALLBACKS */
364 * pm_late_early_op - Return the PM operation appropriate for given PM event.
365 * @ops: PM operations to choose from.
366 * @state: PM transition of the system being carried out.
368 * Runtime PM is disabled for @dev while this function is being executed.
370 static pm_callback_t
pm_late_early_op(const struct dev_pm_ops
*ops
,
373 switch (state
.event
) {
374 #ifdef CONFIG_SUSPEND
375 case PM_EVENT_SUSPEND
:
376 return ops
->suspend_late
;
377 case PM_EVENT_RESUME
:
378 return ops
->resume_early
;
379 #endif /* CONFIG_SUSPEND */
380 #ifdef CONFIG_HIBERNATE_CALLBACKS
381 case PM_EVENT_FREEZE
:
382 case PM_EVENT_QUIESCE
:
383 return ops
->freeze_late
;
384 case PM_EVENT_HIBERNATE
:
385 return ops
->poweroff_late
;
387 case PM_EVENT_RECOVER
:
388 return ops
->thaw_early
;
389 case PM_EVENT_RESTORE
:
390 return ops
->restore_early
;
391 #endif /* CONFIG_HIBERNATE_CALLBACKS */
398 * pm_noirq_op - Return the PM operation appropriate for given PM event.
399 * @ops: PM operations to choose from.
400 * @state: PM transition of the system being carried out.
402 * The driver of @dev will not receive interrupts while this function is being
405 static pm_callback_t
pm_noirq_op(const struct dev_pm_ops
*ops
, pm_message_t state
)
407 switch (state
.event
) {
408 #ifdef CONFIG_SUSPEND
409 case PM_EVENT_SUSPEND
:
410 return ops
->suspend_noirq
;
411 case PM_EVENT_RESUME
:
412 return ops
->resume_noirq
;
413 #endif /* CONFIG_SUSPEND */
414 #ifdef CONFIG_HIBERNATE_CALLBACKS
415 case PM_EVENT_FREEZE
:
416 case PM_EVENT_QUIESCE
:
417 return ops
->freeze_noirq
;
418 case PM_EVENT_HIBERNATE
:
419 return ops
->poweroff_noirq
;
421 case PM_EVENT_RECOVER
:
422 return ops
->thaw_noirq
;
423 case PM_EVENT_RESTORE
:
424 return ops
->restore_noirq
;
425 #endif /* CONFIG_HIBERNATE_CALLBACKS */
431 static void pm_dev_dbg(struct device
*dev
, pm_message_t state
, const char *info
)
433 dev_dbg(dev
, "%s%s%s\n", info
, pm_verb(state
.event
),
434 ((state
.event
& PM_EVENT_SLEEP
) && device_may_wakeup(dev
)) ?
435 ", may wakeup" : "");
438 static void pm_dev_err(struct device
*dev
, pm_message_t state
, const char *info
,
441 printk(KERN_ERR
"PM: Device %s failed to %s%s: error %d\n",
442 dev_name(dev
), pm_verb(state
.event
), info
, error
);
445 static void dpm_show_time(ktime_t starttime
, pm_message_t state
, int error
,
452 calltime
= ktime_get();
453 usecs64
= ktime_to_ns(ktime_sub(calltime
, starttime
));
454 do_div(usecs64
, NSEC_PER_USEC
);
459 pm_pr_dbg("%s%s%s of devices %s after %ld.%03ld msecs\n",
460 info
?: "", info
? " " : "", pm_verb(state
.event
),
461 error
? "aborted" : "complete",
462 usecs
/ USEC_PER_MSEC
, usecs
% USEC_PER_MSEC
);
465 static int dpm_run_callback(pm_callback_t cb
, struct device
*dev
,
466 pm_message_t state
, const char *info
)
474 calltime
= initcall_debug_start(dev
, cb
);
476 pm_dev_dbg(dev
, state
, info
);
477 trace_device_pm_callback_start(dev
, info
, state
.event
);
479 trace_device_pm_callback_end(dev
, error
);
480 suspend_report_result(cb
, error
);
482 initcall_debug_report(dev
, calltime
, cb
, error
);
487 #ifdef CONFIG_DPM_WATCHDOG
488 struct dpm_watchdog
{
490 struct task_struct
*tsk
;
491 struct timer_list timer
;
494 #define DECLARE_DPM_WATCHDOG_ON_STACK(wd) \
495 struct dpm_watchdog wd
498 * dpm_watchdog_handler - Driver suspend / resume watchdog handler.
499 * @data: Watchdog object address.
501 * Called when a driver has timed out suspending or resuming.
502 * There's not much we can do here to recover so panic() to
503 * capture a crash-dump in pstore.
505 static void dpm_watchdog_handler(struct timer_list
*t
)
507 struct dpm_watchdog
*wd
= from_timer(wd
, t
, timer
);
509 dev_emerg(wd
->dev
, "**** DPM device timeout ****\n");
510 show_stack(wd
->tsk
, NULL
);
511 panic("%s %s: unrecoverable failure\n",
512 dev_driver_string(wd
->dev
), dev_name(wd
->dev
));
516 * dpm_watchdog_set - Enable pm watchdog for given device.
517 * @wd: Watchdog. Must be allocated on the stack.
518 * @dev: Device to handle.
520 static void dpm_watchdog_set(struct dpm_watchdog
*wd
, struct device
*dev
)
522 struct timer_list
*timer
= &wd
->timer
;
527 timer_setup_on_stack(timer
, dpm_watchdog_handler
, 0);
528 /* use same timeout value for both suspend and resume */
529 timer
->expires
= jiffies
+ HZ
* CONFIG_DPM_WATCHDOG_TIMEOUT
;
534 * dpm_watchdog_clear - Disable suspend/resume watchdog.
535 * @wd: Watchdog to disable.
537 static void dpm_watchdog_clear(struct dpm_watchdog
*wd
)
539 struct timer_list
*timer
= &wd
->timer
;
541 del_timer_sync(timer
);
542 destroy_timer_on_stack(timer
);
545 #define DECLARE_DPM_WATCHDOG_ON_STACK(wd)
546 #define dpm_watchdog_set(x, y)
547 #define dpm_watchdog_clear(x)
550 /*------------------------- Resume routines -------------------------*/
553 * dev_pm_skip_next_resume_phases - Skip next system resume phases for device.
554 * @dev: Target device.
556 * Make the core skip the "early resume" and "resume" phases for @dev.
558 * This function can be called by middle-layer code during the "noirq" phase of
559 * system resume if necessary, but not by device drivers.
561 void dev_pm_skip_next_resume_phases(struct device
*dev
)
563 dev
->power
.is_late_suspended
= false;
564 dev
->power
.is_suspended
= false;
568 * suspend_event - Return a "suspend" message for given "resume" one.
569 * @resume_msg: PM message representing a system-wide resume transition.
571 static pm_message_t
suspend_event(pm_message_t resume_msg
)
573 switch (resume_msg
.event
) {
574 case PM_EVENT_RESUME
:
577 case PM_EVENT_RESTORE
:
579 case PM_EVENT_RECOVER
:
580 return PMSG_HIBERNATE
;
586 * dev_pm_may_skip_resume - System-wide device resume optimization check.
587 * @dev: Target device.
589 * Checks whether or not the device may be left in suspend after a system-wide
590 * transition to the working state.
592 bool dev_pm_may_skip_resume(struct device
*dev
)
594 return !dev
->power
.must_resume
&& pm_transition
.event
!= PM_EVENT_RESTORE
;
597 static pm_callback_t
dpm_subsys_resume_noirq_cb(struct device
*dev
,
601 pm_callback_t callback
;
604 if (dev
->pm_domain
) {
605 info
= "noirq power domain ";
606 callback
= pm_noirq_op(&dev
->pm_domain
->ops
, state
);
607 } else if (dev
->type
&& dev
->type
->pm
) {
608 info
= "noirq type ";
609 callback
= pm_noirq_op(dev
->type
->pm
, state
);
610 } else if (dev
->class && dev
->class->pm
) {
611 info
= "noirq class ";
612 callback
= pm_noirq_op(dev
->class->pm
, state
);
613 } else if (dev
->bus
&& dev
->bus
->pm
) {
615 callback
= pm_noirq_op(dev
->bus
->pm
, state
);
626 static pm_callback_t
dpm_subsys_suspend_noirq_cb(struct device
*dev
,
628 const char **info_p
);
630 static pm_callback_t
dpm_subsys_suspend_late_cb(struct device
*dev
,
632 const char **info_p
);
635 * device_resume_noirq - Execute a "noirq resume" callback for given device.
636 * @dev: Device to handle.
637 * @state: PM transition of the system being carried out.
638 * @async: If true, the device is being resumed asynchronously.
640 * The driver of @dev will not receive interrupts while this function is being
643 static int device_resume_noirq(struct device
*dev
, pm_message_t state
, bool async
)
645 pm_callback_t callback
;
653 if (dev
->power
.syscore
|| dev
->power
.direct_complete
)
656 if (!dev
->power
.is_noirq_suspended
)
659 if (!dpm_wait_for_superior(dev
, async
))
662 skip_resume
= dev_pm_may_skip_resume(dev
);
664 callback
= dpm_subsys_resume_noirq_cb(dev
, state
, &info
);
671 if (dev_pm_smart_suspend_and_suspended(dev
)) {
672 pm_message_t suspend_msg
= suspend_event(state
);
675 * If "freeze" callbacks have been skipped during a transition
676 * related to hibernation, the subsequent "thaw" callbacks must
677 * be skipped too or bad things may happen. Otherwise, resume
678 * callbacks are going to be run for the device, so its runtime
679 * PM status must be changed to reflect the new state after the
680 * transition under way.
682 if (!dpm_subsys_suspend_late_cb(dev
, suspend_msg
, NULL
) &&
683 !dpm_subsys_suspend_noirq_cb(dev
, suspend_msg
, NULL
)) {
684 if (state
.event
== PM_EVENT_THAW
) {
688 pm_runtime_set_active(dev
);
693 if (dev
->driver
&& dev
->driver
->pm
) {
694 info
= "noirq driver ";
695 callback
= pm_noirq_op(dev
->driver
->pm
, state
);
699 error
= dpm_run_callback(callback
, dev
, state
, info
);
702 dev
->power
.is_noirq_suspended
= false;
706 * The device is going to be left in suspend, but it might not
707 * have been in runtime suspend before the system suspended, so
708 * its runtime PM status needs to be updated to avoid confusing
709 * the runtime PM framework when runtime PM is enabled for the
712 pm_runtime_set_suspended(dev
);
713 dev_pm_skip_next_resume_phases(dev
);
717 complete_all(&dev
->power
.completion
);
722 static bool is_async(struct device
*dev
)
724 return dev
->power
.async_suspend
&& pm_async_enabled
725 && !pm_trace_is_enabled();
728 static void async_resume_noirq(void *data
, async_cookie_t cookie
)
730 struct device
*dev
= (struct device
*)data
;
733 error
= device_resume_noirq(dev
, pm_transition
, true);
735 pm_dev_err(dev
, pm_transition
, " async", error
);
740 void dpm_noirq_resume_devices(pm_message_t state
)
743 ktime_t starttime
= ktime_get();
745 trace_suspend_resume(TPS("dpm_resume_noirq"), state
.event
, true);
746 mutex_lock(&dpm_list_mtx
);
747 pm_transition
= state
;
750 * Advanced the async threads upfront,
751 * in case the starting of async threads is
752 * delayed by non-async resuming devices.
754 list_for_each_entry(dev
, &dpm_noirq_list
, power
.entry
) {
755 reinit_completion(&dev
->power
.completion
);
758 async_schedule(async_resume_noirq
, dev
);
762 while (!list_empty(&dpm_noirq_list
)) {
763 dev
= to_device(dpm_noirq_list
.next
);
765 list_move_tail(&dev
->power
.entry
, &dpm_late_early_list
);
766 mutex_unlock(&dpm_list_mtx
);
768 if (!is_async(dev
)) {
771 error
= device_resume_noirq(dev
, state
, false);
773 suspend_stats
.failed_resume_noirq
++;
774 dpm_save_failed_step(SUSPEND_RESUME_NOIRQ
);
775 dpm_save_failed_dev(dev_name(dev
));
776 pm_dev_err(dev
, state
, " noirq", error
);
780 mutex_lock(&dpm_list_mtx
);
783 mutex_unlock(&dpm_list_mtx
);
784 async_synchronize_full();
785 dpm_show_time(starttime
, state
, 0, "noirq");
786 trace_suspend_resume(TPS("dpm_resume_noirq"), state
.event
, false);
789 void dpm_noirq_end(void)
791 resume_device_irqs();
792 device_wakeup_disarm_wake_irqs();
797 * dpm_resume_noirq - Execute "noirq resume" callbacks for all devices.
798 * @state: PM transition of the system being carried out.
800 * Invoke the "noirq" resume callbacks for all devices in dpm_noirq_list and
801 * allow device drivers' interrupt handlers to be called.
803 void dpm_resume_noirq(pm_message_t state
)
805 dpm_noirq_resume_devices(state
);
809 static pm_callback_t
dpm_subsys_resume_early_cb(struct device
*dev
,
813 pm_callback_t callback
;
816 if (dev
->pm_domain
) {
817 info
= "early power domain ";
818 callback
= pm_late_early_op(&dev
->pm_domain
->ops
, state
);
819 } else if (dev
->type
&& dev
->type
->pm
) {
820 info
= "early type ";
821 callback
= pm_late_early_op(dev
->type
->pm
, state
);
822 } else if (dev
->class && dev
->class->pm
) {
823 info
= "early class ";
824 callback
= pm_late_early_op(dev
->class->pm
, state
);
825 } else if (dev
->bus
&& dev
->bus
->pm
) {
827 callback
= pm_late_early_op(dev
->bus
->pm
, state
);
839 * device_resume_early - Execute an "early resume" callback for given device.
840 * @dev: Device to handle.
841 * @state: PM transition of the system being carried out.
842 * @async: If true, the device is being resumed asynchronously.
844 * Runtime PM is disabled for @dev while this function is being executed.
846 static int device_resume_early(struct device
*dev
, pm_message_t state
, bool async
)
848 pm_callback_t callback
;
855 if (dev
->power
.syscore
|| dev
->power
.direct_complete
)
858 if (!dev
->power
.is_late_suspended
)
861 if (!dpm_wait_for_superior(dev
, async
))
864 callback
= dpm_subsys_resume_early_cb(dev
, state
, &info
);
866 if (!callback
&& dev
->driver
&& dev
->driver
->pm
) {
867 info
= "early driver ";
868 callback
= pm_late_early_op(dev
->driver
->pm
, state
);
871 error
= dpm_run_callback(callback
, dev
, state
, info
);
872 dev
->power
.is_late_suspended
= false;
877 pm_runtime_enable(dev
);
878 complete_all(&dev
->power
.completion
);
882 static void async_resume_early(void *data
, async_cookie_t cookie
)
884 struct device
*dev
= (struct device
*)data
;
887 error
= device_resume_early(dev
, pm_transition
, true);
889 pm_dev_err(dev
, pm_transition
, " async", error
);
895 * dpm_resume_early - Execute "early resume" callbacks for all devices.
896 * @state: PM transition of the system being carried out.
898 void dpm_resume_early(pm_message_t state
)
901 ktime_t starttime
= ktime_get();
903 trace_suspend_resume(TPS("dpm_resume_early"), state
.event
, true);
904 mutex_lock(&dpm_list_mtx
);
905 pm_transition
= state
;
908 * Advanced the async threads upfront,
909 * in case the starting of async threads is
910 * delayed by non-async resuming devices.
912 list_for_each_entry(dev
, &dpm_late_early_list
, power
.entry
) {
913 reinit_completion(&dev
->power
.completion
);
916 async_schedule(async_resume_early
, dev
);
920 while (!list_empty(&dpm_late_early_list
)) {
921 dev
= to_device(dpm_late_early_list
.next
);
923 list_move_tail(&dev
->power
.entry
, &dpm_suspended_list
);
924 mutex_unlock(&dpm_list_mtx
);
926 if (!is_async(dev
)) {
929 error
= device_resume_early(dev
, state
, false);
931 suspend_stats
.failed_resume_early
++;
932 dpm_save_failed_step(SUSPEND_RESUME_EARLY
);
933 dpm_save_failed_dev(dev_name(dev
));
934 pm_dev_err(dev
, state
, " early", error
);
937 mutex_lock(&dpm_list_mtx
);
940 mutex_unlock(&dpm_list_mtx
);
941 async_synchronize_full();
942 dpm_show_time(starttime
, state
, 0, "early");
943 trace_suspend_resume(TPS("dpm_resume_early"), state
.event
, false);
947 * dpm_resume_start - Execute "noirq" and "early" device callbacks.
948 * @state: PM transition of the system being carried out.
950 void dpm_resume_start(pm_message_t state
)
952 dpm_resume_noirq(state
);
953 dpm_resume_early(state
);
955 EXPORT_SYMBOL_GPL(dpm_resume_start
);
958 * device_resume - Execute "resume" callbacks for given device.
959 * @dev: Device to handle.
960 * @state: PM transition of the system being carried out.
961 * @async: If true, the device is being resumed asynchronously.
963 static int device_resume(struct device
*dev
, pm_message_t state
, bool async
)
965 pm_callback_t callback
= NULL
;
966 const char *info
= NULL
;
968 DECLARE_DPM_WATCHDOG_ON_STACK(wd
);
973 if (dev
->power
.syscore
)
976 if (dev
->power
.direct_complete
) {
977 /* Match the pm_runtime_disable() in __device_suspend(). */
978 pm_runtime_enable(dev
);
982 if (!dpm_wait_for_superior(dev
, async
))
985 dpm_watchdog_set(&wd
, dev
);
989 * This is a fib. But we'll allow new children to be added below
990 * a resumed device, even if the device hasn't been completed yet.
992 dev
->power
.is_prepared
= false;
994 if (!dev
->power
.is_suspended
)
997 if (dev
->pm_domain
) {
998 info
= "power domain ";
999 callback
= pm_op(&dev
->pm_domain
->ops
, state
);
1003 if (dev
->type
&& dev
->type
->pm
) {
1005 callback
= pm_op(dev
->type
->pm
, state
);
1009 if (dev
->class && dev
->class->pm
) {
1011 callback
= pm_op(dev
->class->pm
, state
);
1018 callback
= pm_op(dev
->bus
->pm
, state
);
1019 } else if (dev
->bus
->resume
) {
1020 info
= "legacy bus ";
1021 callback
= dev
->bus
->resume
;
1027 if (!callback
&& dev
->driver
&& dev
->driver
->pm
) {
1029 callback
= pm_op(dev
->driver
->pm
, state
);
1033 error
= dpm_run_callback(callback
, dev
, state
, info
);
1034 dev
->power
.is_suspended
= false;
1038 dpm_watchdog_clear(&wd
);
1041 complete_all(&dev
->power
.completion
);
1043 TRACE_RESUME(error
);
1048 static void async_resume(void *data
, async_cookie_t cookie
)
1050 struct device
*dev
= (struct device
*)data
;
1053 error
= device_resume(dev
, pm_transition
, true);
1055 pm_dev_err(dev
, pm_transition
, " async", error
);
1060 * dpm_resume - Execute "resume" callbacks for non-sysdev devices.
1061 * @state: PM transition of the system being carried out.
1063 * Execute the appropriate "resume" callback for all devices whose status
1064 * indicates that they are suspended.
1066 void dpm_resume(pm_message_t state
)
1069 ktime_t starttime
= ktime_get();
1071 trace_suspend_resume(TPS("dpm_resume"), state
.event
, true);
1074 mutex_lock(&dpm_list_mtx
);
1075 pm_transition
= state
;
1078 list_for_each_entry(dev
, &dpm_suspended_list
, power
.entry
) {
1079 reinit_completion(&dev
->power
.completion
);
1080 if (is_async(dev
)) {
1082 async_schedule(async_resume
, dev
);
1086 while (!list_empty(&dpm_suspended_list
)) {
1087 dev
= to_device(dpm_suspended_list
.next
);
1089 if (!is_async(dev
)) {
1092 mutex_unlock(&dpm_list_mtx
);
1094 error
= device_resume(dev
, state
, false);
1096 suspend_stats
.failed_resume
++;
1097 dpm_save_failed_step(SUSPEND_RESUME
);
1098 dpm_save_failed_dev(dev_name(dev
));
1099 pm_dev_err(dev
, state
, "", error
);
1102 mutex_lock(&dpm_list_mtx
);
1104 if (!list_empty(&dev
->power
.entry
))
1105 list_move_tail(&dev
->power
.entry
, &dpm_prepared_list
);
1108 mutex_unlock(&dpm_list_mtx
);
1109 async_synchronize_full();
1110 dpm_show_time(starttime
, state
, 0, NULL
);
1113 trace_suspend_resume(TPS("dpm_resume"), state
.event
, false);
1117 * device_complete - Complete a PM transition for given device.
1118 * @dev: Device to handle.
1119 * @state: PM transition of the system being carried out.
1121 static void device_complete(struct device
*dev
, pm_message_t state
)
1123 void (*callback
)(struct device
*) = NULL
;
1124 const char *info
= NULL
;
1126 if (dev
->power
.syscore
)
1131 if (dev
->pm_domain
) {
1132 info
= "completing power domain ";
1133 callback
= dev
->pm_domain
->ops
.complete
;
1134 } else if (dev
->type
&& dev
->type
->pm
) {
1135 info
= "completing type ";
1136 callback
= dev
->type
->pm
->complete
;
1137 } else if (dev
->class && dev
->class->pm
) {
1138 info
= "completing class ";
1139 callback
= dev
->class->pm
->complete
;
1140 } else if (dev
->bus
&& dev
->bus
->pm
) {
1141 info
= "completing bus ";
1142 callback
= dev
->bus
->pm
->complete
;
1145 if (!callback
&& dev
->driver
&& dev
->driver
->pm
) {
1146 info
= "completing driver ";
1147 callback
= dev
->driver
->pm
->complete
;
1151 pm_dev_dbg(dev
, state
, info
);
1157 pm_runtime_put(dev
);
1161 * dpm_complete - Complete a PM transition for all non-sysdev devices.
1162 * @state: PM transition of the system being carried out.
1164 * Execute the ->complete() callbacks for all devices whose PM status is not
1165 * DPM_ON (this allows new devices to be registered).
1167 void dpm_complete(pm_message_t state
)
1169 struct list_head list
;
1171 trace_suspend_resume(TPS("dpm_complete"), state
.event
, true);
1174 INIT_LIST_HEAD(&list
);
1175 mutex_lock(&dpm_list_mtx
);
1176 while (!list_empty(&dpm_prepared_list
)) {
1177 struct device
*dev
= to_device(dpm_prepared_list
.prev
);
1180 dev
->power
.is_prepared
= false;
1181 list_move(&dev
->power
.entry
, &list
);
1182 mutex_unlock(&dpm_list_mtx
);
1184 trace_device_pm_callback_start(dev
, "", state
.event
);
1185 device_complete(dev
, state
);
1186 trace_device_pm_callback_end(dev
, 0);
1188 mutex_lock(&dpm_list_mtx
);
1191 list_splice(&list
, &dpm_list
);
1192 mutex_unlock(&dpm_list_mtx
);
1194 /* Allow device probing and trigger re-probing of deferred devices */
1195 device_unblock_probing();
1196 trace_suspend_resume(TPS("dpm_complete"), state
.event
, false);
1200 * dpm_resume_end - Execute "resume" callbacks and complete system transition.
1201 * @state: PM transition of the system being carried out.
1203 * Execute "resume" callbacks for all devices and complete the PM transition of
1206 void dpm_resume_end(pm_message_t state
)
1209 dpm_complete(state
);
1211 EXPORT_SYMBOL_GPL(dpm_resume_end
);
1214 /*------------------------- Suspend routines -------------------------*/
1217 * resume_event - Return a "resume" message for given "suspend" sleep state.
1218 * @sleep_state: PM message representing a sleep state.
1220 * Return a PM message representing the resume event corresponding to given
1223 static pm_message_t
resume_event(pm_message_t sleep_state
)
1225 switch (sleep_state
.event
) {
1226 case PM_EVENT_SUSPEND
:
1228 case PM_EVENT_FREEZE
:
1229 case PM_EVENT_QUIESCE
:
1230 return PMSG_RECOVER
;
1231 case PM_EVENT_HIBERNATE
:
1232 return PMSG_RESTORE
;
1237 static void dpm_superior_set_must_resume(struct device
*dev
)
1239 struct device_link
*link
;
1243 dev
->parent
->power
.must_resume
= true;
1245 idx
= device_links_read_lock();
1247 list_for_each_entry_rcu(link
, &dev
->links
.suppliers
, c_node
)
1248 link
->supplier
->power
.must_resume
= true;
1250 device_links_read_unlock(idx
);
1253 static pm_callback_t
dpm_subsys_suspend_noirq_cb(struct device
*dev
,
1255 const char **info_p
)
1257 pm_callback_t callback
;
1260 if (dev
->pm_domain
) {
1261 info
= "noirq power domain ";
1262 callback
= pm_noirq_op(&dev
->pm_domain
->ops
, state
);
1263 } else if (dev
->type
&& dev
->type
->pm
) {
1264 info
= "noirq type ";
1265 callback
= pm_noirq_op(dev
->type
->pm
, state
);
1266 } else if (dev
->class && dev
->class->pm
) {
1267 info
= "noirq class ";
1268 callback
= pm_noirq_op(dev
->class->pm
, state
);
1269 } else if (dev
->bus
&& dev
->bus
->pm
) {
1270 info
= "noirq bus ";
1271 callback
= pm_noirq_op(dev
->bus
->pm
, state
);
1282 static bool device_must_resume(struct device
*dev
, pm_message_t state
,
1283 bool no_subsys_suspend_noirq
)
1285 pm_message_t resume_msg
= resume_event(state
);
1288 * If all of the device driver's "noirq", "late" and "early" callbacks
1289 * are invoked directly by the core, the decision to allow the device to
1290 * stay in suspend can be based on its current runtime PM status and its
1293 if (no_subsys_suspend_noirq
&&
1294 !dpm_subsys_suspend_late_cb(dev
, state
, NULL
) &&
1295 !dpm_subsys_resume_early_cb(dev
, resume_msg
, NULL
) &&
1296 !dpm_subsys_resume_noirq_cb(dev
, resume_msg
, NULL
))
1297 return !pm_runtime_status_suspended(dev
) &&
1298 (resume_msg
.event
!= PM_EVENT_RESUME
||
1299 (device_can_wakeup(dev
) && !device_may_wakeup(dev
)));
1302 * The only safe strategy here is to require that if the device may not
1303 * be left in suspend, resume callbacks must be invoked for it.
1305 return !dev
->power
.may_skip_resume
;
1309 * __device_suspend_noirq - Execute a "noirq suspend" callback for given device.
1310 * @dev: Device to handle.
1311 * @state: PM transition of the system being carried out.
1312 * @async: If true, the device is being suspended asynchronously.
1314 * The driver of @dev will not receive interrupts while this function is being
1317 static int __device_suspend_noirq(struct device
*dev
, pm_message_t state
, bool async
)
1319 pm_callback_t callback
;
1321 bool no_subsys_cb
= false;
1327 dpm_wait_for_subordinate(dev
, async
);
1332 if (pm_wakeup_pending()) {
1333 async_error
= -EBUSY
;
1337 if (dev
->power
.syscore
|| dev
->power
.direct_complete
)
1340 callback
= dpm_subsys_suspend_noirq_cb(dev
, state
, &info
);
1344 no_subsys_cb
= !dpm_subsys_suspend_late_cb(dev
, state
, NULL
);
1346 if (dev_pm_smart_suspend_and_suspended(dev
) && no_subsys_cb
)
1349 if (dev
->driver
&& dev
->driver
->pm
) {
1350 info
= "noirq driver ";
1351 callback
= pm_noirq_op(dev
->driver
->pm
, state
);
1355 error
= dpm_run_callback(callback
, dev
, state
, info
);
1357 async_error
= error
;
1362 dev
->power
.is_noirq_suspended
= true;
1364 if (dev_pm_test_driver_flags(dev
, DPM_FLAG_LEAVE_SUSPENDED
)) {
1365 dev
->power
.must_resume
= dev
->power
.must_resume
||
1366 atomic_read(&dev
->power
.usage_count
) > 1 ||
1367 device_must_resume(dev
, state
, no_subsys_cb
);
1369 dev
->power
.must_resume
= true;
1372 if (dev
->power
.must_resume
)
1373 dpm_superior_set_must_resume(dev
);
1376 complete_all(&dev
->power
.completion
);
1377 TRACE_SUSPEND(error
);
1381 static void async_suspend_noirq(void *data
, async_cookie_t cookie
)
1383 struct device
*dev
= (struct device
*)data
;
1386 error
= __device_suspend_noirq(dev
, pm_transition
, true);
1388 dpm_save_failed_dev(dev_name(dev
));
1389 pm_dev_err(dev
, pm_transition
, " async", error
);
1395 static int device_suspend_noirq(struct device
*dev
)
1397 reinit_completion(&dev
->power
.completion
);
1399 if (is_async(dev
)) {
1401 async_schedule(async_suspend_noirq
, dev
);
1404 return __device_suspend_noirq(dev
, pm_transition
, false);
1407 void dpm_noirq_begin(void)
1410 device_wakeup_arm_wake_irqs();
1411 suspend_device_irqs();
1414 int dpm_noirq_suspend_devices(pm_message_t state
)
1416 ktime_t starttime
= ktime_get();
1419 trace_suspend_resume(TPS("dpm_suspend_noirq"), state
.event
, true);
1420 mutex_lock(&dpm_list_mtx
);
1421 pm_transition
= state
;
1424 while (!list_empty(&dpm_late_early_list
)) {
1425 struct device
*dev
= to_device(dpm_late_early_list
.prev
);
1428 mutex_unlock(&dpm_list_mtx
);
1430 error
= device_suspend_noirq(dev
);
1432 mutex_lock(&dpm_list_mtx
);
1434 pm_dev_err(dev
, state
, " noirq", error
);
1435 dpm_save_failed_dev(dev_name(dev
));
1439 if (!list_empty(&dev
->power
.entry
))
1440 list_move(&dev
->power
.entry
, &dpm_noirq_list
);
1446 mutex_unlock(&dpm_list_mtx
);
1447 async_synchronize_full();
1449 error
= async_error
;
1452 suspend_stats
.failed_suspend_noirq
++;
1453 dpm_save_failed_step(SUSPEND_SUSPEND_NOIRQ
);
1455 dpm_show_time(starttime
, state
, error
, "noirq");
1456 trace_suspend_resume(TPS("dpm_suspend_noirq"), state
.event
, false);
1461 * dpm_suspend_noirq - Execute "noirq suspend" callbacks for all devices.
1462 * @state: PM transition of the system being carried out.
1464 * Prevent device drivers' interrupt handlers from being called and invoke
1465 * "noirq" suspend callbacks for all non-sysdev devices.
1467 int dpm_suspend_noirq(pm_message_t state
)
1472 ret
= dpm_noirq_suspend_devices(state
);
1474 dpm_resume_noirq(resume_event(state
));
1479 static void dpm_propagate_wakeup_to_parent(struct device
*dev
)
1481 struct device
*parent
= dev
->parent
;
1486 spin_lock_irq(&parent
->power
.lock
);
1488 if (dev
->power
.wakeup_path
&& !parent
->power
.ignore_children
)
1489 parent
->power
.wakeup_path
= true;
1491 spin_unlock_irq(&parent
->power
.lock
);
1494 static pm_callback_t
dpm_subsys_suspend_late_cb(struct device
*dev
,
1496 const char **info_p
)
1498 pm_callback_t callback
;
1501 if (dev
->pm_domain
) {
1502 info
= "late power domain ";
1503 callback
= pm_late_early_op(&dev
->pm_domain
->ops
, state
);
1504 } else if (dev
->type
&& dev
->type
->pm
) {
1505 info
= "late type ";
1506 callback
= pm_late_early_op(dev
->type
->pm
, state
);
1507 } else if (dev
->class && dev
->class->pm
) {
1508 info
= "late class ";
1509 callback
= pm_late_early_op(dev
->class->pm
, state
);
1510 } else if (dev
->bus
&& dev
->bus
->pm
) {
1512 callback
= pm_late_early_op(dev
->bus
->pm
, state
);
1524 * __device_suspend_late - Execute a "late suspend" callback for given device.
1525 * @dev: Device to handle.
1526 * @state: PM transition of the system being carried out.
1527 * @async: If true, the device is being suspended asynchronously.
1529 * Runtime PM is disabled for @dev while this function is being executed.
1531 static int __device_suspend_late(struct device
*dev
, pm_message_t state
, bool async
)
1533 pm_callback_t callback
;
1540 __pm_runtime_disable(dev
, false);
1542 dpm_wait_for_subordinate(dev
, async
);
1547 if (pm_wakeup_pending()) {
1548 async_error
= -EBUSY
;
1552 if (dev
->power
.syscore
|| dev
->power
.direct_complete
)
1555 callback
= dpm_subsys_suspend_late_cb(dev
, state
, &info
);
1559 if (dev_pm_smart_suspend_and_suspended(dev
) &&
1560 !dpm_subsys_suspend_noirq_cb(dev
, state
, NULL
))
1563 if (dev
->driver
&& dev
->driver
->pm
) {
1564 info
= "late driver ";
1565 callback
= pm_late_early_op(dev
->driver
->pm
, state
);
1569 error
= dpm_run_callback(callback
, dev
, state
, info
);
1571 async_error
= error
;
1574 dpm_propagate_wakeup_to_parent(dev
);
1577 dev
->power
.is_late_suspended
= true;
1580 TRACE_SUSPEND(error
);
1581 complete_all(&dev
->power
.completion
);
1585 static void async_suspend_late(void *data
, async_cookie_t cookie
)
1587 struct device
*dev
= (struct device
*)data
;
1590 error
= __device_suspend_late(dev
, pm_transition
, true);
1592 dpm_save_failed_dev(dev_name(dev
));
1593 pm_dev_err(dev
, pm_transition
, " async", error
);
1598 static int device_suspend_late(struct device
*dev
)
1600 reinit_completion(&dev
->power
.completion
);
1602 if (is_async(dev
)) {
1604 async_schedule(async_suspend_late
, dev
);
1608 return __device_suspend_late(dev
, pm_transition
, false);
1612 * dpm_suspend_late - Execute "late suspend" callbacks for all devices.
1613 * @state: PM transition of the system being carried out.
1615 int dpm_suspend_late(pm_message_t state
)
1617 ktime_t starttime
= ktime_get();
1620 trace_suspend_resume(TPS("dpm_suspend_late"), state
.event
, true);
1621 mutex_lock(&dpm_list_mtx
);
1622 pm_transition
= state
;
1625 while (!list_empty(&dpm_suspended_list
)) {
1626 struct device
*dev
= to_device(dpm_suspended_list
.prev
);
1629 mutex_unlock(&dpm_list_mtx
);
1631 error
= device_suspend_late(dev
);
1633 mutex_lock(&dpm_list_mtx
);
1634 if (!list_empty(&dev
->power
.entry
))
1635 list_move(&dev
->power
.entry
, &dpm_late_early_list
);
1638 pm_dev_err(dev
, state
, " late", error
);
1639 dpm_save_failed_dev(dev_name(dev
));
1648 mutex_unlock(&dpm_list_mtx
);
1649 async_synchronize_full();
1651 error
= async_error
;
1653 suspend_stats
.failed_suspend_late
++;
1654 dpm_save_failed_step(SUSPEND_SUSPEND_LATE
);
1655 dpm_resume_early(resume_event(state
));
1657 dpm_show_time(starttime
, state
, error
, "late");
1658 trace_suspend_resume(TPS("dpm_suspend_late"), state
.event
, false);
1663 * dpm_suspend_end - Execute "late" and "noirq" device suspend callbacks.
1664 * @state: PM transition of the system being carried out.
1666 int dpm_suspend_end(pm_message_t state
)
1668 int error
= dpm_suspend_late(state
);
1672 error
= dpm_suspend_noirq(state
);
1674 dpm_resume_early(resume_event(state
));
1680 EXPORT_SYMBOL_GPL(dpm_suspend_end
);
1683 * legacy_suspend - Execute a legacy (bus or class) suspend callback for device.
1684 * @dev: Device to suspend.
1685 * @state: PM transition of the system being carried out.
1686 * @cb: Suspend callback to execute.
1687 * @info: string description of caller.
1689 static int legacy_suspend(struct device
*dev
, pm_message_t state
,
1690 int (*cb
)(struct device
*dev
, pm_message_t state
),
1696 calltime
= initcall_debug_start(dev
, cb
);
1698 trace_device_pm_callback_start(dev
, info
, state
.event
);
1699 error
= cb(dev
, state
);
1700 trace_device_pm_callback_end(dev
, error
);
1701 suspend_report_result(cb
, error
);
1703 initcall_debug_report(dev
, calltime
, cb
, error
);
1708 static void dpm_clear_superiors_direct_complete(struct device
*dev
)
1710 struct device_link
*link
;
1714 spin_lock_irq(&dev
->parent
->power
.lock
);
1715 dev
->parent
->power
.direct_complete
= false;
1716 spin_unlock_irq(&dev
->parent
->power
.lock
);
1719 idx
= device_links_read_lock();
1721 list_for_each_entry_rcu(link
, &dev
->links
.suppliers
, c_node
) {
1722 spin_lock_irq(&link
->supplier
->power
.lock
);
1723 link
->supplier
->power
.direct_complete
= false;
1724 spin_unlock_irq(&link
->supplier
->power
.lock
);
1727 device_links_read_unlock(idx
);
1731 * __device_suspend - Execute "suspend" callbacks for given device.
1732 * @dev: Device to handle.
1733 * @state: PM transition of the system being carried out.
1734 * @async: If true, the device is being suspended asynchronously.
1736 static int __device_suspend(struct device
*dev
, pm_message_t state
, bool async
)
1738 pm_callback_t callback
= NULL
;
1739 const char *info
= NULL
;
1741 DECLARE_DPM_WATCHDOG_ON_STACK(wd
);
1746 dpm_wait_for_subordinate(dev
, async
);
1749 dev
->power
.direct_complete
= false;
1754 * If a device configured to wake up the system from sleep states
1755 * has been suspended at run time and there's a resume request pending
1756 * for it, this is equivalent to the device signaling wakeup, so the
1757 * system suspend operation should be aborted.
1759 if (pm_runtime_barrier(dev
) && device_may_wakeup(dev
))
1760 pm_wakeup_event(dev
, 0);
1762 if (pm_wakeup_pending()) {
1763 dev
->power
.direct_complete
= false;
1764 async_error
= -EBUSY
;
1768 if (dev
->power
.syscore
)
1771 /* Avoid direct_complete to let wakeup_path propagate. */
1772 if (device_may_wakeup(dev
) || dev
->power
.wakeup_path
)
1773 dev
->power
.direct_complete
= false;
1775 if (dev
->power
.direct_complete
) {
1776 if (pm_runtime_status_suspended(dev
)) {
1777 pm_runtime_disable(dev
);
1778 if (pm_runtime_status_suspended(dev
))
1781 pm_runtime_enable(dev
);
1783 dev
->power
.direct_complete
= false;
1786 dev
->power
.may_skip_resume
= false;
1787 dev
->power
.must_resume
= false;
1789 dpm_watchdog_set(&wd
, dev
);
1792 if (dev
->pm_domain
) {
1793 info
= "power domain ";
1794 callback
= pm_op(&dev
->pm_domain
->ops
, state
);
1798 if (dev
->type
&& dev
->type
->pm
) {
1800 callback
= pm_op(dev
->type
->pm
, state
);
1804 if (dev
->class && dev
->class->pm
) {
1806 callback
= pm_op(dev
->class->pm
, state
);
1813 callback
= pm_op(dev
->bus
->pm
, state
);
1814 } else if (dev
->bus
->suspend
) {
1815 pm_dev_dbg(dev
, state
, "legacy bus ");
1816 error
= legacy_suspend(dev
, state
, dev
->bus
->suspend
,
1823 if (!callback
&& dev
->driver
&& dev
->driver
->pm
) {
1825 callback
= pm_op(dev
->driver
->pm
, state
);
1828 error
= dpm_run_callback(callback
, dev
, state
, info
);
1832 dev
->power
.is_suspended
= true;
1833 if (device_may_wakeup(dev
))
1834 dev
->power
.wakeup_path
= true;
1836 dpm_propagate_wakeup_to_parent(dev
);
1837 dpm_clear_superiors_direct_complete(dev
);
1841 dpm_watchdog_clear(&wd
);
1845 async_error
= error
;
1847 complete_all(&dev
->power
.completion
);
1848 TRACE_SUSPEND(error
);
1852 static void async_suspend(void *data
, async_cookie_t cookie
)
1854 struct device
*dev
= (struct device
*)data
;
1857 error
= __device_suspend(dev
, pm_transition
, true);
1859 dpm_save_failed_dev(dev_name(dev
));
1860 pm_dev_err(dev
, pm_transition
, " async", error
);
1866 static int device_suspend(struct device
*dev
)
1868 reinit_completion(&dev
->power
.completion
);
1870 if (is_async(dev
)) {
1872 async_schedule(async_suspend
, dev
);
1876 return __device_suspend(dev
, pm_transition
, false);
1880 * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices.
1881 * @state: PM transition of the system being carried out.
1883 int dpm_suspend(pm_message_t state
)
1885 ktime_t starttime
= ktime_get();
1888 trace_suspend_resume(TPS("dpm_suspend"), state
.event
, true);
1893 mutex_lock(&dpm_list_mtx
);
1894 pm_transition
= state
;
1896 while (!list_empty(&dpm_prepared_list
)) {
1897 struct device
*dev
= to_device(dpm_prepared_list
.prev
);
1900 mutex_unlock(&dpm_list_mtx
);
1902 error
= device_suspend(dev
);
1904 mutex_lock(&dpm_list_mtx
);
1906 pm_dev_err(dev
, state
, "", error
);
1907 dpm_save_failed_dev(dev_name(dev
));
1911 if (!list_empty(&dev
->power
.entry
))
1912 list_move(&dev
->power
.entry
, &dpm_suspended_list
);
1917 mutex_unlock(&dpm_list_mtx
);
1918 async_synchronize_full();
1920 error
= async_error
;
1922 suspend_stats
.failed_suspend
++;
1923 dpm_save_failed_step(SUSPEND_SUSPEND
);
1925 dpm_show_time(starttime
, state
, error
, NULL
);
1926 trace_suspend_resume(TPS("dpm_suspend"), state
.event
, false);
1931 * device_prepare - Prepare a device for system power transition.
1932 * @dev: Device to handle.
1933 * @state: PM transition of the system being carried out.
1935 * Execute the ->prepare() callback(s) for given device. No new children of the
1936 * device may be registered after this function has returned.
1938 static int device_prepare(struct device
*dev
, pm_message_t state
)
1940 int (*callback
)(struct device
*) = NULL
;
1943 if (dev
->power
.syscore
)
1946 WARN_ON(!pm_runtime_enabled(dev
) &&
1947 dev_pm_test_driver_flags(dev
, DPM_FLAG_SMART_SUSPEND
|
1948 DPM_FLAG_LEAVE_SUSPENDED
));
1951 * If a device's parent goes into runtime suspend at the wrong time,
1952 * it won't be possible to resume the device. To prevent this we
1953 * block runtime suspend here, during the prepare phase, and allow
1954 * it again during the complete phase.
1956 pm_runtime_get_noresume(dev
);
1960 dev
->power
.wakeup_path
= false;
1962 if (dev
->power
.no_pm_callbacks
)
1966 callback
= dev
->pm_domain
->ops
.prepare
;
1967 else if (dev
->type
&& dev
->type
->pm
)
1968 callback
= dev
->type
->pm
->prepare
;
1969 else if (dev
->class && dev
->class->pm
)
1970 callback
= dev
->class->pm
->prepare
;
1971 else if (dev
->bus
&& dev
->bus
->pm
)
1972 callback
= dev
->bus
->pm
->prepare
;
1974 if (!callback
&& dev
->driver
&& dev
->driver
->pm
)
1975 callback
= dev
->driver
->pm
->prepare
;
1978 ret
= callback(dev
);
1984 suspend_report_result(callback
, ret
);
1985 pm_runtime_put(dev
);
1989 * A positive return value from ->prepare() means "this device appears
1990 * to be runtime-suspended and its state is fine, so if it really is
1991 * runtime-suspended, you can leave it in that state provided that you
1992 * will do the same thing with all of its descendants". This only
1993 * applies to suspend transitions, however.
1995 spin_lock_irq(&dev
->power
.lock
);
1996 dev
->power
.direct_complete
= state
.event
== PM_EVENT_SUSPEND
&&
1997 ((pm_runtime_suspended(dev
) && ret
> 0) ||
1998 dev
->power
.no_pm_callbacks
) &&
1999 !dev_pm_test_driver_flags(dev
, DPM_FLAG_NEVER_SKIP
);
2000 spin_unlock_irq(&dev
->power
.lock
);
2005 * dpm_prepare - Prepare all non-sysdev devices for a system PM transition.
2006 * @state: PM transition of the system being carried out.
2008 * Execute the ->prepare() callback(s) for all devices.
2010 int dpm_prepare(pm_message_t state
)
2014 trace_suspend_resume(TPS("dpm_prepare"), state
.event
, true);
2018 * Give a chance for the known devices to complete their probes, before
2019 * disable probing of devices. This sync point is important at least
2020 * at boot time + hibernation restore.
2022 wait_for_device_probe();
2024 * It is unsafe if probing of devices will happen during suspend or
2025 * hibernation and system behavior will be unpredictable in this case.
2026 * So, let's prohibit device's probing here and defer their probes
2027 * instead. The normal behavior will be restored in dpm_complete().
2029 device_block_probing();
2031 mutex_lock(&dpm_list_mtx
);
2032 while (!list_empty(&dpm_list
)) {
2033 struct device
*dev
= to_device(dpm_list
.next
);
2036 mutex_unlock(&dpm_list_mtx
);
2038 trace_device_pm_callback_start(dev
, "", state
.event
);
2039 error
= device_prepare(dev
, state
);
2040 trace_device_pm_callback_end(dev
, error
);
2042 mutex_lock(&dpm_list_mtx
);
2044 if (error
== -EAGAIN
) {
2049 printk(KERN_INFO
"PM: Device %s not prepared "
2050 "for power transition: code %d\n",
2051 dev_name(dev
), error
);
2055 dev
->power
.is_prepared
= true;
2056 if (!list_empty(&dev
->power
.entry
))
2057 list_move_tail(&dev
->power
.entry
, &dpm_prepared_list
);
2060 mutex_unlock(&dpm_list_mtx
);
2061 trace_suspend_resume(TPS("dpm_prepare"), state
.event
, false);
2066 * dpm_suspend_start - Prepare devices for PM transition and suspend them.
2067 * @state: PM transition of the system being carried out.
2069 * Prepare all non-sysdev devices for system PM transition and execute "suspend"
2070 * callbacks for them.
2072 int dpm_suspend_start(pm_message_t state
)
2076 error
= dpm_prepare(state
);
2078 suspend_stats
.failed_prepare
++;
2079 dpm_save_failed_step(SUSPEND_PREPARE
);
2081 error
= dpm_suspend(state
);
2084 EXPORT_SYMBOL_GPL(dpm_suspend_start
);
2086 void __suspend_report_result(const char *function
, void *fn
, int ret
)
2089 printk(KERN_ERR
"%s(): %pF returns %d\n", function
, fn
, ret
);
2091 EXPORT_SYMBOL_GPL(__suspend_report_result
);
2094 * device_pm_wait_for_dev - Wait for suspend/resume of a device to complete.
2095 * @dev: Device to wait for.
2096 * @subordinate: Device that needs to wait for @dev.
2098 int device_pm_wait_for_dev(struct device
*subordinate
, struct device
*dev
)
2100 dpm_wait(dev
, subordinate
->power
.async_suspend
);
2103 EXPORT_SYMBOL_GPL(device_pm_wait_for_dev
);
2106 * dpm_for_each_dev - device iterator.
2107 * @data: data for the callback.
2108 * @fn: function to be called for each device.
2110 * Iterate over devices in dpm_list, and call @fn for each device,
2113 void dpm_for_each_dev(void *data
, void (*fn
)(struct device
*, void *))
2121 list_for_each_entry(dev
, &dpm_list
, power
.entry
)
2125 EXPORT_SYMBOL_GPL(dpm_for_each_dev
);
2127 static bool pm_ops_is_empty(const struct dev_pm_ops
*ops
)
2132 return !ops
->prepare
&&
2134 !ops
->suspend_late
&&
2135 !ops
->suspend_noirq
&&
2136 !ops
->resume_noirq
&&
2137 !ops
->resume_early
&&
2142 void device_pm_check_callbacks(struct device
*dev
)
2144 spin_lock_irq(&dev
->power
.lock
);
2145 dev
->power
.no_pm_callbacks
=
2146 (!dev
->bus
|| (pm_ops_is_empty(dev
->bus
->pm
) &&
2147 !dev
->bus
->suspend
&& !dev
->bus
->resume
)) &&
2148 (!dev
->class || pm_ops_is_empty(dev
->class->pm
)) &&
2149 (!dev
->type
|| pm_ops_is_empty(dev
->type
->pm
)) &&
2150 (!dev
->pm_domain
|| pm_ops_is_empty(&dev
->pm_domain
->ops
)) &&
2151 (!dev
->driver
|| (pm_ops_is_empty(dev
->driver
->pm
) &&
2152 !dev
->driver
->suspend
&& !dev
->driver
->resume
));
2153 spin_unlock_irq(&dev
->power
.lock
);
2156 bool dev_pm_smart_suspend_and_suspended(struct device
*dev
)
2158 return dev_pm_test_driver_flags(dev
, DPM_FLAG_SMART_SUSPEND
) &&
2159 pm_runtime_status_suspended(dev
);