1 // SPDX-License-Identifier: GPL-2.0
3 * drivers/base/power/main.c - Where the driver meets power management.
5 * Copyright (c) 2003 Patrick Mochel
6 * Copyright (c) 2003 Open Source Development Lab
8 * The driver model core calls device_pm_add() when a device is registered.
9 * This will initialize the embedded device_pm_info object in the device
10 * and add it to the list of power-controlled devices. sysfs entries for
11 * controlling device power management will also be added.
13 * A separate list is used for keeping track of power info, because the power
14 * domain dependencies may differ from the ancestral dependencies that the
15 * subsystem list maintains.
18 #define pr_fmt(fmt) "PM: " fmt
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/devfreq.h>
36 #include <linux/timer.h>
41 typedef int (*pm_callback_t
)(struct device
*);
44 * The entries in the dpm_list list are in a depth first order, simply
45 * because children are guaranteed to be discovered after parents, and
46 * are inserted at the back of the list on discovery.
48 * Since device_pm_add() may be called with a device lock held,
49 * we must never try to acquire a device lock while holding
54 static LIST_HEAD(dpm_prepared_list
);
55 static LIST_HEAD(dpm_suspended_list
);
56 static LIST_HEAD(dpm_late_early_list
);
57 static LIST_HEAD(dpm_noirq_list
);
59 struct suspend_stats suspend_stats
;
60 static DEFINE_MUTEX(dpm_list_mtx
);
61 static pm_message_t pm_transition
;
63 static int async_error
;
65 static const char *pm_verb(int event
)
68 case PM_EVENT_SUSPEND
:
74 case PM_EVENT_QUIESCE
:
76 case PM_EVENT_HIBERNATE
:
80 case PM_EVENT_RESTORE
:
82 case PM_EVENT_RECOVER
:
85 return "(unknown PM event)";
90 * device_pm_sleep_init - Initialize system suspend-related device fields.
91 * @dev: Device object being initialized.
93 void device_pm_sleep_init(struct device
*dev
)
95 dev
->power
.is_prepared
= false;
96 dev
->power
.is_suspended
= false;
97 dev
->power
.is_noirq_suspended
= false;
98 dev
->power
.is_late_suspended
= false;
99 init_completion(&dev
->power
.completion
);
100 complete_all(&dev
->power
.completion
);
101 dev
->power
.wakeup
= NULL
;
102 INIT_LIST_HEAD(&dev
->power
.entry
);
106 * device_pm_lock - Lock the list of active devices used by the PM core.
108 void device_pm_lock(void)
110 mutex_lock(&dpm_list_mtx
);
114 * device_pm_unlock - Unlock the list of active devices used by the PM core.
116 void device_pm_unlock(void)
118 mutex_unlock(&dpm_list_mtx
);
122 * device_pm_add - Add a device to the PM core's list of active devices.
123 * @dev: Device to add to the list.
125 void device_pm_add(struct device
*dev
)
127 /* Skip PM setup/initialization. */
128 if (device_pm_not_required(dev
))
131 pr_debug("Adding info for %s:%s\n",
132 dev
->bus
? dev
->bus
->name
: "No Bus", dev_name(dev
));
133 device_pm_check_callbacks(dev
);
134 mutex_lock(&dpm_list_mtx
);
135 if (dev
->parent
&& dev
->parent
->power
.is_prepared
)
136 dev_warn(dev
, "parent %s should not be sleeping\n",
137 dev_name(dev
->parent
));
138 list_add_tail(&dev
->power
.entry
, &dpm_list
);
139 dev
->power
.in_dpm_list
= true;
140 mutex_unlock(&dpm_list_mtx
);
144 * device_pm_remove - Remove a device from the PM core's list of active devices.
145 * @dev: Device to be removed from the list.
147 void device_pm_remove(struct device
*dev
)
149 if (device_pm_not_required(dev
))
152 pr_debug("Removing info for %s:%s\n",
153 dev
->bus
? dev
->bus
->name
: "No Bus", dev_name(dev
));
154 complete_all(&dev
->power
.completion
);
155 mutex_lock(&dpm_list_mtx
);
156 list_del_init(&dev
->power
.entry
);
157 dev
->power
.in_dpm_list
= false;
158 mutex_unlock(&dpm_list_mtx
);
159 device_wakeup_disable(dev
);
160 pm_runtime_remove(dev
);
161 device_pm_check_callbacks(dev
);
165 * device_pm_move_before - Move device in the PM core's list of active devices.
166 * @deva: Device to move in dpm_list.
167 * @devb: Device @deva should come before.
169 void device_pm_move_before(struct device
*deva
, struct device
*devb
)
171 pr_debug("Moving %s:%s before %s:%s\n",
172 deva
->bus
? deva
->bus
->name
: "No Bus", dev_name(deva
),
173 devb
->bus
? devb
->bus
->name
: "No Bus", dev_name(devb
));
174 /* Delete deva from dpm_list and reinsert before devb. */
175 list_move_tail(&deva
->power
.entry
, &devb
->power
.entry
);
179 * device_pm_move_after - Move device in the PM core's list of active devices.
180 * @deva: Device to move in dpm_list.
181 * @devb: Device @deva should come after.
183 void device_pm_move_after(struct device
*deva
, struct device
*devb
)
185 pr_debug("Moving %s:%s after %s:%s\n",
186 deva
->bus
? deva
->bus
->name
: "No Bus", dev_name(deva
),
187 devb
->bus
? devb
->bus
->name
: "No Bus", dev_name(devb
));
188 /* Delete deva from dpm_list and reinsert after devb. */
189 list_move(&deva
->power
.entry
, &devb
->power
.entry
);
193 * device_pm_move_last - Move device to end of the PM core's list of devices.
194 * @dev: Device to move in dpm_list.
196 void device_pm_move_last(struct device
*dev
)
198 pr_debug("Moving %s:%s to end of list\n",
199 dev
->bus
? dev
->bus
->name
: "No Bus", dev_name(dev
));
200 list_move_tail(&dev
->power
.entry
, &dpm_list
);
203 static ktime_t
initcall_debug_start(struct device
*dev
, void *cb
)
205 if (!pm_print_times_enabled
)
208 dev_info(dev
, "calling %pS @ %i, parent: %s\n", cb
,
209 task_pid_nr(current
),
210 dev
->parent
? dev_name(dev
->parent
) : "none");
214 static void initcall_debug_report(struct device
*dev
, ktime_t calltime
,
220 if (!pm_print_times_enabled
)
223 rettime
= ktime_get();
224 nsecs
= (s64
) ktime_to_ns(ktime_sub(rettime
, calltime
));
226 dev_info(dev
, "%pS returned %d after %Ld usecs\n", cb
, error
,
227 (unsigned long long)nsecs
>> 10);
231 * dpm_wait - Wait for a PM operation to complete.
232 * @dev: Device to wait for.
233 * @async: If unset, wait only if the device's power.async_suspend flag is set.
235 static void dpm_wait(struct device
*dev
, bool async
)
240 if (async
|| (pm_async_enabled
&& dev
->power
.async_suspend
))
241 wait_for_completion(&dev
->power
.completion
);
244 static int dpm_wait_fn(struct device
*dev
, void *async_ptr
)
246 dpm_wait(dev
, *((bool *)async_ptr
));
250 static void dpm_wait_for_children(struct device
*dev
, bool async
)
252 device_for_each_child(dev
, &async
, dpm_wait_fn
);
255 static void dpm_wait_for_suppliers(struct device
*dev
, bool async
)
257 struct device_link
*link
;
260 idx
= device_links_read_lock();
263 * If the supplier goes away right after we've checked the link to it,
264 * we'll wait for its completion to change the state, but that's fine,
265 * because the only things that will block as a result are the SRCU
266 * callbacks freeing the link objects for the links in the list we're
269 list_for_each_entry_rcu(link
, &dev
->links
.suppliers
, c_node
)
270 if (READ_ONCE(link
->status
) != DL_STATE_DORMANT
)
271 dpm_wait(link
->supplier
, async
);
273 device_links_read_unlock(idx
);
276 static void dpm_wait_for_superior(struct device
*dev
, bool async
)
278 dpm_wait(dev
->parent
, async
);
279 dpm_wait_for_suppliers(dev
, async
);
282 static void dpm_wait_for_consumers(struct device
*dev
, bool async
)
284 struct device_link
*link
;
287 idx
= device_links_read_lock();
290 * The status of a device link can only be changed from "dormant" by a
291 * probe, but that cannot happen during system suspend/resume. In
292 * theory it can change to "dormant" at that time, but then it is
293 * reasonable to wait for the target device anyway (eg. if it goes
294 * away, it's better to wait for it to go away completely and then
295 * continue instead of trying to continue in parallel with its
298 list_for_each_entry_rcu(link
, &dev
->links
.consumers
, s_node
)
299 if (READ_ONCE(link
->status
) != DL_STATE_DORMANT
)
300 dpm_wait(link
->consumer
, async
);
302 device_links_read_unlock(idx
);
305 static void dpm_wait_for_subordinate(struct device
*dev
, bool async
)
307 dpm_wait_for_children(dev
, async
);
308 dpm_wait_for_consumers(dev
, async
);
312 * pm_op - Return the PM operation appropriate for given PM event.
313 * @ops: PM operations to choose from.
314 * @state: PM transition of the system being carried out.
316 static pm_callback_t
pm_op(const struct dev_pm_ops
*ops
, pm_message_t state
)
318 switch (state
.event
) {
319 #ifdef CONFIG_SUSPEND
320 case PM_EVENT_SUSPEND
:
322 case PM_EVENT_RESUME
:
324 #endif /* CONFIG_SUSPEND */
325 #ifdef CONFIG_HIBERNATE_CALLBACKS
326 case PM_EVENT_FREEZE
:
327 case PM_EVENT_QUIESCE
:
329 case PM_EVENT_HIBERNATE
:
330 return ops
->poweroff
;
332 case PM_EVENT_RECOVER
:
335 case PM_EVENT_RESTORE
:
337 #endif /* CONFIG_HIBERNATE_CALLBACKS */
344 * pm_late_early_op - Return the PM operation appropriate for given PM event.
345 * @ops: PM operations to choose from.
346 * @state: PM transition of the system being carried out.
348 * Runtime PM is disabled for @dev while this function is being executed.
350 static pm_callback_t
pm_late_early_op(const struct dev_pm_ops
*ops
,
353 switch (state
.event
) {
354 #ifdef CONFIG_SUSPEND
355 case PM_EVENT_SUSPEND
:
356 return ops
->suspend_late
;
357 case PM_EVENT_RESUME
:
358 return ops
->resume_early
;
359 #endif /* CONFIG_SUSPEND */
360 #ifdef CONFIG_HIBERNATE_CALLBACKS
361 case PM_EVENT_FREEZE
:
362 case PM_EVENT_QUIESCE
:
363 return ops
->freeze_late
;
364 case PM_EVENT_HIBERNATE
:
365 return ops
->poweroff_late
;
367 case PM_EVENT_RECOVER
:
368 return ops
->thaw_early
;
369 case PM_EVENT_RESTORE
:
370 return ops
->restore_early
;
371 #endif /* CONFIG_HIBERNATE_CALLBACKS */
378 * pm_noirq_op - Return the PM operation appropriate for given PM event.
379 * @ops: PM operations to choose from.
380 * @state: PM transition of the system being carried out.
382 * The driver of @dev will not receive interrupts while this function is being
385 static pm_callback_t
pm_noirq_op(const struct dev_pm_ops
*ops
, pm_message_t state
)
387 switch (state
.event
) {
388 #ifdef CONFIG_SUSPEND
389 case PM_EVENT_SUSPEND
:
390 return ops
->suspend_noirq
;
391 case PM_EVENT_RESUME
:
392 return ops
->resume_noirq
;
393 #endif /* CONFIG_SUSPEND */
394 #ifdef CONFIG_HIBERNATE_CALLBACKS
395 case PM_EVENT_FREEZE
:
396 case PM_EVENT_QUIESCE
:
397 return ops
->freeze_noirq
;
398 case PM_EVENT_HIBERNATE
:
399 return ops
->poweroff_noirq
;
401 case PM_EVENT_RECOVER
:
402 return ops
->thaw_noirq
;
403 case PM_EVENT_RESTORE
:
404 return ops
->restore_noirq
;
405 #endif /* CONFIG_HIBERNATE_CALLBACKS */
411 static void pm_dev_dbg(struct device
*dev
, pm_message_t state
, const char *info
)
413 dev_dbg(dev
, "%s%s%s\n", info
, pm_verb(state
.event
),
414 ((state
.event
& PM_EVENT_SLEEP
) && device_may_wakeup(dev
)) ?
415 ", may wakeup" : "");
418 static void pm_dev_err(struct device
*dev
, pm_message_t state
, const char *info
,
421 pr_err("Device %s failed to %s%s: error %d\n",
422 dev_name(dev
), pm_verb(state
.event
), info
, error
);
425 static void dpm_show_time(ktime_t starttime
, pm_message_t state
, int error
,
432 calltime
= ktime_get();
433 usecs64
= ktime_to_ns(ktime_sub(calltime
, starttime
));
434 do_div(usecs64
, NSEC_PER_USEC
);
439 pm_pr_dbg("%s%s%s of devices %s after %ld.%03ld msecs\n",
440 info
?: "", info
? " " : "", pm_verb(state
.event
),
441 error
? "aborted" : "complete",
442 usecs
/ USEC_PER_MSEC
, usecs
% USEC_PER_MSEC
);
445 static int dpm_run_callback(pm_callback_t cb
, struct device
*dev
,
446 pm_message_t state
, const char *info
)
454 calltime
= initcall_debug_start(dev
, cb
);
456 pm_dev_dbg(dev
, state
, info
);
457 trace_device_pm_callback_start(dev
, info
, state
.event
);
459 trace_device_pm_callback_end(dev
, error
);
460 suspend_report_result(cb
, error
);
462 initcall_debug_report(dev
, calltime
, cb
, error
);
467 #ifdef CONFIG_DPM_WATCHDOG
468 struct dpm_watchdog
{
470 struct task_struct
*tsk
;
471 struct timer_list timer
;
474 #define DECLARE_DPM_WATCHDOG_ON_STACK(wd) \
475 struct dpm_watchdog wd
478 * dpm_watchdog_handler - Driver suspend / resume watchdog handler.
479 * @t: The timer that PM watchdog depends on.
481 * Called when a driver has timed out suspending or resuming.
482 * There's not much we can do here to recover so panic() to
483 * capture a crash-dump in pstore.
485 static void dpm_watchdog_handler(struct timer_list
*t
)
487 struct dpm_watchdog
*wd
= from_timer(wd
, t
, timer
);
489 dev_emerg(wd
->dev
, "**** DPM device timeout ****\n");
490 show_stack(wd
->tsk
, NULL
);
491 panic("%s %s: unrecoverable failure\n",
492 dev_driver_string(wd
->dev
), dev_name(wd
->dev
));
496 * dpm_watchdog_set - Enable pm watchdog for given device.
497 * @wd: Watchdog. Must be allocated on the stack.
498 * @dev: Device to handle.
500 static void dpm_watchdog_set(struct dpm_watchdog
*wd
, struct device
*dev
)
502 struct timer_list
*timer
= &wd
->timer
;
507 timer_setup_on_stack(timer
, dpm_watchdog_handler
, 0);
508 /* use same timeout value for both suspend and resume */
509 timer
->expires
= jiffies
+ HZ
* CONFIG_DPM_WATCHDOG_TIMEOUT
;
514 * dpm_watchdog_clear - Disable suspend/resume watchdog.
515 * @wd: Watchdog to disable.
517 static void dpm_watchdog_clear(struct dpm_watchdog
*wd
)
519 struct timer_list
*timer
= &wd
->timer
;
521 del_timer_sync(timer
);
522 destroy_timer_on_stack(timer
);
525 #define DECLARE_DPM_WATCHDOG_ON_STACK(wd)
526 #define dpm_watchdog_set(x, y)
527 #define dpm_watchdog_clear(x)
530 /*------------------------- Resume routines -------------------------*/
533 * dev_pm_skip_next_resume_phases - Skip next system resume phases for device.
534 * @dev: Target device.
536 * Make the core skip the "early resume" and "resume" phases for @dev.
538 * This function can be called by middle-layer code during the "noirq" phase of
539 * system resume if necessary, but not by device drivers.
541 void dev_pm_skip_next_resume_phases(struct device
*dev
)
543 dev
->power
.is_late_suspended
= false;
544 dev
->power
.is_suspended
= false;
548 * suspend_event - Return a "suspend" message for given "resume" one.
549 * @resume_msg: PM message representing a system-wide resume transition.
551 static pm_message_t
suspend_event(pm_message_t resume_msg
)
553 switch (resume_msg
.event
) {
554 case PM_EVENT_RESUME
:
557 case PM_EVENT_RESTORE
:
559 case PM_EVENT_RECOVER
:
560 return PMSG_HIBERNATE
;
566 * dev_pm_may_skip_resume - System-wide device resume optimization check.
567 * @dev: Target device.
569 * Checks whether or not the device may be left in suspend after a system-wide
570 * transition to the working state.
572 bool dev_pm_may_skip_resume(struct device
*dev
)
574 return !dev
->power
.must_resume
&& pm_transition
.event
!= PM_EVENT_RESTORE
;
577 static pm_callback_t
dpm_subsys_resume_noirq_cb(struct device
*dev
,
581 pm_callback_t callback
;
584 if (dev
->pm_domain
) {
585 info
= "noirq power domain ";
586 callback
= pm_noirq_op(&dev
->pm_domain
->ops
, state
);
587 } else if (dev
->type
&& dev
->type
->pm
) {
588 info
= "noirq type ";
589 callback
= pm_noirq_op(dev
->type
->pm
, state
);
590 } else if (dev
->class && dev
->class->pm
) {
591 info
= "noirq class ";
592 callback
= pm_noirq_op(dev
->class->pm
, state
);
593 } else if (dev
->bus
&& dev
->bus
->pm
) {
595 callback
= pm_noirq_op(dev
->bus
->pm
, state
);
606 static pm_callback_t
dpm_subsys_suspend_noirq_cb(struct device
*dev
,
608 const char **info_p
);
610 static pm_callback_t
dpm_subsys_suspend_late_cb(struct device
*dev
,
612 const char **info_p
);
615 * device_resume_noirq - Execute a "noirq resume" callback for given device.
616 * @dev: Device to handle.
617 * @state: PM transition of the system being carried out.
618 * @async: If true, the device is being resumed asynchronously.
620 * The driver of @dev will not receive interrupts while this function is being
623 static int device_resume_noirq(struct device
*dev
, pm_message_t state
, bool async
)
625 pm_callback_t callback
;
633 if (dev
->power
.syscore
|| dev
->power
.direct_complete
)
636 if (!dev
->power
.is_noirq_suspended
)
639 dpm_wait_for_superior(dev
, async
);
641 skip_resume
= dev_pm_may_skip_resume(dev
);
643 callback
= dpm_subsys_resume_noirq_cb(dev
, state
, &info
);
650 if (dev_pm_smart_suspend_and_suspended(dev
)) {
651 pm_message_t suspend_msg
= suspend_event(state
);
654 * If "freeze" callbacks have been skipped during a transition
655 * related to hibernation, the subsequent "thaw" callbacks must
656 * be skipped too or bad things may happen. Otherwise, resume
657 * callbacks are going to be run for the device, so its runtime
658 * PM status must be changed to reflect the new state after the
659 * transition under way.
661 if (!dpm_subsys_suspend_late_cb(dev
, suspend_msg
, NULL
) &&
662 !dpm_subsys_suspend_noirq_cb(dev
, suspend_msg
, NULL
)) {
663 if (state
.event
== PM_EVENT_THAW
) {
667 pm_runtime_set_active(dev
);
672 if (dev
->driver
&& dev
->driver
->pm
) {
673 info
= "noirq driver ";
674 callback
= pm_noirq_op(dev
->driver
->pm
, state
);
678 error
= dpm_run_callback(callback
, dev
, state
, info
);
681 dev
->power
.is_noirq_suspended
= false;
685 * The device is going to be left in suspend, but it might not
686 * have been in runtime suspend before the system suspended, so
687 * its runtime PM status needs to be updated to avoid confusing
688 * the runtime PM framework when runtime PM is enabled for the
691 pm_runtime_set_suspended(dev
);
692 dev_pm_skip_next_resume_phases(dev
);
696 complete_all(&dev
->power
.completion
);
701 static bool is_async(struct device
*dev
)
703 return dev
->power
.async_suspend
&& pm_async_enabled
704 && !pm_trace_is_enabled();
707 static bool dpm_async_fn(struct device
*dev
, async_func_t func
)
709 reinit_completion(&dev
->power
.completion
);
713 async_schedule(func
, dev
);
720 static void async_resume_noirq(void *data
, async_cookie_t cookie
)
722 struct device
*dev
= (struct device
*)data
;
725 error
= device_resume_noirq(dev
, pm_transition
, true);
727 pm_dev_err(dev
, pm_transition
, " async", error
);
732 void dpm_noirq_resume_devices(pm_message_t state
)
735 ktime_t starttime
= ktime_get();
737 trace_suspend_resume(TPS("dpm_resume_noirq"), state
.event
, true);
738 mutex_lock(&dpm_list_mtx
);
739 pm_transition
= state
;
742 * Advanced the async threads upfront,
743 * in case the starting of async threads is
744 * delayed by non-async resuming devices.
746 list_for_each_entry(dev
, &dpm_noirq_list
, power
.entry
)
747 dpm_async_fn(dev
, async_resume_noirq
);
749 while (!list_empty(&dpm_noirq_list
)) {
750 dev
= to_device(dpm_noirq_list
.next
);
752 list_move_tail(&dev
->power
.entry
, &dpm_late_early_list
);
753 mutex_unlock(&dpm_list_mtx
);
755 if (!is_async(dev
)) {
758 error
= device_resume_noirq(dev
, state
, false);
760 suspend_stats
.failed_resume_noirq
++;
761 dpm_save_failed_step(SUSPEND_RESUME_NOIRQ
);
762 dpm_save_failed_dev(dev_name(dev
));
763 pm_dev_err(dev
, state
, " noirq", error
);
767 mutex_lock(&dpm_list_mtx
);
770 mutex_unlock(&dpm_list_mtx
);
771 async_synchronize_full();
772 dpm_show_time(starttime
, state
, 0, "noirq");
773 trace_suspend_resume(TPS("dpm_resume_noirq"), state
.event
, false);
776 void dpm_noirq_end(void)
778 resume_device_irqs();
779 device_wakeup_disarm_wake_irqs();
784 * dpm_resume_noirq - Execute "noirq resume" callbacks for all devices.
785 * @state: PM transition of the system being carried out.
787 * Invoke the "noirq" resume callbacks for all devices in dpm_noirq_list and
788 * allow device drivers' interrupt handlers to be called.
790 void dpm_resume_noirq(pm_message_t state
)
792 dpm_noirq_resume_devices(state
);
796 static pm_callback_t
dpm_subsys_resume_early_cb(struct device
*dev
,
800 pm_callback_t callback
;
803 if (dev
->pm_domain
) {
804 info
= "early power domain ";
805 callback
= pm_late_early_op(&dev
->pm_domain
->ops
, state
);
806 } else if (dev
->type
&& dev
->type
->pm
) {
807 info
= "early type ";
808 callback
= pm_late_early_op(dev
->type
->pm
, state
);
809 } else if (dev
->class && dev
->class->pm
) {
810 info
= "early class ";
811 callback
= pm_late_early_op(dev
->class->pm
, state
);
812 } else if (dev
->bus
&& dev
->bus
->pm
) {
814 callback
= pm_late_early_op(dev
->bus
->pm
, state
);
826 * device_resume_early - Execute an "early resume" callback for given device.
827 * @dev: Device to handle.
828 * @state: PM transition of the system being carried out.
829 * @async: If true, the device is being resumed asynchronously.
831 * Runtime PM is disabled for @dev while this function is being executed.
833 static int device_resume_early(struct device
*dev
, pm_message_t state
, bool async
)
835 pm_callback_t callback
;
842 if (dev
->power
.syscore
|| dev
->power
.direct_complete
)
845 if (!dev
->power
.is_late_suspended
)
848 dpm_wait_for_superior(dev
, async
);
850 callback
= dpm_subsys_resume_early_cb(dev
, state
, &info
);
852 if (!callback
&& dev
->driver
&& dev
->driver
->pm
) {
853 info
= "early driver ";
854 callback
= pm_late_early_op(dev
->driver
->pm
, state
);
857 error
= dpm_run_callback(callback
, dev
, state
, info
);
858 dev
->power
.is_late_suspended
= false;
863 pm_runtime_enable(dev
);
864 complete_all(&dev
->power
.completion
);
868 static void async_resume_early(void *data
, async_cookie_t cookie
)
870 struct device
*dev
= (struct device
*)data
;
873 error
= device_resume_early(dev
, pm_transition
, true);
875 pm_dev_err(dev
, pm_transition
, " async", error
);
881 * dpm_resume_early - Execute "early resume" callbacks for all devices.
882 * @state: PM transition of the system being carried out.
884 void dpm_resume_early(pm_message_t state
)
887 ktime_t starttime
= ktime_get();
889 trace_suspend_resume(TPS("dpm_resume_early"), state
.event
, true);
890 mutex_lock(&dpm_list_mtx
);
891 pm_transition
= state
;
894 * Advanced the async threads upfront,
895 * in case the starting of async threads is
896 * delayed by non-async resuming devices.
898 list_for_each_entry(dev
, &dpm_late_early_list
, power
.entry
)
899 dpm_async_fn(dev
, async_resume_early
);
901 while (!list_empty(&dpm_late_early_list
)) {
902 dev
= to_device(dpm_late_early_list
.next
);
904 list_move_tail(&dev
->power
.entry
, &dpm_suspended_list
);
905 mutex_unlock(&dpm_list_mtx
);
907 if (!is_async(dev
)) {
910 error
= device_resume_early(dev
, state
, false);
912 suspend_stats
.failed_resume_early
++;
913 dpm_save_failed_step(SUSPEND_RESUME_EARLY
);
914 dpm_save_failed_dev(dev_name(dev
));
915 pm_dev_err(dev
, state
, " early", error
);
918 mutex_lock(&dpm_list_mtx
);
921 mutex_unlock(&dpm_list_mtx
);
922 async_synchronize_full();
923 dpm_show_time(starttime
, state
, 0, "early");
924 trace_suspend_resume(TPS("dpm_resume_early"), state
.event
, false);
928 * dpm_resume_start - Execute "noirq" and "early" device callbacks.
929 * @state: PM transition of the system being carried out.
931 void dpm_resume_start(pm_message_t state
)
933 dpm_resume_noirq(state
);
934 dpm_resume_early(state
);
936 EXPORT_SYMBOL_GPL(dpm_resume_start
);
939 * device_resume - Execute "resume" callbacks for given device.
940 * @dev: Device to handle.
941 * @state: PM transition of the system being carried out.
942 * @async: If true, the device is being resumed asynchronously.
944 static int device_resume(struct device
*dev
, pm_message_t state
, bool async
)
946 pm_callback_t callback
= NULL
;
947 const char *info
= NULL
;
949 DECLARE_DPM_WATCHDOG_ON_STACK(wd
);
954 if (dev
->power
.syscore
)
957 if (dev
->power
.direct_complete
) {
958 /* Match the pm_runtime_disable() in __device_suspend(). */
959 pm_runtime_enable(dev
);
963 dpm_wait_for_superior(dev
, async
);
964 dpm_watchdog_set(&wd
, dev
);
968 * This is a fib. But we'll allow new children to be added below
969 * a resumed device, even if the device hasn't been completed yet.
971 dev
->power
.is_prepared
= false;
973 if (!dev
->power
.is_suspended
)
976 if (dev
->pm_domain
) {
977 info
= "power domain ";
978 callback
= pm_op(&dev
->pm_domain
->ops
, state
);
982 if (dev
->type
&& dev
->type
->pm
) {
984 callback
= pm_op(dev
->type
->pm
, state
);
988 if (dev
->class && dev
->class->pm
) {
990 callback
= pm_op(dev
->class->pm
, state
);
997 callback
= pm_op(dev
->bus
->pm
, state
);
998 } else if (dev
->bus
->resume
) {
999 info
= "legacy bus ";
1000 callback
= dev
->bus
->resume
;
1006 if (!callback
&& dev
->driver
&& dev
->driver
->pm
) {
1008 callback
= pm_op(dev
->driver
->pm
, state
);
1012 error
= dpm_run_callback(callback
, dev
, state
, info
);
1013 dev
->power
.is_suspended
= false;
1017 dpm_watchdog_clear(&wd
);
1020 complete_all(&dev
->power
.completion
);
1022 TRACE_RESUME(error
);
1027 static void async_resume(void *data
, async_cookie_t cookie
)
1029 struct device
*dev
= (struct device
*)data
;
1032 error
= device_resume(dev
, pm_transition
, true);
1034 pm_dev_err(dev
, pm_transition
, " async", error
);
1039 * dpm_resume - Execute "resume" callbacks for non-sysdev devices.
1040 * @state: PM transition of the system being carried out.
1042 * Execute the appropriate "resume" callback for all devices whose status
1043 * indicates that they are suspended.
1045 void dpm_resume(pm_message_t state
)
1048 ktime_t starttime
= ktime_get();
1050 trace_suspend_resume(TPS("dpm_resume"), state
.event
, true);
1053 mutex_lock(&dpm_list_mtx
);
1054 pm_transition
= state
;
1057 list_for_each_entry(dev
, &dpm_suspended_list
, power
.entry
)
1058 dpm_async_fn(dev
, async_resume
);
1060 while (!list_empty(&dpm_suspended_list
)) {
1061 dev
= to_device(dpm_suspended_list
.next
);
1063 if (!is_async(dev
)) {
1066 mutex_unlock(&dpm_list_mtx
);
1068 error
= device_resume(dev
, state
, false);
1070 suspend_stats
.failed_resume
++;
1071 dpm_save_failed_step(SUSPEND_RESUME
);
1072 dpm_save_failed_dev(dev_name(dev
));
1073 pm_dev_err(dev
, state
, "", error
);
1076 mutex_lock(&dpm_list_mtx
);
1078 if (!list_empty(&dev
->power
.entry
))
1079 list_move_tail(&dev
->power
.entry
, &dpm_prepared_list
);
1082 mutex_unlock(&dpm_list_mtx
);
1083 async_synchronize_full();
1084 dpm_show_time(starttime
, state
, 0, NULL
);
1088 trace_suspend_resume(TPS("dpm_resume"), state
.event
, false);
1092 * device_complete - Complete a PM transition for given device.
1093 * @dev: Device to handle.
1094 * @state: PM transition of the system being carried out.
1096 static void device_complete(struct device
*dev
, pm_message_t state
)
1098 void (*callback
)(struct device
*) = NULL
;
1099 const char *info
= NULL
;
1101 if (dev
->power
.syscore
)
1106 if (dev
->pm_domain
) {
1107 info
= "completing power domain ";
1108 callback
= dev
->pm_domain
->ops
.complete
;
1109 } else if (dev
->type
&& dev
->type
->pm
) {
1110 info
= "completing type ";
1111 callback
= dev
->type
->pm
->complete
;
1112 } else if (dev
->class && dev
->class->pm
) {
1113 info
= "completing class ";
1114 callback
= dev
->class->pm
->complete
;
1115 } else if (dev
->bus
&& dev
->bus
->pm
) {
1116 info
= "completing bus ";
1117 callback
= dev
->bus
->pm
->complete
;
1120 if (!callback
&& dev
->driver
&& dev
->driver
->pm
) {
1121 info
= "completing driver ";
1122 callback
= dev
->driver
->pm
->complete
;
1126 pm_dev_dbg(dev
, state
, info
);
1132 pm_runtime_put(dev
);
1136 * dpm_complete - Complete a PM transition for all non-sysdev devices.
1137 * @state: PM transition of the system being carried out.
1139 * Execute the ->complete() callbacks for all devices whose PM status is not
1140 * DPM_ON (this allows new devices to be registered).
1142 void dpm_complete(pm_message_t state
)
1144 struct list_head list
;
1146 trace_suspend_resume(TPS("dpm_complete"), state
.event
, true);
1149 INIT_LIST_HEAD(&list
);
1150 mutex_lock(&dpm_list_mtx
);
1151 while (!list_empty(&dpm_prepared_list
)) {
1152 struct device
*dev
= to_device(dpm_prepared_list
.prev
);
1155 dev
->power
.is_prepared
= false;
1156 list_move(&dev
->power
.entry
, &list
);
1157 mutex_unlock(&dpm_list_mtx
);
1159 trace_device_pm_callback_start(dev
, "", state
.event
);
1160 device_complete(dev
, state
);
1161 trace_device_pm_callback_end(dev
, 0);
1163 mutex_lock(&dpm_list_mtx
);
1166 list_splice(&list
, &dpm_list
);
1167 mutex_unlock(&dpm_list_mtx
);
1169 /* Allow device probing and trigger re-probing of deferred devices */
1170 device_unblock_probing();
1171 trace_suspend_resume(TPS("dpm_complete"), state
.event
, false);
1175 * dpm_resume_end - Execute "resume" callbacks and complete system transition.
1176 * @state: PM transition of the system being carried out.
1178 * Execute "resume" callbacks for all devices and complete the PM transition of
1181 void dpm_resume_end(pm_message_t state
)
1184 dpm_complete(state
);
1186 EXPORT_SYMBOL_GPL(dpm_resume_end
);
1189 /*------------------------- Suspend routines -------------------------*/
1192 * resume_event - Return a "resume" message for given "suspend" sleep state.
1193 * @sleep_state: PM message representing a sleep state.
1195 * Return a PM message representing the resume event corresponding to given
1198 static pm_message_t
resume_event(pm_message_t sleep_state
)
1200 switch (sleep_state
.event
) {
1201 case PM_EVENT_SUSPEND
:
1203 case PM_EVENT_FREEZE
:
1204 case PM_EVENT_QUIESCE
:
1205 return PMSG_RECOVER
;
1206 case PM_EVENT_HIBERNATE
:
1207 return PMSG_RESTORE
;
1212 static void dpm_superior_set_must_resume(struct device
*dev
)
1214 struct device_link
*link
;
1218 dev
->parent
->power
.must_resume
= true;
1220 idx
= device_links_read_lock();
1222 list_for_each_entry_rcu(link
, &dev
->links
.suppliers
, c_node
)
1223 link
->supplier
->power
.must_resume
= true;
1225 device_links_read_unlock(idx
);
1228 static pm_callback_t
dpm_subsys_suspend_noirq_cb(struct device
*dev
,
1230 const char **info_p
)
1232 pm_callback_t callback
;
1235 if (dev
->pm_domain
) {
1236 info
= "noirq power domain ";
1237 callback
= pm_noirq_op(&dev
->pm_domain
->ops
, state
);
1238 } else if (dev
->type
&& dev
->type
->pm
) {
1239 info
= "noirq type ";
1240 callback
= pm_noirq_op(dev
->type
->pm
, state
);
1241 } else if (dev
->class && dev
->class->pm
) {
1242 info
= "noirq class ";
1243 callback
= pm_noirq_op(dev
->class->pm
, state
);
1244 } else if (dev
->bus
&& dev
->bus
->pm
) {
1245 info
= "noirq bus ";
1246 callback
= pm_noirq_op(dev
->bus
->pm
, state
);
1257 static bool device_must_resume(struct device
*dev
, pm_message_t state
,
1258 bool no_subsys_suspend_noirq
)
1260 pm_message_t resume_msg
= resume_event(state
);
1263 * If all of the device driver's "noirq", "late" and "early" callbacks
1264 * are invoked directly by the core, the decision to allow the device to
1265 * stay in suspend can be based on its current runtime PM status and its
1268 if (no_subsys_suspend_noirq
&&
1269 !dpm_subsys_suspend_late_cb(dev
, state
, NULL
) &&
1270 !dpm_subsys_resume_early_cb(dev
, resume_msg
, NULL
) &&
1271 !dpm_subsys_resume_noirq_cb(dev
, resume_msg
, NULL
))
1272 return !pm_runtime_status_suspended(dev
) &&
1273 (resume_msg
.event
!= PM_EVENT_RESUME
||
1274 (device_can_wakeup(dev
) && !device_may_wakeup(dev
)));
1277 * The only safe strategy here is to require that if the device may not
1278 * be left in suspend, resume callbacks must be invoked for it.
1280 return !dev
->power
.may_skip_resume
;
1284 * __device_suspend_noirq - Execute a "noirq suspend" callback for given device.
1285 * @dev: Device to handle.
1286 * @state: PM transition of the system being carried out.
1287 * @async: If true, the device is being suspended asynchronously.
1289 * The driver of @dev will not receive interrupts while this function is being
1292 static int __device_suspend_noirq(struct device
*dev
, pm_message_t state
, bool async
)
1294 pm_callback_t callback
;
1296 bool no_subsys_cb
= false;
1302 dpm_wait_for_subordinate(dev
, async
);
1307 if (pm_wakeup_pending()) {
1308 async_error
= -EBUSY
;
1312 if (dev
->power
.syscore
|| dev
->power
.direct_complete
)
1315 callback
= dpm_subsys_suspend_noirq_cb(dev
, state
, &info
);
1319 no_subsys_cb
= !dpm_subsys_suspend_late_cb(dev
, state
, NULL
);
1321 if (dev_pm_smart_suspend_and_suspended(dev
) && no_subsys_cb
)
1324 if (dev
->driver
&& dev
->driver
->pm
) {
1325 info
= "noirq driver ";
1326 callback
= pm_noirq_op(dev
->driver
->pm
, state
);
1330 error
= dpm_run_callback(callback
, dev
, state
, info
);
1332 async_error
= error
;
1337 dev
->power
.is_noirq_suspended
= true;
1339 if (dev_pm_test_driver_flags(dev
, DPM_FLAG_LEAVE_SUSPENDED
)) {
1340 dev
->power
.must_resume
= dev
->power
.must_resume
||
1341 atomic_read(&dev
->power
.usage_count
) > 1 ||
1342 device_must_resume(dev
, state
, no_subsys_cb
);
1344 dev
->power
.must_resume
= true;
1347 if (dev
->power
.must_resume
)
1348 dpm_superior_set_must_resume(dev
);
1351 complete_all(&dev
->power
.completion
);
1352 TRACE_SUSPEND(error
);
1356 static void async_suspend_noirq(void *data
, async_cookie_t cookie
)
1358 struct device
*dev
= (struct device
*)data
;
1361 error
= __device_suspend_noirq(dev
, pm_transition
, true);
1363 dpm_save_failed_dev(dev_name(dev
));
1364 pm_dev_err(dev
, pm_transition
, " async", error
);
1370 static int device_suspend_noirq(struct device
*dev
)
1372 if (dpm_async_fn(dev
, async_suspend_noirq
))
1375 return __device_suspend_noirq(dev
, pm_transition
, false);
1378 void dpm_noirq_begin(void)
1381 device_wakeup_arm_wake_irqs();
1382 suspend_device_irqs();
1385 int dpm_noirq_suspend_devices(pm_message_t state
)
1387 ktime_t starttime
= ktime_get();
1390 trace_suspend_resume(TPS("dpm_suspend_noirq"), state
.event
, true);
1391 mutex_lock(&dpm_list_mtx
);
1392 pm_transition
= state
;
1395 while (!list_empty(&dpm_late_early_list
)) {
1396 struct device
*dev
= to_device(dpm_late_early_list
.prev
);
1399 mutex_unlock(&dpm_list_mtx
);
1401 error
= device_suspend_noirq(dev
);
1403 mutex_lock(&dpm_list_mtx
);
1405 pm_dev_err(dev
, state
, " noirq", error
);
1406 dpm_save_failed_dev(dev_name(dev
));
1410 if (!list_empty(&dev
->power
.entry
))
1411 list_move(&dev
->power
.entry
, &dpm_noirq_list
);
1417 mutex_unlock(&dpm_list_mtx
);
1418 async_synchronize_full();
1420 error
= async_error
;
1423 suspend_stats
.failed_suspend_noirq
++;
1424 dpm_save_failed_step(SUSPEND_SUSPEND_NOIRQ
);
1426 dpm_show_time(starttime
, state
, error
, "noirq");
1427 trace_suspend_resume(TPS("dpm_suspend_noirq"), state
.event
, false);
1432 * dpm_suspend_noirq - Execute "noirq suspend" callbacks for all devices.
1433 * @state: PM transition of the system being carried out.
1435 * Prevent device drivers' interrupt handlers from being called and invoke
1436 * "noirq" suspend callbacks for all non-sysdev devices.
1438 int dpm_suspend_noirq(pm_message_t state
)
1443 ret
= dpm_noirq_suspend_devices(state
);
1445 dpm_resume_noirq(resume_event(state
));
1450 static void dpm_propagate_wakeup_to_parent(struct device
*dev
)
1452 struct device
*parent
= dev
->parent
;
1457 spin_lock_irq(&parent
->power
.lock
);
1459 if (dev
->power
.wakeup_path
&& !parent
->power
.ignore_children
)
1460 parent
->power
.wakeup_path
= true;
1462 spin_unlock_irq(&parent
->power
.lock
);
1465 static pm_callback_t
dpm_subsys_suspend_late_cb(struct device
*dev
,
1467 const char **info_p
)
1469 pm_callback_t callback
;
1472 if (dev
->pm_domain
) {
1473 info
= "late power domain ";
1474 callback
= pm_late_early_op(&dev
->pm_domain
->ops
, state
);
1475 } else if (dev
->type
&& dev
->type
->pm
) {
1476 info
= "late type ";
1477 callback
= pm_late_early_op(dev
->type
->pm
, state
);
1478 } else if (dev
->class && dev
->class->pm
) {
1479 info
= "late class ";
1480 callback
= pm_late_early_op(dev
->class->pm
, state
);
1481 } else if (dev
->bus
&& dev
->bus
->pm
) {
1483 callback
= pm_late_early_op(dev
->bus
->pm
, state
);
1495 * __device_suspend_late - Execute a "late suspend" callback for given device.
1496 * @dev: Device to handle.
1497 * @state: PM transition of the system being carried out.
1498 * @async: If true, the device is being suspended asynchronously.
1500 * Runtime PM is disabled for @dev while this function is being executed.
1502 static int __device_suspend_late(struct device
*dev
, pm_message_t state
, bool async
)
1504 pm_callback_t callback
;
1511 __pm_runtime_disable(dev
, false);
1513 dpm_wait_for_subordinate(dev
, async
);
1518 if (pm_wakeup_pending()) {
1519 async_error
= -EBUSY
;
1523 if (dev
->power
.syscore
|| dev
->power
.direct_complete
)
1526 callback
= dpm_subsys_suspend_late_cb(dev
, state
, &info
);
1530 if (dev_pm_smart_suspend_and_suspended(dev
) &&
1531 !dpm_subsys_suspend_noirq_cb(dev
, state
, NULL
))
1534 if (dev
->driver
&& dev
->driver
->pm
) {
1535 info
= "late driver ";
1536 callback
= pm_late_early_op(dev
->driver
->pm
, state
);
1540 error
= dpm_run_callback(callback
, dev
, state
, info
);
1542 async_error
= error
;
1545 dpm_propagate_wakeup_to_parent(dev
);
1548 dev
->power
.is_late_suspended
= true;
1551 TRACE_SUSPEND(error
);
1552 complete_all(&dev
->power
.completion
);
1556 static void async_suspend_late(void *data
, async_cookie_t cookie
)
1558 struct device
*dev
= (struct device
*)data
;
1561 error
= __device_suspend_late(dev
, pm_transition
, true);
1563 dpm_save_failed_dev(dev_name(dev
));
1564 pm_dev_err(dev
, pm_transition
, " async", error
);
1569 static int device_suspend_late(struct device
*dev
)
1571 if (dpm_async_fn(dev
, async_suspend_late
))
1574 return __device_suspend_late(dev
, pm_transition
, false);
1578 * dpm_suspend_late - Execute "late suspend" callbacks for all devices.
1579 * @state: PM transition of the system being carried out.
1581 int dpm_suspend_late(pm_message_t state
)
1583 ktime_t starttime
= ktime_get();
1586 trace_suspend_resume(TPS("dpm_suspend_late"), state
.event
, true);
1587 mutex_lock(&dpm_list_mtx
);
1588 pm_transition
= state
;
1591 while (!list_empty(&dpm_suspended_list
)) {
1592 struct device
*dev
= to_device(dpm_suspended_list
.prev
);
1595 mutex_unlock(&dpm_list_mtx
);
1597 error
= device_suspend_late(dev
);
1599 mutex_lock(&dpm_list_mtx
);
1600 if (!list_empty(&dev
->power
.entry
))
1601 list_move(&dev
->power
.entry
, &dpm_late_early_list
);
1604 pm_dev_err(dev
, state
, " late", error
);
1605 dpm_save_failed_dev(dev_name(dev
));
1614 mutex_unlock(&dpm_list_mtx
);
1615 async_synchronize_full();
1617 error
= async_error
;
1619 suspend_stats
.failed_suspend_late
++;
1620 dpm_save_failed_step(SUSPEND_SUSPEND_LATE
);
1621 dpm_resume_early(resume_event(state
));
1623 dpm_show_time(starttime
, state
, error
, "late");
1624 trace_suspend_resume(TPS("dpm_suspend_late"), state
.event
, false);
1629 * dpm_suspend_end - Execute "late" and "noirq" device suspend callbacks.
1630 * @state: PM transition of the system being carried out.
1632 int dpm_suspend_end(pm_message_t state
)
1634 int error
= dpm_suspend_late(state
);
1638 error
= dpm_suspend_noirq(state
);
1640 dpm_resume_early(resume_event(state
));
1646 EXPORT_SYMBOL_GPL(dpm_suspend_end
);
1649 * legacy_suspend - Execute a legacy (bus or class) suspend callback for device.
1650 * @dev: Device to suspend.
1651 * @state: PM transition of the system being carried out.
1652 * @cb: Suspend callback to execute.
1653 * @info: string description of caller.
1655 static int legacy_suspend(struct device
*dev
, pm_message_t state
,
1656 int (*cb
)(struct device
*dev
, pm_message_t state
),
1662 calltime
= initcall_debug_start(dev
, cb
);
1664 trace_device_pm_callback_start(dev
, info
, state
.event
);
1665 error
= cb(dev
, state
);
1666 trace_device_pm_callback_end(dev
, error
);
1667 suspend_report_result(cb
, error
);
1669 initcall_debug_report(dev
, calltime
, cb
, error
);
1674 static void dpm_clear_superiors_direct_complete(struct device
*dev
)
1676 struct device_link
*link
;
1680 spin_lock_irq(&dev
->parent
->power
.lock
);
1681 dev
->parent
->power
.direct_complete
= false;
1682 spin_unlock_irq(&dev
->parent
->power
.lock
);
1685 idx
= device_links_read_lock();
1687 list_for_each_entry_rcu(link
, &dev
->links
.suppliers
, c_node
) {
1688 spin_lock_irq(&link
->supplier
->power
.lock
);
1689 link
->supplier
->power
.direct_complete
= false;
1690 spin_unlock_irq(&link
->supplier
->power
.lock
);
1693 device_links_read_unlock(idx
);
1697 * __device_suspend - Execute "suspend" callbacks for given device.
1698 * @dev: Device to handle.
1699 * @state: PM transition of the system being carried out.
1700 * @async: If true, the device is being suspended asynchronously.
1702 static int __device_suspend(struct device
*dev
, pm_message_t state
, bool async
)
1704 pm_callback_t callback
= NULL
;
1705 const char *info
= NULL
;
1707 DECLARE_DPM_WATCHDOG_ON_STACK(wd
);
1712 dpm_wait_for_subordinate(dev
, async
);
1715 dev
->power
.direct_complete
= false;
1720 * If a device configured to wake up the system from sleep states
1721 * has been suspended at run time and there's a resume request pending
1722 * for it, this is equivalent to the device signaling wakeup, so the
1723 * system suspend operation should be aborted.
1725 if (pm_runtime_barrier(dev
) && device_may_wakeup(dev
))
1726 pm_wakeup_event(dev
, 0);
1728 if (pm_wakeup_pending()) {
1729 dev
->power
.direct_complete
= false;
1730 async_error
= -EBUSY
;
1734 if (dev
->power
.syscore
)
1737 /* Avoid direct_complete to let wakeup_path propagate. */
1738 if (device_may_wakeup(dev
) || dev
->power
.wakeup_path
)
1739 dev
->power
.direct_complete
= false;
1741 if (dev
->power
.direct_complete
) {
1742 if (pm_runtime_status_suspended(dev
)) {
1743 pm_runtime_disable(dev
);
1744 if (pm_runtime_status_suspended(dev
)) {
1745 pm_dev_dbg(dev
, state
, "direct-complete ");
1749 pm_runtime_enable(dev
);
1751 dev
->power
.direct_complete
= false;
1754 dev
->power
.may_skip_resume
= false;
1755 dev
->power
.must_resume
= false;
1757 dpm_watchdog_set(&wd
, dev
);
1760 if (dev
->pm_domain
) {
1761 info
= "power domain ";
1762 callback
= pm_op(&dev
->pm_domain
->ops
, state
);
1766 if (dev
->type
&& dev
->type
->pm
) {
1768 callback
= pm_op(dev
->type
->pm
, state
);
1772 if (dev
->class && dev
->class->pm
) {
1774 callback
= pm_op(dev
->class->pm
, state
);
1781 callback
= pm_op(dev
->bus
->pm
, state
);
1782 } else if (dev
->bus
->suspend
) {
1783 pm_dev_dbg(dev
, state
, "legacy bus ");
1784 error
= legacy_suspend(dev
, state
, dev
->bus
->suspend
,
1791 if (!callback
&& dev
->driver
&& dev
->driver
->pm
) {
1793 callback
= pm_op(dev
->driver
->pm
, state
);
1796 error
= dpm_run_callback(callback
, dev
, state
, info
);
1800 dev
->power
.is_suspended
= true;
1801 if (device_may_wakeup(dev
))
1802 dev
->power
.wakeup_path
= true;
1804 dpm_propagate_wakeup_to_parent(dev
);
1805 dpm_clear_superiors_direct_complete(dev
);
1809 dpm_watchdog_clear(&wd
);
1813 async_error
= error
;
1815 complete_all(&dev
->power
.completion
);
1816 TRACE_SUSPEND(error
);
1820 static void async_suspend(void *data
, async_cookie_t cookie
)
1822 struct device
*dev
= (struct device
*)data
;
1825 error
= __device_suspend(dev
, pm_transition
, true);
1827 dpm_save_failed_dev(dev_name(dev
));
1828 pm_dev_err(dev
, pm_transition
, " async", error
);
1834 static int device_suspend(struct device
*dev
)
1836 if (dpm_async_fn(dev
, async_suspend
))
1839 return __device_suspend(dev
, pm_transition
, false);
1843 * dpm_suspend - Execute "suspend" callbacks for all non-sysdev devices.
1844 * @state: PM transition of the system being carried out.
1846 int dpm_suspend(pm_message_t state
)
1848 ktime_t starttime
= ktime_get();
1851 trace_suspend_resume(TPS("dpm_suspend"), state
.event
, true);
1857 mutex_lock(&dpm_list_mtx
);
1858 pm_transition
= state
;
1860 while (!list_empty(&dpm_prepared_list
)) {
1861 struct device
*dev
= to_device(dpm_prepared_list
.prev
);
1864 mutex_unlock(&dpm_list_mtx
);
1866 error
= device_suspend(dev
);
1868 mutex_lock(&dpm_list_mtx
);
1870 pm_dev_err(dev
, state
, "", error
);
1871 dpm_save_failed_dev(dev_name(dev
));
1875 if (!list_empty(&dev
->power
.entry
))
1876 list_move(&dev
->power
.entry
, &dpm_suspended_list
);
1881 mutex_unlock(&dpm_list_mtx
);
1882 async_synchronize_full();
1884 error
= async_error
;
1886 suspend_stats
.failed_suspend
++;
1887 dpm_save_failed_step(SUSPEND_SUSPEND
);
1889 dpm_show_time(starttime
, state
, error
, NULL
);
1890 trace_suspend_resume(TPS("dpm_suspend"), state
.event
, false);
1895 * device_prepare - Prepare a device for system power transition.
1896 * @dev: Device to handle.
1897 * @state: PM transition of the system being carried out.
1899 * Execute the ->prepare() callback(s) for given device. No new children of the
1900 * device may be registered after this function has returned.
1902 static int device_prepare(struct device
*dev
, pm_message_t state
)
1904 int (*callback
)(struct device
*) = NULL
;
1907 if (dev
->power
.syscore
)
1910 WARN_ON(!pm_runtime_enabled(dev
) &&
1911 dev_pm_test_driver_flags(dev
, DPM_FLAG_SMART_SUSPEND
|
1912 DPM_FLAG_LEAVE_SUSPENDED
));
1915 * If a device's parent goes into runtime suspend at the wrong time,
1916 * it won't be possible to resume the device. To prevent this we
1917 * block runtime suspend here, during the prepare phase, and allow
1918 * it again during the complete phase.
1920 pm_runtime_get_noresume(dev
);
1924 dev
->power
.wakeup_path
= false;
1926 if (dev
->power
.no_pm_callbacks
)
1930 callback
= dev
->pm_domain
->ops
.prepare
;
1931 else if (dev
->type
&& dev
->type
->pm
)
1932 callback
= dev
->type
->pm
->prepare
;
1933 else if (dev
->class && dev
->class->pm
)
1934 callback
= dev
->class->pm
->prepare
;
1935 else if (dev
->bus
&& dev
->bus
->pm
)
1936 callback
= dev
->bus
->pm
->prepare
;
1938 if (!callback
&& dev
->driver
&& dev
->driver
->pm
)
1939 callback
= dev
->driver
->pm
->prepare
;
1942 ret
= callback(dev
);
1948 suspend_report_result(callback
, ret
);
1949 pm_runtime_put(dev
);
1953 * A positive return value from ->prepare() means "this device appears
1954 * to be runtime-suspended and its state is fine, so if it really is
1955 * runtime-suspended, you can leave it in that state provided that you
1956 * will do the same thing with all of its descendants". This only
1957 * applies to suspend transitions, however.
1959 spin_lock_irq(&dev
->power
.lock
);
1960 dev
->power
.direct_complete
= state
.event
== PM_EVENT_SUSPEND
&&
1961 ((pm_runtime_suspended(dev
) && ret
> 0) ||
1962 dev
->power
.no_pm_callbacks
) &&
1963 !dev_pm_test_driver_flags(dev
, DPM_FLAG_NEVER_SKIP
);
1964 spin_unlock_irq(&dev
->power
.lock
);
1969 * dpm_prepare - Prepare all non-sysdev devices for a system PM transition.
1970 * @state: PM transition of the system being carried out.
1972 * Execute the ->prepare() callback(s) for all devices.
1974 int dpm_prepare(pm_message_t state
)
1978 trace_suspend_resume(TPS("dpm_prepare"), state
.event
, true);
1982 * Give a chance for the known devices to complete their probes, before
1983 * disable probing of devices. This sync point is important at least
1984 * at boot time + hibernation restore.
1986 wait_for_device_probe();
1988 * It is unsafe if probing of devices will happen during suspend or
1989 * hibernation and system behavior will be unpredictable in this case.
1990 * So, let's prohibit device's probing here and defer their probes
1991 * instead. The normal behavior will be restored in dpm_complete().
1993 device_block_probing();
1995 mutex_lock(&dpm_list_mtx
);
1996 while (!list_empty(&dpm_list
)) {
1997 struct device
*dev
= to_device(dpm_list
.next
);
2000 mutex_unlock(&dpm_list_mtx
);
2002 trace_device_pm_callback_start(dev
, "", state
.event
);
2003 error
= device_prepare(dev
, state
);
2004 trace_device_pm_callback_end(dev
, error
);
2006 mutex_lock(&dpm_list_mtx
);
2008 if (error
== -EAGAIN
) {
2013 pr_info("Device %s not prepared for power transition: code %d\n",
2014 dev_name(dev
), error
);
2018 dev
->power
.is_prepared
= true;
2019 if (!list_empty(&dev
->power
.entry
))
2020 list_move_tail(&dev
->power
.entry
, &dpm_prepared_list
);
2023 mutex_unlock(&dpm_list_mtx
);
2024 trace_suspend_resume(TPS("dpm_prepare"), state
.event
, false);
2029 * dpm_suspend_start - Prepare devices for PM transition and suspend them.
2030 * @state: PM transition of the system being carried out.
2032 * Prepare all non-sysdev devices for system PM transition and execute "suspend"
2033 * callbacks for them.
2035 int dpm_suspend_start(pm_message_t state
)
2039 error
= dpm_prepare(state
);
2041 suspend_stats
.failed_prepare
++;
2042 dpm_save_failed_step(SUSPEND_PREPARE
);
2044 error
= dpm_suspend(state
);
2047 EXPORT_SYMBOL_GPL(dpm_suspend_start
);
2049 void __suspend_report_result(const char *function
, void *fn
, int ret
)
2052 pr_err("%s(): %pS returns %d\n", function
, fn
, ret
);
2054 EXPORT_SYMBOL_GPL(__suspend_report_result
);
2057 * device_pm_wait_for_dev - Wait for suspend/resume of a device to complete.
2058 * @subordinate: Device that needs to wait for @dev.
2059 * @dev: Device to wait for.
2061 int device_pm_wait_for_dev(struct device
*subordinate
, struct device
*dev
)
2063 dpm_wait(dev
, subordinate
->power
.async_suspend
);
2066 EXPORT_SYMBOL_GPL(device_pm_wait_for_dev
);
2069 * dpm_for_each_dev - device iterator.
2070 * @data: data for the callback.
2071 * @fn: function to be called for each device.
2073 * Iterate over devices in dpm_list, and call @fn for each device,
2076 void dpm_for_each_dev(void *data
, void (*fn
)(struct device
*, void *))
2084 list_for_each_entry(dev
, &dpm_list
, power
.entry
)
2088 EXPORT_SYMBOL_GPL(dpm_for_each_dev
);
2090 static bool pm_ops_is_empty(const struct dev_pm_ops
*ops
)
2095 return !ops
->prepare
&&
2097 !ops
->suspend_late
&&
2098 !ops
->suspend_noirq
&&
2099 !ops
->resume_noirq
&&
2100 !ops
->resume_early
&&
2105 void device_pm_check_callbacks(struct device
*dev
)
2107 spin_lock_irq(&dev
->power
.lock
);
2108 dev
->power
.no_pm_callbacks
=
2109 (!dev
->bus
|| (pm_ops_is_empty(dev
->bus
->pm
) &&
2110 !dev
->bus
->suspend
&& !dev
->bus
->resume
)) &&
2111 (!dev
->class || pm_ops_is_empty(dev
->class->pm
)) &&
2112 (!dev
->type
|| pm_ops_is_empty(dev
->type
->pm
)) &&
2113 (!dev
->pm_domain
|| pm_ops_is_empty(&dev
->pm_domain
->ops
)) &&
2114 (!dev
->driver
|| (pm_ops_is_empty(dev
->driver
->pm
) &&
2115 !dev
->driver
->suspend
&& !dev
->driver
->resume
));
2116 spin_unlock_irq(&dev
->power
.lock
);
2119 bool dev_pm_smart_suspend_and_suspended(struct device
*dev
)
2121 return dev_pm_test_driver_flags(dev
, DPM_FLAG_SMART_SUSPEND
) &&
2122 pm_runtime_status_suspended(dev
);